blob: 9e1585c3152e39d90b925ad4dd61dbc920b44cf6 [file] [log] [blame]
Dirk Eibach8c22a8f2011-03-21 17:59:36 +01001/*
2 * ads1015.c - lm_sensors driver for ads1015 12-bit 4-input ADC
3 * (C) Copyright 2010
4 * Dirk Eibach, Guntermann & Drunck GmbH <eibach@gdsys.de>
5 *
6 * Based on the ads7828 driver by Steve Hardy.
7 *
8 * Datasheet available at: http://focus.ti.com/lit/ds/symlink/ads1015.pdf
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 as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/delay.h>
29#include <linux/i2c.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/err.h>
33#include <linux/mutex.h>
34#include <linux/of.h>
35
36#include <linux/i2c/ads1015.h>
37
38/* ADS1015 registers */
39enum {
40 ADS1015_CONVERSION = 0,
41 ADS1015_CONFIG = 1,
42};
43
44/* PGA fullscale voltages in mV */
45static const unsigned int fullscale_table[8] = {
46 6144, 4096, 2048, 1024, 512, 256, 256, 256 };
47
48#define ADS1015_CONFIG_CHANNELS 8
49#define ADS1015_DEFAULT_CHANNELS 0xff
50
51struct ads1015_data {
52 struct device *hwmon_dev;
53 struct mutex update_lock; /* mutex protect updates */
54 struct attribute *attr_table[ADS1015_CONFIG_CHANNELS + 1];
55 struct attribute_group attr_group;
56};
57
58static s32 ads1015_read_reg(struct i2c_client *client, unsigned int reg)
59{
60 s32 data = i2c_smbus_read_word_data(client, reg);
61
62 return (data < 0) ? data : swab16(data);
63}
64
65static s32 ads1015_write_reg(struct i2c_client *client, unsigned int reg,
66 u16 val)
67{
68 return i2c_smbus_write_word_data(client, reg, swab16(val));
69}
70
71static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
72 int *value)
73{
74 u16 config;
75 s16 conversion;
76 unsigned int pga;
77 int fullscale;
78 unsigned int k;
79 struct ads1015_data *data = i2c_get_clientdata(client);
80 int res;
81
82 mutex_lock(&data->update_lock);
83
84 /* get fullscale voltage */
85 res = ads1015_read_reg(client, ADS1015_CONFIG);
86 if (res < 0)
87 goto err_unlock;
88 config = res;
89 pga = (config >> 9) & 0x0007;
90 fullscale = fullscale_table[pga];
91
92 /* set channel and start single conversion */
93 config &= ~(0x0007 << 12);
94 config |= (1 << 15) | (1 << 8) | (channel & 0x0007) << 12;
95
96 /* wait until conversion finished */
97 res = ads1015_write_reg(client, ADS1015_CONFIG, config);
98 if (res < 0)
99 goto err_unlock;
100 for (k = 0; k < 5; ++k) {
101 msleep(1);
102 res = ads1015_read_reg(client, ADS1015_CONFIG);
103 if (res < 0)
104 goto err_unlock;
105 config = res;
106 if (config & (1 << 15))
107 break;
108 }
109 if (k == 5) {
110 res = -EIO;
111 goto err_unlock;
112 }
113
114 res = ads1015_read_reg(client, ADS1015_CONVERSION);
115 if (res < 0)
116 goto err_unlock;
117 conversion = res;
118
119 mutex_unlock(&data->update_lock);
120
121 *value = DIV_ROUND_CLOSEST(conversion * fullscale, 0x7ff0);
122
123 return 0;
124
125err_unlock:
126 mutex_unlock(&data->update_lock);
127 return res;
128}
129
130/* sysfs callback function */
131static ssize_t show_in(struct device *dev, struct device_attribute *da,
132 char *buf)
133{
134 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
135 struct i2c_client *client = to_i2c_client(dev);
136 int in;
137 int res;
138
139 res = ads1015_read_value(client, attr->index, &in);
140
141 return (res < 0) ? res : sprintf(buf, "%d\n", in);
142}
143
144#define in_reg(offset)\
145static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in,\
146 NULL, offset)
147
148in_reg(0);
149in_reg(1);
150in_reg(2);
151in_reg(3);
152in_reg(4);
153in_reg(5);
154in_reg(6);
155in_reg(7);
156
157static struct attribute *all_attributes[] = {
158 &sensor_dev_attr_in0_input.dev_attr.attr,
159 &sensor_dev_attr_in1_input.dev_attr.attr,
160 &sensor_dev_attr_in2_input.dev_attr.attr,
161 &sensor_dev_attr_in3_input.dev_attr.attr,
162 &sensor_dev_attr_in4_input.dev_attr.attr,
163 &sensor_dev_attr_in5_input.dev_attr.attr,
164 &sensor_dev_attr_in6_input.dev_attr.attr,
165 &sensor_dev_attr_in7_input.dev_attr.attr,
166};
167
168/*
169 * Driver interface
170 */
171
172static int ads1015_remove(struct i2c_client *client)
173{
174 struct ads1015_data *data = i2c_get_clientdata(client);
175 hwmon_device_unregister(data->hwmon_dev);
176 sysfs_remove_group(&client->dev.kobj, &data->attr_group);
177 kfree(data);
178 return 0;
179}
180
181static unsigned int ads1015_get_exported_channels(struct i2c_client *client)
182{
183 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
184#ifdef CONFIG_OF
185 struct device_node *np = client->dev.of_node;
186 const __be32 *of_channels;
187 int of_channels_size;
188#endif
189
190 /* prefer platform data */
191 if (pdata)
192 return pdata->exported_channels;
193
194#ifdef CONFIG_OF
195 /* fallback on OF */
196 of_channels = of_get_property(np, "exported-channels",
197 &of_channels_size);
198 if (of_channels && (of_channels_size == sizeof(*of_channels)))
199 return be32_to_cpup(of_channels);
200#endif
201
202 /* fallback on default configuration */
203 return ADS1015_DEFAULT_CHANNELS;
204}
205
206static int ads1015_probe(struct i2c_client *client,
207 const struct i2c_device_id *id)
208{
209 struct ads1015_data *data;
210 int err;
211 unsigned int exported_channels;
212 unsigned int k;
213 unsigned int n = 0;
214
215 data = kzalloc(sizeof(struct ads1015_data), GFP_KERNEL);
216 if (!data) {
217 err = -ENOMEM;
218 goto exit;
219 }
220
221 i2c_set_clientdata(client, data);
222 mutex_init(&data->update_lock);
223
224 /* build sysfs attribute group */
225 data->attr_group.attrs = data->attr_table;
226 exported_channels = ads1015_get_exported_channels(client);
227 for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k) {
228 if (!(exported_channels & (1<<k)))
229 continue;
230 data->attr_table[n++] = all_attributes[k];
231 }
232 err = sysfs_create_group(&client->dev.kobj, &data->attr_group);
233 if (err)
234 goto exit_free;
235
236 data->hwmon_dev = hwmon_device_register(&client->dev);
237 if (IS_ERR(data->hwmon_dev)) {
238 err = PTR_ERR(data->hwmon_dev);
239 goto exit_remove;
240 }
241
242 return 0;
243
244exit_remove:
245 sysfs_remove_group(&client->dev.kobj, &data->attr_group);
246exit_free:
247 kfree(data);
248exit:
249 return err;
250}
251
252static const struct i2c_device_id ads1015_id[] = {
253 { "ads1015", 0 },
254 { }
255};
256MODULE_DEVICE_TABLE(i2c, ads1015_id);
257
258static struct i2c_driver ads1015_driver = {
259 .driver = {
260 .name = "ads1015",
261 },
262 .probe = ads1015_probe,
263 .remove = ads1015_remove,
264 .id_table = ads1015_id,
265};
266
267static int __init sensors_ads1015_init(void)
268{
269 return i2c_add_driver(&ads1015_driver);
270}
271
272static void __exit sensors_ads1015_exit(void)
273{
274 i2c_del_driver(&ads1015_driver);
275}
276
277MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
278MODULE_DESCRIPTION("ADS1015 driver");
279MODULE_LICENSE("GPL");
280
281module_init(sensors_ads1015_init);
282module_exit(sensors_ads1015_exit);