gpiolib.c 105 KB

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