pinmux.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * Core driver for the pin muxing portions of the pin control subsystem
  3. *
  4. * Copyright (C) 2011 ST-Ericsson SA
  5. * Written on behalf of Linaro for ST-Ericsson
  6. * Based on bits of regulator core, gpio core and clk core
  7. *
  8. * Author: Linus Walleij <linus.walleij@linaro.org>
  9. *
  10. * License terms: GNU General Public License (GPL) version 2
  11. */
  12. #define pr_fmt(fmt) "pinmux core: " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/slab.h>
  18. #include <linux/radix-tree.h>
  19. #include <linux/err.h>
  20. #include <linux/list.h>
  21. #include <linux/mutex.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/pinctrl/machine.h>
  27. #include <linux/pinctrl/pinmux.h>
  28. #include "core.h"
  29. /* List of pinmuxes */
  30. static DEFINE_MUTEX(pinmux_list_mutex);
  31. static LIST_HEAD(pinmux_list);
  32. /* Global pinmux maps, we allow one set only */
  33. static struct pinmux_map const *pinmux_maps;
  34. static unsigned pinmux_maps_num;
  35. /**
  36. * struct pinmux_group - group list item for pinmux groups
  37. * @node: pinmux group list node
  38. * @group_selector: the group selector for this group
  39. */
  40. struct pinmux_group {
  41. struct list_head node;
  42. unsigned group_selector;
  43. };
  44. /**
  45. * struct pinmux - per-device pinmux state holder
  46. * @node: global list node
  47. * @dev: the device using this pinmux
  48. * @usecount: the number of active users of this mux setting, used to keep
  49. * track of nested use cases
  50. * @pins: an array of discrete physical pins used in this mapping, taken
  51. * from the global pin enumeration space (copied from pinmux map)
  52. * @num_pins: the number of pins in this mapping array, i.e. the number of
  53. * elements in .pins so we can iterate over that array (copied from
  54. * pinmux map)
  55. * @pctldev: pin control device handling this pinmux
  56. * @func_selector: the function selector for the pinmux device handling
  57. * this pinmux
  58. * @groups: the group selectors for the pinmux device and
  59. * selector combination handling this pinmux, this is a list that
  60. * will be traversed on all pinmux operations such as
  61. * get/put/enable/disable
  62. * @mutex: a lock for the pinmux state holder
  63. */
  64. struct pinmux {
  65. struct list_head node;
  66. struct device *dev;
  67. unsigned usecount;
  68. struct pinctrl_dev *pctldev;
  69. unsigned func_selector;
  70. struct list_head groups;
  71. struct mutex mutex;
  72. };
  73. /**
  74. * struct pinmux_hog - a list item to stash mux hogs
  75. * @node: pinmux hog list node
  76. * @map: map entry responsible for this hogging
  77. * @pmx: the pinmux hogged by this item
  78. */
  79. struct pinmux_hog {
  80. struct list_head node;
  81. struct pinmux_map const *map;
  82. struct pinmux *pmx;
  83. };
  84. /**
  85. * pin_request() - request a single pin to be muxed in, typically for GPIO
  86. * @pin: the pin number in the global pin space
  87. * @function: a functional name to give to this pin, passed to the driver
  88. * so it knows what function to mux in, e.g. the string "gpioNN"
  89. * means that you want to mux in the pin for use as GPIO number NN
  90. * @gpio: if this request concerns a single GPIO pin
  91. * @gpio_range: the range matching the GPIO pin if this is a request for a
  92. * single GPIO pin
  93. */
  94. static int pin_request(struct pinctrl_dev *pctldev,
  95. int pin, const char *function, bool gpio,
  96. struct pinctrl_gpio_range *gpio_range)
  97. {
  98. struct pin_desc *desc;
  99. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  100. int status = -EINVAL;
  101. dev_dbg(&pctldev->dev, "request pin %d for %s\n", pin, function);
  102. if (!pin_is_valid(pctldev, pin)) {
  103. dev_err(&pctldev->dev, "pin is invalid\n");
  104. return -EINVAL;
  105. }
  106. if (!function) {
  107. dev_err(&pctldev->dev, "no function name given\n");
  108. return -EINVAL;
  109. }
  110. desc = pin_desc_get(pctldev, pin);
  111. if (desc == NULL) {
  112. dev_err(&pctldev->dev,
  113. "pin is not registered so it cannot be requested\n");
  114. goto out;
  115. }
  116. spin_lock(&desc->lock);
  117. if (desc->mux_function) {
  118. spin_unlock(&desc->lock);
  119. dev_err(&pctldev->dev,
  120. "pin already requested\n");
  121. goto out;
  122. }
  123. desc->mux_function = function;
  124. spin_unlock(&desc->lock);
  125. /* Let each pin increase references to this module */
  126. if (!try_module_get(pctldev->owner)) {
  127. dev_err(&pctldev->dev,
  128. "could not increase module refcount for pin %d\n",
  129. pin);
  130. status = -EINVAL;
  131. goto out_free_pin;
  132. }
  133. /*
  134. * If there is no kind of request function for the pin we just assume
  135. * we got it by default and proceed.
  136. */
  137. if (gpio && ops->gpio_request_enable)
  138. /* This requests and enables a single GPIO pin */
  139. status = ops->gpio_request_enable(pctldev, gpio_range, pin);
  140. else if (ops->request)
  141. status = ops->request(pctldev, pin);
  142. else
  143. status = 0;
  144. if (status)
  145. dev_err(&pctldev->dev, "->request on device %s failed "
  146. "for pin %d\n",
  147. pctldev->desc->name, pin);
  148. out_free_pin:
  149. if (status) {
  150. spin_lock(&desc->lock);
  151. desc->mux_function = NULL;
  152. spin_unlock(&desc->lock);
  153. }
  154. out:
  155. if (status)
  156. dev_err(&pctldev->dev, "pin-%d (%s) status %d\n",
  157. pin, function ? : "?", status);
  158. return status;
  159. }
  160. /**
  161. * pin_free() - release a single muxed in pin so something else can be muxed
  162. * @pctldev: pin controller device handling this pin
  163. * @pin: the pin to free
  164. * @free_func: whether to free the pin's assigned function name string
  165. */
  166. static void pin_free(struct pinctrl_dev *pctldev, int pin, int free_func)
  167. {
  168. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  169. struct pin_desc *desc;
  170. desc = pin_desc_get(pctldev, pin);
  171. if (desc == NULL) {
  172. dev_err(&pctldev->dev,
  173. "pin is not registered so it cannot be freed\n");
  174. return;
  175. }
  176. if (ops->free)
  177. ops->free(pctldev, pin);
  178. spin_lock(&desc->lock);
  179. if (free_func)
  180. kfree(desc->mux_function);
  181. desc->mux_function = NULL;
  182. spin_unlock(&desc->lock);
  183. module_put(pctldev->owner);
  184. }
  185. /**
  186. * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
  187. * @gpio: the GPIO pin number from the GPIO subsystem number space
  188. */
  189. int pinmux_request_gpio(unsigned gpio)
  190. {
  191. char gpiostr[16];
  192. const char *function;
  193. struct pinctrl_dev *pctldev;
  194. struct pinctrl_gpio_range *range;
  195. int ret;
  196. int pin;
  197. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  198. if (ret)
  199. return -EINVAL;
  200. /* Convert to the pin controllers number space */
  201. pin = gpio - range->base;
  202. /* Conjure some name stating what chip and pin this is taken by */
  203. snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
  204. function = kstrdup(gpiostr, GFP_KERNEL);
  205. if (!function)
  206. return -EINVAL;
  207. ret = pin_request(pctldev, pin, function, true, range);
  208. if (ret < 0)
  209. kfree(function);
  210. return ret;
  211. }
  212. EXPORT_SYMBOL_GPL(pinmux_request_gpio);
  213. /**
  214. * pinmux_free_gpio() - free a single pin, currently used as GPIO
  215. * @gpio: the GPIO pin number from the GPIO subsystem number space
  216. */
  217. void pinmux_free_gpio(unsigned gpio)
  218. {
  219. struct pinctrl_dev *pctldev;
  220. struct pinctrl_gpio_range *range;
  221. int ret;
  222. int pin;
  223. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  224. if (ret)
  225. return;
  226. /* Convert to the pin controllers number space */
  227. pin = gpio - range->base;
  228. pin_free(pctldev, pin, true);
  229. }
  230. EXPORT_SYMBOL_GPL(pinmux_free_gpio);
  231. /**
  232. * pinmux_register_mappings() - register a set of pinmux mappings
  233. * @maps: the pinmux mappings table to register
  234. * @num_maps: the number of maps in the mapping table
  235. *
  236. * Only call this once during initialization of your machine, the function is
  237. * tagged as __init and won't be callable after init has completed. The map
  238. * passed into this function will be owned by the pinmux core and cannot be
  239. * free:d.
  240. */
  241. int __init pinmux_register_mappings(struct pinmux_map const *maps,
  242. unsigned num_maps)
  243. {
  244. int i;
  245. if (pinmux_maps != NULL) {
  246. pr_err("pinmux mappings already registered, you can only "
  247. "register one set of maps\n");
  248. return -EINVAL;
  249. }
  250. pr_debug("add %d pinmux maps\n", num_maps);
  251. for (i = 0; i < num_maps; i++) {
  252. /* Sanity check the mapping */
  253. if (!maps[i].name) {
  254. pr_err("failed to register map %d: "
  255. "no map name given\n", i);
  256. return -EINVAL;
  257. }
  258. if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
  259. pr_err("failed to register map %s (%d): "
  260. "no pin control device given\n",
  261. maps[i].name, i);
  262. return -EINVAL;
  263. }
  264. if (!maps[i].function) {
  265. pr_err("failed to register map %s (%d): "
  266. "no function ID given\n", maps[i].name, i);
  267. return -EINVAL;
  268. }
  269. if (!maps[i].dev && !maps[i].dev_name)
  270. pr_debug("add system map %s function %s with no device\n",
  271. maps[i].name,
  272. maps[i].function);
  273. else
  274. pr_debug("register map %s, function %s\n",
  275. maps[i].name,
  276. maps[i].function);
  277. }
  278. pinmux_maps = maps;
  279. pinmux_maps_num = num_maps;
  280. return 0;
  281. }
  282. /**
  283. * acquire_pins() - acquire all the pins for a certain funcion on a pinmux
  284. * @pctldev: the device to take the pins on
  285. * @func_selector: the function selector to acquire the pins for
  286. * @group_selector: the group selector containing the pins to acquire
  287. */
  288. static int acquire_pins(struct pinctrl_dev *pctldev,
  289. unsigned func_selector,
  290. unsigned group_selector)
  291. {
  292. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  293. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  294. const char *func = pmxops->get_function_name(pctldev,
  295. func_selector);
  296. const unsigned *pins;
  297. unsigned num_pins;
  298. int ret;
  299. int i;
  300. ret = pctlops->get_group_pins(pctldev, group_selector,
  301. &pins, &num_pins);
  302. if (ret)
  303. return ret;
  304. dev_dbg(&pctldev->dev, "requesting the %u pins from group %u\n",
  305. num_pins, group_selector);
  306. /* Try to allocate all pins in this group, one by one */
  307. for (i = 0; i < num_pins; i++) {
  308. ret = pin_request(pctldev, pins[i], func, false, NULL);
  309. if (ret) {
  310. dev_err(&pctldev->dev,
  311. "could not get pin %d for function %s "
  312. "on device %s - conflicting mux mappings?\n",
  313. pins[i], func ? : "(undefined)",
  314. pinctrl_dev_get_name(pctldev));
  315. /* On error release all taken pins */
  316. i--; /* this pin just failed */
  317. for (; i >= 0; i--)
  318. pin_free(pctldev, pins[i], false);
  319. return -ENODEV;
  320. }
  321. }
  322. return 0;
  323. }
  324. /**
  325. * release_pins() - release pins taken by earlier acquirement
  326. * @pctldev: the device to free the pinx on
  327. * @group_selector: the group selector containing the pins to free
  328. */
  329. static void release_pins(struct pinctrl_dev *pctldev,
  330. unsigned group_selector)
  331. {
  332. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  333. const unsigned *pins;
  334. unsigned num_pins;
  335. int ret;
  336. int i;
  337. ret = pctlops->get_group_pins(pctldev, group_selector,
  338. &pins, &num_pins);
  339. if (ret) {
  340. dev_err(&pctldev->dev, "could not get pins to release for "
  341. "group selector %d\n",
  342. group_selector);
  343. return;
  344. }
  345. for (i = 0; i < num_pins; i++)
  346. pin_free(pctldev, pins[i], false);
  347. }
  348. /**
  349. * pinmux_check_pin_group() - check function and pin group combo
  350. * @pctldev: device to check the pin group vs function for
  351. * @func_selector: the function selector to check the pin group for, we have
  352. * already looked this up in the calling function
  353. * @pin_group: the pin group to match to the function
  354. *
  355. * This function will check that the pinmux driver can supply the
  356. * selected pin group for a certain function, returns the group selector if
  357. * the group and function selector will work fine together, else returns
  358. * negative
  359. */
  360. static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
  361. unsigned func_selector,
  362. const char *pin_group)
  363. {
  364. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  365. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  366. int ret;
  367. /*
  368. * If the driver does not support different pin groups for the
  369. * functions, we only support group 0, and assume this exists.
  370. */
  371. if (!pctlops || !pctlops->list_groups)
  372. return 0;
  373. /*
  374. * Passing NULL (no specific group) will select the first and
  375. * hopefully only group of pins available for this function.
  376. */
  377. if (!pin_group) {
  378. char const * const *groups;
  379. unsigned num_groups;
  380. ret = pmxops->get_function_groups(pctldev, func_selector,
  381. &groups, &num_groups);
  382. if (ret)
  383. return ret;
  384. if (num_groups < 1)
  385. return -EINVAL;
  386. ret = pinctrl_get_group_selector(pctldev, groups[0]);
  387. if (ret < 0) {
  388. dev_err(&pctldev->dev,
  389. "function %s wants group %s but the pin "
  390. "controller does not seem to have that group\n",
  391. pmxops->get_function_name(pctldev, func_selector),
  392. groups[0]);
  393. return ret;
  394. }
  395. if (num_groups > 1)
  396. dev_dbg(&pctldev->dev,
  397. "function %s support more than one group, "
  398. "default-selecting first group %s (%d)\n",
  399. pmxops->get_function_name(pctldev, func_selector),
  400. groups[0],
  401. ret);
  402. return ret;
  403. }
  404. dev_dbg(&pctldev->dev,
  405. "check if we have pin group %s on controller %s\n",
  406. pin_group, pinctrl_dev_get_name(pctldev));
  407. ret = pinctrl_get_group_selector(pctldev, pin_group);
  408. if (ret < 0) {
  409. dev_dbg(&pctldev->dev,
  410. "%s does not support pin group %s with function %s\n",
  411. pinctrl_dev_get_name(pctldev),
  412. pin_group,
  413. pmxops->get_function_name(pctldev, func_selector));
  414. }
  415. return ret;
  416. }
  417. /**
  418. * pinmux_search_function() - check pin control driver for a certain function
  419. * @pctldev: device to check for function and position
  420. * @map: function map containing the function and position to look for
  421. * @func_selector: returns the applicable function selector if found
  422. * @group_selector: returns the applicable group selector if found
  423. *
  424. * This will search the pinmux driver for an applicable
  425. * function with a specific pin group, returns 0 if these can be mapped
  426. * negative otherwise
  427. */
  428. static int pinmux_search_function(struct pinctrl_dev *pctldev,
  429. struct pinmux_map const *map,
  430. unsigned *func_selector,
  431. unsigned *group_selector)
  432. {
  433. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  434. unsigned selector = 0;
  435. /* See if this pctldev has this function */
  436. while (ops->list_functions(pctldev, selector) >= 0) {
  437. const char *fname = ops->get_function_name(pctldev,
  438. selector);
  439. int ret;
  440. if (!strcmp(map->function, fname)) {
  441. /* Found the function, check pin group */
  442. ret = pinmux_check_pin_group(pctldev, selector,
  443. map->group);
  444. if (ret < 0)
  445. return ret;
  446. /* This function and group selector can be used */
  447. *func_selector = selector;
  448. *group_selector = ret;
  449. return 0;
  450. }
  451. selector++;
  452. }
  453. pr_err("%s does not support function %s\n",
  454. pinctrl_dev_get_name(pctldev), map->function);
  455. return -EINVAL;
  456. }
  457. /**
  458. * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
  459. */
  460. static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
  461. struct pinmux *pmx,
  462. struct device *dev,
  463. const char *devname,
  464. struct pinmux_map const *map)
  465. {
  466. unsigned func_selector;
  467. unsigned group_selector;
  468. struct pinmux_group *grp;
  469. int ret;
  470. /*
  471. * Note that we're not locking the pinmux mutex here, because
  472. * this is only called at pinmux initialization time when it
  473. * has not been added to any list and thus is not reachable
  474. * by anyone else.
  475. */
  476. if (pmx->pctldev && pmx->pctldev != pctldev) {
  477. dev_err(&pctldev->dev,
  478. "different pin control devices given for device %s, "
  479. "function %s\n",
  480. devname,
  481. map->function);
  482. return -EINVAL;
  483. }
  484. pmx->dev = dev;
  485. pmx->pctldev = pctldev;
  486. /* Now go into the driver and try to match a function and group */
  487. ret = pinmux_search_function(pctldev, map, &func_selector,
  488. &group_selector);
  489. if (ret < 0)
  490. return ret;
  491. /*
  492. * If the function selector is already set, it needs to be identical,
  493. * we support several groups with one function but not several
  494. * functions with one or several groups in the same pinmux.
  495. */
  496. if (pmx->func_selector != UINT_MAX &&
  497. pmx->func_selector != func_selector) {
  498. dev_err(&pctldev->dev,
  499. "dual function defines in the map for device %s\n",
  500. devname);
  501. return -EINVAL;
  502. }
  503. pmx->func_selector = func_selector;
  504. /* Now add this group selector, we may have many of them */
  505. grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
  506. if (!grp)
  507. return -ENOMEM;
  508. grp->group_selector = group_selector;
  509. ret = acquire_pins(pctldev, func_selector, group_selector);
  510. if (ret) {
  511. kfree(grp);
  512. return ret;
  513. }
  514. list_add(&grp->node, &pmx->groups);
  515. return 0;
  516. }
  517. static void pinmux_free_groups(struct pinmux *pmx)
  518. {
  519. struct list_head *node, *tmp;
  520. list_for_each_safe(node, tmp, &pmx->groups) {
  521. struct pinmux_group *grp =
  522. list_entry(node, struct pinmux_group, node);
  523. /* Release all pins taken by this group */
  524. release_pins(pmx->pctldev, grp->group_selector);
  525. list_del(node);
  526. kfree(grp);
  527. }
  528. }
  529. /**
  530. * pinmux_get() - retrieves the pinmux for a certain device
  531. * @dev: the device to get the pinmux for
  532. * @name: an optional specific mux mapping name or NULL, the name is only
  533. * needed if you want to have more than one mapping per device, or if you
  534. * need an anonymous pinmux (not tied to any specific device)
  535. */
  536. struct pinmux *pinmux_get(struct device *dev, const char *name)
  537. {
  538. struct pinmux_map const *map = NULL;
  539. struct pinctrl_dev *pctldev = NULL;
  540. const char *devname = NULL;
  541. struct pinmux *pmx;
  542. bool found_map;
  543. unsigned num_maps = 0;
  544. int ret = -ENODEV;
  545. int i;
  546. /* We must have dev or ID or both */
  547. if (!dev && !name)
  548. return ERR_PTR(-EINVAL);
  549. if (dev)
  550. devname = dev_name(dev);
  551. pr_debug("get mux %s for device %s\n", name,
  552. devname ? devname : "(none)");
  553. /*
  554. * create the state cookie holder struct pinmux for each
  555. * mapping, this is what consumers will get when requesting
  556. * a pinmux handle with pinmux_get()
  557. */
  558. pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
  559. if (pmx == NULL)
  560. return ERR_PTR(-ENOMEM);
  561. mutex_init(&pmx->mutex);
  562. pmx->func_selector = UINT_MAX;
  563. INIT_LIST_HEAD(&pmx->groups);
  564. /* Iterate over the pinmux maps to locate the right ones */
  565. for (i = 0; i < pinmux_maps_num; i++) {
  566. map = &pinmux_maps[i];
  567. found_map = false;
  568. /*
  569. * First, try to find the pctldev given in the map
  570. */
  571. pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
  572. map->ctrl_dev_name);
  573. if (!pctldev) {
  574. const char *devname = NULL;
  575. if (map->ctrl_dev)
  576. devname = dev_name(map->ctrl_dev);
  577. else if (map->ctrl_dev_name)
  578. devname = map->ctrl_dev_name;
  579. pr_warning("could not find a pinctrl device for pinmux "
  580. "function %s, fishy, they shall all have one\n",
  581. map->function);
  582. pr_warning("given pinctrl device name: %s",
  583. devname ? devname : "UNDEFINED");
  584. /* Continue to check the other mappings anyway... */
  585. continue;
  586. }
  587. pr_debug("in map, found pctldev %s to handle function %s",
  588. dev_name(&pctldev->dev), map->function);
  589. /*
  590. * If we're looking for a specific named map, this must match,
  591. * else we loop and look for the next.
  592. */
  593. if (name != NULL) {
  594. if (map->name == NULL)
  595. continue;
  596. if (strcmp(map->name, name))
  597. continue;
  598. }
  599. /*
  600. * This is for the case where no device name is given, we
  601. * already know that the function name matches from above
  602. * code.
  603. */
  604. if (!map->dev_name && (name != NULL))
  605. found_map = true;
  606. /* If the mapping has a device set up it must match */
  607. if (map->dev_name &&
  608. (!devname || !strcmp(map->dev_name, devname)))
  609. /* MATCH! */
  610. found_map = true;
  611. /* If this map is applicable, then apply it */
  612. if (found_map) {
  613. ret = pinmux_enable_muxmap(pctldev, pmx, dev,
  614. devname, map);
  615. if (ret) {
  616. pinmux_free_groups(pmx);
  617. kfree(pmx);
  618. return ERR_PTR(ret);
  619. }
  620. num_maps++;
  621. }
  622. }
  623. /* We should have atleast one map, right */
  624. if (!num_maps) {
  625. pr_err("could not find any mux maps for device %s, ID %s\n",
  626. devname ? devname : "(anonymous)",
  627. name ? name : "(undefined)");
  628. kfree(pmx);
  629. return ERR_PTR(-EINVAL);
  630. }
  631. pr_debug("found %u mux maps for device %s, UD %s\n",
  632. num_maps,
  633. devname ? devname : "(anonymous)",
  634. name ? name : "(undefined)");
  635. /* Add the pinmux to the global list */
  636. mutex_lock(&pinmux_list_mutex);
  637. list_add(&pmx->node, &pinmux_list);
  638. mutex_unlock(&pinmux_list_mutex);
  639. return pmx;
  640. }
  641. EXPORT_SYMBOL_GPL(pinmux_get);
  642. /**
  643. * pinmux_put() - release a previously claimed pinmux
  644. * @pmx: a pinmux previously claimed by pinmux_get()
  645. */
  646. void pinmux_put(struct pinmux *pmx)
  647. {
  648. if (pmx == NULL)
  649. return;
  650. mutex_lock(&pmx->mutex);
  651. if (pmx->usecount)
  652. pr_warn("releasing pinmux with active users!\n");
  653. /* Free the groups and all acquired pins */
  654. pinmux_free_groups(pmx);
  655. mutex_unlock(&pmx->mutex);
  656. /* Remove from list */
  657. mutex_lock(&pinmux_list_mutex);
  658. list_del(&pmx->node);
  659. mutex_unlock(&pinmux_list_mutex);
  660. kfree(pmx);
  661. }
  662. EXPORT_SYMBOL_GPL(pinmux_put);
  663. /**
  664. * pinmux_enable() - enable a certain pinmux setting
  665. * @pmx: the pinmux to enable, previously claimed by pinmux_get()
  666. */
  667. int pinmux_enable(struct pinmux *pmx)
  668. {
  669. int ret = 0;
  670. if (pmx == NULL)
  671. return -EINVAL;
  672. mutex_lock(&pmx->mutex);
  673. if (pmx->usecount++ == 0) {
  674. struct pinctrl_dev *pctldev = pmx->pctldev;
  675. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  676. struct pinmux_group *grp;
  677. list_for_each_entry(grp, &pmx->groups, node) {
  678. ret = ops->enable(pctldev, pmx->func_selector,
  679. grp->group_selector);
  680. if (ret) {
  681. /*
  682. * TODO: call disable() on all groups we called
  683. * enable() on to this point?
  684. */
  685. pmx->usecount--;
  686. break;
  687. }
  688. }
  689. }
  690. mutex_unlock(&pmx->mutex);
  691. return ret;
  692. }
  693. EXPORT_SYMBOL_GPL(pinmux_enable);
  694. /**
  695. * pinmux_disable() - disable a certain pinmux setting
  696. * @pmx: the pinmux to disable, previously claimed by pinmux_get()
  697. */
  698. void pinmux_disable(struct pinmux *pmx)
  699. {
  700. if (pmx == NULL)
  701. return;
  702. mutex_lock(&pmx->mutex);
  703. if (--pmx->usecount == 0) {
  704. struct pinctrl_dev *pctldev = pmx->pctldev;
  705. const struct pinmux_ops *ops = pctldev->desc->pmxops;
  706. struct pinmux_group *grp;
  707. list_for_each_entry(grp, &pmx->groups, node) {
  708. ops->disable(pctldev, pmx->func_selector,
  709. grp->group_selector);
  710. }
  711. }
  712. mutex_unlock(&pmx->mutex);
  713. }
  714. EXPORT_SYMBOL_GPL(pinmux_disable);
  715. int pinmux_check_ops(const struct pinmux_ops *ops)
  716. {
  717. /* Check that we implement required operations */
  718. if (!ops->list_functions ||
  719. !ops->get_function_name ||
  720. !ops->get_function_groups ||
  721. !ops->enable ||
  722. !ops->disable)
  723. return -EINVAL;
  724. return 0;
  725. }
  726. /* Hog a single map entry and add to the hoglist */
  727. static int pinmux_hog_map(struct pinctrl_dev *pctldev,
  728. struct pinmux_map const *map)
  729. {
  730. struct pinmux_hog *hog;
  731. struct pinmux *pmx;
  732. int ret;
  733. if (map->dev || map->dev_name) {
  734. /*
  735. * TODO: the day we have device tree support, we can
  736. * traverse the device tree and hog to specific device nodes
  737. * without any problems, so then we can hog pinmuxes for
  738. * all devices that just want a static pin mux at this point.
  739. */
  740. dev_err(&pctldev->dev, "map %s wants to hog a non-system "
  741. "pinmux, this is not going to work\n", map->name);
  742. return -EINVAL;
  743. }
  744. hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
  745. if (!hog)
  746. return -ENOMEM;
  747. pmx = pinmux_get(NULL, map->name);
  748. if (IS_ERR(pmx)) {
  749. kfree(hog);
  750. dev_err(&pctldev->dev,
  751. "could not get the %s pinmux mapping for hogging\n",
  752. map->name);
  753. return PTR_ERR(pmx);
  754. }
  755. ret = pinmux_enable(pmx);
  756. if (ret) {
  757. pinmux_put(pmx);
  758. kfree(hog);
  759. dev_err(&pctldev->dev,
  760. "could not enable the %s pinmux mapping for hogging\n",
  761. map->name);
  762. return ret;
  763. }
  764. hog->map = map;
  765. hog->pmx = pmx;
  766. dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name,
  767. map->function);
  768. mutex_lock(&pctldev->pinmux_hogs_lock);
  769. list_add(&hog->node, &pctldev->pinmux_hogs);
  770. mutex_unlock(&pctldev->pinmux_hogs_lock);
  771. return 0;
  772. }
  773. /**
  774. * pinmux_hog_maps() - hog specific map entries on controller device
  775. * @pctldev: the pin control device to hog entries on
  776. *
  777. * When the pin controllers are registered, there may be some specific pinmux
  778. * map entries that need to be hogged, i.e. get+enabled until the system shuts
  779. * down.
  780. */
  781. int pinmux_hog_maps(struct pinctrl_dev *pctldev)
  782. {
  783. struct device *dev = &pctldev->dev;
  784. const char *devname = dev_name(dev);
  785. int ret;
  786. int i;
  787. INIT_LIST_HEAD(&pctldev->pinmux_hogs);
  788. mutex_init(&pctldev->pinmux_hogs_lock);
  789. for (i = 0; i < pinmux_maps_num; i++) {
  790. struct pinmux_map const *map = &pinmux_maps[i];
  791. if (((map->ctrl_dev == dev) ||
  792. !strcmp(map->ctrl_dev_name, devname)) &&
  793. map->hog_on_boot) {
  794. /* OK time to hog! */
  795. ret = pinmux_hog_map(pctldev, map);
  796. if (ret)
  797. return ret;
  798. }
  799. }
  800. return 0;
  801. }
  802. /**
  803. * pinmux_hog_maps() - unhog specific map entries on controller device
  804. * @pctldev: the pin control device to unhog entries on
  805. */
  806. void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
  807. {
  808. struct list_head *node, *tmp;
  809. mutex_lock(&pctldev->pinmux_hogs_lock);
  810. list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
  811. struct pinmux_hog *hog =
  812. list_entry(node, struct pinmux_hog, node);
  813. pinmux_disable(hog->pmx);
  814. pinmux_put(hog->pmx);
  815. list_del(node);
  816. kfree(hog);
  817. }
  818. mutex_unlock(&pctldev->pinmux_hogs_lock);
  819. }
  820. #ifdef CONFIG_DEBUG_FS
  821. /* Called from pincontrol core */
  822. static int pinmux_functions_show(struct seq_file *s, void *what)
  823. {
  824. struct pinctrl_dev *pctldev = s->private;
  825. const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
  826. unsigned func_selector = 0;
  827. while (pmxops->list_functions(pctldev, func_selector) >= 0) {
  828. const char *func = pmxops->get_function_name(pctldev,
  829. func_selector);
  830. const char * const *groups;
  831. unsigned num_groups;
  832. int ret;
  833. int i;
  834. ret = pmxops->get_function_groups(pctldev, func_selector,
  835. &groups, &num_groups);
  836. if (ret)
  837. seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
  838. func);
  839. seq_printf(s, "function: %s, groups = [ ", func);
  840. for (i = 0; i < num_groups; i++)
  841. seq_printf(s, "%s ", groups[i]);
  842. seq_puts(s, "]\n");
  843. func_selector++;
  844. }
  845. return 0;
  846. }
  847. static int pinmux_pins_show(struct seq_file *s, void *what)
  848. {
  849. struct pinctrl_dev *pctldev = s->private;
  850. unsigned pin;
  851. seq_puts(s, "Pinmux settings per pin\n");
  852. seq_puts(s, "Format: pin (name): pinmuxfunction\n");
  853. /* The highest pin number need to be included in the loop, thus <= */
  854. for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
  855. struct pin_desc *desc;
  856. desc = pin_desc_get(pctldev, pin);
  857. /* Pin space may be sparse */
  858. if (desc == NULL)
  859. continue;
  860. seq_printf(s, "pin %d (%s): %s\n", pin,
  861. desc->name ? desc->name : "unnamed",
  862. desc->mux_function ? desc->mux_function
  863. : "UNCLAIMED");
  864. }
  865. return 0;
  866. }
  867. static int pinmux_hogs_show(struct seq_file *s, void *what)
  868. {
  869. struct pinctrl_dev *pctldev = s->private;
  870. struct pinmux_hog *hog;
  871. seq_puts(s, "Pinmux map hogs held by device\n");
  872. list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
  873. seq_printf(s, "%s\n", hog->map->name);
  874. return 0;
  875. }
  876. static int pinmux_show(struct seq_file *s, void *what)
  877. {
  878. struct pinmux *pmx;
  879. seq_puts(s, "Requested pinmuxes and their maps:\n");
  880. list_for_each_entry(pmx, &pinmux_list, node) {
  881. struct pinctrl_dev *pctldev = pmx->pctldev;
  882. const struct pinmux_ops *pmxops;
  883. const struct pinctrl_ops *pctlops;
  884. struct pinmux_group *grp;
  885. if (!pctldev) {
  886. seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
  887. continue;
  888. }
  889. pmxops = pctldev->desc->pmxops;
  890. pctlops = pctldev->desc->pctlops;
  891. seq_printf(s, "device: %s function: %s (%u),",
  892. pinctrl_dev_get_name(pmx->pctldev),
  893. pmxops->get_function_name(pctldev, pmx->func_selector),
  894. pmx->func_selector);
  895. seq_printf(s, " groups: [");
  896. list_for_each_entry(grp, &pmx->groups, node) {
  897. seq_printf(s, " %s (%u)",
  898. pctlops->get_group_name(pctldev, grp->group_selector),
  899. grp->group_selector);
  900. }
  901. seq_printf(s, " ]");
  902. seq_printf(s, " users: %u map-> %s\n",
  903. pmx->usecount,
  904. pmx->dev ? dev_name(pmx->dev) : "(system)");
  905. }
  906. return 0;
  907. }
  908. static int pinmux_maps_show(struct seq_file *s, void *what)
  909. {
  910. int i;
  911. seq_puts(s, "Pinmux maps:\n");
  912. for (i = 0; i < pinmux_maps_num; i++) {
  913. struct pinmux_map const *map = &pinmux_maps[i];
  914. seq_printf(s, "%s:\n", map->name);
  915. if (map->dev || map->dev_name)
  916. seq_printf(s, " device: %s\n",
  917. map->dev ? dev_name(map->dev) :
  918. map->dev_name);
  919. else
  920. seq_printf(s, " SYSTEM MUX\n");
  921. seq_printf(s, " controlling device %s\n",
  922. map->ctrl_dev ? dev_name(map->ctrl_dev) :
  923. map->ctrl_dev_name);
  924. seq_printf(s, " function: %s\n", map->function);
  925. seq_printf(s, " group: %s\n", map->group ? map->group :
  926. "(default)");
  927. }
  928. return 0;
  929. }
  930. static int pinmux_functions_open(struct inode *inode, struct file *file)
  931. {
  932. return single_open(file, pinmux_functions_show, inode->i_private);
  933. }
  934. static int pinmux_pins_open(struct inode *inode, struct file *file)
  935. {
  936. return single_open(file, pinmux_pins_show, inode->i_private);
  937. }
  938. static int pinmux_hogs_open(struct inode *inode, struct file *file)
  939. {
  940. return single_open(file, pinmux_hogs_show, inode->i_private);
  941. }
  942. static int pinmux_open(struct inode *inode, struct file *file)
  943. {
  944. return single_open(file, pinmux_show, NULL);
  945. }
  946. static int pinmux_maps_open(struct inode *inode, struct file *file)
  947. {
  948. return single_open(file, pinmux_maps_show, NULL);
  949. }
  950. static const struct file_operations pinmux_functions_ops = {
  951. .open = pinmux_functions_open,
  952. .read = seq_read,
  953. .llseek = seq_lseek,
  954. .release = single_release,
  955. };
  956. static const struct file_operations pinmux_pins_ops = {
  957. .open = pinmux_pins_open,
  958. .read = seq_read,
  959. .llseek = seq_lseek,
  960. .release = single_release,
  961. };
  962. static const struct file_operations pinmux_hogs_ops = {
  963. .open = pinmux_hogs_open,
  964. .read = seq_read,
  965. .llseek = seq_lseek,
  966. .release = single_release,
  967. };
  968. static const struct file_operations pinmux_ops = {
  969. .open = pinmux_open,
  970. .read = seq_read,
  971. .llseek = seq_lseek,
  972. .release = single_release,
  973. };
  974. static const struct file_operations pinmux_maps_ops = {
  975. .open = pinmux_maps_open,
  976. .read = seq_read,
  977. .llseek = seq_lseek,
  978. .release = single_release,
  979. };
  980. void pinmux_init_device_debugfs(struct dentry *devroot,
  981. struct pinctrl_dev *pctldev)
  982. {
  983. debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
  984. devroot, pctldev, &pinmux_functions_ops);
  985. debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
  986. devroot, pctldev, &pinmux_pins_ops);
  987. debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
  988. devroot, pctldev, &pinmux_hogs_ops);
  989. }
  990. void pinmux_init_debugfs(struct dentry *subsys_root)
  991. {
  992. debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
  993. subsys_root, NULL, &pinmux_ops);
  994. debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
  995. subsys_root, NULL, &pinmux_maps_ops);
  996. }
  997. #endif /* CONFIG_DEBUG_FS */