]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/hwmon/lm90.c
hwmon: (lm90) Fix handling of hysteresis value
[linux-2.6-omap-h63xx.git] / drivers / hwmon / lm90.c
1 /*
2  * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3  *          monitoring
4  * Copyright (C) 2003-2008  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. The
16  * driver doesn't handle it since it can be done easily in user-space.
17  * Note that there is no way to differentiate between both chips.
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 and MAX6649 chips
36  * made by Maxim.  These are again similar to the LM86, but they use
37  * unsigned temperature values and can report temperatures from 0 to
38  * 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 /*
97  * Insmod parameters
98  */
99
100 I2C_CLIENT_INSMOD_8(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680,
101                     max6646);
102
103 /*
104  * The LM90 registers
105  */
106
107 #define LM90_REG_R_MAN_ID               0xFE
108 #define LM90_REG_R_CHIP_ID              0xFF
109 #define LM90_REG_R_CONFIG1              0x03
110 #define LM90_REG_W_CONFIG1              0x09
111 #define LM90_REG_R_CONFIG2              0xBF
112 #define LM90_REG_W_CONFIG2              0xBF
113 #define LM90_REG_R_CONVRATE             0x04
114 #define LM90_REG_W_CONVRATE             0x0A
115 #define LM90_REG_R_STATUS               0x02
116 #define LM90_REG_R_LOCAL_TEMP           0x00
117 #define LM90_REG_R_LOCAL_HIGH           0x05
118 #define LM90_REG_W_LOCAL_HIGH           0x0B
119 #define LM90_REG_R_LOCAL_LOW            0x06
120 #define LM90_REG_W_LOCAL_LOW            0x0C
121 #define LM90_REG_R_LOCAL_CRIT           0x20
122 #define LM90_REG_W_LOCAL_CRIT           0x20
123 #define LM90_REG_R_REMOTE_TEMPH         0x01
124 #define LM90_REG_R_REMOTE_TEMPL         0x10
125 #define LM90_REG_R_REMOTE_OFFSH         0x11
126 #define LM90_REG_W_REMOTE_OFFSH         0x11
127 #define LM90_REG_R_REMOTE_OFFSL         0x12
128 #define LM90_REG_W_REMOTE_OFFSL         0x12
129 #define LM90_REG_R_REMOTE_HIGHH         0x07
130 #define LM90_REG_W_REMOTE_HIGHH         0x0D
131 #define LM90_REG_R_REMOTE_HIGHL         0x13
132 #define LM90_REG_W_REMOTE_HIGHL         0x13
133 #define LM90_REG_R_REMOTE_LOWH          0x08
134 #define LM90_REG_W_REMOTE_LOWH          0x0E
135 #define LM90_REG_R_REMOTE_LOWL          0x14
136 #define LM90_REG_W_REMOTE_LOWL          0x14
137 #define LM90_REG_R_REMOTE_CRIT          0x19
138 #define LM90_REG_W_REMOTE_CRIT          0x19
139 #define LM90_REG_R_TCRIT_HYST           0x21
140 #define LM90_REG_W_TCRIT_HYST           0x21
141
142 /* MAX6646/6647/6649/6657/6658/6659 registers */
143
144 #define MAX6657_REG_R_LOCAL_TEMPL       0x11
145
146 /*
147  * Device flags
148  */
149 #define LM90_FLAG_ADT7461_EXT           0x01    /* ADT7461 extended mode */
150
151 /*
152  * Functions declaration
153  */
154
155 static int lm90_detect(struct i2c_client *client, int kind,
156                        struct i2c_board_info *info);
157 static int lm90_probe(struct i2c_client *client,
158                       const struct i2c_device_id *id);
159 static void lm90_init_client(struct i2c_client *client);
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", lm99 },
173         { "lm99", lm99 },       /* Missing temperature offset */
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         { }
183 };
184 MODULE_DEVICE_TABLE(i2c, lm90_id);
185
186 static struct i2c_driver lm90_driver = {
187         .class          = I2C_CLASS_HWMON,
188         .driver = {
189                 .name   = "lm90",
190         },
191         .probe          = lm90_probe,
192         .remove         = lm90_remove,
193         .id_table       = lm90_id,
194         .detect         = lm90_detect,
195         .address_data   = &addr_data,
196 };
197
198 /*
199  * Client data (each client gets its own)
200  */
201
202 struct lm90_data {
203         struct device *hwmon_dev;
204         struct mutex update_lock;
205         char valid; /* zero until following fields are valid */
206         unsigned long last_updated; /* in jiffies */
207         int kind;
208         int flags;
209
210         /* registers values */
211         s8 temp8[4];    /* 0: local low limit
212                            1: local high limit
213                            2: local critical limit
214                            3: remote critical limit */
215         s16 temp11[5];  /* 0: remote input
216                            1: remote low limit
217                            2: remote high limit
218                            3: remote offset (except max6646 and max6657)
219                            4: local input */
220         u8 temp_hyst;
221         u8 alarms; /* bitvector */
222 };
223
224 /*
225  * Conversions
226  * For local temperatures and limits, critical limits and the hysteresis
227  * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
228  * For remote temperatures and limits, it uses signed 11-bit values with
229  * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some
230  * Maxim chips use unsigned values.
231  */
232
233 static inline int temp_from_s8(s8 val)
234 {
235         return val * 1000;
236 }
237
238 static inline int temp_from_u8(u8 val)
239 {
240         return val * 1000;
241 }
242
243 static inline int temp_from_s16(s16 val)
244 {
245         return val / 32 * 125;
246 }
247
248 static inline int temp_from_u16(u16 val)
249 {
250         return val / 32 * 125;
251 }
252
253 static s8 temp_to_s8(long val)
254 {
255         if (val <= -128000)
256                 return -128;
257         if (val >= 127000)
258                 return 127;
259         if (val < 0)
260                 return (val - 500) / 1000;
261         return (val + 500) / 1000;
262 }
263
264 static u8 temp_to_u8(long val)
265 {
266         if (val <= 0)
267                 return 0;
268         if (val >= 255000)
269                 return 255;
270         return (val + 500) / 1000;
271 }
272
273 static s16 temp_to_s16(long val)
274 {
275         if (val <= -128000)
276                 return 0x8000;
277         if (val >= 127875)
278                 return 0x7FE0;
279         if (val < 0)
280                 return (val - 62) / 125 * 32;
281         return (val + 62) / 125 * 32;
282 }
283
284 static u8 hyst_to_reg(long val)
285 {
286         if (val <= 0)
287                 return 0;
288         if (val >= 30500)
289                 return 31;
290         return (val + 500) / 1000;
291 }
292
293 /*
294  * ADT7461 in compatibility mode is almost identical to LM90 except that
295  * attempts to write values that are outside the range 0 < temp < 127 are
296  * treated as the boundary value.
297  *
298  * ADT7461 in "extended mode" operation uses unsigned integers offset by
299  * 64 (e.g., 0 -> -64 degC).  The range is restricted to -64..191 degC.
300  */
301 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
302 {
303         if (data->flags & LM90_FLAG_ADT7461_EXT)
304                 return (val - 64) * 1000;
305         else
306                 return temp_from_s8(val);
307 }
308
309 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
310 {
311         if (data->flags & LM90_FLAG_ADT7461_EXT)
312                 return (val - 0x4000) / 64 * 250;
313         else
314                 return temp_from_s16(val);
315 }
316
317 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
318 {
319         if (data->flags & LM90_FLAG_ADT7461_EXT) {
320                 if (val <= -64000)
321                         return 0;
322                 if (val >= 191000)
323                         return 0xFF;
324                 return (val + 500 + 64000) / 1000;
325         } else {
326                 if (val <= 0)
327                         return 0;
328                 if (val >= 127000)
329                         return 127;
330                 return (val + 500) / 1000;
331         }
332 }
333
334 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
335 {
336         if (data->flags & LM90_FLAG_ADT7461_EXT) {
337                 if (val <= -64000)
338                         return 0;
339                 if (val >= 191750)
340                         return 0xFFC0;
341                 return (val + 64000 + 125) / 250 * 64;
342         } else {
343                 if (val <= 0)
344                         return 0;
345                 if (val >= 127750)
346                         return 0x7FC0;
347                 return (val + 125) / 250 * 64;
348         }
349 }
350
351 /*
352  * Sysfs stuff
353  */
354
355 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
356                           char *buf)
357 {
358         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
359         struct lm90_data *data = lm90_update_device(dev);
360         int temp;
361
362         if (data->kind == adt7461)
363                 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
364         else if (data->kind == max6646)
365                 temp = temp_from_u8(data->temp8[attr->index]);
366         else
367                 temp = temp_from_s8(data->temp8[attr->index]);
368
369         return sprintf(buf, "%d\n", temp);
370 }
371
372 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
373                          const char *buf, size_t count)
374 {
375         static const u8 reg[4] = {
376                 LM90_REG_W_LOCAL_LOW,
377                 LM90_REG_W_LOCAL_HIGH,
378                 LM90_REG_W_LOCAL_CRIT,
379                 LM90_REG_W_REMOTE_CRIT,
380         };
381
382         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
383         struct i2c_client *client = to_i2c_client(dev);
384         struct lm90_data *data = i2c_get_clientdata(client);
385         long val = simple_strtol(buf, NULL, 10);
386         int nr = attr->index;
387
388         mutex_lock(&data->update_lock);
389         if (data->kind == adt7461)
390                 data->temp8[nr] = temp_to_u8_adt7461(data, val);
391         else if (data->kind == max6646)
392                 data->temp8[nr] = temp_to_u8(val);
393         else
394                 data->temp8[nr] = temp_to_s8(val);
395         i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
396         mutex_unlock(&data->update_lock);
397         return count;
398 }
399
400 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
401                            char *buf)
402 {
403         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
404         struct lm90_data *data = lm90_update_device(dev);
405         int temp;
406
407         if (data->kind == adt7461)
408                 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
409         else if (data->kind == max6646)
410                 temp = temp_from_u16(data->temp11[attr->index]);
411         else
412                 temp = temp_from_s16(data->temp11[attr->index]);
413
414         return sprintf(buf, "%d\n", temp);
415 }
416
417 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
418                           const char *buf, size_t count)
419 {
420         static const u8 reg[6] = {
421                 LM90_REG_W_REMOTE_LOWH,
422                 LM90_REG_W_REMOTE_LOWL,
423                 LM90_REG_W_REMOTE_HIGHH,
424                 LM90_REG_W_REMOTE_HIGHL,
425                 LM90_REG_W_REMOTE_OFFSH,
426                 LM90_REG_W_REMOTE_OFFSL,
427         };
428
429         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
430         struct i2c_client *client = to_i2c_client(dev);
431         struct lm90_data *data = i2c_get_clientdata(client);
432         long val = simple_strtol(buf, NULL, 10);
433         int nr = attr->index;
434
435         mutex_lock(&data->update_lock);
436         if (data->kind == adt7461)
437                 data->temp11[nr] = temp_to_u16_adt7461(data, val);
438         else if (data->kind == max6657 || data->kind == max6680)
439                 data->temp11[nr] = temp_to_s8(val) << 8;
440         else if (data->kind == max6646)
441                 data->temp11[nr] = temp_to_u8(val) << 8;
442         else
443                 data->temp11[nr] = temp_to_s16(val);
444
445         i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
446                                   data->temp11[nr] >> 8);
447         if (data->kind != max6657 && data->kind != max6680
448             && data->kind != max6646)
449                 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
450                                           data->temp11[nr] & 0xff);
451         mutex_unlock(&data->update_lock);
452         return count;
453 }
454
455 static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
456                              char *buf)
457 {
458         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
459         struct lm90_data *data = lm90_update_device(dev);
460         int temp;
461
462         if (data->kind == adt7461)
463                 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
464         else if (data->kind == max6646)
465                 temp = temp_from_u8(data->temp8[attr->index]);
466         else
467                 temp = temp_from_s8(data->temp8[attr->index]);
468
469         return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
470 }
471
472 static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
473                             const char *buf, size_t count)
474 {
475         struct i2c_client *client = to_i2c_client(dev);
476         struct lm90_data *data = i2c_get_clientdata(client);
477         long val = simple_strtol(buf, NULL, 10);
478         int temp;
479
480         mutex_lock(&data->update_lock);
481         if (data->kind == adt7461)
482                 temp = temp_from_u8_adt7461(data, data->temp8[2]);
483         else if (data->kind == max6646)
484                 temp = temp_from_u8(data->temp8[2]);
485         else
486                 temp = temp_from_s8(data->temp8[2]);
487
488         data->temp_hyst = hyst_to_reg(temp - val);
489         i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
490                                   data->temp_hyst);
491         mutex_unlock(&data->update_lock);
492         return count;
493 }
494
495 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
496                            char *buf)
497 {
498         struct lm90_data *data = lm90_update_device(dev);
499         return sprintf(buf, "%d\n", data->alarms);
500 }
501
502 static ssize_t show_alarm(struct device *dev, struct device_attribute
503                           *devattr, char *buf)
504 {
505         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
506         struct lm90_data *data = lm90_update_device(dev);
507         int bitnr = attr->index;
508
509         return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
510 }
511
512 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
513 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
514 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
515         set_temp8, 0);
516 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
517         set_temp11, 1);
518 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
519         set_temp8, 1);
520 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
521         set_temp11, 2);
522 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
523         set_temp8, 2);
524 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
525         set_temp8, 3);
526 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
527         set_temphyst, 2);
528 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
529 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
530         set_temp11, 3);
531
532 /* Individual alarm files */
533 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
534 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
535 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
536 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
537 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
538 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
539 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
540 /* Raw alarm file for compatibility */
541 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
542
543 static struct attribute *lm90_attributes[] = {
544         &sensor_dev_attr_temp1_input.dev_attr.attr,
545         &sensor_dev_attr_temp2_input.dev_attr.attr,
546         &sensor_dev_attr_temp1_min.dev_attr.attr,
547         &sensor_dev_attr_temp2_min.dev_attr.attr,
548         &sensor_dev_attr_temp1_max.dev_attr.attr,
549         &sensor_dev_attr_temp2_max.dev_attr.attr,
550         &sensor_dev_attr_temp1_crit.dev_attr.attr,
551         &sensor_dev_attr_temp2_crit.dev_attr.attr,
552         &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
553         &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
554
555         &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
556         &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
557         &sensor_dev_attr_temp2_fault.dev_attr.attr,
558         &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
559         &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
560         &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
561         &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
562         &dev_attr_alarms.attr,
563         NULL
564 };
565
566 static const struct attribute_group lm90_group = {
567         .attrs = lm90_attributes,
568 };
569
570 /* pec used for ADM1032 only */
571 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
572                         char *buf)
573 {
574         struct i2c_client *client = to_i2c_client(dev);
575         return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
576 }
577
578 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
579                        const char *buf, size_t count)
580 {
581         struct i2c_client *client = to_i2c_client(dev);
582         long val = simple_strtol(buf, NULL, 10);
583
584         switch (val) {
585         case 0:
586                 client->flags &= ~I2C_CLIENT_PEC;
587                 break;
588         case 1:
589                 client->flags |= I2C_CLIENT_PEC;
590                 break;
591         default:
592                 return -EINVAL;
593         }
594
595         return count;
596 }
597
598 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
599
600 /*
601  * Real code
602  */
603
604 /* The ADM1032 supports PEC but not on write byte transactions, so we need
605    to explicitly ask for a transaction without PEC. */
606 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
607 {
608         return i2c_smbus_xfer(client->adapter, client->addr,
609                               client->flags & ~I2C_CLIENT_PEC,
610                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
611 }
612
613 /* It is assumed that client->update_lock is held (unless we are in
614    detection or initialization steps). This matters when PEC is enabled,
615    because we don't want the address pointer to change between the write
616    byte and the read byte transactions. */
617 static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
618 {
619         int err;
620
621         if (client->flags & I2C_CLIENT_PEC) {
622                 err = adm1032_write_byte(client, reg);
623                 if (err >= 0)
624                         err = i2c_smbus_read_byte(client);
625         } else
626                 err = i2c_smbus_read_byte_data(client, reg);
627
628         if (err < 0) {
629                 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
630                          reg, err);
631                 return err;
632         }
633         *value = err;
634
635         return 0;
636 }
637
638 /* Return 0 if detection is successful, -ENODEV otherwise */
639 static int lm90_detect(struct i2c_client *new_client, int kind,
640                        struct i2c_board_info *info)
641 {
642         struct i2c_adapter *adapter = new_client->adapter;
643         int address = new_client->addr;
644         const char *name = "";
645
646         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
647                 return -ENODEV;
648
649         /*
650          * Now we do the remaining detection. A negative kind means that
651          * the driver was loaded with no force parameter (default), so we
652          * must both detect and identify the chip. A zero kind means that
653          * the driver was loaded with the force parameter, the detection
654          * step shall be skipped. A positive kind means that the driver
655          * was loaded with the force parameter and a given kind of chip is
656          * requested, so both the detection and the identification steps
657          * are skipped.
658          */
659
660         /* Default to an LM90 if forced */
661         if (kind == 0)
662                 kind = lm90;
663
664         if (kind < 0) { /* detection and identification */
665                 int man_id, chip_id, reg_config1, reg_convrate;
666
667                 if ((man_id = i2c_smbus_read_byte_data(new_client,
668                                                 LM90_REG_R_MAN_ID)) < 0
669                  || (chip_id = i2c_smbus_read_byte_data(new_client,
670                                                 LM90_REG_R_CHIP_ID)) < 0
671                  || (reg_config1 = i2c_smbus_read_byte_data(new_client,
672                                                 LM90_REG_R_CONFIG1)) < 0
673                  || (reg_convrate = i2c_smbus_read_byte_data(new_client,
674                                                 LM90_REG_R_CONVRATE)) < 0)
675                         return -ENODEV;
676                 
677                 if ((address == 0x4C || address == 0x4D)
678                  && man_id == 0x01) { /* National Semiconductor */
679                         int reg_config2;
680
681                         if ((reg_config2 = i2c_smbus_read_byte_data(new_client,
682                                                 LM90_REG_R_CONFIG2)) < 0)
683                                 return -ENODEV;
684
685                         if ((reg_config1 & 0x2A) == 0x00
686                          && (reg_config2 & 0xF8) == 0x00
687                          && reg_convrate <= 0x09) {
688                                 if (address == 0x4C
689                                  && (chip_id & 0xF0) == 0x20) { /* LM90 */
690                                         kind = lm90;
691                                 } else
692                                 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
693                                         kind = lm99;
694                                 } else
695                                 if (address == 0x4C
696                                  && (chip_id & 0xF0) == 0x10) { /* LM86 */
697                                         kind = lm86;
698                                 }
699                         }
700                 } else
701                 if ((address == 0x4C || address == 0x4D)
702                  && man_id == 0x41) { /* Analog Devices */
703                         if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
704                          && (reg_config1 & 0x3F) == 0x00
705                          && reg_convrate <= 0x0A) {
706                                 kind = adm1032;
707                         } else
708                         if (chip_id == 0x51 /* ADT7461 */
709                          && (reg_config1 & 0x1B) == 0x00
710                          && reg_convrate <= 0x0A) {
711                                 kind = adt7461;
712                         }
713                 } else
714                 if (man_id == 0x4D) { /* Maxim */
715                         /*
716                          * The MAX6657, MAX6658 and MAX6659 do NOT have a
717                          * chip_id register. Reading from that address will
718                          * return the last read value, which in our case is
719                          * those of the man_id register. Likewise, the config1
720                          * register seems to lack a low nibble, so the value
721                          * will be those of the previous read, so in our case
722                          * those of the man_id register.
723                          */
724                         if (chip_id == man_id
725                          && (address == 0x4C || address == 0x4D)
726                          && (reg_config1 & 0x1F) == (man_id & 0x0F)
727                          && reg_convrate <= 0x09) {
728                                 kind = max6657;
729                         } else
730                         /* The chip_id register of the MAX6680 and MAX6681
731                          * holds the revision of the chip.
732                          * the lowest bit of the config1 register is unused
733                          * and should return zero when read, so should the
734                          * second to last bit of config1 (software reset)
735                          */
736                         if (chip_id == 0x01
737                          && (reg_config1 & 0x03) == 0x00
738                          && reg_convrate <= 0x07) {
739                                 kind = max6680;
740                         } else
741                         /* The chip_id register of the MAX6646/6647/6649
742                          * holds the revision of the chip.
743                          * The lowest 6 bits of the config1 register are
744                          * unused and should return zero when read.
745                          */
746                         if (chip_id == 0x59
747                          && (reg_config1 & 0x3f) == 0x00
748                          && reg_convrate <= 0x07) {
749                                 kind = max6646;
750                         }
751                 }
752
753                 if (kind <= 0) { /* identification failed */
754                         dev_dbg(&adapter->dev,
755                                 "Unsupported chip at 0x%02x (man_id=0x%02X, "
756                                 "chip_id=0x%02X)\n", address, man_id, chip_id);
757                         return -ENODEV;
758                 }
759         }
760
761         /* Fill the i2c board info */
762         if (kind == lm90) {
763                 name = "lm90";
764         } else if (kind == adm1032) {
765                 name = "adm1032";
766                 /* The ADM1032 supports PEC, but only if combined
767                    transactions are not used. */
768                 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
769                         info->flags |= I2C_CLIENT_PEC;
770         } else if (kind == lm99) {
771                 name = "lm99";
772         } else if (kind == lm86) {
773                 name = "lm86";
774         } else if (kind == max6657) {
775                 name = "max6657";
776         } else if (kind == max6680) {
777                 name = "max6680";
778         } else if (kind == adt7461) {
779                 name = "adt7461";
780         } else if (kind == max6646) {
781                 name = "max6646";
782         }
783         strlcpy(info->type, name, I2C_NAME_SIZE);
784
785         return 0;
786 }
787
788 static int lm90_probe(struct i2c_client *new_client,
789                       const struct i2c_device_id *id)
790 {
791         struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
792         struct lm90_data *data;
793         int err;
794
795         data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
796         if (!data) {
797                 err = -ENOMEM;
798                 goto exit;
799         }
800         i2c_set_clientdata(new_client, data);
801         mutex_init(&data->update_lock);
802
803         /* Set the device type */
804         data->kind = id->driver_data;
805         if (data->kind == adm1032) {
806                 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
807                         new_client->flags &= ~I2C_CLIENT_PEC;
808         }
809
810         /* Initialize the LM90 chip */
811         lm90_init_client(new_client);
812
813         /* Register sysfs hooks */
814         if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
815                 goto exit_free;
816         if (new_client->flags & I2C_CLIENT_PEC) {
817                 if ((err = device_create_file(&new_client->dev,
818                                               &dev_attr_pec)))
819                         goto exit_remove_files;
820         }
821         if (data->kind != max6657 && data->kind != max6646) {
822                 if ((err = device_create_file(&new_client->dev,
823                                 &sensor_dev_attr_temp2_offset.dev_attr)))
824                         goto exit_remove_files;
825         }
826
827         data->hwmon_dev = hwmon_device_register(&new_client->dev);
828         if (IS_ERR(data->hwmon_dev)) {
829                 err = PTR_ERR(data->hwmon_dev);
830                 goto exit_remove_files;
831         }
832
833         return 0;
834
835 exit_remove_files:
836         sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
837         device_remove_file(&new_client->dev, &dev_attr_pec);
838 exit_free:
839         kfree(data);
840 exit:
841         return err;
842 }
843
844 static void lm90_init_client(struct i2c_client *client)
845 {
846         u8 config, config_orig;
847         struct lm90_data *data = i2c_get_clientdata(client);
848
849         /*
850          * Start the conversions.
851          */
852         i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
853                                   5); /* 2 Hz */
854         if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
855                 dev_warn(&client->dev, "Initialization failed!\n");
856                 return;
857         }
858         config_orig = config;
859
860         /* Check Temperature Range Select */
861         if (data->kind == adt7461) {
862                 if (config & 0x04)
863                         data->flags |= LM90_FLAG_ADT7461_EXT;
864         }
865
866         /*
867          * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
868          * 0.125 degree resolution) and range (0x08, extend range
869          * to -64 degree) mode for the remote temperature sensor.
870          */
871         if (data->kind == max6680) {
872                 config |= 0x18;
873         }
874
875         config &= 0xBF; /* run */
876         if (config != config_orig) /* Only write if changed */
877                 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
878 }
879
880 static int lm90_remove(struct i2c_client *client)
881 {
882         struct lm90_data *data = i2c_get_clientdata(client);
883
884         hwmon_device_unregister(data->hwmon_dev);
885         sysfs_remove_group(&client->dev.kobj, &lm90_group);
886         device_remove_file(&client->dev, &dev_attr_pec);
887         if (data->kind != max6657 && data->kind != max6646)
888                 device_remove_file(&client->dev,
889                                    &sensor_dev_attr_temp2_offset.dev_attr);
890
891         kfree(data);
892         return 0;
893 }
894
895 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
896 {
897         int err;
898         u8 oldh, newh, l;
899
900         /*
901          * There is a trick here. We have to read two registers to have the
902          * sensor temperature, but we have to beware a conversion could occur
903          * inbetween the readings. The datasheet says we should either use
904          * the one-shot conversion register, which we don't want to do
905          * (disables hardware monitoring) or monitor the busy bit, which is
906          * impossible (we can't read the values and monitor that bit at the
907          * exact same time). So the solution used here is to read the high
908          * byte once, then the low byte, then the high byte again. If the new
909          * high byte matches the old one, then we have a valid reading. Else
910          * we have to read the low byte again, and now we believe we have a
911          * correct reading.
912          */
913         if ((err = lm90_read_reg(client, regh, &oldh))
914          || (err = lm90_read_reg(client, regl, &l))
915          || (err = lm90_read_reg(client, regh, &newh)))
916                 return err;
917         if (oldh != newh) {
918                 err = lm90_read_reg(client, regl, &l);
919                 if (err)
920                         return err;
921         }
922         *value = (newh << 8) | l;
923
924         return 0;
925 }
926
927 static struct lm90_data *lm90_update_device(struct device *dev)
928 {
929         struct i2c_client *client = to_i2c_client(dev);
930         struct lm90_data *data = i2c_get_clientdata(client);
931
932         mutex_lock(&data->update_lock);
933
934         if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
935                 u8 h, l;
936
937                 dev_dbg(&client->dev, "Updating lm90 data.\n");
938                 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
939                 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
940                 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
941                 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
942                 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
943
944                 if (data->kind == max6657 || data->kind == max6646) {
945                         lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
946                                     MAX6657_REG_R_LOCAL_TEMPL,
947                                     &data->temp11[4]);
948                 } else {
949                         if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
950                                           &h) == 0)
951                                 data->temp11[4] = h << 8;
952                 }
953                 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
954                             LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
955
956                 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
957                         data->temp11[1] = h << 8;
958                         if (data->kind != max6657 && data->kind != max6680
959                          && data->kind != max6646
960                          && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
961                                           &l) == 0)
962                                 data->temp11[1] |= l;
963                 }
964                 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
965                         data->temp11[2] = h << 8;
966                         if (data->kind != max6657 && data->kind != max6680
967                          && data->kind != max6646
968                          && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
969                                           &l) == 0)
970                                 data->temp11[2] |= l;
971                 }
972
973                 if (data->kind != max6657 && data->kind != max6646) {
974                         if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
975                                           &h) == 0
976                          && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
977                                           &l) == 0)
978                                 data->temp11[3] = (h << 8) | l;
979                 }
980                 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
981
982                 data->last_updated = jiffies;
983                 data->valid = 1;
984         }
985
986         mutex_unlock(&data->update_lock);
987
988         return data;
989 }
990
991 static int __init sensors_lm90_init(void)
992 {
993         return i2c_add_driver(&lm90_driver);
994 }
995
996 static void __exit sensors_lm90_exit(void)
997 {
998         i2c_del_driver(&lm90_driver);
999 }
1000
1001 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1002 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1003 MODULE_LICENSE("GPL");
1004
1005 module_init(sensors_lm90_init);
1006 module_exit(sensors_lm90_exit);