Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
11efe71f SG |
2 | /* |
3 | * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED | |
4 | * | |
5 | * Copyright (C) 2010 LaCie | |
6 | * | |
7 | * Author: Simon Guinot <sguinot@lacie.com> | |
8 | * | |
9 | * Based on leds-gpio.c by Raphael Assenat <raph@8d.com> | |
11efe71f SG |
10 | */ |
11 | ||
12 | #include <linux/kernel.h> | |
11efe71f SG |
13 | #include <linux/platform_device.h> |
14 | #include <linux/slab.h> | |
c7896490 | 15 | #include <linux/gpio/consumer.h> |
11efe71f | 16 | #include <linux/leds.h> |
54f4dedb | 17 | #include <linux/module.h> |
c68f46dd | 18 | #include <linux/of.h> |
4b90432d | 19 | #include "leds.h" |
11efe71f | 20 | |
c7896490 LW |
21 | enum ns2_led_modes { |
22 | NS_V2_LED_OFF, | |
23 | NS_V2_LED_ON, | |
24 | NS_V2_LED_SATA, | |
25 | }; | |
26 | ||
27 | struct ns2_led_modval { | |
28 | enum ns2_led_modes mode; | |
29 | int cmd_level; | |
30 | int slow_level; | |
31 | }; | |
32 | ||
01026cec | 33 | struct ns2_led_of_one { |
c7896490 LW |
34 | const char *name; |
35 | const char *default_trigger; | |
ccbbb117 LW |
36 | struct gpio_desc *cmd; |
37 | struct gpio_desc *slow; | |
c7896490 LW |
38 | int num_modes; |
39 | struct ns2_led_modval *modval; | |
40 | }; | |
41 | ||
01d0b14d | 42 | struct ns2_led_of { |
01026cec MB |
43 | int num_leds; |
44 | struct ns2_led_of_one *leds; | |
c7896490 LW |
45 | }; |
46 | ||
11efe71f | 47 | /* |
f7fafd08 VD |
48 | * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED |
49 | * modes are available: off, on and SATA activity blinking. The LED modes are | |
50 | * controlled through two GPIOs (command and slow): each combination of values | |
51 | * for the command/slow GPIOs corresponds to a LED mode. | |
11efe71f SG |
52 | */ |
53 | ||
01026cec | 54 | struct ns2_led { |
11efe71f | 55 | struct led_classdev cdev; |
ccbbb117 LW |
56 | struct gpio_desc *cmd; |
57 | struct gpio_desc *slow; | |
4b90432d | 58 | bool can_sleep; |
11efe71f SG |
59 | unsigned char sata; /* True when SATA mode active. */ |
60 | rwlock_t rw_lock; /* Lock GPIOs. */ | |
f7fafd08 VD |
61 | int num_modes; |
62 | struct ns2_led_modval *modval; | |
11efe71f SG |
63 | }; |
64 | ||
a78bd8f3 | 65 | static int ns2_led_get_mode(struct ns2_led *led, enum ns2_led_modes *mode) |
11efe71f SG |
66 | { |
67 | int i; | |
11efe71f SG |
68 | int cmd_level; |
69 | int slow_level; | |
70 | ||
a78bd8f3 MB |
71 | cmd_level = gpiod_get_value_cansleep(led->cmd); |
72 | slow_level = gpiod_get_value_cansleep(led->slow); | |
11efe71f | 73 | |
a78bd8f3 MB |
74 | for (i = 0; i < led->num_modes; i++) { |
75 | if (cmd_level == led->modval[i].cmd_level && | |
76 | slow_level == led->modval[i].slow_level) { | |
77 | *mode = led->modval[i].mode; | |
a2fc703c | 78 | return 0; |
11efe71f SG |
79 | } |
80 | } | |
81 | ||
a2fc703c | 82 | return -EINVAL; |
11efe71f SG |
83 | } |
84 | ||
a78bd8f3 | 85 | static void ns2_led_set_mode(struct ns2_led *led, enum ns2_led_modes mode) |
11efe71f SG |
86 | { |
87 | int i; | |
4b90432d | 88 | bool found = false; |
f539dfed | 89 | unsigned long flags; |
11efe71f | 90 | |
a78bd8f3 MB |
91 | for (i = 0; i < led->num_modes; i++) |
92 | if (mode == led->modval[i].mode) { | |
4b90432d SG |
93 | found = true; |
94 | break; | |
11efe71f | 95 | } |
4b90432d SG |
96 | |
97 | if (!found) | |
98 | return; | |
99 | ||
a78bd8f3 | 100 | write_lock_irqsave(&led->rw_lock, flags); |
4b90432d | 101 | |
a78bd8f3 MB |
102 | if (!led->can_sleep) { |
103 | gpiod_set_value(led->cmd, led->modval[i].cmd_level); | |
104 | gpiod_set_value(led->slow, led->modval[i].slow_level); | |
4b90432d | 105 | goto exit_unlock; |
11efe71f SG |
106 | } |
107 | ||
a78bd8f3 MB |
108 | gpiod_set_value_cansleep(led->cmd, led->modval[i].cmd_level); |
109 | gpiod_set_value_cansleep(led->slow, led->modval[i].slow_level); | |
4b90432d SG |
110 | |
111 | exit_unlock: | |
a78bd8f3 | 112 | write_unlock_irqrestore(&led->rw_lock, flags); |
11efe71f SG |
113 | } |
114 | ||
115 | static void ns2_led_set(struct led_classdev *led_cdev, | |
116 | enum led_brightness value) | |
117 | { | |
a78bd8f3 | 118 | struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev); |
11efe71f SG |
119 | enum ns2_led_modes mode; |
120 | ||
121 | if (value == LED_OFF) | |
122 | mode = NS_V2_LED_OFF; | |
a78bd8f3 | 123 | else if (led->sata) |
11efe71f SG |
124 | mode = NS_V2_LED_SATA; |
125 | else | |
126 | mode = NS_V2_LED_ON; | |
127 | ||
a78bd8f3 | 128 | ns2_led_set_mode(led, mode); |
11efe71f SG |
129 | } |
130 | ||
c29e650b JA |
131 | static int ns2_led_set_blocking(struct led_classdev *led_cdev, |
132 | enum led_brightness value) | |
133 | { | |
134 | ns2_led_set(led_cdev, value); | |
135 | return 0; | |
136 | } | |
137 | ||
11efe71f SG |
138 | static ssize_t ns2_led_sata_store(struct device *dev, |
139 | struct device_attribute *attr, | |
140 | const char *buff, size_t count) | |
141 | { | |
e5971bbc | 142 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
a78bd8f3 | 143 | struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev); |
11efe71f SG |
144 | int ret; |
145 | unsigned long enable; | |
11efe71f | 146 | |
3874350c | 147 | ret = kstrtoul(buff, 10, &enable); |
11efe71f SG |
148 | if (ret < 0) |
149 | return ret; | |
150 | ||
151 | enable = !!enable; | |
152 | ||
a78bd8f3 | 153 | if (led->sata == enable) |
4b90432d | 154 | goto exit; |
11efe71f | 155 | |
a78bd8f3 | 156 | led->sata = enable; |
4b90432d SG |
157 | |
158 | if (!led_get_brightness(led_cdev)) | |
159 | goto exit; | |
11efe71f | 160 | |
4b90432d | 161 | if (enable) |
a78bd8f3 | 162 | ns2_led_set_mode(led, NS_V2_LED_SATA); |
4b90432d | 163 | else |
a78bd8f3 | 164 | ns2_led_set_mode(led, NS_V2_LED_ON); |
11efe71f | 165 | |
4b90432d | 166 | exit: |
11efe71f SG |
167 | return count; |
168 | } | |
169 | ||
170 | static ssize_t ns2_led_sata_show(struct device *dev, | |
171 | struct device_attribute *attr, char *buf) | |
172 | { | |
e5971bbc | 173 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
a78bd8f3 | 174 | struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev); |
11efe71f | 175 | |
a78bd8f3 | 176 | return sprintf(buf, "%d\n", led->sata); |
11efe71f SG |
177 | } |
178 | ||
179 | static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store); | |
180 | ||
475f8548 JH |
181 | static struct attribute *ns2_led_attrs[] = { |
182 | &dev_attr_sata.attr, | |
183 | NULL | |
184 | }; | |
185 | ATTRIBUTE_GROUPS(ns2_led); | |
186 | ||
98ea1ea2 | 187 | static int |
b3f96922 | 188 | create_ns2_led(struct device *dev, struct ns2_led *led, |
01026cec | 189 | const struct ns2_led_of_one *template) |
11efe71f SG |
190 | { |
191 | int ret; | |
192 | enum ns2_led_modes mode; | |
193 | ||
a78bd8f3 MB |
194 | rwlock_init(&led->rw_lock); |
195 | ||
196 | led->cdev.name = template->name; | |
197 | led->cdev.default_trigger = template->default_trigger; | |
198 | led->cdev.blink_set = NULL; | |
199 | led->cdev.flags |= LED_CORE_SUSPENDRESUME; | |
200 | led->cdev.groups = ns2_led_groups; | |
201 | led->cmd = template->cmd; | |
202 | led->slow = template->slow; | |
203 | led->can_sleep = gpiod_cansleep(led->cmd) | gpiod_cansleep(led->slow); | |
204 | if (led->can_sleep) | |
205 | led->cdev.brightness_set_blocking = ns2_led_set_blocking; | |
c29e650b | 206 | else |
a78bd8f3 MB |
207 | led->cdev.brightness_set = ns2_led_set; |
208 | led->modval = template->modval; | |
209 | led->num_modes = template->num_modes; | |
11efe71f | 210 | |
a78bd8f3 | 211 | ret = ns2_led_get_mode(led, &mode); |
11efe71f | 212 | if (ret < 0) |
04195823 | 213 | return ret; |
11efe71f SG |
214 | |
215 | /* Set LED initial state. */ | |
a78bd8f3 MB |
216 | led->sata = (mode == NS_V2_LED_SATA) ? 1 : 0; |
217 | led->cdev.brightness = (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL; | |
11efe71f | 218 | |
b3f96922 | 219 | return devm_led_classdev_register(dev, &led->cdev); |
11efe71f SG |
220 | } |
221 | ||
f72deb71 | 222 | static int ns2_leds_parse_one(struct device *dev, struct device_node *np, |
01026cec | 223 | struct ns2_led_of_one *led) |
f72deb71 MB |
224 | { |
225 | struct ns2_led_modval *modval; | |
226 | int nmodes, ret, i; | |
227 | ||
228 | ret = of_property_read_string(np, "label", &led->name); | |
229 | if (ret) | |
230 | led->name = np->name; | |
231 | ||
528c9515 MB |
232 | led->cmd = devm_gpiod_get_from_of_node(dev, np, "cmd-gpio", 0, |
233 | GPIOD_ASIS, led->name); | |
f72deb71 MB |
234 | if (IS_ERR(led->cmd)) |
235 | return PTR_ERR(led->cmd); | |
236 | ||
528c9515 MB |
237 | led->slow = devm_gpiod_get_from_of_node(dev, np, "slow-gpio", 0, |
238 | GPIOD_ASIS, led->name); | |
f72deb71 MB |
239 | if (IS_ERR(led->slow)) |
240 | return PTR_ERR(led->slow); | |
241 | ||
242 | of_property_read_string(np, "linux,default-trigger", | |
243 | &led->default_trigger); | |
244 | ||
245 | ret = of_property_count_u32_elems(np, "modes-map"); | |
246 | if (ret < 0 || ret % 3) { | |
247 | dev_err(dev, "Missing or malformed modes-map for %pOF\n", np); | |
248 | return -EINVAL; | |
249 | } | |
250 | ||
251 | nmodes = ret / 3; | |
252 | modval = devm_kcalloc(dev, nmodes, sizeof(*modval), GFP_KERNEL); | |
253 | if (!modval) | |
254 | return -ENOMEM; | |
255 | ||
256 | for (i = 0; i < nmodes; i++) { | |
257 | u32 val; | |
258 | ||
259 | of_property_read_u32_index(np, "modes-map", 3 * i, &val); | |
260 | modval[i].mode = val; | |
261 | of_property_read_u32_index(np, "modes-map", 3 * i + 1, &val); | |
262 | modval[i].cmd_level = val; | |
263 | of_property_read_u32_index(np, "modes-map", 3 * i + 2, &val); | |
264 | modval[i].slow_level = val; | |
265 | } | |
266 | ||
267 | led->num_modes = nmodes; | |
268 | led->modval = modval; | |
269 | ||
270 | return 0; | |
271 | } | |
272 | ||
72052fcc SG |
273 | /* |
274 | * Translate OpenFirmware node properties into platform_data. | |
275 | */ | |
cf4af012 | 276 | static int |
01d0b14d | 277 | ns2_leds_parse_of(struct device *dev, struct ns2_led_of *ofdata) |
72052fcc | 278 | { |
8853c95e | 279 | struct device_node *np = dev_of_node(dev); |
72052fcc | 280 | struct device_node *child; |
01026cec | 281 | struct ns2_led_of_one *led, *leds; |
79937a4b | 282 | int ret, num_leds = 0; |
72052fcc | 283 | |
99a013c8 | 284 | num_leds = of_get_available_child_count(np); |
72052fcc SG |
285 | if (!num_leds) |
286 | return -ENODEV; | |
287 | ||
a86854d0 | 288 | leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led), |
72052fcc SG |
289 | GFP_KERNEL); |
290 | if (!leds) | |
291 | return -ENOMEM; | |
292 | ||
f7fafd08 | 293 | led = leds; |
99a013c8 | 294 | for_each_available_child_of_node(np, child) { |
f72deb71 MB |
295 | ret = ns2_leds_parse_one(dev, child, led++); |
296 | if (ret < 0) { | |
297 | of_node_put(child); | |
298 | return ret; | |
f7fafd08 | 299 | } |
72052fcc SG |
300 | } |
301 | ||
01d0b14d MB |
302 | ofdata->leds = leds; |
303 | ofdata->num_leds = num_leds; | |
72052fcc SG |
304 | |
305 | return 0; | |
306 | } | |
307 | ||
308 | static const struct of_device_id of_ns2_leds_match[] = { | |
309 | { .compatible = "lacie,ns2-leds", }, | |
310 | {}, | |
311 | }; | |
98f9cc7f | 312 | MODULE_DEVICE_TABLE(of, of_ns2_leds_match); |
72052fcc | 313 | |
98ea1ea2 | 314 | static int ns2_led_probe(struct platform_device *pdev) |
11efe71f | 315 | { |
b3f96922 | 316 | struct device *dev = &pdev->dev; |
01d0b14d | 317 | struct ns2_led_of *ofdata; |
01026cec | 318 | struct ns2_led *leds; |
11efe71f SG |
319 | int i; |
320 | int ret; | |
321 | ||
b3f96922 | 322 | ofdata = devm_kzalloc(dev, sizeof(struct ns2_led_of), GFP_KERNEL); |
01d0b14d MB |
323 | if (!ofdata) |
324 | return -ENOMEM; | |
72052fcc | 325 | |
b3f96922 | 326 | ret = ns2_leds_parse_of(dev, ofdata); |
01d0b14d MB |
327 | if (ret) |
328 | return ret; | |
11efe71f | 329 | |
b3f96922 | 330 | leds = devm_kzalloc(dev, array_size(sizeof(*leds), ofdata->num_leds), |
19d4deb7 MB |
331 | GFP_KERNEL); |
332 | if (!leds) | |
11efe71f SG |
333 | return -ENOMEM; |
334 | ||
01d0b14d | 335 | for (i = 0; i < ofdata->num_leds; i++) { |
b3f96922 | 336 | ret = create_ns2_led(dev, &leds[i], &ofdata->leds[i]); |
40f97281 | 337 | if (ret < 0) |
a209f766 | 338 | return ret; |
11efe71f SG |
339 | } |
340 | ||
11efe71f | 341 | return 0; |
11efe71f SG |
342 | } |
343 | ||
11efe71f SG |
344 | static struct platform_driver ns2_led_driver = { |
345 | .probe = ns2_led_probe, | |
11efe71f | 346 | .driver = { |
72052fcc | 347 | .name = "leds-ns2", |
72052fcc | 348 | .of_match_table = of_match_ptr(of_ns2_leds_match), |
11efe71f SG |
349 | }, |
350 | }; | |
11efe71f | 351 | |
892a8843 | 352 | module_platform_driver(ns2_led_driver); |
11efe71f SG |
353 | |
354 | MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>"); | |
355 | MODULE_DESCRIPTION("Network Space v2 LED driver"); | |
356 | MODULE_LICENSE("GPL"); | |
892a8843 | 357 | MODULE_ALIAS("platform:leds-ns2"); |