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>
33 * The chip also supports addresses 0x35..0x37. Don't scan those addresses
34 * since they are also used by some EEPROMs, which may result in false
37 static const unsigned short normal_i2c
[] = {
38 0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
};
41 #define ADC128_REG_IN_MAX(nr) (0x2a + (nr) * 2)
42 #define ADC128_REG_IN_MIN(nr) (0x2b + (nr) * 2)
43 #define ADC128_REG_IN(nr) (0x20 + (nr))
45 #define ADC128_REG_TEMP 0x27
46 #define ADC128_REG_TEMP_MAX 0x38
47 #define ADC128_REG_TEMP_HYST 0x39
49 #define ADC128_REG_CONFIG 0x00
50 #define ADC128_REG_ALARM 0x01
51 #define ADC128_REG_MASK 0x03
52 #define ADC128_REG_CONV_RATE 0x07
53 #define ADC128_REG_ONESHOT 0x09
54 #define ADC128_REG_SHUTDOWN 0x0a
55 #define ADC128_REG_CONFIG_ADV 0x0b
56 #define ADC128_REG_BUSY_STATUS 0x0c
58 #define ADC128_REG_MAN_ID 0x3e
59 #define ADC128_REG_DEV_ID 0x3f
62 struct i2c_client
*client
;
63 struct regulator
*regulator
;
64 int vref
; /* Reference voltage in mV */
65 struct mutex update_lock
;
66 bool valid
; /* true if following fields are valid */
67 unsigned long last_updated
; /* In jiffies */
69 u16 in
[3][7]; /* Register value, normalized to 12 bit
74 s16 temp
[3]; /* Register value, normalized to 9 bit
75 * 0: sensor 1: limit 2: hyst
77 u8 alarms
; /* alarm register value */
80 static struct adc128_data
*adc128_update_device(struct device
*dev
)
82 struct adc128_data
*data
= dev_get_drvdata(dev
);
83 struct i2c_client
*client
= data
->client
;
84 struct adc128_data
*ret
= data
;
87 mutex_lock(&data
->update_lock
);
89 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
90 for (i
= 0; i
< 7; i
++) {
91 rv
= i2c_smbus_read_word_swapped(client
,
95 data
->in
[0][i
] = rv
>> 4;
97 rv
= i2c_smbus_read_byte_data(client
,
98 ADC128_REG_IN_MIN(i
));
101 data
->in
[1][i
] = rv
<< 4;
103 rv
= i2c_smbus_read_byte_data(client
,
104 ADC128_REG_IN_MAX(i
));
107 data
->in
[2][i
] = rv
<< 4;
110 rv
= i2c_smbus_read_word_swapped(client
, ADC128_REG_TEMP
);
113 data
->temp
[0] = rv
>> 7;
115 rv
= i2c_smbus_read_byte_data(client
, ADC128_REG_TEMP_MAX
);
118 data
->temp
[1] = rv
<< 1;
120 rv
= i2c_smbus_read_byte_data(client
, ADC128_REG_TEMP_HYST
);
123 data
->temp
[2] = rv
<< 1;
125 rv
= i2c_smbus_read_byte_data(client
, ADC128_REG_ALARM
);
130 data
->last_updated
= jiffies
;
139 mutex_unlock(&data
->update_lock
);
143 static ssize_t
adc128_show_in(struct device
*dev
, struct device_attribute
*attr
,
146 struct adc128_data
*data
= adc128_update_device(dev
);
147 int index
= to_sensor_dev_attr_2(attr
)->index
;
148 int nr
= to_sensor_dev_attr_2(attr
)->nr
;
152 return PTR_ERR(data
);
154 val
= DIV_ROUND_CLOSEST(data
->in
[index
][nr
] * data
->vref
, 4095);
155 return sprintf(buf
, "%d\n", val
);
158 static ssize_t
adc128_set_in(struct device
*dev
, struct device_attribute
*attr
,
159 const char *buf
, size_t count
)
161 struct adc128_data
*data
= dev_get_drvdata(dev
);
162 int index
= to_sensor_dev_attr_2(attr
)->index
;
163 int nr
= to_sensor_dev_attr_2(attr
)->nr
;
168 err
= kstrtol(buf
, 10, &val
);
172 mutex_lock(&data
->update_lock
);
173 /* 10 mV LSB on limit registers */
174 regval
= clamp_val(DIV_ROUND_CLOSEST(val
, 10), 0, 255);
175 data
->in
[index
][nr
] = regval
<< 4;
176 reg
= index
== 1 ? ADC128_REG_IN_MIN(nr
) : ADC128_REG_IN_MAX(nr
);
177 i2c_smbus_write_byte_data(data
->client
, reg
, regval
);
178 mutex_unlock(&data
->update_lock
);
183 static ssize_t
adc128_show_temp(struct device
*dev
,
184 struct device_attribute
*attr
, char *buf
)
186 struct adc128_data
*data
= adc128_update_device(dev
);
187 int index
= to_sensor_dev_attr(attr
)->index
;
191 return PTR_ERR(data
);
193 temp
= sign_extend32(data
->temp
[index
], 8);
194 return sprintf(buf
, "%d\n", temp
* 500);/* 0.5 degrees C resolution */
197 static ssize_t
adc128_set_temp(struct device
*dev
,
198 struct device_attribute
*attr
,
199 const char *buf
, size_t count
)
201 struct adc128_data
*data
= dev_get_drvdata(dev
);
202 int index
= to_sensor_dev_attr(attr
)->index
;
207 err
= kstrtol(buf
, 10, &val
);
211 mutex_lock(&data
->update_lock
);
212 regval
= clamp_val(DIV_ROUND_CLOSEST(val
, 1000), -128, 127);
213 data
->temp
[index
] = regval
<< 1;
214 i2c_smbus_write_byte_data(data
->client
,
215 index
== 1 ? ADC128_REG_TEMP_MAX
216 : ADC128_REG_TEMP_HYST
,
218 mutex_unlock(&data
->update_lock
);
223 static ssize_t
adc128_show_alarm(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 struct adc128_data
*data
= adc128_update_device(dev
);
227 int mask
= 1 << to_sensor_dev_attr(attr
)->index
;
231 return PTR_ERR(data
);
234 * Clear an alarm after reporting it to user space. If it is still
235 * active, the next update sequence will set the alarm bit again.
237 alarms
= data
->alarms
;
238 data
->alarms
&= ~mask
;
240 return sprintf(buf
, "%u\n", !!(alarms
& mask
));
243 static SENSOR_DEVICE_ATTR_2(in0_input
, S_IRUGO
,
244 adc128_show_in
, NULL
, 0, 0);
245 static SENSOR_DEVICE_ATTR_2(in0_min
, S_IWUSR
| S_IRUGO
,
246 adc128_show_in
, adc128_set_in
, 0, 1);
247 static SENSOR_DEVICE_ATTR_2(in0_max
, S_IWUSR
| S_IRUGO
,
248 adc128_show_in
, adc128_set_in
, 0, 2);
250 static SENSOR_DEVICE_ATTR_2(in1_input
, S_IRUGO
,
251 adc128_show_in
, NULL
, 1, 0);
252 static SENSOR_DEVICE_ATTR_2(in1_min
, S_IWUSR
| S_IRUGO
,
253 adc128_show_in
, adc128_set_in
, 1, 1);
254 static SENSOR_DEVICE_ATTR_2(in1_max
, S_IWUSR
| S_IRUGO
,
255 adc128_show_in
, adc128_set_in
, 1, 2);
257 static SENSOR_DEVICE_ATTR_2(in2_input
, S_IRUGO
,
258 adc128_show_in
, NULL
, 2, 0);
259 static SENSOR_DEVICE_ATTR_2(in2_min
, S_IWUSR
| S_IRUGO
,
260 adc128_show_in
, adc128_set_in
, 2, 1);
261 static SENSOR_DEVICE_ATTR_2(in2_max
, S_IWUSR
| S_IRUGO
,
262 adc128_show_in
, adc128_set_in
, 2, 2);
264 static SENSOR_DEVICE_ATTR_2(in3_input
, S_IRUGO
,
265 adc128_show_in
, NULL
, 3, 0);
266 static SENSOR_DEVICE_ATTR_2(in3_min
, S_IWUSR
| S_IRUGO
,
267 adc128_show_in
, adc128_set_in
, 3, 1);
268 static SENSOR_DEVICE_ATTR_2(in3_max
, S_IWUSR
| S_IRUGO
,
269 adc128_show_in
, adc128_set_in
, 3, 2);
271 static SENSOR_DEVICE_ATTR_2(in4_input
, S_IRUGO
,
272 adc128_show_in
, NULL
, 4, 0);
273 static SENSOR_DEVICE_ATTR_2(in4_min
, S_IWUSR
| S_IRUGO
,
274 adc128_show_in
, adc128_set_in
, 4, 1);
275 static SENSOR_DEVICE_ATTR_2(in4_max
, S_IWUSR
| S_IRUGO
,
276 adc128_show_in
, adc128_set_in
, 4, 2);
278 static SENSOR_DEVICE_ATTR_2(in5_input
, S_IRUGO
,
279 adc128_show_in
, NULL
, 5, 0);
280 static SENSOR_DEVICE_ATTR_2(in5_min
, S_IWUSR
| S_IRUGO
,
281 adc128_show_in
, adc128_set_in
, 5, 1);
282 static SENSOR_DEVICE_ATTR_2(in5_max
, S_IWUSR
| S_IRUGO
,
283 adc128_show_in
, adc128_set_in
, 5, 2);
285 static SENSOR_DEVICE_ATTR_2(in6_input
, S_IRUGO
,
286 adc128_show_in
, NULL
, 6, 0);
287 static SENSOR_DEVICE_ATTR_2(in6_min
, S_IWUSR
| S_IRUGO
,
288 adc128_show_in
, adc128_set_in
, 6, 1);
289 static SENSOR_DEVICE_ATTR_2(in6_max
, S_IWUSR
| S_IRUGO
,
290 adc128_show_in
, adc128_set_in
, 6, 2);
292 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, adc128_show_temp
, NULL
, 0);
293 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
,
294 adc128_show_temp
, adc128_set_temp
, 1);
295 static SENSOR_DEVICE_ATTR(temp1_max_hyst
, S_IWUSR
| S_IRUGO
,
296 adc128_show_temp
, adc128_set_temp
, 2);
298 static SENSOR_DEVICE_ATTR(in0_alarm
, S_IRUGO
, adc128_show_alarm
, NULL
, 0);
299 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, adc128_show_alarm
, NULL
, 1);
300 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, adc128_show_alarm
, NULL
, 2);
301 static SENSOR_DEVICE_ATTR(in3_alarm
, S_IRUGO
, adc128_show_alarm
, NULL
, 3);
302 static SENSOR_DEVICE_ATTR(in4_alarm
, S_IRUGO
, adc128_show_alarm
, NULL
, 4);
303 static SENSOR_DEVICE_ATTR(in5_alarm
, S_IRUGO
, adc128_show_alarm
, NULL
, 5);
304 static SENSOR_DEVICE_ATTR(in6_alarm
, S_IRUGO
, adc128_show_alarm
, NULL
, 6);
305 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, adc128_show_alarm
, NULL
, 7);
307 static struct attribute
*adc128_attrs
[] = {
308 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
309 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
310 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
311 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
312 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
313 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
314 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
315 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
316 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
317 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
318 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
319 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
320 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
321 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
322 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
323 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
324 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
325 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
326 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
327 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
328 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
329 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
330 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
331 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
332 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
333 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
334 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
335 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
336 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
337 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
338 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
339 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
342 ATTRIBUTE_GROUPS(adc128
);
344 static int adc128_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
348 if (!i2c_check_functionality(client
->adapter
,
349 I2C_FUNC_SMBUS_BYTE_DATA
|
350 I2C_FUNC_SMBUS_WORD_DATA
))
353 man_id
= i2c_smbus_read_byte_data(client
, ADC128_REG_MAN_ID
);
354 dev_id
= i2c_smbus_read_byte_data(client
, ADC128_REG_DEV_ID
);
355 if (man_id
!= 0x01 || dev_id
!= 0x09)
358 /* Check unused bits for confirmation */
359 if (i2c_smbus_read_byte_data(client
, ADC128_REG_CONFIG
) & 0xf4)
361 if (i2c_smbus_read_byte_data(client
, ADC128_REG_CONV_RATE
) & 0xfe)
363 if (i2c_smbus_read_byte_data(client
, ADC128_REG_ONESHOT
) & 0xfe)
365 if (i2c_smbus_read_byte_data(client
, ADC128_REG_SHUTDOWN
) & 0xfe)
367 if (i2c_smbus_read_byte_data(client
, ADC128_REG_CONFIG_ADV
) & 0xf8)
369 if (i2c_smbus_read_byte_data(client
, ADC128_REG_BUSY_STATUS
) & 0xfc)
372 strlcpy(info
->type
, "adc128d818", I2C_NAME_SIZE
);
377 static int adc128_init_client(struct adc128_data
*data
)
379 struct i2c_client
*client
= data
->client
;
383 * Reset chip to defaults.
384 * This makes most other initializations unnecessary.
386 err
= i2c_smbus_write_byte_data(client
, ADC128_REG_CONFIG
, 0x80);
390 /* Start monitoring */
391 err
= i2c_smbus_write_byte_data(client
, ADC128_REG_CONFIG
, 0x01);
395 /* If external vref is selected, configure the chip to use it */
396 if (data
->regulator
) {
397 err
= i2c_smbus_write_byte_data(client
,
398 ADC128_REG_CONFIG_ADV
, 0x01);
406 static int adc128_probe(struct i2c_client
*client
,
407 const struct i2c_device_id
*id
)
409 struct device
*dev
= &client
->dev
;
410 struct regulator
*regulator
;
411 struct device
*hwmon_dev
;
412 struct adc128_data
*data
;
415 data
= devm_kzalloc(dev
, sizeof(struct adc128_data
), GFP_KERNEL
);
419 /* vref is optional. If specified, is used as chip reference voltage */
420 regulator
= devm_regulator_get_optional(dev
, "vref");
421 if (!IS_ERR(regulator
)) {
422 data
->regulator
= regulator
;
423 err
= regulator_enable(regulator
);
426 vref
= regulator_get_voltage(regulator
);
431 data
->vref
= DIV_ROUND_CLOSEST(vref
, 1000);
433 data
->vref
= 2560; /* 2.56V, in mV */
436 data
->client
= client
;
437 i2c_set_clientdata(client
, data
);
438 mutex_init(&data
->update_lock
);
440 /* Initialize the chip */
441 err
= adc128_init_client(data
);
445 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
446 data
, adc128_groups
);
447 if (IS_ERR(hwmon_dev
)) {
448 err
= PTR_ERR(hwmon_dev
);
456 regulator_disable(data
->regulator
);
460 static int adc128_remove(struct i2c_client
*client
)
462 struct adc128_data
*data
= i2c_get_clientdata(client
);
465 regulator_disable(data
->regulator
);
470 static const struct i2c_device_id adc128_id
[] = {
474 MODULE_DEVICE_TABLE(i2c
, adc128_id
);
476 static struct i2c_driver adc128_driver
= {
477 .class = I2C_CLASS_HWMON
,
479 .name
= "adc128d818",
481 .probe
= adc128_probe
,
482 .remove
= adc128_remove
,
483 .id_table
= adc128_id
,
484 .detect
= adc128_detect
,
485 .address_list
= normal_i2c
,
488 module_i2c_driver(adc128_driver
);
490 MODULE_AUTHOR("Guenter Roeck");
491 MODULE_DESCRIPTION("Driver for ADC128D818");
492 MODULE_LICENSE("GPL");