core.c 35 KB

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