blob: 9cf445732675f2b565abbe301c386aad00e34817 [file] [log] [blame]
John Linn0bcb6062008-11-12 13:25:38 -08001/*
Michal Simek74600ee2013-06-03 14:31:17 +02002 * Xilinx gpio driver for xps/axi_gpio IP.
John Linn0bcb6062008-11-12 13:25:38 -08003 *
Michal Simek74600ee2013-06-03 14:31:17 +02004 * Copyright 2008 - 2013 Xilinx, Inc.
John Linn0bcb6062008-11-12 13:25:38 -08005 *
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.
9 *
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
13 */
14
Michal Simek74600ee2013-06-03 14:31:17 +020015#include <linux/bitops.h>
John Linn0bcb6062008-11-12 13:25:38 -080016#include <linux/init.h>
17#include <linux/errno.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -040018#include <linux/module.h>
John Linn0bcb6062008-11-12 13:25:38 -080019#include <linux/of_device.h>
20#include <linux/of_platform.h>
21#include <linux/of_gpio.h>
22#include <linux/io.h>
23#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
John Linn0bcb6062008-11-12 13:25:38 -080025
26/* Register Offset Definitions */
27#define XGPIO_DATA_OFFSET (0x0) /* Data register */
28#define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */
29
Michal Simek74600ee2013-06-03 14:31:17 +020030#define XGPIO_CHANNEL_OFFSET 0x8
31
32/* Read/Write access to the GPIO registers */
Ricardo Ribalda Delgadoc54c58b2014-12-17 16:51:10 +010033#if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
Michal Simekcc090d62013-06-03 14:31:18 +020034# define xgpio_readreg(offset) readl(offset)
35# define xgpio_writereg(offset, val) writel(val, offset)
36#else
37# define xgpio_readreg(offset) __raw_readl(offset)
38# define xgpio_writereg(offset, val) __raw_writel(val, offset)
39#endif
Michal Simek74600ee2013-06-03 14:31:17 +020040
41/**
42 * struct xgpio_instance - Stores information about GPIO device
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +010043 * @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
Michal Simek74600ee2013-06-03 14:31:17 +020048 */
John Linn0bcb6062008-11-12 13:25:38 -080049struct xgpio_instance {
50 struct of_mm_gpio_chip mmchip;
Michal Simek74600ee2013-06-03 14:31:17 +020051 u32 gpio_state;
52 u32 gpio_dir;
Michal Simek74600ee2013-06-03 14:31:17 +020053 spinlock_t gpio_lock;
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +010054 bool inited;
55};
56
57struct xgpio {
58 struct xgpio_instance port[2];
John Linn0bcb6062008-11-12 13:25:38 -080059};
60
61/**
62 * xgpio_get - Read the specified signal of the GPIO device.
63 * @gc: Pointer to gpio_chip device structure.
64 * @gpio: GPIO signal number.
65 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +010066 * This function reads the specified signal of the GPIO device.
67 *
68 * Return:
69 * 0 if direction of GPIO signals is set as input otherwise it
70 * returns negative error value.
John Linn0bcb6062008-11-12 13:25:38 -080071 */
72static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
73{
74 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
75
Ricardo Ribalda Delgadobc2f3dc2014-12-17 16:51:08 +010076 return !!(xgpio_readreg(mm_gc->regs + XGPIO_DATA_OFFSET) & BIT(gpio));
John Linn0bcb6062008-11-12 13:25:38 -080077}
78
79/**
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.
84 *
85 * This function writes the specified value in to the specified signal of the
86 * GPIO device.
87 */
88static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
89{
90 unsigned long flags;
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);
94
95 spin_lock_irqsave(&chip->gpio_lock, flags);
96
97 /* Write to GPIO signal and set its direction to output */
98 if (val)
Michal Simek9f7f0b22013-06-03 14:31:19 +020099 chip->gpio_state |= BIT(gpio);
John Linn0bcb6062008-11-12 13:25:38 -0800100 else
Michal Simek9f7f0b22013-06-03 14:31:19 +0200101 chip->gpio_state &= ~BIT(gpio);
Michal Simek74600ee2013-06-03 14:31:17 +0200102
Ricardo Ribalda Delgadobc2f3dc2014-12-17 16:51:08 +0100103 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state);
John Linn0bcb6062008-11-12 13:25:38 -0800104
105 spin_unlock_irqrestore(&chip->gpio_lock, flags);
106}
107
108/**
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.
112 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100113 * Return:
114 * 0 - if direction of GPIO signals is set as input
115 * otherwise it returns negative error value.
John Linn0bcb6062008-11-12 13:25:38 -0800116 */
117static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
118{
119 unsigned long flags;
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);
123
124 spin_lock_irqsave(&chip->gpio_lock, flags);
125
126 /* Set the GPIO bit in shadow register and set direction as input */
Michal Simek9f7f0b22013-06-03 14:31:19 +0200127 chip->gpio_dir |= BIT(gpio);
Ricardo Ribalda Delgadobc2f3dc2014-12-17 16:51:08 +0100128 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir);
John Linn0bcb6062008-11-12 13:25:38 -0800129
130 spin_unlock_irqrestore(&chip->gpio_lock, flags);
131
132 return 0;
133}
134
135/**
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.
140 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100141 * This function sets the direction of specified GPIO signal as output.
142 *
143 * Return:
144 * If all GPIO signals of GPIO chip is configured as input then it returns
John Linn0bcb6062008-11-12 13:25:38 -0800145 * error otherwise it returns 0.
146 */
147static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
148{
149 unsigned long flags;
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);
153
154 spin_lock_irqsave(&chip->gpio_lock, flags);
155
156 /* Write state of GPIO signal */
157 if (val)
Michal Simek9f7f0b22013-06-03 14:31:19 +0200158 chip->gpio_state |= BIT(gpio);
John Linn0bcb6062008-11-12 13:25:38 -0800159 else
Michal Simek9f7f0b22013-06-03 14:31:19 +0200160 chip->gpio_state &= ~BIT(gpio);
Ricardo Ribalda Delgadobc2f3dc2014-12-17 16:51:08 +0100161 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state);
John Linn0bcb6062008-11-12 13:25:38 -0800162
163 /* Clear the GPIO bit in shadow register and set direction as output */
Michal Simek9f7f0b22013-06-03 14:31:19 +0200164 chip->gpio_dir &= ~BIT(gpio);
Ricardo Ribalda Delgadobc2f3dc2014-12-17 16:51:08 +0100165 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir);
John Linn0bcb6062008-11-12 13:25:38 -0800166
167 spin_unlock_irqrestore(&chip->gpio_lock, flags);
168
169 return 0;
170}
171
172/**
173 * xgpio_save_regs - Set initial values of GPIO pins
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100174 * @mm_gc: Pointer to memory mapped GPIO chip structure
John Linn0bcb6062008-11-12 13:25:38 -0800175 */
176static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc)
177{
178 struct xgpio_instance *chip =
179 container_of(mm_gc, struct xgpio_instance, mmchip);
180
Ricardo Ribalda Delgadobc2f3dc2014-12-17 16:51:08 +0100181 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state);
182 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir);
John Linn0bcb6062008-11-12 13:25:38 -0800183}
184
185/**
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100186 * xgpio_remove - Remove method for the GPIO device.
187 * @pdev: pointer to the platform device
188 *
189 * This function remove gpiochips and frees all the allocated resources.
190 */
191static int xgpio_remove(struct platform_device *pdev)
192{
193 struct xgpio *xgpio = platform_get_drvdata(pdev);
194 int i;
195
196 for (i = 0; i < 2; i++) {
197 if (!xgpio->port[i].inited)
198 continue;
199 gpiochip_remove(&xgpio->port[i].mmchip.gc);
200
201 if (i == 1)
202 xgpio->port[i].mmchip.regs -= XGPIO_CHANNEL_OFFSET;
203
204 iounmap(xgpio->port[i].mmchip.regs);
205 kfree(xgpio->port[i].mmchip.gc.label);
206 }
207
208 return 0;
209}
210
211/**
John Linn0bcb6062008-11-12 13:25:38 -0800212 * xgpio_of_probe - Probe method for the GPIO device.
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100213 * @pdev: pointer to the platform device
John Linn0bcb6062008-11-12 13:25:38 -0800214 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100215 * Return:
216 * It returns 0, if the driver is bound to the GPIO device, or
217 * a negative value if there is an error.
John Linn0bcb6062008-11-12 13:25:38 -0800218 */
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100219static int xgpio_probe(struct platform_device *pdev)
John Linn0bcb6062008-11-12 13:25:38 -0800220{
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100221 struct xgpio *xgpio;
John Linn0bcb6062008-11-12 13:25:38 -0800222 struct xgpio_instance *chip;
John Linn0bcb6062008-11-12 13:25:38 -0800223 int status = 0;
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100224 struct device_node *np = pdev->dev.of_node;
John Linn0bcb6062008-11-12 13:25:38 -0800225 const u32 *tree_info;
Gernot Vormayr1b4c5a62014-09-24 00:58:45 +0200226 u32 ngpio;
John Linn0bcb6062008-11-12 13:25:38 -0800227
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100228 xgpio = devm_kzalloc(&pdev->dev, sizeof(*xgpio), GFP_KERNEL);
229 if (!xgpio)
John Linn0bcb6062008-11-12 13:25:38 -0800230 return -ENOMEM;
John Linn0bcb6062008-11-12 13:25:38 -0800231
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100232 platform_set_drvdata(pdev, xgpio);
233
234 chip = &xgpio->port[0];
235
John Linn0bcb6062008-11-12 13:25:38 -0800236 /* Update GPIO state shadow register with default value */
Michal Simek6f8bf502013-06-03 14:31:16 +0200237 of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state);
238
239 /* By default, all pins are inputs */
240 chip->gpio_dir = 0xFFFFFFFF;
John Linn0bcb6062008-11-12 13:25:38 -0800241
242 /* Update GPIO direction shadow register with default value */
Michal Simek6f8bf502013-06-03 14:31:16 +0200243 of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir);
244
Gernot Vormayr1b4c5a62014-09-24 00:58:45 +0200245 /*
246 * Check device node and parent device node for device width
247 * and assume default width of 32
248 */
249 if (of_property_read_u32(np, "xlnx,gpio-width", &ngpio))
250 ngpio = 32;
251 chip->mmchip.gc.ngpio = (u16)ngpio;
John Linn0bcb6062008-11-12 13:25:38 -0800252
253 spin_lock_init(&chip->gpio_lock);
254
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100255 chip->mmchip.gc.dev = &pdev->dev;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600256 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;
John Linn0bcb6062008-11-12 13:25:38 -0800260
261 chip->mmchip.save_regs = xgpio_save_regs;
262
263 /* Call the OF gpio helper to setup and register the GPIO device */
264 status = of_mm_gpiochip_add(np, &chip->mmchip);
265 if (status) {
John Linn0bcb6062008-11-12 13:25:38 -0800266 pr_err("%s: error in probe function with status %d\n",
267 np->full_name, status);
268 return status;
269 }
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100270 chip->inited = true;
Michal Simek74600ee2013-06-03 14:31:17 +0200271
272 pr_info("XGpio: %s: registered, base is %d\n", np->full_name,
273 chip->mmchip.gc.base);
274
275 tree_info = of_get_property(np, "xlnx,is-dual", NULL);
276 if (tree_info && be32_to_cpup(tree_info)) {
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100277 chip = &xgpio->port[1];
Michal Simek74600ee2013-06-03 14:31:17 +0200278
Michal Simek74600ee2013-06-03 14:31:17 +0200279 /* Update GPIO state shadow register with default value */
280 of_property_read_u32(np, "xlnx,dout-default-2",
281 &chip->gpio_state);
282
283 /* By default, all pins are inputs */
284 chip->gpio_dir = 0xFFFFFFFF;
285
286 /* Update GPIO direction shadow register with default value */
287 of_property_read_u32(np, "xlnx,tri-default-2", &chip->gpio_dir);
288
Gernot Vormayr1b4c5a62014-09-24 00:58:45 +0200289 /*
290 * Check device node and parent device node for device width
291 * and assume default width of 32
292 */
293 if (of_property_read_u32(np, "xlnx,gpio2-width", &ngpio))
294 ngpio = 32;
295 chip->mmchip.gc.ngpio = (u16)ngpio;
Michal Simek74600ee2013-06-03 14:31:17 +0200296
297 spin_lock_init(&chip->gpio_lock);
298
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100299 chip->mmchip.gc.dev = &pdev->dev;
Michal Simek74600ee2013-06-03 14:31:17 +0200300 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;
304
305 chip->mmchip.save_regs = xgpio_save_regs;
306
307 /* Call the OF gpio helper to setup and register the GPIO dev */
308 status = of_mm_gpiochip_add(np, &chip->mmchip);
309 if (status) {
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100310 xgpio_remove(pdev);
Michal Simek74600ee2013-06-03 14:31:17 +0200311 pr_err("%s: error in probe function with status %d\n",
312 np->full_name, status);
313 return status;
314 }
Ricardo Ribalda Delgadobc2f3dc2014-12-17 16:51:08 +0100315
316 /* Add dual channel offset */
317 chip->mmchip.regs += XGPIO_CHANNEL_OFFSET;
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100318 chip->inited = true;
Ricardo Ribalda Delgadobc2f3dc2014-12-17 16:51:08 +0100319
Michal Simek74600ee2013-06-03 14:31:17 +0200320 pr_info("XGpio: %s: dual channel registered, base is %d\n",
321 np->full_name, chip->mmchip.gc.base);
322 }
323
John Linn0bcb6062008-11-12 13:25:38 -0800324 return 0;
325}
326
Jingoo Han9992bc92014-05-07 18:08:20 +0900327static const struct of_device_id xgpio_of_match[] = {
John Linn0bcb6062008-11-12 13:25:38 -0800328 { .compatible = "xlnx,xps-gpio-1.00.a", },
329 { /* end of list */ },
330};
331
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100332MODULE_DEVICE_TABLE(of, xgpio_of_match);
333
334static struct platform_driver xgpio_plat_driver = {
335 .probe = xgpio_probe,
336 .remove = xgpio_remove,
337 .driver = {
338 .name = "gpio-xilinx",
339 .of_match_table = xgpio_of_match,
340 },
341};
342
John Linn0bcb6062008-11-12 13:25:38 -0800343static int __init xgpio_init(void)
344{
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100345 return platform_driver_register(&xgpio_plat_driver);
John Linn0bcb6062008-11-12 13:25:38 -0800346}
347
John Linn0bcb6062008-11-12 13:25:38 -0800348subsys_initcall(xgpio_init);
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100349
350static void __exit xgpio_exit(void)
351{
352 platform_driver_unregister(&xgpio_plat_driver);
353}
354module_exit(xgpio_exit);
John Linn0bcb6062008-11-12 13:25:38 -0800355
356MODULE_AUTHOR("Xilinx, Inc.");
357MODULE_DESCRIPTION("Xilinx GPIO driver");
358MODULE_LICENSE("GPL");