hwmon: (lm80) Convert voltage display function macros into functions
[firefly-linux-kernel-4.4.55.git] / drivers / hwmon / lm80.c
1 /*
2  * lm80.c - From lm_sensors, Linux kernel modules for hardware
3  *          monitoring
4  * Copyright (C) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5  *                           and Philip Edelbrock <phil@netroedge.com>
6  *
7  * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33
34 /* Addresses to scan */
35 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
36                                                 0x2e, 0x2f, I2C_CLIENT_END };
37
38 /* Many LM80 constants specified below */
39
40 /* The LM80 registers */
41 #define LM80_REG_IN_MAX(nr)             (0x2a + (nr) * 2)
42 #define LM80_REG_IN_MIN(nr)             (0x2b + (nr) * 2)
43 #define LM80_REG_IN(nr)                 (0x20 + (nr))
44
45 #define LM80_REG_FAN1                   0x28
46 #define LM80_REG_FAN2                   0x29
47 #define LM80_REG_FAN_MIN(nr)            (0x3b + (nr))
48
49 #define LM80_REG_TEMP                   0x27
50 #define LM80_REG_TEMP_HOT_MAX           0x38
51 #define LM80_REG_TEMP_HOT_HYST          0x39
52 #define LM80_REG_TEMP_OS_MAX            0x3a
53 #define LM80_REG_TEMP_OS_HYST           0x3b
54
55 #define LM80_REG_CONFIG                 0x00
56 #define LM80_REG_ALARM1                 0x01
57 #define LM80_REG_ALARM2                 0x02
58 #define LM80_REG_MASK1                  0x03
59 #define LM80_REG_MASK2                  0x04
60 #define LM80_REG_FANDIV                 0x05
61 #define LM80_REG_RES                    0x06
62
63 #define LM96080_REG_CONV_RATE           0x07
64 #define LM96080_REG_MAN_ID              0x3e
65 #define LM96080_REG_DEV_ID              0x3f
66
67
68 /*
69  * Conversions. Rounding and limit checking is only done on the TO_REG
70  * variants. Note that you should be a bit careful with which arguments
71  * these macros are called: arguments may be evaluated more than once.
72  * Fixing this is just not worth it.
73  */
74
75 #define IN_TO_REG(val)          (clamp_val(((val) + 5) / 10, 0, 255))
76 #define IN_FROM_REG(val)        ((val) * 10)
77
78 static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
79 {
80         if (rpm == 0)
81                 return 255;
82         rpm = clamp_val(rpm, 1, 1000000);
83         return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
84 }
85
86 #define FAN_FROM_REG(val, div)  ((val) == 0 ? -1 : \
87                                 (val) == 255 ? 0 : 1350000/((div) * (val)))
88
89 #define TEMP_FROM_REG(reg)      ((reg) * 125 / 32)
90 #define TEMP_TO_REG(temp)       (DIV_ROUND_CLOSEST(clamp_val((temp), \
91                                         -128000, 127000), 1000) << 8)
92
93 #define DIV_FROM_REG(val)               (1 << (val))
94
95 enum temp_index {
96         t_input = 0,
97         t_hot_max,
98         t_hot_hyst,
99         t_os_max,
100         t_os_hyst,
101         t_num_temp
102 };
103
104 static const u8 temp_regs[t_num_temp] = {
105         [t_input] = LM80_REG_TEMP,
106         [t_hot_max] = LM80_REG_TEMP_HOT_MAX,
107         [t_hot_hyst] = LM80_REG_TEMP_HOT_HYST,
108         [t_os_max] = LM80_REG_TEMP_OS_MAX,
109         [t_os_hyst] = LM80_REG_TEMP_OS_HYST,
110 };
111
112 enum in_index {
113         i_input = 0,
114         i_max,
115         i_min,
116         i_num_in
117 };
118
119 /*
120  * Client data (each client gets its own)
121  */
122
123 struct lm80_data {
124         struct i2c_client *client;
125         struct mutex update_lock;
126         char error;             /* !=0 if error occurred during last update */
127         char valid;             /* !=0 if following fields are valid */
128         unsigned long last_updated;     /* In jiffies */
129
130         u8 in[i_num_in][7];     /* Register value, 1st index is enum in_index */
131         u8 fan[2];              /* Register value */
132         u8 fan_min[2];          /* Register value */
133         u8 fan_div[2];          /* Register encoding, shifted right */
134         s16 temp[t_num_temp];   /* Register values, normalized to 16 bit */
135         u16 alarms;             /* Register encoding, combined */
136 };
137
138 /*
139  * Functions declaration
140  */
141
142 static int lm80_probe(struct i2c_client *client,
143                       const struct i2c_device_id *id);
144 static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info);
145 static void lm80_init_client(struct i2c_client *client);
146 static struct lm80_data *lm80_update_device(struct device *dev);
147 static int lm80_read_value(struct i2c_client *client, u8 reg);
148 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
149
150 /*
151  * Driver data (common to all clients)
152  */
153
154 static const struct i2c_device_id lm80_id[] = {
155         { "lm80", 0 },
156         { "lm96080", 1 },
157         { }
158 };
159 MODULE_DEVICE_TABLE(i2c, lm80_id);
160
161 static struct i2c_driver lm80_driver = {
162         .class          = I2C_CLASS_HWMON,
163         .driver = {
164                 .name   = "lm80",
165         },
166         .probe          = lm80_probe,
167         .id_table       = lm80_id,
168         .detect         = lm80_detect,
169         .address_list   = normal_i2c,
170 };
171
172 /*
173  * Sysfs stuff
174  */
175
176 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
177                        char *buf)
178 {
179         struct lm80_data *data = lm80_update_device(dev);
180         int index = to_sensor_dev_attr_2(attr)->index;
181         int nr = to_sensor_dev_attr_2(attr)->nr;
182
183         if (IS_ERR(data))
184                 return PTR_ERR(data);
185         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr][index]));
186 }
187
188 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
189                       const char *buf, size_t count)
190 {
191         struct lm80_data *data = dev_get_drvdata(dev);
192         struct i2c_client *client = data->client;
193         int index = to_sensor_dev_attr_2(attr)->index;
194         int nr = to_sensor_dev_attr_2(attr)->nr;
195         long val;
196         u8 reg;
197         int err = kstrtol(buf, 10, &val);
198         if (err < 0)
199                 return err;
200
201         reg = nr == i_min ? LM80_REG_IN_MIN(index) : LM80_REG_IN_MAX(index);
202
203         mutex_lock(&data->update_lock);
204         data->in[nr][index] = IN_TO_REG(val);
205         lm80_write_value(client, reg, data->in[nr][index]);
206         mutex_unlock(&data->update_lock);
207         return count;
208 }
209
210 #define show_fan(suffix, value) \
211 static ssize_t show_fan_##suffix(struct device *dev, \
212         struct device_attribute *attr, char *buf) \
213 { \
214         int nr = to_sensor_dev_attr(attr)->index; \
215         struct lm80_data *data = lm80_update_device(dev); \
216         if (IS_ERR(data)) \
217                 return PTR_ERR(data); \
218         return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
219                        DIV_FROM_REG(data->fan_div[nr]))); \
220 }
221 show_fan(min, fan_min)
222 show_fan(input, fan)
223
224 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
225         char *buf)
226 {
227         int nr = to_sensor_dev_attr(attr)->index;
228         struct lm80_data *data = lm80_update_device(dev);
229         if (IS_ERR(data))
230                 return PTR_ERR(data);
231         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
232 }
233
234 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
235         const char *buf, size_t count)
236 {
237         int nr = to_sensor_dev_attr(attr)->index;
238         struct lm80_data *data = dev_get_drvdata(dev);
239         struct i2c_client *client = data->client;
240         unsigned long val;
241         int err = kstrtoul(buf, 10, &val);
242         if (err < 0)
243                 return err;
244
245         mutex_lock(&data->update_lock);
246         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
247         lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
248         mutex_unlock(&data->update_lock);
249         return count;
250 }
251
252 /*
253  * Note: we save and restore the fan minimum here, because its value is
254  * determined in part by the fan divisor.  This follows the principle of
255  * least surprise; the user doesn't expect the fan minimum to change just
256  * because the divisor changed.
257  */
258 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
259         const char *buf, size_t count)
260 {
261         int nr = to_sensor_dev_attr(attr)->index;
262         struct lm80_data *data = dev_get_drvdata(dev);
263         struct i2c_client *client = data->client;
264         unsigned long min, val;
265         u8 reg;
266         int err = kstrtoul(buf, 10, &val);
267         if (err < 0)
268                 return err;
269
270         /* Save fan_min */
271         mutex_lock(&data->update_lock);
272         min = FAN_FROM_REG(data->fan_min[nr],
273                            DIV_FROM_REG(data->fan_div[nr]));
274
275         switch (val) {
276         case 1:
277                 data->fan_div[nr] = 0;
278                 break;
279         case 2:
280                 data->fan_div[nr] = 1;
281                 break;
282         case 4:
283                 data->fan_div[nr] = 2;
284                 break;
285         case 8:
286                 data->fan_div[nr] = 3;
287                 break;
288         default:
289                 dev_err(dev,
290                         "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
291                         val);
292                 mutex_unlock(&data->update_lock);
293                 return -EINVAL;
294         }
295
296         reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
297             | (data->fan_div[nr] << (2 * (nr + 1)));
298         lm80_write_value(client, LM80_REG_FANDIV, reg);
299
300         /* Restore fan_min */
301         data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
302         lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
303         mutex_unlock(&data->update_lock);
304
305         return count;
306 }
307
308 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
309                          char *buf)
310 {
311         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
312         struct lm80_data *data = lm80_update_device(dev);
313         if (IS_ERR(data))
314                 return PTR_ERR(data);
315         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
316 }
317
318 static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
319                         const char *buf, size_t count)
320 {
321         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
322         struct lm80_data *data = dev_get_drvdata(dev);
323         struct i2c_client *client = data->client;
324         int nr = attr->index;
325         long val;
326         int err = kstrtol(buf, 10, &val);
327         if (err < 0)
328                 return err;
329
330         mutex_lock(&data->update_lock);
331         data->temp[nr] = TEMP_TO_REG(val);
332         lm80_write_value(client, temp_regs[nr], data->temp[nr] >> 8);
333         mutex_unlock(&data->update_lock);
334         return count;
335 }
336
337 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
338                            char *buf)
339 {
340         struct lm80_data *data = lm80_update_device(dev);
341         if (IS_ERR(data))
342                 return PTR_ERR(data);
343         return sprintf(buf, "%u\n", data->alarms);
344 }
345
346 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
347                           char *buf)
348 {
349         int bitnr = to_sensor_dev_attr(attr)->index;
350         struct lm80_data *data = lm80_update_device(dev);
351         if (IS_ERR(data))
352                 return PTR_ERR(data);
353         return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
354 }
355
356 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO,
357                 show_in, set_in, i_min, 0);
358 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO,
359                 show_in, set_in, i_min, 1);
360 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO,
361                 show_in, set_in, i_min, 2);
362 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO,
363                 show_in, set_in, i_min, 3);
364 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO,
365                 show_in, set_in, i_min, 4);
366 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO,
367                 show_in, set_in, i_min, 5);
368 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO,
369                 show_in, set_in, i_min, 6);
370 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO,
371                 show_in, set_in, i_max, 0);
372 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO,
373                 show_in, set_in, i_max, 1);
374 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO,
375                 show_in, set_in, i_max, 2);
376 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO,
377                 show_in, set_in, i_max, 3);
378 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO,
379                 show_in, set_in, i_max, 4);
380 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO,
381                 show_in, set_in, i_max, 5);
382 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO,
383                 show_in, set_in, i_max, 6);
384 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, i_input, 0);
385 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, i_input, 1);
386 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, i_input, 2);
387 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, i_input, 3);
388 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, i_input, 4);
389 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, i_input, 5);
390 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, i_input, 6);
391 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
392                 show_fan_min, set_fan_min, 0);
393 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
394                 show_fan_min, set_fan_min, 1);
395 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
396 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
397 static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
398                 show_fan_div, set_fan_div, 0);
399 static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
400                 show_fan_div, set_fan_div, 1);
401 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input);
402 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
403                 set_temp, t_hot_max);
404 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp,
405                 set_temp, t_hot_hyst);
406 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp,
407                 set_temp, t_os_max);
408 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp,
409                 set_temp, t_os_hyst);
410 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
411 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
412 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
413 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
414 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
415 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
416 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
417 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
418 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
419 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
420 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
421 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
422
423 /*
424  * Real code
425  */
426
427 static struct attribute *lm80_attrs[] = {
428         &sensor_dev_attr_in0_min.dev_attr.attr,
429         &sensor_dev_attr_in1_min.dev_attr.attr,
430         &sensor_dev_attr_in2_min.dev_attr.attr,
431         &sensor_dev_attr_in3_min.dev_attr.attr,
432         &sensor_dev_attr_in4_min.dev_attr.attr,
433         &sensor_dev_attr_in5_min.dev_attr.attr,
434         &sensor_dev_attr_in6_min.dev_attr.attr,
435         &sensor_dev_attr_in0_max.dev_attr.attr,
436         &sensor_dev_attr_in1_max.dev_attr.attr,
437         &sensor_dev_attr_in2_max.dev_attr.attr,
438         &sensor_dev_attr_in3_max.dev_attr.attr,
439         &sensor_dev_attr_in4_max.dev_attr.attr,
440         &sensor_dev_attr_in5_max.dev_attr.attr,
441         &sensor_dev_attr_in6_max.dev_attr.attr,
442         &sensor_dev_attr_in0_input.dev_attr.attr,
443         &sensor_dev_attr_in1_input.dev_attr.attr,
444         &sensor_dev_attr_in2_input.dev_attr.attr,
445         &sensor_dev_attr_in3_input.dev_attr.attr,
446         &sensor_dev_attr_in4_input.dev_attr.attr,
447         &sensor_dev_attr_in5_input.dev_attr.attr,
448         &sensor_dev_attr_in6_input.dev_attr.attr,
449         &sensor_dev_attr_fan1_min.dev_attr.attr,
450         &sensor_dev_attr_fan2_min.dev_attr.attr,
451         &sensor_dev_attr_fan1_input.dev_attr.attr,
452         &sensor_dev_attr_fan2_input.dev_attr.attr,
453         &sensor_dev_attr_fan1_div.dev_attr.attr,
454         &sensor_dev_attr_fan2_div.dev_attr.attr,
455         &sensor_dev_attr_temp1_input.dev_attr.attr,
456         &sensor_dev_attr_temp1_max.dev_attr.attr,
457         &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
458         &sensor_dev_attr_temp1_crit.dev_attr.attr,
459         &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
460         &dev_attr_alarms.attr,
461         &sensor_dev_attr_in0_alarm.dev_attr.attr,
462         &sensor_dev_attr_in1_alarm.dev_attr.attr,
463         &sensor_dev_attr_in2_alarm.dev_attr.attr,
464         &sensor_dev_attr_in3_alarm.dev_attr.attr,
465         &sensor_dev_attr_in4_alarm.dev_attr.attr,
466         &sensor_dev_attr_in5_alarm.dev_attr.attr,
467         &sensor_dev_attr_in6_alarm.dev_attr.attr,
468         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
469         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
470         &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
471         &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
472         NULL
473 };
474 ATTRIBUTE_GROUPS(lm80);
475
476 /* Return 0 if detection is successful, -ENODEV otherwise */
477 static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
478 {
479         struct i2c_adapter *adapter = client->adapter;
480         int i, cur, man_id, dev_id;
481         const char *name = NULL;
482
483         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
484                 return -ENODEV;
485
486         /* First check for unused bits, common to both chip types */
487         if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
488          || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80))
489                 return -ENODEV;
490
491         /*
492          * The LM96080 has manufacturer and stepping/die rev registers so we
493          * can just check that. The LM80 does not have such registers so we
494          * have to use a more expensive trick.
495          */
496         man_id = lm80_read_value(client, LM96080_REG_MAN_ID);
497         dev_id = lm80_read_value(client, LM96080_REG_DEV_ID);
498         if (man_id == 0x01 && dev_id == 0x08) {
499                 /* Check more unused bits for confirmation */
500                 if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe)
501                         return -ENODEV;
502
503                 name = "lm96080";
504         } else {
505                 /* Check 6-bit addressing */
506                 for (i = 0x2a; i <= 0x3d; i++) {
507                         cur = i2c_smbus_read_byte_data(client, i);
508                         if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
509                          || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
510                          || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
511                                 return -ENODEV;
512                 }
513
514                 name = "lm80";
515         }
516
517         strlcpy(info->type, name, I2C_NAME_SIZE);
518
519         return 0;
520 }
521
522 static int lm80_probe(struct i2c_client *client,
523                       const struct i2c_device_id *id)
524 {
525         struct device *dev = &client->dev;
526         struct device *hwmon_dev;
527         struct lm80_data *data;
528
529         data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL);
530         if (!data)
531                 return -ENOMEM;
532
533         data->client = client;
534         mutex_init(&data->update_lock);
535
536         /* Initialize the LM80 chip */
537         lm80_init_client(client);
538
539         /* A few vars need to be filled upon startup */
540         data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
541         data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
542
543         hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
544                                                            data, lm80_groups);
545
546         return PTR_ERR_OR_ZERO(hwmon_dev);
547 }
548
549 static int lm80_read_value(struct i2c_client *client, u8 reg)
550 {
551         return i2c_smbus_read_byte_data(client, reg);
552 }
553
554 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
555 {
556         return i2c_smbus_write_byte_data(client, reg, value);
557 }
558
559 /* Called when we have found a new LM80. */
560 static void lm80_init_client(struct i2c_client *client)
561 {
562         /*
563          * Reset all except Watchdog values and last conversion values
564          * This sets fan-divs to 2, among others. This makes most other
565          * initializations unnecessary
566          */
567         lm80_write_value(client, LM80_REG_CONFIG, 0x80);
568         /* Set 11-bit temperature resolution */
569         lm80_write_value(client, LM80_REG_RES, 0x08);
570
571         /* Start monitoring */
572         lm80_write_value(client, LM80_REG_CONFIG, 0x01);
573 }
574
575 static struct lm80_data *lm80_update_device(struct device *dev)
576 {
577         struct lm80_data *data = dev_get_drvdata(dev);
578         struct i2c_client *client = data->client;
579         int i;
580         int rv;
581         int prev_rv;
582         struct lm80_data *ret = data;
583
584         mutex_lock(&data->update_lock);
585
586         if (data->error)
587                 lm80_init_client(client);
588
589         if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
590                 dev_dbg(dev, "Starting lm80 update\n");
591                 for (i = 0; i <= 6; i++) {
592                         rv = lm80_read_value(client, LM80_REG_IN(i));
593                         if (rv < 0)
594                                 goto abort;
595                         data->in[i_input][i] = rv;
596
597                         rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
598                         if (rv < 0)
599                                 goto abort;
600                         data->in[i_min][i] = rv;
601
602                         rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
603                         if (rv < 0)
604                                 goto abort;
605                         data->in[i_max][i] = rv;
606                 }
607
608                 rv = lm80_read_value(client, LM80_REG_FAN1);
609                 if (rv < 0)
610                         goto abort;
611                 data->fan[0] = rv;
612
613                 rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
614                 if (rv < 0)
615                         goto abort;
616                 data->fan_min[0] = rv;
617
618                 rv = lm80_read_value(client, LM80_REG_FAN2);
619                 if (rv < 0)
620                         goto abort;
621                 data->fan[1] = rv;
622
623                 rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
624                 if (rv < 0)
625                         goto abort;
626                 data->fan_min[1] = rv;
627
628                 prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
629                 if (rv < 0)
630                         goto abort;
631                 rv = lm80_read_value(client, LM80_REG_RES);
632                 if (rv < 0)
633                         goto abort;
634                 data->temp[t_input] = (prev_rv << 8) | (rv & 0xf0);
635
636                 for (i = t_input + 1; i < t_num_temp; i++) {
637                         rv = lm80_read_value(client, temp_regs[i]);
638                         if (rv < 0)
639                                 goto abort;
640                         data->temp[i] = rv << 8;
641                 }
642
643                 rv = lm80_read_value(client, LM80_REG_FANDIV);
644                 if (rv < 0)
645                         goto abort;
646                 data->fan_div[0] = (rv >> 2) & 0x03;
647                 data->fan_div[1] = (rv >> 4) & 0x03;
648
649                 prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
650                 if (rv < 0)
651                         goto abort;
652                 rv = lm80_read_value(client, LM80_REG_ALARM2);
653                 if (rv < 0)
654                         goto abort;
655                 data->alarms = prev_rv + (rv << 8);
656
657                 data->last_updated = jiffies;
658                 data->valid = 1;
659                 data->error = 0;
660         }
661         goto done;
662
663 abort:
664         ret = ERR_PTR(rv);
665         data->valid = 0;
666         data->error = 1;
667
668 done:
669         mutex_unlock(&data->update_lock);
670
671         return ret;
672 }
673
674 module_i2c_driver(lm80_driver);
675
676 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
677         "Philip Edelbrock <phil@netroedge.com>");
678 MODULE_DESCRIPTION("LM80 driver");
679 MODULE_LICENSE("GPL");