blob: 6f22f46000e2d0a587cc111ca47d0dde569a2dc2 [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>
Javier Martinez Canillasc172d222017-03-15 01:45:00 -030018#include <linux/of_device.h>
Daniel Balutaecc24e72016-02-11 15:49:54 +020019#include <linux/init.h>
20#include <linux/i2c.h>
21#include <linux/regmap.h>
22#include <linux/pm_runtime.h>
23#include <linux/mutex.h>
24#include <linux/delay.h>
25
Wolfram Sang9010624c2017-05-21 22:34:39 +020026#include <linux/platform_data/ads1015.h>
Daniel Balutaecc24e72016-02-11 15:49:54 +020027
28#include <linux/iio/iio.h>
29#include <linux/iio/types.h>
30#include <linux/iio/sysfs.h>
31#include <linux/iio/buffer.h>
32#include <linux/iio/triggered_buffer.h>
33#include <linux/iio/trigger_consumer.h>
34
35#define ADS1015_DRV_NAME "ads1015"
36
37#define ADS1015_CONV_REG 0x00
38#define ADS1015_CFG_REG 0x01
39
40#define ADS1015_CFG_DR_SHIFT 5
41#define ADS1015_CFG_MOD_SHIFT 8
42#define ADS1015_CFG_PGA_SHIFT 9
43#define ADS1015_CFG_MUX_SHIFT 12
44
45#define ADS1015_CFG_DR_MASK GENMASK(7, 5)
46#define ADS1015_CFG_MOD_MASK BIT(8)
47#define ADS1015_CFG_PGA_MASK GENMASK(11, 9)
48#define ADS1015_CFG_MUX_MASK GENMASK(14, 12)
49
50/* device operating modes */
51#define ADS1015_CONTINUOUS 0
52#define ADS1015_SINGLESHOT 1
53
54#define ADS1015_SLEEP_DELAY_MS 2000
55#define ADS1015_DEFAULT_PGA 2
56#define ADS1015_DEFAULT_DATA_RATE 4
57#define ADS1015_DEFAULT_CHAN 0
58
Javier Martinez Canillasc172d222017-03-15 01:45:00 -030059enum chip_ids {
Matt Ranostayba35f112016-05-15 22:18:46 -070060 ADS1015,
61 ADS1115,
62};
63
Daniel Balutaecc24e72016-02-11 15:49:54 +020064enum ads1015_channels {
65 ADS1015_AIN0_AIN1 = 0,
66 ADS1015_AIN0_AIN3,
67 ADS1015_AIN1_AIN3,
68 ADS1015_AIN2_AIN3,
69 ADS1015_AIN0,
70 ADS1015_AIN1,
71 ADS1015_AIN2,
72 ADS1015_AIN3,
73 ADS1015_TIMESTAMP,
74};
75
76static const unsigned int ads1015_data_rate[] = {
77 128, 250, 490, 920, 1600, 2400, 3300, 3300
78};
79
Matt Ranostayba35f112016-05-15 22:18:46 -070080static const unsigned int ads1115_data_rate[] = {
81 8, 16, 32, 64, 128, 250, 475, 860
82};
83
Akinobu Mita8d0e8e72017-07-21 00:24:18 +090084/*
85 * Translation from PGA bits to full-scale positive and negative input voltage
86 * range in mV
87 */
88static int ads1015_fullscale_range[] = {
89 6144, 4096, 2048, 1024, 512, 256, 256, 256
Daniel Balutaecc24e72016-02-11 15:49:54 +020090};
91
92#define ADS1015_V_CHAN(_chan, _addr) { \
93 .type = IIO_VOLTAGE, \
94 .indexed = 1, \
95 .address = _addr, \
96 .channel = _chan, \
97 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
98 BIT(IIO_CHAN_INFO_SCALE) | \
99 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
100 .scan_index = _addr, \
101 .scan_type = { \
102 .sign = 's', \
103 .realbits = 12, \
104 .storagebits = 16, \
105 .shift = 4, \
106 .endianness = IIO_CPU, \
107 }, \
Matt Ranostay8ac8aa62016-05-17 15:02:03 -0700108 .datasheet_name = "AIN"#_chan, \
Daniel Balutaecc24e72016-02-11 15:49:54 +0200109}
110
111#define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \
112 .type = IIO_VOLTAGE, \
113 .differential = 1, \
114 .indexed = 1, \
115 .address = _addr, \
116 .channel = _chan, \
117 .channel2 = _chan2, \
118 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
119 BIT(IIO_CHAN_INFO_SCALE) | \
120 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
121 .scan_index = _addr, \
122 .scan_type = { \
123 .sign = 's', \
124 .realbits = 12, \
125 .storagebits = 16, \
126 .shift = 4, \
127 .endianness = IIO_CPU, \
128 }, \
Matt Ranostay8ac8aa62016-05-17 15:02:03 -0700129 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
Daniel Balutaecc24e72016-02-11 15:49:54 +0200130}
131
Matt Ranostayba35f112016-05-15 22:18:46 -0700132#define ADS1115_V_CHAN(_chan, _addr) { \
133 .type = IIO_VOLTAGE, \
134 .indexed = 1, \
135 .address = _addr, \
136 .channel = _chan, \
137 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
138 BIT(IIO_CHAN_INFO_SCALE) | \
139 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
140 .scan_index = _addr, \
141 .scan_type = { \
142 .sign = 's', \
143 .realbits = 16, \
144 .storagebits = 16, \
145 .endianness = IIO_CPU, \
146 }, \
Matt Ranostay8ac8aa62016-05-17 15:02:03 -0700147 .datasheet_name = "AIN"#_chan, \
Matt Ranostayba35f112016-05-15 22:18:46 -0700148}
149
150#define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) { \
151 .type = IIO_VOLTAGE, \
152 .differential = 1, \
153 .indexed = 1, \
154 .address = _addr, \
155 .channel = _chan, \
156 .channel2 = _chan2, \
157 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
158 BIT(IIO_CHAN_INFO_SCALE) | \
159 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
160 .scan_index = _addr, \
161 .scan_type = { \
162 .sign = 's', \
163 .realbits = 16, \
164 .storagebits = 16, \
165 .endianness = IIO_CPU, \
166 }, \
Matt Ranostay8ac8aa62016-05-17 15:02:03 -0700167 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
Matt Ranostayba35f112016-05-15 22:18:46 -0700168}
169
Daniel Balutaecc24e72016-02-11 15:49:54 +0200170struct ads1015_data {
171 struct regmap *regmap;
172 /*
173 * Protects ADC ops, e.g: concurrent sysfs/buffered
174 * data reads, configuration updates
175 */
176 struct mutex lock;
177 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
Matt Ranostayba35f112016-05-15 22:18:46 -0700178
179 unsigned int *data_rate;
Akinobu Mita73e3e3f2017-07-21 00:24:20 +0900180 /*
181 * Set to true when the ADC is switched to the continuous-conversion
182 * mode and exits from a power-down state. This flag is used to avoid
183 * getting the stale result from the conversion register.
184 */
185 bool conv_invalid;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200186};
187
188static bool ads1015_is_writeable_reg(struct device *dev, unsigned int reg)
189{
190 return (reg == ADS1015_CFG_REG);
191}
192
193static const struct regmap_config ads1015_regmap_config = {
194 .reg_bits = 8,
195 .val_bits = 16,
196 .max_register = ADS1015_CFG_REG,
197 .writeable_reg = ads1015_is_writeable_reg,
198};
199
200static const struct iio_chan_spec ads1015_channels[] = {
201 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
202 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
203 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
204 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
205 ADS1015_V_CHAN(0, ADS1015_AIN0),
206 ADS1015_V_CHAN(1, ADS1015_AIN1),
207 ADS1015_V_CHAN(2, ADS1015_AIN2),
208 ADS1015_V_CHAN(3, ADS1015_AIN3),
209 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
210};
211
Matt Ranostayba35f112016-05-15 22:18:46 -0700212static const struct iio_chan_spec ads1115_channels[] = {
213 ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
214 ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
215 ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
216 ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
217 ADS1115_V_CHAN(0, ADS1015_AIN0),
218 ADS1115_V_CHAN(1, ADS1015_AIN1),
219 ADS1115_V_CHAN(2, ADS1015_AIN2),
220 ADS1115_V_CHAN(3, ADS1015_AIN3),
221 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
222};
223
Daniel Balutaecc24e72016-02-11 15:49:54 +0200224static int ads1015_set_power_state(struct ads1015_data *data, bool on)
225{
226 int ret;
227 struct device *dev = regmap_get_device(data->regmap);
228
229 if (on) {
230 ret = pm_runtime_get_sync(dev);
231 if (ret < 0)
232 pm_runtime_put_noidle(dev);
233 } else {
234 pm_runtime_mark_last_busy(dev);
235 ret = pm_runtime_put_autosuspend(dev);
236 }
237
Akinobu Mitaa6fe5e52017-07-21 00:24:21 +0900238 return ret < 0 ? ret : 0;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200239}
240
241static
242int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
243{
244 int ret, pga, dr, conv_time;
Akinobu Mita4744d4e2017-07-21 00:24:22 +0900245 unsigned int old, mask, cfg;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200246
247 if (chan < 0 || chan >= ADS1015_CHANNELS)
248 return -EINVAL;
249
Akinobu Mita4744d4e2017-07-21 00:24:22 +0900250 ret = regmap_read(data->regmap, ADS1015_CFG_REG, &old);
251 if (ret)
Daniel Balutaecc24e72016-02-11 15:49:54 +0200252 return ret;
253
Akinobu Mita4744d4e2017-07-21 00:24:22 +0900254 pga = data->channel_data[chan].pga;
255 dr = data->channel_data[chan].data_rate;
256 mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
257 ADS1015_CFG_DR_MASK;
258 cfg = chan << ADS1015_CFG_MUX_SHIFT | pga << ADS1015_CFG_PGA_SHIFT |
259 dr << ADS1015_CFG_DR_SHIFT;
260
261 cfg = (old & ~mask) | (cfg & mask);
262
263 ret = regmap_write(data->regmap, ADS1015_CFG_REG, cfg);
264 if (ret)
265 return ret;
266
267 if (old != cfg || data->conv_invalid) {
268 int dr_old = (old & ADS1015_CFG_DR_MASK) >>
269 ADS1015_CFG_DR_SHIFT;
270
271 conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr_old]);
272 conv_time += DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200273 usleep_range(conv_time, conv_time + 1);
Akinobu Mita73e3e3f2017-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 Mita8d0e8e72017-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{
Akinobu Mita56b57a92017-07-21 00:24:23 +0900315 int i;
Akinobu Mita8d0e8e72017-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 Mita8d0e8e72017-07-21 00:24:18 +0900319 for (i = 0; i < ARRAY_SIZE(ads1015_fullscale_range); i++) {
320 if (ads1015_fullscale_range[i] == fullscale) {
Akinobu Mita56b57a92017-07-21 00:24:23 +0900321 data->channel_data[chan->address].pga = i;
322 return 0;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200323 }
Akinobu Mita8d0e8e72017-07-21 00:24:18 +0900324 }
Daniel Balutaecc24e72016-02-11 15:49:54 +0200325
Akinobu Mita56b57a92017-07-21 00:24:23 +0900326 return -EINVAL;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200327}
328
329static int ads1015_set_data_rate(struct ads1015_data *data, int chan, int rate)
330{
Akinobu Mita0d106b72017-07-21 00:24:17 +0900331 int i;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200332
Akinobu Mita0d106b72017-07-21 00:24:17 +0900333 for (i = 0; i < ARRAY_SIZE(ads1015_data_rate); i++) {
Matt Ranostayba35f112016-05-15 22:18:46 -0700334 if (data->data_rate[i] == rate) {
Akinobu Mita0d106b72017-07-21 00:24:17 +0900335 data->channel_data[chan].data_rate = i;
336 return 0;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200337 }
Akinobu Mita0d106b72017-07-21 00:24:17 +0900338 }
Daniel Balutaecc24e72016-02-11 15:49:54 +0200339
Akinobu Mita0d106b72017-07-21 00:24:17 +0900340 return -EINVAL;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200341}
342
343static int ads1015_read_raw(struct iio_dev *indio_dev,
344 struct iio_chan_spec const *chan, int *val,
345 int *val2, long mask)
346{
347 int ret, idx;
348 struct ads1015_data *data = iio_priv(indio_dev);
349
350 mutex_lock(&indio_dev->mlock);
351 mutex_lock(&data->lock);
352 switch (mask) {
Matt Ranostayba35f112016-05-15 22:18:46 -0700353 case IIO_CHAN_INFO_RAW: {
354 int shift = chan->scan_type.shift;
355
Daniel Balutaecc24e72016-02-11 15:49:54 +0200356 if (iio_buffer_enabled(indio_dev)) {
357 ret = -EBUSY;
358 break;
359 }
360
361 ret = ads1015_set_power_state(data, true);
362 if (ret < 0)
363 break;
364
365 ret = ads1015_get_adc_result(data, chan->address, val);
366 if (ret < 0) {
367 ads1015_set_power_state(data, false);
368 break;
369 }
370
Matt Ranostayba35f112016-05-15 22:18:46 -0700371 *val = sign_extend32(*val >> shift, 15 - shift);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200372
373 ret = ads1015_set_power_state(data, false);
374 if (ret < 0)
375 break;
376
377 ret = IIO_VAL_INT;
378 break;
Matt Ranostayba35f112016-05-15 22:18:46 -0700379 }
Daniel Balutaecc24e72016-02-11 15:49:54 +0200380 case IIO_CHAN_INFO_SCALE:
381 idx = data->channel_data[chan->address].pga;
Akinobu Mita8d0e8e72017-07-21 00:24:18 +0900382 *val = ads1015_fullscale_range[idx];
383 *val2 = chan->scan_type.realbits - 1;
384 ret = IIO_VAL_FRACTIONAL_LOG2;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200385 break;
386 case IIO_CHAN_INFO_SAMP_FREQ:
387 idx = data->channel_data[chan->address].data_rate;
Matt Ranostayba35f112016-05-15 22:18:46 -0700388 *val = data->data_rate[idx];
Daniel Balutaecc24e72016-02-11 15:49:54 +0200389 ret = IIO_VAL_INT;
390 break;
391 default:
392 ret = -EINVAL;
393 break;
394 }
395 mutex_unlock(&data->lock);
396 mutex_unlock(&indio_dev->mlock);
397
398 return ret;
399}
400
401static int ads1015_write_raw(struct iio_dev *indio_dev,
402 struct iio_chan_spec const *chan, int val,
403 int val2, long mask)
404{
405 struct ads1015_data *data = iio_priv(indio_dev);
406 int ret;
407
408 mutex_lock(&data->lock);
409 switch (mask) {
410 case IIO_CHAN_INFO_SCALE:
Akinobu Mita8d0e8e72017-07-21 00:24:18 +0900411 ret = ads1015_set_scale(data, chan, val, val2);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200412 break;
413 case IIO_CHAN_INFO_SAMP_FREQ:
414 ret = ads1015_set_data_rate(data, chan->address, val);
415 break;
416 default:
417 ret = -EINVAL;
418 break;
419 }
420 mutex_unlock(&data->lock);
421
422 return ret;
423}
424
425static int ads1015_buffer_preenable(struct iio_dev *indio_dev)
426{
427 return ads1015_set_power_state(iio_priv(indio_dev), true);
428}
429
430static int ads1015_buffer_postdisable(struct iio_dev *indio_dev)
431{
432 return ads1015_set_power_state(iio_priv(indio_dev), false);
433}
434
435static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
436 .preenable = ads1015_buffer_preenable,
437 .postenable = iio_triggered_buffer_postenable,
438 .predisable = iio_triggered_buffer_predisable,
439 .postdisable = ads1015_buffer_postdisable,
440 .validate_scan_mask = &iio_validate_scan_mask_onehot,
441};
442
Akinobu Mita8d0e8e72017-07-21 00:24:18 +0900443static IIO_CONST_ATTR_NAMED(ads1015_scale_available, scale_available,
444 "3 2 1 0.5 0.25 0.125");
445static IIO_CONST_ATTR_NAMED(ads1115_scale_available, scale_available,
446 "0.1875 0.125 0.0625 0.03125 0.015625 0.007813");
Matt Ranostayba35f112016-05-15 22:18:46 -0700447
448static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available,
449 sampling_frequency_available, "128 250 490 920 1600 2400 3300");
450static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available,
451 sampling_frequency_available, "8 16 32 64 128 250 475 860");
Daniel Balutaecc24e72016-02-11 15:49:54 +0200452
453static struct attribute *ads1015_attributes[] = {
Akinobu Mita8d0e8e72017-07-21 00:24:18 +0900454 &iio_const_attr_ads1015_scale_available.dev_attr.attr,
Matt Ranostayba35f112016-05-15 22:18:46 -0700455 &iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr,
Daniel Balutaecc24e72016-02-11 15:49:54 +0200456 NULL,
457};
458
459static const struct attribute_group ads1015_attribute_group = {
460 .attrs = ads1015_attributes,
461};
462
Matt Ranostayba35f112016-05-15 22:18:46 -0700463static struct attribute *ads1115_attributes[] = {
Akinobu Mita8d0e8e72017-07-21 00:24:18 +0900464 &iio_const_attr_ads1115_scale_available.dev_attr.attr,
Matt Ranostayba35f112016-05-15 22:18:46 -0700465 &iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr,
466 NULL,
467};
468
469static const struct attribute_group ads1115_attribute_group = {
470 .attrs = ads1115_attributes,
471};
472
Bhumika Goyal99a22f02017-01-21 22:33:00 +0530473static const struct iio_info ads1015_info = {
Daniel Balutaecc24e72016-02-11 15:49:54 +0200474 .driver_module = THIS_MODULE,
475 .read_raw = ads1015_read_raw,
476 .write_raw = ads1015_write_raw,
Matt Ranostayba35f112016-05-15 22:18:46 -0700477 .attrs = &ads1015_attribute_group,
478};
479
Bhumika Goyal99a22f02017-01-21 22:33:00 +0530480static const struct iio_info ads1115_info = {
Matt Ranostayba35f112016-05-15 22:18:46 -0700481 .driver_module = THIS_MODULE,
482 .read_raw = ads1015_read_raw,
483 .write_raw = ads1015_write_raw,
484 .attrs = &ads1115_attribute_group,
Daniel Balutaecc24e72016-02-11 15:49:54 +0200485};
486
487#ifdef CONFIG_OF
488static int ads1015_get_channels_config_of(struct i2c_client *client)
489{
Giorgio Dal Molin522caeb2016-08-16 20:43:37 +0200490 struct iio_dev *indio_dev = i2c_get_clientdata(client);
491 struct ads1015_data *data = iio_priv(indio_dev);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200492 struct device_node *node;
493
494 if (!client->dev.of_node ||
495 !of_get_next_child(client->dev.of_node, NULL))
496 return -EINVAL;
497
498 for_each_child_of_node(client->dev.of_node, node) {
499 u32 pval;
500 unsigned int channel;
501 unsigned int pga = ADS1015_DEFAULT_PGA;
502 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
503
504 if (of_property_read_u32(node, "reg", &pval)) {
Rob Herring3921db42017-07-18 16:43:08 -0500505 dev_err(&client->dev, "invalid reg on %pOF\n",
506 node);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200507 continue;
508 }
509
510 channel = pval;
511 if (channel >= ADS1015_CHANNELS) {
512 dev_err(&client->dev,
Rob Herring3921db42017-07-18 16:43:08 -0500513 "invalid channel index %d on %pOF\n",
514 channel, node);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200515 continue;
516 }
517
518 if (!of_property_read_u32(node, "ti,gain", &pval)) {
519 pga = pval;
520 if (pga > 6) {
Rob Herring3921db42017-07-18 16:43:08 -0500521 dev_err(&client->dev, "invalid gain on %pOF\n",
522 node);
Wei Yongjun943bbe72016-08-26 14:31:50 +0000523 of_node_put(node);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200524 return -EINVAL;
525 }
526 }
527
528 if (!of_property_read_u32(node, "ti,datarate", &pval)) {
529 data_rate = pval;
530 if (data_rate > 7) {
531 dev_err(&client->dev,
Rob Herring3921db42017-07-18 16:43:08 -0500532 "invalid data_rate on %pOF\n",
533 node);
Wei Yongjun943bbe72016-08-26 14:31:50 +0000534 of_node_put(node);
Daniel Balutaecc24e72016-02-11 15:49:54 +0200535 return -EINVAL;
536 }
537 }
538
539 data->channel_data[channel].pga = pga;
540 data->channel_data[channel].data_rate = data_rate;
541 }
542
543 return 0;
544}
545#endif
546
547static void ads1015_get_channels_config(struct i2c_client *client)
548{
549 unsigned int k;
550
551 struct iio_dev *indio_dev = i2c_get_clientdata(client);
552 struct ads1015_data *data = iio_priv(indio_dev);
553 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
554
555 /* prefer platform data */
556 if (pdata) {
557 memcpy(data->channel_data, pdata->channel_data,
558 sizeof(data->channel_data));
559 return;
560 }
561
562#ifdef CONFIG_OF
563 if (!ads1015_get_channels_config_of(client))
564 return;
565#endif
566 /* fallback on default configuration */
567 for (k = 0; k < ADS1015_CHANNELS; ++k) {
568 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
569 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
570 }
571}
572
573static int ads1015_probe(struct i2c_client *client,
574 const struct i2c_device_id *id)
575{
576 struct iio_dev *indio_dev;
577 struct ads1015_data *data;
578 int ret;
Javier Martinez Canillasc172d222017-03-15 01:45:00 -0300579 enum chip_ids chip;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200580
581 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
582 if (!indio_dev)
583 return -ENOMEM;
584
585 data = iio_priv(indio_dev);
586 i2c_set_clientdata(client, indio_dev);
587
588 mutex_init(&data->lock);
589
590 indio_dev->dev.parent = &client->dev;
Matt Ranostayf5241db2016-06-30 19:33:50 -0700591 indio_dev->dev.of_node = client->dev.of_node;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200592 indio_dev->name = ADS1015_DRV_NAME;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200593 indio_dev->modes = INDIO_DIRECT_MODE;
594
Javier Martinez Canillasc172d222017-03-15 01:45:00 -0300595 if (client->dev.of_node)
596 chip = (enum chip_ids)of_device_get_match_data(&client->dev);
597 else
598 chip = id->driver_data;
599 switch (chip) {
Matt Ranostayba35f112016-05-15 22:18:46 -0700600 case ADS1015:
601 indio_dev->channels = ads1015_channels;
602 indio_dev->num_channels = ARRAY_SIZE(ads1015_channels);
603 indio_dev->info = &ads1015_info;
604 data->data_rate = (unsigned int *) &ads1015_data_rate;
605 break;
606 case ADS1115:
607 indio_dev->channels = ads1115_channels;
608 indio_dev->num_channels = ARRAY_SIZE(ads1115_channels);
609 indio_dev->info = &ads1115_info;
610 data->data_rate = (unsigned int *) &ads1115_data_rate;
611 break;
612 }
613
Daniel Balutaecc24e72016-02-11 15:49:54 +0200614 /* we need to keep this ABI the same as used by hwmon ADS1015 driver */
615 ads1015_get_channels_config(client);
616
617 data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config);
618 if (IS_ERR(data->regmap)) {
619 dev_err(&client->dev, "Failed to allocate register map\n");
620 return PTR_ERR(data->regmap);
621 }
622
623 ret = iio_triggered_buffer_setup(indio_dev, NULL,
624 ads1015_trigger_handler,
625 &ads1015_buffer_setup_ops);
626 if (ret < 0) {
627 dev_err(&client->dev, "iio triggered buffer setup failed\n");
628 return ret;
629 }
Akinobu Mitae8245c62017-07-21 00:24:19 +0900630
631 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
632 ADS1015_CFG_MOD_MASK,
633 ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
634 if (ret)
635 return ret;
636
Akinobu Mita73e3e3f2017-07-21 00:24:20 +0900637 data->conv_invalid = true;
638
Daniel Balutaecc24e72016-02-11 15:49:54 +0200639 ret = pm_runtime_set_active(&client->dev);
640 if (ret)
641 goto err_buffer_cleanup;
642 pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS);
643 pm_runtime_use_autosuspend(&client->dev);
644 pm_runtime_enable(&client->dev);
645
646 ret = iio_device_register(indio_dev);
647 if (ret < 0) {
648 dev_err(&client->dev, "Failed to register IIO device\n");
649 goto err_buffer_cleanup;
650 }
651
652 return 0;
653
654err_buffer_cleanup:
655 iio_triggered_buffer_cleanup(indio_dev);
656
657 return ret;
658}
659
660static int ads1015_remove(struct i2c_client *client)
661{
662 struct iio_dev *indio_dev = i2c_get_clientdata(client);
663 struct ads1015_data *data = iio_priv(indio_dev);
664
665 iio_device_unregister(indio_dev);
666
667 pm_runtime_disable(&client->dev);
668 pm_runtime_set_suspended(&client->dev);
669 pm_runtime_put_noidle(&client->dev);
670
671 iio_triggered_buffer_cleanup(indio_dev);
672
673 /* power down single shot mode */
674 return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
675 ADS1015_CFG_MOD_MASK,
676 ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
677}
678
679#ifdef CONFIG_PM
680static int ads1015_runtime_suspend(struct device *dev)
681{
682 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
683 struct ads1015_data *data = iio_priv(indio_dev);
684
685 return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
686 ADS1015_CFG_MOD_MASK,
687 ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
688}
689
690static int ads1015_runtime_resume(struct device *dev)
691{
692 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
693 struct ads1015_data *data = iio_priv(indio_dev);
Akinobu Mita73e3e3f2017-07-21 00:24:20 +0900694 int ret;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200695
Akinobu Mita73e3e3f2017-07-21 00:24:20 +0900696 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
Daniel Balutaecc24e72016-02-11 15:49:54 +0200697 ADS1015_CFG_MOD_MASK,
698 ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
Akinobu Mita73e3e3f2017-07-21 00:24:20 +0900699 if (!ret)
700 data->conv_invalid = true;
701
702 return ret;
Daniel Balutaecc24e72016-02-11 15:49:54 +0200703}
704#endif
705
706static const struct dev_pm_ops ads1015_pm_ops = {
707 SET_RUNTIME_PM_OPS(ads1015_runtime_suspend,
708 ads1015_runtime_resume, NULL)
709};
710
711static const struct i2c_device_id ads1015_id[] = {
Matt Ranostayba35f112016-05-15 22:18:46 -0700712 {"ads1015", ADS1015},
713 {"ads1115", ADS1115},
Daniel Balutaecc24e72016-02-11 15:49:54 +0200714 {}
715};
716MODULE_DEVICE_TABLE(i2c, ads1015_id);
717
Javier Martinez Canillasc172d222017-03-15 01:45:00 -0300718static const struct of_device_id ads1015_of_match[] = {
719 {
720 .compatible = "ti,ads1015",
721 .data = (void *)ADS1015
722 },
723 {
724 .compatible = "ti,ads1115",
725 .data = (void *)ADS1115
726 },
727 {}
728};
729MODULE_DEVICE_TABLE(of, ads1015_of_match);
730
Daniel Balutaecc24e72016-02-11 15:49:54 +0200731static struct i2c_driver ads1015_driver = {
732 .driver = {
733 .name = ADS1015_DRV_NAME,
Javier Martinez Canillasc172d222017-03-15 01:45:00 -0300734 .of_match_table = ads1015_of_match,
Daniel Balutaecc24e72016-02-11 15:49:54 +0200735 .pm = &ads1015_pm_ops,
736 },
737 .probe = ads1015_probe,
738 .remove = ads1015_remove,
739 .id_table = ads1015_id,
740};
741
742module_i2c_driver(ads1015_driver);
743
744MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
745MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
746MODULE_LICENSE("GPL v2");