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> |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 9 | #include <linux/init.h> |
| 10 | #include <linux/errno.h> |
Paul Gortmaker | bb207ef | 2011-07-03 13:38:09 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 12 | #include <linux/of_device.h> |
| 13 | #include <linux/of_platform.h> |
| 14 | #include <linux/of_gpio.h> |
| 15 | #include <linux/io.h> |
Linus Walleij | 516df4e | 2018-08-06 18:39:59 +0200 | [diff] [blame] | 16 | #include <linux/gpio/driver.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 18 | |
| 19 | /* Register Offset Definitions */ |
| 20 | #define XGPIO_DATA_OFFSET (0x0) /* Data register */ |
| 21 | #define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */ |
| 22 | |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 23 | #define XGPIO_CHANNEL_OFFSET 0x8 |
| 24 | |
| 25 | /* Read/Write access to the GPIO registers */ |
Ricardo Ribalda Delgado | c54c58b | 2014-12-17 16:51:10 +0100 | [diff] [blame] | 26 | #if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86) |
Michal Simek | cc090d6 | 2013-06-03 14:31:18 +0200 | [diff] [blame] | 27 | # define xgpio_readreg(offset) readl(offset) |
| 28 | # define xgpio_writereg(offset, val) writel(val, offset) |
| 29 | #else |
| 30 | # define xgpio_readreg(offset) __raw_readl(offset) |
| 31 | # define xgpio_writereg(offset, val) __raw_writel(val, offset) |
| 32 | #endif |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * struct xgpio_instance - Stores information about GPIO device |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 36 | * @mmchip: OF GPIO chip for memory mapped banks |
Michal Simek | 3c1b5c9b | 2015-05-04 16:37:16 +0200 | [diff] [blame] | 37 | * @gpio_width: GPIO width for every channel |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 38 | * @gpio_state: GPIO state shadow register |
| 39 | * @gpio_dir: GPIO direction shadow register |
| 40 | * @gpio_lock: Lock used for synchronization |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 41 | */ |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 42 | struct xgpio_instance { |
| 43 | struct of_mm_gpio_chip mmchip; |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 44 | unsigned int gpio_width[2]; |
| 45 | u32 gpio_state[2]; |
| 46 | u32 gpio_dir[2]; |
| 47 | spinlock_t gpio_lock[2]; |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 50 | static inline int xgpio_index(struct xgpio_instance *chip, int gpio) |
| 51 | { |
| 52 | if (gpio >= chip->gpio_width[0]) |
| 53 | return 1; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static inline int xgpio_regoffset(struct xgpio_instance *chip, int gpio) |
| 59 | { |
| 60 | if (xgpio_index(chip, gpio)) |
| 61 | return XGPIO_CHANNEL_OFFSET; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static inline int xgpio_offset(struct xgpio_instance *chip, int gpio) |
| 67 | { |
| 68 | if (xgpio_index(chip, gpio)) |
| 69 | return gpio - chip->gpio_width[0]; |
| 70 | |
| 71 | return gpio; |
| 72 | } |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * xgpio_get - Read the specified signal of the GPIO device. |
| 76 | * @gc: Pointer to gpio_chip device structure. |
| 77 | * @gpio: GPIO signal number. |
| 78 | * |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 79 | * This function reads the specified signal of the GPIO device. |
| 80 | * |
| 81 | * Return: |
| 82 | * 0 if direction of GPIO signals is set as input otherwise it |
| 83 | * returns negative error value. |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 84 | */ |
| 85 | static int xgpio_get(struct gpio_chip *gc, unsigned int gpio) |
| 86 | { |
| 87 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 88 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 89 | u32 val; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 90 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 91 | val = xgpio_readreg(mm_gc->regs + XGPIO_DATA_OFFSET + |
| 92 | xgpio_regoffset(chip, gpio)); |
| 93 | |
| 94 | return !!(val & BIT(xgpio_offset(chip, gpio))); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * xgpio_set - Write the specified signal of the GPIO device. |
| 99 | * @gc: Pointer to gpio_chip device structure. |
| 100 | * @gpio: GPIO signal number. |
| 101 | * @val: Value to be written to specified signal. |
| 102 | * |
| 103 | * This function writes the specified value in to the specified signal of the |
| 104 | * GPIO device. |
| 105 | */ |
| 106 | static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val) |
| 107 | { |
| 108 | unsigned long flags; |
| 109 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 110 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 111 | int index = xgpio_index(chip, gpio); |
| 112 | int offset = xgpio_offset(chip, gpio); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 113 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 114 | spin_lock_irqsave(&chip->gpio_lock[index], flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 115 | |
| 116 | /* Write to GPIO signal and set its direction to output */ |
| 117 | if (val) |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 118 | chip->gpio_state[index] |= BIT(offset); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 119 | else |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 120 | chip->gpio_state[index] &= ~BIT(offset); |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 121 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 122 | xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + |
| 123 | xgpio_regoffset(chip, gpio), chip->gpio_state[index]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 124 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 125 | spin_unlock_irqrestore(&chip->gpio_lock[index], flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 129 | * xgpio_set_multiple - Write the specified signals of the GPIO device. |
| 130 | * @gc: Pointer to gpio_chip device structure. |
| 131 | * @mask: Mask of the GPIOS to modify. |
| 132 | * @bits: Value to be wrote on each GPIO |
| 133 | * |
| 134 | * This function writes the specified values into the specified signals of the |
| 135 | * GPIO devices. |
| 136 | */ |
| 137 | static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, |
| 138 | unsigned long *bits) |
| 139 | { |
| 140 | unsigned long flags; |
| 141 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
| 142 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
| 143 | int index = xgpio_index(chip, 0); |
| 144 | int offset, i; |
| 145 | |
| 146 | spin_lock_irqsave(&chip->gpio_lock[index], flags); |
| 147 | |
| 148 | /* Write to GPIO signals */ |
| 149 | for (i = 0; i < gc->ngpio; i++) { |
| 150 | if (*mask == 0) |
| 151 | break; |
| 152 | if (index != xgpio_index(chip, i)) { |
| 153 | xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + |
| 154 | xgpio_regoffset(chip, i), |
| 155 | chip->gpio_state[index]); |
| 156 | spin_unlock_irqrestore(&chip->gpio_lock[index], flags); |
| 157 | index = xgpio_index(chip, i); |
| 158 | spin_lock_irqsave(&chip->gpio_lock[index], flags); |
| 159 | } |
| 160 | if (__test_and_clear_bit(i, mask)) { |
| 161 | offset = xgpio_offset(chip, i); |
| 162 | if (test_bit(i, bits)) |
| 163 | chip->gpio_state[index] |= BIT(offset); |
| 164 | else |
| 165 | chip->gpio_state[index] &= ~BIT(offset); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + |
| 170 | xgpio_regoffset(chip, i), chip->gpio_state[index]); |
| 171 | |
| 172 | spin_unlock_irqrestore(&chip->gpio_lock[index], flags); |
| 173 | } |
| 174 | |
| 175 | /** |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 176 | * xgpio_dir_in - Set the direction of the specified GPIO signal as input. |
| 177 | * @gc: Pointer to gpio_chip device structure. |
| 178 | * @gpio: GPIO signal number. |
| 179 | * |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 180 | * Return: |
| 181 | * 0 - if direction of GPIO signals is set as input |
| 182 | * otherwise it returns negative error value. |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 183 | */ |
| 184 | static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio) |
| 185 | { |
| 186 | unsigned long flags; |
| 187 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 188 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 189 | int index = xgpio_index(chip, gpio); |
| 190 | int offset = xgpio_offset(chip, gpio); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 191 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 192 | spin_lock_irqsave(&chip->gpio_lock[index], flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 193 | |
| 194 | /* 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] | 195 | chip->gpio_dir[index] |= BIT(offset); |
| 196 | xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET + |
| 197 | xgpio_regoffset(chip, gpio), chip->gpio_dir[index]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 198 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 199 | spin_unlock_irqrestore(&chip->gpio_lock[index], flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * xgpio_dir_out - Set the direction of the specified GPIO signal as output. |
| 206 | * @gc: Pointer to gpio_chip device structure. |
| 207 | * @gpio: GPIO signal number. |
| 208 | * @val: Value to be written to specified signal. |
| 209 | * |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 210 | * This function sets the direction of specified GPIO signal as output. |
| 211 | * |
| 212 | * Return: |
| 213 | * 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] | 214 | * error otherwise it returns 0. |
| 215 | */ |
| 216 | static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) |
| 217 | { |
| 218 | unsigned long flags; |
| 219 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 220 | struct xgpio_instance *chip = gpiochip_get_data(gc); |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 221 | int index = xgpio_index(chip, gpio); |
| 222 | int offset = xgpio_offset(chip, gpio); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 223 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 224 | spin_lock_irqsave(&chip->gpio_lock[index], flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 225 | |
| 226 | /* Write state of GPIO signal */ |
| 227 | if (val) |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 228 | chip->gpio_state[index] |= BIT(offset); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 229 | else |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 230 | chip->gpio_state[index] &= ~BIT(offset); |
| 231 | xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + |
| 232 | xgpio_regoffset(chip, gpio), chip->gpio_state[index]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 233 | |
| 234 | /* 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] | 235 | chip->gpio_dir[index] &= ~BIT(offset); |
| 236 | xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET + |
| 237 | xgpio_regoffset(chip, gpio), chip->gpio_dir[index]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 238 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 239 | spin_unlock_irqrestore(&chip->gpio_lock[index], flags); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * xgpio_save_regs - Set initial values of GPIO pins |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 246 | * @mm_gc: Pointer to memory mapped GPIO chip structure |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 247 | */ |
| 248 | static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc) |
| 249 | { |
Guenter Roeck | de06c1d | 2016-01-06 16:20:10 -0800 | [diff] [blame] | 250 | struct xgpio_instance *chip = |
| 251 | container_of(mm_gc, struct xgpio_instance, mmchip); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 252 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 253 | xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state[0]); |
| 254 | xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir[0]); |
| 255 | |
| 256 | if (!chip->gpio_width[1]) |
| 257 | return; |
| 258 | |
Raphaël Teysseyre | 5b2c912 | 2015-06-24 09:19:45 +0200 | [diff] [blame] | 259 | xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + XGPIO_CHANNEL_OFFSET, |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 260 | chip->gpio_state[1]); |
Raphaël Teysseyre | 5b2c912 | 2015-06-24 09:19:45 +0200 | [diff] [blame] | 261 | xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET + XGPIO_CHANNEL_OFFSET, |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 262 | chip->gpio_dir[1]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 266 | * xgpio_remove - Remove method for the GPIO device. |
| 267 | * @pdev: pointer to the platform device |
| 268 | * |
| 269 | * This function remove gpiochips and frees all the allocated resources. |
Michal Simek | 3c1b5c9b | 2015-05-04 16:37:16 +0200 | [diff] [blame] | 270 | * |
| 271 | * Return: 0 always |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 272 | */ |
| 273 | static int xgpio_remove(struct platform_device *pdev) |
| 274 | { |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 275 | struct xgpio_instance *chip = platform_get_drvdata(pdev); |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 276 | |
Ricardo Ribalda Delgado | c458e45 | 2014-12-17 16:51:14 +0100 | [diff] [blame] | 277 | of_mm_gpiochip_remove(&chip->mmchip); |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /** |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 283 | * xgpio_of_probe - Probe method for the GPIO device. |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 284 | * @pdev: pointer to the platform device |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 285 | * |
Ricardo Ribalda Delgado | 4ae798f | 2014-12-17 16:51:11 +0100 | [diff] [blame] | 286 | * Return: |
| 287 | * It returns 0, if the driver is bound to the GPIO device, or |
| 288 | * a negative value if there is an error. |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 289 | */ |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 290 | static int xgpio_probe(struct platform_device *pdev) |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 291 | { |
| 292 | struct xgpio_instance *chip; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 293 | int status = 0; |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 294 | struct device_node *np = pdev->dev.of_node; |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 295 | u32 is_dual; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 296 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 297 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
| 298 | if (!chip) |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 299 | return -ENOMEM; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 300 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 301 | platform_set_drvdata(pdev, chip); |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 302 | |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 303 | /* Update GPIO state shadow register with default value */ |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 304 | of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 305 | |
| 306 | /* Update GPIO direction shadow register with default value */ |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 307 | if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0])) |
| 308 | chip->gpio_dir[0] = 0xFFFFFFFF; |
Michal Simek | 6f8bf50 | 2013-06-03 14:31:16 +0200 | [diff] [blame] | 309 | |
Gernot Vormayr | 1b4c5a6 | 2014-09-24 00:58:45 +0200 | [diff] [blame] | 310 | /* |
| 311 | * Check device node and parent device node for device width |
| 312 | * and assume default width of 32 |
| 313 | */ |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 314 | if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0])) |
| 315 | chip->gpio_width[0] = 32; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 316 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 317 | spin_lock_init(&chip->gpio_lock[0]); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 318 | |
Ricardo Ribalda Delgado | 1d6902d | 2014-12-17 16:51:12 +0100 | [diff] [blame] | 319 | if (of_property_read_u32(np, "xlnx,is-dual", &is_dual)) |
| 320 | is_dual = 0; |
| 321 | |
| 322 | if (is_dual) { |
| 323 | /* Update GPIO state shadow register with default value */ |
| 324 | of_property_read_u32(np, "xlnx,dout-default-2", |
| 325 | &chip->gpio_state[1]); |
| 326 | |
| 327 | /* Update GPIO direction shadow register with default value */ |
| 328 | if (of_property_read_u32(np, "xlnx,tri-default-2", |
| 329 | &chip->gpio_dir[1])) |
| 330 | chip->gpio_dir[1] = 0xFFFFFFFF; |
| 331 | |
| 332 | /* |
| 333 | * Check device node and parent device node for device width |
| 334 | * and assume default width of 32 |
| 335 | */ |
| 336 | if (of_property_read_u32(np, "xlnx,gpio2-width", |
| 337 | &chip->gpio_width[1])) |
| 338 | chip->gpio_width[1] = 32; |
| 339 | |
| 340 | spin_lock_init(&chip->gpio_lock[1]); |
| 341 | } |
| 342 | |
| 343 | chip->mmchip.gc.ngpio = chip->gpio_width[0] + chip->gpio_width[1]; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 344 | chip->mmchip.gc.parent = &pdev->dev; |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 345 | chip->mmchip.gc.direction_input = xgpio_dir_in; |
| 346 | chip->mmchip.gc.direction_output = xgpio_dir_out; |
| 347 | chip->mmchip.gc.get = xgpio_get; |
| 348 | chip->mmchip.gc.set = xgpio_set; |
Iban Rodriguez | 8e7c1b80 | 2016-06-03 14:54:41 +0200 | [diff] [blame] | 349 | chip->mmchip.gc.set_multiple = xgpio_set_multiple; |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 350 | |
| 351 | chip->mmchip.save_regs = xgpio_save_regs; |
| 352 | |
| 353 | /* Call the OF gpio helper to setup and register the GPIO device */ |
Linus Walleij | 097d88e | 2015-12-07 15:20:17 +0100 | [diff] [blame] | 354 | status = of_mm_gpiochip_add_data(np, &chip->mmchip, chip); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 355 | if (status) { |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 356 | pr_err("%pOF: error in probe function with status %d\n", |
| 357 | np, status); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 358 | return status; |
| 359 | } |
Michal Simek | 74600ee | 2013-06-03 14:31:17 +0200 | [diff] [blame] | 360 | |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
Jingoo Han | 9992bc9 | 2014-05-07 18:08:20 +0900 | [diff] [blame] | 364 | static const struct of_device_id xgpio_of_match[] = { |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 365 | { .compatible = "xlnx,xps-gpio-1.00.a", }, |
| 366 | { /* end of list */ }, |
| 367 | }; |
| 368 | |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 369 | MODULE_DEVICE_TABLE(of, xgpio_of_match); |
| 370 | |
| 371 | static struct platform_driver xgpio_plat_driver = { |
| 372 | .probe = xgpio_probe, |
| 373 | .remove = xgpio_remove, |
| 374 | .driver = { |
| 375 | .name = "gpio-xilinx", |
| 376 | .of_match_table = xgpio_of_match, |
| 377 | }, |
| 378 | }; |
| 379 | |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 380 | static int __init xgpio_init(void) |
| 381 | { |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 382 | return platform_driver_register(&xgpio_plat_driver); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 383 | } |
| 384 | |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 385 | subsys_initcall(xgpio_init); |
Ricardo Ribalda Delgado | 749564f | 2014-12-17 16:51:09 +0100 | [diff] [blame] | 386 | |
| 387 | static void __exit xgpio_exit(void) |
| 388 | { |
| 389 | platform_driver_unregister(&xgpio_plat_driver); |
| 390 | } |
| 391 | module_exit(xgpio_exit); |
John Linn | 0bcb606 | 2008-11-12 13:25:38 -0800 | [diff] [blame] | 392 | |
| 393 | MODULE_AUTHOR("Xilinx, Inc."); |
| 394 | MODULE_DESCRIPTION("Xilinx GPIO driver"); |
| 395 | MODULE_LICENSE("GPL"); |