core.c 36 KB

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