pinmux.c 21 KB

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