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