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