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
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.
10 Signed-off-by: Eddie James <eajames@linux.ibm.com>
11 Signed-off-by: Pavel Machek <pavel@ucw.cz>
13 drivers/leds/leds-pca955x.c | 54 +++++++++++++++++++++++++++++++------
14 1 file changed, 46 insertions(+), 8 deletions(-)
16 diff --git a/drivers/leds/leds-pca955x.c b/drivers/leds/leds-pca955x.c
17 index e47ba7c3b7c7d8..fa1d77d86ef67b 100644
18 --- a/drivers/leds/leds-pca955x.c
19 +++ b/drivers/leds/leds-pca955x.c
20 @@ -129,6 +129,7 @@ struct pca955x_led {
21 int led_num; /* 0 .. 15 potentially */
25 const char *default_trigger;
28 @@ -439,6 +440,7 @@ pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
30 device_for_each_child_node(&client->dev, child) {
36 @@ -457,6 +459,18 @@ pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
37 fwnode_property_read_u32(child, "type", &led->type);
38 fwnode_property_read_string(child, "linux,default-trigger",
39 &led->default_trigger);
41 + if (!fwnode_property_read_string(child, "default-state",
43 + if (!strcmp(state, "keep"))
44 + led->default_state = LEDS_GPIO_DEFSTATE_KEEP;
45 + else if (!strcmp(state, "on"))
46 + led->default_state = LEDS_GPIO_DEFSTATE_ON;
48 + led->default_state = LEDS_GPIO_DEFSTATE_OFF;
50 + led->default_state = LEDS_GPIO_DEFSTATE_OFF;
54 pdata->num_leds = chip->bits;
55 @@ -485,6 +499,7 @@ static int pca955x_probe(struct i2c_client *client,
57 struct pca955x_platform_data *pdata;
59 + bool keep_pwm = false;
61 chip = &pca955x_chipdefs[id->driver_data];
62 adapter = client->adapter;
63 @@ -565,14 +580,35 @@ static int pca955x_probe(struct i2c_client *client,
64 led->brightness_set_blocking = pca955x_led_set;
65 led->brightness_get = pca955x_led_get;
67 + if (pdata->leds[i].default_state ==
68 + LEDS_GPIO_DEFSTATE_OFF) {
69 + err = pca955x_led_set(led, LED_OFF);
72 + } else if (pdata->leds[i].default_state ==
73 + LEDS_GPIO_DEFSTATE_ON) {
74 + err = pca955x_led_set(led, LED_FULL);
79 err = devm_led_classdev_register(&client->dev, led);
84 - err = pca955x_led_set(led, LED_OFF);
88 + * For default-state == "keep", let the core update the
89 + * brightness from the hardware, then check the
90 + * brightness to see if it's using PWM1. If so, PWM1
91 + * should not be written below.
93 + if (pdata->leds[i].default_state ==
94 + LEDS_GPIO_DEFSTATE_KEEP) {
95 + if (led->brightness != LED_FULL &&
96 + led->brightness != LED_OFF &&
97 + led->brightness != LED_HALF)
103 @@ -581,10 +617,12 @@ static int pca955x_probe(struct i2c_client *client,
107 - /* PWM1 is used for variable brightness, default to OFF */
108 - err = pca955x_write_pwm(client, 1, 0);
112 + /* PWM1 is used for variable brightness, default to OFF */
113 + err = pca955x_write_pwm(client, 1, 0);
118 /* Set to fast frequency so we do not see flashing */
119 err = pca955x_write_psc(client, 0, 0);