2 * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
4 * Copyright (C) 2010 LaCie
6 * Author: Simon Guinot <sguinot@lacie.com>
8 * Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
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.
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.
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
25 #include <linux/kernel.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/gpio.h>
29 #include <linux/leds.h>
30 #include <linux/module.h>
31 #include <linux/platform_data/leds-kirkwood-ns2.h>
33 #include <linux/of_gpio.h>
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.
44 struct led_classdev cdev
;
49 unsigned char sata
; /* True when SATA mode active. */
50 rwlock_t rw_lock
; /* Lock GPIOs. */
51 struct work_struct work
;
53 struct ns2_led_modval
*modval
;
56 static void ns2_led_work(struct work_struct
*work
)
58 struct ns2_led_data
*led_dat
=
59 container_of(work
, struct ns2_led_data
, work
);
60 int i
= led_dat
->mode_index
;
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
);
66 static int ns2_led_get_mode(struct ns2_led_data
*led_dat
,
67 enum ns2_led_modes
*mode
)
74 cmd_level
= gpio_get_value_cansleep(led_dat
->cmd
);
75 slow_level
= gpio_get_value_cansleep(led_dat
->slow
);
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
;
89 static void ns2_led_set_mode(struct ns2_led_data
*led_dat
,
90 enum ns2_led_modes mode
)
96 for (i
= 0; i
< led_dat
->num_modes
; i
++)
97 if (mode
== led_dat
->modval
[i
].mode
) {
105 write_lock_irqsave(&led_dat
->rw_lock
, flags
);
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
);
115 led_dat
->mode_index
= i
;
116 schedule_work(&led_dat
->work
);
119 write_unlock_irqrestore(&led_dat
->rw_lock
, flags
);
122 static void ns2_led_set(struct led_classdev
*led_cdev
,
123 enum led_brightness value
)
125 struct ns2_led_data
*led_dat
=
126 container_of(led_cdev
, struct ns2_led_data
, cdev
);
127 enum ns2_led_modes mode
;
129 if (value
== LED_OFF
)
130 mode
= NS_V2_LED_OFF
;
131 else if (led_dat
->sata
)
132 mode
= NS_V2_LED_SATA
;
136 ns2_led_set_mode(led_dat
, mode
);
139 static ssize_t
ns2_led_sata_store(struct device
*dev
,
140 struct device_attribute
*attr
,
141 const char *buff
, size_t count
)
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
);
147 unsigned long enable
;
149 ret
= kstrtoul(buff
, 10, &enable
);
155 if (led_dat
->sata
== enable
)
158 led_dat
->sata
= enable
;
160 if (!led_get_brightness(led_cdev
))
164 ns2_led_set_mode(led_dat
, NS_V2_LED_SATA
);
166 ns2_led_set_mode(led_dat
, NS_V2_LED_ON
);
172 static ssize_t
ns2_led_sata_show(struct device
*dev
,
173 struct device_attribute
*attr
, char *buf
)
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
);
179 return sprintf(buf
, "%d\n", led_dat
->sata
);
182 static DEVICE_ATTR(sata
, 0644, ns2_led_sata_show
, ns2_led_sata_store
);
184 static struct attribute
*ns2_led_attrs
[] = {
188 ATTRIBUTE_GROUPS(ns2_led
);
191 create_ns2_led(struct platform_device
*pdev
, struct ns2_led_data
*led_dat
,
192 const struct ns2_led
*template)
195 enum ns2_led_modes mode
;
197 ret
= devm_gpio_request_one(&pdev
->dev
, template->cmd
,
198 gpio_get_value_cansleep(template->cmd
) ?
199 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
,
202 dev_err(&pdev
->dev
, "%s: failed to setup command GPIO\n",
207 ret
= devm_gpio_request_one(&pdev
->dev
, template->slow
,
208 gpio_get_value_cansleep(template->slow
) ?
209 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
,
212 dev_err(&pdev
->dev
, "%s: failed to setup slow GPIO\n",
217 rwlock_init(&led_dat
->rw_lock
);
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
;
224 led_dat
->cdev
.groups
= ns2_led_groups
;
225 led_dat
->cmd
= template->cmd
;
226 led_dat
->slow
= template->slow
;
227 led_dat
->can_sleep
= gpio_cansleep(led_dat
->cmd
) |
228 gpio_cansleep(led_dat
->slow
);
229 led_dat
->modval
= template->modval
;
230 led_dat
->num_modes
= template->num_modes
;
232 ret
= ns2_led_get_mode(led_dat
, &mode
);
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
;
241 INIT_WORK(&led_dat
->work
, ns2_led_work
);
243 ret
= led_classdev_register(&pdev
->dev
, &led_dat
->cdev
);
250 static void delete_ns2_led(struct ns2_led_data
*led_dat
)
252 led_classdev_unregister(&led_dat
->cdev
);
253 cancel_work_sync(&led_dat
->work
);
256 #ifdef CONFIG_OF_GPIO
258 * Translate OpenFirmware node properties into platform_data.
261 ns2_leds_get_of_pdata(struct device
*dev
, struct ns2_led_platform_data
*pdata
)
263 struct device_node
*np
= dev
->of_node
;
264 struct device_node
*child
;
265 struct ns2_led
*led
, *leds
;
268 num_leds
= of_get_child_count(np
);
272 leds
= devm_kzalloc(dev
, num_leds
* sizeof(struct ns2_led
),
278 for_each_child_of_node(np
, child
) {
280 int ret
, i
, num_modes
;
281 struct ns2_led_modval
*modval
;
283 ret
= of_get_named_gpio(child
, "cmd-gpio", 0);
287 ret
= of_get_named_gpio(child
, "slow-gpio", 0);
291 ret
= of_property_read_string(child
, "label", &string
);
292 led
->name
= (ret
== 0) ? string
: child
->name
;
293 ret
= of_property_read_string(child
, "linux,default-trigger",
296 led
->default_trigger
= string
;
298 ret
= of_property_count_u32_elems(child
, "modes-map");
299 if (ret
< 0 || ret
% 3) {
301 "Missing or malformed modes-map property\n");
306 modval
= devm_kzalloc(dev
,
307 num_modes
* sizeof(struct ns2_led_modval
),
312 for (i
= 0; i
< num_modes
; i
++) {
313 of_property_read_u32_index(child
,
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
);
324 led
->num_modes
= num_modes
;
325 led
->modval
= modval
;
331 pdata
->num_leds
= num_leds
;
336 static const struct of_device_id of_ns2_leds_match
[] = {
337 { .compatible
= "lacie,ns2-leds", },
340 MODULE_DEVICE_TABLE(of
, of_ns2_leds_match
);
341 #endif /* CONFIG_OF_GPIO */
343 struct ns2_led_priv
{
345 struct ns2_led_data leds_data
[];
348 static inline int sizeof_ns2_led_priv(int num_leds
)
350 return sizeof(struct ns2_led_priv
) +
351 (sizeof(struct ns2_led_data
) * num_leds
);
354 static int ns2_led_probe(struct platform_device
*pdev
)
356 struct ns2_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
357 struct ns2_led_priv
*priv
;
361 #ifdef CONFIG_OF_GPIO
363 pdata
= devm_kzalloc(&pdev
->dev
,
364 sizeof(struct ns2_led_platform_data
),
369 ret
= ns2_leds_get_of_pdata(&pdev
->dev
, pdata
);
376 #endif /* CONFIG_OF_GPIO */
378 priv
= devm_kzalloc(&pdev
->dev
,
379 sizeof_ns2_led_priv(pdata
->num_leds
), GFP_KERNEL
);
382 priv
->num_leds
= pdata
->num_leds
;
384 for (i
= 0; i
< priv
->num_leds
; i
++) {
385 ret
= create_ns2_led(pdev
, &priv
->leds_data
[i
],
388 for (i
= i
- 1; i
>= 0; i
--)
389 delete_ns2_led(&priv
->leds_data
[i
]);
394 platform_set_drvdata(pdev
, priv
);
399 static int ns2_led_remove(struct platform_device
*pdev
)
402 struct ns2_led_priv
*priv
;
404 priv
= platform_get_drvdata(pdev
);
406 for (i
= 0; i
< priv
->num_leds
; i
++)
407 delete_ns2_led(&priv
->leds_data
[i
]);
412 static struct platform_driver ns2_led_driver
= {
413 .probe
= ns2_led_probe
,
414 .remove
= ns2_led_remove
,
417 .of_match_table
= of_match_ptr(of_ns2_leds_match
),
421 module_platform_driver(ns2_led_driver
);
423 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
424 MODULE_DESCRIPTION("Network Space v2 LED driver");
425 MODULE_LICENSE("GPL");
426 MODULE_ALIAS("platform:leds-ns2");