pinmux.c 31 KB

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