pinmux.c 30 KB

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