Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Generic GPIO driver for logic cells found in the Nomadik SoC |
| 3 | * |
| 4 | * Copyright (C) 2008,2009 STMicroelectronics |
| 5 | * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it> |
| 6 | * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/device.h> |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 18 | #include <linux/clk.h> |
| 19 | #include <linux/err.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 20 | #include <linux/gpio.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/irq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 25 | |
| 26 | #include <mach/hardware.h> |
| 27 | #include <mach/gpio.h> |
| 28 | |
| 29 | /* |
| 30 | * The GPIO module in the Nomadik family of Systems-on-Chip is an |
| 31 | * AMBA device, managing 32 pins and alternate functions. The logic block |
| 32 | * is currently only used in the Nomadik. |
| 33 | * |
| 34 | * Symbols in this file are called "nmk_gpio" for "nomadik gpio" |
| 35 | */ |
| 36 | |
| 37 | #define NMK_GPIO_PER_CHIP 32 |
| 38 | struct nmk_gpio_chip { |
| 39 | struct gpio_chip chip; |
| 40 | void __iomem *addr; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 41 | struct clk *clk; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 42 | unsigned int parent_irq; |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 43 | spinlock_t lock; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 44 | /* Keep track of configured edges */ |
| 45 | u32 edge_rising; |
| 46 | u32 edge_falling; |
| 47 | }; |
| 48 | |
Rabin Vincent | 6f9a974 | 2010-06-02 05:50:28 +0100 | [diff] [blame^] | 49 | static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip, |
| 50 | unsigned offset, int gpio_mode) |
| 51 | { |
| 52 | u32 bit = 1 << offset; |
| 53 | u32 afunc, bfunc; |
| 54 | |
| 55 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit; |
| 56 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit; |
| 57 | if (gpio_mode & NMK_GPIO_ALT_A) |
| 58 | afunc |= bit; |
| 59 | if (gpio_mode & NMK_GPIO_ALT_B) |
| 60 | bfunc |= bit; |
| 61 | writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA); |
| 62 | writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB); |
| 63 | } |
| 64 | |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 65 | static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip, |
| 66 | unsigned offset, enum nmk_gpio_slpm mode) |
| 67 | { |
| 68 | u32 bit = 1 << offset; |
| 69 | u32 slpm; |
| 70 | |
| 71 | slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC); |
| 72 | if (mode == NMK_GPIO_SLPM_NOCHANGE) |
| 73 | slpm |= bit; |
| 74 | else |
| 75 | slpm &= ~bit; |
| 76 | writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC); |
| 77 | } |
| 78 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 79 | static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip, |
| 80 | unsigned offset, enum nmk_gpio_pull pull) |
| 81 | { |
| 82 | u32 bit = 1 << offset; |
| 83 | u32 pdis; |
| 84 | |
| 85 | pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS); |
| 86 | if (pull == NMK_GPIO_PULL_NONE) |
| 87 | pdis |= bit; |
| 88 | else |
| 89 | pdis &= ~bit; |
| 90 | writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS); |
| 91 | |
| 92 | if (pull == NMK_GPIO_PULL_UP) |
| 93 | writel(bit, nmk_chip->addr + NMK_GPIO_DATS); |
| 94 | else if (pull == NMK_GPIO_PULL_DOWN) |
| 95 | writel(bit, nmk_chip->addr + NMK_GPIO_DATC); |
| 96 | } |
| 97 | |
| 98 | /** |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 99 | * nmk_gpio_set_slpm() - configure the sleep mode of a pin |
| 100 | * @gpio: pin number |
| 101 | * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE, |
| 102 | * |
| 103 | * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is |
| 104 | * changed to an input (with pullup/down enabled) in sleep and deep sleep. If |
| 105 | * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was |
| 106 | * configured even when in sleep and deep sleep. |
| 107 | */ |
| 108 | int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode) |
| 109 | { |
| 110 | struct nmk_gpio_chip *nmk_chip; |
| 111 | unsigned long flags; |
| 112 | |
| 113 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 114 | if (!nmk_chip) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 118 | __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode); |
| 119 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /** |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 125 | * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio |
| 126 | * @gpio: pin number |
| 127 | * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE |
| 128 | * |
| 129 | * Enables/disables pull up/down on a specified pin. This only takes effect if |
| 130 | * the pin is configured as an input (either explicitly or by the alternate |
| 131 | * function). |
| 132 | * |
| 133 | * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is |
| 134 | * configured as an input. Otherwise, due to the way the controller registers |
| 135 | * work, this function will change the value output on the pin. |
| 136 | */ |
| 137 | int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull) |
| 138 | { |
| 139 | struct nmk_gpio_chip *nmk_chip; |
| 140 | unsigned long flags; |
| 141 | |
| 142 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 143 | if (!nmk_chip) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 147 | __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull); |
| 148 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 153 | /* Mode functions */ |
| 154 | int nmk_gpio_set_mode(int gpio, int gpio_mode) |
| 155 | { |
| 156 | struct nmk_gpio_chip *nmk_chip; |
| 157 | unsigned long flags; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 158 | |
| 159 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 160 | if (!nmk_chip) |
| 161 | return -EINVAL; |
| 162 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 163 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Rabin Vincent | 6f9a974 | 2010-06-02 05:50:28 +0100 | [diff] [blame^] | 164 | __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 165 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | EXPORT_SYMBOL(nmk_gpio_set_mode); |
| 170 | |
| 171 | int nmk_gpio_get_mode(int gpio) |
| 172 | { |
| 173 | struct nmk_gpio_chip *nmk_chip; |
| 174 | u32 afunc, bfunc, bit; |
| 175 | |
| 176 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 177 | if (!nmk_chip) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | bit = 1 << (gpio - nmk_chip->chip.base); |
| 181 | |
| 182 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit; |
| 183 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit; |
| 184 | |
| 185 | return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0); |
| 186 | } |
| 187 | EXPORT_SYMBOL(nmk_gpio_get_mode); |
| 188 | |
| 189 | |
| 190 | /* IRQ functions */ |
| 191 | static inline int nmk_gpio_get_bitmask(int gpio) |
| 192 | { |
| 193 | return 1 << (gpio % 32); |
| 194 | } |
| 195 | |
| 196 | static void nmk_gpio_irq_ack(unsigned int irq) |
| 197 | { |
| 198 | int gpio; |
| 199 | struct nmk_gpio_chip *nmk_chip; |
| 200 | |
| 201 | gpio = NOMADIK_IRQ_TO_GPIO(irq); |
| 202 | nmk_chip = get_irq_chip_data(irq); |
| 203 | if (!nmk_chip) |
| 204 | return; |
| 205 | writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC); |
| 206 | } |
| 207 | |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 208 | static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip, |
| 209 | int gpio, bool enable) |
| 210 | { |
| 211 | u32 bitmask = nmk_gpio_get_bitmask(gpio); |
| 212 | u32 reg; |
| 213 | |
| 214 | /* we must individually set/clear the two edges */ |
| 215 | if (nmk_chip->edge_rising & bitmask) { |
| 216 | reg = readl(nmk_chip->addr + NMK_GPIO_RIMSC); |
| 217 | if (enable) |
| 218 | reg |= bitmask; |
| 219 | else |
| 220 | reg &= ~bitmask; |
| 221 | writel(reg, nmk_chip->addr + NMK_GPIO_RIMSC); |
| 222 | } |
| 223 | if (nmk_chip->edge_falling & bitmask) { |
| 224 | reg = readl(nmk_chip->addr + NMK_GPIO_FIMSC); |
| 225 | if (enable) |
| 226 | reg |= bitmask; |
| 227 | else |
| 228 | reg &= ~bitmask; |
| 229 | writel(reg, nmk_chip->addr + NMK_GPIO_FIMSC); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static void nmk_gpio_irq_modify(unsigned int irq, bool enable) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 234 | { |
| 235 | int gpio; |
| 236 | struct nmk_gpio_chip *nmk_chip; |
| 237 | unsigned long flags; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 238 | u32 bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 239 | |
| 240 | gpio = NOMADIK_IRQ_TO_GPIO(irq); |
| 241 | nmk_chip = get_irq_chip_data(irq); |
| 242 | bitmask = nmk_gpio_get_bitmask(gpio); |
| 243 | if (!nmk_chip) |
| 244 | return; |
| 245 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 246 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 247 | __nmk_gpio_irq_modify(nmk_chip, gpio, enable); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 248 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void nmk_gpio_irq_mask(unsigned int irq) |
| 252 | { |
| 253 | nmk_gpio_irq_modify(irq, false); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | static void nmk_gpio_irq_unmask(unsigned int irq) |
| 257 | { |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 258 | nmk_gpio_irq_modify(irq, true); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type) |
| 262 | { |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 263 | bool enabled = !(irq_to_desc(irq)->status & IRQ_DISABLED); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 264 | int gpio; |
| 265 | struct nmk_gpio_chip *nmk_chip; |
| 266 | unsigned long flags; |
| 267 | u32 bitmask; |
| 268 | |
| 269 | gpio = NOMADIK_IRQ_TO_GPIO(irq); |
| 270 | nmk_chip = get_irq_chip_data(irq); |
| 271 | bitmask = nmk_gpio_get_bitmask(gpio); |
| 272 | if (!nmk_chip) |
| 273 | return -EINVAL; |
| 274 | |
| 275 | if (type & IRQ_TYPE_LEVEL_HIGH) |
| 276 | return -EINVAL; |
| 277 | if (type & IRQ_TYPE_LEVEL_LOW) |
| 278 | return -EINVAL; |
| 279 | |
| 280 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 281 | |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 282 | if (enabled) |
| 283 | __nmk_gpio_irq_modify(nmk_chip, gpio, false); |
| 284 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 285 | nmk_chip->edge_rising &= ~bitmask; |
| 286 | if (type & IRQ_TYPE_EDGE_RISING) |
| 287 | nmk_chip->edge_rising |= bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 288 | |
| 289 | nmk_chip->edge_falling &= ~bitmask; |
| 290 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 291 | nmk_chip->edge_falling |= bitmask; |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 292 | |
| 293 | if (enabled) |
| 294 | __nmk_gpio_irq_modify(nmk_chip, gpio, true); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 295 | |
| 296 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 297 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static struct irq_chip nmk_gpio_irq_chip = { |
| 302 | .name = "Nomadik-GPIO", |
| 303 | .ack = nmk_gpio_irq_ack, |
| 304 | .mask = nmk_gpio_irq_mask, |
| 305 | .unmask = nmk_gpio_irq_unmask, |
| 306 | .set_type = nmk_gpio_irq_set_type, |
| 307 | }; |
| 308 | |
| 309 | static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc) |
| 310 | { |
| 311 | struct nmk_gpio_chip *nmk_chip; |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 312 | struct irq_chip *host_chip = get_irq_chip(irq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 313 | unsigned int gpio_irq; |
| 314 | u32 pending; |
| 315 | unsigned int first_irq; |
| 316 | |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 317 | if (host_chip->mask_ack) |
| 318 | host_chip->mask_ack(irq); |
| 319 | else { |
| 320 | host_chip->mask(irq); |
| 321 | if (host_chip->ack) |
| 322 | host_chip->ack(irq); |
| 323 | } |
| 324 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 325 | nmk_chip = get_irq_data(irq); |
| 326 | first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base); |
| 327 | while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) { |
| 328 | gpio_irq = first_irq + __ffs(pending); |
| 329 | generic_handle_irq(gpio_irq); |
| 330 | } |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 331 | |
| 332 | host_chip->unmask(irq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip) |
| 336 | { |
| 337 | unsigned int first_irq; |
| 338 | int i; |
| 339 | |
| 340 | first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base); |
| 341 | for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) { |
| 342 | set_irq_chip(i, &nmk_gpio_irq_chip); |
| 343 | set_irq_handler(i, handle_edge_irq); |
| 344 | set_irq_flags(i, IRQF_VALID); |
| 345 | set_irq_chip_data(i, nmk_chip); |
Rabin Vincent | 2210d64 | 2010-05-06 10:45:18 +0100 | [diff] [blame] | 346 | set_irq_type(i, IRQ_TYPE_EDGE_FALLING); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 347 | } |
| 348 | set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler); |
| 349 | set_irq_data(nmk_chip->parent_irq, nmk_chip); |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* I/O Functions */ |
| 354 | static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset) |
| 355 | { |
| 356 | struct nmk_gpio_chip *nmk_chip = |
| 357 | container_of(chip, struct nmk_gpio_chip, chip); |
| 358 | |
| 359 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
| 360 | return 0; |
| 361 | } |
| 362 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 363 | static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset) |
| 364 | { |
| 365 | struct nmk_gpio_chip *nmk_chip = |
| 366 | container_of(chip, struct nmk_gpio_chip, chip); |
| 367 | u32 bit = 1 << offset; |
| 368 | |
| 369 | return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0; |
| 370 | } |
| 371 | |
| 372 | static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset, |
| 373 | int val) |
| 374 | { |
| 375 | struct nmk_gpio_chip *nmk_chip = |
| 376 | container_of(chip, struct nmk_gpio_chip, chip); |
| 377 | u32 bit = 1 << offset; |
| 378 | |
| 379 | if (val) |
| 380 | writel(bit, nmk_chip->addr + NMK_GPIO_DATS); |
| 381 | else |
| 382 | writel(bit, nmk_chip->addr + NMK_GPIO_DATC); |
| 383 | } |
| 384 | |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 385 | static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset, |
| 386 | int val) |
| 387 | { |
| 388 | struct nmk_gpio_chip *nmk_chip = |
| 389 | container_of(chip, struct nmk_gpio_chip, chip); |
| 390 | |
| 391 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS); |
| 392 | nmk_gpio_set_output(chip, offset, val); |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 397 | /* This structure is replicated for each GPIO block allocated at probe time */ |
| 398 | static struct gpio_chip nmk_gpio_template = { |
| 399 | .direction_input = nmk_gpio_make_input, |
| 400 | .get = nmk_gpio_get_input, |
| 401 | .direction_output = nmk_gpio_make_output, |
| 402 | .set = nmk_gpio_set_output, |
| 403 | .ngpio = NMK_GPIO_PER_CHIP, |
| 404 | .can_sleep = 0, |
| 405 | }; |
| 406 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 407 | static int __init nmk_gpio_probe(struct platform_device *dev) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 408 | { |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 409 | struct nmk_gpio_platform_data *pdata = dev->dev.platform_data; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 410 | struct nmk_gpio_chip *nmk_chip; |
| 411 | struct gpio_chip *chip; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 412 | struct resource *res; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 413 | struct clk *clk; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 414 | int irq; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 415 | int ret; |
| 416 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 417 | if (!pdata) |
| 418 | return -ENODEV; |
| 419 | |
| 420 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 421 | if (!res) { |
| 422 | ret = -ENOENT; |
| 423 | goto out; |
| 424 | } |
| 425 | |
| 426 | irq = platform_get_irq(dev, 0); |
| 427 | if (irq < 0) { |
| 428 | ret = irq; |
| 429 | goto out; |
| 430 | } |
| 431 | |
| 432 | if (request_mem_region(res->start, resource_size(res), |
| 433 | dev_name(&dev->dev)) == NULL) { |
| 434 | ret = -EBUSY; |
| 435 | goto out; |
| 436 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 437 | |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 438 | clk = clk_get(&dev->dev, NULL); |
| 439 | if (IS_ERR(clk)) { |
| 440 | ret = PTR_ERR(clk); |
| 441 | goto out_release; |
| 442 | } |
| 443 | |
| 444 | clk_enable(clk); |
| 445 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 446 | nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL); |
| 447 | if (!nmk_chip) { |
| 448 | ret = -ENOMEM; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 449 | goto out_clk; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 450 | } |
| 451 | /* |
| 452 | * The virt address in nmk_chip->addr is in the nomadik register space, |
| 453 | * so we can simply convert the resource address, without remapping |
| 454 | */ |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 455 | nmk_chip->clk = clk; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 456 | nmk_chip->addr = io_p2v(res->start); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 457 | nmk_chip->chip = nmk_gpio_template; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 458 | nmk_chip->parent_irq = irq; |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 459 | spin_lock_init(&nmk_chip->lock); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 460 | |
| 461 | chip = &nmk_chip->chip; |
| 462 | chip->base = pdata->first_gpio; |
| 463 | chip->label = pdata->name; |
| 464 | chip->dev = &dev->dev; |
| 465 | chip->owner = THIS_MODULE; |
| 466 | |
| 467 | ret = gpiochip_add(&nmk_chip->chip); |
| 468 | if (ret) |
| 469 | goto out_free; |
| 470 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 471 | platform_set_drvdata(dev, nmk_chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 472 | |
| 473 | nmk_gpio_init_irq(nmk_chip); |
| 474 | |
| 475 | dev_info(&dev->dev, "Bits %i-%i at address %p\n", |
| 476 | nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr); |
| 477 | return 0; |
| 478 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 479 | out_free: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 480 | kfree(nmk_chip); |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 481 | out_clk: |
| 482 | clk_disable(clk); |
| 483 | clk_put(clk); |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 484 | out_release: |
| 485 | release_mem_region(res->start, resource_size(res)); |
| 486 | out: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 487 | dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret, |
| 488 | pdata->first_gpio, pdata->first_gpio+31); |
| 489 | return ret; |
| 490 | } |
| 491 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 492 | static int __exit nmk_gpio_remove(struct platform_device *dev) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 493 | { |
| 494 | struct nmk_gpio_chip *nmk_chip; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 495 | struct resource *res; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 496 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 497 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 498 | |
| 499 | nmk_chip = platform_get_drvdata(dev); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 500 | gpiochip_remove(&nmk_chip->chip); |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 501 | clk_disable(nmk_chip->clk); |
| 502 | clk_put(nmk_chip->clk); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 503 | kfree(nmk_chip); |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 504 | release_mem_region(res->start, resource_size(res)); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 509 | static struct platform_driver nmk_gpio_driver = { |
| 510 | .driver = { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 511 | .owner = THIS_MODULE, |
| 512 | .name = "gpio", |
| 513 | }, |
| 514 | .probe = nmk_gpio_probe, |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 515 | .remove = __exit_p(nmk_gpio_remove), |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 516 | .suspend = NULL, /* to be done */ |
| 517 | .resume = NULL, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | static int __init nmk_gpio_init(void) |
| 521 | { |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 522 | return platform_driver_register(&nmk_gpio_driver); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | arch_initcall(nmk_gpio_init); |
| 526 | |
| 527 | MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini"); |
| 528 | MODULE_DESCRIPTION("Nomadik GPIO Driver"); |
| 529 | MODULE_LICENSE("GPL"); |
| 530 | |
| 531 | |