pinmux.c 16 KB

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