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>
10 #include <linux/errno.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/of_platform.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
37 * @regs: register block
38 * @gpio_width: GPIO width for every channel
39 * @gpio_state: GPIO state shadow register
40 * @gpio_dir: GPIO direction shadow register
41 * @gpio_lock: Lock used for synchronization
42 * @clk: clock resource for this driver
44 struct xgpio_instance {
47 unsigned int gpio_width[2];
50 spinlock_t gpio_lock[2];
54 static inline int xgpio_index(struct xgpio_instance *chip, int gpio)
56 if (gpio >= chip->gpio_width[0])
62 static inline int xgpio_regoffset(struct xgpio_instance *chip, int gpio)
64 if (xgpio_index(chip, gpio))
65 return XGPIO_CHANNEL_OFFSET;
70 static inline int xgpio_offset(struct xgpio_instance *chip, int gpio)
72 if (xgpio_index(chip, gpio))
73 return gpio - chip->gpio_width[0];
79 * xgpio_get - Read the specified signal of the GPIO device.
80 * @gc: Pointer to gpio_chip device structure.
81 * @gpio: GPIO signal number.
83 * This function reads the specified signal of the GPIO device.
86 * 0 if direction of GPIO signals is set as input otherwise it
87 * returns negative error value.
89 static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
91 struct xgpio_instance *chip = gpiochip_get_data(gc);
94 val = xgpio_readreg(chip->regs + XGPIO_DATA_OFFSET +
95 xgpio_regoffset(chip, gpio));
97 return !!(val & BIT(xgpio_offset(chip, gpio)));
101 * xgpio_set - Write the specified signal of the GPIO device.
102 * @gc: Pointer to gpio_chip device structure.
103 * @gpio: GPIO signal number.
104 * @val: Value to be written to specified signal.
106 * This function writes the specified value in to the specified signal of the
109 static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
112 struct xgpio_instance *chip = gpiochip_get_data(gc);
113 int index = xgpio_index(chip, gpio);
114 int offset = xgpio_offset(chip, gpio);
116 spin_lock_irqsave(&chip->gpio_lock[index], flags);
118 /* Write to GPIO signal and set its direction to output */
120 chip->gpio_state[index] |= BIT(offset);
122 chip->gpio_state[index] &= ~BIT(offset);
124 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
125 xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
127 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
131 * xgpio_set_multiple - Write the specified signals of the GPIO device.
132 * @gc: Pointer to gpio_chip device structure.
133 * @mask: Mask of the GPIOS to modify.
134 * @bits: Value to be wrote on each GPIO
136 * This function writes the specified values into the specified signals of the
139 static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
143 struct xgpio_instance *chip = gpiochip_get_data(gc);
144 int index = xgpio_index(chip, 0);
147 spin_lock_irqsave(&chip->gpio_lock[index], flags);
149 /* Write to GPIO signals */
150 for (i = 0; i < gc->ngpio; i++) {
153 /* Once finished with an index write it out to the register */
154 if (index != xgpio_index(chip, i)) {
155 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
156 index * XGPIO_CHANNEL_OFFSET,
157 chip->gpio_state[index]);
158 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
159 index = xgpio_index(chip, i);
160 spin_lock_irqsave(&chip->gpio_lock[index], flags);
162 if (__test_and_clear_bit(i, mask)) {
163 offset = xgpio_offset(chip, i);
164 if (test_bit(i, bits))
165 chip->gpio_state[index] |= BIT(offset);
167 chip->gpio_state[index] &= ~BIT(offset);
171 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
172 index * XGPIO_CHANNEL_OFFSET, chip->gpio_state[index]);
174 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
178 * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
179 * @gc: Pointer to gpio_chip device structure.
180 * @gpio: GPIO signal number.
183 * 0 - if direction of GPIO signals is set as input
184 * otherwise it returns negative error value.
186 static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
189 struct xgpio_instance *chip = gpiochip_get_data(gc);
190 int index = xgpio_index(chip, gpio);
191 int offset = xgpio_offset(chip, gpio);
193 spin_lock_irqsave(&chip->gpio_lock[index], flags);
195 /* Set the GPIO bit in shadow register and set direction as input */
196 chip->gpio_dir[index] |= BIT(offset);
197 xgpio_writereg(chip->regs + XGPIO_TRI_OFFSET +
198 xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
200 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
206 * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
207 * @gc: Pointer to gpio_chip device structure.
208 * @gpio: GPIO signal number.
209 * @val: Value to be written to specified signal.
211 * This function sets the direction of specified GPIO signal as output.
214 * If all GPIO signals of GPIO chip is configured as input then it returns
215 * error otherwise it returns 0.
217 static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
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(chip->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(chip->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 * @chip: Pointer to GPIO instance
248 static void xgpio_save_regs(struct xgpio_instance *chip)
250 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET, chip->gpio_state[0]);
251 xgpio_writereg(chip->regs + XGPIO_TRI_OFFSET, chip->gpio_dir[0]);
253 if (!chip->gpio_width[1])
256 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET + XGPIO_CHANNEL_OFFSET,
257 chip->gpio_state[1]);
258 xgpio_writereg(chip->regs + XGPIO_TRI_OFFSET + XGPIO_CHANNEL_OFFSET,
263 * xgpio_remove - Remove method for the GPIO device.
264 * @pdev: pointer to the platform device
266 * This function remove gpiochips and frees all the allocated resources.
270 static int xgpio_remove(struct platform_device *pdev)
272 struct xgpio_instance *gpio = platform_get_drvdata(pdev);
274 clk_disable_unprepare(gpio->clk);
280 * xgpio_of_probe - Probe method for the GPIO device.
281 * @pdev: pointer to the platform device
284 * It returns 0, if the driver is bound to the GPIO device, or
285 * a negative value if there is an error.
287 static int xgpio_probe(struct platform_device *pdev)
289 struct xgpio_instance *chip;
291 struct device_node *np = pdev->dev.of_node;
294 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
298 platform_set_drvdata(pdev, chip);
300 /* Update GPIO state shadow register with default value */
301 if (of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0]))
302 chip->gpio_state[0] = 0x0;
304 /* Update GPIO direction shadow register with default value */
305 if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0]))
306 chip->gpio_dir[0] = 0xFFFFFFFF;
309 * Check device node and parent device node for device width
310 * and assume default width of 32
312 if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0]))
313 chip->gpio_width[0] = 32;
315 spin_lock_init(&chip->gpio_lock[0]);
317 if (of_property_read_u32(np, "xlnx,is-dual", &is_dual))
321 /* Update GPIO state shadow register with default value */
322 if (of_property_read_u32(np, "xlnx,dout-default-2",
323 &chip->gpio_state[1]))
324 chip->gpio_state[1] = 0x0;
326 /* Update GPIO direction shadow register with default value */
327 if (of_property_read_u32(np, "xlnx,tri-default-2",
329 chip->gpio_dir[1] = 0xFFFFFFFF;
332 * Check device node and parent device node for device width
333 * and assume default width of 32
335 if (of_property_read_u32(np, "xlnx,gpio2-width",
336 &chip->gpio_width[1]))
337 chip->gpio_width[1] = 32;
339 spin_lock_init(&chip->gpio_lock[1]);
343 chip->gc.ngpio = chip->gpio_width[0] + chip->gpio_width[1];
344 chip->gc.parent = &pdev->dev;
345 chip->gc.direction_input = xgpio_dir_in;
346 chip->gc.direction_output = xgpio_dir_out;
347 chip->gc.get = xgpio_get;
348 chip->gc.set = xgpio_set;
349 chip->gc.set_multiple = xgpio_set_multiple;
351 chip->gc.label = dev_name(&pdev->dev);
353 chip->regs = devm_platform_ioremap_resource(pdev, 0);
354 if (IS_ERR(chip->regs)) {
355 dev_err(&pdev->dev, "failed to ioremap memory resource\n");
356 return PTR_ERR(chip->regs);
359 chip->clk = devm_clk_get_optional(&pdev->dev, NULL);
360 if (IS_ERR(chip->clk)) {
361 if (PTR_ERR(chip->clk) != -EPROBE_DEFER)
362 dev_dbg(&pdev->dev, "Input clock not found\n");
363 return PTR_ERR(chip->clk);
366 status = clk_prepare_enable(chip->clk);
368 dev_err(&pdev->dev, "Failed to prepare clk\n");
372 xgpio_save_regs(chip);
374 status = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
376 dev_err(&pdev->dev, "failed to add GPIO chip\n");
377 clk_disable_unprepare(chip->clk);
384 static const struct of_device_id xgpio_of_match[] = {
385 { .compatible = "xlnx,xps-gpio-1.00.a", },
386 { /* end of list */ },
389 MODULE_DEVICE_TABLE(of, xgpio_of_match);
391 static struct platform_driver xgpio_plat_driver = {
392 .probe = xgpio_probe,
393 .remove = xgpio_remove,
395 .name = "gpio-xilinx",
396 .of_match_table = xgpio_of_match,
400 static int __init xgpio_init(void)
402 return platform_driver_register(&xgpio_plat_driver);
405 subsys_initcall(xgpio_init);
407 static void __exit xgpio_exit(void)
409 platform_driver_unregister(&xgpio_plat_driver);
411 module_exit(xgpio_exit);
413 MODULE_AUTHOR("Xilinx, Inc.");
414 MODULE_DESCRIPTION("Xilinx GPIO driver");
415 MODULE_LICENSE("GPL");