]>
Commit | Line | Data |
---|---|---|
3faee942 AM |
1 | /* |
2 | * drivers/leds/leds-apu.c | |
3 | * Copyright (C) 2017 Alan Mizrahi, alan at mizrahi dot com dot ve | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. Neither the names of the copyright holders nor the names of its | |
14 | * contributors may be used to endorse or promote products derived from | |
15 | * this software without specific prior written permission. | |
16 | * | |
17 | * Alternatively, this software may be distributed under the terms of the | |
18 | * GNU General Public License ("GPL") version 2 as published by the Free | |
19 | * Software Foundation. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
31 | * POSSIBILITY OF SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #include <linux/dmi.h> | |
35 | #include <linux/err.h> | |
36 | #include <linux/init.h> | |
37 | #include <linux/io.h> | |
38 | #include <linux/kernel.h> | |
39 | #include <linux/leds.h> | |
40 | #include <linux/module.h> | |
41 | #include <linux/platform_device.h> | |
42 | ||
43 | #define APU1_FCH_ACPI_MMIO_BASE 0xFED80000 | |
44 | #define APU1_FCH_GPIO_BASE (APU1_FCH_ACPI_MMIO_BASE + 0x01BD) | |
45 | #define APU1_LEDON 0x08 | |
46 | #define APU1_LEDOFF 0xC8 | |
47 | #define APU1_NUM_GPIO 3 | |
48 | #define APU1_IOSIZE sizeof(u8) | |
49 | ||
3faee942 AM |
50 | /* LED access parameters */ |
51 | struct apu_param { | |
52 | void __iomem *addr; /* for ioread/iowrite */ | |
53 | }; | |
54 | ||
55 | /* LED private data */ | |
56 | struct apu_led_priv { | |
57 | struct led_classdev cdev; | |
58 | struct apu_param param; | |
59 | }; | |
60 | #define cdev_to_priv(c) container_of(c, struct apu_led_priv, cdev) | |
61 | ||
62 | /* LED profile */ | |
63 | struct apu_led_profile { | |
64 | const char *name; | |
65 | enum led_brightness brightness; | |
66 | unsigned long offset; /* for devm_ioremap */ | |
67 | }; | |
68 | ||
3faee942 AM |
69 | struct apu_led_pdata { |
70 | struct platform_device *pdev; | |
71 | struct apu_led_priv *pled; | |
72 | const struct apu_led_profile *profile; | |
3faee942 | 73 | int num_led_instances; |
3faee942 AM |
74 | spinlock_t lock; |
75 | }; | |
76 | ||
77 | static struct apu_led_pdata *apu_led; | |
78 | ||
79 | static const struct apu_led_profile apu1_led_profile[] = { | |
80 | { "apu:green:1", LED_ON, APU1_FCH_GPIO_BASE + 0 * APU1_IOSIZE }, | |
81 | { "apu:green:2", LED_OFF, APU1_FCH_GPIO_BASE + 1 * APU1_IOSIZE }, | |
82 | { "apu:green:3", LED_OFF, APU1_FCH_GPIO_BASE + 2 * APU1_IOSIZE }, | |
83 | }; | |
84 | ||
3faee942 AM |
85 | static const struct dmi_system_id apu_led_dmi_table[] __initconst = { |
86 | { | |
87 | .ident = "apu", | |
88 | .matches = { | |
89 | DMI_MATCH(DMI_SYS_VENDOR, "PC Engines"), | |
90 | DMI_MATCH(DMI_PRODUCT_NAME, "APU") | |
91 | } | |
92 | }, | |
3faee942 AM |
93 | {} |
94 | }; | |
95 | MODULE_DEVICE_TABLE(dmi, apu_led_dmi_table); | |
96 | ||
97 | static void apu1_led_brightness_set(struct led_classdev *led, enum led_brightness value) | |
98 | { | |
99 | struct apu_led_priv *pled = cdev_to_priv(led); | |
100 | ||
101 | spin_lock(&apu_led->lock); | |
102 | iowrite8(value ? APU1_LEDON : APU1_LEDOFF, pled->param.addr); | |
103 | spin_unlock(&apu_led->lock); | |
104 | } | |
105 | ||
3faee942 AM |
106 | static int apu_led_config(struct device *dev, struct apu_led_pdata *apuld) |
107 | { | |
108 | int i; | |
109 | int err; | |
110 | ||
a86854d0 KC |
111 | apu_led->pled = devm_kcalloc(dev, |
112 | apu_led->num_led_instances, sizeof(struct apu_led_priv), | |
3faee942 AM |
113 | GFP_KERNEL); |
114 | ||
115 | if (!apu_led->pled) | |
116 | return -ENOMEM; | |
117 | ||
118 | for (i = 0; i < apu_led->num_led_instances; i++) { | |
119 | struct apu_led_priv *pled = &apu_led->pled[i]; | |
120 | struct led_classdev *led_cdev = &pled->cdev; | |
121 | ||
122 | led_cdev->name = apu_led->profile[i].name; | |
123 | led_cdev->brightness = apu_led->profile[i].brightness; | |
124 | led_cdev->max_brightness = 1; | |
125 | led_cdev->flags = LED_CORE_SUSPENDRESUME; | |
da97735c | 126 | led_cdev->brightness_set = apu1_led_brightness_set; |
3faee942 AM |
127 | |
128 | pled->param.addr = devm_ioremap(dev, | |
08e83826 | 129 | apu_led->profile[i].offset, APU1_IOSIZE); |
3faee942 AM |
130 | if (!pled->param.addr) { |
131 | err = -ENOMEM; | |
132 | goto error; | |
133 | } | |
134 | ||
135 | err = led_classdev_register(dev, led_cdev); | |
136 | if (err) | |
137 | goto error; | |
138 | ||
da97735c | 139 | apu1_led_brightness_set(led_cdev, apu_led->profile[i].brightness); |
3faee942 AM |
140 | } |
141 | ||
142 | return 0; | |
143 | ||
144 | error: | |
145 | while (i-- > 0) | |
146 | led_classdev_unregister(&apu_led->pled[i].cdev); | |
147 | ||
148 | return err; | |
149 | } | |
150 | ||
151 | static int __init apu_led_probe(struct platform_device *pdev) | |
152 | { | |
153 | apu_led = devm_kzalloc(&pdev->dev, sizeof(*apu_led), GFP_KERNEL); | |
154 | ||
155 | if (!apu_led) | |
156 | return -ENOMEM; | |
157 | ||
158 | apu_led->pdev = pdev; | |
159 | ||
0344e616 | 160 | apu_led->profile = apu1_led_profile; |
0344e616 | 161 | apu_led->num_led_instances = ARRAY_SIZE(apu1_led_profile); |
3faee942 AM |
162 | |
163 | spin_lock_init(&apu_led->lock); | |
164 | return apu_led_config(&pdev->dev, apu_led); | |
165 | } | |
166 | ||
167 | static struct platform_driver apu_led_driver = { | |
168 | .driver = { | |
169 | .name = KBUILD_MODNAME, | |
170 | }, | |
171 | }; | |
172 | ||
173 | static int __init apu_led_init(void) | |
174 | { | |
175 | struct platform_device *pdev; | |
176 | int err; | |
177 | ||
178 | if (!dmi_match(DMI_SYS_VENDOR, "PC Engines")) { | |
179 | pr_err("No PC Engines board detected\n"); | |
180 | return -ENODEV; | |
181 | } | |
0344e616 | 182 | if (!(dmi_match(DMI_PRODUCT_NAME, "APU"))) { |
3faee942 AM |
183 | pr_err("Unknown PC Engines board: %s\n", |
184 | dmi_get_system_info(DMI_PRODUCT_NAME)); | |
185 | return -ENODEV; | |
186 | } | |
187 | ||
188 | pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0); | |
189 | if (IS_ERR(pdev)) { | |
190 | pr_err("Device allocation failed\n"); | |
191 | return PTR_ERR(pdev); | |
192 | } | |
193 | ||
194 | err = platform_driver_probe(&apu_led_driver, apu_led_probe); | |
195 | if (err) { | |
196 | pr_err("Probe platform driver failed\n"); | |
197 | platform_device_unregister(pdev); | |
198 | } | |
199 | ||
200 | return err; | |
201 | } | |
202 | ||
203 | static void __exit apu_led_exit(void) | |
204 | { | |
205 | int i; | |
206 | ||
207 | for (i = 0; i < apu_led->num_led_instances; i++) | |
208 | led_classdev_unregister(&apu_led->pled[i].cdev); | |
209 | ||
210 | platform_device_unregister(apu_led->pdev); | |
211 | platform_driver_unregister(&apu_led_driver); | |
212 | } | |
213 | ||
214 | module_init(apu_led_init); | |
215 | module_exit(apu_led_exit); | |
216 | ||
217 | MODULE_AUTHOR("Alan Mizrahi"); | |
0344e616 | 218 | MODULE_DESCRIPTION("PC Engines APU1 front LED driver"); |
3faee942 AM |
219 | MODULE_LICENSE("GPL v2"); |
220 | MODULE_ALIAS("platform:leds_apu"); |