gpiolib.c 64 KB

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