pinmux.c 24 KB

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