pinctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. };
  37. static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
  38. {
  39. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  40. return pmx->pfc->info->nr_groups;
  41. }
  42. static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
  43. unsigned selector)
  44. {
  45. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  46. return pmx->pfc->info->groups[selector].name;
  47. }
  48. static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  49. const unsigned **pins, unsigned *num_pins)
  50. {
  51. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  52. *pins = pmx->pfc->info->groups[selector].pins;
  53. *num_pins = pmx->pfc->info->groups[selector].nr_pins;
  54. return 0;
  55. }
  56. static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  57. unsigned offset)
  58. {
  59. seq_printf(s, "%s", DRV_NAME);
  60. }
  61. static int sh_pfc_dt_subnode_to_map(struct device *dev, struct device_node *np,
  62. struct pinctrl_map **map,
  63. unsigned int *num_maps, unsigned int *index)
  64. {
  65. struct pinctrl_map *maps = *map;
  66. unsigned int nmaps = *num_maps;
  67. unsigned int idx = *index;
  68. const char *function = NULL;
  69. struct property *prop;
  70. const char *group;
  71. int ret;
  72. /* Parse the function and configuration properties. At least a function
  73. * or one configuration must be specified.
  74. */
  75. ret = of_property_read_string(np, "renesas,function", &function);
  76. if (ret < 0 && ret != -EINVAL) {
  77. dev_err(dev, "Invalid function in DT\n");
  78. return ret;
  79. }
  80. if (!function) {
  81. dev_err(dev, "DT node must contain at least one function\n");
  82. goto done;
  83. }
  84. /* Count the number of groups and reallocate mappings. */
  85. ret = of_property_count_strings(np, "renesas,groups");
  86. if (ret < 0 && ret != -EINVAL) {
  87. dev_err(dev, "Invalid pin groups list in DT\n");
  88. goto done;
  89. }
  90. if (!ret) {
  91. dev_err(dev, "No group provided in DT node\n");
  92. ret = -ENODEV;
  93. goto done;
  94. }
  95. nmaps += ret;
  96. maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
  97. if (maps == NULL) {
  98. ret = -ENOMEM;
  99. goto done;
  100. }
  101. *map = maps;
  102. *num_maps = nmaps;
  103. /* Iterate over pins and groups and create the mappings. */
  104. of_property_for_each_string(np, "renesas,groups", prop, group) {
  105. maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
  106. maps[idx].data.mux.group = group;
  107. maps[idx].data.mux.function = function;
  108. idx++;
  109. }
  110. ret = 0;
  111. done:
  112. *index = idx;
  113. return ret;
  114. }
  115. static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
  116. struct pinctrl_map *map, unsigned num_maps)
  117. {
  118. kfree(map);
  119. }
  120. static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
  121. struct device_node *np,
  122. struct pinctrl_map **map, unsigned *num_maps)
  123. {
  124. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  125. struct device *dev = pmx->pfc->dev;
  126. struct device_node *child;
  127. unsigned int index;
  128. int ret;
  129. *map = NULL;
  130. *num_maps = 0;
  131. index = 0;
  132. for_each_child_of_node(np, child) {
  133. ret = sh_pfc_dt_subnode_to_map(dev, child, map, num_maps,
  134. &index);
  135. if (ret < 0)
  136. goto done;
  137. }
  138. /* If no mapping has been found in child nodes try the config node. */
  139. if (*num_maps == 0) {
  140. ret = sh_pfc_dt_subnode_to_map(dev, np, map, num_maps, &index);
  141. if (ret < 0)
  142. goto done;
  143. }
  144. if (*num_maps)
  145. return 0;
  146. dev_err(dev, "no mapping found in node %s\n", np->full_name);
  147. ret = -EINVAL;
  148. done:
  149. if (ret < 0)
  150. sh_pfc_dt_free_map(pctldev, *map, *num_maps);
  151. return ret;
  152. }
  153. static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
  154. .get_groups_count = sh_pfc_get_groups_count,
  155. .get_group_name = sh_pfc_get_group_name,
  156. .get_group_pins = sh_pfc_get_group_pins,
  157. .pin_dbg_show = sh_pfc_pin_dbg_show,
  158. .dt_node_to_map = sh_pfc_dt_node_to_map,
  159. .dt_free_map = sh_pfc_dt_free_map,
  160. };
  161. static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
  162. {
  163. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  164. return pmx->pfc->info->nr_functions;
  165. }
  166. static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
  167. unsigned selector)
  168. {
  169. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  170. return pmx->pfc->info->functions[selector].name;
  171. }
  172. static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
  173. unsigned selector,
  174. const char * const **groups,
  175. unsigned * const num_groups)
  176. {
  177. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  178. *groups = pmx->pfc->info->functions[selector].groups;
  179. *num_groups = pmx->pfc->info->functions[selector].nr_groups;
  180. return 0;
  181. }
  182. static int sh_pfc_func_enable(struct pinctrl_dev *pctldev, unsigned selector,
  183. unsigned group)
  184. {
  185. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  186. struct sh_pfc *pfc = pmx->pfc;
  187. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
  188. unsigned long flags;
  189. unsigned int i;
  190. int ret = 0;
  191. spin_lock_irqsave(&pfc->lock, flags);
  192. for (i = 0; i < grp->nr_pins; ++i) {
  193. int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
  194. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  195. if (cfg->type != PINMUX_TYPE_NONE) {
  196. ret = -EBUSY;
  197. goto done;
  198. }
  199. }
  200. for (i = 0; i < grp->nr_pins; ++i) {
  201. ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
  202. if (ret < 0)
  203. break;
  204. }
  205. done:
  206. spin_unlock_irqrestore(&pfc->lock, flags);
  207. return ret;
  208. }
  209. static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
  210. unsigned group)
  211. {
  212. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  213. struct sh_pfc *pfc = pmx->pfc;
  214. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
  215. unsigned long flags;
  216. unsigned int i;
  217. spin_lock_irqsave(&pfc->lock, flags);
  218. for (i = 0; i < grp->nr_pins; ++i) {
  219. int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
  220. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  221. cfg->type = PINMUX_TYPE_NONE;
  222. }
  223. spin_unlock_irqrestore(&pfc->lock, flags);
  224. }
  225. static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
  226. struct pinctrl_gpio_range *range,
  227. unsigned offset)
  228. {
  229. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  230. struct sh_pfc *pfc = pmx->pfc;
  231. int idx = sh_pfc_get_pin_index(pfc, offset);
  232. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  233. unsigned long flags;
  234. int ret;
  235. spin_lock_irqsave(&pfc->lock, flags);
  236. if (cfg->type != PINMUX_TYPE_NONE) {
  237. dev_err(pfc->dev,
  238. "Pin %u is busy, can't configure it as GPIO.\n",
  239. offset);
  240. ret = -EBUSY;
  241. goto done;
  242. }
  243. if (!pfc->gpio) {
  244. /* If GPIOs are handled externally the pin mux type need to be
  245. * set to GPIO here.
  246. */
  247. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  248. ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
  249. if (ret < 0)
  250. goto done;
  251. }
  252. cfg->type = PINMUX_TYPE_GPIO;
  253. ret = 0;
  254. done:
  255. spin_unlock_irqrestore(&pfc->lock, flags);
  256. return ret;
  257. }
  258. static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
  259. struct pinctrl_gpio_range *range,
  260. unsigned offset)
  261. {
  262. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  263. struct sh_pfc *pfc = pmx->pfc;
  264. int idx = sh_pfc_get_pin_index(pfc, offset);
  265. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  266. unsigned long flags;
  267. spin_lock_irqsave(&pfc->lock, flags);
  268. cfg->type = PINMUX_TYPE_NONE;
  269. spin_unlock_irqrestore(&pfc->lock, flags);
  270. }
  271. static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
  272. struct pinctrl_gpio_range *range,
  273. unsigned offset, bool input)
  274. {
  275. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  276. struct sh_pfc *pfc = pmx->pfc;
  277. int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
  278. int idx = sh_pfc_get_pin_index(pfc, offset);
  279. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  280. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  281. unsigned long flags;
  282. unsigned int dir;
  283. int ret;
  284. /* Check if the requested direction is supported by the pin. Not all SoC
  285. * provide pin config data, so perform the check conditionally.
  286. */
  287. if (pin->configs) {
  288. dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
  289. if (!(pin->configs & dir))
  290. return -EINVAL;
  291. }
  292. spin_lock_irqsave(&pfc->lock, flags);
  293. ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
  294. if (ret < 0)
  295. goto done;
  296. cfg->type = new_type;
  297. done:
  298. spin_unlock_irqrestore(&pfc->lock, flags);
  299. return ret;
  300. }
  301. static const struct pinmux_ops sh_pfc_pinmux_ops = {
  302. .get_functions_count = sh_pfc_get_functions_count,
  303. .get_function_name = sh_pfc_get_function_name,
  304. .get_function_groups = sh_pfc_get_function_groups,
  305. .enable = sh_pfc_func_enable,
  306. .disable = sh_pfc_func_disable,
  307. .gpio_request_enable = sh_pfc_gpio_request_enable,
  308. .gpio_disable_free = sh_pfc_gpio_disable_free,
  309. .gpio_set_direction = sh_pfc_gpio_set_direction,
  310. };
  311. /* Check whether the requested parameter is supported for a pin. */
  312. static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
  313. enum pin_config_param param)
  314. {
  315. int idx = sh_pfc_get_pin_index(pfc, _pin);
  316. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  317. switch (param) {
  318. case PIN_CONFIG_BIAS_DISABLE:
  319. return true;
  320. case PIN_CONFIG_BIAS_PULL_UP:
  321. return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
  322. case PIN_CONFIG_BIAS_PULL_DOWN:
  323. return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
  324. default:
  325. return false;
  326. }
  327. }
  328. static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
  329. unsigned long *config)
  330. {
  331. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  332. struct sh_pfc *pfc = pmx->pfc;
  333. enum pin_config_param param = pinconf_to_config_param(*config);
  334. unsigned long flags;
  335. unsigned int bias;
  336. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  337. return -ENOTSUPP;
  338. switch (param) {
  339. case PIN_CONFIG_BIAS_DISABLE:
  340. case PIN_CONFIG_BIAS_PULL_UP:
  341. case PIN_CONFIG_BIAS_PULL_DOWN:
  342. if (!pfc->info->ops || !pfc->info->ops->get_bias)
  343. return -ENOTSUPP;
  344. spin_lock_irqsave(&pfc->lock, flags);
  345. bias = pfc->info->ops->get_bias(pfc, _pin);
  346. spin_unlock_irqrestore(&pfc->lock, flags);
  347. if (bias != param)
  348. return -EINVAL;
  349. *config = 0;
  350. break;
  351. default:
  352. return -ENOTSUPP;
  353. }
  354. return 0;
  355. }
  356. static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
  357. unsigned long config)
  358. {
  359. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  360. struct sh_pfc *pfc = pmx->pfc;
  361. enum pin_config_param param = pinconf_to_config_param(config);
  362. unsigned long flags;
  363. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  364. return -ENOTSUPP;
  365. switch (param) {
  366. case PIN_CONFIG_BIAS_PULL_UP:
  367. case PIN_CONFIG_BIAS_PULL_DOWN:
  368. case PIN_CONFIG_BIAS_DISABLE:
  369. if (!pfc->info->ops || !pfc->info->ops->set_bias)
  370. return -ENOTSUPP;
  371. spin_lock_irqsave(&pfc->lock, flags);
  372. pfc->info->ops->set_bias(pfc, _pin, param);
  373. spin_unlock_irqrestore(&pfc->lock, flags);
  374. break;
  375. default:
  376. return -ENOTSUPP;
  377. }
  378. return 0;
  379. }
  380. static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  381. unsigned long config)
  382. {
  383. struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
  384. const unsigned int *pins;
  385. unsigned int num_pins;
  386. unsigned int i;
  387. pins = pmx->pfc->info->groups[group].pins;
  388. num_pins = pmx->pfc->info->groups[group].nr_pins;
  389. for (i = 0; i < num_pins; ++i)
  390. sh_pfc_pinconf_set(pctldev, pins[i], config);
  391. return 0;
  392. }
  393. static const struct pinconf_ops sh_pfc_pinconf_ops = {
  394. .is_generic = true,
  395. .pin_config_get = sh_pfc_pinconf_get,
  396. .pin_config_set = sh_pfc_pinconf_set,
  397. .pin_config_group_set = sh_pfc_pinconf_group_set,
  398. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  399. };
  400. /* PFC ranges -> pinctrl pin descs */
  401. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  402. {
  403. const struct pinmux_range *ranges;
  404. struct pinmux_range def_range;
  405. unsigned int nr_ranges;
  406. unsigned int nr_pins;
  407. unsigned int i;
  408. if (pfc->info->ranges == NULL) {
  409. def_range.begin = 0;
  410. def_range.end = pfc->info->nr_pins - 1;
  411. ranges = &def_range;
  412. nr_ranges = 1;
  413. } else {
  414. ranges = pfc->info->ranges;
  415. nr_ranges = pfc->info->nr_ranges;
  416. }
  417. pmx->pins = devm_kzalloc(pfc->dev,
  418. sizeof(*pmx->pins) * pfc->info->nr_pins,
  419. GFP_KERNEL);
  420. if (unlikely(!pmx->pins))
  421. return -ENOMEM;
  422. pmx->configs = devm_kzalloc(pfc->dev,
  423. sizeof(*pmx->configs) * pfc->info->nr_pins,
  424. GFP_KERNEL);
  425. if (unlikely(!pmx->configs))
  426. return -ENOMEM;
  427. for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
  428. const struct pinmux_range *range = &ranges[i];
  429. unsigned int number;
  430. for (number = range->begin; number <= range->end;
  431. number++, nr_pins++) {
  432. struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
  433. struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
  434. const struct sh_pfc_pin *info =
  435. &pfc->info->pins[nr_pins];
  436. pin->number = number;
  437. pin->name = info->name;
  438. cfg->type = PINMUX_TYPE_NONE;
  439. }
  440. }
  441. pfc->nr_pins = ranges[nr_ranges-1].end + 1;
  442. return nr_ranges;
  443. }
  444. int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
  445. {
  446. struct sh_pfc_pinctrl *pmx;
  447. int nr_ranges;
  448. pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
  449. if (unlikely(!pmx))
  450. return -ENOMEM;
  451. pmx->pfc = pfc;
  452. pfc->pinctrl = pmx;
  453. nr_ranges = sh_pfc_map_pins(pfc, pmx);
  454. if (unlikely(nr_ranges < 0))
  455. return nr_ranges;
  456. pmx->pctl_desc.name = DRV_NAME;
  457. pmx->pctl_desc.owner = THIS_MODULE;
  458. pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
  459. pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
  460. pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
  461. pmx->pctl_desc.pins = pmx->pins;
  462. pmx->pctl_desc.npins = pfc->info->nr_pins;
  463. pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
  464. if (pmx->pctl == NULL)
  465. return -EINVAL;
  466. return 0;
  467. }
  468. int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
  469. {
  470. struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
  471. pinctrl_unregister(pmx->pctl);
  472. pfc->pinctrl = NULL;
  473. return 0;
  474. }