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 | */ |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/bitops.h> |
| 19 | #include <linux/gpio.h> |
| 20 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 21 | static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r) |
| 22 | { |
| 23 | if (enum_id < r->begin) |
| 24 | return 0; |
| 25 | |
| 26 | if (enum_id > r->end) |
| 27 | return 0; |
| 28 | |
| 29 | return 1; |
| 30 | } |
| 31 | |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 32 | static unsigned long gpio_read_raw_reg(unsigned long reg, |
| 33 | unsigned long reg_width) |
| 34 | { |
| 35 | switch (reg_width) { |
| 36 | case 8: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 37 | return __raw_readb(reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 38 | case 16: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 39 | return __raw_readw(reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 40 | case 32: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 41 | return __raw_readl(reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | BUG(); |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static void gpio_write_raw_reg(unsigned long reg, |
| 49 | unsigned long reg_width, |
| 50 | unsigned long data) |
| 51 | { |
| 52 | switch (reg_width) { |
| 53 | case 8: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 54 | __raw_writeb(data, reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 55 | return; |
| 56 | case 16: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 57 | __raw_writew(data, reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 58 | return; |
| 59 | case 32: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 60 | __raw_writel(data, reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | BUG(); |
| 65 | } |
| 66 | |
| 67 | static void gpio_write_bit(struct pinmux_data_reg *dr, |
| 68 | unsigned long in_pos, unsigned long value) |
| 69 | { |
| 70 | unsigned long pos; |
| 71 | |
| 72 | pos = dr->reg_width - (in_pos + 1); |
| 73 | |
Paul Mundt | fd2cb0c | 2009-11-30 12:15:04 +0900 | [diff] [blame] | 74 | pr_debug("write_bit addr = %lx, value = %ld, pos = %ld, " |
| 75 | "r_width = %ld\n", |
| 76 | dr->reg, !!value, pos, dr->reg_width); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 77 | |
| 78 | if (value) |
| 79 | set_bit(pos, &dr->reg_shadow); |
| 80 | else |
| 81 | clear_bit(pos, &dr->reg_shadow); |
| 82 | |
| 83 | gpio_write_raw_reg(dr->reg, dr->reg_width, dr->reg_shadow); |
| 84 | } |
| 85 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 86 | static int gpio_read_reg(unsigned long reg, unsigned long reg_width, |
| 87 | unsigned long field_width, unsigned long in_pos) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 88 | { |
| 89 | unsigned long data, mask, pos; |
| 90 | |
| 91 | data = 0; |
| 92 | mask = (1 << field_width) - 1; |
| 93 | pos = reg_width - ((in_pos + 1) * field_width); |
| 94 | |
Paul Mundt | fd2cb0c | 2009-11-30 12:15:04 +0900 | [diff] [blame] | 95 | pr_debug("read_reg: addr = %lx, pos = %ld, " |
| 96 | "r_width = %ld, f_width = %ld\n", |
| 97 | reg, pos, reg_width, field_width); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 98 | |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 99 | data = gpio_read_raw_reg(reg, reg_width); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 100 | return (data >> pos) & mask; |
| 101 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 102 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 103 | static void gpio_write_reg(unsigned long reg, unsigned long reg_width, |
| 104 | unsigned long field_width, unsigned long in_pos, |
| 105 | unsigned long value) |
| 106 | { |
| 107 | unsigned long mask, pos; |
| 108 | |
| 109 | mask = (1 << field_width) - 1; |
| 110 | pos = reg_width - ((in_pos + 1) * field_width); |
| 111 | |
Paul Mundt | fd2cb0c | 2009-11-30 12:15:04 +0900 | [diff] [blame] | 112 | pr_debug("write_reg addr = %lx, value = %ld, pos = %ld, " |
| 113 | "r_width = %ld, f_width = %ld\n", |
| 114 | reg, value, pos, reg_width, field_width); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 115 | |
| 116 | mask = ~(mask << pos); |
| 117 | value = value << pos; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 118 | |
| 119 | switch (reg_width) { |
| 120 | case 8: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 121 | __raw_writeb((__raw_readb(reg) & mask) | value, reg); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 122 | break; |
| 123 | case 16: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 124 | __raw_writew((__raw_readw(reg) & mask) | value, reg); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 125 | break; |
| 126 | case 32: |
Paul Mundt | 9cdae91 | 2009-11-30 12:10:41 +0900 | [diff] [blame] | 127 | __raw_writel((__raw_readl(reg) & mask) | value, reg); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 128 | break; |
| 129 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 130 | } |
| 131 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 132 | static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 133 | { |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 134 | struct pinmux_gpio *gpiop = &gpioc->gpios[gpio]; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 135 | struct pinmux_data_reg *data_reg; |
| 136 | int k, n; |
| 137 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 138 | if (!enum_in_range(gpiop->enum_id, &gpioc->data)) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 139 | return -1; |
| 140 | |
| 141 | k = 0; |
| 142 | while (1) { |
| 143 | data_reg = gpioc->data_regs + k; |
| 144 | |
| 145 | if (!data_reg->reg_width) |
| 146 | break; |
| 147 | |
| 148 | for (n = 0; n < data_reg->reg_width; n++) { |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 149 | if (data_reg->enum_ids[n] == gpiop->enum_id) { |
| 150 | gpiop->flags &= ~PINMUX_FLAG_DREG; |
| 151 | gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT); |
| 152 | gpiop->flags &= ~PINMUX_FLAG_DBIT; |
| 153 | gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 154 | return 0; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | k++; |
| 158 | } |
| 159 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 160 | BUG(); |
| 161 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 162 | return -1; |
| 163 | } |
| 164 | |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 165 | static void setup_data_regs(struct pinmux_info *gpioc) |
| 166 | { |
| 167 | struct pinmux_data_reg *drp; |
| 168 | int k; |
| 169 | |
| 170 | for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++) |
| 171 | setup_data_reg(gpioc, k); |
| 172 | |
| 173 | k = 0; |
| 174 | while (1) { |
| 175 | drp = gpioc->data_regs + k; |
| 176 | |
| 177 | if (!drp->reg_width) |
| 178 | break; |
| 179 | |
| 180 | drp->reg_shadow = gpio_read_raw_reg(drp->reg, drp->reg_width); |
| 181 | k++; |
| 182 | } |
| 183 | } |
| 184 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 185 | static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio, |
| 186 | struct pinmux_data_reg **drp, int *bitp) |
| 187 | { |
| 188 | struct pinmux_gpio *gpiop = &gpioc->gpios[gpio]; |
| 189 | int k, n; |
| 190 | |
| 191 | if (!enum_in_range(gpiop->enum_id, &gpioc->data)) |
| 192 | return -1; |
| 193 | |
| 194 | k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT; |
| 195 | n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT; |
| 196 | *drp = gpioc->data_regs + k; |
| 197 | *bitp = n; |
| 198 | return 0; |
| 199 | } |
| 200 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 201 | static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id, |
| 202 | struct pinmux_cfg_reg **crp, int *indexp, |
| 203 | unsigned long **cntp) |
| 204 | { |
| 205 | struct pinmux_cfg_reg *config_reg; |
| 206 | unsigned long r_width, f_width; |
| 207 | int k, n; |
| 208 | |
| 209 | k = 0; |
| 210 | while (1) { |
| 211 | config_reg = gpioc->cfg_regs + k; |
| 212 | |
| 213 | r_width = config_reg->reg_width; |
| 214 | f_width = config_reg->field_width; |
| 215 | |
| 216 | if (!r_width) |
| 217 | break; |
| 218 | for (n = 0; n < (r_width / f_width) * 1 << f_width; n++) { |
| 219 | if (config_reg->enum_ids[n] == enum_id) { |
| 220 | *crp = config_reg; |
| 221 | *indexp = n; |
| 222 | *cntp = &config_reg->cnt[n / (1 << f_width)]; |
| 223 | return 0; |
| 224 | } |
| 225 | } |
| 226 | k++; |
| 227 | } |
| 228 | |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio, |
| 233 | int pos, pinmux_enum_t *enum_idp) |
| 234 | { |
| 235 | pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id; |
| 236 | pinmux_enum_t *data = gpioc->gpio_data; |
| 237 | int k; |
| 238 | |
| 239 | if (!enum_in_range(enum_id, &gpioc->data)) { |
| 240 | if (!enum_in_range(enum_id, &gpioc->mark)) { |
| 241 | pr_err("non data/mark enum_id for gpio %d\n", gpio); |
| 242 | return -1; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (pos) { |
| 247 | *enum_idp = data[pos + 1]; |
| 248 | return pos + 1; |
| 249 | } |
| 250 | |
| 251 | for (k = 0; k < gpioc->gpio_data_size; k++) { |
| 252 | if (data[k] == enum_id) { |
| 253 | *enum_idp = data[k + 1]; |
| 254 | return k + 1; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio); |
| 259 | return -1; |
| 260 | } |
| 261 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 262 | static void write_config_reg(struct pinmux_info *gpioc, |
| 263 | struct pinmux_cfg_reg *crp, |
| 264 | int index) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 265 | { |
| 266 | unsigned long ncomb, pos, value; |
| 267 | |
| 268 | ncomb = 1 << crp->field_width; |
| 269 | pos = index / ncomb; |
| 270 | value = index % ncomb; |
| 271 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 272 | gpio_write_reg(crp->reg, crp->reg_width, crp->field_width, pos, value); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int check_config_reg(struct pinmux_info *gpioc, |
| 276 | struct pinmux_cfg_reg *crp, |
| 277 | int index) |
| 278 | { |
| 279 | unsigned long ncomb, pos, value; |
| 280 | |
| 281 | ncomb = 1 << crp->field_width; |
| 282 | pos = index / ncomb; |
| 283 | value = index % ncomb; |
| 284 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 285 | if (gpio_read_reg(crp->reg, crp->reg_width, |
| 286 | crp->field_width, pos) == value) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 287 | return 0; |
| 288 | |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE }; |
| 293 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 294 | static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio, |
| 295 | int pinmux_type, int cfg_mode) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 296 | { |
| 297 | struct pinmux_cfg_reg *cr = NULL; |
| 298 | pinmux_enum_t enum_id; |
| 299 | struct pinmux_range *range; |
| 300 | int in_range, pos, index; |
| 301 | unsigned long *cntp; |
| 302 | |
| 303 | switch (pinmux_type) { |
| 304 | |
| 305 | case PINMUX_TYPE_FUNCTION: |
| 306 | range = NULL; |
| 307 | break; |
| 308 | |
| 309 | case PINMUX_TYPE_OUTPUT: |
| 310 | range = &gpioc->output; |
| 311 | break; |
| 312 | |
| 313 | case PINMUX_TYPE_INPUT: |
| 314 | range = &gpioc->input; |
| 315 | break; |
| 316 | |
| 317 | case PINMUX_TYPE_INPUT_PULLUP: |
| 318 | range = &gpioc->input_pu; |
| 319 | break; |
| 320 | |
| 321 | case PINMUX_TYPE_INPUT_PULLDOWN: |
| 322 | range = &gpioc->input_pd; |
| 323 | break; |
| 324 | |
| 325 | default: |
| 326 | goto out_err; |
| 327 | } |
| 328 | |
| 329 | pos = 0; |
| 330 | enum_id = 0; |
| 331 | index = 0; |
| 332 | while (1) { |
| 333 | pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id); |
| 334 | if (pos <= 0) |
| 335 | goto out_err; |
| 336 | |
| 337 | if (!enum_id) |
| 338 | break; |
| 339 | |
| 340 | in_range = enum_in_range(enum_id, &gpioc->function); |
Magnus Damm | 42eed42 | 2008-10-22 18:29:17 +0900 | [diff] [blame] | 341 | if (!in_range && range) { |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 342 | in_range = enum_in_range(enum_id, range); |
| 343 | |
Magnus Damm | 42eed42 | 2008-10-22 18:29:17 +0900 | [diff] [blame] | 344 | if (in_range && enum_id == range->force) |
| 345 | continue; |
| 346 | } |
| 347 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 348 | if (!in_range) |
| 349 | continue; |
| 350 | |
| 351 | if (get_config_reg(gpioc, enum_id, &cr, &index, &cntp) != 0) |
| 352 | goto out_err; |
| 353 | |
| 354 | switch (cfg_mode) { |
| 355 | case GPIO_CFG_DRYRUN: |
| 356 | if (!*cntp || !check_config_reg(gpioc, cr, index)) |
| 357 | continue; |
| 358 | break; |
| 359 | |
| 360 | case GPIO_CFG_REQ: |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 361 | write_config_reg(gpioc, cr, index); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 362 | *cntp = *cntp + 1; |
| 363 | break; |
| 364 | |
| 365 | case GPIO_CFG_FREE: |
| 366 | *cntp = *cntp - 1; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | out_err: |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | static DEFINE_SPINLOCK(gpio_lock); |
| 377 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 378 | static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 379 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 380 | return container_of(chip, struct pinmux_info, chip); |
| 381 | } |
| 382 | |
| 383 | static int sh_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 384 | { |
| 385 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 386 | struct pinmux_data_reg *dummy; |
| 387 | unsigned long flags; |
| 388 | int i, ret, pinmux_type; |
| 389 | |
| 390 | ret = -EINVAL; |
| 391 | |
| 392 | if (!gpioc) |
| 393 | goto err_out; |
| 394 | |
| 395 | spin_lock_irqsave(&gpio_lock, flags); |
| 396 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 397 | if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 398 | goto err_unlock; |
| 399 | |
| 400 | /* setup pin function here if no data is associated with pin */ |
| 401 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 402 | if (get_data_reg(gpioc, offset, &dummy, &i) != 0) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 403 | pinmux_type = PINMUX_TYPE_FUNCTION; |
| 404 | else |
| 405 | pinmux_type = PINMUX_TYPE_GPIO; |
| 406 | |
| 407 | if (pinmux_type == PINMUX_TYPE_FUNCTION) { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 408 | if (pinmux_config_gpio(gpioc, offset, |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 409 | pinmux_type, |
| 410 | GPIO_CFG_DRYRUN) != 0) |
| 411 | goto err_unlock; |
| 412 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 413 | if (pinmux_config_gpio(gpioc, offset, |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 414 | pinmux_type, |
| 415 | GPIO_CFG_REQ) != 0) |
| 416 | BUG(); |
| 417 | } |
| 418 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 419 | gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE; |
| 420 | gpioc->gpios[offset].flags |= pinmux_type; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 421 | |
| 422 | ret = 0; |
| 423 | err_unlock: |
| 424 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 425 | err_out: |
| 426 | return ret; |
| 427 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 428 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 429 | static void sh_gpio_free(struct gpio_chip *chip, unsigned offset) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 430 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 431 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 432 | unsigned long flags; |
| 433 | int pinmux_type; |
| 434 | |
| 435 | if (!gpioc) |
| 436 | return; |
| 437 | |
| 438 | spin_lock_irqsave(&gpio_lock, flags); |
| 439 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 440 | pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE; |
| 441 | pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE); |
| 442 | gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE; |
| 443 | gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 444 | |
| 445 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 446 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 447 | |
| 448 | static int pinmux_direction(struct pinmux_info *gpioc, |
| 449 | unsigned gpio, int new_pinmux_type) |
| 450 | { |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 451 | int pinmux_type; |
| 452 | int ret = -EINVAL; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 453 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 454 | if (!gpioc) |
| 455 | goto err_out; |
| 456 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 457 | pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE; |
| 458 | |
| 459 | switch (pinmux_type) { |
| 460 | case PINMUX_TYPE_GPIO: |
| 461 | break; |
| 462 | case PINMUX_TYPE_OUTPUT: |
| 463 | case PINMUX_TYPE_INPUT: |
| 464 | case PINMUX_TYPE_INPUT_PULLUP: |
| 465 | case PINMUX_TYPE_INPUT_PULLDOWN: |
| 466 | pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE); |
| 467 | break; |
| 468 | default: |
| 469 | goto err_out; |
| 470 | } |
| 471 | |
| 472 | if (pinmux_config_gpio(gpioc, gpio, |
| 473 | new_pinmux_type, |
| 474 | GPIO_CFG_DRYRUN) != 0) |
| 475 | goto err_out; |
| 476 | |
| 477 | if (pinmux_config_gpio(gpioc, gpio, |
| 478 | new_pinmux_type, |
| 479 | GPIO_CFG_REQ) != 0) |
| 480 | BUG(); |
| 481 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 482 | gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE; |
| 483 | gpioc->gpios[gpio].flags |= new_pinmux_type; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 484 | |
| 485 | ret = 0; |
| 486 | err_out: |
| 487 | return ret; |
| 488 | } |
| 489 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 490 | static int sh_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 491 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 492 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 493 | unsigned long flags; |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 494 | int ret; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 495 | |
| 496 | spin_lock_irqsave(&gpio_lock, flags); |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 497 | ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 498 | spin_unlock_irqrestore(&gpio_lock, flags); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 499 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 500 | return ret; |
| 501 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 502 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 503 | static void sh_gpio_set_value(struct pinmux_info *gpioc, |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 504 | unsigned gpio, int value) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 505 | { |
| 506 | struct pinmux_data_reg *dr = NULL; |
| 507 | int bit = 0; |
| 508 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 509 | if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 510 | BUG(); |
| 511 | else |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 512 | gpio_write_bit(dr, bit, value); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 513 | } |
| 514 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 515 | static int sh_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 516 | int value) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 517 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 518 | struct pinmux_info *gpioc = chip_to_pinmux(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 519 | unsigned long flags; |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 520 | int ret; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 521 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 522 | sh_gpio_set_value(gpioc, offset, value); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 523 | spin_lock_irqsave(&gpio_lock, flags); |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 524 | ret = pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 525 | spin_unlock_irqrestore(&gpio_lock, flags); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 526 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 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 int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 531 | { |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 532 | struct pinmux_data_reg *dr = NULL; |
| 533 | int bit = 0; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 534 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 535 | if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0) { |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 536 | BUG(); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 537 | return 0; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 538 | } |
| 539 | |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 540 | return gpio_read_reg(dr->reg, dr->reg_width, 1, bit); |
| 541 | } |
| 542 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 543 | static int sh_gpio_get(struct gpio_chip *chip, unsigned offset) |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 544 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 545 | return sh_gpio_get_value(chip_to_pinmux(chip), offset); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 546 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 547 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 548 | 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] | 549 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 550 | sh_gpio_set_value(chip_to_pinmux(chip), offset, value); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 551 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 552 | |
| 553 | int register_pinmux(struct pinmux_info *pip) |
| 554 | { |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 555 | struct gpio_chip *chip = &pip->chip; |
| 556 | |
| 557 | pr_info("sh pinmux: %s handling gpio %d -> %d\n", |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 558 | pip->name, pip->first_gpio, pip->last_gpio); |
| 559 | |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 560 | setup_data_regs(pip); |
| 561 | |
| 562 | chip->request = sh_gpio_request; |
| 563 | chip->free = sh_gpio_free; |
| 564 | chip->direction_input = sh_gpio_direction_input; |
| 565 | chip->get = sh_gpio_get; |
| 566 | chip->direction_output = sh_gpio_direction_output; |
| 567 | chip->set = sh_gpio_set; |
| 568 | |
| 569 | WARN_ON(pip->first_gpio != 0); /* needs testing */ |
| 570 | |
| 571 | chip->label = pip->name; |
| 572 | chip->owner = THIS_MODULE; |
| 573 | chip->base = pip->first_gpio; |
| 574 | chip->ngpio = (pip->last_gpio - pip->first_gpio) + 1; |
| 575 | |
| 576 | return gpiochip_add(chip); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 577 | } |