gpiolib.c 49 KB

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