hwmon: (lm90) Introduce device feature bits
[pandora-kernel.git] / drivers / hwmon / lm90.c
1 /*
2  * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3  *          monitoring
4  * Copyright (C) 2003-2010  Jean Delvare <khali@linux-fr.org>
5  *
6  * Based on the lm83 driver. The LM90 is a sensor chip made by National
7  * Semiconductor. It reports up to two temperatures (its own plus up to
8  * one external one) with a 0.125 deg resolution (1 deg for local
9  * temperature) and a 3-4 deg accuracy.
10  *
11  * This driver also supports the LM89 and LM99, two other sensor chips
12  * made by National Semiconductor. Both have an increased remote
13  * temperature measurement accuracy (1 degree), and the LM99
14  * additionally shifts remote temperatures (measured and limits) by 16
15  * degrees, which allows for higher temperatures measurement.
16  * Note that there is no way to differentiate between both chips.
17  * When device is auto-detected, the driver will assume an LM99.
18  *
19  * This driver also supports the LM86, another sensor chip made by
20  * National Semiconductor. It is exactly similar to the LM90 except it
21  * has a higher accuracy.
22  *
23  * This driver also supports the ADM1032, a sensor chip made by Analog
24  * Devices. That chip is similar to the LM90, with a few differences
25  * that are not handled by this driver. Among others, it has a higher
26  * accuracy than the LM90, much like the LM86 does.
27  *
28  * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
29  * chips made by Maxim. These chips are similar to the LM86.
30  * Note that there is no easy way to differentiate between the three
31  * variants. The extra address and features of the MAX6659 are not
32  * supported by this driver. These chips lack the remote temperature
33  * offset feature.
34  *
35  * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
36  * MAX6692 chips made by Maxim.  These are again similar to the LM86,
37  * but they use unsigned temperature values and can report temperatures
38  * from 0 to 145 degrees.
39  *
40  * This driver also supports the MAX6680 and MAX6681, two other sensor
41  * chips made by Maxim. These are quite similar to the other Maxim
42  * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
43  * be treated identically.
44  *
45  * This driver also supports the ADT7461 chip from Analog Devices.
46  * It's supported in both compatibility and extended mode. It is mostly
47  * compatible with LM90 except for a data format difference for the
48  * temperature value registers.
49  *
50  * Since the LM90 was the first chipset supported by this driver, most
51  * comments will refer to this chipset, but are actually general and
52  * concern all supported chipsets, unless mentioned otherwise.
53  *
54  * This program is free software; you can redistribute it and/or modify
55  * it under the terms of the GNU General Public License as published by
56  * the Free Software Foundation; either version 2 of the License, or
57  * (at your option) any later version.
58  *
59  * This program is distributed in the hope that it will be useful,
60  * but WITHOUT ANY WARRANTY; without even the implied warranty of
61  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62  * GNU General Public License for more details.
63  *
64  * You should have received a copy of the GNU General Public License
65  * along with this program; if not, write to the Free Software
66  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
67  */
68
69 #include <linux/module.h>
70 #include <linux/init.h>
71 #include <linux/slab.h>
72 #include <linux/jiffies.h>
73 #include <linux/i2c.h>
74 #include <linux/hwmon-sysfs.h>
75 #include <linux/hwmon.h>
76 #include <linux/err.h>
77 #include <linux/mutex.h>
78 #include <linux/sysfs.h>
79
80 /*
81  * Addresses to scan
82  * Address is fully defined internally and cannot be changed except for
83  * MAX6659, MAX6680 and MAX6681.
84  * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
85  * and MAX6658 have address 0x4c.
86  * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
87  * MAX6647 has address 0x4e.
88  * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
89  * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
90  * 0x4c, 0x4d or 0x4e.
91  */
92
93 static const unsigned short normal_i2c[] = {
94         0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
95
96 enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646,
97         w83l771 };
98
99 /*
100  * The LM90 registers
101  */
102
103 #define LM90_REG_R_MAN_ID               0xFE
104 #define LM90_REG_R_CHIP_ID              0xFF
105 #define LM90_REG_R_CONFIG1              0x03
106 #define LM90_REG_W_CONFIG1              0x09
107 #define LM90_REG_R_CONFIG2              0xBF
108 #define LM90_REG_W_CONFIG2              0xBF
109 #define LM90_REG_R_CONVRATE             0x04
110 #define LM90_REG_W_CONVRATE             0x0A
111 #define LM90_REG_R_STATUS               0x02
112 #define LM90_REG_R_LOCAL_TEMP           0x00
113 #define LM90_REG_R_LOCAL_HIGH           0x05
114 #define LM90_REG_W_LOCAL_HIGH           0x0B
115 #define LM90_REG_R_LOCAL_LOW            0x06
116 #define LM90_REG_W_LOCAL_LOW            0x0C
117 #define LM90_REG_R_LOCAL_CRIT           0x20
118 #define LM90_REG_W_LOCAL_CRIT           0x20
119 #define LM90_REG_R_REMOTE_TEMPH         0x01
120 #define LM90_REG_R_REMOTE_TEMPL         0x10
121 #define LM90_REG_R_REMOTE_OFFSH         0x11
122 #define LM90_REG_W_REMOTE_OFFSH         0x11
123 #define LM90_REG_R_REMOTE_OFFSL         0x12
124 #define LM90_REG_W_REMOTE_OFFSL         0x12
125 #define LM90_REG_R_REMOTE_HIGHH         0x07
126 #define LM90_REG_W_REMOTE_HIGHH         0x0D
127 #define LM90_REG_R_REMOTE_HIGHL         0x13
128 #define LM90_REG_W_REMOTE_HIGHL         0x13
129 #define LM90_REG_R_REMOTE_LOWH          0x08
130 #define LM90_REG_W_REMOTE_LOWH          0x0E
131 #define LM90_REG_R_REMOTE_LOWL          0x14
132 #define LM90_REG_W_REMOTE_LOWL          0x14
133 #define LM90_REG_R_REMOTE_CRIT          0x19
134 #define LM90_REG_W_REMOTE_CRIT          0x19
135 #define LM90_REG_R_TCRIT_HYST           0x21
136 #define LM90_REG_W_TCRIT_HYST           0x21
137
138 /* MAX6646/6647/6649/6657/6658/6659 registers */
139
140 #define MAX6657_REG_R_LOCAL_TEMPL       0x11
141
142 /*
143  * Device flags
144  */
145 #define LM90_FLAG_ADT7461_EXT   (1 << 0) /* ADT7461 extended mode       */
146 /* Device features */
147 #define LM90_HAVE_OFFSET        (1 << 1) /* temperature offset register */
148 #define LM90_HAVE_LOCAL_EXT     (1 << 2) /* extended local temperature  */
149 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit       */
150
151 /*
152  * Functions declaration
153  */
154
155 static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
156 static int lm90_probe(struct i2c_client *client,
157                       const struct i2c_device_id *id);
158 static void lm90_init_client(struct i2c_client *client);
159 static void lm90_alert(struct i2c_client *client, unsigned int flag);
160 static int lm90_remove(struct i2c_client *client);
161 static struct lm90_data *lm90_update_device(struct device *dev);
162
163 /*
164  * Driver data (common to all clients)
165  */
166
167 static const struct i2c_device_id lm90_id[] = {
168         { "adm1032", adm1032 },
169         { "adt7461", adt7461 },
170         { "lm90", lm90 },
171         { "lm86", lm86 },
172         { "lm89", lm86 },
173         { "lm99", lm99 },
174         { "max6646", max6646 },
175         { "max6647", max6646 },
176         { "max6649", max6646 },
177         { "max6657", max6657 },
178         { "max6658", max6657 },
179         { "max6659", max6657 },
180         { "max6680", max6680 },
181         { "max6681", max6680 },
182         { "w83l771", w83l771 },
183         { }
184 };
185 MODULE_DEVICE_TABLE(i2c, lm90_id);
186
187 static struct i2c_driver lm90_driver = {
188         .class          = I2C_CLASS_HWMON,
189         .driver = {
190                 .name   = "lm90",
191         },
192         .probe          = lm90_probe,
193         .remove         = lm90_remove,
194         .alert          = lm90_alert,
195         .id_table       = lm90_id,
196         .detect         = lm90_detect,
197         .address_list   = normal_i2c,
198 };
199
200 /*
201  * Client data (each client gets its own)
202  */
203
204 struct lm90_data {
205         struct device *hwmon_dev;
206         struct mutex update_lock;
207         char valid; /* zero until following fields are valid */
208         unsigned long last_updated; /* in jiffies */
209         int kind;
210         int flags;
211
212         u8 config_orig;         /* Original configuration register value */
213         u8 alert_alarms;        /* Which alarm bits trigger ALERT# */
214
215         /* registers values */
216         s8 temp8[4];    /* 0: local low limit
217                            1: local high limit
218                            2: local critical limit
219                            3: remote critical limit */
220         s16 temp11[5];  /* 0: remote input
221                            1: remote low limit
222                            2: remote high limit
223                            3: remote offset (except max6646 and max6657)
224                            4: local input */
225         u8 temp_hyst;
226         u8 alarms; /* bitvector */
227 };
228
229 /*
230  * Conversions
231  * For local temperatures and limits, critical limits and the hysteresis
232  * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
233  * For remote temperatures and limits, it uses signed 11-bit values with
234  * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some
235  * Maxim chips use unsigned values.
236  */
237
238 static inline int temp_from_s8(s8 val)
239 {
240         return val * 1000;
241 }
242
243 static inline int temp_from_u8(u8 val)
244 {
245         return val * 1000;
246 }
247
248 static inline int temp_from_s16(s16 val)
249 {
250         return val / 32 * 125;
251 }
252
253 static inline int temp_from_u16(u16 val)
254 {
255         return val / 32 * 125;
256 }
257
258 static s8 temp_to_s8(long val)
259 {
260         if (val <= -128000)
261                 return -128;
262         if (val >= 127000)
263                 return 127;
264         if (val < 0)
265                 return (val - 500) / 1000;
266         return (val + 500) / 1000;
267 }
268
269 static u8 temp_to_u8(long val)
270 {
271         if (val <= 0)
272                 return 0;
273         if (val >= 255000)
274                 return 255;
275         return (val + 500) / 1000;
276 }
277
278 static s16 temp_to_s16(long val)
279 {
280         if (val <= -128000)
281                 return 0x8000;
282         if (val >= 127875)
283                 return 0x7FE0;
284         if (val < 0)
285                 return (val - 62) / 125 * 32;
286         return (val + 62) / 125 * 32;
287 }
288
289 static u8 hyst_to_reg(long val)
290 {
291         if (val <= 0)
292                 return 0;
293         if (val >= 30500)
294                 return 31;
295         return (val + 500) / 1000;
296 }
297
298 /*
299  * ADT7461 in compatibility mode is almost identical to LM90 except that
300  * attempts to write values that are outside the range 0 < temp < 127 are
301  * treated as the boundary value.
302  *
303  * ADT7461 in "extended mode" operation uses unsigned integers offset by
304  * 64 (e.g., 0 -> -64 degC).  The range is restricted to -64..191 degC.
305  */
306 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
307 {
308         if (data->flags & LM90_FLAG_ADT7461_EXT)
309                 return (val - 64) * 1000;
310         else
311                 return temp_from_s8(val);
312 }
313
314 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
315 {
316         if (data->flags & LM90_FLAG_ADT7461_EXT)
317                 return (val - 0x4000) / 64 * 250;
318         else
319                 return temp_from_s16(val);
320 }
321
322 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
323 {
324         if (data->flags & LM90_FLAG_ADT7461_EXT) {
325                 if (val <= -64000)
326                         return 0;
327                 if (val >= 191000)
328                         return 0xFF;
329                 return (val + 500 + 64000) / 1000;
330         } else {
331                 if (val <= 0)
332                         return 0;
333                 if (val >= 127000)
334                         return 127;
335                 return (val + 500) / 1000;
336         }
337 }
338
339 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
340 {
341         if (data->flags & LM90_FLAG_ADT7461_EXT) {
342                 if (val <= -64000)
343                         return 0;
344                 if (val >= 191750)
345                         return 0xFFC0;
346                 return (val + 64000 + 125) / 250 * 64;
347         } else {
348                 if (val <= 0)
349                         return 0;
350                 if (val >= 127750)
351                         return 0x7FC0;
352                 return (val + 125) / 250 * 64;
353         }
354 }
355
356 /*
357  * Sysfs stuff
358  */
359
360 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
361                           char *buf)
362 {
363         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
364         struct lm90_data *data = lm90_update_device(dev);
365         int temp;
366
367         if (data->kind == adt7461)
368                 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
369         else if (data->kind == max6646)
370                 temp = temp_from_u8(data->temp8[attr->index]);
371         else
372                 temp = temp_from_s8(data->temp8[attr->index]);
373
374         /* +16 degrees offset for temp2 for the LM99 */
375         if (data->kind == lm99 && attr->index == 3)
376                 temp += 16000;
377
378         return sprintf(buf, "%d\n", temp);
379 }
380
381 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
382                          const char *buf, size_t count)
383 {
384         static const u8 reg[4] = {
385                 LM90_REG_W_LOCAL_LOW,
386                 LM90_REG_W_LOCAL_HIGH,
387                 LM90_REG_W_LOCAL_CRIT,
388                 LM90_REG_W_REMOTE_CRIT,
389         };
390
391         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
392         struct i2c_client *client = to_i2c_client(dev);
393         struct lm90_data *data = i2c_get_clientdata(client);
394         int nr = attr->index;
395         long val;
396         int err;
397
398         err = strict_strtol(buf, 10, &val);
399         if (err < 0)
400                 return err;
401
402         /* +16 degrees offset for temp2 for the LM99 */
403         if (data->kind == lm99 && attr->index == 3)
404                 val -= 16000;
405
406         mutex_lock(&data->update_lock);
407         if (data->kind == adt7461)
408                 data->temp8[nr] = temp_to_u8_adt7461(data, val);
409         else if (data->kind == max6646)
410                 data->temp8[nr] = temp_to_u8(val);
411         else
412                 data->temp8[nr] = temp_to_s8(val);
413         i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
414         mutex_unlock(&data->update_lock);
415         return count;
416 }
417
418 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
419                            char *buf)
420 {
421         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
422         struct lm90_data *data = lm90_update_device(dev);
423         int temp;
424
425         if (data->kind == adt7461)
426                 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
427         else if (data->kind == max6646)
428                 temp = temp_from_u16(data->temp11[attr->index]);
429         else
430                 temp = temp_from_s16(data->temp11[attr->index]);
431
432         /* +16 degrees offset for temp2 for the LM99 */
433         if (data->kind == lm99 &&  attr->index <= 2)
434                 temp += 16000;
435
436         return sprintf(buf, "%d\n", temp);
437 }
438
439 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
440                           const char *buf, size_t count)
441 {
442         static const u8 reg[6] = {
443                 LM90_REG_W_REMOTE_LOWH,
444                 LM90_REG_W_REMOTE_LOWL,
445                 LM90_REG_W_REMOTE_HIGHH,
446                 LM90_REG_W_REMOTE_HIGHL,
447                 LM90_REG_W_REMOTE_OFFSH,
448                 LM90_REG_W_REMOTE_OFFSL,
449         };
450
451         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
452         struct i2c_client *client = to_i2c_client(dev);
453         struct lm90_data *data = i2c_get_clientdata(client);
454         int nr = attr->index;
455         long val;
456         int err;
457
458         err = strict_strtol(buf, 10, &val);
459         if (err < 0)
460                 return err;
461
462         /* +16 degrees offset for temp2 for the LM99 */
463         if (data->kind == lm99 && attr->index <= 2)
464                 val -= 16000;
465
466         mutex_lock(&data->update_lock);
467         if (data->kind == adt7461)
468                 data->temp11[nr] = temp_to_u16_adt7461(data, val);
469         else if (data->kind == max6646)
470                 data->temp11[nr] = temp_to_u8(val) << 8;
471         else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
472                 data->temp11[nr] = temp_to_s16(val);
473         else
474                 data->temp11[nr] = temp_to_s8(val) << 8;
475
476         i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
477                                   data->temp11[nr] >> 8);
478         if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
479                 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
480                                           data->temp11[nr] & 0xff);
481         mutex_unlock(&data->update_lock);
482         return count;
483 }
484
485 static ssize_t show_temphyst(struct device *dev,
486                              struct device_attribute *devattr,
487                              char *buf)
488 {
489         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
490         struct lm90_data *data = lm90_update_device(dev);
491         int temp;
492
493         if (data->kind == adt7461)
494                 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
495         else if (data->kind == max6646)
496                 temp = temp_from_u8(data->temp8[attr->index]);
497         else
498                 temp = temp_from_s8(data->temp8[attr->index]);
499
500         /* +16 degrees offset for temp2 for the LM99 */
501         if (data->kind == lm99 && attr->index == 3)
502                 temp += 16000;
503
504         return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
505 }
506
507 static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
508                             const char *buf, size_t count)
509 {
510         struct i2c_client *client = to_i2c_client(dev);
511         struct lm90_data *data = i2c_get_clientdata(client);
512         long val;
513         int err;
514         int temp;
515
516         err = strict_strtol(buf, 10, &val);
517         if (err < 0)
518                 return err;
519
520         mutex_lock(&data->update_lock);
521         if (data->kind == adt7461)
522                 temp = temp_from_u8_adt7461(data, data->temp8[2]);
523         else if (data->kind == max6646)
524                 temp = temp_from_u8(data->temp8[2]);
525         else
526                 temp = temp_from_s8(data->temp8[2]);
527
528         data->temp_hyst = hyst_to_reg(temp - val);
529         i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
530                                   data->temp_hyst);
531         mutex_unlock(&data->update_lock);
532         return count;
533 }
534
535 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
536                            char *buf)
537 {
538         struct lm90_data *data = lm90_update_device(dev);
539         return sprintf(buf, "%d\n", data->alarms);
540 }
541
542 static ssize_t show_alarm(struct device *dev, struct device_attribute
543                           *devattr, char *buf)
544 {
545         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
546         struct lm90_data *data = lm90_update_device(dev);
547         int bitnr = attr->index;
548
549         return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
550 }
551
552 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
553 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
554 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
555         set_temp8, 0);
556 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
557         set_temp11, 1);
558 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
559         set_temp8, 1);
560 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
561         set_temp11, 2);
562 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
563         set_temp8, 2);
564 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
565         set_temp8, 3);
566 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
567         set_temphyst, 2);
568 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
569 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
570         set_temp11, 3);
571
572 /* Individual alarm files */
573 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
574 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
575 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
576 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
577 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
578 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
579 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
580 /* Raw alarm file for compatibility */
581 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
582
583 static struct attribute *lm90_attributes[] = {
584         &sensor_dev_attr_temp1_input.dev_attr.attr,
585         &sensor_dev_attr_temp2_input.dev_attr.attr,
586         &sensor_dev_attr_temp1_min.dev_attr.attr,
587         &sensor_dev_attr_temp2_min.dev_attr.attr,
588         &sensor_dev_attr_temp1_max.dev_attr.attr,
589         &sensor_dev_attr_temp2_max.dev_attr.attr,
590         &sensor_dev_attr_temp1_crit.dev_attr.attr,
591         &sensor_dev_attr_temp2_crit.dev_attr.attr,
592         &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
593         &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
594
595         &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
596         &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
597         &sensor_dev_attr_temp2_fault.dev_attr.attr,
598         &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
599         &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
600         &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
601         &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
602         &dev_attr_alarms.attr,
603         NULL
604 };
605
606 static const struct attribute_group lm90_group = {
607         .attrs = lm90_attributes,
608 };
609
610 /* pec used for ADM1032 only */
611 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
612                         char *buf)
613 {
614         struct i2c_client *client = to_i2c_client(dev);
615         return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
616 }
617
618 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
619                        const char *buf, size_t count)
620 {
621         struct i2c_client *client = to_i2c_client(dev);
622         long val;
623         int err;
624
625         err = strict_strtol(buf, 10, &val);
626         if (err < 0)
627                 return err;
628
629         switch (val) {
630         case 0:
631                 client->flags &= ~I2C_CLIENT_PEC;
632                 break;
633         case 1:
634                 client->flags |= I2C_CLIENT_PEC;
635                 break;
636         default:
637                 return -EINVAL;
638         }
639
640         return count;
641 }
642
643 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
644
645 /*
646  * Real code
647  */
648
649 /*
650  * The ADM1032 supports PEC but not on write byte transactions, so we need
651  * to explicitly ask for a transaction without PEC.
652  */
653 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
654 {
655         return i2c_smbus_xfer(client->adapter, client->addr,
656                               client->flags & ~I2C_CLIENT_PEC,
657                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
658 }
659
660 /*
661  * It is assumed that client->update_lock is held (unless we are in
662  * detection or initialization steps). This matters when PEC is enabled,
663  * because we don't want the address pointer to change between the write
664  * byte and the read byte transactions.
665  */
666 static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
667 {
668         int err;
669
670         if (client->flags & I2C_CLIENT_PEC) {
671                 err = adm1032_write_byte(client, reg);
672                 if (err >= 0)
673                         err = i2c_smbus_read_byte(client);
674         } else
675                 err = i2c_smbus_read_byte_data(client, reg);
676
677         if (err < 0) {
678                 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
679                          reg, err);
680                 return err;
681         }
682         *value = err;
683
684         return 0;
685 }
686
687 /* Return 0 if detection is successful, -ENODEV otherwise */
688 static int lm90_detect(struct i2c_client *new_client,
689                        struct i2c_board_info *info)
690 {
691         struct i2c_adapter *adapter = new_client->adapter;
692         int address = new_client->addr;
693         const char *name = NULL;
694         int man_id, chip_id, reg_config1, reg_convrate;
695
696         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
697                 return -ENODEV;
698
699         /* detection and identification */
700         if ((man_id = i2c_smbus_read_byte_data(new_client,
701                                                 LM90_REG_R_MAN_ID)) < 0
702          || (chip_id = i2c_smbus_read_byte_data(new_client,
703                                                 LM90_REG_R_CHIP_ID)) < 0
704          || (reg_config1 = i2c_smbus_read_byte_data(new_client,
705                                                 LM90_REG_R_CONFIG1)) < 0
706          || (reg_convrate = i2c_smbus_read_byte_data(new_client,
707                                                 LM90_REG_R_CONVRATE)) < 0)
708                 return -ENODEV;
709
710         if ((address == 0x4C || address == 0x4D)
711          && man_id == 0x01) { /* National Semiconductor */
712                 int reg_config2;
713
714                 reg_config2 = i2c_smbus_read_byte_data(new_client,
715                                                 LM90_REG_R_CONFIG2);
716                 if (reg_config2 < 0)
717                         return -ENODEV;
718
719                 if ((reg_config1 & 0x2A) == 0x00
720                  && (reg_config2 & 0xF8) == 0x00
721                  && reg_convrate <= 0x09) {
722                         if (address == 0x4C
723                          && (chip_id & 0xF0) == 0x20) { /* LM90 */
724                                 name = "lm90";
725                         } else
726                         if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
727                                 name = "lm99";
728                                 dev_info(&adapter->dev,
729                                          "Assuming LM99 chip at 0x%02x\n",
730                                          address);
731                                 dev_info(&adapter->dev,
732                                          "If it is an LM89, instantiate it "
733                                          "with the new_device sysfs "
734                                          "interface\n");
735                         } else
736                         if (address == 0x4C
737                          && (chip_id & 0xF0) == 0x10) { /* LM86 */
738                                 name = "lm86";
739                         }
740                 }
741         } else
742         if ((address == 0x4C || address == 0x4D)
743          && man_id == 0x41) { /* Analog Devices */
744                 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
745                  && (reg_config1 & 0x3F) == 0x00
746                  && reg_convrate <= 0x0A) {
747                         name = "adm1032";
748                         /* The ADM1032 supports PEC, but only if combined
749                            transactions are not used. */
750                         if (i2c_check_functionality(adapter,
751                                                     I2C_FUNC_SMBUS_BYTE))
752                                 info->flags |= I2C_CLIENT_PEC;
753                 } else
754                 if (chip_id == 0x51 /* ADT7461 */
755                  && (reg_config1 & 0x1B) == 0x00
756                  && reg_convrate <= 0x0A) {
757                         name = "adt7461";
758                 }
759         } else
760         if (man_id == 0x4D) { /* Maxim */
761                 /*
762                  * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
763                  * register. Reading from that address will return the last
764                  * read value, which in our case is those of the man_id
765                  * register. Likewise, the config1 register seems to lack a
766                  * low nibble, so the value will be those of the previous
767                  * read, so in our case those of the man_id register.
768                  */
769                 if (chip_id == man_id
770                  && (address == 0x4C || address == 0x4D)
771                  && (reg_config1 & 0x1F) == (man_id & 0x0F)
772                  && reg_convrate <= 0x09) {
773                         name = "max6657";
774                 } else
775                 /*
776                  * The chip_id register of the MAX6680 and MAX6681 holds the
777                  * revision of the chip. The lowest bit of the config1 register
778                  * is unused and should return zero when read, so should the
779                  * second to last bit of config1 (software reset).
780                  */
781                 if (chip_id == 0x01
782                  && (reg_config1 & 0x03) == 0x00
783                  && reg_convrate <= 0x07) {
784                         name = "max6680";
785                 } else
786                 /*
787                  * The chip_id register of the MAX6646/6647/6649 holds the
788                  * revision of the chip. The lowest 6 bits of the config1
789                  * register are unused and should return zero when read.
790                  */
791                 if (chip_id == 0x59
792                  && (reg_config1 & 0x3f) == 0x00
793                  && reg_convrate <= 0x07) {
794                         name = "max6646";
795                 }
796         } else
797         if (address == 0x4C
798          && man_id == 0x5C) { /* Winbond/Nuvoton */
799                 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
800                  && (reg_config1 & 0x2A) == 0x00
801                  && reg_convrate <= 0x08) {
802                         name = "w83l771";
803                 }
804         }
805
806         if (!name) { /* identification failed */
807                 dev_dbg(&adapter->dev,
808                         "Unsupported chip at 0x%02x (man_id=0x%02X, "
809                         "chip_id=0x%02X)\n", address, man_id, chip_id);
810                 return -ENODEV;
811         }
812
813         strlcpy(info->type, name, I2C_NAME_SIZE);
814
815         return 0;
816 }
817
818 static int lm90_probe(struct i2c_client *new_client,
819                       const struct i2c_device_id *id)
820 {
821         struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
822         struct lm90_data *data;
823         int err;
824
825         data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
826         if (!data) {
827                 err = -ENOMEM;
828                 goto exit;
829         }
830         i2c_set_clientdata(new_client, data);
831         mutex_init(&data->update_lock);
832
833         /* Set the device type */
834         data->kind = id->driver_data;
835         if (data->kind == adm1032) {
836                 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
837                         new_client->flags &= ~I2C_CLIENT_PEC;
838         }
839
840         /* Different devices have different alarm bits triggering the
841          * ALERT# output */
842         switch (data->kind) {
843         case lm90:
844         case lm99:
845         case lm86:
846                 data->alert_alarms = 0x7b;
847                 break;
848         default:
849                 data->alert_alarms = 0x7c;
850                 break;
851         }
852
853         /* Set chip capabilities */
854         if (data->kind != max6657 && data->kind != max6646)
855                 data->flags |= LM90_HAVE_OFFSET;
856
857         if (data->kind == max6657 || data->kind == max6646)
858                 data->flags |= LM90_HAVE_LOCAL_EXT;
859
860         if (data->kind != max6657 && data->kind != max6646
861             && data->kind != max6680)
862                 data->flags |= LM90_HAVE_REM_LIMIT_EXT;
863
864         /* Initialize the LM90 chip */
865         lm90_init_client(new_client);
866
867         /* Register sysfs hooks */
868         err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
869         if (err)
870                 goto exit_free;
871         if (new_client->flags & I2C_CLIENT_PEC) {
872                 err = device_create_file(&new_client->dev, &dev_attr_pec);
873                 if (err)
874                         goto exit_remove_files;
875         }
876         if (data->flags & LM90_HAVE_OFFSET) {
877                 err = device_create_file(&new_client->dev,
878                                         &sensor_dev_attr_temp2_offset.dev_attr);
879                 if (err)
880                         goto exit_remove_files;
881         }
882
883         data->hwmon_dev = hwmon_device_register(&new_client->dev);
884         if (IS_ERR(data->hwmon_dev)) {
885                 err = PTR_ERR(data->hwmon_dev);
886                 goto exit_remove_files;
887         }
888
889         return 0;
890
891 exit_remove_files:
892         sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
893         device_remove_file(&new_client->dev, &dev_attr_pec);
894 exit_free:
895         kfree(data);
896 exit:
897         return err;
898 }
899
900 static void lm90_init_client(struct i2c_client *client)
901 {
902         u8 config;
903         struct lm90_data *data = i2c_get_clientdata(client);
904
905         /*
906          * Start the conversions.
907          */
908         i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
909                                   5); /* 2 Hz */
910         if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
911                 dev_warn(&client->dev, "Initialization failed!\n");
912                 return;
913         }
914         data->config_orig = config;
915
916         /* Check Temperature Range Select */
917         if (data->kind == adt7461) {
918                 if (config & 0x04)
919                         data->flags |= LM90_FLAG_ADT7461_EXT;
920         }
921
922         /*
923          * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
924          * 0.125 degree resolution) and range (0x08, extend range
925          * to -64 degree) mode for the remote temperature sensor.
926          */
927         if (data->kind == max6680)
928                 config |= 0x18;
929
930         config &= 0xBF; /* run */
931         if (config != data->config_orig) /* Only write if changed */
932                 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
933 }
934
935 static int lm90_remove(struct i2c_client *client)
936 {
937         struct lm90_data *data = i2c_get_clientdata(client);
938
939         hwmon_device_unregister(data->hwmon_dev);
940         sysfs_remove_group(&client->dev.kobj, &lm90_group);
941         device_remove_file(&client->dev, &dev_attr_pec);
942         if (data->flags & LM90_HAVE_OFFSET)
943                 device_remove_file(&client->dev,
944                                    &sensor_dev_attr_temp2_offset.dev_attr);
945
946         /* Restore initial configuration */
947         i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
948                                   data->config_orig);
949
950         kfree(data);
951         return 0;
952 }
953
954 static void lm90_alert(struct i2c_client *client, unsigned int flag)
955 {
956         struct lm90_data *data = i2c_get_clientdata(client);
957         u8 config, alarms;
958
959         lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
960         if ((alarms & 0x7f) == 0) {
961                 dev_info(&client->dev, "Everything OK\n");
962         } else {
963                 if (alarms & 0x61)
964                         dev_warn(&client->dev,
965                                  "temp%d out of range, please check!\n", 1);
966                 if (alarms & 0x1a)
967                         dev_warn(&client->dev,
968                                  "temp%d out of range, please check!\n", 2);
969                 if (alarms & 0x04)
970                         dev_warn(&client->dev,
971                                  "temp%d diode open, please check!\n", 2);
972
973                 /* Disable ALERT# output, because these chips don't implement
974                   SMBus alert correctly; they should only hold the alert line
975                   low briefly. */
976                 if ((data->kind == adm1032 || data->kind == adt7461)
977                  && (alarms & data->alert_alarms)) {
978                         dev_dbg(&client->dev, "Disabling ALERT#\n");
979                         lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
980                         i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
981                                                   config | 0x80);
982                 }
983         }
984 }
985
986 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
987 {
988         int err;
989         u8 oldh, newh, l;
990
991         /*
992          * There is a trick here. We have to read two registers to have the
993          * sensor temperature, but we have to beware a conversion could occur
994          * inbetween the readings. The datasheet says we should either use
995          * the one-shot conversion register, which we don't want to do
996          * (disables hardware monitoring) or monitor the busy bit, which is
997          * impossible (we can't read the values and monitor that bit at the
998          * exact same time). So the solution used here is to read the high
999          * byte once, then the low byte, then the high byte again. If the new
1000          * high byte matches the old one, then we have a valid reading. Else
1001          * we have to read the low byte again, and now we believe we have a
1002          * correct reading.
1003          */
1004         if ((err = lm90_read_reg(client, regh, &oldh))
1005          || (err = lm90_read_reg(client, regl, &l))
1006          || (err = lm90_read_reg(client, regh, &newh)))
1007                 return err;
1008         if (oldh != newh) {
1009                 err = lm90_read_reg(client, regl, &l);
1010                 if (err)
1011                         return err;
1012         }
1013         *value = (newh << 8) | l;
1014
1015         return 0;
1016 }
1017
1018 static struct lm90_data *lm90_update_device(struct device *dev)
1019 {
1020         struct i2c_client *client = to_i2c_client(dev);
1021         struct lm90_data *data = i2c_get_clientdata(client);
1022
1023         mutex_lock(&data->update_lock);
1024
1025         if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
1026          || !data->valid) {
1027                 u8 h, l;
1028
1029                 dev_dbg(&client->dev, "Updating lm90 data.\n");
1030                 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
1031                 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
1032                 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
1033                 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
1034                 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
1035
1036                 if (data->flags & LM90_HAVE_LOCAL_EXT) {
1037                         lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
1038                                     MAX6657_REG_R_LOCAL_TEMPL,
1039                                     &data->temp11[4]);
1040                 } else {
1041                         if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
1042                                           &h) == 0)
1043                                 data->temp11[4] = h << 8;
1044                 }
1045                 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
1046                             LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
1047
1048                 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
1049                         data->temp11[1] = h << 8;
1050                         if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
1051                          && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
1052                                           &l) == 0)
1053                                 data->temp11[1] |= l;
1054                 }
1055                 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
1056                         data->temp11[2] = h << 8;
1057                         if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
1058                          && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
1059                                           &l) == 0)
1060                                 data->temp11[2] |= l;
1061                 }
1062
1063                 if (data->flags & LM90_HAVE_OFFSET) {
1064                         if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
1065                                           &h) == 0
1066                          && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
1067                                           &l) == 0)
1068                                 data->temp11[3] = (h << 8) | l;
1069                 }
1070                 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
1071
1072                 /* Re-enable ALERT# output if it was originally enabled and
1073                  * relevant alarms are all clear */
1074                 if ((data->config_orig & 0x80) == 0
1075                  && (data->alarms & data->alert_alarms) == 0) {
1076                         u8 config;
1077
1078                         lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1079                         if (config & 0x80) {
1080                                 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1081                                 i2c_smbus_write_byte_data(client,
1082                                                           LM90_REG_W_CONFIG1,
1083                                                           config & ~0x80);
1084                         }
1085                 }
1086
1087                 data->last_updated = jiffies;
1088                 data->valid = 1;
1089         }
1090
1091         mutex_unlock(&data->update_lock);
1092
1093         return data;
1094 }
1095
1096 static int __init sensors_lm90_init(void)
1097 {
1098         return i2c_add_driver(&lm90_driver);
1099 }
1100
1101 static void __exit sensors_lm90_exit(void)
1102 {
1103         i2c_del_driver(&lm90_driver);
1104 }
1105
1106 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1107 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1108 MODULE_LICENSE("GPL");
1109
1110 module_init(sensors_lm90_init);
1111 module_exit(sensors_lm90_exit);