cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

qcom-spmi-vadc.c (23841B)


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