From nobody Sun Nov 10 21:51:04 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 903FFC38142 for ; Wed, 1 Feb 2023 03:19:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230152AbjBADTI (ORCPT ); Tue, 31 Jan 2023 22:19:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229879AbjBADTF (ORCPT ); Tue, 31 Jan 2023 22:19:05 -0500 Received: from mg.richtek.com (mg.richtek.com [220.130.44.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3AEF648A12; Tue, 31 Jan 2023 19:18:51 -0800 (PST) X-MailGates: (flag:4,DYNAMIC,BADHELO,RELAY,NOHOST:PASS)(compute_score:DE LIVER,40,3) Received: from 192.168.10.47 by mg.richtek.com with MailGates ESMTP Server V5.0(17471:0:AUTH_RELAY) (envelope-from ); Wed, 01 Feb 2023 11:18:27 +0800 (CST) Received: from ex4.rt.l (192.168.10.47) by ex4.rt.l (192.168.10.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.20; Wed, 1 Feb 2023 11:18:26 +0800 Received: from linuxcarl2.richtek.com (192.168.10.154) by ex4.rt.l (192.168.10.45) with Microsoft SMTP Server id 15.2.1118.20 via Frontend Transport; Wed, 1 Feb 2023 11:18:26 +0800 From: ChiaEn Wu To: , , , , , CC: , , , , , , , , Alice Chen , ChiaEn Wu Subject: [PATCH v17 1/3] leds: rgb: mt6370: Add MediaTek MT6370 current sink type LED Indicator support Date: Wed, 1 Feb 2023 11:17:27 +0800 Message-ID: <722bf19116258aee9fb184ea674a63eb28ecfd3c.1675246896.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 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. Co-developed-by: Alice Chen Acked-by: Jacek Anaszewski Signed-off-by: Alice Chen Signed-off-by: ChiYuan Huang Signed-off-by: ChiaEn Wu --- v17 - Update the year of Copyright from 2022 to 2023 --- drivers/leds/rgb/Kconfig | 13 + drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-mt6370-rgb.c | 1009 ++++++++++++++++++++++++++++++++= ++++ 3 files changed, 1023 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 =20 If compiled as a module, the module will be named leds-qcom-lpg. =20 +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 @@ =20 obj-$(CONFIG_LEDS_PWM_MULTICOLOR) +=3D leds-pwm-multicolor.o obj-$(CONFIG_LEDS_QCOM_LPG) +=3D leds-qcom-lpg.o +obj-$(CONFIG_LEDS_MT6370_RGB) +=3D leds-mt6370-rgb.o diff --git a/drivers/leds/rgb/leds-mt6370-rgb.c b/drivers/leds/rgb/leds-mt6= 370-rgb.c new file mode 100644 index 00000000..2a5d582 --- /dev/null +++ b/drivers/leds/rgb/leds-mt6370-rgb.c @@ -0,0 +1,1009 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 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 =3D 0, + MT6370_LED_ISNK2, + MT6370_LED_ISNK3, + MT6370_LED_ISNK4, + MT6370_MAX_LEDS +}; + +enum mt6370_led_mode { + MT6370_LED_PWM_MODE =3D 0, + MT6370_LED_BREATH_MODE, + MT6370_LED_REG_MODE, + MT6370_LED_MAX_MODE +}; + +enum mt6370_led_field { + F_RGB_EN =3D 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 =3D 0, + R_LED4_CURR, + R_LED_TRFON, + R_LED_TOFF, + R_MAX_RANGES +}; + +enum mt6370_pattern { + P_LED_TR1 =3D 0, + P_LED_TR2, + P_LED_TF1, + P_LED_TF2, + P_LED_TON, + P_LED_TOFF, + P_MAX_PATTERNS +}; + +#define MT6370_REG_DEV_INFO 0x100 +#define MT6370_REG_RGB1_DIM 0x182 +#define MT6370_REG_RGB2_DIM 0x183 +#define MT6370_REG_RGB3_DIM 0x184 +#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_DIM 0x187 +#define MT6372_REG_RGB2_DIM 0x188 +#define MT6372_REG_RGB3_DIM 0x189 +#define MT6372_REG_RGB4_DIM 0x18A +#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) BIT(MT6370_LED_ISNK4 - id) +#define MT6370_VIRTUAL_MULTICOLOR 5 +#define MC_CHANNEL_NUM 3 +#define MT6370_PWM_DUTY (BIT(5) - 1) +#define MT6372_PWM_DUTY (BIT(8) - 1) + +struct mt6370_led { + /* + * If the color of the LED in DT is set to + * - 'LED_COLOR_ID_RGB' + * - 'LED_COLOR_ID_MULTI' + * The member 'index' of this struct will be set to + * 'MT6370_VIRTUAL_MULTICOLOR'. + * If so, this LED will choose 'struct led_classdev_mc mc' to use. + * Instead, if the member 'index' of this struct is set to + * 'MT6370_LED_ISNK1' ~ 'MT6370_LED_ISNK4', then this LED will choose + * 'struct led_classdev isink' to use. + */ + union { + struct led_classdev isink; + struct led_classdev_mc mc; + }; + struct mt6370_priv *priv; + enum led_default_state default_state; + u32 index; +}; + +struct mt6370_pdata { + const unsigned int *tfreq; + unsigned int tfreq_len; + u16 reg_rgb1_tr; + s16 reg_rgb_chrind_tr; + u8 pwm_duty; +}; + +struct mt6370_priv { + /* Per LED access lock */ + struct mutex lock; + struct regmap *regmap; + struct regmap_field *fields[F_MAX_FIELDS]; + const struct reg_field *reg_fields; + const struct linear_range *ranges; + struct reg_cfg *reg_cfgs; + const struct mt6370_pdata *pdata; + unsigned int leds_count; + unsigned int leds_active; + struct mt6370_led leds[]; +}; + +static const struct reg_field common_reg_fields[F_MAX_FIELDS] =3D { + [F_RGB_EN] =3D REG_FIELD(MT6370_REG_RGB_EN, 4, 7), + [F_CHGIND_EN] =3D REG_FIELD(MT6370_REG_RGB_CHRIND_DIM, 7, 7), + [F_LED1_CURR] =3D REG_FIELD(MT6370_REG_RGB1_ISNK, 0, 2), + [F_LED2_CURR] =3D REG_FIELD(MT6370_REG_RGB2_ISNK, 0, 2), + [F_LED3_CURR] =3D REG_FIELD(MT6370_REG_RGB3_ISNK, 0, 2), + [F_LED4_CURR] =3D REG_FIELD(MT6370_REG_RGB_CHRIND_CTRL, 0, 1), + [F_LED1_MODE] =3D REG_FIELD(MT6370_REG_RGB1_DIM, 5, 6), + [F_LED2_MODE] =3D REG_FIELD(MT6370_REG_RGB2_DIM, 5, 6), + [F_LED3_MODE] =3D REG_FIELD(MT6370_REG_RGB3_DIM, 5, 6), + [F_LED4_MODE] =3D REG_FIELD(MT6370_REG_RGB_CHRIND_DIM, 5, 6), + [F_LED1_DUTY] =3D REG_FIELD(MT6370_REG_RGB1_DIM, 0, 4), + [F_LED2_DUTY] =3D REG_FIELD(MT6370_REG_RGB2_DIM, 0, 4), + [F_LED3_DUTY] =3D REG_FIELD(MT6370_REG_RGB3_DIM, 0, 4), + [F_LED4_DUTY] =3D REG_FIELD(MT6370_REG_RGB_CHRIND_DIM, 0, 4), + [F_LED1_FREQ] =3D REG_FIELD(MT6370_REG_RGB1_ISNK, 3, 5), + [F_LED2_FREQ] =3D REG_FIELD(MT6370_REG_RGB2_ISNK, 3, 5), + [F_LED3_FREQ] =3D REG_FIELD(MT6370_REG_RGB3_ISNK, 3, 5), + [F_LED4_FREQ] =3D REG_FIELD(MT6370_REG_RGB_CHRIND_CTRL, 2, 4), +}; + +static const struct reg_field mt6372_reg_fields[F_MAX_FIELDS] =3D { + [F_RGB_EN] =3D REG_FIELD(MT6372_REG_RGB_EN, 4, 7), + [F_CHGIND_EN] =3D REG_FIELD(MT6372_REG_RGB_EN, 3, 3), + [F_LED1_CURR] =3D REG_FIELD(MT6372_REG_RGB1_ISNK, 0, 3), + [F_LED2_CURR] =3D REG_FIELD(MT6372_REG_RGB2_ISNK, 0, 3), + [F_LED3_CURR] =3D REG_FIELD(MT6372_REG_RGB3_ISNK, 0, 3), + [F_LED4_CURR] =3D REG_FIELD(MT6372_REG_RGB4_ISNK, 0, 3), + [F_LED1_MODE] =3D REG_FIELD(MT6372_REG_RGB1_ISNK, 6, 7), + [F_LED2_MODE] =3D REG_FIELD(MT6372_REG_RGB2_ISNK, 6, 7), + [F_LED3_MODE] =3D REG_FIELD(MT6372_REG_RGB3_ISNK, 6, 7), + [F_LED4_MODE] =3D REG_FIELD(MT6372_REG_RGB4_ISNK, 6, 7), + [F_LED1_DUTY] =3D REG_FIELD(MT6372_REG_RGB1_DIM, 0, 7), + [F_LED2_DUTY] =3D REG_FIELD(MT6372_REG_RGB2_DIM, 0, 7), + [F_LED3_DUTY] =3D REG_FIELD(MT6372_REG_RGB3_DIM, 0, 7), + [F_LED4_DUTY] =3D REG_FIELD(MT6372_REG_RGB4_DIM, 0, 7), + [F_LED1_FREQ] =3D REG_FIELD(MT6372_REG_RGB12_FREQ, 5, 7), + [F_LED2_FREQ] =3D REG_FIELD(MT6372_REG_RGB12_FREQ, 2, 4), + [F_LED3_FREQ] =3D REG_FIELD(MT6372_REG_RGB34_FREQ, 5, 7), + [F_LED4_FREQ] =3D REG_FIELD(MT6372_REG_RGB34_FREQ, 2, 4), +}; + +/* Current unit: microamp, time unit: millisecond */ +static const struct linear_range common_led_ranges[R_MAX_RANGES] =3D { + [R_LED123_CURR] =3D { 4000, 1, 6, 4000 }, + [R_LED4_CURR] =3D { 2000, 1, 3, 2000 }, + [R_LED_TRFON] =3D { 125, 0, 15, 200 }, + [R_LED_TOFF] =3D { 250, 0, 15, 400 }, +}; + +static const struct linear_range mt6372_led_ranges[R_MAX_RANGES] =3D { + [R_LED123_CURR] =3D { 2000, 1, 14, 2000 }, + [R_LED4_CURR] =3D { 2000, 1, 14, 2000 }, + [R_LED_TRFON] =3D { 125, 0, 15, 250 }, + [R_LED_TOFF] =3D { 250, 0, 15, 500 }, +}; + +static const unsigned int common_tfreqs[] =3D { + 10000, 5000, 2000, 1000, 500, 200, 5, 1, +}; + +static const unsigned int mt6372_tfreqs[] =3D { + 8000, 4000, 2000, 1000, 500, 250, 8, 4, +}; + +static const struct mt6370_pdata common_pdata =3D { + .tfreq =3D common_tfreqs, + .tfreq_len =3D ARRAY_SIZE(common_tfreqs), + .pwm_duty =3D MT6370_PWM_DUTY, + .reg_rgb1_tr =3D MT6370_REG_RGB1_TR, + .reg_rgb_chrind_tr =3D MT6370_REG_RGB_CHRIND_TR, +}; + +static const struct mt6370_pdata mt6372_pdata =3D { + .tfreq =3D mt6372_tfreqs, + .tfreq_len =3D ARRAY_SIZE(mt6372_tfreqs), + .pwm_duty =3D MT6372_PWM_DUTY, + .reg_rgb1_tr =3D MT6372_REG_RGB1_TR, + .reg_rgb_chrind_tr =3D -1, +}; + +static enum mt6370_led_field mt6370_get_led_current_field(unsigned int led= _no) +{ + switch (led_no) { + case MT6370_LED_ISNK1: + return F_LED1_CURR; + case MT6370_LED_ISNK2: + return F_LED2_CURR; + case MT6370_LED_ISNK3: + return F_LED3_CURR; + default: + return F_LED4_CURR; + } +} + +static int mt6370_set_led_brightness(struct mt6370_priv *priv, + unsigned int led_no, unsigned int level) +{ + enum mt6370_led_field sel_field; + + sel_field =3D mt6370_get_led_current_field(led_no); + + return regmap_field_write(priv->fields[sel_field], level); +} + +static int mt6370_get_led_brightness(struct mt6370_priv *priv, + unsigned int led_no, unsigned int *level) +{ + enum mt6370_led_field sel_field; + + sel_field =3D mt6370_get_led_current_field(led_no); + + return regmap_field_read(priv->fields[sel_field], level); +} + +static int mt6370_set_led_duty(struct mt6370_priv *priv, unsigned int led_= no, + unsigned int ton, unsigned int toff) +{ + const struct mt6370_pdata *pdata =3D priv->pdata; + enum mt6370_led_field sel_field; + unsigned int divisor, ratio; + + divisor =3D pdata->pwm_duty; + ratio =3D ton * divisor / (ton + toff); + + switch (led_no) { + case MT6370_LED_ISNK1: + sel_field =3D F_LED1_DUTY; + break; + case MT6370_LED_ISNK2: + sel_field =3D F_LED2_DUTY; + break; + case MT6370_LED_ISNK3: + sel_field =3D F_LED3_DUTY; + break; + default: + sel_field =3D F_LED4_DUTY; + break; + } + + return regmap_field_write(priv->fields[sel_field], ratio); +} + +static int mt6370_set_led_freq(struct mt6370_priv *priv, unsigned int led_= no, + unsigned int ton, unsigned int toff) +{ + const struct mt6370_pdata *pdata =3D priv->pdata; + enum mt6370_led_field sel_field; + unsigned int tfreq_len =3D pdata->tfreq_len; + unsigned int tsum, sel; + + tsum =3D ton + toff; + + if (tsum > pdata->tfreq[0] || tsum < pdata->tfreq[tfreq_len - 1]) + return -EOPNOTSUPP; + + sel =3D find_closest_descending(tsum, pdata->tfreq, tfreq_len); + + switch (led_no) { + case MT6370_LED_ISNK1: + sel_field =3D F_LED1_FREQ; + break; + case MT6370_LED_ISNK2: + sel_field =3D F_LED2_FREQ; + break; + case MT6370_LED_ISNK3: + sel_field =3D F_LED3_FREQ; + break; + default: + sel_field =3D F_LED4_FREQ; + break; + } + + return regmap_field_write(priv->fields[sel_field], sel); +} + +static void mt6370_get_breath_reg_base(struct mt6370_priv *priv, + unsigned int led_no, unsigned int *base) +{ + const struct mt6370_pdata *pdata =3D priv->pdata; + + if (pdata->reg_rgb_chrind_tr < 0) { + *base =3D pdata->reg_rgb1_tr + led_no * 3; + return; + } + + switch (led_no) { + case MT6370_LED_ISNK1: + case MT6370_LED_ISNK2: + case MT6370_LED_ISNK3: + *base =3D pdata->reg_rgb1_tr + led_no * 3; + break; + default: + *base =3D pdata->reg_rgb_chrind_tr; + break; + } +} + +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; + u32 val =3D 0; + 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 =3D 0; i < P_MAX_PATTERNS; i++) { + curr =3D pattern + i; + + sel_range =3D i =3D=3D P_LED_TOFF ? R_LED_TOFF : R_LED_TRFON; + + linear_range_get_selector_within(priv->ranges + sel_range, + curr->delta_t, &sel); + + if (i % 2) { + val |=3D sel; + } else { + val <<=3D 8; + val |=3D sel << 4; + } + } + + put_unaligned_be24(val, pattern_val); + + return 0; +} + +static int mt6370_set_led_mode(struct mt6370_priv *priv, unsigned int led_= no, + enum mt6370_led_mode mode) +{ + enum mt6370_led_field sel_field; + + switch (led_no) { + case MT6370_LED_ISNK1: + sel_field =3D F_LED1_MODE; + break; + case MT6370_LED_ISNK2: + sel_field =3D F_LED2_MODE; + break; + case MT6370_LED_ISNK3: + sel_field =3D F_LED3_MODE; + break; + default: + sel_field =3D F_LED4_MODE; + break; + } + + return regmap_field_write(priv->fields[sel_field], mode); +} + +static int mt6370_mc_brightness_set(struct led_classdev *lcdev, + enum led_brightness level) +{ + struct led_classdev_mc *mccdev =3D lcdev_to_mccdev(lcdev); + struct mt6370_led *led =3D container_of(mccdev, struct mt6370_led, mc); + struct mt6370_priv *priv =3D led->priv; + struct mc_subled *subled; + unsigned int enable, disable; + int i, ret; + + mutex_lock(&priv->lock); + + led_mc_calc_color_components(mccdev, level); + + ret =3D regmap_field_read(priv->fields[F_RGB_EN], &enable); + if (ret) + goto out_unlock; + + disable =3D enable; + + for (i =3D 0; i < mccdev->num_colors; i++) { + u32 brightness; + + subled =3D mccdev->subled_info + i; + brightness =3D min(subled->brightness, lcdev->max_brightness); + disable &=3D ~MT6370_CHEN_BIT(subled->channel); + + if (level =3D=3D 0) { + enable &=3D ~MT6370_CHEN_BIT(subled->channel); + + ret =3D mt6370_set_led_mode(priv, subled->channel, + MT6370_LED_REG_MODE); + if (ret) + goto out_unlock; + + continue; + } + + if (brightness =3D=3D 0) { + enable &=3D ~MT6370_CHEN_BIT(subled->channel); + continue; + } + + enable |=3D MT6370_CHEN_BIT(subled->channel); + + ret =3D mt6370_set_led_brightness(priv, subled->channel, + brightness); + if (ret) + goto out_unlock; + } + + ret =3D regmap_field_write(priv->fields[F_RGB_EN], disable); + if (ret) + goto out_unlock; + + ret =3D regmap_field_write(priv->fields[F_RGB_EN], enable); + +out_unlock: + mutex_unlock(&priv->lock); + + return ret; +} + +static int mt6370_mc_blink_set(struct led_classdev *lcdev, + unsigned long *delay_on, + unsigned long *delay_off) +{ + struct led_classdev_mc *mccdev =3D lcdev_to_mccdev(lcdev); + struct mt6370_led *led =3D container_of(mccdev, struct mt6370_led, mc); + struct mt6370_priv *priv =3D led->priv; + struct mc_subled *subled; + unsigned int enable, disable; + int i, ret; + + mutex_lock(&priv->lock); + + if (!*delay_on && !*delay_off) + *delay_on =3D *delay_off =3D 500; + + ret =3D regmap_field_read(priv->fields[F_RGB_EN], &enable); + if (ret) + goto out_unlock; + + disable =3D enable; + + for (i =3D 0; i < mccdev->num_colors; i++) { + subled =3D mccdev->subled_info + i; + + disable &=3D ~MT6370_CHEN_BIT(subled->channel); + + ret =3D mt6370_set_led_duty(priv, subled->channel, *delay_on, + *delay_off); + if (ret) + goto out_unlock; + + ret =3D mt6370_set_led_freq(priv, subled->channel, *delay_on, + *delay_off); + if (ret) + goto out_unlock; + + ret =3D mt6370_set_led_mode(priv, subled->channel, + MT6370_LED_PWM_MODE); + if (ret) + goto out_unlock; + } + + /* Toggle to make pattern timing the same */ + ret =3D regmap_field_write(priv->fields[F_RGB_EN], disable); + if (ret) + goto out_unlock; + + ret =3D regmap_field_write(priv->fields[F_RGB_EN], enable); + +out_unlock: + mutex_unlock(&priv->lock); + + return ret; +} + +static int mt6370_mc_pattern_set(struct led_classdev *lcdev, + struct led_pattern *pattern, u32 len, int repeat) +{ + struct led_classdev_mc *mccdev =3D lcdev_to_mccdev(lcdev); + struct mt6370_led *led =3D container_of(mccdev, struct mt6370_led, mc); + struct mt6370_priv *priv =3D led->priv; + struct mc_subled *subled; + unsigned int reg_base, enable, disable; + u8 params[P_MAX_PATTERNS / 2]; + int i, ret; + + mutex_lock(&priv->lock); + + ret =3D mt6370_gen_breath_pattern(priv, pattern, len, params, + sizeof(params)); + if (ret) + goto out_unlock; + + ret =3D regmap_field_read(priv->fields[F_RGB_EN], &enable); + if (ret) + goto out_unlock; + + disable =3D enable; + + for (i =3D 0; i < mccdev->num_colors; i++) { + subled =3D mccdev->subled_info + i; + + mt6370_get_breath_reg_base(priv, subled->channel, ®_base); + disable &=3D ~MT6370_CHEN_BIT(subled->channel); + + ret =3D regmap_raw_write(priv->regmap, reg_base, params, + sizeof(params)); + if (ret) + goto out_unlock; + + ret =3D mt6370_set_led_mode(priv, subled->channel, + MT6370_LED_BREATH_MODE); + if (ret) + goto out_unlock; + } + + /* Toggle to make pattern timing be the same */ + ret =3D regmap_field_write(priv->fields[F_RGB_EN], disable); + if (ret) + goto out_unlock; + + ret =3D regmap_field_write(priv->fields[F_RGB_EN], enable); + +out_unlock: + mutex_unlock(&priv->lock); + + return ret; +} + +static inline int mt6370_mc_pattern_clear(struct led_classdev *lcdev) +{ + struct led_classdev_mc *mccdev =3D lcdev_to_mccdev(lcdev); + struct mt6370_led *led =3D container_of(mccdev, struct mt6370_led, mc); + struct mt6370_priv *priv =3D led->priv; + struct mc_subled *subled; + int i, ret; + + mutex_lock(&led->priv->lock); + + for (i =3D 0; i < mccdev->num_colors; i++) { + subled =3D mccdev->subled_info + i; + + ret =3D mt6370_set_led_mode(priv, subled->channel, + MT6370_LED_REG_MODE); + if (ret) + break; + } + + mutex_unlock(&led->priv->lock); + + return ret; +} + +static int mt6370_isnk_brightness_set(struct led_classdev *lcdev, + enum led_brightness level) +{ + struct mt6370_led *led =3D container_of(lcdev, struct mt6370_led, isink); + struct mt6370_priv *priv =3D led->priv; + unsigned int enable; + int ret; + + mutex_lock(&priv->lock); + + ret =3D regmap_field_read(priv->fields[F_RGB_EN], &enable); + if (ret) + goto out_unlock; + + if (level =3D=3D 0) { + enable &=3D ~MT6370_CHEN_BIT(led->index); + + ret =3D mt6370_set_led_mode(priv, led->index, + MT6370_LED_REG_MODE); + if (ret) + goto out_unlock; + } else { + enable |=3D MT6370_CHEN_BIT(led->index); + + ret =3D mt6370_set_led_brightness(priv, led->index, level); + if (ret) + goto out_unlock; + } + + ret =3D regmap_field_write(priv->fields[F_RGB_EN], enable); + +out_unlock: + mutex_unlock(&priv->lock); + + return ret; +} + +static int mt6370_isnk_blink_set(struct led_classdev *lcdev, + unsigned long *delay_on, + unsigned long *delay_off) +{ + struct mt6370_led *led =3D container_of(lcdev, struct mt6370_led, isink); + struct mt6370_priv *priv =3D led->priv; + int ret; + + mutex_lock(&priv->lock); + + if (!*delay_on && !*delay_off) + *delay_on =3D *delay_off =3D 500; + + ret =3D mt6370_set_led_duty(priv, led->index, *delay_on, *delay_off); + if (ret) + goto out_unlock; + + ret =3D mt6370_set_led_freq(priv, led->index, *delay_on, *delay_off); + if (ret) + goto out_unlock; + + ret =3D mt6370_set_led_mode(priv, led->index, MT6370_LED_PWM_MODE); + +out_unlock: + mutex_unlock(&priv->lock); + + return ret; +} + +static int mt6370_isnk_pattern_set(struct led_classdev *lcdev, + struct led_pattern *pattern, u32 len, + int repeat) +{ + struct mt6370_led *led =3D container_of(lcdev, struct mt6370_led, isink); + struct mt6370_priv *priv =3D led->priv; + unsigned int reg_base; + u8 params[P_MAX_PATTERNS / 2]; + int ret; + + mutex_lock(&priv->lock); + + ret =3D mt6370_gen_breath_pattern(priv, pattern, len, params, + sizeof(params)); + if (ret) + goto out_unlock; + + mt6370_get_breath_reg_base(priv, led->index, ®_base); + + ret =3D regmap_raw_write(priv->regmap, reg_base, params, sizeof(params)); + if (ret) + goto out_unlock; + + ret =3D mt6370_set_led_mode(priv, led->index, MT6370_LED_BREATH_MODE); + +out_unlock: + mutex_unlock(&priv->lock); + + return ret; +} + +static inline int mt6370_isnk_pattern_clear(struct led_classdev *lcdev) +{ + struct mt6370_led *led =3D container_of(lcdev, struct mt6370_led, isink); + struct mt6370_priv *priv =3D led->priv; + int ret; + + mutex_lock(&led->priv->lock); + ret =3D mt6370_set_led_mode(priv, led->index, MT6370_LED_REG_MODE); + mutex_unlock(&led->priv->lock); + + return ret; +} + +static int mt6370_init_led_properties(struct device *dev, + struct mt6370_led *led, + struct led_init_data *init_data) +{ + struct mt6370_priv *priv =3D led->priv; + struct led_classdev *lcdev; + struct fwnode_handle *child; + enum mt6370_led_ranges sel_range; + u32 max_uA, max_level; + int ret; + + if (led->index =3D=3D MT6370_VIRTUAL_MULTICOLOR) { + struct mc_subled *sub_led; + u32 num_color =3D 0; + + sub_led =3D devm_kcalloc(dev, MC_CHANNEL_NUM, sizeof(*sub_led), + GFP_KERNEL); + if (!sub_led) + return -ENOMEM; + + fwnode_for_each_child_node(init_data->fwnode, child) { + u32 reg, color; + + ret =3D fwnode_property_read_u32(child, "reg", ®); + if (ret || reg > MT6370_LED_ISNK3 || + priv->leds_active & BIT(reg)) { + fwnode_handle_put(child); + return -EINVAL; + } + + ret =3D fwnode_property_read_u32(child, "color", &color); + if (ret) { + fwnode_handle_put(child); + return dev_err_probe(dev, ret, "LED %d, no color specified\n", + led->index); + } + + priv->leds_active |=3D BIT(reg); + sub_led[num_color].color_index =3D color; + sub_led[num_color].channel =3D reg; + sub_led[num_color].intensity =3D 0; + num_color++; + } + + if (num_color < 2) + return dev_err_probe(dev, -EINVAL, + "Multicolor must include 2 or more LED channels\n"); + + led->mc.num_colors =3D num_color; + led->mc.subled_info =3D sub_led; + + lcdev =3D &led->mc.led_cdev; + lcdev->brightness_set_blocking =3D mt6370_mc_brightness_set; + lcdev->blink_set =3D mt6370_mc_blink_set; + lcdev->pattern_set =3D mt6370_mc_pattern_set; + lcdev->pattern_clear =3D mt6370_mc_pattern_clear; + } else { + lcdev =3D &led->isink; + lcdev->brightness_set_blocking =3D mt6370_isnk_brightness_set; + lcdev->blink_set =3D mt6370_isnk_blink_set; + lcdev->pattern_set =3D mt6370_isnk_pattern_set; + lcdev->pattern_clear =3D mt6370_isnk_pattern_clear; + } + + ret =3D fwnode_property_read_u32(init_data->fwnode, "led-max-microamp", + &max_uA); + if (ret) { + dev_warn(dev, "Not specified led-max-microamp, config to the minimum\n"); + max_uA =3D 0; + } + + if (led->index =3D=3D MT6370_LED_ISNK4) + sel_range =3D R_LED4_CURR; + else + sel_range =3D R_LED123_CURR; + + linear_range_get_selector_within(priv->ranges + sel_range, max_uA, + &max_level); + + lcdev->max_brightness =3D max_level; + + led->default_state =3D led_init_default_state_get(init_data->fwnode); + + return 0; +} + +static int mt6370_isnk_init_default_state(struct mt6370_led *led) +{ + struct mt6370_priv *priv =3D led->priv; + unsigned int enable, level; + int ret; + + ret =3D mt6370_get_led_brightness(priv, led->index, &level); + if (ret) + return ret; + + ret =3D regmap_field_read(priv->fields[F_RGB_EN], &enable); + if (ret) + return ret; + + if (!(enable & MT6370_CHEN_BIT(led->index))) + level =3D 0; + + switch (led->default_state) { + case LEDS_DEFSTATE_ON: + led->isink.brightness =3D led->isink.max_brightness; + break; + case LEDS_DEFSTATE_KEEP: + led->isink.brightness =3D min(level, led->isink.max_brightness); + break; + default: + led->isink.brightness =3D 0; + break; + } + + return mt6370_isnk_brightness_set(&led->isink, led->isink.brightness); +} + +static int mt6370_led_register(struct device *dev, struct mt6370_led *led, + struct led_init_data *init_data) +{ + struct mt6370_priv *priv =3D led->priv; + int ret; + + if (led->index =3D=3D MT6370_VIRTUAL_MULTICOLOR) { + ret =3D mt6370_mc_brightness_set(&led->mc.led_cdev, 0); + if (ret) + return dev_err_probe(dev, ret, "Couldn't set multicolor brightness\n"); + + ret =3D devm_led_classdev_multicolor_register_ext(dev, &led->mc, + init_data); + if (ret) + return dev_err_probe(dev, ret, "Couldn't register multicolor\n"); + } else { + if (led->index =3D=3D MT6370_LED_ISNK4) { + ret =3D regmap_field_write(priv->fields[F_CHGIND_EN], 1); + if (ret) + return dev_err_probe(dev, ret, "Failed to set CHRIND to SW\n"); + } + + ret =3D mt6370_isnk_init_default_state(led); + if (ret) + return dev_err_probe(dev, ret, "Failed to init %d isnk state\n", + led->index); + + ret =3D devm_led_classdev_register_ext(dev, &led->isink, + init_data); + if (ret) + return dev_err_probe(dev, ret, "Couldn't register isink %d\n", led->ind= ex); + } + + return 0; +} + +static int mt6370_check_vendor_info(struct mt6370_priv *priv) +{ + unsigned int devinfo, vid; + int ret; + + ret =3D regmap_read(priv->regmap, MT6370_REG_DEV_INFO, &devinfo); + if (ret) + return ret; + + vid =3D FIELD_GET(MT6370_VENID_MASK, devinfo); + if (vid =3D=3D 0x9 || vid =3D=3D 0xb) { + priv->reg_fields =3D mt6372_reg_fields; + priv->ranges =3D mt6372_led_ranges; + priv->pdata =3D &mt6372_pdata; + } else { + /* Common for MT6370/71 */ + priv->reg_fields =3D common_reg_fields; + priv->ranges =3D common_led_ranges; + priv->pdata =3D &common_pdata; + } + + return 0; +} + +static int mt6370_leds_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct mt6370_priv *priv; + struct fwnode_handle *child; + size_t count; + unsigned int i =3D 0; + int 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"); + + ret =3D mt6370_check_vendor_info(priv); + if (ret) + return dev_err_probe(dev, ret, "Failed to check vendor info\n"); + + ret =3D devm_regmap_field_bulk_alloc(dev, priv->regmap, priv->fields, + priv->reg_fields, F_MAX_FIELDS); + if (ret) + return dev_err_probe(dev, ret, "Failed to allocate regmap field\n"); + + device_for_each_child_node(dev, child) { + struct mt6370_led *led =3D priv->leds + i++; + struct led_init_data init_data =3D { .fwnode =3D child }; + u32 reg, color; + + ret =3D fwnode_property_read_u32(child, "reg", ®); + if (ret) { + fwnode_handle_put(child); + return dev_err_probe(dev, ret, "Failed to parse reg property\n"); + } + + if (reg >=3D MT6370_MAX_LEDS) { + fwnode_handle_put(child); + return dev_err_probe(dev, -EINVAL, "Error reg property number\n"); + } + + ret =3D fwnode_property_read_u32(child, "color", &color); + if (ret) { + fwnode_handle_put(child); + return dev_err_probe(dev, ret, "Failed to parse color property\n"); + } + + if (color =3D=3D LED_COLOR_ID_RGB || color =3D=3D LED_COLOR_ID_MULTI) + reg =3D MT6370_VIRTUAL_MULTICOLOR; + + if (priv->leds_active & BIT(reg)) { + fwnode_handle_put(child); + return dev_err_probe(dev, -EINVAL, "Duplicate reg property\n"); + } + + priv->leds_active |=3D BIT(reg); + + led->index =3D reg; + led->priv =3D priv; + + ret =3D mt6370_init_led_properties(dev, led, &init_data); + if (ret) { + fwnode_handle_put(child); + return ret; + } + + ret =3D mt6370_led_register(dev, led, &init_data); + if (ret) { + fwnode_handle_put(child); + return ret; + } + } + + return 0; +} + +static const struct of_device_id mt6370_rgbled_device_table[] =3D { + { .compatible =3D "mediatek,mt6370-indicator" }, + {} +}; +MODULE_DEVICE_TABLE(of, mt6370_rgbled_device_table); + +static struct platform_driver mt6370_rgbled_driver =3D { + .driver =3D { + .name =3D "mt6370-indicator", + .of_match_table =3D mt6370_rgbled_device_table, + }, + .probe =3D mt6370_leds_probe, +}; +module_platform_driver(mt6370_rgbled_driver); + +MODULE_AUTHOR("Alice Chen "); +MODULE_AUTHOR("ChiYuan Huang "); +MODULE_DESCRIPTION("MediaTek MT6370 RGB LED Driver"); +MODULE_LICENSE("GPL"); --=20 2.7.4 From nobody Sun Nov 10 21:51:04 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 587FEC6379F for ; Wed, 1 Feb 2023 03:19:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230404AbjBADTT (ORCPT ); Tue, 31 Jan 2023 22:19:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230104AbjBADTL (ORCPT ); Tue, 31 Jan 2023 22:19:11 -0500 Received: from mg.richtek.com (mg.richtek.com [220.130.44.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4E73D27D55; Tue, 31 Jan 2023 19:19:07 -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(17481:0:AUTH_RELAY) (envelope-from ); Wed, 01 Feb 2023 11:18:38 +0800 (CST) Received: from ex4.rt.l (192.168.10.47) 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.20; Wed, 1 Feb 2023 11:18:37 +0800 Received: from linuxcarl2.richtek.com (192.168.10.154) by ex4.rt.l (192.168.10.45) with Microsoft SMTP Server id 15.2.1118.20 via Frontend Transport; Wed, 1 Feb 2023 11:18:37 +0800 From: ChiaEn Wu To: , , , , , CC: , , , , , , , , Alice Chen , ChiaEn Wu Subject: [PATCH v17 2/3] leds: flash: mt6370: Add MediaTek MT6370 flashlight support Date: Wed, 1 Feb 2023 11:17:28 +0800 Message-ID: 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. Co-developed-by: Alice Chen Acked-by: Jacek Anaszewski Signed-off-by: Alice Chen Signed-off-by: ChiYuan Huang Signed-off-by: ChiaEn Wu --- v17 - Update the year of Copyright from 2022 to 2023 --- drivers/leds/flash/Kconfig | 13 + drivers/leds/flash/Makefile | 1 + drivers/leds/flash/leds-mt6370-flash.c | 596 +++++++++++++++++++++++++++++= ++++ 3 files changed, 610 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..e5c4c94 --- /dev/null +++ b/drivers/leds/flash/leds-mt6370-flash.c @@ -0,0 +1,596 @@ +// 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 led_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_FLCSEN_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_strobe_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]; + + 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_no), + 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 brightness 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]; + + 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_no), + 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_FLCSEN_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_torch_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, + sizeof(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_MASK; + 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 v4l2_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 v4l2_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_data); + if (ret) + return dev_err_probe(parent, ret, + "Couldn't register flash %d\n", led->led_no); + + mt6370_init_v4l2_flash_config(led, &v4l2_config); + led->v4l2_flash =3D v4l2_flash_init(parent, fwnode, &led->flash, + &v4l2_flash_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->v4l2_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]); + } + + 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_STEP_uA); + + lcdev->max_brightness =3D (val - MT6370_ITORCH_MIN_uA) / + MT6370_ITORCH_STEP_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 current 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" }, + { /* sentinel */ } +}; +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 From nobody Sun Nov 10 21:51:04 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 1D3DAC636CC for ; Wed, 1 Feb 2023 03:19:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230116AbjBADTW (ORCPT ); Tue, 31 Jan 2023 22:19:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229893AbjBADTR (ORCPT ); Tue, 31 Jan 2023 22:19:17 -0500 Received: from mg.richtek.com (mg.richtek.com [220.130.44.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3B523458AF; Tue, 31 Jan 2023 19:19:13 -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(17481:0:AUTH_RELAY) (envelope-from ); Wed, 01 Feb 2023 11:18:52 +0800 (CST) Received: from ex4.rt.l (192.168.10.47) 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.20; Wed, 1 Feb 2023 11:18:51 +0800 Received: from linuxcarl2.richtek.com (192.168.10.154) by ex4.rt.l (192.168.10.45) with Microsoft SMTP Server id 15.2.1118.20 via Frontend Transport; Wed, 1 Feb 2023 11:18:51 +0800 From: ChiaEn Wu To: , , , , , CC: , , , , , , , , ChiaEn Wu Subject: [PATCH v17 3/3] docs: leds: Add MT6370 RGB LED pattern document Date: Wed, 1 Feb 2023 11:17:29 +0800 Message-ID: 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 Document the MT6370 RGB LED pattern trigger. This simply describe how the pattern works, each timing period, and the pattern diagram for MT6370 RGB LED. Signed-off-by: ChiYuan Huang Signed-off-by: ChiaEn Wu --- Documentation/leds/leds-mt6370-rgb.rst | 64 ++++++++++++++++++++++++++++++= ++++ 1 file changed, 64 insertions(+) create mode 100644 Documentation/leds/leds-mt6370-rgb.rst diff --git a/Documentation/leds/leds-mt6370-rgb.rst b/Documentation/leds/le= ds-mt6370-rgb.rst new file mode 100644 index 00000000..d1b2e4f --- /dev/null +++ b/Documentation/leds/leds-mt6370-rgb.rst @@ -0,0 +1,64 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Kernel driver for Mediatek MT6370 RGB LED +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Description +----------- + +The MT6370 integrates a four-channel RGB LED driver, designed to provide a +variety of lighting effect for mobile device applications. The RGB LED dri= ver +includes a smart LED string controller and it can drive 3 channels of LEDs= with +a sink current up to 24mA and a CHG_VIN power good indicator LED with sink +current up to 6mA. It provides three operation modes for RGB LEDs: +PWM Dimming mode, breath pattern mode, and constant current mode. The devi= ce +can increase or decrease the brightness of the RGB LED via I2C interface. + +The breath pattern for a channel can be programmed using the "pattern" tri= gger, +using the hw_pattern attribute. + +/sys/class/leds//hw_pattern +-------------------------------- + +Specify a hardware breath pattern for a MT6370 RGB LED. + +The breath pattern is a series of timing pairs, with the hold-time express= ed in +milliseconds. And the brightness is controlled by +'/sys/class/leds//brightness'. The pattern doesn't include the bright= ness +setting. Hardware pattern only controls the timing for each pattern stage +depending on the current brightness setting. + +Pattern diagram:: + + "0 Tr1 0 Tr2 0 Tf1 0 Tf2 0 Ton 0 Toff" --> '0' for dummy brightness c= ode + + ^ + | =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + | / \ / +Icurr | / \ / + | / \ / + | / \ / .....repeat + | / \ / + | --- --- --- + |--- --- --- + +----------------------------------=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D------------> Time + < Tr1>< Ton >< Toff >< Tr1> + +Timing description:: + +Tr1: First rising time for duty 0 to 30%. +Tr2: Second rising time for duty 31% to 100%. +Ton: On time for duty 100%. +Tf1: First falling time for duty 100% to 31%. +Tf2: Second falling time for duty 30% to 0%. +Toff: Off time for duty 0%. + +Tr1/Tr2/Tf1/Tf2/Ton: 125ms to 3125ms, 200ms per step. +Toff: 250ms to 6250ms, 400ms per step. + +Pattern example:: + + "0 125 0 125 0 125 0 125 0 625 0 1050" + +This Will configure Tr1/Tr2/Tf1/Tf2 to 125m, Ton to 625ms, and Toff to 105= 0ms. --=20 2.7.4