Skip to contentSkip to conversation

backlight: aw99706: Add support for Awinic AW99706 backlight

PATCH Superseded Junjie Cao 4 participants 10 messages Nov 3, 2025 – Nov 4, 2025

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Junjie Cao

author
#0 lore raw

v2 cover backlight: aw99706: Add support for Awinic AW99706 backlight

From: Pengyu Luo <[email protected]>

Add support for Awinic AW99706 backlight, which can be found in tablet and notebook backlight, one case is the Lenovo Legion Y700 Gen4. This driver refers to the official datasheets and android driver, they can be found in [1].

[1] https://www.awinic.com/en/productDetail/AW99706QNR

---
base-commit: 72fb0170ef1f45addf726319c52a0562b6913707
---
Changes in v2:
- add handler for max-brightness and default-brightness
- add properties(max-brightness, default-brightness) (Krzysztof)
- use proper units for properties (Krzysztof)
- drop non-fixed properties (Krzysztof)
- include default values in the aw99706_dt_props table (Daniel)
- warn when a property value from DT is invalid (Daniel)
- drop warning when optional properties are missing (Daniel)
- add a function pointer into the aw99706_dt_props table to handle lookup (Daniel)
- use a lookup function instead of hardcoding the formula for the iLED max (Daniel)
- move BL enalbe handler into aw99706_update_brightness (Daniel)
- Link to v1: https://lore.kernel.org/linux-leds/[email protected]

Junjie Cao (2):
dt-bindings: leds: backlight: Add Awinic AW99706 backlight
backlight: aw99706: Add support for Awinic AW99706 backlight

 .../leds/backlight/awinic,aw99706.yaml        | 100 ++++
 MAINTAINERS                                   |   6 +
 drivers/video/backlight/Kconfig               |   8 +
 drivers/video/backlight/Makefile              |   1 +
 drivers/video/backlight/aw99706.c             | 492 ++++++++++++++++++
 5 files changed, 607 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml
 create mode 100644 drivers/video/backlight/aw99706.c
Signature · 1 line
2.51.1.dirty
author
#1 lore raw

PATCH v2 1/2 dt-bindings: leds: backlight: Add Awinic AW99706 backlight

From: Pengyu Luo <[email protected]>

Add Awinic AW99706 backlight binding documentation.

---
Changes in v2:
- use proper units for properties (Krzysztof)
- drop non-fixed properties (Krzysztof)
- add properties(max-brightness, default-brightness) (Krzysztof)
- Link to v1: https://lore.kernel.org/linux-leds/[email protected]

 .../leds/backlight/awinic,aw99706.yaml        | 100 ++++++++++++++++++
 1 file changed, 100 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +100 -0
new file mode 100644
index 000000000..9b7266e61
@@ -0,0 +1,100 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/backlight/awinic,aw99706.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Awinic AW99706 6-channel WLED Backlight Driver
+
+maintainers:
+  - Junjie Cao <[email protected]>
+
+allOf:
+  - $ref: common.yaml#
+
+properties:
+  compatible:
+    const: awinic,aw99706
+
+  reg:
+    maxItems: 1
+
+  enable-gpios:
+    description: GPIO to use to enable/disable the backlight (HWEN pin).
+    maxItems: 1
+
+  awinic,dim-mode:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: >
+      Select dimming mode of the device.
+        0 = Bypass mode.
+        1 = DC mode.
+        2 = MIX mode(PWM at low brightness and DC at high brightness).
+        3 = MIX-26k mode(MIX mode with different PWM frequency).
+    enum: [ 0, 1, 2, 3 ]
+    default: 1
+
+  awinic,sw-freq-hz:
+    description: Boost switching frequency in Hz.
+    enum: [ 300000, 400000, 500000, 600000, 660000, 750000, 850000, 1000000, 1200000, 1330000, 1500000, 1700000 ]
+    default: 750000
+
+  awinic,sw-ilmt-microamp:
+    description: Switching current limitation in uA.
+    enum: [ 1500000, 2000000, 2500000, 3000000 ]
+    default: 3000000
+
+  awinic,iled-max-microamp:
+    description: Maximum LED current setting in uA.
+    minimum: 5000
+    maximum: 50000
+    multipleOf: 500
+    default: 20000
+
+  awinic,uvlo-thres-microvolt:
+    description: UVLO(Under Voltage Lock Out) in uV.
+    enum: [ 2200000, 5000000 ]
+    default: 2200000
+
+  awinic,ramp-ctl:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: >
+      Select ramp control and filter of the device.
+        0 = Fade in/fade out.
+        1 = Light filter.
+        2 = Medium filter.
+        3 = Heavy filter.
+    enum: [ 0, 1, 2, 3 ]
+    default: 2
+
+required:
+  - compatible
+  - reg
+  - enable-gpios
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        aw99706@76 {
+            compatible = "awinic,aw99706";
+            reg = <0x76>;
+            enable-gpios = <&tlmm 88 GPIO_ACTIVE_HIGH>;
+            default-brightness = <2047>;
+            max-brightness = <4095>;
+            awinic,dim-mode = <1>;
+            awinic,sw-freq-hz = <750000>;
+            awinic,sw-ilmt-microamp = <3000000>;
+            awinic,uvlo-thres-microvolt = <2200000>;
+            awinic,iled-max-microamp = <20000>;
+            awinic,ramp-ctl = <2>;
+        };
+    };
+
+...
Signature · 1 line
2.51.1.dirty
#2 lore raw

Re: [PATCH v2 1/2] dt-bindings: leds: backlight: Add Awinic AW99706 backlight

Quoted 12 lines · Junjie Cao

From: Pengyu Luo <[email protected]>

Add Awinic AW99706 backlight binding documentation.

---
Changes in v2:
- use proper units for properties (Krzysztof)
- drop non-fixed properties (Krzysztof)
- add properties(max-brightness, default-brightness) (Krzysztof)
- Link to v1: https://lore.kernel.org/linux-leds/[email protected]

Quoted 4 lines
 .../leds/backlight/awinic,aw99706.yaml        | 100 ++++++++++++++++++
 1 file changed, 100 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:
./Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml:39:111: [warning] line too long (113 > 110 characters) (line-length)

dtschema/dtc warnings/errors:

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

#3 lore raw

Re: [PATCH v2 1/2] dt-bindings: leds: backlight: Add Awinic AW99706 backlight

In reply to Junjie Cao
Quoted 5 lines · Junjie Cao

From: Pengyu Luo <[email protected]>

Add Awinic AW99706 backlight binding documentation.

Messed DCO chain. This wasn't here, so you must have altered v1 to add some weird change.

This is a blocker, please read carefully submitting patches and DCO.

Quoted 6 lines

---
Changes in v2:
- use proper units for properties (Krzysztof)
- drop non-fixed properties (Krzysztof)
- add properties(max-brightness, default-brightness) (Krzysztof)
- Link to v1: https://lore.kernel.org/linux-leds/[email protected]

...

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +4 -0
+  awinic,dim-mode:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: >
+      Select dimming mode of the device.

Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml line 39

Quoted 10 lines
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +10 -0
+        0 = Bypass mode.
+        1 = DC mode.
+        2 = MIX mode(PWM at low brightness and DC at high brightness).
+        3 = MIX-26k mode(MIX mode with different PWM frequency).
+    enum: [ 0, 1, 2, 3 ]
+    default: 1
+
+  awinic,sw-freq-hz:
+    description: Boost switching frequency in Hz.
+    enum: [ 300000, 400000, 500000, 600000, 660000, 750000, 850000, 1000000, 1200000, 1330000, 1500000, 1700000 ]

Please wrap code according to the preferred limit expressed in Kernel coding style (checkpatch is not a coding style description, but only a tool).

Quoted 36 lines
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +36 -0
+    default: 750000
+
+  awinic,sw-ilmt-microamp:
+    description: Switching current limitation in uA.
+    enum: [ 1500000, 2000000, 2500000, 3000000 ]
+    default: 3000000
+
+  awinic,iled-max-microamp:
+    description: Maximum LED current setting in uA.
+    minimum: 5000
+    maximum: 50000
+    multipleOf: 500
+    default: 20000
+
+  awinic,uvlo-thres-microvolt:
+    description: UVLO(Under Voltage Lock Out) in uV.
+    enum: [ 2200000, 5000000 ]
+    default: 2200000
+
+  awinic,ramp-ctl:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: >
+      Select ramp control and filter of the device.
+        0 = Fade in/fade out.
+        1 = Light filter.
+        2 = Medium filter.
+        3 = Heavy filter.
+    enum: [ 0, 1, 2, 3 ]
+    default: 2
+
+required:
+  - compatible
+  - reg
+  - enable-gpios
+
+unevaluatedProperties: false

Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml line 85

Quoted 10 lines
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +10 -0
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        aw99706@76 {

Node names should be generic. See also an explanation and list of examples (not exhaustive) in DT specification:
https://devicetree-specification.readthedocs.io/en/latest/chapter2-devicetree-basics.html#generic-names-recommendation
If you cannot find a name matching your device, please check in kernel sources for similar cases or you can grow the spec (via pull request to DT spec repo).

Quoted 18 lines
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +15 -0
+            compatible = "awinic,aw99706";
+            reg = <0x76>;
+            enable-gpios = <&tlmm 88 GPIO_ACTIVE_HIGH>;
+            default-brightness = <2047>;
+            max-brightness = <4095>;
+            awinic,dim-mode = <1>;
+            awinic,sw-freq-hz = <750000>;
+            awinic,sw-ilmt-microamp = <3000000>;
+            awinic,uvlo-thres-microvolt = <2200000>;
+            awinic,iled-max-microamp = <20000>;
+            awinic,ramp-ctl = <2>;
+        };
+    };
+
+...
Signature · 1 line
2.51.1.dirty
author
#4 lore raw

Re: [PATCH v2 1/2] dt-bindings: leds: backlight: Add Awinic AW99706 backlight

Quoted 4 lines · Krzysztof Kozlowski
Quoted 2 lines · Junjie Cao

From: Pengyu Luo <[email protected]>

Quoted 9 lines
Quoted 3 lines

Add Awinic AW99706 backlight binding documentation.

Messed DCO chain. This wasn't here, so you must have altered v1 to add some weird change.

This is a blocker, please read carefully submitting patches and DCO.

Apologies for the DCO mess.

The tablet device is currently with Pengyu. He helped with testing and tweaked the .c driver file (patch 2/2) after my change. The entire series was then re-formatted on his machine, which caused his git configuration to be incorrectly applied to the From: line of this dt-binding patch (patch 1/2).

I am the actual author of this dt-binding file. I will correct the authorship and DCO chain in v2.

Quoted 24 lines
Quoted 6 lines

---
Changes in v2:
- use proper units for properties (Krzysztof)
- drop non-fixed properties (Krzysztof)
- add properties(max-brightness, default-brightness) (Krzysztof)
- Link to v1: https://lore.kernel.org/linux-leds/[email protected]

...

Quoted 4 lines
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +4 -0
+  awinic,dim-mode:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: >
+      Select dimming mode of the device.

Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml line 39

Quoted 10 lines
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +10 -0
+        0 = Bypass mode.
+        1 = DC mode.
+        2 = MIX mode(PWM at low brightness and DC at high brightness).
+        3 = MIX-26k mode(MIX mode with different PWM frequency).
+    enum: [ 0, 1, 2, 3 ]
+    default: 1
+
+  awinic,sw-freq-hz:
+    description: Boost switching frequency in Hz.
+    enum: [ 300000, 400000, 500000, 600000, 660000, 750000, 850000, 1000000, 1200000, 1330000, 1500000, 1700000 ]
Quoted 4 lines

Please wrap code according to the preferred limit expressed in Kernel coding style (checkpatch is not a coding style description, but only a tool).

ACK.

Quoted 45 lines
Quoted 45 lines
Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml new file +45 -0
+    default: 750000
+
+  awinic,sw-ilmt-microamp:
+    description: Switching current limitation in uA.
+    enum: [ 1500000, 2000000, 2500000, 3000000 ]
+    default: 3000000
+
+  awinic,iled-max-microamp:
+    description: Maximum LED current setting in uA.
+    minimum: 5000
+    maximum: 50000
+    multipleOf: 500
+    default: 20000
+
+  awinic,uvlo-thres-microvolt:
+    description: UVLO(Under Voltage Lock Out) in uV.
+    enum: [ 2200000, 5000000 ]
+    default: 2200000
+
+  awinic,ramp-ctl:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: >
+      Select ramp control and filter of the device.
+        0 = Fade in/fade out.
+        1 = Light filter.
+        2 = Medium filter.
+        3 = Heavy filter.
+    enum: [ 0, 1, 2, 3 ]
+    default: 2
+
+required:
+  - compatible
+  - reg
+  - enable-gpios
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
Quoted 9 lines
Quoted 1 line

+ aw99706@76 {

Node names should be generic. See also an explanation and list of examples (not exhaustive) in DT specification:
https://devicetree-specification.readthedocs.io/en/latest/chapter2-devicetree-basics.html#generic-names-recommendation
If you cannot find a name matching your device, please check in kernel sources for similar cases or you can grow the spec (via pull request to DT spec repo).

I see. backlight@76, thanks for your detailed explanation.

Regards,
Junjie

author
#5 lore raw

PATCH v2 2/2 backlight: aw99706: Add support for Awinic AW99706 backlight

In reply to Junjie Cao

From: Pengyu Luo <[email protected]>

Add support for Awinic AW99706 backlight, which can be found in tablet and notebook backlight, one case is the Lenovo Legion Y700 Gen4. This driver refers to the official datasheets and android driver, they can be found in [1].

[1] https://www.awinic.com/en/productDetail/AW99706QNR

---
Changes in v2:
- add handler for max-brightness and default-brightness
- use proper units for properties (Krzysztof)
- drop non-fixed properties (Krzysztof)
- include default values in the aw99706_dt_props table (Daniel)
- warn when a property value from DT is invalid (Daniel)
- drop warning when optional properties are missing (Daniel)
- add a function pointer into the aw99706_dt_props table to handle lookup (Daniel)
- use a lookup function instead of hardcoding the formula for the iLED max (Daniel)
- move BL enalbe handler into aw99706_update_brightness (Daniel)
- Link to v1: https://lore.kernel.org/linux-leds/[email protected]

 MAINTAINERS                       |   6 +
 drivers/video/backlight/Kconfig   |   8 +
 drivers/video/backlight/Makefile  |   1 +
 drivers/video/backlight/aw99706.c | 492 ++++++++++++++++++++++++++++++
 4 files changed, 507 insertions(+)
 create mode 100644 drivers/video/backlight/aw99706.c
MAINTAINERS +6 -0
index be21f1fa8..551d8328e 100644
@@ -4140,6 +4140,12 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/adc/avia-hx711.yaml
 F:	drivers/iio/adc/hx711.c
 
+AWINIC AW99706 WLED BACKLIGHT DRIVER
+M:	Junjie Cao <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/leds/backlight/awinic,aw99706.yaml
+F:	drivers/video/backlight/aw99706.c
+
 AX.25 NETWORK LAYER
 L:	[email protected]
 S:	Orphan
drivers/video/backlight/Kconfig +8 -0
index d9374d208..35c7bfad0 100644
@@ -156,6 +156,14 @@ config BACKLIGHT_ATMEL_LCDC
 	  If in doubt, it's safe to enable this option; it doesn't kick
 	  in unless the board's description says it's wired that way.
 
+config BACKLIGHT_AW99706
+	tristate "Backlight Driver for Awinic AW99706"
+	depends on I2C
+	select REGMAP_I2C
+	help
+	  If you have a LCD backlight connected to the WLED output of AW99706
+	  WLED output, say Y here to enable this driver.
+
 config BACKLIGHT_EP93XX
 	tristate "Cirrus EP93xx Backlight Driver"
 	depends on FB_EP93XX
drivers/video/backlight/Makefile +1 -0
index dfbb169bf..a5d62b018 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_BACKLIGHT_ADP8870)		+= adp8870_bl.o
 obj-$(CONFIG_BACKLIGHT_APPLE)		+= apple_bl.o
 obj-$(CONFIG_BACKLIGHT_APPLE_DWI)	+= apple_dwi_bl.o
 obj-$(CONFIG_BACKLIGHT_AS3711)		+= as3711_bl.o
+obj-$(CONFIG_BACKLIGHT_AW99706)		+= aw99706.o
 obj-$(CONFIG_BACKLIGHT_BD6107)		+= bd6107.o
 obj-$(CONFIG_BACKLIGHT_CLASS_DEVICE)	+= backlight.o
 obj-$(CONFIG_BACKLIGHT_DA903X)		+= da903x_bl.o
drivers/video/backlight/aw99706.c new file +492 -0

492 lines not on this page · raw message

Signature · 1 line
2.51.1.dirty
#6 lore raw
Quoted 11 lines · Junjie Cao

From: Pengyu Luo <[email protected]>

Add support for Awinic AW99706 backlight, which can be found in tablet and notebook backlight, one case is the Lenovo Legion Y700 Gen4. This driver refers to the official datasheets and android driver, they can be found in [1].

[1] https://www.awinic.com/en/productDetail/AW99706QNR

Quoted 12 lines

---
Changes in v2:
- add handler for max-brightness and default-brightness
- use proper units for properties (Krzysztof)
- drop non-fixed properties (Krzysztof)
- include default values in the aw99706_dt_props table (Daniel)
- warn when a property value from DT is invalid (Daniel)
- drop warning when optional properties are missing (Daniel)
- add a function pointer into the aw99706_dt_props table to handle lookup (Daniel)
- use a lookup function instead of hardcoding the formula for the iLED max (Daniel)
- move BL enalbe handler into aw99706_update_brightness (Daniel)
- Link to v1: https://lore.kernel.org/linux-leds/[email protected]

Thanks for the changes.

I'm afraid I don't like encoding the shift in the DT properties table. Caching something that is so easy to recalculate makes no sense to me. See below:

drivers/video/backlight/aw99706.c line 162

Quoted 8 lines
drivers/video/backlight/aw99706.c new file +8 -0
+struct aw99706_dt_prop {
+	const char * const name;
+	int (*lookup)(const struct aw99706_dt_prop *prop, u32 dt_val, u8 *val);
+	const u32 * const lookup_tbl;
+	u8 tbl_size;
+	u8 reg;
+	u8 mask;
+	u8 shift;

There should bee no need to record shift here. It's just a duplicating information already held in mask.

Quoted 34 lines
drivers/video/backlight/aw99706.c new file +34 -0
+	u32 def_val;
+};
+
+static int aw99706_dt_property_lookup(const struct aw99706_dt_prop *prop,
+				      u32 dt_val, u8 *val)
+{
+	int i;
+
+	if (!prop->lookup_tbl) {
+		*val = dt_val;
+		return 0;
+	}
+
+	for (i = 0; i < prop->tbl_size; i++)
+		if (prop->lookup_tbl[i] == dt_val)
+			break;
+
+	*val = i;
+
+	return i == prop->tbl_size ? -1 : 0;
+}
+
+#define MIN_ILED_MAX	5000
+#define MAX_ILED_MAX	50000
+#define STEP_ILED_MAX	500
+
+static int
+aw99706_dt_property_iled_max_convert(const struct aw99706_dt_prop *prop,
+				     u32 dt_val, u8 *val)
+{
+	if (dt_val > MAX_ILED_MAX || dt_val < MIN_ILED_MAX)
+		return -1;
+
+	*val = (dt_val - MIN_ILED_MAX) / STEP_ILED_MAX;

drivers/video/backlight/aw99706.c line 206

Quoted 10 lines
drivers/video/backlight/aw99706.c new file +10 -0
+
+	return (dt_val - MIN_ILED_MAX) % STEP_ILED_MAX;
+}
+
+static const struct aw99706_dt_prop aw99706_dt_props[] = {
+	{
+		"awinic,dim-mode", aw99706_dt_property_lookup,
+		NULL, 0,
+		AW99706_CFG0_REG,
+		AW99706_DIM_MODE_MASK, __builtin_ctz(AW99706_DIM_MODE_MASK),

These __builtin_ctz() calls shouldn't be in the lookup table (if they are not in the lookup table then can never be inconsistant with the mask).

Quoted 2 lines
(quoted diff) +2 -0
+		1,
+	},

<snip>

Quoted 31 lines
drivers/video/backlight/aw99706.c new file +31 -0
+	{
+		"awinic,ramp-ctl", aw99706_dt_property_lookup,
+		NULL, 0,
+		AW99706_CFG6_REG,
+		AW99706_RAMP_CTL_MASK, __builtin_ctz(AW99706_RAMP_CTL_MASK),
+		2,
+	},
+};
+
+struct reg_init_data {
+	u8 reg;
+	u8 mask;
+	u8 val;
+};
+
+static struct reg_init_data reg_init_tbl[ARRAY_SIZE(aw99706_dt_props)];
+
+static void aw99706_dt_parse(struct aw99706_device *aw,
+			     struct backlight_properties *bl_props)
+{
+	const struct aw99706_dt_prop *prop;
+	u32 dt_val;
+	int ret, i;
+	u8 val;
+
+	for (i = 0; i < ARRAY_SIZE(aw99706_dt_props); i++) {
+		prop = &aw99706_dt_props[i];
+		ret = device_property_read_u32(aw->dev, prop->name, &dt_val);
+		if (ret < 0)
+			dt_val = prop->def_val;
+

drivers/video/backlight/aw99706.c line 278

Quoted 10 lines
drivers/video/backlight/aw99706.c new file +10 -0
+		if (prop->lookup(prop, dt_val, &val)) {
+			dev_warn(aw->dev, "invalid value %d for property %s, using default value %d\n",
+				 dt_val, prop->name, prop->def_val);
+
+			prop->lookup(prop, prop->def_val, &val);
+		}
+
+		reg_init_tbl[i].reg = prop->reg;
+		reg_init_tbl[i].mask = prop->mask;
+		reg_init_tbl[i].val = val << prop->shift;

Can't you just use FIELD_PREP() to set val (either here or at the point the init table is consumed)? That why there's no ffs() or clz() at all.

drivers/video/backlight/aw99706.c line 282

Quoted 4 lines
drivers/video/backlight/aw99706.c new file +4 -0
+	}
+
+	aw->init_tbl = reg_init_tbl;
+	aw->init_tbl_size = ARRAY_SIZE(reg_init_tbl);

Copying a pointer to a single instance static data buffer into a dynamically allocated data structure isn't right.

You should include the init table as part of struct aw99706_device.

Quoted 30 lines
drivers/video/backlight/aw99706.c new file +30 -0
+
+	bl_props->brightness = AW99706_MAX_BRT_LVL >> 1;
+	bl_props->max_brightness = AW99706_MAX_BRT_LVL;
+	device_property_read_u32(aw->dev, "default-brightness",
+				 &bl_props->brightness);
+	device_property_read_u32(aw->dev, "max-brightness",
+				 &bl_props->max_brightness);
+
+	if (bl_props->max_brightness > AW99706_MAX_BRT_LVL)
+		bl_props->max_brightness = AW99706_MAX_BRT_LVL;
+
+	if (bl_props->brightness > bl_props->max_brightness)
+		bl_props->brightness = bl_props->max_brightness;
+}
+
+static int aw99706_hw_init(struct aw99706_device *aw)
+{
+	int ret, i;
+
+	gpiod_set_value_cansleep(aw->hwen_gpio, 1);
+
+	for (i = 0; i < aw->init_tbl_size; i++) {
+		ret = aw99706_i2c_update_bits(aw, aw->init_tbl[i].reg,
+					      aw->init_tbl[i].mask,
+					      aw->init_tbl[i].val);
+		if (ret < 0) {
+			dev_err(aw->dev, "Failed to write init data %d\n", ret);
+			return ret;
+		}
+	}

drivers/video/backlight/aw99706.c line 322

Quoted 10 lines
drivers/video/backlight/aw99706.c new file +10 -0
+
+	return 0;
+}
+
+static int aw99706_bl_enable(struct aw99706_device *aw, bool en)
+{
+	int ret;
+	u8 val;
+
+	FIELD_MODIFY(AW99706_BACKLIGHT_EN_MASK, &val, en);

This should use FIELD_PREP() not FIELD_MODIFY();

drivers/video/backlight/aw99706.c line 329

Quoted 7 lines
drivers/video/backlight/aw99706.c new file +7 -0
+	ret = aw99706_i2c_update_bits(aw, AW99706_CFGD_REG,
+				      AW99706_BACKLIGHT_EN_MASK, val);
+	if (ret)
+		dev_err(aw->dev, "Failed to enable backlight!\n");
+
+	return ret;
+}

Daniel.

author
#7 lore raw
Quoted 135 lines · Daniel Thompson
Quoted 11 lines · Junjie Cao

From: Pengyu Luo <[email protected]>

Add support for Awinic AW99706 backlight, which can be found in tablet and notebook backlight, one case is the Lenovo Legion Y700 Gen4. This driver refers to the official datasheets and android driver, they can be found in [1].

[1] https://www.awinic.com/en/productDetail/AW99706QNR

Quoted 12 lines

---
Changes in v2:
- add handler for max-brightness and default-brightness
- use proper units for properties (Krzysztof)
- drop non-fixed properties (Krzysztof)
- include default values in the aw99706_dt_props table (Daniel)
- warn when a property value from DT is invalid (Daniel)
- drop warning when optional properties are missing (Daniel)
- add a function pointer into the aw99706_dt_props table to handle lookup (Daniel)
- use a lookup function instead of hardcoding the formula for the iLED max (Daniel)
- move BL enalbe handler into aw99706_update_brightness (Daniel)
- Link to v1: https://lore.kernel.org/linux-leds/[email protected]

Thanks for the changes.

I'm afraid I don't like encoding the shift in the DT properties table. Caching something that is so easy to recalculate makes no sense to me. See below:

drivers/video/backlight/aw99706.c line 162

Quoted 8 lines
drivers/video/backlight/aw99706.c new file +8 -0
+struct aw99706_dt_prop {
+     const char * const name;
+     int (*lookup)(const struct aw99706_dt_prop *prop, u32 dt_val, u8 *val);
+     const u32 * const lookup_tbl;
+     u8 tbl_size;
+     u8 reg;
+     u8 mask;
+     u8 shift;

There should bee no need to record shift here. It's just a duplicating information already held in mask.

Quoted 34 lines
drivers/video/backlight/aw99706.c new file +34 -0
+     u32 def_val;
+};
+
+static int aw99706_dt_property_lookup(const struct aw99706_dt_prop *prop,
+                                   u32 dt_val, u8 *val)
+{
+     int i;
+
+     if (!prop->lookup_tbl) {
+             *val = dt_val;
+             return 0;
+     }
+
+     for (i = 0; i < prop->tbl_size; i++)
+             if (prop->lookup_tbl[i] == dt_val)
+                     break;
+
+     *val = i;
+
+     return i == prop->tbl_size ? -1 : 0;
+}
+
+#define MIN_ILED_MAX 5000
+#define MAX_ILED_MAX 50000
+#define STEP_ILED_MAX        500
+
+static int
+aw99706_dt_property_iled_max_convert(const struct aw99706_dt_prop *prop,
+                                  u32 dt_val, u8 *val)
+{
+     if (dt_val > MAX_ILED_MAX || dt_val < MIN_ILED_MAX)
+             return -1;
+
+     *val = (dt_val - MIN_ILED_MAX) / STEP_ILED_MAX;

drivers/video/backlight/aw99706.c line 206

Quoted 10 lines
drivers/video/backlight/aw99706.c new file +10 -0
+
+     return (dt_val - MIN_ILED_MAX) % STEP_ILED_MAX;
+}
+
+static const struct aw99706_dt_prop aw99706_dt_props[] = {
+     {
+             "awinic,dim-mode", aw99706_dt_property_lookup,
+             NULL, 0,
+             AW99706_CFG0_REG,
+             AW99706_DIM_MODE_MASK, __builtin_ctz(AW99706_DIM_MODE_MASK),

These __builtin_ctz() calls shouldn't be in the lookup table (if they are not in the lookup table then can never be inconsistant with the mask).

Quoted 2 lines
(quoted diff) +2 -0
+             1,
+     },

<snip>

Quoted 36 lines
drivers/video/backlight/aw99706.c new file +36 -0
+     {
+             "awinic,ramp-ctl", aw99706_dt_property_lookup,
+             NULL, 0,
+             AW99706_CFG6_REG,
+             AW99706_RAMP_CTL_MASK, __builtin_ctz(AW99706_RAMP_CTL_MASK),
+             2,
+     },
+};
+
+struct reg_init_data {
+     u8 reg;
+     u8 mask;
+     u8 val;
+};
+
+static struct reg_init_data reg_init_tbl[ARRAY_SIZE(aw99706_dt_props)];
+
+static void aw99706_dt_parse(struct aw99706_device *aw,
+                          struct backlight_properties *bl_props)
+{
+     const struct aw99706_dt_prop *prop;
+     u32 dt_val;
+     int ret, i;
+     u8 val;
+
+     for (i = 0; i < ARRAY_SIZE(aw99706_dt_props); i++) {
+             prop = &aw99706_dt_props[i];
+             ret = device_property_read_u32(aw->dev, prop->name, &dt_val);
+             if (ret < 0)
+                     dt_val = prop->def_val;
+
+             if (prop->lookup(prop, dt_val, &val)) {
+                     dev_warn(aw->dev, "invalid value %d for property %s, using default value %d\n",
+                              dt_val, prop->name, prop->def_val);
+
+                     prop->lookup(prop, prop->def_val, &val);
Quoted 9 lines

drivers/video/backlight/aw99706.c line 278

Quoted 5 lines
drivers/video/backlight/aw99706.c new file +5 -0
+             }
+
+             reg_init_tbl[i].reg = prop->reg;
+             reg_init_tbl[i].mask = prop->mask;
+             reg_init_tbl[i].val = val << prop->shift;

Can't you just use FIELD_PREP() to set val (either here or at the point the init table is consumed)? That why there's no ffs() or clz() at all.

Thanks for it, I tried to find something like FIELD_PREP() to avoid keeping shift, but I failed to notice it in the bitfield.h. I will drop the shift field and use it to handle bit operations gracefully.

Quoted 11 lines

drivers/video/backlight/aw99706.c line 282

Quoted 5 lines
drivers/video/backlight/aw99706.c new file +4 -0
+     }
+
+     aw->init_tbl = reg_init_tbl;
+     aw->init_tbl_size = ARRAY_SIZE(reg_init_tbl);

Copying a pointer to a single instance static data buffer into a dynamically allocated data structure isn't right.

You should include the init table as part of struct aw99706_device.

I see. Multiple instances should be taken into account.

Quoted 35 lines
Quoted 35 lines
drivers/video/backlight/aw99706.c new file +34 -0
+
+     bl_props->brightness = AW99706_MAX_BRT_LVL >> 1;
+     bl_props->max_brightness = AW99706_MAX_BRT_LVL;
+     device_property_read_u32(aw->dev, "default-brightness",
+                              &bl_props->brightness);
+     device_property_read_u32(aw->dev, "max-brightness",
+                              &bl_props->max_brightness);
+
+     if (bl_props->max_brightness > AW99706_MAX_BRT_LVL)
+             bl_props->max_brightness = AW99706_MAX_BRT_LVL;
+
+     if (bl_props->brightness > bl_props->max_brightness)
+             bl_props->brightness = bl_props->max_brightness;
+}
+
+static int aw99706_hw_init(struct aw99706_device *aw)
+{
+     int ret, i;
+
+     gpiod_set_value_cansleep(aw->hwen_gpio, 1);
+
+     for (i = 0; i < aw->init_tbl_size; i++) {
+             ret = aw99706_i2c_update_bits(aw, aw->init_tbl[i].reg,
+                                           aw->init_tbl[i].mask,
+                                           aw->init_tbl[i].val);
+             if (ret < 0) {
+                     dev_err(aw->dev, "Failed to write init data %d\n", ret);
+                     return ret;
+             }
+     }
+
+     return 0;
+}
+
Quoted 9 lines

drivers/video/backlight/aw99706.c line 322

Quoted 6 lines
drivers/video/backlight/aw99706.c new file +6 -0
+static int aw99706_bl_enable(struct aw99706_device *aw, bool en)
+{
+     int ret;
+     u8 val;
+
+     FIELD_MODIFY(AW99706_BACKLIGHT_EN_MASK, &val, en);

This should use FIELD_PREP() not FIELD_MODIFY();

ACK. FIELD_PREP() makes more sense here.

Regards,
Junjie

#8 lore raw
In reply to Junjie Cao
Quoted 10 lines · Junjie Cao
drivers/video/backlight/aw99706.c new file +10 -0
+struct reg_init_data;
+
+struct aw99706_device {
+	struct i2c_client *client;
+	struct device *dev;
+	struct regmap *regmap;
+	struct backlight_device *bl_dev;
+	struct gpio_desc *hwen_gpio;
+	struct reg_init_data *init_tbl;
+	int init_tbl_size;

drivers/video/backlight/aw99706.c line 100

Quoted 10 lines
drivers/video/backlight/aw99706.c new file +10 -0
+	bool bl_enable;
+};
+
+enum reg_access {
+	REG_NONE_ACCESS	= 0,
+	REG_RD_ACCESS	= 1,
+	REG_WR_ACCESS	= 2,
+};
+
+const u8 aw99706_regs[AW99706_REG_MAX + 1] = {

Why isn't this static?

Quoted 27 lines
drivers/video/backlight/aw99706.c new file +27 -0
+	[AW99706_CFG0_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG1_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG2_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG3_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG4_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG5_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG6_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG7_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG8_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFG9_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFGA_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFGB_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFGC_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_CFGD_REG]		= REG_RD_ACCESS | REG_WR_ACCESS,
+	[AW99706_FLAG_REG]		= REG_RD_ACCESS,
+	[AW99706_CHIPID_REG]		= REG_RD_ACCESS,
+	[AW99706_LED_OPEN_FLAG_REG]	= REG_RD_ACCESS,
+	[AW99706_LED_SHORT_FLAG_REG]	= REG_RD_ACCESS,
+
+	/*
+	 * Write bit is dropped here, writing BIT(0) to MTPLDOSEL will unlock
+	 * Multi-time Programmable (MTP).
+	 */
+	[AW99706_MTPLDOSEL_REG]		= REG_RD_ACCESS,
+	[AW99706_MTPRUN_REG]		= REG_NONE_ACCESS,
+};
+

...

Quoted 7 lines
drivers/video/backlight/aw99706.c new file +7 -0
+	aw = devm_kzalloc(dev, sizeof(*aw), GFP_KERNEL);
+	if (!aw)
+		return -ENOMEM;
+
+	aw->client = client;
+	aw->dev = dev;
+	i2c_set_clientdata(client, aw);

drivers/video/backlight/aw99706.c

Quoted 10 lines
drivers/video/backlight/aw99706.c new file +10 -0
+
+	aw->regmap = devm_regmap_init_i2c(client, &aw99706_regmap_config);
+	if (IS_ERR(aw->regmap))
+		return dev_err_probe(dev, PTR_ERR(aw->regmap),
+				     "Failed to init regmap\n");
+
+	ret = aw99706_chip_id_read(aw);
+	if (ret != AW99706_ID)
+		return dev_err_probe(dev, ret,
+				     "Failed to validate chip id\n")

Why are you exiting the probe with a positive value like 4 or 8? This makes no sense. Registers do not coontain Linux return codes.

Best regards,
Krzysztof

author
#9 lore raw
Quoted 16 lines · Krzysztof Kozlowski
Quoted 14 lines · Junjie Cao
drivers/video/backlight/aw99706.c new file +14 -0
+struct reg_init_data;
+
+struct aw99706_device {
+     struct i2c_client *client;
+     struct device *dev;
+     struct regmap *regmap;
+     struct backlight_device *bl_dev;
+     struct gpio_desc *hwen_gpio;
+     struct reg_init_data *init_tbl;
+     int init_tbl_size;
+     bool bl_enable;
+};
+
+enum reg_access {
Quoted 9 lines

drivers/video/backlight/aw99706.c line 100

Quoted 6 lines
drivers/video/backlight/aw99706.c new file +6 -0
+     REG_NONE_ACCESS = 0,
+     REG_RD_ACCESS   = 1,
+     REG_WR_ACCESS   = 2,
+};
+
+const u8 aw99706_regs[AW99706_REG_MAX + 1] = {

Why isn't this static?

That was an oversight on my part. It will be static.

Quoted 42 lines
Quoted 27 lines
drivers/video/backlight/aw99706.c new file +27 -0
+     [AW99706_CFG0_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG1_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG2_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG3_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG4_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG5_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG6_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG7_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG8_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFG9_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFGA_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFGB_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFGC_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_CFGD_REG]              = REG_RD_ACCESS | REG_WR_ACCESS,
+     [AW99706_FLAG_REG]              = REG_RD_ACCESS,
+     [AW99706_CHIPID_REG]            = REG_RD_ACCESS,
+     [AW99706_LED_OPEN_FLAG_REG]     = REG_RD_ACCESS,
+     [AW99706_LED_SHORT_FLAG_REG]    = REG_RD_ACCESS,
+
+     /*
+      * Write bit is dropped here, writing BIT(0) to MTPLDOSEL will unlock
+      * Multi-time Programmable (MTP).
+      */
+     [AW99706_MTPLDOSEL_REG]         = REG_RD_ACCESS,
+     [AW99706_MTPRUN_REG]            = REG_NONE_ACCESS,
+};
+

...

Quoted 12 lines
drivers/video/backlight/aw99706.c new file +12 -0
+     aw = devm_kzalloc(dev, sizeof(*aw), GFP_KERNEL);
+     if (!aw)
+             return -ENOMEM;
+
+     aw->client = client;
+     aw->dev = dev;
+     i2c_set_clientdata(client, aw);
+
+     aw->regmap = devm_regmap_init_i2c(client, &aw99706_regmap_config);
+     if (IS_ERR(aw->regmap))
+             return dev_err_probe(dev, PTR_ERR(aw->regmap),
+                                  "Failed to init regmap\n");
Quoted 9 lines

drivers/video/backlight/aw99706.c

Quoted 5 lines
drivers/video/backlight/aw99706.c new file +5 -0
+
+     ret = aw99706_chip_id_read(aw);
+     if (ret != AW99706_ID)
+             return dev_err_probe(dev, ret,
+                                  "Failed to validate chip id\n")

Why are you exiting the probe with a positive value like 4 or 8? This makes no sense. Registers do not coontain Linux return codes.

Thanks for pointing this out. Using -ENODEV makes more sense. I will do it in the next version.

Regards,
Junjie