hwmon: (lm75) cleanup/reorg
[linux-2.6] / 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/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include "lm75.h"
31
32
33 /*
34  * This driver handles the LM75 and compatible digital temperature sensors.
35  * Compatibles include at least the DS75, DS1775, MCP980x, STDS75, TCN75,
36  * TMP100, TMP101, TMP75, TMP175, and TMP275.
37  */
38
39 /* Addresses scanned by legacy style driver binding */
40 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
41                                         0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
42
43 /* Insmod parameters (only for legacy style driver binding) */
44 I2C_CLIENT_INSMOD_1(lm75);
45
46
47 /* The LM75 registers */
48 #define LM75_REG_CONF           0x01
49 static const u8 LM75_REG_TEMP[3] = {
50         0x00,           /* input */
51         0x03,           /* max */
52         0x02,           /* hyst */
53 };
54
55 /* Each client has this additional data */
56 struct lm75_data {
57         struct i2c_client       client;
58         struct device           *hwmon_dev;
59         struct mutex            update_lock;
60         char                    valid;          /* !=0 if registers are valid */
61         unsigned long           last_updated;   /* In jiffies */
62         u16                     temp[3];        /* Register values,
63                                                    0 = input
64                                                    1 = max
65                                                    2 = hyst */
66 };
67
68 static void lm75_init_client(struct i2c_client *client);
69 static int lm75_read_value(struct i2c_client *client, u8 reg);
70 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
71 static struct lm75_data *lm75_update_device(struct device *dev);
72
73
74 /*-----------------------------------------------------------------------*/
75
76 /* sysfs attributes for hwmon */
77
78 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
79                          char *buf)
80 {
81         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
82         struct lm75_data *data = lm75_update_device(dev);
83         return sprintf(buf, "%d\n",
84                        LM75_TEMP_FROM_REG(data->temp[attr->index]));
85 }
86
87 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
88                         const char *buf, size_t count)
89 {
90         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
91         struct i2c_client *client = to_i2c_client(dev);
92         struct lm75_data *data = i2c_get_clientdata(client);
93         int nr = attr->index;
94         long temp = simple_strtol(buf, NULL, 10);
95
96         mutex_lock(&data->update_lock);
97         data->temp[nr] = LM75_TEMP_TO_REG(temp);
98         lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
99         mutex_unlock(&data->update_lock);
100         return count;
101 }
102
103 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
104                         show_temp, set_temp, 1);
105 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
106                         show_temp, set_temp, 2);
107 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
108
109 static struct attribute *lm75_attributes[] = {
110         &sensor_dev_attr_temp1_input.dev_attr.attr,
111         &sensor_dev_attr_temp1_max.dev_attr.attr,
112         &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
113
114         NULL
115 };
116
117 static const struct attribute_group lm75_group = {
118         .attrs = lm75_attributes,
119 };
120
121 /*-----------------------------------------------------------------------*/
122
123 /* "Legacy" I2C driver binding */
124
125 static struct i2c_driver lm75_driver;
126
127 /* This function is called by i2c_probe */
128 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
129 {
130         int i;
131         struct i2c_client *new_client;
132         struct lm75_data *data;
133         int err = 0;
134
135         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
136                                      I2C_FUNC_SMBUS_WORD_DATA))
137                 goto exit;
138
139         /* OK. For now, we presume we have a valid address. We create the
140            client structure, even though there may be no sensor present.
141            But it allows us to use i2c_smbus_read_*_data() calls. */
142         if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
143                 err = -ENOMEM;
144                 goto exit;
145         }
146
147         new_client = &data->client;
148         i2c_set_clientdata(new_client, data);
149         new_client->addr = address;
150         new_client->adapter = adapter;
151         new_client->driver = &lm75_driver;
152         new_client->flags = 0;
153
154         /* Now, we do the remaining detection. There is no identification-
155            dedicated register so we have to rely on several tricks:
156            unused bits, registers cycling over 8-address boundaries,
157            addresses 0x04-0x07 returning the last read value.
158            The cycling+unused addresses combination is not tested,
159            since it would significantly slow the detection down and would
160            hardly add any value. */
161         if (kind < 0) {
162                 int cur, conf, hyst, os;
163
164                 /* Unused addresses */
165                 cur = i2c_smbus_read_word_data(new_client, 0);
166                 conf = i2c_smbus_read_byte_data(new_client, 1);
167                 hyst = i2c_smbus_read_word_data(new_client, 2);
168                 if (i2c_smbus_read_word_data(new_client, 4) != hyst
169                  || i2c_smbus_read_word_data(new_client, 5) != hyst
170                  || i2c_smbus_read_word_data(new_client, 6) != hyst
171                  || i2c_smbus_read_word_data(new_client, 7) != hyst)
172                         goto exit_free;
173                 os = i2c_smbus_read_word_data(new_client, 3);
174                 if (i2c_smbus_read_word_data(new_client, 4) != os
175                  || i2c_smbus_read_word_data(new_client, 5) != os
176                  || i2c_smbus_read_word_data(new_client, 6) != os
177                  || i2c_smbus_read_word_data(new_client, 7) != os)
178                         goto exit_free;
179
180                 /* Unused bits */
181                 if (conf & 0xe0)
182                         goto exit_free;
183
184                 /* Addresses cycling */
185                 for (i = 8; i < 0xff; i += 8)
186                         if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
187                          || i2c_smbus_read_word_data(new_client, i + 2) != hyst
188                          || i2c_smbus_read_word_data(new_client, i + 3) != os)
189                                 goto exit_free;
190         }
191
192         /* NOTE: we treat "force=..." and "force_lm75=..." the same. */
193         strlcpy(new_client->name, "lm75", I2C_NAME_SIZE);
194
195         /* Fill in the remaining client fields and put it into the global list */
196         data->valid = 0;
197         mutex_init(&data->update_lock);
198
199         /* Tell the I2C layer a new client has arrived */
200         if ((err = i2c_attach_client(new_client)))
201                 goto exit_free;
202
203         /* Initialize the LM75 chip */
204         lm75_init_client(new_client);
205
206         /* Register sysfs hooks */
207         if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group)))
208                 goto exit_detach;
209
210         data->hwmon_dev = hwmon_device_register(&new_client->dev);
211         if (IS_ERR(data->hwmon_dev)) {
212                 err = PTR_ERR(data->hwmon_dev);
213                 goto exit_remove;
214         }
215
216         return 0;
217
218 exit_remove:
219         sysfs_remove_group(&new_client->dev.kobj, &lm75_group);
220 exit_detach:
221         i2c_detach_client(new_client);
222 exit_free:
223         kfree(data);
224 exit:
225         return err;
226 }
227
228 static int lm75_attach_adapter(struct i2c_adapter *adapter)
229 {
230         if (!(adapter->class & I2C_CLASS_HWMON))
231                 return 0;
232         return i2c_probe(adapter, &addr_data, lm75_detect);
233 }
234
235 static int lm75_detach_client(struct i2c_client *client)
236 {
237         struct lm75_data *data = i2c_get_clientdata(client);
238         hwmon_device_unregister(data->hwmon_dev);
239         sysfs_remove_group(&client->dev.kobj, &lm75_group);
240         i2c_detach_client(client);
241         kfree(data);
242         return 0;
243 }
244
245 static struct i2c_driver lm75_driver = {
246         .driver = {
247                 .name   = "lm75",
248         },
249         .attach_adapter = lm75_attach_adapter,
250         .detach_client  = lm75_detach_client,
251 };
252
253 /*-----------------------------------------------------------------------*/
254
255 /* register access */
256
257 /* All registers are word-sized, except for the configuration register.
258    LM75 uses a high-byte first convention, which is exactly opposite to
259    the SMBus standard. */
260 static int lm75_read_value(struct i2c_client *client, u8 reg)
261 {
262         int value;
263
264         if (reg == LM75_REG_CONF)
265                 return i2c_smbus_read_byte_data(client, reg);
266
267         value = i2c_smbus_read_word_data(client, reg);
268         return (value < 0) ? value : swab16(value);
269 }
270
271 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
272 {
273         if (reg == LM75_REG_CONF)
274                 return i2c_smbus_write_byte_data(client, reg, value);
275         else
276                 return i2c_smbus_write_word_data(client, reg, swab16(value));
277 }
278
279 static void lm75_init_client(struct i2c_client *client)
280 {
281         int reg;
282
283         /* Enable if in shutdown mode */
284         reg = lm75_read_value(client, LM75_REG_CONF);
285         if (reg >= 0 && (reg & 0x01))
286                 lm75_write_value(client, LM75_REG_CONF, reg & 0xfe);
287 }
288
289 static struct lm75_data *lm75_update_device(struct device *dev)
290 {
291         struct i2c_client *client = to_i2c_client(dev);
292         struct lm75_data *data = i2c_get_clientdata(client);
293
294         mutex_lock(&data->update_lock);
295
296         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
297             || !data->valid) {
298                 int i;
299                 dev_dbg(&client->dev, "Starting lm75 update\n");
300
301                 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
302                         int status;
303
304                         status = lm75_read_value(client, LM75_REG_TEMP[i]);
305                         if (status < 0)
306                                 dev_dbg(&client->dev, "reg %d, err %d\n",
307                                                 LM75_REG_TEMP[i], status);
308                         else
309                                 data->temp[i] = status;
310                 }
311                 data->last_updated = jiffies;
312                 data->valid = 1;
313         }
314
315         mutex_unlock(&data->update_lock);
316
317         return data;
318 }
319
320 /*-----------------------------------------------------------------------*/
321
322 /* module glue */
323
324 static int __init sensors_lm75_init(void)
325 {
326         return i2c_add_driver(&lm75_driver);
327 }
328
329 static void __exit sensors_lm75_exit(void)
330 {
331         i2c_del_driver(&lm75_driver);
332 }
333
334 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
335 MODULE_DESCRIPTION("LM75 driver");
336 MODULE_LICENSE("GPL");
337
338 module_init(sensors_lm75_init);
339 module_exit(sensors_lm75_exit);