3 * Copyright (c) 2016 Christian Lamparter <chunkeey (at) googlemail.com>
5 * Based on gpio-apu2.c - AMD FCH GPIO support for PC-Engines APU-2 board
7 * Copyright (c) 2015 Carsten Spiess <fli4l at carsten-spiess.de>
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.
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.
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.
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>
34 #include <linux/version.h>
35 #include <linux/dmi.h>
36 #include <linux/string.h>
38 #include <linux/leds.h>
39 #include <linux/input.h>
40 #include <linux/gpio_keys.h>
42 #define DEVNAME "leds-apu2"
44 #define FCH_ACPI_MMIO_BASE 0xFED80000
45 #define FCH_GPIO_BASE (FCH_ACPI_MMIO_BASE + 0x1500)
46 #define FCH_GPIO_SIZE 0x300
48 #define APU_NUM_GPIO 4
50 #define GPIO_BIT_DIR 23
51 #define GPIO_BIT_WRITE 22
52 #define GPIO_BIT_READ 16
54 /* internal variables */
55 static struct pci_dev *gpio_apu2_pci;
56 static DEFINE_SPINLOCK (gpio_lock);
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;
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 */
67 MODULE_DEVICE_TABLE (pci, gpio_apu2_pci_tbl);
69 /* EGPIO89=GPIO32, AGPIO68=GPIO57, AGPIO69=GPIO58, AGPIO70=GPIO59 */
70 static u8 gpio_offset[APU_NUM_GPIO] = {89, 68, 69, 70};
72 static void __iomem *gpio_addr[APU_NUM_GPIO] = {NULL, NULL, NULL, NULL};
74 static int gpio_apu2_get_dir (struct gpio_chip *chip, unsigned offset)
78 val = ~ioread32 (gpio_addr[offset]);
80 return (val >> GPIO_BIT_DIR) & 1;
83 static int gpio_apu2_dir_in (struct gpio_chip *gc, unsigned offset)
87 spin_lock_bh (&gpio_lock);
89 val = ioread32 (gpio_addr[offset]);
90 val &= ~BIT(GPIO_BIT_DIR);
91 iowrite32 (val, gpio_addr[offset]);
93 spin_unlock_bh (&gpio_lock);
98 static int gpio_apu2_dir_out (struct gpio_chip *chip, unsigned offset,
103 spin_lock_bh (&gpio_lock);
105 val = ioread32 (gpio_addr[offset]);
106 val |= BIT(GPIO_BIT_DIR);
107 iowrite32 (val, gpio_addr[offset]);
109 spin_unlock_bh (&gpio_lock);
114 static int gpio_apu2_get_data (struct gpio_chip *chip, unsigned offset)
118 val = ioread32 (gpio_addr[offset]);
120 return (val >> GPIO_BIT_READ) & 1;
123 static void gpio_apu2_set_data (struct gpio_chip *chip, unsigned offset, int value)
127 spin_lock_bh (&gpio_lock);
129 val = ioread32 (gpio_addr[offset]);
132 val |= BIT(GPIO_BIT_WRITE);
134 val &= ~BIT(GPIO_BIT_WRITE);
136 iowrite32 (val, gpio_addr[offset]);
138 spin_unlock_bh (&gpio_lock);
141 static struct gpio_chip gpio_apu2_chip = {
143 .owner = THIS_MODULE,
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,
156 static int gpio_apu2_probe (struct platform_device *dev)
160 struct pci_dev *pci_dev = NULL;
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;
173 pr_info ("%s: PCI Revision ID: 0x%x\n", DEVNAME, gpio_apu2_pci->revision);
175 /* Determine type of southbridge chipset */
176 if (gpio_apu2_pci->revision < 0x40) {
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);
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));
192 pr_err ("%s: map GPIO%d address failed\n", DEVNAME, gpio_offset[i]);
197 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
198 gpio_apu2_chip.dev = &dev->dev;
200 gpio_apu2_chip.parent = &dev->dev;
202 ret = gpiochip_add (&gpio_apu2_chip);
204 pr_err ("%s: adding gpiochip failed\n", DEVNAME);
210 static int gpio_apu2_remove (struct platform_device *dev)
212 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
214 ret = gpiochip_remove (&gpio_apu2_chip);
215 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0) */
216 gpiochip_remove (&gpio_apu2_chip);
217 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0) */
221 static struct platform_driver gpio_apu2_driver = {
222 .probe = gpio_apu2_probe,
223 .remove = gpio_apu2_remove,
225 .owner = THIS_MODULE,
230 static struct gpio_led apu2_leds_gpio[] = {
232 .name = "apu2:green:power",
237 .name = "apu2:green:led2",
242 .name = "apu2:green:led3",
248 static struct gpio_keys_button apu2_gpio_keys[] = {
250 .desc = "Reset button",
253 .debounce_interval = 60,
259 static void register_gpio_keys_polled(int id, unsigned poll_interval,
261 struct gpio_keys_button *buttons)
263 struct gpio_keys_platform_data pdata = { };
266 keydev = platform_device_alloc("gpio-keys-polled", id);
268 printk(KERN_ERR "Failed to allocate gpio-keys platform device\n");
272 pdata.poll_interval = poll_interval;
273 pdata.nbuttons = nbuttons;
274 pdata.buttons = buttons;
276 err = platform_device_add_data(keydev, &pdata, sizeof(pdata));
278 dev_err(&keydev->dev, "failed to add platform data to key driver (%d)", err);
282 err = platform_device_add(keydev);
284 dev_err(&keydev->dev, "failed to register key platform device (%d)", err);
291 platform_device_put(keydev);
295 static void register_leds_gpio(int id, unsigned num_leds, struct gpio_led *leds)
297 struct gpio_led_platform_data pdata = { };
300 leddev = platform_device_alloc("leds-gpio", id);
302 printk(KERN_ERR "Failed to allocate leds-gpio platform device\n");
306 pdata.num_leds = num_leds;
309 err = platform_device_add_data(leddev, &pdata, sizeof(pdata));
311 dev_err(&leddev->dev, "failed to add platform data to key driver (%d)", err);
315 err = platform_device_add(leddev);
317 dev_err(&leddev->dev, "failed to register key platform device (%d)", err);
324 platform_device_put(leddev);
328 static int __init gpio_apu2_init (void)
331 const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
332 const char *board_name = dmi_get_system_info(DMI_BOARD_NAME);
334 /* Match the device name/model */
337 || strcasecmp(board_vendor, "PC Engines") \
338 || (strcasecmp(board_name, "apu2") \
339 && strcasecmp(board_name, "apu3") \
340 && strcasecmp(board_name, "PC Engines apu2") \
341 && strcasecmp(board_name, "PC Engines apu3"))) {
346 pr_info ("%s: load APU2/LED GPIO driver module\n", DEVNAME);
348 err = platform_driver_register (&gpio_apu2_driver);
352 gpio_apu2_platform_device = platform_device_register_simple (DEVNAME, -1, NULL, 0);
353 if (IS_ERR(gpio_apu2_platform_device)) {
354 err = PTR_ERR(gpio_apu2_platform_device);
358 pr_info ("%s: APU2 GPIO/LED driver module loaded\n", DEVNAME);
360 register_leds_gpio(-1, ARRAY_SIZE(apu2_leds_gpio), apu2_leds_gpio);
361 register_gpio_keys_polled(-1, 20, ARRAY_SIZE(apu2_gpio_keys), apu2_gpio_keys);
365 platform_driver_unregister (&gpio_apu2_driver);
370 static void __exit gpio_apu2_exit (void)
372 platform_device_unregister (gpio_apu2_platform_device);
373 platform_device_unregister (leddev);
374 platform_device_unregister (keydev);
375 platform_driver_unregister (&gpio_apu2_driver);
376 pr_info ("%s: APU2 GPIO/LED driver module unloaded\n", DEVNAME);
379 MODULE_AUTHOR ("Carsten Spiess <fli4l at carsten-spiess.de>");
380 MODULE_DESCRIPTION("GPIO driver for AMD FCH on PC-Engines APU-2");
381 MODULE_LICENSE("GPL");
383 module_init (gpio_apu2_init);
384 module_exit (gpio_apu2_exit);