gpiolib.c 88 KB

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