blob: af05e20c986b54e1f68b5152f4c75e03e4e5470e [file] [log] [blame]
Daniel Balutaecc24e72016-02-11 15:49:54 +02001/*
2 * ADS1015 - Texas Instruments Analog-to-Digital Converter
3 *
4 * Copyright (c) 2016, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * IIO driver for ADS1015 ADC 7-bit I2C slave address:
11 * * 0x48 - ADDR connected to Ground
12 * * 0x49 - ADDR connected to Vdd
13 * * 0x4A - ADDR connected to SDA
14 * * 0x4B - ADDR connected to SCL
15 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/i2c.h>
20#include <linux/regmap.h>
21#include <linux/pm_runtime.h>
22#include <linux/mutex.h>
23#include <linux/delay.h>
24
25#include <linux/i2c/ads1015.h>
26
27#include <linux/iio/iio.h>
28#include <linux/iio/types.h>
29#include <linux/iio/sysfs.h>
30#include <linux/iio/buffer.h>
31#include <linux/iio/triggered_buffer.h>
32#include <linux/iio/trigger_consumer.h>
33
34#define ADS1015_DRV_NAME "ads1015"
35
36#define ADS1015_CONV_REG 0x00
37#define ADS1015_CFG_REG 0x01
38
39#define ADS1015_CFG_DR_SHIFT 5
40#define ADS1015_CFG_MOD_SHIFT 8
41#define ADS1015_CFG_PGA_SHIFT 9
42#define ADS1015_CFG_MUX_SHIFT 12
43
44#define ADS1015_CFG_DR_MASK GENMASK(7, 5)
45#define ADS1015_CFG_MOD_MASK BIT(8)
46#define ADS1015_CFG_PGA_MASK GENMASK(11, 9)
47#define ADS1015_CFG_MUX_MASK GENMASK(14, 12)
48
49/* device operating modes */
50#define ADS1015_CONTINUOUS 0
51#define ADS1015_SINGLESHOT 1
52
53#define ADS1015_SLEEP_DELAY_MS 2000
54#define ADS1015_DEFAULT_PGA 2
55#define ADS1015_DEFAULT_DATA_RATE 4
56#define ADS1015_DEFAULT_CHAN 0
57
Matt Ranostayba35f112016-05-15 22:18:46 -070058enum {
59 ADS1015,
60 ADS1115,
61};
62
Daniel Balutaecc24e72016-02-11 15:49:54 +020063enum ads1015_channels {
64 ADS1015_AIN0_AIN1 = 0,
65 ADS1015_AIN0_AIN3,
66 ADS1015_AIN1_AIN3,
67 ADS1015_AIN2_AIN3,
68 ADS1015_AIN0,
69 ADS1015_AIN1,
70 ADS1015_AIN2,
71 ADS1015_AIN3,
72 ADS1015_TIMESTAMP,
73};
74
75static const unsigned int ads1015_data_rate[] = {
76 128, 250, 490, 920, 1600, 2400, 3300, 3300
77};
78
Matt Ranostayba35f112016-05-15 22:18:46 -070079static const unsigned int ads1115_data_rate[] = {
80 8, 16, 32, 64, 128, 250, 475, 860
81};
82
Akinobu Mita115af6c2017-07-21 00:24:18 +090083/*
84 * Translation from PGA bits to full-scale positive and negative input voltage
85 * range in mV
86 */
87static int ads1015_fullscale_range[] = {
88 6144, 4096, 2048, 1024, 512, 256, 256, 256
Daniel Balutaecc24e72016-02-11 15:49:54 +020089};
90
91#define ADS1015_V_CHAN(_chan, _addr) { \
92 .type = IIO_VOLTAGE, \
93 .indexed = 1, \
94 .address = _addr, \
95 .channel = _chan, \
96 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
97 BIT(IIO_CHAN_INFO_SCALE) | \
98 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
99 .scan_index = _addr, \
100 .scan_type = { \
101 .sign = 's', \
102 .realbits = 12, \
103 .storagebits = 16, \
104 .shift = 4, \
105 .endianness = IIO_CPU, \
106 }, \
Matt Ranostay8ac8aa62016-05-17 15:02:03 -0700107 .datasheet_name = "AIN"#_chan, \
Daniel Balutaecc24e72016-02-11 15:49:54 +0200108}
109
110#define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \
111 .type = IIO_VOLTAGE, \
112 .differential = 1, \
113 .indexed = 1, \
114 .address = _addr, \
115 .channel = _chan, \
116 .channel2 = _chan2, \
117 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
118 BIT(IIO_CHAN_INFO_SCALE) | \
119 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
120 .scan_index = _addr, \
121 .scan_type = { \
122 .sign = 's', \
123 .realbits = 12, \
124 .storagebits = 16, \
125 .shift = 4, \
126 .endianness = IIO_CPU, \
127 }, \
Matt Ranostay8ac8aa62016-05-17 15:02:03 -0700128 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
Daniel Balutaecc24e72016-02-11 15:49:54 +0200129}
130
Matt Ranostayba35f112016-05-15 22:18:46 -0700131#define ADS1115_V_CHAN(_chan, _addr) { \
132 .type = IIO_VOLTAGE, \
133 .indexed = 1, \
134 .address = _addr, \
135 .channel = _chan, \
136 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
137 BIT(IIO_CHAN_INFO_SCALE) | \
138 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
139 .scan_index = _addr, \
140 .scan_type = { \
141 .sign = 's', \
142 .realbits = 16, \
143 .storagebits = 16, \
144 .endianness = IIO_CPU, \
145 }, \
Matt Ranostay8ac8aa62016-05-17 15:02:03 -0700146 .datasheet_name = "AIN"#_chan, \
Matt Ranostayba35f112016-05-15 22:18:46 -0700147}
148
149#define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) { \
150 .type = IIO_VOLTAGE, \
151 .differential = 1, \
152 .indexed = 1, \
153 .address = _addr, \
154 .channel = _chan, \
155 .channel2 = _chan2, \
156 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
157 BIT(IIO_CHAN_INFO_SCALE) | \
158 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
159 .scan_index = _addr, \
160 .scan_type = { \
161 .sign = 's', \
162 .realbits = 16, \
163 .storagebits = 16, \
164 .endianness = IIO_CPU, \
165 }, \
Matt Ranostay8ac8aa62016-05-17 15:02:03 -0700166 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
Matt Ranostayba35f112016-05-15 22:18:46 -0700167}
168
Daniel Balutaecc24e72016-02-11 15:49:54 +0200169struct ads1015_data {
170 struct regmap *regmap;
171 /*
172 * Protects ADC ops, e.g: concurrent sysfs/buffered
173 * data reads, configuration updates
174 */
175 struct mutex lock;
176 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
Matt Ranostayba35f112016-05-15 22:18:46 -0700177
178 unsigned int *data_rate;
Akinobu Mita1ed45652017-07-21 00:24:20 +0900179 /*
180 * Set to true when the ADC is switched to the continuous-conversion
181 * mode and exits from a power-down state. This flag is used to avoid
182 * getting the stale result from the conversion register.
183 */
184 bool conv_invalid;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200185};
186
187static bool ads1015_is_writeable_reg(struct device *dev, unsigned int reg)
188{
189 return (reg == ADS1015_CFG_REG);
190}
191
192static const struct regmap_config ads1015_regmap_config = {
193 .reg_bits = 8,
194 .val_bits = 16,
195 .max_register = ADS1015_CFG_REG,
196 .writeable_reg = ads1015_is_writeable_reg,
197};
198
199static const struct iio_chan_spec ads1015_channels[] = {
200 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
201 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
202 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
203 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
204 ADS1015_V_CHAN(0, ADS1015_AIN0),
205 ADS1015_V_CHAN(1, ADS1015_AIN1),
206 ADS1015_V_CHAN(2, ADS1015_AIN2),
207 ADS1015_V_CHAN(3, ADS1015_AIN3),
208 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
209};
210
Matt Ranostayba35f112016-05-15 22:18:46 -0700211static const struct iio_chan_spec ads1115_channels[] = {
212 ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
213 ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
214 ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
215 ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
216 ADS1115_V_CHAN(0, ADS1015_AIN0),
217 ADS1115_V_CHAN(1, ADS1015_AIN1),
218 ADS1115_V_CHAN(2, ADS1015_AIN2),
219 ADS1115_V_CHAN(3, ADS1015_AIN3),
220 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
221};
222
Daniel Balutaecc24e72016-02-11 15:49:54 +0200223static int ads1015_set_power_state(struct ads1015_data *data, bool on)
224{
225 int ret;
226 struct device *dev = regmap_get_device(data->regmap);
227
228 if (on) {
229 ret = pm_runtime_get_sync(dev);
230 if (ret < 0)
231 pm_runtime_put_noidle(dev);
232 } else {
233 pm_runtime_mark_last_busy(dev);
234 ret = pm_runtime_put_autosuspend(dev);
235 }
236
Akinobu Mitaff4a98e2017-07-21 00:24:21 +0900237 return ret < 0 ? ret : 0;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200238}
239
240static
241int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
242{
243 int ret, pga, dr, conv_time;
Akinobu Mitaffb58b82017-07-21 00:24:22 +0900244 unsigned int old, mask, cfg;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200245
246 if (chan < 0 || chan >= ADS1015_CHANNELS)
247 return -EINVAL;
248
Akinobu Mitaffb58b82017-07-21 00:24:22 +0900249 ret = regmap_read(data->regmap, ADS1015_CFG_REG, &old);
250 if (ret)
Daniel Balutaecc24e72016-02-11 15:49:54 +0200251 return ret;
252
Akinobu Mitaffb58b82017-07-21 00:24:22 +0900253 pga = data->channel_data[chan].pga;
254 dr = data->channel_data[chan].data_rate;
255 mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
256 ADS1015_CFG_DR_MASK;
257 cfg = chan << ADS1015_CFG_MUX_SHIFT | pga << ADS1015_CFG_PGA_SHIFT |
258 dr << ADS1015_CFG_DR_SHIFT;
259
260 cfg = (old & ~mask) | (cfg & mask);
261
262 ret = regmap_write(data->regmap, ADS1015_CFG_REG, cfg);
263 if (ret)
264 return ret;
265
266 if (old != cfg || data->conv_invalid) {
267 int dr_old = (old & ADS1015_CFG_DR_MASK) >>
268 ADS1015_CFG_DR_SHIFT;
269
270 conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr_old]);
271 conv_time += DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]);
Ladislav Michl2383ba62017-08-25 07:39:16 +0200272 conv_time += conv_time / 10; /* 10% internal clock inaccuracy */
Daniel Balutaecc24e72016-02-11 15:49:54 +0200273 usleep_range(conv_time, conv_time + 1);
Akinobu Mita1ed45652017-07-21 00:24:20 +0900274 data->conv_invalid = false;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200275 }
276
277 return regmap_read(data->regmap, ADS1015_CONV_REG, val);
278}
279
280static irqreturn_t ads1015_trigger_handler(int irq, void *p)
281{
282 struct iio_poll_func *pf = p;
283 struct iio_dev *indio_dev = pf->indio_dev;
284 struct ads1015_data *data = iio_priv(indio_dev);
285 s16 buf[8]; /* 1x s16 ADC val + 3x s16 padding + 4x s16 timestamp */
286 int chan, ret, res;
287
288 memset(buf, 0, sizeof(buf));
289
290 mutex_lock(&data->lock);
291 chan = find_first_bit(indio_dev->active_scan_mask,
292 indio_dev->masklength);
293 ret = ads1015_get_adc_result(data, chan, &res);
294 if (ret < 0) {
295 mutex_unlock(&data->lock);
296 goto err;
297 }
298
299 buf[0] = res;
300 mutex_unlock(&data->lock);
301
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100302 iio_push_to_buffers_with_timestamp(indio_dev, buf,
303 iio_get_time_ns(indio_dev));
Daniel Balutaecc24e72016-02-11 15:49:54 +0200304
305err:
306 iio_trigger_notify_done(indio_dev->trig);
307
308 return IRQ_HANDLED;
309}
310
Akinobu Mita115af6c2017-07-21 00:24:18 +0900311static int ads1015_set_scale(struct ads1015_data *data,
312 struct iio_chan_spec const *chan,
Daniel Balutaecc24e72016-02-11 15:49:54 +0200313 int scale, int uscale)
314{
315 int i, ret, rindex = -1;
Akinobu Mita115af6c2017-07-21 00:24:18 +0900316 int fullscale = div_s64((scale * 1000000LL + uscale) <<
317 (chan->scan_type.realbits - 1), 1000000);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200318
Akinobu Mita115af6c2017-07-21 00:24:18 +0900319 for (i = 0; i < ARRAY_SIZE(ads1015_fullscale_range); i++) {
320 if (ads1015_fullscale_range[i] == fullscale) {
Daniel Balutaecc24e72016-02-11 15:49:54 +0200321 rindex = i;
322 break;
323 }
Akinobu Mita115af6c2017-07-21 00:24:18 +0900324 }
Daniel Balutaecc24e72016-02-11 15:49:54 +0200325 if (rindex < 0)
326 return -EINVAL;
327
328 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
329 ADS1015_CFG_PGA_MASK,
330 rindex << ADS1015_CFG_PGA_SHIFT);
331 if (ret < 0)
332 return ret;
333
Akinobu Mita115af6c2017-07-21 00:24:18 +0900334 data->channel_data[chan->address].pga = rindex;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200335
336 return 0;
337}
338
339static int ads1015_set_data_rate(struct ads1015_data *data, int chan, int rate)
340{
Akinobu Mita177d84e2017-07-21 00:24:17 +0900341 int i;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200342
Akinobu Mita177d84e2017-07-21 00:24:17 +0900343 for (i = 0; i < ARRAY_SIZE(ads1015_data_rate); i++) {
Matt Ranostayba35f112016-05-15 22:18:46 -0700344 if (data->data_rate[i] == rate) {
Akinobu Mita177d84e2017-07-21 00:24:17 +0900345 data->channel_data[chan].data_rate = i;
346 return 0;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200347 }
Akinobu Mita177d84e2017-07-21 00:24:17 +0900348 }
Daniel Balutaecc24e72016-02-11 15:49:54 +0200349
Akinobu Mita177d84e2017-07-21 00:24:17 +0900350 return -EINVAL;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200351}
352
353static int ads1015_read_raw(struct iio_dev *indio_dev,
354 struct iio_chan_spec const *chan, int *val,
355 int *val2, long mask)
356{
357 int ret, idx;
358 struct ads1015_data *data = iio_priv(indio_dev);
359
360 mutex_lock(&indio_dev->mlock);
361 mutex_lock(&data->lock);
362 switch (mask) {
Matt Ranostayba35f112016-05-15 22:18:46 -0700363 case IIO_CHAN_INFO_RAW: {
364 int shift = chan->scan_type.shift;
365
Daniel Balutaecc24e72016-02-11 15:49:54 +0200366 if (iio_buffer_enabled(indio_dev)) {
367 ret = -EBUSY;
368 break;
369 }
370
371 ret = ads1015_set_power_state(data, true);
372 if (ret < 0)
373 break;
374
375 ret = ads1015_get_adc_result(data, chan->address, val);
376 if (ret < 0) {
377 ads1015_set_power_state(data, false);
378 break;
379 }
380
Matt Ranostayba35f112016-05-15 22:18:46 -0700381 *val = sign_extend32(*val >> shift, 15 - shift);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200382
383 ret = ads1015_set_power_state(data, false);
384 if (ret < 0)
385 break;
386
387 ret = IIO_VAL_INT;
388 break;
Matt Ranostayba35f112016-05-15 22:18:46 -0700389 }
Daniel Balutaecc24e72016-02-11 15:49:54 +0200390 case IIO_CHAN_INFO_SCALE:
391 idx = data->channel_data[chan->address].pga;
Akinobu Mita115af6c2017-07-21 00:24:18 +0900392 *val = ads1015_fullscale_range[idx];
393 *val2 = chan->scan_type.realbits - 1;
394 ret = IIO_VAL_FRACTIONAL_LOG2;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200395 break;
396 case IIO_CHAN_INFO_SAMP_FREQ:
397 idx = data->channel_data[chan->address].data_rate;
Matt Ranostayba35f112016-05-15 22:18:46 -0700398 *val = data->data_rate[idx];
Daniel Balutaecc24e72016-02-11 15:49:54 +0200399 ret = IIO_VAL_INT;
400 break;
401 default:
402 ret = -EINVAL;
403 break;
404 }
405 mutex_unlock(&data->lock);
406 mutex_unlock(&indio_dev->mlock);
407
408 return ret;
409}
410
411static int ads1015_write_raw(struct iio_dev *indio_dev,
412 struct iio_chan_spec const *chan, int val,
413 int val2, long mask)
414{
415 struct ads1015_data *data = iio_priv(indio_dev);
416 int ret;
417
418 mutex_lock(&data->lock);
419 switch (mask) {
420 case IIO_CHAN_INFO_SCALE:
Akinobu Mita115af6c2017-07-21 00:24:18 +0900421 ret = ads1015_set_scale(data, chan, val, val2);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200422 break;
423 case IIO_CHAN_INFO_SAMP_FREQ:
424 ret = ads1015_set_data_rate(data, chan->address, val);
425 break;
426 default:
427 ret = -EINVAL;
428 break;
429 }
430 mutex_unlock(&data->lock);
431
432 return ret;
433}
434
435static int ads1015_buffer_preenable(struct iio_dev *indio_dev)
436{
437 return ads1015_set_power_state(iio_priv(indio_dev), true);
438}
439
440static int ads1015_buffer_postdisable(struct iio_dev *indio_dev)
441{
442 return ads1015_set_power_state(iio_priv(indio_dev), false);
443}
444
445static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
446 .preenable = ads1015_buffer_preenable,
447 .postenable = iio_triggered_buffer_postenable,
448 .predisable = iio_triggered_buffer_predisable,
449 .postdisable = ads1015_buffer_postdisable,
450 .validate_scan_mask = &iio_validate_scan_mask_onehot,
451};
452
Akinobu Mita115af6c2017-07-21 00:24:18 +0900453static IIO_CONST_ATTR_NAMED(ads1015_scale_available, scale_available,
454 "3 2 1 0.5 0.25 0.125");
455static IIO_CONST_ATTR_NAMED(ads1115_scale_available, scale_available,
456 "0.1875 0.125 0.0625 0.03125 0.015625 0.007813");
Matt Ranostayba35f112016-05-15 22:18:46 -0700457
458static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available,
459 sampling_frequency_available, "128 250 490 920 1600 2400 3300");
460static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available,
461 sampling_frequency_available, "8 16 32 64 128 250 475 860");
Daniel Balutaecc24e72016-02-11 15:49:54 +0200462
463static struct attribute *ads1015_attributes[] = {
Akinobu Mita115af6c2017-07-21 00:24:18 +0900464 &iio_const_attr_ads1015_scale_available.dev_attr.attr,
Matt Ranostayba35f112016-05-15 22:18:46 -0700465 &iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr,
Daniel Balutaecc24e72016-02-11 15:49:54 +0200466 NULL,
467};
468
469static const struct attribute_group ads1015_attribute_group = {
470 .attrs = ads1015_attributes,
471};
472
Matt Ranostayba35f112016-05-15 22:18:46 -0700473static struct attribute *ads1115_attributes[] = {
Akinobu Mita115af6c2017-07-21 00:24:18 +0900474 &iio_const_attr_ads1115_scale_available.dev_attr.attr,
Matt Ranostayba35f112016-05-15 22:18:46 -0700475 &iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr,
476 NULL,
477};
478
479static const struct attribute_group ads1115_attribute_group = {
480 .attrs = ads1115_attributes,
481};
482
483static struct iio_info ads1015_info = {
Daniel Balutaecc24e72016-02-11 15:49:54 +0200484 .driver_module = THIS_MODULE,
485 .read_raw = ads1015_read_raw,
486 .write_raw = ads1015_write_raw,
Matt Ranostayba35f112016-05-15 22:18:46 -0700487 .attrs = &ads1015_attribute_group,
488};
489
490static struct iio_info ads1115_info = {
491 .driver_module = THIS_MODULE,
492 .read_raw = ads1015_read_raw,
493 .write_raw = ads1015_write_raw,
494 .attrs = &ads1115_attribute_group,
Daniel Balutaecc24e72016-02-11 15:49:54 +0200495};
496
497#ifdef CONFIG_OF
498static int ads1015_get_channels_config_of(struct i2c_client *client)
499{
Giorgio Dal Molin522caeb2016-08-16 20:43:37 +0200500 struct iio_dev *indio_dev = i2c_get_clientdata(client);
501 struct ads1015_data *data = iio_priv(indio_dev);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200502 struct device_node *node;
503
504 if (!client->dev.of_node ||
505 !of_get_next_child(client->dev.of_node, NULL))
506 return -EINVAL;
507
508 for_each_child_of_node(client->dev.of_node, node) {
509 u32 pval;
510 unsigned int channel;
511 unsigned int pga = ADS1015_DEFAULT_PGA;
512 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
513
514 if (of_property_read_u32(node, "reg", &pval)) {
515 dev_err(&client->dev, "invalid reg on %s\n",
516 node->full_name);
517 continue;
518 }
519
520 channel = pval;
521 if (channel >= ADS1015_CHANNELS) {
522 dev_err(&client->dev,
523 "invalid channel index %d on %s\n",
524 channel, node->full_name);
525 continue;
526 }
527
528 if (!of_property_read_u32(node, "ti,gain", &pval)) {
529 pga = pval;
530 if (pga > 6) {
531 dev_err(&client->dev, "invalid gain on %s\n",
532 node->full_name);
Wei Yongjun943bbe72016-08-26 14:31:50 +0000533 of_node_put(node);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200534 return -EINVAL;
535 }
536 }
537
538 if (!of_property_read_u32(node, "ti,datarate", &pval)) {
539 data_rate = pval;
540 if (data_rate > 7) {
541 dev_err(&client->dev,
542 "invalid data_rate on %s\n",
543 node->full_name);
Wei Yongjun943bbe72016-08-26 14:31:50 +0000544 of_node_put(node);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200545 return -EINVAL;
546 }
547 }
548
549 data->channel_data[channel].pga = pga;
550 data->channel_data[channel].data_rate = data_rate;
551 }
552
553 return 0;
554}
555#endif
556
557static void ads1015_get_channels_config(struct i2c_client *client)
558{
559 unsigned int k;
560
561 struct iio_dev *indio_dev = i2c_get_clientdata(client);
562 struct ads1015_data *data = iio_priv(indio_dev);
563 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
564
565 /* prefer platform data */
566 if (pdata) {
567 memcpy(data->channel_data, pdata->channel_data,
568 sizeof(data->channel_data));
569 return;
570 }
571
572#ifdef CONFIG_OF
573 if (!ads1015_get_channels_config_of(client))
574 return;
575#endif
576 /* fallback on default configuration */
577 for (k = 0; k < ADS1015_CHANNELS; ++k) {
578 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
579 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
580 }
581}
582
583static int ads1015_probe(struct i2c_client *client,
584 const struct i2c_device_id *id)
585{
586 struct iio_dev *indio_dev;
587 struct ads1015_data *data;
588 int ret;
589
590 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
591 if (!indio_dev)
592 return -ENOMEM;
593
594 data = iio_priv(indio_dev);
595 i2c_set_clientdata(client, indio_dev);
596
597 mutex_init(&data->lock);
598
599 indio_dev->dev.parent = &client->dev;
Matt Ranostayf5241db2016-06-30 19:33:50 -0700600 indio_dev->dev.of_node = client->dev.of_node;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200601 indio_dev->name = ADS1015_DRV_NAME;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200602 indio_dev->modes = INDIO_DIRECT_MODE;
603
Matt Ranostayba35f112016-05-15 22:18:46 -0700604 switch (id->driver_data) {
605 case ADS1015:
606 indio_dev->channels = ads1015_channels;
607 indio_dev->num_channels = ARRAY_SIZE(ads1015_channels);
608 indio_dev->info = &ads1015_info;
609 data->data_rate = (unsigned int *) &ads1015_data_rate;
610 break;
611 case ADS1115:
612 indio_dev->channels = ads1115_channels;
613 indio_dev->num_channels = ARRAY_SIZE(ads1115_channels);
614 indio_dev->info = &ads1115_info;
615 data->data_rate = (unsigned int *) &ads1115_data_rate;
616 break;
617 }
618
Daniel Balutaecc24e72016-02-11 15:49:54 +0200619 /* we need to keep this ABI the same as used by hwmon ADS1015 driver */
620 ads1015_get_channels_config(client);
621
622 data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config);
623 if (IS_ERR(data->regmap)) {
624 dev_err(&client->dev, "Failed to allocate register map\n");
625 return PTR_ERR(data->regmap);
626 }
627
628 ret = iio_triggered_buffer_setup(indio_dev, NULL,
629 ads1015_trigger_handler,
630 &ads1015_buffer_setup_ops);
631 if (ret < 0) {
632 dev_err(&client->dev, "iio triggered buffer setup failed\n");
633 return ret;
634 }
Akinobu Mitac72ad1a2017-07-21 00:24:19 +0900635
636 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
637 ADS1015_CFG_MOD_MASK,
638 ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
639 if (ret)
640 return ret;
641
Akinobu Mita1ed45652017-07-21 00:24:20 +0900642 data->conv_invalid = true;
643
Daniel Balutaecc24e72016-02-11 15:49:54 +0200644 ret = pm_runtime_set_active(&client->dev);
645 if (ret)
646 goto err_buffer_cleanup;
647 pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS);
648 pm_runtime_use_autosuspend(&client->dev);
649 pm_runtime_enable(&client->dev);
650
651 ret = iio_device_register(indio_dev);
652 if (ret < 0) {
653 dev_err(&client->dev, "Failed to register IIO device\n");
654 goto err_buffer_cleanup;
655 }
656
657 return 0;
658
659err_buffer_cleanup:
660 iio_triggered_buffer_cleanup(indio_dev);
661
662 return ret;
663}
664
665static int ads1015_remove(struct i2c_client *client)
666{
667 struct iio_dev *indio_dev = i2c_get_clientdata(client);
668 struct ads1015_data *data = iio_priv(indio_dev);
669
670 iio_device_unregister(indio_dev);
671
672 pm_runtime_disable(&client->dev);
673 pm_runtime_set_suspended(&client->dev);
674 pm_runtime_put_noidle(&client->dev);
675
676 iio_triggered_buffer_cleanup(indio_dev);
677
678 /* power down single shot mode */
679 return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
680 ADS1015_CFG_MOD_MASK,
681 ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
682}
683
684#ifdef CONFIG_PM
685static int ads1015_runtime_suspend(struct device *dev)
686{
687 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
688 struct ads1015_data *data = iio_priv(indio_dev);
689
690 return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
691 ADS1015_CFG_MOD_MASK,
692 ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
693}
694
695static int ads1015_runtime_resume(struct device *dev)
696{
697 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
698 struct ads1015_data *data = iio_priv(indio_dev);
Akinobu Mita1ed45652017-07-21 00:24:20 +0900699 int ret;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200700
Akinobu Mita1ed45652017-07-21 00:24:20 +0900701 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
Daniel Balutaecc24e72016-02-11 15:49:54 +0200702 ADS1015_CFG_MOD_MASK,
703 ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
Akinobu Mita1ed45652017-07-21 00:24:20 +0900704 if (!ret)
705 data->conv_invalid = true;
706
707 return ret;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200708}
709#endif
710
711static const struct dev_pm_ops ads1015_pm_ops = {
712 SET_RUNTIME_PM_OPS(ads1015_runtime_suspend,
713 ads1015_runtime_resume, NULL)
714};
715
716static const struct i2c_device_id ads1015_id[] = {
Matt Ranostayba35f112016-05-15 22:18:46 -0700717 {"ads1015", ADS1015},
718 {"ads1115", ADS1115},
Daniel Balutaecc24e72016-02-11 15:49:54 +0200719 {}
720};
721MODULE_DEVICE_TABLE(i2c, ads1015_id);
722
723static struct i2c_driver ads1015_driver = {
724 .driver = {
725 .name = ADS1015_DRV_NAME,
726 .pm = &ads1015_pm_ops,
727 },
728 .probe = ads1015_probe,
729 .remove = ads1015_remove,
730 .id_table = ads1015_id,
731};
732
733module_i2c_driver(ads1015_driver);
734
735MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
736MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
737MODULE_LICENSE("GPL v2");