pinctrl-zynqmp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ZynqMP pin controller
  4. *
  5. * Copyright (C) 2020, 2021 Xilinx, Inc.
  6. *
  7. * Sai Krishna Potthuri <[email protected]>
  8. * Rajan Vaja <[email protected]>
  9. */
  10. #include <dt-bindings/pinctrl/pinctrl-zynqmp.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/firmware/xlnx-zynqmp.h>
  16. #include <linux/pinctrl/pinmux.h>
  17. #include <linux/pinctrl/pinconf-generic.h>
  18. #include "core.h"
  19. #include "pinctrl-utils.h"
  20. #define ZYNQMP_PIN_PREFIX "MIO"
  21. #define PINCTRL_GET_FUNC_NAME_RESP_LEN 16
  22. #define MAX_FUNC_NAME_LEN 16
  23. #define MAX_GROUP_PIN 50
  24. #define MAX_PIN_GROUPS 50
  25. #define END_OF_FUNCTIONS "END_OF_FUNCTIONS"
  26. #define NUM_GROUPS_PER_RESP 6
  27. #define PINCTRL_GET_FUNC_GROUPS_RESP_LEN 12
  28. #define PINCTRL_GET_PIN_GROUPS_RESP_LEN 12
  29. #define NA_GROUP 0xFFFF
  30. #define RESERVED_GROUP 0xFFFE
  31. #define DRIVE_STRENGTH_2MA 2
  32. #define DRIVE_STRENGTH_4MA 4
  33. #define DRIVE_STRENGTH_8MA 8
  34. #define DRIVE_STRENGTH_12MA 12
  35. /**
  36. * struct zynqmp_pmux_function - a pinmux function
  37. * @name: Name of the pin mux function
  38. * @groups: List of pin groups for this function
  39. * @ngroups: Number of entries in @groups
  40. * @node: Firmware node matching with the function
  41. *
  42. * This structure holds information about pin control function
  43. * and function group names supporting that function.
  44. */
  45. struct zynqmp_pmux_function {
  46. char name[MAX_FUNC_NAME_LEN];
  47. const char * const *groups;
  48. unsigned int ngroups;
  49. };
  50. /**
  51. * struct zynqmp_pinctrl - driver data
  52. * @pctrl: Pin control device
  53. * @groups: Pin groups
  54. * @ngroups: Number of @groups
  55. * @funcs: Pin mux functions
  56. * @nfuncs: Number of @funcs
  57. *
  58. * This struct is stored as driver data and used to retrieve
  59. * information regarding pin control functions, groups and
  60. * group pins.
  61. */
  62. struct zynqmp_pinctrl {
  63. struct pinctrl_dev *pctrl;
  64. const struct zynqmp_pctrl_group *groups;
  65. unsigned int ngroups;
  66. const struct zynqmp_pmux_function *funcs;
  67. unsigned int nfuncs;
  68. };
  69. /**
  70. * struct zynqmp_pctrl_group - Pin control group info
  71. * @name: Group name
  72. * @pins: Group pin numbers
  73. * @npins: Number of pins in the group
  74. */
  75. struct zynqmp_pctrl_group {
  76. const char *name;
  77. unsigned int pins[MAX_GROUP_PIN];
  78. unsigned int npins;
  79. };
  80. static struct pinctrl_desc zynqmp_desc;
  81. static int zynqmp_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  82. {
  83. struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  84. return pctrl->ngroups;
  85. }
  86. static const char *zynqmp_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  87. unsigned int selector)
  88. {
  89. struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  90. return pctrl->groups[selector].name;
  91. }
  92. static int zynqmp_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  93. unsigned int selector,
  94. const unsigned int **pins,
  95. unsigned int *npins)
  96. {
  97. struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  98. *pins = pctrl->groups[selector].pins;
  99. *npins = pctrl->groups[selector].npins;
  100. return 0;
  101. }
  102. static const struct pinctrl_ops zynqmp_pctrl_ops = {
  103. .get_groups_count = zynqmp_pctrl_get_groups_count,
  104. .get_group_name = zynqmp_pctrl_get_group_name,
  105. .get_group_pins = zynqmp_pctrl_get_group_pins,
  106. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  107. .dt_free_map = pinctrl_utils_free_map,
  108. };
  109. static int zynqmp_pinmux_request_pin(struct pinctrl_dev *pctldev,
  110. unsigned int pin)
  111. {
  112. int ret;
  113. ret = zynqmp_pm_pinctrl_request(pin);
  114. if (ret) {
  115. dev_err(pctldev->dev, "request failed for pin %u\n", pin);
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. static int zynqmp_pmux_get_functions_count(struct pinctrl_dev *pctldev)
  121. {
  122. struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  123. return pctrl->nfuncs;
  124. }
  125. static const char *zynqmp_pmux_get_function_name(struct pinctrl_dev *pctldev,
  126. unsigned int selector)
  127. {
  128. struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  129. return pctrl->funcs[selector].name;
  130. }
  131. /**
  132. * zynqmp_pmux_get_function_groups() - Get groups for the function
  133. * @pctldev: Pincontrol device pointer.
  134. * @selector: Function ID
  135. * @groups: Group names.
  136. * @num_groups: Number of function groups.
  137. *
  138. * Get function's group count and group names.
  139. *
  140. * Return: 0
  141. */
  142. static int zynqmp_pmux_get_function_groups(struct pinctrl_dev *pctldev,
  143. unsigned int selector,
  144. const char * const **groups,
  145. unsigned * const num_groups)
  146. {
  147. struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  148. *groups = pctrl->funcs[selector].groups;
  149. *num_groups = pctrl->funcs[selector].ngroups;
  150. return 0;
  151. }
  152. /**
  153. * zynqmp_pinmux_set_mux() - Set requested function for the group
  154. * @pctldev: Pincontrol device pointer.
  155. * @function: Function ID.
  156. * @group: Group ID.
  157. *
  158. * Loop through all pins of the group and call firmware API
  159. * to set requested function for all pins in the group.
  160. *
  161. * Return: 0 on success else error code.
  162. */
  163. static int zynqmp_pinmux_set_mux(struct pinctrl_dev *pctldev,
  164. unsigned int function,
  165. unsigned int group)
  166. {
  167. struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  168. const struct zynqmp_pctrl_group *pgrp = &pctrl->groups[group];
  169. int ret, i;
  170. for (i = 0; i < pgrp->npins; i++) {
  171. unsigned int pin = pgrp->pins[i];
  172. ret = zynqmp_pm_pinctrl_set_function(pin, function);
  173. if (ret) {
  174. dev_err(pctldev->dev, "set mux failed for pin %u\n",
  175. pin);
  176. return ret;
  177. }
  178. }
  179. return 0;
  180. }
  181. static int zynqmp_pinmux_release_pin(struct pinctrl_dev *pctldev,
  182. unsigned int pin)
  183. {
  184. int ret;
  185. ret = zynqmp_pm_pinctrl_release(pin);
  186. if (ret) {
  187. dev_err(pctldev->dev, "free pin failed for pin %u\n",
  188. pin);
  189. return ret;
  190. }
  191. return 0;
  192. }
  193. static const struct pinmux_ops zynqmp_pinmux_ops = {
  194. .request = zynqmp_pinmux_request_pin,
  195. .get_functions_count = zynqmp_pmux_get_functions_count,
  196. .get_function_name = zynqmp_pmux_get_function_name,
  197. .get_function_groups = zynqmp_pmux_get_function_groups,
  198. .set_mux = zynqmp_pinmux_set_mux,
  199. .free = zynqmp_pinmux_release_pin,
  200. };
  201. /**
  202. * zynqmp_pinconf_cfg_get() - get config value for the pin
  203. * @pctldev: Pin control device pointer.
  204. * @pin: Pin number.
  205. * @config: Value of config param.
  206. *
  207. * Get value of the requested configuration parameter for the
  208. * given pin.
  209. *
  210. * Return: 0 on success else error code.
  211. */
  212. static int zynqmp_pinconf_cfg_get(struct pinctrl_dev *pctldev,
  213. unsigned int pin,
  214. unsigned long *config)
  215. {
  216. unsigned int arg, param = pinconf_to_config_param(*config);
  217. int ret;
  218. switch (param) {
  219. case PIN_CONFIG_SLEW_RATE:
  220. param = PM_PINCTRL_CONFIG_SLEW_RATE;
  221. ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg);
  222. break;
  223. case PIN_CONFIG_BIAS_PULL_UP:
  224. param = PM_PINCTRL_CONFIG_PULL_CTRL;
  225. ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg);
  226. if (arg != PM_PINCTRL_BIAS_PULL_UP)
  227. return -EINVAL;
  228. arg = 1;
  229. break;
  230. case PIN_CONFIG_BIAS_PULL_DOWN:
  231. param = PM_PINCTRL_CONFIG_PULL_CTRL;
  232. ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg);
  233. if (arg != PM_PINCTRL_BIAS_PULL_DOWN)
  234. return -EINVAL;
  235. arg = 1;
  236. break;
  237. case PIN_CONFIG_BIAS_DISABLE:
  238. param = PM_PINCTRL_CONFIG_BIAS_STATUS;
  239. ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg);
  240. if (arg != PM_PINCTRL_BIAS_DISABLE)
  241. return -EINVAL;
  242. arg = 1;
  243. break;
  244. case PIN_CONFIG_POWER_SOURCE:
  245. param = PM_PINCTRL_CONFIG_VOLTAGE_STATUS;
  246. ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg);
  247. break;
  248. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  249. param = PM_PINCTRL_CONFIG_SCHMITT_CMOS;
  250. ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg);
  251. break;
  252. case PIN_CONFIG_DRIVE_STRENGTH:
  253. param = PM_PINCTRL_CONFIG_DRIVE_STRENGTH;
  254. ret = zynqmp_pm_pinctrl_get_config(pin, param, &arg);
  255. switch (arg) {
  256. case PM_PINCTRL_DRIVE_STRENGTH_2MA:
  257. arg = DRIVE_STRENGTH_2MA;
  258. break;
  259. case PM_PINCTRL_DRIVE_STRENGTH_4MA:
  260. arg = DRIVE_STRENGTH_4MA;
  261. break;
  262. case PM_PINCTRL_DRIVE_STRENGTH_8MA:
  263. arg = DRIVE_STRENGTH_8MA;
  264. break;
  265. case PM_PINCTRL_DRIVE_STRENGTH_12MA:
  266. arg = DRIVE_STRENGTH_12MA;
  267. break;
  268. default:
  269. /* Invalid drive strength */
  270. dev_warn(pctldev->dev,
  271. "Invalid drive strength for pin %d\n",
  272. pin);
  273. return -EINVAL;
  274. }
  275. break;
  276. default:
  277. ret = -ENOTSUPP;
  278. break;
  279. }
  280. if (ret)
  281. return ret;
  282. param = pinconf_to_config_param(*config);
  283. *config = pinconf_to_config_packed(param, arg);
  284. return 0;
  285. }
  286. /**
  287. * zynqmp_pinconf_cfg_set() - Set requested config for the pin
  288. * @pctldev: Pincontrol device pointer.
  289. * @pin: Pin number.
  290. * @configs: Configuration to set.
  291. * @num_configs: Number of configurations.
  292. *
  293. * Loop through all configurations and call firmware API
  294. * to set requested configurations for the pin.
  295. *
  296. * Return: 0 on success else error code.
  297. */
  298. static int zynqmp_pinconf_cfg_set(struct pinctrl_dev *pctldev,
  299. unsigned int pin, unsigned long *configs,
  300. unsigned int num_configs)
  301. {
  302. int i, ret;
  303. for (i = 0; i < num_configs; i++) {
  304. unsigned int param = pinconf_to_config_param(configs[i]);
  305. unsigned int arg = pinconf_to_config_argument(configs[i]);
  306. unsigned int value;
  307. switch (param) {
  308. case PIN_CONFIG_SLEW_RATE:
  309. param = PM_PINCTRL_CONFIG_SLEW_RATE;
  310. ret = zynqmp_pm_pinctrl_set_config(pin, param, arg);
  311. break;
  312. case PIN_CONFIG_BIAS_PULL_UP:
  313. param = PM_PINCTRL_CONFIG_PULL_CTRL;
  314. arg = PM_PINCTRL_BIAS_PULL_UP;
  315. ret = zynqmp_pm_pinctrl_set_config(pin, param, arg);
  316. break;
  317. case PIN_CONFIG_BIAS_PULL_DOWN:
  318. param = PM_PINCTRL_CONFIG_PULL_CTRL;
  319. arg = PM_PINCTRL_BIAS_PULL_DOWN;
  320. ret = zynqmp_pm_pinctrl_set_config(pin, param, arg);
  321. break;
  322. case PIN_CONFIG_BIAS_DISABLE:
  323. param = PM_PINCTRL_CONFIG_BIAS_STATUS;
  324. arg = PM_PINCTRL_BIAS_DISABLE;
  325. ret = zynqmp_pm_pinctrl_set_config(pin, param, arg);
  326. break;
  327. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  328. param = PM_PINCTRL_CONFIG_SCHMITT_CMOS;
  329. ret = zynqmp_pm_pinctrl_set_config(pin, param, arg);
  330. break;
  331. case PIN_CONFIG_DRIVE_STRENGTH:
  332. switch (arg) {
  333. case DRIVE_STRENGTH_2MA:
  334. value = PM_PINCTRL_DRIVE_STRENGTH_2MA;
  335. break;
  336. case DRIVE_STRENGTH_4MA:
  337. value = PM_PINCTRL_DRIVE_STRENGTH_4MA;
  338. break;
  339. case DRIVE_STRENGTH_8MA:
  340. value = PM_PINCTRL_DRIVE_STRENGTH_8MA;
  341. break;
  342. case DRIVE_STRENGTH_12MA:
  343. value = PM_PINCTRL_DRIVE_STRENGTH_12MA;
  344. break;
  345. default:
  346. /* Invalid drive strength */
  347. dev_warn(pctldev->dev,
  348. "Invalid drive strength for pin %d\n",
  349. pin);
  350. return -EINVAL;
  351. }
  352. param = PM_PINCTRL_CONFIG_DRIVE_STRENGTH;
  353. ret = zynqmp_pm_pinctrl_set_config(pin, param, value);
  354. break;
  355. case PIN_CONFIG_POWER_SOURCE:
  356. param = PM_PINCTRL_CONFIG_VOLTAGE_STATUS;
  357. ret = zynqmp_pm_pinctrl_get_config(pin, param, &value);
  358. if (arg != value)
  359. dev_warn(pctldev->dev,
  360. "Invalid IO Standard requested for pin %d\n",
  361. pin);
  362. break;
  363. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  364. case PIN_CONFIG_MODE_LOW_POWER:
  365. /*
  366. * These cases are mentioned in dts but configurable
  367. * registers are unknown. So falling through to ignore
  368. * boot time warnings as of now.
  369. */
  370. ret = 0;
  371. break;
  372. default:
  373. dev_warn(pctldev->dev,
  374. "unsupported configuration parameter '%u'\n",
  375. param);
  376. ret = -ENOTSUPP;
  377. break;
  378. }
  379. param = pinconf_to_config_param(configs[i]);
  380. arg = pinconf_to_config_argument(configs[i]);
  381. if (ret)
  382. dev_warn(pctldev->dev,
  383. "failed to set: pin %u param %u value %u\n",
  384. pin, param, arg);
  385. }
  386. return 0;
  387. }
  388. /**
  389. * zynqmp_pinconf_group_set() - Set requested config for the group
  390. * @pctldev: Pincontrol device pointer.
  391. * @selector: Group ID.
  392. * @configs: Configuration to set.
  393. * @num_configs: Number of configurations.
  394. *
  395. * Call function to set configs for each pin in the group.
  396. *
  397. * Return: 0 on success else error code.
  398. */
  399. static int zynqmp_pinconf_group_set(struct pinctrl_dev *pctldev,
  400. unsigned int selector,
  401. unsigned long *configs,
  402. unsigned int num_configs)
  403. {
  404. int i, ret;
  405. struct zynqmp_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  406. const struct zynqmp_pctrl_group *pgrp = &pctrl->groups[selector];
  407. for (i = 0; i < pgrp->npins; i++) {
  408. ret = zynqmp_pinconf_cfg_set(pctldev, pgrp->pins[i], configs,
  409. num_configs);
  410. if (ret)
  411. return ret;
  412. }
  413. return 0;
  414. }
  415. static const struct pinconf_ops zynqmp_pinconf_ops = {
  416. .is_generic = true,
  417. .pin_config_get = zynqmp_pinconf_cfg_get,
  418. .pin_config_set = zynqmp_pinconf_cfg_set,
  419. .pin_config_group_set = zynqmp_pinconf_group_set,
  420. };
  421. static struct pinctrl_desc zynqmp_desc = {
  422. .name = "zynqmp_pinctrl",
  423. .owner = THIS_MODULE,
  424. .pctlops = &zynqmp_pctrl_ops,
  425. .pmxops = &zynqmp_pinmux_ops,
  426. .confops = &zynqmp_pinconf_ops,
  427. };
  428. static int zynqmp_pinctrl_get_function_groups(u32 fid, u32 index, u16 *groups)
  429. {
  430. struct zynqmp_pm_query_data qdata = {0};
  431. u32 payload[PAYLOAD_ARG_CNT];
  432. int ret;
  433. qdata.qid = PM_QID_PINCTRL_GET_FUNCTION_GROUPS;
  434. qdata.arg1 = fid;
  435. qdata.arg2 = index;
  436. ret = zynqmp_pm_query_data(qdata, payload);
  437. if (ret)
  438. return ret;
  439. memcpy(groups, &payload[1], PINCTRL_GET_FUNC_GROUPS_RESP_LEN);
  440. return 0;
  441. }
  442. static int zynqmp_pinctrl_get_func_num_groups(u32 fid, unsigned int *ngroups)
  443. {
  444. struct zynqmp_pm_query_data qdata = {0};
  445. u32 payload[PAYLOAD_ARG_CNT];
  446. int ret;
  447. qdata.qid = PM_QID_PINCTRL_GET_NUM_FUNCTION_GROUPS;
  448. qdata.arg1 = fid;
  449. ret = zynqmp_pm_query_data(qdata, payload);
  450. if (ret)
  451. return ret;
  452. *ngroups = payload[1];
  453. return 0;
  454. }
  455. /**
  456. * zynqmp_pinctrl_prepare_func_groups() - prepare function and groups data
  457. * @dev: Device pointer.
  458. * @fid: Function ID.
  459. * @func: Function data.
  460. * @groups: Groups data.
  461. *
  462. * Query firmware to get group IDs for each function. Firmware returns
  463. * group IDs. Based on the group index for the function, group names in
  464. * the function are stored. For example, the first group in "eth0" function
  465. * is named as "eth0_0" and the second group as "eth0_1" and so on.
  466. *
  467. * Based on the group ID received from the firmware, function stores name of
  468. * the group for that group ID. For example, if "eth0" first group ID
  469. * is x, groups[x] name will be stored as "eth0_0".
  470. *
  471. * Once done for each function, each function would have its group names
  472. * and each group would also have their names.
  473. *
  474. * Return: 0 on success else error code.
  475. */
  476. static int zynqmp_pinctrl_prepare_func_groups(struct device *dev, u32 fid,
  477. struct zynqmp_pmux_function *func,
  478. struct zynqmp_pctrl_group *groups)
  479. {
  480. u16 resp[NUM_GROUPS_PER_RESP] = {0};
  481. const char **fgroups;
  482. int ret, index, i;
  483. fgroups = devm_kzalloc(dev, sizeof(*fgroups) * func->ngroups, GFP_KERNEL);
  484. if (!fgroups)
  485. return -ENOMEM;
  486. for (index = 0; index < func->ngroups; index += NUM_GROUPS_PER_RESP) {
  487. ret = zynqmp_pinctrl_get_function_groups(fid, index, resp);
  488. if (ret)
  489. return ret;
  490. for (i = 0; i < NUM_GROUPS_PER_RESP; i++) {
  491. if (resp[i] == NA_GROUP)
  492. goto done;
  493. if (resp[i] == RESERVED_GROUP)
  494. continue;
  495. fgroups[index + i] = devm_kasprintf(dev, GFP_KERNEL,
  496. "%s_%d_grp",
  497. func->name,
  498. index + i);
  499. if (!fgroups[index + i])
  500. return -ENOMEM;
  501. groups[resp[i]].name = devm_kasprintf(dev, GFP_KERNEL,
  502. "%s_%d_grp",
  503. func->name,
  504. index + i);
  505. if (!groups[resp[i]].name)
  506. return -ENOMEM;
  507. }
  508. }
  509. done:
  510. func->groups = fgroups;
  511. return 0;
  512. }
  513. static void zynqmp_pinctrl_get_function_name(u32 fid, char *name)
  514. {
  515. struct zynqmp_pm_query_data qdata = {0};
  516. u32 payload[PAYLOAD_ARG_CNT];
  517. qdata.qid = PM_QID_PINCTRL_GET_FUNCTION_NAME;
  518. qdata.arg1 = fid;
  519. /*
  520. * Name of the function is maximum 16 bytes and cannot
  521. * accommodate the return value in SMC buffers, hence ignoring
  522. * the return value for this specific qid.
  523. */
  524. zynqmp_pm_query_data(qdata, payload);
  525. memcpy(name, payload, PINCTRL_GET_FUNC_NAME_RESP_LEN);
  526. }
  527. static int zynqmp_pinctrl_get_num_functions(unsigned int *nfuncs)
  528. {
  529. struct zynqmp_pm_query_data qdata = {0};
  530. u32 payload[PAYLOAD_ARG_CNT];
  531. int ret;
  532. qdata.qid = PM_QID_PINCTRL_GET_NUM_FUNCTIONS;
  533. ret = zynqmp_pm_query_data(qdata, payload);
  534. if (ret)
  535. return ret;
  536. *nfuncs = payload[1];
  537. return 0;
  538. }
  539. static int zynqmp_pinctrl_get_pin_groups(u32 pin, u32 index, u16 *groups)
  540. {
  541. struct zynqmp_pm_query_data qdata = {0};
  542. u32 payload[PAYLOAD_ARG_CNT];
  543. int ret;
  544. qdata.qid = PM_QID_PINCTRL_GET_PIN_GROUPS;
  545. qdata.arg1 = pin;
  546. qdata.arg2 = index;
  547. ret = zynqmp_pm_query_data(qdata, payload);
  548. if (ret)
  549. return ret;
  550. memcpy(groups, &payload[1], PINCTRL_GET_PIN_GROUPS_RESP_LEN);
  551. return 0;
  552. }
  553. static void zynqmp_pinctrl_group_add_pin(struct zynqmp_pctrl_group *group,
  554. unsigned int pin)
  555. {
  556. group->pins[group->npins++] = pin;
  557. }
  558. /**
  559. * zynqmp_pinctrl_create_pin_groups() - assign pins to respective groups
  560. * @dev: Device pointer.
  561. * @groups: Groups data.
  562. * @pin: Pin number.
  563. *
  564. * Query firmware to get groups available for the given pin.
  565. * Based on the firmware response(group IDs for the pin), add
  566. * pin number to the respective group's pin array.
  567. *
  568. * Once all pins are queries, each group would have its number
  569. * of pins and pin numbers data.
  570. *
  571. * Return: 0 on success else error code.
  572. */
  573. static int zynqmp_pinctrl_create_pin_groups(struct device *dev,
  574. struct zynqmp_pctrl_group *groups,
  575. unsigned int pin)
  576. {
  577. u16 resp[NUM_GROUPS_PER_RESP] = {0};
  578. int ret, i, index = 0;
  579. do {
  580. ret = zynqmp_pinctrl_get_pin_groups(pin, index, resp);
  581. if (ret)
  582. return ret;
  583. for (i = 0; i < NUM_GROUPS_PER_RESP; i++) {
  584. if (resp[i] == NA_GROUP)
  585. return ret;
  586. if (resp[i] == RESERVED_GROUP)
  587. continue;
  588. zynqmp_pinctrl_group_add_pin(&groups[resp[i]], pin);
  589. }
  590. index += NUM_GROUPS_PER_RESP;
  591. } while (index <= MAX_PIN_GROUPS);
  592. return 0;
  593. }
  594. /**
  595. * zynqmp_pinctrl_prepare_group_pins() - prepare each group's pin data
  596. * @dev: Device pointer.
  597. * @groups: Groups data.
  598. * @ngroups: Number of groups.
  599. *
  600. * Prepare pin number and number of pins data for each pins.
  601. *
  602. * Return: 0 on success else error code.
  603. */
  604. static int zynqmp_pinctrl_prepare_group_pins(struct device *dev,
  605. struct zynqmp_pctrl_group *groups,
  606. unsigned int ngroups)
  607. {
  608. unsigned int pin;
  609. int ret;
  610. for (pin = 0; pin < zynqmp_desc.npins; pin++) {
  611. ret = zynqmp_pinctrl_create_pin_groups(dev, groups, pin);
  612. if (ret)
  613. return ret;
  614. }
  615. return 0;
  616. }
  617. /**
  618. * zynqmp_pinctrl_prepare_function_info() - prepare function info
  619. * @dev: Device pointer.
  620. * @pctrl: Pin control driver data.
  621. *
  622. * Query firmware for functions, groups and pin information and
  623. * prepare pin control driver data.
  624. *
  625. * Query number of functions and number of function groups (number
  626. * of groups in the given function) to allocate required memory buffers
  627. * for functions and groups. Once buffers are allocated to store
  628. * functions and groups data, query and store required information
  629. * (number of groups and group names for each function, number of
  630. * pins and pin numbers for each group).
  631. *
  632. * Return: 0 on success else error code.
  633. */
  634. static int zynqmp_pinctrl_prepare_function_info(struct device *dev,
  635. struct zynqmp_pinctrl *pctrl)
  636. {
  637. struct zynqmp_pmux_function *funcs;
  638. struct zynqmp_pctrl_group *groups;
  639. int ret, i;
  640. ret = zynqmp_pinctrl_get_num_functions(&pctrl->nfuncs);
  641. if (ret)
  642. return ret;
  643. funcs = devm_kzalloc(dev, sizeof(*funcs) * pctrl->nfuncs, GFP_KERNEL);
  644. if (!funcs)
  645. return -ENOMEM;
  646. for (i = 0; i < pctrl->nfuncs; i++) {
  647. zynqmp_pinctrl_get_function_name(i, funcs[i].name);
  648. ret = zynqmp_pinctrl_get_func_num_groups(i, &funcs[i].ngroups);
  649. if (ret)
  650. return ret;
  651. pctrl->ngroups += funcs[i].ngroups;
  652. }
  653. groups = devm_kzalloc(dev, sizeof(*groups) * pctrl->ngroups, GFP_KERNEL);
  654. if (!groups)
  655. return -ENOMEM;
  656. for (i = 0; i < pctrl->nfuncs; i++) {
  657. ret = zynqmp_pinctrl_prepare_func_groups(dev, i, &funcs[i],
  658. groups);
  659. if (ret)
  660. return ret;
  661. }
  662. ret = zynqmp_pinctrl_prepare_group_pins(dev, groups, pctrl->ngroups);
  663. if (ret)
  664. return ret;
  665. pctrl->funcs = funcs;
  666. pctrl->groups = groups;
  667. return 0;
  668. }
  669. static int zynqmp_pinctrl_get_num_pins(unsigned int *npins)
  670. {
  671. struct zynqmp_pm_query_data qdata = {0};
  672. u32 payload[PAYLOAD_ARG_CNT];
  673. int ret;
  674. qdata.qid = PM_QID_PINCTRL_GET_NUM_PINS;
  675. ret = zynqmp_pm_query_data(qdata, payload);
  676. if (ret)
  677. return ret;
  678. *npins = payload[1];
  679. return 0;
  680. }
  681. /**
  682. * zynqmp_pinctrl_prepare_pin_desc() - prepare pin description info
  683. * @dev: Device pointer.
  684. * @zynqmp_pins: Pin information.
  685. * @npins: Number of pins.
  686. *
  687. * Query number of pins information from firmware and prepare pin
  688. * description containing pin number and pin name.
  689. *
  690. * Return: 0 on success else error code.
  691. */
  692. static int zynqmp_pinctrl_prepare_pin_desc(struct device *dev,
  693. const struct pinctrl_pin_desc
  694. **zynqmp_pins,
  695. unsigned int *npins)
  696. {
  697. struct pinctrl_pin_desc *pins, *pin;
  698. int ret;
  699. int i;
  700. ret = zynqmp_pinctrl_get_num_pins(npins);
  701. if (ret)
  702. return ret;
  703. pins = devm_kzalloc(dev, sizeof(*pins) * *npins, GFP_KERNEL);
  704. if (!pins)
  705. return -ENOMEM;
  706. for (i = 0; i < *npins; i++) {
  707. pin = &pins[i];
  708. pin->number = i;
  709. pin->name = devm_kasprintf(dev, GFP_KERNEL, "%s%d",
  710. ZYNQMP_PIN_PREFIX, i);
  711. if (!pin->name)
  712. return -ENOMEM;
  713. }
  714. *zynqmp_pins = pins;
  715. return 0;
  716. }
  717. static int zynqmp_pinctrl_probe(struct platform_device *pdev)
  718. {
  719. struct zynqmp_pinctrl *pctrl;
  720. int ret;
  721. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  722. if (!pctrl)
  723. return -ENOMEM;
  724. ret = zynqmp_pinctrl_prepare_pin_desc(&pdev->dev,
  725. &zynqmp_desc.pins,
  726. &zynqmp_desc.npins);
  727. if (ret) {
  728. dev_err(&pdev->dev, "pin desc prepare fail with %d\n", ret);
  729. return ret;
  730. }
  731. ret = zynqmp_pinctrl_prepare_function_info(&pdev->dev, pctrl);
  732. if (ret) {
  733. dev_err(&pdev->dev, "function info prepare fail with %d\n", ret);
  734. return ret;
  735. }
  736. pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &zynqmp_desc, pctrl);
  737. if (IS_ERR(pctrl->pctrl))
  738. return PTR_ERR(pctrl->pctrl);
  739. platform_set_drvdata(pdev, pctrl);
  740. return ret;
  741. }
  742. static const struct of_device_id zynqmp_pinctrl_of_match[] = {
  743. { .compatible = "xlnx,zynqmp-pinctrl" },
  744. { }
  745. };
  746. MODULE_DEVICE_TABLE(of, zynqmp_pinctrl_of_match);
  747. static struct platform_driver zynqmp_pinctrl_driver = {
  748. .driver = {
  749. .name = "zynqmp-pinctrl",
  750. .of_match_table = zynqmp_pinctrl_of_match,
  751. },
  752. .probe = zynqmp_pinctrl_probe,
  753. };
  754. module_platform_driver(zynqmp_pinctrl_driver);
  755. MODULE_AUTHOR("Sai Krishna Potthuri <[email protected]>");
  756. MODULE_DESCRIPTION("ZynqMP Pin Controller Driver");
  757. MODULE_LICENSE("GPL v2");