devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360
@ 2020-10-20  8:07 Gene Chen
  2020-10-20  8:07 ` [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC Gene Chen
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Gene Chen @ 2020-10-20  8:07 UTC (permalink / raw)
  To: jic23, robh+dt, matthias.bgg
  Cc: knaack.h, lars, pmeerw, devicetree, linux-iio, linux-arm-kernel,
	linux-mediatek, linux-kernel, gene_chen, Wilma.Wu, shufan_lee,
	cy_huang, benjamin.chao

In-Reply-To: 

This patch series add MT6360 ADC support contains driver, testing document
and binding document

Gene Chen (2)
  dt-bindings: iio: adc: add bindings doc for MT6360 ADC
  Documentation: ABI: testing: mt6360: Add ADC sysfs guideline
  iio: adc: mt6360: Add ADC driver for MT6360

 Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360                 |   78 ++
 Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml |   34 
 drivers/iio/adc/Kconfig                                            |   11 
 drivers/iio/adc/Makefile                                           |    1 
 drivers/iio/adc/mt6360-adc.c                                       |  372 ++++++++++
 5 files changed, 496 insertions(+)

changelogs between v1 & v2
 - adc: use IIO_CHAN_INFO_PROCESSED only
 - adc: use devm_iio_triggered_buffer_setup
 - adc: use use s64 to record timestamp

changelogs between v2 & v3
 - Rearrange include file order by alphabet
 - Set line length constraint below 100
 - Add Document for testing adc sysfs node guideline
 - Set compiler 64 bit aligned when handle iio timestamp

changelogs between v3 & v4
 - Fix sysfs guideline description
 - Replace iio channel processed by raw/scale/offset
 - Add comment of read adc flow for special HW design

changelogs between v4 & v5
 - Rename dt-bindings aligned to file name
 - Aligned sysfs node name with driver and add VBUSDIVX description
 - Add ADC channel sysfs node "*_labels"

changelogs between v5 & v6
 - Memset aligned adc data
 - Remove strong casting void pointer

changelogs between v6 & v7
 - Avoid adc polling infinite loop


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

* [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC
  2020-10-20  8:07 [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360 Gene Chen
@ 2020-10-20  8:07 ` Gene Chen
  2020-10-20 15:53   ` Rob Herring
  2020-11-01 15:34   ` Jonathan Cameron
  2020-10-20  8:07 ` [PATCH v7 2/3] Documentation: ABI: testing: mt6360: Add ADC sysfs guideline Gene Chen
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 9+ messages in thread
From: Gene Chen @ 2020-10-20  8:07 UTC (permalink / raw)
  To: jic23, robh+dt, matthias.bgg
  Cc: knaack.h, lars, pmeerw, devicetree, linux-iio, linux-arm-kernel,
	linux-mediatek, linux-kernel, gene_chen, Wilma.Wu, shufan_lee,
	cy_huang, benjamin.chao

From: Gene Chen <gene_chen@richtek.com>

This change adds the binding doc for the MT6360 ADC.

Signed-off-by: Gene Chen <gene_chen@richtek.com>
---
 .../bindings/iio/adc/mediatek,mt6360-adc.yaml      | 34 ++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml

diff --git a/Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml b/Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml
new file mode 100644
index 0000000..a7350ec
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/mediatek,mt6360-adc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Mediatek MT6360 and similar ADCs
+
+maintainers:
+  - Gene Chen <gene_chen@richtek.com>
+
+description: |
+  Family of simple ADCs with i2c interface and internal references.
+
+properties:
+  compatible:
+    const: mediatek,mt6360-adc
+
+  "#io-channel-cells":
+    const: 1
+
+required:
+  - compatible
+  - "#io-channel-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    adc {
+      compatible = "mediatek,mt6360-adc";
+      #io-channel-cells = <1>;
+    };
+...
-- 
2.7.4


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

* [PATCH v7 2/3] Documentation: ABI: testing: mt6360: Add ADC sysfs guideline
  2020-10-20  8:07 [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360 Gene Chen
  2020-10-20  8:07 ` [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC Gene Chen
@ 2020-10-20  8:07 ` Gene Chen
  2020-10-20  8:07 ` [PATCH v7 3/3] iio: adc: mt6360: Add ADC driver for MT6360 Gene Chen
  2020-11-01 15:40 ` [PATCH v7 0/3] " Jonathan Cameron
  3 siblings, 0 replies; 9+ messages in thread
From: Gene Chen @ 2020-10-20  8:07 UTC (permalink / raw)
  To: jic23, robh+dt, matthias.bgg
  Cc: knaack.h, lars, pmeerw, devicetree, linux-iio, linux-arm-kernel,
	linux-mediatek, linux-kernel, gene_chen, Wilma.Wu, shufan_lee,
	cy_huang, benjamin.chao

From: Gene Chen <gene_chen@richtek.com>

Add ABI documentation for mt6360 ADC sysfs interfaces.

Signed-off-by: Gene Chen <gene_chen@richtek.com>
---
 Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360 | 78 ++++++++++++++++++++++
 1 file changed, 78 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360

diff --git a/Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360 b/Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360
new file mode 100644
index 0000000..e5a7b1c
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360
@@ -0,0 +1,78 @@
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage0_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 USBID ADC which connected to connector ID pin.
+		Calculating with scale and offset returns voltage in uV
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage1_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 VBUS ADC with lower accuracy(+-75mA)
+		higher measure range(1~22mV)
+		Calculating with scale and offset returns voltage in uV
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage2_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 VBUS ADC with higher accuracy(+-30mA)
+		lower measure range(1~9.76V)
+		Calculating with scale and offset returns voltage in uV
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage3_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 VSYS ADC
+		Calculating with scale and offset returns voltage in uV
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage4_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 VBAT ADC
+		Calculating with scale and offset returns voltage in uV
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_current5_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 IBUS ADC
+		Calculating with scale and offset returns voltage in uA
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_current6_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 IBAT ADC
+		Calculating with scale and offset returns voltage in uA
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_current7_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 CHG_VDDP ADC
+		Calculating with scale and offset returns voltage in uV
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_temp8_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 IC junction temperature
+		Calculating with scale and offset returns temperature in degree
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage9_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 VREF_TS ADC
+		Calculating with scale and offset returns voltage in uV
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage10_raw
+KernelVersion:	5.8.0
+Contact:	gene_chen@richtek.com
+Description:
+		Indicated MT6360 TS ADC
+		Calculating with scale and offset returns voltage in uV
-- 
2.7.4


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

* [PATCH v7 3/3] iio: adc: mt6360: Add ADC driver for MT6360
  2020-10-20  8:07 [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360 Gene Chen
  2020-10-20  8:07 ` [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC Gene Chen
  2020-10-20  8:07 ` [PATCH v7 2/3] Documentation: ABI: testing: mt6360: Add ADC sysfs guideline Gene Chen
@ 2020-10-20  8:07 ` Gene Chen
  2020-11-01 15:40 ` [PATCH v7 0/3] " Jonathan Cameron
  3 siblings, 0 replies; 9+ messages in thread
From: Gene Chen @ 2020-10-20  8:07 UTC (permalink / raw)
  To: jic23, robh+dt, matthias.bgg
  Cc: knaack.h, lars, pmeerw, devicetree, linux-iio, linux-arm-kernel,
	linux-mediatek, linux-kernel, gene_chen, Wilma.Wu, shufan_lee,
	cy_huang, benjamin.chao

From: Gene Chen <gene_chen@richtek.com>

Add MT6360 ADC driver including Charger Current, Voltage, and
Temperature.

Signed-off-by: Gene Chen <gene_chen@richtek.com>
---
 drivers/iio/adc/Kconfig      |  11 ++
 drivers/iio/adc/Makefile     |   1 +
 drivers/iio/adc/mt6360-adc.c | 372 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 384 insertions(+)
 create mode 100644 drivers/iio/adc/mt6360-adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index d94dc80..a135ada 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -703,6 +703,17 @@ config MCP3911
 	  This driver can also be built as a module. If so, the module will be
 	  called mcp3911.
 
+config MEDIATEK_MT6360_ADC
+	tristate "Mediatek MT6360 ADC driver"
+	depends on MFD_MT6360
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say Y here to enable MT6360 ADC support.
+	  Integrated for System Monitoring includes
+	  is used in smartphones and tablets and supports a 11 channel
+	  general purpose ADC.
+
 config MEDIATEK_MT6577_AUXADC
 	tristate "MediaTek AUXADC driver"
 	depends on ARCH_MEDIATEK || COMPILE_TEST
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 90f94ad..5fca90a 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -65,6 +65,7 @@ obj-$(CONFIG_MAX9611) += max9611.o
 obj-$(CONFIG_MCP320X) += mcp320x.o
 obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_MCP3911) += mcp3911.o
+obj-$(CONFIG_MEDIATEK_MT6360_ADC) += mt6360-adc.o
 obj-$(CONFIG_MEDIATEK_MT6577_AUXADC) += mt6577_auxadc.o
 obj-$(CONFIG_MEN_Z188_ADC) += men_z188_adc.o
 obj-$(CONFIG_MESON_SARADC) += meson_saradc.o
diff --git a/drivers/iio/adc/mt6360-adc.c b/drivers/iio/adc/mt6360-adc.c
new file mode 100644
index 0000000..f57db30
--- /dev/null
+++ b/drivers/iio/adc/mt6360-adc.c
@@ -0,0 +1,372 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bits.h>
+#include <linux/delay.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/ktime.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/unaligned/be_byteshift.h>
+
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+
+#define MT6360_REG_PMUCHGCTRL3	0x313
+#define MT6360_REG_PMUADCCFG	0x356
+#define MT6360_REG_PMUADCIDLET	0x358
+#define MT6360_REG_PMUADCRPT1	0x35A
+
+/* PMUCHGCTRL3 0x313 */
+#define MT6360_AICR_MASK	GENMASK(7, 2)
+#define MT6360_AICR_SHFT	2
+#define MT6360_AICR_400MA	0x6
+/* PMUADCCFG 0x356 */
+#define MT6360_ADCEN_MASK	BIT(15)
+/* PMUADCRPT1 0x35A */
+#define MT6360_PREFERCH_MASK	GENMASK(7, 4)
+#define MT6360_PREFERCH_SHFT	4
+#define MT6360_RPTCH_MASK	GENMASK(3, 0)
+#define MT6360_NO_PREFER	15
+
+/* Time in ms */
+#define ADC_WAIT_TIME_MS	25
+#define ADC_CONV_TIMEOUT_MS	100
+#define ADC_LOOP_TIME_US	2000
+
+enum {
+	MT6360_CHAN_USBID = 0,
+	MT6360_CHAN_VBUSDIV5,
+	MT6360_CHAN_VBUSDIV2,
+	MT6360_CHAN_VSYS,
+	MT6360_CHAN_VBAT,
+	MT6360_CHAN_IBUS,
+	MT6360_CHAN_IBAT,
+	MT6360_CHAN_CHG_VDDP,
+	MT6360_CHAN_TEMP_JC,
+	MT6360_CHAN_VREF_TS,
+	MT6360_CHAN_TS,
+	MT6360_CHAN_MAX
+};
+
+struct mt6360_adc_data {
+	struct device *dev;
+	struct regmap *regmap;
+	/* Due to only one set of ADC control, this lock is used to prevent the race condition */
+	struct mutex adc_lock;
+	ktime_t last_off_timestamps[MT6360_CHAN_MAX];
+};
+
+static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int *val)
+{
+	__be16 adc_enable;
+	u8 rpt[3];
+	ktime_t predict_end_t, timeout;
+	unsigned int pre_wait_time;
+	int ret;
+
+	mutex_lock(&mad->adc_lock);
+
+	/* Select the preferred ADC channel */
+	ret = regmap_update_bits(mad->regmap, MT6360_REG_PMUADCRPT1, MT6360_PREFERCH_MASK,
+				 channel << MT6360_PREFERCH_SHFT);
+	if (ret)
+		goto out_adc_lock;
+
+	adc_enable = cpu_to_be16(MT6360_ADCEN_MASK | BIT(channel));
+	ret = regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG, &adc_enable, sizeof(adc_enable));
+	if (ret)
+		goto out_adc_lock;
+
+	predict_end_t = ktime_add_ms(mad->last_off_timestamps[channel], 2 * ADC_WAIT_TIME_MS);
+
+	if (ktime_after(ktime_get(), predict_end_t))
+		pre_wait_time = ADC_WAIT_TIME_MS;
+	else
+		pre_wait_time = 3 * ADC_WAIT_TIME_MS;
+
+	if (msleep_interruptible(pre_wait_time)) {
+		ret = -ERESTARTSYS;
+		goto out_adc_conv;
+	}
+
+	timeout = ktime_add_ms(ktime_get(), ADC_CONV_TIMEOUT_MS);
+	while (true) {
+		ret = regmap_raw_read(mad->regmap, MT6360_REG_PMUADCRPT1, rpt, sizeof(rpt));
+		if (ret)
+			goto out_adc_conv;
+
+		/*
+		 * There are two functions, ZCV and TypeC OTP, running ADC VBAT and TS in
+		 * background, and ADC samples are taken on a fixed frequency no matter read the
+		 * previous one or not.
+		 * To avoid conflict, We set minimum time threshold after enable ADC and
+		 * check report channel is the same.
+		 * The worst case is run the same ADC twice and background function is also running,
+		 * ADC conversion sequence is desire channel before start ADC, background ADC,
+		 * desire channel after start ADC.
+		 * So the minimum correct data is three times of typical conversion time.
+		 */
+		if ((rpt[0] & MT6360_RPTCH_MASK) == channel)
+			break;
+
+		if (ktime_compare(ktime_get(), timeout) > 0) {
+			ret = -ETIMEDOUT;
+			goto out_adc_conv;
+		}
+
+		usleep_range(ADC_LOOP_TIME_US / 2, ADC_LOOP_TIME_US);
+	}
+
+	*val = rpt[1] << 8 | rpt[2];
+	ret = IIO_VAL_INT;
+
+out_adc_conv:
+	/* Only keep ADC enable */
+	adc_enable = cpu_to_be16(MT6360_ADCEN_MASK);
+	regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG, &adc_enable, sizeof(adc_enable));
+	mad->last_off_timestamps[channel] = ktime_get();
+	/* Config prefer channel to NO_PREFER */
+	regmap_update_bits(mad->regmap, MT6360_REG_PMUADCRPT1, MT6360_PREFERCH_MASK,
+			   MT6360_NO_PREFER << MT6360_PREFERCH_SHFT);
+out_adc_lock:
+	mutex_unlock(&mad->adc_lock);
+
+	return ret;
+}
+
+static int mt6360_adc_read_scale(struct mt6360_adc_data *mad, int channel, int *val, int *val2)
+{
+	unsigned int regval;
+	int ret;
+
+	switch (channel) {
+	case MT6360_CHAN_USBID:
+	case MT6360_CHAN_VSYS:
+	case MT6360_CHAN_VBAT:
+	case MT6360_CHAN_CHG_VDDP:
+	case MT6360_CHAN_VREF_TS:
+	case MT6360_CHAN_TS:
+		*val = 1250;
+		return IIO_VAL_INT;
+	case MT6360_CHAN_VBUSDIV5:
+		*val = 6250;
+		return IIO_VAL_INT;
+	case MT6360_CHAN_VBUSDIV2:
+	case MT6360_CHAN_IBUS:
+	case MT6360_CHAN_IBAT:
+		*val = 2500;
+
+		if (channel == MT6360_CHAN_IBUS) {
+			/* IBUS will be affected by input current limit for the different Ron */
+			/* Check whether the config is <400mA or not */
+			ret = regmap_read(mad->regmap, MT6360_REG_PMUCHGCTRL3, &regval);
+			if (ret)
+				return ret;
+
+			regval = (regval & MT6360_AICR_MASK) >> MT6360_AICR_SHFT;
+			if (regval < MT6360_AICR_400MA)
+				*val = 1900;
+		}
+
+		return IIO_VAL_INT;
+	case MT6360_CHAN_TEMP_JC:
+		*val = 105;
+		*val2 = 100;
+		return IIO_VAL_FRACTIONAL;
+	}
+
+	return -EINVAL;
+}
+
+static int mt6360_adc_read_offset(struct mt6360_adc_data *mad, int channel, int *val)
+{
+	*val = (channel == MT6360_CHAN_TEMP_JC) ? -80 : 0;
+	return IIO_VAL_INT;
+}
+
+static int mt6360_adc_read_raw(struct iio_dev *iio_dev, const struct iio_chan_spec *chan,
+			       int *val, int *val2, long mask)
+{
+	struct mt6360_adc_data *mad = iio_priv(iio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		return mt6360_adc_read_channel(mad, chan->channel, val);
+	case IIO_CHAN_INFO_SCALE:
+		return mt6360_adc_read_scale(mad, chan->channel, val, val2);
+	case IIO_CHAN_INFO_OFFSET:
+		return mt6360_adc_read_offset(mad, chan->channel, val);
+	}
+
+	return -EINVAL;
+}
+
+static const char *mt6360_channel_labels[MT6360_CHAN_MAX] = {
+	"usbid", "vbusdiv5", "vbusdiv2", "vsys", "vbat", "ibus", "ibat", "chg_vddp",
+	"temp_jc", "vref_ts", "ts",
+};
+
+static int mt6360_adc_read_label(struct iio_dev *iio_dev, const struct iio_chan_spec *chan,
+				 char *label)
+{
+	return snprintf(label, PAGE_SIZE, "%s\n", mt6360_channel_labels[chan->channel]);
+}
+
+static const struct iio_info mt6360_adc_iio_info = {
+	.read_raw = mt6360_adc_read_raw,
+	.read_label = mt6360_adc_read_label,
+};
+
+#define MT6360_ADC_CHAN(_idx, _type) {				\
+	.type = _type,						\
+	.channel = MT6360_CHAN_##_idx,				\
+	.scan_index = MT6360_CHAN_##_idx,			\
+	.datasheet_name = #_idx,				\
+	.scan_type =  {						\
+		.sign = 'u',					\
+		.realbits = 16,					\
+		.storagebits = 16,				\
+		.endianness = IIO_CPU,				\
+	},							\
+	.indexed = 1,						\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
+				BIT(IIO_CHAN_INFO_SCALE) |	\
+				BIT(IIO_CHAN_INFO_OFFSET),	\
+}
+
+static const struct iio_chan_spec mt6360_adc_channels[] = {
+	MT6360_ADC_CHAN(USBID, IIO_VOLTAGE),
+	MT6360_ADC_CHAN(VBUSDIV5, IIO_VOLTAGE),
+	MT6360_ADC_CHAN(VBUSDIV2, IIO_VOLTAGE),
+	MT6360_ADC_CHAN(VSYS, IIO_VOLTAGE),
+	MT6360_ADC_CHAN(VBAT, IIO_VOLTAGE),
+	MT6360_ADC_CHAN(IBUS, IIO_CURRENT),
+	MT6360_ADC_CHAN(IBAT, IIO_CURRENT),
+	MT6360_ADC_CHAN(CHG_VDDP, IIO_VOLTAGE),
+	MT6360_ADC_CHAN(TEMP_JC, IIO_TEMP),
+	MT6360_ADC_CHAN(VREF_TS, IIO_VOLTAGE),
+	MT6360_ADC_CHAN(TS, IIO_VOLTAGE),
+	IIO_CHAN_SOFT_TIMESTAMP(MT6360_CHAN_MAX),
+};
+
+static irqreturn_t mt6360_adc_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct mt6360_adc_data *mad = iio_priv(indio_dev);
+	struct {
+		u16 values[MT6360_CHAN_MAX];
+		int64_t timestamp;
+	} data __aligned(8);
+	int i = 0, bit, val, ret;
+
+	memset(&data, 0, sizeof(data));
+	for_each_set_bit(bit, indio_dev->active_scan_mask, indio_dev->masklength) {
+		ret = mt6360_adc_read_channel(mad, bit, &val);
+		if (ret < 0) {
+			dev_warn(&indio_dev->dev, "Failed to get channel %d conversion val\n", bit);
+			goto out;
+		}
+
+		data.values[i++] = val;
+	}
+	iio_push_to_buffers_with_timestamp(indio_dev, &data, iio_get_time_ns(indio_dev));
+out:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+static inline int mt6360_adc_reset(struct mt6360_adc_data *info)
+{
+	__be16 adc_enable;
+	ktime_t all_off_time;
+	int i, ret;
+
+	/* Clear ADC idle wait time to 0 */
+	ret = regmap_write(info->regmap, MT6360_REG_PMUADCIDLET, 0);
+	if (ret)
+		return ret;
+
+	/* Only keep ADC enable, but keep all channels off */
+	adc_enable = cpu_to_be16(MT6360_ADCEN_MASK);
+	ret = regmap_raw_write(info->regmap, MT6360_REG_PMUADCCFG, &adc_enable, sizeof(adc_enable));
+	if (ret)
+		return ret;
+
+	/* Reset all channel off time to the current one */
+	all_off_time = ktime_get();
+	for (i = 0; i < MT6360_CHAN_MAX; i++)
+		info->last_off_timestamps[i] = all_off_time;
+
+	return 0;
+}
+
+static int mt6360_adc_probe(struct platform_device *pdev)
+{
+	struct mt6360_adc_data *mad;
+	struct regmap *regmap;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!regmap) {
+		dev_err(&pdev->dev, "Failed to get parent regmap\n");
+		return -ENODEV;
+	}
+
+	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*mad));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	mad = iio_priv(indio_dev);
+	mad->dev = &pdev->dev;
+	mad->regmap = regmap;
+	mutex_init(&mad->adc_lock);
+
+	ret = mt6360_adc_reset(mad);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "Failed to reset adc\n");
+		return ret;
+	}
+
+	indio_dev->name = dev_name(&pdev->dev);
+	indio_dev->dev.parent = &pdev->dev;
+	indio_dev->info = &mt6360_adc_iio_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = mt6360_adc_channels;
+	indio_dev->num_channels = ARRAY_SIZE(mt6360_adc_channels);
+
+	ret = devm_iio_triggered_buffer_setup(&pdev->dev, indio_dev, NULL,
+					      mt6360_adc_trigger_handler, NULL);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to allocate iio trigger buffer\n");
+		return ret;
+	}
+
+	return devm_iio_device_register(&pdev->dev, indio_dev);
+}
+
+static const struct of_device_id __maybe_unused mt6360_adc_of_id[] = {
+	{ .compatible = "mediatek,mt6360-adc", },
+	{}
+};
+MODULE_DEVICE_TABLE(of, mt6360_adc_of_id);
+
+static struct platform_driver mt6360_adc_driver = {
+	.driver = {
+		.name = "mt6360-adc",
+		.of_match_table = mt6360_adc_of_id,
+	},
+	.probe = mt6360_adc_probe,
+};
+module_platform_driver(mt6360_adc_driver);
+
+MODULE_AUTHOR("Gene Chen <gene_chen@richtek.com>");
+MODULE_DESCRIPTION("MT6360 ADC Driver");
+MODULE_LICENSE("GPL v2");
-- 
2.7.4


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

* Re: [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC
  2020-10-20  8:07 ` [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC Gene Chen
@ 2020-10-20 15:53   ` Rob Herring
  2020-11-01 15:34   ` Jonathan Cameron
  1 sibling, 0 replies; 9+ messages in thread
From: Rob Herring @ 2020-10-20 15:53 UTC (permalink / raw)
  To: Gene Chen
  Cc: linux-kernel, Wilma.Wu, matthias.bgg, knaack.h, cy_huang, lars,
	jic23, gene_chen, linux-iio, benjamin.chao, linux-arm-kernel,
	robh+dt, shufan_lee, devicetree, linux-mediatek, pmeerw

On Tue, 20 Oct 2020 16:07:45 +0800, Gene Chen wrote:
> From: Gene Chen <gene_chen@richtek.com>
> 
> This change adds the binding doc for the MT6360 ADC.
> 
> Signed-off-by: Gene Chen <gene_chen@richtek.com>
> ---
>  .../bindings/iio/adc/mediatek,mt6360-adc.yaml      | 34 ++++++++++++++++++++++
>  1 file changed, 34 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml
> 


Please add Acked-by/Reviewed-by tags when posting new versions. However,
there's no need to repost patches *only* to add the tags. The upstream
maintainer will do that for acks received on the version they apply.

If a tag was not added on purpose, please state why and what changed.


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

* Re: [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC
  2020-10-20  8:07 ` [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC Gene Chen
  2020-10-20 15:53   ` Rob Herring
@ 2020-11-01 15:34   ` Jonathan Cameron
  1 sibling, 0 replies; 9+ messages in thread
From: Jonathan Cameron @ 2020-11-01 15:34 UTC (permalink / raw)
  To: Gene Chen
  Cc: robh+dt, matthias.bgg, knaack.h, lars, pmeerw, devicetree,
	linux-iio, linux-arm-kernel, linux-mediatek, linux-kernel,
	gene_chen, Wilma.Wu, shufan_lee, cy_huang, benjamin.chao

On Tue, 20 Oct 2020 16:07:45 +0800
Gene Chen <gene.chen.richtek@gmail.com> wrote:

> From: Gene Chen <gene_chen@richtek.com>
> 
> This change adds the binding doc for the MT6360 ADC.

One thing I'd previously missed below.

Jonathan

> 
> Signed-off-by: Gene Chen <gene_chen@richtek.com>
> ---
>  .../bindings/iio/adc/mediatek,mt6360-adc.yaml      | 34 ++++++++++++++++++++++
>  1 file changed, 34 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml
> 
> diff --git a/Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml b/Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml
> new file mode 100644
> index 0000000..a7350ec
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml
> @@ -0,0 +1,34 @@
> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/iio/adc/mediatek,mt6360-adc.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Mediatek MT6360 and similar ADCs
> +
> +maintainers:
> +  - Gene Chen <gene_chen@richtek.com>
> +
> +description: |
> +  Family of simple ADCs with i2c interface and internal references.

That seems unlikely...

> +
> +properties:
> +  compatible:
> +    const: mediatek,mt6360-adc
> +
> +  "#io-channel-cells":
> +    const: 1
> +
> +required:
> +  - compatible
> +  - "#io-channel-cells"
> +
> +additionalProperties: false
> +
> +examples:
> +  - |
> +    adc {
> +      compatible = "mediatek,mt6360-adc";
> +      #io-channel-cells = <1>;
> +    };
> +...


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

* Re: [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360
  2020-10-20  8:07 [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360 Gene Chen
                   ` (2 preceding siblings ...)
  2020-10-20  8:07 ` [PATCH v7 3/3] iio: adc: mt6360: Add ADC driver for MT6360 Gene Chen
@ 2020-11-01 15:40 ` Jonathan Cameron
  2020-11-02  7:04   ` Gene Chen
  2020-11-08 16:23   ` Jonathan Cameron
  3 siblings, 2 replies; 9+ messages in thread
From: Jonathan Cameron @ 2020-11-01 15:40 UTC (permalink / raw)
  To: Gene Chen
  Cc: robh+dt, matthias.bgg, knaack.h, lars, pmeerw, devicetree,
	linux-iio, linux-arm-kernel, linux-mediatek, linux-kernel,
	gene_chen, Wilma.Wu, shufan_lee, cy_huang, benjamin.chao

On Tue, 20 Oct 2020 16:07:44 +0800
Gene Chen <gene.chen.richtek@gmail.com> wrote:

> In-Reply-To: 
> 
> This patch series add MT6360 ADC support contains driver, testing document
> and binding document
> 
Hi Gene,

Other than that small edit needed in the dt binding this is stalled on
the label code getting upstreamed into the core.

I've asked Cristian to repost that so hopefully we can resolve that dependency
and get this merged fairly soon.

For the dt thing I can just drop the description entirely, but would prefer
if you sent a v8 fixing the tags issue Rob pointed out and also fixing that
description.

Thanks,

Jonathan

> Gene Chen (2)
>   dt-bindings: iio: adc: add bindings doc for MT6360 ADC
>   Documentation: ABI: testing: mt6360: Add ADC sysfs guideline
>   iio: adc: mt6360: Add ADC driver for MT6360
> 
>  Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360                 |   78 ++
>  Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml |   34 
>  drivers/iio/adc/Kconfig                                            |   11 
>  drivers/iio/adc/Makefile                                           |    1 
>  drivers/iio/adc/mt6360-adc.c                                       |  372 ++++++++++
>  5 files changed, 496 insertions(+)
> 
> changelogs between v1 & v2
>  - adc: use IIO_CHAN_INFO_PROCESSED only
>  - adc: use devm_iio_triggered_buffer_setup
>  - adc: use use s64 to record timestamp
> 
> changelogs between v2 & v3
>  - Rearrange include file order by alphabet
>  - Set line length constraint below 100
>  - Add Document for testing adc sysfs node guideline
>  - Set compiler 64 bit aligned when handle iio timestamp
> 
> changelogs between v3 & v4
>  - Fix sysfs guideline description
>  - Replace iio channel processed by raw/scale/offset
>  - Add comment of read adc flow for special HW design
> 
> changelogs between v4 & v5
>  - Rename dt-bindings aligned to file name
>  - Aligned sysfs node name with driver and add VBUSDIVX description
>  - Add ADC channel sysfs node "*_labels"
> 
> changelogs between v5 & v6
>  - Memset aligned adc data
>  - Remove strong casting void pointer
> 
> changelogs between v6 & v7
>  - Avoid adc polling infinite loop
> 


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

* Re: [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360
  2020-11-01 15:40 ` [PATCH v7 0/3] " Jonathan Cameron
@ 2020-11-02  7:04   ` Gene Chen
  2020-11-08 16:23   ` Jonathan Cameron
  1 sibling, 0 replies; 9+ messages in thread
From: Gene Chen @ 2020-11-02  7:04 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Rob Herring, Matthias Brugger, knaack.h, lars, pmeerw,
	devicetree, linux-iio, linux-arm Mailing List,
	moderated list:ARM/Mediatek SoC support,
	Linux Kernel Mailing List, Gene Chen, Wilma.Wu, shufan_lee,
	cy_huang, benjamin.chao

Jonathan Cameron <jic23@kernel.org> 於 2020年11月1日 週日 下午11:40寫道:
>
> On Tue, 20 Oct 2020 16:07:44 +0800
> Gene Chen <gene.chen.richtek@gmail.com> wrote:
>
> > In-Reply-To:
> >
> > This patch series add MT6360 ADC support contains driver, testing document
> > and binding document
> >
> Hi Gene,
>
> Other than that small edit needed in the dt binding this is stalled on
> the label code getting upstreamed into the core.
>
> I've asked Cristian to repost that so hopefully we can resolve that dependency
> and get this merged fairly soon.
>
> For the dt thing I can just drop the description entirely, but would prefer
> if you sent a v8 fixing the tags issue Rob pointed out and also fixing that
> description.
>
> Thanks,
>
> Jonathan
>

=============
> +description: |
> +  Family of simple ADCs with i2c interface and internal references.
That seems unlikely...
=============

Do you means I should remove this description?
If yes, I will also update PATCH v8 for add tags and fix dt-bindings.

> > Gene Chen (2)
> >   dt-bindings: iio: adc: add bindings doc for MT6360 ADC
> >   Documentation: ABI: testing: mt6360: Add ADC sysfs guideline
> >   iio: adc: mt6360: Add ADC driver for MT6360
> >
> >  Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360                 |   78 ++
> >  Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml |   34
> >  drivers/iio/adc/Kconfig                                            |   11
> >  drivers/iio/adc/Makefile                                           |    1
> >  drivers/iio/adc/mt6360-adc.c                                       |  372 ++++++++++
> >  5 files changed, 496 insertions(+)
> >
> > changelogs between v1 & v2
> >  - adc: use IIO_CHAN_INFO_PROCESSED only
> >  - adc: use devm_iio_triggered_buffer_setup
> >  - adc: use use s64 to record timestamp
> >
> > changelogs between v2 & v3
> >  - Rearrange include file order by alphabet
> >  - Set line length constraint below 100
> >  - Add Document for testing adc sysfs node guideline
> >  - Set compiler 64 bit aligned when handle iio timestamp
> >
> > changelogs between v3 & v4
> >  - Fix sysfs guideline description
> >  - Replace iio channel processed by raw/scale/offset
> >  - Add comment of read adc flow for special HW design
> >
> > changelogs between v4 & v5
> >  - Rename dt-bindings aligned to file name
> >  - Aligned sysfs node name with driver and add VBUSDIVX description
> >  - Add ADC channel sysfs node "*_labels"
> >
> > changelogs between v5 & v6
> >  - Memset aligned adc data
> >  - Remove strong casting void pointer
> >
> > changelogs between v6 & v7
> >  - Avoid adc polling infinite loop
> >
>

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

* Re: [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360
  2020-11-01 15:40 ` [PATCH v7 0/3] " Jonathan Cameron
  2020-11-02  7:04   ` Gene Chen
@ 2020-11-08 16:23   ` Jonathan Cameron
  1 sibling, 0 replies; 9+ messages in thread
From: Jonathan Cameron @ 2020-11-08 16:23 UTC (permalink / raw)
  To: Gene Chen
  Cc: robh+dt, matthias.bgg, knaack.h, lars, pmeerw, devicetree,
	linux-iio, linux-arm-kernel, linux-mediatek, linux-kernel,
	gene_chen, Wilma.Wu, shufan_lee, cy_huang, benjamin.chao

On Sun, 1 Nov 2020 15:40:19 +0000
Jonathan Cameron <jic23@kernel.org> wrote:

> On Tue, 20 Oct 2020 16:07:44 +0800
> Gene Chen <gene.chen.richtek@gmail.com> wrote:
> 
> > In-Reply-To: 
> > 
> > This patch series add MT6360 ADC support contains driver, testing document
> > and binding document
> >   
> Hi Gene,
> 
> Other than that small edit needed in the dt binding this is stalled on
> the label code getting upstreamed into the core.
> 
> I've asked Cristian to repost that so hopefully we can resolve that dependency
> and get this merged fairly soon.
> 
> For the dt thing I can just drop the description entirely, but would prefer
> if you sent a v8 fixing the tags issue Rob pointed out and also fixing that
> description.
I've tweaked as described and applied to the togreg branch of iio.git and
pushed out as testing for the autobuilders to poke at it more than I do.

Thanks,

Jonathan

> 
> Thanks,
> 
> Jonathan
> 
> > Gene Chen (2)
> >   dt-bindings: iio: adc: add bindings doc for MT6360 ADC
> >   Documentation: ABI: testing: mt6360: Add ADC sysfs guideline
> >   iio: adc: mt6360: Add ADC driver for MT6360
> > 
> >  Documentation/ABI/testing/sysfs-bus-iio-adc-mt6360                 |   78 ++
> >  Documentation/devicetree/bindings/iio/adc/mediatek,mt6360-adc.yaml |   34 
> >  drivers/iio/adc/Kconfig                                            |   11 
> >  drivers/iio/adc/Makefile                                           |    1 
> >  drivers/iio/adc/mt6360-adc.c                                       |  372 ++++++++++
> >  5 files changed, 496 insertions(+)
> > 
> > changelogs between v1 & v2
> >  - adc: use IIO_CHAN_INFO_PROCESSED only
> >  - adc: use devm_iio_triggered_buffer_setup
> >  - adc: use use s64 to record timestamp
> > 
> > changelogs between v2 & v3
> >  - Rearrange include file order by alphabet
> >  - Set line length constraint below 100
> >  - Add Document for testing adc sysfs node guideline
> >  - Set compiler 64 bit aligned when handle iio timestamp
> > 
> > changelogs between v3 & v4
> >  - Fix sysfs guideline description
> >  - Replace iio channel processed by raw/scale/offset
> >  - Add comment of read adc flow for special HW design
> > 
> > changelogs between v4 & v5
> >  - Rename dt-bindings aligned to file name
> >  - Aligned sysfs node name with driver and add VBUSDIVX description
> >  - Add ADC channel sysfs node "*_labels"
> > 
> > changelogs between v5 & v6
> >  - Memset aligned adc data
> >  - Remove strong casting void pointer
> > 
> > changelogs between v6 & v7
> >  - Avoid adc polling infinite loop
> >   
> 


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

end of thread, other threads:[~2020-11-08 16:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-20  8:07 [PATCH v7 0/3] iio: adc: mt6360: Add ADC driver for MT6360 Gene Chen
2020-10-20  8:07 ` [PATCH v7 1/3] dt-bindings: iio: adc: add bindings doc for MT6360 ADC Gene Chen
2020-10-20 15:53   ` Rob Herring
2020-11-01 15:34   ` Jonathan Cameron
2020-10-20  8:07 ` [PATCH v7 2/3] Documentation: ABI: testing: mt6360: Add ADC sysfs guideline Gene Chen
2020-10-20  8:07 ` [PATCH v7 3/3] iio: adc: mt6360: Add ADC driver for MT6360 Gene Chen
2020-11-01 15:40 ` [PATCH v7 0/3] " Jonathan Cameron
2020-11-02  7:04   ` Gene Chen
2020-11-08 16:23   ` 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).