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