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

2022-07-16 Thread Andy Shevchenko
On Fri, Jul 15, 2022 at 1:29 PM ChiaEn Wu  wrote:

> The MediaTek MT6370 is a highly-integrated smart power management IC,
> whichincludes a single cell Li-Ion/Li-Polymer switching battery

which includes

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

channels

> The commit add the support of MT6370 FLASH LED.

Add the

...

> +#define MT6370_FLCSEN_MASK_ALL (BIT(0) | BIT(1))

GENMASK()

...

> +   for (i = 0; i < MT6370_MAX_LEDS; i++) {
> +   ret = regmap_update_bits(priv->regmap,
> +   MT6370_REG_FLEDISTRB(i),
> +   MT6370_ISTROBE_MASK, 
> flevel[i]);
> +   if (ret)
> +   return ret;
> +   }
> +   } else {

> +   ret = regmap_update_bits(priv->regmap,
> +MT6370_REG_FLEDISTRB(led->led_no),
> +MT6370_ISTROBE_MASK, val);
> +   }
> +   return ret;

return regmap_update_bits(...);
  }

  return 0;

...

> +   /*
> +* If the flash need to be on,

needs

> +* config the flash current ramping up to the setting value.
> +* Else, always recover back to the minimum one.
> +*/

...

> +   /* For the flash turn on/off, need to wait HW ramping up/down time

to turn

> +* 5ms/500us to prevent the unexpected problem.
> +*/

Wrong multi-line comment style.

> +

No need for a blank line.

> +   if (!prev && curr)
> +   usleep_range(5000, 6000);
> +   else if (prev && !curr)
> +   udelay(500);

...

> +static int mt6370_led_register(struct device *parent, struct mt6370_led *led,
> +   struct led_init_data *init_data)
> +{
> +   struct v4l2_flash_config v4l2_config = {0};
> +   int ret;
> +
> +   ret = devm_led_classdev_flash_register_ext(parent, &led->flash,
> +  init_data);
> +   if (ret) {
> +   dev_err(parent, "Couldn't register flash %d\n", led->led_no);
> +   return ret;

return dev_err_probe() here and everywhere where it is about probe stage.

> +   }
> +
> +   mt6370_init_v4l2_flash_config(led, &v4l2_config);
> +   led->v4l2_flash = v4l2_flash_init(parent, init_data->fwnode,
> + &led->flash, &v4l2_flash_ops,
> + &v4l2_config);
> +   if (IS_ERR(led->v4l2_flash)) {
> +   dev_err(parent, "Failed to register %d v4l2 sd\n", 
> led->led_no);
> +   return PTR_ERR(led->v4l2_flash);
> +   }
> +
> +   return 0;
> +}

...

> +   num = fwnode_property_count_u32(init_data->fwnode, "led-sources");
> +   if (num < 1 || num > MT6370_MAX_LEDS) {
> +   dev_err(priv->dev,
> +   "Not specified or wrong number of led-sources\n");
> +   return -EINVAL;

Ditto.

> +   }

...

> +   ret = fwnode_property_read_u32(init_data->fwnode, "led-max-microamp",
> +  &val);

One line?

...

> +   val = clamp_align(val, MT6370_STRBTO_MIN_US, 
> MT6370_STRBTO_MAX_US,
> + MT6370_STRBTO_STEP_US);

I would name it mt6370_clamp() to avoid potential collision in the
global namespace in the future.

-- 
With Best Regards,
Andy Shevchenko


[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 leds_coun