blob: 73973fdbd8be738ac57c23c6fb728b309a0a13d3 [file] [log] [blame]
Simon Guinot24467832010-10-07 16:38:49 +02001/*
2 * leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
3 *
4 * Copyright (C) 2010 LaCie
5 *
6 * Author: Simon Guinot <sguinot@lacie.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/irq.h>
26#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/platform_device.h>
29#include <linux/gpio.h>
30#include <linux/leds.h>
31#include <mach/leds-netxbig.h>
32
33/*
34 * GPIO extension bus.
35 */
36
37static DEFINE_SPINLOCK(gpio_ext_lock);
38
39static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
40{
41 int pin;
42
43 for (pin = 0; pin < gpio_ext->num_addr; pin++)
44 gpio_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
45}
46
47static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
48{
49 int pin;
50
51 for (pin = 0; pin < gpio_ext->num_data; pin++)
52 gpio_set_value(gpio_ext->data[pin], (data >> pin) & 1);
53}
54
55static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
56{
57 /* Enable select is done on the raising edge. */
58 gpio_set_value(gpio_ext->enable, 0);
59 gpio_set_value(gpio_ext->enable, 1);
60}
61
62static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
63 int addr, int value)
64{
65 unsigned long flags;
66
67 spin_lock_irqsave(&gpio_ext_lock, flags);
68 gpio_ext_set_addr(gpio_ext, addr);
69 gpio_ext_set_data(gpio_ext, value);
70 gpio_ext_enable_select(gpio_ext);
71 spin_unlock_irqrestore(&gpio_ext_lock, flags);
72}
73
74static int __devinit gpio_ext_init(struct netxbig_gpio_ext *gpio_ext)
75{
76 int err;
77 int i;
78
79 if (unlikely(!gpio_ext))
80 return -EINVAL;
81
82 /* Configure address GPIOs. */
83 for (i = 0; i < gpio_ext->num_addr; i++) {
Axel Linb96a5732012-01-10 15:09:37 -080084 err = gpio_request_one(gpio_ext->addr[i], GPIOF_OUT_INIT_LOW,
85 "GPIO extension addr");
Simon Guinot24467832010-10-07 16:38:49 +020086 if (err)
87 goto err_free_addr;
Simon Guinot24467832010-10-07 16:38:49 +020088 }
89 /* Configure data GPIOs. */
90 for (i = 0; i < gpio_ext->num_data; i++) {
Axel Linb96a5732012-01-10 15:09:37 -080091 err = gpio_request_one(gpio_ext->data[i], GPIOF_OUT_INIT_LOW,
92 "GPIO extension data");
Simon Guinot24467832010-10-07 16:38:49 +020093 if (err)
94 goto err_free_data;
Simon Guinot24467832010-10-07 16:38:49 +020095 }
96 /* Configure "enable select" GPIO. */
Axel Linb96a5732012-01-10 15:09:37 -080097 err = gpio_request_one(gpio_ext->enable, GPIOF_OUT_INIT_LOW,
98 "GPIO extension enable");
Simon Guinot24467832010-10-07 16:38:49 +020099 if (err)
100 goto err_free_data;
Simon Guinot24467832010-10-07 16:38:49 +0200101
102 return 0;
103
104err_free_data:
105 for (i = i - 1; i >= 0; i--)
106 gpio_free(gpio_ext->data[i]);
107 i = gpio_ext->num_addr;
108err_free_addr:
109 for (i = i - 1; i >= 0; i--)
110 gpio_free(gpio_ext->addr[i]);
111
112 return err;
113}
114
Arnd Bergmannb8cd7422012-05-10 13:01:46 -0700115static void gpio_ext_free(struct netxbig_gpio_ext *gpio_ext)
Simon Guinot24467832010-10-07 16:38:49 +0200116{
117 int i;
118
119 gpio_free(gpio_ext->enable);
120 for (i = gpio_ext->num_addr - 1; i >= 0; i--)
121 gpio_free(gpio_ext->addr[i]);
122 for (i = gpio_ext->num_data - 1; i >= 0; i--)
123 gpio_free(gpio_ext->data[i]);
124}
125
126/*
127 * Class LED driver.
128 */
129
130struct netxbig_led_data {
131 struct netxbig_gpio_ext *gpio_ext;
132 struct led_classdev cdev;
133 int mode_addr;
134 int *mode_val;
135 int bright_addr;
136 int bright_max;
137 struct netxbig_led_timer *timer;
138 int num_timer;
139 enum netxbig_led_mode mode;
140 int sata;
141 spinlock_t lock;
142};
143
144static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
145 unsigned long delay_on,
146 unsigned long delay_off,
147 struct netxbig_led_timer *timer,
148 int num_timer)
149{
150 int i;
151
152 for (i = 0; i < num_timer; i++) {
153 if (timer[i].delay_on == delay_on &&
154 timer[i].delay_off == delay_off) {
155 *mode = timer[i].mode;
156 return 0;
157 }
158 }
159 return -EINVAL;
160}
161
162static int netxbig_led_blink_set(struct led_classdev *led_cdev,
163 unsigned long *delay_on,
164 unsigned long *delay_off)
165{
166 struct netxbig_led_data *led_dat =
167 container_of(led_cdev, struct netxbig_led_data, cdev);
168 enum netxbig_led_mode mode;
169 int mode_val;
170 int ret;
171
172 /* Look for a LED mode with the requested timer frequency. */
173 ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
174 led_dat->timer, led_dat->num_timer);
175 if (ret < 0)
176 return ret;
177
178 mode_val = led_dat->mode_val[mode];
179 if (mode_val == NETXBIG_LED_INVALID_MODE)
180 return -EINVAL;
181
182 spin_lock_irq(&led_dat->lock);
183
184 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
185 led_dat->mode = mode;
186
187 spin_unlock_irq(&led_dat->lock);
188
189 return 0;
190}
191
192static void netxbig_led_set(struct led_classdev *led_cdev,
193 enum led_brightness value)
194{
195 struct netxbig_led_data *led_dat =
196 container_of(led_cdev, struct netxbig_led_data, cdev);
197 enum netxbig_led_mode mode;
198 int mode_val, bright_val;
199 int set_brightness = 1;
200 unsigned long flags;
201
202 spin_lock_irqsave(&led_dat->lock, flags);
203
204 if (value == LED_OFF) {
205 mode = NETXBIG_LED_OFF;
206 set_brightness = 0;
207 } else {
208 if (led_dat->sata)
209 mode = NETXBIG_LED_SATA;
210 else if (led_dat->mode == NETXBIG_LED_OFF)
211 mode = NETXBIG_LED_ON;
212 else /* Keep 'timer' mode. */
213 mode = led_dat->mode;
214 }
215 mode_val = led_dat->mode_val[mode];
216
217 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
218 led_dat->mode = mode;
219 /*
220 * Note that the brightness register is shared between all the
221 * SATA LEDs. So, change the brightness setting for a single
222 * SATA LED will affect all the others.
223 */
224 if (set_brightness) {
225 bright_val = DIV_ROUND_UP(value * led_dat->bright_max,
226 LED_FULL);
227 gpio_ext_set_value(led_dat->gpio_ext,
228 led_dat->bright_addr, bright_val);
229 }
230
231 spin_unlock_irqrestore(&led_dat->lock, flags);
232}
233
234static ssize_t netxbig_led_sata_store(struct device *dev,
235 struct device_attribute *attr,
236 const char *buff, size_t count)
237{
238 struct led_classdev *led_cdev = dev_get_drvdata(dev);
239 struct netxbig_led_data *led_dat =
240 container_of(led_cdev, struct netxbig_led_data, cdev);
241 unsigned long enable;
242 enum netxbig_led_mode mode;
243 int mode_val;
244 int ret;
245
246 ret = strict_strtoul(buff, 10, &enable);
247 if (ret < 0)
248 return ret;
249
250 enable = !!enable;
251
252 spin_lock_irq(&led_dat->lock);
253
254 if (led_dat->sata == enable) {
255 ret = count;
256 goto exit_unlock;
257 }
258
259 if (led_dat->mode != NETXBIG_LED_ON &&
260 led_dat->mode != NETXBIG_LED_SATA)
261 mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
262 else if (enable)
263 mode = NETXBIG_LED_SATA;
264 else
265 mode = NETXBIG_LED_ON;
266
267 mode_val = led_dat->mode_val[mode];
268 if (mode_val == NETXBIG_LED_INVALID_MODE) {
269 ret = -EINVAL;
270 goto exit_unlock;
271 }
272
273 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
274 led_dat->mode = mode;
275 led_dat->sata = enable;
276
277 ret = count;
278
279exit_unlock:
280 spin_unlock_irq(&led_dat->lock);
281
282 return ret;
283}
284
285static ssize_t netxbig_led_sata_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 struct led_classdev *led_cdev = dev_get_drvdata(dev);
289 struct netxbig_led_data *led_dat =
290 container_of(led_cdev, struct netxbig_led_data, cdev);
291
292 return sprintf(buf, "%d\n", led_dat->sata);
293}
294
295static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
296
Arnd Bergmannb8cd7422012-05-10 13:01:46 -0700297static void delete_netxbig_led(struct netxbig_led_data *led_dat)
Simon Guinot24467832010-10-07 16:38:49 +0200298{
299 if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
300 device_remove_file(led_dat->cdev.dev, &dev_attr_sata);
301 led_classdev_unregister(&led_dat->cdev);
302}
303
304static int __devinit
305create_netxbig_led(struct platform_device *pdev,
306 struct netxbig_led_data *led_dat,
307 const struct netxbig_led *template)
308{
309 struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
310 int ret;
311
312 spin_lock_init(&led_dat->lock);
313 led_dat->gpio_ext = pdata->gpio_ext;
314 led_dat->cdev.name = template->name;
315 led_dat->cdev.default_trigger = template->default_trigger;
316 led_dat->cdev.blink_set = netxbig_led_blink_set;
317 led_dat->cdev.brightness_set = netxbig_led_set;
318 /*
319 * Because the GPIO extension bus don't allow to read registers
320 * value, there is no way to probe the LED initial state.
321 * So, the initial sysfs LED value for the "brightness" and "sata"
322 * attributes are inconsistent.
323 *
324 * Note that the initial LED state can't be reconfigured.
325 * The reason is that the LED behaviour must stay uniform during
326 * the whole boot process (bootloader+linux).
327 */
328 led_dat->sata = 0;
329 led_dat->cdev.brightness = LED_OFF;
330 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
331 led_dat->mode_addr = template->mode_addr;
332 led_dat->mode_val = template->mode_val;
333 led_dat->bright_addr = template->bright_addr;
334 led_dat->bright_max = (1 << pdata->gpio_ext->num_data) - 1;
335 led_dat->timer = pdata->timer;
336 led_dat->num_timer = pdata->num_timer;
337
338 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
339 if (ret < 0)
340 return ret;
341
342 /*
343 * If available, expose the SATA activity blink capability through
344 * a "sata" sysfs attribute.
345 */
346 if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE) {
347 ret = device_create_file(led_dat->cdev.dev, &dev_attr_sata);
348 if (ret)
349 led_classdev_unregister(&led_dat->cdev);
350 }
351
352 return ret;
353}
354
355static int __devinit netxbig_led_probe(struct platform_device *pdev)
356{
357 struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
358 struct netxbig_led_data *leds_data;
359 int i;
360 int ret;
361
362 if (!pdata)
363 return -EINVAL;
364
365 leds_data = kzalloc(sizeof(struct netxbig_led_data) * pdata->num_leds,
366 GFP_KERNEL);
367 if (!leds_data)
368 return -ENOMEM;
369
370 ret = gpio_ext_init(pdata->gpio_ext);
371 if (ret < 0)
372 goto err_free_data;
373
374 for (i = 0; i < pdata->num_leds; i++) {
375 ret = create_netxbig_led(pdev, &leds_data[i], &pdata->leds[i]);
376 if (ret < 0)
377 goto err_free_leds;
378 }
379
380 platform_set_drvdata(pdev, leds_data);
381
382 return 0;
383
384err_free_leds:
385 for (i = i - 1; i >= 0; i--)
386 delete_netxbig_led(&leds_data[i]);
387
388 gpio_ext_free(pdata->gpio_ext);
389err_free_data:
390 kfree(leds_data);
391
392 return ret;
393}
394
395static int __devexit netxbig_led_remove(struct platform_device *pdev)
396{
397 struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
398 struct netxbig_led_data *leds_data;
399 int i;
400
401 leds_data = platform_get_drvdata(pdev);
402
403 for (i = 0; i < pdata->num_leds; i++)
404 delete_netxbig_led(&leds_data[i]);
405
406 gpio_ext_free(pdata->gpio_ext);
407 kfree(leds_data);
408
409 return 0;
410}
411
412static struct platform_driver netxbig_led_driver = {
413 .probe = netxbig_led_probe,
414 .remove = __devexit_p(netxbig_led_remove),
415 .driver = {
416 .name = "leds-netxbig",
417 .owner = THIS_MODULE,
418 },
419};
Simon Guinot24467832010-10-07 16:38:49 +0200420
Axel Lin892a8842012-01-10 15:09:24 -0800421module_platform_driver(netxbig_led_driver);
Simon Guinot24467832010-10-07 16:38:49 +0200422
423MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
424MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
425MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800426MODULE_ALIAS("platform:leds-netxbig");