core.c 36 KB

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