gpiolib.c 99 KB

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