core.c 57 KB

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