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 233f72deb71SMarek Behún static int ns2_leds_parse_one(struct device *dev, struct device_node *np, 234f72deb71SMarek Behún struct ns2_led *led) 235f72deb71SMarek Behún { 236f72deb71SMarek Behún struct ns2_led_modval *modval; 237f72deb71SMarek Behún int nmodes, ret, i; 238f72deb71SMarek Behún 239f72deb71SMarek Behún ret = of_property_read_string(np, "label", &led->name); 240f72deb71SMarek Behún if (ret) 241f72deb71SMarek Behún led->name = np->name; 242f72deb71SMarek Behún 243528c9515SMarek Behún led->cmd = devm_gpiod_get_from_of_node(dev, np, "cmd-gpio", 0, 244528c9515SMarek Behún GPIOD_ASIS, led->name); 245f72deb71SMarek Behún if (IS_ERR(led->cmd)) 246f72deb71SMarek Behún return PTR_ERR(led->cmd); 247f72deb71SMarek Behún 248528c9515SMarek Behún led->slow = devm_gpiod_get_from_of_node(dev, np, "slow-gpio", 0, 249528c9515SMarek Behún GPIOD_ASIS, led->name); 250f72deb71SMarek Behún if (IS_ERR(led->slow)) 251f72deb71SMarek Behún return PTR_ERR(led->slow); 252f72deb71SMarek Behún 253f72deb71SMarek Behún of_property_read_string(np, "linux,default-trigger", 254f72deb71SMarek Behún &led->default_trigger); 255f72deb71SMarek Behún 256f72deb71SMarek Behún ret = of_property_count_u32_elems(np, "modes-map"); 257f72deb71SMarek Behún if (ret < 0 || ret % 3) { 258f72deb71SMarek Behún dev_err(dev, "Missing or malformed modes-map for %pOF\n", np); 259f72deb71SMarek Behún return -EINVAL; 260f72deb71SMarek Behún } 261f72deb71SMarek Behún 262f72deb71SMarek Behún nmodes = ret / 3; 263f72deb71SMarek Behún modval = devm_kcalloc(dev, nmodes, sizeof(*modval), GFP_KERNEL); 264f72deb71SMarek Behún if (!modval) 265f72deb71SMarek Behún return -ENOMEM; 266f72deb71SMarek Behún 267f72deb71SMarek Behún for (i = 0; i < nmodes; i++) { 268f72deb71SMarek Behún u32 val; 269f72deb71SMarek Behún 270f72deb71SMarek Behún of_property_read_u32_index(np, "modes-map", 3 * i, &val); 271f72deb71SMarek Behún modval[i].mode = val; 272f72deb71SMarek Behún of_property_read_u32_index(np, "modes-map", 3 * i + 1, &val); 273f72deb71SMarek Behún modval[i].cmd_level = val; 274f72deb71SMarek Behún of_property_read_u32_index(np, "modes-map", 3 * i + 2, &val); 275f72deb71SMarek Behún modval[i].slow_level = val; 276f72deb71SMarek Behún } 277f72deb71SMarek Behún 278f72deb71SMarek Behún led->num_modes = nmodes; 279f72deb71SMarek Behún led->modval = modval; 280f72deb71SMarek Behún 281f72deb71SMarek Behún return 0; 282f72deb71SMarek Behún } 283f72deb71SMarek Behún 28472052fccSSimon Guinot /* 28572052fccSSimon Guinot * Translate OpenFirmware node properties into platform_data. 28672052fccSSimon Guinot */ 287cf4af012SLinus Torvalds static int 28801d0b14dSMarek Behún ns2_leds_parse_of(struct device *dev, struct ns2_led_of *ofdata) 28972052fccSSimon Guinot { 2908853c95eSMarek Behún struct device_node *np = dev_of_node(dev); 29172052fccSSimon Guinot struct device_node *child; 292f7fafd08SVincent Donnefort struct ns2_led *led, *leds; 29379937a4bSNishka Dasgupta int ret, num_leds = 0; 29472052fccSSimon Guinot 29599a013c8SMarek Behún num_leds = of_get_available_child_count(np); 29672052fccSSimon Guinot if (!num_leds) 29772052fccSSimon Guinot return -ENODEV; 29872052fccSSimon Guinot 299a86854d0SKees Cook leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led), 30072052fccSSimon Guinot GFP_KERNEL); 30172052fccSSimon Guinot if (!leds) 30272052fccSSimon Guinot return -ENOMEM; 30372052fccSSimon Guinot 304f7fafd08SVincent Donnefort led = leds; 30599a013c8SMarek Behún for_each_available_child_of_node(np, child) { 306f72deb71SMarek Behún ret = ns2_leds_parse_one(dev, child, led++); 307f72deb71SMarek Behún if (ret < 0) { 308f72deb71SMarek Behún of_node_put(child); 309f72deb71SMarek Behún return ret; 310ccbbb117SLinus Walleij } 31172052fccSSimon Guinot } 31272052fccSSimon Guinot 31301d0b14dSMarek Behún ofdata->leds = leds; 31401d0b14dSMarek Behún ofdata->num_leds = num_leds; 31572052fccSSimon Guinot 31672052fccSSimon Guinot return 0; 31772052fccSSimon Guinot } 31872052fccSSimon Guinot 31972052fccSSimon Guinot static const struct of_device_id of_ns2_leds_match[] = { 32072052fccSSimon Guinot { .compatible = "lacie,ns2-leds", }, 32172052fccSSimon Guinot {}, 32272052fccSSimon Guinot }; 32398f9cc7fSLuis de Bethencourt MODULE_DEVICE_TABLE(of, of_ns2_leds_match); 32472052fccSSimon Guinot 32598ea1ea2SBill Pemberton static int ns2_led_probe(struct platform_device *pdev) 32611efe71fSSimon Guinot { 32701d0b14dSMarek Behún struct ns2_led_of *ofdata; 32819d4deb7SMarek Behún struct ns2_led_data *leds; 32911efe71fSSimon Guinot int i; 33011efe71fSSimon Guinot int ret; 33111efe71fSSimon Guinot 33201d0b14dSMarek Behún ofdata = devm_kzalloc(&pdev->dev, sizeof(struct ns2_led_of), 33372052fccSSimon Guinot GFP_KERNEL); 33401d0b14dSMarek Behún if (!ofdata) 33572052fccSSimon Guinot return -ENOMEM; 33672052fccSSimon Guinot 33701d0b14dSMarek Behún ret = ns2_leds_parse_of(&pdev->dev, ofdata); 33872052fccSSimon Guinot if (ret) 33972052fccSSimon Guinot return ret; 34011efe71fSSimon Guinot 34119d4deb7SMarek Behún leds = devm_kzalloc(&pdev->dev, array_size(sizeof(*leds), 34201d0b14dSMarek Behún ofdata->num_leds), 34319d4deb7SMarek Behún GFP_KERNEL); 34419d4deb7SMarek Behún if (!leds) 34511efe71fSSimon Guinot return -ENOMEM; 34611efe71fSSimon Guinot 34701d0b14dSMarek Behún for (i = 0; i < ofdata->num_leds; i++) { 34801d0b14dSMarek Behún ret = create_ns2_led(pdev, &leds[i], &ofdata->leds[i]); 34940f97281SMarek Behún if (ret < 0) 350a209f766SBryan Wu return ret; 351a209f766SBryan Wu } 35211efe71fSSimon Guinot 35311efe71fSSimon Guinot return 0; 35411efe71fSSimon Guinot } 35511efe71fSSimon Guinot 35611efe71fSSimon Guinot static struct platform_driver ns2_led_driver = { 35711efe71fSSimon Guinot .probe = ns2_led_probe, 35811efe71fSSimon Guinot .driver = { 35911efe71fSSimon Guinot .name = "leds-ns2", 36072052fccSSimon Guinot .of_match_table = of_match_ptr(of_ns2_leds_match), 36111efe71fSSimon Guinot }, 36211efe71fSSimon Guinot }; 36311efe71fSSimon Guinot 364892a8843SAxel Lin module_platform_driver(ns2_led_driver); 36511efe71fSSimon Guinot 36611efe71fSSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>"); 36711efe71fSSimon Guinot MODULE_DESCRIPTION("Network Space v2 LED driver"); 36811efe71fSSimon Guinot MODULE_LICENSE("GPL"); 369892a8843SAxel Lin MODULE_ALIAS("platform:leds-ns2"); 370