pinmux.c 26 KB

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