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