gpiolib.c 113 KB

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