pinmux.c 15 KB

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