staging: iio: adc: increase delay before reading config REG
[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_OPERATION_MODE_SHIFT                    8
44 #define ADS1015_COTINUOUS_MODE                          0
45 #define ADS1015_SINGLE_SHOT_MODE                        1
46
47 #define ADS1015_DATA_RATE_SHIFT                         5
48 #define ADS1015_128_SPS                                 0
49 #define ADS1015_250_SPS                                 1
50 #define ADS1015_490_SPS                                 2
51 #define ADS1015_920_SPS                                 3
52 #define ADS1015_1600_SPS                                4
53 #define ADS1015_2400_SPS                                5
54 #define ADS1015_3300_SPS                                6
55
56 #define ADS1015_COMPARATOR_MODE_SHIFT                   4
57 #define ADS1015_TRADITIONAL_COMPARATOR                  0
58 #define ADS1015_WINDOW_COMPARATOR                       1
59
60 #define ADS1015_COMPARATOR_POLARITY_SHIFT               3
61 #define ADS1015_COMPARATOR_POLARITY_ACTIVITY_LOW        0
62 #define ADS1015_COMPARATOR_POLARITY_ACTIVITY_HIGH       1
63
64 #define ADS1015_COMPARATOR_LATCHING_SHIFT               2
65 #define ADS1015_COMPARATOR_NON_LATCHING                 0
66 #define ADS1015_COMPARATOR_LATCHING                     1
67
68 #define ADS1015_COMPARATOR_QUEUE_SHIFT                  0
69 #define ADS1015_ONE_CONVERSION                          0
70 #define ADS1015_TWO_CONVERSIONS                         1
71 #define ADS1015_FOUR_CONVERSIONS                        2
72 #define ADS1015_DISABLE_CONVERSION                      3
73 #define ADS_1015_MAX_RETRIES                    6
74 #define ADS1015_CHANNEL_NAME(_name)     "ads1015-chan-"#_name
75
76 #define ADS1015_CHAN_IIO(chan, name)                            \
77 {                                                               \
78         .datasheet_name = "ads1015-chan-"#name,                 \
79         .type = IIO_VOLTAGE,                                    \
80         .indexed = 1,                                           \
81         .channel = chan,                                        \
82         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |          \
83                         BIT(IIO_CHAN_INFO_PROCESSED),           \
84 }
85
86 static const struct iio_chan_spec ads1015_iio_channel[] = {
87         ADS1015_CHAN_IIO(0, "in0-in1"),
88         ADS1015_CHAN_IIO(1, "in0-in3"),
89         ADS1015_CHAN_IIO(2, "in1-in3"),
90         ADS1015_CHAN_IIO(3, "in2-in3"),
91         ADS1015_CHAN_IIO(4, "in0-gnd"),
92         ADS1015_CHAN_IIO(5, "in1-gnd"),
93         ADS1015_CHAN_IIO(6, "in2-gnd"),
94         ADS1015_CHAN_IIO(7, "in3-gnd"),
95 };
96
97 static const struct i2c_device_id ads1015_i2c_id[] = {
98         { "ti,ads1015", 0 },
99         { }
100 };
101 MODULE_DEVICE_TABLE(i2c, ads1015_i2c_id);
102
103 static const struct regmap_config ads1015_regmap_config = {
104         .reg_bits = 8,
105         .val_bits = 16,
106         .max_register = 4,
107 };
108
109 struct ads1015_adc_threshold_ranges {
110         int lower;
111         int upper;
112 };
113
114 struct ads1015_property {
115         bool is_continuous_mode;
116         int pga;                /* Programmable gain amplifier */
117         int sampling_freq;
118         int comparator_mode;
119         int comparator_queue;
120         int channel_number;
121         int tolerance;
122         struct ads1015_adc_threshold_ranges *threshold_ranges;
123         int num_conditions;
124         int num_retries;
125         int retry_delay_ms;
126 };
127
128 struct ads1015 {
129         struct device *dev;
130         struct i2c_client *clients;
131         struct regmap *rmap;
132         struct ads1015_property adc_prop;
133         struct ads1015_property adc_os_prop;
134         struct mutex lock;
135         u16 config;
136         u16 os_config;
137         bool continuous_dt_node;
138         bool is_shutdown;
139 };
140
141 static int ads1015_write(struct regmap *regmap, unsigned int reg, u16 val)
142 {
143         val = swab16(val & 0xffff);
144         return regmap_raw_write(regmap, reg, &val, 2);
145 }
146
147 static int ads1015_read(struct regmap *regmap, unsigned int reg, u16 *val)
148 {
149         int ret;
150
151         ret = regmap_raw_read(regmap, reg, val, 2);
152         if (ret == 0)
153                 *val = swab16(*val & 0xffff);
154         return ret;
155 }
156
157 static int ads1015_start_conversion(struct ads1015 *adc, int chan)
158 {
159         u16 channel_mux_val = chan;
160         u16 reg_val;
161         int ret;
162         int timeout = 10;
163
164         if (adc->continuous_dt_node && (chan != adc->adc_prop.channel_number))
165                 reg_val = adc->os_config;
166         else
167                 reg_val = adc->config;
168
169         reg_val |= (ADS1015_SINGLE_SHOT_MODE << ADS1015_OPERATION_MODE_SHIFT);
170         reg_val &= ~ADS1015_INPUT_MULTIPLEXER_MASK;
171         reg_val |= (channel_mux_val << ADS1015_INPUT_MULTIPLEXER_SHIFT);
172         ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, reg_val);
173         if (ret < 0) {
174                 dev_err(adc->dev, "Config Reg Write failed %d\n", ret);
175                 return ret;
176         }
177
178         reg_val |= ADS1015_MASK_CONV_START;
179         ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, reg_val);
180         if (ret < 0) {
181                 dev_err(adc->dev, "Config reg write failed %d\n", ret);
182                 return ret;
183         }
184
185         /* Wait for conversion */
186         do {
187                 udelay(800);
188                 ret = ads1015_read(adc->rmap, ADS1015_CONFIG_REG, &reg_val);
189                 if (ret < 0) {
190                         dev_err(adc->dev, "Config reg read failed %d\n", ret);
191                         return ret;
192                 }
193                 if (reg_val & ADS1015_MASK_CONV_START)
194                         return 0;
195         } while (--timeout > 0);
196         return -ETIMEDOUT;
197 }
198
199 static int ads1015_threshold_update(struct ads1015 *adc, int *adc_val)
200 {
201         struct ads1015_property *adc_prop = &adc->adc_prop;
202         struct ads1015_adc_threshold_ranges *thres_range;
203         u16 reg_val = 0;
204         int ret;
205         int lower, upper;
206         s16 lower_s16;
207         s16 upper_s16;
208         int i;
209         int stable_count = 0;
210         int last_val;
211         int cur_val;
212         int max_retry = adc_prop->num_retries;
213         bool in_valid_range = false;
214
215         ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, &reg_val);
216         if (ret < 0) {
217                 dev_err(adc->dev, "CONVERSION_REG Read failed %d\n", ret);
218                 return ret;
219         }
220
221         last_val = (s16)reg_val;
222         last_val = (last_val / 16);
223
224         do {
225                 mdelay(adc_prop->retry_delay_ms);
226                 ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, &reg_val);
227                 if (ret < 0) {
228                         dev_err(adc->dev,
229                                 "CONVERSION Read failed %d\n", ret);
230                         return ret;
231                 }
232
233                 cur_val = (s16)reg_val;
234                 cur_val = (cur_val / 16);
235
236                 if (cur_val >= (last_val - adc_prop->tolerance) &&
237                         cur_val <= (last_val + adc_prop->tolerance))
238                         stable_count++;
239                 else
240                         stable_count = 0;
241
242                 if (stable_count == 3)
243                         break;
244                 last_val = cur_val;
245         } while (--max_retry > 0);
246
247         lower = -2047;
248         upper = 2047;
249         for (i = 0; i < adc_prop->num_conditions; i++) {
250                 thres_range = &adc_prop->threshold_ranges[i];
251                 if (thres_range->lower <= cur_val &&
252                         thres_range->upper >= cur_val) {
253                         lower = thres_range->lower;
254                         upper = thres_range->upper;
255                         in_valid_range = true;
256                         break;
257                 }
258
259                 if (thres_range->upper < cur_val) {
260                         if (lower < thres_range->upper)
261                                 lower = thres_range->upper;
262                 }
263
264                 if (thres_range->lower > cur_val) {
265                         if (upper > thres_range->lower)
266                                 upper = thres_range->lower;
267                 }
268         }
269
270         *adc_val = cur_val;
271         lower_s16 = (s16) (lower * 16);
272         upper_s16 = (s16)(upper * 16);
273         if (!in_valid_range) {
274                 dev_info(adc->dev,
275                         "Not in valid threshold range. Val: %d\n", cur_val);
276                 WARN_ON(1);
277         }
278
279         ret = ads1015_write(adc->rmap, ADS1015_LO_THRESH_REG, (u16)lower_s16);
280         if (ret < 0) {
281                 dev_err(adc->dev, "LO_THRESH_REG write failed %d\n", ret);
282                 return ret;
283         }
284
285         ret = ads1015_write(adc->rmap, ADS1015_HI_THRESH_REG, (u16)upper_s16);
286         if (ret < 0) {
287                 dev_err(adc->dev, "HI_THRESH_REG write failed %d\n", ret);
288                 return ret;
289         }
290
291         dev_info(adc->dev,
292                 "ADC TH values LTV: UTV: RV: 0x%04x 0x%04x 0x%04x\n",
293                 (u16)lower_s16, (u16)upper_s16, reg_val);
294         return 0;
295 }
296
297 static int ads1015_read_raw(struct iio_dev *iodev,
298         struct iio_chan_spec const *chan, int *val, int *val2, long mask)
299 {
300         struct ads1015 *adc = iio_priv(iodev);
301         int ret = 0, cs_ret = 0;
302         u16 reg_val = 0;
303         int rval;
304
305         if ((mask != IIO_CHAN_INFO_RAW) && (mask != IIO_CHAN_INFO_PROCESSED)) {
306                 dev_err(adc->dev, "Invalid mask 0x%08lx\n", mask);
307                 return -EINVAL;
308         }
309
310         mutex_lock(&adc->lock);
311         if (adc->is_shutdown) {
312                 mutex_unlock(&adc->lock);
313                 return -EINVAL;
314         }
315         mutex_unlock(&adc->lock);
316
317         mutex_lock(&iodev->mlock);
318
319         if ((adc->adc_prop.is_continuous_mode) &&
320                 (chan->channel == adc->adc_prop.channel_number)) {
321                 ret = ads1015_threshold_update(adc, val);
322                 goto done;
323         }
324
325         ret = ads1015_start_conversion(adc, chan->channel);
326         if (ret < 0) {
327                 dev_err(adc->dev, "Start conversion failed %d\n", ret);
328                 goto done;
329         }
330
331         ret = ads1015_read(adc->rmap, ADS1015_CONVERSION_REG, &reg_val);
332         if (ret < 0) {
333                 dev_err(adc->dev, "Reg 0x%x Read failed, %d\n",
334                         ADS1015_CONVERSION_REG, ret);
335                 goto done;
336         }
337         rval = (s16)reg_val;
338         *val = (rval >> 4);
339
340 done:
341         /* if device is enabled in cotinuous mode set it here again */
342         if (adc->adc_prop.is_continuous_mode) {
343                 cs_ret = ads1015_write(adc->rmap,
344                                         ADS1015_CONFIG_REG, adc->config);
345                 if (cs_ret < 0)
346                         dev_err(adc->dev,
347                                 "CONFIG CS mode write failed %d\n", cs_ret);
348         }
349         mutex_unlock(&iodev->mlock);
350         if (!ret)
351                 return IIO_VAL_INT;
352         return ret;
353 }
354
355 static const struct iio_info ads1015_iio_info = {
356         .read_raw = ads1015_read_raw,
357         .driver_module = THIS_MODULE,
358 };
359
360 static int ads1015_configure(struct ads1015 *adc)
361 {
362         int ret, val;
363         u16 reg_val = 0;
364         u16 reg_os_val = 0;
365
366         /* Set PGA */
367         reg_val |= (adc->adc_prop.pga << ADS1015_AMPLIFIER_GAIN_SHIFT);
368         reg_val |= (adc->adc_prop.channel_number <<
369                                 ADS1015_INPUT_MULTIPLEXER_SHIFT);
370
371         /* Set operation mode */
372         reg_val |= (ADS1015_SINGLE_SHOT_MODE << ADS1015_OPERATION_MODE_SHIFT);
373         /* Set Sampling frequence rate */
374         reg_val |= (adc->adc_prop.sampling_freq << ADS1015_DATA_RATE_SHIFT);
375         /* Set Comparator mode */
376         reg_val |= (adc->adc_prop.comparator_mode <<
377                                 ADS1015_COMPARATOR_MODE_SHIFT);
378         /* Set Comparator polarity to active low */
379         reg_val |= (ADS1015_COMPARATOR_POLARITY_ACTIVITY_LOW <<
380                                         ADS1015_COMPARATOR_POLARITY_SHIFT);
381         /* Set Comparator latch */
382         reg_val |= (ADS1015_COMPARATOR_NON_LATCHING <<
383                                 ADS1015_COMPARATOR_LATCHING_SHIFT);
384         /* Assert after one conversion */
385         reg_val |= (adc->adc_prop.comparator_queue <<
386                                 ADS1015_COMPARATOR_QUEUE_SHIFT);
387
388         ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, reg_val);
389         if (ret < 0) {
390                 dev_err(adc->dev, "CONFIG reg write failed %d\n", ret);
391                 return ret;
392         }
393         adc->config = reg_val;
394
395         if (!adc->adc_prop.is_continuous_mode)
396                 return 0;
397
398         ret = ads1015_start_conversion(adc, adc->adc_prop.channel_number);
399         if (ret < 0) {
400                 dev_err(adc->dev, "Start conversion failed %d\n", ret);
401                 return ret;
402         }
403         ret = ads1015_threshold_update(adc, &val);
404         if (ret < 0) {
405                 dev_err(adc->dev, "Continuous mode config failed: %d\n", ret);
406                 return ret;
407         }
408
409         /* Configure in continuous mode */
410         adc->config &= ~(1 << ADS1015_OPERATION_MODE_SHIFT);
411         adc->config |= (ADS1015_COTINUOUS_MODE << ADS1015_OPERATION_MODE_SHIFT);
412
413         ret = ads1015_write(adc->rmap, ADS1015_CONFIG_REG, adc->config);
414         if (ret < 0) {
415                 dev_err(adc->dev, "CONFIG reg write failed %d\n", ret);
416                 return ret;
417         }
418
419         if (adc->continuous_dt_node) {
420                 /* Set PGA */
421                 reg_os_val |= (adc->adc_os_prop.pga <<
422                                         ADS1015_AMPLIFIER_GAIN_SHIFT);
423                 reg_os_val |= (adc->adc_os_prop.channel_number <<
424                                         ADS1015_INPUT_MULTIPLEXER_SHIFT);
425                 /* Set operation mode */
426                 reg_os_val |= (ADS1015_SINGLE_SHOT_MODE <<
427                                         ADS1015_OPERATION_MODE_SHIFT);
428                 /* Set Sampling frequence rate */
429                 reg_os_val |= (adc->adc_os_prop.sampling_freq <<
430                                         ADS1015_DATA_RATE_SHIFT);
431                 /* Set Comparator mode */
432                 reg_os_val |= (adc->adc_os_prop.comparator_mode <<
433                                         ADS1015_COMPARATOR_MODE_SHIFT);
434                 /* Set Comparator polarity to active low */
435                 reg_os_val |= (ADS1015_COMPARATOR_POLARITY_ACTIVITY_LOW <<
436                                         ADS1015_COMPARATOR_POLARITY_SHIFT);
437                 /* Set Comparator latch */
438                 reg_os_val |= (ADS1015_COMPARATOR_NON_LATCHING <<
439                                         ADS1015_COMPARATOR_LATCHING_SHIFT);
440                 /* Assert after one conversion */
441                 reg_os_val |= (adc->adc_os_prop.comparator_queue <<
442                                         ADS1015_COMPARATOR_QUEUE_SHIFT);
443
444                 adc->os_config = reg_os_val;
445         }
446
447         return ret;
448 }
449
450 static int ads1015_get_dt_data(struct ads1015 *adc, struct device_node *np)
451 {
452         struct ads1015_property *adc_prop = &adc->adc_prop;
453         struct ads1015_property *adc_os_prop = &adc->adc_os_prop;
454         struct device_node *cnode;
455         struct device_node *tnode;
456         int ret = 0;
457         u32 val, lower_adc, upper_adc;
458         int nranges, i;
459
460         ret = of_property_read_u32(np, "ti,programmable-gain-amplifier", &val);
461         adc_os_prop->pga = (ret) ? ADS1015_2048MV_AMPLIFIER_GAIN : val;
462
463         ret = of_property_read_u32(np, "sampling-frequency", &val);
464         adc_os_prop->sampling_freq = (ret) ? ADS1015_920_SPS : val;
465
466         ret = of_property_read_u32(np, "ti,comparator-mode", &val);
467         adc_os_prop->comparator_mode = (ret) ? ADS1015_WINDOW_COMPARATOR : val;
468
469         ret = of_property_read_u32(np, "ti,comparator-queue", &val);
470         adc_os_prop->comparator_queue = (ret) ? ADS1015_FOUR_CONVERSIONS : val;
471
472         adc_os_prop->is_continuous_mode = of_property_read_bool(np,
473                                         "ti,enable-continuous-mode");
474
475         ret = of_property_read_u32(np, "ti,maximum-retries", &val);
476         adc_os_prop->num_retries = (ret) ? 0 : val;
477
478         if (adc_os_prop->num_retries > ADS_1015_MAX_RETRIES)
479                 adc_os_prop->num_retries = ADS_1015_MAX_RETRIES;
480
481         ret = of_property_read_u32(np, "ti,tolerance-val", &val);
482         adc_os_prop->tolerance = (ret) ? 10 : val;
483
484         ret = of_property_read_u32(np, "ti,delay-between-retry-ms", &val);
485         adc_os_prop->retry_delay_ms = (ret) ? 10 : val;
486
487         ret = of_property_read_u32(np, "ti,continuous-channel-number", &val);
488         if (!ret) {
489                 adc_os_prop->channel_number = val;
490         } else {
491                 adc_os_prop->channel_number = -EINVAL;
492                 if (adc_os_prop->is_continuous_mode) {
493                         dev_err(adc->dev,
494                                 "Continuous channel number not available\n");
495                         return -EINVAL;
496                 }
497         }
498
499         /* If continuous mode then initialised with default */
500         if (adc_os_prop->is_continuous_mode) {
501                 adc_prop->pga = adc_os_prop->pga;
502                 adc_prop->sampling_freq = adc_os_prop->sampling_freq;
503                 adc_prop->comparator_mode = adc_os_prop->comparator_mode;
504                 adc_prop->comparator_queue = adc_os_prop->comparator_queue;
505                 adc_prop->is_continuous_mode = adc_os_prop->is_continuous_mode;
506                 adc_prop->num_retries = adc_os_prop->num_retries;
507                 adc_prop->tolerance = adc_os_prop->tolerance;
508                 adc_prop->retry_delay_ms = adc_os_prop->retry_delay_ms;
509                 adc_prop->channel_number = adc_os_prop->channel_number;
510                 adc_prop->is_continuous_mode = true;
511         }
512
513         tnode = np;
514         cnode = of_find_node_by_name(np, "continuous-mode");
515         if (!cnode) {
516                 if (adc_os_prop->is_continuous_mode)
517                         goto parse_range;
518                 goto parse_done;
519         }
520
521         tnode = cnode;
522         adc_prop->is_continuous_mode = true;
523         adc->continuous_dt_node = true;
524
525         ret = of_property_read_u32(cnode, "ti,programmable-gain-amplifier",
526                                 &val);
527         adc_prop->pga = (ret) ? ADS1015_2048MV_AMPLIFIER_GAIN : val;
528
529         ret = of_property_read_u32(cnode, "sampling-frequency", &val);
530         adc_prop->sampling_freq = (ret) ? ADS1015_920_SPS : val;
531
532         ret = of_property_read_u32(cnode, "ti,comparator-mode", &val);
533         adc_prop->comparator_mode = (ret) ? ADS1015_WINDOW_COMPARATOR : val;
534
535         ret = of_property_read_u32(cnode, "ti,comparator-queue", &val);
536         adc_prop->comparator_queue = (ret) ? ADS1015_FOUR_CONVERSIONS : val;
537
538         ret = of_property_read_u32(cnode, "ti,maximum-retries", &val);
539         if (!ret) {
540                 adc_prop->num_retries = val;
541                 if (adc_prop->num_retries > ADS_1015_MAX_RETRIES)
542                         adc_prop->num_retries = ADS_1015_MAX_RETRIES;
543         }
544
545         ret = of_property_read_u32(cnode, "ti,tolerance-val", &val);
546         if (!ret)
547                 adc_prop->tolerance = val;
548
549         ret = of_property_read_u32(cnode, "ti,delay-between-retry-ms", &val);
550         if (!ret)
551                 adc_prop->retry_delay_ms = val;
552
553         ret = of_property_read_u32(cnode, "ti,continuous-channel-number", &val);
554         if (!ret) {
555                 adc_prop->channel_number = val;
556         } else {
557                 dev_err(adc->dev, "Continuous channel not provided\n");
558                 return ret;
559         }
560
561
562 parse_range:
563         nranges = of_property_count_u32(tnode, "ti,adc-valid-threshold-ranges");
564         if (nranges <= 0)
565                 return -EINVAL;
566
567         nranges = (nranges  / 2);
568         adc_prop->threshold_ranges = devm_kzalloc(adc->dev, nranges *
569                 sizeof(*adc_prop->threshold_ranges), GFP_KERNEL);
570         if (!adc_prop->threshold_ranges)
571                 return -ENOMEM;
572
573         for (i = 0; i < nranges; ++i) {
574                 lower_adc = 0;
575                 upper_adc = 0;
576
577                 of_property_read_u32_index(tnode,
578                         "ti,adc-valid-threshold-ranges", i * 2 + 0, &lower_adc);
579                 of_property_read_u32_index(tnode,
580                         "ti,adc-valid-threshold-ranges", i * 2 + 1, &upper_adc);
581
582                 adc_prop->threshold_ranges[i].lower = (int)lower_adc;
583                 adc_prop->threshold_ranges[i].upper = (int)upper_adc;
584         };
585         adc_prop->num_conditions = nranges;
586
587 parse_done:
588         return 0;
589 }
590
591 static int ads1015_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
592 {
593         struct ads1015 *adc;
594         struct device_node *node = i2c->dev.of_node;
595         struct iio_dev *iodev;
596         struct ads1015_property *adc_prop;
597         int ret;
598
599         if (!node) {
600                 dev_err(&i2c->dev, "Device DT not found\n");
601                 return -ENODEV;
602         }
603
604         iodev = devm_iio_device_alloc(&i2c->dev, sizeof(*adc));
605         if (!iodev) {
606                 dev_err(&i2c->dev, "iio_device_alloc failed\n");
607                 return -ENOMEM;
608         }
609         adc = iio_priv(iodev);
610         adc->dev = &i2c->dev;
611         i2c_set_clientdata(i2c, adc);
612         adc_prop = &adc->adc_prop;
613         adc->continuous_dt_node = false;
614         adc->is_shutdown = false;
615         mutex_init(&adc->lock);
616         ret = ads1015_get_dt_data(adc, node);
617         if (ret < 0)
618                 return ret;
619
620         adc->rmap = devm_regmap_init_i2c(i2c, &ads1015_regmap_config);
621         if (IS_ERR(adc->rmap)) {
622                 ret = PTR_ERR(adc->rmap);
623                 dev_err(adc->dev, "regmap_init failed %d\n", ret);
624                 return ret;
625         }
626
627         iodev->name = "ads1015";
628         iodev->dev.parent = &i2c->dev;
629         iodev->info = &ads1015_iio_info;
630         iodev->modes = INDIO_DIRECT_MODE;
631         iodev->channels = ads1015_iio_channel;
632         iodev->num_channels = ARRAY_SIZE(ads1015_iio_channel);
633         ret = devm_iio_device_register(&i2c->dev, iodev);
634         if (ret < 0) {
635                 dev_err(adc->dev, "iio_device_register() failed: %d\n", ret);
636                 return ret;
637         }
638
639         ret = ads1015_configure(adc);
640         if (ret < 0) {
641                 dev_err(adc->dev, "ADC configuration failed: %d\n", ret);
642                 return ret;
643         }
644
645         return 0;
646 }
647
648 static void ads1015_shutdown(struct i2c_client *client)
649 {
650         struct ads1015 *adc = i2c_get_clientdata(client);
651
652         mutex_lock(&adc->lock);
653         adc->is_shutdown = true;
654         mutex_unlock(&adc->lock);
655 }
656
657 static const struct of_device_id ads1015_of_match[] = {
658         { .compatible = "ads1015", },
659         {},
660 };
661
662 struct i2c_driver ads1015_driver = {
663         .probe = ads1015_probe,
664         .driver = {
665                 .name = "ads1015",
666                 .owner = THIS_MODULE,
667                 .of_match_table = ads1015_of_match,
668         },
669         .id_table = ads1015_i2c_id,
670         .shutdown = ads1015_shutdown,
671 };
672
673 module_i2c_driver(ads1015_driver);
674
675 MODULE_DESCRIPTION("ads1015 driver");
676 MODULE_AUTHOR("Mallikarjun Kasoju <mkasoju@nvidia.com>");
677 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
678 MODULE_ALIAS("i2c:ads1015");
679 MODULE_LICENSE("GPL v2");