blob: fb93b35ab19e161c1bf812d723620ebf8278b0fc [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Harini Katakam3242ba12014-07-08 16:32:35 +05302/*
3 * Xilinx Zynq GPIO device driver
4 *
5 * Copyright (C) 2009 - 2014 Xilinx, Inc.
Harini Katakam3242ba12014-07-08 16:32:35 +05306 */
7
8#include <linux/bitops.h>
9#include <linux/clk.h>
10#include <linux/gpio/driver.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
Glenn Langedockfdcfec112020-06-17 17:07:21 +053013#include <linux/spinlock.h>
Harini Katakam3242ba12014-07-08 16:32:35 +053014#include <linux/io.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/pm_runtime.h>
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +053018#include <linux/of.h>
Harini Katakam3242ba12014-07-08 16:32:35 +053019
20#define DRIVER_NAME "zynq-gpio"
21
22/* Maximum banks */
23#define ZYNQ_GPIO_MAX_BANK 4
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +053024#define ZYNQMP_GPIO_MAX_BANK 6
Harini Katakam3242ba12014-07-08 16:32:35 +053025
26#define ZYNQ_GPIO_BANK0_NGPIO 32
27#define ZYNQ_GPIO_BANK1_NGPIO 22
28#define ZYNQ_GPIO_BANK2_NGPIO 32
29#define ZYNQ_GPIO_BANK3_NGPIO 32
30
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +053031#define ZYNQMP_GPIO_BANK0_NGPIO 26
32#define ZYNQMP_GPIO_BANK1_NGPIO 26
33#define ZYNQMP_GPIO_BANK2_NGPIO 26
34#define ZYNQMP_GPIO_BANK3_NGPIO 32
35#define ZYNQMP_GPIO_BANK4_NGPIO 32
36#define ZYNQMP_GPIO_BANK5_NGPIO 32
Harini Katakam3242ba12014-07-08 16:32:35 +053037
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +053038#define ZYNQ_GPIO_NR_GPIOS 118
39#define ZYNQMP_GPIO_NR_GPIOS 174
40
41#define ZYNQ_GPIO_BANK0_PIN_MIN(str) 0
42#define ZYNQ_GPIO_BANK0_PIN_MAX(str) (ZYNQ_GPIO_BANK0_PIN_MIN(str) + \
43 ZYNQ##str##_GPIO_BANK0_NGPIO - 1)
44#define ZYNQ_GPIO_BANK1_PIN_MIN(str) (ZYNQ_GPIO_BANK0_PIN_MAX(str) + 1)
45#define ZYNQ_GPIO_BANK1_PIN_MAX(str) (ZYNQ_GPIO_BANK1_PIN_MIN(str) + \
46 ZYNQ##str##_GPIO_BANK1_NGPIO - 1)
47#define ZYNQ_GPIO_BANK2_PIN_MIN(str) (ZYNQ_GPIO_BANK1_PIN_MAX(str) + 1)
48#define ZYNQ_GPIO_BANK2_PIN_MAX(str) (ZYNQ_GPIO_BANK2_PIN_MIN(str) + \
49 ZYNQ##str##_GPIO_BANK2_NGPIO - 1)
50#define ZYNQ_GPIO_BANK3_PIN_MIN(str) (ZYNQ_GPIO_BANK2_PIN_MAX(str) + 1)
51#define ZYNQ_GPIO_BANK3_PIN_MAX(str) (ZYNQ_GPIO_BANK3_PIN_MIN(str) + \
52 ZYNQ##str##_GPIO_BANK3_NGPIO - 1)
53#define ZYNQ_GPIO_BANK4_PIN_MIN(str) (ZYNQ_GPIO_BANK3_PIN_MAX(str) + 1)
54#define ZYNQ_GPIO_BANK4_PIN_MAX(str) (ZYNQ_GPIO_BANK4_PIN_MIN(str) + \
55 ZYNQ##str##_GPIO_BANK4_NGPIO - 1)
56#define ZYNQ_GPIO_BANK5_PIN_MIN(str) (ZYNQ_GPIO_BANK4_PIN_MAX(str) + 1)
57#define ZYNQ_GPIO_BANK5_PIN_MAX(str) (ZYNQ_GPIO_BANK5_PIN_MIN(str) + \
58 ZYNQ##str##_GPIO_BANK5_NGPIO - 1)
Harini Katakam3242ba12014-07-08 16:32:35 +053059
Harini Katakam3242ba12014-07-08 16:32:35 +053060/* Register offsets for the GPIO device */
61/* LSW Mask & Data -WO */
62#define ZYNQ_GPIO_DATA_LSW_OFFSET(BANK) (0x000 + (8 * BANK))
63/* MSW Mask & Data -WO */
64#define ZYNQ_GPIO_DATA_MSW_OFFSET(BANK) (0x004 + (8 * BANK))
65/* Data Register-RW */
Swapna Manupati06aa0902017-08-07 13:01:57 +020066#define ZYNQ_GPIO_DATA_OFFSET(BANK) (0x040 + (4 * BANK))
Harini Katakam3242ba12014-07-08 16:32:35 +053067#define ZYNQ_GPIO_DATA_RO_OFFSET(BANK) (0x060 + (4 * BANK))
68/* Direction mode reg-RW */
69#define ZYNQ_GPIO_DIRM_OFFSET(BANK) (0x204 + (0x40 * BANK))
70/* Output enable reg-RW */
71#define ZYNQ_GPIO_OUTEN_OFFSET(BANK) (0x208 + (0x40 * BANK))
72/* Interrupt mask reg-RO */
73#define ZYNQ_GPIO_INTMASK_OFFSET(BANK) (0x20C + (0x40 * BANK))
74/* Interrupt enable reg-WO */
75#define ZYNQ_GPIO_INTEN_OFFSET(BANK) (0x210 + (0x40 * BANK))
76/* Interrupt disable reg-WO */
77#define ZYNQ_GPIO_INTDIS_OFFSET(BANK) (0x214 + (0x40 * BANK))
78/* Interrupt status reg-RO */
79#define ZYNQ_GPIO_INTSTS_OFFSET(BANK) (0x218 + (0x40 * BANK))
80/* Interrupt type reg-RW */
81#define ZYNQ_GPIO_INTTYPE_OFFSET(BANK) (0x21C + (0x40 * BANK))
82/* Interrupt polarity reg-RW */
83#define ZYNQ_GPIO_INTPOL_OFFSET(BANK) (0x220 + (0x40 * BANK))
84/* Interrupt on any, reg-RW */
85#define ZYNQ_GPIO_INTANY_OFFSET(BANK) (0x224 + (0x40 * BANK))
86
87/* Disable all interrupts mask */
88#define ZYNQ_GPIO_IXR_DISABLE_ALL 0xFFFFFFFF
89
90/* Mid pin number of a bank */
91#define ZYNQ_GPIO_MID_PIN_NUM 16
92
93/* GPIO upper 16 bit mask */
94#define ZYNQ_GPIO_UPPER_MASK 0xFFFF0000
95
Soren Brinkmann3638bd42017-06-08 10:32:07 -070096/* set to differentiate zynq from zynqmp, 0=zynqmp, 1=zynq */
97#define ZYNQ_GPIO_QUIRK_IS_ZYNQ BIT(0)
Swapna Manupati06aa0902017-08-07 13:01:57 +020098#define GPIO_QUIRK_DATA_RO_BUG BIT(1)
Nava kishore Mannee3296f12016-09-23 16:56:58 +053099
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200100struct gpio_regs {
101 u32 datamsw[ZYNQMP_GPIO_MAX_BANK];
102 u32 datalsw[ZYNQMP_GPIO_MAX_BANK];
103 u32 dirm[ZYNQMP_GPIO_MAX_BANK];
104 u32 outen[ZYNQMP_GPIO_MAX_BANK];
105 u32 int_en[ZYNQMP_GPIO_MAX_BANK];
106 u32 int_dis[ZYNQMP_GPIO_MAX_BANK];
107 u32 int_type[ZYNQMP_GPIO_MAX_BANK];
108 u32 int_polarity[ZYNQMP_GPIO_MAX_BANK];
109 u32 int_any[ZYNQMP_GPIO_MAX_BANK];
110};
Michal Simekeb73d6e2017-08-07 13:01:59 +0200111
Harini Katakam3242ba12014-07-08 16:32:35 +0530112/**
113 * struct zynq_gpio - gpio device private data structure
114 * @chip: instance of the gpio_chip
115 * @base_addr: base address of the GPIO device
116 * @clk: clock resource for this controller
Ezra Savard59e22112014-08-29 10:58:46 -0700117 * @irq: interrupt for the GPIO device
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530118 * @p_data: pointer to platform data
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200119 * @context: context registers
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530120 * @dirlock: lock used for direction in/out synchronization
Harini Katakam3242ba12014-07-08 16:32:35 +0530121 */
122struct zynq_gpio {
123 struct gpio_chip chip;
124 void __iomem *base_addr;
125 struct clk *clk;
Ezra Savard59e22112014-08-29 10:58:46 -0700126 int irq;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530127 const struct zynq_platform_data *p_data;
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200128 struct gpio_regs context;
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530129 spinlock_t dirlock; /* lock */
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530130};
131
132/**
133 * struct zynq_platform_data - zynq gpio platform data structure
134 * @label: string to store in gpio->label
Nava kishore Manne6ae51042017-08-07 13:01:58 +0200135 * @quirks: Flags is used to identify the platform
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530136 * @ngpio: max number of gpio pins
137 * @max_bank: maximum number of gpio banks
138 * @bank_min: this array represents bank's min pin
139 * @bank_max: this array represents bank's max pin
Nava kishore Manne6ae51042017-08-07 13:01:58 +0200140 */
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530141struct zynq_platform_data {
142 const char *label;
Nava kishore Mannee3296f12016-09-23 16:56:58 +0530143 u32 quirks;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530144 u16 ngpio;
145 int max_bank;
146 int bank_min[ZYNQMP_GPIO_MAX_BANK];
147 int bank_max[ZYNQMP_GPIO_MAX_BANK];
Harini Katakam3242ba12014-07-08 16:32:35 +0530148};
149
Lars-Peter Clausen6dd85952014-07-18 11:52:13 +0200150static struct irq_chip zynq_gpio_level_irqchip;
151static struct irq_chip zynq_gpio_edge_irqchip;
Linus Walleijfa9795d2015-08-27 14:26:46 +0200152
Harini Katakam3242ba12014-07-08 16:32:35 +0530153/**
Soren Brinkmann3638bd42017-06-08 10:32:07 -0700154 * zynq_gpio_is_zynq - test if HW is zynq or zynqmp
155 * @gpio: Pointer to driver data struct
156 *
157 * Return: 0 if zynqmp, 1 if zynq.
158 */
159static int zynq_gpio_is_zynq(struct zynq_gpio *gpio)
160{
161 return !!(gpio->p_data->quirks & ZYNQ_GPIO_QUIRK_IS_ZYNQ);
162}
163
164/**
Swapna Manupati06aa0902017-08-07 13:01:57 +0200165 * gpio_data_ro_bug - test if HW bug exists or not
166 * @gpio: Pointer to driver data struct
167 *
168 * Return: 0 if bug doesnot exist, 1 if bug exists.
169 */
170static int gpio_data_ro_bug(struct zynq_gpio *gpio)
171{
172 return !!(gpio->p_data->quirks & GPIO_QUIRK_DATA_RO_BUG);
173}
174
175/**
Harini Katakam3242ba12014-07-08 16:32:35 +0530176 * zynq_gpio_get_bank_pin - Get the bank number and pin number within that bank
177 * for a given pin in the GPIO device
178 * @pin_num: gpio pin number within the device
179 * @bank_num: an output parameter used to return the bank number of the gpio
180 * pin
181 * @bank_pin_num: an output parameter used to return pin number within a bank
182 * for the given gpio pin
Nava kishore Manne6ae51042017-08-07 13:01:58 +0200183 * @gpio: gpio device data structure
Harini Katakam3242ba12014-07-08 16:32:35 +0530184 *
185 * Returns the bank number and pin offset within the bank.
186 */
187static inline void zynq_gpio_get_bank_pin(unsigned int pin_num,
188 unsigned int *bank_num,
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530189 unsigned int *bank_pin_num,
190 struct zynq_gpio *gpio)
Harini Katakam3242ba12014-07-08 16:32:35 +0530191{
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530192 int bank;
Harini Katakam3242ba12014-07-08 16:32:35 +0530193
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530194 for (bank = 0; bank < gpio->p_data->max_bank; bank++) {
195 if ((pin_num >= gpio->p_data->bank_min[bank]) &&
Michal Simek16ee62e2017-08-07 13:02:01 +0200196 (pin_num <= gpio->p_data->bank_max[bank])) {
Nava kishore Manne2717cfc2017-08-07 13:02:00 +0200197 *bank_num = bank;
198 *bank_pin_num = pin_num -
199 gpio->p_data->bank_min[bank];
200 return;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530201 }
202 }
203
204 /* default */
205 WARN(true, "invalid GPIO pin number: %u", pin_num);
206 *bank_num = 0;
207 *bank_pin_num = 0;
208}
Lars-Peter Clausen016da142014-08-18 11:54:56 +0200209
Harini Katakam3242ba12014-07-08 16:32:35 +0530210/**
211 * zynq_gpio_get_value - Get the state of the specified pin of GPIO device
212 * @chip: gpio_chip instance to be worked on
213 * @pin: gpio pin number within the device
214 *
215 * This function reads the state of the specified pin of the GPIO device.
216 *
217 * Return: 0 if the pin is low, 1 if pin is high.
218 */
219static int zynq_gpio_get_value(struct gpio_chip *chip, unsigned int pin)
220{
221 u32 data;
222 unsigned int bank_num, bank_pin_num;
Linus Walleij31a89442015-12-07 15:29:53 +0100223 struct zynq_gpio *gpio = gpiochip_get_data(chip);
Harini Katakam3242ba12014-07-08 16:32:35 +0530224
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530225 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num, gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530226
Swapna Manupati06aa0902017-08-07 13:01:57 +0200227 if (gpio_data_ro_bug(gpio)) {
228 if (zynq_gpio_is_zynq(gpio)) {
229 if (bank_num <= 1) {
230 data = readl_relaxed(gpio->base_addr +
231 ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
232 } else {
233 data = readl_relaxed(gpio->base_addr +
234 ZYNQ_GPIO_DATA_OFFSET(bank_num));
235 }
236 } else {
237 if (bank_num <= 2) {
238 data = readl_relaxed(gpio->base_addr +
239 ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
240 } else {
241 data = readl_relaxed(gpio->base_addr +
242 ZYNQ_GPIO_DATA_OFFSET(bank_num));
243 }
244 }
245 } else {
246 data = readl_relaxed(gpio->base_addr +
247 ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
248 }
Harini Katakam3242ba12014-07-08 16:32:35 +0530249 return (data >> bank_pin_num) & 1;
250}
251
252/**
253 * zynq_gpio_set_value - Modify the state of the pin with specified value
254 * @chip: gpio_chip instance to be worked on
255 * @pin: gpio pin number within the device
256 * @state: value used to modify the state of the specified pin
257 *
258 * This function calculates the register offset (i.e to lower 16 bits or
259 * upper 16 bits) based on the given pin number and sets the state of a
260 * gpio pin to the specified value. The state is either 0 or non-zero.
261 */
262static void zynq_gpio_set_value(struct gpio_chip *chip, unsigned int pin,
263 int state)
264{
265 unsigned int reg_offset, bank_num, bank_pin_num;
Linus Walleij31a89442015-12-07 15:29:53 +0100266 struct zynq_gpio *gpio = gpiochip_get_data(chip);
Harini Katakam3242ba12014-07-08 16:32:35 +0530267
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530268 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num, gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530269
270 if (bank_pin_num >= ZYNQ_GPIO_MID_PIN_NUM) {
271 /* only 16 data bits in bit maskable reg */
272 bank_pin_num -= ZYNQ_GPIO_MID_PIN_NUM;
273 reg_offset = ZYNQ_GPIO_DATA_MSW_OFFSET(bank_num);
274 } else {
275 reg_offset = ZYNQ_GPIO_DATA_LSW_OFFSET(bank_num);
276 }
277
278 /*
279 * get the 32 bit value to be written to the mask/data register where
280 * the upper 16 bits is the mask and lower 16 bits is the data
281 */
282 state = !!state;
283 state = ~(1 << (bank_pin_num + ZYNQ_GPIO_MID_PIN_NUM)) &
284 ((state << bank_pin_num) | ZYNQ_GPIO_UPPER_MASK);
285
286 writel_relaxed(state, gpio->base_addr + reg_offset);
287}
288
289/**
290 * zynq_gpio_dir_in - Set the direction of the specified GPIO pin as input
291 * @chip: gpio_chip instance to be worked on
292 * @pin: gpio pin number within the device
293 *
294 * This function uses the read-modify-write sequence to set the direction of
295 * the gpio pin as input.
296 *
297 * Return: 0 always
298 */
299static int zynq_gpio_dir_in(struct gpio_chip *chip, unsigned int pin)
300{
301 u32 reg;
302 unsigned int bank_num, bank_pin_num;
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530303 unsigned long flags;
Linus Walleij31a89442015-12-07 15:29:53 +0100304 struct zynq_gpio *gpio = gpiochip_get_data(chip);
Harini Katakam3242ba12014-07-08 16:32:35 +0530305
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530306 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num, gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530307
Nava kishore Mannee3296f12016-09-23 16:56:58 +0530308 /*
309 * On zynq bank 0 pins 7 and 8 are special and cannot be used
310 * as inputs.
311 */
Soren Brinkmann3638bd42017-06-08 10:32:07 -0700312 if (zynq_gpio_is_zynq(gpio) && bank_num == 0 &&
Michal Simek16ee62e2017-08-07 13:02:01 +0200313 (bank_pin_num == 7 || bank_pin_num == 8))
Harini Katakam3242ba12014-07-08 16:32:35 +0530314 return -EINVAL;
315
316 /* clear the bit in direction mode reg to set the pin as input */
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530317 spin_lock_irqsave(&gpio->dirlock, flags);
Harini Katakam3242ba12014-07-08 16:32:35 +0530318 reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
319 reg &= ~BIT(bank_pin_num);
320 writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530321 spin_unlock_irqrestore(&gpio->dirlock, flags);
Harini Katakam3242ba12014-07-08 16:32:35 +0530322
323 return 0;
324}
325
326/**
327 * zynq_gpio_dir_out - Set the direction of the specified GPIO pin as output
328 * @chip: gpio_chip instance to be worked on
329 * @pin: gpio pin number within the device
330 * @state: value to be written to specified pin
331 *
332 * This function sets the direction of specified GPIO pin as output, configures
333 * the Output Enable register for the pin and uses zynq_gpio_set to set
334 * the state of the pin to the value specified.
335 *
336 * Return: 0 always
337 */
338static int zynq_gpio_dir_out(struct gpio_chip *chip, unsigned int pin,
339 int state)
340{
341 u32 reg;
342 unsigned int bank_num, bank_pin_num;
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530343 unsigned long flags;
Linus Walleij31a89442015-12-07 15:29:53 +0100344 struct zynq_gpio *gpio = gpiochip_get_data(chip);
Harini Katakam3242ba12014-07-08 16:32:35 +0530345
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530346 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num, gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530347
348 /* set the GPIO pin as output */
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530349 spin_lock_irqsave(&gpio->dirlock, flags);
Harini Katakam3242ba12014-07-08 16:32:35 +0530350 reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
351 reg |= BIT(bank_pin_num);
352 writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
353
354 /* configure the output enable reg for the pin */
355 reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
356 reg |= BIT(bank_pin_num);
357 writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530358 spin_unlock_irqrestore(&gpio->dirlock, flags);
Harini Katakam3242ba12014-07-08 16:32:35 +0530359
360 /* set the state of the pin */
361 zynq_gpio_set_value(chip, pin, state);
362 return 0;
363}
364
365/**
Brandon Maier61690052018-11-28 11:14:17 -0600366 * zynq_gpio_get_direction - Read the direction of the specified GPIO pin
367 * @chip: gpio_chip instance to be worked on
368 * @pin: gpio pin number within the device
369 *
370 * This function returns the direction of the specified GPIO.
371 *
Matti Vaittinene42615e2019-11-06 10:54:12 +0200372 * Return: GPIO_LINE_DIRECTION_OUT or GPIO_LINE_DIRECTION_IN
Brandon Maier61690052018-11-28 11:14:17 -0600373 */
374static int zynq_gpio_get_direction(struct gpio_chip *chip, unsigned int pin)
375{
376 u32 reg;
377 unsigned int bank_num, bank_pin_num;
378 struct zynq_gpio *gpio = gpiochip_get_data(chip);
379
380 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num, gpio);
381
382 reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
383
Matti Vaittinene42615e2019-11-06 10:54:12 +0200384 if (reg & BIT(bank_pin_num))
385 return GPIO_LINE_DIRECTION_OUT;
386
387 return GPIO_LINE_DIRECTION_IN;
Brandon Maier61690052018-11-28 11:14:17 -0600388}
389
390/**
Harini Katakam3242ba12014-07-08 16:32:35 +0530391 * zynq_gpio_irq_mask - Disable the interrupts for a gpio pin
392 * @irq_data: per irq and chip data passed down to chip functions
393 *
394 * This function calculates gpio pin number from irq number and sets the
395 * bit in the Interrupt Disable register of the corresponding bank to disable
396 * interrupts for that pin.
397 */
398static void zynq_gpio_irq_mask(struct irq_data *irq_data)
399{
400 unsigned int device_pin_num, bank_num, bank_pin_num;
Linus Walleijfa9795d2015-08-27 14:26:46 +0200401 struct zynq_gpio *gpio =
Linus Walleij31a89442015-12-07 15:29:53 +0100402 gpiochip_get_data(irq_data_get_irq_chip_data(irq_data));
Harini Katakam3242ba12014-07-08 16:32:35 +0530403
404 device_pin_num = irq_data->hwirq;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530405 zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num, gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530406 writel_relaxed(BIT(bank_pin_num),
407 gpio->base_addr + ZYNQ_GPIO_INTDIS_OFFSET(bank_num));
408}
409
410/**
411 * zynq_gpio_irq_unmask - Enable the interrupts for a gpio pin
412 * @irq_data: irq data containing irq number of gpio pin for the interrupt
413 * to enable
414 *
415 * This function calculates the gpio pin number from irq number and sets the
416 * bit in the Interrupt Enable register of the corresponding bank to enable
417 * interrupts for that pin.
418 */
419static void zynq_gpio_irq_unmask(struct irq_data *irq_data)
420{
421 unsigned int device_pin_num, bank_num, bank_pin_num;
Linus Walleijfa9795d2015-08-27 14:26:46 +0200422 struct zynq_gpio *gpio =
Linus Walleij31a89442015-12-07 15:29:53 +0100423 gpiochip_get_data(irq_data_get_irq_chip_data(irq_data));
Harini Katakam3242ba12014-07-08 16:32:35 +0530424
425 device_pin_num = irq_data->hwirq;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530426 zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num, gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530427 writel_relaxed(BIT(bank_pin_num),
428 gpio->base_addr + ZYNQ_GPIO_INTEN_OFFSET(bank_num));
429}
430
431/**
Lars-Peter Clausen190dc2e2014-07-18 11:52:12 +0200432 * zynq_gpio_irq_ack - Acknowledge the interrupt of a gpio pin
433 * @irq_data: irq data containing irq number of gpio pin for the interrupt
434 * to ack
435 *
436 * This function calculates gpio pin number from irq number and sets the bit
437 * in the Interrupt Status Register of the corresponding bank, to ACK the irq.
438 */
439static void zynq_gpio_irq_ack(struct irq_data *irq_data)
440{
441 unsigned int device_pin_num, bank_num, bank_pin_num;
Linus Walleijfa9795d2015-08-27 14:26:46 +0200442 struct zynq_gpio *gpio =
Linus Walleij31a89442015-12-07 15:29:53 +0100443 gpiochip_get_data(irq_data_get_irq_chip_data(irq_data));
Lars-Peter Clausen190dc2e2014-07-18 11:52:12 +0200444
445 device_pin_num = irq_data->hwirq;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530446 zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num, gpio);
Lars-Peter Clausen190dc2e2014-07-18 11:52:12 +0200447 writel_relaxed(BIT(bank_pin_num),
448 gpio->base_addr + ZYNQ_GPIO_INTSTS_OFFSET(bank_num));
449}
450
451/**
452 * zynq_gpio_irq_enable - Enable the interrupts for a gpio pin
453 * @irq_data: irq data containing irq number of gpio pin for the interrupt
454 * to enable
455 *
Colin Cronin20a8a962015-05-18 11:41:43 -0700456 * Clears the INTSTS bit and unmasks the given interrupt.
Lars-Peter Clausen190dc2e2014-07-18 11:52:12 +0200457 */
458static void zynq_gpio_irq_enable(struct irq_data *irq_data)
459{
460 /*
461 * The Zynq GPIO controller does not disable interrupt detection when
462 * the interrupt is masked and only disables the propagation of the
463 * interrupt. This means when the controller detects an interrupt
464 * condition while the interrupt is logically disabled it will propagate
465 * that interrupt event once the interrupt is enabled. This will cause
466 * the interrupt consumer to see spurious interrupts to prevent this
467 * first make sure that the interrupt is not asserted and then enable
468 * it.
469 */
470 zynq_gpio_irq_ack(irq_data);
471 zynq_gpio_irq_unmask(irq_data);
472}
473
474/**
Harini Katakam3242ba12014-07-08 16:32:35 +0530475 * zynq_gpio_set_irq_type - Set the irq type for a gpio pin
476 * @irq_data: irq data containing irq number of gpio pin
477 * @type: interrupt type that is to be set for the gpio pin
478 *
479 * This function gets the gpio pin number and its bank from the gpio pin number
480 * and configures the INT_TYPE, INT_POLARITY and INT_ANY registers.
481 *
482 * Return: 0, negative error otherwise.
483 * TYPE-EDGE_RISING, INT_TYPE - 1, INT_POLARITY - 1, INT_ANY - 0;
484 * TYPE-EDGE_FALLING, INT_TYPE - 1, INT_POLARITY - 0, INT_ANY - 0;
485 * TYPE-EDGE_BOTH, INT_TYPE - 1, INT_POLARITY - NA, INT_ANY - 1;
486 * TYPE-LEVEL_HIGH, INT_TYPE - 0, INT_POLARITY - 1, INT_ANY - NA;
487 * TYPE-LEVEL_LOW, INT_TYPE - 0, INT_POLARITY - 0, INT_ANY - NA
488 */
489static int zynq_gpio_set_irq_type(struct irq_data *irq_data, unsigned int type)
490{
491 u32 int_type, int_pol, int_any;
492 unsigned int device_pin_num, bank_num, bank_pin_num;
Linus Walleijfa9795d2015-08-27 14:26:46 +0200493 struct zynq_gpio *gpio =
Linus Walleij31a89442015-12-07 15:29:53 +0100494 gpiochip_get_data(irq_data_get_irq_chip_data(irq_data));
Harini Katakam3242ba12014-07-08 16:32:35 +0530495
496 device_pin_num = irq_data->hwirq;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530497 zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num, gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530498
499 int_type = readl_relaxed(gpio->base_addr +
500 ZYNQ_GPIO_INTTYPE_OFFSET(bank_num));
501 int_pol = readl_relaxed(gpio->base_addr +
502 ZYNQ_GPIO_INTPOL_OFFSET(bank_num));
503 int_any = readl_relaxed(gpio->base_addr +
504 ZYNQ_GPIO_INTANY_OFFSET(bank_num));
505
506 /*
507 * based on the type requested, configure the INT_TYPE, INT_POLARITY
508 * and INT_ANY registers
509 */
510 switch (type) {
511 case IRQ_TYPE_EDGE_RISING:
512 int_type |= BIT(bank_pin_num);
513 int_pol |= BIT(bank_pin_num);
514 int_any &= ~BIT(bank_pin_num);
515 break;
516 case IRQ_TYPE_EDGE_FALLING:
517 int_type |= BIT(bank_pin_num);
518 int_pol &= ~BIT(bank_pin_num);
519 int_any &= ~BIT(bank_pin_num);
520 break;
521 case IRQ_TYPE_EDGE_BOTH:
522 int_type |= BIT(bank_pin_num);
523 int_any |= BIT(bank_pin_num);
524 break;
525 case IRQ_TYPE_LEVEL_HIGH:
526 int_type &= ~BIT(bank_pin_num);
527 int_pol |= BIT(bank_pin_num);
528 break;
529 case IRQ_TYPE_LEVEL_LOW:
530 int_type &= ~BIT(bank_pin_num);
531 int_pol &= ~BIT(bank_pin_num);
532 break;
533 default:
534 return -EINVAL;
535 }
536
537 writel_relaxed(int_type,
538 gpio->base_addr + ZYNQ_GPIO_INTTYPE_OFFSET(bank_num));
539 writel_relaxed(int_pol,
540 gpio->base_addr + ZYNQ_GPIO_INTPOL_OFFSET(bank_num));
541 writel_relaxed(int_any,
542 gpio->base_addr + ZYNQ_GPIO_INTANY_OFFSET(bank_num));
Lars-Peter Clausen6dd85952014-07-18 11:52:13 +0200543
Michal Simek16ee62e2017-08-07 13:02:01 +0200544 if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixner47c08462015-06-23 14:37:42 +0200545 irq_set_chip_handler_name_locked(irq_data,
Michal Simek16ee62e2017-08-07 13:02:01 +0200546 &zynq_gpio_level_irqchip,
547 handle_fasteoi_irq, NULL);
548 else
Thomas Gleixner47c08462015-06-23 14:37:42 +0200549 irq_set_chip_handler_name_locked(irq_data,
Michal Simek16ee62e2017-08-07 13:02:01 +0200550 &zynq_gpio_edge_irqchip,
551 handle_level_irq, NULL);
Lars-Peter Clausen6dd85952014-07-18 11:52:13 +0200552
Harini Katakam3242ba12014-07-08 16:32:35 +0530553 return 0;
554}
555
556static int zynq_gpio_set_wake(struct irq_data *data, unsigned int on)
557{
Linus Walleijfa9795d2015-08-27 14:26:46 +0200558 struct zynq_gpio *gpio =
Linus Walleij31a89442015-12-07 15:29:53 +0100559 gpiochip_get_data(irq_data_get_irq_chip_data(data));
Ezra Savard59e22112014-08-29 10:58:46 -0700560
561 irq_set_irq_wake(gpio->irq, on);
Harini Katakam3242ba12014-07-08 16:32:35 +0530562
563 return 0;
564}
565
Thomas Petazzonic2df3de2019-02-08 11:40:46 +0100566static int zynq_gpio_irq_reqres(struct irq_data *d)
567{
568 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
569 int ret;
570
571 ret = pm_runtime_get_sync(chip->parent);
572 if (ret < 0)
573 return ret;
574
575 return gpiochip_reqres_irq(chip, d->hwirq);
576}
577
578static void zynq_gpio_irq_relres(struct irq_data *d)
579{
580 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
581
582 gpiochip_relres_irq(chip, d->hwirq);
583 pm_runtime_put(chip->parent);
584}
585
Harini Katakam3242ba12014-07-08 16:32:35 +0530586/* irq chip descriptor */
Lars-Peter Clausen6dd85952014-07-18 11:52:13 +0200587static struct irq_chip zynq_gpio_level_irqchip = {
Harini Katakam3242ba12014-07-08 16:32:35 +0530588 .name = DRIVER_NAME,
Lars-Peter Clausen190dc2e2014-07-18 11:52:12 +0200589 .irq_enable = zynq_gpio_irq_enable,
Lars-Peter Clausen6dd85952014-07-18 11:52:13 +0200590 .irq_eoi = zynq_gpio_irq_ack,
591 .irq_mask = zynq_gpio_irq_mask,
592 .irq_unmask = zynq_gpio_irq_unmask,
593 .irq_set_type = zynq_gpio_set_irq_type,
594 .irq_set_wake = zynq_gpio_set_wake,
Thomas Petazzonic2df3de2019-02-08 11:40:46 +0100595 .irq_request_resources = zynq_gpio_irq_reqres,
596 .irq_release_resources = zynq_gpio_irq_relres,
Ezra Savarda1946772014-08-29 10:58:45 -0700597 .flags = IRQCHIP_EOI_THREADED | IRQCHIP_EOI_IF_HANDLED |
598 IRQCHIP_MASK_ON_SUSPEND,
Lars-Peter Clausen6dd85952014-07-18 11:52:13 +0200599};
600
601static struct irq_chip zynq_gpio_edge_irqchip = {
602 .name = DRIVER_NAME,
603 .irq_enable = zynq_gpio_irq_enable,
604 .irq_ack = zynq_gpio_irq_ack,
Harini Katakam3242ba12014-07-08 16:32:35 +0530605 .irq_mask = zynq_gpio_irq_mask,
606 .irq_unmask = zynq_gpio_irq_unmask,
607 .irq_set_type = zynq_gpio_set_irq_type,
608 .irq_set_wake = zynq_gpio_set_wake,
Thomas Petazzonic2df3de2019-02-08 11:40:46 +0100609 .irq_request_resources = zynq_gpio_irq_reqres,
610 .irq_release_resources = zynq_gpio_irq_relres,
Ezra Savarda1946772014-08-29 10:58:45 -0700611 .flags = IRQCHIP_MASK_ON_SUSPEND,
Harini Katakam3242ba12014-07-08 16:32:35 +0530612};
613
Lars-Peter Clausen5a2533a2014-08-18 11:54:55 +0200614static void zynq_gpio_handle_bank_irq(struct zynq_gpio *gpio,
615 unsigned int bank_num,
616 unsigned long pending)
617{
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530618 unsigned int bank_offset = gpio->p_data->bank_min[bank_num];
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100619 struct irq_domain *irqdomain = gpio->chip.irq.domain;
Lars-Peter Clausen5a2533a2014-08-18 11:54:55 +0200620 int offset;
621
622 if (!pending)
623 return;
624
625 for_each_set_bit(offset, &pending, 32) {
626 unsigned int gpio_irq;
627
Lars-Peter Clausen016da142014-08-18 11:54:56 +0200628 gpio_irq = irq_find_mapping(irqdomain, offset + bank_offset);
Lars-Peter Clausen5a2533a2014-08-18 11:54:55 +0200629 generic_handle_irq(gpio_irq);
630 }
631}
632
Harini Katakam3242ba12014-07-08 16:32:35 +0530633/**
634 * zynq_gpio_irqhandler - IRQ handler for the gpio banks of a gpio device
Harini Katakam3242ba12014-07-08 16:32:35 +0530635 * @desc: irq descriptor instance of the 'irq'
636 *
637 * This function reads the Interrupt Status Register of each bank to get the
638 * gpio pin number which has triggered an interrupt. It then acks the triggered
639 * interrupt and calls the pin specific handler set by the higher layer
640 * application for that pin.
641 * Note: A bug is reported if no handler is set for the gpio pin.
642 */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200643static void zynq_gpio_irqhandler(struct irq_desc *desc)
Harini Katakam3242ba12014-07-08 16:32:35 +0530644{
645 u32 int_sts, int_enb;
646 unsigned int bank_num;
Linus Walleijfa9795d2015-08-27 14:26:46 +0200647 struct zynq_gpio *gpio =
Linus Walleij31a89442015-12-07 15:29:53 +0100648 gpiochip_get_data(irq_desc_get_handler_data(desc));
Harini Katakam3242ba12014-07-08 16:32:35 +0530649 struct irq_chip *irqchip = irq_desc_get_chip(desc);
650
651 chained_irq_enter(irqchip, desc);
652
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530653 for (bank_num = 0; bank_num < gpio->p_data->max_bank; bank_num++) {
Harini Katakam3242ba12014-07-08 16:32:35 +0530654 int_sts = readl_relaxed(gpio->base_addr +
655 ZYNQ_GPIO_INTSTS_OFFSET(bank_num));
656 int_enb = readl_relaxed(gpio->base_addr +
657 ZYNQ_GPIO_INTMASK_OFFSET(bank_num));
Lars-Peter Clausen5a2533a2014-08-18 11:54:55 +0200658 zynq_gpio_handle_bank_irq(gpio, bank_num, int_sts & ~int_enb);
Harini Katakam3242ba12014-07-08 16:32:35 +0530659 }
660
661 chained_irq_exit(irqchip, desc);
662}
663
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200664static void zynq_gpio_save_context(struct zynq_gpio *gpio)
665{
666 unsigned int bank_num;
667
668 for (bank_num = 0; bank_num < gpio->p_data->max_bank; bank_num++) {
669 gpio->context.datalsw[bank_num] =
670 readl_relaxed(gpio->base_addr +
671 ZYNQ_GPIO_DATA_LSW_OFFSET(bank_num));
672 gpio->context.datamsw[bank_num] =
673 readl_relaxed(gpio->base_addr +
674 ZYNQ_GPIO_DATA_MSW_OFFSET(bank_num));
675 gpio->context.dirm[bank_num] = readl_relaxed(gpio->base_addr +
676 ZYNQ_GPIO_DIRM_OFFSET(bank_num));
677 gpio->context.int_en[bank_num] = readl_relaxed(gpio->base_addr +
678 ZYNQ_GPIO_INTMASK_OFFSET(bank_num));
679 gpio->context.int_type[bank_num] =
680 readl_relaxed(gpio->base_addr +
681 ZYNQ_GPIO_INTTYPE_OFFSET(bank_num));
682 gpio->context.int_polarity[bank_num] =
683 readl_relaxed(gpio->base_addr +
684 ZYNQ_GPIO_INTPOL_OFFSET(bank_num));
685 gpio->context.int_any[bank_num] =
686 readl_relaxed(gpio->base_addr +
687 ZYNQ_GPIO_INTANY_OFFSET(bank_num));
688 }
689}
690
691static void zynq_gpio_restore_context(struct zynq_gpio *gpio)
692{
693 unsigned int bank_num;
694
695 for (bank_num = 0; bank_num < gpio->p_data->max_bank; bank_num++) {
Swapna Manupati36f2e722019-12-26 17:42:11 +0530696 writel_relaxed(ZYNQ_GPIO_IXR_DISABLE_ALL, gpio->base_addr +
697 ZYNQ_GPIO_INTDIS_OFFSET(bank_num));
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200698 writel_relaxed(gpio->context.datalsw[bank_num],
699 gpio->base_addr +
700 ZYNQ_GPIO_DATA_LSW_OFFSET(bank_num));
701 writel_relaxed(gpio->context.datamsw[bank_num],
702 gpio->base_addr +
703 ZYNQ_GPIO_DATA_MSW_OFFSET(bank_num));
704 writel_relaxed(gpio->context.dirm[bank_num],
705 gpio->base_addr +
706 ZYNQ_GPIO_DIRM_OFFSET(bank_num));
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200707 writel_relaxed(gpio->context.int_type[bank_num],
708 gpio->base_addr +
709 ZYNQ_GPIO_INTTYPE_OFFSET(bank_num));
710 writel_relaxed(gpio->context.int_polarity[bank_num],
711 gpio->base_addr +
712 ZYNQ_GPIO_INTPOL_OFFSET(bank_num));
713 writel_relaxed(gpio->context.int_any[bank_num],
714 gpio->base_addr +
715 ZYNQ_GPIO_INTANY_OFFSET(bank_num));
Swapna Manupati36f2e722019-12-26 17:42:11 +0530716 writel_relaxed(~(gpio->context.int_en[bank_num]),
717 gpio->base_addr +
718 ZYNQ_GPIO_INTEN_OFFSET(bank_num));
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200719 }
720}
Michal Simekeb73d6e2017-08-07 13:01:59 +0200721
Harini Katakam3242ba12014-07-08 16:32:35 +0530722static int __maybe_unused zynq_gpio_suspend(struct device *dev)
723{
Shubhrajyoti Dattaa76e8652018-05-07 17:06:00 +0530724 struct zynq_gpio *gpio = dev_get_drvdata(dev);
Shubhrajyoti Datta5e3a8ec2018-05-07 16:06:54 +0530725 struct irq_data *data = irq_get_irq_data(gpio->irq);
Ezra Savard59e22112014-08-29 10:58:46 -0700726
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200727 if (!irqd_is_wakeup_set(data)) {
728 zynq_gpio_save_context(gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530729 return pm_runtime_force_suspend(dev);
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200730 }
Harini Katakam3242ba12014-07-08 16:32:35 +0530731
732 return 0;
733}
734
735static int __maybe_unused zynq_gpio_resume(struct device *dev)
736{
Shubhrajyoti Dattaa76e8652018-05-07 17:06:00 +0530737 struct zynq_gpio *gpio = dev_get_drvdata(dev);
Shubhrajyoti Datta5e3a8ec2018-05-07 16:06:54 +0530738 struct irq_data *data = irq_get_irq_data(gpio->irq);
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200739 int ret;
Ezra Savard59e22112014-08-29 10:58:46 -0700740
Shubhrajyoti Dattae11de4d2017-08-07 13:01:54 +0200741 if (!irqd_is_wakeup_set(data)) {
742 ret = pm_runtime_force_resume(dev);
743 zynq_gpio_restore_context(gpio);
744 return ret;
745 }
Harini Katakam3242ba12014-07-08 16:32:35 +0530746
747 return 0;
748}
749
750static int __maybe_unused zynq_gpio_runtime_suspend(struct device *dev)
751{
Wolfram Sang38ccad02018-10-21 22:00:01 +0200752 struct zynq_gpio *gpio = dev_get_drvdata(dev);
Harini Katakam3242ba12014-07-08 16:32:35 +0530753
754 clk_disable_unprepare(gpio->clk);
755
756 return 0;
757}
758
759static int __maybe_unused zynq_gpio_runtime_resume(struct device *dev)
760{
Wolfram Sang38ccad02018-10-21 22:00:01 +0200761 struct zynq_gpio *gpio = dev_get_drvdata(dev);
Harini Katakam3242ba12014-07-08 16:32:35 +0530762
763 return clk_prepare_enable(gpio->clk);
764}
765
Nava kishore Manne2717cfc2017-08-07 13:02:00 +0200766static int zynq_gpio_request(struct gpio_chip *chip, unsigned int offset)
Harini Katakam3242ba12014-07-08 16:32:35 +0530767{
768 int ret;
769
Linus Walleij58383c782015-11-04 09:56:26 +0100770 ret = pm_runtime_get_sync(chip->parent);
Harini Katakam3242ba12014-07-08 16:32:35 +0530771
772 /*
773 * If the device is already active pm_runtime_get() will return 1 on
774 * success, but gpio_request still needs to return 0.
775 */
776 return ret < 0 ? ret : 0;
777}
778
Nava kishore Manne2717cfc2017-08-07 13:02:00 +0200779static void zynq_gpio_free(struct gpio_chip *chip, unsigned int offset)
Harini Katakam3242ba12014-07-08 16:32:35 +0530780{
Linus Walleij58383c782015-11-04 09:56:26 +0100781 pm_runtime_put(chip->parent);
Harini Katakam3242ba12014-07-08 16:32:35 +0530782}
783
784static const struct dev_pm_ops zynq_gpio_dev_pm_ops = {
785 SET_SYSTEM_SLEEP_PM_OPS(zynq_gpio_suspend, zynq_gpio_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +0100786 SET_RUNTIME_PM_OPS(zynq_gpio_runtime_suspend,
Michal Simek16ee62e2017-08-07 13:02:01 +0200787 zynq_gpio_runtime_resume, NULL)
Harini Katakam3242ba12014-07-08 16:32:35 +0530788};
789
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530790static const struct zynq_platform_data zynqmp_gpio_def = {
791 .label = "zynqmp_gpio",
Swapna Manupati06aa0902017-08-07 13:01:57 +0200792 .quirks = GPIO_QUIRK_DATA_RO_BUG,
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530793 .ngpio = ZYNQMP_GPIO_NR_GPIOS,
794 .max_bank = ZYNQMP_GPIO_MAX_BANK,
795 .bank_min[0] = ZYNQ_GPIO_BANK0_PIN_MIN(MP),
796 .bank_max[0] = ZYNQ_GPIO_BANK0_PIN_MAX(MP),
797 .bank_min[1] = ZYNQ_GPIO_BANK1_PIN_MIN(MP),
798 .bank_max[1] = ZYNQ_GPIO_BANK1_PIN_MAX(MP),
799 .bank_min[2] = ZYNQ_GPIO_BANK2_PIN_MIN(MP),
800 .bank_max[2] = ZYNQ_GPIO_BANK2_PIN_MAX(MP),
801 .bank_min[3] = ZYNQ_GPIO_BANK3_PIN_MIN(MP),
802 .bank_max[3] = ZYNQ_GPIO_BANK3_PIN_MAX(MP),
803 .bank_min[4] = ZYNQ_GPIO_BANK4_PIN_MIN(MP),
804 .bank_max[4] = ZYNQ_GPIO_BANK4_PIN_MAX(MP),
805 .bank_min[5] = ZYNQ_GPIO_BANK5_PIN_MIN(MP),
806 .bank_max[5] = ZYNQ_GPIO_BANK5_PIN_MAX(MP),
807};
808
809static const struct zynq_platform_data zynq_gpio_def = {
810 .label = "zynq_gpio",
Swapna Manupati06aa0902017-08-07 13:01:57 +0200811 .quirks = ZYNQ_GPIO_QUIRK_IS_ZYNQ | GPIO_QUIRK_DATA_RO_BUG,
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530812 .ngpio = ZYNQ_GPIO_NR_GPIOS,
813 .max_bank = ZYNQ_GPIO_MAX_BANK,
814 .bank_min[0] = ZYNQ_GPIO_BANK0_PIN_MIN(),
815 .bank_max[0] = ZYNQ_GPIO_BANK0_PIN_MAX(),
816 .bank_min[1] = ZYNQ_GPIO_BANK1_PIN_MIN(),
817 .bank_max[1] = ZYNQ_GPIO_BANK1_PIN_MAX(),
818 .bank_min[2] = ZYNQ_GPIO_BANK2_PIN_MIN(),
819 .bank_max[2] = ZYNQ_GPIO_BANK2_PIN_MAX(),
820 .bank_min[3] = ZYNQ_GPIO_BANK3_PIN_MIN(),
821 .bank_max[3] = ZYNQ_GPIO_BANK3_PIN_MAX(),
822};
823
824static const struct of_device_id zynq_gpio_of_match[] = {
Masahiro Yamada7808c422017-05-13 01:18:45 +0900825 { .compatible = "xlnx,zynq-gpio-1.0", .data = &zynq_gpio_def },
826 { .compatible = "xlnx,zynqmp-gpio-1.0", .data = &zynqmp_gpio_def },
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530827 { /* end of table */ }
828};
829MODULE_DEVICE_TABLE(of, zynq_gpio_of_match);
830
Harini Katakam3242ba12014-07-08 16:32:35 +0530831/**
832 * zynq_gpio_probe - Initialization method for a zynq_gpio device
833 * @pdev: platform device instance
834 *
835 * This function allocates memory resources for the gpio device and registers
836 * all the banks of the device. It will also set up interrupts for the gpio
837 * pins.
838 * Note: Interrupts are disabled for all the banks during initialization.
839 *
840 * Return: 0 on success, negative error otherwise.
841 */
842static int zynq_gpio_probe(struct platform_device *pdev)
843{
Ezra Savard59e22112014-08-29 10:58:46 -0700844 int ret, bank_num;
Harini Katakam3242ba12014-07-08 16:32:35 +0530845 struct zynq_gpio *gpio;
846 struct gpio_chip *chip;
Linus Walleijf6a70532019-08-09 15:26:49 +0200847 struct gpio_irq_chip *girq;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530848 const struct of_device_id *match;
Harini Katakam3242ba12014-07-08 16:32:35 +0530849
850 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
851 if (!gpio)
852 return -ENOMEM;
853
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530854 match = of_match_node(zynq_gpio_of_match, pdev->dev.of_node);
855 if (!match) {
856 dev_err(&pdev->dev, "of_match_node() failed\n");
857 return -EINVAL;
858 }
859 gpio->p_data = match->data;
Harini Katakam3242ba12014-07-08 16:32:35 +0530860 platform_set_drvdata(pdev, gpio);
861
Enrico Weigelt, metux IT consult77bc0e62019-03-11 19:55:21 +0100862 gpio->base_addr = devm_platform_ioremap_resource(pdev, 0);
Harini Katakam3242ba12014-07-08 16:32:35 +0530863 if (IS_ERR(gpio->base_addr))
864 return PTR_ERR(gpio->base_addr);
865
Ezra Savard59e22112014-08-29 10:58:46 -0700866 gpio->irq = platform_get_irq(pdev, 0);
Stephen Boyd15bddb72019-07-30 11:15:15 -0700867 if (gpio->irq < 0)
Ezra Savard59e22112014-08-29 10:58:46 -0700868 return gpio->irq;
Harini Katakam3242ba12014-07-08 16:32:35 +0530869
870 /* configure the gpio chip */
871 chip = &gpio->chip;
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530872 chip->label = gpio->p_data->label;
Harini Katakam3242ba12014-07-08 16:32:35 +0530873 chip->owner = THIS_MODULE;
Linus Walleij58383c782015-11-04 09:56:26 +0100874 chip->parent = &pdev->dev;
Harini Katakam3242ba12014-07-08 16:32:35 +0530875 chip->get = zynq_gpio_get_value;
876 chip->set = zynq_gpio_set_value;
877 chip->request = zynq_gpio_request;
878 chip->free = zynq_gpio_free;
879 chip->direction_input = zynq_gpio_dir_in;
880 chip->direction_output = zynq_gpio_dir_out;
Brandon Maier61690052018-11-28 11:14:17 -0600881 chip->get_direction = zynq_gpio_get_direction;
Michal Simek060f3eb2018-04-11 15:55:01 +0200882 chip->base = of_alias_get_id(pdev->dev.of_node, "gpio");
Anurag Kumar Vulishabdf7a4a2015-06-04 17:40:32 +0530883 chip->ngpio = gpio->p_data->ngpio;
Harini Katakam3242ba12014-07-08 16:32:35 +0530884
Michal Simek3773c192015-12-10 12:10:12 +0100885 /* Retrieve GPIO clock */
Harini Katakam3242ba12014-07-08 16:32:35 +0530886 gpio->clk = devm_clk_get(&pdev->dev, NULL);
887 if (IS_ERR(gpio->clk)) {
888 dev_err(&pdev->dev, "input clock not found.\n");
889 return PTR_ERR(gpio->clk);
890 }
Helmut Grohne0f84f292016-06-03 14:15:32 +0200891 ret = clk_prepare_enable(gpio->clk);
892 if (ret) {
893 dev_err(&pdev->dev, "Unable to enable clock.\n");
894 return ret;
895 }
Michal Simek3773c192015-12-10 12:10:12 +0100896
Glenn Langedockfdcfec112020-06-17 17:07:21 +0530897 spin_lock_init(&gpio->dirlock);
898
Helmut Grohne0f84f292016-06-03 14:15:32 +0200899 pm_runtime_set_active(&pdev->dev);
Michal Simek3773c192015-12-10 12:10:12 +0100900 pm_runtime_enable(&pdev->dev);
901 ret = pm_runtime_get_sync(&pdev->dev);
902 if (ret < 0)
Shubhrajyoti Datta615d23f2016-04-04 23:44:06 +0530903 goto err_pm_dis;
Harini Katakam3242ba12014-07-08 16:32:35 +0530904
Linus Walleijf6a70532019-08-09 15:26:49 +0200905 /* disable interrupts for all banks */
906 for (bank_num = 0; bank_num < gpio->p_data->max_bank; bank_num++)
907 writel_relaxed(ZYNQ_GPIO_IXR_DISABLE_ALL, gpio->base_addr +
908 ZYNQ_GPIO_INTDIS_OFFSET(bank_num));
909
910 /* Set up the GPIO irqchip */
911 girq = &chip->irq;
912 girq->chip = &zynq_gpio_edge_irqchip;
913 girq->parent_handler = zynq_gpio_irqhandler;
914 girq->num_parents = 1;
915 girq->parents = devm_kcalloc(&pdev->dev, 1,
916 sizeof(*girq->parents),
917 GFP_KERNEL);
918 if (!girq->parents) {
919 ret = -ENOMEM;
920 goto err_pm_put;
921 }
922 girq->parents[0] = gpio->irq;
923 girq->default_type = IRQ_TYPE_NONE;
924 girq->handler = handle_level_irq;
925
Harini Katakam3242ba12014-07-08 16:32:35 +0530926 /* report a bug if gpio chip registration fails */
Linus Walleij31a89442015-12-07 15:29:53 +0100927 ret = gpiochip_add_data(chip, gpio);
Harini Katakam3242ba12014-07-08 16:32:35 +0530928 if (ret) {
929 dev_err(&pdev->dev, "Failed to add gpio chip\n");
Michal Simek3773c192015-12-10 12:10:12 +0100930 goto err_pm_put;
Harini Katakam3242ba12014-07-08 16:32:35 +0530931 }
932
Michal Simek3773c192015-12-10 12:10:12 +0100933 pm_runtime_put(&pdev->dev);
Harini Katakam3242ba12014-07-08 16:32:35 +0530934
Harini Katakam3242ba12014-07-08 16:32:35 +0530935 return 0;
936
Michal Simek3773c192015-12-10 12:10:12 +0100937err_pm_put:
938 pm_runtime_put(&pdev->dev);
Shubhrajyoti Datta615d23f2016-04-04 23:44:06 +0530939err_pm_dis:
940 pm_runtime_disable(&pdev->dev);
Helmut Grohne0f84f292016-06-03 14:15:32 +0200941 clk_disable_unprepare(gpio->clk);
Harini Katakam3242ba12014-07-08 16:32:35 +0530942
943 return ret;
944}
945
946/**
947 * zynq_gpio_remove - Driver removal function
948 * @pdev: platform device instance
949 *
950 * Return: 0 always
951 */
952static int zynq_gpio_remove(struct platform_device *pdev)
953{
Harini Katakam3242ba12014-07-08 16:32:35 +0530954 struct zynq_gpio *gpio = platform_get_drvdata(pdev);
955
956 pm_runtime_get_sync(&pdev->dev);
Linus Walleijda26d5d2014-09-16 15:11:41 -0700957 gpiochip_remove(&gpio->chip);
Harini Katakam3242ba12014-07-08 16:32:35 +0530958 clk_disable_unprepare(gpio->clk);
959 device_set_wakeup_capable(&pdev->dev, 0);
Michal Simek6b956af2015-06-25 10:29:19 +0200960 pm_runtime_disable(&pdev->dev);
Harini Katakam3242ba12014-07-08 16:32:35 +0530961 return 0;
962}
963
Harini Katakam3242ba12014-07-08 16:32:35 +0530964static struct platform_driver zynq_gpio_driver = {
965 .driver = {
966 .name = DRIVER_NAME,
Harini Katakam3242ba12014-07-08 16:32:35 +0530967 .pm = &zynq_gpio_dev_pm_ops,
968 .of_match_table = zynq_gpio_of_match,
969 },
970 .probe = zynq_gpio_probe,
971 .remove = zynq_gpio_remove,
972};
973
974/**
975 * zynq_gpio_init - Initial driver registration call
976 *
977 * Return: value from platform_driver_register
978 */
979static int __init zynq_gpio_init(void)
980{
981 return platform_driver_register(&zynq_gpio_driver);
982}
983postcore_initcall(zynq_gpio_init);
984
Masahiro Yamada80d2bf52015-06-17 17:51:41 +0900985static void __exit zynq_gpio_exit(void)
986{
987 platform_driver_unregister(&zynq_gpio_driver);
988}
989module_exit(zynq_gpio_exit);
990
Harini Katakam3242ba12014-07-08 16:32:35 +0530991MODULE_AUTHOR("Xilinx Inc.");
992MODULE_DESCRIPTION("Zynq GPIO driver");
993MODULE_LICENSE("GPL");