gpiolib.c 112 KB

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