core.c 36 KB

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