pinmux.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. pin_free(pctldev, pins[i], NULL);
  434. } else {
  435. const char *gname;
  436. gname = pctlops->get_group_name(pctldev,
  437. setting->data.mux.group);
  438. dev_warn(pctldev->dev,
  439. "not freeing pin %d (%s) as part of "
  440. "deactivating group %s - it is already "
  441. "used for some other setting",
  442. pins[i], desc->name, gname);
  443. }
  444. }
  445. }
  446. #ifdef CONFIG_DEBUG_FS
  447. /* Called from pincontrol core */
  448. static int pinmux_functions_show(struct seq_file *s, void *what)
  449. {
  450. struct pinctrl_dev *pctldev = s->private;
  451. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  452. unsigned nfuncs;
  453. unsigned func_selector = 0;
  454. if (!pmxops)
  455. return 0;
  456. mutex_lock(&pctldev->mutex);
  457. nfuncs = pmxops->get_functions_count(pctldev);
  458. while (func_selector < nfuncs) {
  459. const char *func = pmxops->get_function_name(pctldev,
  460. func_selector);
  461. const char * const *groups;
  462. unsigned num_groups;
  463. int ret;
  464. int i;
  465. ret = pmxops->get_function_groups(pctldev, func_selector,
  466. &groups, &num_groups);
  467. if (ret) {
  468. seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
  469. func);
  470. func_selector++;
  471. continue;
  472. }
  473. seq_printf(s, "function: %s, groups = [ ", func);
  474. for (i = 0; i < num_groups; i++)
  475. seq_printf(s, "%s ", groups[i]);
  476. seq_puts(s, "]\n");
  477. func_selector++;
  478. }
  479. mutex_unlock(&pctldev->mutex);
  480. return 0;
  481. }
  482. static int pinmux_pins_show(struct seq_file *s, void *what)
  483. {
  484. struct pinctrl_dev *pctldev = s->private;
  485. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  486. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  487. unsigned i, pin;
  488. if (!pmxops)
  489. return 0;
  490. seq_puts(s, "Pinmux settings per pin\n");
  491. if (pmxops->strict)
  492. seq_puts(s,
  493. "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
  494. else
  495. seq_puts(s,
  496. "Format: pin (name): mux_owner gpio_owner hog?\n");
  497. mutex_lock(&pctldev->mutex);
  498. /* The pin number can be retrived from the pin controller descriptor */
  499. for (i = 0; i < pctldev->desc->npins; i++) {
  500. struct pin_desc *desc;
  501. bool is_hog = false;
  502. pin = pctldev->desc->pins[i].number;
  503. desc = pin_desc_get(pctldev, pin);
  504. /* Skip if we cannot search the pin */
  505. if (desc == NULL)
  506. continue;
  507. if (desc->mux_owner &&
  508. !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
  509. is_hog = true;
  510. if (pmxops->strict) {
  511. if (desc->mux_owner)
  512. seq_printf(s, "pin %d (%s): device %s%s",
  513. pin, desc->name, desc->mux_owner,
  514. is_hog ? " (HOG)" : "");
  515. else if (desc->gpio_owner)
  516. seq_printf(s, "pin %d (%s): GPIO %s",
  517. pin, desc->name, desc->gpio_owner);
  518. else
  519. seq_printf(s, "pin %d (%s): UNCLAIMED",
  520. pin, desc->name);
  521. } else {
  522. /* For non-strict controllers */
  523. seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
  524. desc->mux_owner ? desc->mux_owner
  525. : "(MUX UNCLAIMED)",
  526. desc->gpio_owner ? desc->gpio_owner
  527. : "(GPIO UNCLAIMED)",
  528. is_hog ? " (HOG)" : "");
  529. }
  530. /* If mux: print function+group claiming the pin */
  531. if (desc->mux_setting)
  532. seq_printf(s, " function %s group %s\n",
  533. pmxops->get_function_name(pctldev,
  534. desc->mux_setting->func),
  535. pctlops->get_group_name(pctldev,
  536. desc->mux_setting->group));
  537. else
  538. seq_putc(s, '\n');
  539. }
  540. mutex_unlock(&pctldev->mutex);
  541. return 0;
  542. }
  543. void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
  544. {
  545. seq_printf(s, "group %s\nfunction %s\n",
  546. map->data.mux.group ? map->data.mux.group : "(default)",
  547. map->data.mux.function);
  548. }
  549. void pinmux_show_setting(struct seq_file *s,
  550. const struct pinctrl_setting *setting)
  551. {
  552. struct pinctrl_dev *pctldev = setting->pctldev;
  553. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  554. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  555. seq_printf(s, "group: %s (%u) function: %s (%u)\n",
  556. pctlops->get_group_name(pctldev, setting->data.mux.group),
  557. setting->data.mux.group,
  558. pmxops->get_function_name(pctldev, setting->data.mux.func),
  559. setting->data.mux.func);
  560. }
  561. static int pinmux_functions_open(struct inode *inode, struct file *file)
  562. {
  563. return single_open(file, pinmux_functions_show, inode->i_private);
  564. }
  565. static int pinmux_pins_open(struct inode *inode, struct file *file)
  566. {
  567. return single_open(file, pinmux_pins_show, inode->i_private);
  568. }
  569. static const struct file_operations pinmux_functions_ops = {
  570. .open = pinmux_functions_open,
  571. .read = seq_read,
  572. .llseek = seq_lseek,
  573. .release = single_release,
  574. };
  575. static const struct file_operations pinmux_pins_ops = {
  576. .open = pinmux_pins_open,
  577. .read = seq_read,
  578. .llseek = seq_lseek,
  579. .release = single_release,
  580. };
  581. void pinmux_init_device_debugfs(struct dentry *devroot,
  582. struct pinctrl_dev *pctldev)
  583. {
  584. debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
  585. devroot, pctldev, &pinmux_functions_ops);
  586. debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
  587. devroot, pctldev, &pinmux_pins_ops);
  588. }
  589. #endif /* CONFIG_DEBUG_FS */
  590. #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
  591. /**
  592. * pinmux_generic_get_function_count() - returns number of functions
  593. * @pctldev: pin controller device
  594. */
  595. int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
  596. {
  597. return pctldev->num_functions;
  598. }
  599. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
  600. /**
  601. * pinmux_generic_get_function_name() - returns the function name
  602. * @pctldev: pin controller device
  603. * @selector: function number
  604. */
  605. const char *
  606. pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
  607. unsigned int selector)
  608. {
  609. struct function_desc *function;
  610. function = radix_tree_lookup(&pctldev->pin_function_tree,
  611. selector);
  612. if (!function)
  613. return NULL;
  614. return function->name;
  615. }
  616. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
  617. /**
  618. * pinmux_generic_get_function_groups() - gets the function groups
  619. * @pctldev: pin controller device
  620. * @selector: function number
  621. * @groups: array of pin groups
  622. * @num_groups: number of pin groups
  623. */
  624. int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
  625. unsigned int selector,
  626. const char * const **groups,
  627. unsigned * const num_groups)
  628. {
  629. struct function_desc *function;
  630. function = radix_tree_lookup(&pctldev->pin_function_tree,
  631. selector);
  632. if (!function) {
  633. dev_err(pctldev->dev, "%s could not find function%i\n",
  634. __func__, selector);
  635. return -EINVAL;
  636. }
  637. *groups = function->group_names;
  638. *num_groups = function->num_group_names;
  639. return 0;
  640. }
  641. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
  642. /**
  643. * pinmux_generic_get_function() - returns a function based on the number
  644. * @pctldev: pin controller device
  645. * @group_selector: function number
  646. */
  647. struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
  648. unsigned int selector)
  649. {
  650. struct function_desc *function;
  651. function = radix_tree_lookup(&pctldev->pin_function_tree,
  652. selector);
  653. if (!function)
  654. return NULL;
  655. return function;
  656. }
  657. EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
  658. /**
  659. * pinmux_generic_add_function() - adds a function group
  660. * @pctldev: pin controller device
  661. * @name: name of the function
  662. * @groups: array of pin groups
  663. * @num_groups: number of pin groups
  664. * @data: pin controller driver specific data
  665. */
  666. int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
  667. const char *name,
  668. const char **groups,
  669. const unsigned int num_groups,
  670. void *data)
  671. {
  672. struct function_desc *function;
  673. function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
  674. if (!function)
  675. return -ENOMEM;
  676. function->name = name;
  677. function->group_names = groups;
  678. function->num_group_names = num_groups;
  679. function->data = data;
  680. radix_tree_insert(&pctldev->pin_function_tree, pctldev->num_functions,
  681. function);
  682. pctldev->num_functions++;
  683. return 0;
  684. }
  685. EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
  686. /**
  687. * pinmux_generic_remove_function() - removes a numbered function
  688. * @pctldev: pin controller device
  689. * @selector: function number
  690. *
  691. * Note that the caller must take care of locking.
  692. */
  693. int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
  694. unsigned int selector)
  695. {
  696. struct function_desc *function;
  697. function = radix_tree_lookup(&pctldev->pin_function_tree,
  698. selector);
  699. if (!function)
  700. return -ENOENT;
  701. radix_tree_delete(&pctldev->pin_function_tree, selector);
  702. devm_kfree(pctldev->dev, function);
  703. pctldev->num_functions--;
  704. return 0;
  705. }
  706. EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
  707. /**
  708. * pinmux_generic_free_functions() - removes all functions
  709. * @pctldev: pin controller device
  710. *
  711. * Note that the caller must take care of locking. The pinctrl
  712. * functions are allocated with devm_kzalloc() so no need to free
  713. * them here.
  714. */
  715. void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
  716. {
  717. struct radix_tree_iter iter;
  718. void __rcu **slot;
  719. radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
  720. radix_tree_delete(&pctldev->pin_function_tree, iter.index);
  721. pctldev->num_functions = 0;
  722. }
  723. #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */