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