Re: [PATCH v9 09/10] leds: flash: mt6370: Add MediaTek MT6370 flashlight support

2022-09-20 Thread ChiaEn Wu
On Sun, Sep 18, 2022 at 3:22 AM Han Jingoo  wrote:
>
> On Mon, Aug 29, 2022 ChiaEn Wu  wrote:

...

> > +#define MT6370_ITORCH_MIN_uA   25000
> > +#define MT6370_ITORCH_STEP_uA  12500
> > +#define MT6370_ITORCH_MAX_uA   40
> > +#define MT6370_ITORCH_DOUBLE_MAX_uA80
> > +#define MT6370_ISTRB_MIN_uA5
> > +#define MT6370_ISTRB_STEP_uA   12500
> > +#define MT6370_ISTRB_MAX_uA150
> > +#define MT6370_ISTRB_DOUBLE_MAX_uA 300
>
> Use upper letters as below:
>
> #define MT6370_ITORCH_MIN_UA   25000
> #define MT6370_ITORCH_STEP_UA  12500
> #define MT6370_ITORCH_MAX_UA   40
> #define MT6370_ITORCH_DOUBLE_MAX_UA80
> #define MT6370_ISTRB_MIN_UA5
> #define MT6370_ISTRB_STEP_UA   12500
> #define MT6370_ISTRB_MAX_UA150
> #define MT6370_ISTRB_DOUBLE_MAX_UA 300
>
>
> > +#define MT6370_STRBTO_MIN_US   64000
> > +#define MT6370_STRBTO_STEP_US  32000
> > +#define MT6370_STRBTO_MAX_US   2432000
> > +

Hi Jingoo,

This coding style is in accordance with Andy's opinion in this mail:
https://lore.kernel.org/linux-arm-kernel/CAHp75Vciq4M4kVrabNV9vTLLcd1vR=bme8jledaf9mkrtpc...@mail.gmail.com/

And I will revise other parts in v12.
Thanks for your review!

--
Best Regards,
ChiaEn Wu


[PATCH v9 10/10] video: backlight: mt6370: Add MediaTek MT6370 support

2022-08-29 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 support for the MediaTek MT6370 backlight driver.
It controls 4 channels of 8 series WLEDs in
2048 (only for MT6370/MT6371) / 16384 (only for MT6372)
current steps (30 mA) in exponential or linear mapping curves.

Reviewed-by: Daniel Thompson 
Signed-off-by: ChiaEn Wu 
---

v9
- Revise the format of the comments.
---
 drivers/video/backlight/Kconfig|  13 ++
 drivers/video/backlight/Makefile   |   1 +
 drivers/video/backlight/mt6370-backlight.c | 351 +
 3 files changed, 365 insertions(+)
 create mode 100644 drivers/video/backlight/mt6370-backlight.c

diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig
index a003e02..936ba1e 100644
--- a/drivers/video/backlight/Kconfig
+++ b/drivers/video/backlight/Kconfig
@@ -268,6 +268,19 @@ config BACKLIGHT_MAX8925
  If you have a LCD backlight connected to the WLED output of MAX8925
  WLED output, say Y here to enable this driver.
 
+config BACKLIGHT_MT6370
+   tristate "MediaTek MT6370 Backlight Driver"
+   depends on MFD_MT6370
+   help
+ This enables support for Mediatek MT6370 Backlight driver.
+ It's commonly used to drive the display WLED. There are 4 channels
+ inside, and each channel supports up to 30mA of current capability
+ with 2048 current steps (only for MT6370/MT6371) or 16384 current
+ steps (only for MT6372) in exponential or linear mapping curves.
+
+ This driver can also be built as a module. If so, the module
+ will be called "mt6370-backlight".
+
 config BACKLIGHT_APPLE
tristate "Apple Backlight Driver"
depends on X86 && ACPI
diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile
index cae2c83..e815f3f 100644
--- a/drivers/video/backlight/Makefile
+++ b/drivers/video/backlight/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_BACKLIGHT_LP855X)+= lp855x_bl.o
 obj-$(CONFIG_BACKLIGHT_LP8788) += lp8788_bl.o
 obj-$(CONFIG_BACKLIGHT_LV5207LP)   += lv5207lp.o
 obj-$(CONFIG_BACKLIGHT_MAX8925)+= max8925_bl.o
+obj-$(CONFIG_BACKLIGHT_MT6370) += mt6370-backlight.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)  += omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PANDORA)+= pandora_bl.o
 obj-$(CONFIG_BACKLIGHT_PCF50633)   += pcf50633-backlight.o
diff --git a/drivers/video/backlight/mt6370-backlight.c 
b/drivers/video/backlight/mt6370-backlight.c
new file mode 100644
index 000..623d4f2
--- /dev/null
+++ b/drivers/video/backlight/mt6370-backlight.c
@@ -0,0 +1,351 @@
+// 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 
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_BL_EN   0x1A0
+#define MT6370_REG_BL_BSTCTRL  0x1A1
+#define MT6370_REG_BL_PWM  0x1A2
+#define MT6370_REG_BL_DIM2 0x1A4
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+#define MT6370_BL_EXT_EN_MASK  BIT(7)
+#define MT6370_BL_EN_MASK  BIT(6)
+#define MT6370_BL_CODE_MASKBIT(0)
+#define MT6370_BL_CH_MASK  GENMASK(5, 2)
+#define MT6370_BL_CH_SHIFT 2
+#define MT6370_BL_DIM2_COMMON_MASK GENMASK(2, 0)
+#define MT6370_BL_DIM2_COMMON_SHIFT3
+#define MT6370_BL_DIM2_6372_MASK   GENMASK(5, 0)
+#define MT6370_BL_DIM2_6372_SHIFT  6
+#define MT6370_BL_PWM_EN_MASK  BIT(7)
+#define MT6370_BL_PWM_HYS_EN_MASK  BIT(2)
+#define MT6370_BL_PWM_HYS_SEL_MASK GENMASK(1, 0)
+#define MT6370_BL_OVP_EN_MASK  BIT(7)
+#define MT6370_BL_OVP_SEL_MASK GENMASK(6, 5)
+#define MT6370_BL_OVP_SEL_SHIFT5
+#define MT6370_BL_OC_EN_MASK   BIT(3)
+#define MT6370_BL_OC_SEL_MASK  GENMASK(2, 1)
+#define MT6370_BL_OC_SEL_SHIFT 1
+
+#define MT6370_BL_PWM_HYS_TH_MIN_STEP  1
+#define MT6370_BL_PWM_HYS_TH_MAX_STEP  64
+#define MT6370_BL_OVP_MIN_UV   1700
+#define MT6370_BL_OVP_MAX_UV   2900
+#define MT6370_BL_OVP_STEP_UV  400
+#define MT6370_BL_OCP_MIN_UA   90
+#define MT6370_BL_OCP_MAX_UA   180
+#define MT6370_BL_OCP_STEP_UA  30
+#define MT6370_BL_MAX_COMMON_BRIGHTNESS2048
+#define MT6370_BL_MAX_6372_BRIGHTNESS  16384
+#define MT6370_BL_MAX_CH   15
+
+enum {
+   MT6370_VID_COMMON = 1,
+   MT6370_VID_6372,
+};
+
+struct mt6370_priv {
+   u8 dim

[PATCH v9 09/10] leds: flash: mt6370: Add MediaTek MT6370 flashlight support

2022-08-29 Thread ChiaEn Wu
From: Alice Chen 

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 support for the MT6370 Flash LED driver. Flash LED in MT6370
has 2 channels and support torch/strobe mode.

Signed-off-by: Alice Chen 
Signed-off-by: ChiaEn Wu 
---

v9
- Revise the format of the comments.
---
 drivers/leds/flash/Kconfig |  14 +
 drivers/leds/flash/Makefile|   1 +
 drivers/leds/flash/leds-mt6370-flash.c | 632 +
 3 files changed, 647 insertions(+)
 create mode 100644 drivers/leds/flash/leds-mt6370-flash.c

diff --git a/drivers/leds/flash/Kconfig b/drivers/leds/flash/Kconfig
index d3eb689..405bd16 100644
--- a/drivers/leds/flash/Kconfig
+++ b/drivers/leds/flash/Kconfig
@@ -61,6 +61,20 @@ config LEDS_MT6360
  Independent current sources supply for each flash LED support torch
  and strobe mode.
 
+config LEDS_MT6370_FLASH
+   tristate "Flash LED Support for MediaTek MT6370 PMIC"
+   depends on LEDS_CLASS && OF
+   depends on LEDS_CLASS_FLASH || !LEDS_CLASS_FLASH
+   depends on V4L2_FLASH_LED_CLASS || !V4L2_FLASH_LED_CLASS
+   depends on MFD_MT6370
+   help
+ Support 2 channels and torch/strobe mode.
+ Say Y here to enable support for
+ MT6370_FLASH_LED device.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-flash".
+
 config LEDS_RT4505
tristate "LED support for RT4505 flashlight controller"
depends on I2C && OF
diff --git a/drivers/leds/flash/Makefile b/drivers/leds/flash/Makefile
index 0acbddc..0c1f3c5 100644
--- a/drivers/leds/flash/Makefile
+++ b/drivers/leds/flash/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_LEDS_MAX77693) += leds-max77693.o
 obj-$(CONFIG_LEDS_RT4505)  += leds-rt4505.o
 obj-$(CONFIG_LEDS_RT8515)  += leds-rt8515.o
 obj-$(CONFIG_LEDS_SGM3140) += leds-sgm3140.o
+obj-$(CONFIG_LEDS_MT6370_FLASH)+= leds-mt6370-flash.o
diff --git a/drivers/leds/flash/leds-mt6370-flash.c 
b/drivers/leds/flash/leds-mt6370-flash.c
new file mode 100644
index 000..a8ec7e1
--- /dev/null
+++ b/drivers/leds/flash/leds-mt6370-flash.c
@@ -0,0 +1,632 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_FLASH1,
+   MT6370_LED_FLASH2,
+   MT6370_MAX_LEDS
+};
+
+/* Virtual definition for multicolor */
+
+#define MT6370_REG_FLEDEN  0x17E
+#define MT6370_REG_STRBTO  0x173
+#define MT6370_REG_CHGSTAT20x1D1
+#define MT6370_REG_FLEDSTAT1   0x1D9
+#define MT6370_REG_FLEDISTRB(_id)  (0x174 + 4 * (_id))
+#define MT6370_REG_FLEDITOR(_id)   (0x175 + 4 * (_id))
+#define MT6370_ITORCH_MASK GENMASK(4, 0)
+#define MT6370_ISTROBE_MASKGENMASK(6, 0)
+#define MT6370_STRBTO_MASK GENMASK(6, 0)
+#define MT6370_TORCHEN_MASKBIT(3)
+#define MT6370_STROBEN_MASKBIT(2)
+#define MT6370_FLCSEN_MASK(_id)BIT(MT6370_LED_FLASH2 - (_id))
+#define MT6370_FLCSEN_MASK_ALL GENMASK(1, 0)
+#define MT6370_FLEDCHGVINOVP_MASK  BIT(3)
+#define MT6370_FLED1STRBTO_MASKBIT(11)
+#define MT6370_FLED2STRBTO_MASKBIT(10)
+#define MT6370_FLED1STRB_MASK  BIT(9)
+#define MT6370_FLED2STRB_MASK  BIT(8)
+#define MT6370_FLED1SHORT_MASK BIT(7)
+#define MT6370_FLED2SHORT_MASK BIT(6)
+#define MT6370_FLEDLVF_MASKBIT(3)
+
+#define MT6370_LED_JOINT   2
+#define MT6370_RANGE_FLED_REG  4
+#define MT6370_ITORCH_MIN_uA   25000
+#define MT6370_ITORCH_STEP_uA  12500
+#define MT6370_ITORCH_MAX_uA   40
+#define MT6370_ITORCH_DOUBLE_MAX_uA80
+#define MT6370_ISTRB_MIN_uA5
+#define MT6370_ISTRB_STEP_uA   12500
+#define MT6370_ISTRB_MAX_uA150
+#define MT6370_ISTRB_DOUBLE_MAX_uA 300
+#define MT6370_STRBTO_MIN_US   64000
+#define MT6370_STRBTO_STEP_US  32000
+#define MT6370_STRBTO_MAX_US   2432000
+
+#define STATE_OFF  0
+#define STATE_KEEP 1
+#define STATE_ON   2
+
+#define to_mt6370_led(ptr, member) container_of(ptr, struct mt6370_led, member)
+
+struct mt6370_led {
+   struct led_classdev_flash flash;
+   struct v4l2_flash *v4l2_flash;
+   struct mt6370_priv *priv;
+   u32 led_no;
+   u32 default_state;
+};

[PATCH v9 08/10] leds: rgb: mt6370: Add MediaTek MT6370 current sink type LED Indicator support

2022-08-29 Thread ChiaEn Wu
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 support for the MediaTek MT6370 Current Sink Type LED Indicator
driver. It can control four channels current-sink RGB LEDs with 3 modes:
constant current, PWM, and breath mode.

Reviewed-by: AngeloGioacchino Del Regno 

Co-developed-by: Alice Chen 
Signed-off-by: Alice Chen 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 drivers/leds/rgb/Kconfig   |   13 +
 drivers/leds/rgb/Makefile  |1 +
 drivers/leds/rgb/leds-mt6370-rgb.c | 1010 
 3 files changed, 1024 insertions(+)
 create mode 100644 drivers/leds/rgb/leds-mt6370-rgb.c

diff --git a/drivers/leds/rgb/Kconfig b/drivers/leds/rgb/Kconfig
index 204cf47..7d86bb2 100644
--- a/drivers/leds/rgb/Kconfig
+++ b/drivers/leds/rgb/Kconfig
@@ -26,4 +26,17 @@ config LEDS_QCOM_LPG
 
  If compiled as a module, the module will be named leds-qcom-lpg.
 
+config LEDS_MT6370_RGB
+   tristate "LED Support for MediaTek MT6370 PMIC"
+   depends on MFD_MT6370
+   select LINEAR_RANGE
+   help
+ Say Y here to enable support for MT6370_RGB LED device.
+ In MT6370, there are four channel current-sink LED drivers that
+ support hardware pattern for constant current, PWM, and breath mode.
+ Isink4 channel can also be used as a CHG_VIN power good indicator.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-rgb".
+
 endif # LEDS_CLASS_MULTICOLOR
diff --git a/drivers/leds/rgb/Makefile b/drivers/leds/rgb/Makefile
index 0675bc0..8c01daf 100644
--- a/drivers/leds/rgb/Makefile
+++ b/drivers/leds/rgb/Makefile
@@ -2,3 +2,4 @@
 
 obj-$(CONFIG_LEDS_PWM_MULTICOLOR)  += leds-pwm-multicolor.o
 obj-$(CONFIG_LEDS_QCOM_LPG)+= leds-qcom-lpg.o
+obj-$(CONFIG_LEDS_MT6370_RGB)  += leds-mt6370-rgb.o
diff --git a/drivers/leds/rgb/leds-mt6370-rgb.c 
b/drivers/leds/rgb/leds-mt6370-rgb.c
new file mode 100644
index 000..1f9d648
--- /dev/null
+++ b/drivers/leds/rgb/leds-mt6370-rgb.c
@@ -0,0 +1,1010 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Authors:
+ *   ChiYuan Huang 
+ *   Alice Chen 
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_ISNK1 = 0,
+   MT6370_LED_ISNK2,
+   MT6370_LED_ISNK3,
+   MT6370_LED_ISNK4,
+   MT6370_MAX_LEDS
+};
+
+enum mt6370_led_mode {
+   MT6370_LED_PWM_MODE = 0,
+   MT6370_LED_BREATH_MODE,
+   MT6370_LED_REG_MODE,
+   MT6370_LED_MAX_MODE
+};
+
+enum mt6370_led_field {
+   F_RGB_EN = 0,
+   F_CHGIND_EN,
+   F_LED1_CURR,
+   F_LED2_CURR,
+   F_LED3_CURR,
+   F_LED4_CURR,
+   F_LED1_MODE,
+   F_LED2_MODE,
+   F_LED3_MODE,
+   F_LED4_MODE,
+   F_LED1_DUTY,
+   F_LED2_DUTY,
+   F_LED3_DUTY,
+   F_LED4_DUTY,
+   F_LED1_FREQ,
+   F_LED2_FREQ,
+   F_LED3_FREQ,
+   F_LED4_FREQ,
+   F_MAX_FIELDS
+};
+
+enum mt6370_led_ranges {
+   R_LED123_CURR = 0,
+   R_LED4_CURR,
+   R_LED_TRFON,
+   R_LED_TOFF,
+   R_MAX_RANGES
+};
+
+enum mt6370_pattern {
+   P_LED_TR1 = 0,
+   P_LED_TR2,
+   P_LED_TF1,
+   P_LED_TF2,
+   P_LED_TON,
+   P_LED_TOFF,
+   P_MAX_PATTERNS
+};
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_RGB1_DIM0x182
+#define MT6370_REG_RGB2_DIM0x183
+#define MT6370_REG_RGB3_DIM0x184
+#define MT6370_REG_RGB_EN  0x185
+#define MT6370_REG_RGB1_ISNK   0x186
+#define MT6370_REG_RGB2_ISNK   0x187
+#define MT6370_REG_RGB3_ISNK   0x188
+#define MT6370_REG_RGB1_TR 0x189
+#define MT6370_REG_RGB_CHRIND_DIM  0x192
+#define MT6370_REG_RGB_CHRIND_CTRL 0x193
+#define MT6370_REG_RGB_CHRIND_TR   0x194
+
+#define MT6372_REG_RGB_EN  0x182
+#define MT6372_REG_RGB1_ISNK   0x183
+#define MT6372_REG_RGB2_ISNK   0x184
+#define MT6372_REG_RGB3_ISNK   0x185
+#define MT6372_REG_RGB4_ISNK   0x186
+#define MT6372_REG_RGB1_DIM0x187
+#define MT6372_REG_RGB2_DIM0x188
+#define MT6372_REG_RGB3_DIM0x189
+#define MT6372_REG_RGB4_DIM0x18A
+#define MT6372_REG_RGB12_FREQ  0x18B
+#define MT6372_REG_RGB34_FREQ

[PATCH v9 07/10] power: supply: mt6370: Add MediaTek MT6370 charger driver

2022-08-29 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 support for the MediaTek MT6370 Charger driver. 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.

Reviewed-by: Andy Shevchenko 
Signed-off-by: ChiaEn Wu 
---

v9
- Fix wrong 'F_IINLMTSEL' setting in 'mt6370_chg_init_setting()'
- Revise the usage of LINEAR_RANGE_IDX()
---
 drivers/power/supply/Kconfig  |  14 +
 drivers/power/supply/Makefile |   1 +
 drivers/power/supply/mt6370-charger.c | 957 ++
 3 files changed, 972 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..11c48ec
--- /dev/null
+++ b/drivers/power/supply/mt6370-charger.c
@@ -0,0 +1,957 @@
+// 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,
+   /* MT6370_REG_USB_STATUS1 */
+   F_USB_STAT, F_CHGDET,
+   /* MT6370_REG_CHG_STAT */
+   F_CHG_STAT, F_BOOST_STAT, F_VBAT_LVL,
+   /* MT6370_REG_FLED_EN */
+   F_FL_STROB

[PATCH v9 06/10] iio: adc: mt6370: Add MediaTek MT6370 support

2022-08-29 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 support for the MT6370 ADC driver for system monitoring, including
charger current, voltage, and temperature.

Reviewed-by: AngeloGioacchino Del Regno 

Reviewed-by: Andy Shevchenko 
Acked-by: Jonathan Cameron 
Signed-off-by: ChiaEn Wu 
---
 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, "Failed to read ADC register (%d)\n", ret);
+   goto adc_unlock;
+   }
+
+

[PATCH v9 05/10] lib: add linear range index macro

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

Add linear_range_idx macro for declaring the linear_range struct simply.

Signed-off-by: ChiaEn Wu 
---

v9
- Revise LINEAR_RANGE() and LINEAR_RANGE_IDX()
---
 include/linux/linear_range.h | 11 +++
 1 file changed, 11 insertions(+)

diff --git a/include/linux/linear_range.h b/include/linux/linear_range.h
index fd3d0b3..2e4f4c3 100644
--- a/include/linux/linear_range.h
+++ b/include/linux/linear_range.h
@@ -26,6 +26,17 @@ struct linear_range {
unsigned int step;
 };
 
+#define LINEAR_RANGE(_min, _min_sel, _max_sel, _step)  \
+   {   \
+   .min = _min,\
+   .min_sel = _min_sel,\
+   .max_sel = _max_sel,\
+   .step = _step,  \
+   }
+
+#define LINEAR_RANGE_IDX(_idx, _min, _min_sel, _max_sel, _step)\
+   [_idx] = LINEAR_RANGE(_min, _min_sel, _max_sel, _step)
+
 unsigned int linear_range_values_in_range(const struct linear_range *r);
 unsigned int linear_range_values_in_range_array(const struct linear_range *r,
int ranges);
-- 
2.7.4



[PATCH v9 04/10] dt-bindings: backlight: Add MediaTek MT6370 backlight

2022-08-29 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MT6370 backlight binding documentation.

Reviewed-by: Rob Herring 
Reviewed-by: Daniel Thompson 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 .../leds/backlight/mediatek,mt6370-backlight.yaml  | 121 +
 1 file changed, 121 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
new file mode 100644
index 000..5533b65
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
@@ -0,0 +1,121 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: 
http://devicetree.org/schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Backlight
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  The MT6370 Backlight WLED driver supports up to a 29V output voltage for
+  4 channels of 8 series WLEDs. Each channel supports up to 30mA of current
+  capability with 2048 current steps (11 bits, only for MT6370/MT6371) or
+  16384 current steps (14 bits, only for MT6372) in exponential or linear
+  mapping curves.
+
+allOf:
+  - $ref: common.yaml#
+
+properties:
+  compatible:
+enum:
+  - mediatek,mt6370-backlight
+  - mediatek,mt6372-backlight
+
+  default-brightness:
+minimum: 0
+
+  max-brightness:
+minimum: 0
+
+  enable-gpios:
+description: External backlight 'enable' pin
+maxItems: 1
+
+  mediatek,bled-pwm-enable:
+description: |
+  Enable external PWM input for backlight dimming
+type: boolean
+
+  mediatek,bled-pwm-hys-enable:
+description: |
+  Enable the backlight input-hysteresis for PWM mode
+type: boolean
+
+  mediatek,bled-pwm-hys-input-th-steps:
+$ref: /schemas/types.yaml#/definitions/uint8
+enum: [1, 4, 16, 64]
+description: |
+  The selection of the upper and lower bounds threshold of backlight
+  PWM resolution. If we choose selection 64, the variation of PWM
+  resolution needs more than 64 steps.
+
+  mediatek,bled-ovp-shutdown:
+description: |
+  Enable the backlight shutdown when OVP level triggered
+type: boolean
+
+  mediatek,bled-ovp-microvolt:
+enum: [1700, 2100, 2500, 2900]
+description: |
+  Backlight OVP level selection.
+
+  mediatek,bled-ocp-shutdown:
+description: |
+  Enable the backlight shutdown when OCP level triggerred.
+type: boolean
+
+  mediatek,bled-ocp-microamp:
+enum: [90, 120, 150, 180]
+description: |
+  Backlight OC level selection.
+
+  mediatek,bled-exponential-mode-enable:
+description: |
+  Enable the exponential mode of backlight brightness. If this property
+  is not enabled, the default is to use linear mode.
+type: boolean
+
+  mediatek,bled-channel-use:
+$ref: /schemas/types.yaml#/definitions/uint8
+description: |
+  Backlight LED channel to be used.
+  Each bit mapping to:
+- 0: CH4
+- 1: CH3
+- 2: CH2
+- 3: CH1
+minimum: 1
+maximum: 15
+
+if:
+  properties:
+compatible:
+  contains:
+const: mediatek,mt6372-backlight
+
+then:
+  properties:
+default-brightness:
+  maximum: 16384
+
+max-brightness:
+  maximum: 16384
+
+else:
+  properties:
+default-brightness:
+  maximum: 2048
+
+max-brightness:
+  maximum: 2048
+
+required:
+  - compatible
+  - mediatek,bled-channel-use
+
+additionalProperties: false
-- 
2.7.4



[PATCH v9 03/10] dt-bindings: leds: Add MediaTek MT6370 flashlight

2022-08-29 Thread ChiaEn Wu
From: Alice Chen 

Add MediaTek MT6370 flashlight binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: Alice Chen 
Signed-off-by: ChiaEn Wu 
---
 .../bindings/leds/mediatek,mt6370-flashlight.yaml  | 41 ++
 1 file changed, 41 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
new file mode 100644
index 000..e9d02ed
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-flashlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Flash LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 flash LED driver include 2-channel flash LED support Torch/Strobe 
Mode.
+
+properties:
+  compatible:
+const: mediatek,mt6370-flashlight
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-1]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1]
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v9 02/10] dt-bindings: leds: mt6370: Add MediaTek MT6370 current sink type LED indicator

2022-08-29 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 current sink type LED indicator binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 .../bindings/leds/mediatek,mt6370-indicator.yaml   | 81 ++
 1 file changed, 81 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
new file mode 100644
index 000..204b103
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-indicator.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 LED driver include 4-channel RGB LED support Register/PWM/Breath 
Mode
+
+allOf:
+  - $ref: leds-class-multicolor.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-indicator
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^multi-led@[0-3]$":
+type: object
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-2]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2]
+
+required:
+  - reg
+  - color
+
+required:
+  - reg
+  - color
+  - "#address-cells"
+  - "#size-cells"
+
+  "^led@[0-3]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+required:
+  - reg
+  - color
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v9 01/10] dt-bindings: power: supply: Add MediaTek MT6370 Charger

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

Add MediaTek MT6370 Charger binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiaEn Wu 
---
 .../power/supply/mediatek,mt6370-charger.yaml  | 88 ++
 1 file changed, 88 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml

diff --git 
a/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
new file mode 100644
index 000..bd09a0a
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/mediatek,mt6370-charger.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Battery Charger
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Provides Battery Charger, Boost for OTG devices and BC1.2 detection.
+
+properties:
+  compatible:
+const: mediatek,mt6370-charger
+
+  interrupts:
+description: |
+  Specify what irqs are needed to be handled by MT6370 Charger driver. IRQ
+  "MT6370_IRQ_CHG_MIVR", "MT6370_IRQ_ATTACH" and "MT6370_IRQ_OVPCTRL_UVP_D"
+  are required.
+items:
+  - description: BC1.2 done irq
+  - description: usb plug in irq
+  - description: mivr irq
+
+  interrupt-names:
+items:
+  - const: attach_i
+  - const: uvp_d_evt
+  - const: mivr
+
+  io-channels:
+description: |
+  Use ADC channel to read VBUS, IBUS, IBAT, etc., info.
+minItems: 1
+items:
+  - description: |
+  VBUS voltage with lower accuracy (+-75mV) but higher measure
+  range (1~22V)
+  - description: |
+  VBUS voltage with higher accuracy (+-30mV) but lower measure
+  range (1~9.76V)
+  - description: the main system input voltage
+  - description: battery voltage
+  - description: battery temperature-sense input voltage
+  - description: IBUS current (required)
+  - description: battery current
+  - description: |
+  regulated output voltage to supply for the PWM low-side gate driver
+  and the bootstrap capacitor
+  - description: IC junction temperature
+
+  io-channel-names:
+minItems: 1
+items:
+  - const: vbusdiv5
+  - const: vbusdiv2
+  - const: vsys
+  - const: vbat
+  - const: ts_bat
+  - const: ibus
+  - const: ibat
+  - const: chg_vddp
+  - const: temp_jc
+
+  usb-otg-vbus-regulator:
+type: object
+description: OTG boost regulator.
+unevaluatedProperties: false
+$ref: /schemas/regulator/regulator.yaml#
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - interrupts
+  - interrupt-names
+  - io-channels
+
+additionalProperties: false
+
+...
-- 
2.7.4



[PATCH v9 00/10] Add MediaTek MT6370 PMIC support

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

This patch series add MediaTek MT6370 PMIC support and add a index macro
to . The 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.

First, in this series of patches,
'dt-binding: mfd', 'mfd driver' has been applied by Lee in the v7.
https://lore.kernel.org/all/yvjdpq0mwnpqz...@google.com/
https://lore.kernel.org/all/yvjdxepc2cb58...@google.com/

'tcpci driver' has been applied by Greg in the v8.
https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git/commit/?h=usb-next=c2a8ea5997fdfeb43eda259d5533234c3cae05d7

Second, the LED RGB driver is based on Andy's patch which moves
led_init_default_state_get() to the global header.
https://lore.kernel.org/all/20220805154907.32263-3-andriy.shevche...@linux.intel.com/

In addition, we added a macro to the  for declaring the
linear_range struct simply (see patch v9-0005) and made some changes for
MT6370 drivers (see v9 section of the change log below).

Thank you,
ChiaEn Wu
---
Changes in v9:
- In Patch 05/10:
- Revise LINEAR_RANGE() and LINEAR_RANGE_IDX()

- In Patch 07/10:
- Fix wrong 'F_IINLMTSEL' setting in 'mt6370_chg_init_setting()'
- Revise the usage of LINEAR_RANGE_IDX()

- In Patch 09/10:
- Revise the format of the comments.

- In Patch 10/10:
- Revise the format of the comments.


Changes in v8:
- In Patch 06/12:
- Add linear_range_idx macro for declaring the linear_range
  struct simply.

- In Patch 09/12:
- Remove MT6370_CHG_LINEAR_RANGE() macro, and change to use
  LINEAR_RANGE_IDX() macro

- In Patch 10/12:
- Add include header file 
- Remove useless 'enum mt6370_state' because of using 'enum 
led_default_state'
- Revise the calculation of 'val' in 'mt6370_gen_breath_pattern()'
- Replace the medthod of getting default-state by using
  led_init_default_state_get()

- In Patch 11/12:
- Add missing 'dependency config' in Kconfig
- Revise all multi-line comments

- In Patch 12/12:
- Add missing header file 
- Remove useledd header file 
- Revise the comment of 'mt6370_check_vendor_info()'


Changes in v7:
- In Patch 05/13:
- Add the second compatible string for 'mt6372'
- Add 'mediatek,bled-exponential-mode-enable' property for enable the
  exponential mode of backlight brightness
- Add validation for the maximum value of 'default-brightness' and
  'max-brightness'

- In Patch 07/13:
- Move '#define MT6370_REG_MAXADDR' to the next line of
  '#define MT6370_REG_CHG_MASK1'
- Rename 'MT6370_REG_ADDRLEN' to 'MT6370_MAX_ADDRLEN'

- In Patch 08/13:
- Revise 'devm_add_action_or_reset(dev, ...)' to one line
- Revise 'return regmap_update_bits(...)' with using positive
  conditional

- In Patch 09/13:
- 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

- In Patch 10/13:
- Revise the method to enable/disable irq
- Revise all 'if (ret < 0)' to 'if (ret)' after using
  mt6370_chg_field_set/get()
- Revise all 'OTG' text again

- In Patch 11/13:
- Add the comment for the union of 'struct mt6370_led'
- Revise the wrong description of 'Authors'
- Revise some typos (e.g. led --> LED)
- Revise 'if (!fwnode_property_read_string())' to
  'ret = fwnode_property_read_string()'
- Replace 'memcpy(...)' with 'put_unaligned_be24()' in
  mt6370_gen_breath_pattern()
- Replace all 'LED_OFF' with 0
- Remove the redundant assignment in mt6370_mc_pattern_clear()

- In Patch 12/13:
- Fix the indentation.
- For the well defined macro, the parenthesis is needed for input 
parameters.
- Replace some dev_warn to dev_info in 'init_flash_properties'.
- Add sentinel comment for the terminator entry of of_device_id.
- Use priv->fled_torch_used directly.
- Delete 0 in {}.
- Use _uA instead of _UA in definition.
- Refine the description.
- Use usleep_range instead of udelay.
- Rename config to LEDS_MT6370_FLASH.
- Add missing ">" in copyright.
- Change the Kconfig order

- In Patch 13/13:
- Add support 'exponential mode' property parsing
- Add 'return dev_err_probe()' after 'if (IS_ERR(priv->enable_gpio))'
- Add 'mt6372' compatible string
- Revise Kconfig help text
- Revise update()/get() for supporting 16384 steps (MT6372)

[RESEND PATCH v8 07/12] usb: typec: tcpci_mt6370: Add MediaTek MT6370 tcpci driver

2022-08-24 Thread ChiaEn Wu
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 support for the Type-C & Power Delivery controller in
MediaTek MT6370 IC.

Reviewed-by: AngeloGioacchino Del Regno 

Reviewed-by: Guenter Roeck 
Reviewed-by: Andy Shevchenko 
Reviewed-by: Heikki Krogerus 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 drivers/usb/typec/tcpm/Kconfig|  11 ++
 drivers/usb/typec/tcpm/Makefile   |   1 +
 drivers/usb/typec/tcpm/tcpci_mt6370.c | 207 ++
 3 files changed, 219 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm/tcpci_mt6370.c

diff --git a/drivers/usb/typec/tcpm/Kconfig b/drivers/usb/typec/tcpm/Kconfig
index 073fd2ea5e0b..e6b88ca4a4b9 100644
--- a/drivers/usb/typec/tcpm/Kconfig
+++ b/drivers/usb/typec/tcpm/Kconfig
@@ -35,6 +35,17 @@ config TYPEC_MT6360
  USB Type-C. It works with Type-C Port Controller Manager
  to provide USB PD and USB Type-C functionalities.
 
+config TYPEC_TCPCI_MT6370
+   tristate "MediaTek MT6370 Type-C driver"
+   depends on MFD_MT6370
+   help
+ MediaTek MT6370 is a multi-functional IC that includes
+ USB Type-C. It works with Type-C Port Controller Manager
+ to provide USB PD and USB Type-C functionalities.
+
+ This driver can also be built as a module. The module
+ will be called "tcpci_mt6370".
+
 config TYPEC_TCPCI_MAXIM
tristate "Maxim TCPCI based Type-C chip driver"
help
diff --git a/drivers/usb/typec/tcpm/Makefile b/drivers/usb/typec/tcpm/Makefile
index 7d499f3569fd..906d9dced8e7 100644
--- a/drivers/usb/typec/tcpm/Makefile
+++ b/drivers/usb/typec/tcpm/Makefile
@@ -6,4 +6,5 @@ typec_wcove-y   := wcove.o
 obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
 obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
 obj-$(CONFIG_TYPEC_MT6360) += tcpci_mt6360.o
+obj-$(CONFIG_TYPEC_TCPCI_MT6370)   += tcpci_mt6370.o
 obj-$(CONFIG_TYPEC_TCPCI_MAXIM)+= tcpci_maxim.o
diff --git a/drivers/usb/typec/tcpm/tcpci_mt6370.c 
b/drivers/usb/typec/tcpm/tcpci_mt6370.c
new file mode 100644
index ..c5bb201a5163
--- /dev/null
+++ b/drivers/usb/typec/tcpm/tcpci_mt6370.c
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define MT6370_REG_SYSCTRL80x9B
+
+#define MT6370_AUTOIDLE_MASK   BIT(3)
+
+#define MT6370_VENDOR_ID   0x29CF
+#define MT6370_TCPC_DID_A  0x2170
+
+struct mt6370_priv {
+   struct device *dev;
+   struct regulator *vbus;
+   struct tcpci *tcpci;
+   struct tcpci_data tcpci_data;
+};
+
+static const struct reg_sequence mt6370_reg_init[] = {
+   REG_SEQ(0xA0, 0x1, 1000),
+   REG_SEQ(0x81, 0x38, 0),
+   REG_SEQ(0x82, 0x82, 0),
+   REG_SEQ(0xBA, 0xFC, 0),
+   REG_SEQ(0xBB, 0x50, 0),
+   REG_SEQ(0x9E, 0x8F, 0),
+   REG_SEQ(0xA1, 0x5, 0),
+   REG_SEQ(0xA2, 0x4, 0),
+   REG_SEQ(0xA3, 0x4A, 0),
+   REG_SEQ(0xA4, 0x01, 0),
+   REG_SEQ(0x95, 0x01, 0),
+   REG_SEQ(0x80, 0x71, 0),
+   REG_SEQ(0x9B, 0x3A, 1000),
+};
+
+static int mt6370_tcpc_init(struct tcpci *tcpci, struct tcpci_data *data)
+{
+   u16 did;
+   int ret;
+
+   ret = regmap_register_patch(data->regmap, mt6370_reg_init,
+   ARRAY_SIZE(mt6370_reg_init));
+   if (ret)
+   return ret;
+
+   ret = regmap_raw_read(data->regmap, TCPC_BCD_DEV, , sizeof(u16));
+   if (ret)
+   return ret;
+
+   if (did == MT6370_TCPC_DID_A)
+   return regmap_write(data->regmap, TCPC_FAULT_CTRL, 0x80);
+
+   return 0;
+}
+
+static int mt6370_tcpc_set_vconn(struct tcpci *tcpci, struct tcpci_data *data,
+bool enable)
+{
+   return regmap_update_bits(data->regmap, MT6370_REG_SYSCTRL8,
+ MT6370_AUTOIDLE_MASK,
+ enable ? 0 : MT6370_AUTOIDLE_MASK);
+}
+
+static int mt6370_tcpc_set_vbus(struct tcpci *tcpci, struct tcpci_data *data,
+   bool source, bool sink)
+{
+   struct mt6370_priv *priv = container_of(data, struct mt6370_priv,
+   tcpci_data);
+   int ret;
+
+   ret = regulator_is_enabled(priv->vbus);
+   if (ret < 0)
+   return ret;
+
+   if (ret && !source)
+   re

[RESNED PATCH v8 03/12] dt-bindings: leds: mt6370: Add MediaTek MT6370 current sink type LED indicator

2022-08-24 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 current sink type LED indicator binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 .../leds/mediatek,mt6370-indicator.yaml   | 81 +++
 1 file changed, 81 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
new file mode 100644
index ..204b103ffc2c
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-indicator.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 LED driver include 4-channel RGB LED support Register/PWM/Breath 
Mode
+
+allOf:
+  - $ref: leds-class-multicolor.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-indicator
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^multi-led@[0-3]$":
+type: object
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-2]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2]
+
+required:
+  - reg
+  - color
+
+required:
+  - reg
+  - color
+  - "#address-cells"
+  - "#size-cells"
+
+  "^led@[0-3]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+required:
+  - reg
+  - color
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.34.1



[RESEND PATCH v8 12/12] video: backlight: mt6370: Add MediaTek MT6370 support

2022-08-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.

Add support for the MediaTek MT6370 backlight driver.
It controls 4 channels of 8 series WLEDs in
2048 (only for MT6370/MT6371) / 16384 (only for MT6372)
current steps (30 mA) in exponential or linear mapping curves.

Signed-off-by: ChiaEn Wu 
---

v8
- Add missing header file 
- Remove useledd header file 
- Revise the comment of 'mt6370_check_vendor_info()'
---
 drivers/video/backlight/Kconfig|  13 +
 drivers/video/backlight/Makefile   |   1 +
 drivers/video/backlight/mt6370-backlight.c | 351 +
 3 files changed, 365 insertions(+)
 create mode 100644 drivers/video/backlight/mt6370-backlight.c

diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig
index a003e02e13ce..936ba1e4d35e 100644
--- a/drivers/video/backlight/Kconfig
+++ b/drivers/video/backlight/Kconfig
@@ -268,6 +268,19 @@ config BACKLIGHT_MAX8925
  If you have a LCD backlight connected to the WLED output of MAX8925
  WLED output, say Y here to enable this driver.
 
+config BACKLIGHT_MT6370
+   tristate "MediaTek MT6370 Backlight Driver"
+   depends on MFD_MT6370
+   help
+ This enables support for Mediatek MT6370 Backlight driver.
+ It's commonly used to drive the display WLED. There are 4 channels
+ inside, and each channel supports up to 30mA of current capability
+ with 2048 current steps (only for MT6370/MT6371) or 16384 current
+ steps (only for MT6372) in exponential or linear mapping curves.
+
+ This driver can also be built as a module. If so, the module
+ will be called "mt6370-backlight".
+
 config BACKLIGHT_APPLE
tristate "Apple Backlight Driver"
depends on X86 && ACPI
diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile
index cae2c83422ae..e815f3f1deff 100644
--- a/drivers/video/backlight/Makefile
+++ b/drivers/video/backlight/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_BACKLIGHT_LP855X)+= lp855x_bl.o
 obj-$(CONFIG_BACKLIGHT_LP8788) += lp8788_bl.o
 obj-$(CONFIG_BACKLIGHT_LV5207LP)   += lv5207lp.o
 obj-$(CONFIG_BACKLIGHT_MAX8925)+= max8925_bl.o
+obj-$(CONFIG_BACKLIGHT_MT6370) += mt6370-backlight.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)  += omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PANDORA)+= pandora_bl.o
 obj-$(CONFIG_BACKLIGHT_PCF50633)   += pcf50633-backlight.o
diff --git a/drivers/video/backlight/mt6370-backlight.c 
b/drivers/video/backlight/mt6370-backlight.c
new file mode 100644
index ..844c94655633
--- /dev/null
+++ b/drivers/video/backlight/mt6370-backlight.c
@@ -0,0 +1,351 @@
+// 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 
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_BL_EN   0x1A0
+#define MT6370_REG_BL_BSTCTRL  0x1A1
+#define MT6370_REG_BL_PWM  0x1A2
+#define MT6370_REG_BL_DIM2 0x1A4
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+#define MT6370_BL_EXT_EN_MASK  BIT(7)
+#define MT6370_BL_EN_MASK  BIT(6)
+#define MT6370_BL_CODE_MASKBIT(0)
+#define MT6370_BL_CH_MASK  GENMASK(5, 2)
+#define MT6370_BL_CH_SHIFT 2
+#define MT6370_BL_DIM2_COMMON_MASK GENMASK(2, 0)
+#define MT6370_BL_DIM2_COMMON_SHIFT3
+#define MT6370_BL_DIM2_6372_MASK   GENMASK(5, 0)
+#define MT6370_BL_DIM2_6372_SHIFT  6
+#define MT6370_BL_PWM_EN_MASK  BIT(7)
+#define MT6370_BL_PWM_HYS_EN_MASK  BIT(2)
+#define MT6370_BL_PWM_HYS_SEL_MASK GENMASK(1, 0)
+#define MT6370_BL_OVP_EN_MASK  BIT(7)
+#define MT6370_BL_OVP_SEL_MASK GENMASK(6, 5)
+#define MT6370_BL_OVP_SEL_SHIFT5
+#define MT6370_BL_OC_EN_MASK   BIT(3)
+#define MT6370_BL_OC_SEL_MASK  GENMASK(2, 1)
+#define MT6370_BL_OC_SEL_SHIFT 1
+
+#define MT6370_BL_PWM_HYS_TH_MIN_STEP  1
+#define MT6370_BL_PWM_HYS_TH_MAX_STEP  64
+#define MT6370_BL_OVP_MIN_UV   1700
+#define MT6370_BL_OVP_MAX_UV   2900
+#define MT6370_BL_OVP_STEP_UV  400
+#define MT6370_BL_OCP_MIN_UA   90
+#define MT6370_BL_OCP_MAX_UA   180
+#define MT6370_BL_OCP_STEP_UA  30
+#define MT6370_BL_MAX_COMMON_BRIGHTNESS2048
+#define MT6370_BL_MAX_6372_BRIGHTNESS  16384
+#define MT6370_BL_MAX_CH   15
+
+enum {
+   MT6370_VID_COMMON = 1,
+   

[RESEND PATCH v8 11/12] leds: flash: mt6370: Add MediaTek MT6370 flashlight support

2022-08-15 Thread ChiaEn Wu
From: Alice Chen 

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 support for the MT6370 Flash LED driver. Flash LED in MT6370
has 2 channels and support torch/strobe mode.

Signed-off-by: Alice Chen 
Signed-off-by: ChiaEn Wu 
---

v8
- Add missing 'dependency config' in Kconfig
- Revise all multi-line comments
---
 drivers/leds/flash/Kconfig |  14 +
 drivers/leds/flash/Makefile|   1 +
 drivers/leds/flash/leds-mt6370-flash.c | 632 +
 3 files changed, 647 insertions(+)
 create mode 100644 drivers/leds/flash/leds-mt6370-flash.c

diff --git a/drivers/leds/flash/Kconfig b/drivers/leds/flash/Kconfig
index d3eb689b193c..405bd16f7013 100644
--- a/drivers/leds/flash/Kconfig
+++ b/drivers/leds/flash/Kconfig
@@ -61,6 +61,20 @@ config LEDS_MT6360
  Independent current sources supply for each flash LED support torch
  and strobe mode.
 
+config LEDS_MT6370_FLASH
+   tristate "Flash LED Support for MediaTek MT6370 PMIC"
+   depends on LEDS_CLASS && OF
+   depends on LEDS_CLASS_FLASH || !LEDS_CLASS_FLASH
+   depends on V4L2_FLASH_LED_CLASS || !V4L2_FLASH_LED_CLASS
+   depends on MFD_MT6370
+   help
+ Support 2 channels and torch/strobe mode.
+ Say Y here to enable support for
+ MT6370_FLASH_LED device.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-flash".
+
 config LEDS_RT4505
tristate "LED support for RT4505 flashlight controller"
depends on I2C && OF
diff --git a/drivers/leds/flash/Makefile b/drivers/leds/flash/Makefile
index 0acbddc0b91b..0c1f3c5ffe41 100644
--- a/drivers/leds/flash/Makefile
+++ b/drivers/leds/flash/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_LEDS_MAX77693) += leds-max77693.o
 obj-$(CONFIG_LEDS_RT4505)  += leds-rt4505.o
 obj-$(CONFIG_LEDS_RT8515)  += leds-rt8515.o
 obj-$(CONFIG_LEDS_SGM3140) += leds-sgm3140.o
+obj-$(CONFIG_LEDS_MT6370_FLASH)+= leds-mt6370-flash.o
diff --git a/drivers/leds/flash/leds-mt6370-flash.c 
b/drivers/leds/flash/leds-mt6370-flash.c
new file mode 100644
index ..c22770856167
--- /dev/null
+++ b/drivers/leds/flash/leds-mt6370-flash.c
@@ -0,0 +1,632 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_FLASH1,
+   MT6370_LED_FLASH2,
+   MT6370_MAX_LEDS
+};
+
+/* Virtual definition for multicolor */
+
+#define MT6370_REG_FLEDEN  0x17E
+#define MT6370_REG_STRBTO  0x173
+#define MT6370_REG_CHGSTAT20x1D1
+#define MT6370_REG_FLEDSTAT1   0x1D9
+#define MT6370_REG_FLEDISTRB(_id)  (0x174 + 4 * (_id))
+#define MT6370_REG_FLEDITOR(_id)   (0x175 + 4 * (_id))
+#define MT6370_ITORCH_MASK GENMASK(4, 0)
+#define MT6370_ISTROBE_MASKGENMASK(6, 0)
+#define MT6370_STRBTO_MASK GENMASK(6, 0)
+#define MT6370_TORCHEN_MASKBIT(3)
+#define MT6370_STROBEN_MASKBIT(2)
+#define MT6370_FLCSEN_MASK(_id)BIT(MT6370_LED_FLASH2 - (_id))
+#define MT6370_FLCSEN_MASK_ALL GENMASK(1, 0)
+#define MT6370_FLEDCHGVINOVP_MASK  BIT(3)
+#define MT6370_FLED1STRBTO_MASKBIT(11)
+#define MT6370_FLED2STRBTO_MASKBIT(10)
+#define MT6370_FLED1STRB_MASK  BIT(9)
+#define MT6370_FLED2STRB_MASK  BIT(8)
+#define MT6370_FLED1SHORT_MASK BIT(7)
+#define MT6370_FLED2SHORT_MASK BIT(6)
+#define MT6370_FLEDLVF_MASKBIT(3)
+
+#define MT6370_LED_JOINT   2
+#define MT6370_RANGE_FLED_REG  4
+#define MT6370_ITORCH_MIN_uA   25000
+#define MT6370_ITORCH_STEP_uA  12500
+#define MT6370_ITORCH_MAX_uA   40
+#define MT6370_ITORCH_DOUBLE_MAX_uA80
+#define MT6370_ISTRB_MIN_uA5
+#define MT6370_ISTRB_STEP_uA   12500
+#define MT6370_ISTRB_MAX_uA150
+#define MT6370_ISTRB_DOUBLE_MAX_uA 300
+#define MT6370_STRBTO_MIN_US   64000
+#define MT6370_STRBTO_STEP_US  32000
+#define MT6370_STRBTO_MAX_US   2432000
+
+#define STATE_OFF  0
+#define STATE_KEEP 1
+#define STATE_ON   2
+
+#define to_mt6370_led(ptr, member) container_of(ptr, struct mt6370_led, member)
+
+struct mt6370_led {
+   struct led_classdev_flash flash;
+   struct v4l2_flash *v4l2_flash;
+   struct 

[RESEND PATCH v8 10/12] leds: rgb: mt6370: Add MediaTek MT6370 current sink type LED Indicator support

2022-08-15 Thread ChiaEn Wu
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 support for the MediaTek MT6370 Current Sink Type LED Indicator
driver. It can control four channels current-sink RGB LEDs with 3 modes:
constant current, PWM, and breath mode.

Reviewed-by: AngeloGioacchino Del Regno 

Co-developed-by: Alice Chen 
Signed-off-by: Alice Chen 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---

v8
- Add include header file 
- Remove useless 'enum mt6370_state' because of using 'enum led_default_state'
- Revise the calculation of 'val' in 'mt6370_gen_breath_pattern()'
- Replace the medthod of getting default-state by using
  led_init_default_state_get()
---
 drivers/leds/rgb/Kconfig   |   13 +
 drivers/leds/rgb/Makefile  |1 +
 drivers/leds/rgb/leds-mt6370-rgb.c | 1010 
 3 files changed, 1024 insertions(+)
 create mode 100644 drivers/leds/rgb/leds-mt6370-rgb.c

diff --git a/drivers/leds/rgb/Kconfig b/drivers/leds/rgb/Kconfig
index 204cf470beae..7d86bb26c54b 100644
--- a/drivers/leds/rgb/Kconfig
+++ b/drivers/leds/rgb/Kconfig
@@ -26,4 +26,17 @@ config LEDS_QCOM_LPG
 
  If compiled as a module, the module will be named leds-qcom-lpg.
 
+config LEDS_MT6370_RGB
+   tristate "LED Support for MediaTek MT6370 PMIC"
+   depends on MFD_MT6370
+   select LINEAR_RANGE
+   help
+ Say Y here to enable support for MT6370_RGB LED device.
+ In MT6370, there are four channel current-sink LED drivers that
+ support hardware pattern for constant current, PWM, and breath mode.
+ Isink4 channel can also be used as a CHG_VIN power good indicator.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-rgb".
+
 endif # LEDS_CLASS_MULTICOLOR
diff --git a/drivers/leds/rgb/Makefile b/drivers/leds/rgb/Makefile
index 0675bc0f6e18..8c01daf63f61 100644
--- a/drivers/leds/rgb/Makefile
+++ b/drivers/leds/rgb/Makefile
@@ -2,3 +2,4 @@
 
 obj-$(CONFIG_LEDS_PWM_MULTICOLOR)  += leds-pwm-multicolor.o
 obj-$(CONFIG_LEDS_QCOM_LPG)+= leds-qcom-lpg.o
+obj-$(CONFIG_LEDS_MT6370_RGB)  += leds-mt6370-rgb.o
diff --git a/drivers/leds/rgb/leds-mt6370-rgb.c 
b/drivers/leds/rgb/leds-mt6370-rgb.c
new file mode 100644
index ..1f9d6484482d
--- /dev/null
+++ b/drivers/leds/rgb/leds-mt6370-rgb.c
@@ -0,0 +1,1010 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Authors:
+ *   ChiYuan Huang 
+ *   Alice Chen 
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_ISNK1 = 0,
+   MT6370_LED_ISNK2,
+   MT6370_LED_ISNK3,
+   MT6370_LED_ISNK4,
+   MT6370_MAX_LEDS
+};
+
+enum mt6370_led_mode {
+   MT6370_LED_PWM_MODE = 0,
+   MT6370_LED_BREATH_MODE,
+   MT6370_LED_REG_MODE,
+   MT6370_LED_MAX_MODE
+};
+
+enum mt6370_led_field {
+   F_RGB_EN = 0,
+   F_CHGIND_EN,
+   F_LED1_CURR,
+   F_LED2_CURR,
+   F_LED3_CURR,
+   F_LED4_CURR,
+   F_LED1_MODE,
+   F_LED2_MODE,
+   F_LED3_MODE,
+   F_LED4_MODE,
+   F_LED1_DUTY,
+   F_LED2_DUTY,
+   F_LED3_DUTY,
+   F_LED4_DUTY,
+   F_LED1_FREQ,
+   F_LED2_FREQ,
+   F_LED3_FREQ,
+   F_LED4_FREQ,
+   F_MAX_FIELDS
+};
+
+enum mt6370_led_ranges {
+   R_LED123_CURR = 0,
+   R_LED4_CURR,
+   R_LED_TRFON,
+   R_LED_TOFF,
+   R_MAX_RANGES
+};
+
+enum mt6370_pattern {
+   P_LED_TR1 = 0,
+   P_LED_TR2,
+   P_LED_TF1,
+   P_LED_TF2,
+   P_LED_TON,
+   P_LED_TOFF,
+   P_MAX_PATTERNS
+};
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_RGB1_DIM0x182
+#define MT6370_REG_RGB2_DIM0x183
+#define MT6370_REG_RGB3_DIM0x184
+#define MT6370_REG_RGB_EN  0x185
+#define MT6370_REG_RGB1_ISNK   0x186
+#define MT6370_REG_RGB2_ISNK   0x187
+#define MT6370_REG_RGB3_ISNK   0x188
+#define MT6370_REG_RGB1_TR 0x189
+#define MT6370_REG_RGB_CHRIND_DIM  0x192
+#define MT6370_REG_RGB_CHRIND_CTRL 0x193
+#define MT6370_REG_RGB_CHRIND_TR   0x194
+
+#define MT6372_REG_RGB_EN  0x182
+#define MT6372_REG_RGB1_ISNK   0x183
+#define MT6372_REG_RGB2_ISNK   0x184
+#define MT6372_REG_RGB3_ISNK   0x185
+#define MT6372_REG_RGB4_ISNK   0x186
+#de

[RESEND PATCH v8 08/12] iio: adc: mt6370: Add MediaTek MT6370 support

2022-08-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.

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

Reviewed-by: AngeloGioacchino Del Regno 

Reviewed-by: Andy Shevchenko 
Acked-by: Jonathan Cameron 
Signed-off-by: ChiaEn Wu 
---
 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 7fe5930891e0..995cbb5e5b9e 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 1772a549a3c8..c6bc35fcf1ac 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 ..2a46471f819b
--- /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, "Failed to read ADC register (%d)\n", ret);
+   goto adc_un

[RESEND PATCH v8 06/12] lib: add linear range index macro

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

Add linear_range_idx macro for declaring the linear_range struct simply.

Signed-off-by: ChiaEn Wu 
---
 include/linux/linear_range.h | 8 
 1 file changed, 8 insertions(+)

diff --git a/include/linux/linear_range.h b/include/linux/linear_range.h
index fd3d0b358f22..fb53ea13c593 100644
--- a/include/linux/linear_range.h
+++ b/include/linux/linear_range.h
@@ -26,6 +26,14 @@ struct linear_range {
unsigned int step;
 };
 
+#define LINEAR_RANGE_IDX(_min, _min_sel, _max_sel, _step)  \
+   {   \
+   .min = _min,\
+   .min_sel = _min_sel,\
+   .max_sel = _max_sel,\
+   .step = _step,  \
+   }
+
 unsigned int linear_range_values_in_range(const struct linear_range *r);
 unsigned int linear_range_values_in_range_array(const struct linear_range *r,
int ranges);
-- 
2.34.1



[RESEND PATCH v8 09/12] power: supply: mt6370: Add MediaTek MT6370 charger driver

2022-08-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.

Add support for the MediaTek MT6370 Charger driver. 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.

Reviewed-by: Andy Shevchenko 
Signed-off-by: ChiaEn Wu 
---

v8
- Remove MT6370_CHG_LINEAR_RANGE() macro, and change to use
  LINEAR_RANGE_IDX() macro
---
 drivers/power/supply/Kconfig  |  14 +
 drivers/power/supply/Makefile |   1 +
 drivers/power/supply/mt6370-charger.c | 957 ++
 3 files changed, 972 insertions(+)
 create mode 100644 drivers/power/supply/mt6370-charger.c

diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index 1aa8323ad9f6..591deb82e2c6 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 7f02f36aea55..8c9527643c86 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 ..db3e03463160
--- /dev/null
+++ b/drivers/power/supply/mt6370-charger.c
@@ -0,0 +1,957 @@
+// 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,
+   /* MT6370_REG_USB_STATUS1 */
+   F_USB_STAT, F_CHGDET,
+   /* MT6370_REG_CHG_STAT */
+   F_CHG_STAT, F_BOOST_STAT, F_VBAT_LVL,
+   /* MT6370_REG_FLED_EN */
+   F_FL_STROB

[RESEND PATCH v8 05/12] dt-bindings: backlight: Add MediaTek MT6370 backlight

2022-08-15 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MT6370 backlight binding documentation.

Reviewed-by: Rob Herring 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 .../backlight/mediatek,mt6370-backlight.yaml  | 121 ++
 1 file changed, 121 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
new file mode 100644
index ..5533b6562d92
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
@@ -0,0 +1,121 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: 
http://devicetree.org/schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Backlight
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  The MT6370 Backlight WLED driver supports up to a 29V output voltage for
+  4 channels of 8 series WLEDs. Each channel supports up to 30mA of current
+  capability with 2048 current steps (11 bits, only for MT6370/MT6371) or
+  16384 current steps (14 bits, only for MT6372) in exponential or linear
+  mapping curves.
+
+allOf:
+  - $ref: common.yaml#
+
+properties:
+  compatible:
+enum:
+  - mediatek,mt6370-backlight
+  - mediatek,mt6372-backlight
+
+  default-brightness:
+minimum: 0
+
+  max-brightness:
+minimum: 0
+
+  enable-gpios:
+description: External backlight 'enable' pin
+maxItems: 1
+
+  mediatek,bled-pwm-enable:
+description: |
+  Enable external PWM input for backlight dimming
+type: boolean
+
+  mediatek,bled-pwm-hys-enable:
+description: |
+  Enable the backlight input-hysteresis for PWM mode
+type: boolean
+
+  mediatek,bled-pwm-hys-input-th-steps:
+$ref: /schemas/types.yaml#/definitions/uint8
+enum: [1, 4, 16, 64]
+description: |
+  The selection of the upper and lower bounds threshold of backlight
+  PWM resolution. If we choose selection 64, the variation of PWM
+  resolution needs more than 64 steps.
+
+  mediatek,bled-ovp-shutdown:
+description: |
+  Enable the backlight shutdown when OVP level triggered
+type: boolean
+
+  mediatek,bled-ovp-microvolt:
+enum: [1700, 2100, 2500, 2900]
+description: |
+  Backlight OVP level selection.
+
+  mediatek,bled-ocp-shutdown:
+description: |
+  Enable the backlight shutdown when OCP level triggerred.
+type: boolean
+
+  mediatek,bled-ocp-microamp:
+enum: [90, 120, 150, 180]
+description: |
+  Backlight OC level selection.
+
+  mediatek,bled-exponential-mode-enable:
+description: |
+  Enable the exponential mode of backlight brightness. If this property
+  is not enabled, the default is to use linear mode.
+type: boolean
+
+  mediatek,bled-channel-use:
+$ref: /schemas/types.yaml#/definitions/uint8
+description: |
+  Backlight LED channel to be used.
+  Each bit mapping to:
+- 0: CH4
+- 1: CH3
+- 2: CH2
+- 3: CH1
+minimum: 1
+maximum: 15
+
+if:
+  properties:
+compatible:
+  contains:
+const: mediatek,mt6372-backlight
+
+then:
+  properties:
+default-brightness:
+  maximum: 16384
+
+max-brightness:
+  maximum: 16384
+
+else:
+  properties:
+default-brightness:
+  maximum: 2048
+
+max-brightness:
+  maximum: 2048
+
+required:
+  - compatible
+  - mediatek,bled-channel-use
+
+additionalProperties: false
-- 
2.34.1



[RESEND v8 04/12] dt-bindings: leds: Add MediaTek MT6370 flashlight

2022-08-15 Thread ChiaEn Wu
From: Alice Chen 

Add MediaTek MT6370 flashlight binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: Alice Chen 
Signed-off-by: ChiaEn Wu 
---
 .../leds/mediatek,mt6370-flashlight.yaml  | 41 +++
 1 file changed, 41 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
new file mode 100644
index ..e9d02ed6a590
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-flashlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Flash LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 flash LED driver include 2-channel flash LED support Torch/Strobe 
Mode.
+
+properties:
+  compatible:
+const: mediatek,mt6370-flashlight
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-1]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1]
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.34.1



[RESEND PATCH v8 01/12] dt-bindings: usb: Add MediaTek MT6370 TCPC

2022-08-15 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 TCPC binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 .../bindings/usb/mediatek,mt6370-tcpc.yaml| 36 +++
 1 file changed, 36 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml 
b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
new file mode 100644
index ..72f56cc88457
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/mediatek,mt6370-tcpc.yaml#;
+$schema: "http://devicetree.org/meta-schemas/core.yaml#;
+
+title: MediatTek MT6370 Type-C Port Switch and Power Delivery controller
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  MediaTek MT6370 is a multi-functional device.
+  It integrates charger, ADC, flash, RGB indicators,
+  regulators (DSV/VIBLDO), and TypeC Port Switch with Power Delivery 
controller.
+  This document only describes MT6370 Type-C Port Switch and
+  Power Delivery controller.
+
+properties:
+  compatible:
+enum:
+  - mediatek,mt6370-tcpc
+
+  interrupts:
+maxItems: 1
+
+  connector:
+type: object
+$ref: /schemas/connector/usb-connector.yaml#
+unevaluatedProperties: false
+
+additionalProperties: false
+
+required:
+  - compatible
+  - interrupts
-- 
2.34.1



[RESEND PATCH v8 02/12] dt-bindings: power: supply: Add MediaTek MT6370 Charger

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

Add MediaTek MT6370 Charger binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiaEn Wu 
---
 .../power/supply/mediatek,mt6370-charger.yaml | 88 +++
 1 file changed, 88 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml

diff --git 
a/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
new file mode 100644
index ..bd09a0af7e65
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/mediatek,mt6370-charger.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Battery Charger
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Provides Battery Charger, Boost for OTG devices and BC1.2 detection.
+
+properties:
+  compatible:
+const: mediatek,mt6370-charger
+
+  interrupts:
+description: |
+  Specify what irqs are needed to be handled by MT6370 Charger driver. IRQ
+  "MT6370_IRQ_CHG_MIVR", "MT6370_IRQ_ATTACH" and "MT6370_IRQ_OVPCTRL_UVP_D"
+  are required.
+items:
+  - description: BC1.2 done irq
+  - description: usb plug in irq
+  - description: mivr irq
+
+  interrupt-names:
+items:
+  - const: attach_i
+  - const: uvp_d_evt
+  - const: mivr
+
+  io-channels:
+description: |
+  Use ADC channel to read VBUS, IBUS, IBAT, etc., info.
+minItems: 1
+items:
+  - description: |
+  VBUS voltage with lower accuracy (+-75mV) but higher measure
+  range (1~22V)
+  - description: |
+  VBUS voltage with higher accuracy (+-30mV) but lower measure
+  range (1~9.76V)
+  - description: the main system input voltage
+  - description: battery voltage
+  - description: battery temperature-sense input voltage
+  - description: IBUS current (required)
+  - description: battery current
+  - description: |
+  regulated output voltage to supply for the PWM low-side gate driver
+  and the bootstrap capacitor
+  - description: IC junction temperature
+
+  io-channel-names:
+minItems: 1
+items:
+  - const: vbusdiv5
+  - const: vbusdiv2
+  - const: vsys
+  - const: vbat
+  - const: ts_bat
+  - const: ibus
+  - const: ibat
+  - const: chg_vddp
+  - const: temp_jc
+
+  usb-otg-vbus-regulator:
+type: object
+description: OTG boost regulator.
+unevaluatedProperties: false
+$ref: /schemas/regulator/regulator.yaml#
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - interrupts
+  - interrupt-names
+  - io-channels
+
+additionalProperties: false
+
+...
-- 
2.34.1



[RESEND PATCH v8 00/12] Add MediaTek MT6370 PMIC support

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

I apologize for using the wrong mail list to send the wrong emails
for the last v8 patchset...

This patch series add MediaTek MT6370 PMIC support and add a index macro
to . The 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.

First, in this series of patches,
'dt-binding: mfd', 'mfd driver' has been applied by Lee in the v7.
https://lore.kernel.org/all/yvjdpq0mwnpqz...@google.com/
https://lore.kernel.org/all/yvjdxepc2cb58...@google.com/

Second, the LED RGB driver is based on Andy Shevchenko's patch which moves
led_init_default_state_get() to the global header.
https://lore.kernel.org/all/20220805154907.32263-3-andriy.shevche...@linux.intel.com/

In addition, we added a macro to the < for declaring the
linear_range struct simply (see patch v8-0006) and made some changes for
MT6370 drivers (see v8 section of the change log below).

Thank you,
ChiaEn Wu
---
Changes in v8:
- In Patch 06/12:
- Add linear_range_idx macro for declaring the linear_range
  struct simply.

- In Patch 09/12:
- Remove MT6370_CHG_LINEAR_RANGE() macro, and change to use
  LINEAR_RANGE_IDX() macro

- In Patch 10/12:
- Add include header file 
- Remove useless 'enum mt6370_state' because of using 'enum 
led_default_state'
- Revise the calculation of 'val' in 'mt6370_gen_breath_pattern()'
- Replace the medthod of getting default-state by using
  led_init_default_state_get()

- In Patch 11/12:
- Add missing 'dependency config' in Kconfig
- Revise all multi-line comments

- In Patch 12/12:
- Add missing header file 
- Remove useledd header file 
- Revise the comment of 'mt6370_check_vendor_info()'


Changes in v7:
- In Patch 05/13:
- Add the second compatible string for 'mt6372'
- Add 'mediatek,bled-exponential-mode-enable' property for enable the
  exponential mode of backlight brightness
- Add validation for the maximum value of 'default-brightness' and
  'max-brightness'

- In Patch 07/13:
- Move '#define MT6370_REG_MAXADDR' to the next line of
  '#define MT6370_REG_CHG_MASK1'
- Rename 'MT6370_REG_ADDRLEN' to 'MT6370_MAX_ADDRLEN'

- In Patch 08/13:
- Revise 'devm_add_action_or_reset(dev, ...)' to one line
- Revise 'return regmap_update_bits(...)' with using positive
  conditional

- In Patch 09/13:
- 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

- In Patch 10/13:
- Revise the method to enable/disable irq
- Revise all 'if (ret < 0)' to 'if (ret)' after using
  mt6370_chg_field_set/get()
- Revise all 'OTG' text again

- In Patch 11/13:
- Add the comment for the union of 'struct mt6370_led'
- Revise the wrong description of 'Authors'
- Revise some typos (e.g. led --> LED)
- Revise 'if (!fwnode_property_read_string())' to
  'ret = fwnode_property_read_string()'
- Replace 'memcpy(...)' with 'put_unaligned_be24()' in
  mt6370_gen_breath_pattern()
- Replace all 'LED_OFF' with 0
- Remove the redundant assignment in mt6370_mc_pattern_clear()

- In Patch 12/13:
- Fix the indentation.
- For the well defined macro, the parenthesis is needed for input 
parameters.
- Replace some dev_warn to dev_info in 'init_flash_properties'.
- Add sentinel comment for the terminator entry of of_device_id.
- Use priv->fled_torch_used directly.
- Delete 0 in {}.
- Use _uA instead of _UA in definition.
- Refine the description.
- Use usleep_range instead of udelay.
- Rename config to LEDS_MT6370_FLASH.
- Add missing ">" in copyright.
- Change the Kconfig order

- In Patch 13/13:
- Add support 'exponential mode' property parsing
- Add 'return dev_err_probe()' after 'if (IS_ERR(priv->enable_gpio))'
- Add 'mt6372' compatible string
- Revise Kconfig help text
- Revise update()/get() for supporting 16384 steps (MT6372)
- Revise all shift usages form using 'ffs() and fls()' to defining the
  _SHIFT macros.
- Revise 'brightness ? 1 : 0' to '!!brightness' in gpiod_set_value()


Changes in v6:
- In Patch 03/13:
- Add 'reg' property of led of multi-led to prevent checking
  error.

- In Patch 08/13:
- Convert tcpci as device resource managed with
  'devm_add_action_or_reset' api.

[PATCH v7 04/13] dt-bindings: leds: Add MediaTek MT6370 flashlight

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

Add MediaTek MT6370 flashlight binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: Alice Chen 
Signed-off-by: ChiaEn Wu 
---
 .../bindings/leds/mediatek,mt6370-flashlight.yaml  | 41 ++
 1 file changed, 41 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
new file mode 100644
index 000..e9d02ed
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-flashlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Flash LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 flash LED driver include 2-channel flash LED support Torch/Strobe 
Mode.
+
+properties:
+  compatible:
+const: mediatek,mt6370-flashlight
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-1]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1]
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v7 12/13] leds: flash: mt6370: Add MediaTek MT6370 flashlight support

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

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 MT6370 Flash LED driver. Flash LED in MT6370
has 2 channels and support torch/strobe mode.

Signed-off-by: Alice Chen 
Signed-off-by: ChiaEn Wu 
---

v7
- Fix the indentation.
- For the well defined macro, the parenthesis is needed for input parameters.
- Replace some dev_warn to dev_info in 'init_flash_properties'.
- Add sentinel comment for the terminator entry of of_device_id.
- Use priv->fled_torch_used directly.
- Delete 0 in {}.
- Use _uA instead of _UA in definition.
- Refine the description.
- Use usleep_range instead of udelay.
- Rename config to LEDS_MT6370_FLASH.
- Add missing ">" in copyright.
- Change the Kconfig order
---
 drivers/leds/flash/Kconfig |  12 +
 drivers/leds/flash/Makefile|   1 +
 drivers/leds/flash/leds-mt6370-flash.c | 632 +
 3 files changed, 645 insertions(+)
 create mode 100644 drivers/leds/flash/leds-mt6370-flash.c

diff --git a/drivers/leds/flash/Kconfig b/drivers/leds/flash/Kconfig
index d3eb689..4b1afc1 100644
--- a/drivers/leds/flash/Kconfig
+++ b/drivers/leds/flash/Kconfig
@@ -61,6 +61,18 @@ config LEDS_MT6360
  Independent current sources supply for each flash LED support torch
  and strobe mode.
 
+config LEDS_MT6370_FLASH
+   tristate "Flash LED Support for MediaTek MT6370 PMIC"
+   depends on LEDS_CLASS
+   depends on MFD_MT6370
+   help
+ Support 2 channels and torch/strobe mode.
+ Say Y here to enable support for
+ MT6370_FLASH_LED device.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-flash".
+
 config LEDS_RT4505
tristate "LED support for RT4505 flashlight controller"
depends on I2C && OF
diff --git a/drivers/leds/flash/Makefile b/drivers/leds/flash/Makefile
index 0acbddc..0c1f3c5 100644
--- a/drivers/leds/flash/Makefile
+++ b/drivers/leds/flash/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_LEDS_MAX77693) += leds-max77693.o
 obj-$(CONFIG_LEDS_RT4505)  += leds-rt4505.o
 obj-$(CONFIG_LEDS_RT8515)  += leds-rt8515.o
 obj-$(CONFIG_LEDS_SGM3140) += leds-sgm3140.o
+obj-$(CONFIG_LEDS_MT6370_FLASH)+= leds-mt6370-flash.o
diff --git a/drivers/leds/flash/leds-mt6370-flash.c 
b/drivers/leds/flash/leds-mt6370-flash.c
new file mode 100644
index 000..e36e264
--- /dev/null
+++ b/drivers/leds/flash/leds-mt6370-flash.c
@@ -0,0 +1,632 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_FLASH1,
+   MT6370_LED_FLASH2,
+   MT6370_MAX_LEDS
+};
+
+/* Virtual definition for multicolor */
+
+#define MT6370_REG_FLEDEN  0x17E
+#define MT6370_REG_STRBTO  0x173
+#define MT6370_REG_CHGSTAT20x1D1
+#define MT6370_REG_FLEDSTAT1   0x1D9
+#define MT6370_REG_FLEDISTRB(_id)  (0x174 + 4 * (_id))
+#define MT6370_REG_FLEDITOR(_id)   (0x175 + 4 * (_id))
+#define MT6370_ITORCH_MASK GENMASK(4, 0)
+#define MT6370_ISTROBE_MASKGENMASK(6, 0)
+#define MT6370_STRBTO_MASK GENMASK(6, 0)
+#define MT6370_TORCHEN_MASKBIT(3)
+#define MT6370_STROBEN_MASKBIT(2)
+#define MT6370_FLCSEN_MASK(_id)BIT(MT6370_LED_FLASH2 - (_id))
+#define MT6370_FLCSEN_MASK_ALL GENMASK(1, 0)
+#define MT6370_FLEDCHGVINOVP_MASK  BIT(3)
+#define MT6370_FLED1STRBTO_MASKBIT(11)
+#define MT6370_FLED2STRBTO_MASKBIT(10)
+#define MT6370_FLED1STRB_MASK  BIT(9)
+#define MT6370_FLED2STRB_MASK  BIT(8)
+#define MT6370_FLED1SHORT_MASK BIT(7)
+#define MT6370_FLED2SHORT_MASK BIT(6)
+#define MT6370_FLEDLVF_MASKBIT(3)
+
+#define MT6370_LED_JOINT   2
+#define MT6370_RANGE_FLED_REG  4
+#define MT6370_ITORCH_MIN_uA   25000
+#define MT6370_ITORCH_STEP_uA  12500
+#define MT6370_ITORCH_MAX_uA   40
+#define MT6370_ITORCH_DOUBLE_MAX_uA80
+#define MT6370_ISTRB_MIN_uA5
+#define MT6370_ISTRB_STEP_uA   12500
+#define MT6370_ISTRB_MAX_uA150
+#define MT6370_ISTRB_DOUBLE_MAX_uA 300
+#define MT6370_STRBTO_MIN_US   64000
+#define MT6370_STRBTO_STEP_US  32000
+#define MT6370_STRBTO_MAX_US   2432000
+
+#define STATE_OFF  0
+#define STATE_KEEP  

[PATCH v7 07/13] mfd: mt6370: Add MediaTek MT6370 support

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

This adds support for the MediaTek MT6370 SubPMIC. 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.

Reviewed-by: Andy Shevchenko 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---

v7
- Move '#define MT6370_REG_MAXADDR' to the next line of
  '#define MT6370_REG_CHG_MASK1'
- Rename 'MT6370_REG_ADDRLEN' to 'MT6370_MAX_ADDRLEN'
---
 drivers/mfd/Kconfig  |  16 +++
 drivers/mfd/Makefile |   1 +
 drivers/mfd/mt6370.c | 312 +++
 drivers/mfd/mt6370.h |  99 
 4 files changed, 428 insertions(+)
 create mode 100644 drivers/mfd/mt6370.c
 create mode 100644 drivers/mfd/mt6370.h

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 9566341..0666bda 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -938,6 +938,22 @@ config MFD_MT6360
  PMIC part includes 2-channel BUCKs and 2-channel LDOs
  LDO part includes 4-channel LDOs
 
+config MFD_MT6370
+   tristate "MediaTek MT6370 SubPMIC"
+   select MFD_CORE
+   select REGMAP_I2C
+   select REGMAP_IRQ
+   depends on I2C
+   help
+ Say Y here to enable MT6370 SubPMIC functional support.
+ It consists 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 driver can also be built as a module. If so, the module
+ will be called "mt6370".
+
 config MFD_MT6397
tristate "MediaTek MT6397 PMIC Support"
select MFD_CORE
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 8c69867..81dbed3 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -173,6 +173,7 @@ obj-$(CONFIG_MFD_MAX8998)   += max8998.o max8998-irq.o
 obj-$(CONFIG_MFD_MP2629)   += mp2629.o
 
 obj-$(CONFIG_MFD_MT6360)   += mt6360-core.o
+obj-$(CONFIG_MFD_MT6370)   += mt6370.o
 mt6397-objs:= mt6397-core.o mt6397-irq.o mt6358-irq.o
 obj-$(CONFIG_MFD_MT6397)   += mt6397.o
 
diff --git a/drivers/mfd/mt6370.c b/drivers/mfd/mt6370.c
new file mode 100644
index 000..cf19cce
--- /dev/null
+++ b/drivers/mfd/mt6370.c
@@ -0,0 +1,312 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "mt6370.h"
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_CHG_IRQ10x1C0
+#define MT6370_REG_CHG_MASK1   0x1E0
+#define MT6370_REG_MAXADDR 0x1FF
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+
+#define MT6370_NUM_IRQREGS 16
+#define MT6370_USBC_I2CADDR0x4E
+#define MT6370_MAX_ADDRLEN 2
+
+#define MT6370_VENID_RT50810x8
+#define MT6370_VENID_RT5081A   0xA
+#define MT6370_VENID_MT63700xE
+#define MT6370_VENID_MT63710xF
+#define MT6370_VENID_MT6372P   0x9
+#define MT6370_VENID_MT6372CP  0xB
+
+static const struct regmap_irq mt6370_irqs[] = {
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_DIRCHGON, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TREG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_AICR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_MIVR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_PWR_RDY, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_FL_CHG_VINOVP, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VSYSUV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VSYSOV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VBATOV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VINOVPCHG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_COLD, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_COOL, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_WARM, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_HOT, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_STATC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_FAULT, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_STATC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TMR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_BATABS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_ADPBAD, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_RVP, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TSHUTDOWN, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_IINMEAS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_ICCMEAS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHGDET_DONE, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_WDTMR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_SSFINISH, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_RECHG, 8),
+ 

[PATCH v7 08/13] usb: typec: tcpci_mt6370: Add MediaTek MT6370 tcpci driver

2022-08-05 Thread ChiaEn Wu
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: AngeloGioacchino Del Regno 

Reviewed-by: Guenter Roeck 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---

v7
- Revise 'devm_add_action_or_reset(dev, ...)' to one line
- Revise 'return regmap_update_bits(...)' with using positive
  conditional
---
 drivers/usb/typec/tcpm/Kconfig|  11 ++
 drivers/usb/typec/tcpm/Makefile   |   1 +
 drivers/usb/typec/tcpm/tcpci_mt6370.c | 207 ++
 3 files changed, 219 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm/tcpci_mt6370.c

diff --git a/drivers/usb/typec/tcpm/Kconfig b/drivers/usb/typec/tcpm/Kconfig
index 073fd2e..e6b88ca 100644
--- a/drivers/usb/typec/tcpm/Kconfig
+++ b/drivers/usb/typec/tcpm/Kconfig
@@ -35,6 +35,17 @@ config TYPEC_MT6360
  USB Type-C. It works with Type-C Port Controller Manager
  to provide USB PD and USB Type-C functionalities.
 
+config TYPEC_TCPCI_MT6370
+   tristate "MediaTek MT6370 Type-C driver"
+   depends on MFD_MT6370
+   help
+ MediaTek MT6370 is a multi-functional IC that includes
+ USB Type-C. It works with Type-C Port Controller Manager
+ to provide USB PD and USB Type-C functionalities.
+
+ This driver can also be built as a module. The module
+ will be called "tcpci_mt6370".
+
 config TYPEC_TCPCI_MAXIM
tristate "Maxim TCPCI based Type-C chip driver"
help
diff --git a/drivers/usb/typec/tcpm/Makefile b/drivers/usb/typec/tcpm/Makefile
index 7d499f3..906d9dc 100644
--- a/drivers/usb/typec/tcpm/Makefile
+++ b/drivers/usb/typec/tcpm/Makefile
@@ -6,4 +6,5 @@ typec_wcove-y   := wcove.o
 obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
 obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
 obj-$(CONFIG_TYPEC_MT6360) += tcpci_mt6360.o
+obj-$(CONFIG_TYPEC_TCPCI_MT6370)   += tcpci_mt6370.o
 obj-$(CONFIG_TYPEC_TCPCI_MAXIM)+= tcpci_maxim.o
diff --git a/drivers/usb/typec/tcpm/tcpci_mt6370.c 
b/drivers/usb/typec/tcpm/tcpci_mt6370.c
new file mode 100644
index 000..c5bb201
--- /dev/null
+++ b/drivers/usb/typec/tcpm/tcpci_mt6370.c
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define MT6370_REG_SYSCTRL80x9B
+
+#define MT6370_AUTOIDLE_MASK   BIT(3)
+
+#define MT6370_VENDOR_ID   0x29CF
+#define MT6370_TCPC_DID_A  0x2170
+
+struct mt6370_priv {
+   struct device *dev;
+   struct regulator *vbus;
+   struct tcpci *tcpci;
+   struct tcpci_data tcpci_data;
+};
+
+static const struct reg_sequence mt6370_reg_init[] = {
+   REG_SEQ(0xA0, 0x1, 1000),
+   REG_SEQ(0x81, 0x38, 0),
+   REG_SEQ(0x82, 0x82, 0),
+   REG_SEQ(0xBA, 0xFC, 0),
+   REG_SEQ(0xBB, 0x50, 0),
+   REG_SEQ(0x9E, 0x8F, 0),
+   REG_SEQ(0xA1, 0x5, 0),
+   REG_SEQ(0xA2, 0x4, 0),
+   REG_SEQ(0xA3, 0x4A, 0),
+   REG_SEQ(0xA4, 0x01, 0),
+   REG_SEQ(0x95, 0x01, 0),
+   REG_SEQ(0x80, 0x71, 0),
+   REG_SEQ(0x9B, 0x3A, 1000),
+};
+
+static int mt6370_tcpc_init(struct tcpci *tcpci, struct tcpci_data *data)
+{
+   u16 did;
+   int ret;
+
+   ret = regmap_register_patch(data->regmap, mt6370_reg_init,
+   ARRAY_SIZE(mt6370_reg_init));
+   if (ret)
+   return ret;
+
+   ret = regmap_raw_read(data->regmap, TCPC_BCD_DEV, , sizeof(u16));
+   if (ret)
+   return ret;
+
+   if (did == MT6370_TCPC_DID_A)
+   return regmap_write(data->regmap, TCPC_FAULT_CTRL, 0x80);
+
+   return 0;
+}
+
+static int mt6370_tcpc_set_vconn(struct tcpci *tcpci, struct tcpci_data *data,
+bool enable)
+{
+   return regmap_update_bits(data->regmap, MT6370_REG_SYSCTRL8,
+ MT6370_AUTOIDLE_MASK,
+ enable ? 0 : MT6370_AUTOIDLE_MASK);
+}
+
+static int mt6370_tcpc_set_vbus(struct tcpci *tcpci, struct tcpci_data *data,
+   bool source, bool sink)
+{
+   struct mt6370_priv *priv = container_of(data, struct mt6370_priv,
+   tcpci_data);
+   int ret;
+
+   ret = regulator_is_enabled(priv->vbus);
+   if (ret < 0)
+   return ret;
+
+   if (

[PATCH v7 05/13] dt-bindings: backlight: Add MediaTek MT6370 backlight

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

Add MT6370 backlight binding documentation.

Reviewed-by: Rob Herring 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---

v7
- Add the second compatible string for 'mt6372'
- Add 'mediatek,bled-exponential-mode-enable' property for enable the
  exponential mode of backlight brightness
- Add validation for the maximum value of 'default-brightness' and
  'max-brightness'
---
 .../leds/backlight/mediatek,mt6370-backlight.yaml  | 121 +
 1 file changed, 121 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
new file mode 100644
index 000..5533b65
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
@@ -0,0 +1,121 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: 
http://devicetree.org/schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Backlight
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  The MT6370 Backlight WLED driver supports up to a 29V output voltage for
+  4 channels of 8 series WLEDs. Each channel supports up to 30mA of current
+  capability with 2048 current steps (11 bits, only for MT6370/MT6371) or
+  16384 current steps (14 bits, only for MT6372) in exponential or linear
+  mapping curves.
+
+allOf:
+  - $ref: common.yaml#
+
+properties:
+  compatible:
+enum:
+  - mediatek,mt6370-backlight
+  - mediatek,mt6372-backlight
+
+  default-brightness:
+minimum: 0
+
+  max-brightness:
+minimum: 0
+
+  enable-gpios:
+description: External backlight 'enable' pin
+maxItems: 1
+
+  mediatek,bled-pwm-enable:
+description: |
+  Enable external PWM input for backlight dimming
+type: boolean
+
+  mediatek,bled-pwm-hys-enable:
+description: |
+  Enable the backlight input-hysteresis for PWM mode
+type: boolean
+
+  mediatek,bled-pwm-hys-input-th-steps:
+$ref: /schemas/types.yaml#/definitions/uint8
+enum: [1, 4, 16, 64]
+description: |
+  The selection of the upper and lower bounds threshold of backlight
+  PWM resolution. If we choose selection 64, the variation of PWM
+  resolution needs more than 64 steps.
+
+  mediatek,bled-ovp-shutdown:
+description: |
+  Enable the backlight shutdown when OVP level triggered
+type: boolean
+
+  mediatek,bled-ovp-microvolt:
+enum: [1700, 2100, 2500, 2900]
+description: |
+  Backlight OVP level selection.
+
+  mediatek,bled-ocp-shutdown:
+description: |
+  Enable the backlight shutdown when OCP level triggerred.
+type: boolean
+
+  mediatek,bled-ocp-microamp:
+enum: [90, 120, 150, 180]
+description: |
+  Backlight OC level selection.
+
+  mediatek,bled-exponential-mode-enable:
+description: |
+  Enable the exponential mode of backlight brightness. If this property
+  is not enabled, the default is to use linear mode.
+type: boolean
+
+  mediatek,bled-channel-use:
+$ref: /schemas/types.yaml#/definitions/uint8
+description: |
+  Backlight LED channel to be used.
+  Each bit mapping to:
+- 0: CH4
+- 1: CH3
+- 2: CH2
+- 3: CH1
+minimum: 1
+maximum: 15
+
+if:
+  properties:
+compatible:
+  contains:
+const: mediatek,mt6372-backlight
+
+then:
+  properties:
+default-brightness:
+  maximum: 16384
+
+max-brightness:
+  maximum: 16384
+
+else:
+  properties:
+default-brightness:
+  maximum: 2048
+
+max-brightness:
+  maximum: 2048
+
+required:
+  - compatible
+  - mediatek,bled-channel-use
+
+additionalProperties: false
-- 
2.7.4



[PATCH v7 03/13] dt-bindings: leds: mt6370: Add MediaTek MT6370 current sink type LED indicator

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

Add MediaTek MT6370 current sink type LED indicator binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 .../bindings/leds/mediatek,mt6370-indicator.yaml   | 81 ++
 1 file changed, 81 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
new file mode 100644
index 000..204b103
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-indicator.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 LED driver include 4-channel RGB LED support Register/PWM/Breath 
Mode
+
+allOf:
+  - $ref: leds-class-multicolor.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-indicator
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^multi-led@[0-3]$":
+type: object
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-2]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2]
+
+required:
+  - reg
+  - color
+
+required:
+  - reg
+  - color
+  - "#address-cells"
+  - "#size-cells"
+
+  "^led@[0-3]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+required:
+  - reg
+  - color
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v7 01/13] dt-bindings: usb: Add MediaTek MT6370 TCPC

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

Add MediaTek MT6370 TCPC binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 .../bindings/usb/mediatek,mt6370-tcpc.yaml | 36 ++
 1 file changed, 36 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml 
b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
new file mode 100644
index 000..72f56cc
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/mediatek,mt6370-tcpc.yaml#;
+$schema: "http://devicetree.org/meta-schemas/core.yaml#;
+
+title: MediatTek MT6370 Type-C Port Switch and Power Delivery controller
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  MediaTek MT6370 is a multi-functional device.
+  It integrates charger, ADC, flash, RGB indicators,
+  regulators (DSV/VIBLDO), and TypeC Port Switch with Power Delivery 
controller.
+  This document only describes MT6370 Type-C Port Switch and
+  Power Delivery controller.
+
+properties:
+  compatible:
+enum:
+  - mediatek,mt6370-tcpc
+
+  interrupts:
+maxItems: 1
+
+  connector:
+type: object
+$ref: /schemas/connector/usb-connector.yaml#
+unevaluatedProperties: false
+
+additionalProperties: false
+
+required:
+  - compatible
+  - interrupts
-- 
2.7.4



[PATCH v7 06/13] dt-bindings: mfd: Add MediaTek MT6370

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

Add MediaTek MT6370 binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---
 .../devicetree/bindings/mfd/mediatek,mt6370.yaml   | 280 +
 include/dt-bindings/iio/adc/mediatek,mt6370_adc.h  |  18 ++
 2 files changed, 298 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
 create mode 100644 include/dt-bindings/iio/adc/mediatek,mt6370_adc.h

diff --git a/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml 
b/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
new file mode 100644
index 000..410e2d4
--- /dev/null
+++ b/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
@@ -0,0 +1,280 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/mediatek,mt6370.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 SubPMIC
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  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.
+
+properties:
+  compatible:
+const: mediatek,mt6370
+
+  reg:
+maxItems: 1
+
+  wakeup-source: true
+
+  interrupts:
+maxItems: 1
+
+  interrupt-controller: true
+
+  "#interrupt-cells":
+const: 1
+
+  adc:
+type: object
+description: |
+  Provides 9 channels for system monitoring, including VBUSDIV5 (lower
+  accuracy, higher measure range), VBUSDIV2 (higher accuracy, lower
+  measure range), VBAT, VSYS, CHG_VDDP, TS_BAT, IBUS, IBAT, and TEMP_JC.
+
+properties:
+  compatible:
+const: mediatek,mt6370-adc
+
+  "#io-channel-cells":
+const: 1
+
+required:
+  - compatible
+  - "#io-channel-cells"
+
+  backlight:
+type: object
+$ref: /schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+
+  charger:
+type: object
+$ref: /schemas/power/supply/mediatek,mt6370-charger.yaml#
+
+  tcpc:
+type: object
+$ref: /schemas/usb/mediatek,mt6370-tcpc.yaml#
+
+  indicator:
+type: object
+$ref: /schemas/leds/mediatek,mt6370-indicator.yaml#
+
+  flashlight:
+type: object
+$ref: /schemas/leds/mediatek,mt6370-flashlight.yaml#
+
+  regulators:
+type: object
+description: |
+  List all supported regulators, which support the control for DisplayBias
+  voltages and one general purpose LDO which commonly used to drive the
+  vibrator.
+
+patternProperties:
+  "^(dsvbst|vibldo)$":
+$ref: /schemas/regulator/regulator.yaml#
+type: object
+unevaluatedProperties: false
+
+  "^(dsvpos|dsvneg)$":
+$ref: /schemas/regulator/regulator.yaml#
+type: object
+unevaluatedProperties: false
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - interrupt-controller
+  - "#interrupt-cells"
+  - regulators
+  - adc
+  - backlight
+  - indicator
+  - tcpc
+  - charger
+  - flashlight
+
+additionalProperties: false
+
+examples:
+  - |
+#include 
+#include 
+#include 
+#include 
+i2c {
+  #address-cells = <1>;
+  #size-cells = <0>;
+
+pmic@34 {
+compatible = "mediatek,mt6370";
+reg = <0x34>;
+wakeup-source;
+interrupts-extended = < 3 IRQ_TYPE_LEVEL_LOW>;
+interrupt-controller;
+#interrupt-cells = <1>;
+
+mt6370_adc: adc {
+  compatible = "mediatek,mt6370-adc";
+  #io-channel-cells = <1>;
+};
+
+backlight {
+  compatible = "mediatek,mt6370-backlight";
+  mediatek,bled-channel-use = /bits/ 8 <15>;
+};
+
+charger {
+  compatible = "mediatek,mt6370-charger";
+  interrupts = <48>, <68>, <6>;
+  interrupt-names = "attach_i", "uvp_d_evt", "mivr";
+  io-channels = <_adc MT6370_CHAN_IBUS>;
+
+  mt6370_otg_vbus: usb-otg-vbus-regulator {
+regulator-name = "mt6370-usb-otg-vbus";
+regulator-min-microvolt = <435>;
+regulator-max-microvolt = <580>;
+regulator-min-microamp = <50>;
+regulator-max-microamp = <300>;
+  };
+};
+
+indicator {
+  compatible = "mediatek,mt6370-indicator";
+  #address-cells = <1>;
+  #size-cells = <0>;
+
+  multi-led@0 {
+r

[PATCH v7 02/13] dt-bindings: power: supply: Add MediaTek MT6370 Charger

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

Add MediaTek MT6370 Charger binding documentation.

Reviewed-by: Krzysztof Kozlowski 
Signed-off-by: ChiaEn Wu 
---
 .../power/supply/mediatek,mt6370-charger.yaml  | 88 ++
 1 file changed, 88 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml

diff --git 
a/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
new file mode 100644
index 000..bd09a0a
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/mediatek,mt6370-charger.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Battery Charger
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Provides Battery Charger, Boost for OTG devices and BC1.2 detection.
+
+properties:
+  compatible:
+const: mediatek,mt6370-charger
+
+  interrupts:
+description: |
+  Specify what irqs are needed to be handled by MT6370 Charger driver. IRQ
+  "MT6370_IRQ_CHG_MIVR", "MT6370_IRQ_ATTACH" and "MT6370_IRQ_OVPCTRL_UVP_D"
+  are required.
+items:
+  - description: BC1.2 done irq
+  - description: usb plug in irq
+  - description: mivr irq
+
+  interrupt-names:
+items:
+  - const: attach_i
+  - const: uvp_d_evt
+  - const: mivr
+
+  io-channels:
+description: |
+  Use ADC channel to read VBUS, IBUS, IBAT, etc., info.
+minItems: 1
+items:
+  - description: |
+  VBUS voltage with lower accuracy (+-75mV) but higher measure
+  range (1~22V)
+  - description: |
+  VBUS voltage with higher accuracy (+-30mV) but lower measure
+  range (1~9.76V)
+  - description: the main system input voltage
+  - description: battery voltage
+  - description: battery temperature-sense input voltage
+  - description: IBUS current (required)
+  - description: battery current
+  - description: |
+  regulated output voltage to supply for the PWM low-side gate driver
+  and the bootstrap capacitor
+  - description: IC junction temperature
+
+  io-channel-names:
+minItems: 1
+items:
+  - const: vbusdiv5
+  - const: vbusdiv2
+  - const: vsys
+  - const: vbat
+  - const: ts_bat
+  - const: ibus
+  - const: ibat
+  - const: chg_vddp
+  - const: temp_jc
+
+  usb-otg-vbus-regulator:
+type: object
+description: OTG boost regulator.
+unevaluatedProperties: false
+$ref: /schemas/regulator/regulator.yaml#
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - interrupts
+  - interrupt-names
+  - io-channels
+
+additionalProperties: false
+
+...
-- 
2.7.4



[PATCH v7 00/13] Add MediaTek MT6370 PMIC support

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

This patch series add MediaTek MT6370 PMIC support. The 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.

In this series of patches, we based on Andy Shevchenko's mfd patch which
is used to adjust the Makefile order.
(https://lore.kernel.org/all/20220801114211.36267-3-andriy.shevche...@linux.intel.com/)
Among this, we also took some changes for MT6370 drivers, revised the
MT6370 device tree files to comply with DT specifications, and revised
the wrong SoB chain in entire patches.

PS.
"[PATCH v7 06/13] dt-bindings: mfd: Add MediaTek MT6370" depends on previous
DT binding patches, so before applying this patch, please apply other DT
patches first. Thanks!

PS2.
Our MFD DT-binding depends on LED flash and LED RGB DT-bindings, but the
Kconfigs of LED flash and LED RGB depend on MFD. Due to dependency
consideration, we also submit LED flash and LED RGB with other patches
at this time.


Thank you,
ChiaEn Wu
---
Changes in v7:
- In Patch 05/13:
- Add the second compatible string for 'mt6372'
- Add 'mediatek,bled-exponential-mode-enable' property for enable the
  exponential mode of backlight brightness
- Add validation for the maximum value of 'default-brightness' and
  'max-brightness'

- In Patch 07/13:
- Move '#define MT6370_REG_MAXADDR' to the next line of
  '#define MT6370_REG_CHG_MASK1'
- Rename 'MT6370_REG_ADDRLEN' to 'MT6370_MAX_ADDRLEN'

- In Patch 08/13:
- Revise 'devm_add_action_or_reset(dev, ...)' to one line
- Revise 'return regmap_update_bits(...)' with using positive
  conditional

- In Patch 09/13:
- 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

- In Patch 10/13:
- Revise the method to enable/disable irq
- Revise all 'if (ret < 0)' to 'if (ret)' after using
  mt6370_chg_field_set/get()
- Revise all 'OTG' text

- In Patch 11/13:
- Add the comment for the union of 'struct mt6370_led'
- Revise the wrong description of 'Authors'
- Revise some typos (e.g. led --> LED)
- Revise 'if (!fwnode_property_read_string())' to
  'ret = fwnode_property_read_string()'
- Replace 'memcpy(...)' with 'put_unaligned_be24()' in
  mt6370_gen_breath_pattern()
- Replace all 'LED_OFF' with 0
- Remove the redundant assignment in mt6370_mc_pattern_clear()

- In Patch 12/13:
- Fix the indentation.
- For the well defined macro, the parenthesis is needed for input 
parameters.
- Replace some dev_warn to dev_info in 'init_flash_properties'.
- Add sentinel comment for the terminator entry of of_device_id.
- Use priv->fled_torch_used directly.
- Delete 0 in {}.
- Use _uA instead of _UA in definition.
- Refine the description.
- Use usleep_range instead of udelay.
- Rename config to LEDS_MT6370_FLASH.
- Add missing ">" in copyright.
- Change the Kconfig order

- In Patch 13/13:
- Add support 'exponential mode' property parsing
- Add 'return dev_err_probe()' after 'if (IS_ERR(priv->enable_gpio))'
- Add 'mt6372' compatible string
- Revise Kconfig help text
- Revise update()/get() for supporting 16384 steps (MT6372)
- Revise all shift usages form using 'ffs() and fls()' to defining the
  _SHIFT macros.
- Revise 'brightness ? 1 : 0' to '!!brightness' in gpiod_set_value()


Changes in v6:
- In Patch 03/13:
- Add 'reg' property of led of multi-led to prevent checking
  error.

- In Patch 08/13:
- Convert tcpci as device resource managed with
  'devm_add_action_or_reset' api.
- Refine remvoe callback.
- Refine the commit text from 'this commit add' to 'add'.

- In Patch 09/13:
- Using 'struct device *dev = >dev' in probe()
- Revise the sixth parameter of regmap_read_poll_timeout() by
  Replacing '1000' with 'MILLI'
- Revise the units of three macros
- MT6370_AICR_400MA --> MT6370_AICR_400_mA
- MT6370_ICHG_500MA --> MT6370_ICHG_500_mA
- MT6370_ICHG_900MA --> MT6370_ICHG_900_mA

- In patch 10/13:
- Remove the varable (*psy_desc) of struct mt6370_priv
- Remove the deprecated usb type (POWER_SUPPLY_TYPE_USB_CDP and
  POWER_SUPPLY_TYPE_USB_DCP)
- Remove useless remove()
- Revise all units from mini- to micro-
- Revise get/set p

[PATCH v7 11/13] leds: rgb: mt6370: Add MediaTek MT6370 current sink type LED Indicator support

2022-08-05 Thread ChiaEn Wu
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 MediaTek MT6370 Current Sink Type LED Indicator
driver. It can control four channels current-sink RGB LEDs with 3 modes,
constant current, PWM, and breath mode.

Reviewed-by: AngeloGioacchino Del Regno 

Co-developed-by: Alice Chen 
Signed-off-by: Alice Chen 
Signed-off-by: ChiYuan Huang 
Signed-off-by: ChiaEn Wu 
---

v7
- Add the comment for the union of 'struct mt6370_led'
- Revise the wrong description of 'Authors'
- Revise some typos (e.g. led --> LED)
- Revise 'if (!fwnode_property_read_string())' to
  'ret = fwnode_property_read_string()'
- Replace 'memcpy(...)' with 'put_unaligned_be24()' in
  mt6370_gen_breath_pattern()
- Replace all 'LED_OFF' with 0
- Remove the redundant assignment in mt6370_mc_pattern_clear()
---
 drivers/leds/rgb/Kconfig   |   13 +
 drivers/leds/rgb/Makefile  |1 +
 drivers/leds/rgb/leds-mt6370-rgb.c | 1022 
 3 files changed, 1036 insertions(+)
 create mode 100644 drivers/leds/rgb/leds-mt6370-rgb.c

diff --git a/drivers/leds/rgb/Kconfig b/drivers/leds/rgb/Kconfig
index 204cf47..7d86bb2 100644
--- a/drivers/leds/rgb/Kconfig
+++ b/drivers/leds/rgb/Kconfig
@@ -26,4 +26,17 @@ config LEDS_QCOM_LPG
 
  If compiled as a module, the module will be named leds-qcom-lpg.
 
+config LEDS_MT6370_RGB
+   tristate "LED Support for MediaTek MT6370 PMIC"
+   depends on MFD_MT6370
+   select LINEAR_RANGE
+   help
+ Say Y here to enable support for MT6370_RGB LED device.
+ In MT6370, there are four channel current-sink LED drivers that
+ support hardware pattern for constant current, PWM, and breath mode.
+ Isink4 channel can also be used as a CHG_VIN power good indicator.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-rgb".
+
 endif # LEDS_CLASS_MULTICOLOR
diff --git a/drivers/leds/rgb/Makefile b/drivers/leds/rgb/Makefile
index 0675bc0..8c01daf 100644
--- a/drivers/leds/rgb/Makefile
+++ b/drivers/leds/rgb/Makefile
@@ -2,3 +2,4 @@
 
 obj-$(CONFIG_LEDS_PWM_MULTICOLOR)  += leds-pwm-multicolor.o
 obj-$(CONFIG_LEDS_QCOM_LPG)+= leds-qcom-lpg.o
+obj-$(CONFIG_LEDS_MT6370_RGB)  += leds-mt6370-rgb.o
diff --git a/drivers/leds/rgb/leds-mt6370-rgb.c 
b/drivers/leds/rgb/leds-mt6370-rgb.c
new file mode 100644
index 000..42cc13a
--- /dev/null
+++ b/drivers/leds/rgb/leds-mt6370-rgb.c
@@ -0,0 +1,1022 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Authors:
+ *   ChiYuan Huang 
+ *   Alice Chen 
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_ISNK1 = 0,
+   MT6370_LED_ISNK2,
+   MT6370_LED_ISNK3,
+   MT6370_LED_ISNK4,
+   MT6370_MAX_LEDS
+};
+
+enum mt6370_led_mode {
+   MT6370_LED_PWM_MODE = 0,
+   MT6370_LED_BREATH_MODE,
+   MT6370_LED_REG_MODE,
+   MT6370_LED_MAX_MODE
+};
+
+enum mt6370_led_field {
+   F_RGB_EN = 0,
+   F_CHGIND_EN,
+   F_LED1_CURR,
+   F_LED2_CURR,
+   F_LED3_CURR,
+   F_LED4_CURR,
+   F_LED1_MODE,
+   F_LED2_MODE,
+   F_LED3_MODE,
+   F_LED4_MODE,
+   F_LED1_DUTY,
+   F_LED2_DUTY,
+   F_LED3_DUTY,
+   F_LED4_DUTY,
+   F_LED1_FREQ,
+   F_LED2_FREQ,
+   F_LED3_FREQ,
+   F_LED4_FREQ,
+   F_MAX_FIELDS
+};
+
+enum mt6370_led_ranges {
+   R_LED123_CURR = 0,
+   R_LED4_CURR,
+   R_LED_TRFON,
+   R_LED_TOFF,
+   R_MAX_RANGES
+};
+
+enum mt6370_pattern {
+   P_LED_TR1 = 0,
+   P_LED_TR2,
+   P_LED_TF1,
+   P_LED_TF2,
+   P_LED_TON,
+   P_LED_TOFF,
+   P_MAX_PATTERNS
+};
+
+enum mt6370_state {
+   STATE_OFF = 0,
+   STATE_KEEP,
+   STATE_ON,
+   STATE_MAX
+};
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_RGB1_DIM0x182
+#define MT6370_REG_RGB2_DIM0x183
+#define MT6370_REG_RGB3_DIM0x184
+#define MT6370_REG_RGB_EN  0x185
+#define MT6370_REG_RGB1_ISNK   0x186
+#define MT6370_REG_RGB2_ISNK   0x187
+#define MT6370_REG_RGB3_ISNK   0x188
+#define MT6370_REG_RGB1_TR 0x189
+#define MT6370_REG_RGB_CHRIND_DIM  0x192
+#define MT6370_REG_RGB_CHRIND_CTRL 0x193
+#define MT6370_REG_RGB_CHRIND_TR   0x194
+
+#define MT6372_REG_RGB_EN  0x182
+#

[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);
+

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

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 MediaTek MT6370 Charger driver. 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 
---

v7
- Revise the method to enable/disable irq
- Revise all 'if (ret < 0)' to 'if (ret)' after using
  mt6370_chg_field_set/get()
- Revise all 'OTG' text
---
 drivers/power/supply/Kconfig  |  14 +
 drivers/power/supply/Makefile |   1 +
 drivers/power/supply/mt6370-charger.c | 965 ++
 3 files changed, 980 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..42fddc3
--- /dev/null
+++ b/drivers/power/supply/mt6370-charger.c
@@ -0,0 +1,965 @@
+// 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,
+   /* MT6370_REG_USB_STATUS1 */
+   F_USB_STAT, F_CHGDET,
+   /* MT6370_REG_CHG_STAT */
+   F_CHG_STAT, F_BOOST_STAT, F_VBAT_LVL,
+   /* MT63

[PATCH v7 13/13] video: backlight: 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 MediaTek MT6370 backlight driver.
It controls 4 channels of 8 series WLEDs in
2048 (only for MT6370/MT6371) / 16384 (only for MT6372)
current steps (30 mA) in exponential or linear mapping curves.

Signed-off-by: ChiaEn Wu 
---

v7
- Add support 'exponential mode' property parsing
- Add 'return dev_err_probe()' after 'if (IS_ERR(priv->enable_gpio))'
- Add 'mt6372' compatible string
- Revise Kconfig help text
- Revise update()/get() for supporting 16384 steps (MT6372)
- Revise all shift usages form using 'ffs() and fls()' to defining the
  _SHIFT macros.
- Revise 'brightness ? 1 : 0' to '!!brightness' in gpiod_set_value()
---
 drivers/video/backlight/Kconfig|  13 ++
 drivers/video/backlight/Makefile   |   1 +
 drivers/video/backlight/mt6370-backlight.c | 351 +
 3 files changed, 365 insertions(+)
 create mode 100644 drivers/video/backlight/mt6370-backlight.c

diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig
index a003e02..936ba1e 100644
--- a/drivers/video/backlight/Kconfig
+++ b/drivers/video/backlight/Kconfig
@@ -268,6 +268,19 @@ config BACKLIGHT_MAX8925
  If you have a LCD backlight connected to the WLED output of MAX8925
  WLED output, say Y here to enable this driver.
 
+config BACKLIGHT_MT6370
+   tristate "MediaTek MT6370 Backlight Driver"
+   depends on MFD_MT6370
+   help
+ This enables support for Mediatek MT6370 Backlight driver.
+ It's commonly used to drive the display WLED. There are 4 channels
+ inside, and each channel supports up to 30mA of current capability
+ with 2048 current steps (only for MT6370/MT6371) or 16384 current
+ steps (only for MT6372) in exponential or linear mapping curves.
+
+ This driver can also be built as a module. If so, the module
+ will be called "mt6370-backlight".
+
 config BACKLIGHT_APPLE
tristate "Apple Backlight Driver"
depends on X86 && ACPI
diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile
index cae2c83..e815f3f 100644
--- a/drivers/video/backlight/Makefile
+++ b/drivers/video/backlight/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_BACKLIGHT_LP855X)+= lp855x_bl.o
 obj-$(CONFIG_BACKLIGHT_LP8788) += lp8788_bl.o
 obj-$(CONFIG_BACKLIGHT_LV5207LP)   += lv5207lp.o
 obj-$(CONFIG_BACKLIGHT_MAX8925)+= max8925_bl.o
+obj-$(CONFIG_BACKLIGHT_MT6370) += mt6370-backlight.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)  += omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PANDORA)+= pandora_bl.o
 obj-$(CONFIG_BACKLIGHT_PCF50633)   += pcf50633-backlight.o
diff --git a/drivers/video/backlight/mt6370-backlight.c 
b/drivers/video/backlight/mt6370-backlight.c
new file mode 100644
index 000..4c889b4
--- /dev/null
+++ b/drivers/video/backlight/mt6370-backlight.c
@@ -0,0 +1,351 @@
+// 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 
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_BL_EN   0x1A0
+#define MT6370_REG_BL_BSTCTRL  0x1A1
+#define MT6370_REG_BL_PWM  0x1A2
+#define MT6370_REG_BL_DIM2 0x1A4
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+#define MT6370_BL_EXT_EN_MASK  BIT(7)
+#define MT6370_BL_EN_MASK  BIT(6)
+#define MT6370_BL_CODE_MASKBIT(0)
+#define MT6370_BL_CH_MASK  GENMASK(5, 2)
+#define MT6370_BL_CH_SHIFT 2
+#define MT6370_BL_DIM2_COMMON_MASK GENMASK(2, 0)
+#define MT6370_BL_DIM2_COMMON_SHIFT3
+#define MT6370_BL_DIM2_6372_MASK   GENMASK(5, 0)
+#define MT6370_BL_DIM2_6372_SHIFT  6
+#define MT6370_BL_PWM_EN_MASK  BIT(7)
+#define MT6370_BL_PWM_HYS_EN_MASK  BIT(2)
+#define MT6370_BL_PWM_HYS_SEL_MASK GENMASK(1, 0)
+#define MT6370_BL_OVP_EN_MASK  BIT(7)
+#define MT6370_BL_OVP_SEL_MASK GENMASK(6, 5)
+#define MT6370_BL_OVP_SEL_SHIFT5
+#define MT6370_BL_OC_EN_MASK   BIT(3)
+#define MT6370_BL_OC_SEL_MASK  GENMASK(2, 1)
+#define MT6370_BL_OC_SEL_SHIFT 1
+
+#define MT6370_BL_PWM_HYS_TH_MIN_STEP  1
+#define MT6370_BL_PWM_HYS_TH_MAX_STEP  64
+#define MT6370_BL_OVP_MIN_UV   1700
+#define MT6370_BL_OVP_MAX_UV   2900
+#define MT6370_BL_OVP_STEP_UV  400
+#define MT6370_BL_OCP_MIN_UA   90
+#define MT

Re: [PATCH v6 11/13] leds: rgb: mt6370: Add MediaTek MT6370 current sink type LED Indicator support

2022-07-27 Thread ChiaEn Wu
On Tue, Jul 26, 2022 at 8:18 PM Andy Shevchenko
 wrote:

...

> > Just for saving memory space.
> > Because these led_classdevs do not be used at the same time.
> > Or do you think it would be better to rewrite it as follows?
> > -
> > struct mt6370_led {
> >struct led_classdev isink;
> >struct led_classdev_mc mc;
> >struct mt6370_priv *priv;
> >u32 default_state;
> >u32 index;
> > };
> > -
>
> You obviously didn't get what I'm talking about...
> Each union to work properly should have an associated variable that
> holds the information of which field of the union is in use. Do you
> have such a variable? If not, how does your code know which one to
> use? If yes, add a proper comment there.
>

Ummm... from my understanding,
if the colors of these four LEDs are set to 'LED_COLOR_ID_RGB' or
'LED_COLOR_ID_MULTI' in DT,
their 'led->index' will be set to 'MT6370_VIRTUAL_MULTICOLOR' in
'mt6370_leds_probe()'.
If so, these led devices will be set as 'struct led_classdev_mc' and
use related ops functions in 'mt6370_init_led_properties()'.
Instead, they whose 'led->index' is not 'MT6370_VIRTUAL_MULTICOLOR'
will be set as 'struct led_classdev'.
So, maybe the member 'index' of the 'struct mt6370_led' is what you
describe the information of which field of the union is in use?
I will add the proper comment here to describe this thing. I'm so
sorry for misunderstanding your mean last time.
Thanks again for your review.

-- 
Best Regards,
ChiaEn Wu


Re: [PATCH v6 13/13] video: backlight: mt6370: Add MediaTek MT6370 support

2022-07-27 Thread ChiaEn Wu
t,
> > maybe I can just modify the maximum value of default-brightness and
> > max-brightness in DT to 16384,
> > modify the description and add some comments.
>
> What for?
>
> All the other backlight drivers (there are >130 of them) expose the hardware
> range[1]. Most userspaces will already know how to handle that (by reading
> the max_brightness and, if it is recent enough, also the scale
> properties in sysfs).
>
> I'm still don't understand why one should fix a bug in the userspace by
> implementing a hack in the driver.
>
>
> [1] Well almost. The PWM backlight driver does contain support for
> dead-spot avoidance and to allow the adoption of exponential scale.
> However this  purpose of these is based on how PWM backlights work
>
> > And then on the driver side,
> > we can use mt6370_check_vendor_info() to determine whether it is MT6372.
> > If no, then in mt6370_bl_update_status(), first brightness_val / 8 and then 
> > set.
> > In mt6370_bl_get_brightness(), first brightness_val * 8 and then return;
> >
> > If I do this change, does this meet your requirements?
>
> Not really.
>
> It's still misleading a sophisticated userspace, which may make bad
> rounding decisions for backlight animation, in order to support a broken
> one.
>
>
> > > > Or, for the reasons, I have just explained (just one customer has this
> > > > requirement), then we do not make any changes for compatibility
> > > > reasons?
> > >
> > > I'd be curious what the compatiblity reasons are. In other words what
> > > software breaks?
> >
> > The reason is as above. We just hope the users who use this series SubPMIC 
> > can
> > directly apply this driver to drive the backlight device without
> > knowing the underlying hardware.
> > Not software breaks.
>
> As above, ignoring the max_brightness property is a bug in the
> userspace. I'm still unclear why sending faked ranges to userspace
> it a better solution than fixing the userspace.

Ok, I got it!
If I add a second compatible string (like 'mediatek,mt6372-backlight')
in the DT section,
and append 'if-then-else' to determine the correct maximum value of
'default-brightness' and 'max-brightness',
Also, I will append 'bled exponential mode' to make user control using
linear or exponential mode.
These changes I will explain to DT's maintainer again.

Back to the driver section,
do I still need to use the register to confirm again whether this
SubPMIC used now is MT6372 and record this information? (using
'mt6370_check_vendor_info()')
I am afraid that the user who uses the MT6370 hardware, but the DT
compatible string is set to 'mediatek,mt6372-backlight'.
This may cause errors when update/get brightness values.
So I hope the driver here can check again to make sure the
'default-brightness', 'max-brightness', can be updated/got correctly.
I don't know if this will make you feel redundant if I do this??

Thank you so much!

-- 
Best Regards,
ChiaEn Wu


Re: [PATCH v6 11/13] leds: rgb: mt6370: Add MediaTek MT6370 current sink type LED Indicator support

2022-07-26 Thread ChiaEn Wu
On Mon, Jul 25, 2022 at 4:41 PM Andy Shevchenko
 wrote:
...
> > From: ChiYuan Huang 
>
>  (Note this and read below)

...

> In conjunction with above what SoB of Alice means?
>
> You really need to take your time and (re-)read
> https://www.kernel.org/doc/html/latest/process/submitting-patches.html.

Hi Andy,

Thanks for your reply.
We are very sorry for this mistake. We will revise it in the next patch.

>
> ...
>
> > + * Author: Alice Chen 
> > + * Author: ChiYuan Huang 
>
> Would
>  * Authors:
>  *Name_of_Author 1
>  *Name_of_Author 2
>
> work for you?

It looks good, thanks! We will apply this in the next patch.

 ...

> > +struct mt6370_led {
> > +   union {
> > +   struct led_classdev isink;
> > +   struct led_classdev_mc mc;
> > +   };
>
> Where is the field that makes union work?

Just for saving memory space.
Because these led_classdevs do not be used at the same time.
Or do you think it would be better to rewrite it as follows?
-
struct mt6370_led {
   struct led_classdev isink;
   struct led_classdev_mc mc;
   struct mt6370_priv *priv;
   u32 default_state;
   u32 index;
};
-

...

> > +static int mt6370_gen_breath_pattern(struct mt6370_priv *priv,
> > +struct led_pattern *pattern, u32 len,
> > +u8 *pattern_val, u32 val_len)
> > +{
> > +   enum mt6370_led_ranges sel_range;
> > +   struct led_pattern *curr;
> > +   unsigned int sel;
> > +   u8 val[P_MAX_PATTERNS / 2] = {};
> > +   int i;
> > +
> > +   if (len < P_MAX_PATTERNS && val_len < P_MAX_PATTERNS / 2)
> > +   return -EINVAL;
> > +
> > +   /*
> > +* Pattern list
> > +* tr1: byte 0, b'[7: 4]
> > +* tr2: byte 0, b'[3: 0]
> > +* tf1: byte 1, b'[7: 4]
> > +* tf2: byte 1, b'[3: 0]
> > +* ton: byte 2, b'[7: 4]
> > +* toff: byte 2, b'[3: 0]
> > +*/
> > +   for (i = 0; i < P_MAX_PATTERNS; i++) {
> > +   curr = pattern + i;
> > +
> > +   sel_range = i == P_LED_TOFF ? R_LED_TOFF : R_LED_TRFON;
> > +
> > +   linear_range_get_selector_within(priv->ranges + sel_range,
> > +curr->delta_t, );
> > +
> > +   val[i / 2] |= sel << (4 * ((i + 1) % 2));
> > +   }
> > +
> > +   memcpy(pattern_val, val, 3);
>
> Isn't it something like put_unaligned_be24()/put_unaligned_le24()?

OK, we will try to apply this method in the next patch.
Thank you so much for reviewing our patches so many times and
providing so many great suggestions!

-- 
Best Regards,
ChiaEn Wu


Re: [PATCH v6 13/13] video: backlight: mt6370: Add MediaTek MT6370 support

2022-07-26 Thread ChiaEn Wu
On Tue, Jul 26, 2022 at 5:31 PM Daniel Thompson
 wrote:
...
> > > Does the MT6372 support more steps than this? In other words does it use
> > > a fourteen bit scale or does it use an 11-bit scale at a different
> > > register location?
> >
> > Hi Daniel,
> >
> > Thanks for your reply.
> > Yes, MT6372 can support 16384 steps and uses a 14-bit scale register
> > location. But the maximum current of each
> > channel of MT6372 is the same as MT6370 and MT6371, both 30mA.
> > The main reason why MT6372 is designed this way is that one of the
> > customers asked for a more delicate
> > adjustment of the backlight brightness. But other customers actually
> > do not have such requirements.
> > Therefore, we designed it this way for maximum compatibility in software.

Sorry for I used of the wrong word, I mean is 'driver', not
higher-level software

>
> I don't think that is an acceptable approach for the upstream kernel.
>
> To be "compatible" with (broken) software this driver ends up reducing
> the capability of the upstream kernel to the point it becomes unable to
> meet requirements for delicate adjustment (requirements that were
> sufficiently important to change the hardware design so you could meet
> them).

Originally, we just wanted to use one version of the driver to cover
all the SubPMIC of the 6370 series(6370~6372).
And, the users who use this series SubPMIC can directly apply this
driver to drive the backlight device without knowing the underlying
hardware.
To achieve this goal, we have designed it to look like this.

>
>
...
> > > > +
> > > > + if (brightness) {
> > > > + brightness_val[0] = (brightness - 1) & 
> > > > MT6370_BL_DIM2_MASK;
> > > > + brightness_val[1] = (brightness - 1) >> 
> > > > fls(MT6370_BL_DIM2_MASK);
> > > > +
> > > > + /*
> > > > +  * To make MT6372 using 14 bits to control the brightness
> > > > +  * backward compatible with 11 bits brightness control
> > > > +  * (like MT6370 and MT6371 do), we left shift the value
> > > > +  * and pad with 1 to remaining bits. Hence, the MT6372's
> > > > +  * backlight brightness will be almost the same as 
> > > > MT6370's
> > > > +  * and MT6371's.
> > > > +  */
> > > > + if (priv->vid_type == MT6370_VID_6372) {
> > > > + brightness_val[0] <<= MT6370_BL_DIM2_6372_SHIFT;
> > > > + brightness_val[0] |= MT6370_BL_DUMMY_6372_MASK;
> > > > + }
> > >
> > > This somewhat depends on the answer to the first question above, but
> > > what is the point of this shifting? If the range is 14-bit then the
> > > driver should set max_brightness to 16384 and present the full range of
> > > the MT6372 to the user.
> >
> > So should we make all 16384 steps of MT6372 available to users?
>
> Yes.
>
>
> > Does that mean the DTS needs to be modified as well?
>
> Yes... the property to set initial brightness needs a 14-bit range.
>
> It would also be a good idea to discuss with the DT maintainers whether
> you should introduce a second compatible string (ending 6372) in order
> to allow the DT validation checks to detect accidental use of MT6372
> ranges on MT6370 hardware.

hmmm... I have just thought about it,
maybe I can just modify the maximum value of default-brightness and
max-brightness in DT to 16384,
modify the description and add some comments.

And then on the driver side,
we can use mt6370_check_vendor_info( ) to determine whether it is MT6372.
If no, then in mt6370_bl_update_status(), first brightness_val / 8 and then set.
In mt6370_bl_get_brightness(), first brightness_val * 8 and then return;

If I do this change, does this meet your requirements?

>
>
> > Or, for the reasons, I have just explained (just one customer has this
> > requirement), then we do not make any changes for compatibility
> > reasons?
>
> I'd be curious what the compatiblity reasons are. In other words what
> software breaks?

The reason is as above. We just hope the users who use this series SubPMIC can
directly apply this driver to drive the backlight device without
knowing the underlying hardware.
Not software breaks.

Thanks!

>
> Normally the userspace backlight code reads the max_brightness property
> and configures things accordingly (and therefore if you the component
> that breaks is something like an Android HAL then fix the HAL instead).
>
>
> Daniel.

-- 
Best Regards,
ChiaEn Wu


Re: [PATCH v6 13/13] video: backlight: mt6370: Add MediaTek MT6370 support

2022-07-25 Thread ChiaEn Wu
On Mon, Jul 25, 2022 at 6:31 PM Daniel Thompson
 wrote:
>
> On Fri, Jul 22, 2022 at 06:24:07PM +0800, ChiaEn Wu wrote:
> > diff --git a/drivers/video/backlight/Kconfig 
> > b/drivers/video/backlight/Kconfig
> > index a003e02..846dbe7 100644
> > --- a/drivers/video/backlight/Kconfig
> > +++ b/drivers/video/backlight/Kconfig
> > @@ -268,6 +268,18 @@ config BACKLIGHT_MAX8925
> > If you have a LCD backlight connected to the WLED output of MAX8925
> > WLED output, say Y here to enable this driver.
> >
> > +config BACKLIGHT_MT6370
> > + tristate "MediaTek MT6370 Backlight Driver"
> > + depends on MFD_MT6370
> > + help
> > +   This enables support for Mediatek MT6370 Backlight driver.
> > +   It's commonly used to drive the display WLED. There are 4 channels
> > +   inside, and each channel supports up to 30mA of current capability
> > +   with 2048 current steps in exponential or linear mapping curves.
>
> Does the MT6372 support more steps than this? In other words does it use
> a fourteen bit scale or does it use an 11-bit scale at a different
> register location?

Hi Daniel,

Thanks for your reply.
Yes, MT6372 can support 16384 steps and uses a 14-bit scale register
location. But the maximum current of each
channel of MT6372 is the same as MT6370 and MT6371, both 30mA.
The main reason why MT6372 is designed this way is that one of the
customers asked for a more delicate
adjustment of the backlight brightness. But other customers actually
do not have such requirements.
Therefore, we designed it this way for maximum compatibility in software.

>
>
> > +
> > +   This driver can also be built as a module. If so, the module
> > +   will be called "mt6370-backlight".
> > +
> > [...]
> > diff --git a/drivers/video/backlight/mt6370-backlight.c 
> > b/drivers/video/backlight/mt6370-backlight.c
> > new file mode 100644
> > index 000..ba00a8f
> > --- /dev/null
> > +++ b/drivers/video/backlight/mt6370-backlight.c
> > [...]
> > +static int mt6370_bl_update_status(struct backlight_device *bl_dev)
> > +{
> > + struct mt6370_priv *priv = bl_get_data(bl_dev);
> > + int brightness = backlight_get_brightness(bl_dev);
> > + unsigned int enable_val;
> > + u8 brightness_val[2];
> > + int ret;
> > +
> > + if (brightness) {
> > + brightness_val[0] = (brightness - 1) & MT6370_BL_DIM2_MASK;
> > + brightness_val[1] = (brightness - 1) >> 
> > fls(MT6370_BL_DIM2_MASK);
> > +
> > + /*
> > +  * To make MT6372 using 14 bits to control the brightness
> > +  * backward compatible with 11 bits brightness control
> > +  * (like MT6370 and MT6371 do), we left shift the value
> > +  * and pad with 1 to remaining bits. Hence, the MT6372's
> > +  * backlight brightness will be almost the same as MT6370's
> > +  * and MT6371's.
> > +  */
> > + if (priv->vid_type == MT6370_VID_6372) {
> > + brightness_val[0] <<= MT6370_BL_DIM2_6372_SHIFT;
> > + brightness_val[0] |= MT6370_BL_DUMMY_6372_MASK;
> > + }
>
> This somewhat depends on the answer to the first question above, but
> what is the point of this shifting? If the range is 14-bit then the
> driver should set max_brightness to 16384 and present the full range of
> the MT6372 to the user.

So should we make all 16384 steps of MT6372 available to users?
Does that mean the DTS needs to be modified as well?
Or, for the reasons, I have just explained (just one customer has this
requirement), then we do not make any changes for compatibility
reasons?
Thanks.

>
> Especially when using linear mappings (which are a totally pointless
> scale to use for a backlight) the extra steps are useful for backlight
> animation.
>
>
> Daniel.

-- 
Best Regards,
ChiaEn Wu


Re: [PATCH v6 07/13] mfd: mt6370: Add MediaTek MT6370 support

2022-07-25 Thread ChiaEn Wu
On Mon, Jul 25, 2022 at 4:43 PM Andy Shevchenko
 wrote:
>
> ...
>
> > > > +#define MT6370_REG_DEV_INFO0x100
> > > > +#define MT6370_REG_CHG_IRQ10x1C0
> > > > +#define MT6370_REG_CHG_MASK1   0x1E0
> > > > +
> > > > +#define MT6370_VENID_MASK  GENMASK(7, 4)
> > > > +
> > > > +#define MT6370_NUM_IRQREGS 16
> > > > +#define MT6370_USBC_I2CADDR0x4E
> > >
> > > > +#define MT6370_REG_ADDRLEN 2
> > > > +#define MT6370_REG_MAXADDR 0x1FF
> > >
> > > These two more logically to have near to other _REG_* definitions above.
> >

...

>
> You lost me. Namespace has a meaning, i.e. grouping items of a kind.
> In your proposal I don't see that. If REG_MAXADDR and REG_ADDRLEN are
> _not_ of the _REG_ kind as per above, why do they have this namespace
> in the first place?

oh... Sorry, I just got the wrong meaning
maybe it should be revised like this, right??
---
#define MT6370_REG_DEV_INFO0x100
#define MT6370_REG_CHG_IRQ10x1C0
#define MT6370_REG_CHG_MASK1   0x1E0
#define MT6370_REG_MAXADDR 0x1FF // Move it to here

#define MT6370_VENID_MASK  GENMASK(7, 4)

#define MT6370_NUM_IRQREGS 16
#define MT6370_USBC_I2CADDR0x4E

#define MT6370_MAX_ADDRLEN 2// Rename
---

Thanks!

-- 
Best Regards,
ChiaEn Wu


Re: [PATCH v6 07/13] mfd: mt6370: Add MediaTek MT6370 support

2022-07-25 Thread ChiaEn Wu
On Mon, Jul 25, 2022 at 4:00 PM Andy Shevchenko
 wrote:
>

...

>
> > +#define MT6370_REG_DEV_INFO0x100
> > +#define MT6370_REG_CHG_IRQ10x1C0
> > +#define MT6370_REG_CHG_MASK1   0x1E0
> > +
> > +#define MT6370_VENID_MASK  GENMASK(7, 4)
> > +
> > +#define MT6370_NUM_IRQREGS 16
> > +#define MT6370_USBC_I2CADDR0x4E
>
> > +#define MT6370_REG_ADDRLEN 2
> > +#define MT6370_REG_MAXADDR 0x1FF
>
> These two more logically to have near to other _REG_* definitions above.

Hi Andy,
Thanks for your review.
Do you mean that we should move '#define MT6370_USBC_I2CADDR' and
'#define MT6370_REG_MAXADDR' after the line '#define
MT6370_REG_CHG_MASK1'?
---
#define MT6370_REG_DEV_INFO0x100
#define MT6370_REG_CHG_IRQ10x1C0
#define MT6370_REG_CHG_MASK1   0x1E0
#define MT6370_USBC_I2CADDR0x4E
#define MT6370_REG_MAXADDR 0x1FF

#define MT6370_VENID_MASK  GENMASK(7, 4)

#define MT6370_NUM_IRQREGS 16
#define MT6370_REG_ADDRLEN 2
---
Like this?

>
> --
> With Best Regards,
> Andy Shevchenko

-- 
Best Regards,
ChiaEn Wu


Re: [PATCH v6 08/13] usb: typec: tcpci_mt6370: Add MediaTek MT6370 tcpci driver

2022-07-25 Thread ChiaEn Wu
On Mon, Jul 25, 2022 at 3:06 PM Chunfeng Yun  wrote:
>
> On Fri, 2022-07-22 at 18:24 +0800, 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 support for the Type-C & Power Delivery controller in
> > MediaTek MT6370 IC.
> >
> > Signed-off-by: ChiYuan Huang 
> > Reviewed-by: AngeloGioacchino Del Regno <
> > angelogioacchino.delre...@collabora.com>
> > ---
> >
> > v6
> > - Convert tcpci as device resource managed with
> > 'devm_add_action_or_reset' API.
> > - Refine remvoe callback.
> > - Refine the commit text from 'This commit add' to 'Add'.
> > ---
> >  drivers/usb/typec/tcpm/Kconfig|  11 ++
> >  drivers/usb/typec/tcpm/Makefile   |   1 +
> >  drivers/usb/typec/tcpm/tcpci_mt6370.c | 208
> > ++
> >  3 files changed, 220 insertions(+)
> >  create mode 100644 drivers/usb/typec/tcpm/tcpci_mt6370.c
> >
> > diff --git a/drivers/usb/typec/tcpm/Kconfig
> > b/drivers/usb/typec/tcpm/Kconfig
> > index 073fd2e..e6b88ca 100644
> > --- a/drivers/usb/typec/tcpm/Kconfig
> > +++ b/drivers/usb/typec/tcpm/Kconfig
> > @@ -35,6 +35,17 @@ config TYPEC_MT6360
> > USB Type-C. It works with Type-C Port Controller Manager
> > to provide USB PD and USB Type-C functionalities.
> >
> > +config TYPEC_TCPCI_MT6370
> > + tristate "MediaTek MT6370 Type-C driver"
> > + depends on MFD_MT6370
> > + help
> > +   MediaTek MT6370 is a multi-functional IC that includes
> > +   USB Type-C. It works with Type-C Port Controller Manager
> > +   to provide USB PD and USB Type-C functionalities.
> > +
> > +   This driver can also be built as a module. The module
> > +   will be called "tcpci_mt6370".
> > +
> >  config TYPEC_TCPCI_MAXIM
> >   tristate "Maxim TCPCI based Type-C chip driver"
> >   help
> > diff --git a/drivers/usb/typec/tcpm/Makefile
> > b/drivers/usb/typec/tcpm/Makefile
> > index 7d499f3..906d9dc 100644
> > --- a/drivers/usb/typec/tcpm/Makefile
> > +++ b/drivers/usb/typec/tcpm/Makefile
> > @@ -6,4 +6,5 @@ typec_wcove-y := wcove.o
> >  obj-$(CONFIG_TYPEC_TCPCI)+= tcpci.o
> >  obj-$(CONFIG_TYPEC_RT1711H)  += tcpci_rt1711h.o
> >  obj-$(CONFIG_TYPEC_MT6360)   += tcpci_mt6360.o
> > +obj-$(CONFIG_TYPEC_TCPCI_MT6370) += tcpci_mt6370.o
> >  obj-$(CONFIG_TYPEC_TCPCI_MAXIM)  += tcpci_maxim.o
> > diff --git a/drivers/usb/typec/tcpm/tcpci_mt6370.c
> > b/drivers/usb/typec/tcpm/tcpci_mt6370.c
> > new file mode 100644
> > index 000..4f53319
> > --- /dev/null
> > +++ b/drivers/usb/typec/tcpm/tcpci_mt6370.c
> > @@ -0,0 +1,208 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Copyright (C) 2022 Richtek Technology Corp.
> > + *
> > + * Author: ChiYuan Huang 
> > + */
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include "tcpci.h"
> > +
> > +#define MT6370_REG_SYSCTRL8  0x9B
> > +
> > +#define MT6370_AUTOIDLE_MASK BIT(3)
> > +
> > +#define MT6370_VENDOR_ID 0x29CF
> > +#define MT6370_TCPC_DID_A0x2170
> > +
> > +struct mt6370_priv {
> > + struct device *dev;
> > + struct regulator *vbus;
> > + struct tcpci *tcpci;
> > + struct tcpci_data tcpci_data;
> > +};
> > +
> > +static const struct reg_sequence mt6370_reg_init[] = {
> > + REG_SEQ(0xA0, 0x1, 1000),
> > + REG_SEQ(0x81, 0x38, 0),
> > + REG_SEQ(0x82, 0x82, 0),
> > + REG_SEQ(0xBA, 0xFC, 0),
> > + REG_SEQ(0xBB, 0x50, 0),
> > + REG_SEQ(0x9E, 0x8F, 0),
> > + REG_SEQ(0xA1, 0x5, 0),
> > + REG_SEQ(0xA2, 0x4, 0),
> > + REG_SEQ(0xA3, 0x4A, 0),
> > + REG_SEQ(0xA4, 0x01, 0),
> > + REG_SEQ(0x95, 0x01, 0),
> > + REG_SEQ(0x80, 0x71, 0),
> > + REG_SEQ(0x9B, 0x3A, 1000),
> > +};
> > +
> > +static int mt6370_tc

[PATCH v6 13/13] video: backlight: mt6370: Add MediaTek MT6370 support

2022-07-22 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 support for MediaTek MT6370 Backlight driver. It's commonly used
to drive the display WLED. There are 4 channels inside, and each channel
supports up to 30mA of current capability with 2048 current steps in
exponential or linear mapping curves.

Signed-off-by: ChiaEn Wu 
---
 drivers/video/backlight/Kconfig|  12 +
 drivers/video/backlight/Makefile   |   1 +
 drivers/video/backlight/mt6370-backlight.c | 339 +
 3 files changed, 352 insertions(+)
 create mode 100644 drivers/video/backlight/mt6370-backlight.c

diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig
index a003e02..846dbe7 100644
--- a/drivers/video/backlight/Kconfig
+++ b/drivers/video/backlight/Kconfig
@@ -268,6 +268,18 @@ config BACKLIGHT_MAX8925
  If you have a LCD backlight connected to the WLED output of MAX8925
  WLED output, say Y here to enable this driver.
 
+config BACKLIGHT_MT6370
+   tristate "MediaTek MT6370 Backlight Driver"
+   depends on MFD_MT6370
+   help
+ This enables support for Mediatek MT6370 Backlight driver.
+ It's commonly used to drive the display WLED. There are 4 channels
+ inside, and each channel supports up to 30mA of current capability
+ with 2048 current steps in exponential or linear mapping curves.
+
+ This driver can also be built as a module. If so, the module
+ will be called "mt6370-backlight".
+
 config BACKLIGHT_APPLE
tristate "Apple Backlight Driver"
depends on X86 && ACPI
diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile
index cae2c83..e815f3f 100644
--- a/drivers/video/backlight/Makefile
+++ b/drivers/video/backlight/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_BACKLIGHT_LP855X)+= lp855x_bl.o
 obj-$(CONFIG_BACKLIGHT_LP8788) += lp8788_bl.o
 obj-$(CONFIG_BACKLIGHT_LV5207LP)   += lv5207lp.o
 obj-$(CONFIG_BACKLIGHT_MAX8925)+= max8925_bl.o
+obj-$(CONFIG_BACKLIGHT_MT6370) += mt6370-backlight.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)  += omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PANDORA)+= pandora_bl.o
 obj-$(CONFIG_BACKLIGHT_PCF50633)   += pcf50633-backlight.o
diff --git a/drivers/video/backlight/mt6370-backlight.c 
b/drivers/video/backlight/mt6370-backlight.c
new file mode 100644
index 000..ba00a8f
--- /dev/null
+++ b/drivers/video/backlight/mt6370-backlight.c
@@ -0,0 +1,339 @@
+// 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 
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_BL_EN   0x1A0
+#define MT6370_REG_BL_BSTCTRL  0x1A1
+#define MT6370_REG_BL_PWM  0x1A2
+#define MT6370_REG_BL_DIM2 0x1A4
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+#define MT6370_BL_EXT_EN_MASK  BIT(7)
+#define MT6370_BL_EN_MASK  BIT(6)
+#define MT6370_BL_CONFIG_MASK  BIT(0)
+#define MT6370_BL_CH_MASK  GENMASK(5, 2)
+#define MT6370_BL_DIM2_MASKGENMASK(2, 0)
+#define MT6370_BL_DUMMY_6372_MASK  GENMASK(2, 0)
+#define MT6370_BL_DIM2_6372_SHIFT  3
+#define MT6370_BL_PWM_EN_MASK  BIT(7)
+#define MT6370_BL_PWM_HYS_EN_MASK  BIT(2)
+#define MT6370_BL_PWM_HYS_SEL_MASK GENMASK(1, 0)
+#define MT6370_BL_OVP_EN_MASK  BIT(7)
+#define MT6370_BL_OVP_SEL_MASK GENMASK(6, 5)
+#define MT6370_BL_OC_EN_MASK   BIT(3)
+#define MT6370_BL_OC_SEL_MASK  GENMASK(2, 1)
+
+#define MT6370_BL_PWM_HYS_TH_MIN_STEP  1
+#define MT6370_BL_PWM_HYS_TH_MAX_STEP  64
+#define MT6370_BL_OVP_MIN_UV   1700
+#define MT6370_BL_OVP_MAX_UV   2900
+#define MT6370_BL_OVP_STEP_UV  400
+#define MT6370_BL_OCP_MIN_UA   90
+#define MT6370_BL_OCP_MAX_UA   180
+#define MT6370_BL_OCP_STEP_UA  30
+#define MT6370_BL_MAX_BRIGHTNESS   2048
+#define MT6370_BL_MAX_CH   15
+
+enum {
+   MT6370_VID_COMMON = 0,
+   MT6370_VID_6372,
+};
+
+struct mt6370_priv {
+   int vid_type;
+   struct backlight_device *bl;
+   struct device *dev;
+   struct gpio_desc *enable_gpio;
+   struct regmap *regmap;
+};
+
+static int mt6370_bl_update_status(struct backlight_device *bl_dev)
+{
+   struct mt6370_priv *priv = bl_get_data(bl_dev);
+   int brightness = backlight_get_brightness(bl_dev);
+   unsigned i

[PATCH v6 12/13] leds: flash: mt6370: Add MediaTek MT6370 flashlight support

2022-07-22 Thread ChiaEn Wu
From: Alice Chen 

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.

The Flash LED in MT6370 has 2 channels and support torch/strobe mode.
Add the support of MT6370 FLASH LED.

Signed-off-by: Alice Chen 
---

v6
- Use 'GENMASK' instead of 'BIT'.
- Use dev_err_probe to decrease LOC.
- Use 'dev' variable to make probe function more clean.
- Refine the return of _mt6370_flash_brightness_set function.
- Refine the descriptions.
- Use mt6370_clamp() instead of clamp_align().
- Use device resource managed API for v4l2 flash_release.
---
 drivers/leds/flash/Kconfig |  12 +
 drivers/leds/flash/Makefile|   1 +
 drivers/leds/flash/leds-mt6370-flash.c | 633 +
 3 files changed, 646 insertions(+)
 create mode 100644 drivers/leds/flash/leds-mt6370-flash.c

diff --git a/drivers/leds/flash/Kconfig b/drivers/leds/flash/Kconfig
index d3eb689..d5761ed 100644
--- a/drivers/leds/flash/Kconfig
+++ b/drivers/leds/flash/Kconfig
@@ -90,4 +90,16 @@ config LEDS_SGM3140
  This option enables support for the SGM3140 500mA Buck/Boost Charge
  Pump LED Driver.
 
+config LEDS_MT6370_FLASHLIGHT
+   tristate "Flash LED Support for MediaTek MT6370 PMIC"
+   depends on LEDS_CLASS
+   depends on MFD_MT6370
+   help
+ Support 2 channels and torch/strobe mode.
+ Say Y here to enable support for
+ MT6370_FLASH_LED device.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-flash".
+
 endif # LEDS_CLASS_FLASH
diff --git a/drivers/leds/flash/Makefile b/drivers/leds/flash/Makefile
index 0acbddc..4c4c171 100644
--- a/drivers/leds/flash/Makefile
+++ b/drivers/leds/flash/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_LEDS_MAX77693) += leds-max77693.o
 obj-$(CONFIG_LEDS_RT4505)  += leds-rt4505.o
 obj-$(CONFIG_LEDS_RT8515)  += leds-rt8515.o
 obj-$(CONFIG_LEDS_SGM3140) += leds-sgm3140.o
+obj-$(CONFIG_LEDS_MT6370_FLASHLIGHT)   += leds-mt6370-flash.o
diff --git a/drivers/leds/flash/leds-mt6370-flash.c 
b/drivers/leds/flash/leds-mt6370-flash.c
new file mode 100644
index 000..fe439ee
--- /dev/null
+++ b/drivers/leds/flash/leds-mt6370-flash.c
@@ -0,0 +1,633 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_FLASH1,
+   MT6370_LED_FLASH2,
+   MT6370_MAX_LEDS
+};
+
+/* Virtual definition for multicolor */
+
+#define MT6370_REG_FLEDEN  0x17E
+#define MT6370_REG_STRBTO  0x173
+#define MT6370_REG_CHGSTAT20x1D1
+#define MT6370_REG_FLEDSTAT1   0x1D9
+#defineMT6370_REG_FLEDISTRB(_id)   (0x174 + 4 * _id)
+#define MT6370_REG_FLEDITOR(_id)   (0x175 + 4 * _id)
+#define MT6370_ITORCH_MASK GENMASK(4, 0)
+#define MT6370_ISTROBE_MASKGENMASK(6, 0)
+#define MT6370_STRBTO_MASK GENMASK(6, 0)
+#define MT6370_TORCHEN_MASKBIT(3)
+#define MT6370_STROBEN_MASKBIT(2)
+#define MT6370_FLCSEN_MASK(_id)BIT(MT6370_LED_FLASH2 - _id)
+#define MT6370_FLCSEN_MASK_ALL GENMASK(1, 0)
+#define MT6370_FLEDCHGVINOVP_MASK  BIT(3)
+#define MT6370_FLED1STRBTO_MASKBIT(11)
+#define MT6370_FLED2STRBTO_MASKBIT(10)
+#define MT6370_FLED1STRB_MASK  BIT(9)
+#define MT6370_FLED2STRB_MASK  BIT(8)
+#define MT6370_FLED1SHORT_MASK BIT(7)
+#define MT6370_FLED2SHORT_MASK BIT(6)
+#define MT6370_FLEDLVF_MASKBIT(3)
+
+#define MT6370_LED_JOINT   2
+#define MT6370_RANGE_FLED_REG  4
+#define MT6370_ITORCH_MIN_UA   25000
+#define MT6370_ITORCH_STEP_UA  12500
+#define MT6370_ITORCH_MAX_UA   40
+#define MT6370_ITORCH_DOUBLE_MAX_UA80
+#define MT6370_ISTRB_MIN_UA5
+#define MT6370_ISTRB_STEP_UA   12500
+#define MT6370_ISTRB_MAX_UA150
+#define MT6370_ISTRB_DOUBLE_MAX_UA 300
+#define MT6370_STRBTO_MIN_US   64000
+#define MT6370_STRBTO_STEP_US  32000
+#define MT6370_STRBTO_MAX_US   2432000
+
+#define STATE_OFF  0
+#define STATE_KEEP 1
+#define STATE_ON   2
+
+#define to_mt6370_led(ptr, member) container_of(ptr, struct mt6370_led, member)
+
+struct mt6370_led {
+   struct led_classdev_flash flash;
+   struct v4l2_flash *v4l2_flash;
+   struct mt6370_priv *priv;
+   u32 led_no;
+   u32 default_state;
+};
+
+struct mt6370_priv {

[PATCH v6 11/13] leds: rgb: mt6370: Add MediaTek MT6370 current sink type LED Indicator support

2022-07-22 Thread ChiaEn Wu
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.

In MediaTek MT6370, there are four channel current-sink RGB LEDs that
support hardware pattern for constant current, PWM, and breath mode.
Isink4 channel can also be used as a CHG_VIN power good indicator.

Signed-off-by: Alice Chen 
Signed-off-by: ChiYuan Huang 
---

v6
- Remove the 'ko' from mt6370 led Kconfig description.
- Add both authors for Alice and ChiYuan.
- Use pdata to distinguish the code from mt6370/71 to mt6372.
- Instead of 'state' define, use the 'state' enum.
- Fix the typo for 'MT6372_PMW_DUTY'.
- For pwm_duty define, replace with bit macro - 1.
- Refine all the labels from 'out' to 'out_unlock'.
- Use struct 'dev' variable and 'dev_err_probe' to optimize the LOC.
- Revise for the array initialization from {0} to {}.
- Move into rgb folder and rename file name to 'leds-mt6370-rgb'.
- Refine the 'comma' usage in struct/enum.
---
 drivers/leds/rgb/Kconfig   |   13 +
 drivers/leds/rgb/Makefile  |1 +
 drivers/leds/rgb/leds-mt6370-rgb.c | 1004 
 3 files changed, 1018 insertions(+)
 create mode 100644 drivers/leds/rgb/leds-mt6370-rgb.c

diff --git a/drivers/leds/rgb/Kconfig b/drivers/leds/rgb/Kconfig
index 204cf47..d6ad875 100644
--- a/drivers/leds/rgb/Kconfig
+++ b/drivers/leds/rgb/Kconfig
@@ -26,4 +26,17 @@ config LEDS_QCOM_LPG
 
  If compiled as a module, the module will be named leds-qcom-lpg.
 
+config LEDS_MT6370_RGB
+   tristate "LED Support for MediaTek MT6370 PMIC"
+   depends on MFD_MT6370
+   select LINEAR_RANGE
+   help
+ Say Y here to enable support for MT6370_RGB LED device.
+ In MT6370, there are four channel current-sink LED drivers that
+ support hardware pattern for constant current, PWM, and breath mode.
+ Isink4 channel can also be used as a CHG_VIN power good indicator.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-rgb".
+
 endif # LEDS_CLASS_MULTICOLOR
diff --git a/drivers/leds/rgb/Makefile b/drivers/leds/rgb/Makefile
index 0675bc0..8c01daf 100644
--- a/drivers/leds/rgb/Makefile
+++ b/drivers/leds/rgb/Makefile
@@ -2,3 +2,4 @@
 
 obj-$(CONFIG_LEDS_PWM_MULTICOLOR)  += leds-pwm-multicolor.o
 obj-$(CONFIG_LEDS_QCOM_LPG)+= leds-qcom-lpg.o
+obj-$(CONFIG_LEDS_MT6370_RGB)  += leds-mt6370-rgb.o
diff --git a/drivers/leds/rgb/leds-mt6370-rgb.c 
b/drivers/leds/rgb/leds-mt6370-rgb.c
new file mode 100644
index 000..bf52989
--- /dev/null
+++ b/drivers/leds/rgb/leds-mt6370-rgb.c
@@ -0,0 +1,1004 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+enum {
+   MT6370_LED_ISNK1 = 0,
+   MT6370_LED_ISNK2,
+   MT6370_LED_ISNK3,
+   MT6370_LED_ISNK4,
+   MT6370_MAX_LEDS
+};
+
+enum mt6370_led_mode {
+   MT6370_LED_PWM_MODE = 0,
+   MT6370_LED_BREATH_MODE,
+   MT6370_LED_REG_MODE,
+   MT6370_LED_MAX_MODE
+};
+
+enum mt6370_led_field {
+   F_RGB_EN = 0,
+   F_CHGIND_EN,
+   F_LED1_CURR,
+   F_LED2_CURR,
+   F_LED3_CURR,
+   F_LED4_CURR,
+   F_LED1_MODE,
+   F_LED2_MODE,
+   F_LED3_MODE,
+   F_LED4_MODE,
+   F_LED1_DUTY,
+   F_LED2_DUTY,
+   F_LED3_DUTY,
+   F_LED4_DUTY,
+   F_LED1_FREQ,
+   F_LED2_FREQ,
+   F_LED3_FREQ,
+   F_LED4_FREQ,
+   F_MAX_FIELDS
+};
+
+enum mt6370_led_ranges {
+   R_LED123_CURR = 0,
+   R_LED4_CURR,
+   R_LED_TRFON,
+   R_LED_TOFF,
+   R_MAX_RANGES
+};
+
+enum mt6370_pattern {
+   P_LED_TR1 = 0,
+   P_LED_TR2,
+   P_LED_TF1,
+   P_LED_TF2,
+   P_LED_TON,
+   P_LED_TOFF,
+   P_MAX_PATTERNS
+};
+
+enum mt6370_state {
+   STATE_OFF = 0,
+   STATE_KEEP,
+   STATE_ON,
+   STATE_MAX
+};
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_RGB1_DIM0x182
+#define MT6370_REG_RGB2_DIM0x183
+#define MT6370_REG_RGB3_DIM0x184
+#define MT6370_REG_RGB_EN  0x185
+#define MT6370_REG_RGB1_ISNK   0x186
+#define MT6370_REG_RGB2_ISNK   0x187
+#define MT6370_REG_RGB3_ISNK   0x188
+#define MT6370_REG_RGB1_TR 0x189
+#define MT6370_REG_RGB_CHRIND_DIM  0x192
+#define MT6370_REG_RGB_CHRIND_CTRL 0x193
+#define MT6370_REG_RGB_CHRIND_TR   0x194
+

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

2022-07-22 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 
---

v6
- Remove the varable (*psy_desc) of struct mt6370_priv
- Remove the deprecated usb type (POWER_SUPPLY_TYPE_USB_CDP and
  POWER_SUPPLY_TYPE_USB_DCP)
- Remove useless remove()
- Revise all units from mini- to micro-
- Revise get/set power_supply_prop (change to directly return get/set
  regmap_field)
- Revise probe() and use devm_add_action_or_reset() for handling of the
  workqueue/delayed_work/mutex
- Revise mt6370_chg_psy_desc
  - Add '.name = "mt6370-charger"'
  - Use 'static const'

---

uevent dump:
vim3l:/sys/class/power_supply/mt6370-charger # cat uevent
POWER_SUPPLY_NAME=mt6370-charger
POWER_SUPPLY_TYPE=USB
POWER_SUPPLY_ONLINE=0
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_CHARGE_TYPE=N/A
POWER_SUPPLY_CONSTANT_CHARGE_CURRENT=90
POWER_SUPPLY_CONSTANT_CHARGE_CURRENT_MAX=500
POWER_SUPPLY_CONSTANT_CHARGE_VOLTAGE=420
POWER_SUPPLY_CONSTANT_CHARGE_VOLTAGE_MAX=471
POWER_SUPPLY_INPUT_CURRENT_LIMIT=50
POWER_SUPPLY_INPUT_VOLTAGE_LIMIT=440
POWER_SUPPLY_PRECHARGE_CURRENT=15
POWER_SUPPLY_CHARGE_TERM_CURRENT=25
POWER_SUPPLY_USB_TYPE=[Unknown] SDP CDP DCP
---
 drivers/power/supply/Kconfig  |  14 +
 drivers/power/supply/Makefile |   1 +
 drivers/power/supply/mt6370-charger.c | 973 ++
 3 files changed, 988 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..84f1436
--- /dev/null
+++ b/drivers/power/supply/mt6370-charger.c
@@ -0,0 +1,973 @@
+// 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 MT

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

2022-07-22 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 the MT6370 ADC driver for system monitoring, including
charger current, voltage, and temperature.

Signed-off-by: ChiaEn Wu 
Reviewed-by: AngeloGioacchino Del Regno 

---

v6
- Using 'struct device *dev = >dev' in probe()
- Revise the sixth parameter of regmap_read_poll_timeout() by
  replacing '1000' with 'MILLI'
- Revise the units of three macros
  - MT6370_AICR_400MA --> MT6370_AICR_400_mA
  - MT6370_ICHG_500MA --> MT6370_ICHG_500_mA
  - MT6370_ICHG_900MA --> MT6370_ICHG_900_mA
---
 drivers/iio/adc/Kconfig  |  12 ++
 drivers/iio/adc/Makefile |   1 +
 drivers/iio/adc/mt6370-adc.c | 274 +++
 3 files changed, 287 insertions(+)
 create mode 100644 drivers/iio/adc/mt6370-adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 48ace74..60bcc28 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -737,6 +737,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 39d806f..0ce285c 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..c665de9
--- /dev/null
+++ b/drivers/iio/adc/mt6370-adc.c
@@ -0,0 +1,274 @@
+// 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 /* AICR */
+#define MT6370_REG_CHG_CTRL7   0x117 /* ICHG */
+#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_400_mA 0x6
+#define MT6370_ICHG_500_mA 0x4
+#define MT6370_ICHG_900_mA 0x8
+
+#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, "Failed to read ADC register (%d)\n", ret);
+   goto adc_unlock;
+   }
+
+   ret = regmap_raw_read(priv->regmap, MT6370_REG_ADC_DATA_H,
+ _val, sizeof(be_val));
+   if (ret)
+   goto adc_unlock;
+
+   *val = be16_to_

[PATCH v6 08/13] usb: typec: tcpci_mt6370: Add MediaTek MT6370 tcpci driver

2022-07-22 Thread ChiaEn Wu
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 support for the Type-C & Power Delivery controller in
MediaTek MT6370 IC.

Signed-off-by: ChiYuan Huang 
Reviewed-by: AngeloGioacchino Del Regno 

---

v6
- Convert tcpci as device resource managed with 'devm_add_action_or_reset' API.
- Refine remvoe callback.
- Refine the commit text from 'This commit add' to 'Add'.
---
 drivers/usb/typec/tcpm/Kconfig|  11 ++
 drivers/usb/typec/tcpm/Makefile   |   1 +
 drivers/usb/typec/tcpm/tcpci_mt6370.c | 208 ++
 3 files changed, 220 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm/tcpci_mt6370.c

diff --git a/drivers/usb/typec/tcpm/Kconfig b/drivers/usb/typec/tcpm/Kconfig
index 073fd2e..e6b88ca 100644
--- a/drivers/usb/typec/tcpm/Kconfig
+++ b/drivers/usb/typec/tcpm/Kconfig
@@ -35,6 +35,17 @@ config TYPEC_MT6360
  USB Type-C. It works with Type-C Port Controller Manager
  to provide USB PD and USB Type-C functionalities.
 
+config TYPEC_TCPCI_MT6370
+   tristate "MediaTek MT6370 Type-C driver"
+   depends on MFD_MT6370
+   help
+ MediaTek MT6370 is a multi-functional IC that includes
+ USB Type-C. It works with Type-C Port Controller Manager
+ to provide USB PD and USB Type-C functionalities.
+
+ This driver can also be built as a module. The module
+ will be called "tcpci_mt6370".
+
 config TYPEC_TCPCI_MAXIM
tristate "Maxim TCPCI based Type-C chip driver"
help
diff --git a/drivers/usb/typec/tcpm/Makefile b/drivers/usb/typec/tcpm/Makefile
index 7d499f3..906d9dc 100644
--- a/drivers/usb/typec/tcpm/Makefile
+++ b/drivers/usb/typec/tcpm/Makefile
@@ -6,4 +6,5 @@ typec_wcove-y   := wcove.o
 obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
 obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
 obj-$(CONFIG_TYPEC_MT6360) += tcpci_mt6360.o
+obj-$(CONFIG_TYPEC_TCPCI_MT6370)   += tcpci_mt6370.o
 obj-$(CONFIG_TYPEC_TCPCI_MAXIM)+= tcpci_maxim.o
diff --git a/drivers/usb/typec/tcpm/tcpci_mt6370.c 
b/drivers/usb/typec/tcpm/tcpci_mt6370.c
new file mode 100644
index 000..4f53319
--- /dev/null
+++ b/drivers/usb/typec/tcpm/tcpci_mt6370.c
@@ -0,0 +1,208 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "tcpci.h"
+
+#define MT6370_REG_SYSCTRL80x9B
+
+#define MT6370_AUTOIDLE_MASK   BIT(3)
+
+#define MT6370_VENDOR_ID   0x29CF
+#define MT6370_TCPC_DID_A  0x2170
+
+struct mt6370_priv {
+   struct device *dev;
+   struct regulator *vbus;
+   struct tcpci *tcpci;
+   struct tcpci_data tcpci_data;
+};
+
+static const struct reg_sequence mt6370_reg_init[] = {
+   REG_SEQ(0xA0, 0x1, 1000),
+   REG_SEQ(0x81, 0x38, 0),
+   REG_SEQ(0x82, 0x82, 0),
+   REG_SEQ(0xBA, 0xFC, 0),
+   REG_SEQ(0xBB, 0x50, 0),
+   REG_SEQ(0x9E, 0x8F, 0),
+   REG_SEQ(0xA1, 0x5, 0),
+   REG_SEQ(0xA2, 0x4, 0),
+   REG_SEQ(0xA3, 0x4A, 0),
+   REG_SEQ(0xA4, 0x01, 0),
+   REG_SEQ(0x95, 0x01, 0),
+   REG_SEQ(0x80, 0x71, 0),
+   REG_SEQ(0x9B, 0x3A, 1000),
+};
+
+static int mt6370_tcpc_init(struct tcpci *tcpci, struct tcpci_data *data)
+{
+   u16 did;
+   int ret;
+
+   ret = regmap_register_patch(data->regmap, mt6370_reg_init,
+   ARRAY_SIZE(mt6370_reg_init));
+   if (ret)
+   return ret;
+
+   ret = regmap_raw_read(data->regmap, TCPC_BCD_DEV, , sizeof(u16));
+   if (ret)
+   return ret;
+
+   if (did == MT6370_TCPC_DID_A)
+   return regmap_write(data->regmap, TCPC_FAULT_CTRL, 0x80);
+
+   return 0;
+}
+
+static int mt6370_tcpc_set_vconn(struct tcpci *tcpci, struct tcpci_data *data,
+bool enable)
+{
+   return regmap_update_bits(data->regmap, MT6370_REG_SYSCTRL8,
+ MT6370_AUTOIDLE_MASK,
+ !enable ? MT6370_AUTOIDLE_MASK : 0);
+}
+
+static int mt6370_tcpc_set_vbus(struct tcpci *tcpci, struct tcpci_data *data,
+   bool source, bool sink)
+{
+   struct mt6370_priv *priv = container_of(data, struct mt6370_priv,
+   tcpci_data);
+   int ret;
+
+   ret = regulator_is_enabled(priv->vbus);
+   if (ret < 0)
+   return ret;
+
+   if (ret && !source)
+   return regulator_disable(priv->vbus);
+
+   

[PATCH v6 07/13] mfd: mt6370: Add MediaTek MT6370 support

2022-07-22 Thread ChiaEn Wu
From: ChiYuan Huang 

This adds support for the MediaTek MT6370 SubPMIC. 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.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Andy Shevchenko 
---
 drivers/mfd/Kconfig  |  16 +++
 drivers/mfd/Makefile |   1 +
 drivers/mfd/mt6370.c | 281 +++
 drivers/mfd/mt6370.h |  99 ++
 4 files changed, 397 insertions(+)
 create mode 100644 drivers/mfd/mt6370.c
 create mode 100644 drivers/mfd/mt6370.h

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 3b59456..a9bcae3 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -937,6 +937,22 @@ config MFD_MT6360
  PMIC part includes 2-channel BUCKs and 2-channel LDOs
  LDO part includes 4-channel LDOs
 
+config MFD_MT6370
+   tristate "MediaTek MT6370 SubPMIC"
+   select MFD_CORE
+   select REGMAP_I2C
+   select REGMAP_IRQ
+   depends on I2C
+   help
+ Say Y here to enable MT6370 SubPMIC functional support.
+ It consists 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 driver can also be built as a module. If so, the module
+ will be called "mt6370".
+
 config MFD_MT6397
tristate "MediaTek MT6397 PMIC Support"
select MFD_CORE
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 8c69867..81dbed3 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -173,6 +173,7 @@ obj-$(CONFIG_MFD_MAX8998)   += max8998.o max8998-irq.o
 obj-$(CONFIG_MFD_MP2629)   += mp2629.o
 
 obj-$(CONFIG_MFD_MT6360)   += mt6360-core.o
+obj-$(CONFIG_MFD_MT6370)   += mt6370.o
 mt6397-objs:= mt6397-core.o mt6397-irq.o mt6358-irq.o
 obj-$(CONFIG_MFD_MT6397)   += mt6397.o
 
diff --git a/drivers/mfd/mt6370.c b/drivers/mfd/mt6370.c
new file mode 100644
index 000..070c1c7
--- /dev/null
+++ b/drivers/mfd/mt6370.c
@@ -0,0 +1,281 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "mt6370.h"
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_CHG_IRQ10x1C0
+#define MT6370_REG_CHG_MASK1   0x1E0
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+
+#define MT6370_NUM_IRQREGS 16
+#define MT6370_USBC_I2CADDR0x4E
+#define MT6370_REG_ADDRLEN 2
+#define MT6370_REG_MAXADDR 0x1FF
+
+#define MT6370_VENID_RT50810x8
+#define MT6370_VENID_RT5081A   0xA
+#define MT6370_VENID_MT63700xE
+#define MT6370_VENID_MT63710xF
+#define MT6370_VENID_MT6372P   0x9
+#define MT6370_VENID_MT6372CP  0xB
+
+static const struct regmap_irq mt6370_irqs[] = {
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_DIRCHGON, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TREG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_AICR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_MIVR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_PWR_RDY, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_FL_CHG_VINOVP, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VSYSUV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VSYSOV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VBATOV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VINOVPCHG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_COLD, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_COOL, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_WARM, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_HOT, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_STATC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_FAULT, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_STATC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TMR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_BATABS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_ADPBAD, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_RVP, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TSHUTDOWN, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_IINMEAS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_ICCMEAS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHGDET_DONE, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_WDTMR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_SSFINISH, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_RECHG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TERM, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_IEOC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_ADC_DONE, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_PUMPX_DONE, 8),
+   

[PATCH v6 06/13] dt-bindings: mfd: Add MediaTek MT6370

2022-07-22 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Krzysztof Kozlowski 
---
 .../devicetree/bindings/mfd/mediatek,mt6370.yaml   | 280 +
 include/dt-bindings/iio/adc/mediatek,mt6370_adc.h  |  18 ++
 2 files changed, 298 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
 create mode 100644 include/dt-bindings/iio/adc/mediatek,mt6370_adc.h

diff --git a/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml 
b/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
new file mode 100644
index 000..410e2d4
--- /dev/null
+++ b/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
@@ -0,0 +1,280 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/mediatek,mt6370.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 SubPMIC
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  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.
+
+properties:
+  compatible:
+const: mediatek,mt6370
+
+  reg:
+maxItems: 1
+
+  wakeup-source: true
+
+  interrupts:
+maxItems: 1
+
+  interrupt-controller: true
+
+  "#interrupt-cells":
+const: 1
+
+  adc:
+type: object
+description: |
+  Provides 9 channels for system monitoring, including VBUSDIV5 (lower
+  accuracy, higher measure range), VBUSDIV2 (higher accuracy, lower
+  measure range), VBAT, VSYS, CHG_VDDP, TS_BAT, IBUS, IBAT, and TEMP_JC.
+
+properties:
+  compatible:
+const: mediatek,mt6370-adc
+
+  "#io-channel-cells":
+const: 1
+
+required:
+  - compatible
+  - "#io-channel-cells"
+
+  backlight:
+type: object
+$ref: /schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+
+  charger:
+type: object
+$ref: /schemas/power/supply/mediatek,mt6370-charger.yaml#
+
+  tcpc:
+type: object
+$ref: /schemas/usb/mediatek,mt6370-tcpc.yaml#
+
+  indicator:
+type: object
+$ref: /schemas/leds/mediatek,mt6370-indicator.yaml#
+
+  flashlight:
+type: object
+$ref: /schemas/leds/mediatek,mt6370-flashlight.yaml#
+
+  regulators:
+type: object
+description: |
+  List all supported regulators, which support the control for DisplayBias
+  voltages and one general purpose LDO which commonly used to drive the
+  vibrator.
+
+patternProperties:
+  "^(dsvbst|vibldo)$":
+$ref: /schemas/regulator/regulator.yaml#
+type: object
+unevaluatedProperties: false
+
+  "^(dsvpos|dsvneg)$":
+$ref: /schemas/regulator/regulator.yaml#
+type: object
+unevaluatedProperties: false
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - interrupt-controller
+  - "#interrupt-cells"
+  - regulators
+  - adc
+  - backlight
+  - indicator
+  - tcpc
+  - charger
+  - flashlight
+
+additionalProperties: false
+
+examples:
+  - |
+#include 
+#include 
+#include 
+#include 
+i2c {
+  #address-cells = <1>;
+  #size-cells = <0>;
+
+pmic@34 {
+compatible = "mediatek,mt6370";
+reg = <0x34>;
+wakeup-source;
+interrupts-extended = < 3 IRQ_TYPE_LEVEL_LOW>;
+interrupt-controller;
+#interrupt-cells = <1>;
+
+mt6370_adc: adc {
+  compatible = "mediatek,mt6370-adc";
+  #io-channel-cells = <1>;
+};
+
+backlight {
+  compatible = "mediatek,mt6370-backlight";
+  mediatek,bled-channel-use = /bits/ 8 <15>;
+};
+
+charger {
+  compatible = "mediatek,mt6370-charger";
+  interrupts = <48>, <68>, <6>;
+  interrupt-names = "attach_i", "uvp_d_evt", "mivr";
+  io-channels = <_adc MT6370_CHAN_IBUS>;
+
+  mt6370_otg_vbus: usb-otg-vbus-regulator {
+regulator-name = "mt6370-usb-otg-vbus";
+regulator-min-microvolt = <435>;
+regulator-max-microvolt = <580>;
+regulator-min-microamp = <50>;
+regulator-max-microamp = <300>;
+  };
+};
+
+indicator {
+  compatible = "mediatek,mt6370-indicator";
+  #address-cells = <1>;
+  #size-cells = <0>;
+
+  multi-led@0 {
+reg = <0>;
+function = LED_FUNCTION_INDICATOR;
+color = ;
+led-max-microamp = <24000>;
+#address-cells = <1>;
+#size-cells = <0>;
+led@0 {
+  reg = <0>;
+  color = ;
+};
+led@1 {
+   

[PATCH v6 05/13] dt-bindings: backlight: Add MediaTek MT6370 backlight

2022-07-22 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MT6370 backlight binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Rob Herring 
---
 .../leds/backlight/mediatek,mt6370-backlight.yaml  | 92 ++
 1 file changed, 92 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
new file mode 100644
index 000..d674212
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
@@ -0,0 +1,92 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: 
http://devicetree.org/schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Backlight
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  The MT6370 Backlight WLED driver supports up to a 29V output voltage for
+  4 channels of 8 series WLEDs. Each channel supports up to 30mA of current
+  capability with 2048 current steps (11 bits) in exponential or linear
+  mapping curves.
+
+allOf:
+  - $ref: common.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-backlight
+
+  default-brightness:
+minimum: 0
+maximum: 2048
+
+  max-brightness:
+minimum: 0
+maximum: 2048
+
+  enable-gpios:
+description: External backlight 'enable' pin
+maxItems: 1
+
+  mediatek,bled-pwm-enable:
+description: |
+  Enable external PWM input for backlight dimming
+type: boolean
+
+  mediatek,bled-pwm-hys-enable:
+description: |
+  Enable the backlight input-hysteresis for PWM mode
+type: boolean
+
+  mediatek,bled-pwm-hys-input-th-steps:
+$ref: /schemas/types.yaml#/definitions/uint8
+enum: [1, 4, 16, 64]
+description: |
+  The selection of the upper and lower bounds threshold of backlight
+  PWM resolution. If we choose selection 64, the variation of PWM
+  resolution needs more than 64 steps.
+
+  mediatek,bled-ovp-shutdown:
+description: |
+  Enable the backlight shutdown when OVP level triggered
+type: boolean
+
+  mediatek,bled-ovp-microvolt:
+enum: [1700, 2100, 2500, 2900]
+description: |
+  Backlight OVP level selection.
+
+  mediatek,bled-ocp-shutdown:
+description: |
+  Enable the backlight shutdown when OCP level triggerred.
+type: boolean
+
+  mediatek,bled-ocp-microamp:
+enum: [90, 120, 150, 180]
+description: |
+  Backlight OC level selection.
+
+  mediatek,bled-channel-use:
+$ref: /schemas/types.yaml#/definitions/uint8
+description: |
+  Backlight LED channel to be used.
+  Each bit mapping to:
+- 0: CH4
+- 1: CH3
+- 2: CH2
+- 3: CH1
+minimum: 1
+maximum: 15
+
+required:
+  - compatible
+  - mediatek,bled-channel-use
+
+additionalProperties: false
-- 
2.7.4



[PATCH v6 04/13] dt-bindings: leds: Add MediaTek MT6370 flashlight

2022-07-22 Thread ChiaEn Wu
From: Alice Chen 

Add MediaTek MT6370 flashlight binding documentation.

Signed-off-by: Alice Chen 
Reviewed-by: Krzysztof Kozlowski 
---
 .../bindings/leds/mediatek,mt6370-flashlight.yaml  | 41 ++
 1 file changed, 41 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
new file mode 100644
index 000..e9d02ed
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-flashlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Flash LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 flash LED driver include 2-channel flash LED support Torch/Strobe 
Mode.
+
+properties:
+  compatible:
+const: mediatek,mt6370-flashlight
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-1]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1]
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v6 03/13] dt-bindings: leds: mt6370: Add MediaTek MT6370 current sink type LED indicator

2022-07-22 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 current sink type LED indicator binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Krzysztof Kozlowski 
---

v6
- Add 'reg' property of led of multi-led to prevent checking error.
---
 .../bindings/leds/mediatek,mt6370-indicator.yaml   | 81 ++
 1 file changed, 81 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
new file mode 100644
index 000..204b103
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-indicator.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 LED driver include 4-channel RGB LED support Register/PWM/Breath 
Mode
+
+allOf:
+  - $ref: leds-class-multicolor.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-indicator
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^multi-led@[0-3]$":
+type: object
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-2]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2]
+
+required:
+  - reg
+  - color
+
+required:
+  - reg
+  - color
+  - "#address-cells"
+  - "#size-cells"
+
+  "^led@[0-3]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+required:
+  - reg
+  - color
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v6 02/13] dt-bindings: power: supply: Add MediaTek MT6370 Charger

2022-07-22 Thread ChiaEn Wu
From: ChiaEn Wu 

Add MediaTek MT6370 Charger binding documentation.

Signed-off-by: ChiaEn Wu 
Reviewed-by: Krzysztof Kozlowski 
---
 .../power/supply/mediatek,mt6370-charger.yaml  | 88 ++
 1 file changed, 88 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml

diff --git 
a/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
new file mode 100644
index 000..bd09a0a
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/mediatek,mt6370-charger.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Battery Charger
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Provides Battery Charger, Boost for OTG devices and BC1.2 detection.
+
+properties:
+  compatible:
+const: mediatek,mt6370-charger
+
+  interrupts:
+description: |
+  Specify what irqs are needed to be handled by MT6370 Charger driver. IRQ
+  "MT6370_IRQ_CHG_MIVR", "MT6370_IRQ_ATTACH" and "MT6370_IRQ_OVPCTRL_UVP_D"
+  are required.
+items:
+  - description: BC1.2 done irq
+  - description: usb plug in irq
+  - description: mivr irq
+
+  interrupt-names:
+items:
+  - const: attach_i
+  - const: uvp_d_evt
+  - const: mivr
+
+  io-channels:
+description: |
+  Use ADC channel to read VBUS, IBUS, IBAT, etc., info.
+minItems: 1
+items:
+  - description: |
+  VBUS voltage with lower accuracy (+-75mV) but higher measure
+  range (1~22V)
+  - description: |
+  VBUS voltage with higher accuracy (+-30mV) but lower measure
+  range (1~9.76V)
+  - description: the main system input voltage
+  - description: battery voltage
+  - description: battery temperature-sense input voltage
+  - description: IBUS current (required)
+  - description: battery current
+  - description: |
+  regulated output voltage to supply for the PWM low-side gate driver
+  and the bootstrap capacitor
+  - description: IC junction temperature
+
+  io-channel-names:
+minItems: 1
+items:
+  - const: vbusdiv5
+  - const: vbusdiv2
+  - const: vsys
+  - const: vbat
+  - const: ts_bat
+  - const: ibus
+  - const: ibat
+  - const: chg_vddp
+  - const: temp_jc
+
+  usb-otg-vbus-regulator:
+type: object
+description: OTG boost regulator.
+unevaluatedProperties: false
+$ref: /schemas/regulator/regulator.yaml#
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - interrupts
+  - interrupt-names
+  - io-channels
+
+additionalProperties: false
+
+...
-- 
2.7.4



[PATCH v6 01/13] dt-bindings: usb: Add MediaTek MT6370 TCPC

2022-07-22 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 TCPC binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Krzysztof Kozlowski 
---
 .../bindings/usb/mediatek,mt6370-tcpc.yaml | 36 ++
 1 file changed, 36 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml 
b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
new file mode 100644
index 000..72f56cc
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/mediatek,mt6370-tcpc.yaml#;
+$schema: "http://devicetree.org/meta-schemas/core.yaml#;
+
+title: MediatTek MT6370 Type-C Port Switch and Power Delivery controller
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  MediaTek MT6370 is a multi-functional device.
+  It integrates charger, ADC, flash, RGB indicators,
+  regulators (DSV/VIBLDO), and TypeC Port Switch with Power Delivery 
controller.
+  This document only describes MT6370 Type-C Port Switch and
+  Power Delivery controller.
+
+properties:
+  compatible:
+enum:
+  - mediatek,mt6370-tcpc
+
+  interrupts:
+maxItems: 1
+
+  connector:
+type: object
+$ref: /schemas/connector/usb-connector.yaml#
+unevaluatedProperties: false
+
+additionalProperties: false
+
+required:
+  - compatible
+  - interrupts
-- 
2.7.4



[PATCH v6 00/13] Add MediaTek MT6370 PMIC support

2022-07-22 Thread ChiaEn Wu
From: ChiaEn Wu 

This patch series add MediaTek MT6370 PMIC support. The 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.

In this series of patches, we based on Andy Shevchenko's mfd patch used to
adjust the Makefile order.
(https://lore.kernel.org/all/20220616182524.7956-2-andriy.shevche...@linux.intel.com/)
Among with this, we took some changes for MT6370 and refined the MT6370 device
tree files to comply with DT specifications.

"[PATCH v6 06/13] dt-bindings: mfd: Add MediaTek MT6370" depends on previous
DT binding patches, so before applying this patch, please apply other DT
patches first. Thanks!

Thank you,
ChiaEn Wu

---
Changes in v6:
- In Patch 03/13:
- Add 'reg' property of led of multi-led to prevent checking
  error.

- In Patch 08/13:
- Convert tcpci as device resource managed with
   'devm_add_action_or_reset' api.
- Refine remvoe callback.
- Refine the commit text from 'this commit add' to 'add'.

- In Patch 09/13:
- Using 'struct device *dev = >dev' in probe()
- Revise the sixth parameter of regmap_read_poll_timeout() by
   replacing '1000' with 'MILLI'
- Revise the units of three macros
- MT6370_AICR_400MA --> MT6370_AICR_400_mA
- MT6370_ICHG_500MA --> MT6370_ICHG_500_mA
- MT6370_ICHG_900MA --> MT6370_ICHG_900_mA

- In patch 10/13:
- Remove the varable (*psy_desc) of struct mt6370_priv
- Remove the deprecated usb type (POWER_SUPPLY_TYPE_USB_CDP and
   POWER_SUPPLY_TYPE_USB_DCP)
- Remove useless remove()
- Revise all units from mini- to micro-
- Revise get/set power_supply_prop (change to directly return get/set
   regmap_field)
- Revise probe() and use devm_add_action_or_reset() for handling of the
   workqueue/delayed_work/mutex
- Revise mt6370_chg_psy_desc
   - Add '.name = "mt6370-charger"'
   - Use 'static const'

- In patch 11/13:
- Remove the 'ko' from mt6370 led Kconfig description.
- Add both authors for Alice and ChiYuan.
- Use pdata to distinguish the code from mt6370/71 to mt6372.
- Instead of 'state' define, use the 'state' enum.
- Fix the typo for 'MT6372_PMW_DUTY'.
- For pwm_duty define, replace with bit macro - 1.
- Refine all the labels from 'out' to 'out_unlock'.
- Use struct 'dev' variable and 'dev_err_probe' to optimize the LOC.
- Revise for the array initialization from {0} to {}.
- Move into rgb folder and rename file name to 'leds-mt6370-rgb'.
- Refine the 'comma' usage in struct/enum.

- In patch 12/13:
- Use 'GENMASK' instead of 'BIT'.
- Use dev_err_probe to decrease LOC.
- Use 'dev' variable to make probe function more clean.
- Refine the return of _mt6370_flash_brightness_set function.
- Refine the descriptions.
- Use mt6370_clamp() instead of clamp_align().
- Use device resource managed API for v4l2 flash_release.


Changes in v5:
- In patch 07/13:
- Add the comma in the last REGMAP_IRQ_REG_LINE(),
   DEFINE_RES_IRQ_NAMED() and MFD_CELL_RES()
- Add the prefix in the first parameter of all mfd_cell
- Move enum and struct mt6370_info to mt6370.h
- Remove struct device *dev in struct mt6370_info
- Revise the description of Kconfig help text
- Revise MODULE_DESCRIPTION()

- In patch 08/13:
- Add comma for the last index of mt6370_reg_init.
- Use dev_err_probe to decrease LOC.
- Use 'dev' variable to make probe function more clean.
- Refine kconfig text.
- Remove both 'else' in set_vbus callback.
- Remove comma for of_device_id if the assigned member is only one.

- In patch 09/13:
- Replace using snprintf() with sysfs_emit() in mt6370_adc_read_label()
- Remove macro ADC_CONV_TIME_US
- Revise all variable ordering
- Revise the description of Kconfig help text
- Revise MODULE_DESCRIPTION()

- In patch 10/13:
- 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

- In patch 12/13:
- Refine the coding style.
- Use "dev" instead of ">dev".

- In patch 13/13:
- Add

Re: [PATCH v5 13/13] video: backlight: mt6370: Add MediaTek MT6370 support

2022-07-18 Thread ChiaEn Wu
On Mon, Jul 18, 2022 at 4:27 PM AngeloGioacchino Del Regno
 wrote:
>



> >>
> >> Hello ChiaEn,
> >>
> >> I propose to move this one to drivers/leds (or drivers/pwm) and, instead of
> >> registering a backlight device, register a PWM device.
> >>
> >> This way you will be able to reuse the generic backlight-pwm driver, as 
> >> you'd
> >> be feeding the PWM device exposed by this driver to the generic one: this 
> >> will
> >> most importantly make it easy to chain it with MTK_DISP_PWM (mtk-pwm-disp)
> >> with a devicetree that looks like...
> >
> > Out of interest, does MT6370 have the same structure for backlights as the 
> > prior
> > systems using mtk-pwm-disp or was mtk-pwm-disp simply a normal(-ish) PWM
> > that relied on something on the board for all the constant current
> > driver hardware?
> >
> >
>
> As per my understanding, mtk-pwm-disp is chained to other multimedia features 
> of
> the display block of MediaTek SoCs, such as the AAL (adaptive ambient light),
> CABC (content adaptive backlight control) etc, other than being a normal(ish)
> PWM... that's the reason of my request.
>
> Moreover, in the end, this PMIC's backlight controller is just a "fancy" PWM
> controller, with OCP/OVP.
>
> >>
> >>  pwmleds-disp {
> >>  compatible = "pwm-leds";
> >>
> >>  disp_led: disp-pwm {
> >>  label = "backlight-pwm";
> >>  pwms = < 0 50>;
> >>  max-brightness = <1024>;
> >>  };
> >>  };
> >>
> >>  backlight_lcd0: backlight {
> >>  compatible = "led-backlight";
> >>  leds = <_led>, <_bl_led>;
> >>  default-brightness-level = <300>;
> >>  };
> >
> > I think this proposal has to start with the devicetree bindings rather
> > than the driver. Instead I think the question is: does this proposal
> > result in DT bindings that better describe the underlying hardware?
> >
>
>  From how I understand it - yes: we have a fancy PWM () that we use
> to control display backlight (backlight-pwm)...
>
> Obviously, here we're not talking about OLEDs, but LCDs, where the backlight
> is made of multiple strings of WhiteLED (effectively, a "pwm-leds" controlled
> "led-backlight").
>
> Using PWM will also allow for a little more fine-grained board specific
> configuration, as I think that this PMIC (and/or variants of it) will be
> used in completely different form factors: I think that's going to be both
> smartphones and tablets/laptops... and I want to avoid vendor properties
> to configure the PWM part in a somehow different way.
>
> > This device has lots of backlight centric features (OCP, OVP, single
> > control with multiple outputs, exponential curves, etc) and its not
> > clear where they would fit into the "PWM" bindings.
> >
>
> For OCP and OVP, the only bindings that fit would be regulators, but that's
> not a regulator... and that's about it - I don't really have arguments for
> that.
>
> What I really want to see here is usage of "generic" drivers like led_bl
> and/or pwm_bl as to get some "standardization" around with all the benefits
> that this carries.
>
> > Come to think of it I'm also a little worried also about the whole linear
> > versus exponential curve thing since I thought LED drivers were required
> > to use exponential curves.
> >
>
> That probably depends on how the controller interprets the data, I guess,
> but I agree with you on this thought.

Hi Angelo,

MT6370 is just a SubPMIC, not an SoC, and is applied in cellular
telephones, tablet PCs, and portable instruments.
And the PWM mode of the MT6370 backlight driver is optional, and not
must be enabled.
>From our perspective, this MT6370 backlight driver is not the same as
mtk-pwm-disp related driver.
Thanks!

>
> Regards,
> Angelo

-- 
Best Regards,
ChiaEn Wu


[PATCH v5 13/13] video: backlight: mt6370: Add MediaTek MT6370 support

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 support for MediaTek MT6370 Backlight driver. It's commonly used
to drive the display WLED. There are 4 channels inside, and each channel
supports up to 30mA of current capability with 2048 current steps in
exponential or linear mapping curves.

Signed-off-by: ChiaEn Wu 
---

v5
- Add missed 
- Add struct device *dev in probe() to make code cleaning
- Remove useless including header file , 
- Remove useless variable uasage in mt6370_init_backlight_properties()
- Remove redundant checking enable_gpio in mt6370_bl_update_status()
- Remove redundant parentheses in mt6370_bl_get_brightness()
- Revise the description of Kconfig help text
- Revise the calculation of hys_th_steps
---
 drivers/video/backlight/Kconfig|  12 +
 drivers/video/backlight/Makefile   |   1 +
 drivers/video/backlight/mt6370-backlight.c | 339 +
 3 files changed, 352 insertions(+)
 create mode 100644 drivers/video/backlight/mt6370-backlight.c

diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig
index a003e02..846dbe7 100644
--- a/drivers/video/backlight/Kconfig
+++ b/drivers/video/backlight/Kconfig
@@ -268,6 +268,18 @@ config BACKLIGHT_MAX8925
  If you have a LCD backlight connected to the WLED output of MAX8925
  WLED output, say Y here to enable this driver.
 
+config BACKLIGHT_MT6370
+   tristate "MediaTek MT6370 Backlight Driver"
+   depends on MFD_MT6370
+   help
+ This enables support for Mediatek MT6370 Backlight driver.
+ It's commonly used to drive the display WLED. There are 4 channels
+ inside, and each channel supports up to 30mA of current capability
+ with 2048 current steps in exponential or linear mapping curves.
+
+ This driver can also be built as a module. If so, the module
+ will be called "mt6370-backlight".
+
 config BACKLIGHT_APPLE
tristate "Apple Backlight Driver"
depends on X86 && ACPI
diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile
index cae2c83..e815f3f 100644
--- a/drivers/video/backlight/Makefile
+++ b/drivers/video/backlight/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_BACKLIGHT_LP855X)+= lp855x_bl.o
 obj-$(CONFIG_BACKLIGHT_LP8788) += lp8788_bl.o
 obj-$(CONFIG_BACKLIGHT_LV5207LP)   += lv5207lp.o
 obj-$(CONFIG_BACKLIGHT_MAX8925)+= max8925_bl.o
+obj-$(CONFIG_BACKLIGHT_MT6370) += mt6370-backlight.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)  += omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PANDORA)+= pandora_bl.o
 obj-$(CONFIG_BACKLIGHT_PCF50633)   += pcf50633-backlight.o
diff --git a/drivers/video/backlight/mt6370-backlight.c 
b/drivers/video/backlight/mt6370-backlight.c
new file mode 100644
index 000..ba00a8f
--- /dev/null
+++ b/drivers/video/backlight/mt6370-backlight.c
@@ -0,0 +1,339 @@
+// 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 
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_BL_EN   0x1A0
+#define MT6370_REG_BL_BSTCTRL  0x1A1
+#define MT6370_REG_BL_PWM  0x1A2
+#define MT6370_REG_BL_DIM2 0x1A4
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+#define MT6370_BL_EXT_EN_MASK  BIT(7)
+#define MT6370_BL_EN_MASK  BIT(6)
+#define MT6370_BL_CONFIG_MASK  BIT(0)
+#define MT6370_BL_CH_MASK  GENMASK(5, 2)
+#define MT6370_BL_DIM2_MASKGENMASK(2, 0)
+#define MT6370_BL_DUMMY_6372_MASK  GENMASK(2, 0)
+#define MT6370_BL_DIM2_6372_SHIFT  3
+#define MT6370_BL_PWM_EN_MASK  BIT(7)
+#define MT6370_BL_PWM_HYS_EN_MASK  BIT(2)
+#define MT6370_BL_PWM_HYS_SEL_MASK GENMASK(1, 0)
+#define MT6370_BL_OVP_EN_MASK  BIT(7)
+#define MT6370_BL_OVP_SEL_MASK GENMASK(6, 5)
+#define MT6370_BL_OC_EN_MASK   BIT(3)
+#define MT6370_BL_OC_SEL_MASK  GENMASK(2, 1)
+
+#define MT6370_BL_PWM_HYS_TH_MIN_STEP  1
+#define MT6370_BL_PWM_HYS_TH_MAX_STEP  64
+#define MT6370_BL_OVP_MIN_UV   1700
+#define MT6370_BL_OVP_MAX_UV   2900
+#define MT6370_BL_OVP_STEP_UV  400
+#define MT6370_BL_OCP_MIN_UA   90
+#define MT6370_BL_OCP_MAX_UA   180
+#define MT6370_BL_OCP_STEP_UA  30
+#define MT6370_BL_MAX_BRIGHTNESS   2048
+#define MT6370_BL_MAX_CH   15
+
+enum {
+   MT6370_VID_COMMON = 0,
+   MT6370_VID_637

[PATCH v5 12/13] leds: flashlight: mt6370: Add MediaTek MT6370 flashlight support

2022-07-15 Thread ChiaEn Wu
From: Alice Chen 

The MediaTek MT6370 is a highly-integrated smart power management IC,
whichincludes 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.

The Flash LED in MT6370 has 2 channel and support torch/strobe mode.
The commit add the support of MT6370 FLASH LED.

Signed-off-by: Alice Chen 
---

v5
- Refine descriptions.
- Refine the macro name.
- Refine the brackets and blanks.
---
 drivers/leds/flash/Kconfig |  12 +
 drivers/leds/flash/Makefile|   1 +
 drivers/leds/flash/leds-mt6370-flash.c | 661 +
 3 files changed, 674 insertions(+)
 create mode 100644 drivers/leds/flash/leds-mt6370-flash.c

diff --git a/drivers/leds/flash/Kconfig b/drivers/leds/flash/Kconfig
index d3eb689..d5761ed 100644
--- a/drivers/leds/flash/Kconfig
+++ b/drivers/leds/flash/Kconfig
@@ -90,4 +90,16 @@ config LEDS_SGM3140
  This option enables support for the SGM3140 500mA Buck/Boost Charge
  Pump LED Driver.
 
+config LEDS_MT6370_FLASHLIGHT
+   tristate "Flash LED Support for MediaTek MT6370 PMIC"
+   depends on LEDS_CLASS
+   depends on MFD_MT6370
+   help
+ Support 2 channels and torch/strobe mode.
+ Say Y here to enable support for
+ MT6370_FLASH_LED device.
+
+ This driver can also be built as a module. If so, the module
+ will be called "leds-mt6370-flash".
+
 endif # LEDS_CLASS_FLASH
diff --git a/drivers/leds/flash/Makefile b/drivers/leds/flash/Makefile
index 0acbddc..4c4c171 100644
--- a/drivers/leds/flash/Makefile
+++ b/drivers/leds/flash/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_LEDS_MAX77693) += leds-max77693.o
 obj-$(CONFIG_LEDS_RT4505)  += leds-rt4505.o
 obj-$(CONFIG_LEDS_RT8515)  += leds-rt8515.o
 obj-$(CONFIG_LEDS_SGM3140) += leds-sgm3140.o
+obj-$(CONFIG_LEDS_MT6370_FLASHLIGHT)   += leds-mt6370-flash.o
diff --git a/drivers/leds/flash/leds-mt6370-flash.c 
b/drivers/leds/flash/leds-mt6370-flash.c
new file mode 100644
index 000..eccda69
--- /dev/null
+++ b/drivers/leds/flash/leds-mt6370-flash.c
@@ -0,0 +1,661 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_FLASH1,
+   MT6370_LED_FLASH2,
+   MT6370_MAX_LEDS
+};
+
+/* Virtual definition for multicolor */
+
+#define MT6370_REG_FLEDEN  0x17E
+#define MT6370_REG_STRBTO  0x173
+#define MT6370_REG_CHGSTAT20x1D1
+#define MT6370_REG_FLEDSTAT1   0x1D9
+#defineMT6370_REG_FLEDISTRB(_id)   (0x174 + 4 * _id)
+#define MT6370_REG_FLEDITOR(_id)   (0x175 + 4 * _id)
+#define MT6370_ITORCH_MASK GENMASK(4, 0)
+#define MT6370_ISTROBE_MASKGENMASK(6, 0)
+#define MT6370_STRBTO_MASK GENMASK(6, 0)
+#define MT6370_TORCHEN_MASKBIT(3)
+#define MT6370_STROBEN_MASKBIT(2)
+#define MT6370_FLCSEN_MASK(_id)BIT(MT6370_LED_FLASH2 - _id)
+#define MT6370_FLCSEN_MASK_ALL (BIT(0) | BIT(1))
+#define MT6370_FLEDCHGVINOVP_MASK  BIT(3)
+#define MT6370_FLED1STRBTO_MASKBIT(11)
+#define MT6370_FLED2STRBTO_MASKBIT(10)
+#define MT6370_FLED1STRB_MASK  BIT(9)
+#define MT6370_FLED2STRB_MASK  BIT(8)
+#define MT6370_FLED1SHORT_MASK BIT(7)
+#define MT6370_FLED2SHORT_MASK BIT(6)
+#define MT6370_FLEDLVF_MASKBIT(3)
+
+#define MT6370_LED_JOINT   2
+#define MT6370_RANGE_FLED_REG  4
+#define MT6370_ITORCH_MIN_UA   25000
+#define MT6370_ITORCH_STEP_UA  12500
+#define MT6370_ITORCH_MAX_UA   40
+#define MT6370_ITORCH_DOUBLE_MAX_UA80
+#define MT6370_ISTRB_MIN_UA5
+#define MT6370_ISTRB_STEP_UA   12500
+#define MT6370_ISTRB_MAX_UA150
+#define MT6370_ISTRB_DOUBLE_MAX_UA 300
+#define MT6370_STRBTO_MIN_US   64000
+#define MT6370_STRBTO_STEP_US  32000
+#define MT6370_STRBTO_MAX_US   2432000
+
+#define STATE_OFF  0
+#define STATE_KEEP 1
+#define STATE_ON   2
+
+#define to_mt6370_led(ptr, member) container_of(ptr, struct mt6370_led, member)
+
+struct mt6370_led {
+   struct led_classdev_flash flash;
+   struct v4l2_flash *v4l2_flash;
+   struct mt6370_priv *priv;
+   u32 led_no;
+   u32 default_state;
+};
+
+struct mt6370_priv {
+   struct device *dev;
+   struct regmap *regmap;
+   struct mutex lock;
+   unsigned int fled_strobe_used;
+   unsigned int fled_torch_used;
+   unsigned int leds_active;
+   unsigned int 

[PATCH v5 11/13] leds: mt6370: Add MediaTek MT6370 current sink type LED Indicator support

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

In MediaTek MT6370, there are four channel current-sink RGB LEDs that
support hardware pattern for constant current, PWM, and breath mode.
Isink4 channel can also be used as a CHG_VIN power good indicator.

Signed-off-by: ChiYuan Huang 
---
 drivers/leds/Kconfig   |  14 +
 drivers/leds/Makefile  |   1 +
 drivers/leds/leds-mt6370.c | 994 +
 3 files changed, 1009 insertions(+)
 create mode 100644 drivers/leds/leds-mt6370.c

diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index a49979f..71bacb5 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -244,6 +244,20 @@ config LEDS_MT6323
  This option enables support for on-chip LED drivers found on
  Mediatek MT6323 PMIC.
 
+config LEDS_MT6370_RGB
+   tristate "LED Support for MediaTek MT6370 PMIC"
+   depends on LEDS_CLASS
+   depends on MFD_MT6370
+   select LINEAR_RANGE
+   help
+ Say Y here to enable support for MT6370_RGB LED device.
+ In MT6370, there are four channel current-sink LED drivers that
+ support hardware pattern for constant current, PWM, and breath mode.
+ Isink4 channel can also be used as a CHG_VIN power good indicator.
+
+ This driver can also be built as a module. If so the module
+ will be called "leds-mt6370.ko".
+
 config LEDS_S3C24XX
tristate "LED Support for Samsung S3C24XX GPIO LEDs"
depends on LEDS_CLASS
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index 4fd2f92..557be42 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -62,6 +62,7 @@ obj-$(CONFIG_LEDS_MIKROTIK_RB532) += leds-rb532.o
 obj-$(CONFIG_LEDS_MLXCPLD) += leds-mlxcpld.o
 obj-$(CONFIG_LEDS_MLXREG)  += leds-mlxreg.o
 obj-$(CONFIG_LEDS_MT6323)  += leds-mt6323.o
+obj-$(CONFIG_LEDS_MT6370_RGB)  += leds-mt6370.o
 obj-$(CONFIG_LEDS_NET48XX) += leds-net48xx.o
 obj-$(CONFIG_LEDS_NETXBIG) += leds-netxbig.o
 obj-$(CONFIG_LEDS_NIC78BX) += leds-nic78bx.o
diff --git a/drivers/leds/leds-mt6370.c b/drivers/leds/leds-mt6370.c
new file mode 100644
index 000..1038232
--- /dev/null
+++ b/drivers/leds/leds-mt6370.c
@@ -0,0 +1,994 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+enum {
+   MT6370_LED_ISNK1 = 0,
+   MT6370_LED_ISNK2,
+   MT6370_LED_ISNK3,
+   MT6370_LED_ISNK4,
+   MT6370_MAX_LEDS
+};
+
+enum mt6370_led_mode {
+   MT6370_LED_PWM_MODE = 0,
+   MT6370_LED_BREATH_MODE,
+   MT6370_LED_REG_MODE,
+   MT6370_LED_MAX_MODE
+};
+
+enum mt6370_led_field {
+   F_RGB_EN = 0,
+   F_CHGIND_EN,
+   F_LED1_CURR,
+   F_LED2_CURR,
+   F_LED3_CURR,
+   F_LED4_CURR,
+   F_LED1_MODE,
+   F_LED2_MODE,
+   F_LED3_MODE,
+   F_LED4_MODE,
+   F_LED1_DUTY,
+   F_LED2_DUTY,
+   F_LED3_DUTY,
+   F_LED4_DUTY,
+   F_LED1_FREQ,
+   F_LED2_FREQ,
+   F_LED3_FREQ,
+   F_LED4_FREQ,
+   F_MAX_FIELDS
+};
+
+enum mt6370_led_ranges {
+   R_LED123_CURR = 0,
+   R_LED4_CURR,
+   R_LED_TRFON,
+   R_LED_TOFF,
+   R_MAX_RANGES,
+};
+
+enum mt6370_pattern {
+   P_LED_TR1 = 0,
+   P_LED_TR2,
+   P_LED_TF1,
+   P_LED_TF2,
+   P_LED_TON,
+   P_LED_TOFF,
+   P_MAX_PATTERNS
+};
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_RGB1_DIM0x182
+#define MT6370_REG_RGB2_DIM0x183
+#define MT6370_REG_RGB3_DIM0x184
+#define MT6370_REG_RGB_EN  0x185
+#define MT6370_REG_RGB1_ISNK   0x186
+#define MT6370_REG_RGB2_ISNK   0x187
+#define MT6370_REG_RGB3_ISNK   0x188
+#define MT6370_REG_RGB1_TR 0x189
+#define MT6370_REG_RGB_CHRIND_DIM  0x192
+#define MT6370_REG_RGB_CHRIND_CTRL 0x193
+#define MT6370_REG_RGB_CHRIND_TR   0x194
+
+#define MT6372_REG_RGB_EN  0x182
+#define MT6372_REG_RGB1_ISNK   0x183
+#define MT6372_REG_RGB2_ISNK   0x184
+#define MT6372_REG_RGB3_ISNK   0x185
+#define MT6372_REG_RGB4_ISNK   0x186
+#define MT6372_REG_RGB1_DIM0x187
+#define MT6372_REG_RGB2_DIM0x188
+#define MT6372_REG_RGB3_DIM0x189
+#define 

[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,
+   /* MT63

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

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 support the MT6370 ADC driver for system monitoring, including
charger current, voltage, and temperature.

Signed-off-by: ChiaEn Wu 
---

v5
- Replace using snprintf() with sysfs_emit() in mt6370_adc_read_label()
- Remove macro ADC_CONV_TIME_US
- Revise all variable ordering
- Revise the description of Kconfig help text
- Revise MODULE_DESCRIPTION()
---
 drivers/iio/adc/Kconfig  |  12 ++
 drivers/iio/adc/Makefile |   1 +
 drivers/iio/adc/mt6370-adc.c | 273 +++
 3 files changed, 286 insertions(+)
 create mode 100644 drivers/iio/adc/mt6370-adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 48ace74..60bcc28 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -737,6 +737,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 39d806f..0ce285c 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..51ef133
--- /dev/null
+++ b/drivers/iio/adc/mt6370-adc.c
@@ -0,0 +1,273 @@
+// 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 
+
+#define MT6370_REG_CHG_CTRL3   0x113 /* AICR */
+#define MT6370_REG_CHG_CTRL7   0x117 /* ICHG */
+#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_400MA  0x6
+#define MT6370_ICHG_500MA  0x4
+#define MT6370_ICHG_900MA  0x8
+
+#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 * 1000 * 3);
+   if (ret) {
+   dev_err(priv->dev, "Failed to read ADC register (%d)\n", ret);
+   goto adc_unlock;
+   }
+
+   ret = regmap_raw_read(priv->regmap, MT6370_REG_ADC_DATA_H,
+ _val, sizeof(be_val));
+   if (ret)
+   goto adc_unlock;
+
+   *val = be16_to_cpu(be_val);
+   ret = IIO_VAL_INT;
+
+adc_unlock:
+   mutex_unlock(>adc_lock);
+
+   return ret;
+}
+
+static int mt6370_adc_read_scale(struct mt6370_ad

[PATCH v5 08/13] usb: typec: tcpci_mt6370: Add MediaTek MT6370 tcpci driver

2022-07-15 Thread ChiaEn Wu
From: ChiYuan Huang 

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

This commit add support for the Type-C & Power Delivery controller in
MediaTek MT6370 IC.

Signed-off-by: ChiYuan Huang 
---

v5
- Add comma for the last index of mt6370_reg_init.
- Use dev_err_probe to decrease LOC.
- Use 'dev' variable to make probe function more clean.
- Refine kconfig text.
- Remove both 'else' in set_vbus callback.
- Remove comma for of_device_id if the assigned member is only one.
---
 drivers/usb/typec/tcpm/Kconfig|  11 ++
 drivers/usb/typec/tcpm/Makefile   |   1 +
 drivers/usb/typec/tcpm/tcpci_mt6370.c | 207 ++
 3 files changed, 219 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm/tcpci_mt6370.c

diff --git a/drivers/usb/typec/tcpm/Kconfig b/drivers/usb/typec/tcpm/Kconfig
index 073fd2e..e6b88ca 100644
--- a/drivers/usb/typec/tcpm/Kconfig
+++ b/drivers/usb/typec/tcpm/Kconfig
@@ -35,6 +35,17 @@ config TYPEC_MT6360
  USB Type-C. It works with Type-C Port Controller Manager
  to provide USB PD and USB Type-C functionalities.
 
+config TYPEC_TCPCI_MT6370
+   tristate "MediaTek MT6370 Type-C driver"
+   depends on MFD_MT6370
+   help
+ MediaTek MT6370 is a multi-functional IC that includes
+ USB Type-C. It works with Type-C Port Controller Manager
+ to provide USB PD and USB Type-C functionalities.
+
+ This driver can also be built as a module. The module
+ will be called "tcpci_mt6370".
+
 config TYPEC_TCPCI_MAXIM
tristate "Maxim TCPCI based Type-C chip driver"
help
diff --git a/drivers/usb/typec/tcpm/Makefile b/drivers/usb/typec/tcpm/Makefile
index 7d499f3..906d9dc 100644
--- a/drivers/usb/typec/tcpm/Makefile
+++ b/drivers/usb/typec/tcpm/Makefile
@@ -6,4 +6,5 @@ typec_wcove-y   := wcove.o
 obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
 obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
 obj-$(CONFIG_TYPEC_MT6360) += tcpci_mt6360.o
+obj-$(CONFIG_TYPEC_TCPCI_MT6370)   += tcpci_mt6370.o
 obj-$(CONFIG_TYPEC_TCPCI_MAXIM)+= tcpci_maxim.o
diff --git a/drivers/usb/typec/tcpm/tcpci_mt6370.c 
b/drivers/usb/typec/tcpm/tcpci_mt6370.c
new file mode 100644
index 000..a08b0ba
--- /dev/null
+++ b/drivers/usb/typec/tcpm/tcpci_mt6370.c
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "tcpci.h"
+
+#define MT6370_REG_SYSCTRL80x9B
+
+#define MT6370_AUTOIDLE_MASK   BIT(3)
+
+#define MT6370_VENDOR_ID   0x29CF
+#define MT6370_TCPC_DID_A  0x2170
+
+struct mt6370_priv {
+   struct device *dev;
+   struct regulator *vbus;
+   struct tcpci *tcpci;
+   struct tcpci_data tcpci_data;
+   int irq;
+};
+
+static const struct reg_sequence mt6370_reg_init[] = {
+   REG_SEQ(0xA0, 0x1, 1000),
+   REG_SEQ(0x81, 0x38, 0),
+   REG_SEQ(0x82, 0x82, 0),
+   REG_SEQ(0xBA, 0xFC, 0),
+   REG_SEQ(0xBB, 0x50, 0),
+   REG_SEQ(0x9E, 0x8F, 0),
+   REG_SEQ(0xA1, 0x5, 0),
+   REG_SEQ(0xA2, 0x4, 0),
+   REG_SEQ(0xA3, 0x4A, 0),
+   REG_SEQ(0xA4, 0x01, 0),
+   REG_SEQ(0x95, 0x01, 0),
+   REG_SEQ(0x80, 0x71, 0),
+   REG_SEQ(0x9B, 0x3A, 1000),
+};
+
+static int mt6370_tcpc_init(struct tcpci *tcpci, struct tcpci_data *data)
+{
+   u16 did;
+   int ret;
+
+   ret = regmap_register_patch(data->regmap, mt6370_reg_init,
+   ARRAY_SIZE(mt6370_reg_init));
+   if (ret)
+   return ret;
+
+   ret = regmap_raw_read(data->regmap, TCPC_BCD_DEV, , sizeof(u16));
+   if (ret)
+   return ret;
+
+   if (did == MT6370_TCPC_DID_A)
+   return regmap_write(data->regmap, TCPC_FAULT_CTRL, 0x80);
+
+   return 0;
+}
+
+static int mt6370_tcpc_set_vconn(struct tcpci *tcpci, struct tcpci_data *data,
+bool enable)
+{
+   return regmap_update_bits(data->regmap, MT6370_REG_SYSCTRL8,
+ MT6370_AUTOIDLE_MASK,
+ !enable ? MT6370_AUTOIDLE_MASK : 0);
+}
+
+static int mt6370_tcpc_set_vbus(struct tcpci *tcpci, struct tcpci_data *data,
+   bool source, bool sink)
+{
+   struct mt6370_priv *priv = container_of(data, struct mt6370_priv,
+   tcpci_data);
+   int ret;
+
+   ret = regulator_is_enabled(priv->vbus);
+   if (ret < 0)
+   return 

[PATCH v5 07/13] mfd: mt6370: Add MediaTek MT6370 support

2022-07-15 Thread ChiaEn Wu
From: ChiYuan Huang 

This adds support for the MediaTek MT6370 SubPMIC. 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.

Signed-off-by: ChiYuan Huang 
---
v5

- Add the comma in the last REGMAP_IRQ_REG_LINE(),
  DEFINE_RES_IRQ_NAMED() and MFD_CELL_RES()
- Add the prefix in the first parameter of all mfd_cell
- Move enum and struct mt6370_info to mt6370.h
- Remove struct device *dev in struct mt6370_info
- Revise the description of Kconfig help text
- Revise MODULE_DESCRIPTION()
---
 drivers/mfd/Kconfig  |  16 +++
 drivers/mfd/Makefile |   1 +
 drivers/mfd/mt6370.c | 281 +++
 drivers/mfd/mt6370.h |  99 ++
 4 files changed, 397 insertions(+)
 create mode 100644 drivers/mfd/mt6370.c
 create mode 100644 drivers/mfd/mt6370.h

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 3b59456..a9bcae3 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -937,6 +937,22 @@ config MFD_MT6360
  PMIC part includes 2-channel BUCKs and 2-channel LDOs
  LDO part includes 4-channel LDOs
 
+config MFD_MT6370
+   tristate "MediaTek MT6370 SubPMIC"
+   select MFD_CORE
+   select REGMAP_I2C
+   select REGMAP_IRQ
+   depends on I2C
+   help
+ Say Y here to enable MT6370 SubPMIC functional support.
+ It consists 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 driver can also be built as a module. If so, the module
+ will be called "mt6370".
+
 config MFD_MT6397
tristate "MediaTek MT6397 PMIC Support"
select MFD_CORE
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 8c69867..81dbed3 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -173,6 +173,7 @@ obj-$(CONFIG_MFD_MAX8998)   += max8998.o max8998-irq.o
 obj-$(CONFIG_MFD_MP2629)   += mp2629.o
 
 obj-$(CONFIG_MFD_MT6360)   += mt6360-core.o
+obj-$(CONFIG_MFD_MT6370)   += mt6370.o
 mt6397-objs:= mt6397-core.o mt6397-irq.o mt6358-irq.o
 obj-$(CONFIG_MFD_MT6397)   += mt6397.o
 
diff --git a/drivers/mfd/mt6370.c b/drivers/mfd/mt6370.c
new file mode 100644
index 000..070c1c7
--- /dev/null
+++ b/drivers/mfd/mt6370.c
@@ -0,0 +1,281 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "mt6370.h"
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_CHG_IRQ10x1C0
+#define MT6370_REG_CHG_MASK1   0x1E0
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+
+#define MT6370_NUM_IRQREGS 16
+#define MT6370_USBC_I2CADDR0x4E
+#define MT6370_REG_ADDRLEN 2
+#define MT6370_REG_MAXADDR 0x1FF
+
+#define MT6370_VENID_RT50810x8
+#define MT6370_VENID_RT5081A   0xA
+#define MT6370_VENID_MT63700xE
+#define MT6370_VENID_MT63710xF
+#define MT6370_VENID_MT6372P   0x9
+#define MT6370_VENID_MT6372CP  0xB
+
+static const struct regmap_irq mt6370_irqs[] = {
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_DIRCHGON, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TREG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_AICR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_MIVR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_PWR_RDY, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_FL_CHG_VINOVP, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VSYSUV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VSYSOV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VBATOV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VINOVPCHG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_COLD, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_COOL, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_WARM, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_HOT, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_STATC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_FAULT, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_STATC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TMR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_BATABS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_ADPBAD, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_RVP, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TSHUTDOWN, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_IINMEAS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_ICCMEAS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHGDET_DONE, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_WDTMR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_SSFINISH, 

[PATCH v5 06/13] dt-bindings: mfd: Add MediaTek MT6370

2022-07-15 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Krzysztof Kozlowski 
---
 .../devicetree/bindings/mfd/mediatek,mt6370.yaml   | 280 +
 include/dt-bindings/iio/adc/mediatek,mt6370_adc.h  |  18 ++
 2 files changed, 298 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
 create mode 100644 include/dt-bindings/iio/adc/mediatek,mt6370_adc.h

diff --git a/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml 
b/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
new file mode 100644
index 000..410e2d4
--- /dev/null
+++ b/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
@@ -0,0 +1,280 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/mediatek,mt6370.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 SubPMIC
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  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.
+
+properties:
+  compatible:
+const: mediatek,mt6370
+
+  reg:
+maxItems: 1
+
+  wakeup-source: true
+
+  interrupts:
+maxItems: 1
+
+  interrupt-controller: true
+
+  "#interrupt-cells":
+const: 1
+
+  adc:
+type: object
+description: |
+  Provides 9 channels for system monitoring, including VBUSDIV5 (lower
+  accuracy, higher measure range), VBUSDIV2 (higher accuracy, lower
+  measure range), VBAT, VSYS, CHG_VDDP, TS_BAT, IBUS, IBAT, and TEMP_JC.
+
+properties:
+  compatible:
+const: mediatek,mt6370-adc
+
+  "#io-channel-cells":
+const: 1
+
+required:
+  - compatible
+  - "#io-channel-cells"
+
+  backlight:
+type: object
+$ref: /schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+
+  charger:
+type: object
+$ref: /schemas/power/supply/mediatek,mt6370-charger.yaml#
+
+  tcpc:
+type: object
+$ref: /schemas/usb/mediatek,mt6370-tcpc.yaml#
+
+  indicator:
+type: object
+$ref: /schemas/leds/mediatek,mt6370-indicator.yaml#
+
+  flashlight:
+type: object
+$ref: /schemas/leds/mediatek,mt6370-flashlight.yaml#
+
+  regulators:
+type: object
+description: |
+  List all supported regulators, which support the control for DisplayBias
+  voltages and one general purpose LDO which commonly used to drive the
+  vibrator.
+
+patternProperties:
+  "^(dsvbst|vibldo)$":
+$ref: /schemas/regulator/regulator.yaml#
+type: object
+unevaluatedProperties: false
+
+  "^(dsvpos|dsvneg)$":
+$ref: /schemas/regulator/regulator.yaml#
+type: object
+unevaluatedProperties: false
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - interrupt-controller
+  - "#interrupt-cells"
+  - regulators
+  - adc
+  - backlight
+  - indicator
+  - tcpc
+  - charger
+  - flashlight
+
+additionalProperties: false
+
+examples:
+  - |
+#include 
+#include 
+#include 
+#include 
+i2c {
+  #address-cells = <1>;
+  #size-cells = <0>;
+
+pmic@34 {
+compatible = "mediatek,mt6370";
+reg = <0x34>;
+wakeup-source;
+interrupts-extended = < 3 IRQ_TYPE_LEVEL_LOW>;
+interrupt-controller;
+#interrupt-cells = <1>;
+
+mt6370_adc: adc {
+  compatible = "mediatek,mt6370-adc";
+  #io-channel-cells = <1>;
+};
+
+backlight {
+  compatible = "mediatek,mt6370-backlight";
+  mediatek,bled-channel-use = /bits/ 8 <15>;
+};
+
+charger {
+  compatible = "mediatek,mt6370-charger";
+  interrupts = <48>, <68>, <6>;
+  interrupt-names = "attach_i", "uvp_d_evt", "mivr";
+  io-channels = <_adc MT6370_CHAN_IBUS>;
+
+  mt6370_otg_vbus: usb-otg-vbus-regulator {
+regulator-name = "mt6370-usb-otg-vbus";
+regulator-min-microvolt = <435>;
+regulator-max-microvolt = <580>;
+regulator-min-microamp = <50>;
+regulator-max-microamp = <300>;
+  };
+};
+
+indicator {
+  compatible = "mediatek,mt6370-indicator";
+  #address-cells = <1>;
+  #size-cells = <0>;
+
+  multi-led@0 {
+reg = <0>;
+function = LED_FUNCTION_INDICATOR;
+color = ;
+led-max-microamp = <24000>;
+#address-cells = <1>;
+#size-cells = <0>;
+led@0 {
+  reg = <0>;
+  color = ;
+};
+led@1 {
+   

[PATCH v5 05/13] dt-bindings: backlight: Add MediaTek MT6370 backlight

2022-07-15 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MT6370 backlight binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Rob Herring 
---
 .../leds/backlight/mediatek,mt6370-backlight.yaml  | 92 ++
 1 file changed, 92 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
new file mode 100644
index 000..d674212
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
@@ -0,0 +1,92 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: 
http://devicetree.org/schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Backlight
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  The MT6370 Backlight WLED driver supports up to a 29V output voltage for
+  4 channels of 8 series WLEDs. Each channel supports up to 30mA of current
+  capability with 2048 current steps (11 bits) in exponential or linear
+  mapping curves.
+
+allOf:
+  - $ref: common.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-backlight
+
+  default-brightness:
+minimum: 0
+maximum: 2048
+
+  max-brightness:
+minimum: 0
+maximum: 2048
+
+  enable-gpios:
+description: External backlight 'enable' pin
+maxItems: 1
+
+  mediatek,bled-pwm-enable:
+description: |
+  Enable external PWM input for backlight dimming
+type: boolean
+
+  mediatek,bled-pwm-hys-enable:
+description: |
+  Enable the backlight input-hysteresis for PWM mode
+type: boolean
+
+  mediatek,bled-pwm-hys-input-th-steps:
+$ref: /schemas/types.yaml#/definitions/uint8
+enum: [1, 4, 16, 64]
+description: |
+  The selection of the upper and lower bounds threshold of backlight
+  PWM resolution. If we choose selection 64, the variation of PWM
+  resolution needs more than 64 steps.
+
+  mediatek,bled-ovp-shutdown:
+description: |
+  Enable the backlight shutdown when OVP level triggered
+type: boolean
+
+  mediatek,bled-ovp-microvolt:
+enum: [1700, 2100, 2500, 2900]
+description: |
+  Backlight OVP level selection.
+
+  mediatek,bled-ocp-shutdown:
+description: |
+  Enable the backlight shutdown when OCP level triggerred.
+type: boolean
+
+  mediatek,bled-ocp-microamp:
+enum: [90, 120, 150, 180]
+description: |
+  Backlight OC level selection.
+
+  mediatek,bled-channel-use:
+$ref: /schemas/types.yaml#/definitions/uint8
+description: |
+  Backlight LED channel to be used.
+  Each bit mapping to:
+- 0: CH4
+- 1: CH3
+- 2: CH2
+- 3: CH1
+minimum: 1
+maximum: 15
+
+required:
+  - compatible
+  - mediatek,bled-channel-use
+
+additionalProperties: false
-- 
2.7.4



[PATCH v5 04/13] dt-bindings: leds: Add MediaTek MT6370 flashlight

2022-07-15 Thread ChiaEn Wu
From: Alice Chen 

Add MediaTek MT6370 flashlight binding documentation.

Signed-off-by: Alice Chen 
Reviewed-by: Krzysztof Kozlowski 
---
 .../bindings/leds/mediatek,mt6370-flashlight.yaml  | 41 ++
 1 file changed, 41 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
new file mode 100644
index 000..e9d02ed
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-flashlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Flash LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 flash LED driver include 2-channel flash LED support Torch/Strobe 
Mode.
+
+properties:
+  compatible:
+const: mediatek,mt6370-flashlight
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-1]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1]
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v5 03/13] dt-bindings: leds: mt6370: Add MediaTek MT6370 current sink type LED indicator

2022-07-15 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 current sink type LED indicator binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Krzysztof Kozlowski 
---
 .../bindings/leds/mediatek,mt6370-indicator.yaml   | 77 ++
 1 file changed, 77 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
new file mode 100644
index 000..45030f3
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
@@ -0,0 +1,77 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-indicator.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 LED driver include 4-channel RGB LED support Register/PWM/Breath 
Mode
+
+allOf:
+  - $ref: leds-class-multicolor.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-indicator
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^multi-led@[0-3]$":
+type: object
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-2]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+required:
+  - reg
+  - color
+
+required:
+  - reg
+  - color
+  - "#address-cells"
+  - "#size-cells"
+
+  "^led@[0-3]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+required:
+  - reg
+  - color
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v5 02/13] dt-bindings: power: supply: Add MediaTek MT6370 Charger

2022-07-15 Thread ChiaEn Wu
From: ChiaEn Wu 

Add MediaTek MT6370 Charger binding documentation.

Signed-off-by: ChiaEn Wu 
Reviewed-by: Krzysztof Kozlowski 
---
 .../power/supply/mediatek,mt6370-charger.yaml  | 88 ++
 1 file changed, 88 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml

diff --git 
a/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
new file mode 100644
index 000..bd09a0a
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/mediatek,mt6370-charger.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Battery Charger
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Provides Battery Charger, Boost for OTG devices and BC1.2 detection.
+
+properties:
+  compatible:
+const: mediatek,mt6370-charger
+
+  interrupts:
+description: |
+  Specify what irqs are needed to be handled by MT6370 Charger driver. IRQ
+  "MT6370_IRQ_CHG_MIVR", "MT6370_IRQ_ATTACH" and "MT6370_IRQ_OVPCTRL_UVP_D"
+  are required.
+items:
+  - description: BC1.2 done irq
+  - description: usb plug in irq
+  - description: mivr irq
+
+  interrupt-names:
+items:
+  - const: attach_i
+  - const: uvp_d_evt
+  - const: mivr
+
+  io-channels:
+description: |
+  Use ADC channel to read VBUS, IBUS, IBAT, etc., info.
+minItems: 1
+items:
+  - description: |
+  VBUS voltage with lower accuracy (+-75mV) but higher measure
+  range (1~22V)
+  - description: |
+  VBUS voltage with higher accuracy (+-30mV) but lower measure
+  range (1~9.76V)
+  - description: the main system input voltage
+  - description: battery voltage
+  - description: battery temperature-sense input voltage
+  - description: IBUS current (required)
+  - description: battery current
+  - description: |
+  regulated output voltage to supply for the PWM low-side gate driver
+  and the bootstrap capacitor
+  - description: IC junction temperature
+
+  io-channel-names:
+minItems: 1
+items:
+  - const: vbusdiv5
+  - const: vbusdiv2
+  - const: vsys
+  - const: vbat
+  - const: ts_bat
+  - const: ibus
+  - const: ibat
+  - const: chg_vddp
+  - const: temp_jc
+
+  usb-otg-vbus-regulator:
+type: object
+description: OTG boost regulator.
+unevaluatedProperties: false
+$ref: /schemas/regulator/regulator.yaml#
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - interrupts
+  - interrupt-names
+  - io-channels
+
+additionalProperties: false
+
+...
-- 
2.7.4



[PATCH v5 01/13] dt-bindings: usb: Add MediaTek MT6370 TCPC

2022-07-15 Thread ChiaEn Wu
From: ChiYuan Huang 

Add MediaTek MT6370 TCPC binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Krzysztof Kozlowski 
---
 .../bindings/usb/mediatek,mt6370-tcpc.yaml | 36 ++
 1 file changed, 36 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml 
b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
new file mode 100644
index 000..72f56cc
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/mediatek,mt6370-tcpc.yaml#;
+$schema: "http://devicetree.org/meta-schemas/core.yaml#;
+
+title: MediatTek MT6370 Type-C Port Switch and Power Delivery controller
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  MediaTek MT6370 is a multi-functional device.
+  It integrates charger, ADC, flash, RGB indicators,
+  regulators (DSV/VIBLDO), and TypeC Port Switch with Power Delivery 
controller.
+  This document only describes MT6370 Type-C Port Switch and
+  Power Delivery controller.
+
+properties:
+  compatible:
+enum:
+  - mediatek,mt6370-tcpc
+
+  interrupts:
+maxItems: 1
+
+  connector:
+type: object
+$ref: /schemas/connector/usb-connector.yaml#
+unevaluatedProperties: false
+
+additionalProperties: false
+
+required:
+  - compatible
+  - interrupts
-- 
2.7.4



[PATCH v5 00/13] Add MediaTek MT6370 PMIC support

2022-07-15 Thread ChiaEn Wu
From: ChiaEn Wu 

This patch series add MediaTek MT6370 PMIC support. The 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.

In this series of patches, we based on Andy Shevchenko's mfd patch used to
adjust the Makefile order.
(https://lore.kernel.org/all/20220616182524.7956-2-andriy.shevche...@linux.intel.com/)
Among with this, we took some changes for MT6370 and refined the MT6370 device
tree files to comply with DT specifications.

"[PATCH v5 06/13] dt-bindings: mfd: Add MediaTek MT6370" depends on previous
DT binding patches, so before applying this patch, please apply other DT
patches first. Thanks!

Thank you,
ChiaEn Wu

---
Changes in v5:
- In patch 07/13:
- Add the comma in the last REGMAP_IRQ_REG_LINE(),
   DEFINE_RES_IRQ_NAMED() and MFD_CELL_RES()
- Add the prefix in the first parameter of all mfd_cell
- Move enum and struct mt6370_info to mt6370.h
- Remove struct device *dev in struct mt6370_info
- Revise the description of Kconfig help text
- Revise MODULE_DESCRIPTION()

- In patch 08/13:
- Add comma for the last index of mt6370_reg_init.
- Use dev_err_probe to decrease LOC.
- Use 'dev' variable to make probe function more clean.
- Refine kconfig text.
- Remove both 'else' in set_vbus callback.
- Remove comma for of_device_id if the assigned member is only one.

- In patch 09/13:
- Replace using snprintf() with sysfs_emit() in mt6370_adc_read_label()
- Remove macro ADC_CONV_TIME_US
- Revise all variable ordering
- Revise the description of Kconfig help text
- Revise MODULE_DESCRIPTION()

- In patch 10/13:
- 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

- In patch 12/13:
- Refine descriptions.
- Refine the macro name.
- Refine the bracket and blanks.

- In patch 13/13:
- Add missed 
- Add struct device *dev in probe() to make code cleaning
- Remove useless including header file , 
- Remove useless variable uasage in mt6370_init_backlight_properties()
- Remove redundant checking enable_gpio in mt6370_bl_update_status()
- Remove redundant parentheses in mt6370_bl_get_brightness()
- Revise the description of Kconfig help text
- Revise the calculation of hys_th_steps


Changes in v4:
- In patch 02/13:
- Add minItems of "io-channel-names"
- Replace text "Mediatek" with "MediaTek"

- In patch 06/13:
- Roll back all "$ref: " to v2 patch style (using "/schemas/...")

- In patch 07/13:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Replace "first break and then return" with "return directly"
   in "mt6370_check_vendor_info()"
- Add module name related description in Kconfig helptext
- Add Copyright in the source code
- Add header file "mt6370.h" for all "#define IRQ"
- Adjust Makefile order of MT6370
- Refine "bank_idx" and "bank_addr" in
  "mt6375_regmap_read()" / "mt6375_regmap_write()"
- Refine redundant "else if" in "mt6370_regmap_read()"

- In patch 08/13:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Replace "first ret=regulator_(dis/en)able and then return"
   with "return directly" in "mt6370_tcpc_set_vbus()"
- Replace header file  with 
- Add Copyright in the source code
- Add module name related description in Kconfig helptext
- Remove header file 
- Refine all probe error by using dev_err_probe()

- In patch 09/13:
- Replace text "Mediatek" with "MediaTek"
- Replace all "first dev_err() and then return" with
   "return dev_err_probe()"
- Add Copyright in the source code
- Add module name related description in Kconfig
- Add unit suffix of macro "ADC_CONV_POLLING_TIME"
- Add new macro "ADC_CONV_TIME_MS"
- Adjust the position of include file 
- Adjust the postions 

Re: [PATCH v4 13/13] video: backlight: mt6370: Add Mediatek MT6370 support

2022-07-14 Thread ChiaEn Wu
Andy Shevchenko  於 2022年7月13日 週三 晚上8:07寫道:
>
> On Wed, Jul 13, 2022 at 12:53 PM ChiaEn Wu  wrote:
> > Andy Shevchenko  於 2022年7月5日 週二 清晨5:14寫道:
> > > On Mon, Jul 4, 2022 at 7:43 AM ChiaEn Wu  wrote:
>
> Please, remove unneeded context when replying!
>
> ...
>
> > > > +   brightness_val[0] = (brightness - 1) & 
> > > > MT6370_BL_DIM2_MASK;
> > > > +   brightness_val[1] = (brightness - 1)
> > > > +   >> fls(MT6370_BL_DIM2_MASK);
> > >
> > > Bad indentation. One line?
> >
> > Well... if indent to one line, it will be over 80 characters(or called 
> > columns?)
> > From my understanding, it is not allowed, right??
>
> It's allowed to some extent.Use your common sense.
> Here it's obviously broken indentation.
>
> ...
>
> > > > +   prop_val = (ilog2(roundup_pow_of_two(prop_val)) + 1) >> 
> > > > 1;
> > >
> > > Isn't something closer to get_order() or fls()?
> >
> > I will revise it to "(get_order(prop_va * PAGE_SIZE) + 1) / 2" and
> > this change is meet your expectations??
>
> Nope. Try again. What about fls()?

I have tried two methods so far, as follows
-
/*
 * prop_val =  1  -->  1 steps --> b'00
 * prop_val =  2 ~  4 -->  4 steps --> b'01
 * prop_val =  5 ~ 16 --> 16 steps --> b'10
 * prop_val = 17 ~ 64 --> 64 steps --> b'11
*/

// 1. use fls() and ffs() combination
prop_val = ffs(prop_val) == fls(prop_val) ? fls(prop_val) >> 1 :
(fls(prop_val) + 1) >> 1;

// 2. use one line for-loop, but without fls()
for (i = --prop_val, prop_val = 0; i >> 2 * prop_val != 0; prop_val++);
-
Do these changes meet your expectations??

>
> ...
>
> > > > +   props->max_brightness = min_t(u32, brightness,
> > > > + MT6370_BL_MAX_BRIGHTNESS);
> > >
> > > One line?
> >
> >  Ditto, it will be over 80 characters...
>
> As per above.
>
> --
> With Best Regards,
> Andy Shevchenko


Re: [PATCH v4 13/13] video: backlight: mt6370: Add Mediatek MT6370 support

2022-07-13 Thread ChiaEn Wu
Hi Andy,
Thanks for your reply! I have some questions want to ask you below.

Andy Shevchenko  於 2022年7月5日 週二 清晨5:14寫道:
>
> On Mon, Jul 4, 2022 at 7:43 AM ChiaEn Wu  wrote:
> >
> > From: ChiaEn Wu 
> >
> > Add Mediatek MT6370 Backlight support.
>
> ...
>
> > + This driver can also be built as a module. If so the module
>
> If so,
>
> > + will be called "mt6370-backlight.ko".
>
> No ".ko" part.
>
> ...
>
> > +#include 
>
> Can you elaborate on this?
>
> > +#include 
> > +#include 
> > +#include 
> > +#include 
>
> > +#include 
>
> Can you elaborate on this?
>
> > +#include 
> > +#include 
>
> Missed mod_devicetable.h.
>
> ...
>
> > +   brightness_val[0] = (brightness - 1) & MT6370_BL_DIM2_MASK;
> > +   brightness_val[1] = (brightness - 1)
> > +   >> fls(MT6370_BL_DIM2_MASK);
>
> Bad indentation. One line?

Well... if indent to one line, it will be over 80 characters(or called columns?)
>From my understanding, it is not allowed, right??

>
> ...
>
> > +   if (priv->enable_gpio)
>
> Dup check.
>
> > +   gpiod_set_value(priv->enable_gpio, brightness ? 1 : 0);
>
> ...
>
> > +   brightness = brightness_val[1] << fls(MT6370_BL_DIM2_MASK);
> > +   brightness += (brightness_val[0] & MT6370_BL_DIM2_MASK);
>
> Too many parentheses.
>
> ...
>
> > +   /*
> > +* prop_val =  1  -->  1 steps --> 0x00
> > +* prop_val =  2 ~  4 -->  4 steps --> 0x01
> > +* prop_val =  5 ~ 16 --> 16 steps --> 0x10
> > +* prop_val = 17 ~ 64 --> 64 steps --> 0x11
> > +*/
> > +   prop_val = (ilog2(roundup_pow_of_two(prop_val)) + 1) >> 1;
>
> Isn't something closer to get_order() or fls()?

I will revise it to "(get_order(prop_va * PAGE_SIZE) + 1) / 2" and
this change is meet your expectations??

>
> ...
>
> > +   props->max_brightness = min_t(u32, brightness,
> > + MT6370_BL_MAX_BRIGHTNESS);
>
> One line?

 Ditto, it will be over 80 characters...

>
> ...
>
> > +   val = 0;
>
> Do you need this here?
>
> > +   prop_val = 0;
>
> Useless.
>
> > +   ret = device_property_read_u8(dev, "mediatek,bled-channel-use",
> > + _val);
> > +   if (ret) {
> > +   dev_err(dev, "mediatek,bled-channel-use DT property 
> > missing\n");
> > +   return ret;
> > +   }
> > +
> > +   if (!prop_val || prop_val > MT6370_BL_MAX_CH) {
> > +   dev_err(dev,
> > +   "No channel specified or over than upper bound 
> > (%d)\n",
> > +   prop_val);
> > +   return -EINVAL;
> > +   }
>
> ...
>
> > +static int mt6370_bl_probe(struct platform_device *pdev)
> > +{
> > +   struct mt6370_priv *priv;
> > +   struct backlight_properties props = {
> > +   .type = BACKLIGHT_RAW,
> > +   .scale = BACKLIGHT_SCALE_LINEAR,
> > +   };
> > +   int ret;
>
>   struct device *dev = >dev;
>
> will save you a few LoCs.
>
> --
> With Best Regards,
> Andy Shevchenko

Best regards,
ChiaEn Wu


Re: [PATCH v3 07/14] mfd: mt6370: Add Mediatek MT6370 support

2022-07-13 Thread ChiaEn Wu
Hi Lee,

Lee Jones  於 2022年7月13日 週三 下午4:04寫道:
>
> On Wed, 13 Jul 2022, ChiaEn Wu wrote:
>
> > Hi Lee,
> >
> > Thanks for your reply.
> >
> > Lee Jones  於 2022年7月12日 週二 晚上11:29寫道:
> > >
> > > On Thu, 23 Jun 2022, ChiaEn Wu wrote:
> > >
> > > > From: ChiYuan Huang 
> > > >
> > > > Add Mediatek MT6370 MFD support.
> > >
> > > No such thing as "MFD support".
> > >
> > > And you're not getting away with submitting a 370 line patch with a 5
> > > word change log either. :)
> > >
> > > Please at least tell us what the device is and what it's used for.
> >
> > I sincerely apologize.
> > We will add more descriptions of the MT6370 feature in the v5 patch.
> >
> > >
> > > > Signed-off-by: ChiYuan Huang 
> > > > ---
> > > >
> > > > v3
> > > > - Refine Kconfig help text
> > > > - Refine error message of unknown vendor ID in
> > > >   mt6370_check_vendor_info()
> > > > - Refine return value handling of mt6370_regmap_read()
> > > > - Refine all probe error by using dev_err_probe()
> > > > - Refine "bank_idx" and "bank_addr" in mt6370_regmap_read() and
> > > >   mt6370_regmap_write()
> > > > - Add "#define VENID*" and drop the comments in
> > > >   mt6370_check_vendor_info()
> > > > - Drop "MFD" in MODULE_DESCRIPTION()
> > > > ---
> > > >  drivers/mfd/Kconfig  |  13 ++
> > > >  drivers/mfd/Makefile |   1 +
> > > >  drivers/mfd/mt6370.c | 358 
> > > > +++
> > > >  3 files changed, 372 insertions(+)
> > > >  create mode 100644 drivers/mfd/mt6370.c
>
> [...]
>
> > > > +static const struct mfd_cell mt6370_devices[] = {
> > > > + MFD_CELL_OF("adc", NULL, NULL, 0, 0, "mediatek,mt6370-adc"),
> > > > + MFD_CELL_OF("charger", NULL, NULL, 0, 0, 
> > > > "mediatek,mt6370-charger"),
> > > > + MFD_CELL_OF("backlight", NULL, NULL, 0, 0, 
> > > > "mediatek,mt6370-backlight"),
> > > > + MFD_CELL_OF("flashlight", NULL, NULL, 0, 0, 
> > > > "mediatek,mt6370-flashlight"),
> > > > + MFD_CELL_OF("indicator", NULL, NULL, 0, 0, 
> > > > "mediatek,mt6370-indicator"),
> > > > + MFD_CELL_OF("tcpc", NULL, NULL, 0, 0, "mediatek,mt6370-tcpc"),
> > > > + MFD_CELL_RES("regulator", mt6370_regulator_irqs)
> > >
> > > The first parameters here should be prepended with something, perhaps
> > > "mt6370_"?
>
> > OK, we will add the prefix in the next patch.

Sorry, I forgot to ask a question in the last mail.
I wonder if using "mt6370-xxx" (dash) is better than using "mt6370_"
(underline) ??
Thanks.

> [...]
>
> > > > +static int mt6370_probe(struct i2c_client *i2c)
> > > > +{
> > > > + struct mt6370_info *info;
> > > > + struct i2c_client *usbc_i2c;
> > > > + int ret;
> > > > +
> > > > + info = devm_kzalloc(>dev, sizeof(*info), GFP_KERNEL);
> > > > + if (!info)
> > > > + return -ENOMEM;
> > > > +
> > > > + info->dev = >dev;
> > > > +
> > > > + usbc_i2c = devm_i2c_new_dummy_device(>dev, i2c->adapter,
> > > > +  MT6370_USBC_I2CADDR);
> > > > + if (IS_ERR(usbc_i2c))
> > > > + return dev_err_probe(>dev, PTR_ERR(usbc_i2c),
> > > > +  "Failed to register USBC I2C 
> > > > client\n");
> > > > +
> > > > + /* Assign I2C client for PMU and TypeC */
> > > > + info->i2c[MT6370_PMU_I2C] = i2c;
> > > > + info->i2c[MT6370_USBC_I2C] = usbc_i2c;
> > > > +
> > > > + info->regmap = devm_regmap_init(>dev, _regmap_bus, 
> > > > info,
> > > > + _regmap_config);
> > >
> > > Apart from in mt6370_check_vendor_info() where is this actually used?
> >
> > Well... from my understanding, we use this MFD driver to make other
> > drivers of MT6370 (e.g. charger, ADC, led...) use the same regmap
> > settings.
> > Thus, this regmap is not only used in mt6370_check_vendor_info().
>
> Well for that to happen you need to store the data somewhere for the
> child devices to fetch from.  I don't see that happening in this
> patch?  What did I miss?

hmmm... I got your point... I will let regmap be a local var in
probe() in the next patch.
Thank you so much!

>
> --
> Lee Jones [李琼斯]
> Principal Technical Lead - Developer Services
> Linaro.org │ Open source software for Arm SoCs
> Follow Linaro: Facebook | Twitter | Blog

Best regards,
ChiaEn Wu


Re: [PATCH v3 07/14] mfd: mt6370: Add Mediatek MT6370 support

2022-07-12 Thread ChiaEn Wu
Hi Lee,

Thanks for your reply.

Lee Jones  於 2022年7月12日 週二 晚上11:29寫道:
>
> On Thu, 23 Jun 2022, ChiaEn Wu wrote:
>
> > From: ChiYuan Huang 
> >
> > Add Mediatek MT6370 MFD support.
>
> No such thing as "MFD support".
>
> And you're not getting away with submitting a 370 line patch with a 5
> word change log either. :)
>
> Please at least tell us what the device is and what it's used for.

I sincerely apologize.
We will add more descriptions of the MT6370 feature in the v5 patch.

>
> > Signed-off-by: ChiYuan Huang 
> > ---
> >
> > v3
> > - Refine Kconfig help text
> > - Refine error message of unknown vendor ID in
> >   mt6370_check_vendor_info()
> > - Refine return value handling of mt6370_regmap_read()
> > - Refine all probe error by using dev_err_probe()
> > - Refine "bank_idx" and "bank_addr" in mt6370_regmap_read() and
> >   mt6370_regmap_write()
> > - Add "#define VENID*" and drop the comments in
> >   mt6370_check_vendor_info()
> > - Drop "MFD" in MODULE_DESCRIPTION()
> > ---
> >  drivers/mfd/Kconfig  |  13 ++
> >  drivers/mfd/Makefile |   1 +
> >  drivers/mfd/mt6370.c | 358 
> > +++
> >  3 files changed, 372 insertions(+)
> >  create mode 100644 drivers/mfd/mt6370.c
> >
> > diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
> > index 3b59456..4c900c4 100644
> > --- a/drivers/mfd/Kconfig
> > +++ b/drivers/mfd/Kconfig
> > @@ -937,6 +937,19 @@ config MFD_MT6360
> > PMIC part includes 2-channel BUCKs and 2-channel LDOs
> > LDO part includes 4-channel LDOs
> >
> > +config MFD_MT6370
> > + tristate "Mediatek MT6370 SubPMIC"
> > + select MFD_CORE
> > + select REGMAP_I2C
> > + select REGMAP_IRQ
> > + depends on I2C
> > + help
> > +   Say Y here to enable MT6370 SubPMIC functional support.
> > +   It consists 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.
> > +
> >  config MFD_MT6397
> >   tristate "MediaTek MT6397 PMIC Support"
> >   select MFD_CORE
> > diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
> > index 858cacf..62b2712 100644
> > --- a/drivers/mfd/Makefile
> > +++ b/drivers/mfd/Makefile
> > @@ -242,6 +242,7 @@ obj-$(CONFIG_INTEL_SOC_PMIC_BXTWC)+= 
> > intel_soc_pmic_bxtwc.o
> >  obj-$(CONFIG_INTEL_SOC_PMIC_CHTWC)   += intel_soc_pmic_chtwc.o
> >  obj-$(CONFIG_INTEL_SOC_PMIC_CHTDC_TI)+= intel_soc_pmic_chtdc_ti.o
> >  obj-$(CONFIG_MFD_MT6360) += mt6360-core.o
> > +obj-$(CONFIG_MFD_MT6370) += mt6370.o
> >  mt6397-objs  := mt6397-core.o mt6397-irq.o mt6358-irq.o
> >  obj-$(CONFIG_MFD_MT6397) += mt6397.o
> >  obj-$(CONFIG_INTEL_SOC_PMIC_MRFLD)   += intel_soc_pmic_mrfld.o
> > diff --git a/drivers/mfd/mt6370.c b/drivers/mfd/mt6370.c
> > new file mode 100644
> > index 000..49f02b1
> > --- /dev/null
> > +++ b/drivers/mfd/mt6370.c
> > @@ -0,0 +1,358 @@
> > +// SPDX-License-Identifier: GPL-2.0
>
> No Copyright?

We have already added Copyright in the v4 patch.

>
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +enum {
> > + MT6370_USBC_I2C = 0,
> > + MT6370_PMU_I2C,
> > + MT6370_MAX_I2C
> > +};
> > +
> > +#define MT6370_REG_DEV_INFO  0x100
> > +#define MT6370_REG_CHG_IRQ1  0x1C0
> > +#define MT6370_REG_CHG_MASK1 0x1E0
> > +
> > +#define MT6370_VENID_MASKGENMASK(7, 4)
> > +
> > +#define MT6370_NUM_IRQREGS   16
> > +#define MT6370_USBC_I2CADDR  0x4E
> > +#define MT6370_REG_ADDRLEN   2
> > +#define MT6370_REG_MAXADDR   0x1FF
> > +
> > +#define MT6370_VENID_RT5081  0x8
> > +#define MT6370_VENID_RT5081A 0xA
> > +#define MT6370_VENID_MT6370  0xE
> > +#define MT6370_VENID_MT6371  0xF
> > +#define MT6370_VENID_MT6372P 0x9
> > +#define MT6370_VENID_MT6372CP0xB
> > +
> > +/* IRQ definitions */
> > +#define MT6370_IRQ_DIRCHGON  0
> > +#define MT6370_IRQ_CHG_TREG  4
> > +#define MT6370_IRQ_CHG_AICR  5
> > +#define MT6370_IRQ_CHG_MIVR  6
> > +#de

[PATCH v4 01/13] dt-bindings: usb: Add Mediatek MT6370 TCPC

2022-07-04 Thread ChiaEn Wu
From: ChiYuan Huang 

Add Mediatek MT6370 TCPC binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Krzysztof Kozlowski 
---
 .../bindings/usb/mediatek,mt6370-tcpc.yaml | 36 ++
 1 file changed, 36 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml

diff --git a/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml 
b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
new file mode 100644
index 000..72f56cc
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/mediatek,mt6370-tcpc.yaml
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/usb/mediatek,mt6370-tcpc.yaml#;
+$schema: "http://devicetree.org/meta-schemas/core.yaml#;
+
+title: MediatTek MT6370 Type-C Port Switch and Power Delivery controller
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  MediaTek MT6370 is a multi-functional device.
+  It integrates charger, ADC, flash, RGB indicators,
+  regulators (DSV/VIBLDO), and TypeC Port Switch with Power Delivery 
controller.
+  This document only describes MT6370 Type-C Port Switch and
+  Power Delivery controller.
+
+properties:
+  compatible:
+enum:
+  - mediatek,mt6370-tcpc
+
+  interrupts:
+maxItems: 1
+
+  connector:
+type: object
+$ref: /schemas/connector/usb-connector.yaml#
+unevaluatedProperties: false
+
+additionalProperties: false
+
+required:
+  - compatible
+  - interrupts
-- 
2.7.4



[PATCH v4 13/13] video: backlight: mt6370: Add Mediatek MT6370 support

2022-07-04 Thread ChiaEn Wu
From: ChiaEn Wu 

Add Mediatek MT6370 Backlight support.

Signed-off-by: ChiaEn Wu 
---

v4:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Add Copyright in the source code
- Revise the comment of "PWM HYS STEPS"
---
 drivers/video/backlight/Kconfig|  12 +
 drivers/video/backlight/Makefile   |   1 +
 drivers/video/backlight/mt6370-backlight.c | 352 +
 3 files changed, 365 insertions(+)
 create mode 100644 drivers/video/backlight/mt6370-backlight.c

diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig
index a003e02..099b0d0 100644
--- a/drivers/video/backlight/Kconfig
+++ b/drivers/video/backlight/Kconfig
@@ -268,6 +268,18 @@ config BACKLIGHT_MAX8925
  If you have a LCD backlight connected to the WLED output of MAX8925
  WLED output, say Y here to enable this driver.
 
+config BACKLIGHT_MT6370
+   tristate "MediaTek MT6370 Backlight Driver"
+   depends on MFD_MT6370
+   help
+ This enables support for Mediatek MT6370 Backlight driver.
+ It's commonly used to drive the display WLED. There are 4 channels
+ inside, and each channel supports up to 30mA of current capability
+ with 2048 current steps in exponential or linear mapping curves.
+
+ This driver can also be built as a module. If so the module
+ will be called "mt6370-backlight.ko".
+
 config BACKLIGHT_APPLE
tristate "Apple Backlight Driver"
depends on X86 && ACPI
diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile
index cae2c83..e815f3f 100644
--- a/drivers/video/backlight/Makefile
+++ b/drivers/video/backlight/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_BACKLIGHT_LP855X)+= lp855x_bl.o
 obj-$(CONFIG_BACKLIGHT_LP8788) += lp8788_bl.o
 obj-$(CONFIG_BACKLIGHT_LV5207LP)   += lv5207lp.o
 obj-$(CONFIG_BACKLIGHT_MAX8925)+= max8925_bl.o
+obj-$(CONFIG_BACKLIGHT_MT6370) += mt6370-backlight.o
 obj-$(CONFIG_BACKLIGHT_OMAP1)  += omap1_bl.o
 obj-$(CONFIG_BACKLIGHT_PANDORA)+= pandora_bl.o
 obj-$(CONFIG_BACKLIGHT_PCF50633)   += pcf50633-backlight.o
diff --git a/drivers/video/backlight/mt6370-backlight.c 
b/drivers/video/backlight/mt6370-backlight.c
new file mode 100644
index 000..655f585
--- /dev/null
+++ b/drivers/video/backlight/mt6370-backlight.c
@@ -0,0 +1,352 @@
+// 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_DEV_INFO0x100
+#define MT6370_REG_BL_EN   0x1A0
+#define MT6370_REG_BL_BSTCTRL  0x1A1
+#define MT6370_REG_BL_PWM  0x1A2
+#define MT6370_REG_BL_DIM2 0x1A4
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+#define MT6370_BL_EXT_EN_MASK  BIT(7)
+#define MT6370_BL_EN_MASK  BIT(6)
+#define MT6370_BL_CONFIG_MASK  BIT(0)
+#define MT6370_BL_CH_MASK  GENMASK(5, 2)
+#define MT6370_BL_DIM2_MASKGENMASK(2, 0)
+#define MT6370_BL_DUMMY_6372_MASK  GENMASK(2, 0)
+#define MT6370_BL_DIM2_6372_SHIFT  3
+#define MT6370_BL_PWM_EN_MASK  BIT(7)
+#define MT6370_BL_PWM_HYS_EN_MASK  BIT(2)
+#define MT6370_BL_PWM_HYS_SEL_MASK GENMASK(1, 0)
+#define MT6370_BL_OVP_EN_MASK  BIT(7)
+#define MT6370_BL_OVP_SEL_MASK GENMASK(6, 5)
+#define MT6370_BL_OC_EN_MASK   BIT(3)
+#define MT6370_BL_OC_SEL_MASK  GENMASK(2, 1)
+
+#define MT6370_BL_PWM_HYS_TH_MIN_STEP  1
+#define MT6370_BL_PWM_HYS_TH_MAX_STEP  64
+#define MT6370_BL_OVP_MIN_UV   1700
+#define MT6370_BL_OVP_MAX_UV   2900
+#define MT6370_BL_OVP_STEP_UV  400
+#define MT6370_BL_OCP_MIN_UA   90
+#define MT6370_BL_OCP_MAX_UA   180
+#define MT6370_BL_OCP_STEP_UA  30
+#define MT6370_BL_MAX_BRIGHTNESS   2048
+#define MT6370_BL_MAX_CH   15
+
+enum {
+   MT6370_VID_COMMON = 0,
+   MT6370_VID_6372,
+};
+
+struct mt6370_priv {
+   int vid_type;
+   struct backlight_device *bl;
+   struct device *dev;
+   struct gpio_desc *enable_gpio;
+   struct regmap *regmap;
+};
+
+static int mt6370_bl_update_status(struct backlight_device *bl_dev)
+{
+   struct mt6370_priv *priv = bl_get_data(bl_dev);
+   int brightness = backlight_get_brightness(bl_dev);
+   unsigned int enable_val;
+   u8 brightness_val[2];
+   int ret;
+
+   if (brightness) {
+   brightness_val[0] = (brightness - 1) & MT6370_BL_DIM2_MASK;
+   brightness_val[1] = (brightness - 1)
+   >> fls(MT6370_BL_DIM2_MASK);
+
+ 

[PATCH v4 08/13] usb: typec: tcpci_mt6370: Add Mediatek MT6370 tcpci driver

2022-07-04 Thread ChiaEn Wu
From: ChiYuan Huang 

Add chip level mt6370 tcpci driver.

Signed-off-by: ChiYuan Huang 
---

v4:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Replace "first ret=regulator_(dis/en)able and then return"
  with "return directly" in "mt6370_tcpc_set_vbus()"
- Replace header file  with 
- Add Copyright in the source code
- Add module name related description in Kconfig helptext
- Remove header file 
- Refine all probe error by using dev_err_probe()
---
 drivers/usb/typec/tcpm/Kconfig|  11 ++
 drivers/usb/typec/tcpm/Makefile   |   1 +
 drivers/usb/typec/tcpm/tcpci_mt6370.c | 207 ++
 3 files changed, 219 insertions(+)
 create mode 100644 drivers/usb/typec/tcpm/tcpci_mt6370.c

diff --git a/drivers/usb/typec/tcpm/Kconfig b/drivers/usb/typec/tcpm/Kconfig
index 073fd2e..ccdd791 100644
--- a/drivers/usb/typec/tcpm/Kconfig
+++ b/drivers/usb/typec/tcpm/Kconfig
@@ -35,6 +35,17 @@ config TYPEC_MT6360
  USB Type-C. It works with Type-C Port Controller Manager
  to provide USB PD and USB Type-C functionalities.
 
+config TYPEC_TCPCI_MT6370
+   tristate "MediaTek MT6370 Type-C driver"
+   depends on MFD_MT6370
+   help
+ MediaTek MT6370 is a multi-functional IC that includes
+ USB Type-C. It works with Type-C Port Controller Manager
+ to provide USB PD and USB Type-C functionalities.
+
+ This driver can also be built as a module. If so the module
+ will be called "tcpci_mt6370.ko".
+
 config TYPEC_TCPCI_MAXIM
tristate "Maxim TCPCI based Type-C chip driver"
help
diff --git a/drivers/usb/typec/tcpm/Makefile b/drivers/usb/typec/tcpm/Makefile
index 7d499f3..906d9dc 100644
--- a/drivers/usb/typec/tcpm/Makefile
+++ b/drivers/usb/typec/tcpm/Makefile
@@ -6,4 +6,5 @@ typec_wcove-y   := wcove.o
 obj-$(CONFIG_TYPEC_TCPCI)  += tcpci.o
 obj-$(CONFIG_TYPEC_RT1711H)+= tcpci_rt1711h.o
 obj-$(CONFIG_TYPEC_MT6360) += tcpci_mt6360.o
+obj-$(CONFIG_TYPEC_TCPCI_MT6370)   += tcpci_mt6370.o
 obj-$(CONFIG_TYPEC_TCPCI_MAXIM)+= tcpci_maxim.o
diff --git a/drivers/usb/typec/tcpm/tcpci_mt6370.c 
b/drivers/usb/typec/tcpm/tcpci_mt6370.c
new file mode 100644
index 000..12336ca
--- /dev/null
+++ b/drivers/usb/typec/tcpm/tcpci_mt6370.c
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "tcpci.h"
+
+#define MT6370_REG_SYSCTRL80x9B
+
+#define MT6370_AUTOIDLE_MASK   BIT(3)
+
+#define MT6370_VENDOR_ID   0x29CF
+#define MT6370_TCPC_DID_A  0x2170
+
+struct mt6370_priv {
+   struct device *dev;
+   struct regulator *vbus;
+   struct tcpci *tcpci;
+   struct tcpci_data tcpci_data;
+   int irq;
+};
+
+static const struct reg_sequence mt6370_reg_init[] = {
+   REG_SEQ(0xA0, 0x1, 1000),
+   REG_SEQ(0x81, 0x38, 0),
+   REG_SEQ(0x82, 0x82, 0),
+   REG_SEQ(0xBA, 0xFC, 0),
+   REG_SEQ(0xBB, 0x50, 0),
+   REG_SEQ(0x9E, 0x8F, 0),
+   REG_SEQ(0xA1, 0x5, 0),
+   REG_SEQ(0xA2, 0x4, 0),
+   REG_SEQ(0xA3, 0x4A, 0),
+   REG_SEQ(0xA4, 0x01, 0),
+   REG_SEQ(0x95, 0x01, 0),
+   REG_SEQ(0x80, 0x71, 0),
+   REG_SEQ(0x9B, 0x3A, 1000)
+};
+
+static int mt6370_tcpc_init(struct tcpci *tcpci, struct tcpci_data *data)
+{
+   u16 did;
+   int ret;
+
+   ret = regmap_register_patch(data->regmap, mt6370_reg_init,
+   ARRAY_SIZE(mt6370_reg_init));
+   if (ret)
+   return ret;
+
+   ret = regmap_raw_read(data->regmap, TCPC_BCD_DEV, , sizeof(u16));
+   if (ret)
+   return ret;
+
+   if (did == MT6370_TCPC_DID_A)
+   return regmap_write(data->regmap, TCPC_FAULT_CTRL, 0x80);
+
+   return 0;
+}
+
+static int mt6370_tcpc_set_vconn(struct tcpci *tcpci, struct tcpci_data *data,
+bool enable)
+{
+   return regmap_update_bits(data->regmap, MT6370_REG_SYSCTRL8,
+ MT6370_AUTOIDLE_MASK,
+ !enable ? MT6370_AUTOIDLE_MASK : 0);
+}
+
+static int mt6370_tcpc_set_vbus(struct tcpci *tcpci, struct tcpci_data *data,
+   bool source, bool sink)
+{
+   struct mt6370_priv *priv = container_of(data, struct mt6370_priv,
+   tcpci_data);
+   int ret;
+
+   ret = regulator_is_enabled(priv->vbus);
+   if (ret < 0)
+   return ret;
+
+   if (ret && !source)
+   return regulator_disable(priv->vbus);
+   else if (!ret && source)
+   return regulator_enable(priv->vbus);
+   else
+   return 0;
+}
+
+static irqreturn_t mt6370_irq_handler(int irq, void *dev_id)
+{

[PATCH v4 02/13] dt-bindings: power: supply: Add Mediatek MT6370 Charger

2022-07-04 Thread ChiaEn Wu
From: ChiaEn Wu 

Add Mediatek MT6370 Charger binding documentation.

Signed-off-by: ChiaEn Wu 
---

v4:
- Add minItems of "io-channel-names"
- Replace text "Mediatek" with "MediaTek"
---
 .../power/supply/mediatek,mt6370-charger.yaml  | 88 ++
 1 file changed, 88 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml

diff --git 
a/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
new file mode 100644
index 000..bd09a0a
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/mediatek,mt6370-charger.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Battery Charger
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Provides Battery Charger, Boost for OTG devices and BC1.2 detection.
+
+properties:
+  compatible:
+const: mediatek,mt6370-charger
+
+  interrupts:
+description: |
+  Specify what irqs are needed to be handled by MT6370 Charger driver. IRQ
+  "MT6370_IRQ_CHG_MIVR", "MT6370_IRQ_ATTACH" and "MT6370_IRQ_OVPCTRL_UVP_D"
+  are required.
+items:
+  - description: BC1.2 done irq
+  - description: usb plug in irq
+  - description: mivr irq
+
+  interrupt-names:
+items:
+  - const: attach_i
+  - const: uvp_d_evt
+  - const: mivr
+
+  io-channels:
+description: |
+  Use ADC channel to read VBUS, IBUS, IBAT, etc., info.
+minItems: 1
+items:
+  - description: |
+  VBUS voltage with lower accuracy (+-75mV) but higher measure
+  range (1~22V)
+  - description: |
+  VBUS voltage with higher accuracy (+-30mV) but lower measure
+  range (1~9.76V)
+  - description: the main system input voltage
+  - description: battery voltage
+  - description: battery temperature-sense input voltage
+  - description: IBUS current (required)
+  - description: battery current
+  - description: |
+  regulated output voltage to supply for the PWM low-side gate driver
+  and the bootstrap capacitor
+  - description: IC junction temperature
+
+  io-channel-names:
+minItems: 1
+items:
+  - const: vbusdiv5
+  - const: vbusdiv2
+  - const: vsys
+  - const: vbat
+  - const: ts_bat
+  - const: ibus
+  - const: ibat
+  - const: chg_vddp
+  - const: temp_jc
+
+  usb-otg-vbus-regulator:
+type: object
+description: OTG boost regulator.
+unevaluatedProperties: false
+$ref: /schemas/regulator/regulator.yaml#
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - interrupts
+  - interrupt-names
+  - io-channels
+
+additionalProperties: false
+
+...
-- 
2.7.4



[PATCH v4 03/13] dt-bindings: leds: mt6370: Add Mediatek mt6370 current sink type LED indicator

2022-07-04 Thread ChiaEn Wu
From: ChiYuan Huang 

Add Mediatek mt6370 current sink type LED indicator binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Krzysztof Kozlowski 
---
 .../bindings/leds/mediatek,mt6370-indicator.yaml   | 77 ++
 1 file changed, 77 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
new file mode 100644
index 000..45030f3
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-indicator.yaml
@@ -0,0 +1,77 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-indicator.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 LED driver include 4-channel RGB LED support Register/PWM/Breath 
Mode
+
+allOf:
+  - $ref: leds-class-multicolor.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-indicator
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^multi-led@[0-3]$":
+type: object
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-2]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+required:
+  - reg
+  - color
+
+required:
+  - reg
+  - color
+  - "#address-cells"
+  - "#size-cells"
+
+  "^led@[0-3]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1, 2, 3]
+
+required:
+  - reg
+  - color
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v4 12/13] leds: flashlight: mt6370: Add Mediatek MT6370 flashlight support

2022-07-04 Thread ChiaEn Wu
From: Alice Chen 

Add Mediatek MT6370 flashlight support.

Signed-off-by: Alice Chen 
---

v4:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Add Copyright in the source code
---
 drivers/leds/flash/Kconfig |  12 +
 drivers/leds/flash/Makefile|   1 +
 drivers/leds/flash/leds-mt6370-flash.c | 662 +
 3 files changed, 675 insertions(+)
 create mode 100644 drivers/leds/flash/leds-mt6370-flash.c

diff --git a/drivers/leds/flash/Kconfig b/drivers/leds/flash/Kconfig
index d3eb689..cd366e8 100644
--- a/drivers/leds/flash/Kconfig
+++ b/drivers/leds/flash/Kconfig
@@ -90,4 +90,16 @@ config LEDS_SGM3140
  This option enables support for the SGM3140 500mA Buck/Boost Charge
  Pump LED Driver.
 
+config LEDS_MT6370_FLASHLIGHT
+   tristate "Flash LED Support for MediaTek MT6370 PMIC"
+   depends on LEDS_CLASS
+   depends on MFD_MT6370
+   help
+ Support 2 channels and torch/strobe mode.
+ Say Y here to enable support for
+ MT6370_FLASH_LED device.
+
+ This driver can also be built as a module. If so the module
+ will be called "leds-mt6370-flash.ko".
+
 endif # LEDS_CLASS_FLASH
diff --git a/drivers/leds/flash/Makefile b/drivers/leds/flash/Makefile
index 0acbddc..4c4c171 100644
--- a/drivers/leds/flash/Makefile
+++ b/drivers/leds/flash/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_LEDS_MAX77693) += leds-max77693.o
 obj-$(CONFIG_LEDS_RT4505)  += leds-rt4505.o
 obj-$(CONFIG_LEDS_RT8515)  += leds-rt8515.o
 obj-$(CONFIG_LEDS_SGM3140) += leds-sgm3140.o
+obj-$(CONFIG_LEDS_MT6370_FLASHLIGHT)   += leds-mt6370-flash.o
diff --git a/drivers/leds/flash/leds-mt6370-flash.c 
b/drivers/leds/flash/leds-mt6370-flash.c
new file mode 100644
index 000..d988977
--- /dev/null
+++ b/drivers/leds/flash/leds-mt6370-flash.c
@@ -0,0 +1,662 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+enum {
+   MT6370_LED_FLASH1,
+   MT6370_LED_FLASH2,
+   MT6370_MAX_LEDS
+};
+
+/* Virtual definition for multicolor */
+
+#define MT6370_REG_FLEDEN  0x17E
+#define MT6370_REG_STRBTO  0x173
+#define MT6370_REG_CHGSTAT20x1D1
+#define MT6370_REG_FLEDSTAT1   0x1D9
+#defineMT6370_REG_FLEDISTRB(_id)   (0x174 + 4 * _id)
+#define MT6370_REG_FLEDITOR(_id)   (0x175 + 4 * _id)
+#define MT6370_ITORCH_MASK GENMASK(4, 0)
+#define MT6370_ISTROBE_MASKGENMASK(6, 0)
+#define MT6370_STRBTO_MASK GENMASK(6, 0)
+#define MT6370_TORCHEN_MASKBIT(3)
+#define MT6370_STROBEN_MASKBIT(2)
+#define MT6370_FLCSEN_MASK(_id)BIT(MT6370_LED_FLASH2 - _id)
+#define MT6370_FLCSEN_MASK_ALL (BIT(0) | BIT(1))
+#define MT6370_FLEDCHGVINOVP_MASK  BIT(3)
+#define MT6370_FLED1STRBTO_MASKBIT(11)
+#define MT6370_FLED2STRBTO_MASKBIT(10)
+#define MT6370_FLED1STRB_MASK  BIT(9)
+#define MT6370_FLED2STRB_MASK  BIT(8)
+#define MT6370_FLED1SHORT_MASK BIT(7)
+#define MT6370_FLED2SHORT_MASK BIT(6)
+#define MT6370_FLEDLVF_MASKBIT(3)
+
+#define MT6370_LED_JOINT   2
+#define MT6370_RANGE_FLED_REG  4
+#define MT6370_ITORCH_MINUA25000
+#define MT6370_ITORCH_STEPUA   12500
+#define MT6370_ITORCH_MAXUA40
+#define MT6370_ITORCH_DOUBLE_MAXUA 80
+#define MT6370_ISTRB_MINUA 5
+#define MT6370_ISTRB_STEPUA12500
+#define MT6370_ISTRB_MAXUA 150
+#define MT6370_ISTRB_DOUBLE_MAXUA  300
+#define MT6370_STRBTO_MINUS64000
+#define MT6370_STRBTO_STEPUS   32000
+#define MT6370_STRBTO_MAXUS2432000
+
+#define STATE_OFF  0
+#define STATE_KEEP 1
+#define STATE_ON   2
+
+#define to_mt6370_led(ptr, member) container_of(ptr, struct mt6370_led, member)
+
+struct mt6370_led {
+   struct led_classdev_flash flash;
+   struct v4l2_flash *v4l2_flash;
+   struct mt6370_priv *priv;
+   u32 led_no;
+   u32 default_state;
+};
+
+struct mt6370_priv {
+   struct device *dev;
+   struct regmap *regmap;
+   struct mutex lock;
+   unsigned int fled_strobe_used;
+   unsigned int fled_torch_used;
+   unsigned int leds_active;
+   unsigned int leds_count;
+   struct mt6370_led leds[];
+};
+
+static int mt6370_torch_brightness_set(struct led_classdev *lcdev,
+  enum led_brightness level)
+{
+   struct mt6370_led *led = to_mt6370_led(lcdev, flash.led_cdev);
+   struct mt6370_priv *priv = led->priv;
+   u32 led_enable_mask = (led->led_no == MT6370_LED_JOINT) ?
+ 

[PATCH v4 07/13] mfd: mt6370: Add Mediatek MT6370 support

2022-07-04 Thread ChiaEn Wu
From: ChiYuan Huang 

Add Mediatek MT6370 MFD support.

Signed-off-by: ChiYuan Huang 
---

v4
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Replace "first break and then return" with "return directly"
  in
  "mt6370_check_vendor_info()"
- Add module name related description in Kconfig helptext
- Add Copyright in the source code
- Add header file "mt6370.h" for all "#define IRQ"
- Adjust Makefile order of MT6370
- Refine "bank_idx" and "bank_addr" in
  "mt6375_regmap_read()" / "mt6375_regmap_write()"
- Refine redundant "else if" in "mt6370_regmap_read()"
---
 drivers/mfd/Kconfig  |  16 +++
 drivers/mfd/Makefile |   1 +
 drivers/mfd/mt6370.c | 289 +++
 drivers/mfd/mt6370.h |  88 
 4 files changed, 394 insertions(+)
 create mode 100644 drivers/mfd/mt6370.c
 create mode 100644 drivers/mfd/mt6370.h

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 3b59456..dafc960 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -937,6 +937,22 @@ config MFD_MT6360
  PMIC part includes 2-channel BUCKs and 2-channel LDOs
  LDO part includes 4-channel LDOs
 
+config MFD_MT6370
+   tristate "MediaTek MT6370 SubPMIC"
+   select MFD_CORE
+   select REGMAP_I2C
+   select REGMAP_IRQ
+   depends on I2C
+   help
+ Say Y here to enable MT6370 SubPMIC functional support.
+ It consists 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 driver can also be built as a module. If so the module
+ will be called "mt6370.ko".
+
 config MFD_MT6397
tristate "MediaTek MT6397 PMIC Support"
select MFD_CORE
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 8c69867..81dbed3 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -173,6 +173,7 @@ obj-$(CONFIG_MFD_MAX8998)   += max8998.o max8998-irq.o
 obj-$(CONFIG_MFD_MP2629)   += mp2629.o
 
 obj-$(CONFIG_MFD_MT6360)   += mt6360-core.o
+obj-$(CONFIG_MFD_MT6370)   += mt6370.o
 mt6397-objs:= mt6397-core.o mt6397-irq.o mt6358-irq.o
 obj-$(CONFIG_MFD_MT6397)   += mt6397.o
 
diff --git a/drivers/mfd/mt6370.c b/drivers/mfd/mt6370.c
new file mode 100644
index 000..a0a805e
--- /dev/null
+++ b/drivers/mfd/mt6370.c
@@ -0,0 +1,289 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "mt6370.h"
+
+enum {
+   MT6370_USBC_I2C = 0,
+   MT6370_PMU_I2C,
+   MT6370_MAX_I2C
+};
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_CHG_IRQ10x1C0
+#define MT6370_REG_CHG_MASK1   0x1E0
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+
+#define MT6370_NUM_IRQREGS 16
+#define MT6370_USBC_I2CADDR0x4E
+#define MT6370_REG_ADDRLEN 2
+#define MT6370_REG_MAXADDR 0x1FF
+
+#define MT6370_VENID_RT50810x8
+#define MT6370_VENID_RT5081A   0xA
+#define MT6370_VENID_MT63700xE
+#define MT6370_VENID_MT63710xF
+#define MT6370_VENID_MT6372P   0x9
+#define MT6370_VENID_MT6372CP  0xB
+
+struct mt6370_info {
+   struct i2c_client *i2c[MT6370_MAX_I2C];
+   struct device *dev;
+   struct regmap *regmap;
+   struct regmap_irq_chip_data *irq_data;
+};
+
+static const struct regmap_irq mt6370_irqs[] = {
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_DIRCHGON, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TREG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_AICR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_MIVR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_PWR_RDY, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_FL_CHG_VINOVP, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VSYSUV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VSYSOV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VBATOV, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_VINOVPCHG, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_COLD, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_COOL, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_WARM, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_BAT_HOT, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TS_STATC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_FAULT, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_STATC, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_TMR, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_BATABS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_ADPBAD, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_RVP, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_TSHUTDOWN, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_IINMEAS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHG_ICCMEAS, 8),
+   REGMAP_IRQ_REG_LINE(MT6370_IRQ_CHGDET_DONE, 8),

[PATCH v4 10/13] power: supply: mt6370: Add Mediatek MT6370 charger driver

2022-07-04 Thread ChiaEn Wu
From: ChiaEn Wu 

Add Mediatek MT6370 charger driver.

Signed-off-by: ChiaEn Wu 
---

v4:
- Replace text "Mediatek" with "MediaTek" in Kconfig and
  MODULE_DESCRIPTION()
- Replace "mt6370_chg_val_to_reg" and "mt6370_chg_reg_to_val"
  with
  "linear_range" API
- Replace "first break and then return" with "return directly"
  in all
  cases of get/set power_supply_property
- Replace all "first dev_err() and then return" with "return
  dev_err_probe()"
- Replace all "return IS_ERR(priv->rdev) ? PTR_ERR(priv->rdev) :
  0" with
  "PTR_ERR_OR_ZERO()"
- Replace "priv->dev->of_node" with "dev_of_node()"
- Add Copyright in the source code
- Add module name related description in Kconfig helptext
- Add proper unit of "MT6370_MIVR_IBUS_TH"
- Add error check in "mt6370_chg_get_status"
- Remove including  header file
- Remove redundant comma of every enum terminator line
- Remove unwanted blank lines
- Remove the useless label (toggle_cfo_exit:)
- Remove using atomic
- Remove using of_match_ptr()
- Fix some incorrect characters
- Fix updating wrong bits when using ena_gpiod of OTG regulator
- Adjust the probe order in probe()
---
 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..27c9b5a 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.ko".
+
 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..f960fbe
--- /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 */

[PATCH v4 06/13] dt-bindings: mfd: Add Mediatek MT6370

2022-07-04 Thread ChiaEn Wu
From: ChiYuan Huang 

Add Mediatek MT6370 binding documentation.

Signed-off-by: ChiYuan Huang 
---

v4
- Roll back all "$ref: " to v2 patch style (using
  "/schemas/...")
---
 .../devicetree/bindings/mfd/mediatek,mt6370.yaml   | 280 +
 include/dt-bindings/iio/adc/mediatek,mt6370_adc.h  |  18 ++
 2 files changed, 298 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
 create mode 100644 include/dt-bindings/iio/adc/mediatek,mt6370_adc.h

diff --git a/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml 
b/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
new file mode 100644
index 000..410e2d4
--- /dev/null
+++ b/Documentation/devicetree/bindings/mfd/mediatek,mt6370.yaml
@@ -0,0 +1,280 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/mediatek,mt6370.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 SubPMIC
+
+maintainers:
+  - ChiYuan Huang 
+
+description: |
+  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.
+
+properties:
+  compatible:
+const: mediatek,mt6370
+
+  reg:
+maxItems: 1
+
+  wakeup-source: true
+
+  interrupts:
+maxItems: 1
+
+  interrupt-controller: true
+
+  "#interrupt-cells":
+const: 1
+
+  adc:
+type: object
+description: |
+  Provides 9 channels for system monitoring, including VBUSDIV5 (lower
+  accuracy, higher measure range), VBUSDIV2 (higher accuracy, lower
+  measure range), VBAT, VSYS, CHG_VDDP, TS_BAT, IBUS, IBAT, and TEMP_JC.
+
+properties:
+  compatible:
+const: mediatek,mt6370-adc
+
+  "#io-channel-cells":
+const: 1
+
+required:
+  - compatible
+  - "#io-channel-cells"
+
+  backlight:
+type: object
+$ref: /schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+
+  charger:
+type: object
+$ref: /schemas/power/supply/mediatek,mt6370-charger.yaml#
+
+  tcpc:
+type: object
+$ref: /schemas/usb/mediatek,mt6370-tcpc.yaml#
+
+  indicator:
+type: object
+$ref: /schemas/leds/mediatek,mt6370-indicator.yaml#
+
+  flashlight:
+type: object
+$ref: /schemas/leds/mediatek,mt6370-flashlight.yaml#
+
+  regulators:
+type: object
+description: |
+  List all supported regulators, which support the control for DisplayBias
+  voltages and one general purpose LDO which commonly used to drive the
+  vibrator.
+
+patternProperties:
+  "^(dsvbst|vibldo)$":
+$ref: /schemas/regulator/regulator.yaml#
+type: object
+unevaluatedProperties: false
+
+  "^(dsvpos|dsvneg)$":
+$ref: /schemas/regulator/regulator.yaml#
+type: object
+unevaluatedProperties: false
+
+properties:
+  enable-gpios:
+maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - interrupt-controller
+  - "#interrupt-cells"
+  - regulators
+  - adc
+  - backlight
+  - indicator
+  - tcpc
+  - charger
+  - flashlight
+
+additionalProperties: false
+
+examples:
+  - |
+#include 
+#include 
+#include 
+#include 
+i2c {
+  #address-cells = <1>;
+  #size-cells = <0>;
+
+pmic@34 {
+compatible = "mediatek,mt6370";
+reg = <0x34>;
+wakeup-source;
+interrupts-extended = < 3 IRQ_TYPE_LEVEL_LOW>;
+interrupt-controller;
+#interrupt-cells = <1>;
+
+mt6370_adc: adc {
+  compatible = "mediatek,mt6370-adc";
+  #io-channel-cells = <1>;
+};
+
+backlight {
+  compatible = "mediatek,mt6370-backlight";
+  mediatek,bled-channel-use = /bits/ 8 <15>;
+};
+
+charger {
+  compatible = "mediatek,mt6370-charger";
+  interrupts = <48>, <68>, <6>;
+  interrupt-names = "attach_i", "uvp_d_evt", "mivr";
+  io-channels = <_adc MT6370_CHAN_IBUS>;
+
+  mt6370_otg_vbus: usb-otg-vbus-regulator {
+regulator-name = "mt6370-usb-otg-vbus";
+regulator-min-microvolt = <435>;
+regulator-max-microvolt = <580>;
+regulator-min-microamp = <50>;
+regulator-max-microamp = <300>;
+  };
+};
+
+indicator {
+  compatible = "mediatek,mt6370-indicator";
+  #address-cells = <1>;
+  #size-cells = <0>;
+
+  multi-led@0 {
+reg = <0>;
+function = LED_FUNCTION_INDICATOR;
+color = ;
+led-max-microamp = <24000>;
+#address-cells = <1>;
+#size-cells = <0>;
+led@0 {
+  reg = <0>;
+  color = 

[PATCH v4 04/13] dt-bindings: leds: Add Mediatek MT6370 flashlight

2022-07-04 Thread ChiaEn Wu
From: Alice Chen 

Add Mediatek MT6370 flashlight binding documentation.

Signed-off-by: Alice Chen 
Reviewed-by: Krzysztof Kozlowski 
---
 .../bindings/leds/mediatek,mt6370-flashlight.yaml  | 41 ++
 1 file changed, 41 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml 
b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
new file mode 100644
index 000..e9d02ed
--- /dev/null
+++ b/Documentation/devicetree/bindings/leds/mediatek,mt6370-flashlight.yaml
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/mediatek,mt6370-flashlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Flash LED driver for MT6370 PMIC from MediaTek Integrated.
+
+maintainers:
+  - Alice Chen 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  Add MT6370 flash LED driver include 2-channel flash LED support Torch/Strobe 
Mode.
+
+properties:
+  compatible:
+const: mediatek,mt6370-flashlight
+
+  "#address-cells":
+const: 1
+
+  "#size-cells":
+const: 0
+
+patternProperties:
+  "^led@[0-1]$":
+type: object
+$ref: common.yaml#
+unevaluatedProperties: false
+
+properties:
+  reg:
+enum: [0, 1]
+
+required:
+  - compatible
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
-- 
2.7.4



[PATCH v4 09/13] iio: adc: mt6370: Add Mediatek MT6370 support

2022-07-04 Thread ChiaEn Wu
From: ChiaEn Wu 

Add Mediatek MT6370 ADC support.

Signed-off-by: ChiaEn Wu 
---

v4:
- Replace text "Mediatek" with "MediaTek"
- Replace all "first dev_err() and then return" with "return
  dev_err_probe()"
- Add Copyright in the source code
- Add module name related description in Kconfig
- Add unit suffix of macro "ADC_CONV_POLLING_TIME"
- Add new macro "ADC_CONV_TIME_MS"
- Adjust the position of include file 
- Adjust the postions between  and
  
- Fix some incorrect characters
---
 drivers/iio/adc/Kconfig  |  12 ++
 drivers/iio/adc/Makefile |   1 +
 drivers/iio/adc/mt6370-adc.c | 274 +++
 3 files changed, 287 insertions(+)
 create mode 100644 drivers/iio/adc/mt6370-adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 48ace74..fca382d 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -737,6 +737,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.ko".
+
 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 39d806f..0ce285c 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..0493d6e
--- /dev/null
+++ b/drivers/iio/adc/mt6370-adc.c
@@ -0,0 +1,274 @@
+// 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 
+
+#define MT6370_REG_CHG_CTRL3   0x113 /* AICR */
+#define MT6370_REG_CHG_CTRL7   0x117 /* ICHG */
+#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_400MA  0x6
+#define MT6370_ICHG_500MA  0x4
+#define MT6370_ICHG_900MA  0x8
+
+#define ADC_CONV_TIME_US   35000
+#define ADC_CONV_TIME_MS   (ADC_CONV_TIME_US / 1000)
+#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)
+{
+   __be16 be_val;
+   unsigned int reg_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_US * 3);
+   if (ret) {
+   dev_err(priv->dev, "Failed to read ADC register (%d)\n", ret);
+   goto adc_unlock;
+   }
+
+   ret = regmap_raw_read(priv->regmap, MT6370_REG_ADC_DATA_H,
+ _val, sizeof(be_val));
+   if (ret)
+   goto adc_unlock;
+
+   *val = be16_to_cpu(be_val);
+   ret = IIO_VAL_INT;
+
+adc_unlock:
+   mutex_unlock(>adc_lock);
+
+   return ret;
+}
+
+static int mt6370_adc_read_scale(struct mt6370_adc_data *priv,
+int chan, int *val1, int *val

[PATCH v4 05/13] dt-bindings: backlight: Add Mediatek MT6370 backlight

2022-07-04 Thread ChiaEn Wu
From: ChiYuan Huang 

Add mt6370 backlight binding documentation.

Signed-off-by: ChiYuan Huang 
Reviewed-by: Rob Herring 
---
 .../leds/backlight/mediatek,mt6370-backlight.yaml  | 92 ++
 1 file changed, 92 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml

diff --git 
a/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
new file mode 100644
index 000..d674212
--- /dev/null
+++ 
b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
@@ -0,0 +1,92 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: 
http://devicetree.org/schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT6370 Backlight
+
+maintainers:
+  - ChiaEn Wu 
+
+description: |
+  This module is part of the MT6370 MFD device.
+  The MT6370 Backlight WLED driver supports up to a 29V output voltage for
+  4 channels of 8 series WLEDs. Each channel supports up to 30mA of current
+  capability with 2048 current steps (11 bits) in exponential or linear
+  mapping curves.
+
+allOf:
+  - $ref: common.yaml#
+
+properties:
+  compatible:
+const: mediatek,mt6370-backlight
+
+  default-brightness:
+minimum: 0
+maximum: 2048
+
+  max-brightness:
+minimum: 0
+maximum: 2048
+
+  enable-gpios:
+description: External backlight 'enable' pin
+maxItems: 1
+
+  mediatek,bled-pwm-enable:
+description: |
+  Enable external PWM input for backlight dimming
+type: boolean
+
+  mediatek,bled-pwm-hys-enable:
+description: |
+  Enable the backlight input-hysteresis for PWM mode
+type: boolean
+
+  mediatek,bled-pwm-hys-input-th-steps:
+$ref: /schemas/types.yaml#/definitions/uint8
+enum: [1, 4, 16, 64]
+description: |
+  The selection of the upper and lower bounds threshold of backlight
+  PWM resolution. If we choose selection 64, the variation of PWM
+  resolution needs more than 64 steps.
+
+  mediatek,bled-ovp-shutdown:
+description: |
+  Enable the backlight shutdown when OVP level triggered
+type: boolean
+
+  mediatek,bled-ovp-microvolt:
+enum: [1700, 2100, 2500, 2900]
+description: |
+  Backlight OVP level selection.
+
+  mediatek,bled-ocp-shutdown:
+description: |
+  Enable the backlight shutdown when OCP level triggerred.
+type: boolean
+
+  mediatek,bled-ocp-microamp:
+enum: [90, 120, 150, 180]
+description: |
+  Backlight OC level selection.
+
+  mediatek,bled-channel-use:
+$ref: /schemas/types.yaml#/definitions/uint8
+description: |
+  Backlight LED channel to be used.
+  Each bit mapping to:
+- 0: CH4
+- 1: CH3
+- 2: CH2
+- 3: CH1
+minimum: 1
+maximum: 15
+
+required:
+  - compatible
+  - mediatek,bled-channel-use
+
+additionalProperties: false
-- 
2.7.4



[PATCH v4 11/13] leds: mt6370: Add Mediatek MT6370 current sink type LED Indicator support

2022-07-04 Thread ChiaEn Wu
From: ChiYuan Huang 

Add Mediatek MT6370 current sink type LED Indicator driver.

Signed-off-by: ChiYuan Huang 
---

v4:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Replace text "const" with "constant" in Kconfig
- Add Copyright in the source code
---
 drivers/leds/Kconfig   |  14 +
 drivers/leds/Makefile  |   1 +
 drivers/leds/leds-mt6370.c | 994 +
 3 files changed, 1009 insertions(+)
 create mode 100644 drivers/leds/leds-mt6370.c

diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index a49979f..71bacb5 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -244,6 +244,20 @@ config LEDS_MT6323
  This option enables support for on-chip LED drivers found on
  Mediatek MT6323 PMIC.
 
+config LEDS_MT6370_RGB
+   tristate "LED Support for MediaTek MT6370 PMIC"
+   depends on LEDS_CLASS
+   depends on MFD_MT6370
+   select LINEAR_RANGE
+   help
+ Say Y here to enable support for MT6370_RGB LED device.
+ In MT6370, there are four channel current-sink LED drivers that
+ support hardware pattern for constant current, PWM, and breath mode.
+ Isink4 channel can also be used as a CHG_VIN power good indicator.
+
+ This driver can also be built as a module. If so the module
+ will be called "leds-mt6370.ko".
+
 config LEDS_S3C24XX
tristate "LED Support for Samsung S3C24XX GPIO LEDs"
depends on LEDS_CLASS
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index 4fd2f92..557be42 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -62,6 +62,7 @@ obj-$(CONFIG_LEDS_MIKROTIK_RB532) += leds-rb532.o
 obj-$(CONFIG_LEDS_MLXCPLD) += leds-mlxcpld.o
 obj-$(CONFIG_LEDS_MLXREG)  += leds-mlxreg.o
 obj-$(CONFIG_LEDS_MT6323)  += leds-mt6323.o
+obj-$(CONFIG_LEDS_MT6370_RGB)  += leds-mt6370.o
 obj-$(CONFIG_LEDS_NET48XX) += leds-net48xx.o
 obj-$(CONFIG_LEDS_NETXBIG) += leds-netxbig.o
 obj-$(CONFIG_LEDS_NIC78BX) += leds-nic78bx.o
diff --git a/drivers/leds/leds-mt6370.c b/drivers/leds/leds-mt6370.c
new file mode 100644
index 000..1038232
--- /dev/null
+++ b/drivers/leds/leds-mt6370.c
@@ -0,0 +1,994 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2022 Richtek Technology Corp.
+ *
+ * Author: Alice Chen 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+enum {
+   MT6370_LED_ISNK1 = 0,
+   MT6370_LED_ISNK2,
+   MT6370_LED_ISNK3,
+   MT6370_LED_ISNK4,
+   MT6370_MAX_LEDS
+};
+
+enum mt6370_led_mode {
+   MT6370_LED_PWM_MODE = 0,
+   MT6370_LED_BREATH_MODE,
+   MT6370_LED_REG_MODE,
+   MT6370_LED_MAX_MODE
+};
+
+enum mt6370_led_field {
+   F_RGB_EN = 0,
+   F_CHGIND_EN,
+   F_LED1_CURR,
+   F_LED2_CURR,
+   F_LED3_CURR,
+   F_LED4_CURR,
+   F_LED1_MODE,
+   F_LED2_MODE,
+   F_LED3_MODE,
+   F_LED4_MODE,
+   F_LED1_DUTY,
+   F_LED2_DUTY,
+   F_LED3_DUTY,
+   F_LED4_DUTY,
+   F_LED1_FREQ,
+   F_LED2_FREQ,
+   F_LED3_FREQ,
+   F_LED4_FREQ,
+   F_MAX_FIELDS
+};
+
+enum mt6370_led_ranges {
+   R_LED123_CURR = 0,
+   R_LED4_CURR,
+   R_LED_TRFON,
+   R_LED_TOFF,
+   R_MAX_RANGES,
+};
+
+enum mt6370_pattern {
+   P_LED_TR1 = 0,
+   P_LED_TR2,
+   P_LED_TF1,
+   P_LED_TF2,
+   P_LED_TON,
+   P_LED_TOFF,
+   P_MAX_PATTERNS
+};
+
+#define MT6370_REG_DEV_INFO0x100
+#define MT6370_REG_RGB1_DIM0x182
+#define MT6370_REG_RGB2_DIM0x183
+#define MT6370_REG_RGB3_DIM0x184
+#define MT6370_REG_RGB_EN  0x185
+#define MT6370_REG_RGB1_ISNK   0x186
+#define MT6370_REG_RGB2_ISNK   0x187
+#define MT6370_REG_RGB3_ISNK   0x188
+#define MT6370_REG_RGB1_TR 0x189
+#define MT6370_REG_RGB_CHRIND_DIM  0x192
+#define MT6370_REG_RGB_CHRIND_CTRL 0x193
+#define MT6370_REG_RGB_CHRIND_TR   0x194
+
+#define MT6372_REG_RGB_EN  0x182
+#define MT6372_REG_RGB1_ISNK   0x183
+#define MT6372_REG_RGB2_ISNK   0x184
+#define MT6372_REG_RGB3_ISNK   0x185
+#define MT6372_REG_RGB4_ISNK   0x186
+#define MT6372_REG_RGB1_DIM0x187
+#define MT6372_REG_RGB2_DIM0x188
+#define MT6372_REG_RGB3_DIM0x189
+#define MT6372_REG_RGB4_DIM0x18A
+#define MT6372_REG_RGB12_FREQ  0x18B
+#define MT6372_REG_RGB34_FREQ  0x18C
+#define MT6372_REG_RGB1_TR 0x18D
+
+#define MT6370_VENID_MASK  GENMASK(7, 4)
+#define MT6370_CHEN_BIT(id)

[PATCH v4 00/13] Add Mediatek MT6370 PMIC support

2022-07-04 Thread ChiaEn Wu
From: ChiaEn Wu 

This patch series add Mediatek MT6370 PMIC support. The 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.

In this series of patches, we based on Andy Shevchenko's mfd patch used to
adjust the Makefile order.
(https://lore.kernel.org/all/20220616182524.7956-2-andriy.shevche...@linux.intel.com/)
Among with this, we took some changes for MT6370 and refined the MT6370 device
tree files to comply with DT specifications.

Thank you,
ChiaEn Wu

---
Changes in v4:
- In patch 02/13:
- Add minItems of "io-channel-names"
- Replace text "Mediatek" with "MediaTek"

- In patch 06/13:
- Roll back all "$ref: " to v2 patch style (using
  "/schemas/...")

- In patch 07/13:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Replace "first break and then return" with "return directly"
  in
  "mt6370_check_vendor_info()"
- Add module name related description in Kconfig helptext
- Add Copyright in the source code
- Add header file "mt6370.h" for all "#define IRQ"
- Adjust Makefile order of MT6370
- Refine "bank_idx" and "bank_addr" in
  "mt6375_regmap_read()" / "mt6375_regmap_write()"
- Refine redundant "else if" in "mt6370_regmap_read()"

- In patch 08/13:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Replace "first ret=regulator_(dis/en)able and then return"
  with "return directly" in "mt6370_tcpc_set_vbus()"
- Replace header file  with 
- Add Copyright in the source code
- Add module name related description in Kconfig helptext
- Remove header file 
- Refine all probe error by using dev_err_probe()

- In patch 09/13:
- Replace text "Mediatek" with "MediaTek"
- Replace all "first dev_err() and then return" with "return
  dev_err_probe()"
- Add Copyright in the source code
- Add module name related description in Kconfig
- Add unit suffix of macro "ADC_CONV_POLLING_TIME"
- Add new macro "ADC_CONV_TIME_MS"
- Adjust the position of include file 
- Adjust the postions between  and
  
- Fix some incorrect characters

- In patch 10/13:
- Replace text "Mediatek" with "MediaTek" in Kconfig and
  MODULE_DESCRIPTION()
- Replace "mt6370_chg_val_to_reg" and "mt6370_chg_reg_to_val"
  with
  "linear_range" API
- Replace "first break and then return" with "return directly"
  in all
  cases of get/set power_supply_property
- Replace all "first dev_err() and then return" with "return
  dev_err_probe()"
- Replace all "return IS_ERR(priv->rdev) ? PTR_ERR(priv->rdev) :
  0" with
  "PTR_ERR_OR_ZERO()"
- Replace "priv->dev->of_node" with "dev_of_node()"
- Add Copyright in the source code
- Add module name related description in Kconfig helptext
- Add proper unit of "MT6370_MIVR_IBUS_TH"
- Add error check in "mt6370_chg_get_status"
- Remove including  header file
- Remove redundant comma of every enum terminator line
- Remove unwanted blank lines
- Remove the useless label (toggle_cfo_exit:)
- Remove using atomic
- Remove using of_match_ptr()
- Fix some incorrect characters
- Fix updating wrong bits when using ena_gpiod of OTG regulator
- Adjust the probe order in probe()

- In patch 11/13:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Replace text "const" with "constant" in Kconfig
- Add Copyright in the source code

- In patch 12/13:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Add Copyright in the source code

- In patch 13/13:
- Replace text "Mediatek" with "MediaTek" in Kconfig
- Add Copyright in the source code
- Revise the comment of "PWM HYS STEPS"


Changes in v3:
- Remove ADC ABI file, which is added in v2 Patch 7
- In patch 02/14:
- Add items and remove maxItems of io-channels
- Add io-channel-names and describe each item
- Add "unevaluatedPr

Re: [PATCH v3 11/14] power: supply: mt6370: Add Mediatek MT6370 charger driver

2022-06-29 Thread ChiaEn Wu
Hi Andy,

Sorry for the late reply, I have some questions to ask you below. Thanks!

Andy Shevchenko  於 2022年6月24日 週五 凌晨2:56寫道:
>
> On Thu, Jun 23, 2022 at 2:00 PM ChiaEn Wu  wrote:
> >
> > From: ChiaEn Wu 
> >
> > Add Mediatek MT6370 charger driver.
>
> ...
>
> > +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.
>
> Module name?
>
> ...
>
> > +#include 
>
> This usually goes after linux/*
>
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
>
> > +#include 
>
>
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
>
> ...
>
> > +#define MT6370_MIVR_IBUS_TH10  /* 100 mA */
>
> Instead of comment, add proper units.
>
> ...
>
> > +   MT6370_USB_STAT_DCP,
> > +   MT6370_USB_STAT_CDP,
> > +   MT6370_USB_STAT_MAX,
>
> No comma for a terminator line.
>
> ...
>
> > +static inline u32 mt6370_chg_val_to_reg(const struct mt6370_chg_range 
> > *range,
> > +   u32 val)
> > +static inline u32 mt6370_chg_reg_to_val(const struct mt6370_chg_range 
> > *range,
> > +   u8 reg)
>
> I'm wondering if you can use the
> https://elixir.bootlin.com/linux/v5.19-rc3/source/include/linux/linear_range.h
> APIs.

Thanks for your helpful comments!
I will refine it in the next patch!

>
> ...
>
> > +   int ret = 0;
>
> This seems a redundant assignment, see below.
>
> > +   rcfg->ena_gpiod = fwnode_gpiod_get_index(of_fwnode_handle(of),
> > +"enable", 0,
>
> For index == 0 don't use _index API.
>
> > +GPIOD_OUT_LOW |
> > +
> > GPIOD_FLAGS_BIT_NONEXCLUSIVE,
> > +rdesc->name);
> > +   if (IS_ERR(rcfg->ena_gpiod)) {
> > +   dev_err(priv->dev, "Failed to requeset OTG EN Pin\n");
>
> request
>
> > +   rcfg->ena_gpiod = NULL;
>
> So, use _optional and return any errors you got.

These days, I tried to use various APIs in , and also
try to use _optional APIs.
But my OTG regulator node is a child node of the charger node, like below.

// copy-paste from our mfd dt-binding example
charger {
  compatible = "mediatek,mt6370-charger";
  interrupts = <48>, <68>, <6>;
  interrupt-names = "attach_i", "uvp_d_evt", "mivr";
  io-channels = <_adc MT6370_CHAN_IBUS>;

  mt6370_otg_vbus: usb-otg-vbus-regulator {
regulator-name = "mt6370-usb-otg-vbus";
regulator-min-microvolt = <435>;
regulator-max-microvolt = <580>;
regulator-min-microamp = <50>;
regulator-max-microamp = <300>;
  };
};

Hence, if I use _optional APIs, it will always get NULL.
And, If I use 'gpiod_get_from_of_node' here, this API will only parse
the 'enable' property, not 'enable-gpio' or 'enable-gpios', we need to
add the '-gpio' suffix before we use this API.

Only 'fwnode_gpiod_get_index' can match this case. Although fwnode
parsing is not preferred, 'of_parse_cb' already can guarantee the
callback will only be used by the regulator of_node parsing.

>
> > +   } else {
> > +   val = MT6370_OPA_MODE_MASK | MT6370_OTG_PIN_EN_MASK;
> > +   ret = regmap_update_bits(priv->regmap, MT6370_REG_CHG_CTRL1,
> > +val, val);
> > +   if (ret)
> > +   dev_err(priv->dev, "Failed to set otg bits\n");
> > +   }
>
> ...
>
> > +   irq_num = platform_get_irq_byname(pdev, irq_name);
>
> > +
>
> Unwant

Re: [PATCH v2 08/15] mfd: mt6370: Add Mediatek MT6370 support

2022-06-27 Thread ChiaEn Wu
Hi Lee,

Thanks for your reply!

Lee Jones  於 2022年6月27日 週一 晚上10:14寫道:
>
> On Sat, 18 Jun 2022, ChiaEn Wu wrote:
>
> > Hi Lee,
> >
> > Thanks for your helpful comments, we have some questions and replies below.
> >
> > Lee Jones  於 2022年6月16日 週四 清晨6:49寫道:
> >
> > >
> > > On Mon, 13 Jun 2022, ChiaEn Wu wrote:
> > >
> > > > From: ChiYuan Huang 
> > > >
> > > > Add Mediatek MT6370 MFD support.
> > > >
> > > > Signed-off-by: ChiYuan Huang 
> > > > ---
> > > >  drivers/mfd/Kconfig  |  13 ++
> > > >  drivers/mfd/Makefile |   1 +
> > > >  drivers/mfd/mt6370.c | 349 +++
> > > >  3 files changed, 363 insertions(+)
> > > >  create mode 100644 drivers/mfd/mt6370.c
> > > >
> > > > diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
> > > > index 3b59456f5545..d9a7524a3e0e 100644
> > > > --- a/drivers/mfd/Kconfig
> > > > +++ b/drivers/mfd/Kconfig
> > > > @@ -937,6 +937,19 @@ config MFD_MT6360
> > > > PMIC part includes 2-channel BUCKs and 2-channel LDOs
> > > > LDO part includes 4-channel LDOs
> > > >
> > > > +config MFD_MT6370
> > > > + tristate "Mediatek MT6370 SubPMIC"
> > > > + select MFD_CORE
> > > > + select REGMAP_I2C
> > > > + select REGMAP_IRQ
> > > > + depends on I2C
> > > > + help
> > > > +   Say Y here to enable MT6370 SubPMIC functional support.
> > > > +   It integrate single cell battery charger with adc monitoring, 
> > > > RGB
> > >
> > > s/integrates/consists of a/
> > >
> > > "ADC"
> >
> > We will fine it in the next patch.
> >
> > >
> > > > +   LEDs, dual channel flashlight, WLED backlight driver, display 
> > > > bias
> > >
> > > > +   voltage supply, one general purpose LDO, and cc logic
> > > > +   controller with USBPD commmunication capable.
> > >
> > > The last part makes no sense - "and is USBPD"?
> >
> > If we modify this help text to
> > "one general purpose LDO, and the USB Type-C & PD controller complies
> > with the latest USB Type-C and PD standards",
> > did these modifications meet your expectations?
>
> "one general purpose LDO and a USB Type-C & PD controller that
> complies with the latest USB Type-C and PD standards"
>
> Better?

Yes, thanks! We will modify it like that in the next patch.

>
> > > >  config MFD_MT6397
> > > >   tristate "MediaTek MT6397 PMIC Support"
> > > >   select MFD_CORE
> > > > diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
> > > > index 858cacf659d6..62b27125420e 100644
> > > > --- a/drivers/mfd/Makefile
> > > > +++ b/drivers/mfd/Makefile
> > > > @@ -242,6 +242,7 @@ obj-$(CONFIG_INTEL_SOC_PMIC_BXTWC)+= 
> > > > intel_soc_pmic_bxtwc.o
> > > >  obj-$(CONFIG_INTEL_SOC_PMIC_CHTWC)   += intel_soc_pmic_chtwc.o
> > > >  obj-$(CONFIG_INTEL_SOC_PMIC_CHTDC_TI)+= 
> > > > intel_soc_pmic_chtdc_ti.o
> > > >  obj-$(CONFIG_MFD_MT6360) += mt6360-core.o
> > > > +obj-$(CONFIG_MFD_MT6370) += mt6370.o
> > > >  mt6397-objs  := mt6397-core.o mt6397-irq.o mt6358-irq.o
> > > >  obj-$(CONFIG_MFD_MT6397) += mt6397.o
> > > >  obj-$(CONFIG_INTEL_SOC_PMIC_MRFLD)   += intel_soc_pmic_mrfld.o
> > > > diff --git a/drivers/mfd/mt6370.c b/drivers/mfd/mt6370.c
> > > > new file mode 100644
> > > > index ..6af9f73c9c0c
> > > > --- /dev/null
> > > > +++ b/drivers/mfd/mt6370.c
> > > > @@ -0,0 +1,349 @@
> > > > +// SPDX-License-Identifier: GPL-2.0
> > > > +
> > > > +#include 
> > > > +#include 
> > > > +#include 
> > > > +#include 
> > > > +#include 
> > > > +#include 
> > > > +#include 
> > > > +
> > > > +enum {
> > > > + MT6370_USBC_I2C = 0,
> > > > + MT6370_PMU_I2C,
> > > > + MT6370_MAX_I2C
> > > > +};
> > > > +
> > > > +#define MT6370_REG_DEV_INFO  0x100
> > > > +#define MT6370_REG_CHG_IRQ1  0x1C0
> > > > +#define MT6370_REG_CHG_MAS

Re: [PATCH v3 09/14] regulator: mt6370: Add mt6370 DisplayBias and VibLDO support

2022-06-24 Thread ChiaEn Wu
Hi Andy,

Thanks for your helpful comments!

Andy Shevchenko  於 2022年6月24日 週五 凌晨2:19寫道:
>
> On Thu, Jun 23, 2022 at 2:00 PM ChiaEn Wu  wrote:
> >
> > From: ChiYuan Huang 
> >
> > Add mt6370 DisplayBias and VibLDO support.
>
> ...
>
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
>
> > +#include 
>
> Any users of this? (See below)
>
> > +#include 
> > +#include 
> > +#include 
> > +#include 
>
> ...
>
> > +#define MT6370_LDO_MINUV   160
> > +#define MT6370_LDO_STPUV   20
> > +#define MT6370_LDO_N_VOLT  13
> > +#define MT6370_DBVBOOST_MINUV  400
> > +#define MT6370_DBVBOOST_STPUV  5
> > +#define MT6370_DBVBOOST_N_VOLT 45
> > +#define MT6370_DBVOUT_MINUV400
> > +#define MT6370_DBVOUT_STPUV5
> > +#define MT6370_DBVOUT_N_VOLT   41
>
> If UV is a unit suffix, make it _UV.
>
> ...
>
> > +   .of_match = of_match_ptr("dsvbst"),
>
> Would it even be called / used if CONFIG_OF=n?

We got a notification from Mark telling us that this patch has been
applied to git.
( 
https://lore.kernel.org/linux-arm-kernel/165599931844.321775.8085559092337130067.b4...@kernel.org/
)
So, should we need to make any other changes in the next submission?

>
> ...
>
> > +   .regulators_node = of_match_ptr("regulators"),
>
> Ditto.
>
> ...
>
> > +   for (i = 0; i < ARRAY_SIZE(mt6370_irqs); i++) {
> > +   irq = platform_get_irq_byname(pdev, mt6370_irqs[i].name);
> > +
> > +   rdev = priv->rdev[mt6370_irqs[i].rid];
> > +
> > +   ret = devm_request_threaded_irq(priv->dev, irq, NULL,
> > +   mt6370_irqs[i].handler, 0,
> > +   mt6370_irqs[i].name, rdev);
> > +   if (ret) {
>
> > +   dev_err(priv->dev,
> > +   "Failed to register (%d) interrupt\n", i);
> > +   return ret;
>
> return dev_err_probe(...); ?
>
> > +   }
> > +   }
>
> ...
>
> > +   for (i = 0; i < MT6370_MAX_IDX; i++) {
> > +   rdev = devm_regulator_register(priv->dev,
> > +  mt6370_regulator_descs + i,
> > +  );
> > +   if (IS_ERR(rdev)) {
>
> > +   dev_err(priv->dev,
> > +   "Failed to register (%d) regulator\n", i);
> > +   return PTR_ERR(rdev);
>
> return dev_err_probe(...); ?
>
> > +   }
> > +
> > +   priv->rdev[i] = rdev;
> > +   }
>
> ...
>
> > +   if (!priv->regmap) {
> > +   dev_err(>dev, "Failed to init regmap\n");
> > +   return -ENODEV;
> > +   }
>
> return dev_err_probe(...);
>
> --
> With Best Regards,
> Andy Shevchenko

Best regards,
ChiaEn Wu


Re: [PATCH v3 07/14] mfd: mt6370: Add Mediatek MT6370 support

2022-06-24 Thread ChiaEn Wu
Hi Andy,

Thanks for your helpful comments! We have some questions below.

Andy Shevchenko  於 2022年6月24日 週五 凌晨2:01寫道:
>
> On Thu, Jun 23, 2022 at 1:59 PM ChiaEn Wu  wrote:
> >
> > From: ChiYuan Huang 
> >
> > Add Mediatek MT6370 MFD support.
>
> ...
>
> > +config MFD_MT6370
> > +   tristate "Mediatek MT6370 SubPMIC"
> > +   select MFD_CORE
> > +   select REGMAP_I2C
> > +   select REGMAP_IRQ
> > +   depends on I2C
> > +   help
> > + Say Y here to enable MT6370 SubPMIC functional support.
> > + It consists 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.
>
> What will be the module name in case it's chosen to be built as a module?

OK, we will add related text in the next patch! Thanks!

>
> ...
>
> >  obj-$(CONFIG_INTEL_SOC_PMIC_BXTWC)  += intel_soc_pmic_bxtwc.o
> >  obj-$(CONFIG_INTEL_SOC_PMIC_CHTWC) += intel_soc_pmic_chtwc.o
> >  obj-$(CONFIG_INTEL_SOC_PMIC_CHTDC_TI)  += intel_soc_pmic_chtdc_ti.o
> >  obj-$(CONFIG_MFD_MT6360)   += mt6360-core.o
> > +obj-$(CONFIG_MFD_MT6370)   += mt6370.o
> >  mt6397-objs:= mt6397-core.o mt6397-irq.o mt6358-irq.o
> >  obj-$(CONFIG_MFD_MT6397)   += mt6397.o
> >  obj-$(CONFIG_INTEL_SOC_PMIC_MRFLD) += intel_soc_pmic_mrfld.o
>
> This whole bunch of drivers is in the wrong place in Makefile.
>
> https://lore.kernel.org/all/20220616182524.7956-2-andriy.shevche...@linux.intel.com/
>

hmm... So shall we need to cherry-pick your this patch first,
then modify the Makefile before the next submission??

> ...
>
> > +#define MT6370_REG_MAXADDR 0x1FF
>
> Wondering if (BIT(10) - 1) gives a better hint on how hardware limits
> this (so it will be clear it's 10-bit address).

well... This "0x1FF" is just a virtual mapping value to map the max
address of the PMU bank(0x1XX).
So, I feel its means is different from using (BIT(10) - 1) here.

>
> ...
>
> > +static int mt6370_check_vendor_info(struct mt6370_info *info)
> > +{
> > +   unsigned int devinfo;
> > +   int ret;
> > +
> > +   ret = regmap_read(info->regmap, MT6370_REG_DEV_INFO, );
> > +   if (ret)
> > +   return ret;
> > +
> > +   switch (FIELD_GET(MT6370_VENID_MASK, devinfo)) {
> > +   case MT6370_VENID_RT5081:
> > +   case MT6370_VENID_RT5081A:
> > +   case MT6370_VENID_MT6370:
> > +   case MT6370_VENID_MT6371:
> > +   case MT6370_VENID_MT6372P:
> > +   case MT6370_VENID_MT6372CP:
>
> return 0;
>
> > +   break;
> > +   default:
> > +   dev_err(info->dev, "Unknown Vendor ID 0x%02x\n", devinfo);
> > +   return -ENODEV;
> > +   }
> > +
> > +   return 0;
>
> ...and drop these two lines?

OK! We will refine it in the next patch!

>
> > +}
>
> ...
>
> > +   bank_idx = *(u8 *)reg_buf;
> > +   bank_addr = *(u8 *)(reg_buf + 1);
>
> Why not
>
>   const u8 *u8_buf = reg_buf;
>
>   bank_idx = u8_buf[0];
>   bank_addr = u8_buf[1];
>
> ?

We will refine it in the next patch! Thanks!

>
> ...
>
> > +   if (ret < 0)
> > +   return ret;
> > +   else if (ret != val_size)
>
> Redundant 'else'.

I'm not quite sure what you mean, so I made the following changes first.

   if (ret < 0)
  return ret;
   if (ret != val_size)
  return -EIO;

I don't know if it meets your expectations??

>
> > +   return -EIO;
>
> ...
>
> > +   bank_idx = *(u8 *)data;
> > +   bank_addr = *(u8 *)(data + 1);
>
> As per above.
>
> --
> With Best Regards,
> Andy Shevchenko

Best regards,
ChiaEn Wu


Re: [PATCH v3 14/14] video: backlight: mt6370: Add Mediatek MT6370 support

2022-06-24 Thread ChiaEn Wu
Hi Daniel,

Thanks for your comments!

Daniel Thompson  於 2022年6月23日 週四 晚上9:43寫道:
>
> On Thu, Jun 23, 2022 at 07:56:31PM +0800, ChiaEn Wu wrote:
> > From: ChiaEn Wu 
> >
> > Add Mediatek MT6370 Backlight support.
> >
> > Signed-off-by: ChiaEn Wu 
>
> > diff --git a/drivers/video/backlight/Kconfig 
> > b/drivers/video/backlight/Kconfig
> > index a003e02..7cd823d 100644
> > 
> > +static int mt6370_init_backlight_properties(struct mt6370_priv *priv,
> > + struct backlight_properties 
> > *props)
> > +{
> > + struct device *dev = priv->dev;
> > + u8 prop_val;
> > + u32 brightness, ovp_uV, ocp_uA;
> > + unsigned int mask, val;
> > + int ret;
> > +
> > + /* Vendor optional properties */
> > + val = 0;
> > + if (device_property_read_bool(dev, "mediatek,bled-pwm-enable"))
> > + val |= MT6370_BL_PWM_EN_MASK;
> > +
> > + if (device_property_read_bool(dev, "mediatek,bled-pwm-hys-enable"))
> > + val |= MT6370_BL_PWM_HYS_EN_MASK;
> > +
> > + ret = device_property_read_u8(dev,
> > +   "mediatek,bled-pwm-hys-input-th-steps",
> > +   _val);
> > + if (!ret) {
> > + prop_val = clamp_val(prop_val,
> > +  MT6370_BL_PWM_HYS_TH_MIN_STEP,
> > +  MT6370_BL_PWM_HYS_TH_MAX_STEP);
> > + /*
> > +  * prop_val =  1  -->  1 steps --> 0x00
> > +  * prop_val =  2 ~  4 -->  4 steps --> 0x01
> > +  * prop_val =  5 ~ 16 --> 16 steps --> 0x10
> > +  * prop_val = 17 ~ 64 --> 64 steps --> 0x11
>
>   ^
> These numbers are binary, not hex, right? If so, the comments
> should be 0b00 to 0b03 .

Ohh! Yes! These numbers are binary!
I so apologize for making this mistake...
I will revise the comments in the next patch!
Thank you so much!

>
>
> > +  */
> > + prop_val = (ilog2(roundup_pow_of_two(prop_val)) + 1) >> 1;
> > + val |= prop_val << (ffs(MT6370_BL_PWM_HYS_SEL_MASK) - 1);
> > + }
> > +
> > + ret = regmap_update_bits(priv->regmap, MT6370_REG_BL_PWM,
> > +  val, val);
> > + if (ret)
> > + return ret;
>
> Overall, I like this approach! Easy to read and understand.
>
>
> > 
> > +static int mt6370_bl_probe(struct platform_device *pdev)
> > +{
> > + struct mt6370_priv *priv;
> > + struct backlight_properties props = {
> > + .type = BACKLIGHT_RAW,
> > + .scale = BACKLIGHT_SCALE_LINEAR,
>
> Sorry, I missed this before but the KConfig comment says that the
> backlight can support both linear and exponential curves.
>
> Is there a good reason to default to linear?

Well...
The customers who used this PMIC have very few or even no use exponential curve,
so I set the default to linear.

If you think this is inappropriate, I will add a DT property to
control this feature in the next patch!

By the way,
I found some mistakes in my probe() function... I didn't use "return"
when I use dev_err_probe()...
I will refine it in the next patch!

>
>
> Daniel.
> >

Best regards,
ChiaEn Wu


Re: [PATCH v3 05/14] dt-bindings: backlight: Add Mediatek MT6370 backlight

2022-06-24 Thread ChiaEn Wu
Hi Joe,

Joe Simmons-Talbott  於 2022年6月23日 週四 晚上9:17寫道:
>
> On Thu, Jun 23, 2022 at 07:56:22PM +0800, ChiaEn Wu wrote:
> > From: ChiYuan Huang 
> >
> > Add mt6370 backlight binding documentation.
> >
> > Signed-off-by: ChiYuan Huang 
> > ---
> >
> > v3
> > - Rename "mediatek,bled-pwm-hys-input-threshold-steps" to
> >   "mediatek,bled-pwm-hys-input-th-steps"
> > - Refine "bled-pwm-hys-input-th-steps", "bled-ovp-microvolt",
> >   "bled-ocp-microamp" enum values
> > ---
> >  .../leds/backlight/mediatek,mt6370-backlight.yaml  | 92 
> > ++
> >  1 file changed, 92 insertions(+)
> >  create mode 100644 
> > Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
> >
> > diff --git 
> > a/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
> >  
> > b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
> > new file mode 100644
> > index 000..26563ae
> > --- /dev/null
> > +++ 
> > b/Documentation/devicetree/bindings/leds/backlight/mediatek,mt6370-backlight.yaml
> > @@ -0,0 +1,92 @@
> > +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> > +%YAML 1.2
> > +---
> > +$id: 
> > http://devicetree.org/schemas/leds/backlight/mediatek,mt6370-backlight.yaml#
> > +$schema: http://devicetree.org/meta-schemas/core.yaml#
> > +
> > +title: Mediatek MT6370 Backlight
> > +
> > +maintainers:
> > +  - ChiaEn Wu 
> > +
> > +description: |
> > +  This module is part of the MT6370 MFD device.
> > +  The MT6370 Backlight WLED driver supports up to a 29V output voltage for
> > +  4 channels of 8 series WLEDs. Each channel supports up to 30mA of current
> > +  capability with 2048 current steps (11 bits) in exponential or linear
> > +  mapping curves.
> > +
> > +allOf:
> > +  - $ref: common.yaml#
> > +
> > +properties:
> > +  compatible:
> > +const: mediatek,mt6370-backlight
> > +
> > +  default-brightness:
> > +minimum: 0
> > +maximum: 2048
> > +
> > +  max-brightness:
> > +minimum: 0
> > +maximum: 2048
> > +
> > +  enable-gpios:
> > +description: External backlight 'enable' pin
> > +maxItems: 1
> > +
> > +  mediatek,bled-pwm-enable:
> > +description: |
> > +  Enable external PWM input for backlight dimming
> > +type: boolean
> > +
> > +  mediatek,bled-pwm-hys-enable:
> > +description: |
> > +  Enable the backlight input-hysteresis for PWM mode
> > +type: boolean
> > +
> > +  mediatek,bled-pwm-hys-input-th-steps:
> > +$ref: /schemas/types.yaml#/definitions/uint8
> > +enum: [1, 4, 16, 64]
> > +description: |
> > +  The selection of the upper and lower bounds threshold of backlight
> > +  PWM resolution. If we choose selection 64, the variation of PWM
> > +  resolution needs over than 64 steps.
>
> more than?
>
> Thanks,
> Joe
>

Thanks for your helpful comments!
I will revise this in the next patch. Thanks!

> > +
> > +  mediatek,bled-ovp-shutdown:
> > +description: |
> > +  Enable the backlight shutdown when OVP level triggered
> > +type: boolean
> > +
> > +  mediatek,bled-ovp-microvolt:
> > +enum: [1700, 2100, 2500, 2900]
> > +description: |
> > +  Backlight OVP level selection.
> > +
> > +  mediatek,bled-ocp-shutdown:
> > +description: |
> > +  Enable the backlight shutdown when OCP level triggerred.
> > +type: boolean
> > +
> > +  mediatek,bled-ocp-microamp:
> > +enum: [90, 120, 150, 180]
> > +description: |
> > +  Backlight OC level selection.
> > +
> > +  mediatek,bled-channel-use:
> > +$ref: /schemas/types.yaml#/definitions/uint8
> > +description: |
> > +  Backlight LED channel to be used.
> > +  Each bit mapping to:
> > +- 0: CH4
> > +- 1: CH3
> > +- 2: CH2
> > +- 3: CH1
> > +minimum: 1
> > +maximum: 15
> > +
> > +required:
> > +  - compatible
> > +  - mediatek,bled-channel-use
> > +
> > +additionalProperties: false
> > --
> > 2.7.4
> >

Best regards,
ChiaEn Wu


  1   2   >