gpiolib.c 98 KB

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