core.c 39 KB

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