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