gpiolib.c 49 KB

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