pinmux.c 16 KB

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