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/init.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/gpio.h>
30 #include <linux/leds.h>
31 #include <linux/module.h>
32 #include <linux/platform_data/leds-kirkwood-ns2.h>
33 #include <linux/of_gpio.h>
36 * The Network Space v2 dual-GPIO LED is wired to a CPLD and can blink in
37 * relation with the SATA activity. This capability is exposed through the
38 * "sata" sysfs attribute.
40 * The following array detail the different LED registers and the combination
41 * of their possible values:
43 * cmd_led | slow_led | /SATA active | LED state
48 * 0 | 0 | 0 | blink (rate 300ms)
57 struct ns2_led_mode_value
{
58 enum ns2_led_modes mode
;
63 static struct ns2_led_mode_value ns2_led_modval
[] = {
64 { NS_V2_LED_OFF
, 1, 0 },
65 { NS_V2_LED_ON
, 0, 1 },
66 { NS_V2_LED_ON
, 1, 1 },
67 { NS_V2_LED_SATA
, 0, 0 },
71 struct led_classdev cdev
;
74 unsigned char sata
; /* True when SATA mode active. */
75 rwlock_t rw_lock
; /* Lock GPIOs. */
78 static int ns2_led_get_mode(struct ns2_led_data
*led_dat
,
79 enum ns2_led_modes
*mode
)
86 read_lock_irq(&led_dat
->rw_lock
);
88 cmd_level
= gpio_get_value(led_dat
->cmd
);
89 slow_level
= gpio_get_value(led_dat
->slow
);
91 for (i
= 0; i
< ARRAY_SIZE(ns2_led_modval
); i
++) {
92 if (cmd_level
== ns2_led_modval
[i
].cmd_level
&&
93 slow_level
== ns2_led_modval
[i
].slow_level
) {
94 *mode
= ns2_led_modval
[i
].mode
;
100 read_unlock_irq(&led_dat
->rw_lock
);
105 static void ns2_led_set_mode(struct ns2_led_data
*led_dat
,
106 enum ns2_led_modes mode
)
111 write_lock_irqsave(&led_dat
->rw_lock
, flags
);
113 for (i
= 0; i
< ARRAY_SIZE(ns2_led_modval
); i
++) {
114 if (mode
== ns2_led_modval
[i
].mode
) {
115 gpio_set_value(led_dat
->cmd
,
116 ns2_led_modval
[i
].cmd_level
);
117 gpio_set_value(led_dat
->slow
,
118 ns2_led_modval
[i
].slow_level
);
122 write_unlock_irqrestore(&led_dat
->rw_lock
, flags
);
125 static void ns2_led_set(struct led_classdev
*led_cdev
,
126 enum led_brightness value
)
128 struct ns2_led_data
*led_dat
=
129 container_of(led_cdev
, struct ns2_led_data
, cdev
);
130 enum ns2_led_modes mode
;
132 if (value
== LED_OFF
)
133 mode
= NS_V2_LED_OFF
;
134 else if (led_dat
->sata
)
135 mode
= NS_V2_LED_SATA
;
139 ns2_led_set_mode(led_dat
, mode
);
142 static ssize_t
ns2_led_sata_store(struct device
*dev
,
143 struct device_attribute
*attr
,
144 const char *buff
, size_t count
)
146 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
147 struct ns2_led_data
*led_dat
=
148 container_of(led_cdev
, struct ns2_led_data
, cdev
);
150 unsigned long enable
;
151 enum ns2_led_modes mode
;
153 ret
= kstrtoul(buff
, 10, &enable
);
159 if (led_dat
->sata
== enable
)
162 ret
= ns2_led_get_mode(led_dat
, &mode
);
166 if (enable
&& mode
== NS_V2_LED_ON
)
167 ns2_led_set_mode(led_dat
, NS_V2_LED_SATA
);
168 if (!enable
&& mode
== NS_V2_LED_SATA
)
169 ns2_led_set_mode(led_dat
, NS_V2_LED_ON
);
171 led_dat
->sata
= enable
;
176 static ssize_t
ns2_led_sata_show(struct device
*dev
,
177 struct device_attribute
*attr
, char *buf
)
179 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
180 struct ns2_led_data
*led_dat
=
181 container_of(led_cdev
, struct ns2_led_data
, cdev
);
183 return sprintf(buf
, "%d\n", led_dat
->sata
);
186 static DEVICE_ATTR(sata
, 0644, ns2_led_sata_show
, ns2_led_sata_store
);
189 create_ns2_led(struct platform_device
*pdev
, struct ns2_led_data
*led_dat
,
190 const struct ns2_led
*template)
193 enum ns2_led_modes mode
;
195 ret
= devm_gpio_request_one(&pdev
->dev
, template->cmd
,
196 gpio_get_value(template->cmd
) ?
197 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
,
200 dev_err(&pdev
->dev
, "%s: failed to setup command GPIO\n",
205 ret
= devm_gpio_request_one(&pdev
->dev
, template->slow
,
206 gpio_get_value(template->slow
) ?
207 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
,
210 dev_err(&pdev
->dev
, "%s: failed to setup slow GPIO\n",
215 rwlock_init(&led_dat
->rw_lock
);
217 led_dat
->cdev
.name
= template->name
;
218 led_dat
->cdev
.default_trigger
= template->default_trigger
;
219 led_dat
->cdev
.blink_set
= NULL
;
220 led_dat
->cdev
.brightness_set
= ns2_led_set
;
221 led_dat
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
222 led_dat
->cmd
= template->cmd
;
223 led_dat
->slow
= template->slow
;
225 ret
= ns2_led_get_mode(led_dat
, &mode
);
229 /* Set LED initial state. */
230 led_dat
->sata
= (mode
== NS_V2_LED_SATA
) ? 1 : 0;
231 led_dat
->cdev
.brightness
=
232 (mode
== NS_V2_LED_OFF
) ? LED_OFF
: LED_FULL
;
234 ret
= led_classdev_register(&pdev
->dev
, &led_dat
->cdev
);
238 ret
= device_create_file(led_dat
->cdev
.dev
, &dev_attr_sata
);
245 led_classdev_unregister(&led_dat
->cdev
);
249 static void delete_ns2_led(struct ns2_led_data
*led_dat
)
251 device_remove_file(led_dat
->cdev
.dev
, &dev_attr_sata
);
252 led_classdev_unregister(&led_dat
->cdev
);
255 #ifdef CONFIG_OF_GPIO
257 * Translate OpenFirmware node properties into platform_data.
260 ns2_leds_get_of_pdata(struct device
*dev
, struct ns2_led_platform_data
*pdata
)
262 struct device_node
*np
= dev
->of_node
;
263 struct device_node
*child
;
264 struct ns2_led
*leds
;
268 num_leds
= of_get_child_count(np
);
272 leds
= devm_kzalloc(dev
, num_leds
* sizeof(struct ns2_led
),
277 for_each_child_of_node(np
, child
) {
281 ret
= of_get_named_gpio(child
, "cmd-gpio", 0);
285 ret
= of_get_named_gpio(child
, "slow-gpio", 0);
289 ret
= of_property_read_string(child
, "label", &string
);
290 leds
[i
].name
= (ret
== 0) ? string
: child
->name
;
291 ret
= of_property_read_string(child
, "linux,default-trigger",
294 leds
[i
].default_trigger
= string
;
300 pdata
->num_leds
= num_leds
;
305 static const struct of_device_id of_ns2_leds_match
[] = {
306 { .compatible
= "lacie,ns2-leds", },
309 #endif /* CONFIG_OF_GPIO */
311 struct ns2_led_priv
{
313 struct ns2_led_data leds_data
[];
316 static inline int sizeof_ns2_led_priv(int num_leds
)
318 return sizeof(struct ns2_led_priv
) +
319 (sizeof(struct ns2_led_data
) * num_leds
);
322 static int ns2_led_probe(struct platform_device
*pdev
)
324 struct ns2_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
325 struct ns2_led_priv
*priv
;
329 #ifdef CONFIG_OF_GPIO
331 pdata
= devm_kzalloc(&pdev
->dev
,
332 sizeof(struct ns2_led_platform_data
),
337 ret
= ns2_leds_get_of_pdata(&pdev
->dev
, pdata
);
344 #endif /* CONFIG_OF_GPIO */
346 priv
= devm_kzalloc(&pdev
->dev
,
347 sizeof_ns2_led_priv(pdata
->num_leds
), GFP_KERNEL
);
350 priv
->num_leds
= pdata
->num_leds
;
352 for (i
= 0; i
< priv
->num_leds
; i
++) {
353 ret
= create_ns2_led(pdev
, &priv
->leds_data
[i
],
356 for (i
= i
- 1; i
>= 0; i
--)
357 delete_ns2_led(&priv
->leds_data
[i
]);
362 platform_set_drvdata(pdev
, priv
);
367 static int ns2_led_remove(struct platform_device
*pdev
)
370 struct ns2_led_priv
*priv
;
372 priv
= platform_get_drvdata(pdev
);
374 for (i
= 0; i
< priv
->num_leds
; i
++)
375 delete_ns2_led(&priv
->leds_data
[i
]);
380 static struct platform_driver ns2_led_driver
= {
381 .probe
= ns2_led_probe
,
382 .remove
= ns2_led_remove
,
385 .owner
= THIS_MODULE
,
386 .of_match_table
= of_match_ptr(of_ns2_leds_match
),
390 module_platform_driver(ns2_led_driver
);
392 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
393 MODULE_DESCRIPTION("Network Space v2 LED driver");
394 MODULE_LICENSE("GPL");
395 MODULE_ALIAS("platform:leds-ns2");