blob: 5828743a9f1dd54816fed32197778410a0198041 [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>
Srinivas Neeli65bbe532020-11-12 22:42:22 +05309#include <linux/clk.h>
John Linn0bcb6062008-11-12 13:25:38 -080010#include <linux/errno.h>
Srinivas Neeli8c669fe2020-11-12 22:42:20 +053011#include <linux/gpio/driver.h>
12#include <linux/init.h>
Srinivas Neelia32c7ca2021-01-29 19:56:48 +053013#include <linux/interrupt.h>
Srinivas Neeli8c669fe2020-11-12 22:42:20 +053014#include <linux/io.h>
Srinivas Neelia32c7ca2021-01-29 19:56:48 +053015#include <linux/irq.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -040016#include <linux/module.h>
John Linn0bcb6062008-11-12 13:25:38 -080017#include <linux/of_device.h>
18#include <linux/of_platform.h>
Srinivas Neeli26b04772021-01-29 19:56:49 +053019#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
John Linn0bcb6062008-11-12 13:25:38 -080021
22/* Register Offset Definitions */
23#define XGPIO_DATA_OFFSET (0x0) /* Data register */
24#define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */
25
Andy Shevchenko043aa3d2021-05-10 22:46:31 +030026#define XGPIO_CHANNEL0_OFFSET 0x0
27#define XGPIO_CHANNEL1_OFFSET 0x8
Michal Simek74600ee2013-06-03 14:31:17 +020028
Srinivas Neelia32c7ca2021-01-29 19:56:48 +053029#define XGPIO_GIER_OFFSET 0x11c /* Global Interrupt Enable */
30#define XGPIO_GIER_IE BIT(31)
31#define XGPIO_IPISR_OFFSET 0x120 /* IP Interrupt Status */
32#define XGPIO_IPIER_OFFSET 0x128 /* IP Interrupt Enable */
33
Michal Simek74600ee2013-06-03 14:31:17 +020034/* Read/Write access to the GPIO registers */
Ricardo Ribalda Delgadoc54c58b2014-12-17 16:51:10 +010035#if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
Michal Simekcc090d62013-06-03 14:31:18 +020036# define xgpio_readreg(offset) readl(offset)
37# define xgpio_writereg(offset, val) writel(val, offset)
38#else
39# define xgpio_readreg(offset) __raw_readl(offset)
40# define xgpio_writereg(offset, val) __raw_writel(val, offset)
41#endif
Michal Simek74600ee2013-06-03 14:31:17 +020042
43/**
44 * struct xgpio_instance - Stores information about GPIO device
Robert Hancock1ebd06872019-06-07 11:04:16 -060045 * @gc: GPIO chip
46 * @regs: register block
Michal Simek3c1b5c9b2015-05-04 16:37:16 +020047 * @gpio_width: GPIO width for every channel
Srinivas Neelia32c7ca2021-01-29 19:56:48 +053048 * @gpio_state: GPIO write state shadow register
49 * @gpio_last_irq_read: GPIO read state register from last interrupt
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +010050 * @gpio_dir: GPIO direction shadow register
51 * @gpio_lock: Lock used for synchronization
Srinivas Neelia32c7ca2021-01-29 19:56:48 +053052 * @irq: IRQ used by GPIO device
53 * @irqchip: IRQ chip
54 * @irq_enable: GPIO IRQ enable/disable bitfield
55 * @irq_rising_edge: GPIO IRQ rising edge enable/disable bitfield
56 * @irq_falling_edge: GPIO IRQ falling edge enable/disable bitfield
Srinivas Neeli65bbe532020-11-12 22:42:22 +053057 * @clk: clock resource for this driver
Michal Simek74600ee2013-06-03 14:31:17 +020058 */
John Linn0bcb6062008-11-12 13:25:38 -080059struct xgpio_instance {
Robert Hancock1ebd06872019-06-07 11:04:16 -060060 struct gpio_chip gc;
61 void __iomem *regs;
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010062 unsigned int gpio_width[2];
63 u32 gpio_state[2];
Srinivas Neelia32c7ca2021-01-29 19:56:48 +053064 u32 gpio_last_irq_read[2];
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010065 u32 gpio_dir[2];
Srinivas Neeli37ef3342021-01-29 19:56:47 +053066 spinlock_t gpio_lock; /* For serializing operations */
Srinivas Neelia32c7ca2021-01-29 19:56:48 +053067 int irq;
68 struct irq_chip irqchip;
69 u32 irq_enable[2];
70 u32 irq_rising_edge[2];
71 u32 irq_falling_edge[2];
Srinivas Neeli65bbe532020-11-12 22:42:22 +053072 struct clk *clk;
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +010073};
74
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010075static inline int xgpio_index(struct xgpio_instance *chip, int gpio)
76{
77 if (gpio >= chip->gpio_width[0])
78 return 1;
79
80 return 0;
81}
82
Andy Shevchenko043aa3d2021-05-10 22:46:31 +030083static inline int xgpio_regoffset(struct xgpio_instance *chip, int ch)
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010084{
Andy Shevchenko043aa3d2021-05-10 22:46:31 +030085 switch (ch) {
86 case 0:
87 return XGPIO_CHANNEL0_OFFSET;
88 case 1:
89 return XGPIO_CHANNEL1_OFFSET;
90 default:
91 return -EINVAL;
92 }
93}
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +010094
Andy Shevchenko043aa3d2021-05-10 22:46:31 +030095static inline u32 xgpio_read_chan(struct xgpio_instance *chip, int reg, int ch)
96{
97 return xgpio_readreg(chip->regs + reg + xgpio_regoffset(chip, ch));
98}
99
100static inline void xgpio_write_chan(struct xgpio_instance *chip, int reg, int ch, u32 v)
101{
102 xgpio_writereg(chip->regs + reg + xgpio_regoffset(chip, ch), v);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100103}
104
105static inline int xgpio_offset(struct xgpio_instance *chip, int gpio)
106{
107 if (xgpio_index(chip, gpio))
108 return gpio - chip->gpio_width[0];
109
110 return gpio;
111}
John Linn0bcb6062008-11-12 13:25:38 -0800112
113/**
114 * xgpio_get - Read the specified signal of the GPIO device.
115 * @gc: Pointer to gpio_chip device structure.
116 * @gpio: GPIO signal number.
117 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100118 * This function reads the specified signal of the GPIO device.
119 *
120 * Return:
121 * 0 if direction of GPIO signals is set as input otherwise it
122 * returns negative error value.
John Linn0bcb6062008-11-12 13:25:38 -0800123 */
124static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
125{
Linus Walleij097d88e2015-12-07 15:20:17 +0100126 struct xgpio_instance *chip = gpiochip_get_data(gc);
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300127 int index = xgpio_index(chip, gpio);
128 int offset = xgpio_offset(chip, gpio);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100129 u32 val;
John Linn0bcb6062008-11-12 13:25:38 -0800130
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300131 val = xgpio_read_chan(chip, XGPIO_DATA_OFFSET, index);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100132
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300133 return !!(val & BIT(offset));
John Linn0bcb6062008-11-12 13:25:38 -0800134}
135
136/**
137 * xgpio_set - Write the specified signal of the GPIO device.
138 * @gc: Pointer to gpio_chip device structure.
139 * @gpio: GPIO signal number.
140 * @val: Value to be written to specified signal.
141 *
142 * This function writes the specified value in to the specified signal of the
143 * GPIO device.
144 */
145static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
146{
147 unsigned long flags;
Linus Walleij097d88e2015-12-07 15:20:17 +0100148 struct xgpio_instance *chip = gpiochip_get_data(gc);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100149 int index = xgpio_index(chip, gpio);
150 int offset = xgpio_offset(chip, gpio);
John Linn0bcb6062008-11-12 13:25:38 -0800151
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530152 spin_lock_irqsave(&chip->gpio_lock, flags);
John Linn0bcb6062008-11-12 13:25:38 -0800153
154 /* Write to GPIO signal and set its direction to output */
155 if (val)
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100156 chip->gpio_state[index] |= BIT(offset);
John Linn0bcb6062008-11-12 13:25:38 -0800157 else
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100158 chip->gpio_state[index] &= ~BIT(offset);
Michal Simek74600ee2013-06-03 14:31:17 +0200159
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300160 xgpio_write_chan(chip, XGPIO_DATA_OFFSET, index, chip->gpio_state[index]);
John Linn0bcb6062008-11-12 13:25:38 -0800161
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530162 spin_unlock_irqrestore(&chip->gpio_lock, flags);
John Linn0bcb6062008-11-12 13:25:38 -0800163}
164
165/**
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200166 * xgpio_set_multiple - Write the specified signals of the GPIO device.
167 * @gc: Pointer to gpio_chip device structure.
168 * @mask: Mask of the GPIOS to modify.
169 * @bits: Value to be wrote on each GPIO
170 *
171 * This function writes the specified values into the specified signals of the
172 * GPIO devices.
173 */
174static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
175 unsigned long *bits)
176{
177 unsigned long flags;
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200178 struct xgpio_instance *chip = gpiochip_get_data(gc);
179 int index = xgpio_index(chip, 0);
180 int offset, i;
181
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530182 spin_lock_irqsave(&chip->gpio_lock, flags);
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200183
184 /* Write to GPIO signals */
185 for (i = 0; i < gc->ngpio; i++) {
186 if (*mask == 0)
187 break;
Paul Thomasc3afa802020-01-25 17:14:10 -0500188 /* Once finished with an index write it out to the register */
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200189 if (index != xgpio_index(chip, i)) {
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300190 xgpio_write_chan(chip, XGPIO_DATA_OFFSET, index,
191 chip->gpio_state[index]);
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530192 spin_unlock_irqrestore(&chip->gpio_lock, flags);
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200193 index = xgpio_index(chip, i);
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530194 spin_lock_irqsave(&chip->gpio_lock, flags);
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200195 }
196 if (__test_and_clear_bit(i, mask)) {
197 offset = xgpio_offset(chip, i);
198 if (test_bit(i, bits))
199 chip->gpio_state[index] |= BIT(offset);
200 else
201 chip->gpio_state[index] &= ~BIT(offset);
202 }
203 }
204
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300205 xgpio_write_chan(chip, XGPIO_DATA_OFFSET, index, chip->gpio_state[index]);
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200206
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530207 spin_unlock_irqrestore(&chip->gpio_lock, flags);
Iban Rodriguez8e7c1b802016-06-03 14:54:41 +0200208}
209
210/**
John Linn0bcb6062008-11-12 13:25:38 -0800211 * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
212 * @gc: Pointer to gpio_chip device structure.
213 * @gpio: GPIO signal number.
214 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100215 * Return:
216 * 0 - if direction of GPIO signals is set as input
217 * otherwise it returns negative error value.
John Linn0bcb6062008-11-12 13:25:38 -0800218 */
219static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
220{
221 unsigned long flags;
Linus Walleij097d88e2015-12-07 15:20:17 +0100222 struct xgpio_instance *chip = gpiochip_get_data(gc);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100223 int index = xgpio_index(chip, gpio);
224 int offset = xgpio_offset(chip, gpio);
John Linn0bcb6062008-11-12 13:25:38 -0800225
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530226 spin_lock_irqsave(&chip->gpio_lock, flags);
John Linn0bcb6062008-11-12 13:25:38 -0800227
228 /* Set the GPIO bit in shadow register and set direction as input */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100229 chip->gpio_dir[index] |= BIT(offset);
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300230 xgpio_write_chan(chip, XGPIO_TRI_OFFSET, index, chip->gpio_dir[index]);
John Linn0bcb6062008-11-12 13:25:38 -0800231
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530232 spin_unlock_irqrestore(&chip->gpio_lock, flags);
John Linn0bcb6062008-11-12 13:25:38 -0800233
234 return 0;
235}
236
237/**
238 * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
239 * @gc: Pointer to gpio_chip device structure.
240 * @gpio: GPIO signal number.
241 * @val: Value to be written to specified signal.
242 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100243 * This function sets the direction of specified GPIO signal as output.
244 *
245 * Return:
246 * If all GPIO signals of GPIO chip is configured as input then it returns
John Linn0bcb6062008-11-12 13:25:38 -0800247 * error otherwise it returns 0.
248 */
249static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
250{
251 unsigned long flags;
Linus Walleij097d88e2015-12-07 15:20:17 +0100252 struct xgpio_instance *chip = gpiochip_get_data(gc);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100253 int index = xgpio_index(chip, gpio);
254 int offset = xgpio_offset(chip, gpio);
John Linn0bcb6062008-11-12 13:25:38 -0800255
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530256 spin_lock_irqsave(&chip->gpio_lock, flags);
John Linn0bcb6062008-11-12 13:25:38 -0800257
258 /* Write state of GPIO signal */
259 if (val)
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100260 chip->gpio_state[index] |= BIT(offset);
John Linn0bcb6062008-11-12 13:25:38 -0800261 else
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100262 chip->gpio_state[index] &= ~BIT(offset);
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300263 xgpio_write_chan(chip, XGPIO_DATA_OFFSET, index, chip->gpio_state[index]);
John Linn0bcb6062008-11-12 13:25:38 -0800264
265 /* Clear the GPIO bit in shadow register and set direction as output */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100266 chip->gpio_dir[index] &= ~BIT(offset);
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300267 xgpio_write_chan(chip, XGPIO_TRI_OFFSET, index, chip->gpio_dir[index]);
John Linn0bcb6062008-11-12 13:25:38 -0800268
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530269 spin_unlock_irqrestore(&chip->gpio_lock, flags);
John Linn0bcb6062008-11-12 13:25:38 -0800270
271 return 0;
272}
273
274/**
275 * xgpio_save_regs - Set initial values of GPIO pins
Robert Hancock1ebd06872019-06-07 11:04:16 -0600276 * @chip: Pointer to GPIO instance
John Linn0bcb6062008-11-12 13:25:38 -0800277 */
Robert Hancock1ebd06872019-06-07 11:04:16 -0600278static void xgpio_save_regs(struct xgpio_instance *chip)
John Linn0bcb6062008-11-12 13:25:38 -0800279{
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300280 xgpio_write_chan(chip, XGPIO_DATA_OFFSET, 0, chip->gpio_state[0]);
281 xgpio_write_chan(chip, XGPIO_TRI_OFFSET, 0, chip->gpio_dir[0]);
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100282
283 if (!chip->gpio_width[1])
284 return;
285
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300286 xgpio_write_chan(chip, XGPIO_DATA_OFFSET, 1, chip->gpio_state[1]);
287 xgpio_write_chan(chip, XGPIO_TRI_OFFSET, 1, chip->gpio_dir[1]);
John Linn0bcb6062008-11-12 13:25:38 -0800288}
289
Srinivas Neeli26b04772021-01-29 19:56:49 +0530290static int xgpio_request(struct gpio_chip *chip, unsigned int offset)
291{
292 int ret;
293
294 ret = pm_runtime_get_sync(chip->parent);
295 /*
296 * If the device is already active pm_runtime_get() will return 1 on
297 * success, but gpio_request still needs to return 0.
298 */
299 return ret < 0 ? ret : 0;
300}
301
302static void xgpio_free(struct gpio_chip *chip, unsigned int offset)
303{
304 pm_runtime_put(chip->parent);
305}
306
307static int __maybe_unused xgpio_suspend(struct device *dev)
308{
309 struct xgpio_instance *gpio = dev_get_drvdata(dev);
310 struct irq_data *data = irq_get_irq_data(gpio->irq);
311
312 if (!data) {
313 dev_err(dev, "irq_get_irq_data() failed\n");
314 return -EINVAL;
315 }
316
317 if (!irqd_is_wakeup_set(data))
318 return pm_runtime_force_suspend(dev);
319
320 return 0;
321}
322
John Linn0bcb6062008-11-12 13:25:38 -0800323/**
Srinivas Neeli0230a412020-11-12 22:42:25 +0530324 * xgpio_remove - Remove method for the GPIO device.
325 * @pdev: pointer to the platform device
326 *
327 * This function remove gpiochips and frees all the allocated resources.
328 *
329 * Return: 0 always
330 */
331static int xgpio_remove(struct platform_device *pdev)
332{
333 struct xgpio_instance *gpio = platform_get_drvdata(pdev);
334
Srinivas Neeli26b04772021-01-29 19:56:49 +0530335 pm_runtime_get_sync(&pdev->dev);
336 pm_runtime_put_noidle(&pdev->dev);
337 pm_runtime_disable(&pdev->dev);
Srinivas Neeli0230a412020-11-12 22:42:25 +0530338 clk_disable_unprepare(gpio->clk);
339
340 return 0;
341}
342
343/**
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530344 * xgpio_irq_ack - Acknowledge a child GPIO interrupt.
345 * @irq_data: per IRQ and chip data passed down to chip functions
346 * This currently does nothing, but irq_ack is unconditionally called by
347 * handle_edge_irq and therefore must be defined.
348 */
349static void xgpio_irq_ack(struct irq_data *irq_data)
350{
351}
352
Srinivas Neeli26b04772021-01-29 19:56:49 +0530353static int __maybe_unused xgpio_resume(struct device *dev)
354{
355 struct xgpio_instance *gpio = dev_get_drvdata(dev);
356 struct irq_data *data = irq_get_irq_data(gpio->irq);
357
358 if (!data) {
359 dev_err(dev, "irq_get_irq_data() failed\n");
360 return -EINVAL;
361 }
362
363 if (!irqd_is_wakeup_set(data))
364 return pm_runtime_force_resume(dev);
365
366 return 0;
367}
368
369static int __maybe_unused xgpio_runtime_suspend(struct device *dev)
370{
371 struct platform_device *pdev = to_platform_device(dev);
372 struct xgpio_instance *gpio = platform_get_drvdata(pdev);
373
374 clk_disable(gpio->clk);
375
376 return 0;
377}
378
379static int __maybe_unused xgpio_runtime_resume(struct device *dev)
380{
381 struct platform_device *pdev = to_platform_device(dev);
382 struct xgpio_instance *gpio = platform_get_drvdata(pdev);
383
384 return clk_enable(gpio->clk);
385}
386
387static const struct dev_pm_ops xgpio_dev_pm_ops = {
388 SET_SYSTEM_SLEEP_PM_OPS(xgpio_suspend, xgpio_resume)
389 SET_RUNTIME_PM_OPS(xgpio_runtime_suspend,
390 xgpio_runtime_resume, NULL)
391};
392
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530393/**
394 * xgpio_irq_mask - Write the specified signal of the GPIO device.
395 * @irq_data: per IRQ and chip data passed down to chip functions
396 */
397static void xgpio_irq_mask(struct irq_data *irq_data)
398{
399 unsigned long flags;
400 struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
401 int irq_offset = irqd_to_hwirq(irq_data);
402 int index = xgpio_index(chip, irq_offset);
403 int offset = xgpio_offset(chip, irq_offset);
404
405 spin_lock_irqsave(&chip->gpio_lock, flags);
406
407 chip->irq_enable[index] &= ~BIT(offset);
408
409 if (!chip->irq_enable[index]) {
410 /* Disable per channel interrupt */
411 u32 temp = xgpio_readreg(chip->regs + XGPIO_IPIER_OFFSET);
412
413 temp &= ~BIT(index);
414 xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, temp);
415 }
416 spin_unlock_irqrestore(&chip->gpio_lock, flags);
417}
418
419/**
420 * xgpio_irq_unmask - Write the specified signal of the GPIO device.
421 * @irq_data: per IRQ and chip data passed down to chip functions
422 */
423static void xgpio_irq_unmask(struct irq_data *irq_data)
424{
425 unsigned long flags;
426 struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
427 int irq_offset = irqd_to_hwirq(irq_data);
428 int index = xgpio_index(chip, irq_offset);
429 int offset = xgpio_offset(chip, irq_offset);
430 u32 old_enable = chip->irq_enable[index];
431
432 spin_lock_irqsave(&chip->gpio_lock, flags);
433
434 chip->irq_enable[index] |= BIT(offset);
435
436 if (!old_enable) {
437 /* Clear any existing per-channel interrupts */
438 u32 val = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET) &
439 BIT(index);
440
441 if (val)
442 xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, val);
443
444 /* Update GPIO IRQ read data before enabling interrupt*/
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300445 val = xgpio_read_chan(chip, XGPIO_DATA_OFFSET, index);
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530446 chip->gpio_last_irq_read[index] = val;
447
448 /* Enable per channel interrupt */
449 val = xgpio_readreg(chip->regs + XGPIO_IPIER_OFFSET);
450 val |= BIT(index);
451 xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, val);
452 }
453
454 spin_unlock_irqrestore(&chip->gpio_lock, flags);
455}
456
457/**
458 * xgpio_set_irq_type - Write the specified signal of the GPIO device.
459 * @irq_data: Per IRQ and chip data passed down to chip functions
460 * @type: Interrupt type that is to be set for the gpio pin
461 *
462 * Return:
463 * 0 if interrupt type is supported otherwise -EINVAL
464 */
465static int xgpio_set_irq_type(struct irq_data *irq_data, unsigned int type)
466{
467 struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
468 int irq_offset = irqd_to_hwirq(irq_data);
469 int index = xgpio_index(chip, irq_offset);
470 int offset = xgpio_offset(chip, irq_offset);
471
472 /*
473 * The Xilinx GPIO hardware provides a single interrupt status
474 * indication for any state change in a given GPIO channel (bank).
475 * Therefore, only rising edge or falling edge triggers are
476 * supported.
477 */
478 switch (type & IRQ_TYPE_SENSE_MASK) {
479 case IRQ_TYPE_EDGE_BOTH:
480 chip->irq_rising_edge[index] |= BIT(offset);
481 chip->irq_falling_edge[index] |= BIT(offset);
482 break;
483 case IRQ_TYPE_EDGE_RISING:
484 chip->irq_rising_edge[index] |= BIT(offset);
485 chip->irq_falling_edge[index] &= ~BIT(offset);
486 break;
487 case IRQ_TYPE_EDGE_FALLING:
488 chip->irq_rising_edge[index] &= ~BIT(offset);
489 chip->irq_falling_edge[index] |= BIT(offset);
490 break;
491 default:
492 return -EINVAL;
493 }
494
495 irq_set_handler_locked(irq_data, handle_edge_irq);
496 return 0;
497}
498
499/**
500 * xgpio_irqhandler - Gpio interrupt service routine
501 * @desc: Pointer to interrupt description
502 */
503static void xgpio_irqhandler(struct irq_desc *desc)
504{
505 struct xgpio_instance *chip = irq_desc_get_handler_data(desc);
506 struct irq_chip *irqchip = irq_desc_get_chip(desc);
507 u32 num_channels = chip->gpio_width[1] ? 2 : 1;
508 u32 offset = 0, index;
509 u32 status = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET);
510
511 xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, status);
512
513 chained_irq_enter(irqchip, desc);
514 for (index = 0; index < num_channels; index++) {
515 if ((status & BIT(index))) {
516 unsigned long rising_events, falling_events, all_events;
517 unsigned long flags;
518 u32 data, bit;
519 unsigned int irq;
520
521 spin_lock_irqsave(&chip->gpio_lock, flags);
Andy Shevchenko043aa3d2021-05-10 22:46:31 +0300522 data = xgpio_read_chan(chip, XGPIO_DATA_OFFSET, index);
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530523 rising_events = data &
524 ~chip->gpio_last_irq_read[index] &
525 chip->irq_enable[index] &
526 chip->irq_rising_edge[index];
527 falling_events = ~data &
528 chip->gpio_last_irq_read[index] &
529 chip->irq_enable[index] &
530 chip->irq_falling_edge[index];
531 dev_dbg(chip->gc.parent,
532 "IRQ chan %u rising 0x%lx falling 0x%lx\n",
533 index, rising_events, falling_events);
534 all_events = rising_events | falling_events;
535 chip->gpio_last_irq_read[index] = data;
536 spin_unlock_irqrestore(&chip->gpio_lock, flags);
537
538 for_each_set_bit(bit, &all_events, 32) {
539 irq = irq_find_mapping(chip->gc.irq.domain,
540 offset + bit);
541 generic_handle_irq(irq);
542 }
543 }
544 offset += chip->gpio_width[index];
545 }
546
547 chained_irq_exit(irqchip, desc);
548}
549
550/**
John Linn0bcb6062008-11-12 13:25:38 -0800551 * xgpio_of_probe - Probe method for the GPIO device.
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100552 * @pdev: pointer to the platform device
John Linn0bcb6062008-11-12 13:25:38 -0800553 *
Ricardo Ribalda Delgado4ae798f2014-12-17 16:51:11 +0100554 * Return:
555 * It returns 0, if the driver is bound to the GPIO device, or
556 * a negative value if there is an error.
John Linn0bcb6062008-11-12 13:25:38 -0800557 */
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100558static int xgpio_probe(struct platform_device *pdev)
John Linn0bcb6062008-11-12 13:25:38 -0800559{
560 struct xgpio_instance *chip;
John Linn0bcb6062008-11-12 13:25:38 -0800561 int status = 0;
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100562 struct device_node *np = pdev->dev.of_node;
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530563 u32 is_dual = 0;
564 u32 cells = 2;
565 struct gpio_irq_chip *girq;
566 u32 temp;
John Linn0bcb6062008-11-12 13:25:38 -0800567
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100568 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
569 if (!chip)
John Linn0bcb6062008-11-12 13:25:38 -0800570 return -ENOMEM;
John Linn0bcb6062008-11-12 13:25:38 -0800571
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100572 platform_set_drvdata(pdev, chip);
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100573
John Linn0bcb6062008-11-12 13:25:38 -0800574 /* Update GPIO state shadow register with default value */
Srinivas Neelibea67ae2020-11-12 22:42:27 +0530575 if (of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0]))
576 chip->gpio_state[0] = 0x0;
John Linn0bcb6062008-11-12 13:25:38 -0800577
578 /* Update GPIO direction shadow register with default value */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100579 if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0]))
580 chip->gpio_dir[0] = 0xFFFFFFFF;
Michal Simek6f8bf502013-06-03 14:31:16 +0200581
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530582 /* Update cells with gpio-cells value */
583 if (of_property_read_u32(np, "#gpio-cells", &cells))
584 dev_dbg(&pdev->dev, "Missing gpio-cells property\n");
585
586 if (cells != 2) {
587 dev_err(&pdev->dev, "#gpio-cells mismatch\n");
588 return -EINVAL;
589 }
590
Gernot Vormayr1b4c5a62014-09-24 00:58:45 +0200591 /*
592 * Check device node and parent device node for device width
593 * and assume default width of 32
594 */
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100595 if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0]))
596 chip->gpio_width[0] = 32;
John Linn0bcb6062008-11-12 13:25:38 -0800597
Srinivas Neeli6e551bf2021-01-29 19:56:50 +0530598 if (chip->gpio_width[0] > 32)
599 return -EINVAL;
600
Srinivas Neeli37ef3342021-01-29 19:56:47 +0530601 spin_lock_init(&chip->gpio_lock);
John Linn0bcb6062008-11-12 13:25:38 -0800602
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100603 if (of_property_read_u32(np, "xlnx,is-dual", &is_dual))
604 is_dual = 0;
605
606 if (is_dual) {
607 /* Update GPIO state shadow register with default value */
Srinivas Neelibea67ae2020-11-12 22:42:27 +0530608 if (of_property_read_u32(np, "xlnx,dout-default-2",
609 &chip->gpio_state[1]))
610 chip->gpio_state[1] = 0x0;
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100611
612 /* Update GPIO direction shadow register with default value */
613 if (of_property_read_u32(np, "xlnx,tri-default-2",
614 &chip->gpio_dir[1]))
615 chip->gpio_dir[1] = 0xFFFFFFFF;
616
617 /*
618 * Check device node and parent device node for device width
619 * and assume default width of 32
620 */
621 if (of_property_read_u32(np, "xlnx,gpio2-width",
622 &chip->gpio_width[1]))
623 chip->gpio_width[1] = 32;
624
Srinivas Neeli6e551bf2021-01-29 19:56:50 +0530625 if (chip->gpio_width[1] > 32)
626 return -EINVAL;
Ricardo Ribalda Delgado1d6902d2014-12-17 16:51:12 +0100627 }
628
Robert Hancock1ebd06872019-06-07 11:04:16 -0600629 chip->gc.base = -1;
630 chip->gc.ngpio = chip->gpio_width[0] + chip->gpio_width[1];
631 chip->gc.parent = &pdev->dev;
632 chip->gc.direction_input = xgpio_dir_in;
633 chip->gc.direction_output = xgpio_dir_out;
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530634 chip->gc.of_gpio_n_cells = cells;
Robert Hancock1ebd06872019-06-07 11:04:16 -0600635 chip->gc.get = xgpio_get;
636 chip->gc.set = xgpio_set;
Srinivas Neeli26b04772021-01-29 19:56:49 +0530637 chip->gc.request = xgpio_request;
638 chip->gc.free = xgpio_free;
Robert Hancock1ebd06872019-06-07 11:04:16 -0600639 chip->gc.set_multiple = xgpio_set_multiple;
John Linn0bcb6062008-11-12 13:25:38 -0800640
Robert Hancock1ebd06872019-06-07 11:04:16 -0600641 chip->gc.label = dev_name(&pdev->dev);
John Linn0bcb6062008-11-12 13:25:38 -0800642
Robert Hancock1ebd06872019-06-07 11:04:16 -0600643 chip->regs = devm_platform_ioremap_resource(pdev, 0);
644 if (IS_ERR(chip->regs)) {
645 dev_err(&pdev->dev, "failed to ioremap memory resource\n");
646 return PTR_ERR(chip->regs);
647 }
648
Srinivas Neeli65bbe532020-11-12 22:42:22 +0530649 chip->clk = devm_clk_get_optional(&pdev->dev, NULL);
Srinivas Neeli45c52772021-01-29 19:56:46 +0530650 if (IS_ERR(chip->clk))
651 return dev_err_probe(&pdev->dev, PTR_ERR(chip->clk), "input clock not found.\n");
Srinivas Neeli65bbe532020-11-12 22:42:22 +0530652
653 status = clk_prepare_enable(chip->clk);
654 if (status < 0) {
655 dev_err(&pdev->dev, "Failed to prepare clk\n");
656 return status;
657 }
Srinivas Neeli26b04772021-01-29 19:56:49 +0530658 pm_runtime_get_noresume(&pdev->dev);
659 pm_runtime_set_active(&pdev->dev);
660 pm_runtime_enable(&pdev->dev);
Srinivas Neeli65bbe532020-11-12 22:42:22 +0530661
Robert Hancock1ebd06872019-06-07 11:04:16 -0600662 xgpio_save_regs(chip);
663
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530664 chip->irq = platform_get_irq_optional(pdev, 0);
665 if (chip->irq <= 0)
666 goto skip_irq;
667
668 chip->irqchip.name = "gpio-xilinx";
669 chip->irqchip.irq_ack = xgpio_irq_ack;
670 chip->irqchip.irq_mask = xgpio_irq_mask;
671 chip->irqchip.irq_unmask = xgpio_irq_unmask;
672 chip->irqchip.irq_set_type = xgpio_set_irq_type;
673
674 /* Disable per-channel interrupts */
675 xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, 0);
676 /* Clear any existing per-channel interrupts */
677 temp = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET);
678 xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, temp);
679 /* Enable global interrupts */
680 xgpio_writereg(chip->regs + XGPIO_GIER_OFFSET, XGPIO_GIER_IE);
681
682 girq = &chip->gc.irq;
683 girq->chip = &chip->irqchip;
684 girq->parent_handler = xgpio_irqhandler;
685 girq->num_parents = 1;
686 girq->parents = devm_kcalloc(&pdev->dev, 1,
687 sizeof(*girq->parents),
688 GFP_KERNEL);
689 if (!girq->parents) {
690 status = -ENOMEM;
Srinivas Neeli26b04772021-01-29 19:56:49 +0530691 goto err_pm_put;
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530692 }
693 girq->parents[0] = chip->irq;
694 girq->default_type = IRQ_TYPE_NONE;
695 girq->handler = handle_bad_irq;
696
697skip_irq:
Robert Hancock1ebd06872019-06-07 11:04:16 -0600698 status = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
John Linn0bcb6062008-11-12 13:25:38 -0800699 if (status) {
Robert Hancock1ebd06872019-06-07 11:04:16 -0600700 dev_err(&pdev->dev, "failed to add GPIO chip\n");
Srinivas Neeli26b04772021-01-29 19:56:49 +0530701 goto err_pm_put;
John Linn0bcb6062008-11-12 13:25:38 -0800702 }
Michal Simek74600ee2013-06-03 14:31:17 +0200703
Srinivas Neeli26b04772021-01-29 19:56:49 +0530704 pm_runtime_put(&pdev->dev);
John Linn0bcb6062008-11-12 13:25:38 -0800705 return 0;
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530706
Srinivas Neeli26b04772021-01-29 19:56:49 +0530707err_pm_put:
708 pm_runtime_disable(&pdev->dev);
709 pm_runtime_put_noidle(&pdev->dev);
Srinivas Neelia32c7ca2021-01-29 19:56:48 +0530710 clk_disable_unprepare(chip->clk);
711 return status;
John Linn0bcb6062008-11-12 13:25:38 -0800712}
713
Jingoo Han9992bc92014-05-07 18:08:20 +0900714static const struct of_device_id xgpio_of_match[] = {
John Linn0bcb6062008-11-12 13:25:38 -0800715 { .compatible = "xlnx,xps-gpio-1.00.a", },
716 { /* end of list */ },
717};
718
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100719MODULE_DEVICE_TABLE(of, xgpio_of_match);
720
721static struct platform_driver xgpio_plat_driver = {
722 .probe = xgpio_probe,
Srinivas Neeli0230a412020-11-12 22:42:25 +0530723 .remove = xgpio_remove,
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100724 .driver = {
725 .name = "gpio-xilinx",
726 .of_match_table = xgpio_of_match,
Srinivas Neeli26b04772021-01-29 19:56:49 +0530727 .pm = &xgpio_dev_pm_ops,
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100728 },
729};
730
John Linn0bcb6062008-11-12 13:25:38 -0800731static int __init xgpio_init(void)
732{
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100733 return platform_driver_register(&xgpio_plat_driver);
John Linn0bcb6062008-11-12 13:25:38 -0800734}
735
John Linn0bcb6062008-11-12 13:25:38 -0800736subsys_initcall(xgpio_init);
Ricardo Ribalda Delgado749564f2014-12-17 16:51:09 +0100737
738static void __exit xgpio_exit(void)
739{
740 platform_driver_unregister(&xgpio_plat_driver);
741}
742module_exit(xgpio_exit);
John Linn0bcb6062008-11-12 13:25:38 -0800743
744MODULE_AUTHOR("Xilinx, Inc.");
745MODULE_DESCRIPTION("Xilinx GPIO driver");
746MODULE_LICENSE("GPL");