core.c 39 KB

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