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