pinmux.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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(struct pinctrl_map const *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_save_context(struct pinctrl_dev *pctldev, const char *function)
  275. {
  276. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  277. if (!pmxops || !pmxops->save_context)
  278. return -EINVAL;
  279. return pmxops->save_context(pctldev);
  280. }
  281. EXPORT_SYMBOL(pinmux_save_context);
  282. void pinmux_restore_context(struct pinctrl_dev *pctldev, const char *function)
  283. {
  284. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  285. if (!pmxops || !pmxops->restore_context)
  286. return;
  287. pmxops->restore_context(pctldev);
  288. }
  289. EXPORT_SYMBOL(pinmux_restore_context);
  290. int pinmux_map_to_setting(struct pinctrl_map const *map,
  291. struct pinctrl_setting *setting)
  292. {
  293. struct pinctrl_dev *pctldev = setting->pctldev;
  294. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  295. char const * const *groups;
  296. unsigned num_groups;
  297. int ret;
  298. const char *group;
  299. if (!pmxops) {
  300. dev_err(pctldev->dev, "does not support mux function\n");
  301. return -EINVAL;
  302. }
  303. ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
  304. if (ret < 0) {
  305. dev_err(pctldev->dev, "invalid function %s in map table\n",
  306. map->data.mux.function);
  307. return ret;
  308. }
  309. setting->data.mux.func = ret;
  310. ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
  311. &groups, &num_groups);
  312. if (ret < 0) {
  313. dev_err(pctldev->dev, "can't query groups for function %s\n",
  314. map->data.mux.function);
  315. return ret;
  316. }
  317. if (!num_groups) {
  318. dev_err(pctldev->dev,
  319. "function %s can't be selected on any group\n",
  320. map->data.mux.function);
  321. return -EINVAL;
  322. }
  323. if (map->data.mux.group) {
  324. group = map->data.mux.group;
  325. ret = match_string(groups, num_groups, group);
  326. if (ret < 0) {
  327. dev_err(pctldev->dev,
  328. "invalid group \"%s\" for function \"%s\"\n",
  329. group, map->data.mux.function);
  330. return ret;
  331. }
  332. } else {
  333. group = groups[0];
  334. }
  335. ret = pinctrl_get_group_selector(pctldev, group);
  336. if (ret < 0) {
  337. dev_err(pctldev->dev, "invalid group %s in map table\n",
  338. map->data.mux.group);
  339. return ret;
  340. }
  341. setting->data.mux.group = ret;
  342. return 0;
  343. }
  344. void pinmux_free_setting(struct pinctrl_setting const *setting)
  345. {
  346. /* This function is currently unused */
  347. }
  348. int pinmux_enable_setting(struct pinctrl_setting const *setting)
  349. {
  350. struct pinctrl_dev *pctldev = setting->pctldev;
  351. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  352. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  353. int ret = 0;
  354. const unsigned *pins = NULL;
  355. unsigned num_pins = 0;
  356. int i;
  357. struct pin_desc *desc;
  358. if (pctlops->get_group_pins)
  359. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  360. &pins, &num_pins);
  361. if (ret) {
  362. const char *gname;
  363. /* errors only affect debug data, so just warn */
  364. gname = pctlops->get_group_name(pctldev,
  365. setting->data.mux.group);
  366. dev_warn(pctldev->dev,
  367. "could not get pins for group %s\n",
  368. gname);
  369. num_pins = 0;
  370. }
  371. /* Try to allocate all pins in this group, one by one */
  372. for (i = 0; i < num_pins; i++) {
  373. ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
  374. if (ret) {
  375. const char *gname;
  376. const char *pname;
  377. desc = pin_desc_get(pctldev, pins[i]);
  378. pname = desc ? desc->name : "non-existing";
  379. gname = pctlops->get_group_name(pctldev,
  380. setting->data.mux.group);
  381. dev_err(pctldev->dev,
  382. "could not request pin %d (%s) from group %s "
  383. " on device %s\n",
  384. pins[i], pname, gname,
  385. pinctrl_dev_get_name(pctldev));
  386. goto err_pin_request;
  387. }
  388. }
  389. /* Now that we have acquired the pins, encode the mux setting */
  390. for (i = 0; i < num_pins; i++) {
  391. desc = pin_desc_get(pctldev, pins[i]);
  392. if (desc == NULL) {
  393. dev_warn(pctldev->dev,
  394. "could not get pin desc for pin %d\n",
  395. pins[i]);
  396. continue;
  397. }
  398. desc->mux_setting = &(setting->data.mux);
  399. }
  400. ret = ops->set_mux(pctldev, setting->data.mux.func,
  401. setting->data.mux.group);
  402. if (ret)
  403. goto err_set_mux;
  404. return 0;
  405. err_set_mux:
  406. for (i = 0; i < num_pins; i++) {
  407. desc = pin_desc_get(pctldev, pins[i]);
  408. if (desc)
  409. desc->mux_setting = NULL;
  410. }
  411. err_pin_request:
  412. /* On error release all taken pins */
  413. while (--i >= 0)
  414. pin_free(pctldev, pins[i], NULL);
  415. return ret;
  416. }
  417. void pinmux_disable_setting(struct pinctrl_setting const *setting)
  418. {
  419. struct pinctrl_dev *pctldev = setting->pctldev;
  420. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  421. int ret = 0;
  422. const unsigned *pins = NULL;
  423. unsigned num_pins = 0;
  424. int i;
  425. struct pin_desc *desc;
  426. if (pctlops->get_group_pins)
  427. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  428. &pins, &num_pins);
  429. if (ret) {
  430. const char *gname;
  431. /* errors only affect debug data, so just warn */
  432. gname = pctlops->get_group_name(pctldev,
  433. setting->data.mux.group);
  434. dev_warn(pctldev->dev,
  435. "could not get pins for group %s\n",
  436. gname);
  437. num_pins = 0;
  438. }
  439. /* Flag the descs that no setting is active */
  440. for (i = 0; i < num_pins; i++) {
  441. desc = pin_desc_get(pctldev, pins[i]);
  442. if (desc == NULL) {
  443. dev_warn(pctldev->dev,
  444. "could not get pin desc for pin %d\n",
  445. pins[i]);
  446. continue;
  447. }
  448. if (desc->mux_setting == &(setting->data.mux)) {
  449. desc->mux_setting = NULL;
  450. /* And release the pin */
  451. pin_free(pctldev, pins[i], NULL);
  452. } else {
  453. const char *gname;
  454. gname = pctlops->get_group_name(pctldev,
  455. setting->data.mux.group);
  456. dev_warn(pctldev->dev,
  457. "not freeing pin %d (%s) as part of "
  458. "deactivating group %s - it is already "
  459. "used for some other setting",
  460. pins[i], desc->name, gname);
  461. }
  462. }
  463. }
  464. #ifdef CONFIG_DEBUG_FS
  465. /* Called from pincontrol core */
  466. static int pinmux_functions_show(struct seq_file *s, void *what)
  467. {
  468. struct pinctrl_dev *pctldev = s->private;
  469. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  470. unsigned nfuncs;
  471. unsigned func_selector = 0;
  472. if (!pmxops)
  473. return 0;
  474. mutex_lock(&pctldev->mutex);
  475. nfuncs = pmxops->get_functions_count(pctldev);
  476. while (func_selector < nfuncs) {
  477. const char *func = pmxops->get_function_name(pctldev,
  478. func_selector);
  479. const char * const *groups;
  480. unsigned num_groups;
  481. int ret;
  482. int i;
  483. ret = pmxops->get_function_groups(pctldev, func_selector,
  484. &groups, &num_groups);
  485. if (ret) {
  486. seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
  487. func);
  488. func_selector++;
  489. continue;
  490. }
  491. seq_printf(s, "function: %s, groups = [ ", func);
  492. for (i = 0; i < num_groups; i++)
  493. seq_printf(s, "%s ", groups[i]);
  494. seq_puts(s, "]\n");
  495. func_selector++;
  496. }
  497. mutex_unlock(&pctldev->mutex);
  498. return 0;
  499. }
  500. static int pinmux_pins_show(struct seq_file *s, void *what)
  501. {
  502. struct pinctrl_dev *pctldev = s->private;
  503. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  504. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  505. unsigned i, pin;
  506. if (!pmxops)
  507. return 0;
  508. seq_puts(s, "Pinmux settings per pin\n");
  509. if (pmxops->strict)
  510. seq_puts(s,
  511. "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
  512. else
  513. seq_puts(s,
  514. "Format: pin (name): mux_owner gpio_owner hog?\n");
  515. mutex_lock(&pctldev->mutex);
  516. /* The pin number can be retrived from the pin controller descriptor */
  517. for (i = 0; i < pctldev->desc->npins; i++) {
  518. struct pin_desc *desc;
  519. bool is_hog = false;
  520. pin = pctldev->desc->pins[i].number;
  521. desc = pin_desc_get(pctldev, pin);
  522. /* Skip if we cannot search the pin */
  523. if (desc == NULL)
  524. continue;
  525. if (desc->mux_owner &&
  526. !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
  527. is_hog = true;
  528. if (pmxops->strict) {
  529. if (desc->mux_owner)
  530. seq_printf(s, "pin %d (%s): device %s%s",
  531. pin, desc->name, desc->mux_owner,
  532. is_hog ? " (HOG)" : "");
  533. else if (desc->gpio_owner)
  534. seq_printf(s, "pin %d (%s): GPIO %s",
  535. pin, desc->name, desc->gpio_owner);
  536. else
  537. seq_printf(s, "pin %d (%s): UNCLAIMED",
  538. pin, desc->name);
  539. } else {
  540. /* For non-strict controllers */
  541. seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
  542. desc->mux_owner ? desc->mux_owner
  543. : "(MUX UNCLAIMED)",
  544. desc->gpio_owner ? desc->gpio_owner
  545. : "(GPIO UNCLAIMED)",
  546. is_hog ? " (HOG)" : "");
  547. }
  548. /* If mux: print function+group claiming the pin */
  549. if (desc->mux_setting)
  550. seq_printf(s, " function %s group %s\n",
  551. pmxops->get_function_name(pctldev,
  552. desc->mux_setting->func),
  553. pctlops->get_group_name(pctldev,
  554. desc->mux_setting->group));
  555. else
  556. seq_printf(s, "\n");
  557. }
  558. mutex_unlock(&pctldev->mutex);
  559. return 0;
  560. }
  561. void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map)
  562. {
  563. seq_printf(s, "group %s\nfunction %s\n",
  564. map->data.mux.group ? map->data.mux.group : "(default)",
  565. map->data.mux.function);
  566. }
  567. void pinmux_show_setting(struct seq_file *s,
  568. struct pinctrl_setting const *setting)
  569. {
  570. struct pinctrl_dev *pctldev = setting->pctldev;
  571. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  572. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  573. seq_printf(s, "group: %s (%u) function: %s (%u)\n",
  574. pctlops->get_group_name(pctldev, setting->data.mux.group),
  575. setting->data.mux.group,
  576. pmxops->get_function_name(pctldev, setting->data.mux.func),
  577. setting->data.mux.func);
  578. }
  579. static int pinmux_functions_open(struct inode *inode, struct file *file)
  580. {
  581. return single_open(file, pinmux_functions_show, inode->i_private);
  582. }
  583. static int pinmux_pins_open(struct inode *inode, struct file *file)
  584. {
  585. return single_open(file, pinmux_pins_show, inode->i_private);
  586. }
  587. static const struct file_operations pinmux_functions_ops = {
  588. .open = pinmux_functions_open,
  589. .read = seq_read,
  590. .llseek = seq_lseek,
  591. .release = single_release,
  592. };
  593. static const struct file_operations pinmux_pins_ops = {
  594. .open = pinmux_pins_open,
  595. .read = seq_read,
  596. .llseek = seq_lseek,
  597. .release = single_release,
  598. };
  599. void pinmux_init_device_debugfs(struct dentry *devroot,
  600. struct pinctrl_dev *pctldev)
  601. {
  602. debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
  603. devroot, pctldev, &pinmux_functions_ops);
  604. debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
  605. devroot, pctldev, &pinmux_pins_ops);
  606. }
  607. #endif /* CONFIG_DEBUG_FS */
  608. #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
  609. /**
  610. * pinmux_generic_get_function_count() - returns number of functions
  611. * @pctldev: pin controller device
  612. */
  613. int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
  614. {
  615. return pctldev->num_functions;
  616. }
  617. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
  618. /**
  619. * pinmux_generic_get_function_name() - returns the function name
  620. * @pctldev: pin controller device
  621. * @selector: function number
  622. */
  623. const char *
  624. pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
  625. unsigned int selector)
  626. {
  627. struct function_desc *function;
  628. function = radix_tree_lookup(&pctldev->pin_function_tree,
  629. selector);
  630. if (!function)
  631. return NULL;
  632. return function->name;
  633. }
  634. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
  635. /**
  636. * pinmux_generic_get_function_groups() - gets the function groups
  637. * @pctldev: pin controller device
  638. * @selector: function number
  639. * @groups: array of pin groups
  640. * @num_groups: number of pin groups
  641. */
  642. int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
  643. unsigned int selector,
  644. const char * const **groups,
  645. unsigned * const num_groups)
  646. {
  647. struct function_desc *function;
  648. function = radix_tree_lookup(&pctldev->pin_function_tree,
  649. selector);
  650. if (!function) {
  651. dev_err(pctldev->dev, "%s could not find function%i\n",
  652. __func__, selector);
  653. return -EINVAL;
  654. }
  655. *groups = function->group_names;
  656. *num_groups = function->num_group_names;
  657. return 0;
  658. }
  659. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
  660. /**
  661. * pinmux_generic_get_function() - returns a function based on the number
  662. * @pctldev: pin controller device
  663. * @group_selector: function number
  664. */
  665. struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
  666. unsigned int selector)
  667. {
  668. struct function_desc *function;
  669. function = radix_tree_lookup(&pctldev->pin_function_tree,
  670. selector);
  671. if (!function)
  672. return NULL;
  673. return function;
  674. }
  675. EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
  676. /**
  677. * pinmux_generic_get_function_groups() - gets the function groups
  678. * @pctldev: pin controller device
  679. * @name: name of the function
  680. * @groups: array of pin groups
  681. * @num_groups: number of pin groups
  682. * @data: pin controller driver specific data
  683. */
  684. int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
  685. const char *name,
  686. const char **groups,
  687. const unsigned int num_groups,
  688. void *data)
  689. {
  690. struct function_desc *function;
  691. function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
  692. if (!function)
  693. return -ENOMEM;
  694. function->name = name;
  695. function->group_names = groups;
  696. function->num_group_names = num_groups;
  697. function->data = data;
  698. radix_tree_insert(&pctldev->pin_function_tree, pctldev->num_functions,
  699. function);
  700. pctldev->num_functions++;
  701. return 0;
  702. }
  703. EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
  704. /**
  705. * pinmux_generic_remove_function() - removes a numbered function
  706. * @pctldev: pin controller device
  707. * @selector: function number
  708. *
  709. * Note that the caller must take care of locking.
  710. */
  711. int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
  712. unsigned int selector)
  713. {
  714. struct function_desc *function;
  715. function = radix_tree_lookup(&pctldev->pin_function_tree,
  716. selector);
  717. if (!function)
  718. return -ENOENT;
  719. radix_tree_delete(&pctldev->pin_function_tree, selector);
  720. devm_kfree(pctldev->dev, function);
  721. pctldev->num_functions--;
  722. return 0;
  723. }
  724. EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
  725. /**
  726. * pinmux_generic_free_functions() - removes all functions
  727. * @pctldev: pin controller device
  728. *
  729. * Note that the caller must take care of locking.
  730. */
  731. void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
  732. {
  733. struct radix_tree_iter iter;
  734. struct function_desc *function;
  735. unsigned long *indices;
  736. void **slot;
  737. int i = 0;
  738. indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
  739. pctldev->num_functions, GFP_KERNEL);
  740. if (!indices)
  741. return;
  742. radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
  743. indices[i++] = iter.index;
  744. for (i = 0; i < pctldev->num_functions; i++) {
  745. function = radix_tree_lookup(&pctldev->pin_function_tree,
  746. indices[i]);
  747. radix_tree_delete(&pctldev->pin_function_tree, indices[i]);
  748. devm_kfree(pctldev->dev, function);
  749. }
  750. pctldev->num_functions = 0;
  751. }
  752. #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */