pinmux.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. desc->mux_usecount--;
  177. if (desc->mux_usecount)
  178. return NULL;
  179. }
  180. /*
  181. * If there is no kind of request function for the pin we just assume
  182. * we got it by default and proceed.
  183. */
  184. if (gpio_range && ops->gpio_disable_free)
  185. ops->gpio_disable_free(pctldev, gpio_range, pin);
  186. else if (ops->free)
  187. ops->free(pctldev, pin);
  188. if (gpio_range) {
  189. owner = desc->gpio_owner;
  190. desc->gpio_owner = NULL;
  191. } else {
  192. owner = desc->mux_owner;
  193. desc->mux_owner = NULL;
  194. desc->mux_setting = NULL;
  195. }
  196. module_put(pctldev->owner);
  197. return owner;
  198. }
  199. /**
  200. * pinmux_request_gpio() - request pinmuxing for a GPIO pin
  201. * @pctldev: pin controller device affected
  202. * @pin: the pin to mux in for GPIO
  203. * @range: the applicable GPIO range
  204. */
  205. int pinmux_request_gpio(struct pinctrl_dev *pctldev,
  206. struct pinctrl_gpio_range *range,
  207. unsigned pin, unsigned gpio)
  208. {
  209. const char *owner;
  210. int ret;
  211. /* Conjure some name stating what chip and pin this is taken by */
  212. owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
  213. if (!owner)
  214. return -EINVAL;
  215. ret = pin_request(pctldev, pin, owner, range);
  216. if (ret < 0)
  217. kfree(owner);
  218. return ret;
  219. }
  220. /**
  221. * pinmux_free_gpio() - release a pin from GPIO muxing
  222. * @pctldev: the pin controller device for the pin
  223. * @pin: the affected currently GPIO-muxed in pin
  224. * @range: applicable GPIO range
  225. */
  226. void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
  227. struct pinctrl_gpio_range *range)
  228. {
  229. const char *owner;
  230. owner = pin_free(pctldev, pin, range);
  231. kfree(owner);
  232. }
  233. /**
  234. * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
  235. * @pctldev: the pin controller handling this pin
  236. * @range: applicable GPIO range
  237. * @pin: the affected GPIO pin in this controller
  238. * @input: true if we set the pin as input, false for output
  239. */
  240. int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
  241. struct pinctrl_gpio_range *range,
  242. unsigned pin, bool input)
  243. {
  244. const struct pinmux_ops *ops;
  245. int ret;
  246. ops = pctldev->desc->pmxops;
  247. if (ops->gpio_set_direction)
  248. ret = ops->gpio_set_direction(pctldev, range, pin, input);
  249. else
  250. ret = 0;
  251. return ret;
  252. }
  253. static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
  254. const char *function)
  255. {
  256. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  257. unsigned nfuncs = ops->get_functions_count(pctldev);
  258. unsigned selector = 0;
  259. /* See if this pctldev has this function */
  260. while (selector < nfuncs) {
  261. const char *fname = ops->get_function_name(pctldev,
  262. selector);
  263. if (!strcmp(function, fname))
  264. return selector;
  265. selector++;
  266. }
  267. pr_err("%s does not support function %s\n",
  268. pinctrl_dev_get_name(pctldev), function);
  269. return -EINVAL;
  270. }
  271. int pinmux_map_to_setting(struct pinctrl_map const *map,
  272. struct pinctrl_setting *setting)
  273. {
  274. struct pinctrl_dev *pctldev = setting->pctldev;
  275. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  276. char const * const *groups;
  277. unsigned num_groups;
  278. int ret;
  279. const char *group;
  280. int i;
  281. if (!pmxops) {
  282. dev_err(pctldev->dev, "does not support mux function\n");
  283. return -EINVAL;
  284. }
  285. ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
  286. if (ret < 0) {
  287. dev_err(pctldev->dev, "invalid function %s in map table\n",
  288. map->data.mux.function);
  289. return ret;
  290. }
  291. setting->data.mux.func = ret;
  292. ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
  293. &groups, &num_groups);
  294. if (ret < 0) {
  295. dev_err(pctldev->dev, "can't query groups for function %s\n",
  296. map->data.mux.function);
  297. return ret;
  298. }
  299. if (!num_groups) {
  300. dev_err(pctldev->dev,
  301. "function %s can't be selected on any group\n",
  302. map->data.mux.function);
  303. return -EINVAL;
  304. }
  305. if (map->data.mux.group) {
  306. bool found = false;
  307. group = map->data.mux.group;
  308. for (i = 0; i < num_groups; i++) {
  309. if (!strcmp(group, groups[i])) {
  310. found = true;
  311. break;
  312. }
  313. }
  314. if (!found) {
  315. dev_err(pctldev->dev,
  316. "invalid group \"%s\" for function \"%s\"\n",
  317. group, map->data.mux.function);
  318. return -EINVAL;
  319. }
  320. } else {
  321. group = groups[0];
  322. }
  323. ret = pinctrl_get_group_selector(pctldev, group);
  324. if (ret < 0) {
  325. dev_err(pctldev->dev, "invalid group %s in map table\n",
  326. map->data.mux.group);
  327. return ret;
  328. }
  329. setting->data.mux.group = ret;
  330. return 0;
  331. }
  332. void pinmux_free_setting(struct pinctrl_setting const *setting)
  333. {
  334. /* This function is currently unused */
  335. }
  336. int pinmux_enable_setting(struct pinctrl_setting const *setting)
  337. {
  338. struct pinctrl_dev *pctldev = setting->pctldev;
  339. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  340. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  341. int ret;
  342. const unsigned *pins;
  343. unsigned num_pins;
  344. int i;
  345. struct pin_desc *desc;
  346. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  347. &pins, &num_pins);
  348. if (ret) {
  349. /* errors only affect debug data, so just warn */
  350. dev_warn(pctldev->dev,
  351. "could not get pins for group selector %d\n",
  352. setting->data.mux.group);
  353. num_pins = 0;
  354. }
  355. /* Try to allocate all pins in this group, one by one */
  356. for (i = 0; i < num_pins; i++) {
  357. ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
  358. if (ret) {
  359. dev_err(pctldev->dev,
  360. "could not request pin %d on device %s\n",
  361. pins[i], pinctrl_dev_get_name(pctldev));
  362. goto err_pin_request;
  363. }
  364. }
  365. /* Now that we have acquired the pins, encode the mux setting */
  366. for (i = 0; i < num_pins; i++) {
  367. desc = pin_desc_get(pctldev, pins[i]);
  368. if (desc == NULL) {
  369. dev_warn(pctldev->dev,
  370. "could not get pin desc for pin %d\n",
  371. pins[i]);
  372. continue;
  373. }
  374. desc->mux_setting = &(setting->data.mux);
  375. }
  376. ret = ops->enable(pctldev, setting->data.mux.func,
  377. setting->data.mux.group);
  378. if (ret)
  379. goto err_enable;
  380. return 0;
  381. err_enable:
  382. for (i = 0; i < num_pins; i++) {
  383. desc = pin_desc_get(pctldev, pins[i]);
  384. if (desc)
  385. desc->mux_setting = NULL;
  386. }
  387. err_pin_request:
  388. /* On error release all taken pins */
  389. while (--i >= 0)
  390. pin_free(pctldev, pins[i], NULL);
  391. return ret;
  392. }
  393. void pinmux_disable_setting(struct pinctrl_setting const *setting)
  394. {
  395. struct pinctrl_dev *pctldev = setting->pctldev;
  396. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  397. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  398. int ret;
  399. const unsigned *pins;
  400. unsigned num_pins;
  401. int i;
  402. struct pin_desc *desc;
  403. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  404. &pins, &num_pins);
  405. if (ret) {
  406. /* errors only affect debug data, so just warn */
  407. dev_warn(pctldev->dev,
  408. "could not get pins for group selector %d\n",
  409. setting->data.mux.group);
  410. num_pins = 0;
  411. }
  412. /* Flag the descs that no setting is active */
  413. for (i = 0; i < num_pins; i++) {
  414. desc = pin_desc_get(pctldev, pins[i]);
  415. if (desc == NULL) {
  416. dev_warn(pctldev->dev,
  417. "could not get pin desc for pin %d\n",
  418. pins[i]);
  419. continue;
  420. }
  421. desc->mux_setting = NULL;
  422. }
  423. /* And release the pins */
  424. for (i = 0; i < num_pins; i++)
  425. pin_free(pctldev, pins[i], NULL);
  426. if (ops->disable)
  427. ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
  428. }
  429. #ifdef CONFIG_DEBUG_FS
  430. /* Called from pincontrol core */
  431. static int pinmux_functions_show(struct seq_file *s, void *what)
  432. {
  433. struct pinctrl_dev *pctldev = s->private;
  434. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  435. unsigned nfuncs;
  436. unsigned func_selector = 0;
  437. if (!pmxops)
  438. return 0;
  439. mutex_lock(&pinctrl_mutex);
  440. nfuncs = pmxops->get_functions_count(pctldev);
  441. while (func_selector < nfuncs) {
  442. const char *func = pmxops->get_function_name(pctldev,
  443. func_selector);
  444. const char * const *groups;
  445. unsigned num_groups;
  446. int ret;
  447. int i;
  448. ret = pmxops->get_function_groups(pctldev, func_selector,
  449. &groups, &num_groups);
  450. if (ret)
  451. seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
  452. func);
  453. seq_printf(s, "function: %s, groups = [ ", func);
  454. for (i = 0; i < num_groups; i++)
  455. seq_printf(s, "%s ", groups[i]);
  456. seq_puts(s, "]\n");
  457. func_selector++;
  458. }
  459. mutex_unlock(&pinctrl_mutex);
  460. return 0;
  461. }
  462. static int pinmux_pins_show(struct seq_file *s, void *what)
  463. {
  464. struct pinctrl_dev *pctldev = s->private;
  465. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  466. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  467. unsigned i, pin;
  468. if (!pmxops)
  469. return 0;
  470. seq_puts(s, "Pinmux settings per pin\n");
  471. seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
  472. mutex_lock(&pinctrl_mutex);
  473. /* The pin number can be retrived from the pin controller descriptor */
  474. for (i = 0; i < pctldev->desc->npins; i++) {
  475. struct pin_desc *desc;
  476. bool is_hog = false;
  477. pin = pctldev->desc->pins[i].number;
  478. desc = pin_desc_get(pctldev, pin);
  479. /* Skip if we cannot search the pin */
  480. if (desc == NULL)
  481. continue;
  482. if (desc->mux_owner &&
  483. !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
  484. is_hog = true;
  485. seq_printf(s, "pin %d (%s): %s %s%s", pin,
  486. desc->name ? desc->name : "unnamed",
  487. desc->mux_owner ? desc->mux_owner
  488. : "(MUX UNCLAIMED)",
  489. desc->gpio_owner ? desc->gpio_owner
  490. : "(GPIO UNCLAIMED)",
  491. is_hog ? " (HOG)" : "");
  492. if (desc->mux_setting)
  493. seq_printf(s, " function %s group %s\n",
  494. pmxops->get_function_name(pctldev,
  495. desc->mux_setting->func),
  496. pctlops->get_group_name(pctldev,
  497. desc->mux_setting->group));
  498. else
  499. seq_printf(s, "\n");
  500. }
  501. mutex_unlock(&pinctrl_mutex);
  502. return 0;
  503. }
  504. void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
  505. {
  506. seq_printf(s, "group %s\nfunction %s\n",
  507. map->data.mux.group ? map->data.mux.group : "(default)",
  508. map->data.mux.function);
  509. }
  510. void pinmux_show_setting(struct seq_file *s,
  511. struct pinctrl_setting const *setting)
  512. {
  513. struct pinctrl_dev *pctldev = setting->pctldev;
  514. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  515. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  516. seq_printf(s, "group: %s (%u) function: %s (%u)\n",
  517. pctlops->get_group_name(pctldev, setting->data.mux.group),
  518. setting->data.mux.group,
  519. pmxops->get_function_name(pctldev, setting->data.mux.func),
  520. setting->data.mux.func);
  521. }
  522. static int pinmux_functions_open(struct inode *inode, struct file *file)
  523. {
  524. return single_open(file, pinmux_functions_show, inode->i_private);
  525. }
  526. static int pinmux_pins_open(struct inode *inode, struct file *file)
  527. {
  528. return single_open(file, pinmux_pins_show, inode->i_private);
  529. }
  530. static const struct file_operations pinmux_functions_ops = {
  531. .open = pinmux_functions_open,
  532. .read = seq_read,
  533. .llseek = seq_lseek,
  534. .release = single_release,
  535. };
  536. static const struct file_operations pinmux_pins_ops = {
  537. .open = pinmux_pins_open,
  538. .read = seq_read,
  539. .llseek = seq_lseek,
  540. .release = single_release,
  541. };
  542. void pinmux_init_device_debugfs(struct dentry *devroot,
  543. struct pinctrl_dev *pctldev)
  544. {
  545. debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
  546. devroot, pctldev, &pinmux_functions_ops);
  547. debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
  548. devroot, pctldev, &pinmux_pins_ops);
  549. }
  550. #endif /* CONFIG_DEBUG_FS */