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