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