blob: df71c610535324017aa42e028901c27c58178917 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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/of_device.h>
19#include <linux/init.h>
20#include <linux/irq.h>
21#include <linux/i2c.h>
22#include <linux/regmap.h>
23#include <linux/pm_runtime.h>
24#include <linux/mutex.h>
25#include <linux/delay.h>
26
27#include <linux/platform_data/ads1015.h>
28
29#include <linux/iio/iio.h>
30#include <linux/iio/types.h>
31#include <linux/iio/sysfs.h>
32#include <linux/iio/events.h>
33#include <linux/iio/buffer.h>
34#include <linux/iio/triggered_buffer.h>
35#include <linux/iio/trigger_consumer.h>
36
37#define ADS1015_DRV_NAME "ads1015"
38
39#define ADS1015_CONV_REG 0x00
40#define ADS1015_CFG_REG 0x01
41#define ADS1015_LO_THRESH_REG 0x02
42#define ADS1015_HI_THRESH_REG 0x03
43
44#define ADS1015_CFG_COMP_QUE_SHIFT 0
45#define ADS1015_CFG_COMP_LAT_SHIFT 2
46#define ADS1015_CFG_COMP_POL_SHIFT 3
47#define ADS1015_CFG_COMP_MODE_SHIFT 4
48#define ADS1015_CFG_DR_SHIFT 5
49#define ADS1015_CFG_MOD_SHIFT 8
50#define ADS1015_CFG_PGA_SHIFT 9
51#define ADS1015_CFG_MUX_SHIFT 12
52
53#define ADS1015_CFG_COMP_QUE_MASK GENMASK(1, 0)
54#define ADS1015_CFG_COMP_LAT_MASK BIT(2)
55#define ADS1015_CFG_COMP_POL_MASK BIT(3)
56#define ADS1015_CFG_COMP_MODE_MASK BIT(4)
57#define ADS1015_CFG_DR_MASK GENMASK(7, 5)
58#define ADS1015_CFG_MOD_MASK BIT(8)
59#define ADS1015_CFG_PGA_MASK GENMASK(11, 9)
60#define ADS1015_CFG_MUX_MASK GENMASK(14, 12)
61
62/* Comparator queue and disable field */
63#define ADS1015_CFG_COMP_DISABLE 3
64
65/* Comparator polarity field */
66#define ADS1015_CFG_COMP_POL_LOW 0
67#define ADS1015_CFG_COMP_POL_HIGH 1
68
69/* Comparator mode field */
70#define ADS1015_CFG_COMP_MODE_TRAD 0
71#define ADS1015_CFG_COMP_MODE_WINDOW 1
72
73/* device operating modes */
74#define ADS1015_CONTINUOUS 0
75#define ADS1015_SINGLESHOT 1
76
77#define ADS1015_SLEEP_DELAY_MS 2000
78#define ADS1015_DEFAULT_PGA 2
79#define ADS1015_DEFAULT_DATA_RATE 4
80#define ADS1015_DEFAULT_CHAN 0
81
82enum chip_ids {
83 ADS1015,
84 ADS1115,
85};
86
87enum ads1015_channels {
88 ADS1015_AIN0_AIN1 = 0,
89 ADS1015_AIN0_AIN3,
90 ADS1015_AIN1_AIN3,
91 ADS1015_AIN2_AIN3,
92 ADS1015_AIN0,
93 ADS1015_AIN1,
94 ADS1015_AIN2,
95 ADS1015_AIN3,
96 ADS1015_TIMESTAMP,
97};
98
99static const unsigned int ads1015_data_rate[] = {
100 128, 250, 490, 920, 1600, 2400, 3300, 3300
101};
102
103static const unsigned int ads1115_data_rate[] = {
104 8, 16, 32, 64, 128, 250, 475, 860
105};
106
107/*
108 * Translation from PGA bits to full-scale positive and negative input voltage
109 * range in mV
110 */
111static int ads1015_fullscale_range[] = {
112 6144, 4096, 2048, 1024, 512, 256, 256, 256
113};
114
115/*
116 * Translation from COMP_QUE field value to the number of successive readings
117 * exceed the threshold values before an interrupt is generated
118 */
119static const int ads1015_comp_queue[] = { 1, 2, 4 };
120
121static const struct iio_event_spec ads1015_events[] = {
122 {
123 .type = IIO_EV_TYPE_THRESH,
124 .dir = IIO_EV_DIR_RISING,
125 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
126 BIT(IIO_EV_INFO_ENABLE),
127 }, {
128 .type = IIO_EV_TYPE_THRESH,
129 .dir = IIO_EV_DIR_FALLING,
130 .mask_separate = BIT(IIO_EV_INFO_VALUE),
131 }, {
132 .type = IIO_EV_TYPE_THRESH,
133 .dir = IIO_EV_DIR_EITHER,
134 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
135 BIT(IIO_EV_INFO_PERIOD),
136 },
137};
138
139#define ADS1015_V_CHAN(_chan, _addr) { \
140 .type = IIO_VOLTAGE, \
141 .indexed = 1, \
142 .address = _addr, \
143 .channel = _chan, \
144 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
145 BIT(IIO_CHAN_INFO_SCALE) | \
146 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
147 .scan_index = _addr, \
148 .scan_type = { \
149 .sign = 's', \
150 .realbits = 12, \
151 .storagebits = 16, \
152 .shift = 4, \
153 .endianness = IIO_CPU, \
154 }, \
155 .event_spec = ads1015_events, \
156 .num_event_specs = ARRAY_SIZE(ads1015_events), \
157 .datasheet_name = "AIN"#_chan, \
158}
159
160#define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \
161 .type = IIO_VOLTAGE, \
162 .differential = 1, \
163 .indexed = 1, \
164 .address = _addr, \
165 .channel = _chan, \
166 .channel2 = _chan2, \
167 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
168 BIT(IIO_CHAN_INFO_SCALE) | \
169 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
170 .scan_index = _addr, \
171 .scan_type = { \
172 .sign = 's', \
173 .realbits = 12, \
174 .storagebits = 16, \
175 .shift = 4, \
176 .endianness = IIO_CPU, \
177 }, \
178 .event_spec = ads1015_events, \
179 .num_event_specs = ARRAY_SIZE(ads1015_events), \
180 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
181}
182
183#define ADS1115_V_CHAN(_chan, _addr) { \
184 .type = IIO_VOLTAGE, \
185 .indexed = 1, \
186 .address = _addr, \
187 .channel = _chan, \
188 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
189 BIT(IIO_CHAN_INFO_SCALE) | \
190 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
191 .scan_index = _addr, \
192 .scan_type = { \
193 .sign = 's', \
194 .realbits = 16, \
195 .storagebits = 16, \
196 .endianness = IIO_CPU, \
197 }, \
198 .event_spec = ads1015_events, \
199 .num_event_specs = ARRAY_SIZE(ads1015_events), \
200 .datasheet_name = "AIN"#_chan, \
201}
202
203#define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) { \
204 .type = IIO_VOLTAGE, \
205 .differential = 1, \
206 .indexed = 1, \
207 .address = _addr, \
208 .channel = _chan, \
209 .channel2 = _chan2, \
210 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
211 BIT(IIO_CHAN_INFO_SCALE) | \
212 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
213 .scan_index = _addr, \
214 .scan_type = { \
215 .sign = 's', \
216 .realbits = 16, \
217 .storagebits = 16, \
218 .endianness = IIO_CPU, \
219 }, \
220 .event_spec = ads1015_events, \
221 .num_event_specs = ARRAY_SIZE(ads1015_events), \
222 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
223}
224
225struct ads1015_thresh_data {
226 unsigned int comp_queue;
227 int high_thresh;
228 int low_thresh;
229};
230
231struct ads1015_data {
232 struct regmap *regmap;
233 /*
234 * Protects ADC ops, e.g: concurrent sysfs/buffered
235 * data reads, configuration updates
236 */
237 struct mutex lock;
238 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
239
240 unsigned int event_channel;
241 unsigned int comp_mode;
242 struct ads1015_thresh_data thresh_data[ADS1015_CHANNELS];
243
244 unsigned int *data_rate;
245 /*
246 * Set to true when the ADC is switched to the continuous-conversion
247 * mode and exits from a power-down state. This flag is used to avoid
248 * getting the stale result from the conversion register.
249 */
250 bool conv_invalid;
251};
252
253static bool ads1015_event_channel_enabled(struct ads1015_data *data)
254{
255 return (data->event_channel != ADS1015_CHANNELS);
256}
257
258static void ads1015_event_channel_enable(struct ads1015_data *data, int chan,
259 int comp_mode)
260{
261 WARN_ON(ads1015_event_channel_enabled(data));
262
263 data->event_channel = chan;
264 data->comp_mode = comp_mode;
265}
266
267static void ads1015_event_channel_disable(struct ads1015_data *data, int chan)
268{
269 data->event_channel = ADS1015_CHANNELS;
270}
271
272static bool ads1015_is_writeable_reg(struct device *dev, unsigned int reg)
273{
274 switch (reg) {
275 case ADS1015_CFG_REG:
276 case ADS1015_LO_THRESH_REG:
277 case ADS1015_HI_THRESH_REG:
278 return true;
279 default:
280 return false;
281 }
282}
283
284static const struct regmap_config ads1015_regmap_config = {
285 .reg_bits = 8,
286 .val_bits = 16,
287 .max_register = ADS1015_HI_THRESH_REG,
288 .writeable_reg = ads1015_is_writeable_reg,
289};
290
291static const struct iio_chan_spec ads1015_channels[] = {
292 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
293 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
294 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
295 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
296 ADS1015_V_CHAN(0, ADS1015_AIN0),
297 ADS1015_V_CHAN(1, ADS1015_AIN1),
298 ADS1015_V_CHAN(2, ADS1015_AIN2),
299 ADS1015_V_CHAN(3, ADS1015_AIN3),
300 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
301};
302
303static const struct iio_chan_spec ads1115_channels[] = {
304 ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
305 ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
306 ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
307 ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
308 ADS1115_V_CHAN(0, ADS1015_AIN0),
309 ADS1115_V_CHAN(1, ADS1015_AIN1),
310 ADS1115_V_CHAN(2, ADS1015_AIN2),
311 ADS1115_V_CHAN(3, ADS1015_AIN3),
312 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
313};
314
315#ifdef CONFIG_PM
316static int ads1015_set_power_state(struct ads1015_data *data, bool on)
317{
318 int ret;
319 struct device *dev = regmap_get_device(data->regmap);
320
321 if (on) {
322 ret = pm_runtime_get_sync(dev);
323 if (ret < 0)
324 pm_runtime_put_noidle(dev);
325 } else {
326 pm_runtime_mark_last_busy(dev);
327 ret = pm_runtime_put_autosuspend(dev);
328 }
329
330 return ret < 0 ? ret : 0;
331}
332
333#else /* !CONFIG_PM */
334
335static int ads1015_set_power_state(struct ads1015_data *data, bool on)
336{
337 return 0;
338}
339
340#endif /* !CONFIG_PM */
341
342static
343int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
344{
345 int ret, pga, dr, conv_time;
346 unsigned int old, mask, cfg;
347
348 if (chan < 0 || chan >= ADS1015_CHANNELS)
349 return -EINVAL;
350
351 ret = regmap_read(data->regmap, ADS1015_CFG_REG, &old);
352 if (ret)
353 return ret;
354
355 pga = data->channel_data[chan].pga;
356 dr = data->channel_data[chan].data_rate;
357 mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
358 ADS1015_CFG_DR_MASK;
359 cfg = chan << ADS1015_CFG_MUX_SHIFT | pga << ADS1015_CFG_PGA_SHIFT |
360 dr << ADS1015_CFG_DR_SHIFT;
361
362 if (ads1015_event_channel_enabled(data)) {
363 mask |= ADS1015_CFG_COMP_QUE_MASK | ADS1015_CFG_COMP_MODE_MASK;
364 cfg |= data->thresh_data[chan].comp_queue <<
365 ADS1015_CFG_COMP_QUE_SHIFT |
366 data->comp_mode <<
367 ADS1015_CFG_COMP_MODE_SHIFT;
368 }
369
370 cfg = (old & ~mask) | (cfg & mask);
371
372 ret = regmap_write(data->regmap, ADS1015_CFG_REG, cfg);
373 if (ret)
374 return ret;
375
376 if (old != cfg || data->conv_invalid) {
377 int dr_old = (old & ADS1015_CFG_DR_MASK) >>
378 ADS1015_CFG_DR_SHIFT;
379
380 conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr_old]);
381 conv_time += DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]);
382 conv_time += conv_time / 10; /* 10% internal clock inaccuracy */
383 usleep_range(conv_time, conv_time + 1);
384 data->conv_invalid = false;
385 }
386
387 return regmap_read(data->regmap, ADS1015_CONV_REG, val);
388}
389
390static irqreturn_t ads1015_trigger_handler(int irq, void *p)
391{
392 struct iio_poll_func *pf = p;
393 struct iio_dev *indio_dev = pf->indio_dev;
394 struct ads1015_data *data = iio_priv(indio_dev);
395 s16 buf[8]; /* 1x s16 ADC val + 3x s16 padding + 4x s16 timestamp */
396 int chan, ret, res;
397
398 memset(buf, 0, sizeof(buf));
399
400 mutex_lock(&data->lock);
401 chan = find_first_bit(indio_dev->active_scan_mask,
402 indio_dev->masklength);
403 ret = ads1015_get_adc_result(data, chan, &res);
404 if (ret < 0) {
405 mutex_unlock(&data->lock);
406 goto err;
407 }
408
409 buf[0] = res;
410 mutex_unlock(&data->lock);
411
412 iio_push_to_buffers_with_timestamp(indio_dev, buf,
413 iio_get_time_ns(indio_dev));
414
415err:
416 iio_trigger_notify_done(indio_dev->trig);
417
418 return IRQ_HANDLED;
419}
420
421static int ads1015_set_scale(struct ads1015_data *data,
422 struct iio_chan_spec const *chan,
423 int scale, int uscale)
424{
425 int i;
426 int fullscale = div_s64((scale * 1000000LL + uscale) <<
427 (chan->scan_type.realbits - 1), 1000000);
428
429 for (i = 0; i < ARRAY_SIZE(ads1015_fullscale_range); i++) {
430 if (ads1015_fullscale_range[i] == fullscale) {
431 data->channel_data[chan->address].pga = i;
432 return 0;
433 }
434 }
435
436 return -EINVAL;
437}
438
439static int ads1015_set_data_rate(struct ads1015_data *data, int chan, int rate)
440{
441 int i;
442
443 for (i = 0; i < ARRAY_SIZE(ads1015_data_rate); i++) {
444 if (data->data_rate[i] == rate) {
445 data->channel_data[chan].data_rate = i;
446 return 0;
447 }
448 }
449
450 return -EINVAL;
451}
452
453static int ads1015_read_raw(struct iio_dev *indio_dev,
454 struct iio_chan_spec const *chan, int *val,
455 int *val2, long mask)
456{
457 int ret, idx;
458 struct ads1015_data *data = iio_priv(indio_dev);
459
460 mutex_lock(&data->lock);
461 switch (mask) {
462 case IIO_CHAN_INFO_RAW: {
463 int shift = chan->scan_type.shift;
464
465 ret = iio_device_claim_direct_mode(indio_dev);
466 if (ret)
467 break;
468
469 if (ads1015_event_channel_enabled(data) &&
470 data->event_channel != chan->address) {
471 ret = -EBUSY;
472 goto release_direct;
473 }
474
475 ret = ads1015_set_power_state(data, true);
476 if (ret < 0)
477 goto release_direct;
478
479 ret = ads1015_get_adc_result(data, chan->address, val);
480 if (ret < 0) {
481 ads1015_set_power_state(data, false);
482 goto release_direct;
483 }
484
485 *val = sign_extend32(*val >> shift, 15 - shift);
486
487 ret = ads1015_set_power_state(data, false);
488 if (ret < 0)
489 goto release_direct;
490
491 ret = IIO_VAL_INT;
492release_direct:
493 iio_device_release_direct_mode(indio_dev);
494 break;
495 }
496 case IIO_CHAN_INFO_SCALE:
497 idx = data->channel_data[chan->address].pga;
498 *val = ads1015_fullscale_range[idx];
499 *val2 = chan->scan_type.realbits - 1;
500 ret = IIO_VAL_FRACTIONAL_LOG2;
501 break;
502 case IIO_CHAN_INFO_SAMP_FREQ:
503 idx = data->channel_data[chan->address].data_rate;
504 *val = data->data_rate[idx];
505 ret = IIO_VAL_INT;
506 break;
507 default:
508 ret = -EINVAL;
509 break;
510 }
511 mutex_unlock(&data->lock);
512
513 return ret;
514}
515
516static int ads1015_write_raw(struct iio_dev *indio_dev,
517 struct iio_chan_spec const *chan, int val,
518 int val2, long mask)
519{
520 struct ads1015_data *data = iio_priv(indio_dev);
521 int ret;
522
523 mutex_lock(&data->lock);
524 switch (mask) {
525 case IIO_CHAN_INFO_SCALE:
526 ret = ads1015_set_scale(data, chan, val, val2);
527 break;
528 case IIO_CHAN_INFO_SAMP_FREQ:
529 ret = ads1015_set_data_rate(data, chan->address, val);
530 break;
531 default:
532 ret = -EINVAL;
533 break;
534 }
535 mutex_unlock(&data->lock);
536
537 return ret;
538}
539
540static int ads1015_read_event(struct iio_dev *indio_dev,
541 const struct iio_chan_spec *chan, enum iio_event_type type,
542 enum iio_event_direction dir, enum iio_event_info info, int *val,
543 int *val2)
544{
545 struct ads1015_data *data = iio_priv(indio_dev);
546 int ret;
547 unsigned int comp_queue;
548 int period;
549 int dr;
550
551 mutex_lock(&data->lock);
552
553 switch (info) {
554 case IIO_EV_INFO_VALUE:
555 *val = (dir == IIO_EV_DIR_RISING) ?
556 data->thresh_data[chan->address].high_thresh :
557 data->thresh_data[chan->address].low_thresh;
558 ret = IIO_VAL_INT;
559 break;
560 case IIO_EV_INFO_PERIOD:
561 dr = data->channel_data[chan->address].data_rate;
562 comp_queue = data->thresh_data[chan->address].comp_queue;
563 period = ads1015_comp_queue[comp_queue] *
564 USEC_PER_SEC / data->data_rate[dr];
565
566 *val = period / USEC_PER_SEC;
567 *val2 = period % USEC_PER_SEC;
568 ret = IIO_VAL_INT_PLUS_MICRO;
569 break;
570 default:
571 ret = -EINVAL;
572 break;
573 }
574
575 mutex_unlock(&data->lock);
576
577 return ret;
578}
579
580static int ads1015_write_event(struct iio_dev *indio_dev,
581 const struct iio_chan_spec *chan, enum iio_event_type type,
582 enum iio_event_direction dir, enum iio_event_info info, int val,
583 int val2)
584{
585 struct ads1015_data *data = iio_priv(indio_dev);
586 int realbits = chan->scan_type.realbits;
587 int ret = 0;
588 long long period;
589 int i;
590 int dr;
591
592 mutex_lock(&data->lock);
593
594 switch (info) {
595 case IIO_EV_INFO_VALUE:
596 if (val >= 1 << (realbits - 1) || val < -1 << (realbits - 1)) {
597 ret = -EINVAL;
598 break;
599 }
600 if (dir == IIO_EV_DIR_RISING)
601 data->thresh_data[chan->address].high_thresh = val;
602 else
603 data->thresh_data[chan->address].low_thresh = val;
604 break;
605 case IIO_EV_INFO_PERIOD:
606 dr = data->channel_data[chan->address].data_rate;
607 period = val * USEC_PER_SEC + val2;
608
609 for (i = 0; i < ARRAY_SIZE(ads1015_comp_queue) - 1; i++) {
610 if (period <= ads1015_comp_queue[i] *
611 USEC_PER_SEC / data->data_rate[dr])
612 break;
613 }
614 data->thresh_data[chan->address].comp_queue = i;
615 break;
616 default:
617 ret = -EINVAL;
618 break;
619 }
620
621 mutex_unlock(&data->lock);
622
623 return ret;
624}
625
626static int ads1015_read_event_config(struct iio_dev *indio_dev,
627 const struct iio_chan_spec *chan, enum iio_event_type type,
628 enum iio_event_direction dir)
629{
630 struct ads1015_data *data = iio_priv(indio_dev);
631 int ret = 0;
632
633 mutex_lock(&data->lock);
634 if (data->event_channel == chan->address) {
635 switch (dir) {
636 case IIO_EV_DIR_RISING:
637 ret = 1;
638 break;
639 case IIO_EV_DIR_EITHER:
640 ret = (data->comp_mode == ADS1015_CFG_COMP_MODE_WINDOW);
641 break;
642 default:
643 ret = -EINVAL;
644 break;
645 }
646 }
647 mutex_unlock(&data->lock);
648
649 return ret;
650}
651
652static int ads1015_enable_event_config(struct ads1015_data *data,
653 const struct iio_chan_spec *chan, int comp_mode)
654{
655 int low_thresh = data->thresh_data[chan->address].low_thresh;
656 int high_thresh = data->thresh_data[chan->address].high_thresh;
657 int ret;
658 unsigned int val;
659
660 if (ads1015_event_channel_enabled(data)) {
661 if (data->event_channel != chan->address ||
662 (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD &&
663 comp_mode == ADS1015_CFG_COMP_MODE_WINDOW))
664 return -EBUSY;
665
666 return 0;
667 }
668
669 if (comp_mode == ADS1015_CFG_COMP_MODE_TRAD) {
670 low_thresh = max(-1 << (chan->scan_type.realbits - 1),
671 high_thresh - 1);
672 }
673 ret = regmap_write(data->regmap, ADS1015_LO_THRESH_REG,
674 low_thresh << chan->scan_type.shift);
675 if (ret)
676 return ret;
677
678 ret = regmap_write(data->regmap, ADS1015_HI_THRESH_REG,
679 high_thresh << chan->scan_type.shift);
680 if (ret)
681 return ret;
682
683 ret = ads1015_set_power_state(data, true);
684 if (ret < 0)
685 return ret;
686
687 ads1015_event_channel_enable(data, chan->address, comp_mode);
688
689 ret = ads1015_get_adc_result(data, chan->address, &val);
690 if (ret) {
691 ads1015_event_channel_disable(data, chan->address);
692 ads1015_set_power_state(data, false);
693 }
694
695 return ret;
696}
697
698static int ads1015_disable_event_config(struct ads1015_data *data,
699 const struct iio_chan_spec *chan, int comp_mode)
700{
701 int ret;
702
703 if (!ads1015_event_channel_enabled(data))
704 return 0;
705
706 if (data->event_channel != chan->address)
707 return 0;
708
709 if (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD &&
710 comp_mode == ADS1015_CFG_COMP_MODE_WINDOW)
711 return 0;
712
713 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
714 ADS1015_CFG_COMP_QUE_MASK,
715 ADS1015_CFG_COMP_DISABLE <<
716 ADS1015_CFG_COMP_QUE_SHIFT);
717 if (ret)
718 return ret;
719
720 ads1015_event_channel_disable(data, chan->address);
721
722 return ads1015_set_power_state(data, false);
723}
724
725static int ads1015_write_event_config(struct iio_dev *indio_dev,
726 const struct iio_chan_spec *chan, enum iio_event_type type,
727 enum iio_event_direction dir, int state)
728{
729 struct ads1015_data *data = iio_priv(indio_dev);
730 int ret;
731 int comp_mode = (dir == IIO_EV_DIR_EITHER) ?
732 ADS1015_CFG_COMP_MODE_WINDOW : ADS1015_CFG_COMP_MODE_TRAD;
733
734 mutex_lock(&data->lock);
735
736 /* Prevent from enabling both buffer and event at a time */
737 ret = iio_device_claim_direct_mode(indio_dev);
738 if (ret) {
739 mutex_unlock(&data->lock);
740 return ret;
741 }
742
743 if (state)
744 ret = ads1015_enable_event_config(data, chan, comp_mode);
745 else
746 ret = ads1015_disable_event_config(data, chan, comp_mode);
747
748 iio_device_release_direct_mode(indio_dev);
749 mutex_unlock(&data->lock);
750
751 return ret;
752}
753
754static irqreturn_t ads1015_event_handler(int irq, void *priv)
755{
756 struct iio_dev *indio_dev = priv;
757 struct ads1015_data *data = iio_priv(indio_dev);
758 int val;
759 int ret;
760
761 /* Clear the latched ALERT/RDY pin */
762 ret = regmap_read(data->regmap, ADS1015_CONV_REG, &val);
763 if (ret)
764 return IRQ_HANDLED;
765
766 if (ads1015_event_channel_enabled(data)) {
767 enum iio_event_direction dir;
768 u64 code;
769
770 dir = data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD ?
771 IIO_EV_DIR_RISING : IIO_EV_DIR_EITHER;
772 code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, data->event_channel,
773 IIO_EV_TYPE_THRESH, dir);
774 iio_push_event(indio_dev, code, iio_get_time_ns(indio_dev));
775 }
776
777 return IRQ_HANDLED;
778}
779
780static int ads1015_buffer_preenable(struct iio_dev *indio_dev)
781{
782 struct ads1015_data *data = iio_priv(indio_dev);
783
784 /* Prevent from enabling both buffer and event at a time */
785 if (ads1015_event_channel_enabled(data))
786 return -EBUSY;
787
788 return ads1015_set_power_state(iio_priv(indio_dev), true);
789}
790
791static int ads1015_buffer_postdisable(struct iio_dev *indio_dev)
792{
793 return ads1015_set_power_state(iio_priv(indio_dev), false);
794}
795
796static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
797 .preenable = ads1015_buffer_preenable,
798 .postenable = iio_triggered_buffer_postenable,
799 .predisable = iio_triggered_buffer_predisable,
800 .postdisable = ads1015_buffer_postdisable,
801 .validate_scan_mask = &iio_validate_scan_mask_onehot,
802};
803
804static IIO_CONST_ATTR_NAMED(ads1015_scale_available, scale_available,
805 "3 2 1 0.5 0.25 0.125");
806static IIO_CONST_ATTR_NAMED(ads1115_scale_available, scale_available,
807 "0.1875 0.125 0.0625 0.03125 0.015625 0.007813");
808
809static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available,
810 sampling_frequency_available, "128 250 490 920 1600 2400 3300");
811static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available,
812 sampling_frequency_available, "8 16 32 64 128 250 475 860");
813
814static struct attribute *ads1015_attributes[] = {
815 &iio_const_attr_ads1015_scale_available.dev_attr.attr,
816 &iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr,
817 NULL,
818};
819
820static const struct attribute_group ads1015_attribute_group = {
821 .attrs = ads1015_attributes,
822};
823
824static struct attribute *ads1115_attributes[] = {
825 &iio_const_attr_ads1115_scale_available.dev_attr.attr,
826 &iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr,
827 NULL,
828};
829
830static const struct attribute_group ads1115_attribute_group = {
831 .attrs = ads1115_attributes,
832};
833
834static const struct iio_info ads1015_info = {
835 .driver_module = THIS_MODULE,
836 .read_raw = ads1015_read_raw,
837 .write_raw = ads1015_write_raw,
838 .read_event_value = ads1015_read_event,
839 .write_event_value = ads1015_write_event,
840 .read_event_config = ads1015_read_event_config,
841 .write_event_config = ads1015_write_event_config,
842 .attrs = &ads1015_attribute_group,
843};
844
845static const struct iio_info ads1115_info = {
846 .driver_module = THIS_MODULE,
847 .read_raw = ads1015_read_raw,
848 .write_raw = ads1015_write_raw,
849 .read_event_value = ads1015_read_event,
850 .write_event_value = ads1015_write_event,
851 .read_event_config = ads1015_read_event_config,
852 .write_event_config = ads1015_write_event_config,
853 .attrs = &ads1115_attribute_group,
854};
855
856#ifdef CONFIG_OF
857static int ads1015_get_channels_config_of(struct i2c_client *client)
858{
859 struct iio_dev *indio_dev = i2c_get_clientdata(client);
860 struct ads1015_data *data = iio_priv(indio_dev);
861 struct device_node *node;
862
863 if (!client->dev.of_node ||
864 !of_get_next_child(client->dev.of_node, NULL))
865 return -EINVAL;
866
867 for_each_child_of_node(client->dev.of_node, node) {
868 u32 pval;
869 unsigned int channel;
870 unsigned int pga = ADS1015_DEFAULT_PGA;
871 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
872
873 if (of_property_read_u32(node, "reg", &pval)) {
874 dev_err(&client->dev, "invalid reg on %pOF\n",
875 node);
876 continue;
877 }
878
879 channel = pval;
880 if (channel >= ADS1015_CHANNELS) {
881 dev_err(&client->dev,
882 "invalid channel index %d on %pOF\n",
883 channel, node);
884 continue;
885 }
886
887 if (!of_property_read_u32(node, "ti,gain", &pval)) {
888 pga = pval;
889 if (pga > 6) {
890 dev_err(&client->dev, "invalid gain on %pOF\n",
891 node);
892 of_node_put(node);
893 return -EINVAL;
894 }
895 }
896
897 if (!of_property_read_u32(node, "ti,datarate", &pval)) {
898 data_rate = pval;
899 if (data_rate > 7) {
900 dev_err(&client->dev,
901 "invalid data_rate on %pOF\n",
902 node);
903 of_node_put(node);
904 return -EINVAL;
905 }
906 }
907
908 data->channel_data[channel].pga = pga;
909 data->channel_data[channel].data_rate = data_rate;
910 }
911
912 return 0;
913}
914#endif
915
916static void ads1015_get_channels_config(struct i2c_client *client)
917{
918 unsigned int k;
919
920 struct iio_dev *indio_dev = i2c_get_clientdata(client);
921 struct ads1015_data *data = iio_priv(indio_dev);
922 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
923
924 /* prefer platform data */
925 if (pdata) {
926 memcpy(data->channel_data, pdata->channel_data,
927 sizeof(data->channel_data));
928 return;
929 }
930
931#ifdef CONFIG_OF
932 if (!ads1015_get_channels_config_of(client))
933 return;
934#endif
935 /* fallback on default configuration */
936 for (k = 0; k < ADS1015_CHANNELS; ++k) {
937 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
938 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
939 }
940}
941
942static int ads1015_set_conv_mode(struct ads1015_data *data, int mode)
943{
944 return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
945 ADS1015_CFG_MOD_MASK,
946 mode << ADS1015_CFG_MOD_SHIFT);
947}
948
949static int ads1015_probe(struct i2c_client *client,
950 const struct i2c_device_id *id)
951{
952 struct iio_dev *indio_dev;
953 struct ads1015_data *data;
954 int ret;
955 enum chip_ids chip;
956 int i;
957
958 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
959 if (!indio_dev)
960 return -ENOMEM;
961
962 data = iio_priv(indio_dev);
963 i2c_set_clientdata(client, indio_dev);
964
965 mutex_init(&data->lock);
966
967 indio_dev->dev.parent = &client->dev;
968 indio_dev->dev.of_node = client->dev.of_node;
969 indio_dev->name = ADS1015_DRV_NAME;
970 indio_dev->modes = INDIO_DIRECT_MODE;
971
972 if (client->dev.of_node)
973 chip = (enum chip_ids)of_device_get_match_data(&client->dev);
974 else
975 chip = id->driver_data;
976 switch (chip) {
977 case ADS1015:
978 indio_dev->channels = ads1015_channels;
979 indio_dev->num_channels = ARRAY_SIZE(ads1015_channels);
980 indio_dev->info = &ads1015_info;
981 data->data_rate = (unsigned int *) &ads1015_data_rate;
982 break;
983 case ADS1115:
984 indio_dev->channels = ads1115_channels;
985 indio_dev->num_channels = ARRAY_SIZE(ads1115_channels);
986 indio_dev->info = &ads1115_info;
987 data->data_rate = (unsigned int *) &ads1115_data_rate;
988 break;
989 }
990
991 data->event_channel = ADS1015_CHANNELS;
992 /*
993 * Set default lower and upper threshold to min and max value
994 * respectively.
995 */
996 for (i = 0; i < ADS1015_CHANNELS; i++) {
997 int realbits = indio_dev->channels[i].scan_type.realbits;
998
999 data->thresh_data[i].low_thresh = -1 << (realbits - 1);
1000 data->thresh_data[i].high_thresh = (1 << (realbits - 1)) - 1;
1001 }
1002
1003 /* we need to keep this ABI the same as used by hwmon ADS1015 driver */
1004 ads1015_get_channels_config(client);
1005
1006 data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config);
1007 if (IS_ERR(data->regmap)) {
1008 dev_err(&client->dev, "Failed to allocate register map\n");
1009 return PTR_ERR(data->regmap);
1010 }
1011
1012 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
1013 ads1015_trigger_handler,
1014 &ads1015_buffer_setup_ops);
1015 if (ret < 0) {
1016 dev_err(&client->dev, "iio triggered buffer setup failed\n");
1017 return ret;
1018 }
1019
1020 if (client->irq) {
1021 unsigned long irq_trig =
1022 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1023 unsigned int cfg_comp_mask = ADS1015_CFG_COMP_QUE_MASK |
1024 ADS1015_CFG_COMP_LAT_MASK | ADS1015_CFG_COMP_POL_MASK;
1025 unsigned int cfg_comp =
1026 ADS1015_CFG_COMP_DISABLE << ADS1015_CFG_COMP_QUE_SHIFT |
1027 1 << ADS1015_CFG_COMP_LAT_SHIFT;
1028
1029 switch (irq_trig) {
1030 case IRQF_TRIGGER_LOW:
1031 cfg_comp |= ADS1015_CFG_COMP_POL_LOW <<
1032 ADS1015_CFG_COMP_POL_SHIFT;
1033 break;
1034 case IRQF_TRIGGER_HIGH:
1035 cfg_comp |= ADS1015_CFG_COMP_POL_HIGH <<
1036 ADS1015_CFG_COMP_POL_SHIFT;
1037 break;
1038 default:
1039 return -EINVAL;
1040 }
1041
1042 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
1043 cfg_comp_mask, cfg_comp);
1044 if (ret)
1045 return ret;
1046
1047 ret = devm_request_threaded_irq(&client->dev, client->irq,
1048 NULL, ads1015_event_handler,
1049 irq_trig | IRQF_ONESHOT,
1050 client->name, indio_dev);
1051 if (ret)
1052 return ret;
1053 }
1054
1055 ret = ads1015_set_conv_mode(data, ADS1015_CONTINUOUS);
1056 if (ret)
1057 return ret;
1058
1059 data->conv_invalid = true;
1060
1061 ret = pm_runtime_set_active(&client->dev);
1062 if (ret)
1063 return ret;
1064 pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS);
1065 pm_runtime_use_autosuspend(&client->dev);
1066 pm_runtime_enable(&client->dev);
1067
1068 ret = iio_device_register(indio_dev);
1069 if (ret < 0) {
1070 dev_err(&client->dev, "Failed to register IIO device\n");
1071 return ret;
1072 }
1073
1074 return 0;
1075}
1076
1077static int ads1015_remove(struct i2c_client *client)
1078{
1079 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1080 struct ads1015_data *data = iio_priv(indio_dev);
1081
1082 iio_device_unregister(indio_dev);
1083
1084 pm_runtime_disable(&client->dev);
1085 pm_runtime_set_suspended(&client->dev);
1086 pm_runtime_put_noidle(&client->dev);
1087
1088 /* power down single shot mode */
1089 return ads1015_set_conv_mode(data, ADS1015_SINGLESHOT);
1090}
1091
1092#ifdef CONFIG_PM
1093static int ads1015_runtime_suspend(struct device *dev)
1094{
1095 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1096 struct ads1015_data *data = iio_priv(indio_dev);
1097
1098 return ads1015_set_conv_mode(data, ADS1015_SINGLESHOT);
1099}
1100
1101static int ads1015_runtime_resume(struct device *dev)
1102{
1103 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1104 struct ads1015_data *data = iio_priv(indio_dev);
1105 int ret;
1106
1107 ret = ads1015_set_conv_mode(data, ADS1015_CONTINUOUS);
1108 if (!ret)
1109 data->conv_invalid = true;
1110
1111 return ret;
1112}
1113#endif
1114
1115static const struct dev_pm_ops ads1015_pm_ops = {
1116 SET_RUNTIME_PM_OPS(ads1015_runtime_suspend,
1117 ads1015_runtime_resume, NULL)
1118};
1119
1120static const struct i2c_device_id ads1015_id[] = {
1121 {"ads1015", ADS1015},
1122 {"ads1115", ADS1115},
1123 {}
1124};
1125MODULE_DEVICE_TABLE(i2c, ads1015_id);
1126
1127static const struct of_device_id ads1015_of_match[] = {
1128 {
1129 .compatible = "ti,ads1015",
1130 .data = (void *)ADS1015
1131 },
1132 {
1133 .compatible = "ti,ads1115",
1134 .data = (void *)ADS1115
1135 },
1136 {}
1137};
1138MODULE_DEVICE_TABLE(of, ads1015_of_match);
1139
1140static struct i2c_driver ads1015_driver = {
1141 .driver = {
1142 .name = ADS1015_DRV_NAME,
1143 .of_match_table = ads1015_of_match,
1144 .pm = &ads1015_pm_ops,
1145 },
1146 .probe = ads1015_probe,
1147 .remove = ads1015_remove,
1148 .id_table = ads1015_id,
1149};
1150
1151module_i2c_driver(ads1015_driver);
1152
1153MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1154MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
1155MODULE_LICENSE("GPL v2");