ar71xx: add LED driver for the RB750
[openwrt/svn-archive/archive.git] / target / linux / ar71xx / files / drivers / leds / leds-rb750.c
1 /*
2 * LED driver for the RouterBOARD 750
3 *
4 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/leds.h>
15
16 #include <asm/mach-ar71xx/mach-rb750.h>
17
18 #define DRV_NAME "leds-rb750"
19
20 struct rb750_led_dev {
21 struct led_classdev cdev;
22 u32 mask;
23 int active_low;
24 };
25
26 struct rb750_led_drvdata {
27 struct rb750_led_dev *led_devs;
28 int num_leds;
29 };
30
31 static inline struct rb750_led_dev *to_rbled(struct led_classdev *led_cdev)
32 {
33 return (struct rb750_led_dev *)container_of(led_cdev,
34 struct rb750_led_dev, cdev);
35 }
36
37 static void rb750_led_brightness_set(struct led_classdev *led_cdev,
38 enum led_brightness value)
39 {
40 struct rb750_led_dev *rbled = to_rbled(led_cdev);
41 int level;
42
43 level = (value == LED_OFF) ? 0 : 1;
44 level ^= rbled->active_low;
45
46 if (level)
47 rb750_latch_change(0, rbled->mask);
48 else
49 rb750_latch_change(rbled->mask, 0);
50 }
51
52 static int __devinit rb750_led_probe(struct platform_device *pdev)
53 {
54 struct rb750_led_platform_data *pdata;
55 struct rb750_led_drvdata *drvdata;
56 int ret = 0;
57 int i;
58
59 pdata = pdev->dev.platform_data;
60 if (!pdata)
61 return -EINVAL;
62
63 drvdata = kzalloc(sizeof(struct rb750_led_drvdata) +
64 sizeof(struct rb750_led_dev) * pdata->num_leds,
65 GFP_KERNEL);
66 if (!drvdata)
67 return -ENOMEM;
68
69 drvdata->num_leds = pdata->num_leds;
70 drvdata->led_devs = (struct rb750_led_dev *) &drvdata[1];
71
72 for (i = 0; i < drvdata->num_leds; i++) {
73 struct rb750_led_dev *rbled = &drvdata->led_devs[i];
74 struct rb750_led_data *led_data = &pdata->leds[i];
75
76 rbled->cdev.name = led_data->name;
77 rbled->cdev.default_trigger = led_data->default_trigger;
78 rbled->cdev.brightness_set = rb750_led_brightness_set;
79 rbled->cdev.brightness = LED_OFF;
80
81 rbled->mask = led_data->mask;
82 rbled->active_low = !!led_data->active_low;
83
84 ret = led_classdev_register(&pdev->dev, &rbled->cdev);
85 if (ret)
86 goto err;
87 }
88
89 platform_set_drvdata(pdev, drvdata);
90 return 0;
91
92 err:
93 for (i = i - 1; i >= 0; i--)
94 led_classdev_unregister(&drvdata->led_devs[i].cdev);
95
96 kfree(drvdata);
97 return ret;
98 }
99
100 static int __devexit rb750_led_remove(struct platform_device *pdev)
101 {
102 struct rb750_led_drvdata *drvdata;
103 int i;
104
105 drvdata = platform_get_drvdata(pdev);
106 for (i = 0; i < drvdata->num_leds; i++)
107 led_classdev_unregister(&drvdata->led_devs[i].cdev);
108
109 kfree(drvdata);
110 return 0;
111 }
112
113 static struct platform_driver rb750_led_driver = {
114 .probe = rb750_led_probe,
115 .remove = __devexit_p(rb750_led_remove),
116 .driver = {
117 .name = DRV_NAME,
118 .owner = THIS_MODULE,
119 },
120 };
121
122 MODULE_ALIAS("platform:leds-rb750");
123
124 static int __init rb750_led_init(void)
125 {
126 return platform_driver_register(&rb750_led_driver);
127 }
128
129 static void __exit rb750_led_exit(void)
130 {
131 platform_driver_unregister(&rb750_led_driver);
132 }
133
134 module_init(rb750_led_init);
135 module_exit(rb750_led_exit);
136
137 MODULE_DESCRIPTION(DRV_NAME);
138 MODULE_DESCRIPTION("LED driver for the RouterBOARD 750");
139 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
140 MODULE_LICENSE("GPL v2");