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. dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
  43. return -EINVAL;
  44. }
  45. /* Check that all functions registered have names */
  46. nfuncs = ops->get_functions_count(pctldev);
  47. while (selector < nfuncs) {
  48. const char *fname = ops->get_function_name(pctldev,
  49. selector);
  50. if (!fname) {
  51. dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
  52. selector);
  53. return -EINVAL;
  54. }
  55. selector++;
  56. }
  57. return 0;
  58. }
  59. int pinmux_validate_map(struct pinctrl_map const *map, int i)
  60. {
  61. if (!map->data.mux.function) {
  62. pr_err("failed to register map %s (%d): no function given\n",
  63. map->name, i);
  64. return -EINVAL;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * pin_request() - request a single pin to be muxed in, typically for GPIO
  70. * @pin: the pin number in the global pin space
  71. * @owner: a representation of the owner of this pin; typically the device
  72. * name that controls its mux function, or the requested GPIO name
  73. * @gpio_range: the range matching the GPIO pin if this is a request for a
  74. * single GPIO pin
  75. */
  76. static int pin_request(struct pinctrl_dev *pctldev,
  77. int pin, const char *owner,
  78. struct pinctrl_gpio_range *gpio_range)
  79. {
  80. struct pin_desc *desc;
  81. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  82. int status = -EINVAL;
  83. desc = pin_desc_get(pctldev, pin);
  84. if (desc == NULL) {
  85. dev_err(pctldev->dev,
  86. "pin %d is not registered so it cannot be requested\n",
  87. pin);
  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 %s already requested by %s; cannot claim for %s\n",
  97. desc->name, desc->gpio_owner, owner);
  98. goto out;
  99. }
  100. desc->gpio_owner = owner;
  101. } else {
  102. if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
  103. dev_err(pctldev->dev,
  104. "pin %s already requested by %s; cannot claim for %s\n",
  105. desc->name, desc->mux_owner, owner);
  106. goto out;
  107. }
  108. desc->mux_usecount++;
  109. if (desc->mux_usecount > 1)
  110. return 0;
  111. desc->mux_owner = owner;
  112. }
  113. /* Let each pin increase references to this module */
  114. if (!try_module_get(pctldev->owner)) {
  115. dev_err(pctldev->dev,
  116. "could not increase module refcount for pin %d\n",
  117. pin);
  118. status = -EINVAL;
  119. goto out_free_pin;
  120. }
  121. /*
  122. * If there is no kind of request function for the pin we just assume
  123. * we got it by default and proceed.
  124. */
  125. if (gpio_range && ops->gpio_request_enable)
  126. /* This requests and enables a single GPIO pin */
  127. status = ops->gpio_request_enable(pctldev, gpio_range, pin);
  128. else if (ops->request)
  129. status = ops->request(pctldev, pin);
  130. else
  131. status = 0;
  132. if (status) {
  133. dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
  134. module_put(pctldev->owner);
  135. }
  136. out_free_pin:
  137. if (status) {
  138. if (gpio_range) {
  139. desc->gpio_owner = NULL;
  140. } else {
  141. desc->mux_usecount--;
  142. if (!desc->mux_usecount)
  143. desc->mux_owner = NULL;
  144. }
  145. }
  146. out:
  147. if (status)
  148. dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
  149. pin, owner, status);
  150. return status;
  151. }
  152. /**
  153. * pin_free() - release a single muxed in pin so something else can be muxed
  154. * @pctldev: pin controller device handling this pin
  155. * @pin: the pin to free
  156. * @gpio_range: the range matching the GPIO pin if this is a request for a
  157. * single GPIO pin
  158. *
  159. * This function returns a pointer to the previous owner. This is used
  160. * for callers that dynamically allocate an owner name so it can be freed
  161. * once the pin is free. This is done for GPIO request functions.
  162. */
  163. static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
  164. struct pinctrl_gpio_range *gpio_range)
  165. {
  166. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  167. struct pin_desc *desc;
  168. const char *owner;
  169. desc = pin_desc_get(pctldev, pin);
  170. if (desc == NULL) {
  171. dev_err(pctldev->dev,
  172. "pin is not registered so it cannot be freed\n");
  173. return NULL;
  174. }
  175. if (!gpio_range) {
  176. desc->mux_usecount--;
  177. if (desc->mux_usecount)
  178. return NULL;
  179. }
  180. /*
  181. * If there is no kind of request function for the pin we just assume
  182. * we got it by default and proceed.
  183. */
  184. if (gpio_range && ops->gpio_disable_free)
  185. ops->gpio_disable_free(pctldev, gpio_range, pin);
  186. else if (ops->free)
  187. ops->free(pctldev, pin);
  188. if (gpio_range) {
  189. owner = desc->gpio_owner;
  190. desc->gpio_owner = NULL;
  191. } else {
  192. owner = desc->mux_owner;
  193. desc->mux_owner = NULL;
  194. desc->mux_setting = NULL;
  195. }
  196. module_put(pctldev->owner);
  197. return owner;
  198. }
  199. /**
  200. * pinmux_request_gpio() - request pinmuxing for a GPIO pin
  201. * @pctldev: pin controller device affected
  202. * @pin: the pin to mux in for GPIO
  203. * @range: the applicable GPIO range
  204. */
  205. int pinmux_request_gpio(struct pinctrl_dev *pctldev,
  206. struct pinctrl_gpio_range *range,
  207. unsigned pin, unsigned gpio)
  208. {
  209. char gpiostr[16];
  210. const char *owner;
  211. int ret;
  212. /* Conjure some name stating what chip and pin this is taken by */
  213. snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
  214. owner = kstrdup(gpiostr, GFP_KERNEL);
  215. if (!owner)
  216. return -EINVAL;
  217. ret = pin_request(pctldev, pin, owner, range);
  218. if (ret < 0)
  219. kfree(owner);
  220. return ret;
  221. }
  222. /**
  223. * pinmux_free_gpio() - release a pin from GPIO muxing
  224. * @pctldev: the pin controller device for the pin
  225. * @pin: the affected currently GPIO-muxed in pin
  226. * @range: applicable GPIO range
  227. */
  228. void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
  229. struct pinctrl_gpio_range *range)
  230. {
  231. const char *owner;
  232. owner = pin_free(pctldev, pin, range);
  233. kfree(owner);
  234. }
  235. /**
  236. * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
  237. * @pctldev: the pin controller handling this pin
  238. * @range: applicable GPIO range
  239. * @pin: the affected GPIO pin in this controller
  240. * @input: true if we set the pin as input, false for output
  241. */
  242. int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
  243. struct pinctrl_gpio_range *range,
  244. unsigned pin, bool input)
  245. {
  246. const struct pinmux_ops *ops;
  247. int ret;
  248. ops = pctldev->desc->pmxops;
  249. if (ops->gpio_set_direction)
  250. ret = ops->gpio_set_direction(pctldev, range, pin, input);
  251. else
  252. ret = 0;
  253. return ret;
  254. }
  255. static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
  256. const char *function)
  257. {
  258. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  259. unsigned nfuncs = ops->get_functions_count(pctldev);
  260. unsigned selector = 0;
  261. /* See if this pctldev has this function */
  262. while (selector < nfuncs) {
  263. const char *fname = ops->get_function_name(pctldev,
  264. selector);
  265. if (!strcmp(function, fname))
  266. return selector;
  267. selector++;
  268. }
  269. pr_err("%s does not support function %s\n",
  270. pinctrl_dev_get_name(pctldev), function);
  271. return -EINVAL;
  272. }
  273. int pinmux_map_to_setting(struct pinctrl_map const *map,
  274. struct pinctrl_setting *setting)
  275. {
  276. struct pinctrl_dev *pctldev = setting->pctldev;
  277. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  278. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  279. char const * const *groups;
  280. unsigned num_groups;
  281. int ret;
  282. const char *group;
  283. int i;
  284. const unsigned *pins;
  285. unsigned num_pins;
  286. if (!pmxops) {
  287. dev_err(pctldev->dev, "does not support mux function\n");
  288. return -EINVAL;
  289. }
  290. ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
  291. if (ret < 0) {
  292. dev_err(pctldev->dev, "invalid function %s in map table\n",
  293. map->data.mux.function);
  294. return ret;
  295. }
  296. setting->data.mux.func = ret;
  297. ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
  298. &groups, &num_groups);
  299. if (ret < 0) {
  300. dev_err(pctldev->dev, "can't query groups for function %s\n",
  301. map->data.mux.function);
  302. return ret;
  303. }
  304. if (!num_groups) {
  305. dev_err(pctldev->dev,
  306. "function %s can't be selected on any group\n",
  307. map->data.mux.function);
  308. return -EINVAL;
  309. }
  310. if (map->data.mux.group) {
  311. bool found = false;
  312. group = map->data.mux.group;
  313. for (i = 0; i < num_groups; i++) {
  314. if (!strcmp(group, groups[i])) {
  315. found = true;
  316. break;
  317. }
  318. }
  319. if (!found) {
  320. dev_err(pctldev->dev,
  321. "invalid group \"%s\" for function \"%s\"\n",
  322. group, map->data.mux.function);
  323. return -EINVAL;
  324. }
  325. } else {
  326. group = groups[0];
  327. }
  328. ret = pinctrl_get_group_selector(pctldev, group);
  329. if (ret < 0) {
  330. dev_err(pctldev->dev, "invalid group %s in map table\n",
  331. map->data.mux.group);
  332. return ret;
  333. }
  334. setting->data.mux.group = ret;
  335. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
  336. &num_pins);
  337. if (ret) {
  338. dev_err(pctldev->dev,
  339. "could not get pins for device %s group selector %d\n",
  340. pinctrl_dev_get_name(pctldev), setting->data.mux.group);
  341. return -ENODEV;
  342. }
  343. /* Try to allocate all pins in this group, one by one */
  344. for (i = 0; i < num_pins; i++) {
  345. ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
  346. if (ret) {
  347. dev_err(pctldev->dev,
  348. "could not request pin %d on device %s\n",
  349. pins[i], pinctrl_dev_get_name(pctldev));
  350. /* On error release all taken pins */
  351. i--; /* this pin just failed */
  352. for (; i >= 0; i--)
  353. pin_free(pctldev, pins[i], NULL);
  354. return -ENODEV;
  355. }
  356. }
  357. return 0;
  358. }
  359. void pinmux_free_setting(struct pinctrl_setting const *setting)
  360. {
  361. struct pinctrl_dev *pctldev = setting->pctldev;
  362. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  363. const unsigned *pins;
  364. unsigned num_pins;
  365. int ret;
  366. int i;
  367. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  368. &pins, &num_pins);
  369. if (ret) {
  370. dev_err(pctldev->dev,
  371. "could not get pins for device %s group selector %d\n",
  372. pinctrl_dev_get_name(pctldev), setting->data.mux.group);
  373. return;
  374. }
  375. for (i = 0; i < num_pins; i++)
  376. pin_free(pctldev, pins[i], NULL);
  377. }
  378. int pinmux_enable_setting(struct pinctrl_setting const *setting)
  379. {
  380. struct pinctrl_dev *pctldev = setting->pctldev;
  381. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  382. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  383. int ret;
  384. const unsigned *pins;
  385. unsigned num_pins;
  386. int i;
  387. struct pin_desc *desc;
  388. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  389. &pins, &num_pins);
  390. if (ret) {
  391. /* errors only affect debug data, so just warn */
  392. dev_warn(pctldev->dev,
  393. "could not get pins for group selector %d\n",
  394. setting->data.mux.group);
  395. num_pins = 0;
  396. }
  397. for (i = 0; i < num_pins; i++) {
  398. desc = pin_desc_get(pctldev, pins[i]);
  399. if (desc == NULL) {
  400. dev_warn(pctldev->dev,
  401. "could not get pin desc for pin %d\n",
  402. pins[i]);
  403. continue;
  404. }
  405. desc->mux_setting = &(setting->data.mux);
  406. }
  407. return ops->enable(pctldev, setting->data.mux.func,
  408. setting->data.mux.group);
  409. }
  410. void pinmux_disable_setting(struct pinctrl_setting const *setting)
  411. {
  412. struct pinctrl_dev *pctldev = setting->pctldev;
  413. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  414. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  415. int ret;
  416. const unsigned *pins;
  417. unsigned num_pins;
  418. int i;
  419. struct pin_desc *desc;
  420. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  421. &pins, &num_pins);
  422. if (ret) {
  423. /* errors only affect debug data, so just warn */
  424. dev_warn(pctldev->dev,
  425. "could not get pins for group selector %d\n",
  426. setting->data.mux.group);
  427. num_pins = 0;
  428. }
  429. for (i = 0; i < num_pins; i++) {
  430. desc = pin_desc_get(pctldev, pins[i]);
  431. if (desc == NULL) {
  432. dev_warn(pctldev->dev,
  433. "could not get pin desc for pin %d\n",
  434. pins[i]);
  435. continue;
  436. }
  437. desc->mux_setting = NULL;
  438. }
  439. if (ops->disable)
  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 */