gpiolib.c 113 KB

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