pinmux.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. if (gpio_range) {
  213. owner = desc->gpio_owner;
  214. desc->gpio_owner = NULL;
  215. } else {
  216. owner = desc->mux_owner;
  217. desc->mux_owner = NULL;
  218. desc->mux_setting = NULL;
  219. }
  220. }
  221. /*
  222. * If there is no kind of request function for the pin we just assume
  223. * we got it by default and proceed.
  224. */
  225. if (gpio_range && ops->gpio_disable_free)
  226. ops->gpio_disable_free(pctldev, gpio_range, pin);
  227. else if (ops->free)
  228. ops->free(pctldev, pin);
  229. module_put(pctldev->owner);
  230. return owner;
  231. }
  232. /**
  233. * pinmux_request_gpio() - request pinmuxing for a GPIO pin
  234. * @pctldev: pin controller device affected
  235. * @pin: the pin to mux in for GPIO
  236. * @range: the applicable GPIO range
  237. * @gpio: number of requested GPIO
  238. */
  239. int pinmux_request_gpio(struct pinctrl_dev *pctldev,
  240. struct pinctrl_gpio_range *range,
  241. unsigned int pin, unsigned int gpio)
  242. {
  243. const char *owner;
  244. int ret;
  245. /* Conjure some name stating what chip and pin this is taken by */
  246. owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
  247. if (!owner)
  248. return -ENOMEM;
  249. ret = pin_request(pctldev, pin, owner, range);
  250. if (ret < 0)
  251. kfree(owner);
  252. return ret;
  253. }
  254. /**
  255. * pinmux_free_gpio() - release a pin from GPIO muxing
  256. * @pctldev: the pin controller device for the pin
  257. * @pin: the affected currently GPIO-muxed in pin
  258. * @range: applicable GPIO range
  259. */
  260. void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned int pin,
  261. struct pinctrl_gpio_range *range)
  262. {
  263. const char *owner;
  264. owner = pin_free(pctldev, pin, range);
  265. kfree(owner);
  266. }
  267. /**
  268. * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
  269. * @pctldev: the pin controller handling this pin
  270. * @range: applicable GPIO range
  271. * @pin: the affected GPIO pin in this controller
  272. * @input: true if we set the pin as input, false for output
  273. */
  274. int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
  275. struct pinctrl_gpio_range *range,
  276. unsigned int pin, bool input)
  277. {
  278. const struct pinmux_ops *ops;
  279. int ret;
  280. ops = pctldev->desc->pmxops;
  281. if (ops->gpio_set_direction)
  282. ret = ops->gpio_set_direction(pctldev, range, pin, input);
  283. else
  284. ret = 0;
  285. return ret;
  286. }
  287. static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
  288. const char *function)
  289. {
  290. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  291. unsigned int nfuncs = ops->get_functions_count(pctldev);
  292. unsigned int selector = 0;
  293. /* See if this pctldev has this function */
  294. while (selector < nfuncs) {
  295. const char *fname = ops->get_function_name(pctldev, selector);
  296. if (fname && !strcmp(function, fname))
  297. return selector;
  298. selector++;
  299. }
  300. return -EINVAL;
  301. }
  302. int pinmux_map_to_setting(const struct pinctrl_map *map,
  303. struct pinctrl_setting *setting)
  304. {
  305. struct pinctrl_dev *pctldev = setting->pctldev;
  306. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  307. char const * const *groups;
  308. unsigned int num_groups;
  309. int ret;
  310. const char *group;
  311. if (!pmxops) {
  312. dev_err(pctldev->dev, "does not support mux function\n");
  313. return -EINVAL;
  314. }
  315. ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
  316. if (ret < 0) {
  317. dev_err(pctldev->dev, "invalid function %s in map table\n",
  318. map->data.mux.function);
  319. return ret;
  320. }
  321. setting->data.mux.func = ret;
  322. ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
  323. &groups, &num_groups);
  324. if (ret < 0) {
  325. dev_err(pctldev->dev, "can't query groups for function %s\n",
  326. map->data.mux.function);
  327. return ret;
  328. }
  329. if (!num_groups) {
  330. dev_err(pctldev->dev,
  331. "function %s can't be selected on any group\n",
  332. map->data.mux.function);
  333. return -EINVAL;
  334. }
  335. if (map->data.mux.group) {
  336. group = map->data.mux.group;
  337. ret = match_string(groups, num_groups, group);
  338. if (ret < 0) {
  339. dev_err(pctldev->dev,
  340. "invalid group \"%s\" for function \"%s\"\n",
  341. group, map->data.mux.function);
  342. return ret;
  343. }
  344. } else {
  345. group = groups[0];
  346. }
  347. ret = pinctrl_get_group_selector(pctldev, group);
  348. if (ret < 0) {
  349. dev_err(pctldev->dev, "invalid group %s in map table\n",
  350. map->data.mux.group);
  351. return ret;
  352. }
  353. setting->data.mux.group = ret;
  354. return 0;
  355. }
  356. void pinmux_free_setting(const struct pinctrl_setting *setting)
  357. {
  358. /* This function is currently unused */
  359. }
  360. int pinmux_enable_setting(const struct pinctrl_setting *setting)
  361. {
  362. struct pinctrl_dev *pctldev = setting->pctldev;
  363. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  364. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  365. int ret = 0;
  366. const unsigned int *pins = NULL;
  367. unsigned int num_pins = 0;
  368. int i;
  369. struct pin_desc *desc;
  370. if (pctlops->get_group_pins)
  371. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  372. &pins, &num_pins);
  373. if (ret) {
  374. const char *gname;
  375. /* errors only affect debug data, so just warn */
  376. gname = pctlops->get_group_name(pctldev,
  377. setting->data.mux.group);
  378. dev_warn(pctldev->dev,
  379. "could not get pins for group %s\n",
  380. gname);
  381. num_pins = 0;
  382. }
  383. /* Try to allocate all pins in this group, one by one */
  384. for (i = 0; i < num_pins; i++) {
  385. ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
  386. if (ret) {
  387. const char *gname;
  388. const char *pname;
  389. desc = pin_desc_get(pctldev, pins[i]);
  390. pname = desc ? desc->name : "non-existing";
  391. gname = pctlops->get_group_name(pctldev,
  392. setting->data.mux.group);
  393. dev_err_probe(pctldev->dev, ret,
  394. "could not request pin %d (%s) from group %s on device %s\n",
  395. pins[i], pname, gname,
  396. pinctrl_dev_get_name(pctldev));
  397. goto err_pin_request;
  398. }
  399. }
  400. /* Now that we have acquired the pins, encode the mux setting */
  401. for (i = 0; i < num_pins; i++) {
  402. desc = pin_desc_get(pctldev, pins[i]);
  403. if (desc == NULL) {
  404. dev_warn(pctldev->dev,
  405. "could not get pin desc for pin %d\n",
  406. pins[i]);
  407. continue;
  408. }
  409. scoped_guard(mutex, &desc->mux_lock)
  410. desc->mux_setting = &(setting->data.mux);
  411. }
  412. ret = ops->set_mux(pctldev, setting->data.mux.func,
  413. setting->data.mux.group);
  414. if (ret)
  415. goto err_set_mux;
  416. return 0;
  417. err_set_mux:
  418. for (i = 0; i < num_pins; i++) {
  419. desc = pin_desc_get(pctldev, pins[i]);
  420. if (desc) {
  421. scoped_guard(mutex, &desc->mux_lock)
  422. desc->mux_setting = NULL;
  423. }
  424. }
  425. err_pin_request:
  426. /* On error release all taken pins */
  427. while (--i >= 0)
  428. pin_free(pctldev, pins[i], NULL);
  429. return ret;
  430. }
  431. void pinmux_disable_setting(const struct pinctrl_setting *setting)
  432. {
  433. struct pinctrl_dev *pctldev = setting->pctldev;
  434. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  435. int ret = 0;
  436. const unsigned int *pins = NULL;
  437. unsigned int num_pins = 0;
  438. int i;
  439. struct pin_desc *desc;
  440. bool is_equal;
  441. if (pctlops->get_group_pins)
  442. ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
  443. &pins, &num_pins);
  444. if (ret) {
  445. const char *gname;
  446. /* errors only affect debug data, so just warn */
  447. gname = pctlops->get_group_name(pctldev,
  448. setting->data.mux.group);
  449. dev_warn(pctldev->dev,
  450. "could not get pins for group %s\n",
  451. gname);
  452. num_pins = 0;
  453. }
  454. /* Flag the descs that no setting is active */
  455. for (i = 0; i < num_pins; i++) {
  456. desc = pin_desc_get(pctldev, pins[i]);
  457. if (desc == NULL) {
  458. dev_warn(pctldev->dev,
  459. "could not get pin desc for pin %d\n",
  460. pins[i]);
  461. continue;
  462. }
  463. scoped_guard(mutex, &desc->mux_lock)
  464. is_equal = (desc->mux_setting == &(setting->data.mux));
  465. if (is_equal) {
  466. pin_free(pctldev, pins[i], NULL);
  467. } else {
  468. const char *gname;
  469. gname = pctlops->get_group_name(pctldev,
  470. setting->data.mux.group);
  471. dev_warn(pctldev->dev,
  472. "not freeing pin %d (%s) as part of deactivating group %s - it is already used for some other setting",
  473. pins[i], desc->name, gname);
  474. }
  475. }
  476. }
  477. #ifdef CONFIG_DEBUG_FS
  478. /* Called from pincontrol core */
  479. static int pinmux_functions_show(struct seq_file *s, void *what)
  480. {
  481. struct pinctrl_dev *pctldev = s->private;
  482. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  483. unsigned int nfuncs;
  484. unsigned int func_selector = 0;
  485. if (!pmxops)
  486. return 0;
  487. mutex_lock(&pctldev->mutex);
  488. nfuncs = pmxops->get_functions_count(pctldev);
  489. while (func_selector < nfuncs) {
  490. const char *func = pmxops->get_function_name(pctldev,
  491. func_selector);
  492. const char * const *groups;
  493. unsigned int num_groups;
  494. int ret;
  495. int i;
  496. ret = pmxops->get_function_groups(pctldev, func_selector,
  497. &groups, &num_groups);
  498. if (ret) {
  499. seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
  500. func);
  501. func_selector++;
  502. continue;
  503. }
  504. seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
  505. for (i = 0; i < num_groups; i++)
  506. seq_printf(s, "%s ", groups[i]);
  507. seq_puts(s, "]\n");
  508. func_selector++;
  509. }
  510. mutex_unlock(&pctldev->mutex);
  511. return 0;
  512. }
  513. DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
  514. static int pinmux_pins_show(struct seq_file *s, void *what)
  515. {
  516. struct pinctrl_dev *pctldev = s->private;
  517. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  518. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  519. unsigned int i, pin;
  520. if (!pmxops)
  521. return 0;
  522. seq_puts(s, "Pinmux settings per pin\n");
  523. if (pmxops->strict)
  524. seq_puts(s,
  525. "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
  526. else
  527. seq_puts(s,
  528. "Format: pin (name): mux_owner gpio_owner hog?\n");
  529. mutex_lock(&pctldev->mutex);
  530. /* The pin number can be retrived from the pin controller descriptor */
  531. for (i = 0; i < pctldev->desc->npins; i++) {
  532. struct pin_desc *desc;
  533. bool is_hog = false;
  534. pin = pctldev->desc->pins[i].number;
  535. desc = pin_desc_get(pctldev, pin);
  536. /* Skip if we cannot search the pin */
  537. if (desc == NULL)
  538. continue;
  539. scoped_guard(mutex, &desc->mux_lock) {
  540. if (desc->mux_owner &&
  541. !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
  542. is_hog = true;
  543. if (pmxops->strict) {
  544. if (desc->mux_owner)
  545. seq_printf(s, "pin %d (%s): device %s%s",
  546. pin, desc->name, desc->mux_owner,
  547. is_hog ? " (HOG)" : "");
  548. else if (desc->gpio_owner)
  549. seq_printf(s, "pin %d (%s): GPIO %s",
  550. pin, desc->name, desc->gpio_owner);
  551. else
  552. seq_printf(s, "pin %d (%s): UNCLAIMED",
  553. pin, desc->name);
  554. } else {
  555. /* For non-strict controllers */
  556. seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
  557. desc->mux_owner ? desc->mux_owner
  558. : "(MUX UNCLAIMED)",
  559. desc->gpio_owner ? desc->gpio_owner
  560. : "(GPIO UNCLAIMED)",
  561. is_hog ? " (HOG)" : "");
  562. }
  563. /* If mux: print function+group claiming the pin */
  564. if (desc->mux_setting)
  565. seq_printf(s, " function %s group %s\n",
  566. pmxops->get_function_name(pctldev,
  567. desc->mux_setting->func),
  568. pctlops->get_group_name(pctldev,
  569. desc->mux_setting->group));
  570. else
  571. seq_putc(s, '\n');
  572. }
  573. }
  574. mutex_unlock(&pctldev->mutex);
  575. return 0;
  576. }
  577. DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
  578. void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
  579. {
  580. seq_printf(s, "group %s\nfunction %s\n",
  581. map->data.mux.group ? map->data.mux.group : "(default)",
  582. map->data.mux.function);
  583. }
  584. void pinmux_show_setting(struct seq_file *s,
  585. const struct pinctrl_setting *setting)
  586. {
  587. struct pinctrl_dev *pctldev = setting->pctldev;
  588. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  589. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  590. seq_printf(s, "group: %s (%u) function: %s (%u)\n",
  591. pctlops->get_group_name(pctldev, setting->data.mux.group),
  592. setting->data.mux.group,
  593. pmxops->get_function_name(pctldev, setting->data.mux.func),
  594. setting->data.mux.func);
  595. }
  596. static int pinmux_select_show(struct seq_file *s, void *unused)
  597. {
  598. return -EPERM;
  599. }
  600. static ssize_t pinmux_select_write(struct file *file, const char __user *user_buf,
  601. size_t len, loff_t *ppos)
  602. {
  603. struct seq_file *sfile = file->private_data;
  604. struct pinctrl_dev *pctldev = sfile->private;
  605. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  606. const char *const *groups;
  607. char *buf, *gname, *fname;
  608. unsigned int num_groups;
  609. int fsel, gsel, ret;
  610. buf = memdup_user_nul(user_buf, len);
  611. if (IS_ERR(buf))
  612. return PTR_ERR(buf);
  613. /* remove leading and trailing spaces of input buffer */
  614. gname = strstrip(buf);
  615. if (*gname == '\0') {
  616. ret = -EINVAL;
  617. goto exit_free_buf;
  618. }
  619. /* find a separator which is a spacelike character */
  620. for (fname = gname; !isspace(*fname); fname++) {
  621. if (*fname == '\0') {
  622. ret = -EINVAL;
  623. goto exit_free_buf;
  624. }
  625. }
  626. *fname = '\0';
  627. /* drop extra spaces between function and group names */
  628. fname = skip_spaces(fname + 1);
  629. if (*fname == '\0') {
  630. ret = -EINVAL;
  631. goto exit_free_buf;
  632. }
  633. ret = pinmux_func_name_to_selector(pctldev, fname);
  634. if (ret < 0) {
  635. dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
  636. goto exit_free_buf;
  637. }
  638. fsel = ret;
  639. ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
  640. if (ret) {
  641. dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
  642. goto exit_free_buf;
  643. }
  644. ret = match_string(groups, num_groups, gname);
  645. if (ret < 0) {
  646. dev_err(pctldev->dev, "invalid group %s", gname);
  647. goto exit_free_buf;
  648. }
  649. ret = pinctrl_get_group_selector(pctldev, gname);
  650. if (ret < 0)
  651. goto exit_free_buf;
  652. gsel = ret;
  653. ret = pmxops->set_mux(pctldev, fsel, gsel);
  654. if (ret) {
  655. dev_err(pctldev->dev, "set_mux() failed: %d", ret);
  656. goto exit_free_buf;
  657. }
  658. ret = len;
  659. exit_free_buf:
  660. kfree(buf);
  661. return ret;
  662. }
  663. DEFINE_SHOW_STORE_ATTRIBUTE(pinmux_select);
  664. void pinmux_init_device_debugfs(struct dentry *devroot,
  665. struct pinctrl_dev *pctldev)
  666. {
  667. debugfs_create_file("pinmux-functions", 0444,
  668. devroot, pctldev, &pinmux_functions_fops);
  669. debugfs_create_file("pinmux-pins", 0444,
  670. devroot, pctldev, &pinmux_pins_fops);
  671. debugfs_create_file("pinmux-select", 0200,
  672. devroot, pctldev, &pinmux_select_fops);
  673. }
  674. #endif /* CONFIG_DEBUG_FS */
  675. #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
  676. /**
  677. * pinmux_generic_get_function_count() - returns number of functions
  678. * @pctldev: pin controller device
  679. */
  680. int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
  681. {
  682. return pctldev->num_functions;
  683. }
  684. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
  685. /**
  686. * pinmux_generic_get_function_name() - returns the function name
  687. * @pctldev: pin controller device
  688. * @selector: function number
  689. */
  690. const char *
  691. pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
  692. unsigned int selector)
  693. {
  694. struct function_desc *function;
  695. function = radix_tree_lookup(&pctldev->pin_function_tree,
  696. selector);
  697. if (!function)
  698. return NULL;
  699. return function->func.name;
  700. }
  701. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
  702. /**
  703. * pinmux_generic_get_function_groups() - gets the function groups
  704. * @pctldev: pin controller device
  705. * @selector: function number
  706. * @groups: array of pin groups
  707. * @ngroups: number of pin groups
  708. */
  709. int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
  710. unsigned int selector,
  711. const char * const **groups,
  712. unsigned int * const ngroups)
  713. {
  714. struct function_desc *function;
  715. function = radix_tree_lookup(&pctldev->pin_function_tree,
  716. selector);
  717. if (!function) {
  718. dev_err(pctldev->dev, "%s could not find function%i\n",
  719. __func__, selector);
  720. return -EINVAL;
  721. }
  722. *groups = function->func.groups;
  723. *ngroups = function->func.ngroups;
  724. return 0;
  725. }
  726. EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
  727. /**
  728. * pinmux_generic_get_function() - returns a function based on the number
  729. * @pctldev: pin controller device
  730. * @selector: function number
  731. */
  732. struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
  733. unsigned int selector)
  734. {
  735. struct function_desc *function;
  736. function = radix_tree_lookup(&pctldev->pin_function_tree,
  737. selector);
  738. if (!function)
  739. return NULL;
  740. return function;
  741. }
  742. EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
  743. /**
  744. * pinmux_generic_add_function() - adds a function group
  745. * @pctldev: pin controller device
  746. * @name: name of the function
  747. * @groups: array of pin groups
  748. * @ngroups: number of pin groups
  749. * @data: pin controller driver specific data
  750. */
  751. int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
  752. const char *name,
  753. const char * const *groups,
  754. const unsigned int ngroups,
  755. void *data)
  756. {
  757. struct function_desc *function;
  758. int selector, error;
  759. if (!name)
  760. return -EINVAL;
  761. selector = pinmux_func_name_to_selector(pctldev, name);
  762. if (selector >= 0)
  763. return selector;
  764. selector = pctldev->num_functions;
  765. function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
  766. if (!function)
  767. return -ENOMEM;
  768. *function = PINCTRL_FUNCTION_DESC(name, groups, ngroups, data);
  769. error = radix_tree_insert(&pctldev->pin_function_tree, selector, function);
  770. if (error)
  771. return error;
  772. pctldev->num_functions++;
  773. return selector;
  774. }
  775. EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
  776. /**
  777. * pinmux_generic_remove_function() - removes a numbered function
  778. * @pctldev: pin controller device
  779. * @selector: function number
  780. *
  781. * Note that the caller must take care of locking.
  782. */
  783. int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
  784. unsigned int selector)
  785. {
  786. struct function_desc *function;
  787. function = radix_tree_lookup(&pctldev->pin_function_tree,
  788. selector);
  789. if (!function)
  790. return -ENOENT;
  791. radix_tree_delete(&pctldev->pin_function_tree, selector);
  792. devm_kfree(pctldev->dev, function);
  793. pctldev->num_functions--;
  794. return 0;
  795. }
  796. EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
  797. /**
  798. * pinmux_generic_free_functions() - removes all functions
  799. * @pctldev: pin controller device
  800. *
  801. * Note that the caller must take care of locking. The pinctrl
  802. * functions are allocated with devm_kzalloc() so no need to free
  803. * them here.
  804. */
  805. void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
  806. {
  807. struct radix_tree_iter iter;
  808. void __rcu **slot;
  809. radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
  810. radix_tree_delete(&pctldev->pin_function_tree, iter.index);
  811. pctldev->num_functions = 0;
  812. }
  813. #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */