pinmux.c 21 KB

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