pinctrl-uniphier-core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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/list.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_BASE 0x1d00
  34. struct uniphier_pinctrl_reg_region {
  35. struct list_head node;
  36. unsigned int base;
  37. unsigned int nregs;
  38. u32 vals[0];
  39. };
  40. struct uniphier_pinctrl_priv {
  41. struct pinctrl_desc pctldesc;
  42. struct pinctrl_dev *pctldev;
  43. struct regmap *regmap;
  44. struct uniphier_pinctrl_socdata *socdata;
  45. struct list_head reg_regions;
  46. };
  47. static int uniphier_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  48. {
  49. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  50. return priv->socdata->groups_count;
  51. }
  52. static const char *uniphier_pctl_get_group_name(struct pinctrl_dev *pctldev,
  53. unsigned selector)
  54. {
  55. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  56. return priv->socdata->groups[selector].name;
  57. }
  58. static int uniphier_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  59. unsigned selector,
  60. const unsigned **pins,
  61. unsigned *num_pins)
  62. {
  63. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  64. *pins = priv->socdata->groups[selector].pins;
  65. *num_pins = priv->socdata->groups[selector].num_pins;
  66. return 0;
  67. }
  68. #ifdef CONFIG_DEBUG_FS
  69. static void uniphier_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  70. struct seq_file *s, unsigned offset)
  71. {
  72. const struct pin_desc *desc = pin_desc_get(pctldev, offset);
  73. const char *pull_dir, *drv_type;
  74. switch (uniphier_pin_get_pull_dir(desc->drv_data)) {
  75. case UNIPHIER_PIN_PULL_UP:
  76. pull_dir = "UP";
  77. break;
  78. case UNIPHIER_PIN_PULL_DOWN:
  79. pull_dir = "DOWN";
  80. break;
  81. case UNIPHIER_PIN_PULL_UP_FIXED:
  82. pull_dir = "UP(FIXED)";
  83. break;
  84. case UNIPHIER_PIN_PULL_DOWN_FIXED:
  85. pull_dir = "DOWN(FIXED)";
  86. break;
  87. case UNIPHIER_PIN_PULL_NONE:
  88. pull_dir = "NONE";
  89. break;
  90. default:
  91. BUG();
  92. }
  93. switch (uniphier_pin_get_drv_type(desc->drv_data)) {
  94. case UNIPHIER_PIN_DRV_1BIT:
  95. drv_type = "4/8(mA)";
  96. break;
  97. case UNIPHIER_PIN_DRV_2BIT:
  98. drv_type = "8/12/16/20(mA)";
  99. break;
  100. case UNIPHIER_PIN_DRV_3BIT:
  101. drv_type = "4/5/7/9/11/12/14/16(mA)";
  102. break;
  103. case UNIPHIER_PIN_DRV_FIXED4:
  104. drv_type = "4(mA)";
  105. break;
  106. case UNIPHIER_PIN_DRV_FIXED5:
  107. drv_type = "5(mA)";
  108. break;
  109. case UNIPHIER_PIN_DRV_FIXED8:
  110. drv_type = "8(mA)";
  111. break;
  112. case UNIPHIER_PIN_DRV_NONE:
  113. drv_type = "NONE";
  114. break;
  115. default:
  116. BUG();
  117. }
  118. seq_printf(s, " PULL_DIR=%s DRV_TYPE=%s", pull_dir, drv_type);
  119. }
  120. #endif
  121. static const struct pinctrl_ops uniphier_pctlops = {
  122. .get_groups_count = uniphier_pctl_get_groups_count,
  123. .get_group_name = uniphier_pctl_get_group_name,
  124. .get_group_pins = uniphier_pctl_get_group_pins,
  125. #ifdef CONFIG_DEBUG_FS
  126. .pin_dbg_show = uniphier_pctl_pin_dbg_show,
  127. #endif
  128. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  129. .dt_free_map = pinctrl_utils_free_map,
  130. };
  131. static const unsigned int uniphier_conf_drv_strengths_1bit[] = {4, 8};
  132. static const unsigned int uniphier_conf_drv_strengths_2bit[] = {8, 12, 16, 20};
  133. static const unsigned int uniphier_conf_drv_strengths_3bit[] = {4, 5, 7, 9, 11,
  134. 12, 14, 16};
  135. static const unsigned int uniphier_conf_drv_strengths_fixed4[] = {4};
  136. static const unsigned int uniphier_conf_drv_strengths_fixed5[] = {5};
  137. static const unsigned int uniphier_conf_drv_strengths_fixed8[] = {8};
  138. static int uniphier_conf_get_drvctrl_data(struct pinctrl_dev *pctldev,
  139. unsigned int pin, unsigned int *reg,
  140. unsigned int *shift,
  141. unsigned int *mask,
  142. const unsigned int **strengths)
  143. {
  144. const struct pin_desc *desc = pin_desc_get(pctldev, pin);
  145. enum uniphier_pin_drv_type type =
  146. uniphier_pin_get_drv_type(desc->drv_data);
  147. unsigned int base = 0;
  148. unsigned int stride = 0;
  149. unsigned int width = 0;
  150. unsigned int drvctrl;
  151. switch (type) {
  152. case UNIPHIER_PIN_DRV_1BIT:
  153. *strengths = uniphier_conf_drv_strengths_1bit;
  154. base = UNIPHIER_PINCTRL_DRVCTRL_BASE;
  155. stride = 1;
  156. width = 1;
  157. break;
  158. case UNIPHIER_PIN_DRV_2BIT:
  159. *strengths = uniphier_conf_drv_strengths_2bit;
  160. base = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
  161. stride = 2;
  162. width = 2;
  163. break;
  164. case UNIPHIER_PIN_DRV_3BIT:
  165. *strengths = uniphier_conf_drv_strengths_3bit;
  166. base = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
  167. stride = 4;
  168. width = 3;
  169. break;
  170. case UNIPHIER_PIN_DRV_FIXED4:
  171. *strengths = uniphier_conf_drv_strengths_fixed4;
  172. break;
  173. case UNIPHIER_PIN_DRV_FIXED5:
  174. *strengths = uniphier_conf_drv_strengths_fixed5;
  175. break;
  176. case UNIPHIER_PIN_DRV_FIXED8:
  177. *strengths = uniphier_conf_drv_strengths_fixed8;
  178. break;
  179. default:
  180. /* drive strength control is not supported for this pin */
  181. return -EINVAL;
  182. }
  183. drvctrl = uniphier_pin_get_drvctrl(desc->drv_data);
  184. drvctrl *= stride;
  185. *reg = base + drvctrl / 32 * 4;
  186. *shift = drvctrl % 32;
  187. *mask = (1U << width) - 1;
  188. return 0;
  189. }
  190. static int uniphier_conf_pin_bias_get(struct pinctrl_dev *pctldev,
  191. unsigned int pin,
  192. enum pin_config_param param)
  193. {
  194. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  195. const struct pin_desc *desc = pin_desc_get(pctldev, pin);
  196. enum uniphier_pin_pull_dir pull_dir =
  197. uniphier_pin_get_pull_dir(desc->drv_data);
  198. unsigned int pupdctrl, reg, shift, val;
  199. unsigned int expected = 1;
  200. int ret;
  201. switch (param) {
  202. case PIN_CONFIG_BIAS_DISABLE:
  203. if (pull_dir == UNIPHIER_PIN_PULL_NONE)
  204. return 0;
  205. if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
  206. pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
  207. return -EINVAL;
  208. expected = 0;
  209. break;
  210. case PIN_CONFIG_BIAS_PULL_UP:
  211. if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED)
  212. return 0;
  213. if (pull_dir != UNIPHIER_PIN_PULL_UP)
  214. return -EINVAL;
  215. break;
  216. case PIN_CONFIG_BIAS_PULL_DOWN:
  217. if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
  218. return 0;
  219. if (pull_dir != UNIPHIER_PIN_PULL_DOWN)
  220. return -EINVAL;
  221. break;
  222. default:
  223. BUG();
  224. }
  225. pupdctrl = uniphier_pin_get_pupdctrl(desc->drv_data);
  226. reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
  227. shift = pupdctrl % 32;
  228. ret = regmap_read(priv->regmap, reg, &val);
  229. if (ret)
  230. return ret;
  231. val = (val >> shift) & 1;
  232. return (val == expected) ? 0 : -EINVAL;
  233. }
  234. static int uniphier_conf_pin_drive_get(struct pinctrl_dev *pctldev,
  235. unsigned int pin, u32 *strength)
  236. {
  237. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  238. unsigned int reg, shift, mask, val;
  239. const unsigned int *strengths;
  240. int ret;
  241. ret = uniphier_conf_get_drvctrl_data(pctldev, pin, &reg, &shift,
  242. &mask, &strengths);
  243. if (ret)
  244. return ret;
  245. if (mask) {
  246. ret = regmap_read(priv->regmap, reg, &val);
  247. if (ret)
  248. return ret;
  249. } else {
  250. val = 0;
  251. }
  252. *strength = strengths[(val >> shift) & mask];
  253. return 0;
  254. }
  255. static int uniphier_conf_pin_input_enable_get(struct pinctrl_dev *pctldev,
  256. unsigned int pin)
  257. {
  258. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  259. const struct pin_desc *desc = pin_desc_get(pctldev, pin);
  260. unsigned int iectrl = uniphier_pin_get_iectrl(desc->drv_data);
  261. unsigned int reg, mask, val;
  262. int ret;
  263. if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
  264. /* This pin is always input-enabled. */
  265. return 0;
  266. if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL)
  267. iectrl = pin;
  268. reg = UNIPHIER_PINCTRL_IECTRL_BASE + iectrl / 32 * 4;
  269. mask = BIT(iectrl % 32);
  270. ret = regmap_read(priv->regmap, reg, &val);
  271. if (ret)
  272. return ret;
  273. return val & mask ? 0 : -EINVAL;
  274. }
  275. static int uniphier_conf_pin_config_get(struct pinctrl_dev *pctldev,
  276. unsigned pin,
  277. unsigned long *configs)
  278. {
  279. enum pin_config_param param = pinconf_to_config_param(*configs);
  280. bool has_arg = false;
  281. u32 arg;
  282. int ret;
  283. switch (param) {
  284. case PIN_CONFIG_BIAS_DISABLE:
  285. case PIN_CONFIG_BIAS_PULL_UP:
  286. case PIN_CONFIG_BIAS_PULL_DOWN:
  287. ret = uniphier_conf_pin_bias_get(pctldev, pin, param);
  288. break;
  289. case PIN_CONFIG_DRIVE_STRENGTH:
  290. ret = uniphier_conf_pin_drive_get(pctldev, pin, &arg);
  291. has_arg = true;
  292. break;
  293. case PIN_CONFIG_INPUT_ENABLE:
  294. ret = uniphier_conf_pin_input_enable_get(pctldev, pin);
  295. break;
  296. default:
  297. /* unsupported parameter */
  298. ret = -EINVAL;
  299. break;
  300. }
  301. if (ret == 0 && has_arg)
  302. *configs = pinconf_to_config_packed(param, arg);
  303. return ret;
  304. }
  305. static int uniphier_conf_pin_bias_set(struct pinctrl_dev *pctldev,
  306. unsigned int pin,
  307. enum pin_config_param param, u32 arg)
  308. {
  309. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  310. const struct pin_desc *desc = pin_desc_get(pctldev, pin);
  311. enum uniphier_pin_pull_dir pull_dir =
  312. uniphier_pin_get_pull_dir(desc->drv_data);
  313. unsigned int pupdctrl, reg, shift;
  314. unsigned int val = 1;
  315. switch (param) {
  316. case PIN_CONFIG_BIAS_DISABLE:
  317. if (pull_dir == UNIPHIER_PIN_PULL_NONE)
  318. return 0;
  319. if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
  320. pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED) {
  321. dev_err(pctldev->dev,
  322. "can not disable pull register for pin %s\n",
  323. desc->name);
  324. return -EINVAL;
  325. }
  326. val = 0;
  327. break;
  328. case PIN_CONFIG_BIAS_PULL_UP:
  329. if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED && arg != 0)
  330. return 0;
  331. if (pull_dir != UNIPHIER_PIN_PULL_UP) {
  332. dev_err(pctldev->dev,
  333. "pull-up is unsupported for pin %s\n",
  334. desc->name);
  335. return -EINVAL;
  336. }
  337. if (arg == 0) {
  338. dev_err(pctldev->dev, "pull-up can not be total\n");
  339. return -EINVAL;
  340. }
  341. break;
  342. case PIN_CONFIG_BIAS_PULL_DOWN:
  343. if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED && arg != 0)
  344. return 0;
  345. if (pull_dir != UNIPHIER_PIN_PULL_DOWN) {
  346. dev_err(pctldev->dev,
  347. "pull-down is unsupported for pin %s\n",
  348. desc->name);
  349. return -EINVAL;
  350. }
  351. if (arg == 0) {
  352. dev_err(pctldev->dev, "pull-down can not be total\n");
  353. return -EINVAL;
  354. }
  355. break;
  356. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  357. if (pull_dir == UNIPHIER_PIN_PULL_NONE) {
  358. dev_err(pctldev->dev,
  359. "pull-up/down is unsupported for pin %s\n",
  360. desc->name);
  361. return -EINVAL;
  362. }
  363. if (arg == 0)
  364. return 0; /* configuration ingored */
  365. break;
  366. default:
  367. BUG();
  368. }
  369. pupdctrl = uniphier_pin_get_pupdctrl(desc->drv_data);
  370. reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
  371. shift = pupdctrl % 32;
  372. return regmap_update_bits(priv->regmap, reg, 1 << shift, val << shift);
  373. }
  374. static int uniphier_conf_pin_drive_set(struct pinctrl_dev *pctldev,
  375. unsigned int pin, u32 strength)
  376. {
  377. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  378. const struct pin_desc *desc = pin_desc_get(pctldev, pin);
  379. unsigned int reg, shift, mask, val;
  380. const unsigned int *strengths;
  381. int ret;
  382. ret = uniphier_conf_get_drvctrl_data(pctldev, pin, &reg, &shift,
  383. &mask, &strengths);
  384. if (ret) {
  385. dev_err(pctldev->dev, "cannot set drive strength for pin %s\n",
  386. desc->name);
  387. return ret;
  388. }
  389. for (val = 0; val <= mask; val++) {
  390. if (strengths[val] > strength)
  391. break;
  392. }
  393. if (val == 0) {
  394. dev_err(pctldev->dev,
  395. "unsupported drive strength %u mA for pin %s\n",
  396. strength, desc->name);
  397. return -EINVAL;
  398. }
  399. if (!mask)
  400. return 0;
  401. val--;
  402. return regmap_update_bits(priv->regmap, reg,
  403. mask << shift, val << shift);
  404. }
  405. static int uniphier_conf_pin_input_enable(struct pinctrl_dev *pctldev,
  406. unsigned int pin, u32 enable)
  407. {
  408. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  409. const struct pin_desc *desc = pin_desc_get(pctldev, pin);
  410. unsigned int iectrl = uniphier_pin_get_iectrl(desc->drv_data);
  411. unsigned int reg, mask;
  412. /*
  413. * Multiple pins share one input enable, per-pin disabling is
  414. * impossible.
  415. */
  416. if (!(priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL) &&
  417. !enable)
  418. return -EINVAL;
  419. /* UNIPHIER_PIN_IECTRL_NONE means the pin is always input-enabled */
  420. if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
  421. return enable ? 0 : -EINVAL;
  422. if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL)
  423. iectrl = pin;
  424. reg = UNIPHIER_PINCTRL_IECTRL_BASE + iectrl / 32 * 4;
  425. mask = BIT(iectrl % 32);
  426. return regmap_update_bits(priv->regmap, reg, mask, enable ? mask : 0);
  427. }
  428. static int uniphier_conf_pin_config_set(struct pinctrl_dev *pctldev,
  429. unsigned pin,
  430. unsigned long *configs,
  431. unsigned num_configs)
  432. {
  433. int i, ret;
  434. for (i = 0; i < num_configs; i++) {
  435. enum pin_config_param param =
  436. pinconf_to_config_param(configs[i]);
  437. u32 arg = pinconf_to_config_argument(configs[i]);
  438. switch (param) {
  439. case PIN_CONFIG_BIAS_DISABLE:
  440. case PIN_CONFIG_BIAS_PULL_UP:
  441. case PIN_CONFIG_BIAS_PULL_DOWN:
  442. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  443. ret = uniphier_conf_pin_bias_set(pctldev, pin,
  444. param, arg);
  445. break;
  446. case PIN_CONFIG_DRIVE_STRENGTH:
  447. ret = uniphier_conf_pin_drive_set(pctldev, pin, arg);
  448. break;
  449. case PIN_CONFIG_INPUT_ENABLE:
  450. ret = uniphier_conf_pin_input_enable(pctldev, pin, arg);
  451. break;
  452. default:
  453. dev_err(pctldev->dev,
  454. "unsupported configuration parameter %u\n",
  455. param);
  456. return -EINVAL;
  457. }
  458. if (ret)
  459. return ret;
  460. }
  461. return 0;
  462. }
  463. static int uniphier_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
  464. unsigned selector,
  465. unsigned long *configs,
  466. unsigned num_configs)
  467. {
  468. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  469. const unsigned *pins = priv->socdata->groups[selector].pins;
  470. unsigned num_pins = priv->socdata->groups[selector].num_pins;
  471. int i, ret;
  472. for (i = 0; i < num_pins; i++) {
  473. ret = uniphier_conf_pin_config_set(pctldev, pins[i],
  474. configs, num_configs);
  475. if (ret)
  476. return ret;
  477. }
  478. return 0;
  479. }
  480. static const struct pinconf_ops uniphier_confops = {
  481. .is_generic = true,
  482. .pin_config_get = uniphier_conf_pin_config_get,
  483. .pin_config_set = uniphier_conf_pin_config_set,
  484. .pin_config_group_set = uniphier_conf_pin_config_group_set,
  485. };
  486. static int uniphier_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  487. {
  488. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  489. return priv->socdata->functions_count;
  490. }
  491. static const char *uniphier_pmx_get_function_name(struct pinctrl_dev *pctldev,
  492. unsigned selector)
  493. {
  494. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  495. return priv->socdata->functions[selector].name;
  496. }
  497. static int uniphier_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  498. unsigned selector,
  499. const char * const **groups,
  500. unsigned *num_groups)
  501. {
  502. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  503. *groups = priv->socdata->functions[selector].groups;
  504. *num_groups = priv->socdata->functions[selector].num_groups;
  505. return 0;
  506. }
  507. static int uniphier_pmx_set_one_mux(struct pinctrl_dev *pctldev, unsigned pin,
  508. int muxval)
  509. {
  510. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  511. unsigned int mux_bits, reg_stride, reg, reg_end, shift, mask;
  512. bool load_pinctrl;
  513. int ret;
  514. /* some pins need input-enabling */
  515. ret = uniphier_conf_pin_input_enable(pctldev, pin, 1);
  516. if (ret)
  517. return ret;
  518. if (muxval < 0)
  519. return 0; /* dedicated pin; nothing to do for pin-mux */
  520. if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
  521. /*
  522. * Mode reg_offset bit_position
  523. * Normal 4 * n shift+3:shift
  524. * Debug 4 * n shift+7:shift+4
  525. */
  526. mux_bits = 4;
  527. reg_stride = 8;
  528. load_pinctrl = true;
  529. } else {
  530. /*
  531. * Mode reg_offset bit_position
  532. * Normal 8 * n shift+3:shift
  533. * Debug 8 * n + 4 shift+3:shift
  534. */
  535. mux_bits = 8;
  536. reg_stride = 4;
  537. load_pinctrl = false;
  538. }
  539. reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
  540. reg_end = reg + reg_stride;
  541. shift = pin * mux_bits % 32;
  542. mask = (1U << mux_bits) - 1;
  543. /*
  544. * If reg_stride is greater than 4, the MSB of each pinsel shall be
  545. * stored in the offset+4.
  546. */
  547. for (; reg < reg_end; reg += 4) {
  548. ret = regmap_update_bits(priv->regmap, reg,
  549. mask << shift, muxval << shift);
  550. if (ret)
  551. return ret;
  552. muxval >>= mux_bits;
  553. }
  554. if (load_pinctrl) {
  555. ret = regmap_write(priv->regmap,
  556. UNIPHIER_PINCTRL_LOAD_PINMUX, 1);
  557. if (ret)
  558. return ret;
  559. }
  560. return 0;
  561. }
  562. static int uniphier_pmx_set_mux(struct pinctrl_dev *pctldev,
  563. unsigned func_selector,
  564. unsigned group_selector)
  565. {
  566. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  567. const struct uniphier_pinctrl_group *grp =
  568. &priv->socdata->groups[group_selector];
  569. int i;
  570. int ret;
  571. for (i = 0; i < grp->num_pins; i++) {
  572. ret = uniphier_pmx_set_one_mux(pctldev, grp->pins[i],
  573. grp->muxvals[i]);
  574. if (ret)
  575. return ret;
  576. }
  577. return 0;
  578. }
  579. static int uniphier_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
  580. struct pinctrl_gpio_range *range,
  581. unsigned offset)
  582. {
  583. struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
  584. unsigned int gpio_offset;
  585. int muxval, i;
  586. if (range->pins) {
  587. for (i = 0; i < range->npins; i++)
  588. if (range->pins[i] == offset)
  589. break;
  590. if (WARN_ON(i == range->npins))
  591. return -EINVAL;
  592. gpio_offset = i;
  593. } else {
  594. gpio_offset = offset - range->pin_base;
  595. }
  596. gpio_offset += range->id;
  597. muxval = priv->socdata->get_gpio_muxval(offset, gpio_offset);
  598. return uniphier_pmx_set_one_mux(pctldev, offset, muxval);
  599. }
  600. static const struct pinmux_ops uniphier_pmxops = {
  601. .get_functions_count = uniphier_pmx_get_functions_count,
  602. .get_function_name = uniphier_pmx_get_function_name,
  603. .get_function_groups = uniphier_pmx_get_function_groups,
  604. .set_mux = uniphier_pmx_set_mux,
  605. .gpio_request_enable = uniphier_pmx_gpio_request_enable,
  606. .strict = true,
  607. };
  608. #ifdef CONFIG_PM_SLEEP
  609. static int uniphier_pinctrl_suspend(struct device *dev)
  610. {
  611. struct uniphier_pinctrl_priv *priv = dev_get_drvdata(dev);
  612. struct uniphier_pinctrl_reg_region *r;
  613. int ret;
  614. list_for_each_entry(r, &priv->reg_regions, node) {
  615. ret = regmap_bulk_read(priv->regmap, r->base, r->vals,
  616. r->nregs);
  617. if (ret)
  618. return ret;
  619. }
  620. return 0;
  621. }
  622. static int uniphier_pinctrl_resume(struct device *dev)
  623. {
  624. struct uniphier_pinctrl_priv *priv = dev_get_drvdata(dev);
  625. struct uniphier_pinctrl_reg_region *r;
  626. int ret;
  627. list_for_each_entry(r, &priv->reg_regions, node) {
  628. ret = regmap_bulk_write(priv->regmap, r->base, r->vals,
  629. r->nregs);
  630. if (ret)
  631. return ret;
  632. }
  633. if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
  634. ret = regmap_write(priv->regmap,
  635. UNIPHIER_PINCTRL_LOAD_PINMUX, 1);
  636. if (ret)
  637. return ret;
  638. }
  639. return 0;
  640. }
  641. static int uniphier_pinctrl_add_reg_region(struct device *dev,
  642. struct uniphier_pinctrl_priv *priv,
  643. unsigned int base,
  644. unsigned int count,
  645. unsigned int width)
  646. {
  647. struct uniphier_pinctrl_reg_region *region;
  648. unsigned int nregs;
  649. if (!count)
  650. return 0;
  651. nregs = DIV_ROUND_UP(count * width, 32);
  652. region = devm_kzalloc(dev, struct_size(region, vals, nregs),
  653. GFP_KERNEL);
  654. if (!region)
  655. return -ENOMEM;
  656. region->base = base;
  657. region->nregs = nregs;
  658. list_add_tail(&region->node, &priv->reg_regions);
  659. return 0;
  660. }
  661. #endif
  662. static int uniphier_pinctrl_pm_init(struct device *dev,
  663. struct uniphier_pinctrl_priv *priv)
  664. {
  665. #ifdef CONFIG_PM_SLEEP
  666. const struct uniphier_pinctrl_socdata *socdata = priv->socdata;
  667. unsigned int num_drvctrl = 0;
  668. unsigned int num_drv2ctrl = 0;
  669. unsigned int num_drv3ctrl = 0;
  670. unsigned int num_pupdctrl = 0;
  671. unsigned int num_iectrl = 0;
  672. unsigned int iectrl, drvctrl, pupdctrl;
  673. enum uniphier_pin_drv_type drv_type;
  674. enum uniphier_pin_pull_dir pull_dir;
  675. int i, ret;
  676. for (i = 0; i < socdata->npins; i++) {
  677. void *drv_data = socdata->pins[i].drv_data;
  678. drvctrl = uniphier_pin_get_drvctrl(drv_data);
  679. drv_type = uniphier_pin_get_drv_type(drv_data);
  680. pupdctrl = uniphier_pin_get_pupdctrl(drv_data);
  681. pull_dir = uniphier_pin_get_pull_dir(drv_data);
  682. iectrl = uniphier_pin_get_iectrl(drv_data);
  683. switch (drv_type) {
  684. case UNIPHIER_PIN_DRV_1BIT:
  685. num_drvctrl = max(num_drvctrl, drvctrl + 1);
  686. break;
  687. case UNIPHIER_PIN_DRV_2BIT:
  688. num_drv2ctrl = max(num_drv2ctrl, drvctrl + 1);
  689. break;
  690. case UNIPHIER_PIN_DRV_3BIT:
  691. num_drv3ctrl = max(num_drv3ctrl, drvctrl + 1);
  692. break;
  693. default:
  694. break;
  695. }
  696. if (pull_dir == UNIPHIER_PIN_PULL_UP ||
  697. pull_dir == UNIPHIER_PIN_PULL_DOWN)
  698. num_pupdctrl = max(num_pupdctrl, pupdctrl + 1);
  699. if (iectrl != UNIPHIER_PIN_IECTRL_NONE) {
  700. if (socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL)
  701. iectrl = i;
  702. num_iectrl = max(num_iectrl, iectrl + 1);
  703. }
  704. }
  705. INIT_LIST_HEAD(&priv->reg_regions);
  706. ret = uniphier_pinctrl_add_reg_region(dev, priv,
  707. UNIPHIER_PINCTRL_PINMUX_BASE,
  708. socdata->npins, 8);
  709. if (ret)
  710. return ret;
  711. ret = uniphier_pinctrl_add_reg_region(dev, priv,
  712. UNIPHIER_PINCTRL_DRVCTRL_BASE,
  713. num_drvctrl, 1);
  714. if (ret)
  715. return ret;
  716. ret = uniphier_pinctrl_add_reg_region(dev, priv,
  717. UNIPHIER_PINCTRL_DRV2CTRL_BASE,
  718. num_drv2ctrl, 2);
  719. if (ret)
  720. return ret;
  721. ret = uniphier_pinctrl_add_reg_region(dev, priv,
  722. UNIPHIER_PINCTRL_DRV3CTRL_BASE,
  723. num_drv3ctrl, 3);
  724. if (ret)
  725. return ret;
  726. ret = uniphier_pinctrl_add_reg_region(dev, priv,
  727. UNIPHIER_PINCTRL_PUPDCTRL_BASE,
  728. num_pupdctrl, 1);
  729. if (ret)
  730. return ret;
  731. ret = uniphier_pinctrl_add_reg_region(dev, priv,
  732. UNIPHIER_PINCTRL_IECTRL_BASE,
  733. num_iectrl, 1);
  734. if (ret)
  735. return ret;
  736. #endif
  737. return 0;
  738. }
  739. const struct dev_pm_ops uniphier_pinctrl_pm_ops = {
  740. SET_LATE_SYSTEM_SLEEP_PM_OPS(uniphier_pinctrl_suspend,
  741. uniphier_pinctrl_resume)
  742. };
  743. int uniphier_pinctrl_probe(struct platform_device *pdev,
  744. struct uniphier_pinctrl_socdata *socdata)
  745. {
  746. struct device *dev = &pdev->dev;
  747. struct uniphier_pinctrl_priv *priv;
  748. struct device_node *parent;
  749. int ret;
  750. if (!socdata ||
  751. !socdata->pins || !socdata->npins ||
  752. !socdata->groups || !socdata->groups_count ||
  753. !socdata->functions || !socdata->functions_count) {
  754. dev_err(dev, "pinctrl socdata lacks necessary members\n");
  755. return -EINVAL;
  756. }
  757. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  758. if (!priv)
  759. return -ENOMEM;
  760. parent = of_get_parent(dev->of_node);
  761. priv->regmap = syscon_node_to_regmap(parent);
  762. of_node_put(parent);
  763. if (IS_ERR(priv->regmap)) {
  764. dev_err(dev, "failed to get regmap\n");
  765. return PTR_ERR(priv->regmap);
  766. }
  767. priv->socdata = socdata;
  768. priv->pctldesc.name = dev->driver->name;
  769. priv->pctldesc.pins = socdata->pins;
  770. priv->pctldesc.npins = socdata->npins;
  771. priv->pctldesc.pctlops = &uniphier_pctlops;
  772. priv->pctldesc.pmxops = &uniphier_pmxops;
  773. priv->pctldesc.confops = &uniphier_confops;
  774. priv->pctldesc.owner = dev->driver->owner;
  775. ret = uniphier_pinctrl_pm_init(dev, priv);
  776. if (ret)
  777. return ret;
  778. priv->pctldev = devm_pinctrl_register(dev, &priv->pctldesc, priv);
  779. if (IS_ERR(priv->pctldev)) {
  780. dev_err(dev, "failed to register UniPhier pinctrl driver\n");
  781. return PTR_ERR(priv->pctldev);
  782. }
  783. platform_set_drvdata(pdev, priv);
  784. return 0;
  785. }