Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de> |
| 3 | * JZ4740 platform GPIO support |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the |
| 7 | * Free Software Foundation; either version 2 of the License, or (at your |
| 8 | * option) any later version. |
| 9 | * |
| 10 | * You should have received a copy of the GNU General Public License along |
| 11 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 12 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/sysdev.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/gpio.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/bitops.h> |
| 27 | |
| 28 | #include <linux/debugfs.h> |
| 29 | #include <linux/seq_file.h> |
| 30 | |
| 31 | #include <asm/mach-jz4740/base.h> |
| 32 | |
| 33 | #define JZ4740_GPIO_BASE_A (32*0) |
| 34 | #define JZ4740_GPIO_BASE_B (32*1) |
| 35 | #define JZ4740_GPIO_BASE_C (32*2) |
| 36 | #define JZ4740_GPIO_BASE_D (32*3) |
| 37 | |
| 38 | #define JZ4740_GPIO_NUM_A 32 |
| 39 | #define JZ4740_GPIO_NUM_B 32 |
| 40 | #define JZ4740_GPIO_NUM_C 31 |
| 41 | #define JZ4740_GPIO_NUM_D 32 |
| 42 | |
| 43 | #define JZ4740_IRQ_GPIO_BASE_A (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_A) |
| 44 | #define JZ4740_IRQ_GPIO_BASE_B (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_B) |
| 45 | #define JZ4740_IRQ_GPIO_BASE_C (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_C) |
| 46 | #define JZ4740_IRQ_GPIO_BASE_D (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_D) |
| 47 | |
| 48 | #define JZ_REG_GPIO_PIN 0x00 |
| 49 | #define JZ_REG_GPIO_DATA 0x10 |
| 50 | #define JZ_REG_GPIO_DATA_SET 0x14 |
| 51 | #define JZ_REG_GPIO_DATA_CLEAR 0x18 |
| 52 | #define JZ_REG_GPIO_MASK 0x20 |
| 53 | #define JZ_REG_GPIO_MASK_SET 0x24 |
| 54 | #define JZ_REG_GPIO_MASK_CLEAR 0x28 |
| 55 | #define JZ_REG_GPIO_PULL 0x30 |
| 56 | #define JZ_REG_GPIO_PULL_SET 0x34 |
| 57 | #define JZ_REG_GPIO_PULL_CLEAR 0x38 |
| 58 | #define JZ_REG_GPIO_FUNC 0x40 |
| 59 | #define JZ_REG_GPIO_FUNC_SET 0x44 |
| 60 | #define JZ_REG_GPIO_FUNC_CLEAR 0x48 |
| 61 | #define JZ_REG_GPIO_SELECT 0x50 |
| 62 | #define JZ_REG_GPIO_SELECT_SET 0x54 |
| 63 | #define JZ_REG_GPIO_SELECT_CLEAR 0x58 |
| 64 | #define JZ_REG_GPIO_DIRECTION 0x60 |
| 65 | #define JZ_REG_GPIO_DIRECTION_SET 0x64 |
| 66 | #define JZ_REG_GPIO_DIRECTION_CLEAR 0x68 |
| 67 | #define JZ_REG_GPIO_TRIGGER 0x70 |
| 68 | #define JZ_REG_GPIO_TRIGGER_SET 0x74 |
| 69 | #define JZ_REG_GPIO_TRIGGER_CLEAR 0x78 |
| 70 | #define JZ_REG_GPIO_FLAG 0x80 |
| 71 | #define JZ_REG_GPIO_FLAG_CLEAR 0x14 |
| 72 | |
| 73 | #define GPIO_TO_BIT(gpio) BIT(gpio & 0x1f) |
| 74 | #define GPIO_TO_REG(gpio, reg) (gpio_to_jz_gpio_chip(gpio)->base + (reg)) |
| 75 | #define CHIP_TO_REG(chip, reg) (gpio_chip_to_jz_gpio_chip(chip)->base + (reg)) |
| 76 | |
| 77 | struct jz_gpio_chip { |
| 78 | unsigned int irq; |
| 79 | unsigned int irq_base; |
| 80 | uint32_t wakeup; |
| 81 | uint32_t suspend_mask; |
| 82 | uint32_t edge_trigger_both; |
| 83 | |
| 84 | void __iomem *base; |
| 85 | |
| 86 | spinlock_t lock; |
| 87 | |
| 88 | struct gpio_chip gpio_chip; |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 89 | struct sys_device sysdev; |
| 90 | }; |
| 91 | |
| 92 | static struct jz_gpio_chip jz4740_gpio_chips[]; |
| 93 | |
| 94 | static inline struct jz_gpio_chip *gpio_to_jz_gpio_chip(unsigned int gpio) |
| 95 | { |
| 96 | return &jz4740_gpio_chips[gpio >> 5]; |
| 97 | } |
| 98 | |
| 99 | static inline struct jz_gpio_chip *gpio_chip_to_jz_gpio_chip(struct gpio_chip *gpio_chip) |
| 100 | { |
| 101 | return container_of(gpio_chip, struct jz_gpio_chip, gpio_chip); |
| 102 | } |
| 103 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 104 | static inline struct jz_gpio_chip *irq_to_jz_gpio_chip(struct irq_data *data) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 105 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 106 | return irq_data_get_irq_chip_data(data); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static inline void jz_gpio_write_bit(unsigned int gpio, unsigned int reg) |
| 110 | { |
| 111 | writel(GPIO_TO_BIT(gpio), GPIO_TO_REG(gpio, reg)); |
| 112 | } |
| 113 | |
| 114 | int jz_gpio_set_function(int gpio, enum jz_gpio_function function) |
| 115 | { |
| 116 | if (function == JZ_GPIO_FUNC_NONE) { |
| 117 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_CLEAR); |
| 118 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR); |
| 119 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR); |
| 120 | } else { |
| 121 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_SET); |
| 122 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR); |
| 123 | switch (function) { |
| 124 | case JZ_GPIO_FUNC1: |
| 125 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR); |
| 126 | break; |
| 127 | case JZ_GPIO_FUNC3: |
| 128 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_SET); |
| 129 | case JZ_GPIO_FUNC2: /* Falltrough */ |
| 130 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_SET); |
| 131 | break; |
| 132 | default: |
| 133 | BUG(); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | EXPORT_SYMBOL_GPL(jz_gpio_set_function); |
| 141 | |
| 142 | int jz_gpio_bulk_request(const struct jz_gpio_bulk_request *request, size_t num) |
| 143 | { |
| 144 | size_t i; |
| 145 | int ret; |
| 146 | |
| 147 | for (i = 0; i < num; ++i, ++request) { |
| 148 | ret = gpio_request(request->gpio, request->name); |
| 149 | if (ret) |
| 150 | goto err; |
| 151 | jz_gpio_set_function(request->gpio, request->function); |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | |
| 156 | err: |
| 157 | for (--request; i > 0; --i, --request) { |
| 158 | gpio_free(request->gpio); |
| 159 | jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE); |
| 160 | } |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(jz_gpio_bulk_request); |
| 165 | |
| 166 | void jz_gpio_bulk_free(const struct jz_gpio_bulk_request *request, size_t num) |
| 167 | { |
| 168 | size_t i; |
| 169 | |
| 170 | for (i = 0; i < num; ++i, ++request) { |
| 171 | gpio_free(request->gpio); |
| 172 | jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE); |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | EXPORT_SYMBOL_GPL(jz_gpio_bulk_free); |
| 177 | |
| 178 | void jz_gpio_bulk_suspend(const struct jz_gpio_bulk_request *request, size_t num) |
| 179 | { |
| 180 | size_t i; |
| 181 | |
| 182 | for (i = 0; i < num; ++i, ++request) { |
| 183 | jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE); |
| 184 | jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_DIRECTION_CLEAR); |
| 185 | jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_PULL_SET); |
| 186 | } |
| 187 | } |
| 188 | EXPORT_SYMBOL_GPL(jz_gpio_bulk_suspend); |
| 189 | |
| 190 | void jz_gpio_bulk_resume(const struct jz_gpio_bulk_request *request, size_t num) |
| 191 | { |
| 192 | size_t i; |
| 193 | |
| 194 | for (i = 0; i < num; ++i, ++request) |
| 195 | jz_gpio_set_function(request->gpio, request->function); |
| 196 | } |
| 197 | EXPORT_SYMBOL_GPL(jz_gpio_bulk_resume); |
| 198 | |
| 199 | void jz_gpio_enable_pullup(unsigned gpio) |
| 200 | { |
| 201 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_CLEAR); |
| 202 | } |
| 203 | EXPORT_SYMBOL_GPL(jz_gpio_enable_pullup); |
| 204 | |
| 205 | void jz_gpio_disable_pullup(unsigned gpio) |
| 206 | { |
| 207 | jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_SET); |
| 208 | } |
| 209 | EXPORT_SYMBOL_GPL(jz_gpio_disable_pullup); |
| 210 | |
| 211 | static int jz_gpio_get_value(struct gpio_chip *chip, unsigned gpio) |
| 212 | { |
| 213 | return !!(readl(CHIP_TO_REG(chip, JZ_REG_GPIO_PIN)) & BIT(gpio)); |
| 214 | } |
| 215 | |
| 216 | static void jz_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value) |
| 217 | { |
| 218 | uint32_t __iomem *reg = CHIP_TO_REG(chip, JZ_REG_GPIO_DATA_SET); |
| 219 | reg += !value; |
| 220 | writel(BIT(gpio), reg); |
| 221 | } |
| 222 | |
| 223 | static int jz_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, |
| 224 | int value) |
| 225 | { |
| 226 | writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_SET)); |
| 227 | jz_gpio_set_value(chip, gpio, value); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int jz_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 233 | { |
| 234 | writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_CLEAR)); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | int jz_gpio_port_direction_input(int port, uint32_t mask) |
| 240 | { |
| 241 | writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_CLEAR)); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | EXPORT_SYMBOL(jz_gpio_port_direction_input); |
| 246 | |
| 247 | int jz_gpio_port_direction_output(int port, uint32_t mask) |
| 248 | { |
| 249 | writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_SET)); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | EXPORT_SYMBOL(jz_gpio_port_direction_output); |
| 254 | |
| 255 | void jz_gpio_port_set_value(int port, uint32_t value, uint32_t mask) |
| 256 | { |
| 257 | writel(~value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_CLEAR)); |
| 258 | writel(value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_SET)); |
| 259 | } |
| 260 | EXPORT_SYMBOL(jz_gpio_port_set_value); |
| 261 | |
| 262 | uint32_t jz_gpio_port_get_value(int port, uint32_t mask) |
| 263 | { |
| 264 | uint32_t value = readl(GPIO_TO_REG(port, JZ_REG_GPIO_PIN)); |
| 265 | |
| 266 | return value & mask; |
| 267 | } |
| 268 | EXPORT_SYMBOL(jz_gpio_port_get_value); |
| 269 | |
| 270 | int gpio_to_irq(unsigned gpio) |
| 271 | { |
| 272 | return JZ4740_IRQ_GPIO(0) + gpio; |
| 273 | } |
| 274 | EXPORT_SYMBOL_GPL(gpio_to_irq); |
| 275 | |
| 276 | int irq_to_gpio(unsigned irq) |
| 277 | { |
| 278 | return irq - JZ4740_IRQ_GPIO(0); |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(irq_to_gpio); |
| 281 | |
| 282 | #define IRQ_TO_BIT(irq) BIT(irq_to_gpio(irq) & 0x1f) |
| 283 | |
| 284 | static void jz_gpio_check_trigger_both(struct jz_gpio_chip *chip, unsigned int irq) |
| 285 | { |
| 286 | uint32_t value; |
| 287 | void __iomem *reg; |
| 288 | uint32_t mask = IRQ_TO_BIT(irq); |
| 289 | |
| 290 | if (!(chip->edge_trigger_both & mask)) |
| 291 | return; |
| 292 | |
| 293 | reg = chip->base; |
| 294 | |
| 295 | value = readl(chip->base + JZ_REG_GPIO_PIN); |
| 296 | if (value & mask) |
| 297 | reg += JZ_REG_GPIO_DIRECTION_CLEAR; |
| 298 | else |
| 299 | reg += JZ_REG_GPIO_DIRECTION_SET; |
| 300 | |
| 301 | writel(mask, reg); |
| 302 | } |
| 303 | |
| 304 | static void jz_gpio_irq_demux_handler(unsigned int irq, struct irq_desc *desc) |
| 305 | { |
| 306 | uint32_t flag; |
| 307 | unsigned int gpio_irq; |
| 308 | unsigned int gpio_bank; |
Thomas Gleixner | e4ec798 | 2011-03-27 15:19:28 +0200 | [diff] [blame^] | 309 | struct jz_gpio_chip *chip = irq_desc_get_handler_data(desc); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 310 | |
| 311 | gpio_bank = JZ4740_IRQ_GPIO0 - irq; |
| 312 | |
| 313 | flag = readl(chip->base + JZ_REG_GPIO_FLAG); |
| 314 | |
| 315 | if (!flag) |
| 316 | return; |
| 317 | |
| 318 | gpio_irq = __fls(flag); |
| 319 | |
| 320 | jz_gpio_check_trigger_both(chip, irq); |
| 321 | |
| 322 | gpio_irq += (gpio_bank << 5) + JZ4740_IRQ_GPIO(0); |
| 323 | |
| 324 | generic_handle_irq(gpio_irq); |
| 325 | }; |
| 326 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 327 | static inline void jz_gpio_set_irq_bit(struct irq_data *data, unsigned int reg) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 328 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 329 | struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data); |
| 330 | writel(IRQ_TO_BIT(data->irq), chip->base + reg); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 333 | static void jz_gpio_irq_mask(struct irq_data *data) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 334 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 335 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_MASK_SET); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 338 | static void jz_gpio_irq_unmask(struct irq_data *data) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 339 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 340 | struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 341 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 342 | jz_gpio_check_trigger_both(chip, data->irq); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 343 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 344 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_MASK_CLEAR); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | /* TODO: Check if function is gpio */ |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 348 | static unsigned int jz_gpio_irq_startup(struct irq_data *data) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 349 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 350 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_SELECT_SET); |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 351 | jz_gpio_irq_unmask(data); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 355 | static void jz_gpio_irq_shutdown(struct irq_data *data) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 356 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 357 | jz_gpio_irq_mask(data); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 358 | |
| 359 | /* Set direction to input */ |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 360 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR); |
| 361 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_SELECT_CLEAR); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 364 | static void jz_gpio_irq_ack(struct irq_data *data) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 365 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 366 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_FLAG_CLEAR); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 367 | }; |
| 368 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 369 | static int jz_gpio_irq_set_type(struct irq_data *data, unsigned int flow_type) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 370 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 371 | struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data); |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 372 | unsigned int irq = data->irq; |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 373 | |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 374 | if (flow_type == IRQ_TYPE_EDGE_BOTH) { |
| 375 | uint32_t value = readl(chip->base + JZ_REG_GPIO_PIN); |
| 376 | if (value & IRQ_TO_BIT(irq)) |
| 377 | flow_type = IRQ_TYPE_EDGE_FALLING; |
| 378 | else |
| 379 | flow_type = IRQ_TYPE_EDGE_RISING; |
| 380 | chip->edge_trigger_both |= IRQ_TO_BIT(irq); |
| 381 | } else { |
| 382 | chip->edge_trigger_both &= ~IRQ_TO_BIT(irq); |
| 383 | } |
| 384 | |
| 385 | switch (flow_type) { |
| 386 | case IRQ_TYPE_EDGE_RISING: |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 387 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_SET); |
| 388 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_SET); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 389 | break; |
| 390 | case IRQ_TYPE_EDGE_FALLING: |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 391 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR); |
| 392 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_SET); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 393 | break; |
| 394 | case IRQ_TYPE_LEVEL_HIGH: |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 395 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_SET); |
| 396 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_CLEAR); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 397 | break; |
| 398 | case IRQ_TYPE_LEVEL_LOW: |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 399 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR); |
| 400 | jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_CLEAR); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 401 | break; |
| 402 | default: |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 409 | static int jz_gpio_irq_set_wake(struct irq_data *data, unsigned int on) |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 410 | { |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 411 | struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 412 | spin_lock(&chip->lock); |
| 413 | if (on) |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 414 | chip->wakeup |= IRQ_TO_BIT(data->irq); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 415 | else |
Thomas Gleixner | 42b64f3 | 2011-03-23 21:08:53 +0000 | [diff] [blame] | 416 | chip->wakeup &= ~IRQ_TO_BIT(data->irq); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 417 | spin_unlock(&chip->lock); |
| 418 | |
Thomas Gleixner | e4ec798 | 2011-03-27 15:19:28 +0200 | [diff] [blame^] | 419 | irq_set_irq_wake(chip->irq, on); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Lars-Peter Clausen | cd11d14 | 2011-03-23 21:08:54 +0000 | [diff] [blame] | 423 | static struct irq_chip jz_gpio_irq_chip = { |
| 424 | .name = "GPIO", |
| 425 | .irq_mask = jz_gpio_irq_mask, |
| 426 | .irq_unmask = jz_gpio_irq_unmask, |
| 427 | .irq_ack = jz_gpio_irq_ack, |
| 428 | .irq_startup = jz_gpio_irq_startup, |
| 429 | .irq_shutdown = jz_gpio_irq_shutdown, |
| 430 | .irq_set_type = jz_gpio_irq_set_type, |
| 431 | .irq_set_wake = jz_gpio_irq_set_wake, |
Lars-Peter Clausen | fe92a23 | 2011-03-23 21:08:55 +0000 | [diff] [blame] | 432 | .flags = IRQCHIP_SET_TYPE_MASKED, |
Lars-Peter Clausen | cd11d14 | 2011-03-23 21:08:54 +0000 | [diff] [blame] | 433 | }; |
| 434 | |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 435 | /* |
| 436 | * This lock class tells lockdep that GPIO irqs are in a different |
| 437 | * category than their parents, so it won't report false recursion. |
| 438 | */ |
| 439 | static struct lock_class_key gpio_lock_class; |
| 440 | |
| 441 | #define JZ4740_GPIO_CHIP(_bank) { \ |
| 442 | .irq_base = JZ4740_IRQ_GPIO_BASE_ ## _bank, \ |
| 443 | .gpio_chip = { \ |
| 444 | .label = "Bank " # _bank, \ |
| 445 | .owner = THIS_MODULE, \ |
| 446 | .set = jz_gpio_set_value, \ |
| 447 | .get = jz_gpio_get_value, \ |
| 448 | .direction_output = jz_gpio_direction_output, \ |
| 449 | .direction_input = jz_gpio_direction_input, \ |
| 450 | .base = JZ4740_GPIO_BASE_ ## _bank, \ |
| 451 | .ngpio = JZ4740_GPIO_NUM_ ## _bank, \ |
| 452 | }, \ |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static struct jz_gpio_chip jz4740_gpio_chips[] = { |
| 456 | JZ4740_GPIO_CHIP(A), |
| 457 | JZ4740_GPIO_CHIP(B), |
| 458 | JZ4740_GPIO_CHIP(C), |
| 459 | JZ4740_GPIO_CHIP(D), |
| 460 | }; |
| 461 | |
| 462 | static inline struct jz_gpio_chip *sysdev_to_chip(struct sys_device *dev) |
| 463 | { |
| 464 | return container_of(dev, struct jz_gpio_chip, sysdev); |
| 465 | } |
| 466 | |
| 467 | static int jz4740_gpio_suspend(struct sys_device *dev, pm_message_t state) |
| 468 | { |
| 469 | struct jz_gpio_chip *chip = sysdev_to_chip(dev); |
| 470 | |
| 471 | chip->suspend_mask = readl(chip->base + JZ_REG_GPIO_MASK); |
| 472 | writel(~(chip->wakeup), chip->base + JZ_REG_GPIO_MASK_SET); |
| 473 | writel(chip->wakeup, chip->base + JZ_REG_GPIO_MASK_CLEAR); |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static int jz4740_gpio_resume(struct sys_device *dev) |
| 479 | { |
| 480 | struct jz_gpio_chip *chip = sysdev_to_chip(dev); |
| 481 | uint32_t mask = chip->suspend_mask; |
| 482 | |
| 483 | writel(~mask, chip->base + JZ_REG_GPIO_MASK_CLEAR); |
| 484 | writel(mask, chip->base + JZ_REG_GPIO_MASK_SET); |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static struct sysdev_class jz4740_gpio_sysdev_class = { |
| 490 | .name = "gpio", |
| 491 | .suspend = jz4740_gpio_suspend, |
| 492 | .resume = jz4740_gpio_resume, |
| 493 | }; |
| 494 | |
| 495 | static int jz4740_gpio_chip_init(struct jz_gpio_chip *chip, unsigned int id) |
| 496 | { |
| 497 | int ret, irq; |
| 498 | |
| 499 | chip->sysdev.id = id; |
| 500 | chip->sysdev.cls = &jz4740_gpio_sysdev_class; |
| 501 | ret = sysdev_register(&chip->sysdev); |
| 502 | |
| 503 | if (ret) |
| 504 | return ret; |
| 505 | |
| 506 | spin_lock_init(&chip->lock); |
| 507 | |
| 508 | chip->base = ioremap(JZ4740_GPIO_BASE_ADDR + (id * 0x100), 0x100); |
| 509 | |
| 510 | gpiochip_add(&chip->gpio_chip); |
| 511 | |
| 512 | chip->irq = JZ4740_IRQ_INTC_GPIO(id); |
Thomas Gleixner | e4ec798 | 2011-03-27 15:19:28 +0200 | [diff] [blame^] | 513 | irq_set_handler_data(chip->irq, chip); |
| 514 | irq_set_chained_handler(chip->irq, jz_gpio_irq_demux_handler); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 515 | |
| 516 | for (irq = chip->irq_base; irq < chip->irq_base + chip->gpio_chip.ngpio; ++irq) { |
Lars-Peter Clausen | fe92a23 | 2011-03-23 21:08:55 +0000 | [diff] [blame] | 517 | irq_set_lockdep_class(irq, &gpio_lock_class); |
Thomas Gleixner | e4ec798 | 2011-03-27 15:19:28 +0200 | [diff] [blame^] | 518 | irq_set_chip_data(irq, chip); |
| 519 | irq_set_chip_and_handler(irq, &jz_gpio_irq_chip, |
| 520 | handle_level_irq); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int __init jz4740_gpio_init(void) |
| 527 | { |
| 528 | unsigned int i; |
| 529 | int ret; |
| 530 | |
| 531 | ret = sysdev_class_register(&jz4740_gpio_sysdev_class); |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | |
| 535 | for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i) |
| 536 | jz4740_gpio_chip_init(&jz4740_gpio_chips[i], i); |
| 537 | |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 538 | printk(KERN_INFO "JZ4740 GPIO initialized\n"); |
Lars-Peter Clausen | a55f450 | 2010-07-17 11:11:19 +0000 | [diff] [blame] | 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | arch_initcall(jz4740_gpio_init); |
| 543 | |
| 544 | #ifdef CONFIG_DEBUG_FS |
| 545 | |
| 546 | static inline void gpio_seq_reg(struct seq_file *s, struct jz_gpio_chip *chip, |
| 547 | const char *name, unsigned int reg) |
| 548 | { |
| 549 | seq_printf(s, "\t%s: %08x\n", name, readl(chip->base + reg)); |
| 550 | } |
| 551 | |
| 552 | static int gpio_regs_show(struct seq_file *s, void *unused) |
| 553 | { |
| 554 | struct jz_gpio_chip *chip = jz4740_gpio_chips; |
| 555 | int i; |
| 556 | |
| 557 | for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i, ++chip) { |
| 558 | seq_printf(s, "==GPIO %d==\n", i); |
| 559 | gpio_seq_reg(s, chip, "Pin", JZ_REG_GPIO_PIN); |
| 560 | gpio_seq_reg(s, chip, "Data", JZ_REG_GPIO_DATA); |
| 561 | gpio_seq_reg(s, chip, "Mask", JZ_REG_GPIO_MASK); |
| 562 | gpio_seq_reg(s, chip, "Pull", JZ_REG_GPIO_PULL); |
| 563 | gpio_seq_reg(s, chip, "Func", JZ_REG_GPIO_FUNC); |
| 564 | gpio_seq_reg(s, chip, "Select", JZ_REG_GPIO_SELECT); |
| 565 | gpio_seq_reg(s, chip, "Direction", JZ_REG_GPIO_DIRECTION); |
| 566 | gpio_seq_reg(s, chip, "Trigger", JZ_REG_GPIO_TRIGGER); |
| 567 | gpio_seq_reg(s, chip, "Flag", JZ_REG_GPIO_FLAG); |
| 568 | } |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int gpio_regs_open(struct inode *inode, struct file *file) |
| 574 | { |
| 575 | return single_open(file, gpio_regs_show, NULL); |
| 576 | } |
| 577 | |
| 578 | static const struct file_operations gpio_regs_operations = { |
| 579 | .open = gpio_regs_open, |
| 580 | .read = seq_read, |
| 581 | .llseek = seq_lseek, |
| 582 | .release = single_release, |
| 583 | }; |
| 584 | |
| 585 | static int __init gpio_debugfs_init(void) |
| 586 | { |
| 587 | (void) debugfs_create_file("jz_regs_gpio", S_IFREG | S_IRUGO, |
| 588 | NULL, NULL, &gpio_regs_operations); |
| 589 | return 0; |
| 590 | } |
| 591 | subsys_initcall(gpio_debugfs_init); |
| 592 | |
| 593 | #endif |