gpiolib.c 48 KB

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