gpiolib.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/irq.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/list.h>
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/gpio.h>
  12. #include <linux/of_gpio.h>
  13. #include <linux/idr.h>
  14. #include <linux/slab.h>
  15. #define CREATE_TRACE_POINTS
  16. #include <trace/events/gpio.h>
  17. /* Optional implementation infrastructure for GPIO interfaces.
  18. *
  19. * Platforms may want to use this if they tend to use very many GPIOs
  20. * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
  21. *
  22. * When kernel footprint or instruction count is an issue, simpler
  23. * implementations may be preferred. The GPIO programming interface
  24. * allows for inlining speed-critical get/set operations for common
  25. * cases, so that access to SOC-integrated GPIOs can sometimes cost
  26. * only an instruction or two per bit.
  27. */
  28. /* When debugging, extend minimal trust to callers and platform code.
  29. * Also emit diagnostic messages that may help initial bringup, when
  30. * board setup or driver bugs are most common.
  31. *
  32. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  33. */
  34. #ifdef DEBUG
  35. #define extra_checks 1
  36. #else
  37. #define extra_checks 0
  38. #endif
  39. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  40. * While any GPIO is requested, its gpio_chip is not removable;
  41. * each GPIO's "requested" flag serves as a lock and refcount.
  42. */
  43. static DEFINE_SPINLOCK(gpio_lock);
  44. struct gpio_desc {
  45. struct gpio_chip *chip;
  46. unsigned long flags;
  47. /* flag symbols are bit numbers */
  48. #define FLAG_REQUESTED 0
  49. #define FLAG_IS_OUT 1
  50. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  51. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  52. #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
  53. #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
  54. #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
  55. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  56. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  57. #define ID_SHIFT 16 /* add new flags before this one */
  58. #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
  59. #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
  60. #ifdef CONFIG_DEBUG_FS
  61. const char *label;
  62. #endif
  63. };
  64. static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
  65. #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
  66. static LIST_HEAD(gpio_chips);
  67. #ifdef CONFIG_GPIO_SYSFS
  68. static DEFINE_IDR(dirent_idr);
  69. #endif
  70. /*
  71. * Internal gpiod_* API using descriptors instead of the integer namespace.
  72. * Most of this should eventually go public.
  73. */
  74. static int gpiod_request(struct gpio_desc *desc, const char *label);
  75. static void gpiod_free(struct gpio_desc *desc);
  76. static int gpiod_direction_input(struct gpio_desc *desc);
  77. static int gpiod_direction_output(struct gpio_desc *desc, int value);
  78. static int gpiod_get_direction(const struct gpio_desc *desc);
  79. static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
  80. static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
  81. static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
  82. static int gpiod_get_value(const struct gpio_desc *desc);
  83. static void gpiod_set_value(struct gpio_desc *desc, int value);
  84. static int gpiod_cansleep(const struct gpio_desc *desc);
  85. static int gpiod_to_irq(const struct gpio_desc *desc);
  86. static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  87. static int gpiod_export_link(struct device *dev, const char *name,
  88. struct gpio_desc *desc);
  89. static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
  90. static void gpiod_unexport(struct gpio_desc *desc);
  91. #ifdef CONFIG_DEBUG_FS
  92. #define gpiod_emerg(desc, fmt, ...) \
  93. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
  94. ##__VA_ARGS__)
  95. #define gpiod_crit(desc, fmt, ...) \
  96. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
  97. ##__VA_ARGS__)
  98. #define gpiod_err(desc, fmt, ...) \
  99. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
  100. ##__VA_ARGS__)
  101. #define gpiod_warn(desc, fmt, ...) \
  102. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
  103. ##__VA_ARGS__)
  104. #define gpiod_info(desc, fmt, ...) \
  105. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
  106. ##__VA_ARGS__)
  107. #define gpiod_dbg(desc, fmt, ...) \
  108. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
  109. ##__VA_ARGS__)
  110. #else
  111. #define gpiod_emerg(desc, fmt, ...) \
  112. pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  113. #define gpiod_crit(desc, fmt, ...) \
  114. pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  115. #define gpiod_err(desc, fmt, ...) \
  116. pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  117. #define gpiod_warn(desc, fmt, ...) \
  118. pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  119. #define gpiod_info(desc, fmt, ...) \
  120. pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  121. #define gpiod_dbg(desc, fmt, ...) \
  122. pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  123. #endif
  124. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  125. {
  126. #ifdef CONFIG_DEBUG_FS
  127. d->label = label;
  128. #endif
  129. }
  130. /*
  131. * Return the GPIO number of the passed descriptor relative to its chip
  132. */
  133. static int gpio_chip_hwgpio(const struct gpio_desc *desc)
  134. {
  135. return desc - &desc->chip->desc[0];
  136. }
  137. /**
  138. * Convert a GPIO number to its descriptor
  139. */
  140. static struct gpio_desc *gpio_to_desc(unsigned gpio)
  141. {
  142. if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
  143. return NULL;
  144. else
  145. return &gpio_desc[gpio];
  146. }
  147. /**
  148. * Convert a GPIO descriptor to the integer namespace.
  149. * This should disappear in the future but is needed since we still
  150. * use GPIO numbers for error messages and sysfs nodes
  151. */
  152. static int desc_to_gpio(const struct gpio_desc *desc)
  153. {
  154. return desc->chip->base + gpio_chip_hwgpio(desc);
  155. }
  156. /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
  157. * when setting direction, and otherwise illegal. Until board setup code
  158. * and drivers use explicit requests everywhere (which won't happen when
  159. * those calls have no teeth) we can't avoid autorequesting. This nag
  160. * message should motivate switching to explicit requests... so should
  161. * the weaker cleanup after faults, compared to gpio_request().
  162. *
  163. * NOTE: the autorequest mechanism is going away; at this point it's
  164. * only "legal" in the sense that (old) code using it won't break yet,
  165. * but instead only triggers a WARN() stack dump.
  166. */
  167. static int gpio_ensure_requested(struct gpio_desc *desc)
  168. {
  169. const struct gpio_chip *chip = desc->chip;
  170. const int gpio = desc_to_gpio(desc);
  171. if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
  172. "autorequest GPIO-%d\n", gpio)) {
  173. if (!try_module_get(chip->owner)) {
  174. pr_err("GPIO-%d: module can't be gotten \n", gpio);
  175. clear_bit(FLAG_REQUESTED, &desc->flags);
  176. /* lose */
  177. return -EIO;
  178. }
  179. desc_set_label(desc, "[auto]");
  180. /* caller must chip->request() w/o spinlock */
  181. if (chip->request)
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  187. {
  188. return desc ? desc->chip : NULL;
  189. }
  190. /* caller holds gpio_lock *OR* gpio is marked as requested */
  191. struct gpio_chip *gpio_to_chip(unsigned gpio)
  192. {
  193. return gpiod_to_chip(gpio_to_desc(gpio));
  194. }
  195. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  196. static int gpiochip_find_base(int ngpio)
  197. {
  198. struct gpio_chip *chip;
  199. int base = ARCH_NR_GPIOS - ngpio;
  200. list_for_each_entry_reverse(chip, &gpio_chips, list) {
  201. /* found a free space? */
  202. if (chip->base + chip->ngpio <= base)
  203. break;
  204. else
  205. /* nope, check the space right before the chip */
  206. base = chip->base - ngpio;
  207. }
  208. if (gpio_is_valid(base)) {
  209. pr_debug("%s: found new base at %d\n", __func__, base);
  210. return base;
  211. } else {
  212. pr_err("%s: cannot find free range\n", __func__);
  213. return -ENOSPC;
  214. }
  215. }
  216. /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
  217. static 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. #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. int value;
  313. value = !!gpiod_get_value_cansleep(desc);
  314. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  315. value = !value;
  316. status = sprintf(buf, "%d\n", value);
  317. }
  318. mutex_unlock(&sysfs_lock);
  319. return status;
  320. }
  321. static ssize_t gpio_value_store(struct device *dev,
  322. struct device_attribute *attr, const char *buf, size_t size)
  323. {
  324. struct gpio_desc *desc = dev_get_drvdata(dev);
  325. ssize_t status;
  326. mutex_lock(&sysfs_lock);
  327. if (!test_bit(FLAG_EXPORT, &desc->flags))
  328. status = -EIO;
  329. else if (!test_bit(FLAG_IS_OUT, &desc->flags))
  330. status = -EPERM;
  331. else {
  332. long value;
  333. status = kstrtol(buf, 0, &value);
  334. if (status == 0) {
  335. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  336. value = !value;
  337. gpiod_set_value_cansleep(desc, value != 0);
  338. status = size;
  339. }
  340. }
  341. mutex_unlock(&sysfs_lock);
  342. return status;
  343. }
  344. static const DEVICE_ATTR(value, 0644,
  345. gpio_value_show, gpio_value_store);
  346. static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
  347. {
  348. struct sysfs_dirent *value_sd = priv;
  349. sysfs_notify_dirent(value_sd);
  350. return IRQ_HANDLED;
  351. }
  352. static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
  353. unsigned long gpio_flags)
  354. {
  355. struct sysfs_dirent *value_sd;
  356. unsigned long irq_flags;
  357. int ret, irq, id;
  358. if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
  359. return 0;
  360. irq = gpiod_to_irq(desc);
  361. if (irq < 0)
  362. return -EIO;
  363. id = desc->flags >> ID_SHIFT;
  364. value_sd = idr_find(&dirent_idr, id);
  365. if (value_sd)
  366. free_irq(irq, value_sd);
  367. desc->flags &= ~GPIO_TRIGGER_MASK;
  368. if (!gpio_flags) {
  369. ret = 0;
  370. goto free_id;
  371. }
  372. irq_flags = IRQF_SHARED;
  373. if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
  374. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  375. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  376. if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
  377. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  378. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  379. if (!value_sd) {
  380. value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
  381. if (!value_sd) {
  382. ret = -ENODEV;
  383. goto err_out;
  384. }
  385. ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
  386. if (ret < 0)
  387. goto free_sd;
  388. id = ret;
  389. desc->flags &= GPIO_FLAGS_MASK;
  390. desc->flags |= (unsigned long)id << ID_SHIFT;
  391. if (desc->flags >> ID_SHIFT != id) {
  392. ret = -ERANGE;
  393. goto free_id;
  394. }
  395. }
  396. ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
  397. "gpiolib", value_sd);
  398. if (ret < 0)
  399. goto free_id;
  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. * gpio_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. static 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. int gpio_export(unsigned gpio, bool direction_may_change)
  725. {
  726. return gpiod_export(gpio_to_desc(gpio), direction_may_change);
  727. }
  728. EXPORT_SYMBOL_GPL(gpio_export);
  729. static int match_export(struct device *dev, const void *data)
  730. {
  731. return dev_get_drvdata(dev) == data;
  732. }
  733. /**
  734. * gpio_export_link - create a sysfs link to an exported GPIO node
  735. * @dev: device under which to create symlink
  736. * @name: name of the symlink
  737. * @gpio: gpio to create symlink to, already exported
  738. *
  739. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  740. * node. Caller is responsible for unlinking.
  741. *
  742. * Returns zero on success, else an error.
  743. */
  744. static int gpiod_export_link(struct device *dev, const char *name,
  745. struct gpio_desc *desc)
  746. {
  747. int status = -EINVAL;
  748. if (!desc) {
  749. pr_warn("%s: invalid GPIO\n", __func__);
  750. return -EINVAL;
  751. }
  752. mutex_lock(&sysfs_lock);
  753. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  754. struct device *tdev;
  755. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  756. if (tdev != NULL) {
  757. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  758. name);
  759. } else {
  760. status = -ENODEV;
  761. }
  762. }
  763. mutex_unlock(&sysfs_lock);
  764. if (status)
  765. pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
  766. status);
  767. return status;
  768. }
  769. int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
  770. {
  771. return gpiod_export_link(dev, name, gpio_to_desc(gpio));
  772. }
  773. EXPORT_SYMBOL_GPL(gpio_export_link);
  774. /**
  775. * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
  776. * @gpio: gpio to change
  777. * @value: non-zero to use active low, i.e. inverted values
  778. *
  779. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  780. * The GPIO does not have to be exported yet. If poll(2) support has
  781. * been enabled for either rising or falling edge, it will be
  782. * reconfigured to follow the new polarity.
  783. *
  784. * Returns zero on success, else an error.
  785. */
  786. static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  787. {
  788. struct device *dev = NULL;
  789. int status = -EINVAL;
  790. if (!desc) {
  791. pr_warn("%s: invalid GPIO\n", __func__);
  792. return -EINVAL;
  793. }
  794. mutex_lock(&sysfs_lock);
  795. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  796. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  797. if (dev == NULL) {
  798. status = -ENODEV;
  799. goto unlock;
  800. }
  801. }
  802. status = sysfs_set_active_low(desc, dev, value);
  803. unlock:
  804. mutex_unlock(&sysfs_lock);
  805. if (status)
  806. pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
  807. status);
  808. return status;
  809. }
  810. int gpio_sysfs_set_active_low(unsigned gpio, int value)
  811. {
  812. return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
  813. }
  814. EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
  815. /**
  816. * gpio_unexport - reverse effect of gpio_export()
  817. * @gpio: gpio to make unavailable
  818. *
  819. * This is implicit on gpio_free().
  820. */
  821. static void gpiod_unexport(struct gpio_desc *desc)
  822. {
  823. int status = 0;
  824. struct device *dev = NULL;
  825. if (!desc) {
  826. pr_warn("%s: invalid GPIO\n", __func__);
  827. return;
  828. }
  829. mutex_lock(&sysfs_lock);
  830. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  831. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  832. if (dev) {
  833. gpio_setup_irq(desc, dev, 0);
  834. clear_bit(FLAG_EXPORT, &desc->flags);
  835. } else
  836. status = -ENODEV;
  837. }
  838. mutex_unlock(&sysfs_lock);
  839. if (dev) {
  840. device_unregister(dev);
  841. put_device(dev);
  842. }
  843. if (status)
  844. pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
  845. status);
  846. }
  847. void gpio_unexport(unsigned gpio)
  848. {
  849. gpiod_unexport(gpio_to_desc(gpio));
  850. }
  851. EXPORT_SYMBOL_GPL(gpio_unexport);
  852. static int gpiochip_export(struct gpio_chip *chip)
  853. {
  854. int status;
  855. struct device *dev;
  856. /* Many systems register gpio chips for SOC support very early,
  857. * before driver model support is available. In those cases we
  858. * export this later, in gpiolib_sysfs_init() ... here we just
  859. * verify that _some_ field of gpio_class got initialized.
  860. */
  861. if (!gpio_class.p)
  862. return 0;
  863. /* use chip->base for the ID; it's already known to be unique */
  864. mutex_lock(&sysfs_lock);
  865. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  866. "gpiochip%d", chip->base);
  867. if (!IS_ERR(dev)) {
  868. status = sysfs_create_group(&dev->kobj,
  869. &gpiochip_attr_group);
  870. } else
  871. status = PTR_ERR(dev);
  872. chip->exported = (status == 0);
  873. mutex_unlock(&sysfs_lock);
  874. if (status) {
  875. unsigned long flags;
  876. unsigned gpio;
  877. spin_lock_irqsave(&gpio_lock, flags);
  878. gpio = 0;
  879. while (gpio < chip->ngpio)
  880. chip->desc[gpio++].chip = NULL;
  881. spin_unlock_irqrestore(&gpio_lock, flags);
  882. pr_debug("%s: chip %s status %d\n", __func__,
  883. chip->label, status);
  884. }
  885. return status;
  886. }
  887. static void gpiochip_unexport(struct gpio_chip *chip)
  888. {
  889. int status;
  890. struct device *dev;
  891. mutex_lock(&sysfs_lock);
  892. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  893. if (dev) {
  894. put_device(dev);
  895. device_unregister(dev);
  896. chip->exported = 0;
  897. status = 0;
  898. } else
  899. status = -ENODEV;
  900. mutex_unlock(&sysfs_lock);
  901. if (status)
  902. pr_debug("%s: chip %s status %d\n", __func__,
  903. chip->label, status);
  904. }
  905. static int __init gpiolib_sysfs_init(void)
  906. {
  907. int status;
  908. unsigned long flags;
  909. struct gpio_chip *chip;
  910. status = class_register(&gpio_class);
  911. if (status < 0)
  912. return status;
  913. /* Scan and register the gpio_chips which registered very
  914. * early (e.g. before the class_register above was called).
  915. *
  916. * We run before arch_initcall() so chip->dev nodes can have
  917. * registered, and so arch_initcall() can always gpio_export().
  918. */
  919. spin_lock_irqsave(&gpio_lock, flags);
  920. list_for_each_entry(chip, &gpio_chips, list) {
  921. if (!chip || chip->exported)
  922. continue;
  923. spin_unlock_irqrestore(&gpio_lock, flags);
  924. status = gpiochip_export(chip);
  925. spin_lock_irqsave(&gpio_lock, flags);
  926. }
  927. spin_unlock_irqrestore(&gpio_lock, flags);
  928. return status;
  929. }
  930. postcore_initcall(gpiolib_sysfs_init);
  931. #else
  932. static inline int gpiochip_export(struct gpio_chip *chip)
  933. {
  934. return 0;
  935. }
  936. static inline void gpiochip_unexport(struct gpio_chip *chip)
  937. {
  938. }
  939. static inline int gpiod_export(struct gpio_desc *desc,
  940. bool direction_may_change)
  941. {
  942. return -ENOSYS;
  943. }
  944. static inline int gpiod_export_link(struct device *dev, const char *name,
  945. struct gpio_desc *desc)
  946. {
  947. return -ENOSYS;
  948. }
  949. static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  950. {
  951. return -ENOSYS;
  952. }
  953. static inline void gpiod_unexport(struct gpio_desc *desc)
  954. {
  955. }
  956. #endif /* CONFIG_GPIO_SYSFS */
  957. /*
  958. * Add a new chip to the global chips list, keeping the list of chips sorted
  959. * by base order.
  960. *
  961. * Return -EBUSY if the new chip overlaps with some other chip's integer
  962. * space.
  963. */
  964. static int gpiochip_add_to_list(struct gpio_chip *chip)
  965. {
  966. struct list_head *pos = &gpio_chips;
  967. struct gpio_chip *_chip;
  968. int err = 0;
  969. /* find where to insert our chip */
  970. list_for_each(pos, &gpio_chips) {
  971. _chip = list_entry(pos, struct gpio_chip, list);
  972. /* shall we insert before _chip? */
  973. if (_chip->base >= chip->base + chip->ngpio)
  974. break;
  975. }
  976. /* are we stepping on the chip right before? */
  977. if (pos != &gpio_chips && pos->prev != &gpio_chips) {
  978. _chip = list_entry(pos->prev, struct gpio_chip, list);
  979. if (_chip->base + _chip->ngpio > chip->base) {
  980. dev_err(chip->dev,
  981. "GPIO integer space overlap, cannot add chip\n");
  982. err = -EBUSY;
  983. }
  984. }
  985. if (!err)
  986. list_add_tail(&chip->list, pos);
  987. return err;
  988. }
  989. /**
  990. * gpiochip_add() - register a gpio_chip
  991. * @chip: the chip to register, with chip->base initialized
  992. * Context: potentially before irqs or kmalloc will work
  993. *
  994. * Returns a negative errno if the chip can't be registered, such as
  995. * because the chip->base is invalid or already associated with a
  996. * different chip. Otherwise it returns zero as a success code.
  997. *
  998. * When gpiochip_add() is called very early during boot, so that GPIOs
  999. * can be freely used, the chip->dev device must be registered before
  1000. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  1001. * for GPIOs will fail rudely.
  1002. *
  1003. * If chip->base is negative, this requests dynamic assignment of
  1004. * a range of valid GPIOs.
  1005. */
  1006. int gpiochip_add(struct gpio_chip *chip)
  1007. {
  1008. unsigned long flags;
  1009. int status = 0;
  1010. unsigned id;
  1011. int base = chip->base;
  1012. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  1013. && base >= 0) {
  1014. status = -EINVAL;
  1015. goto fail;
  1016. }
  1017. spin_lock_irqsave(&gpio_lock, flags);
  1018. if (base < 0) {
  1019. base = gpiochip_find_base(chip->ngpio);
  1020. if (base < 0) {
  1021. status = base;
  1022. goto unlock;
  1023. }
  1024. chip->base = base;
  1025. }
  1026. status = gpiochip_add_to_list(chip);
  1027. if (status == 0) {
  1028. chip->desc = &gpio_desc[chip->base];
  1029. for (id = 0; id < chip->ngpio; id++) {
  1030. struct gpio_desc *desc = &chip->desc[id];
  1031. desc->chip = chip;
  1032. /* REVISIT: most hardware initializes GPIOs as
  1033. * inputs (often with pullups enabled) so power
  1034. * usage is minimized. Linux code should set the
  1035. * gpio direction first thing; but until it does,
  1036. * and in case chip->get_direction is not set,
  1037. * we may expose the wrong direction in sysfs.
  1038. */
  1039. desc->flags = !chip->direction_input
  1040. ? (1 << FLAG_IS_OUT)
  1041. : 0;
  1042. }
  1043. }
  1044. spin_unlock_irqrestore(&gpio_lock, flags);
  1045. #ifdef CONFIG_PINCTRL
  1046. INIT_LIST_HEAD(&chip->pin_ranges);
  1047. #endif
  1048. of_gpiochip_add(chip);
  1049. if (status)
  1050. goto fail;
  1051. status = gpiochip_export(chip);
  1052. if (status)
  1053. goto fail;
  1054. pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
  1055. chip->base, chip->base + chip->ngpio - 1,
  1056. chip->label ? : "generic");
  1057. return 0;
  1058. unlock:
  1059. spin_unlock_irqrestore(&gpio_lock, flags);
  1060. fail:
  1061. /* failures here can mean systems won't boot... */
  1062. pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
  1063. chip->base, chip->base + chip->ngpio - 1,
  1064. chip->label ? : "generic");
  1065. return status;
  1066. }
  1067. EXPORT_SYMBOL_GPL(gpiochip_add);
  1068. /**
  1069. * gpiochip_remove() - unregister a gpio_chip
  1070. * @chip: the chip to unregister
  1071. *
  1072. * A gpio_chip with any GPIOs still requested may not be removed.
  1073. */
  1074. int gpiochip_remove(struct gpio_chip *chip)
  1075. {
  1076. unsigned long flags;
  1077. int status = 0;
  1078. unsigned id;
  1079. spin_lock_irqsave(&gpio_lock, flags);
  1080. gpiochip_remove_pin_ranges(chip);
  1081. of_gpiochip_remove(chip);
  1082. for (id = 0; id < chip->ngpio; id++) {
  1083. if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
  1084. status = -EBUSY;
  1085. break;
  1086. }
  1087. }
  1088. if (status == 0) {
  1089. for (id = 0; id < chip->ngpio; id++)
  1090. chip->desc[id].chip = NULL;
  1091. list_del(&chip->list);
  1092. }
  1093. spin_unlock_irqrestore(&gpio_lock, flags);
  1094. if (status == 0)
  1095. gpiochip_unexport(chip);
  1096. return status;
  1097. }
  1098. EXPORT_SYMBOL_GPL(gpiochip_remove);
  1099. /**
  1100. * gpiochip_find() - iterator for locating a specific gpio_chip
  1101. * @data: data to pass to match function
  1102. * @callback: Callback function to check gpio_chip
  1103. *
  1104. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  1105. * determined by a user supplied @match callback. The callback should return
  1106. * 0 if the device doesn't match and non-zero if it does. If the callback is
  1107. * non-zero, this function will return to the caller and not iterate over any
  1108. * more gpio_chips.
  1109. */
  1110. struct gpio_chip *gpiochip_find(void *data,
  1111. int (*match)(struct gpio_chip *chip,
  1112. void *data))
  1113. {
  1114. struct gpio_chip *chip;
  1115. unsigned long flags;
  1116. spin_lock_irqsave(&gpio_lock, flags);
  1117. list_for_each_entry(chip, &gpio_chips, list)
  1118. if (match(chip, data))
  1119. break;
  1120. /* No match? */
  1121. if (&chip->list == &gpio_chips)
  1122. chip = NULL;
  1123. spin_unlock_irqrestore(&gpio_lock, flags);
  1124. return chip;
  1125. }
  1126. EXPORT_SYMBOL_GPL(gpiochip_find);
  1127. #ifdef CONFIG_PINCTRL
  1128. /**
  1129. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  1130. * @chip: the gpiochip to add the range for
  1131. * @pinctrl_name: the dev_name() of the pin controller to map to
  1132. * @gpio_offset: the start offset in the current gpio_chip number space
  1133. * @pin_offset: the start offset in the pin controller number space
  1134. * @npins: the number of pins from the offset of each pin space (GPIO and
  1135. * pin controller) to accumulate in this range
  1136. */
  1137. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  1138. unsigned int gpio_offset, unsigned int pin_offset,
  1139. unsigned int npins)
  1140. {
  1141. struct gpio_pin_range *pin_range;
  1142. int ret;
  1143. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1144. if (!pin_range) {
  1145. pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
  1146. chip->label);
  1147. return -ENOMEM;
  1148. }
  1149. /* Use local offset as range ID */
  1150. pin_range->range.id = gpio_offset;
  1151. pin_range->range.gc = chip;
  1152. pin_range->range.name = chip->label;
  1153. pin_range->range.base = chip->base + gpio_offset;
  1154. pin_range->range.pin_base = pin_offset;
  1155. pin_range->range.npins = npins;
  1156. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1157. &pin_range->range);
  1158. if (IS_ERR(pin_range->pctldev)) {
  1159. ret = PTR_ERR(pin_range->pctldev);
  1160. pr_err("%s: GPIO chip: could not create pin range\n",
  1161. chip->label);
  1162. kfree(pin_range);
  1163. return ret;
  1164. }
  1165. pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1166. chip->label, gpio_offset, gpio_offset + npins - 1,
  1167. pinctl_name,
  1168. pin_offset, pin_offset + npins - 1);
  1169. list_add_tail(&pin_range->node, &chip->pin_ranges);
  1170. return 0;
  1171. }
  1172. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1173. /**
  1174. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1175. * @chip: the chip to remove all the mappings for
  1176. */
  1177. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  1178. {
  1179. struct gpio_pin_range *pin_range, *tmp;
  1180. list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
  1181. list_del(&pin_range->node);
  1182. pinctrl_remove_gpio_range(pin_range->pctldev,
  1183. &pin_range->range);
  1184. kfree(pin_range);
  1185. }
  1186. }
  1187. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1188. #endif /* CONFIG_PINCTRL */
  1189. /* These "optional" allocation calls help prevent drivers from stomping
  1190. * on each other, and help provide better diagnostics in debugfs.
  1191. * They're called even less than the "set direction" calls.
  1192. */
  1193. static int gpiod_request(struct gpio_desc *desc, const char *label)
  1194. {
  1195. struct gpio_chip *chip;
  1196. int status = -EPROBE_DEFER;
  1197. unsigned long flags;
  1198. if (!desc || !desc->chip) {
  1199. pr_warn("%s: invalid GPIO\n", __func__);
  1200. return -EINVAL;
  1201. }
  1202. spin_lock_irqsave(&gpio_lock, flags);
  1203. chip = desc->chip;
  1204. if (!try_module_get(chip->owner))
  1205. goto done;
  1206. /* NOTE: gpio_request() can be called in early boot,
  1207. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1208. */
  1209. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1210. desc_set_label(desc, label ? : "?");
  1211. status = 0;
  1212. } else {
  1213. status = -EBUSY;
  1214. module_put(chip->owner);
  1215. goto done;
  1216. }
  1217. if (chip->request) {
  1218. /* chip->request may sleep */
  1219. spin_unlock_irqrestore(&gpio_lock, flags);
  1220. status = chip->request(chip, gpio_chip_hwgpio(desc));
  1221. spin_lock_irqsave(&gpio_lock, flags);
  1222. if (status < 0) {
  1223. desc_set_label(desc, NULL);
  1224. module_put(chip->owner);
  1225. clear_bit(FLAG_REQUESTED, &desc->flags);
  1226. goto done;
  1227. }
  1228. }
  1229. if (chip->get_direction) {
  1230. /* chip->get_direction may sleep */
  1231. spin_unlock_irqrestore(&gpio_lock, flags);
  1232. gpiod_get_direction(desc);
  1233. spin_lock_irqsave(&gpio_lock, flags);
  1234. }
  1235. done:
  1236. if (status)
  1237. pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
  1238. desc_to_gpio(desc), label ? : "?", status);
  1239. spin_unlock_irqrestore(&gpio_lock, flags);
  1240. return status;
  1241. }
  1242. int gpio_request(unsigned gpio, const char *label)
  1243. {
  1244. return gpiod_request(gpio_to_desc(gpio), label);
  1245. }
  1246. EXPORT_SYMBOL_GPL(gpio_request);
  1247. static void gpiod_free(struct gpio_desc *desc)
  1248. {
  1249. unsigned long flags;
  1250. struct gpio_chip *chip;
  1251. might_sleep();
  1252. if (!desc) {
  1253. WARN_ON(extra_checks);
  1254. return;
  1255. }
  1256. gpiod_unexport(desc);
  1257. spin_lock_irqsave(&gpio_lock, flags);
  1258. chip = desc->chip;
  1259. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1260. if (chip->free) {
  1261. spin_unlock_irqrestore(&gpio_lock, flags);
  1262. might_sleep_if(chip->can_sleep);
  1263. chip->free(chip, gpio_chip_hwgpio(desc));
  1264. spin_lock_irqsave(&gpio_lock, flags);
  1265. }
  1266. desc_set_label(desc, NULL);
  1267. module_put(desc->chip->owner);
  1268. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1269. clear_bit(FLAG_REQUESTED, &desc->flags);
  1270. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1271. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1272. } else
  1273. WARN_ON(extra_checks);
  1274. spin_unlock_irqrestore(&gpio_lock, flags);
  1275. }
  1276. void gpio_free(unsigned gpio)
  1277. {
  1278. gpiod_free(gpio_to_desc(gpio));
  1279. }
  1280. EXPORT_SYMBOL_GPL(gpio_free);
  1281. /**
  1282. * gpio_request_one - request a single GPIO with initial configuration
  1283. * @gpio: the GPIO number
  1284. * @flags: GPIO configuration as specified by GPIOF_*
  1285. * @label: a literal description string of this GPIO
  1286. */
  1287. int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  1288. {
  1289. struct gpio_desc *desc;
  1290. int err;
  1291. desc = gpio_to_desc(gpio);
  1292. err = gpiod_request(desc, label);
  1293. if (err)
  1294. return err;
  1295. if (flags & GPIOF_OPEN_DRAIN)
  1296. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1297. if (flags & GPIOF_OPEN_SOURCE)
  1298. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1299. if (flags & GPIOF_DIR_IN)
  1300. err = gpiod_direction_input(desc);
  1301. else
  1302. err = gpiod_direction_output(desc,
  1303. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  1304. if (err)
  1305. goto free_gpio;
  1306. if (flags & GPIOF_EXPORT) {
  1307. err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
  1308. if (err)
  1309. goto free_gpio;
  1310. }
  1311. return 0;
  1312. free_gpio:
  1313. gpiod_free(desc);
  1314. return err;
  1315. }
  1316. EXPORT_SYMBOL_GPL(gpio_request_one);
  1317. /**
  1318. * gpio_request_array - request multiple GPIOs in a single call
  1319. * @array: array of the 'struct gpio'
  1320. * @num: how many GPIOs in the array
  1321. */
  1322. int gpio_request_array(const struct gpio *array, size_t num)
  1323. {
  1324. int i, err;
  1325. for (i = 0; i < num; i++, array++) {
  1326. err = gpio_request_one(array->gpio, array->flags, array->label);
  1327. if (err)
  1328. goto err_free;
  1329. }
  1330. return 0;
  1331. err_free:
  1332. while (i--)
  1333. gpio_free((--array)->gpio);
  1334. return err;
  1335. }
  1336. EXPORT_SYMBOL_GPL(gpio_request_array);
  1337. /**
  1338. * gpio_free_array - release multiple GPIOs in a single call
  1339. * @array: array of the 'struct gpio'
  1340. * @num: how many GPIOs in the array
  1341. */
  1342. void gpio_free_array(const struct gpio *array, size_t num)
  1343. {
  1344. while (num--)
  1345. gpio_free((array++)->gpio);
  1346. }
  1347. EXPORT_SYMBOL_GPL(gpio_free_array);
  1348. /**
  1349. * gpiochip_is_requested - return string iff signal was requested
  1350. * @chip: controller managing the signal
  1351. * @offset: of signal within controller's 0..(ngpio - 1) range
  1352. *
  1353. * Returns NULL if the GPIO is not currently requested, else a string.
  1354. * If debugfs support is enabled, the string returned is the label passed
  1355. * to gpio_request(); otherwise it is a meaningless constant.
  1356. *
  1357. * This function is for use by GPIO controller drivers. The label can
  1358. * help with diagnostics, and knowing that the signal is used as a GPIO
  1359. * can help avoid accidentally multiplexing it to another controller.
  1360. */
  1361. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1362. {
  1363. struct gpio_desc *desc;
  1364. if (!GPIO_OFFSET_VALID(chip, offset))
  1365. return NULL;
  1366. desc = &chip->desc[offset];
  1367. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  1368. return NULL;
  1369. #ifdef CONFIG_DEBUG_FS
  1370. return desc->label;
  1371. #else
  1372. return "?";
  1373. #endif
  1374. }
  1375. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1376. /* Drivers MUST set GPIO direction before making get/set calls. In
  1377. * some cases this is done in early boot, before IRQs are enabled.
  1378. *
  1379. * As a rule these aren't called more than once (except for drivers
  1380. * using the open-drain emulation idiom) so these are natural places
  1381. * to accumulate extra debugging checks. Note that we can't (yet)
  1382. * rely on gpio_request() having been called beforehand.
  1383. */
  1384. static int gpiod_direction_input(struct gpio_desc *desc)
  1385. {
  1386. unsigned long flags;
  1387. struct gpio_chip *chip;
  1388. int status = -EINVAL;
  1389. int offset;
  1390. if (!desc || !desc->chip) {
  1391. pr_warn("%s: invalid GPIO\n", __func__);
  1392. return -EINVAL;
  1393. }
  1394. chip = desc->chip;
  1395. if (!chip->get || !chip->direction_input) {
  1396. gpiod_warn(desc,
  1397. "%s: missing get() or direction_input() operations\n",
  1398. __func__);
  1399. return -EIO;
  1400. }
  1401. spin_lock_irqsave(&gpio_lock, flags);
  1402. status = gpio_ensure_requested(desc);
  1403. if (status < 0)
  1404. goto fail;
  1405. /* now we know the gpio is valid and chip won't vanish */
  1406. spin_unlock_irqrestore(&gpio_lock, flags);
  1407. might_sleep_if(chip->can_sleep);
  1408. offset = gpio_chip_hwgpio(desc);
  1409. if (status) {
  1410. status = chip->request(chip, offset);
  1411. if (status < 0) {
  1412. gpiod_dbg(desc, "chip request fail, %d\n", status);
  1413. /* and it's not available to anyone else ...
  1414. * gpio_request() is the fully clean solution.
  1415. */
  1416. goto lose;
  1417. }
  1418. }
  1419. status = chip->direction_input(chip, offset);
  1420. if (status == 0)
  1421. clear_bit(FLAG_IS_OUT, &desc->flags);
  1422. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  1423. lose:
  1424. return status;
  1425. fail:
  1426. spin_unlock_irqrestore(&gpio_lock, flags);
  1427. if (status)
  1428. gpiod_dbg(desc, "%s status %d\n", __func__, status);
  1429. return status;
  1430. }
  1431. int gpio_direction_input(unsigned gpio)
  1432. {
  1433. return gpiod_direction_input(gpio_to_desc(gpio));
  1434. }
  1435. EXPORT_SYMBOL_GPL(gpio_direction_input);
  1436. static int gpiod_direction_output(struct gpio_desc *desc, int value)
  1437. {
  1438. unsigned long flags;
  1439. struct gpio_chip *chip;
  1440. int status = -EINVAL;
  1441. int offset;
  1442. if (!desc || !desc->chip) {
  1443. pr_warn("%s: invalid GPIO\n", __func__);
  1444. return -EINVAL;
  1445. }
  1446. /* Open drain pin should not be driven to 1 */
  1447. if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1448. return gpiod_direction_input(desc);
  1449. /* Open source pin should not be driven to 0 */
  1450. if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1451. return gpiod_direction_input(desc);
  1452. chip = desc->chip;
  1453. if (!chip->set || !chip->direction_output) {
  1454. gpiod_warn(desc,
  1455. "%s: missing set() or direction_output() operations\n",
  1456. __func__);
  1457. return -EIO;
  1458. }
  1459. spin_lock_irqsave(&gpio_lock, flags);
  1460. status = gpio_ensure_requested(desc);
  1461. if (status < 0)
  1462. goto fail;
  1463. /* now we know the gpio is valid and chip won't vanish */
  1464. spin_unlock_irqrestore(&gpio_lock, flags);
  1465. might_sleep_if(chip->can_sleep);
  1466. offset = gpio_chip_hwgpio(desc);
  1467. if (status) {
  1468. status = chip->request(chip, offset);
  1469. if (status < 0) {
  1470. gpiod_dbg(desc, "chip request fail, %d\n", status);
  1471. /* and it's not available to anyone else ...
  1472. * gpio_request() is the fully clean solution.
  1473. */
  1474. goto lose;
  1475. }
  1476. }
  1477. status = chip->direction_output(chip, offset, value);
  1478. if (status == 0)
  1479. set_bit(FLAG_IS_OUT, &desc->flags);
  1480. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1481. trace_gpio_direction(desc_to_gpio(desc), 0, status);
  1482. lose:
  1483. return status;
  1484. fail:
  1485. spin_unlock_irqrestore(&gpio_lock, flags);
  1486. if (status)
  1487. gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
  1488. return status;
  1489. }
  1490. int gpio_direction_output(unsigned gpio, int value)
  1491. {
  1492. return gpiod_direction_output(gpio_to_desc(gpio), value);
  1493. }
  1494. EXPORT_SYMBOL_GPL(gpio_direction_output);
  1495. /**
  1496. * gpio_set_debounce - sets @debounce time for a @gpio
  1497. * @gpio: the gpio to set debounce time
  1498. * @debounce: debounce time is microseconds
  1499. *
  1500. * returns -ENOTSUPP if the controller does not support setting
  1501. * debounce.
  1502. */
  1503. static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  1504. {
  1505. unsigned long flags;
  1506. struct gpio_chip *chip;
  1507. int status = -EINVAL;
  1508. int offset;
  1509. if (!desc || !desc->chip) {
  1510. pr_warn("%s: invalid GPIO\n", __func__);
  1511. return -EINVAL;
  1512. }
  1513. chip = desc->chip;
  1514. if (!chip->set || !chip->set_debounce) {
  1515. gpiod_dbg(desc,
  1516. "%s: missing set() or set_debounce() operations\n",
  1517. __func__);
  1518. return -ENOTSUPP;
  1519. }
  1520. spin_lock_irqsave(&gpio_lock, flags);
  1521. status = gpio_ensure_requested(desc);
  1522. if (status < 0)
  1523. goto fail;
  1524. /* now we know the gpio is valid and chip won't vanish */
  1525. spin_unlock_irqrestore(&gpio_lock, flags);
  1526. might_sleep_if(chip->can_sleep);
  1527. offset = gpio_chip_hwgpio(desc);
  1528. return chip->set_debounce(chip, offset, debounce);
  1529. fail:
  1530. spin_unlock_irqrestore(&gpio_lock, flags);
  1531. if (status)
  1532. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  1533. return status;
  1534. }
  1535. int gpio_set_debounce(unsigned gpio, unsigned debounce)
  1536. {
  1537. return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
  1538. }
  1539. EXPORT_SYMBOL_GPL(gpio_set_debounce);
  1540. /* I/O calls are only valid after configuration completed; the relevant
  1541. * "is this a valid GPIO" error checks should already have been done.
  1542. *
  1543. * "Get" operations are often inlinable as reading a pin value register,
  1544. * and masking the relevant bit in that register.
  1545. *
  1546. * When "set" operations are inlinable, they involve writing that mask to
  1547. * one register to set a low value, or a different register to set it high.
  1548. * Otherwise locking is needed, so there may be little value to inlining.
  1549. *
  1550. *------------------------------------------------------------------------
  1551. *
  1552. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1553. * have requested the GPIO. That can include implicit requesting by
  1554. * a direction setting call. Marking a gpio as requested locks its chip
  1555. * in memory, guaranteeing that these table lookups need no more locking
  1556. * and that gpiochip_remove() will fail.
  1557. *
  1558. * REVISIT when debugging, consider adding some instrumentation to ensure
  1559. * that the GPIO was actually requested.
  1560. */
  1561. static int _gpiod_get_value(const struct gpio_desc *desc)
  1562. {
  1563. struct gpio_chip *chip;
  1564. int value;
  1565. int offset;
  1566. chip = desc->chip;
  1567. offset = gpio_chip_hwgpio(desc);
  1568. value = chip->get ? chip->get(chip, offset) : 0;
  1569. trace_gpio_value(desc_to_gpio(desc), 1, value);
  1570. return value;
  1571. }
  1572. /**
  1573. * __gpio_get_value() - return a gpio's value
  1574. * @gpio: gpio whose value will be returned
  1575. * Context: any
  1576. *
  1577. * This is used directly or indirectly to implement gpio_get_value().
  1578. * It returns the zero or nonzero value provided by the associated
  1579. * gpio_chip.get() method; or zero if no such method is provided.
  1580. */
  1581. static int gpiod_get_value(const struct gpio_desc *desc)
  1582. {
  1583. if (!desc)
  1584. return 0;
  1585. /* Should be using gpio_get_value_cansleep() */
  1586. WARN_ON(desc->chip->can_sleep);
  1587. return _gpiod_get_value(desc);
  1588. }
  1589. int __gpio_get_value(unsigned gpio)
  1590. {
  1591. return gpiod_get_value(gpio_to_desc(gpio));
  1592. }
  1593. EXPORT_SYMBOL_GPL(__gpio_get_value);
  1594. /*
  1595. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  1596. * @gpio: Gpio whose state need to be set.
  1597. * @chip: Gpio chip.
  1598. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1599. */
  1600. static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
  1601. {
  1602. int err = 0;
  1603. struct gpio_chip *chip = desc->chip;
  1604. int offset = gpio_chip_hwgpio(desc);
  1605. if (value) {
  1606. err = chip->direction_input(chip, offset);
  1607. if (!err)
  1608. clear_bit(FLAG_IS_OUT, &desc->flags);
  1609. } else {
  1610. err = chip->direction_output(chip, offset, 0);
  1611. if (!err)
  1612. set_bit(FLAG_IS_OUT, &desc->flags);
  1613. }
  1614. trace_gpio_direction(desc_to_gpio(desc), value, err);
  1615. if (err < 0)
  1616. gpiod_err(desc,
  1617. "%s: Error in set_value for open drain err %d\n",
  1618. __func__, err);
  1619. }
  1620. /*
  1621. * _gpio_set_open_source() - Set the open source gpio's value.
  1622. * @gpio: Gpio whose state need to be set.
  1623. * @chip: Gpio chip.
  1624. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1625. */
  1626. static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
  1627. {
  1628. int err = 0;
  1629. struct gpio_chip *chip = desc->chip;
  1630. int offset = gpio_chip_hwgpio(desc);
  1631. if (value) {
  1632. err = chip->direction_output(chip, offset, 1);
  1633. if (!err)
  1634. set_bit(FLAG_IS_OUT, &desc->flags);
  1635. } else {
  1636. err = chip->direction_input(chip, offset);
  1637. if (!err)
  1638. clear_bit(FLAG_IS_OUT, &desc->flags);
  1639. }
  1640. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  1641. if (err < 0)
  1642. gpiod_err(desc,
  1643. "%s: Error in set_value for open source err %d\n",
  1644. __func__, err);
  1645. }
  1646. static void _gpiod_set_value(struct gpio_desc *desc, int value)
  1647. {
  1648. struct gpio_chip *chip;
  1649. chip = desc->chip;
  1650. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1651. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1652. _gpio_set_open_drain_value(desc, value);
  1653. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1654. _gpio_set_open_source_value(desc, value);
  1655. else
  1656. chip->set(chip, gpio_chip_hwgpio(desc), value);
  1657. }
  1658. /**
  1659. * __gpio_set_value() - assign a gpio's value
  1660. * @gpio: gpio whose value will be assigned
  1661. * @value: value to assign
  1662. * Context: any
  1663. *
  1664. * This is used directly or indirectly to implement gpio_set_value().
  1665. * It invokes the associated gpio_chip.set() method.
  1666. */
  1667. static void gpiod_set_value(struct gpio_desc *desc, int value)
  1668. {
  1669. if (!desc)
  1670. return;
  1671. /* Should be using gpio_set_value_cansleep() */
  1672. WARN_ON(desc->chip->can_sleep);
  1673. _gpiod_set_value(desc, value);
  1674. }
  1675. void __gpio_set_value(unsigned gpio, int value)
  1676. {
  1677. return gpiod_set_value(gpio_to_desc(gpio), value);
  1678. }
  1679. EXPORT_SYMBOL_GPL(__gpio_set_value);
  1680. /**
  1681. * __gpio_cansleep() - report whether gpio value access will sleep
  1682. * @gpio: gpio in question
  1683. * Context: any
  1684. *
  1685. * This is used directly or indirectly to implement gpio_cansleep(). It
  1686. * returns nonzero if access reading or writing the GPIO value can sleep.
  1687. */
  1688. static int gpiod_cansleep(const struct gpio_desc *desc)
  1689. {
  1690. if (!desc)
  1691. return 0;
  1692. /* only call this on GPIOs that are valid! */
  1693. return desc->chip->can_sleep;
  1694. }
  1695. int __gpio_cansleep(unsigned gpio)
  1696. {
  1697. return gpiod_cansleep(gpio_to_desc(gpio));
  1698. }
  1699. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  1700. /**
  1701. * __gpio_to_irq() - return the IRQ corresponding to a GPIO
  1702. * @gpio: gpio whose IRQ will be returned (already requested)
  1703. * Context: any
  1704. *
  1705. * This is used directly or indirectly to implement gpio_to_irq().
  1706. * It returns the number of the IRQ signaled by this (input) GPIO,
  1707. * or a negative errno.
  1708. */
  1709. static int gpiod_to_irq(const struct gpio_desc *desc)
  1710. {
  1711. struct gpio_chip *chip;
  1712. int offset;
  1713. if (!desc)
  1714. return -EINVAL;
  1715. chip = desc->chip;
  1716. offset = gpio_chip_hwgpio(desc);
  1717. return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
  1718. }
  1719. int __gpio_to_irq(unsigned gpio)
  1720. {
  1721. return gpiod_to_irq(gpio_to_desc(gpio));
  1722. }
  1723. EXPORT_SYMBOL_GPL(__gpio_to_irq);
  1724. /* There's no value in making it easy to inline GPIO calls that may sleep.
  1725. * Common examples include ones connected to I2C or SPI chips.
  1726. */
  1727. static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  1728. {
  1729. might_sleep_if(extra_checks);
  1730. if (!desc)
  1731. return 0;
  1732. return _gpiod_get_value(desc);
  1733. }
  1734. int gpio_get_value_cansleep(unsigned gpio)
  1735. {
  1736. return gpiod_get_value_cansleep(gpio_to_desc(gpio));
  1737. }
  1738. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  1739. static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  1740. {
  1741. might_sleep_if(extra_checks);
  1742. if (!desc)
  1743. return;
  1744. _gpiod_set_value(desc, value);
  1745. }
  1746. void gpio_set_value_cansleep(unsigned gpio, int value)
  1747. {
  1748. return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
  1749. }
  1750. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  1751. #ifdef CONFIG_DEBUG_FS
  1752. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1753. {
  1754. unsigned i;
  1755. unsigned gpio = chip->base;
  1756. struct gpio_desc *gdesc = &chip->desc[0];
  1757. int is_out;
  1758. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1759. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1760. continue;
  1761. gpiod_get_direction(gdesc);
  1762. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1763. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
  1764. gpio, gdesc->label,
  1765. is_out ? "out" : "in ",
  1766. chip->get
  1767. ? (chip->get(chip, i) ? "hi" : "lo")
  1768. : "? ");
  1769. seq_printf(s, "\n");
  1770. }
  1771. }
  1772. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  1773. {
  1774. unsigned long flags;
  1775. struct gpio_chip *chip = NULL;
  1776. loff_t index = *pos;
  1777. s->private = "";
  1778. spin_lock_irqsave(&gpio_lock, flags);
  1779. list_for_each_entry(chip, &gpio_chips, list)
  1780. if (index-- == 0) {
  1781. spin_unlock_irqrestore(&gpio_lock, flags);
  1782. return chip;
  1783. }
  1784. spin_unlock_irqrestore(&gpio_lock, flags);
  1785. return NULL;
  1786. }
  1787. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1788. {
  1789. unsigned long flags;
  1790. struct gpio_chip *chip = v;
  1791. void *ret = NULL;
  1792. spin_lock_irqsave(&gpio_lock, flags);
  1793. if (list_is_last(&chip->list, &gpio_chips))
  1794. ret = NULL;
  1795. else
  1796. ret = list_entry(chip->list.next, struct gpio_chip, list);
  1797. spin_unlock_irqrestore(&gpio_lock, flags);
  1798. s->private = "\n";
  1799. ++*pos;
  1800. return ret;
  1801. }
  1802. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  1803. {
  1804. }
  1805. static int gpiolib_seq_show(struct seq_file *s, void *v)
  1806. {
  1807. struct gpio_chip *chip = v;
  1808. struct device *dev;
  1809. seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
  1810. chip->base, chip->base + chip->ngpio - 1);
  1811. dev = chip->dev;
  1812. if (dev)
  1813. seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
  1814. dev_name(dev));
  1815. if (chip->label)
  1816. seq_printf(s, ", %s", chip->label);
  1817. if (chip->can_sleep)
  1818. seq_printf(s, ", can sleep");
  1819. seq_printf(s, ":\n");
  1820. if (chip->dbg_show)
  1821. chip->dbg_show(s, chip);
  1822. else
  1823. gpiolib_dbg_show(s, chip);
  1824. return 0;
  1825. }
  1826. static const struct seq_operations gpiolib_seq_ops = {
  1827. .start = gpiolib_seq_start,
  1828. .next = gpiolib_seq_next,
  1829. .stop = gpiolib_seq_stop,
  1830. .show = gpiolib_seq_show,
  1831. };
  1832. static int gpiolib_open(struct inode *inode, struct file *file)
  1833. {
  1834. return seq_open(file, &gpiolib_seq_ops);
  1835. }
  1836. static const struct file_operations gpiolib_operations = {
  1837. .owner = THIS_MODULE,
  1838. .open = gpiolib_open,
  1839. .read = seq_read,
  1840. .llseek = seq_lseek,
  1841. .release = seq_release,
  1842. };
  1843. static int __init gpiolib_debugfs_init(void)
  1844. {
  1845. /* /sys/kernel/debug/gpio */
  1846. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1847. NULL, NULL, &gpiolib_operations);
  1848. return 0;
  1849. }
  1850. subsys_initcall(gpiolib_debugfs_init);
  1851. #endif /* DEBUG_FS */