2 * Driver for TI,ADS1015 ADC
4 * Copyright (c) 2014 - 2015, NVIDIA Corporation. All rights reserved.
6 * Author: Mallikarjun Kasoju <mkasoju@nvidia.com>
7 * Laxman Dewangan <ldewangan@nvidia.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/iio/iio.h>
22 #include <linux/module.h>
24 #include <linux/regmap.h>
26 #define ADS1015_CONVERSION_REG 0x00
27 #define ADS1015_CONFIG_REG 0x01
28 #define ADS1015_LO_THRESH_REG 0x02
29 #define ADS1015_HI_THRESH_REG 0x03
31 #define ADS1015_MASK_CONV_START BIT(15)
32 #define ADS1015_INPUT_MULTIPLEXER_SHIFT 12
33 #define ADS1015_INPUT_MULTIPLEXER_MASK 0x7000
35 #define ADS1015_AMPLIFIER_GAIN_SHIFT 9
36 #define ADS1015_6144MV_AMPLIFIER_GAIN 0
37 #define ADS1015_4096MV_AMPLIFIER_GAIN 1
38 #define ADS1015_2048MV_AMPLIFIER_GAIN 2
39 #define ADS1015_1024MV_AMPLIFIER_GAIN 3
40 #define ADS1015_0512MV_AMPLIFIER_GAIN 4
41 #define ADS1015_0256MV_AMPLIFIER_GAIN 5
43 #define ADS1015_COTINUOUS_MODE 0
44 #define ADS1015_SINGLE_SHOT_MODE BIT(8)
46 #define ADS1015_DATA_RATE_SHIFT 5
47 #define ADS1015_128_SPS 0
48 #define ADS1015_250_SPS 1
49 #define ADS1015_490_SPS 2
50 #define ADS1015_920_SPS 3
51 #define ADS1015_1600_SPS 4
52 #define ADS1015_2400_SPS 5
53 #define ADS1015_3300_SPS 6
55 #define ADS1015_TRADITIONAL_COMPARATOR 0
56 #define ADS1015_WINDOW_COMPARATOR BIT(4)
58 #define ADS1015_COMPARATOR_POLARITY_ACTIVITY_LOW 0
59 #define ADS1015_COMPARATOR_POLARITY_ACTIVITY_HIGH BIT(3)
61 #define ADS1015_COMPARATOR_NON_LATCHING 0
62 #define ADS1015_COMPARATOR_LATCHING BIT(2)
64 #define ADS1015_COMPARATOR_QUEUE_SHIFT 0
65 #define ADS1015_ONE_CONVERSION 0
66 #define ADS1015_TWO_CONVERSIONS 1
67 #define ADS1015_FOUR_CONVERSIONS 2
68 #define ADS1015_DISABLE_CONVERSION 3
69 #define ADS_1015_MAX_RETRIES 6
70 #define ADS1015_CHANNEL_NAME(_name) "ads1015-chan-"#_name
72 #define ADS1015_CHAN_IIO(chan, name) \
74 .datasheet_name = "ads1015-chan-"#name, \
75 .type = IIO_VOLTAGE, \
78 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
79 BIT(IIO_CHAN_INFO_PROCESSED), \
82 static const struct iio_chan_spec ads1015_iio_channel[] = {
83 ADS1015_CHAN_IIO(0, "in0-in1"),
84 ADS1015_CHAN_IIO(1, "in0-in3"),
85 ADS1015_CHAN_IIO(2, "in1-in3"),
86 ADS1015_CHAN_IIO(3, "in2-in3"),
87 ADS1015_CHAN_IIO(4, "in0-gnd"),
88 ADS1015_CHAN_IIO(5, "in1-gnd"),
89 ADS1015_CHAN_IIO(6, "in2-gnd"),
90 ADS1015_CHAN_IIO(7, "in3-gnd"),
93 static const struct i2c_device_id ads1015_i2c_id[] = {
97 MODULE_DEVICE_TABLE(i2c, ads1015_i2c_id);
99 static const struct regmap_config ads1015_regmap_config = {
105 struct ads1015_adc_threshold_ranges {
110 struct ads1015_property {
111 int pga; /* Programmable gain amplifier */
114 int comparator_queue;
117 struct ads1015_adc_threshold_ranges *threshold_ranges;
126 struct i2c_client *clients;
128 struct iio_dev *iiodev;
129 struct ads1015_property adc_cont_prop;
130 struct ads1015_property adc_os_prop;
133 bool enable_continuous_mode;
137 static int ads1015_samp_frq[] = {128, 250, 490, 920, 1600, 2400};
138 static int ads1015_conv_delay[] = {7812, 4000, 2040, 1087, 625, 416, 303};
140 static int ads1015_write(struct regmap *regmap, unsigned int reg, u16 val)
142 val = swab16(val & 0xffff);
143 return regmap_raw_write(regmap, reg, &val, 2);
146 static int ads1015_read(struct regmap *regmap, unsigned int reg, u16 *val)
150 ret = regmap_raw_read(regmap, reg, val, 2);
152 *val = swab16(*val & 0xffff);
156 static int ads1015_get_sampling_code(struct ads1015 *adc, int sampling_freq,
161 /* Sampling frequency can be enum also */
162 if (sampling_freq <= ADS1015_3300_SPS) {
163 scode = sampling_freq;
167 for (scode = 0; scode < ARRAY_SIZE(ads1015_samp_frq); ++scode) {
168 if (sampling_freq <= ads1015_samp_frq[scode])
173 if (scode < ARRAY_SIZE(ads1015_conv_delay)) {
174 *conv_delay_us = ads1015_conv_delay[scode];
177 *conv_delay_us = 7812;
183 static int ads1015_start_conversion(struct ads1015 *adc, int chan)
185 u16 channel_mux_val = chan;
189 int delay = adc->adc_os_prop.conv_delay;
191 if (adc->enable_continuous_mode) {
192 if (chan != adc->adc_cont_prop.channel_number)
193 reg_val = adc->os_config;
195 reg_val = adc->cont_config;
197 reg_val = adc->os_config;
200 reg_val |= ADS1015_SINGLE_SHOT_MODE;
201 reg_val &= ~ADS1015_INPUT_MULTIPLEXER_MASK;
202 reg_val |= (channel_mux_val << ADS1015_INPUT_MULTIPLEXER_SHIFT);
203 ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, reg_val);
205 dev_err(adc->dev, "Config Reg Write failed %d\n", ret);
209 reg_val |= ADS1015_MASK_CONV_START;
210 ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, reg_val);
212 dev_err(adc->dev, "Config reg write failed %d\n", ret);
216 delay = (delay) ? delay : 800;
217 /* Wait for conversion */
220 mdelay(delay / 1000);
221 udelay(delay % 1000);
225 ret = ads1015_read(adc->rmap, ADS1015_CONFIG_REG, ®_val);
227 dev_err(adc->dev, "Config reg read failed %d\n", ret);
230 if (reg_val & ADS1015_MASK_CONV_START)
232 } while (--retry > 0);
236 static int ads1015_threshold_update(struct ads1015 *adc, int *adc_val)
238 struct ads1015_property *adc_cont_prop = &adc->adc_cont_prop;
239 struct ads1015_adc_threshold_ranges *thres_range;
246 int stable_count = 0;
249 int max_retry = adc_cont_prop->num_retries;
250 bool in_valid_range = false;
252 ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, ®_val);
254 dev_err(adc->dev, "CONVERSION_REG Read failed %d\n", ret);
258 last_val = (s16)reg_val;
259 last_val = (last_val / 16);
262 mdelay(adc_cont_prop->retry_delay_ms);
263 ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, ®_val);
266 "CONVERSION Read failed %d\n", ret);
270 cur_val = (s16)reg_val;
271 cur_val = (cur_val / 16);
273 if (cur_val >= (last_val - adc_cont_prop->tolerance) &&
274 cur_val <= (last_val + adc_cont_prop->tolerance))
279 if (stable_count == 3)
282 } while (--max_retry > 0);
286 for (i = 0; i < adc_cont_prop->num_conditions; i++) {
287 thres_range = &adc_cont_prop->threshold_ranges[i];
288 if (thres_range->lower <= cur_val &&
289 thres_range->upper >= cur_val) {
290 lower = thres_range->lower;
291 upper = thres_range->upper;
292 in_valid_range = true;
296 if (thres_range->upper < cur_val) {
297 if (lower < thres_range->upper)
298 lower = thres_range->upper;
301 if (thres_range->lower > cur_val) {
302 if (upper > thres_range->lower)
303 upper = thres_range->lower;
308 lower_s16 = (s16) (lower * 16);
309 upper_s16 = (s16)(upper * 16);
310 if (!in_valid_range) {
312 "Not in valid threshold range. Val: %d\n", cur_val);
316 ret = ads1015_write(adc->rmap, ADS1015_LO_THRESH_REG, (u16)lower_s16);
318 dev_err(adc->dev, "LO_THRESH_REG write failed %d\n", ret);
322 ret = ads1015_write(adc->rmap, ADS1015_HI_THRESH_REG, (u16)upper_s16);
324 dev_err(adc->dev, "HI_THRESH_REG write failed %d\n", ret);
329 "ADC TH values LTV: UTV: RV: 0x%04x 0x%04x 0x%04x\n",
330 (u16)lower_s16, (u16)upper_s16, reg_val);
334 static int ads1015_read_raw(struct iio_dev *iodev,
335 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
337 struct ads1015 *adc = iio_priv(iodev);
338 int ret = 0, cs_ret = 0;
342 if ((mask != IIO_CHAN_INFO_RAW) && (mask != IIO_CHAN_INFO_PROCESSED)) {
343 dev_err(adc->dev, "Invalid mask 0x%08lx\n", mask);
347 mutex_lock(&iodev->mlock);
349 if (adc->is_shutdown) {
350 mutex_unlock(&iodev->mlock);
354 if (adc->enable_continuous_mode &&
355 (chan->channel == adc->adc_cont_prop.channel_number)) {
356 ret = ads1015_threshold_update(adc, val);
360 ret = ads1015_start_conversion(adc, chan->channel);
362 dev_err(adc->dev, "Start conversion failed %d\n", ret);
366 ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, ®_val);
368 dev_err(adc->dev, "Reg 0x%x Read failed, %d\n",
369 ADS1015_CONVERSION_REG, ret);
376 /* if device is enabled in cotinuous mode set it here again */
377 if (adc->enable_continuous_mode) {
378 cs_ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG,
382 "CONFIG CS mode write failed %d\n", cs_ret);
386 mutex_unlock(&iodev->mlock);
392 static const struct iio_info ads1015_iio_info = {
393 .read_raw = ads1015_read_raw,
394 .driver_module = THIS_MODULE,
397 static int ads1015_configure(struct ads1015 *adc)
404 reg_val |= (adc->adc_cont_prop.pga << ADS1015_AMPLIFIER_GAIN_SHIFT);
405 reg_val |= (adc->adc_cont_prop.channel_number <<
406 ADS1015_INPUT_MULTIPLEXER_SHIFT);
408 /* Set operation mode */
409 reg_val |= ADS1015_SINGLE_SHOT_MODE;
410 /* Set Sampling frequence rate */
411 reg_val |= (adc->adc_cont_prop.sampling_freq <<
412 ADS1015_DATA_RATE_SHIFT);
413 /* Set Comparator mode */
414 reg_val |= adc->adc_cont_prop.comparator_mode;
415 /* Set Comparator polarity to active low */
416 reg_val |= ADS1015_COMPARATOR_POLARITY_ACTIVITY_LOW;
417 /* Set Comparator latch */
418 reg_val |= ADS1015_COMPARATOR_NON_LATCHING;
419 /* Assert after one conversion */
420 reg_val |= (adc->adc_cont_prop.comparator_queue <<
421 ADS1015_COMPARATOR_QUEUE_SHIFT);
423 ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, reg_val);
425 dev_err(adc->dev, "CONFIG reg write failed %d\n", ret);
428 adc->cont_config = reg_val;
430 if (!adc->enable_continuous_mode)
433 ret = ads1015_start_conversion(adc, adc->adc_cont_prop.channel_number);
435 dev_err(adc->dev, "Start conversion failed %d\n", ret);
438 ret = ads1015_threshold_update(adc, &val);
440 dev_err(adc->dev, "Continuous mode config failed: %d\n", ret);
444 /* Configure in continuous mode */
445 adc->cont_config &= ~ADS1015_SINGLE_SHOT_MODE;
446 adc->cont_config |= ADS1015_COTINUOUS_MODE;
448 ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, adc->cont_config);
450 dev_err(adc->dev, "CONFIG reg write failed %d\n", ret);
455 os_val = (adc->adc_os_prop.pga << ADS1015_AMPLIFIER_GAIN_SHIFT);
456 os_val |= (adc->adc_os_prop.channel_number <<
457 ADS1015_INPUT_MULTIPLEXER_SHIFT);
458 /* Set operation mode */
459 os_val |= ADS1015_SINGLE_SHOT_MODE;
460 /* Set Sampling frequence rate */
461 os_val |= (adc->adc_os_prop.sampling_freq << ADS1015_DATA_RATE_SHIFT);
462 /* Set Comparator mode */
463 os_val |= adc->adc_os_prop.comparator_mode;
464 /* Set Comparator polarity to active low */
465 os_val |= ADS1015_COMPARATOR_POLARITY_ACTIVITY_LOW;
466 /* Set Comparator latch */
467 os_val |= ADS1015_COMPARATOR_NON_LATCHING;
468 /* Assert after one conversion */
469 os_val |= (adc->adc_os_prop.comparator_queue <<
470 ADS1015_COMPARATOR_QUEUE_SHIFT);
472 adc->os_config = os_val;
476 static int ads1015_get_dt_data(struct ads1015 *adc, struct device_node *np)
478 struct ads1015_property *adc_cont_prop = &adc->adc_cont_prop;
479 struct ads1015_property *adc_os_prop = &adc->adc_os_prop;
480 struct device_node *cnode;
481 struct device_node *tnode;
483 u32 val, lower_adc, upper_adc;
486 ret = of_property_read_u32(np, "ti,programmable-gain-amplifier", &val);
487 adc_os_prop->pga = (ret) ? ADS1015_2048MV_AMPLIFIER_GAIN : val;
489 ret = of_property_read_u32(np, "sampling-frequency", &val);
490 adc_os_prop->sampling_freq = (ret) ? ADS1015_920_SPS : val;
491 adc_os_prop->sampling_freq = ads1015_get_sampling_code(adc,
492 adc_os_prop->sampling_freq, &adc_os_prop->conv_delay);
494 ret = of_property_read_u32(np, "ti,adc-conversion-delay", &val);
496 adc_os_prop->conv_delay = val;
498 ret = of_property_read_bool(np, "ti,traditional-comparator-mode");
499 adc_os_prop->comparator_mode = (ret) ? ADS1015_TRADITIONAL_COMPARATOR :
500 ADS1015_WINDOW_COMPARATOR;
502 ret = of_property_read_u32(np, "ti,comparator-queue", &val);
503 adc_os_prop->comparator_queue = (ret) ? ADS1015_FOUR_CONVERSIONS : val;
505 adc->enable_continuous_mode = of_property_read_bool(np,
506 "ti,enable-continuous-mode");
508 ret = of_property_read_u32(np, "ti,maximum-retries", &val);
509 adc_os_prop->num_retries = (ret) ? 0 : val;
511 if (adc_os_prop->num_retries > ADS_1015_MAX_RETRIES)
512 adc_os_prop->num_retries = ADS_1015_MAX_RETRIES;
514 ret = of_property_read_u32(np, "ti,tolerance-val", &val);
515 adc_os_prop->tolerance = (ret) ? 10 : val;
517 ret = of_property_read_u32(np, "ti,delay-between-retry-ms", &val);
518 adc_os_prop->retry_delay_ms = (ret) ? 10 : val;
520 ret = of_property_read_u32(np, "ti,continuous-channel-number", &val);
522 adc_os_prop->channel_number = val;
524 adc_os_prop->channel_number = -EINVAL;
525 if (adc->enable_continuous_mode) {
527 "Continuous channel number not available\n");
532 /* If continuous mode then initialised with default */
533 if (adc->enable_continuous_mode) {
534 adc_cont_prop->pga = adc_os_prop->pga;
535 adc_cont_prop->sampling_freq = adc_os_prop->sampling_freq;
536 adc_cont_prop->conv_delay = adc_os_prop->conv_delay;
537 adc_cont_prop->comparator_mode = adc_os_prop->comparator_mode;
538 adc_cont_prop->comparator_queue = adc_os_prop->comparator_queue;
539 adc_cont_prop->num_retries = adc_os_prop->num_retries;
540 adc_cont_prop->tolerance = adc_os_prop->tolerance;
541 adc_cont_prop->retry_delay_ms = adc_os_prop->retry_delay_ms;
542 adc_cont_prop->channel_number = adc_os_prop->channel_number;
545 if (adc_os_prop->channel_number == -EINVAL)
546 adc_os_prop->channel_number = 0;
549 cnode = of_find_node_by_name(np, "continuous-mode");
551 if (adc->enable_continuous_mode)
557 adc->enable_continuous_mode = true;
559 ret = of_property_read_u32(cnode, "ti,programmable-gain-amplifier",
561 adc_cont_prop->pga = (ret) ? ADS1015_2048MV_AMPLIFIER_GAIN : val;
563 ret = of_property_read_u32(cnode, "sampling-frequency", &val);
564 adc_cont_prop->sampling_freq = (ret) ? ADS1015_920_SPS : val;
565 adc_cont_prop->sampling_freq = ads1015_get_sampling_code(adc,
566 adc_cont_prop->sampling_freq,
567 &adc_cont_prop->conv_delay);
569 ret = of_property_read_u32(cnode, "ti,adc-conversion-delay", &val);
571 adc_cont_prop->conv_delay = val;
573 ret = of_property_read_bool(cnode, "ti,traditional-comparator-mode");
574 adc_cont_prop->comparator_mode = (ret) ?
575 ADS1015_TRADITIONAL_COMPARATOR :
576 ADS1015_WINDOW_COMPARATOR;
578 ret = of_property_read_u32(cnode, "ti,comparator-queue", &val);
579 adc_cont_prop->comparator_queue = (ret) ? ADS1015_FOUR_CONVERSIONS :
582 ret = of_property_read_u32(cnode, "ti,maximum-retries", &val);
584 adc_cont_prop->num_retries = val;
585 if (adc_cont_prop->num_retries > ADS_1015_MAX_RETRIES)
586 adc_cont_prop->num_retries = ADS_1015_MAX_RETRIES;
589 ret = of_property_read_u32(cnode, "ti,tolerance-val", &val);
591 adc_cont_prop->tolerance = val;
593 ret = of_property_read_u32(cnode, "ti,delay-between-retry-ms", &val);
595 adc_cont_prop->retry_delay_ms = val;
597 ret = of_property_read_u32(cnode, "ti,continuous-channel-number", &val);
599 adc_cont_prop->channel_number = val;
601 dev_err(adc->dev, "Continuous channel not provided\n");
606 if (adc_cont_prop->channel_number == -EINVAL)
609 nranges = of_property_count_u32(tnode, "ti,adc-valid-threshold-ranges");
613 nranges = (nranges / 2);
614 adc_cont_prop->threshold_ranges = devm_kzalloc(adc->dev, nranges *
615 sizeof(*adc_cont_prop->threshold_ranges), GFP_KERNEL);
616 if (!adc_cont_prop->threshold_ranges)
619 for (i = 0; i < nranges; ++i) {
623 of_property_read_u32_index(tnode,
624 "ti,adc-valid-threshold-ranges", i * 2 + 0, &lower_adc);
625 of_property_read_u32_index(tnode,
626 "ti,adc-valid-threshold-ranges", i * 2 + 1, &upper_adc);
628 adc_cont_prop->threshold_ranges[i].lower = (int)lower_adc;
629 adc_cont_prop->threshold_ranges[i].upper = (int)upper_adc;
631 adc_cont_prop->num_conditions = nranges;
637 static int ads1015_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
640 struct device_node *node = i2c->dev.of_node;
641 struct iio_dev *iodev;
645 dev_err(&i2c->dev, "Device DT not found\n");
649 iodev = devm_iio_device_alloc(&i2c->dev, sizeof(*adc));
651 dev_err(&i2c->dev, "iio_device_alloc failed\n");
654 adc = iio_priv(iodev);
655 adc->dev = &i2c->dev;
657 i2c_set_clientdata(i2c, adc);
658 adc->is_shutdown = false;
659 ret = ads1015_get_dt_data(adc, node);
663 adc->rmap = devm_regmap_init_i2c(i2c, &ads1015_regmap_config);
664 if (IS_ERR(adc->rmap)) {
665 ret = PTR_ERR(adc->rmap);
666 dev_err(adc->dev, "regmap_init failed %d\n", ret);
670 iodev->name = "ads1015";
671 iodev->dev.parent = &i2c->dev;
672 iodev->info = &ads1015_iio_info;
673 iodev->modes = INDIO_DIRECT_MODE;
674 iodev->channels = ads1015_iio_channel;
675 iodev->num_channels = ARRAY_SIZE(ads1015_iio_channel);
676 ret = devm_iio_device_register(&i2c->dev, iodev);
678 dev_err(adc->dev, "iio_device_register() failed: %d\n", ret);
682 ret = ads1015_configure(adc);
684 dev_err(adc->dev, "ADC configuration failed: %d\n", ret);
691 static void ads1015_shutdown(struct i2c_client *client)
693 struct ads1015 *adc = i2c_get_clientdata(client);
694 struct iio_dev *iiodev = adc->iiodev;
696 mutex_lock(&iiodev->mlock);
697 adc->is_shutdown = true;
698 mutex_unlock(&iiodev->mlock);
701 static const struct of_device_id ads1015_of_match[] = {
702 { .compatible = "ads1015", },
706 struct i2c_driver ads1015_driver = {
707 .probe = ads1015_probe,
710 .owner = THIS_MODULE,
711 .of_match_table = ads1015_of_match,
713 .id_table = ads1015_i2c_id,
714 .shutdown = ads1015_shutdown,
717 module_i2c_driver(ads1015_driver);
719 MODULE_DESCRIPTION("ads1015 driver");
720 MODULE_AUTHOR("Mallikarjun Kasoju <mkasoju@nvidia.com>");
721 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
722 MODULE_ALIAS("i2c:ads1015");
723 MODULE_LICENSE("GPL v2");