2 * leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
4 * Copyright (C) 2010 LaCie
6 * Author: Simon Guinot <sguinot@lacie.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/irq.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/platform_device.h>
28 #include <linux/gpio.h>
29 #include <linux/leds.h>
30 #include <linux/platform_data/leds-kirkwood-netxbig.h>
36 static DEFINE_SPINLOCK(gpio_ext_lock
);
38 static void gpio_ext_set_addr(struct netxbig_gpio_ext
*gpio_ext
, int addr
)
42 for (pin
= 0; pin
< gpio_ext
->num_addr
; pin
++)
43 gpio_set_value(gpio_ext
->addr
[pin
], (addr
>> pin
) & 1);
46 static void gpio_ext_set_data(struct netxbig_gpio_ext
*gpio_ext
, int data
)
50 for (pin
= 0; pin
< gpio_ext
->num_data
; pin
++)
51 gpio_set_value(gpio_ext
->data
[pin
], (data
>> pin
) & 1);
54 static void gpio_ext_enable_select(struct netxbig_gpio_ext
*gpio_ext
)
56 /* Enable select is done on the raising edge. */
57 gpio_set_value(gpio_ext
->enable
, 0);
58 gpio_set_value(gpio_ext
->enable
, 1);
61 static void gpio_ext_set_value(struct netxbig_gpio_ext
*gpio_ext
,
66 spin_lock_irqsave(&gpio_ext_lock
, flags
);
67 gpio_ext_set_addr(gpio_ext
, addr
);
68 gpio_ext_set_data(gpio_ext
, value
);
69 gpio_ext_enable_select(gpio_ext
);
70 spin_unlock_irqrestore(&gpio_ext_lock
, flags
);
73 static int gpio_ext_init(struct netxbig_gpio_ext
*gpio_ext
)
78 if (unlikely(!gpio_ext
))
81 /* Configure address GPIOs. */
82 for (i
= 0; i
< gpio_ext
->num_addr
; i
++) {
83 err
= gpio_request_one(gpio_ext
->addr
[i
], GPIOF_OUT_INIT_LOW
,
84 "GPIO extension addr");
88 /* Configure data GPIOs. */
89 for (i
= 0; i
< gpio_ext
->num_data
; i
++) {
90 err
= gpio_request_one(gpio_ext
->data
[i
], GPIOF_OUT_INIT_LOW
,
91 "GPIO extension data");
95 /* Configure "enable select" GPIO. */
96 err
= gpio_request_one(gpio_ext
->enable
, GPIOF_OUT_INIT_LOW
,
97 "GPIO extension enable");
104 for (i
= i
- 1; i
>= 0; i
--)
105 gpio_free(gpio_ext
->data
[i
]);
106 i
= gpio_ext
->num_addr
;
108 for (i
= i
- 1; i
>= 0; i
--)
109 gpio_free(gpio_ext
->addr
[i
]);
114 static void gpio_ext_free(struct netxbig_gpio_ext
*gpio_ext
)
118 gpio_free(gpio_ext
->enable
);
119 for (i
= gpio_ext
->num_addr
- 1; i
>= 0; i
--)
120 gpio_free(gpio_ext
->addr
[i
]);
121 for (i
= gpio_ext
->num_data
- 1; i
>= 0; i
--)
122 gpio_free(gpio_ext
->data
[i
]);
129 struct netxbig_led_data
{
130 struct netxbig_gpio_ext
*gpio_ext
;
131 struct led_classdev cdev
;
136 struct netxbig_led_timer
*timer
;
138 enum netxbig_led_mode mode
;
143 static int netxbig_led_get_timer_mode(enum netxbig_led_mode
*mode
,
144 unsigned long delay_on
,
145 unsigned long delay_off
,
146 struct netxbig_led_timer
*timer
,
151 for (i
= 0; i
< num_timer
; i
++) {
152 if (timer
[i
].delay_on
== delay_on
&&
153 timer
[i
].delay_off
== delay_off
) {
154 *mode
= timer
[i
].mode
;
161 static int netxbig_led_blink_set(struct led_classdev
*led_cdev
,
162 unsigned long *delay_on
,
163 unsigned long *delay_off
)
165 struct netxbig_led_data
*led_dat
=
166 container_of(led_cdev
, struct netxbig_led_data
, cdev
);
167 enum netxbig_led_mode mode
;
171 /* Look for a LED mode with the requested timer frequency. */
172 ret
= netxbig_led_get_timer_mode(&mode
, *delay_on
, *delay_off
,
173 led_dat
->timer
, led_dat
->num_timer
);
177 mode_val
= led_dat
->mode_val
[mode
];
178 if (mode_val
== NETXBIG_LED_INVALID_MODE
)
181 spin_lock_irq(&led_dat
->lock
);
183 gpio_ext_set_value(led_dat
->gpio_ext
, led_dat
->mode_addr
, mode_val
);
184 led_dat
->mode
= mode
;
186 spin_unlock_irq(&led_dat
->lock
);
191 static void netxbig_led_set(struct led_classdev
*led_cdev
,
192 enum led_brightness value
)
194 struct netxbig_led_data
*led_dat
=
195 container_of(led_cdev
, struct netxbig_led_data
, cdev
);
196 enum netxbig_led_mode mode
;
197 int mode_val
, bright_val
;
198 int set_brightness
= 1;
201 spin_lock_irqsave(&led_dat
->lock
, flags
);
203 if (value
== LED_OFF
) {
204 mode
= NETXBIG_LED_OFF
;
208 mode
= NETXBIG_LED_SATA
;
209 else if (led_dat
->mode
== NETXBIG_LED_OFF
)
210 mode
= NETXBIG_LED_ON
;
211 else /* Keep 'timer' mode. */
212 mode
= led_dat
->mode
;
214 mode_val
= led_dat
->mode_val
[mode
];
216 gpio_ext_set_value(led_dat
->gpio_ext
, led_dat
->mode_addr
, mode_val
);
217 led_dat
->mode
= mode
;
219 * Note that the brightness register is shared between all the
220 * SATA LEDs. So, change the brightness setting for a single
221 * SATA LED will affect all the others.
223 if (set_brightness
) {
224 bright_val
= DIV_ROUND_UP(value
* led_dat
->bright_max
,
226 gpio_ext_set_value(led_dat
->gpio_ext
,
227 led_dat
->bright_addr
, bright_val
);
230 spin_unlock_irqrestore(&led_dat
->lock
, flags
);
233 static ssize_t
netxbig_led_sata_store(struct device
*dev
,
234 struct device_attribute
*attr
,
235 const char *buff
, size_t count
)
237 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
238 struct netxbig_led_data
*led_dat
=
239 container_of(led_cdev
, struct netxbig_led_data
, cdev
);
240 unsigned long enable
;
241 enum netxbig_led_mode mode
;
245 ret
= kstrtoul(buff
, 10, &enable
);
251 spin_lock_irq(&led_dat
->lock
);
253 if (led_dat
->sata
== enable
) {
258 if (led_dat
->mode
!= NETXBIG_LED_ON
&&
259 led_dat
->mode
!= NETXBIG_LED_SATA
)
260 mode
= led_dat
->mode
; /* Keep modes 'off' and 'timer'. */
262 mode
= NETXBIG_LED_SATA
;
264 mode
= NETXBIG_LED_ON
;
266 mode_val
= led_dat
->mode_val
[mode
];
267 if (mode_val
== NETXBIG_LED_INVALID_MODE
) {
272 gpio_ext_set_value(led_dat
->gpio_ext
, led_dat
->mode_addr
, mode_val
);
273 led_dat
->mode
= mode
;
274 led_dat
->sata
= enable
;
279 spin_unlock_irq(&led_dat
->lock
);
284 static ssize_t
netxbig_led_sata_show(struct device
*dev
,
285 struct device_attribute
*attr
, char *buf
)
287 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
288 struct netxbig_led_data
*led_dat
=
289 container_of(led_cdev
, struct netxbig_led_data
, cdev
);
291 return sprintf(buf
, "%d\n", led_dat
->sata
);
294 static DEVICE_ATTR(sata
, 0644, netxbig_led_sata_show
, netxbig_led_sata_store
);
296 static struct attribute
*netxbig_led_attrs
[] = {
300 ATTRIBUTE_GROUPS(netxbig_led
);
302 static void delete_netxbig_led(struct netxbig_led_data
*led_dat
)
304 led_classdev_unregister(&led_dat
->cdev
);
308 create_netxbig_led(struct platform_device
*pdev
,
309 struct netxbig_led_data
*led_dat
,
310 const struct netxbig_led
*template)
312 struct netxbig_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
314 spin_lock_init(&led_dat
->lock
);
315 led_dat
->gpio_ext
= pdata
->gpio_ext
;
316 led_dat
->cdev
.name
= template->name
;
317 led_dat
->cdev
.default_trigger
= template->default_trigger
;
318 led_dat
->cdev
.blink_set
= netxbig_led_blink_set
;
319 led_dat
->cdev
.brightness_set
= netxbig_led_set
;
321 * Because the GPIO extension bus don't allow to read registers
322 * value, there is no way to probe the LED initial state.
323 * So, the initial sysfs LED value for the "brightness" and "sata"
324 * attributes are inconsistent.
326 * Note that the initial LED state can't be reconfigured.
327 * The reason is that the LED behaviour must stay uniform during
328 * the whole boot process (bootloader+linux).
331 led_dat
->cdev
.brightness
= LED_OFF
;
332 led_dat
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
333 led_dat
->mode_addr
= template->mode_addr
;
334 led_dat
->mode_val
= template->mode_val
;
335 led_dat
->bright_addr
= template->bright_addr
;
336 led_dat
->bright_max
= (1 << pdata
->gpio_ext
->num_data
) - 1;
337 led_dat
->timer
= pdata
->timer
;
338 led_dat
->num_timer
= pdata
->num_timer
;
340 * If available, expose the SATA activity blink capability through
341 * a "sata" sysfs attribute.
343 if (led_dat
->mode_val
[NETXBIG_LED_SATA
] != NETXBIG_LED_INVALID_MODE
)
344 led_dat
->cdev
.groups
= netxbig_led_groups
;
346 return led_classdev_register(&pdev
->dev
, &led_dat
->cdev
);
349 static int netxbig_led_probe(struct platform_device
*pdev
)
351 struct netxbig_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
352 struct netxbig_led_data
*leds_data
;
359 leds_data
= devm_kzalloc(&pdev
->dev
,
360 sizeof(struct netxbig_led_data
) * pdata
->num_leds
, GFP_KERNEL
);
364 ret
= gpio_ext_init(pdata
->gpio_ext
);
368 for (i
= 0; i
< pdata
->num_leds
; i
++) {
369 ret
= create_netxbig_led(pdev
, &leds_data
[i
], &pdata
->leds
[i
]);
374 platform_set_drvdata(pdev
, leds_data
);
379 for (i
= i
- 1; i
>= 0; i
--)
380 delete_netxbig_led(&leds_data
[i
]);
382 gpio_ext_free(pdata
->gpio_ext
);
386 static int netxbig_led_remove(struct platform_device
*pdev
)
388 struct netxbig_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
389 struct netxbig_led_data
*leds_data
;
392 leds_data
= platform_get_drvdata(pdev
);
394 for (i
= 0; i
< pdata
->num_leds
; i
++)
395 delete_netxbig_led(&leds_data
[i
]);
397 gpio_ext_free(pdata
->gpio_ext
);
402 static struct platform_driver netxbig_led_driver
= {
403 .probe
= netxbig_led_probe
,
404 .remove
= netxbig_led_remove
,
406 .name
= "leds-netxbig",
410 module_platform_driver(netxbig_led_driver
);
412 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
413 MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
414 MODULE_LICENSE("GPL");
415 MODULE_ALIAS("platform:leds-netxbig");