Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 2 | /* |
| 3 | * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED |
| 4 | * |
| 5 | * Copyright (C) 2010 LaCie |
| 6 | * |
| 7 | * Author: Simon Guinot <sguinot@lacie.com> |
| 8 | * |
| 9 | * Based on leds-gpio.c by Raphael Assenat <raph@8d.com> |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/gpio.h> |
| 16 | #include <linux/leds.h> |
Paul Gortmaker | 54f4ded | 2011-07-03 13:56:03 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Arnd Bergmann | c02cecb | 2012-08-24 15:21:54 +0200 | [diff] [blame] | 18 | #include <linux/platform_data/leds-kirkwood-ns2.h> |
Sachin Kamat | c68f46d | 2013-09-28 04:38:30 -0700 | [diff] [blame] | 19 | #include <linux/of.h> |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 20 | #include <linux/of_gpio.h> |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 21 | #include "leds.h" |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 22 | |
| 23 | /* |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 24 | * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED |
| 25 | * modes are available: off, on and SATA activity blinking. The LED modes are |
| 26 | * controlled through two GPIOs (command and slow): each combination of values |
| 27 | * for the command/slow GPIOs corresponds to a LED mode. |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 28 | */ |
| 29 | |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 30 | struct ns2_led_data { |
| 31 | struct led_classdev cdev; |
Kitone Elvis Peter | 2224f2f | 2018-08-06 20:27:59 +0300 | [diff] [blame] | 32 | unsigned int cmd; |
| 33 | unsigned int slow; |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 34 | bool can_sleep; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 35 | unsigned char sata; /* True when SATA mode active. */ |
| 36 | rwlock_t rw_lock; /* Lock GPIOs. */ |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 37 | int num_modes; |
| 38 | struct ns2_led_modval *modval; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static int ns2_led_get_mode(struct ns2_led_data *led_dat, |
| 42 | enum ns2_led_modes *mode) |
| 43 | { |
| 44 | int i; |
| 45 | int ret = -EINVAL; |
| 46 | int cmd_level; |
| 47 | int slow_level; |
| 48 | |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 49 | cmd_level = gpio_get_value_cansleep(led_dat->cmd); |
| 50 | slow_level = gpio_get_value_cansleep(led_dat->slow); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 51 | |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 52 | for (i = 0; i < led_dat->num_modes; i++) { |
| 53 | if (cmd_level == led_dat->modval[i].cmd_level && |
| 54 | slow_level == led_dat->modval[i].slow_level) { |
| 55 | *mode = led_dat->modval[i].mode; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 56 | ret = 0; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | static void ns2_led_set_mode(struct ns2_led_data *led_dat, |
| 65 | enum ns2_led_modes mode) |
| 66 | { |
| 67 | int i; |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 68 | bool found = false; |
Simon Guinot | f539dfe | 2010-09-19 15:30:59 +0200 | [diff] [blame] | 69 | unsigned long flags; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 70 | |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 71 | for (i = 0; i < led_dat->num_modes; i++) |
| 72 | if (mode == led_dat->modval[i].mode) { |
| 73 | found = true; |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | if (!found) |
| 78 | return; |
| 79 | |
Simon Guinot | f539dfe | 2010-09-19 15:30:59 +0200 | [diff] [blame] | 80 | write_lock_irqsave(&led_dat->rw_lock, flags); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 81 | |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 82 | if (!led_dat->can_sleep) { |
| 83 | gpio_set_value(led_dat->cmd, |
| 84 | led_dat->modval[i].cmd_level); |
| 85 | gpio_set_value(led_dat->slow, |
| 86 | led_dat->modval[i].slow_level); |
| 87 | goto exit_unlock; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Jacek Anaszewski | c29e650 | 2015-11-20 11:39:41 +0100 | [diff] [blame] | 90 | gpio_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level); |
| 91 | gpio_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level); |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 92 | |
| 93 | exit_unlock: |
Simon Guinot | f539dfe | 2010-09-19 15:30:59 +0200 | [diff] [blame] | 94 | write_unlock_irqrestore(&led_dat->rw_lock, flags); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static void ns2_led_set(struct led_classdev *led_cdev, |
| 98 | enum led_brightness value) |
| 99 | { |
| 100 | struct ns2_led_data *led_dat = |
| 101 | container_of(led_cdev, struct ns2_led_data, cdev); |
| 102 | enum ns2_led_modes mode; |
| 103 | |
| 104 | if (value == LED_OFF) |
| 105 | mode = NS_V2_LED_OFF; |
| 106 | else if (led_dat->sata) |
| 107 | mode = NS_V2_LED_SATA; |
| 108 | else |
| 109 | mode = NS_V2_LED_ON; |
| 110 | |
| 111 | ns2_led_set_mode(led_dat, mode); |
| 112 | } |
| 113 | |
Jacek Anaszewski | c29e650 | 2015-11-20 11:39:41 +0100 | [diff] [blame] | 114 | static int ns2_led_set_blocking(struct led_classdev *led_cdev, |
| 115 | enum led_brightness value) |
| 116 | { |
| 117 | ns2_led_set(led_cdev, value); |
| 118 | return 0; |
| 119 | } |
| 120 | |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 121 | static ssize_t ns2_led_sata_store(struct device *dev, |
| 122 | struct device_attribute *attr, |
| 123 | const char *buff, size_t count) |
| 124 | { |
Simon Guinot | e5971bb | 2010-10-07 16:35:40 +0200 | [diff] [blame] | 125 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 126 | struct ns2_led_data *led_dat = |
| 127 | container_of(led_cdev, struct ns2_led_data, cdev); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 128 | int ret; |
| 129 | unsigned long enable; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 130 | |
Jingoo Han | 3874350 | 2012-10-23 05:25:35 -0700 | [diff] [blame] | 131 | ret = kstrtoul(buff, 10, &enable); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 132 | if (ret < 0) |
| 133 | return ret; |
| 134 | |
| 135 | enable = !!enable; |
| 136 | |
| 137 | if (led_dat->sata == enable) |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 138 | goto exit; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 139 | |
| 140 | led_dat->sata = enable; |
| 141 | |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 142 | if (!led_get_brightness(led_cdev)) |
| 143 | goto exit; |
| 144 | |
| 145 | if (enable) |
| 146 | ns2_led_set_mode(led_dat, NS_V2_LED_SATA); |
| 147 | else |
| 148 | ns2_led_set_mode(led_dat, NS_V2_LED_ON); |
| 149 | |
| 150 | exit: |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 151 | return count; |
| 152 | } |
| 153 | |
| 154 | static ssize_t ns2_led_sata_show(struct device *dev, |
| 155 | struct device_attribute *attr, char *buf) |
| 156 | { |
Simon Guinot | e5971bb | 2010-10-07 16:35:40 +0200 | [diff] [blame] | 157 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 158 | struct ns2_led_data *led_dat = |
| 159 | container_of(led_cdev, struct ns2_led_data, cdev); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 160 | |
| 161 | return sprintf(buf, "%d\n", led_dat->sata); |
| 162 | } |
| 163 | |
| 164 | static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store); |
| 165 | |
Johan Hovold | 475f854 | 2014-06-25 10:08:51 -0700 | [diff] [blame] | 166 | static struct attribute *ns2_led_attrs[] = { |
| 167 | &dev_attr_sata.attr, |
| 168 | NULL |
| 169 | }; |
| 170 | ATTRIBUTE_GROUPS(ns2_led); |
| 171 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 172 | static int |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 173 | create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat, |
| 174 | const struct ns2_led *template) |
| 175 | { |
| 176 | int ret; |
| 177 | enum ns2_led_modes mode; |
| 178 | |
Sachin Kamat | 0419582 | 2012-11-25 10:28:10 +0530 | [diff] [blame] | 179 | ret = devm_gpio_request_one(&pdev->dev, template->cmd, |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 180 | gpio_get_value_cansleep(template->cmd) ? |
Jingoo Han | 9d04cba | 2013-03-07 18:38:26 -0800 | [diff] [blame] | 181 | GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW, |
Jingoo Han | 31c3dc7 | 2012-10-23 05:18:21 -0700 | [diff] [blame] | 182 | template->name); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 183 | if (ret) { |
| 184 | dev_err(&pdev->dev, "%s: failed to setup command GPIO\n", |
| 185 | template->name); |
Jingoo Han | 31c3dc7 | 2012-10-23 05:18:21 -0700 | [diff] [blame] | 186 | return ret; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Sachin Kamat | 0419582 | 2012-11-25 10:28:10 +0530 | [diff] [blame] | 189 | ret = devm_gpio_request_one(&pdev->dev, template->slow, |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 190 | gpio_get_value_cansleep(template->slow) ? |
Jingoo Han | 9d04cba | 2013-03-07 18:38:26 -0800 | [diff] [blame] | 191 | GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW, |
Jingoo Han | 31c3dc7 | 2012-10-23 05:18:21 -0700 | [diff] [blame] | 192 | template->name); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 193 | if (ret) { |
| 194 | dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n", |
| 195 | template->name); |
Sachin Kamat | 0419582 | 2012-11-25 10:28:10 +0530 | [diff] [blame] | 196 | return ret; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | rwlock_init(&led_dat->rw_lock); |
| 200 | |
| 201 | led_dat->cdev.name = template->name; |
| 202 | led_dat->cdev.default_trigger = template->default_trigger; |
| 203 | led_dat->cdev.blink_set = NULL; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 204 | led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; |
Johan Hovold | 475f854 | 2014-06-25 10:08:51 -0700 | [diff] [blame] | 205 | led_dat->cdev.groups = ns2_led_groups; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 206 | led_dat->cmd = template->cmd; |
| 207 | led_dat->slow = template->slow; |
Simon Guinot | 4b90432 | 2015-07-02 19:56:42 +0200 | [diff] [blame] | 208 | led_dat->can_sleep = gpio_cansleep(led_dat->cmd) | |
| 209 | gpio_cansleep(led_dat->slow); |
Jacek Anaszewski | c29e650 | 2015-11-20 11:39:41 +0100 | [diff] [blame] | 210 | if (led_dat->can_sleep) |
| 211 | led_dat->cdev.brightness_set_blocking = ns2_led_set_blocking; |
| 212 | else |
| 213 | led_dat->cdev.brightness_set = ns2_led_set; |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 214 | led_dat->modval = template->modval; |
| 215 | led_dat->num_modes = template->num_modes; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 216 | |
| 217 | ret = ns2_led_get_mode(led_dat, &mode); |
| 218 | if (ret < 0) |
Sachin Kamat | 0419582 | 2012-11-25 10:28:10 +0530 | [diff] [blame] | 219 | return ret; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 220 | |
| 221 | /* Set LED initial state. */ |
| 222 | led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0; |
| 223 | led_dat->cdev.brightness = |
| 224 | (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL; |
| 225 | |
| 226 | ret = led_classdev_register(&pdev->dev, &led_dat->cdev); |
| 227 | if (ret < 0) |
Sachin Kamat | 0419582 | 2012-11-25 10:28:10 +0530 | [diff] [blame] | 228 | return ret; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 229 | |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 230 | return 0; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Arnd Bergmann | b8cd742 | 2012-05-10 13:01:46 -0700 | [diff] [blame] | 233 | static void delete_ns2_led(struct ns2_led_data *led_dat) |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 234 | { |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 235 | led_classdev_unregister(&led_dat->cdev); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 238 | #ifdef CONFIG_OF_GPIO |
| 239 | /* |
| 240 | * Translate OpenFirmware node properties into platform_data. |
| 241 | */ |
Linus Torvalds | cf4af01 | 2012-12-12 12:14:06 -0800 | [diff] [blame] | 242 | static int |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 243 | ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata) |
| 244 | { |
| 245 | struct device_node *np = dev->of_node; |
| 246 | struct device_node *child; |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 247 | struct ns2_led *led, *leds; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 248 | int num_leds = 0; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 249 | |
| 250 | num_leds = of_get_child_count(np); |
| 251 | if (!num_leds) |
| 252 | return -ENODEV; |
| 253 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 254 | leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led), |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 255 | GFP_KERNEL); |
| 256 | if (!leds) |
| 257 | return -ENOMEM; |
| 258 | |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 259 | led = leds; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 260 | for_each_child_of_node(np, child) { |
| 261 | const char *string; |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 262 | int ret, i, num_modes; |
| 263 | struct ns2_led_modval *modval; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 264 | |
| 265 | ret = of_get_named_gpio(child, "cmd-gpio", 0); |
| 266 | if (ret < 0) |
| 267 | return ret; |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 268 | led->cmd = ret; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 269 | ret = of_get_named_gpio(child, "slow-gpio", 0); |
| 270 | if (ret < 0) |
| 271 | return ret; |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 272 | led->slow = ret; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 273 | ret = of_property_read_string(child, "label", &string); |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 274 | led->name = (ret == 0) ? string : child->name; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 275 | ret = of_property_read_string(child, "linux,default-trigger", |
| 276 | &string); |
| 277 | if (ret == 0) |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 278 | led->default_trigger = string; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 279 | |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 280 | ret = of_property_count_u32_elems(child, "modes-map"); |
| 281 | if (ret < 0 || ret % 3) { |
| 282 | dev_err(dev, |
| 283 | "Missing or malformed modes-map property\n"); |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
| 287 | num_modes = ret / 3; |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 288 | modval = devm_kcalloc(dev, |
| 289 | num_modes, |
| 290 | sizeof(struct ns2_led_modval), |
Vincent Donnefort | f7fafd0 | 2015-07-02 19:56:40 +0200 | [diff] [blame] | 291 | GFP_KERNEL); |
| 292 | if (!modval) |
| 293 | return -ENOMEM; |
| 294 | |
| 295 | for (i = 0; i < num_modes; i++) { |
| 296 | of_property_read_u32_index(child, |
| 297 | "modes-map", 3 * i, |
| 298 | (u32 *) &modval[i].mode); |
| 299 | of_property_read_u32_index(child, |
| 300 | "modes-map", 3 * i + 1, |
| 301 | (u32 *) &modval[i].cmd_level); |
| 302 | of_property_read_u32_index(child, |
| 303 | "modes-map", 3 * i + 2, |
| 304 | (u32 *) &modval[i].slow_level); |
| 305 | } |
| 306 | |
| 307 | led->num_modes = num_modes; |
| 308 | led->modval = modval; |
| 309 | |
| 310 | led++; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | pdata->leds = leds; |
| 314 | pdata->num_leds = num_leds; |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static const struct of_device_id of_ns2_leds_match[] = { |
| 320 | { .compatible = "lacie,ns2-leds", }, |
| 321 | {}, |
| 322 | }; |
Luis de Bethencourt | 98f9cc7 | 2015-09-01 23:36:59 +0200 | [diff] [blame] | 323 | MODULE_DEVICE_TABLE(of, of_ns2_leds_match); |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 324 | #endif /* CONFIG_OF_GPIO */ |
| 325 | |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 326 | struct ns2_led_priv { |
| 327 | int num_leds; |
| 328 | struct ns2_led_data leds_data[]; |
| 329 | }; |
| 330 | |
| 331 | static inline int sizeof_ns2_led_priv(int num_leds) |
| 332 | { |
| 333 | return sizeof(struct ns2_led_priv) + |
| 334 | (sizeof(struct ns2_led_data) * num_leds); |
| 335 | } |
| 336 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 337 | static int ns2_led_probe(struct platform_device *pdev) |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 338 | { |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 339 | struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 340 | struct ns2_led_priv *priv; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 341 | int i; |
| 342 | int ret; |
| 343 | |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 344 | #ifdef CONFIG_OF_GPIO |
| 345 | if (!pdata) { |
| 346 | pdata = devm_kzalloc(&pdev->dev, |
| 347 | sizeof(struct ns2_led_platform_data), |
| 348 | GFP_KERNEL); |
| 349 | if (!pdata) |
| 350 | return -ENOMEM; |
| 351 | |
| 352 | ret = ns2_leds_get_of_pdata(&pdev->dev, pdata); |
| 353 | if (ret) |
| 354 | return ret; |
| 355 | } |
| 356 | #else |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 357 | if (!pdata) |
| 358 | return -EINVAL; |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 359 | #endif /* CONFIG_OF_GPIO */ |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 360 | |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 361 | priv = devm_kzalloc(&pdev->dev, |
| 362 | sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL); |
| 363 | if (!priv) |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 364 | return -ENOMEM; |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 365 | priv->num_leds = pdata->num_leds; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 366 | |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 367 | for (i = 0; i < priv->num_leds; i++) { |
| 368 | ret = create_ns2_led(pdev, &priv->leds_data[i], |
| 369 | &pdata->leds[i]); |
Bryan Wu | a209f76 | 2012-07-04 12:30:50 +0800 | [diff] [blame] | 370 | if (ret < 0) { |
| 371 | for (i = i - 1; i >= 0; i--) |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 372 | delete_ns2_led(&priv->leds_data[i]); |
Bryan Wu | a209f76 | 2012-07-04 12:30:50 +0800 | [diff] [blame] | 373 | return ret; |
| 374 | } |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 377 | platform_set_drvdata(pdev, priv); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 378 | |
| 379 | return 0; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 382 | static int ns2_led_remove(struct platform_device *pdev) |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 383 | { |
| 384 | int i; |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 385 | struct ns2_led_priv *priv; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 386 | |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 387 | priv = platform_get_drvdata(pdev); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 388 | |
Simon Guinot | 3de1929 | 2013-03-19 11:07:29 -0700 | [diff] [blame] | 389 | for (i = 0; i < priv->num_leds; i++) |
| 390 | delete_ns2_led(&priv->leds_data[i]); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 391 | |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static struct platform_driver ns2_led_driver = { |
| 396 | .probe = ns2_led_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 397 | .remove = ns2_led_remove, |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 398 | .driver = { |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 399 | .name = "leds-ns2", |
Simon Guinot | 72052fc | 2012-10-17 12:09:03 +0200 | [diff] [blame] | 400 | .of_match_table = of_match_ptr(of_ns2_leds_match), |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 401 | }, |
| 402 | }; |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 403 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 404 | module_platform_driver(ns2_led_driver); |
Simon Guinot | 11efe71 | 2010-07-06 16:08:46 +0200 | [diff] [blame] | 405 | |
| 406 | MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>"); |
| 407 | MODULE_DESCRIPTION("Network Space v2 LED driver"); |
| 408 | MODULE_LICENSE("GPL"); |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 409 | MODULE_ALIAS("platform:leds-ns2"); |