gpiolib.c 108 KB

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