kernel: bump 5.10 to 5.10.150
[openwrt/openwrt.git] / target / linux / generic / backport-5.10 / 842-v5.15-leds-pca955x-implement-the-default-state-property.patch
1 From e46cb6d0c760a5b15e38138845fad99628fafcb8 Mon Sep 17 00:00:00 2001
2 From: Eddie James <eajames@linux.ibm.com>
3 Date: Fri, 16 Jul 2021 17:03:29 -0500
4 Subject: [PATCH] leds: pca955x: Implement the default-state property
5
6 In order to retain the LED state after a system reboot, check the
7 documented default-state device tree property during initialization.
8 Modify the behavior of the probe according to the property.
9
10 Signed-off-by: Eddie James <eajames@linux.ibm.com>
11 Signed-off-by: Pavel Machek <pavel@ucw.cz>
12 ---
13 drivers/leds/leds-pca955x.c | 54 +++++++++++++++++++++++++++++++------
14 1 file changed, 46 insertions(+), 8 deletions(-)
15
16 --- a/drivers/leds/leds-pca955x.c
17 +++ b/drivers/leds/leds-pca955x.c
18 @@ -129,6 +129,7 @@ struct pca955x_led {
19 int led_num; /* 0 .. 15 potentially */
20 char name[32];
21 u32 type;
22 + int default_state;
23 const char *default_trigger;
24 };
25
26 @@ -439,6 +440,7 @@ pca955x_get_pdata(struct i2c_client *cli
27
28 device_for_each_child_node(&client->dev, child) {
29 const char *name;
30 + const char *state;
31 u32 reg;
32 int res;
33
34 @@ -457,6 +459,18 @@ pca955x_get_pdata(struct i2c_client *cli
35 fwnode_property_read_u32(child, "type", &led->type);
36 fwnode_property_read_string(child, "linux,default-trigger",
37 &led->default_trigger);
38 +
39 + if (!fwnode_property_read_string(child, "default-state",
40 + &state)) {
41 + if (!strcmp(state, "keep"))
42 + led->default_state = LEDS_GPIO_DEFSTATE_KEEP;
43 + else if (!strcmp(state, "on"))
44 + led->default_state = LEDS_GPIO_DEFSTATE_ON;
45 + else
46 + led->default_state = LEDS_GPIO_DEFSTATE_OFF;
47 + } else {
48 + led->default_state = LEDS_GPIO_DEFSTATE_OFF;
49 + }
50 }
51
52 pdata->num_leds = chip->bits;
53 @@ -485,6 +499,7 @@ static int pca955x_probe(struct i2c_clie
54 int i, err;
55 struct pca955x_platform_data *pdata;
56 int ngpios = 0;
57 + bool keep_pwm = false;
58
59 chip = &pca955x_chipdefs[id->driver_data];
60 adapter = client->adapter;
61 @@ -565,14 +580,35 @@ static int pca955x_probe(struct i2c_clie
62 led->brightness_set_blocking = pca955x_led_set;
63 led->brightness_get = pca955x_led_get;
64
65 + if (pdata->leds[i].default_state ==
66 + LEDS_GPIO_DEFSTATE_OFF) {
67 + err = pca955x_led_set(led, LED_OFF);
68 + if (err)
69 + return err;
70 + } else if (pdata->leds[i].default_state ==
71 + LEDS_GPIO_DEFSTATE_ON) {
72 + err = pca955x_led_set(led, LED_FULL);
73 + if (err)
74 + return err;
75 + }
76 +
77 err = devm_led_classdev_register(&client->dev, led);
78 if (err)
79 return err;
80
81 - /* Turn off LED */
82 - err = pca955x_led_set(led, LED_OFF);
83 - if (err)
84 - return err;
85 + /*
86 + * For default-state == "keep", let the core update the
87 + * brightness from the hardware, then check the
88 + * brightness to see if it's using PWM1. If so, PWM1
89 + * should not be written below.
90 + */
91 + if (pdata->leds[i].default_state ==
92 + LEDS_GPIO_DEFSTATE_KEEP) {
93 + if (led->brightness != LED_FULL &&
94 + led->brightness != LED_OFF &&
95 + led->brightness != LED_HALF)
96 + keep_pwm = true;
97 + }
98 }
99 }
100
101 @@ -581,10 +617,12 @@ static int pca955x_probe(struct i2c_clie
102 if (err)
103 return err;
104
105 - /* PWM1 is used for variable brightness, default to OFF */
106 - err = pca955x_write_pwm(client, 1, 0);
107 - if (err)
108 - return err;
109 + if (!keep_pwm) {
110 + /* PWM1 is used for variable brightness, default to OFF */
111 + err = pca955x_write_pwm(client, 1, 0);
112 + if (err)
113 + return err;
114 + }
115
116 /* Set to fast frequency so we do not see flashing */
117 err = pca955x_write_psc(client, 0, 0);