pinmux.c 33 KB

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