core.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * Core driver for the pin control subsystem
  3. *
  4. * Copyright (C) 2011-2012 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. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  11. *
  12. * License terms: GNU General Public License (GPL) version 2
  13. */
  14. #define pr_fmt(fmt) "pinctrl core: " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/export.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/radix-tree.h>
  21. #include <linux/err.h>
  22. #include <linux/list.h>
  23. #include <linux/mutex.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/pinctrl/pinctrl.h>
  29. #include <linux/pinctrl/machine.h>
  30. #include "core.h"
  31. #include "pinmux.h"
  32. #include "pinconf.h"
  33. /**
  34. * struct pinctrl_maps - a list item containing part of the mapping table
  35. * @node: mapping table list node
  36. * @maps: array of mapping table entries
  37. * @num_maps: the number of entries in @maps
  38. */
  39. struct pinctrl_maps {
  40. struct list_head node;
  41. struct pinctrl_map const *maps;
  42. unsigned num_maps;
  43. };
  44. /**
  45. * struct pinctrl_hog - a list item to stash control hogs
  46. * @node: pin control hog list node
  47. * @map: map entry responsible for this hogging
  48. * @pmx: the pin control hogged by this item
  49. */
  50. struct pinctrl_hog {
  51. struct list_head node;
  52. struct pinctrl_map const *map;
  53. struct pinctrl *p;
  54. };
  55. /* Global list of pin control devices */
  56. static DEFINE_MUTEX(pinctrldev_list_mutex);
  57. static LIST_HEAD(pinctrldev_list);
  58. /* List of pin controller handles */
  59. static DEFINE_MUTEX(pinctrl_list_mutex);
  60. static LIST_HEAD(pinctrl_list);
  61. /* Global pinctrl maps */
  62. static DEFINE_MUTEX(pinctrl_maps_mutex);
  63. static LIST_HEAD(pinctrl_maps);
  64. #define for_each_maps(_maps_node_, _i_, _map_) \
  65. list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
  66. for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
  67. _i_ < _maps_node_->num_maps; \
  68. i++, _map_ = &_maps_node_->maps[_i_])
  69. const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
  70. {
  71. /* We're not allowed to register devices without name */
  72. return pctldev->desc->name;
  73. }
  74. EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
  75. void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
  76. {
  77. return pctldev->driver_data;
  78. }
  79. EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
  80. /**
  81. * get_pinctrl_dev_from_devname() - look up pin controller device
  82. * @devname: the name of a device instance, as returned by dev_name()
  83. *
  84. * Looks up a pin control device matching a certain device name or pure device
  85. * pointer, the pure device pointer will take precedence.
  86. */
  87. struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
  88. {
  89. struct pinctrl_dev *pctldev = NULL;
  90. bool found = false;
  91. if (!devname)
  92. return NULL;
  93. mutex_lock(&pinctrldev_list_mutex);
  94. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  95. if (!strcmp(dev_name(pctldev->dev), devname)) {
  96. /* Matched on device name */
  97. found = true;
  98. break;
  99. }
  100. }
  101. mutex_unlock(&pinctrldev_list_mutex);
  102. return found ? pctldev : NULL;
  103. }
  104. struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
  105. {
  106. struct pin_desc *pindesc;
  107. unsigned long flags;
  108. spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
  109. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
  110. spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
  111. return pindesc;
  112. }
  113. /**
  114. * pin_get_from_name() - look up a pin number from a name
  115. * @pctldev: the pin control device to lookup the pin on
  116. * @name: the name of the pin to look up
  117. */
  118. int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
  119. {
  120. unsigned i, pin;
  121. /* The pin number can be retrived from the pin controller descriptor */
  122. for (i = 0; i < pctldev->desc->npins; i++) {
  123. struct pin_desc *desc;
  124. pin = pctldev->desc->pins[i].number;
  125. desc = pin_desc_get(pctldev, pin);
  126. /* Pin space may be sparse */
  127. if (desc == NULL)
  128. continue;
  129. if (desc->name && !strcmp(name, desc->name))
  130. return pin;
  131. }
  132. return -EINVAL;
  133. }
  134. /**
  135. * pin_is_valid() - check if pin exists on controller
  136. * @pctldev: the pin control device to check the pin on
  137. * @pin: pin to check, use the local pin controller index number
  138. *
  139. * This tells us whether a certain pin exist on a certain pin controller or
  140. * not. Pin lists may be sparse, so some pins may not exist.
  141. */
  142. bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
  143. {
  144. struct pin_desc *pindesc;
  145. if (pin < 0)
  146. return false;
  147. pindesc = pin_desc_get(pctldev, pin);
  148. if (pindesc == NULL)
  149. return false;
  150. return true;
  151. }
  152. EXPORT_SYMBOL_GPL(pin_is_valid);
  153. /* Deletes a range of pin descriptors */
  154. static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
  155. const struct pinctrl_pin_desc *pins,
  156. unsigned num_pins)
  157. {
  158. int i;
  159. spin_lock(&pctldev->pin_desc_tree_lock);
  160. for (i = 0; i < num_pins; i++) {
  161. struct pin_desc *pindesc;
  162. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
  163. pins[i].number);
  164. if (pindesc != NULL) {
  165. radix_tree_delete(&pctldev->pin_desc_tree,
  166. pins[i].number);
  167. if (pindesc->dynamic_name)
  168. kfree(pindesc->name);
  169. }
  170. kfree(pindesc);
  171. }
  172. spin_unlock(&pctldev->pin_desc_tree_lock);
  173. }
  174. static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
  175. unsigned number, const char *name)
  176. {
  177. struct pin_desc *pindesc;
  178. pindesc = pin_desc_get(pctldev, number);
  179. if (pindesc != NULL) {
  180. pr_err("pin %d already registered on %s\n", number,
  181. pctldev->desc->name);
  182. return -EINVAL;
  183. }
  184. pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
  185. if (pindesc == NULL)
  186. return -ENOMEM;
  187. spin_lock_init(&pindesc->lock);
  188. /* Set owner */
  189. pindesc->pctldev = pctldev;
  190. /* Copy basic pin info */
  191. if (name) {
  192. pindesc->name = name;
  193. } else {
  194. pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
  195. if (pindesc->name == NULL)
  196. return -ENOMEM;
  197. pindesc->dynamic_name = true;
  198. }
  199. spin_lock(&pctldev->pin_desc_tree_lock);
  200. radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
  201. spin_unlock(&pctldev->pin_desc_tree_lock);
  202. pr_debug("registered pin %d (%s) on %s\n",
  203. number, pindesc->name, pctldev->desc->name);
  204. return 0;
  205. }
  206. static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
  207. struct pinctrl_pin_desc const *pins,
  208. unsigned num_descs)
  209. {
  210. unsigned i;
  211. int ret = 0;
  212. for (i = 0; i < num_descs; i++) {
  213. ret = pinctrl_register_one_pin(pctldev,
  214. pins[i].number, pins[i].name);
  215. if (ret)
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
  222. * @pctldev: pin controller device to check
  223. * @gpio: gpio pin to check taken from the global GPIO pin space
  224. *
  225. * Tries to match a GPIO pin number to the ranges handled by a certain pin
  226. * controller, return the range or NULL
  227. */
  228. static struct pinctrl_gpio_range *
  229. pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
  230. {
  231. struct pinctrl_gpio_range *range = NULL;
  232. /* Loop over the ranges */
  233. mutex_lock(&pctldev->gpio_ranges_lock);
  234. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  235. /* Check if we're in the valid range */
  236. if (gpio >= range->base &&
  237. gpio < range->base + range->npins) {
  238. mutex_unlock(&pctldev->gpio_ranges_lock);
  239. return range;
  240. }
  241. }
  242. mutex_unlock(&pctldev->gpio_ranges_lock);
  243. return NULL;
  244. }
  245. /**
  246. * pinctrl_get_device_gpio_range() - find device for GPIO range
  247. * @gpio: the pin to locate the pin controller for
  248. * @outdev: the pin control device if found
  249. * @outrange: the GPIO range if found
  250. *
  251. * Find the pin controller handling a certain GPIO pin from the pinspace of
  252. * the GPIO subsystem, return the device and the matching GPIO range. Returns
  253. * negative if the GPIO range could not be found in any device.
  254. */
  255. int pinctrl_get_device_gpio_range(unsigned gpio,
  256. struct pinctrl_dev **outdev,
  257. struct pinctrl_gpio_range **outrange)
  258. {
  259. struct pinctrl_dev *pctldev = NULL;
  260. /* Loop over the pin controllers */
  261. mutex_lock(&pinctrldev_list_mutex);
  262. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  263. struct pinctrl_gpio_range *range;
  264. range = pinctrl_match_gpio_range(pctldev, gpio);
  265. if (range != NULL) {
  266. *outdev = pctldev;
  267. *outrange = range;
  268. mutex_unlock(&pinctrldev_list_mutex);
  269. return 0;
  270. }
  271. }
  272. mutex_unlock(&pinctrldev_list_mutex);
  273. return -EINVAL;
  274. }
  275. /**
  276. * pinctrl_add_gpio_range() - register a GPIO range for a controller
  277. * @pctldev: pin controller device to add the range to
  278. * @range: the GPIO range to add
  279. *
  280. * This adds a range of GPIOs to be handled by a certain pin controller. Call
  281. * this to register handled ranges after registering your pin controller.
  282. */
  283. void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
  284. struct pinctrl_gpio_range *range)
  285. {
  286. mutex_lock(&pctldev->gpio_ranges_lock);
  287. list_add_tail(&range->node, &pctldev->gpio_ranges);
  288. mutex_unlock(&pctldev->gpio_ranges_lock);
  289. }
  290. /**
  291. * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
  292. * @pctldev: pin controller device to remove the range from
  293. * @range: the GPIO range to remove
  294. */
  295. void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
  296. struct pinctrl_gpio_range *range)
  297. {
  298. mutex_lock(&pctldev->gpio_ranges_lock);
  299. list_del(&range->node);
  300. mutex_unlock(&pctldev->gpio_ranges_lock);
  301. }
  302. /**
  303. * pinctrl_get_group_selector() - returns the group selector for a group
  304. * @pctldev: the pin controller handling the group
  305. * @pin_group: the pin group to look up
  306. */
  307. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  308. const char *pin_group)
  309. {
  310. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  311. unsigned group_selector = 0;
  312. while (pctlops->list_groups(pctldev, group_selector) >= 0) {
  313. const char *gname = pctlops->get_group_name(pctldev,
  314. group_selector);
  315. if (!strcmp(gname, pin_group)) {
  316. dev_dbg(pctldev->dev,
  317. "found group selector %u for %s\n",
  318. group_selector,
  319. pin_group);
  320. return group_selector;
  321. }
  322. group_selector++;
  323. }
  324. dev_err(pctldev->dev, "does not have pin group %s\n",
  325. pin_group);
  326. return -EINVAL;
  327. }
  328. /**
  329. * pinctrl_request_gpio() - request a single pin to be used in as GPIO
  330. * @gpio: the GPIO pin number from the GPIO subsystem number space
  331. *
  332. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  333. * as part of their gpio_request() semantics, platforms and individual drivers
  334. * shall *NOT* request GPIO pins to be muxed in.
  335. */
  336. int pinctrl_request_gpio(unsigned gpio)
  337. {
  338. struct pinctrl_dev *pctldev;
  339. struct pinctrl_gpio_range *range;
  340. int ret;
  341. int pin;
  342. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  343. if (ret)
  344. return -EINVAL;
  345. /* Convert to the pin controllers number space */
  346. pin = gpio - range->base + range->pin_base;
  347. return pinmux_request_gpio(pctldev, range, pin, gpio);
  348. }
  349. EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
  350. /**
  351. * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
  352. * @gpio: the GPIO pin number from the GPIO subsystem number space
  353. *
  354. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  355. * as part of their gpio_free() semantics, platforms and individual drivers
  356. * shall *NOT* request GPIO pins to be muxed out.
  357. */
  358. void pinctrl_free_gpio(unsigned gpio)
  359. {
  360. struct pinctrl_dev *pctldev;
  361. struct pinctrl_gpio_range *range;
  362. int ret;
  363. int pin;
  364. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  365. if (ret)
  366. return;
  367. /* Convert to the pin controllers number space */
  368. pin = gpio - range->base + range->pin_base;
  369. return pinmux_free_gpio(pctldev, pin, range);
  370. }
  371. EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
  372. static int pinctrl_gpio_direction(unsigned gpio, bool input)
  373. {
  374. struct pinctrl_dev *pctldev;
  375. struct pinctrl_gpio_range *range;
  376. int ret;
  377. int pin;
  378. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  379. if (ret)
  380. return ret;
  381. /* Convert to the pin controllers number space */
  382. pin = gpio - range->base + range->pin_base;
  383. return pinmux_gpio_direction(pctldev, range, pin, input);
  384. }
  385. /**
  386. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  387. * @gpio: the GPIO pin number from the GPIO subsystem number space
  388. *
  389. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  390. * as part of their gpio_direction_input() semantics, platforms and individual
  391. * drivers shall *NOT* touch pin control GPIO calls.
  392. */
  393. int pinctrl_gpio_direction_input(unsigned gpio)
  394. {
  395. return pinctrl_gpio_direction(gpio, true);
  396. }
  397. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  398. /**
  399. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  400. * @gpio: the GPIO pin number from the GPIO subsystem number space
  401. *
  402. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  403. * as part of their gpio_direction_output() semantics, platforms and individual
  404. * drivers shall *NOT* touch pin control GPIO calls.
  405. */
  406. int pinctrl_gpio_direction_output(unsigned gpio)
  407. {
  408. return pinctrl_gpio_direction(gpio, false);
  409. }
  410. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  411. static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
  412. {
  413. struct pinctrl_dev *pctldev = NULL;
  414. const char *devname = NULL;
  415. struct pinctrl *p;
  416. bool found_map;
  417. unsigned num_maps = 0;
  418. int ret = -ENODEV;
  419. struct pinctrl_maps *maps_node;
  420. int i;
  421. struct pinctrl_map const *map;
  422. /* We must have dev or ID or both */
  423. if (!dev && !name)
  424. return ERR_PTR(-EINVAL);
  425. if (dev)
  426. devname = dev_name(dev);
  427. pr_debug("get pin control handle %s for device %s\n", name,
  428. devname ? devname : "(none)");
  429. /*
  430. * create the state cookie holder struct pinctrl for each
  431. * mapping, this is what consumers will get when requesting
  432. * a pin control handle with pinctrl_get()
  433. */
  434. p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
  435. if (p == NULL)
  436. return ERR_PTR(-ENOMEM);
  437. mutex_init(&p->mutex);
  438. pinmux_init_pinctrl_handle(p);
  439. /* Iterate over the pin control maps to locate the right ones */
  440. for_each_maps(maps_node, i, map) {
  441. found_map = false;
  442. /*
  443. * First, try to find the pctldev given in the map
  444. */
  445. pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  446. if (!pctldev) {
  447. pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
  448. map->function);
  449. pr_warning("given pinctrl device name: %s",
  450. map->ctrl_dev_name);
  451. /* Continue to check the other mappings anyway... */
  452. continue;
  453. }
  454. pr_debug("in map, found pctldev %s to handle function %s",
  455. dev_name(pctldev->dev), map->function);
  456. /*
  457. * If we're looking for a specific named map, this must match,
  458. * else we loop and look for the next.
  459. */
  460. if (name != NULL) {
  461. if (map->name == NULL)
  462. continue;
  463. if (strcmp(map->name, name))
  464. continue;
  465. }
  466. /*
  467. * This is for the case where no device name is given, we
  468. * already know that the function name matches from above
  469. * code.
  470. */
  471. if (!map->dev_name && (name != NULL))
  472. found_map = true;
  473. /* If the mapping has a device set up it must match */
  474. if (map->dev_name &&
  475. (!devname || !strcmp(map->dev_name, devname)))
  476. /* MATCH! */
  477. found_map = true;
  478. /* If this map is applicable, then apply it */
  479. if (found_map) {
  480. ret = pinmux_apply_muxmap(pctldev, p, dev,
  481. devname, map);
  482. if (ret) {
  483. kfree(p);
  484. return ERR_PTR(ret);
  485. }
  486. num_maps++;
  487. }
  488. }
  489. /* We should have atleast one map, right */
  490. if (!num_maps) {
  491. pr_err("could not find any mux maps for device %s, ID %s\n",
  492. devname ? devname : "(anonymous)",
  493. name ? name : "(undefined)");
  494. kfree(p);
  495. return ERR_PTR(-EINVAL);
  496. }
  497. pr_debug("found %u mux maps for device %s, UD %s\n",
  498. num_maps,
  499. devname ? devname : "(anonymous)",
  500. name ? name : "(undefined)");
  501. /* Add the pinmux to the global list */
  502. mutex_lock(&pinctrl_list_mutex);
  503. list_add_tail(&p->node, &pinctrl_list);
  504. mutex_unlock(&pinctrl_list_mutex);
  505. return p;
  506. }
  507. /**
  508. * pinctrl_get() - retrieves the pin controller handle for a certain device
  509. * @dev: the device to get the pin controller handle for
  510. * @name: an optional specific control mapping name or NULL, the name is only
  511. * needed if you want to have more than one mapping per device, or if you
  512. * need an anonymous pin control (not tied to any specific device)
  513. */
  514. struct pinctrl *pinctrl_get(struct device *dev, const char *name)
  515. {
  516. struct pinctrl *p;
  517. mutex_lock(&pinctrl_maps_mutex);
  518. p = pinctrl_get_locked(dev, name);
  519. mutex_unlock(&pinctrl_maps_mutex);
  520. return p;
  521. }
  522. EXPORT_SYMBOL_GPL(pinctrl_get);
  523. /**
  524. * pinctrl_put() - release a previously claimed pin control handle
  525. * @p: a pin control handle previously claimed by pinctrl_get()
  526. */
  527. void pinctrl_put(struct pinctrl *p)
  528. {
  529. if (p == NULL)
  530. return;
  531. mutex_lock(&p->mutex);
  532. if (p->usecount)
  533. pr_warn("releasing pin control handle with active users!\n");
  534. /* Free the groups and all acquired pins */
  535. pinmux_put(p);
  536. mutex_unlock(&p->mutex);
  537. /* Remove from list */
  538. mutex_lock(&pinctrl_list_mutex);
  539. list_del(&p->node);
  540. mutex_unlock(&pinctrl_list_mutex);
  541. kfree(p);
  542. }
  543. EXPORT_SYMBOL_GPL(pinctrl_put);
  544. /**
  545. * pinctrl_enable() - enable a certain pin controller setting
  546. * @p: the pin control handle to enable, previously claimed by pinctrl_get()
  547. */
  548. int pinctrl_enable(struct pinctrl *p)
  549. {
  550. int ret = 0;
  551. if (p == NULL)
  552. return -EINVAL;
  553. mutex_lock(&p->mutex);
  554. if (p->usecount++ == 0) {
  555. ret = pinmux_enable(p);
  556. if (ret)
  557. p->usecount--;
  558. }
  559. mutex_unlock(&p->mutex);
  560. return ret;
  561. }
  562. EXPORT_SYMBOL_GPL(pinctrl_enable);
  563. /**
  564. * pinctrl_disable() - disable a certain pin control setting
  565. * @p: the pin control handle to disable, previously claimed by pinctrl_get()
  566. */
  567. void pinctrl_disable(struct pinctrl *p)
  568. {
  569. if (p == NULL)
  570. return;
  571. mutex_lock(&p->mutex);
  572. if (--p->usecount == 0) {
  573. pinmux_disable(p);
  574. }
  575. mutex_unlock(&p->mutex);
  576. }
  577. EXPORT_SYMBOL_GPL(pinctrl_disable);
  578. /**
  579. * pinctrl_register_mappings() - register a set of pin controller mappings
  580. * @maps: the pincontrol mappings table to register. This should probably be
  581. * marked with __initdata so it can be discarded after boot. This
  582. * function will perform a shallow copy for the mapping entries.
  583. * @num_maps: the number of maps in the mapping table
  584. */
  585. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  586. unsigned num_maps)
  587. {
  588. int i;
  589. struct pinctrl_maps *maps_node;
  590. pr_debug("add %d pinmux maps\n", num_maps);
  591. /* First sanity check the new mapping */
  592. for (i = 0; i < num_maps; i++) {
  593. if (!maps[i].name) {
  594. pr_err("failed to register map %d: no map name given\n",
  595. i);
  596. return -EINVAL;
  597. }
  598. if (!maps[i].ctrl_dev_name) {
  599. pr_err("failed to register map %s (%d): no pin control device given\n",
  600. maps[i].name, i);
  601. return -EINVAL;
  602. }
  603. if (!maps[i].function) {
  604. pr_err("failed to register map %s (%d): no function ID given\n",
  605. maps[i].name, i);
  606. return -EINVAL;
  607. }
  608. if (!maps[i].dev_name)
  609. pr_debug("add system map %s function %s with no device\n",
  610. maps[i].name,
  611. maps[i].function);
  612. else
  613. pr_debug("register map %s, function %s\n",
  614. maps[i].name,
  615. maps[i].function);
  616. }
  617. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  618. if (!maps_node) {
  619. pr_err("failed to alloc struct pinctrl_maps\n");
  620. return -ENOMEM;
  621. }
  622. maps_node->num_maps = num_maps;
  623. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
  624. if (!maps_node->maps) {
  625. kfree(maps_node);
  626. return -ENOMEM;
  627. }
  628. mutex_lock(&pinctrl_maps_mutex);
  629. list_add_tail(&maps_node->node, &pinctrl_maps);
  630. mutex_unlock(&pinctrl_maps_mutex);
  631. return 0;
  632. }
  633. /* Hog a single map entry and add to the hoglist */
  634. static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
  635. struct pinctrl_map const *map)
  636. {
  637. struct pinctrl_hog *hog;
  638. struct pinctrl *p;
  639. int ret;
  640. hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
  641. if (!hog)
  642. return -ENOMEM;
  643. p = pinctrl_get_locked(pctldev->dev, map->name);
  644. if (IS_ERR(p)) {
  645. kfree(hog);
  646. dev_err(pctldev->dev,
  647. "could not get the %s pin control mapping for hogging\n",
  648. map->name);
  649. return PTR_ERR(p);
  650. }
  651. ret = pinctrl_enable(p);
  652. if (ret) {
  653. pinctrl_put(p);
  654. kfree(hog);
  655. dev_err(pctldev->dev,
  656. "could not enable the %s pin control mapping for hogging\n",
  657. map->name);
  658. return ret;
  659. }
  660. hog->map = map;
  661. hog->p = p;
  662. dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
  663. map->function);
  664. mutex_lock(&pctldev->pinctrl_hogs_lock);
  665. list_add_tail(&hog->node, &pctldev->pinctrl_hogs);
  666. mutex_unlock(&pctldev->pinctrl_hogs_lock);
  667. return 0;
  668. }
  669. /**
  670. * pinctrl_hog_maps() - hog specific map entries on controller device
  671. * @pctldev: the pin control device to hog entries on
  672. *
  673. * When the pin controllers are registered, there may be some specific pinmux
  674. * map entries that need to be hogged, i.e. get+enabled until the system shuts
  675. * down.
  676. */
  677. int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
  678. {
  679. struct device *dev = pctldev->dev;
  680. const char *devname = dev_name(dev);
  681. int ret;
  682. struct pinctrl_maps *maps_node;
  683. int i;
  684. struct pinctrl_map const *map;
  685. INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
  686. mutex_init(&pctldev->pinctrl_hogs_lock);
  687. mutex_lock(&pinctrl_maps_mutex);
  688. for_each_maps(maps_node, i, map) {
  689. if (map->ctrl_dev_name &&
  690. !strcmp(map->ctrl_dev_name, devname) &&
  691. !strcmp(map->dev_name, devname)) {
  692. /* OK time to hog! */
  693. ret = pinctrl_hog_map(pctldev, map);
  694. if (ret) {
  695. mutex_unlock(&pinctrl_maps_mutex);
  696. return ret;
  697. }
  698. }
  699. }
  700. mutex_unlock(&pinctrl_maps_mutex);
  701. return 0;
  702. }
  703. /**
  704. * pinctrl_unhog_maps() - unhog specific map entries on controller device
  705. * @pctldev: the pin control device to unhog entries on
  706. */
  707. void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
  708. {
  709. struct list_head *node, *tmp;
  710. mutex_lock(&pctldev->pinctrl_hogs_lock);
  711. list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
  712. struct pinctrl_hog *hog =
  713. list_entry(node, struct pinctrl_hog, node);
  714. pinctrl_disable(hog->p);
  715. pinctrl_put(hog->p);
  716. list_del(node);
  717. kfree(hog);
  718. }
  719. mutex_unlock(&pctldev->pinctrl_hogs_lock);
  720. }
  721. #ifdef CONFIG_DEBUG_FS
  722. static int pinctrl_pins_show(struct seq_file *s, void *what)
  723. {
  724. struct pinctrl_dev *pctldev = s->private;
  725. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  726. unsigned i, pin;
  727. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  728. /* The pin number can be retrived from the pin controller descriptor */
  729. for (i = 0; i < pctldev->desc->npins; i++) {
  730. struct pin_desc *desc;
  731. pin = pctldev->desc->pins[i].number;
  732. desc = pin_desc_get(pctldev, pin);
  733. /* Pin space may be sparse */
  734. if (desc == NULL)
  735. continue;
  736. seq_printf(s, "pin %d (%s) ", pin,
  737. desc->name ? desc->name : "unnamed");
  738. /* Driver-specific info per pin */
  739. if (ops->pin_dbg_show)
  740. ops->pin_dbg_show(pctldev, s, pin);
  741. seq_puts(s, "\n");
  742. }
  743. return 0;
  744. }
  745. static int pinctrl_groups_show(struct seq_file *s, void *what)
  746. {
  747. struct pinctrl_dev *pctldev = s->private;
  748. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  749. unsigned selector = 0;
  750. /* No grouping */
  751. if (!ops)
  752. return 0;
  753. seq_puts(s, "registered pin groups:\n");
  754. while (ops->list_groups(pctldev, selector) >= 0) {
  755. const unsigned *pins;
  756. unsigned num_pins;
  757. const char *gname = ops->get_group_name(pctldev, selector);
  758. int ret;
  759. int i;
  760. ret = ops->get_group_pins(pctldev, selector,
  761. &pins, &num_pins);
  762. if (ret)
  763. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  764. gname);
  765. else {
  766. seq_printf(s, "group: %s, pins = [ ", gname);
  767. for (i = 0; i < num_pins; i++)
  768. seq_printf(s, "%d ", pins[i]);
  769. seq_puts(s, "]\n");
  770. }
  771. selector++;
  772. }
  773. return 0;
  774. }
  775. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  776. {
  777. struct pinctrl_dev *pctldev = s->private;
  778. struct pinctrl_gpio_range *range = NULL;
  779. seq_puts(s, "GPIO ranges handled:\n");
  780. /* Loop over the ranges */
  781. mutex_lock(&pctldev->gpio_ranges_lock);
  782. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  783. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  784. range->id, range->name,
  785. range->base, (range->base + range->npins - 1),
  786. range->pin_base,
  787. (range->pin_base + range->npins - 1));
  788. }
  789. mutex_unlock(&pctldev->gpio_ranges_lock);
  790. return 0;
  791. }
  792. static int pinctrl_maps_show(struct seq_file *s, void *what)
  793. {
  794. struct pinctrl_maps *maps_node;
  795. int i;
  796. struct pinctrl_map const *map;
  797. seq_puts(s, "Pinctrl maps:\n");
  798. mutex_lock(&pinctrl_maps_mutex);
  799. for_each_maps(maps_node, i, map) {
  800. seq_printf(s, "%s:\n", map->name);
  801. if (map->dev_name)
  802. seq_printf(s, " device: %s\n",
  803. map->dev_name);
  804. else
  805. seq_printf(s, " SYSTEM MUX\n");
  806. seq_printf(s, " controlling device %s\n",
  807. map->ctrl_dev_name);
  808. seq_printf(s, " function: %s\n", map->function);
  809. seq_printf(s, " group: %s\n", map->group ? map->group :
  810. "(default)");
  811. }
  812. mutex_unlock(&pinctrl_maps_mutex);
  813. return 0;
  814. }
  815. static int pinmux_hogs_show(struct seq_file *s, void *what)
  816. {
  817. struct pinctrl_dev *pctldev = s->private;
  818. struct pinctrl_hog *hog;
  819. seq_puts(s, "Pin control map hogs held by device\n");
  820. list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
  821. seq_printf(s, "%s\n", hog->map->name);
  822. return 0;
  823. }
  824. static int pinctrl_devices_show(struct seq_file *s, void *what)
  825. {
  826. struct pinctrl_dev *pctldev;
  827. seq_puts(s, "name [pinmux] [pinconf]\n");
  828. mutex_lock(&pinctrldev_list_mutex);
  829. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  830. seq_printf(s, "%s ", pctldev->desc->name);
  831. if (pctldev->desc->pmxops)
  832. seq_puts(s, "yes ");
  833. else
  834. seq_puts(s, "no ");
  835. if (pctldev->desc->confops)
  836. seq_puts(s, "yes");
  837. else
  838. seq_puts(s, "no");
  839. seq_puts(s, "\n");
  840. }
  841. mutex_unlock(&pinctrldev_list_mutex);
  842. return 0;
  843. }
  844. static int pinctrl_show(struct seq_file *s, void *what)
  845. {
  846. struct pinctrl *p;
  847. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  848. list_for_each_entry(p, &pinctrl_list, node) {
  849. struct pinctrl_dev *pctldev = p->pctldev;
  850. if (!pctldev) {
  851. seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
  852. continue;
  853. }
  854. seq_printf(s, "device: %s",
  855. pinctrl_dev_get_name(p->pctldev));
  856. pinmux_dbg_show(s, p);
  857. seq_printf(s, " users: %u map-> %s\n",
  858. p->usecount,
  859. p->dev ? dev_name(p->dev) : "(system)");
  860. }
  861. return 0;
  862. }
  863. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  864. {
  865. return single_open(file, pinctrl_pins_show, inode->i_private);
  866. }
  867. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  868. {
  869. return single_open(file, pinctrl_groups_show, inode->i_private);
  870. }
  871. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  872. {
  873. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  874. }
  875. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  876. {
  877. return single_open(file, pinctrl_maps_show, inode->i_private);
  878. }
  879. static int pinmux_hogs_open(struct inode *inode, struct file *file)
  880. {
  881. return single_open(file, pinmux_hogs_show, inode->i_private);
  882. }
  883. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  884. {
  885. return single_open(file, pinctrl_devices_show, NULL);
  886. }
  887. static int pinctrl_open(struct inode *inode, struct file *file)
  888. {
  889. return single_open(file, pinctrl_show, NULL);
  890. }
  891. static const struct file_operations pinctrl_pins_ops = {
  892. .open = pinctrl_pins_open,
  893. .read = seq_read,
  894. .llseek = seq_lseek,
  895. .release = single_release,
  896. };
  897. static const struct file_operations pinctrl_groups_ops = {
  898. .open = pinctrl_groups_open,
  899. .read = seq_read,
  900. .llseek = seq_lseek,
  901. .release = single_release,
  902. };
  903. static const struct file_operations pinctrl_gpioranges_ops = {
  904. .open = pinctrl_gpioranges_open,
  905. .read = seq_read,
  906. .llseek = seq_lseek,
  907. .release = single_release,
  908. };
  909. static const struct file_operations pinctrl_maps_ops = {
  910. .open = pinctrl_maps_open,
  911. .read = seq_read,
  912. .llseek = seq_lseek,
  913. .release = single_release,
  914. };
  915. static const struct file_operations pinmux_hogs_ops = {
  916. .open = pinmux_hogs_open,
  917. .read = seq_read,
  918. .llseek = seq_lseek,
  919. .release = single_release,
  920. };
  921. static const struct file_operations pinctrl_devices_ops = {
  922. .open = pinctrl_devices_open,
  923. .read = seq_read,
  924. .llseek = seq_lseek,
  925. .release = single_release,
  926. };
  927. static const struct file_operations pinctrl_ops = {
  928. .open = pinctrl_open,
  929. .read = seq_read,
  930. .llseek = seq_lseek,
  931. .release = single_release,
  932. };
  933. static struct dentry *debugfs_root;
  934. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  935. {
  936. struct dentry *device_root;
  937. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  938. debugfs_root);
  939. pctldev->device_root = device_root;
  940. if (IS_ERR(device_root) || !device_root) {
  941. pr_warn("failed to create debugfs directory for %s\n",
  942. dev_name(pctldev->dev));
  943. return;
  944. }
  945. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  946. device_root, pctldev, &pinctrl_pins_ops);
  947. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  948. device_root, pctldev, &pinctrl_groups_ops);
  949. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  950. device_root, pctldev, &pinctrl_gpioranges_ops);
  951. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  952. device_root, pctldev, &pinctrl_maps_ops);
  953. debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
  954. device_root, pctldev, &pinmux_hogs_ops);
  955. pinmux_init_device_debugfs(device_root, pctldev);
  956. pinconf_init_device_debugfs(device_root, pctldev);
  957. }
  958. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  959. {
  960. debugfs_remove_recursive(pctldev->device_root);
  961. }
  962. static void pinctrl_init_debugfs(void)
  963. {
  964. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  965. if (IS_ERR(debugfs_root) || !debugfs_root) {
  966. pr_warn("failed to create debugfs directory\n");
  967. debugfs_root = NULL;
  968. return;
  969. }
  970. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  971. debugfs_root, NULL, &pinctrl_devices_ops);
  972. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  973. debugfs_root, NULL, &pinctrl_ops);
  974. }
  975. #else /* CONFIG_DEBUG_FS */
  976. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  977. {
  978. }
  979. static void pinctrl_init_debugfs(void)
  980. {
  981. }
  982. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  983. {
  984. }
  985. #endif
  986. /**
  987. * pinctrl_register() - register a pin controller device
  988. * @pctldesc: descriptor for this pin controller
  989. * @dev: parent device for this pin controller
  990. * @driver_data: private pin controller data for this pin controller
  991. */
  992. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  993. struct device *dev, void *driver_data)
  994. {
  995. struct pinctrl_dev *pctldev;
  996. int ret;
  997. if (pctldesc == NULL)
  998. return NULL;
  999. if (pctldesc->name == NULL)
  1000. return NULL;
  1001. pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
  1002. if (pctldev == NULL)
  1003. return NULL;
  1004. /* Initialize pin control device struct */
  1005. pctldev->owner = pctldesc->owner;
  1006. pctldev->desc = pctldesc;
  1007. pctldev->driver_data = driver_data;
  1008. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1009. spin_lock_init(&pctldev->pin_desc_tree_lock);
  1010. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1011. mutex_init(&pctldev->gpio_ranges_lock);
  1012. pctldev->dev = dev;
  1013. /* If we're implementing pinmuxing, check the ops for sanity */
  1014. if (pctldesc->pmxops) {
  1015. ret = pinmux_check_ops(pctldev);
  1016. if (ret) {
  1017. pr_err("%s pinmux ops lacks necessary functions\n",
  1018. pctldesc->name);
  1019. goto out_err;
  1020. }
  1021. }
  1022. /* If we're implementing pinconfig, check the ops for sanity */
  1023. if (pctldesc->confops) {
  1024. ret = pinconf_check_ops(pctldev);
  1025. if (ret) {
  1026. pr_err("%s pin config ops lacks necessary functions\n",
  1027. pctldesc->name);
  1028. goto out_err;
  1029. }
  1030. }
  1031. /* Register all the pins */
  1032. pr_debug("try to register %d pins on %s...\n",
  1033. pctldesc->npins, pctldesc->name);
  1034. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1035. if (ret) {
  1036. pr_err("error during pin registration\n");
  1037. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1038. pctldesc->npins);
  1039. goto out_err;
  1040. }
  1041. pinctrl_init_device_debugfs(pctldev);
  1042. mutex_lock(&pinctrldev_list_mutex);
  1043. list_add_tail(&pctldev->node, &pinctrldev_list);
  1044. mutex_unlock(&pinctrldev_list_mutex);
  1045. pinctrl_hog_maps(pctldev);
  1046. return pctldev;
  1047. out_err:
  1048. kfree(pctldev);
  1049. return NULL;
  1050. }
  1051. EXPORT_SYMBOL_GPL(pinctrl_register);
  1052. /**
  1053. * pinctrl_unregister() - unregister pinmux
  1054. * @pctldev: pin controller to unregister
  1055. *
  1056. * Called by pinmux drivers to unregister a pinmux.
  1057. */
  1058. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1059. {
  1060. if (pctldev == NULL)
  1061. return;
  1062. pinctrl_remove_device_debugfs(pctldev);
  1063. pinctrl_unhog_maps(pctldev);
  1064. /* TODO: check that no pinmuxes are still active? */
  1065. mutex_lock(&pinctrldev_list_mutex);
  1066. list_del(&pctldev->node);
  1067. mutex_unlock(&pinctrldev_list_mutex);
  1068. /* Destroy descriptor tree */
  1069. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1070. pctldev->desc->npins);
  1071. kfree(pctldev);
  1072. }
  1073. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1074. static int __init pinctrl_init(void)
  1075. {
  1076. pr_info("initialized pinctrl subsystem\n");
  1077. pinctrl_init_debugfs();
  1078. return 0;
  1079. }
  1080. /* init early since many drivers really need to initialized pinmux early */
  1081. core_initcall(pinctrl_init);