Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Generic Syscon LEDs Driver
0004  *
0005  * Copyright (c) 2014, Linaro Limited
0006  * Author: Linus Walleij <linus.walleij@linaro.org>
0007  */
0008 #include <linux/io.h>
0009 #include <linux/init.h>
0010 #include <linux/of_device.h>
0011 #include <linux/of_address.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/stat.h>
0014 #include <linux/slab.h>
0015 #include <linux/mfd/syscon.h>
0016 #include <linux/regmap.h>
0017 #include <linux/leds.h>
0018 
0019 /**
0020  * struct syscon_led - state container for syscon based LEDs
0021  * @cdev: LED class device for this LED
0022  * @map: regmap to access the syscon device backing this LED
0023  * @offset: the offset into the syscon regmap for the LED register
0024  * @mask: the bit in the register corresponding to the LED
0025  * @state: current state of the LED
0026  */
0027 struct syscon_led {
0028     struct led_classdev cdev;
0029     struct regmap *map;
0030     u32 offset;
0031     u32 mask;
0032     bool state;
0033 };
0034 
0035 static void syscon_led_set(struct led_classdev *led_cdev,
0036     enum led_brightness value)
0037 {
0038     struct syscon_led *sled =
0039         container_of(led_cdev, struct syscon_led, cdev);
0040     u32 val;
0041     int ret;
0042 
0043     if (value == LED_OFF) {
0044         val = 0;
0045         sled->state = false;
0046     } else {
0047         val = sled->mask;
0048         sled->state = true;
0049     }
0050 
0051     ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val);
0052     if (ret < 0)
0053         dev_err(sled->cdev.dev, "error updating LED status\n");
0054 }
0055 
0056 static int syscon_led_probe(struct platform_device *pdev)
0057 {
0058     struct led_init_data init_data = {};
0059     struct device *dev = &pdev->dev;
0060     struct device_node *np = dev_of_node(dev);
0061     struct device *parent;
0062     struct regmap *map;
0063     struct syscon_led *sled;
0064     const char *state;
0065     int ret;
0066 
0067     parent = dev->parent;
0068     if (!parent) {
0069         dev_err(dev, "no parent for syscon LED\n");
0070         return -ENODEV;
0071     }
0072     map = syscon_node_to_regmap(dev_of_node(parent));
0073     if (IS_ERR(map)) {
0074         dev_err(dev, "no regmap for syscon LED parent\n");
0075         return PTR_ERR(map);
0076     }
0077 
0078     sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
0079     if (!sled)
0080         return -ENOMEM;
0081 
0082     sled->map = map;
0083 
0084     if (of_property_read_u32(np, "offset", &sled->offset))
0085         return -EINVAL;
0086     if (of_property_read_u32(np, "mask", &sled->mask))
0087         return -EINVAL;
0088 
0089     state = of_get_property(np, "default-state", NULL);
0090     if (state) {
0091         if (!strcmp(state, "keep")) {
0092             u32 val;
0093 
0094             ret = regmap_read(map, sled->offset, &val);
0095             if (ret < 0)
0096                 return ret;
0097             sled->state = !!(val & sled->mask);
0098         } else if (!strcmp(state, "on")) {
0099             sled->state = true;
0100             ret = regmap_update_bits(map, sled->offset,
0101                          sled->mask,
0102                          sled->mask);
0103             if (ret < 0)
0104                 return ret;
0105         } else {
0106             sled->state = false;
0107             ret = regmap_update_bits(map, sled->offset,
0108                          sled->mask, 0);
0109             if (ret < 0)
0110                 return ret;
0111         }
0112     }
0113     sled->cdev.brightness_set = syscon_led_set;
0114 
0115     init_data.fwnode = of_fwnode_handle(np);
0116 
0117     ret = devm_led_classdev_register_ext(dev, &sled->cdev, &init_data);
0118     if (ret < 0)
0119         return ret;
0120 
0121     platform_set_drvdata(pdev, sled);
0122     dev_info(dev, "registered LED %s\n", sled->cdev.name);
0123 
0124     return 0;
0125 }
0126 
0127 static const struct of_device_id of_syscon_leds_match[] = {
0128     { .compatible = "register-bit-led", },
0129     {},
0130 };
0131 
0132 static struct platform_driver syscon_led_driver = {
0133     .probe      = syscon_led_probe,
0134     .driver     = {
0135         .name   = "leds-syscon",
0136         .of_match_table = of_syscon_leds_match,
0137         .suppress_bind_attrs = true,
0138     },
0139 };
0140 builtin_platform_driver(syscon_led_driver);