pinctrl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * SuperH Pin Function Controller pinmux support.
  3. *
  4. * Copyright (C) 2012 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #define DRV_NAME "sh-pfc"
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/pinctrl/machine.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/slab.h>
  23. #include <linux/spinlock.h>
  24. #include "core.h"
  25. #include "../core.h"
  26. #include "../pinconf.h"
  27. struct sh_pfc_pin_config {
  28. u32 type;
  29. };
  30. struct sh_pfc_pinctrl {
  31. struct pinctrl_dev *pctl;
  32. struct pinctrl_desc pctl_desc;
  33. struct sh_pfc *pfc;
  34. struct pinctrl_pin_desc *pins;
  35. struct sh_pfc_pin_config *configs;
  36. const char *func_prop_name;
  37. const char *groups_prop_name;
  38. const char *pins_prop_name;
  39. };
  40. static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
  41. {
  42. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  43. return pmx->pfc->info->nr_groups;
  44. }
  45. static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
  46. unsigned selector)
  47. {
  48. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  49. return pmx->pfc->info->groups[selector].name;
  50. }
  51. static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  52. const unsigned **pins, unsigned *num_pins)
  53. {
  54. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  55. *pins = pmx->pfc->info->groups[selector].pins;
  56. *num_pins = pmx->pfc->info->groups[selector].nr_pins;
  57. return 0;
  58. }
  59. static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  60. unsigned offset)
  61. {
  62. seq_printf(s, "%s", DRV_NAME);
  63. }
  64. #ifdef CONFIG_OF
  65. static int sh_pfc_map_add_config(struct pinctrl_map *map,
  66. const char *group_or_pin,
  67. enum pinctrl_map_type type,
  68. unsigned long *configs,
  69. unsigned int num_configs)
  70. {
  71. unsigned long *cfgs;
  72. cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
  73. GFP_KERNEL);
  74. if (cfgs == NULL)
  75. return -ENOMEM;
  76. map->type = type;
  77. map->data.configs.group_or_pin = group_or_pin;
  78. map->data.configs.configs = cfgs;
  79. map->data.configs.num_configs = num_configs;
  80. return 0;
  81. }
  82. static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  83. struct device_node *np,
  84. struct pinctrl_map **map,
  85. unsigned int *num_maps, unsigned int *index)
  86. {
  87. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  88. struct device *dev = pmx->pfc->dev;
  89. struct pinctrl_map *maps = *map;
  90. unsigned int nmaps = *num_maps;
  91. unsigned int idx = *index;
  92. unsigned int num_configs;
  93. const char *function = NULL;
  94. unsigned long *configs;
  95. struct property *prop;
  96. unsigned int num_groups;
  97. unsigned int num_pins;
  98. const char *group;
  99. const char *pin;
  100. int ret;
  101. /* Support both the old Renesas-specific properties and the new standard
  102. * properties. Mixing old and new properties isn't allowed, neither
  103. * inside a subnode nor across subnodes.
  104. */
  105. if (!pmx->func_prop_name) {
  106. if (of_find_property(np, "groups", NULL) ||
  107. of_find_property(np, "pins", NULL)) {
  108. pmx->func_prop_name = "function";
  109. pmx->groups_prop_name = "groups";
  110. pmx->pins_prop_name = "pins";
  111. } else {
  112. pmx->func_prop_name = "renesas,function";
  113. pmx->groups_prop_name = "renesas,groups";
  114. pmx->pins_prop_name = "renesas,pins";
  115. }
  116. }
  117. /* Parse the function and configuration properties. At least a function
  118. * or one configuration must be specified.
  119. */
  120. ret = of_property_read_string(np, pmx->func_prop_name, &function);
  121. if (ret < 0 && ret != -EINVAL) {
  122. dev_err(dev, "Invalid function in DT\n");
  123. return ret;
  124. }
  125. ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
  126. if (ret < 0)
  127. return ret;
  128. if (!function && num_configs == 0) {
  129. dev_err(dev,
  130. "DT node must contain at least a function or config\n");
  131. ret = -ENODEV;
  132. goto done;
  133. }
  134. /* Count the number of pins and groups and reallocate mappings. */
  135. ret = of_property_count_strings(np, pmx->pins_prop_name);
  136. if (ret == -EINVAL) {
  137. num_pins = 0;
  138. } else if (ret < 0) {
  139. dev_err(dev, "Invalid pins list in DT\n");
  140. goto done;
  141. } else {
  142. num_pins = ret;
  143. }
  144. ret = of_property_count_strings(np, pmx->groups_prop_name);
  145. if (ret == -EINVAL) {
  146. num_groups = 0;
  147. } else if (ret < 0) {
  148. dev_err(dev, "Invalid pin groups list in DT\n");
  149. goto done;
  150. } else {
  151. num_groups = ret;
  152. }
  153. if (!num_pins && !num_groups) {
  154. dev_err(dev, "No pin or group provided in DT node\n");
  155. ret = -ENODEV;
  156. goto done;
  157. }
  158. if (function)
  159. nmaps += num_groups;
  160. if (configs)
  161. nmaps += num_pins + num_groups;
  162. maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
  163. if (maps == NULL) {
  164. ret = -ENOMEM;
  165. goto done;
  166. }
  167. *map = maps;
  168. *num_maps = nmaps;
  169. /* Iterate over pins and groups and create the mappings. */
  170. of_property_for_each_string(np, pmx->groups_prop_name, prop, group) {
  171. if (function) {
  172. maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
  173. maps[idx].data.mux.group = group;
  174. maps[idx].data.mux.function = function;
  175. idx++;
  176. }
  177. if (configs) {
  178. ret = sh_pfc_map_add_config(&maps[idx], group,
  179. PIN_MAP_TYPE_CONFIGS_GROUP,
  180. configs, num_configs);
  181. if (ret < 0)
  182. goto done;
  183. idx++;
  184. }
  185. }
  186. if (!configs) {
  187. ret = 0;
  188. goto done;
  189. }
  190. of_property_for_each_string(np, pmx->pins_prop_name, prop, pin) {
  191. ret = sh_pfc_map_add_config(&maps[idx], pin,
  192. PIN_MAP_TYPE_CONFIGS_PIN,
  193. configs, num_configs);
  194. if (ret < 0)
  195. goto done;
  196. idx++;
  197. }
  198. done:
  199. *index = idx;
  200. kfree(configs);
  201. return ret;
  202. }
  203. static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
  204. struct pinctrl_map *map, unsigned num_maps)
  205. {
  206. unsigned int i;
  207. if (map == NULL)
  208. return;
  209. for (i = 0; i < num_maps; ++i) {
  210. if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
  211. map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  212. kfree(map[i].data.configs.configs);
  213. }
  214. kfree(map);
  215. }
  216. static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
  217. struct device_node *np,
  218. struct pinctrl_map **map, unsigned *num_maps)
  219. {
  220. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  221. struct device *dev = pmx->pfc->dev;
  222. struct device_node *child;
  223. unsigned int index;
  224. int ret;
  225. *map = NULL;
  226. *num_maps = 0;
  227. index = 0;
  228. for_each_child_of_node(np, child) {
  229. ret = sh_pfc_dt_subnode_to_map(pctldev, child, map, num_maps,
  230. &index);
  231. if (ret < 0)
  232. goto done;
  233. }
  234. /* If no mapping has been found in child nodes try the config node. */
  235. if (*num_maps == 0) {
  236. ret = sh_pfc_dt_subnode_to_map(pctldev, np, map, num_maps,
  237. &index);
  238. if (ret < 0)
  239. goto done;
  240. }
  241. if (*num_maps)
  242. return 0;
  243. dev_err(dev, "no mapping found in node %s\n", np->full_name);
  244. ret = -EINVAL;
  245. done:
  246. if (ret < 0)
  247. sh_pfc_dt_free_map(pctldev, *map, *num_maps);
  248. return ret;
  249. }
  250. #endif /* CONFIG_OF */
  251. static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
  252. .get_groups_count = sh_pfc_get_groups_count,
  253. .get_group_name = sh_pfc_get_group_name,
  254. .get_group_pins = sh_pfc_get_group_pins,
  255. .pin_dbg_show = sh_pfc_pin_dbg_show,
  256. #ifdef CONFIG_OF
  257. .dt_node_to_map = sh_pfc_dt_node_to_map,
  258. .dt_free_map = sh_pfc_dt_free_map,
  259. #endif
  260. };
  261. static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
  262. {
  263. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  264. return pmx->pfc->info->nr_functions;
  265. }
  266. static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
  267. unsigned selector)
  268. {
  269. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  270. return pmx->pfc->info->functions[selector].name;
  271. }
  272. static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
  273. unsigned selector,
  274. const char * const **groups,
  275. unsigned * const num_groups)
  276. {
  277. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  278. *groups = pmx->pfc->info->functions[selector].groups;
  279. *num_groups = pmx->pfc->info->functions[selector].nr_groups;
  280. return 0;
  281. }
  282. static int sh_pfc_func_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
  283. unsigned group)
  284. {
  285. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  286. struct sh_pfc *pfc = pmx->pfc;
  287. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
  288. unsigned long flags;
  289. unsigned int i;
  290. int ret = 0;
  291. spin_lock_irqsave(&pfc->lock, flags);
  292. for (i = 0; i < grp->nr_pins; ++i) {
  293. int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
  294. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  295. if (cfg->type != PINMUX_TYPE_NONE) {
  296. ret = -EBUSY;
  297. goto done;
  298. }
  299. }
  300. for (i = 0; i < grp->nr_pins; ++i) {
  301. ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
  302. if (ret < 0)
  303. break;
  304. }
  305. done:
  306. spin_unlock_irqrestore(&pfc->lock, flags);
  307. return ret;
  308. }
  309. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  310. struct pinctrl_gpio_range *range,
  311. unsigned offset)
  312. {
  313. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  314. struct sh_pfc *pfc = pmx->pfc;
  315. int idx = sh_pfc_get_pin_index(pfc, offset);
  316. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  317. unsigned long flags;
  318. int ret;
  319. spin_lock_irqsave(&pfc->lock, flags);
  320. if (cfg->type != PINMUX_TYPE_NONE) {
  321. dev_err(pfc->dev,
  322. "Pin %u is busy, can't configure it as GPIO.\n",
  323. offset);
  324. ret = -EBUSY;
  325. goto done;
  326. }
  327. if (!pfc->gpio) {
  328. /* If GPIOs are handled externally the pin mux type need to be
  329. * set to GPIO here.
  330. */
  331. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  332. ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
  333. if (ret < 0)
  334. goto done;
  335. }
  336. cfg->type = PINMUX_TYPE_GPIO;
  337. ret = 0;
  338. done:
  339. spin_unlock_irqrestore(&pfc->lock, flags);
  340. return ret;
  341. }
  342. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  343. struct pinctrl_gpio_range *range,
  344. unsigned offset)
  345. {
  346. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  347. struct sh_pfc *pfc = pmx->pfc;
  348. int idx = sh_pfc_get_pin_index(pfc, offset);
  349. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  350. unsigned long flags;
  351. spin_lock_irqsave(&pfc->lock, flags);
  352. cfg->type = PINMUX_TYPE_NONE;
  353. spin_unlock_irqrestore(&pfc->lock, flags);
  354. }
  355. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  356. struct pinctrl_gpio_range *range,
  357. unsigned offset, bool input)
  358. {
  359. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  360. struct sh_pfc *pfc = pmx->pfc;
  361. int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  362. int idx = sh_pfc_get_pin_index(pfc, offset);
  363. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  364. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  365. unsigned long flags;
  366. unsigned int dir;
  367. int ret;
  368. /* Check if the requested direction is supported by the pin. Not all SoC
  369. * provide pin config data, so perform the check conditionally.
  370. */
  371. if (pin->configs) {
  372. dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
  373. if (!(pin->configs & dir))
  374. return -EINVAL;
  375. }
  376. spin_lock_irqsave(&pfc->lock, flags);
  377. ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
  378. if (ret < 0)
  379. goto done;
  380. cfg->type = new_type;
  381. done:
  382. spin_unlock_irqrestore(&pfc->lock, flags);
  383. return ret;
  384. }
  385. static const struct pinmux_ops sh_pfc_pinmux_ops = {
  386. .get_functions_count = sh_pfc_get_functions_count,
  387. .get_function_name = sh_pfc_get_function_name,
  388. .get_function_groups = sh_pfc_get_function_groups,
  389. .set_mux = sh_pfc_func_set_mux,
  390. .gpio_request_enable = sh_pfc_gpio_request_enable,
  391. .gpio_disable_free = sh_pfc_gpio_disable_free,
  392. .gpio_set_direction = sh_pfc_gpio_set_direction,
  393. };
  394. /* Check whether the requested parameter is supported for a pin. */
  395. static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
  396. enum pin_config_param param)
  397. {
  398. int idx = sh_pfc_get_pin_index(pfc, _pin);
  399. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  400. switch (param) {
  401. case PIN_CONFIG_BIAS_DISABLE:
  402. return pin->configs &
  403. (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
  404. case PIN_CONFIG_BIAS_PULL_UP:
  405. return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
  406. case PIN_CONFIG_BIAS_PULL_DOWN:
  407. return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
  408. case PIN_CONFIG_POWER_SOURCE:
  409. return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
  410. default:
  411. return false;
  412. }
  413. }
  414. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
  415. unsigned long *config)
  416. {
  417. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  418. struct sh_pfc *pfc = pmx->pfc;
  419. enum pin_config_param param = pinconf_to_config_param(*config);
  420. unsigned long flags;
  421. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  422. return -ENOTSUPP;
  423. switch (param) {
  424. case PIN_CONFIG_BIAS_DISABLE:
  425. case PIN_CONFIG_BIAS_PULL_UP:
  426. case PIN_CONFIG_BIAS_PULL_DOWN: {
  427. unsigned int bias;
  428. if (!pfc->info->ops || !pfc->info->ops->get_bias)
  429. return -ENOTSUPP;
  430. spin_lock_irqsave(&pfc->lock, flags);
  431. bias = pfc->info->ops->get_bias(pfc, _pin);
  432. spin_unlock_irqrestore(&pfc->lock, flags);
  433. if (bias != param)
  434. return -EINVAL;
  435. *config = 0;
  436. break;
  437. }
  438. case PIN_CONFIG_POWER_SOURCE: {
  439. int ret;
  440. if (!pfc->info->ops || !pfc->info->ops->get_io_voltage)
  441. return -ENOTSUPP;
  442. spin_lock_irqsave(&pfc->lock, flags);
  443. ret = pfc->info->ops->get_io_voltage(pfc, _pin);
  444. spin_unlock_irqrestore(&pfc->lock, flags);
  445. if (ret < 0)
  446. return ret;
  447. *config = ret;
  448. break;
  449. }
  450. default:
  451. return -ENOTSUPP;
  452. }
  453. return 0;
  454. }
  455. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
  456. unsigned long *configs, unsigned num_configs)
  457. {
  458. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  459. struct sh_pfc *pfc = pmx->pfc;
  460. enum pin_config_param param;
  461. unsigned long flags;
  462. unsigned int i;
  463. for (i = 0; i < num_configs; i++) {
  464. param = pinconf_to_config_param(configs[i]);
  465. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  466. return -ENOTSUPP;
  467. switch (param) {
  468. case PIN_CONFIG_BIAS_PULL_UP:
  469. case PIN_CONFIG_BIAS_PULL_DOWN:
  470. case PIN_CONFIG_BIAS_DISABLE:
  471. if (!pfc->info->ops || !pfc->info->ops->set_bias)
  472. return -ENOTSUPP;
  473. spin_lock_irqsave(&pfc->lock, flags);
  474. pfc->info->ops->set_bias(pfc, _pin, param);
  475. spin_unlock_irqrestore(&pfc->lock, flags);
  476. break;
  477. case PIN_CONFIG_POWER_SOURCE: {
  478. unsigned int arg =
  479. pinconf_to_config_argument(configs[i]);
  480. int ret;
  481. if (!pfc->info->ops || !pfc->info->ops->set_io_voltage)
  482. return -ENOTSUPP;
  483. spin_lock_irqsave(&pfc->lock, flags);
  484. ret = pfc->info->ops->set_io_voltage(pfc, _pin, arg);
  485. spin_unlock_irqrestore(&pfc->lock, flags);
  486. if (ret)
  487. return ret;
  488. break;
  489. }
  490. default:
  491. return -ENOTSUPP;
  492. }
  493. } /* for each config */
  494. return 0;
  495. }
  496. static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  497. unsigned long *configs,
  498. unsigned num_configs)
  499. {
  500. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  501. const unsigned int *pins;
  502. unsigned int num_pins;
  503. unsigned int i;
  504. pins = pmx->pfc->info->groups[group].pins;
  505. num_pins = pmx->pfc->info->groups[group].nr_pins;
  506. for (i = 0; i < num_pins; ++i)
  507. sh_pfc_pinconf_set(pctldev, pins[i], configs, num_configs);
  508. return 0;
  509. }
  510. static const struct pinconf_ops sh_pfc_pinconf_ops = {
  511. .is_generic = true,
  512. .pin_config_get = sh_pfc_pinconf_get,
  513. .pin_config_set = sh_pfc_pinconf_set,
  514. .pin_config_group_set = sh_pfc_pinconf_group_set,
  515. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  516. };
  517. /* PFC ranges -> pinctrl pin descs */
  518. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  519. {
  520. unsigned int i;
  521. /* Allocate and initialize the pins and configs arrays. */
  522. pmx->pins = devm_kzalloc(pfc->dev,
  523. sizeof(*pmx->pins) * pfc->info->nr_pins,
  524. GFP_KERNEL);
  525. if (unlikely(!pmx->pins))
  526. return -ENOMEM;
  527. pmx->configs = devm_kzalloc(pfc->dev,
  528. sizeof(*pmx->configs) * pfc->info->nr_pins,
  529. GFP_KERNEL);
  530. if (unlikely(!pmx->configs))
  531. return -ENOMEM;
  532. for (i = 0; i < pfc->info->nr_pins; ++i) {
  533. const struct sh_pfc_pin *info = &pfc->info->pins[i];
  534. struct sh_pfc_pin_config *cfg = &pmx->configs[i];
  535. struct pinctrl_pin_desc *pin = &pmx->pins[i];
  536. /* If the pin number is equal to -1 all pins are considered */
  537. pin->number = info->pin != (u16)-1 ? info->pin : i;
  538. pin->name = info->name;
  539. cfg->type = PINMUX_TYPE_NONE;
  540. }
  541. return 0;
  542. }
  543. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  544. {
  545. struct sh_pfc_pinctrl *pmx;
  546. int ret;
  547. pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
  548. if (unlikely(!pmx))
  549. return -ENOMEM;
  550. pmx->pfc = pfc;
  551. pfc->pinctrl = pmx;
  552. ret = sh_pfc_map_pins(pfc, pmx);
  553. if (ret < 0)
  554. return ret;
  555. pmx->pctl_desc.name = DRV_NAME;
  556. pmx->pctl_desc.owner = THIS_MODULE;
  557. pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
  558. pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
  559. pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
  560. pmx->pctl_desc.pins = pmx->pins;
  561. pmx->pctl_desc.npins = pfc->info->nr_pins;
  562. pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
  563. if (IS_ERR(pmx->pctl))
  564. return PTR_ERR(pmx->pctl);
  565. return 0;
  566. }
  567. int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
  568. {
  569. struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
  570. pinctrl_unregister(pmx->pctl);
  571. pfc->pinctrl = NULL;
  572. return 0;
  573. }