gpiolib.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bitmap.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/irq.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/list.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/gpio.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/idr.h>
  16. #include <linux/slab.h>
  17. #include <linux/acpi.h>
  18. #include <linux/gpio/driver.h>
  19. #include <linux/gpio/machine.h>
  20. #include <linux/pinctrl/consumer.h>
  21. #include <linux/cdev.h>
  22. #include <linux/fs.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/compat.h>
  25. #include <linux/anon_inodes.h>
  26. #include <linux/file.h>
  27. #include <linux/kfifo.h>
  28. #include <linux/poll.h>
  29. #include <linux/timekeeping.h>
  30. #include <uapi/linux/gpio.h>
  31. #include "gpiolib.h"
  32. #define CREATE_TRACE_POINTS
  33. #include <trace/events/gpio.h>
  34. /* Implementation infrastructure for GPIO interfaces.
  35. *
  36. * The GPIO programming interface allows for inlining speed-critical
  37. * get/set operations for common cases, so that access to SOC-integrated
  38. * GPIOs can sometimes cost only an instruction or two per bit.
  39. */
  40. /* When debugging, extend minimal trust to callers and platform code.
  41. * Also emit diagnostic messages that may help initial bringup, when
  42. * board setup or driver bugs are most common.
  43. *
  44. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  45. */
  46. #ifdef DEBUG
  47. #define extra_checks 1
  48. #else
  49. #define extra_checks 0
  50. #endif
  51. /* Device and char device-related information */
  52. static DEFINE_IDA(gpio_ida);
  53. static dev_t gpio_devt;
  54. #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
  55. static struct bus_type gpio_bus_type = {
  56. .name = "gpio",
  57. };
  58. /*
  59. * Number of GPIOs to use for the fast path in set array
  60. */
  61. #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
  62. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  63. * While any GPIO is requested, its gpio_chip is not removable;
  64. * each GPIO's "requested" flag serves as a lock and refcount.
  65. */
  66. DEFINE_SPINLOCK(gpio_lock);
  67. static DEFINE_MUTEX(gpio_lookup_lock);
  68. static LIST_HEAD(gpio_lookup_list);
  69. LIST_HEAD(gpio_devices);
  70. static DEFINE_MUTEX(gpio_machine_hogs_mutex);
  71. static LIST_HEAD(gpio_machine_hogs);
  72. static void gpiochip_free_hogs(struct gpio_chip *chip);
  73. static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
  74. struct lock_class_key *lock_key,
  75. struct lock_class_key *request_key);
  76. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
  77. static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
  78. static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
  79. static bool gpiolib_initialized;
  80. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  81. {
  82. d->label = label;
  83. }
  84. /**
  85. * gpio_to_desc - Convert a GPIO number to its descriptor
  86. * @gpio: global GPIO number
  87. *
  88. * Returns:
  89. * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
  90. * with the given number exists in the system.
  91. */
  92. struct gpio_desc *gpio_to_desc(unsigned gpio)
  93. {
  94. struct gpio_device *gdev;
  95. unsigned long flags;
  96. spin_lock_irqsave(&gpio_lock, flags);
  97. list_for_each_entry(gdev, &gpio_devices, list) {
  98. if (gdev->base <= gpio &&
  99. gdev->base + gdev->ngpio > gpio) {
  100. spin_unlock_irqrestore(&gpio_lock, flags);
  101. return &gdev->descs[gpio - gdev->base];
  102. }
  103. }
  104. spin_unlock_irqrestore(&gpio_lock, flags);
  105. if (!gpio_is_valid(gpio))
  106. WARN(1, "invalid GPIO %d\n", gpio);
  107. return NULL;
  108. }
  109. EXPORT_SYMBOL_GPL(gpio_to_desc);
  110. /**
  111. * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
  112. * hardware number for this chip
  113. * @chip: GPIO chip
  114. * @hwnum: hardware number of the GPIO for this chip
  115. *
  116. * Returns:
  117. * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
  118. * in the given chip for the specified hardware number.
  119. */
  120. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
  121. u16 hwnum)
  122. {
  123. struct gpio_device *gdev = chip->gpiodev;
  124. if (hwnum >= gdev->ngpio)
  125. return ERR_PTR(-EINVAL);
  126. return &gdev->descs[hwnum];
  127. }
  128. /**
  129. * desc_to_gpio - convert a GPIO descriptor to the integer namespace
  130. * @desc: GPIO descriptor
  131. *
  132. * This should disappear in the future but is needed since we still
  133. * use GPIO numbers for error messages and sysfs nodes.
  134. *
  135. * Returns:
  136. * The global GPIO number for the GPIO specified by its descriptor.
  137. */
  138. int desc_to_gpio(const struct gpio_desc *desc)
  139. {
  140. return desc->gdev->base + (desc - &desc->gdev->descs[0]);
  141. }
  142. EXPORT_SYMBOL_GPL(desc_to_gpio);
  143. /**
  144. * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
  145. * @desc: descriptor to return the chip of
  146. */
  147. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  148. {
  149. if (!desc || !desc->gdev)
  150. return NULL;
  151. return desc->gdev->chip;
  152. }
  153. EXPORT_SYMBOL_GPL(gpiod_to_chip);
  154. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  155. static int gpiochip_find_base(int ngpio)
  156. {
  157. struct gpio_device *gdev;
  158. int base = ARCH_NR_GPIOS - ngpio;
  159. list_for_each_entry_reverse(gdev, &gpio_devices, list) {
  160. /* found a free space? */
  161. if (gdev->base + gdev->ngpio <= base)
  162. break;
  163. else
  164. /* nope, check the space right before the chip */
  165. base = gdev->base - ngpio;
  166. }
  167. if (gpio_is_valid(base)) {
  168. pr_debug("%s: found new base at %d\n", __func__, base);
  169. return base;
  170. } else {
  171. pr_err("%s: cannot find free range\n", __func__);
  172. return -ENOSPC;
  173. }
  174. }
  175. /**
  176. * gpiod_get_direction - return the current direction of a GPIO
  177. * @desc: GPIO to get the direction of
  178. *
  179. * Returns 0 for output, 1 for input, or an error code in case of error.
  180. *
  181. * This function may sleep if gpiod_cansleep() is true.
  182. */
  183. int gpiod_get_direction(struct gpio_desc *desc)
  184. {
  185. struct gpio_chip *chip;
  186. unsigned offset;
  187. int status;
  188. chip = gpiod_to_chip(desc);
  189. offset = gpio_chip_hwgpio(desc);
  190. if (!chip->get_direction)
  191. return -ENOTSUPP;
  192. status = chip->get_direction(chip, offset);
  193. if (status > 0) {
  194. /* GPIOF_DIR_IN, or other positive */
  195. status = 1;
  196. clear_bit(FLAG_IS_OUT, &desc->flags);
  197. }
  198. if (status == 0) {
  199. /* GPIOF_DIR_OUT */
  200. set_bit(FLAG_IS_OUT, &desc->flags);
  201. }
  202. return status;
  203. }
  204. EXPORT_SYMBOL_GPL(gpiod_get_direction);
  205. /*
  206. * Add a new chip to the global chips list, keeping the list of chips sorted
  207. * by range(means [base, base + ngpio - 1]) order.
  208. *
  209. * Return -EBUSY if the new chip overlaps with some other chip's integer
  210. * space.
  211. */
  212. static int gpiodev_add_to_list(struct gpio_device *gdev)
  213. {
  214. struct gpio_device *prev, *next;
  215. if (list_empty(&gpio_devices)) {
  216. /* initial entry in list */
  217. list_add_tail(&gdev->list, &gpio_devices);
  218. return 0;
  219. }
  220. next = list_entry(gpio_devices.next, struct gpio_device, list);
  221. if (gdev->base + gdev->ngpio <= next->base) {
  222. /* add before first entry */
  223. list_add(&gdev->list, &gpio_devices);
  224. return 0;
  225. }
  226. prev = list_entry(gpio_devices.prev, struct gpio_device, list);
  227. if (prev->base + prev->ngpio <= gdev->base) {
  228. /* add behind last entry */
  229. list_add_tail(&gdev->list, &gpio_devices);
  230. return 0;
  231. }
  232. list_for_each_entry_safe(prev, next, &gpio_devices, list) {
  233. /* at the end of the list */
  234. if (&next->list == &gpio_devices)
  235. break;
  236. /* add between prev and next */
  237. if (prev->base + prev->ngpio <= gdev->base
  238. && gdev->base + gdev->ngpio <= next->base) {
  239. list_add(&gdev->list, &prev->list);
  240. return 0;
  241. }
  242. }
  243. dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
  244. return -EBUSY;
  245. }
  246. /*
  247. * Convert a GPIO name to its descriptor
  248. */
  249. static struct gpio_desc *gpio_name_to_desc(const char * const name)
  250. {
  251. struct gpio_device *gdev;
  252. unsigned long flags;
  253. spin_lock_irqsave(&gpio_lock, flags);
  254. list_for_each_entry(gdev, &gpio_devices, list) {
  255. int i;
  256. for (i = 0; i != gdev->ngpio; ++i) {
  257. struct gpio_desc *desc = &gdev->descs[i];
  258. if (!desc->name || !name)
  259. continue;
  260. if (!strcmp(desc->name, name)) {
  261. spin_unlock_irqrestore(&gpio_lock, flags);
  262. return desc;
  263. }
  264. }
  265. }
  266. spin_unlock_irqrestore(&gpio_lock, flags);
  267. return NULL;
  268. }
  269. /*
  270. * Takes the names from gc->names and checks if they are all unique. If they
  271. * are, they are assigned to their gpio descriptors.
  272. *
  273. * Warning if one of the names is already used for a different GPIO.
  274. */
  275. static int gpiochip_set_desc_names(struct gpio_chip *gc)
  276. {
  277. struct gpio_device *gdev = gc->gpiodev;
  278. int i;
  279. if (!gc->names)
  280. return 0;
  281. /* First check all names if they are unique */
  282. for (i = 0; i != gc->ngpio; ++i) {
  283. struct gpio_desc *gpio;
  284. gpio = gpio_name_to_desc(gc->names[i]);
  285. if (gpio)
  286. dev_warn(&gdev->dev,
  287. "Detected name collision for GPIO name '%s'\n",
  288. gc->names[i]);
  289. }
  290. /* Then add all names to the GPIO descriptors */
  291. for (i = 0; i != gc->ngpio; ++i)
  292. gdev->descs[i].name = gc->names[i];
  293. return 0;
  294. }
  295. static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
  296. {
  297. unsigned long *p;
  298. p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
  299. if (!p)
  300. return NULL;
  301. /* Assume by default all GPIOs are valid */
  302. bitmap_fill(p, chip->ngpio);
  303. return p;
  304. }
  305. static int gpiochip_alloc_valid_mask(struct gpio_chip *gpiochip)
  306. {
  307. #ifdef CONFIG_OF_GPIO
  308. int size;
  309. struct device_node *np = gpiochip->of_node;
  310. size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
  311. if (size > 0 && size % 2 == 0)
  312. gpiochip->need_valid_mask = true;
  313. #endif
  314. if (!gpiochip->need_valid_mask)
  315. return 0;
  316. gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
  317. if (!gpiochip->valid_mask)
  318. return -ENOMEM;
  319. return 0;
  320. }
  321. static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
  322. {
  323. if (gpiochip->init_valid_mask)
  324. return gpiochip->init_valid_mask(gpiochip);
  325. return 0;
  326. }
  327. static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
  328. {
  329. kfree(gpiochip->valid_mask);
  330. gpiochip->valid_mask = NULL;
  331. }
  332. bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
  333. unsigned int offset)
  334. {
  335. /* No mask means all valid */
  336. if (likely(!gpiochip->valid_mask))
  337. return true;
  338. return test_bit(offset, gpiochip->valid_mask);
  339. }
  340. EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
  341. /*
  342. * GPIO line handle management
  343. */
  344. /**
  345. * struct linehandle_state - contains the state of a userspace handle
  346. * @gdev: the GPIO device the handle pertains to
  347. * @label: consumer label used to tag descriptors
  348. * @descs: the GPIO descriptors held by this handle
  349. * @numdescs: the number of descriptors held in the descs array
  350. */
  351. struct linehandle_state {
  352. struct gpio_device *gdev;
  353. const char *label;
  354. struct gpio_desc *descs[GPIOHANDLES_MAX];
  355. u32 numdescs;
  356. };
  357. #define GPIOHANDLE_REQUEST_VALID_FLAGS \
  358. (GPIOHANDLE_REQUEST_INPUT | \
  359. GPIOHANDLE_REQUEST_OUTPUT | \
  360. GPIOHANDLE_REQUEST_ACTIVE_LOW | \
  361. GPIOHANDLE_REQUEST_OPEN_DRAIN | \
  362. GPIOHANDLE_REQUEST_OPEN_SOURCE)
  363. static long linehandle_ioctl(struct file *filep, unsigned int cmd,
  364. unsigned long arg)
  365. {
  366. struct linehandle_state *lh = filep->private_data;
  367. void __user *ip = (void __user *)arg;
  368. struct gpiohandle_data ghd;
  369. DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
  370. int i;
  371. if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
  372. /* NOTE: It's ok to read values of output lines. */
  373. int ret = gpiod_get_array_value_complex(false,
  374. true,
  375. lh->numdescs,
  376. lh->descs,
  377. NULL,
  378. vals);
  379. if (ret)
  380. return ret;
  381. memset(&ghd, 0, sizeof(ghd));
  382. for (i = 0; i < lh->numdescs; i++)
  383. ghd.values[i] = test_bit(i, vals);
  384. if (copy_to_user(ip, &ghd, sizeof(ghd)))
  385. return -EFAULT;
  386. return 0;
  387. } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
  388. /*
  389. * All line descriptors were created at once with the same
  390. * flags so just check if the first one is really output.
  391. */
  392. if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
  393. return -EPERM;
  394. if (copy_from_user(&ghd, ip, sizeof(ghd)))
  395. return -EFAULT;
  396. /* Clamp all values to [0,1] */
  397. for (i = 0; i < lh->numdescs; i++)
  398. __assign_bit(i, vals, ghd.values[i]);
  399. /* Reuse the array setting function */
  400. return gpiod_set_array_value_complex(false,
  401. true,
  402. lh->numdescs,
  403. lh->descs,
  404. NULL,
  405. vals);
  406. }
  407. return -EINVAL;
  408. }
  409. #ifdef CONFIG_COMPAT
  410. static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
  411. unsigned long arg)
  412. {
  413. return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
  414. }
  415. #endif
  416. static int linehandle_release(struct inode *inode, struct file *filep)
  417. {
  418. struct linehandle_state *lh = filep->private_data;
  419. struct gpio_device *gdev = lh->gdev;
  420. int i;
  421. for (i = 0; i < lh->numdescs; i++)
  422. gpiod_free(lh->descs[i]);
  423. kfree(lh->label);
  424. kfree(lh);
  425. put_device(&gdev->dev);
  426. return 0;
  427. }
  428. static const struct file_operations linehandle_fileops = {
  429. .release = linehandle_release,
  430. .owner = THIS_MODULE,
  431. .llseek = noop_llseek,
  432. .unlocked_ioctl = linehandle_ioctl,
  433. #ifdef CONFIG_COMPAT
  434. .compat_ioctl = linehandle_ioctl_compat,
  435. #endif
  436. };
  437. static int linehandle_create(struct gpio_device *gdev, void __user *ip)
  438. {
  439. struct gpiohandle_request handlereq;
  440. struct linehandle_state *lh;
  441. struct file *file;
  442. int fd, i, count = 0, ret;
  443. u32 lflags;
  444. if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
  445. return -EFAULT;
  446. if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
  447. return -EINVAL;
  448. lflags = handlereq.flags;
  449. /* Return an error if an unknown flag is set */
  450. if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
  451. return -EINVAL;
  452. /*
  453. * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
  454. * the hardware actually supports enabling both at the same time the
  455. * electrical result would be disastrous.
  456. */
  457. if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
  458. (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
  459. return -EINVAL;
  460. /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
  461. if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
  462. ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
  463. (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
  464. return -EINVAL;
  465. lh = kzalloc(sizeof(*lh), GFP_KERNEL);
  466. if (!lh)
  467. return -ENOMEM;
  468. lh->gdev = gdev;
  469. get_device(&gdev->dev);
  470. /* Make sure this is terminated */
  471. handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
  472. if (strlen(handlereq.consumer_label)) {
  473. lh->label = kstrdup(handlereq.consumer_label,
  474. GFP_KERNEL);
  475. if (!lh->label) {
  476. ret = -ENOMEM;
  477. goto out_free_lh;
  478. }
  479. }
  480. /* Request each GPIO */
  481. for (i = 0; i < handlereq.lines; i++) {
  482. u32 offset = handlereq.lineoffsets[i];
  483. struct gpio_desc *desc;
  484. if (offset >= gdev->ngpio) {
  485. ret = -EINVAL;
  486. goto out_free_descs;
  487. }
  488. desc = &gdev->descs[offset];
  489. ret = gpiod_request(desc, lh->label);
  490. if (ret)
  491. goto out_free_descs;
  492. lh->descs[i] = desc;
  493. count = i + 1;
  494. if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
  495. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  496. if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
  497. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  498. if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
  499. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  500. ret = gpiod_set_transitory(desc, false);
  501. if (ret < 0)
  502. goto out_free_descs;
  503. /*
  504. * Lines have to be requested explicitly for input
  505. * or output, else the line will be treated "as is".
  506. */
  507. if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
  508. int val = !!handlereq.default_values[i];
  509. ret = gpiod_direction_output(desc, val);
  510. if (ret)
  511. goto out_free_descs;
  512. } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
  513. ret = gpiod_direction_input(desc);
  514. if (ret)
  515. goto out_free_descs;
  516. }
  517. dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
  518. offset);
  519. }
  520. /* Let i point at the last handle */
  521. i--;
  522. lh->numdescs = handlereq.lines;
  523. fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
  524. if (fd < 0) {
  525. ret = fd;
  526. goto out_free_descs;
  527. }
  528. file = anon_inode_getfile("gpio-linehandle",
  529. &linehandle_fileops,
  530. lh,
  531. O_RDONLY | O_CLOEXEC);
  532. if (IS_ERR(file)) {
  533. ret = PTR_ERR(file);
  534. goto out_put_unused_fd;
  535. }
  536. handlereq.fd = fd;
  537. if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
  538. /*
  539. * fput() will trigger the release() callback, so do not go onto
  540. * the regular error cleanup path here.
  541. */
  542. fput(file);
  543. put_unused_fd(fd);
  544. return -EFAULT;
  545. }
  546. fd_install(fd, file);
  547. dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
  548. lh->numdescs);
  549. return 0;
  550. out_put_unused_fd:
  551. put_unused_fd(fd);
  552. out_free_descs:
  553. for (i = 0; i < count; i++)
  554. gpiod_free(lh->descs[i]);
  555. kfree(lh->label);
  556. out_free_lh:
  557. kfree(lh);
  558. put_device(&gdev->dev);
  559. return ret;
  560. }
  561. /*
  562. * GPIO line event management
  563. */
  564. /**
  565. * struct lineevent_state - contains the state of a userspace event
  566. * @gdev: the GPIO device the event pertains to
  567. * @label: consumer label used to tag descriptors
  568. * @desc: the GPIO descriptor held by this event
  569. * @eflags: the event flags this line was requested with
  570. * @irq: the interrupt that trigger in response to events on this GPIO
  571. * @wait: wait queue that handles blocking reads of events
  572. * @events: KFIFO for the GPIO events
  573. * @read_lock: mutex lock to protect reads from colliding with adding
  574. * new events to the FIFO
  575. * @timestamp: cache for the timestamp storing it between hardirq
  576. * and IRQ thread, used to bring the timestamp close to the actual
  577. * event
  578. */
  579. struct lineevent_state {
  580. struct gpio_device *gdev;
  581. const char *label;
  582. struct gpio_desc *desc;
  583. u32 eflags;
  584. int irq;
  585. wait_queue_head_t wait;
  586. DECLARE_KFIFO(events, struct gpioevent_data, 16);
  587. struct mutex read_lock;
  588. u64 timestamp;
  589. };
  590. #define GPIOEVENT_REQUEST_VALID_FLAGS \
  591. (GPIOEVENT_REQUEST_RISING_EDGE | \
  592. GPIOEVENT_REQUEST_FALLING_EDGE)
  593. static __poll_t lineevent_poll(struct file *filep,
  594. struct poll_table_struct *wait)
  595. {
  596. struct lineevent_state *le = filep->private_data;
  597. __poll_t events = 0;
  598. poll_wait(filep, &le->wait, wait);
  599. if (!kfifo_is_empty(&le->events))
  600. events = EPOLLIN | EPOLLRDNORM;
  601. return events;
  602. }
  603. static ssize_t lineevent_read(struct file *filep,
  604. char __user *buf,
  605. size_t count,
  606. loff_t *f_ps)
  607. {
  608. struct lineevent_state *le = filep->private_data;
  609. unsigned int copied;
  610. int ret;
  611. if (count < sizeof(struct gpioevent_data))
  612. return -EINVAL;
  613. do {
  614. if (kfifo_is_empty(&le->events)) {
  615. if (filep->f_flags & O_NONBLOCK)
  616. return -EAGAIN;
  617. ret = wait_event_interruptible(le->wait,
  618. !kfifo_is_empty(&le->events));
  619. if (ret)
  620. return ret;
  621. }
  622. if (mutex_lock_interruptible(&le->read_lock))
  623. return -ERESTARTSYS;
  624. ret = kfifo_to_user(&le->events, buf, count, &copied);
  625. mutex_unlock(&le->read_lock);
  626. if (ret)
  627. return ret;
  628. /*
  629. * If we couldn't read anything from the fifo (a different
  630. * thread might have been faster) we either return -EAGAIN if
  631. * the file descriptor is non-blocking, otherwise we go back to
  632. * sleep and wait for more data to arrive.
  633. */
  634. if (copied == 0 && (filep->f_flags & O_NONBLOCK))
  635. return -EAGAIN;
  636. } while (copied == 0);
  637. return copied;
  638. }
  639. static int lineevent_release(struct inode *inode, struct file *filep)
  640. {
  641. struct lineevent_state *le = filep->private_data;
  642. struct gpio_device *gdev = le->gdev;
  643. free_irq(le->irq, le);
  644. gpiod_free(le->desc);
  645. kfree(le->label);
  646. kfree(le);
  647. put_device(&gdev->dev);
  648. return 0;
  649. }
  650. static long lineevent_ioctl(struct file *filep, unsigned int cmd,
  651. unsigned long arg)
  652. {
  653. struct lineevent_state *le = filep->private_data;
  654. void __user *ip = (void __user *)arg;
  655. struct gpiohandle_data ghd;
  656. /*
  657. * We can get the value for an event line but not set it,
  658. * because it is input by definition.
  659. */
  660. if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
  661. int val;
  662. memset(&ghd, 0, sizeof(ghd));
  663. val = gpiod_get_value_cansleep(le->desc);
  664. if (val < 0)
  665. return val;
  666. ghd.values[0] = val;
  667. if (copy_to_user(ip, &ghd, sizeof(ghd)))
  668. return -EFAULT;
  669. return 0;
  670. }
  671. return -EINVAL;
  672. }
  673. #ifdef CONFIG_COMPAT
  674. static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
  675. unsigned long arg)
  676. {
  677. return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
  678. }
  679. #endif
  680. static const struct file_operations lineevent_fileops = {
  681. .release = lineevent_release,
  682. .read = lineevent_read,
  683. .poll = lineevent_poll,
  684. .owner = THIS_MODULE,
  685. .llseek = noop_llseek,
  686. .unlocked_ioctl = lineevent_ioctl,
  687. #ifdef CONFIG_COMPAT
  688. .compat_ioctl = lineevent_ioctl_compat,
  689. #endif
  690. };
  691. static irqreturn_t lineevent_irq_thread(int irq, void *p)
  692. {
  693. struct lineevent_state *le = p;
  694. struct gpioevent_data ge;
  695. int ret;
  696. /* Do not leak kernel stack to userspace */
  697. memset(&ge, 0, sizeof(ge));
  698. ge.timestamp = le->timestamp;
  699. if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
  700. && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
  701. int level = gpiod_get_value_cansleep(le->desc);
  702. if (level)
  703. /* Emit low-to-high event */
  704. ge.id = GPIOEVENT_EVENT_RISING_EDGE;
  705. else
  706. /* Emit high-to-low event */
  707. ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
  708. } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
  709. /* Emit low-to-high event */
  710. ge.id = GPIOEVENT_EVENT_RISING_EDGE;
  711. } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
  712. /* Emit high-to-low event */
  713. ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
  714. } else {
  715. return IRQ_NONE;
  716. }
  717. ret = kfifo_put(&le->events, ge);
  718. if (ret != 0)
  719. wake_up_poll(&le->wait, EPOLLIN);
  720. return IRQ_HANDLED;
  721. }
  722. static irqreturn_t lineevent_irq_handler(int irq, void *p)
  723. {
  724. struct lineevent_state *le = p;
  725. /*
  726. * Just store the timestamp in hardirq context so we get it as
  727. * close in time as possible to the actual event.
  728. */
  729. le->timestamp = ktime_get_real_ns();
  730. return IRQ_WAKE_THREAD;
  731. }
  732. static int lineevent_create(struct gpio_device *gdev, void __user *ip)
  733. {
  734. struct gpioevent_request eventreq;
  735. struct lineevent_state *le;
  736. struct gpio_desc *desc;
  737. struct file *file;
  738. u32 offset;
  739. u32 lflags;
  740. u32 eflags;
  741. int fd;
  742. int ret;
  743. int irqflags = 0;
  744. if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
  745. return -EFAULT;
  746. le = kzalloc(sizeof(*le), GFP_KERNEL);
  747. if (!le)
  748. return -ENOMEM;
  749. le->gdev = gdev;
  750. get_device(&gdev->dev);
  751. /* Make sure this is terminated */
  752. eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
  753. if (strlen(eventreq.consumer_label)) {
  754. le->label = kstrdup(eventreq.consumer_label,
  755. GFP_KERNEL);
  756. if (!le->label) {
  757. ret = -ENOMEM;
  758. goto out_free_le;
  759. }
  760. }
  761. offset = eventreq.lineoffset;
  762. lflags = eventreq.handleflags;
  763. eflags = eventreq.eventflags;
  764. if (offset >= gdev->ngpio) {
  765. ret = -EINVAL;
  766. goto out_free_label;
  767. }
  768. /* Return an error if a unknown flag is set */
  769. if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
  770. (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
  771. ret = -EINVAL;
  772. goto out_free_label;
  773. }
  774. /* This is just wrong: we don't look for events on output lines */
  775. if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
  776. ret = -EINVAL;
  777. goto out_free_label;
  778. }
  779. desc = &gdev->descs[offset];
  780. ret = gpiod_request(desc, le->label);
  781. if (ret)
  782. goto out_free_label;
  783. le->desc = desc;
  784. le->eflags = eflags;
  785. if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
  786. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  787. if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
  788. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  789. if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
  790. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  791. ret = gpiod_direction_input(desc);
  792. if (ret)
  793. goto out_free_desc;
  794. le->irq = gpiod_to_irq(desc);
  795. if (le->irq <= 0) {
  796. ret = -ENODEV;
  797. goto out_free_desc;
  798. }
  799. if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
  800. irqflags |= IRQF_TRIGGER_RISING;
  801. if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
  802. irqflags |= IRQF_TRIGGER_FALLING;
  803. irqflags |= IRQF_ONESHOT;
  804. INIT_KFIFO(le->events);
  805. init_waitqueue_head(&le->wait);
  806. mutex_init(&le->read_lock);
  807. /* Request a thread to read the events */
  808. ret = request_threaded_irq(le->irq,
  809. lineevent_irq_handler,
  810. lineevent_irq_thread,
  811. irqflags,
  812. le->label,
  813. le);
  814. if (ret)
  815. goto out_free_desc;
  816. fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
  817. if (fd < 0) {
  818. ret = fd;
  819. goto out_free_irq;
  820. }
  821. file = anon_inode_getfile("gpio-event",
  822. &lineevent_fileops,
  823. le,
  824. O_RDONLY | O_CLOEXEC);
  825. if (IS_ERR(file)) {
  826. ret = PTR_ERR(file);
  827. goto out_put_unused_fd;
  828. }
  829. eventreq.fd = fd;
  830. if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
  831. /*
  832. * fput() will trigger the release() callback, so do not go onto
  833. * the regular error cleanup path here.
  834. */
  835. fput(file);
  836. put_unused_fd(fd);
  837. return -EFAULT;
  838. }
  839. fd_install(fd, file);
  840. return 0;
  841. out_put_unused_fd:
  842. put_unused_fd(fd);
  843. out_free_irq:
  844. free_irq(le->irq, le);
  845. out_free_desc:
  846. gpiod_free(le->desc);
  847. out_free_label:
  848. kfree(le->label);
  849. out_free_le:
  850. kfree(le);
  851. put_device(&gdev->dev);
  852. return ret;
  853. }
  854. /*
  855. * gpio_ioctl() - ioctl handler for the GPIO chardev
  856. */
  857. static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  858. {
  859. struct gpio_device *gdev = filp->private_data;
  860. struct gpio_chip *chip = gdev->chip;
  861. void __user *ip = (void __user *)arg;
  862. /* We fail any subsequent ioctl():s when the chip is gone */
  863. if (!chip)
  864. return -ENODEV;
  865. /* Fill in the struct and pass to userspace */
  866. if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
  867. struct gpiochip_info chipinfo;
  868. memset(&chipinfo, 0, sizeof(chipinfo));
  869. strncpy(chipinfo.name, dev_name(&gdev->dev),
  870. sizeof(chipinfo.name));
  871. chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
  872. strncpy(chipinfo.label, gdev->label,
  873. sizeof(chipinfo.label));
  874. chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
  875. chipinfo.lines = gdev->ngpio;
  876. if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
  877. return -EFAULT;
  878. return 0;
  879. } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
  880. struct gpioline_info lineinfo;
  881. struct gpio_desc *desc;
  882. if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
  883. return -EFAULT;
  884. if (lineinfo.line_offset >= gdev->ngpio)
  885. return -EINVAL;
  886. desc = &gdev->descs[lineinfo.line_offset];
  887. if (desc->name) {
  888. strncpy(lineinfo.name, desc->name,
  889. sizeof(lineinfo.name));
  890. lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
  891. } else {
  892. lineinfo.name[0] = '\0';
  893. }
  894. if (desc->label) {
  895. strncpy(lineinfo.consumer, desc->label,
  896. sizeof(lineinfo.consumer));
  897. lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
  898. } else {
  899. lineinfo.consumer[0] = '\0';
  900. }
  901. /*
  902. * Userspace only need to know that the kernel is using
  903. * this GPIO so it can't use it.
  904. */
  905. lineinfo.flags = 0;
  906. if (test_bit(FLAG_REQUESTED, &desc->flags) ||
  907. test_bit(FLAG_IS_HOGGED, &desc->flags) ||
  908. test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
  909. test_bit(FLAG_EXPORT, &desc->flags) ||
  910. test_bit(FLAG_SYSFS, &desc->flags))
  911. lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
  912. if (test_bit(FLAG_IS_OUT, &desc->flags))
  913. lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
  914. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  915. lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
  916. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  917. lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
  918. if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  919. lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
  920. if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
  921. return -EFAULT;
  922. return 0;
  923. } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
  924. return linehandle_create(gdev, ip);
  925. } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
  926. return lineevent_create(gdev, ip);
  927. }
  928. return -EINVAL;
  929. }
  930. #ifdef CONFIG_COMPAT
  931. static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
  932. unsigned long arg)
  933. {
  934. return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  935. }
  936. #endif
  937. /**
  938. * gpio_chrdev_open() - open the chardev for ioctl operations
  939. * @inode: inode for this chardev
  940. * @filp: file struct for storing private data
  941. * Returns 0 on success
  942. */
  943. static int gpio_chrdev_open(struct inode *inode, struct file *filp)
  944. {
  945. struct gpio_device *gdev = container_of(inode->i_cdev,
  946. struct gpio_device, chrdev);
  947. /* Fail on open if the backing gpiochip is gone */
  948. if (!gdev->chip)
  949. return -ENODEV;
  950. get_device(&gdev->dev);
  951. filp->private_data = gdev;
  952. return nonseekable_open(inode, filp);
  953. }
  954. /**
  955. * gpio_chrdev_release() - close chardev after ioctl operations
  956. * @inode: inode for this chardev
  957. * @filp: file struct for storing private data
  958. * Returns 0 on success
  959. */
  960. static int gpio_chrdev_release(struct inode *inode, struct file *filp)
  961. {
  962. struct gpio_device *gdev = container_of(inode->i_cdev,
  963. struct gpio_device, chrdev);
  964. put_device(&gdev->dev);
  965. return 0;
  966. }
  967. static const struct file_operations gpio_fileops = {
  968. .release = gpio_chrdev_release,
  969. .open = gpio_chrdev_open,
  970. .owner = THIS_MODULE,
  971. .llseek = no_llseek,
  972. .unlocked_ioctl = gpio_ioctl,
  973. #ifdef CONFIG_COMPAT
  974. .compat_ioctl = gpio_ioctl_compat,
  975. #endif
  976. };
  977. static void gpiodevice_release(struct device *dev)
  978. {
  979. struct gpio_device *gdev = dev_get_drvdata(dev);
  980. list_del(&gdev->list);
  981. ida_simple_remove(&gpio_ida, gdev->id);
  982. kfree_const(gdev->label);
  983. kfree(gdev->descs);
  984. kfree(gdev);
  985. }
  986. static int gpiochip_setup_dev(struct gpio_device *gdev)
  987. {
  988. int status;
  989. cdev_init(&gdev->chrdev, &gpio_fileops);
  990. gdev->chrdev.owner = THIS_MODULE;
  991. gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
  992. status = cdev_device_add(&gdev->chrdev, &gdev->dev);
  993. if (status)
  994. return status;
  995. chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
  996. MAJOR(gpio_devt), gdev->id);
  997. status = gpiochip_sysfs_register(gdev);
  998. if (status)
  999. goto err_remove_device;
  1000. /* From this point, the .release() function cleans up gpio_device */
  1001. gdev->dev.release = gpiodevice_release;
  1002. pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
  1003. __func__, gdev->base, gdev->base + gdev->ngpio - 1,
  1004. dev_name(&gdev->dev), gdev->chip->label ? : "generic");
  1005. return 0;
  1006. err_remove_device:
  1007. cdev_device_del(&gdev->chrdev, &gdev->dev);
  1008. return status;
  1009. }
  1010. static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
  1011. {
  1012. struct gpio_desc *desc;
  1013. int rv;
  1014. desc = gpiochip_get_desc(chip, hog->chip_hwnum);
  1015. if (IS_ERR(desc)) {
  1016. pr_err("%s: unable to get GPIO desc: %ld\n",
  1017. __func__, PTR_ERR(desc));
  1018. return;
  1019. }
  1020. if (test_bit(FLAG_IS_HOGGED, &desc->flags))
  1021. return;
  1022. rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
  1023. if (rv)
  1024. pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
  1025. __func__, chip->label, hog->chip_hwnum, rv);
  1026. }
  1027. static void machine_gpiochip_add(struct gpio_chip *chip)
  1028. {
  1029. struct gpiod_hog *hog;
  1030. mutex_lock(&gpio_machine_hogs_mutex);
  1031. list_for_each_entry(hog, &gpio_machine_hogs, list) {
  1032. if (!strcmp(chip->label, hog->chip_label))
  1033. gpiochip_machine_hog(chip, hog);
  1034. }
  1035. mutex_unlock(&gpio_machine_hogs_mutex);
  1036. }
  1037. static void gpiochip_setup_devs(void)
  1038. {
  1039. struct gpio_device *gdev;
  1040. int err;
  1041. list_for_each_entry(gdev, &gpio_devices, list) {
  1042. err = gpiochip_setup_dev(gdev);
  1043. if (err)
  1044. pr_err("%s: Failed to initialize gpio device (%d)\n",
  1045. dev_name(&gdev->dev), err);
  1046. }
  1047. }
  1048. int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
  1049. struct lock_class_key *lock_key,
  1050. struct lock_class_key *request_key)
  1051. {
  1052. unsigned long flags;
  1053. int status = 0;
  1054. unsigned i;
  1055. int base = chip->base;
  1056. struct gpio_device *gdev;
  1057. /*
  1058. * First: allocate and populate the internal stat container, and
  1059. * set up the struct device.
  1060. */
  1061. gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  1062. if (!gdev)
  1063. return -ENOMEM;
  1064. gdev->dev.bus = &gpio_bus_type;
  1065. gdev->chip = chip;
  1066. chip->gpiodev = gdev;
  1067. if (chip->parent) {
  1068. gdev->dev.parent = chip->parent;
  1069. gdev->dev.of_node = chip->parent->of_node;
  1070. }
  1071. #ifdef CONFIG_OF_GPIO
  1072. /* If the gpiochip has an assigned OF node this takes precedence */
  1073. if (chip->of_node)
  1074. gdev->dev.of_node = chip->of_node;
  1075. else
  1076. chip->of_node = gdev->dev.of_node;
  1077. #endif
  1078. gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
  1079. if (gdev->id < 0) {
  1080. status = gdev->id;
  1081. goto err_free_gdev;
  1082. }
  1083. dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
  1084. device_initialize(&gdev->dev);
  1085. dev_set_drvdata(&gdev->dev, gdev);
  1086. if (chip->parent && chip->parent->driver)
  1087. gdev->owner = chip->parent->driver->owner;
  1088. else if (chip->owner)
  1089. /* TODO: remove chip->owner */
  1090. gdev->owner = chip->owner;
  1091. else
  1092. gdev->owner = THIS_MODULE;
  1093. gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
  1094. if (!gdev->descs) {
  1095. status = -ENOMEM;
  1096. goto err_free_ida;
  1097. }
  1098. if (chip->ngpio == 0) {
  1099. chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
  1100. status = -EINVAL;
  1101. goto err_free_descs;
  1102. }
  1103. if (chip->ngpio > FASTPATH_NGPIO)
  1104. chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
  1105. chip->ngpio, FASTPATH_NGPIO);
  1106. gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
  1107. if (!gdev->label) {
  1108. status = -ENOMEM;
  1109. goto err_free_descs;
  1110. }
  1111. gdev->ngpio = chip->ngpio;
  1112. gdev->data = data;
  1113. spin_lock_irqsave(&gpio_lock, flags);
  1114. /*
  1115. * TODO: this allocates a Linux GPIO number base in the global
  1116. * GPIO numberspace for this chip. In the long run we want to
  1117. * get *rid* of this numberspace and use only descriptors, but
  1118. * it may be a pipe dream. It will not happen before we get rid
  1119. * of the sysfs interface anyways.
  1120. */
  1121. if (base < 0) {
  1122. base = gpiochip_find_base(chip->ngpio);
  1123. if (base < 0) {
  1124. status = base;
  1125. spin_unlock_irqrestore(&gpio_lock, flags);
  1126. goto err_free_label;
  1127. }
  1128. /*
  1129. * TODO: it should not be necessary to reflect the assigned
  1130. * base outside of the GPIO subsystem. Go over drivers and
  1131. * see if anyone makes use of this, else drop this and assign
  1132. * a poison instead.
  1133. */
  1134. chip->base = base;
  1135. }
  1136. gdev->base = base;
  1137. status = gpiodev_add_to_list(gdev);
  1138. if (status) {
  1139. spin_unlock_irqrestore(&gpio_lock, flags);
  1140. goto err_free_label;
  1141. }
  1142. spin_unlock_irqrestore(&gpio_lock, flags);
  1143. for (i = 0; i < chip->ngpio; i++)
  1144. gdev->descs[i].gdev = gdev;
  1145. #ifdef CONFIG_PINCTRL
  1146. INIT_LIST_HEAD(&gdev->pin_ranges);
  1147. #endif
  1148. status = gpiochip_set_desc_names(chip);
  1149. if (status)
  1150. goto err_remove_from_list;
  1151. status = gpiochip_irqchip_init_valid_mask(chip);
  1152. if (status)
  1153. goto err_remove_from_list;
  1154. status = gpiochip_alloc_valid_mask(chip);
  1155. if (status)
  1156. goto err_remove_irqchip_mask;
  1157. status = gpiochip_add_irqchip(chip, lock_key, request_key);
  1158. if (status)
  1159. goto err_remove_chip;
  1160. status = of_gpiochip_add(chip);
  1161. if (status)
  1162. goto err_remove_chip;
  1163. status = gpiochip_init_valid_mask(chip);
  1164. if (status)
  1165. goto err_remove_chip;
  1166. for (i = 0; i < chip->ngpio; i++) {
  1167. struct gpio_desc *desc = &gdev->descs[i];
  1168. if (chip->get_direction && gpiochip_line_is_valid(chip, i))
  1169. desc->flags = !chip->get_direction(chip, i) ?
  1170. (1 << FLAG_IS_OUT) : 0;
  1171. else
  1172. desc->flags = !chip->direction_input ?
  1173. (1 << FLAG_IS_OUT) : 0;
  1174. }
  1175. acpi_gpiochip_add(chip);
  1176. machine_gpiochip_add(chip);
  1177. /*
  1178. * By first adding the chardev, and then adding the device,
  1179. * we get a device node entry in sysfs under
  1180. * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
  1181. * coldplug of device nodes and other udev business.
  1182. * We can do this only if gpiolib has been initialized.
  1183. * Otherwise, defer until later.
  1184. */
  1185. if (gpiolib_initialized) {
  1186. status = gpiochip_setup_dev(gdev);
  1187. if (status)
  1188. goto err_remove_chip;
  1189. }
  1190. return 0;
  1191. err_remove_chip:
  1192. acpi_gpiochip_remove(chip);
  1193. gpiochip_free_hogs(chip);
  1194. of_gpiochip_remove(chip);
  1195. gpiochip_free_valid_mask(chip);
  1196. err_remove_irqchip_mask:
  1197. gpiochip_irqchip_free_valid_mask(chip);
  1198. err_remove_from_list:
  1199. spin_lock_irqsave(&gpio_lock, flags);
  1200. list_del(&gdev->list);
  1201. spin_unlock_irqrestore(&gpio_lock, flags);
  1202. err_free_label:
  1203. kfree_const(gdev->label);
  1204. err_free_descs:
  1205. kfree(gdev->descs);
  1206. err_free_ida:
  1207. ida_simple_remove(&gpio_ida, gdev->id);
  1208. err_free_gdev:
  1209. /* failures here can mean systems won't boot... */
  1210. pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
  1211. gdev->base, gdev->base + gdev->ngpio - 1,
  1212. chip->label ? : "generic", status);
  1213. kfree(gdev);
  1214. return status;
  1215. }
  1216. EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
  1217. /**
  1218. * gpiochip_get_data() - get per-subdriver data for the chip
  1219. * @chip: GPIO chip
  1220. *
  1221. * Returns:
  1222. * The per-subdriver data for the chip.
  1223. */
  1224. void *gpiochip_get_data(struct gpio_chip *chip)
  1225. {
  1226. return chip->gpiodev->data;
  1227. }
  1228. EXPORT_SYMBOL_GPL(gpiochip_get_data);
  1229. /**
  1230. * gpiochip_remove() - unregister a gpio_chip
  1231. * @chip: the chip to unregister
  1232. *
  1233. * A gpio_chip with any GPIOs still requested may not be removed.
  1234. */
  1235. void gpiochip_remove(struct gpio_chip *chip)
  1236. {
  1237. struct gpio_device *gdev = chip->gpiodev;
  1238. struct gpio_desc *desc;
  1239. unsigned long flags;
  1240. unsigned i;
  1241. bool requested = false;
  1242. /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
  1243. gpiochip_sysfs_unregister(gdev);
  1244. gpiochip_free_hogs(chip);
  1245. /* Numb the device, cancelling all outstanding operations */
  1246. gdev->chip = NULL;
  1247. gpiochip_irqchip_remove(chip);
  1248. acpi_gpiochip_remove(chip);
  1249. gpiochip_remove_pin_ranges(chip);
  1250. of_gpiochip_remove(chip);
  1251. gpiochip_free_valid_mask(chip);
  1252. /*
  1253. * We accept no more calls into the driver from this point, so
  1254. * NULL the driver data pointer
  1255. */
  1256. gdev->data = NULL;
  1257. spin_lock_irqsave(&gpio_lock, flags);
  1258. for (i = 0; i < gdev->ngpio; i++) {
  1259. desc = &gdev->descs[i];
  1260. if (test_bit(FLAG_REQUESTED, &desc->flags))
  1261. requested = true;
  1262. }
  1263. spin_unlock_irqrestore(&gpio_lock, flags);
  1264. if (requested)
  1265. dev_crit(&gdev->dev,
  1266. "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
  1267. /*
  1268. * The gpiochip side puts its use of the device to rest here:
  1269. * if there are no userspace clients, the chardev and device will
  1270. * be removed, else it will be dangling until the last user is
  1271. * gone.
  1272. */
  1273. cdev_device_del(&gdev->chrdev, &gdev->dev);
  1274. put_device(&gdev->dev);
  1275. }
  1276. EXPORT_SYMBOL_GPL(gpiochip_remove);
  1277. static void devm_gpio_chip_release(struct device *dev, void *res)
  1278. {
  1279. struct gpio_chip *chip = *(struct gpio_chip **)res;
  1280. gpiochip_remove(chip);
  1281. }
  1282. static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
  1283. {
  1284. struct gpio_chip **r = res;
  1285. if (!r || !*r) {
  1286. WARN_ON(!r || !*r);
  1287. return 0;
  1288. }
  1289. return *r == data;
  1290. }
  1291. /**
  1292. * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
  1293. * @dev: pointer to the device that gpio_chip belongs to.
  1294. * @chip: the chip to register, with chip->base initialized
  1295. * @data: driver-private data associated with this chip
  1296. *
  1297. * Context: potentially before irqs will work
  1298. *
  1299. * The gpio chip automatically be released when the device is unbound.
  1300. *
  1301. * Returns:
  1302. * A negative errno if the chip can't be registered, such as because the
  1303. * chip->base is invalid or already associated with a different chip.
  1304. * Otherwise it returns zero as a success code.
  1305. */
  1306. int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
  1307. void *data)
  1308. {
  1309. struct gpio_chip **ptr;
  1310. int ret;
  1311. ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
  1312. GFP_KERNEL);
  1313. if (!ptr)
  1314. return -ENOMEM;
  1315. ret = gpiochip_add_data(chip, data);
  1316. if (ret < 0) {
  1317. devres_free(ptr);
  1318. return ret;
  1319. }
  1320. *ptr = chip;
  1321. devres_add(dev, ptr);
  1322. return 0;
  1323. }
  1324. EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
  1325. /**
  1326. * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
  1327. * @dev: device for which which resource was allocated
  1328. * @chip: the chip to remove
  1329. *
  1330. * A gpio_chip with any GPIOs still requested may not be removed.
  1331. */
  1332. void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
  1333. {
  1334. int ret;
  1335. ret = devres_release(dev, devm_gpio_chip_release,
  1336. devm_gpio_chip_match, chip);
  1337. WARN_ON(ret);
  1338. }
  1339. EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
  1340. /**
  1341. * gpiochip_find() - iterator for locating a specific gpio_chip
  1342. * @data: data to pass to match function
  1343. * @match: Callback function to check gpio_chip
  1344. *
  1345. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  1346. * determined by a user supplied @match callback. The callback should return
  1347. * 0 if the device doesn't match and non-zero if it does. If the callback is
  1348. * non-zero, this function will return to the caller and not iterate over any
  1349. * more gpio_chips.
  1350. */
  1351. struct gpio_chip *gpiochip_find(void *data,
  1352. int (*match)(struct gpio_chip *chip,
  1353. void *data))
  1354. {
  1355. struct gpio_device *gdev;
  1356. struct gpio_chip *chip = NULL;
  1357. unsigned long flags;
  1358. spin_lock_irqsave(&gpio_lock, flags);
  1359. list_for_each_entry(gdev, &gpio_devices, list)
  1360. if (gdev->chip && match(gdev->chip, data)) {
  1361. chip = gdev->chip;
  1362. break;
  1363. }
  1364. spin_unlock_irqrestore(&gpio_lock, flags);
  1365. return chip;
  1366. }
  1367. EXPORT_SYMBOL_GPL(gpiochip_find);
  1368. static int gpiochip_match_name(struct gpio_chip *chip, void *data)
  1369. {
  1370. const char *name = data;
  1371. return !strcmp(chip->label, name);
  1372. }
  1373. static struct gpio_chip *find_chip_by_name(const char *name)
  1374. {
  1375. return gpiochip_find((void *)name, gpiochip_match_name);
  1376. }
  1377. #ifdef CONFIG_GPIOLIB_IRQCHIP
  1378. /*
  1379. * The following is irqchip helper code for gpiochips.
  1380. */
  1381. static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
  1382. {
  1383. if (!gpiochip->irq.need_valid_mask)
  1384. return 0;
  1385. gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
  1386. if (!gpiochip->irq.valid_mask)
  1387. return -ENOMEM;
  1388. return 0;
  1389. }
  1390. static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
  1391. {
  1392. kfree(gpiochip->irq.valid_mask);
  1393. gpiochip->irq.valid_mask = NULL;
  1394. }
  1395. bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
  1396. unsigned int offset)
  1397. {
  1398. if (!gpiochip_line_is_valid(gpiochip, offset))
  1399. return false;
  1400. /* No mask means all valid */
  1401. if (likely(!gpiochip->irq.valid_mask))
  1402. return true;
  1403. return test_bit(offset, gpiochip->irq.valid_mask);
  1404. }
  1405. EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
  1406. /**
  1407. * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
  1408. * @gpiochip: the gpiochip to set the irqchip chain to
  1409. * @parent_irq: the irq number corresponding to the parent IRQ for this
  1410. * chained irqchip
  1411. * @parent_handler: the parent interrupt handler for the accumulated IRQ
  1412. * coming out of the gpiochip. If the interrupt is nested rather than
  1413. * cascaded, pass NULL in this handler argument
  1414. */
  1415. static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
  1416. unsigned int parent_irq,
  1417. irq_flow_handler_t parent_handler)
  1418. {
  1419. if (!gpiochip->irq.domain) {
  1420. chip_err(gpiochip, "called %s before setting up irqchip\n",
  1421. __func__);
  1422. return;
  1423. }
  1424. if (parent_handler) {
  1425. if (gpiochip->can_sleep) {
  1426. chip_err(gpiochip,
  1427. "you cannot have chained interrupts on a chip that may sleep\n");
  1428. return;
  1429. }
  1430. /*
  1431. * The parent irqchip is already using the chip_data for this
  1432. * irqchip, so our callbacks simply use the handler_data.
  1433. */
  1434. irq_set_chained_handler_and_data(parent_irq, parent_handler,
  1435. gpiochip);
  1436. gpiochip->irq.parent_irq = parent_irq;
  1437. gpiochip->irq.parents = &gpiochip->irq.parent_irq;
  1438. gpiochip->irq.num_parents = 1;
  1439. }
  1440. }
  1441. /**
  1442. * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
  1443. * @gpiochip: the gpiochip to set the irqchip chain to
  1444. * @irqchip: the irqchip to chain to the gpiochip
  1445. * @parent_irq: the irq number corresponding to the parent IRQ for this
  1446. * chained irqchip
  1447. * @parent_handler: the parent interrupt handler for the accumulated IRQ
  1448. * coming out of the gpiochip.
  1449. */
  1450. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  1451. struct irq_chip *irqchip,
  1452. unsigned int parent_irq,
  1453. irq_flow_handler_t parent_handler)
  1454. {
  1455. if (gpiochip->irq.threaded) {
  1456. chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
  1457. return;
  1458. }
  1459. gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler);
  1460. }
  1461. EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
  1462. /**
  1463. * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
  1464. * @gpiochip: the gpiochip to set the irqchip nested handler to
  1465. * @irqchip: the irqchip to nest to the gpiochip
  1466. * @parent_irq: the irq number corresponding to the parent IRQ for this
  1467. * nested irqchip
  1468. */
  1469. void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
  1470. struct irq_chip *irqchip,
  1471. unsigned int parent_irq)
  1472. {
  1473. gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
  1474. }
  1475. EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
  1476. /**
  1477. * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
  1478. * @d: the irqdomain used by this irqchip
  1479. * @irq: the global irq number used by this GPIO irqchip irq
  1480. * @hwirq: the local IRQ/GPIO line offset on this gpiochip
  1481. *
  1482. * This function will set up the mapping for a certain IRQ line on a
  1483. * gpiochip by assigning the gpiochip as chip data, and using the irqchip
  1484. * stored inside the gpiochip.
  1485. */
  1486. int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  1487. irq_hw_number_t hwirq)
  1488. {
  1489. struct gpio_chip *chip = d->host_data;
  1490. int err = 0;
  1491. if (!gpiochip_irqchip_irq_valid(chip, hwirq))
  1492. return -ENXIO;
  1493. irq_set_chip_data(irq, chip);
  1494. /*
  1495. * This lock class tells lockdep that GPIO irqs are in a different
  1496. * category than their parents, so it won't report false recursion.
  1497. */
  1498. irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
  1499. irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
  1500. /* Chips that use nested thread handlers have them marked */
  1501. if (chip->irq.threaded)
  1502. irq_set_nested_thread(irq, 1);
  1503. irq_set_noprobe(irq);
  1504. if (chip->irq.num_parents == 1)
  1505. err = irq_set_parent(irq, chip->irq.parents[0]);
  1506. else if (chip->irq.map)
  1507. err = irq_set_parent(irq, chip->irq.map[hwirq]);
  1508. if (err < 0)
  1509. return err;
  1510. /*
  1511. * No set-up of the hardware will happen if IRQ_TYPE_NONE
  1512. * is passed as default type.
  1513. */
  1514. if (chip->irq.default_type != IRQ_TYPE_NONE)
  1515. irq_set_irq_type(irq, chip->irq.default_type);
  1516. return 0;
  1517. }
  1518. EXPORT_SYMBOL_GPL(gpiochip_irq_map);
  1519. void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
  1520. {
  1521. struct gpio_chip *chip = d->host_data;
  1522. if (chip->irq.threaded)
  1523. irq_set_nested_thread(irq, 0);
  1524. irq_set_chip_and_handler(irq, NULL, NULL);
  1525. irq_set_chip_data(irq, NULL);
  1526. }
  1527. EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
  1528. static const struct irq_domain_ops gpiochip_domain_ops = {
  1529. .map = gpiochip_irq_map,
  1530. .unmap = gpiochip_irq_unmap,
  1531. /* Virtually all GPIO irqchips are twocell:ed */
  1532. .xlate = irq_domain_xlate_twocell,
  1533. };
  1534. static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
  1535. {
  1536. if (!gpiochip_irqchip_irq_valid(chip, offset))
  1537. return -ENXIO;
  1538. return irq_create_mapping(chip->irq.domain, offset);
  1539. }
  1540. static int gpiochip_irq_reqres(struct irq_data *d)
  1541. {
  1542. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  1543. return gpiochip_reqres_irq(chip, d->hwirq);
  1544. }
  1545. static void gpiochip_irq_relres(struct irq_data *d)
  1546. {
  1547. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  1548. gpiochip_relres_irq(chip, d->hwirq);
  1549. }
  1550. static void gpiochip_irq_enable(struct irq_data *d)
  1551. {
  1552. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  1553. gpiochip_enable_irq(chip, d->hwirq);
  1554. if (chip->irq.irq_enable)
  1555. chip->irq.irq_enable(d);
  1556. else
  1557. chip->irq.chip->irq_unmask(d);
  1558. }
  1559. static void gpiochip_irq_disable(struct irq_data *d)
  1560. {
  1561. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  1562. if (chip->irq.irq_disable)
  1563. chip->irq.irq_disable(d);
  1564. else
  1565. chip->irq.chip->irq_mask(d);
  1566. gpiochip_disable_irq(chip, d->hwirq);
  1567. }
  1568. static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
  1569. {
  1570. struct irq_chip *irqchip = gpiochip->irq.chip;
  1571. if (!irqchip->irq_request_resources &&
  1572. !irqchip->irq_release_resources) {
  1573. irqchip->irq_request_resources = gpiochip_irq_reqres;
  1574. irqchip->irq_release_resources = gpiochip_irq_relres;
  1575. }
  1576. if (WARN_ON(gpiochip->irq.irq_enable))
  1577. return;
  1578. /* Check if the irqchip already has this hook... */
  1579. if (irqchip->irq_enable == gpiochip_irq_enable) {
  1580. /*
  1581. * ...and if so, give a gentle warning that this is bad
  1582. * practice.
  1583. */
  1584. chip_info(gpiochip,
  1585. "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
  1586. return;
  1587. }
  1588. gpiochip->irq.irq_enable = irqchip->irq_enable;
  1589. gpiochip->irq.irq_disable = irqchip->irq_disable;
  1590. irqchip->irq_enable = gpiochip_irq_enable;
  1591. irqchip->irq_disable = gpiochip_irq_disable;
  1592. }
  1593. /**
  1594. * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
  1595. * @gpiochip: the GPIO chip to add the IRQ chip to
  1596. * @lock_key: lockdep class for IRQ lock
  1597. * @request_key: lockdep class for IRQ request
  1598. */
  1599. static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
  1600. struct lock_class_key *lock_key,
  1601. struct lock_class_key *request_key)
  1602. {
  1603. struct irq_chip *irqchip = gpiochip->irq.chip;
  1604. const struct irq_domain_ops *ops;
  1605. struct device_node *np;
  1606. unsigned int type;
  1607. unsigned int i;
  1608. if (!irqchip)
  1609. return 0;
  1610. if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
  1611. chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
  1612. return -EINVAL;
  1613. }
  1614. np = gpiochip->gpiodev->dev.of_node;
  1615. type = gpiochip->irq.default_type;
  1616. /*
  1617. * Specifying a default trigger is a terrible idea if DT or ACPI is
  1618. * used to configure the interrupts, as you may end up with
  1619. * conflicting triggers. Tell the user, and reset to NONE.
  1620. */
  1621. if (WARN(np && type != IRQ_TYPE_NONE,
  1622. "%s: Ignoring %u default trigger\n", np->full_name, type))
  1623. type = IRQ_TYPE_NONE;
  1624. if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
  1625. acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
  1626. "Ignoring %u default trigger\n", type);
  1627. type = IRQ_TYPE_NONE;
  1628. }
  1629. gpiochip->to_irq = gpiochip_to_irq;
  1630. gpiochip->irq.default_type = type;
  1631. gpiochip->irq.lock_key = lock_key;
  1632. gpiochip->irq.request_key = request_key;
  1633. if (gpiochip->irq.domain_ops)
  1634. ops = gpiochip->irq.domain_ops;
  1635. else
  1636. ops = &gpiochip_domain_ops;
  1637. gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
  1638. gpiochip->irq.first,
  1639. ops, gpiochip);
  1640. if (!gpiochip->irq.domain)
  1641. return -EINVAL;
  1642. if (gpiochip->irq.parent_handler) {
  1643. void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
  1644. for (i = 0; i < gpiochip->irq.num_parents; i++) {
  1645. /*
  1646. * The parent IRQ chip is already using the chip_data
  1647. * for this IRQ chip, so our callbacks simply use the
  1648. * handler_data.
  1649. */
  1650. irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
  1651. gpiochip->irq.parent_handler,
  1652. data);
  1653. }
  1654. }
  1655. gpiochip_set_irq_hooks(gpiochip);
  1656. acpi_gpiochip_request_interrupts(gpiochip);
  1657. return 0;
  1658. }
  1659. /**
  1660. * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
  1661. * @gpiochip: the gpiochip to remove the irqchip from
  1662. *
  1663. * This is called only from gpiochip_remove()
  1664. */
  1665. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
  1666. {
  1667. struct irq_chip *irqchip = gpiochip->irq.chip;
  1668. unsigned int offset;
  1669. acpi_gpiochip_free_interrupts(gpiochip);
  1670. if (irqchip && gpiochip->irq.parent_handler) {
  1671. struct gpio_irq_chip *irq = &gpiochip->irq;
  1672. unsigned int i;
  1673. for (i = 0; i < irq->num_parents; i++)
  1674. irq_set_chained_handler_and_data(irq->parents[i],
  1675. NULL, NULL);
  1676. }
  1677. /* Remove all IRQ mappings and delete the domain */
  1678. if (gpiochip->irq.domain) {
  1679. unsigned int irq;
  1680. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  1681. if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
  1682. continue;
  1683. irq = irq_find_mapping(gpiochip->irq.domain, offset);
  1684. irq_dispose_mapping(irq);
  1685. }
  1686. irq_domain_remove(gpiochip->irq.domain);
  1687. }
  1688. if (irqchip) {
  1689. if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
  1690. irqchip->irq_request_resources = NULL;
  1691. irqchip->irq_release_resources = NULL;
  1692. }
  1693. if (irqchip->irq_enable == gpiochip_irq_enable) {
  1694. irqchip->irq_enable = gpiochip->irq.irq_enable;
  1695. irqchip->irq_disable = gpiochip->irq.irq_disable;
  1696. }
  1697. }
  1698. gpiochip->irq.irq_enable = NULL;
  1699. gpiochip->irq.irq_disable = NULL;
  1700. gpiochip->irq.chip = NULL;
  1701. gpiochip_irqchip_free_valid_mask(gpiochip);
  1702. }
  1703. /**
  1704. * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
  1705. * @gpiochip: the gpiochip to add the irqchip to
  1706. * @irqchip: the irqchip to add to the gpiochip
  1707. * @first_irq: if not dynamically assigned, the base (first) IRQ to
  1708. * allocate gpiochip irqs from
  1709. * @handler: the irq handler to use (often a predefined irq core function)
  1710. * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
  1711. * to have the core avoid setting up any default type in the hardware.
  1712. * @threaded: whether this irqchip uses a nested thread handler
  1713. * @lock_key: lockdep class for IRQ lock
  1714. * @request_key: lockdep class for IRQ request
  1715. *
  1716. * This function closely associates a certain irqchip with a certain
  1717. * gpiochip, providing an irq domain to translate the local IRQs to
  1718. * global irqs in the gpiolib core, and making sure that the gpiochip
  1719. * is passed as chip data to all related functions. Driver callbacks
  1720. * need to use gpiochip_get_data() to get their local state containers back
  1721. * from the gpiochip passed as chip data. An irqdomain will be stored
  1722. * in the gpiochip that shall be used by the driver to handle IRQ number
  1723. * translation. The gpiochip will need to be initialized and registered
  1724. * before calling this function.
  1725. *
  1726. * This function will handle two cell:ed simple IRQs and assumes all
  1727. * the pins on the gpiochip can generate a unique IRQ. Everything else
  1728. * need to be open coded.
  1729. */
  1730. int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
  1731. struct irq_chip *irqchip,
  1732. unsigned int first_irq,
  1733. irq_flow_handler_t handler,
  1734. unsigned int type,
  1735. bool threaded,
  1736. struct lock_class_key *lock_key,
  1737. struct lock_class_key *request_key)
  1738. {
  1739. struct device_node *of_node;
  1740. if (!gpiochip || !irqchip)
  1741. return -EINVAL;
  1742. if (!gpiochip->parent) {
  1743. pr_err("missing gpiochip .dev parent pointer\n");
  1744. return -EINVAL;
  1745. }
  1746. gpiochip->irq.threaded = threaded;
  1747. of_node = gpiochip->parent->of_node;
  1748. #ifdef CONFIG_OF_GPIO
  1749. /*
  1750. * If the gpiochip has an assigned OF node this takes precedence
  1751. * FIXME: get rid of this and use gpiochip->parent->of_node
  1752. * everywhere
  1753. */
  1754. if (gpiochip->of_node)
  1755. of_node = gpiochip->of_node;
  1756. #endif
  1757. /*
  1758. * Specifying a default trigger is a terrible idea if DT or ACPI is
  1759. * used to configure the interrupts, as you may end-up with
  1760. * conflicting triggers. Tell the user, and reset to NONE.
  1761. */
  1762. if (WARN(of_node && type != IRQ_TYPE_NONE,
  1763. "%pOF: Ignoring %d default trigger\n", of_node, type))
  1764. type = IRQ_TYPE_NONE;
  1765. if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
  1766. acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
  1767. "Ignoring %d default trigger\n", type);
  1768. type = IRQ_TYPE_NONE;
  1769. }
  1770. gpiochip->irq.chip = irqchip;
  1771. gpiochip->irq.handler = handler;
  1772. gpiochip->irq.default_type = type;
  1773. gpiochip->to_irq = gpiochip_to_irq;
  1774. gpiochip->irq.lock_key = lock_key;
  1775. gpiochip->irq.request_key = request_key;
  1776. gpiochip->irq.domain = irq_domain_add_simple(of_node,
  1777. gpiochip->ngpio, first_irq,
  1778. &gpiochip_domain_ops, gpiochip);
  1779. if (!gpiochip->irq.domain) {
  1780. gpiochip->irq.chip = NULL;
  1781. return -EINVAL;
  1782. }
  1783. gpiochip_set_irq_hooks(gpiochip);
  1784. acpi_gpiochip_request_interrupts(gpiochip);
  1785. return 0;
  1786. }
  1787. EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
  1788. #else /* CONFIG_GPIOLIB_IRQCHIP */
  1789. static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
  1790. struct lock_class_key *lock_key,
  1791. struct lock_class_key *request_key)
  1792. {
  1793. return 0;
  1794. }
  1795. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
  1796. static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
  1797. {
  1798. return 0;
  1799. }
  1800. static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
  1801. { }
  1802. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  1803. /**
  1804. * gpiochip_generic_request() - request the gpio function for a pin
  1805. * @chip: the gpiochip owning the GPIO
  1806. * @offset: the offset of the GPIO to request for GPIO function
  1807. */
  1808. int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
  1809. {
  1810. return pinctrl_gpio_request(chip->gpiodev->base + offset);
  1811. }
  1812. EXPORT_SYMBOL_GPL(gpiochip_generic_request);
  1813. /**
  1814. * gpiochip_generic_free() - free the gpio function from a pin
  1815. * @chip: the gpiochip to request the gpio function for
  1816. * @offset: the offset of the GPIO to free from GPIO function
  1817. */
  1818. void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
  1819. {
  1820. pinctrl_gpio_free(chip->gpiodev->base + offset);
  1821. }
  1822. EXPORT_SYMBOL_GPL(gpiochip_generic_free);
  1823. /**
  1824. * gpiochip_generic_config() - apply configuration for a pin
  1825. * @chip: the gpiochip owning the GPIO
  1826. * @offset: the offset of the GPIO to apply the configuration
  1827. * @config: the configuration to be applied
  1828. */
  1829. int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
  1830. unsigned long config)
  1831. {
  1832. return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
  1833. }
  1834. EXPORT_SYMBOL_GPL(gpiochip_generic_config);
  1835. #ifdef CONFIG_PINCTRL
  1836. /**
  1837. * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
  1838. * @chip: the gpiochip to add the range for
  1839. * @pctldev: the pin controller to map to
  1840. * @gpio_offset: the start offset in the current gpio_chip number space
  1841. * @pin_group: name of the pin group inside the pin controller
  1842. *
  1843. * Calling this function directly from a DeviceTree-supported
  1844. * pinctrl driver is DEPRECATED. Please see Section 2.1 of
  1845. * Documentation/devicetree/bindings/gpio/gpio.txt on how to
  1846. * bind pinctrl and gpio drivers via the "gpio-ranges" property.
  1847. */
  1848. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  1849. struct pinctrl_dev *pctldev,
  1850. unsigned int gpio_offset, const char *pin_group)
  1851. {
  1852. struct gpio_pin_range *pin_range;
  1853. struct gpio_device *gdev = chip->gpiodev;
  1854. int ret;
  1855. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1856. if (!pin_range) {
  1857. chip_err(chip, "failed to allocate pin ranges\n");
  1858. return -ENOMEM;
  1859. }
  1860. /* Use local offset as range ID */
  1861. pin_range->range.id = gpio_offset;
  1862. pin_range->range.gc = chip;
  1863. pin_range->range.name = chip->label;
  1864. pin_range->range.base = gdev->base + gpio_offset;
  1865. pin_range->pctldev = pctldev;
  1866. ret = pinctrl_get_group_pins(pctldev, pin_group,
  1867. &pin_range->range.pins,
  1868. &pin_range->range.npins);
  1869. if (ret < 0) {
  1870. kfree(pin_range);
  1871. return ret;
  1872. }
  1873. pinctrl_add_gpio_range(pctldev, &pin_range->range);
  1874. chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
  1875. gpio_offset, gpio_offset + pin_range->range.npins - 1,
  1876. pinctrl_dev_get_devname(pctldev), pin_group);
  1877. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1878. return 0;
  1879. }
  1880. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  1881. /**
  1882. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  1883. * @chip: the gpiochip to add the range for
  1884. * @pinctl_name: the dev_name() of the pin controller to map to
  1885. * @gpio_offset: the start offset in the current gpio_chip number space
  1886. * @pin_offset: the start offset in the pin controller number space
  1887. * @npins: the number of pins from the offset of each pin space (GPIO and
  1888. * pin controller) to accumulate in this range
  1889. *
  1890. * Returns:
  1891. * 0 on success, or a negative error-code on failure.
  1892. *
  1893. * Calling this function directly from a DeviceTree-supported
  1894. * pinctrl driver is DEPRECATED. Please see Section 2.1 of
  1895. * Documentation/devicetree/bindings/gpio/gpio.txt on how to
  1896. * bind pinctrl and gpio drivers via the "gpio-ranges" property.
  1897. */
  1898. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  1899. unsigned int gpio_offset, unsigned int pin_offset,
  1900. unsigned int npins)
  1901. {
  1902. struct gpio_pin_range *pin_range;
  1903. struct gpio_device *gdev = chip->gpiodev;
  1904. int ret;
  1905. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1906. if (!pin_range) {
  1907. chip_err(chip, "failed to allocate pin ranges\n");
  1908. return -ENOMEM;
  1909. }
  1910. /* Use local offset as range ID */
  1911. pin_range->range.id = gpio_offset;
  1912. pin_range->range.gc = chip;
  1913. pin_range->range.name = chip->label;
  1914. pin_range->range.base = gdev->base + gpio_offset;
  1915. pin_range->range.pin_base = pin_offset;
  1916. pin_range->range.npins = npins;
  1917. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1918. &pin_range->range);
  1919. if (IS_ERR(pin_range->pctldev)) {
  1920. ret = PTR_ERR(pin_range->pctldev);
  1921. chip_err(chip, "could not create pin range\n");
  1922. kfree(pin_range);
  1923. return ret;
  1924. }
  1925. chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1926. gpio_offset, gpio_offset + npins - 1,
  1927. pinctl_name,
  1928. pin_offset, pin_offset + npins - 1);
  1929. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1930. return 0;
  1931. }
  1932. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1933. /**
  1934. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1935. * @chip: the chip to remove all the mappings for
  1936. */
  1937. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  1938. {
  1939. struct gpio_pin_range *pin_range, *tmp;
  1940. struct gpio_device *gdev = chip->gpiodev;
  1941. list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
  1942. list_del(&pin_range->node);
  1943. pinctrl_remove_gpio_range(pin_range->pctldev,
  1944. &pin_range->range);
  1945. kfree(pin_range);
  1946. }
  1947. }
  1948. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1949. #endif /* CONFIG_PINCTRL */
  1950. /* These "optional" allocation calls help prevent drivers from stomping
  1951. * on each other, and help provide better diagnostics in debugfs.
  1952. * They're called even less than the "set direction" calls.
  1953. */
  1954. static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
  1955. {
  1956. struct gpio_chip *chip = desc->gdev->chip;
  1957. int status;
  1958. unsigned long flags;
  1959. unsigned offset;
  1960. spin_lock_irqsave(&gpio_lock, flags);
  1961. /* NOTE: gpio_request() can be called in early boot,
  1962. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1963. */
  1964. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1965. desc_set_label(desc, label ? : "?");
  1966. status = 0;
  1967. } else {
  1968. status = -EBUSY;
  1969. goto done;
  1970. }
  1971. if (chip->request) {
  1972. /* chip->request may sleep */
  1973. spin_unlock_irqrestore(&gpio_lock, flags);
  1974. offset = gpio_chip_hwgpio(desc);
  1975. if (gpiochip_line_is_valid(chip, offset))
  1976. status = chip->request(chip, offset);
  1977. else
  1978. status = -EINVAL;
  1979. spin_lock_irqsave(&gpio_lock, flags);
  1980. if (status < 0) {
  1981. desc_set_label(desc, NULL);
  1982. clear_bit(FLAG_REQUESTED, &desc->flags);
  1983. goto done;
  1984. }
  1985. }
  1986. if (chip->get_direction) {
  1987. /* chip->get_direction may sleep */
  1988. spin_unlock_irqrestore(&gpio_lock, flags);
  1989. gpiod_get_direction(desc);
  1990. spin_lock_irqsave(&gpio_lock, flags);
  1991. }
  1992. done:
  1993. spin_unlock_irqrestore(&gpio_lock, flags);
  1994. return status;
  1995. }
  1996. /*
  1997. * This descriptor validation needs to be inserted verbatim into each
  1998. * function taking a descriptor, so we need to use a preprocessor
  1999. * macro to avoid endless duplication. If the desc is NULL it is an
  2000. * optional GPIO and calls should just bail out.
  2001. */
  2002. static int validate_desc(const struct gpio_desc *desc, const char *func)
  2003. {
  2004. if (!desc)
  2005. return 0;
  2006. if (IS_ERR(desc)) {
  2007. pr_warn("%s: invalid GPIO (errorpointer)\n", func);
  2008. return PTR_ERR(desc);
  2009. }
  2010. if (!desc->gdev) {
  2011. pr_warn("%s: invalid GPIO (no device)\n", func);
  2012. return -EINVAL;
  2013. }
  2014. if (!desc->gdev->chip) {
  2015. dev_warn(&desc->gdev->dev,
  2016. "%s: backing chip is gone\n", func);
  2017. return 0;
  2018. }
  2019. return 1;
  2020. }
  2021. #define VALIDATE_DESC(desc) do { \
  2022. int __valid = validate_desc(desc, __func__); \
  2023. if (__valid <= 0) \
  2024. return __valid; \
  2025. } while (0)
  2026. #define VALIDATE_DESC_VOID(desc) do { \
  2027. int __valid = validate_desc(desc, __func__); \
  2028. if (__valid <= 0) \
  2029. return; \
  2030. } while (0)
  2031. int gpiod_request(struct gpio_desc *desc, const char *label)
  2032. {
  2033. int status = -EPROBE_DEFER;
  2034. struct gpio_device *gdev;
  2035. VALIDATE_DESC(desc);
  2036. gdev = desc->gdev;
  2037. if (try_module_get(gdev->owner)) {
  2038. status = gpiod_request_commit(desc, label);
  2039. if (status < 0)
  2040. module_put(gdev->owner);
  2041. else
  2042. get_device(&gdev->dev);
  2043. }
  2044. if (status)
  2045. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  2046. return status;
  2047. }
  2048. static bool gpiod_free_commit(struct gpio_desc *desc)
  2049. {
  2050. bool ret = false;
  2051. unsigned long flags;
  2052. struct gpio_chip *chip;
  2053. might_sleep();
  2054. gpiod_unexport(desc);
  2055. spin_lock_irqsave(&gpio_lock, flags);
  2056. chip = desc->gdev->chip;
  2057. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  2058. if (chip->free) {
  2059. spin_unlock_irqrestore(&gpio_lock, flags);
  2060. might_sleep_if(chip->can_sleep);
  2061. chip->free(chip, gpio_chip_hwgpio(desc));
  2062. spin_lock_irqsave(&gpio_lock, flags);
  2063. }
  2064. desc_set_label(desc, NULL);
  2065. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2066. clear_bit(FLAG_REQUESTED, &desc->flags);
  2067. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  2068. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  2069. clear_bit(FLAG_IS_HOGGED, &desc->flags);
  2070. ret = true;
  2071. }
  2072. spin_unlock_irqrestore(&gpio_lock, flags);
  2073. return ret;
  2074. }
  2075. void gpiod_free(struct gpio_desc *desc)
  2076. {
  2077. if (desc && desc->gdev && gpiod_free_commit(desc)) {
  2078. module_put(desc->gdev->owner);
  2079. put_device(&desc->gdev->dev);
  2080. } else {
  2081. WARN_ON(extra_checks);
  2082. }
  2083. }
  2084. /**
  2085. * gpiochip_is_requested - return string iff signal was requested
  2086. * @chip: controller managing the signal
  2087. * @offset: of signal within controller's 0..(ngpio - 1) range
  2088. *
  2089. * Returns NULL if the GPIO is not currently requested, else a string.
  2090. * The string returned is the label passed to gpio_request(); if none has been
  2091. * passed it is a meaningless, non-NULL constant.
  2092. *
  2093. * This function is for use by GPIO controller drivers. The label can
  2094. * help with diagnostics, and knowing that the signal is used as a GPIO
  2095. * can help avoid accidentally multiplexing it to another controller.
  2096. */
  2097. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  2098. {
  2099. struct gpio_desc *desc;
  2100. if (offset >= chip->ngpio)
  2101. return NULL;
  2102. desc = &chip->gpiodev->descs[offset];
  2103. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  2104. return NULL;
  2105. return desc->label;
  2106. }
  2107. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  2108. /**
  2109. * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
  2110. * @chip: GPIO chip
  2111. * @hwnum: hardware number of the GPIO for which to request the descriptor
  2112. * @label: label for the GPIO
  2113. *
  2114. * Function allows GPIO chip drivers to request and use their own GPIO
  2115. * descriptors via gpiolib API. Difference to gpiod_request() is that this
  2116. * function will not increase reference count of the GPIO chip module. This
  2117. * allows the GPIO chip module to be unloaded as needed (we assume that the
  2118. * GPIO chip driver handles freeing the GPIOs it has requested).
  2119. *
  2120. * Returns:
  2121. * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
  2122. * code on failure.
  2123. */
  2124. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  2125. const char *label)
  2126. {
  2127. struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
  2128. int err;
  2129. if (IS_ERR(desc)) {
  2130. chip_err(chip, "failed to get GPIO descriptor\n");
  2131. return desc;
  2132. }
  2133. err = gpiod_request_commit(desc, label);
  2134. if (err < 0)
  2135. return ERR_PTR(err);
  2136. return desc;
  2137. }
  2138. EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
  2139. /**
  2140. * gpiochip_free_own_desc - Free GPIO requested by the chip driver
  2141. * @desc: GPIO descriptor to free
  2142. *
  2143. * Function frees the given GPIO requested previously with
  2144. * gpiochip_request_own_desc().
  2145. */
  2146. void gpiochip_free_own_desc(struct gpio_desc *desc)
  2147. {
  2148. if (desc)
  2149. gpiod_free_commit(desc);
  2150. }
  2151. EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
  2152. /*
  2153. * Drivers MUST set GPIO direction before making get/set calls. In
  2154. * some cases this is done in early boot, before IRQs are enabled.
  2155. *
  2156. * As a rule these aren't called more than once (except for drivers
  2157. * using the open-drain emulation idiom) so these are natural places
  2158. * to accumulate extra debugging checks. Note that we can't (yet)
  2159. * rely on gpio_request() having been called beforehand.
  2160. */
  2161. /**
  2162. * gpiod_direction_input - set the GPIO direction to input
  2163. * @desc: GPIO to set to input
  2164. *
  2165. * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
  2166. * be called safely on it.
  2167. *
  2168. * Return 0 in case of success, else an error code.
  2169. */
  2170. int gpiod_direction_input(struct gpio_desc *desc)
  2171. {
  2172. struct gpio_chip *chip;
  2173. int status = 0;
  2174. VALIDATE_DESC(desc);
  2175. chip = desc->gdev->chip;
  2176. /*
  2177. * It is legal to have no .get() and .direction_input() specified if
  2178. * the chip is output-only, but you can't specify .direction_input()
  2179. * and not support the .get() operation, that doesn't make sense.
  2180. */
  2181. if (!chip->get && chip->direction_input) {
  2182. gpiod_warn(desc,
  2183. "%s: missing get() but have direction_input()\n",
  2184. __func__);
  2185. return -EIO;
  2186. }
  2187. /*
  2188. * If we have a .direction_input() callback, things are simple,
  2189. * just call it. Else we are some input-only chip so try to check the
  2190. * direction (if .get_direction() is supported) else we silently
  2191. * assume we are in input mode after this.
  2192. */
  2193. if (chip->direction_input) {
  2194. status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
  2195. } else if (chip->get_direction &&
  2196. (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
  2197. gpiod_warn(desc,
  2198. "%s: missing direction_input() operation and line is output\n",
  2199. __func__);
  2200. return -EIO;
  2201. }
  2202. if (status == 0)
  2203. clear_bit(FLAG_IS_OUT, &desc->flags);
  2204. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  2205. return status;
  2206. }
  2207. EXPORT_SYMBOL_GPL(gpiod_direction_input);
  2208. static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
  2209. enum pin_config_param mode)
  2210. {
  2211. unsigned long config = { PIN_CONF_PACKED(mode, 0) };
  2212. return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
  2213. }
  2214. static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
  2215. {
  2216. struct gpio_chip *gc = desc->gdev->chip;
  2217. int val = !!value;
  2218. int ret = 0;
  2219. /*
  2220. * It's OK not to specify .direction_output() if the gpiochip is
  2221. * output-only, but if there is then not even a .set() operation it
  2222. * is pretty tricky to drive the output line.
  2223. */
  2224. if (!gc->set && !gc->direction_output) {
  2225. gpiod_warn(desc,
  2226. "%s: missing set() and direction_output() operations\n",
  2227. __func__);
  2228. return -EIO;
  2229. }
  2230. if (gc->direction_output) {
  2231. ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
  2232. } else {
  2233. /* Check that we are in output mode if we can */
  2234. if (gc->get_direction &&
  2235. gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
  2236. gpiod_warn(desc,
  2237. "%s: missing direction_output() operation\n",
  2238. __func__);
  2239. return -EIO;
  2240. }
  2241. /*
  2242. * If we can't actively set the direction, we are some
  2243. * output-only chip, so just drive the output as desired.
  2244. */
  2245. gc->set(gc, gpio_chip_hwgpio(desc), val);
  2246. }
  2247. if (!ret)
  2248. set_bit(FLAG_IS_OUT, &desc->flags);
  2249. trace_gpio_value(desc_to_gpio(desc), 0, val);
  2250. trace_gpio_direction(desc_to_gpio(desc), 0, ret);
  2251. return ret;
  2252. }
  2253. /**
  2254. * gpiod_direction_output_raw - set the GPIO direction to output
  2255. * @desc: GPIO to set to output
  2256. * @value: initial output value of the GPIO
  2257. *
  2258. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  2259. * be called safely on it. The initial value of the output must be specified
  2260. * as raw value on the physical line without regard for the ACTIVE_LOW status.
  2261. *
  2262. * Return 0 in case of success, else an error code.
  2263. */
  2264. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  2265. {
  2266. VALIDATE_DESC(desc);
  2267. return gpiod_direction_output_raw_commit(desc, value);
  2268. }
  2269. EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
  2270. /**
  2271. * gpiod_direction_output - set the GPIO direction to output
  2272. * @desc: GPIO to set to output
  2273. * @value: initial output value of the GPIO
  2274. *
  2275. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  2276. * be called safely on it. The initial value of the output must be specified
  2277. * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  2278. * account.
  2279. *
  2280. * Return 0 in case of success, else an error code.
  2281. */
  2282. int gpiod_direction_output(struct gpio_desc *desc, int value)
  2283. {
  2284. struct gpio_chip *gc;
  2285. int ret;
  2286. VALIDATE_DESC(desc);
  2287. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2288. value = !value;
  2289. else
  2290. value = !!value;
  2291. /* GPIOs used for enabled IRQs shall not be set as output */
  2292. if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
  2293. test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
  2294. gpiod_err(desc,
  2295. "%s: tried to set a GPIO tied to an IRQ as output\n",
  2296. __func__);
  2297. return -EIO;
  2298. }
  2299. gc = desc->gdev->chip;
  2300. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
  2301. /* First see if we can enable open drain in hardware */
  2302. ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
  2303. PIN_CONFIG_DRIVE_OPEN_DRAIN);
  2304. if (!ret)
  2305. goto set_output_value;
  2306. /* Emulate open drain by not actively driving the line high */
  2307. if (value)
  2308. return gpiod_direction_input(desc);
  2309. }
  2310. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
  2311. ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
  2312. PIN_CONFIG_DRIVE_OPEN_SOURCE);
  2313. if (!ret)
  2314. goto set_output_value;
  2315. /* Emulate open source by not actively driving the line low */
  2316. if (!value)
  2317. return gpiod_direction_input(desc);
  2318. } else {
  2319. gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
  2320. PIN_CONFIG_DRIVE_PUSH_PULL);
  2321. }
  2322. set_output_value:
  2323. return gpiod_direction_output_raw_commit(desc, value);
  2324. }
  2325. EXPORT_SYMBOL_GPL(gpiod_direction_output);
  2326. /**
  2327. * gpiod_set_debounce - sets @debounce time for a GPIO
  2328. * @desc: descriptor of the GPIO for which to set debounce time
  2329. * @debounce: debounce time in microseconds
  2330. *
  2331. * Returns:
  2332. * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
  2333. * debounce time.
  2334. */
  2335. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  2336. {
  2337. struct gpio_chip *chip;
  2338. unsigned long config;
  2339. VALIDATE_DESC(desc);
  2340. chip = desc->gdev->chip;
  2341. if (!chip->set || !chip->set_config) {
  2342. gpiod_dbg(desc,
  2343. "%s: missing set() or set_config() operations\n",
  2344. __func__);
  2345. return -ENOTSUPP;
  2346. }
  2347. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
  2348. return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
  2349. }
  2350. EXPORT_SYMBOL_GPL(gpiod_set_debounce);
  2351. /**
  2352. * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
  2353. * @desc: descriptor of the GPIO for which to configure persistence
  2354. * @transitory: True to lose state on suspend or reset, false for persistence
  2355. *
  2356. * Returns:
  2357. * 0 on success, otherwise a negative error code.
  2358. */
  2359. int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
  2360. {
  2361. struct gpio_chip *chip;
  2362. unsigned long packed;
  2363. int gpio;
  2364. int rc;
  2365. VALIDATE_DESC(desc);
  2366. /*
  2367. * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
  2368. * persistence state.
  2369. */
  2370. if (transitory)
  2371. set_bit(FLAG_TRANSITORY, &desc->flags);
  2372. else
  2373. clear_bit(FLAG_TRANSITORY, &desc->flags);
  2374. /* If the driver supports it, set the persistence state now */
  2375. chip = desc->gdev->chip;
  2376. if (!chip->set_config)
  2377. return 0;
  2378. packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
  2379. !transitory);
  2380. gpio = gpio_chip_hwgpio(desc);
  2381. rc = chip->set_config(chip, gpio, packed);
  2382. if (rc == -ENOTSUPP) {
  2383. dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
  2384. gpio);
  2385. return 0;
  2386. }
  2387. return rc;
  2388. }
  2389. EXPORT_SYMBOL_GPL(gpiod_set_transitory);
  2390. /**
  2391. * gpiod_is_active_low - test whether a GPIO is active-low or not
  2392. * @desc: the gpio descriptor to test
  2393. *
  2394. * Returns 1 if the GPIO is active-low, 0 otherwise.
  2395. */
  2396. int gpiod_is_active_low(const struct gpio_desc *desc)
  2397. {
  2398. VALIDATE_DESC(desc);
  2399. return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2400. }
  2401. EXPORT_SYMBOL_GPL(gpiod_is_active_low);
  2402. /* I/O calls are only valid after configuration completed; the relevant
  2403. * "is this a valid GPIO" error checks should already have been done.
  2404. *
  2405. * "Get" operations are often inlinable as reading a pin value register,
  2406. * and masking the relevant bit in that register.
  2407. *
  2408. * When "set" operations are inlinable, they involve writing that mask to
  2409. * one register to set a low value, or a different register to set it high.
  2410. * Otherwise locking is needed, so there may be little value to inlining.
  2411. *
  2412. *------------------------------------------------------------------------
  2413. *
  2414. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  2415. * have requested the GPIO. That can include implicit requesting by
  2416. * a direction setting call. Marking a gpio as requested locks its chip
  2417. * in memory, guaranteeing that these table lookups need no more locking
  2418. * and that gpiochip_remove() will fail.
  2419. *
  2420. * REVISIT when debugging, consider adding some instrumentation to ensure
  2421. * that the GPIO was actually requested.
  2422. */
  2423. static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
  2424. {
  2425. struct gpio_chip *chip;
  2426. int offset;
  2427. int value;
  2428. chip = desc->gdev->chip;
  2429. offset = gpio_chip_hwgpio(desc);
  2430. value = chip->get ? chip->get(chip, offset) : -EIO;
  2431. value = value < 0 ? value : !!value;
  2432. trace_gpio_value(desc_to_gpio(desc), 1, value);
  2433. return value;
  2434. }
  2435. static int gpio_chip_get_multiple(struct gpio_chip *chip,
  2436. unsigned long *mask, unsigned long *bits)
  2437. {
  2438. if (chip->get_multiple) {
  2439. return chip->get_multiple(chip, mask, bits);
  2440. } else if (chip->get) {
  2441. int i, value;
  2442. for_each_set_bit(i, mask, chip->ngpio) {
  2443. value = chip->get(chip, i);
  2444. if (value < 0)
  2445. return value;
  2446. __assign_bit(i, bits, value);
  2447. }
  2448. return 0;
  2449. }
  2450. return -EIO;
  2451. }
  2452. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  2453. unsigned int array_size,
  2454. struct gpio_desc **desc_array,
  2455. struct gpio_array *array_info,
  2456. unsigned long *value_bitmap)
  2457. {
  2458. int err, i = 0;
  2459. /*
  2460. * Validate array_info against desc_array and its size.
  2461. * It should immediately follow desc_array if both
  2462. * have been obtained from the same gpiod_get_array() call.
  2463. */
  2464. if (array_info && array_info->desc == desc_array &&
  2465. array_size <= array_info->size &&
  2466. (void *)array_info == desc_array + array_info->size) {
  2467. if (!can_sleep)
  2468. WARN_ON(array_info->chip->can_sleep);
  2469. err = gpio_chip_get_multiple(array_info->chip,
  2470. array_info->get_mask,
  2471. value_bitmap);
  2472. if (err)
  2473. return err;
  2474. if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
  2475. bitmap_xor(value_bitmap, value_bitmap,
  2476. array_info->invert_mask, array_size);
  2477. if (bitmap_full(array_info->get_mask, array_size))
  2478. return 0;
  2479. i = find_first_zero_bit(array_info->get_mask, array_size);
  2480. } else {
  2481. array_info = NULL;
  2482. }
  2483. while (i < array_size) {
  2484. struct gpio_chip *chip = desc_array[i]->gdev->chip;
  2485. unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
  2486. unsigned long *mask, *bits;
  2487. int first, j, ret;
  2488. if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
  2489. mask = fastpath;
  2490. } else {
  2491. mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
  2492. sizeof(*mask),
  2493. can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  2494. if (!mask)
  2495. return -ENOMEM;
  2496. }
  2497. bits = mask + BITS_TO_LONGS(chip->ngpio);
  2498. bitmap_zero(mask, chip->ngpio);
  2499. if (!can_sleep)
  2500. WARN_ON(chip->can_sleep);
  2501. /* collect all inputs belonging to the same chip */
  2502. first = i;
  2503. do {
  2504. const struct gpio_desc *desc = desc_array[i];
  2505. int hwgpio = gpio_chip_hwgpio(desc);
  2506. __set_bit(hwgpio, mask);
  2507. i++;
  2508. if (array_info)
  2509. i = find_next_zero_bit(array_info->get_mask,
  2510. array_size, i);
  2511. } while ((i < array_size) &&
  2512. (desc_array[i]->gdev->chip == chip));
  2513. ret = gpio_chip_get_multiple(chip, mask, bits);
  2514. if (ret) {
  2515. if (mask != fastpath)
  2516. kfree(mask);
  2517. return ret;
  2518. }
  2519. for (j = first; j < i; ) {
  2520. const struct gpio_desc *desc = desc_array[j];
  2521. int hwgpio = gpio_chip_hwgpio(desc);
  2522. int value = test_bit(hwgpio, bits);
  2523. if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2524. value = !value;
  2525. __assign_bit(j, value_bitmap, value);
  2526. trace_gpio_value(desc_to_gpio(desc), 1, value);
  2527. j++;
  2528. if (array_info)
  2529. j = find_next_zero_bit(array_info->get_mask, i,
  2530. j);
  2531. }
  2532. if (mask != fastpath)
  2533. kfree(mask);
  2534. }
  2535. return 0;
  2536. }
  2537. /**
  2538. * gpiod_get_raw_value() - return a gpio's raw value
  2539. * @desc: gpio whose value will be returned
  2540. *
  2541. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  2542. * its ACTIVE_LOW status, or negative errno on failure.
  2543. *
  2544. * This function should be called from contexts where we cannot sleep, and will
  2545. * complain if the GPIO chip functions potentially sleep.
  2546. */
  2547. int gpiod_get_raw_value(const struct gpio_desc *desc)
  2548. {
  2549. VALIDATE_DESC(desc);
  2550. /* Should be using gpio_get_value_cansleep() */
  2551. WARN_ON(desc->gdev->chip->can_sleep);
  2552. return gpiod_get_raw_value_commit(desc);
  2553. }
  2554. EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
  2555. /**
  2556. * gpiod_get_value() - return a gpio's value
  2557. * @desc: gpio whose value will be returned
  2558. *
  2559. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  2560. * account, or negative errno on failure.
  2561. *
  2562. * This function should be called from contexts where we cannot sleep, and will
  2563. * complain if the GPIO chip functions potentially sleep.
  2564. */
  2565. int gpiod_get_value(const struct gpio_desc *desc)
  2566. {
  2567. int value;
  2568. VALIDATE_DESC(desc);
  2569. /* Should be using gpio_get_value_cansleep() */
  2570. WARN_ON(desc->gdev->chip->can_sleep);
  2571. value = gpiod_get_raw_value_commit(desc);
  2572. if (value < 0)
  2573. return value;
  2574. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2575. value = !value;
  2576. return value;
  2577. }
  2578. EXPORT_SYMBOL_GPL(gpiod_get_value);
  2579. /**
  2580. * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
  2581. * @array_size: number of elements in the descriptor array / value bitmap
  2582. * @desc_array: array of GPIO descriptors whose values will be read
  2583. * @array_info: information on applicability of fast bitmap processing path
  2584. * @value_bitmap: bitmap to store the read values
  2585. *
  2586. * Read the raw values of the GPIOs, i.e. the values of the physical lines
  2587. * without regard for their ACTIVE_LOW status. Return 0 in case of success,
  2588. * else an error code.
  2589. *
  2590. * This function should be called from contexts where we cannot sleep,
  2591. * and it will complain if the GPIO chip functions potentially sleep.
  2592. */
  2593. int gpiod_get_raw_array_value(unsigned int array_size,
  2594. struct gpio_desc **desc_array,
  2595. struct gpio_array *array_info,
  2596. unsigned long *value_bitmap)
  2597. {
  2598. if (!desc_array)
  2599. return -EINVAL;
  2600. return gpiod_get_array_value_complex(true, false, array_size,
  2601. desc_array, array_info,
  2602. value_bitmap);
  2603. }
  2604. EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
  2605. /**
  2606. * gpiod_get_array_value() - read values from an array of GPIOs
  2607. * @array_size: number of elements in the descriptor array / value bitmap
  2608. * @desc_array: array of GPIO descriptors whose values will be read
  2609. * @array_info: information on applicability of fast bitmap processing path
  2610. * @value_bitmap: bitmap to store the read values
  2611. *
  2612. * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2613. * into account. Return 0 in case of success, else an error code.
  2614. *
  2615. * This function should be called from contexts where we cannot sleep,
  2616. * and it will complain if the GPIO chip functions potentially sleep.
  2617. */
  2618. int gpiod_get_array_value(unsigned int array_size,
  2619. struct gpio_desc **desc_array,
  2620. struct gpio_array *array_info,
  2621. unsigned long *value_bitmap)
  2622. {
  2623. if (!desc_array)
  2624. return -EINVAL;
  2625. return gpiod_get_array_value_complex(false, false, array_size,
  2626. desc_array, array_info,
  2627. value_bitmap);
  2628. }
  2629. EXPORT_SYMBOL_GPL(gpiod_get_array_value);
  2630. /*
  2631. * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
  2632. * @desc: gpio descriptor whose state need to be set.
  2633. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  2634. */
  2635. static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
  2636. {
  2637. int err = 0;
  2638. struct gpio_chip *chip = desc->gdev->chip;
  2639. int offset = gpio_chip_hwgpio(desc);
  2640. if (value) {
  2641. err = chip->direction_input(chip, offset);
  2642. if (!err)
  2643. clear_bit(FLAG_IS_OUT, &desc->flags);
  2644. } else {
  2645. err = chip->direction_output(chip, offset, 0);
  2646. if (!err)
  2647. set_bit(FLAG_IS_OUT, &desc->flags);
  2648. }
  2649. trace_gpio_direction(desc_to_gpio(desc), value, err);
  2650. if (err < 0)
  2651. gpiod_err(desc,
  2652. "%s: Error in set_value for open drain err %d\n",
  2653. __func__, err);
  2654. }
  2655. /*
  2656. * _gpio_set_open_source_value() - Set the open source gpio's value.
  2657. * @desc: gpio descriptor whose state need to be set.
  2658. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  2659. */
  2660. static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
  2661. {
  2662. int err = 0;
  2663. struct gpio_chip *chip = desc->gdev->chip;
  2664. int offset = gpio_chip_hwgpio(desc);
  2665. if (value) {
  2666. err = chip->direction_output(chip, offset, 1);
  2667. if (!err)
  2668. set_bit(FLAG_IS_OUT, &desc->flags);
  2669. } else {
  2670. err = chip->direction_input(chip, offset);
  2671. if (!err)
  2672. clear_bit(FLAG_IS_OUT, &desc->flags);
  2673. }
  2674. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  2675. if (err < 0)
  2676. gpiod_err(desc,
  2677. "%s: Error in set_value for open source err %d\n",
  2678. __func__, err);
  2679. }
  2680. static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
  2681. {
  2682. struct gpio_chip *chip;
  2683. chip = desc->gdev->chip;
  2684. trace_gpio_value(desc_to_gpio(desc), 0, value);
  2685. chip->set(chip, gpio_chip_hwgpio(desc), value);
  2686. }
  2687. /*
  2688. * set multiple outputs on the same chip;
  2689. * use the chip's set_multiple function if available;
  2690. * otherwise set the outputs sequentially;
  2691. * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
  2692. * defines which outputs are to be changed
  2693. * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
  2694. * defines the values the outputs specified by mask are to be set to
  2695. */
  2696. static void gpio_chip_set_multiple(struct gpio_chip *chip,
  2697. unsigned long *mask, unsigned long *bits)
  2698. {
  2699. if (chip->set_multiple) {
  2700. chip->set_multiple(chip, mask, bits);
  2701. } else {
  2702. unsigned int i;
  2703. /* set outputs if the corresponding mask bit is set */
  2704. for_each_set_bit(i, mask, chip->ngpio)
  2705. chip->set(chip, i, test_bit(i, bits));
  2706. }
  2707. }
  2708. int gpiod_set_array_value_complex(bool raw, bool can_sleep,
  2709. unsigned int array_size,
  2710. struct gpio_desc **desc_array,
  2711. struct gpio_array *array_info,
  2712. unsigned long *value_bitmap)
  2713. {
  2714. int i = 0;
  2715. /*
  2716. * Validate array_info against desc_array and its size.
  2717. * It should immediately follow desc_array if both
  2718. * have been obtained from the same gpiod_get_array() call.
  2719. */
  2720. if (array_info && array_info->desc == desc_array &&
  2721. array_size <= array_info->size &&
  2722. (void *)array_info == desc_array + array_info->size) {
  2723. if (!can_sleep)
  2724. WARN_ON(array_info->chip->can_sleep);
  2725. if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
  2726. bitmap_xor(value_bitmap, value_bitmap,
  2727. array_info->invert_mask, array_size);
  2728. gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
  2729. value_bitmap);
  2730. if (bitmap_full(array_info->set_mask, array_size))
  2731. return 0;
  2732. i = find_first_zero_bit(array_info->set_mask, array_size);
  2733. } else {
  2734. array_info = NULL;
  2735. }
  2736. while (i < array_size) {
  2737. struct gpio_chip *chip = desc_array[i]->gdev->chip;
  2738. unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
  2739. unsigned long *mask, *bits;
  2740. int count = 0;
  2741. if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
  2742. mask = fastpath;
  2743. } else {
  2744. mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
  2745. sizeof(*mask),
  2746. can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  2747. if (!mask)
  2748. return -ENOMEM;
  2749. }
  2750. bits = mask + BITS_TO_LONGS(chip->ngpio);
  2751. bitmap_zero(mask, chip->ngpio);
  2752. if (!can_sleep)
  2753. WARN_ON(chip->can_sleep);
  2754. do {
  2755. struct gpio_desc *desc = desc_array[i];
  2756. int hwgpio = gpio_chip_hwgpio(desc);
  2757. int value = test_bit(i, value_bitmap);
  2758. /*
  2759. * Pins applicable for fast input but not for
  2760. * fast output processing may have been already
  2761. * inverted inside the fast path, skip them.
  2762. */
  2763. if (!raw && !(array_info &&
  2764. test_bit(i, array_info->invert_mask)) &&
  2765. test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2766. value = !value;
  2767. trace_gpio_value(desc_to_gpio(desc), 0, value);
  2768. /*
  2769. * collect all normal outputs belonging to the same chip
  2770. * open drain and open source outputs are set individually
  2771. */
  2772. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
  2773. gpio_set_open_drain_value_commit(desc, value);
  2774. } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
  2775. gpio_set_open_source_value_commit(desc, value);
  2776. } else {
  2777. __set_bit(hwgpio, mask);
  2778. if (value)
  2779. __set_bit(hwgpio, bits);
  2780. else
  2781. __clear_bit(hwgpio, bits);
  2782. count++;
  2783. }
  2784. i++;
  2785. if (array_info)
  2786. i = find_next_zero_bit(array_info->set_mask,
  2787. array_size, i);
  2788. } while ((i < array_size) &&
  2789. (desc_array[i]->gdev->chip == chip));
  2790. /* push collected bits to outputs */
  2791. if (count != 0)
  2792. gpio_chip_set_multiple(chip, mask, bits);
  2793. if (mask != fastpath)
  2794. kfree(mask);
  2795. }
  2796. return 0;
  2797. }
  2798. /**
  2799. * gpiod_set_raw_value() - assign a gpio's raw value
  2800. * @desc: gpio whose value will be assigned
  2801. * @value: value to assign
  2802. *
  2803. * Set the raw value of the GPIO, i.e. the value of its physical line without
  2804. * regard for its ACTIVE_LOW status.
  2805. *
  2806. * This function should be called from contexts where we cannot sleep, and will
  2807. * complain if the GPIO chip functions potentially sleep.
  2808. */
  2809. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  2810. {
  2811. VALIDATE_DESC_VOID(desc);
  2812. /* Should be using gpiod_set_value_cansleep() */
  2813. WARN_ON(desc->gdev->chip->can_sleep);
  2814. gpiod_set_raw_value_commit(desc, value);
  2815. }
  2816. EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
  2817. /**
  2818. * gpiod_set_value_nocheck() - set a GPIO line value without checking
  2819. * @desc: the descriptor to set the value on
  2820. * @value: value to set
  2821. *
  2822. * This sets the value of a GPIO line backing a descriptor, applying
  2823. * different semantic quirks like active low and open drain/source
  2824. * handling.
  2825. */
  2826. static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
  2827. {
  2828. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2829. value = !value;
  2830. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  2831. gpio_set_open_drain_value_commit(desc, value);
  2832. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  2833. gpio_set_open_source_value_commit(desc, value);
  2834. else
  2835. gpiod_set_raw_value_commit(desc, value);
  2836. }
  2837. /**
  2838. * gpiod_set_value() - assign a gpio's value
  2839. * @desc: gpio whose value will be assigned
  2840. * @value: value to assign
  2841. *
  2842. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
  2843. * OPEN_DRAIN and OPEN_SOURCE flags into account.
  2844. *
  2845. * This function should be called from contexts where we cannot sleep, and will
  2846. * complain if the GPIO chip functions potentially sleep.
  2847. */
  2848. void gpiod_set_value(struct gpio_desc *desc, int value)
  2849. {
  2850. VALIDATE_DESC_VOID(desc);
  2851. WARN_ON(desc->gdev->chip->can_sleep);
  2852. gpiod_set_value_nocheck(desc, value);
  2853. }
  2854. EXPORT_SYMBOL_GPL(gpiod_set_value);
  2855. /**
  2856. * gpiod_set_raw_array_value() - assign values to an array of GPIOs
  2857. * @array_size: number of elements in the descriptor array / value bitmap
  2858. * @desc_array: array of GPIO descriptors whose values will be assigned
  2859. * @array_info: information on applicability of fast bitmap processing path
  2860. * @value_bitmap: bitmap of values to assign
  2861. *
  2862. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  2863. * without regard for their ACTIVE_LOW status.
  2864. *
  2865. * This function should be called from contexts where we cannot sleep, and will
  2866. * complain if the GPIO chip functions potentially sleep.
  2867. */
  2868. int gpiod_set_raw_array_value(unsigned int array_size,
  2869. struct gpio_desc **desc_array,
  2870. struct gpio_array *array_info,
  2871. unsigned long *value_bitmap)
  2872. {
  2873. if (!desc_array)
  2874. return -EINVAL;
  2875. return gpiod_set_array_value_complex(true, false, array_size,
  2876. desc_array, array_info, value_bitmap);
  2877. }
  2878. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
  2879. /**
  2880. * gpiod_set_array_value() - assign values to an array of GPIOs
  2881. * @array_size: number of elements in the descriptor array / value bitmap
  2882. * @desc_array: array of GPIO descriptors whose values will be assigned
  2883. * @array_info: information on applicability of fast bitmap processing path
  2884. * @value_bitmap: bitmap of values to assign
  2885. *
  2886. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2887. * into account.
  2888. *
  2889. * This function should be called from contexts where we cannot sleep, and will
  2890. * complain if the GPIO chip functions potentially sleep.
  2891. */
  2892. int gpiod_set_array_value(unsigned int array_size,
  2893. struct gpio_desc **desc_array,
  2894. struct gpio_array *array_info,
  2895. unsigned long *value_bitmap)
  2896. {
  2897. if (!desc_array)
  2898. return -EINVAL;
  2899. return gpiod_set_array_value_complex(false, false, array_size,
  2900. desc_array, array_info,
  2901. value_bitmap);
  2902. }
  2903. EXPORT_SYMBOL_GPL(gpiod_set_array_value);
  2904. /**
  2905. * gpiod_cansleep() - report whether gpio value access may sleep
  2906. * @desc: gpio to check
  2907. *
  2908. */
  2909. int gpiod_cansleep(const struct gpio_desc *desc)
  2910. {
  2911. VALIDATE_DESC(desc);
  2912. return desc->gdev->chip->can_sleep;
  2913. }
  2914. EXPORT_SYMBOL_GPL(gpiod_cansleep);
  2915. /**
  2916. * gpiod_set_consumer_name() - set the consumer name for the descriptor
  2917. * @desc: gpio to set the consumer name on
  2918. * @name: the new consumer name
  2919. */
  2920. void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
  2921. {
  2922. VALIDATE_DESC_VOID(desc);
  2923. /* Just overwrite whatever the previous name was */
  2924. desc->label = name;
  2925. }
  2926. EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
  2927. /**
  2928. * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  2929. * @desc: gpio whose IRQ will be returned (already requested)
  2930. *
  2931. * Return the IRQ corresponding to the passed GPIO, or an error code in case of
  2932. * error.
  2933. */
  2934. int gpiod_to_irq(const struct gpio_desc *desc)
  2935. {
  2936. struct gpio_chip *chip;
  2937. int offset;
  2938. /*
  2939. * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
  2940. * requires this function to not return zero on an invalid descriptor
  2941. * but rather a negative error number.
  2942. */
  2943. if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
  2944. return -EINVAL;
  2945. chip = desc->gdev->chip;
  2946. offset = gpio_chip_hwgpio(desc);
  2947. if (chip->to_irq) {
  2948. int retirq = chip->to_irq(chip, offset);
  2949. /* Zero means NO_IRQ */
  2950. if (!retirq)
  2951. return -ENXIO;
  2952. return retirq;
  2953. }
  2954. return -ENXIO;
  2955. }
  2956. EXPORT_SYMBOL_GPL(gpiod_to_irq);
  2957. /**
  2958. * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
  2959. * @chip: the chip the GPIO to lock belongs to
  2960. * @offset: the offset of the GPIO to lock as IRQ
  2961. *
  2962. * This is used directly by GPIO drivers that want to lock down
  2963. * a certain GPIO line to be used for IRQs.
  2964. */
  2965. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
  2966. {
  2967. struct gpio_desc *desc;
  2968. desc = gpiochip_get_desc(chip, offset);
  2969. if (IS_ERR(desc))
  2970. return PTR_ERR(desc);
  2971. /*
  2972. * If it's fast: flush the direction setting if something changed
  2973. * behind our back
  2974. */
  2975. if (!chip->can_sleep && chip->get_direction) {
  2976. int dir = gpiod_get_direction(desc);
  2977. if (dir < 0) {
  2978. chip_err(chip, "%s: cannot get GPIO direction\n",
  2979. __func__);
  2980. return dir;
  2981. }
  2982. }
  2983. if (test_bit(FLAG_IS_OUT, &desc->flags)) {
  2984. chip_err(chip,
  2985. "%s: tried to flag a GPIO set as output for IRQ\n",
  2986. __func__);
  2987. return -EIO;
  2988. }
  2989. set_bit(FLAG_USED_AS_IRQ, &desc->flags);
  2990. set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
  2991. /*
  2992. * If the consumer has not set up a label (such as when the
  2993. * IRQ is referenced from .to_irq()) we set up a label here
  2994. * so it is clear this is used as an interrupt.
  2995. */
  2996. if (!desc->label)
  2997. desc_set_label(desc, "interrupt");
  2998. return 0;
  2999. }
  3000. EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
  3001. /**
  3002. * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
  3003. * @chip: the chip the GPIO to lock belongs to
  3004. * @offset: the offset of the GPIO to lock as IRQ
  3005. *
  3006. * This is used directly by GPIO drivers that want to indicate
  3007. * that a certain GPIO is no longer used exclusively for IRQ.
  3008. */
  3009. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
  3010. {
  3011. struct gpio_desc *desc;
  3012. desc = gpiochip_get_desc(chip, offset);
  3013. if (IS_ERR(desc))
  3014. return;
  3015. clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
  3016. clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
  3017. /* If we only had this marking, erase it */
  3018. if (desc->label && !strcmp(desc->label, "interrupt"))
  3019. desc_set_label(desc, NULL);
  3020. }
  3021. EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
  3022. void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
  3023. {
  3024. struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
  3025. if (!IS_ERR(desc) &&
  3026. !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
  3027. clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
  3028. }
  3029. EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
  3030. void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
  3031. {
  3032. struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
  3033. if (!IS_ERR(desc) &&
  3034. !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
  3035. WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
  3036. set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
  3037. }
  3038. }
  3039. EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
  3040. bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
  3041. {
  3042. if (offset >= chip->ngpio)
  3043. return false;
  3044. return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
  3045. }
  3046. EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
  3047. int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
  3048. {
  3049. int ret;
  3050. if (!try_module_get(chip->gpiodev->owner))
  3051. return -ENODEV;
  3052. ret = gpiochip_lock_as_irq(chip, offset);
  3053. if (ret) {
  3054. chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
  3055. module_put(chip->gpiodev->owner);
  3056. return ret;
  3057. }
  3058. return 0;
  3059. }
  3060. EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
  3061. void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
  3062. {
  3063. gpiochip_unlock_as_irq(chip, offset);
  3064. module_put(chip->gpiodev->owner);
  3065. }
  3066. EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
  3067. bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
  3068. {
  3069. if (offset >= chip->ngpio)
  3070. return false;
  3071. return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
  3072. }
  3073. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
  3074. bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
  3075. {
  3076. if (offset >= chip->ngpio)
  3077. return false;
  3078. return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
  3079. }
  3080. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
  3081. bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
  3082. {
  3083. if (offset >= chip->ngpio)
  3084. return false;
  3085. return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
  3086. }
  3087. EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
  3088. /**
  3089. * gpiod_get_raw_value_cansleep() - return a gpio's raw value
  3090. * @desc: gpio whose value will be returned
  3091. *
  3092. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  3093. * its ACTIVE_LOW status, or negative errno on failure.
  3094. *
  3095. * This function is to be called from contexts that can sleep.
  3096. */
  3097. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  3098. {
  3099. might_sleep_if(extra_checks);
  3100. VALIDATE_DESC(desc);
  3101. return gpiod_get_raw_value_commit(desc);
  3102. }
  3103. EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
  3104. /**
  3105. * gpiod_get_value_cansleep() - return a gpio's value
  3106. * @desc: gpio whose value will be returned
  3107. *
  3108. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  3109. * account, or negative errno on failure.
  3110. *
  3111. * This function is to be called from contexts that can sleep.
  3112. */
  3113. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  3114. {
  3115. int value;
  3116. might_sleep_if(extra_checks);
  3117. VALIDATE_DESC(desc);
  3118. value = gpiod_get_raw_value_commit(desc);
  3119. if (value < 0)
  3120. return value;
  3121. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  3122. value = !value;
  3123. return value;
  3124. }
  3125. EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
  3126. /**
  3127. * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
  3128. * @array_size: number of elements in the descriptor array / value bitmap
  3129. * @desc_array: array of GPIO descriptors whose values will be read
  3130. * @array_info: information on applicability of fast bitmap processing path
  3131. * @value_bitmap: bitmap to store the read values
  3132. *
  3133. * Read the raw values of the GPIOs, i.e. the values of the physical lines
  3134. * without regard for their ACTIVE_LOW status. Return 0 in case of success,
  3135. * else an error code.
  3136. *
  3137. * This function is to be called from contexts that can sleep.
  3138. */
  3139. int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
  3140. struct gpio_desc **desc_array,
  3141. struct gpio_array *array_info,
  3142. unsigned long *value_bitmap)
  3143. {
  3144. might_sleep_if(extra_checks);
  3145. if (!desc_array)
  3146. return -EINVAL;
  3147. return gpiod_get_array_value_complex(true, true, array_size,
  3148. desc_array, array_info,
  3149. value_bitmap);
  3150. }
  3151. EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
  3152. /**
  3153. * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
  3154. * @array_size: number of elements in the descriptor array / value bitmap
  3155. * @desc_array: array of GPIO descriptors whose values will be read
  3156. * @array_info: information on applicability of fast bitmap processing path
  3157. * @value_bitmap: bitmap to store the read values
  3158. *
  3159. * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  3160. * into account. Return 0 in case of success, else an error code.
  3161. *
  3162. * This function is to be called from contexts that can sleep.
  3163. */
  3164. int gpiod_get_array_value_cansleep(unsigned int array_size,
  3165. struct gpio_desc **desc_array,
  3166. struct gpio_array *array_info,
  3167. unsigned long *value_bitmap)
  3168. {
  3169. might_sleep_if(extra_checks);
  3170. if (!desc_array)
  3171. return -EINVAL;
  3172. return gpiod_get_array_value_complex(false, true, array_size,
  3173. desc_array, array_info,
  3174. value_bitmap);
  3175. }
  3176. EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
  3177. /**
  3178. * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
  3179. * @desc: gpio whose value will be assigned
  3180. * @value: value to assign
  3181. *
  3182. * Set the raw value of the GPIO, i.e. the value of its physical line without
  3183. * regard for its ACTIVE_LOW status.
  3184. *
  3185. * This function is to be called from contexts that can sleep.
  3186. */
  3187. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  3188. {
  3189. might_sleep_if(extra_checks);
  3190. VALIDATE_DESC_VOID(desc);
  3191. gpiod_set_raw_value_commit(desc, value);
  3192. }
  3193. EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
  3194. /**
  3195. * gpiod_set_value_cansleep() - assign a gpio's value
  3196. * @desc: gpio whose value will be assigned
  3197. * @value: value to assign
  3198. *
  3199. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  3200. * account
  3201. *
  3202. * This function is to be called from contexts that can sleep.
  3203. */
  3204. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  3205. {
  3206. might_sleep_if(extra_checks);
  3207. VALIDATE_DESC_VOID(desc);
  3208. gpiod_set_value_nocheck(desc, value);
  3209. }
  3210. EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
  3211. /**
  3212. * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
  3213. * @array_size: number of elements in the descriptor array / value bitmap
  3214. * @desc_array: array of GPIO descriptors whose values will be assigned
  3215. * @array_info: information on applicability of fast bitmap processing path
  3216. * @value_bitmap: bitmap of values to assign
  3217. *
  3218. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  3219. * without regard for their ACTIVE_LOW status.
  3220. *
  3221. * This function is to be called from contexts that can sleep.
  3222. */
  3223. int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  3224. struct gpio_desc **desc_array,
  3225. struct gpio_array *array_info,
  3226. unsigned long *value_bitmap)
  3227. {
  3228. might_sleep_if(extra_checks);
  3229. if (!desc_array)
  3230. return -EINVAL;
  3231. return gpiod_set_array_value_complex(true, true, array_size, desc_array,
  3232. array_info, value_bitmap);
  3233. }
  3234. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
  3235. /**
  3236. * gpiod_add_lookup_tables() - register GPIO device consumers
  3237. * @tables: list of tables of consumers to register
  3238. * @n: number of tables in the list
  3239. */
  3240. void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
  3241. {
  3242. unsigned int i;
  3243. mutex_lock(&gpio_lookup_lock);
  3244. for (i = 0; i < n; i++)
  3245. list_add_tail(&tables[i]->list, &gpio_lookup_list);
  3246. mutex_unlock(&gpio_lookup_lock);
  3247. }
  3248. /**
  3249. * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
  3250. * @array_size: number of elements in the descriptor array / value bitmap
  3251. * @desc_array: array of GPIO descriptors whose values will be assigned
  3252. * @array_info: information on applicability of fast bitmap processing path
  3253. * @value_bitmap: bitmap of values to assign
  3254. *
  3255. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  3256. * into account.
  3257. *
  3258. * This function is to be called from contexts that can sleep.
  3259. */
  3260. int gpiod_set_array_value_cansleep(unsigned int array_size,
  3261. struct gpio_desc **desc_array,
  3262. struct gpio_array *array_info,
  3263. unsigned long *value_bitmap)
  3264. {
  3265. might_sleep_if(extra_checks);
  3266. if (!desc_array)
  3267. return -EINVAL;
  3268. return gpiod_set_array_value_complex(false, true, array_size,
  3269. desc_array, array_info,
  3270. value_bitmap);
  3271. }
  3272. EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
  3273. /**
  3274. * gpiod_add_lookup_table() - register GPIO device consumers
  3275. * @table: table of consumers to register
  3276. */
  3277. void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
  3278. {
  3279. mutex_lock(&gpio_lookup_lock);
  3280. list_add_tail(&table->list, &gpio_lookup_list);
  3281. mutex_unlock(&gpio_lookup_lock);
  3282. }
  3283. EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
  3284. /**
  3285. * gpiod_remove_lookup_table() - unregister GPIO device consumers
  3286. * @table: table of consumers to unregister
  3287. */
  3288. void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
  3289. {
  3290. mutex_lock(&gpio_lookup_lock);
  3291. list_del(&table->list);
  3292. mutex_unlock(&gpio_lookup_lock);
  3293. }
  3294. EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
  3295. /**
  3296. * gpiod_add_hogs() - register a set of GPIO hogs from machine code
  3297. * @hogs: table of gpio hog entries with a zeroed sentinel at the end
  3298. */
  3299. void gpiod_add_hogs(struct gpiod_hog *hogs)
  3300. {
  3301. struct gpio_chip *chip;
  3302. struct gpiod_hog *hog;
  3303. mutex_lock(&gpio_machine_hogs_mutex);
  3304. for (hog = &hogs[0]; hog->chip_label; hog++) {
  3305. list_add_tail(&hog->list, &gpio_machine_hogs);
  3306. /*
  3307. * The chip may have been registered earlier, so check if it
  3308. * exists and, if so, try to hog the line now.
  3309. */
  3310. chip = find_chip_by_name(hog->chip_label);
  3311. if (chip)
  3312. gpiochip_machine_hog(chip, hog);
  3313. }
  3314. mutex_unlock(&gpio_machine_hogs_mutex);
  3315. }
  3316. EXPORT_SYMBOL_GPL(gpiod_add_hogs);
  3317. static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
  3318. {
  3319. const char *dev_id = dev ? dev_name(dev) : NULL;
  3320. struct gpiod_lookup_table *table;
  3321. mutex_lock(&gpio_lookup_lock);
  3322. list_for_each_entry(table, &gpio_lookup_list, list) {
  3323. if (table->dev_id && dev_id) {
  3324. /*
  3325. * Valid strings on both ends, must be identical to have
  3326. * a match
  3327. */
  3328. if (!strcmp(table->dev_id, dev_id))
  3329. goto found;
  3330. } else {
  3331. /*
  3332. * One of the pointers is NULL, so both must be to have
  3333. * a match
  3334. */
  3335. if (dev_id == table->dev_id)
  3336. goto found;
  3337. }
  3338. }
  3339. table = NULL;
  3340. found:
  3341. mutex_unlock(&gpio_lookup_lock);
  3342. return table;
  3343. }
  3344. static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
  3345. unsigned int idx,
  3346. enum gpio_lookup_flags *flags)
  3347. {
  3348. struct gpio_desc *desc = ERR_PTR(-ENOENT);
  3349. struct gpiod_lookup_table *table;
  3350. struct gpiod_lookup *p;
  3351. table = gpiod_find_lookup_table(dev);
  3352. if (!table)
  3353. return desc;
  3354. for (p = &table->table[0]; p->chip_label; p++) {
  3355. struct gpio_chip *chip;
  3356. /* idx must always match exactly */
  3357. if (p->idx != idx)
  3358. continue;
  3359. /* If the lookup entry has a con_id, require exact match */
  3360. if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
  3361. continue;
  3362. chip = find_chip_by_name(p->chip_label);
  3363. if (!chip) {
  3364. /*
  3365. * As the lookup table indicates a chip with
  3366. * p->chip_label should exist, assume it may
  3367. * still appear later and let the interested
  3368. * consumer be probed again or let the Deferred
  3369. * Probe infrastructure handle the error.
  3370. */
  3371. dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
  3372. p->chip_label);
  3373. return ERR_PTR(-EPROBE_DEFER);
  3374. }
  3375. if (chip->ngpio <= p->chip_hwnum) {
  3376. dev_err(dev,
  3377. "requested GPIO %d is out of range [0..%d] for chip %s\n",
  3378. idx, chip->ngpio, chip->label);
  3379. return ERR_PTR(-EINVAL);
  3380. }
  3381. desc = gpiochip_get_desc(chip, p->chip_hwnum);
  3382. *flags = p->flags;
  3383. return desc;
  3384. }
  3385. return desc;
  3386. }
  3387. static int dt_gpio_count(struct device *dev, const char *con_id)
  3388. {
  3389. int ret;
  3390. char propname[32];
  3391. unsigned int i;
  3392. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  3393. if (con_id)
  3394. snprintf(propname, sizeof(propname), "%s-%s",
  3395. con_id, gpio_suffixes[i]);
  3396. else
  3397. snprintf(propname, sizeof(propname), "%s",
  3398. gpio_suffixes[i]);
  3399. ret = of_gpio_named_count(dev->of_node, propname);
  3400. if (ret > 0)
  3401. break;
  3402. }
  3403. return ret ? ret : -ENOENT;
  3404. }
  3405. static int platform_gpio_count(struct device *dev, const char *con_id)
  3406. {
  3407. struct gpiod_lookup_table *table;
  3408. struct gpiod_lookup *p;
  3409. unsigned int count = 0;
  3410. table = gpiod_find_lookup_table(dev);
  3411. if (!table)
  3412. return -ENOENT;
  3413. for (p = &table->table[0]; p->chip_label; p++) {
  3414. if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
  3415. (!con_id && !p->con_id))
  3416. count++;
  3417. }
  3418. if (!count)
  3419. return -ENOENT;
  3420. return count;
  3421. }
  3422. /**
  3423. * gpiod_count - return the number of GPIOs associated with a device / function
  3424. * or -ENOENT if no GPIO has been assigned to the requested function
  3425. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3426. * @con_id: function within the GPIO consumer
  3427. */
  3428. int gpiod_count(struct device *dev, const char *con_id)
  3429. {
  3430. int count = -ENOENT;
  3431. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  3432. count = dt_gpio_count(dev, con_id);
  3433. else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
  3434. count = acpi_gpio_count(dev, con_id);
  3435. if (count < 0)
  3436. count = platform_gpio_count(dev, con_id);
  3437. return count;
  3438. }
  3439. EXPORT_SYMBOL_GPL(gpiod_count);
  3440. /**
  3441. * gpiod_get - obtain a GPIO for a given GPIO function
  3442. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3443. * @con_id: function within the GPIO consumer
  3444. * @flags: optional GPIO initialization flags
  3445. *
  3446. * Return the GPIO descriptor corresponding to the function con_id of device
  3447. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  3448. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  3449. */
  3450. struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
  3451. enum gpiod_flags flags)
  3452. {
  3453. return gpiod_get_index(dev, con_id, 0, flags);
  3454. }
  3455. EXPORT_SYMBOL_GPL(gpiod_get);
  3456. /**
  3457. * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
  3458. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3459. * @con_id: function within the GPIO consumer
  3460. * @flags: optional GPIO initialization flags
  3461. *
  3462. * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
  3463. * the requested function it will return NULL. This is convenient for drivers
  3464. * that need to handle optional GPIOs.
  3465. */
  3466. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  3467. const char *con_id,
  3468. enum gpiod_flags flags)
  3469. {
  3470. return gpiod_get_index_optional(dev, con_id, 0, flags);
  3471. }
  3472. EXPORT_SYMBOL_GPL(gpiod_get_optional);
  3473. /**
  3474. * gpiod_configure_flags - helper function to configure a given GPIO
  3475. * @desc: gpio whose value will be assigned
  3476. * @con_id: function within the GPIO consumer
  3477. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  3478. * of_get_gpio_hog()
  3479. * @dflags: gpiod_flags - optional GPIO initialization flags
  3480. *
  3481. * Return 0 on success, -ENOENT if no GPIO has been assigned to the
  3482. * requested function and/or index, or another IS_ERR() code if an error
  3483. * occurred while trying to acquire the GPIO.
  3484. */
  3485. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  3486. unsigned long lflags, enum gpiod_flags dflags)
  3487. {
  3488. int status;
  3489. if (lflags & GPIO_ACTIVE_LOW)
  3490. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  3491. if (lflags & GPIO_OPEN_DRAIN)
  3492. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  3493. else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
  3494. /*
  3495. * This enforces open drain mode from the consumer side.
  3496. * This is necessary for some busses like I2C, but the lookup
  3497. * should *REALLY* have specified them as open drain in the
  3498. * first place, so print a little warning here.
  3499. */
  3500. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  3501. gpiod_warn(desc,
  3502. "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
  3503. }
  3504. if (lflags & GPIO_OPEN_SOURCE)
  3505. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  3506. status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
  3507. if (status < 0)
  3508. return status;
  3509. /* No particular flag request, return here... */
  3510. if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
  3511. pr_debug("no flags found for %s\n", con_id);
  3512. return 0;
  3513. }
  3514. /* Process flags */
  3515. if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
  3516. status = gpiod_direction_output(desc,
  3517. !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
  3518. else
  3519. status = gpiod_direction_input(desc);
  3520. return status;
  3521. }
  3522. /**
  3523. * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
  3524. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3525. * @con_id: function within the GPIO consumer
  3526. * @idx: index of the GPIO to obtain in the consumer
  3527. * @flags: optional GPIO initialization flags
  3528. *
  3529. * This variant of gpiod_get() allows to access GPIOs other than the first
  3530. * defined one for functions that define several GPIOs.
  3531. *
  3532. * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
  3533. * requested function and/or index, or another IS_ERR() code if an error
  3534. * occurred while trying to acquire the GPIO.
  3535. */
  3536. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  3537. const char *con_id,
  3538. unsigned int idx,
  3539. enum gpiod_flags flags)
  3540. {
  3541. struct gpio_desc *desc = NULL;
  3542. int status;
  3543. enum gpio_lookup_flags lookupflags = 0;
  3544. /* Maybe we have a device name, maybe not */
  3545. const char *devname = dev ? dev_name(dev) : "?";
  3546. dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
  3547. if (dev) {
  3548. /* Using device tree? */
  3549. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  3550. dev_dbg(dev, "using device tree for GPIO lookup\n");
  3551. desc = of_find_gpio(dev, con_id, idx, &lookupflags);
  3552. } else if (ACPI_COMPANION(dev)) {
  3553. dev_dbg(dev, "using ACPI for GPIO lookup\n");
  3554. desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
  3555. }
  3556. }
  3557. /*
  3558. * Either we are not using DT or ACPI, or their lookup did not return
  3559. * a result. In that case, use platform lookup as a fallback.
  3560. */
  3561. if (!desc || desc == ERR_PTR(-ENOENT)) {
  3562. dev_dbg(dev, "using lookup tables for GPIO lookup\n");
  3563. desc = gpiod_find(dev, con_id, idx, &lookupflags);
  3564. }
  3565. if (IS_ERR(desc)) {
  3566. dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
  3567. return desc;
  3568. }
  3569. /*
  3570. * If a connection label was passed use that, else attempt to use
  3571. * the device name as label
  3572. */
  3573. status = gpiod_request(desc, con_id ? con_id : devname);
  3574. if (status < 0) {
  3575. if (status == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
  3576. /*
  3577. * This happens when there are several consumers for
  3578. * the same GPIO line: we just return here without
  3579. * further initialization. It is a bit if a hack.
  3580. * This is necessary to support fixed regulators.
  3581. *
  3582. * FIXME: Make this more sane and safe.
  3583. */
  3584. dev_info(dev, "nonexclusive access to GPIO for %s\n",
  3585. con_id ? con_id : devname);
  3586. return desc;
  3587. } else {
  3588. return ERR_PTR(status);
  3589. }
  3590. }
  3591. status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
  3592. if (status < 0) {
  3593. dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
  3594. gpiod_put(desc);
  3595. return ERR_PTR(status);
  3596. }
  3597. return desc;
  3598. }
  3599. EXPORT_SYMBOL_GPL(gpiod_get_index);
  3600. /**
  3601. * gpiod_get_from_of_node() - obtain a GPIO from an OF node
  3602. * @node: handle of the OF node
  3603. * @propname: name of the DT property representing the GPIO
  3604. * @index: index of the GPIO to obtain for the consumer
  3605. * @dflags: GPIO initialization flags
  3606. * @label: label to attach to the requested GPIO
  3607. *
  3608. * Returns:
  3609. * On successful request the GPIO pin is configured in accordance with
  3610. * provided @dflags. If the node does not have the requested GPIO
  3611. * property, NULL is returned.
  3612. *
  3613. * In case of error an ERR_PTR() is returned.
  3614. */
  3615. struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
  3616. const char *propname, int index,
  3617. enum gpiod_flags dflags,
  3618. const char *label)
  3619. {
  3620. struct gpio_desc *desc;
  3621. unsigned long lflags = 0;
  3622. enum of_gpio_flags flags;
  3623. bool active_low = false;
  3624. bool single_ended = false;
  3625. bool open_drain = false;
  3626. bool transitory = false;
  3627. int ret;
  3628. desc = of_get_named_gpiod_flags(node, propname,
  3629. index, &flags);
  3630. if (!desc || IS_ERR(desc)) {
  3631. /* If it is not there, just return NULL */
  3632. if (PTR_ERR(desc) == -ENOENT)
  3633. return NULL;
  3634. return desc;
  3635. }
  3636. active_low = flags & OF_GPIO_ACTIVE_LOW;
  3637. single_ended = flags & OF_GPIO_SINGLE_ENDED;
  3638. open_drain = flags & OF_GPIO_OPEN_DRAIN;
  3639. transitory = flags & OF_GPIO_TRANSITORY;
  3640. ret = gpiod_request(desc, label);
  3641. if (ret)
  3642. return ERR_PTR(ret);
  3643. if (active_low)
  3644. lflags |= GPIO_ACTIVE_LOW;
  3645. if (single_ended) {
  3646. if (open_drain)
  3647. lflags |= GPIO_OPEN_DRAIN;
  3648. else
  3649. lflags |= GPIO_OPEN_SOURCE;
  3650. }
  3651. if (transitory)
  3652. lflags |= GPIO_TRANSITORY;
  3653. ret = gpiod_configure_flags(desc, propname, lflags, dflags);
  3654. if (ret < 0) {
  3655. gpiod_put(desc);
  3656. return ERR_PTR(ret);
  3657. }
  3658. return desc;
  3659. }
  3660. EXPORT_SYMBOL(gpiod_get_from_of_node);
  3661. /**
  3662. * fwnode_get_named_gpiod - obtain a GPIO from firmware node
  3663. * @fwnode: handle of the firmware node
  3664. * @propname: name of the firmware property representing the GPIO
  3665. * @index: index of the GPIO to obtain for the consumer
  3666. * @dflags: GPIO initialization flags
  3667. * @label: label to attach to the requested GPIO
  3668. *
  3669. * This function can be used for drivers that get their configuration
  3670. * from opaque firmware.
  3671. *
  3672. * The function properly finds the corresponding GPIO using whatever is the
  3673. * underlying firmware interface and then makes sure that the GPIO
  3674. * descriptor is requested before it is returned to the caller.
  3675. *
  3676. * Returns:
  3677. * On successful request the GPIO pin is configured in accordance with
  3678. * provided @dflags.
  3679. *
  3680. * In case of error an ERR_PTR() is returned.
  3681. */
  3682. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  3683. const char *propname, int index,
  3684. enum gpiod_flags dflags,
  3685. const char *label)
  3686. {
  3687. struct gpio_desc *desc = ERR_PTR(-ENODEV);
  3688. unsigned long lflags = 0;
  3689. int ret;
  3690. if (!fwnode)
  3691. return ERR_PTR(-EINVAL);
  3692. if (is_of_node(fwnode)) {
  3693. desc = gpiod_get_from_of_node(to_of_node(fwnode),
  3694. propname, index,
  3695. dflags,
  3696. label);
  3697. return desc;
  3698. } else if (is_acpi_node(fwnode)) {
  3699. struct acpi_gpio_info info;
  3700. desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
  3701. if (IS_ERR(desc))
  3702. return desc;
  3703. acpi_gpio_update_gpiod_flags(&dflags, &info);
  3704. if (info.polarity == GPIO_ACTIVE_LOW)
  3705. lflags |= GPIO_ACTIVE_LOW;
  3706. }
  3707. /* Currently only ACPI takes this path */
  3708. ret = gpiod_request(desc, label);
  3709. if (ret)
  3710. return ERR_PTR(ret);
  3711. ret = gpiod_configure_flags(desc, propname, lflags, dflags);
  3712. if (ret < 0) {
  3713. gpiod_put(desc);
  3714. return ERR_PTR(ret);
  3715. }
  3716. return desc;
  3717. }
  3718. EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
  3719. /**
  3720. * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
  3721. * function
  3722. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3723. * @con_id: function within the GPIO consumer
  3724. * @index: index of the GPIO to obtain in the consumer
  3725. * @flags: optional GPIO initialization flags
  3726. *
  3727. * This is equivalent to gpiod_get_index(), except that when no GPIO with the
  3728. * specified index was assigned to the requested function it will return NULL.
  3729. * This is convenient for drivers that need to handle optional GPIOs.
  3730. */
  3731. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  3732. const char *con_id,
  3733. unsigned int index,
  3734. enum gpiod_flags flags)
  3735. {
  3736. struct gpio_desc *desc;
  3737. desc = gpiod_get_index(dev, con_id, index, flags);
  3738. if (IS_ERR(desc)) {
  3739. if (PTR_ERR(desc) == -ENOENT)
  3740. return NULL;
  3741. }
  3742. return desc;
  3743. }
  3744. EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
  3745. /**
  3746. * gpiod_hog - Hog the specified GPIO desc given the provided flags
  3747. * @desc: gpio whose value will be assigned
  3748. * @name: gpio line name
  3749. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  3750. * of_get_gpio_hog()
  3751. * @dflags: gpiod_flags - optional GPIO initialization flags
  3752. */
  3753. int gpiod_hog(struct gpio_desc *desc, const char *name,
  3754. unsigned long lflags, enum gpiod_flags dflags)
  3755. {
  3756. struct gpio_chip *chip;
  3757. struct gpio_desc *local_desc;
  3758. int hwnum;
  3759. int status;
  3760. chip = gpiod_to_chip(desc);
  3761. hwnum = gpio_chip_hwgpio(desc);
  3762. local_desc = gpiochip_request_own_desc(chip, hwnum, name);
  3763. if (IS_ERR(local_desc)) {
  3764. status = PTR_ERR(local_desc);
  3765. pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
  3766. name, chip->label, hwnum, status);
  3767. return status;
  3768. }
  3769. status = gpiod_configure_flags(desc, name, lflags, dflags);
  3770. if (status < 0) {
  3771. pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
  3772. name, chip->label, hwnum, status);
  3773. gpiochip_free_own_desc(desc);
  3774. return status;
  3775. }
  3776. /* Mark GPIO as hogged so it can be identified and removed later */
  3777. set_bit(FLAG_IS_HOGGED, &desc->flags);
  3778. pr_info("GPIO line %d (%s) hogged as %s%s\n",
  3779. desc_to_gpio(desc), name,
  3780. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
  3781. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
  3782. (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
  3783. return 0;
  3784. }
  3785. /**
  3786. * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
  3787. * @chip: gpio chip to act on
  3788. *
  3789. * This is only used by of_gpiochip_remove to free hogged gpios
  3790. */
  3791. static void gpiochip_free_hogs(struct gpio_chip *chip)
  3792. {
  3793. int id;
  3794. for (id = 0; id < chip->ngpio; id++) {
  3795. if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
  3796. gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
  3797. }
  3798. }
  3799. /**
  3800. * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
  3801. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3802. * @con_id: function within the GPIO consumer
  3803. * @flags: optional GPIO initialization flags
  3804. *
  3805. * This function acquires all the GPIOs defined under a given function.
  3806. *
  3807. * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
  3808. * no GPIO has been assigned to the requested function, or another IS_ERR()
  3809. * code if an error occurred while trying to acquire the GPIOs.
  3810. */
  3811. struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
  3812. const char *con_id,
  3813. enum gpiod_flags flags)
  3814. {
  3815. struct gpio_desc *desc;
  3816. struct gpio_descs *descs;
  3817. struct gpio_array *array_info = NULL;
  3818. struct gpio_chip *chip;
  3819. int count, bitmap_size;
  3820. count = gpiod_count(dev, con_id);
  3821. if (count < 0)
  3822. return ERR_PTR(count);
  3823. descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
  3824. if (!descs)
  3825. return ERR_PTR(-ENOMEM);
  3826. for (descs->ndescs = 0; descs->ndescs < count; ) {
  3827. desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
  3828. if (IS_ERR(desc)) {
  3829. gpiod_put_array(descs);
  3830. return ERR_CAST(desc);
  3831. }
  3832. descs->desc[descs->ndescs] = desc;
  3833. chip = gpiod_to_chip(desc);
  3834. /*
  3835. * If pin hardware number of array member 0 is also 0, select
  3836. * its chip as a candidate for fast bitmap processing path.
  3837. */
  3838. if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
  3839. struct gpio_descs *array;
  3840. bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
  3841. chip->ngpio : count);
  3842. array = kzalloc(struct_size(descs, desc, count) +
  3843. struct_size(array_info, invert_mask,
  3844. 3 * bitmap_size), GFP_KERNEL);
  3845. if (!array) {
  3846. gpiod_put_array(descs);
  3847. return ERR_PTR(-ENOMEM);
  3848. }
  3849. memcpy(array, descs,
  3850. struct_size(descs, desc, descs->ndescs + 1));
  3851. kfree(descs);
  3852. descs = array;
  3853. array_info = (void *)(descs->desc + count);
  3854. array_info->get_mask = array_info->invert_mask +
  3855. bitmap_size;
  3856. array_info->set_mask = array_info->get_mask +
  3857. bitmap_size;
  3858. array_info->desc = descs->desc;
  3859. array_info->size = count;
  3860. array_info->chip = chip;
  3861. bitmap_set(array_info->get_mask, descs->ndescs,
  3862. count - descs->ndescs);
  3863. bitmap_set(array_info->set_mask, descs->ndescs,
  3864. count - descs->ndescs);
  3865. descs->info = array_info;
  3866. }
  3867. /* Unmark array members which don't belong to the 'fast' chip */
  3868. if (array_info && array_info->chip != chip) {
  3869. __clear_bit(descs->ndescs, array_info->get_mask);
  3870. __clear_bit(descs->ndescs, array_info->set_mask);
  3871. }
  3872. /*
  3873. * Detect array members which belong to the 'fast' chip
  3874. * but their pins are not in hardware order.
  3875. */
  3876. else if (array_info &&
  3877. gpio_chip_hwgpio(desc) != descs->ndescs) {
  3878. /*
  3879. * Don't use fast path if all array members processed so
  3880. * far belong to the same chip as this one but its pin
  3881. * hardware number is different from its array index.
  3882. */
  3883. if (bitmap_full(array_info->get_mask, descs->ndescs)) {
  3884. array_info = NULL;
  3885. } else {
  3886. __clear_bit(descs->ndescs,
  3887. array_info->get_mask);
  3888. __clear_bit(descs->ndescs,
  3889. array_info->set_mask);
  3890. }
  3891. } else if (array_info) {
  3892. /* Exclude open drain or open source from fast output */
  3893. if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
  3894. gpiochip_line_is_open_source(chip, descs->ndescs))
  3895. __clear_bit(descs->ndescs,
  3896. array_info->set_mask);
  3897. /* Identify 'fast' pins which require invertion */
  3898. if (gpiod_is_active_low(desc))
  3899. __set_bit(descs->ndescs,
  3900. array_info->invert_mask);
  3901. }
  3902. descs->ndescs++;
  3903. }
  3904. if (array_info)
  3905. dev_dbg(dev,
  3906. "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
  3907. array_info->chip->label, array_info->size,
  3908. *array_info->get_mask, *array_info->set_mask,
  3909. *array_info->invert_mask);
  3910. return descs;
  3911. }
  3912. EXPORT_SYMBOL_GPL(gpiod_get_array);
  3913. /**
  3914. * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
  3915. * function
  3916. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3917. * @con_id: function within the GPIO consumer
  3918. * @flags: optional GPIO initialization flags
  3919. *
  3920. * This is equivalent to gpiod_get_array(), except that when no GPIO was
  3921. * assigned to the requested function it will return NULL.
  3922. */
  3923. struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
  3924. const char *con_id,
  3925. enum gpiod_flags flags)
  3926. {
  3927. struct gpio_descs *descs;
  3928. descs = gpiod_get_array(dev, con_id, flags);
  3929. if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
  3930. return NULL;
  3931. return descs;
  3932. }
  3933. EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
  3934. /**
  3935. * gpiod_put - dispose of a GPIO descriptor
  3936. * @desc: GPIO descriptor to dispose of
  3937. *
  3938. * No descriptor can be used after gpiod_put() has been called on it.
  3939. */
  3940. void gpiod_put(struct gpio_desc *desc)
  3941. {
  3942. gpiod_free(desc);
  3943. }
  3944. EXPORT_SYMBOL_GPL(gpiod_put);
  3945. /**
  3946. * gpiod_put_array - dispose of multiple GPIO descriptors
  3947. * @descs: struct gpio_descs containing an array of descriptors
  3948. */
  3949. void gpiod_put_array(struct gpio_descs *descs)
  3950. {
  3951. unsigned int i;
  3952. for (i = 0; i < descs->ndescs; i++)
  3953. gpiod_put(descs->desc[i]);
  3954. kfree(descs);
  3955. }
  3956. EXPORT_SYMBOL_GPL(gpiod_put_array);
  3957. static int __init gpiolib_dev_init(void)
  3958. {
  3959. int ret;
  3960. /* Register GPIO sysfs bus */
  3961. ret = bus_register(&gpio_bus_type);
  3962. if (ret < 0) {
  3963. pr_err("gpiolib: could not register GPIO bus type\n");
  3964. return ret;
  3965. }
  3966. ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
  3967. if (ret < 0) {
  3968. pr_err("gpiolib: failed to allocate char dev region\n");
  3969. bus_unregister(&gpio_bus_type);
  3970. } else {
  3971. gpiolib_initialized = true;
  3972. gpiochip_setup_devs();
  3973. }
  3974. return ret;
  3975. }
  3976. core_initcall(gpiolib_dev_init);
  3977. #ifdef CONFIG_DEBUG_FS
  3978. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
  3979. {
  3980. unsigned i;
  3981. struct gpio_chip *chip = gdev->chip;
  3982. unsigned gpio = gdev->base;
  3983. struct gpio_desc *gdesc = &gdev->descs[0];
  3984. bool is_out;
  3985. bool is_irq;
  3986. bool active_low;
  3987. for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
  3988. if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
  3989. if (gdesc->name) {
  3990. seq_printf(s, " gpio-%-3d (%-20.20s)\n",
  3991. gpio, gdesc->name);
  3992. }
  3993. continue;
  3994. }
  3995. gpiod_get_direction(gdesc);
  3996. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  3997. is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
  3998. active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
  3999. seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
  4000. gpio, gdesc->name ? gdesc->name : "", gdesc->label,
  4001. is_out ? "out" : "in ",
  4002. chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
  4003. is_irq ? "IRQ " : "",
  4004. active_low ? "ACTIVE LOW" : "");
  4005. seq_printf(s, "\n");
  4006. }
  4007. }
  4008. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  4009. {
  4010. unsigned long flags;
  4011. struct gpio_device *gdev = NULL;
  4012. loff_t index = *pos;
  4013. s->private = "";
  4014. spin_lock_irqsave(&gpio_lock, flags);
  4015. list_for_each_entry(gdev, &gpio_devices, list)
  4016. if (index-- == 0) {
  4017. spin_unlock_irqrestore(&gpio_lock, flags);
  4018. return gdev;
  4019. }
  4020. spin_unlock_irqrestore(&gpio_lock, flags);
  4021. return NULL;
  4022. }
  4023. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  4024. {
  4025. unsigned long flags;
  4026. struct gpio_device *gdev = v;
  4027. void *ret = NULL;
  4028. spin_lock_irqsave(&gpio_lock, flags);
  4029. if (list_is_last(&gdev->list, &gpio_devices))
  4030. ret = NULL;
  4031. else
  4032. ret = list_entry(gdev->list.next, struct gpio_device, list);
  4033. spin_unlock_irqrestore(&gpio_lock, flags);
  4034. s->private = "\n";
  4035. ++*pos;
  4036. return ret;
  4037. }
  4038. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  4039. {
  4040. }
  4041. static int gpiolib_seq_show(struct seq_file *s, void *v)
  4042. {
  4043. struct gpio_device *gdev = v;
  4044. struct gpio_chip *chip = gdev->chip;
  4045. struct device *parent;
  4046. if (!chip) {
  4047. seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
  4048. dev_name(&gdev->dev));
  4049. return 0;
  4050. }
  4051. seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
  4052. dev_name(&gdev->dev),
  4053. gdev->base, gdev->base + gdev->ngpio - 1);
  4054. parent = chip->parent;
  4055. if (parent)
  4056. seq_printf(s, ", parent: %s/%s",
  4057. parent->bus ? parent->bus->name : "no-bus",
  4058. dev_name(parent));
  4059. if (chip->label)
  4060. seq_printf(s, ", %s", chip->label);
  4061. if (chip->can_sleep)
  4062. seq_printf(s, ", can sleep");
  4063. seq_printf(s, ":\n");
  4064. if (chip->dbg_show)
  4065. chip->dbg_show(s, chip);
  4066. else
  4067. gpiolib_dbg_show(s, gdev);
  4068. return 0;
  4069. }
  4070. static const struct seq_operations gpiolib_seq_ops = {
  4071. .start = gpiolib_seq_start,
  4072. .next = gpiolib_seq_next,
  4073. .stop = gpiolib_seq_stop,
  4074. .show = gpiolib_seq_show,
  4075. };
  4076. static int gpiolib_open(struct inode *inode, struct file *file)
  4077. {
  4078. return seq_open(file, &gpiolib_seq_ops);
  4079. }
  4080. static const struct file_operations gpiolib_operations = {
  4081. .owner = THIS_MODULE,
  4082. .open = gpiolib_open,
  4083. .read = seq_read,
  4084. .llseek = seq_lseek,
  4085. .release = seq_release,
  4086. };
  4087. static int __init gpiolib_debugfs_init(void)
  4088. {
  4089. /* /sys/kernel/debug/gpio */
  4090. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  4091. NULL, NULL, &gpiolib_operations);
  4092. return 0;
  4093. }
  4094. subsys_initcall(gpiolib_debugfs_init);
  4095. #endif /* DEBUG_FS */