gpiolib.c 123 KB

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