gpiolib.c 64 KB

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