gpiolib.c 83 KB

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