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