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