1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
5 * Author: Arun R Murthy <arun.murthy@stericsson.com>
6 * Author: Daniel Willerud <daniel.willerud@stericsson.com>
7 * Author: Johan Palsson <johan.palsson@stericsson.com>
8 * Author: M'boumba Cedric Madianga
9 * Author: Linus Walleij <linus.walleij@linaro.org>
11 * AB8500 General Purpose ADC driver. The AB8500 uses reference voltages:
12 * VinVADC, and VADC relative to GND to do its job. It monitors main and backup
13 * battery voltages, AC (mains) voltage, USB cable voltage, as well as voltages
14 * representing the temperature of the chip die and battery, accessory
15 * detection by resistance measurements using relative voltages and GSM burst
18 * Some of the voltages are measured on external pins on the IC, such as
19 * battery temperature or "ADC aux" 1 and 2. Other voltages are internal rails
20 * from other parts of the ASIC such as main charger voltage, main and battery
21 * backup voltage or USB VBUS voltage. For this reason drivers for other
22 * parts of the system are required to obtain handles to the ADC to do work
23 * for them and the IIO driver provides arbitration among these consumers.
25 #include <linux/init.h>
26 #include <linux/bits.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/device.h>
30 #include <linux/interrupt.h>
31 #include <linux/spinlock.h>
32 #include <linux/delay.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/platform_device.h>
35 #include <linux/completion.h>
36 #include <linux/regulator/consumer.h>
37 #include <linux/random.h>
38 #include <linux/err.h>
39 #include <linux/slab.h>
40 #include <linux/mfd/abx500.h>
41 #include <linux/mfd/abx500/ab8500.h>
43 /* GPADC register offsets and bit definitions */
45 #define AB8500_GPADC_CTRL1_REG 0x00
46 /* GPADC control register 1 bits */
47 #define AB8500_GPADC_CTRL1_DISABLE 0x00
48 #define AB8500_GPADC_CTRL1_ENABLE BIT(0)
49 #define AB8500_GPADC_CTRL1_TRIG_ENA BIT(1)
50 #define AB8500_GPADC_CTRL1_START_SW_CONV BIT(2)
51 #define AB8500_GPADC_CTRL1_BTEMP_PULL_UP BIT(3)
52 /* 0 = use rising edge, 1 = use falling edge */
53 #define AB8500_GPADC_CTRL1_TRIG_EDGE BIT(4)
54 /* 0 = use VTVOUT, 1 = use VRTC as pull-up supply for battery temp NTC */
55 #define AB8500_GPADC_CTRL1_PUPSUPSEL BIT(5)
56 #define AB8500_GPADC_CTRL1_BUF_ENA BIT(6)
57 #define AB8500_GPADC_CTRL1_ICHAR_ENA BIT(7)
59 #define AB8500_GPADC_CTRL2_REG 0x01
60 #define AB8500_GPADC_CTRL3_REG 0x02
62 * GPADC control register 2 and 3 bits
63 * the bit layout is the same for SW and HW conversion set-up
65 #define AB8500_GPADC_CTRL2_AVG_1 0x00
66 #define AB8500_GPADC_CTRL2_AVG_4 BIT(5)
67 #define AB8500_GPADC_CTRL2_AVG_8 BIT(6)
68 #define AB8500_GPADC_CTRL2_AVG_16 (BIT(5) | BIT(6))
70 enum ab8500_gpadc_channel {
71 AB8500_GPADC_CHAN_UNUSED = 0x00,
72 AB8500_GPADC_CHAN_BAT_CTRL = 0x01,
73 AB8500_GPADC_CHAN_BAT_TEMP = 0x02,
74 /* This is not used on AB8505 */
75 AB8500_GPADC_CHAN_MAIN_CHARGER = 0x03,
76 AB8500_GPADC_CHAN_ACC_DET_1 = 0x04,
77 AB8500_GPADC_CHAN_ACC_DET_2 = 0x05,
78 AB8500_GPADC_CHAN_ADC_AUX_1 = 0x06,
79 AB8500_GPADC_CHAN_ADC_AUX_2 = 0x07,
80 AB8500_GPADC_CHAN_VBAT_A = 0x08,
81 AB8500_GPADC_CHAN_VBUS = 0x09,
82 AB8500_GPADC_CHAN_MAIN_CHARGER_CURRENT = 0x0a,
83 AB8500_GPADC_CHAN_USB_CHARGER_CURRENT = 0x0b,
84 AB8500_GPADC_CHAN_BACKUP_BAT = 0x0c,
86 AB8505_GPADC_CHAN_DIE_TEMP = 0x0d,
87 AB8500_GPADC_CHAN_ID = 0x0e,
88 AB8500_GPADC_CHAN_INTERNAL_TEST_1 = 0x0f,
89 AB8500_GPADC_CHAN_INTERNAL_TEST_2 = 0x10,
90 AB8500_GPADC_CHAN_INTERNAL_TEST_3 = 0x11,
91 /* FIXME: Applicable to all ASIC variants? */
92 AB8500_GPADC_CHAN_XTAL_TEMP = 0x12,
93 AB8500_GPADC_CHAN_VBAT_TRUE_MEAS = 0x13,
94 /* FIXME: Doesn't seem to work with pure AB8500 */
95 AB8500_GPADC_CHAN_BAT_CTRL_AND_IBAT = 0x1c,
96 AB8500_GPADC_CHAN_VBAT_MEAS_AND_IBAT = 0x1d,
97 AB8500_GPADC_CHAN_VBAT_TRUE_MEAS_AND_IBAT = 0x1e,
98 AB8500_GPADC_CHAN_BAT_TEMP_AND_IBAT = 0x1f,
100 * Virtual channel used only for ibat conversion to ampere.
101 * Battery current conversion (ibat) cannot be requested as a
102 * single conversion but it is always requested in combination
103 * with other input requests.
105 AB8500_GPADC_CHAN_IBAT_VIRTUAL = 0xFF,
108 #define AB8500_GPADC_AUTO_TIMER_REG 0x03
110 #define AB8500_GPADC_STAT_REG 0x04
111 #define AB8500_GPADC_STAT_BUSY BIT(0)
113 #define AB8500_GPADC_MANDATAL_REG 0x05
114 #define AB8500_GPADC_MANDATAH_REG 0x06
115 #define AB8500_GPADC_AUTODATAL_REG 0x07
116 #define AB8500_GPADC_AUTODATAH_REG 0x08
117 #define AB8500_GPADC_MUX_CTRL_REG 0x09
118 #define AB8540_GPADC_MANDATA2L_REG 0x09
119 #define AB8540_GPADC_MANDATA2H_REG 0x0A
120 #define AB8540_GPADC_APEAAX_REG 0x10
121 #define AB8540_GPADC_APEAAT_REG 0x11
122 #define AB8540_GPADC_APEAAM_REG 0x12
123 #define AB8540_GPADC_APEAAH_REG 0x13
124 #define AB8540_GPADC_APEAAL_REG 0x14
127 * OTP register offsets
130 #define AB8500_GPADC_CAL_1 0x0F
131 #define AB8500_GPADC_CAL_2 0x10
132 #define AB8500_GPADC_CAL_3 0x11
133 #define AB8500_GPADC_CAL_4 0x12
134 #define AB8500_GPADC_CAL_5 0x13
135 #define AB8500_GPADC_CAL_6 0x14
136 #define AB8500_GPADC_CAL_7 0x15
137 /* New calibration for 8540 */
138 #define AB8540_GPADC_OTP4_REG_7 0x38
139 #define AB8540_GPADC_OTP4_REG_6 0x39
140 #define AB8540_GPADC_OTP4_REG_5 0x3A
142 #define AB8540_GPADC_DIS_ZERO 0x00
143 #define AB8540_GPADC_EN_VBIAS_XTAL_TEMP 0x02
145 /* GPADC constants from AB8500 spec, UM0836 */
146 #define AB8500_ADC_RESOLUTION 1024
147 #define AB8500_ADC_CH_BTEMP_MIN 0
148 #define AB8500_ADC_CH_BTEMP_MAX 1350
149 #define AB8500_ADC_CH_DIETEMP_MIN 0
150 #define AB8500_ADC_CH_DIETEMP_MAX 1350
151 #define AB8500_ADC_CH_CHG_V_MIN 0
152 #define AB8500_ADC_CH_CHG_V_MAX 20030
153 #define AB8500_ADC_CH_ACCDET2_MIN 0
154 #define AB8500_ADC_CH_ACCDET2_MAX 2500
155 #define AB8500_ADC_CH_VBAT_MIN 2300
156 #define AB8500_ADC_CH_VBAT_MAX 4800
157 #define AB8500_ADC_CH_CHG_I_MIN 0
158 #define AB8500_ADC_CH_CHG_I_MAX 1500
159 #define AB8500_ADC_CH_BKBAT_MIN 0
160 #define AB8500_ADC_CH_BKBAT_MAX 3200
162 /* GPADC constants from AB8540 spec */
163 #define AB8500_ADC_CH_IBAT_MIN (-6000) /* mA range measured by ADC for ibat */
164 #define AB8500_ADC_CH_IBAT_MAX 6000
165 #define AB8500_ADC_CH_IBAT_MIN_V (-60) /* mV range measured by ADC for ibat */
166 #define AB8500_ADC_CH_IBAT_MAX_V 60
167 #define AB8500_GPADC_IBAT_VDROP_L (-56) /* mV */
168 #define AB8500_GPADC_IBAT_VDROP_H 56
170 /* This is used to not lose precision when dividing to get gain and offset */
171 #define AB8500_GPADC_CALIB_SCALE 1000
173 * Number of bits shift used to not lose precision
174 * when dividing to get ibat gain.
176 #define AB8500_GPADC_CALIB_SHIFT_IBAT 20
178 /* Time in ms before disabling regulator */
179 #define AB8500_GPADC_AUTOSUSPEND_DELAY 1
181 #define AB8500_GPADC_CONVERSION_TIME 500 /* ms */
183 enum ab8500_cal_channels {
184 AB8500_CAL_VMAIN = 0,
192 * struct ab8500_adc_cal_data - Table for storing gain and offset for the
193 * calibrated ADC channels
194 * @gain: Gain of the ADC channel
195 * @offset: Offset of the ADC channel
196 * @otp_calib_hi: Calibration from OTP
197 * @otp_calib_lo: Calibration from OTP
199 struct ab8500_adc_cal_data {
207 * struct ab8500_gpadc_chan_info - per-channel GPADC info
208 * @name: name of the channel
209 * @id: the internal AB8500 ID number for the channel
210 * @hardware_control: indicate that we want to use hardware ADC control
211 * on this channel, the default is software ADC control. Hardware control
212 * is normally only used to test the battery voltage during GSM bursts
213 * and needs a hardware trigger on the GPADCTrig pin of the ASIC.
214 * @falling_edge: indicate that we want to trigger on falling edge
215 * rather than rising edge, rising edge is the default
216 * @avg_sample: how many samples to average: must be 1, 4, 8 or 16.
217 * @trig_timer: how long to wait for the trigger, in 32kHz periods:
220 struct ab8500_gpadc_chan_info {
223 bool hardware_control;
230 * struct ab8500_gpadc - AB8500 GPADC device information
231 * @dev: pointer to the containing device
232 * @ab8500: pointer to the parent AB8500 device
233 * @chans: internal per-channel information container
234 * @nchans: number of channels
235 * @complete: pointer to the completion that indicates
236 * the completion of an gpadc conversion cycle
237 * @vddadc: pointer to the regulator supplying VDDADC
238 * @irq_sw: interrupt number that is used by gpadc for software ADC conversion
239 * @irq_hw: interrupt number that is used by gpadc for hardware ADC conversion
240 * @cal_data: array of ADC calibration data structs
242 struct ab8500_gpadc {
244 struct ab8500 *ab8500;
245 struct ab8500_gpadc_chan_info *chans;
247 struct completion complete;
248 struct regulator *vddadc;
251 struct ab8500_adc_cal_data cal_data[AB8500_CAL_NR];
254 static struct ab8500_gpadc_chan_info *
255 ab8500_gpadc_get_channel(struct ab8500_gpadc *gpadc, u8 chan)
257 struct ab8500_gpadc_chan_info *ch;
260 for (i = 0; i < gpadc->nchans; i++) {
261 ch = &gpadc->chans[i];
265 if (i == gpadc->nchans)
272 * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage
273 * @gpadc: GPADC instance
274 * @ch: the sampled channel this raw value is coming from
275 * @ad_value: the raw value
277 static int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc,
278 enum ab8500_gpadc_channel ch,
284 case AB8500_GPADC_CHAN_MAIN_CHARGER:
285 /* No calibration data available: just interpolate */
286 if (!gpadc->cal_data[AB8500_CAL_VMAIN].gain) {
287 res = AB8500_ADC_CH_CHG_V_MIN + (AB8500_ADC_CH_CHG_V_MAX -
288 AB8500_ADC_CH_CHG_V_MIN) * ad_value /
289 AB8500_ADC_RESOLUTION;
292 /* Here we can use calibration */
293 res = (int) (ad_value * gpadc->cal_data[AB8500_CAL_VMAIN].gain +
294 gpadc->cal_data[AB8500_CAL_VMAIN].offset) / AB8500_GPADC_CALIB_SCALE;
297 case AB8500_GPADC_CHAN_BAT_CTRL:
298 case AB8500_GPADC_CHAN_BAT_TEMP:
299 case AB8500_GPADC_CHAN_ACC_DET_1:
300 case AB8500_GPADC_CHAN_ADC_AUX_1:
301 case AB8500_GPADC_CHAN_ADC_AUX_2:
302 case AB8500_GPADC_CHAN_XTAL_TEMP:
303 /* No calibration data available: just interpolate */
304 if (!gpadc->cal_data[AB8500_CAL_BTEMP].gain) {
305 res = AB8500_ADC_CH_BTEMP_MIN + (AB8500_ADC_CH_BTEMP_MAX -
306 AB8500_ADC_CH_BTEMP_MIN) * ad_value /
307 AB8500_ADC_RESOLUTION;
310 /* Here we can use calibration */
311 res = (int) (ad_value * gpadc->cal_data[AB8500_CAL_BTEMP].gain +
312 gpadc->cal_data[AB8500_CAL_BTEMP].offset) / AB8500_GPADC_CALIB_SCALE;
315 case AB8500_GPADC_CHAN_VBAT_A:
316 case AB8500_GPADC_CHAN_VBAT_TRUE_MEAS:
317 /* No calibration data available: just interpolate */
318 if (!gpadc->cal_data[AB8500_CAL_VBAT].gain) {
319 res = AB8500_ADC_CH_VBAT_MIN + (AB8500_ADC_CH_VBAT_MAX -
320 AB8500_ADC_CH_VBAT_MIN) * ad_value /
321 AB8500_ADC_RESOLUTION;
324 /* Here we can use calibration */
325 res = (int) (ad_value * gpadc->cal_data[AB8500_CAL_VBAT].gain +
326 gpadc->cal_data[AB8500_CAL_VBAT].offset) / AB8500_GPADC_CALIB_SCALE;
329 case AB8505_GPADC_CHAN_DIE_TEMP:
330 res = AB8500_ADC_CH_DIETEMP_MIN +
331 (AB8500_ADC_CH_DIETEMP_MAX - AB8500_ADC_CH_DIETEMP_MIN) * ad_value /
332 AB8500_ADC_RESOLUTION;
335 case AB8500_GPADC_CHAN_ACC_DET_2:
336 res = AB8500_ADC_CH_ACCDET2_MIN +
337 (AB8500_ADC_CH_ACCDET2_MAX - AB8500_ADC_CH_ACCDET2_MIN) * ad_value /
338 AB8500_ADC_RESOLUTION;
341 case AB8500_GPADC_CHAN_VBUS:
342 res = AB8500_ADC_CH_CHG_V_MIN +
343 (AB8500_ADC_CH_CHG_V_MAX - AB8500_ADC_CH_CHG_V_MIN) * ad_value /
344 AB8500_ADC_RESOLUTION;
347 case AB8500_GPADC_CHAN_MAIN_CHARGER_CURRENT:
348 case AB8500_GPADC_CHAN_USB_CHARGER_CURRENT:
349 res = AB8500_ADC_CH_CHG_I_MIN +
350 (AB8500_ADC_CH_CHG_I_MAX - AB8500_ADC_CH_CHG_I_MIN) * ad_value /
351 AB8500_ADC_RESOLUTION;
354 case AB8500_GPADC_CHAN_BACKUP_BAT:
355 res = AB8500_ADC_CH_BKBAT_MIN +
356 (AB8500_ADC_CH_BKBAT_MAX - AB8500_ADC_CH_BKBAT_MIN) * ad_value /
357 AB8500_ADC_RESOLUTION;
360 case AB8500_GPADC_CHAN_IBAT_VIRTUAL:
361 /* No calibration data available: just interpolate */
362 if (!gpadc->cal_data[AB8500_CAL_IBAT].gain) {
363 res = AB8500_ADC_CH_IBAT_MIN + (AB8500_ADC_CH_IBAT_MAX -
364 AB8500_ADC_CH_IBAT_MIN) * ad_value /
365 AB8500_ADC_RESOLUTION;
368 /* Here we can use calibration */
369 res = (int) (ad_value * gpadc->cal_data[AB8500_CAL_IBAT].gain +
370 gpadc->cal_data[AB8500_CAL_IBAT].offset)
371 >> AB8500_GPADC_CALIB_SHIFT_IBAT;
376 "unknown channel ID: %d, not possible to convert\n",
386 static int ab8500_gpadc_read(struct ab8500_gpadc *gpadc,
387 const struct ab8500_gpadc_chan_info *ch,
392 unsigned long completion_timeout;
394 u8 low_data, high_data, low_data2, high_data2;
397 unsigned int delay_min = 0;
398 unsigned int delay_max = 0;
399 u8 data_low_addr, data_high_addr;
404 /* check if conversion is supported */
405 if ((gpadc->irq_sw <= 0) && !ch->hardware_control)
407 if ((gpadc->irq_hw <= 0) && ch->hardware_control)
410 /* Enable vddadc by grabbing PM runtime */
411 pm_runtime_get_sync(gpadc->dev);
413 /* Check if ADC is not busy, lock and proceed */
415 ret = abx500_get_register_interruptible(gpadc->dev,
416 AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
419 if (!(val & AB8500_GPADC_STAT_BUSY))
422 } while (++looplimit < 10);
423 if (looplimit >= 10 && (val & AB8500_GPADC_STAT_BUSY)) {
424 dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
430 ctrl1 = AB8500_GPADC_CTRL1_ENABLE;
432 /* Select the channel source and set average samples */
433 switch (ch->avg_sample) {
435 ctrl23 = ch->id | AB8500_GPADC_CTRL2_AVG_1;
438 ctrl23 = ch->id | AB8500_GPADC_CTRL2_AVG_4;
441 ctrl23 = ch->id | AB8500_GPADC_CTRL2_AVG_8;
444 ctrl23 = ch->id | AB8500_GPADC_CTRL2_AVG_16;
448 if (ch->hardware_control) {
449 ret = abx500_set_register_interruptible(gpadc->dev,
450 AB8500_GPADC, AB8500_GPADC_CTRL3_REG, ctrl23);
451 ctrl1 |= AB8500_GPADC_CTRL1_TRIG_ENA;
452 if (ch->falling_edge)
453 ctrl1 |= AB8500_GPADC_CTRL1_TRIG_EDGE;
455 ret = abx500_set_register_interruptible(gpadc->dev,
456 AB8500_GPADC, AB8500_GPADC_CTRL2_REG, ctrl23);
460 "gpadc_conversion: set avg samples failed\n");
465 * Enable ADC, buffering, select rising edge and enable ADC path
466 * charging current sense if it needed, ABB 3.0 needs some special
470 case AB8500_GPADC_CHAN_MAIN_CHARGER_CURRENT:
471 case AB8500_GPADC_CHAN_USB_CHARGER_CURRENT:
472 ctrl1 |= AB8500_GPADC_CTRL1_BUF_ENA |
473 AB8500_GPADC_CTRL1_ICHAR_ENA;
475 case AB8500_GPADC_CHAN_BAT_TEMP:
476 if (!is_ab8500_2p0_or_earlier(gpadc->ab8500)) {
477 ctrl1 |= AB8500_GPADC_CTRL1_BUF_ENA |
478 AB8500_GPADC_CTRL1_BTEMP_PULL_UP;
480 * Delay might be needed for ABB8500 cut 3.0, if not,
481 * remove when hardware will be available
483 delay_min = 1000; /* Delay in micro seconds */
484 delay_max = 10000; /* large range optimises sleepmode */
489 ctrl1 |= AB8500_GPADC_CTRL1_BUF_ENA;
493 /* Write configuration to control register 1 */
494 ret = abx500_set_register_interruptible(gpadc->dev,
495 AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ctrl1);
498 "gpadc_conversion: set Control register failed\n");
503 usleep_range(delay_min, delay_max);
505 if (ch->hardware_control) {
506 /* Set trigger delay timer */
507 ret = abx500_set_register_interruptible(gpadc->dev,
508 AB8500_GPADC, AB8500_GPADC_AUTO_TIMER_REG,
512 "gpadc_conversion: trig timer failed\n");
515 completion_timeout = 2 * HZ;
516 data_low_addr = AB8500_GPADC_AUTODATAL_REG;
517 data_high_addr = AB8500_GPADC_AUTODATAH_REG;
519 /* Start SW conversion */
520 ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
521 AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
522 AB8500_GPADC_CTRL1_START_SW_CONV,
523 AB8500_GPADC_CTRL1_START_SW_CONV);
526 "gpadc_conversion: start s/w conv failed\n");
529 completion_timeout = msecs_to_jiffies(AB8500_GPADC_CONVERSION_TIME);
530 data_low_addr = AB8500_GPADC_MANDATAL_REG;
531 data_high_addr = AB8500_GPADC_MANDATAH_REG;
534 /* Wait for completion of conversion */
535 if (!wait_for_completion_timeout(&gpadc->complete,
536 completion_timeout)) {
538 "timeout didn't receive GPADC conv interrupt\n");
543 /* Read the converted RAW data */
544 ret = abx500_get_register_interruptible(gpadc->dev,
545 AB8500_GPADC, data_low_addr, &low_data);
548 "gpadc_conversion: read low data failed\n");
552 ret = abx500_get_register_interruptible(gpadc->dev,
553 AB8500_GPADC, data_high_addr, &high_data);
556 "gpadc_conversion: read high data failed\n");
560 /* Check if double conversion is required */
561 if ((ch->id == AB8500_GPADC_CHAN_BAT_CTRL_AND_IBAT) ||
562 (ch->id == AB8500_GPADC_CHAN_VBAT_MEAS_AND_IBAT) ||
563 (ch->id == AB8500_GPADC_CHAN_VBAT_TRUE_MEAS_AND_IBAT) ||
564 (ch->id == AB8500_GPADC_CHAN_BAT_TEMP_AND_IBAT)) {
566 if (ch->hardware_control) {
570 "gpadc_conversion: only SW double conversion supported\n");
573 /* Read the converted RAW data 2 */
574 ret = abx500_get_register_interruptible(gpadc->dev,
575 AB8500_GPADC, AB8540_GPADC_MANDATA2L_REG,
579 "gpadc_conversion: read sw low data 2 failed\n");
583 ret = abx500_get_register_interruptible(gpadc->dev,
584 AB8500_GPADC, AB8540_GPADC_MANDATA2H_REG,
588 "gpadc_conversion: read sw high data 2 failed\n");
592 *ibat = (high_data2 << 8) | low_data2;
595 "gpadc_conversion: ibat not stored\n");
602 ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
603 AB8500_GPADC_CTRL1_REG, AB8500_GPADC_CTRL1_DISABLE);
605 dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
609 /* This eventually drops the regulator */
610 pm_runtime_mark_last_busy(gpadc->dev);
611 pm_runtime_put_autosuspend(gpadc->dev);
613 return (high_data << 8) | low_data;
617 * It has shown to be needed to turn off the GPADC if an error occurs,
618 * otherwise we might have problem when waiting for the busy bit in the
619 * GPADC status register to go low. In V1.1 there wait_for_completion
620 * seems to timeout when waiting for an interrupt.. Not seen in V2.0
622 (void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
623 AB8500_GPADC_CTRL1_REG, AB8500_GPADC_CTRL1_DISABLE);
624 pm_runtime_put(gpadc->dev);
626 "gpadc_conversion: Failed to AD convert channel %d\n", ch->id);
632 * ab8500_bm_gpadcconvend_handler() - isr for gpadc conversion completion
634 * @data: pointer to the data passed during request irq
636 * This is a interrupt service routine for gpadc conversion completion.
637 * Notifies the gpadc completion is completed and the converted raw value
638 * can be read from the registers.
639 * Returns IRQ status(IRQ_HANDLED)
641 static irqreturn_t ab8500_bm_gpadcconvend_handler(int irq, void *data)
643 struct ab8500_gpadc *gpadc = data;
645 complete(&gpadc->complete);
650 static int otp_cal_regs[] = {
660 static int otp4_cal_regs[] = {
661 AB8540_GPADC_OTP4_REG_7,
662 AB8540_GPADC_OTP4_REG_6,
663 AB8540_GPADC_OTP4_REG_5,
666 static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
669 int ret[ARRAY_SIZE(otp_cal_regs)];
670 u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];
671 int ret_otp4[ARRAY_SIZE(otp4_cal_regs)];
672 u8 gpadc_otp4[ARRAY_SIZE(otp4_cal_regs)];
673 int vmain_high, vmain_low;
674 int btemp_high, btemp_low;
675 int vbat_high, vbat_low;
676 int ibat_high, ibat_low;
677 s64 V_gain, V_offset, V2A_gain, V2A_offset;
679 /* First we read all OTP registers and store the error code */
680 for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
681 ret[i] = abx500_get_register_interruptible(gpadc->dev,
682 AB8500_OTP_EMUL, otp_cal_regs[i], &gpadc_cal[i]);
684 /* Continue anyway: maybe the other registers are OK */
685 dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
686 __func__, otp_cal_regs[i]);
688 /* Put this in the entropy pool as device-unique */
689 add_device_randomness(&ret[i], sizeof(ret[i]));
694 * The ADC calibration data is stored in OTP registers.
695 * The layout of the calibration data is outlined below and a more
696 * detailed description can be found in UM0836
698 * vm_h/l = vmain_high/low
699 * bt_h/l = btemp_high/low
700 * vb_h/l = vbat_high/low
702 * Data bits 8500/9540:
703 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
704 * |.......|.......|.......|.......|.......|.......|.......|.......
706 * |.......|.......|.......|.......|.......|.......|.......|.......
707 * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
708 * |.......|.......|.......|.......|.......|.......|.......|.......
709 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
710 * |.......|.......|.......|.......|.......|.......|.......|.......
711 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
712 * |.......|.......|.......|.......|.......|.......|.......|.......
713 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
714 * |.......|.......|.......|.......|.......|.......|.......|.......
715 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
716 * |.......|.......|.......|.......|.......|.......|.......|.......
717 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
718 * |.......|.......|.......|.......|.......|.......|.......|.......
722 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
723 * |.......|.......|.......|.......|.......|.......|.......|.......
725 * |.......|.......|.......|.......|.......|.......|.......|.......
726 * | vm_h9 | vm_h8 | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
727 * |.......|.......|.......|.......|.......|.......|.......|.......
728 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
729 * |.......|.......|.......|.......|.......|.......|.......|.......
730 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
731 * |.......|.......|.......|.......|.......|.......|.......|.......
732 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
733 * |.......|.......|.......|.......|.......|.......|.......|.......
734 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
735 * |.......|.......|.......|.......|.......|.......|.......|.......
736 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
737 * |.......|.......|.......|.......|.......|.......|.......|.......
741 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
742 * |.......|.......|.......|.......|.......|.......|.......|.......
743 * | | ib_h9 | ib_h8 | ib_h7
744 * |.......|.......|.......|.......|.......|.......|.......|.......
745 * | ib_h6 | ib_h5 | ib_h4 | ib_h3 | ib_h2 | ib_h1 | ib_h0 | ib_l5
746 * |.......|.......|.......|.......|.......|.......|.......|.......
747 * | ib_l4 | ib_l3 | ib_l2 | ib_l1 | ib_l0 |
750 * Ideal output ADC codes corresponding to injected input voltages
751 * during manufacturing is:
753 * vmain_high: Vin = 19500mV / ADC ideal code = 997
754 * vmain_low: Vin = 315mV / ADC ideal code = 16
755 * btemp_high: Vin = 1300mV / ADC ideal code = 985
756 * btemp_low: Vin = 21mV / ADC ideal code = 16
757 * vbat_high: Vin = 4700mV / ADC ideal code = 982
758 * vbat_low: Vin = 2380mV / ADC ideal code = 33
761 if (is_ab8540(gpadc->ab8500)) {
762 /* Calculate gain and offset for VMAIN if all reads succeeded*/
763 if (!(ret[1] < 0 || ret[2] < 0)) {
764 vmain_high = (((gpadc_cal[1] & 0xFF) << 2) |
765 ((gpadc_cal[2] & 0xC0) >> 6));
766 vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
768 gpadc->cal_data[AB8500_CAL_VMAIN].otp_calib_hi =
770 gpadc->cal_data[AB8500_CAL_VMAIN].otp_calib_lo =
773 gpadc->cal_data[AB8500_CAL_VMAIN].gain = AB8500_GPADC_CALIB_SCALE *
774 (19500 - 315) / (vmain_high - vmain_low);
775 gpadc->cal_data[AB8500_CAL_VMAIN].offset = AB8500_GPADC_CALIB_SCALE *
776 19500 - (AB8500_GPADC_CALIB_SCALE * (19500 - 315) /
777 (vmain_high - vmain_low)) * vmain_high;
779 gpadc->cal_data[AB8500_CAL_VMAIN].gain = 0;
782 /* Read IBAT calibration Data */
783 for (i = 0; i < ARRAY_SIZE(otp4_cal_regs); i++) {
784 ret_otp4[i] = abx500_get_register_interruptible(
785 gpadc->dev, AB8500_OTP_EMUL,
786 otp4_cal_regs[i], &gpadc_otp4[i]);
789 "%s: read otp4 reg 0x%02x failed\n",
790 __func__, otp4_cal_regs[i]);
793 /* Calculate gain and offset for IBAT if all reads succeeded */
794 if (!(ret_otp4[0] < 0 || ret_otp4[1] < 0 || ret_otp4[2] < 0)) {
795 ibat_high = (((gpadc_otp4[0] & 0x07) << 7) |
796 ((gpadc_otp4[1] & 0xFE) >> 1));
797 ibat_low = (((gpadc_otp4[1] & 0x01) << 5) |
798 ((gpadc_otp4[2] & 0xF8) >> 3));
800 gpadc->cal_data[AB8500_CAL_IBAT].otp_calib_hi =
802 gpadc->cal_data[AB8500_CAL_IBAT].otp_calib_lo =
805 V_gain = ((AB8500_GPADC_IBAT_VDROP_H - AB8500_GPADC_IBAT_VDROP_L)
806 << AB8500_GPADC_CALIB_SHIFT_IBAT) / (ibat_high - ibat_low);
808 V_offset = (AB8500_GPADC_IBAT_VDROP_H << AB8500_GPADC_CALIB_SHIFT_IBAT) -
809 (((AB8500_GPADC_IBAT_VDROP_H - AB8500_GPADC_IBAT_VDROP_L) <<
810 AB8500_GPADC_CALIB_SHIFT_IBAT) / (ibat_high - ibat_low))
813 * Result obtained is in mV (at a scale factor),
814 * we need to calculate gain and offset to get mA
816 V2A_gain = (AB8500_ADC_CH_IBAT_MAX - AB8500_ADC_CH_IBAT_MIN)/
817 (AB8500_ADC_CH_IBAT_MAX_V - AB8500_ADC_CH_IBAT_MIN_V);
818 V2A_offset = ((AB8500_ADC_CH_IBAT_MAX_V * AB8500_ADC_CH_IBAT_MIN -
819 AB8500_ADC_CH_IBAT_MAX * AB8500_ADC_CH_IBAT_MIN_V)
820 << AB8500_GPADC_CALIB_SHIFT_IBAT)
821 / (AB8500_ADC_CH_IBAT_MAX_V - AB8500_ADC_CH_IBAT_MIN_V);
823 gpadc->cal_data[AB8500_CAL_IBAT].gain =
825 gpadc->cal_data[AB8500_CAL_IBAT].offset =
826 V_offset * V2A_gain + V2A_offset;
828 gpadc->cal_data[AB8500_CAL_IBAT].gain = 0;
831 /* Calculate gain and offset for VMAIN if all reads succeeded */
832 if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
833 vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
834 ((gpadc_cal[1] & 0x3F) << 2) |
835 ((gpadc_cal[2] & 0xC0) >> 6));
836 vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
838 gpadc->cal_data[AB8500_CAL_VMAIN].otp_calib_hi =
840 gpadc->cal_data[AB8500_CAL_VMAIN].otp_calib_lo =
843 gpadc->cal_data[AB8500_CAL_VMAIN].gain = AB8500_GPADC_CALIB_SCALE *
844 (19500 - 315) / (vmain_high - vmain_low);
846 gpadc->cal_data[AB8500_CAL_VMAIN].offset = AB8500_GPADC_CALIB_SCALE *
847 19500 - (AB8500_GPADC_CALIB_SCALE * (19500 - 315) /
848 (vmain_high - vmain_low)) * vmain_high;
850 gpadc->cal_data[AB8500_CAL_VMAIN].gain = 0;
854 /* Calculate gain and offset for BTEMP if all reads succeeded */
855 if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
856 btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
857 (gpadc_cal[3] << 1) | ((gpadc_cal[4] & 0x80) >> 7));
858 btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
860 gpadc->cal_data[AB8500_CAL_BTEMP].otp_calib_hi = (u16)btemp_high;
861 gpadc->cal_data[AB8500_CAL_BTEMP].otp_calib_lo = (u16)btemp_low;
863 gpadc->cal_data[AB8500_CAL_BTEMP].gain =
864 AB8500_GPADC_CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
865 gpadc->cal_data[AB8500_CAL_BTEMP].offset = AB8500_GPADC_CALIB_SCALE * 1300 -
866 (AB8500_GPADC_CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low))
869 gpadc->cal_data[AB8500_CAL_BTEMP].gain = 0;
872 /* Calculate gain and offset for VBAT if all reads succeeded */
873 if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
874 vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
875 vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
877 gpadc->cal_data[AB8500_CAL_VBAT].otp_calib_hi = (u16)vbat_high;
878 gpadc->cal_data[AB8500_CAL_VBAT].otp_calib_lo = (u16)vbat_low;
880 gpadc->cal_data[AB8500_CAL_VBAT].gain = AB8500_GPADC_CALIB_SCALE *
881 (4700 - 2380) / (vbat_high - vbat_low);
882 gpadc->cal_data[AB8500_CAL_VBAT].offset = AB8500_GPADC_CALIB_SCALE * 4700 -
883 (AB8500_GPADC_CALIB_SCALE * (4700 - 2380) /
884 (vbat_high - vbat_low)) * vbat_high;
886 gpadc->cal_data[AB8500_CAL_VBAT].gain = 0;
890 static int ab8500_gpadc_read_raw(struct iio_dev *indio_dev,
891 struct iio_chan_spec const *chan,
892 int *val, int *val2, long mask)
894 struct ab8500_gpadc *gpadc = iio_priv(indio_dev);
895 const struct ab8500_gpadc_chan_info *ch;
899 ch = ab8500_gpadc_get_channel(gpadc, chan->address);
901 dev_err(gpadc->dev, "no such channel %lu\n",
906 raw_val = ab8500_gpadc_read(gpadc, ch, NULL);
910 if (mask == IIO_CHAN_INFO_RAW) {
915 if (mask == IIO_CHAN_INFO_PROCESSED) {
916 processed = ab8500_gpadc_ad_to_voltage(gpadc, ch->id, raw_val);
920 /* Return millivolt or milliamps or millicentigrades */
921 *val = processed * 1000;
928 static int ab8500_gpadc_of_xlate(struct iio_dev *indio_dev,
929 const struct of_phandle_args *iiospec)
933 for (i = 0; i < indio_dev->num_channels; i++)
934 if (indio_dev->channels[i].channel == iiospec->args[0])
940 static const struct iio_info ab8500_gpadc_info = {
941 .of_xlate = ab8500_gpadc_of_xlate,
942 .read_raw = ab8500_gpadc_read_raw,
946 static int ab8500_gpadc_runtime_suspend(struct device *dev)
948 struct iio_dev *indio_dev = dev_get_drvdata(dev);
949 struct ab8500_gpadc *gpadc = iio_priv(indio_dev);
951 regulator_disable(gpadc->vddadc);
956 static int ab8500_gpadc_runtime_resume(struct device *dev)
958 struct iio_dev *indio_dev = dev_get_drvdata(dev);
959 struct ab8500_gpadc *gpadc = iio_priv(indio_dev);
962 ret = regulator_enable(gpadc->vddadc);
964 dev_err(dev, "Failed to enable vddadc: %d\n", ret);
971 * ab8500_gpadc_parse_channel() - process devicetree channel configuration
972 * @dev: pointer to containing device
973 * @np: device tree node for the channel to configure
974 * @ch: channel info to fill in
975 * @iio_chan: IIO channel specification to fill in
977 * The devicetree will set up the channel for use with the specific device,
978 * and define usage for things like AUX GPADC inputs more precisely.
980 static int ab8500_gpadc_parse_channel(struct device *dev,
981 struct device_node *np,
982 struct ab8500_gpadc_chan_info *ch,
983 struct iio_chan_spec *iio_chan)
985 const char *name = np->name;
989 ret = of_property_read_u32(np, "reg", &chan);
991 dev_err(dev, "invalid channel number %s\n", name);
994 if (chan > AB8500_GPADC_CHAN_BAT_TEMP_AND_IBAT) {
995 dev_err(dev, "%s channel number out of range %d\n", name, chan);
999 iio_chan->channel = chan;
1000 iio_chan->datasheet_name = name;
1001 iio_chan->indexed = 1;
1002 iio_chan->address = chan;
1003 iio_chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1004 BIT(IIO_CHAN_INFO_PROCESSED);
1005 /* Most are voltages (also temperatures), some are currents */
1006 if ((chan == AB8500_GPADC_CHAN_MAIN_CHARGER_CURRENT) ||
1007 (chan == AB8500_GPADC_CHAN_USB_CHARGER_CURRENT))
1008 iio_chan->type = IIO_CURRENT;
1010 iio_chan->type = IIO_VOLTAGE;
1014 /* Sensible defaults */
1015 ch->avg_sample = 16;
1016 ch->hardware_control = false;
1017 ch->falling_edge = false;
1024 * ab8500_gpadc_parse_channels() - Parse the GPADC channels from DT
1025 * @gpadc: the GPADC to configure the channels for
1026 * @np: device tree node containing the channel configurations
1027 * @chans: the IIO channels we parsed
1028 * @nchans: the number of IIO channels we parsed
1030 static int ab8500_gpadc_parse_channels(struct ab8500_gpadc *gpadc,
1031 struct device_node *np,
1032 struct iio_chan_spec **chans_parsed,
1033 unsigned int *nchans_parsed)
1035 struct device_node *child;
1036 struct ab8500_gpadc_chan_info *ch;
1037 struct iio_chan_spec *iio_chans;
1038 unsigned int nchans;
1041 nchans = of_get_available_child_count(np);
1043 dev_err(gpadc->dev, "no channel children\n");
1046 dev_info(gpadc->dev, "found %d ADC channels\n", nchans);
1048 iio_chans = devm_kcalloc(gpadc->dev, nchans,
1049 sizeof(*iio_chans), GFP_KERNEL);
1053 gpadc->chans = devm_kcalloc(gpadc->dev, nchans,
1054 sizeof(*gpadc->chans), GFP_KERNEL);
1059 for_each_available_child_of_node(np, child) {
1060 struct iio_chan_spec *iio_chan;
1063 ch = &gpadc->chans[i];
1064 iio_chan = &iio_chans[i];
1066 ret = ab8500_gpadc_parse_channel(gpadc->dev, child, ch,
1074 gpadc->nchans = nchans;
1075 *chans_parsed = iio_chans;
1076 *nchans_parsed = nchans;
1081 static int ab8500_gpadc_probe(struct platform_device *pdev)
1083 struct ab8500_gpadc *gpadc;
1084 struct iio_dev *indio_dev;
1085 struct device *dev = &pdev->dev;
1086 struct device_node *np = pdev->dev.of_node;
1087 struct iio_chan_spec *iio_chans;
1088 unsigned int n_iio_chans;
1091 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
1095 platform_set_drvdata(pdev, indio_dev);
1096 gpadc = iio_priv(indio_dev);
1099 gpadc->ab8500 = dev_get_drvdata(dev->parent);
1101 ret = ab8500_gpadc_parse_channels(gpadc, np, &iio_chans, &n_iio_chans);
1105 gpadc->irq_sw = platform_get_irq_byname(pdev, "SW_CONV_END");
1106 if (gpadc->irq_sw < 0) {
1107 dev_err(dev, "failed to get platform sw_conv_end irq\n");
1108 return gpadc->irq_sw;
1111 gpadc->irq_hw = platform_get_irq_byname(pdev, "HW_CONV_END");
1112 if (gpadc->irq_hw < 0) {
1113 dev_err(dev, "failed to get platform hw_conv_end irq\n");
1114 return gpadc->irq_hw;
1117 /* Initialize completion used to notify completion of conversion */
1118 init_completion(&gpadc->complete);
1120 /* Request interrupts */
1121 ret = devm_request_threaded_irq(dev, gpadc->irq_sw, NULL,
1122 ab8500_bm_gpadcconvend_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
1123 "ab8500-gpadc-sw", gpadc);
1126 "failed to request sw conversion irq %d\n",
1131 ret = devm_request_threaded_irq(dev, gpadc->irq_hw, NULL,
1132 ab8500_bm_gpadcconvend_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
1133 "ab8500-gpadc-hw", gpadc);
1136 "Failed to request hw conversion irq: %d\n",
1141 /* The VTVout LDO used to power the AB8500 GPADC */
1142 gpadc->vddadc = devm_regulator_get(dev, "vddadc");
1143 if (IS_ERR(gpadc->vddadc)) {
1144 ret = PTR_ERR(gpadc->vddadc);
1145 dev_err(dev, "failed to get vddadc\n");
1149 ret = regulator_enable(gpadc->vddadc);
1151 dev_err(dev, "failed to enable vddadc: %d\n", ret);
1155 /* Enable runtime PM */
1156 pm_runtime_get_noresume(dev);
1157 pm_runtime_set_active(dev);
1158 pm_runtime_enable(dev);
1159 pm_runtime_set_autosuspend_delay(dev, AB8500_GPADC_AUTOSUSPEND_DELAY);
1160 pm_runtime_use_autosuspend(dev);
1162 ab8500_gpadc_read_calibration_data(gpadc);
1164 pm_runtime_put(dev);
1166 indio_dev->name = "ab8500-gpadc";
1167 indio_dev->modes = INDIO_DIRECT_MODE;
1168 indio_dev->info = &ab8500_gpadc_info;
1169 indio_dev->channels = iio_chans;
1170 indio_dev->num_channels = n_iio_chans;
1172 ret = devm_iio_device_register(dev, indio_dev);
1179 pm_runtime_get_sync(dev);
1180 pm_runtime_put_noidle(dev);
1181 pm_runtime_disable(dev);
1182 regulator_disable(gpadc->vddadc);
1187 static int ab8500_gpadc_remove(struct platform_device *pdev)
1189 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1190 struct ab8500_gpadc *gpadc = iio_priv(indio_dev);
1192 pm_runtime_get_sync(gpadc->dev);
1193 pm_runtime_put_noidle(gpadc->dev);
1194 pm_runtime_disable(gpadc->dev);
1195 regulator_disable(gpadc->vddadc);
1200 static const struct dev_pm_ops ab8500_gpadc_pm_ops = {
1201 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1202 pm_runtime_force_resume)
1203 SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend,
1204 ab8500_gpadc_runtime_resume,
1208 static struct platform_driver ab8500_gpadc_driver = {
1209 .probe = ab8500_gpadc_probe,
1210 .remove = ab8500_gpadc_remove,
1212 .name = "ab8500-gpadc",
1213 .pm = &ab8500_gpadc_pm_ops,
1216 builtin_platform_driver(ab8500_gpadc_driver);