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_INT_TEMP_VDD_LSB 0x03
25 #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
26 #define ADT7411_REG_VDD_MSB 0x06
27 #define ADT7411_REG_INT_TEMP_MSB 0x07
28 #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
30 #define ADT7411_REG_CFG1 0x18
31 #define ADT7411_CFG1_START_MONITOR (1 << 0)
32 #define ADT7411_CFG1_RESERVED_BIT1 (1 << 1)
33 #define ADT7411_CFG1_EXT_TDM (1 << 2)
34 #define ADT7411_CFG1_RESERVED_BIT3 (1 << 3)
36 #define ADT7411_REG_CFG2 0x19
37 #define ADT7411_CFG2_DISABLE_AVG (1 << 5)
39 #define ADT7411_REG_CFG3 0x1a
40 #define ADT7411_CFG3_ADC_CLK_225 (1 << 0)
41 #define ADT7411_CFG3_RESERVED_BIT1 (1 << 1)
42 #define ADT7411_CFG3_RESERVED_BIT2 (1 << 2)
43 #define ADT7411_CFG3_RESERVED_BIT3 (1 << 3)
44 #define ADT7411_CFG3_REF_VDD (1 << 4)
46 #define ADT7411_REG_DEVICE_ID 0x4d
47 #define ADT7411_REG_MANUFACTURER_ID 0x4e
49 #define ADT7411_DEVICE_ID 0x2
50 #define ADT7411_MANUFACTURER_ID 0x41
52 static const unsigned short normal_i2c
[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END
};
55 struct mutex device_lock
; /* for "atomic" device accesses */
56 struct mutex update_lock
;
57 unsigned long next_update
;
59 struct i2c_client
*client
;
64 * When reading a register containing (up to 4) lsb, all associated
65 * msb-registers get locked by the hardware. After _one_ of those msb is read,
66 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
67 * is protected here with a mutex, too.
69 static int adt7411_read_10_bit(struct i2c_client
*client
, u8 lsb_reg
,
70 u8 msb_reg
, u8 lsb_shift
)
72 struct adt7411_data
*data
= i2c_get_clientdata(client
);
75 mutex_lock(&data
->device_lock
);
77 val
= i2c_smbus_read_byte_data(client
, lsb_reg
);
81 tmp
= (val
>> lsb_shift
) & 3;
82 val
= i2c_smbus_read_byte_data(client
, msb_reg
);
85 val
= (val
<< 2) | tmp
;
88 mutex_unlock(&data
->device_lock
);
93 static int adt7411_modify_bit(struct i2c_client
*client
, u8 reg
, u8 bit
,
96 struct adt7411_data
*data
= i2c_get_clientdata(client
);
99 mutex_lock(&data
->device_lock
);
101 ret
= i2c_smbus_read_byte_data(client
, reg
);
110 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
113 mutex_unlock(&data
->device_lock
);
117 static ssize_t
adt7411_show_bit(struct device
*dev
,
118 struct device_attribute
*attr
, char *buf
)
120 struct sensor_device_attribute_2
*attr2
= to_sensor_dev_attr_2(attr
);
121 struct adt7411_data
*data
= dev_get_drvdata(dev
);
122 struct i2c_client
*client
= data
->client
;
123 int ret
= i2c_smbus_read_byte_data(client
, attr2
->index
);
125 return ret
< 0 ? ret
: sprintf(buf
, "%u\n", !!(ret
& attr2
->nr
));
128 static ssize_t
adt7411_set_bit(struct device
*dev
,
129 struct device_attribute
*attr
, const char *buf
,
132 struct sensor_device_attribute_2
*s_attr2
= to_sensor_dev_attr_2(attr
);
133 struct adt7411_data
*data
= dev_get_drvdata(dev
);
134 struct i2c_client
*client
= data
->client
;
138 ret
= kstrtoul(buf
, 0, &flag
);
142 ret
= adt7411_modify_bit(client
, s_attr2
->index
, s_attr2
->nr
, flag
);
145 mutex_lock(&data
->update_lock
);
146 data
->next_update
= jiffies
;
147 mutex_unlock(&data
->update_lock
);
149 return ret
< 0 ? ret
: count
;
152 #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
153 SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
154 adt7411_set_bit, __bit, __reg)
156 static ADT7411_BIT_ATTR(no_average
, ADT7411_REG_CFG2
, ADT7411_CFG2_DISABLE_AVG
);
157 static ADT7411_BIT_ATTR(fast_sampling
, ADT7411_REG_CFG3
, ADT7411_CFG3_ADC_CLK_225
);
158 static ADT7411_BIT_ATTR(adc_ref_vdd
, ADT7411_REG_CFG3
, ADT7411_CFG3_REF_VDD
);
160 static struct attribute
*adt7411_attrs
[] = {
161 &sensor_dev_attr_no_average
.dev_attr
.attr
,
162 &sensor_dev_attr_fast_sampling
.dev_attr
.attr
,
163 &sensor_dev_attr_adc_ref_vdd
.dev_attr
.attr
,
166 ATTRIBUTE_GROUPS(adt7411
);
168 static int adt7411_read_in_vdd(struct device
*dev
, u32 attr
, long *val
)
170 struct adt7411_data
*data
= dev_get_drvdata(dev
);
171 struct i2c_client
*client
= data
->client
;
176 ret
= adt7411_read_10_bit(client
, ADT7411_REG_INT_TEMP_VDD_LSB
,
177 ADT7411_REG_VDD_MSB
, 2);
180 *val
= ret
* 7000 / 1024;
187 static int adt7411_read_in_chan(struct device
*dev
, u32 attr
, int channel
,
190 struct adt7411_data
*data
= dev_get_drvdata(dev
);
191 struct i2c_client
*client
= data
->client
;
194 int lsb_reg
, lsb_shift
;
195 int nr
= channel
- 1;
197 mutex_lock(&data
->update_lock
);
198 if (time_after_eq(jiffies
, data
->next_update
)) {
199 ret
= i2c_smbus_read_byte_data(client
, ADT7411_REG_CFG3
);
203 if (ret
& ADT7411_CFG3_REF_VDD
) {
204 ret
= adt7411_read_in_vdd(dev
, hwmon_in_input
,
209 data
->vref_cached
= 2250;
212 data
->next_update
= jiffies
+ HZ
;
217 lsb_reg
= ADT7411_REG_EXT_TEMP_AIN14_LSB
+ (nr
>> 2);
218 lsb_shift
= 2 * (nr
& 0x03);
219 ret
= adt7411_read_10_bit(client
, lsb_reg
,
220 ADT7411_REG_EXT_TEMP_AIN1_MSB
+ nr
,
224 *val
= ret
* data
->vref_cached
/ 1024;
232 mutex_unlock(&data
->update_lock
);
236 static int adt7411_read_in(struct device
*dev
, u32 attr
, int channel
,
240 return adt7411_read_in_vdd(dev
, attr
, val
);
242 return adt7411_read_in_chan(dev
, attr
, channel
, val
);
245 static int adt7411_read_temp(struct device
*dev
, u32 attr
, int channel
,
248 struct adt7411_data
*data
= dev_get_drvdata(dev
);
249 struct i2c_client
*client
= data
->client
;
253 case hwmon_temp_input
:
254 regl
= channel
? ADT7411_REG_EXT_TEMP_AIN14_LSB
:
255 ADT7411_REG_INT_TEMP_VDD_LSB
;
256 regh
= channel
? ADT7411_REG_EXT_TEMP_AIN1_MSB
:
257 ADT7411_REG_INT_TEMP_MSB
;
258 ret
= adt7411_read_10_bit(client
, regl
, regh
, 0);
261 ret
= ret
& 0x200 ? ret
- 0x400 : ret
; /* 10 bit signed */
269 static int adt7411_read(struct device
*dev
, enum hwmon_sensor_types type
,
270 u32 attr
, int channel
, long *val
)
274 return adt7411_read_in(dev
, attr
, channel
, val
);
276 return adt7411_read_temp(dev
, attr
, channel
, val
);
282 static umode_t
adt7411_is_visible(const void *_data
,
283 enum hwmon_sensor_types type
,
284 u32 attr
, int channel
)
286 const struct adt7411_data
*data
= _data
;
290 if (channel
> 0 && channel
< 3)
291 return data
->use_ext_temp
? 0 : S_IRUGO
;
296 return data
->use_ext_temp
? S_IRUGO
: 0;
304 static int adt7411_detect(struct i2c_client
*client
,
305 struct i2c_board_info
*info
)
309 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
312 val
= i2c_smbus_read_byte_data(client
, ADT7411_REG_MANUFACTURER_ID
);
313 if (val
< 0 || val
!= ADT7411_MANUFACTURER_ID
) {
314 dev_dbg(&client
->dev
,
315 "Wrong manufacturer ID. Got %d, expected %d\n",
316 val
, ADT7411_MANUFACTURER_ID
);
320 val
= i2c_smbus_read_byte_data(client
, ADT7411_REG_DEVICE_ID
);
321 if (val
< 0 || val
!= ADT7411_DEVICE_ID
) {
322 dev_dbg(&client
->dev
,
323 "Wrong device ID. Got %d, expected %d\n",
324 val
, ADT7411_DEVICE_ID
);
328 strlcpy(info
->type
, "adt7411", I2C_NAME_SIZE
);
333 static int adt7411_init_device(struct adt7411_data
*data
)
338 ret
= i2c_smbus_read_byte_data(data
->client
, ADT7411_REG_CFG3
);
343 * We must only write zero to bit 1 and bit 2 and only one to bit 3
344 * according to the datasheet.
347 val
&= ~(ADT7411_CFG3_RESERVED_BIT1
| ADT7411_CFG3_RESERVED_BIT2
);
348 val
|= ADT7411_CFG3_RESERVED_BIT3
;
350 ret
= i2c_smbus_write_byte_data(data
->client
, ADT7411_REG_CFG3
, val
);
354 ret
= i2c_smbus_read_byte_data(data
->client
, ADT7411_REG_CFG1
);
358 data
->use_ext_temp
= ret
& ADT7411_CFG1_EXT_TDM
;
361 * We must only write zero to bit 1 and only one to bit 3 according to
365 val
&= ~ADT7411_CFG1_RESERVED_BIT1
;
366 val
|= ADT7411_CFG1_RESERVED_BIT3
;
368 /* enable monitoring */
369 val
|= ADT7411_CFG1_START_MONITOR
;
371 return i2c_smbus_write_byte_data(data
->client
, ADT7411_REG_CFG1
, val
);
374 static const u32 adt7411_in_config
[] = {
387 static const struct hwmon_channel_info adt7411_in
= {
389 .config
= adt7411_in_config
,
392 static const u32 adt7411_temp_config
[] = {
398 static const struct hwmon_channel_info adt7411_temp
= {
400 .config
= adt7411_temp_config
,
403 static const struct hwmon_channel_info
*adt7411_info
[] = {
409 static const struct hwmon_ops adt7411_hwmon_ops
= {
410 .is_visible
= adt7411_is_visible
,
411 .read
= adt7411_read
,
414 static const struct hwmon_chip_info adt7411_chip_info
= {
415 .ops
= &adt7411_hwmon_ops
,
416 .info
= adt7411_info
,
419 static int adt7411_probe(struct i2c_client
*client
,
420 const struct i2c_device_id
*id
)
422 struct device
*dev
= &client
->dev
;
423 struct adt7411_data
*data
;
424 struct device
*hwmon_dev
;
427 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
431 i2c_set_clientdata(client
, data
);
432 data
->client
= client
;
433 mutex_init(&data
->device_lock
);
434 mutex_init(&data
->update_lock
);
436 ret
= adt7411_init_device(data
);
440 /* force update on first occasion */
441 data
->next_update
= jiffies
;
443 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
447 return PTR_ERR_OR_ZERO(hwmon_dev
);
450 static const struct i2c_device_id adt7411_id
[] = {
454 MODULE_DEVICE_TABLE(i2c
, adt7411_id
);
456 static struct i2c_driver adt7411_driver
= {
460 .probe
= adt7411_probe
,
461 .id_table
= adt7411_id
,
462 .detect
= adt7411_detect
,
463 .address_list
= normal_i2c
,
464 .class = I2C_CLASS_HWMON
,
467 module_i2c_driver(adt7411_driver
);
469 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
470 "Wolfram Sang <w.sang@pengutronix.de>");
471 MODULE_DESCRIPTION("ADT7411 driver");
472 MODULE_LICENSE("GPL v2");