Thomas Gleixner | 6e75fc0 | 2019-05-27 08:55:22 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 2 | /* |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 3 | * Xilinx gpio driver for xps/axi_gpio IP. |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 4 | * |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 5 | * Copyright 2008 - 2013 Xilinx, Inc. |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 8 | #include <linux/bitops.h> |
Srinivas Neeli | 65bbe53 | 2020-11-12 22:42:22 +0530 | [diff] [blame] | 9 | #include <linux/clk.h> |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 10 | #include <linux/errno.h> |
Srinivas Neeli | 8c669fe | 2020-11-12 22:42:20 +0530 | [diff] [blame] | 11 | #include <linux/gpio/driver.h> |
| 12 | #include <linux/init.h> |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Srinivas Neeli | 8c669fe | 2020-11-12 22:42:20 +0530 | [diff] [blame] | 14 | #include <linux/io.h> |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 15 | #include <linux/irq.h> |
Paul Gortmaker | bb207ef | 2011-07-03 13:38:09 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 17 | #include <linux/of_device.h> |
| 18 | #include <linux/of_platform.h> |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 19 | #include <linux/pm_runtime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 21 | |
| 22 | /* Register Offset Definitions */ |
| 23 | #define XGPIO_DATA_OFFSET (0x0) /* Data register */ |
| 24 | #define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */ |
| 25 | |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 26 | #define XGPIO_CHANNEL0_OFFSET 0x0 |
| 27 | #define XGPIO_CHANNEL1_OFFSET 0x8 |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 28 | |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 29 | #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 Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 34 | /* Read/Write access to the GPIO registers */ |
Ricardo Ribalda Delgado | c54c58b | 2014-12-17 16:51:10 +0100 | [diff] [blame] | 35 | #if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86) |
Michal Simek | cc090d6 | 2013-06-03 14:31:18 +0200 | [diff] [blame] | 36 | # 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 Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * struct xgpio_instance - Stores information about GPIO device |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 45 | * @gc: GPIO chip |
| 46 | * @regs: register block |
Michal Simek | 3c1b5c9b | 2015-05-04 16:37:16 +0200 | [diff] [blame] | 47 | * @gpio_width: GPIO width for every channel |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 48 | * @gpio_state: GPIO write state shadow register |
| 49 | * @gpio_last_irq_read: GPIO read state register from last interrupt |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 50 | * @gpio_dir: GPIO direction shadow register |
| 51 | * @gpio_lock: Lock used for synchronization |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 52 | * @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 Neeli | 65bbe53 | 2020-11-12 22:42:22 +0530 | [diff] [blame] | 57 | * @clk: clock resource for this driver |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 58 | */ |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 59 | struct xgpio_instance { |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 60 | struct gpio_chip gc; |
| 61 | void __iomem *regs; |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 62 | unsigned int gpio_width[2]; |
| 63 | u32 gpio_state[2]; |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 64 | u32 gpio_last_irq_read[2]; |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 65 | u32 gpio_dir[2]; |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 66 | spinlock_t gpio_lock; /* For serializing operations */ |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 67 | 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 Neeli | 65bbe53 | 2020-11-12 22:42:22 +0530 | [diff] [blame] | 72 | struct clk *clk; |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 75 | static 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 Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 83 | static inline int xgpio_regoffset(struct xgpio_instance *chip, int ch) |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 84 | { |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 85 | 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 Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 94 | |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 95 | static 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 | |
| 100 | static 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 Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static 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 Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 112 | |
| 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 Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 118 | * 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 Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 123 | */ |
| 124 | static int xgpio_get(struct gpio_chip *gc, unsigned int gpio) |
| 125 | { |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 126 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 127 | int index = xgpio_index(chip, gpio); |
| 128 | int offset = xgpio_offset(chip, gpio); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 129 | u32 val; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 130 | |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 131 | val = xgpio_read_chan(chip, XGPIO_DATA_OFFSET, index); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 132 | |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 133 | return !!(val & BIT(offset)); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 134 | } |
| 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 | */ |
| 145 | static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val) |
| 146 | { |
| 147 | unsigned long flags; |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 148 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 149 | int index = xgpio_index(chip, gpio); |
| 150 | int offset = xgpio_offset(chip, gpio); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 151 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 152 | spin_lock_irqsave(&chip->gpio_lock, flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 153 | |
| 154 | /* Write to GPIO signal and set its direction to output */ |
| 155 | if (val) |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 156 | chip->gpio_state[index] |= BIT(offset); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 157 | else |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 158 | chip->gpio_state[index] &= ~BIT(offset); |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 159 | |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 160 | xgpio_write_chan(chip, XGPIO_DATA_OFFSET, index, chip->gpio_state[index]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 161 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 162 | spin_unlock_irqrestore(&chip->gpio_lock, flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 166 | * 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 | */ |
| 174 | static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, |
| 175 | unsigned long *bits) |
| 176 | { |
| 177 | unsigned long flags; |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 178 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
| 179 | int index = xgpio_index(chip, 0); |
| 180 | int offset, i; |
| 181 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 182 | spin_lock_irqsave(&chip->gpio_lock, flags); |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 183 | |
| 184 | /* Write to GPIO signals */ |
| 185 | for (i = 0; i < gc->ngpio; i++) { |
| 186 | if (*mask == 0) |
| 187 | break; |
Paul Thomas | c3afa80 | 2020-01-25 17:14:10 -0500 | [diff] [blame] | 188 | /* Once finished with an index write it out to the register */ |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 189 | if (index != xgpio_index(chip, i)) { |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 190 | xgpio_write_chan(chip, XGPIO_DATA_OFFSET, index, |
| 191 | chip->gpio_state[index]); |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 192 | spin_unlock_irqrestore(&chip->gpio_lock, flags); |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 193 | index = xgpio_index(chip, i); |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 194 | spin_lock_irqsave(&chip->gpio_lock, flags); |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 195 | } |
| 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 Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 205 | xgpio_write_chan(chip, XGPIO_DATA_OFFSET, index, chip->gpio_state[index]); |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 206 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 207 | spin_unlock_irqrestore(&chip->gpio_lock, flags); |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 211 | * 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 Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 215 | * Return: |
| 216 | * 0 - if direction of GPIO signals is set as input |
| 217 | * otherwise it returns negative error value. |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 218 | */ |
| 219 | static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio) |
| 220 | { |
| 221 | unsigned long flags; |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 222 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 223 | int index = xgpio_index(chip, gpio); |
| 224 | int offset = xgpio_offset(chip, gpio); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 225 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 226 | spin_lock_irqsave(&chip->gpio_lock, flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 227 | |
| 228 | /* Set the GPIO bit in shadow register and set direction as input */ |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 229 | chip->gpio_dir[index] |= BIT(offset); |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 230 | xgpio_write_chan(chip, XGPIO_TRI_OFFSET, index, chip->gpio_dir[index]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 231 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 232 | spin_unlock_irqrestore(&chip->gpio_lock, flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 233 | |
| 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 Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 243 | * 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 Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 247 | * error otherwise it returns 0. |
| 248 | */ |
| 249 | static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) |
| 250 | { |
| 251 | unsigned long flags; |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 252 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 253 | int index = xgpio_index(chip, gpio); |
| 254 | int offset = xgpio_offset(chip, gpio); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 255 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 256 | spin_lock_irqsave(&chip->gpio_lock, flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 257 | |
| 258 | /* Write state of GPIO signal */ |
| 259 | if (val) |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 260 | chip->gpio_state[index] |= BIT(offset); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 261 | else |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 262 | chip->gpio_state[index] &= ~BIT(offset); |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 263 | xgpio_write_chan(chip, XGPIO_DATA_OFFSET, index, chip->gpio_state[index]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 264 | |
| 265 | /* Clear the GPIO bit in shadow register and set direction as output */ |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 266 | chip->gpio_dir[index] &= ~BIT(offset); |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 267 | xgpio_write_chan(chip, XGPIO_TRI_OFFSET, index, chip->gpio_dir[index]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 268 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 269 | spin_unlock_irqrestore(&chip->gpio_lock, flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * xgpio_save_regs - Set initial values of GPIO pins |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 276 | * @chip: Pointer to GPIO instance |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 277 | */ |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 278 | static void xgpio_save_regs(struct xgpio_instance *chip) |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 279 | { |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 280 | 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 Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 282 | |
| 283 | if (!chip->gpio_width[1]) |
| 284 | return; |
| 285 | |
Andy Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 286 | 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 Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 290 | static 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 | |
| 302 | static void xgpio_free(struct gpio_chip *chip, unsigned int offset) |
| 303 | { |
| 304 | pm_runtime_put(chip->parent); |
| 305 | } |
| 306 | |
| 307 | static 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 Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 323 | /** |
Srinivas Neeli | 0230a41 | 2020-11-12 22:42:25 +0530 | [diff] [blame] | 324 | * 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 | */ |
| 331 | static int xgpio_remove(struct platform_device *pdev) |
| 332 | { |
| 333 | struct xgpio_instance *gpio = platform_get_drvdata(pdev); |
| 334 | |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 335 | pm_runtime_get_sync(&pdev->dev); |
| 336 | pm_runtime_put_noidle(&pdev->dev); |
| 337 | pm_runtime_disable(&pdev->dev); |
Srinivas Neeli | 0230a41 | 2020-11-12 22:42:25 +0530 | [diff] [blame] | 338 | clk_disable_unprepare(gpio->clk); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /** |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 344 | * 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 | */ |
| 349 | static void xgpio_irq_ack(struct irq_data *irq_data) |
| 350 | { |
| 351 | } |
| 352 | |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 353 | static 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 | |
| 369 | static 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 | |
| 379 | static 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 | |
| 387 | static 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 Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 393 | /** |
| 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 | */ |
| 397 | static 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 | */ |
| 423 | static 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 Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 445 | val = xgpio_read_chan(chip, XGPIO_DATA_OFFSET, index); |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 446 | 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 | */ |
| 465 | static 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 | */ |
| 503 | static 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 Shevchenko | 043aa3d | 2021-05-10 22:46:31 +0300 | [diff] [blame^] | 522 | data = xgpio_read_chan(chip, XGPIO_DATA_OFFSET, index); |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 523 | 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 Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 551 | * xgpio_of_probe - Probe method for the GPIO device. |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 552 | * @pdev: pointer to the platform device |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 553 | * |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 554 | * 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 Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 557 | */ |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 558 | static int xgpio_probe(struct platform_device *pdev) |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 559 | { |
| 560 | struct xgpio_instance *chip; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 561 | int status = 0; |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 562 | struct device_node *np = pdev->dev.of_node; |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 563 | u32 is_dual = 0; |
| 564 | u32 cells = 2; |
| 565 | struct gpio_irq_chip *girq; |
| 566 | u32 temp; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 567 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 568 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
| 569 | if (!chip) |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 570 | return -ENOMEM; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 571 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 572 | platform_set_drvdata(pdev, chip); |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 573 | |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 574 | /* Update GPIO state shadow register with default value */ |
Srinivas Neeli | bea67ae | 2020-11-12 22:42:27 +0530 | [diff] [blame] | 575 | if (of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0])) |
| 576 | chip->gpio_state[0] = 0x0; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 577 | |
| 578 | /* Update GPIO direction shadow register with default value */ |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 579 | if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0])) |
| 580 | chip->gpio_dir[0] = 0xFFFFFFFF; |
Michal Simek | 6f8bf50 | 2013-06-03 14:31:16 +0200 | [diff] [blame] | 581 | |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 582 | /* 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 Vormayr | 1b4c5a6 | 2014-09-24 00:58:45 +0200 | [diff] [blame] | 591 | /* |
| 592 | * Check device node and parent device node for device width |
| 593 | * and assume default width of 32 |
| 594 | */ |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 595 | if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0])) |
| 596 | chip->gpio_width[0] = 32; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 597 | |
Srinivas Neeli | 6e551bf | 2021-01-29 19:56:50 +0530 | [diff] [blame] | 598 | if (chip->gpio_width[0] > 32) |
| 599 | return -EINVAL; |
| 600 | |
Srinivas Neeli | 37ef334 | 2021-01-29 19:56:47 +0530 | [diff] [blame] | 601 | spin_lock_init(&chip->gpio_lock); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 602 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 603 | 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 Neeli | bea67ae | 2020-11-12 22:42:27 +0530 | [diff] [blame] | 608 | if (of_property_read_u32(np, "xlnx,dout-default-2", |
| 609 | &chip->gpio_state[1])) |
| 610 | chip->gpio_state[1] = 0x0; |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 611 | |
| 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 Neeli | 6e551bf | 2021-01-29 19:56:50 +0530 | [diff] [blame] | 625 | if (chip->gpio_width[1] > 32) |
| 626 | return -EINVAL; |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 629 | 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 Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 634 | chip->gc.of_gpio_n_cells = cells; |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 635 | chip->gc.get = xgpio_get; |
| 636 | chip->gc.set = xgpio_set; |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 637 | chip->gc.request = xgpio_request; |
| 638 | chip->gc.free = xgpio_free; |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 639 | chip->gc.set_multiple = xgpio_set_multiple; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 640 | |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 641 | chip->gc.label = dev_name(&pdev->dev); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 642 | |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 643 | 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 Neeli | 65bbe53 | 2020-11-12 22:42:22 +0530 | [diff] [blame] | 649 | chip->clk = devm_clk_get_optional(&pdev->dev, NULL); |
Srinivas Neeli | 45c5277 | 2021-01-29 19:56:46 +0530 | [diff] [blame] | 650 | if (IS_ERR(chip->clk)) |
| 651 | return dev_err_probe(&pdev->dev, PTR_ERR(chip->clk), "input clock not found.\n"); |
Srinivas Neeli | 65bbe53 | 2020-11-12 22:42:22 +0530 | [diff] [blame] | 652 | |
| 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 Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 658 | pm_runtime_get_noresume(&pdev->dev); |
| 659 | pm_runtime_set_active(&pdev->dev); |
| 660 | pm_runtime_enable(&pdev->dev); |
Srinivas Neeli | 65bbe53 | 2020-11-12 22:42:22 +0530 | [diff] [blame] | 661 | |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 662 | xgpio_save_regs(chip); |
| 663 | |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 664 | 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 Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 691 | goto err_pm_put; |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 692 | } |
| 693 | girq->parents[0] = chip->irq; |
| 694 | girq->default_type = IRQ_TYPE_NONE; |
| 695 | girq->handler = handle_bad_irq; |
| 696 | |
| 697 | skip_irq: |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 698 | status = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 699 | if (status) { |
Robert Hancock | 1ebd0687 | 2019-06-07 11:04:16 -0600 | [diff] [blame] | 700 | dev_err(&pdev->dev, "failed to add GPIO chip\n"); |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 701 | goto err_pm_put; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 702 | } |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 703 | |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 704 | pm_runtime_put(&pdev->dev); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 705 | return 0; |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 706 | |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 707 | err_pm_put: |
| 708 | pm_runtime_disable(&pdev->dev); |
| 709 | pm_runtime_put_noidle(&pdev->dev); |
Srinivas Neeli | a32c7ca | 2021-01-29 19:56:48 +0530 | [diff] [blame] | 710 | clk_disable_unprepare(chip->clk); |
| 711 | return status; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 712 | } |
| 713 | |
Jingoo Han | 9992bc9 | 2014-05-07 18:08:20 +0900 | [diff] [blame] | 714 | static const struct of_device_id xgpio_of_match[] = { |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 715 | { .compatible = "xlnx,xps-gpio-1.00.a", }, |
| 716 | { /* end of list */ }, |
| 717 | }; |
| 718 | |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 719 | MODULE_DEVICE_TABLE(of, xgpio_of_match); |
| 720 | |
| 721 | static struct platform_driver xgpio_plat_driver = { |
| 722 | .probe = xgpio_probe, |
Srinivas Neeli | 0230a41 | 2020-11-12 22:42:25 +0530 | [diff] [blame] | 723 | .remove = xgpio_remove, |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 724 | .driver = { |
| 725 | .name = "gpio-xilinx", |
| 726 | .of_match_table = xgpio_of_match, |
Srinivas Neeli | 26b0477 | 2021-01-29 19:56:49 +0530 | [diff] [blame] | 727 | .pm = &xgpio_dev_pm_ops, |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 728 | }, |
| 729 | }; |
| 730 | |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 731 | static int __init xgpio_init(void) |
| 732 | { |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 733 | return platform_driver_register(&xgpio_plat_driver); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 734 | } |
| 735 | |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 736 | subsys_initcall(xgpio_init); |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 737 | |
| 738 | static void __exit xgpio_exit(void) |
| 739 | { |
| 740 | platform_driver_unregister(&xgpio_plat_driver); |
| 741 | } |
| 742 | module_exit(xgpio_exit); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 743 | |
| 744 | MODULE_AUTHOR("Xilinx, Inc."); |
| 745 | MODULE_DESCRIPTION("Xilinx GPIO driver"); |
| 746 | MODULE_LICENSE("GPL"); |