]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/hwmon/lm75.c
Merge with /shiny/git/linux-2.6/.git
[karo-tx-linux.git] / drivers / hwmon / lm75.c
1 /*
2     lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-sensor.h>
27 #include "lm75.h"
28
29
30 /* Addresses to scan */
31 static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
32                                         0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
33 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
34
35 /* Insmod parameters */
36 SENSORS_INSMOD_1(lm75);
37
38 /* Many LM75 constants specified below */
39
40 /* The LM75 registers */
41 #define LM75_REG_TEMP           0x00
42 #define LM75_REG_CONF           0x01
43 #define LM75_REG_TEMP_HYST      0x02
44 #define LM75_REG_TEMP_OS        0x03
45
46 /* Each client has this additional data */
47 struct lm75_data {
48         struct i2c_client       client;
49         struct semaphore        update_lock;
50         char                    valid;          /* !=0 if following fields are valid */
51         unsigned long           last_updated;   /* In jiffies */
52         u16                     temp_input;     /* Register values */
53         u16                     temp_max;
54         u16                     temp_hyst;
55 };
56
57 static int lm75_attach_adapter(struct i2c_adapter *adapter);
58 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
59 static void lm75_init_client(struct i2c_client *client);
60 static int lm75_detach_client(struct i2c_client *client);
61 static int lm75_read_value(struct i2c_client *client, u8 reg);
62 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
63 static struct lm75_data *lm75_update_device(struct device *dev);
64
65
66 /* This is the driver that will be inserted */
67 static struct i2c_driver lm75_driver = {
68         .owner          = THIS_MODULE,
69         .name           = "lm75",
70         .id             = I2C_DRIVERID_LM75,
71         .flags          = I2C_DF_NOTIFY,
72         .attach_adapter = lm75_attach_adapter,
73         .detach_client  = lm75_detach_client,
74 };
75
76 #define show(value)     \
77 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)               \
78 {                                                                       \
79         struct lm75_data *data = lm75_update_device(dev);               \
80         return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value));   \
81 }
82 show(temp_max);
83 show(temp_hyst);
84 show(temp_input);
85
86 #define set(value, reg) \
87 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)    \
88 {                                                               \
89         struct i2c_client *client = to_i2c_client(dev);         \
90         struct lm75_data *data = i2c_get_clientdata(client);    \
91         int temp = simple_strtoul(buf, NULL, 10);               \
92                                                                 \
93         down(&data->update_lock);                               \
94         data->value = LM75_TEMP_TO_REG(temp);                   \
95         lm75_write_value(client, reg, data->value);             \
96         up(&data->update_lock);                                 \
97         return count;                                           \
98 }
99 set(temp_max, LM75_REG_TEMP_OS);
100 set(temp_hyst, LM75_REG_TEMP_HYST);
101
102 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
103 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
104 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
105
106 static int lm75_attach_adapter(struct i2c_adapter *adapter)
107 {
108         if (!(adapter->class & I2C_CLASS_HWMON))
109                 return 0;
110         return i2c_detect(adapter, &addr_data, lm75_detect);
111 }
112
113 /* This function is called by i2c_detect */
114 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
115 {
116         int i;
117         struct i2c_client *new_client;
118         struct lm75_data *data;
119         int err = 0;
120         const char *name = "";
121
122         /* Make sure we aren't probing the ISA bus!! This is just a safety check
123            at this moment; i2c_detect really won't call us. */
124 #ifdef DEBUG
125         if (i2c_is_isa_adapter(adapter)) {
126                 dev_dbg(&adapter->dev,
127                         "lm75_detect called for an ISA bus adapter?!?\n");
128                 goto exit;
129         }
130 #endif
131
132         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
133                                      I2C_FUNC_SMBUS_WORD_DATA))
134                 goto exit;
135
136         /* OK. For now, we presume we have a valid client. We now create the
137            client structure, even though we cannot fill it completely yet.
138            But it allows us to access lm75_{read,write}_value. */
139         if (!(data = kmalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
140                 err = -ENOMEM;
141                 goto exit;
142         }
143         memset(data, 0, sizeof(struct lm75_data));
144
145         new_client = &data->client;
146         i2c_set_clientdata(new_client, data);
147         new_client->addr = address;
148         new_client->adapter = adapter;
149         new_client->driver = &lm75_driver;
150         new_client->flags = 0;
151
152         /* Now, we do the remaining detection. There is no identification-
153            dedicated register so we have to rely on several tricks:
154            unused bits, registers cycling over 8-address boundaries,
155            addresses 0x04-0x07 returning the last read value.
156            The cycling+unused addresses combination is not tested,
157            since it would significantly slow the detection down and would
158            hardly add any value. */
159         if (kind < 0) {
160                 int cur, conf, hyst, os;
161
162                 /* Unused addresses */
163                 cur = i2c_smbus_read_word_data(new_client, 0);
164                 conf = i2c_smbus_read_byte_data(new_client, 1);
165                 hyst = i2c_smbus_read_word_data(new_client, 2);
166                 if (i2c_smbus_read_word_data(new_client, 4) != hyst
167                  || i2c_smbus_read_word_data(new_client, 5) != hyst
168                  || i2c_smbus_read_word_data(new_client, 6) != hyst
169                  || i2c_smbus_read_word_data(new_client, 7) != hyst)
170                         goto exit_free;
171                 os = i2c_smbus_read_word_data(new_client, 3);
172                 if (i2c_smbus_read_word_data(new_client, 4) != os
173                  || i2c_smbus_read_word_data(new_client, 5) != os
174                  || i2c_smbus_read_word_data(new_client, 6) != os
175                  || i2c_smbus_read_word_data(new_client, 7) != os)
176                         goto exit_free;
177
178                 /* Unused bits */
179                 if (conf & 0xe0)
180                         goto exit_free;
181
182                 /* Addresses cycling */
183                 for (i = 8; i < 0xff; i += 8)
184                         if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
185                          || i2c_smbus_read_word_data(new_client, i + 2) != hyst
186                          || i2c_smbus_read_word_data(new_client, i + 3) != os)
187                                 goto exit_free;
188         }
189
190         /* Determine the chip type - only one kind supported! */
191         if (kind <= 0)
192                 kind = lm75;
193
194         if (kind == lm75) {
195                 name = "lm75";
196         }
197
198         /* Fill in the remaining client fields and put it into the global list */
199         strlcpy(new_client->name, name, I2C_NAME_SIZE);
200         data->valid = 0;
201         init_MUTEX(&data->update_lock);
202
203         /* Tell the I2C layer a new client has arrived */
204         if ((err = i2c_attach_client(new_client)))
205                 goto exit_free;
206
207         /* Initialize the LM75 chip */
208         lm75_init_client(new_client);
209         
210         /* Register sysfs hooks */
211         device_create_file(&new_client->dev, &dev_attr_temp1_max);
212         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
213         device_create_file(&new_client->dev, &dev_attr_temp1_input);
214
215         return 0;
216
217 exit_free:
218         kfree(data);
219 exit:
220         return err;
221 }
222
223 static int lm75_detach_client(struct i2c_client *client)
224 {
225         i2c_detach_client(client);
226         kfree(i2c_get_clientdata(client));
227         return 0;
228 }
229
230 /* All registers are word-sized, except for the configuration register.
231    LM75 uses a high-byte first convention, which is exactly opposite to
232    the usual practice. */
233 static int lm75_read_value(struct i2c_client *client, u8 reg)
234 {
235         if (reg == LM75_REG_CONF)
236                 return i2c_smbus_read_byte_data(client, reg);
237         else
238                 return swab16(i2c_smbus_read_word_data(client, reg));
239 }
240
241 /* All registers are word-sized, except for the configuration register.
242    LM75 uses a high-byte first convention, which is exactly opposite to
243    the usual practice. */
244 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
245 {
246         if (reg == LM75_REG_CONF)
247                 return i2c_smbus_write_byte_data(client, reg, value);
248         else
249                 return i2c_smbus_write_word_data(client, reg, swab16(value));
250 }
251
252 static void lm75_init_client(struct i2c_client *client)
253 {
254         /* Initialize the LM75 chip */
255         lm75_write_value(client, LM75_REG_CONF, 0);
256 }
257
258 static struct lm75_data *lm75_update_device(struct device *dev)
259 {
260         struct i2c_client *client = to_i2c_client(dev);
261         struct lm75_data *data = i2c_get_clientdata(client);
262
263         down(&data->update_lock);
264
265         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
266             || !data->valid) {
267                 dev_dbg(&client->dev, "Starting lm75 update\n");
268
269                 data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
270                 data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
271                 data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
272                 data->last_updated = jiffies;
273                 data->valid = 1;
274         }
275
276         up(&data->update_lock);
277
278         return data;
279 }
280
281 static int __init sensors_lm75_init(void)
282 {
283         return i2c_add_driver(&lm75_driver);
284 }
285
286 static void __exit sensors_lm75_exit(void)
287 {
288         i2c_del_driver(&lm75_driver);
289 }
290
291 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
292 MODULE_DESCRIPTION("LM75 driver");
293 MODULE_LICENSE("GPL");
294
295 module_init(sensors_lm75_init);
296 module_exit(sensors_lm75_exit);