gpiolib.c 119 KB

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