Commit | Line | Data |
---|---|---|
535f09cc LW |
1 | /* |
2 | * Generic Syscon LEDs Driver | |
3 | * | |
4 | * Copyright (c) 2014, Linaro Limited | |
5 | * Author: Linus Walleij <linus.walleij@linaro.org> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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, | |
20 | * MA 02111-1307 USA | |
535f09cc LW |
21 | */ |
22 | #include <linux/io.h> | |
23 | #include <linux/of_device.h> | |
24 | #include <linux/of_address.h> | |
25 | #include <linux/platform_device.h> | |
26 | #include <linux/stat.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/mfd/syscon.h> | |
29 | #include <linux/regmap.h> | |
30 | #include <linux/leds.h> | |
31 | ||
32 | /** | |
33 | * struct syscon_led - state container for syscon based LEDs | |
34 | * @cdev: LED class device for this LED | |
35 | * @map: regmap to access the syscon device backing this LED | |
36 | * @offset: the offset into the syscon regmap for the LED register | |
37 | * @mask: the bit in the register corresponding to the LED | |
38 | * @state: current state of the LED | |
39 | */ | |
40 | struct syscon_led { | |
41 | struct led_classdev cdev; | |
42 | struct regmap *map; | |
43 | u32 offset; | |
44 | u32 mask; | |
45 | bool state; | |
46 | }; | |
47 | ||
48 | static void syscon_led_set(struct led_classdev *led_cdev, | |
49 | enum led_brightness value) | |
50 | { | |
51 | struct syscon_led *sled = | |
52 | container_of(led_cdev, struct syscon_led, cdev); | |
53 | u32 val; | |
54 | int ret; | |
55 | ||
56 | if (value == LED_OFF) { | |
57 | val = 0; | |
58 | sled->state = false; | |
59 | } else { | |
60 | val = sled->mask; | |
61 | sled->state = true; | |
62 | } | |
63 | ||
64 | ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val); | |
65 | if (ret < 0) | |
66 | dev_err(sled->cdev.dev, "error updating LED status\n"); | |
67 | } | |
68 | ||
69 | static const struct of_device_id syscon_match[] = { | |
70 | { .compatible = "syscon", }, | |
71 | {}, | |
72 | }; | |
73 | ||
74 | static int __init syscon_leds_init(void) | |
75 | { | |
76 | const struct of_device_id *devid; | |
77 | struct device_node *np; | |
78 | struct device_node *child; | |
79 | struct regmap *map; | |
80 | struct platform_device *pdev; | |
81 | struct device *dev; | |
82 | int ret; | |
83 | ||
84 | np = of_find_matching_node_and_match(NULL, syscon_match, | |
85 | &devid); | |
86 | if (!np) | |
87 | return -ENODEV; | |
88 | ||
89 | map = syscon_node_to_regmap(np); | |
90 | if (IS_ERR(map)) | |
91 | return PTR_ERR(map); | |
92 | ||
93 | /* | |
94 | * If the map is there, the device should be there, we allocate | |
95 | * memory on the syscon device's behalf here. | |
96 | */ | |
97 | pdev = of_find_device_by_node(np); | |
98 | if (!pdev) | |
99 | return -ENODEV; | |
100 | dev = &pdev->dev; | |
101 | ||
102 | for_each_available_child_of_node(np, child) { | |
103 | struct syscon_led *sled; | |
104 | const char *state; | |
105 | ||
106 | /* Only check for register-bit-leds */ | |
107 | if (of_property_match_string(child, "compatible", | |
108 | "register-bit-led") < 0) | |
109 | continue; | |
110 | ||
111 | sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL); | |
112 | if (!sled) | |
113 | return -ENOMEM; | |
114 | ||
115 | sled->map = map; | |
116 | ||
117 | if (of_property_read_u32(child, "offset", &sled->offset)) | |
118 | return -EINVAL; | |
119 | if (of_property_read_u32(child, "mask", &sled->mask)) | |
120 | return -EINVAL; | |
121 | sled->cdev.name = | |
122 | of_get_property(child, "label", NULL) ? : child->name; | |
123 | sled->cdev.default_trigger = | |
124 | of_get_property(child, "linux,default-trigger", NULL); | |
125 | ||
126 | state = of_get_property(child, "default-state", NULL); | |
127 | if (state) { | |
128 | if (!strcmp(state, "keep")) { | |
129 | u32 val; | |
130 | ||
131 | ret = regmap_read(map, sled->offset, &val); | |
132 | if (ret < 0) | |
133 | return ret; | |
134 | sled->state = !!(val & sled->mask); | |
135 | } else if (!strcmp(state, "on")) { | |
136 | sled->state = true; | |
137 | ret = regmap_update_bits(map, sled->offset, | |
138 | sled->mask, | |
139 | sled->mask); | |
140 | if (ret < 0) | |
141 | return ret; | |
142 | } else { | |
143 | sled->state = false; | |
144 | ret = regmap_update_bits(map, sled->offset, | |
145 | sled->mask, 0); | |
146 | if (ret < 0) | |
147 | return ret; | |
148 | } | |
149 | ||
150 | } | |
151 | sled->cdev.brightness_set = syscon_led_set; | |
152 | ||
153 | ret = led_classdev_register(dev, &sled->cdev); | |
154 | if (ret < 0) | |
155 | return ret; | |
156 | ||
157 | dev_info(dev, "registered LED %s\n", sled->cdev.name); | |
158 | } | |
159 | ||
160 | return 0; | |
161 | } | |
162 | device_initcall(syscon_leds_init); |