gpiolib.c 76 KB

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