gpiolib.c 49 KB

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