blob: f97b5b356875b77857c0b69ce77d94b33a608ba7 [file] [log] [blame]
Alessandro Zummo2d8dd652007-05-08 17:22:02 +02001/*
2 * An hwmon driver for the Analog Devices AD7416/17/18
3 * Copyright (C) 2006-07 Tower Technologies
4 *
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * Based on lm75.c
8 * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
9 *
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.
13 */
14
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
24#include "lm75.h"
25
Jean Delvare369932f2008-07-16 19:30:08 +020026#define DRV_VERSION "0.4"
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020027
Jean Delvare369932f2008-07-16 19:30:08 +020028enum chips { ad7416, ad7417, ad7418 };
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020029
30/* AD7418 registers */
31#define AD7418_REG_TEMP_IN 0x00
32#define AD7418_REG_CONF 0x01
33#define AD7418_REG_TEMP_HYST 0x02
34#define AD7418_REG_TEMP_OS 0x03
35#define AD7418_REG_ADC 0x04
36#define AD7418_REG_CONF2 0x05
37
38#define AD7418_REG_ADC_CH(x) ((x) << 5)
39#define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
40
41static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
42 AD7418_REG_TEMP_HYST,
43 AD7418_REG_TEMP_OS };
44
45struct ad7418_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070046 struct device *hwmon_dev;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020047 struct attribute_group attrs;
48 enum chips type;
49 struct mutex lock;
50 int adc_max; /* number of ADC channels */
51 char valid;
52 unsigned long last_updated; /* In jiffies */
53 s16 temp[3]; /* Register values */
54 u16 in[4];
55};
56
Jean Delvare369932f2008-07-16 19:30:08 +020057static int ad7418_probe(struct i2c_client *client,
58 const struct i2c_device_id *id);
59static int ad7418_remove(struct i2c_client *client);
60
61static const struct i2c_device_id ad7418_id[] = {
62 { "ad7416", ad7416 },
63 { "ad7417", ad7417 },
64 { "ad7418", ad7418 },
65 { }
66};
67MODULE_DEVICE_TABLE(i2c, ad7418_id);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020068
69static struct i2c_driver ad7418_driver = {
70 .driver = {
71 .name = "ad7418",
72 },
Jean Delvare369932f2008-07-16 19:30:08 +020073 .probe = ad7418_probe,
74 .remove = ad7418_remove,
75 .id_table = ad7418_id,
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020076};
77
78/* All registers are word-sized, except for the configuration registers.
79 * AD7418 uses a high-byte first convention. Do NOT use those functions to
80 * access the configuration registers CONF and CONF2, as they are byte-sized.
81 */
82static inline int ad7418_read(struct i2c_client *client, u8 reg)
83{
84 return swab16(i2c_smbus_read_word_data(client, reg));
85}
86
87static inline int ad7418_write(struct i2c_client *client, u8 reg, u16 value)
88{
89 return i2c_smbus_write_word_data(client, reg, swab16(value));
90}
91
92static void ad7418_init_client(struct i2c_client *client)
93{
94 struct ad7418_data *data = i2c_get_clientdata(client);
95
96 int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
97 if (reg < 0) {
98 dev_err(&client->dev, "cannot read configuration register\n");
99 } else {
100 dev_info(&client->dev, "configuring for mode 1\n");
101 i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
102
103 if (data->type == ad7417 || data->type == ad7418)
104 i2c_smbus_write_byte_data(client,
105 AD7418_REG_CONF2, 0x00);
106 }
107}
108
109static struct ad7418_data *ad7418_update_device(struct device *dev)
110{
111 struct i2c_client *client = to_i2c_client(dev);
112 struct ad7418_data *data = i2c_get_clientdata(client);
113
114 mutex_lock(&data->lock);
115
116 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
117 || !data->valid) {
118 u8 cfg;
119 int i, ch;
120
121 /* read config register and clear channel bits */
122 cfg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
123 cfg &= 0x1F;
124
125 i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
126 cfg | AD7418_CH_TEMP);
127 udelay(30);
128
129 for (i = 0; i < 3; i++) {
130 data->temp[i] = ad7418_read(client, AD7418_REG_TEMP[i]);
131 }
132
133 for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
134 i2c_smbus_write_byte_data(client,
135 AD7418_REG_CONF,
136 cfg | AD7418_REG_ADC_CH(ch));
137
138 udelay(15);
139 data->in[data->adc_max - 1 - i] =
140 ad7418_read(client, AD7418_REG_ADC);
141 }
142
143 /* restore old configuration value */
144 ad7418_write(client, AD7418_REG_CONF, cfg);
145
146 data->last_updated = jiffies;
147 data->valid = 1;
148 }
149
150 mutex_unlock(&data->lock);
151
152 return data;
153}
154
155static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
156 char *buf)
157{
158 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
159 struct ad7418_data *data = ad7418_update_device(dev);
160 return sprintf(buf, "%d\n",
161 LM75_TEMP_FROM_REG(data->temp[attr->index]));
162}
163
164static ssize_t show_adc(struct device *dev, struct device_attribute *devattr,
165 char *buf)
166{
167 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
168 struct ad7418_data *data = ad7418_update_device(dev);
169
170 return sprintf(buf, "%d\n",
171 ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
172}
173
174static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
175 const char *buf, size_t count)
176{
177 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
178 struct i2c_client *client = to_i2c_client(dev);
179 struct ad7418_data *data = i2c_get_clientdata(client);
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200180 long temp = simple_strtol(buf, NULL, 10);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200181
182 mutex_lock(&data->lock);
183 data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
184 ad7418_write(client, AD7418_REG_TEMP[attr->index], data->temp[attr->index]);
185 mutex_unlock(&data->lock);
186 return count;
187}
188
189static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
190static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
191 show_temp, set_temp, 1);
192static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
193 show_temp, set_temp, 2);
194
195static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0);
196static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
197static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
198static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
199
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200200static struct attribute *ad7416_attributes[] = {
201 &sensor_dev_attr_temp1_max.dev_attr.attr,
202 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
203 &sensor_dev_attr_temp1_input.dev_attr.attr,
204 NULL
205};
206
207static struct attribute *ad7417_attributes[] = {
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,
212 &sensor_dev_attr_in2_input.dev_attr.attr,
213 &sensor_dev_attr_in3_input.dev_attr.attr,
214 &sensor_dev_attr_in4_input.dev_attr.attr,
215 NULL
216};
217
218static struct attribute *ad7418_attributes[] = {
219 &sensor_dev_attr_temp1_max.dev_attr.attr,
220 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
221 &sensor_dev_attr_temp1_input.dev_attr.attr,
222 &sensor_dev_attr_in1_input.dev_attr.attr,
223 NULL
224};
225
Jean Delvare369932f2008-07-16 19:30:08 +0200226static int ad7418_probe(struct i2c_client *client,
227 const struct i2c_device_id *id)
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200228{
Jean Delvare369932f2008-07-16 19:30:08 +0200229 struct i2c_adapter *adapter = client->adapter;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200230 struct ad7418_data *data;
Jean Delvare369932f2008-07-16 19:30:08 +0200231 int err;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200232
233 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
Jean Delvare369932f2008-07-16 19:30:08 +0200234 I2C_FUNC_SMBUS_WORD_DATA)) {
235 err = -EOPNOTSUPP;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200236 goto exit;
Jean Delvare369932f2008-07-16 19:30:08 +0200237 }
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200238
239 if (!(data = kzalloc(sizeof(struct ad7418_data), GFP_KERNEL))) {
240 err = -ENOMEM;
241 goto exit;
242 }
243
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200244 i2c_set_clientdata(client, data);
245
246 mutex_init(&data->lock);
Jean Delvare369932f2008-07-16 19:30:08 +0200247 data->type = id->driver_data;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200248
249 switch (data->type) {
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200250 case ad7416:
251 data->adc_max = 0;
252 data->attrs.attrs = ad7416_attributes;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200253 break;
254
255 case ad7417:
256 data->adc_max = 4;
257 data->attrs.attrs = ad7417_attributes;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200258 break;
259
260 case ad7418:
261 data->adc_max = 1;
262 data->attrs.attrs = ad7418_attributes;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200263 break;
264 }
265
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200266 dev_info(&client->dev, "%s chip found\n", client->name);
267
268 /* Initialize the AD7418 chip */
269 ad7418_init_client(client);
270
271 /* Register sysfs hooks */
272 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
Jean Delvare369932f2008-07-16 19:30:08 +0200273 goto exit_free;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200274
Tony Jones1beeffe2007-08-20 13:46:20 -0700275 data->hwmon_dev = hwmon_device_register(&client->dev);
276 if (IS_ERR(data->hwmon_dev)) {
277 err = PTR_ERR(data->hwmon_dev);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200278 goto exit_remove;
279 }
280
281 return 0;
282
283exit_remove:
284 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200285exit_free:
286 kfree(data);
287exit:
288 return err;
289}
290
Jean Delvare369932f2008-07-16 19:30:08 +0200291static int ad7418_remove(struct i2c_client *client)
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200292{
293 struct ad7418_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -0700294 hwmon_device_unregister(data->hwmon_dev);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200295 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200296 kfree(data);
297 return 0;
298}
299
300static int __init ad7418_init(void)
301{
302 return i2c_add_driver(&ad7418_driver);
303}
304
305static void __exit ad7418_exit(void)
306{
307 i2c_del_driver(&ad7418_driver);
308}
309
310MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
311MODULE_DESCRIPTION("AD7416/17/18 driver");
312MODULE_LICENSE("GPL");
313MODULE_VERSION(DRV_VERSION);
314
315module_init(ad7418_init);
316module_exit(ad7418_exit);