pinctrl-uniphier-core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * Copyright (C) 2015-2017 Socionext Inc.
  3. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/export.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/of.h>
  18. #include <linux/pinctrl/pinconf.h>
  19. #include <linux/pinctrl/pinconf-generic.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include "../core.h"
  25. #include "../pinctrl-utils.h"
  26. #include "pinctrl-uniphier.h"
  27. #define UNIPHIER_PINCTRL_PINMUX_BASE 0x1000
  28. #define UNIPHIER_PINCTRL_LOAD_PINMUX 0x1700
  29. #define UNIPHIER_PINCTRL_DRVCTRL_BASE 0x1800
  30. #define UNIPHIER_PINCTRL_DRV2CTRL_BASE 0x1900
  31. #define UNIPHIER_PINCTRL_DRV3CTRL_BASE 0x1980
  32. #define UNIPHIER_PINCTRL_PUPDCTRL_BASE 0x1a00
  33. #define UNIPHIER_PINCTRL_IECTRL 0x1d00
  34. struct uniphier_pinctrl_priv {
  35. struct pinctrl_desc pctldesc;
  36. struct pinctrl_dev *pctldev;
  37. struct regmap *regmap;
  38. struct uniphier_pinctrl_socdata *socdata;
  39. };
  40. static int uniphier_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  41. {
  42. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  43. return priv->socdata->groups_count;
  44. }
  45. static const char *uniphier_pctl_get_group_name(struct pinctrl_dev *pctldev,
  46. unsigned selector)
  47. {
  48. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  49. return priv->socdata->groups[selector].name;
  50. }
  51. static int uniphier_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  52. unsigned selector,
  53. const unsigned **pins,
  54. unsigned *num_pins)
  55. {
  56. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  57. *pins = priv->socdata->groups[selector].pins;
  58. *num_pins = priv->socdata->groups[selector].num_pins;
  59. return 0;
  60. }
  61. #ifdef CONFIG_DEBUG_FS
  62. static void uniphier_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  63. struct seq_file *s, unsigned offset)
  64. {
  65. const struct pin_desc *desc = pin_desc_get(pctldev, offset);
  66. const char *pull_dir, *drv_type;
  67. switch (uniphier_pin_get_pull_dir(desc->drv_data)) {
  68. case UNIPHIER_PIN_PULL_UP:
  69. pull_dir = "UP";
  70. break;
  71. case UNIPHIER_PIN_PULL_DOWN:
  72. pull_dir = "DOWN";
  73. break;
  74. case UNIPHIER_PIN_PULL_UP_FIXED:
  75. pull_dir = "UP(FIXED)";
  76. break;
  77. case UNIPHIER_PIN_PULL_DOWN_FIXED:
  78. pull_dir = "DOWN(FIXED)";
  79. break;
  80. case UNIPHIER_PIN_PULL_NONE:
  81. pull_dir = "NONE";
  82. break;
  83. default:
  84. BUG();
  85. }
  86. switch (uniphier_pin_get_drv_type(desc->drv_data)) {
  87. case UNIPHIER_PIN_DRV_1BIT:
  88. drv_type = "4/8(mA)";
  89. break;
  90. case UNIPHIER_PIN_DRV_2BIT:
  91. drv_type = "8/12/16/20(mA)";
  92. break;
  93. case UNIPHIER_PIN_DRV_3BIT:
  94. drv_type = "4/5/7/9/11/12/14/16(mA)";
  95. break;
  96. case UNIPHIER_PIN_DRV_FIXED4:
  97. drv_type = "4(mA)";
  98. break;
  99. case UNIPHIER_PIN_DRV_FIXED5:
  100. drv_type = "5(mA)";
  101. break;
  102. case UNIPHIER_PIN_DRV_FIXED8:
  103. drv_type = "8(mA)";
  104. break;
  105. case UNIPHIER_PIN_DRV_NONE:
  106. drv_type = "NONE";
  107. break;
  108. default:
  109. BUG();
  110. }
  111. seq_printf(s, " PULL_DIR=%s DRV_TYPE=%s", pull_dir, drv_type);
  112. }
  113. #endif
  114. static const struct pinctrl_ops uniphier_pctlops = {
  115. .get_groups_count = uniphier_pctl_get_groups_count,
  116. .get_group_name = uniphier_pctl_get_group_name,
  117. .get_group_pins = uniphier_pctl_get_group_pins,
  118. #ifdef CONFIG_DEBUG_FS
  119. .pin_dbg_show = uniphier_pctl_pin_dbg_show,
  120. #endif
  121. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  122. .dt_free_map = pinctrl_utils_free_map,
  123. };
  124. static int uniphier_conf_pin_bias_get(struct pinctrl_dev *pctldev,
  125. const struct pin_desc *desc,
  126. enum pin_config_param param)
  127. {
  128. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  129. enum uniphier_pin_pull_dir pull_dir =
  130. uniphier_pin_get_pull_dir(desc->drv_data);
  131. unsigned int pupdctrl, reg, shift, val;
  132. unsigned int expected = 1;
  133. int ret;
  134. switch (param) {
  135. case PIN_CONFIG_BIAS_DISABLE:
  136. if (pull_dir == UNIPHIER_PIN_PULL_NONE)
  137. return 0;
  138. if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
  139. pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
  140. return -EINVAL;
  141. expected = 0;
  142. break;
  143. case PIN_CONFIG_BIAS_PULL_UP:
  144. if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED)
  145. return 0;
  146. if (pull_dir != UNIPHIER_PIN_PULL_UP)
  147. return -EINVAL;
  148. break;
  149. case PIN_CONFIG_BIAS_PULL_DOWN:
  150. if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
  151. return 0;
  152. if (pull_dir != UNIPHIER_PIN_PULL_DOWN)
  153. return -EINVAL;
  154. break;
  155. default:
  156. BUG();
  157. }
  158. pupdctrl = uniphier_pin_get_pupdctrl(desc->drv_data);
  159. reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
  160. shift = pupdctrl % 32;
  161. ret = regmap_read(priv->regmap, reg, &val);
  162. if (ret)
  163. return ret;
  164. val = (val >> shift) & 1;
  165. return (val == expected) ? 0 : -EINVAL;
  166. }
  167. static int uniphier_conf_pin_drive_get(struct pinctrl_dev *pctldev,
  168. const struct pin_desc *desc,
  169. u16 *strength)
  170. {
  171. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  172. enum uniphier_pin_drv_type type =
  173. uniphier_pin_get_drv_type(desc->drv_data);
  174. const unsigned int strength_1bit[] = {4, 8};
  175. const unsigned int strength_2bit[] = {8, 12, 16, 20};
  176. const unsigned int strength_3bit[] = {4, 5, 7, 9, 11, 12, 14, 16};
  177. const unsigned int *supported_strength;
  178. unsigned int drvctrl, reg, shift, mask, width, val;
  179. int ret;
  180. switch (type) {
  181. case UNIPHIER_PIN_DRV_1BIT:
  182. supported_strength = strength_1bit;
  183. reg = UNIPHIER_PINCTRL_DRVCTRL_BASE;
  184. width = 1;
  185. break;
  186. case UNIPHIER_PIN_DRV_2BIT:
  187. supported_strength = strength_2bit;
  188. reg = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
  189. width = 2;
  190. break;
  191. case UNIPHIER_PIN_DRV_3BIT:
  192. supported_strength = strength_3bit;
  193. reg = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
  194. width = 4;
  195. break;
  196. case UNIPHIER_PIN_DRV_FIXED4:
  197. *strength = 4;
  198. return 0;
  199. case UNIPHIER_PIN_DRV_FIXED5:
  200. *strength = 5;
  201. return 0;
  202. case UNIPHIER_PIN_DRV_FIXED8:
  203. *strength = 8;
  204. return 0;
  205. default:
  206. /* drive strength control is not supported for this pin */
  207. return -EINVAL;
  208. }
  209. drvctrl = uniphier_pin_get_drvctrl(desc->drv_data);
  210. drvctrl *= width;
  211. reg += drvctrl / 32 * 4;
  212. shift = drvctrl % 32;
  213. mask = (1U << width) - 1;
  214. ret = regmap_read(priv->regmap, reg, &val);
  215. if (ret)
  216. return ret;
  217. *strength = supported_strength[(val >> shift) & mask];
  218. return 0;
  219. }
  220. static int uniphier_conf_pin_input_enable_get(struct pinctrl_dev *pctldev,
  221. const struct pin_desc *desc)
  222. {
  223. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  224. unsigned int iectrl = uniphier_pin_get_iectrl(desc->drv_data);
  225. unsigned int val;
  226. int ret;
  227. if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
  228. /* This pin is always input-enabled. */
  229. return 0;
  230. ret = regmap_read(priv->regmap, UNIPHIER_PINCTRL_IECTRL, &val);
  231. if (ret)
  232. return ret;
  233. return val & BIT(iectrl) ? 0 : -EINVAL;
  234. }
  235. static int uniphier_conf_pin_config_get(struct pinctrl_dev *pctldev,
  236. unsigned pin,
  237. unsigned long *configs)
  238. {
  239. const struct pin_desc *desc = pin_desc_get(pctldev, pin);
  240. enum pin_config_param param = pinconf_to_config_param(*configs);
  241. bool has_arg = false;
  242. u16 arg;
  243. int ret;
  244. switch (param) {
  245. case PIN_CONFIG_BIAS_DISABLE:
  246. case PIN_CONFIG_BIAS_PULL_UP:
  247. case PIN_CONFIG_BIAS_PULL_DOWN:
  248. ret = uniphier_conf_pin_bias_get(pctldev, desc, param);
  249. break;
  250. case PIN_CONFIG_DRIVE_STRENGTH:
  251. ret = uniphier_conf_pin_drive_get(pctldev, desc, &arg);
  252. has_arg = true;
  253. break;
  254. case PIN_CONFIG_INPUT_ENABLE:
  255. ret = uniphier_conf_pin_input_enable_get(pctldev, desc);
  256. break;
  257. default:
  258. /* unsupported parameter */
  259. ret = -EINVAL;
  260. break;
  261. }
  262. if (ret == 0 && has_arg)
  263. *configs = pinconf_to_config_packed(param, arg);
  264. return ret;
  265. }
  266. static int uniphier_conf_pin_bias_set(struct pinctrl_dev *pctldev,
  267. const struct pin_desc *desc,
  268. enum pin_config_param param, u32 arg)
  269. {
  270. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  271. enum uniphier_pin_pull_dir pull_dir =
  272. uniphier_pin_get_pull_dir(desc->drv_data);
  273. unsigned int pupdctrl, reg, shift;
  274. unsigned int val = 1;
  275. switch (param) {
  276. case PIN_CONFIG_BIAS_DISABLE:
  277. if (pull_dir == UNIPHIER_PIN_PULL_NONE)
  278. return 0;
  279. if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
  280. pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED) {
  281. dev_err(pctldev->dev,
  282. "can not disable pull register for pin %s\n",
  283. desc->name);
  284. return -EINVAL;
  285. }
  286. val = 0;
  287. break;
  288. case PIN_CONFIG_BIAS_PULL_UP:
  289. if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED && arg != 0)
  290. return 0;
  291. if (pull_dir != UNIPHIER_PIN_PULL_UP) {
  292. dev_err(pctldev->dev,
  293. "pull-up is unsupported for pin %s\n",
  294. desc->name);
  295. return -EINVAL;
  296. }
  297. if (arg == 0) {
  298. dev_err(pctldev->dev, "pull-up can not be total\n");
  299. return -EINVAL;
  300. }
  301. break;
  302. case PIN_CONFIG_BIAS_PULL_DOWN:
  303. if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED && arg != 0)
  304. return 0;
  305. if (pull_dir != UNIPHIER_PIN_PULL_DOWN) {
  306. dev_err(pctldev->dev,
  307. "pull-down is unsupported for pin %s\n",
  308. desc->name);
  309. return -EINVAL;
  310. }
  311. if (arg == 0) {
  312. dev_err(pctldev->dev, "pull-down can not be total\n");
  313. return -EINVAL;
  314. }
  315. break;
  316. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  317. if (pull_dir == UNIPHIER_PIN_PULL_NONE) {
  318. dev_err(pctldev->dev,
  319. "pull-up/down is unsupported for pin %s\n",
  320. desc->name);
  321. return -EINVAL;
  322. }
  323. if (arg == 0)
  324. return 0; /* configuration ingored */
  325. break;
  326. default:
  327. BUG();
  328. }
  329. pupdctrl = uniphier_pin_get_pupdctrl(desc->drv_data);
  330. reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
  331. shift = pupdctrl % 32;
  332. return regmap_update_bits(priv->regmap, reg, 1 << shift, val << shift);
  333. }
  334. static int uniphier_conf_pin_drive_set(struct pinctrl_dev *pctldev,
  335. const struct pin_desc *desc,
  336. u16 strength)
  337. {
  338. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  339. enum uniphier_pin_drv_type type =
  340. uniphier_pin_get_drv_type(desc->drv_data);
  341. const unsigned int strength_1bit[] = {4, 8, -1};
  342. const unsigned int strength_2bit[] = {8, 12, 16, 20, -1};
  343. const unsigned int strength_3bit[] = {4, 5, 7, 9, 11, 12, 14, 16, -1};
  344. const unsigned int *supported_strength;
  345. unsigned int drvctrl, reg, shift, mask, width, val;
  346. switch (type) {
  347. case UNIPHIER_PIN_DRV_1BIT:
  348. supported_strength = strength_1bit;
  349. reg = UNIPHIER_PINCTRL_DRVCTRL_BASE;
  350. width = 1;
  351. break;
  352. case UNIPHIER_PIN_DRV_2BIT:
  353. supported_strength = strength_2bit;
  354. reg = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
  355. width = 2;
  356. break;
  357. case UNIPHIER_PIN_DRV_3BIT:
  358. supported_strength = strength_3bit;
  359. reg = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
  360. width = 4;
  361. break;
  362. default:
  363. dev_err(pctldev->dev,
  364. "cannot change drive strength for pin %s\n",
  365. desc->name);
  366. return -EINVAL;
  367. }
  368. for (val = 0; supported_strength[val] > 0; val++) {
  369. if (supported_strength[val] > strength)
  370. break;
  371. }
  372. if (val == 0) {
  373. dev_err(pctldev->dev,
  374. "unsupported drive strength %u mA for pin %s\n",
  375. strength, desc->name);
  376. return -EINVAL;
  377. }
  378. val--;
  379. drvctrl = uniphier_pin_get_drvctrl(desc->drv_data);
  380. drvctrl *= width;
  381. reg += drvctrl / 32 * 4;
  382. shift = drvctrl % 32;
  383. mask = (1U << width) - 1;
  384. return regmap_update_bits(priv->regmap, reg,
  385. mask << shift, val << shift);
  386. }
  387. static int uniphier_conf_pin_input_enable(struct pinctrl_dev *pctldev,
  388. const struct pin_desc *desc,
  389. u16 enable)
  390. {
  391. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  392. unsigned int iectrl = uniphier_pin_get_iectrl(desc->drv_data);
  393. unsigned int reg, mask;
  394. /*
  395. * Multiple pins share one input enable, per-pin disabling is
  396. * impossible.
  397. */
  398. if (!(priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL) &&
  399. !enable)
  400. return -EINVAL;
  401. /* UNIPHIER_PIN_IECTRL_NONE means the pin is always input-enabled */
  402. if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
  403. return enable ? 0 : -EINVAL;
  404. reg = UNIPHIER_PINCTRL_IECTRL + iectrl / 32 * 4;
  405. mask = BIT(iectrl % 32);
  406. return regmap_update_bits(priv->regmap, reg, mask, enable ? mask : 0);
  407. }
  408. static int uniphier_conf_pin_config_set(struct pinctrl_dev *pctldev,
  409. unsigned pin,
  410. unsigned long *configs,
  411. unsigned num_configs)
  412. {
  413. const struct pin_desc *desc = pin_desc_get(pctldev, pin);
  414. int i, ret;
  415. for (i = 0; i < num_configs; i++) {
  416. enum pin_config_param param =
  417. pinconf_to_config_param(configs[i]);
  418. u32 arg = pinconf_to_config_argument(configs[i]);
  419. switch (param) {
  420. case PIN_CONFIG_BIAS_DISABLE:
  421. case PIN_CONFIG_BIAS_PULL_UP:
  422. case PIN_CONFIG_BIAS_PULL_DOWN:
  423. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  424. ret = uniphier_conf_pin_bias_set(pctldev, desc,
  425. param, arg);
  426. break;
  427. case PIN_CONFIG_DRIVE_STRENGTH:
  428. ret = uniphier_conf_pin_drive_set(pctldev, desc, arg);
  429. break;
  430. case PIN_CONFIG_INPUT_ENABLE:
  431. ret = uniphier_conf_pin_input_enable(pctldev, desc,
  432. arg);
  433. break;
  434. default:
  435. dev_err(pctldev->dev,
  436. "unsupported configuration parameter %u\n",
  437. param);
  438. return -EINVAL;
  439. }
  440. if (ret)
  441. return ret;
  442. }
  443. return 0;
  444. }
  445. static int uniphier_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
  446. unsigned selector,
  447. unsigned long *configs,
  448. unsigned num_configs)
  449. {
  450. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  451. const unsigned *pins = priv->socdata->groups[selector].pins;
  452. unsigned num_pins = priv->socdata->groups[selector].num_pins;
  453. int i, ret;
  454. for (i = 0; i < num_pins; i++) {
  455. ret = uniphier_conf_pin_config_set(pctldev, pins[i],
  456. configs, num_configs);
  457. if (ret)
  458. return ret;
  459. }
  460. return 0;
  461. }
  462. static const struct pinconf_ops uniphier_confops = {
  463. .is_generic = true,
  464. .pin_config_get = uniphier_conf_pin_config_get,
  465. .pin_config_set = uniphier_conf_pin_config_set,
  466. .pin_config_group_set = uniphier_conf_pin_config_group_set,
  467. };
  468. static int uniphier_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  469. {
  470. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  471. return priv->socdata->functions_count;
  472. }
  473. static const char *uniphier_pmx_get_function_name(struct pinctrl_dev *pctldev,
  474. unsigned selector)
  475. {
  476. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  477. return priv->socdata->functions[selector].name;
  478. }
  479. static int uniphier_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  480. unsigned selector,
  481. const char * const **groups,
  482. unsigned *num_groups)
  483. {
  484. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  485. *groups = priv->socdata->functions[selector].groups;
  486. *num_groups = priv->socdata->functions[selector].num_groups;
  487. return 0;
  488. }
  489. static int uniphier_pmx_set_one_mux(struct pinctrl_dev *pctldev, unsigned pin,
  490. int muxval)
  491. {
  492. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  493. unsigned int mux_bits, reg_stride, reg, reg_end, shift, mask;
  494. bool load_pinctrl;
  495. int ret;
  496. /* some pins need input-enabling */
  497. ret = uniphier_conf_pin_input_enable(pctldev,
  498. pin_desc_get(pctldev, pin), 1);
  499. if (ret)
  500. return ret;
  501. if (muxval < 0)
  502. return 0; /* dedicated pin; nothing to do for pin-mux */
  503. if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
  504. /*
  505. * Mode reg_offset bit_position
  506. * Normal 4 * n shift+3:shift
  507. * Debug 4 * n shift+7:shift+4
  508. */
  509. mux_bits = 4;
  510. reg_stride = 8;
  511. load_pinctrl = true;
  512. } else {
  513. /*
  514. * Mode reg_offset bit_position
  515. * Normal 8 * n shift+3:shift
  516. * Debug 8 * n + 4 shift+3:shift
  517. */
  518. mux_bits = 8;
  519. reg_stride = 4;
  520. load_pinctrl = false;
  521. }
  522. reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
  523. reg_end = reg + reg_stride;
  524. shift = pin * mux_bits % 32;
  525. mask = (1U << mux_bits) - 1;
  526. /*
  527. * If reg_stride is greater than 4, the MSB of each pinsel shall be
  528. * stored in the offset+4.
  529. */
  530. for (; reg < reg_end; reg += 4) {
  531. ret = regmap_update_bits(priv->regmap, reg,
  532. mask << shift, muxval << shift);
  533. if (ret)
  534. return ret;
  535. muxval >>= mux_bits;
  536. }
  537. if (load_pinctrl) {
  538. ret = regmap_write(priv->regmap,
  539. UNIPHIER_PINCTRL_LOAD_PINMUX, 1);
  540. if (ret)
  541. return ret;
  542. }
  543. return 0;
  544. }
  545. static int uniphier_pmx_set_mux(struct pinctrl_dev *pctldev,
  546. unsigned func_selector,
  547. unsigned group_selector)
  548. {
  549. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  550. const struct uniphier_pinctrl_group *grp =
  551. &priv->socdata->groups[group_selector];
  552. int i;
  553. int ret;
  554. for (i = 0; i < grp->num_pins; i++) {
  555. ret = uniphier_pmx_set_one_mux(pctldev, grp->pins[i],
  556. grp->muxvals[i]);
  557. if (ret)
  558. return ret;
  559. }
  560. return 0;
  561. }
  562. static int uniphier_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
  563. struct pinctrl_gpio_range *range,
  564. unsigned offset)
  565. {
  566. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  567. const struct uniphier_pinctrl_group *groups = priv->socdata->groups;
  568. int groups_count = priv->socdata->groups_count;
  569. enum uniphier_pinmux_gpio_range_type range_type;
  570. int i, j;
  571. if (strstr(range->name, "irq"))
  572. range_type = UNIPHIER_PINMUX_GPIO_RANGE_IRQ;
  573. else
  574. range_type = UNIPHIER_PINMUX_GPIO_RANGE_PORT;
  575. for (i = 0; i < groups_count; i++) {
  576. if (groups[i].range_type != range_type)
  577. continue;
  578. for (j = 0; j < groups[i].num_pins; j++)
  579. if (groups[i].pins[j] == offset)
  580. goto found;
  581. }
  582. dev_err(pctldev->dev, "pin %u does not support GPIO\n", offset);
  583. return -EINVAL;
  584. found:
  585. return uniphier_pmx_set_one_mux(pctldev, offset, groups[i].muxvals[j]);
  586. }
  587. static const struct pinmux_ops uniphier_pmxops = {
  588. .get_functions_count = uniphier_pmx_get_functions_count,
  589. .get_function_name = uniphier_pmx_get_function_name,
  590. .get_function_groups = uniphier_pmx_get_function_groups,
  591. .set_mux = uniphier_pmx_set_mux,
  592. .gpio_request_enable = uniphier_pmx_gpio_request_enable,
  593. .strict = true,
  594. };
  595. int uniphier_pinctrl_probe(struct platform_device *pdev,
  596. struct uniphier_pinctrl_socdata *socdata)
  597. {
  598. struct device *dev = &pdev->dev;
  599. struct uniphier_pinctrl_priv *priv;
  600. struct device_node *parent;
  601. if (!socdata ||
  602. !socdata->pins || !socdata->npins ||
  603. !socdata->groups || !socdata->groups_count ||
  604. !socdata->functions || !socdata->functions_count) {
  605. dev_err(dev, "pinctrl socdata lacks necessary members\n");
  606. return -EINVAL;
  607. }
  608. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  609. if (!priv)
  610. return -ENOMEM;
  611. parent = of_get_parent(dev->of_node);
  612. priv->regmap = syscon_node_to_regmap(parent);
  613. of_node_put(parent);
  614. if (IS_ERR(priv->regmap)) {
  615. dev_err(dev, "failed to get regmap\n");
  616. return PTR_ERR(priv->regmap);
  617. }
  618. priv->socdata = socdata;
  619. priv->pctldesc.name = dev->driver->name;
  620. priv->pctldesc.pins = socdata->pins;
  621. priv->pctldesc.npins = socdata->npins;
  622. priv->pctldesc.pctlops = &uniphier_pctlops;
  623. priv->pctldesc.pmxops = &uniphier_pmxops;
  624. priv->pctldesc.confops = &uniphier_confops;
  625. priv->pctldesc.owner = dev->driver->owner;
  626. priv->pctldev = devm_pinctrl_register(dev, &priv->pctldesc, priv);
  627. if (IS_ERR(priv->pctldev)) {
  628. dev_err(dev, "failed to register UniPhier pinctrl driver\n");
  629. return PTR_ERR(priv->pctldev);
  630. }
  631. platform_set_drvdata(pdev, priv);
  632. return 0;
  633. }
  634. EXPORT_SYMBOL_GPL(uniphier_pinctrl_probe);