gpiolib.c 64 KB

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