Re: [PATCH v5 10/13] power: supply: mt6370: Add MediaTek MT6370 charger driver

2022-07-16 Thread Sebastian Reichel
Hi,

On Fri, Jul 15, 2022 at 07:26:04PM +0800, 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.
> 
> This adds MediaTek MT6370 Charger driver support. The charger module
> of MT6370 supports High-Accuracy Voltage/Current Regulation,
> Average Input Current Regulation, Battery Temperature Sensing,
> Over-Temperature Protection, DPDM Detection for BC1.2.
> 
> Signed-off-by: ChiaEn Wu 
> ---

Please provide a dump from '/sys/class/power_supply//uevent'
below the fold in the next revision. I would like to check that you
got the units right in the next submission (in this one they are
definetly not correct).

> v5
> - Replace unsigned int type of pwr_rdy with bool in
>   mt6370_chg_set_online()
> - Remove redundant 'else' in mt6370_chg_field_get()
> - Revise 'if-else' in mt6370_chg_field_set()
> - Revise 'if' condition in mt6370_chg_enable_irq()
> - Revise all text 'otg' --> 'OTG'
> - Revise MT6370_MIVR_IBUS_TH_100_MA --> MT6370_MIVR_IBUS_TH_100_mA
> - Revise the description of Kconfig help text
> ---
>  drivers/power/supply/Kconfig  |   14 +
>  drivers/power/supply/Makefile |1 +
>  drivers/power/supply/mt6370-charger.c | 1062 
> +
>  3 files changed, 1077 insertions(+)
>  create mode 100644 drivers/power/supply/mt6370-charger.c
> 
> diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
> index 1aa8323..591deb8 100644
> --- a/drivers/power/supply/Kconfig
> +++ b/drivers/power/supply/Kconfig
> @@ -619,6 +619,20 @@ config CHARGER_MT6360
> Average Input Current Regulation, Battery Temperature Sensing,
> Over-Temperature Protection, DPDM Detection for BC1.2.
>  
> +config CHARGER_MT6370
> + tristate "MediaTek MT6370 Charger Driver"
> + depends on MFD_MT6370
> + depends on REGULATOR
> + select LINEAR_RANGES
> + help
> +   Say Y here to enable MT6370 Charger Part.
> +   The device supports High-Accuracy Voltage/Current Regulation,
> +   Average Input Current Regulation, Battery Temperature Sensing,
> +   Over-Temperature Protection, DPDM Detection for BC1.2.
> +
> +   This driver can also be built as a module. If so, the module
> +   will be called "mt6370-charger".
> +
>  config CHARGER_QCOM_SMBB
>   tristate "Qualcomm Switch-Mode Battery Charger and Boost"
>   depends on MFD_SPMI_PMIC || COMPILE_TEST
> diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
> index 7f02f36..8c95276 100644
> --- a/drivers/power/supply/Makefile
> +++ b/drivers/power/supply/Makefile
> @@ -82,6 +82,7 @@ obj-$(CONFIG_CHARGER_MAX8997)   += max8997_charger.o
>  obj-$(CONFIG_CHARGER_MAX8998)+= max8998_charger.o
>  obj-$(CONFIG_CHARGER_MP2629) += mp2629_charger.o
>  obj-$(CONFIG_CHARGER_MT6360) += mt6360_charger.o
> +obj-$(CONFIG_CHARGER_MT6370) += mt6370-charger.o
>  obj-$(CONFIG_CHARGER_QCOM_SMBB)  += qcom_smbb.o
>  obj-$(CONFIG_CHARGER_BQ2415X)+= bq2415x_charger.o
>  obj-$(CONFIG_CHARGER_BQ24190)+= bq24190_charger.o
> diff --git a/drivers/power/supply/mt6370-charger.c 
> b/drivers/power/supply/mt6370-charger.c
> new file mode 100644
> index 000..76a8c91
> --- /dev/null
> +++ b/drivers/power/supply/mt6370-charger.c
> @@ -0,0 +1,1062 @@
> +// 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 
> +#include 
> +#include 
> +#include 
> +
> +#define MT6370_REG_CHG_CTRL1 0x111
> +#define MT6370_REG_CHG_CTRL2 0x112
> +#define MT6370_REG_CHG_CTRL3 0x113
> +#define MT6370_REG_CHG_CTRL4 0x114
> +#define MT6370_REG_CHG_CTRL5 0x115
> +#define MT6370_REG_CHG_CTRL6 0x116
> +#define MT6370_REG_CHG_CTRL7 0x117
> +#define MT6370_REG_CHG_CTRL8 0x118
> +#define MT6370_REG_CHG_CTRL9 0x119
> +#define MT6370_REG_CHG_CTRL100x11A
> +#define MT6370_REG_DEVICE_TYPE   0x122
> +#define MT6370_REG_USB_STATUS1   0x127
> +#define MT6370_REG_CHG_STAT  0x14A
> +#define MT6370_REG_FLED_EN   0x17E
> +#define MT6370_REG_CHG_STAT1 0X1D0
> +#define MT6370_REG_OVPCTRL_STAT  0x1D8
> +
> +#define MT6370_VOBST_MASKGENMASK(7, 2)
> +#define MT6370_OTG_PIN_EN_MASK   BIT(1)
> +#define MT6370_OPA_MODE_MASK BIT(0)
> +#define MT6370_OTG_OC_MASK   GENMASK(2, 0)
> +
> +#define MT6370_MIVR_IBUS_TH_100_mA   10
> +#define MT6370_ADC_CHAN_IBUS 5
> +#define MT6370_ADC_CHAN_MAX 

Re: [PATCH v5 10/13] power: supply: mt6370: Add MediaTek MT6370 charger driver

2022-07-16 Thread Andy Shevchenko
On Fri, Jul 15, 2022 at 1:29 PM 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.
>
> This adds MediaTek MT6370 Charger driver support. The charger module
> of MT6370 supports High-Accuracy Voltage/Current Regulation,
> Average Input Current Regulation, Battery Temperature Sensing,
> Over-Temperature Protection, DPDM Detection for BC1.2.

...

> +static int mt6370_chg_probe(struct platform_device *pdev)
> +{
> +   int ret;
> +   struct mt6370_priv *priv;
> +
> +   priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
> +   if (!priv)
> +   return -ENOMEM;
> +
> +   priv->dev = &pdev->dev;
> +
> +   priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
> +   if (!priv->regmap)
> +   return dev_err_probe(&pdev->dev, -ENODEV,
> +"Failed to get regmap\n");
> +
> +   ret = mt6370_chg_init_rmap_fields(priv);
> +   if (ret)
> +   return dev_err_probe(&pdev->dev, ret,
> +"Failed to init regmap fields\n");
> +
> +   platform_set_drvdata(pdev, priv);
> +
> +   priv->iio_adcs = devm_iio_channel_get_all(priv->dev);
> +   if (IS_ERR(priv->iio_adcs))
> +   return dev_err_probe(&pdev->dev, PTR_ERR(priv->iio_adcs),
> +"Failed to get iio adc\n");
> +
> +   ret = mt6370_chg_init_otg_regulator(priv);
> +   if (ret)
> +   return dev_err_probe(&pdev->dev, ret,
> +"Failed to init otg regulator\n");
> +
> +   ret = mt6370_chg_init_psy(priv);
> +   if (ret)
> +   return dev_err_probe(&pdev->dev, ret, "Failed to init psy\n");
> +
> +   mutex_init(&priv->attach_lock);
> +   priv->attach = MT6370_ATTACH_STAT_DETACH;
> +
> +   priv->wq = create_singlethread_workqueue(dev_name(priv->dev));
> +   if (IS_ERR(priv->wq))

> +   return dev_err_probe(priv->dev, PTR_ERR(priv->wq),
> +"Failed to create workqueue\n");

You need either wrap mutex to be deallocated by devm or don't use
dev_err_probe() here.

> +   INIT_WORK(&priv->bc12_work, mt6370_chg_bc12_work_func);
> +   INIT_DELAYED_WORK(&priv->mivr_dwork, mt6370_chg_mivr_dwork_func);
> +
> +   ret = mt6370_chg_init_setting(priv);
> +   if (ret) {
> +   dev_err(&pdev->dev, "Failed to init mt6370 charger 
> setting\n");
> +   goto probe_out;
> +   }
> +
> +   ret = mt6370_chg_init_irq(priv);
> +   if (ret)
> +   goto probe_out;
> +
> +   mt6370_chg_pwr_rdy_check(priv);
> +
> +   return 0;
> +
> +probe_out:
> +   cancel_delayed_work_sync(&priv->mivr_dwork);
> +   flush_workqueue(priv->wq);
> +   destroy_workqueue(priv->wq);
> +   mutex_destroy(&priv->attach_lock);
> +
> +   return ret;
> +}
> +
> +static int mt6370_chg_remove(struct platform_device *pdev)
> +{
> +   struct mt6370_priv *priv = platform_get_drvdata(pdev);
> +
> +   cancel_delayed_work_sync(&priv->mivr_dwork);
> +   flush_workqueue(priv->wq);
> +   destroy_workqueue(priv->wq);
> +   mutex_destroy(&priv->attach_lock);
> +
> +   return 0;
> +}


-- 
With Best Regards,
Andy Shevchenko


[PATCH v5 10/13] power: supply: mt6370: Add MediaTek MT6370 charger driver

2022-07-15 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.

This adds MediaTek MT6370 Charger driver support. The charger module
of MT6370 supports High-Accuracy Voltage/Current Regulation,
Average Input Current Regulation, Battery Temperature Sensing,
Over-Temperature Protection, DPDM Detection for BC1.2.

Signed-off-by: ChiaEn Wu 
---

v5
- Replace unsigned int type of pwr_rdy with bool in
  mt6370_chg_set_online()
- Remove redundant 'else' in mt6370_chg_field_get()
- Revise 'if-else' in mt6370_chg_field_set()
- Revise 'if' condition in mt6370_chg_enable_irq()
- Revise all text 'otg' --> 'OTG'
- Revise MT6370_MIVR_IBUS_TH_100_MA --> MT6370_MIVR_IBUS_TH_100_mA
- Revise the description of Kconfig help text
---
 drivers/power/supply/Kconfig  |   14 +
 drivers/power/supply/Makefile |1 +
 drivers/power/supply/mt6370-charger.c | 1062 +
 3 files changed, 1077 insertions(+)
 create mode 100644 drivers/power/supply/mt6370-charger.c

diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index 1aa8323..591deb8 100644
--- a/drivers/power/supply/Kconfig
+++ b/drivers/power/supply/Kconfig
@@ -619,6 +619,20 @@ config CHARGER_MT6360
  Average Input Current Regulation, Battery Temperature Sensing,
  Over-Temperature Protection, DPDM Detection for BC1.2.
 
+config CHARGER_MT6370
+   tristate "MediaTek MT6370 Charger Driver"
+   depends on MFD_MT6370
+   depends on REGULATOR
+   select LINEAR_RANGES
+   help
+ Say Y here to enable MT6370 Charger Part.
+ The device supports High-Accuracy Voltage/Current Regulation,
+ Average Input Current Regulation, Battery Temperature Sensing,
+ Over-Temperature Protection, DPDM Detection for BC1.2.
+
+ This driver can also be built as a module. If so, the module
+ will be called "mt6370-charger".
+
 config CHARGER_QCOM_SMBB
tristate "Qualcomm Switch-Mode Battery Charger and Boost"
depends on MFD_SPMI_PMIC || COMPILE_TEST
diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
index 7f02f36..8c95276 100644
--- a/drivers/power/supply/Makefile
+++ b/drivers/power/supply/Makefile
@@ -82,6 +82,7 @@ obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o
 obj-$(CONFIG_CHARGER_MAX8998)  += max8998_charger.o
 obj-$(CONFIG_CHARGER_MP2629)   += mp2629_charger.o
 obj-$(CONFIG_CHARGER_MT6360)   += mt6360_charger.o
+obj-$(CONFIG_CHARGER_MT6370)   += mt6370-charger.o
 obj-$(CONFIG_CHARGER_QCOM_SMBB)+= qcom_smbb.o
 obj-$(CONFIG_CHARGER_BQ2415X)  += bq2415x_charger.o
 obj-$(CONFIG_CHARGER_BQ24190)  += bq24190_charger.o
diff --git a/drivers/power/supply/mt6370-charger.c 
b/drivers/power/supply/mt6370-charger.c
new file mode 100644
index 000..76a8c91
--- /dev/null
+++ b/drivers/power/supply/mt6370-charger.c
@@ -0,0 +1,1062 @@
+// 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 
+#include 
+#include 
+#include 
+
+#define MT6370_REG_CHG_CTRL1   0x111
+#define MT6370_REG_CHG_CTRL2   0x112
+#define MT6370_REG_CHG_CTRL3   0x113
+#define MT6370_REG_CHG_CTRL4   0x114
+#define MT6370_REG_CHG_CTRL5   0x115
+#define MT6370_REG_CHG_CTRL6   0x116
+#define MT6370_REG_CHG_CTRL7   0x117
+#define MT6370_REG_CHG_CTRL8   0x118
+#define MT6370_REG_CHG_CTRL9   0x119
+#define MT6370_REG_CHG_CTRL10  0x11A
+#define MT6370_REG_DEVICE_TYPE 0x122
+#define MT6370_REG_USB_STATUS1 0x127
+#define MT6370_REG_CHG_STAT0x14A
+#define MT6370_REG_FLED_EN 0x17E
+#define MT6370_REG_CHG_STAT1   0X1D0
+#define MT6370_REG_OVPCTRL_STAT0x1D8
+
+#define MT6370_VOBST_MASK  GENMASK(7, 2)
+#define MT6370_OTG_PIN_EN_MASK BIT(1)
+#define MT6370_OPA_MODE_MASK   BIT(0)
+#define MT6370_OTG_OC_MASK GENMASK(2, 0)
+
+#define MT6370_MIVR_IBUS_TH_100_mA 10
+#define MT6370_ADC_CHAN_IBUS   5
+#define MT6370_ADC_CHAN_MAX9
+
+enum mt6370_chg_reg_field {
+   /* MT6370_REG_CHG_CTRL2 */
+   F_IINLMTSEL, F_CFO_EN, F_CHG_EN,
+   /* MT6370_REG_CHG_CTRL3 */
+   F_IAICR, F_AICR_EN, F_ILIM_EN,
+   /* MT6370_REG_CHG_CTRL4 */
+   F_VOREG,
+   /* MT6370_REG_CHG_CTRL6 */
+   F_VMIVR,
+   /* MT6370_REG_CHG_CTRL7 */
+   F_ICHG,
+   /* MT6370_REG_CHG_CTRL8 */
+   F_IPREC,
+   /* MT6370_REG_CHG_CTRL9 */
+   F_IEOC,
+   /* MT6370_REG_DEVICE_TYPE */
+   F_USBCHGEN,
+