Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Pin Control driver for SuperH Pin Function Controller. |
| 4 | * |
| 5 | * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart |
| 6 | * |
| 7 | * Copyright (C) 2008 Magnus Damm |
| 8 | * Copyright (C) 2009 - 2012 Paul Mundt |
| 9 | * Copyright (C) 2017 Marek Vasut |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #define DRV_NAME "sh-pfc" |
| 13 | |
| 14 | #include <common.h> |
| 15 | #include <dm.h> |
| 16 | #include <errno.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <dm/devres.h> |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 19 | #include <dm/pinctrl.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 21 | #include <linux/bug.h> |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 22 | #include <linux/io.h> |
| 23 | #include <linux/sizes.h> |
| 24 | |
| 25 | #include "sh_pfc.h" |
| 26 | |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 27 | enum sh_pfc_model { |
Marek Vasut | c40f2d6 | 2018-01-17 22:18:59 +0100 | [diff] [blame] | 28 | SH_PFC_R8A7790 = 0, |
Marek Vasut | 06ef9e8 | 2018-01-17 17:14:45 +0100 | [diff] [blame] | 29 | SH_PFC_R8A7791, |
Marek Vasut | 1ef3930 | 2018-01-17 22:29:50 +0100 | [diff] [blame] | 30 | SH_PFC_R8A7792, |
Marek Vasut | 06ef9e8 | 2018-01-17 17:14:45 +0100 | [diff] [blame] | 31 | SH_PFC_R8A7793, |
Marek Vasut | 4dd88d5 | 2018-01-17 22:33:59 +0100 | [diff] [blame] | 32 | SH_PFC_R8A7794, |
Marek Vasut | c40f2d6 | 2018-01-17 22:18:59 +0100 | [diff] [blame] | 33 | SH_PFC_R8A7795, |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 34 | SH_PFC_R8A7796, |
Adam Ford | 96980fb | 2020-06-30 09:30:09 -0500 | [diff] [blame] | 35 | SH_PFC_R8A774A1, |
Biju Das | d1d7888 | 2020-10-28 10:34:21 +0000 | [diff] [blame] | 36 | SH_PFC_R8A774B1, |
Lad Prabhakar | 53b88b9 | 2021-03-15 22:24:04 +0000 | [diff] [blame] | 37 | SH_PFC_R8A774C0, |
Biju Das | 121bd00 | 2020-10-28 10:34:22 +0000 | [diff] [blame] | 38 | SH_PFC_R8A774E1, |
Marek Vasut | 72269e0 | 2019-03-04 01:32:44 +0100 | [diff] [blame] | 39 | SH_PFC_R8A77965, |
Marek Vasut | a0e11e5 | 2017-10-09 20:57:29 +0200 | [diff] [blame] | 40 | SH_PFC_R8A77970, |
Marek Vasut | a6a7f48 | 2019-07-29 19:59:44 +0200 | [diff] [blame] | 41 | SH_PFC_R8A77980, |
Marek Vasut | 68a7704 | 2018-04-26 13:09:20 +0200 | [diff] [blame] | 42 | SH_PFC_R8A77990, |
Marek Vasut | 7d35e64 | 2017-10-08 20:57:37 +0200 | [diff] [blame] | 43 | SH_PFC_R8A77995, |
Marek Vasut | 4dbc653 | 2021-04-27 01:55:54 +0200 | [diff] [blame] | 44 | SH_PFC_R8A779A0, |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct sh_pfc_pin_config { |
| 48 | u32 type; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 49 | const char *name; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct sh_pfc_pinctrl { |
| 53 | struct sh_pfc *pfc; |
| 54 | |
| 55 | struct sh_pfc_pin_config *configs; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct sh_pfc_pin_range { |
| 59 | u16 start; |
| 60 | u16 end; |
| 61 | }; |
| 62 | |
| 63 | struct sh_pfc_pinctrl_priv { |
| 64 | struct sh_pfc pfc; |
| 65 | struct sh_pfc_pinctrl pmx; |
| 66 | }; |
| 67 | |
| 68 | int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin) |
| 69 | { |
| 70 | unsigned int offset; |
| 71 | unsigned int i; |
| 72 | |
| 73 | for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) { |
| 74 | const struct sh_pfc_pin_range *range = &pfc->ranges[i]; |
| 75 | |
| 76 | if (pin <= range->end) |
| 77 | return pin >= range->start |
| 78 | ? offset + pin - range->start : -1; |
| 79 | |
| 80 | offset += range->end - range->start + 1; |
| 81 | } |
| 82 | |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | |
| 86 | static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r) |
| 87 | { |
| 88 | if (enum_id < r->begin) |
| 89 | return 0; |
| 90 | |
| 91 | if (enum_id > r->end) |
| 92 | return 0; |
| 93 | |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width) |
| 98 | { |
| 99 | switch (reg_width) { |
| 100 | case 8: |
| 101 | return readb(mapped_reg); |
| 102 | case 16: |
| 103 | return readw(mapped_reg); |
| 104 | case 32: |
| 105 | return readl(mapped_reg); |
| 106 | } |
| 107 | |
| 108 | BUG(); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width, |
| 113 | u32 data) |
| 114 | { |
| 115 | switch (reg_width) { |
| 116 | case 8: |
| 117 | writeb(data, mapped_reg); |
| 118 | return; |
| 119 | case 16: |
| 120 | writew(data, mapped_reg); |
| 121 | return; |
| 122 | case 32: |
| 123 | writel(data, mapped_reg); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | BUG(); |
| 128 | } |
| 129 | |
Marek Vasut | eb13e0f | 2018-06-10 16:05:48 +0200 | [diff] [blame] | 130 | u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg) |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 131 | { |
Marek Vasut | 068a90b | 2018-06-19 06:13:42 +0200 | [diff] [blame] | 132 | return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32); |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Marek Vasut | 651d5a7 | 2021-04-27 22:03:38 +0200 | [diff] [blame] | 135 | static void sh_pfc_unlock_reg(struct sh_pfc *pfc, u32 reg, u32 data) |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 136 | { |
Marek Vasut | 651d5a7 | 2021-04-27 22:03:38 +0200 | [diff] [blame] | 137 | u32 unlock; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 138 | |
Marek Vasut | 651d5a7 | 2021-04-27 22:03:38 +0200 | [diff] [blame] | 139 | if (!pfc->info->unlock_reg) |
| 140 | return; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 141 | |
Marek Vasut | 651d5a7 | 2021-04-27 22:03:38 +0200 | [diff] [blame] | 142 | if (pfc->info->unlock_reg >= 0x80000000UL) |
| 143 | unlock = pfc->info->unlock_reg; |
| 144 | else |
| 145 | /* unlock_reg is a mask */ |
| 146 | unlock = reg & ~pfc->info->unlock_reg; |
| 147 | |
| 148 | sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)unlock, 32, ~data); |
| 149 | } |
| 150 | |
| 151 | void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data) |
| 152 | { |
| 153 | sh_pfc_unlock_reg(pfc, reg, data); |
Marek Vasut | 068a90b | 2018-06-19 06:13:42 +0200 | [diff] [blame] | 154 | sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)reg, 32, data); |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void sh_pfc_config_reg_helper(struct sh_pfc *pfc, |
| 158 | const struct pinmux_cfg_reg *crp, |
| 159 | unsigned int in_pos, |
| 160 | void __iomem **mapped_regp, u32 *maskp, |
| 161 | unsigned int *posp) |
| 162 | { |
| 163 | unsigned int k; |
| 164 | |
| 165 | *mapped_regp = (void __iomem *)(uintptr_t)crp->reg; |
| 166 | |
| 167 | if (crp->field_width) { |
| 168 | *maskp = (1 << crp->field_width) - 1; |
| 169 | *posp = crp->reg_width - ((in_pos + 1) * crp->field_width); |
| 170 | } else { |
| 171 | *maskp = (1 << crp->var_field_width[in_pos]) - 1; |
| 172 | *posp = crp->reg_width; |
| 173 | for (k = 0; k <= in_pos; k++) |
| 174 | *posp -= crp->var_field_width[k]; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static void sh_pfc_write_config_reg(struct sh_pfc *pfc, |
| 179 | const struct pinmux_cfg_reg *crp, |
| 180 | unsigned int field, u32 value) |
| 181 | { |
| 182 | void __iomem *mapped_reg; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 183 | unsigned int pos; |
| 184 | u32 mask, data; |
| 185 | |
| 186 | sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos); |
| 187 | |
| 188 | dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, " |
| 189 | "r_width = %u, f_width = %u\n", |
| 190 | crp->reg, value, field, crp->reg_width, crp->field_width); |
| 191 | |
| 192 | mask = ~(mask << pos); |
| 193 | value = value << pos; |
| 194 | |
| 195 | data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width); |
| 196 | data &= mask; |
| 197 | data |= value; |
| 198 | |
Marek Vasut | 651d5a7 | 2021-04-27 22:03:38 +0200 | [diff] [blame] | 199 | sh_pfc_unlock_reg(pfc, crp->reg, data); |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 200 | sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data); |
| 201 | } |
| 202 | |
| 203 | static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id, |
| 204 | const struct pinmux_cfg_reg **crp, |
| 205 | unsigned int *fieldp, u32 *valuep) |
| 206 | { |
| 207 | unsigned int k = 0; |
| 208 | |
| 209 | while (1) { |
| 210 | const struct pinmux_cfg_reg *config_reg = |
| 211 | pfc->info->cfg_regs + k; |
| 212 | unsigned int r_width = config_reg->reg_width; |
| 213 | unsigned int f_width = config_reg->field_width; |
| 214 | unsigned int curr_width; |
| 215 | unsigned int bit_pos; |
| 216 | unsigned int pos = 0; |
| 217 | unsigned int m = 0; |
| 218 | |
| 219 | if (!r_width) |
| 220 | break; |
| 221 | |
| 222 | for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) { |
| 223 | u32 ncomb; |
| 224 | u32 n; |
| 225 | |
| 226 | if (f_width) |
| 227 | curr_width = f_width; |
| 228 | else |
| 229 | curr_width = config_reg->var_field_width[m]; |
| 230 | |
| 231 | ncomb = 1 << curr_width; |
| 232 | for (n = 0; n < ncomb; n++) { |
| 233 | if (config_reg->enum_ids[pos + n] == enum_id) { |
| 234 | *crp = config_reg; |
| 235 | *fieldp = m; |
| 236 | *valuep = n; |
| 237 | return 0; |
| 238 | } |
| 239 | } |
| 240 | pos += ncomb; |
| 241 | m++; |
| 242 | } |
| 243 | k++; |
| 244 | } |
| 245 | |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos, |
| 250 | u16 *enum_idp) |
| 251 | { |
| 252 | const u16 *data = pfc->info->pinmux_data; |
| 253 | unsigned int k; |
| 254 | |
| 255 | if (pos) { |
| 256 | *enum_idp = data[pos + 1]; |
| 257 | return pos + 1; |
| 258 | } |
| 259 | |
| 260 | for (k = 0; k < pfc->info->pinmux_data_size; k++) { |
| 261 | if (data[k] == mark) { |
| 262 | *enum_idp = data[k + 1]; |
| 263 | return k + 1; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n", |
| 268 | mark); |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
| 272 | int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type) |
| 273 | { |
| 274 | const struct pinmux_range *range; |
| 275 | int pos = 0; |
| 276 | |
| 277 | switch (pinmux_type) { |
| 278 | case PINMUX_TYPE_GPIO: |
| 279 | case PINMUX_TYPE_FUNCTION: |
| 280 | range = NULL; |
| 281 | break; |
| 282 | |
| 283 | case PINMUX_TYPE_OUTPUT: |
| 284 | range = &pfc->info->output; |
| 285 | break; |
| 286 | |
| 287 | case PINMUX_TYPE_INPUT: |
| 288 | range = &pfc->info->input; |
| 289 | break; |
| 290 | |
| 291 | default: |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | |
| 295 | /* Iterate over all the configuration fields we need to update. */ |
| 296 | while (1) { |
| 297 | const struct pinmux_cfg_reg *cr; |
| 298 | unsigned int field; |
| 299 | u16 enum_id; |
| 300 | u32 value; |
| 301 | int in_range; |
| 302 | int ret; |
| 303 | |
| 304 | pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id); |
| 305 | if (pos < 0) |
| 306 | return pos; |
| 307 | |
| 308 | if (!enum_id) |
| 309 | break; |
| 310 | |
| 311 | /* Check if the configuration field selects a function. If it |
| 312 | * doesn't, skip the field if it's not applicable to the |
| 313 | * requested pinmux type. |
| 314 | */ |
| 315 | in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function); |
| 316 | if (!in_range) { |
| 317 | if (pinmux_type == PINMUX_TYPE_FUNCTION) { |
| 318 | /* Functions are allowed to modify all |
| 319 | * fields. |
| 320 | */ |
| 321 | in_range = 1; |
| 322 | } else if (pinmux_type != PINMUX_TYPE_GPIO) { |
| 323 | /* Input/output types can only modify fields |
| 324 | * that correspond to their respective ranges. |
| 325 | */ |
| 326 | in_range = sh_pfc_enum_in_range(enum_id, range); |
| 327 | |
| 328 | /* |
| 329 | * special case pass through for fixed |
| 330 | * input-only or output-only pins without |
| 331 | * function enum register association. |
| 332 | */ |
| 333 | if (in_range && enum_id == range->force) |
| 334 | continue; |
| 335 | } |
| 336 | /* GPIOs are only allowed to modify function fields. */ |
| 337 | } |
| 338 | |
| 339 | if (!in_range) |
| 340 | continue; |
| 341 | |
| 342 | ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value); |
| 343 | if (ret < 0) |
| 344 | return ret; |
| 345 | |
| 346 | sh_pfc_write_config_reg(pfc, cr, field, value); |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Marek Vasut | eb13e0f | 2018-06-10 16:05:48 +0200 | [diff] [blame] | 352 | const struct pinmux_bias_reg * |
| 353 | sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin, |
| 354 | unsigned int *bit) |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 355 | { |
Marek Vasut | eb13e0f | 2018-06-10 16:05:48 +0200 | [diff] [blame] | 356 | unsigned int i, j; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 357 | |
Marek Vasut | eb13e0f | 2018-06-10 16:05:48 +0200 | [diff] [blame] | 358 | for (i = 0; pfc->info->bias_regs[i].puen; i++) { |
| 359 | for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) { |
| 360 | if (pfc->info->bias_regs[i].pins[j] == pin) { |
| 361 | *bit = j; |
| 362 | return &pfc->info->bias_regs[i]; |
| 363 | } |
| 364 | } |
| 365 | } |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 366 | |
Marek Vasut | eb13e0f | 2018-06-10 16:05:48 +0200 | [diff] [blame] | 367 | WARN_ONCE(1, "Pin %u is not in bias info list\n", pin); |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 368 | |
| 369 | return NULL; |
| 370 | } |
| 371 | |
| 372 | static int sh_pfc_init_ranges(struct sh_pfc *pfc) |
| 373 | { |
| 374 | struct sh_pfc_pin_range *range; |
| 375 | unsigned int nr_ranges; |
| 376 | unsigned int i; |
| 377 | |
| 378 | if (pfc->info->pins[0].pin == (u16)-1) { |
| 379 | /* Pin number -1 denotes that the SoC doesn't report pin numbers |
| 380 | * in its pin arrays yet. Consider the pin numbers range as |
| 381 | * continuous and allocate a single range. |
| 382 | */ |
| 383 | pfc->nr_ranges = 1; |
| 384 | pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL); |
| 385 | if (pfc->ranges == NULL) |
| 386 | return -ENOMEM; |
| 387 | |
| 388 | pfc->ranges->start = 0; |
| 389 | pfc->ranges->end = pfc->info->nr_pins - 1; |
| 390 | pfc->nr_gpio_pins = pfc->info->nr_pins; |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /* Count, allocate and fill the ranges. The PFC SoC data pins array must |
| 396 | * be sorted by pin numbers, and pins without a GPIO port must come |
| 397 | * last. |
| 398 | */ |
| 399 | for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) { |
| 400 | if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1) |
| 401 | nr_ranges++; |
| 402 | } |
| 403 | |
| 404 | pfc->nr_ranges = nr_ranges; |
| 405 | pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL); |
| 406 | if (pfc->ranges == NULL) |
| 407 | return -ENOMEM; |
| 408 | |
| 409 | range = pfc->ranges; |
| 410 | range->start = pfc->info->pins[0].pin; |
| 411 | |
| 412 | for (i = 1; i < pfc->info->nr_pins; ++i) { |
| 413 | if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1) |
| 414 | continue; |
| 415 | |
| 416 | range->end = pfc->info->pins[i-1].pin; |
| 417 | if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO)) |
| 418 | pfc->nr_gpio_pins = range->end + 1; |
| 419 | |
| 420 | range++; |
| 421 | range->start = pfc->info->pins[i].pin; |
| 422 | } |
| 423 | |
| 424 | range->end = pfc->info->pins[i-1].pin; |
| 425 | if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO)) |
| 426 | pfc->nr_gpio_pins = range->end + 1; |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev) |
| 432 | { |
| 433 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 434 | |
| 435 | return priv->pfc.info->nr_pins; |
| 436 | } |
| 437 | |
| 438 | static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev, |
| 439 | unsigned selector) |
| 440 | { |
| 441 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 442 | |
| 443 | return priv->pfc.info->pins[selector].name; |
| 444 | } |
| 445 | |
| 446 | static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev) |
| 447 | { |
| 448 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 449 | |
| 450 | return priv->pfc.info->nr_groups; |
| 451 | } |
| 452 | |
| 453 | static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev, |
| 454 | unsigned selector) |
| 455 | { |
| 456 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 457 | |
| 458 | return priv->pfc.info->groups[selector].name; |
| 459 | } |
| 460 | |
Lad Prabhakar | 23ea9b4 | 2020-11-28 13:13:09 +0000 | [diff] [blame] | 461 | static int sh_pfc_pinctrl_get_pin_muxing(struct udevice *dev, |
| 462 | unsigned int selector, |
| 463 | char *buf, int size) |
| 464 | { |
| 465 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 466 | struct sh_pfc_pinctrl *pmx = &priv->pmx; |
| 467 | struct sh_pfc *pfc = &priv->pfc; |
| 468 | struct sh_pfc_pin_config *cfg; |
| 469 | const struct sh_pfc_pin *pin; |
| 470 | int idx; |
| 471 | |
| 472 | pin = &priv->pfc.info->pins[selector]; |
| 473 | if (!pin) { |
| 474 | snprintf(buf, size, "Unknown"); |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | |
| 478 | idx = sh_pfc_get_pin_index(pfc, pin->pin); |
| 479 | cfg = &pmx->configs[idx]; |
| 480 | snprintf(buf, size, "%s", cfg->name); |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 485 | static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev) |
| 486 | { |
| 487 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 488 | |
| 489 | return priv->pfc.info->nr_functions; |
| 490 | } |
| 491 | |
| 492 | static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev, |
| 493 | unsigned selector) |
| 494 | { |
| 495 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 496 | |
| 497 | return priv->pfc.info->functions[selector].name; |
| 498 | } |
| 499 | |
Marek Vasut | 02d34f0 | 2019-04-21 22:46:25 +0200 | [diff] [blame] | 500 | static int sh_pfc_gpio_request_enable(struct udevice *dev, |
| 501 | unsigned pin_selector) |
Marek Vasut | 489d79c | 2017-11-26 18:07:29 +0100 | [diff] [blame] | 502 | { |
| 503 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 504 | struct sh_pfc_pinctrl *pmx = &priv->pmx; |
| 505 | struct sh_pfc *pfc = &priv->pfc; |
| 506 | struct sh_pfc_pin_config *cfg; |
| 507 | const struct sh_pfc_pin *pin = NULL; |
Marek Vasut | 0cc1936 | 2019-04-21 22:46:25 +0200 | [diff] [blame] | 508 | int i, ret, idx; |
Marek Vasut | 489d79c | 2017-11-26 18:07:29 +0100 | [diff] [blame] | 509 | |
titron | 09bf498 | 2019-07-22 17:45:37 +0800 | [diff] [blame] | 510 | for (i = 0; i < pfc->info->nr_pins; i++) { |
Marek Vasut | 489d79c | 2017-11-26 18:07:29 +0100 | [diff] [blame] | 511 | if (priv->pfc.info->pins[i].pin != pin_selector) |
| 512 | continue; |
| 513 | |
| 514 | pin = &priv->pfc.info->pins[i]; |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | if (!pin) |
| 519 | return -EINVAL; |
| 520 | |
| 521 | idx = sh_pfc_get_pin_index(pfc, pin->pin); |
| 522 | cfg = &pmx->configs[idx]; |
| 523 | |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 524 | if (cfg->type != PINMUX_TYPE_NONE) { |
| 525 | if (!strcmp(cfg->name, pin->name)) |
| 526 | return 0; |
| 527 | |
| 528 | dev_err(pfc->dev, "Pin already used as %s\n", |
| 529 | cfg->name); |
Marek Vasut | 489d79c | 2017-11-26 18:07:29 +0100 | [diff] [blame] | 530 | return -EBUSY; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 531 | } |
Marek Vasut | 489d79c | 2017-11-26 18:07:29 +0100 | [diff] [blame] | 532 | |
Marek Vasut | 0cc1936 | 2019-04-21 22:46:25 +0200 | [diff] [blame] | 533 | ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO); |
| 534 | if (ret) |
| 535 | return ret; |
| 536 | |
| 537 | cfg->type = PINMUX_TYPE_GPIO; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 538 | cfg->name = "gpio"; |
Marek Vasut | 0cc1936 | 2019-04-21 22:46:25 +0200 | [diff] [blame] | 539 | |
| 540 | return 0; |
Marek Vasut | 489d79c | 2017-11-26 18:07:29 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Marek Vasut | 02d34f0 | 2019-04-21 22:46:25 +0200 | [diff] [blame] | 543 | static int sh_pfc_gpio_disable_free(struct udevice *dev, |
| 544 | unsigned pin_selector) |
| 545 | { |
| 546 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 547 | struct sh_pfc_pinctrl *pmx = &priv->pmx; |
| 548 | struct sh_pfc *pfc = &priv->pfc; |
| 549 | struct sh_pfc_pin_config *cfg; |
| 550 | const struct sh_pfc_pin *pin = NULL; |
| 551 | int i, idx; |
| 552 | |
titron | 09bf498 | 2019-07-22 17:45:37 +0800 | [diff] [blame] | 553 | for (i = 0; i < pfc->info->nr_pins; i++) { |
Marek Vasut | 02d34f0 | 2019-04-21 22:46:25 +0200 | [diff] [blame] | 554 | if (priv->pfc.info->pins[i].pin != pin_selector) |
| 555 | continue; |
| 556 | |
| 557 | pin = &priv->pfc.info->pins[i]; |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | if (!pin) |
| 562 | return -EINVAL; |
| 563 | |
| 564 | idx = sh_pfc_get_pin_index(pfc, pin->pin); |
| 565 | cfg = &pmx->configs[idx]; |
| 566 | |
| 567 | cfg->type = PINMUX_TYPE_NONE; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 568 | cfg->name = "none"; |
Marek Vasut | 02d34f0 | 2019-04-21 22:46:25 +0200 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
Marek Vasut | 5e6db84 | 2017-11-26 17:42:16 +0100 | [diff] [blame] | 573 | static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector, |
| 574 | unsigned func_selector) |
| 575 | { |
| 576 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 577 | struct sh_pfc_pinctrl *pmx = &priv->pmx; |
| 578 | struct sh_pfc *pfc = &priv->pfc; |
| 579 | const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector]; |
| 580 | int idx = sh_pfc_get_pin_index(pfc, pin->pin); |
| 581 | struct sh_pfc_pin_config *cfg = &pmx->configs[idx]; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 582 | int ret; |
Marek Vasut | 5e6db84 | 2017-11-26 17:42:16 +0100 | [diff] [blame] | 583 | |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 584 | if (cfg->type != PINMUX_TYPE_NONE) { |
| 585 | if (!strcmp(cfg->name, pin->name)) |
| 586 | return 0; |
| 587 | |
| 588 | dev_err(pfc->dev, "Pin already used as %s\n", |
| 589 | cfg->name); |
Marek Vasut | 5e6db84 | 2017-11-26 17:42:16 +0100 | [diff] [blame] | 590 | return -EBUSY; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION); |
| 594 | if (ret) |
| 595 | return ret; |
Marek Vasut | 5e6db84 | 2017-11-26 17:42:16 +0100 | [diff] [blame] | 596 | |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 597 | cfg->type = PINMUX_TYPE_FUNCTION; |
| 598 | cfg->name = "function"; |
| 599 | |
| 600 | return 0; |
Marek Vasut | 5e6db84 | 2017-11-26 17:42:16 +0100 | [diff] [blame] | 601 | } |
| 602 | |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 603 | static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector, |
| 604 | unsigned func_selector) |
| 605 | { |
| 606 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 607 | struct sh_pfc_pinctrl *pmx = &priv->pmx; |
| 608 | struct sh_pfc *pfc = &priv->pfc; |
| 609 | const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector]; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 610 | bool grp_pins_configured = true; |
| 611 | struct sh_pfc_pin_config *cfg; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 612 | unsigned int i; |
| 613 | int ret = 0; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 614 | int idx; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 615 | |
| 616 | for (i = 0; i < grp->nr_pins; ++i) { |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 617 | idx = sh_pfc_get_pin_index(pfc, grp->pins[i]); |
| 618 | cfg = &pmx->configs[idx]; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 619 | |
| 620 | if (cfg->type != PINMUX_TYPE_NONE) { |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 621 | if (!strcmp(cfg->name, grp->name)) |
| 622 | continue; |
| 623 | |
| 624 | dev_err(pfc->dev, "Pin already used as %s\n", |
| 625 | cfg->name); |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 626 | ret = -EBUSY; |
| 627 | goto done; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 628 | } else { |
| 629 | grp_pins_configured = false; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 633 | if (grp_pins_configured) |
| 634 | return 0; |
| 635 | |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 636 | for (i = 0; i < grp->nr_pins; ++i) { |
| 637 | ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION); |
| 638 | if (ret < 0) |
| 639 | break; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 640 | |
| 641 | idx = sh_pfc_get_pin_index(pfc, grp->pins[i]); |
| 642 | cfg = &pmx->configs[idx]; |
| 643 | cfg->type = PINMUX_TYPE_FUNCTION; |
| 644 | cfg->name = priv->pfc.info->groups[group_selector].name; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | done: |
| 648 | return ret; |
| 649 | } |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 650 | #if CONFIG_IS_ENABLED(PINCONF) |
| 651 | static const struct pinconf_param sh_pfc_pinconf_params[] = { |
| 652 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
| 653 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
| 654 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, |
| 655 | { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 }, |
| 656 | { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 }, |
| 657 | }; |
| 658 | |
| 659 | static void __iomem * |
| 660 | sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin, |
| 661 | unsigned int *offset, unsigned int *size) |
| 662 | { |
| 663 | const struct pinmux_drive_reg_field *field; |
| 664 | const struct pinmux_drive_reg *reg; |
| 665 | unsigned int i; |
| 666 | |
| 667 | for (reg = pfc->info->drive_regs; reg->reg; ++reg) { |
| 668 | for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) { |
| 669 | field = ®->fields[i]; |
| 670 | |
| 671 | if (field->size && field->pin == pin) { |
| 672 | *offset = field->offset; |
| 673 | *size = field->size; |
| 674 | |
| 675 | return (void __iomem *)(uintptr_t)reg->reg; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return NULL; |
| 681 | } |
| 682 | |
| 683 | static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc, |
| 684 | unsigned int pin, u16 strength) |
| 685 | { |
| 686 | unsigned int offset; |
| 687 | unsigned int size; |
| 688 | unsigned int step; |
| 689 | void __iomem *reg; |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 690 | u32 val; |
| 691 | |
| 692 | reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size); |
| 693 | if (!reg) |
| 694 | return -EINVAL; |
| 695 | |
| 696 | step = size == 2 ? 6 : 3; |
| 697 | |
| 698 | if (strength < step || strength > 24) |
| 699 | return -EINVAL; |
| 700 | |
| 701 | /* Convert the value from mA based on a full drive strength value of |
| 702 | * 24mA. We can make the full value configurable later if needed. |
| 703 | */ |
| 704 | strength = strength / step - 1; |
| 705 | |
| 706 | val = sh_pfc_read_raw_reg(reg, 32); |
Marek Vasut | 0d9c810 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 707 | val &= ~GENMASK(offset + 4 - 1, offset); |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 708 | val |= strength << offset; |
| 709 | |
Marek Vasut | 651d5a7 | 2021-04-27 22:03:38 +0200 | [diff] [blame] | 710 | sh_pfc_unlock_reg(pfc, (uintptr_t)reg, val); |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 711 | sh_pfc_write_raw_reg(reg, 32, val); |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /* Check whether the requested parameter is supported for a pin. */ |
| 717 | static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin, |
| 718 | unsigned int param) |
| 719 | { |
| 720 | int idx = sh_pfc_get_pin_index(pfc, _pin); |
| 721 | const struct sh_pfc_pin *pin = &pfc->info->pins[idx]; |
| 722 | |
| 723 | switch (param) { |
| 724 | case PIN_CONFIG_BIAS_DISABLE: |
| 725 | return pin->configs & |
| 726 | (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN); |
| 727 | |
| 728 | case PIN_CONFIG_BIAS_PULL_UP: |
| 729 | return pin->configs & SH_PFC_PIN_CFG_PULL_UP; |
| 730 | |
| 731 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 732 | return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN; |
| 733 | |
| 734 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 735 | return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH; |
| 736 | |
| 737 | case PIN_CONFIG_POWER_SOURCE: |
| 738 | return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE; |
| 739 | |
| 740 | default: |
| 741 | return false; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin, |
| 746 | unsigned int param, unsigned int arg) |
| 747 | { |
| 748 | struct sh_pfc *pfc = pmx->pfc; |
| 749 | void __iomem *pocctrl; |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 750 | u32 addr, val; |
| 751 | int bit, ret; |
| 752 | |
| 753 | if (!sh_pfc_pinconf_validate(pfc, _pin, param)) |
| 754 | return -ENOTSUPP; |
| 755 | |
| 756 | switch (param) { |
| 757 | case PIN_CONFIG_BIAS_PULL_UP: |
| 758 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 759 | case PIN_CONFIG_BIAS_DISABLE: |
| 760 | if (!pfc->info->ops || !pfc->info->ops->set_bias) |
| 761 | return -ENOTSUPP; |
| 762 | |
| 763 | pfc->info->ops->set_bias(pfc, _pin, param); |
| 764 | |
| 765 | break; |
| 766 | |
| 767 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 768 | ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg); |
| 769 | if (ret < 0) |
| 770 | return ret; |
| 771 | |
| 772 | break; |
| 773 | |
| 774 | case PIN_CONFIG_POWER_SOURCE: |
| 775 | if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl) |
| 776 | return -ENOTSUPP; |
| 777 | |
| 778 | bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr); |
| 779 | if (bit < 0) { |
| 780 | printf("invalid pin %#x", _pin); |
| 781 | return bit; |
| 782 | } |
| 783 | |
| 784 | if (arg != 1800 && arg != 3300) |
| 785 | return -EINVAL; |
| 786 | |
| 787 | pocctrl = (void __iomem *)(uintptr_t)addr; |
| 788 | |
| 789 | val = sh_pfc_read_raw_reg(pocctrl, 32); |
| 790 | if (arg == 3300) |
| 791 | val |= BIT(bit); |
| 792 | else |
| 793 | val &= ~BIT(bit); |
| 794 | |
Marek Vasut | 651d5a7 | 2021-04-27 22:03:38 +0200 | [diff] [blame] | 795 | sh_pfc_unlock_reg(pfc, addr, val); |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 796 | sh_pfc_write_raw_reg(pocctrl, 32, val); |
| 797 | |
| 798 | break; |
| 799 | |
| 800 | default: |
| 801 | return -ENOTSUPP; |
| 802 | } |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
Marek Vasut | 5e6db84 | 2017-11-26 17:42:16 +0100 | [diff] [blame] | 807 | static int sh_pfc_pinconf_pin_set(struct udevice *dev, |
| 808 | unsigned int pin_selector, |
| 809 | unsigned int param, unsigned int arg) |
| 810 | { |
| 811 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 812 | struct sh_pfc_pinctrl *pmx = &priv->pmx; |
| 813 | struct sh_pfc *pfc = &priv->pfc; |
| 814 | const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector]; |
| 815 | |
| 816 | sh_pfc_pinconf_set(pmx, pin->pin, param, arg); |
| 817 | |
| 818 | return 0; |
| 819 | } |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 820 | |
| 821 | static int sh_pfc_pinconf_group_set(struct udevice *dev, |
| 822 | unsigned int group_selector, |
| 823 | unsigned int param, unsigned int arg) |
| 824 | { |
| 825 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 826 | struct sh_pfc_pinctrl *pmx = &priv->pmx; |
| 827 | struct sh_pfc *pfc = &priv->pfc; |
| 828 | const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector]; |
| 829 | unsigned int i; |
| 830 | |
| 831 | for (i = 0; i < grp->nr_pins; i++) |
| 832 | sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg); |
| 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | #endif |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 837 | |
| 838 | static struct pinctrl_ops sh_pfc_pinctrl_ops = { |
| 839 | .get_pins_count = sh_pfc_pinctrl_get_pins_count, |
| 840 | .get_pin_name = sh_pfc_pinctrl_get_pin_name, |
| 841 | .get_groups_count = sh_pfc_pinctrl_get_groups_count, |
| 842 | .get_group_name = sh_pfc_pinctrl_get_group_name, |
Lad Prabhakar | 23ea9b4 | 2020-11-28 13:13:09 +0000 | [diff] [blame] | 843 | .get_pin_muxing = sh_pfc_pinctrl_get_pin_muxing, |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 844 | .get_functions_count = sh_pfc_pinctrl_get_functions_count, |
| 845 | .get_function_name = sh_pfc_pinctrl_get_function_name, |
| 846 | |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 847 | #if CONFIG_IS_ENABLED(PINCONF) |
| 848 | .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params), |
| 849 | .pinconf_params = sh_pfc_pinconf_params, |
Marek Vasut | 5e6db84 | 2017-11-26 17:42:16 +0100 | [diff] [blame] | 850 | .pinconf_set = sh_pfc_pinconf_pin_set, |
Marek Vasut | c9dd9ae | 2017-09-28 00:56:24 +0200 | [diff] [blame] | 851 | .pinconf_group_set = sh_pfc_pinconf_group_set, |
| 852 | #endif |
Marek Vasut | 5e6db84 | 2017-11-26 17:42:16 +0100 | [diff] [blame] | 853 | .pinmux_set = sh_pfc_pinctrl_pin_set, |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 854 | .pinmux_group_set = sh_pfc_pinctrl_group_set, |
| 855 | .set_state = pinctrl_generic_set_state, |
Marek Vasut | 02d34f0 | 2019-04-21 22:46:25 +0200 | [diff] [blame] | 856 | |
| 857 | .gpio_request_enable = sh_pfc_gpio_request_enable, |
| 858 | .gpio_disable_free = sh_pfc_gpio_disable_free, |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 859 | }; |
| 860 | |
| 861 | static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx) |
| 862 | { |
| 863 | unsigned int i; |
| 864 | |
| 865 | /* Allocate and initialize the pins and configs arrays. */ |
| 866 | pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins, |
| 867 | GFP_KERNEL); |
| 868 | if (unlikely(!pmx->configs)) |
| 869 | return -ENOMEM; |
| 870 | |
| 871 | for (i = 0; i < pfc->info->nr_pins; ++i) { |
| 872 | struct sh_pfc_pin_config *cfg = &pmx->configs[i]; |
| 873 | cfg->type = PINMUX_TYPE_NONE; |
Lad Prabhakar | 13ed8d5 | 2020-11-28 13:13:08 +0000 | [diff] [blame] | 874 | cfg->name = "none"; |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | |
| 881 | static int sh_pfc_pinctrl_probe(struct udevice *dev) |
| 882 | { |
| 883 | struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev); |
| 884 | enum sh_pfc_model model = dev_get_driver_data(dev); |
| 885 | fdt_addr_t base; |
| 886 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 887 | base = dev_read_addr(dev); |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 888 | if (base == FDT_ADDR_T_NONE) |
| 889 | return -EINVAL; |
| 890 | |
| 891 | priv->pfc.regs = devm_ioremap(dev, base, SZ_2K); |
| 892 | if (!priv->pfc.regs) |
| 893 | return -ENOMEM; |
| 894 | |
Marek Vasut | c40f2d6 | 2018-01-17 22:18:59 +0100 | [diff] [blame] | 895 | #ifdef CONFIG_PINCTRL_PFC_R8A7790 |
| 896 | if (model == SH_PFC_R8A7790) |
| 897 | priv->pfc.info = &r8a7790_pinmux_info; |
| 898 | #endif |
Marek Vasut | 06ef9e8 | 2018-01-17 17:14:45 +0100 | [diff] [blame] | 899 | #ifdef CONFIG_PINCTRL_PFC_R8A7791 |
| 900 | if (model == SH_PFC_R8A7791) |
| 901 | priv->pfc.info = &r8a7791_pinmux_info; |
| 902 | #endif |
Marek Vasut | 1ef3930 | 2018-01-17 22:29:50 +0100 | [diff] [blame] | 903 | #ifdef CONFIG_PINCTRL_PFC_R8A7792 |
| 904 | if (model == SH_PFC_R8A7792) |
| 905 | priv->pfc.info = &r8a7792_pinmux_info; |
| 906 | #endif |
Marek Vasut | 06ef9e8 | 2018-01-17 17:14:45 +0100 | [diff] [blame] | 907 | #ifdef CONFIG_PINCTRL_PFC_R8A7793 |
| 908 | if (model == SH_PFC_R8A7793) |
| 909 | priv->pfc.info = &r8a7793_pinmux_info; |
| 910 | #endif |
Marek Vasut | 4dd88d5 | 2018-01-17 22:33:59 +0100 | [diff] [blame] | 911 | #ifdef CONFIG_PINCTRL_PFC_R8A7794 |
| 912 | if (model == SH_PFC_R8A7794) |
| 913 | priv->pfc.info = &r8a7794_pinmux_info; |
| 914 | #endif |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 915 | #ifdef CONFIG_PINCTRL_PFC_R8A7795 |
| 916 | if (model == SH_PFC_R8A7795) |
| 917 | priv->pfc.info = &r8a7795_pinmux_info; |
| 918 | #endif |
| 919 | #ifdef CONFIG_PINCTRL_PFC_R8A7796 |
| 920 | if (model == SH_PFC_R8A7796) |
| 921 | priv->pfc.info = &r8a7796_pinmux_info; |
| 922 | #endif |
Adam Ford | 96980fb | 2020-06-30 09:30:09 -0500 | [diff] [blame] | 923 | #ifdef CONFIG_PINCTRL_PFC_R8A774A1 |
| 924 | if (model == SH_PFC_R8A774A1) |
| 925 | priv->pfc.info = &r8a774a1_pinmux_info; |
| 926 | #endif |
Biju Das | d1d7888 | 2020-10-28 10:34:21 +0000 | [diff] [blame] | 927 | #ifdef CONFIG_PINCTRL_PFC_R8A774B1 |
| 928 | if (model == SH_PFC_R8A774B1) |
| 929 | priv->pfc.info = &r8a774b1_pinmux_info; |
| 930 | #endif |
Lad Prabhakar | 53b88b9 | 2021-03-15 22:24:04 +0000 | [diff] [blame] | 931 | #ifdef CONFIG_PINCTRL_PFC_R8A774C0 |
| 932 | if (model == SH_PFC_R8A774C0) |
| 933 | priv->pfc.info = &r8a774c0_pinmux_info; |
| 934 | #endif |
Biju Das | 121bd00 | 2020-10-28 10:34:22 +0000 | [diff] [blame] | 935 | #ifdef CONFIG_PINCTRL_PFC_R8A774E1 |
| 936 | if (model == SH_PFC_R8A774E1) |
| 937 | priv->pfc.info = &r8a774e1_pinmux_info; |
| 938 | #endif |
Marek Vasut | 72269e0 | 2019-03-04 01:32:44 +0100 | [diff] [blame] | 939 | #ifdef CONFIG_PINCTRL_PFC_R8A77965 |
| 940 | if (model == SH_PFC_R8A77965) |
| 941 | priv->pfc.info = &r8a77965_pinmux_info; |
| 942 | #endif |
Marek Vasut | a0e11e5 | 2017-10-09 20:57:29 +0200 | [diff] [blame] | 943 | #ifdef CONFIG_PINCTRL_PFC_R8A77970 |
| 944 | if (model == SH_PFC_R8A77970) |
| 945 | priv->pfc.info = &r8a77970_pinmux_info; |
| 946 | #endif |
Marek Vasut | a6a7f48 | 2019-07-29 19:59:44 +0200 | [diff] [blame] | 947 | #ifdef CONFIG_PINCTRL_PFC_R8A77980 |
| 948 | if (model == SH_PFC_R8A77980) |
| 949 | priv->pfc.info = &r8a77980_pinmux_info; |
| 950 | #endif |
Marek Vasut | 68a7704 | 2018-04-26 13:09:20 +0200 | [diff] [blame] | 951 | #ifdef CONFIG_PINCTRL_PFC_R8A77990 |
| 952 | if (model == SH_PFC_R8A77990) |
| 953 | priv->pfc.info = &r8a77990_pinmux_info; |
| 954 | #endif |
Marek Vasut | 7d35e64 | 2017-10-08 20:57:37 +0200 | [diff] [blame] | 955 | #ifdef CONFIG_PINCTRL_PFC_R8A77995 |
| 956 | if (model == SH_PFC_R8A77995) |
| 957 | priv->pfc.info = &r8a77995_pinmux_info; |
| 958 | #endif |
Marek Vasut | 4dbc653 | 2021-04-27 01:55:54 +0200 | [diff] [blame] | 959 | #ifdef CONFIG_PINCTRL_PFC_R8A779A0 |
| 960 | if (model == SH_PFC_R8A779A0) |
| 961 | priv->pfc.info = &r8a779a0_pinmux_info; |
| 962 | #endif |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 963 | |
| 964 | priv->pmx.pfc = &priv->pfc; |
| 965 | sh_pfc_init_ranges(&priv->pfc); |
| 966 | sh_pfc_map_pins(&priv->pfc, &priv->pmx); |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | static const struct udevice_id sh_pfc_pinctrl_ids[] = { |
Marek Vasut | c40f2d6 | 2018-01-17 22:18:59 +0100 | [diff] [blame] | 972 | #ifdef CONFIG_PINCTRL_PFC_R8A7790 |
| 973 | { |
| 974 | .compatible = "renesas,pfc-r8a7790", |
| 975 | .data = SH_PFC_R8A7790, |
| 976 | }, |
| 977 | #endif |
Marek Vasut | 06ef9e8 | 2018-01-17 17:14:45 +0100 | [diff] [blame] | 978 | #ifdef CONFIG_PINCTRL_PFC_R8A7791 |
| 979 | { |
| 980 | .compatible = "renesas,pfc-r8a7791", |
| 981 | .data = SH_PFC_R8A7791, |
| 982 | }, |
| 983 | #endif |
Marek Vasut | 1ef3930 | 2018-01-17 22:29:50 +0100 | [diff] [blame] | 984 | #ifdef CONFIG_PINCTRL_PFC_R8A7792 |
| 985 | { |
| 986 | .compatible = "renesas,pfc-r8a7792", |
| 987 | .data = SH_PFC_R8A7792, |
| 988 | }, |
| 989 | #endif |
Marek Vasut | 06ef9e8 | 2018-01-17 17:14:45 +0100 | [diff] [blame] | 990 | #ifdef CONFIG_PINCTRL_PFC_R8A7793 |
| 991 | { |
| 992 | .compatible = "renesas,pfc-r8a7793", |
| 993 | .data = SH_PFC_R8A7793, |
| 994 | }, |
| 995 | #endif |
Marek Vasut | 4dd88d5 | 2018-01-17 22:33:59 +0100 | [diff] [blame] | 996 | #ifdef CONFIG_PINCTRL_PFC_R8A7794 |
| 997 | { |
| 998 | .compatible = "renesas,pfc-r8a7794", |
| 999 | .data = SH_PFC_R8A7794, |
| 1000 | }, |
| 1001 | #endif |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 1002 | #ifdef CONFIG_PINCTRL_PFC_R8A7795 |
| 1003 | { |
| 1004 | .compatible = "renesas,pfc-r8a7795", |
| 1005 | .data = SH_PFC_R8A7795, |
| 1006 | }, |
| 1007 | #endif |
| 1008 | #ifdef CONFIG_PINCTRL_PFC_R8A7796 |
| 1009 | { |
| 1010 | .compatible = "renesas,pfc-r8a7796", |
| 1011 | .data = SH_PFC_R8A7796, |
Marek Vasut | 72269e0 | 2019-03-04 01:32:44 +0100 | [diff] [blame] | 1012 | }, |
| 1013 | #endif |
Adam Ford | 96980fb | 2020-06-30 09:30:09 -0500 | [diff] [blame] | 1014 | #ifdef CONFIG_PINCTRL_PFC_R8A774A1 |
| 1015 | { |
| 1016 | .compatible = "renesas,pfc-r8a774a1", |
| 1017 | .data = SH_PFC_R8A774A1, |
| 1018 | }, |
| 1019 | #endif |
Biju Das | d1d7888 | 2020-10-28 10:34:21 +0000 | [diff] [blame] | 1020 | #ifdef CONFIG_PINCTRL_PFC_R8A774B1 |
| 1021 | { |
| 1022 | .compatible = "renesas,pfc-r8a774b1", |
| 1023 | .data = SH_PFC_R8A774B1, |
| 1024 | }, |
| 1025 | #endif |
Lad Prabhakar | 53b88b9 | 2021-03-15 22:24:04 +0000 | [diff] [blame] | 1026 | #ifdef CONFIG_PINCTRL_PFC_R8A774C0 |
| 1027 | { |
| 1028 | .compatible = "renesas,pfc-r8a774c0", |
| 1029 | .data = SH_PFC_R8A774C0, |
| 1030 | }, |
| 1031 | #endif |
Biju Das | 121bd00 | 2020-10-28 10:34:22 +0000 | [diff] [blame] | 1032 | #ifdef CONFIG_PINCTRL_PFC_R8A774E1 |
| 1033 | { |
| 1034 | .compatible = "renesas,pfc-r8a774e1", |
| 1035 | .data = SH_PFC_R8A774E1, |
| 1036 | }, |
| 1037 | #endif |
Marek Vasut | 72269e0 | 2019-03-04 01:32:44 +0100 | [diff] [blame] | 1038 | #ifdef CONFIG_PINCTRL_PFC_R8A77965 |
| 1039 | { |
Marek Vasut | 20d721e | 2018-02-26 10:35:15 +0100 | [diff] [blame] | 1040 | .compatible = "renesas,pfc-r8a77965", |
Marek Vasut | 72269e0 | 2019-03-04 01:32:44 +0100 | [diff] [blame] | 1041 | .data = SH_PFC_R8A77965, |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 1042 | }, |
| 1043 | #endif |
Marek Vasut | a0e11e5 | 2017-10-09 20:57:29 +0200 | [diff] [blame] | 1044 | #ifdef CONFIG_PINCTRL_PFC_R8A77970 |
| 1045 | { |
| 1046 | .compatible = "renesas,pfc-r8a77970", |
| 1047 | .data = SH_PFC_R8A77970, |
| 1048 | }, |
| 1049 | #endif |
Marek Vasut | a6a7f48 | 2019-07-29 19:59:44 +0200 | [diff] [blame] | 1050 | #ifdef CONFIG_PINCTRL_PFC_R8A77980 |
| 1051 | { |
| 1052 | .compatible = "renesas,pfc-r8a77980", |
| 1053 | .data = SH_PFC_R8A77980, |
| 1054 | }, |
| 1055 | #endif |
Marek Vasut | 68a7704 | 2018-04-26 13:09:20 +0200 | [diff] [blame] | 1056 | #ifdef CONFIG_PINCTRL_PFC_R8A77990 |
| 1057 | { |
| 1058 | .compatible = "renesas,pfc-r8a77990", |
| 1059 | .data = SH_PFC_R8A77990, |
| 1060 | }, |
| 1061 | #endif |
Marek Vasut | 7d35e64 | 2017-10-08 20:57:37 +0200 | [diff] [blame] | 1062 | #ifdef CONFIG_PINCTRL_PFC_R8A77995 |
| 1063 | { |
| 1064 | .compatible = "renesas,pfc-r8a77995", |
| 1065 | .data = SH_PFC_R8A77995, |
| 1066 | }, |
| 1067 | #endif |
Marek Vasut | 4dbc653 | 2021-04-27 01:55:54 +0200 | [diff] [blame] | 1068 | #ifdef CONFIG_PINCTRL_PFC_R8A779A0 |
| 1069 | { |
| 1070 | .compatible = "renesas,pfc-r8a779a0", |
| 1071 | .data = SH_PFC_R8A779A0, |
| 1072 | }, |
| 1073 | #endif |
| 1074 | |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 1075 | { }, |
| 1076 | }; |
| 1077 | |
| 1078 | U_BOOT_DRIVER(pinctrl_sh_pfc) = { |
| 1079 | .name = "sh_pfc_pinctrl", |
| 1080 | .id = UCLASS_PINCTRL, |
| 1081 | .of_match = sh_pfc_pinctrl_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1082 | .priv_auto = sizeof(struct sh_pfc_pinctrl_priv), |
Marek Vasut | 3066a06 | 2017-09-15 21:13:55 +0200 | [diff] [blame] | 1083 | .ops = &sh_pfc_pinctrl_ops, |
| 1084 | .probe = sh_pfc_pinctrl_probe, |
| 1085 | }; |