core.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Core driver for the pin control subsystem
  4. *
  5. * Copyright (C) 2011-2012 ST-Ericsson SA
  6. * Written on behalf of Linaro for ST-Ericsson
  7. * Based on bits of regulator core, gpio core and clk core
  8. *
  9. * Author: Linus Walleij <[email protected]>
  10. *
  11. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  12. */
  13. #define pr_fmt(fmt) "pinctrl core: " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/kref.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/debugfs.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/pinctrl/pinctrl.h>
  26. #include <linux/pinctrl/machine.h>
  27. #ifdef CONFIG_GPIOLIB
  28. #include "../gpio/gpiolib.h"
  29. #include <asm-generic/gpio.h>
  30. #endif
  31. #include "core.h"
  32. #include "devicetree.h"
  33. #include "pinmux.h"
  34. #include "pinconf.h"
  35. static bool pinctrl_dummy_state;
  36. /* Mutex taken to protect pinctrl_list */
  37. static DEFINE_MUTEX(pinctrl_list_mutex);
  38. /* Mutex taken to protect pinctrl_maps */
  39. DEFINE_MUTEX(pinctrl_maps_mutex);
  40. /* Mutex taken to protect pinctrldev_list */
  41. static DEFINE_MUTEX(pinctrldev_list_mutex);
  42. /* Global list of pin control devices (struct pinctrl_dev) */
  43. static LIST_HEAD(pinctrldev_list);
  44. /* List of pin controller handles (struct pinctrl) */
  45. static LIST_HEAD(pinctrl_list);
  46. /* List of pinctrl maps (struct pinctrl_maps) */
  47. LIST_HEAD(pinctrl_maps);
  48. /**
  49. * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
  50. *
  51. * Usually this function is called by platforms without pinctrl driver support
  52. * but run with some shared drivers using pinctrl APIs.
  53. * After calling this function, the pinctrl core will return successfully
  54. * with creating a dummy state for the driver to keep going smoothly.
  55. */
  56. void pinctrl_provide_dummies(void)
  57. {
  58. pinctrl_dummy_state = true;
  59. }
  60. const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
  61. {
  62. /* We're not allowed to register devices without name */
  63. return pctldev->desc->name;
  64. }
  65. EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
  66. const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
  67. {
  68. return dev_name(pctldev->dev);
  69. }
  70. EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
  71. void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
  72. {
  73. return pctldev->driver_data;
  74. }
  75. EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
  76. /**
  77. * get_pinctrl_dev_from_devname() - look up pin controller device
  78. * @devname: the name of a device instance, as returned by dev_name()
  79. *
  80. * Looks up a pin control device matching a certain device name or pure device
  81. * pointer, the pure device pointer will take precedence.
  82. */
  83. struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
  84. {
  85. struct pinctrl_dev *pctldev;
  86. if (!devname)
  87. return NULL;
  88. mutex_lock(&pinctrldev_list_mutex);
  89. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  90. if (!strcmp(dev_name(pctldev->dev), devname)) {
  91. /* Matched on device name */
  92. mutex_unlock(&pinctrldev_list_mutex);
  93. return pctldev;
  94. }
  95. }
  96. mutex_unlock(&pinctrldev_list_mutex);
  97. return NULL;
  98. }
  99. struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
  100. {
  101. struct pinctrl_dev *pctldev;
  102. mutex_lock(&pinctrldev_list_mutex);
  103. list_for_each_entry(pctldev, &pinctrldev_list, node)
  104. if (device_match_of_node(pctldev->dev, np)) {
  105. mutex_unlock(&pinctrldev_list_mutex);
  106. return pctldev;
  107. }
  108. mutex_unlock(&pinctrldev_list_mutex);
  109. return NULL;
  110. }
  111. /**
  112. * pin_get_from_name() - look up a pin number from a name
  113. * @pctldev: the pin control device to lookup the pin on
  114. * @name: the name of the pin to look up
  115. */
  116. int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
  117. {
  118. unsigned i, pin;
  119. /* The pin number can be retrived from the pin controller descriptor */
  120. for (i = 0; i < pctldev->desc->npins; i++) {
  121. struct pin_desc *desc;
  122. pin = pctldev->desc->pins[i].number;
  123. desc = pin_desc_get(pctldev, pin);
  124. /* Pin space may be sparse */
  125. if (desc && !strcmp(name, desc->name))
  126. return pin;
  127. }
  128. return -EINVAL;
  129. }
  130. /**
  131. * pin_get_name() - look up a pin name from a pin id
  132. * @pctldev: the pin control device to lookup the pin on
  133. * @pin: pin number/id to look up
  134. */
  135. const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
  136. {
  137. const struct pin_desc *desc;
  138. desc = pin_desc_get(pctldev, pin);
  139. if (!desc) {
  140. dev_err(pctldev->dev, "failed to get pin(%d) name\n",
  141. pin);
  142. return NULL;
  143. }
  144. return desc->name;
  145. }
  146. EXPORT_SYMBOL_GPL(pin_get_name);
  147. /* Deletes a range of pin descriptors */
  148. static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
  149. const struct pinctrl_pin_desc *pins,
  150. unsigned num_pins)
  151. {
  152. int i;
  153. for (i = 0; i < num_pins; i++) {
  154. struct pin_desc *pindesc;
  155. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
  156. pins[i].number);
  157. if (pindesc) {
  158. radix_tree_delete(&pctldev->pin_desc_tree,
  159. pins[i].number);
  160. if (pindesc->dynamic_name)
  161. kfree(pindesc->name);
  162. }
  163. kfree(pindesc);
  164. }
  165. }
  166. static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
  167. const struct pinctrl_pin_desc *pin)
  168. {
  169. struct pin_desc *pindesc;
  170. pindesc = pin_desc_get(pctldev, pin->number);
  171. if (pindesc) {
  172. dev_err(pctldev->dev, "pin %d already registered\n",
  173. pin->number);
  174. return -EINVAL;
  175. }
  176. pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
  177. if (!pindesc)
  178. return -ENOMEM;
  179. /* Set owner */
  180. pindesc->pctldev = pctldev;
  181. /* Copy basic pin info */
  182. if (pin->name) {
  183. pindesc->name = pin->name;
  184. } else {
  185. pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
  186. if (!pindesc->name) {
  187. kfree(pindesc);
  188. return -ENOMEM;
  189. }
  190. pindesc->dynamic_name = true;
  191. }
  192. pindesc->drv_data = pin->drv_data;
  193. radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
  194. pr_debug("registered pin %d (%s) on %s\n",
  195. pin->number, pindesc->name, pctldev->desc->name);
  196. return 0;
  197. }
  198. static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
  199. const struct pinctrl_pin_desc *pins,
  200. unsigned num_descs)
  201. {
  202. unsigned i;
  203. int ret = 0;
  204. for (i = 0; i < num_descs; i++) {
  205. ret = pinctrl_register_one_pin(pctldev, &pins[i]);
  206. if (ret)
  207. return ret;
  208. }
  209. return 0;
  210. }
  211. /**
  212. * gpio_to_pin() - GPIO range GPIO number to pin number translation
  213. * @range: GPIO range used for the translation
  214. * @gpio: gpio pin to translate to a pin number
  215. *
  216. * Finds the pin number for a given GPIO using the specified GPIO range
  217. * as a base for translation. The distinction between linear GPIO ranges
  218. * and pin list based GPIO ranges is managed correctly by this function.
  219. *
  220. * This function assumes the gpio is part of the specified GPIO range, use
  221. * only after making sure this is the case (e.g. by calling it on the
  222. * result of successful pinctrl_get_device_gpio_range calls)!
  223. */
  224. static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
  225. unsigned int gpio)
  226. {
  227. unsigned int offset = gpio - range->base;
  228. if (range->pins)
  229. return range->pins[offset];
  230. else
  231. return range->pin_base + offset;
  232. }
  233. /**
  234. * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
  235. * @pctldev: pin controller device to check
  236. * @gpio: gpio pin to check taken from the global GPIO pin space
  237. *
  238. * Tries to match a GPIO pin number to the ranges handled by a certain pin
  239. * controller, return the range or NULL
  240. */
  241. static struct pinctrl_gpio_range *
  242. pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
  243. {
  244. struct pinctrl_gpio_range *range;
  245. mutex_lock(&pctldev->mutex);
  246. /* Loop over the ranges */
  247. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  248. /* Check if we're in the valid range */
  249. if (gpio >= range->base &&
  250. gpio < range->base + range->npins) {
  251. mutex_unlock(&pctldev->mutex);
  252. return range;
  253. }
  254. }
  255. mutex_unlock(&pctldev->mutex);
  256. return NULL;
  257. }
  258. /**
  259. * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
  260. * the same GPIO chip are in range
  261. * @gpio: gpio pin to check taken from the global GPIO pin space
  262. *
  263. * This function is complement of pinctrl_match_gpio_range(). If the return
  264. * value of pinctrl_match_gpio_range() is NULL, this function could be used
  265. * to check whether pinctrl device is ready or not. Maybe some GPIO pins
  266. * of the same GPIO chip don't have back-end pinctrl interface.
  267. * If the return value is true, it means that pinctrl device is ready & the
  268. * certain GPIO pin doesn't have back-end pinctrl device. If the return value
  269. * is false, it means that pinctrl device may not be ready.
  270. */
  271. #ifdef CONFIG_GPIOLIB
  272. static bool pinctrl_ready_for_gpio_range(unsigned gpio)
  273. {
  274. struct pinctrl_dev *pctldev;
  275. struct pinctrl_gpio_range *range = NULL;
  276. struct gpio_chip *chip = gpio_to_chip(gpio);
  277. if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
  278. return false;
  279. mutex_lock(&pinctrldev_list_mutex);
  280. /* Loop over the pin controllers */
  281. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  282. /* Loop over the ranges */
  283. mutex_lock(&pctldev->mutex);
  284. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  285. /* Check if any gpio range overlapped with gpio chip */
  286. if (range->base + range->npins - 1 < chip->base ||
  287. range->base > chip->base + chip->ngpio - 1)
  288. continue;
  289. mutex_unlock(&pctldev->mutex);
  290. mutex_unlock(&pinctrldev_list_mutex);
  291. return true;
  292. }
  293. mutex_unlock(&pctldev->mutex);
  294. }
  295. mutex_unlock(&pinctrldev_list_mutex);
  296. return false;
  297. }
  298. #else
  299. static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
  300. #endif
  301. /**
  302. * pinctrl_get_device_gpio_range() - find device for GPIO range
  303. * @gpio: the pin to locate the pin controller for
  304. * @outdev: the pin control device if found
  305. * @outrange: the GPIO range if found
  306. *
  307. * Find the pin controller handling a certain GPIO pin from the pinspace of
  308. * the GPIO subsystem, return the device and the matching GPIO range. Returns
  309. * -EPROBE_DEFER if the GPIO range could not be found in any device since it
  310. * may still have not been registered.
  311. */
  312. static int pinctrl_get_device_gpio_range(unsigned gpio,
  313. struct pinctrl_dev **outdev,
  314. struct pinctrl_gpio_range **outrange)
  315. {
  316. struct pinctrl_dev *pctldev;
  317. mutex_lock(&pinctrldev_list_mutex);
  318. /* Loop over the pin controllers */
  319. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  320. struct pinctrl_gpio_range *range;
  321. range = pinctrl_match_gpio_range(pctldev, gpio);
  322. if (range) {
  323. *outdev = pctldev;
  324. *outrange = range;
  325. mutex_unlock(&pinctrldev_list_mutex);
  326. return 0;
  327. }
  328. }
  329. mutex_unlock(&pinctrldev_list_mutex);
  330. return -EPROBE_DEFER;
  331. }
  332. /**
  333. * pinctrl_add_gpio_range() - register a GPIO range for a controller
  334. * @pctldev: pin controller device to add the range to
  335. * @range: the GPIO range to add
  336. *
  337. * This adds a range of GPIOs to be handled by a certain pin controller. Call
  338. * this to register handled ranges after registering your pin controller.
  339. */
  340. void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
  341. struct pinctrl_gpio_range *range)
  342. {
  343. mutex_lock(&pctldev->mutex);
  344. list_add_tail(&range->node, &pctldev->gpio_ranges);
  345. mutex_unlock(&pctldev->mutex);
  346. }
  347. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
  348. void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
  349. struct pinctrl_gpio_range *ranges,
  350. unsigned nranges)
  351. {
  352. int i;
  353. for (i = 0; i < nranges; i++)
  354. pinctrl_add_gpio_range(pctldev, &ranges[i]);
  355. }
  356. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
  357. struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
  358. struct pinctrl_gpio_range *range)
  359. {
  360. struct pinctrl_dev *pctldev;
  361. pctldev = get_pinctrl_dev_from_devname(devname);
  362. /*
  363. * If we can't find this device, let's assume that is because
  364. * it has not probed yet, so the driver trying to register this
  365. * range need to defer probing.
  366. */
  367. if (!pctldev) {
  368. return ERR_PTR(-EPROBE_DEFER);
  369. }
  370. pinctrl_add_gpio_range(pctldev, range);
  371. return pctldev;
  372. }
  373. EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
  374. int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
  375. const unsigned **pins, unsigned *num_pins)
  376. {
  377. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  378. int gs;
  379. if (!pctlops->get_group_pins)
  380. return -EINVAL;
  381. gs = pinctrl_get_group_selector(pctldev, pin_group);
  382. if (gs < 0)
  383. return gs;
  384. return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
  385. }
  386. EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
  387. struct pinctrl_gpio_range *
  388. pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
  389. unsigned int pin)
  390. {
  391. struct pinctrl_gpio_range *range;
  392. /* Loop over the ranges */
  393. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  394. /* Check if we're in the valid range */
  395. if (range->pins) {
  396. int a;
  397. for (a = 0; a < range->npins; a++) {
  398. if (range->pins[a] == pin)
  399. return range;
  400. }
  401. } else if (pin >= range->pin_base &&
  402. pin < range->pin_base + range->npins)
  403. return range;
  404. }
  405. return NULL;
  406. }
  407. EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
  408. /**
  409. * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
  410. * @pctldev: the pin controller device to look in
  411. * @pin: a controller-local number to find the range for
  412. */
  413. struct pinctrl_gpio_range *
  414. pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
  415. unsigned int pin)
  416. {
  417. struct pinctrl_gpio_range *range;
  418. mutex_lock(&pctldev->mutex);
  419. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
  420. mutex_unlock(&pctldev->mutex);
  421. return range;
  422. }
  423. EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
  424. /**
  425. * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
  426. * @pctldev: pin controller device to remove the range from
  427. * @range: the GPIO range to remove
  428. */
  429. void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
  430. struct pinctrl_gpio_range *range)
  431. {
  432. mutex_lock(&pctldev->mutex);
  433. list_del(&range->node);
  434. mutex_unlock(&pctldev->mutex);
  435. }
  436. EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
  437. #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
  438. /**
  439. * pinctrl_generic_get_group_count() - returns the number of pin groups
  440. * @pctldev: pin controller device
  441. */
  442. int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
  443. {
  444. return pctldev->num_groups;
  445. }
  446. EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
  447. /**
  448. * pinctrl_generic_get_group_name() - returns the name of a pin group
  449. * @pctldev: pin controller device
  450. * @selector: group number
  451. */
  452. const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
  453. unsigned int selector)
  454. {
  455. struct group_desc *group;
  456. group = radix_tree_lookup(&pctldev->pin_group_tree,
  457. selector);
  458. if (!group)
  459. return NULL;
  460. return group->name;
  461. }
  462. EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
  463. /**
  464. * pinctrl_generic_get_group_pins() - gets the pin group pins
  465. * @pctldev: pin controller device
  466. * @selector: group number
  467. * @pins: pins in the group
  468. * @num_pins: number of pins in the group
  469. */
  470. int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
  471. unsigned int selector,
  472. const unsigned int **pins,
  473. unsigned int *num_pins)
  474. {
  475. struct group_desc *group;
  476. group = radix_tree_lookup(&pctldev->pin_group_tree,
  477. selector);
  478. if (!group) {
  479. dev_err(pctldev->dev, "%s could not find pingroup%i\n",
  480. __func__, selector);
  481. return -EINVAL;
  482. }
  483. *pins = group->pins;
  484. *num_pins = group->num_pins;
  485. return 0;
  486. }
  487. EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
  488. /**
  489. * pinctrl_generic_get_group() - returns a pin group based on the number
  490. * @pctldev: pin controller device
  491. * @selector: group number
  492. */
  493. struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
  494. unsigned int selector)
  495. {
  496. struct group_desc *group;
  497. group = radix_tree_lookup(&pctldev->pin_group_tree,
  498. selector);
  499. if (!group)
  500. return NULL;
  501. return group;
  502. }
  503. EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
  504. static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
  505. const char *function)
  506. {
  507. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  508. int ngroups = ops->get_groups_count(pctldev);
  509. int selector = 0;
  510. /* See if this pctldev has this group */
  511. while (selector < ngroups) {
  512. const char *gname = ops->get_group_name(pctldev, selector);
  513. if (gname && !strcmp(function, gname))
  514. return selector;
  515. selector++;
  516. }
  517. return -EINVAL;
  518. }
  519. /**
  520. * pinctrl_generic_add_group() - adds a new pin group
  521. * @pctldev: pin controller device
  522. * @name: name of the pin group
  523. * @pins: pins in the pin group
  524. * @num_pins: number of pins in the pin group
  525. * @data: pin controller driver specific data
  526. *
  527. * Note that the caller must take care of locking.
  528. */
  529. int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
  530. int *pins, int num_pins, void *data)
  531. {
  532. struct group_desc *group;
  533. int selector;
  534. if (!name)
  535. return -EINVAL;
  536. selector = pinctrl_generic_group_name_to_selector(pctldev, name);
  537. if (selector >= 0)
  538. return selector;
  539. selector = pctldev->num_groups;
  540. group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
  541. if (!group)
  542. return -ENOMEM;
  543. group->name = name;
  544. group->pins = pins;
  545. group->num_pins = num_pins;
  546. group->data = data;
  547. radix_tree_insert(&pctldev->pin_group_tree, selector, group);
  548. pctldev->num_groups++;
  549. return selector;
  550. }
  551. EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
  552. /**
  553. * pinctrl_generic_remove_group() - removes a numbered pin group
  554. * @pctldev: pin controller device
  555. * @selector: group number
  556. *
  557. * Note that the caller must take care of locking.
  558. */
  559. int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
  560. unsigned int selector)
  561. {
  562. struct group_desc *group;
  563. group = radix_tree_lookup(&pctldev->pin_group_tree,
  564. selector);
  565. if (!group)
  566. return -ENOENT;
  567. radix_tree_delete(&pctldev->pin_group_tree, selector);
  568. devm_kfree(pctldev->dev, group);
  569. pctldev->num_groups--;
  570. return 0;
  571. }
  572. EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
  573. /**
  574. * pinctrl_generic_free_groups() - removes all pin groups
  575. * @pctldev: pin controller device
  576. *
  577. * Note that the caller must take care of locking. The pinctrl groups
  578. * are allocated with devm_kzalloc() so no need to free them here.
  579. */
  580. static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
  581. {
  582. struct radix_tree_iter iter;
  583. void __rcu **slot;
  584. radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
  585. radix_tree_delete(&pctldev->pin_group_tree, iter.index);
  586. pctldev->num_groups = 0;
  587. }
  588. #else
  589. static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
  590. {
  591. }
  592. #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
  593. /**
  594. * pinctrl_get_group_selector() - returns the group selector for a group
  595. * @pctldev: the pin controller handling the group
  596. * @pin_group: the pin group to look up
  597. */
  598. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  599. const char *pin_group)
  600. {
  601. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  602. unsigned ngroups = pctlops->get_groups_count(pctldev);
  603. unsigned group_selector = 0;
  604. while (group_selector < ngroups) {
  605. const char *gname = pctlops->get_group_name(pctldev,
  606. group_selector);
  607. if (gname && !strcmp(gname, pin_group)) {
  608. dev_dbg(pctldev->dev,
  609. "found group selector %u for %s\n",
  610. group_selector,
  611. pin_group);
  612. return group_selector;
  613. }
  614. group_selector++;
  615. }
  616. dev_err(pctldev->dev, "does not have pin group %s\n",
  617. pin_group);
  618. return -EINVAL;
  619. }
  620. bool pinctrl_gpio_can_use_line(unsigned gpio)
  621. {
  622. struct pinctrl_dev *pctldev;
  623. struct pinctrl_gpio_range *range;
  624. bool result;
  625. int pin;
  626. /*
  627. * Try to obtain GPIO range, if it fails
  628. * we're probably dealing with GPIO driver
  629. * without a backing pin controller - bail out.
  630. */
  631. if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
  632. return true;
  633. mutex_lock(&pctldev->mutex);
  634. /* Convert to the pin controllers number space */
  635. pin = gpio_to_pin(range, gpio);
  636. result = pinmux_can_be_used_for_gpio(pctldev, pin);
  637. mutex_unlock(&pctldev->mutex);
  638. return result;
  639. }
  640. EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
  641. /**
  642. * pinctrl_gpio_request() - request a single pin to be used as GPIO
  643. * @gpio: the GPIO pin number from the GPIO subsystem number space
  644. *
  645. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  646. * as part of their gpio_request() semantics, platforms and individual drivers
  647. * shall *NOT* request GPIO pins to be muxed in.
  648. */
  649. int pinctrl_gpio_request(unsigned gpio)
  650. {
  651. struct pinctrl_dev *pctldev;
  652. struct pinctrl_gpio_range *range;
  653. int ret;
  654. int pin;
  655. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  656. if (ret) {
  657. if (pinctrl_ready_for_gpio_range(gpio))
  658. ret = 0;
  659. return ret;
  660. }
  661. mutex_lock(&pctldev->mutex);
  662. /* Convert to the pin controllers number space */
  663. pin = gpio_to_pin(range, gpio);
  664. ret = pinmux_request_gpio(pctldev, range, pin, gpio);
  665. mutex_unlock(&pctldev->mutex);
  666. return ret;
  667. }
  668. EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
  669. /**
  670. * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
  671. * @gpio: the GPIO pin number from the GPIO subsystem number space
  672. *
  673. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  674. * as part of their gpio_free() semantics, platforms and individual drivers
  675. * shall *NOT* request GPIO pins to be muxed out.
  676. */
  677. void pinctrl_gpio_free(unsigned gpio)
  678. {
  679. struct pinctrl_dev *pctldev;
  680. struct pinctrl_gpio_range *range;
  681. int ret;
  682. int pin;
  683. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  684. if (ret) {
  685. return;
  686. }
  687. mutex_lock(&pctldev->mutex);
  688. /* Convert to the pin controllers number space */
  689. pin = gpio_to_pin(range, gpio);
  690. pinmux_free_gpio(pctldev, pin, range);
  691. mutex_unlock(&pctldev->mutex);
  692. }
  693. EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
  694. static int pinctrl_gpio_direction(unsigned gpio, bool input)
  695. {
  696. struct pinctrl_dev *pctldev;
  697. struct pinctrl_gpio_range *range;
  698. int ret;
  699. int pin;
  700. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  701. if (ret) {
  702. return ret;
  703. }
  704. mutex_lock(&pctldev->mutex);
  705. /* Convert to the pin controllers number space */
  706. pin = gpio_to_pin(range, gpio);
  707. ret = pinmux_gpio_direction(pctldev, range, pin, input);
  708. mutex_unlock(&pctldev->mutex);
  709. return ret;
  710. }
  711. /**
  712. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  713. * @gpio: the GPIO pin number from the GPIO subsystem number space
  714. *
  715. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  716. * as part of their gpio_direction_input() semantics, platforms and individual
  717. * drivers shall *NOT* touch pin control GPIO calls.
  718. */
  719. int pinctrl_gpio_direction_input(unsigned gpio)
  720. {
  721. return pinctrl_gpio_direction(gpio, true);
  722. }
  723. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  724. /**
  725. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  726. * @gpio: the GPIO pin number from the GPIO subsystem number space
  727. *
  728. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  729. * as part of their gpio_direction_output() semantics, platforms and individual
  730. * drivers shall *NOT* touch pin control GPIO calls.
  731. */
  732. int pinctrl_gpio_direction_output(unsigned gpio)
  733. {
  734. return pinctrl_gpio_direction(gpio, false);
  735. }
  736. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  737. /**
  738. * pinctrl_gpio_set_config() - Apply config to given GPIO pin
  739. * @gpio: the GPIO pin number from the GPIO subsystem number space
  740. * @config: the configuration to apply to the GPIO
  741. *
  742. * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
  743. * they need to call the underlying pin controller to change GPIO config
  744. * (for example set debounce time).
  745. */
  746. int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
  747. {
  748. unsigned long configs[] = { config };
  749. struct pinctrl_gpio_range *range;
  750. struct pinctrl_dev *pctldev;
  751. int ret, pin;
  752. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  753. if (ret)
  754. return ret;
  755. mutex_lock(&pctldev->mutex);
  756. pin = gpio_to_pin(range, gpio);
  757. ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
  758. mutex_unlock(&pctldev->mutex);
  759. return ret;
  760. }
  761. EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
  762. static struct pinctrl_state *find_state(struct pinctrl *p,
  763. const char *name)
  764. {
  765. struct pinctrl_state *state;
  766. list_for_each_entry(state, &p->states, node)
  767. if (!strcmp(state->name, name))
  768. return state;
  769. return NULL;
  770. }
  771. static struct pinctrl_state *create_state(struct pinctrl *p,
  772. const char *name)
  773. {
  774. struct pinctrl_state *state;
  775. state = kzalloc(sizeof(*state), GFP_KERNEL);
  776. if (!state)
  777. return ERR_PTR(-ENOMEM);
  778. state->name = name;
  779. INIT_LIST_HEAD(&state->settings);
  780. list_add_tail(&state->node, &p->states);
  781. return state;
  782. }
  783. static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
  784. const struct pinctrl_map *map)
  785. {
  786. struct pinctrl_state *state;
  787. struct pinctrl_setting *setting;
  788. int ret;
  789. state = find_state(p, map->name);
  790. if (!state)
  791. state = create_state(p, map->name);
  792. if (IS_ERR(state))
  793. return PTR_ERR(state);
  794. if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
  795. return 0;
  796. setting = kzalloc(sizeof(*setting), GFP_KERNEL);
  797. if (!setting)
  798. return -ENOMEM;
  799. setting->type = map->type;
  800. if (pctldev)
  801. setting->pctldev = pctldev;
  802. else
  803. setting->pctldev =
  804. get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  805. if (!setting->pctldev) {
  806. kfree(setting);
  807. /* Do not defer probing of hogs (circular loop) */
  808. if (!strcmp(map->ctrl_dev_name, map->dev_name))
  809. return -ENODEV;
  810. /*
  811. * OK let us guess that the driver is not there yet, and
  812. * let's defer obtaining this pinctrl handle to later...
  813. */
  814. dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
  815. map->ctrl_dev_name);
  816. return -EPROBE_DEFER;
  817. }
  818. setting->dev_name = map->dev_name;
  819. switch (map->type) {
  820. case PIN_MAP_TYPE_MUX_GROUP:
  821. ret = pinmux_map_to_setting(map, setting);
  822. break;
  823. case PIN_MAP_TYPE_CONFIGS_PIN:
  824. case PIN_MAP_TYPE_CONFIGS_GROUP:
  825. ret = pinconf_map_to_setting(map, setting);
  826. break;
  827. default:
  828. ret = -EINVAL;
  829. break;
  830. }
  831. if (ret < 0) {
  832. kfree(setting);
  833. return ret;
  834. }
  835. list_add_tail(&setting->node, &state->settings);
  836. return 0;
  837. }
  838. static struct pinctrl *find_pinctrl(struct device *dev)
  839. {
  840. struct pinctrl *p;
  841. mutex_lock(&pinctrl_list_mutex);
  842. list_for_each_entry(p, &pinctrl_list, node)
  843. if (p->dev == dev) {
  844. mutex_unlock(&pinctrl_list_mutex);
  845. return p;
  846. }
  847. mutex_unlock(&pinctrl_list_mutex);
  848. return NULL;
  849. }
  850. static void pinctrl_free(struct pinctrl *p, bool inlist);
  851. static struct pinctrl *create_pinctrl(struct device *dev,
  852. struct pinctrl_dev *pctldev)
  853. {
  854. struct pinctrl *p;
  855. const char *devname;
  856. struct pinctrl_maps *maps_node;
  857. int i;
  858. const struct pinctrl_map *map;
  859. int ret;
  860. /*
  861. * create the state cookie holder struct pinctrl for each
  862. * mapping, this is what consumers will get when requesting
  863. * a pin control handle with pinctrl_get()
  864. */
  865. p = kzalloc(sizeof(*p), GFP_KERNEL);
  866. if (!p)
  867. return ERR_PTR(-ENOMEM);
  868. p->dev = dev;
  869. INIT_LIST_HEAD(&p->states);
  870. INIT_LIST_HEAD(&p->dt_maps);
  871. ret = pinctrl_dt_to_map(p, pctldev);
  872. if (ret < 0) {
  873. kfree(p);
  874. return ERR_PTR(ret);
  875. }
  876. devname = dev_name(dev);
  877. mutex_lock(&pinctrl_maps_mutex);
  878. /* Iterate over the pin control maps to locate the right ones */
  879. for_each_maps(maps_node, i, map) {
  880. /* Map must be for this device */
  881. if (strcmp(map->dev_name, devname))
  882. continue;
  883. /*
  884. * If pctldev is not null, we are claiming hog for it,
  885. * that means, setting that is served by pctldev by itself.
  886. *
  887. * Thus we must skip map that is for this device but is served
  888. * by other device.
  889. */
  890. if (pctldev &&
  891. strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
  892. continue;
  893. ret = add_setting(p, pctldev, map);
  894. /*
  895. * At this point the adding of a setting may:
  896. *
  897. * - Defer, if the pinctrl device is not yet available
  898. * - Fail, if the pinctrl device is not yet available,
  899. * AND the setting is a hog. We cannot defer that, since
  900. * the hog will kick in immediately after the device
  901. * is registered.
  902. *
  903. * If the error returned was not -EPROBE_DEFER then we
  904. * accumulate the errors to see if we end up with
  905. * an -EPROBE_DEFER later, as that is the worst case.
  906. */
  907. if (ret == -EPROBE_DEFER) {
  908. pinctrl_free(p, false);
  909. mutex_unlock(&pinctrl_maps_mutex);
  910. return ERR_PTR(ret);
  911. }
  912. }
  913. mutex_unlock(&pinctrl_maps_mutex);
  914. if (ret < 0) {
  915. /* If some other error than deferral occurred, return here */
  916. pinctrl_free(p, false);
  917. return ERR_PTR(ret);
  918. }
  919. kref_init(&p->users);
  920. /* Add the pinctrl handle to the global list */
  921. mutex_lock(&pinctrl_list_mutex);
  922. list_add_tail(&p->node, &pinctrl_list);
  923. mutex_unlock(&pinctrl_list_mutex);
  924. return p;
  925. }
  926. /**
  927. * pinctrl_get() - retrieves the pinctrl handle for a device
  928. * @dev: the device to obtain the handle for
  929. */
  930. struct pinctrl *pinctrl_get(struct device *dev)
  931. {
  932. struct pinctrl *p;
  933. if (WARN_ON(!dev))
  934. return ERR_PTR(-EINVAL);
  935. /*
  936. * See if somebody else (such as the device core) has already
  937. * obtained a handle to the pinctrl for this device. In that case,
  938. * return another pointer to it.
  939. */
  940. p = find_pinctrl(dev);
  941. if (p) {
  942. dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
  943. kref_get(&p->users);
  944. return p;
  945. }
  946. return create_pinctrl(dev, NULL);
  947. }
  948. EXPORT_SYMBOL_GPL(pinctrl_get);
  949. static void pinctrl_free_setting(bool disable_setting,
  950. struct pinctrl_setting *setting)
  951. {
  952. switch (setting->type) {
  953. case PIN_MAP_TYPE_MUX_GROUP:
  954. if (disable_setting)
  955. pinmux_disable_setting(setting);
  956. pinmux_free_setting(setting);
  957. break;
  958. case PIN_MAP_TYPE_CONFIGS_PIN:
  959. case PIN_MAP_TYPE_CONFIGS_GROUP:
  960. pinconf_free_setting(setting);
  961. break;
  962. default:
  963. break;
  964. }
  965. }
  966. static void pinctrl_free(struct pinctrl *p, bool inlist)
  967. {
  968. struct pinctrl_state *state, *n1;
  969. struct pinctrl_setting *setting, *n2;
  970. mutex_lock(&pinctrl_list_mutex);
  971. list_for_each_entry_safe(state, n1, &p->states, node) {
  972. list_for_each_entry_safe(setting, n2, &state->settings, node) {
  973. pinctrl_free_setting(state == p->state, setting);
  974. list_del(&setting->node);
  975. kfree(setting);
  976. }
  977. list_del(&state->node);
  978. kfree(state);
  979. }
  980. pinctrl_dt_free_maps(p);
  981. if (inlist)
  982. list_del(&p->node);
  983. kfree(p);
  984. mutex_unlock(&pinctrl_list_mutex);
  985. }
  986. /**
  987. * pinctrl_release() - release the pinctrl handle
  988. * @kref: the kref in the pinctrl being released
  989. */
  990. static void pinctrl_release(struct kref *kref)
  991. {
  992. struct pinctrl *p = container_of(kref, struct pinctrl, users);
  993. pinctrl_free(p, true);
  994. }
  995. /**
  996. * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
  997. * @p: the pinctrl handle to release
  998. */
  999. void pinctrl_put(struct pinctrl *p)
  1000. {
  1001. kref_put(&p->users, pinctrl_release);
  1002. }
  1003. EXPORT_SYMBOL_GPL(pinctrl_put);
  1004. /**
  1005. * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
  1006. * @p: the pinctrl handle to retrieve the state from
  1007. * @name: the state name to retrieve
  1008. */
  1009. struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
  1010. const char *name)
  1011. {
  1012. struct pinctrl_state *state;
  1013. state = find_state(p, name);
  1014. if (!state) {
  1015. if (pinctrl_dummy_state) {
  1016. /* create dummy state */
  1017. dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
  1018. name);
  1019. state = create_state(p, name);
  1020. } else
  1021. state = ERR_PTR(-ENODEV);
  1022. }
  1023. return state;
  1024. }
  1025. EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
  1026. static void pinctrl_link_add(struct pinctrl_dev *pctldev,
  1027. struct device *consumer)
  1028. {
  1029. if (pctldev->desc->link_consumers)
  1030. device_link_add(consumer, pctldev->dev,
  1031. DL_FLAG_PM_RUNTIME |
  1032. DL_FLAG_AUTOREMOVE_CONSUMER);
  1033. }
  1034. /**
  1035. * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
  1036. * @p: the pinctrl handle for the device that requests configuration
  1037. * @state: the state handle to select/activate/program
  1038. */
  1039. static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
  1040. {
  1041. struct pinctrl_setting *setting, *setting2;
  1042. struct pinctrl_state *old_state = READ_ONCE(p->state);
  1043. int ret;
  1044. if (old_state) {
  1045. /*
  1046. * For each pinmux setting in the old state, forget SW's record
  1047. * of mux owner for that pingroup. Any pingroups which are
  1048. * still owned by the new state will be re-acquired by the call
  1049. * to pinmux_enable_setting() in the loop below.
  1050. */
  1051. list_for_each_entry(setting, &old_state->settings, node) {
  1052. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
  1053. continue;
  1054. pinmux_disable_setting(setting);
  1055. }
  1056. }
  1057. p->state = NULL;
  1058. /* Apply all the settings for the new state - pinmux first */
  1059. list_for_each_entry(setting, &state->settings, node) {
  1060. switch (setting->type) {
  1061. case PIN_MAP_TYPE_MUX_GROUP:
  1062. ret = pinmux_enable_setting(setting);
  1063. break;
  1064. case PIN_MAP_TYPE_CONFIGS_PIN:
  1065. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1066. ret = 0;
  1067. break;
  1068. default:
  1069. ret = -EINVAL;
  1070. break;
  1071. }
  1072. if (ret < 0)
  1073. goto unapply_new_state;
  1074. /* Do not link hogs (circular dependency) */
  1075. if (p != setting->pctldev->p)
  1076. pinctrl_link_add(setting->pctldev, p->dev);
  1077. }
  1078. /* Apply all the settings for the new state - pinconf after */
  1079. list_for_each_entry(setting, &state->settings, node) {
  1080. switch (setting->type) {
  1081. case PIN_MAP_TYPE_MUX_GROUP:
  1082. ret = 0;
  1083. break;
  1084. case PIN_MAP_TYPE_CONFIGS_PIN:
  1085. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1086. ret = pinconf_apply_setting(setting);
  1087. break;
  1088. default:
  1089. ret = -EINVAL;
  1090. break;
  1091. }
  1092. if (ret < 0) {
  1093. goto unapply_new_state;
  1094. }
  1095. /* Do not link hogs (circular dependency) */
  1096. if (p != setting->pctldev->p)
  1097. pinctrl_link_add(setting->pctldev, p->dev);
  1098. }
  1099. p->state = state;
  1100. return 0;
  1101. unapply_new_state:
  1102. dev_err(p->dev, "Error applying setting, reverse things back\n");
  1103. list_for_each_entry(setting2, &state->settings, node) {
  1104. if (&setting2->node == &setting->node)
  1105. break;
  1106. /*
  1107. * All we can do here is pinmux_disable_setting.
  1108. * That means that some pins are muxed differently now
  1109. * than they were before applying the setting (We can't
  1110. * "unmux a pin"!), but it's not a big deal since the pins
  1111. * are free to be muxed by another apply_setting.
  1112. */
  1113. if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
  1114. pinmux_disable_setting(setting2);
  1115. }
  1116. /* There's no infinite recursive loop here because p->state is NULL */
  1117. if (old_state)
  1118. pinctrl_select_state(p, old_state);
  1119. return ret;
  1120. }
  1121. /**
  1122. * pinctrl_select_state() - select/activate/program a pinctrl state to HW
  1123. * @p: the pinctrl handle for the device that requests configuration
  1124. * @state: the state handle to select/activate/program
  1125. */
  1126. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
  1127. {
  1128. if (p->state == state)
  1129. return 0;
  1130. return pinctrl_commit_state(p, state);
  1131. }
  1132. EXPORT_SYMBOL_GPL(pinctrl_select_state);
  1133. static void devm_pinctrl_release(struct device *dev, void *res)
  1134. {
  1135. pinctrl_put(*(struct pinctrl **)res);
  1136. }
  1137. /**
  1138. * devm_pinctrl_get() - Resource managed pinctrl_get()
  1139. * @dev: the device to obtain the handle for
  1140. *
  1141. * If there is a need to explicitly destroy the returned struct pinctrl,
  1142. * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  1143. */
  1144. struct pinctrl *devm_pinctrl_get(struct device *dev)
  1145. {
  1146. struct pinctrl **ptr, *p;
  1147. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
  1148. if (!ptr)
  1149. return ERR_PTR(-ENOMEM);
  1150. p = pinctrl_get(dev);
  1151. if (!IS_ERR(p)) {
  1152. *ptr = p;
  1153. devres_add(dev, ptr);
  1154. } else {
  1155. devres_free(ptr);
  1156. }
  1157. return p;
  1158. }
  1159. EXPORT_SYMBOL_GPL(devm_pinctrl_get);
  1160. static int devm_pinctrl_match(struct device *dev, void *res, void *data)
  1161. {
  1162. struct pinctrl **p = res;
  1163. return *p == data;
  1164. }
  1165. /**
  1166. * devm_pinctrl_put() - Resource managed pinctrl_put()
  1167. * @p: the pinctrl handle to release
  1168. *
  1169. * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
  1170. * this function will not need to be called and the resource management
  1171. * code will ensure that the resource is freed.
  1172. */
  1173. void devm_pinctrl_put(struct pinctrl *p)
  1174. {
  1175. WARN_ON(devres_release(p->dev, devm_pinctrl_release,
  1176. devm_pinctrl_match, p));
  1177. }
  1178. EXPORT_SYMBOL_GPL(devm_pinctrl_put);
  1179. /**
  1180. * pinctrl_register_mappings() - register a set of pin controller mappings
  1181. * @maps: the pincontrol mappings table to register. Note the pinctrl-core
  1182. * keeps a reference to the passed in maps, so they should _not_ be
  1183. * marked with __initdata.
  1184. * @num_maps: the number of maps in the mapping table
  1185. */
  1186. int pinctrl_register_mappings(const struct pinctrl_map *maps,
  1187. unsigned num_maps)
  1188. {
  1189. int i, ret;
  1190. struct pinctrl_maps *maps_node;
  1191. pr_debug("add %u pinctrl maps\n", num_maps);
  1192. /* First sanity check the new mapping */
  1193. for (i = 0; i < num_maps; i++) {
  1194. if (!maps[i].dev_name) {
  1195. pr_err("failed to register map %s (%d): no device given\n",
  1196. maps[i].name, i);
  1197. return -EINVAL;
  1198. }
  1199. if (!maps[i].name) {
  1200. pr_err("failed to register map %d: no map name given\n",
  1201. i);
  1202. return -EINVAL;
  1203. }
  1204. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
  1205. !maps[i].ctrl_dev_name) {
  1206. pr_err("failed to register map %s (%d): no pin control device given\n",
  1207. maps[i].name, i);
  1208. return -EINVAL;
  1209. }
  1210. switch (maps[i].type) {
  1211. case PIN_MAP_TYPE_DUMMY_STATE:
  1212. break;
  1213. case PIN_MAP_TYPE_MUX_GROUP:
  1214. ret = pinmux_validate_map(&maps[i], i);
  1215. if (ret < 0)
  1216. return ret;
  1217. break;
  1218. case PIN_MAP_TYPE_CONFIGS_PIN:
  1219. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1220. ret = pinconf_validate_map(&maps[i], i);
  1221. if (ret < 0)
  1222. return ret;
  1223. break;
  1224. default:
  1225. pr_err("failed to register map %s (%d): invalid type given\n",
  1226. maps[i].name, i);
  1227. return -EINVAL;
  1228. }
  1229. }
  1230. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  1231. if (!maps_node)
  1232. return -ENOMEM;
  1233. maps_node->maps = maps;
  1234. maps_node->num_maps = num_maps;
  1235. mutex_lock(&pinctrl_maps_mutex);
  1236. list_add_tail(&maps_node->node, &pinctrl_maps);
  1237. mutex_unlock(&pinctrl_maps_mutex);
  1238. return 0;
  1239. }
  1240. EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
  1241. /**
  1242. * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
  1243. * @map: the pincontrol mappings table passed to pinctrl_register_mappings()
  1244. * when registering the mappings.
  1245. */
  1246. void pinctrl_unregister_mappings(const struct pinctrl_map *map)
  1247. {
  1248. struct pinctrl_maps *maps_node;
  1249. mutex_lock(&pinctrl_maps_mutex);
  1250. list_for_each_entry(maps_node, &pinctrl_maps, node) {
  1251. if (maps_node->maps == map) {
  1252. list_del(&maps_node->node);
  1253. kfree(maps_node);
  1254. mutex_unlock(&pinctrl_maps_mutex);
  1255. return;
  1256. }
  1257. }
  1258. mutex_unlock(&pinctrl_maps_mutex);
  1259. }
  1260. EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
  1261. /**
  1262. * pinctrl_force_sleep() - turn a given controller device into sleep state
  1263. * @pctldev: pin controller device
  1264. */
  1265. int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
  1266. {
  1267. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
  1268. return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
  1269. return 0;
  1270. }
  1271. EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
  1272. /**
  1273. * pinctrl_force_default() - turn a given controller device into default state
  1274. * @pctldev: pin controller device
  1275. */
  1276. int pinctrl_force_default(struct pinctrl_dev *pctldev)
  1277. {
  1278. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
  1279. return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
  1280. return 0;
  1281. }
  1282. EXPORT_SYMBOL_GPL(pinctrl_force_default);
  1283. /**
  1284. * pinctrl_init_done() - tell pinctrl probe is done
  1285. *
  1286. * We'll use this time to switch the pins from "init" to "default" unless the
  1287. * driver selected some other state.
  1288. *
  1289. * @dev: device to that's done probing
  1290. */
  1291. int pinctrl_init_done(struct device *dev)
  1292. {
  1293. struct dev_pin_info *pins = dev->pins;
  1294. int ret;
  1295. if (!pins)
  1296. return 0;
  1297. if (IS_ERR(pins->init_state))
  1298. return 0; /* No such state */
  1299. if (pins->p->state != pins->init_state)
  1300. return 0; /* Not at init anyway */
  1301. if (IS_ERR(pins->default_state))
  1302. return 0; /* No default state */
  1303. ret = pinctrl_select_state(pins->p, pins->default_state);
  1304. if (ret)
  1305. dev_err(dev, "failed to activate default pinctrl state\n");
  1306. return ret;
  1307. }
  1308. static int pinctrl_select_bound_state(struct device *dev,
  1309. struct pinctrl_state *state)
  1310. {
  1311. struct dev_pin_info *pins = dev->pins;
  1312. int ret;
  1313. if (IS_ERR(state))
  1314. return 0; /* No such state */
  1315. ret = pinctrl_select_state(pins->p, state);
  1316. if (ret)
  1317. dev_err(dev, "failed to activate pinctrl state %s\n",
  1318. state->name);
  1319. return ret;
  1320. }
  1321. /**
  1322. * pinctrl_select_default_state() - select default pinctrl state
  1323. * @dev: device to select default state for
  1324. */
  1325. int pinctrl_select_default_state(struct device *dev)
  1326. {
  1327. if (!dev->pins)
  1328. return 0;
  1329. return pinctrl_select_bound_state(dev, dev->pins->default_state);
  1330. }
  1331. EXPORT_SYMBOL_GPL(pinctrl_select_default_state);
  1332. #ifdef CONFIG_PM
  1333. /**
  1334. * pinctrl_pm_select_default_state() - select default pinctrl state for PM
  1335. * @dev: device to select default state for
  1336. */
  1337. int pinctrl_pm_select_default_state(struct device *dev)
  1338. {
  1339. return pinctrl_select_default_state(dev);
  1340. }
  1341. EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
  1342. /**
  1343. * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
  1344. * @dev: device to select sleep state for
  1345. */
  1346. int pinctrl_pm_select_sleep_state(struct device *dev)
  1347. {
  1348. if (!dev->pins)
  1349. return 0;
  1350. return pinctrl_select_bound_state(dev, dev->pins->sleep_state);
  1351. }
  1352. EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
  1353. /**
  1354. * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
  1355. * @dev: device to select idle state for
  1356. */
  1357. int pinctrl_pm_select_idle_state(struct device *dev)
  1358. {
  1359. if (!dev->pins)
  1360. return 0;
  1361. return pinctrl_select_bound_state(dev, dev->pins->idle_state);
  1362. }
  1363. EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
  1364. #endif
  1365. #ifdef CONFIG_DEBUG_FS
  1366. static int pinctrl_pins_show(struct seq_file *s, void *what)
  1367. {
  1368. struct pinctrl_dev *pctldev = s->private;
  1369. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1370. unsigned i, pin;
  1371. #ifdef CONFIG_GPIOLIB
  1372. struct pinctrl_gpio_range *range;
  1373. struct gpio_chip *chip;
  1374. int gpio_num;
  1375. #endif
  1376. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  1377. mutex_lock(&pctldev->mutex);
  1378. /* The pin number can be retrived from the pin controller descriptor */
  1379. for (i = 0; i < pctldev->desc->npins; i++) {
  1380. struct pin_desc *desc;
  1381. pin = pctldev->desc->pins[i].number;
  1382. desc = pin_desc_get(pctldev, pin);
  1383. /* Pin space may be sparse */
  1384. if (!desc)
  1385. continue;
  1386. seq_printf(s, "pin %d (%s) ", pin, desc->name);
  1387. #ifdef CONFIG_GPIOLIB
  1388. gpio_num = -1;
  1389. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  1390. if ((pin >= range->pin_base) &&
  1391. (pin < (range->pin_base + range->npins))) {
  1392. gpio_num = range->base + (pin - range->pin_base);
  1393. break;
  1394. }
  1395. }
  1396. if (gpio_num >= 0)
  1397. chip = gpio_to_chip(gpio_num);
  1398. else
  1399. chip = NULL;
  1400. if (chip)
  1401. seq_printf(s, "%u:%s ", gpio_num - chip->gpiodev->base, chip->label);
  1402. else
  1403. seq_puts(s, "0:? ");
  1404. #endif
  1405. /* Driver-specific info per pin */
  1406. if (ops->pin_dbg_show)
  1407. ops->pin_dbg_show(pctldev, s, pin);
  1408. seq_puts(s, "\n");
  1409. }
  1410. mutex_unlock(&pctldev->mutex);
  1411. return 0;
  1412. }
  1413. DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
  1414. static int pinctrl_groups_show(struct seq_file *s, void *what)
  1415. {
  1416. struct pinctrl_dev *pctldev = s->private;
  1417. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1418. unsigned ngroups, selector = 0;
  1419. mutex_lock(&pctldev->mutex);
  1420. ngroups = ops->get_groups_count(pctldev);
  1421. seq_puts(s, "registered pin groups:\n");
  1422. while (selector < ngroups) {
  1423. const unsigned *pins = NULL;
  1424. unsigned num_pins = 0;
  1425. const char *gname = ops->get_group_name(pctldev, selector);
  1426. const char *pname;
  1427. int ret = 0;
  1428. int i;
  1429. if (ops->get_group_pins)
  1430. ret = ops->get_group_pins(pctldev, selector,
  1431. &pins, &num_pins);
  1432. if (ret)
  1433. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  1434. gname);
  1435. else {
  1436. seq_printf(s, "group: %s\n", gname);
  1437. for (i = 0; i < num_pins; i++) {
  1438. pname = pin_get_name(pctldev, pins[i]);
  1439. if (WARN_ON(!pname)) {
  1440. mutex_unlock(&pctldev->mutex);
  1441. return -EINVAL;
  1442. }
  1443. seq_printf(s, "pin %d (%s)\n", pins[i], pname);
  1444. }
  1445. seq_puts(s, "\n");
  1446. }
  1447. selector++;
  1448. }
  1449. mutex_unlock(&pctldev->mutex);
  1450. return 0;
  1451. }
  1452. DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
  1453. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  1454. {
  1455. struct pinctrl_dev *pctldev = s->private;
  1456. struct pinctrl_gpio_range *range;
  1457. seq_puts(s, "GPIO ranges handled:\n");
  1458. mutex_lock(&pctldev->mutex);
  1459. /* Loop over the ranges */
  1460. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  1461. if (range->pins) {
  1462. int a;
  1463. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
  1464. range->id, range->name,
  1465. range->base, (range->base + range->npins - 1));
  1466. for (a = 0; a < range->npins - 1; a++)
  1467. seq_printf(s, "%u, ", range->pins[a]);
  1468. seq_printf(s, "%u}\n", range->pins[a]);
  1469. }
  1470. else
  1471. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  1472. range->id, range->name,
  1473. range->base, (range->base + range->npins - 1),
  1474. range->pin_base,
  1475. (range->pin_base + range->npins - 1));
  1476. }
  1477. mutex_unlock(&pctldev->mutex);
  1478. return 0;
  1479. }
  1480. DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
  1481. static int pinctrl_devices_show(struct seq_file *s, void *what)
  1482. {
  1483. struct pinctrl_dev *pctldev;
  1484. seq_puts(s, "name [pinmux] [pinconf]\n");
  1485. mutex_lock(&pinctrldev_list_mutex);
  1486. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  1487. seq_printf(s, "%s ", pctldev->desc->name);
  1488. if (pctldev->desc->pmxops)
  1489. seq_puts(s, "yes ");
  1490. else
  1491. seq_puts(s, "no ");
  1492. if (pctldev->desc->confops)
  1493. seq_puts(s, "yes");
  1494. else
  1495. seq_puts(s, "no");
  1496. seq_puts(s, "\n");
  1497. }
  1498. mutex_unlock(&pinctrldev_list_mutex);
  1499. return 0;
  1500. }
  1501. DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
  1502. static inline const char *map_type(enum pinctrl_map_type type)
  1503. {
  1504. static const char * const names[] = {
  1505. "INVALID",
  1506. "DUMMY_STATE",
  1507. "MUX_GROUP",
  1508. "CONFIGS_PIN",
  1509. "CONFIGS_GROUP",
  1510. };
  1511. if (type >= ARRAY_SIZE(names))
  1512. return "UNKNOWN";
  1513. return names[type];
  1514. }
  1515. static int pinctrl_maps_show(struct seq_file *s, void *what)
  1516. {
  1517. struct pinctrl_maps *maps_node;
  1518. int i;
  1519. const struct pinctrl_map *map;
  1520. seq_puts(s, "Pinctrl maps:\n");
  1521. mutex_lock(&pinctrl_maps_mutex);
  1522. for_each_maps(maps_node, i, map) {
  1523. seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
  1524. map->dev_name, map->name, map_type(map->type),
  1525. map->type);
  1526. if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
  1527. seq_printf(s, "controlling device %s\n",
  1528. map->ctrl_dev_name);
  1529. switch (map->type) {
  1530. case PIN_MAP_TYPE_MUX_GROUP:
  1531. pinmux_show_map(s, map);
  1532. break;
  1533. case PIN_MAP_TYPE_CONFIGS_PIN:
  1534. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1535. pinconf_show_map(s, map);
  1536. break;
  1537. default:
  1538. break;
  1539. }
  1540. seq_putc(s, '\n');
  1541. }
  1542. mutex_unlock(&pinctrl_maps_mutex);
  1543. return 0;
  1544. }
  1545. DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
  1546. static int pinctrl_show(struct seq_file *s, void *what)
  1547. {
  1548. struct pinctrl *p;
  1549. struct pinctrl_state *state;
  1550. struct pinctrl_setting *setting;
  1551. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  1552. mutex_lock(&pinctrl_list_mutex);
  1553. list_for_each_entry(p, &pinctrl_list, node) {
  1554. seq_printf(s, "device: %s current state: %s\n",
  1555. dev_name(p->dev),
  1556. p->state ? p->state->name : "none");
  1557. list_for_each_entry(state, &p->states, node) {
  1558. seq_printf(s, " state: %s\n", state->name);
  1559. list_for_each_entry(setting, &state->settings, node) {
  1560. struct pinctrl_dev *pctldev = setting->pctldev;
  1561. seq_printf(s, " type: %s controller %s ",
  1562. map_type(setting->type),
  1563. pinctrl_dev_get_name(pctldev));
  1564. switch (setting->type) {
  1565. case PIN_MAP_TYPE_MUX_GROUP:
  1566. pinmux_show_setting(s, setting);
  1567. break;
  1568. case PIN_MAP_TYPE_CONFIGS_PIN:
  1569. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1570. pinconf_show_setting(s, setting);
  1571. break;
  1572. default:
  1573. break;
  1574. }
  1575. }
  1576. }
  1577. }
  1578. mutex_unlock(&pinctrl_list_mutex);
  1579. return 0;
  1580. }
  1581. DEFINE_SHOW_ATTRIBUTE(pinctrl);
  1582. static struct dentry *debugfs_root;
  1583. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1584. {
  1585. struct dentry *device_root;
  1586. const char *debugfs_name;
  1587. if (pctldev->desc->name &&
  1588. strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
  1589. debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
  1590. "%s-%s", dev_name(pctldev->dev),
  1591. pctldev->desc->name);
  1592. if (!debugfs_name) {
  1593. pr_warn("failed to determine debugfs dir name for %s\n",
  1594. dev_name(pctldev->dev));
  1595. return;
  1596. }
  1597. } else {
  1598. debugfs_name = dev_name(pctldev->dev);
  1599. }
  1600. device_root = debugfs_create_dir(debugfs_name, debugfs_root);
  1601. pctldev->device_root = device_root;
  1602. if (IS_ERR(device_root) || !device_root) {
  1603. pr_warn("failed to create debugfs directory for %s\n",
  1604. dev_name(pctldev->dev));
  1605. return;
  1606. }
  1607. debugfs_create_file("pins", 0444,
  1608. device_root, pctldev, &pinctrl_pins_fops);
  1609. debugfs_create_file("pingroups", 0444,
  1610. device_root, pctldev, &pinctrl_groups_fops);
  1611. debugfs_create_file("gpio-ranges", 0444,
  1612. device_root, pctldev, &pinctrl_gpioranges_fops);
  1613. if (pctldev->desc->pmxops)
  1614. pinmux_init_device_debugfs(device_root, pctldev);
  1615. if (pctldev->desc->confops)
  1616. pinconf_init_device_debugfs(device_root, pctldev);
  1617. }
  1618. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1619. {
  1620. debugfs_remove_recursive(pctldev->device_root);
  1621. }
  1622. static void pinctrl_init_debugfs(void)
  1623. {
  1624. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  1625. if (IS_ERR(debugfs_root) || !debugfs_root) {
  1626. pr_warn("failed to create debugfs directory\n");
  1627. debugfs_root = NULL;
  1628. return;
  1629. }
  1630. debugfs_create_file("pinctrl-devices", 0444,
  1631. debugfs_root, NULL, &pinctrl_devices_fops);
  1632. debugfs_create_file("pinctrl-maps", 0444,
  1633. debugfs_root, NULL, &pinctrl_maps_fops);
  1634. debugfs_create_file("pinctrl-handles", 0444,
  1635. debugfs_root, NULL, &pinctrl_fops);
  1636. }
  1637. #else /* CONFIG_DEBUG_FS */
  1638. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1639. {
  1640. }
  1641. static void pinctrl_init_debugfs(void)
  1642. {
  1643. }
  1644. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1645. {
  1646. }
  1647. #endif
  1648. static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
  1649. {
  1650. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1651. if (!ops ||
  1652. !ops->get_groups_count ||
  1653. !ops->get_group_name)
  1654. return -EINVAL;
  1655. return 0;
  1656. }
  1657. /**
  1658. * pinctrl_init_controller() - init a pin controller device
  1659. * @pctldesc: descriptor for this pin controller
  1660. * @dev: parent device for this pin controller
  1661. * @driver_data: private pin controller data for this pin controller
  1662. */
  1663. static struct pinctrl_dev *
  1664. pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
  1665. void *driver_data)
  1666. {
  1667. struct pinctrl_dev *pctldev;
  1668. int ret;
  1669. if (!pctldesc)
  1670. return ERR_PTR(-EINVAL);
  1671. if (!pctldesc->name)
  1672. return ERR_PTR(-EINVAL);
  1673. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  1674. if (!pctldev)
  1675. return ERR_PTR(-ENOMEM);
  1676. /* Initialize pin control device struct */
  1677. pctldev->owner = pctldesc->owner;
  1678. pctldev->desc = pctldesc;
  1679. pctldev->driver_data = driver_data;
  1680. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1681. #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
  1682. INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
  1683. #endif
  1684. #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
  1685. INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
  1686. #endif
  1687. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1688. INIT_LIST_HEAD(&pctldev->node);
  1689. pctldev->dev = dev;
  1690. mutex_init(&pctldev->mutex);
  1691. /* check core ops for sanity */
  1692. ret = pinctrl_check_ops(pctldev);
  1693. if (ret) {
  1694. dev_err(dev, "pinctrl ops lacks necessary functions\n");
  1695. goto out_err;
  1696. }
  1697. /* If we're implementing pinmuxing, check the ops for sanity */
  1698. if (pctldesc->pmxops) {
  1699. ret = pinmux_check_ops(pctldev);
  1700. if (ret)
  1701. goto out_err;
  1702. }
  1703. /* If we're implementing pinconfig, check the ops for sanity */
  1704. if (pctldesc->confops) {
  1705. ret = pinconf_check_ops(pctldev);
  1706. if (ret)
  1707. goto out_err;
  1708. }
  1709. /* Register all the pins */
  1710. dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
  1711. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1712. if (ret) {
  1713. dev_err(dev, "error during pin registration\n");
  1714. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1715. pctldesc->npins);
  1716. goto out_err;
  1717. }
  1718. return pctldev;
  1719. out_err:
  1720. mutex_destroy(&pctldev->mutex);
  1721. kfree(pctldev);
  1722. return ERR_PTR(ret);
  1723. }
  1724. static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
  1725. {
  1726. pctldev->p = create_pinctrl(pctldev->dev, pctldev);
  1727. if (PTR_ERR(pctldev->p) == -ENODEV) {
  1728. dev_dbg(pctldev->dev, "no hogs found\n");
  1729. return 0;
  1730. }
  1731. if (IS_ERR(pctldev->p)) {
  1732. dev_err(pctldev->dev, "error claiming hogs: %li\n",
  1733. PTR_ERR(pctldev->p));
  1734. return PTR_ERR(pctldev->p);
  1735. }
  1736. pctldev->hog_default =
  1737. pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
  1738. if (IS_ERR(pctldev->hog_default)) {
  1739. dev_dbg(pctldev->dev,
  1740. "failed to lookup the default state\n");
  1741. } else {
  1742. if (pinctrl_select_state(pctldev->p,
  1743. pctldev->hog_default))
  1744. dev_err(pctldev->dev,
  1745. "failed to select default state\n");
  1746. }
  1747. pctldev->hog_sleep =
  1748. pinctrl_lookup_state(pctldev->p,
  1749. PINCTRL_STATE_SLEEP);
  1750. if (IS_ERR(pctldev->hog_sleep))
  1751. dev_dbg(pctldev->dev,
  1752. "failed to lookup the sleep state\n");
  1753. return 0;
  1754. }
  1755. int pinctrl_enable(struct pinctrl_dev *pctldev)
  1756. {
  1757. int error;
  1758. error = pinctrl_claim_hogs(pctldev);
  1759. if (error) {
  1760. dev_err(pctldev->dev, "could not claim hogs: %i\n",
  1761. error);
  1762. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1763. pctldev->desc->npins);
  1764. mutex_destroy(&pctldev->mutex);
  1765. kfree(pctldev);
  1766. return error;
  1767. }
  1768. mutex_lock(&pinctrldev_list_mutex);
  1769. list_add_tail(&pctldev->node, &pinctrldev_list);
  1770. mutex_unlock(&pinctrldev_list_mutex);
  1771. pinctrl_init_device_debugfs(pctldev);
  1772. return 0;
  1773. }
  1774. EXPORT_SYMBOL_GPL(pinctrl_enable);
  1775. /**
  1776. * pinctrl_register() - register a pin controller device
  1777. * @pctldesc: descriptor for this pin controller
  1778. * @dev: parent device for this pin controller
  1779. * @driver_data: private pin controller data for this pin controller
  1780. *
  1781. * Note that pinctrl_register() is known to have problems as the pin
  1782. * controller driver functions are called before the driver has a
  1783. * struct pinctrl_dev handle. To avoid issues later on, please use the
  1784. * new pinctrl_register_and_init() below instead.
  1785. */
  1786. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  1787. struct device *dev, void *driver_data)
  1788. {
  1789. struct pinctrl_dev *pctldev;
  1790. int error;
  1791. pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
  1792. if (IS_ERR(pctldev))
  1793. return pctldev;
  1794. error = pinctrl_enable(pctldev);
  1795. if (error)
  1796. return ERR_PTR(error);
  1797. return pctldev;
  1798. }
  1799. EXPORT_SYMBOL_GPL(pinctrl_register);
  1800. /**
  1801. * pinctrl_register_and_init() - register and init pin controller device
  1802. * @pctldesc: descriptor for this pin controller
  1803. * @dev: parent device for this pin controller
  1804. * @driver_data: private pin controller data for this pin controller
  1805. * @pctldev: pin controller device
  1806. *
  1807. * Note that pinctrl_enable() still needs to be manually called after
  1808. * this once the driver is ready.
  1809. */
  1810. int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
  1811. struct device *dev, void *driver_data,
  1812. struct pinctrl_dev **pctldev)
  1813. {
  1814. struct pinctrl_dev *p;
  1815. p = pinctrl_init_controller(pctldesc, dev, driver_data);
  1816. if (IS_ERR(p))
  1817. return PTR_ERR(p);
  1818. /*
  1819. * We have pinctrl_start() call functions in the pin controller
  1820. * driver with create_pinctrl() for at least dt_node_to_map(). So
  1821. * let's make sure pctldev is properly initialized for the
  1822. * pin controller driver before we do anything.
  1823. */
  1824. *pctldev = p;
  1825. return 0;
  1826. }
  1827. EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
  1828. /**
  1829. * pinctrl_unregister() - unregister pinmux
  1830. * @pctldev: pin controller to unregister
  1831. *
  1832. * Called by pinmux drivers to unregister a pinmux.
  1833. */
  1834. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1835. {
  1836. struct pinctrl_gpio_range *range, *n;
  1837. if (!pctldev)
  1838. return;
  1839. mutex_lock(&pctldev->mutex);
  1840. pinctrl_remove_device_debugfs(pctldev);
  1841. mutex_unlock(&pctldev->mutex);
  1842. if (!IS_ERR_OR_NULL(pctldev->p))
  1843. pinctrl_put(pctldev->p);
  1844. mutex_lock(&pinctrldev_list_mutex);
  1845. mutex_lock(&pctldev->mutex);
  1846. /* TODO: check that no pinmuxes are still active? */
  1847. list_del(&pctldev->node);
  1848. pinmux_generic_free_functions(pctldev);
  1849. pinctrl_generic_free_groups(pctldev);
  1850. /* Destroy descriptor tree */
  1851. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1852. pctldev->desc->npins);
  1853. /* remove gpio ranges map */
  1854. list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
  1855. list_del(&range->node);
  1856. mutex_unlock(&pctldev->mutex);
  1857. mutex_destroy(&pctldev->mutex);
  1858. kfree(pctldev);
  1859. mutex_unlock(&pinctrldev_list_mutex);
  1860. }
  1861. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1862. static void devm_pinctrl_dev_release(struct device *dev, void *res)
  1863. {
  1864. struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
  1865. pinctrl_unregister(pctldev);
  1866. }
  1867. static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
  1868. {
  1869. struct pctldev **r = res;
  1870. if (WARN_ON(!r || !*r))
  1871. return 0;
  1872. return *r == data;
  1873. }
  1874. /**
  1875. * devm_pinctrl_register() - Resource managed version of pinctrl_register().
  1876. * @dev: parent device for this pin controller
  1877. * @pctldesc: descriptor for this pin controller
  1878. * @driver_data: private pin controller data for this pin controller
  1879. *
  1880. * Returns an error pointer if pincontrol register failed. Otherwise
  1881. * it returns valid pinctrl handle.
  1882. *
  1883. * The pinctrl device will be automatically released when the device is unbound.
  1884. */
  1885. struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
  1886. struct pinctrl_desc *pctldesc,
  1887. void *driver_data)
  1888. {
  1889. struct pinctrl_dev **ptr, *pctldev;
  1890. ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
  1891. if (!ptr)
  1892. return ERR_PTR(-ENOMEM);
  1893. pctldev = pinctrl_register(pctldesc, dev, driver_data);
  1894. if (IS_ERR(pctldev)) {
  1895. devres_free(ptr);
  1896. return pctldev;
  1897. }
  1898. *ptr = pctldev;
  1899. devres_add(dev, ptr);
  1900. return pctldev;
  1901. }
  1902. EXPORT_SYMBOL_GPL(devm_pinctrl_register);
  1903. /**
  1904. * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
  1905. * @dev: parent device for this pin controller
  1906. * @pctldesc: descriptor for this pin controller
  1907. * @driver_data: private pin controller data for this pin controller
  1908. * @pctldev: pin controller device
  1909. *
  1910. * Returns zero on success or an error number on failure.
  1911. *
  1912. * The pinctrl device will be automatically released when the device is unbound.
  1913. */
  1914. int devm_pinctrl_register_and_init(struct device *dev,
  1915. struct pinctrl_desc *pctldesc,
  1916. void *driver_data,
  1917. struct pinctrl_dev **pctldev)
  1918. {
  1919. struct pinctrl_dev **ptr;
  1920. int error;
  1921. ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
  1922. if (!ptr)
  1923. return -ENOMEM;
  1924. error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
  1925. if (error) {
  1926. devres_free(ptr);
  1927. return error;
  1928. }
  1929. *ptr = *pctldev;
  1930. devres_add(dev, ptr);
  1931. return 0;
  1932. }
  1933. EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
  1934. /**
  1935. * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
  1936. * @dev: device for which resource was allocated
  1937. * @pctldev: the pinctrl device to unregister.
  1938. */
  1939. void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
  1940. {
  1941. WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
  1942. devm_pinctrl_dev_match, pctldev));
  1943. }
  1944. EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
  1945. static int __init pinctrl_init(void)
  1946. {
  1947. pr_info("initialized pinctrl subsystem\n");
  1948. pinctrl_init_debugfs();
  1949. return 0;
  1950. }
  1951. /* init early since many drivers really need to initialized pinmux early */
  1952. core_initcall(pinctrl_init);