pinmux.c 33 KB

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