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