1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADS1015 - Texas Instruments Analog-to-Digital Converter
5 * Copyright (c) 2016, Intel Corporation.
7 * IIO driver for ADS1015 ADC 7-bit I2C slave address:
8 * * 0x48 - ADDR connected to Ground
9 * * 0x49 - ADDR connected to Vdd
10 * * 0x4A - ADDR connected to SDA
11 * * 0x4B - ADDR connected to SCL
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/init.h>
17 #include <linux/irq.h>
18 #include <linux/i2c.h>
19 #include <linux/regmap.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/mutex.h>
22 #include <linux/delay.h>
24 #include <linux/platform_data/ads1015.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/types.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/events.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include <linux/iio/trigger_consumer.h>
34 #define ADS1015_DRV_NAME "ads1015"
36 #define ADS1015_CONV_REG 0x00
37 #define ADS1015_CFG_REG 0x01
38 #define ADS1015_LO_THRESH_REG 0x02
39 #define ADS1015_HI_THRESH_REG 0x03
41 #define ADS1015_CFG_COMP_QUE_SHIFT 0
42 #define ADS1015_CFG_COMP_LAT_SHIFT 2
43 #define ADS1015_CFG_COMP_POL_SHIFT 3
44 #define ADS1015_CFG_COMP_MODE_SHIFT 4
45 #define ADS1015_CFG_DR_SHIFT 5
46 #define ADS1015_CFG_MOD_SHIFT 8
47 #define ADS1015_CFG_PGA_SHIFT 9
48 #define ADS1015_CFG_MUX_SHIFT 12
50 #define ADS1015_CFG_COMP_QUE_MASK GENMASK(1, 0)
51 #define ADS1015_CFG_COMP_LAT_MASK BIT(2)
52 #define ADS1015_CFG_COMP_POL_MASK BIT(3)
53 #define ADS1015_CFG_COMP_MODE_MASK BIT(4)
54 #define ADS1015_CFG_DR_MASK GENMASK(7, 5)
55 #define ADS1015_CFG_MOD_MASK BIT(8)
56 #define ADS1015_CFG_PGA_MASK GENMASK(11, 9)
57 #define ADS1015_CFG_MUX_MASK GENMASK(14, 12)
59 /* Comparator queue and disable field */
60 #define ADS1015_CFG_COMP_DISABLE 3
62 /* Comparator polarity field */
63 #define ADS1015_CFG_COMP_POL_LOW 0
64 #define ADS1015_CFG_COMP_POL_HIGH 1
66 /* Comparator mode field */
67 #define ADS1015_CFG_COMP_MODE_TRAD 0
68 #define ADS1015_CFG_COMP_MODE_WINDOW 1
70 /* device operating modes */
71 #define ADS1015_CONTINUOUS 0
72 #define ADS1015_SINGLESHOT 1
74 #define ADS1015_SLEEP_DELAY_MS 2000
75 #define ADS1015_DEFAULT_PGA 2
76 #define ADS1015_DEFAULT_DATA_RATE 4
77 #define ADS1015_DEFAULT_CHAN 0
84 enum ads1015_channels {
85 ADS1015_AIN0_AIN1 = 0,
96 static const unsigned int ads1015_data_rate[] = {
97 128, 250, 490, 920, 1600, 2400, 3300, 3300
100 static const unsigned int ads1115_data_rate[] = {
101 8, 16, 32, 64, 128, 250, 475, 860
105 * Translation from PGA bits to full-scale positive and negative input voltage
108 static int ads1015_fullscale_range[] = {
109 6144, 4096, 2048, 1024, 512, 256, 256, 256
113 * Translation from COMP_QUE field value to the number of successive readings
114 * exceed the threshold values before an interrupt is generated
116 static const int ads1015_comp_queue[] = { 1, 2, 4 };
118 static const struct iio_event_spec ads1015_events[] = {
120 .type = IIO_EV_TYPE_THRESH,
121 .dir = IIO_EV_DIR_RISING,
122 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
123 BIT(IIO_EV_INFO_ENABLE),
125 .type = IIO_EV_TYPE_THRESH,
126 .dir = IIO_EV_DIR_FALLING,
127 .mask_separate = BIT(IIO_EV_INFO_VALUE),
129 .type = IIO_EV_TYPE_THRESH,
130 .dir = IIO_EV_DIR_EITHER,
131 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
132 BIT(IIO_EV_INFO_PERIOD),
136 #define ADS1015_V_CHAN(_chan, _addr) { \
137 .type = IIO_VOLTAGE, \
141 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
142 BIT(IIO_CHAN_INFO_SCALE) | \
143 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
144 .scan_index = _addr, \
150 .endianness = IIO_CPU, \
152 .event_spec = ads1015_events, \
153 .num_event_specs = ARRAY_SIZE(ads1015_events), \
154 .datasheet_name = "AIN"#_chan, \
157 #define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \
158 .type = IIO_VOLTAGE, \
163 .channel2 = _chan2, \
164 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
165 BIT(IIO_CHAN_INFO_SCALE) | \
166 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
167 .scan_index = _addr, \
173 .endianness = IIO_CPU, \
175 .event_spec = ads1015_events, \
176 .num_event_specs = ARRAY_SIZE(ads1015_events), \
177 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
180 #define ADS1115_V_CHAN(_chan, _addr) { \
181 .type = IIO_VOLTAGE, \
185 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
186 BIT(IIO_CHAN_INFO_SCALE) | \
187 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
188 .scan_index = _addr, \
193 .endianness = IIO_CPU, \
195 .event_spec = ads1015_events, \
196 .num_event_specs = ARRAY_SIZE(ads1015_events), \
197 .datasheet_name = "AIN"#_chan, \
200 #define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) { \
201 .type = IIO_VOLTAGE, \
206 .channel2 = _chan2, \
207 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
208 BIT(IIO_CHAN_INFO_SCALE) | \
209 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
210 .scan_index = _addr, \
215 .endianness = IIO_CPU, \
217 .event_spec = ads1015_events, \
218 .num_event_specs = ARRAY_SIZE(ads1015_events), \
219 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
222 struct ads1015_thresh_data {
223 unsigned int comp_queue;
228 struct ads1015_data {
229 struct regmap *regmap;
231 * Protects ADC ops, e.g: concurrent sysfs/buffered
232 * data reads, configuration updates
235 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
237 unsigned int event_channel;
238 unsigned int comp_mode;
239 struct ads1015_thresh_data thresh_data[ADS1015_CHANNELS];
241 unsigned int *data_rate;
243 * Set to true when the ADC is switched to the continuous-conversion
244 * mode and exits from a power-down state. This flag is used to avoid
245 * getting the stale result from the conversion register.
250 static bool ads1015_event_channel_enabled(struct ads1015_data *data)
252 return (data->event_channel != ADS1015_CHANNELS);
255 static void ads1015_event_channel_enable(struct ads1015_data *data, int chan,
258 WARN_ON(ads1015_event_channel_enabled(data));
260 data->event_channel = chan;
261 data->comp_mode = comp_mode;
264 static void ads1015_event_channel_disable(struct ads1015_data *data, int chan)
266 data->event_channel = ADS1015_CHANNELS;
269 static bool ads1015_is_writeable_reg(struct device *dev, unsigned int reg)
272 case ADS1015_CFG_REG:
273 case ADS1015_LO_THRESH_REG:
274 case ADS1015_HI_THRESH_REG:
281 static const struct regmap_config ads1015_regmap_config = {
284 .max_register = ADS1015_HI_THRESH_REG,
285 .writeable_reg = ads1015_is_writeable_reg,
288 static const struct iio_chan_spec ads1015_channels[] = {
289 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
290 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
291 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
292 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
293 ADS1015_V_CHAN(0, ADS1015_AIN0),
294 ADS1015_V_CHAN(1, ADS1015_AIN1),
295 ADS1015_V_CHAN(2, ADS1015_AIN2),
296 ADS1015_V_CHAN(3, ADS1015_AIN3),
297 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
300 static const struct iio_chan_spec ads1115_channels[] = {
301 ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
302 ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
303 ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
304 ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
305 ADS1115_V_CHAN(0, ADS1015_AIN0),
306 ADS1115_V_CHAN(1, ADS1015_AIN1),
307 ADS1115_V_CHAN(2, ADS1015_AIN2),
308 ADS1115_V_CHAN(3, ADS1015_AIN3),
309 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
312 static int ads1015_set_power_state(struct ads1015_data *data, bool on)
315 struct device *dev = regmap_get_device(data->regmap);
318 ret = pm_runtime_get_sync(dev);
320 pm_runtime_put_noidle(dev);
322 pm_runtime_mark_last_busy(dev);
323 ret = pm_runtime_put_autosuspend(dev);
326 return ret < 0 ? ret : 0;
330 int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
332 int ret, pga, dr, dr_old, conv_time;
333 unsigned int old, mask, cfg;
335 if (chan < 0 || chan >= ADS1015_CHANNELS)
338 ret = regmap_read(data->regmap, ADS1015_CFG_REG, &old);
342 pga = data->channel_data[chan].pga;
343 dr = data->channel_data[chan].data_rate;
344 mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
346 cfg = chan << ADS1015_CFG_MUX_SHIFT | pga << ADS1015_CFG_PGA_SHIFT |
347 dr << ADS1015_CFG_DR_SHIFT;
349 if (ads1015_event_channel_enabled(data)) {
350 mask |= ADS1015_CFG_COMP_QUE_MASK | ADS1015_CFG_COMP_MODE_MASK;
351 cfg |= data->thresh_data[chan].comp_queue <<
352 ADS1015_CFG_COMP_QUE_SHIFT |
354 ADS1015_CFG_COMP_MODE_SHIFT;
357 cfg = (old & ~mask) | (cfg & mask);
359 ret = regmap_write(data->regmap, ADS1015_CFG_REG, cfg);
362 data->conv_invalid = true;
364 if (data->conv_invalid) {
365 dr_old = (old & ADS1015_CFG_DR_MASK) >> ADS1015_CFG_DR_SHIFT;
366 conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr_old]);
367 conv_time += DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]);
368 conv_time += conv_time / 10; /* 10% internal clock inaccuracy */
369 usleep_range(conv_time, conv_time + 1);
370 data->conv_invalid = false;
373 return regmap_read(data->regmap, ADS1015_CONV_REG, val);
376 static irqreturn_t ads1015_trigger_handler(int irq, void *p)
378 struct iio_poll_func *pf = p;
379 struct iio_dev *indio_dev = pf->indio_dev;
380 struct ads1015_data *data = iio_priv(indio_dev);
381 s16 buf[8]; /* 1x s16 ADC val + 3x s16 padding + 4x s16 timestamp */
384 memset(buf, 0, sizeof(buf));
386 mutex_lock(&data->lock);
387 chan = find_first_bit(indio_dev->active_scan_mask,
388 indio_dev->masklength);
389 ret = ads1015_get_adc_result(data, chan, &res);
391 mutex_unlock(&data->lock);
396 mutex_unlock(&data->lock);
398 iio_push_to_buffers_with_timestamp(indio_dev, buf,
399 iio_get_time_ns(indio_dev));
402 iio_trigger_notify_done(indio_dev->trig);
407 static int ads1015_set_scale(struct ads1015_data *data,
408 struct iio_chan_spec const *chan,
409 int scale, int uscale)
412 int fullscale = div_s64((scale * 1000000LL + uscale) <<
413 (chan->scan_type.realbits - 1), 1000000);
415 for (i = 0; i < ARRAY_SIZE(ads1015_fullscale_range); i++) {
416 if (ads1015_fullscale_range[i] == fullscale) {
417 data->channel_data[chan->address].pga = i;
425 static int ads1015_set_data_rate(struct ads1015_data *data, int chan, int rate)
429 for (i = 0; i < ARRAY_SIZE(ads1015_data_rate); i++) {
430 if (data->data_rate[i] == rate) {
431 data->channel_data[chan].data_rate = i;
439 static int ads1015_read_raw(struct iio_dev *indio_dev,
440 struct iio_chan_spec const *chan, int *val,
441 int *val2, long mask)
444 struct ads1015_data *data = iio_priv(indio_dev);
446 mutex_lock(&data->lock);
448 case IIO_CHAN_INFO_RAW: {
449 int shift = chan->scan_type.shift;
451 ret = iio_device_claim_direct_mode(indio_dev);
455 if (ads1015_event_channel_enabled(data) &&
456 data->event_channel != chan->address) {
461 ret = ads1015_set_power_state(data, true);
465 ret = ads1015_get_adc_result(data, chan->address, val);
467 ads1015_set_power_state(data, false);
471 *val = sign_extend32(*val >> shift, 15 - shift);
473 ret = ads1015_set_power_state(data, false);
479 iio_device_release_direct_mode(indio_dev);
482 case IIO_CHAN_INFO_SCALE:
483 idx = data->channel_data[chan->address].pga;
484 *val = ads1015_fullscale_range[idx];
485 *val2 = chan->scan_type.realbits - 1;
486 ret = IIO_VAL_FRACTIONAL_LOG2;
488 case IIO_CHAN_INFO_SAMP_FREQ:
489 idx = data->channel_data[chan->address].data_rate;
490 *val = data->data_rate[idx];
497 mutex_unlock(&data->lock);
502 static int ads1015_write_raw(struct iio_dev *indio_dev,
503 struct iio_chan_spec const *chan, int val,
506 struct ads1015_data *data = iio_priv(indio_dev);
509 mutex_lock(&data->lock);
511 case IIO_CHAN_INFO_SCALE:
512 ret = ads1015_set_scale(data, chan, val, val2);
514 case IIO_CHAN_INFO_SAMP_FREQ:
515 ret = ads1015_set_data_rate(data, chan->address, val);
521 mutex_unlock(&data->lock);
526 static int ads1015_read_event(struct iio_dev *indio_dev,
527 const struct iio_chan_spec *chan, enum iio_event_type type,
528 enum iio_event_direction dir, enum iio_event_info info, int *val,
531 struct ads1015_data *data = iio_priv(indio_dev);
533 unsigned int comp_queue;
537 mutex_lock(&data->lock);
540 case IIO_EV_INFO_VALUE:
541 *val = (dir == IIO_EV_DIR_RISING) ?
542 data->thresh_data[chan->address].high_thresh :
543 data->thresh_data[chan->address].low_thresh;
546 case IIO_EV_INFO_PERIOD:
547 dr = data->channel_data[chan->address].data_rate;
548 comp_queue = data->thresh_data[chan->address].comp_queue;
549 period = ads1015_comp_queue[comp_queue] *
550 USEC_PER_SEC / data->data_rate[dr];
552 *val = period / USEC_PER_SEC;
553 *val2 = period % USEC_PER_SEC;
554 ret = IIO_VAL_INT_PLUS_MICRO;
561 mutex_unlock(&data->lock);
566 static int ads1015_write_event(struct iio_dev *indio_dev,
567 const struct iio_chan_spec *chan, enum iio_event_type type,
568 enum iio_event_direction dir, enum iio_event_info info, int val,
571 struct ads1015_data *data = iio_priv(indio_dev);
572 int realbits = chan->scan_type.realbits;
578 mutex_lock(&data->lock);
581 case IIO_EV_INFO_VALUE:
582 if (val >= 1 << (realbits - 1) || val < -1 << (realbits - 1)) {
586 if (dir == IIO_EV_DIR_RISING)
587 data->thresh_data[chan->address].high_thresh = val;
589 data->thresh_data[chan->address].low_thresh = val;
591 case IIO_EV_INFO_PERIOD:
592 dr = data->channel_data[chan->address].data_rate;
593 period = val * USEC_PER_SEC + val2;
595 for (i = 0; i < ARRAY_SIZE(ads1015_comp_queue) - 1; i++) {
596 if (period <= ads1015_comp_queue[i] *
597 USEC_PER_SEC / data->data_rate[dr])
600 data->thresh_data[chan->address].comp_queue = i;
607 mutex_unlock(&data->lock);
612 static int ads1015_read_event_config(struct iio_dev *indio_dev,
613 const struct iio_chan_spec *chan, enum iio_event_type type,
614 enum iio_event_direction dir)
616 struct ads1015_data *data = iio_priv(indio_dev);
619 mutex_lock(&data->lock);
620 if (data->event_channel == chan->address) {
622 case IIO_EV_DIR_RISING:
625 case IIO_EV_DIR_EITHER:
626 ret = (data->comp_mode == ADS1015_CFG_COMP_MODE_WINDOW);
633 mutex_unlock(&data->lock);
638 static int ads1015_enable_event_config(struct ads1015_data *data,
639 const struct iio_chan_spec *chan, int comp_mode)
641 int low_thresh = data->thresh_data[chan->address].low_thresh;
642 int high_thresh = data->thresh_data[chan->address].high_thresh;
646 if (ads1015_event_channel_enabled(data)) {
647 if (data->event_channel != chan->address ||
648 (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD &&
649 comp_mode == ADS1015_CFG_COMP_MODE_WINDOW))
655 if (comp_mode == ADS1015_CFG_COMP_MODE_TRAD) {
656 low_thresh = max(-1 << (chan->scan_type.realbits - 1),
659 ret = regmap_write(data->regmap, ADS1015_LO_THRESH_REG,
660 low_thresh << chan->scan_type.shift);
664 ret = regmap_write(data->regmap, ADS1015_HI_THRESH_REG,
665 high_thresh << chan->scan_type.shift);
669 ret = ads1015_set_power_state(data, true);
673 ads1015_event_channel_enable(data, chan->address, comp_mode);
675 ret = ads1015_get_adc_result(data, chan->address, &val);
677 ads1015_event_channel_disable(data, chan->address);
678 ads1015_set_power_state(data, false);
684 static int ads1015_disable_event_config(struct ads1015_data *data,
685 const struct iio_chan_spec *chan, int comp_mode)
689 if (!ads1015_event_channel_enabled(data))
692 if (data->event_channel != chan->address)
695 if (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD &&
696 comp_mode == ADS1015_CFG_COMP_MODE_WINDOW)
699 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
700 ADS1015_CFG_COMP_QUE_MASK,
701 ADS1015_CFG_COMP_DISABLE <<
702 ADS1015_CFG_COMP_QUE_SHIFT);
706 ads1015_event_channel_disable(data, chan->address);
708 return ads1015_set_power_state(data, false);
711 static int ads1015_write_event_config(struct iio_dev *indio_dev,
712 const struct iio_chan_spec *chan, enum iio_event_type type,
713 enum iio_event_direction dir, int state)
715 struct ads1015_data *data = iio_priv(indio_dev);
717 int comp_mode = (dir == IIO_EV_DIR_EITHER) ?
718 ADS1015_CFG_COMP_MODE_WINDOW : ADS1015_CFG_COMP_MODE_TRAD;
720 mutex_lock(&data->lock);
722 /* Prevent from enabling both buffer and event at a time */
723 ret = iio_device_claim_direct_mode(indio_dev);
725 mutex_unlock(&data->lock);
730 ret = ads1015_enable_event_config(data, chan, comp_mode);
732 ret = ads1015_disable_event_config(data, chan, comp_mode);
734 iio_device_release_direct_mode(indio_dev);
735 mutex_unlock(&data->lock);
740 static irqreturn_t ads1015_event_handler(int irq, void *priv)
742 struct iio_dev *indio_dev = priv;
743 struct ads1015_data *data = iio_priv(indio_dev);
747 /* Clear the latched ALERT/RDY pin */
748 ret = regmap_read(data->regmap, ADS1015_CONV_REG, &val);
752 if (ads1015_event_channel_enabled(data)) {
753 enum iio_event_direction dir;
756 dir = data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD ?
757 IIO_EV_DIR_RISING : IIO_EV_DIR_EITHER;
758 code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, data->event_channel,
759 IIO_EV_TYPE_THRESH, dir);
760 iio_push_event(indio_dev, code, iio_get_time_ns(indio_dev));
766 static int ads1015_buffer_preenable(struct iio_dev *indio_dev)
768 struct ads1015_data *data = iio_priv(indio_dev);
770 /* Prevent from enabling both buffer and event at a time */
771 if (ads1015_event_channel_enabled(data))
774 return ads1015_set_power_state(iio_priv(indio_dev), true);
777 static int ads1015_buffer_postdisable(struct iio_dev *indio_dev)
779 return ads1015_set_power_state(iio_priv(indio_dev), false);
782 static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
783 .preenable = ads1015_buffer_preenable,
784 .postenable = iio_triggered_buffer_postenable,
785 .predisable = iio_triggered_buffer_predisable,
786 .postdisable = ads1015_buffer_postdisable,
787 .validate_scan_mask = &iio_validate_scan_mask_onehot,
790 static IIO_CONST_ATTR_NAMED(ads1015_scale_available, scale_available,
791 "3 2 1 0.5 0.25 0.125");
792 static IIO_CONST_ATTR_NAMED(ads1115_scale_available, scale_available,
793 "0.1875 0.125 0.0625 0.03125 0.015625 0.007813");
795 static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available,
796 sampling_frequency_available, "128 250 490 920 1600 2400 3300");
797 static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available,
798 sampling_frequency_available, "8 16 32 64 128 250 475 860");
800 static struct attribute *ads1015_attributes[] = {
801 &iio_const_attr_ads1015_scale_available.dev_attr.attr,
802 &iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr,
806 static const struct attribute_group ads1015_attribute_group = {
807 .attrs = ads1015_attributes,
810 static struct attribute *ads1115_attributes[] = {
811 &iio_const_attr_ads1115_scale_available.dev_attr.attr,
812 &iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr,
816 static const struct attribute_group ads1115_attribute_group = {
817 .attrs = ads1115_attributes,
820 static const struct iio_info ads1015_info = {
821 .read_raw = ads1015_read_raw,
822 .write_raw = ads1015_write_raw,
823 .read_event_value = ads1015_read_event,
824 .write_event_value = ads1015_write_event,
825 .read_event_config = ads1015_read_event_config,
826 .write_event_config = ads1015_write_event_config,
827 .attrs = &ads1015_attribute_group,
830 static const struct iio_info ads1115_info = {
831 .read_raw = ads1015_read_raw,
832 .write_raw = ads1015_write_raw,
833 .read_event_value = ads1015_read_event,
834 .write_event_value = ads1015_write_event,
835 .read_event_config = ads1015_read_event_config,
836 .write_event_config = ads1015_write_event_config,
837 .attrs = &ads1115_attribute_group,
841 static int ads1015_get_channels_config_of(struct i2c_client *client)
843 struct iio_dev *indio_dev = i2c_get_clientdata(client);
844 struct ads1015_data *data = iio_priv(indio_dev);
845 struct device_node *node;
847 if (!client->dev.of_node ||
848 !of_get_next_child(client->dev.of_node, NULL))
851 for_each_child_of_node(client->dev.of_node, node) {
853 unsigned int channel;
854 unsigned int pga = ADS1015_DEFAULT_PGA;
855 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
857 if (of_property_read_u32(node, "reg", &pval)) {
858 dev_err(&client->dev, "invalid reg on %pOF\n",
864 if (channel >= ADS1015_CHANNELS) {
865 dev_err(&client->dev,
866 "invalid channel index %d on %pOF\n",
871 if (!of_property_read_u32(node, "ti,gain", &pval)) {
874 dev_err(&client->dev, "invalid gain on %pOF\n",
881 if (!of_property_read_u32(node, "ti,datarate", &pval)) {
884 dev_err(&client->dev,
885 "invalid data_rate on %pOF\n",
892 data->channel_data[channel].pga = pga;
893 data->channel_data[channel].data_rate = data_rate;
900 static void ads1015_get_channels_config(struct i2c_client *client)
904 struct iio_dev *indio_dev = i2c_get_clientdata(client);
905 struct ads1015_data *data = iio_priv(indio_dev);
906 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
908 /* prefer platform data */
910 memcpy(data->channel_data, pdata->channel_data,
911 sizeof(data->channel_data));
916 if (!ads1015_get_channels_config_of(client))
919 /* fallback on default configuration */
920 for (k = 0; k < ADS1015_CHANNELS; ++k) {
921 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
922 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
926 static int ads1015_set_conv_mode(struct ads1015_data *data, int mode)
928 return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
929 ADS1015_CFG_MOD_MASK,
930 mode << ADS1015_CFG_MOD_SHIFT);
933 static int ads1015_probe(struct i2c_client *client,
934 const struct i2c_device_id *id)
936 struct iio_dev *indio_dev;
937 struct ads1015_data *data;
942 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
946 data = iio_priv(indio_dev);
947 i2c_set_clientdata(client, indio_dev);
949 mutex_init(&data->lock);
951 indio_dev->dev.parent = &client->dev;
952 indio_dev->dev.of_node = client->dev.of_node;
953 indio_dev->name = ADS1015_DRV_NAME;
954 indio_dev->modes = INDIO_DIRECT_MODE;
956 if (client->dev.of_node)
957 chip = (enum chip_ids)of_device_get_match_data(&client->dev);
959 chip = id->driver_data;
962 indio_dev->channels = ads1015_channels;
963 indio_dev->num_channels = ARRAY_SIZE(ads1015_channels);
964 indio_dev->info = &ads1015_info;
965 data->data_rate = (unsigned int *) &ads1015_data_rate;
968 indio_dev->channels = ads1115_channels;
969 indio_dev->num_channels = ARRAY_SIZE(ads1115_channels);
970 indio_dev->info = &ads1115_info;
971 data->data_rate = (unsigned int *) &ads1115_data_rate;
975 data->event_channel = ADS1015_CHANNELS;
977 * Set default lower and upper threshold to min and max value
980 for (i = 0; i < ADS1015_CHANNELS; i++) {
981 int realbits = indio_dev->channels[i].scan_type.realbits;
983 data->thresh_data[i].low_thresh = -1 << (realbits - 1);
984 data->thresh_data[i].high_thresh = (1 << (realbits - 1)) - 1;
987 /* we need to keep this ABI the same as used by hwmon ADS1015 driver */
988 ads1015_get_channels_config(client);
990 data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config);
991 if (IS_ERR(data->regmap)) {
992 dev_err(&client->dev, "Failed to allocate register map\n");
993 return PTR_ERR(data->regmap);
996 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
997 ads1015_trigger_handler,
998 &ads1015_buffer_setup_ops);
1000 dev_err(&client->dev, "iio triggered buffer setup failed\n");
1005 unsigned long irq_trig =
1006 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1007 unsigned int cfg_comp_mask = ADS1015_CFG_COMP_QUE_MASK |
1008 ADS1015_CFG_COMP_LAT_MASK | ADS1015_CFG_COMP_POL_MASK;
1009 unsigned int cfg_comp =
1010 ADS1015_CFG_COMP_DISABLE << ADS1015_CFG_COMP_QUE_SHIFT |
1011 1 << ADS1015_CFG_COMP_LAT_SHIFT;
1014 case IRQF_TRIGGER_LOW:
1015 cfg_comp |= ADS1015_CFG_COMP_POL_LOW <<
1016 ADS1015_CFG_COMP_POL_SHIFT;
1018 case IRQF_TRIGGER_HIGH:
1019 cfg_comp |= ADS1015_CFG_COMP_POL_HIGH <<
1020 ADS1015_CFG_COMP_POL_SHIFT;
1026 ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
1027 cfg_comp_mask, cfg_comp);
1031 ret = devm_request_threaded_irq(&client->dev, client->irq,
1032 NULL, ads1015_event_handler,
1033 irq_trig | IRQF_ONESHOT,
1034 client->name, indio_dev);
1039 ret = ads1015_set_conv_mode(data, ADS1015_CONTINUOUS);
1043 data->conv_invalid = true;
1045 ret = pm_runtime_set_active(&client->dev);
1048 pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS);
1049 pm_runtime_use_autosuspend(&client->dev);
1050 pm_runtime_enable(&client->dev);
1052 ret = iio_device_register(indio_dev);
1054 dev_err(&client->dev, "Failed to register IIO device\n");
1061 static int ads1015_remove(struct i2c_client *client)
1063 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1064 struct ads1015_data *data = iio_priv(indio_dev);
1066 iio_device_unregister(indio_dev);
1068 pm_runtime_disable(&client->dev);
1069 pm_runtime_set_suspended(&client->dev);
1070 pm_runtime_put_noidle(&client->dev);
1072 /* power down single shot mode */
1073 return ads1015_set_conv_mode(data, ADS1015_SINGLESHOT);
1077 static int ads1015_runtime_suspend(struct device *dev)
1079 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1080 struct ads1015_data *data = iio_priv(indio_dev);
1082 return ads1015_set_conv_mode(data, ADS1015_SINGLESHOT);
1085 static int ads1015_runtime_resume(struct device *dev)
1087 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1088 struct ads1015_data *data = iio_priv(indio_dev);
1091 ret = ads1015_set_conv_mode(data, ADS1015_CONTINUOUS);
1093 data->conv_invalid = true;
1099 static const struct dev_pm_ops ads1015_pm_ops = {
1100 SET_RUNTIME_PM_OPS(ads1015_runtime_suspend,
1101 ads1015_runtime_resume, NULL)
1104 static const struct i2c_device_id ads1015_id[] = {
1105 {"ads1015", ADS1015},
1106 {"ads1115", ADS1115},
1109 MODULE_DEVICE_TABLE(i2c, ads1015_id);
1111 static const struct of_device_id ads1015_of_match[] = {
1113 .compatible = "ti,ads1015",
1114 .data = (void *)ADS1015
1117 .compatible = "ti,ads1115",
1118 .data = (void *)ADS1115
1122 MODULE_DEVICE_TABLE(of, ads1015_of_match);
1124 static struct i2c_driver ads1015_driver = {
1126 .name = ADS1015_DRV_NAME,
1127 .of_match_table = ads1015_of_match,
1128 .pm = &ads1015_pm_ops,
1130 .probe = ads1015_probe,
1131 .remove = ads1015_remove,
1132 .id_table = ads1015_id,
1135 module_i2c_driver(ads1015_driver);
1137 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1138 MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
1139 MODULE_LICENSE("GPL v2");