Skip to contentSkip to conversation

qcom: spmi-wled: Support for QCOM wled driver

PATCH In review Kiran Gunda 5 participants 28 messages Nov 16, 2017 – Apr 23, 2018

Subsystems (5)
lore
Share & tools
lore
#1 lore raw

PATCH 1/4 qcom: spmi-wled: Add support for qcom wled driver

In reply to Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  90 ++++
 drivers/video/backlight/Kconfig                    |   9 +
 drivers/video/backlight/Makefile                   |   1 +
 drivers/video/backlight/qcom-spmi-wled.c           | 504 +++++++++++++++++++++
 4 files changed, 604 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
 create mode 100644 drivers/video/backlight/qcom-spmi-wled.c
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +90 -0
new file mode 100644
index 0000000..f1ea25b
@@ -0,0 +1,90 @@
+Binding for Qualcomm WLED driver
+
+WLED (White Light Emitting Diode) driver is used for controlling display
+backlight that is part of PMIC on Qualcomm Technologies reference platforms.
+The PMIC is connected to the host processor via SPMI bus.
+
+- compatible
+	Usage:      required
+	Value type: <string>
+	Definition: should be "qcom,pm8998-spmi-wled".
+
+- reg
+	Usage:      required
+	Value type: <prop-encoded-array>
+	Definition:  Base address and size of the WLED modules.
+
+- reg-names
+	Usage:      required
+	Value type: <string>
+	Definition:  Names associated with base addresses. should be
+		     "qcom-wled-ctrl-base", "qcom-wled-sink-base".
+
+- label
+	Usage:      required
+	Value type: <string>
+	Definition: The name of the backlight device.
+
+- default-brightness
+	Usage:      optional
+	Value type: <u32>
+	Definition: brightness value on boot, value from: 0-4095
+		    default: 2048
+
+- qcom,fs-current-limit
+	Usage:      optional
+	Value type: <u32>
+	Definition: per-string full scale current limit in uA. value from
+		    0 to 30000 with 5000 uA resolution. default: 25000 uA
+
+- qcom,current-boost-limit
+	Usage:      optional
+	Value type: <u32>
+	Definition: ILIM threshold in mA. values are 105, 280, 450, 620, 970,
+		    1150, 1300, 1500. default: 970 mA
+
+- qcom,switching-freq
+	Usage:      optional
+	Value type: <u32>
+	Definition: Switching frequency in KHz. values are
+		    600, 640, 685, 738, 800, 872, 960, 1066, 1200, 1371,
+		    1600, 1920, 2400, 3200, 4800, 9600.
+		    default: 800 KHz
+
+- qcom,ovp
+	Usage:      optional
+	Value type: <u32>
+	Definition: Over-voltage protection limit in mV. values are 31100,
+		    29600, 19600, 18100.
+	            default: 29600 mV
+
+- qcom,string-cfg
+	Usage:      optional
+	Value type: <u32>
+	Definition: Bit mask of the wled strings. Bit 0 to 3 indicates strings
+		    0 to 3 respectively. Wled module has four strings of leds
+		    numbered from 0 to 3. Each string of leds are operated
+		    individually. Specify the strings using the bit mask. Any
+		    combination of led strings can be used.
+		    default value is 15 (b1111).
+
+- qcom,en-cabc
+	Usage:      optional
+	Value type: <bool>
+	Definition: Specify if cabc (content adaptive backlight control) is
+		    needed.
+
+Example:
+
+qcom-wled@d800 {
+	compatible = "qcom,pm8998-spmi-wled";
+	reg = <0xd800 0xd900>;
+	reg-names = "qcom-wled-ctrl-base", "qcom-wled-sink-base";
+	label = "backlight";
+
+	qcom,fs-current-limit = <25000>;
+	qcom,current-boost-limit = <970>;
+	qcom,switching-freq = <800>;
+	qcom,ovp = <29600>;
+	qcom,string-cfg = <15>;
+};
drivers/video/backlight/Kconfig +9 -0
index 4e1d2ad..19ea799 100644
@@ -306,6 +306,15 @@ config BACKLIGHT_PM8941_WLED
 	  If you have the Qualcomm PM8941, say Y to enable a driver for the
 	  WLED block.
 
+config BACKLIGHT_QCOM_SPMI_WLED
+	tristate "Qualcomm WLED Driver"
+	select REGMAP
+	help
+	  If you have the Qualcomm WLED used for backlight control, say Y to
+	  enable a driver for the  WLED block. This driver provides the
+	  interface to the display driver to adjust the brightness of the
+	  display backlight. This supports PMI8998 currently.
+
 config BACKLIGHT_SAHARA
 	tristate "Tabletkiosk Sahara Touch-iT Backlight Driver"
 	depends on X86
drivers/video/backlight/Makefile +1 -0
index 5e28f01..f6627e5 100644
@@ -51,6 +51,7 @@ obj-$(CONFIG_BACKLIGHT_PANDORA)		+= pandora_bl.o
 obj-$(CONFIG_BACKLIGHT_PCF50633)	+= pcf50633-backlight.o
 obj-$(CONFIG_BACKLIGHT_PM8941_WLED)	+= pm8941-wled.o
 obj-$(CONFIG_BACKLIGHT_PWM)		+= pwm_bl.o
+obj-$(CONFIG_BACKLIGHT_QCOM_SPMI_WLED)	+= qcom-spmi-wled.o
 obj-$(CONFIG_BACKLIGHT_SAHARA)		+= kb3886_bl.o
 obj-$(CONFIG_BACKLIGHT_SKY81452)	+= sky81452-backlight.o
 obj-$(CONFIG_BACKLIGHT_TOSA)		+= tosa_bl.o
drivers/video/backlight/qcom-spmi-wled.c new file +504 -0

504 lines not on this page · raw message

Signature · 7 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#12 lore raw

PATCH 2/4 qcom: spmi-wled: Add support for short circuit handling

In reply to Kiran Gunda

Handle the short circuit(SC) interrupt and check if the SC interrupt is valid. Re-enable the module to check if it goes away. Disable the module altogether if the SC event persists.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  22 ++++
 drivers/video/backlight/qcom-spmi-wled.c           | 126 ++++++++++++++++++++-
 2 files changed, 142 insertions(+), 6 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +22 -0
index f1ea25b..768608c 100644
@@ -74,6 +74,26 @@ The PMIC is connected to the host processor via SPMI bus.
 	Definition: Specify if cabc (content adaptive backlight control) is
 		    needed.
 
+- qcom,ext-pfet-sc-pro-en
+	Usage:      optional
+	Value type: <bool>
+	Definition: Specify if external PFET control for short circuit
+		    protection is needed.
+
+- interrupts
+	Usage:      optional
+	Value type: <prop encoded array>
+	Definition: Interrupts associated with WLED. Interrupts can be
+		    specified as per the encoding listed under
+		    Documentation/devicetree/bindings/spmi/
+		    qcom,spmi-pmic-arb.txt.
+
+- interrupt-names
+	Usage:      optional
+	Value type: <string>
+	Definition: Interrupt names associated with the interrupts.
+		    Must be "sc-irq".
+
 Example:
 
 qcom-wled@d800 {
@@ -82,6 +102,8 @@ qcom-wled@d800 {
 	reg-names = "qcom-wled-ctrl-base", "qcom-wled-sink-base";
 	label = "backlight";
 
+	interrupts = <0x3 0xd8 0x2 IRQ_TYPE_EDGE_RISING>;
+	interrupt-names = "sc-irq";
 	qcom,fs-current-limit = <25000>;
 	qcom,current-boost-limit = <970>;
 	qcom,switching-freq = <800>;
drivers/video/backlight/qcom-spmi-wled.c +120 -6

232 lines not on this page · raw message

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#17 lore raw

PATCH 3/4 qcom: spmi-wled: Add support for OVP interrupt handling

In reply to Kiran Gunda

WLED peripheral has over voltage protection(OVP) circuitry and the OVP fault is notified through an interrupt. Though this fault condition rising is due to an incorrect hardware configuration is mitigated in the hardware, it still needs to be detected and handled. Add support for it.

When WLED module is enabled, keep OVP fault interrupt disabled for 10 ms to account for soft start delay.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  7 +-
 drivers/video/backlight/qcom-spmi-wled.c           | 83 ++++++++++++++++++++++
 2 files changed, 87 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -3
index 768608c..d39ee93 100644
@@ -92,7 +92,7 @@ The PMIC is connected to the host processor via SPMI bus.
 	Usage:      optional
 	Value type: <string>
 	Definition: Interrupt names associated with the interrupts.
-		    Must be "sc-irq".
+		    Currently supported interrupts are "sc-irq" and "ovp-irq".
 
 Example:
 
@@ -102,8 +102,9 @@ qcom-wled@d800 {
 	reg-names = "qcom-wled-ctrl-base", "qcom-wled-sink-base";
 	label = "backlight";
 
-	interrupts = <0x3 0xd8 0x2 IRQ_TYPE_EDGE_RISING>;
-	interrupt-names = "sc-irq";
+	interrupts = <0x3 0xd8 0x2 IRQ_TYPE_EDGE_RISING>,
+			<0x3 0xd8 0x1 IRQ_TYPE_EDGE_RISING>;
+	interrupt-names = "sc-irq", "ovp-irq";
 	qcom,fs-current-limit = <25000>;
 	qcom,current-boost-limit = <970>;
 	qcom,switching-freq = <800>;
drivers/video/backlight/qcom-spmi-wled.c +83 -0

134 lines not on this page · raw message

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#20 lore raw

PATCH 4/4 qcom: spmi-wled: Add auto-calibration logic support

In reply to Kiran Gunda

The auto-calibration algorithm checks if the current WLED sink configuration is valid. It tries enabling every sink and checks if the OVP fault is observed. Based on this information it detects and enables the valid sink configuration. Auto calibration will be triggered when the OVP fault interrupts are seen frequently thereby it tries to fix the sink configuration.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |   5 +
 drivers/video/backlight/qcom-spmi-wled.c           | 304 ++++++++++++++++++++-
 2 files changed, 306 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +5 -0
index d39ee93..f06c0cd 100644
@@ -94,6 +94,11 @@ The PMIC is connected to the host processor via SPMI bus.
 	Definition: Interrupt names associated with the interrupts.
 		    Currently supported interrupts are "sc-irq" and "ovp-irq".
 
+- qcom,auto-calibration
+	Usage:      optional
+	Value type: <bool>
+	Definition: Enables auto-calibration of the WLED sink configuration.
+
 Example:
 
 qcom-wled@d800 {
drivers/video/backlight/qcom-spmi-wled.c +301 -3

385 lines not on this page · raw message

Signature · 7 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#0 lore raw

cover qcom: spmi-wled: Support for QCOM wled driver

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight. This driver exposes two APIs to set and get the brightness of the display backlight through the backlight framework. This driver has the support to handle the OVP (over voltage protection) and the SC (short circuit protection) interrupts. It also has the auto calibration algorithm support to configure the right strings if the user specified string configuration is in-correct.

Kiran Gunda (4):
qcom: spmi-wled: Add support for qcom wled driver
qcom: spmi-wled: Add support for short circuit handling
qcom: spmi-wled: Add support for OVP interrupt handling
qcom: spmi-wled: Add auto-calibration logic support

 .../bindings/leds/backlight/qcom-spmi-wled.txt     | 118 +++
 drivers/video/backlight/Kconfig                    |   9 +
 drivers/video/backlight/Makefile                   |   1 +
 drivers/video/backlight/qcom-spmi-wled.c           | 999 +++++++++++++++++++++
 4 files changed, 1127 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
 create mode 100644 drivers/video/backlight/qcom-spmi-wled.c
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

In reply to Kiran Gunda
Quoted 3 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

Hi Kiran,

This driver has a lot in common with the already upstream pm8941-wled.c, because it's just a new revision of the same block.

Please extend the existing driver rather than providing a new one (and yes, renaming the file is okay).

Regards,
Bjorn

#3 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

Quoted 8 lines · Bjorn Andersson
Quoted 3 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

Hi Kiran,

Quoted 8 lines

This driver has a lot in common with the already upstream pm8941-wled.c, because it's just a new revision of the same block.

Please extend the existing driver rather than providing a new one (and yes, renaming the file is okay).

Regards,
Bjorn

Hi Bjorn,

Yes this driver design is similar to pm8941, however the WLED HW block has undergone quite a few changes in analog and digital from PM8941 to PM8998. Few of them include splitting one module into wled-ctrl and wled-sink peripherals, changes in the register offsets and the bit interpretation. Hence we concluded that it was better to have a new driver to support this new gen WELD module and decouple it from the pm8941. Also, going forward this driver will support AMOLED AVDD rail (not supported by pm8941) touching a few more registers/configuration and newer PMICs. So spinning off a new driver would make it cleaner and easier to extend further.

Thanks,
Kiran

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

Quoted 15 lines · On Thu 16 Nov 22:36 PST 2017, [email protected] wrote:
Quoted 14 lines · Bjorn Andersson
Quoted 3 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

Hi Kiran,

This driver has a lot in common with the already upstream pm8941-wled.c, because it's just a new revision of the same block.

Please extend the existing driver rather than providing a new one (and yes, renaming the file is okay).

Quoted 8 lines
Quoted 2 lines

Regards,
Bjorn

Hi Bjorn,

Yes this driver design is similar to pm8941, however the WLED HW block has undergone quite a few changes in analog and digital from PM8941 to PM8998.

I can see that, looking at the documentation.

Quoted 3 lines

Few of them include splitting one module into wled-ctrl and wled-sink peripherals, changes in the register offsets and the bit interpretation.

This is typical and something we need to handle in all these drivers, to avoid having one driver per platform.

Quoted 2 lines

Hence we concluded that it was better to have a new driver to support this new gen WELD module and decouple it from the pm8941.

Okay, I can see how it's easier to not have to case about anything but pmi8998 in this driver, but where do you add the support for other WLED versions? What about PMI8994? Will there not be similar differences (registers that has moved around) in the future?

Quoted 3 lines

Also, going forward this driver will support AMOLED AVDD rail (not supported by pm8941) touching a few more registers/configuration and newer PMICs.

Is this a feature that was introduced in PMI8998? Will this support not be dependent on the pmic version?

Quoted 3 lines

So spinning off a new driver would make it cleaner and easier to extend further.

It's for sure easier at this point in time, but your argumentation implies that PMI8998+1 should go into it's own driver as well.

I suspect that if you're going to reuse this driver for future PMIC versions you will have to deal with register layout differences and new feature set, and as such I'm not convinced that a new driver is needed.

Can you give any concrete examples of where it is not possible or undesirable to maintain the pm8941 support in the same driver?

Regards,
Bjorn

#5 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

Quoted 57 lines · Bjorn Andersson
Quoted 15 lines · On Thu 16 Nov 22:36 PST 2017, [email protected] wrote:
Quoted 14 lines · Bjorn Andersson
Quoted 3 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

Hi Kiran,

This driver has a lot in common with the already upstream pm8941-wled.c, because it's just a new revision of the same block.

Please extend the existing driver rather than providing a new one (and yes, renaming the file is okay).

Quoted 8 lines
Quoted 2 lines

Regards,
Bjorn

Hi Bjorn,

Yes this driver design is similar to pm8941, however the WLED HW block has undergone quite a few changes in analog and digital from PM8941 to PM8998.

I can see that, looking at the documentation.

Quoted 3 lines

Few of them include splitting one module into wled-ctrl and wled-sink peripherals, changes in the register offsets and the bit interpretation.

This is typical and something we need to handle in all these drivers, to avoid having one driver per platform.

Quoted 2 lines

Hence we concluded that it was better to have a new driver to support this new gen WELD module and decouple it from the pm8941.

Okay, I can see how it's easier to not have to case about anything but pmi8998 in this driver, but where do you add the support for other WLED versions? What about PMI8994? Will there not be similar differences (registers that has moved around) in the future?

Quoted 3 lines

Also, going forward this driver will support AMOLED AVDD rail (not supported by pm8941) touching a few more registers/configuration and newer PMICs.

Is this a feature that was introduced in PMI8998? Will this support not be dependent on the pmic version?

Quoted 3 lines

So spinning off a new driver would make it cleaner and easier to extend further.

It's for sure easier at this point in time, but your argumentation implies that PMI8998+1 should go into it's own driver as well.

Quoted 6 lines

I suspect that if you're going to reuse this driver for future PMIC versions you will have to deal with register layout differences and new feature set, and as such I'm not convinced that a new driver is needed.

Can you give any concrete examples of where it is not possible or undesirable to maintain the pm8941 support in the same driver?

I agree with Bjorn. If you can support multiple devices in a single driver with a couple of simple ddata struct differences and a slightly different regmap, you should.

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#7 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

In reply to Bjorn Andersson
Quoted 62 lines · Bjorn Andersson
Quoted 15 lines · On Thu 16 Nov 22:36 PST 2017, [email protected]…
Quoted 14 lines · Bjorn Andersson
Quoted 3 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

Hi Kiran,

This driver has a lot in common with the already upstream pm8941-wled.c, because it's just a new revision of the same block.

Please extend the existing driver rather than providing a new one (and yes, renaming the file is okay).

Quoted 8 lines
Quoted 2 lines

Regards,
Bjorn

Hi Bjorn,

Yes this driver design is similar to pm8941, however the WLED HW block has undergone quite a few changes in analog and digital from PM8941 to PM8998.

I can see that, looking at the documentation.

Quoted 3 lines

Few of them include splitting one module into wled-ctrl and wled-sink peripherals, changes in the register offsets and the bit interpretation.

This is typical and something we need to handle in all these drivers, to avoid having one driver per platform.

Quoted 2 lines

Hence we concluded that it was better to have a new driver to support this new gen WELD module and decouple it from the pm8941.

Okay, I can see how it's easier to not have to case about anything but pmi8998 in this driver, but where do you add the support for other WLED versions? What about PMI8994? Will there not be similar differences (registers that has moved around) in the future?

Quoted 3 lines

Also, going forward this driver will support AMOLED AVDD rail (not supported by pm8941) touching a few more registers/configuration and newer PMICs.

Is this a feature that was introduced in PMI8998? Will this support not be dependent on the pmic version?

Quoted 3 lines

So spinning off a new driver would make it cleaner and easier to extend further.

It's for sure easier at this point in time, but your argumentation implies that PMI8998+1 should go into it's own driver as well.

I suspect that if you're going to reuse this driver for future PMIC versions you will have to deal with register layout differences and new feature set, and as such I'm not convinced that a new driver is needed.

Quoted 5 lines

Can you give any concrete examples of where it is not possible or undesirable to maintain the pm8941 support in the same driver?

Regards,
Bjorn

Hi Bjorn,

Thanks for the inputs! Following are the reasons to go for the new driver and this driver can support 5 PMICs.

1.Majority of register, offsets and config values don’t match up between
PMI8998 and PM8941
2.Feature such as – SC protection handling in SW cannot be done for 8941 as there is no SC event/irq, AMOELD AVDD cannot supported by PM8941 3.Feature such as – string auto-calibration even if common will have to use different offsets/registers in the same SW logic
4.PMI8998, PMI8994, PMI8950 and PM660 all of them have this same WLED module (and register map) with very minor changes unlike 8941.

Thanks,
Kiran

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

In reply to Lee Jones
Quoted 63 lines · Lee Jones
Quoted 61 lines · Bjorn Andersson
Quoted 15 lines · On Thu 16 Nov 22:36 PST 2017, [email protected] wrote:
Quoted 14 lines · Bjorn Andersson
Quoted 3 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

Hi Kiran,

This driver has a lot in common with the already upstream pm8941-wled.c, because it's just a new revision of the same block.

Please extend the existing driver rather than providing a new one (and yes, renaming the file is okay).

Quoted 8 lines
Quoted 2 lines

Regards,
Bjorn

Hi Bjorn,

Yes this driver design is similar to pm8941, however the WLED HW block has undergone quite a few changes in analog and digital from PM8941 to PM8998.

I can see that, looking at the documentation.

Quoted 3 lines

Few of them include splitting one module into wled-ctrl and wled-sink peripherals, changes in the register offsets and the bit interpretation.

This is typical and something we need to handle in all these drivers, to avoid having one driver per platform.

Quoted 2 lines

Hence we concluded that it was better to have a new driver to support this new gen WELD module and decouple it from the pm8941.

Okay, I can see how it's easier to not have to case about anything but pmi8998 in this driver, but where do you add the support for other WLED versions? What about PMI8994? Will there not be similar differences (registers that has moved around) in the future?

Quoted 3 lines

Also, going forward this driver will support AMOLED AVDD rail (not supported by pm8941) touching a few more registers/configuration and newer PMICs.

Is this a feature that was introduced in PMI8998? Will this support not be dependent on the pmic version?

Quoted 3 lines

So spinning off a new driver would make it cleaner and easier to extend further.

It's for sure easier at this point in time, but your argumentation implies that PMI8998+1 should go into it's own driver as well.

I suspect that if you're going to reuse this driver for future PMIC versions you will have to deal with register layout differences and new feature set, and as such I'm not convinced that a new driver is needed.

Quoted 6 lines
Quoted 2 lines

Can you give any concrete examples of where it is not possible or undesirable to maintain the pm8941 support in the same driver?

I agree with Bjorn. If you can support multiple devices in a single driver with a couple of simple ddata struct differences and a slightly different regmap, you should.

Hi Lee,
Thanks for the feedback! The regmap difference is not confined to couple of registers. Except the one register all the registers have some difference in offset or bitmap or config values as compared to the pm8941. Below is the table for the reference. The below table covers only the registers those exist in the pm8941 driver, if we keep adding the other features the changes may be huge. Apart from this I have mentioned other feature differences between pm8941 and pm8998 as well in response to Bjorn. Please refer that as well.

Register Compatibility between 8998 Vs 8941
======== ===================================================
WLED_MODULE_ENABLE Register address offset is same and config values match
WLED1_ILED_SYNC_BIT Register address offset and config values not matching.
WLED1_SWITCHING_FREQUENCY Register address offset and config values are matching.
But there is an extra override bit in
pm8998.
WLED1_WLED_OVP Register address offset same. But config values are not matching.
WLED1_WLED_ILIM Register address offset is same. But config values are not matching
WLED1_EN_CURRENT_SINK Both register address offset and config values are not matching.

WLED1_LED1_MODULATOR_EN	       Both register address offset and config values are not matching.
WLED1_LED1_FULL_SCALE_CURRENT  Both register address offset and config values are not matching.
WLED1_LED1_MODULATOR_SRC_SE    Register address offset not matching, but the config values are matching

WLED1_LED1_CABC_EN Register address offset not matching, but the config values are not matching.

Thanks,
Kiran

maintainer
#8 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

In reply to Kiran Gunda
Quoted 6 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  90 ++++

Please split bindings to separate patch.

Quoted 6 lines
 drivers/video/backlight/Kconfig                    |   9 +
 drivers/video/backlight/Makefile                   |   1 +
 drivers/video/backlight/qcom-spmi-wled.c           | 504 +++++++++++++++++++++
 4 files changed, 604 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
 create mode 100644 drivers/video/backlight/qcom-spmi-wled.c
maintainer
#13 lore raw

Re: [PATCH V1 2/4] qcom: spmi-wled: Add support for short circuit handling

In reply to Kiran Gunda
Quoted 7 lines · Kiran Gunda

Handle the short circuit(SC) interrupt and check if the SC interrupt is valid. Re-enable the module to check if it goes away. Disable the module altogether if the SC event persists.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  22 ++++

... and also put all the binding in one patch. I want to review the full view of the h/w, not piecemeal.

Quoted 2 lines
 drivers/video/backlight/qcom-spmi-wled.c           | 126 ++++++++++++++++++++-
 2 files changed, 142 insertions(+), 6 deletions(-)

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#14 lore raw

Re: [PATCH V1 2/4] qcom: spmi-wled: Add support for short circuit handling

Quoted 5 lines · Rob Herring
Quoted 4 lines · Kiran Gunda

Handle the short circuit(SC) interrupt and check if the SC interrupt is valid. Re-enable the module to check if it goes away. Disable the module altogether if the SC event persists.

Quoted 7 lines
Quoted 3 lines
---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  22 ++++

... and also put all the binding in one patch. I want to review the full view of the h/w, not piecemeal.

Sure. Will send the next series with the changes suggested.

Quoted 3 lines
Quoted 3 lines
 drivers/video/backlight/qcom-spmi-wled.c           | 126 
++++++++++++++++++++-
 2 files changed, 142 insertions(+), 6 deletions(-)
#9 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

In reply to Kiran Gunda
Quoted 19 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  90 ++++
 drivers/video/backlight/Kconfig                    |   9 +
 drivers/video/backlight/Makefile                   |   1 +
 drivers/video/backlight/qcom-spmi-wled.c           | 504 +++++++++++++++++++++
 4 files changed, 604 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
 create mode 100644 drivers/video/backlight/qcom-spmi-wled.c
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +0 -0
new file mode 100644
index 0000000..f1ea25b

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 2

Quoted 2 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +2 -0
@@ -0,0 +1,90 @@
+Binding for Qualcomm WLED driver
+

This binding document quite well describe the pm8941 as well, so please improve the existing binding (changing to this style is preferable).

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 10

Quoted 8 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +8 -0
+WLED (White Light Emitting Diode) driver is used for controlling display
+backlight that is part of PMIC on Qualcomm Technologies reference platforms.
+The PMIC is connected to the host processor via SPMI bus.
+
+- compatible
+	Usage:      required
+	Value type: <string>
+	Definition: should be "qcom,pm8998-spmi-wled".

There's no WLED in the pm8998, so please make this pmi8998. This pmic is SPMI only, so there's no need to keep "spmi" in the compatible.

Quoted 18 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +18 -0
+
+- reg
+	Usage:      required
+	Value type: <prop-encoded-array>
+	Definition:  Base address and size of the WLED modules.
+
+- reg-names
+	Usage:      required
+	Value type: <string>
+	Definition:  Names associated with base addresses. should be
+		     "qcom-wled-ctrl-base", "qcom-wled-sink-base".
+
+- label
+	Usage:      required
+	Value type: <string>
+	Definition: The name of the backlight device.
+
+- default-brightness

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 38

Quoted 10 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +10 -0
+	Usage:      optional
+	Value type: <u32>
+	Definition: brightness value on boot, value from: 0-4095
+		    default: 2048
+
+- qcom,fs-current-limit
+	Usage:      optional
+	Value type: <u32>
+	Definition: per-string full scale current limit in uA. value from
+		    0 to 30000 with 5000 uA resolution. default: 25000 uA

"in steps of 5mA"

Quoted 21 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +21 -0
+
+- qcom,current-boost-limit
+	Usage:      optional
+	Value type: <u32>
+	Definition: ILIM threshold in mA. values are 105, 280, 450, 620, 970,
+		    1150, 1300, 1500. default: 970 mA
+
+- qcom,switching-freq
+	Usage:      optional
+	Value type: <u32>
+	Definition: Switching frequency in KHz. values are
+		    600, 640, 685, 738, 800, 872, 960, 1066, 1200, 1371,
+		    1600, 1920, 2400, 3200, 4800, 9600.
+		    default: 800 KHz
+
+- qcom,ovp
+	Usage:      optional
+	Value type: <u32>
+	Definition: Over-voltage protection limit in mV. values are 31100,
+		    29600, 19600, 18100.
+	            default: 29600 mV

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 69

Quoted 10 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +10 -0
+
+- qcom,string-cfg
+	Usage:      optional
+	Value type: <u32>
+	Definition: Bit mask of the wled strings. Bit 0 to 3 indicates strings
+		    0 to 3 respectively. Wled module has four strings of leds
+		    numbered from 0 to 3. Each string of leds are operated
+		    individually. Specify the strings using the bit mask. Any
+		    combination of led strings can be used.
+		    default value is 15 (b1111).

Please try to avoid expressing things as bitmasks in DT.

The only difference from 8941 here is that there's one additional string, so please start off by expressing this as the existing binding.

If you really need this flexibility you can follow up with an addition of a property like this, but name it something like "qcom,enabled-strings" and make this support available for pm8941 as well.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 71

Quoted 2 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +2 -0
+
+- qcom,en-cabc

No need for the "en", the presence of a bool property means that it's enabled.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 75

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Specify if cabc (content adaptive backlight control) is
+		    needed.

I presume cabc isn't ever "needed", just make the description "Enable content adaptive backlight control".

Quoted 15 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +15 -0
+
+Example:
+
+qcom-wled@d800 {
+	compatible = "qcom,pm8998-spmi-wled";
+	reg = <0xd800 0xd900>;
+	reg-names = "qcom-wled-ctrl-base", "qcom-wled-sink-base";
+	label = "backlight";
+
+	qcom,fs-current-limit = <25000>;
+	qcom,current-boost-limit = <970>;
+	qcom,switching-freq = <800>;
+	qcom,ovp = <29600>;
+	qcom,string-cfg = <15>;
+};

[..]

drivers/video/backlight/qcom-spmi-wled.c

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +0 -0

After reviewing your arguments and comparing the drivers I still think it's beneficial to support both these hardware revisions in the same driver.

The majority of the register differences relates to the current sink being split out, but this can easily be handled by a few well places accessor functions - which depends on this being the case or not.

The addition of OVP handling would benefit 8941 as well.

The short circuit handling in your patches are isolated and not taking this code path on 8941 should not pose any problems.

[..]

Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +12 -0
+/* General definitions */
+#define QCOM_WLED_DEFAULT_BRIGHTNESS		2048
+#define  QCOM_WLED_MAX_BRIGHTNESS		4095
+
+/* WLED control registers */
+#define QCOM_WLED_CTRL_MOD_ENABLE		0x46
+#define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
+#define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
+
+#define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
+#define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
+

drivers/video/backlight/qcom-spmi-wled.c line 43

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+#define QCOM_WLED_CTRL_OVP			0x4d
+#define  QCOM_WLED_CTRL_OVP_MASK		GENMASK(1, 0)
+
+#define QCOM_WLED_CTRL_ILIM			0x4e
+#define  QCOM_WLED_CTRL_ILIM_MASK		GENMASK(2, 0)
+
+/* WLED sink registers */
+#define QCOM_WLED_SINK_CURR_SINK_EN		0x46
+#define  QCOM_WLED_SINK_CURR_SINK_MASK		GENMASK(7, 4)
+#define  QCOM_WLED_SINK_CURR_SINK_SHFT		0x04

Shifts are typically not given as hex...

Quoted 26 lines
drivers/video/backlight/qcom-spmi-wled.c +26 -0
+
+#define QCOM_WLED_SINK_SYNC			0x47
+#define  QCOM_WLED_SINK_SYNC_MASK		GENMASK(3, 0)
+#define  QCOM_WLED_SINK_SYNC_LED1		BIT(0)
+#define  QCOM_WLED_SINK_SYNC_LED2		BIT(1)
+#define  QCOM_WLED_SINK_SYNC_LED3		BIT(2)
+#define  QCOM_WLED_SINK_SYNC_LED4		BIT(3)
+#define  QCOM_WLED_SINK_SYNC_CLEAR		0x00
+
+#define QCOM_WLED_SINK_MOD_EN_REG(n)		(0x50 + (n * 0x10))
+#define  QCOM_WLED_SINK_REG_STR_MOD_MASK	BIT(7)
+#define  QCOM_WLED_SINK_REG_STR_MOD_EN		BIT(7)
+
+#define QCOM_WLED_SINK_SYNC_DLY_REG(n)		(0x51 + (n * 0x10))
+#define QCOM_WLED_SINK_FS_CURR_REG(n)		(0x52 + (n * 0x10))
+#define  QCOM_WLED_SINK_FS_MASK			GENMASK(3, 0)
+
+#define QCOM_WLED_SINK_CABC_REG(n)		(0x56 + (n * 0x10))
+#define  QCOM_WLED_SINK_CABC_MASK		BIT(7)
+#define  QCOM_WLED_SINK_CABC_EN			BIT(7)
+
+#define QCOM_WLED_SINK_BRIGHT_LSB_REG(n)	(0x57 + (n * 0x10))
+#define QCOM_WLED_SINK_BRIGHT_MSB_REG(n)	(0x58 + (n * 0x10))
+
+struct qcom_wled_config {
+	u32 i_boost_limit;

drivers/video/backlight/qcom-spmi-wled.c line 79

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	u32 ovp;
+	u32 switch_freq;
+	u32 fs_current;
+	u32 string_cfg;
+	bool en_cabc;
+};
+
+struct qcom_wled {
+	const char *name;
+	struct platform_device *pdev;

Lug around the struct device * instead of the platform_device, and use this for dev_* prints throughout the code.

drivers/video/backlight/qcom-spmi-wled.c line 84

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+	struct regmap *regmap;
+	u16 sink_addr;
+	u16 ctrl_addr;
+	u32 brightness;
+	bool prev_state;

You can derive prev_state from wled->brightness in qcom_wled_update_status().

drivers/video/backlight/qcom-spmi-wled.c line 95

Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +11 -0
+
+	struct qcom_wled_config cfg;
+};
+
+static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
+{
+	int rc;
+
+	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
+			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);

This shift obfuscate the fact that val is only 0 or 1, make val a bool and make the macro for the enabled state be BIT(7).

Quoted 27 lines
drivers/video/backlight/qcom-spmi-wled.c +27 -0
+	return rc;
+}
+
+static int qcom_wled_get_brightness(struct backlight_device *bl)
+{
+	struct qcom_wled *wled = bl_get_data(bl);
+
+	return wled->brightness;
+}
+
+static int qcom_wled_sync_toggle(struct qcom_wled *wled)
+{
+	int rc;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_SYNC,
+			QCOM_WLED_SINK_SYNC_MASK, QCOM_WLED_SINK_SYNC_MASK);
+	if (rc < 0)
+		return rc;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_SYNC,
+			QCOM_WLED_SINK_SYNC_MASK, QCOM_WLED_SINK_SYNC_CLEAR);
+
+	return rc;
+}
+

drivers/video/backlight/qcom-spmi-wled.c line 132

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness)
+{
+	int rc, i;
+	u16 low_limit = QCOM_WLED_MAX_BRIGHTNESS * 4 / 1000;
+	u8 string_cfg = wled->cfg.string_cfg;
+	u8 v[2];
+
+	/* WLED's lower limit of operation is 0.4% */
+	if (brightness > 0 && brightness < low_limit)
+		brightness = low_limit;

What happens between 0 and 0.4%? Is this policy or is this related to some hardware issue?

Also, this function will not be called with brightness = 0, so you don't need to check that case.

drivers/video/backlight/qcom-spmi-wled.c line 137

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+
+	v[0] = brightness & 0xff;
+	v[1] = (brightness >> 8) & 0xf;
+
+	for (i = 0; (string_cfg >> i) != 0; i++) {

The condition looks optimal... Just loop from 0 to 3 and it will be easier to read without any measurable losses.

drivers/video/backlight/qcom-spmi-wled.c

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +1 -0
+		if (string_cfg & BIT(i)) {

Flip this condition around and use "continue" to reduce the indentation level of the rest of the block.

Quoted 15 lines
drivers/video/backlight/qcom-spmi-wled.c +15 -0
+			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
+			if (rc < 0)
+				return rc;
+		}
+	}
+
+	return 0;
+}
+
+static int qcom_wled_update_status(struct backlight_device *bl)
+{
+	struct qcom_wled *wled = bl_get_data(bl);
+	u16 brightness = bl->props.brightness;
+	int rc;

drivers/video/backlight/qcom-spmi-wled.c line 163

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+
+	if (bl->props.power != FB_BLANK_UNBLANK ||
+	    bl->props.fb_blank != FB_BLANK_UNBLANK ||
+	    bl->props.state & BL_CORE_FBBLANK)
+		brightness = 0;
+
+	if (brightness) {
+		rc = qcom_wled_set_brightness(wled, brightness);
+		if (rc < 0) {
+			pr_err("wled failed to set brightness rc:%d\n", rc);

Use dev_err() and dev_dbg() throughout the driver.

drivers/video/backlight/qcom-spmi-wled.c line 173

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			return rc;
+		}
+
+		if (!!brightness != wled->prev_state) {
+			rc = qcom_wled_module_enable(wled, !!brightness);
+			if (rc < 0) {
+				pr_err("wled enable failed rc:%d\n", rc);
+				return rc;
+			}
+		}

This block is exactly the same as the else statement, there's no need to repeat yourself.

Quoted 37 lines
drivers/video/backlight/qcom-spmi-wled.c +37 -0
+	} else {
+		rc = qcom_wled_module_enable(wled, brightness);
+		if (rc < 0) {
+			pr_err("wled disable failed rc:%d\n", rc);
+			return rc;
+		}
+	}
+
+	wled->prev_state = !!brightness;
+
+	rc = qcom_wled_sync_toggle(wled);
+	if (rc < 0) {
+		pr_err("wled sync failed rc:%d\n", rc);
+		return rc;
+	}
+
+	wled->brightness = brightness;
+
+	return rc;
+}
+
+static int qcom_wled_setup(struct qcom_wled *wled)
+{
+	int rc, temp, i;
+	u8 sink_en = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_OVP,
+			QCOM_WLED_CTRL_OVP_MASK, wled->cfg.ovp);
+	if (rc < 0)
+		return rc;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_ILIM,
+			QCOM_WLED_CTRL_ILIM_MASK, wled->cfg.i_boost_limit);
+	if (rc < 0)

drivers/video/backlight/qcom-spmi-wled.c line 220

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+		return rc;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_SWITCH_FREQ,
+			QCOM_WLED_CTRL_SWITCH_FREQ_MASK, wled->cfg.switch_freq);
+	if (rc < 0)
+		return rc;
+
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (string_cfg & BIT(i)) {

Same as above.

Quoted 18 lines
drivers/video/backlight/qcom-spmi-wled.c +18 -0
+			u16 addr = wled->sink_addr +
+					QCOM_WLED_SINK_MOD_EN_REG(i);
+
+			rc = regmap_update_bits(wled->regmap, addr,
+					QCOM_WLED_SINK_REG_STR_MOD_MASK,
+					QCOM_WLED_SINK_REG_STR_MOD_EN);
+			if (rc < 0)
+				return rc;
+
+			addr = wled->sink_addr +
+					QCOM_WLED_SINK_FS_CURR_REG(i);
+			rc = regmap_update_bits(wled->regmap, addr,
+					QCOM_WLED_SINK_FS_MASK,
+					wled->cfg.fs_current);
+			if (rc < 0)
+				return rc;
+
+			addr = wled->sink_addr +

drivers/video/backlight/qcom-spmi-wled.c line 248

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+					QCOM_WLED_SINK_CABC_REG(i);
+			rc = regmap_update_bits(wled->regmap, addr,
+					QCOM_WLED_SINK_CABC_MASK,
+					wled->cfg.en_cabc ?
+					QCOM_WLED_SINK_CABC_EN : 0);
+			if (rc)
+				return rc;
+
+			temp = i + QCOM_WLED_SINK_CURR_SINK_SHFT;
+			sink_en |= 1 << temp;

I'm failing to see the reason for the "temp" variable here. Please do:

sink_en |= BIT(i + QCOM_WLED_SINK_CURR_SINK_SHFT)

Quoted 18 lines
drivers/video/backlight/qcom-spmi-wled.c +18 -0
+		}
+	}
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			QCOM_WLED_SINK_CURR_SINK_MASK, sink_en);
+	if (rc < 0)
+		return rc;
+
+	rc = qcom_wled_sync_toggle(wled);
+	if (rc < 0) {
+		pr_err("Failed to toggle sync reg rc:%d\n", rc);
+		return rc;
+	}
+
+	return 0;
+}
+

[..]

drivers/video/backlight/qcom-spmi-wled.c line 346

Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +12 -0
+static int qcom_wled_configure(struct qcom_wled *wled, struct device *dev)
+{
+	struct qcom_wled_config *cfg = &wled->cfg;
+	const __be32 *prop_addr;
+	u32 val, c;
+	int rc, i, j;
+
+	const struct {
+		const char *name;
+		u32 *val_ptr;
+		const struct qcom_wled_var_cfg *cfg;
+	} u32_opts[] = {

I suggest that you tie this list of options to the compatible (through of_device_id->data) and pass it as a parameter to this function. That way you can handle variation in properties and their values between different compatibles.

[..]

drivers/video/backlight/qcom-spmi-wled.c line 400

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	*cfg = wled_config_defaults;
+	for (i = 0; i < ARRAY_SIZE(u32_opts); ++i) {
+		rc = of_property_read_u32(dev->of_node, u32_opts[i].name, &val);

of_property_read_u32() returns -ENODATA when there's no associated data, you can probably use this to implement support for the boolean types in the same list of opts.

[..]

Quoted 30 lines
drivers/video/backlight/qcom-spmi-wled.c +30 -0
+	}
+
+	for (i = 0; i < ARRAY_SIZE(bool_opts); ++i) {
+		if (of_property_read_bool(dev->of_node, bool_opts[i].name))
+			*bool_opts[i].val_ptr = true;
+	}
+
+	return 0;
+}
+
+static const struct backlight_ops qcom_wled_ops = {
+	.update_status = qcom_wled_update_status,
+	.get_brightness = qcom_wled_get_brightness,
+};
+
+static int qcom_wled_probe(struct platform_device *pdev)
+{
+	struct backlight_properties props;
+	struct backlight_device *bl;
+	struct qcom_wled *wled;
+	struct regmap *regmap;
+	u32 val;
+	int rc;
+
+	regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!regmap) {
+		pr_err("Unable to get regmap\n");
+		return -EINVAL;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c line 462

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	wled = devm_kzalloc(&pdev->dev, sizeof(*wled), GFP_KERNEL);
+	if (!wled)
+		return -ENOMEM;
+
+	wled->regmap = regmap;
+	wled->pdev = pdev;
+
+	rc = qcom_wled_configure(wled, &pdev->dev);
+	if (rc < 0) {
+		pr_err("wled configure failed rc:%d\n", rc);

qcom_wled_configure() already printed an error message for you, no need to repeat this.

drivers/video/backlight/qcom-spmi-wled.c line 98

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+		return rc;
+	}
+

Please also run checkpatch.pl with the --strict option and fix the indentation issues reported.

Regards,
Bjorn

#15 lore raw

Re: [PATCH V1 2/4] qcom: spmi-wled: Add support for short circuit handling

In reply to Kiran Gunda
Quoted 15 lines · Kiran Gunda

Handle the short circuit(SC) interrupt and check if the SC interrupt is valid. Re-enable the module to check if it goes away. Disable the module altogether if the SC event persists.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  22 ++++
 drivers/video/backlight/qcom-spmi-wled.c           | 126 ++++++++++++++++++++-
 2 files changed, 142 insertions(+), 6 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
index f1ea25b..768608c 100644

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt The PMIC is connected to the host processor via SPMI bus. · line 77

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +1 -0
@@ -74,6 +74,26 @@ The PMIC is connected to the host processor via SPMI bus.
 	Definition: Specify if cabc (content adaptive backlight control) is
 		    needed.
 
+- qcom,ext-pfet-sc-pro-en

Please use readable names, rather than a bunch of abbreviations.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt The PMIC is connected to the host processor via SPMI bus. · line 81

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Specify if external PFET control for short circuit
+		    protection is needed.

What does this mean? At least change the wording to "...protection is used".

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+
+- interrupts
+	Usage:      optional
+	Value type: <prop encoded array>

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt The PMIC is connected to the host processor via SPMI bus. · line 95

Quoted 10 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +10 -0
+	Definition: Interrupts associated with WLED. Interrupts can be
+		    specified as per the encoding listed under
+		    Documentation/devicetree/bindings/spmi/
+		    qcom,spmi-pmic-arb.txt.
+
+- interrupt-names
+	Usage:      optional
+	Value type: <string>
+	Definition: Interrupt names associated with the interrupts.
+		    Must be "sc-irq".

This is obviously an irq, so no need to include that in the name. I would also prefer if you use the name "short" to make this easier to read.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt qcom-wled@d800 { · line 105

Quoted 9 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +2 -0
+
 Example:
 
 qcom-wled@d800 {
@@ -82,6 +102,8 @@ qcom-wled@d800 {
 	reg-names = "qcom-wled-ctrl-base", "qcom-wled-sink-base";
 	label = "backlight";
 
+	interrupts = <0x3 0xd8 0x2 IRQ_TYPE_EDGE_RISING>;

We tend to write these on the form <decimal, hex, decimal, enum>, please follow this.

Quoted 19 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +1 -0
+	interrupt-names = "sc-irq";
 	qcom,fs-current-limit = <25000>;
 	qcom,current-boost-limit = <970>;
 	qcom,switching-freq = <800>;
drivers/video/backlight/qcom-spmi-wled.c +3 -0
index 14c3adc..7dbaaa7 100644
@@ -11,6 +11,9 @@
  * GNU General Public License for more details.
  */
 
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+#include <linux/ktime.h>
 #include <linux/kernel.h>
 #include <linux/backlight.h>
 #include <linux/module.h>

drivers/video/backlight/qcom-spmi-wled.c line 31

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
@@ -23,7 +26,13 @@
 #define QCOM_WLED_DEFAULT_BRIGHTNESS		2048
 #define  QCOM_WLED_MAX_BRIGHTNESS		4095
 
+#define QCOM_WLED_SC_DLY_MS			20
+#define QCOM_WLED_SC_CNT_MAX			5
+#define QCOM_WLED_SC_RESET_CNT_DLY_US		1000000

With times of this ballpark you can just use jiffies, with this just being HZ.

Quoted 24 lines
drivers/video/backlight/qcom-spmi-wled.c +12 -0
+
 /* WLED control registers */
+#define QCOM_WLED_CTRL_FAULT_STATUS		0x08
+
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
@@ -37,6 +46,15 @@
 #define QCOM_WLED_CTRL_ILIM			0x4e
 #define  QCOM_WLED_CTRL_ILIM_MASK		GENMASK(2, 0)
 
+#define QCOM_WLED_CTRL_SHORT_PROTECT		0x5e
+#define  QCOM_WLED_CTRL_SHORT_EN_MASK		BIT(7)
+
+#define QCOM_WLED_CTRL_SEC_ACCESS		0xd0
+#define  QCOM_WLED_CTRL_SEC_UNLOCK		0xa5
+
+#define QCOM_WLED_CTRL_TEST1			0xe2
+#define  QCOM_WLED_EXT_FET_DTEST2		0x09
+
 /* WLED sink registers */
 #define QCOM_WLED_SINK_CURR_SINK_EN		0x46
 #define  QCOM_WLED_SINK_CURR_SINK_MASK		GENMASK(7, 4)

drivers/video/backlight/qcom-spmi-wled.c struct qcom_wled_config { · line 92

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
@@ -71,19 +89,23 @@ struct qcom_wled_config {
 	u32 switch_freq;
 	u32 fs_current;
 	u32 string_cfg;
+	int sc_irq;

Keep data parsed directly from DT in the config and move this to qcom_wled.

drivers/video/backlight/qcom-spmi-wled.c struct qcom_wled_config { · line 94

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 	bool en_cabc;
+	bool ext_pfet_sc_pro_en;

This name is long and hard to parse. "external_pfet" would be much easier to read.

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -0
 };
 
 struct qcom_wled {
 	const char *name;
 	struct platform_device *pdev;
 	struct regmap *regmap;

drivers/video/backlight/qcom-spmi-wled.c struct qcom_wled_config { · line 86

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -2
+	struct mutex lock;
+	struct qcom_wled_config cfg;
+	ktime_t last_sc_event_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
 	u32 brightness;
+	u32 sc_count;
 	bool prev_state;
-
-	struct qcom_wled_config cfg;

Moving this seems unnecessary.

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_update_status(struct backlight_device *bl) · line 182

Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
@@ -157,25 +179,26 @@ static int qcom_wled_update_status(struct backlight_device *bl)
 	    bl->props.state & BL_CORE_FBBLANK)
 		brightness = 0;
 
+	mutex_lock(&wled->lock);

Is this lock necessary?

Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +7 -0
+static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled)
+{
+	struct qcom_wled *wled = _wled;
+	int rc;
+	u32 val;
+	s64 elapsed_time;
+

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_update_status(struct backlight_device *bl) · line 236

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_read(wled->regmap,
+		wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS, &val);
+	if (rc < 0) {
+		pr_err("Error in reading WLED_FAULT_STATUS rc=%d\n", rc);
+		return IRQ_HANDLED;
+	}
+
+	wled->sc_count++;
+	pr_err("WLED short circuit detected %d times fault_status=%x\n",
+		wled->sc_count, val);

Who will read this and is it worth the extra read of FAULT_STATUS just to produce this print?

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_update_status(struct backlight_device *bl) · line 248

Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +12 -0
+	mutex_lock(&wled->lock);
+	rc = qcom_wled_module_enable(wled, false);
+	if (rc < 0) {
+		pr_err("wled disable failed rc:%d\n", rc);
+		goto unlock_mutex;
+	}
+
+	elapsed_time = ktime_us_delta(ktime_get(),
+				wled->last_sc_event_time);
+	if (elapsed_time > QCOM_WLED_SC_RESET_CNT_DLY_US) {
+		wled->sc_count = 0;
+	} else if (wled->sc_count > QCOM_WLED_SC_CNT_MAX) {

This isn't really "else elapsed_time was more than DLY_US". Split this into:

if (elapsed_time > xyz)

	wled->sc_count = 0;

if (wled->sc_count > QCOM_WLED_SC_CNT_MAX)

	...

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_update_status(struct backlight_device *bl) · line 249

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +1 -0
+		pr_err("SC trigged %d times, disabling WLED forever!\n",

"forever" as in "until someone turns it on again"?

Quoted 27 lines
drivers/video/backlight/qcom-spmi-wled.c +18 -0
+			wled->sc_count);
+		goto unlock_mutex;
+	}
+
+	wled->last_sc_event_time = ktime_get();
+
+	msleep(QCOM_WLED_SC_DLY_MS);
+	rc = qcom_wled_module_enable(wled, true);
+	if (rc < 0)
+		pr_err("wled enable failed rc:%d\n", rc);
+
+unlock_mutex:
+	mutex_unlock(&wled->lock);
+
+	return IRQ_HANDLED;
+}
+
 static int qcom_wled_setup(struct qcom_wled *wled)
 {
 	int rc, temp, i;
 	u8 sink_en = 0;
 	u8 string_cfg = wled->cfg.string_cfg;
+	int sc_irq = wled->cfg.sc_irq;
 
 	rc = regmap_update_bits(wled->regmap,
 			wled->ctrl_addr + QCOM_WLED_CTRL_OVP,

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 337

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
@@ -261,6 +334,39 @@ static int qcom_wled_setup(struct qcom_wled *wled)
 		return rc;
 	}
 
+	if (sc_irq >= 0) {

I think things will be cleaner if you let qcom_wled_setup() configure the hardware based on the wled->cfg (as is done to this point) and then deal with the interrupts in a separate code path from the probe function.

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 343

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		rc = devm_request_threaded_irq(&wled->pdev->dev, sc_irq,
+				NULL, qcom_wled_sc_irq_handler, IRQF_ONESHOT,
+				"qcom_wled_sc_irq", wled);
+		if (rc < 0) {
+			pr_err("Unable to request sc(%d) IRQ(err:%d)\n",
+				sc_irq, rc);

sc_irq is just a number without meaning, no need to print it.

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 355

Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +12 -0
+			return rc;
+		}
+
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_SHORT_PROTECT,
+				QCOM_WLED_CTRL_SHORT_EN_MASK,
+				QCOM_WLED_CTRL_SHORT_EN_MASK);
+		if (rc < 0)
+			return rc;
+
+		if (wled->cfg.ext_pfet_sc_pro_en) {
+			/* unlock the secure access regisetr */

Spelling of register, and this operation does "Unlock the secure register access" it doesn't unlock the secure access register.

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 364

Quoted 9 lines
drivers/video/backlight/qcom-spmi-wled.c +9 -0
+			rc = regmap_write(wled->regmap, wled->ctrl_addr +
+					QCOM_WLED_CTRL_SEC_ACCESS,
+					QCOM_WLED_CTRL_SEC_UNLOCK);
+			if (rc < 0)
+				return rc;
+
+			rc = regmap_write(wled->regmap,
+					wled->ctrl_addr + QCOM_WLED_CTRL_TEST1,
+					QCOM_WLED_EXT_FET_DTEST2);

What is the relationship between DTEST2 and the external FET?

Quoted 25 lines
drivers/video/backlight/qcom-spmi-wled.c +7 -0
+			if (rc < 0)
+				return rc;
+		}
+	}
+
 	return 0;
 }
 
@@ -271,6 +377,7 @@ static int qcom_wled_setup(struct qcom_wled *wled)
 	.switch_freq = 11,
 	.string_cfg = 0xf,
 	.en_cabc = 0,
+	.ext_pfet_sc_pro_en = 1,
 };
 
 struct qcom_wled_var_cfg {
@@ -376,6 +483,7 @@ static int qcom_wled_configure(struct qcom_wled *wled, struct device *dev)
 		bool *val_ptr;
 	} bool_opts[] = {
 		{ "qcom,en-cabc", &cfg->en_cabc, },
+		{ "qcom,ext-pfet-sc-pro", &cfg->ext_pfet_sc_pro_en, },
 	};
 
 	prop_addr = of_get_address(dev->of_node, 0, NULL, NULL);

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_configure(struct qcom_wled *wled, struct device *dev) · line 541

Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
@@ -427,6 +535,10 @@ static int qcom_wled_configure(struct qcom_wled *wled, struct device *dev)
 			*bool_opts[i].val_ptr = true;
 	}
 
+	wled->cfg.sc_irq = platform_get_irq_byname(wled->pdev, "sc-irq");
+	if (wled->cfg.sc_irq < 0)
+		dev_dbg(&wled->pdev->dev, "sc irq is not used\n");
+

Move this to qcom_wled_probe() or into its own code path, together with the rest of the sc_irq initialization.

And as you're not enabling or disabling it you can store it in a local variable.

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 372

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	return 0;
 }
 

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#18 lore raw

Re: [PATCH V1 3/4] qcom: spmi-wled: Add support for OVP interrupt handling

In reply to Kiran Gunda
Quoted 19 lines · Kiran Gunda

WLED peripheral has over voltage protection(OVP) circuitry and the OVP fault is notified through an interrupt. Though this fault condition rising is due to an incorrect hardware configuration is mitigated in the hardware, it still needs to be detected and handled. Add support for it.

When WLED module is enabled, keep OVP fault interrupt disabled for 10 ms to account for soft start delay.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  7 +-
 drivers/video/backlight/qcom-spmi-wled.c           | 83 ++++++++++++++++++++++
 2 files changed, 87 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
index 768608c..d39ee93 100644

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt The PMIC is connected to the host processor via SPMI bus. · line 96

Quoted 6 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +1 -1
@@ -92,7 +92,7 @@ The PMIC is connected to the host processor via SPMI bus.
 	Usage:      optional
 	Value type: <string>
 	Definition: Interrupt names associated with the interrupts.
-		    Must be "sc-irq".
+		    Currently supported interrupts are "sc-irq" and "ovp-irq".
 

As before, we know this is an IRQ, so omit the -irq from the name.

[..]

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +0 -0

[..]

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -0
@@ -115,6 +123,28 @@ static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_module_enable(struct qcom_wled *wled, int val) · line 135

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	if (rc < 0)
+		return rc;
+	/*
+	 * Wait for at least 10ms before enabling OVP fault interrupt after
+	 * enabling the module so that soft start is completed. Keep the OVP
+	 * interrupt disabled when the module is disabled.
+	 */
+	if (val) {
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);

This is sleeping in the brightness/enable code path, can you schedule_delayed_work() instead to not block this code path unnecessarily?

Quoted 33 lines
drivers/video/backlight/qcom-spmi-wled.c +26 -0
+
+		if (wled->cfg.ovp_irq > 0 && wled->ovp_irq_disabled) {
+			enable_irq(wled->cfg.ovp_irq);
+			wled->ovp_irq_disabled = false;
+		}
+	} else {
+		if (wled->cfg.ovp_irq > 0 && !wled->ovp_irq_disabled) {
+			disable_irq(wled->cfg.ovp_irq);
+			wled->ovp_irq_disabled = true;
+		}
+	}
+
 	return rc;
 }
 
@@ -264,12 +294,42 @@ static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)
+{
+	struct qcom_wled *wled = _wled;
+	int rc;
+	u32 int_sts, fault_sts;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+	if (rc < 0) {
+		pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+		return IRQ_HANDLED;
+	}
+
+	rc = regmap_read(wled->regmap, wled->ctrl_addr +

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 320

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			QCOM_WLED_CTRL_FAULT_STATUS, &fault_sts);
+	if (rc < 0) {
+		pr_err("Error in reading WLED_FAULT_STATUS rc=%d\n", rc);
+		return IRQ_HANDLED;
+	}
+
+	if (fault_sts &
+		(QCOM_WLED_CTRL_OVP_FAULT_BIT | QCOM_WLED_CTRL_ILIM_FAULT_BIT))
+		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
+			int_sts, fault_sts);

All this function does is print things to the log. When is this information consumed and by whom? dev_dbg() instead?

Quoted 16 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+
+	return IRQ_HANDLED;
+}
+
 static int qcom_wled_setup(struct qcom_wled *wled)
 {
 	int rc, temp, i;
 	u8 sink_en = 0;
+	u32 val;
 	u8 string_cfg = wled->cfg.string_cfg;
 	int sc_irq = wled->cfg.sc_irq;
+	int ovp_irq = wled->cfg.ovp_irq;
 
 	rc = regmap_update_bits(wled->regmap,
 			wled->ctrl_addr + QCOM_WLED_CTRL_OVP,

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 430

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
@@ -367,6 +427,25 @@ static int qcom_wled_setup(struct qcom_wled *wled)
 		}
 	}
 
+	if (ovp_irq >= 0) {

As with the previous patch.

[..]

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +0 -0
@@ -539,6 +618,10 @@ static int qcom_wled_configure(struct qcom_wled *wled, struct device *dev)

[..]

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_configure(struct qcom_wled *wled, struct device *dev) · line 624

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	wled->cfg.ovp_irq = platform_get_irq_byname(wled->pdev, "ovp-irq");
+	if (wled->cfg.ovp_irq < 0)
+		dev_dbg(&wled->pdev->dev, "ovp irq is not used\n");
+

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#21 lore raw

Re: [PATCH V1 4/4] qcom: spmi-wled: Add auto-calibration logic support

In reply to Kiran Gunda
Quoted 7 lines · Kiran Gunda

The auto-calibration algorithm checks if the current WLED sink configuration is valid. It tries enabling every sink and checks if the OVP fault is observed. Based on this information it detects and enables the valid sink configuration. Auto calibration will be triggered when the OVP fault interrupts are seen frequently thereby it tries to fix the sink configuration.

So it's not auto "calibration" it's auto "detection" of strings?

When is this feature needed?

Quoted 11 lines
---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |   5 +
 drivers/video/backlight/qcom-spmi-wled.c           | 304 ++++++++++++++++++++-
 2 files changed, 306 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
index d39ee93..f06c0cd 100644

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt The PMIC is connected to the host processor via SPMI bus. · line 97

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +1 -0
@@ -94,6 +94,11 @@ The PMIC is connected to the host processor via SPMI bus.
 	Definition: Interrupt names associated with the interrupts.
 		    Currently supported interrupts are "sc-irq" and "ovp-irq".
 
+- qcom,auto-calibration

qcom,auto-string-detect?

Quoted 12 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Enables auto-calibration of the WLED sink configuration.
+
 Example:
 
 qcom-wled@d800 {
drivers/video/backlight/qcom-spmi-wled.c +0 -0
index 8b2a77a..aee5c56 100644

drivers/video/backlight/qcom-spmi-wled.c line 41

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
@@ -38,11 +38,14 @@
 #define  QCOM_WLED_CTRL_SC_FAULT_BIT		BIT(2)
 
 #define QCOM_WLED_CTRL_INT_RT_STS		0x10
+#define  QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT	BIT(1)

The use of BIT() makes this a mask and not a bit number, so if you just drop that you can afford to spell out the "FAULT" like the data sheet does. Perhaps even making it QCOM_WLED_CTRL_OVP_FAULT_STATUS ?

drivers/video/backlight/qcom-spmi-wled.c line 47

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
 
+#define QCOM_WLED_CTRL_FDBK_OP			0x48

This is called WLED_CTRL_FEEDBACK_CONTROL, why the need to make it unreadable?

Quoted 39 lines
drivers/video/backlight/qcom-spmi-wled.c +9 -0
+
 #define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
 #define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
 
@@ -99,6 +102,7 @@ struct qcom_wled_config {
 	int ovp_irq;
 	bool en_cabc;
 	bool ext_pfet_sc_pro_en;
+	bool auto_calib_enabled;
 };
 
 struct qcom_wled {
@@ -108,18 +112,25 @@ struct qcom_wled {
 	struct mutex lock;
 	struct qcom_wled_config cfg;
 	ktime_t last_sc_event_time;
+	ktime_t start_ovp_fault_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
+	u16 auto_calibration_ovp_count;
 	u32 brightness;
 	u32 sc_count;
 	bool prev_state;
 	bool ovp_irq_disabled;
+	bool auto_calib_done;
+	bool force_mod_disable;
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
 {
 	int rc;
 
+	if (wled->force_mod_disable)
+		return 0;
+
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness) · line 190

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
@@ -187,12 +198,10 @@ static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness)
 	v[1] = (brightness >> 8) & 0xf;
 
 	for (i = 0; (string_cfg >> i) != 0; i++) {
-		if (string_cfg & BIT(i)) {

Why was this check here in the first place, if it's now fine to configure the brightness of all strings?

Also, a single-string config of 0b0001 will only set brightness on the first string, while 0b1000 will set brightness on all strings.

Quoted 26 lines
drivers/video/backlight/qcom-spmi-wled.c +14 -1
 			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
 					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
 			if (rc < 0)
 				return rc;
-		}
 	}
 
 	return 0;
@@ -294,6 +303,262 @@ static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+#define AUTO_CALIB_BRIGHTNESS		200
+static int qcom_wled_auto_calibrate(struct qcom_wled *wled)
+{
+	int rc = 0, i;
+	u32 sink_config = 0, int_sts;
+	u8 reg = 0, sink_test = 0, sink_valid = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	/* read configured sink configuration */
+	rc = regmap_read(wled->regmap, wled->sink_addr +
+			QCOM_WLED_SINK_CURR_SINK_EN, &sink_config);
+	if (rc < 0) {
+		pr_err("Failed to read SINK configuration rc=%d\n", rc);
+		goto failed_calib;

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 329

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	}
+
+	/* disable the module before starting calibration */
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+			QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable WLED module rc=%d\n",	rc);
+		goto failed_calib;
+	}

Any error handling beyond this point seems to leave the backlight off (indefinitely?), this does seem like potentially bad user experience...

In particular I wonder about the case when this would happen at some random time, minutes, hours, days, months after the device was booted.

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+
+	/* set low brightness across all sinks */
+	rc = qcom_wled_set_brightness(wled, AUTO_CALIB_BRIGHTNESS);
+	if (rc < 0) {

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+		pr_err("Failed to set brightness for calibration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	if (wled->cfg.en_cabc) {
+		for (i = 0; (string_cfg >> i) != 0; i++) {
+			reg = 0;
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Just replace "reg" with 0.

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+	}
+
+	/* disable all sinks */

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 359

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable all sinks rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* iterate through the strings one by one */
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		sink_test = 1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i);

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i);

Quoted 14 lines
drivers/video/backlight/qcom-spmi-wled.c +14 -0
+
+		/* Enable feedback control */
+		rc = regmap_write(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_FDBK_OP, i + 1);
+		if (rc < 0) {
+			pr_err("Failed to enable feedback for SINK %d rc = %d\n",
+				i + 1, rc);
+			goto failed_calib;
+		}
+
+		/* enable the sink */
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_CURR_SINK_EN, sink_test);
+		if (rc < 0) {

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 383

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			pr_err("Failed to configure SINK %d rc=%d\n",
+						i + 1, rc);
+			goto failed_calib;
+		}
+
+		/* Enable the module */
+		rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK,
+				QCOM_WLED_CTRL_MOD_EN_MASK);

I like the use of regmap_update_bits(..., MASK, MASK) it's clean, but makes me wonder why it's done differently in qcom_wled_module_enable().

Quoted 46 lines
drivers/video/backlight/qcom-spmi-wled.c +46 -0
+		if (rc < 0) {
+			pr_err("Failed to enable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+		rc = regmap_read(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+		if (rc < 0) {
+			pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		if (int_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT)
+			pr_debug("WLED OVP fault detected with SINK %d\n",
+						i + 1);
+		else
+			sink_valid |= sink_test;
+
+		/* Disable the module */
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+		if (rc < 0) {
+			pr_err("Failed to disable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	if (sink_valid == sink_config) {
+		pr_debug("WLED auto-calibration complete, default sink-config=%x OK!\n",
+						sink_config);
+	} else {
+		pr_warn("Invalid WLED default sink config=%x changing it to=%x\n",
+						sink_config, sink_valid);
+		sink_config = sink_valid;
+	}
+
+	if (!sink_config) {
+		pr_warn("No valid WLED sinks found\n");
+		wled->force_mod_disable = true;
+		goto failed_calib;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 439

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	/* write the new sink configuration */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			sink_config);
+	if (rc < 0) {
+		pr_err("Failed to reconfigure the default sink rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* MODULATOR_EN setting for valid sinks */

"Enable valid sinks"

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 442

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (wled->cfg.en_cabc) {
+			reg = QCOM_WLED_SINK_CABC_EN;

"reg" is a bad name of a variable holding the "value" to be written to a register.

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Again, just inline the value in the function call.

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 450

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+
+		if (sink_config & (1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i)))

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i)

Quoted 28 lines
drivers/video/backlight/qcom-spmi-wled.c +28 -0
+			reg = QCOM_WLED_SINK_REG_STR_MOD_EN;
+		else
+			reg = 0x0; /* disable modulator_en for unused sink */
+
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_MOD_EN_REG(i), reg);
+		if (rc < 0) {
+			pr_err("Failed to configure MODULATOR_EN rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	/* restore the feedback setting */
+	rc = regmap_write(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FDBK_OP, 0);
+	if (rc < 0) {
+		pr_err("Failed to restore feedback setting rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* restore  brightness */
+	rc = qcom_wled_set_brightness(wled, wled->brightness);
+	if (rc < 0) {
+		pr_err("Failed to set brightness after calibration rc=%d\n",
+			rc);
+		goto failed_calib;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 488

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+			QCOM_WLED_CTRL_MOD_EN_MASK,
+			QCOM_WLED_CTRL_MOD_EN_MASK);
+	if (rc < 0) {
+		pr_err("Failed to enable WLED module rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* delay for WLED soft-start */

What comes after this that you want to delay?

This delay is used to make the OVP IRQ not fire immediately, but as we've now successfully executed the string auto detection run we're never going to do anything in the OVP handler.

Quoted 30 lines
drivers/video/backlight/qcom-spmi-wled.c +30 -0
+	usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+			QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+failed_calib:
+	return rc;
+}
+
+#define WLED_AUTO_CAL_OVP_COUNT		5
+#define WLED_AUTO_CAL_CNT_DLY_US	1000000	/* 1 second */
+static bool qcom_wled_auto_cal_required(struct qcom_wled *wled)
+{
+	s64 elapsed_time_us;
+
+	/*
+	 * Check if the OVP fault was an occasional one
+	 * or if its firing continuously, the latter qualifies
+	 * for an auto-calibration check.
+	 */
+	if (!wled->auto_calibration_ovp_count) {
+		wled->start_ovp_fault_time = ktime_get();
+		wled->auto_calibration_ovp_count++;
+	} else {
+		elapsed_time_us = ktime_us_delta(ktime_get(),
+				wled->start_ovp_fault_time);
+		if (elapsed_time_us > WLED_AUTO_CAL_CNT_DLY_US)
+			wled->auto_calibration_ovp_count = 0;
+		else
+			wled->auto_calibration_ovp_count++;
+
+		if (wled->auto_calibration_ovp_count >=

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 528

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+				WLED_AUTO_CAL_OVP_COUNT) {
+			wled->auto_calibration_ovp_count = 0;
+			return true;
+		}
+	}
+
+	return false;
+}
+
+static int qcom_wled_auto_calibrate_at_init(struct qcom_wled *wled)

I presume this function is expected to detect if there is a invalid configuration at boot and try to figure out which strings are actually wired.

Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +11 -0
+{
+	int rc;
+	u32 fault_status = 0, rt_status = 0;
+
+	if (!wled->cfg.auto_calib_enabled)
+		return 0;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS,
+			&rt_status);
+	if (rc < 0)

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 549

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+		pr_err("Failed to read RT status rc=%d\n", rc);
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS,
+			&fault_status);
+	if (rc < 0)
+		pr_err("Failed to read fault status rc=%d\n", rc);
+
+	if ((rt_status & QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT) ||
+			(fault_status & QCOM_WLED_CTRL_OVP_FAULT_BIT)) {

You should be able to drop the extra () around these.

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 553

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+		mutex_lock(&wled->lock);
+		rc = qcom_wled_auto_calibrate(wled);
+		if (rc < 0)
+			pr_err("Failed auto-calibration rc=%d\n", rc);

qcom_wled_auto_calibrate() did already print, no need to repeat this.

Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+		else
+			wled->auto_calib_done = true;
+		mutex_unlock(&wled->lock);
+	}
+
+	return rc;
+}
+
 static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)
 {
 	struct qcom_wled *wled = _wled;

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 589

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
@@ -319,6 +584,33 @@ static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)
 		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
 			int_sts, fault_sts);
 
+	if (fault_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT) {
+		if (wled->cfg.auto_calib_enabled && !wled->auto_calib_done) {
+			if (qcom_wled_auto_cal_required(wled)) {

So this will be invoked only once, iff we didn't boot with a faulty configuration in which case the qcom_wled_auto_calibrate_at_init() has already done this step and set auto_calib_done.

Which also would mean that all logic in this handler, beyond the printouts, are only ever going to be executed zero or one times.

Why don't you just do auto-detection during probe (iff the flag is set in DT) and you can remove all this extra logic?

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 600

Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +11 -0
+				mutex_lock(&wled->lock);
+				if (wled->cfg.ovp_irq > 0 &&
+						!wled->ovp_irq_disabled) {
+					disable_irq_nosync(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = true;
+				}
+
+				rc = qcom_wled_auto_calibrate(wled);
+				if (rc < 0)
+					pr_err("Failed auto-calibration rc=%d\n",
+						rc);

qcom_wled_auto_calibrate() did already print.

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 613

Quoted 13 lines
drivers/video/backlight/qcom-spmi-wled.c +13 -0
+				else
+					wled->auto_calib_done = true;
+
+				if (wled->cfg.ovp_irq > 0 &&
+						wled->ovp_irq_disabled) {
+					enable_irq(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = false;
+				}
+				mutex_unlock(&wled->lock);
+			}
+		}
+	}
+

Regards,
Bjorn

#10 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

In reply to Bjorn Andersson
Quoted 17 lines · Bjorn Andersson
Quoted 15 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  90 ++++
 drivers/video/backlight/Kconfig                    |   9 +
 drivers/video/backlight/Makefile                   |   1 +
 drivers/video/backlight/qcom-spmi-wled.c           | 504 
+++++++++++++++++++++
 4 files changed, 604 insertions(+)
 create mode 100644 
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
 create mode 100644 drivers/video/backlight/qcom-spmi-wled.c
Quoted 13 lines

Review line 2

Quoted 9 lines
(quoted diff) +0 -0
new file mode 100644
index 0000000..f1ea25b

b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

(quoted diff) +2 -0
@@ -0,0 +1,90 @@
+Binding for Qualcomm WLED driver
+

This binding document quite well describe the pm8941 as well, so please improve the existing binding (changing to this style is preferable).

Sure. Will do it in the next series, where I will re-use pm8941 driver for PMI8998 as well.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +1 -0
+WLED (White Light Emitting Diode) driver is used for controlling 

display
+backlight that is part of PMIC on Qualcomm Technologies reference platforms.
+The PMIC is connected to the host processor via SPMI bus.

Quoted 9 lines

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 10

Quoted 5 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +5 -0
+
+- compatible
+	Usage:      required
+	Value type: <string>
+	Definition: should be "qcom,pm8998-spmi-wled".

There's no WLED in the pm8998, so please make this pmi8998. This pmic is SPMI only, so there's no need to keep "spmi" in the compatible.

Sure. Will change it.

Quoted 22 lines
Quoted 22 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +22 -0
+
+- reg
+	Usage:      required
+	Value type: <prop-encoded-array>
+	Definition:  Base address and size of the WLED modules.
+
+- reg-names
+	Usage:      required
+	Value type: <string>
+	Definition:  Names associated with base addresses. should be
+		     "qcom-wled-ctrl-base", "qcom-wled-sink-base".
+
+- label
+	Usage:      required
+	Value type: <string>
+	Definition: The name of the backlight device.
+
+- default-brightness
+	Usage:      optional
+	Value type: <u32>
+	Definition: brightness value on boot, value from: 0-4095
+		    default: 2048
Quoted 9 lines

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 38

Quoted 6 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +6 -0
+
+- qcom,fs-current-limit
+	Usage:      optional
+	Value type: <u32>
+	Definition: per-string full scale current limit in uA. value from
+		    0 to 30000 with 5000 uA resolution. default: 25000 uA

"in steps of 5mA"

Will address it in next series.

Quoted 36 lines
Quoted 28 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +5 -0
+
+- qcom,current-boost-limit
+	Usage:      optional
+	Value type: <u32>
+	Definition: ILIM threshold in mA. values are 105, 280, 450, 620, 

970,

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +21 -0
+		    1150, 1300, 1500. default: 970 mA
+
+- qcom,switching-freq
+	Usage:      optional
+	Value type: <u32>
+	Definition: Switching frequency in KHz. values are
+		    600, 640, 685, 738, 800, 872, 960, 1066, 1200, 1371,
+		    1600, 1920, 2400, 3200, 4800, 9600.
+		    default: 800 KHz
+
+- qcom,ovp
+	Usage:      optional
+	Value type: <u32>
+	Definition: Over-voltage protection limit in mV. values are 31100,
+		    29600, 19600, 18100.
+	            default: 29600 mV
+
+- qcom,string-cfg
+	Usage:      optional
+	Value type: <u32>
+	Definition: Bit mask of the wled strings. Bit 0 to 3 indicates 

strings

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 69

Quoted 5 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +5 -0
+		    0 to 3 respectively. Wled module has four strings of leds
+		    numbered from 0 to 3. Each string of leds are operated
+		    individually. Specify the strings using the bit mask. Any
+		    combination of led strings can be used.
+		    default value is 15 (b1111).

Please try to avoid expressing things as bitmasks in DT.

Quoted 8 lines

The only difference from 8941 here is that there's one additional string, so please start off by expressing this as the existing binding.

If you really need this flexibility you can follow up with an addition of a property like this, but name it something like "qcom,enabled-strings" and make this support available for pm8941 as well.

Sure. Will address it.

Quoted 6 lines
Quoted 2 lines

+
+- qcom,en-cabc

No need for the "en", the presence of a bool property means that it's enabled.

Will address it in next series.

Quoted 8 lines

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 75

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Specify if cabc (content adaptive backlight control) is
+		    needed.

I presume cabc isn't ever "needed", just make the description "Enable content adaptive backlight control".

Will address it in next series.

Quoted 26 lines
Quoted 15 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +15 -0
+
+Example:
+
+qcom-wled@d800 {
+	compatible = "qcom,pm8998-spmi-wled";
+	reg = <0xd800 0xd900>;
+	reg-names = "qcom-wled-ctrl-base", "qcom-wled-sink-base";
+	label = "backlight";
+
+	qcom,fs-current-limit = <25000>;
+	qcom,current-boost-limit = <970>;
+	qcom,switching-freq = <800>;
+	qcom,ovp = <29600>;
+	qcom,string-cfg = <15>;
+};

[..]

drivers/video/backlight/qcom-spmi-wled.c

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +0 -0

After reviewing your arguments and comparing the drivers I still think it's beneficial to support both these hardware revisions in the same driver.

The majority of the register differences relates to the current sink being split out, but this can easily be handled by a few well places accessor functions - which depends on this being the case or not.

Quoted 6 lines

The addition of OVP handling would benefit 8941 as well.

The short circuit handling in your patches are isolated and not taking this code path on 8941 should not pose any problems.

[..]

Ok. I will reuse the pm8941-wled.c driver for pmi8998.

Quoted 16 lines
Quoted 16 lines
drivers/video/backlight/qcom-spmi-wled.c +16 -0
+/* General definitions */
+#define QCOM_WLED_DEFAULT_BRIGHTNESS		2048
+#define  QCOM_WLED_MAX_BRIGHTNESS		4095
+
+/* WLED control registers */
+#define QCOM_WLED_CTRL_MOD_ENABLE		0x46
+#define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
+#define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
+
+#define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
+#define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
+
+#define QCOM_WLED_CTRL_OVP			0x4d
+#define  QCOM_WLED_CTRL_OVP_MASK		GENMASK(1, 0)
+
+#define QCOM_WLED_CTRL_ILIM			0x4e
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 43

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+#define  QCOM_WLED_CTRL_ILIM_MASK		GENMASK(2, 0)
+
+/* WLED sink registers */
+#define QCOM_WLED_SINK_CURR_SINK_EN		0x46
+#define  QCOM_WLED_SINK_CURR_SINK_MASK		GENMASK(7, 4)
+#define  QCOM_WLED_SINK_CURR_SINK_SHFT		0x04

Shifts are typically not given as hex...

Will address it in next series.

Quoted 31 lines
Quoted 31 lines
drivers/video/backlight/qcom-spmi-wled.c +31 -0
+
+#define QCOM_WLED_SINK_SYNC			0x47
+#define  QCOM_WLED_SINK_SYNC_MASK		GENMASK(3, 0)
+#define  QCOM_WLED_SINK_SYNC_LED1		BIT(0)
+#define  QCOM_WLED_SINK_SYNC_LED2		BIT(1)
+#define  QCOM_WLED_SINK_SYNC_LED3		BIT(2)
+#define  QCOM_WLED_SINK_SYNC_LED4		BIT(3)
+#define  QCOM_WLED_SINK_SYNC_CLEAR		0x00
+
+#define QCOM_WLED_SINK_MOD_EN_REG(n)		(0x50 + (n * 0x10))
+#define  QCOM_WLED_SINK_REG_STR_MOD_MASK	BIT(7)
+#define  QCOM_WLED_SINK_REG_STR_MOD_EN		BIT(7)
+
+#define QCOM_WLED_SINK_SYNC_DLY_REG(n)		(0x51 + (n * 0x10))
+#define QCOM_WLED_SINK_FS_CURR_REG(n)		(0x52 + (n * 0x10))
+#define  QCOM_WLED_SINK_FS_MASK			GENMASK(3, 0)
+
+#define QCOM_WLED_SINK_CABC_REG(n)		(0x56 + (n * 0x10))
+#define  QCOM_WLED_SINK_CABC_MASK		BIT(7)
+#define  QCOM_WLED_SINK_CABC_EN			BIT(7)
+
+#define QCOM_WLED_SINK_BRIGHT_LSB_REG(n)	(0x57 + (n * 0x10))
+#define QCOM_WLED_SINK_BRIGHT_MSB_REG(n)	(0x58 + (n * 0x10))
+
+struct qcom_wled_config {
+	u32 i_boost_limit;
+	u32 ovp;
+	u32 switch_freq;
+	u32 fs_current;
+	u32 string_cfg;
+	bool en_cabc;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 79

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+};
+
+struct qcom_wled {
+	const char *name;
+	struct platform_device *pdev;

Lug around the struct device * instead of the platform_device, and use this for dev_* prints throughout the code.

Will address it in next series.

Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 84

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+	struct regmap *regmap;
+	u16 sink_addr;
+	u16 ctrl_addr;
+	u32 brightness;
+	bool prev_state;

You can derive prev_state from wled->brightness in qcom_wled_update_status().

Will remove it in next series.

Quoted 6 lines
Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+
+	struct qcom_wled_config cfg;
+};
+
+static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
+{
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 95

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+	int rc;
+
+	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
+			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);

This shift obfuscate the fact that val is only 0 or 1, make val a bool and make the macro for the enabled state be BIT(7).

Will address it in next series.

Quoted 33 lines
Quoted 33 lines
drivers/video/backlight/qcom-spmi-wled.c +28 -0
+	return rc;
+}
+
+static int qcom_wled_get_brightness(struct backlight_device *bl)
+{
+	struct qcom_wled *wled = bl_get_data(bl);
+
+	return wled->brightness;
+}
+
+static int qcom_wled_sync_toggle(struct qcom_wled *wled)
+{
+	int rc;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_SYNC,
+			QCOM_WLED_SINK_SYNC_MASK, QCOM_WLED_SINK_SYNC_MASK);
+	if (rc < 0)
+		return rc;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_SYNC,
+			QCOM_WLED_SINK_SYNC_MASK, QCOM_WLED_SINK_SYNC_CLEAR);
+
+	return rc;
+}
+
+static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 

brightness)

drivers/video/backlight/qcom-spmi-wled.c +4 -0
+{
+	int rc, i;
+	u16 low_limit = QCOM_WLED_MAX_BRIGHTNESS * 4 / 1000;
+	u8 string_cfg = wled->cfg.string_cfg;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 132

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+	u8 v[2];
+
+	/* WLED's lower limit of operation is 0.4% */
+	if (brightness > 0 && brightness < low_limit)
+		brightness = low_limit;

What happens between 0 and 0.4%? Is this policy or is this related to some hardware issue?

This is related to a HW bug and if the brightness goes below 0.4% when the module is enabled, we see the continuous OVP interrupts.

Quoted 3 lines

Also, this function will not be called with brightness = 0, so you don't need to check that case.

We are disabling the module when the brightness is '0'. We update the brightness and enable the module for the next update request. So it is not needed to call this function for '0' brightness.

Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 137

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+
+	v[0] = brightness & 0xff;
+	v[1] = (brightness >> 8) & 0xf;
+
+	for (i = 0; (string_cfg >> i) != 0; i++) {

The condition looks optimal... Just loop from 0 to 3 and it will be easier to read without any measurable losses.

sure. will address it in next series.

Quoted 5 lines

drivers/video/backlight/qcom-spmi-wled.c

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +1 -0
+		if (string_cfg & BIT(i)) {

Flip this condition around and use "continue" to reduce the indentation level of the rest of the block.

sure. will address it in next series.

Quoted 19 lines
Quoted 19 lines
drivers/video/backlight/qcom-spmi-wled.c +19 -0
+			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
+			if (rc < 0)
+				return rc;
+		}
+	}
+
+	return 0;
+}
+
+static int qcom_wled_update_status(struct backlight_device *bl)
+{
+	struct qcom_wled *wled = bl_get_data(bl);
+	u16 brightness = bl->props.brightness;
+	int rc;
+
+	if (bl->props.power != FB_BLANK_UNBLANK ||
+	    bl->props.fb_blank != FB_BLANK_UNBLANK ||
+	    bl->props.state & BL_CORE_FBBLANK)
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 163

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		brightness = 0;
+
+	if (brightness) {
+		rc = qcom_wled_set_brightness(wled, brightness);
+		if (rc < 0) {
+			pr_err("wled failed to set brightness rc:%d\n", rc);

Use dev_err() and dev_dbg() throughout the driver.

sure. will address it in next series.

Quoted 5 lines
Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			return rc;
+		}
+
+		if (!!brightness != wled->prev_state) {
+			rc = qcom_wled_module_enable(wled, !!brightness);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 173

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			if (rc < 0) {
+				pr_err("wled enable failed rc:%d\n", rc);
+				return rc;
+			}
+		}

This block is exactly the same as the else statement, there's no need to repeat yourself.

This else is for the "if (brightness) {" not for the just above it.

Quoted 41 lines
Quoted 41 lines
drivers/video/backlight/qcom-spmi-wled.c +41 -0
+	} else {
+		rc = qcom_wled_module_enable(wled, brightness);
+		if (rc < 0) {
+			pr_err("wled disable failed rc:%d\n", rc);
+			return rc;
+		}
+	}
+
+	wled->prev_state = !!brightness;
+
+	rc = qcom_wled_sync_toggle(wled);
+	if (rc < 0) {
+		pr_err("wled sync failed rc:%d\n", rc);
+		return rc;
+	}
+
+	wled->brightness = brightness;
+
+	return rc;
+}
+
+static int qcom_wled_setup(struct qcom_wled *wled)
+{
+	int rc, temp, i;
+	u8 sink_en = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_OVP,
+			QCOM_WLED_CTRL_OVP_MASK, wled->cfg.ovp);
+	if (rc < 0)
+		return rc;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_ILIM,
+			QCOM_WLED_CTRL_ILIM_MASK, wled->cfg.i_boost_limit);
+	if (rc < 0)
+		return rc;
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_SWITCH_FREQ,
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 220

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+			QCOM_WLED_CTRL_SWITCH_FREQ_MASK, wled->cfg.switch_freq);
+	if (rc < 0)
+		return rc;
+
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (string_cfg & BIT(i)) {

Same as above.

Ok. Will address it in next series.

Quoted 31 lines
Quoted 18 lines
drivers/video/backlight/qcom-spmi-wled.c +18 -0
+			u16 addr = wled->sink_addr +
+					QCOM_WLED_SINK_MOD_EN_REG(i);
+
+			rc = regmap_update_bits(wled->regmap, addr,
+					QCOM_WLED_SINK_REG_STR_MOD_MASK,
+					QCOM_WLED_SINK_REG_STR_MOD_EN);
+			if (rc < 0)
+				return rc;
+
+			addr = wled->sink_addr +
+					QCOM_WLED_SINK_FS_CURR_REG(i);
+			rc = regmap_update_bits(wled->regmap, addr,
+					QCOM_WLED_SINK_FS_MASK,
+					wled->cfg.fs_current);
+			if (rc < 0)
+				return rc;
+
+			addr = wled->sink_addr +

drivers/video/backlight/qcom-spmi-wled.c line 248

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+					QCOM_WLED_SINK_CABC_REG(i);
+			rc = regmap_update_bits(wled->regmap, addr,
+					QCOM_WLED_SINK_CABC_MASK,
+					wled->cfg.en_cabc ?
+					QCOM_WLED_SINK_CABC_EN : 0);
+			if (rc)
+				return rc;
+
+			temp = i + QCOM_WLED_SINK_CURR_SINK_SHFT;
+			sink_en |= 1 << temp;

I'm failing to see the reason for the "temp" variable here. Please do:

Quoted 2 lines

sink_en |= BIT(i + QCOM_WLED_SINK_CURR_SINK_SHFT)

Ok. Will address it in next series.

Quoted 29 lines
Quoted 18 lines
drivers/video/backlight/qcom-spmi-wled.c +18 -0
+		}
+	}
+
+	rc = regmap_update_bits(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			QCOM_WLED_SINK_CURR_SINK_MASK, sink_en);
+	if (rc < 0)
+		return rc;
+
+	rc = qcom_wled_sync_toggle(wled);
+	if (rc < 0) {
+		pr_err("Failed to toggle sync reg rc:%d\n", rc);
+		return rc;
+	}
+
+	return 0;
+}
+

[..]

Quoted 10 lines

+static int qcom_wled_configure(struct qcom_wled *wled, struct device *dev)

drivers/video/backlight/qcom-spmi-wled.c +8 -0
+{
+	struct qcom_wled_config *cfg = &wled->cfg;
+	const __be32 *prop_addr;
+	u32 val, c;
+	int rc, i, j;
+
+	const struct {
+		const char *name;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 346

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+		u32 *val_ptr;
+		const struct qcom_wled_var_cfg *cfg;
+	} u32_opts[] = {

I suggest that you tie this list of options to the compatible (through of_device_id->data) and pass it as a parameter to this function. That way you can handle variation in properties and their values between different compatibles.

Ok. Will address it in next series.

Quoted 45 lines

[..]

drivers/video/backlight/qcom-spmi-wled.c line 400

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	*cfg = wled_config_defaults;
+	for (i = 0; i < ARRAY_SIZE(u32_opts); ++i) {
+		rc = of_property_read_u32(dev->of_node, u32_opts[i].name, &val);

of_property_read_u32() returns -ENODATA when there's no associated data, you can probably use this to implement support for the boolean types in the same list of opts.

[..]

Quoted 35 lines
drivers/video/backlight/qcom-spmi-wled.c +35 -0
+	}
+
+	for (i = 0; i < ARRAY_SIZE(bool_opts); ++i) {
+		if (of_property_read_bool(dev->of_node, bool_opts[i].name))
+			*bool_opts[i].val_ptr = true;
+	}
+
+	return 0;
+}
+
+static const struct backlight_ops qcom_wled_ops = {
+	.update_status = qcom_wled_update_status,
+	.get_brightness = qcom_wled_get_brightness,
+};
+
+static int qcom_wled_probe(struct platform_device *pdev)
+{
+	struct backlight_properties props;
+	struct backlight_device *bl;
+	struct qcom_wled *wled;
+	struct regmap *regmap;
+	u32 val;
+	int rc;
+
+	regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!regmap) {
+		pr_err("Unable to get regmap\n");
+		return -EINVAL;
+	}
+
+	wled = devm_kzalloc(&pdev->dev, sizeof(*wled), GFP_KERNEL);
+	if (!wled)
+		return -ENOMEM;
+
+	wled->regmap = regmap;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c line 462

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+	wled->pdev = pdev;
+
+	rc = qcom_wled_configure(wled, &pdev->dev);
+	if (rc < 0) {
+		pr_err("wled configure failed rc:%d\n", rc);

qcom_wled_configure() already printed an error message for you, no need to repeat this.

Will address it in next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c line 98

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+		return rc;
+	}
+

Please also run checkpatch.pl with the --strict option and fix the indentation issues reported.

Sure.

Quoted 2 lines

Regards,
Bjorn

#16 lore raw

Re: [PATCH V1 2/4] qcom: spmi-wled: Add support for short circuit handling

In reply to Bjorn Andersson
Quoted 24 lines · Bjorn Andersson
Quoted 22 lines · Kiran Gunda

Handle the short circuit(SC) interrupt and check if the SC interrupt is valid. Re-enable the module to check if it goes away. Disable the module altogether if the SC event persists.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  22 ++++
 drivers/video/backlight/qcom-spmi-wled.c           | 126 
++++++++++++++++++++-
 2 files changed, 142 insertions(+), 6 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -1
index f1ea25b..768608c 100644
--- 

a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
+++
b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
@@ -74,6 +74,26 @@ The PMIC is connected to the host processor via 

SPMI bus.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
 	Definition: Specify if cabc (content adaptive backlight control) is
 		    needed.
Quoted 4 lines
Quoted 1 line

+- qcom,ext-pfet-sc-pro-en

Please use readable names, rather than a bunch of abbreviations.

Ok. Will address it in next series.

Quoted 8 lines

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt The PMIC is connected to the host processor via SPMI bus. · line 81

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Specify if external PFET control for short circuit
+		    protection is needed.

What does this mean? At least change the wording to "...protection is used".

Ok. Will address it in next series.

Quoted 10 lines
Quoted 10 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +10 -0
+
+- interrupts
+	Usage:      optional
+	Value type: <prop encoded array>
+	Definition: Interrupts associated with WLED. Interrupts can be
+		    specified as per the encoding listed under
+		    Documentation/devicetree/bindings/spmi/
+		    qcom,spmi-pmic-arb.txt.
+
+- interrupt-names
Quoted 9 lines

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt The PMIC is connected to the host processor via SPMI bus. · line 95

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <string>
+	Definition: Interrupt names associated with the interrupts.
+		    Must be "sc-irq".

This is obviously an irq, so no need to include that in the name. I would also prefer if you use the name "short" to make this easier to read.

Ok. Will address it in next series.

Quoted 8 lines
Quoted 8 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +1 -0
+
 Example:
 
 qcom-wled@d800 {
@@ -82,6 +102,8 @@ qcom-wled@d800 {
 	reg-names = "qcom-wled-ctrl-base", "qcom-wled-sink-base";
 	label = "backlight";
 
Quoted 5 lines

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt qcom-wled@d800 { · line 105

Quoted 1 line
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +1 -0
+	interrupts = <0x3 0xd8 0x2 IRQ_TYPE_EDGE_RISING>;

We tend to write these on the form <decimal, hex, decimal, enum>, please follow this.

Ok. Will address it in next series.

Quoted 22 lines
Quoted 22 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +1 -0
+	interrupt-names = "sc-irq";
 	qcom,fs-current-limit = <25000>;
 	qcom,current-boost-limit = <970>;
 	qcom,switching-freq = <800>;
drivers/video/backlight/qcom-spmi-wled.c +3 -0
index 14c3adc..7dbaaa7 100644
@@ -11,6 +11,9 @@
  * GNU General Public License for more details.
  */
 
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+#include <linux/ktime.h>
 #include <linux/kernel.h>
 #include <linux/backlight.h>
 #include <linux/module.h>
@@ -23,7 +26,13 @@
 #define QCOM_WLED_DEFAULT_BRIGHTNESS		2048
 #define  QCOM_WLED_MAX_BRIGHTNESS		4095
 
Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c line 31

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+#define QCOM_WLED_SC_DLY_MS			20
+#define QCOM_WLED_SC_CNT_MAX			5
+#define QCOM_WLED_SC_RESET_CNT_DLY_US		1000000

With times of this ballpark you can just use jiffies, with this just being HZ.

Ok. Will address it in next series.

Quoted 23 lines
Quoted 23 lines
drivers/video/backlight/qcom-spmi-wled.c +12 -0
+
 /* WLED control registers */
+#define QCOM_WLED_CTRL_FAULT_STATUS		0x08
+
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
@@ -37,6 +46,15 @@
 #define QCOM_WLED_CTRL_ILIM			0x4e
 #define  QCOM_WLED_CTRL_ILIM_MASK		GENMASK(2, 0)
 
+#define QCOM_WLED_CTRL_SHORT_PROTECT		0x5e
+#define  QCOM_WLED_CTRL_SHORT_EN_MASK		BIT(7)
+
+#define QCOM_WLED_CTRL_SEC_ACCESS		0xd0
+#define  QCOM_WLED_CTRL_SEC_UNLOCK		0xa5
+
+#define QCOM_WLED_CTRL_TEST1			0xe2
+#define  QCOM_WLED_EXT_FET_DTEST2		0x09
+
 /* WLED sink registers */
 #define QCOM_WLED_SINK_CURR_SINK_EN		0x46
 #define  QCOM_WLED_SINK_CURR_SINK_MASK		GENMASK(7, 4)
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c struct qcom_wled_config { · line 92

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
@@ -71,19 +89,23 @@ struct qcom_wled_config {
 	u32 switch_freq;
 	u32 fs_current;
 	u32 string_cfg;
+	int sc_irq;

Keep data parsed directly from DT in the config and move this to qcom_wled.

Ok. Will address it in next series.

Quoted 6 lines

drivers/video/backlight/qcom-spmi-wled.c struct qcom_wled_config { · line 94

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 	bool en_cabc;
+	bool ext_pfet_sc_pro_en;

This name is long and hard to parse. "external_pfet" would be much easier to read.

Ok. Will address it in next series.

Quoted 19 lines
Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -0
 };
 
 struct qcom_wled {
 	const char *name;
 	struct platform_device *pdev;
 	struct regmap *regmap;

drivers/video/backlight/qcom-spmi-wled.c struct qcom_wled_config { · line 86

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -2
+	struct mutex lock;
+	struct qcom_wled_config cfg;
+	ktime_t last_sc_event_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
 	u32 brightness;
+	u32 sc_count;
 	bool prev_state;
-
-	struct qcom_wled_config cfg;

Moving this seems unnecessary.

Ok. Will address it in next series.

Quoted 8 lines
Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -0
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
@@ -157,25 +179,26 @@ static int qcom_wled_update_status(struct 
 backlight_device *bl)
 	    bl->props.state & BL_CORE_FBBLANK)
 		brightness = 0;
 
Quoted 4 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_update_status(struct · line 183

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +1 -0
+	mutex_lock(&wled->lock);

Is this lock necessary?

Yes. It avoid the race between the upate_status and sc_irq as the module is enabled at one place and disabled at other place respectively.

Quoted 12 lines
Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +12 -0
+static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled)
+{
+	struct qcom_wled *wled = _wled;
+	int rc;
+	u32 val;
+	s64 elapsed_time;
+
+	rc = regmap_read(wled->regmap,
+		wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS, &val);
+	if (rc < 0) {
+		pr_err("Error in reading WLED_FAULT_STATUS rc=%d\n", rc);
+		return IRQ_HANDLED;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_update_status(struct backlight_device *bl) · line 236

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+	}
+
+	wled->sc_count++;
+	pr_err("WLED short circuit detected %d times fault_status=%x\n",
+		wled->sc_count, val);

Who will read this and is it worth the extra read of FAULT_STATUS just to produce this print?

As this SC irq gets triggered in very rare conditions, i think it is okay to have a print for the information purpose.

Quoted 16 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_update_status(struct backlight_device *bl) · line 248

Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +12 -0
+	mutex_lock(&wled->lock);
+	rc = qcom_wled_module_enable(wled, false);
+	if (rc < 0) {
+		pr_err("wled disable failed rc:%d\n", rc);
+		goto unlock_mutex;
+	}
+
+	elapsed_time = ktime_us_delta(ktime_get(),
+				wled->last_sc_event_time);
+	if (elapsed_time > QCOM_WLED_SC_RESET_CNT_DLY_US) {
+		wled->sc_count = 0;
+	} else if (wled->sc_count > QCOM_WLED_SC_CNT_MAX) {

This isn't really "else elapsed_time was more than DLY_US". Split this into:

Quoted 6 lines

if (elapsed_time > xyz)

	wled->sc_count = 0;

if (wled->sc_count > QCOM_WLED_SC_CNT_MAX)

	...

Ok. sure.

Quoted 4 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_update_status(struct backlight_device *bl) · line 249

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +1 -0
+		pr_err("SC trigged %d times, disabling WLED forever!\n",

"forever" as in "until someone turns it on again"?

Yes. It is turned on for the next reboot or some one forcefully enables it form the sysfs.

Quoted 31 lines
Quoted 31 lines
drivers/video/backlight/qcom-spmi-wled.c +18 -0
+			wled->sc_count);
+		goto unlock_mutex;
+	}
+
+	wled->last_sc_event_time = ktime_get();
+
+	msleep(QCOM_WLED_SC_DLY_MS);
+	rc = qcom_wled_module_enable(wled, true);
+	if (rc < 0)
+		pr_err("wled enable failed rc:%d\n", rc);
+
+unlock_mutex:
+	mutex_unlock(&wled->lock);
+
+	return IRQ_HANDLED;
+}
+
 static int qcom_wled_setup(struct qcom_wled *wled)
 {
 	int rc, temp, i;
 	u8 sink_en = 0;
 	u8 string_cfg = wled->cfg.string_cfg;
+	int sc_irq = wled->cfg.sc_irq;
 
 	rc = regmap_update_bits(wled->regmap,
 			wled->ctrl_addr + QCOM_WLED_CTRL_OVP,
@@ -261,6 +334,39 @@ static int qcom_wled_setup(struct qcom_wled 
 *wled)
 		return rc;
 	}
 
Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 337

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +1 -0
+	if (sc_irq >= 0) {

I think things will be cleaner if you let qcom_wled_setup() configure the hardware based on the wled->cfg (as is done to this point) and then deal with the interrupts in a separate code path from the probe function.

Ok. sure.

Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 343

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		rc = devm_request_threaded_irq(&wled->pdev->dev, sc_irq,
+				NULL, qcom_wled_sc_irq_handler, IRQF_ONESHOT,
+				"qcom_wled_sc_irq", wled);
+		if (rc < 0) {
+			pr_err("Unable to request sc(%d) IRQ(err:%d)\n",
+				sc_irq, rc);

sc_irq is just a number without meaning, no need to print it.

Sure. Will remove it.

Quoted 7 lines
Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +7 -0
+			return rc;
+		}
+
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_SHORT_PROTECT,
+				QCOM_WLED_CTRL_SHORT_EN_MASK,
+				QCOM_WLED_CTRL_SHORT_EN_MASK);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 355

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+		if (rc < 0)
+			return rc;
+
+		if (wled->cfg.ext_pfet_sc_pro_en) {
+			/* unlock the secure access regisetr */

Spelling of register, and this operation does "Unlock the secure register access" it doesn't unlock the secure access register.

Sure. Will correct it.

Quoted 4 lines
Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+			rc = regmap_write(wled->regmap, wled->ctrl_addr +
+					QCOM_WLED_CTRL_SEC_ACCESS,
+					QCOM_WLED_CTRL_SEC_UNLOCK);
+			if (rc < 0)
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 364

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+				return rc;
+
+			rc = regmap_write(wled->regmap,
+					wled->ctrl_addr + QCOM_WLED_CTRL_TEST1,
+					QCOM_WLED_EXT_FET_DTEST2);

What is the relationship between DTEST2 and the external FET?
External FET is controlled through the DTEST2 register. External FET is not part of the

WLED IP so it is controlled from the DTEST pins.

Quoted 38 lines
Quoted 26 lines
drivers/video/backlight/qcom-spmi-wled.c +7 -0
+			if (rc < 0)
+				return rc;
+		}
+	}
+
 	return 0;
 }
@@ -271,6 +377,7 @@ static int qcom_wled_setup(struct qcom_wled *wled)
 	.switch_freq = 11,
 	.string_cfg = 0xf,
 	.en_cabc = 0,
+	.ext_pfet_sc_pro_en = 1,
 };
 
 struct qcom_wled_var_cfg {
@@ -376,6 +483,7 @@ static int qcom_wled_configure(struct qcom_wled 
 *wled, struct device *dev)
 		bool *val_ptr;
 	} bool_opts[] = {
 		{ "qcom,en-cabc", &cfg->en_cabc, },
+		{ "qcom,ext-pfet-sc-pro", &cfg->ext_pfet_sc_pro_en, },
 	};
 
drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	prop_addr = of_get_address(dev->of_node, 0, NULL, NULL);

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_configure(struct qcom_wled · line 542

Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
@@ -427,6 +535,10 @@ static int qcom_wled_configure(struct qcom_wled 
 *wled, struct device *dev)
 			*bool_opts[i].val_ptr = true;
 	}
 
+	wled->cfg.sc_irq = platform_get_irq_byname(wled->pdev, "sc-irq");
+	if (wled->cfg.sc_irq < 0)
+		dev_dbg(&wled->pdev->dev, "sc irq is not used\n");
+

Move this to qcom_wled_probe() or into its own code path, together with the rest of the sc_irq initialization.

Quoted 3 lines

And as you're not enabling or disabling it you can store it in a local variable.

Ok. Sure.

Quoted 6 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_configure(struct qcom_wled · line 544

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	return 0;
 }

Regards,
Bjorn

#19 lore raw

Re: [PATCH V1 3/4] qcom: spmi-wled: Add support for OVP interrupt handling

In reply to Bjorn Andersson
Quoted 34 lines · Bjorn Andersson
Quoted 24 lines · Kiran Gunda

WLED peripheral has over voltage protection(OVP) circuitry and the OVP fault is notified through an interrupt. Though this fault condition rising is due to an incorrect hardware configuration is mitigated in the hardware, it still needs to be detected and handled. Add support for it.

When WLED module is enabled, keep OVP fault interrupt disabled for 10 ms to account for soft start delay.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  7 +-
 drivers/video/backlight/qcom-spmi-wled.c           | 83 
++++++++++++++++++++++
 2 files changed, 87 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -1
index 768608c..d39ee93 100644
--- 

a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
+++
b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
@@ -92,7 +92,7 @@ The PMIC is connected to the host processor via SPMI 

bus.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt The PMIC is connected to the host processor via SPMI bus. · line 95

Quoted 5 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +1 -1
 	Usage:      optional
 	Value type: <string>
 	Definition: Interrupt names associated with the interrupts.
-		    Must be "sc-irq".
+		    Currently supported interrupts are "sc-irq" and "ovp-irq".

As before, we know this is an IRQ, so omit the -irq from the name.

Quoted 1 line

[..]

Sure. Will change it in the next series.

Quoted 13 lines
Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +0 -0

[..]

Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
@@ -115,6 +123,28 @@ static int qcom_wled_module_enable(struct 
 qcom_wled *wled, int val)
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);
+	if (rc < 0)
+		return rc;
+	/*
+	 * Wait for at least 10ms before enabling OVP fault interrupt after
+	 * enabling the module so that soft start is completed. Keep the OVP
+	 * interrupt disabled when the module is disabled.
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_module_enable(struct qcom_wled *wled, int val) · line 135

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	 */
+	if (val) {
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);

This is sleeping in the brightness/enable code path, can you schedule_delayed_work() instead to not block this code path unnecessarily?

Sure. Will change it in the next series.

Quoted 39 lines
Quoted 39 lines
drivers/video/backlight/qcom-spmi-wled.c +31 -0
+
+		if (wled->cfg.ovp_irq > 0 && wled->ovp_irq_disabled) {
+			enable_irq(wled->cfg.ovp_irq);
+			wled->ovp_irq_disabled = false;
+		}
+	} else {
+		if (wled->cfg.ovp_irq > 0 && !wled->ovp_irq_disabled) {
+			disable_irq(wled->cfg.ovp_irq);
+			wled->ovp_irq_disabled = true;
+		}
+	}
+
 	return rc;
 }
@@ -264,12 +294,42 @@ static irqreturn_t qcom_wled_sc_irq_handler(int 
 irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)
+{
+	struct qcom_wled *wled = _wled;
+	int rc;
+	u32 int_sts, fault_sts;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+	if (rc < 0) {
+		pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+		return IRQ_HANDLED;
+	}
+
+	rc = regmap_read(wled->regmap, wled->ctrl_addr +
+			QCOM_WLED_CTRL_FAULT_STATUS, &fault_sts);
+	if (rc < 0) {
+		pr_err("Error in reading WLED_FAULT_STATUS rc=%d\n", rc);
+		return IRQ_HANDLED;
+	}
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 320

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+
+	if (fault_sts &
+		(QCOM_WLED_CTRL_OVP_FAULT_BIT | QCOM_WLED_CTRL_ILIM_FAULT_BIT))
+		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
+			int_sts, fault_sts);

All this function does is print things to the log. When is this information consumed and by whom? dev_dbg() instead?

Sure. Will change it in the next series.

Quoted 20 lines
Quoted 20 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+
+	return IRQ_HANDLED;
+}
+
 static int qcom_wled_setup(struct qcom_wled *wled)
 {
 	int rc, temp, i;
 	u8 sink_en = 0;
+	u32 val;
 	u8 string_cfg = wled->cfg.string_cfg;
 	int sc_irq = wled->cfg.sc_irq;
+	int ovp_irq = wled->cfg.ovp_irq;
 
 	rc = regmap_update_bits(wled->regmap,
drivers/video/backlight/qcom-spmi-wled.c +0 -0
 			wled->ctrl_addr + QCOM_WLED_CTRL_OVP,
@@ -367,6 +427,25 @@ static int qcom_wled_setup(struct qcom_wled 
 *wled)
 		}
 	}
 
Quoted 5 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_setup(struct qcom_wled *wled) · line 430

Quoted 1 line
drivers/video/backlight/qcom-spmi-wled.c +1 -0
+	if (ovp_irq >= 0) {

As with the previous patch.

[..]

Sure. Will change it in the next series.

Quoted 10 lines
Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -0
@@ -539,6 +618,10 @@ static int qcom_wled_configure(struct qcom_wled 
 *wled, struct device *dev)

[..]

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_configure(struct qcom_wled *wled, struct device *dev) · line 624

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	wled->cfg.ovp_irq = platform_get_irq_byname(wled->pdev, "ovp-irq");
+	if (wled->cfg.ovp_irq < 0)
+		dev_dbg(&wled->pdev->dev, "ovp irq is not used\n");
+

Regards,
Bjorn

#11 lore raw

Re: [PATCH V1 1/4] qcom: spmi-wled: Add support for qcom wled driver

In reply to Kiran Gunda
Quoted 43 lines · Kiran Gunda

WLED driver provides the interface to the display driver to adjust the brightness of the display backlight.

---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |  90 ++++
 drivers/video/backlight/Kconfig                    |   9 +
 drivers/video/backlight/Makefile                   |   1 +
 drivers/video/backlight/qcom-spmi-wled.c           | 504 +++++++++++++++++++++
 4 files changed, 604 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
 create mode 100644 drivers/video/backlight/qcom-spmi-wled.c
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +24 -0
new file mode 100644
index 0000000..f1ea25b
@@ -0,0 +1,90 @@
+Binding for Qualcomm WLED driver
+
+WLED (White Light Emitting Diode) driver is used for controlling display
+backlight that is part of PMIC on Qualcomm Technologies reference platforms.
+The PMIC is connected to the host processor via SPMI bus.
+
+- compatible
+	Usage:      required
+	Value type: <string>
+	Definition: should be "qcom,pm8998-spmi-wled".
+
+- reg
+	Usage:      required
+	Value type: <prop-encoded-array>
+	Definition:  Base address and size of the WLED modules.
+
+- reg-names
+	Usage:      required
+	Value type: <string>
+	Definition:  Names associated with base addresses. should be
+		     "qcom-wled-ctrl-base", "qcom-wled-sink-base".
+
+- label
+	Usage:      required

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 34

Quoted 10 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +10 -0
+	Value type: <string>
+	Definition: The name of the backlight device.
+
+- default-brightness
+	Usage:      optional
+	Value type: <u32>
+	Definition: brightness value on boot, value from: 0-4095
+		    default: 2048
+
+- qcom,fs-current-limit

I'd add "-uA" suffix here.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt line 40

Quoted 6 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt new file +6 -0
+	Usage:      optional
+	Value type: <u32>
+	Definition: per-string full scale current limit in uA. value from
+		    0 to 30000 with 5000 uA resolution. default: 25000 uA
+
+- qcom,current-boost-limit

And -mA suffix here. Similar for other units.

Thanks,
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#22 lore raw

Re: [PATCH V1 4/4] qcom: spmi-wled: Add auto-calibration logic support

In reply to Bjorn Andersson
Quoted 10 lines · Bjorn Andersson
Quoted 7 lines · Kiran Gunda

The auto-calibration algorithm checks if the current WLED sink configuration is valid. It tries enabling every sink and checks if the OVP fault is observed. Based on this information it detects and enables the valid sink configuration. Auto calibration will be triggered when the OVP fault interrupts are seen frequently thereby it tries to fix the sink configuration.

Quoted 2 lines

So it's not auto "calibration" it's auto "detection" of strings?

Hi Bjorn,
Sorry for late response. Please find my answers.

Correct. This is the auto detection, This is the name given by the HW/systems team.

Quoted 2 lines

When is this feature needed?

This feature is needed if the string configuration is given wrong in the DT node by the user.

Quoted 18 lines
Quoted 18 lines
---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |   5 +
 drivers/video/backlight/qcom-spmi-wled.c           | 304 
++++++++++++++++++++-
 2 files changed, 306 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -1
index d39ee93..f06c0cd 100644
--- 

a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
+++
b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
@@ -94,6 +94,11 @@ The PMIC is connected to the host processor via 

SPMI bus.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
 	Definition: Interrupt names associated with the interrupts.
 		    Currently supported interrupts are "sc-irq" and "ovp-irq".
Quoted 4 lines
Quoted 1 line

+- qcom,auto-calibration

qcom,auto-string-detect?

ok. Will address in the next patch.

Quoted 14 lines
Quoted 14 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Enables auto-calibration of the WLED sink configuration.
+
 Example:
 
 qcom-wled@d800 {
drivers/video/backlight/qcom-spmi-wled.c +0 -0
index 8b2a77a..aee5c56 100644
@@ -38,11 +38,14 @@
 #define  QCOM_WLED_CTRL_SC_FAULT_BIT		BIT(2)
 
Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c line 41

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_INT_RT_STS		0x10
+#define  QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT	BIT(1)

The use of BIT() makes this a mask and not a bit number, so if you just drop that you can afford to spell out the "FAULT" like the data sheet does. Perhaps even making it QCOM_WLED_CTRL_OVP_FAULT_STATUS ?

ok. Will change it in the next series.

Quoted 10 lines

drivers/video/backlight/qcom-spmi-wled.c line 47

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
 
+#define QCOM_WLED_CTRL_FDBK_OP			0x48

This is called WLED_CTRL_FEEDBACK_CONTROL, why the need to make it unreadable?

Ok. Will address it in next series.

Quoted 42 lines
Quoted 42 lines
drivers/video/backlight/qcom-spmi-wled.c +9 -0
+
 #define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
 #define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
 
@@ -99,6 +102,7 @@ struct qcom_wled_config {
 	int ovp_irq;
 	bool en_cabc;
 	bool ext_pfet_sc_pro_en;
+	bool auto_calib_enabled;
 };
 
 struct qcom_wled {
@@ -108,18 +112,25 @@ struct qcom_wled {
 	struct mutex lock;
 	struct qcom_wled_config cfg;
 	ktime_t last_sc_event_time;
+	ktime_t start_ovp_fault_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
+	u16 auto_calibration_ovp_count;
 	u32 brightness;
 	u32 sc_count;
 	bool prev_state;
 	bool ovp_irq_disabled;
+	bool auto_calib_done;
+	bool force_mod_disable;
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
 {
 	int rc;
 
+	if (wled->force_mod_disable)
+		return 0;
+
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);
@@ -187,12 +198,10 @@ static int qcom_wled_set_brightness(struct 
 qcom_wled *wled, u16 brightness)
 	v[1] = (brightness >> 8) & 0xf;
 
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness) · line 190

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 	for (i = 0; (string_cfg >> i) != 0; i++) {
-		if (string_cfg & BIT(i)) {

Why was this check here in the first place, if it's now fine to configure the brightness of all strings?

Also, a single-string config of 0b0001 will only set brightness on the first string, while 0b1000 will set brightness on all strings.

I will correct/remove it next series.

Quoted 32 lines
Quoted 32 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
 					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
 			if (rc < 0)
 				return rc;
-		}
 	}
 
drivers/video/backlight/qcom-spmi-wled.c +19 -0
 	return 0;
@@ -294,6 +303,262 @@ static irqreturn_t qcom_wled_sc_irq_handler(int 
 irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+#define AUTO_CALIB_BRIGHTNESS		200
+static int qcom_wled_auto_calibrate(struct qcom_wled *wled)
+{
+	int rc = 0, i;
+	u32 sink_config = 0, int_sts;
+	u8 reg = 0, sink_test = 0, sink_valid = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	/* read configured sink configuration */
+	rc = regmap_read(wled->regmap, wled->sink_addr +
+			QCOM_WLED_SINK_CURR_SINK_EN, &sink_config);
+	if (rc < 0) {
+		pr_err("Failed to read SINK configuration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* disable the module before starting calibration */
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 329

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable WLED module rc=%d\n",	rc);
+		goto failed_calib;
+	}

Any error handling beyond this point seems to leave the backlight off (indefinitely?), this does seem like potentially bad user experience...

Ok. will address in next series.

Quoted 4 lines

In particular I wonder about the case when this would happen at some random time, minutes, hours, days, months after the device was booted.

This will happen for every reboot.

Quoted 8 lines
Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+
+	/* set low brightness across all sinks */
+	rc = qcom_wled_set_brightness(wled, AUTO_CALIB_BRIGHTNESS);
+	if (rc < 0) {
+		pr_err("Failed to set brightness for calibration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (wled->cfg.en_cabc) {
+		for (i = 0; (string_cfg >> i) != 0; i++) {
+			reg = 0;
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Just replace "reg" with 0.

Ok. will address in next series.

Quoted 11 lines
Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+	}
+
+	/* disable all sinks */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable all sinks rc=%d\n", rc);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 359

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		goto failed_calib;
+	}
+
+	/* iterate through the strings one by one */
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		sink_test = 1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i);

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i);

Will address in next series.

Quoted 19 lines
Quoted 19 lines
drivers/video/backlight/qcom-spmi-wled.c +19 -0
+
+		/* Enable feedback control */
+		rc = regmap_write(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_FDBK_OP, i + 1);
+		if (rc < 0) {
+			pr_err("Failed to enable feedback for SINK %d rc = %d\n",
+				i + 1, rc);
+			goto failed_calib;
+		}
+
+		/* enable the sink */
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_CURR_SINK_EN, sink_test);
+		if (rc < 0) {
+			pr_err("Failed to configure SINK %d rc=%d\n",
+						i + 1, rc);
+			goto failed_calib;
+		}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 383

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+		/* Enable the module */
+		rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK,
+				QCOM_WLED_CTRL_MOD_EN_MASK);

I like the use of regmap_update_bits(..., MASK, MASK) it's clean, but makes me wonder why it's done differently in qcom_wled_module_enable().

will address it in the next series.

Quoted 51 lines
Quoted 51 lines
drivers/video/backlight/qcom-spmi-wled.c +50 -0
+		if (rc < 0) {
+			pr_err("Failed to enable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+		rc = regmap_read(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+		if (rc < 0) {
+			pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		if (int_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT)
+			pr_debug("WLED OVP fault detected with SINK %d\n",
+						i + 1);
+		else
+			sink_valid |= sink_test;
+
+		/* Disable the module */
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+		if (rc < 0) {
+			pr_err("Failed to disable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	if (sink_valid == sink_config) {
+		pr_debug("WLED auto-calibration complete, default sink-config=%x 
 OK!\n",
+						sink_config);
+	} else {
+		pr_warn("Invalid WLED default sink config=%x changing it to=%x\n",
+						sink_config, sink_valid);
+		sink_config = sink_valid;
+	}
+
+	if (!sink_config) {
+		pr_warn("No valid WLED sinks found\n");
+		wled->force_mod_disable = true;
+		goto failed_calib;
+	}
+
+	/* write the new sink configuration */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			sink_config);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 439

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (rc < 0) {
+		pr_err("Failed to reconfigure the default sink rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* MODULATOR_EN setting for valid sinks */

"Enable valid sinks"

Will address it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 442

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (wled->cfg.en_cabc) {
+			reg = QCOM_WLED_SINK_CABC_EN;

"reg" is a bad name of a variable holding the "value" to be written to a register.

Will address it in the next series.

Quoted 6 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Again, just inline the value in the function call.

Will address it in the next series.

Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 450

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+
+		if (sink_config & (1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i)))

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i)

Will address it in the next series.

Quoted 41 lines
Quoted 28 lines
drivers/video/backlight/qcom-spmi-wled.c +28 -0
+			reg = QCOM_WLED_SINK_REG_STR_MOD_EN;
+		else
+			reg = 0x0; /* disable modulator_en for unused sink */
+
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_MOD_EN_REG(i), reg);
+		if (rc < 0) {
+			pr_err("Failed to configure MODULATOR_EN rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	/* restore the feedback setting */
+	rc = regmap_write(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FDBK_OP, 0);
+	if (rc < 0) {
+		pr_err("Failed to restore feedback setting rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* restore  brightness */
+	rc = qcom_wled_set_brightness(wled, wled->brightness);
+	if (rc < 0) {
+		pr_err("Failed to set brightness after calibration rc=%d\n",
+			rc);
+		goto failed_calib;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 488

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+			QCOM_WLED_CTRL_MOD_EN_MASK,
+			QCOM_WLED_CTRL_MOD_EN_MASK);
+	if (rc < 0) {
+		pr_err("Failed to enable WLED module rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* delay for WLED soft-start */

What comes after this that you want to delay?

Quoted 4 lines

This delay is used to make the OVP IRQ not fire immediately, but as we've now successfully executed the string auto detection run we're never going to do anything in the OVP handler.

Will correct it in the next series.

Quoted 36 lines
Quoted 36 lines
drivers/video/backlight/qcom-spmi-wled.c +36 -0
+	usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+			QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+failed_calib:
+	return rc;
+}
+
+#define WLED_AUTO_CAL_OVP_COUNT		5
+#define WLED_AUTO_CAL_CNT_DLY_US	1000000	/* 1 second */
+static bool qcom_wled_auto_cal_required(struct qcom_wled *wled)
+{
+	s64 elapsed_time_us;
+
+	/*
+	 * Check if the OVP fault was an occasional one
+	 * or if its firing continuously, the latter qualifies
+	 * for an auto-calibration check.
+	 */
+	if (!wled->auto_calibration_ovp_count) {
+		wled->start_ovp_fault_time = ktime_get();
+		wled->auto_calibration_ovp_count++;
+	} else {
+		elapsed_time_us = ktime_us_delta(ktime_get(),
+				wled->start_ovp_fault_time);
+		if (elapsed_time_us > WLED_AUTO_CAL_CNT_DLY_US)
+			wled->auto_calibration_ovp_count = 0;
+		else
+			wled->auto_calibration_ovp_count++;
+
+		if (wled->auto_calibration_ovp_count >=
+				WLED_AUTO_CAL_OVP_COUNT) {
+			wled->auto_calibration_ovp_count = 0;
+			return true;
+		}
+	}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 528

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	return false;
+}
+
+static int qcom_wled_auto_calibrate_at_init(struct qcom_wled *wled)

I presume this function is expected to detect if there is a invalid configuration at boot and try to figure out which strings are actually wired.

Correct.

Quoted 15 lines
Quoted 15 lines
drivers/video/backlight/qcom-spmi-wled.c +15 -0
+{
+	int rc;
+	u32 fault_status = 0, rt_status = 0;
+
+	if (!wled->cfg.auto_calib_enabled)
+		return 0;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS,
+			&rt_status);
+	if (rc < 0)
+		pr_err("Failed to read RT status rc=%d\n", rc);
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS,
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 549

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+			&fault_status);
+	if (rc < 0)
+		pr_err("Failed to read fault status rc=%d\n", rc);
+
+	if ((rt_status & QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT) ||
+			(fault_status & QCOM_WLED_CTRL_OVP_FAULT_BIT)) {

You should be able to drop the extra () around these.

Ok. Will remove it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 553

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+		mutex_lock(&wled->lock);
+		rc = qcom_wled_auto_calibrate(wled);
+		if (rc < 0)
+			pr_err("Failed auto-calibration rc=%d\n", rc);

qcom_wled_auto_calibrate() did already print, no need to repeat this.

Ok. Will remove this in the next series.

Quoted 25 lines
Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+		else
+			wled->auto_calib_done = true;
+		mutex_unlock(&wled->lock);
+	}
+
+	return rc;
+}
+
 static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)

{

drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	struct qcom_wled *wled = _wled;

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int · line 590

Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
@@ -319,6 +584,33 @@ static irqreturn_t qcom_wled_ovp_irq_handler(int 
 irq, void *_wled)
 		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
 			int_sts, fault_sts);
 
+	if (fault_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT) {
+		if (wled->cfg.auto_calib_enabled && !wled->auto_calib_done) {
+			if (qcom_wled_auto_cal_required(wled)) {

So this will be invoked only once, iff we didn't boot with a faulty configuration in which case the qcom_wled_auto_calibrate_at_init() has already done this step and set auto_calib_done.

Quoted 6 lines

Which also would mean that all logic in this handler, beyond the printouts, are only ever going to be executed zero or one times.

Why don't you just do auto-detection during probe (iff the flag is set in DT) and you can remove all this extra logic?

I think we have seen a issue, where the OVP interrupt is not getting set
some times during the execution of this function at boot. In that case the auto calibration is done bit later. That's why this code is added.

Quoted 5 lines
Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+				mutex_lock(&wled->lock);
+				if (wled->cfg.ovp_irq > 0 &&
+						!wled->ovp_irq_disabled) {
+					disable_irq_nosync(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = true;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 600

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+				}
+
+				rc = qcom_wled_auto_calibrate(wled);
+				if (rc < 0)
+					pr_err("Failed auto-calibration rc=%d\n",
+						rc);

qcom_wled_auto_calibrate() did already print.

Ok. I will remove it in the next series.

Quoted 20 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 613

Quoted 13 lines
drivers/video/backlight/qcom-spmi-wled.c +13 -0
+				else
+					wled->auto_calib_done = true;
+
+				if (wled->cfg.ovp_irq > 0 &&
+						wled->ovp_irq_disabled) {
+					enable_irq(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = false;
+				}
+				mutex_unlock(&wled->lock);
+			}
+		}
+	}
+

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#23 lore raw

Re: [PATCH V1 4/4] qcom: spmi-wled: Add auto-calibration logic support

Quoted 12 lines · On Thu 19 Apr 03:45 PDT 2018, [email protected] wrote:
Quoted 10 lines · Bjorn Andersson
Quoted 7 lines · Kiran Gunda

The auto-calibration algorithm checks if the current WLED sink configuration is valid. It tries enabling every sink and checks if the OVP fault is observed. Based on this information it detects and enables the valid sink configuration. Auto calibration will be triggered when the OVP fault interrupts are seen frequently thereby it tries to fix the sink configuration.

Quoted 5 lines
Quoted 2 lines

So it's not auto "calibration" it's auto "detection" of strings?

Hi Bjorn,
Sorry for late response. Please find my answers.

No worries, happy to hear back from you!

Quoted 2 lines

Correct. This is the auto detection, This is the name given by the HW/systems team.

I think the name should be considered a "hardware bug", that we can work around in software (give it a useful name and document what the original name was).

Quoted 4 lines
Quoted 2 lines

When is this feature needed?

This feature is needed if the string configuration is given wrong in the DT node by the user.

DT describes the hardware and for all other nodes it must do so accurately.

For cases where the hardware supports auto detection of functionality we remove information from DT and rely on that logic to figure out the hardware. We do not use it to reconfigure the hardware once we detect an error. So when auto-detection is enabled it should always be used to probe the hardware.

Regards,
Bjorn

Quoted 505 lines
Quoted 20 lines
Quoted 20 lines
---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |   5 +
 drivers/video/backlight/qcom-spmi-wled.c           | 304
++++++++++++++++++++-
 2 files changed, 306 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt index d39ee93..f06c0cd 100644
---
a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
+++
b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

(quoted diff) +0 -0
@@ -94,6 +94,11 @@ The PMIC is connected to the host processor via

SPMI bus.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
 	Definition: Interrupt names associated with the interrupts.
 		    Currently supported interrupts are "sc-irq" and "ovp-irq".
Quoted 4 lines
Quoted 1 line

+- qcom,auto-calibration

qcom,auto-string-detect?

ok. Will address in the next patch.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Enables auto-calibration of the WLED sink configuration.
+
 Example:
 
 qcom-wled@d800 {
drivers/video/backlight/qcom-spmi-wled.c +0 -0
index 8b2a77a..aee5c56 100644
@@ -38,11 +38,14 @@
 #define  QCOM_WLED_CTRL_SC_FAULT_BIT		BIT(2)
 
Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c line 41

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_INT_RT_STS		0x10
+#define  QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT	BIT(1)

The use of BIT() makes this a mask and not a bit number, so if you just drop that you can afford to spell out the "FAULT" like the data sheet does. Perhaps even making it QCOM_WLED_CTRL_OVP_FAULT_STATUS ?

ok. Will change it in the next series.

Quoted 10 lines

drivers/video/backlight/qcom-spmi-wled.c line 47

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
 
+#define QCOM_WLED_CTRL_FDBK_OP			0x48

This is called WLED_CTRL_FEEDBACK_CONTROL, why the need to make it unreadable?

Ok. Will address it in next series.

Quoted 42 lines
Quoted 42 lines
drivers/video/backlight/qcom-spmi-wled.c +9 -0
+
 #define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
 #define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
 
@@ -99,6 +102,7 @@ struct qcom_wled_config {
 	int ovp_irq;
 	bool en_cabc;
 	bool ext_pfet_sc_pro_en;
+	bool auto_calib_enabled;
 };
 
 struct qcom_wled {
@@ -108,18 +112,25 @@ struct qcom_wled {
 	struct mutex lock;
 	struct qcom_wled_config cfg;
 	ktime_t last_sc_event_time;
+	ktime_t start_ovp_fault_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
+	u16 auto_calibration_ovp_count;
 	u32 brightness;
 	u32 sc_count;
 	bool prev_state;
 	bool ovp_irq_disabled;
+	bool auto_calib_done;
+	bool force_mod_disable;
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
 {
 	int rc;
 
+	if (wled->force_mod_disable)
+		return 0;
+
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);
@@ -187,12 +198,10 @@ static int qcom_wled_set_brightness(struct
 qcom_wled *wled, u16 brightness)
 	v[1] = (brightness >> 8) & 0xf;
 
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness) · line 190

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 	for (i = 0; (string_cfg >> i) != 0; i++) {
-		if (string_cfg & BIT(i)) {

Why was this check here in the first place, if it's now fine to configure the brightness of all strings?

Also, a single-string config of 0b0001 will only set brightness on the first string, while 0b1000 will set brightness on all strings.

I will correct/remove it next series.

Quoted 32 lines
Quoted 32 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
 					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
 			if (rc < 0)
 				return rc;
-		}
 	}
 
drivers/video/backlight/qcom-spmi-wled.c +19 -0
 	return 0;
@@ -294,6 +303,262 @@ static irqreturn_t
 qcom_wled_sc_irq_handler(int irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+#define AUTO_CALIB_BRIGHTNESS		200
+static int qcom_wled_auto_calibrate(struct qcom_wled *wled)
+{
+	int rc = 0, i;
+	u32 sink_config = 0, int_sts;
+	u8 reg = 0, sink_test = 0, sink_valid = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	/* read configured sink configuration */
+	rc = regmap_read(wled->regmap, wled->sink_addr +
+			QCOM_WLED_SINK_CURR_SINK_EN, &sink_config);
+	if (rc < 0) {
+		pr_err("Failed to read SINK configuration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* disable the module before starting calibration */
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 329

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable WLED module rc=%d\n",	rc);
+		goto failed_calib;
+	}

Any error handling beyond this point seems to leave the backlight off (indefinitely?), this does seem like potentially bad user experience...

Ok. will address in next series.

Quoted 4 lines

In particular I wonder about the case when this would happen at some random time, minutes, hours, days, months after the device was booted.

This will happen for every reboot.

Quoted 8 lines
Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+
+	/* set low brightness across all sinks */
+	rc = qcom_wled_set_brightness(wled, AUTO_CALIB_BRIGHTNESS);
+	if (rc < 0) {
+		pr_err("Failed to set brightness for calibration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (wled->cfg.en_cabc) {
+		for (i = 0; (string_cfg >> i) != 0; i++) {
+			reg = 0;
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Just replace "reg" with 0.

Ok. will address in next series.

Quoted 11 lines
Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+	}
+
+	/* disable all sinks */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable all sinks rc=%d\n", rc);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 359

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		goto failed_calib;
+	}
+
+	/* iterate through the strings one by one */
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		sink_test = 1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i);

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i);

Will address in next series.

Quoted 19 lines
Quoted 19 lines
drivers/video/backlight/qcom-spmi-wled.c +19 -0
+
+		/* Enable feedback control */
+		rc = regmap_write(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_FDBK_OP, i + 1);
+		if (rc < 0) {
+			pr_err("Failed to enable feedback for SINK %d rc = %d\n",
+				i + 1, rc);
+			goto failed_calib;
+		}
+
+		/* enable the sink */
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_CURR_SINK_EN, sink_test);
+		if (rc < 0) {
+			pr_err("Failed to configure SINK %d rc=%d\n",
+						i + 1, rc);
+			goto failed_calib;
+		}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 383

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+		/* Enable the module */
+		rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK,
+				QCOM_WLED_CTRL_MOD_EN_MASK);

I like the use of regmap_update_bits(..., MASK, MASK) it's clean, but makes me wonder why it's done differently in qcom_wled_module_enable().

will address it in the next series.

Quoted 51 lines
Quoted 51 lines
drivers/video/backlight/qcom-spmi-wled.c +50 -0
+		if (rc < 0) {
+			pr_err("Failed to enable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+		rc = regmap_read(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+		if (rc < 0) {
+			pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		if (int_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT)
+			pr_debug("WLED OVP fault detected with SINK %d\n",
+						i + 1);
+		else
+			sink_valid |= sink_test;
+
+		/* Disable the module */
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+		if (rc < 0) {
+			pr_err("Failed to disable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	if (sink_valid == sink_config) {
+		pr_debug("WLED auto-calibration complete, default sink-config=%x
 OK!\n",
+						sink_config);
+	} else {
+		pr_warn("Invalid WLED default sink config=%x changing it to=%x\n",
+						sink_config, sink_valid);
+		sink_config = sink_valid;
+	}
+
+	if (!sink_config) {
+		pr_warn("No valid WLED sinks found\n");
+		wled->force_mod_disable = true;
+		goto failed_calib;
+	}
+
+	/* write the new sink configuration */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			sink_config);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 439

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (rc < 0) {
+		pr_err("Failed to reconfigure the default sink rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* MODULATOR_EN setting for valid sinks */

"Enable valid sinks"

Will address it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 442

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (wled->cfg.en_cabc) {
+			reg = QCOM_WLED_SINK_CABC_EN;

"reg" is a bad name of a variable holding the "value" to be written to a register.

Will address it in the next series.

Quoted 6 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Again, just inline the value in the function call.

Will address it in the next series.

Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 450

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+
+		if (sink_config & (1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i)))

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i)

Will address it in the next series.

Quoted 41 lines
Quoted 28 lines
drivers/video/backlight/qcom-spmi-wled.c +28 -0
+			reg = QCOM_WLED_SINK_REG_STR_MOD_EN;
+		else
+			reg = 0x0; /* disable modulator_en for unused sink */
+
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_MOD_EN_REG(i), reg);
+		if (rc < 0) {
+			pr_err("Failed to configure MODULATOR_EN rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	/* restore the feedback setting */
+	rc = regmap_write(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FDBK_OP, 0);
+	if (rc < 0) {
+		pr_err("Failed to restore feedback setting rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* restore  brightness */
+	rc = qcom_wled_set_brightness(wled, wled->brightness);
+	if (rc < 0) {
+		pr_err("Failed to set brightness after calibration rc=%d\n",
+			rc);
+		goto failed_calib;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 488

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+			QCOM_WLED_CTRL_MOD_EN_MASK,
+			QCOM_WLED_CTRL_MOD_EN_MASK);
+	if (rc < 0) {
+		pr_err("Failed to enable WLED module rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* delay for WLED soft-start */

What comes after this that you want to delay?

Quoted 4 lines

This delay is used to make the OVP IRQ not fire immediately, but as we've now successfully executed the string auto detection run we're never going to do anything in the OVP handler.

Will correct it in the next series.

Quoted 36 lines
Quoted 36 lines
drivers/video/backlight/qcom-spmi-wled.c +36 -0
+	usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+			QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+failed_calib:
+	return rc;
+}
+
+#define WLED_AUTO_CAL_OVP_COUNT		5
+#define WLED_AUTO_CAL_CNT_DLY_US	1000000	/* 1 second */
+static bool qcom_wled_auto_cal_required(struct qcom_wled *wled)
+{
+	s64 elapsed_time_us;
+
+	/*
+	 * Check if the OVP fault was an occasional one
+	 * or if its firing continuously, the latter qualifies
+	 * for an auto-calibration check.
+	 */
+	if (!wled->auto_calibration_ovp_count) {
+		wled->start_ovp_fault_time = ktime_get();
+		wled->auto_calibration_ovp_count++;
+	} else {
+		elapsed_time_us = ktime_us_delta(ktime_get(),
+				wled->start_ovp_fault_time);
+		if (elapsed_time_us > WLED_AUTO_CAL_CNT_DLY_US)
+			wled->auto_calibration_ovp_count = 0;
+		else
+			wled->auto_calibration_ovp_count++;
+
+		if (wled->auto_calibration_ovp_count >=
+				WLED_AUTO_CAL_OVP_COUNT) {
+			wled->auto_calibration_ovp_count = 0;
+			return true;
+		}
+	}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 528

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	return false;
+}
+
+static int qcom_wled_auto_calibrate_at_init(struct qcom_wled *wled)

I presume this function is expected to detect if there is a invalid configuration at boot and try to figure out which strings are actually wired.

Correct.

Quoted 15 lines
Quoted 15 lines
drivers/video/backlight/qcom-spmi-wled.c +15 -0
+{
+	int rc;
+	u32 fault_status = 0, rt_status = 0;
+
+	if (!wled->cfg.auto_calib_enabled)
+		return 0;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS,
+			&rt_status);
+	if (rc < 0)
+		pr_err("Failed to read RT status rc=%d\n", rc);
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS,
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 549

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+			&fault_status);
+	if (rc < 0)
+		pr_err("Failed to read fault status rc=%d\n", rc);
+
+	if ((rt_status & QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT) ||
+			(fault_status & QCOM_WLED_CTRL_OVP_FAULT_BIT)) {

You should be able to drop the extra () around these.

Ok. Will remove it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 553

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+		mutex_lock(&wled->lock);
+		rc = qcom_wled_auto_calibrate(wled);
+		if (rc < 0)
+			pr_err("Failed auto-calibration rc=%d\n", rc);

qcom_wled_auto_calibrate() did already print, no need to repeat this.

Ok. Will remove this in the next series.

Quoted 25 lines
Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+		else
+			wled->auto_calib_done = true;
+		mutex_unlock(&wled->lock);
+	}
+
+	return rc;
+}
+
 static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)

{

drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	struct qcom_wled *wled = _wled;

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t · line 590

Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
@@ -319,6 +584,33 @@ static irqreturn_t
 qcom_wled_ovp_irq_handler(int irq, void *_wled)
 		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
 			int_sts, fault_sts);
 
+	if (fault_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT) {
+		if (wled->cfg.auto_calib_enabled && !wled->auto_calib_done) {
+			if (qcom_wled_auto_cal_required(wled)) {

So this will be invoked only once, iff we didn't boot with a faulty configuration in which case the qcom_wled_auto_calibrate_at_init() has already done this step and set auto_calib_done.

Quoted 6 lines

Which also would mean that all logic in this handler, beyond the printouts, are only ever going to be executed zero or one times.

Why don't you just do auto-detection during probe (iff the flag is set in DT) and you can remove all this extra logic?

I think we have seen a issue, where the OVP interrupt is not getting set some times during the execution of this function at boot. In that case the auto calibration is
done bit later. That's why this code is added.

Quoted 5 lines
Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+				mutex_lock(&wled->lock);
+				if (wled->cfg.ovp_irq > 0 &&
+						!wled->ovp_irq_disabled) {
+					disable_irq_nosync(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = true;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 600

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+				}
+
+				rc = qcom_wled_auto_calibrate(wled);
+				if (rc < 0)
+					pr_err("Failed auto-calibration rc=%d\n",
+						rc);

qcom_wled_auto_calibrate() did already print.

Ok. I will remove it in the next series.

Quoted 21 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 613

Quoted 13 lines
drivers/video/backlight/qcom-spmi-wled.c +13 -0
+				else
+					wled->auto_calib_done = true;
+
+				if (wled->cfg.ovp_irq > 0 &&
+						wled->ovp_irq_disabled) {
+					enable_irq(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = false;
+				}
+				mutex_unlock(&wled->lock);
+			}
+		}
+	}
+

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#24 lore raw

Re: [PATCH V1 4/4] qcom: spmi-wled: Add auto-calibration logic support

Quoted 14 lines · Bjorn Andersson
Quoted 12 lines · On Thu 19 Apr 03:45 PDT 2018, [email protected] wrote:
Quoted 10 lines · Bjorn Andersson
Quoted 7 lines · Kiran Gunda

The auto-calibration algorithm checks if the current WLED sink configuration is valid. It tries enabling every sink and checks if the OVP fault is observed. Based on this information it detects and enables the valid sink configuration. Auto calibration will be triggered when the OVP fault interrupts are seen frequently thereby it tries to fix the sink configuration.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

So it's not auto "calibration" it's auto "detection" of strings?

Hi Bjorn,
Sorry for late response. Please find my answers.

No worries, happy to hear back from you!

Thanks!

Quoted 7 lines
Quoted 2 lines

Correct. This is the auto detection, This is the name given by the HW/systems team.

I think the name should be considered a "hardware bug", that we can work around in software (give it a useful name and document what the original name was).

I don't think this is the "hardware bug". Rather we can say HW doesn't support it. Hence, we are implementing it as a SW feature to detect the strings present on the display panel, if the user fails to give the correct strings. As you suggested I will rename this to "auto detection" instead of "auto calibration".

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

When is this feature needed?

This feature is needed if the string configuration is given wrong in the DT node by the user.

DT describes the hardware and for all other nodes it must do so accurately.

But the user may not be aware of the strings present on the display panel or may be using the same software on different devices which have different strings present.

Quoted 6 lines

For cases where the hardware supports auto detection of functionality we remove information from DT and rely on that logic to figure out the hardware. We do not use it to reconfigure the hardware once we detect an error. So when auto-detection is enabled it should always be used to probe the hardware.

The auto string detection is not supported in any qcom hardware and i don't think there is a plan to introduce in new hardware also.

Quoted 512 lines

Regards,
Bjorn

Quoted 483 lines
Quoted 20 lines
Quoted 20 lines
---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |   5 +
 drivers/video/backlight/qcom-spmi-wled.c           | 304
++++++++++++++++++++-
 2 files changed, 306 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt index d39ee93..f06c0cd 100644
---
a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
+++
b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

(quoted diff) +0 -0
@@ -94,6 +94,11 @@ The PMIC is connected to the host processor via

SPMI bus.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
 	Definition: Interrupt names associated with the interrupts.
 		    Currently supported interrupts are "sc-irq" and "ovp-irq".
Quoted 4 lines
Quoted 1 line

+- qcom,auto-calibration

qcom,auto-string-detect?

ok. Will address in the next patch.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Enables auto-calibration of the WLED sink configuration.
+
 Example:
 
 qcom-wled@d800 {
drivers/video/backlight/qcom-spmi-wled.c +0 -0
index 8b2a77a..aee5c56 100644
@@ -38,11 +38,14 @@
 #define  QCOM_WLED_CTRL_SC_FAULT_BIT		BIT(2)
 
Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c line 41

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_INT_RT_STS		0x10
+#define  QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT	BIT(1)

The use of BIT() makes this a mask and not a bit number, so if you just drop that you can afford to spell out the "FAULT" like the data sheet does. Perhaps even making it QCOM_WLED_CTRL_OVP_FAULT_STATUS ?

ok. Will change it in the next series.

Quoted 10 lines

drivers/video/backlight/qcom-spmi-wled.c line 47

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
 
+#define QCOM_WLED_CTRL_FDBK_OP			0x48

This is called WLED_CTRL_FEEDBACK_CONTROL, why the need to make it unreadable?

Ok. Will address it in next series.

Quoted 42 lines
Quoted 42 lines
drivers/video/backlight/qcom-spmi-wled.c +9 -0
+
 #define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
 #define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
 
@@ -99,6 +102,7 @@ struct qcom_wled_config {
 	int ovp_irq;
 	bool en_cabc;
 	bool ext_pfet_sc_pro_en;
+	bool auto_calib_enabled;
 };
 
 struct qcom_wled {
@@ -108,18 +112,25 @@ struct qcom_wled {
 	struct mutex lock;
 	struct qcom_wled_config cfg;
 	ktime_t last_sc_event_time;
+	ktime_t start_ovp_fault_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
+	u16 auto_calibration_ovp_count;
 	u32 brightness;
 	u32 sc_count;
 	bool prev_state;
 	bool ovp_irq_disabled;
+	bool auto_calib_done;
+	bool force_mod_disable;
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
 {
 	int rc;
 
+	if (wled->force_mod_disable)
+		return 0;
+
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);
@@ -187,12 +198,10 @@ static int qcom_wled_set_brightness(struct
 qcom_wled *wled, u16 brightness)
 	v[1] = (brightness >> 8) & 0xf;
 
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness) · line 190

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 	for (i = 0; (string_cfg >> i) != 0; i++) {
-		if (string_cfg & BIT(i)) {

Why was this check here in the first place, if it's now fine to configure the brightness of all strings?

Also, a single-string config of 0b0001 will only set brightness on the first string, while 0b1000 will set brightness on all strings.

I will correct/remove it next series.

Quoted 32 lines
Quoted 32 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
 					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
 			if (rc < 0)
 				return rc;
-		}
 	}
 
drivers/video/backlight/qcom-spmi-wled.c +19 -0
 	return 0;
@@ -294,6 +303,262 @@ static irqreturn_t
 qcom_wled_sc_irq_handler(int irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+#define AUTO_CALIB_BRIGHTNESS		200
+static int qcom_wled_auto_calibrate(struct qcom_wled *wled)
+{
+	int rc = 0, i;
+	u32 sink_config = 0, int_sts;
+	u8 reg = 0, sink_test = 0, sink_valid = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	/* read configured sink configuration */
+	rc = regmap_read(wled->regmap, wled->sink_addr +
+			QCOM_WLED_SINK_CURR_SINK_EN, &sink_config);
+	if (rc < 0) {
+		pr_err("Failed to read SINK configuration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* disable the module before starting calibration */
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 329

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable WLED module rc=%d\n",	rc);
+		goto failed_calib;
+	}

Any error handling beyond this point seems to leave the backlight off (indefinitely?), this does seem like potentially bad user experience...

Ok. will address in next series.

Quoted 4 lines

In particular I wonder about the case when this would happen at some random time, minutes, hours, days, months after the device was booted.

This will happen for every reboot.

Quoted 8 lines
Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+
+	/* set low brightness across all sinks */
+	rc = qcom_wled_set_brightness(wled, AUTO_CALIB_BRIGHTNESS);
+	if (rc < 0) {
+		pr_err("Failed to set brightness for calibration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (wled->cfg.en_cabc) {
+		for (i = 0; (string_cfg >> i) != 0; i++) {
+			reg = 0;
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Just replace "reg" with 0.

Ok. will address in next series.

Quoted 11 lines
Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+	}
+
+	/* disable all sinks */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable all sinks rc=%d\n", rc);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 359

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		goto failed_calib;
+	}
+
+	/* iterate through the strings one by one */
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		sink_test = 1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i);

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i);

Will address in next series.

Quoted 19 lines
Quoted 19 lines
drivers/video/backlight/qcom-spmi-wled.c +19 -0
+
+		/* Enable feedback control */
+		rc = regmap_write(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_FDBK_OP, i + 1);
+		if (rc < 0) {
+			pr_err("Failed to enable feedback for SINK %d rc = %d\n",
+				i + 1, rc);
+			goto failed_calib;
+		}
+
+		/* enable the sink */
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_CURR_SINK_EN, sink_test);
+		if (rc < 0) {
+			pr_err("Failed to configure SINK %d rc=%d\n",
+						i + 1, rc);
+			goto failed_calib;
+		}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 383

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+		/* Enable the module */
+		rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK,
+				QCOM_WLED_CTRL_MOD_EN_MASK);

I like the use of regmap_update_bits(..., MASK, MASK) it's clean, but makes me wonder why it's done differently in qcom_wled_module_enable().

will address it in the next series.

Quoted 51 lines
Quoted 51 lines
drivers/video/backlight/qcom-spmi-wled.c +50 -0
+		if (rc < 0) {
+			pr_err("Failed to enable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+		rc = regmap_read(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+		if (rc < 0) {
+			pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		if (int_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT)
+			pr_debug("WLED OVP fault detected with SINK %d\n",
+						i + 1);
+		else
+			sink_valid |= sink_test;
+
+		/* Disable the module */
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+		if (rc < 0) {
+			pr_err("Failed to disable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	if (sink_valid == sink_config) {
+		pr_debug("WLED auto-calibration complete, default sink-config=%x
 OK!\n",
+						sink_config);
+	} else {
+		pr_warn("Invalid WLED default sink config=%x changing it to=%x\n",
+						sink_config, sink_valid);
+		sink_config = sink_valid;
+	}
+
+	if (!sink_config) {
+		pr_warn("No valid WLED sinks found\n");
+		wled->force_mod_disable = true;
+		goto failed_calib;
+	}
+
+	/* write the new sink configuration */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			sink_config);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 439

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (rc < 0) {
+		pr_err("Failed to reconfigure the default sink rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* MODULATOR_EN setting for valid sinks */

"Enable valid sinks"

Will address it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 442

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (wled->cfg.en_cabc) {
+			reg = QCOM_WLED_SINK_CABC_EN;

"reg" is a bad name of a variable holding the "value" to be written to a register.

Will address it in the next series.

Quoted 6 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Again, just inline the value in the function call.

Will address it in the next series.

Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 450

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+
+		if (sink_config & (1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i)))

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i)

Will address it in the next series.

Quoted 41 lines
Quoted 28 lines
drivers/video/backlight/qcom-spmi-wled.c +28 -0
+			reg = QCOM_WLED_SINK_REG_STR_MOD_EN;
+		else
+			reg = 0x0; /* disable modulator_en for unused sink */
+
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_MOD_EN_REG(i), reg);
+		if (rc < 0) {
+			pr_err("Failed to configure MODULATOR_EN rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	/* restore the feedback setting */
+	rc = regmap_write(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FDBK_OP, 0);
+	if (rc < 0) {
+		pr_err("Failed to restore feedback setting rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* restore  brightness */
+	rc = qcom_wled_set_brightness(wled, wled->brightness);
+	if (rc < 0) {
+		pr_err("Failed to set brightness after calibration rc=%d\n",
+			rc);
+		goto failed_calib;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 488

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+			QCOM_WLED_CTRL_MOD_EN_MASK,
+			QCOM_WLED_CTRL_MOD_EN_MASK);
+	if (rc < 0) {
+		pr_err("Failed to enable WLED module rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* delay for WLED soft-start */

What comes after this that you want to delay?

Quoted 4 lines

This delay is used to make the OVP IRQ not fire immediately, but as we've now successfully executed the string auto detection run we're never going to do anything in the OVP handler.

Will correct it in the next series.

Quoted 36 lines
Quoted 36 lines
drivers/video/backlight/qcom-spmi-wled.c +36 -0
+	usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+			QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+failed_calib:
+	return rc;
+}
+
+#define WLED_AUTO_CAL_OVP_COUNT		5
+#define WLED_AUTO_CAL_CNT_DLY_US	1000000	/* 1 second */
+static bool qcom_wled_auto_cal_required(struct qcom_wled *wled)
+{
+	s64 elapsed_time_us;
+
+	/*
+	 * Check if the OVP fault was an occasional one
+	 * or if its firing continuously, the latter qualifies
+	 * for an auto-calibration check.
+	 */
+	if (!wled->auto_calibration_ovp_count) {
+		wled->start_ovp_fault_time = ktime_get();
+		wled->auto_calibration_ovp_count++;
+	} else {
+		elapsed_time_us = ktime_us_delta(ktime_get(),
+				wled->start_ovp_fault_time);
+		if (elapsed_time_us > WLED_AUTO_CAL_CNT_DLY_US)
+			wled->auto_calibration_ovp_count = 0;
+		else
+			wled->auto_calibration_ovp_count++;
+
+		if (wled->auto_calibration_ovp_count >=
+				WLED_AUTO_CAL_OVP_COUNT) {
+			wled->auto_calibration_ovp_count = 0;
+			return true;
+		}
+	}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 528

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	return false;
+}
+
+static int qcom_wled_auto_calibrate_at_init(struct qcom_wled *wled)

I presume this function is expected to detect if there is a invalid configuration at boot and try to figure out which strings are actually wired.

Correct.

Quoted 15 lines
Quoted 15 lines
drivers/video/backlight/qcom-spmi-wled.c +15 -0
+{
+	int rc;
+	u32 fault_status = 0, rt_status = 0;
+
+	if (!wled->cfg.auto_calib_enabled)
+		return 0;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS,
+			&rt_status);
+	if (rc < 0)
+		pr_err("Failed to read RT status rc=%d\n", rc);
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS,
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 549

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+			&fault_status);
+	if (rc < 0)
+		pr_err("Failed to read fault status rc=%d\n", rc);
+
+	if ((rt_status & QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT) ||
+			(fault_status & QCOM_WLED_CTRL_OVP_FAULT_BIT)) {

You should be able to drop the extra () around these.

Ok. Will remove it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 553

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+		mutex_lock(&wled->lock);
+		rc = qcom_wled_auto_calibrate(wled);
+		if (rc < 0)
+			pr_err("Failed auto-calibration rc=%d\n", rc);

qcom_wled_auto_calibrate() did already print, no need to repeat this.

Ok. Will remove this in the next series.

Quoted 25 lines
Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+		else
+			wled->auto_calib_done = true;
+		mutex_unlock(&wled->lock);
+	}
+
+	return rc;
+}
+
 static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)

{

drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	struct qcom_wled *wled = _wled;

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t · line 590

Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
@@ -319,6 +584,33 @@ static irqreturn_t
 qcom_wled_ovp_irq_handler(int irq, void *_wled)
 		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
 			int_sts, fault_sts);
 
+	if (fault_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT) {
+		if (wled->cfg.auto_calib_enabled && !wled->auto_calib_done) {
+			if (qcom_wled_auto_cal_required(wled)) {

So this will be invoked only once, iff we didn't boot with a faulty configuration in which case the qcom_wled_auto_calibrate_at_init() has already done this step and set auto_calib_done.

Quoted 6 lines

Which also would mean that all logic in this handler, beyond the printouts, are only ever going to be executed zero or one times.

Why don't you just do auto-detection during probe (iff the flag is set in DT) and you can remove all this extra logic?

I think we have seen a issue, where the OVP interrupt is not getting set some times during the execution of this function at boot. In that case the auto calibration is
done bit later. That's why this code is added.

Quoted 5 lines
Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+				mutex_lock(&wled->lock);
+				if (wled->cfg.ovp_irq > 0 &&
+						!wled->ovp_irq_disabled) {
+					disable_irq_nosync(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = true;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 600

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+				}
+
+				rc = qcom_wled_auto_calibrate(wled);
+				if (rc < 0)
+					pr_err("Failed auto-calibration rc=%d\n",
+						rc);

qcom_wled_auto_calibrate() did already print.

Quoted 22 lines

Ok. I will remove it in the next series.

Quoted 21 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 613

Quoted 13 lines
drivers/video/backlight/qcom-spmi-wled.c +13 -0
+				else
+					wled->auto_calib_done = true;
+
+				if (wled->cfg.ovp_irq > 0 &&
+						wled->ovp_irq_disabled) {
+					enable_irq(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = false;
+				}
+				mutex_unlock(&wled->lock);
+			}
+		}
+	}
+

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#25 lore raw

Re: [PATCH V1 4/4] qcom: spmi-wled: Add auto-calibration logic support

Quoted 3 lines · On Thu 19 Apr 22:43 PDT 2018, [email protected] wrote:
Quoted 2 lines · Bjorn Andersson
Quoted 1 line · On Thu 19 Apr 03:45 PDT 2018, [email protected] wrote:

On 2017-12-05 11:10, Bjorn Andersson wrote:

[..]

Quoted 11 lines
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

When is this feature needed?

This feature is needed if the string configuration is given wrong in the DT node by the user.

DT describes the hardware and for all other nodes it must do so accurately.

But the user may not be aware of the strings present on the display panel or may be using the same software on different devices which have different strings present.

Swapping display board would still require an update to the DTS, to support the new panel. But I think that if you're implementing auto string detection, it should be used as the mechanism to detect the strings, not something that is run at some later point in time when we detect a failure.

Quoted 9 lines
Quoted 6 lines

For cases where the hardware supports auto detection of functionality we remove information from DT and rely on that logic to figure out the hardware. We do not use it to reconfigure the hardware once we detect an error. So when auto-detection is enabled it should always be used to probe the hardware.

The auto string detection is not supported in any qcom hardware and i don't think there is a plan to introduce in new hardware also.

Sorry, I don't follow. Who is using auto string detection then?

Regards,
Bjorn

Quoted 515 lines
Quoted 515 lines

Regards,
Bjorn

Quoted 485 lines
Quoted 20 lines
Quoted 20 lines
---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |   5 +
 drivers/video/backlight/qcom-spmi-wled.c           | 304
++++++++++++++++++++-
 2 files changed, 306 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt index d39ee93..f06c0cd 100644
---
a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
+++
b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

(quoted diff) +0 -0
@@ -94,6 +94,11 @@ The PMIC is connected to the host processor via

SPMI bus.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
 	Definition: Interrupt names associated with the interrupts.
 		    Currently supported interrupts are "sc-irq" and "ovp-irq".
Quoted 4 lines
Quoted 1 line

+- qcom,auto-calibration

qcom,auto-string-detect?

ok. Will address in the next patch.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Enables auto-calibration of the WLED sink configuration.
+
 Example:
 
 qcom-wled@d800 {
drivers/video/backlight/qcom-spmi-wled.c +0 -0
index 8b2a77a..aee5c56 100644
@@ -38,11 +38,14 @@
 #define  QCOM_WLED_CTRL_SC_FAULT_BIT		BIT(2)
 
Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c line 41

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_INT_RT_STS		0x10
+#define  QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT	BIT(1)

The use of BIT() makes this a mask and not a bit number, so if you just drop that you can afford to spell out the "FAULT" like the data sheet does. Perhaps even making it QCOM_WLED_CTRL_OVP_FAULT_STATUS ?

ok. Will change it in the next series.

Quoted 10 lines

drivers/video/backlight/qcom-spmi-wled.c line 47

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
 
+#define QCOM_WLED_CTRL_FDBK_OP			0x48

This is called WLED_CTRL_FEEDBACK_CONTROL, why the need to make it unreadable?

Ok. Will address it in next series.

Quoted 42 lines
Quoted 42 lines
drivers/video/backlight/qcom-spmi-wled.c +9 -0
+
 #define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
 #define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
 
@@ -99,6 +102,7 @@ struct qcom_wled_config {
 	int ovp_irq;
 	bool en_cabc;
 	bool ext_pfet_sc_pro_en;
+	bool auto_calib_enabled;
 };
 
 struct qcom_wled {
@@ -108,18 +112,25 @@ struct qcom_wled {
 	struct mutex lock;
 	struct qcom_wled_config cfg;
 	ktime_t last_sc_event_time;
+	ktime_t start_ovp_fault_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
+	u16 auto_calibration_ovp_count;
 	u32 brightness;
 	u32 sc_count;
 	bool prev_state;
 	bool ovp_irq_disabled;
+	bool auto_calib_done;
+	bool force_mod_disable;
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
 {
 	int rc;
 
+	if (wled->force_mod_disable)
+		return 0;
+
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);
@@ -187,12 +198,10 @@ static int qcom_wled_set_brightness(struct
 qcom_wled *wled, u16 brightness)
 	v[1] = (brightness >> 8) & 0xf;
 
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness) · line 190

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 	for (i = 0; (string_cfg >> i) != 0; i++) {
-		if (string_cfg & BIT(i)) {

Why was this check here in the first place, if it's now fine to configure the brightness of all strings?

Also, a single-string config of 0b0001 will only set brightness on the first string, while 0b1000 will set brightness on all strings.

I will correct/remove it next series.

Quoted 32 lines
Quoted 32 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
 					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
 			if (rc < 0)
 				return rc;
-		}
 	}
 
drivers/video/backlight/qcom-spmi-wled.c +19 -0
 	return 0;
@@ -294,6 +303,262 @@ static irqreturn_t
 qcom_wled_sc_irq_handler(int irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+#define AUTO_CALIB_BRIGHTNESS		200
+static int qcom_wled_auto_calibrate(struct qcom_wled *wled)
+{
+	int rc = 0, i;
+	u32 sink_config = 0, int_sts;
+	u8 reg = 0, sink_test = 0, sink_valid = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	/* read configured sink configuration */
+	rc = regmap_read(wled->regmap, wled->sink_addr +
+			QCOM_WLED_SINK_CURR_SINK_EN, &sink_config);
+	if (rc < 0) {
+		pr_err("Failed to read SINK configuration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* disable the module before starting calibration */
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 329

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable WLED module rc=%d\n",	rc);
+		goto failed_calib;
+	}

Any error handling beyond this point seems to leave the backlight off (indefinitely?), this does seem like potentially bad user experience...

Ok. will address in next series.

Quoted 4 lines

In particular I wonder about the case when this would happen at some random time, minutes, hours, days, months after the device was booted.

This will happen for every reboot.

Quoted 8 lines
Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+
+	/* set low brightness across all sinks */
+	rc = qcom_wled_set_brightness(wled, AUTO_CALIB_BRIGHTNESS);
+	if (rc < 0) {
+		pr_err("Failed to set brightness for calibration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (wled->cfg.en_cabc) {
+		for (i = 0; (string_cfg >> i) != 0; i++) {
+			reg = 0;
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Just replace "reg" with 0.

Ok. will address in next series.

Quoted 11 lines
Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+	}
+
+	/* disable all sinks */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable all sinks rc=%d\n", rc);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 359

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		goto failed_calib;
+	}
+
+	/* iterate through the strings one by one */
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		sink_test = 1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i);

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i);

Will address in next series.

Quoted 19 lines
Quoted 19 lines
drivers/video/backlight/qcom-spmi-wled.c +19 -0
+
+		/* Enable feedback control */
+		rc = regmap_write(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_FDBK_OP, i + 1);
+		if (rc < 0) {
+			pr_err("Failed to enable feedback for SINK %d rc = %d\n",
+				i + 1, rc);
+			goto failed_calib;
+		}
+
+		/* enable the sink */
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_CURR_SINK_EN, sink_test);
+		if (rc < 0) {
+			pr_err("Failed to configure SINK %d rc=%d\n",
+						i + 1, rc);
+			goto failed_calib;
+		}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 383

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+		/* Enable the module */
+		rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK,
+				QCOM_WLED_CTRL_MOD_EN_MASK);

I like the use of regmap_update_bits(..., MASK, MASK) it's clean, but makes me wonder why it's done differently in qcom_wled_module_enable().

will address it in the next series.

Quoted 51 lines
Quoted 51 lines
drivers/video/backlight/qcom-spmi-wled.c +50 -0
+		if (rc < 0) {
+			pr_err("Failed to enable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+		rc = regmap_read(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+		if (rc < 0) {
+			pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		if (int_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT)
+			pr_debug("WLED OVP fault detected with SINK %d\n",
+						i + 1);
+		else
+			sink_valid |= sink_test;
+
+		/* Disable the module */
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+		if (rc < 0) {
+			pr_err("Failed to disable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	if (sink_valid == sink_config) {
+		pr_debug("WLED auto-calibration complete, default sink-config=%x
 OK!\n",
+						sink_config);
+	} else {
+		pr_warn("Invalid WLED default sink config=%x changing it to=%x\n",
+						sink_config, sink_valid);
+		sink_config = sink_valid;
+	}
+
+	if (!sink_config) {
+		pr_warn("No valid WLED sinks found\n");
+		wled->force_mod_disable = true;
+		goto failed_calib;
+	}
+
+	/* write the new sink configuration */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			sink_config);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 439

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (rc < 0) {
+		pr_err("Failed to reconfigure the default sink rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* MODULATOR_EN setting for valid sinks */

"Enable valid sinks"

Will address it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 442

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (wled->cfg.en_cabc) {
+			reg = QCOM_WLED_SINK_CABC_EN;

"reg" is a bad name of a variable holding the "value" to be written to a register.

Will address it in the next series.

Quoted 6 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Again, just inline the value in the function call.

Will address it in the next series.

Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 450

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+
+		if (sink_config & (1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i)))

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i)

Will address it in the next series.

Quoted 41 lines
Quoted 28 lines
drivers/video/backlight/qcom-spmi-wled.c +28 -0
+			reg = QCOM_WLED_SINK_REG_STR_MOD_EN;
+		else
+			reg = 0x0; /* disable modulator_en for unused sink */
+
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_MOD_EN_REG(i), reg);
+		if (rc < 0) {
+			pr_err("Failed to configure MODULATOR_EN rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	/* restore the feedback setting */
+	rc = regmap_write(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FDBK_OP, 0);
+	if (rc < 0) {
+		pr_err("Failed to restore feedback setting rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* restore  brightness */
+	rc = qcom_wled_set_brightness(wled, wled->brightness);
+	if (rc < 0) {
+		pr_err("Failed to set brightness after calibration rc=%d\n",
+			rc);
+		goto failed_calib;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 488

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+			QCOM_WLED_CTRL_MOD_EN_MASK,
+			QCOM_WLED_CTRL_MOD_EN_MASK);
+	if (rc < 0) {
+		pr_err("Failed to enable WLED module rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* delay for WLED soft-start */

What comes after this that you want to delay?

Quoted 4 lines

This delay is used to make the OVP IRQ not fire immediately, but as we've now successfully executed the string auto detection run we're never going to do anything in the OVP handler.

Will correct it in the next series.

Quoted 36 lines
Quoted 36 lines
drivers/video/backlight/qcom-spmi-wled.c +36 -0
+	usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+			QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+failed_calib:
+	return rc;
+}
+
+#define WLED_AUTO_CAL_OVP_COUNT		5
+#define WLED_AUTO_CAL_CNT_DLY_US	1000000	/* 1 second */
+static bool qcom_wled_auto_cal_required(struct qcom_wled *wled)
+{
+	s64 elapsed_time_us;
+
+	/*
+	 * Check if the OVP fault was an occasional one
+	 * or if its firing continuously, the latter qualifies
+	 * for an auto-calibration check.
+	 */
+	if (!wled->auto_calibration_ovp_count) {
+		wled->start_ovp_fault_time = ktime_get();
+		wled->auto_calibration_ovp_count++;
+	} else {
+		elapsed_time_us = ktime_us_delta(ktime_get(),
+				wled->start_ovp_fault_time);
+		if (elapsed_time_us > WLED_AUTO_CAL_CNT_DLY_US)
+			wled->auto_calibration_ovp_count = 0;
+		else
+			wled->auto_calibration_ovp_count++;
+
+		if (wled->auto_calibration_ovp_count >=
+				WLED_AUTO_CAL_OVP_COUNT) {
+			wled->auto_calibration_ovp_count = 0;
+			return true;
+		}
+	}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 528

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	return false;
+}
+
+static int qcom_wled_auto_calibrate_at_init(struct qcom_wled *wled)

I presume this function is expected to detect if there is a invalid configuration at boot and try to figure out which strings are actually wired.

Correct.

Quoted 15 lines
Quoted 15 lines
drivers/video/backlight/qcom-spmi-wled.c +15 -0
+{
+	int rc;
+	u32 fault_status = 0, rt_status = 0;
+
+	if (!wled->cfg.auto_calib_enabled)
+		return 0;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS,
+			&rt_status);
+	if (rc < 0)
+		pr_err("Failed to read RT status rc=%d\n", rc);
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS,
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 549

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+			&fault_status);
+	if (rc < 0)
+		pr_err("Failed to read fault status rc=%d\n", rc);
+
+	if ((rt_status & QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT) ||
+			(fault_status & QCOM_WLED_CTRL_OVP_FAULT_BIT)) {

You should be able to drop the extra () around these.

Ok. Will remove it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 553

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+		mutex_lock(&wled->lock);
+		rc = qcom_wled_auto_calibrate(wled);
+		if (rc < 0)
+			pr_err("Failed auto-calibration rc=%d\n", rc);

qcom_wled_auto_calibrate() did already print, no need to repeat this.

Ok. Will remove this in the next series.

Quoted 25 lines
Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+		else
+			wled->auto_calib_done = true;
+		mutex_unlock(&wled->lock);
+	}
+
+	return rc;
+}
+
 static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)

{

drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	struct qcom_wled *wled = _wled;

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t · line 590

Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
@@ -319,6 +584,33 @@ static irqreturn_t
 qcom_wled_ovp_irq_handler(int irq, void *_wled)
 		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
 			int_sts, fault_sts);
 
+	if (fault_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT) {
+		if (wled->cfg.auto_calib_enabled && !wled->auto_calib_done) {
+			if (qcom_wled_auto_cal_required(wled)) {

So this will be invoked only once, iff we didn't boot with a faulty configuration in which case the qcom_wled_auto_calibrate_at_init() has already done this step and set auto_calib_done.

Quoted 6 lines

Which also would mean that all logic in this handler, beyond the printouts, are only ever going to be executed zero or one times.

Why don't you just do auto-detection during probe (iff the flag is set in DT) and you can remove all this extra logic?

I think we have seen a issue, where the OVP interrupt is not getting set
some times during the execution of this function at boot. In that case the
auto calibration is
done bit later. That's why this code is added.

Quoted 5 lines
Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+				mutex_lock(&wled->lock);
+				if (wled->cfg.ovp_irq > 0 &&
+						!wled->ovp_irq_disabled) {
+					disable_irq_nosync(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = true;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 600

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+				}
+
+				rc = qcom_wled_auto_calibrate(wled);
+				if (rc < 0)
+					pr_err("Failed auto-calibration rc=%d\n",
+						rc);

qcom_wled_auto_calibrate() did already print.

Quoted 22 lines

Ok. I will remove it in the next series.

Quoted 21 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 613

Quoted 13 lines
drivers/video/backlight/qcom-spmi-wled.c +13 -0
+				else
+					wled->auto_calib_done = true;
+
+				if (wled->cfg.ovp_irq > 0 &&
+						wled->ovp_irq_disabled) {
+					enable_irq(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = false;
+				}
+				mutex_unlock(&wled->lock);
+			}
+		}
+	}
+

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#27 lore raw

Re: [PATCH V1 4/4] qcom: spmi-wled: Add auto-calibration logic support

In reply to [email protected]
Quoted 50 lines · On 2018-04-20 11:13, [email protected] wrote:
Quoted 14 lines · Bjorn Andersson
Quoted 12 lines · On Thu 19 Apr 03:45 PDT 2018, [email protected] wrote:
Quoted 10 lines · Bjorn Andersson
Quoted 7 lines · Kiran Gunda

The auto-calibration algorithm checks if the current WLED sink configuration is valid. It tries enabling every sink and checks if the OVP fault is observed. Based on this information it detects and enables the valid sink configuration. Auto calibration will be triggered when the OVP fault interrupts are seen frequently thereby it tries to fix the sink configuration.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

So it's not auto "calibration" it's auto "detection" of strings?

Hi Bjorn,
Sorry for late response. Please find my answers.

No worries, happy to hear back from you!

Thanks!

Quoted 7 lines
Quoted 2 lines

Correct. This is the auto detection, This is the name given by the HW/systems team.

I think the name should be considered a "hardware bug", that we can work around in software (give it a useful name and document what the original name was).

I don't think this is the "hardware bug". Rather we can say HW doesn't support it.
Hence, we are implementing it as a SW feature to detect the strings present on the
display panel, if the user fails to give the correct strings. As you suggested I will
rename this to "auto detection" instead of "auto calibration".

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

When is this feature needed?

This feature is needed if the string configuration is given wrong in the DT node by the user.

DT describes the hardware and for all other nodes it must do so accurately.

But the user may not be aware of the strings present on the display panel or may be using the same software on different devices which have different strings present.

Quoted 165 lines
Quoted 6 lines

For cases where the hardware supports auto detection of functionality we remove information from DT and rely on that logic to figure out the hardware. We do not use it to reconfigure the hardware once we detect an error. So when auto-detection is enabled it should always be used to probe the hardware.

The auto string detection is not supported in any qcom hardware and i don't think there is a plan to introduce in new hardware also.

Quoted 156 lines

Regards,
Bjorn

Quoted 153 lines
Quoted 20 lines
Quoted 20 lines
---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |   5 +
 drivers/video/backlight/qcom-spmi-wled.c           | 304
++++++++++++++++++++-
 2 files changed, 306 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt index d39ee93..f06c0cd 100644
---
a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
+++
b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

(quoted diff) +0 -0
@@ -94,6 +94,11 @@ The PMIC is connected to the host processor via

SPMI bus.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
 	Definition: Interrupt names associated with the interrupts.
 		    Currently supported interrupts are "sc-irq" and "ovp-irq".
Quoted 4 lines
Quoted 1 line

+- qcom,auto-calibration

qcom,auto-string-detect?

ok. Will address in the next patch.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Enables auto-calibration of the WLED sink configuration.
+
 Example:
 
 qcom-wled@d800 {
drivers/video/backlight/qcom-spmi-wled.c +0 -0
index 8b2a77a..aee5c56 100644
@@ -38,11 +38,14 @@
 #define  QCOM_WLED_CTRL_SC_FAULT_BIT		BIT(2)
 
Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c line 41

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_INT_RT_STS		0x10
+#define  QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT	BIT(1)

The use of BIT() makes this a mask and not a bit number, so if you just drop that you can afford to spell out the "FAULT" like the data sheet does. Perhaps even making it QCOM_WLED_CTRL_OVP_FAULT_STATUS ?

ok. Will change it in the next series.

Quoted 10 lines

drivers/video/backlight/qcom-spmi-wled.c line 47

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
 
+#define QCOM_WLED_CTRL_FDBK_OP			0x48

This is called WLED_CTRL_FEEDBACK_CONTROL, why the need to make it unreadable?

Ok. Will address it in next series.

Quoted 42 lines
Quoted 42 lines
drivers/video/backlight/qcom-spmi-wled.c +9 -0
+
 #define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
 #define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
 
@@ -99,6 +102,7 @@ struct qcom_wled_config {
 	int ovp_irq;
 	bool en_cabc;
 	bool ext_pfet_sc_pro_en;
+	bool auto_calib_enabled;
 };
 
 struct qcom_wled {
@@ -108,18 +112,25 @@ struct qcom_wled {
 	struct mutex lock;
 	struct qcom_wled_config cfg;
 	ktime_t last_sc_event_time;
+	ktime_t start_ovp_fault_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
+	u16 auto_calibration_ovp_count;
 	u32 brightness;
 	u32 sc_count;
 	bool prev_state;
 	bool ovp_irq_disabled;
+	bool auto_calib_done;
+	bool force_mod_disable;
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
 {
 	int rc;
 
+	if (wled->force_mod_disable)
+		return 0;
+
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);
@@ -187,12 +198,10 @@ static int qcom_wled_set_brightness(struct
 qcom_wled *wled, u16 brightness)
 	v[1] = (brightness >> 8) & 0xf;
 
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness) · line 190

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 	for (i = 0; (string_cfg >> i) != 0; i++) {
-		if (string_cfg & BIT(i)) {

Why was this check here in the first place, if it's now fine to configure the brightness of all strings?

Also, a single-string config of 0b0001 will only set brightness on the first string, while 0b1000 will set brightness on all strings.

I will correct/remove it next series.

Quoted 32 lines
Quoted 32 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
 					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
 			if (rc < 0)
 				return rc;
-		}
 	}
 
drivers/video/backlight/qcom-spmi-wled.c +19 -0
 	return 0;
@@ -294,6 +303,262 @@ static irqreturn_t
 qcom_wled_sc_irq_handler(int irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+#define AUTO_CALIB_BRIGHTNESS		200
+static int qcom_wled_auto_calibrate(struct qcom_wled *wled)
+{
+	int rc = 0, i;
+	u32 sink_config = 0, int_sts;
+	u8 reg = 0, sink_test = 0, sink_valid = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	/* read configured sink configuration */
+	rc = regmap_read(wled->regmap, wled->sink_addr +
+			QCOM_WLED_SINK_CURR_SINK_EN, &sink_config);
+	if (rc < 0) {
+		pr_err("Failed to read SINK configuration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* disable the module before starting calibration */
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 329

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable WLED module rc=%d\n",	rc);
+		goto failed_calib;
+	}

Any error handling beyond this point seems to leave the backlight off (indefinitely?), this does seem like potentially bad user experience...

Ok. will address in next series.

Quoted 1 line

It is very unlikely to hit the error path in the below code. As the below code just deals with the registers read/writes through the SPMI interface, which is a stable interface. In worst case if see the errors, there is no use of bypassing the error handling as the register write itself doesn't go through and the complete system gets effected. Having error handling at-least gives some information.

Quoted 356 lines
Quoted 356 lines
Quoted 330 lines
Quoted 3 lines

In particular I wonder about the case when this would happen at some random time, minutes, hours, days, months after the device was booted.

This will happen for every reboot.

Quoted 8 lines
Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+
+	/* set low brightness across all sinks */
+	rc = qcom_wled_set_brightness(wled, AUTO_CALIB_BRIGHTNESS);
+	if (rc < 0) {
+		pr_err("Failed to set brightness for calibration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (wled->cfg.en_cabc) {
+		for (i = 0; (string_cfg >> i) != 0; i++) {
+			reg = 0;
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Just replace "reg" with 0.

Ok. will address in next series.

Quoted 11 lines
Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+	}
+
+	/* disable all sinks */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable all sinks rc=%d\n", rc);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 359

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		goto failed_calib;
+	}
+
+	/* iterate through the strings one by one */
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		sink_test = 1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i);

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i);

Will address in next series.

Quoted 19 lines
Quoted 19 lines
drivers/video/backlight/qcom-spmi-wled.c +19 -0
+
+		/* Enable feedback control */
+		rc = regmap_write(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_FDBK_OP, i + 1);
+		if (rc < 0) {
+			pr_err("Failed to enable feedback for SINK %d rc = %d\n",
+				i + 1, rc);
+			goto failed_calib;
+		}
+
+		/* enable the sink */
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_CURR_SINK_EN, sink_test);
+		if (rc < 0) {
+			pr_err("Failed to configure SINK %d rc=%d\n",
+						i + 1, rc);
+			goto failed_calib;
+		}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 383

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+		/* Enable the module */
+		rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK,
+				QCOM_WLED_CTRL_MOD_EN_MASK);

I like the use of regmap_update_bits(..., MASK, MASK) it's clean, but makes me wonder why it's done differently in qcom_wled_module_enable().

will address it in the next series.

Quoted 51 lines
Quoted 51 lines
drivers/video/backlight/qcom-spmi-wled.c +50 -0
+		if (rc < 0) {
+			pr_err("Failed to enable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+		rc = regmap_read(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+		if (rc < 0) {
+			pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		if (int_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT)
+			pr_debug("WLED OVP fault detected with SINK %d\n",
+						i + 1);
+		else
+			sink_valid |= sink_test;
+
+		/* Disable the module */
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+		if (rc < 0) {
+			pr_err("Failed to disable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	if (sink_valid == sink_config) {
+		pr_debug("WLED auto-calibration complete, default sink-config=%x
 OK!\n",
+						sink_config);
+	} else {
+		pr_warn("Invalid WLED default sink config=%x changing it to=%x\n",
+						sink_config, sink_valid);
+		sink_config = sink_valid;
+	}
+
+	if (!sink_config) {
+		pr_warn("No valid WLED sinks found\n");
+		wled->force_mod_disable = true;
+		goto failed_calib;
+	}
+
+	/* write the new sink configuration */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			sink_config);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 439

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (rc < 0) {
+		pr_err("Failed to reconfigure the default sink rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* MODULATOR_EN setting for valid sinks */

"Enable valid sinks"

Will address it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 442

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (wled->cfg.en_cabc) {
+			reg = QCOM_WLED_SINK_CABC_EN;

"reg" is a bad name of a variable holding the "value" to be written to a register.

Will address it in the next series.

Quoted 6 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Again, just inline the value in the function call.

Will address it in the next series.

Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 450

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+
+		if (sink_config & (1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i)))

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i)

Will address it in the next series.

Quoted 41 lines
Quoted 28 lines
drivers/video/backlight/qcom-spmi-wled.c +28 -0
+			reg = QCOM_WLED_SINK_REG_STR_MOD_EN;
+		else
+			reg = 0x0; /* disable modulator_en for unused sink */
+
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_MOD_EN_REG(i), reg);
+		if (rc < 0) {
+			pr_err("Failed to configure MODULATOR_EN rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	/* restore the feedback setting */
+	rc = regmap_write(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FDBK_OP, 0);
+	if (rc < 0) {
+		pr_err("Failed to restore feedback setting rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* restore  brightness */
+	rc = qcom_wled_set_brightness(wled, wled->brightness);
+	if (rc < 0) {
+		pr_err("Failed to set brightness after calibration rc=%d\n",
+			rc);
+		goto failed_calib;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 488

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+			QCOM_WLED_CTRL_MOD_EN_MASK,
+			QCOM_WLED_CTRL_MOD_EN_MASK);
+	if (rc < 0) {
+		pr_err("Failed to enable WLED module rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* delay for WLED soft-start */

What comes after this that you want to delay?

Quoted 4 lines

This delay is used to make the OVP IRQ not fire immediately, but as we've now successfully executed the string auto detection run we're never going to do anything in the OVP handler.

Will correct it in the next series.

Quoted 36 lines
Quoted 36 lines
drivers/video/backlight/qcom-spmi-wled.c +36 -0
+	usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+			QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+failed_calib:
+	return rc;
+}
+
+#define WLED_AUTO_CAL_OVP_COUNT		5
+#define WLED_AUTO_CAL_CNT_DLY_US	1000000	/* 1 second */
+static bool qcom_wled_auto_cal_required(struct qcom_wled *wled)
+{
+	s64 elapsed_time_us;
+
+	/*
+	 * Check if the OVP fault was an occasional one
+	 * or if its firing continuously, the latter qualifies
+	 * for an auto-calibration check.
+	 */
+	if (!wled->auto_calibration_ovp_count) {
+		wled->start_ovp_fault_time = ktime_get();
+		wled->auto_calibration_ovp_count++;
+	} else {
+		elapsed_time_us = ktime_us_delta(ktime_get(),
+				wled->start_ovp_fault_time);
+		if (elapsed_time_us > WLED_AUTO_CAL_CNT_DLY_US)
+			wled->auto_calibration_ovp_count = 0;
+		else
+			wled->auto_calibration_ovp_count++;
+
+		if (wled->auto_calibration_ovp_count >=
+				WLED_AUTO_CAL_OVP_COUNT) {
+			wled->auto_calibration_ovp_count = 0;
+			return true;
+		}
+	}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 528

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	return false;
+}
+
+static int qcom_wled_auto_calibrate_at_init(struct qcom_wled *wled)

I presume this function is expected to detect if there is a invalid configuration at boot and try to figure out which strings are actually wired.

Correct.

Quoted 15 lines
Quoted 15 lines
drivers/video/backlight/qcom-spmi-wled.c +15 -0
+{
+	int rc;
+	u32 fault_status = 0, rt_status = 0;
+
+	if (!wled->cfg.auto_calib_enabled)
+		return 0;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS,
+			&rt_status);
+	if (rc < 0)
+		pr_err("Failed to read RT status rc=%d\n", rc);
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS,
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 549

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+			&fault_status);
+	if (rc < 0)
+		pr_err("Failed to read fault status rc=%d\n", rc);
+
+	if ((rt_status & QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT) ||
+			(fault_status & QCOM_WLED_CTRL_OVP_FAULT_BIT)) {

You should be able to drop the extra () around these.

Ok. Will remove it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 553

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+		mutex_lock(&wled->lock);
+		rc = qcom_wled_auto_calibrate(wled);
+		if (rc < 0)
+			pr_err("Failed auto-calibration rc=%d\n", rc);

qcom_wled_auto_calibrate() did already print, no need to repeat this.

Ok. Will remove this in the next series.

Quoted 25 lines
Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+		else
+			wled->auto_calib_done = true;
+		mutex_unlock(&wled->lock);
+	}
+
+	return rc;
+}
+
 static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)

{

drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	struct qcom_wled *wled = _wled;

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t · line 590

Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
@@ -319,6 +584,33 @@ static irqreturn_t
 qcom_wled_ovp_irq_handler(int irq, void *_wled)
 		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
 			int_sts, fault_sts);
 
+	if (fault_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT) {
+		if (wled->cfg.auto_calib_enabled && !wled->auto_calib_done) {
+			if (qcom_wled_auto_cal_required(wled)) {

So this will be invoked only once, iff we didn't boot with a faulty configuration in which case the qcom_wled_auto_calibrate_at_init() has already done this step and set auto_calib_done.

Quoted 6 lines

Which also would mean that all logic in this handler, beyond the printouts, are only ever going to be executed zero or one times.

Why don't you just do auto-detection during probe (iff the flag is set in DT) and you can remove all this extra logic?

I think we have seen a issue, where the OVP interrupt is not getting set some times during the execution of this function at boot. In that case the auto calibration is
done bit later. That's why this code is added.

Quoted 5 lines
Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+				mutex_lock(&wled->lock);
+				if (wled->cfg.ovp_irq > 0 &&
+						!wled->ovp_irq_disabled) {
+					disable_irq_nosync(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = true;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 600

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+				}
+
+				rc = qcom_wled_auto_calibrate(wled);
+				if (rc < 0)
+					pr_err("Failed auto-calibration rc=%d\n",
+						rc);

qcom_wled_auto_calibrate() did already print.

Quoted 22 lines

Ok. I will remove it in the next series.

Quoted 21 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 613

Quoted 13 lines
drivers/video/backlight/qcom-spmi-wled.c +13 -0
+				else
+					wled->auto_calib_done = true;
+
+				if (wled->cfg.ovp_irq > 0 &&
+						wled->ovp_irq_disabled) {
+					enable_irq(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = false;
+				}
+				mutex_unlock(&wled->lock);
+			}
+		}
+	}
+

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#26 lore raw

Re: [PATCH V1 4/4] qcom: spmi-wled: Add auto-calibration logic support

In reply to Bjorn Andersson
Quoted 18 lines · Bjorn Andersson
Quoted 3 lines · On Thu 19 Apr 22:43 PDT 2018, [email protected] wrote:
Quoted 2 lines · Bjorn Andersson
Quoted 1 line · On Thu 19 Apr 03:45 PDT 2018, [email protected] wrote:

On 2017-12-05 11:10, Bjorn Andersson wrote:

[..]

Quoted 11 lines
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

When is this feature needed?

This feature is needed if the string configuration is given wrong in the DT node by the user.

DT describes the hardware and for all other nodes it must do so accurately.

But the user may not be aware of the strings present on the display panel or may be using the same software on different devices which have different strings present.

Quoted 6 lines

Swapping display board would still require an update to the DTS, to support the new panel. But I think that if you're implementing auto string detection, it should be used as the mechanism to detect the strings, not something that is run at some later point in time when we detect a failure.

Ok. got your point. Yes, the detection is done during the boot-up itself, not later point.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

For cases where the hardware supports auto detection of functionality we remove information from DT and rely on that logic to figure out the hardware. We do not use it to reconfigure the hardware once we detect an error. So when auto-detection is enabled it should always be used to probe the hardware.

Quoted 6 lines
Quoted 3 lines

The auto string detection is not supported in any qcom hardware and i don't think there is a plan to introduce in new hardware also.

Sorry, I don't follow. Who is using auto string detection then?

I mean to say. The HW doesn't handle the auto-detection completely in the HW without having the S/W intervention, which we are using now.

Quoted 522 lines

Regards,
Bjorn

Quoted 510 lines
Quoted 510 lines

Regards,
Bjorn

Quoted 507 lines
Quoted 20 lines
Quoted 20 lines
---
 .../bindings/leds/backlight/qcom-spmi-wled.txt     |   5 +
 drivers/video/backlight/qcom-spmi-wled.c           | 304
++++++++++++++++++++-
 2 files changed, 306 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt index d39ee93..f06c0cd 100644
---
a/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt
+++
b/Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt

(quoted diff) +0 -0
@@ -94,6 +94,11 @@ The PMIC is connected to the host processor via

SPMI bus.

Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +0 -0
 	Definition: Interrupt names associated with the interrupts.
 		    Currently supported interrupts are "sc-irq" and "ovp-irq".
Quoted 4 lines
Quoted 1 line

+- qcom,auto-calibration

qcom,auto-string-detect?

ok. Will address in the next patch.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/leds/backlight/qcom-spmi-wled.txt +4 -0
+	Usage:      optional
+	Value type: <bool>
+	Definition: Enables auto-calibration of the WLED sink configuration.
+
 Example:
 
 qcom-wled@d800 {
drivers/video/backlight/qcom-spmi-wled.c +0 -0
index 8b2a77a..aee5c56 100644
@@ -38,11 +38,14 @@
 #define  QCOM_WLED_CTRL_SC_FAULT_BIT		BIT(2)
 
Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c line 41

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_INT_RT_STS		0x10
+#define  QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT	BIT(1)

The use of BIT() makes this a mask and not a bit number, so if you just drop that you can afford to spell out the "FAULT" like the data sheet does. Perhaps even making it QCOM_WLED_CTRL_OVP_FAULT_STATUS ?

ok. Will change it in the next series.

Quoted 10 lines

drivers/video/backlight/qcom-spmi-wled.c line 47

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +1 -0
 #define QCOM_WLED_CTRL_MOD_ENABLE		0x46
 #define  QCOM_WLED_CTRL_MOD_EN_MASK		BIT(7)
 #define  QCOM_WLED_CTRL_MODULE_EN_SHIFT		7
 
+#define QCOM_WLED_CTRL_FDBK_OP			0x48

This is called WLED_CTRL_FEEDBACK_CONTROL, why the need to make it unreadable?

Ok. Will address it in next series.

Quoted 42 lines
Quoted 42 lines
drivers/video/backlight/qcom-spmi-wled.c +9 -0
+
 #define QCOM_WLED_CTRL_SWITCH_FREQ		0x4c
 #define  QCOM_WLED_CTRL_SWITCH_FREQ_MASK	GENMASK(3, 0)
 
@@ -99,6 +102,7 @@ struct qcom_wled_config {
 	int ovp_irq;
 	bool en_cabc;
 	bool ext_pfet_sc_pro_en;
+	bool auto_calib_enabled;
 };
 
 struct qcom_wled {
@@ -108,18 +112,25 @@ struct qcom_wled {
 	struct mutex lock;
 	struct qcom_wled_config cfg;
 	ktime_t last_sc_event_time;
+	ktime_t start_ovp_fault_time;
 	u16 sink_addr;
 	u16 ctrl_addr;
+	u16 auto_calibration_ovp_count;
 	u32 brightness;
 	u32 sc_count;
 	bool prev_state;
 	bool ovp_irq_disabled;
+	bool auto_calib_done;
+	bool force_mod_disable;
 };
 
 static int qcom_wled_module_enable(struct qcom_wled *wled, int val)
 {
 	int rc;
 
+	if (wled->force_mod_disable)
+		return 0;
+
 	rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
 			QCOM_WLED_CTRL_MOD_ENABLE, QCOM_WLED_CTRL_MOD_EN_MASK,
 			val << QCOM_WLED_CTRL_MODULE_EN_SHIFT);
@@ -187,12 +198,10 @@ static int qcom_wled_set_brightness(struct
 qcom_wled *wled, u16 brightness)
 	v[1] = (brightness >> 8) & 0xf;
 
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static int qcom_wled_set_brightness(struct qcom_wled *wled, u16 brightness) · line 190

Quoted 2 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 	for (i = 0; (string_cfg >> i) != 0; i++) {
-		if (string_cfg & BIT(i)) {

Why was this check here in the first place, if it's now fine to configure the brightness of all strings?

Also, a single-string config of 0b0001 will only set brightness on the first string, while 0b1000 will set brightness on all strings.

I will correct/remove it next series.

Quoted 32 lines
Quoted 32 lines
drivers/video/backlight/qcom-spmi-wled.c +0 -1
 			rc = regmap_bulk_write(wled->regmap, wled->sink_addr +
 					QCOM_WLED_SINK_BRIGHT_LSB_REG(i), v, 2);
 			if (rc < 0)
 				return rc;
-		}
 	}
 
drivers/video/backlight/qcom-spmi-wled.c +19 -0
 	return 0;
@@ -294,6 +303,262 @@ static irqreturn_t
 qcom_wled_sc_irq_handler(int irq, void *_wled)
 	return IRQ_HANDLED;
 }
 
+#define AUTO_CALIB_BRIGHTNESS		200
+static int qcom_wled_auto_calibrate(struct qcom_wled *wled)
+{
+	int rc = 0, i;
+	u32 sink_config = 0, int_sts;
+	u8 reg = 0, sink_test = 0, sink_valid = 0;
+	u8 string_cfg = wled->cfg.string_cfg;
+
+	/* read configured sink configuration */
+	rc = regmap_read(wled->regmap, wled->sink_addr +
+			QCOM_WLED_SINK_CURR_SINK_EN, &sink_config);
+	if (rc < 0) {
+		pr_err("Failed to read SINK configuration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* disable the module before starting calibration */
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 329

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable WLED module rc=%d\n",	rc);
+		goto failed_calib;
+	}

Any error handling beyond this point seems to leave the backlight off (indefinitely?), this does seem like potentially bad user experience...

Ok. will address in next series.

Quoted 4 lines

In particular I wonder about the case when this would happen at some random time, minutes, hours, days, months after the device was booted.

This will happen for every reboot.

Quoted 8 lines
Quoted 8 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+
+	/* set low brightness across all sinks */
+	rc = qcom_wled_set_brightness(wled, AUTO_CALIB_BRIGHTNESS);
+	if (rc < 0) {
+		pr_err("Failed to set brightness for calibration rc=%d\n", rc);
+		goto failed_calib;
+	}
+
Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (wled->cfg.en_cabc) {
+		for (i = 0; (string_cfg >> i) != 0; i++) {
+			reg = 0;
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Just replace "reg" with 0.

Ok. will address in next series.

Quoted 11 lines
Quoted 11 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+	}
+
+	/* disable all sinks */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN, 0);
+	if (rc < 0) {
+		pr_err("Failed to disable all sinks rc=%d\n", rc);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 359

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+		goto failed_calib;
+	}
+
+	/* iterate through the strings one by one */
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		sink_test = 1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i);

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i);

Will address in next series.

Quoted 19 lines
Quoted 19 lines
drivers/video/backlight/qcom-spmi-wled.c +19 -0
+
+		/* Enable feedback control */
+		rc = regmap_write(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_FDBK_OP, i + 1);
+		if (rc < 0) {
+			pr_err("Failed to enable feedback for SINK %d rc = %d\n",
+				i + 1, rc);
+			goto failed_calib;
+		}
+
+		/* enable the sink */
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_CURR_SINK_EN, sink_test);
+		if (rc < 0) {
+			pr_err("Failed to configure SINK %d rc=%d\n",
+						i + 1, rc);
+			goto failed_calib;
+		}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 383

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+		/* Enable the module */
+		rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK,
+				QCOM_WLED_CTRL_MOD_EN_MASK);

I like the use of regmap_update_bits(..., MASK, MASK) it's clean, but makes me wonder why it's done differently in qcom_wled_module_enable().

will address it in the next series.

Quoted 51 lines
Quoted 51 lines
drivers/video/backlight/qcom-spmi-wled.c +50 -0
+		if (rc < 0) {
+			pr_err("Failed to enable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+				QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+		rc = regmap_read(wled->regmap, wled->ctrl_addr +
+				QCOM_WLED_CTRL_INT_RT_STS, &int_sts);
+		if (rc < 0) {
+			pr_err("Error in reading WLED_INT_RT_STS rc=%d\n", rc);
+			goto failed_calib;
+		}
+
+		if (int_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT)
+			pr_debug("WLED OVP fault detected with SINK %d\n",
+						i + 1);
+		else
+			sink_valid |= sink_test;
+
+		/* Disable the module */
+		rc = regmap_update_bits(wled->regmap,
+				wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+				QCOM_WLED_CTRL_MOD_EN_MASK, 0);
+		if (rc < 0) {
+			pr_err("Failed to disable WLED module rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	if (sink_valid == sink_config) {
+		pr_debug("WLED auto-calibration complete, default sink-config=%x
 OK!\n",
+						sink_config);
+	} else {
+		pr_warn("Invalid WLED default sink config=%x changing it to=%x\n",
+						sink_config, sink_valid);
+		sink_config = sink_valid;
+	}
+
+	if (!sink_config) {
+		pr_warn("No valid WLED sinks found\n");
+		wled->force_mod_disable = true;
+		goto failed_calib;
+	}
+
+	/* write the new sink configuration */
+	rc = regmap_write(wled->regmap,
+			wled->sink_addr + QCOM_WLED_SINK_CURR_SINK_EN,
+			sink_config);
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 439

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+	if (rc < 0) {
+		pr_err("Failed to reconfigure the default sink rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* MODULATOR_EN setting for valid sinks */

"Enable valid sinks"

Will address it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 442

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+	for (i = 0; (string_cfg >> i) != 0; i++) {
+		if (wled->cfg.en_cabc) {
+			reg = QCOM_WLED_SINK_CABC_EN;

"reg" is a bad name of a variable holding the "value" to be written to a register.

Will address it in the next series.

Quoted 6 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 343

Quoted 3 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
+			rc = regmap_update_bits(wled->regmap, wled->sink_addr +
+					QCOM_WLED_SINK_CABC_REG(i),
+					QCOM_WLED_SINK_CABC_MASK, reg);

Again, just inline the value in the function call.

Will address it in the next series.

Quoted 8 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 450

Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+			if (rc < 0)
+				goto failed_calib;
+		}
+
+		if (sink_config & (1 << (QCOM_WLED_SINK_CURR_SINK_SHFT + i)))

BIT(QCOM_WLED_SINK_CURR_SINK_SHFT + i)

Will address it in the next series.

Quoted 41 lines
Quoted 28 lines
drivers/video/backlight/qcom-spmi-wled.c +28 -0
+			reg = QCOM_WLED_SINK_REG_STR_MOD_EN;
+		else
+			reg = 0x0; /* disable modulator_en for unused sink */
+
+		rc = regmap_write(wled->regmap, wled->sink_addr +
+				QCOM_WLED_SINK_MOD_EN_REG(i), reg);
+		if (rc < 0) {
+			pr_err("Failed to configure MODULATOR_EN rc=%d\n", rc);
+			goto failed_calib;
+		}
+	}
+
+	/* restore the feedback setting */
+	rc = regmap_write(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FDBK_OP, 0);
+	if (rc < 0) {
+		pr_err("Failed to restore feedback setting rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* restore  brightness */
+	rc = qcom_wled_set_brightness(wled, wled->brightness);
+	if (rc < 0) {
+		pr_err("Failed to set brightness after calibration rc=%d\n",
+			rc);
+		goto failed_calib;
+	}
+

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 488

Quoted 10 lines
drivers/video/backlight/qcom-spmi-wled.c +10 -0
+	rc = regmap_update_bits(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_MOD_ENABLE,
+			QCOM_WLED_CTRL_MOD_EN_MASK,
+			QCOM_WLED_CTRL_MOD_EN_MASK);
+	if (rc < 0) {
+		pr_err("Failed to enable WLED module rc=%d\n", rc);
+		goto failed_calib;
+	}
+
+	/* delay for WLED soft-start */

What comes after this that you want to delay?

Quoted 4 lines

This delay is used to make the OVP IRQ not fire immediately, but as we've now successfully executed the string auto detection run we're never going to do anything in the OVP handler.

Will correct it in the next series.

Quoted 36 lines
Quoted 36 lines
drivers/video/backlight/qcom-spmi-wled.c +36 -0
+	usleep_range(QCOM_WLED_SOFT_START_DLY_US,
+			QCOM_WLED_SOFT_START_DLY_US + 1000);
+
+failed_calib:
+	return rc;
+}
+
+#define WLED_AUTO_CAL_OVP_COUNT		5
+#define WLED_AUTO_CAL_CNT_DLY_US	1000000	/* 1 second */
+static bool qcom_wled_auto_cal_required(struct qcom_wled *wled)
+{
+	s64 elapsed_time_us;
+
+	/*
+	 * Check if the OVP fault was an occasional one
+	 * or if its firing continuously, the latter qualifies
+	 * for an auto-calibration check.
+	 */
+	if (!wled->auto_calibration_ovp_count) {
+		wled->start_ovp_fault_time = ktime_get();
+		wled->auto_calibration_ovp_count++;
+	} else {
+		elapsed_time_us = ktime_us_delta(ktime_get(),
+				wled->start_ovp_fault_time);
+		if (elapsed_time_us > WLED_AUTO_CAL_CNT_DLY_US)
+			wled->auto_calibration_ovp_count = 0;
+		else
+			wled->auto_calibration_ovp_count++;
+
+		if (wled->auto_calibration_ovp_count >=
+				WLED_AUTO_CAL_OVP_COUNT) {
+			wled->auto_calibration_ovp_count = 0;
+			return true;
+		}
+	}
+
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 528

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+	return false;
+}
+
+static int qcom_wled_auto_calibrate_at_init(struct qcom_wled *wled)

I presume this function is expected to detect if there is a invalid configuration at boot and try to figure out which strings are actually wired.

Correct.

Quoted 15 lines
Quoted 15 lines
drivers/video/backlight/qcom-spmi-wled.c +15 -0
+{
+	int rc;
+	u32 fault_status = 0, rt_status = 0;
+
+	if (!wled->cfg.auto_calib_enabled)
+		return 0;
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_INT_RT_STS,
+			&rt_status);
+	if (rc < 0)
+		pr_err("Failed to read RT status rc=%d\n", rc);
+
+	rc = regmap_read(wled->regmap,
+			wled->ctrl_addr + QCOM_WLED_CTRL_FAULT_STATUS,
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 549

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+			&fault_status);
+	if (rc < 0)
+		pr_err("Failed to read fault status rc=%d\n", rc);
+
+	if ((rt_status & QCOM_WLED_CTRL_OVP_FLT_RT_STS_BIT) ||
+			(fault_status & QCOM_WLED_CTRL_OVP_FAULT_BIT)) {

You should be able to drop the extra () around these.

Ok. Will remove it in the next series.

Quoted 7 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_sc_irq_handler(int irq, void *_wled) · line 553

Quoted 4 lines
drivers/video/backlight/qcom-spmi-wled.c +4 -0
+		mutex_lock(&wled->lock);
+		rc = qcom_wled_auto_calibrate(wled);
+		if (rc < 0)
+			pr_err("Failed auto-calibration rc=%d\n", rc);

qcom_wled_auto_calibrate() did already print, no need to repeat this.

Ok. Will remove this in the next series.

Quoted 25 lines
Quoted 12 lines
drivers/video/backlight/qcom-spmi-wled.c +8 -0
+		else
+			wled->auto_calib_done = true;
+		mutex_unlock(&wled->lock);
+	}
+
+	return rc;
+}
+
 static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled)

{

drivers/video/backlight/qcom-spmi-wled.c +0 -0
 	struct qcom_wled *wled = _wled;

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t · line 590

Quoted 7 lines
drivers/video/backlight/qcom-spmi-wled.c +3 -0
@@ -319,6 +584,33 @@ static irqreturn_t
 qcom_wled_ovp_irq_handler(int irq, void *_wled)
 		pr_err("WLED OVP fault detected, int_sts=%x fault_sts= %x\n",
 			int_sts, fault_sts);
 
+	if (fault_sts & QCOM_WLED_CTRL_OVP_FAULT_BIT) {
+		if (wled->cfg.auto_calib_enabled && !wled->auto_calib_done) {
+			if (qcom_wled_auto_cal_required(wled)) {

So this will be invoked only once, iff we didn't boot with a faulty configuration in which case the qcom_wled_auto_calibrate_at_init() has already done this step and set auto_calib_done.

Quoted 6 lines

Which also would mean that all logic in this handler, beyond the printouts, are only ever going to be executed zero or one times.

Why don't you just do auto-detection during probe (iff the flag is set in DT) and you can remove all this extra logic?

I think we have seen a issue, where the OVP interrupt is not getting set
some times during the execution of this function at boot. In that case the
auto calibration is
done bit later. That's why this code is added.

Quoted 5 lines
Quoted 5 lines
drivers/video/backlight/qcom-spmi-wled.c +5 -0
+				mutex_lock(&wled->lock);
+				if (wled->cfg.ovp_irq > 0 &&
+						!wled->ovp_irq_disabled) {
+					disable_irq_nosync(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = true;
Quoted 9 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 600

Quoted 6 lines
drivers/video/backlight/qcom-spmi-wled.c +6 -0
+				}
+
+				rc = qcom_wled_auto_calibrate(wled);
+				if (rc < 0)
+					pr_err("Failed auto-calibration rc=%d\n",
+						rc);

qcom_wled_auto_calibrate() did already print.

Ok. I will remove it in the next series.

Quoted 21 lines

drivers/video/backlight/qcom-spmi-wled.c static irqreturn_t qcom_wled_ovp_irq_handler(int irq, void *_wled) · line 613

Quoted 13 lines
drivers/video/backlight/qcom-spmi-wled.c +13 -0
+				else
+					wled->auto_calib_done = true;
+
+				if (wled->cfg.ovp_irq > 0 &&
+						wled->ovp_irq_disabled) {
+					enable_irq(wled->cfg.ovp_irq);
+					wled->ovp_irq_disabled = false;
+				}
+				mutex_unlock(&wled->lock);
+			}
+		}
+	}
+

Regards,
Bjorn
--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 5 lines
Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html