pinmux.c 32 KB

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