blob: 22e0c926989dca8dfe2644418049d56ae99ef2d9 [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
Dirk Eibachc0046862011-03-21 17:59:37 +010048/* Data rates in samples per second */
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040049static const unsigned int data_rate_table_1015[8] = {
50 128, 250, 490, 920, 1600, 2400, 3300, 3300
51};
52
53static const unsigned int data_rate_table_1115[8] = {
54 8, 16, 32, 64, 128, 250, 475, 860
55};
Dirk Eibachc0046862011-03-21 17:59:37 +010056
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010057#define ADS1015_DEFAULT_CHANNELS 0xff
Dirk Eibachc0046862011-03-21 17:59:37 +010058#define ADS1015_DEFAULT_PGA 2
59#define ADS1015_DEFAULT_DATA_RATE 4
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010060
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040061enum ads1015_chips {
62 ads1015,
63 ads1115,
64};
65
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010066struct ads1015_data {
67 struct device *hwmon_dev;
68 struct mutex update_lock; /* mutex protect updates */
Dirk Eibachc0046862011-03-21 17:59:37 +010069 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040070 enum ads1015_chips id;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010071};
72
Guenter Roeck1196573f2012-04-09 13:53:00 -040073static int ads1015_read_adc(struct i2c_client *client, unsigned int channel)
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010074{
75 u16 config;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010076 struct ads1015_data *data = i2c_get_clientdata(client);
Dirk Eibachc0046862011-03-21 17:59:37 +010077 unsigned int pga = data->channel_data[channel].pga;
Dirk Eibachc0046862011-03-21 17:59:37 +010078 unsigned int data_rate = data->channel_data[channel].data_rate;
79 unsigned int conversion_time_ms;
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040080 const unsigned int * const rate_table = data->id == ads1115 ?
81 data_rate_table_1115 : data_rate_table_1015;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010082 int res;
83
84 mutex_lock(&data->update_lock);
85
Dirk Eibachc0046862011-03-21 17:59:37 +010086 /* get channel parameters */
Jean Delvare90f41022011-11-04 12:00:47 +010087 res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010088 if (res < 0)
89 goto err_unlock;
90 config = res;
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040091 conversion_time_ms = DIV_ROUND_UP(1000, rate_table[data_rate]);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010092
Dirk Eibachc0046862011-03-21 17:59:37 +010093 /* setup and start single conversion */
94 config &= 0x001f;
95 config |= (1 << 15) | (1 << 8);
96 config |= (channel & 0x0007) << 12;
97 config |= (pga & 0x0007) << 9;
98 config |= (data_rate & 0x0007) << 5;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010099
Jean Delvare90f41022011-11-04 12:00:47 +0100100 res = i2c_smbus_write_word_swapped(client, ADS1015_CONFIG, config);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100101 if (res < 0)
102 goto err_unlock;
Dirk Eibachc0046862011-03-21 17:59:37 +0100103
104 /* wait until conversion finished */
105 msleep(conversion_time_ms);
Jean Delvare90f41022011-11-04 12:00:47 +0100106 res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
Dirk Eibachc0046862011-03-21 17:59:37 +0100107 if (res < 0)
108 goto err_unlock;
109 config = res;
110 if (!(config & (1 << 15))) {
111 /* conversion not finished in time */
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100112 res = -EIO;
113 goto err_unlock;
114 }
115
Jean Delvare90f41022011-11-04 12:00:47 +0100116 res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100117
118err_unlock:
119 mutex_unlock(&data->update_lock);
120 return res;
121}
122
Guenter Roeck1196573f2012-04-09 13:53:00 -0400123static int ads1015_reg_to_mv(struct i2c_client *client, unsigned int channel,
124 s16 reg)
125{
126 struct ads1015_data *data = i2c_get_clientdata(client);
127 unsigned int pga = data->channel_data[channel].pga;
128 int fullscale = fullscale_table[pga];
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +0400129 const unsigned mask = data->id == ads1115 ? 0x7fff : 0x7ff0;
Guenter Roeck1196573f2012-04-09 13:53:00 -0400130
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +0400131 return DIV_ROUND_CLOSEST(reg * fullscale, mask);
Guenter Roeck1196573f2012-04-09 13:53:00 -0400132}
133
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100134/* sysfs callback function */
135static ssize_t show_in(struct device *dev, struct device_attribute *da,
136 char *buf)
137{
138 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
139 struct i2c_client *client = to_i2c_client(dev);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100140 int res;
Guenter Roeck1196573f2012-04-09 13:53:00 -0400141 int index = attr->index;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100142
Guenter Roeck1196573f2012-04-09 13:53:00 -0400143 res = ads1015_read_adc(client, index);
144 if (res < 0)
145 return res;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100146
Guenter Roeck1196573f2012-04-09 13:53:00 -0400147 return sprintf(buf, "%d\n", ads1015_reg_to_mv(client, index, res));
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100148}
149
Jean Delvarefdf241a2011-03-21 17:59:37 +0100150static const struct sensor_device_attribute ads1015_in[] = {
151 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
152 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
153 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
154 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
155 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
156 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
157 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
158 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100159};
160
161/*
162 * Driver interface
163 */
164
165static int ads1015_remove(struct i2c_client *client)
166{
167 struct ads1015_data *data = i2c_get_clientdata(client);
Jean Delvarefdf241a2011-03-21 17:59:37 +0100168 int k;
169
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100170 hwmon_device_unregister(data->hwmon_dev);
Dirk Eibachc0046862011-03-21 17:59:37 +0100171 for (k = 0; k < ADS1015_CHANNELS; ++k)
Jean Delvarefdf241a2011-03-21 17:59:37 +0100172 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100173 return 0;
174}
175
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100176#ifdef CONFIG_OF
Dirk Eibachc0046862011-03-21 17:59:37 +0100177static int ads1015_get_channels_config_of(struct i2c_client *client)
178{
179 struct ads1015_data *data = i2c_get_clientdata(client);
180 struct device_node *node;
181
182 if (!client->dev.of_node
183 || !of_get_next_child(client->dev.of_node, NULL))
184 return -EINVAL;
185
186 for_each_child_of_node(client->dev.of_node, node) {
187 const __be32 *property;
188 int len;
189 unsigned int channel;
190 unsigned int pga = ADS1015_DEFAULT_PGA;
191 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
192
193 property = of_get_property(node, "reg", &len);
194 if (!property || len != sizeof(int)) {
195 dev_err(&client->dev, "invalid reg on %s\n",
196 node->full_name);
197 continue;
198 }
199
200 channel = be32_to_cpup(property);
Axel Lin56de1372014-07-30 11:13:52 +0800201 if (channel >= ADS1015_CHANNELS) {
Dirk Eibachc0046862011-03-21 17:59:37 +0100202 dev_err(&client->dev,
203 "invalid channel index %d on %s\n",
204 channel, node->full_name);
205 continue;
206 }
207
208 property = of_get_property(node, "ti,gain", &len);
209 if (property && len == sizeof(int)) {
210 pga = be32_to_cpup(property);
211 if (pga > 6) {
212 dev_err(&client->dev,
213 "invalid gain on %s\n",
214 node->full_name);
215 }
216 }
217
218 property = of_get_property(node, "ti,datarate", &len);
219 if (property && len == sizeof(int)) {
220 data_rate = be32_to_cpup(property);
221 if (data_rate > 7) {
222 dev_err(&client->dev,
223 "invalid data_rate on %s\n",
224 node->full_name);
225 }
226 }
227
228 data->channel_data[channel].enabled = true;
229 data->channel_data[channel].pga = pga;
230 data->channel_data[channel].data_rate = data_rate;
231 }
232
233 return 0;
234}
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100235#endif
236
Dirk Eibachc0046862011-03-21 17:59:37 +0100237static void ads1015_get_channels_config(struct i2c_client *client)
238{
239 unsigned int k;
240 struct ads1015_data *data = i2c_get_clientdata(client);
241 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
242
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100243 /* prefer platform data */
Dirk Eibachc0046862011-03-21 17:59:37 +0100244 if (pdata) {
245 memcpy(data->channel_data, pdata->channel_data,
246 sizeof(data->channel_data));
247 return;
248 }
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100249
250#ifdef CONFIG_OF
Dirk Eibachc0046862011-03-21 17:59:37 +0100251 if (!ads1015_get_channels_config_of(client))
252 return;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100253#endif
254
255 /* fallback on default configuration */
Dirk Eibachc0046862011-03-21 17:59:37 +0100256 for (k = 0; k < ADS1015_CHANNELS; ++k) {
257 data->channel_data[k].enabled = true;
258 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
259 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
260 }
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100261}
262
263static int ads1015_probe(struct i2c_client *client,
264 const struct i2c_device_id *id)
265{
266 struct ads1015_data *data;
267 int err;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100268 unsigned int k;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100269
Guenter Roeck57457e32012-06-02 09:58:00 -0700270 data = devm_kzalloc(&client->dev, sizeof(struct ads1015_data),
271 GFP_KERNEL);
272 if (!data)
273 return -ENOMEM;
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +0400274 data->id = id->driver_data;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100275 i2c_set_clientdata(client, data);
276 mutex_init(&data->update_lock);
277
278 /* build sysfs attribute group */
Dirk Eibachc0046862011-03-21 17:59:37 +0100279 ads1015_get_channels_config(client);
280 for (k = 0; k < ADS1015_CHANNELS; ++k) {
281 if (!data->channel_data[k].enabled)
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100282 continue;
Jean Delvarefdf241a2011-03-21 17:59:37 +0100283 err = device_create_file(&client->dev, &ads1015_in[k].dev_attr);
284 if (err)
Guenter Roeck363434b2012-02-22 08:13:52 -0800285 goto exit_remove;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100286 }
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100287
288 data->hwmon_dev = hwmon_device_register(&client->dev);
289 if (IS_ERR(data->hwmon_dev)) {
290 err = PTR_ERR(data->hwmon_dev);
291 goto exit_remove;
292 }
293
294 return 0;
295
296exit_remove:
Dirk Eibachc0046862011-03-21 17:59:37 +0100297 for (k = 0; k < ADS1015_CHANNELS; ++k)
Jean Delvarefdf241a2011-03-21 17:59:37 +0100298 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100299 return err;
300}
301
302static const struct i2c_device_id ads1015_id[] = {
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +0400303 { "ads1015", ads1015},
304 { "ads1115", ads1115},
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100305 { }
306};
307MODULE_DEVICE_TABLE(i2c, ads1015_id);
308
309static struct i2c_driver ads1015_driver = {
310 .driver = {
311 .name = "ads1015",
312 },
313 .probe = ads1015_probe,
314 .remove = ads1015_remove,
315 .id_table = ads1015_id,
316};
317
Axel Linf0967ee2012-01-20 15:38:18 +0800318module_i2c_driver(ads1015_driver);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100319
320MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
321MODULE_DESCRIPTION("ADS1015 driver");
322MODULE_LICENSE("GPL");