Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 1 | /* |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 2 | * SuperH Pin Function Controller support. |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Magnus Damm |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 5 | * Copyright (C) 2009 - 2012 Paul Mundt |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file "COPYING" in the main directory of this archive |
| 9 | * for more details. |
| 10 | */ |
Paul Mundt | b72421d | 2010-10-04 03:54:56 +0900 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/kernel.h> |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 15 | #include <linux/sh_pfc.h> |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 16 | #include <linux/module.h> |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 17 | #include <linux/err.h> |
| 18 | #include <linux/io.h> |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/ioport.h> |
| 22 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 23 | static struct sh_pfc *sh_pfc __read_mostly; |
| 24 | |
| 25 | static inline bool sh_pfc_initialized(void) |
| 26 | { |
| 27 | return !!sh_pfc; |
| 28 | } |
| 29 | |
| 30 | static void pfc_iounmap(struct sh_pfc *pfc) |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 31 | { |
| 32 | int k; |
| 33 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 34 | for (k = 0; k < pfc->num_resources; k++) |
| 35 | if (pfc->window[k].virt) |
| 36 | iounmap(pfc->window[k].virt); |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 37 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 38 | kfree(pfc->window); |
| 39 | pfc->window = NULL; |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 40 | } |
| 41 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 42 | static int pfc_ioremap(struct sh_pfc *pfc) |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 43 | { |
| 44 | struct resource *res; |
| 45 | int k; |
| 46 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 47 | if (!pfc->num_resources) |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 48 | return 0; |
| 49 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 50 | pfc->window = kzalloc(pfc->num_resources * sizeof(*pfc->window), |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 51 | GFP_NOWAIT); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 52 | if (!pfc->window) |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 53 | goto err1; |
| 54 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 55 | for (k = 0; k < pfc->num_resources; k++) { |
| 56 | res = pfc->resource + k; |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 57 | WARN_ON(resource_type(res) != IORESOURCE_MEM); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 58 | pfc->window[k].phys = res->start; |
| 59 | pfc->window[k].size = resource_size(res); |
| 60 | pfc->window[k].virt = ioremap_nocache(res->start, |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 61 | resource_size(res)); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 62 | if (!pfc->window[k].virt) |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 63 | goto err2; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | |
| 68 | err2: |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 69 | pfc_iounmap(pfc); |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 70 | err1: |
| 71 | return -1; |
| 72 | } |
| 73 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 74 | static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc, |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 75 | unsigned long address) |
| 76 | { |
| 77 | struct pfc_window *window; |
| 78 | int k; |
| 79 | |
| 80 | /* scan through physical windows and convert address */ |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 81 | for (k = 0; k < pfc->num_resources; k++) { |
| 82 | window = pfc->window + k; |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 83 | |
| 84 | if (address < window->phys) |
| 85 | continue; |
| 86 | |
| 87 | if (address >= (window->phys + window->size)) |
| 88 | continue; |
| 89 | |
| 90 | return window->virt + (address - window->phys); |
| 91 | } |
| 92 | |
| 93 | /* no windows defined, register must be 1:1 mapped virt:phys */ |
| 94 | return (void __iomem *)address; |
| 95 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 96 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 97 | static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r) |
| 98 | { |
| 99 | if (enum_id < r->begin) |
| 100 | return 0; |
| 101 | |
| 102 | if (enum_id > r->end) |
| 103 | return 0; |
| 104 | |
| 105 | return 1; |
| 106 | } |
| 107 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 108 | static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg, |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 109 | unsigned long reg_width) |
| 110 | { |
| 111 | switch (reg_width) { |
| 112 | case 8: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 113 | return ioread8(mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 114 | case 16: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 115 | return ioread16(mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 116 | case 32: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 117 | return ioread32(mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | BUG(); |
| 121 | return 0; |
| 122 | } |
| 123 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 124 | static void gpio_write_raw_reg(void __iomem *mapped_reg, |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 125 | unsigned long reg_width, |
| 126 | unsigned long data) |
| 127 | { |
| 128 | switch (reg_width) { |
| 129 | case 8: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 130 | iowrite8(data, mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 131 | return; |
| 132 | case 16: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 133 | iowrite16(data, mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 134 | return; |
| 135 | case 32: |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 136 | iowrite32(data, mapped_reg); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
| 140 | BUG(); |
| 141 | } |
| 142 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 143 | int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos) |
Magnus Damm | 92554d9 | 2011-12-14 01:00:37 +0900 | [diff] [blame] | 144 | { |
| 145 | unsigned long pos; |
| 146 | |
| 147 | pos = dr->reg_width - (in_pos + 1); |
| 148 | |
| 149 | pr_debug("read_bit: addr = %lx, pos = %ld, " |
| 150 | "r_width = %ld\n", dr->reg, pos, dr->reg_width); |
| 151 | |
| 152 | return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1; |
| 153 | } |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 154 | EXPORT_SYMBOL_GPL(sh_pfc_read_bit); |
Magnus Damm | 92554d9 | 2011-12-14 01:00:37 +0900 | [diff] [blame] | 155 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 156 | void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos, |
| 157 | unsigned long value) |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 158 | { |
| 159 | unsigned long pos; |
| 160 | |
| 161 | pos = dr->reg_width - (in_pos + 1); |
| 162 | |
Paul Mundt | ca6f2d7 | 2009-12-09 15:51:27 +0900 | [diff] [blame] | 163 | pr_debug("write_bit addr = %lx, value = %d, pos = %ld, " |
Paul Mundt | fd2cb0c | 2009-11-30 12:15:04 +0900 | [diff] [blame] | 164 | "r_width = %ld\n", |
| 165 | dr->reg, !!value, pos, dr->reg_width); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 166 | |
| 167 | if (value) |
| 168 | set_bit(pos, &dr->reg_shadow); |
| 169 | else |
| 170 | clear_bit(pos, &dr->reg_shadow); |
| 171 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 172 | 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] | 173 | } |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 174 | EXPORT_SYMBOL_GPL(sh_pfc_write_bit); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 175 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 176 | static void config_reg_helper(struct sh_pfc *pfc, |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 177 | struct pinmux_cfg_reg *crp, |
| 178 | unsigned long in_pos, |
| 179 | void __iomem **mapped_regp, |
| 180 | unsigned long *maskp, |
| 181 | unsigned long *posp) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 182 | { |
Magnus Damm | f78a26f | 2011-12-14 01:01:05 +0900 | [diff] [blame] | 183 | int k; |
| 184 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 185 | *mapped_regp = pfc_phys_to_virt(pfc, crp->reg); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 186 | |
Magnus Damm | f78a26f | 2011-12-14 01:01:05 +0900 | [diff] [blame] | 187 | if (crp->field_width) { |
| 188 | *maskp = (1 << crp->field_width) - 1; |
| 189 | *posp = crp->reg_width - ((in_pos + 1) * crp->field_width); |
| 190 | } else { |
| 191 | *maskp = (1 << crp->var_field_width[in_pos]) - 1; |
| 192 | *posp = crp->reg_width; |
| 193 | for (k = 0; k <= in_pos; k++) |
| 194 | *posp -= crp->var_field_width[k]; |
| 195 | } |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 196 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 197 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 198 | static int read_config_reg(struct sh_pfc *pfc, |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 199 | struct pinmux_cfg_reg *crp, |
| 200 | unsigned long field) |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 201 | { |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 202 | void __iomem *mapped_reg; |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 203 | unsigned long mask, pos; |
| 204 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 205 | config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 206 | |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 207 | pr_debug("read_reg: addr = %lx, field = %ld, " |
Paul Mundt | fd2cb0c | 2009-11-30 12:15:04 +0900 | [diff] [blame] | 208 | "r_width = %ld, f_width = %ld\n", |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 209 | crp->reg, field, crp->reg_width, crp->field_width); |
| 210 | |
| 211 | return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask; |
| 212 | } |
| 213 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 214 | static void write_config_reg(struct sh_pfc *pfc, |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 215 | struct pinmux_cfg_reg *crp, |
| 216 | unsigned long field, unsigned long value) |
| 217 | { |
| 218 | void __iomem *mapped_reg; |
Magnus Damm | e499ada | 2011-12-14 01:01:14 +0900 | [diff] [blame] | 219 | unsigned long mask, pos, data; |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 220 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 221 | config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos); |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 222 | |
| 223 | pr_debug("write_reg addr = %lx, value = %ld, field = %ld, " |
| 224 | "r_width = %ld, f_width = %ld\n", |
| 225 | crp->reg, value, field, crp->reg_width, crp->field_width); |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 226 | |
| 227 | mask = ~(mask << pos); |
| 228 | value = value << pos; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 229 | |
Magnus Damm | e499ada | 2011-12-14 01:01:14 +0900 | [diff] [blame] | 230 | data = gpio_read_raw_reg(mapped_reg, crp->reg_width); |
| 231 | data &= mask; |
| 232 | data |= value; |
| 233 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 234 | if (pfc->unlock_reg) |
| 235 | gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->unlock_reg), |
Magnus Damm | e499ada | 2011-12-14 01:01:14 +0900 | [diff] [blame] | 236 | 32, ~data); |
| 237 | |
| 238 | gpio_write_raw_reg(mapped_reg, crp->reg_width, data); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 241 | static int setup_data_reg(struct sh_pfc *pfc, unsigned gpio) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 242 | { |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 243 | struct pinmux_gpio *gpiop = &pfc->gpios[gpio]; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 244 | struct pinmux_data_reg *data_reg; |
| 245 | int k, n; |
| 246 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 247 | if (!enum_in_range(gpiop->enum_id, &pfc->data)) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 248 | return -1; |
| 249 | |
| 250 | k = 0; |
| 251 | while (1) { |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 252 | data_reg = pfc->data_regs + k; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 253 | |
| 254 | if (!data_reg->reg_width) |
| 255 | break; |
| 256 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 257 | data_reg->mapped_reg = pfc_phys_to_virt(pfc, data_reg->reg); |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 258 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 259 | for (n = 0; n < data_reg->reg_width; n++) { |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 260 | if (data_reg->enum_ids[n] == gpiop->enum_id) { |
| 261 | gpiop->flags &= ~PINMUX_FLAG_DREG; |
| 262 | gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT); |
| 263 | gpiop->flags &= ~PINMUX_FLAG_DBIT; |
| 264 | gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 265 | return 0; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | k++; |
| 269 | } |
| 270 | |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 271 | BUG(); |
| 272 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 273 | return -1; |
| 274 | } |
| 275 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 276 | static void setup_data_regs(struct sh_pfc *pfc) |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 277 | { |
| 278 | struct pinmux_data_reg *drp; |
| 279 | int k; |
| 280 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 281 | for (k = pfc->first_gpio; k <= pfc->last_gpio; k++) |
| 282 | setup_data_reg(pfc, k); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 283 | |
| 284 | k = 0; |
| 285 | while (1) { |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 286 | drp = pfc->data_regs + k; |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 287 | |
| 288 | if (!drp->reg_width) |
| 289 | break; |
| 290 | |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 291 | drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg, |
| 292 | drp->reg_width); |
Magnus Damm | 3292094 | 2008-12-25 18:17:26 +0900 | [diff] [blame] | 293 | k++; |
| 294 | } |
| 295 | } |
| 296 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 297 | int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio, |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 298 | struct pinmux_data_reg **drp, int *bitp) |
| 299 | { |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 300 | struct pinmux_gpio *gpiop = &pfc->gpios[gpio]; |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 301 | int k, n; |
| 302 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 303 | if (!enum_in_range(gpiop->enum_id, &pfc->data)) |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 304 | return -1; |
| 305 | |
| 306 | k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT; |
| 307 | n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 308 | *drp = pfc->data_regs + k; |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 309 | *bitp = n; |
| 310 | return 0; |
| 311 | } |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 312 | EXPORT_SYMBOL_GPL(sh_pfc_get_data_reg); |
Magnus Damm | 18801be | 2008-12-25 18:17:09 +0900 | [diff] [blame] | 313 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 314 | static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id, |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 315 | struct pinmux_cfg_reg **crp, |
| 316 | int *fieldp, int *valuep, |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 317 | unsigned long **cntp) |
| 318 | { |
| 319 | struct pinmux_cfg_reg *config_reg; |
Magnus Damm | f78a26f | 2011-12-14 01:01:05 +0900 | [diff] [blame] | 320 | unsigned long r_width, f_width, curr_width, ncomb; |
| 321 | int k, m, n, pos, bit_pos; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 322 | |
| 323 | k = 0; |
| 324 | while (1) { |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 325 | config_reg = pfc->cfg_regs + k; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 326 | |
| 327 | r_width = config_reg->reg_width; |
| 328 | f_width = config_reg->field_width; |
| 329 | |
| 330 | if (!r_width) |
| 331 | break; |
Magnus Damm | f78a26f | 2011-12-14 01:01:05 +0900 | [diff] [blame] | 332 | |
| 333 | pos = 0; |
| 334 | m = 0; |
| 335 | for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) { |
| 336 | if (f_width) |
| 337 | curr_width = f_width; |
| 338 | else |
| 339 | curr_width = config_reg->var_field_width[m]; |
| 340 | |
| 341 | ncomb = 1 << curr_width; |
| 342 | for (n = 0; n < ncomb; n++) { |
| 343 | if (config_reg->enum_ids[pos + n] == enum_id) { |
| 344 | *crp = config_reg; |
| 345 | *fieldp = m; |
| 346 | *valuep = n; |
| 347 | *cntp = &config_reg->cnt[m]; |
| 348 | return 0; |
| 349 | } |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 350 | } |
Magnus Damm | f78a26f | 2011-12-14 01:01:05 +0900 | [diff] [blame] | 351 | pos += ncomb; |
| 352 | m++; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 353 | } |
| 354 | k++; |
| 355 | } |
| 356 | |
| 357 | return -1; |
| 358 | } |
| 359 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 360 | int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos, |
| 361 | pinmux_enum_t *enum_idp) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 362 | { |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 363 | pinmux_enum_t enum_id = pfc->gpios[gpio].enum_id; |
| 364 | pinmux_enum_t *data = pfc->gpio_data; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 365 | int k; |
| 366 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 367 | if (!enum_in_range(enum_id, &pfc->data)) { |
| 368 | if (!enum_in_range(enum_id, &pfc->mark)) { |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 369 | pr_err("non data/mark enum_id for gpio %d\n", gpio); |
| 370 | return -1; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (pos) { |
| 375 | *enum_idp = data[pos + 1]; |
| 376 | return pos + 1; |
| 377 | } |
| 378 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 379 | for (k = 0; k < pfc->gpio_data_size; k++) { |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 380 | if (data[k] == enum_id) { |
| 381 | *enum_idp = data[k + 1]; |
| 382 | return k + 1; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio); |
| 387 | return -1; |
| 388 | } |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 389 | EXPORT_SYMBOL_GPL(sh_pfc_gpio_to_enum); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 390 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 391 | int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type, |
| 392 | int cfg_mode) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 393 | { |
| 394 | struct pinmux_cfg_reg *cr = NULL; |
| 395 | pinmux_enum_t enum_id; |
| 396 | struct pinmux_range *range; |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 397 | int in_range, pos, field, value; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 398 | unsigned long *cntp; |
| 399 | |
| 400 | switch (pinmux_type) { |
| 401 | |
| 402 | case PINMUX_TYPE_FUNCTION: |
| 403 | range = NULL; |
| 404 | break; |
| 405 | |
| 406 | case PINMUX_TYPE_OUTPUT: |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 407 | range = &pfc->output; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | case PINMUX_TYPE_INPUT: |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 411 | range = &pfc->input; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 412 | break; |
| 413 | |
| 414 | case PINMUX_TYPE_INPUT_PULLUP: |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 415 | range = &pfc->input_pu; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 416 | break; |
| 417 | |
| 418 | case PINMUX_TYPE_INPUT_PULLDOWN: |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 419 | range = &pfc->input_pd; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 420 | break; |
| 421 | |
| 422 | default: |
| 423 | goto out_err; |
| 424 | } |
| 425 | |
| 426 | pos = 0; |
| 427 | enum_id = 0; |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 428 | field = 0; |
| 429 | value = 0; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 430 | while (1) { |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 431 | pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 432 | if (pos <= 0) |
| 433 | goto out_err; |
| 434 | |
| 435 | if (!enum_id) |
| 436 | break; |
| 437 | |
Magnus Damm | 50dd314 | 2010-01-19 13:52:28 +0000 | [diff] [blame] | 438 | /* first check if this is a function enum */ |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 439 | in_range = enum_in_range(enum_id, &pfc->function); |
Magnus Damm | 50dd314 | 2010-01-19 13:52:28 +0000 | [diff] [blame] | 440 | if (!in_range) { |
| 441 | /* not a function enum */ |
| 442 | if (range) { |
| 443 | /* |
| 444 | * other range exists, so this pin is |
| 445 | * a regular GPIO pin that now is being |
| 446 | * bound to a specific direction. |
| 447 | * |
| 448 | * for this case we only allow function enums |
| 449 | * and the enums that match the other range. |
| 450 | */ |
| 451 | in_range = enum_in_range(enum_id, range); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 452 | |
Magnus Damm | 50dd314 | 2010-01-19 13:52:28 +0000 | [diff] [blame] | 453 | /* |
| 454 | * special case pass through for fixed |
| 455 | * input-only or output-only pins without |
| 456 | * function enum register association. |
| 457 | */ |
| 458 | if (in_range && enum_id == range->force) |
| 459 | continue; |
| 460 | } else { |
| 461 | /* |
| 462 | * no other range exists, so this pin |
| 463 | * must then be of the function type. |
| 464 | * |
| 465 | * allow function type pins to select |
| 466 | * any combination of function/in/out |
| 467 | * in their MARK lists. |
| 468 | */ |
| 469 | in_range = 1; |
| 470 | } |
Magnus Damm | 42eed42 | 2008-10-22 18:29:17 +0900 | [diff] [blame] | 471 | } |
| 472 | |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 473 | if (!in_range) |
| 474 | continue; |
| 475 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 476 | if (get_config_reg(pfc, enum_id, &cr, |
Magnus Damm | ad4a07f | 2011-12-14 01:00:46 +0900 | [diff] [blame] | 477 | &field, &value, &cntp) != 0) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 478 | goto out_err; |
| 479 | |
| 480 | switch (cfg_mode) { |
| 481 | case GPIO_CFG_DRYRUN: |
Magnus Damm | 18925e1 | 2011-12-14 01:00:55 +0900 | [diff] [blame] | 482 | if (!*cntp || |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 483 | (read_config_reg(pfc, cr, field) != value)) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 484 | continue; |
| 485 | break; |
| 486 | |
| 487 | case GPIO_CFG_REQ: |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 488 | write_config_reg(pfc, cr, field, value); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 489 | *cntp = *cntp + 1; |
| 490 | break; |
| 491 | |
| 492 | case GPIO_CFG_FREE: |
| 493 | *cntp = *cntp - 1; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | out_err: |
| 500 | return -1; |
| 501 | } |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 502 | EXPORT_SYMBOL_GPL(sh_pfc_config_gpio); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 503 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 504 | int sh_pfc_set_direction(struct sh_pfc *pfc, unsigned gpio, |
| 505 | int new_pinmux_type) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 506 | { |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 507 | int pinmux_type; |
| 508 | int ret = -EINVAL; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 509 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 510 | if (!pfc) |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 511 | goto err_out; |
| 512 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 513 | pinmux_type = pfc->gpios[gpio].flags & PINMUX_FLAG_TYPE; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 514 | |
| 515 | switch (pinmux_type) { |
| 516 | case PINMUX_TYPE_GPIO: |
| 517 | break; |
| 518 | case PINMUX_TYPE_OUTPUT: |
| 519 | case PINMUX_TYPE_INPUT: |
| 520 | case PINMUX_TYPE_INPUT_PULLUP: |
| 521 | case PINMUX_TYPE_INPUT_PULLDOWN: |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 522 | sh_pfc_config_gpio(pfc, gpio, pinmux_type, GPIO_CFG_FREE); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 523 | break; |
| 524 | default: |
| 525 | goto err_out; |
| 526 | } |
| 527 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 528 | if (sh_pfc_config_gpio(pfc, gpio, |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 529 | new_pinmux_type, |
| 530 | GPIO_CFG_DRYRUN) != 0) |
| 531 | goto err_out; |
| 532 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 533 | if (sh_pfc_config_gpio(pfc, gpio, |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 534 | new_pinmux_type, |
| 535 | GPIO_CFG_REQ) != 0) |
| 536 | BUG(); |
| 537 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 538 | pfc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE; |
| 539 | pfc->gpios[gpio].flags |= new_pinmux_type; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 540 | |
| 541 | ret = 0; |
| 542 | err_out: |
| 543 | return ret; |
| 544 | } |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 545 | EXPORT_SYMBOL_GPL(sh_pfc_set_direction); |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 546 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 547 | int register_sh_pfc(struct sh_pfc *pfc) |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 548 | { |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 549 | int (*initroutine)(struct sh_pfc *) = NULL; |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 550 | int ret; |
Magnus Damm | 2967dab | 2008-10-08 20:41:43 +0900 | [diff] [blame] | 551 | |
Paul Mundt | 06d5631 | 2012-06-21 00:03:41 +0900 | [diff] [blame^] | 552 | /* |
| 553 | * Ensure that the type encoding fits |
| 554 | */ |
| 555 | BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1)); |
| 556 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 557 | if (sh_pfc) |
| 558 | return -EBUSY; |
Magnus Damm | 0fc64cc | 2008-12-25 18:17:18 +0900 | [diff] [blame] | 559 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 560 | ret = pfc_ioremap(pfc); |
| 561 | if (unlikely(ret < 0)) |
Magnus Damm | b0e1021 | 2011-12-09 12:14:27 +0900 | [diff] [blame] | 562 | return ret; |
| 563 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 564 | spin_lock_init(&pfc->lock); |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 565 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 566 | setup_data_regs(pfc); |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 567 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 568 | sh_pfc = pfc; |
| 569 | pr_info("%s support registered\n", pfc->name); |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 570 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 571 | initroutine = symbol_request(sh_pfc_register_gpiochip); |
| 572 | if (initroutine) { |
| 573 | (*initroutine)(pfc); |
| 574 | symbol_put_addr(initroutine); |
| 575 | } |
Magnus Damm | 69edbba | 2008-12-25 18:17:34 +0900 | [diff] [blame] | 576 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 577 | return 0; |
Paul Mundt | b72421d | 2010-10-04 03:54:56 +0900 | [diff] [blame] | 578 | } |