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