gpiolib.c 94 KB

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