pinmux.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Core driver for the pin muxing portions of the pin control subsystem
  3. *
  4. * Copyright (C) 2011-2012 ST-Ericsson SA
  5. * Written on behalf of Linaro for ST-Ericsson
  6. * Based on bits of regulator core, gpio core and clk core
  7. *
  8. * Author: Linus Walleij <linus.walleij@linaro.org>
  9. *
  10. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  11. *
  12. * License terms: GNU General Public License (GPL) version 2
  13. */
  14. #define pr_fmt(fmt) "pinmux core: " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/radix-tree.h>
  21. #include <linux/err.h>
  22. #include <linux/list.h>
  23. #include <linux/string.h>
  24. #include <linux/sysfs.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/pinctrl/machine.h>
  28. #include <linux/pinctrl/pinmux.h>
  29. #include "core.h"
  30. #include "pinmux.h"
  31. int pinmux_check_ops(struct pinctrl_dev *pctldev)
  32. {
  33. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  34. unsigned nfuncs;
  35. unsigned selector = 0;
  36. /* Check that we implement required operations */
  37. if (!ops ||
  38. !ops->get_functions_count ||
  39. !ops->get_function_name ||
  40. !ops->get_function_groups ||
  41. !ops->enable) {
  42. dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
  43. return -EINVAL;
  44. }
  45. /* Check that all functions registered have names */
  46. nfuncs = ops->get_functions_count(pctldev);
  47. while (selector < nfuncs) {
  48. const char *fname = ops->get_function_name(pctldev,
  49. selector);
  50. if (!fname) {
  51. dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
  52. selector);
  53. return -EINVAL;
  54. }
  55. selector++;
  56. }
  57. return 0;
  58. }
  59. int pinmux_validate_map(struct pinctrl_map const *map, int i)
  60. {
  61. if (!map->data.mux.function) {
  62. pr_err("failed to register map %s (%d): no function given\n",
  63. map->name, i);
  64. return -EINVAL;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * pin_request() - request a single pin to be muxed in, typically for GPIO
  70. * @pin: the pin number in the global pin space
  71. * @owner: a representation of the owner of this pin; typically the device
  72. * name that controls its mux function, or the requested GPIO name
  73. * @gpio_range: the range matching the GPIO pin if this is a request for a
  74. * single GPIO pin
  75. */
  76. static int pin_request(struct pinctrl_dev *pctldev,
  77. int pin, const char *owner,
  78. struct pinctrl_gpio_range *gpio_range)
  79. {
  80. struct pin_desc *desc;
  81. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  82. int status = -EINVAL;
  83. desc = pin_desc_get(pctldev, pin);
  84. if (desc == NULL) {
  85. dev_err(pctldev->dev,
  86. "pin %d is not registered so it cannot be requested\n",
  87. pin);
  88. goto out;
  89. }
  90. dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
  91. pin, desc->name, owner);
  92. if (gpio_range) {
  93. /* There's no need to support multiple GPIO requests */
  94. if (desc->gpio_owner) {
  95. dev_err(pctldev->dev,
  96. "pin %s already requested by %s; cannot claim for %s\n",
  97. desc->name, desc->gpio_owner, owner);
  98. goto out;
  99. }
  100. desc->gpio_owner = owner;
  101. } else {
  102. if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
  103. dev_err(pctldev->dev,
  104. "pin %s already requested by %s; cannot claim for %s\n",
  105. desc->name, desc->mux_owner, owner);
  106. goto out;
  107. }
  108. desc->mux_usecount++;
  109. if (desc->mux_usecount > 1)
  110. return 0;
  111. desc->mux_owner = owner;
  112. }
  113. /* Let each pin increase references to this module */
  114. if (!try_module_get(pctldev->owner)) {
  115. dev_err(pctldev->dev,
  116. "could not increase module refcount for pin %d\n",
  117. pin);
  118. status = -EINVAL;
  119. goto out_free_pin;
  120. }
  121. /*
  122. * If there is no kind of request function for the pin we just assume
  123. * we got it by default and proceed.
  124. */
  125. if (gpio_range && ops->gpio_request_enable)
  126. /* This requests and enables a single GPIO pin */
  127. status = ops->gpio_request_enable(pctldev, gpio_range, pin);
  128. else if (ops->request)
  129. status = ops->request(pctldev, pin);
  130. else
  131. status = 0;
  132. if (status) {
  133. dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
  134. module_put(pctldev->owner);
  135. }
  136. out_free_pin:
  137. if (status) {
  138. if (gpio_range) {
  139. desc->gpio_owner = NULL;
  140. } else {
  141. desc->mux_usecount--;
  142. if (!desc->mux_usecount)
  143. desc->mux_owner = NULL;
  144. }
  145. }
  146. out:
  147. if (status)
  148. dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
  149. pin, owner, status);
  150. return status;
  151. }
  152. /**
  153. * pin_free() - release a single muxed in pin so something else can be muxed
  154. * @pctldev: pin controller device handling this pin
  155. * @pin: the pin to free
  156. * @gpio_range: the range matching the GPIO pin if this is a request for a
  157. * single GPIO pin
  158. *
  159. * This function returns a pointer to the previous owner. This is used
  160. * for callers that dynamically allocate an owner name so it can be freed
  161. * once the pin is free. This is done for GPIO request functions.
  162. */
  163. static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
  164. struct pinctrl_gpio_range *gpio_range)
  165. {
  166. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  167. struct pin_desc *desc;
  168. const char *owner;
  169. desc = pin_desc_get(pctldev, pin);
  170. if (desc == NULL) {
  171. dev_err(pctldev->dev,
  172. "pin is not registered so it cannot be freed\n");
  173. return NULL;
  174. }
  175. if (!gpio_range) {
  176. /*
  177. * A pin should not be freed more times than allocated.
  178. */
  179. if (WARN_ON(!desc->mux_usecount))
  180. return NULL;
  181. desc->mux_usecount--;
  182. if (desc->mux_usecount)
  183. return NULL;
  184. }
  185. /*
  186. * If there is no kind of request function for the pin we just assume
  187. * we got it by default and proceed.
  188. */
  189. if (gpio_range && ops->gpio_disable_free)
  190. ops->gpio_disable_free(pctldev, gpio_range, pin);
  191. else if (ops->free)
  192. ops->free(pctldev, pin);
  193. if (gpio_range) {
  194. owner = desc->gpio_owner;
  195. desc->gpio_owner = NULL;
  196. } else {
  197. owner = desc->mux_owner;
  198. desc->mux_owner = NULL;
  199. desc->mux_setting = NULL;
  200. }
  201. module_put(pctldev->owner);
  202. return owner;
  203. }
  204. /**
  205. * pinmux_request_gpio() - request pinmuxing for a GPIO pin
  206. * @pctldev: pin controller device affected
  207. * @pin: the pin to mux in for GPIO
  208. * @range: the applicable GPIO range
  209. */
  210. int pinmux_request_gpio(struct pinctrl_dev *pctldev,
  211. struct pinctrl_gpio_range *range,
  212. unsigned pin, unsigned gpio)
  213. {
  214. const char *owner;
  215. int ret;
  216. /* Conjure some name stating what chip and pin this is taken by */
  217. owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
  218. if (!owner)
  219. return -EINVAL;
  220. ret = pin_request(pctldev, pin, owner, range);
  221. if (ret < 0)
  222. kfree(owner);
  223. return ret;
  224. }
  225. /**
  226. * pinmux_free_gpio() - release a pin from GPIO muxing
  227. * @pctldev: the pin controller device for the pin
  228. * @pin: the affected currently GPIO-muxed in pin
  229. * @range: applicable GPIO range
  230. */
  231. void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
  232. struct pinctrl_gpio_range *range)
  233. {
  234. const char *owner;
  235. owner = pin_free(pctldev, pin, range);
  236. kfree(owner);
  237. }
  238. /**
  239. * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
  240. * @pctldev: the pin controller handling this pin
  241. * @range: applicable GPIO range
  242. * @pin: the affected GPIO pin in this controller
  243. * @input: true if we set the pin as input, false for output
  244. */
  245. int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
  246. struct pinctrl_gpio_range *range,
  247. unsigned pin, bool input)
  248. {
  249. const struct pinmux_ops *ops;
  250. int ret;
  251. ops = pctldev->desc->pmxops;
  252. if (ops->gpio_set_direction)
  253. ret = ops->gpio_set_direction(pctldev, range, pin, input);
  254. else
  255. ret = 0;
  256. return ret;
  257. }
  258. static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
  259. const char *function)
  260. {
  261. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  262. unsigned nfuncs = ops->get_functions_count(pctldev);
  263. unsigned selector = 0;
  264. /* See if this pctldev has this function */
  265. while (selector < nfuncs) {
  266. const char *fname = ops->get_function_name(pctldev,
  267. selector);
  268. if (!strcmp(function, fname))
  269. return selector;
  270. selector++;
  271. }
  272. pr_err("%s does not support function %s\n",
  273. pinctrl_dev_get_name(pctldev), function);
  274. return -EINVAL;
  275. }
  276. int pinmux_map_to_setting(struct pinctrl_map const *map,
  277. struct pinctrl_setting *setting)
  278. {
  279. struct pinctrl_dev *pctldev = setting->pctldev;
  280. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  281. char const * const *groups;
  282. unsigned num_groups;
  283. int ret;
  284. const char *group;
  285. int i;
  286. if (!pmxops) {
  287. dev_err(pctldev->dev, "does not support mux function\n");
  288. return -EINVAL;
  289. }
  290. ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
  291. if (ret < 0) {
  292. dev_err(pctldev->dev, "invalid function %s in map table\n",
  293. map->data.mux.function);
  294. return ret;
  295. }
  296. setting->data.mux.func = ret;
  297. ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
  298. &groups, &num_groups);
  299. if (ret < 0) {
  300. dev_err(pctldev->dev, "can't query groups for function %s\n",
  301. map->data.mux.function);
  302. return ret;
  303. }
  304. if (!num_groups) {
  305. dev_err(pctldev->dev,
  306. "function %s can't be selected on any group\n",
  307. map->data.mux.function);
  308. return -EINVAL;
  309. }
  310. if (map->data.mux.group) {
  311. bool found = false;
  312. group = map->data.mux.group;
  313. for (i = 0; i < num_groups; i++) {
  314. if (!strcmp(group, groups[i])) {
  315. found = true;
  316. break;
  317. }
  318. }
  319. if (!found) {
  320. dev_err(pctldev->dev,
  321. "invalid group \"%s\" for function \"%s\"\n",
  322. group, map->data.mux.function);
  323. return -EINVAL;
  324. }
  325. } else {
  326. group = groups[0];
  327. }
  328. ret = pinctrl_get_group_selector(pctldev, group);
  329. if (ret < 0) {
  330. dev_err(pctldev->dev, "invalid group %s in map table\n",
  331. map->data.mux.group);
  332. return ret;
  333. }
  334. setting->data.mux.group = ret;
  335. return 0;
  336. }
  337. void pinmux_free_setting(struct pinctrl_setting const *setting)
  338. {
  339. /* This function is currently unused */
  340. }
  341. int pinmux_enable_setting(struct pinctrl_setting const *setting)
  342. {
  343. struct pinctrl_dev *pctldev = setting->pctldev;
  344. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  345. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  346. int ret;
  347. const unsigned *pins;
  348. unsigned num_pins;
  349. int i;
  350. struct pin_desc *desc;
  351. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  352. &pins, &num_pins);
  353. if (ret) {
  354. const char *gname;
  355. /* errors only affect debug data, so just warn */
  356. gname = pctlops->get_group_name(pctldev,
  357. setting->data.mux.group);
  358. dev_warn(pctldev->dev,
  359. "could not get pins for group %s\n",
  360. gname);
  361. num_pins = 0;
  362. }
  363. /* Try to allocate all pins in this group, one by one */
  364. for (i = 0; i < num_pins; i++) {
  365. ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
  366. if (ret) {
  367. const char *gname;
  368. const char *pname;
  369. desc = pin_desc_get(pctldev, pins[i]);
  370. pname = desc ? desc->name : "non-existing";
  371. gname = pctlops->get_group_name(pctldev,
  372. setting->data.mux.group);
  373. dev_err(pctldev->dev,
  374. "could not request pin %d (%s) from group %s "
  375. " on device %s\n",
  376. pins[i], pname, gname,
  377. pinctrl_dev_get_name(pctldev));
  378. goto err_pin_request;
  379. }
  380. }
  381. /* Now that we have acquired the pins, encode the mux setting */
  382. for (i = 0; i < num_pins; i++) {
  383. desc = pin_desc_get(pctldev, pins[i]);
  384. if (desc == NULL) {
  385. dev_warn(pctldev->dev,
  386. "could not get pin desc for pin %d\n",
  387. pins[i]);
  388. continue;
  389. }
  390. desc->mux_setting = &(setting->data.mux);
  391. }
  392. ret = ops->enable(pctldev, setting->data.mux.func,
  393. setting->data.mux.group);
  394. if (ret)
  395. goto err_enable;
  396. return 0;
  397. err_enable:
  398. for (i = 0; i < num_pins; i++) {
  399. desc = pin_desc_get(pctldev, pins[i]);
  400. if (desc)
  401. desc->mux_setting = NULL;
  402. }
  403. err_pin_request:
  404. /* On error release all taken pins */
  405. while (--i >= 0)
  406. pin_free(pctldev, pins[i], NULL);
  407. return ret;
  408. }
  409. void pinmux_disable_setting(struct pinctrl_setting const *setting)
  410. {
  411. struct pinctrl_dev *pctldev = setting->pctldev;
  412. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  413. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  414. int ret;
  415. const unsigned *pins;
  416. unsigned num_pins;
  417. int i;
  418. struct pin_desc *desc;
  419. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  420. &pins, &num_pins);
  421. if (ret) {
  422. const char *gname;
  423. /* errors only affect debug data, so just warn */
  424. gname = pctlops->get_group_name(pctldev,
  425. setting->data.mux.group);
  426. dev_warn(pctldev->dev,
  427. "could not get pins for group %s\n",
  428. gname);
  429. num_pins = 0;
  430. }
  431. /* Flag the descs that no setting is active */
  432. for (i = 0; i < num_pins; i++) {
  433. desc = pin_desc_get(pctldev, pins[i]);
  434. if (desc == NULL) {
  435. dev_warn(pctldev->dev,
  436. "could not get pin desc for pin %d\n",
  437. pins[i]);
  438. continue;
  439. }
  440. if (desc->mux_setting == &(setting->data.mux)) {
  441. desc->mux_setting = NULL;
  442. /* And release the pin */
  443. pin_free(pctldev, pins[i], NULL);
  444. } else {
  445. const char *gname;
  446. const char *pname;
  447. pname = desc ? desc->name : "non-existing";
  448. gname = pctlops->get_group_name(pctldev,
  449. setting->data.mux.group);
  450. dev_warn(pctldev->dev,
  451. "not freeing pin %d (%s) as part of "
  452. "deactivating group %s - it is already "
  453. "used for some other setting",
  454. pins[i], pname, gname);
  455. }
  456. }
  457. if (ops->disable)
  458. ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
  459. }
  460. #ifdef CONFIG_DEBUG_FS
  461. /* Called from pincontrol core */
  462. static int pinmux_functions_show(struct seq_file *s, void *what)
  463. {
  464. struct pinctrl_dev *pctldev = s->private;
  465. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  466. unsigned nfuncs;
  467. unsigned func_selector = 0;
  468. if (!pmxops)
  469. return 0;
  470. mutex_lock(&pctldev->mutex);
  471. nfuncs = pmxops->get_functions_count(pctldev);
  472. while (func_selector < nfuncs) {
  473. const char *func = pmxops->get_function_name(pctldev,
  474. func_selector);
  475. const char * const *groups;
  476. unsigned num_groups;
  477. int ret;
  478. int i;
  479. ret = pmxops->get_function_groups(pctldev, func_selector,
  480. &groups, &num_groups);
  481. if (ret)
  482. seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
  483. func);
  484. seq_printf(s, "function: %s, groups = [ ", func);
  485. for (i = 0; i < num_groups; i++)
  486. seq_printf(s, "%s ", groups[i]);
  487. seq_puts(s, "]\n");
  488. func_selector++;
  489. }
  490. mutex_unlock(&pctldev->mutex);
  491. return 0;
  492. }
  493. static int pinmux_pins_show(struct seq_file *s, void *what)
  494. {
  495. struct pinctrl_dev *pctldev = s->private;
  496. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  497. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  498. unsigned i, pin;
  499. if (!pmxops)
  500. return 0;
  501. seq_puts(s, "Pinmux settings per pin\n");
  502. seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
  503. mutex_lock(&pctldev->mutex);
  504. /* The pin number can be retrived from the pin controller descriptor */
  505. for (i = 0; i < pctldev->desc->npins; i++) {
  506. struct pin_desc *desc;
  507. bool is_hog = false;
  508. pin = pctldev->desc->pins[i].number;
  509. desc = pin_desc_get(pctldev, pin);
  510. /* Skip if we cannot search the pin */
  511. if (desc == NULL)
  512. continue;
  513. if (desc->mux_owner &&
  514. !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
  515. is_hog = true;
  516. seq_printf(s, "pin %d (%s): %s %s%s", pin,
  517. desc->name ? desc->name : "unnamed",
  518. desc->mux_owner ? desc->mux_owner
  519. : "(MUX UNCLAIMED)",
  520. desc->gpio_owner ? desc->gpio_owner
  521. : "(GPIO UNCLAIMED)",
  522. is_hog ? " (HOG)" : "");
  523. if (desc->mux_setting)
  524. seq_printf(s, " function %s group %s\n",
  525. pmxops->get_function_name(pctldev,
  526. desc->mux_setting->func),
  527. pctlops->get_group_name(pctldev,
  528. desc->mux_setting->group));
  529. else
  530. seq_printf(s, "\n");
  531. }
  532. mutex_unlock(&pctldev->mutex);
  533. return 0;
  534. }
  535. void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
  536. {
  537. seq_printf(s, "group %s\nfunction %s\n",
  538. map->data.mux.group ? map->data.mux.group : "(default)",
  539. map->data.mux.function);
  540. }
  541. void pinmux_show_setting(struct seq_file *s,
  542. struct pinctrl_setting const *setting)
  543. {
  544. struct pinctrl_dev *pctldev = setting->pctldev;
  545. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  546. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  547. seq_printf(s, "group: %s (%u) function: %s (%u)\n",
  548. pctlops->get_group_name(pctldev, setting->data.mux.group),
  549. setting->data.mux.group,
  550. pmxops->get_function_name(pctldev, setting->data.mux.func),
  551. setting->data.mux.func);
  552. }
  553. static int pinmux_functions_open(struct inode *inode, struct file *file)
  554. {
  555. return single_open(file, pinmux_functions_show, inode->i_private);
  556. }
  557. static int pinmux_pins_open(struct inode *inode, struct file *file)
  558. {
  559. return single_open(file, pinmux_pins_show, inode->i_private);
  560. }
  561. static const struct file_operations pinmux_functions_ops = {
  562. .open = pinmux_functions_open,
  563. .read = seq_read,
  564. .llseek = seq_lseek,
  565. .release = single_release,
  566. };
  567. static const struct file_operations pinmux_pins_ops = {
  568. .open = pinmux_pins_open,
  569. .read = seq_read,
  570. .llseek = seq_lseek,
  571. .release = single_release,
  572. };
  573. void pinmux_init_device_debugfs(struct dentry *devroot,
  574. struct pinctrl_dev *pctldev)
  575. {
  576. debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
  577. devroot, pctldev, &pinmux_functions_ops);
  578. debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
  579. devroot, pctldev, &pinmux_pins_ops);
  580. }
  581. #endif /* CONFIG_DEBUG_FS */