109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
263c3ecd9SLinus Walleij /*
363c3ecd9SLinus Walleij * Qualcomm PM8xxx PMIC XOADC driver
463c3ecd9SLinus Walleij *
563c3ecd9SLinus Walleij * These ADCs are known as HK/XO (house keeping / chrystal oscillator)
663c3ecd9SLinus Walleij * "XO" in "XOADC" means Chrystal Oscillator. It's a bunch of
763c3ecd9SLinus Walleij * specific-purpose and general purpose ADC converters and channels.
863c3ecd9SLinus Walleij *
963c3ecd9SLinus Walleij * Copyright (C) 2017 Linaro Ltd.
1063c3ecd9SLinus Walleij * Author: Linus Walleij <linus.walleij@linaro.org>
1163c3ecd9SLinus Walleij */
1263c3ecd9SLinus Walleij
13ec82edb2SDmitry Baryshkov #include <linux/iio/adc/qcom-vadc-common.h>
1463c3ecd9SLinus Walleij #include <linux/iio/iio.h>
1563c3ecd9SLinus Walleij #include <linux/iio/sysfs.h>
1663c3ecd9SLinus Walleij #include <linux/module.h>
179e90c177SNuno Sá #include <linux/mod_devicetable.h>
1863c3ecd9SLinus Walleij #include <linux/platform_device.h>
199e90c177SNuno Sá #include <linux/property.h>
2063c3ecd9SLinus Walleij #include <linux/regmap.h>
2163c3ecd9SLinus Walleij #include <linux/init.h>
2263c3ecd9SLinus Walleij #include <linux/interrupt.h>
2363c3ecd9SLinus Walleij #include <linux/regulator/consumer.h>
2463c3ecd9SLinus Walleij
2563c3ecd9SLinus Walleij /*
2663c3ecd9SLinus Walleij * Definitions for the "user processor" registers lifted from the v3.4
2763c3ecd9SLinus Walleij * Qualcomm tree. Their kernel has two out-of-tree drivers for the ADC:
2863c3ecd9SLinus Walleij * drivers/misc/pmic8058-xoadc.c
2963c3ecd9SLinus Walleij * drivers/hwmon/pm8xxx-adc.c
3063c3ecd9SLinus Walleij * None of them contain any complete register specification, so this is
3163c3ecd9SLinus Walleij * a best effort of combining the information.
3263c3ecd9SLinus Walleij */
3363c3ecd9SLinus Walleij
3463c3ecd9SLinus Walleij /* These appear to be "battery monitor" registers */
3563c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1 0x17e
3663c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1_EN_BTM BIT(0)
3763c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1_SEL_OP_MODE BIT(1)
3863c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL1 BIT(2)
3963c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL2 BIT(3)
4063c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL3 BIT(4)
4163c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1_MEAS_INTERVAL4 BIT(5)
4263c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1_EOC BIT(6)
4363c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL1_REQ BIT(7)
4463c3ecd9SLinus Walleij
4563c3ecd9SLinus Walleij #define ADC_ARB_BTM_AMUX_CNTRL 0x17f
4663c3ecd9SLinus Walleij #define ADC_ARB_BTM_ANA_PARAM 0x180
4763c3ecd9SLinus Walleij #define ADC_ARB_BTM_DIG_PARAM 0x181
4863c3ecd9SLinus Walleij #define ADC_ARB_BTM_RSV 0x182
4963c3ecd9SLinus Walleij #define ADC_ARB_BTM_DATA1 0x183
5063c3ecd9SLinus Walleij #define ADC_ARB_BTM_DATA0 0x184
5163c3ecd9SLinus Walleij #define ADC_ARB_BTM_BAT_COOL_THR1 0x185
5263c3ecd9SLinus Walleij #define ADC_ARB_BTM_BAT_COOL_THR0 0x186
5363c3ecd9SLinus Walleij #define ADC_ARB_BTM_BAT_WARM_THR1 0x187
5463c3ecd9SLinus Walleij #define ADC_ARB_BTM_BAT_WARM_THR0 0x188
5563c3ecd9SLinus Walleij #define ADC_ARB_BTM_CNTRL2 0x18c
5663c3ecd9SLinus Walleij
5763c3ecd9SLinus Walleij /* Proper ADC registers */
5863c3ecd9SLinus Walleij
5963c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL 0x197
6063c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL_EN_ARB BIT(0)
6163c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL_RSV1 BIT(1)
6263c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL_RSV2 BIT(2)
6363c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL_RSV3 BIT(3)
6463c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL_RSV4 BIT(4)
6563c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL_RSV5 BIT(5)
6663c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL_EOC BIT(6)
6763c3ecd9SLinus Walleij #define ADC_ARB_USRP_CNTRL_REQ BIT(7)
6863c3ecd9SLinus Walleij
6963c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL 0x198
7063c3ecd9SLinus Walleij /*
7163c3ecd9SLinus Walleij * The channel mask includes the bits selecting channel mux and prescaler
7263c3ecd9SLinus Walleij * on PM8058, or channel mux and premux on PM8921.
7363c3ecd9SLinus Walleij */
7463c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_CHAN_MASK 0xfc
7563c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_RSV0 BIT(0)
7663c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_RSV1 BIT(1)
7763c3ecd9SLinus Walleij /* On PM8058 this is prescaling, on PM8921 this is premux */
7863c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_PRESCALEMUX0 BIT(2)
7963c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_PRESCALEMUX1 BIT(3)
8063c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_SEL0 BIT(4)
8163c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_SEL1 BIT(5)
8263c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_SEL2 BIT(6)
8363c3ecd9SLinus Walleij #define ADC_ARB_USRP_AMUX_CNTRL_SEL3 BIT(7)
8463c3ecd9SLinus Walleij #define ADC_AMUX_PREMUX_SHIFT 2
8563c3ecd9SLinus Walleij #define ADC_AMUX_SEL_SHIFT 4
8663c3ecd9SLinus Walleij
8763c3ecd9SLinus Walleij /* We know very little about the bits in this register */
8863c3ecd9SLinus Walleij #define ADC_ARB_USRP_ANA_PARAM 0x199
8963c3ecd9SLinus Walleij #define ADC_ARB_USRP_ANA_PARAM_DIS 0xFE
9063c3ecd9SLinus Walleij #define ADC_ARB_USRP_ANA_PARAM_EN 0xFF
9163c3ecd9SLinus Walleij
9263c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM 0x19A
9363c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT0 BIT(0)
9463c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT1 BIT(1)
9563c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM_CLK_RATE0 BIT(2)
9663c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM_CLK_RATE1 BIT(3)
9763c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM_EOC BIT(4)
9863c3ecd9SLinus Walleij /*
9963c3ecd9SLinus Walleij * On a later ADC the decimation factors are defined as
10063c3ecd9SLinus Walleij * 00 = 512, 01 = 1024, 10 = 2048, 11 = 4096 so assume this
10163c3ecd9SLinus Walleij * holds also for this older XOADC.
10263c3ecd9SLinus Walleij */
10363c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM_DEC_RATE0 BIT(5)
10463c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM_DEC_RATE1 BIT(6)
10563c3ecd9SLinus Walleij #define ADC_ARB_USRP_DIG_PARAM_EN BIT(7)
10663c3ecd9SLinus Walleij #define ADC_DIG_PARAM_DEC_SHIFT 5
10763c3ecd9SLinus Walleij
10863c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV 0x19B
10963c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV_RST BIT(0)
11063c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV_DTEST0 BIT(1)
11163c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV_DTEST1 BIT(2)
11263c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV_OP BIT(3)
11363c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV_IP_SEL0 BIT(4)
11463c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV_IP_SEL1 BIT(5)
11563c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV_IP_SEL2 BIT(6)
11663c3ecd9SLinus Walleij #define ADC_ARB_USRP_RSV_TRM BIT(7)
11763c3ecd9SLinus Walleij #define ADC_RSV_IP_SEL_SHIFT 4
11863c3ecd9SLinus Walleij
11963c3ecd9SLinus Walleij #define ADC_ARB_USRP_DATA0 0x19D
12063c3ecd9SLinus Walleij #define ADC_ARB_USRP_DATA1 0x19C
12163c3ecd9SLinus Walleij
1228f0f0f2cSLee Jones /*
12363c3ecd9SLinus Walleij * Physical channels which MUST exist on all PM variants in order to provide
12463c3ecd9SLinus Walleij * proper reference points for calibration.
12563c3ecd9SLinus Walleij *
12663c3ecd9SLinus Walleij * @PM8XXX_CHANNEL_INTERNAL: 625mV reference channel
12763c3ecd9SLinus Walleij * @PM8XXX_CHANNEL_125V: 1250mV reference channel
12863c3ecd9SLinus Walleij * @PM8XXX_CHANNEL_INTERNAL_2: 325mV reference channel
12963c3ecd9SLinus Walleij * @PM8XXX_CHANNEL_MUXOFF: channel to reduce input load on mux, apparently also
13063c3ecd9SLinus Walleij * measures XO temperature
13163c3ecd9SLinus Walleij */
13263c3ecd9SLinus Walleij #define PM8XXX_CHANNEL_INTERNAL 0x0c
13363c3ecd9SLinus Walleij #define PM8XXX_CHANNEL_125V 0x0d
13463c3ecd9SLinus Walleij #define PM8XXX_CHANNEL_INTERNAL_2 0x0e
13563c3ecd9SLinus Walleij #define PM8XXX_CHANNEL_MUXOFF 0x0f
13663c3ecd9SLinus Walleij
13763c3ecd9SLinus Walleij /*
13863c3ecd9SLinus Walleij * PM8058 AMUX premux scaling, two bits. This is done of the channel before
13963c3ecd9SLinus Walleij * reaching the AMUX.
14063c3ecd9SLinus Walleij */
14163c3ecd9SLinus Walleij #define PM8058_AMUX_PRESCALE_0 0x0 /* No scaling on the signal */
14263c3ecd9SLinus Walleij #define PM8058_AMUX_PRESCALE_1 0x1 /* Unity scaling selected by the user */
14363c3ecd9SLinus Walleij #define PM8058_AMUX_PRESCALE_1_DIV3 0x2 /* 1/3 prescaler on the input */
14463c3ecd9SLinus Walleij
14563c3ecd9SLinus Walleij /* Defines reference voltage for the XOADC */
14663c3ecd9SLinus Walleij #define AMUX_RSV0 0x0 /* XO_IN/XOADC_GND, special selection to read XO temp */
14763c3ecd9SLinus Walleij #define AMUX_RSV1 0x1 /* PMIC_IN/XOADC_GND */
14863c3ecd9SLinus Walleij #define AMUX_RSV2 0x2 /* PMIC_IN/BMS_CSP */
14963c3ecd9SLinus Walleij #define AMUX_RSV3 0x3 /* not used */
15063c3ecd9SLinus Walleij #define AMUX_RSV4 0x4 /* XOADC_GND/XOADC_GND */
15163c3ecd9SLinus Walleij #define AMUX_RSV5 0x5 /* XOADC_VREF/XOADC_GND */
15263c3ecd9SLinus Walleij #define XOADC_RSV_MAX 5 /* 3 bits 0..7, 3 and 6,7 are invalid */
15363c3ecd9SLinus Walleij
15463c3ecd9SLinus Walleij /**
15563c3ecd9SLinus Walleij * struct xoadc_channel - encodes channel properties and defaults
15663c3ecd9SLinus Walleij * @datasheet_name: the hardwarename of this channel
15763c3ecd9SLinus Walleij * @pre_scale_mux: prescale (PM8058) or premux (PM8921) for selecting
15863c3ecd9SLinus Walleij * this channel. Both this and the amux channel is needed to uniquely
15963c3ecd9SLinus Walleij * identify a channel. Values 0..3.
16063c3ecd9SLinus Walleij * @amux_channel: value of the ADC_ARB_USRP_AMUX_CNTRL register for this
16163c3ecd9SLinus Walleij * channel, bits 4..7, selects the amux, values 0..f
16263c3ecd9SLinus Walleij * @prescale: the channels have hard-coded prescale ratios defined
16363c3ecd9SLinus Walleij * by the hardware, this tells us what it is
16463c3ecd9SLinus Walleij * @type: corresponding IIO channel type, usually IIO_VOLTAGE or
16563c3ecd9SLinus Walleij * IIO_TEMP
16663c3ecd9SLinus Walleij * @scale_fn_type: the liner interpolation etc to convert the
16763c3ecd9SLinus Walleij * ADC code to the value that IIO expects, in uV or millicelsius
16863c3ecd9SLinus Walleij * etc. This scale function can be pretty elaborate if different
16963c3ecd9SLinus Walleij * thermistors are connected or other hardware characteristics are
17063c3ecd9SLinus Walleij * deployed.
17163c3ecd9SLinus Walleij * @amux_ip_rsv: ratiometric scale value used by the analog muxer: this
17263c3ecd9SLinus Walleij * selects the reference voltage for ratiometric scaling
17363c3ecd9SLinus Walleij */
17463c3ecd9SLinus Walleij struct xoadc_channel {
17563c3ecd9SLinus Walleij const char *datasheet_name;
17663c3ecd9SLinus Walleij u8 pre_scale_mux:2;
17763c3ecd9SLinus Walleij u8 amux_channel:4;
178a5e9b2ddSAndy Shevchenko const struct u32_fract prescale;
17963c3ecd9SLinus Walleij enum iio_chan_type type;
18063c3ecd9SLinus Walleij enum vadc_scale_fn_type scale_fn_type;
18163c3ecd9SLinus Walleij u8 amux_ip_rsv:3;
18263c3ecd9SLinus Walleij };
18363c3ecd9SLinus Walleij
18463c3ecd9SLinus Walleij /**
18563c3ecd9SLinus Walleij * struct xoadc_variant - encodes the XOADC variant characteristics
18663c3ecd9SLinus Walleij * @name: name of this PMIC variant
18763c3ecd9SLinus Walleij * @channels: the hardware channels and respective settings and defaults
18863c3ecd9SLinus Walleij * @broken_ratiometric: if the PMIC has broken ratiometric scaling (this
18963c3ecd9SLinus Walleij * is a known problem on PM8058)
19063c3ecd9SLinus Walleij * @prescaling: this variant uses AMUX bits 2 & 3 for prescaling (PM8058)
19163c3ecd9SLinus Walleij * @second_level_mux: this variant uses AMUX bits 2 & 3 for a second level
19263c3ecd9SLinus Walleij * mux
19363c3ecd9SLinus Walleij */
19463c3ecd9SLinus Walleij struct xoadc_variant {
19563c3ecd9SLinus Walleij const char name[16];
19663c3ecd9SLinus Walleij const struct xoadc_channel *channels;
19763c3ecd9SLinus Walleij bool broken_ratiometric;
19863c3ecd9SLinus Walleij bool prescaling;
19963c3ecd9SLinus Walleij bool second_level_mux;
20063c3ecd9SLinus Walleij };
20163c3ecd9SLinus Walleij
20263c3ecd9SLinus Walleij /*
20363c3ecd9SLinus Walleij * XOADC_CHAN macro parameters:
20463c3ecd9SLinus Walleij * _dname: the name of the channel
20563c3ecd9SLinus Walleij * _presmux: prescaler (PM8058) or premux (PM8921) setting for this channel
20663c3ecd9SLinus Walleij * _amux: the value in bits 2..7 of the ADC_ARB_USRP_AMUX_CNTRL register
20763c3ecd9SLinus Walleij * for this channel. On some PMICs some of the bits select a prescaler, and
20863c3ecd9SLinus Walleij * on some PMICs some of the bits select various complex multiplex settings.
20963c3ecd9SLinus Walleij * _type: IIO channel type
21063c3ecd9SLinus Walleij * _prenum: prescaler numerator (dividend)
21163c3ecd9SLinus Walleij * _preden: prescaler denominator (divisor)
21263c3ecd9SLinus Walleij * _scale: scaling function type, this selects how the raw valued is mangled
21363c3ecd9SLinus Walleij * to output the actual processed measurement
21463c3ecd9SLinus Walleij * _amip: analog mux input parent when using ratiometric measurements
21563c3ecd9SLinus Walleij */
21663c3ecd9SLinus Walleij #define XOADC_CHAN(_dname, _presmux, _amux, _type, _prenum, _preden, _scale, _amip) \
21763c3ecd9SLinus Walleij { \
21863c3ecd9SLinus Walleij .datasheet_name = __stringify(_dname), \
21963c3ecd9SLinus Walleij .pre_scale_mux = _presmux, \
22063c3ecd9SLinus Walleij .amux_channel = _amux, \
221a5e9b2ddSAndy Shevchenko .prescale = { \
222a5e9b2ddSAndy Shevchenko .numerator = _prenum, .denominator = _preden, \
223a5e9b2ddSAndy Shevchenko }, \
22463c3ecd9SLinus Walleij .type = _type, \
22563c3ecd9SLinus Walleij .scale_fn_type = _scale, \
22663c3ecd9SLinus Walleij .amux_ip_rsv = _amip, \
22763c3ecd9SLinus Walleij }
22863c3ecd9SLinus Walleij
22963c3ecd9SLinus Walleij /*
23063c3ecd9SLinus Walleij * Taken from arch/arm/mach-msm/board-9615.c in the vendor tree:
23163c3ecd9SLinus Walleij * TODO: incomplete, needs testing.
23263c3ecd9SLinus Walleij */
23363c3ecd9SLinus Walleij static const struct xoadc_channel pm8018_xoadc_channels[] = {
23463c3ecd9SLinus Walleij XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
23563c3ecd9SLinus Walleij XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
23663c3ecd9SLinus Walleij XOADC_CHAN(VPH_PWR, 0x00, 0x02, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
23763c3ecd9SLinus Walleij XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
23863c3ecd9SLinus Walleij /* Used for battery ID or battery temperature */
23963c3ecd9SLinus Walleij XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV2),
24063c3ecd9SLinus Walleij XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
24163c3ecd9SLinus Walleij XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
24263c3ecd9SLinus Walleij XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
24363c3ecd9SLinus Walleij { }, /* Sentinel */
24463c3ecd9SLinus Walleij };
24563c3ecd9SLinus Walleij
24663c3ecd9SLinus Walleij /*
24763c3ecd9SLinus Walleij * Taken from arch/arm/mach-msm/board-8930-pmic.c in the vendor tree:
24863c3ecd9SLinus Walleij * TODO: needs testing.
24963c3ecd9SLinus Walleij */
25063c3ecd9SLinus Walleij static const struct xoadc_channel pm8038_xoadc_channels[] = {
25163c3ecd9SLinus Walleij XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
25263c3ecd9SLinus Walleij XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
25363c3ecd9SLinus Walleij XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
25463c3ecd9SLinus Walleij XOADC_CHAN(ICHG, 0x00, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
25563c3ecd9SLinus Walleij XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
25663c3ecd9SLinus Walleij XOADC_CHAN(AMUX5, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
25763c3ecd9SLinus Walleij XOADC_CHAN(AMUX6, 0x00, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
25863c3ecd9SLinus Walleij XOADC_CHAN(AMUX7, 0x00, 0x07, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
25963c3ecd9SLinus Walleij /* AMUX8 used for battery temperature in most cases */
26063c3ecd9SLinus Walleij XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_TEMP, 1, 1, SCALE_THERM_100K_PULLUP, AMUX_RSV2),
26163c3ecd9SLinus Walleij XOADC_CHAN(AMUX9, 0x00, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
26263c3ecd9SLinus Walleij XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 4, SCALE_DEFAULT, AMUX_RSV1),
26363c3ecd9SLinus Walleij XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
26463c3ecd9SLinus Walleij XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
26563c3ecd9SLinus Walleij XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
26663c3ecd9SLinus Walleij XOADC_CHAN(INTERNAL_2, 0x00, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
26763c3ecd9SLinus Walleij XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
26863c3ecd9SLinus Walleij { }, /* Sentinel */
26963c3ecd9SLinus Walleij };
27063c3ecd9SLinus Walleij
27163c3ecd9SLinus Walleij /*
27263c3ecd9SLinus Walleij * This was created by cross-referencing the vendor tree
27363c3ecd9SLinus Walleij * arch/arm/mach-msm/board-msm8x60.c msm_adc_channels_data[]
27463c3ecd9SLinus Walleij * with the "channel types" (first field) to find the right
27563c3ecd9SLinus Walleij * configuration for these channels on an MSM8x60 i.e. PM8058
27663c3ecd9SLinus Walleij * setup.
27763c3ecd9SLinus Walleij */
27863c3ecd9SLinus Walleij static const struct xoadc_channel pm8058_xoadc_channels[] = {
27963c3ecd9SLinus Walleij XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
28063c3ecd9SLinus Walleij XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
28163c3ecd9SLinus Walleij XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 10, SCALE_DEFAULT, AMUX_RSV1),
28263c3ecd9SLinus Walleij XOADC_CHAN(ICHG, 0x00, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
28363c3ecd9SLinus Walleij XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
28463c3ecd9SLinus Walleij /*
28563c3ecd9SLinus Walleij * AMUX channels 5 thru 9 are referred to as MPP5 thru MPP9 in
28663c3ecd9SLinus Walleij * some code and documentation. But they are really just 5
28763c3ecd9SLinus Walleij * channels just like any other. They are connected to a switching
28863c3ecd9SLinus Walleij * matrix where they can be routed to any of the MPPs, not just
28963c3ecd9SLinus Walleij * 1-to-1 onto MPP5 thru 9, so naming them MPP5 thru MPP9 is
29063c3ecd9SLinus Walleij * very confusing.
29163c3ecd9SLinus Walleij */
29263c3ecd9SLinus Walleij XOADC_CHAN(AMUX5, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
29363c3ecd9SLinus Walleij XOADC_CHAN(AMUX6, 0x00, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
29463c3ecd9SLinus Walleij XOADC_CHAN(AMUX7, 0x00, 0x07, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
29563c3ecd9SLinus Walleij XOADC_CHAN(AMUX8, 0x00, 0x08, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
29663c3ecd9SLinus Walleij XOADC_CHAN(AMUX9, 0x00, 0x09, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
29763c3ecd9SLinus Walleij XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
29863c3ecd9SLinus Walleij XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
29963c3ecd9SLinus Walleij XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
30063c3ecd9SLinus Walleij XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
30163c3ecd9SLinus Walleij XOADC_CHAN(INTERNAL_2, 0x00, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
30263c3ecd9SLinus Walleij XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
30363c3ecd9SLinus Walleij /* There are also "unity" and divided by 3 channels (prescaler) but noone is using them */
30463c3ecd9SLinus Walleij { }, /* Sentinel */
30563c3ecd9SLinus Walleij };
30663c3ecd9SLinus Walleij
30763c3ecd9SLinus Walleij /*
30863c3ecd9SLinus Walleij * The PM8921 has some pre-muxing on its channels, this comes from the vendor tree
30963c3ecd9SLinus Walleij * include/linux/mfd/pm8xxx/pm8xxx-adc.h
31063c3ecd9SLinus Walleij * board-flo-pmic.c (Nexus 7) and board-8064-pmic.c
31163c3ecd9SLinus Walleij */
31263c3ecd9SLinus Walleij static const struct xoadc_channel pm8921_xoadc_channels[] = {
31363c3ecd9SLinus Walleij XOADC_CHAN(VCOIN, 0x00, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
31463c3ecd9SLinus Walleij XOADC_CHAN(VBAT, 0x00, 0x01, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
31563c3ecd9SLinus Walleij XOADC_CHAN(DCIN, 0x00, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
31663c3ecd9SLinus Walleij /* channel "ICHG" is reserved and not used on PM8921 */
31763c3ecd9SLinus Walleij XOADC_CHAN(VPH_PWR, 0x00, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
31863c3ecd9SLinus Walleij XOADC_CHAN(IBAT, 0x00, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
31963c3ecd9SLinus Walleij /* CHAN 6 & 7 (MPP1 & MPP2) are reserved for MPP channels on PM8921 */
32063c3ecd9SLinus Walleij XOADC_CHAN(BATT_THERM, 0x00, 0x08, IIO_TEMP, 1, 1, SCALE_THERM_100K_PULLUP, AMUX_RSV1),
32163c3ecd9SLinus Walleij XOADC_CHAN(BATT_ID, 0x00, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
32263c3ecd9SLinus Walleij XOADC_CHAN(USB_VBUS, 0x00, 0x0a, IIO_VOLTAGE, 1, 4, SCALE_DEFAULT, AMUX_RSV1),
32363c3ecd9SLinus Walleij XOADC_CHAN(DIE_TEMP, 0x00, 0x0b, IIO_TEMP, 1, 1, SCALE_PMIC_THERM, AMUX_RSV1),
32463c3ecd9SLinus Walleij XOADC_CHAN(INTERNAL, 0x00, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
32563c3ecd9SLinus Walleij XOADC_CHAN(125V, 0x00, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
32663c3ecd9SLinus Walleij /* FIXME: look into the scaling of this temperature */
32763c3ecd9SLinus Walleij XOADC_CHAN(CHG_TEMP, 0x00, 0x0e, IIO_TEMP, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
32863c3ecd9SLinus Walleij XOADC_CHAN(MUXOFF, 0x00, 0x0f, IIO_TEMP, 1, 1, SCALE_XOTHERM, AMUX_RSV0),
32963c3ecd9SLinus Walleij /* The following channels have premux bit 0 set to 1 (all end in 4) */
33063c3ecd9SLinus Walleij XOADC_CHAN(ATEST_8, 0x01, 0x00, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
33163c3ecd9SLinus Walleij /* Set scaling to 1/2 based on the name for these two */
33263c3ecd9SLinus Walleij XOADC_CHAN(USB_SNS_DIV20, 0x01, 0x01, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
33363c3ecd9SLinus Walleij XOADC_CHAN(DCIN_SNS_DIV20, 0x01, 0x02, IIO_VOLTAGE, 1, 2, SCALE_DEFAULT, AMUX_RSV1),
33463c3ecd9SLinus Walleij XOADC_CHAN(AMUX3, 0x01, 0x03, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
33563c3ecd9SLinus Walleij XOADC_CHAN(AMUX4, 0x01, 0x04, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
33663c3ecd9SLinus Walleij XOADC_CHAN(AMUX5, 0x01, 0x05, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
33763c3ecd9SLinus Walleij XOADC_CHAN(AMUX6, 0x01, 0x06, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
33863c3ecd9SLinus Walleij XOADC_CHAN(AMUX7, 0x01, 0x07, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
33963c3ecd9SLinus Walleij XOADC_CHAN(AMUX8, 0x01, 0x08, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
34063c3ecd9SLinus Walleij /* Internal test signals, I think */
34163c3ecd9SLinus Walleij XOADC_CHAN(ATEST_1, 0x01, 0x09, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
34263c3ecd9SLinus Walleij XOADC_CHAN(ATEST_2, 0x01, 0x0a, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
34363c3ecd9SLinus Walleij XOADC_CHAN(ATEST_3, 0x01, 0x0b, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
34463c3ecd9SLinus Walleij XOADC_CHAN(ATEST_4, 0x01, 0x0c, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
34563c3ecd9SLinus Walleij XOADC_CHAN(ATEST_5, 0x01, 0x0d, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
34663c3ecd9SLinus Walleij XOADC_CHAN(ATEST_6, 0x01, 0x0e, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
34763c3ecd9SLinus Walleij XOADC_CHAN(ATEST_7, 0x01, 0x0f, IIO_VOLTAGE, 1, 1, SCALE_DEFAULT, AMUX_RSV1),
34863c3ecd9SLinus Walleij /* The following channels have premux bit 1 set to 1 (all end in 8) */
34963c3ecd9SLinus Walleij /* I guess even ATEST8 will be divided by 3 here */
35063c3ecd9SLinus Walleij XOADC_CHAN(ATEST_8, 0x02, 0x00, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
35163c3ecd9SLinus Walleij /* I guess div 2 div 3 becomes div 6 */
35263c3ecd9SLinus Walleij XOADC_CHAN(USB_SNS_DIV20_DIV3, 0x02, 0x01, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
35363c3ecd9SLinus Walleij XOADC_CHAN(DCIN_SNS_DIV20_DIV3, 0x02, 0x02, IIO_VOLTAGE, 1, 6, SCALE_DEFAULT, AMUX_RSV1),
35463c3ecd9SLinus Walleij XOADC_CHAN(AMUX3_DIV3, 0x02, 0x03, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
35563c3ecd9SLinus Walleij XOADC_CHAN(AMUX4_DIV3, 0x02, 0x04, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
35663c3ecd9SLinus Walleij XOADC_CHAN(AMUX5_DIV3, 0x02, 0x05, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
35763c3ecd9SLinus Walleij XOADC_CHAN(AMUX6_DIV3, 0x02, 0x06, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
35863c3ecd9SLinus Walleij XOADC_CHAN(AMUX7_DIV3, 0x02, 0x07, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
35963c3ecd9SLinus Walleij XOADC_CHAN(AMUX8_DIV3, 0x02, 0x08, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
36063c3ecd9SLinus Walleij XOADC_CHAN(ATEST_1_DIV3, 0x02, 0x09, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
36163c3ecd9SLinus Walleij XOADC_CHAN(ATEST_2_DIV3, 0x02, 0x0a, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
36263c3ecd9SLinus Walleij XOADC_CHAN(ATEST_3_DIV3, 0x02, 0x0b, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
36363c3ecd9SLinus Walleij XOADC_CHAN(ATEST_4_DIV3, 0x02, 0x0c, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
36463c3ecd9SLinus Walleij XOADC_CHAN(ATEST_5_DIV3, 0x02, 0x0d, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
36563c3ecd9SLinus Walleij XOADC_CHAN(ATEST_6_DIV3, 0x02, 0x0e, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
36663c3ecd9SLinus Walleij XOADC_CHAN(ATEST_7_DIV3, 0x02, 0x0f, IIO_VOLTAGE, 1, 3, SCALE_DEFAULT, AMUX_RSV1),
36763c3ecd9SLinus Walleij { }, /* Sentinel */
36863c3ecd9SLinus Walleij };
36963c3ecd9SLinus Walleij
37063c3ecd9SLinus Walleij /**
37163c3ecd9SLinus Walleij * struct pm8xxx_chan_info - ADC channel information
37263c3ecd9SLinus Walleij * @name: name of this channel
37363c3ecd9SLinus Walleij * @hwchan: pointer to hardware channel information (muxing & scaling settings)
37463c3ecd9SLinus Walleij * @calibration: whether to use absolute or ratiometric calibration
37563c3ecd9SLinus Walleij * @scale_fn_type: scaling function type
37663c3ecd9SLinus Walleij * @decimation: 0,1,2,3
37763c3ecd9SLinus Walleij * @amux_ip_rsv: ratiometric scale value if using ratiometric
37863c3ecd9SLinus Walleij * calibration: 0, 1, 2, 4, 5.
37963c3ecd9SLinus Walleij */
38063c3ecd9SLinus Walleij struct pm8xxx_chan_info {
38163c3ecd9SLinus Walleij const char *name;
38263c3ecd9SLinus Walleij const struct xoadc_channel *hwchan;
38363c3ecd9SLinus Walleij enum vadc_calibration calibration;
38463c3ecd9SLinus Walleij u8 decimation:2;
38563c3ecd9SLinus Walleij u8 amux_ip_rsv:3;
38663c3ecd9SLinus Walleij };
38763c3ecd9SLinus Walleij
38863c3ecd9SLinus Walleij /**
38963c3ecd9SLinus Walleij * struct pm8xxx_xoadc - state container for the XOADC
39063c3ecd9SLinus Walleij * @dev: pointer to device
39163c3ecd9SLinus Walleij * @map: regmap to access registers
3928f0f0f2cSLee Jones * @variant: XOADC variant characteristics
39363c3ecd9SLinus Walleij * @vref: reference voltage regulator
39463c3ecd9SLinus Walleij * characteristics of the channels, and sensible default settings
39563c3ecd9SLinus Walleij * @nchans: number of channels, configured by the device tree
39663c3ecd9SLinus Walleij * @chans: the channel information per-channel, configured by the device tree
39763c3ecd9SLinus Walleij * @iio_chans: IIO channel specifiers
39863c3ecd9SLinus Walleij * @graph: linear calibration parameters for absolute and
39963c3ecd9SLinus Walleij * ratiometric measurements
40063c3ecd9SLinus Walleij * @complete: completion to indicate end of conversion
40163c3ecd9SLinus Walleij * @lock: lock to restrict access to the hardware to one client at the time
40263c3ecd9SLinus Walleij */
40363c3ecd9SLinus Walleij struct pm8xxx_xoadc {
40463c3ecd9SLinus Walleij struct device *dev;
40563c3ecd9SLinus Walleij struct regmap *map;
40663c3ecd9SLinus Walleij const struct xoadc_variant *variant;
40763c3ecd9SLinus Walleij struct regulator *vref;
40863c3ecd9SLinus Walleij unsigned int nchans;
40963c3ecd9SLinus Walleij struct pm8xxx_chan_info *chans;
41063c3ecd9SLinus Walleij struct iio_chan_spec *iio_chans;
41163c3ecd9SLinus Walleij struct vadc_linear_graph graph[2];
41263c3ecd9SLinus Walleij struct completion complete;
41363c3ecd9SLinus Walleij struct mutex lock;
41463c3ecd9SLinus Walleij };
41563c3ecd9SLinus Walleij
pm8xxx_eoc_irq(int irq,void * d)41663c3ecd9SLinus Walleij static irqreturn_t pm8xxx_eoc_irq(int irq, void *d)
41763c3ecd9SLinus Walleij {
41863c3ecd9SLinus Walleij struct iio_dev *indio_dev = d;
41963c3ecd9SLinus Walleij struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
42063c3ecd9SLinus Walleij
42163c3ecd9SLinus Walleij complete(&adc->complete);
42263c3ecd9SLinus Walleij
42363c3ecd9SLinus Walleij return IRQ_HANDLED;
42463c3ecd9SLinus Walleij }
42563c3ecd9SLinus Walleij
42663c3ecd9SLinus Walleij static struct pm8xxx_chan_info *
pm8xxx_get_channel(struct pm8xxx_xoadc * adc,u8 chan)42763c3ecd9SLinus Walleij pm8xxx_get_channel(struct pm8xxx_xoadc *adc, u8 chan)
42863c3ecd9SLinus Walleij {
42963c3ecd9SLinus Walleij int i;
43063c3ecd9SLinus Walleij
43163c3ecd9SLinus Walleij for (i = 0; i < adc->nchans; i++) {
432e0f0ae83SLinus Torvalds struct pm8xxx_chan_info *ch = &adc->chans[i];
43363c3ecd9SLinus Walleij if (ch->hwchan->amux_channel == chan)
43463c3ecd9SLinus Walleij return ch;
43563c3ecd9SLinus Walleij }
436e0f0ae83SLinus Torvalds return NULL;
437e0f0ae83SLinus Torvalds }
43863c3ecd9SLinus Walleij
pm8xxx_read_channel_rsv(struct pm8xxx_xoadc * adc,const struct pm8xxx_chan_info * ch,u8 rsv,u16 * adc_code,bool force_ratiometric)43963c3ecd9SLinus Walleij static int pm8xxx_read_channel_rsv(struct pm8xxx_xoadc *adc,
44063c3ecd9SLinus Walleij const struct pm8xxx_chan_info *ch,
44163c3ecd9SLinus Walleij u8 rsv, u16 *adc_code,
44263c3ecd9SLinus Walleij bool force_ratiometric)
44363c3ecd9SLinus Walleij {
44463c3ecd9SLinus Walleij int ret;
44563c3ecd9SLinus Walleij unsigned int val;
44663c3ecd9SLinus Walleij u8 rsvmask, rsvval;
44763c3ecd9SLinus Walleij u8 lsb, msb;
44863c3ecd9SLinus Walleij
44963c3ecd9SLinus Walleij dev_dbg(adc->dev, "read channel \"%s\", amux %d, prescale/mux: %d, rsv %d\n",
45063c3ecd9SLinus Walleij ch->name, ch->hwchan->amux_channel, ch->hwchan->pre_scale_mux, rsv);
45163c3ecd9SLinus Walleij
45263c3ecd9SLinus Walleij mutex_lock(&adc->lock);
45363c3ecd9SLinus Walleij
45463c3ecd9SLinus Walleij /* Mux in this channel */
45563c3ecd9SLinus Walleij val = ch->hwchan->amux_channel << ADC_AMUX_SEL_SHIFT;
45663c3ecd9SLinus Walleij val |= ch->hwchan->pre_scale_mux << ADC_AMUX_PREMUX_SHIFT;
45763c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_AMUX_CNTRL, val);
45863c3ecd9SLinus Walleij if (ret)
45963c3ecd9SLinus Walleij goto unlock;
46063c3ecd9SLinus Walleij
46163c3ecd9SLinus Walleij /* Set up ratiometric scale value, mask off all bits except these */
46263c3ecd9SLinus Walleij rsvmask = (ADC_ARB_USRP_RSV_RST | ADC_ARB_USRP_RSV_DTEST0 |
46363c3ecd9SLinus Walleij ADC_ARB_USRP_RSV_DTEST1 | ADC_ARB_USRP_RSV_OP);
46463c3ecd9SLinus Walleij if (adc->variant->broken_ratiometric && !force_ratiometric) {
46563c3ecd9SLinus Walleij /*
46663c3ecd9SLinus Walleij * Apparently the PM8058 has some kind of bug which is
46763c3ecd9SLinus Walleij * reflected in the vendor tree drivers/misc/pmix8058-xoadc.c
46863c3ecd9SLinus Walleij * which just hardcodes the RSV selector to SEL1 (0x20) for
46963c3ecd9SLinus Walleij * most cases and SEL0 (0x10) for the MUXOFF channel only.
47063c3ecd9SLinus Walleij * If we force ratiometric (currently only done when attempting
47163c3ecd9SLinus Walleij * to do ratiometric calibration) this doesn't seem to work
47263c3ecd9SLinus Walleij * very well and I suspect ratiometric conversion is simply
47363c3ecd9SLinus Walleij * broken or not supported on the PM8058.
47463c3ecd9SLinus Walleij *
47563c3ecd9SLinus Walleij * Maybe IO_SEL2 doesn't exist on PM8058 and bits 4 & 5 select
47663c3ecd9SLinus Walleij * the mode alone.
47763c3ecd9SLinus Walleij *
47863c3ecd9SLinus Walleij * Some PM8058 register documentation would be nice to get
47963c3ecd9SLinus Walleij * this right.
48063c3ecd9SLinus Walleij */
48163c3ecd9SLinus Walleij if (ch->hwchan->amux_channel == PM8XXX_CHANNEL_MUXOFF)
48263c3ecd9SLinus Walleij rsvval = ADC_ARB_USRP_RSV_IP_SEL0;
48363c3ecd9SLinus Walleij else
48463c3ecd9SLinus Walleij rsvval = ADC_ARB_USRP_RSV_IP_SEL1;
48563c3ecd9SLinus Walleij } else {
48663c3ecd9SLinus Walleij if (rsv == 0xff)
48763c3ecd9SLinus Walleij rsvval = (ch->amux_ip_rsv << ADC_RSV_IP_SEL_SHIFT) |
48863c3ecd9SLinus Walleij ADC_ARB_USRP_RSV_TRM;
48963c3ecd9SLinus Walleij else
49063c3ecd9SLinus Walleij rsvval = (rsv << ADC_RSV_IP_SEL_SHIFT) |
49163c3ecd9SLinus Walleij ADC_ARB_USRP_RSV_TRM;
49263c3ecd9SLinus Walleij }
49363c3ecd9SLinus Walleij
49463c3ecd9SLinus Walleij ret = regmap_update_bits(adc->map,
49563c3ecd9SLinus Walleij ADC_ARB_USRP_RSV,
49663c3ecd9SLinus Walleij ~rsvmask,
49763c3ecd9SLinus Walleij rsvval);
49863c3ecd9SLinus Walleij if (ret)
49963c3ecd9SLinus Walleij goto unlock;
50063c3ecd9SLinus Walleij
50163c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_ANA_PARAM,
50263c3ecd9SLinus Walleij ADC_ARB_USRP_ANA_PARAM_DIS);
50363c3ecd9SLinus Walleij if (ret)
50463c3ecd9SLinus Walleij goto unlock;
50563c3ecd9SLinus Walleij
50663c3ecd9SLinus Walleij /* Decimation factor */
50763c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_DIG_PARAM,
50863c3ecd9SLinus Walleij ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT0 |
50963c3ecd9SLinus Walleij ADC_ARB_USRP_DIG_PARAM_SEL_SHIFT1 |
51063c3ecd9SLinus Walleij ch->decimation << ADC_DIG_PARAM_DEC_SHIFT);
51163c3ecd9SLinus Walleij if (ret)
51263c3ecd9SLinus Walleij goto unlock;
51363c3ecd9SLinus Walleij
51463c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_ANA_PARAM,
51563c3ecd9SLinus Walleij ADC_ARB_USRP_ANA_PARAM_EN);
51663c3ecd9SLinus Walleij if (ret)
51763c3ecd9SLinus Walleij goto unlock;
51863c3ecd9SLinus Walleij
51963c3ecd9SLinus Walleij /* Enable the arbiter, the Qualcomm code does it twice like this */
52063c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
52163c3ecd9SLinus Walleij ADC_ARB_USRP_CNTRL_EN_ARB);
52263c3ecd9SLinus Walleij if (ret)
52363c3ecd9SLinus Walleij goto unlock;
52463c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
52563c3ecd9SLinus Walleij ADC_ARB_USRP_CNTRL_EN_ARB);
52663c3ecd9SLinus Walleij if (ret)
52763c3ecd9SLinus Walleij goto unlock;
52863c3ecd9SLinus Walleij
52963c3ecd9SLinus Walleij
53063c3ecd9SLinus Walleij /* Fire a request! */
53163c3ecd9SLinus Walleij reinit_completion(&adc->complete);
53263c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL,
53363c3ecd9SLinus Walleij ADC_ARB_USRP_CNTRL_EN_ARB |
53463c3ecd9SLinus Walleij ADC_ARB_USRP_CNTRL_REQ);
53563c3ecd9SLinus Walleij if (ret)
53663c3ecd9SLinus Walleij goto unlock;
53763c3ecd9SLinus Walleij
53863c3ecd9SLinus Walleij /* Next the interrupt occurs */
53963c3ecd9SLinus Walleij ret = wait_for_completion_timeout(&adc->complete,
54063c3ecd9SLinus Walleij VADC_CONV_TIME_MAX_US);
54163c3ecd9SLinus Walleij if (!ret) {
54263c3ecd9SLinus Walleij dev_err(adc->dev, "conversion timed out\n");
54363c3ecd9SLinus Walleij ret = -ETIMEDOUT;
54463c3ecd9SLinus Walleij goto unlock;
54563c3ecd9SLinus Walleij }
54663c3ecd9SLinus Walleij
54763c3ecd9SLinus Walleij ret = regmap_read(adc->map, ADC_ARB_USRP_DATA0, &val);
54863c3ecd9SLinus Walleij if (ret)
54963c3ecd9SLinus Walleij goto unlock;
55063c3ecd9SLinus Walleij lsb = val;
55163c3ecd9SLinus Walleij ret = regmap_read(adc->map, ADC_ARB_USRP_DATA1, &val);
55263c3ecd9SLinus Walleij if (ret)
55363c3ecd9SLinus Walleij goto unlock;
55463c3ecd9SLinus Walleij msb = val;
55563c3ecd9SLinus Walleij *adc_code = (msb << 8) | lsb;
55663c3ecd9SLinus Walleij
55763c3ecd9SLinus Walleij /* Turn off the ADC by setting the arbiter to 0 twice */
55863c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, 0);
55963c3ecd9SLinus Walleij if (ret)
56063c3ecd9SLinus Walleij goto unlock;
56163c3ecd9SLinus Walleij ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, 0);
56263c3ecd9SLinus Walleij if (ret)
56363c3ecd9SLinus Walleij goto unlock;
56463c3ecd9SLinus Walleij
56563c3ecd9SLinus Walleij unlock:
56663c3ecd9SLinus Walleij mutex_unlock(&adc->lock);
56763c3ecd9SLinus Walleij return ret;
56863c3ecd9SLinus Walleij }
56963c3ecd9SLinus Walleij
pm8xxx_read_channel(struct pm8xxx_xoadc * adc,const struct pm8xxx_chan_info * ch,u16 * adc_code)57063c3ecd9SLinus Walleij static int pm8xxx_read_channel(struct pm8xxx_xoadc *adc,
57163c3ecd9SLinus Walleij const struct pm8xxx_chan_info *ch,
57263c3ecd9SLinus Walleij u16 *adc_code)
57363c3ecd9SLinus Walleij {
57463c3ecd9SLinus Walleij /*
57563c3ecd9SLinus Walleij * Normally we just use the ratiometric scale value (RSV) predefined
57663c3ecd9SLinus Walleij * for the channel, but during calibration we need to modify this
57763c3ecd9SLinus Walleij * so this wrapper is a helper hiding the more complex version.
57863c3ecd9SLinus Walleij */
57963c3ecd9SLinus Walleij return pm8xxx_read_channel_rsv(adc, ch, 0xff, adc_code, false);
58063c3ecd9SLinus Walleij }
58163c3ecd9SLinus Walleij
pm8xxx_calibrate_device(struct pm8xxx_xoadc * adc)58263c3ecd9SLinus Walleij static int pm8xxx_calibrate_device(struct pm8xxx_xoadc *adc)
58363c3ecd9SLinus Walleij {
58463c3ecd9SLinus Walleij const struct pm8xxx_chan_info *ch;
58563c3ecd9SLinus Walleij u16 read_1250v;
58663c3ecd9SLinus Walleij u16 read_0625v;
58763c3ecd9SLinus Walleij u16 read_nomux_rsv5;
58863c3ecd9SLinus Walleij u16 read_nomux_rsv4;
58963c3ecd9SLinus Walleij int ret;
59063c3ecd9SLinus Walleij
59163c3ecd9SLinus Walleij adc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
59263c3ecd9SLinus Walleij adc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE;
59363c3ecd9SLinus Walleij
59463c3ecd9SLinus Walleij /* Common reference channel calibration */
59563c3ecd9SLinus Walleij ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_125V);
59663c3ecd9SLinus Walleij if (!ch)
59763c3ecd9SLinus Walleij return -ENODEV;
59863c3ecd9SLinus Walleij ret = pm8xxx_read_channel(adc, ch, &read_1250v);
59963c3ecd9SLinus Walleij if (ret) {
60063c3ecd9SLinus Walleij dev_err(adc->dev, "could not read 1.25V reference channel\n");
60163c3ecd9SLinus Walleij return -ENODEV;
60263c3ecd9SLinus Walleij }
60363c3ecd9SLinus Walleij ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_INTERNAL);
60463c3ecd9SLinus Walleij if (!ch)
60563c3ecd9SLinus Walleij return -ENODEV;
60663c3ecd9SLinus Walleij ret = pm8xxx_read_channel(adc, ch, &read_0625v);
60763c3ecd9SLinus Walleij if (ret) {
60863c3ecd9SLinus Walleij dev_err(adc->dev, "could not read 0.625V reference channel\n");
60963c3ecd9SLinus Walleij return -ENODEV;
61063c3ecd9SLinus Walleij }
61163c3ecd9SLinus Walleij if (read_1250v == read_0625v) {
61263c3ecd9SLinus Walleij dev_err(adc->dev, "read same ADC code for 1.25V and 0.625V\n");
61363c3ecd9SLinus Walleij return -ENODEV;
61463c3ecd9SLinus Walleij }
61563c3ecd9SLinus Walleij
61663c3ecd9SLinus Walleij adc->graph[VADC_CALIB_ABSOLUTE].dy = read_1250v - read_0625v;
61763c3ecd9SLinus Walleij adc->graph[VADC_CALIB_ABSOLUTE].gnd = read_0625v;
61863c3ecd9SLinus Walleij
61963c3ecd9SLinus Walleij dev_info(adc->dev, "absolute calibration dx = %d uV, dy = %d units\n",
62063c3ecd9SLinus Walleij VADC_ABSOLUTE_RANGE_UV, adc->graph[VADC_CALIB_ABSOLUTE].dy);
62163c3ecd9SLinus Walleij
62263c3ecd9SLinus Walleij /* Ratiometric calibration */
62363c3ecd9SLinus Walleij ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_MUXOFF);
62463c3ecd9SLinus Walleij if (!ch)
62563c3ecd9SLinus Walleij return -ENODEV;
62663c3ecd9SLinus Walleij ret = pm8xxx_read_channel_rsv(adc, ch, AMUX_RSV5,
62763c3ecd9SLinus Walleij &read_nomux_rsv5, true);
62863c3ecd9SLinus Walleij if (ret) {
62963c3ecd9SLinus Walleij dev_err(adc->dev, "could not read MUXOFF reference channel\n");
63063c3ecd9SLinus Walleij return -ENODEV;
63163c3ecd9SLinus Walleij }
63263c3ecd9SLinus Walleij ret = pm8xxx_read_channel_rsv(adc, ch, AMUX_RSV4,
63363c3ecd9SLinus Walleij &read_nomux_rsv4, true);
63463c3ecd9SLinus Walleij if (ret) {
63563c3ecd9SLinus Walleij dev_err(adc->dev, "could not read MUXOFF reference channel\n");
63663c3ecd9SLinus Walleij return -ENODEV;
63763c3ecd9SLinus Walleij }
63863c3ecd9SLinus Walleij adc->graph[VADC_CALIB_RATIOMETRIC].dy =
63963c3ecd9SLinus Walleij read_nomux_rsv5 - read_nomux_rsv4;
64063c3ecd9SLinus Walleij adc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_nomux_rsv4;
64163c3ecd9SLinus Walleij
64263c3ecd9SLinus Walleij dev_info(adc->dev, "ratiometric calibration dx = %d, dy = %d units\n",
64363c3ecd9SLinus Walleij VADC_RATIOMETRIC_RANGE,
64463c3ecd9SLinus Walleij adc->graph[VADC_CALIB_RATIOMETRIC].dy);
64563c3ecd9SLinus Walleij
64663c3ecd9SLinus Walleij return 0;
64763c3ecd9SLinus Walleij }
64863c3ecd9SLinus Walleij
pm8xxx_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)64963c3ecd9SLinus Walleij static int pm8xxx_read_raw(struct iio_dev *indio_dev,
65063c3ecd9SLinus Walleij struct iio_chan_spec const *chan,
65163c3ecd9SLinus Walleij int *val, int *val2, long mask)
65263c3ecd9SLinus Walleij {
65363c3ecd9SLinus Walleij struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
65463c3ecd9SLinus Walleij const struct pm8xxx_chan_info *ch;
65563c3ecd9SLinus Walleij u16 adc_code;
65663c3ecd9SLinus Walleij int ret;
65763c3ecd9SLinus Walleij
65863c3ecd9SLinus Walleij switch (mask) {
65963c3ecd9SLinus Walleij case IIO_CHAN_INFO_PROCESSED:
66063c3ecd9SLinus Walleij ch = pm8xxx_get_channel(adc, chan->address);
66163c3ecd9SLinus Walleij if (!ch) {
66263c3ecd9SLinus Walleij dev_err(adc->dev, "no such channel %lu\n",
66363c3ecd9SLinus Walleij chan->address);
66463c3ecd9SLinus Walleij return -EINVAL;
66563c3ecd9SLinus Walleij }
66663c3ecd9SLinus Walleij ret = pm8xxx_read_channel(adc, ch, &adc_code);
66763c3ecd9SLinus Walleij if (ret)
66863c3ecd9SLinus Walleij return ret;
66963c3ecd9SLinus Walleij
67063c3ecd9SLinus Walleij ret = qcom_vadc_scale(ch->hwchan->scale_fn_type,
67163c3ecd9SLinus Walleij &adc->graph[ch->calibration],
67263c3ecd9SLinus Walleij &ch->hwchan->prescale,
67363c3ecd9SLinus Walleij (ch->calibration == VADC_CALIB_ABSOLUTE),
67463c3ecd9SLinus Walleij adc_code, val);
67563c3ecd9SLinus Walleij if (ret)
67663c3ecd9SLinus Walleij return ret;
67763c3ecd9SLinus Walleij
67863c3ecd9SLinus Walleij return IIO_VAL_INT;
67963c3ecd9SLinus Walleij case IIO_CHAN_INFO_RAW:
68063c3ecd9SLinus Walleij ch = pm8xxx_get_channel(adc, chan->address);
68163c3ecd9SLinus Walleij if (!ch) {
68263c3ecd9SLinus Walleij dev_err(adc->dev, "no such channel %lu\n",
68363c3ecd9SLinus Walleij chan->address);
68463c3ecd9SLinus Walleij return -EINVAL;
68563c3ecd9SLinus Walleij }
68663c3ecd9SLinus Walleij ret = pm8xxx_read_channel(adc, ch, &adc_code);
68763c3ecd9SLinus Walleij if (ret)
68863c3ecd9SLinus Walleij return ret;
68963c3ecd9SLinus Walleij
69063c3ecd9SLinus Walleij *val = (int)adc_code;
69163c3ecd9SLinus Walleij return IIO_VAL_INT;
69263c3ecd9SLinus Walleij default:
69363c3ecd9SLinus Walleij return -EINVAL;
69463c3ecd9SLinus Walleij }
69563c3ecd9SLinus Walleij }
69663c3ecd9SLinus Walleij
pm8xxx_fwnode_xlate(struct iio_dev * indio_dev,const struct fwnode_reference_args * iiospec)6979e90c177SNuno Sá static int pm8xxx_fwnode_xlate(struct iio_dev *indio_dev,
6989e90c177SNuno Sá const struct fwnode_reference_args *iiospec)
69963c3ecd9SLinus Walleij {
70063c3ecd9SLinus Walleij struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
70163c3ecd9SLinus Walleij u8 pre_scale_mux;
70263c3ecd9SLinus Walleij u8 amux_channel;
70363c3ecd9SLinus Walleij unsigned int i;
70463c3ecd9SLinus Walleij
70563c3ecd9SLinus Walleij /*
70663c3ecd9SLinus Walleij * First cell is prescaler or premux, second cell is analog
70763c3ecd9SLinus Walleij * mux.
70863c3ecd9SLinus Walleij */
7099e90c177SNuno Sá if (iiospec->nargs != 2) {
7109e90c177SNuno Sá dev_err(&indio_dev->dev, "wrong number of arguments for %pfwP need 2 got %d\n",
7119e90c177SNuno Sá iiospec->fwnode,
7129e90c177SNuno Sá iiospec->nargs);
71363c3ecd9SLinus Walleij return -EINVAL;
71463c3ecd9SLinus Walleij }
71563c3ecd9SLinus Walleij pre_scale_mux = (u8)iiospec->args[0];
71663c3ecd9SLinus Walleij amux_channel = (u8)iiospec->args[1];
71763c3ecd9SLinus Walleij dev_dbg(&indio_dev->dev, "pre scale/mux: %02x, amux: %02x\n",
71863c3ecd9SLinus Walleij pre_scale_mux, amux_channel);
71963c3ecd9SLinus Walleij
72063c3ecd9SLinus Walleij /* We need to match exactly on the prescale/premux and channel */
72163c3ecd9SLinus Walleij for (i = 0; i < adc->nchans; i++)
72263c3ecd9SLinus Walleij if (adc->chans[i].hwchan->pre_scale_mux == pre_scale_mux &&
72363c3ecd9SLinus Walleij adc->chans[i].hwchan->amux_channel == amux_channel)
72463c3ecd9SLinus Walleij return i;
72563c3ecd9SLinus Walleij
72663c3ecd9SLinus Walleij return -EINVAL;
72763c3ecd9SLinus Walleij }
72863c3ecd9SLinus Walleij
72963c3ecd9SLinus Walleij static const struct iio_info pm8xxx_xoadc_info = {
7309e90c177SNuno Sá .fwnode_xlate = pm8xxx_fwnode_xlate,
73163c3ecd9SLinus Walleij .read_raw = pm8xxx_read_raw,
73263c3ecd9SLinus Walleij };
73363c3ecd9SLinus Walleij
pm8xxx_xoadc_parse_channel(struct device * dev,struct fwnode_handle * fwnode,const struct xoadc_channel * hw_channels,struct iio_chan_spec * iio_chan,struct pm8xxx_chan_info * ch)73463c3ecd9SLinus Walleij static int pm8xxx_xoadc_parse_channel(struct device *dev,
7359e90c177SNuno Sá struct fwnode_handle *fwnode,
73663c3ecd9SLinus Walleij const struct xoadc_channel *hw_channels,
73763c3ecd9SLinus Walleij struct iio_chan_spec *iio_chan,
73863c3ecd9SLinus Walleij struct pm8xxx_chan_info *ch)
73963c3ecd9SLinus Walleij {
7409e90c177SNuno Sá const char *name = fwnode_get_name(fwnode);
74163c3ecd9SLinus Walleij const struct xoadc_channel *hwchan;
7429e90c177SNuno Sá u32 pre_scale_mux, amux_channel, reg[2];
74363c3ecd9SLinus Walleij u32 rsv, dec;
74463c3ecd9SLinus Walleij int ret;
74563c3ecd9SLinus Walleij int chid;
74663c3ecd9SLinus Walleij
7479e90c177SNuno Sá ret = fwnode_property_read_u32_array(fwnode, "reg", reg,
7489e90c177SNuno Sá ARRAY_SIZE(reg));
74963c3ecd9SLinus Walleij if (ret) {
7509e90c177SNuno Sá dev_err(dev, "invalid pre scale/mux or amux channel number %s\n",
7519e90c177SNuno Sá name);
75263c3ecd9SLinus Walleij return ret;
75363c3ecd9SLinus Walleij }
7549e90c177SNuno Sá
7559e90c177SNuno Sá pre_scale_mux = reg[0];
7569e90c177SNuno Sá amux_channel = reg[1];
75763c3ecd9SLinus Walleij
75863c3ecd9SLinus Walleij /* Find the right channel setting */
75963c3ecd9SLinus Walleij chid = 0;
76063c3ecd9SLinus Walleij hwchan = &hw_channels[0];
761*30da26eaSKasumov Ruslan while (hwchan->datasheet_name) {
76263c3ecd9SLinus Walleij if (hwchan->pre_scale_mux == pre_scale_mux &&
76363c3ecd9SLinus Walleij hwchan->amux_channel == amux_channel)
76463c3ecd9SLinus Walleij break;
76563c3ecd9SLinus Walleij hwchan++;
76663c3ecd9SLinus Walleij chid++;
76763c3ecd9SLinus Walleij }
76863c3ecd9SLinus Walleij /* The sentinel does not have a name assigned */
76963c3ecd9SLinus Walleij if (!hwchan->datasheet_name) {
77063c3ecd9SLinus Walleij dev_err(dev, "could not locate channel %02x/%02x\n",
77163c3ecd9SLinus Walleij pre_scale_mux, amux_channel);
77263c3ecd9SLinus Walleij return -EINVAL;
77363c3ecd9SLinus Walleij }
77463c3ecd9SLinus Walleij ch->name = name;
77563c3ecd9SLinus Walleij ch->hwchan = hwchan;
77663c3ecd9SLinus Walleij /* Everyone seems to use absolute calibration except in special cases */
77763c3ecd9SLinus Walleij ch->calibration = VADC_CALIB_ABSOLUTE;
77863c3ecd9SLinus Walleij /* Everyone seems to use default ("type 2") decimation */
77963c3ecd9SLinus Walleij ch->decimation = VADC_DEF_DECIMATION;
78063c3ecd9SLinus Walleij
7819e90c177SNuno Sá if (!fwnode_property_read_u32(fwnode, "qcom,ratiometric", &rsv)) {
78263c3ecd9SLinus Walleij ch->calibration = VADC_CALIB_RATIOMETRIC;
78363c3ecd9SLinus Walleij if (rsv > XOADC_RSV_MAX) {
78463c3ecd9SLinus Walleij dev_err(dev, "%s too large RSV value %d\n", name, rsv);
78563c3ecd9SLinus Walleij return -EINVAL;
78663c3ecd9SLinus Walleij }
78763c3ecd9SLinus Walleij if (rsv == AMUX_RSV3) {
78863c3ecd9SLinus Walleij dev_err(dev, "%s invalid RSV value %d\n", name, rsv);
78963c3ecd9SLinus Walleij return -EINVAL;
79063c3ecd9SLinus Walleij }
79163c3ecd9SLinus Walleij }
79263c3ecd9SLinus Walleij
79363c3ecd9SLinus Walleij /* Optional decimation, if omitted we use the default */
7949e90c177SNuno Sá ret = fwnode_property_read_u32(fwnode, "qcom,decimation", &dec);
79563c3ecd9SLinus Walleij if (!ret) {
79663c3ecd9SLinus Walleij ret = qcom_vadc_decimation_from_dt(dec);
79763c3ecd9SLinus Walleij if (ret < 0) {
79863c3ecd9SLinus Walleij dev_err(dev, "%s invalid decimation %d\n",
79963c3ecd9SLinus Walleij name, dec);
80063c3ecd9SLinus Walleij return ret;
80163c3ecd9SLinus Walleij }
80263c3ecd9SLinus Walleij ch->decimation = ret;
80363c3ecd9SLinus Walleij }
80463c3ecd9SLinus Walleij
80563c3ecd9SLinus Walleij iio_chan->channel = chid;
80663c3ecd9SLinus Walleij iio_chan->address = hwchan->amux_channel;
80763c3ecd9SLinus Walleij iio_chan->datasheet_name = hwchan->datasheet_name;
80863c3ecd9SLinus Walleij iio_chan->type = hwchan->type;
80963c3ecd9SLinus Walleij /* All channels are raw or processed */
81063c3ecd9SLinus Walleij iio_chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
81163c3ecd9SLinus Walleij BIT(IIO_CHAN_INFO_PROCESSED);
81263c3ecd9SLinus Walleij iio_chan->indexed = 1;
81363c3ecd9SLinus Walleij
814a5e9b2ddSAndy Shevchenko dev_dbg(dev,
815a5e9b2ddSAndy Shevchenko "channel [PRESCALE/MUX: %02x AMUX: %02x] \"%s\" ref voltage: %d, decimation %d prescale %d/%d, scale function %d\n",
81663c3ecd9SLinus Walleij hwchan->pre_scale_mux, hwchan->amux_channel, ch->name,
817a5e9b2ddSAndy Shevchenko ch->amux_ip_rsv, ch->decimation, hwchan->prescale.numerator,
818a5e9b2ddSAndy Shevchenko hwchan->prescale.denominator, hwchan->scale_fn_type);
81963c3ecd9SLinus Walleij
82063c3ecd9SLinus Walleij return 0;
82163c3ecd9SLinus Walleij }
82263c3ecd9SLinus Walleij
pm8xxx_xoadc_parse_channels(struct pm8xxx_xoadc * adc)8239e90c177SNuno Sá static int pm8xxx_xoadc_parse_channels(struct pm8xxx_xoadc *adc)
82463c3ecd9SLinus Walleij {
8259e90c177SNuno Sá struct fwnode_handle *child;
82663c3ecd9SLinus Walleij struct pm8xxx_chan_info *ch;
82763c3ecd9SLinus Walleij int ret;
82863c3ecd9SLinus Walleij int i;
82963c3ecd9SLinus Walleij
8309e90c177SNuno Sá adc->nchans = device_get_child_node_count(adc->dev);
83163c3ecd9SLinus Walleij if (!adc->nchans) {
83263c3ecd9SLinus Walleij dev_err(adc->dev, "no channel children\n");
83363c3ecd9SLinus Walleij return -ENODEV;
83463c3ecd9SLinus Walleij }
83563c3ecd9SLinus Walleij dev_dbg(adc->dev, "found %d ADC channels\n", adc->nchans);
83663c3ecd9SLinus Walleij
83763c3ecd9SLinus Walleij adc->iio_chans = devm_kcalloc(adc->dev, adc->nchans,
83863c3ecd9SLinus Walleij sizeof(*adc->iio_chans), GFP_KERNEL);
83963c3ecd9SLinus Walleij if (!adc->iio_chans)
84063c3ecd9SLinus Walleij return -ENOMEM;
84163c3ecd9SLinus Walleij
84263c3ecd9SLinus Walleij adc->chans = devm_kcalloc(adc->dev, adc->nchans,
84363c3ecd9SLinus Walleij sizeof(*adc->chans), GFP_KERNEL);
84463c3ecd9SLinus Walleij if (!adc->chans)
84563c3ecd9SLinus Walleij return -ENOMEM;
84663c3ecd9SLinus Walleij
84763c3ecd9SLinus Walleij i = 0;
8489e90c177SNuno Sá device_for_each_child_node(adc->dev, child) {
84963c3ecd9SLinus Walleij ch = &adc->chans[i];
85063c3ecd9SLinus Walleij ret = pm8xxx_xoadc_parse_channel(adc->dev, child,
85163c3ecd9SLinus Walleij adc->variant->channels,
85263c3ecd9SLinus Walleij &adc->iio_chans[i],
85363c3ecd9SLinus Walleij ch);
85463c3ecd9SLinus Walleij if (ret) {
8559e90c177SNuno Sá fwnode_handle_put(child);
85663c3ecd9SLinus Walleij return ret;
85763c3ecd9SLinus Walleij }
85863c3ecd9SLinus Walleij i++;
85963c3ecd9SLinus Walleij }
86063c3ecd9SLinus Walleij
86163c3ecd9SLinus Walleij /* Check for required channels */
86263c3ecd9SLinus Walleij ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_125V);
86363c3ecd9SLinus Walleij if (!ch) {
86463c3ecd9SLinus Walleij dev_err(adc->dev, "missing 1.25V reference channel\n");
86563c3ecd9SLinus Walleij return -ENODEV;
86663c3ecd9SLinus Walleij }
86763c3ecd9SLinus Walleij ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_INTERNAL);
86863c3ecd9SLinus Walleij if (!ch) {
86963c3ecd9SLinus Walleij dev_err(adc->dev, "missing 0.625V reference channel\n");
87063c3ecd9SLinus Walleij return -ENODEV;
87163c3ecd9SLinus Walleij }
87263c3ecd9SLinus Walleij ch = pm8xxx_get_channel(adc, PM8XXX_CHANNEL_MUXOFF);
87363c3ecd9SLinus Walleij if (!ch) {
87463c3ecd9SLinus Walleij dev_err(adc->dev, "missing MUXOFF reference channel\n");
87563c3ecd9SLinus Walleij return -ENODEV;
87663c3ecd9SLinus Walleij }
87763c3ecd9SLinus Walleij
87863c3ecd9SLinus Walleij return 0;
87963c3ecd9SLinus Walleij }
88063c3ecd9SLinus Walleij
pm8xxx_xoadc_probe(struct platform_device * pdev)88163c3ecd9SLinus Walleij static int pm8xxx_xoadc_probe(struct platform_device *pdev)
88263c3ecd9SLinus Walleij {
88363c3ecd9SLinus Walleij const struct xoadc_variant *variant;
88463c3ecd9SLinus Walleij struct pm8xxx_xoadc *adc;
88563c3ecd9SLinus Walleij struct iio_dev *indio_dev;
88663c3ecd9SLinus Walleij struct regmap *map;
88763c3ecd9SLinus Walleij struct device *dev = &pdev->dev;
88863c3ecd9SLinus Walleij int ret;
88963c3ecd9SLinus Walleij
8909e90c177SNuno Sá variant = device_get_match_data(dev);
89163c3ecd9SLinus Walleij if (!variant)
89263c3ecd9SLinus Walleij return -ENODEV;
89363c3ecd9SLinus Walleij
89463c3ecd9SLinus Walleij indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
89563c3ecd9SLinus Walleij if (!indio_dev)
89663c3ecd9SLinus Walleij return -ENOMEM;
89763c3ecd9SLinus Walleij platform_set_drvdata(pdev, indio_dev);
89863c3ecd9SLinus Walleij
89963c3ecd9SLinus Walleij adc = iio_priv(indio_dev);
90063c3ecd9SLinus Walleij adc->dev = dev;
90163c3ecd9SLinus Walleij adc->variant = variant;
90263c3ecd9SLinus Walleij init_completion(&adc->complete);
90363c3ecd9SLinus Walleij mutex_init(&adc->lock);
90463c3ecd9SLinus Walleij
9059e90c177SNuno Sá ret = pm8xxx_xoadc_parse_channels(adc);
90663c3ecd9SLinus Walleij if (ret)
90763c3ecd9SLinus Walleij return ret;
90863c3ecd9SLinus Walleij
90963c3ecd9SLinus Walleij map = dev_get_regmap(dev->parent, NULL);
91063c3ecd9SLinus Walleij if (!map) {
91163c3ecd9SLinus Walleij dev_err(dev, "parent regmap unavailable.\n");
91294f08a06SCai Huoqing return -ENODEV;
91363c3ecd9SLinus Walleij }
91463c3ecd9SLinus Walleij adc->map = map;
91563c3ecd9SLinus Walleij
91663c3ecd9SLinus Walleij /* Bring up regulator */
91763c3ecd9SLinus Walleij adc->vref = devm_regulator_get(dev, "xoadc-ref");
91894f08a06SCai Huoqing if (IS_ERR(adc->vref))
91994f08a06SCai Huoqing return dev_err_probe(dev, PTR_ERR(adc->vref),
92094f08a06SCai Huoqing "failed to get XOADC VREF regulator\n");
92163c3ecd9SLinus Walleij ret = regulator_enable(adc->vref);
92263c3ecd9SLinus Walleij if (ret) {
92363c3ecd9SLinus Walleij dev_err(dev, "failed to enable XOADC VREF regulator\n");
92463c3ecd9SLinus Walleij return ret;
92563c3ecd9SLinus Walleij }
92663c3ecd9SLinus Walleij
92763c3ecd9SLinus Walleij ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
92863c3ecd9SLinus Walleij pm8xxx_eoc_irq, NULL, 0, variant->name, indio_dev);
92963c3ecd9SLinus Walleij if (ret) {
93063c3ecd9SLinus Walleij dev_err(dev, "unable to request IRQ\n");
93163c3ecd9SLinus Walleij goto out_disable_vref;
93263c3ecd9SLinus Walleij }
93363c3ecd9SLinus Walleij
93463c3ecd9SLinus Walleij indio_dev->name = variant->name;
93563c3ecd9SLinus Walleij indio_dev->modes = INDIO_DIRECT_MODE;
93663c3ecd9SLinus Walleij indio_dev->info = &pm8xxx_xoadc_info;
93763c3ecd9SLinus Walleij indio_dev->channels = adc->iio_chans;
93863c3ecd9SLinus Walleij indio_dev->num_channels = adc->nchans;
93963c3ecd9SLinus Walleij
94063c3ecd9SLinus Walleij ret = iio_device_register(indio_dev);
94163c3ecd9SLinus Walleij if (ret)
94263c3ecd9SLinus Walleij goto out_disable_vref;
94363c3ecd9SLinus Walleij
94463c3ecd9SLinus Walleij ret = pm8xxx_calibrate_device(adc);
94563c3ecd9SLinus Walleij if (ret)
94663c3ecd9SLinus Walleij goto out_unreg_device;
94763c3ecd9SLinus Walleij
94863c3ecd9SLinus Walleij dev_info(dev, "%s XOADC driver enabled\n", variant->name);
94963c3ecd9SLinus Walleij
95063c3ecd9SLinus Walleij return 0;
95163c3ecd9SLinus Walleij
95263c3ecd9SLinus Walleij out_unreg_device:
95363c3ecd9SLinus Walleij iio_device_unregister(indio_dev);
95463c3ecd9SLinus Walleij out_disable_vref:
95563c3ecd9SLinus Walleij regulator_disable(adc->vref);
95663c3ecd9SLinus Walleij
95763c3ecd9SLinus Walleij return ret;
95863c3ecd9SLinus Walleij }
95963c3ecd9SLinus Walleij
pm8xxx_xoadc_remove(struct platform_device * pdev)96063c3ecd9SLinus Walleij static int pm8xxx_xoadc_remove(struct platform_device *pdev)
96163c3ecd9SLinus Walleij {
96263c3ecd9SLinus Walleij struct iio_dev *indio_dev = platform_get_drvdata(pdev);
96363c3ecd9SLinus Walleij struct pm8xxx_xoadc *adc = iio_priv(indio_dev);
96463c3ecd9SLinus Walleij
96563c3ecd9SLinus Walleij iio_device_unregister(indio_dev);
96663c3ecd9SLinus Walleij
96763c3ecd9SLinus Walleij regulator_disable(adc->vref);
96863c3ecd9SLinus Walleij
96963c3ecd9SLinus Walleij return 0;
97063c3ecd9SLinus Walleij }
97163c3ecd9SLinus Walleij
97263c3ecd9SLinus Walleij static const struct xoadc_variant pm8018_variant = {
97363c3ecd9SLinus Walleij .name = "PM8018-XOADC",
97463c3ecd9SLinus Walleij .channels = pm8018_xoadc_channels,
97563c3ecd9SLinus Walleij };
97663c3ecd9SLinus Walleij
97763c3ecd9SLinus Walleij static const struct xoadc_variant pm8038_variant = {
97863c3ecd9SLinus Walleij .name = "PM8038-XOADC",
97963c3ecd9SLinus Walleij .channels = pm8038_xoadc_channels,
98063c3ecd9SLinus Walleij };
98163c3ecd9SLinus Walleij
98263c3ecd9SLinus Walleij static const struct xoadc_variant pm8058_variant = {
98363c3ecd9SLinus Walleij .name = "PM8058-XOADC",
98463c3ecd9SLinus Walleij .channels = pm8058_xoadc_channels,
98563c3ecd9SLinus Walleij .broken_ratiometric = true,
98663c3ecd9SLinus Walleij .prescaling = true,
98763c3ecd9SLinus Walleij };
98863c3ecd9SLinus Walleij
98963c3ecd9SLinus Walleij static const struct xoadc_variant pm8921_variant = {
99063c3ecd9SLinus Walleij .name = "PM8921-XOADC",
99163c3ecd9SLinus Walleij .channels = pm8921_xoadc_channels,
99263c3ecd9SLinus Walleij .second_level_mux = true,
99363c3ecd9SLinus Walleij };
99463c3ecd9SLinus Walleij
99563c3ecd9SLinus Walleij static const struct of_device_id pm8xxx_xoadc_id_table[] = {
99663c3ecd9SLinus Walleij {
99763c3ecd9SLinus Walleij .compatible = "qcom,pm8018-adc",
99863c3ecd9SLinus Walleij .data = &pm8018_variant,
99963c3ecd9SLinus Walleij },
100063c3ecd9SLinus Walleij {
100163c3ecd9SLinus Walleij .compatible = "qcom,pm8038-adc",
100263c3ecd9SLinus Walleij .data = &pm8038_variant,
100363c3ecd9SLinus Walleij },
100463c3ecd9SLinus Walleij {
100563c3ecd9SLinus Walleij .compatible = "qcom,pm8058-adc",
100663c3ecd9SLinus Walleij .data = &pm8058_variant,
100763c3ecd9SLinus Walleij },
100863c3ecd9SLinus Walleij {
100963c3ecd9SLinus Walleij .compatible = "qcom,pm8921-adc",
101063c3ecd9SLinus Walleij .data = &pm8921_variant,
101163c3ecd9SLinus Walleij },
101263c3ecd9SLinus Walleij { },
101363c3ecd9SLinus Walleij };
101463c3ecd9SLinus Walleij MODULE_DEVICE_TABLE(of, pm8xxx_xoadc_id_table);
101563c3ecd9SLinus Walleij
101663c3ecd9SLinus Walleij static struct platform_driver pm8xxx_xoadc_driver = {
101763c3ecd9SLinus Walleij .driver = {
101863c3ecd9SLinus Walleij .name = "pm8xxx-adc",
101963c3ecd9SLinus Walleij .of_match_table = pm8xxx_xoadc_id_table,
102063c3ecd9SLinus Walleij },
102163c3ecd9SLinus Walleij .probe = pm8xxx_xoadc_probe,
102263c3ecd9SLinus Walleij .remove = pm8xxx_xoadc_remove,
102363c3ecd9SLinus Walleij };
102463c3ecd9SLinus Walleij module_platform_driver(pm8xxx_xoadc_driver);
102563c3ecd9SLinus Walleij
102663c3ecd9SLinus Walleij MODULE_DESCRIPTION("PM8xxx XOADC driver");
102763c3ecd9SLinus Walleij MODULE_LICENSE("GPL v2");
102863c3ecd9SLinus Walleij MODULE_ALIAS("platform:pm8xxx-xoadc");
1029