1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017-2022 Linaro Ltd
4 * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
5 * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
7 #include <linux/bits.h>
8 #include <linux/bitfield.h>
9 #include <linux/led-class-multicolor.h>
10 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/pwm.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
18 #define LPG_SUBTYPE_REG 0x05
19 #define LPG_SUBTYPE_LPG 0x2
20 #define LPG_SUBTYPE_PWM 0xb
21 #define LPG_SUBTYPE_HI_RES_PWM 0xc
22 #define LPG_SUBTYPE_LPG_LITE 0x11
23 #define LPG_PATTERN_CONFIG_REG 0x40
24 #define LPG_SIZE_CLK_REG 0x41
25 #define PWM_CLK_SELECT_MASK GENMASK(1, 0)
26 #define PWM_CLK_SELECT_HI_RES_MASK GENMASK(2, 0)
27 #define PWM_SIZE_HI_RES_MASK GENMASK(6, 4)
28 #define LPG_PREDIV_CLK_REG 0x42
29 #define PWM_FREQ_PRE_DIV_MASK GENMASK(6, 5)
30 #define PWM_FREQ_EXP_MASK GENMASK(2, 0)
31 #define PWM_TYPE_CONFIG_REG 0x43
32 #define PWM_VALUE_REG 0x44
33 #define PWM_ENABLE_CONTROL_REG 0x46
34 #define PWM_SYNC_REG 0x47
35 #define LPG_RAMP_DURATION_REG 0x50
36 #define LPG_HI_PAUSE_REG 0x52
37 #define LPG_LO_PAUSE_REG 0x54
38 #define LPG_HI_IDX_REG 0x56
39 #define LPG_LO_IDX_REG 0x57
40 #define PWM_SEC_ACCESS_REG 0xd0
41 #define PWM_DTEST_REG(x) (0xe2 + (x) - 1)
43 #define TRI_LED_SRC_SEL 0x45
44 #define TRI_LED_EN_CTL 0x46
45 #define TRI_LED_ATC_CTL 0x47
47 #define LPG_LUT_REG(x) (0x40 + (x) * 2)
48 #define RAMP_CONTROL_REG 0xc8
50 #define LPG_RESOLUTION_9BIT BIT(9)
51 #define LPG_RESOLUTION_15BIT BIT(15)
53 #define LPG_MAX_PREDIV 6
59 * struct lpg - LPG device context
60 * @dev: pointer to LPG device
61 * @map: regmap for register access
62 * @lock: used to synchronize LED and pwm callback requests
63 * @pwm: PWM-chip object, if operating in PWM mode
64 * @data: reference to version specific data
65 * @lut_base: base address of the LUT block (optional)
66 * @lut_size: number of entries in the LUT block
67 * @lut_bitmap: allocation bitmap for LUT entries
68 * @triled_base: base address of the TRILED block (optional)
69 * @triled_src: power-source for the TRILED
70 * @triled_has_atc_ctl: true if there is TRI_LED_ATC_CTL register
71 * @triled_has_src_sel: true if there is TRI_LED_SRC_SEL register
72 * @channels: list of PWM channels
73 * @num_channels: number of @channels
83 const struct lpg_data *data;
87 unsigned long *lut_bitmap;
91 bool triled_has_atc_ctl;
92 bool triled_has_src_sel;
94 struct lpg_channel *channels;
95 unsigned int num_channels;
99 * struct lpg_channel - per channel data
100 * @lpg: reference to parent lpg
101 * @base: base address of the PWM channel
102 * @triled_mask: mask in TRILED to enable this channel
103 * @lut_mask: mask in LUT to start pattern generator for this channel
104 * @subtype: PMIC hardware block subtype
105 * @in_use: channel is exposed to LED framework
106 * @color: color of the LED attached to this channel
107 * @dtest_line: DTEST line for output, or 0 if disabled
108 * @dtest_value: DTEST line configuration
109 * @pwm_value: duty (in microseconds) of the generated pulses, overridden by LUT
110 * @enabled: output enabled?
111 * @period: period (in nanoseconds) of the generated pulses
112 * @clk_sel: reference clock frequency selector
113 * @pre_div_sel: divider selector of the reference clock
114 * @pre_div_exp: exponential divider of the reference clock
115 * @pwm_resolution_sel: pwm resolution selector
116 * @ramp_enabled: duty cycle is driven by iterating over lookup table
117 * @ramp_ping_pong: reverse through pattern, rather than wrapping to start
118 * @ramp_oneshot: perform only a single pass over the pattern
119 * @ramp_reverse: iterate over pattern backwards
120 * @ramp_tick_ms: length (in milliseconds) of one step in the pattern
121 * @ramp_lo_pause_ms: pause (in milliseconds) before iterating over pattern
122 * @ramp_hi_pause_ms: pause (in milliseconds) after iterating over pattern
123 * @pattern_lo_idx: start index of associated pattern
124 * @pattern_hi_idx: last index of associated pattern
130 unsigned int triled_mask;
131 unsigned int lut_mask;
132 unsigned int subtype;
145 unsigned int clk_sel;
146 unsigned int pre_div_sel;
147 unsigned int pre_div_exp;
148 unsigned int pwm_resolution_sel;
154 unsigned short ramp_tick_ms;
155 unsigned long ramp_lo_pause_ms;
156 unsigned long ramp_hi_pause_ms;
158 unsigned int pattern_lo_idx;
159 unsigned int pattern_hi_idx;
163 * struct lpg_led - logical LED object
164 * @lpg: lpg context reference
165 * @cdev: LED class device
166 * @mcdev: Multicolor LED class device
167 * @num_channels: number of @channels
168 * @channels: list of channels associated with the LED
173 struct led_classdev cdev;
174 struct led_classdev_mc mcdev;
176 unsigned int num_channels;
177 struct lpg_channel *channels[];
181 * struct lpg_channel_data - per channel initialization data
182 * @base: base address for PWM channel registers
183 * @triled_mask: bitmask for controlling this channel in TRILED
185 struct lpg_channel_data {
191 * struct lpg_data - initialization data
192 * @lut_base: base address of LUT block
193 * @lut_size: number of entries in LUT
194 * @triled_base: base address of TRILED
195 * @triled_has_atc_ctl: true if there is TRI_LED_ATC_CTL register
196 * @triled_has_src_sel: true if there is TRI_LED_SRC_SEL register
197 * @num_channels: number of channels in LPG
198 * @channels: list of channel initialization data
201 unsigned int lut_base;
202 unsigned int lut_size;
203 unsigned int triled_base;
204 bool triled_has_atc_ctl;
205 bool triled_has_src_sel;
207 const struct lpg_channel_data *channels;
210 static int triled_set(struct lpg *lpg, unsigned int mask, unsigned int enable)
212 /* Skip if we don't have a triled block */
213 if (!lpg->triled_base)
216 return regmap_update_bits(lpg->map, lpg->triled_base + TRI_LED_EN_CTL,
220 static int lpg_lut_store(struct lpg *lpg, struct led_pattern *pattern,
221 size_t len, unsigned int *lo_idx, unsigned int *hi_idx)
227 idx = bitmap_find_next_zero_area(lpg->lut_bitmap, lpg->lut_size,
229 if (idx >= lpg->lut_size)
232 for (i = 0; i < len; i++) {
233 val = pattern[i].brightness;
235 regmap_bulk_write(lpg->map, lpg->lut_base + LPG_LUT_REG(idx + i),
239 bitmap_set(lpg->lut_bitmap, idx, len);
242 *hi_idx = idx + len - 1;
247 static void lpg_lut_free(struct lpg *lpg, unsigned int lo_idx, unsigned int hi_idx)
251 len = hi_idx - lo_idx + 1;
255 bitmap_clear(lpg->lut_bitmap, lo_idx, len);
258 static int lpg_lut_sync(struct lpg *lpg, unsigned int mask)
260 return regmap_write(lpg->map, lpg->lut_base + RAMP_CONTROL_REG, mask);
263 static const unsigned int lpg_clk_rates[] = {0, 1024, 32768, 19200000};
264 static const unsigned int lpg_clk_rates_hi_res[] = {0, 1024, 32768, 19200000, 76800000};
265 static const unsigned int lpg_pre_divs[] = {1, 3, 5, 6};
266 static const unsigned int lpg_pwm_resolution[] = {9};
267 static const unsigned int lpg_pwm_resolution_hi_res[] = {8, 9, 10, 11, 12, 13, 14, 15};
269 static int lpg_calc_freq(struct lpg_channel *chan, uint64_t period)
271 unsigned int i, pwm_resolution_count, best_pwm_resolution_sel = 0;
272 const unsigned int *clk_rate_arr, *pwm_resolution_arr;
273 unsigned int clk_sel, clk_len, best_clk = 0;
274 unsigned int div, best_div = 0;
275 unsigned int m, best_m = 0;
276 unsigned int resolution;
278 unsigned int best_err = UINT_MAX;
279 u64 max_period, min_period;
284 * The PWM period is determined by:
286 * resolution * pre_div * 2^M
287 * period = --------------------------
290 * Resolution = 2^9 bits for PWM or
291 * 2^{8, 9, 10, 11, 12, 13, 14, 15} bits for high resolution PWM
292 * pre_div = {1, 3, 5, 6} and
295 * This allows for periods between 27uS and 384s for PWM channels and periods between
296 * 3uS and 24576s for high resolution PWMs.
297 * The PWM framework wants a period of equal or lower length than requested,
298 * reject anything below minimum period.
301 if (chan->subtype == LPG_SUBTYPE_HI_RES_PWM) {
302 clk_rate_arr = lpg_clk_rates_hi_res;
303 clk_len = ARRAY_SIZE(lpg_clk_rates_hi_res);
304 pwm_resolution_arr = lpg_pwm_resolution_hi_res;
305 pwm_resolution_count = ARRAY_SIZE(lpg_pwm_resolution_hi_res);
306 max_res = LPG_RESOLUTION_15BIT;
308 clk_rate_arr = lpg_clk_rates;
309 clk_len = ARRAY_SIZE(lpg_clk_rates);
310 pwm_resolution_arr = lpg_pwm_resolution;
311 pwm_resolution_count = ARRAY_SIZE(lpg_pwm_resolution);
312 max_res = LPG_RESOLUTION_9BIT;
315 min_period = (u64)NSEC_PER_SEC *
316 div64_u64((1 << pwm_resolution_arr[0]), clk_rate_arr[clk_len - 1]);
317 if (period <= min_period)
320 /* Limit period to largest possible value, to avoid overflows */
321 max_period = (u64)NSEC_PER_SEC * max_res * LPG_MAX_PREDIV *
322 div64_u64((1 << LPG_MAX_M), 1024);
323 if (period > max_period)
327 * Search for the pre_div, refclk, resolution and M by solving the rewritten formula
328 * for each refclk, resolution and pre_div value:
331 * M = log2 -------------------------------------
332 * NSEC_PER_SEC * pre_div * resolution
335 for (i = 0; i < pwm_resolution_count; i++) {
336 resolution = 1 << pwm_resolution_arr[i];
337 for (clk_sel = 1; clk_sel < clk_len; clk_sel++) {
338 u64 numerator = period * clk_rate_arr[clk_sel];
340 for (div = 0; div < ARRAY_SIZE(lpg_pre_divs); div++) {
341 u64 denominator = (u64)NSEC_PER_SEC * lpg_pre_divs[div] *
346 if (numerator < denominator)
349 ratio = div64_u64(numerator, denominator);
354 actual = DIV_ROUND_UP_ULL(denominator * (1 << m),
355 clk_rate_arr[clk_sel]);
356 error = period - actual;
357 if (error < best_err) {
362 best_period = actual;
363 best_pwm_resolution_sel = i;
368 chan->clk_sel = best_clk;
369 chan->pre_div_sel = best_div;
370 chan->pre_div_exp = best_m;
371 chan->period = best_period;
372 chan->pwm_resolution_sel = best_pwm_resolution_sel;
376 static void lpg_calc_duty(struct lpg_channel *chan, uint64_t duty)
380 unsigned int clk_rate;
382 if (chan->subtype == LPG_SUBTYPE_HI_RES_PWM) {
383 max = LPG_RESOLUTION_15BIT - 1;
384 clk_rate = lpg_clk_rates_hi_res[chan->clk_sel];
386 max = LPG_RESOLUTION_9BIT - 1;
387 clk_rate = lpg_clk_rates[chan->clk_sel];
390 val = div64_u64(duty * clk_rate,
391 (u64)NSEC_PER_SEC * lpg_pre_divs[chan->pre_div_sel] * (1 << chan->pre_div_exp));
393 chan->pwm_value = min(val, max);
396 static void lpg_apply_freq(struct lpg_channel *chan)
399 struct lpg *lpg = chan->lpg;
406 /* Specify resolution, based on the subtype of the channel */
407 switch (chan->subtype) {
408 case LPG_SUBTYPE_LPG:
409 val |= GENMASK(5, 4);
411 case LPG_SUBTYPE_PWM:
414 case LPG_SUBTYPE_HI_RES_PWM:
415 val |= FIELD_PREP(PWM_SIZE_HI_RES_MASK, chan->pwm_resolution_sel);
417 case LPG_SUBTYPE_LPG_LITE:
423 regmap_write(lpg->map, chan->base + LPG_SIZE_CLK_REG, val);
425 val = FIELD_PREP(PWM_FREQ_PRE_DIV_MASK, chan->pre_div_sel) |
426 FIELD_PREP(PWM_FREQ_EXP_MASK, chan->pre_div_exp);
427 regmap_write(lpg->map, chan->base + LPG_PREDIV_CLK_REG, val);
430 #define LPG_ENABLE_GLITCH_REMOVAL BIT(5)
432 static void lpg_enable_glitch(struct lpg_channel *chan)
434 struct lpg *lpg = chan->lpg;
436 regmap_update_bits(lpg->map, chan->base + PWM_TYPE_CONFIG_REG,
437 LPG_ENABLE_GLITCH_REMOVAL, 0);
440 static void lpg_disable_glitch(struct lpg_channel *chan)
442 struct lpg *lpg = chan->lpg;
444 regmap_update_bits(lpg->map, chan->base + PWM_TYPE_CONFIG_REG,
445 LPG_ENABLE_GLITCH_REMOVAL,
446 LPG_ENABLE_GLITCH_REMOVAL);
449 static void lpg_apply_pwm_value(struct lpg_channel *chan)
451 struct lpg *lpg = chan->lpg;
452 u16 val = chan->pwm_value;
457 regmap_bulk_write(lpg->map, chan->base + PWM_VALUE_REG, &val, sizeof(val));
460 #define LPG_PATTERN_CONFIG_LO_TO_HI BIT(4)
461 #define LPG_PATTERN_CONFIG_REPEAT BIT(3)
462 #define LPG_PATTERN_CONFIG_TOGGLE BIT(2)
463 #define LPG_PATTERN_CONFIG_PAUSE_HI BIT(1)
464 #define LPG_PATTERN_CONFIG_PAUSE_LO BIT(0)
466 static void lpg_apply_lut_control(struct lpg_channel *chan)
468 struct lpg *lpg = chan->lpg;
469 unsigned int hi_pause;
470 unsigned int lo_pause;
471 unsigned int conf = 0;
472 unsigned int lo_idx = chan->pattern_lo_idx;
473 unsigned int hi_idx = chan->pattern_hi_idx;
474 u16 step = chan->ramp_tick_ms;
476 if (!chan->ramp_enabled || chan->pattern_lo_idx == chan->pattern_hi_idx)
479 hi_pause = DIV_ROUND_UP(chan->ramp_hi_pause_ms, step);
480 lo_pause = DIV_ROUND_UP(chan->ramp_lo_pause_ms, step);
482 if (!chan->ramp_reverse)
483 conf |= LPG_PATTERN_CONFIG_LO_TO_HI;
484 if (!chan->ramp_oneshot)
485 conf |= LPG_PATTERN_CONFIG_REPEAT;
486 if (chan->ramp_ping_pong)
487 conf |= LPG_PATTERN_CONFIG_TOGGLE;
488 if (chan->ramp_hi_pause_ms)
489 conf |= LPG_PATTERN_CONFIG_PAUSE_HI;
490 if (chan->ramp_lo_pause_ms)
491 conf |= LPG_PATTERN_CONFIG_PAUSE_LO;
493 regmap_write(lpg->map, chan->base + LPG_PATTERN_CONFIG_REG, conf);
494 regmap_write(lpg->map, chan->base + LPG_HI_IDX_REG, hi_idx);
495 regmap_write(lpg->map, chan->base + LPG_LO_IDX_REG, lo_idx);
497 regmap_bulk_write(lpg->map, chan->base + LPG_RAMP_DURATION_REG, &step, sizeof(step));
498 regmap_write(lpg->map, chan->base + LPG_HI_PAUSE_REG, hi_pause);
499 regmap_write(lpg->map, chan->base + LPG_LO_PAUSE_REG, lo_pause);
502 #define LPG_ENABLE_CONTROL_OUTPUT BIT(7)
503 #define LPG_ENABLE_CONTROL_BUFFER_TRISTATE BIT(5)
504 #define LPG_ENABLE_CONTROL_SRC_PWM BIT(2)
505 #define LPG_ENABLE_CONTROL_RAMP_GEN BIT(1)
507 static void lpg_apply_control(struct lpg_channel *chan)
510 struct lpg *lpg = chan->lpg;
512 ctrl = LPG_ENABLE_CONTROL_BUFFER_TRISTATE;
515 ctrl |= LPG_ENABLE_CONTROL_OUTPUT;
517 if (chan->pattern_lo_idx != chan->pattern_hi_idx)
518 ctrl |= LPG_ENABLE_CONTROL_RAMP_GEN;
520 ctrl |= LPG_ENABLE_CONTROL_SRC_PWM;
522 regmap_write(lpg->map, chan->base + PWM_ENABLE_CONTROL_REG, ctrl);
525 * Due to LPG hardware bug, in the PWM mode, having enabled PWM,
526 * We have to write PWM values one more time.
529 lpg_apply_pwm_value(chan);
532 #define LPG_SYNC_PWM BIT(0)
534 static void lpg_apply_sync(struct lpg_channel *chan)
536 struct lpg *lpg = chan->lpg;
538 regmap_write(lpg->map, chan->base + PWM_SYNC_REG, LPG_SYNC_PWM);
541 static int lpg_parse_dtest(struct lpg *lpg)
543 struct lpg_channel *chan;
544 struct device_node *np = lpg->dev->of_node;
549 count = of_property_count_u32_elems(np, "qcom,dtest");
550 if (count == -EINVAL) {
552 } else if (count < 0) {
555 } else if (count != lpg->data->num_channels * 2) {
556 dev_err(lpg->dev, "qcom,dtest needs to be %d items\n",
557 lpg->data->num_channels * 2);
561 for (i = 0; i < lpg->data->num_channels; i++) {
562 chan = &lpg->channels[i];
564 ret = of_property_read_u32_index(np, "qcom,dtest", i * 2,
569 ret = of_property_read_u32_index(np, "qcom,dtest", i * 2 + 1,
578 dev_err(lpg->dev, "malformed qcom,dtest\n");
582 static void lpg_apply_dtest(struct lpg_channel *chan)
584 struct lpg *lpg = chan->lpg;
586 if (!chan->dtest_line)
589 regmap_write(lpg->map, chan->base + PWM_SEC_ACCESS_REG, 0xa5);
590 regmap_write(lpg->map, chan->base + PWM_DTEST_REG(chan->dtest_line),
594 static void lpg_apply(struct lpg_channel *chan)
596 lpg_disable_glitch(chan);
597 lpg_apply_freq(chan);
598 lpg_apply_pwm_value(chan);
599 lpg_apply_control(chan);
600 lpg_apply_sync(chan);
601 lpg_apply_lut_control(chan);
602 lpg_enable_glitch(chan);
605 static void lpg_brightness_set(struct lpg_led *led, struct led_classdev *cdev,
606 struct mc_subled *subleds)
608 enum led_brightness brightness;
609 struct lpg_channel *chan;
610 unsigned int triled_enabled = 0;
611 unsigned int triled_mask = 0;
612 unsigned int lut_mask = 0;
614 struct lpg *lpg = led->lpg;
617 for (i = 0; i < led->num_channels; i++) {
618 chan = led->channels[i];
619 brightness = subleds[i].brightness;
621 if (brightness == LED_OFF) {
622 chan->enabled = false;
623 chan->ramp_enabled = false;
624 } else if (chan->pattern_lo_idx != chan->pattern_hi_idx) {
625 lpg_calc_freq(chan, NSEC_PER_MSEC);
627 chan->enabled = true;
628 chan->ramp_enabled = true;
630 lut_mask |= chan->lut_mask;
631 triled_enabled |= chan->triled_mask;
633 lpg_calc_freq(chan, NSEC_PER_MSEC);
635 duty = div_u64(brightness * chan->period, cdev->max_brightness);
636 lpg_calc_duty(chan, duty);
637 chan->enabled = true;
638 chan->ramp_enabled = false;
640 triled_enabled |= chan->triled_mask;
643 triled_mask |= chan->triled_mask;
648 /* Toggle triled lines */
650 triled_set(lpg, triled_mask, triled_enabled);
652 /* Trigger start of ramp generator(s) */
654 lpg_lut_sync(lpg, lut_mask);
657 static int lpg_brightness_single_set(struct led_classdev *cdev,
658 enum led_brightness value)
660 struct lpg_led *led = container_of(cdev, struct lpg_led, cdev);
661 struct mc_subled info;
663 mutex_lock(&led->lpg->lock);
665 info.brightness = value;
666 lpg_brightness_set(led, cdev, &info);
668 mutex_unlock(&led->lpg->lock);
673 static int lpg_brightness_mc_set(struct led_classdev *cdev,
674 enum led_brightness value)
676 struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
677 struct lpg_led *led = container_of(mc, struct lpg_led, mcdev);
679 mutex_lock(&led->lpg->lock);
681 led_mc_calc_color_components(mc, value);
682 lpg_brightness_set(led, cdev, mc->subled_info);
684 mutex_unlock(&led->lpg->lock);
689 static int lpg_blink_set(struct lpg_led *led,
690 unsigned long *delay_on, unsigned long *delay_off)
692 struct lpg_channel *chan;
694 unsigned int triled_mask = 0;
695 struct lpg *lpg = led->lpg;
699 if (!*delay_on && !*delay_off) {
704 duty = *delay_on * NSEC_PER_MSEC;
705 period = (*delay_on + *delay_off) * NSEC_PER_MSEC;
707 for (i = 0; i < led->num_channels; i++) {
708 chan = led->channels[i];
710 lpg_calc_freq(chan, period);
711 lpg_calc_duty(chan, duty);
713 chan->enabled = true;
714 chan->ramp_enabled = false;
716 triled_mask |= chan->triled_mask;
721 /* Enable triled lines */
722 triled_set(lpg, triled_mask, triled_mask);
724 chan = led->channels[0];
725 duty = div_u64(chan->pwm_value * chan->period, LPG_RESOLUTION_9BIT);
726 *delay_on = div_u64(duty, NSEC_PER_MSEC);
727 *delay_off = div_u64(chan->period - duty, NSEC_PER_MSEC);
732 static int lpg_blink_single_set(struct led_classdev *cdev,
733 unsigned long *delay_on, unsigned long *delay_off)
735 struct lpg_led *led = container_of(cdev, struct lpg_led, cdev);
738 mutex_lock(&led->lpg->lock);
740 ret = lpg_blink_set(led, delay_on, delay_off);
742 mutex_unlock(&led->lpg->lock);
747 static int lpg_blink_mc_set(struct led_classdev *cdev,
748 unsigned long *delay_on, unsigned long *delay_off)
750 struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
751 struct lpg_led *led = container_of(mc, struct lpg_led, mcdev);
754 mutex_lock(&led->lpg->lock);
756 ret = lpg_blink_set(led, delay_on, delay_off);
758 mutex_unlock(&led->lpg->lock);
763 static int lpg_pattern_set(struct lpg_led *led, struct led_pattern *led_pattern,
766 struct lpg_channel *chan;
767 struct lpg *lpg = led->lpg;
768 struct led_pattern *pattern;
769 unsigned int brightness_a;
770 unsigned int brightness_b;
771 unsigned int actual_len;
772 unsigned int hi_pause;
773 unsigned int lo_pause;
774 unsigned int delta_t;
778 bool ping_pong = true;
781 /* Hardware only support oneshot or indefinite loops */
782 if (repeat != -1 && repeat != 1)
786 * The standardized leds-trigger-pattern format defines that the
787 * brightness of the LED follows a linear transition from one entry
788 * in the pattern to the next, over the given delta_t time. It
789 * describes that the way to perform instant transitions a zero-length
790 * entry should be added following a pattern entry.
792 * The LPG hardware is only able to perform the latter (no linear
793 * transitions), so require each entry in the pattern to be followed by
794 * a zero-length transition.
799 pattern = kcalloc(len / 2, sizeof(*pattern), GFP_KERNEL);
803 for (i = 0; i < len; i += 2) {
804 if (led_pattern[i].brightness != led_pattern[i + 1].brightness)
805 goto out_free_pattern;
806 if (led_pattern[i + 1].delta_t != 0)
807 goto out_free_pattern;
809 pattern[i / 2].brightness = led_pattern[i].brightness;
810 pattern[i / 2].delta_t = led_pattern[i].delta_t;
816 * Specifying a pattern of length 1 causes the hardware to iterate
817 * through the entire LUT, so prohibit this.
820 goto out_free_pattern;
823 * The LPG plays patterns with at a fixed pace, a "low pause" can be
824 * used to stretch the first delay of the pattern and a "high pause"
827 * In order to save space the pattern can be played in "ping pong"
828 * mode, in which the pattern is first played forward, then "high
829 * pause" is applied, then the pattern is played backwards and finally
830 * the "low pause" is applied.
832 * The middle elements of the pattern are used to determine delta_t and
833 * the "low pause" and "high pause" multipliers are derrived from this.
835 * The first element in the pattern is used to determine "low pause".
837 * If the specified pattern is a palindrome the ping pong mode is
838 * enabled. In this scenario the delta_t of the middle entry (i.e. the
839 * last in the programmed pattern) determines the "high pause".
842 /* Detect palindromes and use "ping pong" to reduce LUT usage */
843 for (i = 0; i < len / 2; i++) {
844 brightness_a = pattern[i].brightness;
845 brightness_b = pattern[len - i - 1].brightness;
847 if (brightness_a != brightness_b) {
853 /* The pattern length to be written to the LUT */
855 actual_len = (len + 1) / 2;
860 * Validate that all delta_t in the pattern are the same, with the
861 * exception of the middle element in case of ping_pong.
863 delta_t = pattern[1].delta_t;
864 for (i = 2; i < len; i++) {
865 if (pattern[i].delta_t != delta_t) {
867 * Allow last entry in the full or shortened pattern to
868 * specify hi pause. Reject other variations.
870 if (i != actual_len - 1)
871 goto out_free_pattern;
875 /* LPG_RAMP_DURATION_REG is a 9bit */
876 if (delta_t >= BIT(9))
877 goto out_free_pattern;
879 /* Find "low pause" and "high pause" in the pattern */
880 lo_pause = pattern[0].delta_t;
881 hi_pause = pattern[actual_len - 1].delta_t;
883 mutex_lock(&lpg->lock);
884 ret = lpg_lut_store(lpg, pattern, actual_len, &lo_idx, &hi_idx);
888 for (i = 0; i < led->num_channels; i++) {
889 chan = led->channels[i];
891 chan->ramp_tick_ms = delta_t;
892 chan->ramp_ping_pong = ping_pong;
893 chan->ramp_oneshot = repeat != -1;
895 chan->ramp_lo_pause_ms = lo_pause;
896 chan->ramp_hi_pause_ms = hi_pause;
898 chan->pattern_lo_idx = lo_idx;
899 chan->pattern_hi_idx = hi_idx;
903 mutex_unlock(&lpg->lock);
910 static int lpg_pattern_single_set(struct led_classdev *cdev,
911 struct led_pattern *pattern, u32 len,
914 struct lpg_led *led = container_of(cdev, struct lpg_led, cdev);
917 ret = lpg_pattern_set(led, pattern, len, repeat);
921 lpg_brightness_single_set(cdev, LED_FULL);
926 static int lpg_pattern_mc_set(struct led_classdev *cdev,
927 struct led_pattern *pattern, u32 len,
930 struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
931 struct lpg_led *led = container_of(mc, struct lpg_led, mcdev);
934 ret = lpg_pattern_set(led, pattern, len, repeat);
938 led_mc_calc_color_components(mc, LED_FULL);
939 lpg_brightness_set(led, cdev, mc->subled_info);
944 static int lpg_pattern_clear(struct lpg_led *led)
946 struct lpg_channel *chan;
947 struct lpg *lpg = led->lpg;
950 mutex_lock(&lpg->lock);
952 chan = led->channels[0];
953 lpg_lut_free(lpg, chan->pattern_lo_idx, chan->pattern_hi_idx);
955 for (i = 0; i < led->num_channels; i++) {
956 chan = led->channels[i];
957 chan->pattern_lo_idx = 0;
958 chan->pattern_hi_idx = 0;
961 mutex_unlock(&lpg->lock);
966 static int lpg_pattern_single_clear(struct led_classdev *cdev)
968 struct lpg_led *led = container_of(cdev, struct lpg_led, cdev);
970 return lpg_pattern_clear(led);
973 static int lpg_pattern_mc_clear(struct led_classdev *cdev)
975 struct led_classdev_mc *mc = lcdev_to_mccdev(cdev);
976 struct lpg_led *led = container_of(mc, struct lpg_led, mcdev);
978 return lpg_pattern_clear(led);
981 static int lpg_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
983 struct lpg *lpg = container_of(chip, struct lpg, pwm);
984 struct lpg_channel *chan = &lpg->channels[pwm->hwpwm];
986 return chan->in_use ? -EBUSY : 0;
991 * - Updating both duty and period is not done atomically, so the output signal
992 * will momentarily be a mix of the settings.
993 * - Changed parameters takes effect immediately.
994 * - A disabled channel outputs a logical 0.
996 static int lpg_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
997 const struct pwm_state *state)
999 struct lpg *lpg = container_of(chip, struct lpg, pwm);
1000 struct lpg_channel *chan = &lpg->channels[pwm->hwpwm];
1003 if (state->polarity != PWM_POLARITY_NORMAL)
1006 mutex_lock(&lpg->lock);
1008 if (state->enabled) {
1009 ret = lpg_calc_freq(chan, state->period);
1013 lpg_calc_duty(chan, state->duty_cycle);
1015 chan->enabled = state->enabled;
1019 triled_set(lpg, chan->triled_mask, chan->enabled ? chan->triled_mask : 0);
1022 mutex_unlock(&lpg->lock);
1027 static int lpg_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
1028 struct pwm_state *state)
1030 struct lpg *lpg = container_of(chip, struct lpg, pwm);
1031 struct lpg_channel *chan = &lpg->channels[pwm->hwpwm];
1032 unsigned int resolution;
1033 unsigned int pre_div;
1034 unsigned int refclk;
1040 ret = regmap_read(lpg->map, chan->base + LPG_SIZE_CLK_REG, &val);
1044 if (chan->subtype == LPG_SUBTYPE_HI_RES_PWM) {
1045 refclk = lpg_clk_rates_hi_res[FIELD_GET(PWM_CLK_SELECT_HI_RES_MASK, val)];
1046 resolution = lpg_pwm_resolution_hi_res[FIELD_GET(PWM_SIZE_HI_RES_MASK, val)];
1048 refclk = lpg_clk_rates[FIELD_GET(PWM_CLK_SELECT_MASK, val)];
1053 ret = regmap_read(lpg->map, chan->base + LPG_PREDIV_CLK_REG, &val);
1057 pre_div = lpg_pre_divs[FIELD_GET(PWM_FREQ_PRE_DIV_MASK, val)];
1058 m = FIELD_GET(PWM_FREQ_EXP_MASK, val);
1060 ret = regmap_bulk_read(lpg->map, chan->base + PWM_VALUE_REG, &pwm_value, sizeof(pwm_value));
1064 state->period = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * (1 << resolution) *
1065 pre_div * (1 << m), refclk);
1066 state->duty_cycle = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * pwm_value * pre_div * (1 << m), refclk);
1069 state->duty_cycle = 0;
1072 ret = regmap_read(lpg->map, chan->base + PWM_ENABLE_CONTROL_REG, &val);
1076 state->enabled = FIELD_GET(LPG_ENABLE_CONTROL_OUTPUT, val);
1077 state->polarity = PWM_POLARITY_NORMAL;
1079 if (state->duty_cycle > state->period)
1080 state->duty_cycle = state->period;
1085 static const struct pwm_ops lpg_pwm_ops = {
1086 .request = lpg_pwm_request,
1087 .apply = lpg_pwm_apply,
1088 .get_state = lpg_pwm_get_state,
1089 .owner = THIS_MODULE,
1092 static int lpg_add_pwm(struct lpg *lpg)
1097 lpg->pwm.dev = lpg->dev;
1098 lpg->pwm.npwm = lpg->num_channels;
1099 lpg->pwm.ops = &lpg_pwm_ops;
1101 ret = pwmchip_add(&lpg->pwm);
1103 dev_err(lpg->dev, "failed to add PWM chip: ret %d\n", ret);
1108 static int lpg_parse_channel(struct lpg *lpg, struct device_node *np,
1109 struct lpg_channel **channel)
1111 struct lpg_channel *chan;
1112 u32 color = LED_COLOR_ID_GREEN;
1116 ret = of_property_read_u32(np, "reg", ®);
1117 if (ret || !reg || reg > lpg->num_channels) {
1118 dev_err(lpg->dev, "invalid \"reg\" of %pOFn\n", np);
1122 chan = &lpg->channels[reg - 1];
1123 chan->in_use = true;
1125 ret = of_property_read_u32(np, "color", &color);
1126 if (ret < 0 && ret != -EINVAL) {
1127 dev_err(lpg->dev, "failed to parse \"color\" of %pOF\n", np);
1131 chan->color = color;
1138 static int lpg_add_led(struct lpg *lpg, struct device_node *np)
1140 struct led_init_data init_data = {};
1141 struct led_classdev *cdev;
1142 struct device_node *child;
1143 struct mc_subled *info;
1144 struct lpg_led *led;
1151 ret = of_property_read_u32(np, "color", &color);
1152 if (ret < 0 && ret != -EINVAL) {
1153 dev_err(lpg->dev, "failed to parse \"color\" of %pOF\n", np);
1157 if (color == LED_COLOR_ID_RGB)
1158 num_channels = of_get_available_child_count(np);
1162 led = devm_kzalloc(lpg->dev, struct_size(led, channels, num_channels), GFP_KERNEL);
1167 led->num_channels = num_channels;
1169 if (color == LED_COLOR_ID_RGB) {
1170 info = devm_kcalloc(lpg->dev, num_channels, sizeof(*info), GFP_KERNEL);
1174 for_each_available_child_of_node(np, child) {
1175 ret = lpg_parse_channel(lpg, child, &led->channels[i]);
1179 info[i].color_index = led->channels[i]->color;
1180 info[i].intensity = 0;
1184 led->mcdev.subled_info = info;
1185 led->mcdev.num_colors = num_channels;
1187 cdev = &led->mcdev.led_cdev;
1188 cdev->brightness_set_blocking = lpg_brightness_mc_set;
1189 cdev->blink_set = lpg_blink_mc_set;
1191 /* Register pattern accessors only if we have a LUT block */
1192 if (lpg->lut_base) {
1193 cdev->pattern_set = lpg_pattern_mc_set;
1194 cdev->pattern_clear = lpg_pattern_mc_clear;
1197 ret = lpg_parse_channel(lpg, np, &led->channels[0]);
1202 cdev->brightness_set_blocking = lpg_brightness_single_set;
1203 cdev->blink_set = lpg_blink_single_set;
1205 /* Register pattern accessors only if we have a LUT block */
1206 if (lpg->lut_base) {
1207 cdev->pattern_set = lpg_pattern_single_set;
1208 cdev->pattern_clear = lpg_pattern_single_clear;
1212 cdev->default_trigger = of_get_property(np, "linux,default-trigger", NULL);
1213 cdev->max_brightness = LPG_RESOLUTION_9BIT - 1;
1215 if (!of_property_read_string(np, "default-state", &state) &&
1216 !strcmp(state, "on"))
1217 cdev->brightness = cdev->max_brightness;
1219 cdev->brightness = LED_OFF;
1221 cdev->brightness_set_blocking(cdev, cdev->brightness);
1223 init_data.fwnode = of_fwnode_handle(np);
1225 if (color == LED_COLOR_ID_RGB)
1226 ret = devm_led_classdev_multicolor_register_ext(lpg->dev, &led->mcdev, &init_data);
1228 ret = devm_led_classdev_register_ext(lpg->dev, &led->cdev, &init_data);
1230 dev_err(lpg->dev, "unable to register %s\n", cdev->name);
1235 static int lpg_init_channels(struct lpg *lpg)
1237 const struct lpg_data *data = lpg->data;
1238 struct lpg_channel *chan;
1241 lpg->num_channels = data->num_channels;
1242 lpg->channels = devm_kcalloc(lpg->dev, data->num_channels,
1243 sizeof(struct lpg_channel), GFP_KERNEL);
1247 for (i = 0; i < data->num_channels; i++) {
1248 chan = &lpg->channels[i];
1251 chan->base = data->channels[i].base;
1252 chan->triled_mask = data->channels[i].triled_mask;
1253 chan->lut_mask = BIT(i);
1255 regmap_read(lpg->map, chan->base + LPG_SUBTYPE_REG, &chan->subtype);
1261 static int lpg_init_triled(struct lpg *lpg)
1263 struct device_node *np = lpg->dev->of_node;
1266 /* Skip initialization if we don't have a triled block */
1267 if (!lpg->data->triled_base)
1270 lpg->triled_base = lpg->data->triled_base;
1271 lpg->triled_has_atc_ctl = lpg->data->triled_has_atc_ctl;
1272 lpg->triled_has_src_sel = lpg->data->triled_has_src_sel;
1274 if (lpg->triled_has_src_sel) {
1275 ret = of_property_read_u32(np, "qcom,power-source", &lpg->triled_src);
1276 if (ret || lpg->triled_src == 2 || lpg->triled_src > 3) {
1277 dev_err(lpg->dev, "invalid power source\n");
1282 /* Disable automatic trickle charge LED */
1283 if (lpg->triled_has_atc_ctl)
1284 regmap_write(lpg->map, lpg->triled_base + TRI_LED_ATC_CTL, 0);
1286 /* Configure power source */
1287 if (lpg->triled_has_src_sel)
1288 regmap_write(lpg->map, lpg->triled_base + TRI_LED_SRC_SEL, lpg->triled_src);
1290 /* Default all outputs to off */
1291 regmap_write(lpg->map, lpg->triled_base + TRI_LED_EN_CTL, 0);
1296 static int lpg_init_lut(struct lpg *lpg)
1298 const struct lpg_data *data = lpg->data;
1300 if (!data->lut_base)
1303 lpg->lut_base = data->lut_base;
1304 lpg->lut_size = data->lut_size;
1306 lpg->lut_bitmap = devm_bitmap_zalloc(lpg->dev, lpg->lut_size, GFP_KERNEL);
1307 if (!lpg->lut_bitmap)
1313 static int lpg_probe(struct platform_device *pdev)
1315 struct device_node *np;
1320 lpg = devm_kzalloc(&pdev->dev, sizeof(*lpg), GFP_KERNEL);
1324 lpg->data = of_device_get_match_data(&pdev->dev);
1328 platform_set_drvdata(pdev, lpg);
1330 lpg->dev = &pdev->dev;
1331 mutex_init(&lpg->lock);
1333 lpg->map = dev_get_regmap(pdev->dev.parent, NULL);
1335 return dev_err_probe(&pdev->dev, -ENXIO, "parent regmap unavailable\n");
1337 ret = lpg_init_channels(lpg);
1341 ret = lpg_parse_dtest(lpg);
1345 ret = lpg_init_triled(lpg);
1349 ret = lpg_init_lut(lpg);
1353 for_each_available_child_of_node(pdev->dev.of_node, np) {
1354 ret = lpg_add_led(lpg, np);
1359 for (i = 0; i < lpg->num_channels; i++)
1360 lpg_apply_dtest(&lpg->channels[i]);
1362 return lpg_add_pwm(lpg);
1365 static int lpg_remove(struct platform_device *pdev)
1367 struct lpg *lpg = platform_get_drvdata(pdev);
1369 pwmchip_remove(&lpg->pwm);
1374 static const struct lpg_data pm8916_pwm_data = {
1376 .channels = (const struct lpg_channel_data[]) {
1381 static const struct lpg_data pm8941_lpg_data = {
1385 .triled_base = 0xd000,
1386 .triled_has_atc_ctl = true,
1387 .triled_has_src_sel = true,
1390 .channels = (const struct lpg_channel_data[]) {
1395 { .base = 0xb500, .triled_mask = BIT(5) },
1396 { .base = 0xb600, .triled_mask = BIT(6) },
1397 { .base = 0xb700, .triled_mask = BIT(7) },
1402 static const struct lpg_data pm8994_lpg_data = {
1407 .channels = (const struct lpg_channel_data[]) {
1417 static const struct lpg_data pmi8994_lpg_data = {
1421 .triled_base = 0xd000,
1422 .triled_has_atc_ctl = true,
1423 .triled_has_src_sel = true,
1426 .channels = (const struct lpg_channel_data[]) {
1427 { .base = 0xb100, .triled_mask = BIT(5) },
1428 { .base = 0xb200, .triled_mask = BIT(6) },
1429 { .base = 0xb300, .triled_mask = BIT(7) },
1434 static const struct lpg_data pmi8998_lpg_data = {
1438 .triled_base = 0xd000,
1441 .channels = (const struct lpg_channel_data[]) {
1444 { .base = 0xb300, .triled_mask = BIT(5) },
1445 { .base = 0xb400, .triled_mask = BIT(6) },
1446 { .base = 0xb500, .triled_mask = BIT(7) },
1451 static const struct lpg_data pm8150b_lpg_data = {
1455 .triled_base = 0xd000,
1458 .channels = (const struct lpg_channel_data[]) {
1459 { .base = 0xb100, .triled_mask = BIT(7) },
1460 { .base = 0xb200, .triled_mask = BIT(6) },
1464 static const struct lpg_data pm8150l_lpg_data = {
1468 .triled_base = 0xd000,
1471 .channels = (const struct lpg_channel_data[]) {
1472 { .base = 0xb100, .triled_mask = BIT(7) },
1473 { .base = 0xb200, .triled_mask = BIT(6) },
1474 { .base = 0xb300, .triled_mask = BIT(5) },
1481 static const struct lpg_data pm8350c_pwm_data = {
1482 .triled_base = 0xef00,
1485 .channels = (const struct lpg_channel_data[]) {
1486 { .base = 0xe800, .triled_mask = BIT(7) },
1487 { .base = 0xe900, .triled_mask = BIT(6) },
1488 { .base = 0xea00, .triled_mask = BIT(5) },
1493 static const struct lpg_data pmk8550_pwm_data = {
1495 .channels = (const struct lpg_channel_data[]) {
1501 static const struct of_device_id lpg_of_table[] = {
1502 { .compatible = "qcom,pm8150b-lpg", .data = &pm8150b_lpg_data },
1503 { .compatible = "qcom,pm8150l-lpg", .data = &pm8150l_lpg_data },
1504 { .compatible = "qcom,pm8350c-pwm", .data = &pm8350c_pwm_data },
1505 { .compatible = "qcom,pm8916-pwm", .data = &pm8916_pwm_data },
1506 { .compatible = "qcom,pm8941-lpg", .data = &pm8941_lpg_data },
1507 { .compatible = "qcom,pm8994-lpg", .data = &pm8994_lpg_data },
1508 { .compatible = "qcom,pmi8994-lpg", .data = &pmi8994_lpg_data },
1509 { .compatible = "qcom,pmi8998-lpg", .data = &pmi8998_lpg_data },
1510 { .compatible = "qcom,pmc8180c-lpg", .data = &pm8150l_lpg_data },
1511 { .compatible = "qcom,pmk8550-pwm", .data = &pmk8550_pwm_data },
1514 MODULE_DEVICE_TABLE(of, lpg_of_table);
1516 static struct platform_driver lpg_driver = {
1518 .remove = lpg_remove,
1520 .name = "qcom-spmi-lpg",
1521 .of_match_table = lpg_of_table,
1524 module_platform_driver(lpg_driver);
1526 MODULE_DESCRIPTION("Qualcomm LPG LED driver");
1527 MODULE_LICENSE("GPL v2");