xref: /openbmc/linux/drivers/iio/adc/ab8500-gpadc.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
107063bbfSLinus Walleij // SPDX-License-Identifier: GPL-2.0-only
207063bbfSLinus Walleij /*
307063bbfSLinus Walleij  * Copyright (C) ST-Ericsson SA 2010
407063bbfSLinus Walleij  *
507063bbfSLinus Walleij  * Author: Arun R Murthy <arun.murthy@stericsson.com>
607063bbfSLinus Walleij  * Author: Daniel Willerud <daniel.willerud@stericsson.com>
707063bbfSLinus Walleij  * Author: Johan Palsson <johan.palsson@stericsson.com>
807063bbfSLinus Walleij  * Author: M'boumba Cedric Madianga
907063bbfSLinus Walleij  * Author: Linus Walleij <linus.walleij@linaro.org>
1007063bbfSLinus Walleij  *
1107063bbfSLinus Walleij  * AB8500 General Purpose ADC driver. The AB8500 uses reference voltages:
1207063bbfSLinus Walleij  * VinVADC, and VADC relative to GND to do its job. It monitors main and backup
1307063bbfSLinus Walleij  * battery voltages, AC (mains) voltage, USB cable voltage, as well as voltages
1407063bbfSLinus Walleij  * representing the temperature of the chip die and battery, accessory
1507063bbfSLinus Walleij  * detection by resistance measurements using relative voltages and GSM burst
1607063bbfSLinus Walleij  * information.
1707063bbfSLinus Walleij  *
1807063bbfSLinus Walleij  * Some of the voltages are measured on external pins on the IC, such as
1907063bbfSLinus Walleij  * battery temperature or "ADC aux" 1 and 2. Other voltages are internal rails
2007063bbfSLinus Walleij  * from other parts of the ASIC such as main charger voltage, main and battery
2107063bbfSLinus Walleij  * backup voltage or USB VBUS voltage. For this reason drivers for other
2207063bbfSLinus Walleij  * parts of the system are required to obtain handles to the ADC to do work
2307063bbfSLinus Walleij  * for them and the IIO driver provides arbitration among these consumers.
2407063bbfSLinus Walleij  */
2507063bbfSLinus Walleij #include <linux/init.h>
2607063bbfSLinus Walleij #include <linux/bits.h>
2707063bbfSLinus Walleij #include <linux/iio/iio.h>
2807063bbfSLinus Walleij #include <linux/iio/sysfs.h>
2907063bbfSLinus Walleij #include <linux/device.h>
3007063bbfSLinus Walleij #include <linux/interrupt.h>
3107063bbfSLinus Walleij #include <linux/spinlock.h>
3207063bbfSLinus Walleij #include <linux/delay.h>
3307063bbfSLinus Walleij #include <linux/pm_runtime.h>
3407063bbfSLinus Walleij #include <linux/platform_device.h>
3507063bbfSLinus Walleij #include <linux/completion.h>
3607063bbfSLinus Walleij #include <linux/regulator/consumer.h>
3707063bbfSLinus Walleij #include <linux/random.h>
3807063bbfSLinus Walleij #include <linux/err.h>
3907063bbfSLinus Walleij #include <linux/slab.h>
4007063bbfSLinus Walleij #include <linux/mfd/abx500.h>
4107063bbfSLinus Walleij #include <linux/mfd/abx500/ab8500.h>
4207063bbfSLinus Walleij 
4307063bbfSLinus Walleij /* GPADC register offsets and bit definitions */
4407063bbfSLinus Walleij 
4507063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_REG		0x00
4607063bbfSLinus Walleij /* GPADC control register 1 bits */
4707063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_DISABLE		0x00
4807063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_ENABLE		BIT(0)
4907063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_TRIG_ENA		BIT(1)
5007063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_START_SW_CONV	BIT(2)
5107063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_BTEMP_PULL_UP	BIT(3)
5207063bbfSLinus Walleij /* 0 = use rising edge, 1 = use falling edge */
5307063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_TRIG_EDGE		BIT(4)
5407063bbfSLinus Walleij /* 0 = use VTVOUT, 1 = use VRTC as pull-up supply for battery temp NTC */
5507063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_PUPSUPSEL		BIT(5)
5607063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_BUF_ENA		BIT(6)
5707063bbfSLinus Walleij #define AB8500_GPADC_CTRL1_ICHAR_ENA		BIT(7)
5807063bbfSLinus Walleij 
5907063bbfSLinus Walleij #define AB8500_GPADC_CTRL2_REG		0x01
6007063bbfSLinus Walleij #define AB8500_GPADC_CTRL3_REG		0x02
6107063bbfSLinus Walleij /*
6207063bbfSLinus Walleij  * GPADC control register 2 and 3 bits
6307063bbfSLinus Walleij  * the bit layout is the same for SW and HW conversion set-up
6407063bbfSLinus Walleij  */
6507063bbfSLinus Walleij #define AB8500_GPADC_CTRL2_AVG_1		0x00
6607063bbfSLinus Walleij #define AB8500_GPADC_CTRL2_AVG_4		BIT(5)
6707063bbfSLinus Walleij #define AB8500_GPADC_CTRL2_AVG_8		BIT(6)
6807063bbfSLinus Walleij #define AB8500_GPADC_CTRL2_AVG_16		(BIT(5) | BIT(6))
6907063bbfSLinus Walleij 
7007063bbfSLinus Walleij enum ab8500_gpadc_channel {
7107063bbfSLinus Walleij 	AB8500_GPADC_CHAN_UNUSED = 0x00,
7207063bbfSLinus Walleij 	AB8500_GPADC_CHAN_BAT_CTRL = 0x01,
7307063bbfSLinus Walleij 	AB8500_GPADC_CHAN_BAT_TEMP = 0x02,
7407063bbfSLinus Walleij 	/* This is not used on AB8505 */
7507063bbfSLinus Walleij 	AB8500_GPADC_CHAN_MAIN_CHARGER = 0x03,
7607063bbfSLinus Walleij 	AB8500_GPADC_CHAN_ACC_DET_1 = 0x04,
7707063bbfSLinus Walleij 	AB8500_GPADC_CHAN_ACC_DET_2 = 0x05,
7807063bbfSLinus Walleij 	AB8500_GPADC_CHAN_ADC_AUX_1 = 0x06,
7907063bbfSLinus Walleij 	AB8500_GPADC_CHAN_ADC_AUX_2 = 0x07,
8007063bbfSLinus Walleij 	AB8500_GPADC_CHAN_VBAT_A = 0x08,
8107063bbfSLinus Walleij 	AB8500_GPADC_CHAN_VBUS = 0x09,
8207063bbfSLinus Walleij 	AB8500_GPADC_CHAN_MAIN_CHARGER_CURRENT = 0x0a,
8307063bbfSLinus Walleij 	AB8500_GPADC_CHAN_USB_CHARGER_CURRENT = 0x0b,
8407063bbfSLinus Walleij 	AB8500_GPADC_CHAN_BACKUP_BAT = 0x0c,
8507063bbfSLinus Walleij 	/* Only on AB8505 */
8607063bbfSLinus Walleij 	AB8505_GPADC_CHAN_DIE_TEMP = 0x0d,
8707063bbfSLinus Walleij 	AB8500_GPADC_CHAN_ID = 0x0e,
8807063bbfSLinus Walleij 	AB8500_GPADC_CHAN_INTERNAL_TEST_1 = 0x0f,
8907063bbfSLinus Walleij 	AB8500_GPADC_CHAN_INTERNAL_TEST_2 = 0x10,
9007063bbfSLinus Walleij 	AB8500_GPADC_CHAN_INTERNAL_TEST_3 = 0x11,
9107063bbfSLinus Walleij 	/* FIXME: Applicable to all ASIC variants? */
9207063bbfSLinus Walleij 	AB8500_GPADC_CHAN_XTAL_TEMP = 0x12,
9307063bbfSLinus Walleij 	AB8500_GPADC_CHAN_VBAT_TRUE_MEAS = 0x13,
9407063bbfSLinus Walleij 	/* FIXME: Doesn't seem to work with pure AB8500 */
9507063bbfSLinus Walleij 	AB8500_GPADC_CHAN_BAT_CTRL_AND_IBAT = 0x1c,
9607063bbfSLinus Walleij 	AB8500_GPADC_CHAN_VBAT_MEAS_AND_IBAT = 0x1d,
9707063bbfSLinus Walleij 	AB8500_GPADC_CHAN_VBAT_TRUE_MEAS_AND_IBAT = 0x1e,
9807063bbfSLinus Walleij 	AB8500_GPADC_CHAN_BAT_TEMP_AND_IBAT = 0x1f,
9907063bbfSLinus Walleij 	/*
10007063bbfSLinus Walleij 	 * Virtual channel used only for ibat conversion to ampere.
10107063bbfSLinus Walleij 	 * Battery current conversion (ibat) cannot be requested as a
10207063bbfSLinus Walleij 	 * single conversion but it is always requested in combination
10307063bbfSLinus Walleij 	 * with other input requests.
10407063bbfSLinus Walleij 	 */
10507063bbfSLinus Walleij 	AB8500_GPADC_CHAN_IBAT_VIRTUAL = 0xFF,
10607063bbfSLinus Walleij };
10707063bbfSLinus Walleij 
10807063bbfSLinus Walleij #define AB8500_GPADC_AUTO_TIMER_REG	0x03
10907063bbfSLinus Walleij 
11007063bbfSLinus Walleij #define AB8500_GPADC_STAT_REG		0x04
11107063bbfSLinus Walleij #define AB8500_GPADC_STAT_BUSY		BIT(0)
11207063bbfSLinus Walleij 
11307063bbfSLinus Walleij #define AB8500_GPADC_MANDATAL_REG	0x05
11407063bbfSLinus Walleij #define AB8500_GPADC_MANDATAH_REG	0x06
11507063bbfSLinus Walleij #define AB8500_GPADC_AUTODATAL_REG	0x07
11607063bbfSLinus Walleij #define AB8500_GPADC_AUTODATAH_REG	0x08
11707063bbfSLinus Walleij #define AB8500_GPADC_MUX_CTRL_REG	0x09
11807063bbfSLinus Walleij #define AB8540_GPADC_MANDATA2L_REG	0x09
11907063bbfSLinus Walleij #define AB8540_GPADC_MANDATA2H_REG	0x0A
12007063bbfSLinus Walleij #define AB8540_GPADC_APEAAX_REG		0x10
12107063bbfSLinus Walleij #define AB8540_GPADC_APEAAT_REG		0x11
12207063bbfSLinus Walleij #define AB8540_GPADC_APEAAM_REG		0x12
12307063bbfSLinus Walleij #define AB8540_GPADC_APEAAH_REG		0x13
12407063bbfSLinus Walleij #define AB8540_GPADC_APEAAL_REG		0x14
12507063bbfSLinus Walleij 
12607063bbfSLinus Walleij /*
12707063bbfSLinus Walleij  * OTP register offsets
12807063bbfSLinus Walleij  * Bank : 0x15
12907063bbfSLinus Walleij  */
13007063bbfSLinus Walleij #define AB8500_GPADC_CAL_1	0x0F
13107063bbfSLinus Walleij #define AB8500_GPADC_CAL_2	0x10
13207063bbfSLinus Walleij #define AB8500_GPADC_CAL_3	0x11
13307063bbfSLinus Walleij #define AB8500_GPADC_CAL_4	0x12
13407063bbfSLinus Walleij #define AB8500_GPADC_CAL_5	0x13
13507063bbfSLinus Walleij #define AB8500_GPADC_CAL_6	0x14
13607063bbfSLinus Walleij #define AB8500_GPADC_CAL_7	0x15
13707063bbfSLinus Walleij /* New calibration for 8540 */
13807063bbfSLinus Walleij #define AB8540_GPADC_OTP4_REG_7	0x38
13907063bbfSLinus Walleij #define AB8540_GPADC_OTP4_REG_6	0x39
14007063bbfSLinus Walleij #define AB8540_GPADC_OTP4_REG_5	0x3A
14107063bbfSLinus Walleij 
14207063bbfSLinus Walleij #define AB8540_GPADC_DIS_ZERO	0x00
14307063bbfSLinus Walleij #define AB8540_GPADC_EN_VBIAS_XTAL_TEMP	0x02
14407063bbfSLinus Walleij 
14507063bbfSLinus Walleij /* GPADC constants from AB8500 spec, UM0836 */
14607063bbfSLinus Walleij #define AB8500_ADC_RESOLUTION		1024
14707063bbfSLinus Walleij #define AB8500_ADC_CH_BTEMP_MIN		0
14807063bbfSLinus Walleij #define AB8500_ADC_CH_BTEMP_MAX		1350
14907063bbfSLinus Walleij #define AB8500_ADC_CH_DIETEMP_MIN	0
15007063bbfSLinus Walleij #define AB8500_ADC_CH_DIETEMP_MAX	1350
15107063bbfSLinus Walleij #define AB8500_ADC_CH_CHG_V_MIN		0
15207063bbfSLinus Walleij #define AB8500_ADC_CH_CHG_V_MAX		20030
15307063bbfSLinus Walleij #define AB8500_ADC_CH_ACCDET2_MIN	0
15407063bbfSLinus Walleij #define AB8500_ADC_CH_ACCDET2_MAX	2500
15507063bbfSLinus Walleij #define AB8500_ADC_CH_VBAT_MIN		2300
15607063bbfSLinus Walleij #define AB8500_ADC_CH_VBAT_MAX		4800
15707063bbfSLinus Walleij #define AB8500_ADC_CH_CHG_I_MIN		0
15807063bbfSLinus Walleij #define AB8500_ADC_CH_CHG_I_MAX		1500
15907063bbfSLinus Walleij #define AB8500_ADC_CH_BKBAT_MIN		0
16007063bbfSLinus Walleij #define AB8500_ADC_CH_BKBAT_MAX		3200
16107063bbfSLinus Walleij 
16207063bbfSLinus Walleij /* GPADC constants from AB8540 spec */
16307063bbfSLinus Walleij #define AB8500_ADC_CH_IBAT_MIN		(-6000) /* mA range measured by ADC for ibat */
16407063bbfSLinus Walleij #define AB8500_ADC_CH_IBAT_MAX		6000
16507063bbfSLinus Walleij #define AB8500_ADC_CH_IBAT_MIN_V	(-60)	/* mV range measured by ADC for ibat */
16607063bbfSLinus Walleij #define AB8500_ADC_CH_IBAT_MAX_V	60
16707063bbfSLinus Walleij #define AB8500_GPADC_IBAT_VDROP_L	(-56)  /* mV */
16807063bbfSLinus Walleij #define AB8500_GPADC_IBAT_VDROP_H	56
16907063bbfSLinus Walleij 
17007063bbfSLinus Walleij /* This is used to not lose precision when dividing to get gain and offset */
17107063bbfSLinus Walleij #define AB8500_GPADC_CALIB_SCALE	1000
17207063bbfSLinus Walleij /*
17307063bbfSLinus Walleij  * Number of bits shift used to not lose precision
17407063bbfSLinus Walleij  * when dividing to get ibat gain.
17507063bbfSLinus Walleij  */
17607063bbfSLinus Walleij #define AB8500_GPADC_CALIB_SHIFT_IBAT	20
17707063bbfSLinus Walleij 
17807063bbfSLinus Walleij /* Time in ms before disabling regulator */
17907063bbfSLinus Walleij #define AB8500_GPADC_AUTOSUSPEND_DELAY	1
18007063bbfSLinus Walleij 
18107063bbfSLinus Walleij #define AB8500_GPADC_CONVERSION_TIME	500 /* ms */
18207063bbfSLinus Walleij 
18307063bbfSLinus Walleij enum ab8500_cal_channels {
18407063bbfSLinus Walleij 	AB8500_CAL_VMAIN = 0,
18507063bbfSLinus Walleij 	AB8500_CAL_BTEMP,
18607063bbfSLinus Walleij 	AB8500_CAL_VBAT,
18707063bbfSLinus Walleij 	AB8500_CAL_IBAT,
18807063bbfSLinus Walleij 	AB8500_CAL_NR,
18907063bbfSLinus Walleij };
19007063bbfSLinus Walleij 
19107063bbfSLinus Walleij /**
19207063bbfSLinus Walleij  * struct ab8500_adc_cal_data - Table for storing gain and offset for the
19307063bbfSLinus Walleij  * calibrated ADC channels
19407063bbfSLinus Walleij  * @gain: Gain of the ADC channel
19507063bbfSLinus Walleij  * @offset: Offset of the ADC channel
19607063bbfSLinus Walleij  * @otp_calib_hi: Calibration from OTP
19707063bbfSLinus Walleij  * @otp_calib_lo: Calibration from OTP
19807063bbfSLinus Walleij  */
19907063bbfSLinus Walleij struct ab8500_adc_cal_data {
20007063bbfSLinus Walleij 	s64 gain;
20107063bbfSLinus Walleij 	s64 offset;
20207063bbfSLinus Walleij 	u16 otp_calib_hi;
20307063bbfSLinus Walleij 	u16 otp_calib_lo;
20407063bbfSLinus Walleij };
20507063bbfSLinus Walleij 
20607063bbfSLinus Walleij /**
20707063bbfSLinus Walleij  * struct ab8500_gpadc_chan_info - per-channel GPADC info
20807063bbfSLinus Walleij  * @name: name of the channel
20907063bbfSLinus Walleij  * @id: the internal AB8500 ID number for the channel
21007063bbfSLinus Walleij  * @hardware_control: indicate that we want to use hardware ADC control
21107063bbfSLinus Walleij  * on this channel, the default is software ADC control. Hardware control
21207063bbfSLinus Walleij  * is normally only used to test the battery voltage during GSM bursts
21307063bbfSLinus Walleij  * and needs a hardware trigger on the GPADCTrig pin of the ASIC.
21407063bbfSLinus Walleij  * @falling_edge: indicate that we want to trigger on falling edge
21507063bbfSLinus Walleij  * rather than rising edge, rising edge is the default
21607063bbfSLinus Walleij  * @avg_sample: how many samples to average: must be 1, 4, 8 or 16.
21707063bbfSLinus Walleij  * @trig_timer: how long to wait for the trigger, in 32kHz periods:
21807063bbfSLinus Walleij  * 0 .. 255 periods
21907063bbfSLinus Walleij  */
22007063bbfSLinus Walleij struct ab8500_gpadc_chan_info {
22107063bbfSLinus Walleij 	const char *name;
22207063bbfSLinus Walleij 	u8 id;
22307063bbfSLinus Walleij 	bool hardware_control;
22407063bbfSLinus Walleij 	bool falling_edge;
22507063bbfSLinus Walleij 	u8 avg_sample;
22607063bbfSLinus Walleij 	u8 trig_timer;
22707063bbfSLinus Walleij };
22807063bbfSLinus Walleij 
22907063bbfSLinus Walleij /**
23007063bbfSLinus Walleij  * struct ab8500_gpadc - AB8500 GPADC device information
23107063bbfSLinus Walleij  * @dev: pointer to the containing device
23207063bbfSLinus Walleij  * @ab8500: pointer to the parent AB8500 device
23307063bbfSLinus Walleij  * @chans: internal per-channel information container
23407063bbfSLinus Walleij  * @nchans: number of channels
23507063bbfSLinus Walleij  * @complete: pointer to the completion that indicates
23607063bbfSLinus Walleij  * the completion of an gpadc conversion cycle
23707063bbfSLinus Walleij  * @vddadc: pointer to the regulator supplying VDDADC
23807063bbfSLinus Walleij  * @irq_sw: interrupt number that is used by gpadc for software ADC conversion
23907063bbfSLinus Walleij  * @irq_hw: interrupt number that is used by gpadc for hardware ADC conversion
24007063bbfSLinus Walleij  * @cal_data: array of ADC calibration data structs
24107063bbfSLinus Walleij  */
24207063bbfSLinus Walleij struct ab8500_gpadc {
24307063bbfSLinus Walleij 	struct device *dev;
24407063bbfSLinus Walleij 	struct ab8500 *ab8500;
24507063bbfSLinus Walleij 	struct ab8500_gpadc_chan_info *chans;
24607063bbfSLinus Walleij 	unsigned int nchans;
24707063bbfSLinus Walleij 	struct completion complete;
24807063bbfSLinus Walleij 	struct regulator *vddadc;
24907063bbfSLinus Walleij 	int irq_sw;
25007063bbfSLinus Walleij 	int irq_hw;
25107063bbfSLinus Walleij 	struct ab8500_adc_cal_data cal_data[AB8500_CAL_NR];
25207063bbfSLinus Walleij };
25307063bbfSLinus Walleij 
25407063bbfSLinus Walleij static struct ab8500_gpadc_chan_info *
ab8500_gpadc_get_channel(struct ab8500_gpadc * gpadc,u8 chan)25507063bbfSLinus Walleij ab8500_gpadc_get_channel(struct ab8500_gpadc *gpadc, u8 chan)
25607063bbfSLinus Walleij {
25707063bbfSLinus Walleij 	struct ab8500_gpadc_chan_info *ch;
25807063bbfSLinus Walleij 	int i;
25907063bbfSLinus Walleij 
26007063bbfSLinus Walleij 	for (i = 0; i < gpadc->nchans; i++) {
26107063bbfSLinus Walleij 		ch = &gpadc->chans[i];
26207063bbfSLinus Walleij 		if (ch->id == chan)
26307063bbfSLinus Walleij 			break;
26407063bbfSLinus Walleij 	}
26507063bbfSLinus Walleij 	if (i == gpadc->nchans)
26607063bbfSLinus Walleij 		return NULL;
26707063bbfSLinus Walleij 
26807063bbfSLinus Walleij 	return ch;
26907063bbfSLinus Walleij }
27007063bbfSLinus Walleij 
27107063bbfSLinus Walleij /**
27207063bbfSLinus Walleij  * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage
27307063bbfSLinus Walleij  * @gpadc: GPADC instance
27407063bbfSLinus Walleij  * @ch: the sampled channel this raw value is coming from
27507063bbfSLinus Walleij  * @ad_value: the raw value
27607063bbfSLinus Walleij  */
ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc * gpadc,enum ab8500_gpadc_channel ch,int ad_value)27707063bbfSLinus Walleij static int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc,
27807063bbfSLinus Walleij 				      enum ab8500_gpadc_channel ch,
27907063bbfSLinus Walleij 				      int ad_value)
28007063bbfSLinus Walleij {
28107063bbfSLinus Walleij 	int res;
28207063bbfSLinus Walleij 
28307063bbfSLinus Walleij 	switch (ch) {
28407063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_MAIN_CHARGER:
28507063bbfSLinus Walleij 		/* No calibration data available: just interpolate */
28607063bbfSLinus Walleij 		if (!gpadc->cal_data[AB8500_CAL_VMAIN].gain) {
28707063bbfSLinus Walleij 			res = AB8500_ADC_CH_CHG_V_MIN + (AB8500_ADC_CH_CHG_V_MAX -
28807063bbfSLinus Walleij 				AB8500_ADC_CH_CHG_V_MIN) * ad_value /
28907063bbfSLinus Walleij 				AB8500_ADC_RESOLUTION;
29007063bbfSLinus Walleij 			break;
29107063bbfSLinus Walleij 		}
29207063bbfSLinus Walleij 		/* Here we can use calibration */
29307063bbfSLinus Walleij 		res = (int) (ad_value * gpadc->cal_data[AB8500_CAL_VMAIN].gain +
29407063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].offset) / AB8500_GPADC_CALIB_SCALE;
29507063bbfSLinus Walleij 		break;
29607063bbfSLinus Walleij 
29707063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_BAT_CTRL:
29807063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_BAT_TEMP:
29907063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_ACC_DET_1:
30007063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_ADC_AUX_1:
30107063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_ADC_AUX_2:
30207063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_XTAL_TEMP:
30307063bbfSLinus Walleij 		/* No calibration data available: just interpolate */
30407063bbfSLinus Walleij 		if (!gpadc->cal_data[AB8500_CAL_BTEMP].gain) {
30507063bbfSLinus Walleij 			res = AB8500_ADC_CH_BTEMP_MIN + (AB8500_ADC_CH_BTEMP_MAX -
30607063bbfSLinus Walleij 				AB8500_ADC_CH_BTEMP_MIN) * ad_value /
30707063bbfSLinus Walleij 				AB8500_ADC_RESOLUTION;
30807063bbfSLinus Walleij 			break;
30907063bbfSLinus Walleij 		}
31007063bbfSLinus Walleij 		/* Here we can use calibration */
31107063bbfSLinus Walleij 		res = (int) (ad_value * gpadc->cal_data[AB8500_CAL_BTEMP].gain +
31207063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_BTEMP].offset) / AB8500_GPADC_CALIB_SCALE;
31307063bbfSLinus Walleij 		break;
31407063bbfSLinus Walleij 
31507063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_VBAT_A:
31607063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_VBAT_TRUE_MEAS:
31707063bbfSLinus Walleij 		/* No calibration data available: just interpolate */
31807063bbfSLinus Walleij 		if (!gpadc->cal_data[AB8500_CAL_VBAT].gain) {
31907063bbfSLinus Walleij 			res = AB8500_ADC_CH_VBAT_MIN + (AB8500_ADC_CH_VBAT_MAX -
32007063bbfSLinus Walleij 				AB8500_ADC_CH_VBAT_MIN) * ad_value /
32107063bbfSLinus Walleij 				AB8500_ADC_RESOLUTION;
32207063bbfSLinus Walleij 			break;
32307063bbfSLinus Walleij 		}
32407063bbfSLinus Walleij 		/* Here we can use calibration */
32507063bbfSLinus Walleij 		res = (int) (ad_value * gpadc->cal_data[AB8500_CAL_VBAT].gain +
32607063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VBAT].offset) / AB8500_GPADC_CALIB_SCALE;
32707063bbfSLinus Walleij 		break;
32807063bbfSLinus Walleij 
32907063bbfSLinus Walleij 	case AB8505_GPADC_CHAN_DIE_TEMP:
33007063bbfSLinus Walleij 		res = AB8500_ADC_CH_DIETEMP_MIN +
33107063bbfSLinus Walleij 			(AB8500_ADC_CH_DIETEMP_MAX - AB8500_ADC_CH_DIETEMP_MIN) * ad_value /
33207063bbfSLinus Walleij 			AB8500_ADC_RESOLUTION;
33307063bbfSLinus Walleij 		break;
33407063bbfSLinus Walleij 
33507063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_ACC_DET_2:
33607063bbfSLinus Walleij 		res = AB8500_ADC_CH_ACCDET2_MIN +
33707063bbfSLinus Walleij 			(AB8500_ADC_CH_ACCDET2_MAX - AB8500_ADC_CH_ACCDET2_MIN) * ad_value /
33807063bbfSLinus Walleij 			AB8500_ADC_RESOLUTION;
33907063bbfSLinus Walleij 		break;
34007063bbfSLinus Walleij 
34107063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_VBUS:
34207063bbfSLinus Walleij 		res = AB8500_ADC_CH_CHG_V_MIN +
34307063bbfSLinus Walleij 			(AB8500_ADC_CH_CHG_V_MAX - AB8500_ADC_CH_CHG_V_MIN) * ad_value /
34407063bbfSLinus Walleij 			AB8500_ADC_RESOLUTION;
34507063bbfSLinus Walleij 		break;
34607063bbfSLinus Walleij 
34707063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_MAIN_CHARGER_CURRENT:
34807063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_USB_CHARGER_CURRENT:
34907063bbfSLinus Walleij 		res = AB8500_ADC_CH_CHG_I_MIN +
35007063bbfSLinus Walleij 			(AB8500_ADC_CH_CHG_I_MAX - AB8500_ADC_CH_CHG_I_MIN) * ad_value /
35107063bbfSLinus Walleij 			AB8500_ADC_RESOLUTION;
35207063bbfSLinus Walleij 		break;
35307063bbfSLinus Walleij 
35407063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_BACKUP_BAT:
35507063bbfSLinus Walleij 		res = AB8500_ADC_CH_BKBAT_MIN +
35607063bbfSLinus Walleij 			(AB8500_ADC_CH_BKBAT_MAX - AB8500_ADC_CH_BKBAT_MIN) * ad_value /
35707063bbfSLinus Walleij 			AB8500_ADC_RESOLUTION;
35807063bbfSLinus Walleij 		break;
35907063bbfSLinus Walleij 
36007063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_IBAT_VIRTUAL:
36107063bbfSLinus Walleij 		/* No calibration data available: just interpolate */
36207063bbfSLinus Walleij 		if (!gpadc->cal_data[AB8500_CAL_IBAT].gain) {
36307063bbfSLinus Walleij 			res = AB8500_ADC_CH_IBAT_MIN + (AB8500_ADC_CH_IBAT_MAX -
36407063bbfSLinus Walleij 				AB8500_ADC_CH_IBAT_MIN) * ad_value /
36507063bbfSLinus Walleij 				AB8500_ADC_RESOLUTION;
36607063bbfSLinus Walleij 			break;
36707063bbfSLinus Walleij 		}
36807063bbfSLinus Walleij 		/* Here we can use calibration */
36907063bbfSLinus Walleij 		res = (int) (ad_value * gpadc->cal_data[AB8500_CAL_IBAT].gain +
37007063bbfSLinus Walleij 				gpadc->cal_data[AB8500_CAL_IBAT].offset)
37107063bbfSLinus Walleij 				>> AB8500_GPADC_CALIB_SHIFT_IBAT;
37207063bbfSLinus Walleij 		break;
37307063bbfSLinus Walleij 
37407063bbfSLinus Walleij 	default:
37507063bbfSLinus Walleij 		dev_err(gpadc->dev,
37607063bbfSLinus Walleij 			"unknown channel ID: %d, not possible to convert\n",
37707063bbfSLinus Walleij 			ch);
37807063bbfSLinus Walleij 		res = -EINVAL;
37907063bbfSLinus Walleij 		break;
38007063bbfSLinus Walleij 
38107063bbfSLinus Walleij 	}
38207063bbfSLinus Walleij 
38307063bbfSLinus Walleij 	return res;
38407063bbfSLinus Walleij }
38507063bbfSLinus Walleij 
ab8500_gpadc_read(struct ab8500_gpadc * gpadc,const struct ab8500_gpadc_chan_info * ch,int * ibat)38607063bbfSLinus Walleij static int ab8500_gpadc_read(struct ab8500_gpadc *gpadc,
38707063bbfSLinus Walleij 			     const struct ab8500_gpadc_chan_info *ch,
38807063bbfSLinus Walleij 			     int *ibat)
38907063bbfSLinus Walleij {
39007063bbfSLinus Walleij 	int ret;
39107063bbfSLinus Walleij 	int looplimit = 0;
39207063bbfSLinus Walleij 	unsigned long completion_timeout;
39307063bbfSLinus Walleij 	u8 val;
39407063bbfSLinus Walleij 	u8 low_data, high_data, low_data2, high_data2;
39507063bbfSLinus Walleij 	u8 ctrl1;
39607063bbfSLinus Walleij 	u8 ctrl23;
39707063bbfSLinus Walleij 	unsigned int delay_min = 0;
39807063bbfSLinus Walleij 	unsigned int delay_max = 0;
39907063bbfSLinus Walleij 	u8 data_low_addr, data_high_addr;
40007063bbfSLinus Walleij 
40107063bbfSLinus Walleij 	if (!gpadc)
40207063bbfSLinus Walleij 		return -ENODEV;
40307063bbfSLinus Walleij 
40407063bbfSLinus Walleij 	/* check if conversion is supported */
40507063bbfSLinus Walleij 	if ((gpadc->irq_sw <= 0) && !ch->hardware_control)
40607063bbfSLinus Walleij 		return -ENOTSUPP;
40707063bbfSLinus Walleij 	if ((gpadc->irq_hw <= 0) && ch->hardware_control)
40807063bbfSLinus Walleij 		return -ENOTSUPP;
40907063bbfSLinus Walleij 
41007063bbfSLinus Walleij 	/* Enable vddadc by grabbing PM runtime */
41107063bbfSLinus Walleij 	pm_runtime_get_sync(gpadc->dev);
41207063bbfSLinus Walleij 
41307063bbfSLinus Walleij 	/* Check if ADC is not busy, lock and proceed */
41407063bbfSLinus Walleij 	do {
41507063bbfSLinus Walleij 		ret = abx500_get_register_interruptible(gpadc->dev,
41607063bbfSLinus Walleij 			AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
41707063bbfSLinus Walleij 		if (ret < 0)
41807063bbfSLinus Walleij 			goto out;
41907063bbfSLinus Walleij 		if (!(val & AB8500_GPADC_STAT_BUSY))
42007063bbfSLinus Walleij 			break;
42107063bbfSLinus Walleij 		msleep(20);
42207063bbfSLinus Walleij 	} while (++looplimit < 10);
42307063bbfSLinus Walleij 	if (looplimit >= 10 && (val & AB8500_GPADC_STAT_BUSY)) {
42407063bbfSLinus Walleij 		dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
42507063bbfSLinus Walleij 		ret = -EINVAL;
42607063bbfSLinus Walleij 		goto out;
42707063bbfSLinus Walleij 	}
42807063bbfSLinus Walleij 
42907063bbfSLinus Walleij 	/* Enable GPADC */
43007063bbfSLinus Walleij 	ctrl1 = AB8500_GPADC_CTRL1_ENABLE;
43107063bbfSLinus Walleij 
43207063bbfSLinus Walleij 	/* Select the channel source and set average samples */
43307063bbfSLinus Walleij 	switch (ch->avg_sample) {
43407063bbfSLinus Walleij 	case 1:
43507063bbfSLinus Walleij 		ctrl23 = ch->id | AB8500_GPADC_CTRL2_AVG_1;
43607063bbfSLinus Walleij 		break;
43707063bbfSLinus Walleij 	case 4:
43807063bbfSLinus Walleij 		ctrl23 = ch->id | AB8500_GPADC_CTRL2_AVG_4;
43907063bbfSLinus Walleij 		break;
44007063bbfSLinus Walleij 	case 8:
44107063bbfSLinus Walleij 		ctrl23 = ch->id | AB8500_GPADC_CTRL2_AVG_8;
44207063bbfSLinus Walleij 		break;
44307063bbfSLinus Walleij 	default:
44407063bbfSLinus Walleij 		ctrl23 = ch->id | AB8500_GPADC_CTRL2_AVG_16;
44507063bbfSLinus Walleij 		break;
44607063bbfSLinus Walleij 	}
44707063bbfSLinus Walleij 
44807063bbfSLinus Walleij 	if (ch->hardware_control) {
44907063bbfSLinus Walleij 		ret = abx500_set_register_interruptible(gpadc->dev,
45007063bbfSLinus Walleij 				AB8500_GPADC, AB8500_GPADC_CTRL3_REG, ctrl23);
45107063bbfSLinus Walleij 		ctrl1 |= AB8500_GPADC_CTRL1_TRIG_ENA;
45207063bbfSLinus Walleij 		if (ch->falling_edge)
45307063bbfSLinus Walleij 			ctrl1 |= AB8500_GPADC_CTRL1_TRIG_EDGE;
45407063bbfSLinus Walleij 	} else {
45507063bbfSLinus Walleij 		ret = abx500_set_register_interruptible(gpadc->dev,
45607063bbfSLinus Walleij 				AB8500_GPADC, AB8500_GPADC_CTRL2_REG, ctrl23);
45707063bbfSLinus Walleij 	}
45807063bbfSLinus Walleij 	if (ret < 0) {
45907063bbfSLinus Walleij 		dev_err(gpadc->dev,
46007063bbfSLinus Walleij 			"gpadc_conversion: set avg samples failed\n");
46107063bbfSLinus Walleij 		goto out;
46207063bbfSLinus Walleij 	}
46307063bbfSLinus Walleij 
46407063bbfSLinus Walleij 	/*
46507063bbfSLinus Walleij 	 * Enable ADC, buffering, select rising edge and enable ADC path
46607063bbfSLinus Walleij 	 * charging current sense if it needed, ABB 3.0 needs some special
46707063bbfSLinus Walleij 	 * treatment too.
46807063bbfSLinus Walleij 	 */
46907063bbfSLinus Walleij 	switch (ch->id) {
47007063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_MAIN_CHARGER_CURRENT:
47107063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_USB_CHARGER_CURRENT:
47207063bbfSLinus Walleij 		ctrl1 |= AB8500_GPADC_CTRL1_BUF_ENA |
47307063bbfSLinus Walleij 			AB8500_GPADC_CTRL1_ICHAR_ENA;
47407063bbfSLinus Walleij 		break;
47507063bbfSLinus Walleij 	case AB8500_GPADC_CHAN_BAT_TEMP:
47607063bbfSLinus Walleij 		if (!is_ab8500_2p0_or_earlier(gpadc->ab8500)) {
47707063bbfSLinus Walleij 			ctrl1 |= AB8500_GPADC_CTRL1_BUF_ENA |
47807063bbfSLinus Walleij 				AB8500_GPADC_CTRL1_BTEMP_PULL_UP;
47907063bbfSLinus Walleij 			/*
48007063bbfSLinus Walleij 			 * Delay might be needed for ABB8500 cut 3.0, if not,
48107063bbfSLinus Walleij 			 * remove when hardware will be available
48207063bbfSLinus Walleij 			 */
48307063bbfSLinus Walleij 			delay_min = 1000; /* Delay in micro seconds */
48407063bbfSLinus Walleij 			delay_max = 10000; /* large range optimises sleepmode */
48507063bbfSLinus Walleij 			break;
48607063bbfSLinus Walleij 		}
487df561f66SGustavo A. R. Silva 		fallthrough;
48807063bbfSLinus Walleij 	default:
48907063bbfSLinus Walleij 		ctrl1 |= AB8500_GPADC_CTRL1_BUF_ENA;
49007063bbfSLinus Walleij 		break;
49107063bbfSLinus Walleij 	}
49207063bbfSLinus Walleij 
49307063bbfSLinus Walleij 	/* Write configuration to control register 1 */
49407063bbfSLinus Walleij 	ret = abx500_set_register_interruptible(gpadc->dev,
49507063bbfSLinus Walleij 		AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ctrl1);
49607063bbfSLinus Walleij 	if (ret < 0) {
49707063bbfSLinus Walleij 		dev_err(gpadc->dev,
49807063bbfSLinus Walleij 			"gpadc_conversion: set Control register failed\n");
49907063bbfSLinus Walleij 		goto out;
50007063bbfSLinus Walleij 	}
50107063bbfSLinus Walleij 
50207063bbfSLinus Walleij 	if (delay_min != 0)
50307063bbfSLinus Walleij 		usleep_range(delay_min, delay_max);
50407063bbfSLinus Walleij 
50507063bbfSLinus Walleij 	if (ch->hardware_control) {
50607063bbfSLinus Walleij 		/* Set trigger delay timer */
50707063bbfSLinus Walleij 		ret = abx500_set_register_interruptible(gpadc->dev,
50807063bbfSLinus Walleij 			AB8500_GPADC, AB8500_GPADC_AUTO_TIMER_REG,
50907063bbfSLinus Walleij 			ch->trig_timer);
51007063bbfSLinus Walleij 		if (ret < 0) {
51107063bbfSLinus Walleij 			dev_err(gpadc->dev,
51207063bbfSLinus Walleij 				"gpadc_conversion: trig timer failed\n");
51307063bbfSLinus Walleij 			goto out;
51407063bbfSLinus Walleij 		}
51507063bbfSLinus Walleij 		completion_timeout = 2 * HZ;
51607063bbfSLinus Walleij 		data_low_addr = AB8500_GPADC_AUTODATAL_REG;
51707063bbfSLinus Walleij 		data_high_addr = AB8500_GPADC_AUTODATAH_REG;
51807063bbfSLinus Walleij 	} else {
51907063bbfSLinus Walleij 		/* Start SW conversion */
52007063bbfSLinus Walleij 		ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
52107063bbfSLinus Walleij 			AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
52207063bbfSLinus Walleij 			AB8500_GPADC_CTRL1_START_SW_CONV,
52307063bbfSLinus Walleij 			AB8500_GPADC_CTRL1_START_SW_CONV);
52407063bbfSLinus Walleij 		if (ret < 0) {
52507063bbfSLinus Walleij 			dev_err(gpadc->dev,
52607063bbfSLinus Walleij 				"gpadc_conversion: start s/w conv failed\n");
52707063bbfSLinus Walleij 			goto out;
52807063bbfSLinus Walleij 		}
52907063bbfSLinus Walleij 		completion_timeout = msecs_to_jiffies(AB8500_GPADC_CONVERSION_TIME);
53007063bbfSLinus Walleij 		data_low_addr = AB8500_GPADC_MANDATAL_REG;
53107063bbfSLinus Walleij 		data_high_addr = AB8500_GPADC_MANDATAH_REG;
53207063bbfSLinus Walleij 	}
53307063bbfSLinus Walleij 
53407063bbfSLinus Walleij 	/* Wait for completion of conversion */
53507063bbfSLinus Walleij 	if (!wait_for_completion_timeout(&gpadc->complete,
53607063bbfSLinus Walleij 			completion_timeout)) {
53707063bbfSLinus Walleij 		dev_err(gpadc->dev,
53807063bbfSLinus Walleij 			"timeout didn't receive GPADC conv interrupt\n");
53907063bbfSLinus Walleij 		ret = -EINVAL;
54007063bbfSLinus Walleij 		goto out;
54107063bbfSLinus Walleij 	}
54207063bbfSLinus Walleij 
54307063bbfSLinus Walleij 	/* Read the converted RAW data */
54407063bbfSLinus Walleij 	ret = abx500_get_register_interruptible(gpadc->dev,
54507063bbfSLinus Walleij 			AB8500_GPADC, data_low_addr, &low_data);
54607063bbfSLinus Walleij 	if (ret < 0) {
54707063bbfSLinus Walleij 		dev_err(gpadc->dev,
54807063bbfSLinus Walleij 			"gpadc_conversion: read low data failed\n");
54907063bbfSLinus Walleij 		goto out;
55007063bbfSLinus Walleij 	}
55107063bbfSLinus Walleij 
55207063bbfSLinus Walleij 	ret = abx500_get_register_interruptible(gpadc->dev,
55307063bbfSLinus Walleij 		AB8500_GPADC, data_high_addr, &high_data);
55407063bbfSLinus Walleij 	if (ret < 0) {
55507063bbfSLinus Walleij 		dev_err(gpadc->dev,
55607063bbfSLinus Walleij 			"gpadc_conversion: read high data failed\n");
55707063bbfSLinus Walleij 		goto out;
55807063bbfSLinus Walleij 	}
55907063bbfSLinus Walleij 
56007063bbfSLinus Walleij 	/* Check if double conversion is required */
56107063bbfSLinus Walleij 	if ((ch->id == AB8500_GPADC_CHAN_BAT_CTRL_AND_IBAT) ||
56207063bbfSLinus Walleij 	    (ch->id == AB8500_GPADC_CHAN_VBAT_MEAS_AND_IBAT) ||
56307063bbfSLinus Walleij 	    (ch->id == AB8500_GPADC_CHAN_VBAT_TRUE_MEAS_AND_IBAT) ||
56407063bbfSLinus Walleij 	    (ch->id == AB8500_GPADC_CHAN_BAT_TEMP_AND_IBAT)) {
56507063bbfSLinus Walleij 
56607063bbfSLinus Walleij 		if (ch->hardware_control) {
56707063bbfSLinus Walleij 			/* not supported */
56807063bbfSLinus Walleij 			ret = -ENOTSUPP;
56907063bbfSLinus Walleij 			dev_err(gpadc->dev,
57007063bbfSLinus Walleij 				"gpadc_conversion: only SW double conversion supported\n");
57107063bbfSLinus Walleij 			goto out;
57207063bbfSLinus Walleij 		} else {
57307063bbfSLinus Walleij 			/* Read the converted RAW data 2 */
57407063bbfSLinus Walleij 			ret = abx500_get_register_interruptible(gpadc->dev,
57507063bbfSLinus Walleij 				AB8500_GPADC, AB8540_GPADC_MANDATA2L_REG,
57607063bbfSLinus Walleij 				&low_data2);
57707063bbfSLinus Walleij 			if (ret < 0) {
57807063bbfSLinus Walleij 				dev_err(gpadc->dev,
57907063bbfSLinus Walleij 					"gpadc_conversion: read sw low data 2 failed\n");
58007063bbfSLinus Walleij 				goto out;
58107063bbfSLinus Walleij 			}
58207063bbfSLinus Walleij 
58307063bbfSLinus Walleij 			ret = abx500_get_register_interruptible(gpadc->dev,
58407063bbfSLinus Walleij 				AB8500_GPADC, AB8540_GPADC_MANDATA2H_REG,
58507063bbfSLinus Walleij 				&high_data2);
58607063bbfSLinus Walleij 			if (ret < 0) {
58707063bbfSLinus Walleij 				dev_err(gpadc->dev,
58807063bbfSLinus Walleij 					"gpadc_conversion: read sw high data 2 failed\n");
58907063bbfSLinus Walleij 				goto out;
59007063bbfSLinus Walleij 			}
59107063bbfSLinus Walleij 			if (ibat != NULL) {
59207063bbfSLinus Walleij 				*ibat = (high_data2 << 8) | low_data2;
59307063bbfSLinus Walleij 			} else {
59407063bbfSLinus Walleij 				dev_warn(gpadc->dev,
59507063bbfSLinus Walleij 					"gpadc_conversion: ibat not stored\n");
59607063bbfSLinus Walleij 			}
59707063bbfSLinus Walleij 
59807063bbfSLinus Walleij 		}
59907063bbfSLinus Walleij 	}
60007063bbfSLinus Walleij 
60107063bbfSLinus Walleij 	/* Disable GPADC */
60207063bbfSLinus Walleij 	ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
60307063bbfSLinus Walleij 		AB8500_GPADC_CTRL1_REG, AB8500_GPADC_CTRL1_DISABLE);
60407063bbfSLinus Walleij 	if (ret < 0) {
60507063bbfSLinus Walleij 		dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
60607063bbfSLinus Walleij 		goto out;
60707063bbfSLinus Walleij 	}
60807063bbfSLinus Walleij 
60907063bbfSLinus Walleij 	/* This eventually drops the regulator */
61007063bbfSLinus Walleij 	pm_runtime_mark_last_busy(gpadc->dev);
61107063bbfSLinus Walleij 	pm_runtime_put_autosuspend(gpadc->dev);
61207063bbfSLinus Walleij 
61307063bbfSLinus Walleij 	return (high_data << 8) | low_data;
61407063bbfSLinus Walleij 
61507063bbfSLinus Walleij out:
61607063bbfSLinus Walleij 	/*
61707063bbfSLinus Walleij 	 * It has shown to be needed to turn off the GPADC if an error occurs,
61807063bbfSLinus Walleij 	 * otherwise we might have problem when waiting for the busy bit in the
61907063bbfSLinus Walleij 	 * GPADC status register to go low. In V1.1 there wait_for_completion
62007063bbfSLinus Walleij 	 * seems to timeout when waiting for an interrupt.. Not seen in V2.0
62107063bbfSLinus Walleij 	 */
62207063bbfSLinus Walleij 	(void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
62307063bbfSLinus Walleij 		AB8500_GPADC_CTRL1_REG, AB8500_GPADC_CTRL1_DISABLE);
62407063bbfSLinus Walleij 	pm_runtime_put(gpadc->dev);
62507063bbfSLinus Walleij 	dev_err(gpadc->dev,
62607063bbfSLinus Walleij 		"gpadc_conversion: Failed to AD convert channel %d\n", ch->id);
62707063bbfSLinus Walleij 
62807063bbfSLinus Walleij 	return ret;
62907063bbfSLinus Walleij }
63007063bbfSLinus Walleij 
63107063bbfSLinus Walleij /**
63207063bbfSLinus Walleij  * ab8500_bm_gpadcconvend_handler() - isr for gpadc conversion completion
63307063bbfSLinus Walleij  * @irq: irq number
63407063bbfSLinus Walleij  * @data: pointer to the data passed during request irq
63507063bbfSLinus Walleij  *
63607063bbfSLinus Walleij  * This is a interrupt service routine for gpadc conversion completion.
63707063bbfSLinus Walleij  * Notifies the gpadc completion is completed and the converted raw value
63807063bbfSLinus Walleij  * can be read from the registers.
63907063bbfSLinus Walleij  * Returns IRQ status(IRQ_HANDLED)
64007063bbfSLinus Walleij  */
ab8500_bm_gpadcconvend_handler(int irq,void * data)64107063bbfSLinus Walleij static irqreturn_t ab8500_bm_gpadcconvend_handler(int irq, void *data)
64207063bbfSLinus Walleij {
64307063bbfSLinus Walleij 	struct ab8500_gpadc *gpadc = data;
64407063bbfSLinus Walleij 
64507063bbfSLinus Walleij 	complete(&gpadc->complete);
64607063bbfSLinus Walleij 
64707063bbfSLinus Walleij 	return IRQ_HANDLED;
64807063bbfSLinus Walleij }
64907063bbfSLinus Walleij 
65007063bbfSLinus Walleij static int otp_cal_regs[] = {
65107063bbfSLinus Walleij 	AB8500_GPADC_CAL_1,
65207063bbfSLinus Walleij 	AB8500_GPADC_CAL_2,
65307063bbfSLinus Walleij 	AB8500_GPADC_CAL_3,
65407063bbfSLinus Walleij 	AB8500_GPADC_CAL_4,
65507063bbfSLinus Walleij 	AB8500_GPADC_CAL_5,
65607063bbfSLinus Walleij 	AB8500_GPADC_CAL_6,
65707063bbfSLinus Walleij 	AB8500_GPADC_CAL_7,
65807063bbfSLinus Walleij };
65907063bbfSLinus Walleij 
66007063bbfSLinus Walleij static int otp4_cal_regs[] = {
66107063bbfSLinus Walleij 	AB8540_GPADC_OTP4_REG_7,
66207063bbfSLinus Walleij 	AB8540_GPADC_OTP4_REG_6,
66307063bbfSLinus Walleij 	AB8540_GPADC_OTP4_REG_5,
66407063bbfSLinus Walleij };
66507063bbfSLinus Walleij 
ab8500_gpadc_read_calibration_data(struct ab8500_gpadc * gpadc)66607063bbfSLinus Walleij static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
66707063bbfSLinus Walleij {
66807063bbfSLinus Walleij 	int i;
66907063bbfSLinus Walleij 	int ret[ARRAY_SIZE(otp_cal_regs)];
67007063bbfSLinus Walleij 	u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];
67107063bbfSLinus Walleij 	int ret_otp4[ARRAY_SIZE(otp4_cal_regs)];
67207063bbfSLinus Walleij 	u8 gpadc_otp4[ARRAY_SIZE(otp4_cal_regs)];
67307063bbfSLinus Walleij 	int vmain_high, vmain_low;
67407063bbfSLinus Walleij 	int btemp_high, btemp_low;
67507063bbfSLinus Walleij 	int vbat_high, vbat_low;
67607063bbfSLinus Walleij 	int ibat_high, ibat_low;
67707063bbfSLinus Walleij 	s64 V_gain, V_offset, V2A_gain, V2A_offset;
67807063bbfSLinus Walleij 
67907063bbfSLinus Walleij 	/* First we read all OTP registers and store the error code */
68007063bbfSLinus Walleij 	for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
68107063bbfSLinus Walleij 		ret[i] = abx500_get_register_interruptible(gpadc->dev,
68207063bbfSLinus Walleij 			AB8500_OTP_EMUL, otp_cal_regs[i],  &gpadc_cal[i]);
68307063bbfSLinus Walleij 		if (ret[i] < 0) {
68407063bbfSLinus Walleij 			/* Continue anyway: maybe the other registers are OK */
68507063bbfSLinus Walleij 			dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
68607063bbfSLinus Walleij 				__func__, otp_cal_regs[i]);
68707063bbfSLinus Walleij 		} else {
68807063bbfSLinus Walleij 			/* Put this in the entropy pool as device-unique */
68907063bbfSLinus Walleij 			add_device_randomness(&ret[i], sizeof(ret[i]));
69007063bbfSLinus Walleij 		}
69107063bbfSLinus Walleij 	}
69207063bbfSLinus Walleij 
69307063bbfSLinus Walleij 	/*
69407063bbfSLinus Walleij 	 * The ADC calibration data is stored in OTP registers.
69507063bbfSLinus Walleij 	 * The layout of the calibration data is outlined below and a more
69607063bbfSLinus Walleij 	 * detailed description can be found in UM0836
69707063bbfSLinus Walleij 	 *
69807063bbfSLinus Walleij 	 * vm_h/l = vmain_high/low
69907063bbfSLinus Walleij 	 * bt_h/l = btemp_high/low
70007063bbfSLinus Walleij 	 * vb_h/l = vbat_high/low
70107063bbfSLinus Walleij 	 *
70207063bbfSLinus Walleij 	 * Data bits 8500/9540:
70307063bbfSLinus Walleij 	 * | 7	   | 6	   | 5	   | 4	   | 3	   | 2	   | 1	   | 0
70407063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
70507063bbfSLinus Walleij 	 * |						   | vm_h9 | vm_h8
70607063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
70707063bbfSLinus Walleij 	 * |		   | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
70807063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
70907063bbfSLinus Walleij 	 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
71007063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
71107063bbfSLinus Walleij 	 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
71207063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
71307063bbfSLinus Walleij 	 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
71407063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
71507063bbfSLinus Walleij 	 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
71607063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
71707063bbfSLinus Walleij 	 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
71807063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
71907063bbfSLinus Walleij 	 *
72007063bbfSLinus Walleij 	 * Data bits 8540:
72107063bbfSLinus Walleij 	 * OTP2
72207063bbfSLinus Walleij 	 * | 7	   | 6	   | 5	   | 4	   | 3	   | 2	   | 1	   | 0
72307063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
72407063bbfSLinus Walleij 	 * |
72507063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
72607063bbfSLinus Walleij 	 * | vm_h9 | vm_h8 | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
72707063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
72807063bbfSLinus Walleij 	 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
72907063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
73007063bbfSLinus Walleij 	 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
73107063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
73207063bbfSLinus Walleij 	 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
73307063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
73407063bbfSLinus Walleij 	 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
73507063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
73607063bbfSLinus Walleij 	 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
73707063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
73807063bbfSLinus Walleij 	 *
73907063bbfSLinus Walleij 	 * Data bits 8540:
74007063bbfSLinus Walleij 	 * OTP4
74107063bbfSLinus Walleij 	 * | 7	   | 6	   | 5	   | 4	   | 3	   | 2	   | 1	   | 0
74207063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
74307063bbfSLinus Walleij 	 * |					   | ib_h9 | ib_h8 | ib_h7
74407063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
74507063bbfSLinus Walleij 	 * | ib_h6 | ib_h5 | ib_h4 | ib_h3 | ib_h2 | ib_h1 | ib_h0 | ib_l5
74607063bbfSLinus Walleij 	 * |.......|.......|.......|.......|.......|.......|.......|.......
74707063bbfSLinus Walleij 	 * | ib_l4 | ib_l3 | ib_l2 | ib_l1 | ib_l0 |
74807063bbfSLinus Walleij 	 *
74907063bbfSLinus Walleij 	 *
75007063bbfSLinus Walleij 	 * Ideal output ADC codes corresponding to injected input voltages
75107063bbfSLinus Walleij 	 * during manufacturing is:
75207063bbfSLinus Walleij 	 *
75307063bbfSLinus Walleij 	 * vmain_high: Vin = 19500mV / ADC ideal code = 997
75407063bbfSLinus Walleij 	 * vmain_low:  Vin = 315mV   / ADC ideal code = 16
75507063bbfSLinus Walleij 	 * btemp_high: Vin = 1300mV  / ADC ideal code = 985
75607063bbfSLinus Walleij 	 * btemp_low:  Vin = 21mV    / ADC ideal code = 16
75707063bbfSLinus Walleij 	 * vbat_high:  Vin = 4700mV  / ADC ideal code = 982
75807063bbfSLinus Walleij 	 * vbat_low:   Vin = 2380mV  / ADC ideal code = 33
75907063bbfSLinus Walleij 	 */
76007063bbfSLinus Walleij 
76107063bbfSLinus Walleij 	if (is_ab8540(gpadc->ab8500)) {
76207063bbfSLinus Walleij 		/* Calculate gain and offset for VMAIN if all reads succeeded*/
76307063bbfSLinus Walleij 		if (!(ret[1] < 0 || ret[2] < 0)) {
76407063bbfSLinus Walleij 			vmain_high = (((gpadc_cal[1] & 0xFF) << 2) |
76507063bbfSLinus Walleij 				((gpadc_cal[2] & 0xC0) >> 6));
76607063bbfSLinus Walleij 			vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
76707063bbfSLinus Walleij 
76807063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].otp_calib_hi =
76907063bbfSLinus Walleij 				(u16)vmain_high;
77007063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].otp_calib_lo =
77107063bbfSLinus Walleij 				(u16)vmain_low;
77207063bbfSLinus Walleij 
77307063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].gain = AB8500_GPADC_CALIB_SCALE *
77407063bbfSLinus Walleij 				(19500 - 315) / (vmain_high - vmain_low);
77507063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].offset = AB8500_GPADC_CALIB_SCALE *
77607063bbfSLinus Walleij 				19500 - (AB8500_GPADC_CALIB_SCALE * (19500 - 315) /
77707063bbfSLinus Walleij 				(vmain_high - vmain_low)) * vmain_high;
77807063bbfSLinus Walleij 		} else {
77907063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].gain = 0;
78007063bbfSLinus Walleij 		}
78107063bbfSLinus Walleij 
78207063bbfSLinus Walleij 		/* Read IBAT calibration Data */
78307063bbfSLinus Walleij 		for (i = 0; i < ARRAY_SIZE(otp4_cal_regs); i++) {
78407063bbfSLinus Walleij 			ret_otp4[i] = abx500_get_register_interruptible(
78507063bbfSLinus Walleij 					gpadc->dev, AB8500_OTP_EMUL,
78607063bbfSLinus Walleij 					otp4_cal_regs[i],  &gpadc_otp4[i]);
78707063bbfSLinus Walleij 			if (ret_otp4[i] < 0)
78807063bbfSLinus Walleij 				dev_err(gpadc->dev,
78907063bbfSLinus Walleij 					"%s: read otp4 reg 0x%02x failed\n",
79007063bbfSLinus Walleij 					__func__, otp4_cal_regs[i]);
79107063bbfSLinus Walleij 		}
79207063bbfSLinus Walleij 
79307063bbfSLinus Walleij 		/* Calculate gain and offset for IBAT if all reads succeeded */
79407063bbfSLinus Walleij 		if (!(ret_otp4[0] < 0 || ret_otp4[1] < 0 || ret_otp4[2] < 0)) {
79507063bbfSLinus Walleij 			ibat_high = (((gpadc_otp4[0] & 0x07) << 7) |
79607063bbfSLinus Walleij 				((gpadc_otp4[1] & 0xFE) >> 1));
79707063bbfSLinus Walleij 			ibat_low = (((gpadc_otp4[1] & 0x01) << 5) |
79807063bbfSLinus Walleij 				((gpadc_otp4[2] & 0xF8) >> 3));
79907063bbfSLinus Walleij 
80007063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_IBAT].otp_calib_hi =
80107063bbfSLinus Walleij 				(u16)ibat_high;
80207063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_IBAT].otp_calib_lo =
80307063bbfSLinus Walleij 				(u16)ibat_low;
80407063bbfSLinus Walleij 
80507063bbfSLinus Walleij 			V_gain = ((AB8500_GPADC_IBAT_VDROP_H - AB8500_GPADC_IBAT_VDROP_L)
80607063bbfSLinus Walleij 				<< AB8500_GPADC_CALIB_SHIFT_IBAT) / (ibat_high - ibat_low);
80707063bbfSLinus Walleij 
80807063bbfSLinus Walleij 			V_offset = (AB8500_GPADC_IBAT_VDROP_H << AB8500_GPADC_CALIB_SHIFT_IBAT) -
80907063bbfSLinus Walleij 				(((AB8500_GPADC_IBAT_VDROP_H - AB8500_GPADC_IBAT_VDROP_L) <<
81007063bbfSLinus Walleij 				AB8500_GPADC_CALIB_SHIFT_IBAT) / (ibat_high - ibat_low))
81107063bbfSLinus Walleij 				* ibat_high;
81207063bbfSLinus Walleij 			/*
81307063bbfSLinus Walleij 			 * Result obtained is in mV (at a scale factor),
81407063bbfSLinus Walleij 			 * we need to calculate gain and offset to get mA
81507063bbfSLinus Walleij 			 */
81607063bbfSLinus Walleij 			V2A_gain = (AB8500_ADC_CH_IBAT_MAX - AB8500_ADC_CH_IBAT_MIN)/
81707063bbfSLinus Walleij 				(AB8500_ADC_CH_IBAT_MAX_V - AB8500_ADC_CH_IBAT_MIN_V);
81807063bbfSLinus Walleij 			V2A_offset = ((AB8500_ADC_CH_IBAT_MAX_V * AB8500_ADC_CH_IBAT_MIN -
81907063bbfSLinus Walleij 				AB8500_ADC_CH_IBAT_MAX * AB8500_ADC_CH_IBAT_MIN_V)
82007063bbfSLinus Walleij 				<< AB8500_GPADC_CALIB_SHIFT_IBAT)
82107063bbfSLinus Walleij 				/ (AB8500_ADC_CH_IBAT_MAX_V - AB8500_ADC_CH_IBAT_MIN_V);
82207063bbfSLinus Walleij 
82307063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_IBAT].gain =
82407063bbfSLinus Walleij 				V_gain * V2A_gain;
82507063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_IBAT].offset =
82607063bbfSLinus Walleij 				V_offset * V2A_gain + V2A_offset;
82707063bbfSLinus Walleij 		} else {
82807063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_IBAT].gain = 0;
82907063bbfSLinus Walleij 		}
83007063bbfSLinus Walleij 	} else {
83107063bbfSLinus Walleij 		/* Calculate gain and offset for VMAIN if all reads succeeded */
83207063bbfSLinus Walleij 		if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
83307063bbfSLinus Walleij 			vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
83407063bbfSLinus Walleij 				((gpadc_cal[1] & 0x3F) << 2) |
83507063bbfSLinus Walleij 				((gpadc_cal[2] & 0xC0) >> 6));
83607063bbfSLinus Walleij 			vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
83707063bbfSLinus Walleij 
83807063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].otp_calib_hi =
83907063bbfSLinus Walleij 				(u16)vmain_high;
84007063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].otp_calib_lo =
84107063bbfSLinus Walleij 				(u16)vmain_low;
84207063bbfSLinus Walleij 
84307063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].gain = AB8500_GPADC_CALIB_SCALE *
84407063bbfSLinus Walleij 				(19500 - 315) / (vmain_high - vmain_low);
84507063bbfSLinus Walleij 
84607063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].offset = AB8500_GPADC_CALIB_SCALE *
84707063bbfSLinus Walleij 				19500 - (AB8500_GPADC_CALIB_SCALE * (19500 - 315) /
84807063bbfSLinus Walleij 				(vmain_high - vmain_low)) * vmain_high;
84907063bbfSLinus Walleij 		} else {
85007063bbfSLinus Walleij 			gpadc->cal_data[AB8500_CAL_VMAIN].gain = 0;
85107063bbfSLinus Walleij 		}
85207063bbfSLinus Walleij 	}
85307063bbfSLinus Walleij 
85407063bbfSLinus Walleij 	/* Calculate gain and offset for BTEMP if all reads succeeded */
85507063bbfSLinus Walleij 	if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
85607063bbfSLinus Walleij 		btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
85707063bbfSLinus Walleij 			(gpadc_cal[3] << 1) | ((gpadc_cal[4] & 0x80) >> 7));
85807063bbfSLinus Walleij 		btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
85907063bbfSLinus Walleij 
86007063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_BTEMP].otp_calib_hi = (u16)btemp_high;
86107063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_BTEMP].otp_calib_lo = (u16)btemp_low;
86207063bbfSLinus Walleij 
86307063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_BTEMP].gain =
86407063bbfSLinus Walleij 			AB8500_GPADC_CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
86507063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_BTEMP].offset = AB8500_GPADC_CALIB_SCALE * 1300 -
86607063bbfSLinus Walleij 			(AB8500_GPADC_CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low))
86707063bbfSLinus Walleij 			* btemp_high;
86807063bbfSLinus Walleij 	} else {
86907063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_BTEMP].gain = 0;
87007063bbfSLinus Walleij 	}
87107063bbfSLinus Walleij 
87207063bbfSLinus Walleij 	/* Calculate gain and offset for VBAT if all reads succeeded */
87307063bbfSLinus Walleij 	if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
87407063bbfSLinus Walleij 		vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
87507063bbfSLinus Walleij 		vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
87607063bbfSLinus Walleij 
87707063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_VBAT].otp_calib_hi = (u16)vbat_high;
87807063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_VBAT].otp_calib_lo = (u16)vbat_low;
87907063bbfSLinus Walleij 
88007063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_VBAT].gain = AB8500_GPADC_CALIB_SCALE *
88107063bbfSLinus Walleij 			(4700 - 2380) /	(vbat_high - vbat_low);
88207063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_VBAT].offset = AB8500_GPADC_CALIB_SCALE * 4700 -
88307063bbfSLinus Walleij 			(AB8500_GPADC_CALIB_SCALE * (4700 - 2380) /
88407063bbfSLinus Walleij 			(vbat_high - vbat_low)) * vbat_high;
88507063bbfSLinus Walleij 	} else {
88607063bbfSLinus Walleij 		gpadc->cal_data[AB8500_CAL_VBAT].gain = 0;
88707063bbfSLinus Walleij 	}
88807063bbfSLinus Walleij }
88907063bbfSLinus Walleij 
ab8500_gpadc_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)89007063bbfSLinus Walleij static int ab8500_gpadc_read_raw(struct iio_dev *indio_dev,
89107063bbfSLinus Walleij 				 struct iio_chan_spec const *chan,
89207063bbfSLinus Walleij 				 int *val, int *val2, long mask)
89307063bbfSLinus Walleij {
89407063bbfSLinus Walleij 	struct ab8500_gpadc *gpadc = iio_priv(indio_dev);
89507063bbfSLinus Walleij 	const struct ab8500_gpadc_chan_info *ch;
89607063bbfSLinus Walleij 	int raw_val;
89707063bbfSLinus Walleij 	int processed;
89807063bbfSLinus Walleij 
89907063bbfSLinus Walleij 	ch = ab8500_gpadc_get_channel(gpadc, chan->address);
90007063bbfSLinus Walleij 	if (!ch) {
90107063bbfSLinus Walleij 		dev_err(gpadc->dev, "no such channel %lu\n",
90207063bbfSLinus Walleij 			chan->address);
90307063bbfSLinus Walleij 		return -EINVAL;
90407063bbfSLinus Walleij 	}
90507063bbfSLinus Walleij 
90607063bbfSLinus Walleij 	raw_val = ab8500_gpadc_read(gpadc, ch, NULL);
90707063bbfSLinus Walleij 	if (raw_val < 0)
90807063bbfSLinus Walleij 		return raw_val;
90907063bbfSLinus Walleij 
91007063bbfSLinus Walleij 	if (mask == IIO_CHAN_INFO_RAW) {
91107063bbfSLinus Walleij 		*val = raw_val;
91207063bbfSLinus Walleij 		return IIO_VAL_INT;
91307063bbfSLinus Walleij 	}
91407063bbfSLinus Walleij 
91507063bbfSLinus Walleij 	if (mask == IIO_CHAN_INFO_PROCESSED) {
91607063bbfSLinus Walleij 		processed = ab8500_gpadc_ad_to_voltage(gpadc, ch->id, raw_val);
91707063bbfSLinus Walleij 		if (processed < 0)
91807063bbfSLinus Walleij 			return processed;
91907063bbfSLinus Walleij 
92007063bbfSLinus Walleij 		/* Return millivolt or milliamps or millicentigrades */
9214f543408SLinus Walleij 		*val = processed;
92207063bbfSLinus Walleij 		return IIO_VAL_INT;
92307063bbfSLinus Walleij 	}
92407063bbfSLinus Walleij 
92507063bbfSLinus Walleij 	return -EINVAL;
92607063bbfSLinus Walleij }
92707063bbfSLinus Walleij 
ab8500_gpadc_fwnode_xlate(struct iio_dev * indio_dev,const struct fwnode_reference_args * iiospec)928dec7e2c8SNuno Sá static int ab8500_gpadc_fwnode_xlate(struct iio_dev *indio_dev,
929dec7e2c8SNuno Sá 				     const struct fwnode_reference_args *iiospec)
93007063bbfSLinus Walleij {
93107063bbfSLinus Walleij 	int i;
93207063bbfSLinus Walleij 
93307063bbfSLinus Walleij 	for (i = 0; i < indio_dev->num_channels; i++)
93407063bbfSLinus Walleij 		if (indio_dev->channels[i].channel == iiospec->args[0])
93507063bbfSLinus Walleij 			return i;
93607063bbfSLinus Walleij 
93707063bbfSLinus Walleij 	return -EINVAL;
93807063bbfSLinus Walleij }
93907063bbfSLinus Walleij 
94007063bbfSLinus Walleij static const struct iio_info ab8500_gpadc_info = {
941dec7e2c8SNuno Sá 	.fwnode_xlate = ab8500_gpadc_fwnode_xlate,
94207063bbfSLinus Walleij 	.read_raw = ab8500_gpadc_read_raw,
94307063bbfSLinus Walleij };
94407063bbfSLinus Walleij 
ab8500_gpadc_runtime_suspend(struct device * dev)94507063bbfSLinus Walleij static int ab8500_gpadc_runtime_suspend(struct device *dev)
94607063bbfSLinus Walleij {
94707063bbfSLinus Walleij 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
94807063bbfSLinus Walleij 	struct ab8500_gpadc *gpadc = iio_priv(indio_dev);
94907063bbfSLinus Walleij 
95007063bbfSLinus Walleij 	regulator_disable(gpadc->vddadc);
95107063bbfSLinus Walleij 
95207063bbfSLinus Walleij 	return 0;
95307063bbfSLinus Walleij }
95407063bbfSLinus Walleij 
ab8500_gpadc_runtime_resume(struct device * dev)95507063bbfSLinus Walleij static int ab8500_gpadc_runtime_resume(struct device *dev)
95607063bbfSLinus Walleij {
95707063bbfSLinus Walleij 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
95807063bbfSLinus Walleij 	struct ab8500_gpadc *gpadc = iio_priv(indio_dev);
95907063bbfSLinus Walleij 	int ret;
96007063bbfSLinus Walleij 
96107063bbfSLinus Walleij 	ret = regulator_enable(gpadc->vddadc);
96207063bbfSLinus Walleij 	if (ret)
96307063bbfSLinus Walleij 		dev_err(dev, "Failed to enable vddadc: %d\n", ret);
96407063bbfSLinus Walleij 
96507063bbfSLinus Walleij 	return ret;
96607063bbfSLinus Walleij }
96707063bbfSLinus Walleij 
96807063bbfSLinus Walleij /**
96907063bbfSLinus Walleij  * ab8500_gpadc_parse_channel() - process devicetree channel configuration
97007063bbfSLinus Walleij  * @dev: pointer to containing device
971dec7e2c8SNuno Sá  * @fwnode: fw node for the channel to configure
97207063bbfSLinus Walleij  * @ch: channel info to fill in
97307063bbfSLinus Walleij  * @iio_chan: IIO channel specification to fill in
97407063bbfSLinus Walleij  *
97507063bbfSLinus Walleij  * The devicetree will set up the channel for use with the specific device,
97607063bbfSLinus Walleij  * and define usage for things like AUX GPADC inputs more precisely.
97707063bbfSLinus Walleij  */
ab8500_gpadc_parse_channel(struct device * dev,struct fwnode_handle * fwnode,struct ab8500_gpadc_chan_info * ch,struct iio_chan_spec * iio_chan)97807063bbfSLinus Walleij static int ab8500_gpadc_parse_channel(struct device *dev,
979dec7e2c8SNuno Sá 				      struct fwnode_handle *fwnode,
98007063bbfSLinus Walleij 				      struct ab8500_gpadc_chan_info *ch,
98107063bbfSLinus Walleij 				      struct iio_chan_spec *iio_chan)
98207063bbfSLinus Walleij {
983dec7e2c8SNuno Sá 	const char *name = fwnode_get_name(fwnode);
98407063bbfSLinus Walleij 	u32 chan;
98507063bbfSLinus Walleij 	int ret;
98607063bbfSLinus Walleij 
987dec7e2c8SNuno Sá 	ret = fwnode_property_read_u32(fwnode, "reg", &chan);
98807063bbfSLinus Walleij 	if (ret) {
98907063bbfSLinus Walleij 		dev_err(dev, "invalid channel number %s\n", name);
99007063bbfSLinus Walleij 		return ret;
99107063bbfSLinus Walleij 	}
99207063bbfSLinus Walleij 	if (chan > AB8500_GPADC_CHAN_BAT_TEMP_AND_IBAT) {
99307063bbfSLinus Walleij 		dev_err(dev, "%s channel number out of range %d\n", name, chan);
99407063bbfSLinus Walleij 		return -EINVAL;
99507063bbfSLinus Walleij 	}
99607063bbfSLinus Walleij 
99707063bbfSLinus Walleij 	iio_chan->channel = chan;
99807063bbfSLinus Walleij 	iio_chan->datasheet_name = name;
99907063bbfSLinus Walleij 	iio_chan->indexed = 1;
100007063bbfSLinus Walleij 	iio_chan->address = chan;
100107063bbfSLinus Walleij 	iio_chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
100207063bbfSLinus Walleij 		BIT(IIO_CHAN_INFO_PROCESSED);
100307063bbfSLinus Walleij 	/* Most are voltages (also temperatures), some are currents */
100407063bbfSLinus Walleij 	if ((chan == AB8500_GPADC_CHAN_MAIN_CHARGER_CURRENT) ||
100507063bbfSLinus Walleij 	    (chan == AB8500_GPADC_CHAN_USB_CHARGER_CURRENT))
100607063bbfSLinus Walleij 		iio_chan->type = IIO_CURRENT;
100707063bbfSLinus Walleij 	else
100807063bbfSLinus Walleij 		iio_chan->type = IIO_VOLTAGE;
100907063bbfSLinus Walleij 
101007063bbfSLinus Walleij 	ch->id = chan;
101107063bbfSLinus Walleij 
101207063bbfSLinus Walleij 	/* Sensible defaults */
101307063bbfSLinus Walleij 	ch->avg_sample = 16;
101407063bbfSLinus Walleij 	ch->hardware_control = false;
101507063bbfSLinus Walleij 	ch->falling_edge = false;
101607063bbfSLinus Walleij 	ch->trig_timer = 0;
101707063bbfSLinus Walleij 
101807063bbfSLinus Walleij 	return 0;
101907063bbfSLinus Walleij }
102007063bbfSLinus Walleij 
102107063bbfSLinus Walleij /**
102207063bbfSLinus Walleij  * ab8500_gpadc_parse_channels() - Parse the GPADC channels from DT
102307063bbfSLinus Walleij  * @gpadc: the GPADC to configure the channels for
102407063bbfSLinus Walleij  * @chans: the IIO channels we parsed
102507063bbfSLinus Walleij  * @nchans: the number of IIO channels we parsed
102607063bbfSLinus Walleij  */
ab8500_gpadc_parse_channels(struct ab8500_gpadc * gpadc,struct iio_chan_spec ** chans_parsed,unsigned int * nchans_parsed)102707063bbfSLinus Walleij static int ab8500_gpadc_parse_channels(struct ab8500_gpadc *gpadc,
102807063bbfSLinus Walleij 				       struct iio_chan_spec **chans_parsed,
102907063bbfSLinus Walleij 				       unsigned int *nchans_parsed)
103007063bbfSLinus Walleij {
1031dec7e2c8SNuno Sá 	struct fwnode_handle *child;
103207063bbfSLinus Walleij 	struct ab8500_gpadc_chan_info *ch;
103307063bbfSLinus Walleij 	struct iio_chan_spec *iio_chans;
103407063bbfSLinus Walleij 	unsigned int nchans;
103507063bbfSLinus Walleij 	int i;
103607063bbfSLinus Walleij 
1037dec7e2c8SNuno Sá 	nchans = device_get_child_node_count(gpadc->dev);
103807063bbfSLinus Walleij 	if (!nchans) {
103907063bbfSLinus Walleij 		dev_err(gpadc->dev, "no channel children\n");
104007063bbfSLinus Walleij 		return -ENODEV;
104107063bbfSLinus Walleij 	}
104207063bbfSLinus Walleij 	dev_info(gpadc->dev, "found %d ADC channels\n", nchans);
104307063bbfSLinus Walleij 
104407063bbfSLinus Walleij 	iio_chans = devm_kcalloc(gpadc->dev, nchans,
104507063bbfSLinus Walleij 				 sizeof(*iio_chans), GFP_KERNEL);
104607063bbfSLinus Walleij 	if (!iio_chans)
104707063bbfSLinus Walleij 		return -ENOMEM;
104807063bbfSLinus Walleij 
104907063bbfSLinus Walleij 	gpadc->chans = devm_kcalloc(gpadc->dev, nchans,
105007063bbfSLinus Walleij 				    sizeof(*gpadc->chans), GFP_KERNEL);
105107063bbfSLinus Walleij 	if (!gpadc->chans)
105207063bbfSLinus Walleij 		return -ENOMEM;
105307063bbfSLinus Walleij 
105407063bbfSLinus Walleij 	i = 0;
1055dec7e2c8SNuno Sá 	device_for_each_child_node(gpadc->dev, child) {
105607063bbfSLinus Walleij 		struct iio_chan_spec *iio_chan;
105707063bbfSLinus Walleij 		int ret;
105807063bbfSLinus Walleij 
105907063bbfSLinus Walleij 		ch = &gpadc->chans[i];
106007063bbfSLinus Walleij 		iio_chan = &iio_chans[i];
106107063bbfSLinus Walleij 
106207063bbfSLinus Walleij 		ret = ab8500_gpadc_parse_channel(gpadc->dev, child, ch,
106307063bbfSLinus Walleij 						 iio_chan);
106407063bbfSLinus Walleij 		if (ret) {
1065dec7e2c8SNuno Sá 			fwnode_handle_put(child);
106607063bbfSLinus Walleij 			return ret;
106707063bbfSLinus Walleij 		}
106807063bbfSLinus Walleij 		i++;
106907063bbfSLinus Walleij 	}
107007063bbfSLinus Walleij 	gpadc->nchans = nchans;
107107063bbfSLinus Walleij 	*chans_parsed = iio_chans;
107207063bbfSLinus Walleij 	*nchans_parsed = nchans;
107307063bbfSLinus Walleij 
107407063bbfSLinus Walleij 	return 0;
107507063bbfSLinus Walleij }
107607063bbfSLinus Walleij 
ab8500_gpadc_probe(struct platform_device * pdev)107707063bbfSLinus Walleij static int ab8500_gpadc_probe(struct platform_device *pdev)
107807063bbfSLinus Walleij {
107907063bbfSLinus Walleij 	struct ab8500_gpadc *gpadc;
108007063bbfSLinus Walleij 	struct iio_dev *indio_dev;
108107063bbfSLinus Walleij 	struct device *dev = &pdev->dev;
108207063bbfSLinus Walleij 	struct iio_chan_spec *iio_chans;
108307063bbfSLinus Walleij 	unsigned int n_iio_chans;
108407063bbfSLinus Walleij 	int ret;
108507063bbfSLinus Walleij 
108607063bbfSLinus Walleij 	indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
108707063bbfSLinus Walleij 	if (!indio_dev)
108807063bbfSLinus Walleij 		return -ENOMEM;
108907063bbfSLinus Walleij 
109007063bbfSLinus Walleij 	platform_set_drvdata(pdev, indio_dev);
109107063bbfSLinus Walleij 	gpadc = iio_priv(indio_dev);
109207063bbfSLinus Walleij 
109307063bbfSLinus Walleij 	gpadc->dev = dev;
109407063bbfSLinus Walleij 	gpadc->ab8500 = dev_get_drvdata(dev->parent);
109507063bbfSLinus Walleij 
1096dec7e2c8SNuno Sá 	ret = ab8500_gpadc_parse_channels(gpadc, &iio_chans, &n_iio_chans);
109707063bbfSLinus Walleij 	if (ret)
109807063bbfSLinus Walleij 		return ret;
109907063bbfSLinus Walleij 
110007063bbfSLinus Walleij 	gpadc->irq_sw = platform_get_irq_byname(pdev, "SW_CONV_END");
110108e9734aSCai Huoqing 	if (gpadc->irq_sw < 0)
1102*089c1e11SRuan Jinjie 		return gpadc->irq_sw;
110307063bbfSLinus Walleij 
1104cef49e5eSLinus Walleij 	if (is_ab8500(gpadc->ab8500)) {
110507063bbfSLinus Walleij 		gpadc->irq_hw = platform_get_irq_byname(pdev, "HW_CONV_END");
110608e9734aSCai Huoqing 		if (gpadc->irq_hw < 0)
1107*089c1e11SRuan Jinjie 			return gpadc->irq_hw;
1108cef49e5eSLinus Walleij 	} else {
1109cef49e5eSLinus Walleij 		gpadc->irq_hw = 0;
1110cef49e5eSLinus Walleij 	}
111107063bbfSLinus Walleij 
111207063bbfSLinus Walleij 	/* Initialize completion used to notify completion of conversion */
111307063bbfSLinus Walleij 	init_completion(&gpadc->complete);
111407063bbfSLinus Walleij 
111507063bbfSLinus Walleij 	/* Request interrupts */
111607063bbfSLinus Walleij 	ret = devm_request_threaded_irq(dev, gpadc->irq_sw, NULL,
111707063bbfSLinus Walleij 		ab8500_bm_gpadcconvend_handler,	IRQF_NO_SUSPEND | IRQF_ONESHOT,
111807063bbfSLinus Walleij 		"ab8500-gpadc-sw", gpadc);
111907063bbfSLinus Walleij 	if (ret < 0) {
112007063bbfSLinus Walleij 		dev_err(dev,
112107063bbfSLinus Walleij 			"failed to request sw conversion irq %d\n",
112207063bbfSLinus Walleij 			gpadc->irq_sw);
112307063bbfSLinus Walleij 		return ret;
112407063bbfSLinus Walleij 	}
112507063bbfSLinus Walleij 
1126cef49e5eSLinus Walleij 	if (gpadc->irq_hw) {
112707063bbfSLinus Walleij 		ret = devm_request_threaded_irq(dev, gpadc->irq_hw, NULL,
112807063bbfSLinus Walleij 			ab8500_bm_gpadcconvend_handler,	IRQF_NO_SUSPEND | IRQF_ONESHOT,
112907063bbfSLinus Walleij 			"ab8500-gpadc-hw", gpadc);
113007063bbfSLinus Walleij 		if (ret < 0) {
113107063bbfSLinus Walleij 			dev_err(dev,
113207063bbfSLinus Walleij 				"Failed to request hw conversion irq: %d\n",
113307063bbfSLinus Walleij 				gpadc->irq_hw);
113407063bbfSLinus Walleij 			return ret;
113507063bbfSLinus Walleij 		}
1136cef49e5eSLinus Walleij 	}
113707063bbfSLinus Walleij 
113807063bbfSLinus Walleij 	/* The VTVout LDO used to power the AB8500 GPADC */
113907063bbfSLinus Walleij 	gpadc->vddadc = devm_regulator_get(dev, "vddadc");
114008e9734aSCai Huoqing 	if (IS_ERR(gpadc->vddadc))
114108e9734aSCai Huoqing 		return dev_err_probe(dev, PTR_ERR(gpadc->vddadc),
114208e9734aSCai Huoqing 				     "failed to get vddadc\n");
114307063bbfSLinus Walleij 
114407063bbfSLinus Walleij 	ret = regulator_enable(gpadc->vddadc);
114507063bbfSLinus Walleij 	if (ret) {
114607063bbfSLinus Walleij 		dev_err(dev, "failed to enable vddadc: %d\n", ret);
114707063bbfSLinus Walleij 		return ret;
114807063bbfSLinus Walleij 	}
114907063bbfSLinus Walleij 
115007063bbfSLinus Walleij 	/* Enable runtime PM */
115107063bbfSLinus Walleij 	pm_runtime_get_noresume(dev);
115207063bbfSLinus Walleij 	pm_runtime_set_active(dev);
115307063bbfSLinus Walleij 	pm_runtime_enable(dev);
115407063bbfSLinus Walleij 	pm_runtime_set_autosuspend_delay(dev, AB8500_GPADC_AUTOSUSPEND_DELAY);
115507063bbfSLinus Walleij 	pm_runtime_use_autosuspend(dev);
115607063bbfSLinus Walleij 
115707063bbfSLinus Walleij 	ab8500_gpadc_read_calibration_data(gpadc);
115807063bbfSLinus Walleij 
115907063bbfSLinus Walleij 	pm_runtime_put(dev);
116007063bbfSLinus Walleij 
116107063bbfSLinus Walleij 	indio_dev->name = "ab8500-gpadc";
116207063bbfSLinus Walleij 	indio_dev->modes = INDIO_DIRECT_MODE;
116307063bbfSLinus Walleij 	indio_dev->info = &ab8500_gpadc_info;
116407063bbfSLinus Walleij 	indio_dev->channels = iio_chans;
116507063bbfSLinus Walleij 	indio_dev->num_channels = n_iio_chans;
116607063bbfSLinus Walleij 
116707063bbfSLinus Walleij 	ret = devm_iio_device_register(dev, indio_dev);
116807063bbfSLinus Walleij 	if (ret)
116907063bbfSLinus Walleij 		goto out_dis_pm;
117007063bbfSLinus Walleij 
117107063bbfSLinus Walleij 	return 0;
117207063bbfSLinus Walleij 
117307063bbfSLinus Walleij out_dis_pm:
117407063bbfSLinus Walleij 	pm_runtime_get_sync(dev);
117507063bbfSLinus Walleij 	pm_runtime_put_noidle(dev);
117607063bbfSLinus Walleij 	pm_runtime_disable(dev);
117707063bbfSLinus Walleij 	regulator_disable(gpadc->vddadc);
117807063bbfSLinus Walleij 
117907063bbfSLinus Walleij 	return ret;
118007063bbfSLinus Walleij }
118107063bbfSLinus Walleij 
ab8500_gpadc_remove(struct platform_device * pdev)118207063bbfSLinus Walleij static int ab8500_gpadc_remove(struct platform_device *pdev)
118307063bbfSLinus Walleij {
118407063bbfSLinus Walleij 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
118507063bbfSLinus Walleij 	struct ab8500_gpadc *gpadc = iio_priv(indio_dev);
118607063bbfSLinus Walleij 
118707063bbfSLinus Walleij 	pm_runtime_get_sync(gpadc->dev);
118807063bbfSLinus Walleij 	pm_runtime_put_noidle(gpadc->dev);
118907063bbfSLinus Walleij 	pm_runtime_disable(gpadc->dev);
119007063bbfSLinus Walleij 	regulator_disable(gpadc->vddadc);
119107063bbfSLinus Walleij 
119207063bbfSLinus Walleij 	return 0;
119307063bbfSLinus Walleij }
119407063bbfSLinus Walleij 
1195c62433f9SJonathan Cameron static DEFINE_RUNTIME_DEV_PM_OPS(ab8500_gpadc_pm_ops,
1196c62433f9SJonathan Cameron 				 ab8500_gpadc_runtime_suspend,
1197c62433f9SJonathan Cameron 				 ab8500_gpadc_runtime_resume, NULL);
119807063bbfSLinus Walleij 
119907063bbfSLinus Walleij static struct platform_driver ab8500_gpadc_driver = {
120007063bbfSLinus Walleij 	.probe = ab8500_gpadc_probe,
120107063bbfSLinus Walleij 	.remove = ab8500_gpadc_remove,
120207063bbfSLinus Walleij 	.driver = {
120307063bbfSLinus Walleij 		.name = "ab8500-gpadc",
1204c62433f9SJonathan Cameron 		.pm = pm_ptr(&ab8500_gpadc_pm_ops),
120507063bbfSLinus Walleij 	},
120607063bbfSLinus Walleij };
120707063bbfSLinus Walleij builtin_platform_driver(ab8500_gpadc_driver);
1208