blob: a9748b5198e634f6a0938cc8bdff61f14a120cbc [file] [log] [blame]
Thomas Gleixner6e75fc02019-05-27 08:55:22 +02001// SPDX-License-Identifier: GPL-2.0-only
John Linn0bcb6062008-11-12 13:25:38 -08002/*
Michal Simek74600ee2013-06-03 14:31:17 +02003 * Xilinx gpio driver for xps/axi_gpio IP.
John Linn0bcb6062008-11-12 13:25:38 -08004 *
Michal Simek74600ee2013-06-03 14:31:17 +02005 * Copyright 2008 - 2013 Xilinx, Inc.
John Linn0bcb6062008-11-12 13:25:38 -08006 */
7
Michal Simek74600ee2013-06-03 14:31:17 +02008#include <linux/bitops.h>
John Linn0bcb6062008-11-12 13:25:38 -08009#include <linux/init.h>
10#include <linux/errno.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -040011#include <linux/module.h>
John Linn0bcb6062008-11-12 13:25:38 -080012#include <linux/of_device.h>
13#include <linux/of_platform.h>
John Linn0bcb6062008-11-12 13:25:38 -080014#include <linux/io.h>
Linus Walleij516df4e2018-08-06 18:39:59 +020015#include <linux/gpio/driver.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
John Linn0bcb6062008-11-12 13:25:38 -080017
18/* Register Offset Definitions */
19#define XGPIO_DATA_OFFSET (0x0) /* Data register */
20#define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */
21
Michal Simek74600ee2013-06-03 14:31:17 +020022#define XGPIO_CHANNEL_OFFSET 0x8
23
24/* Read/Write access to the GPIO registers */
Ricardo Ribalda Delgadoc54c58b2014-12-17 16:51:10 +010025#if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
Michal Simekcc090d62013-06-03 14:31:18 +020026# define xgpio_readreg(offset) readl(offset)
27# define xgpio_writereg(offset, val) writel(val, offset)
28#else
29# define xgpio_readreg(offset) __raw_readl(offset)
30# define xgpio_writereg(offset, val) __raw_writel(val, offset)
31#endif
Michal Simek74600ee2013-06-03 14:31:17 +020032
33/**
34 * struct xgpio_instance - Stores information about GPIO device
Robert Hancock1ebd06872019-06-07 11:04:16 -060035 * @gc: GPIO chip
36 * @regs: register block
Michal Simek3c1b5c9b2015-05-04 16:37:16 +020037 * @gpio_width: GPIO width for every channel
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +010038 * @gpio_state: GPIO state shadow register
39 * @gpio_dir: GPIO direction shadow register
40 * @gpio_lock: Lock used for synchronization
Michal Simek74600ee2013-06-03 14:31:17 +020041 */
John Linn0bcb6062008-11-12 13:25:38 -080042struct xgpio_instance {
Robert Hancock1ebd06872019-06-07 11:04:16 -060043 struct gpio_chip gc;
44 void __iomem *regs;
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010045 unsigned int gpio_width[2];
46 u32 gpio_state[2];
47 u32 gpio_dir[2];
48 spinlock_t gpio_lock[2];
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +010049};
50
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010051static inline int xgpio_index(struct xgpio_instance *chip, int gpio)
52{
53 if (gpio >= chip->gpio_width[0])
54 return 1;
55
56 return 0;
57}
58
59static inline int xgpio_regoffset(struct xgpio_instance *chip, int gpio)
60{
61 if (xgpio_index(chip, gpio))
62 return XGPIO_CHANNEL_OFFSET;
63
64 return 0;
65}
66
67static inline int xgpio_offset(struct xgpio_instance *chip, int gpio)
68{
69 if (xgpio_index(chip, gpio))
70 return gpio - chip->gpio_width[0];
71
72 return gpio;
73}
John Linn0bcb6062008-11-12 13:25:38 -080074
75/**
76 * xgpio_get - Read the specified signal of the GPIO device.
77 * @gc: Pointer to gpio_chip device structure.
78 * @gpio: GPIO signal number.
79 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +010080 * This function reads the specified signal of the GPIO device.
81 *
82 * Return:
83 * 0 if direction of GPIO signals is set as input otherwise it
84 * returns negative error value.
John Linn0bcb6062008-11-12 13:25:38 -080085 */
86static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
87{
Linus Walleij097d88e2015-12-07 15:20:17 +010088 struct xgpio_instance *chip = gpiochip_get_data(gc);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010089 u32 val;
John Linn0bcb6062008-11-12 13:25:38 -080090
Robert Hancock1ebd06872019-06-07 11:04:16 -060091 val = xgpio_readreg(chip->regs + XGPIO_DATA_OFFSET +
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010092 xgpio_regoffset(chip, gpio));
93
94 return !!(val & BIT(xgpio_offset(chip, gpio)));
John Linn0bcb6062008-11-12 13:25:38 -080095}
96
97/**
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.
102 *
103 * This function writes the specified value in to the specified signal of the
104 * GPIO device.
105 */
106static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
107{
108 unsigned long flags;
Linus Walleij097d88e2015-12-07 15:20:17 +0100109 struct xgpio_instance *chip = gpiochip_get_data(gc);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100110 int index = xgpio_index(chip, gpio);
111 int offset = xgpio_offset(chip, gpio);
John Linn0bcb6062008-11-12 13:25:38 -0800112
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100113 spin_lock_irqsave(&chip->gpio_lock[index], flags);
John Linn0bcb6062008-11-12 13:25:38 -0800114
115 /* Write to GPIO signal and set its direction to output */
116 if (val)
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100117 chip->gpio_state[index] |= BIT(offset);
John Linn0bcb6062008-11-12 13:25:38 -0800118 else
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100119 chip->gpio_state[index] &= ~BIT(offset);
Michal Simek74600ee2013-06-03 14:31:17 +0200120
Robert Hancock1ebd06872019-06-07 11:04:16 -0600121 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100122 xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
John Linn0bcb6062008-11-12 13:25:38 -0800123
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100124 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
John Linn0bcb6062008-11-12 13:25:38 -0800125}
126
127/**
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200128 * xgpio_set_multiple - Write the specified signals of the GPIO device.
129 * @gc: Pointer to gpio_chip device structure.
130 * @mask: Mask of the GPIOS to modify.
131 * @bits: Value to be wrote on each GPIO
132 *
133 * This function writes the specified values into the specified signals of the
134 * GPIO devices.
135 */
136static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
137 unsigned long *bits)
138{
139 unsigned long flags;
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200140 struct xgpio_instance *chip = gpiochip_get_data(gc);
141 int index = xgpio_index(chip, 0);
142 int offset, i;
143
144 spin_lock_irqsave(&chip->gpio_lock[index], flags);
145
146 /* Write to GPIO signals */
147 for (i = 0; i < gc->ngpio; i++) {
148 if (*mask == 0)
149 break;
150 if (index != xgpio_index(chip, i)) {
Robert Hancock1ebd06872019-06-07 11:04:16 -0600151 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200152 xgpio_regoffset(chip, i),
153 chip->gpio_state[index]);
154 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
155 index = xgpio_index(chip, i);
156 spin_lock_irqsave(&chip->gpio_lock[index], flags);
157 }
158 if (__test_and_clear_bit(i, mask)) {
159 offset = xgpio_offset(chip, i);
160 if (test_bit(i, bits))
161 chip->gpio_state[index] |= BIT(offset);
162 else
163 chip->gpio_state[index] &= ~BIT(offset);
164 }
165 }
166
Robert Hancock1ebd06872019-06-07 11:04:16 -0600167 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200168 xgpio_regoffset(chip, i), chip->gpio_state[index]);
169
170 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
171}
172
173/**
John Linn0bcb6062008-11-12 13:25:38 -0800174 * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
175 * @gc: Pointer to gpio_chip device structure.
176 * @gpio: GPIO signal number.
177 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100178 * Return:
179 * 0 - if direction of GPIO signals is set as input
180 * otherwise it returns negative error value.
John Linn0bcb6062008-11-12 13:25:38 -0800181 */
182static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
183{
184 unsigned long flags;
Linus Walleij097d88e2015-12-07 15:20:17 +0100185 struct xgpio_instance *chip = gpiochip_get_data(gc);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100186 int index = xgpio_index(chip, gpio);
187 int offset = xgpio_offset(chip, gpio);
John Linn0bcb6062008-11-12 13:25:38 -0800188
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100189 spin_lock_irqsave(&chip->gpio_lock[index], flags);
John Linn0bcb6062008-11-12 13:25:38 -0800190
191 /* Set the GPIO bit in shadow register and set direction as input */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100192 chip->gpio_dir[index] |= BIT(offset);
Robert Hancock1ebd06872019-06-07 11:04:16 -0600193 xgpio_writereg(chip->regs + XGPIO_TRI_OFFSET +
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100194 xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
John Linn0bcb6062008-11-12 13:25:38 -0800195
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100196 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
John Linn0bcb6062008-11-12 13:25:38 -0800197
198 return 0;
199}
200
201/**
202 * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
203 * @gc: Pointer to gpio_chip device structure.
204 * @gpio: GPIO signal number.
205 * @val: Value to be written to specified signal.
206 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100207 * This function sets the direction of specified GPIO signal as output.
208 *
209 * Return:
210 * If all GPIO signals of GPIO chip is configured as input then it returns
John Linn0bcb6062008-11-12 13:25:38 -0800211 * error otherwise it returns 0.
212 */
213static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
214{
215 unsigned long flags;
Linus Walleij097d88e2015-12-07 15:20:17 +0100216 struct xgpio_instance *chip = gpiochip_get_data(gc);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100217 int index = xgpio_index(chip, gpio);
218 int offset = xgpio_offset(chip, gpio);
John Linn0bcb6062008-11-12 13:25:38 -0800219
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100220 spin_lock_irqsave(&chip->gpio_lock[index], flags);
John Linn0bcb6062008-11-12 13:25:38 -0800221
222 /* Write state of GPIO signal */
223 if (val)
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100224 chip->gpio_state[index] |= BIT(offset);
John Linn0bcb6062008-11-12 13:25:38 -0800225 else
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100226 chip->gpio_state[index] &= ~BIT(offset);
Robert Hancock1ebd06872019-06-07 11:04:16 -0600227 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100228 xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
John Linn0bcb6062008-11-12 13:25:38 -0800229
230 /* Clear the GPIO bit in shadow register and set direction as output */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100231 chip->gpio_dir[index] &= ~BIT(offset);
Robert Hancock1ebd06872019-06-07 11:04:16 -0600232 xgpio_writereg(chip->regs + XGPIO_TRI_OFFSET +
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100233 xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
John Linn0bcb6062008-11-12 13:25:38 -0800234
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100235 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
John Linn0bcb6062008-11-12 13:25:38 -0800236
237 return 0;
238}
239
240/**
241 * xgpio_save_regs - Set initial values of GPIO pins
Robert Hancock1ebd06872019-06-07 11:04:16 -0600242 * @chip: Pointer to GPIO instance
John Linn0bcb6062008-11-12 13:25:38 -0800243 */
Robert Hancock1ebd06872019-06-07 11:04:16 -0600244static void xgpio_save_regs(struct xgpio_instance *chip)
John Linn0bcb6062008-11-12 13:25:38 -0800245{
Robert Hancock1ebd06872019-06-07 11:04:16 -0600246 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET, chip->gpio_state[0]);
247 xgpio_writereg(chip->regs + XGPIO_TRI_OFFSET, chip->gpio_dir[0]);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100248
249 if (!chip->gpio_width[1])
250 return;
251
Robert Hancock1ebd06872019-06-07 11:04:16 -0600252 xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET + XGPIO_CHANNEL_OFFSET,
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100253 chip->gpio_state[1]);
Robert Hancock1ebd06872019-06-07 11:04:16 -0600254 xgpio_writereg(chip->regs + XGPIO_TRI_OFFSET + XGPIO_CHANNEL_OFFSET,
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100255 chip->gpio_dir[1]);
John Linn0bcb6062008-11-12 13:25:38 -0800256}
257
258/**
259 * xgpio_of_probe - Probe method for the GPIO device.
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100260 * @pdev: pointer to the platform device
John Linn0bcb6062008-11-12 13:25:38 -0800261 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100262 * Return:
263 * It returns 0, if the driver is bound to the GPIO device, or
264 * a negative value if there is an error.
John Linn0bcb6062008-11-12 13:25:38 -0800265 */
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100266static int xgpio_probe(struct platform_device *pdev)
John Linn0bcb6062008-11-12 13:25:38 -0800267{
268 struct xgpio_instance *chip;
John Linn0bcb6062008-11-12 13:25:38 -0800269 int status = 0;
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100270 struct device_node *np = pdev->dev.of_node;
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100271 u32 is_dual;
John Linn0bcb6062008-11-12 13:25:38 -0800272
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100273 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
274 if (!chip)
John Linn0bcb6062008-11-12 13:25:38 -0800275 return -ENOMEM;
John Linn0bcb6062008-11-12 13:25:38 -0800276
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100277 platform_set_drvdata(pdev, chip);
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100278
John Linn0bcb6062008-11-12 13:25:38 -0800279 /* Update GPIO state shadow register with default value */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100280 of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0]);
John Linn0bcb6062008-11-12 13:25:38 -0800281
282 /* Update GPIO direction shadow register with default value */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100283 if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0]))
284 chip->gpio_dir[0] = 0xFFFFFFFF;
Michal Simek6f8bf502013-06-03 14:31:16 +0200285
Gernot Vormayr1b4c5a62014-09-24 00:58:45 +0200286 /*
287 * Check device node and parent device node for device width
288 * and assume default width of 32
289 */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100290 if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0]))
291 chip->gpio_width[0] = 32;
John Linn0bcb6062008-11-12 13:25:38 -0800292
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100293 spin_lock_init(&chip->gpio_lock[0]);
John Linn0bcb6062008-11-12 13:25:38 -0800294
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100295 if (of_property_read_u32(np, "xlnx,is-dual", &is_dual))
296 is_dual = 0;
297
298 if (is_dual) {
299 /* Update GPIO state shadow register with default value */
300 of_property_read_u32(np, "xlnx,dout-default-2",
301 &chip->gpio_state[1]);
302
303 /* Update GPIO direction shadow register with default value */
304 if (of_property_read_u32(np, "xlnx,tri-default-2",
305 &chip->gpio_dir[1]))
306 chip->gpio_dir[1] = 0xFFFFFFFF;
307
308 /*
309 * Check device node and parent device node for device width
310 * and assume default width of 32
311 */
312 if (of_property_read_u32(np, "xlnx,gpio2-width",
313 &chip->gpio_width[1]))
314 chip->gpio_width[1] = 32;
315
316 spin_lock_init(&chip->gpio_lock[1]);
317 }
318
Robert Hancock1ebd06872019-06-07 11:04:16 -0600319 chip->gc.base = -1;
320 chip->gc.ngpio = chip->gpio_width[0] + chip->gpio_width[1];
321 chip->gc.parent = &pdev->dev;
322 chip->gc.direction_input = xgpio_dir_in;
323 chip->gc.direction_output = xgpio_dir_out;
324 chip->gc.get = xgpio_get;
325 chip->gc.set = xgpio_set;
326 chip->gc.set_multiple = xgpio_set_multiple;
John Linn0bcb6062008-11-12 13:25:38 -0800327
Robert Hancock1ebd06872019-06-07 11:04:16 -0600328 chip->gc.label = dev_name(&pdev->dev);
John Linn0bcb6062008-11-12 13:25:38 -0800329
Robert Hancock1ebd06872019-06-07 11:04:16 -0600330 chip->regs = devm_platform_ioremap_resource(pdev, 0);
331 if (IS_ERR(chip->regs)) {
332 dev_err(&pdev->dev, "failed to ioremap memory resource\n");
333 return PTR_ERR(chip->regs);
334 }
335
336 xgpio_save_regs(chip);
337
338 status = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
John Linn0bcb6062008-11-12 13:25:38 -0800339 if (status) {
Robert Hancock1ebd06872019-06-07 11:04:16 -0600340 dev_err(&pdev->dev, "failed to add GPIO chip\n");
John Linn0bcb6062008-11-12 13:25:38 -0800341 return status;
342 }
Michal Simek74600ee2013-06-03 14:31:17 +0200343
John Linn0bcb6062008-11-12 13:25:38 -0800344 return 0;
345}
346
Jingoo Han9992bc92014-05-07 18:08:20 +0900347static const struct of_device_id xgpio_of_match[] = {
John Linn0bcb6062008-11-12 13:25:38 -0800348 { .compatible = "xlnx,xps-gpio-1.00.a", },
349 { /* end of list */ },
350};
351
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100352MODULE_DEVICE_TABLE(of, xgpio_of_match);
353
354static struct platform_driver xgpio_plat_driver = {
355 .probe = xgpio_probe,
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100356 .driver = {
357 .name = "gpio-xilinx",
358 .of_match_table = xgpio_of_match,
359 },
360};
361
John Linn0bcb6062008-11-12 13:25:38 -0800362static int __init xgpio_init(void)
363{
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100364 return platform_driver_register(&xgpio_plat_driver);
John Linn0bcb6062008-11-12 13:25:38 -0800365}
366
John Linn0bcb6062008-11-12 13:25:38 -0800367subsys_initcall(xgpio_init);
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100368
369static void __exit xgpio_exit(void)
370{
371 platform_driver_unregister(&xgpio_plat_driver);
372}
373module_exit(xgpio_exit);
John Linn0bcb6062008-11-12 13:25:38 -0800374
375MODULE_AUTHOR("Xilinx, Inc.");
376MODULE_DESCRIPTION("Xilinx GPIO driver");
377MODULE_LICENSE("GPL");