hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/leds/led-class.c
....@@ -1,12 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * LED Class Core
34 *
45 * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
56 * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com>
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License version 2 as
9
- * published by the Free Software Foundation.
107 */
118
129 #include <linux/ctype.h>
....@@ -17,10 +14,12 @@
1714 #include <linux/leds.h>
1815 #include <linux/list.h>
1916 #include <linux/module.h>
17
+#include <linux/property.h>
2018 #include <linux/slab.h>
2119 #include <linux/spinlock.h>
2220 #include <linux/timer.h>
2321 #include <uapi/linux/uleds.h>
22
+#include <linux/of.h>
2423 #include "leds.h"
2524
2625 static struct class *leds_class;
....@@ -57,6 +56,7 @@
5756 if (state == LED_OFF)
5857 led_trigger_remove(led_cdev);
5958 led_set_brightness(led_cdev, state);
59
+ flush_work(&led_cdev->set_brightness_work);
6060
6161 ret = size;
6262 unlock:
....@@ -75,13 +75,13 @@
7575 static DEVICE_ATTR_RO(max_brightness);
7676
7777 #ifdef CONFIG_LEDS_TRIGGERS
78
-static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
79
-static struct attribute *led_trigger_attrs[] = {
80
- &dev_attr_trigger.attr,
78
+static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
79
+static struct bin_attribute *led_trigger_bin_attrs[] = {
80
+ &bin_attr_trigger,
8181 NULL,
8282 };
8383 static const struct attribute_group led_trigger_group = {
84
- .attrs = led_trigger_attrs,
84
+ .bin_attrs = led_trigger_bin_attrs,
8585 };
8686 #endif
8787
....@@ -216,12 +216,97 @@
216216
217217 static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
218218
219
-static int match_name(struct device *dev, const void *data)
219
+/**
220
+ * of_led_get() - request a LED device via the LED framework
221
+ * @np: device node to get the LED device from
222
+ * @index: the index of the LED
223
+ *
224
+ * Returns the LED device parsed from the phandle specified in the "leds"
225
+ * property of a device tree node or a negative error-code on failure.
226
+ */
227
+struct led_classdev *of_led_get(struct device_node *np, int index)
220228 {
221
- if (!dev_name(dev))
222
- return 0;
223
- return !strcmp(dev_name(dev), (char *)data);
229
+ struct device *led_dev;
230
+ struct led_classdev *led_cdev;
231
+ struct device_node *led_node;
232
+
233
+ led_node = of_parse_phandle(np, "leds", index);
234
+ if (!led_node)
235
+ return ERR_PTR(-ENOENT);
236
+
237
+ led_dev = class_find_device_by_of_node(leds_class, led_node);
238
+ of_node_put(led_node);
239
+ put_device(led_dev);
240
+
241
+ if (!led_dev)
242
+ return ERR_PTR(-EPROBE_DEFER);
243
+
244
+ led_cdev = dev_get_drvdata(led_dev);
245
+
246
+ if (!try_module_get(led_cdev->dev->parent->driver->owner)) {
247
+ put_device(led_cdev->dev);
248
+ return ERR_PTR(-ENODEV);
249
+ }
250
+
251
+ return led_cdev;
224252 }
253
+EXPORT_SYMBOL_GPL(of_led_get);
254
+
255
+/**
256
+ * led_put() - release a LED device
257
+ * @led_cdev: LED device
258
+ */
259
+void led_put(struct led_classdev *led_cdev)
260
+{
261
+ module_put(led_cdev->dev->parent->driver->owner);
262
+ put_device(led_cdev->dev);
263
+}
264
+EXPORT_SYMBOL_GPL(led_put);
265
+
266
+static void devm_led_release(struct device *dev, void *res)
267
+{
268
+ struct led_classdev **p = res;
269
+
270
+ led_put(*p);
271
+}
272
+
273
+/**
274
+ * devm_of_led_get - Resource-managed request of a LED device
275
+ * @dev: LED consumer
276
+ * @index: index of the LED to obtain in the consumer
277
+ *
278
+ * The device node of the device is parse to find the request LED device.
279
+ * The LED device returned from this function is automatically released
280
+ * on driver detach.
281
+ *
282
+ * @return a pointer to a LED device or ERR_PTR(errno) on failure.
283
+ */
284
+struct led_classdev *__must_check devm_of_led_get(struct device *dev,
285
+ int index)
286
+{
287
+ struct led_classdev *led;
288
+ struct led_classdev **dr;
289
+
290
+ if (!dev)
291
+ return ERR_PTR(-EINVAL);
292
+
293
+ led = of_led_get(dev->of_node, index);
294
+ if (IS_ERR(led))
295
+ return led;
296
+
297
+ dr = devres_alloc(devm_led_release, sizeof(struct led_classdev *),
298
+ GFP_KERNEL);
299
+ if (!dr) {
300
+ led_put(led);
301
+ return ERR_PTR(-ENOMEM);
302
+ }
303
+
304
+ *dr = led;
305
+ devres_add(dev, dr);
306
+
307
+ return led;
308
+}
309
+EXPORT_SYMBOL_GPL(devm_of_led_get);
225310
226311 static int led_classdev_next_name(const char *init_name, char *name,
227312 size_t len)
....@@ -233,7 +318,7 @@
233318 strlcpy(name, init_name, len);
234319
235320 while ((ret < len) &&
236
- (dev = class_find_device(leds_class, NULL, name, match_name))) {
321
+ (dev = class_find_device_by_name(leds_class, name))) {
237322 put_device(dev);
238323 ret = snprintf(name, len, "%s_%u", init_name, ++i);
239324 }
....@@ -245,40 +330,65 @@
245330 }
246331
247332 /**
248
- * of_led_classdev_register - register a new object of led_classdev class.
333
+ * led_classdev_register_ext - register a new object of led_classdev class
334
+ * with init data.
249335 *
250336 * @parent: parent of LED device
251337 * @led_cdev: the led_classdev structure for this device.
252
- * @np: DT node describing this LED
338
+ * @init_data: LED class device initialization data
253339 */
254
-int of_led_classdev_register(struct device *parent, struct device_node *np,
255
- struct led_classdev *led_cdev)
340
+int led_classdev_register_ext(struct device *parent,
341
+ struct led_classdev *led_cdev,
342
+ struct led_init_data *init_data)
256343 {
257
- char name[LED_MAX_NAME_SIZE];
344
+ char composed_name[LED_MAX_NAME_SIZE];
345
+ char final_name[LED_MAX_NAME_SIZE];
346
+ const char *proposed_name = composed_name;
258347 int ret;
259348
260
- ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
349
+ if (init_data) {
350
+ if (init_data->devname_mandatory && !init_data->devicename) {
351
+ dev_err(parent, "Mandatory device name is missing");
352
+ return -EINVAL;
353
+ }
354
+ ret = led_compose_name(parent, init_data, composed_name);
355
+ if (ret < 0)
356
+ return ret;
357
+
358
+ if (init_data->fwnode)
359
+ fwnode_property_read_string(init_data->fwnode,
360
+ "linux,default-trigger",
361
+ &led_cdev->default_trigger);
362
+ } else {
363
+ proposed_name = led_cdev->name;
364
+ }
365
+
366
+ ret = led_classdev_next_name(proposed_name, final_name, sizeof(final_name));
261367 if (ret < 0)
262368 return ret;
263369
264370 mutex_init(&led_cdev->led_access);
265371 mutex_lock(&led_cdev->led_access);
266372 led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
267
- led_cdev, led_cdev->groups, "%s", name);
373
+ led_cdev, led_cdev->groups, "%s", final_name);
268374 if (IS_ERR(led_cdev->dev)) {
269375 mutex_unlock(&led_cdev->led_access);
270376 return PTR_ERR(led_cdev->dev);
271377 }
272
- led_cdev->dev->of_node = np;
378
+ if (init_data && init_data->fwnode) {
379
+ led_cdev->dev->fwnode = init_data->fwnode;
380
+ led_cdev->dev->of_node = to_of_node(init_data->fwnode);
381
+ }
273382
274383 if (ret)
275384 dev_warn(parent, "Led %s renamed to %s due to name collision",
276
- led_cdev->name, dev_name(led_cdev->dev));
385
+ proposed_name, dev_name(led_cdev->dev));
277386
278387 if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
279388 ret = led_add_brightness_hw_changed(led_cdev);
280389 if (ret) {
281390 device_unregister(led_cdev->dev);
391
+ led_cdev->dev = NULL;
282392 mutex_unlock(&led_cdev->led_access);
283393 return ret;
284394 }
....@@ -314,7 +424,7 @@
314424
315425 return 0;
316426 }
317
-EXPORT_SYMBOL_GPL(of_led_classdev_register);
427
+EXPORT_SYMBOL_GPL(led_classdev_register_ext);
318428
319429 /**
320430 * led_classdev_unregister - unregisters a object of led_properties class.
....@@ -324,6 +434,9 @@
324434 */
325435 void led_classdev_unregister(struct led_classdev *led_cdev)
326436 {
437
+ if (IS_ERR_OR_NULL(led_cdev->dev))
438
+ return;
439
+
327440 #ifdef CONFIG_LEDS_TRIGGERS
328441 down_write(&led_cdev->trigger_lock);
329442 if (led_cdev->trigger)
....@@ -359,14 +472,15 @@
359472 }
360473
361474 /**
362
- * devm_of_led_classdev_register - resource managed led_classdev_register()
475
+ * devm_led_classdev_register_ext - resource managed led_classdev_register_ext()
363476 *
364477 * @parent: parent of LED device
365478 * @led_cdev: the led_classdev structure for this device.
479
+ * @init_data: LED class device initialization data
366480 */
367
-int devm_of_led_classdev_register(struct device *parent,
368
- struct device_node *np,
369
- struct led_classdev *led_cdev)
481
+int devm_led_classdev_register_ext(struct device *parent,
482
+ struct led_classdev *led_cdev,
483
+ struct led_init_data *init_data)
370484 {
371485 struct led_classdev **dr;
372486 int rc;
....@@ -375,7 +489,7 @@
375489 if (!dr)
376490 return -ENOMEM;
377491
378
- rc = of_led_classdev_register(parent, np, led_cdev);
492
+ rc = led_classdev_register_ext(parent, led_cdev, init_data);
379493 if (rc) {
380494 devres_free(dr);
381495 return rc;
....@@ -386,11 +500,11 @@
386500
387501 return 0;
388502 }
389
-EXPORT_SYMBOL_GPL(devm_of_led_classdev_register);
503
+EXPORT_SYMBOL_GPL(devm_led_classdev_register_ext);
390504
391505 static int devm_led_classdev_match(struct device *dev, void *res, void *data)
392506 {
393
- struct led_cdev **p = res;
507
+ struct led_classdev **p = res;
394508
395509 if (WARN_ON(!p || !*p))
396510 return 0;