gpiolib.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/irq.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/list.h>
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/gpio.h>
  12. #include <linux/of_gpio.h>
  13. #include <linux/idr.h>
  14. #include <linux/slab.h>
  15. #include <linux/acpi.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/gpio/machine.h>
  18. #include "gpiolib.h"
  19. #define CREATE_TRACE_POINTS
  20. #include <trace/events/gpio.h>
  21. /* Implementation infrastructure for GPIO interfaces.
  22. *
  23. * The GPIO programming interface allows for inlining speed-critical
  24. * get/set operations for common cases, so that access to SOC-integrated
  25. * GPIOs can sometimes cost only an instruction or two per bit.
  26. */
  27. /* When debugging, extend minimal trust to callers and platform code.
  28. * Also emit diagnostic messages that may help initial bringup, when
  29. * board setup or driver bugs are most common.
  30. *
  31. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  32. */
  33. #ifdef DEBUG
  34. #define extra_checks 1
  35. #else
  36. #define extra_checks 0
  37. #endif
  38. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  39. * While any GPIO is requested, its gpio_chip is not removable;
  40. * each GPIO's "requested" flag serves as a lock and refcount.
  41. */
  42. DEFINE_SPINLOCK(gpio_lock);
  43. #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
  44. static DEFINE_MUTEX(gpio_lookup_lock);
  45. static LIST_HEAD(gpio_lookup_list);
  46. LIST_HEAD(gpio_chips);
  47. static void gpiochip_free_hogs(struct gpio_chip *chip);
  48. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
  49. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  50. {
  51. d->label = label;
  52. }
  53. /**
  54. * Convert a GPIO number to its descriptor
  55. */
  56. struct gpio_desc *gpio_to_desc(unsigned gpio)
  57. {
  58. struct gpio_chip *chip;
  59. unsigned long flags;
  60. spin_lock_irqsave(&gpio_lock, flags);
  61. list_for_each_entry(chip, &gpio_chips, list) {
  62. if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
  63. spin_unlock_irqrestore(&gpio_lock, flags);
  64. return &chip->desc[gpio - chip->base];
  65. }
  66. }
  67. spin_unlock_irqrestore(&gpio_lock, flags);
  68. if (!gpio_is_valid(gpio))
  69. WARN(1, "invalid GPIO %d\n", gpio);
  70. return NULL;
  71. }
  72. EXPORT_SYMBOL_GPL(gpio_to_desc);
  73. /**
  74. * Get the GPIO descriptor corresponding to the given hw number for this chip.
  75. */
  76. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
  77. u16 hwnum)
  78. {
  79. if (hwnum >= chip->ngpio)
  80. return ERR_PTR(-EINVAL);
  81. return &chip->desc[hwnum];
  82. }
  83. /**
  84. * Convert a GPIO descriptor to the integer namespace.
  85. * This should disappear in the future but is needed since we still
  86. * use GPIO numbers for error messages and sysfs nodes
  87. */
  88. int desc_to_gpio(const struct gpio_desc *desc)
  89. {
  90. return desc->chip->base + (desc - &desc->chip->desc[0]);
  91. }
  92. EXPORT_SYMBOL_GPL(desc_to_gpio);
  93. /**
  94. * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
  95. * @desc: descriptor to return the chip of
  96. */
  97. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  98. {
  99. return desc ? desc->chip : NULL;
  100. }
  101. EXPORT_SYMBOL_GPL(gpiod_to_chip);
  102. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  103. static int gpiochip_find_base(int ngpio)
  104. {
  105. struct gpio_chip *chip;
  106. int base = ARCH_NR_GPIOS - ngpio;
  107. list_for_each_entry_reverse(chip, &gpio_chips, list) {
  108. /* found a free space? */
  109. if (chip->base + chip->ngpio <= base)
  110. break;
  111. else
  112. /* nope, check the space right before the chip */
  113. base = chip->base - ngpio;
  114. }
  115. if (gpio_is_valid(base)) {
  116. pr_debug("%s: found new base at %d\n", __func__, base);
  117. return base;
  118. } else {
  119. pr_err("%s: cannot find free range\n", __func__);
  120. return -ENOSPC;
  121. }
  122. }
  123. /**
  124. * gpiod_get_direction - return the current direction of a GPIO
  125. * @desc: GPIO to get the direction of
  126. *
  127. * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
  128. *
  129. * This function may sleep if gpiod_cansleep() is true.
  130. */
  131. int gpiod_get_direction(struct gpio_desc *desc)
  132. {
  133. struct gpio_chip *chip;
  134. unsigned offset;
  135. int status = -EINVAL;
  136. chip = gpiod_to_chip(desc);
  137. offset = gpio_chip_hwgpio(desc);
  138. if (!chip->get_direction)
  139. return status;
  140. status = chip->get_direction(chip, offset);
  141. if (status > 0) {
  142. /* GPIOF_DIR_IN, or other positive */
  143. status = 1;
  144. clear_bit(FLAG_IS_OUT, &desc->flags);
  145. }
  146. if (status == 0) {
  147. /* GPIOF_DIR_OUT */
  148. set_bit(FLAG_IS_OUT, &desc->flags);
  149. }
  150. return status;
  151. }
  152. EXPORT_SYMBOL_GPL(gpiod_get_direction);
  153. /*
  154. * Add a new chip to the global chips list, keeping the list of chips sorted
  155. * by base order.
  156. *
  157. * Return -EBUSY if the new chip overlaps with some other chip's integer
  158. * space.
  159. */
  160. static int gpiochip_add_to_list(struct gpio_chip *chip)
  161. {
  162. struct list_head *pos;
  163. struct gpio_chip *_chip;
  164. int err = 0;
  165. /* find where to insert our chip */
  166. list_for_each(pos, &gpio_chips) {
  167. _chip = list_entry(pos, struct gpio_chip, list);
  168. /* shall we insert before _chip? */
  169. if (_chip->base >= chip->base + chip->ngpio)
  170. break;
  171. }
  172. /* are we stepping on the chip right before? */
  173. if (pos != &gpio_chips && pos->prev != &gpio_chips) {
  174. _chip = list_entry(pos->prev, struct gpio_chip, list);
  175. if (_chip->base + _chip->ngpio > chip->base) {
  176. dev_err(chip->dev,
  177. "GPIO integer space overlap, cannot add chip\n");
  178. err = -EBUSY;
  179. }
  180. }
  181. if (!err)
  182. list_add_tail(&chip->list, pos);
  183. return err;
  184. }
  185. /**
  186. * gpiochip_add() - register a gpio_chip
  187. * @chip: the chip to register, with chip->base initialized
  188. * Context: potentially before irqs will work
  189. *
  190. * Returns a negative errno if the chip can't be registered, such as
  191. * because the chip->base is invalid or already associated with a
  192. * different chip. Otherwise it returns zero as a success code.
  193. *
  194. * When gpiochip_add() is called very early during boot, so that GPIOs
  195. * can be freely used, the chip->dev device must be registered before
  196. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  197. * for GPIOs will fail rudely.
  198. *
  199. * If chip->base is negative, this requests dynamic assignment of
  200. * a range of valid GPIOs.
  201. */
  202. int gpiochip_add(struct gpio_chip *chip)
  203. {
  204. unsigned long flags;
  205. int status = 0;
  206. unsigned id;
  207. int base = chip->base;
  208. struct gpio_desc *descs;
  209. descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
  210. if (!descs)
  211. return -ENOMEM;
  212. spin_lock_irqsave(&gpio_lock, flags);
  213. if (base < 0) {
  214. base = gpiochip_find_base(chip->ngpio);
  215. if (base < 0) {
  216. status = base;
  217. spin_unlock_irqrestore(&gpio_lock, flags);
  218. goto err_free_descs;
  219. }
  220. chip->base = base;
  221. }
  222. status = gpiochip_add_to_list(chip);
  223. if (status) {
  224. spin_unlock_irqrestore(&gpio_lock, flags);
  225. goto err_free_descs;
  226. }
  227. for (id = 0; id < chip->ngpio; id++) {
  228. struct gpio_desc *desc = &descs[id];
  229. desc->chip = chip;
  230. /* REVISIT: most hardware initializes GPIOs as inputs (often
  231. * with pullups enabled) so power usage is minimized. Linux
  232. * code should set the gpio direction first thing; but until
  233. * it does, and in case chip->get_direction is not set, we may
  234. * expose the wrong direction in sysfs.
  235. */
  236. desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
  237. }
  238. chip->desc = descs;
  239. spin_unlock_irqrestore(&gpio_lock, flags);
  240. #ifdef CONFIG_PINCTRL
  241. INIT_LIST_HEAD(&chip->pin_ranges);
  242. #endif
  243. if (!chip->owner && chip->dev && chip->dev->driver)
  244. chip->owner = chip->dev->driver->owner;
  245. status = of_gpiochip_add(chip);
  246. if (status)
  247. goto err_remove_chip;
  248. acpi_gpiochip_add(chip);
  249. status = gpiochip_sysfs_register(chip);
  250. if (status)
  251. goto err_remove_chip;
  252. pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
  253. chip->base, chip->base + chip->ngpio - 1,
  254. chip->label ? : "generic");
  255. return 0;
  256. err_remove_chip:
  257. acpi_gpiochip_remove(chip);
  258. gpiochip_free_hogs(chip);
  259. of_gpiochip_remove(chip);
  260. spin_lock_irqsave(&gpio_lock, flags);
  261. list_del(&chip->list);
  262. spin_unlock_irqrestore(&gpio_lock, flags);
  263. chip->desc = NULL;
  264. err_free_descs:
  265. kfree(descs);
  266. /* failures here can mean systems won't boot... */
  267. pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
  268. chip->base, chip->base + chip->ngpio - 1,
  269. chip->label ? : "generic");
  270. return status;
  271. }
  272. EXPORT_SYMBOL_GPL(gpiochip_add);
  273. /**
  274. * gpiochip_remove() - unregister a gpio_chip
  275. * @chip: the chip to unregister
  276. *
  277. * A gpio_chip with any GPIOs still requested may not be removed.
  278. */
  279. void gpiochip_remove(struct gpio_chip *chip)
  280. {
  281. struct gpio_desc *desc;
  282. unsigned long flags;
  283. unsigned id;
  284. bool requested = false;
  285. gpiochip_sysfs_unregister(chip);
  286. gpiochip_irqchip_remove(chip);
  287. acpi_gpiochip_remove(chip);
  288. gpiochip_remove_pin_ranges(chip);
  289. gpiochip_free_hogs(chip);
  290. of_gpiochip_remove(chip);
  291. spin_lock_irqsave(&gpio_lock, flags);
  292. for (id = 0; id < chip->ngpio; id++) {
  293. desc = &chip->desc[id];
  294. desc->chip = NULL;
  295. if (test_bit(FLAG_REQUESTED, &desc->flags))
  296. requested = true;
  297. }
  298. list_del(&chip->list);
  299. spin_unlock_irqrestore(&gpio_lock, flags);
  300. if (requested)
  301. dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
  302. kfree(chip->desc);
  303. chip->desc = NULL;
  304. }
  305. EXPORT_SYMBOL_GPL(gpiochip_remove);
  306. /**
  307. * gpiochip_find() - iterator for locating a specific gpio_chip
  308. * @data: data to pass to match function
  309. * @callback: Callback function to check gpio_chip
  310. *
  311. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  312. * determined by a user supplied @match callback. The callback should return
  313. * 0 if the device doesn't match and non-zero if it does. If the callback is
  314. * non-zero, this function will return to the caller and not iterate over any
  315. * more gpio_chips.
  316. */
  317. struct gpio_chip *gpiochip_find(void *data,
  318. int (*match)(struct gpio_chip *chip,
  319. void *data))
  320. {
  321. struct gpio_chip *chip;
  322. unsigned long flags;
  323. spin_lock_irqsave(&gpio_lock, flags);
  324. list_for_each_entry(chip, &gpio_chips, list)
  325. if (match(chip, data))
  326. break;
  327. /* No match? */
  328. if (&chip->list == &gpio_chips)
  329. chip = NULL;
  330. spin_unlock_irqrestore(&gpio_lock, flags);
  331. return chip;
  332. }
  333. EXPORT_SYMBOL_GPL(gpiochip_find);
  334. static int gpiochip_match_name(struct gpio_chip *chip, void *data)
  335. {
  336. const char *name = data;
  337. return !strcmp(chip->label, name);
  338. }
  339. static struct gpio_chip *find_chip_by_name(const char *name)
  340. {
  341. return gpiochip_find((void *)name, gpiochip_match_name);
  342. }
  343. #ifdef CONFIG_GPIOLIB_IRQCHIP
  344. /*
  345. * The following is irqchip helper code for gpiochips.
  346. */
  347. /**
  348. * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
  349. * @gpiochip: the gpiochip to set the irqchip chain to
  350. * @irqchip: the irqchip to chain to the gpiochip
  351. * @parent_irq: the irq number corresponding to the parent IRQ for this
  352. * chained irqchip
  353. * @parent_handler: the parent interrupt handler for the accumulated IRQ
  354. * coming out of the gpiochip. If the interrupt is nested rather than
  355. * cascaded, pass NULL in this handler argument
  356. */
  357. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  358. struct irq_chip *irqchip,
  359. int parent_irq,
  360. irq_flow_handler_t parent_handler)
  361. {
  362. unsigned int offset;
  363. if (!gpiochip->irqdomain) {
  364. chip_err(gpiochip, "called %s before setting up irqchip\n",
  365. __func__);
  366. return;
  367. }
  368. if (parent_handler) {
  369. if (gpiochip->can_sleep) {
  370. chip_err(gpiochip,
  371. "you cannot have chained interrupts on a "
  372. "chip that may sleep\n");
  373. return;
  374. }
  375. /*
  376. * The parent irqchip is already using the chip_data for this
  377. * irqchip, so our callbacks simply use the handler_data.
  378. */
  379. irq_set_chained_handler_and_data(parent_irq, parent_handler,
  380. gpiochip);
  381. gpiochip->irq_parent = parent_irq;
  382. }
  383. /* Set the parent IRQ for all affected IRQs */
  384. for (offset = 0; offset < gpiochip->ngpio; offset++)
  385. irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
  386. parent_irq);
  387. }
  388. EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
  389. /**
  390. * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
  391. * @d: the irqdomain used by this irqchip
  392. * @irq: the global irq number used by this GPIO irqchip irq
  393. * @hwirq: the local IRQ/GPIO line offset on this gpiochip
  394. *
  395. * This function will set up the mapping for a certain IRQ line on a
  396. * gpiochip by assigning the gpiochip as chip data, and using the irqchip
  397. * stored inside the gpiochip.
  398. */
  399. static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  400. irq_hw_number_t hwirq)
  401. {
  402. struct gpio_chip *chip = d->host_data;
  403. irq_set_chip_data(irq, chip);
  404. /*
  405. * This lock class tells lockdep that GPIO irqs are in a different
  406. * category than their parents, so it won't report false recursion.
  407. */
  408. irq_set_lockdep_class(irq, chip->lock_key);
  409. irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
  410. /* Chips that can sleep need nested thread handlers */
  411. if (chip->can_sleep && !chip->irq_not_threaded)
  412. irq_set_nested_thread(irq, 1);
  413. irq_set_noprobe(irq);
  414. /*
  415. * No set-up of the hardware will happen if IRQ_TYPE_NONE
  416. * is passed as default type.
  417. */
  418. if (chip->irq_default_type != IRQ_TYPE_NONE)
  419. irq_set_irq_type(irq, chip->irq_default_type);
  420. return 0;
  421. }
  422. static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
  423. {
  424. struct gpio_chip *chip = d->host_data;
  425. if (chip->can_sleep)
  426. irq_set_nested_thread(irq, 0);
  427. irq_set_chip_and_handler(irq, NULL, NULL);
  428. irq_set_chip_data(irq, NULL);
  429. }
  430. static const struct irq_domain_ops gpiochip_domain_ops = {
  431. .map = gpiochip_irq_map,
  432. .unmap = gpiochip_irq_unmap,
  433. /* Virtually all GPIO irqchips are twocell:ed */
  434. .xlate = irq_domain_xlate_twocell,
  435. };
  436. static int gpiochip_irq_reqres(struct irq_data *d)
  437. {
  438. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  439. if (!try_module_get(chip->owner))
  440. return -ENODEV;
  441. if (gpiochip_lock_as_irq(chip, d->hwirq)) {
  442. chip_err(chip,
  443. "unable to lock HW IRQ %lu for IRQ\n",
  444. d->hwirq);
  445. module_put(chip->owner);
  446. return -EINVAL;
  447. }
  448. return 0;
  449. }
  450. static void gpiochip_irq_relres(struct irq_data *d)
  451. {
  452. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  453. gpiochip_unlock_as_irq(chip, d->hwirq);
  454. module_put(chip->owner);
  455. }
  456. static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
  457. {
  458. return irq_find_mapping(chip->irqdomain, offset);
  459. }
  460. /**
  461. * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
  462. * @gpiochip: the gpiochip to remove the irqchip from
  463. *
  464. * This is called only from gpiochip_remove()
  465. */
  466. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
  467. {
  468. unsigned int offset;
  469. acpi_gpiochip_free_interrupts(gpiochip);
  470. if (gpiochip->irq_parent) {
  471. irq_set_chained_handler(gpiochip->irq_parent, NULL);
  472. irq_set_handler_data(gpiochip->irq_parent, NULL);
  473. }
  474. /* Remove all IRQ mappings and delete the domain */
  475. if (gpiochip->irqdomain) {
  476. for (offset = 0; offset < gpiochip->ngpio; offset++)
  477. irq_dispose_mapping(
  478. irq_find_mapping(gpiochip->irqdomain, offset));
  479. irq_domain_remove(gpiochip->irqdomain);
  480. }
  481. if (gpiochip->irqchip) {
  482. gpiochip->irqchip->irq_request_resources = NULL;
  483. gpiochip->irqchip->irq_release_resources = NULL;
  484. gpiochip->irqchip = NULL;
  485. }
  486. }
  487. /**
  488. * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
  489. * @gpiochip: the gpiochip to add the irqchip to
  490. * @irqchip: the irqchip to add to the gpiochip
  491. * @first_irq: if not dynamically assigned, the base (first) IRQ to
  492. * allocate gpiochip irqs from
  493. * @handler: the irq handler to use (often a predefined irq core function)
  494. * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
  495. * to have the core avoid setting up any default type in the hardware.
  496. * @lock_key: lockdep class
  497. *
  498. * This function closely associates a certain irqchip with a certain
  499. * gpiochip, providing an irq domain to translate the local IRQs to
  500. * global irqs in the gpiolib core, and making sure that the gpiochip
  501. * is passed as chip data to all related functions. Driver callbacks
  502. * need to use container_of() to get their local state containers back
  503. * from the gpiochip passed as chip data. An irqdomain will be stored
  504. * in the gpiochip that shall be used by the driver to handle IRQ number
  505. * translation. The gpiochip will need to be initialized and registered
  506. * before calling this function.
  507. *
  508. * This function will handle two cell:ed simple IRQs and assumes all
  509. * the pins on the gpiochip can generate a unique IRQ. Everything else
  510. * need to be open coded.
  511. */
  512. int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  513. struct irq_chip *irqchip,
  514. unsigned int first_irq,
  515. irq_flow_handler_t handler,
  516. unsigned int type,
  517. struct lock_class_key *lock_key)
  518. {
  519. struct device_node *of_node;
  520. unsigned int offset;
  521. unsigned irq_base = 0;
  522. if (!gpiochip || !irqchip)
  523. return -EINVAL;
  524. if (!gpiochip->dev) {
  525. pr_err("missing gpiochip .dev parent pointer\n");
  526. return -EINVAL;
  527. }
  528. of_node = gpiochip->dev->of_node;
  529. #ifdef CONFIG_OF_GPIO
  530. /*
  531. * If the gpiochip has an assigned OF node this takes precedence
  532. * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
  533. */
  534. if (gpiochip->of_node)
  535. of_node = gpiochip->of_node;
  536. #endif
  537. gpiochip->irqchip = irqchip;
  538. gpiochip->irq_handler = handler;
  539. gpiochip->irq_default_type = type;
  540. gpiochip->to_irq = gpiochip_to_irq;
  541. gpiochip->lock_key = lock_key;
  542. gpiochip->irqdomain = irq_domain_add_simple(of_node,
  543. gpiochip->ngpio, first_irq,
  544. &gpiochip_domain_ops, gpiochip);
  545. if (!gpiochip->irqdomain) {
  546. gpiochip->irqchip = NULL;
  547. return -EINVAL;
  548. }
  549. /*
  550. * It is possible for a driver to override this, but only if the
  551. * alternative functions are both implemented.
  552. */
  553. if (!irqchip->irq_request_resources &&
  554. !irqchip->irq_release_resources) {
  555. irqchip->irq_request_resources = gpiochip_irq_reqres;
  556. irqchip->irq_release_resources = gpiochip_irq_relres;
  557. }
  558. /*
  559. * Prepare the mapping since the irqchip shall be orthogonal to
  560. * any gpiochip calls. If the first_irq was zero, this is
  561. * necessary to allocate descriptors for all IRQs.
  562. */
  563. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  564. irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
  565. if (offset == 0)
  566. /*
  567. * Store the base into the gpiochip to be used when
  568. * unmapping the irqs.
  569. */
  570. gpiochip->irq_base = irq_base;
  571. }
  572. acpi_gpiochip_request_interrupts(gpiochip);
  573. return 0;
  574. }
  575. EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
  576. #else /* CONFIG_GPIOLIB_IRQCHIP */
  577. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
  578. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  579. #ifdef CONFIG_PINCTRL
  580. /**
  581. * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
  582. * @chip: the gpiochip to add the range for
  583. * @pctldev: the pin controller to map to
  584. * @gpio_offset: the start offset in the current gpio_chip number space
  585. * @pin_group: name of the pin group inside the pin controller
  586. */
  587. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  588. struct pinctrl_dev *pctldev,
  589. unsigned int gpio_offset, const char *pin_group)
  590. {
  591. struct gpio_pin_range *pin_range;
  592. int ret;
  593. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  594. if (!pin_range) {
  595. chip_err(chip, "failed to allocate pin ranges\n");
  596. return -ENOMEM;
  597. }
  598. /* Use local offset as range ID */
  599. pin_range->range.id = gpio_offset;
  600. pin_range->range.gc = chip;
  601. pin_range->range.name = chip->label;
  602. pin_range->range.base = chip->base + gpio_offset;
  603. pin_range->pctldev = pctldev;
  604. ret = pinctrl_get_group_pins(pctldev, pin_group,
  605. &pin_range->range.pins,
  606. &pin_range->range.npins);
  607. if (ret < 0) {
  608. kfree(pin_range);
  609. return ret;
  610. }
  611. pinctrl_add_gpio_range(pctldev, &pin_range->range);
  612. chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
  613. gpio_offset, gpio_offset + pin_range->range.npins - 1,
  614. pinctrl_dev_get_devname(pctldev), pin_group);
  615. list_add_tail(&pin_range->node, &chip->pin_ranges);
  616. return 0;
  617. }
  618. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  619. /**
  620. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  621. * @chip: the gpiochip to add the range for
  622. * @pinctrl_name: the dev_name() of the pin controller to map to
  623. * @gpio_offset: the start offset in the current gpio_chip number space
  624. * @pin_offset: the start offset in the pin controller number space
  625. * @npins: the number of pins from the offset of each pin space (GPIO and
  626. * pin controller) to accumulate in this range
  627. */
  628. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  629. unsigned int gpio_offset, unsigned int pin_offset,
  630. unsigned int npins)
  631. {
  632. struct gpio_pin_range *pin_range;
  633. int ret;
  634. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  635. if (!pin_range) {
  636. chip_err(chip, "failed to allocate pin ranges\n");
  637. return -ENOMEM;
  638. }
  639. /* Use local offset as range ID */
  640. pin_range->range.id = gpio_offset;
  641. pin_range->range.gc = chip;
  642. pin_range->range.name = chip->label;
  643. pin_range->range.base = chip->base + gpio_offset;
  644. pin_range->range.pin_base = pin_offset;
  645. pin_range->range.npins = npins;
  646. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  647. &pin_range->range);
  648. if (IS_ERR(pin_range->pctldev)) {
  649. ret = PTR_ERR(pin_range->pctldev);
  650. chip_err(chip, "could not create pin range\n");
  651. kfree(pin_range);
  652. return ret;
  653. }
  654. chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
  655. gpio_offset, gpio_offset + npins - 1,
  656. pinctl_name,
  657. pin_offset, pin_offset + npins - 1);
  658. list_add_tail(&pin_range->node, &chip->pin_ranges);
  659. return 0;
  660. }
  661. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  662. /**
  663. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  664. * @chip: the chip to remove all the mappings for
  665. */
  666. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  667. {
  668. struct gpio_pin_range *pin_range, *tmp;
  669. list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
  670. list_del(&pin_range->node);
  671. pinctrl_remove_gpio_range(pin_range->pctldev,
  672. &pin_range->range);
  673. kfree(pin_range);
  674. }
  675. }
  676. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  677. #endif /* CONFIG_PINCTRL */
  678. /* These "optional" allocation calls help prevent drivers from stomping
  679. * on each other, and help provide better diagnostics in debugfs.
  680. * They're called even less than the "set direction" calls.
  681. */
  682. static int __gpiod_request(struct gpio_desc *desc, const char *label)
  683. {
  684. struct gpio_chip *chip = desc->chip;
  685. int status;
  686. unsigned long flags;
  687. spin_lock_irqsave(&gpio_lock, flags);
  688. /* NOTE: gpio_request() can be called in early boot,
  689. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  690. */
  691. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  692. desc_set_label(desc, label ? : "?");
  693. status = 0;
  694. } else {
  695. status = -EBUSY;
  696. goto done;
  697. }
  698. if (chip->request) {
  699. /* chip->request may sleep */
  700. spin_unlock_irqrestore(&gpio_lock, flags);
  701. status = chip->request(chip, gpio_chip_hwgpio(desc));
  702. spin_lock_irqsave(&gpio_lock, flags);
  703. if (status < 0) {
  704. desc_set_label(desc, NULL);
  705. clear_bit(FLAG_REQUESTED, &desc->flags);
  706. goto done;
  707. }
  708. }
  709. if (chip->get_direction) {
  710. /* chip->get_direction may sleep */
  711. spin_unlock_irqrestore(&gpio_lock, flags);
  712. gpiod_get_direction(desc);
  713. spin_lock_irqsave(&gpio_lock, flags);
  714. }
  715. done:
  716. spin_unlock_irqrestore(&gpio_lock, flags);
  717. return status;
  718. }
  719. int gpiod_request(struct gpio_desc *desc, const char *label)
  720. {
  721. int status = -EPROBE_DEFER;
  722. struct gpio_chip *chip;
  723. if (!desc) {
  724. pr_warn("%s: invalid GPIO\n", __func__);
  725. return -EINVAL;
  726. }
  727. chip = desc->chip;
  728. if (!chip)
  729. goto done;
  730. if (try_module_get(chip->owner)) {
  731. status = __gpiod_request(desc, label);
  732. if (status < 0)
  733. module_put(chip->owner);
  734. }
  735. done:
  736. if (status)
  737. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  738. return status;
  739. }
  740. static bool __gpiod_free(struct gpio_desc *desc)
  741. {
  742. bool ret = false;
  743. unsigned long flags;
  744. struct gpio_chip *chip;
  745. might_sleep();
  746. gpiod_unexport(desc);
  747. spin_lock_irqsave(&gpio_lock, flags);
  748. chip = desc->chip;
  749. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  750. if (chip->free) {
  751. spin_unlock_irqrestore(&gpio_lock, flags);
  752. might_sleep_if(chip->can_sleep);
  753. chip->free(chip, gpio_chip_hwgpio(desc));
  754. spin_lock_irqsave(&gpio_lock, flags);
  755. }
  756. desc_set_label(desc, NULL);
  757. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  758. clear_bit(FLAG_REQUESTED, &desc->flags);
  759. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  760. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  761. clear_bit(FLAG_IS_HOGGED, &desc->flags);
  762. ret = true;
  763. }
  764. spin_unlock_irqrestore(&gpio_lock, flags);
  765. return ret;
  766. }
  767. void gpiod_free(struct gpio_desc *desc)
  768. {
  769. if (desc && __gpiod_free(desc))
  770. module_put(desc->chip->owner);
  771. else
  772. WARN_ON(extra_checks);
  773. }
  774. /**
  775. * gpiochip_is_requested - return string iff signal was requested
  776. * @chip: controller managing the signal
  777. * @offset: of signal within controller's 0..(ngpio - 1) range
  778. *
  779. * Returns NULL if the GPIO is not currently requested, else a string.
  780. * The string returned is the label passed to gpio_request(); if none has been
  781. * passed it is a meaningless, non-NULL constant.
  782. *
  783. * This function is for use by GPIO controller drivers. The label can
  784. * help with diagnostics, and knowing that the signal is used as a GPIO
  785. * can help avoid accidentally multiplexing it to another controller.
  786. */
  787. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  788. {
  789. struct gpio_desc *desc;
  790. if (!GPIO_OFFSET_VALID(chip, offset))
  791. return NULL;
  792. desc = &chip->desc[offset];
  793. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  794. return NULL;
  795. return desc->label;
  796. }
  797. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  798. /**
  799. * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
  800. * @desc: GPIO descriptor to request
  801. * @label: label for the GPIO
  802. *
  803. * Function allows GPIO chip drivers to request and use their own GPIO
  804. * descriptors via gpiolib API. Difference to gpiod_request() is that this
  805. * function will not increase reference count of the GPIO chip module. This
  806. * allows the GPIO chip module to be unloaded as needed (we assume that the
  807. * GPIO chip driver handles freeing the GPIOs it has requested).
  808. */
  809. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  810. const char *label)
  811. {
  812. struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
  813. int err;
  814. if (IS_ERR(desc)) {
  815. chip_err(chip, "failed to get GPIO descriptor\n");
  816. return desc;
  817. }
  818. err = __gpiod_request(desc, label);
  819. if (err < 0)
  820. return ERR_PTR(err);
  821. return desc;
  822. }
  823. EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
  824. /**
  825. * gpiochip_free_own_desc - Free GPIO requested by the chip driver
  826. * @desc: GPIO descriptor to free
  827. *
  828. * Function frees the given GPIO requested previously with
  829. * gpiochip_request_own_desc().
  830. */
  831. void gpiochip_free_own_desc(struct gpio_desc *desc)
  832. {
  833. if (desc)
  834. __gpiod_free(desc);
  835. }
  836. EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
  837. /* Drivers MUST set GPIO direction before making get/set calls. In
  838. * some cases this is done in early boot, before IRQs are enabled.
  839. *
  840. * As a rule these aren't called more than once (except for drivers
  841. * using the open-drain emulation idiom) so these are natural places
  842. * to accumulate extra debugging checks. Note that we can't (yet)
  843. * rely on gpio_request() having been called beforehand.
  844. */
  845. /**
  846. * gpiod_direction_input - set the GPIO direction to input
  847. * @desc: GPIO to set to input
  848. *
  849. * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
  850. * be called safely on it.
  851. *
  852. * Return 0 in case of success, else an error code.
  853. */
  854. int gpiod_direction_input(struct gpio_desc *desc)
  855. {
  856. struct gpio_chip *chip;
  857. int status = -EINVAL;
  858. if (!desc || !desc->chip) {
  859. pr_warn("%s: invalid GPIO\n", __func__);
  860. return -EINVAL;
  861. }
  862. chip = desc->chip;
  863. if (!chip->get || !chip->direction_input) {
  864. gpiod_warn(desc,
  865. "%s: missing get() or direction_input() operations\n",
  866. __func__);
  867. return -EIO;
  868. }
  869. status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
  870. if (status == 0)
  871. clear_bit(FLAG_IS_OUT, &desc->flags);
  872. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  873. return status;
  874. }
  875. EXPORT_SYMBOL_GPL(gpiod_direction_input);
  876. static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  877. {
  878. struct gpio_chip *chip;
  879. int status = -EINVAL;
  880. /* GPIOs used for IRQs shall not be set as output */
  881. if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
  882. gpiod_err(desc,
  883. "%s: tried to set a GPIO tied to an IRQ as output\n",
  884. __func__);
  885. return -EIO;
  886. }
  887. /* Open drain pin should not be driven to 1 */
  888. if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  889. return gpiod_direction_input(desc);
  890. /* Open source pin should not be driven to 0 */
  891. if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  892. return gpiod_direction_input(desc);
  893. chip = desc->chip;
  894. if (!chip->set || !chip->direction_output) {
  895. gpiod_warn(desc,
  896. "%s: missing set() or direction_output() operations\n",
  897. __func__);
  898. return -EIO;
  899. }
  900. status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
  901. if (status == 0)
  902. set_bit(FLAG_IS_OUT, &desc->flags);
  903. trace_gpio_value(desc_to_gpio(desc), 0, value);
  904. trace_gpio_direction(desc_to_gpio(desc), 0, status);
  905. return status;
  906. }
  907. /**
  908. * gpiod_direction_output_raw - set the GPIO direction to output
  909. * @desc: GPIO to set to output
  910. * @value: initial output value of the GPIO
  911. *
  912. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  913. * be called safely on it. The initial value of the output must be specified
  914. * as raw value on the physical line without regard for the ACTIVE_LOW status.
  915. *
  916. * Return 0 in case of success, else an error code.
  917. */
  918. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  919. {
  920. if (!desc || !desc->chip) {
  921. pr_warn("%s: invalid GPIO\n", __func__);
  922. return -EINVAL;
  923. }
  924. return _gpiod_direction_output_raw(desc, value);
  925. }
  926. EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
  927. /**
  928. * gpiod_direction_output - set the GPIO direction to output
  929. * @desc: GPIO to set to output
  930. * @value: initial output value of the GPIO
  931. *
  932. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  933. * be called safely on it. The initial value of the output must be specified
  934. * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  935. * account.
  936. *
  937. * Return 0 in case of success, else an error code.
  938. */
  939. int gpiod_direction_output(struct gpio_desc *desc, int value)
  940. {
  941. if (!desc || !desc->chip) {
  942. pr_warn("%s: invalid GPIO\n", __func__);
  943. return -EINVAL;
  944. }
  945. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  946. value = !value;
  947. return _gpiod_direction_output_raw(desc, value);
  948. }
  949. EXPORT_SYMBOL_GPL(gpiod_direction_output);
  950. /**
  951. * gpiod_set_debounce - sets @debounce time for a @gpio
  952. * @gpio: the gpio to set debounce time
  953. * @debounce: debounce time is microseconds
  954. *
  955. * returns -ENOTSUPP if the controller does not support setting
  956. * debounce.
  957. */
  958. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  959. {
  960. struct gpio_chip *chip;
  961. if (!desc || !desc->chip) {
  962. pr_warn("%s: invalid GPIO\n", __func__);
  963. return -EINVAL;
  964. }
  965. chip = desc->chip;
  966. if (!chip->set || !chip->set_debounce) {
  967. gpiod_dbg(desc,
  968. "%s: missing set() or set_debounce() operations\n",
  969. __func__);
  970. return -ENOTSUPP;
  971. }
  972. return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
  973. }
  974. EXPORT_SYMBOL_GPL(gpiod_set_debounce);
  975. /**
  976. * gpiod_is_active_low - test whether a GPIO is active-low or not
  977. * @desc: the gpio descriptor to test
  978. *
  979. * Returns 1 if the GPIO is active-low, 0 otherwise.
  980. */
  981. int gpiod_is_active_low(const struct gpio_desc *desc)
  982. {
  983. return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  984. }
  985. EXPORT_SYMBOL_GPL(gpiod_is_active_low);
  986. /* I/O calls are only valid after configuration completed; the relevant
  987. * "is this a valid GPIO" error checks should already have been done.
  988. *
  989. * "Get" operations are often inlinable as reading a pin value register,
  990. * and masking the relevant bit in that register.
  991. *
  992. * When "set" operations are inlinable, they involve writing that mask to
  993. * one register to set a low value, or a different register to set it high.
  994. * Otherwise locking is needed, so there may be little value to inlining.
  995. *
  996. *------------------------------------------------------------------------
  997. *
  998. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  999. * have requested the GPIO. That can include implicit requesting by
  1000. * a direction setting call. Marking a gpio as requested locks its chip
  1001. * in memory, guaranteeing that these table lookups need no more locking
  1002. * and that gpiochip_remove() will fail.
  1003. *
  1004. * REVISIT when debugging, consider adding some instrumentation to ensure
  1005. * that the GPIO was actually requested.
  1006. */
  1007. static int _gpiod_get_raw_value(const struct gpio_desc *desc)
  1008. {
  1009. struct gpio_chip *chip;
  1010. int offset;
  1011. int value;
  1012. chip = desc->chip;
  1013. offset = gpio_chip_hwgpio(desc);
  1014. value = chip->get ? chip->get(chip, offset) : -EIO;
  1015. value = value < 0 ? value : !!value;
  1016. trace_gpio_value(desc_to_gpio(desc), 1, value);
  1017. return value;
  1018. }
  1019. /**
  1020. * gpiod_get_raw_value() - return a gpio's raw value
  1021. * @desc: gpio whose value will be returned
  1022. *
  1023. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  1024. * its ACTIVE_LOW status, or negative errno on failure.
  1025. *
  1026. * This function should be called from contexts where we cannot sleep, and will
  1027. * complain if the GPIO chip functions potentially sleep.
  1028. */
  1029. int gpiod_get_raw_value(const struct gpio_desc *desc)
  1030. {
  1031. if (!desc)
  1032. return 0;
  1033. /* Should be using gpio_get_value_cansleep() */
  1034. WARN_ON(desc->chip->can_sleep);
  1035. return _gpiod_get_raw_value(desc);
  1036. }
  1037. EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
  1038. /**
  1039. * gpiod_get_value() - return a gpio's value
  1040. * @desc: gpio whose value will be returned
  1041. *
  1042. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  1043. * account, or negative errno on failure.
  1044. *
  1045. * This function should be called from contexts where we cannot sleep, and will
  1046. * complain if the GPIO chip functions potentially sleep.
  1047. */
  1048. int gpiod_get_value(const struct gpio_desc *desc)
  1049. {
  1050. int value;
  1051. if (!desc)
  1052. return 0;
  1053. /* Should be using gpio_get_value_cansleep() */
  1054. WARN_ON(desc->chip->can_sleep);
  1055. value = _gpiod_get_raw_value(desc);
  1056. if (value < 0)
  1057. return value;
  1058. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1059. value = !value;
  1060. return value;
  1061. }
  1062. EXPORT_SYMBOL_GPL(gpiod_get_value);
  1063. /*
  1064. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  1065. * @desc: gpio descriptor whose state need to be set.
  1066. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  1067. */
  1068. static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
  1069. {
  1070. int err = 0;
  1071. struct gpio_chip *chip = desc->chip;
  1072. int offset = gpio_chip_hwgpio(desc);
  1073. if (value) {
  1074. err = chip->direction_input(chip, offset);
  1075. if (!err)
  1076. clear_bit(FLAG_IS_OUT, &desc->flags);
  1077. } else {
  1078. err = chip->direction_output(chip, offset, 0);
  1079. if (!err)
  1080. set_bit(FLAG_IS_OUT, &desc->flags);
  1081. }
  1082. trace_gpio_direction(desc_to_gpio(desc), value, err);
  1083. if (err < 0)
  1084. gpiod_err(desc,
  1085. "%s: Error in set_value for open drain err %d\n",
  1086. __func__, err);
  1087. }
  1088. /*
  1089. * _gpio_set_open_source_value() - Set the open source gpio's value.
  1090. * @desc: gpio descriptor whose state need to be set.
  1091. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  1092. */
  1093. static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
  1094. {
  1095. int err = 0;
  1096. struct gpio_chip *chip = desc->chip;
  1097. int offset = gpio_chip_hwgpio(desc);
  1098. if (value) {
  1099. err = chip->direction_output(chip, offset, 1);
  1100. if (!err)
  1101. set_bit(FLAG_IS_OUT, &desc->flags);
  1102. } else {
  1103. err = chip->direction_input(chip, offset);
  1104. if (!err)
  1105. clear_bit(FLAG_IS_OUT, &desc->flags);
  1106. }
  1107. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  1108. if (err < 0)
  1109. gpiod_err(desc,
  1110. "%s: Error in set_value for open source err %d\n",
  1111. __func__, err);
  1112. }
  1113. static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
  1114. {
  1115. struct gpio_chip *chip;
  1116. chip = desc->chip;
  1117. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1118. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1119. _gpio_set_open_drain_value(desc, value);
  1120. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1121. _gpio_set_open_source_value(desc, value);
  1122. else
  1123. chip->set(chip, gpio_chip_hwgpio(desc), value);
  1124. }
  1125. /*
  1126. * set multiple outputs on the same chip;
  1127. * use the chip's set_multiple function if available;
  1128. * otherwise set the outputs sequentially;
  1129. * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
  1130. * defines which outputs are to be changed
  1131. * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
  1132. * defines the values the outputs specified by mask are to be set to
  1133. */
  1134. static void gpio_chip_set_multiple(struct gpio_chip *chip,
  1135. unsigned long *mask, unsigned long *bits)
  1136. {
  1137. if (chip->set_multiple) {
  1138. chip->set_multiple(chip, mask, bits);
  1139. } else {
  1140. int i;
  1141. for (i = 0; i < chip->ngpio; i++) {
  1142. if (mask[BIT_WORD(i)] == 0) {
  1143. /* no more set bits in this mask word;
  1144. * skip ahead to the next word */
  1145. i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
  1146. continue;
  1147. }
  1148. /* set outputs if the corresponding mask bit is set */
  1149. if (__test_and_clear_bit(i, mask))
  1150. chip->set(chip, i, test_bit(i, bits));
  1151. }
  1152. }
  1153. }
  1154. static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
  1155. unsigned int array_size,
  1156. struct gpio_desc **desc_array,
  1157. int *value_array)
  1158. {
  1159. int i = 0;
  1160. while (i < array_size) {
  1161. struct gpio_chip *chip = desc_array[i]->chip;
  1162. unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
  1163. unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
  1164. int count = 0;
  1165. if (!can_sleep)
  1166. WARN_ON(chip->can_sleep);
  1167. memset(mask, 0, sizeof(mask));
  1168. do {
  1169. struct gpio_desc *desc = desc_array[i];
  1170. int hwgpio = gpio_chip_hwgpio(desc);
  1171. int value = value_array[i];
  1172. if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1173. value = !value;
  1174. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1175. /*
  1176. * collect all normal outputs belonging to the same chip
  1177. * open drain and open source outputs are set individually
  1178. */
  1179. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
  1180. _gpio_set_open_drain_value(desc, value);
  1181. } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
  1182. _gpio_set_open_source_value(desc, value);
  1183. } else {
  1184. __set_bit(hwgpio, mask);
  1185. if (value)
  1186. __set_bit(hwgpio, bits);
  1187. else
  1188. __clear_bit(hwgpio, bits);
  1189. count++;
  1190. }
  1191. i++;
  1192. } while ((i < array_size) && (desc_array[i]->chip == chip));
  1193. /* push collected bits to outputs */
  1194. if (count != 0)
  1195. gpio_chip_set_multiple(chip, mask, bits);
  1196. }
  1197. }
  1198. /**
  1199. * gpiod_set_raw_value() - assign a gpio's raw value
  1200. * @desc: gpio whose value will be assigned
  1201. * @value: value to assign
  1202. *
  1203. * Set the raw value of the GPIO, i.e. the value of its physical line without
  1204. * regard for its ACTIVE_LOW status.
  1205. *
  1206. * This function should be called from contexts where we cannot sleep, and will
  1207. * complain if the GPIO chip functions potentially sleep.
  1208. */
  1209. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  1210. {
  1211. if (!desc)
  1212. return;
  1213. /* Should be using gpio_set_value_cansleep() */
  1214. WARN_ON(desc->chip->can_sleep);
  1215. _gpiod_set_raw_value(desc, value);
  1216. }
  1217. EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
  1218. /**
  1219. * gpiod_set_value() - assign a gpio's value
  1220. * @desc: gpio whose value will be assigned
  1221. * @value: value to assign
  1222. *
  1223. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1224. * account
  1225. *
  1226. * This function should be called from contexts where we cannot sleep, and will
  1227. * complain if the GPIO chip functions potentially sleep.
  1228. */
  1229. void gpiod_set_value(struct gpio_desc *desc, int value)
  1230. {
  1231. if (!desc)
  1232. return;
  1233. /* Should be using gpio_set_value_cansleep() */
  1234. WARN_ON(desc->chip->can_sleep);
  1235. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1236. value = !value;
  1237. _gpiod_set_raw_value(desc, value);
  1238. }
  1239. EXPORT_SYMBOL_GPL(gpiod_set_value);
  1240. /**
  1241. * gpiod_set_raw_array_value() - assign values to an array of GPIOs
  1242. * @array_size: number of elements in the descriptor / value arrays
  1243. * @desc_array: array of GPIO descriptors whose values will be assigned
  1244. * @value_array: array of values to assign
  1245. *
  1246. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  1247. * without regard for their ACTIVE_LOW status.
  1248. *
  1249. * This function should be called from contexts where we cannot sleep, and will
  1250. * complain if the GPIO chip functions potentially sleep.
  1251. */
  1252. void gpiod_set_raw_array_value(unsigned int array_size,
  1253. struct gpio_desc **desc_array, int *value_array)
  1254. {
  1255. if (!desc_array)
  1256. return;
  1257. gpiod_set_array_value_priv(true, false, array_size, desc_array,
  1258. value_array);
  1259. }
  1260. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
  1261. /**
  1262. * gpiod_set_array_value() - assign values to an array of GPIOs
  1263. * @array_size: number of elements in the descriptor / value arrays
  1264. * @desc_array: array of GPIO descriptors whose values will be assigned
  1265. * @value_array: array of values to assign
  1266. *
  1267. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  1268. * into account.
  1269. *
  1270. * This function should be called from contexts where we cannot sleep, and will
  1271. * complain if the GPIO chip functions potentially sleep.
  1272. */
  1273. void gpiod_set_array_value(unsigned int array_size,
  1274. struct gpio_desc **desc_array, int *value_array)
  1275. {
  1276. if (!desc_array)
  1277. return;
  1278. gpiod_set_array_value_priv(false, false, array_size, desc_array,
  1279. value_array);
  1280. }
  1281. EXPORT_SYMBOL_GPL(gpiod_set_array_value);
  1282. /**
  1283. * gpiod_cansleep() - report whether gpio value access may sleep
  1284. * @desc: gpio to check
  1285. *
  1286. */
  1287. int gpiod_cansleep(const struct gpio_desc *desc)
  1288. {
  1289. if (!desc)
  1290. return 0;
  1291. return desc->chip->can_sleep;
  1292. }
  1293. EXPORT_SYMBOL_GPL(gpiod_cansleep);
  1294. /**
  1295. * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  1296. * @desc: gpio whose IRQ will be returned (already requested)
  1297. *
  1298. * Return the IRQ corresponding to the passed GPIO, or an error code in case of
  1299. * error.
  1300. */
  1301. int gpiod_to_irq(const struct gpio_desc *desc)
  1302. {
  1303. struct gpio_chip *chip;
  1304. int offset;
  1305. if (!desc)
  1306. return -EINVAL;
  1307. chip = desc->chip;
  1308. offset = gpio_chip_hwgpio(desc);
  1309. return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
  1310. }
  1311. EXPORT_SYMBOL_GPL(gpiod_to_irq);
  1312. /**
  1313. * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
  1314. * @chip: the chip the GPIO to lock belongs to
  1315. * @offset: the offset of the GPIO to lock as IRQ
  1316. *
  1317. * This is used directly by GPIO drivers that want to lock down
  1318. * a certain GPIO line to be used for IRQs.
  1319. */
  1320. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
  1321. {
  1322. if (offset >= chip->ngpio)
  1323. return -EINVAL;
  1324. if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
  1325. chip_err(chip,
  1326. "%s: tried to flag a GPIO set as output for IRQ\n",
  1327. __func__);
  1328. return -EIO;
  1329. }
  1330. set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
  1331. return 0;
  1332. }
  1333. EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
  1334. /**
  1335. * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
  1336. * @chip: the chip the GPIO to lock belongs to
  1337. * @offset: the offset of the GPIO to lock as IRQ
  1338. *
  1339. * This is used directly by GPIO drivers that want to indicate
  1340. * that a certain GPIO is no longer used exclusively for IRQ.
  1341. */
  1342. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
  1343. {
  1344. if (offset >= chip->ngpio)
  1345. return;
  1346. clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
  1347. }
  1348. EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
  1349. /**
  1350. * gpiod_get_raw_value_cansleep() - return a gpio's raw value
  1351. * @desc: gpio whose value will be returned
  1352. *
  1353. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  1354. * its ACTIVE_LOW status, or negative errno on failure.
  1355. *
  1356. * This function is to be called from contexts that can sleep.
  1357. */
  1358. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  1359. {
  1360. might_sleep_if(extra_checks);
  1361. if (!desc)
  1362. return 0;
  1363. return _gpiod_get_raw_value(desc);
  1364. }
  1365. EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
  1366. /**
  1367. * gpiod_get_value_cansleep() - return a gpio's value
  1368. * @desc: gpio whose value will be returned
  1369. *
  1370. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  1371. * account, or negative errno on failure.
  1372. *
  1373. * This function is to be called from contexts that can sleep.
  1374. */
  1375. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  1376. {
  1377. int value;
  1378. might_sleep_if(extra_checks);
  1379. if (!desc)
  1380. return 0;
  1381. value = _gpiod_get_raw_value(desc);
  1382. if (value < 0)
  1383. return value;
  1384. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1385. value = !value;
  1386. return value;
  1387. }
  1388. EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
  1389. /**
  1390. * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
  1391. * @desc: gpio whose value will be assigned
  1392. * @value: value to assign
  1393. *
  1394. * Set the raw value of the GPIO, i.e. the value of its physical line without
  1395. * regard for its ACTIVE_LOW status.
  1396. *
  1397. * This function is to be called from contexts that can sleep.
  1398. */
  1399. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  1400. {
  1401. might_sleep_if(extra_checks);
  1402. if (!desc)
  1403. return;
  1404. _gpiod_set_raw_value(desc, value);
  1405. }
  1406. EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
  1407. /**
  1408. * gpiod_set_value_cansleep() - assign a gpio's value
  1409. * @desc: gpio whose value will be assigned
  1410. * @value: value to assign
  1411. *
  1412. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1413. * account
  1414. *
  1415. * This function is to be called from contexts that can sleep.
  1416. */
  1417. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  1418. {
  1419. might_sleep_if(extra_checks);
  1420. if (!desc)
  1421. return;
  1422. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1423. value = !value;
  1424. _gpiod_set_raw_value(desc, value);
  1425. }
  1426. EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
  1427. /**
  1428. * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
  1429. * @array_size: number of elements in the descriptor / value arrays
  1430. * @desc_array: array of GPIO descriptors whose values will be assigned
  1431. * @value_array: array of values to assign
  1432. *
  1433. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  1434. * without regard for their ACTIVE_LOW status.
  1435. *
  1436. * This function is to be called from contexts that can sleep.
  1437. */
  1438. void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  1439. struct gpio_desc **desc_array,
  1440. int *value_array)
  1441. {
  1442. might_sleep_if(extra_checks);
  1443. if (!desc_array)
  1444. return;
  1445. gpiod_set_array_value_priv(true, true, array_size, desc_array,
  1446. value_array);
  1447. }
  1448. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
  1449. /**
  1450. * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
  1451. * @array_size: number of elements in the descriptor / value arrays
  1452. * @desc_array: array of GPIO descriptors whose values will be assigned
  1453. * @value_array: array of values to assign
  1454. *
  1455. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  1456. * into account.
  1457. *
  1458. * This function is to be called from contexts that can sleep.
  1459. */
  1460. void gpiod_set_array_value_cansleep(unsigned int array_size,
  1461. struct gpio_desc **desc_array,
  1462. int *value_array)
  1463. {
  1464. might_sleep_if(extra_checks);
  1465. if (!desc_array)
  1466. return;
  1467. gpiod_set_array_value_priv(false, true, array_size, desc_array,
  1468. value_array);
  1469. }
  1470. EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
  1471. /**
  1472. * gpiod_add_lookup_table() - register GPIO device consumers
  1473. * @table: table of consumers to register
  1474. */
  1475. void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
  1476. {
  1477. mutex_lock(&gpio_lookup_lock);
  1478. list_add_tail(&table->list, &gpio_lookup_list);
  1479. mutex_unlock(&gpio_lookup_lock);
  1480. }
  1481. /**
  1482. * gpiod_remove_lookup_table() - unregister GPIO device consumers
  1483. * @table: table of consumers to unregister
  1484. */
  1485. void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
  1486. {
  1487. mutex_lock(&gpio_lookup_lock);
  1488. list_del(&table->list);
  1489. mutex_unlock(&gpio_lookup_lock);
  1490. }
  1491. static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  1492. unsigned int idx,
  1493. enum gpio_lookup_flags *flags)
  1494. {
  1495. char prop_name[32]; /* 32 is max size of property name */
  1496. enum of_gpio_flags of_flags;
  1497. struct gpio_desc *desc;
  1498. unsigned int i;
  1499. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  1500. if (con_id)
  1501. snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
  1502. gpio_suffixes[i]);
  1503. else
  1504. snprintf(prop_name, sizeof(prop_name), "%s",
  1505. gpio_suffixes[i]);
  1506. desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
  1507. &of_flags);
  1508. if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
  1509. break;
  1510. }
  1511. if (IS_ERR(desc))
  1512. return desc;
  1513. if (of_flags & OF_GPIO_ACTIVE_LOW)
  1514. *flags |= GPIO_ACTIVE_LOW;
  1515. return desc;
  1516. }
  1517. static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
  1518. unsigned int idx,
  1519. enum gpio_lookup_flags *flags)
  1520. {
  1521. struct acpi_device *adev = ACPI_COMPANION(dev);
  1522. struct acpi_gpio_info info;
  1523. struct gpio_desc *desc;
  1524. char propname[32];
  1525. int i;
  1526. /* Try first from _DSD */
  1527. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  1528. if (con_id && strcmp(con_id, "gpios")) {
  1529. snprintf(propname, sizeof(propname), "%s-%s",
  1530. con_id, gpio_suffixes[i]);
  1531. } else {
  1532. snprintf(propname, sizeof(propname), "%s",
  1533. gpio_suffixes[i]);
  1534. }
  1535. desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
  1536. if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
  1537. break;
  1538. }
  1539. /* Then from plain _CRS GPIOs */
  1540. if (IS_ERR(desc)) {
  1541. desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
  1542. if (IS_ERR(desc))
  1543. return desc;
  1544. }
  1545. if (info.active_low)
  1546. *flags |= GPIO_ACTIVE_LOW;
  1547. return desc;
  1548. }
  1549. static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
  1550. {
  1551. const char *dev_id = dev ? dev_name(dev) : NULL;
  1552. struct gpiod_lookup_table *table;
  1553. mutex_lock(&gpio_lookup_lock);
  1554. list_for_each_entry(table, &gpio_lookup_list, list) {
  1555. if (table->dev_id && dev_id) {
  1556. /*
  1557. * Valid strings on both ends, must be identical to have
  1558. * a match
  1559. */
  1560. if (!strcmp(table->dev_id, dev_id))
  1561. goto found;
  1562. } else {
  1563. /*
  1564. * One of the pointers is NULL, so both must be to have
  1565. * a match
  1566. */
  1567. if (dev_id == table->dev_id)
  1568. goto found;
  1569. }
  1570. }
  1571. table = NULL;
  1572. found:
  1573. mutex_unlock(&gpio_lookup_lock);
  1574. return table;
  1575. }
  1576. static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
  1577. unsigned int idx,
  1578. enum gpio_lookup_flags *flags)
  1579. {
  1580. struct gpio_desc *desc = ERR_PTR(-ENOENT);
  1581. struct gpiod_lookup_table *table;
  1582. struct gpiod_lookup *p;
  1583. table = gpiod_find_lookup_table(dev);
  1584. if (!table)
  1585. return desc;
  1586. for (p = &table->table[0]; p->chip_label; p++) {
  1587. struct gpio_chip *chip;
  1588. /* idx must always match exactly */
  1589. if (p->idx != idx)
  1590. continue;
  1591. /* If the lookup entry has a con_id, require exact match */
  1592. if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
  1593. continue;
  1594. chip = find_chip_by_name(p->chip_label);
  1595. if (!chip) {
  1596. dev_err(dev, "cannot find GPIO chip %s\n",
  1597. p->chip_label);
  1598. return ERR_PTR(-ENODEV);
  1599. }
  1600. if (chip->ngpio <= p->chip_hwnum) {
  1601. dev_err(dev,
  1602. "requested GPIO %d is out of range [0..%d] for chip %s\n",
  1603. idx, chip->ngpio, chip->label);
  1604. return ERR_PTR(-EINVAL);
  1605. }
  1606. desc = gpiochip_get_desc(chip, p->chip_hwnum);
  1607. *flags = p->flags;
  1608. return desc;
  1609. }
  1610. return desc;
  1611. }
  1612. static int dt_gpio_count(struct device *dev, const char *con_id)
  1613. {
  1614. int ret;
  1615. char propname[32];
  1616. unsigned int i;
  1617. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  1618. if (con_id)
  1619. snprintf(propname, sizeof(propname), "%s-%s",
  1620. con_id, gpio_suffixes[i]);
  1621. else
  1622. snprintf(propname, sizeof(propname), "%s",
  1623. gpio_suffixes[i]);
  1624. ret = of_gpio_named_count(dev->of_node, propname);
  1625. if (ret >= 0)
  1626. break;
  1627. }
  1628. return ret;
  1629. }
  1630. static int platform_gpio_count(struct device *dev, const char *con_id)
  1631. {
  1632. struct gpiod_lookup_table *table;
  1633. struct gpiod_lookup *p;
  1634. unsigned int count = 0;
  1635. table = gpiod_find_lookup_table(dev);
  1636. if (!table)
  1637. return -ENOENT;
  1638. for (p = &table->table[0]; p->chip_label; p++) {
  1639. if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
  1640. (!con_id && !p->con_id))
  1641. count++;
  1642. }
  1643. if (!count)
  1644. return -ENOENT;
  1645. return count;
  1646. }
  1647. /**
  1648. * gpiod_count - return the number of GPIOs associated with a device / function
  1649. * or -ENOENT if no GPIO has been assigned to the requested function
  1650. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1651. * @con_id: function within the GPIO consumer
  1652. */
  1653. int gpiod_count(struct device *dev, const char *con_id)
  1654. {
  1655. int count = -ENOENT;
  1656. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  1657. count = dt_gpio_count(dev, con_id);
  1658. else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
  1659. count = acpi_gpio_count(dev, con_id);
  1660. if (count < 0)
  1661. count = platform_gpio_count(dev, con_id);
  1662. return count;
  1663. }
  1664. EXPORT_SYMBOL_GPL(gpiod_count);
  1665. /**
  1666. * gpiod_get - obtain a GPIO for a given GPIO function
  1667. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1668. * @con_id: function within the GPIO consumer
  1669. * @flags: optional GPIO initialization flags
  1670. *
  1671. * Return the GPIO descriptor corresponding to the function con_id of device
  1672. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  1673. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  1674. */
  1675. struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
  1676. enum gpiod_flags flags)
  1677. {
  1678. return gpiod_get_index(dev, con_id, 0, flags);
  1679. }
  1680. EXPORT_SYMBOL_GPL(gpiod_get);
  1681. /**
  1682. * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
  1683. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1684. * @con_id: function within the GPIO consumer
  1685. * @flags: optional GPIO initialization flags
  1686. *
  1687. * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
  1688. * the requested function it will return NULL. This is convenient for drivers
  1689. * that need to handle optional GPIOs.
  1690. */
  1691. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  1692. const char *con_id,
  1693. enum gpiod_flags flags)
  1694. {
  1695. return gpiod_get_index_optional(dev, con_id, 0, flags);
  1696. }
  1697. EXPORT_SYMBOL_GPL(gpiod_get_optional);
  1698. /**
  1699. * gpiod_configure_flags - helper function to configure a given GPIO
  1700. * @desc: gpio whose value will be assigned
  1701. * @con_id: function within the GPIO consumer
  1702. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  1703. * of_get_gpio_hog()
  1704. * @dflags: gpiod_flags - optional GPIO initialization flags
  1705. *
  1706. * Return 0 on success, -ENOENT if no GPIO has been assigned to the
  1707. * requested function and/or index, or another IS_ERR() code if an error
  1708. * occurred while trying to acquire the GPIO.
  1709. */
  1710. static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  1711. unsigned long lflags, enum gpiod_flags dflags)
  1712. {
  1713. int status;
  1714. if (lflags & GPIO_ACTIVE_LOW)
  1715. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1716. if (lflags & GPIO_OPEN_DRAIN)
  1717. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1718. if (lflags & GPIO_OPEN_SOURCE)
  1719. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1720. /* No particular flag request, return here... */
  1721. if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
  1722. pr_debug("no flags found for %s\n", con_id);
  1723. return 0;
  1724. }
  1725. /* Process flags */
  1726. if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
  1727. status = gpiod_direction_output(desc,
  1728. dflags & GPIOD_FLAGS_BIT_DIR_VAL);
  1729. else
  1730. status = gpiod_direction_input(desc);
  1731. return status;
  1732. }
  1733. /**
  1734. * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
  1735. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1736. * @con_id: function within the GPIO consumer
  1737. * @idx: index of the GPIO to obtain in the consumer
  1738. * @flags: optional GPIO initialization flags
  1739. *
  1740. * This variant of gpiod_get() allows to access GPIOs other than the first
  1741. * defined one for functions that define several GPIOs.
  1742. *
  1743. * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
  1744. * requested function and/or index, or another IS_ERR() code if an error
  1745. * occurred while trying to acquire the GPIO.
  1746. */
  1747. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  1748. const char *con_id,
  1749. unsigned int idx,
  1750. enum gpiod_flags flags)
  1751. {
  1752. struct gpio_desc *desc = NULL;
  1753. int status;
  1754. enum gpio_lookup_flags lookupflags = 0;
  1755. dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
  1756. if (dev) {
  1757. /* Using device tree? */
  1758. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  1759. dev_dbg(dev, "using device tree for GPIO lookup\n");
  1760. desc = of_find_gpio(dev, con_id, idx, &lookupflags);
  1761. } else if (ACPI_COMPANION(dev)) {
  1762. dev_dbg(dev, "using ACPI for GPIO lookup\n");
  1763. desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
  1764. }
  1765. }
  1766. /*
  1767. * Either we are not using DT or ACPI, or their lookup did not return
  1768. * a result. In that case, use platform lookup as a fallback.
  1769. */
  1770. if (!desc || desc == ERR_PTR(-ENOENT)) {
  1771. dev_dbg(dev, "using lookup tables for GPIO lookup\n");
  1772. desc = gpiod_find(dev, con_id, idx, &lookupflags);
  1773. }
  1774. if (IS_ERR(desc)) {
  1775. dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
  1776. return desc;
  1777. }
  1778. status = gpiod_request(desc, con_id);
  1779. if (status < 0)
  1780. return ERR_PTR(status);
  1781. status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
  1782. if (status < 0) {
  1783. dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
  1784. gpiod_put(desc);
  1785. return ERR_PTR(status);
  1786. }
  1787. return desc;
  1788. }
  1789. EXPORT_SYMBOL_GPL(gpiod_get_index);
  1790. /**
  1791. * fwnode_get_named_gpiod - obtain a GPIO from firmware node
  1792. * @fwnode: handle of the firmware node
  1793. * @propname: name of the firmware property representing the GPIO
  1794. *
  1795. * This function can be used for drivers that get their configuration
  1796. * from firmware.
  1797. *
  1798. * Function properly finds the corresponding GPIO using whatever is the
  1799. * underlying firmware interface and then makes sure that the GPIO
  1800. * descriptor is requested before it is returned to the caller.
  1801. *
  1802. * In case of error an ERR_PTR() is returned.
  1803. */
  1804. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  1805. const char *propname)
  1806. {
  1807. struct gpio_desc *desc = ERR_PTR(-ENODEV);
  1808. bool active_low = false;
  1809. int ret;
  1810. if (!fwnode)
  1811. return ERR_PTR(-EINVAL);
  1812. if (is_of_node(fwnode)) {
  1813. enum of_gpio_flags flags;
  1814. desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
  1815. &flags);
  1816. if (!IS_ERR(desc))
  1817. active_low = flags & OF_GPIO_ACTIVE_LOW;
  1818. } else if (is_acpi_node(fwnode)) {
  1819. struct acpi_gpio_info info;
  1820. desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0,
  1821. &info);
  1822. if (!IS_ERR(desc))
  1823. active_low = info.active_low;
  1824. }
  1825. if (IS_ERR(desc))
  1826. return desc;
  1827. ret = gpiod_request(desc, NULL);
  1828. if (ret)
  1829. return ERR_PTR(ret);
  1830. /* Only value flag can be set from both DT and ACPI is active_low */
  1831. if (active_low)
  1832. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1833. return desc;
  1834. }
  1835. EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
  1836. /**
  1837. * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
  1838. * function
  1839. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1840. * @con_id: function within the GPIO consumer
  1841. * @index: index of the GPIO to obtain in the consumer
  1842. * @flags: optional GPIO initialization flags
  1843. *
  1844. * This is equivalent to gpiod_get_index(), except that when no GPIO with the
  1845. * specified index was assigned to the requested function it will return NULL.
  1846. * This is convenient for drivers that need to handle optional GPIOs.
  1847. */
  1848. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  1849. const char *con_id,
  1850. unsigned int index,
  1851. enum gpiod_flags flags)
  1852. {
  1853. struct gpio_desc *desc;
  1854. desc = gpiod_get_index(dev, con_id, index, flags);
  1855. if (IS_ERR(desc)) {
  1856. if (PTR_ERR(desc) == -ENOENT)
  1857. return NULL;
  1858. }
  1859. return desc;
  1860. }
  1861. EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
  1862. /**
  1863. * gpiod_hog - Hog the specified GPIO desc given the provided flags
  1864. * @desc: gpio whose value will be assigned
  1865. * @name: gpio line name
  1866. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  1867. * of_get_gpio_hog()
  1868. * @dflags: gpiod_flags - optional GPIO initialization flags
  1869. */
  1870. int gpiod_hog(struct gpio_desc *desc, const char *name,
  1871. unsigned long lflags, enum gpiod_flags dflags)
  1872. {
  1873. struct gpio_chip *chip;
  1874. struct gpio_desc *local_desc;
  1875. int hwnum;
  1876. int status;
  1877. chip = gpiod_to_chip(desc);
  1878. hwnum = gpio_chip_hwgpio(desc);
  1879. local_desc = gpiochip_request_own_desc(chip, hwnum, name);
  1880. if (IS_ERR(local_desc)) {
  1881. pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
  1882. name, chip->label, hwnum);
  1883. return PTR_ERR(local_desc);
  1884. }
  1885. status = gpiod_configure_flags(desc, name, lflags, dflags);
  1886. if (status < 0) {
  1887. pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
  1888. name, chip->label, hwnum);
  1889. gpiochip_free_own_desc(desc);
  1890. return status;
  1891. }
  1892. /* Mark GPIO as hogged so it can be identified and removed later */
  1893. set_bit(FLAG_IS_HOGGED, &desc->flags);
  1894. pr_info("GPIO line %d (%s) hogged as %s%s\n",
  1895. desc_to_gpio(desc), name,
  1896. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
  1897. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
  1898. (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
  1899. return 0;
  1900. }
  1901. /**
  1902. * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
  1903. * @chip: gpio chip to act on
  1904. *
  1905. * This is only used by of_gpiochip_remove to free hogged gpios
  1906. */
  1907. static void gpiochip_free_hogs(struct gpio_chip *chip)
  1908. {
  1909. int id;
  1910. for (id = 0; id < chip->ngpio; id++) {
  1911. if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
  1912. gpiochip_free_own_desc(&chip->desc[id]);
  1913. }
  1914. }
  1915. /**
  1916. * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
  1917. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1918. * @con_id: function within the GPIO consumer
  1919. * @flags: optional GPIO initialization flags
  1920. *
  1921. * This function acquires all the GPIOs defined under a given function.
  1922. *
  1923. * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
  1924. * no GPIO has been assigned to the requested function, or another IS_ERR()
  1925. * code if an error occurred while trying to acquire the GPIOs.
  1926. */
  1927. struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
  1928. const char *con_id,
  1929. enum gpiod_flags flags)
  1930. {
  1931. struct gpio_desc *desc;
  1932. struct gpio_descs *descs;
  1933. int count;
  1934. count = gpiod_count(dev, con_id);
  1935. if (count < 0)
  1936. return ERR_PTR(count);
  1937. descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
  1938. GFP_KERNEL);
  1939. if (!descs)
  1940. return ERR_PTR(-ENOMEM);
  1941. for (descs->ndescs = 0; descs->ndescs < count; ) {
  1942. desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
  1943. if (IS_ERR(desc)) {
  1944. gpiod_put_array(descs);
  1945. return ERR_CAST(desc);
  1946. }
  1947. descs->desc[descs->ndescs] = desc;
  1948. descs->ndescs++;
  1949. }
  1950. return descs;
  1951. }
  1952. EXPORT_SYMBOL_GPL(gpiod_get_array);
  1953. /**
  1954. * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
  1955. * function
  1956. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1957. * @con_id: function within the GPIO consumer
  1958. * @flags: optional GPIO initialization flags
  1959. *
  1960. * This is equivalent to gpiod_get_array(), except that when no GPIO was
  1961. * assigned to the requested function it will return NULL.
  1962. */
  1963. struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
  1964. const char *con_id,
  1965. enum gpiod_flags flags)
  1966. {
  1967. struct gpio_descs *descs;
  1968. descs = gpiod_get_array(dev, con_id, flags);
  1969. if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
  1970. return NULL;
  1971. return descs;
  1972. }
  1973. EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
  1974. /**
  1975. * gpiod_put - dispose of a GPIO descriptor
  1976. * @desc: GPIO descriptor to dispose of
  1977. *
  1978. * No descriptor can be used after gpiod_put() has been called on it.
  1979. */
  1980. void gpiod_put(struct gpio_desc *desc)
  1981. {
  1982. gpiod_free(desc);
  1983. }
  1984. EXPORT_SYMBOL_GPL(gpiod_put);
  1985. /**
  1986. * gpiod_put_array - dispose of multiple GPIO descriptors
  1987. * @descs: struct gpio_descs containing an array of descriptors
  1988. */
  1989. void gpiod_put_array(struct gpio_descs *descs)
  1990. {
  1991. unsigned int i;
  1992. for (i = 0; i < descs->ndescs; i++)
  1993. gpiod_put(descs->desc[i]);
  1994. kfree(descs);
  1995. }
  1996. EXPORT_SYMBOL_GPL(gpiod_put_array);
  1997. #ifdef CONFIG_DEBUG_FS
  1998. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1999. {
  2000. unsigned i;
  2001. unsigned gpio = chip->base;
  2002. struct gpio_desc *gdesc = &chip->desc[0];
  2003. int is_out;
  2004. int is_irq;
  2005. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  2006. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  2007. continue;
  2008. gpiod_get_direction(gdesc);
  2009. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  2010. is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
  2011. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
  2012. gpio, gdesc->label,
  2013. is_out ? "out" : "in ",
  2014. chip->get
  2015. ? (chip->get(chip, i) ? "hi" : "lo")
  2016. : "? ",
  2017. is_irq ? "IRQ" : " ");
  2018. seq_printf(s, "\n");
  2019. }
  2020. }
  2021. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  2022. {
  2023. unsigned long flags;
  2024. struct gpio_chip *chip = NULL;
  2025. loff_t index = *pos;
  2026. s->private = "";
  2027. spin_lock_irqsave(&gpio_lock, flags);
  2028. list_for_each_entry(chip, &gpio_chips, list)
  2029. if (index-- == 0) {
  2030. spin_unlock_irqrestore(&gpio_lock, flags);
  2031. return chip;
  2032. }
  2033. spin_unlock_irqrestore(&gpio_lock, flags);
  2034. return NULL;
  2035. }
  2036. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2037. {
  2038. unsigned long flags;
  2039. struct gpio_chip *chip = v;
  2040. void *ret = NULL;
  2041. spin_lock_irqsave(&gpio_lock, flags);
  2042. if (list_is_last(&chip->list, &gpio_chips))
  2043. ret = NULL;
  2044. else
  2045. ret = list_entry(chip->list.next, struct gpio_chip, list);
  2046. spin_unlock_irqrestore(&gpio_lock, flags);
  2047. s->private = "\n";
  2048. ++*pos;
  2049. return ret;
  2050. }
  2051. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  2052. {
  2053. }
  2054. static int gpiolib_seq_show(struct seq_file *s, void *v)
  2055. {
  2056. struct gpio_chip *chip = v;
  2057. struct device *dev;
  2058. seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
  2059. chip->base, chip->base + chip->ngpio - 1);
  2060. dev = chip->dev;
  2061. if (dev)
  2062. seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
  2063. dev_name(dev));
  2064. if (chip->label)
  2065. seq_printf(s, ", %s", chip->label);
  2066. if (chip->can_sleep)
  2067. seq_printf(s, ", can sleep");
  2068. seq_printf(s, ":\n");
  2069. if (chip->dbg_show)
  2070. chip->dbg_show(s, chip);
  2071. else
  2072. gpiolib_dbg_show(s, chip);
  2073. return 0;
  2074. }
  2075. static const struct seq_operations gpiolib_seq_ops = {
  2076. .start = gpiolib_seq_start,
  2077. .next = gpiolib_seq_next,
  2078. .stop = gpiolib_seq_stop,
  2079. .show = gpiolib_seq_show,
  2080. };
  2081. static int gpiolib_open(struct inode *inode, struct file *file)
  2082. {
  2083. return seq_open(file, &gpiolib_seq_ops);
  2084. }
  2085. static const struct file_operations gpiolib_operations = {
  2086. .owner = THIS_MODULE,
  2087. .open = gpiolib_open,
  2088. .read = seq_read,
  2089. .llseek = seq_lseek,
  2090. .release = seq_release,
  2091. };
  2092. static int __init gpiolib_debugfs_init(void)
  2093. {
  2094. /* /sys/kernel/debug/gpio */
  2095. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  2096. NULL, NULL, &gpiolib_operations);
  2097. return 0;
  2098. }
  2099. subsys_initcall(gpiolib_debugfs_init);
  2100. #endif /* DEBUG_FS */