xref: /openbmc/linux/drivers/leds/leds-syscon.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2535f09ccSLinus Walleij /*
3535f09ccSLinus Walleij  * Generic Syscon LEDs Driver
4535f09ccSLinus Walleij  *
5535f09ccSLinus Walleij  * Copyright (c) 2014, Linaro Limited
6535f09ccSLinus Walleij  * Author: Linus Walleij <linus.walleij@linaro.org>
7535f09ccSLinus Walleij  */
8535f09ccSLinus Walleij #include <linux/io.h>
9f7d98a65SPaul Gortmaker #include <linux/init.h>
10*3192f141SRob Herring #include <linux/of.h>
11535f09ccSLinus Walleij #include <linux/platform_device.h>
12535f09ccSLinus Walleij #include <linux/stat.h>
13535f09ccSLinus Walleij #include <linux/slab.h>
14535f09ccSLinus Walleij #include <linux/mfd/syscon.h>
15535f09ccSLinus Walleij #include <linux/regmap.h>
16535f09ccSLinus Walleij #include <linux/leds.h>
17535f09ccSLinus Walleij 
18535f09ccSLinus Walleij /**
19535f09ccSLinus Walleij  * struct syscon_led - state container for syscon based LEDs
20535f09ccSLinus Walleij  * @cdev: LED class device for this LED
21535f09ccSLinus Walleij  * @map: regmap to access the syscon device backing this LED
22535f09ccSLinus Walleij  * @offset: the offset into the syscon regmap for the LED register
23535f09ccSLinus Walleij  * @mask: the bit in the register corresponding to the LED
24535f09ccSLinus Walleij  * @state: current state of the LED
25535f09ccSLinus Walleij  */
26535f09ccSLinus Walleij struct syscon_led {
27535f09ccSLinus Walleij 	struct led_classdev cdev;
28535f09ccSLinus Walleij 	struct regmap *map;
29535f09ccSLinus Walleij 	u32 offset;
30535f09ccSLinus Walleij 	u32 mask;
31535f09ccSLinus Walleij 	bool state;
32535f09ccSLinus Walleij };
33535f09ccSLinus Walleij 
syscon_led_set(struct led_classdev * led_cdev,enum led_brightness value)34535f09ccSLinus Walleij static void syscon_led_set(struct led_classdev *led_cdev,
35535f09ccSLinus Walleij 	enum led_brightness value)
36535f09ccSLinus Walleij {
37535f09ccSLinus Walleij 	struct syscon_led *sled =
38535f09ccSLinus Walleij 		container_of(led_cdev, struct syscon_led, cdev);
39535f09ccSLinus Walleij 	u32 val;
40535f09ccSLinus Walleij 	int ret;
41535f09ccSLinus Walleij 
42535f09ccSLinus Walleij 	if (value == LED_OFF) {
43535f09ccSLinus Walleij 		val = 0;
44535f09ccSLinus Walleij 		sled->state = false;
45535f09ccSLinus Walleij 	} else {
46535f09ccSLinus Walleij 		val = sled->mask;
47535f09ccSLinus Walleij 		sled->state = true;
48535f09ccSLinus Walleij 	}
49535f09ccSLinus Walleij 
50535f09ccSLinus Walleij 	ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val);
51535f09ccSLinus Walleij 	if (ret < 0)
52535f09ccSLinus Walleij 		dev_err(sled->cdev.dev, "error updating LED status\n");
53535f09ccSLinus Walleij }
54535f09ccSLinus Walleij 
syscon_led_probe(struct platform_device * pdev)55a917d4b4SLinus Walleij static int syscon_led_probe(struct platform_device *pdev)
56535f09ccSLinus Walleij {
5700663196SMarek Behún 	struct led_init_data init_data = {};
58a917d4b4SLinus Walleij 	struct device *dev = &pdev->dev;
598853c95eSMarek Behún 	struct device_node *np = dev_of_node(dev);
60a917d4b4SLinus Walleij 	struct device *parent;
61a917d4b4SLinus Walleij 	struct regmap *map;
62535f09ccSLinus Walleij 	struct syscon_led *sled;
63265d313eSAndy Shevchenko 	enum led_default_state state;
64265d313eSAndy Shevchenko 	u32 value;
65a917d4b4SLinus Walleij 	int ret;
66535f09ccSLinus Walleij 
67a917d4b4SLinus Walleij 	parent = dev->parent;
68a917d4b4SLinus Walleij 	if (!parent) {
69a917d4b4SLinus Walleij 		dev_err(dev, "no parent for syscon LED\n");
70a917d4b4SLinus Walleij 		return -ENODEV;
71a917d4b4SLinus Walleij 	}
728853c95eSMarek Behún 	map = syscon_node_to_regmap(dev_of_node(parent));
73991a3f61SBjorn Andersson 	if (IS_ERR(map)) {
74a917d4b4SLinus Walleij 		dev_err(dev, "no regmap for syscon LED parent\n");
75991a3f61SBjorn Andersson 		return PTR_ERR(map);
76a917d4b4SLinus Walleij 	}
77535f09ccSLinus Walleij 
78535f09ccSLinus Walleij 	sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
79535f09ccSLinus Walleij 	if (!sled)
80535f09ccSLinus Walleij 		return -ENOMEM;
81535f09ccSLinus Walleij 
82535f09ccSLinus Walleij 	sled->map = map;
83535f09ccSLinus Walleij 
84a917d4b4SLinus Walleij 	if (of_property_read_u32(np, "offset", &sled->offset))
85535f09ccSLinus Walleij 		return -EINVAL;
86a917d4b4SLinus Walleij 	if (of_property_read_u32(np, "mask", &sled->mask))
87535f09ccSLinus Walleij 		return -EINVAL;
88535f09ccSLinus Walleij 
89265d313eSAndy Shevchenko 	init_data.fwnode = of_fwnode_handle(np);
90535f09ccSLinus Walleij 
91265d313eSAndy Shevchenko 	state = led_init_default_state_get(init_data.fwnode);
92265d313eSAndy Shevchenko 	switch (state) {
93265d313eSAndy Shevchenko 	case LEDS_DEFSTATE_ON:
94265d313eSAndy Shevchenko 		ret = regmap_update_bits(map, sled->offset, sled->mask, sled->mask);
95535f09ccSLinus Walleij 		if (ret < 0)
96535f09ccSLinus Walleij 			return ret;
97535f09ccSLinus Walleij 		sled->state = true;
98265d313eSAndy Shevchenko 		break;
99265d313eSAndy Shevchenko 	case LEDS_DEFSTATE_KEEP:
100265d313eSAndy Shevchenko 		ret = regmap_read(map, sled->offset, &value);
101535f09ccSLinus Walleij 		if (ret < 0)
102535f09ccSLinus Walleij 			return ret;
103265d313eSAndy Shevchenko 		sled->state = !!(value & sled->mask);
104265d313eSAndy Shevchenko 		break;
105265d313eSAndy Shevchenko 	default:
106265d313eSAndy Shevchenko 		ret = regmap_update_bits(map, sled->offset, sled->mask, 0);
107265d313eSAndy Shevchenko 		if (ret < 0)
108265d313eSAndy Shevchenko 			return ret;
109535f09ccSLinus Walleij 		sled->state = false;
110535f09ccSLinus Walleij 	}
111535f09ccSLinus Walleij 	sled->cdev.brightness_set = syscon_led_set;
112535f09ccSLinus Walleij 
11300663196SMarek Behún 	ret = devm_led_classdev_register_ext(dev, &sled->cdev, &init_data);
114535f09ccSLinus Walleij 	if (ret < 0)
115535f09ccSLinus Walleij 		return ret;
116535f09ccSLinus Walleij 
117a917d4b4SLinus Walleij 	platform_set_drvdata(pdev, sled);
118535f09ccSLinus Walleij 	dev_info(dev, "registered LED %s\n", sled->cdev.name);
119a917d4b4SLinus Walleij 
1203f6e42c8SLinus Walleij 	return 0;
1213f6e42c8SLinus Walleij }
1223f6e42c8SLinus Walleij 
123a917d4b4SLinus Walleij static const struct of_device_id of_syscon_leds_match[] = {
124a917d4b4SLinus Walleij 	{ .compatible = "register-bit-led", },
125a917d4b4SLinus Walleij 	{},
126a917d4b4SLinus Walleij };
127a917d4b4SLinus Walleij 
128a917d4b4SLinus Walleij static struct platform_driver syscon_led_driver = {
129a917d4b4SLinus Walleij 	.probe		= syscon_led_probe,
130a917d4b4SLinus Walleij 	.driver		= {
131a917d4b4SLinus Walleij 		.name	= "leds-syscon",
132a917d4b4SLinus Walleij 		.of_match_table = of_syscon_leds_match,
133f7d98a65SPaul Gortmaker 		.suppress_bind_attrs = true,
134a917d4b4SLinus Walleij 	},
135a917d4b4SLinus Walleij };
136f7d98a65SPaul Gortmaker builtin_platform_driver(syscon_led_driver);
137