Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Pinmuxed GPIO support for SuperH. |
| 3 | * |
| 4 | * Copyright (C) 2008 Magnus Damm |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | */ |
Paul Mundt | b72421d | 2010-10-04 03:54:56 +0900 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/irq.h> |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/gpio.h> |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/ioport.h> |
| 24 | |
| 25 | static void pfc_iounmap(struct pinmux_info *pip) |
| 26 | { |
| 27 | int k; |
| 28 | |
| 29 | for (k = 0; k < pip->num_resources; k++) |
| 30 | if (pip->window[k].virt) |
| 31 | iounmap(pip->window[k].virt); |
| 32 | |
| 33 | kfree(pip->window); |
| 34 | pip->window = NULL; |
| 35 | } |
| 36 | |
| 37 | static int pfc_ioremap(struct pinmux_info *pip) |
| 38 | { |
| 39 | struct resource *res; |
| 40 | int k; |
| 41 | |
| 42 | if (!pip->num_resources) |
| 43 | return 0; |
| 44 | |
| 45 | pip->window = kzalloc(pip->num_resources * sizeof(*pip->window), |
| 46 | GFP_NOWAIT); |
| 47 | if (!pip->window) |
| 48 | goto err1; |
| 49 | |
| 50 | for (k = 0; k < pip->num_resources; k++) { |
| 51 | res = pip->resource + k; |
| 52 | WARN_ON(resource_type(res) != IORESOURCE_MEM); |
| 53 | pip->window[k].phys = res->start; |
| 54 | pip->window[k].size = resource_size(res); |
| 55 | pip->window[k].virt = ioremap_nocache(res->start, |
| 56 | resource_size(res)); |
| 57 | if (!pip->window[k].virt) |
| 58 | goto err2; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | |
| 63 | err2: |
| 64 | pfc_iounmap(pip); |
| 65 | err1: |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | static void __iomem *pfc_phys_to_virt(struct pinmux_info *pip, |
| 70 | unsigned long address) |
| 71 | { |
| 72 | struct pfc_window *window; |
| 73 | int k; |
| 74 | |
| 75 | /* scan through physical windows and convert address */ |
| 76 | for (k = 0; k < pip->num_resources; k++) { |
| 77 | window = pip->window + k; |
| 78 | |
| 79 | if (address < window->phys) |
| 80 | continue; |
| 81 | |
| 82 | if (address >= (window->phys + window->size)) |
| 83 | continue; |
| 84 | |
| 85 | return window->virt + (address - window->phys); |
| 86 | } |
| 87 | |
| 88 | /* no windows defined, register must be 1:1 mapped virt:phys */ |
| 89 | return (void __iomem *)address; |
| 90 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 91 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 92 | static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r) |
| 93 | { |
| 94 | if (enum_id < r->begin) |
| 95 | return 0; |
| 96 | |
| 97 | if (enum_id > r->end) |
| 98 | return 0; |
| 99 | |
| 100 | return 1; |
| 101 | } |
| 102 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 103 | static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg, |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 104 | unsigned long reg_width) |
| 105 | { |
| 106 | switch (reg_width) { |
| 107 | case 8: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 108 | return ioread8(mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 109 | case 16: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 110 | return ioread16(mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 111 | case 32: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 112 | return ioread32(mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | BUG(); |
| 116 | return 0; |
| 117 | } |
| 118 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 119 | static void gpio_write_raw_reg(void __iomem *mapped_reg, |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 120 | unsigned long reg_width, |
| 121 | unsigned long data) |
| 122 | { |
| 123 | switch (reg_width) { |
| 124 | case 8: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 125 | iowrite8(data, mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 126 | return; |
| 127 | case 16: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 128 | iowrite16(data, mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 129 | return; |
| 130 | case 32: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 131 | iowrite32(data, mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
| 135 | BUG(); |
| 136 | } |
| 137 | |
Magnus Damm | 92554d9 | 2011-12-14 01:00:37 +0900 | [diff] [blame] | 138 | static int gpio_read_bit(struct pinmux_data_reg *dr, |
| 139 | unsigned long in_pos) |
| 140 | { |
| 141 | unsigned long pos; |
| 142 | |
| 143 | pos = dr->reg_width - (in_pos + 1); |
| 144 | |
| 145 | pr_debug("read_bit: addr = %lx, pos = %ld, " |
| 146 | "r_width = %ld\n", dr->reg, pos, dr->reg_width); |
| 147 | |
| 148 | return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1; |
| 149 | } |
| 150 | |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 151 | static void gpio_write_bit(struct pinmux_data_reg *dr, |
| 152 | unsigned long in_pos, unsigned long value) |
| 153 | { |
| 154 | unsigned long pos; |
| 155 | |
| 156 | pos = dr->reg_width - (in_pos + 1); |
| 157 | |
Paul Mundt | ca6f2d7 | 2009-12-09 15:51:27 +0900 | [diff] [blame] | 158 | pr_debug("write_bit addr = %lx, value = %d, pos = %ld, " |
Paul Mundt | fd2cb0c | 2009-11-30 12:15:04 +0900 | [diff] [blame] | 159 | "r_width = %ld\n", |
| 160 | dr->reg, !!value, pos, dr->reg_width); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 161 | |
| 162 | if (value) |
| 163 | set_bit(pos, &dr->reg_shadow); |
| 164 | else |
| 165 | clear_bit(pos, &dr->reg_shadow); |
| 166 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 167 | gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 168 | } |
| 169 | |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 170 | static void config_reg_helper(struct pinmux_info *gpioc, |
| 171 | struct pinmux_cfg_reg *crp, |
| 172 | unsigned long in_pos, |
| 173 | void __iomem **mapped_regp, |
| 174 | unsigned long *maskp, |
| 175 | unsigned long *posp) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 176 | { |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 177 | *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 178 | |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 179 | *maskp = (1 << crp->field_width) - 1; |
| 180 | *posp = crp->reg_width - ((in_pos + 1) * crp->field_width); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 181 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 182 | |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 183 | static int read_config_reg(struct pinmux_info *gpioc, |
| 184 | struct pinmux_cfg_reg *crp, |
| 185 | unsigned long field) |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 186 | { |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 187 | void __iomem *mapped_reg; |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 188 | unsigned long mask, pos; |
| 189 | |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 190 | config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 191 | |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 192 | pr_debug("read_reg: addr = %lx, field = %ld, " |
Paul Mundt | fd2cb0c | 2009-11-30 12:15:04 +0900 | [diff] [blame] | 193 | "r_width = %ld, f_width = %ld\n", |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 194 | crp->reg, field, crp->reg_width, crp->field_width); |
| 195 | |
| 196 | return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask; |
| 197 | } |
| 198 | |
| 199 | static void write_config_reg(struct pinmux_info *gpioc, |
| 200 | struct pinmux_cfg_reg *crp, |
| 201 | unsigned long field, unsigned long value) |
| 202 | { |
| 203 | void __iomem *mapped_reg; |
| 204 | unsigned long mask, pos; |
| 205 | |
| 206 | config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos); |
| 207 | |
| 208 | pr_debug("write_reg addr = %lx, value = %ld, field = %ld, " |
| 209 | "r_width = %ld, f_width = %ld\n", |
| 210 | crp->reg, value, field, crp->reg_width, crp->field_width); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 211 | |
| 212 | mask = ~(mask << pos); |
| 213 | value = value << pos; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 214 | |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 215 | switch (crp->reg_width) { |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 216 | case 8: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 217 | iowrite8((ioread8(mapped_reg) & mask) | value, mapped_reg); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 218 | break; |
| 219 | case 16: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 220 | iowrite16((ioread16(mapped_reg) & mask) | value, mapped_reg); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 221 | break; |
| 222 | case 32: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 223 | iowrite32((ioread32(mapped_reg) & mask) | value, mapped_reg); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 224 | break; |
| 225 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 226 | } |
| 227 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 228 | static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 229 | { |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 230 | struct pinmux_gpio *gpiop = &gpioc->gpios[gpio]; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 231 | struct pinmux_data_reg *data_reg; |
| 232 | int k, n; |
| 233 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 234 | if (!enum_in_range(gpiop->enum_id, &gpioc->data)) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 235 | return -1; |
| 236 | |
| 237 | k = 0; |
| 238 | while (1) { |
| 239 | data_reg = gpioc->data_regs + k; |
| 240 | |
| 241 | if (!data_reg->reg_width) |
| 242 | break; |
| 243 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 244 | data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg); |
| 245 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 246 | for (n = 0; n < data_reg->reg_width; n++) { |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 247 | if (data_reg->enum_ids[n] == gpiop->enum_id) { |
| 248 | gpiop->flags &= ~PINMUX_FLAG_DREG; |
| 249 | gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT); |
| 250 | gpiop->flags &= ~PINMUX_FLAG_DBIT; |
| 251 | gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 252 | return 0; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | k++; |
| 256 | } |
| 257 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 258 | BUG(); |
| 259 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 260 | return -1; |
| 261 | } |
| 262 | |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 263 | static void setup_data_regs(struct pinmux_info *gpioc) |
| 264 | { |
| 265 | struct pinmux_data_reg *drp; |
| 266 | int k; |
| 267 | |
| 268 | for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++) |
| 269 | setup_data_reg(gpioc, k); |
| 270 | |
| 271 | k = 0; |
| 272 | while (1) { |
| 273 | drp = gpioc->data_regs + k; |
| 274 | |
| 275 | if (!drp->reg_width) |
| 276 | break; |
| 277 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 278 | drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg, |
| 279 | drp->reg_width); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 280 | k++; |
| 281 | } |
| 282 | } |
| 283 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 284 | static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio, |
| 285 | struct pinmux_data_reg **drp, int *bitp) |
| 286 | { |
| 287 | struct pinmux_gpio *gpiop = &gpioc->gpios[gpio]; |
| 288 | int k, n; |
| 289 | |
| 290 | if (!enum_in_range(gpiop->enum_id, &gpioc->data)) |
| 291 | return -1; |
| 292 | |
| 293 | k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT; |
| 294 | n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT; |
| 295 | *drp = gpioc->data_regs + k; |
| 296 | *bitp = n; |
| 297 | return 0; |
| 298 | } |
| 299 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 300 | static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id, |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 301 | struct pinmux_cfg_reg **crp, |
| 302 | int *fieldp, int *valuep, |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 303 | unsigned long **cntp) |
| 304 | { |
| 305 | struct pinmux_cfg_reg *config_reg; |
| 306 | unsigned long r_width, f_width; |
| 307 | int k, n; |
| 308 | |
| 309 | k = 0; |
| 310 | while (1) { |
| 311 | config_reg = gpioc->cfg_regs + k; |
| 312 | |
| 313 | r_width = config_reg->reg_width; |
| 314 | f_width = config_reg->field_width; |
| 315 | |
| 316 | if (!r_width) |
| 317 | break; |
Magnus Damm | c63bcc6 | 2011-10-17 18:01:19 +0900 | [diff] [blame] | 318 | for (n = 0; n < (r_width / f_width) * (1 << f_width); n++) { |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 319 | if (config_reg->enum_ids[n] == enum_id) { |
| 320 | *crp = config_reg; |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 321 | *fieldp = n / (1 << f_width); |
| 322 | *valuep = n % (1 << f_width); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 323 | *cntp = &config_reg->cnt[n / (1 << f_width)]; |
| 324 | return 0; |
| 325 | } |
| 326 | } |
| 327 | k++; |
| 328 | } |
| 329 | |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio, |
| 334 | int pos, pinmux_enum_t *enum_idp) |
| 335 | { |
| 336 | pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id; |
| 337 | pinmux_enum_t *data = gpioc->gpio_data; |
| 338 | int k; |
| 339 | |
| 340 | if (!enum_in_range(enum_id, &gpioc->data)) { |
| 341 | if (!enum_in_range(enum_id, &gpioc->mark)) { |
| 342 | pr_err("non data/mark enum_id for gpio %d\n", gpio); |
| 343 | return -1; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (pos) { |
| 348 | *enum_idp = data[pos + 1]; |
| 349 | return pos + 1; |
| 350 | } |
| 351 | |
| 352 | for (k = 0; k < gpioc->gpio_data_size; k++) { |
| 353 | if (data[k] == enum_id) { |
| 354 | *enum_idp = data[k + 1]; |
| 355 | return k + 1; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio); |
| 360 | return -1; |
| 361 | } |
| 362 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 363 | enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE }; |
| 364 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 365 | static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio, |
| 366 | int pinmux_type, int cfg_mode) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 367 | { |
| 368 | struct pinmux_cfg_reg *cr = NULL; |
| 369 | pinmux_enum_t enum_id; |
| 370 | struct pinmux_range *range; |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 371 | int in_range, pos, field, value; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 372 | unsigned long *cntp; |
| 373 | |
| 374 | switch (pinmux_type) { |
| 375 | |
| 376 | case PINMUX_TYPE_FUNCTION: |
| 377 | range = NULL; |
| 378 | break; |
| 379 | |
| 380 | case PINMUX_TYPE_OUTPUT: |
| 381 | range = &gpioc->output; |
| 382 | break; |
| 383 | |
| 384 | case PINMUX_TYPE_INPUT: |
| 385 | range = &gpioc->input; |
| 386 | break; |
| 387 | |
| 388 | case PINMUX_TYPE_INPUT_PULLUP: |
| 389 | range = &gpioc->input_pu; |
| 390 | break; |
| 391 | |
| 392 | case PINMUX_TYPE_INPUT_PULLDOWN: |
| 393 | range = &gpioc->input_pd; |
| 394 | break; |
| 395 | |
| 396 | default: |
| 397 | goto out_err; |
| 398 | } |
| 399 | |
| 400 | pos = 0; |
| 401 | enum_id = 0; |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 402 | field = 0; |
| 403 | value = 0; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 404 | while (1) { |
| 405 | pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id); |
| 406 | if (pos <= 0) |
| 407 | goto out_err; |
| 408 | |
| 409 | if (!enum_id) |
| 410 | break; |
| 411 | |
Magnus Damm | 50dd314 | 2010-01-19 13:52:28 +0000 | [diff] [blame] | 412 | /* first check if this is a function enum */ |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 413 | in_range = enum_in_range(enum_id, &gpioc->function); |
Magnus Damm | 50dd314 | 2010-01-19 13:52:28 +0000 | [diff] [blame] | 414 | if (!in_range) { |
| 415 | /* not a function enum */ |
| 416 | if (range) { |
| 417 | /* |
| 418 | * other range exists, so this pin is |
| 419 | * a regular GPIO pin that now is being |
| 420 | * bound to a specific direction. |
| 421 | * |
| 422 | * for this case we only allow function enums |
| 423 | * and the enums that match the other range. |
| 424 | */ |
| 425 | in_range = enum_in_range(enum_id, range); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 426 | |
Magnus Damm | 50dd314 | 2010-01-19 13:52:28 +0000 | [diff] [blame] | 427 | /* |
| 428 | * special case pass through for fixed |
| 429 | * input-only or output-only pins without |
| 430 | * function enum register association. |
| 431 | */ |
| 432 | if (in_range && enum_id == range->force) |
| 433 | continue; |
| 434 | } else { |
| 435 | /* |
| 436 | * no other range exists, so this pin |
| 437 | * must then be of the function type. |
| 438 | * |
| 439 | * allow function type pins to select |
| 440 | * any combination of function/in/out |
| 441 | * in their MARK lists. |
| 442 | */ |
| 443 | in_range = 1; |
| 444 | } |
Magnus Damm | 42eed42 | 2008-10-22 18:29:17 +0900 | [diff] [blame] | 445 | } |
| 446 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 447 | if (!in_range) |
| 448 | continue; |
| 449 | |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 450 | if (get_config_reg(gpioc, enum_id, &cr, |
| 451 | &field, &value, &cntp) != 0) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 452 | goto out_err; |
| 453 | |
| 454 | switch (cfg_mode) { |
| 455 | case GPIO_CFG_DRYRUN: |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame^] | 456 | if (!*cntp || |
| 457 | (read_config_reg(gpioc, cr, field) != value)) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 458 | continue; |
| 459 | break; |
| 460 | |
| 461 | case GPIO_CFG_REQ: |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 462 | write_config_reg(gpioc, cr, field, value); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 463 | *cntp = *cntp + 1; |
| 464 | break; |
| 465 | |
| 466 | case GPIO_CFG_FREE: |
| 467 | *cntp = *cntp - 1; |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | out_err: |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | static DEFINE_SPINLOCK(gpio_lock); |
| 478 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 479 | static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 480 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 481 | return container_of(chip, struct pinmux_info, chip); |
| 482 | } |
| 483 | |
| 484 | static int sh_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 485 | { |
| 486 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 487 | struct pinmux_data_reg *dummy; |
| 488 | unsigned long flags; |
| 489 | int i, ret, pinmux_type; |
| 490 | |
| 491 | ret = -EINVAL; |
| 492 | |
| 493 | if (!gpioc) |
| 494 | goto err_out; |
| 495 | |
| 496 | spin_lock_irqsave(&gpio_lock, flags); |
| 497 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 498 | if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 499 | goto err_unlock; |
| 500 | |
| 501 | /* setup pin function here if no data is associated with pin */ |
| 502 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 503 | if (get_data_reg(gpioc, offset, &dummy, &i) != 0) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 504 | pinmux_type = PINMUX_TYPE_FUNCTION; |
| 505 | else |
| 506 | pinmux_type = PINMUX_TYPE_GPIO; |
| 507 | |
| 508 | if (pinmux_type == PINMUX_TYPE_FUNCTION) { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 509 | if (pinmux_config_gpio(gpioc, offset, |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 510 | pinmux_type, |
| 511 | GPIO_CFG_DRYRUN) != 0) |
| 512 | goto err_unlock; |
| 513 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 514 | if (pinmux_config_gpio(gpioc, offset, |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 515 | pinmux_type, |
| 516 | GPIO_CFG_REQ) != 0) |
| 517 | BUG(); |
| 518 | } |
| 519 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 520 | gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE; |
| 521 | gpioc->gpios[offset].flags |= pinmux_type; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 522 | |
| 523 | ret = 0; |
| 524 | err_unlock: |
| 525 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 526 | err_out: |
| 527 | return ret; |
| 528 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 529 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 530 | static void sh_gpio_free(struct gpio_chip *chip, unsigned offset) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 531 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 532 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 533 | unsigned long flags; |
| 534 | int pinmux_type; |
| 535 | |
| 536 | if (!gpioc) |
| 537 | return; |
| 538 | |
| 539 | spin_lock_irqsave(&gpio_lock, flags); |
| 540 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 541 | pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE; |
| 542 | pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE); |
| 543 | gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE; |
| 544 | gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 545 | |
| 546 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 547 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 548 | |
| 549 | static int pinmux_direction(struct pinmux_info *gpioc, |
| 550 | unsigned gpio, int new_pinmux_type) |
| 551 | { |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 552 | int pinmux_type; |
| 553 | int ret = -EINVAL; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 554 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 555 | if (!gpioc) |
| 556 | goto err_out; |
| 557 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 558 | pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE; |
| 559 | |
| 560 | switch (pinmux_type) { |
| 561 | case PINMUX_TYPE_GPIO: |
| 562 | break; |
| 563 | case PINMUX_TYPE_OUTPUT: |
| 564 | case PINMUX_TYPE_INPUT: |
| 565 | case PINMUX_TYPE_INPUT_PULLUP: |
| 566 | case PINMUX_TYPE_INPUT_PULLDOWN: |
| 567 | pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE); |
| 568 | break; |
| 569 | default: |
| 570 | goto err_out; |
| 571 | } |
| 572 | |
| 573 | if (pinmux_config_gpio(gpioc, gpio, |
| 574 | new_pinmux_type, |
| 575 | GPIO_CFG_DRYRUN) != 0) |
| 576 | goto err_out; |
| 577 | |
| 578 | if (pinmux_config_gpio(gpioc, gpio, |
| 579 | new_pinmux_type, |
| 580 | GPIO_CFG_REQ) != 0) |
| 581 | BUG(); |
| 582 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 583 | gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE; |
| 584 | gpioc->gpios[gpio].flags |= new_pinmux_type; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 585 | |
| 586 | ret = 0; |
| 587 | err_out: |
| 588 | return ret; |
| 589 | } |
| 590 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 591 | static int sh_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 592 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 593 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 594 | unsigned long flags; |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 595 | int ret; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 596 | |
| 597 | spin_lock_irqsave(&gpio_lock, flags); |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 598 | ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 599 | spin_unlock_irqrestore(&gpio_lock, flags); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 600 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 601 | return ret; |
| 602 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 603 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 604 | static void sh_gpio_set_value(struct pinmux_info *gpioc, |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 605 | unsigned gpio, int value) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 606 | { |
| 607 | struct pinmux_data_reg *dr = NULL; |
| 608 | int bit = 0; |
| 609 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 610 | if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 611 | BUG(); |
| 612 | else |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 613 | gpio_write_bit(dr, bit, value); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 614 | } |
| 615 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 616 | static int sh_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 617 | int value) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 618 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 619 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 620 | unsigned long flags; |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 621 | int ret; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 622 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 623 | sh_gpio_set_value(gpioc, offset, value); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 624 | spin_lock_irqsave(&gpio_lock, flags); |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 625 | ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 626 | spin_unlock_irqrestore(&gpio_lock, flags); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 627 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 628 | return ret; |
| 629 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 630 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 631 | static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 632 | { |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 633 | struct pinmux_data_reg *dr = NULL; |
| 634 | int bit = 0; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 635 | |
Paul Mundt | e8184a4 | 2010-10-04 05:15:20 +0900 | [diff] [blame] | 636 | if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0) |
| 637 | return -EINVAL; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 638 | |
Magnus Damm | 92554d9 | 2011-12-14 01:00:37 +0900 | [diff] [blame] | 639 | return gpio_read_bit(dr, bit); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 640 | } |
| 641 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 642 | static int sh_gpio_get(struct gpio_chip *chip, unsigned offset) |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 643 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 644 | return sh_gpio_get_value(chip_to_pinmux(chip), offset); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 645 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 646 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 647 | static void sh_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 648 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 649 | sh_gpio_set_value(chip_to_pinmux(chip), offset, value); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 650 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 651 | |
Magnus Damm | ad2a8e7 | 2011-09-28 16:50:58 +0900 | [diff] [blame] | 652 | static int sh_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 653 | { |
| 654 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
| 655 | pinmux_enum_t enum_id; |
| 656 | pinmux_enum_t *enum_ids; |
| 657 | int i, k, pos; |
| 658 | |
| 659 | pos = 0; |
| 660 | enum_id = 0; |
| 661 | while (1) { |
| 662 | pos = get_gpio_enum_id(gpioc, offset, pos, &enum_id); |
| 663 | if (pos <= 0 || !enum_id) |
| 664 | break; |
| 665 | |
| 666 | for (i = 0; i < gpioc->gpio_irq_size; i++) { |
| 667 | enum_ids = gpioc->gpio_irq[i].enum_ids; |
| 668 | for (k = 0; enum_ids[k]; k++) { |
| 669 | if (enum_ids[k] == enum_id) |
| 670 | return gpioc->gpio_irq[i].irq; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | return -ENOSYS; |
| 676 | } |
| 677 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 678 | int register_pinmux(struct pinmux_info *pip) |
| 679 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 680 | struct gpio_chip *chip = &pip->chip; |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 681 | int ret; |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 682 | |
Paul Mundt | b72421d | 2010-10-04 03:54:56 +0900 | [diff] [blame] | 683 | pr_info("%s handling gpio %d -> %d\n", |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 684 | pip->name, pip->first_gpio, pip->last_gpio); |
| 685 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 686 | ret = pfc_ioremap(pip); |
| 687 | if (ret < 0) |
| 688 | return ret; |
| 689 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 690 | setup_data_regs(pip); |
| 691 | |
| 692 | chip->request = sh_gpio_request; |
| 693 | chip->free = sh_gpio_free; |
| 694 | chip->direction_input = sh_gpio_direction_input; |
| 695 | chip->get = sh_gpio_get; |
| 696 | chip->direction_output = sh_gpio_direction_output; |
| 697 | chip->set = sh_gpio_set; |
Magnus Damm | ad2a8e7 | 2011-09-28 16:50:58 +0900 | [diff] [blame] | 698 | chip->to_irq = sh_gpio_to_irq; |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 699 | |
| 700 | WARN_ON(pip->first_gpio != 0); /* needs testing */ |
| 701 | |
| 702 | chip->label = pip->name; |
| 703 | chip->owner = THIS_MODULE; |
| 704 | chip->base = pip->first_gpio; |
| 705 | chip->ngpio = (pip->last_gpio - pip->first_gpio) + 1; |
| 706 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 707 | ret = gpiochip_add(chip); |
| 708 | if (ret < 0) |
| 709 | pfc_iounmap(pip); |
| 710 | |
| 711 | return ret; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 712 | } |
Paul Mundt | b72421d | 2010-10-04 03:54:56 +0900 | [diff] [blame] | 713 | |
| 714 | int unregister_pinmux(struct pinmux_info *pip) |
| 715 | { |
| 716 | pr_info("%s deregistering\n", pip->name); |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 717 | pfc_iounmap(pip); |
Paul Mundt | b72421d | 2010-10-04 03:54:56 +0900 | [diff] [blame] | 718 | return gpiochip_remove(&pip->chip); |
| 719 | } |