xref: /OK3568_Linux_fs/kernel/drivers/iio/adc/qcom-pm8xxx-xoadc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Qualcomm PM8xxx PMIC XOADC driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * These ADCs are known as HK/XO (house keeping / chrystal oscillator)
6*4882a593Smuzhiyun  * "XO" in "XOADC" means Chrystal Oscillator. It's a bunch of
7*4882a593Smuzhiyun  * specific-purpose and general purpose ADC converters and channels.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (C) 2017 Linaro Ltd.
10*4882a593Smuzhiyun  * Author: Linus Walleij <linus.walleij@linaro.org>
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/iio/iio.h>
14*4882a593Smuzhiyun #include <linux/iio/sysfs.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/of.h>
17*4882a593Smuzhiyun #include <linux/of_device.h>
18*4882a593Smuzhiyun #include <linux/platform_device.h>
19*4882a593Smuzhiyun #include <linux/regmap.h>
20*4882a593Smuzhiyun #include <linux/init.h>
21*4882a593Smuzhiyun #include <linux/interrupt.h>
22*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include "qcom-vadc-common.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * Definitions for the "user processor" registers lifted from the v3.4
28*4882a593Smuzhiyun  * Qualcomm tree. Their kernel has two out-of-tree drivers for the ADC:
29*4882a593Smuzhiyun  * drivers/misc/pmic8058-xoadc.c
30*4882a593Smuzhiyun  * drivers/hwmon/pm8xxx-adc.c
31*4882a593Smuzhiyun  * None of them contain any complete register specification, so this is
32*4882a593Smuzhiyun  * a best effort of combining the information.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* These appear to be "battery monitor" registers */
36*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1			0x17e
37*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1_EN_BTM		BIT(0)
38*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1_SEL_OP_MODE		BIT(1)
39*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL1	BIT(2)
40*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL2	BIT(3)
41*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL3	BIT(4)
42*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL4	BIT(5)
43*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1_EOC			BIT(6)
44*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL1_REQ			BIT(7)
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define ADC_ARB_BTM_AMUX_CNTRL			0x17f
47*4882a593Smuzhiyun #define ADC_ARB_BTM_ANA_PARAM			0x180
48*4882a593Smuzhiyun #define ADC_ARB_BTM_DIG_PARAM			0x181
49*4882a593Smuzhiyun #define ADC_ARB_BTM_RSV				0x182
50*4882a593Smuzhiyun #define ADC_ARB_BTM_DATA1			0x183
51*4882a593Smuzhiyun #define ADC_ARB_BTM_DATA0			0x184
52*4882a593Smuzhiyun #define ADC_ARB_BTM_BAT_COOL_THR1		0x185
53*4882a593Smuzhiyun #define ADC_ARB_BTM_BAT_COOL_THR0		0x186
54*4882a593Smuzhiyun #define ADC_ARB_BTM_BAT_WARM_THR1		0x187
55*4882a593Smuzhiyun #define ADC_ARB_BTM_BAT_WARM_THR0		0x188
56*4882a593Smuzhiyun #define ADC_ARB_BTM_CNTRL2			0x18c
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* Proper ADC registers */
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL			0x197
61*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL_EN_ARB		BIT(0)
62*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL_RSV1			BIT(1)
63*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL_RSV2			BIT(2)
64*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL_RSV3			BIT(3)
65*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL_RSV4			BIT(4)
66*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL_RSV5			BIT(5)
67*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL_EOC			BIT(6)
68*4882a593Smuzhiyun #define ADC_ARB_USRP_CNTRL_REQ			BIT(7)
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL			0x198
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * The channel mask includes the bits selecting channel mux and prescaler
73*4882a593Smuzhiyun  * on PM8058, or channel mux and premux on PM8921.
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_CHAN_MASK	0xfc
76*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_RSV0		BIT(0)
77*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_RSV1		BIT(1)
78*4882a593Smuzhiyun /* On PM8058 this is prescaling, on PM8921 this is premux */
79*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_PRESCALEMUX0	BIT(2)
80*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_PRESCALEMUX1	BIT(3)
81*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_SEL0		BIT(4)
82*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_SEL1		BIT(5)
83*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_SEL2		BIT(6)
84*4882a593Smuzhiyun #define ADC_ARB_USRP_AMUX_CNTRL_SEL3		BIT(7)
85*4882a593Smuzhiyun #define ADC_AMUX_PREMUX_SHIFT			2
86*4882a593Smuzhiyun #define ADC_AMUX_SEL_SHIFT			4
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /* We know very little about the bits in this register */
89*4882a593Smuzhiyun #define ADC_ARB_USRP_ANA_PARAM			0x199
90*4882a593Smuzhiyun #define ADC_ARB_USRP_ANA_PARAM_DIS		0xFE
91*4882a593Smuzhiyun #define ADC_ARB_USRP_ANA_PARAM_EN		0xFF
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM			0x19A
94*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT0	BIT(0)
95*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT1	BIT(1)
96*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM_CLK_RATE0	BIT(2)
97*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM_CLK_RATE1	BIT(3)
98*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM_EOC		BIT(4)
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun  * On a later ADC the decimation factors are defined as
101*4882a593Smuzhiyun  * 00 = 512, 01 = 1024, 10 = 2048, 11 = 4096 so assume this
102*4882a593Smuzhiyun  * holds also for this older XOADC.
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM_DEC_RATE0	BIT(5)
105*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM_DEC_RATE1	BIT(6)
106*4882a593Smuzhiyun #define ADC_ARB_USRP_DIG_PARAM_EN		BIT(7)
107*4882a593Smuzhiyun #define ADC_DIG_PARAM_DEC_SHIFT			5
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV			0x19B
110*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV_RST			BIT(0)
111*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV_DTEST0			BIT(1)
112*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV_DTEST1			BIT(2)
113*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV_OP			BIT(3)
114*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV_IP_SEL0		BIT(4)
115*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV_IP_SEL1		BIT(5)
116*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV_IP_SEL2		BIT(6)
117*4882a593Smuzhiyun #define ADC_ARB_USRP_RSV_TRM			BIT(7)
118*4882a593Smuzhiyun #define ADC_RSV_IP_SEL_SHIFT			4
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun #define ADC_ARB_USRP_DATA0			0x19D
121*4882a593Smuzhiyun #define ADC_ARB_USRP_DATA1			0x19C
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun  * Physical channels which MUST exist on all PM variants in order to provide
125*4882a593Smuzhiyun  * proper reference points for calibration.
126*4882a593Smuzhiyun  *
127*4882a593Smuzhiyun  * @PM8XXX_CHANNEL_INTERNAL: 625mV reference channel
128*4882a593Smuzhiyun  * @PM8XXX_CHANNEL_125V: 1250mV reference channel
129*4882a593Smuzhiyun  * @PM8XXX_CHANNEL_INTERNAL_2: 325mV reference channel
130*4882a593Smuzhiyun  * @PM8XXX_CHANNEL_MUXOFF: channel to reduce input load on mux, apparently also
131*4882a593Smuzhiyun  * measures XO temperature
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun #define PM8XXX_CHANNEL_INTERNAL		0x0c
134*4882a593Smuzhiyun #define PM8XXX_CHANNEL_125V		0x0d
135*4882a593Smuzhiyun #define PM8XXX_CHANNEL_INTERNAL_2	0x0e
136*4882a593Smuzhiyun #define PM8XXX_CHANNEL_MUXOFF		0x0f
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun  * PM8058 AMUX premux scaling, two bits. This is done of the channel before
140*4882a593Smuzhiyun  * reaching the AMUX.
141*4882a593Smuzhiyun  */
142*4882a593Smuzhiyun #define PM8058_AMUX_PRESCALE_0 0x0 /* No scaling on the signal */
143*4882a593Smuzhiyun #define PM8058_AMUX_PRESCALE_1 0x1 /* Unity scaling selected by the user */
144*4882a593Smuzhiyun #define PM8058_AMUX_PRESCALE_1_DIV3 0x2 /* 1/3 prescaler on the input */
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /* Defines reference voltage for the XOADC */
147*4882a593Smuzhiyun #define AMUX_RSV0 0x0 /* XO_IN/XOADC_GND, special selection to read XO temp */
148*4882a593Smuzhiyun #define AMUX_RSV1 0x1 /* PMIC_IN/XOADC_GND */
149*4882a593Smuzhiyun #define AMUX_RSV2 0x2 /* PMIC_IN/BMS_CSP */
150*4882a593Smuzhiyun #define AMUX_RSV3 0x3 /* not used */
151*4882a593Smuzhiyun #define AMUX_RSV4 0x4 /* XOADC_GND/XOADC_GND */
152*4882a593Smuzhiyun #define AMUX_RSV5 0x5 /* XOADC_VREF/XOADC_GND */
153*4882a593Smuzhiyun #define XOADC_RSV_MAX 5 /* 3 bits 0..7, 3 and 6,7 are invalid */
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /**
156*4882a593Smuzhiyun  * struct xoadc_channel - encodes channel properties and defaults
157*4882a593Smuzhiyun  * @datasheet_name: the hardwarename of this channel
158*4882a593Smuzhiyun  * @pre_scale_mux: prescale (PM8058) or premux (PM8921) for selecting
159*4882a593Smuzhiyun  * this channel. Both this and the amux channel is needed to uniquely
160*4882a593Smuzhiyun  * identify a channel. Values 0..3.
161*4882a593Smuzhiyun  * @amux_channel: value of the ADC_ARB_USRP_AMUX_CNTRL register for this
162*4882a593Smuzhiyun  * channel, bits 4..7, selects the amux, values 0..f
163*4882a593Smuzhiyun  * @prescale: the channels have hard-coded prescale ratios defined
164*4882a593Smuzhiyun  * by the hardware, this tells us what it is
165*4882a593Smuzhiyun  * @type: corresponding IIO channel type, usually IIO_VOLTAGE or
166*4882a593Smuzhiyun  * IIO_TEMP
167*4882a593Smuzhiyun  * @scale_fn_type: the liner interpolation etc to convert the
168*4882a593Smuzhiyun  * ADC code to the value that IIO expects, in uV or millicelsius
169*4882a593Smuzhiyun  * etc. This scale function can be pretty elaborate if different
170*4882a593Smuzhiyun  * thermistors are connected or other hardware characteristics are
171*4882a593Smuzhiyun  * deployed.
172*4882a593Smuzhiyun  * @amux_ip_rsv: ratiometric scale value used by the analog muxer: this
173*4882a593Smuzhiyun  * selects the reference voltage for ratiometric scaling
174*4882a593Smuzhiyun  */
175*4882a593Smuzhiyun struct xoadc_channel {
176*4882a593Smuzhiyun 	const char *datasheet_name;
177*4882a593Smuzhiyun 	u8 pre_scale_mux:2;
178*4882a593Smuzhiyun 	u8 amux_channel:4;
179*4882a593Smuzhiyun 	const struct vadc_prescale_ratio prescale;
180*4882a593Smuzhiyun 	enum iio_chan_type type;
181*4882a593Smuzhiyun 	enum vadc_scale_fn_type scale_fn_type;
182*4882a593Smuzhiyun 	u8 amux_ip_rsv:3;
183*4882a593Smuzhiyun };
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun  * struct xoadc_variant - encodes the XOADC variant characteristics
187*4882a593Smuzhiyun  * @name: name of this PMIC variant
188*4882a593Smuzhiyun  * @channels: the hardware channels and respective settings and defaults
189*4882a593Smuzhiyun  * @broken_ratiometric: if the PMIC has broken ratiometric scaling (this
190*4882a593Smuzhiyun  * is a known problem on PM8058)
191*4882a593Smuzhiyun  * @prescaling: this variant uses AMUX bits 2 & 3 for prescaling (PM8058)
192*4882a593Smuzhiyun  * @second_level_mux: this variant uses AMUX bits 2 & 3 for a second level
193*4882a593Smuzhiyun  * mux
194*4882a593Smuzhiyun  */
195*4882a593Smuzhiyun struct xoadc_variant {
196*4882a593Smuzhiyun 	const char name[16];
197*4882a593Smuzhiyun 	const struct xoadc_channel *channels;
198*4882a593Smuzhiyun 	bool broken_ratiometric;
199*4882a593Smuzhiyun 	bool prescaling;
200*4882a593Smuzhiyun 	bool second_level_mux;
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /*
204*4882a593Smuzhiyun  * XOADC_CHAN macro parameters:
205*4882a593Smuzhiyun  * _dname: the name of the channel
206*4882a593Smuzhiyun  * _presmux: prescaler (PM8058) or premux (PM8921) setting for this channel
207*4882a593Smuzhiyun  * _amux: the value in bits 2..7 of the ADC_ARB_USRP_AMUX_CNTRL register
208*4882a593Smuzhiyun  * for this channel. On some PMICs some of the bits select a prescaler, and
209*4882a593Smuzhiyun  * on some PMICs some of the bits select various complex multiplex settings.
210*4882a593Smuzhiyun  * _type: IIO channel type
211*4882a593Smuzhiyun  * _prenum: prescaler numerator (dividend)
212*4882a593Smuzhiyun  * _preden: prescaler denominator (divisor)
213*4882a593Smuzhiyun  * _scale: scaling function type, this selects how the raw valued is mangled
214*4882a593Smuzhiyun  * to output the actual processed measurement
215*4882a593Smuzhiyun  * _amip: analog mux input parent when using ratiometric measurements
216*4882a593Smuzhiyun  */
217*4882a593Smuzhiyun #define XOADC_CHAN(_dname, _presmux, _amux, _type, _prenum, _preden, _scale, _amip) \
218*4882a593Smuzhiyun 	{								\
219*4882a593Smuzhiyun 		.datasheet_name = __stringify(_dname),			\
220*4882a593Smuzhiyun 		.pre_scale_mux = _presmux,				\
221*4882a593Smuzhiyun 		.amux_channel = _amux,					\
222*4882a593Smuzhiyun 		.prescale = { .num = _prenum, .den = _preden },		\
223*4882a593Smuzhiyun 		.type = _type,						\
224*4882a593Smuzhiyun 		.scale_fn_type = _scale,				\
225*4882a593Smuzhiyun 		.amux_ip_rsv = _amip,					\
226*4882a593Smuzhiyun 	}
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun  * Taken from arch/arm/mach-msm/board-9615.c in the vendor tree:
230*4882a593Smuzhiyun  * TODO: incomplete, needs testing.
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun static const struct xoadc_channel pm8018_xoadc_channels[] = {
233*4882a593Smuzhiyun 	XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
234*4882a593Smuzhiyun 	XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
235*4882a593Smuzhiyun 	XOADC_CHAN(VPH_PWR, 0x00, 0x02, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
236*4882a593Smuzhiyun 	XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
237*4882a593Smuzhiyun 	/* Used for battery ID or battery temperature */
238*4882a593Smuzhiyun 	XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV2),
239*4882a593Smuzhiyun 	XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
240*4882a593Smuzhiyun 	XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
241*4882a593Smuzhiyun 	XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
242*4882a593Smuzhiyun 	{ }, /* Sentinel */
243*4882a593Smuzhiyun };
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun /*
246*4882a593Smuzhiyun  * Taken from arch/arm/mach-msm/board-8930-pmic.c in the vendor tree:
247*4882a593Smuzhiyun  * TODO: needs testing.
248*4882a593Smuzhiyun  */
249*4882a593Smuzhiyun static const struct xoadc_channel pm8038_xoadc_channels[] = {
250*4882a593Smuzhiyun 	XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
251*4882a593Smuzhiyun 	XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
252*4882a593Smuzhiyun 	XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
253*4882a593Smuzhiyun 	XOADC_CHAN(ICHG, 0x00, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
254*4882a593Smuzhiyun 	XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
255*4882a593Smuzhiyun 	XOADC_CHAN(AMUX5, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
256*4882a593Smuzhiyun 	XOADC_CHAN(AMUX6, 0x00, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
257*4882a593Smuzhiyun 	XOADC_CHAN(AMUX7, 0x00, 0x07, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
258*4882a593Smuzhiyun 	/* AMUX8 used for battery temperature in most cases */
259*4882a593Smuzhiyun 	XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_TEMP, 1, 1, SCALE_THERM_100K_PULLUP, AMUX_RSV2),
260*4882a593Smuzhiyun 	XOADC_CHAN(AMUX9, 0x00, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
261*4882a593Smuzhiyun 	XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 4, SCALE_DEFAULT, AMUX_RSV1),
262*4882a593Smuzhiyun 	XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
263*4882a593Smuzhiyun 	XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
264*4882a593Smuzhiyun 	XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
265*4882a593Smuzhiyun 	XOADC_CHAN(INTERNAL_2, 0x00, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
266*4882a593Smuzhiyun 	XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
267*4882a593Smuzhiyun 	{ }, /* Sentinel */
268*4882a593Smuzhiyun };
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun  * This was created by cross-referencing the vendor tree
272*4882a593Smuzhiyun  * arch/arm/mach-msm/board-msm8x60.c msm_adc_channels_data[]
273*4882a593Smuzhiyun  * with the "channel types" (first field) to find the right
274*4882a593Smuzhiyun  * configuration for these channels on an MSM8x60 i.e. PM8058
275*4882a593Smuzhiyun  * setup.
276*4882a593Smuzhiyun  */
277*4882a593Smuzhiyun static const struct xoadc_channel pm8058_xoadc_channels[] = {
278*4882a593Smuzhiyun 	XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
279*4882a593Smuzhiyun 	XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
280*4882a593Smuzhiyun 	XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 10, SCALE_DEFAULT, AMUX_RSV1),
281*4882a593Smuzhiyun 	XOADC_CHAN(ICHG, 0x00, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
282*4882a593Smuzhiyun 	XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
283*4882a593Smuzhiyun 	/*
284*4882a593Smuzhiyun 	 * AMUX channels 5 thru 9 are referred to as MPP5 thru MPP9 in
285*4882a593Smuzhiyun 	 * some code and documentation. But they are really just 5
286*4882a593Smuzhiyun 	 * channels just like any other. They are connected to a switching
287*4882a593Smuzhiyun 	 * matrix where they can be routed to any of the MPPs, not just
288*4882a593Smuzhiyun 	 * 1-to-1 onto MPP5 thru 9, so naming them MPP5 thru MPP9 is
289*4882a593Smuzhiyun 	 * very confusing.
290*4882a593Smuzhiyun 	 */
291*4882a593Smuzhiyun 	XOADC_CHAN(AMUX5, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
292*4882a593Smuzhiyun 	XOADC_CHAN(AMUX6, 0x00, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
293*4882a593Smuzhiyun 	XOADC_CHAN(AMUX7, 0x00, 0x07, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
294*4882a593Smuzhiyun 	XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
295*4882a593Smuzhiyun 	XOADC_CHAN(AMUX9, 0x00, 0x09, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
296*4882a593Smuzhiyun 	XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
297*4882a593Smuzhiyun 	XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
298*4882a593Smuzhiyun 	XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
299*4882a593Smuzhiyun 	XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
300*4882a593Smuzhiyun 	XOADC_CHAN(INTERNAL_2, 0x00, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
301*4882a593Smuzhiyun 	XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
302*4882a593Smuzhiyun 	/* There are also "unity" and divided by 3 channels (prescaler) but noone is using them */
303*4882a593Smuzhiyun 	{ }, /* Sentinel */
304*4882a593Smuzhiyun };
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun /*
307*4882a593Smuzhiyun  * The PM8921 has some pre-muxing on its channels, this comes from the vendor tree
308*4882a593Smuzhiyun  * include/linux/mfd/pm8xxx/pm8xxx-adc.h
309*4882a593Smuzhiyun  * board-flo-pmic.c (Nexus 7) and board-8064-pmic.c
310*4882a593Smuzhiyun  */
311*4882a593Smuzhiyun static const struct xoadc_channel pm8921_xoadc_channels[] = {
312*4882a593Smuzhiyun 	XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
313*4882a593Smuzhiyun 	XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
314*4882a593Smuzhiyun 	XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
315*4882a593Smuzhiyun 	/* channel "ICHG" is reserved and not used on PM8921 */
316*4882a593Smuzhiyun 	XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
317*4882a593Smuzhiyun 	XOADC_CHAN(IBAT, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
318*4882a593Smuzhiyun 	/* CHAN 6 & 7 (MPP1 & MPP2) are reserved for MPP channels on PM8921 */
319*4882a593Smuzhiyun 	XOADC_CHAN(BATT_THERM, 0x00, 0x08, IIO_TEMP, 1, 1, SCALE_THERM_100K_PULLUP, AMUX_RSV1),
320*4882a593Smuzhiyun 	XOADC_CHAN(BATT_ID, 0x00, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
321*4882a593Smuzhiyun 	XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 4, SCALE_DEFAULT, AMUX_RSV1),
322*4882a593Smuzhiyun 	XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
323*4882a593Smuzhiyun 	XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
324*4882a593Smuzhiyun 	XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
325*4882a593Smuzhiyun 	/* FIXME: look into the scaling of this temperature */
326*4882a593Smuzhiyun 	XOADC_CHAN(CHG_TEMP, 0x00, 0x0e, IIO_TEMP, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
327*4882a593Smuzhiyun 	XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
328*4882a593Smuzhiyun 	/* The following channels have premux bit 0 set to 1 (all end in 4) */
329*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_8, 0x01, 0x00, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
330*4882a593Smuzhiyun 	/* Set scaling to 1/2 based on the name for these two */
331*4882a593Smuzhiyun 	XOADC_CHAN(USB_SNS_DIV20, 0x01, 0x01, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
332*4882a593Smuzhiyun 	XOADC_CHAN(DCIN_SNS_DIV20, 0x01, 0x02, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
333*4882a593Smuzhiyun 	XOADC_CHAN(AMUX3, 0x01, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
334*4882a593Smuzhiyun 	XOADC_CHAN(AMUX4, 0x01, 0x04, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
335*4882a593Smuzhiyun 	XOADC_CHAN(AMUX5, 0x01, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
336*4882a593Smuzhiyun 	XOADC_CHAN(AMUX6, 0x01, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
337*4882a593Smuzhiyun 	XOADC_CHAN(AMUX7, 0x01, 0x07, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
338*4882a593Smuzhiyun 	XOADC_CHAN(AMUX8, 0x01, 0x08, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
339*4882a593Smuzhiyun 	/* Internal test signals, I think */
340*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_1, 0x01, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
341*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_2, 0x01, 0x0a, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
342*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_3, 0x01, 0x0b, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
343*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_4, 0x01, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
344*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_5, 0x01, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
345*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_6, 0x01, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
346*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_7, 0x01, 0x0f, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
347*4882a593Smuzhiyun 	/* The following channels have premux bit 1 set to 1 (all end in 8) */
348*4882a593Smuzhiyun 	/* I guess even ATEST8 will be divided by 3 here */
349*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_8, 0x02, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
350*4882a593Smuzhiyun 	/* I guess div 2 div 3 becomes div 6 */
351*4882a593Smuzhiyun 	XOADC_CHAN(USB_SNS_DIV20_DIV3, 0x02, 0x01, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
352*4882a593Smuzhiyun 	XOADC_CHAN(DCIN_SNS_DIV20_DIV3, 0x02, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
353*4882a593Smuzhiyun 	XOADC_CHAN(AMUX3_DIV3, 0x02, 0x03, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
354*4882a593Smuzhiyun 	XOADC_CHAN(AMUX4_DIV3, 0x02, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
355*4882a593Smuzhiyun 	XOADC_CHAN(AMUX5_DIV3, 0x02, 0x05, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
356*4882a593Smuzhiyun 	XOADC_CHAN(AMUX6_DIV3, 0x02, 0x06, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
357*4882a593Smuzhiyun 	XOADC_CHAN(AMUX7_DIV3, 0x02, 0x07, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
358*4882a593Smuzhiyun 	XOADC_CHAN(AMUX8_DIV3, 0x02, 0x08, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
359*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_1_DIV3, 0x02, 0x09, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
360*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_2_DIV3, 0x02, 0x0a, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
361*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_3_DIV3, 0x02, 0x0b, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
362*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_4_DIV3, 0x02, 0x0c, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
363*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_5_DIV3, 0x02, 0x0d, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
364*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_6_DIV3, 0x02, 0x0e, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
365*4882a593Smuzhiyun 	XOADC_CHAN(ATEST_7_DIV3, 0x02, 0x0f, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
366*4882a593Smuzhiyun 	{ }, /* Sentinel */
367*4882a593Smuzhiyun };
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun /**
370*4882a593Smuzhiyun  * struct pm8xxx_chan_info - ADC channel information
371*4882a593Smuzhiyun  * @name: name of this channel
372*4882a593Smuzhiyun  * @hwchan: pointer to hardware channel information (muxing & scaling settings)
373*4882a593Smuzhiyun  * @calibration: whether to use absolute or ratiometric calibration
374*4882a593Smuzhiyun  * @scale_fn_type: scaling function type
375*4882a593Smuzhiyun  * @decimation: 0,1,2,3
376*4882a593Smuzhiyun  * @amux_ip_rsv: ratiometric scale value if using ratiometric
377*4882a593Smuzhiyun  * calibration: 0, 1, 2, 4, 5.
378*4882a593Smuzhiyun  */
379*4882a593Smuzhiyun struct pm8xxx_chan_info {
380*4882a593Smuzhiyun 	const char *name;
381*4882a593Smuzhiyun 	const struct xoadc_channel *hwchan;
382*4882a593Smuzhiyun 	enum vadc_calibration calibration;
383*4882a593Smuzhiyun 	u8 decimation:2;
384*4882a593Smuzhiyun 	u8 amux_ip_rsv:3;
385*4882a593Smuzhiyun };
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun /**
388*4882a593Smuzhiyun  * struct pm8xxx_xoadc - state container for the XOADC
389*4882a593Smuzhiyun  * @dev: pointer to device
390*4882a593Smuzhiyun  * @map: regmap to access registers
391*4882a593Smuzhiyun  * @variant: XOADC variant characteristics
392*4882a593Smuzhiyun  * @vref: reference voltage regulator
393*4882a593Smuzhiyun  * characteristics of the channels, and sensible default settings
394*4882a593Smuzhiyun  * @nchans: number of channels, configured by the device tree
395*4882a593Smuzhiyun  * @chans: the channel information per-channel, configured by the device tree
396*4882a593Smuzhiyun  * @iio_chans: IIO channel specifiers
397*4882a593Smuzhiyun  * @graph: linear calibration parameters for absolute and
398*4882a593Smuzhiyun  * ratiometric measurements
399*4882a593Smuzhiyun  * @complete: completion to indicate end of conversion
400*4882a593Smuzhiyun  * @lock: lock to restrict access to the hardware to one client at the time
401*4882a593Smuzhiyun  */
402*4882a593Smuzhiyun struct pm8xxx_xoadc {
403*4882a593Smuzhiyun 	struct device *dev;
404*4882a593Smuzhiyun 	struct regmap *map;
405*4882a593Smuzhiyun 	const struct xoadc_variant *variant;
406*4882a593Smuzhiyun 	struct regulator *vref;
407*4882a593Smuzhiyun 	unsigned int nchans;
408*4882a593Smuzhiyun 	struct pm8xxx_chan_info *chans;
409*4882a593Smuzhiyun 	struct iio_chan_spec *iio_chans;
410*4882a593Smuzhiyun 	struct vadc_linear_graph graph[2];
411*4882a593Smuzhiyun 	struct completion complete;
412*4882a593Smuzhiyun 	struct mutex lock;
413*4882a593Smuzhiyun };
414*4882a593Smuzhiyun 
pm8xxx_eoc_irq(int irq,void * d)415*4882a593Smuzhiyun static irqreturn_t pm8xxx_eoc_irq(int irq, void *d)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct iio_dev *indio_dev = d;
418*4882a593Smuzhiyun 	struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	complete(&adc->complete);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	return IRQ_HANDLED;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun static struct pm8xxx_chan_info *
pm8xxx_get_channel(struct pm8xxx_xoadc * adc,u8 chan)426*4882a593Smuzhiyun pm8xxx_get_channel(struct pm8xxx_xoadc *adc, u8 chan)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	int i;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	for (i = 0; i < adc->nchans; i++) {
431*4882a593Smuzhiyun 		struct pm8xxx_chan_info *ch = &adc->chans[i];
432*4882a593Smuzhiyun 		if (ch->hwchan->amux_channel == chan)
433*4882a593Smuzhiyun 			return ch;
434*4882a593Smuzhiyun 	}
435*4882a593Smuzhiyun 	return NULL;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
pm8xxx_read_channel_rsv(struct pm8xxx_xoadc * adc,const struct pm8xxx_chan_info * ch,u8 rsv,u16 * adc_code,bool force_ratiometric)438*4882a593Smuzhiyun static int pm8xxx_read_channel_rsv(struct pm8xxx_xoadc *adc,
439*4882a593Smuzhiyun 				   const struct pm8xxx_chan_info *ch,
440*4882a593Smuzhiyun 				   u8 rsv, u16 *adc_code,
441*4882a593Smuzhiyun 				   bool force_ratiometric)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	int ret;
444*4882a593Smuzhiyun 	unsigned int val;
445*4882a593Smuzhiyun 	u8 rsvmask, rsvval;
446*4882a593Smuzhiyun 	u8 lsb, msb;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	dev_dbg(adc->dev, "read channel \"%s\", amux %d, prescale/mux: %d, rsv %d\n",
449*4882a593Smuzhiyun 		ch->name, ch->hwchan->amux_channel, ch->hwchan->pre_scale_mux, rsv);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	mutex_lock(&adc->lock);
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	/* Mux in this channel */
454*4882a593Smuzhiyun 	val = ch->hwchan->amux_channel << ADC_AMUX_SEL_SHIFT;
455*4882a593Smuzhiyun 	val |= ch->hwchan->pre_scale_mux << ADC_AMUX_PREMUX_SHIFT;
456*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_AMUX_CNTRL, val);
457*4882a593Smuzhiyun 	if (ret)
458*4882a593Smuzhiyun 		goto unlock;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	/* Set up ratiometric scale value, mask off all bits except these */
461*4882a593Smuzhiyun 	rsvmask = (ADC_ARB_USRP_RSV_RST | ADC_ARB_USRP_RSV_DTEST0 |
462*4882a593Smuzhiyun 		   ADC_ARB_USRP_RSV_DTEST1 | ADC_ARB_USRP_RSV_OP);
463*4882a593Smuzhiyun 	if (adc->variant->broken_ratiometric && !force_ratiometric) {
464*4882a593Smuzhiyun 		/*
465*4882a593Smuzhiyun 		 * Apparently the PM8058 has some kind of bug which is
466*4882a593Smuzhiyun 		 * reflected in the vendor tree drivers/misc/pmix8058-xoadc.c
467*4882a593Smuzhiyun 		 * which just hardcodes the RSV selector to SEL1 (0x20) for
468*4882a593Smuzhiyun 		 * most cases and SEL0 (0x10) for the MUXOFF channel only.
469*4882a593Smuzhiyun 		 * If we force ratiometric (currently only done when attempting
470*4882a593Smuzhiyun 		 * to do ratiometric calibration) this doesn't seem to work
471*4882a593Smuzhiyun 		 * very well and I suspect ratiometric conversion is simply
472*4882a593Smuzhiyun 		 * broken or not supported on the PM8058.
473*4882a593Smuzhiyun 		 *
474*4882a593Smuzhiyun 		 * Maybe IO_SEL2 doesn't exist on PM8058 and bits 4 & 5 select
475*4882a593Smuzhiyun 		 * the mode alone.
476*4882a593Smuzhiyun 		 *
477*4882a593Smuzhiyun 		 * Some PM8058 register documentation would be nice to get
478*4882a593Smuzhiyun 		 * this right.
479*4882a593Smuzhiyun 		 */
480*4882a593Smuzhiyun 		if (ch->hwchan->amux_channel == PM8XXX_CHANNEL_MUXOFF)
481*4882a593Smuzhiyun 			rsvval = ADC_ARB_USRP_RSV_IP_SEL0;
482*4882a593Smuzhiyun 		else
483*4882a593Smuzhiyun 			rsvval = ADC_ARB_USRP_RSV_IP_SEL1;
484*4882a593Smuzhiyun 	} else {
485*4882a593Smuzhiyun 		if (rsv == 0xff)
486*4882a593Smuzhiyun 			rsvval = (ch->amux_ip_rsv << ADC_RSV_IP_SEL_SHIFT) |
487*4882a593Smuzhiyun 				ADC_ARB_USRP_RSV_TRM;
488*4882a593Smuzhiyun 		else
489*4882a593Smuzhiyun 			rsvval = (rsv << ADC_RSV_IP_SEL_SHIFT) |
490*4882a593Smuzhiyun 				ADC_ARB_USRP_RSV_TRM;
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	ret = regmap_update_bits(adc->map,
494*4882a593Smuzhiyun 				 ADC_ARB_USRP_RSV,
495*4882a593Smuzhiyun 				 ~rsvmask,
496*4882a593Smuzhiyun 				 rsvval);
497*4882a593Smuzhiyun 	if (ret)
498*4882a593Smuzhiyun 		goto unlock;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_ANA_PARAM,
501*4882a593Smuzhiyun 			   ADC_ARB_USRP_ANA_PARAM_DIS);
502*4882a593Smuzhiyun 	if (ret)
503*4882a593Smuzhiyun 		goto unlock;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	/* Decimation factor */
506*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_DIG_PARAM,
507*4882a593Smuzhiyun 			   ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT0 |
508*4882a593Smuzhiyun 			   ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT1 |
509*4882a593Smuzhiyun 			   ch->decimation << ADC_DIG_PARAM_DEC_SHIFT);
510*4882a593Smuzhiyun 	if (ret)
511*4882a593Smuzhiyun 		goto unlock;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_ANA_PARAM,
514*4882a593Smuzhiyun 			   ADC_ARB_USRP_ANA_PARAM_EN);
515*4882a593Smuzhiyun 	if (ret)
516*4882a593Smuzhiyun 		goto unlock;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/* Enable the arbiter, the Qualcomm code does it twice like this */
519*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
520*4882a593Smuzhiyun 			   ADC_ARB_USRP_CNTRL_EN_ARB);
521*4882a593Smuzhiyun 	if (ret)
522*4882a593Smuzhiyun 		goto unlock;
523*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
524*4882a593Smuzhiyun 			   ADC_ARB_USRP_CNTRL_EN_ARB);
525*4882a593Smuzhiyun 	if (ret)
526*4882a593Smuzhiyun 		goto unlock;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	/* Fire a request! */
530*4882a593Smuzhiyun 	reinit_completion(&adc->complete);
531*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
532*4882a593Smuzhiyun 			   ADC_ARB_USRP_CNTRL_EN_ARB |
533*4882a593Smuzhiyun 			   ADC_ARB_USRP_CNTRL_REQ);
534*4882a593Smuzhiyun 	if (ret)
535*4882a593Smuzhiyun 		goto unlock;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	/* Next the interrupt occurs */
538*4882a593Smuzhiyun 	ret = wait_for_completion_timeout(&adc->complete,
539*4882a593Smuzhiyun 					  VADC_CONV_TIME_MAX_US);
540*4882a593Smuzhiyun 	if (!ret) {
541*4882a593Smuzhiyun 		dev_err(adc->dev, "conversion timed out\n");
542*4882a593Smuzhiyun 		ret = -ETIMEDOUT;
543*4882a593Smuzhiyun 		goto unlock;
544*4882a593Smuzhiyun 	}
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	ret = regmap_read(adc->map, ADC_ARB_USRP_DATA0, &val);
547*4882a593Smuzhiyun 	if (ret)
548*4882a593Smuzhiyun 		goto unlock;
549*4882a593Smuzhiyun 	lsb = val;
550*4882a593Smuzhiyun 	ret = regmap_read(adc->map, ADC_ARB_USRP_DATA1, &val);
551*4882a593Smuzhiyun 	if (ret)
552*4882a593Smuzhiyun 		goto unlock;
553*4882a593Smuzhiyun 	msb = val;
554*4882a593Smuzhiyun 	*adc_code = (msb << 8) | lsb;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	/* Turn off the ADC by setting the arbiter to 0 twice */
557*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, 0);
558*4882a593Smuzhiyun 	if (ret)
559*4882a593Smuzhiyun 		goto unlock;
560*4882a593Smuzhiyun 	ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, 0);
561*4882a593Smuzhiyun 	if (ret)
562*4882a593Smuzhiyun 		goto unlock;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun unlock:
565*4882a593Smuzhiyun 	mutex_unlock(&adc->lock);
566*4882a593Smuzhiyun 	return ret;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
pm8xxx_read_channel(struct pm8xxx_xoadc * adc,const struct pm8xxx_chan_info * ch,u16 * adc_code)569*4882a593Smuzhiyun static int pm8xxx_read_channel(struct pm8xxx_xoadc *adc,
570*4882a593Smuzhiyun 			       const struct pm8xxx_chan_info *ch,
571*4882a593Smuzhiyun 			       u16 *adc_code)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun 	/*
574*4882a593Smuzhiyun 	 * Normally we just use the ratiometric scale value (RSV) predefined
575*4882a593Smuzhiyun 	 * for the channel, but during calibration we need to modify this
576*4882a593Smuzhiyun 	 * so this wrapper is a helper hiding the more complex version.
577*4882a593Smuzhiyun 	 */
578*4882a593Smuzhiyun 	return pm8xxx_read_channel_rsv(adc, ch, 0xff, adc_code, false);
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun 
pm8xxx_calibrate_device(struct pm8xxx_xoadc * adc)581*4882a593Smuzhiyun static int pm8xxx_calibrate_device(struct pm8xxx_xoadc *adc)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	const struct pm8xxx_chan_info *ch;
584*4882a593Smuzhiyun 	u16 read_1250v;
585*4882a593Smuzhiyun 	u16 read_0625v;
586*4882a593Smuzhiyun 	u16 read_nomux_rsv5;
587*4882a593Smuzhiyun 	u16 read_nomux_rsv4;
588*4882a593Smuzhiyun 	int ret;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	adc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
591*4882a593Smuzhiyun 	adc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	/* Common reference channel calibration */
594*4882a593Smuzhiyun 	ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_125V);
595*4882a593Smuzhiyun 	if (!ch)
596*4882a593Smuzhiyun 		return -ENODEV;
597*4882a593Smuzhiyun 	ret = pm8xxx_read_channel(adc, ch, &read_1250v);
598*4882a593Smuzhiyun 	if (ret) {
599*4882a593Smuzhiyun 		dev_err(adc->dev, "could not read 1.25V reference channel\n");
600*4882a593Smuzhiyun 		return -ENODEV;
601*4882a593Smuzhiyun 	}
602*4882a593Smuzhiyun 	ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_INTERNAL);
603*4882a593Smuzhiyun 	if (!ch)
604*4882a593Smuzhiyun 		return -ENODEV;
605*4882a593Smuzhiyun 	ret = pm8xxx_read_channel(adc, ch, &read_0625v);
606*4882a593Smuzhiyun 	if (ret) {
607*4882a593Smuzhiyun 		dev_err(adc->dev, "could not read 0.625V reference channel\n");
608*4882a593Smuzhiyun 		return -ENODEV;
609*4882a593Smuzhiyun 	}
610*4882a593Smuzhiyun 	if (read_1250v == read_0625v) {
611*4882a593Smuzhiyun 		dev_err(adc->dev, "read same ADC code for 1.25V and 0.625V\n");
612*4882a593Smuzhiyun 		return -ENODEV;
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	adc->graph[VADC_CALIB_ABSOLUTE].dy = read_1250v - read_0625v;
616*4882a593Smuzhiyun 	adc->graph[VADC_CALIB_ABSOLUTE].gnd = read_0625v;
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	dev_info(adc->dev, "absolute calibration dx = %d uV, dy = %d units\n",
619*4882a593Smuzhiyun 		 VADC_ABSOLUTE_RANGE_UV, adc->graph[VADC_CALIB_ABSOLUTE].dy);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* Ratiometric calibration */
622*4882a593Smuzhiyun 	ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_MUXOFF);
623*4882a593Smuzhiyun 	if (!ch)
624*4882a593Smuzhiyun 		return -ENODEV;
625*4882a593Smuzhiyun 	ret = pm8xxx_read_channel_rsv(adc, ch, AMUX_RSV5,
626*4882a593Smuzhiyun 				      &read_nomux_rsv5, true);
627*4882a593Smuzhiyun 	if (ret) {
628*4882a593Smuzhiyun 		dev_err(adc->dev, "could not read MUXOFF reference channel\n");
629*4882a593Smuzhiyun 		return -ENODEV;
630*4882a593Smuzhiyun 	}
631*4882a593Smuzhiyun 	ret = pm8xxx_read_channel_rsv(adc, ch, AMUX_RSV4,
632*4882a593Smuzhiyun 				      &read_nomux_rsv4, true);
633*4882a593Smuzhiyun 	if (ret) {
634*4882a593Smuzhiyun 		dev_err(adc->dev, "could not read MUXOFF reference channel\n");
635*4882a593Smuzhiyun 		return -ENODEV;
636*4882a593Smuzhiyun 	}
637*4882a593Smuzhiyun 	adc->graph[VADC_CALIB_RATIOMETRIC].dy =
638*4882a593Smuzhiyun 		read_nomux_rsv5 - read_nomux_rsv4;
639*4882a593Smuzhiyun 	adc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_nomux_rsv4;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	dev_info(adc->dev, "ratiometric calibration dx = %d, dy = %d units\n",
642*4882a593Smuzhiyun 		 VADC_RATIOMETRIC_RANGE,
643*4882a593Smuzhiyun 		 adc->graph[VADC_CALIB_RATIOMETRIC].dy);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	return 0;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun 
pm8xxx_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)648*4882a593Smuzhiyun static int pm8xxx_read_raw(struct iio_dev *indio_dev,
649*4882a593Smuzhiyun 			   struct iio_chan_spec const *chan,
650*4882a593Smuzhiyun 			   int *val, int *val2, long mask)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun 	struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
653*4882a593Smuzhiyun 	const struct pm8xxx_chan_info *ch;
654*4882a593Smuzhiyun 	u16 adc_code;
655*4882a593Smuzhiyun 	int ret;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	switch (mask) {
658*4882a593Smuzhiyun 	case IIO_CHAN_INFO_PROCESSED:
659*4882a593Smuzhiyun 		ch = pm8xxx_get_channel(adc, chan->address);
660*4882a593Smuzhiyun 		if (!ch) {
661*4882a593Smuzhiyun 			dev_err(adc->dev, "no such channel %lu\n",
662*4882a593Smuzhiyun 				chan->address);
663*4882a593Smuzhiyun 			return -EINVAL;
664*4882a593Smuzhiyun 		}
665*4882a593Smuzhiyun 		ret = pm8xxx_read_channel(adc, ch, &adc_code);
666*4882a593Smuzhiyun 		if (ret)
667*4882a593Smuzhiyun 			return ret;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 		ret = qcom_vadc_scale(ch->hwchan->scale_fn_type,
670*4882a593Smuzhiyun 				      &adc->graph[ch->calibration],
671*4882a593Smuzhiyun 				      &ch->hwchan->prescale,
672*4882a593Smuzhiyun 				      (ch->calibration == VADC_CALIB_ABSOLUTE),
673*4882a593Smuzhiyun 				      adc_code, val);
674*4882a593Smuzhiyun 		if (ret)
675*4882a593Smuzhiyun 			return ret;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 		return IIO_VAL_INT;
678*4882a593Smuzhiyun 	case IIO_CHAN_INFO_RAW:
679*4882a593Smuzhiyun 		ch = pm8xxx_get_channel(adc, chan->address);
680*4882a593Smuzhiyun 		if (!ch) {
681*4882a593Smuzhiyun 			dev_err(adc->dev, "no such channel %lu\n",
682*4882a593Smuzhiyun 				chan->address);
683*4882a593Smuzhiyun 			return -EINVAL;
684*4882a593Smuzhiyun 		}
685*4882a593Smuzhiyun 		ret = pm8xxx_read_channel(adc, ch, &adc_code);
686*4882a593Smuzhiyun 		if (ret)
687*4882a593Smuzhiyun 			return ret;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 		*val = (int)adc_code;
690*4882a593Smuzhiyun 		return IIO_VAL_INT;
691*4882a593Smuzhiyun 	default:
692*4882a593Smuzhiyun 		return -EINVAL;
693*4882a593Smuzhiyun 	}
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun 
pm8xxx_of_xlate(struct iio_dev * indio_dev,const struct of_phandle_args * iiospec)696*4882a593Smuzhiyun static int pm8xxx_of_xlate(struct iio_dev *indio_dev,
697*4882a593Smuzhiyun 			   const struct of_phandle_args *iiospec)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun 	struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
700*4882a593Smuzhiyun 	u8 pre_scale_mux;
701*4882a593Smuzhiyun 	u8 amux_channel;
702*4882a593Smuzhiyun 	unsigned int i;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	/*
705*4882a593Smuzhiyun 	 * First cell is prescaler or premux, second cell is analog
706*4882a593Smuzhiyun 	 * mux.
707*4882a593Smuzhiyun 	 */
708*4882a593Smuzhiyun 	if (iiospec->args_count != 2) {
709*4882a593Smuzhiyun 		dev_err(&indio_dev->dev, "wrong number of arguments for %pOFn need 2 got %d\n",
710*4882a593Smuzhiyun 			iiospec->np,
711*4882a593Smuzhiyun 			iiospec->args_count);
712*4882a593Smuzhiyun 		return -EINVAL;
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 	pre_scale_mux = (u8)iiospec->args[0];
715*4882a593Smuzhiyun 	amux_channel = (u8)iiospec->args[1];
716*4882a593Smuzhiyun 	dev_dbg(&indio_dev->dev, "pre scale/mux: %02x, amux: %02x\n",
717*4882a593Smuzhiyun 		pre_scale_mux, amux_channel);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	/* We need to match exactly on the prescale/premux and channel */
720*4882a593Smuzhiyun 	for (i = 0; i < adc->nchans; i++)
721*4882a593Smuzhiyun 		if (adc->chans[i].hwchan->pre_scale_mux == pre_scale_mux &&
722*4882a593Smuzhiyun 		    adc->chans[i].hwchan->amux_channel == amux_channel)
723*4882a593Smuzhiyun 			return i;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	return -EINVAL;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun static const struct iio_info pm8xxx_xoadc_info = {
729*4882a593Smuzhiyun 	.of_xlate = pm8xxx_of_xlate,
730*4882a593Smuzhiyun 	.read_raw = pm8xxx_read_raw,
731*4882a593Smuzhiyun };
732*4882a593Smuzhiyun 
pm8xxx_xoadc_parse_channel(struct device * dev,struct device_node * np,const struct xoadc_channel * hw_channels,struct iio_chan_spec * iio_chan,struct pm8xxx_chan_info * ch)733*4882a593Smuzhiyun static int pm8xxx_xoadc_parse_channel(struct device *dev,
734*4882a593Smuzhiyun 				      struct device_node *np,
735*4882a593Smuzhiyun 				      const struct xoadc_channel *hw_channels,
736*4882a593Smuzhiyun 				      struct iio_chan_spec *iio_chan,
737*4882a593Smuzhiyun 				      struct pm8xxx_chan_info *ch)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	const char *name = np->name;
740*4882a593Smuzhiyun 	const struct xoadc_channel *hwchan;
741*4882a593Smuzhiyun 	u32 pre_scale_mux, amux_channel;
742*4882a593Smuzhiyun 	u32 rsv, dec;
743*4882a593Smuzhiyun 	int ret;
744*4882a593Smuzhiyun 	int chid;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	ret = of_property_read_u32_index(np, "reg", 0, &pre_scale_mux);
747*4882a593Smuzhiyun 	if (ret) {
748*4882a593Smuzhiyun 		dev_err(dev, "invalid pre scale/mux number %s\n", name);
749*4882a593Smuzhiyun 		return ret;
750*4882a593Smuzhiyun 	}
751*4882a593Smuzhiyun 	ret = of_property_read_u32_index(np, "reg", 1, &amux_channel);
752*4882a593Smuzhiyun 	if (ret) {
753*4882a593Smuzhiyun 		dev_err(dev, "invalid amux channel number %s\n", name);
754*4882a593Smuzhiyun 		return ret;
755*4882a593Smuzhiyun 	}
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	/* Find the right channel setting */
758*4882a593Smuzhiyun 	chid = 0;
759*4882a593Smuzhiyun 	hwchan = &hw_channels[0];
760*4882a593Smuzhiyun 	while (hwchan && hwchan->datasheet_name) {
761*4882a593Smuzhiyun 		if (hwchan->pre_scale_mux == pre_scale_mux &&
762*4882a593Smuzhiyun 		    hwchan->amux_channel == amux_channel)
763*4882a593Smuzhiyun 			break;
764*4882a593Smuzhiyun 		hwchan++;
765*4882a593Smuzhiyun 		chid++;
766*4882a593Smuzhiyun 	}
767*4882a593Smuzhiyun 	/* The sentinel does not have a name assigned */
768*4882a593Smuzhiyun 	if (!hwchan->datasheet_name) {
769*4882a593Smuzhiyun 		dev_err(dev, "could not locate channel %02x/%02x\n",
770*4882a593Smuzhiyun 			pre_scale_mux, amux_channel);
771*4882a593Smuzhiyun 		return -EINVAL;
772*4882a593Smuzhiyun 	}
773*4882a593Smuzhiyun 	ch->name = name;
774*4882a593Smuzhiyun 	ch->hwchan = hwchan;
775*4882a593Smuzhiyun 	/* Everyone seems to use absolute calibration except in special cases */
776*4882a593Smuzhiyun 	ch->calibration = VADC_CALIB_ABSOLUTE;
777*4882a593Smuzhiyun 	/* Everyone seems to use default ("type 2") decimation */
778*4882a593Smuzhiyun 	ch->decimation = VADC_DEF_DECIMATION;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	if (!of_property_read_u32(np, "qcom,ratiometric", &rsv)) {
781*4882a593Smuzhiyun 		ch->calibration = VADC_CALIB_RATIOMETRIC;
782*4882a593Smuzhiyun 		if (rsv > XOADC_RSV_MAX) {
783*4882a593Smuzhiyun 			dev_err(dev, "%s too large RSV value %d\n", name, rsv);
784*4882a593Smuzhiyun 			return -EINVAL;
785*4882a593Smuzhiyun 		}
786*4882a593Smuzhiyun 		if (rsv == AMUX_RSV3) {
787*4882a593Smuzhiyun 			dev_err(dev, "%s invalid RSV value %d\n", name, rsv);
788*4882a593Smuzhiyun 			return -EINVAL;
789*4882a593Smuzhiyun 		}
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	/* Optional decimation, if omitted we use the default */
793*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "qcom,decimation", &dec);
794*4882a593Smuzhiyun 	if (!ret) {
795*4882a593Smuzhiyun 		ret = qcom_vadc_decimation_from_dt(dec);
796*4882a593Smuzhiyun 		if (ret < 0) {
797*4882a593Smuzhiyun 			dev_err(dev, "%s invalid decimation %d\n",
798*4882a593Smuzhiyun 				name, dec);
799*4882a593Smuzhiyun 			return ret;
800*4882a593Smuzhiyun 		}
801*4882a593Smuzhiyun 		ch->decimation = ret;
802*4882a593Smuzhiyun 	}
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	iio_chan->channel = chid;
805*4882a593Smuzhiyun 	iio_chan->address = hwchan->amux_channel;
806*4882a593Smuzhiyun 	iio_chan->datasheet_name = hwchan->datasheet_name;
807*4882a593Smuzhiyun 	iio_chan->type = hwchan->type;
808*4882a593Smuzhiyun 	/* All channels are raw or processed */
809*4882a593Smuzhiyun 	iio_chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
810*4882a593Smuzhiyun 		BIT(IIO_CHAN_INFO_PROCESSED);
811*4882a593Smuzhiyun 	iio_chan->indexed = 1;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	dev_dbg(dev, "channel [PRESCALE/MUX: %02x AMUX: %02x] \"%s\" "
814*4882a593Smuzhiyun 		"ref voltage: %d, decimation %d "
815*4882a593Smuzhiyun 		"prescale %d/%d, scale function %d\n",
816*4882a593Smuzhiyun 		hwchan->pre_scale_mux, hwchan->amux_channel, ch->name,
817*4882a593Smuzhiyun 		ch->amux_ip_rsv, ch->decimation, hwchan->prescale.num,
818*4882a593Smuzhiyun 		hwchan->prescale.den, hwchan->scale_fn_type);
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	return 0;
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun 
pm8xxx_xoadc_parse_channels(struct pm8xxx_xoadc * adc,struct device_node * np)823*4882a593Smuzhiyun static int pm8xxx_xoadc_parse_channels(struct pm8xxx_xoadc *adc,
824*4882a593Smuzhiyun 				       struct device_node *np)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun 	struct device_node *child;
827*4882a593Smuzhiyun 	struct pm8xxx_chan_info *ch;
828*4882a593Smuzhiyun 	int ret;
829*4882a593Smuzhiyun 	int i;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	adc->nchans = of_get_available_child_count(np);
832*4882a593Smuzhiyun 	if (!adc->nchans) {
833*4882a593Smuzhiyun 		dev_err(adc->dev, "no channel children\n");
834*4882a593Smuzhiyun 		return -ENODEV;
835*4882a593Smuzhiyun 	}
836*4882a593Smuzhiyun 	dev_dbg(adc->dev, "found %d ADC channels\n", adc->nchans);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	adc->iio_chans = devm_kcalloc(adc->dev, adc->nchans,
839*4882a593Smuzhiyun 				      sizeof(*adc->iio_chans), GFP_KERNEL);
840*4882a593Smuzhiyun 	if (!adc->iio_chans)
841*4882a593Smuzhiyun 		return -ENOMEM;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	adc->chans = devm_kcalloc(adc->dev, adc->nchans,
844*4882a593Smuzhiyun 				  sizeof(*adc->chans), GFP_KERNEL);
845*4882a593Smuzhiyun 	if (!adc->chans)
846*4882a593Smuzhiyun 		return -ENOMEM;
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	i = 0;
849*4882a593Smuzhiyun 	for_each_available_child_of_node(np, child) {
850*4882a593Smuzhiyun 		ch = &adc->chans[i];
851*4882a593Smuzhiyun 		ret = pm8xxx_xoadc_parse_channel(adc->dev, child,
852*4882a593Smuzhiyun 						 adc->variant->channels,
853*4882a593Smuzhiyun 						 &adc->iio_chans[i],
854*4882a593Smuzhiyun 						 ch);
855*4882a593Smuzhiyun 		if (ret) {
856*4882a593Smuzhiyun 			of_node_put(child);
857*4882a593Smuzhiyun 			return ret;
858*4882a593Smuzhiyun 		}
859*4882a593Smuzhiyun 		i++;
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	/* Check for required channels */
863*4882a593Smuzhiyun 	ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_125V);
864*4882a593Smuzhiyun 	if (!ch) {
865*4882a593Smuzhiyun 		dev_err(adc->dev, "missing 1.25V reference channel\n");
866*4882a593Smuzhiyun 		return -ENODEV;
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun 	ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_INTERNAL);
869*4882a593Smuzhiyun 	if (!ch) {
870*4882a593Smuzhiyun 		dev_err(adc->dev, "missing 0.625V reference channel\n");
871*4882a593Smuzhiyun 		return -ENODEV;
872*4882a593Smuzhiyun 	}
873*4882a593Smuzhiyun 	ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_MUXOFF);
874*4882a593Smuzhiyun 	if (!ch) {
875*4882a593Smuzhiyun 		dev_err(adc->dev, "missing MUXOFF reference channel\n");
876*4882a593Smuzhiyun 		return -ENODEV;
877*4882a593Smuzhiyun 	}
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	return 0;
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun 
pm8xxx_xoadc_probe(struct platform_device * pdev)882*4882a593Smuzhiyun static int pm8xxx_xoadc_probe(struct platform_device *pdev)
883*4882a593Smuzhiyun {
884*4882a593Smuzhiyun 	const struct xoadc_variant *variant;
885*4882a593Smuzhiyun 	struct pm8xxx_xoadc *adc;
886*4882a593Smuzhiyun 	struct iio_dev *indio_dev;
887*4882a593Smuzhiyun 	struct device_node *np = pdev->dev.of_node;
888*4882a593Smuzhiyun 	struct regmap *map;
889*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
890*4882a593Smuzhiyun 	int ret;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	variant = of_device_get_match_data(dev);
893*4882a593Smuzhiyun 	if (!variant)
894*4882a593Smuzhiyun 		return -ENODEV;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
897*4882a593Smuzhiyun 	if (!indio_dev)
898*4882a593Smuzhiyun 		return -ENOMEM;
899*4882a593Smuzhiyun 	platform_set_drvdata(pdev, indio_dev);
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	adc = iio_priv(indio_dev);
902*4882a593Smuzhiyun 	adc->dev = dev;
903*4882a593Smuzhiyun 	adc->variant = variant;
904*4882a593Smuzhiyun 	init_completion(&adc->complete);
905*4882a593Smuzhiyun 	mutex_init(&adc->lock);
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	ret = pm8xxx_xoadc_parse_channels(adc, np);
908*4882a593Smuzhiyun 	if (ret)
909*4882a593Smuzhiyun 		return ret;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	map = dev_get_regmap(dev->parent, NULL);
912*4882a593Smuzhiyun 	if (!map) {
913*4882a593Smuzhiyun 		dev_err(dev, "parent regmap unavailable.\n");
914*4882a593Smuzhiyun 		return -ENXIO;
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun 	adc->map = map;
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	/* Bring up regulator */
919*4882a593Smuzhiyun 	adc->vref = devm_regulator_get(dev, "xoadc-ref");
920*4882a593Smuzhiyun 	if (IS_ERR(adc->vref)) {
921*4882a593Smuzhiyun 		dev_err(dev, "failed to get XOADC VREF regulator\n");
922*4882a593Smuzhiyun 		return PTR_ERR(adc->vref);
923*4882a593Smuzhiyun 	}
924*4882a593Smuzhiyun 	ret = regulator_enable(adc->vref);
925*4882a593Smuzhiyun 	if (ret) {
926*4882a593Smuzhiyun 		dev_err(dev, "failed to enable XOADC VREF regulator\n");
927*4882a593Smuzhiyun 		return ret;
928*4882a593Smuzhiyun 	}
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
931*4882a593Smuzhiyun 			pm8xxx_eoc_irq, NULL, 0, variant->name, indio_dev);
932*4882a593Smuzhiyun 	if (ret) {
933*4882a593Smuzhiyun 		dev_err(dev, "unable to request IRQ\n");
934*4882a593Smuzhiyun 		goto out_disable_vref;
935*4882a593Smuzhiyun 	}
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	indio_dev->name = variant->name;
938*4882a593Smuzhiyun 	indio_dev->modes = INDIO_DIRECT_MODE;
939*4882a593Smuzhiyun 	indio_dev->info = &pm8xxx_xoadc_info;
940*4882a593Smuzhiyun 	indio_dev->channels = adc->iio_chans;
941*4882a593Smuzhiyun 	indio_dev->num_channels = adc->nchans;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	ret = iio_device_register(indio_dev);
944*4882a593Smuzhiyun 	if (ret)
945*4882a593Smuzhiyun 		goto out_disable_vref;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	ret = pm8xxx_calibrate_device(adc);
948*4882a593Smuzhiyun 	if (ret)
949*4882a593Smuzhiyun 		goto out_unreg_device;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	dev_info(dev, "%s XOADC driver enabled\n", variant->name);
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	return 0;
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun out_unreg_device:
956*4882a593Smuzhiyun 	iio_device_unregister(indio_dev);
957*4882a593Smuzhiyun out_disable_vref:
958*4882a593Smuzhiyun 	regulator_disable(adc->vref);
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	return ret;
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun 
pm8xxx_xoadc_remove(struct platform_device * pdev)963*4882a593Smuzhiyun static int pm8xxx_xoadc_remove(struct platform_device *pdev)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
966*4882a593Smuzhiyun 	struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	iio_device_unregister(indio_dev);
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	regulator_disable(adc->vref);
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	return 0;
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun static const struct xoadc_variant pm8018_variant = {
976*4882a593Smuzhiyun 	.name = "PM8018-XOADC",
977*4882a593Smuzhiyun 	.channels = pm8018_xoadc_channels,
978*4882a593Smuzhiyun };
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun static const struct xoadc_variant pm8038_variant = {
981*4882a593Smuzhiyun 	.name = "PM8038-XOADC",
982*4882a593Smuzhiyun 	.channels = pm8038_xoadc_channels,
983*4882a593Smuzhiyun };
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun static const struct xoadc_variant pm8058_variant = {
986*4882a593Smuzhiyun 	.name = "PM8058-XOADC",
987*4882a593Smuzhiyun 	.channels = pm8058_xoadc_channels,
988*4882a593Smuzhiyun 	.broken_ratiometric = true,
989*4882a593Smuzhiyun 	.prescaling = true,
990*4882a593Smuzhiyun };
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun static const struct xoadc_variant pm8921_variant = {
993*4882a593Smuzhiyun 	.name = "PM8921-XOADC",
994*4882a593Smuzhiyun 	.channels = pm8921_xoadc_channels,
995*4882a593Smuzhiyun 	.second_level_mux = true,
996*4882a593Smuzhiyun };
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun static const struct of_device_id pm8xxx_xoadc_id_table[] = {
999*4882a593Smuzhiyun 	{
1000*4882a593Smuzhiyun 		.compatible = "qcom,pm8018-adc",
1001*4882a593Smuzhiyun 		.data = &pm8018_variant,
1002*4882a593Smuzhiyun 	},
1003*4882a593Smuzhiyun 	{
1004*4882a593Smuzhiyun 		.compatible = "qcom,pm8038-adc",
1005*4882a593Smuzhiyun 		.data = &pm8038_variant,
1006*4882a593Smuzhiyun 	},
1007*4882a593Smuzhiyun 	{
1008*4882a593Smuzhiyun 		.compatible = "qcom,pm8058-adc",
1009*4882a593Smuzhiyun 		.data = &pm8058_variant,
1010*4882a593Smuzhiyun 	},
1011*4882a593Smuzhiyun 	{
1012*4882a593Smuzhiyun 		.compatible = "qcom,pm8921-adc",
1013*4882a593Smuzhiyun 		.data = &pm8921_variant,
1014*4882a593Smuzhiyun 	},
1015*4882a593Smuzhiyun 	{ },
1016*4882a593Smuzhiyun };
1017*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, pm8xxx_xoadc_id_table);
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun static struct platform_driver pm8xxx_xoadc_driver = {
1020*4882a593Smuzhiyun 	.driver		= {
1021*4882a593Smuzhiyun 		.name	= "pm8xxx-adc",
1022*4882a593Smuzhiyun 		.of_match_table = pm8xxx_xoadc_id_table,
1023*4882a593Smuzhiyun 	},
1024*4882a593Smuzhiyun 	.probe		= pm8xxx_xoadc_probe,
1025*4882a593Smuzhiyun 	.remove		= pm8xxx_xoadc_remove,
1026*4882a593Smuzhiyun };
1027*4882a593Smuzhiyun module_platform_driver(pm8xxx_xoadc_driver);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun MODULE_DESCRIPTION("PM8xxx XOADC driver");
1030*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1031*4882a593Smuzhiyun MODULE_ALIAS("platform:pm8xxx-xoadc");
1032