pinmux.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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->set_mux) {
  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(const struct pinctrl_map *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 || ops->strict) &&
  93. desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
  94. dev_err(pctldev->dev,
  95. "pin %s already requested by %s; cannot claim for %s\n",
  96. desc->name, desc->mux_owner, owner);
  97. goto out;
  98. }
  99. if ((gpio_range || ops->strict) && desc->gpio_owner) {
  100. dev_err(pctldev->dev,
  101. "pin %s already requested by %s; cannot claim for %s\n",
  102. desc->name, desc->gpio_owner, owner);
  103. goto out;
  104. }
  105. if (gpio_range) {
  106. desc->gpio_owner = owner;
  107. } else {
  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. /*
  177. * A pin should not be freed more times than allocated.
  178. */
  179. if (WARN_ON(!desc->mux_usecount))
  180. return NULL;
  181. desc->mux_usecount--;
  182. if (desc->mux_usecount)
  183. return NULL;
  184. }
  185. /*
  186. * If there is no kind of request function for the pin we just assume
  187. * we got it by default and proceed.
  188. */
  189. if (gpio_range && ops->gpio_disable_free)
  190. ops->gpio_disable_free(pctldev, gpio_range, pin);
  191. else if (ops->free)
  192. ops->free(pctldev, pin);
  193. if (gpio_range) {
  194. owner = desc->gpio_owner;
  195. desc->gpio_owner = NULL;
  196. } else {
  197. owner = desc->mux_owner;
  198. desc->mux_owner = NULL;
  199. desc->mux_setting = NULL;
  200. }
  201. module_put(pctldev->owner);
  202. return owner;
  203. }
  204. /**
  205. * pinmux_request_gpio() - request pinmuxing for a GPIO pin
  206. * @pctldev: pin controller device affected
  207. * @pin: the pin to mux in for GPIO
  208. * @range: the applicable GPIO range
  209. */
  210. int pinmux_request_gpio(struct pinctrl_dev *pctldev,
  211. struct pinctrl_gpio_range *range,
  212. unsigned pin, unsigned gpio)
  213. {
  214. const char *owner;
  215. int ret;
  216. /* Conjure some name stating what chip and pin this is taken by */
  217. owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
  218. if (!owner)
  219. return -ENOMEM;
  220. ret = pin_request(pctldev, pin, owner, range);
  221. if (ret < 0)
  222. kfree(owner);
  223. return ret;
  224. }
  225. /**
  226. * pinmux_free_gpio() - release a pin from GPIO muxing
  227. * @pctldev: the pin controller device for the pin
  228. * @pin: the affected currently GPIO-muxed in pin
  229. * @range: applicable GPIO range
  230. */
  231. void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
  232. struct pinctrl_gpio_range *range)
  233. {
  234. const char *owner;
  235. owner = pin_free(pctldev, pin, range);
  236. kfree(owner);
  237. }
  238. /**
  239. * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
  240. * @pctldev: the pin controller handling this pin
  241. * @range: applicable GPIO range
  242. * @pin: the affected GPIO pin in this controller
  243. * @input: true if we set the pin as input, false for output
  244. */
  245. int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
  246. struct pinctrl_gpio_range *range,
  247. unsigned pin, bool input)
  248. {
  249. const struct pinmux_ops *ops;
  250. int ret;
  251. ops = pctldev->desc->pmxops;
  252. if (ops->gpio_set_direction)
  253. ret = ops->gpio_set_direction(pctldev, range, pin, input);
  254. else
  255. ret = 0;
  256. return ret;
  257. }
  258. static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
  259. const char *function)
  260. {
  261. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  262. unsigned nfuncs = ops->get_functions_count(pctldev);
  263. unsigned selector = 0;
  264. /* See if this pctldev has this function */
  265. while (selector < nfuncs) {
  266. const char *fname = ops->get_function_name(pctldev, selector);
  267. if (!strcmp(function, fname))
  268. return selector;
  269. selector++;
  270. }
  271. dev_err(pctldev->dev, "function '%s' not supported\n", function);
  272. return -EINVAL;
  273. }
  274. int pinmux_map_to_setting(const struct pinctrl_map *map,
  275. struct pinctrl_setting *setting)
  276. {
  277. struct pinctrl_dev *pctldev = setting->pctldev;
  278. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  279. char const * const *groups;
  280. unsigned num_groups;
  281. int ret;
  282. const char *group;
  283. if (!pmxops) {
  284. dev_err(pctldev->dev, "does not support mux function\n");
  285. return -EINVAL;
  286. }
  287. ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
  288. if (ret < 0) {
  289. dev_err(pctldev->dev, "invalid function %s in map table\n",
  290. map->data.mux.function);
  291. return ret;
  292. }
  293. setting->data.mux.func = ret;
  294. ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
  295. &groups, &num_groups);
  296. if (ret < 0) {
  297. dev_err(pctldev->dev, "can't query groups for function %s\n",
  298. map->data.mux.function);
  299. return ret;
  300. }
  301. if (!num_groups) {
  302. dev_err(pctldev->dev,
  303. "function %s can't be selected on any group\n",
  304. map->data.mux.function);
  305. return -EINVAL;
  306. }
  307. if (map->data.mux.group) {
  308. group = map->data.mux.group;
  309. ret = match_string(groups, num_groups, group);
  310. if (ret < 0) {
  311. dev_err(pctldev->dev,
  312. "invalid group \"%s\" for function \"%s\"\n",
  313. group, map->data.mux.function);
  314. return ret;
  315. }
  316. } else {
  317. group = groups[0];
  318. }
  319. ret = pinctrl_get_group_selector(pctldev, group);
  320. if (ret < 0) {
  321. dev_err(pctldev->dev, "invalid group %s in map table\n",
  322. map->data.mux.group);
  323. return ret;
  324. }
  325. setting->data.mux.group = ret;
  326. return 0;
  327. }
  328. void pinmux_free_setting(const struct pinctrl_setting *setting)
  329. {
  330. /* This function is currently unused */
  331. }
  332. int pinmux_enable_setting(const struct pinctrl_setting *setting)
  333. {
  334. struct pinctrl_dev *pctldev = setting->pctldev;
  335. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  336. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  337. int ret = 0;
  338. const unsigned *pins = NULL;
  339. unsigned num_pins = 0;
  340. int i;
  341. struct pin_desc *desc;
  342. if (pctlops->get_group_pins)
  343. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  344. &pins, &num_pins);
  345. if (ret) {
  346. const char *gname;
  347. /* errors only affect debug data, so just warn */
  348. gname = pctlops->get_group_name(pctldev,
  349. setting->data.mux.group);
  350. dev_warn(pctldev->dev,
  351. "could not get pins for group %s\n",
  352. gname);
  353. num_pins = 0;
  354. }
  355. /* Try to allocate all pins in this group, one by one */
  356. for (i = 0; i < num_pins; i++) {
  357. ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
  358. if (ret) {
  359. const char *gname;
  360. const char *pname;
  361. desc = pin_desc_get(pctldev, pins[i]);
  362. pname = desc ? desc->name : "non-existing";
  363. gname = pctlops->get_group_name(pctldev,
  364. setting->data.mux.group);
  365. dev_err(pctldev->dev,
  366. "could not request pin %d (%s) from group %s "
  367. " on device %s\n",
  368. pins[i], pname, gname,
  369. pinctrl_dev_get_name(pctldev));
  370. goto err_pin_request;
  371. }
  372. }
  373. /* Now that we have acquired the pins, encode the mux setting */
  374. for (i = 0; i < num_pins; i++) {
  375. desc = pin_desc_get(pctldev, pins[i]);
  376. if (desc == NULL) {
  377. dev_warn(pctldev->dev,
  378. "could not get pin desc for pin %d\n",
  379. pins[i]);
  380. continue;
  381. }
  382. desc->mux_setting = &(setting->data.mux);
  383. }
  384. ret = ops->set_mux(pctldev, setting->data.mux.func,
  385. setting->data.mux.group);
  386. if (ret)
  387. goto err_set_mux;
  388. return 0;
  389. err_set_mux:
  390. for (i = 0; i < num_pins; i++) {
  391. desc = pin_desc_get(pctldev, pins[i]);
  392. if (desc)
  393. desc->mux_setting = NULL;
  394. }
  395. err_pin_request:
  396. /* On error release all taken pins */
  397. while (--i >= 0)
  398. pin_free(pctldev, pins[i], NULL);
  399. return ret;
  400. }
  401. void pinmux_disable_setting(const struct pinctrl_setting *setting)
  402. {
  403. struct pinctrl_dev *pctldev = setting->pctldev;
  404. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  405. int ret = 0;
  406. const unsigned *pins = NULL;
  407. unsigned num_pins = 0;
  408. int i;
  409. struct pin_desc *desc;
  410. if (pctlops->get_group_pins)
  411. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  412. &pins, &num_pins);
  413. if (ret) {
  414. const char *gname;
  415. /* errors only affect debug data, so just warn */
  416. gname = pctlops->get_group_name(pctldev,
  417. setting->data.mux.group);
  418. dev_warn(pctldev->dev,
  419. "could not get pins for group %s\n",
  420. gname);
  421. num_pins = 0;
  422. }
  423. /* Flag the descs that no setting is active */
  424. for (i = 0; i < num_pins; i++) {
  425. desc = pin_desc_get(pctldev, pins[i]);
  426. if (desc == NULL) {
  427. dev_warn(pctldev->dev,
  428. "could not get pin desc for pin %d\n",
  429. pins[i]);
  430. continue;
  431. }
  432. if (desc->mux_setting == &(setting->data.mux)) {
  433. desc->mux_setting = NULL;
  434. /* And release the pin */
  435. pin_free(pctldev, pins[i], NULL);
  436. } else {
  437. const char *gname;
  438. gname = pctlops->get_group_name(pctldev,
  439. setting->data.mux.group);
  440. dev_warn(pctldev->dev,
  441. "not freeing pin %d (%s) as part of "
  442. "deactivating group %s - it is already "
  443. "used for some other setting",
  444. pins[i], desc->name, gname);
  445. }
  446. }
  447. }
  448. #ifdef CONFIG_DEBUG_FS
  449. /* Called from pincontrol core */
  450. static int pinmux_functions_show(struct seq_file *s, void *what)
  451. {
  452. struct pinctrl_dev *pctldev = s->private;
  453. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  454. unsigned nfuncs;
  455. unsigned func_selector = 0;
  456. if (!pmxops)
  457. return 0;
  458. mutex_lock(&pctldev->mutex);
  459. nfuncs = pmxops->get_functions_count(pctldev);
  460. while (func_selector < nfuncs) {
  461. const char *func = pmxops->get_function_name(pctldev,
  462. func_selector);
  463. const char * const *groups;
  464. unsigned num_groups;
  465. int ret;
  466. int i;
  467. ret = pmxops->get_function_groups(pctldev, func_selector,
  468. &groups, &num_groups);
  469. if (ret) {
  470. seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
  471. func);
  472. func_selector++;
  473. continue;
  474. }
  475. seq_printf(s, "function: %s, groups = [ ", func);
  476. for (i = 0; i < num_groups; i++)
  477. seq_printf(s, "%s ", groups[i]);
  478. seq_puts(s, "]\n");
  479. func_selector++;
  480. }
  481. mutex_unlock(&pctldev->mutex);
  482. return 0;
  483. }
  484. static int pinmux_pins_show(struct seq_file *s, void *what)
  485. {
  486. struct pinctrl_dev *pctldev = s->private;
  487. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  488. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  489. unsigned i, pin;
  490. if (!pmxops)
  491. return 0;
  492. seq_puts(s, "Pinmux settings per pin\n");
  493. if (pmxops->strict)
  494. seq_puts(s,
  495. "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
  496. else
  497. seq_puts(s,
  498. "Format: pin (name): mux_owner gpio_owner hog?\n");
  499. mutex_lock(&pctldev->mutex);
  500. /* The pin number can be retrived from the pin controller descriptor */
  501. for (i = 0; i < pctldev->desc->npins; i++) {
  502. struct pin_desc *desc;
  503. bool is_hog = false;
  504. pin = pctldev->desc->pins[i].number;
  505. desc = pin_desc_get(pctldev, pin);
  506. /* Skip if we cannot search the pin */
  507. if (desc == NULL)
  508. continue;
  509. if (desc->mux_owner &&
  510. !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
  511. is_hog = true;
  512. if (pmxops->strict) {
  513. if (desc->mux_owner)
  514. seq_printf(s, "pin %d (%s): device %s%s",
  515. pin, desc->name, desc->mux_owner,
  516. is_hog ? " (HOG)" : "");
  517. else if (desc->gpio_owner)
  518. seq_printf(s, "pin %d (%s): GPIO %s",
  519. pin, desc->name, desc->gpio_owner);
  520. else
  521. seq_printf(s, "pin %d (%s): UNCLAIMED",
  522. pin, desc->name);
  523. } else {
  524. /* For non-strict controllers */
  525. seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
  526. desc->mux_owner ? desc->mux_owner
  527. : "(MUX UNCLAIMED)",
  528. desc->gpio_owner ? desc->gpio_owner
  529. : "(GPIO UNCLAIMED)",
  530. is_hog ? " (HOG)" : "");
  531. }
  532. /* If mux: print function+group claiming the pin */
  533. if (desc->mux_setting)
  534. seq_printf(s, " function %s group %s\n",
  535. pmxops->get_function_name(pctldev,
  536. desc->mux_setting->func),
  537. pctlops->get_group_name(pctldev,
  538. desc->mux_setting->group));
  539. else
  540. seq_printf(s, "\n");
  541. }
  542. mutex_unlock(&pctldev->mutex);
  543. return 0;
  544. }
  545. void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
  546. {
  547. seq_printf(s, "group %s\nfunction %s\n",
  548. map->data.mux.group ? map->data.mux.group : "(default)",
  549. map->data.mux.function);
  550. }
  551. void pinmux_show_setting(struct seq_file *s,
  552. const struct pinctrl_setting *setting)
  553. {
  554. struct pinctrl_dev *pctldev = setting->pctldev;
  555. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  556. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  557. seq_printf(s, "group: %s (%u) function: %s (%u)\n",
  558. pctlops->get_group_name(pctldev, setting->data.mux.group),
  559. setting->data.mux.group,
  560. pmxops->get_function_name(pctldev, setting->data.mux.func),
  561. setting->data.mux.func);
  562. }
  563. static int pinmux_functions_open(struct inode *inode, struct file *file)
  564. {
  565. return single_open(file, pinmux_functions_show, inode->i_private);
  566. }
  567. static int pinmux_pins_open(struct inode *inode, struct file *file)
  568. {
  569. return single_open(file, pinmux_pins_show, inode->i_private);
  570. }
  571. static const struct file_operations pinmux_functions_ops = {
  572. .open = pinmux_functions_open,
  573. .read = seq_read,
  574. .llseek = seq_lseek,
  575. .release = single_release,
  576. };
  577. static const struct file_operations pinmux_pins_ops = {
  578. .open = pinmux_pins_open,
  579. .read = seq_read,
  580. .llseek = seq_lseek,
  581. .release = single_release,
  582. };
  583. void pinmux_init_device_debugfs(struct dentry *devroot,
  584. struct pinctrl_dev *pctldev)
  585. {
  586. debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
  587. devroot, pctldev, &pinmux_functions_ops);
  588. debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
  589. devroot, pctldev, &pinmux_pins_ops);
  590. }
  591. #endif /* CONFIG_DEBUG_FS */
  592. #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
  593. /**
  594. * pinmux_generic_get_function_count() - returns number of functions
  595. * @pctldev: pin controller device
  596. */
  597. int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
  598. {
  599. return pctldev->num_functions;
  600. }
  601. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
  602. /**
  603. * pinmux_generic_get_function_name() - returns the function name
  604. * @pctldev: pin controller device
  605. * @selector: function number
  606. */
  607. const char *
  608. pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
  609. unsigned int selector)
  610. {
  611. struct function_desc *function;
  612. function = radix_tree_lookup(&pctldev->pin_function_tree,
  613. selector);
  614. if (!function)
  615. return NULL;
  616. return function->name;
  617. }
  618. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
  619. /**
  620. * pinmux_generic_get_function_groups() - gets the function groups
  621. * @pctldev: pin controller device
  622. * @selector: function number
  623. * @groups: array of pin groups
  624. * @num_groups: number of pin groups
  625. */
  626. int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
  627. unsigned int selector,
  628. const char * const **groups,
  629. unsigned * const num_groups)
  630. {
  631. struct function_desc *function;
  632. function = radix_tree_lookup(&pctldev->pin_function_tree,
  633. selector);
  634. if (!function) {
  635. dev_err(pctldev->dev, "%s could not find function%i\n",
  636. __func__, selector);
  637. return -EINVAL;
  638. }
  639. *groups = function->group_names;
  640. *num_groups = function->num_group_names;
  641. return 0;
  642. }
  643. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
  644. /**
  645. * pinmux_generic_get_function() - returns a function based on the number
  646. * @pctldev: pin controller device
  647. * @group_selector: function number
  648. */
  649. struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
  650. unsigned int selector)
  651. {
  652. struct function_desc *function;
  653. function = radix_tree_lookup(&pctldev->pin_function_tree,
  654. selector);
  655. if (!function)
  656. return NULL;
  657. return function;
  658. }
  659. EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
  660. /**
  661. * pinmux_generic_add_function() - adds a function group
  662. * @pctldev: pin controller device
  663. * @name: name of the function
  664. * @groups: array of pin groups
  665. * @num_groups: number of pin groups
  666. * @data: pin controller driver specific data
  667. */
  668. int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
  669. const char *name,
  670. const char **groups,
  671. const unsigned int num_groups,
  672. void *data)
  673. {
  674. struct function_desc *function;
  675. function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
  676. if (!function)
  677. return -ENOMEM;
  678. function->name = name;
  679. function->group_names = groups;
  680. function->num_group_names = num_groups;
  681. function->data = data;
  682. radix_tree_insert(&pctldev->pin_function_tree, pctldev->num_functions,
  683. function);
  684. pctldev->num_functions++;
  685. return 0;
  686. }
  687. EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
  688. /**
  689. * pinmux_generic_remove_function() - removes a numbered function
  690. * @pctldev: pin controller device
  691. * @selector: function number
  692. *
  693. * Note that the caller must take care of locking.
  694. */
  695. int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
  696. unsigned int selector)
  697. {
  698. struct function_desc *function;
  699. function = radix_tree_lookup(&pctldev->pin_function_tree,
  700. selector);
  701. if (!function)
  702. return -ENOENT;
  703. radix_tree_delete(&pctldev->pin_function_tree, selector);
  704. devm_kfree(pctldev->dev, function);
  705. pctldev->num_functions--;
  706. return 0;
  707. }
  708. EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
  709. /**
  710. * pinmux_generic_free_functions() - removes all functions
  711. * @pctldev: pin controller device
  712. *
  713. * Note that the caller must take care of locking. The pinctrl
  714. * functions are allocated with devm_kzalloc() so no need to free
  715. * them here.
  716. */
  717. void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
  718. {
  719. struct radix_tree_iter iter;
  720. void __rcu **slot;
  721. radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
  722. radix_tree_delete(&pctldev->pin_function_tree, iter.index);
  723. pctldev->num_functions = 0;
  724. }
  725. #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */