core.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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 *find_pinctrl_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. if (!pctldev)
  304. return NULL;
  305. pinctrl_add_gpio_range(pctldev, range);
  306. return pctldev;
  307. }
  308. EXPORT_SYMBOL_GPL(find_pinctrl_and_add_gpio_range);
  309. /**
  310. * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
  311. * @pctldev: pin controller device to remove the range from
  312. * @range: the GPIO range to remove
  313. */
  314. void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
  315. struct pinctrl_gpio_range *range)
  316. {
  317. mutex_lock(&pinctrl_mutex);
  318. list_del(&range->node);
  319. mutex_unlock(&pinctrl_mutex);
  320. }
  321. EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
  322. /**
  323. * pinctrl_get_group_selector() - returns the group selector for a group
  324. * @pctldev: the pin controller handling the group
  325. * @pin_group: the pin group to look up
  326. */
  327. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  328. const char *pin_group)
  329. {
  330. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  331. unsigned ngroups = pctlops->get_groups_count(pctldev);
  332. unsigned group_selector = 0;
  333. while (group_selector < ngroups) {
  334. const char *gname = pctlops->get_group_name(pctldev,
  335. group_selector);
  336. if (!strcmp(gname, pin_group)) {
  337. dev_dbg(pctldev->dev,
  338. "found group selector %u for %s\n",
  339. group_selector,
  340. pin_group);
  341. return group_selector;
  342. }
  343. group_selector++;
  344. }
  345. dev_err(pctldev->dev, "does not have pin group %s\n",
  346. pin_group);
  347. return -EINVAL;
  348. }
  349. /**
  350. * pinctrl_request_gpio() - request a single pin to be used in as GPIO
  351. * @gpio: the GPIO pin number from the GPIO subsystem number space
  352. *
  353. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  354. * as part of their gpio_request() semantics, platforms and individual drivers
  355. * shall *NOT* request GPIO pins to be muxed in.
  356. */
  357. int pinctrl_request_gpio(unsigned gpio)
  358. {
  359. struct pinctrl_dev *pctldev;
  360. struct pinctrl_gpio_range *range;
  361. int ret;
  362. int pin;
  363. mutex_lock(&pinctrl_mutex);
  364. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  365. if (ret) {
  366. mutex_unlock(&pinctrl_mutex);
  367. return ret;
  368. }
  369. /* Convert to the pin controllers number space */
  370. pin = gpio - range->base + range->pin_base;
  371. ret = pinmux_request_gpio(pctldev, range, pin, gpio);
  372. mutex_unlock(&pinctrl_mutex);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
  376. /**
  377. * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
  378. * @gpio: the GPIO pin number from the GPIO subsystem number space
  379. *
  380. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  381. * as part of their gpio_free() semantics, platforms and individual drivers
  382. * shall *NOT* request GPIO pins to be muxed out.
  383. */
  384. void pinctrl_free_gpio(unsigned gpio)
  385. {
  386. struct pinctrl_dev *pctldev;
  387. struct pinctrl_gpio_range *range;
  388. int ret;
  389. int pin;
  390. mutex_lock(&pinctrl_mutex);
  391. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  392. if (ret) {
  393. mutex_unlock(&pinctrl_mutex);
  394. return;
  395. }
  396. /* Convert to the pin controllers number space */
  397. pin = gpio - range->base + range->pin_base;
  398. pinmux_free_gpio(pctldev, pin, range);
  399. mutex_unlock(&pinctrl_mutex);
  400. }
  401. EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
  402. static int pinctrl_gpio_direction(unsigned gpio, bool input)
  403. {
  404. struct pinctrl_dev *pctldev;
  405. struct pinctrl_gpio_range *range;
  406. int ret;
  407. int pin;
  408. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  409. if (ret)
  410. return ret;
  411. /* Convert to the pin controllers number space */
  412. pin = gpio - range->base + range->pin_base;
  413. return pinmux_gpio_direction(pctldev, range, pin, input);
  414. }
  415. /**
  416. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  417. * @gpio: the GPIO pin number from the GPIO subsystem number space
  418. *
  419. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  420. * as part of their gpio_direction_input() semantics, platforms and individual
  421. * drivers shall *NOT* touch pin control GPIO calls.
  422. */
  423. int pinctrl_gpio_direction_input(unsigned gpio)
  424. {
  425. int ret;
  426. mutex_lock(&pinctrl_mutex);
  427. ret = pinctrl_gpio_direction(gpio, true);
  428. mutex_unlock(&pinctrl_mutex);
  429. return ret;
  430. }
  431. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  432. /**
  433. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  434. * @gpio: the GPIO pin number from the GPIO subsystem number space
  435. *
  436. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  437. * as part of their gpio_direction_output() semantics, platforms and individual
  438. * drivers shall *NOT* touch pin control GPIO calls.
  439. */
  440. int pinctrl_gpio_direction_output(unsigned gpio)
  441. {
  442. int ret;
  443. mutex_lock(&pinctrl_mutex);
  444. ret = pinctrl_gpio_direction(gpio, false);
  445. mutex_unlock(&pinctrl_mutex);
  446. return ret;
  447. }
  448. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  449. static struct pinctrl_state *find_state(struct pinctrl *p,
  450. const char *name)
  451. {
  452. struct pinctrl_state *state;
  453. list_for_each_entry(state, &p->states, node)
  454. if (!strcmp(state->name, name))
  455. return state;
  456. return NULL;
  457. }
  458. static struct pinctrl_state *create_state(struct pinctrl *p,
  459. const char *name)
  460. {
  461. struct pinctrl_state *state;
  462. state = kzalloc(sizeof(*state), GFP_KERNEL);
  463. if (state == NULL) {
  464. dev_err(p->dev,
  465. "failed to alloc struct pinctrl_state\n");
  466. return ERR_PTR(-ENOMEM);
  467. }
  468. state->name = name;
  469. INIT_LIST_HEAD(&state->settings);
  470. list_add_tail(&state->node, &p->states);
  471. return state;
  472. }
  473. static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
  474. {
  475. struct pinctrl_state *state;
  476. struct pinctrl_setting *setting;
  477. int ret;
  478. state = find_state(p, map->name);
  479. if (!state)
  480. state = create_state(p, map->name);
  481. if (IS_ERR(state))
  482. return PTR_ERR(state);
  483. if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
  484. return 0;
  485. setting = kzalloc(sizeof(*setting), GFP_KERNEL);
  486. if (setting == NULL) {
  487. dev_err(p->dev,
  488. "failed to alloc struct pinctrl_setting\n");
  489. return -ENOMEM;
  490. }
  491. setting->type = map->type;
  492. setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  493. if (setting->pctldev == NULL) {
  494. dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
  495. map->ctrl_dev_name);
  496. kfree(setting);
  497. /*
  498. * OK let us guess that the driver is not there yet, and
  499. * let's defer obtaining this pinctrl handle to later...
  500. */
  501. return -EPROBE_DEFER;
  502. }
  503. setting->dev_name = map->dev_name;
  504. switch (map->type) {
  505. case PIN_MAP_TYPE_MUX_GROUP:
  506. ret = pinmux_map_to_setting(map, setting);
  507. break;
  508. case PIN_MAP_TYPE_CONFIGS_PIN:
  509. case PIN_MAP_TYPE_CONFIGS_GROUP:
  510. ret = pinconf_map_to_setting(map, setting);
  511. break;
  512. default:
  513. ret = -EINVAL;
  514. break;
  515. }
  516. if (ret < 0) {
  517. kfree(setting);
  518. return ret;
  519. }
  520. list_add_tail(&setting->node, &state->settings);
  521. return 0;
  522. }
  523. static struct pinctrl *find_pinctrl(struct device *dev)
  524. {
  525. struct pinctrl *p;
  526. list_for_each_entry(p, &pinctrl_list, node)
  527. if (p->dev == dev)
  528. return p;
  529. return NULL;
  530. }
  531. static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
  532. static struct pinctrl *create_pinctrl(struct device *dev)
  533. {
  534. struct pinctrl *p;
  535. const char *devname;
  536. struct pinctrl_maps *maps_node;
  537. int i;
  538. struct pinctrl_map const *map;
  539. int ret;
  540. /*
  541. * create the state cookie holder struct pinctrl for each
  542. * mapping, this is what consumers will get when requesting
  543. * a pin control handle with pinctrl_get()
  544. */
  545. p = kzalloc(sizeof(*p), GFP_KERNEL);
  546. if (p == NULL) {
  547. dev_err(dev, "failed to alloc struct pinctrl\n");
  548. return ERR_PTR(-ENOMEM);
  549. }
  550. p->dev = dev;
  551. INIT_LIST_HEAD(&p->states);
  552. INIT_LIST_HEAD(&p->dt_maps);
  553. ret = pinctrl_dt_to_map(p);
  554. if (ret < 0) {
  555. kfree(p);
  556. return ERR_PTR(ret);
  557. }
  558. devname = dev_name(dev);
  559. /* Iterate over the pin control maps to locate the right ones */
  560. for_each_maps(maps_node, i, map) {
  561. /* Map must be for this device */
  562. if (strcmp(map->dev_name, devname))
  563. continue;
  564. ret = add_setting(p, map);
  565. if (ret < 0) {
  566. pinctrl_put_locked(p, false);
  567. return ERR_PTR(ret);
  568. }
  569. }
  570. /* Add the pinmux to the global list */
  571. list_add_tail(&p->node, &pinctrl_list);
  572. return p;
  573. }
  574. static struct pinctrl *pinctrl_get_locked(struct device *dev)
  575. {
  576. struct pinctrl *p;
  577. if (WARN_ON(!dev))
  578. return ERR_PTR(-EINVAL);
  579. p = find_pinctrl(dev);
  580. if (p != NULL)
  581. return ERR_PTR(-EBUSY);
  582. return create_pinctrl(dev);
  583. }
  584. /**
  585. * pinctrl_get() - retrieves the pinctrl handle for a device
  586. * @dev: the device to obtain the handle for
  587. */
  588. struct pinctrl *pinctrl_get(struct device *dev)
  589. {
  590. struct pinctrl *p;
  591. mutex_lock(&pinctrl_mutex);
  592. p = pinctrl_get_locked(dev);
  593. mutex_unlock(&pinctrl_mutex);
  594. return p;
  595. }
  596. EXPORT_SYMBOL_GPL(pinctrl_get);
  597. static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
  598. {
  599. struct pinctrl_state *state, *n1;
  600. struct pinctrl_setting *setting, *n2;
  601. list_for_each_entry_safe(state, n1, &p->states, node) {
  602. list_for_each_entry_safe(setting, n2, &state->settings, node) {
  603. switch (setting->type) {
  604. case PIN_MAP_TYPE_MUX_GROUP:
  605. if (state == p->state)
  606. pinmux_disable_setting(setting);
  607. pinmux_free_setting(setting);
  608. break;
  609. case PIN_MAP_TYPE_CONFIGS_PIN:
  610. case PIN_MAP_TYPE_CONFIGS_GROUP:
  611. pinconf_free_setting(setting);
  612. break;
  613. default:
  614. break;
  615. }
  616. list_del(&setting->node);
  617. kfree(setting);
  618. }
  619. list_del(&state->node);
  620. kfree(state);
  621. }
  622. pinctrl_dt_free_maps(p);
  623. if (inlist)
  624. list_del(&p->node);
  625. kfree(p);
  626. }
  627. /**
  628. * pinctrl_put() - release a previously claimed pinctrl handle
  629. * @p: the pinctrl handle to release
  630. */
  631. void pinctrl_put(struct pinctrl *p)
  632. {
  633. mutex_lock(&pinctrl_mutex);
  634. pinctrl_put_locked(p, true);
  635. mutex_unlock(&pinctrl_mutex);
  636. }
  637. EXPORT_SYMBOL_GPL(pinctrl_put);
  638. static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
  639. const char *name)
  640. {
  641. struct pinctrl_state *state;
  642. state = find_state(p, name);
  643. if (!state) {
  644. if (pinctrl_dummy_state) {
  645. /* create dummy state */
  646. dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
  647. name);
  648. state = create_state(p, name);
  649. } else
  650. state = ERR_PTR(-ENODEV);
  651. }
  652. return state;
  653. }
  654. /**
  655. * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
  656. * @p: the pinctrl handle to retrieve the state from
  657. * @name: the state name to retrieve
  658. */
  659. struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
  660. {
  661. struct pinctrl_state *s;
  662. mutex_lock(&pinctrl_mutex);
  663. s = pinctrl_lookup_state_locked(p, name);
  664. mutex_unlock(&pinctrl_mutex);
  665. return s;
  666. }
  667. EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
  668. static int pinctrl_select_state_locked(struct pinctrl *p,
  669. struct pinctrl_state *state)
  670. {
  671. struct pinctrl_setting *setting, *setting2;
  672. int ret;
  673. if (p->state == state)
  674. return 0;
  675. if (p->state) {
  676. /*
  677. * The set of groups with a mux configuration in the old state
  678. * may not be identical to the set of groups with a mux setting
  679. * in the new state. While this might be unusual, it's entirely
  680. * possible for the "user"-supplied mapping table to be written
  681. * that way. For each group that was configured in the old state
  682. * but not in the new state, this code puts that group into a
  683. * safe/disabled state.
  684. */
  685. list_for_each_entry(setting, &p->state->settings, node) {
  686. bool found = false;
  687. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
  688. continue;
  689. list_for_each_entry(setting2, &state->settings, node) {
  690. if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
  691. continue;
  692. if (setting2->data.mux.group ==
  693. setting->data.mux.group) {
  694. found = true;
  695. break;
  696. }
  697. }
  698. if (!found)
  699. pinmux_disable_setting(setting);
  700. }
  701. }
  702. p->state = state;
  703. /* Apply all the settings for the new state */
  704. list_for_each_entry(setting, &state->settings, node) {
  705. switch (setting->type) {
  706. case PIN_MAP_TYPE_MUX_GROUP:
  707. ret = pinmux_enable_setting(setting);
  708. break;
  709. case PIN_MAP_TYPE_CONFIGS_PIN:
  710. case PIN_MAP_TYPE_CONFIGS_GROUP:
  711. ret = pinconf_apply_setting(setting);
  712. break;
  713. default:
  714. ret = -EINVAL;
  715. break;
  716. }
  717. if (ret < 0) {
  718. /* FIXME: Difficult to return to prev state */
  719. return ret;
  720. }
  721. }
  722. return 0;
  723. }
  724. /**
  725. * pinctrl_select() - select/activate/program a pinctrl state to HW
  726. * @p: the pinctrl handle for the device that requests configuratio
  727. * @state: the state handle to select/activate/program
  728. */
  729. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
  730. {
  731. int ret;
  732. mutex_lock(&pinctrl_mutex);
  733. ret = pinctrl_select_state_locked(p, state);
  734. mutex_unlock(&pinctrl_mutex);
  735. return ret;
  736. }
  737. EXPORT_SYMBOL_GPL(pinctrl_select_state);
  738. static void devm_pinctrl_release(struct device *dev, void *res)
  739. {
  740. pinctrl_put(*(struct pinctrl **)res);
  741. }
  742. /**
  743. * struct devm_pinctrl_get() - Resource managed pinctrl_get()
  744. * @dev: the device to obtain the handle for
  745. *
  746. * If there is a need to explicitly destroy the returned struct pinctrl,
  747. * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  748. */
  749. struct pinctrl *devm_pinctrl_get(struct device *dev)
  750. {
  751. struct pinctrl **ptr, *p;
  752. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
  753. if (!ptr)
  754. return ERR_PTR(-ENOMEM);
  755. p = pinctrl_get(dev);
  756. if (!IS_ERR(p)) {
  757. *ptr = p;
  758. devres_add(dev, ptr);
  759. } else {
  760. devres_free(ptr);
  761. }
  762. return p;
  763. }
  764. EXPORT_SYMBOL_GPL(devm_pinctrl_get);
  765. static int devm_pinctrl_match(struct device *dev, void *res, void *data)
  766. {
  767. struct pinctrl **p = res;
  768. return *p == data;
  769. }
  770. /**
  771. * devm_pinctrl_put() - Resource managed pinctrl_put()
  772. * @p: the pinctrl handle to release
  773. *
  774. * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
  775. * this function will not need to be called and the resource management
  776. * code will ensure that the resource is freed.
  777. */
  778. void devm_pinctrl_put(struct pinctrl *p)
  779. {
  780. WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
  781. devm_pinctrl_match, p));
  782. pinctrl_put(p);
  783. }
  784. EXPORT_SYMBOL_GPL(devm_pinctrl_put);
  785. int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
  786. bool dup, bool locked)
  787. {
  788. int i, ret;
  789. struct pinctrl_maps *maps_node;
  790. pr_debug("add %d pinmux maps\n", num_maps);
  791. /* First sanity check the new mapping */
  792. for (i = 0; i < num_maps; i++) {
  793. if (!maps[i].dev_name) {
  794. pr_err("failed to register map %s (%d): no device given\n",
  795. maps[i].name, i);
  796. return -EINVAL;
  797. }
  798. if (!maps[i].name) {
  799. pr_err("failed to register map %d: no map name given\n",
  800. i);
  801. return -EINVAL;
  802. }
  803. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
  804. !maps[i].ctrl_dev_name) {
  805. pr_err("failed to register map %s (%d): no pin control device given\n",
  806. maps[i].name, i);
  807. return -EINVAL;
  808. }
  809. switch (maps[i].type) {
  810. case PIN_MAP_TYPE_DUMMY_STATE:
  811. break;
  812. case PIN_MAP_TYPE_MUX_GROUP:
  813. ret = pinmux_validate_map(&maps[i], i);
  814. if (ret < 0)
  815. return ret;
  816. break;
  817. case PIN_MAP_TYPE_CONFIGS_PIN:
  818. case PIN_MAP_TYPE_CONFIGS_GROUP:
  819. ret = pinconf_validate_map(&maps[i], i);
  820. if (ret < 0)
  821. return ret;
  822. break;
  823. default:
  824. pr_err("failed to register map %s (%d): invalid type given\n",
  825. maps[i].name, i);
  826. return -EINVAL;
  827. }
  828. }
  829. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  830. if (!maps_node) {
  831. pr_err("failed to alloc struct pinctrl_maps\n");
  832. return -ENOMEM;
  833. }
  834. maps_node->num_maps = num_maps;
  835. if (dup) {
  836. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
  837. GFP_KERNEL);
  838. if (!maps_node->maps) {
  839. pr_err("failed to duplicate mapping table\n");
  840. kfree(maps_node);
  841. return -ENOMEM;
  842. }
  843. } else {
  844. maps_node->maps = maps;
  845. }
  846. if (!locked)
  847. mutex_lock(&pinctrl_mutex);
  848. list_add_tail(&maps_node->node, &pinctrl_maps);
  849. if (!locked)
  850. mutex_unlock(&pinctrl_mutex);
  851. return 0;
  852. }
  853. /**
  854. * pinctrl_register_mappings() - register a set of pin controller mappings
  855. * @maps: the pincontrol mappings table to register. This should probably be
  856. * marked with __initdata so it can be discarded after boot. This
  857. * function will perform a shallow copy for the mapping entries.
  858. * @num_maps: the number of maps in the mapping table
  859. */
  860. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  861. unsigned num_maps)
  862. {
  863. return pinctrl_register_map(maps, num_maps, true, false);
  864. }
  865. void pinctrl_unregister_map(struct pinctrl_map const *map)
  866. {
  867. struct pinctrl_maps *maps_node;
  868. list_for_each_entry(maps_node, &pinctrl_maps, node) {
  869. if (maps_node->maps == map) {
  870. list_del(&maps_node->node);
  871. return;
  872. }
  873. }
  874. }
  875. #ifdef CONFIG_DEBUG_FS
  876. static int pinctrl_pins_show(struct seq_file *s, void *what)
  877. {
  878. struct pinctrl_dev *pctldev = s->private;
  879. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  880. unsigned i, pin;
  881. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  882. mutex_lock(&pinctrl_mutex);
  883. /* The pin number can be retrived from the pin controller descriptor */
  884. for (i = 0; i < pctldev->desc->npins; i++) {
  885. struct pin_desc *desc;
  886. pin = pctldev->desc->pins[i].number;
  887. desc = pin_desc_get(pctldev, pin);
  888. /* Pin space may be sparse */
  889. if (desc == NULL)
  890. continue;
  891. seq_printf(s, "pin %d (%s) ", pin,
  892. desc->name ? desc->name : "unnamed");
  893. /* Driver-specific info per pin */
  894. if (ops->pin_dbg_show)
  895. ops->pin_dbg_show(pctldev, s, pin);
  896. seq_puts(s, "\n");
  897. }
  898. mutex_unlock(&pinctrl_mutex);
  899. return 0;
  900. }
  901. static int pinctrl_groups_show(struct seq_file *s, void *what)
  902. {
  903. struct pinctrl_dev *pctldev = s->private;
  904. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  905. unsigned ngroups, selector = 0;
  906. ngroups = ops->get_groups_count(pctldev);
  907. mutex_lock(&pinctrl_mutex);
  908. seq_puts(s, "registered pin groups:\n");
  909. while (selector < ngroups) {
  910. const unsigned *pins;
  911. unsigned num_pins;
  912. const char *gname = ops->get_group_name(pctldev, selector);
  913. const char *pname;
  914. int ret;
  915. int i;
  916. ret = ops->get_group_pins(pctldev, selector,
  917. &pins, &num_pins);
  918. if (ret)
  919. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  920. gname);
  921. else {
  922. seq_printf(s, "group: %s\n", gname);
  923. for (i = 0; i < num_pins; i++) {
  924. pname = pin_get_name(pctldev, pins[i]);
  925. if (WARN_ON(!pname)) {
  926. mutex_unlock(&pinctrl_mutex);
  927. return -EINVAL;
  928. }
  929. seq_printf(s, "pin %d (%s)\n", pins[i], pname);
  930. }
  931. seq_puts(s, "\n");
  932. }
  933. selector++;
  934. }
  935. mutex_unlock(&pinctrl_mutex);
  936. return 0;
  937. }
  938. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  939. {
  940. struct pinctrl_dev *pctldev = s->private;
  941. struct pinctrl_gpio_range *range = NULL;
  942. seq_puts(s, "GPIO ranges handled:\n");
  943. mutex_lock(&pinctrl_mutex);
  944. /* Loop over the ranges */
  945. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  946. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  947. range->id, range->name,
  948. range->base, (range->base + range->npins - 1),
  949. range->pin_base,
  950. (range->pin_base + range->npins - 1));
  951. }
  952. mutex_unlock(&pinctrl_mutex);
  953. return 0;
  954. }
  955. static int pinctrl_devices_show(struct seq_file *s, void *what)
  956. {
  957. struct pinctrl_dev *pctldev;
  958. seq_puts(s, "name [pinmux] [pinconf]\n");
  959. mutex_lock(&pinctrl_mutex);
  960. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  961. seq_printf(s, "%s ", pctldev->desc->name);
  962. if (pctldev->desc->pmxops)
  963. seq_puts(s, "yes ");
  964. else
  965. seq_puts(s, "no ");
  966. if (pctldev->desc->confops)
  967. seq_puts(s, "yes");
  968. else
  969. seq_puts(s, "no");
  970. seq_puts(s, "\n");
  971. }
  972. mutex_unlock(&pinctrl_mutex);
  973. return 0;
  974. }
  975. static inline const char *map_type(enum pinctrl_map_type type)
  976. {
  977. static const char * const names[] = {
  978. "INVALID",
  979. "DUMMY_STATE",
  980. "MUX_GROUP",
  981. "CONFIGS_PIN",
  982. "CONFIGS_GROUP",
  983. };
  984. if (type >= ARRAY_SIZE(names))
  985. return "UNKNOWN";
  986. return names[type];
  987. }
  988. static int pinctrl_maps_show(struct seq_file *s, void *what)
  989. {
  990. struct pinctrl_maps *maps_node;
  991. int i;
  992. struct pinctrl_map const *map;
  993. seq_puts(s, "Pinctrl maps:\n");
  994. mutex_lock(&pinctrl_mutex);
  995. for_each_maps(maps_node, i, map) {
  996. seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
  997. map->dev_name, map->name, map_type(map->type),
  998. map->type);
  999. if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
  1000. seq_printf(s, "controlling device %s\n",
  1001. map->ctrl_dev_name);
  1002. switch (map->type) {
  1003. case PIN_MAP_TYPE_MUX_GROUP:
  1004. pinmux_show_map(s, map);
  1005. break;
  1006. case PIN_MAP_TYPE_CONFIGS_PIN:
  1007. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1008. pinconf_show_map(s, map);
  1009. break;
  1010. default:
  1011. break;
  1012. }
  1013. seq_printf(s, "\n");
  1014. }
  1015. mutex_unlock(&pinctrl_mutex);
  1016. return 0;
  1017. }
  1018. static int pinctrl_show(struct seq_file *s, void *what)
  1019. {
  1020. struct pinctrl *p;
  1021. struct pinctrl_state *state;
  1022. struct pinctrl_setting *setting;
  1023. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  1024. mutex_lock(&pinctrl_mutex);
  1025. list_for_each_entry(p, &pinctrl_list, node) {
  1026. seq_printf(s, "device: %s current state: %s\n",
  1027. dev_name(p->dev),
  1028. p->state ? p->state->name : "none");
  1029. list_for_each_entry(state, &p->states, node) {
  1030. seq_printf(s, " state: %s\n", state->name);
  1031. list_for_each_entry(setting, &state->settings, node) {
  1032. struct pinctrl_dev *pctldev = setting->pctldev;
  1033. seq_printf(s, " type: %s controller %s ",
  1034. map_type(setting->type),
  1035. pinctrl_dev_get_name(pctldev));
  1036. switch (setting->type) {
  1037. case PIN_MAP_TYPE_MUX_GROUP:
  1038. pinmux_show_setting(s, setting);
  1039. break;
  1040. case PIN_MAP_TYPE_CONFIGS_PIN:
  1041. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1042. pinconf_show_setting(s, setting);
  1043. break;
  1044. default:
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. mutex_unlock(&pinctrl_mutex);
  1051. return 0;
  1052. }
  1053. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  1054. {
  1055. return single_open(file, pinctrl_pins_show, inode->i_private);
  1056. }
  1057. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  1058. {
  1059. return single_open(file, pinctrl_groups_show, inode->i_private);
  1060. }
  1061. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  1062. {
  1063. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  1064. }
  1065. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  1066. {
  1067. return single_open(file, pinctrl_devices_show, NULL);
  1068. }
  1069. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  1070. {
  1071. return single_open(file, pinctrl_maps_show, NULL);
  1072. }
  1073. static int pinctrl_open(struct inode *inode, struct file *file)
  1074. {
  1075. return single_open(file, pinctrl_show, NULL);
  1076. }
  1077. static const struct file_operations pinctrl_pins_ops = {
  1078. .open = pinctrl_pins_open,
  1079. .read = seq_read,
  1080. .llseek = seq_lseek,
  1081. .release = single_release,
  1082. };
  1083. static const struct file_operations pinctrl_groups_ops = {
  1084. .open = pinctrl_groups_open,
  1085. .read = seq_read,
  1086. .llseek = seq_lseek,
  1087. .release = single_release,
  1088. };
  1089. static const struct file_operations pinctrl_gpioranges_ops = {
  1090. .open = pinctrl_gpioranges_open,
  1091. .read = seq_read,
  1092. .llseek = seq_lseek,
  1093. .release = single_release,
  1094. };
  1095. static const struct file_operations pinctrl_devices_ops = {
  1096. .open = pinctrl_devices_open,
  1097. .read = seq_read,
  1098. .llseek = seq_lseek,
  1099. .release = single_release,
  1100. };
  1101. static const struct file_operations pinctrl_maps_ops = {
  1102. .open = pinctrl_maps_open,
  1103. .read = seq_read,
  1104. .llseek = seq_lseek,
  1105. .release = single_release,
  1106. };
  1107. static const struct file_operations pinctrl_ops = {
  1108. .open = pinctrl_open,
  1109. .read = seq_read,
  1110. .llseek = seq_lseek,
  1111. .release = single_release,
  1112. };
  1113. static struct dentry *debugfs_root;
  1114. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1115. {
  1116. struct dentry *device_root;
  1117. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  1118. debugfs_root);
  1119. pctldev->device_root = device_root;
  1120. if (IS_ERR(device_root) || !device_root) {
  1121. pr_warn("failed to create debugfs directory for %s\n",
  1122. dev_name(pctldev->dev));
  1123. return;
  1124. }
  1125. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  1126. device_root, pctldev, &pinctrl_pins_ops);
  1127. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  1128. device_root, pctldev, &pinctrl_groups_ops);
  1129. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  1130. device_root, pctldev, &pinctrl_gpioranges_ops);
  1131. pinmux_init_device_debugfs(device_root, pctldev);
  1132. pinconf_init_device_debugfs(device_root, pctldev);
  1133. }
  1134. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1135. {
  1136. debugfs_remove_recursive(pctldev->device_root);
  1137. }
  1138. static void pinctrl_init_debugfs(void)
  1139. {
  1140. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  1141. if (IS_ERR(debugfs_root) || !debugfs_root) {
  1142. pr_warn("failed to create debugfs directory\n");
  1143. debugfs_root = NULL;
  1144. return;
  1145. }
  1146. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  1147. debugfs_root, NULL, &pinctrl_devices_ops);
  1148. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  1149. debugfs_root, NULL, &pinctrl_maps_ops);
  1150. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  1151. debugfs_root, NULL, &pinctrl_ops);
  1152. }
  1153. #else /* CONFIG_DEBUG_FS */
  1154. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1155. {
  1156. }
  1157. static void pinctrl_init_debugfs(void)
  1158. {
  1159. }
  1160. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1161. {
  1162. }
  1163. #endif
  1164. static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
  1165. {
  1166. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1167. if (!ops ||
  1168. !ops->get_groups_count ||
  1169. !ops->get_group_name ||
  1170. !ops->get_group_pins)
  1171. return -EINVAL;
  1172. if (ops->dt_node_to_map && !ops->dt_free_map)
  1173. return -EINVAL;
  1174. return 0;
  1175. }
  1176. /**
  1177. * pinctrl_register() - register a pin controller device
  1178. * @pctldesc: descriptor for this pin controller
  1179. * @dev: parent device for this pin controller
  1180. * @driver_data: private pin controller data for this pin controller
  1181. */
  1182. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  1183. struct device *dev, void *driver_data)
  1184. {
  1185. struct pinctrl_dev *pctldev;
  1186. int ret;
  1187. if (!pctldesc)
  1188. return NULL;
  1189. if (!pctldesc->name)
  1190. return NULL;
  1191. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  1192. if (pctldev == NULL) {
  1193. dev_err(dev, "failed to alloc struct pinctrl_dev\n");
  1194. return NULL;
  1195. }
  1196. /* Initialize pin control device struct */
  1197. pctldev->owner = pctldesc->owner;
  1198. pctldev->desc = pctldesc;
  1199. pctldev->driver_data = driver_data;
  1200. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1201. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1202. pctldev->dev = dev;
  1203. /* check core ops for sanity */
  1204. if (pinctrl_check_ops(pctldev)) {
  1205. dev_err(dev, "pinctrl ops lacks necessary functions\n");
  1206. goto out_err;
  1207. }
  1208. /* If we're implementing pinmuxing, check the ops for sanity */
  1209. if (pctldesc->pmxops) {
  1210. if (pinmux_check_ops(pctldev))
  1211. goto out_err;
  1212. }
  1213. /* If we're implementing pinconfig, check the ops for sanity */
  1214. if (pctldesc->confops) {
  1215. if (pinconf_check_ops(pctldev))
  1216. goto out_err;
  1217. }
  1218. /* Register all the pins */
  1219. dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
  1220. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1221. if (ret) {
  1222. dev_err(dev, "error during pin registration\n");
  1223. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1224. pctldesc->npins);
  1225. goto out_err;
  1226. }
  1227. mutex_lock(&pinctrl_mutex);
  1228. list_add_tail(&pctldev->node, &pinctrldev_list);
  1229. pctldev->p = pinctrl_get_locked(pctldev->dev);
  1230. if (!IS_ERR(pctldev->p)) {
  1231. struct pinctrl_state *s =
  1232. pinctrl_lookup_state_locked(pctldev->p,
  1233. PINCTRL_STATE_DEFAULT);
  1234. if (IS_ERR(s)) {
  1235. dev_dbg(dev, "failed to lookup the default state\n");
  1236. } else {
  1237. if (pinctrl_select_state_locked(pctldev->p, s))
  1238. dev_err(dev,
  1239. "failed to select default state\n");
  1240. }
  1241. }
  1242. mutex_unlock(&pinctrl_mutex);
  1243. pinctrl_init_device_debugfs(pctldev);
  1244. return pctldev;
  1245. out_err:
  1246. kfree(pctldev);
  1247. return NULL;
  1248. }
  1249. EXPORT_SYMBOL_GPL(pinctrl_register);
  1250. /**
  1251. * pinctrl_unregister() - unregister pinmux
  1252. * @pctldev: pin controller to unregister
  1253. *
  1254. * Called by pinmux drivers to unregister a pinmux.
  1255. */
  1256. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1257. {
  1258. struct pinctrl_gpio_range *range, *n;
  1259. if (pctldev == NULL)
  1260. return;
  1261. pinctrl_remove_device_debugfs(pctldev);
  1262. mutex_lock(&pinctrl_mutex);
  1263. if (!IS_ERR(pctldev->p))
  1264. pinctrl_put_locked(pctldev->p, true);
  1265. /* TODO: check that no pinmuxes are still active? */
  1266. list_del(&pctldev->node);
  1267. /* Destroy descriptor tree */
  1268. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1269. pctldev->desc->npins);
  1270. /* remove gpio ranges map */
  1271. list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
  1272. list_del(&range->node);
  1273. kfree(pctldev);
  1274. mutex_unlock(&pinctrl_mutex);
  1275. }
  1276. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1277. static int __init pinctrl_init(void)
  1278. {
  1279. pr_info("initialized pinctrl subsystem\n");
  1280. pinctrl_init_debugfs();
  1281. return 0;
  1282. }
  1283. /* init early since many drivers really need to initialized pinmux early */
  1284. core_initcall(pinctrl_init);