iio: Remove superfluous of_node assignments
[platform/kernel/linux-rpi.git] / drivers / iio / adc / qcom-spmi-adc5.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018, 2020, The Linux Foundation. All rights reserved.
4  */
5
6 #include <linux/bitops.h>
7 #include <linux/completion.h>
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/iio/iio.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/log2.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20
21 #include <dt-bindings/iio/qcom,spmi-vadc.h>
22 #include "qcom-vadc-common.h"
23
24 #define ADC5_USR_REVISION1                      0x0
25 #define ADC5_USR_STATUS1                        0x8
26 #define ADC5_USR_STATUS1_CONV_FAULT             BIT(7)
27 #define ADC5_USR_STATUS1_REQ_STS                BIT(1)
28 #define ADC5_USR_STATUS1_EOC                    BIT(0)
29 #define ADC5_USR_STATUS1_REQ_STS_EOC_MASK       0x3
30
31 #define ADC5_USR_STATUS2                        0x9
32 #define ADC5_USR_STATUS2_CONV_SEQ_MASK          0x70
33 #define ADC5_USR_STATUS2_CONV_SEQ_MASK_SHIFT    0x5
34
35 #define ADC5_USR_IBAT_MEAS                      0xf
36 #define ADC5_USR_IBAT_MEAS_SUPPORTED            BIT(0)
37
38 #define ADC5_USR_DIG_PARAM                      0x42
39 #define ADC5_USR_DIG_PARAM_CAL_VAL              BIT(6)
40 #define ADC5_USR_DIG_PARAM_CAL_VAL_SHIFT        6
41 #define ADC5_USR_DIG_PARAM_CAL_SEL              0x30
42 #define ADC5_USR_DIG_PARAM_CAL_SEL_SHIFT        4
43 #define ADC5_USR_DIG_PARAM_DEC_RATIO_SEL        0xc
44 #define ADC5_USR_DIG_PARAM_DEC_RATIO_SEL_SHIFT  2
45
46 #define ADC5_USR_FAST_AVG_CTL                   0x43
47 #define ADC5_USR_FAST_AVG_CTL_EN                BIT(7)
48 #define ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK      0x7
49
50 #define ADC5_USR_CH_SEL_CTL                     0x44
51
52 #define ADC5_USR_DELAY_CTL                      0x45
53 #define ADC5_USR_HW_SETTLE_DELAY_MASK           0xf
54
55 #define ADC5_USR_EN_CTL1                        0x46
56 #define ADC5_USR_EN_CTL1_ADC_EN                 BIT(7)
57
58 #define ADC5_USR_CONV_REQ                       0x47
59 #define ADC5_USR_CONV_REQ_REQ                   BIT(7)
60
61 #define ADC5_USR_DATA0                          0x50
62
63 #define ADC5_USR_DATA1                          0x51
64
65 #define ADC5_USR_IBAT_DATA0                     0x52
66
67 #define ADC5_USR_IBAT_DATA1                     0x53
68
69 #define ADC_CHANNEL_OFFSET                      0x8
70 #define ADC_CHANNEL_MASK                        GENMASK(7, 0)
71
72 /*
73  * Conversion time varies based on the decimation, clock rate, fast average
74  * samples and measurements queued across different VADC peripherals.
75  * Set the timeout to a max of 100ms.
76  */
77 #define ADC5_CONV_TIME_MIN_US                   263
78 #define ADC5_CONV_TIME_MAX_US                   264
79 #define ADC5_CONV_TIME_RETRY                    400
80 #define ADC5_CONV_TIMEOUT                       msecs_to_jiffies(100)
81
82 /* Digital version >= 5.3 supports hw_settle_2 */
83 #define ADC5_HW_SETTLE_DIFF_MINOR               3
84 #define ADC5_HW_SETTLE_DIFF_MAJOR               5
85
86 /* For PMIC7 */
87 #define ADC_APP_SID                             0x40
88 #define ADC_APP_SID_MASK                        GENMASK(3, 0)
89 #define ADC7_CONV_TIMEOUT                       msecs_to_jiffies(10)
90
91 enum adc5_cal_method {
92         ADC5_NO_CAL = 0,
93         ADC5_RATIOMETRIC_CAL,
94         ADC5_ABSOLUTE_CAL
95 };
96
97 enum adc5_cal_val {
98         ADC5_TIMER_CAL = 0,
99         ADC5_NEW_CAL
100 };
101
102 /**
103  * struct adc5_channel_prop - ADC channel property.
104  * @channel: channel number, refer to the channel list.
105  * @cal_method: calibration method.
106  * @cal_val: calibration value
107  * @decimation: sampling rate supported for the channel.
108  * @sid: slave id of PMIC owning the channel, for PMIC7.
109  * @prescale: channel scaling performed on the input signal.
110  * @hw_settle_time: the time between AMUX being configured and the
111  *      start of conversion.
112  * @avg_samples: ability to provide single result from the ADC
113  *      that is an average of multiple measurements.
114  * @scale_fn_type: Represents the scaling function to convert voltage
115  *      physical units desired by the client for the channel.
116  * @datasheet_name: Channel name used in device tree.
117  */
118 struct adc5_channel_prop {
119         unsigned int            channel;
120         enum adc5_cal_method    cal_method;
121         enum adc5_cal_val       cal_val;
122         unsigned int            decimation;
123         unsigned int            sid;
124         unsigned int            prescale;
125         unsigned int            hw_settle_time;
126         unsigned int            avg_samples;
127         enum vadc_scale_fn_type scale_fn_type;
128         const char              *datasheet_name;
129 };
130
131 /**
132  * struct adc5_chip - ADC private structure.
133  * @regmap: SPMI ADC5 peripheral register map field.
134  * @dev: SPMI ADC5 device.
135  * @base: base address for the ADC peripheral.
136  * @nchannels: number of ADC channels.
137  * @chan_props: array of ADC channel properties.
138  * @iio_chans: array of IIO channels specification.
139  * @poll_eoc: use polling instead of interrupt.
140  * @complete: ADC result notification after interrupt is received.
141  * @lock: ADC lock for access to the peripheral.
142  * @data: software configuration data.
143  */
144 struct adc5_chip {
145         struct regmap           *regmap;
146         struct device           *dev;
147         u16                     base;
148         unsigned int            nchannels;
149         struct adc5_channel_prop        *chan_props;
150         struct iio_chan_spec    *iio_chans;
151         bool                    poll_eoc;
152         struct completion       complete;
153         struct mutex            lock;
154         const struct adc5_data  *data;
155 };
156
157 static const struct vadc_prescale_ratio adc5_prescale_ratios[] = {
158         {.num =  1, .den =  1},
159         {.num =  1, .den =  3},
160         {.num =  1, .den =  4},
161         {.num =  1, .den =  6},
162         {.num =  1, .den = 20},
163         {.num =  1, .den =  8},
164         {.num = 10, .den = 81},
165         {.num =  1, .den = 10},
166         {.num =  1, .den = 16}
167 };
168
169 static int adc5_read(struct adc5_chip *adc, u16 offset, u8 *data, int len)
170 {
171         return regmap_bulk_read(adc->regmap, adc->base + offset, data, len);
172 }
173
174 static int adc5_write(struct adc5_chip *adc, u16 offset, u8 *data, int len)
175 {
176         return regmap_bulk_write(adc->regmap, adc->base + offset, data, len);
177 }
178
179 static int adc5_masked_write(struct adc5_chip *adc, u16 offset, u8 mask, u8 val)
180 {
181         return regmap_update_bits(adc->regmap, adc->base + offset, mask, val);
182 }
183
184 static int adc5_prescaling_from_dt(u32 num, u32 den)
185 {
186         unsigned int pre;
187
188         for (pre = 0; pre < ARRAY_SIZE(adc5_prescale_ratios); pre++)
189                 if (adc5_prescale_ratios[pre].num == num &&
190                     adc5_prescale_ratios[pre].den == den)
191                         break;
192
193         if (pre == ARRAY_SIZE(adc5_prescale_ratios))
194                 return -EINVAL;
195
196         return pre;
197 }
198
199 static int adc5_hw_settle_time_from_dt(u32 value,
200                                         const unsigned int *hw_settle)
201 {
202         unsigned int i;
203
204         for (i = 0; i < VADC_HW_SETTLE_SAMPLES_MAX; i++) {
205                 if (value == hw_settle[i])
206                         return i;
207         }
208
209         return -EINVAL;
210 }
211
212 static int adc5_avg_samples_from_dt(u32 value)
213 {
214         if (!is_power_of_2(value) || value > ADC5_AVG_SAMPLES_MAX)
215                 return -EINVAL;
216
217         return __ffs(value);
218 }
219
220 static int adc5_decimation_from_dt(u32 value,
221                                         const unsigned int *decimation)
222 {
223         unsigned int i;
224
225         for (i = 0; i < ADC5_DECIMATION_SAMPLES_MAX; i++) {
226                 if (value == decimation[i])
227                         return i;
228         }
229
230         return -EINVAL;
231 }
232
233 static int adc5_read_voltage_data(struct adc5_chip *adc, u16 *data)
234 {
235         int ret;
236         u8 rslt_lsb, rslt_msb;
237
238         ret = adc5_read(adc, ADC5_USR_DATA0, &rslt_lsb, sizeof(rslt_lsb));
239         if (ret)
240                 return ret;
241
242         ret = adc5_read(adc, ADC5_USR_DATA1, &rslt_msb, sizeof(rslt_lsb));
243         if (ret)
244                 return ret;
245
246         *data = (rslt_msb << 8) | rslt_lsb;
247
248         if (*data == ADC5_USR_DATA_CHECK) {
249                 dev_err(adc->dev, "Invalid data:0x%x\n", *data);
250                 return -EINVAL;
251         }
252
253         dev_dbg(adc->dev, "voltage raw code:0x%x\n", *data);
254
255         return 0;
256 }
257
258 static int adc5_poll_wait_eoc(struct adc5_chip *adc)
259 {
260         unsigned int count, retry = ADC5_CONV_TIME_RETRY;
261         u8 status1;
262         int ret;
263
264         for (count = 0; count < retry; count++) {
265                 ret = adc5_read(adc, ADC5_USR_STATUS1, &status1,
266                                                         sizeof(status1));
267                 if (ret)
268                         return ret;
269
270                 status1 &= ADC5_USR_STATUS1_REQ_STS_EOC_MASK;
271                 if (status1 == ADC5_USR_STATUS1_EOC)
272                         return 0;
273
274                 usleep_range(ADC5_CONV_TIME_MIN_US, ADC5_CONV_TIME_MAX_US);
275         }
276
277         return -ETIMEDOUT;
278 }
279
280 static void adc5_update_dig_param(struct adc5_chip *adc,
281                         struct adc5_channel_prop *prop, u8 *data)
282 {
283         /* Update calibration value */
284         *data &= ~ADC5_USR_DIG_PARAM_CAL_VAL;
285         *data |= (prop->cal_val << ADC5_USR_DIG_PARAM_CAL_VAL_SHIFT);
286
287         /* Update calibration select */
288         *data &= ~ADC5_USR_DIG_PARAM_CAL_SEL;
289         *data |= (prop->cal_method << ADC5_USR_DIG_PARAM_CAL_SEL_SHIFT);
290
291         /* Update decimation ratio select */
292         *data &= ~ADC5_USR_DIG_PARAM_DEC_RATIO_SEL;
293         *data |= (prop->decimation << ADC5_USR_DIG_PARAM_DEC_RATIO_SEL_SHIFT);
294 }
295
296 static int adc5_configure(struct adc5_chip *adc,
297                         struct adc5_channel_prop *prop)
298 {
299         int ret;
300         u8 buf[6];
301
302         /* Read registers 0x42 through 0x46 */
303         ret = adc5_read(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf));
304         if (ret)
305                 return ret;
306
307         /* Digital param selection */
308         adc5_update_dig_param(adc, prop, &buf[0]);
309
310         /* Update fast average sample value */
311         buf[1] &= (u8) ~ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK;
312         buf[1] |= prop->avg_samples;
313
314         /* Select ADC channel */
315         buf[2] = prop->channel;
316
317         /* Select HW settle delay for channel */
318         buf[3] &= (u8) ~ADC5_USR_HW_SETTLE_DELAY_MASK;
319         buf[3] |= prop->hw_settle_time;
320
321         /* Select ADC enable */
322         buf[4] |= ADC5_USR_EN_CTL1_ADC_EN;
323
324         /* Select CONV request */
325         buf[5] |= ADC5_USR_CONV_REQ_REQ;
326
327         if (!adc->poll_eoc)
328                 reinit_completion(&adc->complete);
329
330         return adc5_write(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf));
331 }
332
333 static int adc7_configure(struct adc5_chip *adc,
334                         struct adc5_channel_prop *prop)
335 {
336         int ret;
337         u8 conv_req = 0, buf[4];
338
339         ret = adc5_masked_write(adc, ADC_APP_SID, ADC_APP_SID_MASK, prop->sid);
340         if (ret)
341                 return ret;
342
343         ret = adc5_read(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf));
344         if (ret)
345                 return ret;
346
347         /* Digital param selection */
348         adc5_update_dig_param(adc, prop, &buf[0]);
349
350         /* Update fast average sample value */
351         buf[1] &= ~ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK;
352         buf[1] |= prop->avg_samples;
353
354         /* Select ADC channel */
355         buf[2] = prop->channel;
356
357         /* Select HW settle delay for channel */
358         buf[3] &= ~ADC5_USR_HW_SETTLE_DELAY_MASK;
359         buf[3] |= prop->hw_settle_time;
360
361         /* Select CONV request */
362         conv_req = ADC5_USR_CONV_REQ_REQ;
363
364         if (!adc->poll_eoc)
365                 reinit_completion(&adc->complete);
366
367         ret = adc5_write(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf));
368         if (ret)
369                 return ret;
370
371         return adc5_write(adc, ADC5_USR_CONV_REQ, &conv_req, 1);
372 }
373
374 static int adc5_do_conversion(struct adc5_chip *adc,
375                         struct adc5_channel_prop *prop,
376                         struct iio_chan_spec const *chan,
377                         u16 *data_volt, u16 *data_cur)
378 {
379         int ret;
380
381         mutex_lock(&adc->lock);
382
383         ret = adc5_configure(adc, prop);
384         if (ret) {
385                 dev_err(adc->dev, "ADC configure failed with %d\n", ret);
386                 goto unlock;
387         }
388
389         if (adc->poll_eoc) {
390                 ret = adc5_poll_wait_eoc(adc);
391                 if (ret) {
392                         dev_err(adc->dev, "EOC bit not set\n");
393                         goto unlock;
394                 }
395         } else {
396                 ret = wait_for_completion_timeout(&adc->complete,
397                                                         ADC5_CONV_TIMEOUT);
398                 if (!ret) {
399                         dev_dbg(adc->dev, "Did not get completion timeout.\n");
400                         ret = adc5_poll_wait_eoc(adc);
401                         if (ret) {
402                                 dev_err(adc->dev, "EOC bit not set\n");
403                                 goto unlock;
404                         }
405                 }
406         }
407
408         ret = adc5_read_voltage_data(adc, data_volt);
409 unlock:
410         mutex_unlock(&adc->lock);
411
412         return ret;
413 }
414
415 static int adc7_do_conversion(struct adc5_chip *adc,
416                         struct adc5_channel_prop *prop,
417                         struct iio_chan_spec const *chan,
418                         u16 *data_volt, u16 *data_cur)
419 {
420         int ret;
421         u8 status;
422
423         mutex_lock(&adc->lock);
424
425         ret = adc7_configure(adc, prop);
426         if (ret) {
427                 dev_err(adc->dev, "ADC configure failed with %d\n", ret);
428                 goto unlock;
429         }
430
431         /* No support for polling mode at present */
432         wait_for_completion_timeout(&adc->complete, ADC7_CONV_TIMEOUT);
433
434         ret = adc5_read(adc, ADC5_USR_STATUS1, &status, 1);
435         if (ret)
436                 goto unlock;
437
438         if (status & ADC5_USR_STATUS1_CONV_FAULT) {
439                 dev_err(adc->dev, "Unexpected conversion fault\n");
440                 ret = -EIO;
441                 goto unlock;
442         }
443
444         ret = adc5_read_voltage_data(adc, data_volt);
445
446 unlock:
447         mutex_unlock(&adc->lock);
448
449         return ret;
450 }
451
452 static irqreturn_t adc5_isr(int irq, void *dev_id)
453 {
454         struct adc5_chip *adc = dev_id;
455
456         complete(&adc->complete);
457
458         return IRQ_HANDLED;
459 }
460
461 static int adc5_of_xlate(struct iio_dev *indio_dev,
462                                 const struct of_phandle_args *iiospec)
463 {
464         struct adc5_chip *adc = iio_priv(indio_dev);
465         int i;
466
467         for (i = 0; i < adc->nchannels; i++)
468                 if (adc->chan_props[i].channel == iiospec->args[0])
469                         return i;
470
471         return -EINVAL;
472 }
473
474 static int adc7_of_xlate(struct iio_dev *indio_dev,
475                                 const struct of_phandle_args *iiospec)
476 {
477         struct adc5_chip *adc = iio_priv(indio_dev);
478         int i, v_channel;
479
480         for (i = 0; i < adc->nchannels; i++) {
481                 v_channel = (adc->chan_props[i].sid << ADC_CHANNEL_OFFSET) |
482                         adc->chan_props[i].channel;
483                 if (v_channel == iiospec->args[0])
484                         return i;
485         }
486
487         return -EINVAL;
488 }
489
490 static int adc5_read_raw(struct iio_dev *indio_dev,
491                          struct iio_chan_spec const *chan, int *val, int *val2,
492                          long mask)
493 {
494         struct adc5_chip *adc = iio_priv(indio_dev);
495         struct adc5_channel_prop *prop;
496         u16 adc_code_volt, adc_code_cur;
497         int ret;
498
499         prop = &adc->chan_props[chan->address];
500
501         switch (mask) {
502         case IIO_CHAN_INFO_PROCESSED:
503                 ret = adc5_do_conversion(adc, prop, chan,
504                                 &adc_code_volt, &adc_code_cur);
505                 if (ret)
506                         return ret;
507
508                 ret = qcom_adc5_hw_scale(prop->scale_fn_type,
509                         &adc5_prescale_ratios[prop->prescale],
510                         adc->data,
511                         adc_code_volt, val);
512                 if (ret)
513                         return ret;
514
515                 return IIO_VAL_INT;
516         default:
517                 return -EINVAL;
518         }
519 }
520
521 static int adc7_read_raw(struct iio_dev *indio_dev,
522                          struct iio_chan_spec const *chan, int *val, int *val2,
523                          long mask)
524 {
525         struct adc5_chip *adc = iio_priv(indio_dev);
526         struct adc5_channel_prop *prop;
527         u16 adc_code_volt, adc_code_cur;
528         int ret;
529
530         prop = &adc->chan_props[chan->address];
531
532         switch (mask) {
533         case IIO_CHAN_INFO_PROCESSED:
534                 ret = adc7_do_conversion(adc, prop, chan,
535                                         &adc_code_volt, &adc_code_cur);
536                 if (ret)
537                         return ret;
538
539                 ret = qcom_adc5_hw_scale(prop->scale_fn_type,
540                         &adc5_prescale_ratios[prop->prescale],
541                         adc->data,
542                         adc_code_volt, val);
543
544                 if (ret)
545                         return ret;
546
547                 return IIO_VAL_INT;
548         default:
549                 return -EINVAL;
550         }
551 }
552
553 static const struct iio_info adc5_info = {
554         .read_raw = adc5_read_raw,
555         .of_xlate = adc5_of_xlate,
556 };
557
558 static const struct iio_info adc7_info = {
559         .read_raw = adc7_read_raw,
560         .of_xlate = adc7_of_xlate,
561 };
562
563 struct adc5_channels {
564         const char *datasheet_name;
565         unsigned int prescale_index;
566         enum iio_chan_type type;
567         long info_mask;
568         enum vadc_scale_fn_type scale_fn_type;
569 };
570
571 /* In these definitions, _pre refers to an index into adc5_prescale_ratios. */
572 #define ADC5_CHAN(_dname, _type, _mask, _pre, _scale)                   \
573         {                                                               \
574                 .datasheet_name = _dname,                               \
575                 .prescale_index = _pre,                                 \
576                 .type = _type,                                          \
577                 .info_mask = _mask,                                     \
578                 .scale_fn_type = _scale,                                \
579         },                                                              \
580
581 #define ADC5_CHAN_TEMP(_dname, _pre, _scale)                            \
582         ADC5_CHAN(_dname, IIO_TEMP,                                     \
583                 BIT(IIO_CHAN_INFO_PROCESSED),                           \
584                 _pre, _scale)                                           \
585
586 #define ADC5_CHAN_VOLT(_dname, _pre, _scale)                            \
587         ADC5_CHAN(_dname, IIO_VOLTAGE,                                  \
588                   BIT(IIO_CHAN_INFO_PROCESSED),                         \
589                   _pre, _scale)                                         \
590
591 static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = {
592         [ADC5_REF_GND]          = ADC5_CHAN_VOLT("ref_gnd", 0,
593                                         SCALE_HW_CALIB_DEFAULT)
594         [ADC5_1P25VREF]         = ADC5_CHAN_VOLT("vref_1p25", 0,
595                                         SCALE_HW_CALIB_DEFAULT)
596         [ADC5_VPH_PWR]          = ADC5_CHAN_VOLT("vph_pwr", 1,
597                                         SCALE_HW_CALIB_DEFAULT)
598         [ADC5_VBAT_SNS]         = ADC5_CHAN_VOLT("vbat_sns", 1,
599                                         SCALE_HW_CALIB_DEFAULT)
600         [ADC5_DIE_TEMP]         = ADC5_CHAN_TEMP("die_temp", 0,
601                                         SCALE_HW_CALIB_PMIC_THERM)
602         [ADC5_USB_IN_I]         = ADC5_CHAN_VOLT("usb_in_i_uv", 0,
603                                         SCALE_HW_CALIB_DEFAULT)
604         [ADC5_USB_IN_V_16]      = ADC5_CHAN_VOLT("usb_in_v_div_16", 8,
605                                         SCALE_HW_CALIB_DEFAULT)
606         [ADC5_CHG_TEMP]         = ADC5_CHAN_TEMP("chg_temp", 0,
607                                         SCALE_HW_CALIB_PM5_CHG_TEMP)
608         /* Charger prescales SBUx and MID_CHG to fit within 1.8V upper unit */
609         [ADC5_SBUx]             = ADC5_CHAN_VOLT("chg_sbux", 1,
610                                         SCALE_HW_CALIB_DEFAULT)
611         [ADC5_MID_CHG_DIV6]     = ADC5_CHAN_VOLT("chg_mid_chg", 3,
612                                         SCALE_HW_CALIB_DEFAULT)
613         [ADC5_XO_THERM_100K_PU] = ADC5_CHAN_TEMP("xo_therm", 0,
614                                         SCALE_HW_CALIB_XOTHERM)
615         [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
616                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
617         [ADC5_AMUX_THM2_100K_PU] = ADC5_CHAN_TEMP("amux_thm2_100k_pu", 0,
618                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
619         [ADC5_AMUX_THM3_100K_PU] = ADC5_CHAN_TEMP("amux_thm3_100k_pu", 0,
620                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
621         [ADC5_AMUX_THM2]        = ADC5_CHAN_TEMP("amux_thm2", 0,
622                                         SCALE_HW_CALIB_PM5_SMB_TEMP)
623 };
624
625 static const struct adc5_channels adc7_chans_pmic[ADC5_MAX_CHANNEL] = {
626         [ADC7_REF_GND]          = ADC5_CHAN_VOLT("ref_gnd", 0,
627                                         SCALE_HW_CALIB_DEFAULT)
628         [ADC7_1P25VREF]         = ADC5_CHAN_VOLT("vref_1p25", 0,
629                                         SCALE_HW_CALIB_DEFAULT)
630         [ADC7_VPH_PWR]          = ADC5_CHAN_VOLT("vph_pwr", 1,
631                                         SCALE_HW_CALIB_DEFAULT)
632         [ADC7_VBAT_SNS]         = ADC5_CHAN_VOLT("vbat_sns", 3,
633                                         SCALE_HW_CALIB_DEFAULT)
634         [ADC7_DIE_TEMP]         = ADC5_CHAN_TEMP("die_temp", 0,
635                                         SCALE_HW_CALIB_PMIC_THERM_PM7)
636         [ADC7_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_pu2", 0,
637                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
638         [ADC7_AMUX_THM2_100K_PU] = ADC5_CHAN_TEMP("amux_thm2_pu2", 0,
639                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
640         [ADC7_AMUX_THM3_100K_PU] = ADC5_CHAN_TEMP("amux_thm3_pu2", 0,
641                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
642         [ADC7_AMUX_THM4_100K_PU] = ADC5_CHAN_TEMP("amux_thm4_pu2", 0,
643                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
644         [ADC7_AMUX_THM5_100K_PU] = ADC5_CHAN_TEMP("amux_thm5_pu2", 0,
645                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
646         [ADC7_AMUX_THM6_100K_PU] = ADC5_CHAN_TEMP("amux_thm6_pu2", 0,
647                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
648         [ADC7_GPIO1_100K_PU]    = ADC5_CHAN_TEMP("gpio1_pu2", 0,
649                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
650         [ADC7_GPIO2_100K_PU]    = ADC5_CHAN_TEMP("gpio2_pu2", 0,
651                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
652         [ADC7_GPIO3_100K_PU]    = ADC5_CHAN_TEMP("gpio3_pu2", 0,
653                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
654         [ADC7_GPIO4_100K_PU]    = ADC5_CHAN_TEMP("gpio4_pu2", 0,
655                                         SCALE_HW_CALIB_THERM_100K_PU_PM7)
656 };
657
658 static const struct adc5_channels adc5_chans_rev2[ADC5_MAX_CHANNEL] = {
659         [ADC5_REF_GND]          = ADC5_CHAN_VOLT("ref_gnd", 0,
660                                         SCALE_HW_CALIB_DEFAULT)
661         [ADC5_1P25VREF]         = ADC5_CHAN_VOLT("vref_1p25", 0,
662                                         SCALE_HW_CALIB_DEFAULT)
663         [ADC5_VPH_PWR]          = ADC5_CHAN_VOLT("vph_pwr", 1,
664                                         SCALE_HW_CALIB_DEFAULT)
665         [ADC5_VBAT_SNS]         = ADC5_CHAN_VOLT("vbat_sns", 1,
666                                         SCALE_HW_CALIB_DEFAULT)
667         [ADC5_VCOIN]            = ADC5_CHAN_VOLT("vcoin", 1,
668                                         SCALE_HW_CALIB_DEFAULT)
669         [ADC5_DIE_TEMP]         = ADC5_CHAN_TEMP("die_temp", 0,
670                                         SCALE_HW_CALIB_PMIC_THERM)
671         [ADC5_AMUX_THM1_100K_PU] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
672                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
673         [ADC5_AMUX_THM2_100K_PU] = ADC5_CHAN_TEMP("amux_thm2_100k_pu", 0,
674                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
675         [ADC5_AMUX_THM3_100K_PU] = ADC5_CHAN_TEMP("amux_thm3_100k_pu", 0,
676                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
677         [ADC5_AMUX_THM4_100K_PU] = ADC5_CHAN_TEMP("amux_thm4_100k_pu", 0,
678                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
679         [ADC5_AMUX_THM5_100K_PU] = ADC5_CHAN_TEMP("amux_thm5_100k_pu", 0,
680                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
681         [ADC5_XO_THERM_100K_PU] = ADC5_CHAN_TEMP("xo_therm_100k_pu", 0,
682                                         SCALE_HW_CALIB_THERM_100K_PULLUP)
683 };
684
685 static int adc5_get_dt_channel_data(struct adc5_chip *adc,
686                                     struct adc5_channel_prop *prop,
687                                     struct device_node *node,
688                                     const struct adc5_data *data)
689 {
690         const char *name = node->name, *channel_name;
691         u32 chan, value, varr[2];
692         u32 sid = 0;
693         int ret;
694         struct device *dev = adc->dev;
695
696         ret = of_property_read_u32(node, "reg", &chan);
697         if (ret) {
698                 dev_err(dev, "invalid channel number %s\n", name);
699                 return ret;
700         }
701
702         /* Value read from "reg" is virtual channel number */
703
704         /* virtual channel number = sid << 8 | channel number */
705
706         if (adc->data->info == &adc7_info) {
707                 sid = chan >> ADC_CHANNEL_OFFSET;
708                 chan = chan & ADC_CHANNEL_MASK;
709         }
710
711         if (chan > ADC5_PARALLEL_ISENSE_VBAT_IDATA ||
712             !data->adc_chans[chan].datasheet_name) {
713                 dev_err(dev, "%s invalid channel number %d\n", name, chan);
714                 return -EINVAL;
715         }
716
717         /* the channel has DT description */
718         prop->channel = chan;
719         prop->sid = sid;
720
721         channel_name = of_get_property(node,
722                                 "label", NULL) ? : node->name;
723         if (!channel_name) {
724                 dev_err(dev, "Invalid channel name\n");
725                 return -EINVAL;
726         }
727         prop->datasheet_name = channel_name;
728
729         ret = of_property_read_u32(node, "qcom,decimation", &value);
730         if (!ret) {
731                 ret = adc5_decimation_from_dt(value, data->decimation);
732                 if (ret < 0) {
733                         dev_err(dev, "%02x invalid decimation %d\n",
734                                 chan, value);
735                         return ret;
736                 }
737                 prop->decimation = ret;
738         } else {
739                 prop->decimation = ADC5_DECIMATION_DEFAULT;
740         }
741
742         ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
743         if (!ret) {
744                 ret = adc5_prescaling_from_dt(varr[0], varr[1]);
745                 if (ret < 0) {
746                         dev_err(dev, "%02x invalid pre-scaling <%d %d>\n",
747                                 chan, varr[0], varr[1]);
748                         return ret;
749                 }
750                 prop->prescale = ret;
751         } else {
752                 prop->prescale =
753                         adc->data->adc_chans[prop->channel].prescale_index;
754         }
755
756         ret = of_property_read_u32(node, "qcom,hw-settle-time", &value);
757         if (!ret) {
758                 u8 dig_version[2];
759
760                 ret = adc5_read(adc, ADC5_USR_REVISION1, dig_version,
761                                                         sizeof(dig_version));
762                 if (ret) {
763                         dev_err(dev, "Invalid dig version read %d\n", ret);
764                         return ret;
765                 }
766
767                 dev_dbg(dev, "dig_ver:minor:%d, major:%d\n", dig_version[0],
768                                                 dig_version[1]);
769                 /* Digital controller >= 5.3 have hw_settle_2 option */
770                 if ((dig_version[0] >= ADC5_HW_SETTLE_DIFF_MINOR &&
771                         dig_version[1] >= ADC5_HW_SETTLE_DIFF_MAJOR) ||
772                         adc->data->info == &adc7_info)
773                         ret = adc5_hw_settle_time_from_dt(value,
774                                                         data->hw_settle_2);
775                 else
776                         ret = adc5_hw_settle_time_from_dt(value,
777                                                         data->hw_settle_1);
778
779                 if (ret < 0) {
780                         dev_err(dev, "%02x invalid hw-settle-time %d us\n",
781                                 chan, value);
782                         return ret;
783                 }
784                 prop->hw_settle_time = ret;
785         } else {
786                 prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
787         }
788
789         ret = of_property_read_u32(node, "qcom,avg-samples", &value);
790         if (!ret) {
791                 ret = adc5_avg_samples_from_dt(value);
792                 if (ret < 0) {
793                         dev_err(dev, "%02x invalid avg-samples %d\n",
794                                 chan, value);
795                         return ret;
796                 }
797                 prop->avg_samples = ret;
798         } else {
799                 prop->avg_samples = VADC_DEF_AVG_SAMPLES;
800         }
801
802         if (of_property_read_bool(node, "qcom,ratiometric"))
803                 prop->cal_method = ADC5_RATIOMETRIC_CAL;
804         else
805                 prop->cal_method = ADC5_ABSOLUTE_CAL;
806
807         /*
808          * Default to using timer calibration. Using a fresh calibration value
809          * for every conversion will increase the overall time for a request.
810          */
811         prop->cal_val = ADC5_TIMER_CAL;
812
813         dev_dbg(dev, "%02x name %s\n", chan, name);
814
815         return 0;
816 }
817
818 static const struct adc5_data adc5_data_pmic = {
819         .full_scale_code_volt = 0x70e4,
820         .full_scale_code_cur = 0x2710,
821         .adc_chans = adc5_chans_pmic,
822         .info = &adc5_info,
823         .decimation = (unsigned int [ADC5_DECIMATION_SAMPLES_MAX])
824                                 {250, 420, 840},
825         .hw_settle_1 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
826                                 {15, 100, 200, 300, 400, 500, 600, 700,
827                                 800, 900, 1, 2, 4, 6, 8, 10},
828         .hw_settle_2 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
829                                 {15, 100, 200, 300, 400, 500, 600, 700,
830                                 1, 2, 4, 8, 16, 32, 64, 128},
831 };
832
833 static const struct adc5_data adc7_data_pmic = {
834         .full_scale_code_volt = 0x70e4,
835         .adc_chans = adc7_chans_pmic,
836         .info = &adc7_info,
837         .decimation = (unsigned int [ADC5_DECIMATION_SAMPLES_MAX])
838                                 {85, 340, 1360},
839         .hw_settle_2 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
840                                 {15, 100, 200, 300, 400, 500, 600, 700,
841                                 1000, 2000, 4000, 8000, 16000, 32000,
842                                 64000, 128000},
843 };
844
845 static const struct adc5_data adc5_data_pmic_rev2 = {
846         .full_scale_code_volt = 0x4000,
847         .full_scale_code_cur = 0x1800,
848         .adc_chans = adc5_chans_rev2,
849         .info = &adc5_info,
850         .decimation = (unsigned int [ADC5_DECIMATION_SAMPLES_MAX])
851                                 {256, 512, 1024},
852         .hw_settle_1 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
853                                 {0, 100, 200, 300, 400, 500, 600, 700,
854                                 800, 900, 1, 2, 4, 6, 8, 10},
855         .hw_settle_2 = (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX])
856                                 {15, 100, 200, 300, 400, 500, 600, 700,
857                                 1, 2, 4, 8, 16, 32, 64, 128},
858 };
859
860 static const struct of_device_id adc5_match_table[] = {
861         {
862                 .compatible = "qcom,spmi-adc5",
863                 .data = &adc5_data_pmic,
864         },
865         {
866                 .compatible = "qcom,spmi-adc7",
867                 .data = &adc7_data_pmic,
868         },
869         {
870                 .compatible = "qcom,spmi-adc-rev2",
871                 .data = &adc5_data_pmic_rev2,
872         },
873         { }
874 };
875 MODULE_DEVICE_TABLE(of, adc5_match_table);
876
877 static int adc5_get_dt_data(struct adc5_chip *adc, struct device_node *node)
878 {
879         const struct adc5_channels *adc_chan;
880         struct iio_chan_spec *iio_chan;
881         struct adc5_channel_prop prop, *chan_props;
882         struct device_node *child;
883         unsigned int index = 0;
884         const struct of_device_id *id;
885         const struct adc5_data *data;
886         int ret;
887
888         adc->nchannels = of_get_available_child_count(node);
889         if (!adc->nchannels)
890                 return -EINVAL;
891
892         adc->iio_chans = devm_kcalloc(adc->dev, adc->nchannels,
893                                        sizeof(*adc->iio_chans), GFP_KERNEL);
894         if (!adc->iio_chans)
895                 return -ENOMEM;
896
897         adc->chan_props = devm_kcalloc(adc->dev, adc->nchannels,
898                                         sizeof(*adc->chan_props), GFP_KERNEL);
899         if (!adc->chan_props)
900                 return -ENOMEM;
901
902         chan_props = adc->chan_props;
903         iio_chan = adc->iio_chans;
904         id = of_match_node(adc5_match_table, node);
905         if (id)
906                 data = id->data;
907         else
908                 data = &adc5_data_pmic;
909         adc->data = data;
910
911         for_each_available_child_of_node(node, child) {
912                 ret = adc5_get_dt_channel_data(adc, &prop, child, data);
913                 if (ret) {
914                         of_node_put(child);
915                         return ret;
916                 }
917
918                 prop.scale_fn_type =
919                         data->adc_chans[prop.channel].scale_fn_type;
920                 *chan_props = prop;
921                 adc_chan = &data->adc_chans[prop.channel];
922
923                 iio_chan->channel = prop.channel;
924                 iio_chan->datasheet_name = prop.datasheet_name;
925                 iio_chan->extend_name = prop.datasheet_name;
926                 iio_chan->info_mask_separate = adc_chan->info_mask;
927                 iio_chan->type = adc_chan->type;
928                 iio_chan->address = index;
929                 iio_chan++;
930                 chan_props++;
931                 index++;
932         }
933
934         return 0;
935 }
936
937 static int adc5_probe(struct platform_device *pdev)
938 {
939         struct device_node *node = pdev->dev.of_node;
940         struct device *dev = &pdev->dev;
941         struct iio_dev *indio_dev;
942         struct adc5_chip *adc;
943         struct regmap *regmap;
944         int ret, irq_eoc;
945         u32 reg;
946
947         regmap = dev_get_regmap(dev->parent, NULL);
948         if (!regmap)
949                 return -ENODEV;
950
951         ret = of_property_read_u32(node, "reg", &reg);
952         if (ret < 0)
953                 return ret;
954
955         indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
956         if (!indio_dev)
957                 return -ENOMEM;
958
959         adc = iio_priv(indio_dev);
960         adc->regmap = regmap;
961         adc->dev = dev;
962         adc->base = reg;
963
964         init_completion(&adc->complete);
965         mutex_init(&adc->lock);
966
967         ret = adc5_get_dt_data(adc, node);
968         if (ret) {
969                 dev_err(dev, "adc get dt data failed\n");
970                 return ret;
971         }
972
973         irq_eoc = platform_get_irq(pdev, 0);
974         if (irq_eoc < 0) {
975                 if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL)
976                         return irq_eoc;
977                 adc->poll_eoc = true;
978         } else {
979                 ret = devm_request_irq(dev, irq_eoc, adc5_isr, 0,
980                                        "pm-adc5", adc);
981                 if (ret)
982                         return ret;
983         }
984
985         indio_dev->name = pdev->name;
986         indio_dev->modes = INDIO_DIRECT_MODE;
987         indio_dev->info = adc->data->info;
988         indio_dev->channels = adc->iio_chans;
989         indio_dev->num_channels = adc->nchannels;
990
991         return devm_iio_device_register(dev, indio_dev);
992 }
993
994 static struct platform_driver adc5_driver = {
995         .driver = {
996                 .name = "qcom-spmi-adc5.c",
997                 .of_match_table = adc5_match_table,
998         },
999         .probe = adc5_probe,
1000 };
1001 module_platform_driver(adc5_driver);
1002
1003 MODULE_ALIAS("platform:qcom-spmi-adc5");
1004 MODULE_DESCRIPTION("Qualcomm Technologies Inc. PMIC5 ADC driver");
1005 MODULE_LICENSE("GPL v2");