linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] dt-bindings: iio: adc: Add binding for current-from-voltage
       [not found] <20200516022619.30779-1-xc-racer2@live.ca>
@ 2020-05-16  2:26 ` Jonathan Bakker
  2020-05-28 20:13   ` Rob Herring
  2020-05-16  2:26 ` [PATCH 2/2] iio: adc: Add current-from-voltage driver Jonathan Bakker
  1 sibling, 1 reply; 4+ messages in thread
From: Jonathan Bakker @ 2020-05-16  2:26 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio, linux-kernel,
	devicetree
  Cc: linus.walleij, Jonathan Bakker

Some devices may require a current adc, but only have a voltage
ADC onboard.  In order to read the current, they have a resistor
connected to the ADC.  Add bindings for this possibility.

Signed-off-by: Jonathan Bakker <xc-racer2@live.ca>
---
 .../iio/adc/linux,current-from-voltage.yaml   | 47 +++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml

diff --git a/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml b/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
new file mode 100644
index 000000000000..385d317607c3
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/linux,current-from-voltage.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Current ADC from voltage ADC and resistor
+
+maintainers:
+  - Jonathan Bakker <xc-racer2@live.ca>
+
+properties:
+  compatible:
+    const: linux,current-from-voltage
+
+  io-channel-names:
+    const: adc
+
+  io-channels:
+    maxItems: 1
+    description: Voltage ADC channel
+
+  linux,resistor-ohms:
+    description: Strength of resistor connected to voltage ADC
+
+  "#io-channel-cells":
+    const: 0
+
+required:
+  - compatible
+  - io-channel-names
+  - io-channels
+  - linux,resistor-ohms
+  - "#io-channel-cells"
+
+examples:
+  - |
+    current-from-voltage {
+      compatible = "linux,current-from-voltage";
+      io-channel-names = "adc";
+      io-channels = <&adc 9>;
+      linux,resistor-ohms = <47>;
+      #io-channel-cells = <0>;
+      io-channel-ranges;
+    };
+
+...
-- 
2.20.1


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

* [PATCH 2/2] iio: adc: Add current-from-voltage driver
       [not found] <20200516022619.30779-1-xc-racer2@live.ca>
  2020-05-16  2:26 ` [PATCH 1/2] dt-bindings: iio: adc: Add binding for current-from-voltage Jonathan Bakker
@ 2020-05-16  2:26 ` Jonathan Bakker
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Bakker @ 2020-05-16  2:26 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, robh+dt, linux-iio, linux-kernel,
	devicetree
  Cc: linus.walleij, Jonathan Bakker

Some devices may require a current adc, but only have a voltage
ADC onboard.  In order to read the current, they have a resistor
connected to the ADC.

Suggested-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Jonathan Bakker <xc-racer2@live.ca>
---
 MAINTAINERS                            |   8 ++
 drivers/iio/adc/Kconfig                |   9 ++
 drivers/iio/adc/Makefile               |   1 +
 drivers/iio/adc/current-from-voltage.c | 123 +++++++++++++++++++++++++
 4 files changed, 141 insertions(+)
 create mode 100644 drivers/iio/adc/current-from-voltage.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 2926327e4976..094cf512b403 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4503,6 +4503,14 @@ T:	git git://linuxtv.org/media_tree.git
 F:	Documentation/devicetree/bindings/media/allwinner,sun6i-a31-csi.yaml
 F:	drivers/media/platform/sunxi/sun6i-csi/
 
+CURRENT ADC FROM VOLTAGE ADC DRIVER
+M:	Jonathan Bakker <xc-racer2@live.ca>
+L:	linux-iio@vger.kernel.org
+S:	Maintained
+T:	git git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git
+F:	Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
+F:	drivers/iio/adc/current-from-voltage.c
+
 CW1200 WLAN driver
 M:	Solomon Peachy <pizza@shaftnet.org>
 S:	Maintained
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 12bb8b7ca1ff..84e6ccb36024 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -344,6 +344,15 @@ config CPCAP_ADC
 	  This driver can also be built as a module. If so, the module will be
 	  called cpcap-adc.
 
+config CURRENT_FROM_VOLTAGE
+	tristate "Current from voltage shim driver"
+	help
+	  Say yes here to build support for a shim driver converting a voltage
+	  ADC coupled with a resistor to a current ADC.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called current-from-voltage.
+
 config DA9150_GPADC
 	tristate "Dialog DA9150 GPADC driver support"
 	depends on MFD_DA9150
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 637807861112..d293184fc32a 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
 obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
 obj-$(CONFIG_CC10001_ADC) += cc10001_adc.o
 obj-$(CONFIG_CPCAP_ADC) += cpcap-adc.o
+obj-$(CONFIG_CURRENT_FROM_VOLTAGE) += current-from-voltage.o
 obj-$(CONFIG_DA9150_GPADC) += da9150-gpadc.o
 obj-$(CONFIG_DLN2_ADC) += dln2-adc.o
 obj-$(CONFIG_ENVELOPE_DETECTOR) += envelope-detector.o
diff --git a/drivers/iio/adc/current-from-voltage.c b/drivers/iio/adc/current-from-voltage.c
new file mode 100644
index 000000000000..69cb18e0995b
--- /dev/null
+++ b/drivers/iio/adc/current-from-voltage.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for converting a resistor and a voltage ADC to a current ADC
+ *
+ * Copyright (C) 2020 Jonathan Bakker <xc-racer2@live.ca>
+ */
+
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/iio/consumer.h>
+#include <linux/iio/iio.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+struct shim {
+	struct iio_channel *adc;
+	u32 resistor_value;
+};
+
+static int shim_read_raw(struct iio_dev *indio_dev,
+				      struct iio_chan_spec const *chan,
+				      int *val, int *val2, long mask)
+{
+	struct shim *shim = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = iio_read_channel_processed(shim->adc, val);
+		if (ret < 0) {
+			dev_err(&indio_dev->dev, "fail reading voltage ADC\n");
+			return ret;
+		}
+
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		*val = 1;
+		*val2 = shim->resistor_value;
+
+		return IIO_VAL_FRACTIONAL;
+	}
+
+	return -EINVAL;
+}
+
+static const struct iio_chan_spec shim_iio_channel = {
+	.type = IIO_CURRENT,
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
+		| BIT(IIO_CHAN_INFO_SCALE),
+};
+
+static const struct iio_info shim_info = {
+	.read_raw = &shim_read_raw,
+};
+
+static int shim_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct iio_dev *indio_dev;
+	struct shim *shim;
+	enum iio_chan_type type;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*shim));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, indio_dev);
+	shim = iio_priv(indio_dev);
+
+	indio_dev->name = dev_name(dev);
+	indio_dev->dev.parent = dev;
+	indio_dev->dev.of_node = dev->of_node;
+	indio_dev->info = &shim_info;
+	indio_dev->channels = &shim_iio_channel;
+	indio_dev->num_channels = 1;
+
+	shim->adc = devm_iio_channel_get(dev, "adc");
+	if (IS_ERR(shim->adc)) {
+		if (PTR_ERR(shim->adc) != -EPROBE_DEFER)
+			dev_err(dev, "failed to get adc input channel\n");
+		return PTR_ERR(shim->adc);
+	}
+
+	ret = iio_get_channel_type(shim->adc, &type);
+	if (ret < 0)
+		return ret;
+
+	if (type != IIO_VOLTAGE) {
+		dev_err(dev, "ADC is of the wrong type\n");
+		return -EINVAL;
+	}
+
+	ret = device_property_read_u32(dev, "linux,resistor-ohms",
+				      &shim->resistor_value);
+	if (ret < 0) {
+		dev_err(dev, "no resistor value found\n");
+		return ret;
+	}
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct of_device_id shim_match[] = {
+	{ .compatible = "linux,current-from-voltage", },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, shim_match);
+
+static struct platform_driver shim_driver = {
+	.probe = shim_probe,
+	.driver = {
+		.name = "current-from-voltage",
+		.of_match_table = shim_match,
+	},
+};
+module_platform_driver(shim_driver);
+
+MODULE_DESCRIPTION("Current ADC from voltage ADC and resistor");
+MODULE_AUTHOR("Jonathan Bakker <xc-racer2@live.ca>");
+MODULE_LICENSE("GPL v2");
-- 
2.20.1


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

* Re: [PATCH 1/2] dt-bindings: iio: adc: Add binding for current-from-voltage
  2020-05-16  2:26 ` [PATCH 1/2] dt-bindings: iio: adc: Add binding for current-from-voltage Jonathan Bakker
@ 2020-05-28 20:13   ` Rob Herring
  2020-05-28 20:16     ` Jonathan Bakker
  0 siblings, 1 reply; 4+ messages in thread
From: Rob Herring @ 2020-05-28 20:13 UTC (permalink / raw)
  To: Jonathan Bakker
  Cc: jic23, knaack.h, lars, pmeerw, linux-iio, linux-kernel,
	devicetree, linus.walleij

On Fri, May 15, 2020 at 07:26:18PM -0700, Jonathan Bakker wrote:
> Some devices may require a current adc, but only have a voltage
> ADC onboard.  In order to read the current, they have a resistor
> connected to the ADC.  Add bindings for this possibility.
> 
> Signed-off-by: Jonathan Bakker <xc-racer2@live.ca>
> ---
>  .../iio/adc/linux,current-from-voltage.yaml   | 47 +++++++++++++++++++
>  1 file changed, 47 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
> 
> diff --git a/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml b/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
> new file mode 100644
> index 000000000000..385d317607c3
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
> @@ -0,0 +1,47 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/iio/adc/linux,current-from-voltage.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Current ADC from voltage ADC and resistor
> +
> +maintainers:
> +  - Jonathan Bakker <xc-racer2@live.ca>
> +
> +properties:
> +  compatible:
> +    const: linux,current-from-voltage

How is an ADC with a resistor attached a Linux thing? So you don't need 
'linux', but then 'current-from-voltage' isn't the best naming. I don't 
have a suggestion ATM.

> +
> +  io-channel-names:
> +    const: adc
> +
> +  io-channels:
> +    maxItems: 1
> +    description: Voltage ADC channel
> +
> +  linux,resistor-ohms:
> +    description: Strength of resistor connected to voltage ADC

Wouldn't you need this to be micro-ohms? Otherwise, there'd be too much 
voltage drop?

Rob

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

* Re: [PATCH 1/2] dt-bindings: iio: adc: Add binding for current-from-voltage
  2020-05-28 20:13   ` Rob Herring
@ 2020-05-28 20:16     ` Jonathan Bakker
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Bakker @ 2020-05-28 20:16 UTC (permalink / raw)
  To: Rob Herring
  Cc: jic23, knaack.h, lars, pmeerw, linux-iio, linux-kernel,
	devicetree, linus.walleij

Hi Rob,

On 2020-05-28 1:13 p.m., Rob Herring wrote:
> On Fri, May 15, 2020 at 07:26:18PM -0700, Jonathan Bakker wrote:
>> Some devices may require a current adc, but only have a voltage
>> ADC onboard.  In order to read the current, they have a resistor
>> connected to the ADC.  Add bindings for this possibility.
>>
>> Signed-off-by: Jonathan Bakker <xc-racer2@live.ca>
>> ---
>>  .../iio/adc/linux,current-from-voltage.yaml   | 47 +++++++++++++++++++
>>  1 file changed, 47 insertions(+)
>>  create mode 100644 Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
>>
>> diff --git a/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml b/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
>> new file mode 100644
>> index 000000000000..385d317607c3
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/iio/adc/linux,current-from-voltage.yaml
>> @@ -0,0 +1,47 @@
>> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
>> +%YAML 1.2
>> +---
>> +$id: http://devicetree.org/schemas/iio/adc/linux,current-from-voltage.yaml#
>> +$schema: http://devicetree.org/meta-schemas/core.yaml#
>> +
>> +title: Current ADC from voltage ADC and resistor
>> +
>> +maintainers:
>> +  - Jonathan Bakker <xc-racer2@live.ca>
>> +
>> +properties:
>> +  compatible:
>> +    const: linux,current-from-voltage
> 
> How is an ADC with a resistor attached a Linux thing? So you don't need 
> 'linux', but then 'current-from-voltage' isn't the best naming. I don't 
> have a suggestion ATM.
> 

The good/bad news is that I was re-implementing an existing driver under a new name :)

The compatible is current-sense-shunt for this exact same purpose.

Thanks,
Jonathan

>> +
>> +  io-channel-names:
>> +    const: adc
>> +
>> +  io-channels:
>> +    maxItems: 1
>> +    description: Voltage ADC channel
>> +
>> +  linux,resistor-ohms:
>> +    description: Strength of resistor connected to voltage ADC
> 
> Wouldn't you need this to be micro-ohms? Otherwise, there'd be too much 
> voltage drop?
> 
> Rob
> 

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

end of thread, other threads:[~2020-05-28 20:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20200516022619.30779-1-xc-racer2@live.ca>
2020-05-16  2:26 ` [PATCH 1/2] dt-bindings: iio: adc: Add binding for current-from-voltage Jonathan Bakker
2020-05-28 20:13   ` Rob Herring
2020-05-28 20:16     ` Jonathan Bakker
2020-05-16  2:26 ` [PATCH 2/2] iio: adc: Add current-from-voltage driver Jonathan Bakker

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