gpiolib.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398
  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. #endif
  1068. gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
  1069. if (gdev->id < 0) {
  1070. status = gdev->id;
  1071. goto err_free_gdev;
  1072. }
  1073. dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
  1074. device_initialize(&gdev->dev);
  1075. dev_set_drvdata(&gdev->dev, gdev);
  1076. if (chip->parent && chip->parent->driver)
  1077. gdev->owner = chip->parent->driver->owner;
  1078. else if (chip->owner)
  1079. /* TODO: remove chip->owner */
  1080. gdev->owner = chip->owner;
  1081. else
  1082. gdev->owner = THIS_MODULE;
  1083. gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
  1084. if (!gdev->descs) {
  1085. status = -ENOMEM;
  1086. goto err_free_gdev;
  1087. }
  1088. if (chip->ngpio == 0) {
  1089. chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
  1090. status = -EINVAL;
  1091. goto err_free_descs;
  1092. }
  1093. if (chip->ngpio > FASTPATH_NGPIO)
  1094. chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
  1095. chip->ngpio, FASTPATH_NGPIO);
  1096. gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
  1097. if (!gdev->label) {
  1098. status = -ENOMEM;
  1099. goto err_free_descs;
  1100. }
  1101. gdev->ngpio = chip->ngpio;
  1102. gdev->data = data;
  1103. spin_lock_irqsave(&gpio_lock, flags);
  1104. /*
  1105. * TODO: this allocates a Linux GPIO number base in the global
  1106. * GPIO numberspace for this chip. In the long run we want to
  1107. * get *rid* of this numberspace and use only descriptors, but
  1108. * it may be a pipe dream. It will not happen before we get rid
  1109. * of the sysfs interface anyways.
  1110. */
  1111. if (base < 0) {
  1112. base = gpiochip_find_base(chip->ngpio);
  1113. if (base < 0) {
  1114. status = base;
  1115. spin_unlock_irqrestore(&gpio_lock, flags);
  1116. goto err_free_label;
  1117. }
  1118. /*
  1119. * TODO: it should not be necessary to reflect the assigned
  1120. * base outside of the GPIO subsystem. Go over drivers and
  1121. * see if anyone makes use of this, else drop this and assign
  1122. * a poison instead.
  1123. */
  1124. chip->base = base;
  1125. }
  1126. gdev->base = base;
  1127. status = gpiodev_add_to_list(gdev);
  1128. if (status) {
  1129. spin_unlock_irqrestore(&gpio_lock, flags);
  1130. goto err_free_label;
  1131. }
  1132. spin_unlock_irqrestore(&gpio_lock, flags);
  1133. for (i = 0; i < chip->ngpio; i++) {
  1134. struct gpio_desc *desc = &gdev->descs[i];
  1135. desc->gdev = gdev;
  1136. /* REVISIT: most hardware initializes GPIOs as inputs (often
  1137. * with pullups enabled) so power usage is minimized. Linux
  1138. * code should set the gpio direction first thing; but until
  1139. * it does, and in case chip->get_direction is not set, we may
  1140. * expose the wrong direction in sysfs.
  1141. */
  1142. desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
  1143. }
  1144. #ifdef CONFIG_PINCTRL
  1145. INIT_LIST_HEAD(&gdev->pin_ranges);
  1146. #endif
  1147. status = gpiochip_set_desc_names(chip);
  1148. if (status)
  1149. goto err_remove_from_list;
  1150. status = gpiochip_irqchip_init_valid_mask(chip);
  1151. if (status)
  1152. goto err_remove_from_list;
  1153. status = gpiochip_init_valid_mask(chip);
  1154. if (status)
  1155. goto err_remove_irqchip_mask;
  1156. status = gpiochip_add_irqchip(chip, lock_key, request_key);
  1157. if (status)
  1158. goto err_remove_chip;
  1159. status = of_gpiochip_add(chip);
  1160. if (status)
  1161. goto err_remove_chip;
  1162. acpi_gpiochip_add(chip);
  1163. machine_gpiochip_add(chip);
  1164. /*
  1165. * By first adding the chardev, and then adding the device,
  1166. * we get a device node entry in sysfs under
  1167. * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
  1168. * coldplug of device nodes and other udev business.
  1169. * We can do this only if gpiolib has been initialized.
  1170. * Otherwise, defer until later.
  1171. */
  1172. if (gpiolib_initialized) {
  1173. status = gpiochip_setup_dev(gdev);
  1174. if (status)
  1175. goto err_remove_chip;
  1176. }
  1177. return 0;
  1178. err_remove_chip:
  1179. acpi_gpiochip_remove(chip);
  1180. gpiochip_free_hogs(chip);
  1181. of_gpiochip_remove(chip);
  1182. gpiochip_free_valid_mask(chip);
  1183. err_remove_irqchip_mask:
  1184. gpiochip_irqchip_free_valid_mask(chip);
  1185. err_remove_from_list:
  1186. spin_lock_irqsave(&gpio_lock, flags);
  1187. list_del(&gdev->list);
  1188. spin_unlock_irqrestore(&gpio_lock, flags);
  1189. err_free_label:
  1190. kfree_const(gdev->label);
  1191. err_free_descs:
  1192. kfree(gdev->descs);
  1193. err_free_gdev:
  1194. ida_simple_remove(&gpio_ida, gdev->id);
  1195. /* failures here can mean systems won't boot... */
  1196. pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
  1197. gdev->base, gdev->base + gdev->ngpio - 1,
  1198. chip->label ? : "generic", status);
  1199. kfree(gdev);
  1200. return status;
  1201. }
  1202. EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
  1203. /**
  1204. * gpiochip_get_data() - get per-subdriver data for the chip
  1205. * @chip: GPIO chip
  1206. *
  1207. * Returns:
  1208. * The per-subdriver data for the chip.
  1209. */
  1210. void *gpiochip_get_data(struct gpio_chip *chip)
  1211. {
  1212. return chip->gpiodev->data;
  1213. }
  1214. EXPORT_SYMBOL_GPL(gpiochip_get_data);
  1215. /**
  1216. * gpiochip_remove() - unregister a gpio_chip
  1217. * @chip: the chip to unregister
  1218. *
  1219. * A gpio_chip with any GPIOs still requested may not be removed.
  1220. */
  1221. void gpiochip_remove(struct gpio_chip *chip)
  1222. {
  1223. struct gpio_device *gdev = chip->gpiodev;
  1224. struct gpio_desc *desc;
  1225. unsigned long flags;
  1226. unsigned i;
  1227. bool requested = false;
  1228. /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
  1229. gpiochip_sysfs_unregister(gdev);
  1230. gpiochip_free_hogs(chip);
  1231. /* Numb the device, cancelling all outstanding operations */
  1232. gdev->chip = NULL;
  1233. gpiochip_irqchip_remove(chip);
  1234. acpi_gpiochip_remove(chip);
  1235. gpiochip_remove_pin_ranges(chip);
  1236. of_gpiochip_remove(chip);
  1237. gpiochip_free_valid_mask(chip);
  1238. /*
  1239. * We accept no more calls into the driver from this point, so
  1240. * NULL the driver data pointer
  1241. */
  1242. gdev->data = NULL;
  1243. spin_lock_irqsave(&gpio_lock, flags);
  1244. for (i = 0; i < gdev->ngpio; i++) {
  1245. desc = &gdev->descs[i];
  1246. if (test_bit(FLAG_REQUESTED, &desc->flags))
  1247. requested = true;
  1248. }
  1249. spin_unlock_irqrestore(&gpio_lock, flags);
  1250. if (requested)
  1251. dev_crit(&gdev->dev,
  1252. "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
  1253. /*
  1254. * The gpiochip side puts its use of the device to rest here:
  1255. * if there are no userspace clients, the chardev and device will
  1256. * be removed, else it will be dangling until the last user is
  1257. * gone.
  1258. */
  1259. cdev_device_del(&gdev->chrdev, &gdev->dev);
  1260. put_device(&gdev->dev);
  1261. }
  1262. EXPORT_SYMBOL_GPL(gpiochip_remove);
  1263. static void devm_gpio_chip_release(struct device *dev, void *res)
  1264. {
  1265. struct gpio_chip *chip = *(struct gpio_chip **)res;
  1266. gpiochip_remove(chip);
  1267. }
  1268. static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
  1269. {
  1270. struct gpio_chip **r = res;
  1271. if (!r || !*r) {
  1272. WARN_ON(!r || !*r);
  1273. return 0;
  1274. }
  1275. return *r == data;
  1276. }
  1277. /**
  1278. * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
  1279. * @dev: the device pointer on which irq_chip belongs to.
  1280. * @chip: the chip to register, with chip->base initialized
  1281. * @data: driver-private data associated with this chip
  1282. *
  1283. * Context: potentially before irqs will work
  1284. *
  1285. * The gpio chip automatically be released when the device is unbound.
  1286. *
  1287. * Returns:
  1288. * A negative errno if the chip can't be registered, such as because the
  1289. * chip->base is invalid or already associated with a different chip.
  1290. * Otherwise it returns zero as a success code.
  1291. */
  1292. int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
  1293. void *data)
  1294. {
  1295. struct gpio_chip **ptr;
  1296. int ret;
  1297. ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
  1298. GFP_KERNEL);
  1299. if (!ptr)
  1300. return -ENOMEM;
  1301. ret = gpiochip_add_data(chip, data);
  1302. if (ret < 0) {
  1303. devres_free(ptr);
  1304. return ret;
  1305. }
  1306. *ptr = chip;
  1307. devres_add(dev, ptr);
  1308. return 0;
  1309. }
  1310. EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
  1311. /**
  1312. * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
  1313. * @dev: device for which which resource was allocated
  1314. * @chip: the chip to remove
  1315. *
  1316. * A gpio_chip with any GPIOs still requested may not be removed.
  1317. */
  1318. void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
  1319. {
  1320. int ret;
  1321. ret = devres_release(dev, devm_gpio_chip_release,
  1322. devm_gpio_chip_match, chip);
  1323. WARN_ON(ret);
  1324. }
  1325. EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
  1326. /**
  1327. * gpiochip_find() - iterator for locating a specific gpio_chip
  1328. * @data: data to pass to match function
  1329. * @match: Callback function to check gpio_chip
  1330. *
  1331. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  1332. * determined by a user supplied @match callback. The callback should return
  1333. * 0 if the device doesn't match and non-zero if it does. If the callback is
  1334. * non-zero, this function will return to the caller and not iterate over any
  1335. * more gpio_chips.
  1336. */
  1337. struct gpio_chip *gpiochip_find(void *data,
  1338. int (*match)(struct gpio_chip *chip,
  1339. void *data))
  1340. {
  1341. struct gpio_device *gdev;
  1342. struct gpio_chip *chip = NULL;
  1343. unsigned long flags;
  1344. spin_lock_irqsave(&gpio_lock, flags);
  1345. list_for_each_entry(gdev, &gpio_devices, list)
  1346. if (gdev->chip && match(gdev->chip, data)) {
  1347. chip = gdev->chip;
  1348. break;
  1349. }
  1350. spin_unlock_irqrestore(&gpio_lock, flags);
  1351. return chip;
  1352. }
  1353. EXPORT_SYMBOL_GPL(gpiochip_find);
  1354. static int gpiochip_match_name(struct gpio_chip *chip, void *data)
  1355. {
  1356. const char *name = data;
  1357. return !strcmp(chip->label, name);
  1358. }
  1359. static struct gpio_chip *find_chip_by_name(const char *name)
  1360. {
  1361. return gpiochip_find((void *)name, gpiochip_match_name);
  1362. }
  1363. #ifdef CONFIG_GPIOLIB_IRQCHIP
  1364. /*
  1365. * The following is irqchip helper code for gpiochips.
  1366. */
  1367. static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
  1368. {
  1369. if (!gpiochip->irq.need_valid_mask)
  1370. return 0;
  1371. gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
  1372. if (!gpiochip->irq.valid_mask)
  1373. return -ENOMEM;
  1374. return 0;
  1375. }
  1376. static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
  1377. {
  1378. kfree(gpiochip->irq.valid_mask);
  1379. gpiochip->irq.valid_mask = NULL;
  1380. }
  1381. bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
  1382. unsigned int offset)
  1383. {
  1384. if (!gpiochip_line_is_valid(gpiochip, offset))
  1385. return false;
  1386. /* No mask means all valid */
  1387. if (likely(!gpiochip->irq.valid_mask))
  1388. return true;
  1389. return test_bit(offset, gpiochip->irq.valid_mask);
  1390. }
  1391. EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
  1392. /**
  1393. * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
  1394. * @gpiochip: the gpiochip to set the irqchip chain to
  1395. * @irqchip: the irqchip to chain to the gpiochip
  1396. * @parent_irq: the irq number corresponding to the parent IRQ for this
  1397. * chained irqchip
  1398. * @parent_handler: the parent interrupt handler for the accumulated IRQ
  1399. * coming out of the gpiochip. If the interrupt is nested rather than
  1400. * cascaded, pass NULL in this handler argument
  1401. */
  1402. static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
  1403. struct irq_chip *irqchip,
  1404. unsigned int parent_irq,
  1405. irq_flow_handler_t parent_handler)
  1406. {
  1407. unsigned int offset;
  1408. if (!gpiochip->irq.domain) {
  1409. chip_err(gpiochip, "called %s before setting up irqchip\n",
  1410. __func__);
  1411. return;
  1412. }
  1413. if (parent_handler) {
  1414. if (gpiochip->can_sleep) {
  1415. chip_err(gpiochip,
  1416. "you cannot have chained interrupts on a chip that may sleep\n");
  1417. return;
  1418. }
  1419. /*
  1420. * The parent irqchip is already using the chip_data for this
  1421. * irqchip, so our callbacks simply use the handler_data.
  1422. */
  1423. irq_set_chained_handler_and_data(parent_irq, parent_handler,
  1424. gpiochip);
  1425. gpiochip->irq.parents = &parent_irq;
  1426. gpiochip->irq.num_parents = 1;
  1427. }
  1428. /* Set the parent IRQ for all affected IRQs */
  1429. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  1430. if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
  1431. continue;
  1432. irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
  1433. parent_irq);
  1434. }
  1435. }
  1436. /**
  1437. * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
  1438. * @gpiochip: the gpiochip to set the irqchip chain to
  1439. * @irqchip: the irqchip to chain to the gpiochip
  1440. * @parent_irq: the irq number corresponding to the parent IRQ for this
  1441. * chained irqchip
  1442. * @parent_handler: the parent interrupt handler for the accumulated IRQ
  1443. * coming out of the gpiochip. If the interrupt is nested rather than
  1444. * cascaded, pass NULL in this handler argument
  1445. */
  1446. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  1447. struct irq_chip *irqchip,
  1448. unsigned int parent_irq,
  1449. irq_flow_handler_t parent_handler)
  1450. {
  1451. if (gpiochip->irq.threaded) {
  1452. chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
  1453. return;
  1454. }
  1455. gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
  1456. parent_handler);
  1457. }
  1458. EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
  1459. /**
  1460. * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
  1461. * @gpiochip: the gpiochip to set the irqchip nested handler to
  1462. * @irqchip: the irqchip to nest to the gpiochip
  1463. * @parent_irq: the irq number corresponding to the parent IRQ for this
  1464. * nested irqchip
  1465. */
  1466. void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
  1467. struct irq_chip *irqchip,
  1468. unsigned int parent_irq)
  1469. {
  1470. gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
  1471. NULL);
  1472. }
  1473. EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
  1474. /**
  1475. * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
  1476. * @d: the irqdomain used by this irqchip
  1477. * @irq: the global irq number used by this GPIO irqchip irq
  1478. * @hwirq: the local IRQ/GPIO line offset on this gpiochip
  1479. *
  1480. * This function will set up the mapping for a certain IRQ line on a
  1481. * gpiochip by assigning the gpiochip as chip data, and using the irqchip
  1482. * stored inside the gpiochip.
  1483. */
  1484. int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  1485. irq_hw_number_t hwirq)
  1486. {
  1487. struct gpio_chip *chip = d->host_data;
  1488. int err = 0;
  1489. if (!gpiochip_irqchip_irq_valid(chip, hwirq))
  1490. return -ENXIO;
  1491. irq_set_chip_data(irq, chip);
  1492. /*
  1493. * This lock class tells lockdep that GPIO irqs are in a different
  1494. * category than their parents, so it won't report false recursion.
  1495. */
  1496. irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
  1497. irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
  1498. /* Chips that use nested thread handlers have them marked */
  1499. if (chip->irq.threaded)
  1500. irq_set_nested_thread(irq, 1);
  1501. irq_set_noprobe(irq);
  1502. if (chip->irq.num_parents == 1)
  1503. err = irq_set_parent(irq, chip->irq.parents[0]);
  1504. else if (chip->irq.map)
  1505. err = irq_set_parent(irq, chip->irq.map[hwirq]);
  1506. if (err < 0)
  1507. return err;
  1508. /*
  1509. * No set-up of the hardware will happen if IRQ_TYPE_NONE
  1510. * is passed as default type.
  1511. */
  1512. if (chip->irq.default_type != IRQ_TYPE_NONE)
  1513. irq_set_irq_type(irq, chip->irq.default_type);
  1514. return 0;
  1515. }
  1516. EXPORT_SYMBOL_GPL(gpiochip_irq_map);
  1517. void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
  1518. {
  1519. struct gpio_chip *chip = d->host_data;
  1520. if (chip->irq.threaded)
  1521. irq_set_nested_thread(irq, 0);
  1522. irq_set_chip_and_handler(irq, NULL, NULL);
  1523. irq_set_chip_data(irq, NULL);
  1524. }
  1525. EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
  1526. static const struct irq_domain_ops gpiochip_domain_ops = {
  1527. .map = gpiochip_irq_map,
  1528. .unmap = gpiochip_irq_unmap,
  1529. /* Virtually all GPIO irqchips are twocell:ed */
  1530. .xlate = irq_domain_xlate_twocell,
  1531. };
  1532. static int gpiochip_irq_reqres(struct irq_data *d)
  1533. {
  1534. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  1535. if (!try_module_get(chip->gpiodev->owner))
  1536. return -ENODEV;
  1537. if (gpiochip_lock_as_irq(chip, d->hwirq)) {
  1538. chip_err(chip,
  1539. "unable to lock HW IRQ %lu for IRQ\n",
  1540. d->hwirq);
  1541. module_put(chip->gpiodev->owner);
  1542. return -EINVAL;
  1543. }
  1544. return 0;
  1545. }
  1546. static void gpiochip_irq_relres(struct irq_data *d)
  1547. {
  1548. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  1549. gpiochip_unlock_as_irq(chip, d->hwirq);
  1550. module_put(chip->gpiodev->owner);
  1551. }
  1552. static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
  1553. {
  1554. if (!gpiochip_irqchip_irq_valid(chip, offset))
  1555. return -ENXIO;
  1556. return irq_create_mapping(chip->irq.domain, offset);
  1557. }
  1558. /**
  1559. * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
  1560. * @gpiochip: the GPIO chip to add the IRQ chip to
  1561. * @lock_key: lockdep class for IRQ lock
  1562. * @request_key: lockdep class for IRQ request
  1563. */
  1564. static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
  1565. struct lock_class_key *lock_key,
  1566. struct lock_class_key *request_key)
  1567. {
  1568. struct irq_chip *irqchip = gpiochip->irq.chip;
  1569. const struct irq_domain_ops *ops;
  1570. struct device_node *np;
  1571. unsigned int type;
  1572. unsigned int i;
  1573. if (!irqchip)
  1574. return 0;
  1575. if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
  1576. chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
  1577. return -EINVAL;
  1578. }
  1579. np = gpiochip->gpiodev->dev.of_node;
  1580. type = gpiochip->irq.default_type;
  1581. /*
  1582. * Specifying a default trigger is a terrible idea if DT or ACPI is
  1583. * used to configure the interrupts, as you may end up with
  1584. * conflicting triggers. Tell the user, and reset to NONE.
  1585. */
  1586. if (WARN(np && type != IRQ_TYPE_NONE,
  1587. "%s: Ignoring %u default trigger\n", np->full_name, type))
  1588. type = IRQ_TYPE_NONE;
  1589. if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
  1590. acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
  1591. "Ignoring %u default trigger\n", type);
  1592. type = IRQ_TYPE_NONE;
  1593. }
  1594. gpiochip->to_irq = gpiochip_to_irq;
  1595. gpiochip->irq.default_type = type;
  1596. gpiochip->irq.lock_key = lock_key;
  1597. gpiochip->irq.request_key = request_key;
  1598. if (gpiochip->irq.domain_ops)
  1599. ops = gpiochip->irq.domain_ops;
  1600. else
  1601. ops = &gpiochip_domain_ops;
  1602. gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
  1603. gpiochip->irq.first,
  1604. ops, gpiochip);
  1605. if (!gpiochip->irq.domain)
  1606. return -EINVAL;
  1607. /*
  1608. * It is possible for a driver to override this, but only if the
  1609. * alternative functions are both implemented.
  1610. */
  1611. if (!irqchip->irq_request_resources &&
  1612. !irqchip->irq_release_resources) {
  1613. irqchip->irq_request_resources = gpiochip_irq_reqres;
  1614. irqchip->irq_release_resources = gpiochip_irq_relres;
  1615. }
  1616. if (gpiochip->irq.parent_handler) {
  1617. void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
  1618. for (i = 0; i < gpiochip->irq.num_parents; i++) {
  1619. /*
  1620. * The parent IRQ chip is already using the chip_data
  1621. * for this IRQ chip, so our callbacks simply use the
  1622. * handler_data.
  1623. */
  1624. irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
  1625. gpiochip->irq.parent_handler,
  1626. data);
  1627. }
  1628. }
  1629. acpi_gpiochip_request_interrupts(gpiochip);
  1630. return 0;
  1631. }
  1632. /**
  1633. * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
  1634. * @gpiochip: the gpiochip to remove the irqchip from
  1635. *
  1636. * This is called only from gpiochip_remove()
  1637. */
  1638. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
  1639. {
  1640. unsigned int offset;
  1641. acpi_gpiochip_free_interrupts(gpiochip);
  1642. if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
  1643. struct gpio_irq_chip *irq = &gpiochip->irq;
  1644. unsigned int i;
  1645. for (i = 0; i < irq->num_parents; i++)
  1646. irq_set_chained_handler_and_data(irq->parents[i],
  1647. NULL, NULL);
  1648. }
  1649. /* Remove all IRQ mappings and delete the domain */
  1650. if (gpiochip->irq.domain) {
  1651. unsigned int irq;
  1652. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  1653. if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
  1654. continue;
  1655. irq = irq_find_mapping(gpiochip->irq.domain, offset);
  1656. irq_dispose_mapping(irq);
  1657. }
  1658. irq_domain_remove(gpiochip->irq.domain);
  1659. }
  1660. if (gpiochip->irq.chip) {
  1661. gpiochip->irq.chip->irq_request_resources = NULL;
  1662. gpiochip->irq.chip->irq_release_resources = NULL;
  1663. gpiochip->irq.chip = NULL;
  1664. }
  1665. gpiochip_irqchip_free_valid_mask(gpiochip);
  1666. }
  1667. /**
  1668. * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
  1669. * @gpiochip: the gpiochip to add the irqchip to
  1670. * @irqchip: the irqchip to add to the gpiochip
  1671. * @first_irq: if not dynamically assigned, the base (first) IRQ to
  1672. * allocate gpiochip irqs from
  1673. * @handler: the irq handler to use (often a predefined irq core function)
  1674. * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
  1675. * to have the core avoid setting up any default type in the hardware.
  1676. * @threaded: whether this irqchip uses a nested thread handler
  1677. * @lock_key: lockdep class for IRQ lock
  1678. * @request_key: lockdep class for IRQ request
  1679. *
  1680. * This function closely associates a certain irqchip with a certain
  1681. * gpiochip, providing an irq domain to translate the local IRQs to
  1682. * global irqs in the gpiolib core, and making sure that the gpiochip
  1683. * is passed as chip data to all related functions. Driver callbacks
  1684. * need to use gpiochip_get_data() to get their local state containers back
  1685. * from the gpiochip passed as chip data. An irqdomain will be stored
  1686. * in the gpiochip that shall be used by the driver to handle IRQ number
  1687. * translation. The gpiochip will need to be initialized and registered
  1688. * before calling this function.
  1689. *
  1690. * This function will handle two cell:ed simple IRQs and assumes all
  1691. * the pins on the gpiochip can generate a unique IRQ. Everything else
  1692. * need to be open coded.
  1693. */
  1694. int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
  1695. struct irq_chip *irqchip,
  1696. unsigned int first_irq,
  1697. irq_flow_handler_t handler,
  1698. unsigned int type,
  1699. bool threaded,
  1700. struct lock_class_key *lock_key,
  1701. struct lock_class_key *request_key)
  1702. {
  1703. struct device_node *of_node;
  1704. if (!gpiochip || !irqchip)
  1705. return -EINVAL;
  1706. if (!gpiochip->parent) {
  1707. pr_err("missing gpiochip .dev parent pointer\n");
  1708. return -EINVAL;
  1709. }
  1710. gpiochip->irq.threaded = threaded;
  1711. of_node = gpiochip->parent->of_node;
  1712. #ifdef CONFIG_OF_GPIO
  1713. /*
  1714. * If the gpiochip has an assigned OF node this takes precedence
  1715. * FIXME: get rid of this and use gpiochip->parent->of_node
  1716. * everywhere
  1717. */
  1718. if (gpiochip->of_node)
  1719. of_node = gpiochip->of_node;
  1720. #endif
  1721. /*
  1722. * Specifying a default trigger is a terrible idea if DT or ACPI is
  1723. * used to configure the interrupts, as you may end-up with
  1724. * conflicting triggers. Tell the user, and reset to NONE.
  1725. */
  1726. if (WARN(of_node && type != IRQ_TYPE_NONE,
  1727. "%pOF: Ignoring %d default trigger\n", of_node, type))
  1728. type = IRQ_TYPE_NONE;
  1729. if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
  1730. acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
  1731. "Ignoring %d default trigger\n", type);
  1732. type = IRQ_TYPE_NONE;
  1733. }
  1734. gpiochip->irq.chip = irqchip;
  1735. gpiochip->irq.handler = handler;
  1736. gpiochip->irq.default_type = type;
  1737. gpiochip->to_irq = gpiochip_to_irq;
  1738. gpiochip->irq.lock_key = lock_key;
  1739. gpiochip->irq.request_key = request_key;
  1740. gpiochip->irq.domain = irq_domain_add_simple(of_node,
  1741. gpiochip->ngpio, first_irq,
  1742. &gpiochip_domain_ops, gpiochip);
  1743. if (!gpiochip->irq.domain) {
  1744. gpiochip->irq.chip = NULL;
  1745. return -EINVAL;
  1746. }
  1747. /*
  1748. * It is possible for a driver to override this, but only if the
  1749. * alternative functions are both implemented.
  1750. */
  1751. if (!irqchip->irq_request_resources &&
  1752. !irqchip->irq_release_resources) {
  1753. irqchip->irq_request_resources = gpiochip_irq_reqres;
  1754. irqchip->irq_release_resources = gpiochip_irq_relres;
  1755. }
  1756. acpi_gpiochip_request_interrupts(gpiochip);
  1757. return 0;
  1758. }
  1759. EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
  1760. #else /* CONFIG_GPIOLIB_IRQCHIP */
  1761. static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
  1762. struct lock_class_key *lock_key,
  1763. struct lock_class_key *request_key)
  1764. {
  1765. return 0;
  1766. }
  1767. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
  1768. static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
  1769. {
  1770. return 0;
  1771. }
  1772. static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
  1773. { }
  1774. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  1775. /**
  1776. * gpiochip_generic_request() - request the gpio function for a pin
  1777. * @chip: the gpiochip owning the GPIO
  1778. * @offset: the offset of the GPIO to request for GPIO function
  1779. */
  1780. int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
  1781. {
  1782. return pinctrl_gpio_request(chip->gpiodev->base + offset);
  1783. }
  1784. EXPORT_SYMBOL_GPL(gpiochip_generic_request);
  1785. /**
  1786. * gpiochip_generic_free() - free the gpio function from a pin
  1787. * @chip: the gpiochip to request the gpio function for
  1788. * @offset: the offset of the GPIO to free from GPIO function
  1789. */
  1790. void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
  1791. {
  1792. pinctrl_gpio_free(chip->gpiodev->base + offset);
  1793. }
  1794. EXPORT_SYMBOL_GPL(gpiochip_generic_free);
  1795. /**
  1796. * gpiochip_generic_config() - apply configuration for a pin
  1797. * @chip: the gpiochip owning the GPIO
  1798. * @offset: the offset of the GPIO to apply the configuration
  1799. * @config: the configuration to be applied
  1800. */
  1801. int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
  1802. unsigned long config)
  1803. {
  1804. return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
  1805. }
  1806. EXPORT_SYMBOL_GPL(gpiochip_generic_config);
  1807. #ifdef CONFIG_PINCTRL
  1808. /**
  1809. * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
  1810. * @chip: the gpiochip to add the range for
  1811. * @pctldev: the pin controller to map to
  1812. * @gpio_offset: the start offset in the current gpio_chip number space
  1813. * @pin_group: name of the pin group inside the pin controller
  1814. *
  1815. * Calling this function directly from a DeviceTree-supported
  1816. * pinctrl driver is DEPRECATED. Please see Section 2.1 of
  1817. * Documentation/devicetree/bindings/gpio/gpio.txt on how to
  1818. * bind pinctrl and gpio drivers via the "gpio-ranges" property.
  1819. */
  1820. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  1821. struct pinctrl_dev *pctldev,
  1822. unsigned int gpio_offset, const char *pin_group)
  1823. {
  1824. struct gpio_pin_range *pin_range;
  1825. struct gpio_device *gdev = chip->gpiodev;
  1826. int ret;
  1827. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1828. if (!pin_range) {
  1829. chip_err(chip, "failed to allocate pin ranges\n");
  1830. return -ENOMEM;
  1831. }
  1832. /* Use local offset as range ID */
  1833. pin_range->range.id = gpio_offset;
  1834. pin_range->range.gc = chip;
  1835. pin_range->range.name = chip->label;
  1836. pin_range->range.base = gdev->base + gpio_offset;
  1837. pin_range->pctldev = pctldev;
  1838. ret = pinctrl_get_group_pins(pctldev, pin_group,
  1839. &pin_range->range.pins,
  1840. &pin_range->range.npins);
  1841. if (ret < 0) {
  1842. kfree(pin_range);
  1843. return ret;
  1844. }
  1845. pinctrl_add_gpio_range(pctldev, &pin_range->range);
  1846. chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
  1847. gpio_offset, gpio_offset + pin_range->range.npins - 1,
  1848. pinctrl_dev_get_devname(pctldev), pin_group);
  1849. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1850. return 0;
  1851. }
  1852. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  1853. /**
  1854. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  1855. * @chip: the gpiochip to add the range for
  1856. * @pinctl_name: the dev_name() of the pin controller to map to
  1857. * @gpio_offset: the start offset in the current gpio_chip number space
  1858. * @pin_offset: the start offset in the pin controller number space
  1859. * @npins: the number of pins from the offset of each pin space (GPIO and
  1860. * pin controller) to accumulate in this range
  1861. *
  1862. * Returns:
  1863. * 0 on success, or a negative error-code on failure.
  1864. *
  1865. * Calling this function directly from a DeviceTree-supported
  1866. * pinctrl driver is DEPRECATED. Please see Section 2.1 of
  1867. * Documentation/devicetree/bindings/gpio/gpio.txt on how to
  1868. * bind pinctrl and gpio drivers via the "gpio-ranges" property.
  1869. */
  1870. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  1871. unsigned int gpio_offset, unsigned int pin_offset,
  1872. unsigned int npins)
  1873. {
  1874. struct gpio_pin_range *pin_range;
  1875. struct gpio_device *gdev = chip->gpiodev;
  1876. int ret;
  1877. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1878. if (!pin_range) {
  1879. chip_err(chip, "failed to allocate pin ranges\n");
  1880. return -ENOMEM;
  1881. }
  1882. /* Use local offset as range ID */
  1883. pin_range->range.id = gpio_offset;
  1884. pin_range->range.gc = chip;
  1885. pin_range->range.name = chip->label;
  1886. pin_range->range.base = gdev->base + gpio_offset;
  1887. pin_range->range.pin_base = pin_offset;
  1888. pin_range->range.npins = npins;
  1889. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1890. &pin_range->range);
  1891. if (IS_ERR(pin_range->pctldev)) {
  1892. ret = PTR_ERR(pin_range->pctldev);
  1893. chip_err(chip, "could not create pin range\n");
  1894. kfree(pin_range);
  1895. return ret;
  1896. }
  1897. chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1898. gpio_offset, gpio_offset + npins - 1,
  1899. pinctl_name,
  1900. pin_offset, pin_offset + npins - 1);
  1901. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1902. return 0;
  1903. }
  1904. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1905. /**
  1906. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1907. * @chip: the chip to remove all the mappings for
  1908. */
  1909. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  1910. {
  1911. struct gpio_pin_range *pin_range, *tmp;
  1912. struct gpio_device *gdev = chip->gpiodev;
  1913. list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
  1914. list_del(&pin_range->node);
  1915. pinctrl_remove_gpio_range(pin_range->pctldev,
  1916. &pin_range->range);
  1917. kfree(pin_range);
  1918. }
  1919. }
  1920. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1921. #endif /* CONFIG_PINCTRL */
  1922. /* These "optional" allocation calls help prevent drivers from stomping
  1923. * on each other, and help provide better diagnostics in debugfs.
  1924. * They're called even less than the "set direction" calls.
  1925. */
  1926. static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
  1927. {
  1928. struct gpio_chip *chip = desc->gdev->chip;
  1929. int status;
  1930. unsigned long flags;
  1931. spin_lock_irqsave(&gpio_lock, flags);
  1932. /* NOTE: gpio_request() can be called in early boot,
  1933. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1934. */
  1935. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1936. desc_set_label(desc, label ? : "?");
  1937. status = 0;
  1938. } else {
  1939. status = -EBUSY;
  1940. goto done;
  1941. }
  1942. if (chip->request) {
  1943. /* chip->request may sleep */
  1944. spin_unlock_irqrestore(&gpio_lock, flags);
  1945. status = chip->request(chip, gpio_chip_hwgpio(desc));
  1946. spin_lock_irqsave(&gpio_lock, flags);
  1947. if (status < 0) {
  1948. desc_set_label(desc, NULL);
  1949. clear_bit(FLAG_REQUESTED, &desc->flags);
  1950. goto done;
  1951. }
  1952. }
  1953. if (chip->get_direction) {
  1954. /* chip->get_direction may sleep */
  1955. spin_unlock_irqrestore(&gpio_lock, flags);
  1956. gpiod_get_direction(desc);
  1957. spin_lock_irqsave(&gpio_lock, flags);
  1958. }
  1959. done:
  1960. spin_unlock_irqrestore(&gpio_lock, flags);
  1961. return status;
  1962. }
  1963. /*
  1964. * This descriptor validation needs to be inserted verbatim into each
  1965. * function taking a descriptor, so we need to use a preprocessor
  1966. * macro to avoid endless duplication. If the desc is NULL it is an
  1967. * optional GPIO and calls should just bail out.
  1968. */
  1969. static int validate_desc(const struct gpio_desc *desc, const char *func)
  1970. {
  1971. if (!desc)
  1972. return 0;
  1973. if (IS_ERR(desc)) {
  1974. pr_warn("%s: invalid GPIO (errorpointer)\n", func);
  1975. return PTR_ERR(desc);
  1976. }
  1977. if (!desc->gdev) {
  1978. pr_warn("%s: invalid GPIO (no device)\n", func);
  1979. return -EINVAL;
  1980. }
  1981. if (!desc->gdev->chip) {
  1982. dev_warn(&desc->gdev->dev,
  1983. "%s: backing chip is gone\n", func);
  1984. return 0;
  1985. }
  1986. return 1;
  1987. }
  1988. #define VALIDATE_DESC(desc) do { \
  1989. int __valid = validate_desc(desc, __func__); \
  1990. if (__valid <= 0) \
  1991. return __valid; \
  1992. } while (0)
  1993. #define VALIDATE_DESC_VOID(desc) do { \
  1994. int __valid = validate_desc(desc, __func__); \
  1995. if (__valid <= 0) \
  1996. return; \
  1997. } while (0)
  1998. int gpiod_request(struct gpio_desc *desc, const char *label)
  1999. {
  2000. int status = -EPROBE_DEFER;
  2001. struct gpio_device *gdev;
  2002. VALIDATE_DESC(desc);
  2003. gdev = desc->gdev;
  2004. if (try_module_get(gdev->owner)) {
  2005. status = gpiod_request_commit(desc, label);
  2006. if (status < 0)
  2007. module_put(gdev->owner);
  2008. else
  2009. get_device(&gdev->dev);
  2010. }
  2011. if (status)
  2012. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  2013. return status;
  2014. }
  2015. static bool gpiod_free_commit(struct gpio_desc *desc)
  2016. {
  2017. bool ret = false;
  2018. unsigned long flags;
  2019. struct gpio_chip *chip;
  2020. might_sleep();
  2021. gpiod_unexport(desc);
  2022. spin_lock_irqsave(&gpio_lock, flags);
  2023. chip = desc->gdev->chip;
  2024. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  2025. if (chip->free) {
  2026. spin_unlock_irqrestore(&gpio_lock, flags);
  2027. might_sleep_if(chip->can_sleep);
  2028. chip->free(chip, gpio_chip_hwgpio(desc));
  2029. spin_lock_irqsave(&gpio_lock, flags);
  2030. }
  2031. desc_set_label(desc, NULL);
  2032. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2033. clear_bit(FLAG_REQUESTED, &desc->flags);
  2034. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  2035. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  2036. clear_bit(FLAG_IS_HOGGED, &desc->flags);
  2037. ret = true;
  2038. }
  2039. spin_unlock_irqrestore(&gpio_lock, flags);
  2040. return ret;
  2041. }
  2042. void gpiod_free(struct gpio_desc *desc)
  2043. {
  2044. if (desc && desc->gdev && gpiod_free_commit(desc)) {
  2045. module_put(desc->gdev->owner);
  2046. put_device(&desc->gdev->dev);
  2047. } else {
  2048. WARN_ON(extra_checks);
  2049. }
  2050. }
  2051. /**
  2052. * gpiochip_is_requested - return string iff signal was requested
  2053. * @chip: controller managing the signal
  2054. * @offset: of signal within controller's 0..(ngpio - 1) range
  2055. *
  2056. * Returns NULL if the GPIO is not currently requested, else a string.
  2057. * The string returned is the label passed to gpio_request(); if none has been
  2058. * passed it is a meaningless, non-NULL constant.
  2059. *
  2060. * This function is for use by GPIO controller drivers. The label can
  2061. * help with diagnostics, and knowing that the signal is used as a GPIO
  2062. * can help avoid accidentally multiplexing it to another controller.
  2063. */
  2064. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  2065. {
  2066. struct gpio_desc *desc;
  2067. if (offset >= chip->ngpio)
  2068. return NULL;
  2069. desc = &chip->gpiodev->descs[offset];
  2070. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  2071. return NULL;
  2072. return desc->label;
  2073. }
  2074. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  2075. /**
  2076. * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
  2077. * @chip: GPIO chip
  2078. * @hwnum: hardware number of the GPIO for which to request the descriptor
  2079. * @label: label for the GPIO
  2080. *
  2081. * Function allows GPIO chip drivers to request and use their own GPIO
  2082. * descriptors via gpiolib API. Difference to gpiod_request() is that this
  2083. * function will not increase reference count of the GPIO chip module. This
  2084. * allows the GPIO chip module to be unloaded as needed (we assume that the
  2085. * GPIO chip driver handles freeing the GPIOs it has requested).
  2086. *
  2087. * Returns:
  2088. * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
  2089. * code on failure.
  2090. */
  2091. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  2092. const char *label)
  2093. {
  2094. struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
  2095. int err;
  2096. if (IS_ERR(desc)) {
  2097. chip_err(chip, "failed to get GPIO descriptor\n");
  2098. return desc;
  2099. }
  2100. err = gpiod_request_commit(desc, label);
  2101. if (err < 0)
  2102. return ERR_PTR(err);
  2103. return desc;
  2104. }
  2105. EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
  2106. /**
  2107. * gpiochip_free_own_desc - Free GPIO requested by the chip driver
  2108. * @desc: GPIO descriptor to free
  2109. *
  2110. * Function frees the given GPIO requested previously with
  2111. * gpiochip_request_own_desc().
  2112. */
  2113. void gpiochip_free_own_desc(struct gpio_desc *desc)
  2114. {
  2115. if (desc)
  2116. gpiod_free_commit(desc);
  2117. }
  2118. EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
  2119. /*
  2120. * Drivers MUST set GPIO direction before making get/set calls. In
  2121. * some cases this is done in early boot, before IRQs are enabled.
  2122. *
  2123. * As a rule these aren't called more than once (except for drivers
  2124. * using the open-drain emulation idiom) so these are natural places
  2125. * to accumulate extra debugging checks. Note that we can't (yet)
  2126. * rely on gpio_request() having been called beforehand.
  2127. */
  2128. /**
  2129. * gpiod_direction_input - set the GPIO direction to input
  2130. * @desc: GPIO to set to input
  2131. *
  2132. * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
  2133. * be called safely on it.
  2134. *
  2135. * Return 0 in case of success, else an error code.
  2136. */
  2137. int gpiod_direction_input(struct gpio_desc *desc)
  2138. {
  2139. struct gpio_chip *chip;
  2140. int status = -EINVAL;
  2141. VALIDATE_DESC(desc);
  2142. chip = desc->gdev->chip;
  2143. if (!chip->get || !chip->direction_input) {
  2144. gpiod_warn(desc,
  2145. "%s: missing get() or direction_input() operations\n",
  2146. __func__);
  2147. return -EIO;
  2148. }
  2149. status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
  2150. if (status == 0)
  2151. clear_bit(FLAG_IS_OUT, &desc->flags);
  2152. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  2153. return status;
  2154. }
  2155. EXPORT_SYMBOL_GPL(gpiod_direction_input);
  2156. static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
  2157. enum pin_config_param mode)
  2158. {
  2159. unsigned long config = { PIN_CONF_PACKED(mode, 0) };
  2160. return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
  2161. }
  2162. static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
  2163. {
  2164. struct gpio_chip *gc = desc->gdev->chip;
  2165. int val = !!value;
  2166. int ret;
  2167. if (!gc->set || !gc->direction_output) {
  2168. gpiod_warn(desc,
  2169. "%s: missing set() or direction_output() operations\n",
  2170. __func__);
  2171. return -EIO;
  2172. }
  2173. ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
  2174. if (!ret)
  2175. set_bit(FLAG_IS_OUT, &desc->flags);
  2176. trace_gpio_value(desc_to_gpio(desc), 0, val);
  2177. trace_gpio_direction(desc_to_gpio(desc), 0, ret);
  2178. return ret;
  2179. }
  2180. /**
  2181. * gpiod_direction_output_raw - set the GPIO direction to output
  2182. * @desc: GPIO to set to output
  2183. * @value: initial output value of the GPIO
  2184. *
  2185. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  2186. * be called safely on it. The initial value of the output must be specified
  2187. * as raw value on the physical line without regard for the ACTIVE_LOW status.
  2188. *
  2189. * Return 0 in case of success, else an error code.
  2190. */
  2191. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  2192. {
  2193. VALIDATE_DESC(desc);
  2194. return gpiod_direction_output_raw_commit(desc, value);
  2195. }
  2196. EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
  2197. /**
  2198. * gpiod_direction_output - set the GPIO direction to output
  2199. * @desc: GPIO to set to output
  2200. * @value: initial output value of the GPIO
  2201. *
  2202. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  2203. * be called safely on it. The initial value of the output must be specified
  2204. * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  2205. * account.
  2206. *
  2207. * Return 0 in case of success, else an error code.
  2208. */
  2209. int gpiod_direction_output(struct gpio_desc *desc, int value)
  2210. {
  2211. struct gpio_chip *gc;
  2212. int ret;
  2213. VALIDATE_DESC(desc);
  2214. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2215. value = !value;
  2216. else
  2217. value = !!value;
  2218. /* GPIOs used for IRQs shall not be set as output */
  2219. if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
  2220. gpiod_err(desc,
  2221. "%s: tried to set a GPIO tied to an IRQ as output\n",
  2222. __func__);
  2223. return -EIO;
  2224. }
  2225. gc = desc->gdev->chip;
  2226. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
  2227. /* First see if we can enable open drain in hardware */
  2228. ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
  2229. PIN_CONFIG_DRIVE_OPEN_DRAIN);
  2230. if (!ret)
  2231. goto set_output_value;
  2232. /* Emulate open drain by not actively driving the line high */
  2233. if (value)
  2234. return gpiod_direction_input(desc);
  2235. }
  2236. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
  2237. ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
  2238. PIN_CONFIG_DRIVE_OPEN_SOURCE);
  2239. if (!ret)
  2240. goto set_output_value;
  2241. /* Emulate open source by not actively driving the line low */
  2242. if (!value)
  2243. return gpiod_direction_input(desc);
  2244. } else {
  2245. gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
  2246. PIN_CONFIG_DRIVE_PUSH_PULL);
  2247. }
  2248. set_output_value:
  2249. return gpiod_direction_output_raw_commit(desc, value);
  2250. }
  2251. EXPORT_SYMBOL_GPL(gpiod_direction_output);
  2252. /**
  2253. * gpiod_set_debounce - sets @debounce time for a GPIO
  2254. * @desc: descriptor of the GPIO for which to set debounce time
  2255. * @debounce: debounce time in microseconds
  2256. *
  2257. * Returns:
  2258. * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
  2259. * debounce time.
  2260. */
  2261. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  2262. {
  2263. struct gpio_chip *chip;
  2264. unsigned long config;
  2265. VALIDATE_DESC(desc);
  2266. chip = desc->gdev->chip;
  2267. if (!chip->set || !chip->set_config) {
  2268. gpiod_dbg(desc,
  2269. "%s: missing set() or set_config() operations\n",
  2270. __func__);
  2271. return -ENOTSUPP;
  2272. }
  2273. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
  2274. return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
  2275. }
  2276. EXPORT_SYMBOL_GPL(gpiod_set_debounce);
  2277. /**
  2278. * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
  2279. * @desc: descriptor of the GPIO for which to configure persistence
  2280. * @transitory: True to lose state on suspend or reset, false for persistence
  2281. *
  2282. * Returns:
  2283. * 0 on success, otherwise a negative error code.
  2284. */
  2285. int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
  2286. {
  2287. struct gpio_chip *chip;
  2288. unsigned long packed;
  2289. int gpio;
  2290. int rc;
  2291. VALIDATE_DESC(desc);
  2292. /*
  2293. * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
  2294. * persistence state.
  2295. */
  2296. if (transitory)
  2297. set_bit(FLAG_TRANSITORY, &desc->flags);
  2298. else
  2299. clear_bit(FLAG_TRANSITORY, &desc->flags);
  2300. /* If the driver supports it, set the persistence state now */
  2301. chip = desc->gdev->chip;
  2302. if (!chip->set_config)
  2303. return 0;
  2304. packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
  2305. !transitory);
  2306. gpio = gpio_chip_hwgpio(desc);
  2307. rc = chip->set_config(chip, gpio, packed);
  2308. if (rc == -ENOTSUPP) {
  2309. dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
  2310. gpio);
  2311. return 0;
  2312. }
  2313. return rc;
  2314. }
  2315. EXPORT_SYMBOL_GPL(gpiod_set_transitory);
  2316. /**
  2317. * gpiod_is_active_low - test whether a GPIO is active-low or not
  2318. * @desc: the gpio descriptor to test
  2319. *
  2320. * Returns 1 if the GPIO is active-low, 0 otherwise.
  2321. */
  2322. int gpiod_is_active_low(const struct gpio_desc *desc)
  2323. {
  2324. VALIDATE_DESC(desc);
  2325. return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2326. }
  2327. EXPORT_SYMBOL_GPL(gpiod_is_active_low);
  2328. /* I/O calls are only valid after configuration completed; the relevant
  2329. * "is this a valid GPIO" error checks should already have been done.
  2330. *
  2331. * "Get" operations are often inlinable as reading a pin value register,
  2332. * and masking the relevant bit in that register.
  2333. *
  2334. * When "set" operations are inlinable, they involve writing that mask to
  2335. * one register to set a low value, or a different register to set it high.
  2336. * Otherwise locking is needed, so there may be little value to inlining.
  2337. *
  2338. *------------------------------------------------------------------------
  2339. *
  2340. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  2341. * have requested the GPIO. That can include implicit requesting by
  2342. * a direction setting call. Marking a gpio as requested locks its chip
  2343. * in memory, guaranteeing that these table lookups need no more locking
  2344. * and that gpiochip_remove() will fail.
  2345. *
  2346. * REVISIT when debugging, consider adding some instrumentation to ensure
  2347. * that the GPIO was actually requested.
  2348. */
  2349. static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
  2350. {
  2351. struct gpio_chip *chip;
  2352. int offset;
  2353. int value;
  2354. chip = desc->gdev->chip;
  2355. offset = gpio_chip_hwgpio(desc);
  2356. value = chip->get ? chip->get(chip, offset) : -EIO;
  2357. value = value < 0 ? value : !!value;
  2358. trace_gpio_value(desc_to_gpio(desc), 1, value);
  2359. return value;
  2360. }
  2361. static int gpio_chip_get_multiple(struct gpio_chip *chip,
  2362. unsigned long *mask, unsigned long *bits)
  2363. {
  2364. if (chip->get_multiple) {
  2365. return chip->get_multiple(chip, mask, bits);
  2366. } else if (chip->get) {
  2367. int i, value;
  2368. for_each_set_bit(i, mask, chip->ngpio) {
  2369. value = chip->get(chip, i);
  2370. if (value < 0)
  2371. return value;
  2372. __assign_bit(i, bits, value);
  2373. }
  2374. return 0;
  2375. }
  2376. return -EIO;
  2377. }
  2378. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  2379. unsigned int array_size,
  2380. struct gpio_desc **desc_array,
  2381. int *value_array)
  2382. {
  2383. int i = 0;
  2384. while (i < array_size) {
  2385. struct gpio_chip *chip = desc_array[i]->gdev->chip;
  2386. unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
  2387. unsigned long *mask, *bits;
  2388. int first, j, ret;
  2389. if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
  2390. mask = fastpath;
  2391. } else {
  2392. mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
  2393. sizeof(*mask),
  2394. can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  2395. if (!mask)
  2396. return -ENOMEM;
  2397. }
  2398. bits = mask + BITS_TO_LONGS(chip->ngpio);
  2399. bitmap_zero(mask, chip->ngpio);
  2400. if (!can_sleep)
  2401. WARN_ON(chip->can_sleep);
  2402. /* collect all inputs belonging to the same chip */
  2403. first = i;
  2404. do {
  2405. const struct gpio_desc *desc = desc_array[i];
  2406. int hwgpio = gpio_chip_hwgpio(desc);
  2407. __set_bit(hwgpio, mask);
  2408. i++;
  2409. } while ((i < array_size) &&
  2410. (desc_array[i]->gdev->chip == chip));
  2411. ret = gpio_chip_get_multiple(chip, mask, bits);
  2412. if (ret) {
  2413. if (mask != fastpath)
  2414. kfree(mask);
  2415. return ret;
  2416. }
  2417. for (j = first; j < i; j++) {
  2418. const struct gpio_desc *desc = desc_array[j];
  2419. int hwgpio = gpio_chip_hwgpio(desc);
  2420. int value = test_bit(hwgpio, bits);
  2421. if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2422. value = !value;
  2423. value_array[j] = value;
  2424. trace_gpio_value(desc_to_gpio(desc), 1, value);
  2425. }
  2426. if (mask != fastpath)
  2427. kfree(mask);
  2428. }
  2429. return 0;
  2430. }
  2431. /**
  2432. * gpiod_get_raw_value() - return a gpio's raw value
  2433. * @desc: gpio whose value will be returned
  2434. *
  2435. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  2436. * its ACTIVE_LOW status, or negative errno on failure.
  2437. *
  2438. * This function should be called from contexts where we cannot sleep, and will
  2439. * complain if the GPIO chip functions potentially sleep.
  2440. */
  2441. int gpiod_get_raw_value(const struct gpio_desc *desc)
  2442. {
  2443. VALIDATE_DESC(desc);
  2444. /* Should be using gpio_get_value_cansleep() */
  2445. WARN_ON(desc->gdev->chip->can_sleep);
  2446. return gpiod_get_raw_value_commit(desc);
  2447. }
  2448. EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
  2449. /**
  2450. * gpiod_get_value() - return a gpio's value
  2451. * @desc: gpio whose value will be returned
  2452. *
  2453. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  2454. * account, or negative errno on failure.
  2455. *
  2456. * This function should be called from contexts where we cannot sleep, and will
  2457. * complain if the GPIO chip functions potentially sleep.
  2458. */
  2459. int gpiod_get_value(const struct gpio_desc *desc)
  2460. {
  2461. int value;
  2462. VALIDATE_DESC(desc);
  2463. /* Should be using gpio_get_value_cansleep() */
  2464. WARN_ON(desc->gdev->chip->can_sleep);
  2465. value = gpiod_get_raw_value_commit(desc);
  2466. if (value < 0)
  2467. return value;
  2468. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2469. value = !value;
  2470. return value;
  2471. }
  2472. EXPORT_SYMBOL_GPL(gpiod_get_value);
  2473. /**
  2474. * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
  2475. * @array_size: number of elements in the descriptor / value arrays
  2476. * @desc_array: array of GPIO descriptors whose values will be read
  2477. * @value_array: array to store the read values
  2478. *
  2479. * Read the raw values of the GPIOs, i.e. the values of the physical lines
  2480. * without regard for their ACTIVE_LOW status. Return 0 in case of success,
  2481. * else an error code.
  2482. *
  2483. * This function should be called from contexts where we cannot sleep,
  2484. * and it will complain if the GPIO chip functions potentially sleep.
  2485. */
  2486. int gpiod_get_raw_array_value(unsigned int array_size,
  2487. struct gpio_desc **desc_array, int *value_array)
  2488. {
  2489. if (!desc_array)
  2490. return -EINVAL;
  2491. return gpiod_get_array_value_complex(true, false, array_size,
  2492. desc_array, value_array);
  2493. }
  2494. EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
  2495. /**
  2496. * gpiod_get_array_value() - read values from an array of GPIOs
  2497. * @array_size: number of elements in the descriptor / value arrays
  2498. * @desc_array: array of GPIO descriptors whose values will be read
  2499. * @value_array: array to store the read values
  2500. *
  2501. * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2502. * into account. Return 0 in case of success, else an error code.
  2503. *
  2504. * This function should be called from contexts where we cannot sleep,
  2505. * and it will complain if the GPIO chip functions potentially sleep.
  2506. */
  2507. int gpiod_get_array_value(unsigned int array_size,
  2508. struct gpio_desc **desc_array, int *value_array)
  2509. {
  2510. if (!desc_array)
  2511. return -EINVAL;
  2512. return gpiod_get_array_value_complex(false, false, array_size,
  2513. desc_array, value_array);
  2514. }
  2515. EXPORT_SYMBOL_GPL(gpiod_get_array_value);
  2516. /*
  2517. * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
  2518. * @desc: gpio descriptor whose state need to be set.
  2519. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  2520. */
  2521. static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
  2522. {
  2523. int err = 0;
  2524. struct gpio_chip *chip = desc->gdev->chip;
  2525. int offset = gpio_chip_hwgpio(desc);
  2526. if (value) {
  2527. err = chip->direction_input(chip, offset);
  2528. if (!err)
  2529. clear_bit(FLAG_IS_OUT, &desc->flags);
  2530. } else {
  2531. err = chip->direction_output(chip, offset, 0);
  2532. if (!err)
  2533. set_bit(FLAG_IS_OUT, &desc->flags);
  2534. }
  2535. trace_gpio_direction(desc_to_gpio(desc), value, err);
  2536. if (err < 0)
  2537. gpiod_err(desc,
  2538. "%s: Error in set_value for open drain err %d\n",
  2539. __func__, err);
  2540. }
  2541. /*
  2542. * _gpio_set_open_source_value() - Set the open source gpio's value.
  2543. * @desc: gpio descriptor whose state need to be set.
  2544. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  2545. */
  2546. static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
  2547. {
  2548. int err = 0;
  2549. struct gpio_chip *chip = desc->gdev->chip;
  2550. int offset = gpio_chip_hwgpio(desc);
  2551. if (value) {
  2552. err = chip->direction_output(chip, offset, 1);
  2553. if (!err)
  2554. set_bit(FLAG_IS_OUT, &desc->flags);
  2555. } else {
  2556. err = chip->direction_input(chip, offset);
  2557. if (!err)
  2558. clear_bit(FLAG_IS_OUT, &desc->flags);
  2559. }
  2560. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  2561. if (err < 0)
  2562. gpiod_err(desc,
  2563. "%s: Error in set_value for open source err %d\n",
  2564. __func__, err);
  2565. }
  2566. static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
  2567. {
  2568. struct gpio_chip *chip;
  2569. chip = desc->gdev->chip;
  2570. trace_gpio_value(desc_to_gpio(desc), 0, value);
  2571. chip->set(chip, gpio_chip_hwgpio(desc), value);
  2572. }
  2573. /*
  2574. * set multiple outputs on the same chip;
  2575. * use the chip's set_multiple function if available;
  2576. * otherwise set the outputs sequentially;
  2577. * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
  2578. * defines which outputs are to be changed
  2579. * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
  2580. * defines the values the outputs specified by mask are to be set to
  2581. */
  2582. static void gpio_chip_set_multiple(struct gpio_chip *chip,
  2583. unsigned long *mask, unsigned long *bits)
  2584. {
  2585. if (chip->set_multiple) {
  2586. chip->set_multiple(chip, mask, bits);
  2587. } else {
  2588. unsigned int i;
  2589. /* set outputs if the corresponding mask bit is set */
  2590. for_each_set_bit(i, mask, chip->ngpio)
  2591. chip->set(chip, i, test_bit(i, bits));
  2592. }
  2593. }
  2594. int gpiod_set_array_value_complex(bool raw, bool can_sleep,
  2595. unsigned int array_size,
  2596. struct gpio_desc **desc_array,
  2597. int *value_array)
  2598. {
  2599. int i = 0;
  2600. while (i < array_size) {
  2601. struct gpio_chip *chip = desc_array[i]->gdev->chip;
  2602. unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
  2603. unsigned long *mask, *bits;
  2604. int count = 0;
  2605. if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
  2606. mask = fastpath;
  2607. } else {
  2608. mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
  2609. sizeof(*mask),
  2610. can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  2611. if (!mask)
  2612. return -ENOMEM;
  2613. }
  2614. bits = mask + BITS_TO_LONGS(chip->ngpio);
  2615. bitmap_zero(mask, chip->ngpio);
  2616. if (!can_sleep)
  2617. WARN_ON(chip->can_sleep);
  2618. do {
  2619. struct gpio_desc *desc = desc_array[i];
  2620. int hwgpio = gpio_chip_hwgpio(desc);
  2621. int value = value_array[i];
  2622. if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2623. value = !value;
  2624. trace_gpio_value(desc_to_gpio(desc), 0, value);
  2625. /*
  2626. * collect all normal outputs belonging to the same chip
  2627. * open drain and open source outputs are set individually
  2628. */
  2629. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
  2630. gpio_set_open_drain_value_commit(desc, value);
  2631. } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
  2632. gpio_set_open_source_value_commit(desc, value);
  2633. } else {
  2634. __set_bit(hwgpio, mask);
  2635. if (value)
  2636. __set_bit(hwgpio, bits);
  2637. else
  2638. __clear_bit(hwgpio, bits);
  2639. count++;
  2640. }
  2641. i++;
  2642. } while ((i < array_size) &&
  2643. (desc_array[i]->gdev->chip == chip));
  2644. /* push collected bits to outputs */
  2645. if (count != 0)
  2646. gpio_chip_set_multiple(chip, mask, bits);
  2647. if (mask != fastpath)
  2648. kfree(mask);
  2649. }
  2650. return 0;
  2651. }
  2652. /**
  2653. * gpiod_set_raw_value() - assign a gpio's raw value
  2654. * @desc: gpio whose value will be assigned
  2655. * @value: value to assign
  2656. *
  2657. * Set the raw value of the GPIO, i.e. the value of its physical line without
  2658. * regard for its ACTIVE_LOW status.
  2659. *
  2660. * This function should be called from contexts where we cannot sleep, and will
  2661. * complain if the GPIO chip functions potentially sleep.
  2662. */
  2663. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  2664. {
  2665. VALIDATE_DESC_VOID(desc);
  2666. /* Should be using gpiod_set_value_cansleep() */
  2667. WARN_ON(desc->gdev->chip->can_sleep);
  2668. gpiod_set_raw_value_commit(desc, value);
  2669. }
  2670. EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
  2671. /**
  2672. * gpiod_set_value_nocheck() - set a GPIO line value without checking
  2673. * @desc: the descriptor to set the value on
  2674. * @value: value to set
  2675. *
  2676. * This sets the value of a GPIO line backing a descriptor, applying
  2677. * different semantic quirks like active low and open drain/source
  2678. * handling.
  2679. */
  2680. static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
  2681. {
  2682. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2683. value = !value;
  2684. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  2685. gpio_set_open_drain_value_commit(desc, value);
  2686. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  2687. gpio_set_open_source_value_commit(desc, value);
  2688. else
  2689. gpiod_set_raw_value_commit(desc, value);
  2690. }
  2691. /**
  2692. * gpiod_set_value() - assign a gpio's value
  2693. * @desc: gpio whose value will be assigned
  2694. * @value: value to assign
  2695. *
  2696. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
  2697. * OPEN_DRAIN and OPEN_SOURCE flags into account.
  2698. *
  2699. * This function should be called from contexts where we cannot sleep, and will
  2700. * complain if the GPIO chip functions potentially sleep.
  2701. */
  2702. void gpiod_set_value(struct gpio_desc *desc, int value)
  2703. {
  2704. VALIDATE_DESC_VOID(desc);
  2705. WARN_ON(desc->gdev->chip->can_sleep);
  2706. gpiod_set_value_nocheck(desc, value);
  2707. }
  2708. EXPORT_SYMBOL_GPL(gpiod_set_value);
  2709. /**
  2710. * gpiod_set_raw_array_value() - assign values to an array of GPIOs
  2711. * @array_size: number of elements in the descriptor / value arrays
  2712. * @desc_array: array of GPIO descriptors whose values will be assigned
  2713. * @value_array: array of values to assign
  2714. *
  2715. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  2716. * without regard for their ACTIVE_LOW status.
  2717. *
  2718. * This function should be called from contexts where we cannot sleep, and will
  2719. * complain if the GPIO chip functions potentially sleep.
  2720. */
  2721. int gpiod_set_raw_array_value(unsigned int array_size,
  2722. struct gpio_desc **desc_array, int *value_array)
  2723. {
  2724. if (!desc_array)
  2725. return -EINVAL;
  2726. return gpiod_set_array_value_complex(true, false, array_size,
  2727. desc_array, value_array);
  2728. }
  2729. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
  2730. /**
  2731. * gpiod_set_array_value() - assign values to an array of GPIOs
  2732. * @array_size: number of elements in the descriptor / value arrays
  2733. * @desc_array: array of GPIO descriptors whose values will be assigned
  2734. * @value_array: array of values to assign
  2735. *
  2736. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2737. * into account.
  2738. *
  2739. * This function should be called from contexts where we cannot sleep, and will
  2740. * complain if the GPIO chip functions potentially sleep.
  2741. */
  2742. void gpiod_set_array_value(unsigned int array_size,
  2743. struct gpio_desc **desc_array, int *value_array)
  2744. {
  2745. if (!desc_array)
  2746. return;
  2747. gpiod_set_array_value_complex(false, false, array_size, desc_array,
  2748. value_array);
  2749. }
  2750. EXPORT_SYMBOL_GPL(gpiod_set_array_value);
  2751. /**
  2752. * gpiod_cansleep() - report whether gpio value access may sleep
  2753. * @desc: gpio to check
  2754. *
  2755. */
  2756. int gpiod_cansleep(const struct gpio_desc *desc)
  2757. {
  2758. VALIDATE_DESC(desc);
  2759. return desc->gdev->chip->can_sleep;
  2760. }
  2761. EXPORT_SYMBOL_GPL(gpiod_cansleep);
  2762. /**
  2763. * gpiod_set_consumer_name() - set the consumer name for the descriptor
  2764. * @desc: gpio to set the consumer name on
  2765. * @name: the new consumer name
  2766. */
  2767. void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
  2768. {
  2769. VALIDATE_DESC_VOID(desc);
  2770. /* Just overwrite whatever the previous name was */
  2771. desc->label = name;
  2772. }
  2773. EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
  2774. /**
  2775. * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  2776. * @desc: gpio whose IRQ will be returned (already requested)
  2777. *
  2778. * Return the IRQ corresponding to the passed GPIO, or an error code in case of
  2779. * error.
  2780. */
  2781. int gpiod_to_irq(const struct gpio_desc *desc)
  2782. {
  2783. struct gpio_chip *chip;
  2784. int offset;
  2785. /*
  2786. * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
  2787. * requires this function to not return zero on an invalid descriptor
  2788. * but rather a negative error number.
  2789. */
  2790. if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
  2791. return -EINVAL;
  2792. chip = desc->gdev->chip;
  2793. offset = gpio_chip_hwgpio(desc);
  2794. if (chip->to_irq) {
  2795. int retirq = chip->to_irq(chip, offset);
  2796. /* Zero means NO_IRQ */
  2797. if (!retirq)
  2798. return -ENXIO;
  2799. return retirq;
  2800. }
  2801. return -ENXIO;
  2802. }
  2803. EXPORT_SYMBOL_GPL(gpiod_to_irq);
  2804. /**
  2805. * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
  2806. * @chip: the chip the GPIO to lock belongs to
  2807. * @offset: the offset of the GPIO to lock as IRQ
  2808. *
  2809. * This is used directly by GPIO drivers that want to lock down
  2810. * a certain GPIO line to be used for IRQs.
  2811. */
  2812. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
  2813. {
  2814. struct gpio_desc *desc;
  2815. desc = gpiochip_get_desc(chip, offset);
  2816. if (IS_ERR(desc))
  2817. return PTR_ERR(desc);
  2818. /*
  2819. * If it's fast: flush the direction setting if something changed
  2820. * behind our back
  2821. */
  2822. if (!chip->can_sleep && chip->get_direction) {
  2823. int dir = gpiod_get_direction(desc);
  2824. if (dir < 0) {
  2825. chip_err(chip, "%s: cannot get GPIO direction\n",
  2826. __func__);
  2827. return dir;
  2828. }
  2829. }
  2830. if (test_bit(FLAG_IS_OUT, &desc->flags)) {
  2831. chip_err(chip,
  2832. "%s: tried to flag a GPIO set as output for IRQ\n",
  2833. __func__);
  2834. return -EIO;
  2835. }
  2836. set_bit(FLAG_USED_AS_IRQ, &desc->flags);
  2837. /*
  2838. * If the consumer has not set up a label (such as when the
  2839. * IRQ is referenced from .to_irq()) we set up a label here
  2840. * so it is clear this is used as an interrupt.
  2841. */
  2842. if (!desc->label)
  2843. desc_set_label(desc, "interrupt");
  2844. return 0;
  2845. }
  2846. EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
  2847. /**
  2848. * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
  2849. * @chip: the chip the GPIO to lock belongs to
  2850. * @offset: the offset of the GPIO to lock as IRQ
  2851. *
  2852. * This is used directly by GPIO drivers that want to indicate
  2853. * that a certain GPIO is no longer used exclusively for IRQ.
  2854. */
  2855. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
  2856. {
  2857. struct gpio_desc *desc;
  2858. desc = gpiochip_get_desc(chip, offset);
  2859. if (IS_ERR(desc))
  2860. return;
  2861. clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
  2862. /* If we only had this marking, erase it */
  2863. if (desc->label && !strcmp(desc->label, "interrupt"))
  2864. desc_set_label(desc, NULL);
  2865. }
  2866. EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
  2867. bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
  2868. {
  2869. if (offset >= chip->ngpio)
  2870. return false;
  2871. return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
  2872. }
  2873. EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
  2874. bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
  2875. {
  2876. if (offset >= chip->ngpio)
  2877. return false;
  2878. return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
  2879. }
  2880. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
  2881. bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
  2882. {
  2883. if (offset >= chip->ngpio)
  2884. return false;
  2885. return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
  2886. }
  2887. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
  2888. bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
  2889. {
  2890. if (offset >= chip->ngpio)
  2891. return false;
  2892. return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
  2893. }
  2894. EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
  2895. /**
  2896. * gpiod_get_raw_value_cansleep() - return a gpio's raw value
  2897. * @desc: gpio whose value will be returned
  2898. *
  2899. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  2900. * its ACTIVE_LOW status, or negative errno on failure.
  2901. *
  2902. * This function is to be called from contexts that can sleep.
  2903. */
  2904. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  2905. {
  2906. might_sleep_if(extra_checks);
  2907. VALIDATE_DESC(desc);
  2908. return gpiod_get_raw_value_commit(desc);
  2909. }
  2910. EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
  2911. /**
  2912. * gpiod_get_value_cansleep() - return a gpio's value
  2913. * @desc: gpio whose value will be returned
  2914. *
  2915. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  2916. * account, or negative errno on failure.
  2917. *
  2918. * This function is to be called from contexts that can sleep.
  2919. */
  2920. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  2921. {
  2922. int value;
  2923. might_sleep_if(extra_checks);
  2924. VALIDATE_DESC(desc);
  2925. value = gpiod_get_raw_value_commit(desc);
  2926. if (value < 0)
  2927. return value;
  2928. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2929. value = !value;
  2930. return value;
  2931. }
  2932. EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
  2933. /**
  2934. * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
  2935. * @array_size: number of elements in the descriptor / value arrays
  2936. * @desc_array: array of GPIO descriptors whose values will be read
  2937. * @value_array: array to store the read values
  2938. *
  2939. * Read the raw values of the GPIOs, i.e. the values of the physical lines
  2940. * without regard for their ACTIVE_LOW status. Return 0 in case of success,
  2941. * else an error code.
  2942. *
  2943. * This function is to be called from contexts that can sleep.
  2944. */
  2945. int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
  2946. struct gpio_desc **desc_array,
  2947. int *value_array)
  2948. {
  2949. might_sleep_if(extra_checks);
  2950. if (!desc_array)
  2951. return -EINVAL;
  2952. return gpiod_get_array_value_complex(true, true, array_size,
  2953. desc_array, value_array);
  2954. }
  2955. EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
  2956. /**
  2957. * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
  2958. * @array_size: number of elements in the descriptor / value arrays
  2959. * @desc_array: array of GPIO descriptors whose values will be read
  2960. * @value_array: array to store the read values
  2961. *
  2962. * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2963. * into account. Return 0 in case of success, else an error code.
  2964. *
  2965. * This function is to be called from contexts that can sleep.
  2966. */
  2967. int gpiod_get_array_value_cansleep(unsigned int array_size,
  2968. struct gpio_desc **desc_array,
  2969. int *value_array)
  2970. {
  2971. might_sleep_if(extra_checks);
  2972. if (!desc_array)
  2973. return -EINVAL;
  2974. return gpiod_get_array_value_complex(false, true, array_size,
  2975. desc_array, value_array);
  2976. }
  2977. EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
  2978. /**
  2979. * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
  2980. * @desc: gpio whose value will be assigned
  2981. * @value: value to assign
  2982. *
  2983. * Set the raw value of the GPIO, i.e. the value of its physical line without
  2984. * regard for its ACTIVE_LOW status.
  2985. *
  2986. * This function is to be called from contexts that can sleep.
  2987. */
  2988. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  2989. {
  2990. might_sleep_if(extra_checks);
  2991. VALIDATE_DESC_VOID(desc);
  2992. gpiod_set_raw_value_commit(desc, value);
  2993. }
  2994. EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
  2995. /**
  2996. * gpiod_set_value_cansleep() - assign a gpio's value
  2997. * @desc: gpio whose value will be assigned
  2998. * @value: value to assign
  2999. *
  3000. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  3001. * account
  3002. *
  3003. * This function is to be called from contexts that can sleep.
  3004. */
  3005. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  3006. {
  3007. might_sleep_if(extra_checks);
  3008. VALIDATE_DESC_VOID(desc);
  3009. gpiod_set_value_nocheck(desc, value);
  3010. }
  3011. EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
  3012. /**
  3013. * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
  3014. * @array_size: number of elements in the descriptor / value arrays
  3015. * @desc_array: array of GPIO descriptors whose values will be assigned
  3016. * @value_array: array of values to assign
  3017. *
  3018. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  3019. * without regard for their ACTIVE_LOW status.
  3020. *
  3021. * This function is to be called from contexts that can sleep.
  3022. */
  3023. int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  3024. struct gpio_desc **desc_array,
  3025. int *value_array)
  3026. {
  3027. might_sleep_if(extra_checks);
  3028. if (!desc_array)
  3029. return -EINVAL;
  3030. return gpiod_set_array_value_complex(true, true, array_size, desc_array,
  3031. value_array);
  3032. }
  3033. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
  3034. /**
  3035. * gpiod_add_lookup_tables() - register GPIO device consumers
  3036. * @tables: list of tables of consumers to register
  3037. * @n: number of tables in the list
  3038. */
  3039. void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
  3040. {
  3041. unsigned int i;
  3042. mutex_lock(&gpio_lookup_lock);
  3043. for (i = 0; i < n; i++)
  3044. list_add_tail(&tables[i]->list, &gpio_lookup_list);
  3045. mutex_unlock(&gpio_lookup_lock);
  3046. }
  3047. /**
  3048. * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
  3049. * @array_size: number of elements in the descriptor / value arrays
  3050. * @desc_array: array of GPIO descriptors whose values will be assigned
  3051. * @value_array: array of values to assign
  3052. *
  3053. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  3054. * into account.
  3055. *
  3056. * This function is to be called from contexts that can sleep.
  3057. */
  3058. void gpiod_set_array_value_cansleep(unsigned int array_size,
  3059. struct gpio_desc **desc_array,
  3060. int *value_array)
  3061. {
  3062. might_sleep_if(extra_checks);
  3063. if (!desc_array)
  3064. return;
  3065. gpiod_set_array_value_complex(false, true, array_size, desc_array,
  3066. value_array);
  3067. }
  3068. EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
  3069. /**
  3070. * gpiod_add_lookup_table() - register GPIO device consumers
  3071. * @table: table of consumers to register
  3072. */
  3073. void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
  3074. {
  3075. mutex_lock(&gpio_lookup_lock);
  3076. list_add_tail(&table->list, &gpio_lookup_list);
  3077. mutex_unlock(&gpio_lookup_lock);
  3078. }
  3079. EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
  3080. /**
  3081. * gpiod_remove_lookup_table() - unregister GPIO device consumers
  3082. * @table: table of consumers to unregister
  3083. */
  3084. void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
  3085. {
  3086. mutex_lock(&gpio_lookup_lock);
  3087. list_del(&table->list);
  3088. mutex_unlock(&gpio_lookup_lock);
  3089. }
  3090. EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
  3091. /**
  3092. * gpiod_add_hogs() - register a set of GPIO hogs from machine code
  3093. * @hogs: table of gpio hog entries with a zeroed sentinel at the end
  3094. */
  3095. void gpiod_add_hogs(struct gpiod_hog *hogs)
  3096. {
  3097. struct gpio_chip *chip;
  3098. struct gpiod_hog *hog;
  3099. mutex_lock(&gpio_machine_hogs_mutex);
  3100. for (hog = &hogs[0]; hog->chip_label; hog++) {
  3101. list_add_tail(&hog->list, &gpio_machine_hogs);
  3102. /*
  3103. * The chip may have been registered earlier, so check if it
  3104. * exists and, if so, try to hog the line now.
  3105. */
  3106. chip = find_chip_by_name(hog->chip_label);
  3107. if (chip)
  3108. gpiochip_machine_hog(chip, hog);
  3109. }
  3110. mutex_unlock(&gpio_machine_hogs_mutex);
  3111. }
  3112. EXPORT_SYMBOL_GPL(gpiod_add_hogs);
  3113. static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
  3114. {
  3115. const char *dev_id = dev ? dev_name(dev) : NULL;
  3116. struct gpiod_lookup_table *table;
  3117. mutex_lock(&gpio_lookup_lock);
  3118. list_for_each_entry(table, &gpio_lookup_list, list) {
  3119. if (table->dev_id && dev_id) {
  3120. /*
  3121. * Valid strings on both ends, must be identical to have
  3122. * a match
  3123. */
  3124. if (!strcmp(table->dev_id, dev_id))
  3125. goto found;
  3126. } else {
  3127. /*
  3128. * One of the pointers is NULL, so both must be to have
  3129. * a match
  3130. */
  3131. if (dev_id == table->dev_id)
  3132. goto found;
  3133. }
  3134. }
  3135. table = NULL;
  3136. found:
  3137. mutex_unlock(&gpio_lookup_lock);
  3138. return table;
  3139. }
  3140. static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
  3141. unsigned int idx,
  3142. enum gpio_lookup_flags *flags)
  3143. {
  3144. struct gpio_desc *desc = ERR_PTR(-ENOENT);
  3145. struct gpiod_lookup_table *table;
  3146. struct gpiod_lookup *p;
  3147. table = gpiod_find_lookup_table(dev);
  3148. if (!table)
  3149. return desc;
  3150. for (p = &table->table[0]; p->chip_label; p++) {
  3151. struct gpio_chip *chip;
  3152. /* idx must always match exactly */
  3153. if (p->idx != idx)
  3154. continue;
  3155. /* If the lookup entry has a con_id, require exact match */
  3156. if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
  3157. continue;
  3158. chip = find_chip_by_name(p->chip_label);
  3159. if (!chip) {
  3160. /*
  3161. * As the lookup table indicates a chip with
  3162. * p->chip_label should exist, assume it may
  3163. * still appear later and let the interested
  3164. * consumer be probed again or let the Deferred
  3165. * Probe infrastructure handle the error.
  3166. */
  3167. dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
  3168. p->chip_label);
  3169. return ERR_PTR(-EPROBE_DEFER);
  3170. }
  3171. if (chip->ngpio <= p->chip_hwnum) {
  3172. dev_err(dev,
  3173. "requested GPIO %d is out of range [0..%d] for chip %s\n",
  3174. idx, chip->ngpio, chip->label);
  3175. return ERR_PTR(-EINVAL);
  3176. }
  3177. desc = gpiochip_get_desc(chip, p->chip_hwnum);
  3178. *flags = p->flags;
  3179. return desc;
  3180. }
  3181. return desc;
  3182. }
  3183. static int dt_gpio_count(struct device *dev, const char *con_id)
  3184. {
  3185. int ret;
  3186. char propname[32];
  3187. unsigned int i;
  3188. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  3189. if (con_id)
  3190. snprintf(propname, sizeof(propname), "%s-%s",
  3191. con_id, gpio_suffixes[i]);
  3192. else
  3193. snprintf(propname, sizeof(propname), "%s",
  3194. gpio_suffixes[i]);
  3195. ret = of_gpio_named_count(dev->of_node, propname);
  3196. if (ret > 0)
  3197. break;
  3198. }
  3199. return ret ? ret : -ENOENT;
  3200. }
  3201. static int platform_gpio_count(struct device *dev, const char *con_id)
  3202. {
  3203. struct gpiod_lookup_table *table;
  3204. struct gpiod_lookup *p;
  3205. unsigned int count = 0;
  3206. table = gpiod_find_lookup_table(dev);
  3207. if (!table)
  3208. return -ENOENT;
  3209. for (p = &table->table[0]; p->chip_label; p++) {
  3210. if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
  3211. (!con_id && !p->con_id))
  3212. count++;
  3213. }
  3214. if (!count)
  3215. return -ENOENT;
  3216. return count;
  3217. }
  3218. /**
  3219. * gpiod_count - return the number of GPIOs associated with a device / function
  3220. * or -ENOENT if no GPIO has been assigned to the requested function
  3221. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3222. * @con_id: function within the GPIO consumer
  3223. */
  3224. int gpiod_count(struct device *dev, const char *con_id)
  3225. {
  3226. int count = -ENOENT;
  3227. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  3228. count = dt_gpio_count(dev, con_id);
  3229. else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
  3230. count = acpi_gpio_count(dev, con_id);
  3231. if (count < 0)
  3232. count = platform_gpio_count(dev, con_id);
  3233. return count;
  3234. }
  3235. EXPORT_SYMBOL_GPL(gpiod_count);
  3236. /**
  3237. * gpiod_get - obtain a GPIO for a given GPIO function
  3238. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3239. * @con_id: function within the GPIO consumer
  3240. * @flags: optional GPIO initialization flags
  3241. *
  3242. * Return the GPIO descriptor corresponding to the function con_id of device
  3243. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  3244. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  3245. */
  3246. struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
  3247. enum gpiod_flags flags)
  3248. {
  3249. return gpiod_get_index(dev, con_id, 0, flags);
  3250. }
  3251. EXPORT_SYMBOL_GPL(gpiod_get);
  3252. /**
  3253. * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
  3254. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3255. * @con_id: function within the GPIO consumer
  3256. * @flags: optional GPIO initialization flags
  3257. *
  3258. * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
  3259. * the requested function it will return NULL. This is convenient for drivers
  3260. * that need to handle optional GPIOs.
  3261. */
  3262. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  3263. const char *con_id,
  3264. enum gpiod_flags flags)
  3265. {
  3266. return gpiod_get_index_optional(dev, con_id, 0, flags);
  3267. }
  3268. EXPORT_SYMBOL_GPL(gpiod_get_optional);
  3269. /**
  3270. * gpiod_configure_flags - helper function to configure a given GPIO
  3271. * @desc: gpio whose value will be assigned
  3272. * @con_id: function within the GPIO consumer
  3273. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  3274. * of_get_gpio_hog()
  3275. * @dflags: gpiod_flags - optional GPIO initialization flags
  3276. *
  3277. * Return 0 on success, -ENOENT if no GPIO has been assigned to the
  3278. * requested function and/or index, or another IS_ERR() code if an error
  3279. * occurred while trying to acquire the GPIO.
  3280. */
  3281. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  3282. unsigned long lflags, enum gpiod_flags dflags)
  3283. {
  3284. int status;
  3285. if (lflags & GPIO_ACTIVE_LOW)
  3286. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  3287. if (lflags & GPIO_OPEN_DRAIN)
  3288. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  3289. else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
  3290. /*
  3291. * This enforces open drain mode from the consumer side.
  3292. * This is necessary for some busses like I2C, but the lookup
  3293. * should *REALLY* have specified them as open drain in the
  3294. * first place, so print a little warning here.
  3295. */
  3296. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  3297. gpiod_warn(desc,
  3298. "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
  3299. }
  3300. if (lflags & GPIO_OPEN_SOURCE)
  3301. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  3302. status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
  3303. if (status < 0)
  3304. return status;
  3305. /* No particular flag request, return here... */
  3306. if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
  3307. pr_debug("no flags found for %s\n", con_id);
  3308. return 0;
  3309. }
  3310. /* Process flags */
  3311. if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
  3312. status = gpiod_direction_output(desc,
  3313. !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
  3314. else
  3315. status = gpiod_direction_input(desc);
  3316. return status;
  3317. }
  3318. /**
  3319. * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
  3320. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3321. * @con_id: function within the GPIO consumer
  3322. * @idx: index of the GPIO to obtain in the consumer
  3323. * @flags: optional GPIO initialization flags
  3324. *
  3325. * This variant of gpiod_get() allows to access GPIOs other than the first
  3326. * defined one for functions that define several GPIOs.
  3327. *
  3328. * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
  3329. * requested function and/or index, or another IS_ERR() code if an error
  3330. * occurred while trying to acquire the GPIO.
  3331. */
  3332. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  3333. const char *con_id,
  3334. unsigned int idx,
  3335. enum gpiod_flags flags)
  3336. {
  3337. struct gpio_desc *desc = NULL;
  3338. int status;
  3339. enum gpio_lookup_flags lookupflags = 0;
  3340. /* Maybe we have a device name, maybe not */
  3341. const char *devname = dev ? dev_name(dev) : "?";
  3342. dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
  3343. if (dev) {
  3344. /* Using device tree? */
  3345. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  3346. dev_dbg(dev, "using device tree for GPIO lookup\n");
  3347. desc = of_find_gpio(dev, con_id, idx, &lookupflags);
  3348. } else if (ACPI_COMPANION(dev)) {
  3349. dev_dbg(dev, "using ACPI for GPIO lookup\n");
  3350. desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
  3351. }
  3352. }
  3353. /*
  3354. * Either we are not using DT or ACPI, or their lookup did not return
  3355. * a result. In that case, use platform lookup as a fallback.
  3356. */
  3357. if (!desc || desc == ERR_PTR(-ENOENT)) {
  3358. dev_dbg(dev, "using lookup tables for GPIO lookup\n");
  3359. desc = gpiod_find(dev, con_id, idx, &lookupflags);
  3360. }
  3361. if (IS_ERR(desc)) {
  3362. dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
  3363. return desc;
  3364. }
  3365. /*
  3366. * If a connection label was passed use that, else attempt to use
  3367. * the device name as label
  3368. */
  3369. status = gpiod_request(desc, con_id ? con_id : devname);
  3370. if (status < 0)
  3371. return ERR_PTR(status);
  3372. status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
  3373. if (status < 0) {
  3374. dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
  3375. gpiod_put(desc);
  3376. return ERR_PTR(status);
  3377. }
  3378. return desc;
  3379. }
  3380. EXPORT_SYMBOL_GPL(gpiod_get_index);
  3381. /**
  3382. * gpiod_get_from_of_node() - obtain a GPIO from an OF node
  3383. * @node: handle of the OF node
  3384. * @propname: name of the DT property representing the GPIO
  3385. * @index: index of the GPIO to obtain for the consumer
  3386. * @dflags: GPIO initialization flags
  3387. * @label: label to attach to the requested GPIO
  3388. *
  3389. * Returns:
  3390. * On successful request the GPIO pin is configured in accordance with
  3391. * provided @dflags. If the node does not have the requested GPIO
  3392. * property, NULL is returned.
  3393. *
  3394. * In case of error an ERR_PTR() is returned.
  3395. */
  3396. struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
  3397. const char *propname, int index,
  3398. enum gpiod_flags dflags,
  3399. const char *label)
  3400. {
  3401. struct gpio_desc *desc;
  3402. unsigned long lflags = 0;
  3403. enum of_gpio_flags flags;
  3404. bool active_low = false;
  3405. bool single_ended = false;
  3406. bool open_drain = false;
  3407. bool transitory = false;
  3408. int ret;
  3409. desc = of_get_named_gpiod_flags(node, propname,
  3410. index, &flags);
  3411. if (!desc || IS_ERR(desc)) {
  3412. /* If it is not there, just return NULL */
  3413. if (PTR_ERR(desc) == -ENOENT)
  3414. return NULL;
  3415. return desc;
  3416. }
  3417. active_low = flags & OF_GPIO_ACTIVE_LOW;
  3418. single_ended = flags & OF_GPIO_SINGLE_ENDED;
  3419. open_drain = flags & OF_GPIO_OPEN_DRAIN;
  3420. transitory = flags & OF_GPIO_TRANSITORY;
  3421. ret = gpiod_request(desc, label);
  3422. if (ret)
  3423. return ERR_PTR(ret);
  3424. if (active_low)
  3425. lflags |= GPIO_ACTIVE_LOW;
  3426. if (single_ended) {
  3427. if (open_drain)
  3428. lflags |= GPIO_OPEN_DRAIN;
  3429. else
  3430. lflags |= GPIO_OPEN_SOURCE;
  3431. }
  3432. if (transitory)
  3433. lflags |= GPIO_TRANSITORY;
  3434. ret = gpiod_configure_flags(desc, propname, lflags, dflags);
  3435. if (ret < 0) {
  3436. gpiod_put(desc);
  3437. return ERR_PTR(ret);
  3438. }
  3439. return desc;
  3440. }
  3441. EXPORT_SYMBOL(gpiod_get_from_of_node);
  3442. /**
  3443. * fwnode_get_named_gpiod - obtain a GPIO from firmware node
  3444. * @fwnode: handle of the firmware node
  3445. * @propname: name of the firmware property representing the GPIO
  3446. * @index: index of the GPIO to obtain for the consumer
  3447. * @dflags: GPIO initialization flags
  3448. * @label: label to attach to the requested GPIO
  3449. *
  3450. * This function can be used for drivers that get their configuration
  3451. * from opaque firmware.
  3452. *
  3453. * The function properly finds the corresponding GPIO using whatever is the
  3454. * underlying firmware interface and then makes sure that the GPIO
  3455. * descriptor is requested before it is returned to the caller.
  3456. *
  3457. * Returns:
  3458. * On successful request the GPIO pin is configured in accordance with
  3459. * provided @dflags.
  3460. *
  3461. * In case of error an ERR_PTR() is returned.
  3462. */
  3463. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  3464. const char *propname, int index,
  3465. enum gpiod_flags dflags,
  3466. const char *label)
  3467. {
  3468. struct gpio_desc *desc = ERR_PTR(-ENODEV);
  3469. unsigned long lflags = 0;
  3470. int ret;
  3471. if (!fwnode)
  3472. return ERR_PTR(-EINVAL);
  3473. if (is_of_node(fwnode)) {
  3474. desc = gpiod_get_from_of_node(to_of_node(fwnode),
  3475. propname, index,
  3476. dflags,
  3477. label);
  3478. return desc;
  3479. } else if (is_acpi_node(fwnode)) {
  3480. struct acpi_gpio_info info;
  3481. desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
  3482. if (IS_ERR(desc))
  3483. return desc;
  3484. acpi_gpio_update_gpiod_flags(&dflags, &info);
  3485. if (info.polarity == GPIO_ACTIVE_LOW)
  3486. lflags |= GPIO_ACTIVE_LOW;
  3487. }
  3488. /* Currently only ACPI takes this path */
  3489. ret = gpiod_request(desc, label);
  3490. if (ret)
  3491. return ERR_PTR(ret);
  3492. ret = gpiod_configure_flags(desc, propname, lflags, dflags);
  3493. if (ret < 0) {
  3494. gpiod_put(desc);
  3495. return ERR_PTR(ret);
  3496. }
  3497. return desc;
  3498. }
  3499. EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
  3500. /**
  3501. * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
  3502. * function
  3503. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3504. * @con_id: function within the GPIO consumer
  3505. * @index: index of the GPIO to obtain in the consumer
  3506. * @flags: optional GPIO initialization flags
  3507. *
  3508. * This is equivalent to gpiod_get_index(), except that when no GPIO with the
  3509. * specified index was assigned to the requested function it will return NULL.
  3510. * This is convenient for drivers that need to handle optional GPIOs.
  3511. */
  3512. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  3513. const char *con_id,
  3514. unsigned int index,
  3515. enum gpiod_flags flags)
  3516. {
  3517. struct gpio_desc *desc;
  3518. desc = gpiod_get_index(dev, con_id, index, flags);
  3519. if (IS_ERR(desc)) {
  3520. if (PTR_ERR(desc) == -ENOENT)
  3521. return NULL;
  3522. }
  3523. return desc;
  3524. }
  3525. EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
  3526. /**
  3527. * gpiod_hog - Hog the specified GPIO desc given the provided flags
  3528. * @desc: gpio whose value will be assigned
  3529. * @name: gpio line name
  3530. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  3531. * of_get_gpio_hog()
  3532. * @dflags: gpiod_flags - optional GPIO initialization flags
  3533. */
  3534. int gpiod_hog(struct gpio_desc *desc, const char *name,
  3535. unsigned long lflags, enum gpiod_flags dflags)
  3536. {
  3537. struct gpio_chip *chip;
  3538. struct gpio_desc *local_desc;
  3539. int hwnum;
  3540. int status;
  3541. chip = gpiod_to_chip(desc);
  3542. hwnum = gpio_chip_hwgpio(desc);
  3543. local_desc = gpiochip_request_own_desc(chip, hwnum, name);
  3544. if (IS_ERR(local_desc)) {
  3545. status = PTR_ERR(local_desc);
  3546. pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
  3547. name, chip->label, hwnum, status);
  3548. return status;
  3549. }
  3550. status = gpiod_configure_flags(desc, name, lflags, dflags);
  3551. if (status < 0) {
  3552. pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
  3553. name, chip->label, hwnum, status);
  3554. gpiochip_free_own_desc(desc);
  3555. return status;
  3556. }
  3557. /* Mark GPIO as hogged so it can be identified and removed later */
  3558. set_bit(FLAG_IS_HOGGED, &desc->flags);
  3559. pr_info("GPIO line %d (%s) hogged as %s%s\n",
  3560. desc_to_gpio(desc), name,
  3561. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
  3562. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
  3563. (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
  3564. return 0;
  3565. }
  3566. /**
  3567. * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
  3568. * @chip: gpio chip to act on
  3569. *
  3570. * This is only used by of_gpiochip_remove to free hogged gpios
  3571. */
  3572. static void gpiochip_free_hogs(struct gpio_chip *chip)
  3573. {
  3574. int id;
  3575. for (id = 0; id < chip->ngpio; id++) {
  3576. if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
  3577. gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
  3578. }
  3579. }
  3580. /**
  3581. * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
  3582. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3583. * @con_id: function within the GPIO consumer
  3584. * @flags: optional GPIO initialization flags
  3585. *
  3586. * This function acquires all the GPIOs defined under a given function.
  3587. *
  3588. * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
  3589. * no GPIO has been assigned to the requested function, or another IS_ERR()
  3590. * code if an error occurred while trying to acquire the GPIOs.
  3591. */
  3592. struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
  3593. const char *con_id,
  3594. enum gpiod_flags flags)
  3595. {
  3596. struct gpio_desc *desc;
  3597. struct gpio_descs *descs;
  3598. int count;
  3599. count = gpiod_count(dev, con_id);
  3600. if (count < 0)
  3601. return ERR_PTR(count);
  3602. descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
  3603. if (!descs)
  3604. return ERR_PTR(-ENOMEM);
  3605. for (descs->ndescs = 0; descs->ndescs < count; ) {
  3606. desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
  3607. if (IS_ERR(desc)) {
  3608. gpiod_put_array(descs);
  3609. return ERR_CAST(desc);
  3610. }
  3611. descs->desc[descs->ndescs] = desc;
  3612. descs->ndescs++;
  3613. }
  3614. return descs;
  3615. }
  3616. EXPORT_SYMBOL_GPL(gpiod_get_array);
  3617. /**
  3618. * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
  3619. * function
  3620. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3621. * @con_id: function within the GPIO consumer
  3622. * @flags: optional GPIO initialization flags
  3623. *
  3624. * This is equivalent to gpiod_get_array(), except that when no GPIO was
  3625. * assigned to the requested function it will return NULL.
  3626. */
  3627. struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
  3628. const char *con_id,
  3629. enum gpiod_flags flags)
  3630. {
  3631. struct gpio_descs *descs;
  3632. descs = gpiod_get_array(dev, con_id, flags);
  3633. if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
  3634. return NULL;
  3635. return descs;
  3636. }
  3637. EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
  3638. /**
  3639. * gpiod_put - dispose of a GPIO descriptor
  3640. * @desc: GPIO descriptor to dispose of
  3641. *
  3642. * No descriptor can be used after gpiod_put() has been called on it.
  3643. */
  3644. void gpiod_put(struct gpio_desc *desc)
  3645. {
  3646. gpiod_free(desc);
  3647. }
  3648. EXPORT_SYMBOL_GPL(gpiod_put);
  3649. /**
  3650. * gpiod_put_array - dispose of multiple GPIO descriptors
  3651. * @descs: struct gpio_descs containing an array of descriptors
  3652. */
  3653. void gpiod_put_array(struct gpio_descs *descs)
  3654. {
  3655. unsigned int i;
  3656. for (i = 0; i < descs->ndescs; i++)
  3657. gpiod_put(descs->desc[i]);
  3658. kfree(descs);
  3659. }
  3660. EXPORT_SYMBOL_GPL(gpiod_put_array);
  3661. static int __init gpiolib_dev_init(void)
  3662. {
  3663. int ret;
  3664. /* Register GPIO sysfs bus */
  3665. ret = bus_register(&gpio_bus_type);
  3666. if (ret < 0) {
  3667. pr_err("gpiolib: could not register GPIO bus type\n");
  3668. return ret;
  3669. }
  3670. ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
  3671. if (ret < 0) {
  3672. pr_err("gpiolib: failed to allocate char dev region\n");
  3673. bus_unregister(&gpio_bus_type);
  3674. } else {
  3675. gpiolib_initialized = true;
  3676. gpiochip_setup_devs();
  3677. }
  3678. return ret;
  3679. }
  3680. core_initcall(gpiolib_dev_init);
  3681. #ifdef CONFIG_DEBUG_FS
  3682. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
  3683. {
  3684. unsigned i;
  3685. struct gpio_chip *chip = gdev->chip;
  3686. unsigned gpio = gdev->base;
  3687. struct gpio_desc *gdesc = &gdev->descs[0];
  3688. int is_out;
  3689. int is_irq;
  3690. for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
  3691. if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
  3692. if (gdesc->name) {
  3693. seq_printf(s, " gpio-%-3d (%-20.20s)\n",
  3694. gpio, gdesc->name);
  3695. }
  3696. continue;
  3697. }
  3698. gpiod_get_direction(gdesc);
  3699. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  3700. is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
  3701. seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
  3702. gpio, gdesc->name ? gdesc->name : "", gdesc->label,
  3703. is_out ? "out" : "in ",
  3704. chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
  3705. is_irq ? "IRQ" : " ");
  3706. seq_printf(s, "\n");
  3707. }
  3708. }
  3709. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  3710. {
  3711. unsigned long flags;
  3712. struct gpio_device *gdev = NULL;
  3713. loff_t index = *pos;
  3714. s->private = "";
  3715. spin_lock_irqsave(&gpio_lock, flags);
  3716. list_for_each_entry(gdev, &gpio_devices, list)
  3717. if (index-- == 0) {
  3718. spin_unlock_irqrestore(&gpio_lock, flags);
  3719. return gdev;
  3720. }
  3721. spin_unlock_irqrestore(&gpio_lock, flags);
  3722. return NULL;
  3723. }
  3724. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  3725. {
  3726. unsigned long flags;
  3727. struct gpio_device *gdev = v;
  3728. void *ret = NULL;
  3729. spin_lock_irqsave(&gpio_lock, flags);
  3730. if (list_is_last(&gdev->list, &gpio_devices))
  3731. ret = NULL;
  3732. else
  3733. ret = list_entry(gdev->list.next, struct gpio_device, list);
  3734. spin_unlock_irqrestore(&gpio_lock, flags);
  3735. s->private = "\n";
  3736. ++*pos;
  3737. return ret;
  3738. }
  3739. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  3740. {
  3741. }
  3742. static int gpiolib_seq_show(struct seq_file *s, void *v)
  3743. {
  3744. struct gpio_device *gdev = v;
  3745. struct gpio_chip *chip = gdev->chip;
  3746. struct device *parent;
  3747. if (!chip) {
  3748. seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
  3749. dev_name(&gdev->dev));
  3750. return 0;
  3751. }
  3752. seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
  3753. dev_name(&gdev->dev),
  3754. gdev->base, gdev->base + gdev->ngpio - 1);
  3755. parent = chip->parent;
  3756. if (parent)
  3757. seq_printf(s, ", parent: %s/%s",
  3758. parent->bus ? parent->bus->name : "no-bus",
  3759. dev_name(parent));
  3760. if (chip->label)
  3761. seq_printf(s, ", %s", chip->label);
  3762. if (chip->can_sleep)
  3763. seq_printf(s, ", can sleep");
  3764. seq_printf(s, ":\n");
  3765. if (chip->dbg_show)
  3766. chip->dbg_show(s, chip);
  3767. else
  3768. gpiolib_dbg_show(s, gdev);
  3769. return 0;
  3770. }
  3771. static const struct seq_operations gpiolib_seq_ops = {
  3772. .start = gpiolib_seq_start,
  3773. .next = gpiolib_seq_next,
  3774. .stop = gpiolib_seq_stop,
  3775. .show = gpiolib_seq_show,
  3776. };
  3777. static int gpiolib_open(struct inode *inode, struct file *file)
  3778. {
  3779. return seq_open(file, &gpiolib_seq_ops);
  3780. }
  3781. static const struct file_operations gpiolib_operations = {
  3782. .owner = THIS_MODULE,
  3783. .open = gpiolib_open,
  3784. .read = seq_read,
  3785. .llseek = seq_lseek,
  3786. .release = seq_release,
  3787. };
  3788. static int __init gpiolib_debugfs_init(void)
  3789. {
  3790. /* /sys/kernel/debug/gpio */
  3791. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  3792. NULL, NULL, &gpiolib_operations);
  3793. return 0;
  3794. }
  3795. subsys_initcall(gpiolib_debugfs_init);
  3796. #endif /* DEBUG_FS */