Re: [PATCH v7 09/13] iio: adc: mt6370: Add MediaTek MT6370 support

2022-08-13 Thread Jonathan Cameron
On Fri, 5 Aug 2022 12:54:04 +0200
Andy Shevchenko  wrote:

> On Fri, Aug 5, 2022 at 9:07 AM ChiaEn Wu  wrote:
> >
> > From: ChiaEn Wu 
> >
> > MediaTek MT6370 is a SubPMIC consisting of a single cell battery charger
> > with ADC monitoring, RGB LEDs, dual channel flashlight, WLED backlight
> > driver, display bias voltage supply, one general purpose LDO, and the
> > USB Type-C & PD controller complies with the latest USB Type-C and PD
> > standards.
> >
> > Add a support for the MT6370 ADC driver for system monitoring, including
> > charger current, voltage, and temperature.  
> 
> On Fri, Aug 5, 2022 at 9:07 AM ChiaEn Wu  wrote:
> >
> > From: ChiYuan Huang 
> >
> > The MediaTek MT6370 is a highly-integrated smart power management IC,
> > which includes a single cell Li-Ion/Li-Polymer switching battery
> > charger, a USB Type-C & Power Delivery (PD) controller, dual
> > Flash LED current sources, a RGB LED driver, a backlight WLED driver,
> > a display bias driver and a general LDO for portable devices.
> >
> > Add a support for the Type-C & Power Delivery controller in
> > MediaTek MT6370 IC.  
> 
> Reviewed-by: Andy Shevchenko 
Oops. I just noticed I replied only to Andy due to a misclick.

Acked-by: Jonathan Cameron 

I took a quick glance through, and with Andy's comments now all answered
to his satisfaction I'm fine with this going through the MFD tree
(or another path if one makes more sense).

Thanks to Andy for his reviewing effort on this one!

Note that because of the bindings header I think this has a build dependency on 
the
MFD binding patch.

Jonathan


> 
> > Reviewed-by: AngeloGioacchino Del Regno 
> > 
> > Signed-off-by: ChiaEn Wu 
> > ---
> >
> > v7
> > - Add AICR(100mA ~ 350mA), ICHG(100mA ~ 800mA) macros
> > - Remove 400mA AICR and 900mA ICHG macros
> > - Revise using 'if-else' to 'switch-case' in mt6370_adc_read_scale()
> >   where the adc channel is ibus or ibat
> > ---
> >  drivers/iio/adc/Kconfig  |  12 ++
> >  drivers/iio/adc/Makefile |   1 +
> >  drivers/iio/adc/mt6370-adc.c | 305 
> > +++
> >  3 files changed, 318 insertions(+)
> >  create mode 100644 drivers/iio/adc/mt6370-adc.c
> >
> > diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> > index 7fe5930..995cbb5 100644
> > --- a/drivers/iio/adc/Kconfig
> > +++ b/drivers/iio/adc/Kconfig
> > @@ -736,6 +736,18 @@ config MEDIATEK_MT6360_ADC
> >   is used in smartphones and tablets and supports a 11 channel
> >   general purpose ADC.
> >
> > +config MEDIATEK_MT6370_ADC
> > +   tristate "MediaTek MT6370 ADC driver"
> > +   depends on MFD_MT6370
> > +   help
> > + Say yes here to enable MediaTek MT6370 ADC support.
> > +
> > + This ADC driver provides 9 channels for system monitoring (charger
> > + current, voltage, and temperature).
> > +
> > + This driver can also be built as a module. If so, the module
> > + will be called "mt6370-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 1772a54..c6bc35f 100644
> > --- a/drivers/iio/adc/Makefile
> > +++ b/drivers/iio/adc/Makefile
> > @@ -68,6 +68,7 @@ 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_MT6370_ADC) += mt6370-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/mt6370-adc.c b/drivers/iio/adc/mt6370-adc.c
> > new file mode 100644
> > index 000..2a46471
> > --- /dev/null
> > +++ b/drivers/iio/adc/mt6370-adc.c
> > @@ -0,0 +1,305 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Copyright (C) 2022 Richtek Technology Corp.
> > + *
> > + * Author: ChiaEn Wu 
> > + */
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#include 
> > +
> > +#define MT6370_REG_CHG_CTRL3   0x113
> > +#define MT6370_REG_CHG_CTRL7   0x117
> > +#define MT6370_REG_CHG_ADC 0x121
> > +#define MT6370_REG_ADC_DATA_H  0x14C
> > +
> > +#define MT6370_ADC_START_MASK  BIT(0)
> > +#define MT6370_ADC_IN_SEL_MASK GENMASK(7, 4)
> > +#define MT6370_AICR_ICHG_MASK  GENMASK(7, 2)
> > +
> > +#define MT6370_AICR_100_mA 0x0
> > +#define MT6370_AICR_150_mA 0x1
> > +#define MT6370_AICR_200_mA 0x2
> > +#define MT6370_AICR_250_mA 0x3
> > +#define MT6370_AICR_300_mA 0x4
> > +#define MT6370_AICR_350_mA 0x5
> > +
> > +#define MT6370_ICHG_100_mA 0x0
> > 

Re: [PATCH v7 09/13] iio: adc: mt6370: Add MediaTek MT6370 support

2022-08-05 Thread Andy Shevchenko
On Fri, Aug 5, 2022 at 9:07 AM ChiaEn Wu  wrote:
>
> From: ChiaEn Wu 
>
> MediaTek MT6370 is a SubPMIC consisting of a single cell battery charger
> with ADC monitoring, RGB LEDs, dual channel flashlight, WLED backlight
> driver, display bias voltage supply, one general purpose LDO, and the
> USB Type-C & PD controller complies with the latest USB Type-C and PD
> standards.
>
> Add a support for the MT6370 ADC driver for system monitoring, including
> charger current, voltage, and temperature.

On Fri, Aug 5, 2022 at 9:07 AM ChiaEn Wu  wrote:
>
> From: ChiYuan Huang 
>
> The MediaTek MT6370 is a highly-integrated smart power management IC,
> which includes a single cell Li-Ion/Li-Polymer switching battery
> charger, a USB Type-C & Power Delivery (PD) controller, dual
> Flash LED current sources, a RGB LED driver, a backlight WLED driver,
> a display bias driver and a general LDO for portable devices.
>
> Add a support for the Type-C & Power Delivery controller in
> MediaTek MT6370 IC.

Reviewed-by: Andy Shevchenko 

> Reviewed-by: AngeloGioacchino Del Regno 
> 
> Signed-off-by: ChiaEn Wu 
> ---
>
> v7
> - Add AICR(100mA ~ 350mA), ICHG(100mA ~ 800mA) macros
> - Remove 400mA AICR and 900mA ICHG macros
> - Revise using 'if-else' to 'switch-case' in mt6370_adc_read_scale()
>   where the adc channel is ibus or ibat
> ---
>  drivers/iio/adc/Kconfig  |  12 ++
>  drivers/iio/adc/Makefile |   1 +
>  drivers/iio/adc/mt6370-adc.c | 305 
> +++
>  3 files changed, 318 insertions(+)
>  create mode 100644 drivers/iio/adc/mt6370-adc.c
>
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 7fe5930..995cbb5 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -736,6 +736,18 @@ config MEDIATEK_MT6360_ADC
>   is used in smartphones and tablets and supports a 11 channel
>   general purpose ADC.
>
> +config MEDIATEK_MT6370_ADC
> +   tristate "MediaTek MT6370 ADC driver"
> +   depends on MFD_MT6370
> +   help
> + Say yes here to enable MediaTek MT6370 ADC support.
> +
> + This ADC driver provides 9 channels for system monitoring (charger
> + current, voltage, and temperature).
> +
> + This driver can also be built as a module. If so, the module
> + will be called "mt6370-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 1772a54..c6bc35f 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -68,6 +68,7 @@ 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_MT6370_ADC) += mt6370-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/mt6370-adc.c b/drivers/iio/adc/mt6370-adc.c
> new file mode 100644
> index 000..2a46471
> --- /dev/null
> +++ b/drivers/iio/adc/mt6370-adc.c
> @@ -0,0 +1,305 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2022 Richtek Technology Corp.
> + *
> + * Author: ChiaEn Wu 
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include 
> +
> +#define MT6370_REG_CHG_CTRL3   0x113
> +#define MT6370_REG_CHG_CTRL7   0x117
> +#define MT6370_REG_CHG_ADC 0x121
> +#define MT6370_REG_ADC_DATA_H  0x14C
> +
> +#define MT6370_ADC_START_MASK  BIT(0)
> +#define MT6370_ADC_IN_SEL_MASK GENMASK(7, 4)
> +#define MT6370_AICR_ICHG_MASK  GENMASK(7, 2)
> +
> +#define MT6370_AICR_100_mA 0x0
> +#define MT6370_AICR_150_mA 0x1
> +#define MT6370_AICR_200_mA 0x2
> +#define MT6370_AICR_250_mA 0x3
> +#define MT6370_AICR_300_mA 0x4
> +#define MT6370_AICR_350_mA 0x5
> +
> +#define MT6370_ICHG_100_mA 0x0
> +#define MT6370_ICHG_200_mA 0x1
> +#define MT6370_ICHG_300_mA 0x2
> +#define MT6370_ICHG_400_mA 0x3
> +#define MT6370_ICHG_500_mA 0x4
> +#define MT6370_ICHG_600_mA 0x5
> +#define MT6370_ICHG_700_mA 0x6
> +#define MT6370_ICHG_800_mA 0x7
> +
> +#define ADC_CONV_TIME_MS   35
> +#define ADC_CONV_POLLING_TIME_US   1000
> +
> +struct mt6370_adc_data {
> +   struct device *dev;
> +   struct regmap *regmap;
> +   /*
> +* This mutex lock is for preventing the different ADC channels
> +* from being read at the same time.
> +*/
> +   struct mutex adc_lock;
> +};
> +
> +static int mt6370_adc_read_channel(struct mt6370_adc_data 

[PATCH v7 09/13] iio: adc: mt6370: Add MediaTek MT6370 support

2022-08-05 Thread ChiaEn Wu
From: ChiaEn Wu 

MediaTek MT6370 is a SubPMIC consisting of a single cell battery charger
with ADC monitoring, RGB LEDs, dual channel flashlight, WLED backlight
driver, display bias voltage supply, one general purpose LDO, and the
USB Type-C & PD controller complies with the latest USB Type-C and PD
standards.

Add a support for the MT6370 ADC driver for system monitoring, including
charger current, voltage, and temperature.

Reviewed-by: AngeloGioacchino Del Regno 

Signed-off-by: ChiaEn Wu 
---

v7
- Add AICR(100mA ~ 350mA), ICHG(100mA ~ 800mA) macros
- Remove 400mA AICR and 900mA ICHG macros
- Revise using 'if-else' to 'switch-case' in mt6370_adc_read_scale()
  where the adc channel is ibus or ibat
---
 drivers/iio/adc/Kconfig  |  12 ++
 drivers/iio/adc/Makefile |   1 +
 drivers/iio/adc/mt6370-adc.c | 305 +++
 3 files changed, 318 insertions(+)
 create mode 100644 drivers/iio/adc/mt6370-adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 7fe5930..995cbb5 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -736,6 +736,18 @@ config MEDIATEK_MT6360_ADC
  is used in smartphones and tablets and supports a 11 channel
  general purpose ADC.
 
+config MEDIATEK_MT6370_ADC
+   tristate "MediaTek MT6370 ADC driver"
+   depends on MFD_MT6370
+   help
+ Say yes here to enable MediaTek MT6370 ADC support.
+
+ This ADC driver provides 9 channels for system monitoring (charger
+ current, voltage, and temperature).
+
+ This driver can also be built as a module. If so, the module
+ will be called "mt6370-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 1772a54..c6bc35f 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -68,6 +68,7 @@ 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_MT6370_ADC) += mt6370-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/mt6370-adc.c b/drivers/iio/adc/mt6370-adc.c
new file mode 100644
index 000..2a46471
--- /dev/null
+++ b/drivers/iio/adc/mt6370-adc.c
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiaEn Wu 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#define MT6370_REG_CHG_CTRL3   0x113
+#define MT6370_REG_CHG_CTRL7   0x117
+#define MT6370_REG_CHG_ADC 0x121
+#define MT6370_REG_ADC_DATA_H  0x14C
+
+#define MT6370_ADC_START_MASK  BIT(0)
+#define MT6370_ADC_IN_SEL_MASK GENMASK(7, 4)
+#define MT6370_AICR_ICHG_MASK  GENMASK(7, 2)
+
+#define MT6370_AICR_100_mA 0x0
+#define MT6370_AICR_150_mA 0x1
+#define MT6370_AICR_200_mA 0x2
+#define MT6370_AICR_250_mA 0x3
+#define MT6370_AICR_300_mA 0x4
+#define MT6370_AICR_350_mA 0x5
+
+#define MT6370_ICHG_100_mA 0x0
+#define MT6370_ICHG_200_mA 0x1
+#define MT6370_ICHG_300_mA 0x2
+#define MT6370_ICHG_400_mA 0x3
+#define MT6370_ICHG_500_mA 0x4
+#define MT6370_ICHG_600_mA 0x5
+#define MT6370_ICHG_700_mA 0x6
+#define MT6370_ICHG_800_mA 0x7
+
+#define ADC_CONV_TIME_MS   35
+#define ADC_CONV_POLLING_TIME_US   1000
+
+struct mt6370_adc_data {
+   struct device *dev;
+   struct regmap *regmap;
+   /*
+* This mutex lock is for preventing the different ADC channels
+* from being read at the same time.
+*/
+   struct mutex adc_lock;
+};
+
+static int mt6370_adc_read_channel(struct mt6370_adc_data *priv, int chan,
+  unsigned long addr, int *val)
+{
+   unsigned int reg_val;
+   __be16 be_val;
+   int ret;
+
+   mutex_lock(>adc_lock);
+
+   reg_val = MT6370_ADC_START_MASK |
+ FIELD_PREP(MT6370_ADC_IN_SEL_MASK, addr);
+   ret = regmap_write(priv->regmap, MT6370_REG_CHG_ADC, reg_val);
+   if (ret)
+   goto adc_unlock;
+
+   msleep(ADC_CONV_TIME_MS);
+
+   ret = regmap_read_poll_timeout(priv->regmap,
+  MT6370_REG_CHG_ADC, reg_val,
+  !(reg_val & MT6370_ADC_START_MASK),
+  ADC_CONV_POLLING_TIME_US,
+  ADC_CONV_TIME_MS * MILLI * 3);
+   if (ret) {
+   dev_err(priv->dev,