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