pinctrl-paris.c 28 KB

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