gpiolib.c 125 KB

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