pinmux.c 30 KB

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