]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - drivers/leds/led-class.c
Merge tag 'pwm/for-5.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[linux.git] / drivers / leds / led-class.c
index 438774315e6c480622b648b4ca437e8b10cb5a91..1fc40e8af75eb7afd62effd5e6908e8e172a995f 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/spinlock.h>
 #include <linux/timer.h>
 #include <uapi/linux/uleds.h>
+#include <linux/of.h>
 #include "leds.h"
 
 static struct class *leds_class;
@@ -214,6 +215,98 @@ static int led_resume(struct device *dev)
 
 static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
 
+/**
+ * of_led_get() - request a LED device via the LED framework
+ * @np: device node to get the LED device from
+ * @index: the index of the LED
+ *
+ * Returns the LED device parsed from the phandle specified in the "leds"
+ * property of a device tree node or a negative error-code on failure.
+ */
+struct led_classdev *of_led_get(struct device_node *np, int index)
+{
+       struct device *led_dev;
+       struct led_classdev *led_cdev;
+       struct device_node *led_node;
+
+       led_node = of_parse_phandle(np, "leds", index);
+       if (!led_node)
+               return ERR_PTR(-ENOENT);
+
+       led_dev = class_find_device_by_of_node(leds_class, led_node);
+       of_node_put(led_node);
+
+       if (!led_dev)
+               return ERR_PTR(-EPROBE_DEFER);
+
+       led_cdev = dev_get_drvdata(led_dev);
+
+       if (!try_module_get(led_cdev->dev->parent->driver->owner))
+               return ERR_PTR(-ENODEV);
+
+       return led_cdev;
+}
+EXPORT_SYMBOL_GPL(of_led_get);
+
+/**
+ * led_put() - release a LED device
+ * @led_cdev: LED device
+ */
+void led_put(struct led_classdev *led_cdev)
+{
+       module_put(led_cdev->dev->parent->driver->owner);
+}
+EXPORT_SYMBOL_GPL(led_put);
+
+static void devm_led_release(struct device *dev, void *res)
+{
+       struct led_classdev **p = res;
+
+       led_put(*p);
+}
+
+/**
+ * devm_of_led_get - Resource-managed request of a LED device
+ * @dev:       LED consumer
+ * @index:     index of the LED to obtain in the consumer
+ *
+ * The device node of the device is parse to find the request LED device.
+ * The LED device returned from this function is automatically released
+ * on driver detach.
+ *
+ * @return a pointer to a LED device or ERR_PTR(errno) on failure.
+ */
+struct led_classdev *__must_check devm_of_led_get(struct device *dev,
+                                                 int index)
+{
+       struct led_classdev *led;
+       struct led_classdev **dr;
+
+       if (!dev)
+               return ERR_PTR(-EINVAL);
+
+       /* Not using device tree? */
+       if (!IS_ENABLED(CONFIG_OF) || !dev->of_node)
+               return ERR_PTR(-ENOTSUPP);
+
+       led = of_led_get(dev->of_node, index);
+       if (IS_ERR(led))
+               return led;
+
+       dr = devres_alloc(devm_led_release, sizeof(struct led_classdev *),
+                         GFP_KERNEL);
+       if (!dr) {
+               led_put(led);
+               return ERR_PTR(-ENOMEM);
+       }
+
+       *dr = led;
+       devres_add(dev, dr);
+
+       return led;
+}
+EXPORT_SYMBOL_GPL(devm_of_led_get);
+
 static int led_classdev_next_name(const char *init_name, char *name,
                                  size_t len)
 {
@@ -276,8 +369,10 @@ int led_classdev_register_ext(struct device *parent,
                mutex_unlock(&led_cdev->led_access);
                return PTR_ERR(led_cdev->dev);
        }
-       if (init_data && init_data->fwnode)
+       if (init_data && init_data->fwnode) {
                led_cdev->dev->fwnode = init_data->fwnode;
+               led_cdev->dev->of_node = to_of_node(init_data->fwnode);
+       }
 
        if (ret)
                dev_warn(parent, "Led %s renamed to %s due to name collision",