2 * Driver for TI ADC128D818 System Monitor with Temperature Sensor
4 * Copyright (c) 2014 Guenter Roeck
7 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
8 * and Philip Edelbrock <phil@netroedge.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/mutex.h>
30 #include <linux/bitops.h>
34 * The chip also supports addresses 0x35..0x37. Don't scan those addresses
35 * since they are also used by some EEPROMs, which may result in false
38 static const unsigned short normal_i2c
[] = {
39 0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
};
42 #define ADC128_REG_IN_MAX(nr) (0x2a + (nr) * 2)
43 #define ADC128_REG_IN_MIN(nr) (0x2b + (nr) * 2)
44 #define ADC128_REG_IN(nr) (0x20 + (nr))
46 #define ADC128_REG_TEMP 0x27
47 #define ADC128_REG_TEMP_MAX 0x38
48 #define ADC128_REG_TEMP_HYST 0x39
50 #define ADC128_REG_CONFIG 0x00
51 #define ADC128_REG_ALARM 0x01
52 #define ADC128_REG_MASK 0x03
53 #define ADC128_REG_CONV_RATE 0x07
54 #define ADC128_REG_ONESHOT 0x09
55 #define ADC128_REG_SHUTDOWN 0x0a
56 #define ADC128_REG_CONFIG_ADV 0x0b
57 #define ADC128_REG_BUSY_STATUS 0x0c
59 #define ADC128_REG_MAN_ID 0x3e
60 #define ADC128_REG_DEV_ID 0x3f
62 /* No. of voltage entries in adc128_attrs */
63 #define ADC128_ATTR_NUM_VOLT (8 * 4)
65 /* Voltage inputs visible per operation mode */
66 static const u8 num_inputs
[] = { 7, 8, 4, 6 };
69 struct i2c_client
*client
;
70 struct regulator
*regulator
;
71 int vref
; /* Reference voltage in mV */
72 struct mutex update_lock
;
73 u8 mode
; /* Operation mode */
74 bool valid
; /* true if following fields are valid */
75 unsigned long last_updated
; /* In jiffies */
77 u16 in
[3][8]; /* Register value, normalized to 12 bit
82 s16 temp
[3]; /* Register value, normalized to 9 bit
83 * 0: sensor 1: limit 2: hyst
85 u8 alarms
; /* alarm register value */
88 static struct adc128_data
*adc128_update_device(struct device
*dev
)
90 struct adc128_data
*data
= dev_get_drvdata(dev
);
91 struct i2c_client
*client
= data
->client
;
92 struct adc128_data
*ret
= data
;
95 mutex_lock(&data
->update_lock
);
97 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
98 for (i
= 0; i
< num_inputs
[data
->mode
]; i
++) {
99 rv
= i2c_smbus_read_word_swapped(client
,
103 data
->in
[0][i
] = rv
>> 4;
105 rv
= i2c_smbus_read_byte_data(client
,
106 ADC128_REG_IN_MIN(i
));
109 data
->in
[1][i
] = rv
<< 4;
111 rv
= i2c_smbus_read_byte_data(client
,
112 ADC128_REG_IN_MAX(i
));
115 data
->in
[2][i
] = rv
<< 4;
118 if (data
->mode
!= 1) {
119 rv
= i2c_smbus_read_word_swapped(client
,
123 data
->temp
[0] = rv
>> 7;
125 rv
= i2c_smbus_read_byte_data(client
,
126 ADC128_REG_TEMP_MAX
);
129 data
->temp
[1] = rv
<< 1;
131 rv
= i2c_smbus_read_byte_data(client
,
132 ADC128_REG_TEMP_HYST
);
135 data
->temp
[2] = rv
<< 1;
138 rv
= i2c_smbus_read_byte_data(client
, ADC128_REG_ALARM
);
143 data
->last_updated
= jiffies
;
152 mutex_unlock(&data
->update_lock
);
156 static ssize_t
adc128_in_show(struct device
*dev
,
157 struct device_attribute
*attr
, char *buf
)
159 struct adc128_data
*data
= adc128_update_device(dev
);
160 int index
= to_sensor_dev_attr_2(attr
)->index
;
161 int nr
= to_sensor_dev_attr_2(attr
)->nr
;
165 return PTR_ERR(data
);
167 val
= DIV_ROUND_CLOSEST(data
->in
[index
][nr
] * data
->vref
, 4095);
168 return sprintf(buf
, "%d\n", val
);
171 static ssize_t
adc128_in_store(struct device
*dev
,
172 struct device_attribute
*attr
, const char *buf
,
175 struct adc128_data
*data
= dev_get_drvdata(dev
);
176 int index
= to_sensor_dev_attr_2(attr
)->index
;
177 int nr
= to_sensor_dev_attr_2(attr
)->nr
;
182 err
= kstrtol(buf
, 10, &val
);
186 mutex_lock(&data
->update_lock
);
187 /* 10 mV LSB on limit registers */
188 regval
= clamp_val(DIV_ROUND_CLOSEST(val
, 10), 0, 255);
189 data
->in
[index
][nr
] = regval
<< 4;
190 reg
= index
== 1 ? ADC128_REG_IN_MIN(nr
) : ADC128_REG_IN_MAX(nr
);
191 i2c_smbus_write_byte_data(data
->client
, reg
, regval
);
192 mutex_unlock(&data
->update_lock
);
197 static ssize_t
adc128_temp_show(struct device
*dev
,
198 struct device_attribute
*attr
, char *buf
)
200 struct adc128_data
*data
= adc128_update_device(dev
);
201 int index
= to_sensor_dev_attr(attr
)->index
;
205 return PTR_ERR(data
);
207 temp
= sign_extend32(data
->temp
[index
], 8);
208 return sprintf(buf
, "%d\n", temp
* 500);/* 0.5 degrees C resolution */
211 static ssize_t
adc128_temp_store(struct device
*dev
,
212 struct device_attribute
*attr
,
213 const char *buf
, size_t count
)
215 struct adc128_data
*data
= dev_get_drvdata(dev
);
216 int index
= to_sensor_dev_attr(attr
)->index
;
221 err
= kstrtol(buf
, 10, &val
);
225 mutex_lock(&data
->update_lock
);
226 regval
= clamp_val(DIV_ROUND_CLOSEST(val
, 1000), -128, 127);
227 data
->temp
[index
] = regval
<< 1;
228 i2c_smbus_write_byte_data(data
->client
,
229 index
== 1 ? ADC128_REG_TEMP_MAX
230 : ADC128_REG_TEMP_HYST
,
232 mutex_unlock(&data
->update_lock
);
237 static ssize_t
adc128_alarm_show(struct device
*dev
,
238 struct device_attribute
*attr
, char *buf
)
240 struct adc128_data
*data
= adc128_update_device(dev
);
241 int mask
= 1 << to_sensor_dev_attr(attr
)->index
;
245 return PTR_ERR(data
);
248 * Clear an alarm after reporting it to user space. If it is still
249 * active, the next update sequence will set the alarm bit again.
251 alarms
= data
->alarms
;
252 data
->alarms
&= ~mask
;
254 return sprintf(buf
, "%u\n", !!(alarms
& mask
));
257 static umode_t
adc128_is_visible(struct kobject
*kobj
,
258 struct attribute
*attr
, int index
)
260 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
261 struct adc128_data
*data
= dev_get_drvdata(dev
);
263 if (index
< ADC128_ATTR_NUM_VOLT
) {
264 /* Voltage, visible according to num_inputs[] */
265 if (index
>= num_inputs
[data
->mode
] * 4)
268 /* Temperature, visible if not in mode 1 */
276 static SENSOR_DEVICE_ATTR_2_RO(in0_input
, adc128_in
, 0, 0);
277 static SENSOR_DEVICE_ATTR_2_RW(in0_min
, adc128_in
, 0, 1);
278 static SENSOR_DEVICE_ATTR_2_RW(in0_max
, adc128_in
, 0, 2);
280 static SENSOR_DEVICE_ATTR_2_RO(in1_input
, adc128_in
, 1, 0);
281 static SENSOR_DEVICE_ATTR_2_RW(in1_min
, adc128_in
, 1, 1);
282 static SENSOR_DEVICE_ATTR_2_RW(in1_max
, adc128_in
, 1, 2);
284 static SENSOR_DEVICE_ATTR_2_RO(in2_input
, adc128_in
, 2, 0);
285 static SENSOR_DEVICE_ATTR_2_RW(in2_min
, adc128_in
, 2, 1);
286 static SENSOR_DEVICE_ATTR_2_RW(in2_max
, adc128_in
, 2, 2);
288 static SENSOR_DEVICE_ATTR_2_RO(in3_input
, adc128_in
, 3, 0);
289 static SENSOR_DEVICE_ATTR_2_RW(in3_min
, adc128_in
, 3, 1);
290 static SENSOR_DEVICE_ATTR_2_RW(in3_max
, adc128_in
, 3, 2);
292 static SENSOR_DEVICE_ATTR_2_RO(in4_input
, adc128_in
, 4, 0);
293 static SENSOR_DEVICE_ATTR_2_RW(in4_min
, adc128_in
, 4, 1);
294 static SENSOR_DEVICE_ATTR_2_RW(in4_max
, adc128_in
, 4, 2);
296 static SENSOR_DEVICE_ATTR_2_RO(in5_input
, adc128_in
, 5, 0);
297 static SENSOR_DEVICE_ATTR_2_RW(in5_min
, adc128_in
, 5, 1);
298 static SENSOR_DEVICE_ATTR_2_RW(in5_max
, adc128_in
, 5, 2);
300 static SENSOR_DEVICE_ATTR_2_RO(in6_input
, adc128_in
, 6, 0);
301 static SENSOR_DEVICE_ATTR_2_RW(in6_min
, adc128_in
, 6, 1);
302 static SENSOR_DEVICE_ATTR_2_RW(in6_max
, adc128_in
, 6, 2);
304 static SENSOR_DEVICE_ATTR_2_RO(in7_input
, adc128_in
, 7, 0);
305 static SENSOR_DEVICE_ATTR_2_RW(in7_min
, adc128_in
, 7, 1);
306 static SENSOR_DEVICE_ATTR_2_RW(in7_max
, adc128_in
, 7, 2);
308 static SENSOR_DEVICE_ATTR_RO(temp1_input
, adc128_temp
, 0);
309 static SENSOR_DEVICE_ATTR_RW(temp1_max
, adc128_temp
, 1);
310 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst
, adc128_temp
, 2);
312 static SENSOR_DEVICE_ATTR_RO(in0_alarm
, adc128_alarm
, 0);
313 static SENSOR_DEVICE_ATTR_RO(in1_alarm
, adc128_alarm
, 1);
314 static SENSOR_DEVICE_ATTR_RO(in2_alarm
, adc128_alarm
, 2);
315 static SENSOR_DEVICE_ATTR_RO(in3_alarm
, adc128_alarm
, 3);
316 static SENSOR_DEVICE_ATTR_RO(in4_alarm
, adc128_alarm
, 4);
317 static SENSOR_DEVICE_ATTR_RO(in5_alarm
, adc128_alarm
, 5);
318 static SENSOR_DEVICE_ATTR_RO(in6_alarm
, adc128_alarm
, 6);
319 static SENSOR_DEVICE_ATTR_RO(in7_alarm
, adc128_alarm
, 7);
320 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm
, adc128_alarm
, 7);
322 static struct attribute
*adc128_attrs
[] = {
323 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
324 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
325 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
326 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
327 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
328 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
329 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
330 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
331 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
332 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
333 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
334 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
335 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
336 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
337 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
338 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
339 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
340 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
341 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
342 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
343 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
344 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
345 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
346 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
347 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
348 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
349 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
350 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
351 &sensor_dev_attr_in7_alarm
.dev_attr
.attr
,
352 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
353 &sensor_dev_attr_in7_max
.dev_attr
.attr
,
354 &sensor_dev_attr_in7_min
.dev_attr
.attr
,
355 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
356 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
357 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
358 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
362 static const struct attribute_group adc128_group
= {
363 .attrs
= adc128_attrs
,
364 .is_visible
= adc128_is_visible
,
366 __ATTRIBUTE_GROUPS(adc128
);
368 static int adc128_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
372 if (!i2c_check_functionality(client
->adapter
,
373 I2C_FUNC_SMBUS_BYTE_DATA
|
374 I2C_FUNC_SMBUS_WORD_DATA
))
377 man_id
= i2c_smbus_read_byte_data(client
, ADC128_REG_MAN_ID
);
378 dev_id
= i2c_smbus_read_byte_data(client
, ADC128_REG_DEV_ID
);
379 if (man_id
!= 0x01 || dev_id
!= 0x09)
382 /* Check unused bits for confirmation */
383 if (i2c_smbus_read_byte_data(client
, ADC128_REG_CONFIG
) & 0xf4)
385 if (i2c_smbus_read_byte_data(client
, ADC128_REG_CONV_RATE
) & 0xfe)
387 if (i2c_smbus_read_byte_data(client
, ADC128_REG_ONESHOT
) & 0xfe)
389 if (i2c_smbus_read_byte_data(client
, ADC128_REG_SHUTDOWN
) & 0xfe)
391 if (i2c_smbus_read_byte_data(client
, ADC128_REG_CONFIG_ADV
) & 0xf8)
393 if (i2c_smbus_read_byte_data(client
, ADC128_REG_BUSY_STATUS
) & 0xfc)
396 strlcpy(info
->type
, "adc128d818", I2C_NAME_SIZE
);
401 static int adc128_init_client(struct adc128_data
*data
)
403 struct i2c_client
*client
= data
->client
;
407 * Reset chip to defaults.
408 * This makes most other initializations unnecessary.
410 err
= i2c_smbus_write_byte_data(client
, ADC128_REG_CONFIG
, 0x80);
414 /* Set operation mode, if non-default */
415 if (data
->mode
!= 0) {
416 err
= i2c_smbus_write_byte_data(client
,
417 ADC128_REG_CONFIG_ADV
,
423 /* Start monitoring */
424 err
= i2c_smbus_write_byte_data(client
, ADC128_REG_CONFIG
, 0x01);
428 /* If external vref is selected, configure the chip to use it */
429 if (data
->regulator
) {
430 err
= i2c_smbus_write_byte_data(client
,
431 ADC128_REG_CONFIG_ADV
, 0x01);
439 static int adc128_probe(struct i2c_client
*client
,
440 const struct i2c_device_id
*id
)
442 struct device
*dev
= &client
->dev
;
443 struct regulator
*regulator
;
444 struct device
*hwmon_dev
;
445 struct adc128_data
*data
;
448 data
= devm_kzalloc(dev
, sizeof(struct adc128_data
), GFP_KERNEL
);
452 /* vref is optional. If specified, is used as chip reference voltage */
453 regulator
= devm_regulator_get_optional(dev
, "vref");
454 if (!IS_ERR(regulator
)) {
455 data
->regulator
= regulator
;
456 err
= regulator_enable(regulator
);
459 vref
= regulator_get_voltage(regulator
);
464 data
->vref
= DIV_ROUND_CLOSEST(vref
, 1000);
466 data
->vref
= 2560; /* 2.56V, in mV */
469 /* Operation mode is optional. If unspecified, keep current mode */
470 if (of_property_read_u8(dev
->of_node
, "ti,mode", &data
->mode
) == 0) {
471 if (data
->mode
> 3) {
472 dev_err(dev
, "invalid operation mode %d\n",
478 err
= i2c_smbus_read_byte_data(client
, ADC128_REG_CONFIG_ADV
);
481 data
->mode
= (err
>> 1) & ADC128_REG_MASK
;
484 data
->client
= client
;
485 i2c_set_clientdata(client
, data
);
486 mutex_init(&data
->update_lock
);
488 /* Initialize the chip */
489 err
= adc128_init_client(data
);
493 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
494 data
, adc128_groups
);
495 if (IS_ERR(hwmon_dev
)) {
496 err
= PTR_ERR(hwmon_dev
);
504 regulator_disable(data
->regulator
);
508 static int adc128_remove(struct i2c_client
*client
)
510 struct adc128_data
*data
= i2c_get_clientdata(client
);
513 regulator_disable(data
->regulator
);
518 static const struct i2c_device_id adc128_id
[] = {
522 MODULE_DEVICE_TABLE(i2c
, adc128_id
);
524 static const struct of_device_id adc128_of_match
[] = {
525 { .compatible
= "ti,adc128d818" },
528 MODULE_DEVICE_TABLE(of
, adc128_of_match
);
530 static struct i2c_driver adc128_driver
= {
531 .class = I2C_CLASS_HWMON
,
533 .name
= "adc128d818",
534 .of_match_table
= of_match_ptr(adc128_of_match
),
536 .probe
= adc128_probe
,
537 .remove
= adc128_remove
,
538 .id_table
= adc128_id
,
539 .detect
= adc128_detect
,
540 .address_list
= normal_i2c
,
543 module_i2c_driver(adc128_driver
);
545 MODULE_AUTHOR("Guenter Roeck");
546 MODULE_DESCRIPTION("Driver for ADC128D818");
547 MODULE_LICENSE("GPL");