core.c 31 KB

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