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/delay.h>
23 #include <linux/slab.h>
27 #define DRV_VERSION "0.4"
29 enum chips { ad7416, ad7417, ad7418 };
31 /* AD7418 registers */
32 #define AD7418_REG_TEMP_IN 0x00
33 #define AD7418_REG_CONF 0x01
34 #define AD7418_REG_TEMP_HYST 0x02
35 #define AD7418_REG_TEMP_OS 0x03
36 #define AD7418_REG_ADC 0x04
37 #define AD7418_REG_CONF2 0x05
39 #define AD7418_REG_ADC_CH(x) ((x) << 5)
40 #define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
42 static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
47 struct device *hwmon_dev;
48 struct attribute_group attrs;
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_probe(struct i2c_client *client,
59 const struct i2c_device_id *id);
60 static int ad7418_remove(struct i2c_client *client);
62 static const struct i2c_device_id ad7418_id[] = {
68 MODULE_DEVICE_TABLE(i2c, ad7418_id);
70 static struct i2c_driver ad7418_driver = {
74 .probe = ad7418_probe,
75 .remove = ad7418_remove,
76 .id_table = ad7418_id,
79 /* All registers are word-sized, except for the configuration registers.
80 * AD7418 uses a high-byte first convention. Do NOT use those functions to
81 * access the configuration registers CONF and CONF2, as they are byte-sized.
83 static inline int ad7418_read(struct i2c_client *client, u8 reg)
85 return swab16(i2c_smbus_read_word_data(client, reg));
88 static inline int ad7418_write(struct i2c_client *client, u8 reg, u16 value)
90 return i2c_smbus_write_word_data(client, reg, swab16(value));
93 static void ad7418_init_client(struct i2c_client *client)
95 struct ad7418_data *data = i2c_get_clientdata(client);
97 int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
99 dev_err(&client->dev, "cannot read configuration register\n");
101 dev_info(&client->dev, "configuring for mode 1\n");
102 i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
104 if (data->type == ad7417 || data->type == ad7418)
105 i2c_smbus_write_byte_data(client,
106 AD7418_REG_CONF2, 0x00);
110 static struct ad7418_data *ad7418_update_device(struct device *dev)
112 struct i2c_client *client = to_i2c_client(dev);
113 struct ad7418_data *data = i2c_get_clientdata(client);
115 mutex_lock(&data->lock);
117 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
122 /* read config register and clear channel bits */
123 cfg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
126 i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
127 cfg | AD7418_CH_TEMP);
130 for (i = 0; i < 3; i++) {
131 data->temp[i] = ad7418_read(client, AD7418_REG_TEMP[i]);
134 for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
135 i2c_smbus_write_byte_data(client,
137 cfg | AD7418_REG_ADC_CH(ch));
140 data->in[data->adc_max - 1 - i] =
141 ad7418_read(client, AD7418_REG_ADC);
144 /* restore old configuration value */
145 ad7418_write(client, AD7418_REG_CONF, cfg);
147 data->last_updated = jiffies;
151 mutex_unlock(&data->lock);
156 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
159 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
160 struct ad7418_data *data = ad7418_update_device(dev);
161 return sprintf(buf, "%d\n",
162 LM75_TEMP_FROM_REG(data->temp[attr->index]));
165 static ssize_t show_adc(struct device *dev, struct device_attribute *devattr,
168 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
169 struct ad7418_data *data = ad7418_update_device(dev);
171 return sprintf(buf, "%d\n",
172 ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
175 static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
176 const char *buf, size_t count)
178 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
179 struct i2c_client *client = to_i2c_client(dev);
180 struct ad7418_data *data = i2c_get_clientdata(client);
181 long temp = simple_strtol(buf, NULL, 10);
183 mutex_lock(&data->lock);
184 data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
185 ad7418_write(client, AD7418_REG_TEMP[attr->index], data->temp[attr->index]);
186 mutex_unlock(&data->lock);
190 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
191 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
192 show_temp, set_temp, 1);
193 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
194 show_temp, set_temp, 2);
196 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0);
197 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
198 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
199 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
201 static struct attribute *ad7416_attributes[] = {
202 &sensor_dev_attr_temp1_max.dev_attr.attr,
203 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
204 &sensor_dev_attr_temp1_input.dev_attr.attr,
208 static struct attribute *ad7417_attributes[] = {
209 &sensor_dev_attr_temp1_max.dev_attr.attr,
210 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
211 &sensor_dev_attr_temp1_input.dev_attr.attr,
212 &sensor_dev_attr_in1_input.dev_attr.attr,
213 &sensor_dev_attr_in2_input.dev_attr.attr,
214 &sensor_dev_attr_in3_input.dev_attr.attr,
215 &sensor_dev_attr_in4_input.dev_attr.attr,
219 static struct attribute *ad7418_attributes[] = {
220 &sensor_dev_attr_temp1_max.dev_attr.attr,
221 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
222 &sensor_dev_attr_temp1_input.dev_attr.attr,
223 &sensor_dev_attr_in1_input.dev_attr.attr,
227 static int ad7418_probe(struct i2c_client *client,
228 const struct i2c_device_id *id)
230 struct i2c_adapter *adapter = client->adapter;
231 struct ad7418_data *data;
234 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
235 I2C_FUNC_SMBUS_WORD_DATA)) {
240 if (!(data = kzalloc(sizeof(struct ad7418_data), GFP_KERNEL))) {
245 i2c_set_clientdata(client, data);
247 mutex_init(&data->lock);
248 data->type = id->driver_data;
250 switch (data->type) {
253 data->attrs.attrs = ad7416_attributes;
258 data->attrs.attrs = ad7417_attributes;
263 data->attrs.attrs = ad7418_attributes;
267 dev_info(&client->dev, "%s chip found\n", client->name);
269 /* Initialize the AD7418 chip */
270 ad7418_init_client(client);
272 /* Register sysfs hooks */
273 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
276 data->hwmon_dev = hwmon_device_register(&client->dev);
277 if (IS_ERR(data->hwmon_dev)) {
278 err = PTR_ERR(data->hwmon_dev);
285 sysfs_remove_group(&client->dev.kobj, &data->attrs);
292 static int ad7418_remove(struct i2c_client *client)
294 struct ad7418_data *data = i2c_get_clientdata(client);
295 hwmon_device_unregister(data->hwmon_dev);
296 sysfs_remove_group(&client->dev.kobj, &data->attrs);
301 static int __init ad7418_init(void)
303 return i2c_add_driver(&ad7418_driver);
306 static void __exit ad7418_exit(void)
308 i2c_del_driver(&ad7418_driver);
311 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
312 MODULE_DESCRIPTION("AD7416/17/18 driver");
313 MODULE_LICENSE("GPL");
314 MODULE_VERSION(DRV_VERSION);
316 module_init(ad7418_init);
317 module_exit(ad7418_exit);