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