055801f09605a48b244d3d7695c83b8360593280
[openwrt/staging/noltari.git] /
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 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 */
22 char name[32];
23 u32 type;
24 + int default_state;
25 const char *default_trigger;
26 };
27
28 @@ -439,6 +440,7 @@ pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
29
30 device_for_each_child_node(&client->dev, child) {
31 const char *name;
32 + const char *state;
33 u32 reg;
34 int res;
35
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);
40 +
41 + if (!fwnode_property_read_string(child, "default-state",
42 + &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;
47 + else
48 + led->default_state = LEDS_GPIO_DEFSTATE_OFF;
49 + } else {
50 + led->default_state = LEDS_GPIO_DEFSTATE_OFF;
51 + }
52 }
53
54 pdata->num_leds = chip->bits;
55 @@ -485,6 +499,7 @@ static int pca955x_probe(struct i2c_client *client,
56 int i, err;
57 struct pca955x_platform_data *pdata;
58 int ngpios = 0;
59 + bool keep_pwm = false;
60
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;
66
67 + if (pdata->leds[i].default_state ==
68 + LEDS_GPIO_DEFSTATE_OFF) {
69 + err = pca955x_led_set(led, LED_OFF);
70 + if (err)
71 + return err;
72 + } else if (pdata->leds[i].default_state ==
73 + LEDS_GPIO_DEFSTATE_ON) {
74 + err = pca955x_led_set(led, LED_FULL);
75 + if (err)
76 + return err;
77 + }
78 +
79 err = devm_led_classdev_register(&client->dev, led);
80 if (err)
81 return err;
82
83 - /* Turn off LED */
84 - err = pca955x_led_set(led, LED_OFF);
85 - if (err)
86 - return err;
87 + /*
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.
92 + */
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)
98 + keep_pwm = true;
99 + }
100 }
101 }
102
103 @@ -581,10 +617,12 @@ static int pca955x_probe(struct i2c_client *client,
104 if (err)
105 return err;
106
107 - /* PWM1 is used for variable brightness, default to OFF */
108 - err = pca955x_write_pwm(client, 1, 0);
109 - if (err)
110 - return err;
111 + if (!keep_pwm) {
112 + /* PWM1 is used for variable brightness, default to OFF */
113 + err = pca955x_write_pwm(client, 1, 0);
114 + if (err)
115 + return err;
116 + }
117
118 /* Set to fast frequency so we do not see flashing */
119 err = pca955x_write_psc(client, 0, 0);