gpiolib.c 120 KB

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