pinmux.c 24 KB

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