rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * MediaTek Pinctrl Paris Driver, which implement the vendor per-pin |
| 4 | * bindings for MediaTek SoC. |
| 5 | * |
| 6 | * Copyright (C) 2018 MediaTek Inc. |
| 7 | * Author: Sean Wang <sean.wang@mediatek.com> |
| 8 | * Zhiyong Tao <zhiyong.tao@mediatek.com> |
| 9 | * Hongzhou.Yang <hongzhou.yang@mediatek.com> |
| 10 | */ |
| 11 | |
| 12 | #include <linux/gpio/driver.h> |
| 13 | #include <dt-bindings/pinctrl/mt65xx.h> |
| 14 | #include "pinctrl-paris.h" |
| 15 | |
| 16 | #include "pinctrl-mtk-common-v2_debug.h" |
| 17 | |
| 18 | #define PINCTRL_PINCTRL_DEV KBUILD_MODNAME |
| 19 | |
| 20 | /* Custom pinconf parameters */ |
| 21 | #define MTK_PIN_CONFIG_TDSEL (PIN_CONFIG_END + 1) |
| 22 | #define MTK_PIN_CONFIG_RDSEL (PIN_CONFIG_END + 2) |
| 23 | #define MTK_PIN_CONFIG_PU_ADV (PIN_CONFIG_END + 3) |
| 24 | #define MTK_PIN_CONFIG_PD_ADV (PIN_CONFIG_END + 4) |
| 25 | |
| 26 | static const struct pinconf_generic_params mtk_custom_bindings[] = { |
| 27 | {"mediatek,tdsel", MTK_PIN_CONFIG_TDSEL, 0}, |
| 28 | {"mediatek,rdsel", MTK_PIN_CONFIG_RDSEL, 0}, |
| 29 | {"mediatek,pull-up-adv", MTK_PIN_CONFIG_PU_ADV, 1}, |
| 30 | {"mediatek,pull-down-adv", MTK_PIN_CONFIG_PD_ADV, 1}, |
| 31 | }; |
| 32 | |
| 33 | #ifdef CONFIG_DEBUG_FS |
| 34 | static const struct pin_config_item mtk_conf_items[] = { |
| 35 | PCONFDUMP(MTK_PIN_CONFIG_TDSEL, "tdsel", NULL, true), |
| 36 | PCONFDUMP(MTK_PIN_CONFIG_RDSEL, "rdsel", NULL, true), |
| 37 | PCONFDUMP(MTK_PIN_CONFIG_PU_ADV, "pu-adv", NULL, true), |
| 38 | PCONFDUMP(MTK_PIN_CONFIG_PD_ADV, "pd-adv", NULL, true), |
| 39 | }; |
| 40 | #endif |
| 41 | |
| 42 | static const char * const mtk_gpio_functions[] = { |
| 43 | "func0", "func1", "func2", "func3", |
| 44 | "func4", "func5", "func6", "func7", |
| 45 | "func8", "func9", "func10", "func11", |
| 46 | "func12", "func13", "func14", "func15", |
| 47 | }; |
| 48 | |
| 49 | static int mtk_pinmux_gpio_request_enable(struct pinctrl_dev *pctldev, |
| 50 | struct pinctrl_gpio_range *range, |
| 51 | unsigned int pin) |
| 52 | { |
| 53 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 54 | const struct mtk_pin_desc *desc; |
| 55 | |
| 56 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin]; |
| 57 | |
| 58 | return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE, |
| 59 | hw->soc->gpio_m); |
| 60 | } |
| 61 | |
| 62 | static int mtk_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 63 | struct pinctrl_gpio_range *range, |
| 64 | unsigned int pin, bool input) |
| 65 | { |
| 66 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 67 | const struct mtk_pin_desc *desc; |
| 68 | |
| 69 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin]; |
| 70 | |
| 71 | /* hardware would take 0 as input direction */ |
| 72 | return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, !input); |
| 73 | } |
| 74 | |
| 75 | static int mtk_pinconf_get(struct pinctrl_dev *pctldev, |
| 76 | unsigned int pin, unsigned long *config) |
| 77 | { |
| 78 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 79 | u32 param = pinconf_to_config_param(*config); |
| 80 | int val, val2, err, reg, ret = 1; |
| 81 | const struct mtk_pin_desc *desc; |
| 82 | |
| 83 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin]; |
| 84 | |
| 85 | switch (param) { |
| 86 | case PIN_CONFIG_BIAS_DISABLE: |
| 87 | if (hw->soc->bias_disable_get) { |
| 88 | err = hw->soc->bias_disable_get(hw, desc, &ret); |
| 89 | if (err) |
| 90 | return err; |
| 91 | } else { |
| 92 | return -ENOTSUPP; |
| 93 | } |
| 94 | break; |
| 95 | case PIN_CONFIG_BIAS_PULL_UP: |
| 96 | if (hw->soc->bias_get) { |
| 97 | err = hw->soc->bias_get(hw, desc, 1, &ret); |
| 98 | if (err) |
| 99 | return err; |
| 100 | } else { |
| 101 | return -ENOTSUPP; |
| 102 | } |
| 103 | break; |
| 104 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 105 | if (hw->soc->bias_get) { |
| 106 | err = hw->soc->bias_get(hw, desc, 0, &ret); |
| 107 | if (err) |
| 108 | return err; |
| 109 | } else { |
| 110 | return -ENOTSUPP; |
| 111 | } |
| 112 | break; |
| 113 | case PIN_CONFIG_SLEW_RATE: |
| 114 | err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_SR, &val); |
| 115 | if (err) |
| 116 | return err; |
| 117 | |
| 118 | if (!val) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | break; |
| 122 | case PIN_CONFIG_INPUT_ENABLE: |
| 123 | case PIN_CONFIG_OUTPUT_ENABLE: |
| 124 | err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &val); |
| 125 | if (err) |
| 126 | return err; |
| 127 | |
| 128 | /* HW takes input mode as zero; output mode as non-zero */ |
| 129 | if ((val && param == PIN_CONFIG_INPUT_ENABLE) || |
| 130 | (!val && param == PIN_CONFIG_OUTPUT_ENABLE)) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | break; |
| 134 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 135 | err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &val); |
| 136 | if (err) |
| 137 | return err; |
| 138 | |
| 139 | err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_SMT, &val2); |
| 140 | if (err) |
| 141 | return err; |
| 142 | |
| 143 | if (val || !val2) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | break; |
| 147 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 148 | if (hw->soc->drive_get) { |
| 149 | err = hw->soc->drive_get(hw, desc, &ret); |
| 150 | if (err) |
| 151 | return err; |
| 152 | } else { |
| 153 | err = -ENOTSUPP; |
| 154 | } |
| 155 | break; |
| 156 | case MTK_PIN_CONFIG_TDSEL: |
| 157 | case MTK_PIN_CONFIG_RDSEL: |
| 158 | reg = (param == MTK_PIN_CONFIG_TDSEL) ? |
| 159 | PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL; |
| 160 | |
| 161 | err = mtk_hw_get_value(hw, desc, reg, &val); |
| 162 | if (err) |
| 163 | return err; |
| 164 | |
| 165 | ret = val; |
| 166 | |
| 167 | break; |
| 168 | case MTK_PIN_CONFIG_PU_ADV: |
| 169 | case MTK_PIN_CONFIG_PD_ADV: |
| 170 | if (hw->soc->adv_pull_get) { |
| 171 | bool pullup; |
| 172 | |
| 173 | pullup = param == MTK_PIN_CONFIG_PU_ADV; |
| 174 | err = hw->soc->adv_pull_get(hw, desc, pullup, &ret); |
| 175 | if (err) |
| 176 | return err; |
| 177 | } else { |
| 178 | return -ENOTSUPP; |
| 179 | } |
| 180 | break; |
| 181 | default: |
| 182 | return -ENOTSUPP; |
| 183 | } |
| 184 | |
| 185 | *config = pinconf_to_config_packed(param, ret); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int mtk_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, |
| 191 | enum pin_config_param param, |
| 192 | enum pin_config_param arg) |
| 193 | { |
| 194 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 195 | const struct mtk_pin_desc *desc; |
| 196 | int err = 0; |
| 197 | u32 reg; |
| 198 | |
| 199 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin]; |
| 200 | |
| 201 | switch ((u32)param) { |
| 202 | case PIN_CONFIG_BIAS_DISABLE: |
| 203 | if (hw->soc->bias_disable_set) { |
| 204 | err = hw->soc->bias_disable_set(hw, desc); |
| 205 | if (err) |
| 206 | return err; |
| 207 | } else { |
| 208 | return -ENOTSUPP; |
| 209 | } |
| 210 | break; |
| 211 | case PIN_CONFIG_BIAS_PULL_UP: |
| 212 | if (hw->soc->bias_set) { |
| 213 | err = hw->soc->bias_set(hw, desc, 1); |
| 214 | if (err) |
| 215 | return err; |
| 216 | } else { |
| 217 | return -ENOTSUPP; |
| 218 | } |
| 219 | break; |
| 220 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 221 | if (hw->soc->bias_set) { |
| 222 | err = hw->soc->bias_set(hw, desc, 0); |
| 223 | if (err) |
| 224 | return err; |
| 225 | } else { |
| 226 | return -ENOTSUPP; |
| 227 | } |
| 228 | break; |
| 229 | case PIN_CONFIG_OUTPUT_ENABLE: |
| 230 | err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, |
| 231 | MTK_DISABLE); |
| 232 | if (err) |
| 233 | goto err; |
| 234 | |
| 235 | err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, |
| 236 | MTK_OUTPUT); |
| 237 | if (err) |
| 238 | goto err; |
| 239 | break; |
| 240 | case PIN_CONFIG_INPUT_ENABLE: |
| 241 | if (hw->soc->ies_present) { |
| 242 | mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_IES, |
| 243 | MTK_ENABLE); |
| 244 | } |
| 245 | |
| 246 | err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, |
| 247 | MTK_INPUT); |
| 248 | if (err) |
| 249 | goto err; |
| 250 | break; |
| 251 | case PIN_CONFIG_SLEW_RATE: |
| 252 | err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SR, |
| 253 | arg); |
| 254 | if (err) |
| 255 | goto err; |
| 256 | |
| 257 | break; |
| 258 | case PIN_CONFIG_OUTPUT: |
| 259 | err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, |
| 260 | MTK_OUTPUT); |
| 261 | if (err) |
| 262 | goto err; |
| 263 | |
| 264 | err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DO, |
| 265 | arg); |
| 266 | if (err) |
| 267 | goto err; |
| 268 | break; |
| 269 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 270 | /* arg = 1: Input mode & SMT enable ; |
| 271 | * arg = 0: Output mode & SMT disable |
| 272 | */ |
| 273 | arg = arg ? 2 : 1; |
| 274 | err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, |
| 275 | arg & 1); |
| 276 | if (err) |
| 277 | goto err; |
| 278 | |
| 279 | err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, |
| 280 | !!(arg & 2)); |
| 281 | if (err) |
| 282 | goto err; |
| 283 | break; |
| 284 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 285 | if (hw->soc->drive_set) { |
| 286 | err = hw->soc->drive_set(hw, desc, arg); |
| 287 | if (err) |
| 288 | return err; |
| 289 | } else { |
| 290 | return -ENOTSUPP; |
| 291 | } |
| 292 | break; |
| 293 | case MTK_PIN_CONFIG_TDSEL: |
| 294 | case MTK_PIN_CONFIG_RDSEL: |
| 295 | reg = (param == MTK_PIN_CONFIG_TDSEL) ? |
| 296 | PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL; |
| 297 | |
| 298 | err = mtk_hw_set_value(hw, desc, reg, arg); |
| 299 | if (err) |
| 300 | goto err; |
| 301 | break; |
| 302 | case MTK_PIN_CONFIG_PU_ADV: |
| 303 | case MTK_PIN_CONFIG_PD_ADV: |
| 304 | if (hw->soc->adv_pull_set) { |
| 305 | bool pullup; |
| 306 | |
| 307 | pullup = param == MTK_PIN_CONFIG_PU_ADV; |
| 308 | err = hw->soc->adv_pull_set(hw, desc, pullup, |
| 309 | arg); |
| 310 | if (err) |
| 311 | return err; |
| 312 | } else { |
| 313 | return -ENOTSUPP; |
| 314 | } |
| 315 | break; |
| 316 | default: |
| 317 | err = -ENOTSUPP; |
| 318 | } |
| 319 | |
| 320 | err: |
| 321 | return err; |
| 322 | } |
| 323 | |
| 324 | static struct mtk_pinctrl_group * |
| 325 | mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *hw, u32 pin) |
| 326 | { |
| 327 | int i; |
| 328 | |
| 329 | for (i = 0; i < hw->soc->ngrps; i++) { |
| 330 | struct mtk_pinctrl_group *grp = hw->groups + i; |
| 331 | |
| 332 | if (grp->pin == pin) |
| 333 | return grp; |
| 334 | } |
| 335 | |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | static const struct mtk_func_desc * |
| 340 | mtk_pctrl_find_function_by_pin(struct mtk_pinctrl *hw, u32 pin_num, u32 fnum) |
| 341 | { |
| 342 | const struct mtk_pin_desc *pin = hw->soc->pins + pin_num; |
| 343 | const struct mtk_func_desc *func = pin->funcs; |
| 344 | |
| 345 | while (func && func->name) { |
| 346 | if (func->muxval == fnum) |
| 347 | return func; |
| 348 | func++; |
| 349 | } |
| 350 | |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *hw, u32 pin_num, |
| 355 | u32 fnum) |
| 356 | { |
| 357 | int i; |
| 358 | |
| 359 | for (i = 0; i < hw->soc->npins; i++) { |
| 360 | const struct mtk_pin_desc *pin = hw->soc->pins + i; |
| 361 | |
| 362 | if (pin->number == pin_num) { |
| 363 | const struct mtk_func_desc *func = pin->funcs; |
| 364 | |
| 365 | while (func && func->name) { |
| 366 | if (func->muxval == fnum) |
| 367 | return true; |
| 368 | func++; |
| 369 | } |
| 370 | |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl, |
| 379 | u32 pin, u32 fnum, |
| 380 | struct mtk_pinctrl_group *grp, |
| 381 | struct pinctrl_map **map, |
| 382 | unsigned int *reserved_maps, |
| 383 | unsigned int *num_maps) |
| 384 | { |
| 385 | bool ret; |
| 386 | |
| 387 | if (*num_maps == *reserved_maps) |
| 388 | return -ENOSPC; |
| 389 | |
| 390 | (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP; |
| 391 | (*map)[*num_maps].data.mux.group = grp->name; |
| 392 | |
| 393 | ret = mtk_pctrl_is_function_valid(pctl, pin, fnum); |
| 394 | if (!ret) { |
| 395 | dev_err(pctl->dev, "invalid function %d on pin %d .\n", |
| 396 | fnum, pin); |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum]; |
| 401 | (*num_maps)++; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev, |
| 407 | struct device_node *node, |
| 408 | struct pinctrl_map **map, |
| 409 | unsigned int *reserved_maps, |
| 410 | unsigned int *num_maps) |
| 411 | { |
| 412 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 413 | int num_pins, num_funcs, maps_per_pin, i, err; |
| 414 | struct mtk_pinctrl_group *grp; |
| 415 | unsigned int num_configs; |
| 416 | bool has_config = false; |
| 417 | unsigned long *configs; |
| 418 | u32 pinfunc, pin, func; |
| 419 | struct property *pins; |
| 420 | unsigned int reserve = 0; |
| 421 | |
| 422 | pins = of_find_property(node, "pinmux", NULL); |
| 423 | if (!pins) { |
| 424 | dev_err(hw->dev, "missing pins property in node %pOFn .\n", |
| 425 | node); |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | |
| 429 | err = pinconf_generic_parse_dt_config(node, pctldev, &configs, |
| 430 | &num_configs); |
| 431 | if (err) |
| 432 | return err; |
| 433 | |
| 434 | if (num_configs) |
| 435 | has_config = true; |
| 436 | |
| 437 | num_pins = pins->length / sizeof(u32); |
| 438 | num_funcs = num_pins; |
| 439 | maps_per_pin = 0; |
| 440 | if (num_funcs) |
| 441 | maps_per_pin++; |
| 442 | if (has_config && num_pins >= 1) |
| 443 | maps_per_pin++; |
| 444 | |
| 445 | if (!num_pins || !maps_per_pin) { |
| 446 | err = -EINVAL; |
| 447 | goto exit; |
| 448 | } |
| 449 | |
| 450 | reserve = num_pins * maps_per_pin; |
| 451 | |
| 452 | err = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps, |
| 453 | reserve); |
| 454 | if (err < 0) |
| 455 | goto exit; |
| 456 | |
| 457 | for (i = 0; i < num_pins; i++) { |
| 458 | err = of_property_read_u32_index(node, "pinmux", i, &pinfunc); |
| 459 | if (err) |
| 460 | goto exit; |
| 461 | |
| 462 | pin = MTK_GET_PIN_NO(pinfunc); |
| 463 | func = MTK_GET_PIN_FUNC(pinfunc); |
| 464 | |
| 465 | if (pin >= hw->soc->npins || |
| 466 | func >= ARRAY_SIZE(mtk_gpio_functions)) { |
| 467 | dev_err(hw->dev, "invalid pins value.\n"); |
| 468 | err = -EINVAL; |
| 469 | goto exit; |
| 470 | } |
| 471 | |
| 472 | grp = mtk_pctrl_find_group_by_pin(hw, pin); |
| 473 | if (!grp) { |
| 474 | dev_err(hw->dev, "unable to match pin %d to group\n", |
| 475 | pin); |
| 476 | err = -EINVAL; |
| 477 | goto exit; |
| 478 | } |
| 479 | |
| 480 | err = mtk_pctrl_dt_node_to_map_func(hw, pin, func, grp, map, |
| 481 | reserved_maps, num_maps); |
| 482 | if (err < 0) |
| 483 | goto exit; |
| 484 | |
| 485 | if (has_config) { |
| 486 | err = pinctrl_utils_add_map_configs(pctldev, map, |
| 487 | reserved_maps, |
| 488 | num_maps, |
| 489 | grp->name, |
| 490 | configs, |
| 491 | num_configs, |
| 492 | PIN_MAP_TYPE_CONFIGS_GROUP); |
| 493 | if (err < 0) |
| 494 | goto exit; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | err = 0; |
| 499 | |
| 500 | exit: |
| 501 | kfree(configs); |
| 502 | return err; |
| 503 | } |
| 504 | |
| 505 | static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 506 | struct device_node *np_config, |
| 507 | struct pinctrl_map **map, |
| 508 | unsigned int *num_maps) |
| 509 | { |
| 510 | struct device_node *np; |
| 511 | unsigned int reserved_maps; |
| 512 | int ret; |
| 513 | |
| 514 | *map = NULL; |
| 515 | *num_maps = 0; |
| 516 | reserved_maps = 0; |
| 517 | |
| 518 | for_each_child_of_node(np_config, np) { |
| 519 | ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map, |
| 520 | &reserved_maps, |
| 521 | num_maps); |
| 522 | if (ret < 0) { |
| 523 | pinctrl_utils_free_map(pctldev, *map, *num_maps); |
| 524 | of_node_put(np); |
| 525 | return ret; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev) |
| 533 | { |
| 534 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 535 | |
| 536 | return hw->soc->ngrps; |
| 537 | } |
| 538 | |
| 539 | static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev, |
| 540 | unsigned int group) |
| 541 | { |
| 542 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 543 | |
| 544 | return hw->groups[group].name; |
| 545 | } |
| 546 | |
| 547 | static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev, |
| 548 | unsigned int group, const unsigned int **pins, |
| 549 | unsigned int *num_pins) |
| 550 | { |
| 551 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 552 | |
| 553 | *pins = (unsigned int *)&hw->groups[group].pin; |
| 554 | *num_pins = 1; |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | int mtk_hw_get_value_wrap(struct mtk_pinctrl *hw, unsigned int gpio, int field) |
| 560 | { |
| 561 | const struct mtk_pin_desc *desc; |
| 562 | int value, err; |
| 563 | |
| 564 | if (gpio > hw->soc->npins) |
| 565 | return -EINVAL; |
| 566 | |
| 567 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio]; |
| 568 | |
| 569 | err = mtk_hw_get_value(hw, desc, field, &value); |
| 570 | if (err) |
| 571 | return err; |
| 572 | |
| 573 | return value; |
| 574 | } |
| 575 | |
| 576 | ssize_t mtk_pctrl_show_one_pin(struct mtk_pinctrl *hw, |
| 577 | unsigned int gpio, char *buf, unsigned int bufLen) |
| 578 | { |
| 579 | const struct mtk_pin_desc *desc; |
| 580 | int pinmux, pullup = -1, pullen = -1, r1 = -1, r0 = -1, len = 0; |
| 581 | |
| 582 | if (gpio > hw->soc->npins) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio]; |
| 586 | pinmux = mtk_pctrl_get_pinmux(hw, gpio); |
| 587 | if (pinmux >= hw->soc->nfuncs) |
| 588 | pinmux -= hw->soc->nfuncs; |
| 589 | |
| 590 | mtk_pinconf_bias_get_combo(hw, desc, &pullup, &pullen); |
| 591 | if (pullen == MTK_PUPD_SET_R1R0_00) { |
| 592 | pullen = 0; |
| 593 | r1 = 0; |
| 594 | r0 = 0; |
| 595 | } else if (pullen == MTK_PUPD_SET_R1R0_01) { |
| 596 | pullen = 1; |
| 597 | r1 = 0; |
| 598 | r0 = 1; |
| 599 | } else if (pullen == MTK_PUPD_SET_R1R0_10) { |
| 600 | pullen = 1; |
| 601 | r1 = 1; |
| 602 | r0 = 0; |
| 603 | } else if (pullen == MTK_PUPD_SET_R1R0_11) { |
| 604 | pullen = 1; |
| 605 | r1 = 1; |
| 606 | r0 = 1; |
| 607 | } else if (pullen != MTK_DISABLE && pullen != MTK_ENABLE) { |
| 608 | pullen = 0; |
| 609 | } |
| 610 | len += snprintf(buf + len, bufLen - len, |
| 611 | "%03d: %1d%1d%1d%1d-%02d-%1d%1d-%1d%1d", |
| 612 | gpio, |
| 613 | pinmux, |
| 614 | mtk_pctrl_get_direction(hw, gpio), |
| 615 | mtk_pctrl_get_out(hw, gpio), |
| 616 | mtk_pctrl_get_in(hw, gpio), |
| 617 | mtk_pctrl_get_driving(hw, gpio), |
| 618 | mtk_pctrl_get_smt(hw, gpio), |
| 619 | mtk_pctrl_get_ies(hw, gpio), |
| 620 | pullen, |
| 621 | pullup); |
| 622 | |
| 623 | if (r1 != -1) { |
| 624 | len += snprintf(buf + len, bufLen - len, " [%1d %1d]\n", |
| 625 | r1, r0); |
| 626 | } else { |
| 627 | len += snprintf(buf + len, bufLen - len, "\n"); |
| 628 | } |
| 629 | |
| 630 | return len; |
| 631 | } |
| 632 | |
| 633 | #define PIN_DBG_BUF_SZ 96 |
| 634 | static void mtk_pctrl_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, |
| 635 | unsigned int gpio) |
| 636 | { |
| 637 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 638 | char buf[PIN_DBG_BUF_SZ]; |
| 639 | |
| 640 | (void)mtk_pctrl_show_one_pin(hw, gpio, buf, PIN_DBG_BUF_SZ); |
| 641 | |
| 642 | seq_printf(s, "%s", buf); |
| 643 | } |
| 644 | |
| 645 | static const struct pinctrl_ops mtk_pctlops = { |
| 646 | .dt_node_to_map = mtk_pctrl_dt_node_to_map, |
| 647 | .dt_free_map = pinctrl_utils_free_map, |
| 648 | .get_groups_count = mtk_pctrl_get_groups_count, |
| 649 | .get_group_name = mtk_pctrl_get_group_name, |
| 650 | .get_group_pins = mtk_pctrl_get_group_pins, |
| 651 | .pin_dbg_show = mtk_pctrl_dbg_show, |
| 652 | }; |
| 653 | |
| 654 | static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev) |
| 655 | { |
| 656 | return ARRAY_SIZE(mtk_gpio_functions); |
| 657 | } |
| 658 | |
| 659 | static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev, |
| 660 | unsigned int selector) |
| 661 | { |
| 662 | return mtk_gpio_functions[selector]; |
| 663 | } |
| 664 | |
| 665 | static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev, |
| 666 | unsigned int function, |
| 667 | const char * const **groups, |
| 668 | unsigned int * const num_groups) |
| 669 | { |
| 670 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 671 | |
| 672 | *groups = hw->grp_names; |
| 673 | *num_groups = hw->soc->ngrps; |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev, |
| 679 | unsigned int function, |
| 680 | unsigned int group) |
| 681 | { |
| 682 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 683 | struct mtk_pinctrl_group *grp = hw->groups + group; |
| 684 | const struct mtk_func_desc *desc_func; |
| 685 | const struct mtk_pin_desc *desc; |
| 686 | bool ret; |
| 687 | |
| 688 | ret = mtk_pctrl_is_function_valid(hw, grp->pin, function); |
| 689 | if (!ret) { |
| 690 | dev_err(hw->dev, "invalid function %d on group %d .\n", |
| 691 | function, group); |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | |
| 695 | desc_func = mtk_pctrl_find_function_by_pin(hw, grp->pin, function); |
| 696 | if (!desc_func) |
| 697 | return -EINVAL; |
| 698 | |
| 699 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[grp->pin]; |
| 700 | mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE, desc_func->muxval); |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static const struct pinmux_ops mtk_pmxops = { |
| 706 | .get_functions_count = mtk_pmx_get_funcs_cnt, |
| 707 | .get_function_name = mtk_pmx_get_func_name, |
| 708 | .get_function_groups = mtk_pmx_get_func_groups, |
| 709 | .set_mux = mtk_pmx_set_mux, |
| 710 | .gpio_set_direction = mtk_pinmux_gpio_set_direction, |
| 711 | .gpio_request_enable = mtk_pinmux_gpio_request_enable, |
| 712 | }; |
| 713 | |
| 714 | static int mtk_pconf_group_get(struct pinctrl_dev *pctldev, unsigned int group, |
| 715 | unsigned long *config) |
| 716 | { |
| 717 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 718 | |
| 719 | *config = hw->groups[group].config; |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned int group, |
| 725 | unsigned long *configs, unsigned int num_configs) |
| 726 | { |
| 727 | struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev); |
| 728 | struct mtk_pinctrl_group *grp = &hw->groups[group]; |
| 729 | int i, ret; |
| 730 | |
| 731 | for (i = 0; i < num_configs; i++) { |
| 732 | ret = mtk_pinconf_set(pctldev, grp->pin, |
| 733 | pinconf_to_config_param(configs[i]), |
| 734 | pinconf_to_config_argument(configs[i])); |
| 735 | if (ret < 0) |
| 736 | return ret; |
| 737 | |
| 738 | grp->config = configs[i]; |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static const struct pinconf_ops mtk_confops = { |
| 745 | .pin_config_get = mtk_pinconf_get, |
| 746 | .pin_config_group_get = mtk_pconf_group_get, |
| 747 | .pin_config_group_set = mtk_pconf_group_set, |
| 748 | .is_generic = true, |
| 749 | }; |
| 750 | |
| 751 | static struct pinctrl_desc mtk_desc = { |
| 752 | .name = PINCTRL_PINCTRL_DEV, |
| 753 | .pctlops = &mtk_pctlops, |
| 754 | .pmxops = &mtk_pmxops, |
| 755 | .confops = &mtk_confops, |
| 756 | .owner = THIS_MODULE, |
| 757 | }; |
| 758 | |
| 759 | static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio) |
| 760 | { |
| 761 | struct mtk_pinctrl *hw = gpiochip_get_data(chip); |
| 762 | const struct mtk_pin_desc *desc; |
| 763 | int value, err; |
| 764 | |
| 765 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio]; |
| 766 | |
| 767 | err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &value); |
| 768 | if (err) |
| 769 | return err; |
| 770 | |
| 771 | return !value; |
| 772 | } |
| 773 | |
| 774 | static int mtk_gpio_get(struct gpio_chip *chip, unsigned int gpio) |
| 775 | { |
| 776 | struct mtk_pinctrl *hw = gpiochip_get_data(chip); |
| 777 | const struct mtk_pin_desc *desc; |
| 778 | int value, err; |
| 779 | |
| 780 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio]; |
| 781 | |
| 782 | err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value); |
| 783 | if (err) |
| 784 | return err; |
| 785 | |
| 786 | return !!value; |
| 787 | } |
| 788 | |
| 789 | static void mtk_gpio_set(struct gpio_chip *chip, unsigned int gpio, int value) |
| 790 | { |
| 791 | struct mtk_pinctrl *hw = gpiochip_get_data(chip); |
| 792 | const struct mtk_pin_desc *desc; |
| 793 | |
| 794 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio]; |
| 795 | |
| 796 | mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DO, !!value); |
| 797 | } |
| 798 | |
| 799 | static int mtk_gpio_direction_input(struct gpio_chip *chip, unsigned int gpio) |
| 800 | { |
| 801 | return pinctrl_gpio_direction_input(chip->base + gpio); |
| 802 | } |
| 803 | |
| 804 | static int mtk_gpio_direction_output(struct gpio_chip *chip, unsigned int gpio, |
| 805 | int value) |
| 806 | { |
| 807 | mtk_gpio_set(chip, gpio, value); |
| 808 | |
| 809 | return pinctrl_gpio_direction_output(chip->base + gpio); |
| 810 | } |
| 811 | |
| 812 | static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned int offset) |
| 813 | { |
| 814 | struct mtk_pinctrl *hw = gpiochip_get_data(chip); |
| 815 | const struct mtk_pin_desc *desc; |
| 816 | |
| 817 | if (!hw->eint) |
| 818 | return -ENOTSUPP; |
| 819 | |
| 820 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[offset]; |
| 821 | |
| 822 | if (desc->eint.eint_n == EINT_NA) |
| 823 | return -ENOTSUPP; |
| 824 | |
| 825 | return mtk_eint_find_irq(hw->eint, desc->eint.eint_n); |
| 826 | } |
| 827 | |
| 828 | static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned int offset, |
| 829 | unsigned long config) |
| 830 | { |
| 831 | struct mtk_pinctrl *hw = gpiochip_get_data(chip); |
| 832 | const struct mtk_pin_desc *desc; |
| 833 | u32 debounce; |
| 834 | |
| 835 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[offset]; |
| 836 | |
| 837 | if (!hw->eint || |
| 838 | pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE || |
| 839 | desc->eint.eint_n == EINT_NA) |
| 840 | return -ENOTSUPP; |
| 841 | |
| 842 | debounce = pinconf_to_config_argument(config); |
| 843 | |
| 844 | return mtk_eint_set_debounce(hw->eint, desc->eint.eint_n, debounce); |
| 845 | } |
| 846 | |
| 847 | static int mtk_build_gpiochip(struct mtk_pinctrl *hw, struct device_node *np) |
| 848 | { |
| 849 | struct gpio_chip *chip = &hw->chip; |
| 850 | int ret; |
| 851 | |
| 852 | chip->label = PINCTRL_PINCTRL_DEV; |
| 853 | chip->parent = hw->dev; |
| 854 | chip->request = gpiochip_generic_request; |
| 855 | chip->free = gpiochip_generic_free; |
| 856 | chip->get_direction = mtk_gpio_get_direction; |
| 857 | chip->direction_input = mtk_gpio_direction_input; |
| 858 | chip->direction_output = mtk_gpio_direction_output; |
| 859 | chip->get = mtk_gpio_get; |
| 860 | chip->set = mtk_gpio_set; |
| 861 | chip->to_irq = mtk_gpio_to_irq, |
| 862 | chip->set_config = mtk_gpio_set_config, |
| 863 | chip->base = -1; |
| 864 | chip->ngpio = hw->soc->npins; |
| 865 | chip->of_node = np; |
| 866 | chip->of_gpio_n_cells = 2; |
| 867 | |
| 868 | ret = gpiochip_add_data(chip, hw); |
| 869 | if (ret < 0) |
| 870 | return ret; |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int mtk_pctrl_build_state(struct platform_device *pdev) |
| 876 | { |
| 877 | struct mtk_pinctrl *hw = platform_get_drvdata(pdev); |
| 878 | int i; |
| 879 | |
| 880 | /* Allocate groups */ |
| 881 | hw->groups = devm_kmalloc_array(&pdev->dev, hw->soc->ngrps, |
| 882 | sizeof(*hw->groups), GFP_KERNEL); |
| 883 | if (!hw->groups) |
| 884 | return -ENOMEM; |
| 885 | |
| 886 | /* We assume that one pin is one group, use pin name as group name. */ |
| 887 | hw->grp_names = devm_kmalloc_array(&pdev->dev, hw->soc->ngrps, |
| 888 | sizeof(*hw->grp_names), GFP_KERNEL); |
| 889 | if (!hw->grp_names) |
| 890 | return -ENOMEM; |
| 891 | |
| 892 | for (i = 0; i < hw->soc->npins; i++) { |
| 893 | const struct mtk_pin_desc *pin = hw->soc->pins + i; |
| 894 | struct mtk_pinctrl_group *group = hw->groups + i; |
| 895 | |
| 896 | group->name = pin->name; |
| 897 | group->pin = pin->number; |
| 898 | |
| 899 | hw->grp_names[i] = pin->name; |
| 900 | } |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | //tianyan@2021.10.20 modify for RI gpio start |
| 906 | void mtk_paris_pinctrl_init_kernel(struct mtk_pinctrl *hw) |
| 907 | { |
| 908 | const struct mtk_pin_desc *desc; |
| 909 | |
| 910 | printk("mtk_paris_pinctrl_init_kernel\n"); |
| 911 | |
| 912 | desc = (const struct mtk_pin_desc *)&hw->soc->pins[63]; |
| 913 | mtk_hw_set_value(hw, desc, 0, 0); |
| 914 | } |
| 915 | //tianyan@2021.10.20 modify for RI gpio end |
| 916 | |
| 917 | int mtk_paris_pinctrl_probe(struct platform_device *pdev, |
| 918 | const struct mtk_pin_soc *soc) |
| 919 | { |
| 920 | struct pinctrl_pin_desc *pins; |
| 921 | struct mtk_pinctrl *hw; |
| 922 | struct resource *res; |
| 923 | int err, i; |
| 924 | |
| 925 | hw = devm_kzalloc(&pdev->dev, sizeof(*hw), GFP_KERNEL); |
| 926 | if (!hw) |
| 927 | return -ENOMEM; |
| 928 | |
| 929 | platform_set_drvdata(pdev, hw); |
| 930 | hw->soc = soc; |
| 931 | hw->dev = &pdev->dev; |
| 932 | |
| 933 | if (!hw->soc->nbase_names) { |
| 934 | dev_err(&pdev->dev, |
| 935 | "SoC should be assigned at least one register base\n"); |
| 936 | return -EINVAL; |
| 937 | } |
| 938 | |
| 939 | hw->base = devm_kmalloc_array(&pdev->dev, hw->soc->nbase_names, |
| 940 | sizeof(*hw->base), GFP_KERNEL); |
| 941 | if (!hw->base) |
| 942 | return -ENOMEM; |
| 943 | |
| 944 | for (i = 0; i < hw->soc->nbase_names; i++) { |
| 945 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 946 | hw->soc->base_names[i]); |
| 947 | if (!res) { |
| 948 | dev_err(&pdev->dev, "missing IO resource\n"); |
| 949 | return -ENXIO; |
| 950 | } |
| 951 | |
| 952 | hw->base[i] = devm_ioremap_resource(&pdev->dev, res); |
| 953 | if (IS_ERR(hw->base[i])) |
| 954 | return PTR_ERR(hw->base[i]); |
| 955 | } |
| 956 | |
| 957 | hw->nbase = hw->soc->nbase_names; |
| 958 | |
| 959 | err = mtk_pctrl_build_state(pdev); |
| 960 | if (err) { |
| 961 | dev_err(&pdev->dev, "build state failed: %d\n", err); |
| 962 | return -EINVAL; |
| 963 | } |
| 964 | |
| 965 | /* Copy from internal struct mtk_pin_desc to register to the core */ |
| 966 | pins = devm_kmalloc_array(&pdev->dev, hw->soc->npins, sizeof(*pins), |
| 967 | GFP_KERNEL); |
| 968 | if (!pins) |
| 969 | return -ENOMEM; |
| 970 | |
| 971 | for (i = 0; i < hw->soc->npins; i++) { |
| 972 | pins[i].number = hw->soc->pins[i].number; |
| 973 | pins[i].name = hw->soc->pins[i].name; |
| 974 | } |
| 975 | |
| 976 | /* Setup pins descriptions per SoC types */ |
| 977 | mtk_desc.pins = (const struct pinctrl_pin_desc *)pins; |
| 978 | mtk_desc.npins = hw->soc->npins; |
| 979 | mtk_desc.num_custom_params = ARRAY_SIZE(mtk_custom_bindings); |
| 980 | mtk_desc.custom_params = mtk_custom_bindings; |
| 981 | #ifdef CONFIG_DEBUG_FS |
| 982 | mtk_desc.custom_conf_items = mtk_conf_items; |
| 983 | #endif |
| 984 | |
| 985 | err = devm_pinctrl_register_and_init(&pdev->dev, &mtk_desc, hw, |
| 986 | &hw->pctrl); |
| 987 | if (err) |
| 988 | return err; |
| 989 | |
| 990 | err = pinctrl_enable(hw->pctrl); |
| 991 | if (err) |
| 992 | return err; |
| 993 | |
| 994 | #ifdef CONFIG_DEBUG_FS |
| 995 | if (mtk_gpio_create_attr(&pdev->dev)) |
| 996 | pr_info("[pinctrl]mtk_gpio create attribute error\n"); |
| 997 | #endif |
| 998 | |
| 999 | err = mtk_build_eint(hw, pdev); |
| 1000 | if (err) |
| 1001 | dev_warn(&pdev->dev, |
| 1002 | "Failed to add EINT, but pinctrl still can work\n"); |
| 1003 | |
| 1004 | /* Build gpiochip should be after pinctrl_enable is done */ |
| 1005 | err = mtk_build_gpiochip(hw, pdev->dev.of_node); |
| 1006 | if (err) { |
| 1007 | dev_err(&pdev->dev, "Failed to add gpio_chip\n"); |
| 1008 | return err; |
| 1009 | } |
| 1010 | |
| 1011 | platform_set_drvdata(pdev, hw); |
| 1012 | |
| 1013 | //tianyan@2021.10.20 modify for RI gpio start |
| 1014 | mtk_paris_pinctrl_init_kernel(hw); |
| 1015 | //tianyan@2021.10.20 modify for RI gpio end |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |