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