xref: /linux/drivers/iio/adc/ti-lmp92064.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
162719894SLeonard Göhrs // SPDX-License-Identifier: GPL-2.0
262719894SLeonard Göhrs /*
362719894SLeonard Göhrs  * Texas Instruments LMP92064 SPI ADC driver
462719894SLeonard Göhrs  *
562719894SLeonard Göhrs  * Copyright (c) 2022 Leonard Göhrs <kernel@pengutronix.de>, Pengutronix
662719894SLeonard Göhrs  *
762719894SLeonard Göhrs  * Based on linux/drivers/iio/adc/ti-tsc2046.c
862719894SLeonard Göhrs  * Copyright (c) 2021 Oleksij Rempel <kernel@pengutronix.de>, Pengutronix
962719894SLeonard Göhrs  */
1062719894SLeonard Göhrs 
1162719894SLeonard Göhrs #include <linux/delay.h>
1262719894SLeonard Göhrs #include <linux/gpio/consumer.h>
1362719894SLeonard Göhrs #include <linux/module.h>
1462719894SLeonard Göhrs #include <linux/regmap.h>
1562719894SLeonard Göhrs #include <linux/regulator/consumer.h>
1662719894SLeonard Göhrs #include <linux/spi/spi.h>
1762719894SLeonard Göhrs 
1862719894SLeonard Göhrs #include <linux/iio/iio.h>
19*6c7bc1d2SLeonard Göhrs #include <linux/iio/buffer.h>
2062719894SLeonard Göhrs #include <linux/iio/driver.h>
21*6c7bc1d2SLeonard Göhrs #include <linux/iio/triggered_buffer.h>
22*6c7bc1d2SLeonard Göhrs #include <linux/iio/trigger_consumer.h>
2362719894SLeonard Göhrs 
2462719894SLeonard Göhrs #define TI_LMP92064_REG_CONFIG_A 0x0000
2562719894SLeonard Göhrs #define TI_LMP92064_REG_CONFIG_B 0x0001
2662719894SLeonard Göhrs #define TI_LMP92064_REG_CHIP_REV 0x0006
2762719894SLeonard Göhrs 
2862719894SLeonard Göhrs #define TI_LMP92064_REG_MFR_ID1 0x000C
2962719894SLeonard Göhrs #define TI_LMP92064_REG_MFR_ID2 0x000D
3062719894SLeonard Göhrs 
3162719894SLeonard Göhrs #define TI_LMP92064_REG_REG_UPDATE 0x000F
3262719894SLeonard Göhrs #define TI_LMP92064_REG_CONFIG_REG 0x0100
3362719894SLeonard Göhrs #define TI_LMP92064_REG_STATUS 0x0103
3462719894SLeonard Göhrs 
3562719894SLeonard Göhrs #define TI_LMP92064_REG_DATA_VOUT_LSB 0x0200
3662719894SLeonard Göhrs #define TI_LMP92064_REG_DATA_VOUT_MSB 0x0201
3762719894SLeonard Göhrs #define TI_LMP92064_REG_DATA_COUT_LSB 0x0202
3862719894SLeonard Göhrs #define TI_LMP92064_REG_DATA_COUT_MSB 0x0203
3962719894SLeonard Göhrs 
4062719894SLeonard Göhrs #define TI_LMP92064_VAL_CONFIG_A 0x99
4162719894SLeonard Göhrs #define TI_LMP92064_VAL_CONFIG_B 0x00
4262719894SLeonard Göhrs #define TI_LMP92064_VAL_STATUS_OK 0x01
4362719894SLeonard Göhrs 
4462719894SLeonard Göhrs /*
4562719894SLeonard Göhrs  * Channel number definitions for the two channels of the device
4662719894SLeonard Göhrs  * - IN Current (INC)
4762719894SLeonard Göhrs  * - IN Voltage (INV)
4862719894SLeonard Göhrs  */
4962719894SLeonard Göhrs #define TI_LMP92064_CHAN_INC 0
5062719894SLeonard Göhrs #define TI_LMP92064_CHAN_INV 1
5162719894SLeonard Göhrs 
5262719894SLeonard Göhrs static const struct regmap_range lmp92064_readable_reg_ranges[] = {
5362719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_CONFIG_A, TI_LMP92064_REG_CHIP_REV),
5462719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_MFR_ID1, TI_LMP92064_REG_MFR_ID2),
5562719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_REG_UPDATE, TI_LMP92064_REG_REG_UPDATE),
5662719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_CONFIG_REG, TI_LMP92064_REG_CONFIG_REG),
5762719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_STATUS, TI_LMP92064_REG_STATUS),
5862719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_DATA_VOUT_LSB, TI_LMP92064_REG_DATA_COUT_MSB),
5962719894SLeonard Göhrs };
6062719894SLeonard Göhrs 
6162719894SLeonard Göhrs static const struct regmap_access_table lmp92064_readable_regs = {
6262719894SLeonard Göhrs 	.yes_ranges = lmp92064_readable_reg_ranges,
6362719894SLeonard Göhrs 	.n_yes_ranges = ARRAY_SIZE(lmp92064_readable_reg_ranges),
6462719894SLeonard Göhrs };
6562719894SLeonard Göhrs 
6662719894SLeonard Göhrs static const struct regmap_range lmp92064_writable_reg_ranges[] = {
6762719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_CONFIG_A, TI_LMP92064_REG_CONFIG_B),
6862719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_REG_UPDATE, TI_LMP92064_REG_REG_UPDATE),
6962719894SLeonard Göhrs 	regmap_reg_range(TI_LMP92064_REG_CONFIG_REG, TI_LMP92064_REG_CONFIG_REG),
7062719894SLeonard Göhrs };
7162719894SLeonard Göhrs 
7262719894SLeonard Göhrs static const struct regmap_access_table lmp92064_writable_regs = {
7362719894SLeonard Göhrs 	.yes_ranges = lmp92064_writable_reg_ranges,
7462719894SLeonard Göhrs 	.n_yes_ranges = ARRAY_SIZE(lmp92064_writable_reg_ranges),
7562719894SLeonard Göhrs };
7662719894SLeonard Göhrs 
7762719894SLeonard Göhrs static const struct regmap_config lmp92064_spi_regmap_config = {
7862719894SLeonard Göhrs 	.reg_bits = 16,
7962719894SLeonard Göhrs 	.val_bits = 8,
8062719894SLeonard Göhrs 	.max_register = TI_LMP92064_REG_DATA_COUT_MSB,
8162719894SLeonard Göhrs 	.rd_table = &lmp92064_readable_regs,
8262719894SLeonard Göhrs 	.wr_table = &lmp92064_writable_regs,
8362719894SLeonard Göhrs };
8462719894SLeonard Göhrs 
8562719894SLeonard Göhrs struct lmp92064_adc_priv {
8662719894SLeonard Göhrs 	int shunt_resistor_uohm;
8762719894SLeonard Göhrs 	struct spi_device *spi;
8862719894SLeonard Göhrs 	struct regmap *regmap;
8962719894SLeonard Göhrs };
9062719894SLeonard Göhrs 
9162719894SLeonard Göhrs static const struct iio_chan_spec lmp92064_adc_channels[] = {
9262719894SLeonard Göhrs 	{
9362719894SLeonard Göhrs 		.type = IIO_CURRENT,
9462719894SLeonard Göhrs 		.address = TI_LMP92064_CHAN_INC,
9562719894SLeonard Göhrs 		.info_mask_separate =
9662719894SLeonard Göhrs 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
97*6c7bc1d2SLeonard Göhrs 		.scan_index = TI_LMP92064_CHAN_INC,
98*6c7bc1d2SLeonard Göhrs 		.scan_type = {
99*6c7bc1d2SLeonard Göhrs 			.sign = 'u',
100*6c7bc1d2SLeonard Göhrs 			.realbits = 12,
101*6c7bc1d2SLeonard Göhrs 			.storagebits = 16,
102*6c7bc1d2SLeonard Göhrs 		},
10362719894SLeonard Göhrs 		.datasheet_name = "INC",
10462719894SLeonard Göhrs 	},
10562719894SLeonard Göhrs 	{
10662719894SLeonard Göhrs 		.type = IIO_VOLTAGE,
10762719894SLeonard Göhrs 		.address = TI_LMP92064_CHAN_INV,
10862719894SLeonard Göhrs 		.info_mask_separate =
10962719894SLeonard Göhrs 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
110*6c7bc1d2SLeonard Göhrs 		.scan_index = TI_LMP92064_CHAN_INV,
111*6c7bc1d2SLeonard Göhrs 		.scan_type = {
112*6c7bc1d2SLeonard Göhrs 			.sign = 'u',
113*6c7bc1d2SLeonard Göhrs 			.realbits = 12,
114*6c7bc1d2SLeonard Göhrs 			.storagebits = 16,
115*6c7bc1d2SLeonard Göhrs 		},
11662719894SLeonard Göhrs 		.datasheet_name = "INV",
11762719894SLeonard Göhrs 	},
118*6c7bc1d2SLeonard Göhrs 	IIO_CHAN_SOFT_TIMESTAMP(2),
119*6c7bc1d2SLeonard Göhrs };
120*6c7bc1d2SLeonard Göhrs 
121*6c7bc1d2SLeonard Göhrs static const unsigned long lmp92064_scan_masks[] = {
122*6c7bc1d2SLeonard Göhrs 	BIT(TI_LMP92064_CHAN_INC) | BIT(TI_LMP92064_CHAN_INV),
123*6c7bc1d2SLeonard Göhrs 	0
12462719894SLeonard Göhrs };
12562719894SLeonard Göhrs 
lmp92064_read_meas(struct lmp92064_adc_priv * priv,u16 * res)12662719894SLeonard Göhrs static int lmp92064_read_meas(struct lmp92064_adc_priv *priv, u16 *res)
12762719894SLeonard Göhrs {
12862719894SLeonard Göhrs 	__be16 raw[2];
12962719894SLeonard Göhrs 	int ret;
13062719894SLeonard Göhrs 
13162719894SLeonard Göhrs 	/*
13262719894SLeonard Göhrs 	 * The ADC only latches in new samples if all DATA registers are read
13362719894SLeonard Göhrs 	 * in descending sequential order.
13462719894SLeonard Göhrs 	 * The ADC auto-decrements the register index with each clocked byte.
13562719894SLeonard Göhrs 	 * Read both channels in single SPI transfer by selecting the highest
13662719894SLeonard Göhrs 	 * register using the command below and clocking out all four data
13762719894SLeonard Göhrs 	 * bytes.
13862719894SLeonard Göhrs 	 */
13962719894SLeonard Göhrs 
14062719894SLeonard Göhrs 	ret = regmap_bulk_read(priv->regmap, TI_LMP92064_REG_DATA_COUT_MSB,
14162719894SLeonard Göhrs 			 &raw, sizeof(raw));
14262719894SLeonard Göhrs 
14362719894SLeonard Göhrs 	if (ret) {
14462719894SLeonard Göhrs 		dev_err(&priv->spi->dev, "regmap_bulk_read failed: %pe\n",
14562719894SLeonard Göhrs 			ERR_PTR(ret));
14662719894SLeonard Göhrs 		return ret;
14762719894SLeonard Göhrs 	}
14862719894SLeonard Göhrs 
14962719894SLeonard Göhrs 	res[0] = be16_to_cpu(raw[0]);
15062719894SLeonard Göhrs 	res[1] = be16_to_cpu(raw[1]);
15162719894SLeonard Göhrs 
15262719894SLeonard Göhrs 	return 0;
15362719894SLeonard Göhrs }
15462719894SLeonard Göhrs 
lmp92064_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)15562719894SLeonard Göhrs static int lmp92064_read_raw(struct iio_dev *indio_dev,
15662719894SLeonard Göhrs 			     struct iio_chan_spec const *chan, int *val,
15762719894SLeonard Göhrs 			     int *val2, long mask)
15862719894SLeonard Göhrs {
15962719894SLeonard Göhrs 	struct lmp92064_adc_priv *priv = iio_priv(indio_dev);
16062719894SLeonard Göhrs 	u16 raw[2];
16162719894SLeonard Göhrs 	int ret;
16262719894SLeonard Göhrs 
16362719894SLeonard Göhrs 	switch (mask) {
16462719894SLeonard Göhrs 	case IIO_CHAN_INFO_RAW:
16562719894SLeonard Göhrs 		ret = lmp92064_read_meas(priv, raw);
16662719894SLeonard Göhrs 		if (ret < 0)
16762719894SLeonard Göhrs 			return ret;
16862719894SLeonard Göhrs 
16962719894SLeonard Göhrs 		*val = (chan->address == TI_LMP92064_CHAN_INC) ? raw[0] : raw[1];
17062719894SLeonard Göhrs 
17162719894SLeonard Göhrs 		return IIO_VAL_INT;
17262719894SLeonard Göhrs 	case IIO_CHAN_INFO_SCALE:
17362719894SLeonard Göhrs 		if (chan->address == TI_LMP92064_CHAN_INC) {
17462719894SLeonard Göhrs 			/*
17562719894SLeonard Göhrs 			 * processed (mA) = raw * current_lsb (mA)
17662719894SLeonard Göhrs 			 * current_lsb (mA) = shunt_voltage_lsb (nV) / shunt_resistor (uOhm)
17762719894SLeonard Göhrs 			 * shunt_voltage_lsb (nV) = 81920000 / 4096 = 20000
17862719894SLeonard Göhrs 			 */
17962719894SLeonard Göhrs 			*val = 20000;
18062719894SLeonard Göhrs 			*val2 = priv->shunt_resistor_uohm;
18162719894SLeonard Göhrs 		} else {
18262719894SLeonard Göhrs 			/*
18362719894SLeonard Göhrs 			 * processed (mV) = raw * voltage_lsb (mV)
18462719894SLeonard Göhrs 			 * voltage_lsb (mV) = 2048 / 4096
18562719894SLeonard Göhrs 			 */
18662719894SLeonard Göhrs 			*val = 2048;
18762719894SLeonard Göhrs 			*val2 = 4096;
18862719894SLeonard Göhrs 		}
18962719894SLeonard Göhrs 		return IIO_VAL_FRACTIONAL;
19062719894SLeonard Göhrs 	default:
19162719894SLeonard Göhrs 		return -EINVAL;
19262719894SLeonard Göhrs 	}
19362719894SLeonard Göhrs }
19462719894SLeonard Göhrs 
lmp92064_trigger_handler(int irq,void * p)195*6c7bc1d2SLeonard Göhrs static irqreturn_t lmp92064_trigger_handler(int irq, void *p)
196*6c7bc1d2SLeonard Göhrs {
197*6c7bc1d2SLeonard Göhrs 	struct iio_poll_func *pf = p;
198*6c7bc1d2SLeonard Göhrs 	struct iio_dev *indio_dev = pf->indio_dev;
199*6c7bc1d2SLeonard Göhrs 	struct lmp92064_adc_priv *priv = iio_priv(indio_dev);
200*6c7bc1d2SLeonard Göhrs 	struct {
201*6c7bc1d2SLeonard Göhrs 		u16 values[2];
202*6c7bc1d2SLeonard Göhrs 		int64_t timestamp __aligned(8);
203*6c7bc1d2SLeonard Göhrs 	} data;
204*6c7bc1d2SLeonard Göhrs 	int ret;
205*6c7bc1d2SLeonard Göhrs 
206*6c7bc1d2SLeonard Göhrs 	memset(&data, 0, sizeof(data));
207*6c7bc1d2SLeonard Göhrs 
208*6c7bc1d2SLeonard Göhrs 	ret = lmp92064_read_meas(priv, data.values);
209*6c7bc1d2SLeonard Göhrs 	if (ret)
210*6c7bc1d2SLeonard Göhrs 		goto err;
211*6c7bc1d2SLeonard Göhrs 
212*6c7bc1d2SLeonard Göhrs 	iio_push_to_buffers_with_timestamp(indio_dev, &data,
213*6c7bc1d2SLeonard Göhrs 					   iio_get_time_ns(indio_dev));
214*6c7bc1d2SLeonard Göhrs 
215*6c7bc1d2SLeonard Göhrs err:
216*6c7bc1d2SLeonard Göhrs 	iio_trigger_notify_done(indio_dev->trig);
217*6c7bc1d2SLeonard Göhrs 
218*6c7bc1d2SLeonard Göhrs 	return IRQ_HANDLED;
219*6c7bc1d2SLeonard Göhrs }
220*6c7bc1d2SLeonard Göhrs 
lmp92064_reset(struct lmp92064_adc_priv * priv,struct gpio_desc * gpio_reset)22162719894SLeonard Göhrs static int lmp92064_reset(struct lmp92064_adc_priv *priv,
22262719894SLeonard Göhrs 			  struct gpio_desc *gpio_reset)
22362719894SLeonard Göhrs {
22462719894SLeonard Göhrs 	unsigned int status;
22562719894SLeonard Göhrs 	int ret, i;
22662719894SLeonard Göhrs 
22762719894SLeonard Göhrs 	if (gpio_reset) {
22862719894SLeonard Göhrs 		/*
22962719894SLeonard Göhrs 		 * Perform a hard reset if gpio_reset is available.
23062719894SLeonard Göhrs 		 * The datasheet specifies a very low 3.5ns reset pulse duration and does not
23162719894SLeonard Göhrs 		 * specify how long to wait after a reset to access the device.
23262719894SLeonard Göhrs 		 * Use more conservative pulse lengths to allow analog RC filtering of the
23362719894SLeonard Göhrs 		 * reset line at the board level (as recommended in the datasheet).
23462719894SLeonard Göhrs 		 */
23562719894SLeonard Göhrs 		gpiod_set_value_cansleep(gpio_reset, 1);
23662719894SLeonard Göhrs 		usleep_range(1, 10);
23762719894SLeonard Göhrs 		gpiod_set_value_cansleep(gpio_reset, 0);
23862719894SLeonard Göhrs 		usleep_range(500, 750);
23962719894SLeonard Göhrs 	} else {
24062719894SLeonard Göhrs 		/*
24162719894SLeonard Göhrs 		 * Perform a soft-reset if not.
24262719894SLeonard Göhrs 		 * Also write default values to the config registers that are not
24362719894SLeonard Göhrs 		 * affected by soft reset.
24462719894SLeonard Göhrs 		 */
24562719894SLeonard Göhrs 		ret = regmap_write(priv->regmap, TI_LMP92064_REG_CONFIG_A,
24662719894SLeonard Göhrs 				   TI_LMP92064_VAL_CONFIG_A);
24762719894SLeonard Göhrs 		if (ret < 0)
24862719894SLeonard Göhrs 			return ret;
24962719894SLeonard Göhrs 
25062719894SLeonard Göhrs 		ret = regmap_write(priv->regmap, TI_LMP92064_REG_CONFIG_B,
25162719894SLeonard Göhrs 				   TI_LMP92064_VAL_CONFIG_B);
25262719894SLeonard Göhrs 		if (ret < 0)
25362719894SLeonard Göhrs 			return ret;
25462719894SLeonard Göhrs 	}
25562719894SLeonard Göhrs 
25662719894SLeonard Göhrs 	/*
25762719894SLeonard Göhrs 	 * Wait for the device to signal readiness to prevent reading bogus data
25862719894SLeonard Göhrs 	 * and make sure device is actually connected.
25962719894SLeonard Göhrs 	 * The datasheet does not specify how long this takes but usually it is
26062719894SLeonard Göhrs 	 * not more than 3-4 iterations of this loop.
26162719894SLeonard Göhrs 	 */
26262719894SLeonard Göhrs 	for (i = 0; i < 10; i++) {
26362719894SLeonard Göhrs 		ret = regmap_read(priv->regmap, TI_LMP92064_REG_STATUS, &status);
26462719894SLeonard Göhrs 		if (ret < 0)
26562719894SLeonard Göhrs 			return ret;
26662719894SLeonard Göhrs 
26762719894SLeonard Göhrs 		if (status == TI_LMP92064_VAL_STATUS_OK)
26862719894SLeonard Göhrs 			return 0;
26962719894SLeonard Göhrs 
27062719894SLeonard Göhrs 		usleep_range(1000, 2000);
27162719894SLeonard Göhrs 	}
27262719894SLeonard Göhrs 
27362719894SLeonard Göhrs 	/*
27462719894SLeonard Göhrs 	 * No (correct) response received.
27562719894SLeonard Göhrs 	 * Device is mostly likely not connected to the bus.
27662719894SLeonard Göhrs 	 */
27762719894SLeonard Göhrs 	return -ENXIO;
27862719894SLeonard Göhrs }
27962719894SLeonard Göhrs 
28062719894SLeonard Göhrs static const struct iio_info lmp92064_adc_info = {
28162719894SLeonard Göhrs 	.read_raw = lmp92064_read_raw,
28262719894SLeonard Göhrs };
28362719894SLeonard Göhrs 
lmp92064_adc_probe(struct spi_device * spi)28462719894SLeonard Göhrs static int lmp92064_adc_probe(struct spi_device *spi)
28562719894SLeonard Göhrs {
28662719894SLeonard Göhrs 	struct device *dev = &spi->dev;
28762719894SLeonard Göhrs 	struct lmp92064_adc_priv *priv;
28862719894SLeonard Göhrs 	struct gpio_desc *gpio_reset;
28962719894SLeonard Göhrs 	struct iio_dev *indio_dev;
29062719894SLeonard Göhrs 	u32 shunt_resistor_uohm;
29162719894SLeonard Göhrs 	struct regmap *regmap;
29262719894SLeonard Göhrs 	int ret;
29362719894SLeonard Göhrs 
29462719894SLeonard Göhrs 	ret = spi_setup(spi);
29562719894SLeonard Göhrs 	if (ret < 0)
29662719894SLeonard Göhrs 		return dev_err_probe(dev, ret, "Error in SPI setup\n");
29762719894SLeonard Göhrs 
29862719894SLeonard Göhrs 	regmap = devm_regmap_init_spi(spi, &lmp92064_spi_regmap_config);
29962719894SLeonard Göhrs 	if (IS_ERR(regmap))
30062719894SLeonard Göhrs 		return dev_err_probe(dev, PTR_ERR(regmap),
30162719894SLeonard Göhrs 				     "Failed to set up SPI regmap\n");
30262719894SLeonard Göhrs 
30362719894SLeonard Göhrs 	indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
30462719894SLeonard Göhrs 	if (!indio_dev)
30562719894SLeonard Göhrs 		return -ENOMEM;
30662719894SLeonard Göhrs 
30762719894SLeonard Göhrs 	priv = iio_priv(indio_dev);
30862719894SLeonard Göhrs 
30962719894SLeonard Göhrs 	priv->spi = spi;
31062719894SLeonard Göhrs 	priv->regmap = regmap;
31162719894SLeonard Göhrs 
31262719894SLeonard Göhrs 	ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
31362719894SLeonard Göhrs 				       &shunt_resistor_uohm);
31462719894SLeonard Göhrs 	if (ret < 0)
31562719894SLeonard Göhrs 		return dev_err_probe(dev, ret,
31662719894SLeonard Göhrs 				     "Failed to get shunt-resistor value\n");
31762719894SLeonard Göhrs 
31862719894SLeonard Göhrs 	/*
31962719894SLeonard Göhrs 	 * The shunt resistance is passed to userspace as the denominator of an iio
32062719894SLeonard Göhrs 	 * fraction. Make sure it is in range for that.
32162719894SLeonard Göhrs 	 */
32262719894SLeonard Göhrs 	if (shunt_resistor_uohm == 0 || shunt_resistor_uohm > INT_MAX) {
32362719894SLeonard Göhrs 		dev_err(dev, "Shunt resistance is out of range\n");
32462719894SLeonard Göhrs 		return -EINVAL;
32562719894SLeonard Göhrs 	}
32662719894SLeonard Göhrs 
32762719894SLeonard Göhrs 	priv->shunt_resistor_uohm = shunt_resistor_uohm;
32862719894SLeonard Göhrs 
32962719894SLeonard Göhrs 	ret = devm_regulator_get_enable(dev, "vdd");
33062719894SLeonard Göhrs 	if (ret)
33162719894SLeonard Göhrs 		return ret;
33262719894SLeonard Göhrs 
33362719894SLeonard Göhrs 	ret = devm_regulator_get_enable(dev, "vdig");
33462719894SLeonard Göhrs 	if (ret)
33562719894SLeonard Göhrs 		return ret;
33662719894SLeonard Göhrs 
33762719894SLeonard Göhrs 	gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
33862719894SLeonard Göhrs 	if (IS_ERR(gpio_reset))
33962719894SLeonard Göhrs 		return dev_err_probe(dev, PTR_ERR(gpio_reset),
34062719894SLeonard Göhrs 				     "Failed to get GPIO reset pin\n");
34162719894SLeonard Göhrs 
34262719894SLeonard Göhrs 	ret = lmp92064_reset(priv, gpio_reset);
34362719894SLeonard Göhrs 	if (ret < 0)
34462719894SLeonard Göhrs 		return dev_err_probe(dev, ret, "Failed to reset device\n");
34562719894SLeonard Göhrs 
34662719894SLeonard Göhrs 	indio_dev->name = "lmp92064";
34762719894SLeonard Göhrs 	indio_dev->modes = INDIO_DIRECT_MODE;
34862719894SLeonard Göhrs 	indio_dev->channels = lmp92064_adc_channels;
34962719894SLeonard Göhrs 	indio_dev->num_channels = ARRAY_SIZE(lmp92064_adc_channels);
35062719894SLeonard Göhrs 	indio_dev->info = &lmp92064_adc_info;
351*6c7bc1d2SLeonard Göhrs 	indio_dev->available_scan_masks = lmp92064_scan_masks;
352*6c7bc1d2SLeonard Göhrs 
353*6c7bc1d2SLeonard Göhrs 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
354*6c7bc1d2SLeonard Göhrs 					      lmp92064_trigger_handler, NULL);
355*6c7bc1d2SLeonard Göhrs 	if (ret)
356*6c7bc1d2SLeonard Göhrs 		return dev_err_probe(dev, ret, "Failed to setup buffered read\n");
35762719894SLeonard Göhrs 
35862719894SLeonard Göhrs 	return devm_iio_device_register(dev, indio_dev);
35962719894SLeonard Göhrs }
36062719894SLeonard Göhrs 
36162719894SLeonard Göhrs static const struct spi_device_id lmp92064_id_table[] = {
36262719894SLeonard Göhrs 	{ "lmp92064" },
36362719894SLeonard Göhrs 	{ }
36462719894SLeonard Göhrs };
36562719894SLeonard Göhrs MODULE_DEVICE_TABLE(spi, lmp92064_id_table);
36662719894SLeonard Göhrs 
36762719894SLeonard Göhrs static const struct of_device_id lmp92064_of_table[] = {
36862719894SLeonard Göhrs 	{ .compatible = "ti,lmp92064" },
36962719894SLeonard Göhrs 	{}
37062719894SLeonard Göhrs };
37162719894SLeonard Göhrs MODULE_DEVICE_TABLE(of, lmp92064_of_table);
37262719894SLeonard Göhrs 
37362719894SLeonard Göhrs static struct spi_driver lmp92064_adc_driver = {
37462719894SLeonard Göhrs 	.driver = {
37562719894SLeonard Göhrs 		.name = "lmp92064",
37662719894SLeonard Göhrs 		.of_match_table = lmp92064_of_table,
37762719894SLeonard Göhrs 	},
37862719894SLeonard Göhrs 	.probe = lmp92064_adc_probe,
37962719894SLeonard Göhrs 	.id_table = lmp92064_id_table,
38062719894SLeonard Göhrs };
38162719894SLeonard Göhrs module_spi_driver(lmp92064_adc_driver);
38262719894SLeonard Göhrs 
38362719894SLeonard Göhrs MODULE_AUTHOR("Leonard Göhrs <kernel@pengutronix.de>");
38462719894SLeonard Göhrs MODULE_DESCRIPTION("TI LMP92064 ADC");
38562719894SLeonard Göhrs MODULE_LICENSE("GPL");
386