gpiolib.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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. static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
  44. #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
  45. static DEFINE_MUTEX(gpio_lookup_lock);
  46. static LIST_HEAD(gpio_lookup_list);
  47. LIST_HEAD(gpio_chips);
  48. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  49. {
  50. d->label = label;
  51. }
  52. /**
  53. * Convert a GPIO number to its descriptor
  54. */
  55. struct gpio_desc *gpio_to_desc(unsigned gpio)
  56. {
  57. if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
  58. return NULL;
  59. else
  60. return &gpio_desc[gpio];
  61. }
  62. EXPORT_SYMBOL_GPL(gpio_to_desc);
  63. /**
  64. * Get the GPIO descriptor corresponding to the given hw number for this chip.
  65. */
  66. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
  67. u16 hwnum)
  68. {
  69. if (hwnum >= chip->ngpio)
  70. return ERR_PTR(-EINVAL);
  71. return &chip->desc[hwnum];
  72. }
  73. /**
  74. * Convert a GPIO descriptor to the integer namespace.
  75. * This should disappear in the future but is needed since we still
  76. * use GPIO numbers for error messages and sysfs nodes
  77. */
  78. int desc_to_gpio(const struct gpio_desc *desc)
  79. {
  80. return desc - &gpio_desc[0];
  81. }
  82. EXPORT_SYMBOL_GPL(desc_to_gpio);
  83. /**
  84. * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
  85. * @desc: descriptor to return the chip of
  86. */
  87. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  88. {
  89. return desc ? desc->chip : NULL;
  90. }
  91. EXPORT_SYMBOL_GPL(gpiod_to_chip);
  92. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  93. static int gpiochip_find_base(int ngpio)
  94. {
  95. struct gpio_chip *chip;
  96. int base = ARCH_NR_GPIOS - ngpio;
  97. list_for_each_entry_reverse(chip, &gpio_chips, list) {
  98. /* found a free space? */
  99. if (chip->base + chip->ngpio <= base)
  100. break;
  101. else
  102. /* nope, check the space right before the chip */
  103. base = chip->base - ngpio;
  104. }
  105. if (gpio_is_valid(base)) {
  106. pr_debug("%s: found new base at %d\n", __func__, base);
  107. return base;
  108. } else {
  109. pr_err("%s: cannot find free range\n", __func__);
  110. return -ENOSPC;
  111. }
  112. }
  113. /**
  114. * gpiod_get_direction - return the current direction of a GPIO
  115. * @desc: GPIO to get the direction of
  116. *
  117. * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
  118. *
  119. * This function may sleep if gpiod_cansleep() is true.
  120. */
  121. int gpiod_get_direction(const struct gpio_desc *desc)
  122. {
  123. struct gpio_chip *chip;
  124. unsigned offset;
  125. int status = -EINVAL;
  126. chip = gpiod_to_chip(desc);
  127. offset = gpio_chip_hwgpio(desc);
  128. if (!chip->get_direction)
  129. return status;
  130. status = chip->get_direction(chip, offset);
  131. if (status > 0) {
  132. /* GPIOF_DIR_IN, or other positive */
  133. status = 1;
  134. /* FLAG_IS_OUT is just a cache of the result of get_direction(),
  135. * so it does not affect constness per se */
  136. clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
  137. }
  138. if (status == 0) {
  139. /* GPIOF_DIR_OUT */
  140. set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
  141. }
  142. return status;
  143. }
  144. EXPORT_SYMBOL_GPL(gpiod_get_direction);
  145. /*
  146. * Add a new chip to the global chips list, keeping the list of chips sorted
  147. * by base order.
  148. *
  149. * Return -EBUSY if the new chip overlaps with some other chip's integer
  150. * space.
  151. */
  152. static int gpiochip_add_to_list(struct gpio_chip *chip)
  153. {
  154. struct list_head *pos = &gpio_chips;
  155. struct gpio_chip *_chip;
  156. int err = 0;
  157. /* find where to insert our chip */
  158. list_for_each(pos, &gpio_chips) {
  159. _chip = list_entry(pos, struct gpio_chip, list);
  160. /* shall we insert before _chip? */
  161. if (_chip->base >= chip->base + chip->ngpio)
  162. break;
  163. }
  164. /* are we stepping on the chip right before? */
  165. if (pos != &gpio_chips && pos->prev != &gpio_chips) {
  166. _chip = list_entry(pos->prev, struct gpio_chip, list);
  167. if (_chip->base + _chip->ngpio > chip->base) {
  168. dev_err(chip->dev,
  169. "GPIO integer space overlap, cannot add chip\n");
  170. err = -EBUSY;
  171. }
  172. }
  173. if (!err)
  174. list_add_tail(&chip->list, pos);
  175. return err;
  176. }
  177. /**
  178. * gpiochip_add() - register a gpio_chip
  179. * @chip: the chip to register, with chip->base initialized
  180. * Context: potentially before irqs or kmalloc will work
  181. *
  182. * Returns a negative errno if the chip can't be registered, such as
  183. * because the chip->base is invalid or already associated with a
  184. * different chip. Otherwise it returns zero as a success code.
  185. *
  186. * When gpiochip_add() is called very early during boot, so that GPIOs
  187. * can be freely used, the chip->dev device must be registered before
  188. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  189. * for GPIOs will fail rudely.
  190. *
  191. * If chip->base is negative, this requests dynamic assignment of
  192. * a range of valid GPIOs.
  193. */
  194. int gpiochip_add(struct gpio_chip *chip)
  195. {
  196. unsigned long flags;
  197. int status = 0;
  198. unsigned id;
  199. int base = chip->base;
  200. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  201. && base >= 0) {
  202. status = -EINVAL;
  203. goto fail;
  204. }
  205. spin_lock_irqsave(&gpio_lock, flags);
  206. if (base < 0) {
  207. base = gpiochip_find_base(chip->ngpio);
  208. if (base < 0) {
  209. status = base;
  210. goto unlock;
  211. }
  212. chip->base = base;
  213. }
  214. status = gpiochip_add_to_list(chip);
  215. if (status == 0) {
  216. chip->desc = &gpio_desc[chip->base];
  217. for (id = 0; id < chip->ngpio; id++) {
  218. struct gpio_desc *desc = &chip->desc[id];
  219. desc->chip = chip;
  220. /* REVISIT: most hardware initializes GPIOs as
  221. * inputs (often with pullups enabled) so power
  222. * usage is minimized. Linux code should set the
  223. * gpio direction first thing; but until it does,
  224. * and in case chip->get_direction is not set,
  225. * we may expose the wrong direction in sysfs.
  226. */
  227. desc->flags = !chip->direction_input
  228. ? (1 << FLAG_IS_OUT)
  229. : 0;
  230. }
  231. }
  232. spin_unlock_irqrestore(&gpio_lock, flags);
  233. if (status)
  234. goto fail;
  235. #ifdef CONFIG_PINCTRL
  236. INIT_LIST_HEAD(&chip->pin_ranges);
  237. #endif
  238. of_gpiochip_add(chip);
  239. acpi_gpiochip_add(chip);
  240. status = gpiochip_export(chip);
  241. if (status) {
  242. acpi_gpiochip_remove(chip);
  243. of_gpiochip_remove(chip);
  244. goto fail;
  245. }
  246. pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
  247. chip->base, chip->base + chip->ngpio - 1,
  248. chip->label ? : "generic");
  249. return 0;
  250. unlock:
  251. spin_unlock_irqrestore(&gpio_lock, flags);
  252. fail:
  253. /* failures here can mean systems won't boot... */
  254. pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
  255. chip->base, chip->base + chip->ngpio - 1,
  256. chip->label ? : "generic");
  257. return status;
  258. }
  259. EXPORT_SYMBOL_GPL(gpiochip_add);
  260. /* Forward-declaration */
  261. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
  262. /**
  263. * gpiochip_remove() - unregister a gpio_chip
  264. * @chip: the chip to unregister
  265. *
  266. * A gpio_chip with any GPIOs still requested may not be removed.
  267. */
  268. void gpiochip_remove(struct gpio_chip *chip)
  269. {
  270. unsigned long flags;
  271. unsigned id;
  272. gpiochip_irqchip_remove(chip);
  273. acpi_gpiochip_remove(chip);
  274. gpiochip_remove_pin_ranges(chip);
  275. of_gpiochip_remove(chip);
  276. spin_lock_irqsave(&gpio_lock, flags);
  277. for (id = 0; id < chip->ngpio; id++) {
  278. if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags))
  279. dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
  280. }
  281. for (id = 0; id < chip->ngpio; id++)
  282. chip->desc[id].chip = NULL;
  283. list_del(&chip->list);
  284. spin_unlock_irqrestore(&gpio_lock, flags);
  285. gpiochip_unexport(chip);
  286. }
  287. EXPORT_SYMBOL_GPL(gpiochip_remove);
  288. /**
  289. * gpiochip_find() - iterator for locating a specific gpio_chip
  290. * @data: data to pass to match function
  291. * @callback: Callback function to check gpio_chip
  292. *
  293. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  294. * determined by a user supplied @match callback. The callback should return
  295. * 0 if the device doesn't match and non-zero if it does. If the callback is
  296. * non-zero, this function will return to the caller and not iterate over any
  297. * more gpio_chips.
  298. */
  299. struct gpio_chip *gpiochip_find(void *data,
  300. int (*match)(struct gpio_chip *chip,
  301. void *data))
  302. {
  303. struct gpio_chip *chip;
  304. unsigned long flags;
  305. spin_lock_irqsave(&gpio_lock, flags);
  306. list_for_each_entry(chip, &gpio_chips, list)
  307. if (match(chip, data))
  308. break;
  309. /* No match? */
  310. if (&chip->list == &gpio_chips)
  311. chip = NULL;
  312. spin_unlock_irqrestore(&gpio_lock, flags);
  313. return chip;
  314. }
  315. EXPORT_SYMBOL_GPL(gpiochip_find);
  316. static int gpiochip_match_name(struct gpio_chip *chip, void *data)
  317. {
  318. const char *name = data;
  319. return !strcmp(chip->label, name);
  320. }
  321. static struct gpio_chip *find_chip_by_name(const char *name)
  322. {
  323. return gpiochip_find((void *)name, gpiochip_match_name);
  324. }
  325. #ifdef CONFIG_GPIOLIB_IRQCHIP
  326. /*
  327. * The following is irqchip helper code for gpiochips.
  328. */
  329. /**
  330. * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
  331. * @gpiochip: the gpiochip to set the irqchip chain to
  332. * @irqchip: the irqchip to chain to the gpiochip
  333. * @parent_irq: the irq number corresponding to the parent IRQ for this
  334. * chained irqchip
  335. * @parent_handler: the parent interrupt handler for the accumulated IRQ
  336. * coming out of the gpiochip. If the interrupt is nested rather than
  337. * cascaded, pass NULL in this handler argument
  338. */
  339. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  340. struct irq_chip *irqchip,
  341. int parent_irq,
  342. irq_flow_handler_t parent_handler)
  343. {
  344. unsigned int offset;
  345. if (!gpiochip->irqdomain) {
  346. chip_err(gpiochip, "called %s before setting up irqchip\n",
  347. __func__);
  348. return;
  349. }
  350. if (parent_handler) {
  351. if (gpiochip->can_sleep) {
  352. chip_err(gpiochip,
  353. "you cannot have chained interrupts on a "
  354. "chip that may sleep\n");
  355. return;
  356. }
  357. /*
  358. * The parent irqchip is already using the chip_data for this
  359. * irqchip, so our callbacks simply use the handler_data.
  360. */
  361. irq_set_handler_data(parent_irq, gpiochip);
  362. irq_set_chained_handler(parent_irq, parent_handler);
  363. }
  364. /* Set the parent IRQ for all affected IRQs */
  365. for (offset = 0; offset < gpiochip->ngpio; offset++)
  366. irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
  367. parent_irq);
  368. }
  369. EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
  370. /*
  371. * This lock class tells lockdep that GPIO irqs are in a different
  372. * category than their parents, so it won't report false recursion.
  373. */
  374. static struct lock_class_key gpiochip_irq_lock_class;
  375. /**
  376. * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
  377. * @d: the irqdomain used by this irqchip
  378. * @irq: the global irq number used by this GPIO irqchip irq
  379. * @hwirq: the local IRQ/GPIO line offset on this gpiochip
  380. *
  381. * This function will set up the mapping for a certain IRQ line on a
  382. * gpiochip by assigning the gpiochip as chip data, and using the irqchip
  383. * stored inside the gpiochip.
  384. */
  385. static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  386. irq_hw_number_t hwirq)
  387. {
  388. struct gpio_chip *chip = d->host_data;
  389. irq_set_chip_data(irq, chip);
  390. irq_set_lockdep_class(irq, &gpiochip_irq_lock_class);
  391. irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
  392. /* Chips that can sleep need nested thread handlers */
  393. if (chip->can_sleep && !chip->irq_not_threaded)
  394. irq_set_nested_thread(irq, 1);
  395. #ifdef CONFIG_ARM
  396. set_irq_flags(irq, IRQF_VALID);
  397. #else
  398. irq_set_noprobe(irq);
  399. #endif
  400. /*
  401. * No set-up of the hardware will happen if IRQ_TYPE_NONE
  402. * is passed as default type.
  403. */
  404. if (chip->irq_default_type != IRQ_TYPE_NONE)
  405. irq_set_irq_type(irq, chip->irq_default_type);
  406. return 0;
  407. }
  408. static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
  409. {
  410. struct gpio_chip *chip = d->host_data;
  411. #ifdef CONFIG_ARM
  412. set_irq_flags(irq, 0);
  413. #endif
  414. if (chip->can_sleep)
  415. irq_set_nested_thread(irq, 0);
  416. irq_set_chip_and_handler(irq, NULL, NULL);
  417. irq_set_chip_data(irq, NULL);
  418. }
  419. static const struct irq_domain_ops gpiochip_domain_ops = {
  420. .map = gpiochip_irq_map,
  421. .unmap = gpiochip_irq_unmap,
  422. /* Virtually all GPIO irqchips are twocell:ed */
  423. .xlate = irq_domain_xlate_twocell,
  424. };
  425. static int gpiochip_irq_reqres(struct irq_data *d)
  426. {
  427. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  428. if (gpio_lock_as_irq(chip, d->hwirq)) {
  429. chip_err(chip,
  430. "unable to lock HW IRQ %lu for IRQ\n",
  431. d->hwirq);
  432. return -EINVAL;
  433. }
  434. return 0;
  435. }
  436. static void gpiochip_irq_relres(struct irq_data *d)
  437. {
  438. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  439. gpio_unlock_as_irq(chip, d->hwirq);
  440. }
  441. static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
  442. {
  443. return irq_find_mapping(chip->irqdomain, offset);
  444. }
  445. /**
  446. * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
  447. * @gpiochip: the gpiochip to remove the irqchip from
  448. *
  449. * This is called only from gpiochip_remove()
  450. */
  451. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
  452. {
  453. unsigned int offset;
  454. acpi_gpiochip_free_interrupts(gpiochip);
  455. /* Remove all IRQ mappings and delete the domain */
  456. if (gpiochip->irqdomain) {
  457. for (offset = 0; offset < gpiochip->ngpio; offset++)
  458. irq_dispose_mapping(
  459. irq_find_mapping(gpiochip->irqdomain, offset));
  460. irq_domain_remove(gpiochip->irqdomain);
  461. }
  462. if (gpiochip->irqchip) {
  463. gpiochip->irqchip->irq_request_resources = NULL;
  464. gpiochip->irqchip->irq_release_resources = NULL;
  465. gpiochip->irqchip = NULL;
  466. }
  467. }
  468. /**
  469. * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
  470. * @gpiochip: the gpiochip to add the irqchip to
  471. * @irqchip: the irqchip to add to the gpiochip
  472. * @first_irq: if not dynamically assigned, the base (first) IRQ to
  473. * allocate gpiochip irqs from
  474. * @handler: the irq handler to use (often a predefined irq core function)
  475. * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
  476. * to have the core avoid setting up any default type in the hardware.
  477. *
  478. * This function closely associates a certain irqchip with a certain
  479. * gpiochip, providing an irq domain to translate the local IRQs to
  480. * global irqs in the gpiolib core, and making sure that the gpiochip
  481. * is passed as chip data to all related functions. Driver callbacks
  482. * need to use container_of() to get their local state containers back
  483. * from the gpiochip passed as chip data. An irqdomain will be stored
  484. * in the gpiochip that shall be used by the driver to handle IRQ number
  485. * translation. The gpiochip will need to be initialized and registered
  486. * before calling this function.
  487. *
  488. * This function will handle two cell:ed simple IRQs and assumes all
  489. * the pins on the gpiochip can generate a unique IRQ. Everything else
  490. * need to be open coded.
  491. */
  492. int gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  493. struct irq_chip *irqchip,
  494. unsigned int first_irq,
  495. irq_flow_handler_t handler,
  496. unsigned int type)
  497. {
  498. struct device_node *of_node;
  499. unsigned int offset;
  500. unsigned irq_base = 0;
  501. if (!gpiochip || !irqchip)
  502. return -EINVAL;
  503. if (!gpiochip->dev) {
  504. pr_err("missing gpiochip .dev parent pointer\n");
  505. return -EINVAL;
  506. }
  507. of_node = gpiochip->dev->of_node;
  508. #ifdef CONFIG_OF_GPIO
  509. /*
  510. * If the gpiochip has an assigned OF node this takes precendence
  511. * FIXME: get rid of this and use gpiochip->dev->of_node everywhere
  512. */
  513. if (gpiochip->of_node)
  514. of_node = gpiochip->of_node;
  515. #endif
  516. gpiochip->irqchip = irqchip;
  517. gpiochip->irq_handler = handler;
  518. gpiochip->irq_default_type = type;
  519. gpiochip->to_irq = gpiochip_to_irq;
  520. gpiochip->irqdomain = irq_domain_add_simple(of_node,
  521. gpiochip->ngpio, first_irq,
  522. &gpiochip_domain_ops, gpiochip);
  523. if (!gpiochip->irqdomain) {
  524. gpiochip->irqchip = NULL;
  525. return -EINVAL;
  526. }
  527. irqchip->irq_request_resources = gpiochip_irq_reqres;
  528. irqchip->irq_release_resources = gpiochip_irq_relres;
  529. /*
  530. * Prepare the mapping since the irqchip shall be orthogonal to
  531. * any gpiochip calls. If the first_irq was zero, this is
  532. * necessary to allocate descriptors for all IRQs.
  533. */
  534. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  535. irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
  536. if (offset == 0)
  537. /*
  538. * Store the base into the gpiochip to be used when
  539. * unmapping the irqs.
  540. */
  541. gpiochip->irq_base = irq_base;
  542. }
  543. acpi_gpiochip_request_interrupts(gpiochip);
  544. return 0;
  545. }
  546. EXPORT_SYMBOL_GPL(gpiochip_irqchip_add);
  547. #else /* CONFIG_GPIOLIB_IRQCHIP */
  548. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
  549. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  550. #ifdef CONFIG_PINCTRL
  551. /**
  552. * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
  553. * @chip: the gpiochip to add the range for
  554. * @pinctrl: the dev_name() of the pin controller to map to
  555. * @gpio_offset: the start offset in the current gpio_chip number space
  556. * @pin_group: name of the pin group inside the pin controller
  557. */
  558. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  559. struct pinctrl_dev *pctldev,
  560. unsigned int gpio_offset, const char *pin_group)
  561. {
  562. struct gpio_pin_range *pin_range;
  563. int ret;
  564. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  565. if (!pin_range) {
  566. chip_err(chip, "failed to allocate pin ranges\n");
  567. return -ENOMEM;
  568. }
  569. /* Use local offset as range ID */
  570. pin_range->range.id = gpio_offset;
  571. pin_range->range.gc = chip;
  572. pin_range->range.name = chip->label;
  573. pin_range->range.base = chip->base + gpio_offset;
  574. pin_range->pctldev = pctldev;
  575. ret = pinctrl_get_group_pins(pctldev, pin_group,
  576. &pin_range->range.pins,
  577. &pin_range->range.npins);
  578. if (ret < 0) {
  579. kfree(pin_range);
  580. return ret;
  581. }
  582. pinctrl_add_gpio_range(pctldev, &pin_range->range);
  583. chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
  584. gpio_offset, gpio_offset + pin_range->range.npins - 1,
  585. pinctrl_dev_get_devname(pctldev), pin_group);
  586. list_add_tail(&pin_range->node, &chip->pin_ranges);
  587. return 0;
  588. }
  589. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  590. /**
  591. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  592. * @chip: the gpiochip to add the range for
  593. * @pinctrl_name: the dev_name() of the pin controller to map to
  594. * @gpio_offset: the start offset in the current gpio_chip number space
  595. * @pin_offset: the start offset in the pin controller number space
  596. * @npins: the number of pins from the offset of each pin space (GPIO and
  597. * pin controller) to accumulate in this range
  598. */
  599. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  600. unsigned int gpio_offset, unsigned int pin_offset,
  601. unsigned int npins)
  602. {
  603. struct gpio_pin_range *pin_range;
  604. int ret;
  605. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  606. if (!pin_range) {
  607. chip_err(chip, "failed to allocate pin ranges\n");
  608. return -ENOMEM;
  609. }
  610. /* Use local offset as range ID */
  611. pin_range->range.id = gpio_offset;
  612. pin_range->range.gc = chip;
  613. pin_range->range.name = chip->label;
  614. pin_range->range.base = chip->base + gpio_offset;
  615. pin_range->range.pin_base = pin_offset;
  616. pin_range->range.npins = npins;
  617. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  618. &pin_range->range);
  619. if (IS_ERR(pin_range->pctldev)) {
  620. ret = PTR_ERR(pin_range->pctldev);
  621. chip_err(chip, "could not create pin range\n");
  622. kfree(pin_range);
  623. return ret;
  624. }
  625. chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
  626. gpio_offset, gpio_offset + npins - 1,
  627. pinctl_name,
  628. pin_offset, pin_offset + npins - 1);
  629. list_add_tail(&pin_range->node, &chip->pin_ranges);
  630. return 0;
  631. }
  632. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  633. /**
  634. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  635. * @chip: the chip to remove all the mappings for
  636. */
  637. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  638. {
  639. struct gpio_pin_range *pin_range, *tmp;
  640. list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
  641. list_del(&pin_range->node);
  642. pinctrl_remove_gpio_range(pin_range->pctldev,
  643. &pin_range->range);
  644. kfree(pin_range);
  645. }
  646. }
  647. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  648. #endif /* CONFIG_PINCTRL */
  649. /* These "optional" allocation calls help prevent drivers from stomping
  650. * on each other, and help provide better diagnostics in debugfs.
  651. * They're called even less than the "set direction" calls.
  652. */
  653. static int __gpiod_request(struct gpio_desc *desc, const char *label)
  654. {
  655. struct gpio_chip *chip = desc->chip;
  656. int status;
  657. unsigned long flags;
  658. spin_lock_irqsave(&gpio_lock, flags);
  659. /* NOTE: gpio_request() can be called in early boot,
  660. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  661. */
  662. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  663. desc_set_label(desc, label ? : "?");
  664. status = 0;
  665. } else {
  666. status = -EBUSY;
  667. goto done;
  668. }
  669. if (chip->request) {
  670. /* chip->request may sleep */
  671. spin_unlock_irqrestore(&gpio_lock, flags);
  672. status = chip->request(chip, gpio_chip_hwgpio(desc));
  673. spin_lock_irqsave(&gpio_lock, flags);
  674. if (status < 0) {
  675. desc_set_label(desc, NULL);
  676. clear_bit(FLAG_REQUESTED, &desc->flags);
  677. goto done;
  678. }
  679. }
  680. if (chip->get_direction) {
  681. /* chip->get_direction may sleep */
  682. spin_unlock_irqrestore(&gpio_lock, flags);
  683. gpiod_get_direction(desc);
  684. spin_lock_irqsave(&gpio_lock, flags);
  685. }
  686. done:
  687. spin_unlock_irqrestore(&gpio_lock, flags);
  688. return status;
  689. }
  690. int gpiod_request(struct gpio_desc *desc, const char *label)
  691. {
  692. int status = -EPROBE_DEFER;
  693. struct gpio_chip *chip;
  694. if (!desc) {
  695. pr_warn("%s: invalid GPIO\n", __func__);
  696. return -EINVAL;
  697. }
  698. chip = desc->chip;
  699. if (!chip)
  700. goto done;
  701. if (try_module_get(chip->owner)) {
  702. status = __gpiod_request(desc, label);
  703. if (status < 0)
  704. module_put(chip->owner);
  705. }
  706. done:
  707. if (status)
  708. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  709. return status;
  710. }
  711. static bool __gpiod_free(struct gpio_desc *desc)
  712. {
  713. bool ret = false;
  714. unsigned long flags;
  715. struct gpio_chip *chip;
  716. might_sleep();
  717. gpiod_unexport(desc);
  718. spin_lock_irqsave(&gpio_lock, flags);
  719. chip = desc->chip;
  720. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  721. if (chip->free) {
  722. spin_unlock_irqrestore(&gpio_lock, flags);
  723. might_sleep_if(chip->can_sleep);
  724. chip->free(chip, gpio_chip_hwgpio(desc));
  725. spin_lock_irqsave(&gpio_lock, flags);
  726. }
  727. desc_set_label(desc, NULL);
  728. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  729. clear_bit(FLAG_REQUESTED, &desc->flags);
  730. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  731. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  732. ret = true;
  733. }
  734. spin_unlock_irqrestore(&gpio_lock, flags);
  735. return ret;
  736. }
  737. void gpiod_free(struct gpio_desc *desc)
  738. {
  739. if (desc && __gpiod_free(desc))
  740. module_put(desc->chip->owner);
  741. else
  742. WARN_ON(extra_checks);
  743. }
  744. /**
  745. * gpiochip_is_requested - return string iff signal was requested
  746. * @chip: controller managing the signal
  747. * @offset: of signal within controller's 0..(ngpio - 1) range
  748. *
  749. * Returns NULL if the GPIO is not currently requested, else a string.
  750. * The string returned is the label passed to gpio_request(); if none has been
  751. * passed it is a meaningless, non-NULL constant.
  752. *
  753. * This function is for use by GPIO controller drivers. The label can
  754. * help with diagnostics, and knowing that the signal is used as a GPIO
  755. * can help avoid accidentally multiplexing it to another controller.
  756. */
  757. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  758. {
  759. struct gpio_desc *desc;
  760. if (!GPIO_OFFSET_VALID(chip, offset))
  761. return NULL;
  762. desc = &chip->desc[offset];
  763. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  764. return NULL;
  765. return desc->label;
  766. }
  767. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  768. /**
  769. * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
  770. * @desc: GPIO descriptor to request
  771. * @label: label for the GPIO
  772. *
  773. * Function allows GPIO chip drivers to request and use their own GPIO
  774. * descriptors via gpiolib API. Difference to gpiod_request() is that this
  775. * function will not increase reference count of the GPIO chip module. This
  776. * allows the GPIO chip module to be unloaded as needed (we assume that the
  777. * GPIO chip driver handles freeing the GPIOs it has requested).
  778. */
  779. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  780. const char *label)
  781. {
  782. struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
  783. int err;
  784. if (IS_ERR(desc)) {
  785. chip_err(chip, "failed to get GPIO descriptor\n");
  786. return desc;
  787. }
  788. err = __gpiod_request(desc, label);
  789. if (err < 0)
  790. return ERR_PTR(err);
  791. return desc;
  792. }
  793. EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
  794. /**
  795. * gpiochip_free_own_desc - Free GPIO requested by the chip driver
  796. * @desc: GPIO descriptor to free
  797. *
  798. * Function frees the given GPIO requested previously with
  799. * gpiochip_request_own_desc().
  800. */
  801. void gpiochip_free_own_desc(struct gpio_desc *desc)
  802. {
  803. if (desc)
  804. __gpiod_free(desc);
  805. }
  806. EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
  807. /* Drivers MUST set GPIO direction before making get/set calls. In
  808. * some cases this is done in early boot, before IRQs are enabled.
  809. *
  810. * As a rule these aren't called more than once (except for drivers
  811. * using the open-drain emulation idiom) so these are natural places
  812. * to accumulate extra debugging checks. Note that we can't (yet)
  813. * rely on gpio_request() having been called beforehand.
  814. */
  815. /**
  816. * gpiod_direction_input - set the GPIO direction to input
  817. * @desc: GPIO to set to input
  818. *
  819. * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
  820. * be called safely on it.
  821. *
  822. * Return 0 in case of success, else an error code.
  823. */
  824. int gpiod_direction_input(struct gpio_desc *desc)
  825. {
  826. struct gpio_chip *chip;
  827. int status = -EINVAL;
  828. if (!desc || !desc->chip) {
  829. pr_warn("%s: invalid GPIO\n", __func__);
  830. return -EINVAL;
  831. }
  832. chip = desc->chip;
  833. if (!chip->get || !chip->direction_input) {
  834. gpiod_warn(desc,
  835. "%s: missing get() or direction_input() operations\n",
  836. __func__);
  837. return -EIO;
  838. }
  839. status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
  840. if (status == 0)
  841. clear_bit(FLAG_IS_OUT, &desc->flags);
  842. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  843. return status;
  844. }
  845. EXPORT_SYMBOL_GPL(gpiod_direction_input);
  846. static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  847. {
  848. struct gpio_chip *chip;
  849. int status = -EINVAL;
  850. /* GPIOs used for IRQs shall not be set as output */
  851. if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
  852. gpiod_err(desc,
  853. "%s: tried to set a GPIO tied to an IRQ as output\n",
  854. __func__);
  855. return -EIO;
  856. }
  857. /* Open drain pin should not be driven to 1 */
  858. if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  859. return gpiod_direction_input(desc);
  860. /* Open source pin should not be driven to 0 */
  861. if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  862. return gpiod_direction_input(desc);
  863. chip = desc->chip;
  864. if (!chip->set || !chip->direction_output) {
  865. gpiod_warn(desc,
  866. "%s: missing set() or direction_output() operations\n",
  867. __func__);
  868. return -EIO;
  869. }
  870. status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
  871. if (status == 0)
  872. set_bit(FLAG_IS_OUT, &desc->flags);
  873. trace_gpio_value(desc_to_gpio(desc), 0, value);
  874. trace_gpio_direction(desc_to_gpio(desc), 0, status);
  875. return status;
  876. }
  877. /**
  878. * gpiod_direction_output_raw - set the GPIO direction to output
  879. * @desc: GPIO to set to output
  880. * @value: initial output value of the GPIO
  881. *
  882. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  883. * be called safely on it. The initial value of the output must be specified
  884. * as raw value on the physical line without regard for the ACTIVE_LOW status.
  885. *
  886. * Return 0 in case of success, else an error code.
  887. */
  888. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  889. {
  890. if (!desc || !desc->chip) {
  891. pr_warn("%s: invalid GPIO\n", __func__);
  892. return -EINVAL;
  893. }
  894. return _gpiod_direction_output_raw(desc, value);
  895. }
  896. EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
  897. /**
  898. * gpiod_direction_output - set the GPIO direction to output
  899. * @desc: GPIO to set to output
  900. * @value: initial output value of the GPIO
  901. *
  902. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  903. * be called safely on it. The initial value of the output must be specified
  904. * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  905. * account.
  906. *
  907. * Return 0 in case of success, else an error code.
  908. */
  909. int gpiod_direction_output(struct gpio_desc *desc, int value)
  910. {
  911. if (!desc || !desc->chip) {
  912. pr_warn("%s: invalid GPIO\n", __func__);
  913. return -EINVAL;
  914. }
  915. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  916. value = !value;
  917. return _gpiod_direction_output_raw(desc, value);
  918. }
  919. EXPORT_SYMBOL_GPL(gpiod_direction_output);
  920. /**
  921. * gpiod_set_debounce - sets @debounce time for a @gpio
  922. * @gpio: the gpio to set debounce time
  923. * @debounce: debounce time is microseconds
  924. *
  925. * returns -ENOTSUPP if the controller does not support setting
  926. * debounce.
  927. */
  928. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  929. {
  930. struct gpio_chip *chip;
  931. if (!desc || !desc->chip) {
  932. pr_warn("%s: invalid GPIO\n", __func__);
  933. return -EINVAL;
  934. }
  935. chip = desc->chip;
  936. if (!chip->set || !chip->set_debounce) {
  937. gpiod_dbg(desc,
  938. "%s: missing set() or set_debounce() operations\n",
  939. __func__);
  940. return -ENOTSUPP;
  941. }
  942. return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
  943. }
  944. EXPORT_SYMBOL_GPL(gpiod_set_debounce);
  945. /**
  946. * gpiod_is_active_low - test whether a GPIO is active-low or not
  947. * @desc: the gpio descriptor to test
  948. *
  949. * Returns 1 if the GPIO is active-low, 0 otherwise.
  950. */
  951. int gpiod_is_active_low(const struct gpio_desc *desc)
  952. {
  953. return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  954. }
  955. EXPORT_SYMBOL_GPL(gpiod_is_active_low);
  956. /* I/O calls are only valid after configuration completed; the relevant
  957. * "is this a valid GPIO" error checks should already have been done.
  958. *
  959. * "Get" operations are often inlinable as reading a pin value register,
  960. * and masking the relevant bit in that register.
  961. *
  962. * When "set" operations are inlinable, they involve writing that mask to
  963. * one register to set a low value, or a different register to set it high.
  964. * Otherwise locking is needed, so there may be little value to inlining.
  965. *
  966. *------------------------------------------------------------------------
  967. *
  968. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  969. * have requested the GPIO. That can include implicit requesting by
  970. * a direction setting call. Marking a gpio as requested locks its chip
  971. * in memory, guaranteeing that these table lookups need no more locking
  972. * and that gpiochip_remove() will fail.
  973. *
  974. * REVISIT when debugging, consider adding some instrumentation to ensure
  975. * that the GPIO was actually requested.
  976. */
  977. static bool _gpiod_get_raw_value(const struct gpio_desc *desc)
  978. {
  979. struct gpio_chip *chip;
  980. bool value;
  981. int offset;
  982. chip = desc->chip;
  983. offset = gpio_chip_hwgpio(desc);
  984. value = chip->get ? chip->get(chip, offset) : false;
  985. trace_gpio_value(desc_to_gpio(desc), 1, value);
  986. return value;
  987. }
  988. /**
  989. * gpiod_get_raw_value() - return a gpio's raw value
  990. * @desc: gpio whose value will be returned
  991. *
  992. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  993. * its ACTIVE_LOW status.
  994. *
  995. * This function should be called from contexts where we cannot sleep, and will
  996. * complain if the GPIO chip functions potentially sleep.
  997. */
  998. int gpiod_get_raw_value(const struct gpio_desc *desc)
  999. {
  1000. if (!desc)
  1001. return 0;
  1002. /* Should be using gpio_get_value_cansleep() */
  1003. WARN_ON(desc->chip->can_sleep);
  1004. return _gpiod_get_raw_value(desc);
  1005. }
  1006. EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
  1007. /**
  1008. * gpiod_get_value() - return a gpio's value
  1009. * @desc: gpio whose value will be returned
  1010. *
  1011. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  1012. * account.
  1013. *
  1014. * This function should be called from contexts where we cannot sleep, and will
  1015. * complain if the GPIO chip functions potentially sleep.
  1016. */
  1017. int gpiod_get_value(const struct gpio_desc *desc)
  1018. {
  1019. int value;
  1020. if (!desc)
  1021. return 0;
  1022. /* Should be using gpio_get_value_cansleep() */
  1023. WARN_ON(desc->chip->can_sleep);
  1024. value = _gpiod_get_raw_value(desc);
  1025. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1026. value = !value;
  1027. return value;
  1028. }
  1029. EXPORT_SYMBOL_GPL(gpiod_get_value);
  1030. /*
  1031. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  1032. * @desc: gpio descriptor whose state need to be set.
  1033. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1034. */
  1035. static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
  1036. {
  1037. int err = 0;
  1038. struct gpio_chip *chip = desc->chip;
  1039. int offset = gpio_chip_hwgpio(desc);
  1040. if (value) {
  1041. err = chip->direction_input(chip, offset);
  1042. if (!err)
  1043. clear_bit(FLAG_IS_OUT, &desc->flags);
  1044. } else {
  1045. err = chip->direction_output(chip, offset, 0);
  1046. if (!err)
  1047. set_bit(FLAG_IS_OUT, &desc->flags);
  1048. }
  1049. trace_gpio_direction(desc_to_gpio(desc), value, err);
  1050. if (err < 0)
  1051. gpiod_err(desc,
  1052. "%s: Error in set_value for open drain err %d\n",
  1053. __func__, err);
  1054. }
  1055. /*
  1056. * _gpio_set_open_source_value() - Set the open source gpio's value.
  1057. * @desc: gpio descriptor whose state need to be set.
  1058. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1059. */
  1060. static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
  1061. {
  1062. int err = 0;
  1063. struct gpio_chip *chip = desc->chip;
  1064. int offset = gpio_chip_hwgpio(desc);
  1065. if (value) {
  1066. err = chip->direction_output(chip, offset, 1);
  1067. if (!err)
  1068. set_bit(FLAG_IS_OUT, &desc->flags);
  1069. } else {
  1070. err = chip->direction_input(chip, offset);
  1071. if (!err)
  1072. clear_bit(FLAG_IS_OUT, &desc->flags);
  1073. }
  1074. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  1075. if (err < 0)
  1076. gpiod_err(desc,
  1077. "%s: Error in set_value for open source err %d\n",
  1078. __func__, err);
  1079. }
  1080. static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
  1081. {
  1082. struct gpio_chip *chip;
  1083. chip = desc->chip;
  1084. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1085. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1086. _gpio_set_open_drain_value(desc, value);
  1087. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1088. _gpio_set_open_source_value(desc, value);
  1089. else
  1090. chip->set(chip, gpio_chip_hwgpio(desc), value);
  1091. }
  1092. /**
  1093. * gpiod_set_raw_value() - assign a gpio's raw value
  1094. * @desc: gpio whose value will be assigned
  1095. * @value: value to assign
  1096. *
  1097. * Set the raw value of the GPIO, i.e. the value of its physical line without
  1098. * regard for its ACTIVE_LOW status.
  1099. *
  1100. * This function should be called from contexts where we cannot sleep, and will
  1101. * complain if the GPIO chip functions potentially sleep.
  1102. */
  1103. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  1104. {
  1105. if (!desc)
  1106. return;
  1107. /* Should be using gpio_set_value_cansleep() */
  1108. WARN_ON(desc->chip->can_sleep);
  1109. _gpiod_set_raw_value(desc, value);
  1110. }
  1111. EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
  1112. /**
  1113. * gpiod_set_value() - assign a gpio's value
  1114. * @desc: gpio whose value will be assigned
  1115. * @value: value to assign
  1116. *
  1117. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1118. * account
  1119. *
  1120. * This function should be called from contexts where we cannot sleep, and will
  1121. * complain if the GPIO chip functions potentially sleep.
  1122. */
  1123. void gpiod_set_value(struct gpio_desc *desc, int value)
  1124. {
  1125. if (!desc)
  1126. return;
  1127. /* Should be using gpio_set_value_cansleep() */
  1128. WARN_ON(desc->chip->can_sleep);
  1129. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1130. value = !value;
  1131. _gpiod_set_raw_value(desc, value);
  1132. }
  1133. EXPORT_SYMBOL_GPL(gpiod_set_value);
  1134. /**
  1135. * gpiod_cansleep() - report whether gpio value access may sleep
  1136. * @desc: gpio to check
  1137. *
  1138. */
  1139. int gpiod_cansleep(const struct gpio_desc *desc)
  1140. {
  1141. if (!desc)
  1142. return 0;
  1143. return desc->chip->can_sleep;
  1144. }
  1145. EXPORT_SYMBOL_GPL(gpiod_cansleep);
  1146. /**
  1147. * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  1148. * @desc: gpio whose IRQ will be returned (already requested)
  1149. *
  1150. * Return the IRQ corresponding to the passed GPIO, or an error code in case of
  1151. * error.
  1152. */
  1153. int gpiod_to_irq(const struct gpio_desc *desc)
  1154. {
  1155. struct gpio_chip *chip;
  1156. int offset;
  1157. if (!desc)
  1158. return -EINVAL;
  1159. chip = desc->chip;
  1160. offset = gpio_chip_hwgpio(desc);
  1161. return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
  1162. }
  1163. EXPORT_SYMBOL_GPL(gpiod_to_irq);
  1164. /**
  1165. * gpio_lock_as_irq() - lock a GPIO to be used as IRQ
  1166. * @chip: the chip the GPIO to lock belongs to
  1167. * @offset: the offset of the GPIO to lock as IRQ
  1168. *
  1169. * This is used directly by GPIO drivers that want to lock down
  1170. * a certain GPIO line to be used for IRQs.
  1171. */
  1172. int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
  1173. {
  1174. if (offset >= chip->ngpio)
  1175. return -EINVAL;
  1176. if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
  1177. chip_err(chip,
  1178. "%s: tried to flag a GPIO set as output for IRQ\n",
  1179. __func__);
  1180. return -EIO;
  1181. }
  1182. set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
  1183. return 0;
  1184. }
  1185. EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
  1186. /**
  1187. * gpio_unlock_as_irq() - unlock a GPIO used as IRQ
  1188. * @chip: the chip the GPIO to lock belongs to
  1189. * @offset: the offset of the GPIO to lock as IRQ
  1190. *
  1191. * This is used directly by GPIO drivers that want to indicate
  1192. * that a certain GPIO is no longer used exclusively for IRQ.
  1193. */
  1194. void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
  1195. {
  1196. if (offset >= chip->ngpio)
  1197. return;
  1198. clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
  1199. }
  1200. EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
  1201. /**
  1202. * gpiod_get_raw_value_cansleep() - return a gpio's raw value
  1203. * @desc: gpio whose value will be returned
  1204. *
  1205. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  1206. * its ACTIVE_LOW status.
  1207. *
  1208. * This function is to be called from contexts that can sleep.
  1209. */
  1210. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  1211. {
  1212. might_sleep_if(extra_checks);
  1213. if (!desc)
  1214. return 0;
  1215. return _gpiod_get_raw_value(desc);
  1216. }
  1217. EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
  1218. /**
  1219. * gpiod_get_value_cansleep() - return a gpio's value
  1220. * @desc: gpio whose value will be returned
  1221. *
  1222. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  1223. * account.
  1224. *
  1225. * This function is to be called from contexts that can sleep.
  1226. */
  1227. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  1228. {
  1229. int value;
  1230. might_sleep_if(extra_checks);
  1231. if (!desc)
  1232. return 0;
  1233. value = _gpiod_get_raw_value(desc);
  1234. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1235. value = !value;
  1236. return value;
  1237. }
  1238. EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
  1239. /**
  1240. * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
  1241. * @desc: gpio whose value will be assigned
  1242. * @value: value to assign
  1243. *
  1244. * Set the raw value of the GPIO, i.e. the value of its physical line without
  1245. * regard for its ACTIVE_LOW status.
  1246. *
  1247. * This function is to be called from contexts that can sleep.
  1248. */
  1249. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  1250. {
  1251. might_sleep_if(extra_checks);
  1252. if (!desc)
  1253. return;
  1254. _gpiod_set_raw_value(desc, value);
  1255. }
  1256. EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
  1257. /**
  1258. * gpiod_set_value_cansleep() - assign a gpio's value
  1259. * @desc: gpio whose value will be assigned
  1260. * @value: value to assign
  1261. *
  1262. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1263. * account
  1264. *
  1265. * This function is to be called from contexts that can sleep.
  1266. */
  1267. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  1268. {
  1269. might_sleep_if(extra_checks);
  1270. if (!desc)
  1271. return;
  1272. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1273. value = !value;
  1274. _gpiod_set_raw_value(desc, value);
  1275. }
  1276. EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
  1277. /**
  1278. * gpiod_add_lookup_table() - register GPIO device consumers
  1279. * @table: table of consumers to register
  1280. */
  1281. void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
  1282. {
  1283. mutex_lock(&gpio_lookup_lock);
  1284. list_add_tail(&table->list, &gpio_lookup_list);
  1285. mutex_unlock(&gpio_lookup_lock);
  1286. }
  1287. static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  1288. unsigned int idx,
  1289. enum gpio_lookup_flags *flags)
  1290. {
  1291. static const char *suffixes[] = { "gpios", "gpio" };
  1292. char prop_name[32]; /* 32 is max size of property name */
  1293. enum of_gpio_flags of_flags;
  1294. struct gpio_desc *desc;
  1295. unsigned int i;
  1296. for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
  1297. if (con_id)
  1298. snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]);
  1299. else
  1300. snprintf(prop_name, 32, "%s", suffixes[i]);
  1301. desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
  1302. &of_flags);
  1303. if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
  1304. break;
  1305. }
  1306. if (IS_ERR(desc))
  1307. return desc;
  1308. if (of_flags & OF_GPIO_ACTIVE_LOW)
  1309. *flags |= GPIO_ACTIVE_LOW;
  1310. return desc;
  1311. }
  1312. static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
  1313. unsigned int idx,
  1314. enum gpio_lookup_flags *flags)
  1315. {
  1316. struct acpi_gpio_info info;
  1317. struct gpio_desc *desc;
  1318. desc = acpi_get_gpiod_by_index(dev, idx, &info);
  1319. if (IS_ERR(desc))
  1320. return desc;
  1321. if (info.gpioint && info.active_low)
  1322. *flags |= GPIO_ACTIVE_LOW;
  1323. return desc;
  1324. }
  1325. static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
  1326. {
  1327. const char *dev_id = dev ? dev_name(dev) : NULL;
  1328. struct gpiod_lookup_table *table;
  1329. mutex_lock(&gpio_lookup_lock);
  1330. list_for_each_entry(table, &gpio_lookup_list, list) {
  1331. if (table->dev_id && dev_id) {
  1332. /*
  1333. * Valid strings on both ends, must be identical to have
  1334. * a match
  1335. */
  1336. if (!strcmp(table->dev_id, dev_id))
  1337. goto found;
  1338. } else {
  1339. /*
  1340. * One of the pointers is NULL, so both must be to have
  1341. * a match
  1342. */
  1343. if (dev_id == table->dev_id)
  1344. goto found;
  1345. }
  1346. }
  1347. table = NULL;
  1348. found:
  1349. mutex_unlock(&gpio_lookup_lock);
  1350. return table;
  1351. }
  1352. static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
  1353. unsigned int idx,
  1354. enum gpio_lookup_flags *flags)
  1355. {
  1356. struct gpio_desc *desc = ERR_PTR(-ENOENT);
  1357. struct gpiod_lookup_table *table;
  1358. struct gpiod_lookup *p;
  1359. table = gpiod_find_lookup_table(dev);
  1360. if (!table)
  1361. return desc;
  1362. for (p = &table->table[0]; p->chip_label; p++) {
  1363. struct gpio_chip *chip;
  1364. /* idx must always match exactly */
  1365. if (p->idx != idx)
  1366. continue;
  1367. /* If the lookup entry has a con_id, require exact match */
  1368. if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
  1369. continue;
  1370. chip = find_chip_by_name(p->chip_label);
  1371. if (!chip) {
  1372. dev_err(dev, "cannot find GPIO chip %s\n",
  1373. p->chip_label);
  1374. return ERR_PTR(-ENODEV);
  1375. }
  1376. if (chip->ngpio <= p->chip_hwnum) {
  1377. dev_err(dev,
  1378. "requested GPIO %d is out of range [0..%d] for chip %s\n",
  1379. idx, chip->ngpio, chip->label);
  1380. return ERR_PTR(-EINVAL);
  1381. }
  1382. desc = gpiochip_get_desc(chip, p->chip_hwnum);
  1383. *flags = p->flags;
  1384. return desc;
  1385. }
  1386. return desc;
  1387. }
  1388. /**
  1389. * gpiod_get - obtain a GPIO for a given GPIO function
  1390. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1391. * @con_id: function within the GPIO consumer
  1392. * @flags: optional GPIO initialization flags
  1393. *
  1394. * Return the GPIO descriptor corresponding to the function con_id of device
  1395. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  1396. * another IS_ERR() code if an error occured while trying to acquire the GPIO.
  1397. */
  1398. struct gpio_desc *__must_check __gpiod_get(struct device *dev, const char *con_id,
  1399. enum gpiod_flags flags)
  1400. {
  1401. return gpiod_get_index(dev, con_id, 0, flags);
  1402. }
  1403. EXPORT_SYMBOL_GPL(__gpiod_get);
  1404. /**
  1405. * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
  1406. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1407. * @con_id: function within the GPIO consumer
  1408. * @flags: optional GPIO initialization flags
  1409. *
  1410. * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
  1411. * the requested function it will return NULL. This is convenient for drivers
  1412. * that need to handle optional GPIOs.
  1413. */
  1414. struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
  1415. const char *con_id,
  1416. enum gpiod_flags flags)
  1417. {
  1418. return gpiod_get_index_optional(dev, con_id, 0, flags);
  1419. }
  1420. EXPORT_SYMBOL_GPL(__gpiod_get_optional);
  1421. /**
  1422. * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
  1423. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1424. * @con_id: function within the GPIO consumer
  1425. * @idx: index of the GPIO to obtain in the consumer
  1426. * @flags: optional GPIO initialization flags
  1427. *
  1428. * This variant of gpiod_get() allows to access GPIOs other than the first
  1429. * defined one for functions that define several GPIOs.
  1430. *
  1431. * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
  1432. * requested function and/or index, or another IS_ERR() code if an error
  1433. * occured while trying to acquire the GPIO.
  1434. */
  1435. struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
  1436. const char *con_id,
  1437. unsigned int idx,
  1438. enum gpiod_flags flags)
  1439. {
  1440. struct gpio_desc *desc = NULL;
  1441. int status;
  1442. enum gpio_lookup_flags lookupflags = 0;
  1443. dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
  1444. /* Using device tree? */
  1445. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
  1446. dev_dbg(dev, "using device tree for GPIO lookup\n");
  1447. desc = of_find_gpio(dev, con_id, idx, &lookupflags);
  1448. } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
  1449. dev_dbg(dev, "using ACPI for GPIO lookup\n");
  1450. desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
  1451. }
  1452. /*
  1453. * Either we are not using DT or ACPI, or their lookup did not return
  1454. * a result. In that case, use platform lookup as a fallback.
  1455. */
  1456. if (!desc || desc == ERR_PTR(-ENOENT)) {
  1457. dev_dbg(dev, "using lookup tables for GPIO lookup\n");
  1458. desc = gpiod_find(dev, con_id, idx, &lookupflags);
  1459. }
  1460. if (IS_ERR(desc)) {
  1461. dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
  1462. return desc;
  1463. }
  1464. status = gpiod_request(desc, con_id);
  1465. if (status < 0)
  1466. return ERR_PTR(status);
  1467. if (lookupflags & GPIO_ACTIVE_LOW)
  1468. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1469. if (lookupflags & GPIO_OPEN_DRAIN)
  1470. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1471. if (lookupflags & GPIO_OPEN_SOURCE)
  1472. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1473. /* No particular flag request, return here... */
  1474. if (!(flags & GPIOD_FLAGS_BIT_DIR_SET))
  1475. return desc;
  1476. /* Process flags */
  1477. if (flags & GPIOD_FLAGS_BIT_DIR_OUT)
  1478. status = gpiod_direction_output(desc,
  1479. flags & GPIOD_FLAGS_BIT_DIR_VAL);
  1480. else
  1481. status = gpiod_direction_input(desc);
  1482. if (status < 0) {
  1483. dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
  1484. gpiod_put(desc);
  1485. return ERR_PTR(status);
  1486. }
  1487. return desc;
  1488. }
  1489. EXPORT_SYMBOL_GPL(__gpiod_get_index);
  1490. /**
  1491. * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
  1492. * function
  1493. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  1494. * @con_id: function within the GPIO consumer
  1495. * @index: index of the GPIO to obtain in the consumer
  1496. * @flags: optional GPIO initialization flags
  1497. *
  1498. * This is equivalent to gpiod_get_index(), except that when no GPIO with the
  1499. * specified index was assigned to the requested function it will return NULL.
  1500. * This is convenient for drivers that need to handle optional GPIOs.
  1501. */
  1502. struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
  1503. const char *con_id,
  1504. unsigned int index,
  1505. enum gpiod_flags flags)
  1506. {
  1507. struct gpio_desc *desc;
  1508. desc = gpiod_get_index(dev, con_id, index, flags);
  1509. if (IS_ERR(desc)) {
  1510. if (PTR_ERR(desc) == -ENOENT)
  1511. return NULL;
  1512. }
  1513. return desc;
  1514. }
  1515. EXPORT_SYMBOL_GPL(__gpiod_get_index_optional);
  1516. /**
  1517. * gpiod_put - dispose of a GPIO descriptor
  1518. * @desc: GPIO descriptor to dispose of
  1519. *
  1520. * No descriptor can be used after gpiod_put() has been called on it.
  1521. */
  1522. void gpiod_put(struct gpio_desc *desc)
  1523. {
  1524. gpiod_free(desc);
  1525. }
  1526. EXPORT_SYMBOL_GPL(gpiod_put);
  1527. #ifdef CONFIG_DEBUG_FS
  1528. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1529. {
  1530. unsigned i;
  1531. unsigned gpio = chip->base;
  1532. struct gpio_desc *gdesc = &chip->desc[0];
  1533. int is_out;
  1534. int is_irq;
  1535. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1536. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1537. continue;
  1538. gpiod_get_direction(gdesc);
  1539. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1540. is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
  1541. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
  1542. gpio, gdesc->label,
  1543. is_out ? "out" : "in ",
  1544. chip->get
  1545. ? (chip->get(chip, i) ? "hi" : "lo")
  1546. : "? ",
  1547. is_irq ? "IRQ" : " ");
  1548. seq_printf(s, "\n");
  1549. }
  1550. }
  1551. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  1552. {
  1553. unsigned long flags;
  1554. struct gpio_chip *chip = NULL;
  1555. loff_t index = *pos;
  1556. s->private = "";
  1557. spin_lock_irqsave(&gpio_lock, flags);
  1558. list_for_each_entry(chip, &gpio_chips, list)
  1559. if (index-- == 0) {
  1560. spin_unlock_irqrestore(&gpio_lock, flags);
  1561. return chip;
  1562. }
  1563. spin_unlock_irqrestore(&gpio_lock, flags);
  1564. return NULL;
  1565. }
  1566. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1567. {
  1568. unsigned long flags;
  1569. struct gpio_chip *chip = v;
  1570. void *ret = NULL;
  1571. spin_lock_irqsave(&gpio_lock, flags);
  1572. if (list_is_last(&chip->list, &gpio_chips))
  1573. ret = NULL;
  1574. else
  1575. ret = list_entry(chip->list.next, struct gpio_chip, list);
  1576. spin_unlock_irqrestore(&gpio_lock, flags);
  1577. s->private = "\n";
  1578. ++*pos;
  1579. return ret;
  1580. }
  1581. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  1582. {
  1583. }
  1584. static int gpiolib_seq_show(struct seq_file *s, void *v)
  1585. {
  1586. struct gpio_chip *chip = v;
  1587. struct device *dev;
  1588. seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
  1589. chip->base, chip->base + chip->ngpio - 1);
  1590. dev = chip->dev;
  1591. if (dev)
  1592. seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
  1593. dev_name(dev));
  1594. if (chip->label)
  1595. seq_printf(s, ", %s", chip->label);
  1596. if (chip->can_sleep)
  1597. seq_printf(s, ", can sleep");
  1598. seq_printf(s, ":\n");
  1599. if (chip->dbg_show)
  1600. chip->dbg_show(s, chip);
  1601. else
  1602. gpiolib_dbg_show(s, chip);
  1603. return 0;
  1604. }
  1605. static const struct seq_operations gpiolib_seq_ops = {
  1606. .start = gpiolib_seq_start,
  1607. .next = gpiolib_seq_next,
  1608. .stop = gpiolib_seq_stop,
  1609. .show = gpiolib_seq_show,
  1610. };
  1611. static int gpiolib_open(struct inode *inode, struct file *file)
  1612. {
  1613. return seq_open(file, &gpiolib_seq_ops);
  1614. }
  1615. static const struct file_operations gpiolib_operations = {
  1616. .owner = THIS_MODULE,
  1617. .open = gpiolib_open,
  1618. .read = seq_read,
  1619. .llseek = seq_lseek,
  1620. .release = seq_release,
  1621. };
  1622. static int __init gpiolib_debugfs_init(void)
  1623. {
  1624. /* /sys/kernel/debug/gpio */
  1625. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1626. NULL, NULL, &gpiolib_operations);
  1627. return 0;
  1628. }
  1629. subsys_initcall(gpiolib_debugfs_init);
  1630. #endif /* DEBUG_FS */