pinmux.c 15 KB

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