Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / lm83.c
blob3dba49fbc1d6f4a2f5c2e1bd23e62e5a8782df55
1 /*
2 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2006 Jean Delvare <khali@linux-fr.org>
6 * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
7 * a sensor chip made by National Semiconductor. It reports up to four
8 * temperatures (its own plus up to three external ones) with a 1 deg
9 * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
10 * from National's website at:
11 * http://www.national.com/pf/LM/LM83.html
12 * Since the datasheet omits to give the chip stepping code, I give it
13 * here: 0x03 (at register 0xff).
15 * Also supports the LM82 temp sensor, which is basically a stripped down
16 * model of the LM83. Datasheet is here:
17 * http://www.national.com/pf/LM/LM82.html
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/jiffies.h>
38 #include <linux/i2c.h>
39 #include <linux/hwmon-sysfs.h>
40 #include <linux/hwmon.h>
41 #include <linux/err.h>
42 #include <linux/mutex.h>
43 #include <linux/sysfs.h>
46 * Addresses to scan
47 * Address is selected using 2 three-level pins, resulting in 9 possible
48 * addresses.
51 <<<<<<< HEAD:drivers/hwmon/lm83.c
52 static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
53 0x29, 0x2a, 0x2b,
54 0x4c, 0x4d, 0x4e,
55 I2C_CLIENT_END };
56 =======
57 static const unsigned short normal_i2c[] = {
58 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
59 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/lm83.c
62 * Insmod parameters
65 I2C_CLIENT_INSMOD_2(lm83, lm82);
68 * The LM83 registers
69 * Manufacturer ID is 0x01 for National Semiconductor.
72 #define LM83_REG_R_MAN_ID 0xFE
73 #define LM83_REG_R_CHIP_ID 0xFF
74 #define LM83_REG_R_CONFIG 0x03
75 #define LM83_REG_W_CONFIG 0x09
76 #define LM83_REG_R_STATUS1 0x02
77 #define LM83_REG_R_STATUS2 0x35
78 #define LM83_REG_R_LOCAL_TEMP 0x00
79 #define LM83_REG_R_LOCAL_HIGH 0x05
80 #define LM83_REG_W_LOCAL_HIGH 0x0B
81 #define LM83_REG_R_REMOTE1_TEMP 0x30
82 #define LM83_REG_R_REMOTE1_HIGH 0x38
83 #define LM83_REG_W_REMOTE1_HIGH 0x50
84 #define LM83_REG_R_REMOTE2_TEMP 0x01
85 #define LM83_REG_R_REMOTE2_HIGH 0x07
86 #define LM83_REG_W_REMOTE2_HIGH 0x0D
87 #define LM83_REG_R_REMOTE3_TEMP 0x31
88 #define LM83_REG_R_REMOTE3_HIGH 0x3A
89 #define LM83_REG_W_REMOTE3_HIGH 0x52
90 #define LM83_REG_R_TCRIT 0x42
91 #define LM83_REG_W_TCRIT 0x5A
94 * Conversions and various macros
95 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
98 #define TEMP_FROM_REG(val) ((val) * 1000)
99 #define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
100 (val) >= 127000 ? 127 : \
101 (val) < 0 ? ((val) - 500) / 1000 : \
102 ((val) + 500) / 1000)
104 static const u8 LM83_REG_R_TEMP[] = {
105 LM83_REG_R_LOCAL_TEMP,
106 LM83_REG_R_REMOTE1_TEMP,
107 LM83_REG_R_REMOTE2_TEMP,
108 LM83_REG_R_REMOTE3_TEMP,
109 LM83_REG_R_LOCAL_HIGH,
110 LM83_REG_R_REMOTE1_HIGH,
111 LM83_REG_R_REMOTE2_HIGH,
112 LM83_REG_R_REMOTE3_HIGH,
113 LM83_REG_R_TCRIT,
116 static const u8 LM83_REG_W_HIGH[] = {
117 LM83_REG_W_LOCAL_HIGH,
118 LM83_REG_W_REMOTE1_HIGH,
119 LM83_REG_W_REMOTE2_HIGH,
120 LM83_REG_W_REMOTE3_HIGH,
121 LM83_REG_W_TCRIT,
125 * Functions declaration
128 static int lm83_attach_adapter(struct i2c_adapter *adapter);
129 static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
130 static int lm83_detach_client(struct i2c_client *client);
131 static struct lm83_data *lm83_update_device(struct device *dev);
134 * Driver data (common to all clients)
137 static struct i2c_driver lm83_driver = {
138 .driver = {
139 .name = "lm83",
141 .attach_adapter = lm83_attach_adapter,
142 .detach_client = lm83_detach_client,
146 * Client data (each client gets its own)
149 struct lm83_data {
150 struct i2c_client client;
151 struct device *hwmon_dev;
152 struct mutex update_lock;
153 char valid; /* zero until following fields are valid */
154 unsigned long last_updated; /* in jiffies */
156 /* registers values */
157 s8 temp[9]; /* 0..3: input 1-4,
158 4..7: high limit 1-4,
159 8 : critical limit */
160 u16 alarms; /* bitvector, combined */
164 * Sysfs stuff
167 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
168 char *buf)
170 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
171 struct lm83_data *data = lm83_update_device(dev);
172 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
175 static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
176 const char *buf, size_t count)
178 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
179 struct i2c_client *client = to_i2c_client(dev);
180 struct lm83_data *data = i2c_get_clientdata(client);
181 long val = simple_strtol(buf, NULL, 10);
182 int nr = attr->index;
184 mutex_lock(&data->update_lock);
185 data->temp[nr] = TEMP_TO_REG(val);
186 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
187 data->temp[nr]);
188 mutex_unlock(&data->update_lock);
189 return count;
192 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
193 char *buf)
195 struct lm83_data *data = lm83_update_device(dev);
196 return sprintf(buf, "%d\n", data->alarms);
199 static ssize_t show_alarm(struct device *dev, struct device_attribute
200 *devattr, char *buf)
202 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
203 struct lm83_data *data = lm83_update_device(dev);
204 int bitnr = attr->index;
206 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
209 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
210 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
211 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
212 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
213 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
214 set_temp, 4);
215 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
216 set_temp, 5);
217 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
218 set_temp, 6);
219 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
220 set_temp, 7);
221 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
222 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
223 static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
224 set_temp, 8);
225 static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
227 /* Individual alarm files */
228 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
229 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
230 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
231 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
232 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
233 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
234 static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
235 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
236 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
237 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
238 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
239 /* Raw alarm file for compatibility */
240 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
242 static struct attribute *lm83_attributes[] = {
243 &sensor_dev_attr_temp1_input.dev_attr.attr,
244 &sensor_dev_attr_temp3_input.dev_attr.attr,
245 &sensor_dev_attr_temp1_max.dev_attr.attr,
246 &sensor_dev_attr_temp3_max.dev_attr.attr,
247 &sensor_dev_attr_temp1_crit.dev_attr.attr,
248 &sensor_dev_attr_temp3_crit.dev_attr.attr,
250 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
251 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
252 &sensor_dev_attr_temp3_fault.dev_attr.attr,
253 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
254 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
255 &dev_attr_alarms.attr,
256 NULL
259 static const struct attribute_group lm83_group = {
260 .attrs = lm83_attributes,
263 static struct attribute *lm83_attributes_opt[] = {
264 &sensor_dev_attr_temp2_input.dev_attr.attr,
265 &sensor_dev_attr_temp4_input.dev_attr.attr,
266 &sensor_dev_attr_temp2_max.dev_attr.attr,
267 &sensor_dev_attr_temp4_max.dev_attr.attr,
268 &sensor_dev_attr_temp2_crit.dev_attr.attr,
269 &sensor_dev_attr_temp4_crit.dev_attr.attr,
271 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
272 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
273 &sensor_dev_attr_temp4_fault.dev_attr.attr,
274 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
275 &sensor_dev_attr_temp2_fault.dev_attr.attr,
276 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
277 NULL
280 static const struct attribute_group lm83_group_opt = {
281 .attrs = lm83_attributes_opt,
285 * Real code
288 static int lm83_attach_adapter(struct i2c_adapter *adapter)
290 if (!(adapter->class & I2C_CLASS_HWMON))
291 return 0;
292 return i2c_probe(adapter, &addr_data, lm83_detect);
296 * The following function does more than just detection. If detection
297 * succeeds, it also registers the new chip.
299 static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
301 struct i2c_client *new_client;
302 struct lm83_data *data;
303 int err = 0;
304 const char *name = "";
306 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
307 goto exit;
309 if (!(data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
310 err = -ENOMEM;
311 goto exit;
314 /* The common I2C client data is placed right after the
315 * LM83-specific data. */
316 new_client = &data->client;
317 i2c_set_clientdata(new_client, data);
318 new_client->addr = address;
319 new_client->adapter = adapter;
320 new_client->driver = &lm83_driver;
321 new_client->flags = 0;
323 /* Now we do the detection and identification. A negative kind
324 * means that the driver was loaded with no force parameter
325 * (default), so we must both detect and identify the chip
326 * (actually there is only one possible kind of chip for now, LM83).
327 * A zero kind means that the driver was loaded with the force
328 * parameter, the detection step shall be skipped. A positive kind
329 * means that the driver was loaded with the force parameter and a
330 * given kind of chip is requested, so both the detection and the
331 * identification steps are skipped. */
333 /* Default to an LM83 if forced */
334 if (kind == 0)
335 kind = lm83;
337 if (kind < 0) { /* detection */
338 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
339 & 0xA8) != 0x00) ||
340 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
341 & 0x48) != 0x00) ||
342 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
343 & 0x41) != 0x00)) {
344 dev_dbg(&adapter->dev,
345 "LM83 detection failed at 0x%02x.\n", address);
346 goto exit_free;
350 if (kind <= 0) { /* identification */
351 u8 man_id, chip_id;
353 man_id = i2c_smbus_read_byte_data(new_client,
354 LM83_REG_R_MAN_ID);
355 chip_id = i2c_smbus_read_byte_data(new_client,
356 LM83_REG_R_CHIP_ID);
358 if (man_id == 0x01) { /* National Semiconductor */
359 if (chip_id == 0x03) {
360 kind = lm83;
361 } else
362 if (chip_id == 0x01) {
363 kind = lm82;
367 if (kind <= 0) { /* identification failed */
368 dev_info(&adapter->dev,
369 "Unsupported chip (man_id=0x%02X, "
370 "chip_id=0x%02X).\n", man_id, chip_id);
371 goto exit_free;
375 if (kind == lm83) {
376 name = "lm83";
377 } else
378 if (kind == lm82) {
379 name = "lm82";
382 /* We can fill in the remaining client fields */
383 strlcpy(new_client->name, name, I2C_NAME_SIZE);
384 data->valid = 0;
385 mutex_init(&data->update_lock);
387 /* Tell the I2C layer a new client has arrived */
388 if ((err = i2c_attach_client(new_client)))
389 goto exit_free;
392 * Register sysfs hooks
393 * The LM82 can only monitor one external diode which is
394 * at the same register as the LM83 temp3 entry - so we
395 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
398 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
399 goto exit_detach;
401 if (kind == lm83) {
402 if ((err = sysfs_create_group(&new_client->dev.kobj,
403 &lm83_group_opt)))
404 goto exit_remove_files;
407 data->hwmon_dev = hwmon_device_register(&new_client->dev);
408 if (IS_ERR(data->hwmon_dev)) {
409 err = PTR_ERR(data->hwmon_dev);
410 goto exit_remove_files;
413 return 0;
415 exit_remove_files:
416 sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
417 sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
418 exit_detach:
419 i2c_detach_client(new_client);
420 exit_free:
421 kfree(data);
422 exit:
423 return err;
426 static int lm83_detach_client(struct i2c_client *client)
428 struct lm83_data *data = i2c_get_clientdata(client);
429 int err;
431 hwmon_device_unregister(data->hwmon_dev);
432 sysfs_remove_group(&client->dev.kobj, &lm83_group);
433 sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
435 if ((err = i2c_detach_client(client)))
436 return err;
438 kfree(data);
439 return 0;
442 static struct lm83_data *lm83_update_device(struct device *dev)
444 struct i2c_client *client = to_i2c_client(dev);
445 struct lm83_data *data = i2c_get_clientdata(client);
447 mutex_lock(&data->update_lock);
449 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
450 int nr;
452 dev_dbg(&client->dev, "Updating lm83 data.\n");
453 for (nr = 0; nr < 9; nr++) {
454 data->temp[nr] =
455 i2c_smbus_read_byte_data(client,
456 LM83_REG_R_TEMP[nr]);
458 data->alarms =
459 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
460 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
461 << 8);
463 data->last_updated = jiffies;
464 data->valid = 1;
467 mutex_unlock(&data->update_lock);
469 return data;
472 static int __init sensors_lm83_init(void)
474 return i2c_add_driver(&lm83_driver);
477 static void __exit sensors_lm83_exit(void)
479 i2c_del_driver(&lm83_driver);
482 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
483 MODULE_DESCRIPTION("LM83 driver");
484 MODULE_LICENSE("GPL");
486 module_init(sensors_lm83_init);
487 module_exit(sensors_lm83_exit);