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