gpiolib.c 117 KB

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