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