linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/3] mfd: rn5t618: add ADC support
@ 2020-01-20 21:20 Andreas Kemnade
  2020-01-20 21:20 ` [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619 Andreas Kemnade
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Andreas Kemnade @ 2020-01-20 21:20 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, lee.jones, b.galvani,
	linus.walleij, andreas, linux-kernel, linux-iio, phh, stefan,
	letux-kernel, martin.blumenstingl

This series adds support for the ADC in the RN5T618/RC5T619.
It depends on the IRQ support added in the RTC support series here:
https://lore.kernel.org/lkml/20191220122416.31881-1-andreas@kemnade.info/

I tested the driver only with the RC5T619 but it should work with the with 
the RN5T618 as well based on these facts:
- The corresponding register definitions originally went into the kernel
  for the RN5T618
- Public datasheet sections about the ADC look same.
- Out-of-tree code for these chips look same regarding to ADC

But due to missing hardware I cannot test the patches 2/3 and 3/3 which
add support for the RN5T618 ADC.
I marked these untested patches as RFC, and IMHO they require a Tested-By.
Feel free to ignore them if the whole series would be delayed just because
of missing Tested-By for those.

Changes in v2:
- got an "Applied, thanks" message for the first two, so I do not include
  them anymore
- some cleanups for the ADC driver itself

Andreas Kemnade (3):
  iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619
  mfd: rn5t618: add IRQ definitions for RN5T618
  mfd: rn5t618: add ADC subdevice for RN5T618

 drivers/iio/adc/Kconfig       |  10 ++
 drivers/iio/adc/Makefile      |   1 +
 drivers/iio/adc/rn5t618-adc.c | 253 ++++++++++++++++++++++++++++++++++++++++++
 drivers/mfd/rn5t618.c         |  47 +++++++-
 4 files changed, 309 insertions(+), 2 deletions(-)
 create mode 100644 drivers/iio/adc/rn5t618-adc.c

-- 
2.11.0


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

* [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619
  2020-01-20 21:20 [PATCH v2 0/3] mfd: rn5t618: add ADC support Andreas Kemnade
@ 2020-01-20 21:20 ` Andreas Kemnade
  2020-02-02 17:33   ` Jonathan Cameron
  2020-01-20 21:20 ` [PATCH RFC v2 2/3] mfd: rn5t618: add IRQ definitions for RN5T618 Andreas Kemnade
  2020-01-20 21:20 ` [PATCH RFC v2 3/3] mfd: rn5t618: add ADC subdevice " Andreas Kemnade
  2 siblings, 1 reply; 7+ messages in thread
From: Andreas Kemnade @ 2020-01-20 21:20 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, lee.jones, b.galvani,
	linus.walleij, andreas, linux-kernel, linux-iio, phh, stefan,
	letux-kernel, martin.blumenstingl

Both chips have an A/D converter capable of measuring
things like VBAT, VUSB and analog inputs.

Signed-off-by: Andreas Kemnade <andreas@kemnade.info>
---
Changes in v2:
- enum for channels
- bulk read instead of single byte read for conversion
  result
- fix get_virq error handling
- use devm for registering device and requesting IRQ

 drivers/iio/adc/Kconfig       |  10 ++
 drivers/iio/adc/Makefile      |   1 +
 drivers/iio/adc/rn5t618-adc.c | 253 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 264 insertions(+)
 create mode 100644 drivers/iio/adc/rn5t618-adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index f0af3a42f53c..9ea9489e3f0a 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -735,6 +735,16 @@ config RCAR_GYRO_ADC
 	  To compile this driver as a module, choose M here: the
 	  module will be called rcar-gyroadc.
 
+config RN5T618_ADC
+	tristate "ADC for the RN5T618/RC5T619 family of chips"
+	depends on MFD_RN5T618
+	help
+	  Say yes here to build support for the integrated ADC inside the
+	  RN5T618/619 series PMICs:
+
+	  This driver can also be built as a module. If so, the module
+	  will be called rn5t618-adc.
+
 config ROCKCHIP_SARADC
 	tristate "Rockchip SARADC driver"
 	depends on ARCH_ROCKCHIP || (ARM && COMPILE_TEST)
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index ef9cc485fb67..2aea70556ed0 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -69,6 +69,7 @@ obj-$(CONFIG_QCOM_VADC_COMMON) += qcom-vadc-common.o
 obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o
 obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o
 obj-$(CONFIG_RCAR_GYRO_ADC) += rcar-gyroadc.o
+obj-$(CONFIG_RN5T618_ADC) += rn5t618-adc.o
 obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o
 obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o
 obj-$(CONFIG_SPEAR_ADC) += spear_adc.o
diff --git a/drivers/iio/adc/rn5t618-adc.c b/drivers/iio/adc/rn5t618-adc.c
new file mode 100644
index 000000000000..667bd814569d
--- /dev/null
+++ b/drivers/iio/adc/rn5t618-adc.c
@@ -0,0 +1,253 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * ADC driver for the RICOH RN5T618 power management chip family
+ *
+ * Copyright (C) 2019 Andreas Kemnade
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/interrupt.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/mfd/rn5t618.h>
+#include <linux/platform_device.h>
+#include <linux/completion.h>
+#include <linux/regmap.h>
+#include <linux/iio/iio.h>
+#include <linux/slab.h>
+#include <linux/irqdomain.h>
+
+#define RN5T618_ADC_CONVERSION_TIMEOUT   (msecs_to_jiffies(500))
+#define REFERENCE_VOLT 2500
+
+/* mask for selecting channels for single conversion */
+#define ADCCNT3_CHANNEL_MASK 0x7
+/* average 4-time conversion mode */
+#define ADCCNT3_AVG BIT(3)
+/* set for starting a single conversion, gets cleared by hw when done */
+#define ADCCNT3_GODONE BIT(4)
+/* automatic conversion, period is in ADCCNT2, selected channels are
+ * in ADCCNT1
+ */
+#define ADCCNT3_AUTO BIT(5)
+#define ADCEND_IRQ BIT(0)
+
+struct rn5t618_adc_data {
+	struct device *dev;
+	struct rn5t618 *rn5t618;
+	struct completion conv_completion;
+	int irq;
+};
+
+struct rn5t618_channel_ratios {
+	u16 numerator;
+	u16 denominator;
+};
+
+enum rn5t618_channels {
+	LIMMON = 0,
+	VBAT,
+	VADP,
+	VUSB,
+	VSYS,
+	VTHM,
+	AIN1,
+	AIN0
+};
+
+static const struct rn5t618_channel_ratios rn5t618_ratios[8] = {
+	[LIMMON] = {50, 32}, /* measured across 20mOhm, amplified by 32 */
+	[VBAT] = {2, 1},
+	[VADP] = {3, 1},
+	[VUSB] = {3, 1},
+	[VSYS] = {3, 1},
+	[VTHM] = {1, 1},
+	[AIN1] = {1, 1},
+	[AIN0] = {1, 1},
+};
+
+static int rn5t618_read_adc_reg(struct rn5t618 *rn5t618, int reg, u16 *val)
+{
+	u8 data[2];
+	int ret;
+
+	ret = regmap_bulk_read(rn5t618->regmap, reg, data, sizeof(data));
+	if (ret < 0)
+		return ret;
+
+	*val = (data[0] << 4) | (data[1] & 0xF);
+
+	return 0;
+}
+
+static irqreturn_t rn5t618_adc_irq(int irq, void *data)
+{
+	struct rn5t618_adc_data *adc = data;
+	unsigned int r = 0;
+	int ret;
+
+	/* clear low & high threshold irqs */
+	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC1, 0);
+	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC2, 0);
+
+	ret = regmap_read(adc->rn5t618->regmap, RN5T618_IR_ADC3, &r);
+	if (ret < 0)
+		dev_err(adc->dev, "failed to read IRQ status: %d\n", ret);
+
+	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC3, 0);
+
+	if (r & ADCEND_IRQ)
+		complete(&adc->conv_completion);
+
+	return IRQ_HANDLED;
+}
+
+static int rn5t618_adc_read(struct iio_dev *iio_dev,
+			    const struct iio_chan_spec *chan,
+			    int *val, int *val2, long mask)
+{
+	struct rn5t618_adc_data *adc = iio_priv(iio_dev);
+	u16 raw;
+	int ret;
+
+	/* select channel */
+	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
+				 ADCCNT3_CHANNEL_MASK,
+				 chan->channel);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_write(adc->rn5t618->regmap, RN5T618_EN_ADCIR3, ADCEND_IRQ);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
+				 ADCCNT3_AVG,
+				 mask == IIO_CHAN_INFO_AVERAGE_RAW ?
+				 ADCCNT3_AVG : 0);
+	if (ret < 0)
+		return ret;
+
+	init_completion(&adc->conv_completion);
+	/* single conversion */
+	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
+				 ADCCNT3_GODONE, ADCCNT3_GODONE);
+	if (ret < 0)
+		return ret;
+
+	ret = wait_for_completion_timeout(&adc->conv_completion,
+					  RN5T618_ADC_CONVERSION_TIMEOUT);
+	if (ret == 0) {
+		dev_warn(adc->dev, "timeout waiting for adc result\n");
+		return -ETIMEDOUT;
+	}
+
+	ret = rn5t618_read_adc_reg(adc->rn5t618,
+				   RN5T618_ILIMDATAH + 2 * chan->channel,
+				   &raw);
+	if (ret < 0)
+		return ret;
+
+	*val = raw;
+	if (mask == IIO_CHAN_INFO_PROCESSED)
+		*val = *val * REFERENCE_VOLT *
+		       rn5t618_ratios[chan->channel].numerator /
+		       rn5t618_ratios[chan->channel].denominator / 4095;
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info rn5t618_adc_iio_info = {
+	.read_raw = &rn5t618_adc_read,
+};
+
+#define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
+	.type = _type, \
+	.channel = _channel, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      BIT(IIO_CHAN_INFO_PROCESSED), \
+	.datasheet_name = _name, \
+	.indexed = 1. \
+}
+
+static const struct iio_chan_spec rn5t618_adc_iio_channels[] = {
+	RN5T618_ADC_CHANNEL(LIMMON, IIO_CURRENT, "LIMMON"),
+	RN5T618_ADC_CHANNEL(VBAT, IIO_VOLTAGE, "VBAT"),
+	RN5T618_ADC_CHANNEL(VADP, IIO_VOLTAGE, "VADP"),
+	RN5T618_ADC_CHANNEL(VUSB, IIO_VOLTAGE, "VUSB"),
+	RN5T618_ADC_CHANNEL(VSYS, IIO_VOLTAGE, "VSYS"),
+	RN5T618_ADC_CHANNEL(VTHM, IIO_VOLTAGE, "VTHM"),
+	RN5T618_ADC_CHANNEL(AIN1, IIO_VOLTAGE, "AIN1"),
+	RN5T618_ADC_CHANNEL(AIN0, IIO_VOLTAGE, "AIN0")
+};
+
+static int rn5t618_adc_probe(struct platform_device *pdev)
+{
+	int ret;
+	struct iio_dev *iio_dev;
+	struct rn5t618_adc_data *adc;
+	struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
+		return -ENOMEM;
+	}
+
+	adc = iio_priv(iio_dev);
+	adc->dev = &pdev->dev;
+	adc->rn5t618 = rn5t618;
+
+	if (rn5t618->irq_data)
+		adc->irq = regmap_irq_get_virq(rn5t618->irq_data,
+					       RN5T618_IRQ_ADC);
+
+	if (adc->irq <= 0) {
+		dev_err(&pdev->dev, "get virq failed\n");
+		return -EINVAL;
+	}
+
+	init_completion(&adc->conv_completion);
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->info = &rn5t618_adc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = rn5t618_adc_iio_channels;
+	iio_dev->num_channels = ARRAY_SIZE(rn5t618_adc_iio_channels);
+
+	/* stop any auto-conversion */
+	ret = regmap_write(rn5t618->regmap, RN5T618_ADCCNT3, 0);
+	if (ret < 0)
+		return ret;
+
+	platform_set_drvdata(pdev, iio_dev);
+
+	ret = devm_request_threaded_irq(adc->dev, adc->irq, NULL,
+					rn5t618_adc_irq,
+					IRQF_ONESHOT, dev_name(adc->dev),
+					adc);
+	if (ret < 0) {
+		dev_err(adc->dev, "request irq %d failed: %d\n", adc->irq, ret);
+		return ret;
+	}
+
+	ret = devm_iio_device_register(adc->dev, iio_dev);
+
+	return ret;
+}
+
+static struct platform_driver rn5t618_adc_driver = {
+	.driver = {
+		.name   = "rn5t618-adc",
+	},
+	.probe = rn5t618_adc_probe,
+};
+
+module_platform_driver(rn5t618_adc_driver);
+MODULE_ALIAS("platform:rn5t618-adc");
+MODULE_DESCRIPTION("RICOH RN5T618 ADC driver");
+MODULE_LICENSE("GPL");
-- 
2.11.0


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

* [PATCH RFC v2 2/3] mfd: rn5t618: add IRQ definitions for RN5T618
  2020-01-20 21:20 [PATCH v2 0/3] mfd: rn5t618: add ADC support Andreas Kemnade
  2020-01-20 21:20 ` [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619 Andreas Kemnade
@ 2020-01-20 21:20 ` Andreas Kemnade
  2020-01-20 21:20 ` [PATCH RFC v2 3/3] mfd: rn5t618: add ADC subdevice " Andreas Kemnade
  2 siblings, 0 replies; 7+ messages in thread
From: Andreas Kemnade @ 2020-01-20 21:20 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, lee.jones, b.galvani,
	linus.walleij, andreas, linux-kernel, linux-iio, phh, stefan,
	letux-kernel, martin.blumenstingl

Previously, only the definitions for the RC5T619 were
available.

Signed-off-by: Andreas Kemnade <andreas@kemnade.info>
---
Untested, IMHO only acceptable with a Tested-By

 drivers/mfd/rn5t618.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/drivers/mfd/rn5t618.c b/drivers/mfd/rn5t618.c
index 321836f78120..6828fd40b0a1 100644
--- a/drivers/mfd/rn5t618.c
+++ b/drivers/mfd/rn5t618.c
@@ -58,6 +58,24 @@ static const struct regmap_config rn5t618_regmap_config = {
 	.cache_type	= REGCACHE_RBTREE,
 };
 
+static const struct regmap_irq rn5t618_irqs[] = {
+	REGMAP_IRQ_REG(RN5T618_IRQ_SYS, 0, BIT(0)),
+	REGMAP_IRQ_REG(RN5T618_IRQ_DCDC, 0, BIT(1)),
+	REGMAP_IRQ_REG(RN5T618_IRQ_ADC, 0, BIT(3)),
+	REGMAP_IRQ_REG(RN5T618_IRQ_GPIO, 0, BIT(4)),
+	REGMAP_IRQ_REG(RN5T618_IRQ_CHG, 0, BIT(6)),
+};
+
+static const struct regmap_irq_chip rn5t618_irq_chip = {
+	.name = "rn5t618",
+	.irqs = rn5t618_irqs,
+	.num_irqs = ARRAY_SIZE(rn5t618_irqs),
+	.num_regs = 1,
+	.status_base = RN5T618_INTMON,
+	.mask_base = RN5T618_INTEN,
+	.mask_invert = true,
+};
+
 static const struct regmap_irq rc5t619_irqs[] = {
 	REGMAP_IRQ_REG(RN5T618_IRQ_SYS, 0, BIT(0)),
 	REGMAP_IRQ_REG(RN5T618_IRQ_DCDC, 0, BIT(1)),
@@ -92,6 +110,9 @@ static int rn5t618_irq_init(struct rn5t618 *rn5t618)
 	case RC5T619:
 		irq_chip = &rc5t619_irq_chip;
 		break;
+	case RN5T618:
+		irq_chip = &rn5t618_irq_chip;
+		break;
 	default:
 		irq_chip = NULL;
 		break;
-- 
2.11.0


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

* [PATCH RFC v2 3/3] mfd: rn5t618: add ADC subdevice for RN5T618
  2020-01-20 21:20 [PATCH v2 0/3] mfd: rn5t618: add ADC support Andreas Kemnade
  2020-01-20 21:20 ` [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619 Andreas Kemnade
  2020-01-20 21:20 ` [PATCH RFC v2 2/3] mfd: rn5t618: add IRQ definitions for RN5T618 Andreas Kemnade
@ 2020-01-20 21:20 ` Andreas Kemnade
  2 siblings, 0 replies; 7+ messages in thread
From: Andreas Kemnade @ 2020-01-20 21:20 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, lee.jones, b.galvani,
	linus.walleij, andreas, linux-kernel, linux-iio, phh, stefan,
	letux-kernel, martin.blumenstingl

RN5T618 has an ADC but RN5T567 has not, so
we need separate subdevice lists for both.

Signed-off-by: Andreas Kemnade <andreas@kemnade.info>
---
Untested, IMHO only acceptable with a Tested-By

 drivers/mfd/rn5t618.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/mfd/rn5t618.c b/drivers/mfd/rn5t618.c
index 6828fd40b0a1..d37d7a31cf26 100644
--- a/drivers/mfd/rn5t618.c
+++ b/drivers/mfd/rn5t618.c
@@ -21,6 +21,7 @@
 static const struct mfd_cell rn5t618_cells[] = {
 	{ .name = "rn5t618-regulator" },
 	{ .name = "rn5t618-wdt" },
+	{ .name = "rn5t618-adc" },
 };
 
 static const struct mfd_cell rc5t619_cells[] = {
@@ -30,6 +31,11 @@ static const struct mfd_cell rc5t619_cells[] = {
 	{ .name = "rn5t618-wdt" },
 };
 
+static const struct mfd_cell rn5t567_cells[] = {
+	{ .name = "rn5t618-regulator" },
+	{ .name = "rn5t618-wdt" },
+};
+
 static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg)
 {
 	switch (reg) {
@@ -203,16 +209,32 @@ static int rn5t618_i2c_probe(struct i2c_client *i2c,
 		return ret;
 	}
 
-	if (priv->variant == RC5T619)
+	switch (priv->variant) {
+	case RC5T619:
 		ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
 					   rc5t619_cells,
 					   ARRAY_SIZE(rc5t619_cells),
 					   NULL, 0, NULL);
-	else
+		break;
+	case RN5T618:
 		ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
 					   rn5t618_cells,
 					   ARRAY_SIZE(rn5t618_cells),
 					   NULL, 0, NULL);
+		break;
+	case RN5T567:
+		ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
+					   rn5t567_cells,
+					   ARRAY_SIZE(rn5t567_cells),
+					   NULL, 0, NULL);
+		break;
+	/*
+	 * Should not happen because we come here only with a valid device
+	 * tree match, so variant contains any of the above.
+	 */
+	default:
+		return -ENOENT;
+	}
 	if (ret) {
 		dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret);
 		return ret;
-- 
2.11.0


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

* Re: [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619
  2020-01-20 21:20 ` [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619 Andreas Kemnade
@ 2020-02-02 17:33   ` Jonathan Cameron
  2020-02-11 16:24     ` Andreas Kemnade
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Cameron @ 2020-02-02 17:33 UTC (permalink / raw)
  To: Andreas Kemnade
  Cc: knaack.h, lars, pmeerw, lee.jones, b.galvani, linus.walleij,
	linux-kernel, linux-iio, phh, stefan, letux-kernel,
	martin.blumenstingl

On Mon, 20 Jan 2020 22:20:54 +0100
Andreas Kemnade <andreas@kemnade.info> wrote:

> Both chips have an A/D converter capable of measuring
> things like VBAT, VUSB and analog inputs.
> 
> Signed-off-by: Andreas Kemnade <andreas@kemnade.info>
Sorry I missed one bigger thing in here around PROCESSED vs RAW.
See inline.

Thanks,

Jonathan

> ---
> Changes in v2:
> - enum for channels
> - bulk read instead of single byte read for conversion
>   result
> - fix get_virq error handling
> - use devm for registering device and requesting IRQ
> 
>  drivers/iio/adc/Kconfig       |  10 ++
>  drivers/iio/adc/Makefile      |   1 +
>  drivers/iio/adc/rn5t618-adc.c | 253 ++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 264 insertions(+)
>  create mode 100644 drivers/iio/adc/rn5t618-adc.c
> 
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index f0af3a42f53c..9ea9489e3f0a 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -735,6 +735,16 @@ config RCAR_GYRO_ADC
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called rcar-gyroadc.
>  
> +config RN5T618_ADC
> +	tristate "ADC for the RN5T618/RC5T619 family of chips"
> +	depends on MFD_RN5T618
> +	help
> +	  Say yes here to build support for the integrated ADC inside the
> +	  RN5T618/619 series PMICs:
Why :?
> +
> +	  This driver can also be built as a module. If so, the module
> +	  will be called rn5t618-adc.
> +
>  config ROCKCHIP_SARADC
>  	tristate "Rockchip SARADC driver"
>  	depends on ARCH_ROCKCHIP || (ARM && COMPILE_TEST)
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index ef9cc485fb67..2aea70556ed0 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -69,6 +69,7 @@ obj-$(CONFIG_QCOM_VADC_COMMON) += qcom-vadc-common.o
>  obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o
>  obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o
>  obj-$(CONFIG_RCAR_GYRO_ADC) += rcar-gyroadc.o
> +obj-$(CONFIG_RN5T618_ADC) += rn5t618-adc.o
>  obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o
>  obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o
>  obj-$(CONFIG_SPEAR_ADC) += spear_adc.o
> diff --git a/drivers/iio/adc/rn5t618-adc.c b/drivers/iio/adc/rn5t618-adc.c
> new file mode 100644
> index 000000000000..667bd814569d
> --- /dev/null
> +++ b/drivers/iio/adc/rn5t618-adc.c
> @@ -0,0 +1,253 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * ADC driver for the RICOH RN5T618 power management chip family
> + *
> + * Copyright (C) 2019 Andreas Kemnade
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/device.h>
> +#include <linux/errno.h>
> +#include <linux/interrupt.h>
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/mfd/rn5t618.h>
> +#include <linux/platform_device.h>
> +#include <linux/completion.h>
> +#include <linux/regmap.h>
> +#include <linux/iio/iio.h>
> +#include <linux/slab.h>
> +#include <linux/irqdomain.h>
I may be missing something, but I'm not immediately seeing any irq_domain*
calls?  I suspect the only call is via stuff buried in regmap so we probably
don't need the header here.

> +
> +#define RN5T618_ADC_CONVERSION_TIMEOUT   (msecs_to_jiffies(500))
> +#define REFERENCE_VOLT 2500

Please prefix these defines
RN5T618_* 

It avoids potential clashes in future with things defined in headers.

> +
> +/* mask for selecting channels for single conversion */
> +#define ADCCNT3_CHANNEL_MASK 0x7
> +/* average 4-time conversion mode */
> +#define ADCCNT3_AVG BIT(3)
> +/* set for starting a single conversion, gets cleared by hw when done */
> +#define ADCCNT3_GODONE BIT(4)
> +/* automatic conversion, period is in ADCCNT2, selected channels are
> + * in ADCCNT1
> + */
> +#define ADCCNT3_AUTO BIT(5)
> +#define ADCEND_IRQ BIT(0)
> +
> +struct rn5t618_adc_data {
> +	struct device *dev;
> +	struct rn5t618 *rn5t618;
> +	struct completion conv_completion;
> +	int irq;
> +};
> +
> +struct rn5t618_channel_ratios {
> +	u16 numerator;
> +	u16 denominator;
> +};
> +
> +enum rn5t618_channels {
> +	LIMMON = 0,
> +	VBAT,
> +	VADP,
> +	VUSB,
> +	VSYS,
> +	VTHM,
> +	AIN1,
> +	AIN0
> +};
> +
> +static const struct rn5t618_channel_ratios rn5t618_ratios[8] = {
> +	[LIMMON] = {50, 32}, /* measured across 20mOhm, amplified by 32 */
> +	[VBAT] = {2, 1},
> +	[VADP] = {3, 1},
> +	[VUSB] = {3, 1},
> +	[VSYS] = {3, 1},
> +	[VTHM] = {1, 1},
> +	[AIN1] = {1, 1},
> +	[AIN0] = {1, 1},
> +};
> +
> +static int rn5t618_read_adc_reg(struct rn5t618 *rn5t618, int reg, u16 *val)
> +{
> +	u8 data[2];
> +	int ret;
> +
> +	ret = regmap_bulk_read(rn5t618->regmap, reg, data, sizeof(data));
> +	if (ret < 0)
> +		return ret;
> +
> +	*val = (data[0] << 4) | (data[1] & 0xF);
> +
> +	return 0;
> +}
> +
> +static irqreturn_t rn5t618_adc_irq(int irq, void *data)
> +{
> +	struct rn5t618_adc_data *adc = data;
> +	unsigned int r = 0;
> +	int ret;
> +
> +	/* clear low & high threshold irqs */
> +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC1, 0);
> +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC2, 0);
> +
> +	ret = regmap_read(adc->rn5t618->regmap, RN5T618_IR_ADC3, &r);
> +	if (ret < 0)
> +		dev_err(adc->dev, "failed to read IRQ status: %d\n", ret);
> +
> +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC3, 0);
> +
> +	if (r & ADCEND_IRQ)
> +		complete(&adc->conv_completion);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int rn5t618_adc_read(struct iio_dev *iio_dev,
> +			    const struct iio_chan_spec *chan,
> +			    int *val, int *val2, long mask)
> +{
> +	struct rn5t618_adc_data *adc = iio_priv(iio_dev);
> +	u16 raw;
> +	int ret;
> +
> +	/* select channel */
> +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> +				 ADCCNT3_CHANNEL_MASK,
> +				 chan->channel);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = regmap_write(adc->rn5t618->regmap, RN5T618_EN_ADCIR3, ADCEND_IRQ);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> +				 ADCCNT3_AVG,
> +				 mask == IIO_CHAN_INFO_AVERAGE_RAW ?
> +				 ADCCNT3_AVG : 0);
> +	if (ret < 0)
> +		return ret;
> +
> +	init_completion(&adc->conv_completion);
> +	/* single conversion */
> +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> +				 ADCCNT3_GODONE, ADCCNT3_GODONE);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = wait_for_completion_timeout(&adc->conv_completion,
> +					  RN5T618_ADC_CONVERSION_TIMEOUT);
> +	if (ret == 0) {
> +		dev_warn(adc->dev, "timeout waiting for adc result\n");
> +		return -ETIMEDOUT;
> +	}
> +
> +	ret = rn5t618_read_adc_reg(adc->rn5t618,
> +				   RN5T618_ILIMDATAH + 2 * chan->channel,
> +				   &raw);
> +	if (ret < 0)
> +		return ret;
> +
> +	*val = raw;
> +	if (mask == IIO_CHAN_INFO_PROCESSED)
> +		*val = *val * REFERENCE_VOLT *
> +		       rn5t618_ratios[chan->channel].numerator /
> +		       rn5t618_ratios[chan->channel].denominator / 4095;

This info should be provided as scale so that userspace can do the maths if
it wants to rather than handling it in kernel.

> +
> +	return IIO_VAL_INT;
> +}
> +
> +static const struct iio_info rn5t618_adc_iio_info = {
> +	.read_raw = &rn5t618_adc_read,
> +};
> +
> +#define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
> +	.type = _type, \
> +	.channel = _channel, \
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
> +			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
> +			      BIT(IIO_CHAN_INFO_PROCESSED), \

Sorry, I missed this before. 

As a general rule it makes no sense to expose both RAW and PROCESSED values.
It should be possible to work one out from the other if the relationship is
linear and scale is provided.

> +	.datasheet_name = _name, \
> +	.indexed = 1. \
> +}
> +
> +static const struct iio_chan_spec rn5t618_adc_iio_channels[] = {
> +	RN5T618_ADC_CHANNEL(LIMMON, IIO_CURRENT, "LIMMON"),
> +	RN5T618_ADC_CHANNEL(VBAT, IIO_VOLTAGE, "VBAT"),
> +	RN5T618_ADC_CHANNEL(VADP, IIO_VOLTAGE, "VADP"),
> +	RN5T618_ADC_CHANNEL(VUSB, IIO_VOLTAGE, "VUSB"),
> +	RN5T618_ADC_CHANNEL(VSYS, IIO_VOLTAGE, "VSYS"),
> +	RN5T618_ADC_CHANNEL(VTHM, IIO_VOLTAGE, "VTHM"),
> +	RN5T618_ADC_CHANNEL(AIN1, IIO_VOLTAGE, "AIN1"),
> +	RN5T618_ADC_CHANNEL(AIN0, IIO_VOLTAGE, "AIN0")
> +};
> +
> +static int rn5t618_adc_probe(struct platform_device *pdev)
> +{
> +	int ret;
> +	struct iio_dev *iio_dev;
> +	struct rn5t618_adc_data *adc;
> +	struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
> +
> +	iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
> +	if (!iio_dev) {
> +		dev_err(&pdev->dev, "failed allocating iio device\n");
> +		return -ENOMEM;
> +	}
> +
> +	adc = iio_priv(iio_dev);
> +	adc->dev = &pdev->dev;
> +	adc->rn5t618 = rn5t618;
> +
> +	if (rn5t618->irq_data)
> +		adc->irq = regmap_irq_get_virq(rn5t618->irq_data,
> +					       RN5T618_IRQ_ADC);
> +
> +	if (adc->irq <= 0) {
> +		dev_err(&pdev->dev, "get virq failed\n");
> +		return -EINVAL;
> +	}
> +
> +	init_completion(&adc->conv_completion);
> +
> +	iio_dev->name = dev_name(&pdev->dev);
> +	iio_dev->dev.parent = &pdev->dev;
> +	iio_dev->info = &rn5t618_adc_iio_info;
> +	iio_dev->modes = INDIO_DIRECT_MODE;
> +	iio_dev->channels = rn5t618_adc_iio_channels;
> +	iio_dev->num_channels = ARRAY_SIZE(rn5t618_adc_iio_channels);
> +
> +	/* stop any auto-conversion */
> +	ret = regmap_write(rn5t618->regmap, RN5T618_ADCCNT3, 0);
> +	if (ret < 0)
> +		return ret;
> +
> +	platform_set_drvdata(pdev, iio_dev);
> +
> +	ret = devm_request_threaded_irq(adc->dev, adc->irq, NULL,
> +					rn5t618_adc_irq,
> +					IRQF_ONESHOT, dev_name(adc->dev),
> +					adc);
> +	if (ret < 0) {
> +		dev_err(adc->dev, "request irq %d failed: %d\n", adc->irq, ret);
> +		return ret;
> +	}
> +
> +	ret = devm_iio_device_register(adc->dev, iio_dev);
> +
> +	return ret;
> +}
> +
> +static struct platform_driver rn5t618_adc_driver = {
> +	.driver = {
> +		.name   = "rn5t618-adc",
> +	},
> +	.probe = rn5t618_adc_probe,
> +};
> +
> +module_platform_driver(rn5t618_adc_driver);
> +MODULE_ALIAS("platform:rn5t618-adc");
> +MODULE_DESCRIPTION("RICOH RN5T618 ADC driver");
> +MODULE_LICENSE("GPL");


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

* Re: [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619
  2020-02-02 17:33   ` Jonathan Cameron
@ 2020-02-11 16:24     ` Andreas Kemnade
  2020-02-14 12:55       ` Jonathan Cameron
  0 siblings, 1 reply; 7+ messages in thread
From: Andreas Kemnade @ 2020-02-11 16:24 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: knaack.h, lars, pmeerw, lee.jones, b.galvani, linus.walleij,
	linux-kernel, linux-iio, phh, stefan, letux-kernel,
	martin.blumenstingl

[-- Attachment #1: Type: text/plain, Size: 8971 bytes --]

On Sun, 2 Feb 2020 17:33:55 +0000
Jonathan Cameron <jic23@kernel.org> wrote:

> On Mon, 20 Jan 2020 22:20:54 +0100
> Andreas Kemnade <andreas@kemnade.info> wrote:
> 
> > Both chips have an A/D converter capable of measuring
> > things like VBAT, VUSB and analog inputs.
> > 
> > Signed-off-by: Andreas Kemnade <andreas@kemnade.info>  
> Sorry I missed one bigger thing in here around PROCESSED vs RAW.
> See inline.
> 
> Thanks,
> 
> Jonathan
> 
> > ---
> > Changes in v2:
> > - enum for channels
> > - bulk read instead of single byte read for conversion
> >   result
> > - fix get_virq error handling
> > - use devm for registering device and requesting IRQ
> > 
> >  drivers/iio/adc/Kconfig       |  10 ++
> >  drivers/iio/adc/Makefile      |   1 +
> >  drivers/iio/adc/rn5t618-adc.c | 253 ++++++++++++++++++++++++++++++++++++++++++
> >  3 files changed, 264 insertions(+)
> >  create mode 100644 drivers/iio/adc/rn5t618-adc.c
> > 
> > diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> > index f0af3a42f53c..9ea9489e3f0a 100644
> > --- a/drivers/iio/adc/Kconfig
> > +++ b/drivers/iio/adc/Kconfig
> > @@ -735,6 +735,16 @@ config RCAR_GYRO_ADC
> >  	  To compile this driver as a module, choose M here: the
> >  	  module will be called rcar-gyroadc.
> >  
> > +config RN5T618_ADC
> > +	tristate "ADC for the RN5T618/RC5T619 family of chips"
> > +	depends on MFD_RN5T618
> > +	help
> > +	  Say yes here to build support for the integrated ADC inside the
> > +	  RN5T618/619 series PMICs:  
> Why :?
> > +
> > +	  This driver can also be built as a module. If so, the module
> > +	  will be called rn5t618-adc.
> > +
> >  config ROCKCHIP_SARADC
> >  	tristate "Rockchip SARADC driver"
> >  	depends on ARCH_ROCKCHIP || (ARM && COMPILE_TEST)
> > diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> > index ef9cc485fb67..2aea70556ed0 100644
> > --- a/drivers/iio/adc/Makefile
> > +++ b/drivers/iio/adc/Makefile
> > @@ -69,6 +69,7 @@ obj-$(CONFIG_QCOM_VADC_COMMON) += qcom-vadc-common.o
> >  obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o
> >  obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o
> >  obj-$(CONFIG_RCAR_GYRO_ADC) += rcar-gyroadc.o
> > +obj-$(CONFIG_RN5T618_ADC) += rn5t618-adc.o
> >  obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o
> >  obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o
> >  obj-$(CONFIG_SPEAR_ADC) += spear_adc.o
> > diff --git a/drivers/iio/adc/rn5t618-adc.c b/drivers/iio/adc/rn5t618-adc.c
> > new file mode 100644
> > index 000000000000..667bd814569d
> > --- /dev/null
> > +++ b/drivers/iio/adc/rn5t618-adc.c
> > @@ -0,0 +1,253 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * ADC driver for the RICOH RN5T618 power management chip family
> > + *
> > + * Copyright (C) 2019 Andreas Kemnade
> > + */
> > +
> > +#include <linux/kernel.h>
> > +#include <linux/device.h>
> > +#include <linux/errno.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/init.h>
> > +#include <linux/module.h>
> > +#include <linux/mfd/rn5t618.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/completion.h>
> > +#include <linux/regmap.h>
> > +#include <linux/iio/iio.h>
> > +#include <linux/slab.h>
> > +#include <linux/irqdomain.h>  
> I may be missing something, but I'm not immediately seeing any irq_domain*
> calls?  I suspect the only call is via stuff buried in regmap so we probably
> don't need the header here.
> 
> > +
> > +#define RN5T618_ADC_CONVERSION_TIMEOUT   (msecs_to_jiffies(500))
> > +#define REFERENCE_VOLT 2500  
> 
> Please prefix these defines
> RN5T618_* 
> 
Does that also apply for the defines below?

> It avoids potential clashes in future with things defined in headers.
> 
> > +
> > +/* mask for selecting channels for single conversion */
> > +#define ADCCNT3_CHANNEL_MASK 0x7
> > +/* average 4-time conversion mode */
> > +#define ADCCNT3_AVG BIT(3)
> > +/* set for starting a single conversion, gets cleared by hw when done */
> > +#define ADCCNT3_GODONE BIT(4)
> > +/* automatic conversion, period is in ADCCNT2, selected channels are
> > + * in ADCCNT1
> > + */
> > +#define ADCCNT3_AUTO BIT(5)
> > +#define ADCEND_IRQ BIT(0)
> > +
> > +struct rn5t618_adc_data {
> > +	struct device *dev;
> > +	struct rn5t618 *rn5t618;
> > +	struct completion conv_completion;
> > +	int irq;
> > +};
> > +
> > +struct rn5t618_channel_ratios {
> > +	u16 numerator;
> > +	u16 denominator;
> > +};
> > +
> > +enum rn5t618_channels {
> > +	LIMMON = 0,
> > +	VBAT,
> > +	VADP,
> > +	VUSB,
> > +	VSYS,
> > +	VTHM,
> > +	AIN1,
> > +	AIN0
> > +};
> > +
> > +static const struct rn5t618_channel_ratios rn5t618_ratios[8] = {
> > +	[LIMMON] = {50, 32}, /* measured across 20mOhm, amplified by 32 */
> > +	[VBAT] = {2, 1},
> > +	[VADP] = {3, 1},
> > +	[VUSB] = {3, 1},
> > +	[VSYS] = {3, 1},
> > +	[VTHM] = {1, 1},
> > +	[AIN1] = {1, 1},
> > +	[AIN0] = {1, 1},
> > +};
> > +
> > +static int rn5t618_read_adc_reg(struct rn5t618 *rn5t618, int reg, u16 *val)
> > +{
> > +	u8 data[2];
> > +	int ret;
> > +
> > +	ret = regmap_bulk_read(rn5t618->regmap, reg, data, sizeof(data));
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	*val = (data[0] << 4) | (data[1] & 0xF);
> > +
> > +	return 0;
> > +}
> > +
> > +static irqreturn_t rn5t618_adc_irq(int irq, void *data)
> > +{
> > +	struct rn5t618_adc_data *adc = data;
> > +	unsigned int r = 0;
> > +	int ret;
> > +
> > +	/* clear low & high threshold irqs */
> > +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC1, 0);
> > +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC2, 0);
> > +
> > +	ret = regmap_read(adc->rn5t618->regmap, RN5T618_IR_ADC3, &r);
> > +	if (ret < 0)
> > +		dev_err(adc->dev, "failed to read IRQ status: %d\n", ret);
> > +
> > +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC3, 0);
> > +
> > +	if (r & ADCEND_IRQ)
> > +		complete(&adc->conv_completion);
> > +
> > +	return IRQ_HANDLED;
> > +}
> > +
> > +static int rn5t618_adc_read(struct iio_dev *iio_dev,
> > +			    const struct iio_chan_spec *chan,
> > +			    int *val, int *val2, long mask)
> > +{
> > +	struct rn5t618_adc_data *adc = iio_priv(iio_dev);
> > +	u16 raw;
> > +	int ret;
> > +
> > +	/* select channel */
> > +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> > +				 ADCCNT3_CHANNEL_MASK,
> > +				 chan->channel);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = regmap_write(adc->rn5t618->regmap, RN5T618_EN_ADCIR3, ADCEND_IRQ);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> > +				 ADCCNT3_AVG,
> > +				 mask == IIO_CHAN_INFO_AVERAGE_RAW ?
> > +				 ADCCNT3_AVG : 0);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	init_completion(&adc->conv_completion);
> > +	/* single conversion */
> > +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> > +				 ADCCNT3_GODONE, ADCCNT3_GODONE);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = wait_for_completion_timeout(&adc->conv_completion,
> > +					  RN5T618_ADC_CONVERSION_TIMEOUT);
> > +	if (ret == 0) {
> > +		dev_warn(adc->dev, "timeout waiting for adc result\n");
> > +		return -ETIMEDOUT;
> > +	}
> > +
> > +	ret = rn5t618_read_adc_reg(adc->rn5t618,
> > +				   RN5T618_ILIMDATAH + 2 * chan->channel,
> > +				   &raw);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	*val = raw;
> > +	if (mask == IIO_CHAN_INFO_PROCESSED)
> > +		*val = *val * REFERENCE_VOLT *
> > +		       rn5t618_ratios[chan->channel].numerator /
> > +		       rn5t618_ratios[chan->channel].denominator / 4095;  
> 
> This info should be provided as scale so that userspace can do the maths if
> it wants to rather than handling it in kernel.
> 
can do as has to do? So I guess any simple shell script then cannot simply
read out values from sysfs.
Hmm, how is scale defined here?
processed in mV = raw * scale (which can be IIO_VAL_FRACTIONAL)?

> > +
> > +	return IIO_VAL_INT;
> > +}
> > +
> > +static const struct iio_info rn5t618_adc_iio_info = {
> > +	.read_raw = &rn5t618_adc_read,
> > +};
> > +
> > +#define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
> > +	.type = _type, \
> > +	.channel = _channel, \
> > +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
> > +			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
> > +			      BIT(IIO_CHAN_INFO_PROCESSED), \  
> 
> Sorry, I missed this before. 
> 
> As a general rule it makes no sense to expose both RAW and PROCESSED values.
> It should be possible to work one out from the other if the relationship is
> linear and scale is provided.
> 
hmm, the other adc drivers, I get in touch with, expose both RAW and PROCESSED.
like twl4030_madc. So you want to not have that for new drivers and from the
previous comment you prefer to have PROCESSED dropped here?

Regards,
Andreas

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619
  2020-02-11 16:24     ` Andreas Kemnade
@ 2020-02-14 12:55       ` Jonathan Cameron
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Cameron @ 2020-02-14 12:55 UTC (permalink / raw)
  To: Andreas Kemnade
  Cc: knaack.h, lars, pmeerw, lee.jones, b.galvani, linus.walleij,
	linux-kernel, linux-iio, phh, stefan, letux-kernel,
	martin.blumenstingl

On Tue, 11 Feb 2020 17:24:50 +0100
Andreas Kemnade <andreas@kemnade.info> wrote:

> On Sun, 2 Feb 2020 17:33:55 +0000
> Jonathan Cameron <jic23@kernel.org> wrote:
> 
> > On Mon, 20 Jan 2020 22:20:54 +0100
> > Andreas Kemnade <andreas@kemnade.info> wrote:
> >   
> > > Both chips have an A/D converter capable of measuring
> > > things like VBAT, VUSB and analog inputs.
> > > 
> > > Signed-off-by: Andreas Kemnade <andreas@kemnade.info>    
> > Sorry I missed one bigger thing in here around PROCESSED vs RAW.
> > See inline.
> > 
> > Thanks,
> > 
> > Jonathan
> >   
> > > ---
> > > Changes in v2:
> > > - enum for channels
> > > - bulk read instead of single byte read for conversion
> > >   result
> > > - fix get_virq error handling
> > > - use devm for registering device and requesting IRQ
> > > 
> > >  drivers/iio/adc/Kconfig       |  10 ++
> > >  drivers/iio/adc/Makefile      |   1 +
> > >  drivers/iio/adc/rn5t618-adc.c | 253 ++++++++++++++++++++++++++++++++++++++++++
> > >  3 files changed, 264 insertions(+)
> > >  create mode 100644 drivers/iio/adc/rn5t618-adc.c
> > > 
> > > diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> > > index f0af3a42f53c..9ea9489e3f0a 100644
> > > --- a/drivers/iio/adc/Kconfig
> > > +++ b/drivers/iio/adc/Kconfig
> > > @@ -735,6 +735,16 @@ config RCAR_GYRO_ADC
> > >  	  To compile this driver as a module, choose M here: the
> > >  	  module will be called rcar-gyroadc.
> > >  
> > > +config RN5T618_ADC
> > > +	tristate "ADC for the RN5T618/RC5T619 family of chips"
> > > +	depends on MFD_RN5T618
> > > +	help
> > > +	  Say yes here to build support for the integrated ADC inside the
> > > +	  RN5T618/619 series PMICs:    
> > Why :?  
> > > +
> > > +	  This driver can also be built as a module. If so, the module
> > > +	  will be called rn5t618-adc.
> > > +
> > >  config ROCKCHIP_SARADC
> > >  	tristate "Rockchip SARADC driver"
> > >  	depends on ARCH_ROCKCHIP || (ARM && COMPILE_TEST)
> > > diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> > > index ef9cc485fb67..2aea70556ed0 100644
> > > --- a/drivers/iio/adc/Makefile
> > > +++ b/drivers/iio/adc/Makefile
> > > @@ -69,6 +69,7 @@ obj-$(CONFIG_QCOM_VADC_COMMON) += qcom-vadc-common.o
> > >  obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o
> > >  obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o
> > >  obj-$(CONFIG_RCAR_GYRO_ADC) += rcar-gyroadc.o
> > > +obj-$(CONFIG_RN5T618_ADC) += rn5t618-adc.o
> > >  obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o
> > >  obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o
> > >  obj-$(CONFIG_SPEAR_ADC) += spear_adc.o
> > > diff --git a/drivers/iio/adc/rn5t618-adc.c b/drivers/iio/adc/rn5t618-adc.c
> > > new file mode 100644
> > > index 000000000000..667bd814569d
> > > --- /dev/null
> > > +++ b/drivers/iio/adc/rn5t618-adc.c
> > > @@ -0,0 +1,253 @@
> > > +// SPDX-License-Identifier: GPL-2.0+
> > > +/*
> > > + * ADC driver for the RICOH RN5T618 power management chip family
> > > + *
> > > + * Copyright (C) 2019 Andreas Kemnade
> > > + */
> > > +
> > > +#include <linux/kernel.h>
> > > +#include <linux/device.h>
> > > +#include <linux/errno.h>
> > > +#include <linux/interrupt.h>
> > > +#include <linux/init.h>
> > > +#include <linux/module.h>
> > > +#include <linux/mfd/rn5t618.h>
> > > +#include <linux/platform_device.h>
> > > +#include <linux/completion.h>
> > > +#include <linux/regmap.h>
> > > +#include <linux/iio/iio.h>
> > > +#include <linux/slab.h>
> > > +#include <linux/irqdomain.h>    
> > I may be missing something, but I'm not immediately seeing any irq_domain*
> > calls?  I suspect the only call is via stuff buried in regmap so we probably
> > don't need the header here.
> >   
> > > +
> > > +#define RN5T618_ADC_CONVERSION_TIMEOUT   (msecs_to_jiffies(500))
> > > +#define REFERENCE_VOLT 2500    
> > 
> > Please prefix these defines
> > RN5T618_* 
> >   
> Does that also apply for the defines below?

yes. It's easier to just prefix all driver specific defines than think
carefully about which ones might 'clash' and which ones are very unlikely
to do so.

> 
> > It avoids potential clashes in future with things defined in headers.
> >   
> > > +
> > > +/* mask for selecting channels for single conversion */
> > > +#define ADCCNT3_CHANNEL_MASK 0x7
> > > +/* average 4-time conversion mode */
> > > +#define ADCCNT3_AVG BIT(3)
> > > +/* set for starting a single conversion, gets cleared by hw when done */
> > > +#define ADCCNT3_GODONE BIT(4)
> > > +/* automatic conversion, period is in ADCCNT2, selected channels are
> > > + * in ADCCNT1
> > > + */
> > > +#define ADCCNT3_AUTO BIT(5)
> > > +#define ADCEND_IRQ BIT(0)
> > > +
> > > +struct rn5t618_adc_data {
> > > +	struct device *dev;
> > > +	struct rn5t618 *rn5t618;
> > > +	struct completion conv_completion;
> > > +	int irq;
> > > +};
> > > +
> > > +struct rn5t618_channel_ratios {
> > > +	u16 numerator;
> > > +	u16 denominator;
> > > +};
> > > +
> > > +enum rn5t618_channels {
> > > +	LIMMON = 0,
> > > +	VBAT,
> > > +	VADP,
> > > +	VUSB,
> > > +	VSYS,
> > > +	VTHM,
> > > +	AIN1,
> > > +	AIN0
> > > +};
> > > +
> > > +static const struct rn5t618_channel_ratios rn5t618_ratios[8] = {
> > > +	[LIMMON] = {50, 32}, /* measured across 20mOhm, amplified by 32 */
> > > +	[VBAT] = {2, 1},
> > > +	[VADP] = {3, 1},
> > > +	[VUSB] = {3, 1},
> > > +	[VSYS] = {3, 1},
> > > +	[VTHM] = {1, 1},
> > > +	[AIN1] = {1, 1},
> > > +	[AIN0] = {1, 1},
> > > +};
> > > +
> > > +static int rn5t618_read_adc_reg(struct rn5t618 *rn5t618, int reg, u16 *val)
> > > +{
> > > +	u8 data[2];
> > > +	int ret;
> > > +
> > > +	ret = regmap_bulk_read(rn5t618->regmap, reg, data, sizeof(data));
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	*val = (data[0] << 4) | (data[1] & 0xF);
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +static irqreturn_t rn5t618_adc_irq(int irq, void *data)
> > > +{
> > > +	struct rn5t618_adc_data *adc = data;
> > > +	unsigned int r = 0;
> > > +	int ret;
> > > +
> > > +	/* clear low & high threshold irqs */
> > > +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC1, 0);
> > > +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC2, 0);
> > > +
> > > +	ret = regmap_read(adc->rn5t618->regmap, RN5T618_IR_ADC3, &r);
> > > +	if (ret < 0)
> > > +		dev_err(adc->dev, "failed to read IRQ status: %d\n", ret);
> > > +
> > > +	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC3, 0);
> > > +
> > > +	if (r & ADCEND_IRQ)
> > > +		complete(&adc->conv_completion);
> > > +
> > > +	return IRQ_HANDLED;
> > > +}
> > > +
> > > +static int rn5t618_adc_read(struct iio_dev *iio_dev,
> > > +			    const struct iio_chan_spec *chan,
> > > +			    int *val, int *val2, long mask)
> > > +{
> > > +	struct rn5t618_adc_data *adc = iio_priv(iio_dev);
> > > +	u16 raw;
> > > +	int ret;
> > > +
> > > +	/* select channel */
> > > +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> > > +				 ADCCNT3_CHANNEL_MASK,
> > > +				 chan->channel);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	ret = regmap_write(adc->rn5t618->regmap, RN5T618_EN_ADCIR3, ADCEND_IRQ);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> > > +				 ADCCNT3_AVG,
> > > +				 mask == IIO_CHAN_INFO_AVERAGE_RAW ?
> > > +				 ADCCNT3_AVG : 0);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	init_completion(&adc->conv_completion);
> > > +	/* single conversion */
> > > +	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
> > > +				 ADCCNT3_GODONE, ADCCNT3_GODONE);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	ret = wait_for_completion_timeout(&adc->conv_completion,
> > > +					  RN5T618_ADC_CONVERSION_TIMEOUT);
> > > +	if (ret == 0) {
> > > +		dev_warn(adc->dev, "timeout waiting for adc result\n");
> > > +		return -ETIMEDOUT;
> > > +	}
> > > +
> > > +	ret = rn5t618_read_adc_reg(adc->rn5t618,
> > > +				   RN5T618_ILIMDATAH + 2 * chan->channel,
> > > +				   &raw);
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	*val = raw;
> > > +	if (mask == IIO_CHAN_INFO_PROCESSED)
> > > +		*val = *val * REFERENCE_VOLT *
> > > +		       rn5t618_ratios[chan->channel].numerator /
> > > +		       rn5t618_ratios[chan->channel].denominator / 4095;    
> > 
> > This info should be provided as scale so that userspace can do the maths if
> > it wants to rather than handling it in kernel.
> >   
> can do as has to do? So I guess any simple shell script then cannot simply
> read out values from sysfs.

Takes a little more complexity if you want them in base units, but
not much.  The reason for this preference for just _raw where possible
is to align with what gets packed into the buffers when using
the chrdev route.  In that case you want tight packing and that is
normally much easier if you stick to whatever format the data came
off the device in.



> Hmm, how is scale defined here?
> processed in mV = raw * scale (which can be IIO_VAL_FRACTIONAL)?
Yes.  Should all be documented in the ABI docs.

Documentation/ABI/testing/sysfs-bus-iio*

> 
> > > +
> > > +	return IIO_VAL_INT;
> > > +}
> > > +
> > > +static const struct iio_info rn5t618_adc_iio_info = {
> > > +	.read_raw = &rn5t618_adc_read,
> > > +};
> > > +
> > > +#define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
> > > +	.type = _type, \
> > > +	.channel = _channel, \
> > > +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
> > > +			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
> > > +			      BIT(IIO_CHAN_INFO_PROCESSED), \    
> > 
> > Sorry, I missed this before. 
> > 
> > As a general rule it makes no sense to expose both RAW and PROCESSED values.
> > It should be possible to work one out from the other if the relationship is
> > linear and scale is provided.
> >   
> hmm, the other adc drivers, I get in touch with, expose both RAW and PROCESSED.
> like twl4030_madc. So you want to not have that for new drivers and from the
> previous comment you prefer to have PROCESSED dropped here?

We can't drop it for existing drivers and normally there is some history
to how we ended up with both.  Most commonly it is because the conversion
is non linear so we don't have a good way to represent it (often odd
calibration stuff that was added after the initial driver).  Sometimes
it was just something we missed in review, particularly in older drivers.

We can't fix it there because there may be custom userspace code relying on
it.  All the remotely standard code will happily take either option.

Jonathan

> 
> Regards,
> Andreas


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

end of thread, other threads:[~2020-02-14 12:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-20 21:20 [PATCH v2 0/3] mfd: rn5t618: add ADC support Andreas Kemnade
2020-01-20 21:20 ` [PATCH v2 1/3] iio: adc: rn5t618: Add ADC driver for RN5T618/RC5T619 Andreas Kemnade
2020-02-02 17:33   ` Jonathan Cameron
2020-02-11 16:24     ` Andreas Kemnade
2020-02-14 12:55       ` Jonathan Cameron
2020-01-20 21:20 ` [PATCH RFC v2 2/3] mfd: rn5t618: add IRQ definitions for RN5T618 Andreas Kemnade
2020-01-20 21:20 ` [PATCH RFC v2 3/3] mfd: rn5t618: add ADC subdevice " Andreas Kemnade

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