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