2 * Generic Syscon LEDs Driver
4 * Copyright (c) 2014, Linaro Limited
5 * Author: Linus Walleij <linus.walleij@linaro.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * This driver provides system reboot functionality for APM X-Gene SoC.
23 * For system shutdown, this is board specify. If a board designer
24 * implements GPIO shutdown, use the gpio-poweroff.c driver.
27 #include <linux/of_device.h>
28 #include <linux/of_address.h>
29 #include <linux/platform_device.h>
30 #include <linux/stat.h>
31 #include <linux/slab.h>
32 #include <linux/mfd/syscon.h>
33 #include <linux/regmap.h>
34 #include <linux/leds.h>
37 * struct syscon_led - state container for syscon based LEDs
38 * @cdev: LED class device for this LED
39 * @map: regmap to access the syscon device backing this LED
40 * @offset: the offset into the syscon regmap for the LED register
41 * @mask: the bit in the register corresponding to the LED
42 * @state: current state of the LED
45 struct led_classdev cdev;
52 static void syscon_led_set(struct led_classdev *led_cdev,
53 enum led_brightness value)
55 struct syscon_led *sled =
56 container_of(led_cdev, struct syscon_led, cdev);
60 if (value == LED_OFF) {
68 ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val);
70 dev_err(sled->cdev.dev, "error updating LED status\n");
73 static const struct of_device_id syscon_match[] = {
74 { .compatible = "syscon", },
78 static int __init syscon_leds_init(void)
80 const struct of_device_id *devid;
81 struct device_node *np;
82 struct device_node *child;
84 struct platform_device *pdev;
88 np = of_find_matching_node_and_match(NULL, syscon_match,
93 map = syscon_node_to_regmap(np);
98 * If the map is there, the device should be there, we allocate
99 * memory on the syscon device's behalf here.
101 pdev = of_find_device_by_node(np);
106 for_each_available_child_of_node(np, child) {
107 struct syscon_led *sled;
110 /* Only check for register-bit-leds */
111 if (of_property_match_string(child, "compatible",
112 "register-bit-led") < 0)
115 sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
121 if (of_property_read_u32(child, "offset", &sled->offset))
123 if (of_property_read_u32(child, "mask", &sled->mask))
126 of_get_property(child, "label", NULL) ? : child->name;
127 sled->cdev.default_trigger =
128 of_get_property(child, "linux,default-trigger", NULL);
130 state = of_get_property(child, "default-state", NULL);
132 if (!strcmp(state, "keep")) {
135 ret = regmap_read(map, sled->offset, &val);
138 sled->state = !!(val & sled->mask);
139 } else if (!strcmp(state, "on")) {
141 ret = regmap_update_bits(map, sled->offset,
148 ret = regmap_update_bits(map, sled->offset,
155 sled->cdev.brightness_set = syscon_led_set;
157 ret = led_classdev_register(dev, &sled->cdev);
161 dev_info(dev, "registered LED %s\n", sled->cdev.name);
166 device_initcall(syscon_leds_init);