gpiolib.c 48 KB

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