gpiolib.c 50 KB

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