1 // SPDX-License-Identifier: GPL-2.0-only
3 * Xilinx gpio driver for xps/axi_gpio IP.
5 * Copyright 2008 - 2013 Xilinx, Inc.
8 #include <linux/bitops.h>
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/of_platform.h>
14 #include <linux/of_gpio.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/slab.h>
19 /* Register Offset Definitions */
20 #define XGPIO_DATA_OFFSET (0x0) /* Data register */
21 #define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */
23 #define XGPIO_CHANNEL_OFFSET 0x8
25 /* Read/Write access to the GPIO registers */
26 #if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
27 # define xgpio_readreg(offset) readl(offset)
28 # define xgpio_writereg(offset, val) writel(val, offset)
30 # define xgpio_readreg(offset) __raw_readl(offset)
31 # define xgpio_writereg(offset, val) __raw_writel(val, offset)
35 * struct xgpio_instance - Stores information about GPIO device
36 * @mmchip: OF GPIO chip for memory mapped banks
37 * @gpio_width: GPIO width for every channel
38 * @gpio_state: GPIO state shadow register
39 * @gpio_dir: GPIO direction shadow register
40 * @gpio_lock: Lock used for synchronization
42 struct xgpio_instance {
43 struct of_mm_gpio_chip mmchip;
44 unsigned int gpio_width[2];
47 spinlock_t gpio_lock[2];
50 static inline int xgpio_index(struct xgpio_instance *chip, int gpio)
52 if (gpio >= chip->gpio_width[0])
58 static inline int xgpio_regoffset(struct xgpio_instance *chip, int gpio)
60 if (xgpio_index(chip, gpio))
61 return XGPIO_CHANNEL_OFFSET;
66 static inline int xgpio_offset(struct xgpio_instance *chip, int gpio)
68 if (xgpio_index(chip, gpio))
69 return gpio - chip->gpio_width[0];
75 * xgpio_get - Read the specified signal of the GPIO device.
76 * @gc: Pointer to gpio_chip device structure.
77 * @gpio: GPIO signal number.
79 * This function reads the specified signal of the GPIO device.
82 * 0 if direction of GPIO signals is set as input otherwise it
83 * returns negative error value.
85 static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
87 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
88 struct xgpio_instance *chip = gpiochip_get_data(gc);
91 val = xgpio_readreg(mm_gc->regs + XGPIO_DATA_OFFSET +
92 xgpio_regoffset(chip, gpio));
94 return !!(val & BIT(xgpio_offset(chip, gpio)));
98 * xgpio_set - Write the specified signal of the GPIO device.
99 * @gc: Pointer to gpio_chip device structure.
100 * @gpio: GPIO signal number.
101 * @val: Value to be written to specified signal.
103 * This function writes the specified value in to the specified signal of the
106 static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
109 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
110 struct xgpio_instance *chip = gpiochip_get_data(gc);
111 int index = xgpio_index(chip, gpio);
112 int offset = xgpio_offset(chip, gpio);
114 spin_lock_irqsave(&chip->gpio_lock[index], flags);
116 /* Write to GPIO signal and set its direction to output */
118 chip->gpio_state[index] |= BIT(offset);
120 chip->gpio_state[index] &= ~BIT(offset);
122 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
123 xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
125 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
129 * xgpio_set_multiple - Write the specified signals of the GPIO device.
130 * @gc: Pointer to gpio_chip device structure.
131 * @mask: Mask of the GPIOS to modify.
132 * @bits: Value to be wrote on each GPIO
134 * This function writes the specified values into the specified signals of the
137 static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
141 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
142 struct xgpio_instance *chip = gpiochip_get_data(gc);
143 int index = xgpio_index(chip, 0);
146 spin_lock_irqsave(&chip->gpio_lock[index], flags);
148 /* Write to GPIO signals */
149 for (i = 0; i < gc->ngpio; i++) {
152 if (index != xgpio_index(chip, i)) {
153 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
154 xgpio_regoffset(chip, i),
155 chip->gpio_state[index]);
156 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
157 index = xgpio_index(chip, i);
158 spin_lock_irqsave(&chip->gpio_lock[index], flags);
160 if (__test_and_clear_bit(i, mask)) {
161 offset = xgpio_offset(chip, i);
162 if (test_bit(i, bits))
163 chip->gpio_state[index] |= BIT(offset);
165 chip->gpio_state[index] &= ~BIT(offset);
169 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
170 xgpio_regoffset(chip, i), chip->gpio_state[index]);
172 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
176 * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
177 * @gc: Pointer to gpio_chip device structure.
178 * @gpio: GPIO signal number.
181 * 0 - if direction of GPIO signals is set as input
182 * otherwise it returns negative error value.
184 static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
187 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
188 struct xgpio_instance *chip = gpiochip_get_data(gc);
189 int index = xgpio_index(chip, gpio);
190 int offset = xgpio_offset(chip, gpio);
192 spin_lock_irqsave(&chip->gpio_lock[index], flags);
194 /* Set the GPIO bit in shadow register and set direction as input */
195 chip->gpio_dir[index] |= BIT(offset);
196 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET +
197 xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
199 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
205 * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
206 * @gc: Pointer to gpio_chip device structure.
207 * @gpio: GPIO signal number.
208 * @val: Value to be written to specified signal.
210 * This function sets the direction of specified GPIO signal as output.
213 * If all GPIO signals of GPIO chip is configured as input then it returns
214 * error otherwise it returns 0.
216 static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
219 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
220 struct xgpio_instance *chip = gpiochip_get_data(gc);
221 int index = xgpio_index(chip, gpio);
222 int offset = xgpio_offset(chip, gpio);
224 spin_lock_irqsave(&chip->gpio_lock[index], flags);
226 /* Write state of GPIO signal */
228 chip->gpio_state[index] |= BIT(offset);
230 chip->gpio_state[index] &= ~BIT(offset);
231 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
232 xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
234 /* Clear the GPIO bit in shadow register and set direction as output */
235 chip->gpio_dir[index] &= ~BIT(offset);
236 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET +
237 xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
239 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
245 * xgpio_save_regs - Set initial values of GPIO pins
246 * @mm_gc: Pointer to memory mapped GPIO chip structure
248 static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc)
250 struct xgpio_instance *chip =
251 container_of(mm_gc, struct xgpio_instance, mmchip);
253 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state[0]);
254 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir[0]);
256 if (!chip->gpio_width[1])
259 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + XGPIO_CHANNEL_OFFSET,
260 chip->gpio_state[1]);
261 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET + XGPIO_CHANNEL_OFFSET,
266 * xgpio_remove - Remove method for the GPIO device.
267 * @pdev: pointer to the platform device
269 * This function remove gpiochips and frees all the allocated resources.
273 static int xgpio_remove(struct platform_device *pdev)
275 struct xgpio_instance *chip = platform_get_drvdata(pdev);
277 of_mm_gpiochip_remove(&chip->mmchip);
283 * xgpio_of_probe - Probe method for the GPIO device.
284 * @pdev: pointer to the platform device
287 * It returns 0, if the driver is bound to the GPIO device, or
288 * a negative value if there is an error.
290 static int xgpio_probe(struct platform_device *pdev)
292 struct xgpio_instance *chip;
294 struct device_node *np = pdev->dev.of_node;
297 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
301 platform_set_drvdata(pdev, chip);
303 /* Update GPIO state shadow register with default value */
304 of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0]);
306 /* Update GPIO direction shadow register with default value */
307 if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0]))
308 chip->gpio_dir[0] = 0xFFFFFFFF;
311 * Check device node and parent device node for device width
312 * and assume default width of 32
314 if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0]))
315 chip->gpio_width[0] = 32;
317 spin_lock_init(&chip->gpio_lock[0]);
319 if (of_property_read_u32(np, "xlnx,is-dual", &is_dual))
323 /* Update GPIO state shadow register with default value */
324 of_property_read_u32(np, "xlnx,dout-default-2",
325 &chip->gpio_state[1]);
327 /* Update GPIO direction shadow register with default value */
328 if (of_property_read_u32(np, "xlnx,tri-default-2",
330 chip->gpio_dir[1] = 0xFFFFFFFF;
333 * Check device node and parent device node for device width
334 * and assume default width of 32
336 if (of_property_read_u32(np, "xlnx,gpio2-width",
337 &chip->gpio_width[1]))
338 chip->gpio_width[1] = 32;
340 spin_lock_init(&chip->gpio_lock[1]);
343 chip->mmchip.gc.ngpio = chip->gpio_width[0] + chip->gpio_width[1];
344 chip->mmchip.gc.parent = &pdev->dev;
345 chip->mmchip.gc.direction_input = xgpio_dir_in;
346 chip->mmchip.gc.direction_output = xgpio_dir_out;
347 chip->mmchip.gc.get = xgpio_get;
348 chip->mmchip.gc.set = xgpio_set;
349 chip->mmchip.gc.set_multiple = xgpio_set_multiple;
351 chip->mmchip.save_regs = xgpio_save_regs;
353 /* Call the OF gpio helper to setup and register the GPIO device */
354 status = of_mm_gpiochip_add_data(np, &chip->mmchip, chip);
356 pr_err("%pOF: error in probe function with status %d\n",
364 static const struct of_device_id xgpio_of_match[] = {
365 { .compatible = "xlnx,xps-gpio-1.00.a", },
366 { /* end of list */ },
369 MODULE_DEVICE_TABLE(of, xgpio_of_match);
371 static struct platform_driver xgpio_plat_driver = {
372 .probe = xgpio_probe,
373 .remove = xgpio_remove,
375 .name = "gpio-xilinx",
376 .of_match_table = xgpio_of_match,
380 static int __init xgpio_init(void)
382 return platform_driver_register(&xgpio_plat_driver);
385 subsys_initcall(xgpio_init);
387 static void __exit xgpio_exit(void)
389 platform_driver_unregister(&xgpio_plat_driver);
391 module_exit(xgpio_exit);
393 MODULE_AUTHOR("Xilinx, Inc.");
394 MODULE_DESCRIPTION("Xilinx GPIO driver");
395 MODULE_LICENSE("GPL");