blob: fa02f20b79ff4e1ea1b93911f04eb23ccd7a0963 [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 */
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010054};
55
56static s32 ads1015_read_reg(struct i2c_client *client, unsigned int reg)
57{
58 s32 data = i2c_smbus_read_word_data(client, reg);
59
60 return (data < 0) ? data : swab16(data);
61}
62
63static s32 ads1015_write_reg(struct i2c_client *client, unsigned int reg,
64 u16 val)
65{
66 return i2c_smbus_write_word_data(client, reg, swab16(val));
67}
68
69static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
70 int *value)
71{
72 u16 config;
73 s16 conversion;
74 unsigned int pga;
75 int fullscale;
76 unsigned int k;
77 struct ads1015_data *data = i2c_get_clientdata(client);
78 int res;
79
80 mutex_lock(&data->update_lock);
81
82 /* get fullscale voltage */
83 res = ads1015_read_reg(client, ADS1015_CONFIG);
84 if (res < 0)
85 goto err_unlock;
86 config = res;
87 pga = (config >> 9) & 0x0007;
88 fullscale = fullscale_table[pga];
89
90 /* set channel and start single conversion */
91 config &= ~(0x0007 << 12);
92 config |= (1 << 15) | (1 << 8) | (channel & 0x0007) << 12;
93
94 /* wait until conversion finished */
95 res = ads1015_write_reg(client, ADS1015_CONFIG, config);
96 if (res < 0)
97 goto err_unlock;
98 for (k = 0; k < 5; ++k) {
99 msleep(1);
100 res = ads1015_read_reg(client, ADS1015_CONFIG);
101 if (res < 0)
102 goto err_unlock;
103 config = res;
104 if (config & (1 << 15))
105 break;
106 }
107 if (k == 5) {
108 res = -EIO;
109 goto err_unlock;
110 }
111
112 res = ads1015_read_reg(client, ADS1015_CONVERSION);
113 if (res < 0)
114 goto err_unlock;
115 conversion = res;
116
117 mutex_unlock(&data->update_lock);
118
119 *value = DIV_ROUND_CLOSEST(conversion * fullscale, 0x7ff0);
120
121 return 0;
122
123err_unlock:
124 mutex_unlock(&data->update_lock);
125 return res;
126}
127
128/* sysfs callback function */
129static ssize_t show_in(struct device *dev, struct device_attribute *da,
130 char *buf)
131{
132 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
133 struct i2c_client *client = to_i2c_client(dev);
134 int in;
135 int res;
136
137 res = ads1015_read_value(client, attr->index, &in);
138
139 return (res < 0) ? res : sprintf(buf, "%d\n", in);
140}
141
Jean Delvarefdf241a2011-03-21 17:59:37 +0100142static const struct sensor_device_attribute ads1015_in[] = {
143 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
144 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
145 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
146 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
147 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
148 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
149 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
150 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100151};
152
153/*
154 * Driver interface
155 */
156
157static int ads1015_remove(struct i2c_client *client)
158{
159 struct ads1015_data *data = i2c_get_clientdata(client);
Jean Delvarefdf241a2011-03-21 17:59:37 +0100160 int k;
161
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100162 hwmon_device_unregister(data->hwmon_dev);
Jean Delvarefdf241a2011-03-21 17:59:37 +0100163 for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k)
164 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100165 kfree(data);
166 return 0;
167}
168
169static unsigned int ads1015_get_exported_channels(struct i2c_client *client)
170{
171 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
172#ifdef CONFIG_OF
173 struct device_node *np = client->dev.of_node;
174 const __be32 *of_channels;
175 int of_channels_size;
176#endif
177
178 /* prefer platform data */
179 if (pdata)
180 return pdata->exported_channels;
181
182#ifdef CONFIG_OF
183 /* fallback on OF */
184 of_channels = of_get_property(np, "exported-channels",
185 &of_channels_size);
186 if (of_channels && (of_channels_size == sizeof(*of_channels)))
187 return be32_to_cpup(of_channels);
188#endif
189
190 /* fallback on default configuration */
191 return ADS1015_DEFAULT_CHANNELS;
192}
193
194static int ads1015_probe(struct i2c_client *client,
195 const struct i2c_device_id *id)
196{
197 struct ads1015_data *data;
198 int err;
199 unsigned int exported_channels;
200 unsigned int k;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100201
202 data = kzalloc(sizeof(struct ads1015_data), GFP_KERNEL);
203 if (!data) {
204 err = -ENOMEM;
205 goto exit;
206 }
207
208 i2c_set_clientdata(client, data);
209 mutex_init(&data->update_lock);
210
211 /* build sysfs attribute group */
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100212 exported_channels = ads1015_get_exported_channels(client);
213 for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k) {
214 if (!(exported_channels & (1<<k)))
215 continue;
Jean Delvarefdf241a2011-03-21 17:59:37 +0100216 err = device_create_file(&client->dev, &ads1015_in[k].dev_attr);
217 if (err)
218 goto exit_free;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100219 }
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100220
221 data->hwmon_dev = hwmon_device_register(&client->dev);
222 if (IS_ERR(data->hwmon_dev)) {
223 err = PTR_ERR(data->hwmon_dev);
224 goto exit_remove;
225 }
226
227 return 0;
228
229exit_remove:
Jean Delvarefdf241a2011-03-21 17:59:37 +0100230 for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k)
231 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100232exit_free:
233 kfree(data);
234exit:
235 return err;
236}
237
238static const struct i2c_device_id ads1015_id[] = {
239 { "ads1015", 0 },
240 { }
241};
242MODULE_DEVICE_TABLE(i2c, ads1015_id);
243
244static struct i2c_driver ads1015_driver = {
245 .driver = {
246 .name = "ads1015",
247 },
248 .probe = ads1015_probe,
249 .remove = ads1015_remove,
250 .id_table = ads1015_id,
251};
252
253static int __init sensors_ads1015_init(void)
254{
255 return i2c_add_driver(&ads1015_driver);
256}
257
258static void __exit sensors_ads1015_exit(void)
259{
260 i2c_del_driver(&ads1015_driver);
261}
262
263MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
264MODULE_DESCRIPTION("ADS1015 driver");
265MODULE_LICENSE("GPL");
266
267module_init(sensors_ads1015_init);
268module_exit(sensors_ads1015_exit);