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/init.h>
16 #include <linux/irq.h>
17 #include <linux/i2c.h>
18 #include <linux/property.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/iio/iio.h>
25 #include <linux/iio/types.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/events.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/trigger_consumer.h>
32 #define ADS1015_DRV_NAME "ads1015"
34 #define ADS1015_CHANNELS 8
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
79 struct ads1015_chip_data
{
80 struct iio_chan_spec
const *channels
;
82 const struct iio_info
*info
;
84 const int data_rate_len
;
90 enum ads1015_channels
{
91 ADS1015_AIN0_AIN1
= 0,
102 static const int ads1015_data_rate
[] = {
103 128, 250, 490, 920, 1600, 2400, 3300, 3300
106 static const int ads1115_data_rate
[] = {
107 8, 16, 32, 64, 128, 250, 475, 860
111 * Translation from PGA bits to full-scale positive and negative input voltage
114 static const int ads1015_fullscale_range
[] = {
115 6144, 4096, 2048, 1024, 512, 256, 256, 256
118 static const int ads1015_scale
[] = { /* 12bit ADC */
127 static const int ads1115_scale
[] = { /* 16bit ADC */
137 * Translation from COMP_QUE field value to the number of successive readings
138 * exceed the threshold values before an interrupt is generated
140 static const int ads1015_comp_queue
[] = { 1, 2, 4 };
142 static const struct iio_event_spec ads1015_events
[] = {
144 .type
= IIO_EV_TYPE_THRESH
,
145 .dir
= IIO_EV_DIR_RISING
,
146 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
147 BIT(IIO_EV_INFO_ENABLE
),
149 .type
= IIO_EV_TYPE_THRESH
,
150 .dir
= IIO_EV_DIR_FALLING
,
151 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
153 .type
= IIO_EV_TYPE_THRESH
,
154 .dir
= IIO_EV_DIR_EITHER
,
155 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
156 BIT(IIO_EV_INFO_PERIOD
),
161 * Compile-time check whether _fitbits can accommodate up to _testbits
162 * bits. Returns _fitbits on success, fails to compile otherwise.
164 * The test works such that it multiplies constant _fitbits by constant
165 * double-negation of size of a non-empty structure, i.e. it multiplies
166 * constant _fitbits by constant 1 in each successful compilation case.
167 * The non-empty structure may contain C11 _Static_assert(), make use of
168 * this and place the kernel variant of static assert in there, so that
169 * it performs the compile-time check for _testbits <= _fitbits. Note
170 * that it is not possible to directly use static_assert in compound
171 * statements, hence this convoluted construct.
173 #define FIT_CHECK(_testbits, _fitbits) \
177 static_assert((_testbits) <= (_fitbits)); \
182 #define ADS1015_V_CHAN(_chan, _addr, _realbits, _shift, _event_spec, _num_event_specs) { \
183 .type = IIO_VOLTAGE, \
187 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
188 BIT(IIO_CHAN_INFO_SCALE) | \
189 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
190 .info_mask_shared_by_all_available = \
191 BIT(IIO_CHAN_INFO_SCALE) | \
192 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
193 .scan_index = _addr, \
196 .realbits = (_realbits), \
197 .storagebits = FIT_CHECK((_realbits) + (_shift), 16), \
199 .endianness = IIO_CPU, \
201 .event_spec = (_event_spec), \
202 .num_event_specs = (_num_event_specs), \
203 .datasheet_name = "AIN"#_chan, \
206 #define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr, _realbits, _shift, _event_spec, _num_event_specs) { \
207 .type = IIO_VOLTAGE, \
212 .channel2 = _chan2, \
213 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
214 BIT(IIO_CHAN_INFO_SCALE) | \
215 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
216 .info_mask_shared_by_all_available = \
217 BIT(IIO_CHAN_INFO_SCALE) | \
218 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
219 .scan_index = _addr, \
222 .realbits = (_realbits), \
223 .storagebits = FIT_CHECK((_realbits) + (_shift), 16), \
225 .endianness = IIO_CPU, \
227 .event_spec = (_event_spec), \
228 .num_event_specs = (_num_event_specs), \
229 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
232 struct ads1015_channel_data
{
235 unsigned int data_rate
;
238 struct ads1015_thresh_data
{
239 unsigned int comp_queue
;
244 struct ads1015_data
{
245 struct regmap
*regmap
;
247 * Protects ADC ops, e.g: concurrent sysfs/buffered
248 * data reads, configuration updates
251 struct ads1015_channel_data channel_data
[ADS1015_CHANNELS
];
253 unsigned int event_channel
;
254 unsigned int comp_mode
;
255 struct ads1015_thresh_data thresh_data
[ADS1015_CHANNELS
];
257 const struct ads1015_chip_data
*chip
;
259 * Set to true when the ADC is switched to the continuous-conversion
260 * mode and exits from a power-down state. This flag is used to avoid
261 * getting the stale result from the conversion register.
266 static bool ads1015_event_channel_enabled(struct ads1015_data
*data
)
268 return (data
->event_channel
!= ADS1015_CHANNELS
);
271 static void ads1015_event_channel_enable(struct ads1015_data
*data
, int chan
,
274 WARN_ON(ads1015_event_channel_enabled(data
));
276 data
->event_channel
= chan
;
277 data
->comp_mode
= comp_mode
;
280 static void ads1015_event_channel_disable(struct ads1015_data
*data
, int chan
)
282 data
->event_channel
= ADS1015_CHANNELS
;
285 static const struct regmap_range ads1015_writeable_ranges
[] = {
286 regmap_reg_range(ADS1015_CFG_REG
, ADS1015_HI_THRESH_REG
),
289 static const struct regmap_access_table ads1015_writeable_table
= {
290 .yes_ranges
= ads1015_writeable_ranges
,
291 .n_yes_ranges
= ARRAY_SIZE(ads1015_writeable_ranges
),
294 static const struct regmap_config ads1015_regmap_config
= {
297 .max_register
= ADS1015_HI_THRESH_REG
,
298 .wr_table
= &ads1015_writeable_table
,
301 static const struct regmap_range tla2024_writeable_ranges
[] = {
302 regmap_reg_range(ADS1015_CFG_REG
, ADS1015_CFG_REG
),
305 static const struct regmap_access_table tla2024_writeable_table
= {
306 .yes_ranges
= tla2024_writeable_ranges
,
307 .n_yes_ranges
= ARRAY_SIZE(tla2024_writeable_ranges
),
310 static const struct regmap_config tla2024_regmap_config
= {
313 .max_register
= ADS1015_CFG_REG
,
314 .wr_table
= &tla2024_writeable_table
,
317 static const struct iio_chan_spec ads1015_channels
[] = {
318 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1
, 12, 4,
319 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
320 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3
, 12, 4,
321 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
322 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3
, 12, 4,
323 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
324 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3
, 12, 4,
325 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
326 ADS1015_V_CHAN(0, ADS1015_AIN0
, 12, 4,
327 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
328 ADS1015_V_CHAN(1, ADS1015_AIN1
, 12, 4,
329 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
330 ADS1015_V_CHAN(2, ADS1015_AIN2
, 12, 4,
331 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
332 ADS1015_V_CHAN(3, ADS1015_AIN3
, 12, 4,
333 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
334 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP
),
337 static const struct iio_chan_spec ads1115_channels
[] = {
338 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1
, 16, 0,
339 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
340 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3
, 16, 0,
341 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
342 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3
, 16, 0,
343 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
344 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3
, 16, 0,
345 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
346 ADS1015_V_CHAN(0, ADS1015_AIN0
, 16, 0,
347 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
348 ADS1015_V_CHAN(1, ADS1015_AIN1
, 16, 0,
349 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
350 ADS1015_V_CHAN(2, ADS1015_AIN2
, 16, 0,
351 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
352 ADS1015_V_CHAN(3, ADS1015_AIN3
, 16, 0,
353 ads1015_events
, ARRAY_SIZE(ads1015_events
)),
354 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP
),
357 static const struct iio_chan_spec tla2024_channels
[] = {
358 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1
, 12, 4, NULL
, 0),
359 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3
, 12, 4, NULL
, 0),
360 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3
, 12, 4, NULL
, 0),
361 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3
, 12, 4, NULL
, 0),
362 ADS1015_V_CHAN(0, ADS1015_AIN0
, 12, 4, NULL
, 0),
363 ADS1015_V_CHAN(1, ADS1015_AIN1
, 12, 4, NULL
, 0),
364 ADS1015_V_CHAN(2, ADS1015_AIN2
, 12, 4, NULL
, 0),
365 ADS1015_V_CHAN(3, ADS1015_AIN3
, 12, 4, NULL
, 0),
366 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP
),
371 static int ads1015_set_power_state(struct ads1015_data
*data
, bool on
)
374 struct device
*dev
= regmap_get_device(data
->regmap
);
377 ret
= pm_runtime_resume_and_get(dev
);
379 pm_runtime_mark_last_busy(dev
);
380 ret
= pm_runtime_put_autosuspend(dev
);
383 return ret
< 0 ? ret
: 0;
386 #else /* !CONFIG_PM */
388 static int ads1015_set_power_state(struct ads1015_data
*data
, bool on
)
393 #endif /* !CONFIG_PM */
396 int ads1015_get_adc_result(struct ads1015_data
*data
, int chan
, int *val
)
398 const int *data_rate
= data
->chip
->data_rate
;
399 int ret
, pga
, dr
, dr_old
, conv_time
;
400 unsigned int old
, mask
, cfg
;
402 if (chan
< 0 || chan
>= ADS1015_CHANNELS
)
405 ret
= regmap_read(data
->regmap
, ADS1015_CFG_REG
, &old
);
409 pga
= data
->channel_data
[chan
].pga
;
410 dr
= data
->channel_data
[chan
].data_rate
;
411 mask
= ADS1015_CFG_MUX_MASK
| ADS1015_CFG_PGA_MASK
|
413 cfg
= chan
<< ADS1015_CFG_MUX_SHIFT
| pga
<< ADS1015_CFG_PGA_SHIFT
|
414 dr
<< ADS1015_CFG_DR_SHIFT
;
416 if (ads1015_event_channel_enabled(data
)) {
417 mask
|= ADS1015_CFG_COMP_QUE_MASK
| ADS1015_CFG_COMP_MODE_MASK
;
418 cfg
|= data
->thresh_data
[chan
].comp_queue
<<
419 ADS1015_CFG_COMP_QUE_SHIFT
|
421 ADS1015_CFG_COMP_MODE_SHIFT
;
424 cfg
= (old
& ~mask
) | (cfg
& mask
);
426 ret
= regmap_write(data
->regmap
, ADS1015_CFG_REG
, cfg
);
429 data
->conv_invalid
= true;
431 if (data
->conv_invalid
) {
432 dr_old
= (old
& ADS1015_CFG_DR_MASK
) >> ADS1015_CFG_DR_SHIFT
;
433 conv_time
= DIV_ROUND_UP(USEC_PER_SEC
, data_rate
[dr_old
]);
434 conv_time
+= DIV_ROUND_UP(USEC_PER_SEC
, data_rate
[dr
]);
435 conv_time
+= conv_time
/ 10; /* 10% internal clock inaccuracy */
436 usleep_range(conv_time
, conv_time
+ 1);
437 data
->conv_invalid
= false;
440 return regmap_read(data
->regmap
, ADS1015_CONV_REG
, val
);
443 static irqreturn_t
ads1015_trigger_handler(int irq
, void *p
)
445 struct iio_poll_func
*pf
= p
;
446 struct iio_dev
*indio_dev
= pf
->indio_dev
;
447 struct ads1015_data
*data
= iio_priv(indio_dev
);
448 /* Ensure natural alignment of timestamp */
451 s64 timestamp
__aligned(8);
455 memset(&scan
, 0, sizeof(scan
));
457 mutex_lock(&data
->lock
);
458 chan
= find_first_bit(indio_dev
->active_scan_mask
,
459 iio_get_masklength(indio_dev
));
460 ret
= ads1015_get_adc_result(data
, chan
, &res
);
462 mutex_unlock(&data
->lock
);
467 mutex_unlock(&data
->lock
);
469 iio_push_to_buffers_with_timestamp(indio_dev
, &scan
,
470 iio_get_time_ns(indio_dev
));
473 iio_trigger_notify_done(indio_dev
->trig
);
478 static int ads1015_set_scale(struct ads1015_data
*data
,
479 struct iio_chan_spec
const *chan
,
480 int scale
, int uscale
)
483 int fullscale
= div_s64((scale
* 1000000LL + uscale
) <<
484 (chan
->scan_type
.realbits
- 1), 1000000);
486 for (i
= 0; i
< ARRAY_SIZE(ads1015_fullscale_range
); i
++) {
487 if (ads1015_fullscale_range
[i
] == fullscale
) {
488 data
->channel_data
[chan
->address
].pga
= i
;
496 static int ads1015_set_data_rate(struct ads1015_data
*data
, int chan
, int rate
)
500 for (i
= 0; i
< data
->chip
->data_rate_len
; i
++) {
501 if (data
->chip
->data_rate
[i
] == rate
) {
502 data
->channel_data
[chan
].data_rate
= i
;
510 static int ads1015_read_avail(struct iio_dev
*indio_dev
,
511 struct iio_chan_spec
const *chan
,
512 const int **vals
, int *type
, int *length
,
515 struct ads1015_data
*data
= iio_priv(indio_dev
);
517 if (chan
->type
!= IIO_VOLTAGE
)
521 case IIO_CHAN_INFO_SCALE
:
522 *type
= IIO_VAL_FRACTIONAL_LOG2
;
523 *vals
= data
->chip
->scale
;
524 *length
= data
->chip
->scale_len
;
525 return IIO_AVAIL_LIST
;
526 case IIO_CHAN_INFO_SAMP_FREQ
:
528 *vals
= data
->chip
->data_rate
;
529 *length
= data
->chip
->data_rate_len
;
530 return IIO_AVAIL_LIST
;
536 static int ads1015_read_raw(struct iio_dev
*indio_dev
,
537 struct iio_chan_spec
const *chan
, int *val
,
538 int *val2
, long mask
)
541 struct ads1015_data
*data
= iio_priv(indio_dev
);
543 mutex_lock(&data
->lock
);
545 case IIO_CHAN_INFO_RAW
:
546 ret
= iio_device_claim_direct_mode(indio_dev
);
550 if (ads1015_event_channel_enabled(data
) &&
551 data
->event_channel
!= chan
->address
) {
556 ret
= ads1015_set_power_state(data
, true);
560 ret
= ads1015_get_adc_result(data
, chan
->address
, val
);
562 ads1015_set_power_state(data
, false);
566 *val
= sign_extend32(*val
>> chan
->scan_type
.shift
,
567 chan
->scan_type
.realbits
- 1);
569 ret
= ads1015_set_power_state(data
, false);
575 iio_device_release_direct_mode(indio_dev
);
577 case IIO_CHAN_INFO_SCALE
:
578 idx
= data
->channel_data
[chan
->address
].pga
;
579 *val
= ads1015_fullscale_range
[idx
];
580 *val2
= chan
->scan_type
.realbits
- 1;
581 ret
= IIO_VAL_FRACTIONAL_LOG2
;
583 case IIO_CHAN_INFO_SAMP_FREQ
:
584 idx
= data
->channel_data
[chan
->address
].data_rate
;
585 *val
= data
->chip
->data_rate
[idx
];
592 mutex_unlock(&data
->lock
);
597 static int ads1015_write_raw(struct iio_dev
*indio_dev
,
598 struct iio_chan_spec
const *chan
, int val
,
601 struct ads1015_data
*data
= iio_priv(indio_dev
);
604 mutex_lock(&data
->lock
);
606 case IIO_CHAN_INFO_SCALE
:
607 ret
= ads1015_set_scale(data
, chan
, val
, val2
);
609 case IIO_CHAN_INFO_SAMP_FREQ
:
610 ret
= ads1015_set_data_rate(data
, chan
->address
, val
);
616 mutex_unlock(&data
->lock
);
621 static int ads1015_read_event(struct iio_dev
*indio_dev
,
622 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
623 enum iio_event_direction dir
, enum iio_event_info info
, int *val
,
626 struct ads1015_data
*data
= iio_priv(indio_dev
);
628 unsigned int comp_queue
;
632 mutex_lock(&data
->lock
);
635 case IIO_EV_INFO_VALUE
:
636 *val
= (dir
== IIO_EV_DIR_RISING
) ?
637 data
->thresh_data
[chan
->address
].high_thresh
:
638 data
->thresh_data
[chan
->address
].low_thresh
;
641 case IIO_EV_INFO_PERIOD
:
642 dr
= data
->channel_data
[chan
->address
].data_rate
;
643 comp_queue
= data
->thresh_data
[chan
->address
].comp_queue
;
644 period
= ads1015_comp_queue
[comp_queue
] *
645 USEC_PER_SEC
/ data
->chip
->data_rate
[dr
];
647 *val
= period
/ USEC_PER_SEC
;
648 *val2
= period
% USEC_PER_SEC
;
649 ret
= IIO_VAL_INT_PLUS_MICRO
;
656 mutex_unlock(&data
->lock
);
661 static int ads1015_write_event(struct iio_dev
*indio_dev
,
662 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
663 enum iio_event_direction dir
, enum iio_event_info info
, int val
,
666 struct ads1015_data
*data
= iio_priv(indio_dev
);
667 const int *data_rate
= data
->chip
->data_rate
;
668 int realbits
= chan
->scan_type
.realbits
;
674 mutex_lock(&data
->lock
);
677 case IIO_EV_INFO_VALUE
:
678 if (val
>= 1 << (realbits
- 1) || val
< -1 << (realbits
- 1)) {
682 if (dir
== IIO_EV_DIR_RISING
)
683 data
->thresh_data
[chan
->address
].high_thresh
= val
;
685 data
->thresh_data
[chan
->address
].low_thresh
= val
;
687 case IIO_EV_INFO_PERIOD
:
688 dr
= data
->channel_data
[chan
->address
].data_rate
;
689 period
= val
* USEC_PER_SEC
+ val2
;
691 for (i
= 0; i
< ARRAY_SIZE(ads1015_comp_queue
) - 1; i
++) {
692 if (period
<= ads1015_comp_queue
[i
] *
693 USEC_PER_SEC
/ data_rate
[dr
])
696 data
->thresh_data
[chan
->address
].comp_queue
= i
;
703 mutex_unlock(&data
->lock
);
708 static int ads1015_read_event_config(struct iio_dev
*indio_dev
,
709 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
710 enum iio_event_direction dir
)
712 struct ads1015_data
*data
= iio_priv(indio_dev
);
715 mutex_lock(&data
->lock
);
716 if (data
->event_channel
== chan
->address
) {
718 case IIO_EV_DIR_RISING
:
721 case IIO_EV_DIR_EITHER
:
722 ret
= (data
->comp_mode
== ADS1015_CFG_COMP_MODE_WINDOW
);
729 mutex_unlock(&data
->lock
);
734 static int ads1015_enable_event_config(struct ads1015_data
*data
,
735 const struct iio_chan_spec
*chan
, int comp_mode
)
737 int low_thresh
= data
->thresh_data
[chan
->address
].low_thresh
;
738 int high_thresh
= data
->thresh_data
[chan
->address
].high_thresh
;
742 if (ads1015_event_channel_enabled(data
)) {
743 if (data
->event_channel
!= chan
->address
||
744 (data
->comp_mode
== ADS1015_CFG_COMP_MODE_TRAD
&&
745 comp_mode
== ADS1015_CFG_COMP_MODE_WINDOW
))
751 if (comp_mode
== ADS1015_CFG_COMP_MODE_TRAD
) {
752 low_thresh
= max(-1 << (chan
->scan_type
.realbits
- 1),
755 ret
= regmap_write(data
->regmap
, ADS1015_LO_THRESH_REG
,
756 low_thresh
<< chan
->scan_type
.shift
);
760 ret
= regmap_write(data
->regmap
, ADS1015_HI_THRESH_REG
,
761 high_thresh
<< chan
->scan_type
.shift
);
765 ret
= ads1015_set_power_state(data
, true);
769 ads1015_event_channel_enable(data
, chan
->address
, comp_mode
);
771 ret
= ads1015_get_adc_result(data
, chan
->address
, &val
);
773 ads1015_event_channel_disable(data
, chan
->address
);
774 ads1015_set_power_state(data
, false);
780 static int ads1015_disable_event_config(struct ads1015_data
*data
,
781 const struct iio_chan_spec
*chan
, int comp_mode
)
785 if (!ads1015_event_channel_enabled(data
))
788 if (data
->event_channel
!= chan
->address
)
791 if (data
->comp_mode
== ADS1015_CFG_COMP_MODE_TRAD
&&
792 comp_mode
== ADS1015_CFG_COMP_MODE_WINDOW
)
795 ret
= regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
796 ADS1015_CFG_COMP_QUE_MASK
,
797 ADS1015_CFG_COMP_DISABLE
<<
798 ADS1015_CFG_COMP_QUE_SHIFT
);
802 ads1015_event_channel_disable(data
, chan
->address
);
804 return ads1015_set_power_state(data
, false);
807 static int ads1015_write_event_config(struct iio_dev
*indio_dev
,
808 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
809 enum iio_event_direction dir
, bool state
)
811 struct ads1015_data
*data
= iio_priv(indio_dev
);
813 int comp_mode
= (dir
== IIO_EV_DIR_EITHER
) ?
814 ADS1015_CFG_COMP_MODE_WINDOW
: ADS1015_CFG_COMP_MODE_TRAD
;
816 mutex_lock(&data
->lock
);
818 /* Prevent from enabling both buffer and event at a time */
819 ret
= iio_device_claim_direct_mode(indio_dev
);
821 mutex_unlock(&data
->lock
);
826 ret
= ads1015_enable_event_config(data
, chan
, comp_mode
);
828 ret
= ads1015_disable_event_config(data
, chan
, comp_mode
);
830 iio_device_release_direct_mode(indio_dev
);
831 mutex_unlock(&data
->lock
);
836 static irqreturn_t
ads1015_event_handler(int irq
, void *priv
)
838 struct iio_dev
*indio_dev
= priv
;
839 struct ads1015_data
*data
= iio_priv(indio_dev
);
843 /* Clear the latched ALERT/RDY pin */
844 ret
= regmap_read(data
->regmap
, ADS1015_CONV_REG
, &val
);
848 if (ads1015_event_channel_enabled(data
)) {
849 enum iio_event_direction dir
;
852 dir
= data
->comp_mode
== ADS1015_CFG_COMP_MODE_TRAD
?
853 IIO_EV_DIR_RISING
: IIO_EV_DIR_EITHER
;
854 code
= IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, data
->event_channel
,
855 IIO_EV_TYPE_THRESH
, dir
);
856 iio_push_event(indio_dev
, code
, iio_get_time_ns(indio_dev
));
862 static int ads1015_buffer_preenable(struct iio_dev
*indio_dev
)
864 struct ads1015_data
*data
= iio_priv(indio_dev
);
866 /* Prevent from enabling both buffer and event at a time */
867 if (ads1015_event_channel_enabled(data
))
870 return ads1015_set_power_state(iio_priv(indio_dev
), true);
873 static int ads1015_buffer_postdisable(struct iio_dev
*indio_dev
)
875 return ads1015_set_power_state(iio_priv(indio_dev
), false);
878 static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops
= {
879 .preenable
= ads1015_buffer_preenable
,
880 .postdisable
= ads1015_buffer_postdisable
,
881 .validate_scan_mask
= &iio_validate_scan_mask_onehot
,
884 static const struct iio_info ads1015_info
= {
885 .read_avail
= ads1015_read_avail
,
886 .read_raw
= ads1015_read_raw
,
887 .write_raw
= ads1015_write_raw
,
888 .read_event_value
= ads1015_read_event
,
889 .write_event_value
= ads1015_write_event
,
890 .read_event_config
= ads1015_read_event_config
,
891 .write_event_config
= ads1015_write_event_config
,
894 static const struct iio_info tla2024_info
= {
895 .read_avail
= ads1015_read_avail
,
896 .read_raw
= ads1015_read_raw
,
897 .write_raw
= ads1015_write_raw
,
900 static int ads1015_client_get_channels_config(struct i2c_client
*client
)
902 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
903 struct ads1015_data
*data
= iio_priv(indio_dev
);
904 struct device
*dev
= &client
->dev
;
907 device_for_each_child_node_scoped(dev
, node
) {
909 unsigned int channel
;
910 unsigned int pga
= ADS1015_DEFAULT_PGA
;
911 unsigned int data_rate
= ADS1015_DEFAULT_DATA_RATE
;
913 if (fwnode_property_read_u32(node
, "reg", &pval
)) {
914 dev_err(dev
, "invalid reg on %pfw\n", node
);
919 if (channel
>= ADS1015_CHANNELS
) {
920 dev_err(dev
, "invalid channel index %d on %pfw\n",
925 if (!fwnode_property_read_u32(node
, "ti,gain", &pval
)) {
928 dev_err(dev
, "invalid gain on %pfw\n", node
);
933 if (!fwnode_property_read_u32(node
, "ti,datarate", &pval
)) {
936 dev_err(dev
, "invalid data_rate on %pfw\n", node
);
941 data
->channel_data
[channel
].pga
= pga
;
942 data
->channel_data
[channel
].data_rate
= data_rate
;
947 return i
< 0 ? -EINVAL
: 0;
950 static void ads1015_get_channels_config(struct i2c_client
*client
)
954 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
955 struct ads1015_data
*data
= iio_priv(indio_dev
);
957 if (!ads1015_client_get_channels_config(client
))
960 /* fallback on default configuration */
961 for (k
= 0; k
< ADS1015_CHANNELS
; ++k
) {
962 data
->channel_data
[k
].pga
= ADS1015_DEFAULT_PGA
;
963 data
->channel_data
[k
].data_rate
= ADS1015_DEFAULT_DATA_RATE
;
967 static int ads1015_set_conv_mode(struct ads1015_data
*data
, int mode
)
969 return regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
970 ADS1015_CFG_MOD_MASK
,
971 mode
<< ADS1015_CFG_MOD_SHIFT
);
974 static int ads1015_probe(struct i2c_client
*client
)
976 const struct ads1015_chip_data
*chip
;
977 struct iio_dev
*indio_dev
;
978 struct ads1015_data
*data
;
982 chip
= i2c_get_match_data(client
);
984 return dev_err_probe(&client
->dev
, -EINVAL
, "Unknown chip\n");
986 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
990 data
= iio_priv(indio_dev
);
991 i2c_set_clientdata(client
, indio_dev
);
993 mutex_init(&data
->lock
);
995 indio_dev
->name
= ADS1015_DRV_NAME
;
996 indio_dev
->modes
= INDIO_DIRECT_MODE
;
998 indio_dev
->channels
= chip
->channels
;
999 indio_dev
->num_channels
= chip
->num_channels
;
1000 indio_dev
->info
= chip
->info
;
1002 data
->event_channel
= ADS1015_CHANNELS
;
1005 * Set default lower and upper threshold to min and max value
1008 for (i
= 0; i
< ADS1015_CHANNELS
; i
++) {
1009 int realbits
= indio_dev
->channels
[i
].scan_type
.realbits
;
1011 data
->thresh_data
[i
].low_thresh
= -1 << (realbits
- 1);
1012 data
->thresh_data
[i
].high_thresh
= (1 << (realbits
- 1)) - 1;
1015 /* we need to keep this ABI the same as used by hwmon ADS1015 driver */
1016 ads1015_get_channels_config(client
);
1018 data
->regmap
= devm_regmap_init_i2c(client
, chip
->has_comparator
?
1019 &ads1015_regmap_config
:
1020 &tla2024_regmap_config
);
1021 if (IS_ERR(data
->regmap
)) {
1022 dev_err(&client
->dev
, "Failed to allocate register map\n");
1023 return PTR_ERR(data
->regmap
);
1026 ret
= devm_iio_triggered_buffer_setup(&client
->dev
, indio_dev
, NULL
,
1027 ads1015_trigger_handler
,
1028 &ads1015_buffer_setup_ops
);
1030 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
1034 if (client
->irq
&& chip
->has_comparator
) {
1035 unsigned long irq_trig
= irq_get_trigger_type(client
->irq
);
1036 unsigned int cfg_comp_mask
= ADS1015_CFG_COMP_QUE_MASK
|
1037 ADS1015_CFG_COMP_LAT_MASK
| ADS1015_CFG_COMP_POL_MASK
;
1038 unsigned int cfg_comp
=
1039 ADS1015_CFG_COMP_DISABLE
<< ADS1015_CFG_COMP_QUE_SHIFT
|
1040 1 << ADS1015_CFG_COMP_LAT_SHIFT
;
1043 case IRQF_TRIGGER_FALLING
:
1044 case IRQF_TRIGGER_LOW
:
1045 cfg_comp
|= ADS1015_CFG_COMP_POL_LOW
<<
1046 ADS1015_CFG_COMP_POL_SHIFT
;
1048 case IRQF_TRIGGER_HIGH
:
1049 case IRQF_TRIGGER_RISING
:
1050 cfg_comp
|= ADS1015_CFG_COMP_POL_HIGH
<<
1051 ADS1015_CFG_COMP_POL_SHIFT
;
1057 ret
= regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
1058 cfg_comp_mask
, cfg_comp
);
1062 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1063 NULL
, ads1015_event_handler
,
1064 irq_trig
| IRQF_ONESHOT
,
1065 client
->name
, indio_dev
);
1070 ret
= ads1015_set_conv_mode(data
, ADS1015_CONTINUOUS
);
1074 data
->conv_invalid
= true;
1076 ret
= pm_runtime_set_active(&client
->dev
);
1079 pm_runtime_set_autosuspend_delay(&client
->dev
, ADS1015_SLEEP_DELAY_MS
);
1080 pm_runtime_use_autosuspend(&client
->dev
);
1081 pm_runtime_enable(&client
->dev
);
1083 ret
= iio_device_register(indio_dev
);
1085 dev_err(&client
->dev
, "Failed to register IIO device\n");
1092 static void ads1015_remove(struct i2c_client
*client
)
1094 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1095 struct ads1015_data
*data
= iio_priv(indio_dev
);
1098 iio_device_unregister(indio_dev
);
1100 pm_runtime_disable(&client
->dev
);
1101 pm_runtime_set_suspended(&client
->dev
);
1103 /* power down single shot mode */
1104 ret
= ads1015_set_conv_mode(data
, ADS1015_SINGLESHOT
);
1106 dev_warn(&client
->dev
, "Failed to power down (%pe)\n",
1111 static int ads1015_runtime_suspend(struct device
*dev
)
1113 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1114 struct ads1015_data
*data
= iio_priv(indio_dev
);
1116 return ads1015_set_conv_mode(data
, ADS1015_SINGLESHOT
);
1119 static int ads1015_runtime_resume(struct device
*dev
)
1121 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1122 struct ads1015_data
*data
= iio_priv(indio_dev
);
1125 ret
= ads1015_set_conv_mode(data
, ADS1015_CONTINUOUS
);
1127 data
->conv_invalid
= true;
1133 static const struct dev_pm_ops ads1015_pm_ops
= {
1134 SET_RUNTIME_PM_OPS(ads1015_runtime_suspend
,
1135 ads1015_runtime_resume
, NULL
)
1138 static const struct ads1015_chip_data ads1015_data
= {
1139 .channels
= ads1015_channels
,
1140 .num_channels
= ARRAY_SIZE(ads1015_channels
),
1141 .info
= &ads1015_info
,
1142 .data_rate
= ads1015_data_rate
,
1143 .data_rate_len
= ARRAY_SIZE(ads1015_data_rate
),
1144 .scale
= ads1015_scale
,
1145 .scale_len
= ARRAY_SIZE(ads1015_scale
),
1146 .has_comparator
= true,
1149 static const struct ads1015_chip_data ads1115_data
= {
1150 .channels
= ads1115_channels
,
1151 .num_channels
= ARRAY_SIZE(ads1115_channels
),
1152 .info
= &ads1015_info
,
1153 .data_rate
= ads1115_data_rate
,
1154 .data_rate_len
= ARRAY_SIZE(ads1115_data_rate
),
1155 .scale
= ads1115_scale
,
1156 .scale_len
= ARRAY_SIZE(ads1115_scale
),
1157 .has_comparator
= true,
1160 static const struct ads1015_chip_data tla2024_data
= {
1161 .channels
= tla2024_channels
,
1162 .num_channels
= ARRAY_SIZE(tla2024_channels
),
1163 .info
= &tla2024_info
,
1164 .data_rate
= ads1015_data_rate
,
1165 .data_rate_len
= ARRAY_SIZE(ads1015_data_rate
),
1166 .scale
= ads1015_scale
,
1167 .scale_len
= ARRAY_SIZE(ads1015_scale
),
1168 .has_comparator
= false,
1171 static const struct i2c_device_id ads1015_id
[] = {
1172 { "ads1015", (kernel_ulong_t
)&ads1015_data
},
1173 { "ads1115", (kernel_ulong_t
)&ads1115_data
},
1174 { "tla2024", (kernel_ulong_t
)&tla2024_data
},
1177 MODULE_DEVICE_TABLE(i2c
, ads1015_id
);
1179 static const struct of_device_id ads1015_of_match
[] = {
1180 { .compatible
= "ti,ads1015", .data
= &ads1015_data
},
1181 { .compatible
= "ti,ads1115", .data
= &ads1115_data
},
1182 { .compatible
= "ti,tla2024", .data
= &tla2024_data
},
1185 MODULE_DEVICE_TABLE(of
, ads1015_of_match
);
1187 static struct i2c_driver ads1015_driver
= {
1189 .name
= ADS1015_DRV_NAME
,
1190 .of_match_table
= ads1015_of_match
,
1191 .pm
= &ads1015_pm_ops
,
1193 .probe
= ads1015_probe
,
1194 .remove
= ads1015_remove
,
1195 .id_table
= ads1015_id
,
1198 module_i2c_driver(ads1015_driver
);
1200 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1201 MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
1202 MODULE_LICENSE("GPL v2");