1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/bitops.h>
15#include <linux/completion.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18#include <linux/iio/iio.h>
19#include <linux/interrupt.h>
20#include <linux/kernel.h>
21#include <linux/math64.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/platform_device.h>
25#include <linux/regmap.h>
26#include <linux/slab.h>
27#include <linux/log2.h>
28
29#include <dt-bindings/iio/qcom,spmi-vadc.h>
30
31
32#define VADC_REVISION2 0x1
33#define VADC_REVISION2_SUPPORTED_VADC 1
34
35#define VADC_PERPH_TYPE 0x4
36#define VADC_PERPH_TYPE_ADC 8
37
38#define VADC_PERPH_SUBTYPE 0x5
39#define VADC_PERPH_SUBTYPE_VADC 1
40
41#define VADC_STATUS1 0x8
42#define VADC_STATUS1_OP_MODE 4
43#define VADC_STATUS1_REQ_STS BIT(1)
44#define VADC_STATUS1_EOC BIT(0)
45#define VADC_STATUS1_REQ_STS_EOC_MASK 0x3
46
47#define VADC_MODE_CTL 0x40
48#define VADC_OP_MODE_SHIFT 3
49#define VADC_OP_MODE_NORMAL 0
50#define VADC_AMUX_TRIM_EN BIT(1)
51#define VADC_ADC_TRIM_EN BIT(0)
52
53#define VADC_EN_CTL1 0x46
54#define VADC_EN_CTL1_SET BIT(7)
55
56#define VADC_ADC_CH_SEL_CTL 0x48
57
58#define VADC_ADC_DIG_PARAM 0x50
59#define VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
60
61#define VADC_HW_SETTLE_DELAY 0x51
62
63#define VADC_CONV_REQ 0x52
64#define VADC_CONV_REQ_SET BIT(7)
65
66#define VADC_FAST_AVG_CTL 0x5a
67#define VADC_FAST_AVG_EN 0x5b
68#define VADC_FAST_AVG_EN_SET BIT(7)
69
70#define VADC_ACCESS 0xd0
71#define VADC_ACCESS_DATA 0xa5
72
73#define VADC_PERH_RESET_CTL3 0xda
74#define VADC_FOLLOW_WARM_RB BIT(2)
75
76#define VADC_DATA 0x60
77
78#define VADC_CONV_TIME_MIN_US 2000
79#define VADC_CONV_TIME_MAX_US 2100
80
81
82#define VADC_MIN_ADC_CODE 0x6000
83
84#define VADC_MAX_ADC_CODE 0xa800
85
86#define VADC_ABSOLUTE_RANGE_UV 625000
87#define VADC_RATIOMETRIC_RANGE_UV 1800000
88
89#define VADC_DEF_PRESCALING 0
90#define VADC_DEF_DECIMATION 0
91#define VADC_DEF_HW_SETTLE_TIME 0
92#define VADC_DEF_AVG_SAMPLES 0
93#define VADC_DEF_CALIB_TYPE VADC_CALIB_ABSOLUTE
94
95#define VADC_DECIMATION_MIN 512
96#define VADC_DECIMATION_MAX 4096
97
98#define VADC_HW_SETTLE_DELAY_MAX 10000
99#define VADC_AVG_SAMPLES_MAX 512
100
101#define KELVINMIL_CELSIUSMIL 273150
102
103#define VADC_CHAN_MIN VADC_USBIN
104#define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM
105
106
107
108
109
110
111enum vadc_calibration {
112 VADC_CALIB_ABSOLUTE = 0,
113 VADC_CALIB_RATIOMETRIC
114};
115
116
117
118
119
120
121
122
123
124
125struct vadc_linear_graph {
126 s32 dy;
127 s32 dx;
128 s32 gnd;
129};
130
131
132
133
134
135
136struct vadc_prescale_ratio {
137 u32 num;
138 u32 den;
139};
140
141
142
143
144
145
146
147
148
149
150
151
152struct vadc_channel_prop {
153 unsigned int channel;
154 enum vadc_calibration calibration;
155 unsigned int decimation;
156 unsigned int prescale;
157 unsigned int hw_settle_time;
158 unsigned int avg_samples;
159};
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175struct vadc_priv {
176 struct regmap *regmap;
177 struct device *dev;
178 u16 base;
179 unsigned int nchannels;
180 struct vadc_channel_prop *chan_props;
181 struct iio_chan_spec *iio_chans;
182 bool are_ref_measured;
183 bool poll_eoc;
184 struct completion complete;
185 struct vadc_linear_graph graph[2];
186 struct mutex lock;
187};
188
189static const struct vadc_prescale_ratio vadc_prescale_ratios[] = {
190 {.num = 1, .den = 1},
191 {.num = 1, .den = 3},
192 {.num = 1, .den = 4},
193 {.num = 1, .den = 6},
194 {.num = 1, .den = 20},
195 {.num = 1, .den = 8},
196 {.num = 10, .den = 81},
197 {.num = 1, .den = 10}
198};
199
200static int vadc_read(struct vadc_priv *vadc, u16 offset, u8 *data)
201{
202 return regmap_bulk_read(vadc->regmap, vadc->base + offset, data, 1);
203}
204
205static int vadc_write(struct vadc_priv *vadc, u16 offset, u8 data)
206{
207 return regmap_write(vadc->regmap, vadc->base + offset, data);
208}
209
210static int vadc_reset(struct vadc_priv *vadc)
211{
212 u8 data;
213 int ret;
214
215 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
216 if (ret)
217 return ret;
218
219 ret = vadc_read(vadc, VADC_PERH_RESET_CTL3, &data);
220 if (ret)
221 return ret;
222
223 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
224 if (ret)
225 return ret;
226
227 data |= VADC_FOLLOW_WARM_RB;
228
229 return vadc_write(vadc, VADC_PERH_RESET_CTL3, data);
230}
231
232static int vadc_set_state(struct vadc_priv *vadc, bool state)
233{
234 return vadc_write(vadc, VADC_EN_CTL1, state ? VADC_EN_CTL1_SET : 0);
235}
236
237static void vadc_show_status(struct vadc_priv *vadc)
238{
239 u8 mode, sta1, chan, dig, en, req;
240 int ret;
241
242 ret = vadc_read(vadc, VADC_MODE_CTL, &mode);
243 if (ret)
244 return;
245
246 ret = vadc_read(vadc, VADC_ADC_DIG_PARAM, &dig);
247 if (ret)
248 return;
249
250 ret = vadc_read(vadc, VADC_ADC_CH_SEL_CTL, &chan);
251 if (ret)
252 return;
253
254 ret = vadc_read(vadc, VADC_CONV_REQ, &req);
255 if (ret)
256 return;
257
258 ret = vadc_read(vadc, VADC_STATUS1, &sta1);
259 if (ret)
260 return;
261
262 ret = vadc_read(vadc, VADC_EN_CTL1, &en);
263 if (ret)
264 return;
265
266 dev_err(vadc->dev,
267 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
268 mode, en, chan, dig, req, sta1);
269}
270
271static int vadc_configure(struct vadc_priv *vadc,
272 struct vadc_channel_prop *prop)
273{
274 u8 decimation, mode_ctrl;
275 int ret;
276
277
278 mode_ctrl = (VADC_OP_MODE_NORMAL << VADC_OP_MODE_SHIFT) |
279 VADC_ADC_TRIM_EN | VADC_AMUX_TRIM_EN;
280 ret = vadc_write(vadc, VADC_MODE_CTL, mode_ctrl);
281 if (ret)
282 return ret;
283
284
285 ret = vadc_write(vadc, VADC_ADC_CH_SEL_CTL, prop->channel);
286 if (ret)
287 return ret;
288
289
290 decimation = prop->decimation << VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT;
291 ret = vadc_write(vadc, VADC_ADC_DIG_PARAM, decimation);
292 if (ret)
293 return ret;
294
295
296 ret = vadc_write(vadc, VADC_HW_SETTLE_DELAY, prop->hw_settle_time);
297 if (ret)
298 return ret;
299
300 ret = vadc_write(vadc, VADC_FAST_AVG_CTL, prop->avg_samples);
301 if (ret)
302 return ret;
303
304 if (prop->avg_samples)
305 ret = vadc_write(vadc, VADC_FAST_AVG_EN, VADC_FAST_AVG_EN_SET);
306 else
307 ret = vadc_write(vadc, VADC_FAST_AVG_EN, 0);
308
309 return ret;
310}
311
312static int vadc_poll_wait_eoc(struct vadc_priv *vadc, unsigned int interval_us)
313{
314 unsigned int count, retry;
315 u8 sta1;
316 int ret;
317
318 retry = interval_us / VADC_CONV_TIME_MIN_US;
319
320 for (count = 0; count < retry; count++) {
321 ret = vadc_read(vadc, VADC_STATUS1, &sta1);
322 if (ret)
323 return ret;
324
325 sta1 &= VADC_STATUS1_REQ_STS_EOC_MASK;
326 if (sta1 == VADC_STATUS1_EOC)
327 return 0;
328
329 usleep_range(VADC_CONV_TIME_MIN_US, VADC_CONV_TIME_MAX_US);
330 }
331
332 vadc_show_status(vadc);
333
334 return -ETIMEDOUT;
335}
336
337static int vadc_read_result(struct vadc_priv *vadc, u16 *data)
338{
339 int ret;
340
341 ret = regmap_bulk_read(vadc->regmap, vadc->base + VADC_DATA, data, 2);
342 if (ret)
343 return ret;
344
345 *data = clamp_t(u16, *data, VADC_MIN_ADC_CODE, VADC_MAX_ADC_CODE);
346
347 return 0;
348}
349
350static struct vadc_channel_prop *vadc_get_channel(struct vadc_priv *vadc,
351 unsigned int num)
352{
353 unsigned int i;
354
355 for (i = 0; i < vadc->nchannels; i++)
356 if (vadc->chan_props[i].channel == num)
357 return &vadc->chan_props[i];
358
359 dev_dbg(vadc->dev, "no such channel %02x\n", num);
360
361 return NULL;
362}
363
364static int vadc_do_conversion(struct vadc_priv *vadc,
365 struct vadc_channel_prop *prop, u16 *data)
366{
367 unsigned int timeout;
368 int ret;
369
370 mutex_lock(&vadc->lock);
371
372 ret = vadc_configure(vadc, prop);
373 if (ret)
374 goto unlock;
375
376 if (!vadc->poll_eoc)
377 reinit_completion(&vadc->complete);
378
379 ret = vadc_set_state(vadc, true);
380 if (ret)
381 goto unlock;
382
383 ret = vadc_write(vadc, VADC_CONV_REQ, VADC_CONV_REQ_SET);
384 if (ret)
385 goto err_disable;
386
387 timeout = BIT(prop->avg_samples) * VADC_CONV_TIME_MIN_US * 2;
388
389 if (vadc->poll_eoc) {
390 ret = vadc_poll_wait_eoc(vadc, timeout);
391 } else {
392 ret = wait_for_completion_timeout(&vadc->complete, timeout);
393 if (!ret) {
394 ret = -ETIMEDOUT;
395 goto err_disable;
396 }
397
398
399 ret = vadc_poll_wait_eoc(vadc, VADC_CONV_TIME_MIN_US);
400 if (ret)
401 goto err_disable;
402 }
403
404 ret = vadc_read_result(vadc, data);
405
406err_disable:
407 vadc_set_state(vadc, false);
408 if (ret)
409 dev_err(vadc->dev, "conversion failed\n");
410unlock:
411 mutex_unlock(&vadc->lock);
412 return ret;
413}
414
415static int vadc_measure_ref_points(struct vadc_priv *vadc)
416{
417 struct vadc_channel_prop *prop;
418 u16 read_1, read_2;
419 int ret;
420
421 vadc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE_UV;
422 vadc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
423
424 prop = vadc_get_channel(vadc, VADC_REF_1250MV);
425 ret = vadc_do_conversion(vadc, prop, &read_1);
426 if (ret)
427 goto err;
428
429
430 prop = vadc_get_channel(vadc, VADC_SPARE1);
431 if (!prop)
432 prop = vadc_get_channel(vadc, VADC_REF_625MV);
433
434 ret = vadc_do_conversion(vadc, prop, &read_2);
435 if (ret)
436 goto err;
437
438 if (read_1 == read_2) {
439 ret = -EINVAL;
440 goto err;
441 }
442
443 vadc->graph[VADC_CALIB_ABSOLUTE].dy = read_1 - read_2;
444 vadc->graph[VADC_CALIB_ABSOLUTE].gnd = read_2;
445
446
447 prop = vadc_get_channel(vadc, VADC_VDD_VADC);
448 ret = vadc_do_conversion(vadc, prop, &read_1);
449 if (ret)
450 goto err;
451
452 prop = vadc_get_channel(vadc, VADC_GND_REF);
453 ret = vadc_do_conversion(vadc, prop, &read_2);
454 if (ret)
455 goto err;
456
457 if (read_1 == read_2) {
458 ret = -EINVAL;
459 goto err;
460 }
461
462 vadc->graph[VADC_CALIB_RATIOMETRIC].dy = read_1 - read_2;
463 vadc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_2;
464err:
465 if (ret)
466 dev_err(vadc->dev, "measure reference points failed\n");
467
468 return ret;
469}
470
471static s32 vadc_calibrate(struct vadc_priv *vadc,
472 const struct vadc_channel_prop *prop, u16 adc_code)
473{
474 const struct vadc_prescale_ratio *prescale;
475 s64 voltage;
476
477 voltage = adc_code - vadc->graph[prop->calibration].gnd;
478 voltage *= vadc->graph[prop->calibration].dx;
479 voltage = div64_s64(voltage, vadc->graph[prop->calibration].dy);
480
481 if (prop->calibration == VADC_CALIB_ABSOLUTE)
482 voltage += vadc->graph[prop->calibration].dx;
483
484 if (voltage < 0)
485 voltage = 0;
486
487 prescale = &vadc_prescale_ratios[prop->prescale];
488
489 voltage = voltage * prescale->den;
490
491 return div64_s64(voltage, prescale->num);
492}
493
494static int vadc_decimation_from_dt(u32 value)
495{
496 if (!is_power_of_2(value) || value < VADC_DECIMATION_MIN ||
497 value > VADC_DECIMATION_MAX)
498 return -EINVAL;
499
500 return __ffs64(value / VADC_DECIMATION_MIN);
501}
502
503static int vadc_prescaling_from_dt(u32 num, u32 den)
504{
505 unsigned int pre;
506
507 for (pre = 0; pre < ARRAY_SIZE(vadc_prescale_ratios); pre++)
508 if (vadc_prescale_ratios[pre].num == num &&
509 vadc_prescale_ratios[pre].den == den)
510 break;
511
512 if (pre == ARRAY_SIZE(vadc_prescale_ratios))
513 return -EINVAL;
514
515 return pre;
516}
517
518static int vadc_hw_settle_time_from_dt(u32 value)
519{
520 if ((value <= 1000 && value % 100) || (value > 1000 && value % 2000))
521 return -EINVAL;
522
523 if (value <= 1000)
524 value /= 100;
525 else
526 value = value / 2000 + 10;
527
528 return value;
529}
530
531static int vadc_avg_samples_from_dt(u32 value)
532{
533 if (!is_power_of_2(value) || value > VADC_AVG_SAMPLES_MAX)
534 return -EINVAL;
535
536 return __ffs64(value);
537}
538
539static int vadc_read_raw(struct iio_dev *indio_dev,
540 struct iio_chan_spec const *chan, int *val, int *val2,
541 long mask)
542{
543 struct vadc_priv *vadc = iio_priv(indio_dev);
544 struct vadc_channel_prop *prop;
545 u16 adc_code;
546 int ret;
547
548 switch (mask) {
549 case IIO_CHAN_INFO_PROCESSED:
550 prop = &vadc->chan_props[chan->address];
551 ret = vadc_do_conversion(vadc, prop, &adc_code);
552 if (ret)
553 break;
554
555 *val = vadc_calibrate(vadc, prop, adc_code);
556
557
558 *val /= 2;
559 *val -= KELVINMIL_CELSIUSMIL;
560 return IIO_VAL_INT;
561 case IIO_CHAN_INFO_RAW:
562 prop = &vadc->chan_props[chan->address];
563 ret = vadc_do_conversion(vadc, prop, &adc_code);
564 if (ret)
565 break;
566
567 *val = vadc_calibrate(vadc, prop, adc_code);
568 return IIO_VAL_INT;
569 case IIO_CHAN_INFO_SCALE:
570 *val = 0;
571 *val2 = 1000;
572 return IIO_VAL_INT_PLUS_MICRO;
573 default:
574 ret = -EINVAL;
575 break;
576 }
577
578 return ret;
579}
580
581static int vadc_of_xlate(struct iio_dev *indio_dev,
582 const struct of_phandle_args *iiospec)
583{
584 struct vadc_priv *vadc = iio_priv(indio_dev);
585 unsigned int i;
586
587 for (i = 0; i < vadc->nchannels; i++)
588 if (vadc->iio_chans[i].channel == iiospec->args[0])
589 return i;
590
591 return -EINVAL;
592}
593
594static const struct iio_info vadc_info = {
595 .read_raw = vadc_read_raw,
596 .of_xlate = vadc_of_xlate,
597 .driver_module = THIS_MODULE,
598};
599
600struct vadc_channels {
601 const char *datasheet_name;
602 unsigned int prescale_index;
603 enum iio_chan_type type;
604 long info_mask;
605};
606
607#define VADC_CHAN(_dname, _type, _mask, _pre) \
608 [VADC_##_dname] = { \
609 .datasheet_name = __stringify(_dname), \
610 .prescale_index = _pre, \
611 .type = _type, \
612 .info_mask = _mask \
613 }, \
614
615#define VADC_CHAN_TEMP(_dname, _pre) \
616 VADC_CHAN(_dname, IIO_TEMP, BIT(IIO_CHAN_INFO_PROCESSED), _pre) \
617
618#define VADC_CHAN_VOLT(_dname, _pre) \
619 VADC_CHAN(_dname, IIO_VOLTAGE, \
620 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
621 _pre) \
622
623
624
625
626
627
628static const struct vadc_channels vadc_chans[] = {
629 VADC_CHAN_VOLT(USBIN, 4)
630 VADC_CHAN_VOLT(DCIN, 4)
631 VADC_CHAN_VOLT(VCHG_SNS, 3)
632 VADC_CHAN_VOLT(SPARE1_03, 1)
633 VADC_CHAN_VOLT(USB_ID_MV, 1)
634 VADC_CHAN_VOLT(VCOIN, 1)
635 VADC_CHAN_VOLT(VBAT_SNS, 1)
636 VADC_CHAN_VOLT(VSYS, 1)
637 VADC_CHAN_TEMP(DIE_TEMP, 0)
638 VADC_CHAN_VOLT(REF_625MV, 0)
639 VADC_CHAN_VOLT(REF_1250MV, 0)
640 VADC_CHAN_VOLT(CHG_TEMP, 0)
641 VADC_CHAN_VOLT(SPARE1, 0)
642 VADC_CHAN_VOLT(SPARE2, 0)
643 VADC_CHAN_VOLT(GND_REF, 0)
644 VADC_CHAN_VOLT(VDD_VADC, 0)
645
646 VADC_CHAN_VOLT(P_MUX1_1_1, 0)
647 VADC_CHAN_VOLT(P_MUX2_1_1, 0)
648 VADC_CHAN_VOLT(P_MUX3_1_1, 0)
649 VADC_CHAN_VOLT(P_MUX4_1_1, 0)
650 VADC_CHAN_VOLT(P_MUX5_1_1, 0)
651 VADC_CHAN_VOLT(P_MUX6_1_1, 0)
652 VADC_CHAN_VOLT(P_MUX7_1_1, 0)
653 VADC_CHAN_VOLT(P_MUX8_1_1, 0)
654 VADC_CHAN_VOLT(P_MUX9_1_1, 0)
655 VADC_CHAN_VOLT(P_MUX10_1_1, 0)
656 VADC_CHAN_VOLT(P_MUX11_1_1, 0)
657 VADC_CHAN_VOLT(P_MUX12_1_1, 0)
658 VADC_CHAN_VOLT(P_MUX13_1_1, 0)
659 VADC_CHAN_VOLT(P_MUX14_1_1, 0)
660 VADC_CHAN_VOLT(P_MUX15_1_1, 0)
661 VADC_CHAN_VOLT(P_MUX16_1_1, 0)
662
663 VADC_CHAN_VOLT(P_MUX1_1_3, 1)
664 VADC_CHAN_VOLT(P_MUX2_1_3, 1)
665 VADC_CHAN_VOLT(P_MUX3_1_3, 1)
666 VADC_CHAN_VOLT(P_MUX4_1_3, 1)
667 VADC_CHAN_VOLT(P_MUX5_1_3, 1)
668 VADC_CHAN_VOLT(P_MUX6_1_3, 1)
669 VADC_CHAN_VOLT(P_MUX7_1_3, 1)
670 VADC_CHAN_VOLT(P_MUX8_1_3, 1)
671 VADC_CHAN_VOLT(P_MUX9_1_3, 1)
672 VADC_CHAN_VOLT(P_MUX10_1_3, 1)
673 VADC_CHAN_VOLT(P_MUX11_1_3, 1)
674 VADC_CHAN_VOLT(P_MUX12_1_3, 1)
675 VADC_CHAN_VOLT(P_MUX13_1_3, 1)
676 VADC_CHAN_VOLT(P_MUX14_1_3, 1)
677 VADC_CHAN_VOLT(P_MUX15_1_3, 1)
678 VADC_CHAN_VOLT(P_MUX16_1_3, 1)
679
680 VADC_CHAN_VOLT(LR_MUX1_BAT_THERM, 0)
681 VADC_CHAN_VOLT(LR_MUX2_BAT_ID, 0)
682 VADC_CHAN_VOLT(LR_MUX3_XO_THERM, 0)
683 VADC_CHAN_VOLT(LR_MUX4_AMUX_THM1, 0)
684 VADC_CHAN_VOLT(LR_MUX5_AMUX_THM2, 0)
685 VADC_CHAN_VOLT(LR_MUX6_AMUX_THM3, 0)
686 VADC_CHAN_VOLT(LR_MUX7_HW_ID, 0)
687 VADC_CHAN_VOLT(LR_MUX8_AMUX_THM4, 0)
688 VADC_CHAN_VOLT(LR_MUX9_AMUX_THM5, 0)
689 VADC_CHAN_VOLT(LR_MUX10_USB_ID, 0)
690 VADC_CHAN_VOLT(AMUX_PU1, 0)
691 VADC_CHAN_VOLT(AMUX_PU2, 0)
692 VADC_CHAN_VOLT(LR_MUX3_BUF_XO_THERM, 0)
693
694 VADC_CHAN_VOLT(LR_MUX1_PU1_BAT_THERM, 0)
695 VADC_CHAN_VOLT(LR_MUX2_PU1_BAT_ID, 0)
696 VADC_CHAN_VOLT(LR_MUX3_PU1_XO_THERM, 0)
697 VADC_CHAN_VOLT(LR_MUX4_PU1_AMUX_THM1, 0)
698 VADC_CHAN_VOLT(LR_MUX5_PU1_AMUX_THM2, 0)
699 VADC_CHAN_VOLT(LR_MUX6_PU1_AMUX_THM3, 0)
700 VADC_CHAN_VOLT(LR_MUX7_PU1_AMUX_HW_ID, 0)
701 VADC_CHAN_VOLT(LR_MUX8_PU1_AMUX_THM4, 0)
702 VADC_CHAN_VOLT(LR_MUX9_PU1_AMUX_THM5, 0)
703 VADC_CHAN_VOLT(LR_MUX10_PU1_AMUX_USB_ID, 0)
704 VADC_CHAN_VOLT(LR_MUX3_BUF_PU1_XO_THERM, 0)
705
706 VADC_CHAN_VOLT(LR_MUX1_PU2_BAT_THERM, 0)
707 VADC_CHAN_VOLT(LR_MUX2_PU2_BAT_ID, 0)
708 VADC_CHAN_VOLT(LR_MUX3_PU2_XO_THERM, 0)
709 VADC_CHAN_VOLT(LR_MUX4_PU2_AMUX_THM1, 0)
710 VADC_CHAN_VOLT(LR_MUX5_PU2_AMUX_THM2, 0)
711 VADC_CHAN_VOLT(LR_MUX6_PU2_AMUX_THM3, 0)
712 VADC_CHAN_VOLT(LR_MUX7_PU2_AMUX_HW_ID, 0)
713 VADC_CHAN_VOLT(LR_MUX8_PU2_AMUX_THM4, 0)
714 VADC_CHAN_VOLT(LR_MUX9_PU2_AMUX_THM5, 0)
715 VADC_CHAN_VOLT(LR_MUX10_PU2_AMUX_USB_ID, 0)
716 VADC_CHAN_VOLT(LR_MUX3_BUF_PU2_XO_THERM, 0)
717
718 VADC_CHAN_VOLT(LR_MUX1_PU1_PU2_BAT_THERM, 0)
719 VADC_CHAN_VOLT(LR_MUX2_PU1_PU2_BAT_ID, 0)
720 VADC_CHAN_VOLT(LR_MUX3_PU1_PU2_XO_THERM, 0)
721 VADC_CHAN_VOLT(LR_MUX4_PU1_PU2_AMUX_THM1, 0)
722 VADC_CHAN_VOLT(LR_MUX5_PU1_PU2_AMUX_THM2, 0)
723 VADC_CHAN_VOLT(LR_MUX6_PU1_PU2_AMUX_THM3, 0)
724 VADC_CHAN_VOLT(LR_MUX7_PU1_PU2_AMUX_HW_ID, 0)
725 VADC_CHAN_VOLT(LR_MUX8_PU1_PU2_AMUX_THM4, 0)
726 VADC_CHAN_VOLT(LR_MUX9_PU1_PU2_AMUX_THM5, 0)
727 VADC_CHAN_VOLT(LR_MUX10_PU1_PU2_AMUX_USB_ID, 0)
728 VADC_CHAN_VOLT(LR_MUX3_BUF_PU1_PU2_XO_THERM, 0)
729};
730
731static int vadc_get_dt_channel_data(struct device *dev,
732 struct vadc_channel_prop *prop,
733 struct device_node *node)
734{
735 const char *name = node->name;
736 u32 chan, value, varr[2];
737 int ret;
738
739 ret = of_property_read_u32(node, "reg", &chan);
740 if (ret) {
741 dev_err(dev, "invalid channel number %s\n", name);
742 return ret;
743 }
744
745 if (chan > VADC_CHAN_MAX || chan < VADC_CHAN_MIN) {
746 dev_err(dev, "%s invalid channel number %d\n", name, chan);
747 return -EINVAL;
748 }
749
750
751 prop->channel = chan;
752
753 ret = of_property_read_u32(node, "qcom,decimation", &value);
754 if (!ret) {
755 ret = vadc_decimation_from_dt(value);
756 if (ret < 0) {
757 dev_err(dev, "%02x invalid decimation %d\n",
758 chan, value);
759 return ret;
760 }
761 prop->decimation = ret;
762 } else {
763 prop->decimation = VADC_DEF_DECIMATION;
764 }
765
766 ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
767 if (!ret) {
768 ret = vadc_prescaling_from_dt(varr[0], varr[1]);
769 if (ret < 0) {
770 dev_err(dev, "%02x invalid pre-scaling <%d %d>\n",
771 chan, varr[0], varr[1]);
772 return ret;
773 }
774 prop->prescale = ret;
775 } else {
776 prop->prescale = vadc_chans[prop->channel].prescale_index;
777 }
778
779 ret = of_property_read_u32(node, "qcom,hw-settle-time", &value);
780 if (!ret) {
781 ret = vadc_hw_settle_time_from_dt(value);
782 if (ret < 0) {
783 dev_err(dev, "%02x invalid hw-settle-time %d us\n",
784 chan, value);
785 return ret;
786 }
787 prop->hw_settle_time = ret;
788 } else {
789 prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
790 }
791
792 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
793 if (!ret) {
794 ret = vadc_avg_samples_from_dt(value);
795 if (ret < 0) {
796 dev_err(dev, "%02x invalid avg-samples %d\n",
797 chan, value);
798 return ret;
799 }
800 prop->avg_samples = ret;
801 } else {
802 prop->avg_samples = VADC_DEF_AVG_SAMPLES;
803 }
804
805 if (of_property_read_bool(node, "qcom,ratiometric"))
806 prop->calibration = VADC_CALIB_RATIOMETRIC;
807 else
808 prop->calibration = VADC_CALIB_ABSOLUTE;
809
810 dev_dbg(dev, "%02x name %s\n", chan, name);
811
812 return 0;
813}
814
815static int vadc_get_dt_data(struct vadc_priv *vadc, struct device_node *node)
816{
817 const struct vadc_channels *vadc_chan;
818 struct iio_chan_spec *iio_chan;
819 struct vadc_channel_prop prop;
820 struct device_node *child;
821 unsigned int index = 0;
822 int ret;
823
824 vadc->nchannels = of_get_available_child_count(node);
825 if (!vadc->nchannels)
826 return -EINVAL;
827
828 vadc->iio_chans = devm_kcalloc(vadc->dev, vadc->nchannels,
829 sizeof(*vadc->iio_chans), GFP_KERNEL);
830 if (!vadc->iio_chans)
831 return -ENOMEM;
832
833 vadc->chan_props = devm_kcalloc(vadc->dev, vadc->nchannels,
834 sizeof(*vadc->chan_props), GFP_KERNEL);
835 if (!vadc->chan_props)
836 return -ENOMEM;
837
838 iio_chan = vadc->iio_chans;
839
840 for_each_available_child_of_node(node, child) {
841 ret = vadc_get_dt_channel_data(vadc->dev, &prop, child);
842 if (ret) {
843 of_node_put(child);
844 return ret;
845 }
846
847 vadc->chan_props[index] = prop;
848
849 vadc_chan = &vadc_chans[prop.channel];
850
851 iio_chan->channel = prop.channel;
852 iio_chan->datasheet_name = vadc_chan->datasheet_name;
853 iio_chan->info_mask_separate = vadc_chan->info_mask;
854 iio_chan->type = vadc_chan->type;
855 iio_chan->indexed = 1;
856 iio_chan->address = index++;
857
858 iio_chan++;
859 }
860
861
862 if (!vadc_get_channel(vadc, VADC_REF_1250MV)) {
863 dev_err(vadc->dev, "Please define 1.25V channel\n");
864 return -ENODEV;
865 }
866
867 if (!vadc_get_channel(vadc, VADC_REF_625MV)) {
868 dev_err(vadc->dev, "Please define 0.625V channel\n");
869 return -ENODEV;
870 }
871
872 if (!vadc_get_channel(vadc, VADC_VDD_VADC)) {
873 dev_err(vadc->dev, "Please define VDD channel\n");
874 return -ENODEV;
875 }
876
877 if (!vadc_get_channel(vadc, VADC_GND_REF)) {
878 dev_err(vadc->dev, "Please define GND channel\n");
879 return -ENODEV;
880 }
881
882 return 0;
883}
884
885static irqreturn_t vadc_isr(int irq, void *dev_id)
886{
887 struct vadc_priv *vadc = dev_id;
888
889 complete(&vadc->complete);
890
891 return IRQ_HANDLED;
892}
893
894static int vadc_check_revision(struct vadc_priv *vadc)
895{
896 u8 val;
897 int ret;
898
899 ret = vadc_read(vadc, VADC_PERPH_TYPE, &val);
900 if (ret)
901 return ret;
902
903 if (val < VADC_PERPH_TYPE_ADC) {
904 dev_err(vadc->dev, "%d is not ADC\n", val);
905 return -ENODEV;
906 }
907
908 ret = vadc_read(vadc, VADC_PERPH_SUBTYPE, &val);
909 if (ret)
910 return ret;
911
912 if (val < VADC_PERPH_SUBTYPE_VADC) {
913 dev_err(vadc->dev, "%d is not VADC\n", val);
914 return -ENODEV;
915 }
916
917 ret = vadc_read(vadc, VADC_REVISION2, &val);
918 if (ret)
919 return ret;
920
921 if (val < VADC_REVISION2_SUPPORTED_VADC) {
922 dev_err(vadc->dev, "revision %d not supported\n", val);
923 return -ENODEV;
924 }
925
926 return 0;
927}
928
929static int vadc_probe(struct platform_device *pdev)
930{
931 struct device_node *node = pdev->dev.of_node;
932 struct device *dev = &pdev->dev;
933 struct iio_dev *indio_dev;
934 struct vadc_priv *vadc;
935 struct regmap *regmap;
936 int ret, irq_eoc;
937 u32 reg;
938
939 regmap = dev_get_regmap(dev->parent, NULL);
940 if (!regmap)
941 return -ENODEV;
942
943 ret = of_property_read_u32(node, "reg", ®);
944 if (ret < 0)
945 return ret;
946
947 indio_dev = devm_iio_device_alloc(dev, sizeof(*vadc));
948 if (!indio_dev)
949 return -ENOMEM;
950
951 vadc = iio_priv(indio_dev);
952 vadc->regmap = regmap;
953 vadc->dev = dev;
954 vadc->base = reg;
955 vadc->are_ref_measured = false;
956 init_completion(&vadc->complete);
957 mutex_init(&vadc->lock);
958
959 ret = vadc_check_revision(vadc);
960 if (ret)
961 return ret;
962
963 ret = vadc_get_dt_data(vadc, node);
964 if (ret)
965 return ret;
966
967 irq_eoc = platform_get_irq(pdev, 0);
968 if (irq_eoc < 0) {
969 if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL)
970 return irq_eoc;
971 vadc->poll_eoc = true;
972 } else {
973 ret = devm_request_irq(dev, irq_eoc, vadc_isr, 0,
974 "spmi-vadc", vadc);
975 if (ret)
976 return ret;
977 }
978
979 ret = vadc_reset(vadc);
980 if (ret) {
981 dev_err(dev, "reset failed\n");
982 return ret;
983 }
984
985 ret = vadc_measure_ref_points(vadc);
986 if (ret)
987 return ret;
988
989 indio_dev->dev.parent = dev;
990 indio_dev->dev.of_node = node;
991 indio_dev->name = pdev->name;
992 indio_dev->modes = INDIO_DIRECT_MODE;
993 indio_dev->info = &vadc_info;
994 indio_dev->channels = vadc->iio_chans;
995 indio_dev->num_channels = vadc->nchannels;
996
997 return devm_iio_device_register(dev, indio_dev);
998}
999
1000static const struct of_device_id vadc_match_table[] = {
1001 { .compatible = "qcom,spmi-vadc" },
1002 { }
1003};
1004MODULE_DEVICE_TABLE(of, vadc_match_table);
1005
1006static struct platform_driver vadc_driver = {
1007 .driver = {
1008 .name = "qcom-spmi-vadc",
1009 .of_match_table = vadc_match_table,
1010 },
1011 .probe = vadc_probe,
1012};
1013module_platform_driver(vadc_driver);
1014
1015MODULE_ALIAS("platform:qcom-spmi-vadc");
1016MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver");
1017MODULE_LICENSE("GPL v2");
1018MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
1019MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
1020