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 42c7896490SLinus Walleij struct ns2_led_platform_data { 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 #ifdef CONFIG_OF_GPIO 23472052fccSSimon Guinot /* 23572052fccSSimon Guinot * Translate OpenFirmware node properties into platform_data. 23672052fccSSimon Guinot */ 237cf4af012SLinus Torvalds static int 23872052fccSSimon Guinot ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata) 23972052fccSSimon Guinot { 2408853c95eSMarek Behún struct device_node *np = dev_of_node(dev); 24172052fccSSimon Guinot struct device_node *child; 242f7fafd08SVincent Donnefort struct ns2_led *led, *leds; 24379937a4bSNishka Dasgupta int ret, num_leds = 0; 24472052fccSSimon Guinot 24599a013c8SMarek Behún num_leds = of_get_available_child_count(np); 24672052fccSSimon Guinot if (!num_leds) 24772052fccSSimon Guinot return -ENODEV; 24872052fccSSimon Guinot 249a86854d0SKees Cook leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led), 25072052fccSSimon Guinot GFP_KERNEL); 25172052fccSSimon Guinot if (!leds) 25272052fccSSimon Guinot return -ENOMEM; 25372052fccSSimon Guinot 254f7fafd08SVincent Donnefort led = leds; 25599a013c8SMarek Behún for_each_available_child_of_node(np, child) { 25672052fccSSimon Guinot const char *string; 25779937a4bSNishka Dasgupta int i, num_modes; 258f7fafd08SVincent Donnefort struct ns2_led_modval *modval; 259ccbbb117SLinus Walleij struct gpio_desc *gd; 26072052fccSSimon Guinot 26172052fccSSimon Guinot ret = of_property_read_string(child, "label", &string); 262f7fafd08SVincent Donnefort led->name = (ret == 0) ? string : child->name; 263ccbbb117SLinus Walleij 264ccbbb117SLinus Walleij gd = gpiod_get_from_of_node(child, "cmd-gpio", 0, 265ccbbb117SLinus Walleij GPIOD_ASIS, led->name); 266ccbbb117SLinus Walleij if (IS_ERR(gd)) { 267ccbbb117SLinus Walleij ret = PTR_ERR(gd); 268ccbbb117SLinus Walleij goto err_node_put; 269ccbbb117SLinus Walleij } 270ccbbb117SLinus Walleij led->cmd = gd; 271ccbbb117SLinus Walleij gd = gpiod_get_from_of_node(child, "slow-gpio", 0, 272ccbbb117SLinus Walleij GPIOD_ASIS, led->name); 273ccbbb117SLinus Walleij if (IS_ERR(gd)) { 274ccbbb117SLinus Walleij ret = PTR_ERR(gd); 275ccbbb117SLinus Walleij goto err_node_put; 276ccbbb117SLinus Walleij } 277ccbbb117SLinus Walleij led->slow = gd; 278ccbbb117SLinus Walleij 27972052fccSSimon Guinot ret = of_property_read_string(child, "linux,default-trigger", 28072052fccSSimon Guinot &string); 28172052fccSSimon Guinot if (ret == 0) 282f7fafd08SVincent Donnefort led->default_trigger = string; 28372052fccSSimon Guinot 284f7fafd08SVincent Donnefort ret = of_property_count_u32_elems(child, "modes-map"); 285f7fafd08SVincent Donnefort if (ret < 0 || ret % 3) { 286f7fafd08SVincent Donnefort dev_err(dev, 287f7fafd08SVincent Donnefort "Missing or malformed modes-map property\n"); 28879937a4bSNishka Dasgupta ret = -EINVAL; 28979937a4bSNishka Dasgupta goto err_node_put; 290f7fafd08SVincent Donnefort } 291f7fafd08SVincent Donnefort 292f7fafd08SVincent Donnefort num_modes = ret / 3; 293a86854d0SKees Cook modval = devm_kcalloc(dev, 294a86854d0SKees Cook num_modes, 295a86854d0SKees Cook sizeof(struct ns2_led_modval), 296f7fafd08SVincent Donnefort GFP_KERNEL); 29779937a4bSNishka Dasgupta if (!modval) { 29879937a4bSNishka Dasgupta ret = -ENOMEM; 29979937a4bSNishka Dasgupta goto err_node_put; 30079937a4bSNishka Dasgupta } 301f7fafd08SVincent Donnefort 302f7fafd08SVincent Donnefort for (i = 0; i < num_modes; i++) { 303f7fafd08SVincent Donnefort of_property_read_u32_index(child, 304f7fafd08SVincent Donnefort "modes-map", 3 * i, 305f7fafd08SVincent Donnefort (u32 *) &modval[i].mode); 306f7fafd08SVincent Donnefort of_property_read_u32_index(child, 307f7fafd08SVincent Donnefort "modes-map", 3 * i + 1, 308f7fafd08SVincent Donnefort (u32 *) &modval[i].cmd_level); 309f7fafd08SVincent Donnefort of_property_read_u32_index(child, 310f7fafd08SVincent Donnefort "modes-map", 3 * i + 2, 311f7fafd08SVincent Donnefort (u32 *) &modval[i].slow_level); 312f7fafd08SVincent Donnefort } 313f7fafd08SVincent Donnefort 314f7fafd08SVincent Donnefort led->num_modes = num_modes; 315f7fafd08SVincent Donnefort led->modval = modval; 316f7fafd08SVincent Donnefort 317f7fafd08SVincent Donnefort led++; 31872052fccSSimon Guinot } 31972052fccSSimon Guinot 32072052fccSSimon Guinot pdata->leds = leds; 32172052fccSSimon Guinot pdata->num_leds = num_leds; 32272052fccSSimon Guinot 32372052fccSSimon Guinot return 0; 32479937a4bSNishka Dasgupta 32579937a4bSNishka Dasgupta err_node_put: 32679937a4bSNishka Dasgupta of_node_put(child); 32779937a4bSNishka Dasgupta return ret; 32872052fccSSimon Guinot } 32972052fccSSimon Guinot 33072052fccSSimon Guinot static const struct of_device_id of_ns2_leds_match[] = { 33172052fccSSimon Guinot { .compatible = "lacie,ns2-leds", }, 33272052fccSSimon Guinot {}, 33372052fccSSimon Guinot }; 33498f9cc7fSLuis de Bethencourt MODULE_DEVICE_TABLE(of, of_ns2_leds_match); 33572052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */ 33672052fccSSimon Guinot 33798ea1ea2SBill Pemberton static int ns2_led_probe(struct platform_device *pdev) 33811efe71fSSimon Guinot { 33987aae1eaSJingoo Han struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev); 34019d4deb7SMarek Behún struct ns2_led_data *leds; 34111efe71fSSimon Guinot int i; 34211efe71fSSimon Guinot int ret; 34311efe71fSSimon Guinot 34472052fccSSimon Guinot #ifdef CONFIG_OF_GPIO 34572052fccSSimon Guinot if (!pdata) { 34672052fccSSimon Guinot pdata = devm_kzalloc(&pdev->dev, 34772052fccSSimon Guinot sizeof(struct ns2_led_platform_data), 34872052fccSSimon Guinot GFP_KERNEL); 34972052fccSSimon Guinot if (!pdata) 35072052fccSSimon Guinot return -ENOMEM; 35172052fccSSimon Guinot 35272052fccSSimon Guinot ret = ns2_leds_get_of_pdata(&pdev->dev, pdata); 35372052fccSSimon Guinot if (ret) 35472052fccSSimon Guinot return ret; 35572052fccSSimon Guinot } 35672052fccSSimon Guinot #else 35711efe71fSSimon Guinot if (!pdata) 35811efe71fSSimon Guinot return -EINVAL; 35972052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */ 36011efe71fSSimon Guinot 36119d4deb7SMarek Behún leds = devm_kzalloc(&pdev->dev, array_size(sizeof(*leds), 36219d4deb7SMarek Behún pdata->num_leds), 36319d4deb7SMarek Behún GFP_KERNEL); 36419d4deb7SMarek Behún if (!leds) 36511efe71fSSimon Guinot return -ENOMEM; 36611efe71fSSimon Guinot 36719d4deb7SMarek Behún for (i = 0; i < pdata->num_leds; i++) { 36819d4deb7SMarek Behún ret = create_ns2_led(pdev, &leds[i], &pdata->leds[i]); 36940f97281SMarek Behún if (ret < 0) 370a209f766SBryan Wu return ret; 371a209f766SBryan Wu } 37211efe71fSSimon Guinot 37311efe71fSSimon Guinot return 0; 37411efe71fSSimon Guinot } 37511efe71fSSimon Guinot 37611efe71fSSimon Guinot static struct platform_driver ns2_led_driver = { 37711efe71fSSimon Guinot .probe = ns2_led_probe, 37811efe71fSSimon Guinot .driver = { 37911efe71fSSimon Guinot .name = "leds-ns2", 38072052fccSSimon Guinot .of_match_table = of_match_ptr(of_ns2_leds_match), 38111efe71fSSimon Guinot }, 38211efe71fSSimon Guinot }; 38311efe71fSSimon Guinot 384892a8843SAxel Lin module_platform_driver(ns2_led_driver); 38511efe71fSSimon Guinot 38611efe71fSSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>"); 38711efe71fSSimon Guinot MODULE_DESCRIPTION("Network Space v2 LED driver"); 38811efe71fSSimon Guinot MODULE_LICENSE("GPL"); 389892a8843SAxel Lin MODULE_ALIAS("platform:leds-ns2"); 390