Qualcomm Light Pulse Generator
Subsystems (5)
v5 cover Qualcomm Light Pulse Generator
This series introduces a generic pattern interface in the LED class and a driver for the Qualcomm Light Pulse Generator.
It seems like it's been almost 3 years since I posted v3, which was hung up on the lack of conclusion on the hw_pattern and multicolor support. Now that those are concluded I hope we can make some progress on the LPG support again.
The dts patches are included in the series as "examples", ultimately my expectation is that the dt binding and driver patches are picked up through the leds tree, while Andy or myself take the dts patches.
Bjorn Andersson (4):
dt-bindings: leds: Add Qualcomm Light Pulse Generator binding
leds: Add driver for Qualcomm LPG
arm64: dts: qcom: pm(i)8994: Add mpp and lpg blocks
arm64: dts: qcom: Add user LEDs on db820c
.../bindings/leds/leds-qcom-lpg.yaml | 170 +++ arch/arm64/boot/dts/qcom/apq8096-db820c.dtsi | 49 + arch/arm64/boot/dts/qcom/pm8994.dtsi | 9 + arch/arm64/boot/dts/qcom/pmi8994.dtsi | 20 + drivers/leds/Kconfig | 9 + drivers/leds/Makefile | 1 + drivers/leds/leds-qcom-lpg.c | 1206 +++++++++++++++++ 7 files changed, 1464 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/leds-qcom-lpg.yaml create mode 100644 drivers/leds/leds-qcom-lpg.c
Signature · 1 line
2.28.0
PATCH v5 1/4 dt-bindings: leds: Add Qualcomm Light Pulse Generator binding
This adds the binding document describing the three hardware blocks related to the Light Pulse Generator found in a wide range of Qualcomm PMICs.
---
Changes since v4:
- Dropped quotes around power-source
- Moved "multi-led" to properties
- Corrected tab-indented line in example
.../bindings/leds/leds-qcom-lpg.yaml | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/leds-qcom-lpg.yaml
Documentation/devicetree/bindings/leds/leds-qcom-lpg.yaml new file +170 -0
@@ -0,0 +1,170 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/leds-qcom-lpg.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Qualcomm Light Pulse Generator + +maintainers: + - Bjorn Andersson <[email protected]> + +description: > + The Qualcomm Light Pulse Generator consists of three different hardware blocks; + a ramp generator with lookup table, the light pulse generator and a three + channel current sink. These blocks are found in a wide range of Qualcomm PMICs. + +properties: + compatible: + enum: + - qcom,pm8916-pwm + - qcom,pm8941-lpg + - qcom,pm8994-lpg + - qcom,pmi8994-lpg + - qcom,pmi8998-lpg + + "#pwm-cells": + const: 2 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + qcom,power-source: + $ref: /schemas/types.yaml#definitions/uint32 + description: > + power-source used to drive the output, as defined in the datasheet. + Should be specified if the TRILED block is present + enum: + - 0 + - 1 + - 3 + + multi-led: + type: object + $ref: leds-class-multicolor.yaml# + properties: + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + "^led@[0-9a-f]$": + type: object + $ref: common.yaml# + + properties: + "qcom,dtest": + $ref: /schemas/types.yaml#definitions/uint32-array + description: > + configures the output into an internal test line of the pmic. Specified + by a list of u32 pairs, one pair per channel, where each pair denotes the + test line to drive and the second configures how the value should be + outputed, as defined in the datasheet + minItems: 2 + maxItems: 2 + + required: + - reg + +patternProperties: + "^led@[0-9a-f]$": + type: object + $ref: common.yaml# + properties: + "qcom,dtest": + $ref: /schemas/types.yaml#definitions/uint32-array + description: > + configures the output into an internal test line of the pmic. Specified + by a list of u32 pairs, one pair per channel, where each pair denotes the + test line to drive and the second configures how the value should be + outputed, as defined in the datasheet + minItems: 2 + maxItems: 2 + + required: + - reg + +required: + - compatible + +additionalProperties: false + +examples: + - | + #include <dt-bindings/leds/common.h> + + lpg { + compatible = "qcom,pmi8994-lpg"; + + #address-cells = <1>; + #size-cells = <0>; + + qcom,power-source = <1>; + + led@1 { + reg = <1>; + label = "green:user1"; + }; + + led@2 { + reg = <2>; + label = "green:user0"; + default-state = "on"; + }; + + led@3 { + reg = <3>; + label = "green:user2"; + }; + + led@4 { + reg = <4>; + label = "green:user3"; + + qcom,dtest = <4 1>; + }; + }; + - | + #include <dt-bindings/leds/common.h> + + lpg { + compatible = "qcom,pmi8994-lpg"; + + #address-cells = <1>; + #size-cells = <0>; + + qcom,power-source = <1>; + + multi-led { + color = <LED_COLOR_ID_MULTI>; + label = "rgb:notification"; + + #address-cells = <1>; + #size-cells = <0>; + + led@1 { + reg = <1>; + color = <LED_COLOR_ID_RED>; + }; + + led@2 { + reg = <2>; + color = <LED_COLOR_ID_GREEN>; + }; + + led@3 { + reg = <3>; + color = <LED_COLOR_ID_BLUE>; + }; + }; + }; + - | + lpg { + compatible = "qcom,pm8916-pwm"; + #pwm-cells = <2>; + }; +...
Signature · 1 line
2.28.0
PATCH v5 2/4 leds: Add driver for Qualcomm LPG
In reply to Bjorn AnderssonThe Light Pulse Generator (LPG) is a PWM-block found in a wide range of PMICs from Qualcomm. It can operate on fixed parameters or based on a lookup-table, altering the duty cycle over time - which provides the means for e.g. hardware assisted transitions of LED brightness.
---
Changes since v4:
- Introduced div_u64() to avoid compilation errors on 32 bit machines
- Fixed SPDX license header and a couple of spacing issues pointed out by checkpatch
drivers/leds/Kconfig | 9 + drivers/leds/Makefile | 1 + drivers/leds/leds-qcom-lpg.c | 1206 ++++++++++++++++++++++++++++++++++ 3 files changed, 1216 insertions(+) create mode 100644 drivers/leds/leds-qcom-lpg.c
drivers/leds/Kconfig +9 -0
@@ -803,6 +803,15 @@ config LEDS_POWERNV To compile this driver as a module, choose 'm' here: the module will be called leds-powernv. +config LEDS_QCOM_LPG + tristate "LED support for Qualcomm LPG" + depends on LEDS_CLASS_MULTICOLOR + depends on OF + depends on SPMI + help + This option enables support for the Light Pulse Generator found in a + wide variety of Qualcomm PMICs. + config LEDS_SYSCON bool "LED support for LEDs on system controllers" depends on LEDS_CLASS=y
drivers/leds/Makefile +1 -0
@@ -79,6 +79,7 @@ obj-$(CONFIG_LEDS_PCA963X) += leds-pca963x.o obj-$(CONFIG_LEDS_PM8058) += leds-pm8058.o obj-$(CONFIG_LEDS_POWERNV) += leds-powernv.o obj-$(CONFIG_LEDS_PWM) += leds-pwm.o +obj-$(CONFIG_LEDS_QCOM_LPG) += leds-qcom-lpg.o obj-$(CONFIG_LEDS_REGULATOR) += leds-regulator.o obj-$(CONFIG_LEDS_S3C24XX) += leds-s3c24xx.o obj-$(CONFIG_LEDS_SC27XX_BLTC) += leds-sc27xx-bltc.o
drivers/leds/leds-qcom-lpg.c new file +1206 -0
1,206 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v5 2/4] leds: Add driver for Qualcomm LPG
With the changes done to in V5 i have nothing to add.
Re: [PATCH v5 2/4] leds: Add driver for Qualcomm LPG
Hi!
Quoted 1 line
With the changes done to in V5 i have nothing to add.
Good way to say that is "Patches 7 and 28, Reviewed-by:"...
Best regards,
Pavel
Signature · 1 line
http://www.livejournal.com/~pavelmachek
Martin Botka
Reviewed-byRe: [PATCH v5 2/4] leds: Add driver for Qualcomm LPG
Quoted 1 line
Good way to say that is "Patches 7 and 28, Reviewed-by:"...
7 and 28 ? I dont see any patches 7 and 28 (I assume thats a typo for 8)
Either way.
Re: [PATCH v5 2/4] leds: Add driver for Qualcomm LPG
Hi!
Quoted 7 lines
Quoted 1 line
Good way to say that is "Patches 7 and 28, Reviewed-by:"...
7 and 28 ? I dont see any patches 7 and 28 (I assume thats a typo for 8)
Either way.
Reviewed-by Martin Botka <[email protected]>
I was trying to use obviously bogus numbers to make you to specify which patches you reviewed :-).
Pavel
Signature · 1 line
http://www.livejournal.com/~pavelmachek
Re: [PATCH v5 2/4] leds: Add driver for Qualcomm LPG
Hi!
Quoted 2 lines
I was trying to use obviously bogus numbers to make you to specify which patches you reviewed :-).
Ahhhh now that makes much more sense.
I reviewed patch 2.
Tho from the quick look i dont see anything wrong with the DTS changes either.
Re: [PATCH v5 2/4] leds: Add driver for Qualcomm LPG
In reply to Bjorn AnderssonQuoted 15 lines · Bjorn Andersson
The Light Pulse Generator (LPG) is a PWM-block found in a wide range of PMICs from Qualcomm. It can operate on fixed parameters or based on a lookup-table, altering the duty cycle over time - which provides the means for e.g. hardware assisted transitions of LED brightness.
drivers/leds/Kconfig +7
-0+config LEDS_QCOM_LPG + tristate "LED support for Qualcomm LPG" + depends on LEDS_CLASS_MULTICOLOR + depends on OF + depends on SPMI +#include <linux/of.h> +#include <linux/of_device.h>
...
drivers/leds/leds-qcom-lpg.c line 55
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+struct lpg { +struct device *dev; +struct regmap *map;
Can't you derive the former from the latter?
Quoted 15 lines
drivers/leds/leds-qcom-lpg.c new file +15
-0+ +struct pwm_chip pwm; + +const struct lpg_data *data; + +u32 lut_base; +u32 lut_size; +unsigned long *lut_bitmap; + +u32 triled_base; +u32 triled_src; + +struct lpg_channel *channels; +unsigned int num_channels; +};
...
drivers/leds/leds-qcom-lpg.c line 194
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c new file +5
-0+static int lpg_lut_store (struct lpg *lpg,struct led_pattern *pattern, +size_t len,unsigned int *lo_idx,unsigned int *hi_idx) +{ +unsigned int idx; +u8 val[2 ];
__be16 val;
Quoted 4 lines
drivers/leds/leds-qcom-lpg.c new file +4
-0+int i; + +/* Hardware does not behave when LO_IDX == HI_IDX */ +if (len ==1 )
drivers/leds/leds-qcom-lpg.c line 208
Quoted 10 lines
drivers/leds/leds-qcom-lpg.c new file +10
-0+return -EINVAL ; + + idx =bitmap_find_next_zero_area (lpg->lut_bitmap, lpg->lut_size, +0 , len,0 ); +if (idx >= lpg->lut_size) +return -ENOMEM ; + +for (i =0 ; i < len; i++) { + val[0 ] = pattern[i].brightness &0xff ; + val[1 ] = pattern[i].brightness >>8 ;
cpu_to_be16();
Quoted 12 lines
drivers/leds/leds-qcom-lpg.c new file +12
-0+ +regmap_bulk_write (lpg->map, + lpg->lut_base +LPG_LUT_REG (idx + i), val,2 ); + } + +bitmap_set (lpg->lut_bitmap, idx, len); + + *lo_idx = idx; + *hi_idx = idx + len -1 ; + +return 0 ; +}
...
drivers/leds/leds-qcom-lpg.c line 287
Quoted 12 lines
drivers/leds/leds-qcom-lpg.c new file +12
-0+static void lpg_calc_freq (struct lpg_channel *chan,unsigned int period_us) +{ +int n, m, clk, div; +int best_m, best_div, best_clk; +unsigned int last_err, cur_err, min_err; +unsigned int tmp_p, period_n; + +if (period_us == chan->period_us) +return ; + +/* PWM Period / N */ +if (period_us < ((unsigned int )(-1 ) /NSEC_PER_USEC )) {
Please, replace all these -1 with castings to unsigned types with corresponding limits, likeUINT_MAX here.
drivers/leds/leds-qcom-lpg.c line 293
Quoted 6 lines
drivers/leds/leds-qcom-lpg.c new file +6
-0+ period_n = (period_us *NSEC_PER_USEC ) >>6 ; + n =6 ; + }else { + period_n = (period_us >>9 ) *NSEC_PER_USEC ; + n =9 ; + }
Why inconsistency in branches? Can you rather derive n and calculate only once like
period_n = (period_us >> n) * NSEC_PER_USEC;
?
Quoted 40 lines
drivers/leds/leds-qcom-lpg.c new file +40
-0+ min_err = (unsigned int )(-1 ); + last_err = (unsigned int )(-1 ); + best_m =0 ; + best_clk =0 ; + best_div =0 ; +for (clk =0 ; clk <NUM_PWM_CLK ; clk++) { +for (div =0 ; div <NUM_PWM_PREDIV ; div++) { +/* period_n = (PWM Period / N) */ +/* tmp_p = (Pre-divide * Clock Period) * 2^m */ + tmp_p = lpg_clk_table[div][clk]; +for (m =0 ; m <=NUM_EXP ; m++) { +if (period_n > tmp_p) + cur_err = period_n - tmp_p; +else + cur_err = tmp_p - period_n; + +if (cur_err < min_err) { + min_err = cur_err; + best_m = m; + best_clk = clk; + best_div = div; + } + +if (m && cur_err > last_err) +/* Break for bigger cur_err */ +break ; + + last_err = cur_err; + tmp_p <<=1 ; + } + } + } + +/* Use higher resolution */ +if (best_m >=3 && n ==6 ) { + n +=3 ; + best_m -=3 ; + } + + chan->clk = best_clk;
drivers/leds/leds-qcom-lpg.c line 344
Quoted 10 lines
drivers/leds/leds-qcom-lpg.c new file +10
-0+ chan->pre_div = best_div; + chan->pre_div_exp = best_m; + chan->pwm_size = n; + + chan->period_us = period_us; +} + +static void lpg_calc_duty (struct lpg_channel *chan,unsigned int duty_us) +{ +unsigned long max = (1 << chan->pwm_size) -1 ;
BIT() ?
drivers/leds/leds-qcom-lpg.c line 348
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c new file +4
-0+unsigned long val; + +/* Figure out pwm_value with overflow handling */ +if (duty_us <1 << (sizeof (val) *8 - chan->pwm_size))
BITS_PER_TYPE, but actually BITS_PER_LONG here.
BIT(BITS_PER_LONG - ...)
Quoted 9 lines
drivers/leds/leds-qcom-lpg.c new file +9
-0+ val = (duty_us << chan->pwm_size) / chan->period_us; + else + val = duty_us / (chan->period_us >> chan->pwm_size); + +if (val > max) + val = max; + + chan->pwm_value = val; +}
...
drivers/leds/leds-qcom-lpg.c line 387
Quoted 7 lines
drivers/leds/leds-qcom-lpg.c new file +7
-0+static void lpg_enable_glitch (struct lpg_channel *chan) +{ +struct lpg *lpg = chan->lpg; + +regmap_update_bits (lpg->map, chan->base +PWM_TYPE_CONFIG_REG , +LPG_ENABLE_GLITCH_REMOVAL ,0 ); +}
Here and everywhere else when function declared as void there is no possibility to know if we do something useful or already screwed up the things.
drivers/leds/leds-qcom-lpg.c line 400
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+static void lpg_apply_pwm_value (struct lpg_channel *chan) +{ +u8 val[] = { chan->pwm_value &0xff , chan->pwm_value >>8 };
__le16 and cpu_to_le16()
drivers/leds/leds-qcom-lpg.c line 413
Quoted 13 lines
drivers/leds/leds-qcom-lpg.c new file +12
-0+struct lpg *lpg = chan->lpg; + +if (!chan->enabled) +return ; + +regmap_bulk_write (lpg->map, chan->base +PWM_VALUE_REG , val,2 ); +} +#define LPG_PATTERN_CONFIG_LO_TO_HI BIT(4) +#define LPG_PATTERN_CONFIG_REPEAT BIT(3) +#define LPG_PATTERN_CONFIG_TOGGLE BIT(2) +#define LPG_PATTERN_CONFIG_PAUSE_HI BIT(1) +#define LPG_PATTERN_CONFIG_PAUSE_LO BIT(0)
Did I miss bits.h inclusion at the beginning of the file?
...
drivers/leds/leds-qcom-lpg.c line 597
Quoted 12 lines
drivers/leds/leds-qcom-lpg.c new file +12
-0+static int lpg_blink_set (struct lpg_led *led, +unsigned long delay_on,unsigned long delay_off) +{ +struct lpg_channel *chan; +unsigned int period_us; +unsigned int duty_us; +int i; + +if (!delay_on && !delay_off) { + delay_on =500 ; + delay_off =500 ; + }
Homegrown duty cycle?
I mean, why simply not to pass the duty cycle in percentage in the first place?
Quoted 10 lines
drivers/leds/leds-qcom-lpg.c new file +10
-0+ duty_us = delay_on *USEC_PER_MSEC ; + period_us = (delay_on + delay_off) *USEC_PER_MSEC ; + +for (i =0 ; i < led->num_channels; i++) { + chan = led->channels[i]; + +lpg_calc_freq (chan, period_us); +lpg_calc_duty (chan, duty_us); + + chan->enabled = true;
drivers/leds/leds-qcom-lpg.c line 635
Quoted 10 lines
drivers/leds/leds-qcom-lpg.c new file +9
-0+ chan->ramp_enabled = false; + +lpg_apply (chan); + } + +return 0 ; +} +#define interpolate (x1, y1, x2, y2, x) \ + ((y1) + ((y2) - (y1)) * ((x) - (x1)) / ((x2) - (x1)))
Can you rather create a generic one under lib/ or start include/linux/math.h ?
https://elixir.bootlin.com/linux/latest/A/ident/interpolate
...
Quoted 1 line
+out:
Useless label.
Quoted 1 line
+ return ret;
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = of_property_read_u32(np, "color", &color); + if (ret < 0 && ret != -EINVAL)
This check is fishy. Either you have optional property or not, in the latter case return any error code.
drivers/leds/leds-qcom-lpg.c line 862
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+return ret; + + chan->color = color;
So, it may be -EINVAL?!
drivers/leds/leds-qcom-lpg.c line 871
Quoted 8 lines
drivers/leds/leds-qcom-lpg.c new file +8
-0+ ret =of_property_read_u32_array (np,"qcom,dtest" , dtest,2 ); +if (ret <0 && ret != -EINVAL ) { +dev_err (lpg->dev,"malformed qcom,dtest of %pOFn\n" , np); +return ret; + }else if (!ret) { + chan->dtest_line = dtest[0 ]; + chan->dtest_value = dtest[1 ]; + }
Ditto.
...
drivers/leds/leds-qcom-lpg.c line 860
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+ ret =of_property_read_u32 (np,"color" , &color); +if (ret <0 && ret != -EINVAL ) +return ret;
Ditto.
...
Quoted 1 line
+ size =
sizeof(*led)+num_channels*sizeof(struct lpg_channel *);
Use respective helpers from overflow.h.
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+ led =devm_kzalloc (lpg->dev, size,GFP_KERNEL ); +if (!led) +return -ENOMEM ;
...
drivers/leds/leds-qcom-lpg.c line 1191
Quoted 8 lines
drivers/leds/leds-qcom-lpg.c new file +7
-0+static const struct of_device_id lpg_of_table[] = { + { .compatible ="qcom,pm8916-pwm" , .data = &pm8916_pwm_data }, + { .compatible ="qcom,pm8941-lpg" , .data = &pm8941_lpg_data }, + { .compatible ="qcom,pm8994-lpg" , .data = &pm8994_lpg_data }, + { .compatible ="qcom,pmi8994-lpg" , .data = &pmi8994_lpg_data }, + { .compatible ="qcom,pmi8998-lpg" , .data = &pmi8998_lpg_data }, + {},
No comma needed for terminator lines.
Quoted 1 line
+};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 2/4] leds: Add driver for Qualcomm LPG
Quoted 20 lines · Andy Shevchenko
Quoted 15 lines · Bjorn Andersson
The Light Pulse Generator (LPG) is a PWM-block found in a wide range of PMICs from Qualcomm. It can operate on fixed parameters or based on a lookup-table, altering the duty cycle over time - which provides the means for e.g. hardware assisted transitions of LED brightness.
drivers/leds/Kconfig +7
-0+config LEDS_QCOM_LPG + tristate "LED support for Qualcomm LPG" + depends on LEDS_CLASS_MULTICOLOR + depends on OF + depends on SPMI +#include <linux/of.h> +#include <linux/of_device.h>...
Quoted 6 lines
drivers/leds/leds-qcom-lpg.c line 55
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+struct lpg { +struct device *dev; +struct regmap *map;Can't you derive the former from the latter?
No, because map->dev is actually the dev->parent.
Quoted 26 lines
Quoted 15 lines
drivers/leds/leds-qcom-lpg.c new file +15
-0+ +struct pwm_chip pwm; + +const struct lpg_data *data; + +u32 lut_base; +u32 lut_size; +unsigned long *lut_bitmap; + +u32 triled_base; +u32 triled_src; + +struct lpg_channel *channels; +unsigned int num_channels; +};...
drivers/leds/leds-qcom-lpg.c line 194
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c new file +5
-0+static int lpg_lut_store (struct lpg *lpg,struct led_pattern *pattern, +size_t len,unsigned int *lo_idx,unsigned int *hi_idx) +{ +unsigned int idx; +u8 val[2 ];
__be16val;
Quoted 17 lines
Quoted 4 lines
drivers/leds/leds-qcom-lpg.c new file +4
-0+int i; + +/* Hardware does not behave when LO_IDX == HI_IDX */ +if (len ==1 )drivers/leds/leds-qcom-lpg.c line 208
Quoted 10 lines
drivers/leds/leds-qcom-lpg.c new file +10
-0+return -EINVAL ; + + idx =bitmap_find_next_zero_area (lpg->lut_bitmap, lpg->lut_size, +0 , len,0 ); +if (idx >= lpg->lut_size) +return -ENOMEM ; + +for (i =0 ; i < len; i++) { + val[0 ] = pattern[i].brightness &0xff ; + val[1 ] = pattern[i].brightness >>8 ;
cpu_to_be16();
I like it, but isn't that a le16?
Quoted 15 lines
Quoted 12 lines
drivers/leds/leds-qcom-lpg.c new file +12
-0+ +regmap_bulk_write (lpg->map, + lpg->lut_base +LPG_LUT_REG (idx + i), val,2 ); + } + +bitmap_set (lpg->lut_bitmap, idx, len); + + *lo_idx = idx; + *hi_idx = idx + len -1 ; + +return 0 ; +}...
Quoted 17 lines
drivers/leds/leds-qcom-lpg.c line 287
Quoted 12 lines
drivers/leds/leds-qcom-lpg.c new file +12
-0+static void lpg_calc_freq (struct lpg_channel *chan,unsigned int period_us) +{ +int n, m, clk, div; +int best_m, best_div, best_clk; +unsigned int last_err, cur_err, min_err; +unsigned int tmp_p, period_n; + +if (period_us == chan->period_us) +return ; + +/* PWM Period / N */ +if (period_us < ((unsigned int )(-1 ) /NSEC_PER_USEC )) {Please, replace all these -1 with castings to unsigned types with corresponding limits, like
UINT_MAXhere.
Sure thing.
Quoted 7 lines
drivers/leds/leds-qcom-lpg.c line 293
Quoted 6 lines
drivers/leds/leds-qcom-lpg.c new file +6
-0+ period_n = (period_us *NSEC_PER_USEC ) >>6 ; + n =6 ; + }else { + period_n = (period_us >>9 ) *NSEC_PER_USEC ; + n =9 ; + }
Quoted 6 lines
Why inconsistency in branches? Can you rather derive n and calculate only once like
period_n = (period_us >> n) * NSEC_PER_USEC;?
I inherited this piece from the downstream driver and I assume that the purpose was to avoid loss of precision. I will review this and if nothing else it seems like I would be able to cast period_us to more bits, do the multiply and then shift - in both cases.
Quoted 58 lines
Quoted 41 lines
drivers/leds/leds-qcom-lpg.c new file +40
-0+ min_err = (unsigned int )(-1 ); + last_err = (unsigned int )(-1 ); + best_m =0 ; + best_clk =0 ; + best_div =0 ; +for (clk =0 ; clk <NUM_PWM_CLK ; clk++) { +for (div =0 ; div <NUM_PWM_PREDIV ; div++) { +/* period_n = (PWM Period / N) */ +/* tmp_p = (Pre-divide * Clock Period) * 2^m */ + tmp_p = lpg_clk_table[div][clk]; +for (m =0 ; m <=NUM_EXP ; m++) { +if (period_n > tmp_p) + cur_err = period_n - tmp_p; +else + cur_err = tmp_p - period_n; + +if (cur_err < min_err) { + min_err = cur_err; + best_m = m; + best_clk = clk; + best_div = div; + } + +if (m && cur_err > last_err) +/* Break for bigger cur_err */ +break ; + + last_err = cur_err; + tmp_p <<=1 ; + } + } + } + +/* Use higher resolution */ +if (best_m >=3 && n ==6 ) { + n +=3 ; + best_m -=3 ; + } + + chan->clk = best_clk;drivers/leds/leds-qcom-lpg.c line 344
Quoted 10 lines
drivers/leds/leds-qcom-lpg.c new file +10
-0+ chan->pre_div = best_div; + chan->pre_div_exp = best_m; + chan->pwm_size = n; + + chan->period_us = period_us; +} + +static void lpg_calc_duty (struct lpg_channel *chan,unsigned int duty_us) +{ +unsigned long max = (1 << chan->pwm_size) -1 ;
BIT()?Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+unsigned long val; + +/* Figure out pwm_value with overflow handling */
Quoted 6 lines
Quoted 1 line
+ if (
duty_us< 1 << (sizeof(val) * 8 -chan->pwm_size))
BITS_PER_TYPE, but actuallyBITS_PER_LONGhere.
BIT(BITS_PER_LONG - ...)
Again, this seems to just be a question of avoiding overflow of the 32 bit duty_us. I'll double check the math here as well.
Quoted 12 lines
Quoted 9 lines
drivers/leds/leds-qcom-lpg.c new file +9
-0+ val = (duty_us << chan->pwm_size) / chan->period_us; + else + val = duty_us / (chan->period_us >> chan->pwm_size); + +if (val > max) + val = max; + + chan->pwm_value = val; +}...
Quoted 12 lines
drivers/leds/leds-qcom-lpg.c line 387
Quoted 7 lines
drivers/leds/leds-qcom-lpg.c new file +7
-0+static void lpg_enable_glitch (struct lpg_channel *chan) +{ +struct lpg *lpg = chan->lpg; + +regmap_update_bits (lpg->map, chan->base +PWM_TYPE_CONFIG_REG , +LPG_ENABLE_GLITCH_REMOVAL ,0 ); +}Here and everywhere else when function declared as void there is no possibility to know if we do something useful or already screwed up the things.
Ultimately these are all coming from led_classdev->brightness_set, which is a void function. So there isn't much benefit of propagating this error upwards, today.
Quoted 7 lines
drivers/leds/leds-qcom-lpg.c line 400
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+static void lpg_apply_pwm_value (struct lpg_channel *chan) +{ +u8 val[] = { chan->pwm_value &0xff , chan->pwm_value >>8 };
__le16andcpu_to_le16()
Sounds good.
Quoted 8 lines
Quoted 7 lines
drivers/leds/leds-qcom-lpg.c new file +7
-0+struct lpg *lpg = chan->lpg; + +if (!chan->enabled) +return ; + +regmap_bulk_write (lpg->map, chan->base +PWM_VALUE_REG , val,2 ); +}
Quoted 8 lines
drivers/leds/leds-qcom-lpg.c line 413
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c new file +5
-0+#define LPG_PATTERN_CONFIG_LO_TO_HI BIT(4) +#define LPG_PATTERN_CONFIG_REPEAT BIT(3) +#define LPG_PATTERN_CONFIG_TOGGLE BIT(2) +#define LPG_PATTERN_CONFIG_PAUSE_HI BIT(1) +#define LPG_PATTERN_CONFIG_PAUSE_LO BIT(0)Did I miss
bits.hinclusion at the beginning of the file?
Will add this.
Quoted 18 lines
...
drivers/leds/leds-qcom-lpg.c line 597
Quoted 12 lines
drivers/leds/leds-qcom-lpg.c new file +12
-0+static int lpg_blink_set (struct lpg_led *led, +unsigned long delay_on,unsigned long delay_off) +{ +struct lpg_channel *chan; +unsigned int period_us; +unsigned int duty_us; +int i; + +if (!delay_on && !delay_off) { + delay_on =500 ; + delay_off =500 ; + }Homegrown duty cycle?
I mean, why simply not to pass the duty cycle in percentage in the first place?
Can you explain what you're saying here.
Quoted 18 lines
Quoted 17 lines
drivers/leds/leds-qcom-lpg.c new file +17
-0+ duty_us = delay_on *USEC_PER_MSEC ; + period_us = (delay_on + delay_off) *USEC_PER_MSEC ; + +for (i =0 ; i < led->num_channels; i++) { + chan = led->channels[i]; + +lpg_calc_freq (chan, period_us); +lpg_calc_duty (chan, duty_us); + + chan->enabled = true; + chan->ramp_enabled = false; + +lpg_apply (chan); + } + +return 0 ; +}
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c line 635
Quoted 2 lines
drivers/leds/leds-qcom-lpg.c new file +2
-0+#define interpolate (x1, y1, x2, y2, x) \ + ((y1) + ((y2) - (y1)) * ((x) - (x1)) / ((x2) - (x1)))Can you rather create a generic one under
lib/or startinclude/linux/math.h?
Forgot about this, but I've seen one on LKML, will find it and work on getting that accepted.
Quoted 10 lines
https://elixir.bootlin.com/linux/latest/A/ident/interpolate
...
Quoted 1 line
+out:
Useless label.
Quoted 1 line
+ return ret;
Quoted 8 lines
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = of_property_read_u32(np, "color", &color); + if (ret < 0 && ret != -EINVAL)This check is fishy. Either you have optional property or not, in the latter case return any error code.
There's three possible outcomes here:
1) We found _one_ integer in the property, color is assigned and 0 is returned.
2) We found no property named "color", -EINVAL is returned without color being modified.
3) We found a property but it wasn't a single u32 value so a negative error (not EINVAL) is returned.
Quoted 6 lines
drivers/leds/leds-qcom-lpg.c line 862
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+return ret; + + chan->color = color;So, it may be
-EINVAL?!
So color will either be the value or the property color, or if omitted LED_COLOR_ID_GREEN.
Quoted 11 lines
drivers/leds/leds-qcom-lpg.c line 871
Quoted 8 lines
drivers/leds/leds-qcom-lpg.c new file +8
-0+ ret =of_property_read_u32_array (np,"qcom,dtest" , dtest,2 ); +if (ret <0 && ret != -EINVAL ) { +dev_err (lpg->dev,"malformed qcom,dtest of %pOFn\n" , np); +return ret; + }else if (!ret) { + chan->dtest_line = dtest[0 ]; + chan->dtest_value = dtest[1 ]; + }Ditto.
We're in !ret and as such dtest is initialized.
Quoted 8 lines
...
drivers/leds/leds-qcom-lpg.c line 860
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+ ret =of_property_read_u32 (np,"color" , &color); +if (ret <0 && ret != -EINVAL ) +return ret;Ditto.
As above, if no property color is specified, color remains 0 here which is not LED_COLOR_ID_MULTI and this is a single channel LED without its color specified.
Quoted 6 lines
...
Quoted 1 line
+ size =
sizeof(*led)+num_channels*sizeof(struct lpg_channel *);Use respective helpers from
overflow.h.
Will do, thanks.
Quoted 13 lines
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+ led =devm_kzalloc (lpg->dev, size,GFP_KERNEL ); +if (!led) +return -ENOMEM ;...
Quoted 6 lines
drivers/leds/leds-qcom-lpg.c new file +6
-0+static const struct of_device_id lpg_of_table[] = { + { .compatible ="qcom,pm8916-pwm" , .data = &pm8916_pwm_data }, + { .compatible ="qcom,pm8941-lpg" , .data = &pm8941_lpg_data }, + { .compatible ="qcom,pm8994-lpg" , .data = &pm8994_lpg_data }, + { .compatible ="qcom,pmi8994-lpg" , .data = &pmi8994_lpg_data }, + { .compatible ="qcom,pmi8998-lpg" , .data = &pmi8998_lpg_data },
Quoted 4 lines
Quoted 1 line
+ {},
No comma needed for terminator lines.
Will drop.
Quoted 1 line
Quoted 1 line
+};
Thank you Andy for the review!
Regards,
Bjorn
Re: [PATCH v5 2/4] leds: Add driver for Qualcomm LPG
Quoted 3 lines · Bjorn Andersson
Quoted 2 lines · Andy Shevchenko
On Sat, Oct 17, 2020 at 8:41 AM Bjorn Andersson
<[email protected]> wrote:
...
Quoted 26 lines
Quoted 8 lines
drivers/leds/leds-qcom-lpg.c line 194
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c new file +5
-0+static int lpg_lut_store (struct lpg *lpg,struct led_pattern *pattern, +size_t len,unsigned int *lo_idx,unsigned int *hi_idx) +{ +unsigned int idx; +u8 val[2 ];
__be16val;Quoted 17 lines
Quoted 4 lines
drivers/leds/leds-qcom-lpg.c new file +4
-0+int i; + +/* Hardware does not behave when LO_IDX == HI_IDX */ +if (len ==1 )drivers/leds/leds-qcom-lpg.c line 208
Quoted 10 lines
drivers/leds/leds-qcom-lpg.c new file +10
-0+return -EINVAL ; + + idx =bitmap_find_next_zero_area (lpg->lut_bitmap, lpg->lut_size, +0 , len,0 ); +if (idx >= lpg->lut_size) +return -ENOMEM ; + +for (i =0 ; i < len; i++) { + val[0 ] = pattern[i].brightness &0xff ; + val[1 ] = pattern[i].brightness >>8 ;
cpu_to_be16();
Quoted 1 line
I like it, but isn't that a le16?
Oh, yes.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/leds/leds-qcom-lpg.c new file +11
-0+regmap_bulk_write (lpg->map, + lpg->lut_base +LPG_LUT_REG (idx + i), val,2 ); + } + +bitmap_set (lpg->lut_bitmap, idx, len); + + *lo_idx = idx; + *hi_idx = idx + len -1 ; + +return 0 ; +}
...
Quoted 9 lines
Quoted 9 lines
drivers/leds/leds-qcom-lpg.c line 292
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c new file +5
-0+ period_n = (period_us *NSEC_PER_USEC ) >>6 ; + n =6 ; + }else { + period_n = (period_us >>9 ) *NSEC_PER_USEC ; + n =9 ;Why inconsistency in branches? Can you rather derive n and calculate only once like
Quoted 8 lines
Quoted 3 lines
period_n = (period_us >> n) * NSEC_PER_USEC;?
I inherited this piece from the downstream driver and I assume that the purpose was to avoid loss of precision. I will review this and if nothing else it seems like I would be able to cast
period_usto more bits, do the multiply and then shift - in both cases.
Understood. Yes, please check if precision doesn't suffer and update accordingly.
...
Quoted 5 lines
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c line 344
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+static void lpg_calc_duty (struct lpg_channel *chan,unsigned int duty_us) +{ +unsigned long max = (1 << chan->pwm_size) -1 ;
BIT()?
Actually if you don't use BIT() here (or U suffix) it is UB for pwm_size == 31.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/leds/leds-qcom-lpg.c new file +4
-0+unsigned long val; + +/* Figure out pwm_value with overflow handling */ +if (duty_us <1 << (sizeof (val) *8 - chan->pwm_size))
Quoted 7 lines
Quoted 4 lines
BITS_PER_TYPE, but actuallyBITS_PER_LONGhere.
BIT(BITS_PER_LONG - ...)Again, this seems to just be a question of avoiding overflow of the 32 bit
duty_us. I'll double check the math here as well.
Can pwm_size be equal to 0?
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/leds/leds-qcom-lpg.c new file +9
-0+ val = (duty_us << chan->pwm_size) / chan->period_us; + else + val = duty_us / (chan->period_us >> chan->pwm_size); + +if (val > max) + val = max; + + chan->pwm_value = val; +}
...
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
drivers/leds/leds-qcom-lpg.c new file +7
-0+static int lpg_blink_set (struct lpg_led *led, +unsigned long delay_on,unsigned long delay_off) +{ +struct lpg_channel *chan; +unsigned int period_us; +unsigned int duty_us; +int i;Quoted 8 lines
drivers/leds/leds-qcom-lpg.c line 597
Quoted 5 lines
drivers/leds/leds-qcom-lpg.c new file +5
-0+ +if (!delay_on && !delay_off) { + delay_on =500 ; + delay_off =500 ; + }Homegrown duty cycle?
I mean, why simply not to pass the duty cycle in percentage in the first place?
Quoted 1 line
Can you explain what you're saying here.
Why not to use duty cycle (in %) and period (in us) as a parameter to the function directly?
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
drivers/leds/leds-qcom-lpg.c new file +17
-0+ duty_us = delay_on *USEC_PER_MSEC ; + period_us = (delay_on + delay_off) *USEC_PER_MSEC ; + +for (i =0 ; i < led->num_channels; i++) { + chan = led->channels[i]; + +lpg_calc_freq (chan, period_us); +lpg_calc_duty (chan, duty_us); + + chan->enabled = true; + chan->ramp_enabled = false; + +lpg_apply (chan); + } + +return 0 ; +}
...
Quoted 4 lines
Quoted 1 line
Can you rather create a generic one under
lib/or startinclude/linux/math.h?Forgot about this, but I've seen one on LKML, will find it and work on getting that accepted.
Note, I have submitted the patch that splits out math.h from kernel.h (it's in Andrew's quilt and in Linux Next as of today), you may send a follow up patch that adds this functionality.
...
Quoted 10 lines
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = of_property_read_u32(np, "color", &color); + if (ret < 0 && ret != -EINVAL)This check is fishy. Either you have optional property or not, in the latter case return any error code.
There's three possible outcomes here:
1) We found _one_ integer in the property, color is assigned and 0 is returned.
I didn't get this. Doesn't your YAML schema say that it must be a single integer?
Quoted 9 lines
2) We found no property named "color",
-EINVALis returned without color being modified.
3) We found a property but it wasn't a single u32 value so a negative error (not EINVAL) is returned.Quoted 4 lines
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+return ret; + + chan->color = color;
Quoted 5 lines
Quoted 2 lines
So, it may be
-EINVAL?!So color will either be the value or the property color, or if omitted
LED_COLOR_ID_GREEN.
If property is optional, we do simple
ret = of_read_property_...(&x);
if (ret)
x = default_value;
Otherwise simple
ret = ...
if (ret)
return ret;
is sufficient.
What you have done is a little bit unusual.
...
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/leds/leds-qcom-lpg.c new file +8
-0+ ret =of_property_read_u32_array (np,"qcom,dtest" , dtest,2 ); +if (ret <0 && ret != -EINVAL ) { +dev_err (lpg->dev,"malformed qcom,dtest of %pOFn\n" , np); +return ret; + }else if (!ret) { + chan->dtest_line = dtest[0 ]; + chan->dtest_value = dtest[1 ]; + }
Quoted 4 lines
Quoted 2 lines
Ditto.
We're in !ret and as such dtest is initialized.
As above.
...
Quoted 10 lines
Quoted 6 lines
drivers/leds/leds-qcom-lpg.c line 860
Quoted 3 lines
drivers/leds/leds-qcom-lpg.c new file +3
-0+ ret =of_property_read_u32 (np,"color" , &color); +if (ret <0 && ret != -EINVAL ) +return ret;Ditto.
As above, if no property color is specified, color remains 0 here which is not
LED_COLOR_ID_MULTIand this is a single channel LED without its color specified.
As above.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v5 3/4 arm64: dts: qcom: pm(i)8994: Add mpp and lpg blocks
In reply to Bjorn AnderssonThe pm8994 contains a 6 LPG channels and the pmi8994 contains 4 MPP channels and a 4 channel LPG, with TRILED and LUT blocks.
Add nodes for these blocks.
---
Changes since v4:
- Replaced msm8996 with pm(i)8994 in subject
arch/arm64/boot/dts/qcom/pm8994.dtsi | 9 +++++++++ arch/arm64/boot/dts/qcom/pmi8994.dtsi | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+)
arch/arm64/boot/dts/qcom/pm8994.dtsi +9 -0
@@ -86,6 +86,15 @@ pmic@1 { #address-cells = <1>; #size-cells = <0>; + pm8994_lpg: lpg { + compatible = "qcom,pm8994-lpg"; + + #address-cells = <1>; + #size-cells = <0>; + + status = "disabled"; + }; + pm8994_spmi_regulators: regulators { compatible = "qcom,pm8994-regulators"; };
arch/arm64/boot/dts/qcom/pmi8994.dtsi +20 -0
@@ -19,6 +19,17 @@ pmi8994_gpios: gpios@c000 { interrupt-controller; #interrupt-cells = <2>; }; + + pmi8994_mpps: mpps@a000 { + compatible = "qcom,pm8994-mpp"; + reg = <0xa000>; + gpio-controller; + #gpio-cells = <2>; + interrupts = <0 0xa0 0 IRQ_TYPE_NONE>, + <0 0xa1 0 IRQ_TYPE_NONE>, + <0 0xa2 0 IRQ_TYPE_NONE>, + <0 0xa3 0 IRQ_TYPE_NONE>; + }; }; pmic@3 {
@@ -27,6 +38,15 @@ pmic@3 { #address-cells = <1>; #size-cells = <0>; + pmi8994_lpg: lpg@b100 { + compatible = "qcom,pmi8994-lpg"; + + #address-cells = <1>; + #size-cells = <0>; + + status = "disabled"; + }; + pmi8994_spmi_regulators: regulators { compatible = "qcom,pmi8994-regulators"; #address-cells = <1>;
Signature · 1 line
2.28.0
PATCH v5 4/4 arm64: dts: qcom: Add user LEDs on db820c
In reply to Bjorn AnderssonThe db820c has 4 "user LEDs", all connected to the PMI8994. The first three are connected to the three current sinks provided by the TRILED and the fourth is connected to MPP2.
By utilizing the DTEST bus the MPP is fed the control signal from the fourth LPG block, providing a consistent interface to the user.
---
Changes since v4:
- None
arch/arm64/boot/dts/qcom/apq8096-db820c.dtsi | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+)
arch/arm64/boot/dts/qcom/apq8096-db820c.dtsi +49 -0
@@ -8,6 +8,7 @@ #include "pmi8994.dtsi" #include <dt-bindings/input/input.h> #include <dt-bindings/gpio/gpio.h> +#include <dt-bindings/leds/common.h> #include <dt-bindings/pinctrl/qcom,pmic-gpio.h> #include <dt-bindings/sound/qcom,q6afe.h> #include <dt-bindings/sound/qcom,q6asm.h>
@@ -682,6 +683,54 @@ pinconf { }; }; +&pmi8994_mpps { + pmi8994_mpp2_userled4: mpp2-userled4 { + pins = "mpp2"; + function = "sink"; + + output-low; + qcom,dtest = <4>; + }; +}; + +&pmi8994_lpg { + qcom,power-source = <1>; + + pinctrl-names = "default"; + pinctrl-0 = <&pmi8994_mpp2_userled4>; + + status = "okay"; + + #address-cells = <1>; + #size-cells = <0>; + + led@1 { + reg = <1>; + label = "green:user1"; + + linux,default-trigger = "heartbeat"; + default-state = "on"; + }; + + led@2 { + reg = <2>; + label = "green:user0"; + default-state = "on"; + }; + + led@3 { + reg = <3>; + label = "green:user2"; + }; + + led@4 { + reg = <4>; + label = "green:user3"; + + qcom,dtest = <4 1>; + }; +}; + &pmi8994_spmi_regulators { vdd_gfx: s2@1700 { reg = <0x1700 0x100>;
Signature · 1 line
2.28.0