gpiolib.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656
  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 <linux/pinctrl/consumer.h>
  19. #include <linux/cdev.h>
  20. #include <linux/fs.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/compat.h>
  23. #include <linux/anon_inodes.h>
  24. #include <linux/file.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/poll.h>
  27. #include <linux/timekeeping.h>
  28. #include <uapi/linux/gpio.h>
  29. #include "gpiolib.h"
  30. #define CREATE_TRACE_POINTS
  31. #include <trace/events/gpio.h>
  32. /* Implementation infrastructure for GPIO interfaces.
  33. *
  34. * The GPIO programming interface allows for inlining speed-critical
  35. * get/set operations for common cases, so that access to SOC-integrated
  36. * GPIOs can sometimes cost only an instruction or two per bit.
  37. */
  38. /* When debugging, extend minimal trust to callers and platform code.
  39. * Also emit diagnostic messages that may help initial bringup, when
  40. * board setup or driver bugs are most common.
  41. *
  42. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  43. */
  44. #ifdef DEBUG
  45. #define extra_checks 1
  46. #else
  47. #define extra_checks 0
  48. #endif
  49. /* Device and char device-related information */
  50. static DEFINE_IDA(gpio_ida);
  51. static dev_t gpio_devt;
  52. #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
  53. static struct bus_type gpio_bus_type = {
  54. .name = "gpio",
  55. };
  56. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  57. * While any GPIO is requested, its gpio_chip is not removable;
  58. * each GPIO's "requested" flag serves as a lock and refcount.
  59. */
  60. DEFINE_SPINLOCK(gpio_lock);
  61. static DEFINE_MUTEX(gpio_lookup_lock);
  62. static LIST_HEAD(gpio_lookup_list);
  63. LIST_HEAD(gpio_devices);
  64. static void gpiochip_free_hogs(struct gpio_chip *chip);
  65. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
  66. static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
  67. static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
  68. static bool gpiolib_initialized;
  69. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  70. {
  71. d->label = label;
  72. }
  73. /**
  74. * Convert a GPIO number to its descriptor
  75. */
  76. struct gpio_desc *gpio_to_desc(unsigned gpio)
  77. {
  78. struct gpio_device *gdev;
  79. unsigned long flags;
  80. spin_lock_irqsave(&gpio_lock, flags);
  81. list_for_each_entry(gdev, &gpio_devices, list) {
  82. if (gdev->base <= gpio &&
  83. gdev->base + gdev->ngpio > gpio) {
  84. spin_unlock_irqrestore(&gpio_lock, flags);
  85. return &gdev->descs[gpio - gdev->base];
  86. }
  87. }
  88. spin_unlock_irqrestore(&gpio_lock, flags);
  89. if (!gpio_is_valid(gpio))
  90. WARN(1, "invalid GPIO %d\n", gpio);
  91. return NULL;
  92. }
  93. EXPORT_SYMBOL_GPL(gpio_to_desc);
  94. /**
  95. * Get the GPIO descriptor corresponding to the given hw number for this chip.
  96. */
  97. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
  98. u16 hwnum)
  99. {
  100. struct gpio_device *gdev = chip->gpiodev;
  101. if (hwnum >= gdev->ngpio)
  102. return ERR_PTR(-EINVAL);
  103. return &gdev->descs[hwnum];
  104. }
  105. /**
  106. * Convert a GPIO descriptor to the integer namespace.
  107. * This should disappear in the future but is needed since we still
  108. * use GPIO numbers for error messages and sysfs nodes
  109. */
  110. int desc_to_gpio(const struct gpio_desc *desc)
  111. {
  112. return desc->gdev->base + (desc - &desc->gdev->descs[0]);
  113. }
  114. EXPORT_SYMBOL_GPL(desc_to_gpio);
  115. /**
  116. * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
  117. * @desc: descriptor to return the chip of
  118. */
  119. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  120. {
  121. if (!desc || !desc->gdev || !desc->gdev->chip)
  122. return NULL;
  123. return desc->gdev->chip;
  124. }
  125. EXPORT_SYMBOL_GPL(gpiod_to_chip);
  126. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  127. static int gpiochip_find_base(int ngpio)
  128. {
  129. struct gpio_device *gdev;
  130. int base = ARCH_NR_GPIOS - ngpio;
  131. list_for_each_entry_reverse(gdev, &gpio_devices, list) {
  132. /* found a free space? */
  133. if (gdev->base + gdev->ngpio <= base)
  134. break;
  135. else
  136. /* nope, check the space right before the chip */
  137. base = gdev->base - ngpio;
  138. }
  139. if (gpio_is_valid(base)) {
  140. pr_debug("%s: found new base at %d\n", __func__, base);
  141. return base;
  142. } else {
  143. pr_err("%s: cannot find free range\n", __func__);
  144. return -ENOSPC;
  145. }
  146. }
  147. /**
  148. * gpiod_get_direction - return the current direction of a GPIO
  149. * @desc: GPIO to get the direction of
  150. *
  151. * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
  152. *
  153. * This function may sleep if gpiod_cansleep() is true.
  154. */
  155. int gpiod_get_direction(struct gpio_desc *desc)
  156. {
  157. struct gpio_chip *chip;
  158. unsigned offset;
  159. int status = -EINVAL;
  160. chip = gpiod_to_chip(desc);
  161. offset = gpio_chip_hwgpio(desc);
  162. if (!chip->get_direction)
  163. return status;
  164. status = chip->get_direction(chip, offset);
  165. if (status > 0) {
  166. /* GPIOF_DIR_IN, or other positive */
  167. status = 1;
  168. clear_bit(FLAG_IS_OUT, &desc->flags);
  169. }
  170. if (status == 0) {
  171. /* GPIOF_DIR_OUT */
  172. set_bit(FLAG_IS_OUT, &desc->flags);
  173. }
  174. return status;
  175. }
  176. EXPORT_SYMBOL_GPL(gpiod_get_direction);
  177. /*
  178. * Add a new chip to the global chips list, keeping the list of chips sorted
  179. * by range(means [base, base + ngpio - 1]) order.
  180. *
  181. * Return -EBUSY if the new chip overlaps with some other chip's integer
  182. * space.
  183. */
  184. static int gpiodev_add_to_list(struct gpio_device *gdev)
  185. {
  186. struct gpio_device *prev, *next;
  187. if (list_empty(&gpio_devices)) {
  188. /* initial entry in list */
  189. list_add_tail(&gdev->list, &gpio_devices);
  190. return 0;
  191. }
  192. next = list_entry(gpio_devices.next, struct gpio_device, list);
  193. if (gdev->base + gdev->ngpio <= next->base) {
  194. /* add before first entry */
  195. list_add(&gdev->list, &gpio_devices);
  196. return 0;
  197. }
  198. prev = list_entry(gpio_devices.prev, struct gpio_device, list);
  199. if (prev->base + prev->ngpio <= gdev->base) {
  200. /* add behind last entry */
  201. list_add_tail(&gdev->list, &gpio_devices);
  202. return 0;
  203. }
  204. list_for_each_entry_safe(prev, next, &gpio_devices, list) {
  205. /* at the end of the list */
  206. if (&next->list == &gpio_devices)
  207. break;
  208. /* add between prev and next */
  209. if (prev->base + prev->ngpio <= gdev->base
  210. && gdev->base + gdev->ngpio <= next->base) {
  211. list_add(&gdev->list, &prev->list);
  212. return 0;
  213. }
  214. }
  215. dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
  216. return -EBUSY;
  217. }
  218. /**
  219. * Convert a GPIO name to its descriptor
  220. */
  221. static struct gpio_desc *gpio_name_to_desc(const char * const name)
  222. {
  223. struct gpio_device *gdev;
  224. unsigned long flags;
  225. spin_lock_irqsave(&gpio_lock, flags);
  226. list_for_each_entry(gdev, &gpio_devices, list) {
  227. int i;
  228. for (i = 0; i != gdev->ngpio; ++i) {
  229. struct gpio_desc *desc = &gdev->descs[i];
  230. if (!desc->name || !name)
  231. continue;
  232. if (!strcmp(desc->name, name)) {
  233. spin_unlock_irqrestore(&gpio_lock, flags);
  234. return desc;
  235. }
  236. }
  237. }
  238. spin_unlock_irqrestore(&gpio_lock, flags);
  239. return NULL;
  240. }
  241. /*
  242. * Takes the names from gc->names and checks if they are all unique. If they
  243. * are, they are assigned to their gpio descriptors.
  244. *
  245. * Warning if one of the names is already used for a different GPIO.
  246. */
  247. static int gpiochip_set_desc_names(struct gpio_chip *gc)
  248. {
  249. struct gpio_device *gdev = gc->gpiodev;
  250. int i;
  251. if (!gc->names)
  252. return 0;
  253. /* First check all names if they are unique */
  254. for (i = 0; i != gc->ngpio; ++i) {
  255. struct gpio_desc *gpio;
  256. gpio = gpio_name_to_desc(gc->names[i]);
  257. if (gpio)
  258. dev_warn(&gdev->dev,
  259. "Detected name collision for GPIO name '%s'\n",
  260. gc->names[i]);
  261. }
  262. /* Then add all names to the GPIO descriptors */
  263. for (i = 0; i != gc->ngpio; ++i)
  264. gdev->descs[i].name = gc->names[i];
  265. return 0;
  266. }
  267. /*
  268. * GPIO line handle management
  269. */
  270. /**
  271. * struct linehandle_state - contains the state of a userspace handle
  272. * @gdev: the GPIO device the handle pertains to
  273. * @label: consumer label used to tag descriptors
  274. * @descs: the GPIO descriptors held by this handle
  275. * @numdescs: the number of descriptors held in the descs array
  276. */
  277. struct linehandle_state {
  278. struct gpio_device *gdev;
  279. const char *label;
  280. struct gpio_desc *descs[GPIOHANDLES_MAX];
  281. u32 numdescs;
  282. };
  283. #define GPIOHANDLE_REQUEST_VALID_FLAGS \
  284. (GPIOHANDLE_REQUEST_INPUT | \
  285. GPIOHANDLE_REQUEST_OUTPUT | \
  286. GPIOHANDLE_REQUEST_ACTIVE_LOW | \
  287. GPIOHANDLE_REQUEST_OPEN_DRAIN | \
  288. GPIOHANDLE_REQUEST_OPEN_SOURCE)
  289. static long linehandle_ioctl(struct file *filep, unsigned int cmd,
  290. unsigned long arg)
  291. {
  292. struct linehandle_state *lh = filep->private_data;
  293. void __user *ip = (void __user *)arg;
  294. struct gpiohandle_data ghd;
  295. int i;
  296. if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
  297. int val;
  298. memset(&ghd, 0, sizeof(ghd));
  299. /* TODO: check if descriptors are really input */
  300. for (i = 0; i < lh->numdescs; i++) {
  301. val = gpiod_get_value_cansleep(lh->descs[i]);
  302. if (val < 0)
  303. return val;
  304. ghd.values[i] = val;
  305. }
  306. if (copy_to_user(ip, &ghd, sizeof(ghd)))
  307. return -EFAULT;
  308. return 0;
  309. } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
  310. int vals[GPIOHANDLES_MAX];
  311. /* TODO: check if descriptors are really output */
  312. if (copy_from_user(&ghd, ip, sizeof(ghd)))
  313. return -EFAULT;
  314. /* Clamp all values to [0,1] */
  315. for (i = 0; i < lh->numdescs; i++)
  316. vals[i] = !!ghd.values[i];
  317. /* Reuse the array setting function */
  318. gpiod_set_array_value_complex(false,
  319. true,
  320. lh->numdescs,
  321. lh->descs,
  322. vals);
  323. return 0;
  324. }
  325. return -EINVAL;
  326. }
  327. #ifdef CONFIG_COMPAT
  328. static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
  329. unsigned long arg)
  330. {
  331. return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
  332. }
  333. #endif
  334. static int linehandle_release(struct inode *inode, struct file *filep)
  335. {
  336. struct linehandle_state *lh = filep->private_data;
  337. struct gpio_device *gdev = lh->gdev;
  338. int i;
  339. for (i = 0; i < lh->numdescs; i++)
  340. gpiod_free(lh->descs[i]);
  341. kfree(lh->label);
  342. kfree(lh);
  343. put_device(&gdev->dev);
  344. return 0;
  345. }
  346. static const struct file_operations linehandle_fileops = {
  347. .release = linehandle_release,
  348. .owner = THIS_MODULE,
  349. .llseek = noop_llseek,
  350. .unlocked_ioctl = linehandle_ioctl,
  351. #ifdef CONFIG_COMPAT
  352. .compat_ioctl = linehandle_ioctl_compat,
  353. #endif
  354. };
  355. static int linehandle_create(struct gpio_device *gdev, void __user *ip)
  356. {
  357. struct gpiohandle_request handlereq;
  358. struct linehandle_state *lh;
  359. struct file *file;
  360. int fd, i, ret;
  361. if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
  362. return -EFAULT;
  363. if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
  364. return -EINVAL;
  365. lh = kzalloc(sizeof(*lh), GFP_KERNEL);
  366. if (!lh)
  367. return -ENOMEM;
  368. lh->gdev = gdev;
  369. get_device(&gdev->dev);
  370. /* Make sure this is terminated */
  371. handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
  372. if (strlen(handlereq.consumer_label)) {
  373. lh->label = kstrdup(handlereq.consumer_label,
  374. GFP_KERNEL);
  375. if (!lh->label) {
  376. ret = -ENOMEM;
  377. goto out_free_lh;
  378. }
  379. }
  380. /* Request each GPIO */
  381. for (i = 0; i < handlereq.lines; i++) {
  382. u32 offset = handlereq.lineoffsets[i];
  383. u32 lflags = handlereq.flags;
  384. struct gpio_desc *desc;
  385. if (offset >= gdev->ngpio) {
  386. ret = -EINVAL;
  387. goto out_free_descs;
  388. }
  389. /* Return an error if a unknown flag is set */
  390. if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
  391. ret = -EINVAL;
  392. goto out_free_descs;
  393. }
  394. desc = &gdev->descs[offset];
  395. ret = gpiod_request(desc, lh->label);
  396. if (ret)
  397. goto out_free_descs;
  398. lh->descs[i] = desc;
  399. if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
  400. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  401. if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
  402. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  403. if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
  404. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  405. /*
  406. * Lines have to be requested explicitly for input
  407. * or output, else the line will be treated "as is".
  408. */
  409. if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
  410. int val = !!handlereq.default_values[i];
  411. ret = gpiod_direction_output(desc, val);
  412. if (ret)
  413. goto out_free_descs;
  414. } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
  415. ret = gpiod_direction_input(desc);
  416. if (ret)
  417. goto out_free_descs;
  418. }
  419. dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
  420. offset);
  421. }
  422. /* Let i point at the last handle */
  423. i--;
  424. lh->numdescs = handlereq.lines;
  425. fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
  426. if (fd < 0) {
  427. ret = fd;
  428. goto out_free_descs;
  429. }
  430. file = anon_inode_getfile("gpio-linehandle",
  431. &linehandle_fileops,
  432. lh,
  433. O_RDONLY | O_CLOEXEC);
  434. if (IS_ERR(file)) {
  435. ret = PTR_ERR(file);
  436. goto out_put_unused_fd;
  437. }
  438. handlereq.fd = fd;
  439. if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
  440. /*
  441. * fput() will trigger the release() callback, so do not go onto
  442. * the regular error cleanup path here.
  443. */
  444. fput(file);
  445. put_unused_fd(fd);
  446. return -EFAULT;
  447. }
  448. fd_install(fd, file);
  449. dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
  450. lh->numdescs);
  451. return 0;
  452. out_put_unused_fd:
  453. put_unused_fd(fd);
  454. out_free_descs:
  455. for (; i >= 0; i--)
  456. gpiod_free(lh->descs[i]);
  457. kfree(lh->label);
  458. out_free_lh:
  459. kfree(lh);
  460. put_device(&gdev->dev);
  461. return ret;
  462. }
  463. /*
  464. * GPIO line event management
  465. */
  466. /**
  467. * struct lineevent_state - contains the state of a userspace event
  468. * @gdev: the GPIO device the event pertains to
  469. * @label: consumer label used to tag descriptors
  470. * @desc: the GPIO descriptor held by this event
  471. * @eflags: the event flags this line was requested with
  472. * @irq: the interrupt that trigger in response to events on this GPIO
  473. * @wait: wait queue that handles blocking reads of events
  474. * @events: KFIFO for the GPIO events
  475. * @read_lock: mutex lock to protect reads from colliding with adding
  476. * new events to the FIFO
  477. */
  478. struct lineevent_state {
  479. struct gpio_device *gdev;
  480. const char *label;
  481. struct gpio_desc *desc;
  482. u32 eflags;
  483. int irq;
  484. wait_queue_head_t wait;
  485. DECLARE_KFIFO(events, struct gpioevent_data, 16);
  486. struct mutex read_lock;
  487. };
  488. #define GPIOEVENT_REQUEST_VALID_FLAGS \
  489. (GPIOEVENT_REQUEST_RISING_EDGE | \
  490. GPIOEVENT_REQUEST_FALLING_EDGE)
  491. static unsigned int lineevent_poll(struct file *filep,
  492. struct poll_table_struct *wait)
  493. {
  494. struct lineevent_state *le = filep->private_data;
  495. unsigned int events = 0;
  496. poll_wait(filep, &le->wait, wait);
  497. if (!kfifo_is_empty(&le->events))
  498. events = POLLIN | POLLRDNORM;
  499. return events;
  500. }
  501. static ssize_t lineevent_read(struct file *filep,
  502. char __user *buf,
  503. size_t count,
  504. loff_t *f_ps)
  505. {
  506. struct lineevent_state *le = filep->private_data;
  507. unsigned int copied;
  508. int ret;
  509. if (count < sizeof(struct gpioevent_data))
  510. return -EINVAL;
  511. do {
  512. if (kfifo_is_empty(&le->events)) {
  513. if (filep->f_flags & O_NONBLOCK)
  514. return -EAGAIN;
  515. ret = wait_event_interruptible(le->wait,
  516. !kfifo_is_empty(&le->events));
  517. if (ret)
  518. return ret;
  519. }
  520. if (mutex_lock_interruptible(&le->read_lock))
  521. return -ERESTARTSYS;
  522. ret = kfifo_to_user(&le->events, buf, count, &copied);
  523. mutex_unlock(&le->read_lock);
  524. if (ret)
  525. return ret;
  526. /*
  527. * If we couldn't read anything from the fifo (a different
  528. * thread might have been faster) we either return -EAGAIN if
  529. * the file descriptor is non-blocking, otherwise we go back to
  530. * sleep and wait for more data to arrive.
  531. */
  532. if (copied == 0 && (filep->f_flags & O_NONBLOCK))
  533. return -EAGAIN;
  534. } while (copied == 0);
  535. return copied;
  536. }
  537. static int lineevent_release(struct inode *inode, struct file *filep)
  538. {
  539. struct lineevent_state *le = filep->private_data;
  540. struct gpio_device *gdev = le->gdev;
  541. free_irq(le->irq, le);
  542. gpiod_free(le->desc);
  543. kfree(le->label);
  544. kfree(le);
  545. put_device(&gdev->dev);
  546. return 0;
  547. }
  548. static long lineevent_ioctl(struct file *filep, unsigned int cmd,
  549. unsigned long arg)
  550. {
  551. struct lineevent_state *le = filep->private_data;
  552. void __user *ip = (void __user *)arg;
  553. struct gpiohandle_data ghd;
  554. /*
  555. * We can get the value for an event line but not set it,
  556. * because it is input by definition.
  557. */
  558. if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
  559. int val;
  560. memset(&ghd, 0, sizeof(ghd));
  561. val = gpiod_get_value_cansleep(le->desc);
  562. if (val < 0)
  563. return val;
  564. ghd.values[0] = val;
  565. if (copy_to_user(ip, &ghd, sizeof(ghd)))
  566. return -EFAULT;
  567. return 0;
  568. }
  569. return -EINVAL;
  570. }
  571. #ifdef CONFIG_COMPAT
  572. static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
  573. unsigned long arg)
  574. {
  575. return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
  576. }
  577. #endif
  578. static const struct file_operations lineevent_fileops = {
  579. .release = lineevent_release,
  580. .read = lineevent_read,
  581. .poll = lineevent_poll,
  582. .owner = THIS_MODULE,
  583. .llseek = noop_llseek,
  584. .unlocked_ioctl = lineevent_ioctl,
  585. #ifdef CONFIG_COMPAT
  586. .compat_ioctl = lineevent_ioctl_compat,
  587. #endif
  588. };
  589. static irqreturn_t lineevent_irq_thread(int irq, void *p)
  590. {
  591. struct lineevent_state *le = p;
  592. struct gpioevent_data ge;
  593. int ret, level;
  594. ge.timestamp = ktime_get_real_ns();
  595. level = gpiod_get_value_cansleep(le->desc);
  596. if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
  597. && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
  598. if (level)
  599. /* Emit low-to-high event */
  600. ge.id = GPIOEVENT_EVENT_RISING_EDGE;
  601. else
  602. /* Emit high-to-low event */
  603. ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
  604. } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
  605. /* Emit low-to-high event */
  606. ge.id = GPIOEVENT_EVENT_RISING_EDGE;
  607. } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
  608. /* Emit high-to-low event */
  609. ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
  610. } else {
  611. return IRQ_NONE;
  612. }
  613. ret = kfifo_put(&le->events, ge);
  614. if (ret != 0)
  615. wake_up_poll(&le->wait, POLLIN);
  616. return IRQ_HANDLED;
  617. }
  618. static int lineevent_create(struct gpio_device *gdev, void __user *ip)
  619. {
  620. struct gpioevent_request eventreq;
  621. struct lineevent_state *le;
  622. struct gpio_desc *desc;
  623. struct file *file;
  624. u32 offset;
  625. u32 lflags;
  626. u32 eflags;
  627. int fd;
  628. int ret;
  629. int irqflags = 0;
  630. if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
  631. return -EFAULT;
  632. le = kzalloc(sizeof(*le), GFP_KERNEL);
  633. if (!le)
  634. return -ENOMEM;
  635. le->gdev = gdev;
  636. get_device(&gdev->dev);
  637. /* Make sure this is terminated */
  638. eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
  639. if (strlen(eventreq.consumer_label)) {
  640. le->label = kstrdup(eventreq.consumer_label,
  641. GFP_KERNEL);
  642. if (!le->label) {
  643. ret = -ENOMEM;
  644. goto out_free_le;
  645. }
  646. }
  647. offset = eventreq.lineoffset;
  648. lflags = eventreq.handleflags;
  649. eflags = eventreq.eventflags;
  650. if (offset >= gdev->ngpio) {
  651. ret = -EINVAL;
  652. goto out_free_label;
  653. }
  654. /* Return an error if a unknown flag is set */
  655. if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
  656. (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
  657. ret = -EINVAL;
  658. goto out_free_label;
  659. }
  660. /* This is just wrong: we don't look for events on output lines */
  661. if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
  662. ret = -EINVAL;
  663. goto out_free_label;
  664. }
  665. desc = &gdev->descs[offset];
  666. ret = gpiod_request(desc, le->label);
  667. if (ret)
  668. goto out_free_desc;
  669. le->desc = desc;
  670. le->eflags = eflags;
  671. if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
  672. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  673. if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
  674. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  675. if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
  676. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  677. ret = gpiod_direction_input(desc);
  678. if (ret)
  679. goto out_free_desc;
  680. le->irq = gpiod_to_irq(desc);
  681. if (le->irq <= 0) {
  682. ret = -ENODEV;
  683. goto out_free_desc;
  684. }
  685. if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
  686. irqflags |= IRQF_TRIGGER_RISING;
  687. if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
  688. irqflags |= IRQF_TRIGGER_FALLING;
  689. irqflags |= IRQF_ONESHOT;
  690. irqflags |= IRQF_SHARED;
  691. INIT_KFIFO(le->events);
  692. init_waitqueue_head(&le->wait);
  693. mutex_init(&le->read_lock);
  694. /* Request a thread to read the events */
  695. ret = request_threaded_irq(le->irq,
  696. NULL,
  697. lineevent_irq_thread,
  698. irqflags,
  699. le->label,
  700. le);
  701. if (ret)
  702. goto out_free_desc;
  703. fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
  704. if (fd < 0) {
  705. ret = fd;
  706. goto out_free_irq;
  707. }
  708. file = anon_inode_getfile("gpio-event",
  709. &lineevent_fileops,
  710. le,
  711. O_RDONLY | O_CLOEXEC);
  712. if (IS_ERR(file)) {
  713. ret = PTR_ERR(file);
  714. goto out_put_unused_fd;
  715. }
  716. eventreq.fd = fd;
  717. if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
  718. /*
  719. * fput() will trigger the release() callback, so do not go onto
  720. * the regular error cleanup path here.
  721. */
  722. fput(file);
  723. put_unused_fd(fd);
  724. return -EFAULT;
  725. }
  726. fd_install(fd, file);
  727. return 0;
  728. out_put_unused_fd:
  729. put_unused_fd(fd);
  730. out_free_irq:
  731. free_irq(le->irq, le);
  732. out_free_desc:
  733. gpiod_free(le->desc);
  734. out_free_label:
  735. kfree(le->label);
  736. out_free_le:
  737. kfree(le);
  738. put_device(&gdev->dev);
  739. return ret;
  740. }
  741. /**
  742. * gpio_ioctl() - ioctl handler for the GPIO chardev
  743. */
  744. static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  745. {
  746. struct gpio_device *gdev = filp->private_data;
  747. struct gpio_chip *chip = gdev->chip;
  748. void __user *ip = (void __user *)arg;
  749. /* We fail any subsequent ioctl():s when the chip is gone */
  750. if (!chip)
  751. return -ENODEV;
  752. /* Fill in the struct and pass to userspace */
  753. if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
  754. struct gpiochip_info chipinfo;
  755. memset(&chipinfo, 0, sizeof(chipinfo));
  756. strncpy(chipinfo.name, dev_name(&gdev->dev),
  757. sizeof(chipinfo.name));
  758. chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
  759. strncpy(chipinfo.label, gdev->label,
  760. sizeof(chipinfo.label));
  761. chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
  762. chipinfo.lines = gdev->ngpio;
  763. if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
  764. return -EFAULT;
  765. return 0;
  766. } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
  767. struct gpioline_info lineinfo;
  768. struct gpio_desc *desc;
  769. if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
  770. return -EFAULT;
  771. if (lineinfo.line_offset >= gdev->ngpio)
  772. return -EINVAL;
  773. desc = &gdev->descs[lineinfo.line_offset];
  774. if (desc->name) {
  775. strncpy(lineinfo.name, desc->name,
  776. sizeof(lineinfo.name));
  777. lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
  778. } else {
  779. lineinfo.name[0] = '\0';
  780. }
  781. if (desc->label) {
  782. strncpy(lineinfo.consumer, desc->label,
  783. sizeof(lineinfo.consumer));
  784. lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
  785. } else {
  786. lineinfo.consumer[0] = '\0';
  787. }
  788. /*
  789. * Userspace only need to know that the kernel is using
  790. * this GPIO so it can't use it.
  791. */
  792. lineinfo.flags = 0;
  793. if (test_bit(FLAG_REQUESTED, &desc->flags) ||
  794. test_bit(FLAG_IS_HOGGED, &desc->flags) ||
  795. test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
  796. test_bit(FLAG_EXPORT, &desc->flags) ||
  797. test_bit(FLAG_SYSFS, &desc->flags))
  798. lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
  799. if (test_bit(FLAG_IS_OUT, &desc->flags))
  800. lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
  801. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  802. lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
  803. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  804. lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
  805. if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  806. lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
  807. if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
  808. return -EFAULT;
  809. return 0;
  810. } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
  811. return linehandle_create(gdev, ip);
  812. } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
  813. return lineevent_create(gdev, ip);
  814. }
  815. return -EINVAL;
  816. }
  817. #ifdef CONFIG_COMPAT
  818. static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
  819. unsigned long arg)
  820. {
  821. return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  822. }
  823. #endif
  824. /**
  825. * gpio_chrdev_open() - open the chardev for ioctl operations
  826. * @inode: inode for this chardev
  827. * @filp: file struct for storing private data
  828. * Returns 0 on success
  829. */
  830. static int gpio_chrdev_open(struct inode *inode, struct file *filp)
  831. {
  832. struct gpio_device *gdev = container_of(inode->i_cdev,
  833. struct gpio_device, chrdev);
  834. /* Fail on open if the backing gpiochip is gone */
  835. if (!gdev || !gdev->chip)
  836. return -ENODEV;
  837. get_device(&gdev->dev);
  838. filp->private_data = gdev;
  839. return nonseekable_open(inode, filp);
  840. }
  841. /**
  842. * gpio_chrdev_release() - close chardev after ioctl operations
  843. * @inode: inode for this chardev
  844. * @filp: file struct for storing private data
  845. * Returns 0 on success
  846. */
  847. static int gpio_chrdev_release(struct inode *inode, struct file *filp)
  848. {
  849. struct gpio_device *gdev = container_of(inode->i_cdev,
  850. struct gpio_device, chrdev);
  851. if (!gdev)
  852. return -ENODEV;
  853. put_device(&gdev->dev);
  854. return 0;
  855. }
  856. static const struct file_operations gpio_fileops = {
  857. .release = gpio_chrdev_release,
  858. .open = gpio_chrdev_open,
  859. .owner = THIS_MODULE,
  860. .llseek = no_llseek,
  861. .unlocked_ioctl = gpio_ioctl,
  862. #ifdef CONFIG_COMPAT
  863. .compat_ioctl = gpio_ioctl_compat,
  864. #endif
  865. };
  866. static void gpiodevice_release(struct device *dev)
  867. {
  868. struct gpio_device *gdev = dev_get_drvdata(dev);
  869. list_del(&gdev->list);
  870. ida_simple_remove(&gpio_ida, gdev->id);
  871. kfree(gdev->label);
  872. kfree(gdev->descs);
  873. kfree(gdev);
  874. }
  875. static int gpiochip_setup_dev(struct gpio_device *gdev)
  876. {
  877. int status;
  878. cdev_init(&gdev->chrdev, &gpio_fileops);
  879. gdev->chrdev.owner = THIS_MODULE;
  880. gdev->chrdev.kobj.parent = &gdev->dev.kobj;
  881. gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
  882. status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
  883. if (status < 0)
  884. chip_warn(gdev->chip, "failed to add char device %d:%d\n",
  885. MAJOR(gpio_devt), gdev->id);
  886. else
  887. chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
  888. MAJOR(gpio_devt), gdev->id);
  889. status = device_add(&gdev->dev);
  890. if (status)
  891. goto err_remove_chardev;
  892. status = gpiochip_sysfs_register(gdev);
  893. if (status)
  894. goto err_remove_device;
  895. /* From this point, the .release() function cleans up gpio_device */
  896. gdev->dev.release = gpiodevice_release;
  897. pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
  898. __func__, gdev->base, gdev->base + gdev->ngpio - 1,
  899. dev_name(&gdev->dev), gdev->chip->label ? : "generic");
  900. return 0;
  901. err_remove_device:
  902. device_del(&gdev->dev);
  903. err_remove_chardev:
  904. cdev_del(&gdev->chrdev);
  905. return status;
  906. }
  907. static void gpiochip_setup_devs(void)
  908. {
  909. struct gpio_device *gdev;
  910. int err;
  911. list_for_each_entry(gdev, &gpio_devices, list) {
  912. err = gpiochip_setup_dev(gdev);
  913. if (err)
  914. pr_err("%s: Failed to initialize gpio device (%d)\n",
  915. dev_name(&gdev->dev), err);
  916. }
  917. }
  918. /**
  919. * gpiochip_add_data() - register a gpio_chip
  920. * @chip: the chip to register, with chip->base initialized
  921. * Context: potentially before irqs will work
  922. *
  923. * Returns a negative errno if the chip can't be registered, such as
  924. * because the chip->base is invalid or already associated with a
  925. * different chip. Otherwise it returns zero as a success code.
  926. *
  927. * When gpiochip_add_data() is called very early during boot, so that GPIOs
  928. * can be freely used, the chip->parent device must be registered before
  929. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  930. * for GPIOs will fail rudely.
  931. *
  932. * gpiochip_add_data() must only be called after gpiolib initialization,
  933. * ie after core_initcall().
  934. *
  935. * If chip->base is negative, this requests dynamic assignment of
  936. * a range of valid GPIOs.
  937. */
  938. int gpiochip_add_data(struct gpio_chip *chip, void *data)
  939. {
  940. unsigned long flags;
  941. int status = 0;
  942. unsigned i;
  943. int base = chip->base;
  944. struct gpio_device *gdev;
  945. /*
  946. * First: allocate and populate the internal stat container, and
  947. * set up the struct device.
  948. */
  949. gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  950. if (!gdev)
  951. return -ENOMEM;
  952. gdev->dev.bus = &gpio_bus_type;
  953. gdev->chip = chip;
  954. chip->gpiodev = gdev;
  955. if (chip->parent) {
  956. gdev->dev.parent = chip->parent;
  957. gdev->dev.of_node = chip->parent->of_node;
  958. }
  959. #ifdef CONFIG_OF_GPIO
  960. /* If the gpiochip has an assigned OF node this takes precedence */
  961. if (chip->of_node)
  962. gdev->dev.of_node = chip->of_node;
  963. #endif
  964. gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
  965. if (gdev->id < 0) {
  966. status = gdev->id;
  967. goto err_free_gdev;
  968. }
  969. dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
  970. device_initialize(&gdev->dev);
  971. dev_set_drvdata(&gdev->dev, gdev);
  972. if (chip->parent && chip->parent->driver)
  973. gdev->owner = chip->parent->driver->owner;
  974. else if (chip->owner)
  975. /* TODO: remove chip->owner */
  976. gdev->owner = chip->owner;
  977. else
  978. gdev->owner = THIS_MODULE;
  979. gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
  980. if (!gdev->descs) {
  981. status = -ENOMEM;
  982. goto err_free_gdev;
  983. }
  984. if (chip->ngpio == 0) {
  985. chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
  986. status = -EINVAL;
  987. goto err_free_descs;
  988. }
  989. if (chip->label)
  990. gdev->label = kstrdup(chip->label, GFP_KERNEL);
  991. else
  992. gdev->label = kstrdup("unknown", GFP_KERNEL);
  993. if (!gdev->label) {
  994. status = -ENOMEM;
  995. goto err_free_descs;
  996. }
  997. gdev->ngpio = chip->ngpio;
  998. gdev->data = data;
  999. spin_lock_irqsave(&gpio_lock, flags);
  1000. /*
  1001. * TODO: this allocates a Linux GPIO number base in the global
  1002. * GPIO numberspace for this chip. In the long run we want to
  1003. * get *rid* of this numberspace and use only descriptors, but
  1004. * it may be a pipe dream. It will not happen before we get rid
  1005. * of the sysfs interface anyways.
  1006. */
  1007. if (base < 0) {
  1008. base = gpiochip_find_base(chip->ngpio);
  1009. if (base < 0) {
  1010. status = base;
  1011. spin_unlock_irqrestore(&gpio_lock, flags);
  1012. goto err_free_label;
  1013. }
  1014. /*
  1015. * TODO: it should not be necessary to reflect the assigned
  1016. * base outside of the GPIO subsystem. Go over drivers and
  1017. * see if anyone makes use of this, else drop this and assign
  1018. * a poison instead.
  1019. */
  1020. chip->base = base;
  1021. }
  1022. gdev->base = base;
  1023. status = gpiodev_add_to_list(gdev);
  1024. if (status) {
  1025. spin_unlock_irqrestore(&gpio_lock, flags);
  1026. goto err_free_label;
  1027. }
  1028. spin_unlock_irqrestore(&gpio_lock, flags);
  1029. for (i = 0; i < chip->ngpio; i++) {
  1030. struct gpio_desc *desc = &gdev->descs[i];
  1031. desc->gdev = gdev;
  1032. /*
  1033. * REVISIT: most hardware initializes GPIOs as inputs
  1034. * (often with pullups enabled) so power usage is
  1035. * minimized. Linux code should set the gpio direction
  1036. * first thing; but until it does, and in case
  1037. * chip->get_direction is not set, we may expose the
  1038. * wrong direction in sysfs.
  1039. */
  1040. if (chip->get_direction) {
  1041. /*
  1042. * If we have .get_direction, set up the initial
  1043. * direction flag from the hardware.
  1044. */
  1045. int dir = chip->get_direction(chip, i);
  1046. if (!dir)
  1047. set_bit(FLAG_IS_OUT, &desc->flags);
  1048. } else if (!chip->direction_input) {
  1049. /*
  1050. * If the chip lacks the .direction_input callback
  1051. * we logically assume all lines are outputs.
  1052. */
  1053. set_bit(FLAG_IS_OUT, &desc->flags);
  1054. }
  1055. }
  1056. #ifdef CONFIG_PINCTRL
  1057. INIT_LIST_HEAD(&gdev->pin_ranges);
  1058. #endif
  1059. status = gpiochip_set_desc_names(chip);
  1060. if (status)
  1061. goto err_remove_from_list;
  1062. status = gpiochip_irqchip_init_valid_mask(chip);
  1063. if (status)
  1064. goto err_remove_from_list;
  1065. status = of_gpiochip_add(chip);
  1066. if (status)
  1067. goto err_remove_chip;
  1068. acpi_gpiochip_add(chip);
  1069. /*
  1070. * By first adding the chardev, and then adding the device,
  1071. * we get a device node entry in sysfs under
  1072. * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
  1073. * coldplug of device nodes and other udev business.
  1074. * We can do this only if gpiolib has been initialized.
  1075. * Otherwise, defer until later.
  1076. */
  1077. if (gpiolib_initialized) {
  1078. status = gpiochip_setup_dev(gdev);
  1079. if (status)
  1080. goto err_remove_chip;
  1081. }
  1082. return 0;
  1083. err_remove_chip:
  1084. acpi_gpiochip_remove(chip);
  1085. gpiochip_free_hogs(chip);
  1086. of_gpiochip_remove(chip);
  1087. gpiochip_irqchip_free_valid_mask(chip);
  1088. err_remove_from_list:
  1089. spin_lock_irqsave(&gpio_lock, flags);
  1090. list_del(&gdev->list);
  1091. spin_unlock_irqrestore(&gpio_lock, flags);
  1092. err_free_label:
  1093. kfree(gdev->label);
  1094. err_free_descs:
  1095. kfree(gdev->descs);
  1096. err_free_gdev:
  1097. ida_simple_remove(&gpio_ida, gdev->id);
  1098. /* failures here can mean systems won't boot... */
  1099. pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
  1100. gdev->base, gdev->base + gdev->ngpio - 1,
  1101. chip->label ? : "generic");
  1102. kfree(gdev);
  1103. return status;
  1104. }
  1105. EXPORT_SYMBOL_GPL(gpiochip_add_data);
  1106. /**
  1107. * gpiochip_get_data() - get per-subdriver data for the chip
  1108. */
  1109. void *gpiochip_get_data(struct gpio_chip *chip)
  1110. {
  1111. return chip->gpiodev->data;
  1112. }
  1113. EXPORT_SYMBOL_GPL(gpiochip_get_data);
  1114. /**
  1115. * gpiochip_remove() - unregister a gpio_chip
  1116. * @chip: the chip to unregister
  1117. *
  1118. * A gpio_chip with any GPIOs still requested may not be removed.
  1119. */
  1120. void gpiochip_remove(struct gpio_chip *chip)
  1121. {
  1122. struct gpio_device *gdev = chip->gpiodev;
  1123. struct gpio_desc *desc;
  1124. unsigned long flags;
  1125. unsigned i;
  1126. bool requested = false;
  1127. /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
  1128. gpiochip_sysfs_unregister(gdev);
  1129. gpiochip_free_hogs(chip);
  1130. /* Numb the device, cancelling all outstanding operations */
  1131. gdev->chip = NULL;
  1132. gpiochip_irqchip_remove(chip);
  1133. acpi_gpiochip_remove(chip);
  1134. gpiochip_remove_pin_ranges(chip);
  1135. of_gpiochip_remove(chip);
  1136. /*
  1137. * We accept no more calls into the driver from this point, so
  1138. * NULL the driver data pointer
  1139. */
  1140. gdev->data = NULL;
  1141. spin_lock_irqsave(&gpio_lock, flags);
  1142. for (i = 0; i < gdev->ngpio; i++) {
  1143. desc = &gdev->descs[i];
  1144. if (test_bit(FLAG_REQUESTED, &desc->flags))
  1145. requested = true;
  1146. }
  1147. spin_unlock_irqrestore(&gpio_lock, flags);
  1148. if (requested)
  1149. dev_crit(&gdev->dev,
  1150. "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
  1151. /*
  1152. * The gpiochip side puts its use of the device to rest here:
  1153. * if there are no userspace clients, the chardev and device will
  1154. * be removed, else it will be dangling until the last user is
  1155. * gone.
  1156. */
  1157. cdev_del(&gdev->chrdev);
  1158. device_del(&gdev->dev);
  1159. put_device(&gdev->dev);
  1160. }
  1161. EXPORT_SYMBOL_GPL(gpiochip_remove);
  1162. static void devm_gpio_chip_release(struct device *dev, void *res)
  1163. {
  1164. struct gpio_chip *chip = *(struct gpio_chip **)res;
  1165. gpiochip_remove(chip);
  1166. }
  1167. static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
  1168. {
  1169. struct gpio_chip **r = res;
  1170. if (!r || !*r) {
  1171. WARN_ON(!r || !*r);
  1172. return 0;
  1173. }
  1174. return *r == data;
  1175. }
  1176. /**
  1177. * devm_gpiochip_add_data() - Resource manager piochip_add_data()
  1178. * @dev: the device pointer on which irq_chip belongs to.
  1179. * @chip: the chip to register, with chip->base initialized
  1180. * Context: potentially before irqs will work
  1181. *
  1182. * Returns a negative errno if the chip can't be registered, such as
  1183. * because the chip->base is invalid or already associated with a
  1184. * different chip. Otherwise it returns zero as a success code.
  1185. *
  1186. * The gpio chip automatically be released when the device is unbound.
  1187. */
  1188. int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
  1189. void *data)
  1190. {
  1191. struct gpio_chip **ptr;
  1192. int ret;
  1193. ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
  1194. GFP_KERNEL);
  1195. if (!ptr)
  1196. return -ENOMEM;
  1197. ret = gpiochip_add_data(chip, data);
  1198. if (ret < 0) {
  1199. devres_free(ptr);
  1200. return ret;
  1201. }
  1202. *ptr = chip;
  1203. devres_add(dev, ptr);
  1204. return 0;
  1205. }
  1206. EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
  1207. /**
  1208. * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
  1209. * @dev: device for which which resource was allocated
  1210. * @chip: the chip to remove
  1211. *
  1212. * A gpio_chip with any GPIOs still requested may not be removed.
  1213. */
  1214. void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
  1215. {
  1216. int ret;
  1217. ret = devres_release(dev, devm_gpio_chip_release,
  1218. devm_gpio_chip_match, chip);
  1219. if (!ret)
  1220. WARN_ON(ret);
  1221. }
  1222. EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
  1223. /**
  1224. * gpiochip_find() - iterator for locating a specific gpio_chip
  1225. * @data: data to pass to match function
  1226. * @callback: Callback function to check gpio_chip
  1227. *
  1228. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  1229. * determined by a user supplied @match callback. The callback should return
  1230. * 0 if the device doesn't match and non-zero if it does. If the callback is
  1231. * non-zero, this function will return to the caller and not iterate over any
  1232. * more gpio_chips.
  1233. */
  1234. struct gpio_chip *gpiochip_find(void *data,
  1235. int (*match)(struct gpio_chip *chip,
  1236. void *data))
  1237. {
  1238. struct gpio_device *gdev;
  1239. struct gpio_chip *chip = NULL;
  1240. unsigned long flags;
  1241. spin_lock_irqsave(&gpio_lock, flags);
  1242. list_for_each_entry(gdev, &gpio_devices, list)
  1243. if (gdev->chip && match(gdev->chip, data)) {
  1244. chip = gdev->chip;
  1245. break;
  1246. }
  1247. spin_unlock_irqrestore(&gpio_lock, flags);
  1248. return chip;
  1249. }
  1250. EXPORT_SYMBOL_GPL(gpiochip_find);
  1251. static int gpiochip_match_name(struct gpio_chip *chip, void *data)
  1252. {
  1253. const char *name = data;
  1254. return !strcmp(chip->label, name);
  1255. }
  1256. static struct gpio_chip *find_chip_by_name(const char *name)
  1257. {
  1258. return gpiochip_find((void *)name, gpiochip_match_name);
  1259. }
  1260. #ifdef CONFIG_GPIOLIB_IRQCHIP
  1261. /*
  1262. * The following is irqchip helper code for gpiochips.
  1263. */
  1264. static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
  1265. {
  1266. int i;
  1267. if (!gpiochip->irq_need_valid_mask)
  1268. return 0;
  1269. gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
  1270. sizeof(long), GFP_KERNEL);
  1271. if (!gpiochip->irq_valid_mask)
  1272. return -ENOMEM;
  1273. /* Assume by default all GPIOs are valid */
  1274. for (i = 0; i < gpiochip->ngpio; i++)
  1275. set_bit(i, gpiochip->irq_valid_mask);
  1276. return 0;
  1277. }
  1278. static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
  1279. {
  1280. kfree(gpiochip->irq_valid_mask);
  1281. gpiochip->irq_valid_mask = NULL;
  1282. }
  1283. static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
  1284. unsigned int offset)
  1285. {
  1286. /* No mask means all valid */
  1287. if (likely(!gpiochip->irq_valid_mask))
  1288. return true;
  1289. return test_bit(offset, gpiochip->irq_valid_mask);
  1290. }
  1291. /**
  1292. * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
  1293. * @gpiochip: the gpiochip to set the irqchip chain to
  1294. * @irqchip: the irqchip to chain to the gpiochip
  1295. * @parent_irq: the irq number corresponding to the parent IRQ for this
  1296. * chained irqchip
  1297. * @parent_handler: the parent interrupt handler for the accumulated IRQ
  1298. * coming out of the gpiochip. If the interrupt is nested rather than
  1299. * cascaded, pass NULL in this handler argument
  1300. */
  1301. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  1302. struct irq_chip *irqchip,
  1303. int parent_irq,
  1304. irq_flow_handler_t parent_handler)
  1305. {
  1306. unsigned int offset;
  1307. if (!gpiochip->irqdomain) {
  1308. chip_err(gpiochip, "called %s before setting up irqchip\n",
  1309. __func__);
  1310. return;
  1311. }
  1312. if (parent_handler) {
  1313. if (gpiochip->can_sleep) {
  1314. chip_err(gpiochip,
  1315. "you cannot have chained interrupts on a "
  1316. "chip that may sleep\n");
  1317. return;
  1318. }
  1319. /*
  1320. * The parent irqchip is already using the chip_data for this
  1321. * irqchip, so our callbacks simply use the handler_data.
  1322. */
  1323. irq_set_chained_handler_and_data(parent_irq, parent_handler,
  1324. gpiochip);
  1325. gpiochip->irq_parent = parent_irq;
  1326. }
  1327. /* Set the parent IRQ for all affected IRQs */
  1328. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  1329. if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
  1330. continue;
  1331. irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
  1332. parent_irq);
  1333. }
  1334. }
  1335. EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
  1336. /**
  1337. * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
  1338. * @d: the irqdomain used by this irqchip
  1339. * @irq: the global irq number used by this GPIO irqchip irq
  1340. * @hwirq: the local IRQ/GPIO line offset on this gpiochip
  1341. *
  1342. * This function will set up the mapping for a certain IRQ line on a
  1343. * gpiochip by assigning the gpiochip as chip data, and using the irqchip
  1344. * stored inside the gpiochip.
  1345. */
  1346. static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  1347. irq_hw_number_t hwirq)
  1348. {
  1349. struct gpio_chip *chip = d->host_data;
  1350. irq_set_chip_data(irq, chip);
  1351. /*
  1352. * This lock class tells lockdep that GPIO irqs are in a different
  1353. * category than their parents, so it won't report false recursion.
  1354. */
  1355. irq_set_lockdep_class(irq, chip->lock_key);
  1356. irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
  1357. /* Chips that can sleep need nested thread handlers */
  1358. if (chip->can_sleep && !chip->irq_not_threaded)
  1359. irq_set_nested_thread(irq, 1);
  1360. irq_set_noprobe(irq);
  1361. /*
  1362. * No set-up of the hardware will happen if IRQ_TYPE_NONE
  1363. * is passed as default type.
  1364. */
  1365. if (chip->irq_default_type != IRQ_TYPE_NONE)
  1366. irq_set_irq_type(irq, chip->irq_default_type);
  1367. return 0;
  1368. }
  1369. static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
  1370. {
  1371. struct gpio_chip *chip = d->host_data;
  1372. if (chip->can_sleep)
  1373. irq_set_nested_thread(irq, 0);
  1374. irq_set_chip_and_handler(irq, NULL, NULL);
  1375. irq_set_chip_data(irq, NULL);
  1376. }
  1377. static const struct irq_domain_ops gpiochip_domain_ops = {
  1378. .map = gpiochip_irq_map,
  1379. .unmap = gpiochip_irq_unmap,
  1380. /* Virtually all GPIO irqchips are twocell:ed */
  1381. .xlate = irq_domain_xlate_twocell,
  1382. };
  1383. static int gpiochip_irq_reqres(struct irq_data *d)
  1384. {
  1385. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  1386. if (!try_module_get(chip->gpiodev->owner))
  1387. return -ENODEV;
  1388. if (gpiochip_lock_as_irq(chip, d->hwirq)) {
  1389. chip_err(chip,
  1390. "unable to lock HW IRQ %lu for IRQ\n",
  1391. d->hwirq);
  1392. module_put(chip->gpiodev->owner);
  1393. return -EINVAL;
  1394. }
  1395. return 0;
  1396. }
  1397. static void gpiochip_irq_relres(struct irq_data *d)
  1398. {
  1399. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  1400. gpiochip_unlock_as_irq(chip, d->hwirq);
  1401. module_put(chip->gpiodev->owner);
  1402. }
  1403. static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
  1404. {
  1405. return irq_find_mapping(chip->irqdomain, offset);
  1406. }
  1407. /**
  1408. * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
  1409. * @gpiochip: the gpiochip to remove the irqchip from
  1410. *
  1411. * This is called only from gpiochip_remove()
  1412. */
  1413. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
  1414. {
  1415. unsigned int offset;
  1416. acpi_gpiochip_free_interrupts(gpiochip);
  1417. if (gpiochip->irq_parent) {
  1418. irq_set_chained_handler(gpiochip->irq_parent, NULL);
  1419. irq_set_handler_data(gpiochip->irq_parent, NULL);
  1420. }
  1421. /* Remove all IRQ mappings and delete the domain */
  1422. if (gpiochip->irqdomain) {
  1423. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  1424. if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
  1425. continue;
  1426. irq_dispose_mapping(
  1427. irq_find_mapping(gpiochip->irqdomain, offset));
  1428. }
  1429. irq_domain_remove(gpiochip->irqdomain);
  1430. }
  1431. if (gpiochip->irqchip) {
  1432. gpiochip->irqchip->irq_request_resources = NULL;
  1433. gpiochip->irqchip->irq_release_resources = NULL;
  1434. gpiochip->irqchip = NULL;
  1435. }
  1436. gpiochip_irqchip_free_valid_mask(gpiochip);
  1437. }
  1438. /**
  1439. * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
  1440. * @gpiochip: the gpiochip to add the irqchip to
  1441. * @irqchip: the irqchip to add to the gpiochip
  1442. * @first_irq: if not dynamically assigned, the base (first) IRQ to
  1443. * allocate gpiochip irqs from
  1444. * @handler: the irq handler to use (often a predefined irq core function)
  1445. * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
  1446. * to have the core avoid setting up any default type in the hardware.
  1447. * @lock_key: lockdep class
  1448. *
  1449. * This function closely associates a certain irqchip with a certain
  1450. * gpiochip, providing an irq domain to translate the local IRQs to
  1451. * global irqs in the gpiolib core, and making sure that the gpiochip
  1452. * is passed as chip data to all related functions. Driver callbacks
  1453. * need to use gpiochip_get_data() to get their local state containers back
  1454. * from the gpiochip passed as chip data. An irqdomain will be stored
  1455. * in the gpiochip that shall be used by the driver to handle IRQ number
  1456. * translation. The gpiochip will need to be initialized and registered
  1457. * before calling this function.
  1458. *
  1459. * This function will handle two cell:ed simple IRQs and assumes all
  1460. * the pins on the gpiochip can generate a unique IRQ. Everything else
  1461. * need to be open coded.
  1462. */
  1463. int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  1464. struct irq_chip *irqchip,
  1465. unsigned int first_irq,
  1466. irq_flow_handler_t handler,
  1467. unsigned int type,
  1468. struct lock_class_key *lock_key)
  1469. {
  1470. struct device_node *of_node;
  1471. bool irq_base_set = false;
  1472. unsigned int offset;
  1473. unsigned irq_base = 0;
  1474. if (!gpiochip || !irqchip)
  1475. return -EINVAL;
  1476. if (!gpiochip->parent) {
  1477. pr_err("missing gpiochip .dev parent pointer\n");
  1478. return -EINVAL;
  1479. }
  1480. of_node = gpiochip->parent->of_node;
  1481. #ifdef CONFIG_OF_GPIO
  1482. /*
  1483. * If the gpiochip has an assigned OF node this takes precedence
  1484. * FIXME: get rid of this and use gpiochip->parent->of_node
  1485. * everywhere
  1486. */
  1487. if (gpiochip->of_node)
  1488. of_node = gpiochip->of_node;
  1489. #endif
  1490. /*
  1491. * Specifying a default trigger is a terrible idea if DT or ACPI is
  1492. * used to configure the interrupts, as you may end-up with
  1493. * conflicting triggers. Tell the user, and reset to NONE.
  1494. */
  1495. if (WARN(of_node && type != IRQ_TYPE_NONE,
  1496. "%s: Ignoring %d default trigger\n", of_node->full_name, type))
  1497. type = IRQ_TYPE_NONE;
  1498. if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
  1499. acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
  1500. "Ignoring %d default trigger\n", type);
  1501. type = IRQ_TYPE_NONE;
  1502. }
  1503. gpiochip->irqchip = irqchip;
  1504. gpiochip->irq_handler = handler;
  1505. gpiochip->irq_default_type = type;
  1506. gpiochip->to_irq = gpiochip_to_irq;
  1507. gpiochip->lock_key = lock_key;
  1508. gpiochip->irqdomain = irq_domain_add_simple(of_node,
  1509. gpiochip->ngpio, first_irq,
  1510. &gpiochip_domain_ops, gpiochip);
  1511. if (!gpiochip->irqdomain) {
  1512. gpiochip->irqchip = NULL;
  1513. return -EINVAL;
  1514. }
  1515. /*
  1516. * It is possible for a driver to override this, but only if the
  1517. * alternative functions are both implemented.
  1518. */
  1519. if (!irqchip->irq_request_resources &&
  1520. !irqchip->irq_release_resources) {
  1521. irqchip->irq_request_resources = gpiochip_irq_reqres;
  1522. irqchip->irq_release_resources = gpiochip_irq_relres;
  1523. }
  1524. /*
  1525. * Prepare the mapping since the irqchip shall be orthogonal to
  1526. * any gpiochip calls. If the first_irq was zero, this is
  1527. * necessary to allocate descriptors for all IRQs.
  1528. */
  1529. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  1530. if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
  1531. continue;
  1532. irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
  1533. if (!irq_base_set) {
  1534. /*
  1535. * Store the base into the gpiochip to be used when
  1536. * unmapping the irqs.
  1537. */
  1538. gpiochip->irq_base = irq_base;
  1539. irq_base_set = true;
  1540. }
  1541. }
  1542. acpi_gpiochip_request_interrupts(gpiochip);
  1543. return 0;
  1544. }
  1545. EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
  1546. #else /* CONFIG_GPIOLIB_IRQCHIP */
  1547. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
  1548. static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
  1549. {
  1550. return 0;
  1551. }
  1552. static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
  1553. { }
  1554. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  1555. /**
  1556. * gpiochip_generic_request() - request the gpio function for a pin
  1557. * @chip: the gpiochip owning the GPIO
  1558. * @offset: the offset of the GPIO to request for GPIO function
  1559. */
  1560. int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
  1561. {
  1562. return pinctrl_request_gpio(chip->gpiodev->base + offset);
  1563. }
  1564. EXPORT_SYMBOL_GPL(gpiochip_generic_request);
  1565. /**
  1566. * gpiochip_generic_free() - free the gpio function from a pin
  1567. * @chip: the gpiochip to request the gpio function for
  1568. * @offset: the offset of the GPIO to free from GPIO function
  1569. */
  1570. void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
  1571. {
  1572. pinctrl_free_gpio(chip->gpiodev->base + offset);
  1573. }
  1574. EXPORT_SYMBOL_GPL(gpiochip_generic_free);
  1575. #ifdef CONFIG_PINCTRL
  1576. /**
  1577. * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
  1578. * @chip: the gpiochip to add the range for
  1579. * @pctldev: the pin controller to map to
  1580. * @gpio_offset: the start offset in the current gpio_chip number space
  1581. * @pin_group: name of the pin group inside the pin controller
  1582. */
  1583. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  1584. struct pinctrl_dev *pctldev,
  1585. unsigned int gpio_offset, const char *pin_group)
  1586. {
  1587. struct gpio_pin_range *pin_range;
  1588. struct gpio_device *gdev = chip->gpiodev;
  1589. int ret;
  1590. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1591. if (!pin_range) {
  1592. chip_err(chip, "failed to allocate pin ranges\n");
  1593. return -ENOMEM;
  1594. }
  1595. /* Use local offset as range ID */
  1596. pin_range->range.id = gpio_offset;
  1597. pin_range->range.gc = chip;
  1598. pin_range->range.name = chip->label;
  1599. pin_range->range.base = gdev->base + gpio_offset;
  1600. pin_range->pctldev = pctldev;
  1601. ret = pinctrl_get_group_pins(pctldev, pin_group,
  1602. &pin_range->range.pins,
  1603. &pin_range->range.npins);
  1604. if (ret < 0) {
  1605. kfree(pin_range);
  1606. return ret;
  1607. }
  1608. pinctrl_add_gpio_range(pctldev, &pin_range->range);
  1609. chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
  1610. gpio_offset, gpio_offset + pin_range->range.npins - 1,
  1611. pinctrl_dev_get_devname(pctldev), pin_group);
  1612. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1613. return 0;
  1614. }
  1615. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  1616. /**
  1617. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  1618. * @chip: the gpiochip to add the range for
  1619. * @pinctrl_name: the dev_name() of the pin controller to map to
  1620. * @gpio_offset: the start offset in the current gpio_chip number space
  1621. * @pin_offset: the start offset in the pin controller number space
  1622. * @npins: the number of pins from the offset of each pin space (GPIO and
  1623. * pin controller) to accumulate in this range
  1624. */
  1625. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  1626. unsigned int gpio_offset, unsigned int pin_offset,
  1627. unsigned int npins)
  1628. {
  1629. struct gpio_pin_range *pin_range;
  1630. struct gpio_device *gdev = chip->gpiodev;
  1631. int ret;
  1632. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1633. if (!pin_range) {
  1634. chip_err(chip, "failed to allocate pin ranges\n");
  1635. return -ENOMEM;
  1636. }
  1637. /* Use local offset as range ID */
  1638. pin_range->range.id = gpio_offset;
  1639. pin_range->range.gc = chip;
  1640. pin_range->range.name = chip->label;
  1641. pin_range->range.base = gdev->base + gpio_offset;
  1642. pin_range->range.pin_base = pin_offset;
  1643. pin_range->range.npins = npins;
  1644. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1645. &pin_range->range);
  1646. if (IS_ERR(pin_range->pctldev)) {
  1647. ret = PTR_ERR(pin_range->pctldev);
  1648. chip_err(chip, "could not create pin range\n");
  1649. kfree(pin_range);
  1650. return ret;
  1651. }
  1652. chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1653. gpio_offset, gpio_offset + npins - 1,
  1654. pinctl_name,
  1655. pin_offset, pin_offset + npins - 1);
  1656. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1657. return 0;
  1658. }
  1659. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1660. /**
  1661. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1662. * @chip: the chip to remove all the mappings for
  1663. */
  1664. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  1665. {
  1666. struct gpio_pin_range *pin_range, *tmp;
  1667. struct gpio_device *gdev = chip->gpiodev;
  1668. list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
  1669. list_del(&pin_range->node);
  1670. pinctrl_remove_gpio_range(pin_range->pctldev,
  1671. &pin_range->range);
  1672. kfree(pin_range);
  1673. }
  1674. }
  1675. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1676. #endif /* CONFIG_PINCTRL */
  1677. /* These "optional" allocation calls help prevent drivers from stomping
  1678. * on each other, and help provide better diagnostics in debugfs.
  1679. * They're called even less than the "set direction" calls.
  1680. */
  1681. static int __gpiod_request(struct gpio_desc *desc, const char *label)
  1682. {
  1683. struct gpio_chip *chip = desc->gdev->chip;
  1684. int status;
  1685. unsigned long flags;
  1686. spin_lock_irqsave(&gpio_lock, flags);
  1687. /* NOTE: gpio_request() can be called in early boot,
  1688. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1689. */
  1690. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1691. desc_set_label(desc, label ? : "?");
  1692. status = 0;
  1693. } else {
  1694. status = -EBUSY;
  1695. goto done;
  1696. }
  1697. if (chip->request) {
  1698. /* chip->request may sleep */
  1699. spin_unlock_irqrestore(&gpio_lock, flags);
  1700. status = chip->request(chip, gpio_chip_hwgpio(desc));
  1701. spin_lock_irqsave(&gpio_lock, flags);
  1702. if (status < 0) {
  1703. desc_set_label(desc, NULL);
  1704. clear_bit(FLAG_REQUESTED, &desc->flags);
  1705. goto done;
  1706. }
  1707. }
  1708. if (chip->get_direction) {
  1709. /* chip->get_direction may sleep */
  1710. spin_unlock_irqrestore(&gpio_lock, flags);
  1711. gpiod_get_direction(desc);
  1712. spin_lock_irqsave(&gpio_lock, flags);
  1713. }
  1714. done:
  1715. spin_unlock_irqrestore(&gpio_lock, flags);
  1716. return status;
  1717. }
  1718. /*
  1719. * This descriptor validation needs to be inserted verbatim into each
  1720. * function taking a descriptor, so we need to use a preprocessor
  1721. * macro to avoid endless duplication. If the desc is NULL it is an
  1722. * optional GPIO and calls should just bail out.
  1723. */
  1724. #define VALIDATE_DESC(desc) do { \
  1725. if (!desc) \
  1726. return 0; \
  1727. if (IS_ERR(desc)) { \
  1728. pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
  1729. return PTR_ERR(desc); \
  1730. } \
  1731. if (!desc->gdev) { \
  1732. pr_warn("%s: invalid GPIO (no device)\n", __func__); \
  1733. return -EINVAL; \
  1734. } \
  1735. if ( !desc->gdev->chip ) { \
  1736. dev_warn(&desc->gdev->dev, \
  1737. "%s: backing chip is gone\n", __func__); \
  1738. return 0; \
  1739. } } while (0)
  1740. #define VALIDATE_DESC_VOID(desc) do { \
  1741. if (!desc) \
  1742. return; \
  1743. if (IS_ERR(desc)) { \
  1744. pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
  1745. return; \
  1746. } \
  1747. if (!desc->gdev) { \
  1748. pr_warn("%s: invalid GPIO (no device)\n", __func__); \
  1749. return; \
  1750. } \
  1751. if (!desc->gdev->chip) { \
  1752. dev_warn(&desc->gdev->dev, \
  1753. "%s: backing chip is gone\n", __func__); \
  1754. return; \
  1755. } } while (0)
  1756. int gpiod_request(struct gpio_desc *desc, const char *label)
  1757. {
  1758. int status = -EPROBE_DEFER;
  1759. struct gpio_device *gdev;
  1760. VALIDATE_DESC(desc);
  1761. gdev = desc->gdev;
  1762. if (try_module_get(gdev->owner)) {
  1763. status = __gpiod_request(desc, label);
  1764. if (status < 0)
  1765. module_put(gdev->owner);
  1766. else
  1767. get_device(&gdev->dev);
  1768. }
  1769. if (status)
  1770. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  1771. return status;
  1772. }
  1773. static bool __gpiod_free(struct gpio_desc *desc)
  1774. {
  1775. bool ret = false;
  1776. unsigned long flags;
  1777. struct gpio_chip *chip;
  1778. might_sleep();
  1779. gpiod_unexport(desc);
  1780. spin_lock_irqsave(&gpio_lock, flags);
  1781. chip = desc->gdev->chip;
  1782. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1783. if (chip->free) {
  1784. spin_unlock_irqrestore(&gpio_lock, flags);
  1785. might_sleep_if(chip->can_sleep);
  1786. chip->free(chip, gpio_chip_hwgpio(desc));
  1787. spin_lock_irqsave(&gpio_lock, flags);
  1788. }
  1789. desc_set_label(desc, NULL);
  1790. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1791. clear_bit(FLAG_REQUESTED, &desc->flags);
  1792. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1793. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1794. clear_bit(FLAG_IS_HOGGED, &desc->flags);
  1795. ret = true;
  1796. }
  1797. spin_unlock_irqrestore(&gpio_lock, flags);
  1798. return ret;
  1799. }
  1800. void gpiod_free(struct gpio_desc *desc)
  1801. {
  1802. if (desc && desc->gdev && __gpiod_free(desc)) {
  1803. module_put(desc->gdev->owner);
  1804. put_device(&desc->gdev->dev);
  1805. } else {
  1806. WARN_ON(extra_checks);
  1807. }
  1808. }
  1809. /**
  1810. * gpiochip_is_requested - return string iff signal was requested
  1811. * @chip: controller managing the signal
  1812. * @offset: of signal within controller's 0..(ngpio - 1) range
  1813. *
  1814. * Returns NULL if the GPIO is not currently requested, else a string.
  1815. * The string returned is the label passed to gpio_request(); if none has been
  1816. * passed it is a meaningless, non-NULL constant.
  1817. *
  1818. * This function is for use by GPIO controller drivers. The label can
  1819. * help with diagnostics, and knowing that the signal is used as a GPIO
  1820. * can help avoid accidentally multiplexing it to another controller.
  1821. */
  1822. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1823. {
  1824. struct gpio_desc *desc;
  1825. if (offset >= chip->ngpio)
  1826. return NULL;
  1827. desc = &chip->gpiodev->descs[offset];
  1828. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  1829. return NULL;
  1830. return desc->label;
  1831. }
  1832. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1833. /**
  1834. * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
  1835. * @desc: GPIO descriptor to request
  1836. * @label: label for the GPIO
  1837. *
  1838. * Function allows GPIO chip drivers to request and use their own GPIO
  1839. * descriptors via gpiolib API. Difference to gpiod_request() is that this
  1840. * function will not increase reference count of the GPIO chip module. This
  1841. * allows the GPIO chip module to be unloaded as needed (we assume that the
  1842. * GPIO chip driver handles freeing the GPIOs it has requested).
  1843. */
  1844. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  1845. const char *label)
  1846. {
  1847. struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
  1848. int err;
  1849. if (IS_ERR(desc)) {
  1850. chip_err(chip, "failed to get GPIO descriptor\n");
  1851. return desc;
  1852. }
  1853. err = __gpiod_request(desc, label);
  1854. if (err < 0)
  1855. return ERR_PTR(err);
  1856. return desc;
  1857. }
  1858. EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
  1859. /**
  1860. * gpiochip_free_own_desc - Free GPIO requested by the chip driver
  1861. * @desc: GPIO descriptor to free
  1862. *
  1863. * Function frees the given GPIO requested previously with
  1864. * gpiochip_request_own_desc().
  1865. */
  1866. void gpiochip_free_own_desc(struct gpio_desc *desc)
  1867. {
  1868. if (desc)
  1869. __gpiod_free(desc);
  1870. }
  1871. EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
  1872. /*
  1873. * Drivers MUST set GPIO direction before making get/set calls. In
  1874. * some cases this is done in early boot, before IRQs are enabled.
  1875. *
  1876. * As a rule these aren't called more than once (except for drivers
  1877. * using the open-drain emulation idiom) so these are natural places
  1878. * to accumulate extra debugging checks. Note that we can't (yet)
  1879. * rely on gpio_request() having been called beforehand.
  1880. */
  1881. /**
  1882. * gpiod_direction_input - set the GPIO direction to input
  1883. * @desc: GPIO to set to input
  1884. *
  1885. * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
  1886. * be called safely on it.
  1887. *
  1888. * Return 0 in case of success, else an error code.
  1889. */
  1890. int gpiod_direction_input(struct gpio_desc *desc)
  1891. {
  1892. struct gpio_chip *chip;
  1893. int status = -EINVAL;
  1894. VALIDATE_DESC(desc);
  1895. chip = desc->gdev->chip;
  1896. if (!chip->get || !chip->direction_input) {
  1897. gpiod_warn(desc,
  1898. "%s: missing get() or direction_input() operations\n",
  1899. __func__);
  1900. return -EIO;
  1901. }
  1902. status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
  1903. if (status == 0)
  1904. clear_bit(FLAG_IS_OUT, &desc->flags);
  1905. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  1906. return status;
  1907. }
  1908. EXPORT_SYMBOL_GPL(gpiod_direction_input);
  1909. static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  1910. {
  1911. struct gpio_chip *gc = desc->gdev->chip;
  1912. int ret;
  1913. /* GPIOs used for IRQs shall not be set as output */
  1914. if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
  1915. gpiod_err(desc,
  1916. "%s: tried to set a GPIO tied to an IRQ as output\n",
  1917. __func__);
  1918. return -EIO;
  1919. }
  1920. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
  1921. /* First see if we can enable open drain in hardware */
  1922. if (gc->set_single_ended) {
  1923. ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
  1924. LINE_MODE_OPEN_DRAIN);
  1925. if (!ret)
  1926. goto set_output_value;
  1927. }
  1928. /* Emulate open drain by not actively driving the line high */
  1929. if (value)
  1930. return gpiod_direction_input(desc);
  1931. }
  1932. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
  1933. if (gc->set_single_ended) {
  1934. ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
  1935. LINE_MODE_OPEN_SOURCE);
  1936. if (!ret)
  1937. goto set_output_value;
  1938. }
  1939. /* Emulate open source by not actively driving the line low */
  1940. if (!value)
  1941. return gpiod_direction_input(desc);
  1942. } else {
  1943. /* Make sure to disable open drain/source hardware, if any */
  1944. if (gc->set_single_ended)
  1945. gc->set_single_ended(gc,
  1946. gpio_chip_hwgpio(desc),
  1947. LINE_MODE_PUSH_PULL);
  1948. }
  1949. set_output_value:
  1950. if (!gc->set || !gc->direction_output) {
  1951. gpiod_warn(desc,
  1952. "%s: missing set() or direction_output() operations\n",
  1953. __func__);
  1954. return -EIO;
  1955. }
  1956. ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
  1957. if (!ret)
  1958. set_bit(FLAG_IS_OUT, &desc->flags);
  1959. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1960. trace_gpio_direction(desc_to_gpio(desc), 0, ret);
  1961. return ret;
  1962. }
  1963. /**
  1964. * gpiod_direction_output_raw - set the GPIO direction to output
  1965. * @desc: GPIO to set to output
  1966. * @value: initial output value of the GPIO
  1967. *
  1968. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  1969. * be called safely on it. The initial value of the output must be specified
  1970. * as raw value on the physical line without regard for the ACTIVE_LOW status.
  1971. *
  1972. * Return 0 in case of success, else an error code.
  1973. */
  1974. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  1975. {
  1976. VALIDATE_DESC(desc);
  1977. return _gpiod_direction_output_raw(desc, value);
  1978. }
  1979. EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
  1980. /**
  1981. * gpiod_direction_output - set the GPIO direction to output
  1982. * @desc: GPIO to set to output
  1983. * @value: initial output value of the GPIO
  1984. *
  1985. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  1986. * be called safely on it. The initial value of the output must be specified
  1987. * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1988. * account.
  1989. *
  1990. * Return 0 in case of success, else an error code.
  1991. */
  1992. int gpiod_direction_output(struct gpio_desc *desc, int value)
  1993. {
  1994. VALIDATE_DESC(desc);
  1995. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1996. value = !value;
  1997. return _gpiod_direction_output_raw(desc, value);
  1998. }
  1999. EXPORT_SYMBOL_GPL(gpiod_direction_output);
  2000. /**
  2001. * gpiod_set_debounce - sets @debounce time for a @gpio
  2002. * @gpio: the gpio to set debounce time
  2003. * @debounce: debounce time is microseconds
  2004. *
  2005. * returns -ENOTSUPP if the controller does not support setting
  2006. * debounce.
  2007. */
  2008. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  2009. {
  2010. struct gpio_chip *chip;
  2011. VALIDATE_DESC(desc);
  2012. chip = desc->gdev->chip;
  2013. if (!chip->set || !chip->set_debounce) {
  2014. gpiod_dbg(desc,
  2015. "%s: missing set() or set_debounce() operations\n",
  2016. __func__);
  2017. return -ENOTSUPP;
  2018. }
  2019. return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
  2020. }
  2021. EXPORT_SYMBOL_GPL(gpiod_set_debounce);
  2022. /**
  2023. * gpiod_is_active_low - test whether a GPIO is active-low or not
  2024. * @desc: the gpio descriptor to test
  2025. *
  2026. * Returns 1 if the GPIO is active-low, 0 otherwise.
  2027. */
  2028. int gpiod_is_active_low(const struct gpio_desc *desc)
  2029. {
  2030. VALIDATE_DESC(desc);
  2031. return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2032. }
  2033. EXPORT_SYMBOL_GPL(gpiod_is_active_low);
  2034. /* I/O calls are only valid after configuration completed; the relevant
  2035. * "is this a valid GPIO" error checks should already have been done.
  2036. *
  2037. * "Get" operations are often inlinable as reading a pin value register,
  2038. * and masking the relevant bit in that register.
  2039. *
  2040. * When "set" operations are inlinable, they involve writing that mask to
  2041. * one register to set a low value, or a different register to set it high.
  2042. * Otherwise locking is needed, so there may be little value to inlining.
  2043. *
  2044. *------------------------------------------------------------------------
  2045. *
  2046. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  2047. * have requested the GPIO. That can include implicit requesting by
  2048. * a direction setting call. Marking a gpio as requested locks its chip
  2049. * in memory, guaranteeing that these table lookups need no more locking
  2050. * and that gpiochip_remove() will fail.
  2051. *
  2052. * REVISIT when debugging, consider adding some instrumentation to ensure
  2053. * that the GPIO was actually requested.
  2054. */
  2055. static int _gpiod_get_raw_value(const struct gpio_desc *desc)
  2056. {
  2057. struct gpio_chip *chip;
  2058. int offset;
  2059. int value;
  2060. chip = desc->gdev->chip;
  2061. offset = gpio_chip_hwgpio(desc);
  2062. value = chip->get ? chip->get(chip, offset) : -EIO;
  2063. value = value < 0 ? value : !!value;
  2064. trace_gpio_value(desc_to_gpio(desc), 1, value);
  2065. return value;
  2066. }
  2067. /**
  2068. * gpiod_get_raw_value() - return a gpio's raw value
  2069. * @desc: gpio whose value will be returned
  2070. *
  2071. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  2072. * its ACTIVE_LOW status, or negative errno on failure.
  2073. *
  2074. * This function should be called from contexts where we cannot sleep, and will
  2075. * complain if the GPIO chip functions potentially sleep.
  2076. */
  2077. int gpiod_get_raw_value(const struct gpio_desc *desc)
  2078. {
  2079. VALIDATE_DESC(desc);
  2080. /* Should be using gpio_get_value_cansleep() */
  2081. WARN_ON(desc->gdev->chip->can_sleep);
  2082. return _gpiod_get_raw_value(desc);
  2083. }
  2084. EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
  2085. /**
  2086. * gpiod_get_value() - return a gpio's value
  2087. * @desc: gpio whose value will be returned
  2088. *
  2089. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  2090. * account, or negative errno on failure.
  2091. *
  2092. * This function should be called from contexts where we cannot sleep, and will
  2093. * complain if the GPIO chip functions potentially sleep.
  2094. */
  2095. int gpiod_get_value(const struct gpio_desc *desc)
  2096. {
  2097. int value;
  2098. VALIDATE_DESC(desc);
  2099. /* Should be using gpio_get_value_cansleep() */
  2100. WARN_ON(desc->gdev->chip->can_sleep);
  2101. value = _gpiod_get_raw_value(desc);
  2102. if (value < 0)
  2103. return value;
  2104. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2105. value = !value;
  2106. return value;
  2107. }
  2108. EXPORT_SYMBOL_GPL(gpiod_get_value);
  2109. /*
  2110. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  2111. * @desc: gpio descriptor whose state need to be set.
  2112. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  2113. */
  2114. static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
  2115. {
  2116. int err = 0;
  2117. struct gpio_chip *chip = desc->gdev->chip;
  2118. int offset = gpio_chip_hwgpio(desc);
  2119. if (value) {
  2120. err = chip->direction_input(chip, offset);
  2121. if (!err)
  2122. clear_bit(FLAG_IS_OUT, &desc->flags);
  2123. } else {
  2124. err = chip->direction_output(chip, offset, 0);
  2125. if (!err)
  2126. set_bit(FLAG_IS_OUT, &desc->flags);
  2127. }
  2128. trace_gpio_direction(desc_to_gpio(desc), value, err);
  2129. if (err < 0)
  2130. gpiod_err(desc,
  2131. "%s: Error in set_value for open drain err %d\n",
  2132. __func__, err);
  2133. }
  2134. /*
  2135. * _gpio_set_open_source_value() - Set the open source gpio's value.
  2136. * @desc: gpio descriptor whose state need to be set.
  2137. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  2138. */
  2139. static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
  2140. {
  2141. int err = 0;
  2142. struct gpio_chip *chip = desc->gdev->chip;
  2143. int offset = gpio_chip_hwgpio(desc);
  2144. if (value) {
  2145. err = chip->direction_output(chip, offset, 1);
  2146. if (!err)
  2147. set_bit(FLAG_IS_OUT, &desc->flags);
  2148. } else {
  2149. err = chip->direction_input(chip, offset);
  2150. if (!err)
  2151. clear_bit(FLAG_IS_OUT, &desc->flags);
  2152. }
  2153. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  2154. if (err < 0)
  2155. gpiod_err(desc,
  2156. "%s: Error in set_value for open source err %d\n",
  2157. __func__, err);
  2158. }
  2159. static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
  2160. {
  2161. struct gpio_chip *chip;
  2162. chip = desc->gdev->chip;
  2163. trace_gpio_value(desc_to_gpio(desc), 0, value);
  2164. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  2165. _gpio_set_open_drain_value(desc, value);
  2166. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  2167. _gpio_set_open_source_value(desc, value);
  2168. else
  2169. chip->set(chip, gpio_chip_hwgpio(desc), value);
  2170. }
  2171. /*
  2172. * set multiple outputs on the same chip;
  2173. * use the chip's set_multiple function if available;
  2174. * otherwise set the outputs sequentially;
  2175. * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
  2176. * defines which outputs are to be changed
  2177. * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
  2178. * defines the values the outputs specified by mask are to be set to
  2179. */
  2180. static void gpio_chip_set_multiple(struct gpio_chip *chip,
  2181. unsigned long *mask, unsigned long *bits)
  2182. {
  2183. if (chip->set_multiple) {
  2184. chip->set_multiple(chip, mask, bits);
  2185. } else {
  2186. int i;
  2187. for (i = 0; i < chip->ngpio; i++) {
  2188. if (mask[BIT_WORD(i)] == 0) {
  2189. /* no more set bits in this mask word;
  2190. * skip ahead to the next word */
  2191. i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
  2192. continue;
  2193. }
  2194. /* set outputs if the corresponding mask bit is set */
  2195. if (__test_and_clear_bit(i, mask))
  2196. chip->set(chip, i, test_bit(i, bits));
  2197. }
  2198. }
  2199. }
  2200. void gpiod_set_array_value_complex(bool raw, bool can_sleep,
  2201. unsigned int array_size,
  2202. struct gpio_desc **desc_array,
  2203. int *value_array)
  2204. {
  2205. int i = 0;
  2206. while (i < array_size) {
  2207. struct gpio_chip *chip = desc_array[i]->gdev->chip;
  2208. unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
  2209. unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
  2210. int count = 0;
  2211. if (!can_sleep)
  2212. WARN_ON(chip->can_sleep);
  2213. memset(mask, 0, sizeof(mask));
  2214. do {
  2215. struct gpio_desc *desc = desc_array[i];
  2216. int hwgpio = gpio_chip_hwgpio(desc);
  2217. int value = value_array[i];
  2218. if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2219. value = !value;
  2220. trace_gpio_value(desc_to_gpio(desc), 0, value);
  2221. /*
  2222. * collect all normal outputs belonging to the same chip
  2223. * open drain and open source outputs are set individually
  2224. */
  2225. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
  2226. _gpio_set_open_drain_value(desc, value);
  2227. } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
  2228. _gpio_set_open_source_value(desc, value);
  2229. } else {
  2230. __set_bit(hwgpio, mask);
  2231. if (value)
  2232. __set_bit(hwgpio, bits);
  2233. else
  2234. __clear_bit(hwgpio, bits);
  2235. count++;
  2236. }
  2237. i++;
  2238. } while ((i < array_size) &&
  2239. (desc_array[i]->gdev->chip == chip));
  2240. /* push collected bits to outputs */
  2241. if (count != 0)
  2242. gpio_chip_set_multiple(chip, mask, bits);
  2243. }
  2244. }
  2245. /**
  2246. * gpiod_set_raw_value() - assign a gpio's raw value
  2247. * @desc: gpio whose value will be assigned
  2248. * @value: value to assign
  2249. *
  2250. * Set the raw value of the GPIO, i.e. the value of its physical line without
  2251. * regard for its ACTIVE_LOW status.
  2252. *
  2253. * This function should be called from contexts where we cannot sleep, and will
  2254. * complain if the GPIO chip functions potentially sleep.
  2255. */
  2256. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  2257. {
  2258. VALIDATE_DESC_VOID(desc);
  2259. /* Should be using gpiod_set_value_cansleep() */
  2260. WARN_ON(desc->gdev->chip->can_sleep);
  2261. _gpiod_set_raw_value(desc, value);
  2262. }
  2263. EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
  2264. /**
  2265. * gpiod_set_value() - assign a gpio's value
  2266. * @desc: gpio whose value will be assigned
  2267. * @value: value to assign
  2268. *
  2269. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  2270. * account
  2271. *
  2272. * This function should be called from contexts where we cannot sleep, and will
  2273. * complain if the GPIO chip functions potentially sleep.
  2274. */
  2275. void gpiod_set_value(struct gpio_desc *desc, int value)
  2276. {
  2277. VALIDATE_DESC_VOID(desc);
  2278. /* Should be using gpiod_set_value_cansleep() */
  2279. WARN_ON(desc->gdev->chip->can_sleep);
  2280. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2281. value = !value;
  2282. _gpiod_set_raw_value(desc, value);
  2283. }
  2284. EXPORT_SYMBOL_GPL(gpiod_set_value);
  2285. /**
  2286. * gpiod_set_raw_array_value() - assign values to an array of GPIOs
  2287. * @array_size: number of elements in the descriptor / value arrays
  2288. * @desc_array: array of GPIO descriptors whose values will be assigned
  2289. * @value_array: array of values to assign
  2290. *
  2291. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  2292. * without regard for their ACTIVE_LOW status.
  2293. *
  2294. * This function should be called from contexts where we cannot sleep, and will
  2295. * complain if the GPIO chip functions potentially sleep.
  2296. */
  2297. void gpiod_set_raw_array_value(unsigned int array_size,
  2298. struct gpio_desc **desc_array, int *value_array)
  2299. {
  2300. if (!desc_array)
  2301. return;
  2302. gpiod_set_array_value_complex(true, false, array_size, desc_array,
  2303. value_array);
  2304. }
  2305. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
  2306. /**
  2307. * gpiod_set_array_value() - assign values to an array of GPIOs
  2308. * @array_size: number of elements in the descriptor / value arrays
  2309. * @desc_array: array of GPIO descriptors whose values will be assigned
  2310. * @value_array: array of values to assign
  2311. *
  2312. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2313. * into account.
  2314. *
  2315. * This function should be called from contexts where we cannot sleep, and will
  2316. * complain if the GPIO chip functions potentially sleep.
  2317. */
  2318. void gpiod_set_array_value(unsigned int array_size,
  2319. struct gpio_desc **desc_array, int *value_array)
  2320. {
  2321. if (!desc_array)
  2322. return;
  2323. gpiod_set_array_value_complex(false, false, array_size, desc_array,
  2324. value_array);
  2325. }
  2326. EXPORT_SYMBOL_GPL(gpiod_set_array_value);
  2327. /**
  2328. * gpiod_cansleep() - report whether gpio value access may sleep
  2329. * @desc: gpio to check
  2330. *
  2331. */
  2332. int gpiod_cansleep(const struct gpio_desc *desc)
  2333. {
  2334. VALIDATE_DESC(desc);
  2335. return desc->gdev->chip->can_sleep;
  2336. }
  2337. EXPORT_SYMBOL_GPL(gpiod_cansleep);
  2338. /**
  2339. * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  2340. * @desc: gpio whose IRQ will be returned (already requested)
  2341. *
  2342. * Return the IRQ corresponding to the passed GPIO, or an error code in case of
  2343. * error.
  2344. */
  2345. int gpiod_to_irq(const struct gpio_desc *desc)
  2346. {
  2347. struct gpio_chip *chip;
  2348. int offset;
  2349. /*
  2350. * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
  2351. * requires this function to not return zero on an invalid descriptor
  2352. * but rather a negative error number.
  2353. */
  2354. if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
  2355. return -EINVAL;
  2356. chip = desc->gdev->chip;
  2357. offset = gpio_chip_hwgpio(desc);
  2358. if (chip->to_irq) {
  2359. int retirq = chip->to_irq(chip, offset);
  2360. /* Zero means NO_IRQ */
  2361. if (!retirq)
  2362. return -ENXIO;
  2363. return retirq;
  2364. }
  2365. return -ENXIO;
  2366. }
  2367. EXPORT_SYMBOL_GPL(gpiod_to_irq);
  2368. /**
  2369. * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
  2370. * @chip: the chip the GPIO to lock belongs to
  2371. * @offset: the offset of the GPIO to lock as IRQ
  2372. *
  2373. * This is used directly by GPIO drivers that want to lock down
  2374. * a certain GPIO line to be used for IRQs.
  2375. */
  2376. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
  2377. {
  2378. struct gpio_desc *desc;
  2379. desc = gpiochip_get_desc(chip, offset);
  2380. if (IS_ERR(desc))
  2381. return PTR_ERR(desc);
  2382. /*
  2383. * If it's fast: flush the direction setting if something changed
  2384. * behind our back
  2385. */
  2386. if (!chip->can_sleep && chip->get_direction) {
  2387. int dir = chip->get_direction(chip, offset);
  2388. if (dir)
  2389. clear_bit(FLAG_IS_OUT, &desc->flags);
  2390. else
  2391. set_bit(FLAG_IS_OUT, &desc->flags);
  2392. }
  2393. if (test_bit(FLAG_IS_OUT, &desc->flags)) {
  2394. chip_err(chip,
  2395. "%s: tried to flag a GPIO set as output for IRQ\n",
  2396. __func__);
  2397. return -EIO;
  2398. }
  2399. set_bit(FLAG_USED_AS_IRQ, &desc->flags);
  2400. return 0;
  2401. }
  2402. EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
  2403. /**
  2404. * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
  2405. * @chip: the chip the GPIO to lock belongs to
  2406. * @offset: the offset of the GPIO to lock as IRQ
  2407. *
  2408. * This is used directly by GPIO drivers that want to indicate
  2409. * that a certain GPIO is no longer used exclusively for IRQ.
  2410. */
  2411. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
  2412. {
  2413. if (offset >= chip->ngpio)
  2414. return;
  2415. clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
  2416. }
  2417. EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
  2418. bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
  2419. {
  2420. if (offset >= chip->ngpio)
  2421. return false;
  2422. return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
  2423. }
  2424. EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
  2425. bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
  2426. {
  2427. if (offset >= chip->ngpio)
  2428. return false;
  2429. return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
  2430. }
  2431. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
  2432. bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
  2433. {
  2434. if (offset >= chip->ngpio)
  2435. return false;
  2436. return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
  2437. }
  2438. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
  2439. /**
  2440. * gpiod_get_raw_value_cansleep() - return a gpio's raw value
  2441. * @desc: gpio whose value will be returned
  2442. *
  2443. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  2444. * its ACTIVE_LOW status, or negative errno on failure.
  2445. *
  2446. * This function is to be called from contexts that can sleep.
  2447. */
  2448. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  2449. {
  2450. might_sleep_if(extra_checks);
  2451. VALIDATE_DESC(desc);
  2452. return _gpiod_get_raw_value(desc);
  2453. }
  2454. EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
  2455. /**
  2456. * gpiod_get_value_cansleep() - return a gpio's value
  2457. * @desc: gpio whose value will be returned
  2458. *
  2459. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  2460. * account, or negative errno on failure.
  2461. *
  2462. * This function is to be called from contexts that can sleep.
  2463. */
  2464. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  2465. {
  2466. int value;
  2467. might_sleep_if(extra_checks);
  2468. VALIDATE_DESC(desc);
  2469. value = _gpiod_get_raw_value(desc);
  2470. if (value < 0)
  2471. return value;
  2472. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2473. value = !value;
  2474. return value;
  2475. }
  2476. EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
  2477. /**
  2478. * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
  2479. * @desc: gpio whose value will be assigned
  2480. * @value: value to assign
  2481. *
  2482. * Set the raw value of the GPIO, i.e. the value of its physical line without
  2483. * regard for its ACTIVE_LOW status.
  2484. *
  2485. * This function is to be called from contexts that can sleep.
  2486. */
  2487. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  2488. {
  2489. might_sleep_if(extra_checks);
  2490. VALIDATE_DESC_VOID(desc);
  2491. _gpiod_set_raw_value(desc, value);
  2492. }
  2493. EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
  2494. /**
  2495. * gpiod_set_value_cansleep() - assign a gpio's value
  2496. * @desc: gpio whose value will be assigned
  2497. * @value: value to assign
  2498. *
  2499. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  2500. * account
  2501. *
  2502. * This function is to be called from contexts that can sleep.
  2503. */
  2504. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  2505. {
  2506. might_sleep_if(extra_checks);
  2507. VALIDATE_DESC_VOID(desc);
  2508. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2509. value = !value;
  2510. _gpiod_set_raw_value(desc, value);
  2511. }
  2512. EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
  2513. /**
  2514. * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
  2515. * @array_size: number of elements in the descriptor / value arrays
  2516. * @desc_array: array of GPIO descriptors whose values will be assigned
  2517. * @value_array: array of values to assign
  2518. *
  2519. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  2520. * without regard for their ACTIVE_LOW status.
  2521. *
  2522. * This function is to be called from contexts that can sleep.
  2523. */
  2524. void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  2525. struct gpio_desc **desc_array,
  2526. int *value_array)
  2527. {
  2528. might_sleep_if(extra_checks);
  2529. if (!desc_array)
  2530. return;
  2531. gpiod_set_array_value_complex(true, true, array_size, desc_array,
  2532. value_array);
  2533. }
  2534. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
  2535. /**
  2536. * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
  2537. * @array_size: number of elements in the descriptor / value arrays
  2538. * @desc_array: array of GPIO descriptors whose values will be assigned
  2539. * @value_array: array of values to assign
  2540. *
  2541. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2542. * into account.
  2543. *
  2544. * This function is to be called from contexts that can sleep.
  2545. */
  2546. void gpiod_set_array_value_cansleep(unsigned int array_size,
  2547. struct gpio_desc **desc_array,
  2548. int *value_array)
  2549. {
  2550. might_sleep_if(extra_checks);
  2551. if (!desc_array)
  2552. return;
  2553. gpiod_set_array_value_complex(false, true, array_size, desc_array,
  2554. value_array);
  2555. }
  2556. EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
  2557. /**
  2558. * gpiod_add_lookup_table() - register GPIO device consumers
  2559. * @table: table of consumers to register
  2560. */
  2561. void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
  2562. {
  2563. mutex_lock(&gpio_lookup_lock);
  2564. list_add_tail(&table->list, &gpio_lookup_list);
  2565. mutex_unlock(&gpio_lookup_lock);
  2566. }
  2567. /**
  2568. * gpiod_remove_lookup_table() - unregister GPIO device consumers
  2569. * @table: table of consumers to unregister
  2570. */
  2571. void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
  2572. {
  2573. mutex_lock(&gpio_lookup_lock);
  2574. list_del(&table->list);
  2575. mutex_unlock(&gpio_lookup_lock);
  2576. }
  2577. static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
  2578. {
  2579. const char *dev_id = dev ? dev_name(dev) : NULL;
  2580. struct gpiod_lookup_table *table;
  2581. mutex_lock(&gpio_lookup_lock);
  2582. list_for_each_entry(table, &gpio_lookup_list, list) {
  2583. if (table->dev_id && dev_id) {
  2584. /*
  2585. * Valid strings on both ends, must be identical to have
  2586. * a match
  2587. */
  2588. if (!strcmp(table->dev_id, dev_id))
  2589. goto found;
  2590. } else {
  2591. /*
  2592. * One of the pointers is NULL, so both must be to have
  2593. * a match
  2594. */
  2595. if (dev_id == table->dev_id)
  2596. goto found;
  2597. }
  2598. }
  2599. table = NULL;
  2600. found:
  2601. mutex_unlock(&gpio_lookup_lock);
  2602. return table;
  2603. }
  2604. static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
  2605. unsigned int idx,
  2606. enum gpio_lookup_flags *flags)
  2607. {
  2608. struct gpio_desc *desc = ERR_PTR(-ENOENT);
  2609. struct gpiod_lookup_table *table;
  2610. struct gpiod_lookup *p;
  2611. table = gpiod_find_lookup_table(dev);
  2612. if (!table)
  2613. return desc;
  2614. for (p = &table->table[0]; p->chip_label; p++) {
  2615. struct gpio_chip *chip;
  2616. /* idx must always match exactly */
  2617. if (p->idx != idx)
  2618. continue;
  2619. /* If the lookup entry has a con_id, require exact match */
  2620. if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
  2621. continue;
  2622. chip = find_chip_by_name(p->chip_label);
  2623. if (!chip) {
  2624. dev_err(dev, "cannot find GPIO chip %s\n",
  2625. p->chip_label);
  2626. return ERR_PTR(-ENODEV);
  2627. }
  2628. if (chip->ngpio <= p->chip_hwnum) {
  2629. dev_err(dev,
  2630. "requested GPIO %d is out of range [0..%d] for chip %s\n",
  2631. idx, chip->ngpio, chip->label);
  2632. return ERR_PTR(-EINVAL);
  2633. }
  2634. desc = gpiochip_get_desc(chip, p->chip_hwnum);
  2635. *flags = p->flags;
  2636. return desc;
  2637. }
  2638. return desc;
  2639. }
  2640. static int dt_gpio_count(struct device *dev, const char *con_id)
  2641. {
  2642. int ret;
  2643. char propname[32];
  2644. unsigned int i;
  2645. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  2646. if (con_id)
  2647. snprintf(propname, sizeof(propname), "%s-%s",
  2648. con_id, gpio_suffixes[i]);
  2649. else
  2650. snprintf(propname, sizeof(propname), "%s",
  2651. gpio_suffixes[i]);
  2652. ret = of_gpio_named_count(dev->of_node, propname);
  2653. if (ret >= 0)
  2654. break;
  2655. }
  2656. return ret;
  2657. }
  2658. static int platform_gpio_count(struct device *dev, const char *con_id)
  2659. {
  2660. struct gpiod_lookup_table *table;
  2661. struct gpiod_lookup *p;
  2662. unsigned int count = 0;
  2663. table = gpiod_find_lookup_table(dev);
  2664. if (!table)
  2665. return -ENOENT;
  2666. for (p = &table->table[0]; p->chip_label; p++) {
  2667. if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
  2668. (!con_id && !p->con_id))
  2669. count++;
  2670. }
  2671. if (!count)
  2672. return -ENOENT;
  2673. return count;
  2674. }
  2675. /**
  2676. * gpiod_count - return the number of GPIOs associated with a device / function
  2677. * or -ENOENT if no GPIO has been assigned to the requested function
  2678. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2679. * @con_id: function within the GPIO consumer
  2680. */
  2681. int gpiod_count(struct device *dev, const char *con_id)
  2682. {
  2683. int count = -ENOENT;
  2684. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  2685. count = dt_gpio_count(dev, con_id);
  2686. else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
  2687. count = acpi_gpio_count(dev, con_id);
  2688. if (count < 0)
  2689. count = platform_gpio_count(dev, con_id);
  2690. return count;
  2691. }
  2692. EXPORT_SYMBOL_GPL(gpiod_count);
  2693. /**
  2694. * gpiod_get - obtain a GPIO for a given GPIO function
  2695. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2696. * @con_id: function within the GPIO consumer
  2697. * @flags: optional GPIO initialization flags
  2698. *
  2699. * Return the GPIO descriptor corresponding to the function con_id of device
  2700. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  2701. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  2702. */
  2703. struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
  2704. enum gpiod_flags flags)
  2705. {
  2706. return gpiod_get_index(dev, con_id, 0, flags);
  2707. }
  2708. EXPORT_SYMBOL_GPL(gpiod_get);
  2709. /**
  2710. * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
  2711. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2712. * @con_id: function within the GPIO consumer
  2713. * @flags: optional GPIO initialization flags
  2714. *
  2715. * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
  2716. * the requested function it will return NULL. This is convenient for drivers
  2717. * that need to handle optional GPIOs.
  2718. */
  2719. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  2720. const char *con_id,
  2721. enum gpiod_flags flags)
  2722. {
  2723. return gpiod_get_index_optional(dev, con_id, 0, flags);
  2724. }
  2725. EXPORT_SYMBOL_GPL(gpiod_get_optional);
  2726. /**
  2727. * gpiod_configure_flags - helper function to configure a given GPIO
  2728. * @desc: gpio whose value will be assigned
  2729. * @con_id: function within the GPIO consumer
  2730. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  2731. * of_get_gpio_hog()
  2732. * @dflags: gpiod_flags - optional GPIO initialization flags
  2733. *
  2734. * Return 0 on success, -ENOENT if no GPIO has been assigned to the
  2735. * requested function and/or index, or another IS_ERR() code if an error
  2736. * occurred while trying to acquire the GPIO.
  2737. */
  2738. static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  2739. unsigned long lflags, enum gpiod_flags dflags)
  2740. {
  2741. int status;
  2742. if (lflags & GPIO_ACTIVE_LOW)
  2743. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2744. if (lflags & GPIO_OPEN_DRAIN)
  2745. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  2746. if (lflags & GPIO_OPEN_SOURCE)
  2747. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  2748. /* No particular flag request, return here... */
  2749. if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
  2750. pr_debug("no flags found for %s\n", con_id);
  2751. return 0;
  2752. }
  2753. /* Process flags */
  2754. if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
  2755. status = gpiod_direction_output(desc,
  2756. dflags & GPIOD_FLAGS_BIT_DIR_VAL);
  2757. else
  2758. status = gpiod_direction_input(desc);
  2759. return status;
  2760. }
  2761. /**
  2762. * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
  2763. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2764. * @con_id: function within the GPIO consumer
  2765. * @idx: index of the GPIO to obtain in the consumer
  2766. * @flags: optional GPIO initialization flags
  2767. *
  2768. * This variant of gpiod_get() allows to access GPIOs other than the first
  2769. * defined one for functions that define several GPIOs.
  2770. *
  2771. * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
  2772. * requested function and/or index, or another IS_ERR() code if an error
  2773. * occurred while trying to acquire the GPIO.
  2774. */
  2775. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  2776. const char *con_id,
  2777. unsigned int idx,
  2778. enum gpiod_flags flags)
  2779. {
  2780. struct gpio_desc *desc = NULL;
  2781. int status;
  2782. enum gpio_lookup_flags lookupflags = 0;
  2783. dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
  2784. if (dev) {
  2785. /* Using device tree? */
  2786. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  2787. dev_dbg(dev, "using device tree for GPIO lookup\n");
  2788. desc = of_find_gpio(dev, con_id, idx, &lookupflags);
  2789. } else if (ACPI_COMPANION(dev)) {
  2790. dev_dbg(dev, "using ACPI for GPIO lookup\n");
  2791. desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
  2792. }
  2793. }
  2794. /*
  2795. * Either we are not using DT or ACPI, or their lookup did not return
  2796. * a result. In that case, use platform lookup as a fallback.
  2797. */
  2798. if (!desc || desc == ERR_PTR(-ENOENT)) {
  2799. dev_dbg(dev, "using lookup tables for GPIO lookup\n");
  2800. desc = gpiod_find(dev, con_id, idx, &lookupflags);
  2801. }
  2802. if (IS_ERR(desc)) {
  2803. dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
  2804. return desc;
  2805. }
  2806. status = gpiod_request(desc, con_id);
  2807. if (status < 0)
  2808. return ERR_PTR(status);
  2809. status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
  2810. if (status < 0) {
  2811. dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
  2812. gpiod_put(desc);
  2813. return ERR_PTR(status);
  2814. }
  2815. return desc;
  2816. }
  2817. EXPORT_SYMBOL_GPL(gpiod_get_index);
  2818. /**
  2819. * fwnode_get_named_gpiod - obtain a GPIO from firmware node
  2820. * @fwnode: handle of the firmware node
  2821. * @propname: name of the firmware property representing the GPIO
  2822. *
  2823. * This function can be used for drivers that get their configuration
  2824. * from firmware.
  2825. *
  2826. * Function properly finds the corresponding GPIO using whatever is the
  2827. * underlying firmware interface and then makes sure that the GPIO
  2828. * descriptor is requested before it is returned to the caller.
  2829. *
  2830. * In case of error an ERR_PTR() is returned.
  2831. */
  2832. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  2833. const char *propname)
  2834. {
  2835. struct gpio_desc *desc = ERR_PTR(-ENODEV);
  2836. bool active_low = false;
  2837. bool single_ended = false;
  2838. int ret;
  2839. if (!fwnode)
  2840. return ERR_PTR(-EINVAL);
  2841. if (is_of_node(fwnode)) {
  2842. enum of_gpio_flags flags;
  2843. desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
  2844. &flags);
  2845. if (!IS_ERR(desc)) {
  2846. active_low = flags & OF_GPIO_ACTIVE_LOW;
  2847. single_ended = flags & OF_GPIO_SINGLE_ENDED;
  2848. }
  2849. } else if (is_acpi_node(fwnode)) {
  2850. struct acpi_gpio_info info;
  2851. desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
  2852. if (!IS_ERR(desc))
  2853. active_low = info.polarity == GPIO_ACTIVE_LOW;
  2854. }
  2855. if (IS_ERR(desc))
  2856. return desc;
  2857. ret = gpiod_request(desc, NULL);
  2858. if (ret)
  2859. return ERR_PTR(ret);
  2860. if (active_low)
  2861. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2862. if (single_ended) {
  2863. if (active_low)
  2864. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  2865. else
  2866. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  2867. }
  2868. return desc;
  2869. }
  2870. EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
  2871. /**
  2872. * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
  2873. * function
  2874. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2875. * @con_id: function within the GPIO consumer
  2876. * @index: index of the GPIO to obtain in the consumer
  2877. * @flags: optional GPIO initialization flags
  2878. *
  2879. * This is equivalent to gpiod_get_index(), except that when no GPIO with the
  2880. * specified index was assigned to the requested function it will return NULL.
  2881. * This is convenient for drivers that need to handle optional GPIOs.
  2882. */
  2883. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  2884. const char *con_id,
  2885. unsigned int index,
  2886. enum gpiod_flags flags)
  2887. {
  2888. struct gpio_desc *desc;
  2889. desc = gpiod_get_index(dev, con_id, index, flags);
  2890. if (IS_ERR(desc)) {
  2891. if (PTR_ERR(desc) == -ENOENT)
  2892. return NULL;
  2893. }
  2894. return desc;
  2895. }
  2896. EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
  2897. /**
  2898. * gpiod_hog - Hog the specified GPIO desc given the provided flags
  2899. * @desc: gpio whose value will be assigned
  2900. * @name: gpio line name
  2901. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  2902. * of_get_gpio_hog()
  2903. * @dflags: gpiod_flags - optional GPIO initialization flags
  2904. */
  2905. int gpiod_hog(struct gpio_desc *desc, const char *name,
  2906. unsigned long lflags, enum gpiod_flags dflags)
  2907. {
  2908. struct gpio_chip *chip;
  2909. struct gpio_desc *local_desc;
  2910. int hwnum;
  2911. int status;
  2912. chip = gpiod_to_chip(desc);
  2913. hwnum = gpio_chip_hwgpio(desc);
  2914. local_desc = gpiochip_request_own_desc(chip, hwnum, name);
  2915. if (IS_ERR(local_desc)) {
  2916. status = PTR_ERR(local_desc);
  2917. pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
  2918. name, chip->label, hwnum, status);
  2919. return status;
  2920. }
  2921. status = gpiod_configure_flags(desc, name, lflags, dflags);
  2922. if (status < 0) {
  2923. pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
  2924. name, chip->label, hwnum, status);
  2925. gpiochip_free_own_desc(desc);
  2926. return status;
  2927. }
  2928. /* Mark GPIO as hogged so it can be identified and removed later */
  2929. set_bit(FLAG_IS_HOGGED, &desc->flags);
  2930. pr_info("GPIO line %d (%s) hogged as %s%s\n",
  2931. desc_to_gpio(desc), name,
  2932. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
  2933. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
  2934. (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
  2935. return 0;
  2936. }
  2937. /**
  2938. * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
  2939. * @chip: gpio chip to act on
  2940. *
  2941. * This is only used by of_gpiochip_remove to free hogged gpios
  2942. */
  2943. static void gpiochip_free_hogs(struct gpio_chip *chip)
  2944. {
  2945. int id;
  2946. for (id = 0; id < chip->ngpio; id++) {
  2947. if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
  2948. gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
  2949. }
  2950. }
  2951. /**
  2952. * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
  2953. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2954. * @con_id: function within the GPIO consumer
  2955. * @flags: optional GPIO initialization flags
  2956. *
  2957. * This function acquires all the GPIOs defined under a given function.
  2958. *
  2959. * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
  2960. * no GPIO has been assigned to the requested function, or another IS_ERR()
  2961. * code if an error occurred while trying to acquire the GPIOs.
  2962. */
  2963. struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
  2964. const char *con_id,
  2965. enum gpiod_flags flags)
  2966. {
  2967. struct gpio_desc *desc;
  2968. struct gpio_descs *descs;
  2969. int count;
  2970. count = gpiod_count(dev, con_id);
  2971. if (count < 0)
  2972. return ERR_PTR(count);
  2973. descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
  2974. GFP_KERNEL);
  2975. if (!descs)
  2976. return ERR_PTR(-ENOMEM);
  2977. for (descs->ndescs = 0; descs->ndescs < count; ) {
  2978. desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
  2979. if (IS_ERR(desc)) {
  2980. gpiod_put_array(descs);
  2981. return ERR_CAST(desc);
  2982. }
  2983. descs->desc[descs->ndescs] = desc;
  2984. descs->ndescs++;
  2985. }
  2986. return descs;
  2987. }
  2988. EXPORT_SYMBOL_GPL(gpiod_get_array);
  2989. /**
  2990. * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
  2991. * function
  2992. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2993. * @con_id: function within the GPIO consumer
  2994. * @flags: optional GPIO initialization flags
  2995. *
  2996. * This is equivalent to gpiod_get_array(), except that when no GPIO was
  2997. * assigned to the requested function it will return NULL.
  2998. */
  2999. struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
  3000. const char *con_id,
  3001. enum gpiod_flags flags)
  3002. {
  3003. struct gpio_descs *descs;
  3004. descs = gpiod_get_array(dev, con_id, flags);
  3005. if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
  3006. return NULL;
  3007. return descs;
  3008. }
  3009. EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
  3010. /**
  3011. * gpiod_put - dispose of a GPIO descriptor
  3012. * @desc: GPIO descriptor to dispose of
  3013. *
  3014. * No descriptor can be used after gpiod_put() has been called on it.
  3015. */
  3016. void gpiod_put(struct gpio_desc *desc)
  3017. {
  3018. gpiod_free(desc);
  3019. }
  3020. EXPORT_SYMBOL_GPL(gpiod_put);
  3021. /**
  3022. * gpiod_put_array - dispose of multiple GPIO descriptors
  3023. * @descs: struct gpio_descs containing an array of descriptors
  3024. */
  3025. void gpiod_put_array(struct gpio_descs *descs)
  3026. {
  3027. unsigned int i;
  3028. for (i = 0; i < descs->ndescs; i++)
  3029. gpiod_put(descs->desc[i]);
  3030. kfree(descs);
  3031. }
  3032. EXPORT_SYMBOL_GPL(gpiod_put_array);
  3033. static int __init gpiolib_dev_init(void)
  3034. {
  3035. int ret;
  3036. /* Register GPIO sysfs bus */
  3037. ret = bus_register(&gpio_bus_type);
  3038. if (ret < 0) {
  3039. pr_err("gpiolib: could not register GPIO bus type\n");
  3040. return ret;
  3041. }
  3042. ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
  3043. if (ret < 0) {
  3044. pr_err("gpiolib: failed to allocate char dev region\n");
  3045. bus_unregister(&gpio_bus_type);
  3046. } else {
  3047. gpiolib_initialized = true;
  3048. gpiochip_setup_devs();
  3049. }
  3050. return ret;
  3051. }
  3052. core_initcall(gpiolib_dev_init);
  3053. #ifdef CONFIG_DEBUG_FS
  3054. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
  3055. {
  3056. unsigned i;
  3057. struct gpio_chip *chip = gdev->chip;
  3058. unsigned gpio = gdev->base;
  3059. struct gpio_desc *gdesc = &gdev->descs[0];
  3060. int is_out;
  3061. int is_irq;
  3062. for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
  3063. if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
  3064. if (gdesc->name) {
  3065. seq_printf(s, " gpio-%-3d (%-20.20s)\n",
  3066. gpio, gdesc->name);
  3067. }
  3068. continue;
  3069. }
  3070. gpiod_get_direction(gdesc);
  3071. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  3072. is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
  3073. seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
  3074. gpio, gdesc->name ? gdesc->name : "", gdesc->label,
  3075. is_out ? "out" : "in ",
  3076. chip->get
  3077. ? (chip->get(chip, i) ? "hi" : "lo")
  3078. : "? ",
  3079. is_irq ? "IRQ" : " ");
  3080. seq_printf(s, "\n");
  3081. }
  3082. }
  3083. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  3084. {
  3085. unsigned long flags;
  3086. struct gpio_device *gdev = NULL;
  3087. loff_t index = *pos;
  3088. s->private = "";
  3089. spin_lock_irqsave(&gpio_lock, flags);
  3090. list_for_each_entry(gdev, &gpio_devices, list)
  3091. if (index-- == 0) {
  3092. spin_unlock_irqrestore(&gpio_lock, flags);
  3093. return gdev;
  3094. }
  3095. spin_unlock_irqrestore(&gpio_lock, flags);
  3096. return NULL;
  3097. }
  3098. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  3099. {
  3100. unsigned long flags;
  3101. struct gpio_device *gdev = v;
  3102. void *ret = NULL;
  3103. spin_lock_irqsave(&gpio_lock, flags);
  3104. if (list_is_last(&gdev->list, &gpio_devices))
  3105. ret = NULL;
  3106. else
  3107. ret = list_entry(gdev->list.next, struct gpio_device, list);
  3108. spin_unlock_irqrestore(&gpio_lock, flags);
  3109. s->private = "\n";
  3110. ++*pos;
  3111. return ret;
  3112. }
  3113. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  3114. {
  3115. }
  3116. static int gpiolib_seq_show(struct seq_file *s, void *v)
  3117. {
  3118. struct gpio_device *gdev = v;
  3119. struct gpio_chip *chip = gdev->chip;
  3120. struct device *parent;
  3121. if (!chip) {
  3122. seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
  3123. dev_name(&gdev->dev));
  3124. return 0;
  3125. }
  3126. seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
  3127. dev_name(&gdev->dev),
  3128. gdev->base, gdev->base + gdev->ngpio - 1);
  3129. parent = chip->parent;
  3130. if (parent)
  3131. seq_printf(s, ", parent: %s/%s",
  3132. parent->bus ? parent->bus->name : "no-bus",
  3133. dev_name(parent));
  3134. if (chip->label)
  3135. seq_printf(s, ", %s", chip->label);
  3136. if (chip->can_sleep)
  3137. seq_printf(s, ", can sleep");
  3138. seq_printf(s, ":\n");
  3139. if (chip->dbg_show)
  3140. chip->dbg_show(s, chip);
  3141. else
  3142. gpiolib_dbg_show(s, gdev);
  3143. return 0;
  3144. }
  3145. static const struct seq_operations gpiolib_seq_ops = {
  3146. .start = gpiolib_seq_start,
  3147. .next = gpiolib_seq_next,
  3148. .stop = gpiolib_seq_stop,
  3149. .show = gpiolib_seq_show,
  3150. };
  3151. static int gpiolib_open(struct inode *inode, struct file *file)
  3152. {
  3153. return seq_open(file, &gpiolib_seq_ops);
  3154. }
  3155. static const struct file_operations gpiolib_operations = {
  3156. .owner = THIS_MODULE,
  3157. .open = gpiolib_open,
  3158. .read = seq_read,
  3159. .llseek = seq_lseek,
  3160. .release = seq_release,
  3161. };
  3162. static int __init gpiolib_debugfs_init(void)
  3163. {
  3164. /* /sys/kernel/debug/gpio */
  3165. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  3166. NULL, NULL, &gpiolib_operations);
  3167. return 0;
  3168. }
  3169. subsys_initcall(gpiolib_debugfs_init);
  3170. #endif /* DEBUG_FS */