pinmux.c 16 KB

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