pinmux.c 16 KB

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