Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * U300 GPIO module. |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 3 | * |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 4 | * Copyright (C) 2007-2012 ST-Ericsson AB |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 5 | * License terms: GNU General Public License (GPL) version 2 |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 6 | * COH 901 571/3 - Used in DB3210 (U365 2.0) and DB3350 (U335 1.0) |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 7 | * Author: Linus Walleij <linus.walleij@linaro.org> |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 8 | * Author: Jonas Aaberg <jonas.aberg@stericsson.com> |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 9 | */ |
| 10 | #include <linux/module.h> |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 11 | #include <linux/irq.h> |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/io.h> |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 16 | #include <linux/irqdomain.h> |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 17 | #include <linux/clk.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/gpio.h> |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 21 | #include <linux/list.h> |
| 22 | #include <linux/slab.h> |
Linus Walleij | 28a8d14 | 2012-02-09 01:52:22 +0100 | [diff] [blame] | 23 | #include <linux/pinctrl/consumer.h> |
Linus Walleij | dc0b1aa | 2011-11-16 21:58:10 +0100 | [diff] [blame] | 24 | #include <linux/pinctrl/pinconf-generic.h> |
Linus Walleij | 6517285 | 2012-08-13 10:56:43 +0200 | [diff] [blame] | 25 | #include <linux/platform_data/pinctrl-coh901.h> |
Linus Walleij | dc0b1aa | 2011-11-16 21:58:10 +0100 | [diff] [blame] | 26 | #include "pinctrl-coh901.h" |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 27 | |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 28 | #define U300_GPIO_PORT_STRIDE (0x30) |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 29 | /* |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 30 | * Control Register 32bit (R/W) |
| 31 | * bit 15-9 (mask 0x0000FE00) contains the number of cores. 8*cores |
| 32 | * gives the number of GPIO pins. |
| 33 | * bit 8-2 (mask 0x000001FC) contains the core version ID. |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 34 | */ |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 35 | #define U300_GPIO_CR (0x00) |
| 36 | #define U300_GPIO_CR_SYNC_SEL_ENABLE (0x00000002UL) |
| 37 | #define U300_GPIO_CR_BLOCK_CLKRQ_ENABLE (0x00000001UL) |
| 38 | #define U300_GPIO_PXPDIR (0x04) |
| 39 | #define U300_GPIO_PXPDOR (0x08) |
| 40 | #define U300_GPIO_PXPCR (0x0C) |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 41 | #define U300_GPIO_PXPCR_ALL_PINS_MODE_MASK (0x0000FFFFUL) |
| 42 | #define U300_GPIO_PXPCR_PIN_MODE_MASK (0x00000003UL) |
| 43 | #define U300_GPIO_PXPCR_PIN_MODE_SHIFT (0x00000002UL) |
| 44 | #define U300_GPIO_PXPCR_PIN_MODE_INPUT (0x00000000UL) |
| 45 | #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL (0x00000001UL) |
| 46 | #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN (0x00000002UL) |
| 47 | #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE (0x00000003UL) |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 48 | #define U300_GPIO_PXPER (0x10) |
| 49 | #define U300_GPIO_PXPER_ALL_PULL_UP_DISABLE_MASK (0x000000FFUL) |
| 50 | #define U300_GPIO_PXPER_PULL_UP_DISABLE (0x00000001UL) |
| 51 | #define U300_GPIO_PXIEV (0x14) |
| 52 | #define U300_GPIO_PXIEN (0x18) |
| 53 | #define U300_GPIO_PXIFR (0x1C) |
| 54 | #define U300_GPIO_PXICR (0x20) |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 55 | #define U300_GPIO_PXICR_ALL_IRQ_CONFIG_MASK (0x000000FFUL) |
| 56 | #define U300_GPIO_PXICR_IRQ_CONFIG_MASK (0x00000001UL) |
| 57 | #define U300_GPIO_PXICR_IRQ_CONFIG_FALLING_EDGE (0x00000000UL) |
| 58 | #define U300_GPIO_PXICR_IRQ_CONFIG_RISING_EDGE (0x00000001UL) |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 59 | |
| 60 | /* 8 bits per port, no version has more than 7 ports */ |
| 61 | #define U300_GPIO_PINS_PER_PORT 8 |
| 62 | #define U300_GPIO_MAX (U300_GPIO_PINS_PER_PORT * 7) |
| 63 | |
| 64 | struct u300_gpio { |
| 65 | struct gpio_chip chip; |
| 66 | struct list_head port_list; |
| 67 | struct clk *clk; |
| 68 | struct resource *memres; |
| 69 | void __iomem *base; |
| 70 | struct device *dev; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 71 | u32 stride; |
| 72 | /* Register offsets */ |
| 73 | u32 pcr; |
| 74 | u32 dor; |
| 75 | u32 dir; |
| 76 | u32 per; |
| 77 | u32 icr; |
| 78 | u32 ien; |
| 79 | u32 iev; |
| 80 | }; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 81 | |
| 82 | struct u300_gpio_port { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 83 | struct list_head node; |
| 84 | struct u300_gpio *gpio; |
| 85 | char name[8]; |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 86 | struct irq_domain *domain; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 87 | int irq; |
| 88 | int number; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 89 | u8 toggle_edge_mode; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 92 | /* |
| 93 | * Macro to expand to read a specific register found in the "gpio" |
| 94 | * struct. It requires the struct u300_gpio *gpio variable to exist in |
| 95 | * its context. It calculates the port offset from the given pin |
| 96 | * offset, muliplies by the port stride and adds the register offset |
| 97 | * so it provides a pointer to the desired register. |
| 98 | */ |
| 99 | #define U300_PIN_REG(pin, reg) \ |
| 100 | (gpio->base + (pin >> 3) * gpio->stride + gpio->reg) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 101 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 102 | /* |
| 103 | * Provides a bitmask for a specific gpio pin inside an 8-bit GPIO |
| 104 | * register. |
| 105 | */ |
| 106 | #define U300_PIN_BIT(pin) \ |
| 107 | (1 << (pin & 0x07)) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 108 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 109 | struct u300_gpio_confdata { |
| 110 | u16 bias_mode; |
| 111 | bool output; |
| 112 | int outval; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 115 | /* BS335 has seven ports of 8 bits each = GPIO pins 0..55 */ |
| 116 | #define BS335_GPIO_NUM_PORTS 7 |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 117 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 118 | #define U300_FLOATING_INPUT { \ |
Linus Walleij | a050b3e | 2011-11-16 20:10:09 +0100 | [diff] [blame] | 119 | .bias_mode = PIN_CONFIG_BIAS_HIGH_IMPEDANCE, \ |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 120 | .output = false, \ |
| 121 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 122 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 123 | #define U300_PULL_UP_INPUT { \ |
Linus Walleij | a050b3e | 2011-11-16 20:10:09 +0100 | [diff] [blame] | 124 | .bias_mode = PIN_CONFIG_BIAS_PULL_UP, \ |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 125 | .output = false, \ |
| 126 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 127 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 128 | #define U300_OUTPUT_LOW { \ |
| 129 | .output = true, \ |
| 130 | .outval = 0, \ |
| 131 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 132 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 133 | #define U300_OUTPUT_HIGH { \ |
| 134 | .output = true, \ |
| 135 | .outval = 1, \ |
| 136 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 137 | |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 138 | /* Initial configuration */ |
Uwe Kleine-König | 122dbe7 | 2012-03-30 22:04:51 +0200 | [diff] [blame] | 139 | static const struct __initconst u300_gpio_confdata |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 140 | bs335_gpio_config[BS335_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = { |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 141 | /* Port 0, pins 0-7 */ |
| 142 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 143 | U300_FLOATING_INPUT, |
| 144 | U300_OUTPUT_HIGH, |
| 145 | U300_FLOATING_INPUT, |
| 146 | U300_OUTPUT_LOW, |
| 147 | U300_OUTPUT_LOW, |
| 148 | U300_OUTPUT_LOW, |
| 149 | U300_OUTPUT_LOW, |
| 150 | U300_OUTPUT_LOW, |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 151 | }, |
| 152 | /* Port 1, pins 0-7 */ |
| 153 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 154 | U300_OUTPUT_LOW, |
| 155 | U300_OUTPUT_LOW, |
| 156 | U300_OUTPUT_LOW, |
| 157 | U300_PULL_UP_INPUT, |
| 158 | U300_FLOATING_INPUT, |
| 159 | U300_OUTPUT_HIGH, |
| 160 | U300_OUTPUT_LOW, |
| 161 | U300_OUTPUT_LOW, |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 162 | }, |
| 163 | /* Port 2, pins 0-7 */ |
| 164 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 165 | U300_FLOATING_INPUT, |
| 166 | U300_FLOATING_INPUT, |
| 167 | U300_FLOATING_INPUT, |
| 168 | U300_FLOATING_INPUT, |
| 169 | U300_OUTPUT_LOW, |
| 170 | U300_PULL_UP_INPUT, |
| 171 | U300_OUTPUT_LOW, |
| 172 | U300_PULL_UP_INPUT, |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 173 | }, |
| 174 | /* Port 3, pins 0-7 */ |
| 175 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 176 | U300_PULL_UP_INPUT, |
| 177 | U300_OUTPUT_LOW, |
| 178 | U300_FLOATING_INPUT, |
| 179 | U300_FLOATING_INPUT, |
| 180 | U300_FLOATING_INPUT, |
| 181 | U300_FLOATING_INPUT, |
| 182 | U300_FLOATING_INPUT, |
| 183 | U300_FLOATING_INPUT, |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 184 | }, |
| 185 | /* Port 4, pins 0-7 */ |
| 186 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 187 | U300_FLOATING_INPUT, |
| 188 | U300_FLOATING_INPUT, |
| 189 | U300_FLOATING_INPUT, |
| 190 | U300_FLOATING_INPUT, |
| 191 | U300_FLOATING_INPUT, |
| 192 | U300_FLOATING_INPUT, |
| 193 | U300_FLOATING_INPUT, |
| 194 | U300_FLOATING_INPUT, |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 195 | }, |
| 196 | /* Port 5, pins 0-7 */ |
| 197 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 198 | U300_FLOATING_INPUT, |
| 199 | U300_FLOATING_INPUT, |
| 200 | U300_FLOATING_INPUT, |
| 201 | U300_FLOATING_INPUT, |
| 202 | U300_FLOATING_INPUT, |
| 203 | U300_FLOATING_INPUT, |
| 204 | U300_FLOATING_INPUT, |
| 205 | U300_FLOATING_INPUT, |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 206 | }, |
| 207 | /* Port 6, pind 0-7 */ |
| 208 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 209 | U300_FLOATING_INPUT, |
| 210 | U300_FLOATING_INPUT, |
| 211 | U300_FLOATING_INPUT, |
| 212 | U300_FLOATING_INPUT, |
| 213 | U300_FLOATING_INPUT, |
| 214 | U300_FLOATING_INPUT, |
| 215 | U300_FLOATING_INPUT, |
| 216 | U300_FLOATING_INPUT, |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 217 | } |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 218 | }; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 219 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 220 | /** |
| 221 | * to_u300_gpio() - get the pointer to u300_gpio |
| 222 | * @chip: the gpio chip member of the structure u300_gpio |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 223 | */ |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 224 | static inline struct u300_gpio *to_u300_gpio(struct gpio_chip *chip) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 225 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 226 | return container_of(chip, struct u300_gpio, chip); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 227 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 228 | |
Linus Walleij | b4e3ac7 | 2011-11-16 10:24:39 +0100 | [diff] [blame] | 229 | static int u300_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 230 | { |
| 231 | /* |
| 232 | * Map back to global GPIO space and request muxing, the direction |
| 233 | * parameter does not matter for this controller. |
| 234 | */ |
| 235 | int gpio = chip->base + offset; |
| 236 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 237 | return pinctrl_request_gpio(gpio); |
Linus Walleij | b4e3ac7 | 2011-11-16 10:24:39 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void u300_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 241 | { |
| 242 | int gpio = chip->base + offset; |
| 243 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 244 | pinctrl_free_gpio(gpio); |
Linus Walleij | b4e3ac7 | 2011-11-16 10:24:39 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 247 | static int u300_gpio_get(struct gpio_chip *chip, unsigned offset) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 248 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 249 | struct u300_gpio *gpio = to_u300_gpio(chip); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 250 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 251 | return readl(U300_PIN_REG(offset, dir)) & U300_PIN_BIT(offset); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 252 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 253 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 254 | static void u300_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
Linus Walleij | ee17962 | 2009-09-28 12:36:18 +0100 | [diff] [blame] | 255 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 256 | struct u300_gpio *gpio = to_u300_gpio(chip); |
| 257 | unsigned long flags; |
| 258 | u32 val; |
Linus Walleij | ee17962 | 2009-09-28 12:36:18 +0100 | [diff] [blame] | 259 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 260 | local_irq_save(flags); |
| 261 | |
| 262 | val = readl(U300_PIN_REG(offset, dor)); |
| 263 | if (value) |
| 264 | writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, dor)); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 265 | else |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 266 | writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, dor)); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 267 | |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 268 | local_irq_restore(flags); |
| 269 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 270 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 271 | static int u300_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 272 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 273 | struct u300_gpio *gpio = to_u300_gpio(chip); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 274 | unsigned long flags; |
| 275 | u32 val; |
| 276 | |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 277 | local_irq_save(flags); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 278 | val = readl(U300_PIN_REG(offset, pcr)); |
| 279 | /* Mask out this pin, note 2 bits per setting */ |
| 280 | val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1)); |
| 281 | writel(val, U300_PIN_REG(offset, pcr)); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 282 | local_irq_restore(flags); |
| 283 | return 0; |
| 284 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 285 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 286 | static int u300_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 287 | int value) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 288 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 289 | struct u300_gpio *gpio = to_u300_gpio(chip); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 290 | unsigned long flags; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 291 | u32 oldmode; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 292 | u32 val; |
| 293 | |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 294 | local_irq_save(flags); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 295 | val = readl(U300_PIN_REG(offset, pcr)); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 296 | /* |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 297 | * Drive mode must be set by the special mode set function, set |
| 298 | * push/pull mode by default if no mode has been selected. |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 299 | */ |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 300 | oldmode = val & (U300_GPIO_PXPCR_PIN_MODE_MASK << |
| 301 | ((offset & 0x07) << 1)); |
| 302 | /* mode = 0 means input, else some mode is already set */ |
| 303 | if (oldmode == 0) { |
| 304 | val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << |
| 305 | ((offset & 0x07) << 1)); |
| 306 | val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL |
| 307 | << ((offset & 0x07) << 1)); |
| 308 | writel(val, U300_PIN_REG(offset, pcr)); |
| 309 | } |
| 310 | u300_gpio_set(chip, offset, value); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 311 | local_irq_restore(flags); |
| 312 | return 0; |
| 313 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 314 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 315 | static int u300_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 316 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 317 | struct u300_gpio *gpio = to_u300_gpio(chip); |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 318 | int portno = offset >> 3; |
| 319 | struct u300_gpio_port *port = NULL; |
| 320 | struct list_head *p; |
| 321 | int retirq; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 322 | |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 323 | list_for_each(p, &gpio->port_list) { |
| 324 | port = list_entry(p, struct u300_gpio_port, node); |
| 325 | if (port->number == portno) |
| 326 | break; |
| 327 | } |
| 328 | if (port == NULL) { |
| 329 | dev_err(gpio->dev, "could not locate port for GPIO %d IRQ\n", |
| 330 | offset); |
| 331 | return -EINVAL; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * The local hwirqs on the port are the lower three bits, there |
| 336 | * are exactly 8 IRQs per port since they are 8-bit |
| 337 | */ |
| 338 | retirq = irq_find_mapping(port->domain, (offset & 0x7)); |
| 339 | |
| 340 | dev_dbg(gpio->dev, "request IRQ for GPIO %d, return %d from port %d\n", |
| 341 | offset, retirq, port->number); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 342 | return retirq; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 343 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 344 | |
Linus Walleij | dc0b1aa | 2011-11-16 21:58:10 +0100 | [diff] [blame] | 345 | /* Returning -EINVAL means "supported but not available" */ |
| 346 | int u300_gpio_config_get(struct gpio_chip *chip, |
| 347 | unsigned offset, |
| 348 | unsigned long *config) |
| 349 | { |
| 350 | struct u300_gpio *gpio = to_u300_gpio(chip); |
| 351 | enum pin_config_param param = (enum pin_config_param) *config; |
| 352 | bool biasmode; |
| 353 | u32 drmode; |
| 354 | |
| 355 | /* One bit per pin, clamp to bool range */ |
| 356 | biasmode = !!(readl(U300_PIN_REG(offset, per)) & U300_PIN_BIT(offset)); |
| 357 | |
| 358 | /* Mask out the two bits for this pin and shift to bits 0,1 */ |
| 359 | drmode = readl(U300_PIN_REG(offset, pcr)); |
| 360 | drmode &= (U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1)); |
| 361 | drmode >>= ((offset & 0x07) << 1); |
| 362 | |
| 363 | switch(param) { |
| 364 | case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: |
| 365 | *config = 0; |
| 366 | if (biasmode) |
| 367 | return 0; |
| 368 | else |
| 369 | return -EINVAL; |
| 370 | break; |
| 371 | case PIN_CONFIG_BIAS_PULL_UP: |
| 372 | *config = 0; |
| 373 | if (!biasmode) |
| 374 | return 0; |
| 375 | else |
| 376 | return -EINVAL; |
| 377 | break; |
| 378 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
| 379 | *config = 0; |
| 380 | if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL) |
| 381 | return 0; |
| 382 | else |
| 383 | return -EINVAL; |
| 384 | break; |
| 385 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: |
| 386 | *config = 0; |
| 387 | if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN) |
| 388 | return 0; |
| 389 | else |
| 390 | return -EINVAL; |
| 391 | break; |
| 392 | case PIN_CONFIG_DRIVE_OPEN_SOURCE: |
| 393 | *config = 0; |
| 394 | if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE) |
| 395 | return 0; |
| 396 | else |
| 397 | return -EINVAL; |
| 398 | break; |
| 399 | default: |
| 400 | break; |
| 401 | } |
| 402 | return -ENOTSUPP; |
| 403 | } |
| 404 | |
| 405 | int u300_gpio_config_set(struct gpio_chip *chip, unsigned offset, |
| 406 | enum pin_config_param param) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 407 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 408 | struct u300_gpio *gpio = to_u300_gpio(chip); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 409 | unsigned long flags; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 410 | u32 val; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 411 | |
| 412 | local_irq_save(flags); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 413 | switch (param) { |
Linus Walleij | a050b3e | 2011-11-16 20:10:09 +0100 | [diff] [blame] | 414 | case PIN_CONFIG_BIAS_DISABLE: |
| 415 | case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 416 | val = readl(U300_PIN_REG(offset, per)); |
| 417 | writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, per)); |
| 418 | break; |
Linus Walleij | a050b3e | 2011-11-16 20:10:09 +0100 | [diff] [blame] | 419 | case PIN_CONFIG_BIAS_PULL_UP: |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 420 | val = readl(U300_PIN_REG(offset, per)); |
| 421 | writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, per)); |
| 422 | break; |
Linus Walleij | a050b3e | 2011-11-16 20:10:09 +0100 | [diff] [blame] | 423 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 424 | val = readl(U300_PIN_REG(offset, pcr)); |
| 425 | val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK |
| 426 | << ((offset & 0x07) << 1)); |
| 427 | val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL |
| 428 | << ((offset & 0x07) << 1)); |
| 429 | writel(val, U300_PIN_REG(offset, pcr)); |
| 430 | break; |
Linus Walleij | a050b3e | 2011-11-16 20:10:09 +0100 | [diff] [blame] | 431 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 432 | val = readl(U300_PIN_REG(offset, pcr)); |
| 433 | val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK |
| 434 | << ((offset & 0x07) << 1)); |
| 435 | val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN |
| 436 | << ((offset & 0x07) << 1)); |
| 437 | writel(val, U300_PIN_REG(offset, pcr)); |
| 438 | break; |
Linus Walleij | a050b3e | 2011-11-16 20:10:09 +0100 | [diff] [blame] | 439 | case PIN_CONFIG_DRIVE_OPEN_SOURCE: |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 440 | val = readl(U300_PIN_REG(offset, pcr)); |
| 441 | val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK |
| 442 | << ((offset & 0x07) << 1)); |
| 443 | val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE |
| 444 | << ((offset & 0x07) << 1)); |
| 445 | writel(val, U300_PIN_REG(offset, pcr)); |
| 446 | break; |
| 447 | default: |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 448 | local_irq_restore(flags); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 449 | dev_err(gpio->dev, "illegal configuration requested\n"); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | local_irq_restore(flags); |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static struct gpio_chip u300_gpio_chip = { |
| 457 | .label = "u300-gpio-chip", |
| 458 | .owner = THIS_MODULE, |
Linus Walleij | b4e3ac7 | 2011-11-16 10:24:39 +0100 | [diff] [blame] | 459 | .request = u300_gpio_request, |
| 460 | .free = u300_gpio_free, |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 461 | .get = u300_gpio_get, |
| 462 | .set = u300_gpio_set, |
| 463 | .direction_input = u300_gpio_direction_input, |
| 464 | .direction_output = u300_gpio_direction_output, |
| 465 | .to_irq = u300_gpio_to_irq, |
| 466 | }; |
| 467 | |
| 468 | static void u300_toggle_trigger(struct u300_gpio *gpio, unsigned offset) |
| 469 | { |
| 470 | u32 val; |
| 471 | |
| 472 | val = readl(U300_PIN_REG(offset, icr)); |
| 473 | /* Set mode depending on state */ |
| 474 | if (u300_gpio_get(&gpio->chip, offset)) { |
| 475 | /* High now, let's trigger on falling edge next then */ |
| 476 | writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr)); |
| 477 | dev_dbg(gpio->dev, "next IRQ on falling edge on pin %d\n", |
| 478 | offset); |
| 479 | } else { |
| 480 | /* Low now, let's trigger on rising edge next then */ |
| 481 | writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr)); |
| 482 | dev_dbg(gpio->dev, "next IRQ on rising edge on pin %d\n", |
| 483 | offset); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | static int u300_gpio_irq_type(struct irq_data *d, unsigned trigger) |
| 488 | { |
| 489 | struct u300_gpio_port *port = irq_data_get_irq_chip_data(d); |
| 490 | struct u300_gpio *gpio = port->gpio; |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 491 | int offset = (port->number << 3) + d->hwirq; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 492 | u32 val; |
| 493 | |
| 494 | if ((trigger & IRQF_TRIGGER_RISING) && |
| 495 | (trigger & IRQF_TRIGGER_FALLING)) { |
| 496 | /* |
| 497 | * The GPIO block can only trigger on falling OR rising edges, |
| 498 | * not both. So we need to toggle the mode whenever the pin |
| 499 | * goes from one state to the other with a special state flag |
| 500 | */ |
| 501 | dev_dbg(gpio->dev, |
| 502 | "trigger on both rising and falling edge on pin %d\n", |
| 503 | offset); |
| 504 | port->toggle_edge_mode |= U300_PIN_BIT(offset); |
| 505 | u300_toggle_trigger(gpio, offset); |
| 506 | } else if (trigger & IRQF_TRIGGER_RISING) { |
| 507 | dev_dbg(gpio->dev, "trigger on rising edge on pin %d\n", |
| 508 | offset); |
| 509 | val = readl(U300_PIN_REG(offset, icr)); |
| 510 | writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr)); |
| 511 | port->toggle_edge_mode &= ~U300_PIN_BIT(offset); |
| 512 | } else if (trigger & IRQF_TRIGGER_FALLING) { |
| 513 | dev_dbg(gpio->dev, "trigger on falling edge on pin %d\n", |
| 514 | offset); |
| 515 | val = readl(U300_PIN_REG(offset, icr)); |
| 516 | writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr)); |
| 517 | port->toggle_edge_mode &= ~U300_PIN_BIT(offset); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 520 | return 0; |
| 521 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 522 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 523 | static void u300_gpio_irq_enable(struct irq_data *d) |
| 524 | { |
| 525 | struct u300_gpio_port *port = irq_data_get_irq_chip_data(d); |
| 526 | struct u300_gpio *gpio = port->gpio; |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 527 | int offset = (port->number << 3) + d->hwirq; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 528 | u32 val; |
| 529 | unsigned long flags; |
| 530 | |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 531 | dev_dbg(gpio->dev, "enable IRQ for hwirq %lu on port %s, offset %d\n", |
| 532 | d->hwirq, port->name, offset); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 533 | local_irq_save(flags); |
| 534 | val = readl(U300_PIN_REG(offset, ien)); |
| 535 | writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien)); |
| 536 | local_irq_restore(flags); |
| 537 | } |
| 538 | |
| 539 | static void u300_gpio_irq_disable(struct irq_data *d) |
| 540 | { |
| 541 | struct u300_gpio_port *port = irq_data_get_irq_chip_data(d); |
| 542 | struct u300_gpio *gpio = port->gpio; |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 543 | int offset = (port->number << 3) + d->hwirq; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 544 | u32 val; |
| 545 | unsigned long flags; |
| 546 | |
| 547 | local_irq_save(flags); |
| 548 | val = readl(U300_PIN_REG(offset, ien)); |
| 549 | writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien)); |
| 550 | local_irq_restore(flags); |
| 551 | } |
| 552 | |
| 553 | static struct irq_chip u300_gpio_irqchip = { |
| 554 | .name = "u300-gpio-irqchip", |
| 555 | .irq_enable = u300_gpio_irq_enable, |
| 556 | .irq_disable = u300_gpio_irq_disable, |
| 557 | .irq_set_type = u300_gpio_irq_type, |
| 558 | |
| 559 | }; |
| 560 | |
| 561 | static void u300_gpio_irq_handler(unsigned irq, struct irq_desc *desc) |
| 562 | { |
| 563 | struct u300_gpio_port *port = irq_get_handler_data(irq); |
| 564 | struct u300_gpio *gpio = port->gpio; |
| 565 | int pinoffset = port->number << 3; /* get the right stride */ |
| 566 | unsigned long val; |
| 567 | |
| 568 | desc->irq_data.chip->irq_ack(&desc->irq_data); |
| 569 | /* Read event register */ |
| 570 | val = readl(U300_PIN_REG(pinoffset, iev)); |
| 571 | /* Mask relevant bits */ |
| 572 | val &= 0xFFU; /* 8 bits per port */ |
| 573 | /* ACK IRQ (clear event) */ |
| 574 | writel(val, U300_PIN_REG(pinoffset, iev)); |
| 575 | |
| 576 | /* Call IRQ handler */ |
| 577 | if (val != 0) { |
| 578 | int irqoffset; |
| 579 | |
| 580 | for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) { |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 581 | int pin_irq = irq_find_mapping(port->domain, irqoffset); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 582 | int offset = pinoffset + irqoffset; |
| 583 | |
| 584 | dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n", |
| 585 | pin_irq, offset); |
| 586 | generic_handle_irq(pin_irq); |
| 587 | /* |
| 588 | * Triggering IRQ on both rising and falling edge |
| 589 | * needs mockery |
| 590 | */ |
| 591 | if (port->toggle_edge_mode & U300_PIN_BIT(offset)) |
| 592 | u300_toggle_trigger(gpio, offset); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 596 | desc->irq_data.chip->irq_unmask(&desc->irq_data); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 599 | static void __init u300_gpio_init_pin(struct u300_gpio *gpio, |
| 600 | int offset, |
| 601 | const struct u300_gpio_confdata *conf) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 602 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 603 | /* Set mode: input or output */ |
| 604 | if (conf->output) { |
| 605 | u300_gpio_direction_output(&gpio->chip, offset, conf->outval); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 606 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 607 | /* Deactivate bias mode for output */ |
Linus Walleij | dc0b1aa | 2011-11-16 21:58:10 +0100 | [diff] [blame] | 608 | u300_gpio_config_set(&gpio->chip, offset, |
| 609 | PIN_CONFIG_BIAS_HIGH_IMPEDANCE); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 610 | |
| 611 | /* Set drive mode for output */ |
Linus Walleij | dc0b1aa | 2011-11-16 21:58:10 +0100 | [diff] [blame] | 612 | u300_gpio_config_set(&gpio->chip, offset, |
| 613 | PIN_CONFIG_DRIVE_PUSH_PULL); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 614 | |
| 615 | dev_dbg(gpio->dev, "set up pin %d as output, value: %d\n", |
| 616 | offset, conf->outval); |
| 617 | } else { |
| 618 | u300_gpio_direction_input(&gpio->chip, offset); |
| 619 | |
| 620 | /* Always set output low on input pins */ |
| 621 | u300_gpio_set(&gpio->chip, offset, 0); |
| 622 | |
| 623 | /* Set bias mode for input */ |
Linus Walleij | dc0b1aa | 2011-11-16 21:58:10 +0100 | [diff] [blame] | 624 | u300_gpio_config_set(&gpio->chip, offset, conf->bias_mode); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 625 | |
| 626 | dev_dbg(gpio->dev, "set up pin %d as input, bias: %04x\n", |
| 627 | offset, conf->bias_mode); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | static void __init u300_gpio_init_coh901571(struct u300_gpio *gpio, |
| 632 | struct u300_gpio_platform *plat) |
| 633 | { |
| 634 | int i, j; |
| 635 | |
| 636 | /* Write default config and values to all pins */ |
| 637 | for (i = 0; i < plat->ports; i++) { |
| 638 | for (j = 0; j < 8; j++) { |
| 639 | const struct u300_gpio_confdata *conf; |
| 640 | int offset = (i*8) + j; |
| 641 | |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 642 | conf = &bs335_gpio_config[i][j]; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 643 | u300_gpio_init_pin(gpio, offset, conf); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | static inline void u300_gpio_free_ports(struct u300_gpio *gpio) |
| 649 | { |
| 650 | struct u300_gpio_port *port; |
| 651 | struct list_head *p, *n; |
| 652 | |
| 653 | list_for_each_safe(p, n, &gpio->port_list) { |
| 654 | port = list_entry(p, struct u300_gpio_port, node); |
| 655 | list_del(&port->node); |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 656 | if (port->domain) |
| 657 | irq_domain_remove(port->domain); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 658 | kfree(port); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | static int __init u300_gpio_probe(struct platform_device *pdev) |
| 663 | { |
| 664 | struct u300_gpio_platform *plat = dev_get_platdata(&pdev->dev); |
| 665 | struct u300_gpio *gpio; |
| 666 | int err = 0; |
| 667 | int portno; |
| 668 | u32 val; |
| 669 | u32 ifr; |
| 670 | int i; |
| 671 | |
| 672 | gpio = kzalloc(sizeof(struct u300_gpio), GFP_KERNEL); |
| 673 | if (gpio == NULL) { |
| 674 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
| 675 | return -ENOMEM; |
| 676 | } |
| 677 | |
| 678 | gpio->chip = u300_gpio_chip; |
| 679 | gpio->chip.ngpio = plat->ports * U300_GPIO_PINS_PER_PORT; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 680 | gpio->chip.dev = &pdev->dev; |
| 681 | gpio->chip.base = plat->gpio_base; |
| 682 | gpio->dev = &pdev->dev; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 683 | |
| 684 | /* Get GPIO clock */ |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 685 | gpio->clk = clk_get(gpio->dev, NULL); |
| 686 | if (IS_ERR(gpio->clk)) { |
| 687 | err = PTR_ERR(gpio->clk); |
| 688 | dev_err(gpio->dev, "could not get GPIO clock\n"); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 689 | goto err_no_clk; |
| 690 | } |
Linus Walleij | 27e8461 | 2012-06-19 23:36:15 +0200 | [diff] [blame] | 691 | err = clk_prepare_enable(gpio->clk); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 692 | if (err) { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 693 | dev_err(gpio->dev, "could not enable GPIO clock\n"); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 694 | goto err_no_clk_enable; |
| 695 | } |
| 696 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 697 | gpio->memres = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 698 | if (!gpio->memres) { |
| 699 | dev_err(gpio->dev, "could not get GPIO memory resource\n"); |
| 700 | err = -ENODEV; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 701 | goto err_no_resource; |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 702 | } |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 703 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 704 | if (!request_mem_region(gpio->memres->start, |
| 705 | resource_size(gpio->memres), |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 706 | "GPIO Controller")) { |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 707 | err = -ENODEV; |
| 708 | goto err_no_ioregion; |
| 709 | } |
| 710 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 711 | gpio->base = ioremap(gpio->memres->start, resource_size(gpio->memres)); |
| 712 | if (!gpio->base) { |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 713 | err = -ENOMEM; |
| 714 | goto err_no_ioremap; |
| 715 | } |
| 716 | |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 717 | dev_info(gpio->dev, |
| 718 | "initializing GPIO Controller COH 901 571/3\n"); |
| 719 | gpio->stride = U300_GPIO_PORT_STRIDE; |
| 720 | gpio->pcr = U300_GPIO_PXPCR; |
| 721 | gpio->dor = U300_GPIO_PXPDOR; |
| 722 | gpio->dir = U300_GPIO_PXPDIR; |
| 723 | gpio->per = U300_GPIO_PXPER; |
| 724 | gpio->icr = U300_GPIO_PXICR; |
| 725 | gpio->ien = U300_GPIO_PXIEN; |
| 726 | gpio->iev = U300_GPIO_PXIEV; |
| 727 | ifr = U300_GPIO_PXIFR; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 728 | |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 729 | val = readl(gpio->base + U300_GPIO_CR); |
| 730 | dev_info(gpio->dev, "COH901571/3 block version: %d, " \ |
| 731 | "number of cores: %d totalling %d pins\n", |
| 732 | ((val & 0x000001FC) >> 2), |
| 733 | ((val & 0x0000FE00) >> 9), |
| 734 | ((val & 0x0000FE00) >> 9) * 8); |
| 735 | writel(U300_GPIO_CR_BLOCK_CLKRQ_ENABLE, |
| 736 | gpio->base + U300_GPIO_CR); |
| 737 | u300_gpio_init_coh901571(gpio, plat); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 738 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 739 | /* Add each port with its IRQ separately */ |
| 740 | INIT_LIST_HEAD(&gpio->port_list); |
| 741 | for (portno = 0 ; portno < plat->ports; portno++) { |
| 742 | struct u300_gpio_port *port = |
| 743 | kmalloc(sizeof(struct u300_gpio_port), GFP_KERNEL); |
| 744 | |
| 745 | if (!port) { |
| 746 | dev_err(gpio->dev, "out of memory\n"); |
| 747 | err = -ENOMEM; |
| 748 | goto err_no_port; |
| 749 | } |
| 750 | |
| 751 | snprintf(port->name, 8, "gpio%d", portno); |
| 752 | port->number = portno; |
| 753 | port->gpio = gpio; |
| 754 | |
| 755 | port->irq = platform_get_irq_byname(pdev, |
| 756 | port->name); |
| 757 | |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 758 | dev_dbg(gpio->dev, "register IRQ %d for port %s\n", port->irq, |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 759 | port->name); |
| 760 | |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 761 | port->domain = irq_domain_add_linear(pdev->dev.of_node, |
| 762 | U300_GPIO_PINS_PER_PORT, |
| 763 | &irq_domain_simple_ops, |
| 764 | port); |
| 765 | if (!port->domain) |
| 766 | goto err_no_domain; |
| 767 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 768 | irq_set_chained_handler(port->irq, u300_gpio_irq_handler); |
| 769 | irq_set_handler_data(port->irq, port); |
| 770 | |
| 771 | /* For each GPIO pin set the unique IRQ handler */ |
| 772 | for (i = 0; i < U300_GPIO_PINS_PER_PORT; i++) { |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 773 | int irqno = irq_create_mapping(port->domain, i); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 774 | |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 775 | dev_dbg(gpio->dev, "GPIO%d on port %s gets IRQ %d\n", |
| 776 | gpio->chip.base + (port->number << 3) + i, |
| 777 | port->name, irqno); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 778 | irq_set_chip_and_handler(irqno, &u300_gpio_irqchip, |
| 779 | handle_simple_irq); |
| 780 | set_irq_flags(irqno, IRQF_VALID); |
| 781 | irq_set_chip_data(irqno, port); |
| 782 | } |
| 783 | |
| 784 | /* Turns off irq force (test register) for this port */ |
| 785 | writel(0x0, gpio->base + portno * gpio->stride + ifr); |
| 786 | |
| 787 | list_add_tail(&port->node, &gpio->port_list); |
| 788 | } |
| 789 | dev_dbg(gpio->dev, "initialized %d GPIO ports\n", portno); |
| 790 | |
| 791 | err = gpiochip_add(&gpio->chip); |
| 792 | if (err) { |
| 793 | dev_err(gpio->dev, "unable to add gpiochip: %d\n", err); |
| 794 | goto err_no_chip; |
| 795 | } |
| 796 | |
Linus Walleij | 128a06d | 2012-02-21 14:31:45 +0100 | [diff] [blame] | 797 | /* Spawn pin controller device as child of the GPIO, pass gpio chip */ |
| 798 | plat->pinctrl_device->dev.platform_data = &gpio->chip; |
| 799 | err = platform_device_register(plat->pinctrl_device); |
| 800 | if (err) |
| 801 | goto err_no_pinctrl; |
| 802 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 803 | platform_set_drvdata(pdev, gpio); |
| 804 | |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 805 | return 0; |
| 806 | |
Linus Walleij | 128a06d | 2012-02-21 14:31:45 +0100 | [diff] [blame] | 807 | err_no_pinctrl: |
| 808 | err = gpiochip_remove(&gpio->chip); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 809 | err_no_chip: |
Linus Walleij | a6c45b9 | 2012-10-17 18:31:20 +0200 | [diff] [blame^] | 810 | err_no_domain: |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 811 | err_no_port: |
| 812 | u300_gpio_free_ports(gpio); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 813 | iounmap(gpio->base); |
| 814 | err_no_ioremap: |
| 815 | release_mem_region(gpio->memres->start, resource_size(gpio->memres)); |
| 816 | err_no_ioregion: |
| 817 | err_no_resource: |
Linus Walleij | 27e8461 | 2012-06-19 23:36:15 +0200 | [diff] [blame] | 818 | clk_disable_unprepare(gpio->clk); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 819 | err_no_clk_enable: |
| 820 | clk_put(gpio->clk); |
| 821 | err_no_clk: |
| 822 | kfree(gpio); |
| 823 | dev_info(&pdev->dev, "module ERROR:%d\n", err); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 824 | return err; |
| 825 | } |
| 826 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 827 | static int __exit u300_gpio_remove(struct platform_device *pdev) |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 828 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 829 | struct u300_gpio *gpio = platform_get_drvdata(pdev); |
| 830 | int err; |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 831 | |
| 832 | /* Turn off the GPIO block */ |
Linus Walleij | 04b13de | 2012-08-13 10:36:55 +0200 | [diff] [blame] | 833 | writel(0x00000000U, gpio->base + U300_GPIO_CR); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 834 | |
| 835 | err = gpiochip_remove(&gpio->chip); |
| 836 | if (err < 0) { |
| 837 | dev_err(gpio->dev, "unable to remove gpiochip: %d\n", err); |
| 838 | return err; |
| 839 | } |
| 840 | u300_gpio_free_ports(gpio); |
| 841 | iounmap(gpio->base); |
| 842 | release_mem_region(gpio->memres->start, |
| 843 | resource_size(gpio->memres)); |
Linus Walleij | 27e8461 | 2012-06-19 23:36:15 +0200 | [diff] [blame] | 844 | clk_disable_unprepare(gpio->clk); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 845 | clk_put(gpio->clk); |
| 846 | platform_set_drvdata(pdev, NULL); |
| 847 | kfree(gpio); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 851 | static struct platform_driver u300_gpio_driver = { |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 852 | .driver = { |
| 853 | .name = "u300-gpio", |
| 854 | }, |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 855 | .remove = __exit_p(u300_gpio_remove), |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 856 | }; |
| 857 | |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 858 | static int __init u300_gpio_init(void) |
| 859 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 860 | return platform_driver_probe(&u300_gpio_driver, u300_gpio_probe); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static void __exit u300_gpio_exit(void) |
| 864 | { |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 865 | platform_driver_unregister(&u300_gpio_driver); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | arch_initcall(u300_gpio_init); |
| 869 | module_exit(u300_gpio_exit); |
| 870 | |
| 871 | MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>"); |
Linus Walleij | cc890cd | 2011-09-08 09:04:51 +0100 | [diff] [blame] | 872 | MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335/COH 901 571/3 GPIO driver"); |
Linus Walleij | bd41b99 | 2009-04-23 21:15:04 +0100 | [diff] [blame] | 873 | MODULE_LICENSE("GPL"); |