From nobody Thu Nov 14 17:50:30 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2EF74C64EC4 for ; Fri, 10 Mar 2023 06:56:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230095AbjCJG4u (ORCPT ); Fri, 10 Mar 2023 01:56:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229900AbjCJG4m (ORCPT ); Fri, 10 Mar 2023 01:56:42 -0500 Received: from mg.richtek.com (mg.richtek.com [220.130.44.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0CEF8F7ED5; Thu, 9 Mar 2023 22:56:35 -0800 (PST) X-MailGates: (flag:4,DYNAMIC,BADHELO,RELAY,NOHOST:PASS)(compute_score:DE LIVER,40,3) Received: from 192.168.10.46 by mg.richtek.com with MailGates ESMTP Server V5.0(20043:0:AUTH_RELAY) (envelope-from ); Fri, 10 Mar 2023 14:56:02 +0800 (CST) Received: from ex3.rt.l (192.168.10.46) by ex3.rt.l (192.168.10.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 10 Mar 2023 14:56:02 +0800 Received: from linuxcarl2.richtek.com (192.168.10.154) by ex3.rt.l (192.168.10.45) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 10 Mar 2023 14:56:02 +0800 From: ChiaEn Wu To: , , , , , , CC: , , , , , , , , Alice Chen , ChiaEn Wu Subject: [PATCH v18 2/3] leds: flash: mt6370: Add MediaTek MT6370 flashlight support Date: Fri, 10 Mar 2023 14:55:56 +0800 Message-ID: <52480420a160e5a4c71715fbbf105e684a16e7c2.1678430444.git.chiaen_wu@richtek.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 MT6370 Flash LED driver. Flash LED in MT6370 has 2 channels and support torch/strobe mode. Acked-by: Jacek Anaszewski Co-developed-by: Alice Chen Signed-off-by: Alice Chen Signed-off-by: ChiYuan Huang Signed-off-by: ChiaEn Wu --- v18: - Unwrap each line by 100 chars limit. - For joint flash, add more comments above the source code. - Remove 'sentinel' comment in of_device_id table declaration. --- drivers/leds/flash/Kconfig | 13 + drivers/leds/flash/Makefile | 1 + drivers/leds/flash/leds-mt6370-flash.c | 573 +++++++++++++++++++++++++++++= ++++ 3 files changed, 587 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..49d8922 100644 --- a/drivers/leds/flash/Kconfig +++ b/drivers/leds/flash/Kconfig @@ -61,6 +61,19 @@ config LEDS_MT6360 Independent current sources supply for each flash LED support torch and strobe mode. =20 +config LEDS_MT6370_FLASH + tristate "Flash LED Support for MediaTek MT6370 PMIC" + depends on LEDS_CLASS + 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..4665e8e 100644 --- a/drivers/leds/flash/Makefile +++ b/drivers/leds/flash/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 =20 obj-$(CONFIG_LEDS_MT6360) +=3D leds-mt6360.o +obj-$(CONFIG_LEDS_MT6370_FLASH) +=3D leds-mt6370-flash.o obj-$(CONFIG_LEDS_AAT1290) +=3D leds-aat1290.o obj-$(CONFIG_LEDS_AS3645A) +=3D leds-as3645a.o obj-$(CONFIG_LEDS_KTD2692) +=3D leds-ktd2692.o diff --git a/drivers/leds/flash/leds-mt6370-flash.c b/drivers/leds/flash/le= ds-mt6370-flash.c new file mode 100644 index 00000000..dc00c33 --- /dev/null +++ b/drivers/leds/flash/leds-mt6370-flash.c @@ -0,0 +1,573 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 Richtek Technology Corp. + * + * Authors: + * Alice Chen + * ChiYuan Huang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +enum { + MT6370_LED_FLASH1 =3D 0, + MT6370_LED_FLASH2, + MT6370_MAX_LEDS +}; + +/* Virtual definition for multicolor */ + +#define MT6370_REG_FLEDEN 0x17E +#define MT6370_REG_STRBTO 0x173 +#define MT6370_REG_CHGSTAT2 0x1D1 +#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_MASK GENMASK(6, 0) +#define MT6370_STRBTO_MASK GENMASK(6, 0) +#define MT6370_TORCHEN_MASK BIT(3) +#define MT6370_STROBEN_MASK BIT(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_MASK BIT(11) +#define MT6370_FLED2STRBTO_MASK BIT(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_MASK BIT(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 400000 +#define MT6370_ITORCH_DOUBLE_MAX_uA 800000 +#define MT6370_ISTRB_MIN_uA 50000 +#define MT6370_ISTRB_STEP_uA 12500 +#define MT6370_ISTRB_MAX_uA 1500000 +#define MT6370_ISTRB_DOUBLE_MAX_uA 3000000 +#define MT6370_STRBTO_MIN_US 64000 +#define MT6370_STRBTO_STEP_US 32000 +#define MT6370_STRBTO_MAX_US 2432000 + +#define to_mt6370_led(ptr, member) container_of(ptr, struct mt6370_led, me= mber) + +struct mt6370_led { + struct led_classdev_flash flash; + struct v4l2_flash *v4l2_flash; + struct mt6370_priv *priv; + u8 led_no; +}; + +struct mt6370_priv { + 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 le= d_brightness level) +{ + struct mt6370_led *led =3D to_mt6370_led(lcdev, flash.led_cdev); + struct mt6370_priv *priv =3D led->priv; + u32 led_enable_mask =3D led->led_no =3D=3D MT6370_LED_JOINT ? MT6370_FLCS= EN_MASK_ALL : + MT6370_FLCSEN_MASK(led->led_no); + u32 enable_mask =3D MT6370_TORCHEN_MASK | led_enable_mask; + u32 val =3D level ? led_enable_mask : 0; + u32 curr; + int ret, i; + + mutex_lock(&priv->lock); + + /* + * There is only one set of flash control logic, and this flag is used to= check if 'strobe' + * is currently being used. + */ + if (priv->fled_strobe_used) { + dev_warn(lcdev->dev, "Please disable strobe first [%d]\n", priv->fled_st= robe_used); + ret =3D -EBUSY; + goto unlock; + } + + if (level) + curr =3D priv->fled_torch_used | BIT(led->led_no); + else + curr =3D priv->fled_torch_used & ~BIT(led->led_no); + + if (curr) + val |=3D MT6370_TORCHEN_MASK; + + if (level) { + level -=3D 1; + if (led->led_no =3D=3D MT6370_LED_JOINT) { + u32 flevel[MT6370_MAX_LEDS]; + + /* + * There're two flash channels in MT6370. If joint flash output is used, + * torch current will be averaged output from both channels. + */ + flevel[0] =3D level / 2; + flevel[1] =3D level - flevel[0]; + for (i =3D 0; i < MT6370_MAX_LEDS; i++) { + ret =3D regmap_update_bits(priv->regmap, MT6370_REG_FLEDITOR(i), + MT6370_ITORCH_MASK, flevel[i]); + if (ret) + goto unlock; + } + } else { + ret =3D regmap_update_bits(priv->regmap, MT6370_REG_FLEDITOR(led->led_n= o), + MT6370_ITORCH_MASK, level); + if (ret) + goto unlock; + } + } + + ret =3D regmap_update_bits(priv->regmap, MT6370_REG_FLEDEN, enable_mask, = val); + if (ret) + goto unlock; + + priv->fled_torch_used =3D curr; + +unlock: + mutex_unlock(&priv->lock); + return ret; +} + +static int mt6370_flash_brightness_set(struct led_classdev_flash *fl_cdev,= u32 brightness) +{ + /* + * Because of the current spikes when turning on the flash, the brightnes= s should be kept + * by the LED framework. This empty function is used to prevent checking = failure when + * led_classdev_flash registers ops. + */ + return 0; +} + +static int _mt6370_flash_brightness_set(struct led_classdev_flash *fl_cdev= , u32 brightness) +{ + struct mt6370_led *led =3D to_mt6370_led(fl_cdev, flash); + struct mt6370_priv *priv =3D led->priv; + struct led_flash_setting *setting =3D &fl_cdev->brightness; + u32 val =3D (brightness - setting->min) / setting->step; + int ret, i; + + if (led->led_no =3D=3D MT6370_LED_JOINT) { + u32 flevel[MT6370_MAX_LEDS]; + + /* + * There're two flash channels in MT6370. If joint flash output is used,= storbe + * current will be averaged output from both channels. + */ + flevel[0] =3D val / 2; + flevel[1] =3D val - flevel[0]; + for (i =3D 0; i < MT6370_MAX_LEDS; i++) { + ret =3D regmap_update_bits(priv->regmap, MT6370_REG_FLEDISTRB(i), + MT6370_ISTROBE_MASK, flevel[i]); + if (ret) + break; + } + } else + ret =3D regmap_update_bits(priv->regmap, MT6370_REG_FLEDISTRB(led->led_n= o), + MT6370_ISTROBE_MASK, val); + + return ret; +} + +static int mt6370_strobe_set(struct led_classdev_flash *fl_cdev, bool stat= e) +{ + struct mt6370_led *led =3D to_mt6370_led(fl_cdev, flash); + struct mt6370_priv *priv =3D led->priv; + struct led_classdev *lcdev =3D &fl_cdev->led_cdev; + struct led_flash_setting *s =3D &fl_cdev->brightness; + u32 led_enable_mask =3D led->led_no =3D=3D MT6370_LED_JOINT ? MT6370_FLCS= EN_MASK_ALL : + MT6370_FLCSEN_MASK(led->led_no); + u32 enable_mask =3D MT6370_STROBEN_MASK | led_enable_mask; + u32 val =3D state ? led_enable_mask : 0; + u32 curr; + int ret; + + mutex_lock(&priv->lock); + + /* + * There is only one set of flash control logic, and this flag is used to= check if 'torch' + * is currently being used. + */ + if (priv->fled_torch_used) { + dev_warn(lcdev->dev, "Please disable torch first [0x%x]\n", priv->fled_t= orch_used); + ret =3D -EBUSY; + goto unlock; + } + + if (state) + curr =3D priv->fled_strobe_used | BIT(led->led_no); + else + curr =3D priv->fled_strobe_used & ~BIT(led->led_no); + + if (curr) + val |=3D MT6370_STROBEN_MASK; + + ret =3D regmap_update_bits(priv->regmap, MT6370_REG_FLEDEN, enable_mask, = val); + if (ret) { + dev_err(lcdev->dev, "[%d] control current source %d fail\n", led->led_no= , state); + goto unlock; + } + + /* + * If the flash needs to turn on, configure the flash current to ramp up = to the setting + * value. Otherwise, always revert to the minimum one. + */ + ret =3D _mt6370_flash_brightness_set(fl_cdev, state ? s->val : s->min); + if (ret) { + dev_err(lcdev->dev, "[%d] Failed to set brightness\n", led->led_no); + goto unlock; + } + + /* + * For the flash to turn on/off, we must wait for HW ramping up/down time= 5ms/500us to + * prevent the unexpected problem. + */ + if (!priv->fled_strobe_used && curr) + usleep_range(5000, 6000); + else if (priv->fled_strobe_used && !curr) + usleep_range(500, 600); + + priv->fled_strobe_used =3D curr; + +unlock: + mutex_unlock(&priv->lock); + return ret; +} + +static int mt6370_strobe_get(struct led_classdev_flash *fl_cdev, bool *sta= te) +{ + struct mt6370_led *led =3D to_mt6370_led(fl_cdev, flash); + struct mt6370_priv *priv =3D led->priv; + + mutex_lock(&priv->lock); + *state =3D !!(priv->fled_strobe_used & BIT(led->led_no)); + mutex_unlock(&priv->lock); + + return 0; +} + +static int mt6370_timeout_set(struct led_classdev_flash *fl_cdev, u32 time= out) +{ + struct mt6370_led *led =3D to_mt6370_led(fl_cdev, flash); + struct mt6370_priv *priv =3D led->priv; + struct led_flash_setting *s =3D &fl_cdev->timeout; + u32 val =3D (timeout - s->min) / s->step; + + return regmap_update_bits(priv->regmap, MT6370_REG_STRBTO, MT6370_STRBTO_= MASK, val); +} + +static int mt6370_fault_get(struct led_classdev_flash *fl_cdev, u32 *fault) +{ + struct mt6370_led *led =3D to_mt6370_led(fl_cdev, flash); + struct mt6370_priv *priv =3D led->priv; + u16 fled_stat; + unsigned int chg_stat, strobe_timeout_mask, fled_short_mask; + u32 rfault =3D 0; + int ret; + + ret =3D regmap_read(priv->regmap, MT6370_REG_CHGSTAT2, &chg_stat); + if (ret) + return ret; + + ret =3D regmap_raw_read(priv->regmap, MT6370_REG_FLEDSTAT1, &fled_stat, s= izeof(fled_stat)); + if (ret) + return ret; + + switch (led->led_no) { + case MT6370_LED_FLASH1: + strobe_timeout_mask =3D MT6370_FLED1STRBTO_MASK; + fled_short_mask =3D MT6370_FLED1SHORT_MASK; + break; + + case MT6370_LED_FLASH2: + strobe_timeout_mask =3D MT6370_FLED2STRBTO_MASK; + fled_short_mask =3D MT6370_FLED2SHORT_MASK; + break; + + case MT6370_LED_JOINT: + strobe_timeout_mask =3D MT6370_FLED1STRBTO_MASK | MT6370_FLED2STRBTO_MAS= K; + fled_short_mask =3D MT6370_FLED1SHORT_MASK | MT6370_FLED2SHORT_MASK; + break; + default: + return -EINVAL; + } + + if (chg_stat & MT6370_FLEDCHGVINOVP_MASK) + rfault |=3D LED_FAULT_INPUT_VOLTAGE; + + if (fled_stat & strobe_timeout_mask) + rfault |=3D LED_FAULT_TIMEOUT; + + if (fled_stat & fled_short_mask) + rfault |=3D LED_FAULT_SHORT_CIRCUIT; + + if (fled_stat & MT6370_FLEDLVF_MASK) + rfault |=3D LED_FAULT_UNDER_VOLTAGE; + + *fault =3D rfault; + return ret; +} + +static const struct led_flash_ops mt6370_flash_ops =3D { + .flash_brightness_set =3D mt6370_flash_brightness_set, + .strobe_set =3D mt6370_strobe_set, + .strobe_get =3D mt6370_strobe_get, + .timeout_set =3D mt6370_timeout_set, + .fault_get =3D mt6370_fault_get, +}; + +#if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS) +static int mt6370_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, + bool enable) +{ + struct led_classdev_flash *flash =3D v4l2_flash->fled_cdev; + struct mt6370_led *led =3D to_mt6370_led(flash, flash); + struct mt6370_priv *priv =3D led->priv; + u32 mask =3D led->led_no =3D=3D MT6370_LED_JOINT ? MT6370_FLCSEN_MASK_ALL= : + MT6370_FLCSEN_MASK(led->led_no); + u32 val =3D enable ? mask : 0; + int ret; + + mutex_lock(&priv->lock); + + ret =3D regmap_update_bits(priv->regmap, MT6370_REG_FLEDEN, mask, val); + if (ret) + goto unlock; + + if (enable) + priv->fled_strobe_used |=3D BIT(led->led_no); + else + priv->fled_strobe_used &=3D ~BIT(led->led_no); + +unlock: + mutex_unlock(&priv->lock); + return ret; +} + +static const struct v4l2_flash_ops v4l2_flash_ops =3D { + .external_strobe_set =3D mt6370_flash_external_strobe_set, +}; + +static void mt6370_init_v4l2_flash_config(struct mt6370_led *led, struct v= 4l2_flash_config *cfg) +{ + struct led_classdev *lcdev; + struct led_flash_setting *s =3D &cfg->intensity; + + lcdev =3D &led->flash.led_cdev; + + s->min =3D MT6370_ITORCH_MIN_uA; + s->step =3D MT6370_ITORCH_STEP_uA; + s->val =3D s->max =3D s->min + (lcdev->max_brightness - 1) * s->step; + + cfg->has_external_strobe =3D 1; + strscpy(cfg->dev_name, dev_name(lcdev->dev), sizeof(cfg->dev_name)); + + cfg->flash_faults =3D LED_FAULT_SHORT_CIRCUIT | LED_FAULT_TIMEOUT | + LED_FAULT_INPUT_VOLTAGE | LED_FAULT_UNDER_VOLTAGE; +} +#else +static const struct v4l2_flash_ops v4l2_flash_ops; +static void mt6370_init_v4l2_flash_config(struct mt6370_led *led, struct v= 4l2_flash_config *cfg) +{ +} +#endif + +static void mt6370_v4l2_flash_release(void *v4l2_flash) +{ + v4l2_flash_release(v4l2_flash); +} + +static int mt6370_led_register(struct device *parent, struct mt6370_led *l= ed, + struct fwnode_handle *fwnode) +{ + struct led_init_data init_data =3D { .fwnode =3D fwnode }; + struct v4l2_flash_config v4l2_config =3D {}; + int ret; + + ret =3D devm_led_classdev_flash_register_ext(parent, &led->flash, &init_d= ata); + if (ret) + return dev_err_probe(parent, ret, "Couldn't register flash %d\n", led->l= ed_no); + + mt6370_init_v4l2_flash_config(led, &v4l2_config); + led->v4l2_flash =3D v4l2_flash_init(parent, fwnode, &led->flash, &v4l2_fl= ash_ops, + &v4l2_config); + if (IS_ERR(led->v4l2_flash)) + return dev_err_probe(parent, PTR_ERR(led->v4l2_flash), + "Failed to register %d v4l2 sd\n", led->led_no); + + return devm_add_action_or_reset(parent, mt6370_v4l2_flash_release, led->v= 4l2_flash); +} + +static u32 mt6370_clamp(u32 val, u32 min, u32 max, u32 step) +{ + u32 retval; + + retval =3D clamp_val(val, min, max); + if (step > 1) + retval =3D rounddown(retval - min, step) + min; + + return retval; +} + +static int mt6370_init_flash_properties(struct device *dev, struct mt6370_= led *led, + struct fwnode_handle *fwnode) +{ + struct led_classdev_flash *flash =3D &led->flash; + struct led_classdev *lcdev =3D &flash->led_cdev; + struct mt6370_priv *priv =3D led->priv; + struct led_flash_setting *s; + u32 sources[MT6370_MAX_LEDS]; + u32 max_ua, val; + int i, ret, num; + + num =3D fwnode_property_count_u32(fwnode, "led-sources"); + if (num < 1) + return dev_err_probe(dev, -EINVAL, + "Not specified or wrong number of led-sources\n"); + + ret =3D fwnode_property_read_u32_array(fwnode, "led-sources", sources, nu= m); + if (ret) + return ret; + + for (i =3D 0; i < num; i++) { + if (sources[i] >=3D MT6370_MAX_LEDS) + return -EINVAL; + if (priv->leds_active & BIT(sources[i])) + return -EINVAL; + priv->leds_active |=3D BIT(sources[i]); + } + + /* If both channels are specified in 'led-sources', joint flash output mo= de is used */ + led->led_no =3D num =3D=3D 2 ? MT6370_LED_JOINT : sources[0]; + + max_ua =3D num =3D=3D 2 ? MT6370_ITORCH_DOUBLE_MAX_uA : MT6370_ITORCH_MAX= _uA; + val =3D MT6370_ITORCH_MIN_uA; + ret =3D fwnode_property_read_u32(fwnode, "led-max-microamp", &val); + if (!ret) + val =3D mt6370_clamp(val, MT6370_ITORCH_MIN_uA, max_ua, MT6370_ITORCH_ST= EP_uA); + + lcdev->max_brightness =3D (val - MT6370_ITORCH_MIN_uA) / MT6370_ITORCH_ST= EP_uA + 1; + lcdev->brightness_set_blocking =3D mt6370_torch_brightness_set; + lcdev->flags |=3D LED_DEV_CAP_FLASH; + + max_ua =3D num =3D=3D 2 ? MT6370_ISTRB_DOUBLE_MAX_uA : MT6370_ISTRB_MAX_u= A; + val =3D MT6370_ISTRB_MIN_uA; + ret =3D fwnode_property_read_u32(fwnode, "flash-max-microamp", &val); + if (!ret) + val =3D mt6370_clamp(val, MT6370_ISTRB_MIN_uA, max_ua, MT6370_ISTRB_STEP= _uA); + + s =3D &flash->brightness; + s->min =3D MT6370_ISTRB_MIN_uA; + s->step =3D MT6370_ISTRB_STEP_uA; + s->val =3D s->max =3D val; + + /* Always configure to the minimum level when off to prevent flash curren= t spikes. */ + ret =3D _mt6370_flash_brightness_set(flash, s->min); + if (ret) + return ret; + + + val =3D MT6370_STRBTO_MIN_US; + ret =3D fwnode_property_read_u32(fwnode, "flash-max-timeout-us", &val); + if (!ret) + val =3D mt6370_clamp(val, MT6370_STRBTO_MIN_US, MT6370_STRBTO_MAX_US, + MT6370_STRBTO_STEP_US); + + s =3D &flash->timeout; + s->min =3D MT6370_STRBTO_MIN_US; + s->step =3D MT6370_STRBTO_STEP_US; + s->val =3D s->max =3D val; + + flash->ops =3D &mt6370_flash_ops; + + return 0; +} + +static int mt6370_led_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct mt6370_priv *priv; + struct fwnode_handle *child; + size_t count; + int i =3D 0, ret; + + count =3D device_get_child_node_count(dev); + if (!count || count > MT6370_MAX_LEDS) + return dev_err_probe(dev, -EINVAL, + "No child node or node count over max led number %zu\n", count); + + priv =3D devm_kzalloc(dev, struct_size(priv, leds, count), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->leds_count =3D count; + mutex_init(&priv->lock); + + priv->regmap =3D dev_get_regmap(dev->parent, NULL); + if (!priv->regmap) + return dev_err_probe(dev, -ENODEV, "Failed to get parent regmap\n"); + + device_for_each_child_node(dev, child) { + struct mt6370_led *led =3D priv->leds + i; + + led->priv =3D priv; + + ret =3D mt6370_init_flash_properties(dev, led, child); + if (ret) { + fwnode_handle_put(child); + return ret; + } + + ret =3D mt6370_led_register(dev, led, child); + if (ret) { + fwnode_handle_put(child); + return ret; + } + + i++; + } + + return 0; +} + +static const struct of_device_id mt6370_led_of_id[] =3D { + { .compatible =3D "mediatek,mt6370-flashlight" }, + {} +}; +MODULE_DEVICE_TABLE(of, mt6370_led_of_id); + +static struct platform_driver mt6370_led_driver =3D { + .driver =3D { + .name =3D "mt6370-flashlight", + .of_match_table =3D mt6370_led_of_id, + }, + .probe =3D mt6370_led_probe, +}; +module_platform_driver(mt6370_led_driver); + +MODULE_AUTHOR("Alice Chen "); +MODULE_AUTHOR("ChiYuan Huang "); +MODULE_DESCRIPTION("MT6370 FLASH LED Driver"); +MODULE_LICENSE("GPL"); --=20 2.7.4