Skip to contentSkip to conversation

iio: dac: update ltc2632 device driver

PATCH In review Silvan Murer 2 participants 6 messages May 21, 2018 – May 23, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover iio: dac: update ltc2632 device driver

Changes in v4:
- fix patch format issues
Change in v3:
- remove extra spaces and tab
Changes in v2:
- Add 'optional properties' documentation
- Return an error when a regulator is specified
- Use internal reference when no regulator is specified
- Use iio_device_register instead of devm_iio_device_register

#1 lore raw

PATCH v4 1/2 iio: dac: fix missing of_match_table assignment in the LTC 2632 device

---
 drivers/iio/dac/ltc2632.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/iio/dac/ltc2632.c +10 -9
index ac5e05f..d322b78 100644
@@ -276,15 +276,6 @@ static const struct spi_device_id ltc2632_id[] = {
 };
 MODULE_DEVICE_TABLE(spi, ltc2632_id);
 
-static struct spi_driver ltc2632_driver = {
-	.driver		= {
-		.name	= "ltc2632",
-	},
-	.probe		= ltc2632_probe,
-	.id_table	= ltc2632_id,
-};
-module_spi_driver(ltc2632_driver);
-
 static const struct of_device_id ltc2632_of_match[] = {
 	{
 		.compatible = "lltc,ltc2632-l12",
@@ -309,6 +300,16 @@ static const struct of_device_id ltc2632_of_match[] = {
 };
 MODULE_DEVICE_TABLE(of, ltc2632_of_match);
 
+static struct spi_driver ltc2632_driver = {
+	.driver		= {
+		.name	= "ltc2632",
+		.of_match_table = of_match_ptr(ltc2632_of_match),
+	},
+	.probe		= ltc2632_probe,
+	.id_table	= ltc2632_id,
+};
+module_spi_driver(ltc2632_driver);
+
 MODULE_AUTHOR("Maxime Roussin-Belanger <[email protected]>");
 MODULE_DESCRIPTION("LTC2632 DAC SPI driver");
 MODULE_LICENSE("GPL v2");
Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH v4 1/2] iio: dac: fix missing of_match_table assignment in the LTC 2632 device

Quoted 2 lines · On Mon, 21 May 2018 14:21:27 +0200 Silvan Murer <[email protected]> wrote:

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it.

Thanks,

Jonathan

Quoted 41 lines
---
 drivers/iio/dac/ltc2632.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/iio/dac/ltc2632.c +10 -9
index ac5e05f..d322b78 100644
@@ -276,15 +276,6 @@ static const struct spi_device_id ltc2632_id[] = {
 };
 MODULE_DEVICE_TABLE(spi, ltc2632_id);
 
-static struct spi_driver ltc2632_driver = {
-	.driver		= {
-		.name	= "ltc2632",
-	},
-	.probe		= ltc2632_probe,
-	.id_table	= ltc2632_id,
-};
-module_spi_driver(ltc2632_driver);
-
 static const struct of_device_id ltc2632_of_match[] = {
 	{
 		.compatible = "lltc,ltc2632-l12",
@@ -309,6 +300,16 @@ static const struct of_device_id ltc2632_of_match[] = {
 };
 MODULE_DEVICE_TABLE(of, ltc2632_of_match);
 
+static struct spi_driver ltc2632_driver = {
+	.driver		= {
+		.name	= "ltc2632",
+		.of_match_table = of_match_ptr(ltc2632_of_match),
+	},
+	.probe		= ltc2632_probe,
+	.id_table	= ltc2632_id,
+};
+module_spi_driver(ltc2632_driver);
+
 MODULE_AUTHOR("Maxime Roussin-Belanger <[email protected]>");
 MODULE_DESCRIPTION("LTC2632 DAC SPI driver");
 MODULE_LICENSE("GPL v2");
#3 lore raw

PATCH v4 2/2 iio: dac: Add support for external reference voltage through the regulator framework.

In reply to Silvan Murer
---
 .../devicetree/bindings/iio/dac/ltc2632.txt        | 14 +++++
 drivers/iio/dac/ltc2632.c                          | 70 +++++++++++++++++++---
 2 files changed, 75 insertions(+), 9 deletions(-)
Documentation/devicetree/bindings/iio/dac/ltc2632.txt +14 -0
index eb911e5..e0d5fea 100644
@@ -12,12 +12,26 @@ Required properties:
 Property rules described in Documentation/devicetree/bindings/spi/spi-bus.txt
 apply. In particular, "reg" and "spi-max-frequency" properties must be given.
 
+Optional properties:
+	- vref-supply: Phandle to the external reference voltage supply. This should
+	  only be set if there is an external reference voltage connected to the VREF
+	  pin. If the property is not set the internal reference is used.
+
 Example:
 
+	vref: regulator-vref {
+		compatible = "regulator-fixed";
+		regulator-name = "vref-ltc2632";
+		regulator-min-microvolt = <1250000>;
+		regulator-max-microvolt = <1250000>;
+		regulator-always-on;
+	};
+
 	spi_master {
 		dac: ltc2632@0 {
 			compatible = "lltc,ltc2632-l12";
 			reg = <0>; /* CS0 */
 			spi-max-frequency = <1000000>;
+			vref-supply = <&vref>; /* optional */
 		};
 	};
drivers/iio/dac/ltc2632.c +61 -9

123 lines not on this page · raw message

Signature · 1 line
2.7.4
maintainer
#4 lore raw

Re: [PATCH v4 2/2] iio: dac: Add support for external reference voltage through the regulator framework.

Quoted 2 lines · On Mon, 21 May 2018 14:21:28 +0200 Silvan Murer <[email protected]> wrote:

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it.

Thanks,

Jonathan

Quoted 171 lines
---
 .../devicetree/bindings/iio/dac/ltc2632.txt        | 14 +++++
 drivers/iio/dac/ltc2632.c                          | 70 +++++++++++++++++++---
 2 files changed, 75 insertions(+), 9 deletions(-)
Documentation/devicetree/bindings/iio/dac/ltc2632.txt +14 -0
index eb911e5..e0d5fea 100644
@@ -12,12 +12,26 @@ Required properties:
 Property rules described in Documentation/devicetree/bindings/spi/spi-bus.txt
 apply. In particular, "reg" and "spi-max-frequency" properties must be given.
 
+Optional properties:
+	- vref-supply: Phandle to the external reference voltage supply. This should
+	  only be set if there is an external reference voltage connected to the VREF
+	  pin. If the property is not set the internal reference is used.
+
 Example:
 
+	vref: regulator-vref {
+		compatible = "regulator-fixed";
+		regulator-name = "vref-ltc2632";
+		regulator-min-microvolt = <1250000>;
+		regulator-max-microvolt = <1250000>;
+		regulator-always-on;
+	};
+
 	spi_master {
 		dac: ltc2632@0 {
 			compatible = "lltc,ltc2632-l12";
 			reg = <0>; /* CS0 */
 			spi-max-frequency = <1000000>;
+			vref-supply = <&vref>; /* optional */
 		};
 	};
drivers/iio/dac/ltc2632.c +61 -9
index d322b78..9551156 100644
@@ -2,6 +2,7 @@
  * LTC2632 Digital to analog convertors spi driver
  *
  * Copyright 2017 Maxime Roussin-B_langer
+ * expanded by Silvan Murer <[email protected]>
  *
  * Licensed under the GPL-2.
  */
@@ -10,6 +11,7 @@
 #include <linux/spi/spi.h>
 #include <linux/module.h>
 #include <linux/iio/iio.h>
+#include <linux/regulator/consumer.h>
 
 #define LTC2632_DAC_CHANNELS                    2
 
@@ -28,7 +30,7 @@
 /**
  * struct ltc2632_chip_info - chip specific information
  * @channels:		channel spec for the DAC
- * @vref_mv:		reference voltage
+ * @vref_mv:		internal reference voltage
  */
 struct ltc2632_chip_info {
 	const struct iio_chan_spec *channels;
@@ -39,10 +41,14 @@ struct ltc2632_chip_info {
  * struct ltc2632_state - driver instance specific data
  * @spi_dev:			pointer to the spi_device struct
  * @powerdown_cache_mask	used to show current channel powerdown state
+ * @vref_mv			used reference voltage (internal or external)
+ * @vref_reg		regulator for the reference voltage
  */
 struct ltc2632_state {
 	struct spi_device *spi_dev;
 	unsigned int powerdown_cache_mask;
+	int vref_mv;
+	struct regulator *vref_reg;
 };
 
 enum ltc2632_supported_device_ids {
@@ -90,7 +96,7 @@ static int ltc2632_read_raw(struct iio_dev *indio_dev,
 
 	switch (m) {
 	case IIO_CHAN_INFO_SCALE:
-		*val = chip_info->vref_mv;
+		*val = st->vref_mv;
 		*val2 = chan->scan_type.realbits;
 		return IIO_VAL_FRACTIONAL_LOG2;
 	}
@@ -247,6 +253,45 @@ static int ltc2632_probe(struct spi_device *spi)
 	chip_info = (struct ltc2632_chip_info *)
 			spi_get_device_id(spi)->driver_data;
 
+	st->vref_reg = devm_regulator_get_optional(&spi->dev, "vref");
+	if (PTR_ERR(st->vref_reg) == -ENODEV) {
+		/* use internal reference voltage */
+		st->vref_reg = NULL;
+		st->vref_mv = chip_info->vref_mv;
+
+		ret = ltc2632_spi_write(spi, LTC2632_CMD_INTERNAL_REFER,
+				0, 0, 0);
+		if (ret) {
+			dev_err(&spi->dev,
+				"Set internal reference command failed, %d\n",
+				ret);
+			return ret;
+		}
+	} else if (IS_ERR(st->vref_reg)) {
+		dev_err(&spi->dev,
+				"Error getting voltage reference regulator\n");
+		return PTR_ERR(st->vref_reg);
+	} else {
+		/* use external reference voltage */
+		ret = regulator_enable(st->vref_reg);
+		if (ret) {
+			dev_err(&spi->dev,
+				"enable reference regulator failed, %d\n",
+				ret);
+			return ret;
+		}
+		st->vref_mv = regulator_get_voltage(st->vref_reg) / 1000;
+
+		ret = ltc2632_spi_write(spi, LTC2632_CMD_EXTERNAL_REFER,
+				0, 0, 0);
+		if (ret) {
+			dev_err(&spi->dev,
+				"Set external reference command failed, %d\n",
+				ret);
+			return ret;
+		}
+	}
+
 	indio_dev->dev.parent = &spi->dev;
 	indio_dev->name = dev_of_node(&spi->dev) ? dev_of_node(&spi->dev)->name
 						 : spi_get_device_id(spi)->name;
@@ -255,14 +300,20 @@ static int ltc2632_probe(struct spi_device *spi)
 	indio_dev->channels = chip_info->channels;
 	indio_dev->num_channels = LTC2632_DAC_CHANNELS;
 
-	ret = ltc2632_spi_write(spi, LTC2632_CMD_INTERNAL_REFER, 0, 0, 0);
-	if (ret) {
-		dev_err(&spi->dev,
-			"Set internal reference command failed, %d\n", ret);
-		return ret;
-	}
+	return iio_device_register(indio_dev);
+}
+
+static int ltc2632_remove(struct spi_device *spi)
+{
+	struct iio_dev *indio_dev = spi_get_drvdata(spi);
+	struct ltc2632_state *st = iio_priv(indio_dev);
+
+	iio_device_unregister(indio_dev);
+
+	if (st->vref_reg)
+		regulator_disable(st->vref_reg);
 
-	return devm_iio_device_register(&spi->dev, indio_dev);
+	return 0;
 }
 
 static const struct spi_device_id ltc2632_id[] = {
@@ -306,6 +357,7 @@ static struct spi_driver ltc2632_driver = {
 		.of_match_table = of_match_ptr(ltc2632_of_match),
 	},
 	.probe		= ltc2632_probe,
+	.remove		= ltc2632_remove,
 	.id_table	= ltc2632_id,
 };
 module_spi_driver(ltc2632_driver);
#5 lore raw

Re: [PATCH v4 2/2] iio: dac: Add support for external reference voltage through the regulator framework.

Thank you Jonathan

Quoted 210 lines · Jonathan Cameron
Quoted 3 lines · On Mon, 21 May 2018 14:21:28 +0200 Silvan Murer <[email protected]> wrote:

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it.

Thanks,

Jonathan

Quoted 197 lines

---
 .../devicetree/bindings/iio/dac/ltc2632.txt        | 14 +++++ drivers/iio/dac/ltc2632.c                          | 70
+++++++++++++++++++---

 2 files changed, 75 insertions(+), 9 deletions(-)
Documentation/devicetree/bindings/iio/dac/ltc2632.txt +0 -0
index eb911e5..e0d5fea 100644
@@ -12,12 +12,26 @@ Required properties:

 Property rules described in Documentation/devicetree/bindings/spi/spi-bus.txt apply. In particular, "reg" and "spi-max-frequency" properties must be given.

Documentation/devicetree/bindings/iio/dac/ltc2632.txt +2 -0
+Optional properties:
+	- vref-supply: Phandle to the external reference voltage

supply. This should

Documentation/devicetree/bindings/iio/dac/ltc2632.txt +1 -0
+	  only be set if there is an external reference voltage

connected to the VREF

Documentation/devicetree/bindings/iio/dac/ltc2632.txt +1 -0
+	  pin. If the property is not set the internal reference

is used.
+
 Example:

Documentation/devicetree/bindings/iio/dac/ltc2632.txt +8 -0
+	vref: regulator-vref {
+		compatible = "regulator-fixed";
+		regulator-name = "vref-ltc2632";
+		regulator-min-microvolt = <1250000>;
+		regulator-max-microvolt = <1250000>;
+		regulator-always-on;
+	};
+

  spi_master {
  dac: ltc2632@0 {
  compatible = "lltc,ltc2632-l12"; reg = <0>; /* CS0 */ spi-max-frequency = <1000000>;

Documentation/devicetree/bindings/iio/dac/ltc2632.txt +1 -0
+			vref-supply = <&vref>; /* optional */

  };
  };

drivers/iio/dac/ltc2632.c +2 -0
index d322b78..9551156 100644
@@ -2,6 +2,7 @@
   * LTC2632 Digital to analog convertors spi driver
   *
   * Copyright 2017 Maxime Roussin-B_langer
+ * expanded by Silvan Murer <[email protected]>
   *
   * Licensed under the GPL-2.
   */
@@ -10,6 +11,7 @@
  #include <linux/spi/spi.h>
  #include <linux/module.h>
  #include <linux/iio/iio.h>
+#include <linux/regulator/consumer.h>


 #define LTC2632_DAC_CHANNELS                    2

drivers/iio/dac/ltc2632.c +1 -1
@@ -28,7 +30,7 @@
  /**
   * struct ltc2632_chip_info - chip specific information
   * @channels:		channel spec for the DAC
- * @vref_mv:		reference voltage
+ * @vref_mv:		internal reference voltage
   */
  struct ltc2632_chip_info {
  	const struct iio_chan_spec *channels;
@@ -39,10 +41,14 @@ struct ltc2632_chip_info {
   * struct ltc2632_state - driver instance specific data
   * @spi_dev:			pointer to the spi_device

struct
  * @powerdown_cache_mask used to show current channel
powerdown state
+ * @vref_mv used reference voltage
(internal or external)
+ * @vref_reg regulator for the reference voltage
  */
 struct ltc2632_state {
  struct spi_device *spi_dev;
  unsigned int powerdown_cache_mask;

drivers/iio/dac/ltc2632.c +2 -0
+	int vref_mv;
+	struct regulator *vref_reg;

 };

 enum ltc2632_supported_device_ids {

drivers/iio/dac/ltc2632.c +0 -0
@@ -90,7 +96,7 @@ static int ltc2632_read_raw(struct iio_dev
 *indio_dev,


  switch (m) {
  case IIO_CHAN_INFO_SCALE:

drivers/iio/dac/ltc2632.c +1 -1
-		*val = chip_info->vref_mv;
+		*val = st->vref_mv;

  *val2 = chan->scan_type.realbits;
  return IIO_VAL_FRACTIONAL_LOG2;
  }

drivers/iio/dac/ltc2632.c +0 -0
@@ -247,6 +253,45 @@ static int ltc2632_probe(struct spi_device
 *spi)
  	chip_info = (struct ltc2632_chip_info *)
  			spi_get_device_id(spi)->driver_data;

drivers/iio/dac/ltc2632.c +1 -0
+	st->vref_reg = devm_regulator_get_optional(&spi->dev,

"vref");

drivers/iio/dac/ltc2632.c +6 -0
+	if (PTR_ERR(st->vref_reg) == -ENODEV) {
+		/* use internal reference voltage */
+		st->vref_reg = NULL;
+		st->vref_mv = chip_info->vref_mv;
+
+		ret = ltc2632_spi_write(spi,

LTC2632_CMD_INTERNAL_REFER,

drivers/iio/dac/ltc2632.c +4 -0
+				0, 0, 0);
+		if (ret) {
+			dev_err(&spi->dev,
+				"Set internal reference command

failed, %d\n",

drivers/iio/dac/ltc2632.c +6 -0
+				ret);
+			return ret;
+		}
+	} else if (IS_ERR(st->vref_reg)) {
+		dev_err(&spi->dev,
+				"Error getting voltage reference

regulator\n");

drivers/iio/dac/ltc2632.c +7 -0
+		return PTR_ERR(st->vref_reg);
+	} else {
+		/* use external reference voltage */
+		ret = regulator_enable(st->vref_reg);
+		if (ret) {
+			dev_err(&spi->dev,
+				"enable reference regulator

failed, %d\n",

drivers/iio/dac/ltc2632.c +4 -0
+				ret);
+			return ret;
+		}
+		st->vref_mv = regulator_get_voltage(st->vref_reg)

/ 1000;

drivers/iio/dac/ltc2632.c +2 -0
+
+		ret = ltc2632_spi_write(spi,

LTC2632_CMD_EXTERNAL_REFER,

drivers/iio/dac/ltc2632.c +4 -0
+				0, 0, 0);
+		if (ret) {
+			dev_err(&spi->dev,
+				"Set external reference command

failed, %d\n",

drivers/iio/dac/ltc2632.c +5 -0
+				ret);
+			return ret;
+		}
+	}
+

  indio_dev->dev.parent = &spi->dev; indio_dev->name = dev_of_node(&spi->dev) ? dev_of_node(&spi->dev)->name
   :
spi_get_device_id(spi)->name;

drivers/iio/dac/ltc2632.c +0 -0
@@ -255,14 +300,20 @@ static int ltc2632_probe(struct spi_device
 *spi)
  	indio_dev->channels = chip_info->channels;
  	indio_dev->num_channels = LTC2632_DAC_CHANNELS;

drivers/iio/dac/ltc2632.c +0 -1
-	ret = ltc2632_spi_write(spi, LTC2632_CMD_INTERNAL_REFER,

0, 0, 0);

drivers/iio/dac/ltc2632.c +0 -3
-	if (ret) {
-		dev_err(&spi->dev,
-			"Set internal reference command failed,

%d\n", ret);

drivers/iio/dac/ltc2632.c +12 -2
-		return ret;
-	}
+	return iio_device_register(indio_dev);
+}
+
+static int ltc2632_remove(struct spi_device *spi)
+{
+	struct iio_dev *indio_dev = spi_get_drvdata(spi);
+	struct ltc2632_state *st = iio_priv(indio_dev);
+
+	iio_device_unregister(indio_dev);
+
+	if (st->vref_reg)
+		regulator_disable(st->vref_reg);

drivers/iio/dac/ltc2632.c +1 -1
-	return devm_iio_device_register(&spi->dev, indio_dev);
+	return 0;

 }

 static const struct spi_device_id ltc2632_id[] = {

drivers/iio/dac/ltc2632.c +1 -0
@@ -306,6 +357,7 @@ static struct spi_driver ltc2632_driver = {
  		.of_match_table = of_match_ptr(ltc2632_of_match),
  	},
  	.probe		= ltc2632_probe,
+	.remove		= ltc2632_remove,
  	.id_table	= ltc2632_id,
  };
  module_spi_driver(ltc2632_driver);