[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220830110709.2037302-2-o.rempel@pengutronix.de>
Date: Tue, 30 Aug 2022 13:07:08 +0200
From: Oleksij Rempel <o.rempel@...gutronix.de>
To: Jonathan Cameron <jic23@...nel.org>,
Lars-Peter Clausen <lars@...afoo.de>,
Rob Herring <robh+dt@...nel.org>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@...aro.org>
Cc: Oleksij Rempel <o.rempel@...gutronix.de>, kernel@...gutronix.de,
linux-kernel@...r.kernel.org, linux-iio@...r.kernel.org,
devicetree@...r.kernel.org
Subject: [PATCH v1 2/3] iio: adc: tsc2046: add vref support
If VREF pin is attached, we should use external VREF source instead of
the internal. Otherwise we will get wrong measurements on some of channel
types.
Signed-off-by: Oleksij Rempel <o.rempel@...gutronix.de>
---
drivers/iio/adc/ti-tsc2046.c | 64 +++++++++++++++++++++++++++++++-----
1 file changed, 55 insertions(+), 9 deletions(-)
diff --git a/drivers/iio/adc/ti-tsc2046.c b/drivers/iio/adc/ti-tsc2046.c
index 0d9436a69cbfb..bbc8b4137b0b1 100644
--- a/drivers/iio/adc/ti-tsc2046.c
+++ b/drivers/iio/adc/ti-tsc2046.c
@@ -8,6 +8,7 @@
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/module.h>
+#include <linux/regulator/consumer.h>
#include <linux/spi/spi.h>
#include <asm/unaligned.h>
@@ -175,6 +176,9 @@ struct tsc2046_adc_priv {
u32 time_per_bit_ns;
struct tsc2046_adc_ch_cfg ch_cfg[TI_TSC2046_MAX_CHAN];
+ bool use_internal_vref;
+ unsigned int vref_mv;
+ struct regulator *vref_reg;
};
#define TI_TSC2046_V_CHAN(index, bits, name) \
@@ -252,7 +256,9 @@ static u8 tsc2046_adc_get_cmd(struct tsc2046_adc_priv *priv, int ch_idx,
case TI_TSC2046_ADDR_AUX:
case TI_TSC2046_ADDR_VBAT:
case TI_TSC2046_ADDR_TEMP0:
- pd |= TI_TSC2046_SER | TI_TSC2046_PD1_VREF_ON;
+ pd |= TI_TSC2046_SER;
+ if (priv->use_internal_vref)
+ pd |= TI_TSC2046_PD1_VREF_ON;
}
return TI_TSC2046_START | FIELD_PREP(TI_TSC2046_ADDR, ch_idx) | pd;
@@ -468,7 +474,7 @@ static int tsc2046_adc_read_raw(struct iio_dev *indio_dev,
* So, it is better to use external voltage-divider driver
* instead, which is calculating complete chain.
*/
- *val = TI_TSC2046_INT_VREF;
+ *val = priv->vref_mv;
*val2 = chan->scan_type.realbits;
return IIO_VAL_FRACTIONAL_LOG2;
}
@@ -781,22 +787,42 @@ static int tsc2046_adc_probe(struct spi_device *spi)
indio_dev->num_channels = dcfg->num_channels;
indio_dev->info = &tsc2046_adc_info;
+ priv->vref_reg = devm_regulator_get_optional(&spi->dev, "vref");
+ if (!IS_ERR(priv->vref_reg)) {
+ ret = regulator_enable(priv->vref_reg);
+ if (ret)
+ return ret;
+
+ ret = regulator_get_voltage(priv->vref_reg);
+ if (ret < 0)
+ goto err_regulator_disable;
+
+ priv->vref_mv = ret / 1000;
+ priv->use_internal_vref = false;
+ } else {
+ /* Use internal reference */
+ priv->vref_mv = TI_TSC2046_INT_VREF;
+ priv->use_internal_vref = true;
+ }
+
tsc2046_adc_parse_fwnode(priv);
ret = tsc2046_adc_setup_spi_msg(priv);
if (ret)
- return ret;
+ goto err_regulator_disable;
mutex_init(&priv->slock);
ret = devm_request_irq(dev, spi->irq, &tsc2046_adc_irq,
IRQF_NO_AUTOEN, indio_dev->name, indio_dev);
if (ret)
- return ret;
+ goto err_regulator_disable;
trig = devm_iio_trigger_alloc(dev, "touchscreen-%s", indio_dev->name);
- if (!trig)
- return -ENOMEM;
+ if (!trig) {
+ ret = -ENOMEM;
+ goto err_regulator_disable;
+ }
priv->trig = trig;
iio_trigger_set_drvdata(trig, indio_dev);
@@ -811,20 +837,39 @@ static int tsc2046_adc_probe(struct spi_device *spi)
ret = devm_iio_trigger_register(dev, trig);
if (ret) {
dev_err(dev, "failed to register trigger\n");
- return ret;
+ goto err_regulator_disable;
}
ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
&tsc2046_adc_trigger_handler, NULL);
if (ret) {
dev_err(dev, "Failed to setup triggered buffer\n");
- return ret;
+ goto err_regulator_disable;
}
/* set default trigger */
indio_dev->trig = iio_trigger_get(priv->trig);
- return devm_iio_device_register(dev, indio_dev);
+ ret = devm_iio_device_register(dev, indio_dev);
+ if (ret)
+ goto err_regulator_disable;
+
+ return 0;
+
+err_regulator_disable:
+ if (!IS_ERR(priv->vref_reg))
+ regulator_disable(priv->vref_reg);
+
+ return ret;
+}
+
+static void tsc2046_adc_remove(struct spi_device *spi)
+{
+ struct iio_dev *indio_dev = spi_get_drvdata(spi);
+ struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
+
+ if (!IS_ERR(priv->vref_reg))
+ regulator_disable(priv->vref_reg);
}
static const struct of_device_id ads7950_of_table[] = {
@@ -839,6 +884,7 @@ static struct spi_driver tsc2046_adc_driver = {
.of_match_table = ads7950_of_table,
},
.probe = tsc2046_adc_probe,
+ .remove = tsc2046_adc_remove,
};
module_spi_driver(tsc2046_adc_driver);
--
2.30.2
Powered by blists - more mailing lists