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