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