gpiolib.c 96 KB

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