core.c 35 KB

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