gpiolib.c 54 KB

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