pinmux.c 30 KB

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