Lines Matching +full:led +full:- +full:0

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * LED driver for Mediatek MT6323 PMIC
18 * 32K clock common for LED device.
24 #define RG_VWLED_1M_CK_PDN BIT(0)
30 * individual clock for LED device.
41 #define ISINK_CON(r, i) (r + 0x8 * (i))
44 #define ISINK_DIM_DUTY_MASK (0x1f << 8)
48 #define ISINK_DIM_FSEL_MASK (0xffff)
53 #define ISINK_CH_STEP_MASK (0x7 << 12)
55 #define ISINK_SFSTR0_TC_MASK (0x3 << 1)
57 #define ISINK_SFSTR0_EN_MASK BIT(0)
58 #define ISINK_SFSTR0_EN BIT(0)
60 /* Register to LED channel enablement. */
69 * struct mt6323_led - state container for the LED device
70 * @id: the identifier in MT6323 LED device
71 * @parent: the pointer to MT6323 LED controller
72 * @cdev: LED class device for this LED device
73 * @current_brightness: current state of the LED device
83 * struct mt6323_regs - register spec for the LED device
84 * @top_ckpdn: Offset to ISINK_CKPDN[0..x] registers
86 * @top_ckcon: Offset to ISINK_CKCON[0..x] registers
88 * @isink_con: Offset to ISINKx_CON[0..x] registers
90 * @isink_max_regs: Number of ISINK[0..x] registers
107 * struct mt6323_hwspec - hardware specific parameters
123 * struct mt6323_data - device specific data
133 * struct mt6323_leds - state container for holding LED controller
140 * @led: the array that contains the state of individual
141 * LED device
149 struct mt6323_led *led[MAX_SUPPORTED_LEDS]; member
155 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_led_hw_brightness() local
156 struct mt6323_leds *leds = led->parent; in mt6323_led_hw_brightness()
157 const struct mt6323_regs *regs = leds->pdata->regs; in mt6323_led_hw_brightness()
158 struct regmap *regmap = leds->hw->regmap; in mt6323_led_hw_brightness()
159 u32 con2_mask = 0, con2_val = 0; in mt6323_led_hw_brightness()
169 con2_val |= ISINK_CH_STEP(brightness - 1) | in mt6323_led_hw_brightness()
173 ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[2], led->id), in mt6323_led_hw_brightness()
180 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_led_hw_off() local
181 struct mt6323_leds *leds = led->parent; in mt6323_led_hw_off()
182 const struct mt6323_regs *regs = leds->pdata->regs; in mt6323_led_hw_off()
183 struct regmap *regmap = leds->hw->regmap; in mt6323_led_hw_off()
187 status = ISINK_CH_EN(led->id); in mt6323_led_hw_off()
188 ret = regmap_update_bits(regmap, regs->isink_en_ctrl, in mt6323_led_hw_off()
189 ISINK_CH_EN_MASK(led->id), ~status); in mt6323_led_hw_off()
190 if (ret < 0) in mt6323_led_hw_off()
194 ret = regmap_update_bits(regmap, regs->top_ckpdn[2], in mt6323_led_hw_off()
195 RG_ISINK_CK_PDN_MASK(led->id), in mt6323_led_hw_off()
196 RG_ISINK_CK_PDN(led->id)); in mt6323_led_hw_off()
197 if (ret < 0) in mt6323_led_hw_off()
200 return 0; in mt6323_led_hw_off()
206 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_get_led_hw_brightness() local
207 struct mt6323_leds *leds = led->parent; in mt6323_get_led_hw_brightness()
208 const struct mt6323_regs *regs = leds->pdata->regs; in mt6323_get_led_hw_brightness()
209 struct regmap *regmap = leds->hw->regmap; in mt6323_get_led_hw_brightness()
213 ret = regmap_read(regmap, regs->top_ckpdn[2], &status); in mt6323_get_led_hw_brightness()
214 if (ret < 0) in mt6323_get_led_hw_brightness()
217 if (status & RG_ISINK_CK_PDN_MASK(led->id)) in mt6323_get_led_hw_brightness()
218 return 0; in mt6323_get_led_hw_brightness()
220 ret = regmap_read(regmap, regs->isink_en_ctrl, &status); in mt6323_get_led_hw_brightness()
221 if (ret < 0) in mt6323_get_led_hw_brightness()
224 if (!(status & ISINK_CH_EN(led->id))) in mt6323_get_led_hw_brightness()
225 return 0; in mt6323_get_led_hw_brightness()
227 ret = regmap_read(regmap, ISINK_CON(regs->isink_con[2], led->id), &status); in mt6323_get_led_hw_brightness()
228 if (ret < 0) in mt6323_get_led_hw_brightness()
238 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_led_hw_on() local
239 struct mt6323_leds *leds = led->parent; in mt6323_led_hw_on()
240 const struct mt6323_regs *regs = leds->pdata->regs; in mt6323_led_hw_on()
241 struct regmap *regmap = leds->hw->regmap; in mt6323_led_hw_on()
250 ret = regmap_update_bits(regmap, regs->top_ckcon[1], in mt6323_led_hw_on()
251 RG_ISINK_CK_SEL_MASK(led->id), 0); in mt6323_led_hw_on()
252 if (ret < 0) in mt6323_led_hw_on()
255 status = RG_ISINK_CK_PDN(led->id); in mt6323_led_hw_on()
256 ret = regmap_update_bits(regmap, regs->top_ckpdn[2], in mt6323_led_hw_on()
257 RG_ISINK_CK_PDN_MASK(led->id), in mt6323_led_hw_on()
259 if (ret < 0) in mt6323_led_hw_on()
264 ret = regmap_update_bits(regmap, regs->isink_en_ctrl, in mt6323_led_hw_on()
265 ISINK_CH_EN_MASK(led->id), in mt6323_led_hw_on()
266 ISINK_CH_EN(led->id)); in mt6323_led_hw_on()
267 if (ret < 0) in mt6323_led_hw_on()
271 if (ret < 0) in mt6323_led_hw_on()
274 ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[0], led->id), in mt6323_led_hw_on()
277 if (ret < 0) in mt6323_led_hw_on()
280 ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[1], led->id), in mt6323_led_hw_on()
283 if (ret < 0) in mt6323_led_hw_on()
286 return 0; in mt6323_led_hw_on()
293 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_led_set_blink() local
294 struct mt6323_leds *leds = led->parent; in mt6323_led_set_blink()
295 const struct mt6323_regs *regs = leds->pdata->regs; in mt6323_led_set_blink()
296 const struct mt6323_hwspec *spec = leds->pdata->spec; in mt6323_led_set_blink()
297 struct regmap *regmap = leds->hw->regmap; in mt6323_led_set_blink()
303 * LED subsystem requires a default user in mt6323_led_set_blink()
304 * friendly blink pattern for the LED so using in mt6323_led_set_blink()
319 if (period > spec->max_period) in mt6323_led_set_blink()
320 return -EINVAL; in mt6323_led_set_blink()
324 * which the led is ON. in mt6323_led_set_blink()
326 duty_hw = DIV_ROUND_CLOSEST(*delay_on * 100000ul, period * spec->unit_duty); in mt6323_led_set_blink()
330 return -EINVAL; in mt6323_led_set_blink()
332 mutex_lock(&leds->lock); in mt6323_led_set_blink()
337 if (!led->current_brightness) { in mt6323_led_set_blink()
338 ret = mt6323_led_hw_on(cdev, cdev->max_brightness); in mt6323_led_set_blink()
339 if (ret < 0) in mt6323_led_set_blink()
341 led->current_brightness = cdev->max_brightness; in mt6323_led_set_blink()
344 ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[0], led->id), in mt6323_led_set_blink()
346 ISINK_DIM_DUTY(duty_hw - 1)); in mt6323_led_set_blink()
347 if (ret < 0) in mt6323_led_set_blink()
350 ret = regmap_update_bits(regmap, ISINK_CON(regs->isink_con[1], led->id), in mt6323_led_set_blink()
352 ISINK_DIM_FSEL(period - 1)); in mt6323_led_set_blink()
354 mutex_unlock(&leds->lock); in mt6323_led_set_blink()
362 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_led_set_brightness() local
363 struct mt6323_leds *leds = led->parent; in mt6323_led_set_brightness()
366 mutex_lock(&leds->lock); in mt6323_led_set_brightness()
368 if (!led->current_brightness && brightness) { in mt6323_led_set_brightness()
370 if (ret < 0) in mt6323_led_set_brightness()
374 if (ret < 0) in mt6323_led_set_brightness()
378 if (ret < 0) in mt6323_led_set_brightness()
382 led->current_brightness = brightness; in mt6323_led_set_brightness()
384 mutex_unlock(&leds->lock); in mt6323_led_set_brightness()
391 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mtk_wled_hw_on() local
392 struct mt6323_leds *leds = led->parent; in mtk_wled_hw_on()
393 const struct mt6323_regs *regs = leds->pdata->regs; in mtk_wled_hw_on()
394 struct regmap *regmap = leds->hw->regmap; in mtk_wled_hw_on()
397 ret = regmap_clear_bits(regmap, regs->top_ckpdn[0], RG_VWLED_32K_CK_PDN); in mtk_wled_hw_on()
401 ret = regmap_clear_bits(regmap, regs->top_ckpdn[0], RG_VWLED_6M_CK_PDN); in mtk_wled_hw_on()
405 ret = regmap_clear_bits(regmap, regs->top_ckpdn[0], RG_VWLED_1M_CK_PDN); in mtk_wled_hw_on()
412 ret = regmap_set_bits(regmap, regs->iwled_en_ctrl, BIT(led->id)); in mtk_wled_hw_on()
416 ret = regmap_set_bits(regmap, regs->iwled_en_ctrl, BIT(led->id + 1)); in mtk_wled_hw_on()
420 return 0; in mtk_wled_hw_on()
425 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mtk_wled_hw_off() local
426 struct mt6323_leds *leds = led->parent; in mtk_wled_hw_off()
427 const struct mt6323_regs *regs = leds->pdata->regs; in mtk_wled_hw_off()
428 struct regmap *regmap = leds->hw->regmap; in mtk_wled_hw_off()
431 ret = regmap_clear_bits(regmap, regs->iwled_en_ctrl, BIT(led->id + 1)); in mtk_wled_hw_off()
435 ret = regmap_clear_bits(regmap, regs->iwled_en_ctrl, BIT(led->id)); in mtk_wled_hw_off()
439 ret = regmap_set_bits(regmap, regs->top_ckpdn[0], RG_VWLED_32K_CK_PDN); in mtk_wled_hw_off()
443 ret = regmap_set_bits(regmap, regs->top_ckpdn[0], RG_VWLED_6M_CK_PDN); in mtk_wled_hw_off()
447 ret = regmap_set_bits(regmap, regs->top_ckpdn[0], RG_VWLED_1M_CK_PDN); in mtk_wled_hw_off()
451 return 0; in mtk_wled_hw_off()
456 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_get_wled_brightness() local
457 struct mt6323_leds *leds = led->parent; in mt6323_get_wled_brightness()
458 const struct mt6323_regs *regs = leds->pdata->regs; in mt6323_get_wled_brightness()
459 struct regmap *regmap = leds->hw->regmap; in mt6323_get_wled_brightness()
463 ret = regmap_read(regmap, regs->iwled_en_ctrl, &status); in mt6323_get_wled_brightness()
465 return 0; in mt6323_get_wled_brightness()
468 status &= BIT(led->id) | BIT(led->id + 1); in mt6323_get_wled_brightness()
470 return status ? led->current_brightness : 0; in mt6323_get_wled_brightness()
476 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_wled_set_brightness() local
477 struct mt6323_leds *leds = led->parent; in mt6323_wled_set_brightness()
478 int ret = 0; in mt6323_wled_set_brightness()
480 mutex_lock(&leds->lock); in mt6323_wled_set_brightness()
483 if (!led->current_brightness) in mt6323_wled_set_brightness()
493 led->current_brightness = brightness; in mt6323_wled_set_brightness()
495 mutex_unlock(&leds->lock); in mt6323_wled_set_brightness()
503 struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev); in mt6323_led_set_dt_default() local
505 int ret = 0; in mt6323_led_set_dt_default()
510 ret = mt6323_led_set_brightness(cdev, cdev->max_brightness); in mt6323_led_set_dt_default()
514 if (ret < 0) in mt6323_led_set_dt_default()
516 led->current_brightness = ret; in mt6323_led_set_dt_default()
517 ret = 0; in mt6323_led_set_dt_default()
528 struct device *dev = &pdev->dev; in mt6323_led_probe()
530 struct mt6397_chip *hw = dev_get_drvdata(dev->parent); in mt6323_led_probe()
532 struct mt6323_led *led; in mt6323_led_probe() local
542 return -ENOMEM; in mt6323_led_probe()
545 leds->dev = dev; in mt6323_led_probe()
546 leds->pdata = device_get_match_data(dev); in mt6323_led_probe()
547 regs = leds->pdata->regs; in mt6323_led_probe()
548 spec = leds->pdata->spec; in mt6323_led_probe()
549 max_leds = spec->max_leds + spec->max_wleds; in mt6323_led_probe()
552 * leds->hw points to the underlying bus for the register in mt6323_led_probe()
555 leds->hw = hw; in mt6323_led_probe()
556 mutex_init(&leds->lock); in mt6323_led_probe()
559 ret = regmap_update_bits(leds->hw->regmap, regs->top_ckpdn[0], in mt6323_led_probe()
561 if (ret < 0) { in mt6323_led_probe()
562 dev_err(leds->dev, in mt6323_led_probe()
573 dev_err(dev, "Failed to read led 'reg' property\n"); in mt6323_led_probe()
578 leds->led[reg]) { in mt6323_led_probe()
579 dev_err(dev, "Invalid led reg %u\n", reg); in mt6323_led_probe()
580 return -EINVAL; in mt6323_led_probe()
583 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); in mt6323_led_probe()
584 if (!led) in mt6323_led_probe()
585 return -ENOMEM; in mt6323_led_probe()
587 is_wled = of_property_read_bool(child, "mediatek,is-wled"); in mt6323_led_probe()
589 leds->led[reg] = led; in mt6323_led_probe()
590 leds->led[reg]->id = reg; in mt6323_led_probe()
591 leds->led[reg]->cdev.max_brightness = spec->max_brightness; in mt6323_led_probe()
594 leds->led[reg]->cdev.brightness_set_blocking = in mt6323_led_probe()
596 leds->led[reg]->cdev.brightness_get = in mt6323_led_probe()
599 leds->led[reg]->cdev.brightness_set_blocking = in mt6323_led_probe()
601 leds->led[reg]->cdev.blink_set = mt6323_led_set_blink; in mt6323_led_probe()
602 leds->led[reg]->cdev.brightness_get = in mt6323_led_probe()
605 leds->led[reg]->parent = leds; in mt6323_led_probe()
607 ret = mt6323_led_set_dt_default(&leds->led[reg]->cdev, child); in mt6323_led_probe()
608 if (ret < 0) { in mt6323_led_probe()
609 dev_err(leds->dev, in mt6323_led_probe()
610 "Failed to LED set default from devicetree\n"); in mt6323_led_probe()
616 ret = devm_led_classdev_register_ext(dev, &leds->led[reg]->cdev, in mt6323_led_probe()
619 dev_err(dev, "Failed to register LED: %d\n", ret); in mt6323_led_probe()
624 return 0; in mt6323_led_probe()
630 const struct mt6323_regs *regs = leds->pdata->regs; in mt6323_led_remove()
634 for (i = 0 ; leds->led[i] ; i++) in mt6323_led_remove()
635 mt6323_led_hw_off(&leds->led[i]->cdev); in mt6323_led_remove()
637 regmap_update_bits(leds->hw->regmap, regs->top_ckpdn[0], in mt6323_led_remove()
641 mutex_destroy(&leds->lock); in mt6323_led_remove()
645 .top_ckpdn = (const u16[]){ 0x102, 0x106, 0x10e },
647 .top_ckcon = (const u16[]){ 0x120, 0x126 },
649 .isink_con = (const u16[]){ 0x330, 0x332, 0x334 },
651 .isink_max_regs = 4, /* ISINK[0..3] */
652 .isink_en_ctrl = 0x356,
656 .top_ckpdn = (const u16[]){ 0x138, 0x13e, 0x144 },
658 .top_ckcon = (const u16[]){ 0x14c, 0x14a },
660 .isink_con = (const u16[]){ 0x40c, 0x40e, 0x410, 0x412, 0x414 },
662 .isink_max_regs = 4, /* ISINK[0..3] */
663 .isink_en_ctrl = 0x43a,
667 .top_ckpdn = (const u16[]){ 0x8094, 0x809a, 0x80a0 },
669 .top_ckcon = (const u16[]){ 0x80a6, 0x80ac },
671 .isink_con = (const u16[]){ 0x8cd4 },
673 .isink_max_regs = 12, /* IWLED[0..2, 3..9] */
674 .iwled_en_ctrl = 0x8cda,
686 .max_leds = 0,
707 { .compatible = "mediatek,mt6323-led", .data = &mt6323_pdata},
708 { .compatible = "mediatek,mt6331-led", .data = &mt6331_pdata },
709 { .compatible = "mediatek,mt6332-led", .data = &mt6332_pdata },
718 .name = "mt6323-led",
725 MODULE_DESCRIPTION("LED driver for Mediatek MT6323 PMIC");