gpiolib.c 64 KB

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