11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 211efe71fSSimon Guinot /* 311efe71fSSimon Guinot * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED 411efe71fSSimon Guinot * 511efe71fSSimon Guinot * Copyright (C) 2010 LaCie 611efe71fSSimon Guinot * 711efe71fSSimon Guinot * Author: Simon Guinot <sguinot@lacie.com> 811efe71fSSimon Guinot * 911efe71fSSimon Guinot * Based on leds-gpio.c by Raphael Assenat <raph@8d.com> 1011efe71fSSimon Guinot */ 1111efe71fSSimon Guinot 1211efe71fSSimon Guinot #include <linux/kernel.h> 1311efe71fSSimon Guinot #include <linux/platform_device.h> 1411efe71fSSimon Guinot #include <linux/slab.h> 15c7896490SLinus Walleij #include <linux/gpio/consumer.h> 1611efe71fSSimon Guinot #include <linux/leds.h> 1754f4dedbSPaul Gortmaker #include <linux/module.h> 18c68f46ddSSachin Kamat #include <linux/of.h> 194b90432dSSimon Guinot #include "leds.h" 2011efe71fSSimon Guinot 21c7896490SLinus Walleij enum ns2_led_modes { 22c7896490SLinus Walleij NS_V2_LED_OFF, 23c7896490SLinus Walleij NS_V2_LED_ON, 24c7896490SLinus Walleij NS_V2_LED_SATA, 25c7896490SLinus Walleij }; 26c7896490SLinus Walleij 27c7896490SLinus Walleij struct ns2_led_modval { 28c7896490SLinus Walleij enum ns2_led_modes mode; 29c7896490SLinus Walleij int cmd_level; 30c7896490SLinus Walleij int slow_level; 31c7896490SLinus Walleij }; 32c7896490SLinus Walleij 33c7896490SLinus Walleij struct ns2_led { 34c7896490SLinus Walleij const char *name; 35c7896490SLinus Walleij const char *default_trigger; 36ccbbb117SLinus Walleij struct gpio_desc *cmd; 37ccbbb117SLinus Walleij struct gpio_desc *slow; 38c7896490SLinus Walleij int num_modes; 39c7896490SLinus Walleij struct ns2_led_modval *modval; 40c7896490SLinus Walleij }; 41c7896490SLinus Walleij 4201d0b14dSMarek Behún struct ns2_led_of { 43c7896490SLinus Walleij int num_leds; 44c7896490SLinus Walleij struct ns2_led *leds; 45c7896490SLinus Walleij }; 46c7896490SLinus Walleij 4711efe71fSSimon Guinot /* 48f7fafd08SVincent Donnefort * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED 49f7fafd08SVincent Donnefort * modes are available: off, on and SATA activity blinking. The LED modes are 50f7fafd08SVincent Donnefort * controlled through two GPIOs (command and slow): each combination of values 51f7fafd08SVincent Donnefort * for the command/slow GPIOs corresponds to a LED mode. 5211efe71fSSimon Guinot */ 5311efe71fSSimon Guinot 5411efe71fSSimon Guinot struct ns2_led_data { 5511efe71fSSimon Guinot struct led_classdev cdev; 56ccbbb117SLinus Walleij struct gpio_desc *cmd; 57ccbbb117SLinus Walleij struct gpio_desc *slow; 584b90432dSSimon Guinot bool can_sleep; 5911efe71fSSimon Guinot unsigned char sata; /* True when SATA mode active. */ 6011efe71fSSimon Guinot rwlock_t rw_lock; /* Lock GPIOs. */ 61f7fafd08SVincent Donnefort int num_modes; 62f7fafd08SVincent Donnefort struct ns2_led_modval *modval; 6311efe71fSSimon Guinot }; 6411efe71fSSimon Guinot 6511efe71fSSimon Guinot static int ns2_led_get_mode(struct ns2_led_data *led_dat, 6611efe71fSSimon Guinot enum ns2_led_modes *mode) 6711efe71fSSimon Guinot { 6811efe71fSSimon Guinot int i; 6911efe71fSSimon Guinot int ret = -EINVAL; 7011efe71fSSimon Guinot int cmd_level; 7111efe71fSSimon Guinot int slow_level; 7211efe71fSSimon Guinot 73ccbbb117SLinus Walleij cmd_level = gpiod_get_value_cansleep(led_dat->cmd); 74ccbbb117SLinus Walleij slow_level = gpiod_get_value_cansleep(led_dat->slow); 7511efe71fSSimon Guinot 76f7fafd08SVincent Donnefort for (i = 0; i < led_dat->num_modes; i++) { 77f7fafd08SVincent Donnefort if (cmd_level == led_dat->modval[i].cmd_level && 78f7fafd08SVincent Donnefort slow_level == led_dat->modval[i].slow_level) { 79f7fafd08SVincent Donnefort *mode = led_dat->modval[i].mode; 8011efe71fSSimon Guinot ret = 0; 8111efe71fSSimon Guinot break; 8211efe71fSSimon Guinot } 8311efe71fSSimon Guinot } 8411efe71fSSimon Guinot 8511efe71fSSimon Guinot return ret; 8611efe71fSSimon Guinot } 8711efe71fSSimon Guinot 8811efe71fSSimon Guinot static void ns2_led_set_mode(struct ns2_led_data *led_dat, 8911efe71fSSimon Guinot enum ns2_led_modes mode) 9011efe71fSSimon Guinot { 9111efe71fSSimon Guinot int i; 924b90432dSSimon Guinot bool found = false; 93f539dfedSSimon Guinot unsigned long flags; 9411efe71fSSimon Guinot 954b90432dSSimon Guinot for (i = 0; i < led_dat->num_modes; i++) 964b90432dSSimon Guinot if (mode == led_dat->modval[i].mode) { 974b90432dSSimon Guinot found = true; 984b90432dSSimon Guinot break; 994b90432dSSimon Guinot } 1004b90432dSSimon Guinot 1014b90432dSSimon Guinot if (!found) 1024b90432dSSimon Guinot return; 1034b90432dSSimon Guinot 104f539dfedSSimon Guinot write_lock_irqsave(&led_dat->rw_lock, flags); 10511efe71fSSimon Guinot 1064b90432dSSimon Guinot if (!led_dat->can_sleep) { 107ccbbb117SLinus Walleij gpiod_set_value(led_dat->cmd, 108f7fafd08SVincent Donnefort led_dat->modval[i].cmd_level); 109ccbbb117SLinus Walleij gpiod_set_value(led_dat->slow, 110f7fafd08SVincent Donnefort led_dat->modval[i].slow_level); 1114b90432dSSimon Guinot goto exit_unlock; 11211efe71fSSimon Guinot } 11311efe71fSSimon Guinot 114ccbbb117SLinus Walleij gpiod_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level); 115ccbbb117SLinus Walleij gpiod_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level); 1164b90432dSSimon Guinot 1174b90432dSSimon Guinot exit_unlock: 118f539dfedSSimon Guinot write_unlock_irqrestore(&led_dat->rw_lock, flags); 11911efe71fSSimon Guinot } 12011efe71fSSimon Guinot 12111efe71fSSimon Guinot static void ns2_led_set(struct led_classdev *led_cdev, 12211efe71fSSimon Guinot enum led_brightness value) 12311efe71fSSimon Guinot { 12411efe71fSSimon Guinot struct ns2_led_data *led_dat = 12511efe71fSSimon Guinot container_of(led_cdev, struct ns2_led_data, cdev); 12611efe71fSSimon Guinot enum ns2_led_modes mode; 12711efe71fSSimon Guinot 12811efe71fSSimon Guinot if (value == LED_OFF) 12911efe71fSSimon Guinot mode = NS_V2_LED_OFF; 13011efe71fSSimon Guinot else if (led_dat->sata) 13111efe71fSSimon Guinot mode = NS_V2_LED_SATA; 13211efe71fSSimon Guinot else 13311efe71fSSimon Guinot mode = NS_V2_LED_ON; 13411efe71fSSimon Guinot 13511efe71fSSimon Guinot ns2_led_set_mode(led_dat, mode); 13611efe71fSSimon Guinot } 13711efe71fSSimon Guinot 138c29e650bSJacek Anaszewski static int ns2_led_set_blocking(struct led_classdev *led_cdev, 139c29e650bSJacek Anaszewski enum led_brightness value) 140c29e650bSJacek Anaszewski { 141c29e650bSJacek Anaszewski ns2_led_set(led_cdev, value); 142c29e650bSJacek Anaszewski return 0; 143c29e650bSJacek Anaszewski } 144c29e650bSJacek Anaszewski 14511efe71fSSimon Guinot static ssize_t ns2_led_sata_store(struct device *dev, 14611efe71fSSimon Guinot struct device_attribute *attr, 14711efe71fSSimon Guinot const char *buff, size_t count) 14811efe71fSSimon Guinot { 149e5971bbcSSimon Guinot struct led_classdev *led_cdev = dev_get_drvdata(dev); 150e5971bbcSSimon Guinot struct ns2_led_data *led_dat = 151e5971bbcSSimon Guinot container_of(led_cdev, struct ns2_led_data, cdev); 15211efe71fSSimon Guinot int ret; 15311efe71fSSimon Guinot unsigned long enable; 15411efe71fSSimon Guinot 1553874350cSJingoo Han ret = kstrtoul(buff, 10, &enable); 15611efe71fSSimon Guinot if (ret < 0) 15711efe71fSSimon Guinot return ret; 15811efe71fSSimon Guinot 15911efe71fSSimon Guinot enable = !!enable; 16011efe71fSSimon Guinot 16111efe71fSSimon Guinot if (led_dat->sata == enable) 1624b90432dSSimon Guinot goto exit; 16311efe71fSSimon Guinot 16411efe71fSSimon Guinot led_dat->sata = enable; 16511efe71fSSimon Guinot 1664b90432dSSimon Guinot if (!led_get_brightness(led_cdev)) 1674b90432dSSimon Guinot goto exit; 1684b90432dSSimon Guinot 1694b90432dSSimon Guinot if (enable) 1704b90432dSSimon Guinot ns2_led_set_mode(led_dat, NS_V2_LED_SATA); 1714b90432dSSimon Guinot else 1724b90432dSSimon Guinot ns2_led_set_mode(led_dat, NS_V2_LED_ON); 1734b90432dSSimon Guinot 1744b90432dSSimon Guinot exit: 17511efe71fSSimon Guinot return count; 17611efe71fSSimon Guinot } 17711efe71fSSimon Guinot 17811efe71fSSimon Guinot static ssize_t ns2_led_sata_show(struct device *dev, 17911efe71fSSimon Guinot struct device_attribute *attr, char *buf) 18011efe71fSSimon Guinot { 181e5971bbcSSimon Guinot struct led_classdev *led_cdev = dev_get_drvdata(dev); 182e5971bbcSSimon Guinot struct ns2_led_data *led_dat = 183e5971bbcSSimon Guinot container_of(led_cdev, struct ns2_led_data, cdev); 18411efe71fSSimon Guinot 18511efe71fSSimon Guinot return sprintf(buf, "%d\n", led_dat->sata); 18611efe71fSSimon Guinot } 18711efe71fSSimon Guinot 18811efe71fSSimon Guinot static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store); 18911efe71fSSimon Guinot 190475f8548SJohan Hovold static struct attribute *ns2_led_attrs[] = { 191475f8548SJohan Hovold &dev_attr_sata.attr, 192475f8548SJohan Hovold NULL 193475f8548SJohan Hovold }; 194475f8548SJohan Hovold ATTRIBUTE_GROUPS(ns2_led); 195475f8548SJohan Hovold 19698ea1ea2SBill Pemberton static int 19711efe71fSSimon Guinot create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat, 19811efe71fSSimon Guinot const struct ns2_led *template) 19911efe71fSSimon Guinot { 20011efe71fSSimon Guinot int ret; 20111efe71fSSimon Guinot enum ns2_led_modes mode; 20211efe71fSSimon Guinot 20311efe71fSSimon Guinot rwlock_init(&led_dat->rw_lock); 20411efe71fSSimon Guinot 20511efe71fSSimon Guinot led_dat->cdev.name = template->name; 20611efe71fSSimon Guinot led_dat->cdev.default_trigger = template->default_trigger; 20711efe71fSSimon Guinot led_dat->cdev.blink_set = NULL; 20811efe71fSSimon Guinot led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; 209475f8548SJohan Hovold led_dat->cdev.groups = ns2_led_groups; 21011efe71fSSimon Guinot led_dat->cmd = template->cmd; 21111efe71fSSimon Guinot led_dat->slow = template->slow; 212ccbbb117SLinus Walleij led_dat->can_sleep = gpiod_cansleep(led_dat->cmd) | 213ccbbb117SLinus Walleij gpiod_cansleep(led_dat->slow); 214c29e650bSJacek Anaszewski if (led_dat->can_sleep) 215c29e650bSJacek Anaszewski led_dat->cdev.brightness_set_blocking = ns2_led_set_blocking; 216c29e650bSJacek Anaszewski else 217c29e650bSJacek Anaszewski led_dat->cdev.brightness_set = ns2_led_set; 218f7fafd08SVincent Donnefort led_dat->modval = template->modval; 219f7fafd08SVincent Donnefort led_dat->num_modes = template->num_modes; 22011efe71fSSimon Guinot 22111efe71fSSimon Guinot ret = ns2_led_get_mode(led_dat, &mode); 22211efe71fSSimon Guinot if (ret < 0) 22304195823SSachin Kamat return ret; 22411efe71fSSimon Guinot 22511efe71fSSimon Guinot /* Set LED initial state. */ 22611efe71fSSimon Guinot led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0; 22711efe71fSSimon Guinot led_dat->cdev.brightness = 22811efe71fSSimon Guinot (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL; 22911efe71fSSimon Guinot 23040f97281SMarek Behún return devm_led_classdev_register(&pdev->dev, &led_dat->cdev); 23111efe71fSSimon Guinot } 23211efe71fSSimon Guinot 23372052fccSSimon Guinot /* 23472052fccSSimon Guinot * Translate OpenFirmware node properties into platform_data. 23572052fccSSimon Guinot */ 236cf4af012SLinus Torvalds static int 23701d0b14dSMarek Behún ns2_leds_parse_of(struct device *dev, struct ns2_led_of *ofdata) 23872052fccSSimon Guinot { 2398853c95eSMarek Behún struct device_node *np = dev_of_node(dev); 24072052fccSSimon Guinot struct device_node *child; 241f7fafd08SVincent Donnefort struct ns2_led *led, *leds; 24279937a4bSNishka Dasgupta int ret, num_leds = 0; 24372052fccSSimon Guinot 24499a013c8SMarek Behún num_leds = of_get_available_child_count(np); 24572052fccSSimon Guinot if (!num_leds) 24672052fccSSimon Guinot return -ENODEV; 24772052fccSSimon Guinot 248a86854d0SKees Cook leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led), 24972052fccSSimon Guinot GFP_KERNEL); 25072052fccSSimon Guinot if (!leds) 25172052fccSSimon Guinot return -ENOMEM; 25272052fccSSimon Guinot 253f7fafd08SVincent Donnefort led = leds; 25499a013c8SMarek Behún for_each_available_child_of_node(np, child) { 25572052fccSSimon Guinot const char *string; 25679937a4bSNishka Dasgupta int i, num_modes; 257f7fafd08SVincent Donnefort struct ns2_led_modval *modval; 258ccbbb117SLinus Walleij struct gpio_desc *gd; 25972052fccSSimon Guinot 26072052fccSSimon Guinot ret = of_property_read_string(child, "label", &string); 261f7fafd08SVincent Donnefort led->name = (ret == 0) ? string : child->name; 262ccbbb117SLinus Walleij 263ccbbb117SLinus Walleij gd = gpiod_get_from_of_node(child, "cmd-gpio", 0, 264ccbbb117SLinus Walleij GPIOD_ASIS, led->name); 265ccbbb117SLinus Walleij if (IS_ERR(gd)) { 266ccbbb117SLinus Walleij ret = PTR_ERR(gd); 267ccbbb117SLinus Walleij goto err_node_put; 268ccbbb117SLinus Walleij } 269ccbbb117SLinus Walleij led->cmd = gd; 270ccbbb117SLinus Walleij gd = gpiod_get_from_of_node(child, "slow-gpio", 0, 271ccbbb117SLinus Walleij GPIOD_ASIS, led->name); 272ccbbb117SLinus Walleij if (IS_ERR(gd)) { 273ccbbb117SLinus Walleij ret = PTR_ERR(gd); 274ccbbb117SLinus Walleij goto err_node_put; 275ccbbb117SLinus Walleij } 276ccbbb117SLinus Walleij led->slow = gd; 277ccbbb117SLinus Walleij 27872052fccSSimon Guinot ret = of_property_read_string(child, "linux,default-trigger", 27972052fccSSimon Guinot &string); 28072052fccSSimon Guinot if (ret == 0) 281f7fafd08SVincent Donnefort led->default_trigger = string; 28272052fccSSimon Guinot 283f7fafd08SVincent Donnefort ret = of_property_count_u32_elems(child, "modes-map"); 284f7fafd08SVincent Donnefort if (ret < 0 || ret % 3) { 285f7fafd08SVincent Donnefort dev_err(dev, 286f7fafd08SVincent Donnefort "Missing or malformed modes-map property\n"); 28779937a4bSNishka Dasgupta ret = -EINVAL; 28879937a4bSNishka Dasgupta goto err_node_put; 289f7fafd08SVincent Donnefort } 290f7fafd08SVincent Donnefort 291f7fafd08SVincent Donnefort num_modes = ret / 3; 292a86854d0SKees Cook modval = devm_kcalloc(dev, 293a86854d0SKees Cook num_modes, 294a86854d0SKees Cook sizeof(struct ns2_led_modval), 295f7fafd08SVincent Donnefort GFP_KERNEL); 29679937a4bSNishka Dasgupta if (!modval) { 29779937a4bSNishka Dasgupta ret = -ENOMEM; 29879937a4bSNishka Dasgupta goto err_node_put; 29979937a4bSNishka Dasgupta } 300f7fafd08SVincent Donnefort 301f7fafd08SVincent Donnefort for (i = 0; i < num_modes; i++) { 302f7fafd08SVincent Donnefort of_property_read_u32_index(child, 303f7fafd08SVincent Donnefort "modes-map", 3 * i, 304f7fafd08SVincent Donnefort (u32 *) &modval[i].mode); 305f7fafd08SVincent Donnefort of_property_read_u32_index(child, 306f7fafd08SVincent Donnefort "modes-map", 3 * i + 1, 307f7fafd08SVincent Donnefort (u32 *) &modval[i].cmd_level); 308f7fafd08SVincent Donnefort of_property_read_u32_index(child, 309f7fafd08SVincent Donnefort "modes-map", 3 * i + 2, 310f7fafd08SVincent Donnefort (u32 *) &modval[i].slow_level); 311f7fafd08SVincent Donnefort } 312f7fafd08SVincent Donnefort 313f7fafd08SVincent Donnefort led->num_modes = num_modes; 314f7fafd08SVincent Donnefort led->modval = modval; 315f7fafd08SVincent Donnefort 316f7fafd08SVincent Donnefort led++; 31772052fccSSimon Guinot } 31872052fccSSimon Guinot 31901d0b14dSMarek Behún ofdata->leds = leds; 32001d0b14dSMarek Behún ofdata->num_leds = num_leds; 32172052fccSSimon Guinot 32272052fccSSimon Guinot return 0; 32379937a4bSNishka Dasgupta 32479937a4bSNishka Dasgupta err_node_put: 32579937a4bSNishka Dasgupta of_node_put(child); 32679937a4bSNishka Dasgupta return ret; 32772052fccSSimon Guinot } 32872052fccSSimon Guinot 32972052fccSSimon Guinot static const struct of_device_id of_ns2_leds_match[] = { 33072052fccSSimon Guinot { .compatible = "lacie,ns2-leds", }, 33172052fccSSimon Guinot {}, 33272052fccSSimon Guinot }; 33398f9cc7fSLuis de Bethencourt MODULE_DEVICE_TABLE(of, of_ns2_leds_match); 33472052fccSSimon Guinot 33598ea1ea2SBill Pemberton static int ns2_led_probe(struct platform_device *pdev) 33611efe71fSSimon Guinot { 33701d0b14dSMarek Behún struct ns2_led_of *ofdata; 33819d4deb7SMarek Behún struct ns2_led_data *leds; 33911efe71fSSimon Guinot int i; 34011efe71fSSimon Guinot int ret; 34111efe71fSSimon Guinot 34201d0b14dSMarek Behún ofdata = devm_kzalloc(&pdev->dev, sizeof(struct ns2_led_of), 34372052fccSSimon Guinot GFP_KERNEL); 34401d0b14dSMarek Behún if (!ofdata) 34572052fccSSimon Guinot return -ENOMEM; 34672052fccSSimon Guinot 34701d0b14dSMarek Behún ret = ns2_leds_parse_of(&pdev->dev, ofdata); 34872052fccSSimon Guinot if (ret) 34972052fccSSimon Guinot return ret; 35011efe71fSSimon Guinot 35119d4deb7SMarek Behún leds = devm_kzalloc(&pdev->dev, array_size(sizeof(*leds), 35201d0b14dSMarek Behún ofdata->num_leds), 35319d4deb7SMarek Behún GFP_KERNEL); 35419d4deb7SMarek Behún if (!leds) 35511efe71fSSimon Guinot return -ENOMEM; 35611efe71fSSimon Guinot 35701d0b14dSMarek Behún for (i = 0; i < ofdata->num_leds; i++) { 35801d0b14dSMarek Behún ret = create_ns2_led(pdev, &leds[i], &ofdata->leds[i]); 35940f97281SMarek Behún if (ret < 0) 360a209f766SBryan Wu return ret; 361a209f766SBryan Wu } 36211efe71fSSimon Guinot 36311efe71fSSimon Guinot return 0; 36411efe71fSSimon Guinot } 36511efe71fSSimon Guinot 36611efe71fSSimon Guinot static struct platform_driver ns2_led_driver = { 36711efe71fSSimon Guinot .probe = ns2_led_probe, 36811efe71fSSimon Guinot .driver = { 36911efe71fSSimon Guinot .name = "leds-ns2", 37072052fccSSimon Guinot .of_match_table = of_match_ptr(of_ns2_leds_match), 37111efe71fSSimon Guinot }, 37211efe71fSSimon Guinot }; 37311efe71fSSimon Guinot 374892a8843SAxel Lin module_platform_driver(ns2_led_driver); 37511efe71fSSimon Guinot 37611efe71fSSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>"); 37711efe71fSSimon Guinot MODULE_DESCRIPTION("Network Space v2 LED driver"); 37811efe71fSSimon Guinot MODULE_LICENSE("GPL"); 379892a8843SAxel Lin MODULE_ALIAS("platform:leds-ns2"); 380