]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/staging/iio/adc/ads1015.c
118b869f804d885fb4f1e43bee085263f16b5840
[sojka/nv-tegra/linux-3.10.git] / drivers / staging / iio / adc / ads1015.c
1 /*
2  * Driver for TI,ADS1015 ADC
3  *
4  * Copyright (c) 2014 - 2015, NVIDIA Corporation. All rights reserved.
5  *
6  * Author: Mallikarjun Kasoju <mkasoju@nvidia.com>
7  *         Laxman Dewangan <ldewangan@nvidia.com>
8  *
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.
13  *
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.
18  */
19 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/iio/iio.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/regmap.h>
25
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
30
31 #define ADS1015_MASK_CONV_START                         BIT(15)
32 #define ADS1015_INPUT_MULTIPLEXER_SHIFT                 12
33 #define ADS1015_INPUT_MULTIPLEXER_MASK                  0x7000
34
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
42
43 #define ADS1015_COTINUOUS_MODE                          0
44 #define ADS1015_SINGLE_SHOT_MODE                        BIT(8)
45
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
54
55 #define ADS1015_TRADITIONAL_COMPARATOR                  0
56 #define ADS1015_WINDOW_COMPARATOR                       BIT(4)
57
58 #define ADS1015_COMPARATOR_POLARITY_ACTIVITY_LOW        0
59 #define ADS1015_COMPARATOR_POLARITY_ACTIVITY_HIGH       BIT(3)
60
61 #define ADS1015_COMPARATOR_NON_LATCHING                 0
62 #define ADS1015_COMPARATOR_LATCHING                     BIT(2)
63
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
71
72 #define ADS1015_CHAN_IIO(chan, name)                            \
73 {                                                               \
74         .datasheet_name = "ads1015-chan-"#name,                 \
75         .type = IIO_VOLTAGE,                                    \
76         .indexed = 1,                                           \
77         .channel = chan,                                        \
78         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |          \
79                         BIT(IIO_CHAN_INFO_PROCESSED),           \
80 }
81
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"),
91 };
92
93 static const struct i2c_device_id ads1015_i2c_id[] = {
94         { "ti,ads1015", 0 },
95         { }
96 };
97 MODULE_DEVICE_TABLE(i2c, ads1015_i2c_id);
98
99 static const struct regmap_config ads1015_regmap_config = {
100         .reg_bits = 8,
101         .val_bits = 16,
102         .max_register = 4,
103 };
104
105 struct ads1015_adc_threshold_ranges {
106         int lower;
107         int upper;
108 };
109
110 struct ads1015_property {
111         int pga;                /* Programmable gain amplifier */
112         int sampling_freq;
113         int comparator_mode;
114         int comparator_queue;
115         int channel_number;
116         int tolerance;
117         struct ads1015_adc_threshold_ranges *threshold_ranges;
118         int num_conditions;
119         int num_retries;
120         int retry_delay_ms;
121         int conv_delay;
122 };
123
124 struct ads1015 {
125         struct device *dev;
126         struct i2c_client *clients;
127         struct regmap *rmap;
128         struct iio_dev *iiodev;
129         struct ads1015_property adc_cont_prop;
130         struct ads1015_property adc_os_prop;
131         u16 cont_config;
132         u16 os_config;
133         bool enable_continuous_mode;
134         bool is_shutdown;
135 };
136
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};
139
140 static int ads1015_write(struct regmap *regmap, unsigned int reg, u16 val)
141 {
142         val = swab16(val & 0xffff);
143         return regmap_raw_write(regmap, reg, &val, 2);
144 }
145
146 static int ads1015_read(struct regmap *regmap, unsigned int reg, u16 *val)
147 {
148         int ret;
149
150         ret = regmap_raw_read(regmap, reg, val, 2);
151         if (ret == 0)
152                 *val = swab16(*val & 0xffff);
153         return ret;
154 }
155
156 static int ads1015_get_sampling_code(struct ads1015 *adc, int sampling_freq,
157                 int *conv_delay_us)
158 {
159         int scode;
160
161         /* Sampling frequency can be enum also */
162         if (sampling_freq <= ADS1015_3300_SPS) {
163                 scode = sampling_freq;
164                 goto conv_delay;
165         }
166
167         for (scode = 0; scode < ARRAY_SIZE(ads1015_samp_frq); ++scode) {
168                 if (sampling_freq <= ads1015_samp_frq[scode])
169                         break;
170         }
171
172 conv_delay:
173         if (scode < ARRAY_SIZE(ads1015_conv_delay)) {
174                 *conv_delay_us = ads1015_conv_delay[scode];
175         } else {
176                 scode = 0;
177                 *conv_delay_us = 7812;
178         }
179
180         return scode;
181 }
182
183 static int ads1015_start_conversion(struct ads1015 *adc, int chan)
184 {
185         u16 channel_mux_val = chan;
186         u16 reg_val;
187         int ret;
188         int retry = 10;
189         int delay = adc->adc_os_prop.conv_delay;
190
191         if (adc->enable_continuous_mode) {
192                 if (chan != adc->adc_cont_prop.channel_number)
193                         reg_val = adc->os_config;
194                 else
195                         reg_val = adc->cont_config;
196         } else {
197                 reg_val = adc->os_config;
198         }
199
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);
204         if (ret < 0) {
205                 dev_err(adc->dev, "Config Reg Write failed %d\n", ret);
206                 return ret;
207         }
208
209         reg_val |= ADS1015_MASK_CONV_START;
210         ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, reg_val);
211         if (ret < 0) {
212                 dev_err(adc->dev, "Config reg write failed %d\n", ret);
213                 return ret;
214         }
215
216         delay = (delay) ? delay : 800;
217         /* Wait for conversion */
218         do {
219                 if (delay >= 1000) {
220                         mdelay(delay / 1000);
221                         udelay(delay % 1000);
222                 } else {
223                         udelay(delay);
224                 }
225                 ret = ads1015_read(adc->rmap, ADS1015_CONFIG_REG, &reg_val);
226                 if (ret < 0) {
227                         dev_err(adc->dev, "Config reg read failed %d\n", ret);
228                         return ret;
229                 }
230                 if (reg_val & ADS1015_MASK_CONV_START)
231                         return 0;
232         } while (--retry > 0);
233         return -ETIMEDOUT;
234 }
235
236 static int ads1015_threshold_update(struct ads1015 *adc, int *adc_val)
237 {
238         struct ads1015_property *adc_cont_prop = &adc->adc_cont_prop;
239         struct ads1015_adc_threshold_ranges *thres_range;
240         u16 reg_val = 0;
241         int ret;
242         int lower, upper;
243         s16 lower_s16;
244         s16 upper_s16;
245         int i;
246         int stable_count = 0;
247         int last_val;
248         int cur_val;
249         int max_retry = adc_cont_prop->num_retries;
250         bool in_valid_range = false;
251
252         ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, &reg_val);
253         if (ret < 0) {
254                 dev_err(adc->dev, "CONVERSION_REG Read failed %d\n", ret);
255                 return ret;
256         }
257
258         last_val = (s16)reg_val;
259         last_val = (last_val / 16);
260
261         do {
262                 mdelay(adc_cont_prop->retry_delay_ms);
263                 ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, &reg_val);
264                 if (ret < 0) {
265                         dev_err(adc->dev,
266                                 "CONVERSION Read failed %d\n", ret);
267                         return ret;
268                 }
269
270                 cur_val = (s16)reg_val;
271                 cur_val = (cur_val / 16);
272
273                 if (cur_val >= (last_val - adc_cont_prop->tolerance) &&
274                         cur_val <= (last_val + adc_cont_prop->tolerance))
275                         stable_count++;
276                 else
277                         stable_count = 0;
278
279                 if (stable_count == 3)
280                         break;
281                 last_val = cur_val;
282         } while (--max_retry > 0);
283
284         lower = -2047;
285         upper = 2047;
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;
293                         break;
294                 }
295
296                 if (thres_range->upper < cur_val) {
297                         if (lower < thres_range->upper)
298                                 lower = thres_range->upper;
299                 }
300
301                 if (thres_range->lower > cur_val) {
302                         if (upper > thres_range->lower)
303                                 upper = thres_range->lower;
304                 }
305         }
306
307         *adc_val = cur_val;
308         lower_s16 = (s16) (lower * 16);
309         upper_s16 = (s16)(upper * 16);
310         if (!in_valid_range) {
311                 dev_info(adc->dev,
312                         "Not in valid threshold range. Val: %d\n", cur_val);
313                 WARN_ON(1);
314         }
315
316         ret = ads1015_write(adc->rmap, ADS1015_LO_THRESH_REG, (u16)lower_s16);
317         if (ret < 0) {
318                 dev_err(adc->dev, "LO_THRESH_REG write failed %d\n", ret);
319                 return ret;
320         }
321
322         ret = ads1015_write(adc->rmap, ADS1015_HI_THRESH_REG, (u16)upper_s16);
323         if (ret < 0) {
324                 dev_err(adc->dev, "HI_THRESH_REG write failed %d\n", ret);
325                 return ret;
326         }
327
328         dev_info(adc->dev,
329                 "ADC TH values LTV: UTV: RV: 0x%04x 0x%04x 0x%04x\n",
330                 (u16)lower_s16, (u16)upper_s16, reg_val);
331         return 0;
332 }
333
334 static int ads1015_read_raw(struct iio_dev *iodev,
335         struct iio_chan_spec const *chan, int *val, int *val2, long mask)
336 {
337         struct ads1015 *adc = iio_priv(iodev);
338         int ret = 0, cs_ret = 0;
339         u16 reg_val = 0;
340         int rval;
341
342         if ((mask != IIO_CHAN_INFO_RAW) && (mask != IIO_CHAN_INFO_PROCESSED)) {
343                 dev_err(adc->dev, "Invalid mask 0x%08lx\n", mask);
344                 return -EINVAL;
345         }
346
347         mutex_lock(&iodev->mlock);
348
349         if (adc->is_shutdown) {
350                 mutex_unlock(&iodev->mlock);
351                 return -EINVAL;
352         }
353
354         if (adc->enable_continuous_mode &&
355                 (chan->channel == adc->adc_cont_prop.channel_number)) {
356                 ret = ads1015_threshold_update(adc, val);
357                 goto done_cont;
358         }
359
360         ret = ads1015_start_conversion(adc, chan->channel);
361         if (ret < 0) {
362                 dev_err(adc->dev, "Start conversion failed %d\n", ret);
363                 goto done;
364         }
365
366         ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, &reg_val);
367         if (ret < 0) {
368                 dev_err(adc->dev, "Reg 0x%x Read failed, %d\n",
369                         ADS1015_CONVERSION_REG, ret);
370                 goto done;
371         }
372         rval = (s16)reg_val;
373         *val = (rval >> 4);
374
375 done:
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,
379                                         adc->cont_config);
380                 if (cs_ret < 0)
381                         dev_err(adc->dev,
382                                 "CONFIG CS mode write failed %d\n", cs_ret);
383         }
384
385 done_cont:
386         mutex_unlock(&iodev->mlock);
387         if (!ret)
388                 return IIO_VAL_INT;
389         return ret;
390 }
391
392 static const struct iio_info ads1015_iio_info = {
393         .read_raw = ads1015_read_raw,
394         .driver_module = THIS_MODULE,
395 };
396
397 static int ads1015_configure(struct ads1015 *adc)
398 {
399         int ret, val;
400         u16 reg_val = 0;
401         u16 os_val = 0;
402
403         /* Set PGA */
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);
407
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);
422
423         ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, reg_val);
424         if (ret < 0) {
425                 dev_err(adc->dev, "CONFIG reg write failed %d\n", ret);
426                 return ret;
427         }
428         adc->cont_config = reg_val;
429
430         if (!adc->enable_continuous_mode)
431                 return 0;
432
433         ret = ads1015_start_conversion(adc, adc->adc_cont_prop.channel_number);
434         if (ret < 0) {
435                 dev_err(adc->dev, "Start conversion failed %d\n", ret);
436                 return ret;
437         }
438         ret = ads1015_threshold_update(adc, &val);
439         if (ret < 0) {
440                 dev_err(adc->dev, "Continuous mode config failed: %d\n", ret);
441                 return ret;
442         }
443
444         /* Configure in continuous mode */
445         adc->cont_config &= ~ADS1015_SINGLE_SHOT_MODE;
446         adc->cont_config |= ADS1015_COTINUOUS_MODE;
447
448         ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, adc->cont_config);
449         if (ret < 0) {
450                 dev_err(adc->dev, "CONFIG reg write failed %d\n", ret);
451                 return ret;
452         }
453
454         /* Set PGA */
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);
471
472         adc->os_config = os_val;
473         return ret;
474 }
475
476 static int ads1015_get_dt_data(struct ads1015 *adc, struct device_node *np)
477 {
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;
482         int ret = 0;
483         u32 val, lower_adc, upper_adc;
484         int nranges, i;
485
486         ret = of_property_read_u32(np, "ti,programmable-gain-amplifier", &val);
487         adc_os_prop->pga = (ret) ? ADS1015_2048MV_AMPLIFIER_GAIN : val;
488
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);
493
494         ret = of_property_read_u32(np, "ti,adc-conversion-delay", &val);
495         if (!ret)
496                 adc_os_prop->conv_delay = val;
497
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;
501
502         ret = of_property_read_u32(np, "ti,comparator-queue", &val);
503         adc_os_prop->comparator_queue = (ret) ? ADS1015_FOUR_CONVERSIONS : val;
504
505         adc->enable_continuous_mode = of_property_read_bool(np,
506                                         "ti,enable-continuous-mode");
507
508         ret = of_property_read_u32(np, "ti,maximum-retries", &val);
509         adc_os_prop->num_retries = (ret) ? 0 : val;
510
511         if (adc_os_prop->num_retries > ADS_1015_MAX_RETRIES)
512                 adc_os_prop->num_retries = ADS_1015_MAX_RETRIES;
513
514         ret = of_property_read_u32(np, "ti,tolerance-val", &val);
515         adc_os_prop->tolerance = (ret) ? 10 : val;
516
517         ret = of_property_read_u32(np, "ti,delay-between-retry-ms", &val);
518         adc_os_prop->retry_delay_ms = (ret) ? 10 : val;
519
520         ret = of_property_read_u32(np, "ti,continuous-channel-number", &val);
521         if (!ret) {
522                 adc_os_prop->channel_number = val;
523         } else {
524                 adc_os_prop->channel_number = -EINVAL;
525                 if (adc->enable_continuous_mode) {
526                         dev_err(adc->dev,
527                                 "Continuous channel number not available\n");
528                         return -EINVAL;
529                 }
530         }
531
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;
543         }
544
545         if (adc_os_prop->channel_number == -EINVAL)
546                 adc_os_prop->channel_number = 0;
547
548         tnode = np;
549         cnode = of_find_node_by_name(np, "continuous-mode");
550         if (!cnode) {
551                 if (adc->enable_continuous_mode)
552                         goto parse_range;
553                 goto parse_done;
554         }
555
556         tnode = cnode;
557         adc->enable_continuous_mode = true;
558
559         ret = of_property_read_u32(cnode, "ti,programmable-gain-amplifier",
560                                 &val);
561         adc_cont_prop->pga = (ret) ? ADS1015_2048MV_AMPLIFIER_GAIN : val;
562
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);
568
569         ret = of_property_read_u32(cnode, "ti,adc-conversion-delay", &val);
570         if (!ret)
571                 adc_cont_prop->conv_delay = val;
572
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;
577
578         ret = of_property_read_u32(cnode, "ti,comparator-queue", &val);
579         adc_cont_prop->comparator_queue = (ret) ? ADS1015_FOUR_CONVERSIONS :
580                                                         val;
581
582         ret = of_property_read_u32(cnode, "ti,maximum-retries", &val);
583         if (!ret) {
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;
587         }
588
589         ret = of_property_read_u32(cnode, "ti,tolerance-val", &val);
590         if (!ret)
591                 adc_cont_prop->tolerance = val;
592
593         ret = of_property_read_u32(cnode, "ti,delay-between-retry-ms", &val);
594         if (!ret)
595                 adc_cont_prop->retry_delay_ms = val;
596
597         ret = of_property_read_u32(cnode, "ti,continuous-channel-number", &val);
598         if (!ret) {
599                 adc_cont_prop->channel_number = val;
600         } else {
601                 dev_err(adc->dev, "Continuous channel not provided\n");
602                 return ret;
603         }
604
605 parse_range:
606         if (adc_cont_prop->channel_number == -EINVAL)
607                 return -EINVAL;
608
609         nranges = of_property_count_u32(tnode, "ti,adc-valid-threshold-ranges");
610         if (nranges <= 0)
611                 return -EINVAL;
612
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)
617                 return -ENOMEM;
618
619         for (i = 0; i < nranges; ++i) {
620                 lower_adc = 0;
621                 upper_adc = 0;
622
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);
627
628                 adc_cont_prop->threshold_ranges[i].lower = (int)lower_adc;
629                 adc_cont_prop->threshold_ranges[i].upper = (int)upper_adc;
630         };
631         adc_cont_prop->num_conditions = nranges;
632
633 parse_done:
634         return 0;
635 }
636
637 static int ads1015_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
638 {
639         struct ads1015 *adc;
640         struct device_node *node = i2c->dev.of_node;
641         struct iio_dev *iodev;
642         int ret;
643
644         if (!node) {
645                 dev_err(&i2c->dev, "Device DT not found\n");
646                 return -ENODEV;
647         }
648
649         iodev = devm_iio_device_alloc(&i2c->dev, sizeof(*adc));
650         if (!iodev) {
651                 dev_err(&i2c->dev, "iio_device_alloc failed\n");
652                 return -ENOMEM;
653         }
654         adc = iio_priv(iodev);
655         adc->dev = &i2c->dev;
656         adc->iiodev = iodev;
657         i2c_set_clientdata(i2c, adc);
658         adc->is_shutdown = false;
659         ret = ads1015_get_dt_data(adc, node);
660         if (ret < 0)
661                 return ret;
662
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);
667                 return ret;
668         }
669
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);
677         if (ret < 0) {
678                 dev_err(adc->dev, "iio_device_register() failed: %d\n", ret);
679                 return ret;
680         }
681
682         ret = ads1015_configure(adc);
683         if (ret < 0) {
684                 dev_err(adc->dev, "ADC configuration failed: %d\n", ret);
685                 return ret;
686         }
687
688         return 0;
689 }
690
691 static void ads1015_shutdown(struct i2c_client *client)
692 {
693         struct ads1015 *adc = i2c_get_clientdata(client);
694         struct iio_dev *iiodev = adc->iiodev;
695
696         mutex_lock(&iiodev->mlock);
697         adc->is_shutdown = true;
698         mutex_unlock(&iiodev->mlock);
699 }
700
701 static const struct of_device_id ads1015_of_match[] = {
702         { .compatible = "ads1015", },
703         {},
704 };
705
706 struct i2c_driver ads1015_driver = {
707         .probe = ads1015_probe,
708         .driver = {
709                 .name = "ads1015",
710                 .owner = THIS_MODULE,
711                 .of_match_table = ads1015_of_match,
712         },
713         .id_table = ads1015_i2c_id,
714         .shutdown = ads1015_shutdown,
715 };
716
717 module_i2c_driver(ads1015_driver);
718
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");