gpiolib.c 51 KB

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