1 // SPDX-License-Identifier: GPL-2.0-only
3 * An hwmon driver for the Analog Devices AD7416/17/18
4 * Copyright (C) 2006-07 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
9 * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
12 #include <linux/module.h>
13 #include <linux/jiffies.h>
14 #include <linux/i2c.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <linux/of_device.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
25 #define DRV_VERSION "0.4"
27 enum chips
{ ad7416
, ad7417
, ad7418
};
29 /* AD7418 registers */
30 #define AD7418_REG_TEMP_IN 0x00
31 #define AD7418_REG_CONF 0x01
32 #define AD7418_REG_TEMP_HYST 0x02
33 #define AD7418_REG_TEMP_OS 0x03
34 #define AD7418_REG_ADC 0x04
35 #define AD7418_REG_CONF2 0x05
37 #define AD7418_REG_ADC_CH(x) ((x) << 5)
38 #define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
40 static const u8 AD7418_REG_TEMP
[] = { AD7418_REG_TEMP_IN
,
45 struct i2c_client
*client
;
48 int adc_max
; /* number of ADC channels */
50 unsigned long last_updated
; /* In jiffies */
51 s16 temp
[3]; /* Register values */
55 static int ad7418_update_device(struct device
*dev
)
57 struct ad7418_data
*data
= dev_get_drvdata(dev
);
58 struct i2c_client
*client
= data
->client
;
61 mutex_lock(&data
->lock
);
63 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
68 /* read config register and clear channel bits */
69 val
= i2c_smbus_read_byte_data(client
, AD7418_REG_CONF
);
76 val
= i2c_smbus_write_byte_data(client
, AD7418_REG_CONF
,
77 cfg
| AD7418_CH_TEMP
);
83 for (i
= 0; i
< 3; i
++) {
84 val
= i2c_smbus_read_word_swapped(client
,
92 for (i
= 0, ch
= 4; i
< data
->adc_max
; i
++, ch
--) {
93 val
= i2c_smbus_write_byte_data(client
, AD7418_REG_CONF
,
94 cfg
| AD7418_REG_ADC_CH(ch
));
99 val
= i2c_smbus_read_word_swapped(client
,
104 data
->in
[data
->adc_max
- 1 - i
] = val
;
107 /* restore old configuration value */
108 val
= i2c_smbus_write_word_swapped(client
, AD7418_REG_CONF
,
113 data
->last_updated
= jiffies
;
117 mutex_unlock(&data
->lock
);
122 mutex_unlock(&data
->lock
);
126 static ssize_t
temp_show(struct device
*dev
, struct device_attribute
*devattr
,
129 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
130 struct ad7418_data
*data
= dev_get_drvdata(dev
);
133 ret
= ad7418_update_device(dev
);
137 return sprintf(buf
, "%d\n",
138 LM75_TEMP_FROM_REG(data
->temp
[attr
->index
]));
141 static ssize_t
adc_show(struct device
*dev
, struct device_attribute
*devattr
,
144 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
145 struct ad7418_data
*data
= dev_get_drvdata(dev
);
148 ret
= ad7418_update_device(dev
);
152 return sprintf(buf
, "%d\n",
153 ((data
->in
[attr
->index
] >> 6) * 2500 + 512) / 1024);
156 static ssize_t
temp_store(struct device
*dev
,
157 struct device_attribute
*devattr
, const char *buf
,
160 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
161 struct ad7418_data
*data
= dev_get_drvdata(dev
);
162 struct i2c_client
*client
= data
->client
;
164 int ret
= kstrtol(buf
, 10, &temp
);
169 mutex_lock(&data
->lock
);
170 data
->temp
[attr
->index
] = LM75_TEMP_TO_REG(temp
);
171 i2c_smbus_write_word_swapped(client
,
172 AD7418_REG_TEMP
[attr
->index
],
173 data
->temp
[attr
->index
]);
174 mutex_unlock(&data
->lock
);
178 static SENSOR_DEVICE_ATTR_RO(temp1_input
, temp
, 0);
179 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst
, temp
, 1);
180 static SENSOR_DEVICE_ATTR_RW(temp1_max
, temp
, 2);
182 static SENSOR_DEVICE_ATTR_RO(in1_input
, adc
, 0);
183 static SENSOR_DEVICE_ATTR_RO(in2_input
, adc
, 1);
184 static SENSOR_DEVICE_ATTR_RO(in3_input
, adc
, 2);
185 static SENSOR_DEVICE_ATTR_RO(in4_input
, adc
, 3);
187 static struct attribute
*ad7416_attrs
[] = {
188 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
189 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
190 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
193 ATTRIBUTE_GROUPS(ad7416
);
195 static struct attribute
*ad7417_attrs
[] = {
196 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
197 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
198 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
199 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
200 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
201 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
202 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
205 ATTRIBUTE_GROUPS(ad7417
);
207 static struct attribute
*ad7418_attrs
[] = {
208 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
209 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
210 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
211 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
214 ATTRIBUTE_GROUPS(ad7418
);
216 static void ad7418_init_client(struct i2c_client
*client
)
218 struct ad7418_data
*data
= i2c_get_clientdata(client
);
220 int reg
= i2c_smbus_read_byte_data(client
, AD7418_REG_CONF
);
222 dev_err(&client
->dev
, "cannot read configuration register\n");
224 dev_info(&client
->dev
, "configuring for mode 1\n");
225 i2c_smbus_write_byte_data(client
, AD7418_REG_CONF
, reg
& 0xfe);
227 if (data
->type
== ad7417
|| data
->type
== ad7418
)
228 i2c_smbus_write_byte_data(client
,
229 AD7418_REG_CONF2
, 0x00);
233 static const struct i2c_device_id ad7418_id
[];
235 static int ad7418_probe(struct i2c_client
*client
)
237 struct device
*dev
= &client
->dev
;
238 struct i2c_adapter
*adapter
= client
->adapter
;
239 struct ad7418_data
*data
;
240 struct device
*hwmon_dev
;
241 const struct attribute_group
**attr_groups
= NULL
;
243 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
244 I2C_FUNC_SMBUS_WORD_DATA
))
247 data
= devm_kzalloc(dev
, sizeof(struct ad7418_data
), GFP_KERNEL
);
251 i2c_set_clientdata(client
, data
);
253 mutex_init(&data
->lock
);
254 data
->client
= client
;
256 data
->type
= (enum chips
)of_device_get_match_data(dev
);
258 data
->type
= i2c_match_id(ad7418_id
, client
)->driver_data
;
260 switch (data
->type
) {
263 attr_groups
= ad7416_groups
;
268 attr_groups
= ad7417_groups
;
273 attr_groups
= ad7418_groups
;
277 dev_info(dev
, "%s chip found\n", client
->name
);
279 /* Initialize the AD7418 chip */
280 ad7418_init_client(client
);
282 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
,
285 return PTR_ERR_OR_ZERO(hwmon_dev
);
288 static const struct i2c_device_id ad7418_id
[] = {
289 { "ad7416", ad7416
},
290 { "ad7417", ad7417
},
291 { "ad7418", ad7418
},
294 MODULE_DEVICE_TABLE(i2c
, ad7418_id
);
296 static const struct of_device_id ad7418_dt_ids
[] = {
297 { .compatible
= "adi,ad7416", .data
= (void *)ad7416
, },
298 { .compatible
= "adi,ad7417", .data
= (void *)ad7417
, },
299 { .compatible
= "adi,ad7418", .data
= (void *)ad7418
, },
302 MODULE_DEVICE_TABLE(of
, ad7418_dt_ids
);
304 static struct i2c_driver ad7418_driver
= {
307 .of_match_table
= ad7418_dt_ids
,
309 .probe_new
= ad7418_probe
,
310 .id_table
= ad7418_id
,
313 module_i2c_driver(ad7418_driver
);
315 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
316 MODULE_DESCRIPTION("AD7416/17/18 driver");
317 MODULE_LICENSE("GPL");
318 MODULE_VERSION(DRV_VERSION
);