pinmux.c 16 KB

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