]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/leds/leds-netxbig.c
leds: netxbig: remove legacy board-file support
[mirror_ubuntu-jammy-kernel.git] / drivers / leds / leds-netxbig.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
24467832
SG
2/*
3 * leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
4 *
5 * Copyright (C) 2010 LaCie
6 *
7 * Author: Simon Guinot <sguinot@lacie.com>
24467832
SG
8 */
9
10#include <linux/module.h>
24467832
SG
11#include <linux/irq.h>
12#include <linux/slab.h>
13#include <linux/spinlock.h>
14#include <linux/platform_device.h>
15#include <linux/gpio.h>
2976b179 16#include <linux/of_gpio.h>
24467832 17#include <linux/leds.h>
156189a6
MY
18
19struct netxbig_gpio_ext {
20 unsigned int *addr;
21 int num_addr;
22 unsigned int *data;
23 int num_data;
24 unsigned int enable;
25};
26
27enum netxbig_led_mode {
28 NETXBIG_LED_OFF,
29 NETXBIG_LED_ON,
30 NETXBIG_LED_SATA,
31 NETXBIG_LED_TIMER1,
32 NETXBIG_LED_TIMER2,
33 NETXBIG_LED_MODE_NUM,
34};
35
36#define NETXBIG_LED_INVALID_MODE NETXBIG_LED_MODE_NUM
37
38struct netxbig_led_timer {
39 unsigned long delay_on;
40 unsigned long delay_off;
41 enum netxbig_led_mode mode;
42};
43
44struct netxbig_led {
45 const char *name;
46 const char *default_trigger;
47 int mode_addr;
48 int *mode_val;
49 int bright_addr;
50 int bright_max;
51};
52
53struct netxbig_led_platform_data {
54 struct netxbig_gpio_ext *gpio_ext;
55 struct netxbig_led_timer *timer;
56 int num_timer;
57 struct netxbig_led *leds;
58 int num_leds;
59};
24467832
SG
60
61/*
62 * GPIO extension bus.
63 */
64
65static DEFINE_SPINLOCK(gpio_ext_lock);
66
67static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
68{
69 int pin;
70
71 for (pin = 0; pin < gpio_ext->num_addr; pin++)
72 gpio_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
73}
74
75static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
76{
77 int pin;
78
79 for (pin = 0; pin < gpio_ext->num_data; pin++)
80 gpio_set_value(gpio_ext->data[pin], (data >> pin) & 1);
81}
82
83static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
84{
85 /* Enable select is done on the raising edge. */
86 gpio_set_value(gpio_ext->enable, 0);
87 gpio_set_value(gpio_ext->enable, 1);
88}
89
90static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
91 int addr, int value)
92{
93 unsigned long flags;
94
95 spin_lock_irqsave(&gpio_ext_lock, flags);
96 gpio_ext_set_addr(gpio_ext, addr);
97 gpio_ext_set_data(gpio_ext, value);
98 gpio_ext_enable_select(gpio_ext);
99 spin_unlock_irqrestore(&gpio_ext_lock, flags);
100}
101
cd010955
SG
102static int gpio_ext_init(struct platform_device *pdev,
103 struct netxbig_gpio_ext *gpio_ext)
24467832
SG
104{
105 int err;
106 int i;
107
108 if (unlikely(!gpio_ext))
109 return -EINVAL;
110
111 /* Configure address GPIOs. */
112 for (i = 0; i < gpio_ext->num_addr; i++) {
cd010955
SG
113 err = devm_gpio_request_one(&pdev->dev, gpio_ext->addr[i],
114 GPIOF_OUT_INIT_LOW,
115 "GPIO extension addr");
24467832 116 if (err)
cd010955 117 return err;
24467832
SG
118 }
119 /* Configure data GPIOs. */
120 for (i = 0; i < gpio_ext->num_data; i++) {
cd010955
SG
121 err = devm_gpio_request_one(&pdev->dev, gpio_ext->data[i],
122 GPIOF_OUT_INIT_LOW,
123 "GPIO extension data");
24467832 124 if (err)
cd010955 125 return err;
24467832
SG
126 }
127 /* Configure "enable select" GPIO. */
cd010955
SG
128 err = devm_gpio_request_one(&pdev->dev, gpio_ext->enable,
129 GPIOF_OUT_INIT_LOW,
130 "GPIO extension enable");
24467832 131 if (err)
cd010955 132 return err;
24467832
SG
133
134 return 0;
24467832
SG
135}
136
137/*
138 * Class LED driver.
139 */
140
141struct netxbig_led_data {
142 struct netxbig_gpio_ext *gpio_ext;
143 struct led_classdev cdev;
144 int mode_addr;
145 int *mode_val;
146 int bright_addr;
24467832
SG
147 struct netxbig_led_timer *timer;
148 int num_timer;
149 enum netxbig_led_mode mode;
150 int sata;
151 spinlock_t lock;
152};
153
154static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
155 unsigned long delay_on,
156 unsigned long delay_off,
157 struct netxbig_led_timer *timer,
158 int num_timer)
159{
160 int i;
161
162 for (i = 0; i < num_timer; i++) {
163 if (timer[i].delay_on == delay_on &&
164 timer[i].delay_off == delay_off) {
165 *mode = timer[i].mode;
166 return 0;
167 }
168 }
169 return -EINVAL;
170}
171
172static int netxbig_led_blink_set(struct led_classdev *led_cdev,
173 unsigned long *delay_on,
174 unsigned long *delay_off)
175{
176 struct netxbig_led_data *led_dat =
177 container_of(led_cdev, struct netxbig_led_data, cdev);
178 enum netxbig_led_mode mode;
179 int mode_val;
180 int ret;
181
182 /* Look for a LED mode with the requested timer frequency. */
183 ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
184 led_dat->timer, led_dat->num_timer);
185 if (ret < 0)
186 return ret;
187
188 mode_val = led_dat->mode_val[mode];
189 if (mode_val == NETXBIG_LED_INVALID_MODE)
190 return -EINVAL;
191
192 spin_lock_irq(&led_dat->lock);
193
194 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
195 led_dat->mode = mode;
196
197 spin_unlock_irq(&led_dat->lock);
198
199 return 0;
200}
201
202static void netxbig_led_set(struct led_classdev *led_cdev,
203 enum led_brightness value)
204{
205 struct netxbig_led_data *led_dat =
206 container_of(led_cdev, struct netxbig_led_data, cdev);
207 enum netxbig_led_mode mode;
7b9d9d88 208 int mode_val;
24467832
SG
209 int set_brightness = 1;
210 unsigned long flags;
211
212 spin_lock_irqsave(&led_dat->lock, flags);
213
214 if (value == LED_OFF) {
215 mode = NETXBIG_LED_OFF;
216 set_brightness = 0;
217 } else {
218 if (led_dat->sata)
219 mode = NETXBIG_LED_SATA;
220 else if (led_dat->mode == NETXBIG_LED_OFF)
221 mode = NETXBIG_LED_ON;
222 else /* Keep 'timer' mode. */
223 mode = led_dat->mode;
224 }
225 mode_val = led_dat->mode_val[mode];
226
227 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
228 led_dat->mode = mode;
229 /*
230 * Note that the brightness register is shared between all the
231 * SATA LEDs. So, change the brightness setting for a single
232 * SATA LED will affect all the others.
233 */
7b9d9d88 234 if (set_brightness)
24467832 235 gpio_ext_set_value(led_dat->gpio_ext,
7b9d9d88 236 led_dat->bright_addr, value);
24467832
SG
237
238 spin_unlock_irqrestore(&led_dat->lock, flags);
239}
240
241static ssize_t netxbig_led_sata_store(struct device *dev,
242 struct device_attribute *attr,
243 const char *buff, size_t count)
244{
245 struct led_classdev *led_cdev = dev_get_drvdata(dev);
246 struct netxbig_led_data *led_dat =
247 container_of(led_cdev, struct netxbig_led_data, cdev);
248 unsigned long enable;
249 enum netxbig_led_mode mode;
250 int mode_val;
251 int ret;
252
7517611a 253 ret = kstrtoul(buff, 10, &enable);
24467832
SG
254 if (ret < 0)
255 return ret;
256
257 enable = !!enable;
258
259 spin_lock_irq(&led_dat->lock);
260
261 if (led_dat->sata == enable) {
262 ret = count;
263 goto exit_unlock;
264 }
265
266 if (led_dat->mode != NETXBIG_LED_ON &&
267 led_dat->mode != NETXBIG_LED_SATA)
268 mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
269 else if (enable)
270 mode = NETXBIG_LED_SATA;
271 else
272 mode = NETXBIG_LED_ON;
273
274 mode_val = led_dat->mode_val[mode];
275 if (mode_val == NETXBIG_LED_INVALID_MODE) {
276 ret = -EINVAL;
277 goto exit_unlock;
278 }
279
280 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
281 led_dat->mode = mode;
282 led_dat->sata = enable;
283
284 ret = count;
285
286exit_unlock:
287 spin_unlock_irq(&led_dat->lock);
288
289 return ret;
290}
291
292static ssize_t netxbig_led_sata_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294{
295 struct led_classdev *led_cdev = dev_get_drvdata(dev);
296 struct netxbig_led_data *led_dat =
297 container_of(led_cdev, struct netxbig_led_data, cdev);
298
299 return sprintf(buf, "%d\n", led_dat->sata);
300}
301
302static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
303
588a6a99
JH
304static struct attribute *netxbig_led_attrs[] = {
305 &dev_attr_sata.attr,
306 NULL
307};
308ATTRIBUTE_GROUPS(netxbig_led);
309
cd010955
SG
310static int create_netxbig_led(struct platform_device *pdev,
311 struct netxbig_led_platform_data *pdata,
312 struct netxbig_led_data *led_dat,
313 const struct netxbig_led *template)
24467832 314{
24467832
SG
315 spin_lock_init(&led_dat->lock);
316 led_dat->gpio_ext = pdata->gpio_ext;
317 led_dat->cdev.name = template->name;
318 led_dat->cdev.default_trigger = template->default_trigger;
319 led_dat->cdev.blink_set = netxbig_led_blink_set;
320 led_dat->cdev.brightness_set = netxbig_led_set;
321 /*
322 * Because the GPIO extension bus don't allow to read registers
323 * value, there is no way to probe the LED initial state.
324 * So, the initial sysfs LED value for the "brightness" and "sata"
325 * attributes are inconsistent.
326 *
327 * Note that the initial LED state can't be reconfigured.
328 * The reason is that the LED behaviour must stay uniform during
329 * the whole boot process (bootloader+linux).
330 */
331 led_dat->sata = 0;
332 led_dat->cdev.brightness = LED_OFF;
7b9d9d88 333 led_dat->cdev.max_brightness = template->bright_max;
24467832
SG
334 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
335 led_dat->mode_addr = template->mode_addr;
336 led_dat->mode_val = template->mode_val;
337 led_dat->bright_addr = template->bright_addr;
24467832
SG
338 led_dat->timer = pdata->timer;
339 led_dat->num_timer = pdata->num_timer;
0c86ac2c
SG
340 /*
341 * If available, expose the SATA activity blink capability through
342 * a "sata" sysfs attribute.
343 */
344 if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
345 led_dat->cdev.groups = netxbig_led_groups;
24467832 346
cd010955 347 return devm_led_classdev_register(&pdev->dev, &led_dat->cdev);
24467832
SG
348}
349
2976b179
SG
350static int gpio_ext_get_of_pdata(struct device *dev, struct device_node *np,
351 struct netxbig_gpio_ext *gpio_ext)
352{
353 int *addr, *data;
354 int num_addr, num_data;
355 int ret;
356 int i;
357
358 ret = of_gpio_named_count(np, "addr-gpios");
359 if (ret < 0) {
360 dev_err(dev,
361 "Failed to count GPIOs in DT property addr-gpios\n");
362 return ret;
363 }
364 num_addr = ret;
a86854d0 365 addr = devm_kcalloc(dev, num_addr, sizeof(*addr), GFP_KERNEL);
2976b179
SG
366 if (!addr)
367 return -ENOMEM;
368
369 for (i = 0; i < num_addr; i++) {
370 ret = of_get_named_gpio(np, "addr-gpios", i);
371 if (ret < 0)
372 return ret;
373 addr[i] = ret;
374 }
375 gpio_ext->addr = addr;
376 gpio_ext->num_addr = num_addr;
377
378 ret = of_gpio_named_count(np, "data-gpios");
379 if (ret < 0) {
380 dev_err(dev,
381 "Failed to count GPIOs in DT property data-gpios\n");
382 return ret;
383 }
384 num_data = ret;
a86854d0 385 data = devm_kcalloc(dev, num_data, sizeof(*data), GFP_KERNEL);
2976b179
SG
386 if (!data)
387 return -ENOMEM;
388
389 for (i = 0; i < num_data; i++) {
390 ret = of_get_named_gpio(np, "data-gpios", i);
391 if (ret < 0)
392 return ret;
393 data[i] = ret;
394 }
395 gpio_ext->data = data;
396 gpio_ext->num_data = num_data;
397
398 ret = of_get_named_gpio(np, "enable-gpio", 0);
399 if (ret < 0) {
400 dev_err(dev,
401 "Failed to get GPIO from DT property enable-gpio\n");
402 return ret;
403 }
404 gpio_ext->enable = ret;
405
406 return 0;
407}
408
409static int netxbig_leds_get_of_pdata(struct device *dev,
410 struct netxbig_led_platform_data *pdata)
411{
412 struct device_node *np = dev->of_node;
413 struct device_node *gpio_ext_np;
414 struct device_node *child;
415 struct netxbig_gpio_ext *gpio_ext;
416 struct netxbig_led_timer *timers;
417 struct netxbig_led *leds, *led;
418 int num_timers;
419 int num_leds = 0;
420 int ret;
421 int i;
422
423 /* GPIO extension */
424 gpio_ext_np = of_parse_phandle(np, "gpio-ext", 0);
425 if (!gpio_ext_np) {
426 dev_err(dev, "Failed to get DT handle gpio-ext\n");
427 return -EINVAL;
428 }
429
430 gpio_ext = devm_kzalloc(dev, sizeof(*gpio_ext), GFP_KERNEL);
431 if (!gpio_ext)
432 return -ENOMEM;
433 ret = gpio_ext_get_of_pdata(dev, gpio_ext_np, gpio_ext);
434 if (ret)
435 return ret;
436 of_node_put(gpio_ext_np);
437 pdata->gpio_ext = gpio_ext;
438
439 /* Timers (optional) */
440 ret = of_property_count_u32_elems(np, "timers");
441 if (ret > 0) {
442 if (ret % 3)
443 return -EINVAL;
444 num_timers = ret / 3;
a86854d0 445 timers = devm_kcalloc(dev, num_timers, sizeof(*timers),
2976b179
SG
446 GFP_KERNEL);
447 if (!timers)
448 return -ENOMEM;
449 for (i = 0; i < num_timers; i++) {
450 u32 tmp;
451
452 of_property_read_u32_index(np, "timers", 3 * i,
453 &timers[i].mode);
454 if (timers[i].mode >= NETXBIG_LED_MODE_NUM)
455 return -EINVAL;
456 of_property_read_u32_index(np, "timers",
457 3 * i + 1, &tmp);
458 timers[i].delay_on = tmp;
459 of_property_read_u32_index(np, "timers",
460 3 * i + 2, &tmp);
461 timers[i].delay_off = tmp;
462 }
463 pdata->timer = timers;
464 pdata->num_timer = num_timers;
465 }
466
467 /* LEDs */
468 num_leds = of_get_child_count(np);
469 if (!num_leds) {
470 dev_err(dev, "No LED subnodes found in DT\n");
471 return -ENODEV;
472 }
473
a86854d0 474 leds = devm_kcalloc(dev, num_leds, sizeof(*leds), GFP_KERNEL);
2976b179
SG
475 if (!leds)
476 return -ENOMEM;
477
478 led = leds;
479 for_each_child_of_node(np, child) {
480 const char *string;
481 int *mode_val;
482 int num_modes;
483
484 ret = of_property_read_u32(child, "mode-addr",
485 &led->mode_addr);
486 if (ret)
487 goto err_node_put;
488
489 ret = of_property_read_u32(child, "bright-addr",
490 &led->bright_addr);
491 if (ret)
492 goto err_node_put;
493
494 ret = of_property_read_u32(child, "max-brightness",
495 &led->bright_max);
496 if (ret)
497 goto err_node_put;
498
499 mode_val =
a86854d0
KC
500 devm_kcalloc(dev,
501 NETXBIG_LED_MODE_NUM, sizeof(*mode_val),
2976b179
SG
502 GFP_KERNEL);
503 if (!mode_val) {
504 ret = -ENOMEM;
505 goto err_node_put;
506 }
507
508 for (i = 0; i < NETXBIG_LED_MODE_NUM; i++)
509 mode_val[i] = NETXBIG_LED_INVALID_MODE;
510
511 ret = of_property_count_u32_elems(child, "mode-val");
512 if (ret < 0 || ret % 2) {
513 ret = -EINVAL;
514 goto err_node_put;
515 }
516 num_modes = ret / 2;
517 if (num_modes > NETXBIG_LED_MODE_NUM) {
518 ret = -EINVAL;
519 goto err_node_put;
520 }
521
522 for (i = 0; i < num_modes; i++) {
523 int mode;
524 int val;
525
526 of_property_read_u32_index(child,
527 "mode-val", 2 * i, &mode);
528 of_property_read_u32_index(child,
529 "mode-val", 2 * i + 1, &val);
530 if (mode >= NETXBIG_LED_MODE_NUM) {
531 ret = -EINVAL;
532 goto err_node_put;
533 }
534 mode_val[mode] = val;
535 }
536 led->mode_val = mode_val;
537
538 if (!of_property_read_string(child, "label", &string))
539 led->name = string;
540 else
541 led->name = child->name;
542
543 if (!of_property_read_string(child,
544 "linux,default-trigger", &string))
545 led->default_trigger = string;
546
547 led++;
548 }
549
550 pdata->leds = leds;
551 pdata->num_leds = num_leds;
552
553 return 0;
554
555err_node_put:
556 of_node_put(child);
557 return ret;
558}
559
560static const struct of_device_id of_netxbig_leds_match[] = {
561 { .compatible = "lacie,netxbig-leds", },
562 {},
563};
825fe38a 564MODULE_DEVICE_TABLE(of, of_netxbig_leds_match);
2976b179 565
98ea1ea2 566static int netxbig_led_probe(struct platform_device *pdev)
24467832 567{
156189a6 568 struct netxbig_led_platform_data *pdata;
cd010955 569 struct netxbig_led_data *leds_data;
24467832
SG
570 int i;
571 int ret;
572
156189a6
MY
573 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
574 if (!pdata)
575 return -ENOMEM;
576 ret = netxbig_leds_get_of_pdata(&pdev->dev, pdata);
577 if (ret)
578 return ret;
24467832 579
a86854d0
KC
580 leds_data = devm_kcalloc(&pdev->dev,
581 pdata->num_leds, sizeof(*leds_data),
cd010955
SG
582 GFP_KERNEL);
583 if (!leds_data)
24467832
SG
584 return -ENOMEM;
585
cd010955 586 ret = gpio_ext_init(pdev, pdata->gpio_ext);
24467832 587 if (ret < 0)
8095c385 588 return ret;
24467832
SG
589
590 for (i = 0; i < pdata->num_leds; i++) {
cd010955
SG
591 ret = create_netxbig_led(pdev, pdata,
592 &leds_data[i], &pdata->leds[i]);
24467832 593 if (ret < 0)
cd010955 594 return ret;
24467832 595 }
24467832
SG
596
597 return 0;
598}
599
600static struct platform_driver netxbig_led_driver = {
601 .probe = netxbig_led_probe,
24467832 602 .driver = {
2976b179 603 .name = "leds-netxbig",
156189a6 604 .of_match_table = of_netxbig_leds_match,
24467832
SG
605 },
606};
24467832 607
892a8843 608module_platform_driver(netxbig_led_driver);
24467832
SG
609
610MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
611MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
612MODULE_LICENSE("GPL");
892a8843 613MODULE_ALIAS("platform:leds-netxbig");