core.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  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/kref.h>
  17. #include <linux/export.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/list.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/pinctrl/machine.h>
  29. #include <asm-generic/gpio.h>
  30. #include "core.h"
  31. #include "devicetree.h"
  32. #include "pinmux.h"
  33. #include "pinconf.h"
  34. static bool pinctrl_dummy_state;
  35. /* Mutex taken by all entry points */
  36. DEFINE_MUTEX(pinctrl_mutex);
  37. /* Global list of pin control devices (struct pinctrl_dev) */
  38. LIST_HEAD(pinctrldev_list);
  39. /* List of pin controller handles (struct pinctrl) */
  40. static LIST_HEAD(pinctrl_list);
  41. /* List of pinctrl maps (struct pinctrl_maps) */
  42. LIST_HEAD(pinctrl_maps);
  43. /**
  44. * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
  45. *
  46. * Usually this function is called by platforms without pinctrl driver support
  47. * but run with some shared drivers using pinctrl APIs.
  48. * After calling this function, the pinctrl core will return successfully
  49. * with creating a dummy state for the driver to keep going smoothly.
  50. */
  51. void pinctrl_provide_dummies(void)
  52. {
  53. pinctrl_dummy_state = true;
  54. }
  55. const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
  56. {
  57. /* We're not allowed to register devices without name */
  58. return pctldev->desc->name;
  59. }
  60. EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
  61. const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
  62. {
  63. return dev_name(pctldev->dev);
  64. }
  65. EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
  66. void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
  67. {
  68. return pctldev->driver_data;
  69. }
  70. EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
  71. /**
  72. * get_pinctrl_dev_from_devname() - look up pin controller device
  73. * @devname: the name of a device instance, as returned by dev_name()
  74. *
  75. * Looks up a pin control device matching a certain device name or pure device
  76. * pointer, the pure device pointer will take precedence.
  77. */
  78. struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
  79. {
  80. struct pinctrl_dev *pctldev = NULL;
  81. bool found = false;
  82. if (!devname)
  83. return NULL;
  84. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  85. if (!strcmp(dev_name(pctldev->dev), devname)) {
  86. /* Matched on device name */
  87. found = true;
  88. break;
  89. }
  90. }
  91. return found ? pctldev : NULL;
  92. }
  93. /**
  94. * pin_get_from_name() - look up a pin number from a name
  95. * @pctldev: the pin control device to lookup the pin on
  96. * @name: the name of the pin to look up
  97. */
  98. int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
  99. {
  100. unsigned i, pin;
  101. /* The pin number can be retrived from the pin controller descriptor */
  102. for (i = 0; i < pctldev->desc->npins; i++) {
  103. struct pin_desc *desc;
  104. pin = pctldev->desc->pins[i].number;
  105. desc = pin_desc_get(pctldev, pin);
  106. /* Pin space may be sparse */
  107. if (desc == NULL)
  108. continue;
  109. if (desc->name && !strcmp(name, desc->name))
  110. return pin;
  111. }
  112. return -EINVAL;
  113. }
  114. /**
  115. * pin_get_name_from_id() - look up a pin name from a pin id
  116. * @pctldev: the pin control device to lookup the pin on
  117. * @name: the name of the pin to look up
  118. */
  119. const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
  120. {
  121. const struct pin_desc *desc;
  122. desc = pin_desc_get(pctldev, pin);
  123. if (desc == NULL) {
  124. dev_err(pctldev->dev, "failed to get pin(%d) name\n",
  125. pin);
  126. return NULL;
  127. }
  128. return desc->name;
  129. }
  130. /**
  131. * pin_is_valid() - check if pin exists on controller
  132. * @pctldev: the pin control device to check the pin on
  133. * @pin: pin to check, use the local pin controller index number
  134. *
  135. * This tells us whether a certain pin exist on a certain pin controller or
  136. * not. Pin lists may be sparse, so some pins may not exist.
  137. */
  138. bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
  139. {
  140. struct pin_desc *pindesc;
  141. if (pin < 0)
  142. return false;
  143. mutex_lock(&pinctrl_mutex);
  144. pindesc = pin_desc_get(pctldev, pin);
  145. mutex_unlock(&pinctrl_mutex);
  146. return pindesc != NULL;
  147. }
  148. EXPORT_SYMBOL_GPL(pin_is_valid);
  149. /* Deletes a range of pin descriptors */
  150. static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
  151. const struct pinctrl_pin_desc *pins,
  152. unsigned num_pins)
  153. {
  154. int i;
  155. for (i = 0; i < num_pins; i++) {
  156. struct pin_desc *pindesc;
  157. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
  158. pins[i].number);
  159. if (pindesc != NULL) {
  160. radix_tree_delete(&pctldev->pin_desc_tree,
  161. pins[i].number);
  162. if (pindesc->dynamic_name)
  163. kfree(pindesc->name);
  164. }
  165. kfree(pindesc);
  166. }
  167. }
  168. static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
  169. unsigned number, const char *name)
  170. {
  171. struct pin_desc *pindesc;
  172. pindesc = pin_desc_get(pctldev, number);
  173. if (pindesc != NULL) {
  174. pr_err("pin %d already registered on %s\n", number,
  175. pctldev->desc->name);
  176. return -EINVAL;
  177. }
  178. pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
  179. if (pindesc == NULL) {
  180. dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
  181. return -ENOMEM;
  182. }
  183. /* Set owner */
  184. pindesc->pctldev = pctldev;
  185. /* Copy basic pin info */
  186. if (name) {
  187. pindesc->name = name;
  188. } else {
  189. pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
  190. if (pindesc->name == NULL) {
  191. kfree(pindesc);
  192. return -ENOMEM;
  193. }
  194. pindesc->dynamic_name = true;
  195. }
  196. radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
  197. pr_debug("registered pin %d (%s) on %s\n",
  198. number, pindesc->name, pctldev->desc->name);
  199. return 0;
  200. }
  201. static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
  202. struct pinctrl_pin_desc const *pins,
  203. unsigned num_descs)
  204. {
  205. unsigned i;
  206. int ret = 0;
  207. for (i = 0; i < num_descs; i++) {
  208. ret = pinctrl_register_one_pin(pctldev,
  209. pins[i].number, pins[i].name);
  210. if (ret)
  211. return ret;
  212. }
  213. return 0;
  214. }
  215. /**
  216. * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
  217. * @pctldev: pin controller device to check
  218. * @gpio: gpio pin to check taken from the global GPIO pin space
  219. *
  220. * Tries to match a GPIO pin number to the ranges handled by a certain pin
  221. * controller, return the range or NULL
  222. */
  223. static struct pinctrl_gpio_range *
  224. pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
  225. {
  226. struct pinctrl_gpio_range *range = NULL;
  227. /* Loop over the ranges */
  228. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  229. /* Check if we're in the valid range */
  230. if (gpio >= range->base &&
  231. gpio < range->base + range->npins) {
  232. return range;
  233. }
  234. }
  235. return NULL;
  236. }
  237. /**
  238. * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
  239. * the same GPIO chip are in range
  240. * @gpio: gpio pin to check taken from the global GPIO pin space
  241. *
  242. * This function is complement of pinctrl_match_gpio_range(). If the return
  243. * value of pinctrl_match_gpio_range() is NULL, this function could be used
  244. * to check whether pinctrl device is ready or not. Maybe some GPIO pins
  245. * of the same GPIO chip don't have back-end pinctrl interface.
  246. * If the return value is true, it means that pinctrl device is ready & the
  247. * certain GPIO pin doesn't have back-end pinctrl device. If the return value
  248. * is false, it means that pinctrl device may not be ready.
  249. */
  250. static bool pinctrl_ready_for_gpio_range(unsigned gpio)
  251. {
  252. struct pinctrl_dev *pctldev;
  253. struct pinctrl_gpio_range *range = NULL;
  254. struct gpio_chip *chip = gpio_to_chip(gpio);
  255. /* Loop over the pin controllers */
  256. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  257. /* Loop over the ranges */
  258. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  259. /* Check if any gpio range overlapped with gpio chip */
  260. if (range->base + range->npins - 1 < chip->base ||
  261. range->base > chip->base + chip->ngpio - 1)
  262. continue;
  263. return true;
  264. }
  265. }
  266. return false;
  267. }
  268. /**
  269. * pinctrl_get_device_gpio_range() - find device for GPIO range
  270. * @gpio: the pin to locate the pin controller for
  271. * @outdev: the pin control device if found
  272. * @outrange: the GPIO range if found
  273. *
  274. * Find the pin controller handling a certain GPIO pin from the pinspace of
  275. * the GPIO subsystem, return the device and the matching GPIO range. Returns
  276. * -EPROBE_DEFER if the GPIO range could not be found in any device since it
  277. * may still have not been registered.
  278. */
  279. static int pinctrl_get_device_gpio_range(unsigned gpio,
  280. struct pinctrl_dev **outdev,
  281. struct pinctrl_gpio_range **outrange)
  282. {
  283. struct pinctrl_dev *pctldev = NULL;
  284. /* Loop over the pin controllers */
  285. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  286. struct pinctrl_gpio_range *range;
  287. range = pinctrl_match_gpio_range(pctldev, gpio);
  288. if (range != NULL) {
  289. *outdev = pctldev;
  290. *outrange = range;
  291. return 0;
  292. }
  293. }
  294. return -EPROBE_DEFER;
  295. }
  296. /**
  297. * pinctrl_add_gpio_range() - register a GPIO range for a controller
  298. * @pctldev: pin controller device to add the range to
  299. * @range: the GPIO range to add
  300. *
  301. * This adds a range of GPIOs to be handled by a certain pin controller. Call
  302. * this to register handled ranges after registering your pin controller.
  303. */
  304. void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
  305. struct pinctrl_gpio_range *range)
  306. {
  307. mutex_lock(&pinctrl_mutex);
  308. list_add_tail(&range->node, &pctldev->gpio_ranges);
  309. mutex_unlock(&pinctrl_mutex);
  310. }
  311. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
  312. void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
  313. struct pinctrl_gpio_range *ranges,
  314. unsigned nranges)
  315. {
  316. int i;
  317. for (i = 0; i < nranges; i++)
  318. pinctrl_add_gpio_range(pctldev, &ranges[i]);
  319. }
  320. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
  321. struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
  322. struct pinctrl_gpio_range *range)
  323. {
  324. struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
  325. /*
  326. * If we can't find this device, let's assume that is because
  327. * it has not probed yet, so the driver trying to register this
  328. * range need to defer probing.
  329. */
  330. if (!pctldev)
  331. return ERR_PTR(-EPROBE_DEFER);
  332. pinctrl_add_gpio_range(pctldev, range);
  333. return pctldev;
  334. }
  335. EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
  336. /**
  337. * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
  338. * @pctldev: the pin controller device to look in
  339. * @pin: a controller-local number to find the range for
  340. */
  341. struct pinctrl_gpio_range *
  342. pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
  343. unsigned int pin)
  344. {
  345. struct pinctrl_gpio_range *range = NULL;
  346. /* Loop over the ranges */
  347. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  348. /* Check if we're in the valid range */
  349. if (pin >= range->pin_base &&
  350. pin < range->pin_base + range->npins) {
  351. return range;
  352. }
  353. }
  354. return NULL;
  355. }
  356. EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
  357. /**
  358. * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
  359. * @pctldev: pin controller device to remove the range from
  360. * @range: the GPIO range to remove
  361. */
  362. void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
  363. struct pinctrl_gpio_range *range)
  364. {
  365. mutex_lock(&pinctrl_mutex);
  366. list_del(&range->node);
  367. mutex_unlock(&pinctrl_mutex);
  368. }
  369. EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
  370. /**
  371. * pinctrl_get_group_selector() - returns the group selector for a group
  372. * @pctldev: the pin controller handling the group
  373. * @pin_group: the pin group to look up
  374. */
  375. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  376. const char *pin_group)
  377. {
  378. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  379. unsigned ngroups = pctlops->get_groups_count(pctldev);
  380. unsigned group_selector = 0;
  381. while (group_selector < ngroups) {
  382. const char *gname = pctlops->get_group_name(pctldev,
  383. group_selector);
  384. if (!strcmp(gname, pin_group)) {
  385. dev_dbg(pctldev->dev,
  386. "found group selector %u for %s\n",
  387. group_selector,
  388. pin_group);
  389. return group_selector;
  390. }
  391. group_selector++;
  392. }
  393. dev_err(pctldev->dev, "does not have pin group %s\n",
  394. pin_group);
  395. return -EINVAL;
  396. }
  397. /**
  398. * pinctrl_request_gpio() - request a single pin to be used in as GPIO
  399. * @gpio: the GPIO pin number from the GPIO subsystem number space
  400. *
  401. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  402. * as part of their gpio_request() semantics, platforms and individual drivers
  403. * shall *NOT* request GPIO pins to be muxed in.
  404. */
  405. int pinctrl_request_gpio(unsigned gpio)
  406. {
  407. struct pinctrl_dev *pctldev;
  408. struct pinctrl_gpio_range *range;
  409. int ret;
  410. int pin;
  411. mutex_lock(&pinctrl_mutex);
  412. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  413. if (ret) {
  414. if (pinctrl_ready_for_gpio_range(gpio))
  415. ret = 0;
  416. mutex_unlock(&pinctrl_mutex);
  417. return ret;
  418. }
  419. /* Convert to the pin controllers number space */
  420. pin = gpio - range->base + range->pin_base;
  421. ret = pinmux_request_gpio(pctldev, range, pin, gpio);
  422. mutex_unlock(&pinctrl_mutex);
  423. return ret;
  424. }
  425. EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
  426. /**
  427. * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
  428. * @gpio: the GPIO pin number from the GPIO subsystem number space
  429. *
  430. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  431. * as part of their gpio_free() semantics, platforms and individual drivers
  432. * shall *NOT* request GPIO pins to be muxed out.
  433. */
  434. void pinctrl_free_gpio(unsigned gpio)
  435. {
  436. struct pinctrl_dev *pctldev;
  437. struct pinctrl_gpio_range *range;
  438. int ret;
  439. int pin;
  440. mutex_lock(&pinctrl_mutex);
  441. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  442. if (ret) {
  443. mutex_unlock(&pinctrl_mutex);
  444. return;
  445. }
  446. /* Convert to the pin controllers number space */
  447. pin = gpio - range->base + range->pin_base;
  448. pinmux_free_gpio(pctldev, pin, range);
  449. mutex_unlock(&pinctrl_mutex);
  450. }
  451. EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
  452. static int pinctrl_gpio_direction(unsigned gpio, bool input)
  453. {
  454. struct pinctrl_dev *pctldev;
  455. struct pinctrl_gpio_range *range;
  456. int ret;
  457. int pin;
  458. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  459. if (ret)
  460. return ret;
  461. /* Convert to the pin controllers number space */
  462. pin = gpio - range->base + range->pin_base;
  463. return pinmux_gpio_direction(pctldev, range, pin, input);
  464. }
  465. /**
  466. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  467. * @gpio: the GPIO pin number from the GPIO subsystem number space
  468. *
  469. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  470. * as part of their gpio_direction_input() semantics, platforms and individual
  471. * drivers shall *NOT* touch pin control GPIO calls.
  472. */
  473. int pinctrl_gpio_direction_input(unsigned gpio)
  474. {
  475. int ret;
  476. mutex_lock(&pinctrl_mutex);
  477. ret = pinctrl_gpio_direction(gpio, true);
  478. mutex_unlock(&pinctrl_mutex);
  479. return ret;
  480. }
  481. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  482. /**
  483. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  484. * @gpio: the GPIO pin number from the GPIO subsystem number space
  485. *
  486. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  487. * as part of their gpio_direction_output() semantics, platforms and individual
  488. * drivers shall *NOT* touch pin control GPIO calls.
  489. */
  490. int pinctrl_gpio_direction_output(unsigned gpio)
  491. {
  492. int ret;
  493. mutex_lock(&pinctrl_mutex);
  494. ret = pinctrl_gpio_direction(gpio, false);
  495. mutex_unlock(&pinctrl_mutex);
  496. return ret;
  497. }
  498. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  499. static struct pinctrl_state *find_state(struct pinctrl *p,
  500. const char *name)
  501. {
  502. struct pinctrl_state *state;
  503. list_for_each_entry(state, &p->states, node)
  504. if (!strcmp(state->name, name))
  505. return state;
  506. return NULL;
  507. }
  508. static struct pinctrl_state *create_state(struct pinctrl *p,
  509. const char *name)
  510. {
  511. struct pinctrl_state *state;
  512. state = kzalloc(sizeof(*state), GFP_KERNEL);
  513. if (state == NULL) {
  514. dev_err(p->dev,
  515. "failed to alloc struct pinctrl_state\n");
  516. return ERR_PTR(-ENOMEM);
  517. }
  518. state->name = name;
  519. INIT_LIST_HEAD(&state->settings);
  520. list_add_tail(&state->node, &p->states);
  521. return state;
  522. }
  523. static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
  524. {
  525. struct pinctrl_state *state;
  526. struct pinctrl_setting *setting;
  527. int ret;
  528. state = find_state(p, map->name);
  529. if (!state)
  530. state = create_state(p, map->name);
  531. if (IS_ERR(state))
  532. return PTR_ERR(state);
  533. if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
  534. return 0;
  535. setting = kzalloc(sizeof(*setting), GFP_KERNEL);
  536. if (setting == NULL) {
  537. dev_err(p->dev,
  538. "failed to alloc struct pinctrl_setting\n");
  539. return -ENOMEM;
  540. }
  541. setting->type = map->type;
  542. setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  543. if (setting->pctldev == NULL) {
  544. kfree(setting);
  545. /* Do not defer probing of hogs (circular loop) */
  546. if (!strcmp(map->ctrl_dev_name, map->dev_name))
  547. return -ENODEV;
  548. /*
  549. * OK let us guess that the driver is not there yet, and
  550. * let's defer obtaining this pinctrl handle to later...
  551. */
  552. dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
  553. map->ctrl_dev_name);
  554. return -EPROBE_DEFER;
  555. }
  556. setting->dev_name = map->dev_name;
  557. switch (map->type) {
  558. case PIN_MAP_TYPE_MUX_GROUP:
  559. ret = pinmux_map_to_setting(map, setting);
  560. break;
  561. case PIN_MAP_TYPE_CONFIGS_PIN:
  562. case PIN_MAP_TYPE_CONFIGS_GROUP:
  563. ret = pinconf_map_to_setting(map, setting);
  564. break;
  565. default:
  566. ret = -EINVAL;
  567. break;
  568. }
  569. if (ret < 0) {
  570. kfree(setting);
  571. return ret;
  572. }
  573. list_add_tail(&setting->node, &state->settings);
  574. return 0;
  575. }
  576. static struct pinctrl *find_pinctrl(struct device *dev)
  577. {
  578. struct pinctrl *p;
  579. list_for_each_entry(p, &pinctrl_list, node)
  580. if (p->dev == dev)
  581. return p;
  582. return NULL;
  583. }
  584. static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
  585. static struct pinctrl *create_pinctrl(struct device *dev)
  586. {
  587. struct pinctrl *p;
  588. const char *devname;
  589. struct pinctrl_maps *maps_node;
  590. int i;
  591. struct pinctrl_map const *map;
  592. int ret;
  593. /*
  594. * create the state cookie holder struct pinctrl for each
  595. * mapping, this is what consumers will get when requesting
  596. * a pin control handle with pinctrl_get()
  597. */
  598. p = kzalloc(sizeof(*p), GFP_KERNEL);
  599. if (p == NULL) {
  600. dev_err(dev, "failed to alloc struct pinctrl\n");
  601. return ERR_PTR(-ENOMEM);
  602. }
  603. p->dev = dev;
  604. INIT_LIST_HEAD(&p->states);
  605. INIT_LIST_HEAD(&p->dt_maps);
  606. ret = pinctrl_dt_to_map(p);
  607. if (ret < 0) {
  608. kfree(p);
  609. return ERR_PTR(ret);
  610. }
  611. devname = dev_name(dev);
  612. /* Iterate over the pin control maps to locate the right ones */
  613. for_each_maps(maps_node, i, map) {
  614. /* Map must be for this device */
  615. if (strcmp(map->dev_name, devname))
  616. continue;
  617. ret = add_setting(p, map);
  618. /*
  619. * At this point the adding of a setting may:
  620. *
  621. * - Defer, if the pinctrl device is not yet available
  622. * - Fail, if the pinctrl device is not yet available,
  623. * AND the setting is a hog. We cannot defer that, since
  624. * the hog will kick in immediately after the device
  625. * is registered.
  626. *
  627. * If the error returned was not -EPROBE_DEFER then we
  628. * accumulate the errors to see if we end up with
  629. * an -EPROBE_DEFER later, as that is the worst case.
  630. */
  631. if (ret == -EPROBE_DEFER) {
  632. pinctrl_put_locked(p, false);
  633. return ERR_PTR(ret);
  634. }
  635. }
  636. if (ret < 0) {
  637. /* If some other error than deferral occured, return here */
  638. pinctrl_put_locked(p, false);
  639. return ERR_PTR(ret);
  640. }
  641. kref_init(&p->users);
  642. /* Add the pinctrl handle to the global list */
  643. list_add_tail(&p->node, &pinctrl_list);
  644. return p;
  645. }
  646. static struct pinctrl *pinctrl_get_locked(struct device *dev)
  647. {
  648. struct pinctrl *p;
  649. if (WARN_ON(!dev))
  650. return ERR_PTR(-EINVAL);
  651. /*
  652. * See if somebody else (such as the device core) has already
  653. * obtained a handle to the pinctrl for this device. In that case,
  654. * return another pointer to it.
  655. */
  656. p = find_pinctrl(dev);
  657. if (p != NULL) {
  658. dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
  659. kref_get(&p->users);
  660. return p;
  661. }
  662. return create_pinctrl(dev);
  663. }
  664. /**
  665. * pinctrl_get() - retrieves the pinctrl handle for a device
  666. * @dev: the device to obtain the handle for
  667. */
  668. struct pinctrl *pinctrl_get(struct device *dev)
  669. {
  670. struct pinctrl *p;
  671. mutex_lock(&pinctrl_mutex);
  672. p = pinctrl_get_locked(dev);
  673. mutex_unlock(&pinctrl_mutex);
  674. return p;
  675. }
  676. EXPORT_SYMBOL_GPL(pinctrl_get);
  677. static void pinctrl_free_setting(bool disable_setting,
  678. struct pinctrl_setting *setting)
  679. {
  680. switch (setting->type) {
  681. case PIN_MAP_TYPE_MUX_GROUP:
  682. if (disable_setting)
  683. pinmux_disable_setting(setting);
  684. pinmux_free_setting(setting);
  685. break;
  686. case PIN_MAP_TYPE_CONFIGS_PIN:
  687. case PIN_MAP_TYPE_CONFIGS_GROUP:
  688. pinconf_free_setting(setting);
  689. break;
  690. default:
  691. break;
  692. }
  693. }
  694. static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
  695. {
  696. struct pinctrl_state *state, *n1;
  697. struct pinctrl_setting *setting, *n2;
  698. list_for_each_entry_safe(state, n1, &p->states, node) {
  699. list_for_each_entry_safe(setting, n2, &state->settings, node) {
  700. pinctrl_free_setting(state == p->state, setting);
  701. list_del(&setting->node);
  702. kfree(setting);
  703. }
  704. list_del(&state->node);
  705. kfree(state);
  706. }
  707. pinctrl_dt_free_maps(p);
  708. if (inlist)
  709. list_del(&p->node);
  710. kfree(p);
  711. }
  712. /**
  713. * pinctrl_release() - release the pinctrl handle
  714. * @kref: the kref in the pinctrl being released
  715. */
  716. static void pinctrl_release(struct kref *kref)
  717. {
  718. struct pinctrl *p = container_of(kref, struct pinctrl, users);
  719. pinctrl_put_locked(p, true);
  720. }
  721. /**
  722. * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
  723. * @p: the pinctrl handle to release
  724. */
  725. void pinctrl_put(struct pinctrl *p)
  726. {
  727. mutex_lock(&pinctrl_mutex);
  728. kref_put(&p->users, pinctrl_release);
  729. mutex_unlock(&pinctrl_mutex);
  730. }
  731. EXPORT_SYMBOL_GPL(pinctrl_put);
  732. static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
  733. const char *name)
  734. {
  735. struct pinctrl_state *state;
  736. state = find_state(p, name);
  737. if (!state) {
  738. if (pinctrl_dummy_state) {
  739. /* create dummy state */
  740. dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
  741. name);
  742. state = create_state(p, name);
  743. } else
  744. state = ERR_PTR(-ENODEV);
  745. }
  746. return state;
  747. }
  748. /**
  749. * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
  750. * @p: the pinctrl handle to retrieve the state from
  751. * @name: the state name to retrieve
  752. */
  753. struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
  754. {
  755. struct pinctrl_state *s;
  756. mutex_lock(&pinctrl_mutex);
  757. s = pinctrl_lookup_state_locked(p, name);
  758. mutex_unlock(&pinctrl_mutex);
  759. return s;
  760. }
  761. EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
  762. static int pinctrl_select_state_locked(struct pinctrl *p,
  763. struct pinctrl_state *state)
  764. {
  765. struct pinctrl_setting *setting, *setting2;
  766. struct pinctrl_state *old_state = p->state;
  767. int ret;
  768. if (p->state == state)
  769. return 0;
  770. if (p->state) {
  771. /*
  772. * The set of groups with a mux configuration in the old state
  773. * may not be identical to the set of groups with a mux setting
  774. * in the new state. While this might be unusual, it's entirely
  775. * possible for the "user"-supplied mapping table to be written
  776. * that way. For each group that was configured in the old state
  777. * but not in the new state, this code puts that group into a
  778. * safe/disabled state.
  779. */
  780. list_for_each_entry(setting, &p->state->settings, node) {
  781. bool found = false;
  782. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
  783. continue;
  784. list_for_each_entry(setting2, &state->settings, node) {
  785. if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
  786. continue;
  787. if (setting2->data.mux.group ==
  788. setting->data.mux.group) {
  789. found = true;
  790. break;
  791. }
  792. }
  793. if (!found)
  794. pinmux_disable_setting(setting);
  795. }
  796. }
  797. p->state = NULL;
  798. /* Apply all the settings for the new state */
  799. list_for_each_entry(setting, &state->settings, node) {
  800. switch (setting->type) {
  801. case PIN_MAP_TYPE_MUX_GROUP:
  802. ret = pinmux_enable_setting(setting);
  803. break;
  804. case PIN_MAP_TYPE_CONFIGS_PIN:
  805. case PIN_MAP_TYPE_CONFIGS_GROUP:
  806. ret = pinconf_apply_setting(setting);
  807. break;
  808. default:
  809. ret = -EINVAL;
  810. break;
  811. }
  812. if (ret < 0) {
  813. goto unapply_new_state;
  814. }
  815. }
  816. p->state = state;
  817. return 0;
  818. unapply_new_state:
  819. pr_info("Error applying setting, reverse things back\n");
  820. /*
  821. * If the loop stopped on the 1st entry, nothing has been enabled,
  822. * so jump directly to the 2nd phase
  823. */
  824. if (list_entry(&setting->node, typeof(*setting), node) ==
  825. list_first_entry(&state->settings, typeof(*setting), node))
  826. goto reapply_old_state;
  827. list_for_each_entry(setting2, &state->settings, node) {
  828. if (&setting2->node == &setting->node)
  829. break;
  830. pinctrl_free_setting(true, setting2);
  831. }
  832. reapply_old_state:
  833. if (old_state) {
  834. list_for_each_entry(setting, &old_state->settings, node) {
  835. bool found = false;
  836. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
  837. continue;
  838. list_for_each_entry(setting2, &state->settings, node) {
  839. if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
  840. continue;
  841. if (setting2->data.mux.group ==
  842. setting->data.mux.group) {
  843. found = true;
  844. break;
  845. }
  846. }
  847. if (!found)
  848. pinmux_enable_setting(setting);
  849. }
  850. }
  851. return ret;
  852. }
  853. /**
  854. * pinctrl_select() - select/activate/program a pinctrl state to HW
  855. * @p: the pinctrl handle for the device that requests configuratio
  856. * @state: the state handle to select/activate/program
  857. */
  858. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
  859. {
  860. int ret;
  861. mutex_lock(&pinctrl_mutex);
  862. ret = pinctrl_select_state_locked(p, state);
  863. mutex_unlock(&pinctrl_mutex);
  864. return ret;
  865. }
  866. EXPORT_SYMBOL_GPL(pinctrl_select_state);
  867. static void devm_pinctrl_release(struct device *dev, void *res)
  868. {
  869. pinctrl_put(*(struct pinctrl **)res);
  870. }
  871. /**
  872. * struct devm_pinctrl_get() - Resource managed pinctrl_get()
  873. * @dev: the device to obtain the handle for
  874. *
  875. * If there is a need to explicitly destroy the returned struct pinctrl,
  876. * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  877. */
  878. struct pinctrl *devm_pinctrl_get(struct device *dev)
  879. {
  880. struct pinctrl **ptr, *p;
  881. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
  882. if (!ptr)
  883. return ERR_PTR(-ENOMEM);
  884. p = pinctrl_get(dev);
  885. if (!IS_ERR(p)) {
  886. *ptr = p;
  887. devres_add(dev, ptr);
  888. } else {
  889. devres_free(ptr);
  890. }
  891. return p;
  892. }
  893. EXPORT_SYMBOL_GPL(devm_pinctrl_get);
  894. static int devm_pinctrl_match(struct device *dev, void *res, void *data)
  895. {
  896. struct pinctrl **p = res;
  897. return *p == data;
  898. }
  899. /**
  900. * devm_pinctrl_put() - Resource managed pinctrl_put()
  901. * @p: the pinctrl handle to release
  902. *
  903. * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
  904. * this function will not need to be called and the resource management
  905. * code will ensure that the resource is freed.
  906. */
  907. void devm_pinctrl_put(struct pinctrl *p)
  908. {
  909. WARN_ON(devres_release(p->dev, devm_pinctrl_release,
  910. devm_pinctrl_match, p));
  911. }
  912. EXPORT_SYMBOL_GPL(devm_pinctrl_put);
  913. int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
  914. bool dup, bool locked)
  915. {
  916. int i, ret;
  917. struct pinctrl_maps *maps_node;
  918. pr_debug("add %d pinmux maps\n", num_maps);
  919. /* First sanity check the new mapping */
  920. for (i = 0; i < num_maps; i++) {
  921. if (!maps[i].dev_name) {
  922. pr_err("failed to register map %s (%d): no device given\n",
  923. maps[i].name, i);
  924. return -EINVAL;
  925. }
  926. if (!maps[i].name) {
  927. pr_err("failed to register map %d: no map name given\n",
  928. i);
  929. return -EINVAL;
  930. }
  931. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
  932. !maps[i].ctrl_dev_name) {
  933. pr_err("failed to register map %s (%d): no pin control device given\n",
  934. maps[i].name, i);
  935. return -EINVAL;
  936. }
  937. switch (maps[i].type) {
  938. case PIN_MAP_TYPE_DUMMY_STATE:
  939. break;
  940. case PIN_MAP_TYPE_MUX_GROUP:
  941. ret = pinmux_validate_map(&maps[i], i);
  942. if (ret < 0)
  943. return ret;
  944. break;
  945. case PIN_MAP_TYPE_CONFIGS_PIN:
  946. case PIN_MAP_TYPE_CONFIGS_GROUP:
  947. ret = pinconf_validate_map(&maps[i], i);
  948. if (ret < 0)
  949. return ret;
  950. break;
  951. default:
  952. pr_err("failed to register map %s (%d): invalid type given\n",
  953. maps[i].name, i);
  954. return -EINVAL;
  955. }
  956. }
  957. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  958. if (!maps_node) {
  959. pr_err("failed to alloc struct pinctrl_maps\n");
  960. return -ENOMEM;
  961. }
  962. maps_node->num_maps = num_maps;
  963. if (dup) {
  964. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
  965. GFP_KERNEL);
  966. if (!maps_node->maps) {
  967. pr_err("failed to duplicate mapping table\n");
  968. kfree(maps_node);
  969. return -ENOMEM;
  970. }
  971. } else {
  972. maps_node->maps = maps;
  973. }
  974. if (!locked)
  975. mutex_lock(&pinctrl_mutex);
  976. list_add_tail(&maps_node->node, &pinctrl_maps);
  977. if (!locked)
  978. mutex_unlock(&pinctrl_mutex);
  979. return 0;
  980. }
  981. /**
  982. * pinctrl_register_mappings() - register a set of pin controller mappings
  983. * @maps: the pincontrol mappings table to register. This should probably be
  984. * marked with __initdata so it can be discarded after boot. This
  985. * function will perform a shallow copy for the mapping entries.
  986. * @num_maps: the number of maps in the mapping table
  987. */
  988. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  989. unsigned num_maps)
  990. {
  991. return pinctrl_register_map(maps, num_maps, true, false);
  992. }
  993. void pinctrl_unregister_map(struct pinctrl_map const *map)
  994. {
  995. struct pinctrl_maps *maps_node;
  996. list_for_each_entry(maps_node, &pinctrl_maps, node) {
  997. if (maps_node->maps == map) {
  998. list_del(&maps_node->node);
  999. return;
  1000. }
  1001. }
  1002. }
  1003. /**
  1004. * pinctrl_force_sleep() - turn a given controller device into sleep state
  1005. * @pctldev: pin controller device
  1006. */
  1007. int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
  1008. {
  1009. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
  1010. return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
  1011. return 0;
  1012. }
  1013. EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
  1014. /**
  1015. * pinctrl_force_default() - turn a given controller device into default state
  1016. * @pctldev: pin controller device
  1017. */
  1018. int pinctrl_force_default(struct pinctrl_dev *pctldev)
  1019. {
  1020. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
  1021. return pinctrl_select_state(pctldev->p, pctldev->hog_default);
  1022. return 0;
  1023. }
  1024. EXPORT_SYMBOL_GPL(pinctrl_force_default);
  1025. #ifdef CONFIG_DEBUG_FS
  1026. static int pinctrl_pins_show(struct seq_file *s, void *what)
  1027. {
  1028. struct pinctrl_dev *pctldev = s->private;
  1029. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1030. unsigned i, pin;
  1031. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  1032. mutex_lock(&pinctrl_mutex);
  1033. /* The pin number can be retrived from the pin controller descriptor */
  1034. for (i = 0; i < pctldev->desc->npins; i++) {
  1035. struct pin_desc *desc;
  1036. pin = pctldev->desc->pins[i].number;
  1037. desc = pin_desc_get(pctldev, pin);
  1038. /* Pin space may be sparse */
  1039. if (desc == NULL)
  1040. continue;
  1041. seq_printf(s, "pin %d (%s) ", pin,
  1042. desc->name ? desc->name : "unnamed");
  1043. /* Driver-specific info per pin */
  1044. if (ops->pin_dbg_show)
  1045. ops->pin_dbg_show(pctldev, s, pin);
  1046. seq_puts(s, "\n");
  1047. }
  1048. mutex_unlock(&pinctrl_mutex);
  1049. return 0;
  1050. }
  1051. static int pinctrl_groups_show(struct seq_file *s, void *what)
  1052. {
  1053. struct pinctrl_dev *pctldev = s->private;
  1054. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1055. unsigned ngroups, selector = 0;
  1056. ngroups = ops->get_groups_count(pctldev);
  1057. mutex_lock(&pinctrl_mutex);
  1058. seq_puts(s, "registered pin groups:\n");
  1059. while (selector < ngroups) {
  1060. const unsigned *pins;
  1061. unsigned num_pins;
  1062. const char *gname = ops->get_group_name(pctldev, selector);
  1063. const char *pname;
  1064. int ret;
  1065. int i;
  1066. ret = ops->get_group_pins(pctldev, selector,
  1067. &pins, &num_pins);
  1068. if (ret)
  1069. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  1070. gname);
  1071. else {
  1072. seq_printf(s, "group: %s\n", gname);
  1073. for (i = 0; i < num_pins; i++) {
  1074. pname = pin_get_name(pctldev, pins[i]);
  1075. if (WARN_ON(!pname)) {
  1076. mutex_unlock(&pinctrl_mutex);
  1077. return -EINVAL;
  1078. }
  1079. seq_printf(s, "pin %d (%s)\n", pins[i], pname);
  1080. }
  1081. seq_puts(s, "\n");
  1082. }
  1083. selector++;
  1084. }
  1085. mutex_unlock(&pinctrl_mutex);
  1086. return 0;
  1087. }
  1088. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  1089. {
  1090. struct pinctrl_dev *pctldev = s->private;
  1091. struct pinctrl_gpio_range *range = NULL;
  1092. seq_puts(s, "GPIO ranges handled:\n");
  1093. mutex_lock(&pinctrl_mutex);
  1094. /* Loop over the ranges */
  1095. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  1096. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  1097. range->id, range->name,
  1098. range->base, (range->base + range->npins - 1),
  1099. range->pin_base,
  1100. (range->pin_base + range->npins - 1));
  1101. }
  1102. mutex_unlock(&pinctrl_mutex);
  1103. return 0;
  1104. }
  1105. static int pinctrl_devices_show(struct seq_file *s, void *what)
  1106. {
  1107. struct pinctrl_dev *pctldev;
  1108. seq_puts(s, "name [pinmux] [pinconf]\n");
  1109. mutex_lock(&pinctrl_mutex);
  1110. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  1111. seq_printf(s, "%s ", pctldev->desc->name);
  1112. if (pctldev->desc->pmxops)
  1113. seq_puts(s, "yes ");
  1114. else
  1115. seq_puts(s, "no ");
  1116. if (pctldev->desc->confops)
  1117. seq_puts(s, "yes");
  1118. else
  1119. seq_puts(s, "no");
  1120. seq_puts(s, "\n");
  1121. }
  1122. mutex_unlock(&pinctrl_mutex);
  1123. return 0;
  1124. }
  1125. static inline const char *map_type(enum pinctrl_map_type type)
  1126. {
  1127. static const char * const names[] = {
  1128. "INVALID",
  1129. "DUMMY_STATE",
  1130. "MUX_GROUP",
  1131. "CONFIGS_PIN",
  1132. "CONFIGS_GROUP",
  1133. };
  1134. if (type >= ARRAY_SIZE(names))
  1135. return "UNKNOWN";
  1136. return names[type];
  1137. }
  1138. static int pinctrl_maps_show(struct seq_file *s, void *what)
  1139. {
  1140. struct pinctrl_maps *maps_node;
  1141. int i;
  1142. struct pinctrl_map const *map;
  1143. seq_puts(s, "Pinctrl maps:\n");
  1144. mutex_lock(&pinctrl_mutex);
  1145. for_each_maps(maps_node, i, map) {
  1146. seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
  1147. map->dev_name, map->name, map_type(map->type),
  1148. map->type);
  1149. if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
  1150. seq_printf(s, "controlling device %s\n",
  1151. map->ctrl_dev_name);
  1152. switch (map->type) {
  1153. case PIN_MAP_TYPE_MUX_GROUP:
  1154. pinmux_show_map(s, map);
  1155. break;
  1156. case PIN_MAP_TYPE_CONFIGS_PIN:
  1157. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1158. pinconf_show_map(s, map);
  1159. break;
  1160. default:
  1161. break;
  1162. }
  1163. seq_printf(s, "\n");
  1164. }
  1165. mutex_unlock(&pinctrl_mutex);
  1166. return 0;
  1167. }
  1168. static int pinctrl_show(struct seq_file *s, void *what)
  1169. {
  1170. struct pinctrl *p;
  1171. struct pinctrl_state *state;
  1172. struct pinctrl_setting *setting;
  1173. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  1174. mutex_lock(&pinctrl_mutex);
  1175. list_for_each_entry(p, &pinctrl_list, node) {
  1176. seq_printf(s, "device: %s current state: %s\n",
  1177. dev_name(p->dev),
  1178. p->state ? p->state->name : "none");
  1179. list_for_each_entry(state, &p->states, node) {
  1180. seq_printf(s, " state: %s\n", state->name);
  1181. list_for_each_entry(setting, &state->settings, node) {
  1182. struct pinctrl_dev *pctldev = setting->pctldev;
  1183. seq_printf(s, " type: %s controller %s ",
  1184. map_type(setting->type),
  1185. pinctrl_dev_get_name(pctldev));
  1186. switch (setting->type) {
  1187. case PIN_MAP_TYPE_MUX_GROUP:
  1188. pinmux_show_setting(s, setting);
  1189. break;
  1190. case PIN_MAP_TYPE_CONFIGS_PIN:
  1191. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1192. pinconf_show_setting(s, setting);
  1193. break;
  1194. default:
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. }
  1200. mutex_unlock(&pinctrl_mutex);
  1201. return 0;
  1202. }
  1203. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  1204. {
  1205. return single_open(file, pinctrl_pins_show, inode->i_private);
  1206. }
  1207. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  1208. {
  1209. return single_open(file, pinctrl_groups_show, inode->i_private);
  1210. }
  1211. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  1212. {
  1213. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  1214. }
  1215. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  1216. {
  1217. return single_open(file, pinctrl_devices_show, NULL);
  1218. }
  1219. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  1220. {
  1221. return single_open(file, pinctrl_maps_show, NULL);
  1222. }
  1223. static int pinctrl_open(struct inode *inode, struct file *file)
  1224. {
  1225. return single_open(file, pinctrl_show, NULL);
  1226. }
  1227. static const struct file_operations pinctrl_pins_ops = {
  1228. .open = pinctrl_pins_open,
  1229. .read = seq_read,
  1230. .llseek = seq_lseek,
  1231. .release = single_release,
  1232. };
  1233. static const struct file_operations pinctrl_groups_ops = {
  1234. .open = pinctrl_groups_open,
  1235. .read = seq_read,
  1236. .llseek = seq_lseek,
  1237. .release = single_release,
  1238. };
  1239. static const struct file_operations pinctrl_gpioranges_ops = {
  1240. .open = pinctrl_gpioranges_open,
  1241. .read = seq_read,
  1242. .llseek = seq_lseek,
  1243. .release = single_release,
  1244. };
  1245. static const struct file_operations pinctrl_devices_ops = {
  1246. .open = pinctrl_devices_open,
  1247. .read = seq_read,
  1248. .llseek = seq_lseek,
  1249. .release = single_release,
  1250. };
  1251. static const struct file_operations pinctrl_maps_ops = {
  1252. .open = pinctrl_maps_open,
  1253. .read = seq_read,
  1254. .llseek = seq_lseek,
  1255. .release = single_release,
  1256. };
  1257. static const struct file_operations pinctrl_ops = {
  1258. .open = pinctrl_open,
  1259. .read = seq_read,
  1260. .llseek = seq_lseek,
  1261. .release = single_release,
  1262. };
  1263. static struct dentry *debugfs_root;
  1264. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1265. {
  1266. struct dentry *device_root;
  1267. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  1268. debugfs_root);
  1269. pctldev->device_root = device_root;
  1270. if (IS_ERR(device_root) || !device_root) {
  1271. pr_warn("failed to create debugfs directory for %s\n",
  1272. dev_name(pctldev->dev));
  1273. return;
  1274. }
  1275. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  1276. device_root, pctldev, &pinctrl_pins_ops);
  1277. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  1278. device_root, pctldev, &pinctrl_groups_ops);
  1279. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  1280. device_root, pctldev, &pinctrl_gpioranges_ops);
  1281. pinmux_init_device_debugfs(device_root, pctldev);
  1282. pinconf_init_device_debugfs(device_root, pctldev);
  1283. }
  1284. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1285. {
  1286. debugfs_remove_recursive(pctldev->device_root);
  1287. }
  1288. static void pinctrl_init_debugfs(void)
  1289. {
  1290. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  1291. if (IS_ERR(debugfs_root) || !debugfs_root) {
  1292. pr_warn("failed to create debugfs directory\n");
  1293. debugfs_root = NULL;
  1294. return;
  1295. }
  1296. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  1297. debugfs_root, NULL, &pinctrl_devices_ops);
  1298. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  1299. debugfs_root, NULL, &pinctrl_maps_ops);
  1300. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  1301. debugfs_root, NULL, &pinctrl_ops);
  1302. }
  1303. #else /* CONFIG_DEBUG_FS */
  1304. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1305. {
  1306. }
  1307. static void pinctrl_init_debugfs(void)
  1308. {
  1309. }
  1310. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1311. {
  1312. }
  1313. #endif
  1314. static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
  1315. {
  1316. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1317. if (!ops ||
  1318. !ops->get_groups_count ||
  1319. !ops->get_group_name ||
  1320. !ops->get_group_pins)
  1321. return -EINVAL;
  1322. if (ops->dt_node_to_map && !ops->dt_free_map)
  1323. return -EINVAL;
  1324. return 0;
  1325. }
  1326. /**
  1327. * pinctrl_register() - register a pin controller device
  1328. * @pctldesc: descriptor for this pin controller
  1329. * @dev: parent device for this pin controller
  1330. * @driver_data: private pin controller data for this pin controller
  1331. */
  1332. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  1333. struct device *dev, void *driver_data)
  1334. {
  1335. struct pinctrl_dev *pctldev;
  1336. int ret;
  1337. if (!pctldesc)
  1338. return NULL;
  1339. if (!pctldesc->name)
  1340. return NULL;
  1341. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  1342. if (pctldev == NULL) {
  1343. dev_err(dev, "failed to alloc struct pinctrl_dev\n");
  1344. return NULL;
  1345. }
  1346. /* Initialize pin control device struct */
  1347. pctldev->owner = pctldesc->owner;
  1348. pctldev->desc = pctldesc;
  1349. pctldev->driver_data = driver_data;
  1350. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1351. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1352. pctldev->dev = dev;
  1353. /* check core ops for sanity */
  1354. if (pinctrl_check_ops(pctldev)) {
  1355. dev_err(dev, "pinctrl ops lacks necessary functions\n");
  1356. goto out_err;
  1357. }
  1358. /* If we're implementing pinmuxing, check the ops for sanity */
  1359. if (pctldesc->pmxops) {
  1360. if (pinmux_check_ops(pctldev))
  1361. goto out_err;
  1362. }
  1363. /* If we're implementing pinconfig, check the ops for sanity */
  1364. if (pctldesc->confops) {
  1365. if (pinconf_check_ops(pctldev))
  1366. goto out_err;
  1367. }
  1368. /* Register all the pins */
  1369. dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
  1370. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1371. if (ret) {
  1372. dev_err(dev, "error during pin registration\n");
  1373. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1374. pctldesc->npins);
  1375. goto out_err;
  1376. }
  1377. mutex_lock(&pinctrl_mutex);
  1378. list_add_tail(&pctldev->node, &pinctrldev_list);
  1379. pctldev->p = pinctrl_get_locked(pctldev->dev);
  1380. if (!IS_ERR(pctldev->p)) {
  1381. pctldev->hog_default =
  1382. pinctrl_lookup_state_locked(pctldev->p,
  1383. PINCTRL_STATE_DEFAULT);
  1384. if (IS_ERR(pctldev->hog_default)) {
  1385. dev_dbg(dev, "failed to lookup the default state\n");
  1386. } else {
  1387. if (pinctrl_select_state_locked(pctldev->p,
  1388. pctldev->hog_default))
  1389. dev_err(dev,
  1390. "failed to select default state\n");
  1391. }
  1392. pctldev->hog_sleep =
  1393. pinctrl_lookup_state_locked(pctldev->p,
  1394. PINCTRL_STATE_SLEEP);
  1395. if (IS_ERR(pctldev->hog_sleep))
  1396. dev_dbg(dev, "failed to lookup the sleep state\n");
  1397. }
  1398. mutex_unlock(&pinctrl_mutex);
  1399. pinctrl_init_device_debugfs(pctldev);
  1400. return pctldev;
  1401. out_err:
  1402. kfree(pctldev);
  1403. return NULL;
  1404. }
  1405. EXPORT_SYMBOL_GPL(pinctrl_register);
  1406. /**
  1407. * pinctrl_unregister() - unregister pinmux
  1408. * @pctldev: pin controller to unregister
  1409. *
  1410. * Called by pinmux drivers to unregister a pinmux.
  1411. */
  1412. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1413. {
  1414. struct pinctrl_gpio_range *range, *n;
  1415. if (pctldev == NULL)
  1416. return;
  1417. pinctrl_remove_device_debugfs(pctldev);
  1418. mutex_lock(&pinctrl_mutex);
  1419. if (!IS_ERR(pctldev->p))
  1420. pinctrl_put_locked(pctldev->p, true);
  1421. /* TODO: check that no pinmuxes are still active? */
  1422. list_del(&pctldev->node);
  1423. /* Destroy descriptor tree */
  1424. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1425. pctldev->desc->npins);
  1426. /* remove gpio ranges map */
  1427. list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
  1428. list_del(&range->node);
  1429. kfree(pctldev);
  1430. mutex_unlock(&pinctrl_mutex);
  1431. }
  1432. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1433. static int __init pinctrl_init(void)
  1434. {
  1435. pr_info("initialized pinctrl subsystem\n");
  1436. pinctrl_init_debugfs();
  1437. return 0;
  1438. }
  1439. /* init early since many drivers really need to initialized pinmux early */
  1440. core_initcall(pinctrl_init);