Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awilliam
GitHub Repository: awilliam/linux-vfio
Path: blob/master/drivers/leds/leds-netxbig.c
15109 views
1
/*
2
* leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
3
*
4
* Copyright (C) 2010 LaCie
5
*
6
* Author: Simon Guinot <[email protected]>
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
37
static DEFINE_SPINLOCK(gpio_ext_lock);
38
39
static 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
47
static 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
55
static 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
62
static 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
74
static 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++) {
84
err = gpio_request(gpio_ext->addr[i], "GPIO extension addr");
85
if (err)
86
goto err_free_addr;
87
err = gpio_direction_output(gpio_ext->addr[i], 0);
88
if (err) {
89
gpio_free(gpio_ext->addr[i]);
90
goto err_free_addr;
91
}
92
}
93
/* Configure data GPIOs. */
94
for (i = 0; i < gpio_ext->num_data; i++) {
95
err = gpio_request(gpio_ext->data[i], "GPIO extension data");
96
if (err)
97
goto err_free_data;
98
err = gpio_direction_output(gpio_ext->data[i], 0);
99
if (err) {
100
gpio_free(gpio_ext->data[i]);
101
goto err_free_data;
102
}
103
}
104
/* Configure "enable select" GPIO. */
105
err = gpio_request(gpio_ext->enable, "GPIO extension enable");
106
if (err)
107
goto err_free_data;
108
err = gpio_direction_output(gpio_ext->enable, 0);
109
if (err) {
110
gpio_free(gpio_ext->enable);
111
goto err_free_data;
112
}
113
114
return 0;
115
116
err_free_data:
117
for (i = i - 1; i >= 0; i--)
118
gpio_free(gpio_ext->data[i]);
119
i = gpio_ext->num_addr;
120
err_free_addr:
121
for (i = i - 1; i >= 0; i--)
122
gpio_free(gpio_ext->addr[i]);
123
124
return err;
125
}
126
127
static void __devexit gpio_ext_free(struct netxbig_gpio_ext *gpio_ext)
128
{
129
int i;
130
131
gpio_free(gpio_ext->enable);
132
for (i = gpio_ext->num_addr - 1; i >= 0; i--)
133
gpio_free(gpio_ext->addr[i]);
134
for (i = gpio_ext->num_data - 1; i >= 0; i--)
135
gpio_free(gpio_ext->data[i]);
136
}
137
138
/*
139
* Class LED driver.
140
*/
141
142
struct netxbig_led_data {
143
struct netxbig_gpio_ext *gpio_ext;
144
struct led_classdev cdev;
145
int mode_addr;
146
int *mode_val;
147
int bright_addr;
148
int bright_max;
149
struct netxbig_led_timer *timer;
150
int num_timer;
151
enum netxbig_led_mode mode;
152
int sata;
153
spinlock_t lock;
154
};
155
156
static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
157
unsigned long delay_on,
158
unsigned long delay_off,
159
struct netxbig_led_timer *timer,
160
int num_timer)
161
{
162
int i;
163
164
for (i = 0; i < num_timer; i++) {
165
if (timer[i].delay_on == delay_on &&
166
timer[i].delay_off == delay_off) {
167
*mode = timer[i].mode;
168
return 0;
169
}
170
}
171
return -EINVAL;
172
}
173
174
static int netxbig_led_blink_set(struct led_classdev *led_cdev,
175
unsigned long *delay_on,
176
unsigned long *delay_off)
177
{
178
struct netxbig_led_data *led_dat =
179
container_of(led_cdev, struct netxbig_led_data, cdev);
180
enum netxbig_led_mode mode;
181
int mode_val;
182
int ret;
183
184
/* Look for a LED mode with the requested timer frequency. */
185
ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
186
led_dat->timer, led_dat->num_timer);
187
if (ret < 0)
188
return ret;
189
190
mode_val = led_dat->mode_val[mode];
191
if (mode_val == NETXBIG_LED_INVALID_MODE)
192
return -EINVAL;
193
194
spin_lock_irq(&led_dat->lock);
195
196
gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
197
led_dat->mode = mode;
198
199
spin_unlock_irq(&led_dat->lock);
200
201
return 0;
202
}
203
204
static void netxbig_led_set(struct led_classdev *led_cdev,
205
enum led_brightness value)
206
{
207
struct netxbig_led_data *led_dat =
208
container_of(led_cdev, struct netxbig_led_data, cdev);
209
enum netxbig_led_mode mode;
210
int mode_val, bright_val;
211
int set_brightness = 1;
212
unsigned long flags;
213
214
spin_lock_irqsave(&led_dat->lock, flags);
215
216
if (value == LED_OFF) {
217
mode = NETXBIG_LED_OFF;
218
set_brightness = 0;
219
} else {
220
if (led_dat->sata)
221
mode = NETXBIG_LED_SATA;
222
else if (led_dat->mode == NETXBIG_LED_OFF)
223
mode = NETXBIG_LED_ON;
224
else /* Keep 'timer' mode. */
225
mode = led_dat->mode;
226
}
227
mode_val = led_dat->mode_val[mode];
228
229
gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
230
led_dat->mode = mode;
231
/*
232
* Note that the brightness register is shared between all the
233
* SATA LEDs. So, change the brightness setting for a single
234
* SATA LED will affect all the others.
235
*/
236
if (set_brightness) {
237
bright_val = DIV_ROUND_UP(value * led_dat->bright_max,
238
LED_FULL);
239
gpio_ext_set_value(led_dat->gpio_ext,
240
led_dat->bright_addr, bright_val);
241
}
242
243
spin_unlock_irqrestore(&led_dat->lock, flags);
244
}
245
246
static ssize_t netxbig_led_sata_store(struct device *dev,
247
struct device_attribute *attr,
248
const char *buff, size_t count)
249
{
250
struct led_classdev *led_cdev = dev_get_drvdata(dev);
251
struct netxbig_led_data *led_dat =
252
container_of(led_cdev, struct netxbig_led_data, cdev);
253
unsigned long enable;
254
enum netxbig_led_mode mode;
255
int mode_val;
256
int ret;
257
258
ret = strict_strtoul(buff, 10, &enable);
259
if (ret < 0)
260
return ret;
261
262
enable = !!enable;
263
264
spin_lock_irq(&led_dat->lock);
265
266
if (led_dat->sata == enable) {
267
ret = count;
268
goto exit_unlock;
269
}
270
271
if (led_dat->mode != NETXBIG_LED_ON &&
272
led_dat->mode != NETXBIG_LED_SATA)
273
mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
274
else if (enable)
275
mode = NETXBIG_LED_SATA;
276
else
277
mode = NETXBIG_LED_ON;
278
279
mode_val = led_dat->mode_val[mode];
280
if (mode_val == NETXBIG_LED_INVALID_MODE) {
281
ret = -EINVAL;
282
goto exit_unlock;
283
}
284
285
gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
286
led_dat->mode = mode;
287
led_dat->sata = enable;
288
289
ret = count;
290
291
exit_unlock:
292
spin_unlock_irq(&led_dat->lock);
293
294
return ret;
295
}
296
297
static ssize_t netxbig_led_sata_show(struct device *dev,
298
struct device_attribute *attr, char *buf)
299
{
300
struct led_classdev *led_cdev = dev_get_drvdata(dev);
301
struct netxbig_led_data *led_dat =
302
container_of(led_cdev, struct netxbig_led_data, cdev);
303
304
return sprintf(buf, "%d\n", led_dat->sata);
305
}
306
307
static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
308
309
static void __devexit delete_netxbig_led(struct netxbig_led_data *led_dat)
310
{
311
if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
312
device_remove_file(led_dat->cdev.dev, &dev_attr_sata);
313
led_classdev_unregister(&led_dat->cdev);
314
}
315
316
static int __devinit
317
create_netxbig_led(struct platform_device *pdev,
318
struct netxbig_led_data *led_dat,
319
const struct netxbig_led *template)
320
{
321
struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
322
int ret;
323
324
spin_lock_init(&led_dat->lock);
325
led_dat->gpio_ext = pdata->gpio_ext;
326
led_dat->cdev.name = template->name;
327
led_dat->cdev.default_trigger = template->default_trigger;
328
led_dat->cdev.blink_set = netxbig_led_blink_set;
329
led_dat->cdev.brightness_set = netxbig_led_set;
330
/*
331
* Because the GPIO extension bus don't allow to read registers
332
* value, there is no way to probe the LED initial state.
333
* So, the initial sysfs LED value for the "brightness" and "sata"
334
* attributes are inconsistent.
335
*
336
* Note that the initial LED state can't be reconfigured.
337
* The reason is that the LED behaviour must stay uniform during
338
* the whole boot process (bootloader+linux).
339
*/
340
led_dat->sata = 0;
341
led_dat->cdev.brightness = LED_OFF;
342
led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
343
led_dat->mode_addr = template->mode_addr;
344
led_dat->mode_val = template->mode_val;
345
led_dat->bright_addr = template->bright_addr;
346
led_dat->bright_max = (1 << pdata->gpio_ext->num_data) - 1;
347
led_dat->timer = pdata->timer;
348
led_dat->num_timer = pdata->num_timer;
349
350
ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
351
if (ret < 0)
352
return ret;
353
354
/*
355
* If available, expose the SATA activity blink capability through
356
* a "sata" sysfs attribute.
357
*/
358
if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE) {
359
ret = device_create_file(led_dat->cdev.dev, &dev_attr_sata);
360
if (ret)
361
led_classdev_unregister(&led_dat->cdev);
362
}
363
364
return ret;
365
}
366
367
static int __devinit netxbig_led_probe(struct platform_device *pdev)
368
{
369
struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
370
struct netxbig_led_data *leds_data;
371
int i;
372
int ret;
373
374
if (!pdata)
375
return -EINVAL;
376
377
leds_data = kzalloc(sizeof(struct netxbig_led_data) * pdata->num_leds,
378
GFP_KERNEL);
379
if (!leds_data)
380
return -ENOMEM;
381
382
ret = gpio_ext_init(pdata->gpio_ext);
383
if (ret < 0)
384
goto err_free_data;
385
386
for (i = 0; i < pdata->num_leds; i++) {
387
ret = create_netxbig_led(pdev, &leds_data[i], &pdata->leds[i]);
388
if (ret < 0)
389
goto err_free_leds;
390
}
391
392
platform_set_drvdata(pdev, leds_data);
393
394
return 0;
395
396
err_free_leds:
397
for (i = i - 1; i >= 0; i--)
398
delete_netxbig_led(&leds_data[i]);
399
400
gpio_ext_free(pdata->gpio_ext);
401
err_free_data:
402
kfree(leds_data);
403
404
return ret;
405
}
406
407
static int __devexit netxbig_led_remove(struct platform_device *pdev)
408
{
409
struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
410
struct netxbig_led_data *leds_data;
411
int i;
412
413
leds_data = platform_get_drvdata(pdev);
414
415
for (i = 0; i < pdata->num_leds; i++)
416
delete_netxbig_led(&leds_data[i]);
417
418
gpio_ext_free(pdata->gpio_ext);
419
kfree(leds_data);
420
421
return 0;
422
}
423
424
static struct platform_driver netxbig_led_driver = {
425
.probe = netxbig_led_probe,
426
.remove = __devexit_p(netxbig_led_remove),
427
.driver = {
428
.name = "leds-netxbig",
429
.owner = THIS_MODULE,
430
},
431
};
432
MODULE_ALIAS("platform:leds-netxbig");
433
434
static int __init netxbig_led_init(void)
435
{
436
return platform_driver_register(&netxbig_led_driver);
437
}
438
439
static void __exit netxbig_led_exit(void)
440
{
441
platform_driver_unregister(&netxbig_led_driver);
442
}
443
444
module_init(netxbig_led_init);
445
module_exit(netxbig_led_exit);
446
447
MODULE_AUTHOR("Simon Guinot <[email protected]>");
448
MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
449
MODULE_LICENSE("GPL");
450
451