core.c 56 KB

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