hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/leds/leds-gpio.c
....@@ -1,14 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * LEDs driver for GPIOs
34 *
45 * Copyright (C) 2007 8D Technologies inc.
56 * Raphael Assenat <raph@8d.com>
67 * Copyright (C) 2008 Freescale Semiconductor, Inc.
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License version 2 as
10
- * published by the Free Software Foundation.
11
- *
128 */
139 #include <linux/err.h>
1410 #include <linux/gpio.h>
....@@ -77,40 +73,11 @@
7773
7874 static int create_gpio_led(const struct gpio_led *template,
7975 struct gpio_led_data *led_dat, struct device *parent,
80
- struct device_node *np, gpio_blink_set_t blink_set)
76
+ struct fwnode_handle *fwnode, gpio_blink_set_t blink_set)
8177 {
78
+ struct led_init_data init_data = {};
8279 int ret, state;
8380
84
- led_dat->gpiod = template->gpiod;
85
- if (!led_dat->gpiod) {
86
- /*
87
- * This is the legacy code path for platform code that
88
- * still uses GPIO numbers. Ultimately we would like to get
89
- * rid of this block completely.
90
- */
91
- unsigned long flags = GPIOF_OUT_INIT_LOW;
92
-
93
- /* skip leds that aren't available */
94
- if (!gpio_is_valid(template->gpio)) {
95
- dev_info(parent, "Skipping unavailable LED gpio %d (%s)\n",
96
- template->gpio, template->name);
97
- return 0;
98
- }
99
-
100
- if (template->active_low)
101
- flags |= GPIOF_ACTIVE_LOW;
102
-
103
- ret = devm_gpio_request_one(parent, template->gpio, flags,
104
- template->name);
105
- if (ret < 0)
106
- return ret;
107
-
108
- led_dat->gpiod = gpio_to_desc(template->gpio);
109
- if (!led_dat->gpiod)
110
- return -EINVAL;
111
- }
112
-
113
- led_dat->cdev.name = template->name;
11481 led_dat->cdev.default_trigger = template->default_trigger;
11582 led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
11683 if (!led_dat->can_sleep)
....@@ -141,19 +108,22 @@
141108 if (ret < 0)
142109 return ret;
143110
144
- return devm_of_led_classdev_register(parent, np, &led_dat->cdev);
111
+ if (template->name) {
112
+ led_dat->cdev.name = template->name;
113
+ ret = devm_led_classdev_register(parent, &led_dat->cdev);
114
+ } else {
115
+ init_data.fwnode = fwnode;
116
+ ret = devm_led_classdev_register_ext(parent, &led_dat->cdev,
117
+ &init_data);
118
+ }
119
+
120
+ return ret;
145121 }
146122
147123 struct gpio_leds_priv {
148124 int num_leds;
149125 struct gpio_led_data leds[];
150126 };
151
-
152
-static inline int sizeof_gpio_leds_priv(int num_leds)
153
-{
154
- return sizeof(struct gpio_leds_priv) +
155
- (sizeof(struct gpio_led_data) * num_leds);
156
-}
157127
158128 static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
159129 {
....@@ -166,7 +136,7 @@
166136 if (!count)
167137 return ERR_PTR(-ENODEV);
168138
169
- priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL);
139
+ priv = devm_kzalloc(dev, struct_size(priv, leds, count), GFP_KERNEL);
170140 if (!priv)
171141 return ERR_PTR(-ENOMEM);
172142
....@@ -174,26 +144,21 @@
174144 struct gpio_led_data *led_dat = &priv->leds[priv->num_leds];
175145 struct gpio_led led = {};
176146 const char *state = NULL;
177
- struct device_node *np = to_of_node(child);
178147
179
- ret = fwnode_property_read_string(child, "label", &led.name);
180
- if (ret && IS_ENABLED(CONFIG_OF) && np)
181
- led.name = np->name;
182
- if (!led.name) {
183
- fwnode_handle_put(child);
184
- return ERR_PTR(-EINVAL);
185
- }
186
-
148
+ /*
149
+ * Acquire gpiod from DT with uninitialized label, which
150
+ * will be updated after LED class device is registered,
151
+ * Only then the final LED name is known.
152
+ */
187153 led.gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL, child,
188154 GPIOD_ASIS,
189
- led.name);
155
+ NULL);
190156 if (IS_ERR(led.gpiod)) {
191157 fwnode_handle_put(child);
192158 return ERR_CAST(led.gpiod);
193159 }
194160
195
- fwnode_property_read_string(child, "linux,default-trigger",
196
- &led.default_trigger);
161
+ led_dat->gpiod = led.gpiod;
197162
198163 if (!fwnode_property_read_string(child, "default-state",
199164 &state)) {
....@@ -212,12 +177,14 @@
212177 if (fwnode_property_present(child, "panic-indicator"))
213178 led.panic_indicator = 1;
214179
215
- ret = create_gpio_led(&led, led_dat, dev, np, NULL);
180
+ ret = create_gpio_led(&led, led_dat, dev, child, NULL);
216181 if (ret < 0) {
217182 fwnode_handle_put(child);
218183 return ERR_PTR(ret);
219184 }
220
- led_dat->cdev.dev->of_node = np;
185
+ /* Set gpiod label to match the corresponding LED name. */
186
+ gpiod_set_consumer_name(led_dat->gpiod,
187
+ led_dat->cdev.dev->kobj.name);
221188 priv->num_leds++;
222189 }
223190
....@@ -231,6 +198,52 @@
231198
232199 MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
233200
201
+static struct gpio_desc *gpio_led_get_gpiod(struct device *dev, int idx,
202
+ const struct gpio_led *template)
203
+{
204
+ struct gpio_desc *gpiod;
205
+ unsigned long flags = GPIOF_OUT_INIT_LOW;
206
+ int ret;
207
+
208
+ /*
209
+ * This means the LED does not come from the device tree
210
+ * or ACPI, so let's try just getting it by index from the
211
+ * device, this will hit the board file, if any and get
212
+ * the GPIO from there.
213
+ */
214
+ gpiod = devm_gpiod_get_index(dev, NULL, idx, GPIOD_OUT_LOW);
215
+ if (!IS_ERR(gpiod)) {
216
+ gpiod_set_consumer_name(gpiod, template->name);
217
+ return gpiod;
218
+ }
219
+ if (PTR_ERR(gpiod) != -ENOENT)
220
+ return gpiod;
221
+
222
+ /*
223
+ * This is the legacy code path for platform code that
224
+ * still uses GPIO numbers. Ultimately we would like to get
225
+ * rid of this block completely.
226
+ */
227
+
228
+ /* skip leds that aren't available */
229
+ if (!gpio_is_valid(template->gpio))
230
+ return ERR_PTR(-ENOENT);
231
+
232
+ if (template->active_low)
233
+ flags |= GPIOF_ACTIVE_LOW;
234
+
235
+ ret = devm_gpio_request_one(dev, template->gpio, flags,
236
+ template->name);
237
+ if (ret < 0)
238
+ return ERR_PTR(ret);
239
+
240
+ gpiod = gpio_to_desc(template->gpio);
241
+ if (!gpiod)
242
+ return ERR_PTR(-EINVAL);
243
+
244
+ return gpiod;
245
+}
246
+
234247 static int gpio_led_probe(struct platform_device *pdev)
235248 {
236249 struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
....@@ -238,15 +251,29 @@
238251 int i, ret = 0;
239252
240253 if (pdata && pdata->num_leds) {
241
- priv = devm_kzalloc(&pdev->dev,
242
- sizeof_gpio_leds_priv(pdata->num_leds),
243
- GFP_KERNEL);
254
+ priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, pdata->num_leds),
255
+ GFP_KERNEL);
244256 if (!priv)
245257 return -ENOMEM;
246258
247259 priv->num_leds = pdata->num_leds;
248260 for (i = 0; i < priv->num_leds; i++) {
249
- ret = create_gpio_led(&pdata->leds[i], &priv->leds[i],
261
+ const struct gpio_led *template = &pdata->leds[i];
262
+ struct gpio_led_data *led_dat = &priv->leds[i];
263
+
264
+ if (template->gpiod)
265
+ led_dat->gpiod = template->gpiod;
266
+ else
267
+ led_dat->gpiod =
268
+ gpio_led_get_gpiod(&pdev->dev,
269
+ i, template);
270
+ if (IS_ERR(led_dat->gpiod)) {
271
+ dev_info(&pdev->dev, "Skipping unavailable LED gpio %d (%s)\n",
272
+ template->gpio, template->name);
273
+ continue;
274
+ }
275
+
276
+ ret = create_gpio_led(template, led_dat,
250277 &pdev->dev, NULL,
251278 pdata->gpio_blink_set);
252279 if (ret < 0)