gpiolib.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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. }
  195. mutex_unlock(&sysfs_lock);
  196. return status;
  197. }
  198. static ssize_t gpio_direction_store(struct device *dev,
  199. struct device_attribute *attr, const char *buf, size_t size)
  200. {
  201. const struct gpio_desc *desc = dev_get_drvdata(dev);
  202. unsigned gpio = desc - gpio_desc;
  203. ssize_t status;
  204. mutex_lock(&sysfs_lock);
  205. if (!test_bit(FLAG_EXPORT, &desc->flags))
  206. status = -EIO;
  207. else if (sysfs_streq(buf, "high"))
  208. status = gpio_direction_output(gpio, 1);
  209. else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
  210. status = gpio_direction_output(gpio, 0);
  211. else if (sysfs_streq(buf, "in"))
  212. status = gpio_direction_input(gpio);
  213. else
  214. status = -EINVAL;
  215. mutex_unlock(&sysfs_lock);
  216. return status ? : size;
  217. }
  218. static /* const */ DEVICE_ATTR(direction, 0644,
  219. gpio_direction_show, gpio_direction_store);
  220. static ssize_t gpio_value_show(struct device *dev,
  221. struct device_attribute *attr, char *buf)
  222. {
  223. const struct gpio_desc *desc = dev_get_drvdata(dev);
  224. unsigned gpio = desc - gpio_desc;
  225. ssize_t status;
  226. mutex_lock(&sysfs_lock);
  227. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  228. status = -EIO;
  229. } else {
  230. int value;
  231. value = !!gpio_get_value_cansleep(gpio);
  232. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  233. value = !value;
  234. status = sprintf(buf, "%d\n", value);
  235. }
  236. mutex_unlock(&sysfs_lock);
  237. return status;
  238. }
  239. static ssize_t gpio_value_store(struct device *dev,
  240. struct device_attribute *attr, const char *buf, size_t size)
  241. {
  242. const struct gpio_desc *desc = dev_get_drvdata(dev);
  243. unsigned gpio = desc - gpio_desc;
  244. ssize_t status;
  245. mutex_lock(&sysfs_lock);
  246. if (!test_bit(FLAG_EXPORT, &desc->flags))
  247. status = -EIO;
  248. else if (!test_bit(FLAG_IS_OUT, &desc->flags))
  249. status = -EPERM;
  250. else {
  251. long value;
  252. status = strict_strtol(buf, 0, &value);
  253. if (status == 0) {
  254. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  255. value = !value;
  256. gpio_set_value_cansleep(gpio, value != 0);
  257. status = size;
  258. }
  259. }
  260. mutex_unlock(&sysfs_lock);
  261. return status;
  262. }
  263. static const DEVICE_ATTR(value, 0644,
  264. gpio_value_show, gpio_value_store);
  265. static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
  266. {
  267. struct sysfs_dirent *value_sd = priv;
  268. sysfs_notify_dirent(value_sd);
  269. return IRQ_HANDLED;
  270. }
  271. static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
  272. unsigned long gpio_flags)
  273. {
  274. struct sysfs_dirent *value_sd;
  275. unsigned long irq_flags;
  276. int ret, irq, id;
  277. if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
  278. return 0;
  279. irq = gpio_to_irq(desc - gpio_desc);
  280. if (irq < 0)
  281. return -EIO;
  282. id = desc->flags >> ID_SHIFT;
  283. value_sd = idr_find(&dirent_idr, id);
  284. if (value_sd)
  285. free_irq(irq, value_sd);
  286. desc->flags &= ~GPIO_TRIGGER_MASK;
  287. if (!gpio_flags) {
  288. ret = 0;
  289. goto free_id;
  290. }
  291. irq_flags = IRQF_SHARED;
  292. if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
  293. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  294. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  295. if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
  296. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  297. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  298. if (!value_sd) {
  299. value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
  300. if (!value_sd) {
  301. ret = -ENODEV;
  302. goto err_out;
  303. }
  304. do {
  305. ret = -ENOMEM;
  306. if (idr_pre_get(&dirent_idr, GFP_KERNEL))
  307. ret = idr_get_new_above(&dirent_idr, value_sd,
  308. 1, &id);
  309. } while (ret == -EAGAIN);
  310. if (ret)
  311. goto free_sd;
  312. desc->flags &= GPIO_FLAGS_MASK;
  313. desc->flags |= (unsigned long)id << ID_SHIFT;
  314. if (desc->flags >> ID_SHIFT != id) {
  315. ret = -ERANGE;
  316. goto free_id;
  317. }
  318. }
  319. ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
  320. "gpiolib", value_sd);
  321. if (ret < 0)
  322. goto free_id;
  323. desc->flags |= gpio_flags;
  324. return 0;
  325. free_id:
  326. idr_remove(&dirent_idr, id);
  327. desc->flags &= GPIO_FLAGS_MASK;
  328. free_sd:
  329. if (value_sd)
  330. sysfs_put(value_sd);
  331. err_out:
  332. return ret;
  333. }
  334. static const struct {
  335. const char *name;
  336. unsigned long flags;
  337. } trigger_types[] = {
  338. { "none", 0 },
  339. { "falling", BIT(FLAG_TRIG_FALL) },
  340. { "rising", BIT(FLAG_TRIG_RISE) },
  341. { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
  342. };
  343. static ssize_t gpio_edge_show(struct device *dev,
  344. struct device_attribute *attr, char *buf)
  345. {
  346. const struct gpio_desc *desc = dev_get_drvdata(dev);
  347. ssize_t status;
  348. mutex_lock(&sysfs_lock);
  349. if (!test_bit(FLAG_EXPORT, &desc->flags))
  350. status = -EIO;
  351. else {
  352. int i;
  353. status = 0;
  354. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  355. if ((desc->flags & GPIO_TRIGGER_MASK)
  356. == trigger_types[i].flags) {
  357. status = sprintf(buf, "%s\n",
  358. trigger_types[i].name);
  359. break;
  360. }
  361. }
  362. mutex_unlock(&sysfs_lock);
  363. return status;
  364. }
  365. static ssize_t gpio_edge_store(struct device *dev,
  366. struct device_attribute *attr, const char *buf, size_t size)
  367. {
  368. struct gpio_desc *desc = dev_get_drvdata(dev);
  369. ssize_t status;
  370. int i;
  371. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  372. if (sysfs_streq(trigger_types[i].name, buf))
  373. goto found;
  374. return -EINVAL;
  375. found:
  376. mutex_lock(&sysfs_lock);
  377. if (!test_bit(FLAG_EXPORT, &desc->flags))
  378. status = -EIO;
  379. else {
  380. status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
  381. if (!status)
  382. status = size;
  383. }
  384. mutex_unlock(&sysfs_lock);
  385. return status;
  386. }
  387. static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
  388. static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
  389. int value)
  390. {
  391. int status = 0;
  392. if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
  393. return 0;
  394. if (value)
  395. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  396. else
  397. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  398. /* reconfigure poll(2) support if enabled on one edge only */
  399. if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
  400. !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
  401. unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
  402. gpio_setup_irq(desc, dev, 0);
  403. status = gpio_setup_irq(desc, dev, trigger_flags);
  404. }
  405. return status;
  406. }
  407. static ssize_t gpio_active_low_show(struct device *dev,
  408. struct device_attribute *attr, char *buf)
  409. {
  410. const struct gpio_desc *desc = dev_get_drvdata(dev);
  411. ssize_t status;
  412. mutex_lock(&sysfs_lock);
  413. if (!test_bit(FLAG_EXPORT, &desc->flags))
  414. status = -EIO;
  415. else
  416. status = sprintf(buf, "%d\n",
  417. !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
  418. mutex_unlock(&sysfs_lock);
  419. return status;
  420. }
  421. static ssize_t gpio_active_low_store(struct device *dev,
  422. struct device_attribute *attr, const char *buf, size_t size)
  423. {
  424. struct gpio_desc *desc = dev_get_drvdata(dev);
  425. ssize_t status;
  426. mutex_lock(&sysfs_lock);
  427. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  428. status = -EIO;
  429. } else {
  430. long value;
  431. status = strict_strtol(buf, 0, &value);
  432. if (status == 0)
  433. status = sysfs_set_active_low(desc, dev, value != 0);
  434. }
  435. mutex_unlock(&sysfs_lock);
  436. return status ? : size;
  437. }
  438. static const DEVICE_ATTR(active_low, 0644,
  439. gpio_active_low_show, gpio_active_low_store);
  440. static const struct attribute *gpio_attrs[] = {
  441. &dev_attr_value.attr,
  442. &dev_attr_active_low.attr,
  443. NULL,
  444. };
  445. static const struct attribute_group gpio_attr_group = {
  446. .attrs = (struct attribute **) gpio_attrs,
  447. };
  448. /*
  449. * /sys/class/gpio/gpiochipN/
  450. * /base ... matching gpio_chip.base (N)
  451. * /label ... matching gpio_chip.label
  452. * /ngpio ... matching gpio_chip.ngpio
  453. */
  454. static ssize_t chip_base_show(struct device *dev,
  455. struct device_attribute *attr, char *buf)
  456. {
  457. const struct gpio_chip *chip = dev_get_drvdata(dev);
  458. return sprintf(buf, "%d\n", chip->base);
  459. }
  460. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  461. static ssize_t chip_label_show(struct device *dev,
  462. struct device_attribute *attr, char *buf)
  463. {
  464. const struct gpio_chip *chip = dev_get_drvdata(dev);
  465. return sprintf(buf, "%s\n", chip->label ? : "");
  466. }
  467. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  468. static ssize_t chip_ngpio_show(struct device *dev,
  469. struct device_attribute *attr, char *buf)
  470. {
  471. const struct gpio_chip *chip = dev_get_drvdata(dev);
  472. return sprintf(buf, "%u\n", chip->ngpio);
  473. }
  474. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  475. static const struct attribute *gpiochip_attrs[] = {
  476. &dev_attr_base.attr,
  477. &dev_attr_label.attr,
  478. &dev_attr_ngpio.attr,
  479. NULL,
  480. };
  481. static const struct attribute_group gpiochip_attr_group = {
  482. .attrs = (struct attribute **) gpiochip_attrs,
  483. };
  484. /*
  485. * /sys/class/gpio/export ... write-only
  486. * integer N ... number of GPIO to export (full access)
  487. * /sys/class/gpio/unexport ... write-only
  488. * integer N ... number of GPIO to unexport
  489. */
  490. static ssize_t export_store(struct class *class,
  491. struct class_attribute *attr,
  492. const char *buf, size_t len)
  493. {
  494. long gpio;
  495. int status;
  496. status = strict_strtol(buf, 0, &gpio);
  497. if (status < 0)
  498. goto done;
  499. /* No extra locking here; FLAG_SYSFS just signifies that the
  500. * request and export were done by on behalf of userspace, so
  501. * they may be undone on its behalf too.
  502. */
  503. status = gpio_request(gpio, "sysfs");
  504. if (status < 0) {
  505. if (status == -EPROBE_DEFER)
  506. status = -ENODEV;
  507. goto done;
  508. }
  509. status = gpio_export(gpio, true);
  510. if (status < 0)
  511. gpio_free(gpio);
  512. else
  513. set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
  514. done:
  515. if (status)
  516. pr_debug("%s: status %d\n", __func__, status);
  517. return status ? : len;
  518. }
  519. static ssize_t unexport_store(struct class *class,
  520. struct class_attribute *attr,
  521. const char *buf, size_t len)
  522. {
  523. long gpio;
  524. int status;
  525. status = strict_strtol(buf, 0, &gpio);
  526. if (status < 0)
  527. goto done;
  528. status = -EINVAL;
  529. /* reject bogus commands (gpio_unexport ignores them) */
  530. if (!gpio_is_valid(gpio))
  531. goto done;
  532. /* No extra locking here; FLAG_SYSFS just signifies that the
  533. * request and export were done by on behalf of userspace, so
  534. * they may be undone on its behalf too.
  535. */
  536. if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
  537. status = 0;
  538. gpio_free(gpio);
  539. }
  540. done:
  541. if (status)
  542. pr_debug("%s: status %d\n", __func__, status);
  543. return status ? : len;
  544. }
  545. static struct class_attribute gpio_class_attrs[] = {
  546. __ATTR(export, 0200, NULL, export_store),
  547. __ATTR(unexport, 0200, NULL, unexport_store),
  548. __ATTR_NULL,
  549. };
  550. static struct class gpio_class = {
  551. .name = "gpio",
  552. .owner = THIS_MODULE,
  553. .class_attrs = gpio_class_attrs,
  554. };
  555. /**
  556. * gpio_export - export a GPIO through sysfs
  557. * @gpio: gpio to make available, already requested
  558. * @direction_may_change: true if userspace may change gpio direction
  559. * Context: arch_initcall or later
  560. *
  561. * When drivers want to make a GPIO accessible to userspace after they
  562. * have requested it -- perhaps while debugging, or as part of their
  563. * public interface -- they may use this routine. If the GPIO can
  564. * change direction (some can't) and the caller allows it, userspace
  565. * will see "direction" sysfs attribute which may be used to change
  566. * the gpio's direction. A "value" attribute will always be provided.
  567. *
  568. * Returns zero on success, else an error.
  569. */
  570. int gpio_export(unsigned gpio, bool direction_may_change)
  571. {
  572. unsigned long flags;
  573. struct gpio_desc *desc;
  574. int status;
  575. const char *ioname = NULL;
  576. struct device *dev;
  577. /* can't export until sysfs is available ... */
  578. if (!gpio_class.p) {
  579. pr_debug("%s: called too early!\n", __func__);
  580. return -ENOENT;
  581. }
  582. if (!gpio_is_valid(gpio)) {
  583. pr_debug("%s: gpio %d is not valid\n", __func__, gpio);
  584. return -EINVAL;
  585. }
  586. mutex_lock(&sysfs_lock);
  587. spin_lock_irqsave(&gpio_lock, flags);
  588. desc = &gpio_desc[gpio];
  589. if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
  590. test_bit(FLAG_EXPORT, &desc->flags)) {
  591. spin_unlock_irqrestore(&gpio_lock, flags);
  592. pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
  593. __func__, gpio,
  594. test_bit(FLAG_REQUESTED, &desc->flags),
  595. test_bit(FLAG_EXPORT, &desc->flags));
  596. status = -EPERM;
  597. goto fail_unlock;
  598. }
  599. if (!desc->chip->direction_input || !desc->chip->direction_output)
  600. direction_may_change = false;
  601. spin_unlock_irqrestore(&gpio_lock, flags);
  602. if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
  603. ioname = desc->chip->names[gpio - desc->chip->base];
  604. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  605. desc, ioname ? ioname : "gpio%u", gpio);
  606. if (IS_ERR(dev)) {
  607. status = PTR_ERR(dev);
  608. goto fail_unlock;
  609. }
  610. status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
  611. if (status)
  612. goto fail_unregister_device;
  613. if (direction_may_change) {
  614. status = device_create_file(dev, &dev_attr_direction);
  615. if (status)
  616. goto fail_unregister_device;
  617. }
  618. if (gpio_to_irq(gpio) >= 0 && (direction_may_change ||
  619. !test_bit(FLAG_IS_OUT, &desc->flags))) {
  620. status = device_create_file(dev, &dev_attr_edge);
  621. if (status)
  622. goto fail_unregister_device;
  623. }
  624. set_bit(FLAG_EXPORT, &desc->flags);
  625. mutex_unlock(&sysfs_lock);
  626. return 0;
  627. fail_unregister_device:
  628. device_unregister(dev);
  629. fail_unlock:
  630. mutex_unlock(&sysfs_lock);
  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. * and in case chip->get_direction is not set,
  889. * we may expose the wrong direction in sysfs.
  890. */
  891. gpio_desc[id].flags = !chip->direction_input
  892. ? (1 << FLAG_IS_OUT)
  893. : 0;
  894. }
  895. }
  896. #ifdef CONFIG_PINCTRL
  897. INIT_LIST_HEAD(&chip->pin_ranges);
  898. #endif
  899. of_gpiochip_add(chip);
  900. unlock:
  901. spin_unlock_irqrestore(&gpio_lock, flags);
  902. if (status)
  903. goto fail;
  904. status = gpiochip_export(chip);
  905. if (status)
  906. goto fail;
  907. pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
  908. chip->base, chip->base + chip->ngpio - 1,
  909. chip->label ? : "generic");
  910. return 0;
  911. fail:
  912. /* failures here can mean systems won't boot... */
  913. pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
  914. chip->base, chip->base + chip->ngpio - 1,
  915. chip->label ? : "generic");
  916. return status;
  917. }
  918. EXPORT_SYMBOL_GPL(gpiochip_add);
  919. /**
  920. * gpiochip_remove() - unregister a gpio_chip
  921. * @chip: the chip to unregister
  922. *
  923. * A gpio_chip with any GPIOs still requested may not be removed.
  924. */
  925. int gpiochip_remove(struct gpio_chip *chip)
  926. {
  927. unsigned long flags;
  928. int status = 0;
  929. unsigned id;
  930. spin_lock_irqsave(&gpio_lock, flags);
  931. gpiochip_remove_pin_ranges(chip);
  932. of_gpiochip_remove(chip);
  933. for (id = chip->base; id < chip->base + chip->ngpio; id++) {
  934. if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
  935. status = -EBUSY;
  936. break;
  937. }
  938. }
  939. if (status == 0) {
  940. for (id = chip->base; id < chip->base + chip->ngpio; id++)
  941. gpio_desc[id].chip = NULL;
  942. }
  943. spin_unlock_irqrestore(&gpio_lock, flags);
  944. if (status == 0)
  945. gpiochip_unexport(chip);
  946. return status;
  947. }
  948. EXPORT_SYMBOL_GPL(gpiochip_remove);
  949. /**
  950. * gpiochip_find() - iterator for locating a specific gpio_chip
  951. * @data: data to pass to match function
  952. * @callback: Callback function to check gpio_chip
  953. *
  954. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  955. * determined by a user supplied @match callback. The callback should return
  956. * 0 if the device doesn't match and non-zero if it does. If the callback is
  957. * non-zero, this function will return to the caller and not iterate over any
  958. * more gpio_chips.
  959. */
  960. struct gpio_chip *gpiochip_find(void *data,
  961. int (*match)(struct gpio_chip *chip,
  962. void *data))
  963. {
  964. struct gpio_chip *chip = NULL;
  965. unsigned long flags;
  966. int i;
  967. spin_lock_irqsave(&gpio_lock, flags);
  968. for (i = 0; i < ARCH_NR_GPIOS; i++) {
  969. if (!gpio_desc[i].chip)
  970. continue;
  971. if (match(gpio_desc[i].chip, data)) {
  972. chip = gpio_desc[i].chip;
  973. break;
  974. }
  975. }
  976. spin_unlock_irqrestore(&gpio_lock, flags);
  977. return chip;
  978. }
  979. EXPORT_SYMBOL_GPL(gpiochip_find);
  980. #ifdef CONFIG_PINCTRL
  981. /**
  982. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  983. * @chip: the gpiochip to add the range for
  984. * @pinctrl_name: the dev_name() of the pin controller to map to
  985. * @gpio_offset: the start offset in the current gpio_chip number space
  986. * @pin_offset: the start offset in the pin controller number space
  987. * @npins: the number of pins from the offset of each pin space (GPIO and
  988. * pin controller) to accumulate in this range
  989. */
  990. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  991. unsigned int gpio_offset, unsigned int pin_offset,
  992. unsigned int npins)
  993. {
  994. struct gpio_pin_range *pin_range;
  995. int ret;
  996. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  997. if (!pin_range) {
  998. pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
  999. chip->label);
  1000. return -ENOMEM;
  1001. }
  1002. /* Use local offset as range ID */
  1003. pin_range->range.id = gpio_offset;
  1004. pin_range->range.gc = chip;
  1005. pin_range->range.name = chip->label;
  1006. pin_range->range.base = chip->base + gpio_offset;
  1007. pin_range->range.pin_base = pin_offset;
  1008. pin_range->range.npins = npins;
  1009. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1010. &pin_range->range);
  1011. if (IS_ERR(pin_range->pctldev)) {
  1012. ret = PTR_ERR(pin_range->pctldev);
  1013. pr_err("%s: GPIO chip: could not create pin range\n",
  1014. chip->label);
  1015. kfree(pin_range);
  1016. return ret;
  1017. }
  1018. pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1019. chip->label, gpio_offset, gpio_offset + npins - 1,
  1020. pinctl_name,
  1021. pin_offset, pin_offset + npins - 1);
  1022. list_add_tail(&pin_range->node, &chip->pin_ranges);
  1023. return 0;
  1024. }
  1025. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1026. /**
  1027. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1028. * @chip: the chip to remove all the mappings for
  1029. */
  1030. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  1031. {
  1032. struct gpio_pin_range *pin_range, *tmp;
  1033. list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
  1034. list_del(&pin_range->node);
  1035. pinctrl_remove_gpio_range(pin_range->pctldev,
  1036. &pin_range->range);
  1037. kfree(pin_range);
  1038. }
  1039. }
  1040. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1041. #endif /* CONFIG_PINCTRL */
  1042. /* These "optional" allocation calls help prevent drivers from stomping
  1043. * on each other, and help provide better diagnostics in debugfs.
  1044. * They're called even less than the "set direction" calls.
  1045. */
  1046. int gpio_request(unsigned gpio, const char *label)
  1047. {
  1048. struct gpio_desc *desc;
  1049. struct gpio_chip *chip;
  1050. int status = -EPROBE_DEFER;
  1051. unsigned long flags;
  1052. spin_lock_irqsave(&gpio_lock, flags);
  1053. if (!gpio_is_valid(gpio)) {
  1054. status = -EINVAL;
  1055. goto done;
  1056. }
  1057. desc = &gpio_desc[gpio];
  1058. chip = desc->chip;
  1059. if (chip == NULL)
  1060. goto done;
  1061. if (!try_module_get(chip->owner))
  1062. goto done;
  1063. /* NOTE: gpio_request() can be called in early boot,
  1064. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1065. */
  1066. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1067. desc_set_label(desc, label ? : "?");
  1068. status = 0;
  1069. } else {
  1070. status = -EBUSY;
  1071. module_put(chip->owner);
  1072. goto done;
  1073. }
  1074. if (chip->request) {
  1075. /* chip->request may sleep */
  1076. spin_unlock_irqrestore(&gpio_lock, flags);
  1077. status = chip->request(chip, gpio - chip->base);
  1078. spin_lock_irqsave(&gpio_lock, flags);
  1079. if (status < 0) {
  1080. desc_set_label(desc, NULL);
  1081. module_put(chip->owner);
  1082. clear_bit(FLAG_REQUESTED, &desc->flags);
  1083. goto done;
  1084. }
  1085. }
  1086. if (chip->get_direction) {
  1087. /* chip->get_direction may sleep */
  1088. spin_unlock_irqrestore(&gpio_lock, flags);
  1089. gpio_get_direction(gpio);
  1090. spin_lock_irqsave(&gpio_lock, flags);
  1091. }
  1092. done:
  1093. if (status)
  1094. pr_debug("gpio_request: gpio-%d (%s) status %d\n",
  1095. gpio, label ? : "?", status);
  1096. spin_unlock_irqrestore(&gpio_lock, flags);
  1097. return status;
  1098. }
  1099. EXPORT_SYMBOL_GPL(gpio_request);
  1100. void gpio_free(unsigned gpio)
  1101. {
  1102. unsigned long flags;
  1103. struct gpio_desc *desc;
  1104. struct gpio_chip *chip;
  1105. might_sleep();
  1106. if (!gpio_is_valid(gpio)) {
  1107. WARN_ON(extra_checks);
  1108. return;
  1109. }
  1110. gpio_unexport(gpio);
  1111. spin_lock_irqsave(&gpio_lock, flags);
  1112. desc = &gpio_desc[gpio];
  1113. chip = desc->chip;
  1114. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1115. if (chip->free) {
  1116. spin_unlock_irqrestore(&gpio_lock, flags);
  1117. might_sleep_if(chip->can_sleep);
  1118. chip->free(chip, gpio - chip->base);
  1119. spin_lock_irqsave(&gpio_lock, flags);
  1120. }
  1121. desc_set_label(desc, NULL);
  1122. module_put(desc->chip->owner);
  1123. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1124. clear_bit(FLAG_REQUESTED, &desc->flags);
  1125. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1126. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1127. } else
  1128. WARN_ON(extra_checks);
  1129. spin_unlock_irqrestore(&gpio_lock, flags);
  1130. }
  1131. EXPORT_SYMBOL_GPL(gpio_free);
  1132. /**
  1133. * gpio_request_one - request a single GPIO with initial configuration
  1134. * @gpio: the GPIO number
  1135. * @flags: GPIO configuration as specified by GPIOF_*
  1136. * @label: a literal description string of this GPIO
  1137. */
  1138. int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  1139. {
  1140. int err;
  1141. err = gpio_request(gpio, label);
  1142. if (err)
  1143. return err;
  1144. if (flags & GPIOF_OPEN_DRAIN)
  1145. set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
  1146. if (flags & GPIOF_OPEN_SOURCE)
  1147. set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
  1148. if (flags & GPIOF_DIR_IN)
  1149. err = gpio_direction_input(gpio);
  1150. else
  1151. err = gpio_direction_output(gpio,
  1152. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  1153. if (err)
  1154. goto free_gpio;
  1155. if (flags & GPIOF_EXPORT) {
  1156. err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
  1157. if (err)
  1158. goto free_gpio;
  1159. }
  1160. return 0;
  1161. free_gpio:
  1162. gpio_free(gpio);
  1163. return err;
  1164. }
  1165. EXPORT_SYMBOL_GPL(gpio_request_one);
  1166. /**
  1167. * gpio_request_array - request multiple GPIOs in a single call
  1168. * @array: array of the 'struct gpio'
  1169. * @num: how many GPIOs in the array
  1170. */
  1171. int gpio_request_array(const struct gpio *array, size_t num)
  1172. {
  1173. int i, err;
  1174. for (i = 0; i < num; i++, array++) {
  1175. err = gpio_request_one(array->gpio, array->flags, array->label);
  1176. if (err)
  1177. goto err_free;
  1178. }
  1179. return 0;
  1180. err_free:
  1181. while (i--)
  1182. gpio_free((--array)->gpio);
  1183. return err;
  1184. }
  1185. EXPORT_SYMBOL_GPL(gpio_request_array);
  1186. /**
  1187. * gpio_free_array - release multiple GPIOs in a single call
  1188. * @array: array of the 'struct gpio'
  1189. * @num: how many GPIOs in the array
  1190. */
  1191. void gpio_free_array(const struct gpio *array, size_t num)
  1192. {
  1193. while (num--)
  1194. gpio_free((array++)->gpio);
  1195. }
  1196. EXPORT_SYMBOL_GPL(gpio_free_array);
  1197. /**
  1198. * gpiochip_is_requested - return string iff signal was requested
  1199. * @chip: controller managing the signal
  1200. * @offset: of signal within controller's 0..(ngpio - 1) range
  1201. *
  1202. * Returns NULL if the GPIO is not currently requested, else a string.
  1203. * If debugfs support is enabled, the string returned is the label passed
  1204. * to gpio_request(); otherwise it is a meaningless constant.
  1205. *
  1206. * This function is for use by GPIO controller drivers. The label can
  1207. * help with diagnostics, and knowing that the signal is used as a GPIO
  1208. * can help avoid accidentally multiplexing it to another controller.
  1209. */
  1210. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1211. {
  1212. unsigned gpio = chip->base + offset;
  1213. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  1214. return NULL;
  1215. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  1216. return NULL;
  1217. #ifdef CONFIG_DEBUG_FS
  1218. return gpio_desc[gpio].label;
  1219. #else
  1220. return "?";
  1221. #endif
  1222. }
  1223. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1224. /* Drivers MUST set GPIO direction before making get/set calls. In
  1225. * some cases this is done in early boot, before IRQs are enabled.
  1226. *
  1227. * As a rule these aren't called more than once (except for drivers
  1228. * using the open-drain emulation idiom) so these are natural places
  1229. * to accumulate extra debugging checks. Note that we can't (yet)
  1230. * rely on gpio_request() having been called beforehand.
  1231. */
  1232. int gpio_direction_input(unsigned gpio)
  1233. {
  1234. unsigned long flags;
  1235. struct gpio_chip *chip;
  1236. struct gpio_desc *desc = &gpio_desc[gpio];
  1237. int status = -EINVAL;
  1238. spin_lock_irqsave(&gpio_lock, flags);
  1239. if (!gpio_is_valid(gpio))
  1240. goto fail;
  1241. chip = desc->chip;
  1242. if (!chip || !chip->get || !chip->direction_input)
  1243. goto fail;
  1244. gpio -= chip->base;
  1245. if (gpio >= chip->ngpio)
  1246. goto fail;
  1247. status = gpio_ensure_requested(desc, gpio);
  1248. if (status < 0)
  1249. goto fail;
  1250. /* now we know the gpio is valid and chip won't vanish */
  1251. spin_unlock_irqrestore(&gpio_lock, flags);
  1252. might_sleep_if(chip->can_sleep);
  1253. if (status) {
  1254. status = chip->request(chip, gpio);
  1255. if (status < 0) {
  1256. pr_debug("GPIO-%d: chip request fail, %d\n",
  1257. chip->base + gpio, status);
  1258. /* and it's not available to anyone else ...
  1259. * gpio_request() is the fully clean solution.
  1260. */
  1261. goto lose;
  1262. }
  1263. }
  1264. status = chip->direction_input(chip, gpio);
  1265. if (status == 0)
  1266. clear_bit(FLAG_IS_OUT, &desc->flags);
  1267. trace_gpio_direction(chip->base + gpio, 1, status);
  1268. lose:
  1269. return status;
  1270. fail:
  1271. spin_unlock_irqrestore(&gpio_lock, flags);
  1272. if (status)
  1273. pr_debug("%s: gpio-%d status %d\n",
  1274. __func__, gpio, status);
  1275. return status;
  1276. }
  1277. EXPORT_SYMBOL_GPL(gpio_direction_input);
  1278. int gpio_direction_output(unsigned gpio, int value)
  1279. {
  1280. unsigned long flags;
  1281. struct gpio_chip *chip;
  1282. struct gpio_desc *desc = &gpio_desc[gpio];
  1283. int status = -EINVAL;
  1284. /* Open drain pin should not be driven to 1 */
  1285. if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1286. return gpio_direction_input(gpio);
  1287. /* Open source pin should not be driven to 0 */
  1288. if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1289. return gpio_direction_input(gpio);
  1290. spin_lock_irqsave(&gpio_lock, flags);
  1291. if (!gpio_is_valid(gpio))
  1292. goto fail;
  1293. chip = desc->chip;
  1294. if (!chip || !chip->set || !chip->direction_output)
  1295. goto fail;
  1296. gpio -= chip->base;
  1297. if (gpio >= chip->ngpio)
  1298. goto fail;
  1299. status = gpio_ensure_requested(desc, gpio);
  1300. if (status < 0)
  1301. goto fail;
  1302. /* now we know the gpio is valid and chip won't vanish */
  1303. spin_unlock_irqrestore(&gpio_lock, flags);
  1304. might_sleep_if(chip->can_sleep);
  1305. if (status) {
  1306. status = chip->request(chip, gpio);
  1307. if (status < 0) {
  1308. pr_debug("GPIO-%d: chip request fail, %d\n",
  1309. chip->base + gpio, status);
  1310. /* and it's not available to anyone else ...
  1311. * gpio_request() is the fully clean solution.
  1312. */
  1313. goto lose;
  1314. }
  1315. }
  1316. status = chip->direction_output(chip, gpio, value);
  1317. if (status == 0)
  1318. set_bit(FLAG_IS_OUT, &desc->flags);
  1319. trace_gpio_value(chip->base + gpio, 0, value);
  1320. trace_gpio_direction(chip->base + gpio, 0, status);
  1321. lose:
  1322. return status;
  1323. fail:
  1324. spin_unlock_irqrestore(&gpio_lock, flags);
  1325. if (status)
  1326. pr_debug("%s: gpio-%d status %d\n",
  1327. __func__, gpio, status);
  1328. return status;
  1329. }
  1330. EXPORT_SYMBOL_GPL(gpio_direction_output);
  1331. /**
  1332. * gpio_set_debounce - sets @debounce time for a @gpio
  1333. * @gpio: the gpio to set debounce time
  1334. * @debounce: debounce time is microseconds
  1335. */
  1336. int gpio_set_debounce(unsigned gpio, unsigned debounce)
  1337. {
  1338. unsigned long flags;
  1339. struct gpio_chip *chip;
  1340. struct gpio_desc *desc = &gpio_desc[gpio];
  1341. int status = -EINVAL;
  1342. spin_lock_irqsave(&gpio_lock, flags);
  1343. if (!gpio_is_valid(gpio))
  1344. goto fail;
  1345. chip = desc->chip;
  1346. if (!chip || !chip->set || !chip->set_debounce)
  1347. goto fail;
  1348. gpio -= chip->base;
  1349. if (gpio >= chip->ngpio)
  1350. goto fail;
  1351. status = gpio_ensure_requested(desc, gpio);
  1352. if (status < 0)
  1353. goto fail;
  1354. /* now we know the gpio is valid and chip won't vanish */
  1355. spin_unlock_irqrestore(&gpio_lock, flags);
  1356. might_sleep_if(chip->can_sleep);
  1357. return chip->set_debounce(chip, gpio, debounce);
  1358. fail:
  1359. spin_unlock_irqrestore(&gpio_lock, flags);
  1360. if (status)
  1361. pr_debug("%s: gpio-%d status %d\n",
  1362. __func__, gpio, status);
  1363. return status;
  1364. }
  1365. EXPORT_SYMBOL_GPL(gpio_set_debounce);
  1366. /* I/O calls are only valid after configuration completed; the relevant
  1367. * "is this a valid GPIO" error checks should already have been done.
  1368. *
  1369. * "Get" operations are often inlinable as reading a pin value register,
  1370. * and masking the relevant bit in that register.
  1371. *
  1372. * When "set" operations are inlinable, they involve writing that mask to
  1373. * one register to set a low value, or a different register to set it high.
  1374. * Otherwise locking is needed, so there may be little value to inlining.
  1375. *
  1376. *------------------------------------------------------------------------
  1377. *
  1378. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1379. * have requested the GPIO. That can include implicit requesting by
  1380. * a direction setting call. Marking a gpio as requested locks its chip
  1381. * in memory, guaranteeing that these table lookups need no more locking
  1382. * and that gpiochip_remove() will fail.
  1383. *
  1384. * REVISIT when debugging, consider adding some instrumentation to ensure
  1385. * that the GPIO was actually requested.
  1386. */
  1387. /**
  1388. * __gpio_get_value() - return a gpio's value
  1389. * @gpio: gpio whose value will be returned
  1390. * Context: any
  1391. *
  1392. * This is used directly or indirectly to implement gpio_get_value().
  1393. * It returns the zero or nonzero value provided by the associated
  1394. * gpio_chip.get() method; or zero if no such method is provided.
  1395. */
  1396. int __gpio_get_value(unsigned gpio)
  1397. {
  1398. struct gpio_chip *chip;
  1399. int value;
  1400. chip = gpio_to_chip(gpio);
  1401. /* Should be using gpio_get_value_cansleep() */
  1402. WARN_ON(chip->can_sleep);
  1403. value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1404. trace_gpio_value(gpio, 1, value);
  1405. return value;
  1406. }
  1407. EXPORT_SYMBOL_GPL(__gpio_get_value);
  1408. /*
  1409. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  1410. * @gpio: Gpio whose state need to be set.
  1411. * @chip: Gpio chip.
  1412. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1413. */
  1414. static void _gpio_set_open_drain_value(unsigned gpio,
  1415. struct gpio_chip *chip, int value)
  1416. {
  1417. int err = 0;
  1418. if (value) {
  1419. err = chip->direction_input(chip, gpio - chip->base);
  1420. if (!err)
  1421. clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
  1422. } else {
  1423. err = chip->direction_output(chip, gpio - chip->base, 0);
  1424. if (!err)
  1425. set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
  1426. }
  1427. trace_gpio_direction(gpio, value, err);
  1428. if (err < 0)
  1429. pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
  1430. __func__, gpio, err);
  1431. }
  1432. /*
  1433. * _gpio_set_open_source() - Set the open source gpio's value.
  1434. * @gpio: Gpio whose state need to be set.
  1435. * @chip: Gpio chip.
  1436. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1437. */
  1438. static void _gpio_set_open_source_value(unsigned gpio,
  1439. struct gpio_chip *chip, int value)
  1440. {
  1441. int err = 0;
  1442. if (value) {
  1443. err = chip->direction_output(chip, gpio - chip->base, 1);
  1444. if (!err)
  1445. set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
  1446. } else {
  1447. err = chip->direction_input(chip, gpio - chip->base);
  1448. if (!err)
  1449. clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
  1450. }
  1451. trace_gpio_direction(gpio, !value, err);
  1452. if (err < 0)
  1453. pr_err("%s: Error in set_value for open source gpio%d err %d\n",
  1454. __func__, gpio, err);
  1455. }
  1456. /**
  1457. * __gpio_set_value() - assign a gpio's value
  1458. * @gpio: gpio whose value will be assigned
  1459. * @value: value to assign
  1460. * Context: any
  1461. *
  1462. * This is used directly or indirectly to implement gpio_set_value().
  1463. * It invokes the associated gpio_chip.set() method.
  1464. */
  1465. void __gpio_set_value(unsigned gpio, int value)
  1466. {
  1467. struct gpio_chip *chip;
  1468. chip = gpio_to_chip(gpio);
  1469. /* Should be using gpio_set_value_cansleep() */
  1470. WARN_ON(chip->can_sleep);
  1471. trace_gpio_value(gpio, 0, value);
  1472. if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
  1473. _gpio_set_open_drain_value(gpio, chip, value);
  1474. else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
  1475. _gpio_set_open_source_value(gpio, chip, value);
  1476. else
  1477. chip->set(chip, gpio - chip->base, value);
  1478. }
  1479. EXPORT_SYMBOL_GPL(__gpio_set_value);
  1480. /**
  1481. * __gpio_cansleep() - report whether gpio value access will sleep
  1482. * @gpio: gpio in question
  1483. * Context: any
  1484. *
  1485. * This is used directly or indirectly to implement gpio_cansleep(). It
  1486. * returns nonzero if access reading or writing the GPIO value can sleep.
  1487. */
  1488. int __gpio_cansleep(unsigned gpio)
  1489. {
  1490. struct gpio_chip *chip;
  1491. /* only call this on GPIOs that are valid! */
  1492. chip = gpio_to_chip(gpio);
  1493. return chip->can_sleep;
  1494. }
  1495. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  1496. /**
  1497. * __gpio_to_irq() - return the IRQ corresponding to a GPIO
  1498. * @gpio: gpio whose IRQ will be returned (already requested)
  1499. * Context: any
  1500. *
  1501. * This is used directly or indirectly to implement gpio_to_irq().
  1502. * It returns the number of the IRQ signaled by this (input) GPIO,
  1503. * or a negative errno.
  1504. */
  1505. int __gpio_to_irq(unsigned gpio)
  1506. {
  1507. struct gpio_chip *chip;
  1508. chip = gpio_to_chip(gpio);
  1509. return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
  1510. }
  1511. EXPORT_SYMBOL_GPL(__gpio_to_irq);
  1512. /* There's no value in making it easy to inline GPIO calls that may sleep.
  1513. * Common examples include ones connected to I2C or SPI chips.
  1514. */
  1515. int gpio_get_value_cansleep(unsigned gpio)
  1516. {
  1517. struct gpio_chip *chip;
  1518. int value;
  1519. might_sleep_if(extra_checks);
  1520. chip = gpio_to_chip(gpio);
  1521. value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1522. trace_gpio_value(gpio, 1, value);
  1523. return value;
  1524. }
  1525. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  1526. void gpio_set_value_cansleep(unsigned gpio, int value)
  1527. {
  1528. struct gpio_chip *chip;
  1529. might_sleep_if(extra_checks);
  1530. chip = gpio_to_chip(gpio);
  1531. trace_gpio_value(gpio, 0, value);
  1532. if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
  1533. _gpio_set_open_drain_value(gpio, chip, value);
  1534. else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
  1535. _gpio_set_open_source_value(gpio, chip, value);
  1536. else
  1537. chip->set(chip, gpio - chip->base, value);
  1538. }
  1539. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  1540. #ifdef CONFIG_DEBUG_FS
  1541. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1542. {
  1543. unsigned i;
  1544. unsigned gpio = chip->base;
  1545. struct gpio_desc *gdesc = &gpio_desc[gpio];
  1546. int is_out;
  1547. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1548. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1549. continue;
  1550. gpio_get_direction(gpio);
  1551. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1552. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
  1553. gpio, gdesc->label,
  1554. is_out ? "out" : "in ",
  1555. chip->get
  1556. ? (chip->get(chip, i) ? "hi" : "lo")
  1557. : "? ");
  1558. seq_printf(s, "\n");
  1559. }
  1560. }
  1561. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  1562. {
  1563. struct gpio_chip *chip = NULL;
  1564. unsigned int gpio;
  1565. void *ret = NULL;
  1566. loff_t index = 0;
  1567. /* REVISIT this isn't locked against gpio_chip removal ... */
  1568. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  1569. if (gpio_desc[gpio].chip == chip)
  1570. continue;
  1571. chip = gpio_desc[gpio].chip;
  1572. if (!chip)
  1573. continue;
  1574. if (index++ >= *pos) {
  1575. ret = chip;
  1576. break;
  1577. }
  1578. }
  1579. s->private = "";
  1580. return ret;
  1581. }
  1582. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1583. {
  1584. struct gpio_chip *chip = v;
  1585. unsigned int gpio;
  1586. void *ret = NULL;
  1587. /* skip GPIOs provided by the current chip */
  1588. for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
  1589. chip = gpio_desc[gpio].chip;
  1590. if (chip) {
  1591. ret = chip;
  1592. break;
  1593. }
  1594. }
  1595. s->private = "\n";
  1596. ++*pos;
  1597. return ret;
  1598. }
  1599. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  1600. {
  1601. }
  1602. static int gpiolib_seq_show(struct seq_file *s, void *v)
  1603. {
  1604. struct gpio_chip *chip = v;
  1605. struct device *dev;
  1606. seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
  1607. chip->base, chip->base + chip->ngpio - 1);
  1608. dev = chip->dev;
  1609. if (dev)
  1610. seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
  1611. dev_name(dev));
  1612. if (chip->label)
  1613. seq_printf(s, ", %s", chip->label);
  1614. if (chip->can_sleep)
  1615. seq_printf(s, ", can sleep");
  1616. seq_printf(s, ":\n");
  1617. if (chip->dbg_show)
  1618. chip->dbg_show(s, chip);
  1619. else
  1620. gpiolib_dbg_show(s, chip);
  1621. return 0;
  1622. }
  1623. static const struct seq_operations gpiolib_seq_ops = {
  1624. .start = gpiolib_seq_start,
  1625. .next = gpiolib_seq_next,
  1626. .stop = gpiolib_seq_stop,
  1627. .show = gpiolib_seq_show,
  1628. };
  1629. static int gpiolib_open(struct inode *inode, struct file *file)
  1630. {
  1631. return seq_open(file, &gpiolib_seq_ops);
  1632. }
  1633. static const struct file_operations gpiolib_operations = {
  1634. .owner = THIS_MODULE,
  1635. .open = gpiolib_open,
  1636. .read = seq_read,
  1637. .llseek = seq_lseek,
  1638. .release = seq_release,
  1639. };
  1640. static int __init gpiolib_debugfs_init(void)
  1641. {
  1642. /* /sys/kernel/debug/gpio */
  1643. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1644. NULL, NULL, &gpiolib_operations);
  1645. return 0;
  1646. }
  1647. subsys_initcall(gpiolib_debugfs_init);
  1648. #endif /* DEBUG_FS */