Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / hwmon / adm9240.c
blobcc3e0184e720aaca4c35b7fccdd72e745ec99636
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
6 * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl>
7 * Philip Edelbrock <phil@netroedge.com>
8 * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl>
9 * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable
10 * guidance from Jean Delvare
12 * Driver supports Analog Devices ADM9240
13 * Dallas Semiconductor DS1780
14 * National Semiconductor LM81
16 * ADM9240 is the reference, DS1780 and LM81 are register compatibles
18 * Voltage Six inputs are scaled by chip, VID also reported
19 * Temperature Chip temperature to 0.5'C, maximum and max_hysteris
20 * Fans 2 fans, low speed alarm, automatic fan clock divider
21 * Alarms 16-bit map of active alarms
22 * Analog Out 0..1250 mV output
24 * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm'
26 * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
28 * LM81 extended temp reading not implemented
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/i2c.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/hwmon.h>
37 #include <linux/hwmon-vid.h>
38 #include <linux/err.h>
39 #include <linux/mutex.h>
40 #include <linux/jiffies.h>
41 #include <linux/regmap.h>
43 /* Addresses to scan */
44 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
45 I2C_CLIENT_END };
47 enum chips { adm9240, ds1780, lm81 };
49 /* ADM9240 registers */
50 #define ADM9240_REG_MAN_ID 0x3e
51 #define ADM9240_REG_DIE_REV 0x3f
52 #define ADM9240_REG_CONFIG 0x40
54 #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
55 #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
56 #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
57 #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
58 #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
59 #define ADM9240_REG_INT(nr) (0x41 + (nr))
60 #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
61 #define ADM9240_REG_TEMP 0x27
62 #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
63 #define ADM9240_REG_ANALOG_OUT 0x19
64 #define ADM9240_REG_CHASSIS_CLEAR 0x46
65 #define ADM9240_REG_VID_FAN_DIV 0x47
66 #define ADM9240_REG_I2C_ADDR 0x48
67 #define ADM9240_REG_VID4 0x49
68 #define ADM9240_REG_TEMP_CONF 0x4b
70 /* generalised scaling with integer rounding */
71 static inline int SCALE(long val, int mul, int div)
73 if (val < 0)
74 return (val * mul - div / 2) / div;
75 else
76 return (val * mul + div / 2) / div;
79 /* adm9240 internally scales voltage measurements */
80 static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
82 static inline unsigned int IN_FROM_REG(u8 reg, int n)
84 return SCALE(reg, nom_mv[n], 192);
87 static inline u8 IN_TO_REG(unsigned long val, int n)
89 val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
90 return SCALE(val, 192, nom_mv[n]);
93 /* temperature range: -40..125, 127 disables temperature alarm */
94 static inline s8 TEMP_TO_REG(long val)
96 val = clamp_val(val, -40000, 127000);
97 return SCALE(val, 1, 1000);
100 /* two fans, each with low fan speed limit */
101 static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
103 if (!reg) /* error */
104 return -1;
106 if (reg == 255)
107 return 0;
109 return SCALE(1350000, 1, reg * div);
112 /* analog out 0..1250mV */
113 static inline u8 AOUT_TO_REG(unsigned long val)
115 val = clamp_val(val, 0, 1250);
116 return SCALE(val, 255, 1250);
119 static inline unsigned int AOUT_FROM_REG(u8 reg)
121 return SCALE(reg, 1250, 255);
124 /* per client data */
125 struct adm9240_data {
126 struct i2c_client *client;
127 struct regmap *regmap;
128 struct mutex update_lock;
129 char valid;
130 unsigned long last_updated_measure;
131 unsigned long last_updated_config;
133 u8 in[6]; /* ro in0_input */
134 u8 in_max[6]; /* rw in0_max */
135 u8 in_min[6]; /* rw in0_min */
136 u8 fan[2]; /* ro fan1_input */
137 u8 fan_min[2]; /* rw fan1_min */
138 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
139 s16 temp; /* ro temp1_input, 9-bit sign-extended */
140 s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
141 u16 alarms; /* ro alarms */
142 u8 aout; /* rw aout_output */
143 u8 vid; /* ro vid */
144 u8 vrm; /* -- vrm set on startup, no accessor */
147 /* write new fan div, callers must hold data->update_lock */
148 static int adm9240_write_fan_div(struct adm9240_data *data, int nr,
149 u8 fan_div)
151 unsigned int reg, old, shift = (nr + 2) * 2;
152 int err;
154 err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, &reg);
155 if (err < 0)
156 return err;
157 old = (reg >> shift) & 3;
158 reg &= ~(3 << shift);
159 reg |= (fan_div << shift);
160 err = regmap_write(data->regmap, ADM9240_REG_VID_FAN_DIV, reg);
161 if (err < 0)
162 return err;
163 dev_dbg(&data->client->dev,
164 "fan%d clock divider changed from %u to %u\n",
165 nr + 1, 1 << old, 1 << fan_div);
167 return 0;
170 static int adm9240_update_measure(struct adm9240_data *data)
172 unsigned int val;
173 u8 regs[2];
174 int err;
175 int i;
177 err = regmap_bulk_read(data->regmap, ADM9240_REG_IN(0), &data->in[0], 6);
178 if (err < 0)
179 return err;
180 err = regmap_bulk_read(data->regmap, ADM9240_REG_INT(0), &regs, 2);
181 if (err < 0)
182 return err;
184 data->alarms = regs[0] | regs[1] << 8;
187 * read temperature: assume temperature changes less than
188 * 0.5'C per two measurement cycles thus ignore possible
189 * but unlikely aliasing error on lsb reading. --Grant
191 err = regmap_read(data->regmap, ADM9240_REG_TEMP, &val);
192 if (err < 0)
193 return err;
194 data->temp = val << 8;
195 err = regmap_read(data->regmap, ADM9240_REG_TEMP_CONF, &val);
196 if (err < 0)
197 return err;
198 data->temp |= val;
200 err = regmap_bulk_read(data->regmap, ADM9240_REG_FAN(0),
201 &data->fan[0], 2);
202 if (err < 0)
203 return err;
205 for (i = 0; i < 2; i++) { /* read fans */
206 /* adjust fan clock divider on overflow */
207 if (data->valid && data->fan[i] == 255 &&
208 data->fan_div[i] < 3) {
210 err = adm9240_write_fan_div(data, i,
211 ++data->fan_div[i]);
212 if (err < 0)
213 return err;
215 /* adjust fan_min if active, but not to 0 */
216 if (data->fan_min[i] < 255 &&
217 data->fan_min[i] >= 2)
218 data->fan_min[i] /= 2;
222 return 0;
225 static int adm9240_update_config(struct adm9240_data *data)
227 unsigned int val;
228 int i;
229 int err;
231 for (i = 0; i < 6; i++) {
232 err = regmap_raw_read(data->regmap, ADM9240_REG_IN_MIN(i),
233 &data->in_min[i], 1);
234 if (err < 0)
235 return err;
236 err = regmap_raw_read(data->regmap, ADM9240_REG_IN_MAX(i),
237 &data->in_max[i], 1);
238 if (err < 0)
239 return err;
241 err = regmap_bulk_read(data->regmap, ADM9240_REG_FAN_MIN(0),
242 &data->fan_min[0], 2);
243 if (err < 0)
244 return err;
245 err = regmap_bulk_read(data->regmap, ADM9240_REG_TEMP_MAX(0),
246 &data->temp_max[0], 2);
247 if (err < 0)
248 return err;
250 /* read fan divs and 5-bit VID */
251 err = regmap_read(data->regmap, ADM9240_REG_VID_FAN_DIV, &val);
252 if (err < 0)
253 return err;
254 data->fan_div[0] = (val >> 4) & 3;
255 data->fan_div[1] = (val >> 6) & 3;
256 data->vid = val & 0x0f;
257 err = regmap_read(data->regmap, ADM9240_REG_VID4, &val);
258 if (err < 0)
259 return err;
260 data->vid |= (val & 1) << 4;
261 /* read analog out */
262 err = regmap_raw_read(data->regmap, ADM9240_REG_ANALOG_OUT,
263 &data->aout, 1);
265 return err;
268 static struct adm9240_data *adm9240_update_device(struct device *dev)
270 struct adm9240_data *data = dev_get_drvdata(dev);
271 int err;
273 mutex_lock(&data->update_lock);
275 /* minimum measurement cycle: 1.75 seconds */
276 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
277 || !data->valid) {
278 err = adm9240_update_measure(data);
279 if (err < 0) {
280 data->valid = 0;
281 mutex_unlock(&data->update_lock);
282 return ERR_PTR(err);
284 data->last_updated_measure = jiffies;
287 /* minimum config reading cycle: 300 seconds */
288 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
289 || !data->valid) {
290 err = adm9240_update_config(data);
291 if (err < 0) {
292 data->valid = 0;
293 mutex_unlock(&data->update_lock);
294 return ERR_PTR(err);
296 data->last_updated_config = jiffies;
297 data->valid = 1;
299 mutex_unlock(&data->update_lock);
300 return data;
303 /*** sysfs accessors ***/
305 /* temperature */
306 static ssize_t temp1_input_show(struct device *dev,
307 struct device_attribute *dummy, char *buf)
309 struct adm9240_data *data = adm9240_update_device(dev);
311 if (IS_ERR(data))
312 return PTR_ERR(data);
314 return sprintf(buf, "%d\n", data->temp / 128 * 500); /* 9-bit value */
317 static ssize_t max_show(struct device *dev, struct device_attribute *devattr,
318 char *buf)
320 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
321 struct adm9240_data *data = adm9240_update_device(dev);
323 if (IS_ERR(data))
324 return PTR_ERR(data);
326 return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
329 static ssize_t max_store(struct device *dev, struct device_attribute *devattr,
330 const char *buf, size_t count)
332 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
333 struct adm9240_data *data = dev_get_drvdata(dev);
334 long val;
335 int err;
337 err = kstrtol(buf, 10, &val);
338 if (err)
339 return err;
341 mutex_lock(&data->update_lock);
342 data->temp_max[attr->index] = TEMP_TO_REG(val);
343 err = regmap_write(data->regmap, ADM9240_REG_TEMP_MAX(attr->index),
344 data->temp_max[attr->index]);
345 mutex_unlock(&data->update_lock);
346 return err < 0 ? err : count;
349 static DEVICE_ATTR_RO(temp1_input);
350 static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
351 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, max, 1);
353 /* voltage */
354 static ssize_t in_show(struct device *dev, struct device_attribute *devattr,
355 char *buf)
357 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
358 struct adm9240_data *data = adm9240_update_device(dev);
360 if (IS_ERR(data))
361 return PTR_ERR(data);
363 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
364 attr->index));
367 static ssize_t in_min_show(struct device *dev,
368 struct device_attribute *devattr, char *buf)
370 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
371 struct adm9240_data *data = adm9240_update_device(dev);
373 if (IS_ERR(data))
374 return PTR_ERR(data);
376 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
377 attr->index));
380 static ssize_t in_max_show(struct device *dev,
381 struct device_attribute *devattr, char *buf)
383 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
384 struct adm9240_data *data = adm9240_update_device(dev);
386 if (IS_ERR(data))
387 return PTR_ERR(data);
389 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
390 attr->index));
393 static ssize_t in_min_store(struct device *dev,
394 struct device_attribute *devattr, const char *buf,
395 size_t count)
397 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
398 struct adm9240_data *data = dev_get_drvdata(dev);
399 unsigned long val;
400 int err;
402 err = kstrtoul(buf, 10, &val);
403 if (err)
404 return err;
406 mutex_lock(&data->update_lock);
407 data->in_min[attr->index] = IN_TO_REG(val, attr->index);
408 err = regmap_write(data->regmap, ADM9240_REG_IN_MIN(attr->index),
409 data->in_min[attr->index]);
410 mutex_unlock(&data->update_lock);
411 return err < 0 ? err : count;
414 static ssize_t in_max_store(struct device *dev,
415 struct device_attribute *devattr, const char *buf,
416 size_t count)
418 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
419 struct adm9240_data *data = dev_get_drvdata(dev);
420 unsigned long val;
421 int err;
423 err = kstrtoul(buf, 10, &val);
424 if (err)
425 return err;
427 mutex_lock(&data->update_lock);
428 data->in_max[attr->index] = IN_TO_REG(val, attr->index);
429 err = regmap_write(data->regmap, ADM9240_REG_IN_MAX(attr->index),
430 data->in_max[attr->index]);
431 mutex_unlock(&data->update_lock);
432 return err < 0 ? err : count;
435 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
436 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
437 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
438 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
439 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
440 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
441 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
442 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
443 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
444 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
445 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
446 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
447 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
448 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
449 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
450 static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
451 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
452 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
454 /* fans */
455 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
456 char *buf)
458 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
459 struct adm9240_data *data = adm9240_update_device(dev);
461 if (IS_ERR(data))
462 return PTR_ERR(data);
464 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
465 1 << data->fan_div[attr->index]));
468 static ssize_t fan_min_show(struct device *dev,
469 struct device_attribute *devattr, char *buf)
471 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
472 struct adm9240_data *data = adm9240_update_device(dev);
474 if (IS_ERR(data))
475 return PTR_ERR(data);
477 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
478 1 << data->fan_div[attr->index]));
481 static ssize_t fan_div_show(struct device *dev,
482 struct device_attribute *devattr, char *buf)
484 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
485 struct adm9240_data *data = adm9240_update_device(dev);
487 if (IS_ERR(data))
488 return PTR_ERR(data);
490 return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
494 * set fan speed low limit:
496 * - value is zero: disable fan speed low limit alarm
498 * - value is below fan speed measurement range: enable fan speed low
499 * limit alarm to be asserted while fan speed too slow to measure
501 * - otherwise: select fan clock divider to suit fan speed low limit,
502 * measurement code may adjust registers to ensure fan speed reading
504 static ssize_t fan_min_store(struct device *dev,
505 struct device_attribute *devattr,
506 const char *buf, size_t count)
508 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
509 struct adm9240_data *data = dev_get_drvdata(dev);
510 struct i2c_client *client = data->client;
511 int nr = attr->index;
512 u8 new_div;
513 unsigned long val;
514 int err;
516 err = kstrtoul(buf, 10, &val);
517 if (err)
518 return err;
520 mutex_lock(&data->update_lock);
522 if (!val) {
523 data->fan_min[nr] = 255;
524 new_div = data->fan_div[nr];
526 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
527 nr + 1);
529 } else if (val < 1350000 / (8 * 254)) {
530 new_div = 3;
531 data->fan_min[nr] = 254;
533 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
534 nr + 1, FAN_FROM_REG(254, 1 << new_div));
536 } else {
537 unsigned int new_min = 1350000 / val;
539 new_div = 0;
540 while (new_min > 192 && new_div < 3) {
541 new_div++;
542 new_min /= 2;
544 if (!new_min) /* keep > 0 */
545 new_min++;
547 data->fan_min[nr] = new_min;
549 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
550 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
553 if (new_div != data->fan_div[nr]) {
554 data->fan_div[nr] = new_div;
555 adm9240_write_fan_div(data, nr, new_div);
557 err = regmap_write(data->regmap, ADM9240_REG_FAN_MIN(nr),
558 data->fan_min[nr]);
560 mutex_unlock(&data->update_lock);
561 return err < 0 ? err : count;
564 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
565 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
566 static SENSOR_DEVICE_ATTR_RO(fan1_div, fan_div, 0);
567 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
568 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
569 static SENSOR_DEVICE_ATTR_RO(fan2_div, fan_div, 1);
571 /* alarms */
572 static ssize_t alarms_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
575 struct adm9240_data *data = adm9240_update_device(dev);
577 if (IS_ERR(data))
578 return PTR_ERR(data);
580 return sprintf(buf, "%u\n", data->alarms);
582 static DEVICE_ATTR_RO(alarms);
584 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
585 char *buf)
587 int bitnr = to_sensor_dev_attr(attr)->index;
588 struct adm9240_data *data = adm9240_update_device(dev);
590 if (IS_ERR(data))
591 return PTR_ERR(data);
593 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
595 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
596 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
597 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
598 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
599 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
600 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
601 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
602 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
603 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
605 /* vid */
606 static ssize_t cpu0_vid_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 struct adm9240_data *data = adm9240_update_device(dev);
611 if (IS_ERR(data))
612 return PTR_ERR(data);
614 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
616 static DEVICE_ATTR_RO(cpu0_vid);
618 /* analog output */
619 static ssize_t aout_output_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
622 struct adm9240_data *data = adm9240_update_device(dev);
624 if (IS_ERR(data))
625 return PTR_ERR(data);
627 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
630 static ssize_t aout_output_store(struct device *dev,
631 struct device_attribute *attr,
632 const char *buf, size_t count)
634 struct adm9240_data *data = dev_get_drvdata(dev);
635 long val;
636 int err;
638 err = kstrtol(buf, 10, &val);
639 if (err)
640 return err;
642 mutex_lock(&data->update_lock);
643 data->aout = AOUT_TO_REG(val);
644 err = regmap_write(data->regmap, ADM9240_REG_ANALOG_OUT, data->aout);
645 mutex_unlock(&data->update_lock);
646 return err < 0 ? err : count;
648 static DEVICE_ATTR_RW(aout_output);
650 static ssize_t alarm_store(struct device *dev, struct device_attribute *attr,
651 const char *buf, size_t count)
653 struct adm9240_data *data = dev_get_drvdata(dev);
654 unsigned long val;
655 int err;
657 if (kstrtoul(buf, 10, &val) || val != 0)
658 return -EINVAL;
660 mutex_lock(&data->update_lock);
661 err = regmap_write(data->regmap, ADM9240_REG_CHASSIS_CLEAR, 0x80);
662 data->valid = 0; /* Force cache refresh */
663 mutex_unlock(&data->update_lock);
664 if (err < 0)
665 return err;
666 dev_dbg(&data->client->dev, "chassis intrusion latch cleared\n");
668 return count;
670 static SENSOR_DEVICE_ATTR_RW(intrusion0_alarm, alarm, 12);
672 static struct attribute *adm9240_attrs[] = {
673 &sensor_dev_attr_in0_input.dev_attr.attr,
674 &sensor_dev_attr_in0_min.dev_attr.attr,
675 &sensor_dev_attr_in0_max.dev_attr.attr,
676 &sensor_dev_attr_in0_alarm.dev_attr.attr,
677 &sensor_dev_attr_in1_input.dev_attr.attr,
678 &sensor_dev_attr_in1_min.dev_attr.attr,
679 &sensor_dev_attr_in1_max.dev_attr.attr,
680 &sensor_dev_attr_in1_alarm.dev_attr.attr,
681 &sensor_dev_attr_in2_input.dev_attr.attr,
682 &sensor_dev_attr_in2_min.dev_attr.attr,
683 &sensor_dev_attr_in2_max.dev_attr.attr,
684 &sensor_dev_attr_in2_alarm.dev_attr.attr,
685 &sensor_dev_attr_in3_input.dev_attr.attr,
686 &sensor_dev_attr_in3_min.dev_attr.attr,
687 &sensor_dev_attr_in3_max.dev_attr.attr,
688 &sensor_dev_attr_in3_alarm.dev_attr.attr,
689 &sensor_dev_attr_in4_input.dev_attr.attr,
690 &sensor_dev_attr_in4_min.dev_attr.attr,
691 &sensor_dev_attr_in4_max.dev_attr.attr,
692 &sensor_dev_attr_in4_alarm.dev_attr.attr,
693 &sensor_dev_attr_in5_input.dev_attr.attr,
694 &sensor_dev_attr_in5_min.dev_attr.attr,
695 &sensor_dev_attr_in5_max.dev_attr.attr,
696 &sensor_dev_attr_in5_alarm.dev_attr.attr,
697 &dev_attr_temp1_input.attr,
698 &sensor_dev_attr_temp1_max.dev_attr.attr,
699 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
700 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
701 &sensor_dev_attr_fan1_input.dev_attr.attr,
702 &sensor_dev_attr_fan1_div.dev_attr.attr,
703 &sensor_dev_attr_fan1_min.dev_attr.attr,
704 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
705 &sensor_dev_attr_fan2_input.dev_attr.attr,
706 &sensor_dev_attr_fan2_div.dev_attr.attr,
707 &sensor_dev_attr_fan2_min.dev_attr.attr,
708 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
709 &dev_attr_alarms.attr,
710 &dev_attr_aout_output.attr,
711 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
712 &dev_attr_cpu0_vid.attr,
713 NULL
716 ATTRIBUTE_GROUPS(adm9240);
718 /*** sensor chip detect and driver install ***/
720 /* Return 0 if detection is successful, -ENODEV otherwise */
721 static int adm9240_detect(struct i2c_client *new_client,
722 struct i2c_board_info *info)
724 struct i2c_adapter *adapter = new_client->adapter;
725 const char *name = "";
726 int address = new_client->addr;
727 u8 man_id, die_rev;
729 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
730 return -ENODEV;
732 /* verify chip: reg address should match i2c address */
733 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
734 != address) {
735 dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n",
736 address);
737 return -ENODEV;
740 /* check known chip manufacturer */
741 man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
742 if (man_id == 0x23) {
743 name = "adm9240";
744 } else if (man_id == 0xda) {
745 name = "ds1780";
746 } else if (man_id == 0x01) {
747 name = "lm81";
748 } else {
749 dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
750 man_id);
751 return -ENODEV;
754 /* successful detect, print chip info */
755 die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
756 dev_info(&adapter->dev, "found %s revision %u\n",
757 man_id == 0x23 ? "ADM9240" :
758 man_id == 0xda ? "DS1780" : "LM81", die_rev);
760 strlcpy(info->type, name, I2C_NAME_SIZE);
762 return 0;
765 static int adm9240_init_client(struct i2c_client *client, struct adm9240_data *data)
767 u8 conf, mode;
768 int err;
770 err = regmap_raw_read(data->regmap, ADM9240_REG_CONFIG, &conf, 1);
771 if (err < 0)
772 return err;
773 err = regmap_raw_read(data->regmap, ADM9240_REG_TEMP_CONF, &mode, 1);
774 if (err < 0)
775 return err;
776 mode &= 3;
778 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
780 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
781 data->vrm % 10);
783 if (conf & 1) { /* measurement cycle running: report state */
785 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
786 conf, mode);
788 } else { /* cold start: open limits before starting chip */
789 int i;
791 for (i = 0; i < 6; i++) {
792 err = regmap_write(data->regmap,
793 ADM9240_REG_IN_MIN(i), 0);
794 if (err < 0)
795 return err;
796 err = regmap_write(data->regmap,
797 ADM9240_REG_IN_MAX(i), 255);
798 if (err < 0)
799 return err;
801 for (i = 0; i < 2; i++) {
802 err = regmap_write(data->regmap,
803 ADM9240_REG_FAN_MIN(i), 255);
804 if (err < 0)
805 return err;
807 for (i = 0; i < 2; i++) {
808 err = regmap_write(data->regmap,
809 ADM9240_REG_TEMP_MAX(i), 127);
810 if (err < 0)
811 return err;
814 /* start measurement cycle */
815 err = regmap_write(data->regmap, ADM9240_REG_CONFIG, 1);
816 if (err < 0)
817 return err;
819 dev_info(&client->dev,
820 "cold start: config was 0x%02x mode %u\n", conf, mode);
823 return 0;
826 static const struct regmap_config adm9240_regmap_config = {
827 .reg_bits = 8,
828 .val_bits = 8,
829 .use_single_read = true,
830 .use_single_write = true,
833 static int adm9240_probe(struct i2c_client *new_client)
835 struct device *dev = &new_client->dev;
836 struct device *hwmon_dev;
837 struct adm9240_data *data;
838 int err;
840 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
841 if (!data)
842 return -ENOMEM;
844 data->client = new_client;
845 mutex_init(&data->update_lock);
846 data->regmap = devm_regmap_init_i2c(new_client, &adm9240_regmap_config);
847 if (IS_ERR(data->regmap))
848 return PTR_ERR(data->regmap);
850 err = adm9240_init_client(new_client, data);
851 if (err < 0)
852 return err;
854 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
855 new_client->name,
856 data,
857 adm9240_groups);
858 return PTR_ERR_OR_ZERO(hwmon_dev);
861 static const struct i2c_device_id adm9240_id[] = {
862 { "adm9240", adm9240 },
863 { "ds1780", ds1780 },
864 { "lm81", lm81 },
867 MODULE_DEVICE_TABLE(i2c, adm9240_id);
869 static struct i2c_driver adm9240_driver = {
870 .class = I2C_CLASS_HWMON,
871 .driver = {
872 .name = "adm9240",
874 .probe_new = adm9240_probe,
875 .id_table = adm9240_id,
876 .detect = adm9240_detect,
877 .address_list = normal_i2c,
880 module_i2c_driver(adm9240_driver);
882 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
883 "Grant Coady <gcoady.lk@gmail.com> and others");
884 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
885 MODULE_LICENSE("GPL");