pinctrl-uniphier-core.c 19 KB

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