drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / leds / leds-syscon.c
blobd633ad519d0cdc52ad2ba4790cbcfde7055a2484
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Generic Syscon LEDs Driver
5 * Copyright (c) 2014, Linaro Limited
6 * Author: Linus Walleij <linus.walleij@linaro.org>
7 */
8 #include <linux/io.h>
9 #include <linux/init.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/stat.h>
13 #include <linux/slab.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/regmap.h>
16 #include <linux/leds.h>
18 /**
19 * struct syscon_led - state container for syscon based LEDs
20 * @cdev: LED class device for this LED
21 * @map: regmap to access the syscon device backing this LED
22 * @offset: the offset into the syscon regmap for the LED register
23 * @mask: the bit in the register corresponding to the LED
24 * @state: current state of the LED
26 struct syscon_led {
27 struct led_classdev cdev;
28 struct regmap *map;
29 u32 offset;
30 u32 mask;
31 bool state;
34 static void syscon_led_set(struct led_classdev *led_cdev,
35 enum led_brightness value)
37 struct syscon_led *sled =
38 container_of(led_cdev, struct syscon_led, cdev);
39 u32 val;
40 int ret;
42 if (value == LED_OFF) {
43 val = 0;
44 sled->state = false;
45 } else {
46 val = sled->mask;
47 sled->state = true;
50 ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val);
51 if (ret < 0)
52 dev_err(sled->cdev.dev, "error updating LED status\n");
55 static int syscon_led_probe(struct platform_device *pdev)
57 struct led_init_data init_data = {};
58 struct device *dev = &pdev->dev;
59 struct device_node *np = dev_of_node(dev);
60 struct device *parent;
61 struct regmap *map;
62 struct syscon_led *sled;
63 enum led_default_state state;
64 u32 value;
65 int ret;
67 parent = dev->parent;
68 if (!parent) {
69 dev_err(dev, "no parent for syscon LED\n");
70 return -ENODEV;
72 map = syscon_node_to_regmap(dev_of_node(parent));
73 if (IS_ERR(map)) {
74 dev_err(dev, "no regmap for syscon LED parent\n");
75 return PTR_ERR(map);
78 sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
79 if (!sled)
80 return -ENOMEM;
82 sled->map = map;
84 if (of_property_read_u32(np, "reg", &sled->offset) &&
85 of_property_read_u32(np, "offset", &sled->offset))
86 return -EINVAL;
87 if (of_property_read_u32(np, "mask", &sled->mask))
88 return -EINVAL;
90 init_data.fwnode = of_fwnode_handle(np);
92 state = led_init_default_state_get(init_data.fwnode);
93 switch (state) {
94 case LEDS_DEFSTATE_ON:
95 ret = regmap_update_bits(map, sled->offset, sled->mask, sled->mask);
96 if (ret < 0)
97 return ret;
98 sled->state = true;
99 break;
100 case LEDS_DEFSTATE_KEEP:
101 ret = regmap_read(map, sled->offset, &value);
102 if (ret < 0)
103 return ret;
104 sled->state = !!(value & sled->mask);
105 break;
106 default:
107 ret = regmap_update_bits(map, sled->offset, sled->mask, 0);
108 if (ret < 0)
109 return ret;
110 sled->state = false;
112 sled->cdev.brightness_set = syscon_led_set;
114 ret = devm_led_classdev_register_ext(dev, &sled->cdev, &init_data);
115 if (ret < 0)
116 return ret;
118 platform_set_drvdata(pdev, sled);
119 dev_info(dev, "registered LED %s\n", sled->cdev.name);
121 return 0;
124 static const struct of_device_id of_syscon_leds_match[] = {
125 { .compatible = "register-bit-led", },
129 static struct platform_driver syscon_led_driver = {
130 .probe = syscon_led_probe,
131 .driver = {
132 .name = "leds-syscon",
133 .of_match_table = of_syscon_leds_match,
134 .suppress_bind_attrs = true,
137 builtin_platform_driver(syscon_led_driver);