core.c 32 KB

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