2 * An hwmon driver for the Analog Devices AD7416/17/18
3 * Copyright (C) 2006-07 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
8 * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License,
12 * as published by the Free Software Foundation - version 2.
15 #include <linux/module.h>
16 #include <linux/jiffies.h>
17 #include <linux/i2c.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/of_device.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
28 #define DRV_VERSION "0.4"
30 enum chips { ad7416, ad7417, ad7418 };
32 /* AD7418 registers */
33 #define AD7418_REG_TEMP_IN 0x00
34 #define AD7418_REG_CONF 0x01
35 #define AD7418_REG_TEMP_HYST 0x02
36 #define AD7418_REG_TEMP_OS 0x03
37 #define AD7418_REG_ADC 0x04
38 #define AD7418_REG_CONF2 0x05
40 #define AD7418_REG_ADC_CH(x) ((x) << 5)
41 #define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
43 static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
48 struct i2c_client *client;
51 int adc_max; /* number of ADC channels */
53 unsigned long last_updated; /* In jiffies */
54 s16 temp[3]; /* Register values */
58 static int ad7418_update_device(struct device *dev)
60 struct ad7418_data *data = dev_get_drvdata(dev);
61 struct i2c_client *client = data->client;
64 mutex_lock(&data->lock);
66 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
71 /* read config register and clear channel bits */
72 val = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
79 val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
80 cfg | AD7418_CH_TEMP);
86 for (i = 0; i < 3; i++) {
87 val = i2c_smbus_read_word_swapped(client,
95 for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
96 val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
97 cfg | AD7418_REG_ADC_CH(ch));
102 val = i2c_smbus_read_word_swapped(client,
107 data->in[data->adc_max - 1 - i] = val;
110 /* restore old configuration value */
111 val = i2c_smbus_write_word_swapped(client, AD7418_REG_CONF,
116 data->last_updated = jiffies;
120 mutex_unlock(&data->lock);
125 mutex_unlock(&data->lock);
129 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
132 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
133 struct ad7418_data *data = dev_get_drvdata(dev);
136 ret = ad7418_update_device(dev);
140 return sprintf(buf, "%d\n",
141 LM75_TEMP_FROM_REG(data->temp[attr->index]));
144 static ssize_t adc_show(struct device *dev, struct device_attribute *devattr,
147 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
148 struct ad7418_data *data = dev_get_drvdata(dev);
151 ret = ad7418_update_device(dev);
155 return sprintf(buf, "%d\n",
156 ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
159 static ssize_t temp_store(struct device *dev,
160 struct device_attribute *devattr, const char *buf,
163 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
164 struct ad7418_data *data = dev_get_drvdata(dev);
165 struct i2c_client *client = data->client;
167 int ret = kstrtol(buf, 10, &temp);
172 mutex_lock(&data->lock);
173 data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
174 i2c_smbus_write_word_swapped(client,
175 AD7418_REG_TEMP[attr->index],
176 data->temp[attr->index]);
177 mutex_unlock(&data->lock);
181 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
182 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, 1);
183 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
185 static SENSOR_DEVICE_ATTR_RO(in1_input, adc, 0);
186 static SENSOR_DEVICE_ATTR_RO(in2_input, adc, 1);
187 static SENSOR_DEVICE_ATTR_RO(in3_input, adc, 2);
188 static SENSOR_DEVICE_ATTR_RO(in4_input, adc, 3);
190 static struct attribute *ad7416_attrs[] = {
191 &sensor_dev_attr_temp1_max.dev_attr.attr,
192 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
193 &sensor_dev_attr_temp1_input.dev_attr.attr,
196 ATTRIBUTE_GROUPS(ad7416);
198 static struct attribute *ad7417_attrs[] = {
199 &sensor_dev_attr_temp1_max.dev_attr.attr,
200 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
201 &sensor_dev_attr_temp1_input.dev_attr.attr,
202 &sensor_dev_attr_in1_input.dev_attr.attr,
203 &sensor_dev_attr_in2_input.dev_attr.attr,
204 &sensor_dev_attr_in3_input.dev_attr.attr,
205 &sensor_dev_attr_in4_input.dev_attr.attr,
208 ATTRIBUTE_GROUPS(ad7417);
210 static struct attribute *ad7418_attrs[] = {
211 &sensor_dev_attr_temp1_max.dev_attr.attr,
212 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
213 &sensor_dev_attr_temp1_input.dev_attr.attr,
214 &sensor_dev_attr_in1_input.dev_attr.attr,
217 ATTRIBUTE_GROUPS(ad7418);
219 static void ad7418_init_client(struct i2c_client *client)
221 struct ad7418_data *data = i2c_get_clientdata(client);
223 int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
225 dev_err(&client->dev, "cannot read configuration register\n");
227 dev_info(&client->dev, "configuring for mode 1\n");
228 i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
230 if (data->type == ad7417 || data->type == ad7418)
231 i2c_smbus_write_byte_data(client,
232 AD7418_REG_CONF2, 0x00);
236 static int ad7418_probe(struct i2c_client *client,
237 const struct i2c_device_id *id)
239 struct device *dev = &client->dev;
240 struct i2c_adapter *adapter = client->adapter;
241 struct ad7418_data *data;
242 struct device *hwmon_dev;
243 const struct attribute_group **attr_groups = NULL;
245 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
246 I2C_FUNC_SMBUS_WORD_DATA))
249 data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL);
253 i2c_set_clientdata(client, data);
255 mutex_init(&data->lock);
256 data->client = client;
258 data->type = (enum chips)of_device_get_match_data(dev);
260 data->type = id->driver_data;
262 switch (data->type) {
265 attr_groups = ad7416_groups;
270 attr_groups = ad7417_groups;
275 attr_groups = ad7418_groups;
279 dev_info(dev, "%s chip found\n", client->name);
281 /* Initialize the AD7418 chip */
282 ad7418_init_client(client);
284 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
287 return PTR_ERR_OR_ZERO(hwmon_dev);
290 static const struct i2c_device_id ad7418_id[] = {
291 { "ad7416", ad7416 },
292 { "ad7417", ad7417 },
293 { "ad7418", ad7418 },
296 MODULE_DEVICE_TABLE(i2c, ad7418_id);
298 static const struct of_device_id ad7418_dt_ids[] = {
299 { .compatible = "adi,ad7416", .data = (void *)ad7416, },
300 { .compatible = "adi,ad7417", .data = (void *)ad7417, },
301 { .compatible = "adi,ad7418", .data = (void *)ad7418, },
304 MODULE_DEVICE_TABLE(of, ad7418_dt_ids);
306 static struct i2c_driver ad7418_driver = {
309 .of_match_table = ad7418_dt_ids,
311 .probe = ad7418_probe,
312 .id_table = ad7418_id,
315 module_i2c_driver(ad7418_driver);
317 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
318 MODULE_DESCRIPTION("AD7416/17/18 driver");
319 MODULE_LICENSE("GPL");
320 MODULE_VERSION(DRV_VERSION);