pinmux.c 16 KB

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