1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
5 * Copyright (C) 2008, 2010 Pengutronix
7 * TODO: SPI, use power-down mode for suspend?, interrupt handling?
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
14 #include <linux/mutex.h>
15 #include <linux/jiffies.h>
16 #include <linux/i2c.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/slab.h>
21 #define ADT7411_REG_STAT_1 0x00
22 #define ADT7411_STAT_1_INT_TEMP_HIGH BIT(0)
23 #define ADT7411_STAT_1_INT_TEMP_LOW BIT(1)
24 #define ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1 BIT(2)
25 #define ADT7411_STAT_1_EXT_TEMP_LOW BIT(3)
26 #define ADT7411_STAT_1_EXT_TEMP_FAULT BIT(4)
27 #define ADT7411_STAT_1_AIN2 BIT(5)
28 #define ADT7411_STAT_1_AIN3 BIT(6)
29 #define ADT7411_STAT_1_AIN4 BIT(7)
30 #define ADT7411_REG_STAT_2 0x01
31 #define ADT7411_STAT_2_AIN5 BIT(0)
32 #define ADT7411_STAT_2_AIN6 BIT(1)
33 #define ADT7411_STAT_2_AIN7 BIT(2)
34 #define ADT7411_STAT_2_AIN8 BIT(3)
35 #define ADT7411_STAT_2_VDD BIT(4)
36 #define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
37 #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
38 #define ADT7411_REG_VDD_MSB 0x06
39 #define ADT7411_REG_INT_TEMP_MSB 0x07
40 #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
42 #define ADT7411_REG_CFG1 0x18
43 #define ADT7411_CFG1_START_MONITOR BIT(0)
44 #define ADT7411_CFG1_RESERVED_BIT1 BIT(1)
45 #define ADT7411_CFG1_EXT_TDM BIT(2)
46 #define ADT7411_CFG1_RESERVED_BIT3 BIT(3)
48 #define ADT7411_REG_CFG2 0x19
49 #define ADT7411_CFG2_DISABLE_AVG BIT(5)
51 #define ADT7411_REG_CFG3 0x1a
52 #define ADT7411_CFG3_ADC_CLK_225 BIT(0)
53 #define ADT7411_CFG3_RESERVED_BIT1 BIT(1)
54 #define ADT7411_CFG3_RESERVED_BIT2 BIT(2)
55 #define ADT7411_CFG3_RESERVED_BIT3 BIT(3)
56 #define ADT7411_CFG3_REF_VDD BIT(4)
58 #define ADT7411_REG_VDD_HIGH 0x23
59 #define ADT7411_REG_VDD_LOW 0x24
60 #define ADT7411_REG_TEMP_HIGH(nr) (0x25 + 2 * (nr))
61 #define ADT7411_REG_TEMP_LOW(nr) (0x26 + 2 * (nr))
62 #define ADT7411_REG_IN_HIGH(nr) ((nr) > 1 \
63 ? 0x2b + 2 * ((nr)-2) \
65 #define ADT7411_REG_IN_LOW(nr) ((nr) > 1 \
66 ? 0x2c + 2 * ((nr)-2) \
69 #define ADT7411_REG_DEVICE_ID 0x4d
70 #define ADT7411_REG_MANUFACTURER_ID 0x4e
72 #define ADT7411_DEVICE_ID 0x2
73 #define ADT7411_MANUFACTURER_ID 0x41
75 static const unsigned short normal_i2c
[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END
};
77 static const u8 adt7411_in_alarm_reg
[] = {
89 static const u8 adt7411_in_alarm_bits
[] = {
91 ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
,
101 struct adt7411_data
{
102 struct mutex device_lock
; /* for "atomic" device accesses */
103 struct mutex update_lock
;
104 unsigned long next_update
;
106 struct i2c_client
*client
;
111 * When reading a register containing (up to 4) lsb, all associated
112 * msb-registers get locked by the hardware. After _one_ of those msb is read,
113 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
114 * is protected here with a mutex, too.
116 static int adt7411_read_10_bit(struct i2c_client
*client
, u8 lsb_reg
,
117 u8 msb_reg
, u8 lsb_shift
)
119 struct adt7411_data
*data
= i2c_get_clientdata(client
);
122 mutex_lock(&data
->device_lock
);
124 val
= i2c_smbus_read_byte_data(client
, lsb_reg
);
128 tmp
= (val
>> lsb_shift
) & 3;
129 val
= i2c_smbus_read_byte_data(client
, msb_reg
);
132 val
= (val
<< 2) | tmp
;
135 mutex_unlock(&data
->device_lock
);
140 static int adt7411_modify_bit(struct i2c_client
*client
, u8 reg
, u8 bit
,
143 struct adt7411_data
*data
= i2c_get_clientdata(client
);
146 mutex_lock(&data
->device_lock
);
148 ret
= i2c_smbus_read_byte_data(client
, reg
);
157 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
160 mutex_unlock(&data
->device_lock
);
164 static ssize_t
adt7411_show_bit(struct device
*dev
,
165 struct device_attribute
*attr
, char *buf
)
167 struct sensor_device_attribute_2
*attr2
= to_sensor_dev_attr_2(attr
);
168 struct adt7411_data
*data
= dev_get_drvdata(dev
);
169 struct i2c_client
*client
= data
->client
;
170 int ret
= i2c_smbus_read_byte_data(client
, attr2
->index
);
172 return ret
< 0 ? ret
: sprintf(buf
, "%u\n", !!(ret
& attr2
->nr
));
175 static ssize_t
adt7411_set_bit(struct device
*dev
,
176 struct device_attribute
*attr
, const char *buf
,
179 struct sensor_device_attribute_2
*s_attr2
= to_sensor_dev_attr_2(attr
);
180 struct adt7411_data
*data
= dev_get_drvdata(dev
);
181 struct i2c_client
*client
= data
->client
;
185 ret
= kstrtoul(buf
, 0, &flag
);
189 ret
= adt7411_modify_bit(client
, s_attr2
->index
, s_attr2
->nr
, flag
);
192 mutex_lock(&data
->update_lock
);
193 data
->next_update
= jiffies
;
194 mutex_unlock(&data
->update_lock
);
196 return ret
< 0 ? ret
: count
;
199 #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
200 SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
201 adt7411_set_bit, __bit, __reg)
203 static ADT7411_BIT_ATTR(no_average
, ADT7411_REG_CFG2
, ADT7411_CFG2_DISABLE_AVG
);
204 static ADT7411_BIT_ATTR(fast_sampling
, ADT7411_REG_CFG3
, ADT7411_CFG3_ADC_CLK_225
);
205 static ADT7411_BIT_ATTR(adc_ref_vdd
, ADT7411_REG_CFG3
, ADT7411_CFG3_REF_VDD
);
207 static struct attribute
*adt7411_attrs
[] = {
208 &sensor_dev_attr_no_average
.dev_attr
.attr
,
209 &sensor_dev_attr_fast_sampling
.dev_attr
.attr
,
210 &sensor_dev_attr_adc_ref_vdd
.dev_attr
.attr
,
213 ATTRIBUTE_GROUPS(adt7411
);
215 static int adt7411_read_in_alarm(struct device
*dev
, int channel
, long *val
)
217 struct adt7411_data
*data
= dev_get_drvdata(dev
);
218 struct i2c_client
*client
= data
->client
;
221 ret
= i2c_smbus_read_byte_data(client
, adt7411_in_alarm_reg
[channel
]);
224 *val
= !!(ret
& adt7411_in_alarm_bits
[channel
]);
228 static int adt7411_read_in_vdd(struct device
*dev
, u32 attr
, long *val
)
230 struct adt7411_data
*data
= dev_get_drvdata(dev
);
231 struct i2c_client
*client
= data
->client
;
236 ret
= adt7411_read_10_bit(client
, ADT7411_REG_INT_TEMP_VDD_LSB
,
237 ADT7411_REG_VDD_MSB
, 2);
240 *val
= ret
* 7000 / 1024;
243 ret
= i2c_smbus_read_byte_data(client
, ADT7411_REG_VDD_LOW
);
246 *val
= ret
* 7000 / 256;
249 ret
= i2c_smbus_read_byte_data(client
, ADT7411_REG_VDD_HIGH
);
252 *val
= ret
* 7000 / 256;
255 return adt7411_read_in_alarm(dev
, 0, val
);
261 static int adt7411_update_vref(struct device
*dev
)
263 struct adt7411_data
*data
= dev_get_drvdata(dev
);
264 struct i2c_client
*client
= data
->client
;
267 if (time_after_eq(jiffies
, data
->next_update
)) {
268 val
= i2c_smbus_read_byte_data(client
, ADT7411_REG_CFG3
);
272 if (val
& ADT7411_CFG3_REF_VDD
) {
273 val
= adt7411_read_in_vdd(dev
, hwmon_in_input
,
278 data
->vref_cached
= 2250;
281 data
->next_update
= jiffies
+ HZ
;
287 static int adt7411_read_in_chan(struct device
*dev
, u32 attr
, int channel
,
290 struct adt7411_data
*data
= dev_get_drvdata(dev
);
291 struct i2c_client
*client
= data
->client
;
294 int reg
, lsb_reg
, lsb_shift
;
295 int nr
= channel
- 1;
297 mutex_lock(&data
->update_lock
);
298 ret
= adt7411_update_vref(dev
);
304 lsb_reg
= ADT7411_REG_EXT_TEMP_AIN14_LSB
+ (nr
>> 2);
305 lsb_shift
= 2 * (nr
& 0x03);
306 ret
= adt7411_read_10_bit(client
, lsb_reg
,
307 ADT7411_REG_EXT_TEMP_AIN1_MSB
+ nr
,
311 *val
= ret
* data
->vref_cached
/ 1024;
316 reg
= (attr
== hwmon_in_min
)
317 ? ADT7411_REG_IN_LOW(channel
)
318 : ADT7411_REG_IN_HIGH(channel
);
319 ret
= i2c_smbus_read_byte_data(client
, reg
);
322 *val
= ret
* data
->vref_cached
/ 256;
326 ret
= adt7411_read_in_alarm(dev
, channel
, val
);
333 mutex_unlock(&data
->update_lock
);
337 static int adt7411_read_in(struct device
*dev
, u32 attr
, int channel
,
341 return adt7411_read_in_vdd(dev
, attr
, val
);
343 return adt7411_read_in_chan(dev
, attr
, channel
, val
);
347 static int adt7411_read_temp_alarm(struct device
*dev
, u32 attr
, int channel
,
350 struct adt7411_data
*data
= dev_get_drvdata(dev
);
351 struct i2c_client
*client
= data
->client
;
354 ret
= i2c_smbus_read_byte_data(client
, ADT7411_REG_STAT_1
);
359 case hwmon_temp_min_alarm
:
360 bit
= channel
? ADT7411_STAT_1_EXT_TEMP_LOW
361 : ADT7411_STAT_1_INT_TEMP_LOW
;
363 case hwmon_temp_max_alarm
:
364 bit
= channel
? ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
365 : ADT7411_STAT_1_INT_TEMP_HIGH
;
367 case hwmon_temp_fault
:
368 bit
= ADT7411_STAT_1_EXT_TEMP_FAULT
;
374 *val
= !!(ret
& bit
);
378 static int adt7411_read_temp(struct device
*dev
, u32 attr
, int channel
,
381 struct adt7411_data
*data
= dev_get_drvdata(dev
);
382 struct i2c_client
*client
= data
->client
;
383 int ret
, reg
, regl
, regh
;
386 case hwmon_temp_input
:
387 regl
= channel
? ADT7411_REG_EXT_TEMP_AIN14_LSB
:
388 ADT7411_REG_INT_TEMP_VDD_LSB
;
389 regh
= channel
? ADT7411_REG_EXT_TEMP_AIN1_MSB
:
390 ADT7411_REG_INT_TEMP_MSB
;
391 ret
= adt7411_read_10_bit(client
, regl
, regh
, 0);
394 ret
= ret
& 0x200 ? ret
- 0x400 : ret
; /* 10 bit signed */
399 reg
= (attr
== hwmon_temp_min
)
400 ? ADT7411_REG_TEMP_LOW(channel
)
401 : ADT7411_REG_TEMP_HIGH(channel
);
402 ret
= i2c_smbus_read_byte_data(client
, reg
);
405 ret
= ret
& 0x80 ? ret
- 0x100 : ret
; /* 8 bit signed */
408 case hwmon_temp_min_alarm
:
409 case hwmon_temp_max_alarm
:
410 case hwmon_temp_fault
:
411 return adt7411_read_temp_alarm(dev
, attr
, channel
, val
);
417 static int adt7411_read(struct device
*dev
, enum hwmon_sensor_types type
,
418 u32 attr
, int channel
, long *val
)
422 return adt7411_read_in(dev
, attr
, channel
, val
);
424 return adt7411_read_temp(dev
, attr
, channel
, val
);
430 static int adt7411_write_in_vdd(struct device
*dev
, u32 attr
, long val
)
432 struct adt7411_data
*data
= dev_get_drvdata(dev
);
433 struct i2c_client
*client
= data
->client
;
436 val
= clamp_val(val
, 0, 255 * 7000 / 256);
437 val
= DIV_ROUND_CLOSEST(val
* 256, 7000);
441 reg
= ADT7411_REG_VDD_LOW
;
444 reg
= ADT7411_REG_VDD_HIGH
;
450 return i2c_smbus_write_byte_data(client
, reg
, val
);
453 static int adt7411_write_in_chan(struct device
*dev
, u32 attr
, int channel
,
456 struct adt7411_data
*data
= dev_get_drvdata(dev
);
457 struct i2c_client
*client
= data
->client
;
460 mutex_lock(&data
->update_lock
);
461 ret
= adt7411_update_vref(dev
);
464 val
= clamp_val(val
, 0, 255 * data
->vref_cached
/ 256);
465 val
= DIV_ROUND_CLOSEST(val
* 256, data
->vref_cached
);
469 reg
= ADT7411_REG_IN_LOW(channel
);
472 reg
= ADT7411_REG_IN_HIGH(channel
);
479 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
481 mutex_unlock(&data
->update_lock
);
485 static int adt7411_write_in(struct device
*dev
, u32 attr
, int channel
,
489 return adt7411_write_in_vdd(dev
, attr
, val
);
491 return adt7411_write_in_chan(dev
, attr
, channel
, val
);
494 static int adt7411_write_temp(struct device
*dev
, u32 attr
, int channel
,
497 struct adt7411_data
*data
= dev_get_drvdata(dev
);
498 struct i2c_client
*client
= data
->client
;
501 val
= clamp_val(val
, -128000, 127000);
502 val
= DIV_ROUND_CLOSEST(val
, 1000);
506 reg
= ADT7411_REG_TEMP_LOW(channel
);
509 reg
= ADT7411_REG_TEMP_HIGH(channel
);
515 return i2c_smbus_write_byte_data(client
, reg
, val
);
518 static int adt7411_write(struct device
*dev
, enum hwmon_sensor_types type
,
519 u32 attr
, int channel
, long val
)
523 return adt7411_write_in(dev
, attr
, channel
, val
);
525 return adt7411_write_temp(dev
, attr
, channel
, val
);
531 static umode_t
adt7411_is_visible(const void *_data
,
532 enum hwmon_sensor_types type
,
533 u32 attr
, int channel
)
535 const struct adt7411_data
*data
= _data
;
540 visible
= channel
== 0 || channel
>= 3 || !data
->use_ext_temp
;
544 return visible
? S_IRUGO
: 0;
547 return visible
? S_IRUGO
| S_IWUSR
: 0;
551 visible
= channel
== 0 || data
->use_ext_temp
;
553 case hwmon_temp_input
:
554 case hwmon_temp_min_alarm
:
555 case hwmon_temp_max_alarm
:
556 case hwmon_temp_fault
:
557 return visible
? S_IRUGO
: 0;
560 return visible
? S_IRUGO
| S_IWUSR
: 0;
569 static int adt7411_detect(struct i2c_client
*client
,
570 struct i2c_board_info
*info
)
574 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
577 val
= i2c_smbus_read_byte_data(client
, ADT7411_REG_MANUFACTURER_ID
);
578 if (val
< 0 || val
!= ADT7411_MANUFACTURER_ID
) {
579 dev_dbg(&client
->dev
,
580 "Wrong manufacturer ID. Got %d, expected %d\n",
581 val
, ADT7411_MANUFACTURER_ID
);
585 val
= i2c_smbus_read_byte_data(client
, ADT7411_REG_DEVICE_ID
);
586 if (val
< 0 || val
!= ADT7411_DEVICE_ID
) {
587 dev_dbg(&client
->dev
,
588 "Wrong device ID. Got %d, expected %d\n",
589 val
, ADT7411_DEVICE_ID
);
593 strlcpy(info
->type
, "adt7411", I2C_NAME_SIZE
);
598 static int adt7411_init_device(struct adt7411_data
*data
)
603 ret
= i2c_smbus_read_byte_data(data
->client
, ADT7411_REG_CFG3
);
608 * We must only write zero to bit 1 and bit 2 and only one to bit 3
609 * according to the datasheet.
612 val
&= ~(ADT7411_CFG3_RESERVED_BIT1
| ADT7411_CFG3_RESERVED_BIT2
);
613 val
|= ADT7411_CFG3_RESERVED_BIT3
;
615 ret
= i2c_smbus_write_byte_data(data
->client
, ADT7411_REG_CFG3
, val
);
619 ret
= i2c_smbus_read_byte_data(data
->client
, ADT7411_REG_CFG1
);
623 data
->use_ext_temp
= ret
& ADT7411_CFG1_EXT_TDM
;
626 * We must only write zero to bit 1 and only one to bit 3 according to
630 val
&= ~ADT7411_CFG1_RESERVED_BIT1
;
631 val
|= ADT7411_CFG1_RESERVED_BIT3
;
633 /* enable monitoring */
634 val
|= ADT7411_CFG1_START_MONITOR
;
636 return i2c_smbus_write_byte_data(data
->client
, ADT7411_REG_CFG1
, val
);
639 static const struct hwmon_channel_info
*adt7411_info
[] = {
640 HWMON_CHANNEL_INFO(in
,
641 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
642 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
643 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
644 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
645 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
646 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
647 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
648 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
649 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
),
650 HWMON_CHANNEL_INFO(temp
,
651 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
652 HWMON_T_MAX
| HWMON_T_MAX_ALARM
,
653 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
654 HWMON_T_MAX
| HWMON_T_MAX_ALARM
| HWMON_T_FAULT
),
658 static const struct hwmon_ops adt7411_hwmon_ops
= {
659 .is_visible
= adt7411_is_visible
,
660 .read
= adt7411_read
,
661 .write
= adt7411_write
,
664 static const struct hwmon_chip_info adt7411_chip_info
= {
665 .ops
= &adt7411_hwmon_ops
,
666 .info
= adt7411_info
,
669 static int adt7411_probe(struct i2c_client
*client
)
671 struct device
*dev
= &client
->dev
;
672 struct adt7411_data
*data
;
673 struct device
*hwmon_dev
;
676 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
680 i2c_set_clientdata(client
, data
);
681 data
->client
= client
;
682 mutex_init(&data
->device_lock
);
683 mutex_init(&data
->update_lock
);
685 ret
= adt7411_init_device(data
);
689 /* force update on first occasion */
690 data
->next_update
= jiffies
;
692 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
696 return PTR_ERR_OR_ZERO(hwmon_dev
);
699 static const struct i2c_device_id adt7411_id
[] = {
703 MODULE_DEVICE_TABLE(i2c
, adt7411_id
);
705 static struct i2c_driver adt7411_driver
= {
709 .probe_new
= adt7411_probe
,
710 .id_table
= adt7411_id
,
711 .detect
= adt7411_detect
,
712 .address_list
= normal_i2c
,
713 .class = I2C_CLASS_HWMON
,
716 module_i2c_driver(adt7411_driver
);
718 MODULE_AUTHOR("Sascha Hauer, Wolfram Sang <kernel@pengutronix.de>");
719 MODULE_DESCRIPTION("ADT7411 driver");
720 MODULE_LICENSE("GPL v2");