gpiolib.c 53 KB

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