* [PATCH v5 1/3] dt-bindings: iio: adc: ti,tsc2046: add vref-supply property
@ 2022-09-03 5:02 Oleksij Rempel
2022-09-03 5:02 ` [PATCH v5 2/3] iio: adc: tsc2046: add vref support Oleksij Rempel
2022-09-03 5:02 ` [PATCH v5 3/3] iio: adc: tsc2046: silent spi_device_id warning Oleksij Rempel
0 siblings, 2 replies; 4+ messages in thread
From: Oleksij Rempel @ 2022-09-03 5:02 UTC (permalink / raw)
To: Jonathan Cameron, Lars-Peter Clausen, Rob Herring,
Krzysztof Kozlowski
Cc: Oleksij Rempel, Krzysztof Kozlowski, kernel, linux-kernel,
linux-iio, devicetree, Andy Shevchenko
Add property for the voltage reference supply.
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
Documentation/devicetree/bindings/iio/adc/ti,tsc2046.yaml | 3 +++
1 file changed, 3 insertions(+)
diff --git a/Documentation/devicetree/bindings/iio/adc/ti,tsc2046.yaml b/Documentation/devicetree/bindings/iio/adc/ti,tsc2046.yaml
index 601d69971d84a..7faf12b1598b9 100644
--- a/Documentation/devicetree/bindings/iio/adc/ti,tsc2046.yaml
+++ b/Documentation/devicetree/bindings/iio/adc/ti,tsc2046.yaml
@@ -25,6 +25,9 @@ properties:
spi-max-frequency: true
+ vref-supply:
+ description: Optional supply of the reference voltage
+
"#io-channel-cells":
const: 1
--
2.30.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v5 2/3] iio: adc: tsc2046: add vref support
2022-09-03 5:02 [PATCH v5 1/3] dt-bindings: iio: adc: ti,tsc2046: add vref-supply property Oleksij Rempel
@ 2022-09-03 5:02 ` Oleksij Rempel
2022-09-03 9:26 ` Andy Shevchenko
2022-09-03 5:02 ` [PATCH v5 3/3] iio: adc: tsc2046: silent spi_device_id warning Oleksij Rempel
1 sibling, 1 reply; 4+ messages in thread
From: Oleksij Rempel @ 2022-09-03 5:02 UTC (permalink / raw)
To: Jonathan Cameron, Lars-Peter Clausen, Rob Herring,
Krzysztof Kozlowski
Cc: Oleksij Rempel, kernel, linux-kernel, linux-iio, devicetree,
Andy Shevchenko
If VREF pin is attached, we should use external VREF source instead of
the internal. Otherwise we will get wrong measurements on some of the channel
types.
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
changes v5:
- add "the" before channel
- refactor error handling on regulator registration
- use MILLI instead of 1000
changes v4:
- use vref_reg pointer instead of bool use_internal_vref
- move regulator registration to a separate function
- rework error handling
- add devm_add_action_or_reset
---
drivers/iio/adc/ti-tsc2046.c | 57 ++++++++++++++++++++++++++++++++++--
1 file changed, 55 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/ti-tsc2046.c b/drivers/iio/adc/ti-tsc2046.c
index 0d9436a69cbfb..c7601b29b3bef 100644
--- a/drivers/iio/adc/ti-tsc2046.c
+++ b/drivers/iio/adc/ti-tsc2046.c
@@ -8,7 +8,9 @@
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/module.h>
+#include <linux/regulator/consumer.h>
#include <linux/spi/spi.h>
+#include <linux/units.h>
#include <asm/unaligned.h>
@@ -175,6 +177,8 @@ struct tsc2046_adc_priv {
u32 time_per_bit_ns;
struct tsc2046_adc_ch_cfg ch_cfg[TI_TSC2046_MAX_CHAN];
+ 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->vref_reg)
+ 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;
}
@@ -740,6 +746,49 @@ static void tsc2046_adc_parse_fwnode(struct tsc2046_adc_priv *priv)
}
}
+static void tsc2046_adc_regulator_disable(void *data)
+{
+ struct tsc2046_adc_priv *priv = data;
+
+ regulator_disable(priv->vref_reg);
+}
+
+static int tsc2046_adc_configure_regulator(struct tsc2046_adc_priv *priv)
+{
+ struct device *dev = &priv->spi->dev;
+ int ret;
+
+ priv->vref_reg = devm_regulator_get_optional(dev, "vref");
+ if (IS_ERR(priv->vref_reg)) {
+ /* If regulator exists but can't be get, return an error */
+ if (PTR_ERR(priv->vref_reg) != -ENODEV)
+ return PTR_ERR(priv->vref_reg);
+ priv->vref_reg = NULL;
+ }
+ if (!priv->vref_reg) {
+ /* Use internal reference */
+ priv->vref_mv = TI_TSC2046_INT_VREF;
+ return 0;
+ }
+
+ ret = regulator_enable(priv->vref_reg);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(dev, tsc2046_adc_regulator_disable,
+ priv);
+ if (ret)
+ return ret;
+
+ ret = regulator_get_voltage(priv->vref_reg);
+ if (ret < 0)
+ return ret;
+
+ priv->vref_mv = ret / MILLI;
+
+ return 0;
+}
+
static int tsc2046_adc_probe(struct spi_device *spi)
{
const struct tsc2046_adc_dcfg *dcfg;
@@ -781,6 +830,10 @@ static int tsc2046_adc_probe(struct spi_device *spi)
indio_dev->num_channels = dcfg->num_channels;
indio_dev->info = &tsc2046_adc_info;
+ ret = tsc2046_adc_configure_regulator(priv);
+ if (ret)
+ return ret;
+
tsc2046_adc_parse_fwnode(priv);
ret = tsc2046_adc_setup_spi_msg(priv);
--
2.30.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v5 3/3] iio: adc: tsc2046: silent spi_device_id warning
2022-09-03 5:02 [PATCH v5 1/3] dt-bindings: iio: adc: ti,tsc2046: add vref-supply property Oleksij Rempel
2022-09-03 5:02 ` [PATCH v5 2/3] iio: adc: tsc2046: add vref support Oleksij Rempel
@ 2022-09-03 5:02 ` Oleksij Rempel
1 sibling, 0 replies; 4+ messages in thread
From: Oleksij Rempel @ 2022-09-03 5:02 UTC (permalink / raw)
To: Jonathan Cameron, Lars-Peter Clausen, Rob Herring,
Krzysztof Kozlowski
Cc: Oleksij Rempel, Andy Shevchenko, kernel, linux-kernel, linux-iio,
devicetree
Add spi_device_id to silent following kernel runtime warning:
"SPI driver tsc2046 has no spi_device_id for ti,tsc2046e-adc".
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
---
changes v4:
- add Reviewed-by: Andy Shevchenko
changes v3:
- add missing point
- remove unneeded blank line
- assignment id at the definition line
changes v2:
- attach actual driver_data
- use spi_get_device_id fallback
---
drivers/iio/adc/ti-tsc2046.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/iio/adc/ti-tsc2046.c b/drivers/iio/adc/ti-tsc2046.c
index c7601b29b3bef..c023163f71464 100644
--- a/drivers/iio/adc/ti-tsc2046.c
+++ b/drivers/iio/adc/ti-tsc2046.c
@@ -805,6 +805,11 @@ static int tsc2046_adc_probe(struct spi_device *spi)
}
dcfg = device_get_match_data(dev);
+ if (!dcfg) {
+ const struct spi_device_id *id = spi_get_device_id(spi);
+
+ dcfg = (const struct tsc2046_adc_dcfg *)id->driver_data;
+ }
if (!dcfg)
return -EINVAL;
@@ -886,11 +891,18 @@ static const struct of_device_id ads7950_of_table[] = {
};
MODULE_DEVICE_TABLE(of, ads7950_of_table);
+static const struct spi_device_id tsc2046_adc_spi_ids[] = {
+ { "tsc2046e-adc", (unsigned long)&tsc2046_adc_dcfg_tsc2046e },
+ { }
+};
+MODULE_DEVICE_TABLE(spi, tsc2046_adc_spi_ids);
+
static struct spi_driver tsc2046_adc_driver = {
.driver = {
.name = "tsc2046",
.of_match_table = ads7950_of_table,
},
+ .id_table = tsc2046_adc_spi_ids,
.probe = tsc2046_adc_probe,
};
module_spi_driver(tsc2046_adc_driver);
--
2.30.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v5 2/3] iio: adc: tsc2046: add vref support
2022-09-03 5:02 ` [PATCH v5 2/3] iio: adc: tsc2046: add vref support Oleksij Rempel
@ 2022-09-03 9:26 ` Andy Shevchenko
0 siblings, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2022-09-03 9:26 UTC (permalink / raw)
To: Oleksij Rempel
Cc: Jonathan Cameron, Lars-Peter Clausen, Rob Herring,
Krzysztof Kozlowski, Sascha Hauer, Linux Kernel Mailing List,
linux-iio, devicetree
On Sat, Sep 3, 2022 at 8:03 AM Oleksij Rempel <o.rempel@pengutronix.de> wrote:
>
> If VREF pin is attached, we should use external VREF source instead of
> the internal. Otherwise we will get wrong measurements on some of the channel
> types.
FWIW,
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
> changes v5:
> - add "the" before channel
> - refactor error handling on regulator registration
> - use MILLI instead of 1000
> changes v4:
> - use vref_reg pointer instead of bool use_internal_vref
> - move regulator registration to a separate function
> - rework error handling
> - add devm_add_action_or_reset
> ---
> drivers/iio/adc/ti-tsc2046.c | 57 ++++++++++++++++++++++++++++++++++--
> 1 file changed, 55 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/adc/ti-tsc2046.c b/drivers/iio/adc/ti-tsc2046.c
> index 0d9436a69cbfb..c7601b29b3bef 100644
> --- a/drivers/iio/adc/ti-tsc2046.c
> +++ b/drivers/iio/adc/ti-tsc2046.c
> @@ -8,7 +8,9 @@
> #include <linux/bitfield.h>
> #include <linux/delay.h>
> #include <linux/module.h>
> +#include <linux/regulator/consumer.h>
> #include <linux/spi/spi.h>
> +#include <linux/units.h>
>
> #include <asm/unaligned.h>
>
> @@ -175,6 +177,8 @@ struct tsc2046_adc_priv {
> u32 time_per_bit_ns;
>
> struct tsc2046_adc_ch_cfg ch_cfg[TI_TSC2046_MAX_CHAN];
> + unsigned int vref_mv;
> + struct regulator *vref_reg;
I would put pointer first since it will make sizeof(priv) 4 bytes less
on some architectures due to padding elimination.
> };
>
> #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->vref_reg)
> + 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;
> }
> @@ -740,6 +746,49 @@ static void tsc2046_adc_parse_fwnode(struct tsc2046_adc_priv *priv)
> }
> }
>
> +static void tsc2046_adc_regulator_disable(void *data)
> +{
> + struct tsc2046_adc_priv *priv = data;
> +
> + regulator_disable(priv->vref_reg);
> +}
> +
> +static int tsc2046_adc_configure_regulator(struct tsc2046_adc_priv *priv)
> +{
> + struct device *dev = &priv->spi->dev;
> + int ret;
> +
> + priv->vref_reg = devm_regulator_get_optional(dev, "vref");
> + if (IS_ERR(priv->vref_reg)) {
> + /* If regulator exists but can't be get, return an error */
> + if (PTR_ERR(priv->vref_reg) != -ENODEV)
> + return PTR_ERR(priv->vref_reg);
> + priv->vref_reg = NULL;
> + }
> + if (!priv->vref_reg) {
> + /* Use internal reference */
> + priv->vref_mv = TI_TSC2046_INT_VREF;
> + return 0;
> + }
> +
> + ret = regulator_enable(priv->vref_reg);
> + if (ret)
> + return ret;
> +
> + ret = devm_add_action_or_reset(dev, tsc2046_adc_regulator_disable,
> + priv);
> + if (ret)
> + return ret;
> +
> + ret = regulator_get_voltage(priv->vref_reg);
> + if (ret < 0)
> + return ret;
> +
> + priv->vref_mv = ret / MILLI;
> +
> + return 0;
> +}
> +
> static int tsc2046_adc_probe(struct spi_device *spi)
> {
> const struct tsc2046_adc_dcfg *dcfg;
> @@ -781,6 +830,10 @@ static int tsc2046_adc_probe(struct spi_device *spi)
> indio_dev->num_channels = dcfg->num_channels;
> indio_dev->info = &tsc2046_adc_info;
>
> + ret = tsc2046_adc_configure_regulator(priv);
> + if (ret)
> + return ret;
> +
> tsc2046_adc_parse_fwnode(priv);
>
> ret = tsc2046_adc_setup_spi_msg(priv);
> --
> 2.30.2
>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-09-03 9:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-03 5:02 [PATCH v5 1/3] dt-bindings: iio: adc: ti,tsc2046: add vref-supply property Oleksij Rempel
2022-09-03 5:02 ` [PATCH v5 2/3] iio: adc: tsc2046: add vref support Oleksij Rempel
2022-09-03 9:26 ` Andy Shevchenko
2022-09-03 5:02 ` [PATCH v5 3/3] iio: adc: tsc2046: silent spi_device_id warning Oleksij Rempel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).