2 * Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
4 * Copyright (C) 2008, 2010 Pengutronix
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * TODO: SPI, use power-down mode for suspend?, interrupt handling?
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/jiffies.h>
19 #include <linux/i2c.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/slab.h>
24 #define ADT7411_REG_STAT_1 0x00
25 #define ADT7411_STAT_1_INT_TEMP_HIGH BIT(0)
26 #define ADT7411_STAT_1_INT_TEMP_LOW BIT(1)
27 #define ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1 BIT(2)
28 #define ADT7411_STAT_1_EXT_TEMP_LOW BIT(3)
29 #define ADT7411_STAT_1_EXT_TEMP_FAULT BIT(4)
30 #define ADT7411_STAT_1_AIN2 BIT(5)
31 #define ADT7411_STAT_1_AIN3 BIT(6)
32 #define ADT7411_STAT_1_AIN4 BIT(7)
33 #define ADT7411_REG_STAT_2 0x01
34 #define ADT7411_STAT_2_AIN5 BIT(0)
35 #define ADT7411_STAT_2_AIN6 BIT(1)
36 #define ADT7411_STAT_2_AIN7 BIT(2)
37 #define ADT7411_STAT_2_AIN8 BIT(3)
38 #define ADT7411_STAT_2_VDD BIT(4)
39 #define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
40 #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
41 #define ADT7411_REG_VDD_MSB 0x06
42 #define ADT7411_REG_INT_TEMP_MSB 0x07
43 #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
45 #define ADT7411_REG_CFG1 0x18
46 #define ADT7411_CFG1_START_MONITOR BIT(0)
47 #define ADT7411_CFG1_RESERVED_BIT1 BIT(1)
48 #define ADT7411_CFG1_EXT_TDM BIT(2)
49 #define ADT7411_CFG1_RESERVED_BIT3 BIT(3)
51 #define ADT7411_REG_CFG2 0x19
52 #define ADT7411_CFG2_DISABLE_AVG BIT(5)
54 #define ADT7411_REG_CFG3 0x1a
55 #define ADT7411_CFG3_ADC_CLK_225 BIT(0)
56 #define ADT7411_CFG3_RESERVED_BIT1 BIT(1)
57 #define ADT7411_CFG3_RESERVED_BIT2 BIT(2)
58 #define ADT7411_CFG3_RESERVED_BIT3 BIT(3)
59 #define ADT7411_CFG3_REF_VDD BIT(4)
61 #define ADT7411_REG_VDD_HIGH 0x23
62 #define ADT7411_REG_VDD_LOW 0x24
63 #define ADT7411_REG_TEMP_HIGH(nr) (0x25 + 2 * (nr))
64 #define ADT7411_REG_TEMP_LOW(nr) (0x26 + 2 * (nr))
65 #define ADT7411_REG_IN_HIGH(nr) ((nr) > 1 \
66 ? 0x2b + 2 * ((nr)-2) \
68 #define ADT7411_REG_IN_LOW(nr) ((nr) > 1 \
69 ? 0x2c + 2 * ((nr)-2) \
72 #define ADT7411_REG_DEVICE_ID 0x4d
73 #define ADT7411_REG_MANUFACTURER_ID 0x4e
75 #define ADT7411_DEVICE_ID 0x2
76 #define ADT7411_MANUFACTURER_ID 0x41
78 static const unsigned short normal_i2c
[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END
};
80 static const u8 adt7411_in_alarm_reg
[] = {
92 static const u8 adt7411_in_alarm_bits
[] = {
94 ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
,
104 struct adt7411_data
{
105 struct mutex device_lock
; /* for "atomic" device accesses */
106 struct mutex update_lock
;
107 unsigned long next_update
;
109 struct i2c_client
*client
;
114 * When reading a register containing (up to 4) lsb, all associated
115 * msb-registers get locked by the hardware. After _one_ of those msb is read,
116 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
117 * is protected here with a mutex, too.
119 static int adt7411_read_10_bit(struct i2c_client
*client
, u8 lsb_reg
,
120 u8 msb_reg
, u8 lsb_shift
)
122 struct adt7411_data
*data
= i2c_get_clientdata(client
);
125 mutex_lock(&data
->device_lock
);
127 val
= i2c_smbus_read_byte_data(client
, lsb_reg
);
131 tmp
= (val
>> lsb_shift
) & 3;
132 val
= i2c_smbus_read_byte_data(client
, msb_reg
);
135 val
= (val
<< 2) | tmp
;
138 mutex_unlock(&data
->device_lock
);
143 static int adt7411_modify_bit(struct i2c_client
*client
, u8 reg
, u8 bit
,
146 struct adt7411_data
*data
= i2c_get_clientdata(client
);
149 mutex_lock(&data
->device_lock
);
151 ret
= i2c_smbus_read_byte_data(client
, reg
);
160 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
163 mutex_unlock(&data
->device_lock
);
167 static ssize_t
adt7411_show_bit(struct device
*dev
,
168 struct device_attribute
*attr
, char *buf
)
170 struct sensor_device_attribute_2
*attr2
= to_sensor_dev_attr_2(attr
);
171 struct adt7411_data
*data
= dev_get_drvdata(dev
);
172 struct i2c_client
*client
= data
->client
;
173 int ret
= i2c_smbus_read_byte_data(client
, attr2
->index
);
175 return ret
< 0 ? ret
: sprintf(buf
, "%u\n", !!(ret
& attr2
->nr
));
178 static ssize_t
adt7411_set_bit(struct device
*dev
,
179 struct device_attribute
*attr
, const char *buf
,
182 struct sensor_device_attribute_2
*s_attr2
= to_sensor_dev_attr_2(attr
);
183 struct adt7411_data
*data
= dev_get_drvdata(dev
);
184 struct i2c_client
*client
= data
->client
;
188 ret
= kstrtoul(buf
, 0, &flag
);
192 ret
= adt7411_modify_bit(client
, s_attr2
->index
, s_attr2
->nr
, flag
);
195 mutex_lock(&data
->update_lock
);
196 data
->next_update
= jiffies
;
197 mutex_unlock(&data
->update_lock
);
199 return ret
< 0 ? ret
: count
;
202 #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
203 SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
204 adt7411_set_bit, __bit, __reg)
206 static ADT7411_BIT_ATTR(no_average
, ADT7411_REG_CFG2
, ADT7411_CFG2_DISABLE_AVG
);
207 static ADT7411_BIT_ATTR(fast_sampling
, ADT7411_REG_CFG3
, ADT7411_CFG3_ADC_CLK_225
);
208 static ADT7411_BIT_ATTR(adc_ref_vdd
, ADT7411_REG_CFG3
, ADT7411_CFG3_REF_VDD
);
210 static struct attribute
*adt7411_attrs
[] = {
211 &sensor_dev_attr_no_average
.dev_attr
.attr
,
212 &sensor_dev_attr_fast_sampling
.dev_attr
.attr
,
213 &sensor_dev_attr_adc_ref_vdd
.dev_attr
.attr
,
216 ATTRIBUTE_GROUPS(adt7411
);
218 static int adt7411_read_in_alarm(struct device
*dev
, int channel
, long *val
)
220 struct adt7411_data
*data
= dev_get_drvdata(dev
);
221 struct i2c_client
*client
= data
->client
;
224 ret
= i2c_smbus_read_byte_data(client
, adt7411_in_alarm_reg
[channel
]);
227 *val
= !!(ret
& adt7411_in_alarm_bits
[channel
]);
231 static int adt7411_read_in_vdd(struct device
*dev
, u32 attr
, long *val
)
233 struct adt7411_data
*data
= dev_get_drvdata(dev
);
234 struct i2c_client
*client
= data
->client
;
239 ret
= adt7411_read_10_bit(client
, ADT7411_REG_INT_TEMP_VDD_LSB
,
240 ADT7411_REG_VDD_MSB
, 2);
243 *val
= ret
* 7000 / 1024;
246 ret
= i2c_smbus_read_byte_data(client
, ADT7411_REG_VDD_LOW
);
249 *val
= ret
* 7000 / 256;
252 ret
= i2c_smbus_read_byte_data(client
, ADT7411_REG_VDD_HIGH
);
255 *val
= ret
* 7000 / 256;
258 return adt7411_read_in_alarm(dev
, 0, val
);
264 static int adt7411_update_vref(struct device
*dev
)
266 struct adt7411_data
*data
= dev_get_drvdata(dev
);
267 struct i2c_client
*client
= data
->client
;
270 if (time_after_eq(jiffies
, data
->next_update
)) {
271 val
= i2c_smbus_read_byte_data(client
, ADT7411_REG_CFG3
);
275 if (val
& ADT7411_CFG3_REF_VDD
) {
276 val
= adt7411_read_in_vdd(dev
, hwmon_in_input
,
281 data
->vref_cached
= 2250;
284 data
->next_update
= jiffies
+ HZ
;
290 static int adt7411_read_in_chan(struct device
*dev
, u32 attr
, int channel
,
293 struct adt7411_data
*data
= dev_get_drvdata(dev
);
294 struct i2c_client
*client
= data
->client
;
297 int reg
, lsb_reg
, lsb_shift
;
298 int nr
= channel
- 1;
300 mutex_lock(&data
->update_lock
);
301 ret
= adt7411_update_vref(dev
);
307 lsb_reg
= ADT7411_REG_EXT_TEMP_AIN14_LSB
+ (nr
>> 2);
308 lsb_shift
= 2 * (nr
& 0x03);
309 ret
= adt7411_read_10_bit(client
, lsb_reg
,
310 ADT7411_REG_EXT_TEMP_AIN1_MSB
+ nr
,
314 *val
= ret
* data
->vref_cached
/ 1024;
319 reg
= (attr
== hwmon_in_min
)
320 ? ADT7411_REG_IN_LOW(channel
)
321 : ADT7411_REG_IN_HIGH(channel
);
322 ret
= i2c_smbus_read_byte_data(client
, reg
);
325 *val
= ret
* data
->vref_cached
/ 256;
329 ret
= adt7411_read_in_alarm(dev
, channel
, val
);
336 mutex_unlock(&data
->update_lock
);
340 static int adt7411_read_in(struct device
*dev
, u32 attr
, int channel
,
344 return adt7411_read_in_vdd(dev
, attr
, val
);
346 return adt7411_read_in_chan(dev
, attr
, channel
, val
);
350 static int adt7411_read_temp_alarm(struct device
*dev
, u32 attr
, int channel
,
353 struct adt7411_data
*data
= dev_get_drvdata(dev
);
354 struct i2c_client
*client
= data
->client
;
357 ret
= i2c_smbus_read_byte_data(client
, ADT7411_REG_STAT_1
);
362 case hwmon_temp_min_alarm
:
363 bit
= channel
? ADT7411_STAT_1_EXT_TEMP_LOW
364 : ADT7411_STAT_1_INT_TEMP_LOW
;
366 case hwmon_temp_max_alarm
:
367 bit
= channel
? ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
368 : ADT7411_STAT_1_INT_TEMP_HIGH
;
370 case hwmon_temp_fault
:
371 bit
= ADT7411_STAT_1_EXT_TEMP_FAULT
;
377 *val
= !!(ret
& bit
);
381 static int adt7411_read_temp(struct device
*dev
, u32 attr
, int channel
,
384 struct adt7411_data
*data
= dev_get_drvdata(dev
);
385 struct i2c_client
*client
= data
->client
;
386 int ret
, reg
, regl
, regh
;
389 case hwmon_temp_input
:
390 regl
= channel
? ADT7411_REG_EXT_TEMP_AIN14_LSB
:
391 ADT7411_REG_INT_TEMP_VDD_LSB
;
392 regh
= channel
? ADT7411_REG_EXT_TEMP_AIN1_MSB
:
393 ADT7411_REG_INT_TEMP_MSB
;
394 ret
= adt7411_read_10_bit(client
, regl
, regh
, 0);
397 ret
= ret
& 0x200 ? ret
- 0x400 : ret
; /* 10 bit signed */
402 reg
= (attr
== hwmon_temp_min
)
403 ? ADT7411_REG_TEMP_LOW(channel
)
404 : ADT7411_REG_TEMP_HIGH(channel
);
405 ret
= i2c_smbus_read_byte_data(client
, reg
);
408 ret
= ret
& 0x80 ? ret
- 0x100 : ret
; /* 8 bit signed */
411 case hwmon_temp_min_alarm
:
412 case hwmon_temp_max_alarm
:
413 case hwmon_temp_fault
:
414 return adt7411_read_temp_alarm(dev
, attr
, channel
, val
);
420 static int adt7411_read(struct device
*dev
, enum hwmon_sensor_types type
,
421 u32 attr
, int channel
, long *val
)
425 return adt7411_read_in(dev
, attr
, channel
, val
);
427 return adt7411_read_temp(dev
, attr
, channel
, val
);
433 static int adt7411_write_in_vdd(struct device
*dev
, u32 attr
, long val
)
435 struct adt7411_data
*data
= dev_get_drvdata(dev
);
436 struct i2c_client
*client
= data
->client
;
439 val
= clamp_val(val
, 0, 255 * 7000 / 256);
440 val
= DIV_ROUND_CLOSEST(val
* 256, 7000);
444 reg
= ADT7411_REG_VDD_LOW
;
447 reg
= ADT7411_REG_VDD_HIGH
;
453 return i2c_smbus_write_byte_data(client
, reg
, val
);
456 static int adt7411_write_in_chan(struct device
*dev
, u32 attr
, int channel
,
459 struct adt7411_data
*data
= dev_get_drvdata(dev
);
460 struct i2c_client
*client
= data
->client
;
463 mutex_lock(&data
->update_lock
);
464 ret
= adt7411_update_vref(dev
);
467 val
= clamp_val(val
, 0, 255 * data
->vref_cached
/ 256);
468 val
= DIV_ROUND_CLOSEST(val
* 256, data
->vref_cached
);
472 reg
= ADT7411_REG_IN_LOW(channel
);
475 reg
= ADT7411_REG_IN_HIGH(channel
);
482 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
484 mutex_unlock(&data
->update_lock
);
488 static int adt7411_write_in(struct device
*dev
, u32 attr
, int channel
,
492 return adt7411_write_in_vdd(dev
, attr
, val
);
494 return adt7411_write_in_chan(dev
, attr
, channel
, val
);
497 static int adt7411_write_temp(struct device
*dev
, u32 attr
, int channel
,
500 struct adt7411_data
*data
= dev_get_drvdata(dev
);
501 struct i2c_client
*client
= data
->client
;
504 val
= clamp_val(val
, -128000, 127000);
505 val
= DIV_ROUND_CLOSEST(val
, 1000);
509 reg
= ADT7411_REG_TEMP_LOW(channel
);
512 reg
= ADT7411_REG_TEMP_HIGH(channel
);
518 return i2c_smbus_write_byte_data(client
, reg
, val
);
521 static int adt7411_write(struct device
*dev
, enum hwmon_sensor_types type
,
522 u32 attr
, int channel
, long val
)
526 return adt7411_write_in(dev
, attr
, channel
, val
);
528 return adt7411_write_temp(dev
, attr
, channel
, val
);
534 static umode_t
adt7411_is_visible(const void *_data
,
535 enum hwmon_sensor_types type
,
536 u32 attr
, int channel
)
538 const struct adt7411_data
*data
= _data
;
543 visible
= channel
== 0 || channel
>= 3 || !data
->use_ext_temp
;
547 return visible
? S_IRUGO
: 0;
550 return visible
? S_IRUGO
| S_IWUSR
: 0;
554 visible
= channel
== 0 || data
->use_ext_temp
;
556 case hwmon_temp_input
:
557 case hwmon_temp_min_alarm
:
558 case hwmon_temp_max_alarm
:
559 case hwmon_temp_fault
:
560 return visible
? S_IRUGO
: 0;
563 return visible
? S_IRUGO
| S_IWUSR
: 0;
572 static int adt7411_detect(struct i2c_client
*client
,
573 struct i2c_board_info
*info
)
577 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
580 val
= i2c_smbus_read_byte_data(client
, ADT7411_REG_MANUFACTURER_ID
);
581 if (val
< 0 || val
!= ADT7411_MANUFACTURER_ID
) {
582 dev_dbg(&client
->dev
,
583 "Wrong manufacturer ID. Got %d, expected %d\n",
584 val
, ADT7411_MANUFACTURER_ID
);
588 val
= i2c_smbus_read_byte_data(client
, ADT7411_REG_DEVICE_ID
);
589 if (val
< 0 || val
!= ADT7411_DEVICE_ID
) {
590 dev_dbg(&client
->dev
,
591 "Wrong device ID. Got %d, expected %d\n",
592 val
, ADT7411_DEVICE_ID
);
596 strlcpy(info
->type
, "adt7411", I2C_NAME_SIZE
);
601 static int adt7411_init_device(struct adt7411_data
*data
)
606 ret
= i2c_smbus_read_byte_data(data
->client
, ADT7411_REG_CFG3
);
611 * We must only write zero to bit 1 and bit 2 and only one to bit 3
612 * according to the datasheet.
615 val
&= ~(ADT7411_CFG3_RESERVED_BIT1
| ADT7411_CFG3_RESERVED_BIT2
);
616 val
|= ADT7411_CFG3_RESERVED_BIT3
;
618 ret
= i2c_smbus_write_byte_data(data
->client
, ADT7411_REG_CFG3
, val
);
622 ret
= i2c_smbus_read_byte_data(data
->client
, ADT7411_REG_CFG1
);
626 data
->use_ext_temp
= ret
& ADT7411_CFG1_EXT_TDM
;
629 * We must only write zero to bit 1 and only one to bit 3 according to
633 val
&= ~ADT7411_CFG1_RESERVED_BIT1
;
634 val
|= ADT7411_CFG1_RESERVED_BIT3
;
636 /* enable monitoring */
637 val
|= ADT7411_CFG1_START_MONITOR
;
639 return i2c_smbus_write_byte_data(data
->client
, ADT7411_REG_CFG1
, val
);
642 static const u32 adt7411_in_config
[] = {
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_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
651 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_ALARM
,
655 static const struct hwmon_channel_info adt7411_in
= {
657 .config
= adt7411_in_config
,
660 static const u32 adt7411_temp_config
[] = {
661 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
662 HWMON_T_MAX
| HWMON_T_MAX_ALARM
,
663 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
664 HWMON_T_MAX
| HWMON_T_MAX_ALARM
| HWMON_T_FAULT
,
668 static const struct hwmon_channel_info adt7411_temp
= {
670 .config
= adt7411_temp_config
,
673 static const struct hwmon_channel_info
*adt7411_info
[] = {
679 static const struct hwmon_ops adt7411_hwmon_ops
= {
680 .is_visible
= adt7411_is_visible
,
681 .read
= adt7411_read
,
682 .write
= adt7411_write
,
685 static const struct hwmon_chip_info adt7411_chip_info
= {
686 .ops
= &adt7411_hwmon_ops
,
687 .info
= adt7411_info
,
690 static int adt7411_probe(struct i2c_client
*client
,
691 const struct i2c_device_id
*id
)
693 struct device
*dev
= &client
->dev
;
694 struct adt7411_data
*data
;
695 struct device
*hwmon_dev
;
698 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
702 i2c_set_clientdata(client
, data
);
703 data
->client
= client
;
704 mutex_init(&data
->device_lock
);
705 mutex_init(&data
->update_lock
);
707 ret
= adt7411_init_device(data
);
711 /* force update on first occasion */
712 data
->next_update
= jiffies
;
714 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
718 return PTR_ERR_OR_ZERO(hwmon_dev
);
721 static const struct i2c_device_id adt7411_id
[] = {
725 MODULE_DEVICE_TABLE(i2c
, adt7411_id
);
727 static struct i2c_driver adt7411_driver
= {
731 .probe
= adt7411_probe
,
732 .id_table
= adt7411_id
,
733 .detect
= adt7411_detect
,
734 .address_list
= normal_i2c
,
735 .class = I2C_CLASS_HWMON
,
738 module_i2c_driver(adt7411_driver
);
740 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
741 "Wolfram Sang <w.sang@pengutronix.de>");
742 MODULE_DESCRIPTION("ADT7411 driver");
743 MODULE_LICENSE("GPL v2");