2 * Xilinx gpio driver for xps/axi_gpio IP.
4 * Copyright 2008 - 2013 Xilinx, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * You should have received a copy of the GNU General Public License
11 * along with this program; if not, write to the Free Software
12 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #include <linux/bitops.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/of_platform.h>
21 #include <linux/of_gpio.h>
23 #include <linux/gpio.h>
24 #include <linux/slab.h>
26 /* Register Offset Definitions */
27 #define XGPIO_DATA_OFFSET (0x0) /* Data register */
28 #define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */
30 #define XGPIO_CHANNEL_OFFSET 0x8
32 /* Read/Write access to the GPIO registers */
33 #if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
34 # define xgpio_readreg(offset) readl(offset)
35 # define xgpio_writereg(offset, val) writel(val, offset)
37 # define xgpio_readreg(offset) __raw_readl(offset)
38 # define xgpio_writereg(offset, val) __raw_writel(val, offset)
42 * struct xgpio_instance - Stores information about GPIO device
43 * @mmchip: OF GPIO chip for memory mapped banks
44 * @gpio_state: GPIO state shadow register
45 * @gpio_dir: GPIO direction shadow register
46 * @gpio_lock: Lock used for synchronization
47 * @inited: True if the port has been inited
49 struct xgpio_instance
{
50 struct of_mm_gpio_chip mmchip
;
58 struct xgpio_instance port
[2];
62 * xgpio_get - Read the specified signal of the GPIO device.
63 * @gc: Pointer to gpio_chip device structure.
64 * @gpio: GPIO signal number.
66 * This function reads the specified signal of the GPIO device.
69 * 0 if direction of GPIO signals is set as input otherwise it
70 * returns negative error value.
72 static int xgpio_get(struct gpio_chip
*gc
, unsigned int gpio
)
74 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
76 return !!(xgpio_readreg(mm_gc
->regs
+ XGPIO_DATA_OFFSET
) & BIT(gpio
));
80 * xgpio_set - Write the specified signal of the GPIO device.
81 * @gc: Pointer to gpio_chip device structure.
82 * @gpio: GPIO signal number.
83 * @val: Value to be written to specified signal.
85 * This function writes the specified value in to the specified signal of the
88 static void xgpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
91 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
92 struct xgpio_instance
*chip
=
93 container_of(mm_gc
, struct xgpio_instance
, mmchip
);
95 spin_lock_irqsave(&chip
->gpio_lock
, flags
);
97 /* Write to GPIO signal and set its direction to output */
99 chip
->gpio_state
|= BIT(gpio
);
101 chip
->gpio_state
&= ~BIT(gpio
);
103 xgpio_writereg(mm_gc
->regs
+ XGPIO_DATA_OFFSET
, chip
->gpio_state
);
105 spin_unlock_irqrestore(&chip
->gpio_lock
, flags
);
109 * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
110 * @gc: Pointer to gpio_chip device structure.
111 * @gpio: GPIO signal number.
114 * 0 - if direction of GPIO signals is set as input
115 * otherwise it returns negative error value.
117 static int xgpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
120 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
121 struct xgpio_instance
*chip
=
122 container_of(mm_gc
, struct xgpio_instance
, mmchip
);
124 spin_lock_irqsave(&chip
->gpio_lock
, flags
);
126 /* Set the GPIO bit in shadow register and set direction as input */
127 chip
->gpio_dir
|= BIT(gpio
);
128 xgpio_writereg(mm_gc
->regs
+ XGPIO_TRI_OFFSET
, chip
->gpio_dir
);
130 spin_unlock_irqrestore(&chip
->gpio_lock
, flags
);
136 * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
137 * @gc: Pointer to gpio_chip device structure.
138 * @gpio: GPIO signal number.
139 * @val: Value to be written to specified signal.
141 * This function sets the direction of specified GPIO signal as output.
144 * If all GPIO signals of GPIO chip is configured as input then it returns
145 * error otherwise it returns 0.
147 static int xgpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
150 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
151 struct xgpio_instance
*chip
=
152 container_of(mm_gc
, struct xgpio_instance
, mmchip
);
154 spin_lock_irqsave(&chip
->gpio_lock
, flags
);
156 /* Write state of GPIO signal */
158 chip
->gpio_state
|= BIT(gpio
);
160 chip
->gpio_state
&= ~BIT(gpio
);
161 xgpio_writereg(mm_gc
->regs
+ XGPIO_DATA_OFFSET
, chip
->gpio_state
);
163 /* Clear the GPIO bit in shadow register and set direction as output */
164 chip
->gpio_dir
&= ~BIT(gpio
);
165 xgpio_writereg(mm_gc
->regs
+ XGPIO_TRI_OFFSET
, chip
->gpio_dir
);
167 spin_unlock_irqrestore(&chip
->gpio_lock
, flags
);
173 * xgpio_save_regs - Set initial values of GPIO pins
174 * @mm_gc: Pointer to memory mapped GPIO chip structure
176 static void xgpio_save_regs(struct of_mm_gpio_chip
*mm_gc
)
178 struct xgpio_instance
*chip
=
179 container_of(mm_gc
, struct xgpio_instance
, mmchip
);
181 xgpio_writereg(mm_gc
->regs
+ XGPIO_DATA_OFFSET
, chip
->gpio_state
);
182 xgpio_writereg(mm_gc
->regs
+ XGPIO_TRI_OFFSET
, chip
->gpio_dir
);
186 * xgpio_remove - Remove method for the GPIO device.
187 * @pdev: pointer to the platform device
189 * This function remove gpiochips and frees all the allocated resources.
191 static int xgpio_remove(struct platform_device
*pdev
)
193 struct xgpio
*xgpio
= platform_get_drvdata(pdev
);
196 for (i
= 0; i
< 2; i
++) {
197 if (!xgpio
->port
[i
].inited
)
199 gpiochip_remove(&xgpio
->port
[i
].mmchip
.gc
);
202 xgpio
->port
[i
].mmchip
.regs
-= XGPIO_CHANNEL_OFFSET
;
204 iounmap(xgpio
->port
[i
].mmchip
.regs
);
205 kfree(xgpio
->port
[i
].mmchip
.gc
.label
);
212 * xgpio_of_probe - Probe method for the GPIO device.
213 * @pdev: pointer to the platform device
216 * It returns 0, if the driver is bound to the GPIO device, or
217 * a negative value if there is an error.
219 static int xgpio_probe(struct platform_device
*pdev
)
222 struct xgpio_instance
*chip
;
224 struct device_node
*np
= pdev
->dev
.of_node
;
225 const u32
*tree_info
;
228 xgpio
= devm_kzalloc(&pdev
->dev
, sizeof(*xgpio
), GFP_KERNEL
);
232 platform_set_drvdata(pdev
, xgpio
);
234 chip
= &xgpio
->port
[0];
236 /* Update GPIO state shadow register with default value */
237 of_property_read_u32(np
, "xlnx,dout-default", &chip
->gpio_state
);
239 /* By default, all pins are inputs */
240 chip
->gpio_dir
= 0xFFFFFFFF;
242 /* Update GPIO direction shadow register with default value */
243 of_property_read_u32(np
, "xlnx,tri-default", &chip
->gpio_dir
);
246 * Check device node and parent device node for device width
247 * and assume default width of 32
249 if (of_property_read_u32(np
, "xlnx,gpio-width", &ngpio
))
251 chip
->mmchip
.gc
.ngpio
= (u16
)ngpio
;
253 spin_lock_init(&chip
->gpio_lock
);
255 chip
->mmchip
.gc
.dev
= &pdev
->dev
;
256 chip
->mmchip
.gc
.direction_input
= xgpio_dir_in
;
257 chip
->mmchip
.gc
.direction_output
= xgpio_dir_out
;
258 chip
->mmchip
.gc
.get
= xgpio_get
;
259 chip
->mmchip
.gc
.set
= xgpio_set
;
261 chip
->mmchip
.save_regs
= xgpio_save_regs
;
263 /* Call the OF gpio helper to setup and register the GPIO device */
264 status
= of_mm_gpiochip_add(np
, &chip
->mmchip
);
266 pr_err("%s: error in probe function with status %d\n",
267 np
->full_name
, status
);
272 pr_info("XGpio: %s: registered, base is %d\n", np
->full_name
,
273 chip
->mmchip
.gc
.base
);
275 tree_info
= of_get_property(np
, "xlnx,is-dual", NULL
);
276 if (tree_info
&& be32_to_cpup(tree_info
)) {
277 chip
= &xgpio
->port
[1];
279 /* Update GPIO state shadow register with default value */
280 of_property_read_u32(np
, "xlnx,dout-default-2",
283 /* By default, all pins are inputs */
284 chip
->gpio_dir
= 0xFFFFFFFF;
286 /* Update GPIO direction shadow register with default value */
287 of_property_read_u32(np
, "xlnx,tri-default-2", &chip
->gpio_dir
);
290 * Check device node and parent device node for device width
291 * and assume default width of 32
293 if (of_property_read_u32(np
, "xlnx,gpio2-width", &ngpio
))
295 chip
->mmchip
.gc
.ngpio
= (u16
)ngpio
;
297 spin_lock_init(&chip
->gpio_lock
);
299 chip
->mmchip
.gc
.dev
= &pdev
->dev
;
300 chip
->mmchip
.gc
.direction_input
= xgpio_dir_in
;
301 chip
->mmchip
.gc
.direction_output
= xgpio_dir_out
;
302 chip
->mmchip
.gc
.get
= xgpio_get
;
303 chip
->mmchip
.gc
.set
= xgpio_set
;
305 chip
->mmchip
.save_regs
= xgpio_save_regs
;
307 /* Call the OF gpio helper to setup and register the GPIO dev */
308 status
= of_mm_gpiochip_add(np
, &chip
->mmchip
);
311 pr_err("%s: error in probe function with status %d\n",
312 np
->full_name
, status
);
316 /* Add dual channel offset */
317 chip
->mmchip
.regs
+= XGPIO_CHANNEL_OFFSET
;
320 pr_info("XGpio: %s: dual channel registered, base is %d\n",
321 np
->full_name
, chip
->mmchip
.gc
.base
);
327 static const struct of_device_id xgpio_of_match
[] = {
328 { .compatible
= "xlnx,xps-gpio-1.00.a", },
329 { /* end of list */ },
332 MODULE_DEVICE_TABLE(of
, xgpio_of_match
);
334 static struct platform_driver xgpio_plat_driver
= {
335 .probe
= xgpio_probe
,
336 .remove
= xgpio_remove
,
338 .name
= "gpio-xilinx",
339 .of_match_table
= xgpio_of_match
,
343 static int __init
xgpio_init(void)
345 return platform_driver_register(&xgpio_plat_driver
);
348 subsys_initcall(xgpio_init
);
350 static void __exit
xgpio_exit(void)
352 platform_driver_unregister(&xgpio_plat_driver
);
354 module_exit(xgpio_exit
);
356 MODULE_AUTHOR("Xilinx, Inc.");
357 MODULE_DESCRIPTION("Xilinx GPIO driver");
358 MODULE_LICENSE("GPL");