pinmux.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. /* errors only affect debug data, so just warn */
  355. dev_warn(pctldev->dev,
  356. "could not get pins for group selector %d\n",
  357. setting->data.mux.group);
  358. num_pins = 0;
  359. }
  360. /* Try to allocate all pins in this group, one by one */
  361. for (i = 0; i < num_pins; i++) {
  362. ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
  363. if (ret) {
  364. dev_err(pctldev->dev,
  365. "could not request pin %d on device %s\n",
  366. pins[i], pinctrl_dev_get_name(pctldev));
  367. goto err_pin_request;
  368. }
  369. }
  370. /* Now that we have acquired the pins, encode the mux setting */
  371. for (i = 0; i < num_pins; i++) {
  372. desc = pin_desc_get(pctldev, pins[i]);
  373. if (desc == NULL) {
  374. dev_warn(pctldev->dev,
  375. "could not get pin desc for pin %d\n",
  376. pins[i]);
  377. continue;
  378. }
  379. desc->mux_setting = &(setting->data.mux);
  380. }
  381. ret = ops->enable(pctldev, setting->data.mux.func,
  382. setting->data.mux.group);
  383. if (ret)
  384. goto err_enable;
  385. return 0;
  386. err_enable:
  387. for (i = 0; i < num_pins; i++) {
  388. desc = pin_desc_get(pctldev, pins[i]);
  389. if (desc)
  390. desc->mux_setting = NULL;
  391. }
  392. err_pin_request:
  393. /* On error release all taken pins */
  394. while (--i >= 0)
  395. pin_free(pctldev, pins[i], NULL);
  396. return ret;
  397. }
  398. void pinmux_disable_setting(struct pinctrl_setting const *setting)
  399. {
  400. struct pinctrl_dev *pctldev = setting->pctldev;
  401. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  402. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  403. int ret;
  404. const unsigned *pins;
  405. unsigned num_pins;
  406. int i;
  407. struct pin_desc *desc;
  408. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  409. &pins, &num_pins);
  410. if (ret) {
  411. /* errors only affect debug data, so just warn */
  412. dev_warn(pctldev->dev,
  413. "could not get pins for group selector %d\n",
  414. setting->data.mux.group);
  415. num_pins = 0;
  416. }
  417. /* Flag the descs that no setting is active */
  418. for (i = 0; i < num_pins; i++) {
  419. desc = pin_desc_get(pctldev, pins[i]);
  420. if (desc == NULL) {
  421. dev_warn(pctldev->dev,
  422. "could not get pin desc for pin %d\n",
  423. pins[i]);
  424. continue;
  425. }
  426. desc->mux_setting = NULL;
  427. }
  428. /* And release the pins */
  429. for (i = 0; i < num_pins; i++)
  430. pin_free(pctldev, pins[i], NULL);
  431. if (ops->disable)
  432. ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);
  433. }
  434. #ifdef CONFIG_DEBUG_FS
  435. /* Called from pincontrol core */
  436. static int pinmux_functions_show(struct seq_file *s, void *what)
  437. {
  438. struct pinctrl_dev *pctldev = s->private;
  439. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  440. unsigned nfuncs;
  441. unsigned func_selector = 0;
  442. if (!pmxops)
  443. return 0;
  444. mutex_lock(&pinctrl_mutex);
  445. nfuncs = pmxops->get_functions_count(pctldev);
  446. while (func_selector < nfuncs) {
  447. const char *func = pmxops->get_function_name(pctldev,
  448. func_selector);
  449. const char * const *groups;
  450. unsigned num_groups;
  451. int ret;
  452. int i;
  453. ret = pmxops->get_function_groups(pctldev, func_selector,
  454. &groups, &num_groups);
  455. if (ret)
  456. seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
  457. func);
  458. seq_printf(s, "function: %s, groups = [ ", func);
  459. for (i = 0; i < num_groups; i++)
  460. seq_printf(s, "%s ", groups[i]);
  461. seq_puts(s, "]\n");
  462. func_selector++;
  463. }
  464. mutex_unlock(&pinctrl_mutex);
  465. return 0;
  466. }
  467. static int pinmux_pins_show(struct seq_file *s, void *what)
  468. {
  469. struct pinctrl_dev *pctldev = s->private;
  470. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  471. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  472. unsigned i, pin;
  473. if (!pmxops)
  474. return 0;
  475. seq_puts(s, "Pinmux settings per pin\n");
  476. seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n");
  477. mutex_lock(&pinctrl_mutex);
  478. /* The pin number can be retrived from the pin controller descriptor */
  479. for (i = 0; i < pctldev->desc->npins; i++) {
  480. struct pin_desc *desc;
  481. bool is_hog = false;
  482. pin = pctldev->desc->pins[i].number;
  483. desc = pin_desc_get(pctldev, pin);
  484. /* Skip if we cannot search the pin */
  485. if (desc == NULL)
  486. continue;
  487. if (desc->mux_owner &&
  488. !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
  489. is_hog = true;
  490. seq_printf(s, "pin %d (%s): %s %s%s", pin,
  491. desc->name ? desc->name : "unnamed",
  492. desc->mux_owner ? desc->mux_owner
  493. : "(MUX UNCLAIMED)",
  494. desc->gpio_owner ? desc->gpio_owner
  495. : "(GPIO UNCLAIMED)",
  496. is_hog ? " (HOG)" : "");
  497. if (desc->mux_setting)
  498. seq_printf(s, " function %s group %s\n",
  499. pmxops->get_function_name(pctldev,
  500. desc->mux_setting->func),
  501. pctlops->get_group_name(pctldev,
  502. desc->mux_setting->group));
  503. else
  504. seq_printf(s, "\n");
  505. }
  506. mutex_unlock(&pinctrl_mutex);
  507. return 0;
  508. }
  509. void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
  510. {
  511. seq_printf(s, "group %s\nfunction %s\n",
  512. map->data.mux.group ? map->data.mux.group : "(default)",
  513. map->data.mux.function);
  514. }
  515. void pinmux_show_setting(struct seq_file *s,
  516. struct pinctrl_setting const *setting)
  517. {
  518. struct pinctrl_dev *pctldev = setting->pctldev;
  519. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  520. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  521. seq_printf(s, "group: %s (%u) function: %s (%u)\n",
  522. pctlops->get_group_name(pctldev, setting->data.mux.group),
  523. setting->data.mux.group,
  524. pmxops->get_function_name(pctldev, setting->data.mux.func),
  525. setting->data.mux.func);
  526. }
  527. static int pinmux_functions_open(struct inode *inode, struct file *file)
  528. {
  529. return single_open(file, pinmux_functions_show, inode->i_private);
  530. }
  531. static int pinmux_pins_open(struct inode *inode, struct file *file)
  532. {
  533. return single_open(file, pinmux_pins_show, inode->i_private);
  534. }
  535. static const struct file_operations pinmux_functions_ops = {
  536. .open = pinmux_functions_open,
  537. .read = seq_read,
  538. .llseek = seq_lseek,
  539. .release = single_release,
  540. };
  541. static const struct file_operations pinmux_pins_ops = {
  542. .open = pinmux_pins_open,
  543. .read = seq_read,
  544. .llseek = seq_lseek,
  545. .release = single_release,
  546. };
  547. void pinmux_init_device_debugfs(struct dentry *devroot,
  548. struct pinctrl_dev *pctldev)
  549. {
  550. debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
  551. devroot, pctldev, &pinmux_functions_ops);
  552. debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
  553. devroot, pctldev, &pinmux_pins_ops);
  554. }
  555. #endif /* CONFIG_DEBUG_FS */