gpiolib.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/irq.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/gpio.h>
  10. /* Optional implementation infrastructure for GPIO interfaces.
  11. *
  12. * Platforms may want to use this if they tend to use very many GPIOs
  13. * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
  14. *
  15. * When kernel footprint or instruction count is an issue, simpler
  16. * implementations may be preferred. The GPIO programming interface
  17. * allows for inlining speed-critical get/set operations for common
  18. * cases, so that access to SOC-integrated GPIOs can sometimes cost
  19. * only an instruction or two per bit.
  20. */
  21. /* When debugging, extend minimal trust to callers and platform code.
  22. * Also emit diagnostic messages that may help initial bringup, when
  23. * board setup or driver bugs are most common.
  24. *
  25. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  26. */
  27. #ifdef DEBUG
  28. #define extra_checks 1
  29. #else
  30. #define extra_checks 0
  31. #endif
  32. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  33. * While any GPIO is requested, its gpio_chip is not removable;
  34. * each GPIO's "requested" flag serves as a lock and refcount.
  35. */
  36. static DEFINE_SPINLOCK(gpio_lock);
  37. struct gpio_desc {
  38. struct gpio_chip *chip;
  39. unsigned long flags;
  40. /* flag symbols are bit numbers */
  41. #define FLAG_REQUESTED 0
  42. #define FLAG_IS_OUT 1
  43. #define FLAG_RESERVED 2
  44. #define FLAG_EXPORT 3 /* protected by sysfs_lock */
  45. #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
  46. #ifdef CONFIG_DEBUG_FS
  47. const char *label;
  48. #endif
  49. };
  50. static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
  51. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  52. {
  53. #ifdef CONFIG_DEBUG_FS
  54. d->label = label;
  55. #endif
  56. }
  57. /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
  58. * when setting direction, and otherwise illegal. Until board setup code
  59. * and drivers use explicit requests everywhere (which won't happen when
  60. * those calls have no teeth) we can't avoid autorequesting. This nag
  61. * message should motivate switching to explicit requests...
  62. */
  63. static void gpio_ensure_requested(struct gpio_desc *desc)
  64. {
  65. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  66. pr_warning("GPIO-%d autorequested\n", (int)(desc - gpio_desc));
  67. desc_set_label(desc, "[auto]");
  68. if (!try_module_get(desc->chip->owner))
  69. pr_err("GPIO-%d: module can't be gotten \n",
  70. (int)(desc - gpio_desc));
  71. }
  72. }
  73. /* caller holds gpio_lock *OR* gpio is marked as requested */
  74. static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
  75. {
  76. return gpio_desc[gpio].chip;
  77. }
  78. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  79. static int gpiochip_find_base(int ngpio)
  80. {
  81. int i;
  82. int spare = 0;
  83. int base = -ENOSPC;
  84. for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
  85. struct gpio_desc *desc = &gpio_desc[i];
  86. struct gpio_chip *chip = desc->chip;
  87. if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
  88. spare++;
  89. if (spare == ngpio) {
  90. base = i;
  91. break;
  92. }
  93. } else {
  94. spare = 0;
  95. if (chip)
  96. i -= chip->ngpio - 1;
  97. }
  98. }
  99. if (gpio_is_valid(base))
  100. pr_debug("%s: found new base at %d\n", __func__, base);
  101. return base;
  102. }
  103. /**
  104. * gpiochip_reserve() - reserve range of gpios to use with platform code only
  105. * @start: starting gpio number
  106. * @ngpio: number of gpios to reserve
  107. * Context: platform init, potentially before irqs or kmalloc will work
  108. *
  109. * Returns a negative errno if any gpio within the range is already reserved
  110. * or registered, else returns zero as a success code. Use this function
  111. * to mark a range of gpios as unavailable for dynamic gpio number allocation,
  112. * for example because its driver support is not yet loaded.
  113. */
  114. int __init gpiochip_reserve(int start, int ngpio)
  115. {
  116. int ret = 0;
  117. unsigned long flags;
  118. int i;
  119. if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
  120. return -EINVAL;
  121. spin_lock_irqsave(&gpio_lock, flags);
  122. for (i = start; i < start + ngpio; i++) {
  123. struct gpio_desc *desc = &gpio_desc[i];
  124. if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
  125. ret = -EBUSY;
  126. goto err;
  127. }
  128. set_bit(FLAG_RESERVED, &desc->flags);
  129. }
  130. pr_debug("%s: reserved gpios from %d to %d\n",
  131. __func__, start, start + ngpio - 1);
  132. err:
  133. spin_unlock_irqrestore(&gpio_lock, flags);
  134. return ret;
  135. }
  136. #ifdef CONFIG_GPIO_SYSFS
  137. /* lock protects against unexport_gpio() being called while
  138. * sysfs files are active.
  139. */
  140. static DEFINE_MUTEX(sysfs_lock);
  141. /*
  142. * /sys/class/gpio/gpioN... only for GPIOs that are exported
  143. * /direction
  144. * * MAY BE OMITTED if kernel won't allow direction changes
  145. * * is read/write as "in" or "out"
  146. * * may also be written as "high" or "low", initializing
  147. * output value as specified ("out" implies "low")
  148. * /value
  149. * * always readable, subject to hardware behavior
  150. * * may be writable, as zero/nonzero
  151. *
  152. * REVISIT there will likely be an attribute for configuring async
  153. * notifications, e.g. to specify polling interval or IRQ trigger type
  154. * that would for example trigger a poll() on the "value".
  155. */
  156. static ssize_t gpio_direction_show(struct device *dev,
  157. struct device_attribute *attr, char *buf)
  158. {
  159. const struct gpio_desc *desc = dev_get_drvdata(dev);
  160. ssize_t status;
  161. mutex_lock(&sysfs_lock);
  162. if (!test_bit(FLAG_EXPORT, &desc->flags))
  163. status = -EIO;
  164. else
  165. status = sprintf(buf, "%s\n",
  166. test_bit(FLAG_IS_OUT, &desc->flags)
  167. ? "out" : "in");
  168. mutex_unlock(&sysfs_lock);
  169. return status;
  170. }
  171. static ssize_t gpio_direction_store(struct device *dev,
  172. struct device_attribute *attr, const char *buf, size_t size)
  173. {
  174. const struct gpio_desc *desc = dev_get_drvdata(dev);
  175. unsigned gpio = desc - gpio_desc;
  176. ssize_t status;
  177. mutex_lock(&sysfs_lock);
  178. if (!test_bit(FLAG_EXPORT, &desc->flags))
  179. status = -EIO;
  180. else if (sysfs_streq(buf, "high"))
  181. status = gpio_direction_output(gpio, 1);
  182. else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
  183. status = gpio_direction_output(gpio, 0);
  184. else if (sysfs_streq(buf, "in"))
  185. status = gpio_direction_input(gpio);
  186. else
  187. status = -EINVAL;
  188. mutex_unlock(&sysfs_lock);
  189. return status ? : size;
  190. }
  191. static const DEVICE_ATTR(direction, 0644,
  192. gpio_direction_show, gpio_direction_store);
  193. static ssize_t gpio_value_show(struct device *dev,
  194. struct device_attribute *attr, char *buf)
  195. {
  196. const struct gpio_desc *desc = dev_get_drvdata(dev);
  197. unsigned gpio = desc - gpio_desc;
  198. ssize_t status;
  199. mutex_lock(&sysfs_lock);
  200. if (!test_bit(FLAG_EXPORT, &desc->flags))
  201. status = -EIO;
  202. else
  203. status = sprintf(buf, "%d\n", gpio_get_value_cansleep(gpio));
  204. mutex_unlock(&sysfs_lock);
  205. return status;
  206. }
  207. static ssize_t gpio_value_store(struct device *dev,
  208. struct device_attribute *attr, const char *buf, size_t size)
  209. {
  210. const struct gpio_desc *desc = dev_get_drvdata(dev);
  211. unsigned gpio = desc - gpio_desc;
  212. ssize_t status;
  213. mutex_lock(&sysfs_lock);
  214. if (!test_bit(FLAG_EXPORT, &desc->flags))
  215. status = -EIO;
  216. else if (!test_bit(FLAG_IS_OUT, &desc->flags))
  217. status = -EPERM;
  218. else {
  219. long value;
  220. status = strict_strtol(buf, 0, &value);
  221. if (status == 0) {
  222. gpio_set_value_cansleep(gpio, value != 0);
  223. status = size;
  224. }
  225. }
  226. mutex_unlock(&sysfs_lock);
  227. return status;
  228. }
  229. static /*const*/ DEVICE_ATTR(value, 0644,
  230. gpio_value_show, gpio_value_store);
  231. static const struct attribute *gpio_attrs[] = {
  232. &dev_attr_direction.attr,
  233. &dev_attr_value.attr,
  234. NULL,
  235. };
  236. static const struct attribute_group gpio_attr_group = {
  237. .attrs = (struct attribute **) gpio_attrs,
  238. };
  239. /*
  240. * /sys/class/gpio/gpiochipN/
  241. * /base ... matching gpio_chip.base (N)
  242. * /label ... matching gpio_chip.label
  243. * /ngpio ... matching gpio_chip.ngpio
  244. */
  245. static ssize_t chip_base_show(struct device *dev,
  246. struct device_attribute *attr, char *buf)
  247. {
  248. const struct gpio_chip *chip = dev_get_drvdata(dev);
  249. return sprintf(buf, "%d\n", chip->base);
  250. }
  251. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  252. static ssize_t chip_label_show(struct device *dev,
  253. struct device_attribute *attr, char *buf)
  254. {
  255. const struct gpio_chip *chip = dev_get_drvdata(dev);
  256. return sprintf(buf, "%s\n", chip->label ? : "");
  257. }
  258. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  259. static ssize_t chip_ngpio_show(struct device *dev,
  260. struct device_attribute *attr, char *buf)
  261. {
  262. const struct gpio_chip *chip = dev_get_drvdata(dev);
  263. return sprintf(buf, "%u\n", chip->ngpio);
  264. }
  265. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  266. static const struct attribute *gpiochip_attrs[] = {
  267. &dev_attr_base.attr,
  268. &dev_attr_label.attr,
  269. &dev_attr_ngpio.attr,
  270. NULL,
  271. };
  272. static const struct attribute_group gpiochip_attr_group = {
  273. .attrs = (struct attribute **) gpiochip_attrs,
  274. };
  275. /*
  276. * /sys/class/gpio/export ... write-only
  277. * integer N ... number of GPIO to export (full access)
  278. * /sys/class/gpio/unexport ... write-only
  279. * integer N ... number of GPIO to unexport
  280. */
  281. static ssize_t export_store(struct class *class, const char *buf, size_t len)
  282. {
  283. long gpio;
  284. int status;
  285. status = strict_strtol(buf, 0, &gpio);
  286. if (status < 0)
  287. goto done;
  288. /* No extra locking here; FLAG_SYSFS just signifies that the
  289. * request and export were done by on behalf of userspace, so
  290. * they may be undone on its behalf too.
  291. */
  292. status = gpio_request(gpio, "sysfs");
  293. if (status < 0)
  294. goto done;
  295. status = gpio_export(gpio, true);
  296. if (status < 0)
  297. gpio_free(gpio);
  298. else
  299. set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
  300. done:
  301. if (status)
  302. pr_debug("%s: status %d\n", __func__, status);
  303. return status ? : len;
  304. }
  305. static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
  306. {
  307. long gpio;
  308. int status;
  309. status = strict_strtol(buf, 0, &gpio);
  310. if (status < 0)
  311. goto done;
  312. status = -EINVAL;
  313. /* reject bogus commands (gpio_unexport ignores them) */
  314. if (!gpio_is_valid(gpio))
  315. goto done;
  316. /* No extra locking here; FLAG_SYSFS just signifies that the
  317. * request and export were done by on behalf of userspace, so
  318. * they may be undone on its behalf too.
  319. */
  320. if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
  321. status = 0;
  322. gpio_free(gpio);
  323. }
  324. done:
  325. if (status)
  326. pr_debug("%s: status %d\n", __func__, status);
  327. return status ? : len;
  328. }
  329. static struct class_attribute gpio_class_attrs[] = {
  330. __ATTR(export, 0200, NULL, export_store),
  331. __ATTR(unexport, 0200, NULL, unexport_store),
  332. __ATTR_NULL,
  333. };
  334. static struct class gpio_class = {
  335. .name = "gpio",
  336. .owner = THIS_MODULE,
  337. .class_attrs = gpio_class_attrs,
  338. };
  339. /**
  340. * gpio_export - export a GPIO through sysfs
  341. * @gpio: gpio to make available, already requested
  342. * @direction_may_change: true if userspace may change gpio direction
  343. * Context: arch_initcall or later
  344. *
  345. * When drivers want to make a GPIO accessible to userspace after they
  346. * have requested it -- perhaps while debugging, or as part of their
  347. * public interface -- they may use this routine. If the GPIO can
  348. * change direction (some can't) and the caller allows it, userspace
  349. * will see "direction" sysfs attribute which may be used to change
  350. * the gpio's direction. A "value" attribute will always be provided.
  351. *
  352. * Returns zero on success, else an error.
  353. */
  354. int gpio_export(unsigned gpio, bool direction_may_change)
  355. {
  356. unsigned long flags;
  357. struct gpio_desc *desc;
  358. int status = -EINVAL;
  359. /* can't export until sysfs is available ... */
  360. if (!gpio_class.p) {
  361. pr_debug("%s: called too early!\n", __func__);
  362. return -ENOENT;
  363. }
  364. if (!gpio_is_valid(gpio))
  365. goto done;
  366. mutex_lock(&sysfs_lock);
  367. spin_lock_irqsave(&gpio_lock, flags);
  368. desc = &gpio_desc[gpio];
  369. if (test_bit(FLAG_REQUESTED, &desc->flags)
  370. && !test_bit(FLAG_EXPORT, &desc->flags)) {
  371. status = 0;
  372. if (!desc->chip->direction_input
  373. || !desc->chip->direction_output)
  374. direction_may_change = false;
  375. }
  376. spin_unlock_irqrestore(&gpio_lock, flags);
  377. if (status == 0) {
  378. struct device *dev;
  379. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  380. desc, "gpio%d", gpio);
  381. if (dev) {
  382. if (direction_may_change)
  383. status = sysfs_create_group(&dev->kobj,
  384. &gpio_attr_group);
  385. else
  386. status = device_create_file(dev,
  387. &dev_attr_value);
  388. if (status != 0)
  389. device_unregister(dev);
  390. } else
  391. status = -ENODEV;
  392. if (status == 0)
  393. set_bit(FLAG_EXPORT, &desc->flags);
  394. }
  395. mutex_unlock(&sysfs_lock);
  396. done:
  397. if (status)
  398. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  399. return status;
  400. }
  401. EXPORT_SYMBOL_GPL(gpio_export);
  402. static int match_export(struct device *dev, void *data)
  403. {
  404. return dev_get_drvdata(dev) == data;
  405. }
  406. /**
  407. * gpio_unexport - reverse effect of gpio_export()
  408. * @gpio: gpio to make unavailable
  409. *
  410. * This is implicit on gpio_free().
  411. */
  412. void gpio_unexport(unsigned gpio)
  413. {
  414. struct gpio_desc *desc;
  415. int status = -EINVAL;
  416. if (!gpio_is_valid(gpio))
  417. goto done;
  418. mutex_lock(&sysfs_lock);
  419. desc = &gpio_desc[gpio];
  420. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  421. struct device *dev = NULL;
  422. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  423. if (dev) {
  424. clear_bit(FLAG_EXPORT, &desc->flags);
  425. put_device(dev);
  426. device_unregister(dev);
  427. status = 0;
  428. } else
  429. status = -ENODEV;
  430. }
  431. mutex_unlock(&sysfs_lock);
  432. done:
  433. if (status)
  434. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  435. }
  436. EXPORT_SYMBOL_GPL(gpio_unexport);
  437. static int gpiochip_export(struct gpio_chip *chip)
  438. {
  439. int status;
  440. struct device *dev;
  441. /* Many systems register gpio chips for SOC support very early,
  442. * before driver model support is available. In those cases we
  443. * export this later, in gpiolib_sysfs_init() ... here we just
  444. * verify that _some_ field of gpio_class got initialized.
  445. */
  446. if (!gpio_class.p)
  447. return 0;
  448. /* use chip->base for the ID; it's already known to be unique */
  449. mutex_lock(&sysfs_lock);
  450. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  451. "gpiochip%d", chip->base);
  452. if (dev) {
  453. status = sysfs_create_group(&dev->kobj,
  454. &gpiochip_attr_group);
  455. } else
  456. status = -ENODEV;
  457. chip->exported = (status == 0);
  458. mutex_unlock(&sysfs_lock);
  459. if (status) {
  460. unsigned long flags;
  461. unsigned gpio;
  462. spin_lock_irqsave(&gpio_lock, flags);
  463. gpio = chip->base;
  464. while (gpio_desc[gpio].chip == chip)
  465. gpio_desc[gpio++].chip = NULL;
  466. spin_unlock_irqrestore(&gpio_lock, flags);
  467. pr_debug("%s: chip %s status %d\n", __func__,
  468. chip->label, status);
  469. }
  470. return status;
  471. }
  472. static void gpiochip_unexport(struct gpio_chip *chip)
  473. {
  474. int status;
  475. struct device *dev;
  476. mutex_lock(&sysfs_lock);
  477. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  478. if (dev) {
  479. put_device(dev);
  480. device_unregister(dev);
  481. chip->exported = 0;
  482. status = 0;
  483. } else
  484. status = -ENODEV;
  485. mutex_unlock(&sysfs_lock);
  486. if (status)
  487. pr_debug("%s: chip %s status %d\n", __func__,
  488. chip->label, status);
  489. }
  490. static int __init gpiolib_sysfs_init(void)
  491. {
  492. int status;
  493. unsigned long flags;
  494. unsigned gpio;
  495. status = class_register(&gpio_class);
  496. if (status < 0)
  497. return status;
  498. /* Scan and register the gpio_chips which registered very
  499. * early (e.g. before the class_register above was called).
  500. *
  501. * We run before arch_initcall() so chip->dev nodes can have
  502. * registered, and so arch_initcall() can always gpio_export().
  503. */
  504. spin_lock_irqsave(&gpio_lock, flags);
  505. for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
  506. struct gpio_chip *chip;
  507. chip = gpio_desc[gpio].chip;
  508. if (!chip || chip->exported)
  509. continue;
  510. spin_unlock_irqrestore(&gpio_lock, flags);
  511. status = gpiochip_export(chip);
  512. spin_lock_irqsave(&gpio_lock, flags);
  513. }
  514. spin_unlock_irqrestore(&gpio_lock, flags);
  515. return status;
  516. }
  517. postcore_initcall(gpiolib_sysfs_init);
  518. #else
  519. static inline int gpiochip_export(struct gpio_chip *chip)
  520. {
  521. return 0;
  522. }
  523. static inline void gpiochip_unexport(struct gpio_chip *chip)
  524. {
  525. }
  526. #endif /* CONFIG_GPIO_SYSFS */
  527. /**
  528. * gpiochip_add() - register a gpio_chip
  529. * @chip: the chip to register, with chip->base initialized
  530. * Context: potentially before irqs or kmalloc will work
  531. *
  532. * Returns a negative errno if the chip can't be registered, such as
  533. * because the chip->base is invalid or already associated with a
  534. * different chip. Otherwise it returns zero as a success code.
  535. *
  536. * When gpiochip_add() is called very early during boot, so that GPIOs
  537. * can be freely used, the chip->dev device must be registered before
  538. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  539. * for GPIOs will fail rudely.
  540. *
  541. * If chip->base is negative, this requests dynamic assignment of
  542. * a range of valid GPIOs.
  543. */
  544. int gpiochip_add(struct gpio_chip *chip)
  545. {
  546. unsigned long flags;
  547. int status = 0;
  548. unsigned id;
  549. int base = chip->base;
  550. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  551. && base >= 0) {
  552. status = -EINVAL;
  553. goto fail;
  554. }
  555. spin_lock_irqsave(&gpio_lock, flags);
  556. if (base < 0) {
  557. base = gpiochip_find_base(chip->ngpio);
  558. if (base < 0) {
  559. status = base;
  560. goto unlock;
  561. }
  562. chip->base = base;
  563. }
  564. /* these GPIO numbers must not be managed by another gpio_chip */
  565. for (id = base; id < base + chip->ngpio; id++) {
  566. if (gpio_desc[id].chip != NULL) {
  567. status = -EBUSY;
  568. break;
  569. }
  570. }
  571. if (status == 0) {
  572. for (id = base; id < base + chip->ngpio; id++) {
  573. gpio_desc[id].chip = chip;
  574. /* REVISIT: most hardware initializes GPIOs as
  575. * inputs (often with pullups enabled) so power
  576. * usage is minimized. Linux code should set the
  577. * gpio direction first thing; but until it does,
  578. * we may expose the wrong direction in sysfs.
  579. */
  580. gpio_desc[id].flags = !chip->direction_input
  581. ? (1 << FLAG_IS_OUT)
  582. : 0;
  583. }
  584. }
  585. unlock:
  586. spin_unlock_irqrestore(&gpio_lock, flags);
  587. if (status == 0)
  588. status = gpiochip_export(chip);
  589. fail:
  590. /* failures here can mean systems won't boot... */
  591. if (status)
  592. pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
  593. chip->base, chip->base + chip->ngpio - 1,
  594. chip->label ? : "generic");
  595. return status;
  596. }
  597. EXPORT_SYMBOL_GPL(gpiochip_add);
  598. /**
  599. * gpiochip_remove() - unregister a gpio_chip
  600. * @chip: the chip to unregister
  601. *
  602. * A gpio_chip with any GPIOs still requested may not be removed.
  603. */
  604. int gpiochip_remove(struct gpio_chip *chip)
  605. {
  606. unsigned long flags;
  607. int status = 0;
  608. unsigned id;
  609. spin_lock_irqsave(&gpio_lock, flags);
  610. for (id = chip->base; id < chip->base + chip->ngpio; id++) {
  611. if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
  612. status = -EBUSY;
  613. break;
  614. }
  615. }
  616. if (status == 0) {
  617. for (id = chip->base; id < chip->base + chip->ngpio; id++)
  618. gpio_desc[id].chip = NULL;
  619. }
  620. spin_unlock_irqrestore(&gpio_lock, flags);
  621. if (status == 0)
  622. gpiochip_unexport(chip);
  623. return status;
  624. }
  625. EXPORT_SYMBOL_GPL(gpiochip_remove);
  626. /* These "optional" allocation calls help prevent drivers from stomping
  627. * on each other, and help provide better diagnostics in debugfs.
  628. * They're called even less than the "set direction" calls.
  629. */
  630. int gpio_request(unsigned gpio, const char *label)
  631. {
  632. struct gpio_desc *desc;
  633. int status = -EINVAL;
  634. unsigned long flags;
  635. spin_lock_irqsave(&gpio_lock, flags);
  636. if (!gpio_is_valid(gpio))
  637. goto done;
  638. desc = &gpio_desc[gpio];
  639. if (desc->chip == NULL)
  640. goto done;
  641. if (!try_module_get(desc->chip->owner))
  642. goto done;
  643. /* NOTE: gpio_request() can be called in early boot,
  644. * before IRQs are enabled.
  645. */
  646. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  647. desc_set_label(desc, label ? : "?");
  648. status = 0;
  649. } else {
  650. status = -EBUSY;
  651. module_put(desc->chip->owner);
  652. }
  653. done:
  654. if (status)
  655. pr_debug("gpio_request: gpio-%d (%s) status %d\n",
  656. gpio, label ? : "?", status);
  657. spin_unlock_irqrestore(&gpio_lock, flags);
  658. return status;
  659. }
  660. EXPORT_SYMBOL_GPL(gpio_request);
  661. void gpio_free(unsigned gpio)
  662. {
  663. unsigned long flags;
  664. struct gpio_desc *desc;
  665. if (!gpio_is_valid(gpio)) {
  666. WARN_ON(extra_checks);
  667. return;
  668. }
  669. gpio_unexport(gpio);
  670. spin_lock_irqsave(&gpio_lock, flags);
  671. desc = &gpio_desc[gpio];
  672. if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags)) {
  673. desc_set_label(desc, NULL);
  674. module_put(desc->chip->owner);
  675. } else
  676. WARN_ON(extra_checks);
  677. spin_unlock_irqrestore(&gpio_lock, flags);
  678. }
  679. EXPORT_SYMBOL_GPL(gpio_free);
  680. /**
  681. * gpiochip_is_requested - return string iff signal was requested
  682. * @chip: controller managing the signal
  683. * @offset: of signal within controller's 0..(ngpio - 1) range
  684. *
  685. * Returns NULL if the GPIO is not currently requested, else a string.
  686. * If debugfs support is enabled, the string returned is the label passed
  687. * to gpio_request(); otherwise it is a meaningless constant.
  688. *
  689. * This function is for use by GPIO controller drivers. The label can
  690. * help with diagnostics, and knowing that the signal is used as a GPIO
  691. * can help avoid accidentally multiplexing it to another controller.
  692. */
  693. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  694. {
  695. unsigned gpio = chip->base + offset;
  696. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  697. return NULL;
  698. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  699. return NULL;
  700. #ifdef CONFIG_DEBUG_FS
  701. return gpio_desc[gpio].label;
  702. #else
  703. return "?";
  704. #endif
  705. }
  706. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  707. /* Drivers MUST set GPIO direction before making get/set calls. In
  708. * some cases this is done in early boot, before IRQs are enabled.
  709. *
  710. * As a rule these aren't called more than once (except for drivers
  711. * using the open-drain emulation idiom) so these are natural places
  712. * to accumulate extra debugging checks. Note that we can't (yet)
  713. * rely on gpio_request() having been called beforehand.
  714. */
  715. int gpio_direction_input(unsigned gpio)
  716. {
  717. unsigned long flags;
  718. struct gpio_chip *chip;
  719. struct gpio_desc *desc = &gpio_desc[gpio];
  720. int status = -EINVAL;
  721. spin_lock_irqsave(&gpio_lock, flags);
  722. if (!gpio_is_valid(gpio))
  723. goto fail;
  724. chip = desc->chip;
  725. if (!chip || !chip->get || !chip->direction_input)
  726. goto fail;
  727. gpio -= chip->base;
  728. if (gpio >= chip->ngpio)
  729. goto fail;
  730. gpio_ensure_requested(desc);
  731. /* now we know the gpio is valid and chip won't vanish */
  732. spin_unlock_irqrestore(&gpio_lock, flags);
  733. might_sleep_if(extra_checks && chip->can_sleep);
  734. status = chip->direction_input(chip, gpio);
  735. if (status == 0)
  736. clear_bit(FLAG_IS_OUT, &desc->flags);
  737. return status;
  738. fail:
  739. spin_unlock_irqrestore(&gpio_lock, flags);
  740. if (status)
  741. pr_debug("%s: gpio-%d status %d\n",
  742. __func__, gpio, status);
  743. return status;
  744. }
  745. EXPORT_SYMBOL_GPL(gpio_direction_input);
  746. int gpio_direction_output(unsigned gpio, int value)
  747. {
  748. unsigned long flags;
  749. struct gpio_chip *chip;
  750. struct gpio_desc *desc = &gpio_desc[gpio];
  751. int status = -EINVAL;
  752. spin_lock_irqsave(&gpio_lock, flags);
  753. if (!gpio_is_valid(gpio))
  754. goto fail;
  755. chip = desc->chip;
  756. if (!chip || !chip->set || !chip->direction_output)
  757. goto fail;
  758. gpio -= chip->base;
  759. if (gpio >= chip->ngpio)
  760. goto fail;
  761. gpio_ensure_requested(desc);
  762. /* now we know the gpio is valid and chip won't vanish */
  763. spin_unlock_irqrestore(&gpio_lock, flags);
  764. might_sleep_if(extra_checks && chip->can_sleep);
  765. status = chip->direction_output(chip, gpio, value);
  766. if (status == 0)
  767. set_bit(FLAG_IS_OUT, &desc->flags);
  768. return status;
  769. fail:
  770. spin_unlock_irqrestore(&gpio_lock, flags);
  771. if (status)
  772. pr_debug("%s: gpio-%d status %d\n",
  773. __func__, gpio, status);
  774. return status;
  775. }
  776. EXPORT_SYMBOL_GPL(gpio_direction_output);
  777. /* I/O calls are only valid after configuration completed; the relevant
  778. * "is this a valid GPIO" error checks should already have been done.
  779. *
  780. * "Get" operations are often inlinable as reading a pin value register,
  781. * and masking the relevant bit in that register.
  782. *
  783. * When "set" operations are inlinable, they involve writing that mask to
  784. * one register to set a low value, or a different register to set it high.
  785. * Otherwise locking is needed, so there may be little value to inlining.
  786. *
  787. *------------------------------------------------------------------------
  788. *
  789. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  790. * have requested the GPIO. That can include implicit requesting by
  791. * a direction setting call. Marking a gpio as requested locks its chip
  792. * in memory, guaranteeing that these table lookups need no more locking
  793. * and that gpiochip_remove() will fail.
  794. *
  795. * REVISIT when debugging, consider adding some instrumentation to ensure
  796. * that the GPIO was actually requested.
  797. */
  798. /**
  799. * __gpio_get_value() - return a gpio's value
  800. * @gpio: gpio whose value will be returned
  801. * Context: any
  802. *
  803. * This is used directly or indirectly to implement gpio_get_value().
  804. * It returns the zero or nonzero value provided by the associated
  805. * gpio_chip.get() method; or zero if no such method is provided.
  806. */
  807. int __gpio_get_value(unsigned gpio)
  808. {
  809. struct gpio_chip *chip;
  810. chip = gpio_to_chip(gpio);
  811. WARN_ON(extra_checks && chip->can_sleep);
  812. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  813. }
  814. EXPORT_SYMBOL_GPL(__gpio_get_value);
  815. /**
  816. * __gpio_set_value() - assign a gpio's value
  817. * @gpio: gpio whose value will be assigned
  818. * @value: value to assign
  819. * Context: any
  820. *
  821. * This is used directly or indirectly to implement gpio_set_value().
  822. * It invokes the associated gpio_chip.set() method.
  823. */
  824. void __gpio_set_value(unsigned gpio, int value)
  825. {
  826. struct gpio_chip *chip;
  827. chip = gpio_to_chip(gpio);
  828. WARN_ON(extra_checks && chip->can_sleep);
  829. chip->set(chip, gpio - chip->base, value);
  830. }
  831. EXPORT_SYMBOL_GPL(__gpio_set_value);
  832. /**
  833. * __gpio_cansleep() - report whether gpio value access will sleep
  834. * @gpio: gpio in question
  835. * Context: any
  836. *
  837. * This is used directly or indirectly to implement gpio_cansleep(). It
  838. * returns nonzero if access reading or writing the GPIO value can sleep.
  839. */
  840. int __gpio_cansleep(unsigned gpio)
  841. {
  842. struct gpio_chip *chip;
  843. /* only call this on GPIOs that are valid! */
  844. chip = gpio_to_chip(gpio);
  845. return chip->can_sleep;
  846. }
  847. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  848. /* There's no value in making it easy to inline GPIO calls that may sleep.
  849. * Common examples include ones connected to I2C or SPI chips.
  850. */
  851. int gpio_get_value_cansleep(unsigned gpio)
  852. {
  853. struct gpio_chip *chip;
  854. might_sleep_if(extra_checks);
  855. chip = gpio_to_chip(gpio);
  856. return chip->get(chip, gpio - chip->base);
  857. }
  858. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  859. void gpio_set_value_cansleep(unsigned gpio, int value)
  860. {
  861. struct gpio_chip *chip;
  862. might_sleep_if(extra_checks);
  863. chip = gpio_to_chip(gpio);
  864. chip->set(chip, gpio - chip->base, value);
  865. }
  866. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  867. #ifdef CONFIG_DEBUG_FS
  868. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  869. {
  870. unsigned i;
  871. unsigned gpio = chip->base;
  872. struct gpio_desc *gdesc = &gpio_desc[gpio];
  873. int is_out;
  874. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  875. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  876. continue;
  877. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  878. seq_printf(s, " gpio-%-3d (%-12s) %s %s",
  879. gpio, gdesc->label,
  880. is_out ? "out" : "in ",
  881. chip->get
  882. ? (chip->get(chip, i) ? "hi" : "lo")
  883. : "? ");
  884. if (!is_out) {
  885. int irq = gpio_to_irq(gpio);
  886. struct irq_desc *desc = irq_desc + irq;
  887. /* This races with request_irq(), set_irq_type(),
  888. * and set_irq_wake() ... but those are "rare".
  889. *
  890. * More significantly, trigger type flags aren't
  891. * currently maintained by genirq.
  892. */
  893. if (irq >= 0 && desc->action) {
  894. char *trigger;
  895. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  896. case IRQ_TYPE_NONE:
  897. trigger = "(default)";
  898. break;
  899. case IRQ_TYPE_EDGE_FALLING:
  900. trigger = "edge-falling";
  901. break;
  902. case IRQ_TYPE_EDGE_RISING:
  903. trigger = "edge-rising";
  904. break;
  905. case IRQ_TYPE_EDGE_BOTH:
  906. trigger = "edge-both";
  907. break;
  908. case IRQ_TYPE_LEVEL_HIGH:
  909. trigger = "level-high";
  910. break;
  911. case IRQ_TYPE_LEVEL_LOW:
  912. trigger = "level-low";
  913. break;
  914. default:
  915. trigger = "?trigger?";
  916. break;
  917. }
  918. seq_printf(s, " irq-%d %s%s",
  919. irq, trigger,
  920. (desc->status & IRQ_WAKEUP)
  921. ? " wakeup" : "");
  922. }
  923. }
  924. seq_printf(s, "\n");
  925. }
  926. }
  927. static int gpiolib_show(struct seq_file *s, void *unused)
  928. {
  929. struct gpio_chip *chip = NULL;
  930. unsigned gpio;
  931. int started = 0;
  932. /* REVISIT this isn't locked against gpio_chip removal ... */
  933. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  934. struct device *dev;
  935. if (chip == gpio_desc[gpio].chip)
  936. continue;
  937. chip = gpio_desc[gpio].chip;
  938. if (!chip)
  939. continue;
  940. seq_printf(s, "%sGPIOs %d-%d",
  941. started ? "\n" : "",
  942. chip->base, chip->base + chip->ngpio - 1);
  943. dev = chip->dev;
  944. if (dev)
  945. seq_printf(s, ", %s/%s",
  946. dev->bus ? dev->bus->name : "no-bus",
  947. dev->bus_id);
  948. if (chip->label)
  949. seq_printf(s, ", %s", chip->label);
  950. if (chip->can_sleep)
  951. seq_printf(s, ", can sleep");
  952. seq_printf(s, ":\n");
  953. started = 1;
  954. if (chip->dbg_show)
  955. chip->dbg_show(s, chip);
  956. else
  957. gpiolib_dbg_show(s, chip);
  958. }
  959. return 0;
  960. }
  961. static int gpiolib_open(struct inode *inode, struct file *file)
  962. {
  963. return single_open(file, gpiolib_show, NULL);
  964. }
  965. static struct file_operations gpiolib_operations = {
  966. .open = gpiolib_open,
  967. .read = seq_read,
  968. .llseek = seq_lseek,
  969. .release = single_release,
  970. };
  971. static int __init gpiolib_debugfs_init(void)
  972. {
  973. /* /sys/kernel/debug/gpio */
  974. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  975. NULL, NULL, &gpiolib_operations);
  976. return 0;
  977. }
  978. subsys_initcall(gpiolib_debugfs_init);
  979. #endif /* DEBUG_FS */