gpiolib.c 98 KB

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