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/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/property.h>
25 #include <linux/slab.h>
26
27 #define GEF_GPIO_DIRECT 0x00
28 #define GEF_GPIO_IN 0x04
29 #define GEF_GPIO_OUT 0x08
30 #define GEF_GPIO_TRIG 0x0C
31 #define GEF_GPIO_POLAR_A 0x10
32 #define GEF_GPIO_POLAR_B 0x14
33 #define GEF_GPIO_INT_STAT 0x18
34 #define GEF_GPIO_OVERRUN 0x1C
35 #define GEF_GPIO_MODE 0x20
36
37 static const struct of_device_id gef_gpio_ids[] = {
38 {
39 .compatible = "gef,sbc610-gpio",
40 .data = (void *)19,
41 }, {
42 .compatible = "gef,sbc310-gpio",
43 .data = (void *)6,
44 }, {
45 .compatible = "ge,imp3a-gpio",
46 .data = (void *)16,
47 },
48 { }
49 };
50 MODULE_DEVICE_TABLE(of, gef_gpio_ids);
51
gef_gpio_probe(struct platform_device * pdev)52 static int __init gef_gpio_probe(struct platform_device *pdev)
53 {
54 struct gpio_generic_chip_config config;
55 struct device *dev = &pdev->dev;
56 struct gpio_generic_chip *chip;
57 struct gpio_chip *gc;
58 void __iomem *regs;
59 int ret;
60
61 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
62 if (!chip)
63 return -ENOMEM;
64
65 regs = devm_platform_ioremap_resource(pdev, 0);
66 if (IS_ERR(regs))
67 return PTR_ERR(regs);
68
69 config = (struct gpio_generic_chip_config) {
70 .dev = dev,
71 .sz = 4,
72 .dat = regs + GEF_GPIO_IN,
73 .set = regs + GEF_GPIO_OUT,
74 .dirin = regs + GEF_GPIO_DIRECT,
75 .flags = GPIO_GENERIC_BIG_ENDIAN_BYTE_ORDER,
76 };
77
78 ret = gpio_generic_chip_init(chip, &config);
79 if (ret)
80 return dev_err_probe(dev, ret,
81 "failed to initialize the generic GPIO chip\n");
82
83 gc = &chip->gc;
84
85 /* Setup pointers to chip functions */
86 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pfw", dev_fwnode(dev));
87 if (!gc->label)
88 return -ENOMEM;
89
90 gc->base = -1;
91 gc->ngpio = (uintptr_t)device_get_match_data(dev);
92
93 /* This function adds a memory mapped GPIO chip */
94 ret = devm_gpiochip_add_data(dev, gc, NULL);
95 if (ret)
96 return dev_err_probe(dev, ret, "GPIO chip registration failed\n");
97
98 return 0;
99 };
100
101 static struct platform_driver gef_gpio_driver = {
102 .driver = {
103 .name = "gef-gpio",
104 .of_match_table = gef_gpio_ids,
105 },
106 };
107 module_platform_driver_probe(gef_gpio_driver, gef_gpio_probe);
108
109 MODULE_DESCRIPTION("GE I/O FPGA GPIO driver");
110 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
111 MODULE_LICENSE("GPL");
112