1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for GE FPGA based GPIO 4 * 5 * Author: Martyn Welch <martyn.welch@ge.com> 6 * 7 * 2008 (c) GE Intelligent Platforms Embedded Systems, Inc. 8 */ 9 10 /* 11 * TODO: 12 * 13 * Configuration of output modes (totem-pole/open-drain). 14 * Interrupt configuration - interrupts are always generated, the FPGA relies 15 * on the I/O interrupt controllers mask to stop them from being propagated. 16 */ 17 18 #include <linux/gpio/driver.h> 19 #include <linux/gpio/generic.h> 20 #include <linux/io.h> 21 #include <linux/kernel.h> 22 #include <linux/mod_devicetable.h> 23 #include <linux/module.h> 24 #include <linux/platform_device.h> 25 #include <linux/property.h> 26 #include <linux/slab.h> 27 28 #define GEF_GPIO_DIRECT 0x00 29 #define GEF_GPIO_IN 0x04 30 #define GEF_GPIO_OUT 0x08 31 #define GEF_GPIO_TRIG 0x0C 32 #define GEF_GPIO_POLAR_A 0x10 33 #define GEF_GPIO_POLAR_B 0x14 34 #define GEF_GPIO_INT_STAT 0x18 35 #define GEF_GPIO_OVERRUN 0x1C 36 #define GEF_GPIO_MODE 0x20 37 38 static const struct of_device_id gef_gpio_ids[] = { 39 { 40 .compatible = "gef,sbc610-gpio", 41 .data = (void *)19, 42 }, { 43 .compatible = "gef,sbc310-gpio", 44 .data = (void *)6, 45 }, { 46 .compatible = "ge,imp3a-gpio", 47 .data = (void *)16, 48 }, 49 { } 50 }; 51 MODULE_DEVICE_TABLE(of, gef_gpio_ids); 52 53 static int __init gef_gpio_probe(struct platform_device *pdev) 54 { 55 struct gpio_generic_chip_config config; 56 struct device *dev = &pdev->dev; 57 struct gpio_generic_chip *chip; 58 struct gpio_chip *gc; 59 void __iomem *regs; 60 int ret; 61 62 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 63 if (!chip) 64 return -ENOMEM; 65 66 regs = devm_platform_ioremap_resource(pdev, 0); 67 if (IS_ERR(regs)) 68 return PTR_ERR(regs); 69 70 config = (struct gpio_generic_chip_config) { 71 .dev = dev, 72 .sz = 4, 73 .dat = regs + GEF_GPIO_IN, 74 .set = regs + GEF_GPIO_OUT, 75 .dirin = regs + GEF_GPIO_DIRECT, 76 .flags = GPIO_GENERIC_BIG_ENDIAN_BYTE_ORDER, 77 }; 78 79 ret = gpio_generic_chip_init(chip, &config); 80 if (ret) 81 return dev_err_probe(dev, ret, 82 "failed to initialize the generic GPIO chip\n"); 83 84 gc = &chip->gc; 85 86 /* Setup pointers to chip functions */ 87 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pfw", dev_fwnode(dev)); 88 if (!gc->label) 89 return -ENOMEM; 90 91 gc->base = -1; 92 gc->ngpio = (uintptr_t)device_get_match_data(dev); 93 94 /* This function adds a memory mapped GPIO chip */ 95 ret = devm_gpiochip_add_data(dev, gc, NULL); 96 if (ret) 97 return dev_err_probe(dev, ret, "GPIO chip registration failed\n"); 98 99 return 0; 100 }; 101 102 static struct platform_driver gef_gpio_driver = { 103 .driver = { 104 .name = "gef-gpio", 105 .of_match_table = gef_gpio_ids, 106 }, 107 }; 108 module_platform_driver_probe(gef_gpio_driver, gef_gpio_probe); 109 110 MODULE_DESCRIPTION("GE I/O FPGA GPIO driver"); 111 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>"); 112 MODULE_LICENSE("GPL"); 113