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 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 26 | #include <plat/pincfg.h> |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 27 | #include <mach/hardware.h> |
| 28 | #include <mach/gpio.h> |
| 29 | |
| 30 | /* |
| 31 | * The GPIO module in the Nomadik family of Systems-on-Chip is an |
| 32 | * AMBA device, managing 32 pins and alternate functions. The logic block |
| 33 | * is currently only used in the Nomadik. |
| 34 | * |
| 35 | * Symbols in this file are called "nmk_gpio" for "nomadik gpio" |
| 36 | */ |
| 37 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 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; |
Rabin Vincent | c84c7c0 | 2010-03-17 15:19:04 +0530 | [diff] [blame^] | 47 | u32 backup[10]; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
Rabin Vincent | 6f9a974 | 2010-06-02 05:50:28 +0100 | [diff] [blame] | 50 | static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip, |
| 51 | unsigned offset, int gpio_mode) |
| 52 | { |
| 53 | u32 bit = 1 << offset; |
| 54 | u32 afunc, bfunc; |
| 55 | |
| 56 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit; |
| 57 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit; |
| 58 | if (gpio_mode & NMK_GPIO_ALT_A) |
| 59 | afunc |= bit; |
| 60 | if (gpio_mode & NMK_GPIO_ALT_B) |
| 61 | bfunc |= bit; |
| 62 | writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA); |
| 63 | writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB); |
| 64 | } |
| 65 | |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 66 | static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip, |
| 67 | unsigned offset, enum nmk_gpio_slpm mode) |
| 68 | { |
| 69 | u32 bit = 1 << offset; |
| 70 | u32 slpm; |
| 71 | |
| 72 | slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC); |
| 73 | if (mode == NMK_GPIO_SLPM_NOCHANGE) |
| 74 | slpm |= bit; |
| 75 | else |
| 76 | slpm &= ~bit; |
| 77 | writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC); |
| 78 | } |
| 79 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 80 | static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip, |
| 81 | unsigned offset, enum nmk_gpio_pull pull) |
| 82 | { |
| 83 | u32 bit = 1 << offset; |
| 84 | u32 pdis; |
| 85 | |
| 86 | pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS); |
| 87 | if (pull == NMK_GPIO_PULL_NONE) |
| 88 | pdis |= bit; |
| 89 | else |
| 90 | pdis &= ~bit; |
| 91 | writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS); |
| 92 | |
| 93 | if (pull == NMK_GPIO_PULL_UP) |
| 94 | writel(bit, nmk_chip->addr + NMK_GPIO_DATS); |
| 95 | else if (pull == NMK_GPIO_PULL_DOWN) |
| 96 | writel(bit, nmk_chip->addr + NMK_GPIO_DATC); |
| 97 | } |
| 98 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 99 | static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip, |
| 100 | unsigned offset) |
| 101 | { |
| 102 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
| 103 | } |
| 104 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 105 | static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip, |
| 106 | unsigned offset, int val) |
| 107 | { |
| 108 | if (val) |
| 109 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS); |
| 110 | else |
| 111 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC); |
| 112 | } |
| 113 | |
| 114 | static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip, |
| 115 | unsigned offset, int val) |
| 116 | { |
| 117 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS); |
| 118 | __nmk_gpio_set_output(nmk_chip, offset, val); |
| 119 | } |
| 120 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 121 | static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset, |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 122 | pin_cfg_t cfg, bool sleep) |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 123 | { |
| 124 | static const char *afnames[] = { |
| 125 | [NMK_GPIO_ALT_GPIO] = "GPIO", |
| 126 | [NMK_GPIO_ALT_A] = "A", |
| 127 | [NMK_GPIO_ALT_B] = "B", |
| 128 | [NMK_GPIO_ALT_C] = "C" |
| 129 | }; |
| 130 | static const char *pullnames[] = { |
| 131 | [NMK_GPIO_PULL_NONE] = "none", |
| 132 | [NMK_GPIO_PULL_UP] = "up", |
| 133 | [NMK_GPIO_PULL_DOWN] = "down", |
| 134 | [3] /* illegal */ = "??" |
| 135 | }; |
| 136 | static const char *slpmnames[] = { |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 137 | [NMK_GPIO_SLPM_INPUT] = "input/wakeup", |
| 138 | [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup", |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | int pin = PIN_NUM(cfg); |
| 142 | int pull = PIN_PULL(cfg); |
| 143 | int af = PIN_ALT(cfg); |
| 144 | int slpm = PIN_SLPM(cfg); |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 145 | int output = PIN_DIR(cfg); |
| 146 | int val = PIN_VAL(cfg); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 147 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 148 | dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n", |
| 149 | pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm], |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 150 | output ? "output " : "input", |
| 151 | output ? (val ? "high" : "low") : ""); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 152 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 153 | if (sleep) { |
| 154 | int slpm_pull = PIN_SLPM_PULL(cfg); |
| 155 | int slpm_output = PIN_SLPM_DIR(cfg); |
| 156 | int slpm_val = PIN_SLPM_VAL(cfg); |
| 157 | |
| 158 | /* |
| 159 | * The SLPM_* values are normal values + 1 to allow zero to |
| 160 | * mean "same as normal". |
| 161 | */ |
| 162 | if (slpm_pull) |
| 163 | pull = slpm_pull - 1; |
| 164 | if (slpm_output) |
| 165 | output = slpm_output - 1; |
| 166 | if (slpm_val) |
| 167 | val = slpm_val - 1; |
| 168 | |
| 169 | dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n", |
| 170 | pin, |
| 171 | slpm_pull ? pullnames[pull] : "same", |
| 172 | slpm_output ? (output ? "output" : "input") : "same", |
| 173 | slpm_val ? (val ? "high" : "low") : "same"); |
| 174 | } |
| 175 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 176 | if (output) |
| 177 | __nmk_gpio_make_output(nmk_chip, offset, val); |
| 178 | else { |
| 179 | __nmk_gpio_make_input(nmk_chip, offset); |
| 180 | __nmk_gpio_set_pull(nmk_chip, offset, pull); |
| 181 | } |
| 182 | |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 183 | __nmk_gpio_set_slpm(nmk_chip, offset, slpm); |
| 184 | __nmk_gpio_set_mode(nmk_chip, offset, af); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * nmk_config_pin - configure a pin's mux attributes |
| 189 | * @cfg: pin confguration |
| 190 | * |
| 191 | * Configures a pin's mode (alternate function or GPIO), its pull up status, |
| 192 | * and its sleep mode based on the specified configuration. The @cfg is |
| 193 | * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These |
| 194 | * are constructed using, and can be further enhanced with, the macros in |
| 195 | * plat/pincfg.h. |
| 196 | * |
| 197 | * If a pin's mode is set to GPIO, it is configured as an input to avoid |
| 198 | * side-effects. The gpio can be manipulated later using standard GPIO API |
| 199 | * calls. |
| 200 | */ |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 201 | int nmk_config_pin(pin_cfg_t cfg, bool sleep) |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 202 | { |
| 203 | struct nmk_gpio_chip *nmk_chip; |
| 204 | int gpio = PIN_NUM(cfg); |
| 205 | unsigned long flags; |
| 206 | |
| 207 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 208 | if (!nmk_chip) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 212 | __nmk_config_pin(nmk_chip, gpio - nmk_chip->chip.base, cfg, sleep); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 213 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | EXPORT_SYMBOL(nmk_config_pin); |
| 218 | |
| 219 | /** |
| 220 | * nmk_config_pins - configure several pins at once |
| 221 | * @cfgs: array of pin configurations |
| 222 | * @num: number of elments in the array |
| 223 | * |
| 224 | * Configures several pins using nmk_config_pin(). Refer to that function for |
| 225 | * further information. |
| 226 | */ |
| 227 | int nmk_config_pins(pin_cfg_t *cfgs, int num) |
| 228 | { |
| 229 | int ret = 0; |
| 230 | int i; |
| 231 | |
| 232 | for (i = 0; i < num; i++) { |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 233 | ret = nmk_config_pin(cfgs[i], false); |
Rabin Vincent | 378be06 | 2010-06-02 06:06:29 +0100 | [diff] [blame] | 234 | if (ret) |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | return ret; |
| 239 | } |
| 240 | EXPORT_SYMBOL(nmk_config_pins); |
| 241 | |
Rabin Vincent | dacdc96 | 2010-12-03 20:35:37 +0530 | [diff] [blame] | 242 | int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num) |
| 243 | { |
| 244 | int ret = 0; |
| 245 | int i; |
| 246 | |
| 247 | for (i = 0; i < num; i++) { |
| 248 | ret = nmk_config_pin(cfgs[i], true); |
| 249 | if (ret) |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | EXPORT_SYMBOL(nmk_config_pins_sleep); |
| 256 | |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 257 | /** |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 258 | * nmk_gpio_set_slpm() - configure the sleep mode of a pin |
| 259 | * @gpio: pin number |
| 260 | * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE, |
| 261 | * |
| 262 | * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is |
| 263 | * changed to an input (with pullup/down enabled) in sleep and deep sleep. If |
| 264 | * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was |
| 265 | * configured even when in sleep and deep sleep. |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 266 | * |
| 267 | * On DB8500v2 onwards, this setting loses the previous meaning and instead |
| 268 | * indicates if wakeup detection is enabled on the pin. Note that |
| 269 | * enable_irq_wake() will automatically enable wakeup detection. |
Rabin Vincent | 81a3c29 | 2010-05-27 12:39:23 +0100 | [diff] [blame] | 270 | */ |
| 271 | int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode) |
| 272 | { |
| 273 | struct nmk_gpio_chip *nmk_chip; |
| 274 | unsigned long flags; |
| 275 | |
| 276 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 277 | if (!nmk_chip) |
| 278 | return -EINVAL; |
| 279 | |
| 280 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 281 | __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode); |
| 282 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /** |
Rabin Vincent | 5b327ed | 2010-05-27 12:29:50 +0100 | [diff] [blame] | 288 | * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio |
| 289 | * @gpio: pin number |
| 290 | * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE |
| 291 | * |
| 292 | * Enables/disables pull up/down on a specified pin. This only takes effect if |
| 293 | * the pin is configured as an input (either explicitly or by the alternate |
| 294 | * function). |
| 295 | * |
| 296 | * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is |
| 297 | * configured as an input. Otherwise, due to the way the controller registers |
| 298 | * work, this function will change the value output on the pin. |
| 299 | */ |
| 300 | int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull) |
| 301 | { |
| 302 | struct nmk_gpio_chip *nmk_chip; |
| 303 | unsigned long flags; |
| 304 | |
| 305 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 306 | if (!nmk_chip) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 310 | __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull); |
| 311 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 316 | /* Mode functions */ |
| 317 | int nmk_gpio_set_mode(int gpio, int gpio_mode) |
| 318 | { |
| 319 | struct nmk_gpio_chip *nmk_chip; |
| 320 | unsigned long flags; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 321 | |
| 322 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 323 | if (!nmk_chip) |
| 324 | return -EINVAL; |
| 325 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 326 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Rabin Vincent | 6f9a974 | 2010-06-02 05:50:28 +0100 | [diff] [blame] | 327 | __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] | 328 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | EXPORT_SYMBOL(nmk_gpio_set_mode); |
| 333 | |
| 334 | int nmk_gpio_get_mode(int gpio) |
| 335 | { |
| 336 | struct nmk_gpio_chip *nmk_chip; |
| 337 | u32 afunc, bfunc, bit; |
| 338 | |
| 339 | nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio)); |
| 340 | if (!nmk_chip) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | bit = 1 << (gpio - nmk_chip->chip.base); |
| 344 | |
| 345 | afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit; |
| 346 | bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit; |
| 347 | |
| 348 | return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0); |
| 349 | } |
| 350 | EXPORT_SYMBOL(nmk_gpio_get_mode); |
| 351 | |
| 352 | |
| 353 | /* IRQ functions */ |
| 354 | static inline int nmk_gpio_get_bitmask(int gpio) |
| 355 | { |
| 356 | return 1 << (gpio % 32); |
| 357 | } |
| 358 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 359 | static void nmk_gpio_irq_ack(struct irq_data *d) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 360 | { |
| 361 | int gpio; |
| 362 | struct nmk_gpio_chip *nmk_chip; |
| 363 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 364 | gpio = NOMADIK_IRQ_TO_GPIO(d->irq); |
| 365 | nmk_chip = irq_data_get_irq_chip_data(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 366 | if (!nmk_chip) |
| 367 | return; |
| 368 | writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC); |
| 369 | } |
| 370 | |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 371 | enum nmk_gpio_irq_type { |
| 372 | NORMAL, |
| 373 | WAKE, |
| 374 | }; |
| 375 | |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 376 | static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip, |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 377 | int gpio, enum nmk_gpio_irq_type which, |
| 378 | bool enable) |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 379 | { |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 380 | u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC; |
| 381 | u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 382 | u32 bitmask = nmk_gpio_get_bitmask(gpio); |
| 383 | u32 reg; |
| 384 | |
| 385 | /* we must individually set/clear the two edges */ |
| 386 | if (nmk_chip->edge_rising & bitmask) { |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 387 | reg = readl(nmk_chip->addr + rimsc); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 388 | if (enable) |
| 389 | reg |= bitmask; |
| 390 | else |
| 391 | reg &= ~bitmask; |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 392 | writel(reg, nmk_chip->addr + rimsc); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 393 | } |
| 394 | if (nmk_chip->edge_falling & bitmask) { |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 395 | reg = readl(nmk_chip->addr + fimsc); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 396 | if (enable) |
| 397 | reg |= bitmask; |
| 398 | else |
| 399 | reg &= ~bitmask; |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 400 | writel(reg, nmk_chip->addr + fimsc); |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 404 | static int nmk_gpio_irq_modify(struct irq_data *d, enum nmk_gpio_irq_type which, |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 405 | bool enable) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 406 | { |
| 407 | int gpio; |
| 408 | struct nmk_gpio_chip *nmk_chip; |
| 409 | unsigned long flags; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 410 | u32 bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 411 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 412 | gpio = NOMADIK_IRQ_TO_GPIO(d->irq); |
| 413 | nmk_chip = irq_data_get_irq_chip_data(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 414 | bitmask = nmk_gpio_get_bitmask(gpio); |
| 415 | if (!nmk_chip) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 416 | return -EINVAL; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 417 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 418 | spin_lock_irqsave(&nmk_chip->lock, flags); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 419 | __nmk_gpio_irq_modify(nmk_chip, gpio, which, enable); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 420 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 421 | |
| 422 | return 0; |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 425 | static void nmk_gpio_irq_mask(struct irq_data *d) |
Rabin Vincent | 040e5ec | 2010-05-06 10:42:42 +0100 | [diff] [blame] | 426 | { |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 427 | nmk_gpio_irq_modify(d, NORMAL, false); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 428 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 429 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 430 | static void nmk_gpio_irq_unmask(struct irq_data *d) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 431 | { |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 432 | nmk_gpio_irq_modify(d, NORMAL, true); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 435 | static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 436 | { |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 437 | struct nmk_gpio_chip *nmk_chip; |
| 438 | unsigned long flags; |
| 439 | int gpio; |
| 440 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 441 | gpio = NOMADIK_IRQ_TO_GPIO(d->irq); |
| 442 | nmk_chip = irq_data_get_irq_chip_data(d); |
Rabin Vincent | 7e3f7e5 | 2010-09-02 11:28:05 +0100 | [diff] [blame] | 443 | if (!nmk_chip) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 447 | #ifdef CONFIG_ARCH_U8500 |
| 448 | if (cpu_is_u8500v2()) { |
| 449 | __nmk_gpio_set_slpm(nmk_chip, gpio, |
| 450 | on ? NMK_GPIO_SLPM_WAKEUP_ENABLE |
| 451 | : NMK_GPIO_SLPM_WAKEUP_DISABLE); |
| 452 | } |
| 453 | #endif |
| 454 | __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on); |
| 455 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 456 | |
| 457 | return 0; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 460 | static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 461 | { |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 462 | struct irq_desc *desc = irq_to_desc(d->irq); |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 463 | bool enabled = !(desc->status & IRQ_DISABLED); |
| 464 | bool wake = desc->wake_depth; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 465 | int gpio; |
| 466 | struct nmk_gpio_chip *nmk_chip; |
| 467 | unsigned long flags; |
| 468 | u32 bitmask; |
| 469 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 470 | gpio = NOMADIK_IRQ_TO_GPIO(d->irq); |
| 471 | nmk_chip = irq_data_get_irq_chip_data(d); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 472 | bitmask = nmk_gpio_get_bitmask(gpio); |
| 473 | if (!nmk_chip) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | if (type & IRQ_TYPE_LEVEL_HIGH) |
| 477 | return -EINVAL; |
| 478 | if (type & IRQ_TYPE_LEVEL_LOW) |
| 479 | return -EINVAL; |
| 480 | |
| 481 | spin_lock_irqsave(&nmk_chip->lock, flags); |
| 482 | |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 483 | if (enabled) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 484 | __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false); |
| 485 | |
| 486 | if (wake) |
| 487 | __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false); |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 488 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 489 | nmk_chip->edge_rising &= ~bitmask; |
| 490 | if (type & IRQ_TYPE_EDGE_RISING) |
| 491 | nmk_chip->edge_rising |= bitmask; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 492 | |
| 493 | nmk_chip->edge_falling &= ~bitmask; |
| 494 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 495 | nmk_chip->edge_falling |= bitmask; |
Rabin Vincent | 7a852d8 | 2010-05-06 10:43:55 +0100 | [diff] [blame] | 496 | |
| 497 | if (enabled) |
Rabin Vincent | 4d4e20f | 2010-06-16 06:09:34 +0100 | [diff] [blame] | 498 | __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true); |
| 499 | |
| 500 | if (wake) |
| 501 | __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 502 | |
| 503 | spin_unlock_irqrestore(&nmk_chip->lock, flags); |
| 504 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static struct irq_chip nmk_gpio_irq_chip = { |
| 509 | .name = "Nomadik-GPIO", |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 510 | .irq_ack = nmk_gpio_irq_ack, |
| 511 | .irq_mask = nmk_gpio_irq_mask, |
| 512 | .irq_unmask = nmk_gpio_irq_unmask, |
| 513 | .irq_set_type = nmk_gpio_irq_set_type, |
| 514 | .irq_set_wake = nmk_gpio_irq_set_wake, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc) |
| 518 | { |
| 519 | struct nmk_gpio_chip *nmk_chip; |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 520 | struct irq_chip *host_chip = get_irq_chip(irq); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 521 | unsigned int gpio_irq; |
| 522 | u32 pending; |
| 523 | unsigned int first_irq; |
| 524 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 525 | if (host_chip->irq_mask_ack) |
| 526 | host_chip->irq_mask_ack(&desc->irq_data); |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 527 | else { |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 528 | host_chip->irq_mask(&desc->irq_data); |
| 529 | if (host_chip->irq_ack) |
| 530 | host_chip->irq_ack(&desc->irq_data); |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 533 | nmk_chip = get_irq_data(irq); |
| 534 | first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base); |
| 535 | while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) { |
| 536 | gpio_irq = first_irq + __ffs(pending); |
| 537 | generic_handle_irq(gpio_irq); |
| 538 | } |
Rabin Vincent | aaedaa2 | 2010-03-03 04:50:27 +0100 | [diff] [blame] | 539 | |
Lennert Buytenhek | f272c00 | 2010-11-29 11:16:48 +0100 | [diff] [blame] | 540 | host_chip->irq_unmask(&desc->irq_data); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip) |
| 544 | { |
| 545 | unsigned int first_irq; |
| 546 | int i; |
| 547 | |
| 548 | first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base); |
Rabin Vincent | e493e06 | 2010-03-18 12:35:22 +0530 | [diff] [blame] | 549 | for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 550 | set_irq_chip(i, &nmk_gpio_irq_chip); |
| 551 | set_irq_handler(i, handle_edge_irq); |
| 552 | set_irq_flags(i, IRQF_VALID); |
| 553 | set_irq_chip_data(i, nmk_chip); |
Rabin Vincent | 2210d64 | 2010-05-06 10:45:18 +0100 | [diff] [blame] | 554 | set_irq_type(i, IRQ_TYPE_EDGE_FALLING); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 555 | } |
| 556 | set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler); |
| 557 | set_irq_data(nmk_chip->parent_irq, nmk_chip); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | /* I/O Functions */ |
| 562 | static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset) |
| 563 | { |
| 564 | struct nmk_gpio_chip *nmk_chip = |
| 565 | container_of(chip, struct nmk_gpio_chip, chip); |
| 566 | |
| 567 | writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC); |
| 568 | return 0; |
| 569 | } |
| 570 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 571 | static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset) |
| 572 | { |
| 573 | struct nmk_gpio_chip *nmk_chip = |
| 574 | container_of(chip, struct nmk_gpio_chip, chip); |
| 575 | u32 bit = 1 << offset; |
| 576 | |
| 577 | return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0; |
| 578 | } |
| 579 | |
| 580 | static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset, |
| 581 | int val) |
| 582 | { |
| 583 | struct nmk_gpio_chip *nmk_chip = |
| 584 | container_of(chip, struct nmk_gpio_chip, chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 585 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 586 | __nmk_gpio_set_output(nmk_chip, offset, val); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 589 | static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset, |
| 590 | int val) |
| 591 | { |
| 592 | struct nmk_gpio_chip *nmk_chip = |
| 593 | container_of(chip, struct nmk_gpio_chip, chip); |
| 594 | |
Rabin Vincent | 6720db7 | 2010-09-02 11:28:48 +0100 | [diff] [blame] | 595 | __nmk_gpio_make_output(nmk_chip, offset, val); |
Rabin Vincent | 6647c6c | 2010-05-27 12:22:42 +0100 | [diff] [blame] | 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 600 | static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 601 | { |
| 602 | struct nmk_gpio_chip *nmk_chip = |
| 603 | container_of(chip, struct nmk_gpio_chip, chip); |
| 604 | |
| 605 | return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset; |
| 606 | } |
| 607 | |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 608 | #ifdef CONFIG_DEBUG_FS |
| 609 | |
| 610 | #include <linux/seq_file.h> |
| 611 | |
| 612 | static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 613 | { |
| 614 | int mode; |
| 615 | unsigned i; |
| 616 | unsigned gpio = chip->base; |
| 617 | int is_out; |
| 618 | struct nmk_gpio_chip *nmk_chip = |
| 619 | container_of(chip, struct nmk_gpio_chip, chip); |
| 620 | const char *modes[] = { |
| 621 | [NMK_GPIO_ALT_GPIO] = "gpio", |
| 622 | [NMK_GPIO_ALT_A] = "altA", |
| 623 | [NMK_GPIO_ALT_B] = "altB", |
| 624 | [NMK_GPIO_ALT_C] = "altC", |
| 625 | }; |
| 626 | |
| 627 | for (i = 0; i < chip->ngpio; i++, gpio++) { |
| 628 | const char *label = gpiochip_is_requested(chip, i); |
| 629 | bool pull; |
| 630 | u32 bit = 1 << i; |
| 631 | |
| 632 | if (!label) |
| 633 | continue; |
| 634 | |
| 635 | is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit; |
| 636 | pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit); |
| 637 | mode = nmk_gpio_get_mode(gpio); |
| 638 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s", |
| 639 | gpio, label, |
| 640 | is_out ? "out" : "in ", |
| 641 | chip->get |
| 642 | ? (chip->get(chip, i) ? "hi" : "lo") |
| 643 | : "? ", |
| 644 | (mode < 0) ? "unknown" : modes[mode], |
| 645 | pull ? "pull" : "none"); |
| 646 | |
| 647 | if (!is_out) { |
| 648 | int irq = gpio_to_irq(gpio); |
| 649 | struct irq_desc *desc = irq_to_desc(irq); |
| 650 | |
| 651 | /* This races with request_irq(), set_irq_type(), |
| 652 | * and set_irq_wake() ... but those are "rare". |
| 653 | * |
| 654 | * More significantly, trigger type flags aren't |
| 655 | * currently maintained by genirq. |
| 656 | */ |
| 657 | if (irq >= 0 && desc->action) { |
| 658 | char *trigger; |
| 659 | |
| 660 | switch (desc->status & IRQ_TYPE_SENSE_MASK) { |
| 661 | case IRQ_TYPE_NONE: |
| 662 | trigger = "(default)"; |
| 663 | break; |
| 664 | case IRQ_TYPE_EDGE_FALLING: |
| 665 | trigger = "edge-falling"; |
| 666 | break; |
| 667 | case IRQ_TYPE_EDGE_RISING: |
| 668 | trigger = "edge-rising"; |
| 669 | break; |
| 670 | case IRQ_TYPE_EDGE_BOTH: |
| 671 | trigger = "edge-both"; |
| 672 | break; |
| 673 | case IRQ_TYPE_LEVEL_HIGH: |
| 674 | trigger = "level-high"; |
| 675 | break; |
| 676 | case IRQ_TYPE_LEVEL_LOW: |
| 677 | trigger = "level-low"; |
| 678 | break; |
| 679 | default: |
| 680 | trigger = "?trigger?"; |
| 681 | break; |
| 682 | } |
| 683 | |
| 684 | seq_printf(s, " irq-%d %s%s", |
| 685 | irq, trigger, |
| 686 | (desc->status & IRQ_WAKEUP) |
| 687 | ? " wakeup" : ""); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | seq_printf(s, "\n"); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | #else |
| 696 | #define nmk_gpio_dbg_show NULL |
| 697 | #endif |
| 698 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 699 | /* This structure is replicated for each GPIO block allocated at probe time */ |
| 700 | static struct gpio_chip nmk_gpio_template = { |
| 701 | .direction_input = nmk_gpio_make_input, |
| 702 | .get = nmk_gpio_get_input, |
| 703 | .direction_output = nmk_gpio_make_output, |
| 704 | .set = nmk_gpio_set_output, |
Rabin Vincent | 0d2aec9 | 2010-06-16 06:10:43 +0100 | [diff] [blame] | 705 | .to_irq = nmk_gpio_to_irq, |
Rabin Vincent | d0b543c | 2010-03-04 17:39:05 +0530 | [diff] [blame] | 706 | .dbg_show = nmk_gpio_dbg_show, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 707 | .can_sleep = 0, |
| 708 | }; |
| 709 | |
Uwe Kleine-König | fd0d67d | 2010-09-02 16:13:35 +0100 | [diff] [blame] | 710 | static int __devinit nmk_gpio_probe(struct platform_device *dev) |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 711 | { |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 712 | struct nmk_gpio_platform_data *pdata = dev->dev.platform_data; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 713 | struct nmk_gpio_chip *nmk_chip; |
| 714 | struct gpio_chip *chip; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 715 | struct resource *res; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 716 | struct clk *clk; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 717 | int irq; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 718 | int ret; |
| 719 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 720 | if (!pdata) |
| 721 | return -ENODEV; |
| 722 | |
| 723 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 724 | if (!res) { |
| 725 | ret = -ENOENT; |
| 726 | goto out; |
| 727 | } |
| 728 | |
| 729 | irq = platform_get_irq(dev, 0); |
| 730 | if (irq < 0) { |
| 731 | ret = irq; |
| 732 | goto out; |
| 733 | } |
| 734 | |
| 735 | if (request_mem_region(res->start, resource_size(res), |
| 736 | dev_name(&dev->dev)) == NULL) { |
| 737 | ret = -EBUSY; |
| 738 | goto out; |
| 739 | } |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 740 | |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 741 | clk = clk_get(&dev->dev, NULL); |
| 742 | if (IS_ERR(clk)) { |
| 743 | ret = PTR_ERR(clk); |
| 744 | goto out_release; |
| 745 | } |
| 746 | |
| 747 | clk_enable(clk); |
| 748 | |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 749 | nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL); |
| 750 | if (!nmk_chip) { |
| 751 | ret = -ENOMEM; |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 752 | goto out_clk; |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 753 | } |
| 754 | /* |
| 755 | * The virt address in nmk_chip->addr is in the nomadik register space, |
| 756 | * so we can simply convert the resource address, without remapping |
| 757 | */ |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 758 | nmk_chip->clk = clk; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 759 | nmk_chip->addr = io_p2v(res->start); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 760 | nmk_chip->chip = nmk_gpio_template; |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 761 | nmk_chip->parent_irq = irq; |
Rabin Vincent | c0fcb8d | 2010-03-03 04:48:54 +0100 | [diff] [blame] | 762 | spin_lock_init(&nmk_chip->lock); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 763 | |
| 764 | chip = &nmk_chip->chip; |
| 765 | chip->base = pdata->first_gpio; |
Rabin Vincent | e493e06 | 2010-03-18 12:35:22 +0530 | [diff] [blame] | 766 | chip->ngpio = pdata->num_gpio; |
Rabin Vincent | 8d568ae | 2010-12-08 11:07:54 +0530 | [diff] [blame] | 767 | chip->label = pdata->name ?: dev_name(&dev->dev); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 768 | chip->dev = &dev->dev; |
| 769 | chip->owner = THIS_MODULE; |
| 770 | |
| 771 | ret = gpiochip_add(&nmk_chip->chip); |
| 772 | if (ret) |
| 773 | goto out_free; |
| 774 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 775 | platform_set_drvdata(dev, nmk_chip); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 776 | |
| 777 | nmk_gpio_init_irq(nmk_chip); |
| 778 | |
| 779 | dev_info(&dev->dev, "Bits %i-%i at address %p\n", |
| 780 | nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr); |
| 781 | return 0; |
| 782 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 783 | out_free: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 784 | kfree(nmk_chip); |
Rabin Vincent | af7dc22 | 2010-05-06 11:14:17 +0100 | [diff] [blame] | 785 | out_clk: |
| 786 | clk_disable(clk); |
| 787 | clk_put(clk); |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 788 | out_release: |
| 789 | release_mem_region(res->start, resource_size(res)); |
| 790 | out: |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 791 | dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret, |
| 792 | pdata->first_gpio, pdata->first_gpio+31); |
| 793 | return ret; |
| 794 | } |
| 795 | |
Rabin Vincent | c84c7c0 | 2010-03-17 15:19:04 +0530 | [diff] [blame^] | 796 | #ifdef CONFIG_PM |
| 797 | static int nmk_gpio_pm(struct platform_device *dev, bool suspend) |
| 798 | { |
| 799 | struct nmk_gpio_chip *nmk_chip = platform_get_drvdata(dev); |
| 800 | int i; |
| 801 | static const unsigned int regs[] = { |
| 802 | NMK_GPIO_DAT, |
| 803 | NMK_GPIO_PDIS, |
| 804 | NMK_GPIO_DIR, |
| 805 | NMK_GPIO_AFSLA, |
| 806 | NMK_GPIO_AFSLB, |
| 807 | NMK_GPIO_SLPC, |
| 808 | NMK_GPIO_RIMSC, |
| 809 | NMK_GPIO_FIMSC, |
| 810 | NMK_GPIO_RWIMSC, |
| 811 | NMK_GPIO_FWIMSC, |
| 812 | }; |
| 813 | |
| 814 | BUILD_BUG_ON(ARRAY_SIZE(nmk_chip->backup) != ARRAY_SIZE(regs)); |
| 815 | |
| 816 | /* XXX: is this sufficient? what about pull-up/down configuration? */ |
| 817 | |
| 818 | for (i = 0; i < ARRAY_SIZE(regs); i++) { |
| 819 | if (suspend) |
| 820 | nmk_chip->backup[i] = readl(nmk_chip->addr + regs[i]); |
| 821 | else |
| 822 | writel(nmk_chip->backup[i], nmk_chip->addr + regs[i]); |
| 823 | } |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static int nmk_gpio_suspend(struct platform_device *dev, pm_message_t state) |
| 829 | { |
| 830 | return nmk_gpio_pm(dev, true); |
| 831 | } |
| 832 | |
| 833 | static int nmk_gpio_resume(struct platform_device *dev) |
| 834 | { |
| 835 | return nmk_gpio_pm(dev, false); |
| 836 | } |
| 837 | #else |
| 838 | #define nmk_gpio_suspend NULL |
| 839 | #define nmk_gpio_resume NULL |
| 840 | #endif |
| 841 | |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 842 | static struct platform_driver nmk_gpio_driver = { |
| 843 | .driver = { |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 844 | .owner = THIS_MODULE, |
| 845 | .name = "gpio", |
| 846 | }, |
| 847 | .probe = nmk_gpio_probe, |
Rabin Vincent | c84c7c0 | 2010-03-17 15:19:04 +0530 | [diff] [blame^] | 848 | .suspend = nmk_gpio_suspend, |
| 849 | .resume = nmk_gpio_resume, |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 850 | }; |
| 851 | |
| 852 | static int __init nmk_gpio_init(void) |
| 853 | { |
Rabin Vincent | 3e3c62c | 2010-03-03 04:52:34 +0100 | [diff] [blame] | 854 | return platform_driver_register(&nmk_gpio_driver); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Rabin Vincent | 33f45ea | 2010-06-02 06:09:52 +0100 | [diff] [blame] | 857 | core_initcall(nmk_gpio_init); |
Alessandro Rubini | 2ec1d35 | 2009-07-02 15:29:12 +0100 | [diff] [blame] | 858 | |
| 859 | MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini"); |
| 860 | MODULE_DESCRIPTION("Nomadik GPIO Driver"); |
| 861 | MODULE_LICENSE("GPL"); |
| 862 | |
| 863 | |