LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH v4 0/4] iio: add unit-converter
@ 2018-04-23 21:08 Peter Rosin
  2018-04-23 21:08 ` [PATCH v4 1/4] dt-bindings: iio: afe: add binding for current-sense-shunt Peter Rosin
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Peter Rosin @ 2018-04-23 21:08 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Rob Herring,
	Mark Rutland, David S. Miller, Greg Kroah-Hartman,
	Mauro Carvalho Chehab, Linus Walleij, Andrew Morton,
	Randy Dunlap, Andrew F . Davis, Fabio Estevam, linux-iio,
	devicetree

Hi!

This driver implements support for voltage dividers, current sense
shunts and current sense amplifiers. It's pretty generic and should
be easily adaptable to other linear scaling purposes.

Cheers,
Peter

Changes since v3:    https://lkml.org/lkml/2018/4/10/614
- Split the bindings patch for voltage-divider and current-sense-shunt
  in two, thus retaining the tag from Rob on the latter, when the
  former got a major update. The voltage-divider binding is now
  clear of its history from the generic io-channel-unit-converter
  binding roots (v1 of the series) and makes much more sense on its
  own (IMHO).
- Incorporate the confusing followup patches that added current sense
  amplification to the series.
- Drop the adi,lt6106 binding from that followup and replace it with
  a generic current-sense-amplifier binding.
- Adjust the driver to the above, and also rename it to iio-rescale
  as unit-converter was perhaps a little bit too generic even for the
  driver name.

Changes since v2:    https://lkml.org/lkml/2018/4/3/461
- Rename from current-sence-circuit to current-sense-shunt (this
  should also fix all the typing problems I had with curciut).
- Describe the shunt resistance directly (instead of indirectly
  in the form of a quotient).
- Add a ->props() op to struct unit_converter_cfg to enable simple
  separation of properties for different converters.
- Shuffle the code around to minimize forward declarations.
- Drop the unused indio member from struct unit_converter.
- Drop error report on iio driver registration failure.

Changes since v1:    https://lkml.org/lkml/2018/3/19/801
- Put the driver in the new afe category (Analog Front Ends) and do not
  move the iio-mux driver.
- Do not refer to the source channel as "parent", use "source" instead.
- Have the DT compatible drive the target unit, instead of relying on a
  "type" DT-property for that.
- In the DT bindings, use an unnamed source channel.
- Do not set up writes to _RAW (sorry Phil) as I don't need it and have
  not tested it. It's easy to add back if needed.
- Fail if the source channel does not support _RAW or _SCALE.
- Fix various spelling issues.
- Fix various code style issues.

Peter Rosin (4):
  dt-bindings: iio: afe: add binding for current-sense-shunt
  dt-bindings: iio: afe: add binding for voltage-divider
  dt-bindings: iio: afe: add binding for current-sense-amplifier
  iio: afe: rescale: new driver

 .../bindings/iio/afe/current-sense-amplifier.txt   |  26 ++
 .../bindings/iio/afe/current-sense-shunt.txt       |  41 +++
 .../bindings/iio/afe/voltage-divider.txt           |  53 +++
 MAINTAINERS                                        |   9 +
 drivers/iio/Kconfig                                |   1 +
 drivers/iio/Makefile                               |   1 +
 drivers/iio/afe/Kconfig                            |  19 ++
 drivers/iio/afe/Makefile                           |   6 +
 drivers/iio/afe/iio-rescale.c                      | 359 +++++++++++++++++++++
 9 files changed, 515 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt
 create mode 100644 Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
 create mode 100644 Documentation/devicetree/bindings/iio/afe/voltage-divider.txt
 create mode 100644 drivers/iio/afe/Kconfig
 create mode 100644 drivers/iio/afe/Makefile
 create mode 100644 drivers/iio/afe/iio-rescale.c

-- 
2.11.0

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4 1/4] dt-bindings: iio: afe: add binding for current-sense-shunt
  2018-04-23 21:08 [PATCH v4 0/4] iio: add unit-converter Peter Rosin
@ 2018-04-23 21:08 ` Peter Rosin
  2018-04-28 16:59   ` Jonathan Cameron
  2018-04-23 21:08 ` [PATCH v4 2/4] dt-bindings: iio: afe: add binding for voltage-divider Peter Rosin
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 11+ messages in thread
From: Peter Rosin @ 2018-04-23 21:08 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Rob Herring,
	Mark Rutland, David S. Miller, Greg Kroah-Hartman,
	Mauro Carvalho Chehab, Linus Walleij, Andrew Morton,
	Randy Dunlap, Andrew F . Davis, Fabio Estevam, linux-iio,
	devicetree

An ADC is often used to measure other quantities indirectly. This
binding describe one cases, a current through a shunt resistor
measured by the voltage over it.

Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Peter Rosin <peda@axentia.se>
---
 .../bindings/iio/afe/current-sense-shunt.txt       | 41 ++++++++++++++++++++++
 MAINTAINERS                                        |  6 ++++
 2 files changed, 47 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt

diff --git a/Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt b/Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
new file mode 100644
index 000000000000..4d842aa85040
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
@@ -0,0 +1,41 @@
+Current Sense Shunt
+===================
+
+When an io-channel measures the voltage over a current sense shunt,
+the interesting mesaurement is almost always the current through the
+shunt, not the voltage over it. This binding describes such a current
+sense circuit.
+
+Required properties:
+- compatible : "current-sense-shunt"
+- io-channels : Channel node of a voltage io-channel.
+- shunt-resistor-micro-ohms : The shunt resistance in microohms.
+
+Example:
+The system current is measured by measuring the voltage over a
+3.3 ohms shunt resistor.
+
+sysi {
+	compatible = "current-sense-shunt";
+	io-channels = <&tiadc 0>;
+
+	/* Divide the voltage by 3300000/1000000 (or 3.3) for the current. */
+	shunt-resistor-micro-ohms = <3300000>;
+};
+
+&i2c {
+	tiadc: adc@48 {
+		compatible = "ti,ads1015";
+	      	reg = <0x48>;
+		#io-channel-cells = <1>;
+
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		channel@0 { /* IN0,IN1 differential */
+			reg = <0>;
+			ti,gain = <1>;
+			ti,datarate = <4>;
+		};
+	};
+};
diff --git a/MAINTAINERS b/MAINTAINERS
index 36a28e979e9a..0f7bec878919 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6889,6 +6889,12 @@ F:	drivers/staging/iio/
 F:	include/linux/iio/
 F:	tools/iio/
 
+IIO UNIT CONVERTER
+M:	Peter Rosin <peda@axentia.se>
+L:	linux-iio@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
+
 IKANOS/ADI EAGLE ADSL USB DRIVER
 M:	Matthieu Castet <castet.matthieu@free.fr>
 M:	Stanislaw Gruszka <stf_xl@wp.pl>
-- 
2.11.0

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4 2/4] dt-bindings: iio: afe: add binding for voltage-divider
  2018-04-23 21:08 [PATCH v4 0/4] iio: add unit-converter Peter Rosin
  2018-04-23 21:08 ` [PATCH v4 1/4] dt-bindings: iio: afe: add binding for current-sense-shunt Peter Rosin
@ 2018-04-23 21:08 ` Peter Rosin
  2018-04-27 14:29   ` Rob Herring
  2018-04-23 21:08 ` [PATCH v4 3/4] dt-bindings: iio: afe: add binding for current-sense-amplifier Peter Rosin
  2018-04-23 21:08 ` [PATCH v4 4/4] iio: afe: rescale: new driver Peter Rosin
  3 siblings, 1 reply; 11+ messages in thread
From: Peter Rosin @ 2018-04-23 21:08 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Rob Herring,
	Mark Rutland, David S. Miller, Greg Kroah-Hartman,
	Mauro Carvalho Chehab, Linus Walleij, Andrew Morton,
	Randy Dunlap, Andrew F . Davis, Fabio Estevam, linux-iio,
	devicetree

An ADC is often used to measure other quantities indirectly. This
binding describe one cases, a "big" voltage measured with the help
of a voltage divider.

Signed-off-by: Peter Rosin <peda@axentia.se>
---
 .../bindings/iio/afe/voltage-divider.txt           | 53 ++++++++++++++++++++++
 MAINTAINERS                                        |  1 +
 2 files changed, 54 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/afe/voltage-divider.txt

diff --git a/Documentation/devicetree/bindings/iio/afe/voltage-divider.txt b/Documentation/devicetree/bindings/iio/afe/voltage-divider.txt
new file mode 100644
index 000000000000..afb52ce77c27
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/afe/voltage-divider.txt
@@ -0,0 +1,53 @@
+Voltage divider
+===============
+
+When an io-channel measures the midpoint of a voltage divider, the
+interesting voltage is often the voltage over the full resistance
+of the divider. This binding describes the voltage divider in such
+a curcuit.
+
+    Vin ----.
+            |
+         .-----.
+         |  R  |
+         '-----'
+            |
+            +---- Vout
+            |
+         .-----.
+         | Rout|
+         '-----'
+            |
+           GND
+
+Required properties:
+- compatible : "voltage-divider"
+- io-channels : Channel node of a voltage io-channel measuring Vout.
+- output-ohms : Resistance Rout over which the output voltage is measured.
+	        See full-ohms.
+- full-ohms : Resistance R + Rout for the full divider. The io-channel
+	      is scaled by the Rout / (R + Rout) quotient.
+
+Example:
+The system voltage is circa 12V, but divided down with a 22/222
+voltage divider (R = 200 Ohms, Rout = 22 Ohms) and fed to an ADC.
+
+sysv {
+	compatible = "voltage-divider";
+	io-channels = <&maxadc 1>;
+
+	/* Scale the system voltage by 22/222 to fit the ADC range. */
+	output-ohms = <22>;
+	full-ohms = <222>; /* 200 + 22 */
+};
+
+&spi {
+	maxadc: adc@0 {
+		compatible = "maxim,max1027";
+	      	reg = <0>;
+		#io-channel-cells = <1>;
+		interrupt-parent = <&gpio5>;
+		interrupts = <15 IRQ_TYPE_EDGE_RISING>;
+		spi-max-frequency = <1000000>;
+	};
+};
diff --git a/MAINTAINERS b/MAINTAINERS
index 0f7bec878919..4d534c9321de 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6894,6 +6894,7 @@ M:	Peter Rosin <peda@axentia.se>
 L:	linux-iio@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
+F:	Documentation/devicetree/bindings/iio/afe/voltage-divider.txt
 
 IKANOS/ADI EAGLE ADSL USB DRIVER
 M:	Matthieu Castet <castet.matthieu@free.fr>
-- 
2.11.0

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4 3/4] dt-bindings: iio: afe: add binding for current-sense-amplifier
  2018-04-23 21:08 [PATCH v4 0/4] iio: add unit-converter Peter Rosin
  2018-04-23 21:08 ` [PATCH v4 1/4] dt-bindings: iio: afe: add binding for current-sense-shunt Peter Rosin
  2018-04-23 21:08 ` [PATCH v4 2/4] dt-bindings: iio: afe: add binding for voltage-divider Peter Rosin
@ 2018-04-23 21:08 ` Peter Rosin
  2018-04-27 14:30   ` Rob Herring
  2018-04-23 21:08 ` [PATCH v4 4/4] iio: afe: rescale: new driver Peter Rosin
  3 siblings, 1 reply; 11+ messages in thread
From: Peter Rosin @ 2018-04-23 21:08 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Rob Herring,
	Mark Rutland, David S. Miller, Greg Kroah-Hartman,
	Mauro Carvalho Chehab, Linus Walleij, Andrew Morton,
	Randy Dunlap, Andrew F . Davis, Fabio Estevam, linux-iio,
	devicetree

Similar to current sense shunts, but an amplifier enables the use
of a smaller sense resistance.

Signed-off-by: Peter Rosin <peda@axentia.se>
---
 .../bindings/iio/afe/current-sense-amplifier.txt   | 26 ++++++++++++++++++++++
 MAINTAINERS                                        |  1 +
 2 files changed, 27 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt

diff --git a/Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt b/Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt
new file mode 100644
index 000000000000..0ddbaebba8ce
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt
@@ -0,0 +1,26 @@
+Current Sense Amplifier
+=======================
+
+When an io-channel measures the output voltage from a current sense
+amplifier, the interesting mesaurement is almost always the current
+through the sense resistor, not the voltage output. This binding
+describes such a current sense circuit.
+
+Required properties:
+- compatible : "current-sense-amplifier"
+- io-channels : Channel node of a voltage io-channel.
+- sense-resistor-micro-ohms : The sense resistance in microohms.
+
+Optional properties:
+- sense-gain-mult: Amplifier gain multiplier. The default is <1>.
+- sense-gain-div: Amplifier gain divider. The default is <1>.
+
+Example:
+
+sysi {
+	compatible = "current-sense-amplifier";
+	io-channels = <&tiadc 0>;
+
+	sense-resistor-micro-ohms = <20000>;
+	sense-gain-mul = <50>;
+};
diff --git a/MAINTAINERS b/MAINTAINERS
index 4d534c9321de..1b23e9ebff9c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6893,6 +6893,7 @@ IIO UNIT CONVERTER
 M:	Peter Rosin <peda@axentia.se>
 L:	linux-iio@vger.kernel.org
 S:	Maintained
+F:	Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt
 F:	Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
 F:	Documentation/devicetree/bindings/iio/afe/voltage-divider.txt
 
-- 
2.11.0

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4 4/4] iio: afe: rescale: new driver
  2018-04-23 21:08 [PATCH v4 0/4] iio: add unit-converter Peter Rosin
                   ` (2 preceding siblings ...)
  2018-04-23 21:08 ` [PATCH v4 3/4] dt-bindings: iio: afe: add binding for current-sense-amplifier Peter Rosin
@ 2018-04-23 21:08 ` Peter Rosin
  2018-04-28 17:05   ` Jonathan Cameron
  3 siblings, 1 reply; 11+ messages in thread
From: Peter Rosin @ 2018-04-23 21:08 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Rob Herring,
	Mark Rutland, David S. Miller, Greg Kroah-Hartman,
	Mauro Carvalho Chehab, Linus Walleij, Andrew Morton,
	Randy Dunlap, Andrew F . Davis, Fabio Estevam, linux-iio,
	devicetree

If an ADC channel measures the midpoint of a voltage divider, the
interesting voltage is often the voltage over the full resistance.
E.g. if the full voltage is too big for the ADC to handle.
Likewise, if an ADC channel measures the voltage across a shunt
resistor, with or without amplification, the interesting value is
often the current through the resistor.

This driver solves these problems by allowing to linearly scale a channel
and/or by allowing changes to the type of the channel.

Signed-off-by: Peter Rosin <peda@axentia.se>
---
 MAINTAINERS                   |   1 +
 drivers/iio/Kconfig           |   1 +
 drivers/iio/Makefile          |   1 +
 drivers/iio/afe/Kconfig       |  19 +++
 drivers/iio/afe/Makefile      |   6 +
 drivers/iio/afe/iio-rescale.c | 359 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 387 insertions(+)
 create mode 100644 drivers/iio/afe/Kconfig
 create mode 100644 drivers/iio/afe/Makefile
 create mode 100644 drivers/iio/afe/iio-rescale.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 1b23e9ebff9c..73b8fc5dd2a1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6896,6 +6896,7 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt
 F:	Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
 F:	Documentation/devicetree/bindings/iio/afe/voltage-divider.txt
+F:	drivers/iio/afe/iio-rescale.c
 
 IKANOS/ADI EAGLE ADSL USB DRIVER
 M:	Matthieu Castet <castet.matthieu@free.fr>
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index b3c8c6ef0dff..d69e85a8bdc3 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -70,6 +70,7 @@ config IIO_TRIGGERED_EVENT
 
 source "drivers/iio/accel/Kconfig"
 source "drivers/iio/adc/Kconfig"
+source "drivers/iio/afe/Kconfig"
 source "drivers/iio/amplifiers/Kconfig"
 source "drivers/iio/chemical/Kconfig"
 source "drivers/iio/common/Kconfig"
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index b16b2e9ddc40..d8cba9c229c0 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_IIO_TRIGGERED_EVENT) += industrialio-triggered-event.o
 
 obj-y += accel/
 obj-y += adc/
+obj-y += afe/
 obj-y += amplifiers/
 obj-y += buffer/
 obj-y += chemical/
diff --git a/drivers/iio/afe/Kconfig b/drivers/iio/afe/Kconfig
new file mode 100644
index 000000000000..c91eef04825a
--- /dev/null
+++ b/drivers/iio/afe/Kconfig
@@ -0,0 +1,19 @@
+#
+# Analog Front End drivers
+#
+# When adding new entries keep the list in alphabetical order
+
+menu "Analog Front Ends"
+
+config IIO_RESCALE
+	tristate "IIO rescale"
+	depends on OF || COMPILE_TEST
+	help
+	  Say yes here to build support for the IIO rescaling
+	  that handles voltage dividers, current sense shunts and
+	  current sense amplifiers.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called iio-rescale.
+
+endmenu
diff --git a/drivers/iio/afe/Makefile b/drivers/iio/afe/Makefile
new file mode 100644
index 000000000000..5fabb7bcac47
--- /dev/null
+++ b/drivers/iio/afe/Makefile
@@ -0,0 +1,6 @@
+#
+# Makefile for industrial I/O Analog Front Ends (AFE)
+#
+
+# When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_IIO_RESCALE) += iio-rescale.o
diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
new file mode 100644
index 000000000000..e9ceee66d1e7
--- /dev/null
+++ b/drivers/iio/afe/iio-rescale.c
@@ -0,0 +1,359 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * IIO rescale driver
+ *
+ * Copyright (C) 2018 Axentia Technologies AB
+ *
+ * Author: Peter Rosin <peda@axentia.se>
+ */
+
+#include <linux/err.h>
+#include <linux/gcd.h>
+#include <linux/iio/consumer.h>
+#include <linux/iio/iio.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+
+struct rescale;
+
+struct rescale_cfg {
+	enum iio_chan_type type;
+	int (*props)(struct device *dev, struct rescale *rescale);
+};
+
+struct rescale {
+	const struct rescale_cfg *cfg;
+	struct iio_channel *source;
+	struct iio_chan_spec chan;
+	struct iio_chan_spec_ext_info *ext_info;
+	s32 numerator;
+	s32 denominator;
+};
+
+static int rescale_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long mask)
+{
+	struct rescale *rescale = iio_priv(indio_dev);
+	unsigned long long tmp;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		return iio_read_channel_raw(rescale->source, val);
+
+	case IIO_CHAN_INFO_SCALE:
+		ret = iio_read_channel_scale(rescale->source, val, val2);
+		switch (ret) {
+		case IIO_VAL_FRACTIONAL:
+			*val *= rescale->numerator;
+			*val2 *= rescale->denominator;
+			return ret;
+		case IIO_VAL_INT:
+			*val *= rescale->numerator;
+			if (rescale->denominator == 1)
+				return ret;
+			*val2 = rescale->denominator;
+			return IIO_VAL_FRACTIONAL;
+		case IIO_VAL_FRACTIONAL_LOG2:
+			tmp = *val * 1000000000LL;
+			do_div(tmp, rescale->denominator);
+			tmp *= rescale->numerator;
+			do_div(tmp, 1000000000LL);
+			*val = tmp;
+			return ret;
+		default:
+			return -EOPNOTSUPP;
+		}
+	default:
+		return -EINVAL;
+	}
+}
+
+static int rescale_read_avail(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      const int **vals, int *type, int *length,
+			      long mask)
+{
+	struct rescale *rescale = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		*type = IIO_VAL_INT;
+		return iio_read_avail_channel_raw(rescale->source,
+						  vals, length);
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info rescale_info = {
+	.read_raw = rescale_read_raw,
+	.read_avail = rescale_read_avail,
+};
+
+static ssize_t rescale_read_ext_info(struct iio_dev *indio_dev,
+				     uintptr_t private,
+				     struct iio_chan_spec const *chan,
+				     char *buf)
+{
+	struct rescale *rescale = iio_priv(indio_dev);
+
+	return iio_read_channel_ext_info(rescale->source,
+					 rescale->ext_info[private].name,
+					 buf);
+}
+
+static ssize_t rescale_write_ext_info(struct iio_dev *indio_dev,
+				      uintptr_t private,
+				      struct iio_chan_spec const *chan,
+				      const char *buf, size_t len)
+{
+	struct rescale *rescale = iio_priv(indio_dev);
+
+	return iio_write_channel_ext_info(rescale->source,
+					  rescale->ext_info[private].name,
+					  buf, len);
+}
+
+static int rescale_configure_channel(struct device *dev,
+				     struct rescale *rescale)
+{
+	struct iio_chan_spec *chan = &rescale->chan;
+	struct iio_chan_spec const *schan = rescale->source->channel;
+
+	chan->indexed = 1;
+	chan->output = schan->output;
+	chan->ext_info = rescale->ext_info;
+	chan->type = rescale->cfg->type;
+
+	if (!iio_channel_has_info(schan, IIO_CHAN_INFO_RAW) ||
+	    !iio_channel_has_info(schan, IIO_CHAN_INFO_SCALE)) {
+		dev_err(dev, "source channel does not support raw/scale\n");
+		return -EINVAL;
+	}
+
+	chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+		BIT(IIO_CHAN_INFO_SCALE);
+
+	if (iio_channel_has_available(schan, IIO_CHAN_INFO_RAW))
+		chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW);
+
+	return 0;
+}
+
+static int rescale_current_sense_amplifier_props(struct device *dev,
+						 struct rescale *rescale)
+{
+	u32 sense;
+	u32 gain_mult = 1;
+	u32 gain_div = 1;
+	u32 factor;
+	int ret;
+
+	ret = device_property_read_u32(dev, "sense-resistor-micro-ohms",
+				       &sense);
+	if (ret) {
+		dev_err(dev, "failed to read the sense resistance: %d\n", ret);
+		return ret;
+	}
+
+	device_property_read_u32(dev, "sense-gain-mult", &gain_mult);
+	device_property_read_u32(dev, "sense-gain-div", &gain_div);
+
+	/*
+	 * Calculate the scaling factor, 1 / (gain * sense), or
+	 * gain_div / (gain_mult * sense), while trying to keep the
+	 * numerator/denominator from overflowing.
+	 */
+	factor = gcd(sense, 1000000);
+	rescale->numerator = 1000000 / factor;
+	rescale->denominator = sense / factor;
+
+	factor = gcd(rescale->numerator, gain_mult);
+	rescale->numerator /= factor;
+	rescale->denominator *= gain_mult / factor;
+
+	factor = gcd(rescale->denominator, gain_div);
+	rescale->numerator *= gain_div / factor;
+	rescale->denominator /= factor;
+
+	return 0;
+}
+
+static int rescale_current_sense_shunt_props(struct device *dev,
+					     struct rescale *rescale)
+{
+	u32 shunt;
+	u32 factor;
+	int ret;
+
+	ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
+				       &shunt);
+	if (ret) {
+		dev_err(dev, "failed to read the shunt resistance: %d\n", ret);
+		return ret;
+	}
+
+	factor = gcd(shunt, 1000000);
+	rescale->numerator = 1000000 / factor;
+	rescale->denominator = shunt / factor;
+
+	return 0;
+}
+
+static int rescale_voltage_divider_props(struct device *dev,
+					 struct rescale *rescale)
+{
+	int ret;
+	u32 factor;
+
+	ret = device_property_read_u32(dev, "output-ohms",
+				       &rescale->denominator);
+	if (ret) {
+		dev_err(dev, "failed to read output-ohms: %d\n", ret);
+		return ret;
+	}
+
+	ret = device_property_read_u32(dev, "full-ohms",
+				       &rescale->numerator);
+	if (ret) {
+		dev_err(dev, "failed to read full-ohms: %d\n", ret);
+		return ret;
+	}
+
+	factor = gcd(rescale->numerator, rescale->denominator);
+	rescale->numerator /= factor;
+	rescale->denominator /= factor;
+
+	return 0;
+}
+
+enum rescale_variant {
+	CURRENT_SENSE_AMPLIFIER,
+	CURRENT_SENSE_SHUNT,
+	VOLTAGE_DIVIDER,
+};
+
+static const struct rescale_cfg rescale_cfg[] = {
+	[CURRENT_SENSE_AMPLIFIER] = {
+		.type = IIO_CURRENT,
+		.props = rescale_current_sense_amplifier_props,
+	},
+	[CURRENT_SENSE_SHUNT] = {
+		.type = IIO_CURRENT,
+		.props = rescale_current_sense_shunt_props,
+	},
+	[VOLTAGE_DIVIDER] = {
+		.type = IIO_VOLTAGE,
+		.props = rescale_voltage_divider_props,
+	},
+};
+
+static const struct of_device_id rescale_match[] = {
+	{ .compatible = "current-sense-amplifier",
+	  .data = &rescale_cfg[CURRENT_SENSE_AMPLIFIER], },
+	{ .compatible = "current-sense-shunt",
+	  .data = &rescale_cfg[CURRENT_SENSE_SHUNT], },
+	{ .compatible = "voltage-divider",
+	  .data = &rescale_cfg[VOLTAGE_DIVIDER], },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, rescale_match);
+
+static int rescale_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct iio_dev *indio_dev;
+	struct iio_channel *source;
+	struct rescale *rescale;
+	int sizeof_ext_info;
+	int sizeof_priv;
+	int i;
+	int ret;
+
+	source = devm_iio_channel_get(dev, NULL);
+	if (IS_ERR(source)) {
+		if (PTR_ERR(source) != -EPROBE_DEFER)
+			dev_err(dev, "failed to get source channel\n");
+		return PTR_ERR(source);
+	}
+
+	sizeof_ext_info = iio_get_channel_ext_info_count(source);
+	if (sizeof_ext_info) {
+		sizeof_ext_info += 1; /* one extra entry for the sentinel */
+		sizeof_ext_info *= sizeof(*rescale->ext_info);
+	}
+
+	sizeof_priv = sizeof(*rescale) + sizeof_ext_info;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof_priv);
+	if (!indio_dev)
+		return -ENOMEM;
+
+	rescale = iio_priv(indio_dev);
+
+	rescale->cfg = of_device_get_match_data(dev);
+	rescale->numerator = 1;
+	rescale->denominator = 1;
+
+	ret = rescale->cfg->props(dev, rescale);
+	if (ret)
+		return ret;
+
+	if (!rescale->numerator || !rescale->denominator) {
+		dev_err(dev, "invalid scaling factor.\n");
+		return -EINVAL;
+	}
+
+	platform_set_drvdata(pdev, indio_dev);
+
+	rescale->source = source;
+
+	indio_dev->name = dev_name(dev);
+	indio_dev->dev.parent = dev;
+	indio_dev->info = &rescale_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = &rescale->chan;
+	indio_dev->num_channels = 1;
+	if (sizeof_ext_info) {
+		rescale->ext_info = devm_kmemdup(dev,
+						 source->channel->ext_info,
+						 sizeof_ext_info, GFP_KERNEL);
+		if (!rescale->ext_info)
+			return -ENOMEM;
+
+		for (i = 0; rescale->ext_info[i].name; ++i) {
+			struct iio_chan_spec_ext_info *ext_info =
+				&rescale->ext_info[i];
+
+			if (source->channel->ext_info[i].read)
+				ext_info->read = rescale_read_ext_info;
+			if (source->channel->ext_info[i].write)
+				ext_info->write = rescale_write_ext_info;
+			ext_info->private = i;
+		}
+	}
+
+	ret = rescale_configure_channel(dev, rescale);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static struct platform_driver rescale_driver = {
+	.probe = rescale_probe,
+	.driver = {
+		.name = "iio-rescale",
+		.of_match_table = rescale_match,
+	},
+};
+module_platform_driver(rescale_driver);
+
+MODULE_DESCRIPTION("IIO rescale driver");
+MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
+MODULE_LICENSE("GPL v2");
-- 
2.11.0

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 2/4] dt-bindings: iio: afe: add binding for voltage-divider
  2018-04-23 21:08 ` [PATCH v4 2/4] dt-bindings: iio: afe: add binding for voltage-divider Peter Rosin
@ 2018-04-27 14:29   ` Rob Herring
  2018-04-28 17:01     ` Jonathan Cameron
  0 siblings, 1 reply; 11+ messages in thread
From: Rob Herring @ 2018-04-27 14:29 UTC (permalink / raw)
  To: Peter Rosin
  Cc: linux-kernel, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Mark Rutland,
	David S. Miller, Greg Kroah-Hartman, Mauro Carvalho Chehab,
	Linus Walleij, Andrew Morton, Randy Dunlap, Andrew F . Davis,
	Fabio Estevam, linux-iio, devicetree

On Mon, Apr 23, 2018 at 11:08:07PM +0200, Peter Rosin wrote:
> An ADC is often used to measure other quantities indirectly. This
> binding describe one cases, a "big" voltage measured with the help
> of a voltage divider.
> 
> Signed-off-by: Peter Rosin <peda@axentia.se>
> ---
>  .../bindings/iio/afe/voltage-divider.txt           | 53 ++++++++++++++++++++++
>  MAINTAINERS                                        |  1 +
>  2 files changed, 54 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/afe/voltage-divider.txt

Reviewed-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 3/4] dt-bindings: iio: afe: add binding for current-sense-amplifier
  2018-04-23 21:08 ` [PATCH v4 3/4] dt-bindings: iio: afe: add binding for current-sense-amplifier Peter Rosin
@ 2018-04-27 14:30   ` Rob Herring
  2018-04-28 17:02     ` Jonathan Cameron
  0 siblings, 1 reply; 11+ messages in thread
From: Rob Herring @ 2018-04-27 14:30 UTC (permalink / raw)
  To: Peter Rosin
  Cc: linux-kernel, Jonathan Cameron, Hartmut Knaack,
	Lars-Peter Clausen, Peter Meerwald-Stadler, Mark Rutland,
	David S. Miller, Greg Kroah-Hartman, Mauro Carvalho Chehab,
	Linus Walleij, Andrew Morton, Randy Dunlap, Andrew F . Davis,
	Fabio Estevam, linux-iio, devicetree

On Mon, Apr 23, 2018 at 11:08:08PM +0200, Peter Rosin wrote:
> Similar to current sense shunts, but an amplifier enables the use
> of a smaller sense resistance.
> 
> Signed-off-by: Peter Rosin <peda@axentia.se>
> ---
>  .../bindings/iio/afe/current-sense-amplifier.txt   | 26 ++++++++++++++++++++++
>  MAINTAINERS                                        |  1 +
>  2 files changed, 27 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt

Reviewed-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/4] dt-bindings: iio: afe: add binding for current-sense-shunt
  2018-04-23 21:08 ` [PATCH v4 1/4] dt-bindings: iio: afe: add binding for current-sense-shunt Peter Rosin
@ 2018-04-28 16:59   ` Jonathan Cameron
  0 siblings, 0 replies; 11+ messages in thread
From: Jonathan Cameron @ 2018-04-28 16:59 UTC (permalink / raw)
  To: Peter Rosin
  Cc: linux-kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, Rob Herring, Mark Rutland,
	David S. Miller, Greg Kroah-Hartman, Mauro Carvalho Chehab,
	Linus Walleij, Andrew Morton, Randy Dunlap, Andrew F . Davis,
	Fabio Estevam, linux-iio, devicetree

On Mon, 23 Apr 2018 23:08:06 +0200
Peter Rosin <peda@axentia.se> wrote:

> An ADC is often used to measure other quantities indirectly. This
> binding describe one cases, a current through a shunt resistor
> measured by the voltage over it.
> 
> Reviewed-by: Rob Herring <robh@kernel.org>
> Signed-off-by: Peter Rosin <peda@axentia.se>
One small whitespace issue inline that git am pointed out.

I fixed it.

Thanks,

Jonathan

> ---
>  .../bindings/iio/afe/current-sense-shunt.txt       | 41 ++++++++++++++++++++++
>  MAINTAINERS                                        |  6 ++++
>  2 files changed, 47 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
> 
> diff --git a/Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt b/Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
> new file mode 100644
> index 000000000000..4d842aa85040
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
> @@ -0,0 +1,41 @@
> +Current Sense Shunt
> +===================
> +
> +When an io-channel measures the voltage over a current sense shunt,
> +the interesting mesaurement is almost always the current through the
> +shunt, not the voltage over it. This binding describes such a current
> +sense circuit.
> +
> +Required properties:
> +- compatible : "current-sense-shunt"
> +- io-channels : Channel node of a voltage io-channel.
> +- shunt-resistor-micro-ohms : The shunt resistance in microohms.
> +
> +Example:
> +The system current is measured by measuring the voltage over a
> +3.3 ohms shunt resistor.
> +
> +sysi {
> +	compatible = "current-sense-shunt";
> +	io-channels = <&tiadc 0>;
> +
> +	/* Divide the voltage by 3300000/1000000 (or 3.3) for the current. */
> +	shunt-resistor-micro-ohms = <3300000>;
> +};
> +
> +&i2c {
> +	tiadc: adc@48 {
> +		compatible = "ti,ads1015";
> +	      	reg = <0x48>;

The line above has some stray spaces - cleaned up.

> +		#io-channel-cells = <1>;
> +
> +		#address-cells = <1>;
> +		#size-cells = <0>;
> +
> +		channel@0 { /* IN0,IN1 differential */
> +			reg = <0>;
> +			ti,gain = <1>;
> +			ti,datarate = <4>;
> +		};
> +	};
> +};
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 36a28e979e9a..0f7bec878919 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -6889,6 +6889,12 @@ F:	drivers/staging/iio/
>  F:	include/linux/iio/
>  F:	tools/iio/
>  
> +IIO UNIT CONVERTER
> +M:	Peter Rosin <peda@axentia.se>
> +L:	linux-iio@vger.kernel.org
> +S:	Maintained
> +F:	Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
> +
>  IKANOS/ADI EAGLE ADSL USB DRIVER
>  M:	Matthieu Castet <castet.matthieu@free.fr>
>  M:	Stanislaw Gruszka <stf_xl@wp.pl>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 2/4] dt-bindings: iio: afe: add binding for voltage-divider
  2018-04-27 14:29   ` Rob Herring
@ 2018-04-28 17:01     ` Jonathan Cameron
  0 siblings, 0 replies; 11+ messages in thread
From: Jonathan Cameron @ 2018-04-28 17:01 UTC (permalink / raw)
  To: Rob Herring
  Cc: Peter Rosin, linux-kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, Mark Rutland, David S. Miller,
	Greg Kroah-Hartman, Mauro Carvalho Chehab, Linus Walleij,
	Andrew Morton, Randy Dunlap, Andrew F . Davis, Fabio Estevam,
	linux-iio, devicetree

On Fri, 27 Apr 2018 09:29:17 -0500
Rob Herring <robh@kernel.org> wrote:

> On Mon, Apr 23, 2018 at 11:08:07PM +0200, Peter Rosin wrote:
> > An ADC is often used to measure other quantities indirectly. This
> > binding describe one cases, a "big" voltage measured with the help
> > of a voltage divider.
> > 
> > Signed-off-by: Peter Rosin <peda@axentia.se>
> > ---
> >  .../bindings/iio/afe/voltage-divider.txt           | 53 ++++++++++++++++++++++
> >  MAINTAINERS                                        |  1 +
> >  2 files changed, 54 insertions(+)
> >  create mode 100644 Documentation/devicetree/bindings/iio/afe/voltage-divider.txt  
> 
> Reviewed-by: Rob Herring <robh@kernel.org>

Same white space issue. Fixed.

Applied to the togreg branch of iio.git and pushed out as testing.

Thanks,

Jonathan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 3/4] dt-bindings: iio: afe: add binding for current-sense-amplifier
  2018-04-27 14:30   ` Rob Herring
@ 2018-04-28 17:02     ` Jonathan Cameron
  0 siblings, 0 replies; 11+ messages in thread
From: Jonathan Cameron @ 2018-04-28 17:02 UTC (permalink / raw)
  To: Rob Herring
  Cc: Peter Rosin, linux-kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, Mark Rutland, David S. Miller,
	Greg Kroah-Hartman, Mauro Carvalho Chehab, Linus Walleij,
	Andrew Morton, Randy Dunlap, Andrew F . Davis, Fabio Estevam,
	linux-iio, devicetree

On Fri, 27 Apr 2018 09:30:25 -0500
Rob Herring <robh@kernel.org> wrote:

> On Mon, Apr 23, 2018 at 11:08:08PM +0200, Peter Rosin wrote:
> > Similar to current sense shunts, but an amplifier enables the use
> > of a smaller sense resistance.
> > 
> > Signed-off-by: Peter Rosin <peda@axentia.se>
> > ---
> >  .../bindings/iio/afe/current-sense-amplifier.txt   | 26 ++++++++++++++++++++++
> >  MAINTAINERS                                        |  1 +
> >  2 files changed, 27 insertions(+)
> >  create mode 100644 Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt  
> 
> Reviewed-by: Rob Herring <robh@kernel.org>
> 
Applied

Thanks,

Jonathan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 4/4] iio: afe: rescale: new driver
  2018-04-23 21:08 ` [PATCH v4 4/4] iio: afe: rescale: new driver Peter Rosin
@ 2018-04-28 17:05   ` Jonathan Cameron
  0 siblings, 0 replies; 11+ messages in thread
From: Jonathan Cameron @ 2018-04-28 17:05 UTC (permalink / raw)
  To: Peter Rosin
  Cc: linux-kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, Rob Herring, Mark Rutland,
	David S. Miller, Greg Kroah-Hartman, Mauro Carvalho Chehab,
	Linus Walleij, Andrew Morton, Randy Dunlap, Andrew F . Davis,
	Fabio Estevam, linux-iio, devicetree

On Mon, 23 Apr 2018 23:08:09 +0200
Peter Rosin <peda@axentia.se> wrote:

> If an ADC channel measures the midpoint of a voltage divider, the
> interesting voltage is often the voltage over the full resistance.
> E.g. if the full voltage is too big for the ADC to handle.
> Likewise, if an ADC channel measures the voltage across a shunt
> resistor, with or without amplification, the interesting value is
> often the current through the resistor.
> 
> This driver solves these problems by allowing to linearly scale a channel
> and/or by allowing changes to the type of the channel.
> 
> Signed-off-by: Peter Rosin <peda@axentia.se>

Great work.

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

Thanks,

Jonathan

> ---
>  MAINTAINERS                   |   1 +
>  drivers/iio/Kconfig           |   1 +
>  drivers/iio/Makefile          |   1 +
>  drivers/iio/afe/Kconfig       |  19 +++
>  drivers/iio/afe/Makefile      |   6 +
>  drivers/iio/afe/iio-rescale.c | 359 ++++++++++++++++++++++++++++++++++++++++++
>  6 files changed, 387 insertions(+)
>  create mode 100644 drivers/iio/afe/Kconfig
>  create mode 100644 drivers/iio/afe/Makefile
>  create mode 100644 drivers/iio/afe/iio-rescale.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 1b23e9ebff9c..73b8fc5dd2a1 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -6896,6 +6896,7 @@ S:	Maintained
>  F:	Documentation/devicetree/bindings/iio/afe/current-sense-amplifier.txt
>  F:	Documentation/devicetree/bindings/iio/afe/current-sense-shunt.txt
>  F:	Documentation/devicetree/bindings/iio/afe/voltage-divider.txt
> +F:	drivers/iio/afe/iio-rescale.c
>  
>  IKANOS/ADI EAGLE ADSL USB DRIVER
>  M:	Matthieu Castet <castet.matthieu@free.fr>
> diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
> index b3c8c6ef0dff..d69e85a8bdc3 100644
> --- a/drivers/iio/Kconfig
> +++ b/drivers/iio/Kconfig
> @@ -70,6 +70,7 @@ config IIO_TRIGGERED_EVENT
>  
>  source "drivers/iio/accel/Kconfig"
>  source "drivers/iio/adc/Kconfig"
> +source "drivers/iio/afe/Kconfig"
>  source "drivers/iio/amplifiers/Kconfig"
>  source "drivers/iio/chemical/Kconfig"
>  source "drivers/iio/common/Kconfig"
> diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
> index b16b2e9ddc40..d8cba9c229c0 100644
> --- a/drivers/iio/Makefile
> +++ b/drivers/iio/Makefile
> @@ -15,6 +15,7 @@ obj-$(CONFIG_IIO_TRIGGERED_EVENT) += industrialio-triggered-event.o
>  
>  obj-y += accel/
>  obj-y += adc/
> +obj-y += afe/
>  obj-y += amplifiers/
>  obj-y += buffer/
>  obj-y += chemical/
> diff --git a/drivers/iio/afe/Kconfig b/drivers/iio/afe/Kconfig
> new file mode 100644
> index 000000000000..c91eef04825a
> --- /dev/null
> +++ b/drivers/iio/afe/Kconfig
> @@ -0,0 +1,19 @@
> +#
> +# Analog Front End drivers
> +#
> +# When adding new entries keep the list in alphabetical order
> +
> +menu "Analog Front Ends"
> +
> +config IIO_RESCALE
> +	tristate "IIO rescale"
> +	depends on OF || COMPILE_TEST
> +	help
> +	  Say yes here to build support for the IIO rescaling
> +	  that handles voltage dividers, current sense shunts and
> +	  current sense amplifiers.
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called iio-rescale.
> +
> +endmenu
> diff --git a/drivers/iio/afe/Makefile b/drivers/iio/afe/Makefile
> new file mode 100644
> index 000000000000..5fabb7bcac47
> --- /dev/null
> +++ b/drivers/iio/afe/Makefile
> @@ -0,0 +1,6 @@
> +#
> +# Makefile for industrial I/O Analog Front Ends (AFE)
> +#
> +
> +# When adding new entries keep the list in alphabetical order
> +obj-$(CONFIG_IIO_RESCALE) += iio-rescale.o
> diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
> new file mode 100644
> index 000000000000..e9ceee66d1e7
> --- /dev/null
> +++ b/drivers/iio/afe/iio-rescale.c
> @@ -0,0 +1,359 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * IIO rescale driver
> + *
> + * Copyright (C) 2018 Axentia Technologies AB
> + *
> + * Author: Peter Rosin <peda@axentia.se>
> + */
> +
> +#include <linux/err.h>
> +#include <linux/gcd.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/iio/iio.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/property.h>
> +
> +struct rescale;
> +
> +struct rescale_cfg {
> +	enum iio_chan_type type;
> +	int (*props)(struct device *dev, struct rescale *rescale);
> +};
> +
> +struct rescale {
> +	const struct rescale_cfg *cfg;
> +	struct iio_channel *source;
> +	struct iio_chan_spec chan;
> +	struct iio_chan_spec_ext_info *ext_info;
> +	s32 numerator;
> +	s32 denominator;
> +};
> +
> +static int rescale_read_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan,
> +			    int *val, int *val2, long mask)
> +{
> +	struct rescale *rescale = iio_priv(indio_dev);
> +	unsigned long long tmp;
> +	int ret;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		return iio_read_channel_raw(rescale->source, val);
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		ret = iio_read_channel_scale(rescale->source, val, val2);
> +		switch (ret) {
> +		case IIO_VAL_FRACTIONAL:
> +			*val *= rescale->numerator;
> +			*val2 *= rescale->denominator;
> +			return ret;
> +		case IIO_VAL_INT:
> +			*val *= rescale->numerator;
> +			if (rescale->denominator == 1)
> +				return ret;
> +			*val2 = rescale->denominator;
> +			return IIO_VAL_FRACTIONAL;
> +		case IIO_VAL_FRACTIONAL_LOG2:
> +			tmp = *val * 1000000000LL;
> +			do_div(tmp, rescale->denominator);
> +			tmp *= rescale->numerator;
> +			do_div(tmp, 1000000000LL);
> +			*val = tmp;
> +			return ret;
> +		default:
> +			return -EOPNOTSUPP;
> +		}
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int rescale_read_avail(struct iio_dev *indio_dev,
> +			      struct iio_chan_spec const *chan,
> +			      const int **vals, int *type, int *length,
> +			      long mask)
> +{
> +	struct rescale *rescale = iio_priv(indio_dev);
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		*type = IIO_VAL_INT;
> +		return iio_read_avail_channel_raw(rescale->source,
> +						  vals, length);
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static const struct iio_info rescale_info = {
> +	.read_raw = rescale_read_raw,
> +	.read_avail = rescale_read_avail,
> +};
> +
> +static ssize_t rescale_read_ext_info(struct iio_dev *indio_dev,
> +				     uintptr_t private,
> +				     struct iio_chan_spec const *chan,
> +				     char *buf)
> +{
> +	struct rescale *rescale = iio_priv(indio_dev);
> +
> +	return iio_read_channel_ext_info(rescale->source,
> +					 rescale->ext_info[private].name,
> +					 buf);
> +}
> +
> +static ssize_t rescale_write_ext_info(struct iio_dev *indio_dev,
> +				      uintptr_t private,
> +				      struct iio_chan_spec const *chan,
> +				      const char *buf, size_t len)
> +{
> +	struct rescale *rescale = iio_priv(indio_dev);
> +
> +	return iio_write_channel_ext_info(rescale->source,
> +					  rescale->ext_info[private].name,
> +					  buf, len);
> +}
> +
> +static int rescale_configure_channel(struct device *dev,
> +				     struct rescale *rescale)
> +{
> +	struct iio_chan_spec *chan = &rescale->chan;
> +	struct iio_chan_spec const *schan = rescale->source->channel;
> +
> +	chan->indexed = 1;
> +	chan->output = schan->output;
> +	chan->ext_info = rescale->ext_info;
> +	chan->type = rescale->cfg->type;
> +
> +	if (!iio_channel_has_info(schan, IIO_CHAN_INFO_RAW) ||
> +	    !iio_channel_has_info(schan, IIO_CHAN_INFO_SCALE)) {
> +		dev_err(dev, "source channel does not support raw/scale\n");
> +		return -EINVAL;
> +	}
> +
> +	chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +		BIT(IIO_CHAN_INFO_SCALE);
> +
> +	if (iio_channel_has_available(schan, IIO_CHAN_INFO_RAW))
> +		chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW);
> +
> +	return 0;
> +}
> +
> +static int rescale_current_sense_amplifier_props(struct device *dev,
> +						 struct rescale *rescale)
> +{
> +	u32 sense;
> +	u32 gain_mult = 1;
> +	u32 gain_div = 1;
> +	u32 factor;
> +	int ret;
> +
> +	ret = device_property_read_u32(dev, "sense-resistor-micro-ohms",
> +				       &sense);
> +	if (ret) {
> +		dev_err(dev, "failed to read the sense resistance: %d\n", ret);
> +		return ret;
> +	}
> +
> +	device_property_read_u32(dev, "sense-gain-mult", &gain_mult);
> +	device_property_read_u32(dev, "sense-gain-div", &gain_div);
> +
> +	/*
> +	 * Calculate the scaling factor, 1 / (gain * sense), or
> +	 * gain_div / (gain_mult * sense), while trying to keep the
> +	 * numerator/denominator from overflowing.
> +	 */
> +	factor = gcd(sense, 1000000);
> +	rescale->numerator = 1000000 / factor;
> +	rescale->denominator = sense / factor;
> +
> +	factor = gcd(rescale->numerator, gain_mult);
> +	rescale->numerator /= factor;
> +	rescale->denominator *= gain_mult / factor;
> +
> +	factor = gcd(rescale->denominator, gain_div);
> +	rescale->numerator *= gain_div / factor;
> +	rescale->denominator /= factor;
> +
> +	return 0;
> +}
> +
> +static int rescale_current_sense_shunt_props(struct device *dev,
> +					     struct rescale *rescale)
> +{
> +	u32 shunt;
> +	u32 factor;
> +	int ret;
> +
> +	ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
> +				       &shunt);
> +	if (ret) {
> +		dev_err(dev, "failed to read the shunt resistance: %d\n", ret);
> +		return ret;
> +	}
> +
> +	factor = gcd(shunt, 1000000);
> +	rescale->numerator = 1000000 / factor;
> +	rescale->denominator = shunt / factor;
> +
> +	return 0;
> +}
> +
> +static int rescale_voltage_divider_props(struct device *dev,
> +					 struct rescale *rescale)
> +{
> +	int ret;
> +	u32 factor;
> +
> +	ret = device_property_read_u32(dev, "output-ohms",
> +				       &rescale->denominator);
> +	if (ret) {
> +		dev_err(dev, "failed to read output-ohms: %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = device_property_read_u32(dev, "full-ohms",
> +				       &rescale->numerator);
> +	if (ret) {
> +		dev_err(dev, "failed to read full-ohms: %d\n", ret);
> +		return ret;
> +	}
> +
> +	factor = gcd(rescale->numerator, rescale->denominator);
> +	rescale->numerator /= factor;
> +	rescale->denominator /= factor;
> +
> +	return 0;
> +}
> +
> +enum rescale_variant {
> +	CURRENT_SENSE_AMPLIFIER,
> +	CURRENT_SENSE_SHUNT,
> +	VOLTAGE_DIVIDER,
> +};
> +
> +static const struct rescale_cfg rescale_cfg[] = {
> +	[CURRENT_SENSE_AMPLIFIER] = {
> +		.type = IIO_CURRENT,
> +		.props = rescale_current_sense_amplifier_props,
> +	},
> +	[CURRENT_SENSE_SHUNT] = {
> +		.type = IIO_CURRENT,
> +		.props = rescale_current_sense_shunt_props,
> +	},
> +	[VOLTAGE_DIVIDER] = {
> +		.type = IIO_VOLTAGE,
> +		.props = rescale_voltage_divider_props,
> +	},
> +};
> +
> +static const struct of_device_id rescale_match[] = {
> +	{ .compatible = "current-sense-amplifier",
> +	  .data = &rescale_cfg[CURRENT_SENSE_AMPLIFIER], },
> +	{ .compatible = "current-sense-shunt",
> +	  .data = &rescale_cfg[CURRENT_SENSE_SHUNT], },
> +	{ .compatible = "voltage-divider",
> +	  .data = &rescale_cfg[VOLTAGE_DIVIDER], },
> +	{ /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(of, rescale_match);
> +
> +static int rescale_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct iio_dev *indio_dev;
> +	struct iio_channel *source;
> +	struct rescale *rescale;
> +	int sizeof_ext_info;
> +	int sizeof_priv;
> +	int i;
> +	int ret;
> +
> +	source = devm_iio_channel_get(dev, NULL);
> +	if (IS_ERR(source)) {
> +		if (PTR_ERR(source) != -EPROBE_DEFER)
> +			dev_err(dev, "failed to get source channel\n");
> +		return PTR_ERR(source);
> +	}
> +
> +	sizeof_ext_info = iio_get_channel_ext_info_count(source);
> +	if (sizeof_ext_info) {
> +		sizeof_ext_info += 1; /* one extra entry for the sentinel */
> +		sizeof_ext_info *= sizeof(*rescale->ext_info);
> +	}
> +
> +	sizeof_priv = sizeof(*rescale) + sizeof_ext_info;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof_priv);
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	rescale = iio_priv(indio_dev);
> +
> +	rescale->cfg = of_device_get_match_data(dev);
> +	rescale->numerator = 1;
> +	rescale->denominator = 1;
> +
> +	ret = rescale->cfg->props(dev, rescale);
> +	if (ret)
> +		return ret;
> +
> +	if (!rescale->numerator || !rescale->denominator) {
> +		dev_err(dev, "invalid scaling factor.\n");
> +		return -EINVAL;
> +	}
> +
> +	platform_set_drvdata(pdev, indio_dev);
> +
> +	rescale->source = source;
> +
> +	indio_dev->name = dev_name(dev);
> +	indio_dev->dev.parent = dev;
> +	indio_dev->info = &rescale_info;
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->channels = &rescale->chan;
> +	indio_dev->num_channels = 1;
> +	if (sizeof_ext_info) {
> +		rescale->ext_info = devm_kmemdup(dev,
> +						 source->channel->ext_info,
> +						 sizeof_ext_info, GFP_KERNEL);
> +		if (!rescale->ext_info)
> +			return -ENOMEM;
> +
> +		for (i = 0; rescale->ext_info[i].name; ++i) {
> +			struct iio_chan_spec_ext_info *ext_info =
> +				&rescale->ext_info[i];
> +
> +			if (source->channel->ext_info[i].read)
> +				ext_info->read = rescale_read_ext_info;
> +			if (source->channel->ext_info[i].write)
> +				ext_info->write = rescale_write_ext_info;
> +			ext_info->private = i;
> +		}
> +	}
> +
> +	ret = rescale_configure_channel(dev, rescale);
> +	if (ret)
> +		return ret;
> +
> +	return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static struct platform_driver rescale_driver = {
> +	.probe = rescale_probe,
> +	.driver = {
> +		.name = "iio-rescale",
> +		.of_match_table = rescale_match,
> +	},
> +};
> +module_platform_driver(rescale_driver);
> +
> +MODULE_DESCRIPTION("IIO rescale driver");
> +MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
> +MODULE_LICENSE("GPL v2");

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2018-04-28 17:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-23 21:08 [PATCH v4 0/4] iio: add unit-converter Peter Rosin
2018-04-23 21:08 ` [PATCH v4 1/4] dt-bindings: iio: afe: add binding for current-sense-shunt Peter Rosin
2018-04-28 16:59   ` Jonathan Cameron
2018-04-23 21:08 ` [PATCH v4 2/4] dt-bindings: iio: afe: add binding for voltage-divider Peter Rosin
2018-04-27 14:29   ` Rob Herring
2018-04-28 17:01     ` Jonathan Cameron
2018-04-23 21:08 ` [PATCH v4 3/4] dt-bindings: iio: afe: add binding for current-sense-amplifier Peter Rosin
2018-04-27 14:30   ` Rob Herring
2018-04-28 17:02     ` Jonathan Cameron
2018-04-23 21:08 ` [PATCH v4 4/4] iio: afe: rescale: new driver Peter Rosin
2018-04-28 17:05   ` Jonathan Cameron

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).