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