leds-apu2: Add PC Engines APU2 LED driver
[openwrt/openwrt.git] / package / kernel / leds-apu2 / src / leds-apu2.c
1 /*
2 * APU2 LED/GPIO Driver
3 * Copyright (c) 2016 Christian Lamparter <chunkeey (at) googlemail.com>
4 *
5 * Based on gpio-apu2.c - AMD FCH GPIO support for PC-Engines APU-2 board
6 *
7 * Copyright (c) 2015 Carsten Spiess <fli4l at carsten-spiess.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/miscdevice.h>
27 #include <linux/gpio.h>
28 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/ioport.h>
31 #include <linux/platform_device.h>
32 #include <linux/uaccess.h>
33 #include <linux/io.h>
34 #include <linux/version.h>
35 #include <linux/dmi.h>
36 #include <linux/string.h>
37
38 #include <linux/leds.h>
39 #include <linux/input.h>
40 #include <linux/gpio_keys.h>
41
42 #define DEVNAME "leds-apu2"
43
44 #define FCH_ACPI_MMIO_BASE 0xFED80000
45 #define FCH_GPIO_BASE (FCH_ACPI_MMIO_BASE + 0x1500)
46 #define FCH_GPIO_SIZE 0x300
47
48 #define APU_NUM_GPIO 4
49
50 #define GPIO_BIT_DIR 23
51 #define GPIO_BIT_WRITE 22
52 #define GPIO_BIT_READ 16
53
54 /* internal variables */
55 static struct pci_dev *gpio_apu2_pci;
56 static DEFINE_SPINLOCK (gpio_lock);
57
58 /* the watchdog platform device */
59 static struct platform_device *gpio_apu2_platform_device;
60 static struct platform_device *leddev;
61 static struct platform_device *keydev;
62
63 static const struct pci_device_id gpio_apu2_pci_tbl[] ={
64 {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, PCI_ANY_ID, PCI_ANY_ID},
65 { 0, } /* End of list */
66 };
67 MODULE_DEVICE_TABLE (pci, gpio_apu2_pci_tbl);
68
69 /* EGPIO89=GPIO32, AGPIO68=GPIO57, AGPIO69=GPIO58, AGPIO70=GPIO59 */
70 static u8 gpio_offset[APU_NUM_GPIO] = {89, 68, 69, 70};
71
72 static void __iomem *gpio_addr[APU_NUM_GPIO] = {NULL, NULL, NULL, NULL};
73
74 static int gpio_apu2_get_dir (struct gpio_chip *chip, unsigned offset)
75 {
76 u32 val;
77
78 val = ~ioread32 (gpio_addr[offset]);
79
80 return (val >> GPIO_BIT_DIR) & 1;
81 }
82
83 static int gpio_apu2_dir_in (struct gpio_chip *gc, unsigned offset)
84 {
85 u32 val;
86
87 spin_lock_bh (&gpio_lock);
88
89 val = ioread32 (gpio_addr[offset]);
90 val &= ~BIT(GPIO_BIT_DIR);
91 iowrite32 (val, gpio_addr[offset]);
92
93 spin_unlock_bh (&gpio_lock);
94
95 return 0;
96 }
97
98 static int gpio_apu2_dir_out (struct gpio_chip *chip, unsigned offset,
99 int value)
100 {
101 u32 val;
102
103 spin_lock_bh (&gpio_lock);
104
105 val = ioread32 (gpio_addr[offset]);
106 val |= BIT(GPIO_BIT_DIR);
107 iowrite32 (val, gpio_addr[offset]);
108
109 spin_unlock_bh (&gpio_lock);
110
111 return 0;
112 }
113
114 static int gpio_apu2_get_data (struct gpio_chip *chip, unsigned offset)
115 {
116 u32 val;
117
118 val = ioread32 (gpio_addr[offset]);
119
120 return (val >> GPIO_BIT_READ) & 1;
121 }
122
123 static void gpio_apu2_set_data (struct gpio_chip *chip, unsigned offset, int value)
124 {
125 u32 val;
126
127 spin_lock_bh (&gpio_lock);
128
129 val = ioread32 (gpio_addr[offset]);
130
131 if (value)
132 val |= BIT(GPIO_BIT_WRITE);
133 else
134 val &= ~BIT(GPIO_BIT_WRITE);
135
136 iowrite32 (val, gpio_addr[offset]);
137
138 spin_unlock_bh (&gpio_lock);
139 }
140
141 static struct gpio_chip gpio_apu2_chip = {
142 .label = DEVNAME,
143 .owner = THIS_MODULE,
144 .base = -1,
145 .ngpio = APU_NUM_GPIO,
146 .get_direction = gpio_apu2_get_dir,
147 .direction_input = gpio_apu2_dir_in,
148 .direction_output = gpio_apu2_dir_out,
149 .get = gpio_apu2_get_data,
150 .set = gpio_apu2_set_data,
151 };
152
153 /*
154 *
155 */
156 static int gpio_apu2_probe (struct platform_device *dev)
157 {
158 int ret = 0;
159 int i;
160 struct pci_dev *pci_dev = NULL;
161
162 /* Match the PCI device */
163 for_each_pci_dev (pci_dev) {
164 if (pci_match_id (gpio_apu2_pci_tbl, pci_dev) != NULL) {
165 gpio_apu2_pci = pci_dev;
166 break;
167 }
168 }
169
170 if (!gpio_apu2_pci)
171 return -ENODEV;
172
173 pr_info ("%s: PCI Revision ID: 0x%x\n", DEVNAME, gpio_apu2_pci->revision);
174
175 /* Determine type of southbridge chipset */
176 if (gpio_apu2_pci->revision < 0x40) {
177 return -EACCES;
178 }
179
180 /* Request memory region for GPIO's */
181 if (!devm_request_mem_region (&dev->dev, FCH_GPIO_BASE,
182 FCH_GPIO_SIZE, DEVNAME)){
183 pr_err ("%s: request GPIO mem region failed\n", DEVNAME);
184 return -ENXIO;
185 }
186
187 /* Map IO's for GPIO's */
188 for (i = 0; i < APU_NUM_GPIO; i++) {
189 gpio_addr[i] = devm_ioremap (&dev->dev,
190 FCH_GPIO_BASE + (gpio_offset[i] * sizeof (u32)), sizeof (u32));
191 if (!gpio_addr[i]) {
192 pr_err ("%s: map GPIO%d address failed\n", DEVNAME, gpio_offset[i]);
193 return -ENXIO;
194 }
195 }
196
197 gpio_apu2_chip.dev = &dev->dev;
198 ret = gpiochip_add (&gpio_apu2_chip);
199 if (ret) {
200 pr_err ("%s: adding gpiochip failed\n", DEVNAME);
201 }
202
203 return ret;
204 }
205
206 static int gpio_apu2_remove (struct platform_device *dev)
207 {
208 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
209 int ret;
210 ret = gpiochip_remove (&gpio_apu2_chip);
211 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0) */
212 gpiochip_remove (&gpio_apu2_chip);
213 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0) */
214 return 0;
215 }
216
217 static struct platform_driver gpio_apu2_driver = {
218 .probe = gpio_apu2_probe,
219 .remove = gpio_apu2_remove,
220 .driver = {
221 .owner = THIS_MODULE,
222 .name = DEVNAME
223 }
224 };
225
226 static struct gpio_led apu2_leds_gpio[] = {
227 {
228 .name = "apu2:green:power",
229 .gpio = 509,
230 .active_low = 1,
231 },
232 {
233 .name = "apu2:green:led2",
234 .gpio = 510,
235 .active_low = 1,
236 },
237 {
238 .name = "apu2:green:led3",
239 .gpio = 511,
240 .active_low = 1,
241 },
242 };
243
244 static struct gpio_keys_button apu2_gpio_keys[] = {
245 {
246 .desc = "Reset button",
247 .type = EV_KEY,
248 .code = KEY_RESTART,
249 .debounce_interval = 60,
250 .gpio = 508,
251 .active_low = 1,
252 },
253 };
254
255 static void register_gpio_keys_polled(int id, unsigned poll_interval,
256 unsigned nbuttons,
257 struct gpio_keys_button *buttons)
258 {
259 struct gpio_keys_platform_data pdata = { };
260 int err;
261
262 keydev = platform_device_alloc("gpio-keys-polled", id);
263 if (!keydev) {
264 printk(KERN_ERR "Failed to allocate gpio-keys platform device\n");
265 return;
266 }
267
268 pdata.poll_interval = poll_interval;
269 pdata.nbuttons = nbuttons;
270 pdata.buttons = buttons;
271
272 err = platform_device_add_data(keydev, &pdata, sizeof(pdata));
273 if (err) {
274 dev_err(&keydev->dev, "failed to add platform data to key driver (%d)", err);
275 goto err_put_pdev;
276 }
277
278 err = platform_device_add(keydev);
279 if (err) {
280 dev_err(&keydev->dev, "failed to register key platform device (%d)", err);
281 goto err_put_pdev;
282 }
283
284 return;
285
286 err_put_pdev:
287 platform_device_put(keydev);
288 keydev = NULL;
289 }
290
291 static void register_leds_gpio(int id, unsigned num_leds, struct gpio_led *leds)
292 {
293 struct gpio_led_platform_data pdata = { };
294 int err;
295
296 leddev = platform_device_alloc("leds-gpio", id);
297 if (!leddev) {
298 printk(KERN_ERR "Failed to allocate leds-gpio platform device\n");
299 return;
300 }
301
302 pdata.num_leds = num_leds;
303 pdata.leds = leds;
304
305 err = platform_device_add_data(leddev, &pdata, sizeof(pdata));
306 if (err) {
307 dev_err(&leddev->dev, "failed to add platform data to key driver (%d)", err);
308 goto err_put_pdev;
309 }
310
311 err = platform_device_add(leddev);
312 if (err) {
313 dev_err(&leddev->dev, "failed to register key platform device (%d)", err);
314 goto err_put_pdev;
315 }
316
317 return;
318
319 err_put_pdev:
320 platform_device_put(leddev);
321 leddev = NULL;
322 }
323
324 static int __init gpio_apu2_init (void)
325 {
326 int err;
327 const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
328 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
329
330 /* Match the device name/model */
331 if (!board_name || !board_vendor || strcasecmp(board_vendor, "PC Engines") || strcasecmp(board_name, "apu2")) {
332 err = -ENODEV;
333 goto exit;
334 }
335
336 pr_info ("%s: load APU2/LED GPIO driver module\n", DEVNAME);
337
338 err = platform_driver_register (&gpio_apu2_driver);
339 if (err)
340 goto exit;
341
342 gpio_apu2_platform_device = platform_device_register_simple (DEVNAME, -1, NULL, 0);
343 if (IS_ERR(gpio_apu2_platform_device)) {
344 err = PTR_ERR(gpio_apu2_platform_device);
345 goto exit_driver;
346 }
347
348 pr_info ("%s: APU2 GPIO/LED driver module loaded\n", DEVNAME);
349
350 register_leds_gpio(-1, ARRAY_SIZE(apu2_leds_gpio), apu2_leds_gpio);
351 register_gpio_keys_polled(-1, 20, ARRAY_SIZE(apu2_gpio_keys), apu2_gpio_keys);
352 return 0;
353
354 exit_driver:
355 platform_driver_unregister (&gpio_apu2_driver);
356 exit:
357 return err;
358 }
359
360 static void __exit gpio_apu2_exit (void)
361 {
362 platform_device_unregister (gpio_apu2_platform_device);
363 platform_device_unregister (leddev);
364 platform_device_unregister (keydev);
365 platform_driver_unregister (&gpio_apu2_driver);
366 pr_info ("%s: APU2 GPIO/LED driver module unloaded\n", DEVNAME);
367 }
368
369 MODULE_AUTHOR ("Carsten Spiess <fli4l at carsten-spiess.de>");
370 MODULE_DESCRIPTION("GPIO driver for AMD FCH on PC-Engines APU-2");
371 MODULE_LICENSE("GPL");
372
373 module_init (gpio_apu2_init);
374 module_exit (gpio_apu2_exit);