blob: 98c704d3366afb3dfe6fbd9188695394be1eb852 [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>
Javier Martinez Canillasa1409862017-02-24 10:12:56 -030034#include <linux/of_device.h>
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010035#include <linux/of.h>
36
Wolfram Sang9010624c2017-05-21 22:34:39 +020037#include <linux/platform_data/ads1015.h>
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010038
39/* ADS1015 registers */
40enum {
41 ADS1015_CONVERSION = 0,
42 ADS1015_CONFIG = 1,
43};
44
45/* PGA fullscale voltages in mV */
46static const unsigned int fullscale_table[8] = {
47 6144, 4096, 2048, 1024, 512, 256, 256, 256 };
48
Dirk Eibachc0046862011-03-21 17:59:37 +010049/* Data rates in samples per second */
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040050static const unsigned int data_rate_table_1015[8] = {
51 128, 250, 490, 920, 1600, 2400, 3300, 3300
52};
53
54static const unsigned int data_rate_table_1115[8] = {
55 8, 16, 32, 64, 128, 250, 475, 860
56};
Dirk Eibachc0046862011-03-21 17:59:37 +010057
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010058#define ADS1015_DEFAULT_CHANNELS 0xff
Dirk Eibachc0046862011-03-21 17:59:37 +010059#define ADS1015_DEFAULT_PGA 2
60#define ADS1015_DEFAULT_DATA_RATE 4
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010061
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040062enum ads1015_chips {
63 ads1015,
64 ads1115,
65};
66
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010067struct ads1015_data {
68 struct device *hwmon_dev;
69 struct mutex update_lock; /* mutex protect updates */
Dirk Eibachc0046862011-03-21 17:59:37 +010070 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040071 enum ads1015_chips id;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010072};
73
Guenter Roeck1196573f2012-04-09 13:53:00 -040074static int ads1015_read_adc(struct i2c_client *client, unsigned int channel)
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010075{
76 u16 config;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010077 struct ads1015_data *data = i2c_get_clientdata(client);
Dirk Eibachc0046862011-03-21 17:59:37 +010078 unsigned int pga = data->channel_data[channel].pga;
Dirk Eibachc0046862011-03-21 17:59:37 +010079 unsigned int data_rate = data->channel_data[channel].data_rate;
80 unsigned int conversion_time_ms;
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040081 const unsigned int * const rate_table = data->id == ads1115 ?
82 data_rate_table_1115 : data_rate_table_1015;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010083 int res;
84
85 mutex_lock(&data->update_lock);
86
Dirk Eibachc0046862011-03-21 17:59:37 +010087 /* get channel parameters */
Jean Delvare90f41022011-11-04 12:00:47 +010088 res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010089 if (res < 0)
90 goto err_unlock;
91 config = res;
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +040092 conversion_time_ms = DIV_ROUND_UP(1000, rate_table[data_rate]);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +010093
Dirk Eibachc0046862011-03-21 17:59:37 +010094 /* setup and start single conversion */
95 config &= 0x001f;
96 config |= (1 << 15) | (1 << 8);
97 config |= (channel & 0x0007) << 12;
98 config |= (pga & 0x0007) << 9;
99 config |= (data_rate & 0x0007) << 5;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100100
Jean Delvare90f41022011-11-04 12:00:47 +0100101 res = i2c_smbus_write_word_swapped(client, ADS1015_CONFIG, config);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100102 if (res < 0)
103 goto err_unlock;
Dirk Eibachc0046862011-03-21 17:59:37 +0100104
105 /* wait until conversion finished */
106 msleep(conversion_time_ms);
Jean Delvare90f41022011-11-04 12:00:47 +0100107 res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
Dirk Eibachc0046862011-03-21 17:59:37 +0100108 if (res < 0)
109 goto err_unlock;
110 config = res;
111 if (!(config & (1 << 15))) {
112 /* conversion not finished in time */
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100113 res = -EIO;
114 goto err_unlock;
115 }
116
Jean Delvare90f41022011-11-04 12:00:47 +0100117 res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100118
119err_unlock:
120 mutex_unlock(&data->update_lock);
121 return res;
122}
123
Guenter Roeck1196573f2012-04-09 13:53:00 -0400124static int ads1015_reg_to_mv(struct i2c_client *client, unsigned int channel,
125 s16 reg)
126{
127 struct ads1015_data *data = i2c_get_clientdata(client);
128 unsigned int pga = data->channel_data[channel].pga;
129 int fullscale = fullscale_table[pga];
Peter Rosinacc14692016-02-18 14:07:52 +0100130 const int mask = data->id == ads1115 ? 0x7fff : 0x7ff0;
Guenter Roeck1196573f2012-04-09 13:53:00 -0400131
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +0400132 return DIV_ROUND_CLOSEST(reg * fullscale, mask);
Guenter Roeck1196573f2012-04-09 13:53:00 -0400133}
134
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100135/* sysfs callback function */
136static ssize_t show_in(struct device *dev, struct device_attribute *da,
137 char *buf)
138{
139 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
140 struct i2c_client *client = to_i2c_client(dev);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100141 int res;
Guenter Roeck1196573f2012-04-09 13:53:00 -0400142 int index = attr->index;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100143
Guenter Roeck1196573f2012-04-09 13:53:00 -0400144 res = ads1015_read_adc(client, index);
145 if (res < 0)
146 return res;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100147
Guenter Roeck1196573f2012-04-09 13:53:00 -0400148 return sprintf(buf, "%d\n", ads1015_reg_to_mv(client, index, res));
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100149}
150
Jean Delvarefdf241a2011-03-21 17:59:37 +0100151static const struct sensor_device_attribute ads1015_in[] = {
152 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
153 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
154 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
155 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
156 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
157 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
158 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
159 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100160};
161
162/*
163 * Driver interface
164 */
165
166static int ads1015_remove(struct i2c_client *client)
167{
168 struct ads1015_data *data = i2c_get_clientdata(client);
Jean Delvarefdf241a2011-03-21 17:59:37 +0100169 int k;
170
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100171 hwmon_device_unregister(data->hwmon_dev);
Dirk Eibachc0046862011-03-21 17:59:37 +0100172 for (k = 0; k < ADS1015_CHANNELS; ++k)
Jean Delvarefdf241a2011-03-21 17:59:37 +0100173 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100174 return 0;
175}
176
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100177#ifdef CONFIG_OF
Dirk Eibachc0046862011-03-21 17:59:37 +0100178static int ads1015_get_channels_config_of(struct i2c_client *client)
179{
180 struct ads1015_data *data = i2c_get_clientdata(client);
181 struct device_node *node;
182
183 if (!client->dev.of_node
184 || !of_get_next_child(client->dev.of_node, NULL))
185 return -EINVAL;
186
187 for_each_child_of_node(client->dev.of_node, node) {
Axel Lin8e357622014-08-05 10:56:47 +0800188 u32 pval;
Dirk Eibachc0046862011-03-21 17:59:37 +0100189 unsigned int channel;
190 unsigned int pga = ADS1015_DEFAULT_PGA;
191 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
192
Axel Lin8e357622014-08-05 10:56:47 +0800193 if (of_property_read_u32(node, "reg", &pval)) {
Rob Herringbb923fd2017-07-18 16:43:05 -0500194 dev_err(&client->dev, "invalid reg on %pOF\n", node);
Dirk Eibachc0046862011-03-21 17:59:37 +0100195 continue;
196 }
197
Axel Lin8e357622014-08-05 10:56:47 +0800198 channel = pval;
Axel Lin56de1372014-07-30 11:13:52 +0800199 if (channel >= ADS1015_CHANNELS) {
Dirk Eibachc0046862011-03-21 17:59:37 +0100200 dev_err(&client->dev,
Rob Herringbb923fd2017-07-18 16:43:05 -0500201 "invalid channel index %d on %pOF\n",
202 channel, node);
Dirk Eibachc0046862011-03-21 17:59:37 +0100203 continue;
204 }
205
Axel Lin8e357622014-08-05 10:56:47 +0800206 if (!of_property_read_u32(node, "ti,gain", &pval)) {
207 pga = pval;
Dirk Eibachc0046862011-03-21 17:59:37 +0100208 if (pga > 6) {
Rob Herringbb923fd2017-07-18 16:43:05 -0500209 dev_err(&client->dev, "invalid gain on %pOF\n",
210 node);
Axel Line9814292014-08-05 09:59:49 +0800211 return -EINVAL;
Dirk Eibachc0046862011-03-21 17:59:37 +0100212 }
213 }
214
Axel Lin8e357622014-08-05 10:56:47 +0800215 if (!of_property_read_u32(node, "ti,datarate", &pval)) {
216 data_rate = pval;
Dirk Eibachc0046862011-03-21 17:59:37 +0100217 if (data_rate > 7) {
218 dev_err(&client->dev,
Rob Herringbb923fd2017-07-18 16:43:05 -0500219 "invalid data_rate on %pOF\n", node);
Axel Line9814292014-08-05 09:59:49 +0800220 return -EINVAL;
Dirk Eibachc0046862011-03-21 17:59:37 +0100221 }
222 }
223
224 data->channel_data[channel].enabled = true;
225 data->channel_data[channel].pga = pga;
226 data->channel_data[channel].data_rate = data_rate;
227 }
228
229 return 0;
230}
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100231#endif
232
Dirk Eibachc0046862011-03-21 17:59:37 +0100233static void ads1015_get_channels_config(struct i2c_client *client)
234{
235 unsigned int k;
236 struct ads1015_data *data = i2c_get_clientdata(client);
237 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
238
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100239 /* prefer platform data */
Dirk Eibachc0046862011-03-21 17:59:37 +0100240 if (pdata) {
241 memcpy(data->channel_data, pdata->channel_data,
242 sizeof(data->channel_data));
243 return;
244 }
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100245
246#ifdef CONFIG_OF
Dirk Eibachc0046862011-03-21 17:59:37 +0100247 if (!ads1015_get_channels_config_of(client))
248 return;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100249#endif
250
251 /* fallback on default configuration */
Dirk Eibachc0046862011-03-21 17:59:37 +0100252 for (k = 0; k < ADS1015_CHANNELS; ++k) {
253 data->channel_data[k].enabled = true;
254 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
255 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
256 }
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100257}
258
259static int ads1015_probe(struct i2c_client *client,
260 const struct i2c_device_id *id)
261{
262 struct ads1015_data *data;
263 int err;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100264 unsigned int k;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100265
Guenter Roeck57457e32012-06-02 09:58:00 -0700266 data = devm_kzalloc(&client->dev, sizeof(struct ads1015_data),
267 GFP_KERNEL);
268 if (!data)
269 return -ENOMEM;
Javier Martinez Canillasa1409862017-02-24 10:12:56 -0300270
271 if (client->dev.of_node)
272 data->id = (enum ads1015_chips)
273 of_device_get_match_data(&client->dev);
274 else
275 data->id = id->driver_data;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100276 i2c_set_clientdata(client, data);
277 mutex_init(&data->update_lock);
278
279 /* build sysfs attribute group */
Dirk Eibachc0046862011-03-21 17:59:37 +0100280 ads1015_get_channels_config(client);
281 for (k = 0; k < ADS1015_CHANNELS; ++k) {
282 if (!data->channel_data[k].enabled)
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100283 continue;
Jean Delvarefdf241a2011-03-21 17:59:37 +0100284 err = device_create_file(&client->dev, &ads1015_in[k].dev_attr);
285 if (err)
Guenter Roeck363434b2012-02-22 08:13:52 -0800286 goto exit_remove;
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100287 }
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100288
289 data->hwmon_dev = hwmon_device_register(&client->dev);
290 if (IS_ERR(data->hwmon_dev)) {
291 err = PTR_ERR(data->hwmon_dev);
292 goto exit_remove;
293 }
294
295 return 0;
296
297exit_remove:
Dirk Eibachc0046862011-03-21 17:59:37 +0100298 for (k = 0; k < ADS1015_CHANNELS; ++k)
Jean Delvarefdf241a2011-03-21 17:59:37 +0100299 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100300 return err;
301}
302
303static const struct i2c_device_id ads1015_id[] = {
Evgeniy Dushistov60c1f312013-08-01 23:23:48 +0400304 { "ads1015", ads1015},
305 { "ads1115", ads1115},
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100306 { }
307};
308MODULE_DEVICE_TABLE(i2c, ads1015_id);
309
Javier Martinez Canillasa1409862017-02-24 10:12:56 -0300310static const struct of_device_id ads1015_of_match[] = {
311 {
312 .compatible = "ti,ads1015",
313 .data = (void *)ads1015
314 },
315 {
316 .compatible = "ti,ads1115",
317 .data = (void *)ads1115
318 },
319 { },
320};
321MODULE_DEVICE_TABLE(of, ads1015_of_match);
322
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100323static struct i2c_driver ads1015_driver = {
324 .driver = {
325 .name = "ads1015",
Javier Martinez Canillasa1409862017-02-24 10:12:56 -0300326 .of_match_table = of_match_ptr(ads1015_of_match),
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100327 },
328 .probe = ads1015_probe,
329 .remove = ads1015_remove,
330 .id_table = ads1015_id,
331};
332
Axel Linf0967ee2012-01-20 15:38:18 +0800333module_i2c_driver(ads1015_driver);
Dirk Eibach8c22a8f2011-03-21 17:59:36 +0100334
335MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
336MODULE_DESCRIPTION("ADS1015 driver");
337MODULE_LICENSE("GPL");