gpiolib.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. might_sleep();
  666. if (!gpio_is_valid(gpio)) {
  667. WARN_ON(extra_checks);
  668. return;
  669. }
  670. gpio_unexport(gpio);
  671. spin_lock_irqsave(&gpio_lock, flags);
  672. desc = &gpio_desc[gpio];
  673. if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags)) {
  674. desc_set_label(desc, NULL);
  675. module_put(desc->chip->owner);
  676. } else
  677. WARN_ON(extra_checks);
  678. spin_unlock_irqrestore(&gpio_lock, flags);
  679. }
  680. EXPORT_SYMBOL_GPL(gpio_free);
  681. /**
  682. * gpiochip_is_requested - return string iff signal was requested
  683. * @chip: controller managing the signal
  684. * @offset: of signal within controller's 0..(ngpio - 1) range
  685. *
  686. * Returns NULL if the GPIO is not currently requested, else a string.
  687. * If debugfs support is enabled, the string returned is the label passed
  688. * to gpio_request(); otherwise it is a meaningless constant.
  689. *
  690. * This function is for use by GPIO controller drivers. The label can
  691. * help with diagnostics, and knowing that the signal is used as a GPIO
  692. * can help avoid accidentally multiplexing it to another controller.
  693. */
  694. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  695. {
  696. unsigned gpio = chip->base + offset;
  697. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  698. return NULL;
  699. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  700. return NULL;
  701. #ifdef CONFIG_DEBUG_FS
  702. return gpio_desc[gpio].label;
  703. #else
  704. return "?";
  705. #endif
  706. }
  707. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  708. /* Drivers MUST set GPIO direction before making get/set calls. In
  709. * some cases this is done in early boot, before IRQs are enabled.
  710. *
  711. * As a rule these aren't called more than once (except for drivers
  712. * using the open-drain emulation idiom) so these are natural places
  713. * to accumulate extra debugging checks. Note that we can't (yet)
  714. * rely on gpio_request() having been called beforehand.
  715. */
  716. int gpio_direction_input(unsigned gpio)
  717. {
  718. unsigned long flags;
  719. struct gpio_chip *chip;
  720. struct gpio_desc *desc = &gpio_desc[gpio];
  721. int status = -EINVAL;
  722. spin_lock_irqsave(&gpio_lock, flags);
  723. if (!gpio_is_valid(gpio))
  724. goto fail;
  725. chip = desc->chip;
  726. if (!chip || !chip->get || !chip->direction_input)
  727. goto fail;
  728. gpio -= chip->base;
  729. if (gpio >= chip->ngpio)
  730. goto fail;
  731. gpio_ensure_requested(desc);
  732. /* now we know the gpio is valid and chip won't vanish */
  733. spin_unlock_irqrestore(&gpio_lock, flags);
  734. might_sleep_if(extra_checks && chip->can_sleep);
  735. status = chip->direction_input(chip, gpio);
  736. if (status == 0)
  737. clear_bit(FLAG_IS_OUT, &desc->flags);
  738. return status;
  739. fail:
  740. spin_unlock_irqrestore(&gpio_lock, flags);
  741. if (status)
  742. pr_debug("%s: gpio-%d status %d\n",
  743. __func__, gpio, status);
  744. return status;
  745. }
  746. EXPORT_SYMBOL_GPL(gpio_direction_input);
  747. int gpio_direction_output(unsigned gpio, int value)
  748. {
  749. unsigned long flags;
  750. struct gpio_chip *chip;
  751. struct gpio_desc *desc = &gpio_desc[gpio];
  752. int status = -EINVAL;
  753. spin_lock_irqsave(&gpio_lock, flags);
  754. if (!gpio_is_valid(gpio))
  755. goto fail;
  756. chip = desc->chip;
  757. if (!chip || !chip->set || !chip->direction_output)
  758. goto fail;
  759. gpio -= chip->base;
  760. if (gpio >= chip->ngpio)
  761. goto fail;
  762. gpio_ensure_requested(desc);
  763. /* now we know the gpio is valid and chip won't vanish */
  764. spin_unlock_irqrestore(&gpio_lock, flags);
  765. might_sleep_if(extra_checks && chip->can_sleep);
  766. status = chip->direction_output(chip, gpio, value);
  767. if (status == 0)
  768. set_bit(FLAG_IS_OUT, &desc->flags);
  769. return status;
  770. fail:
  771. spin_unlock_irqrestore(&gpio_lock, flags);
  772. if (status)
  773. pr_debug("%s: gpio-%d status %d\n",
  774. __func__, gpio, status);
  775. return status;
  776. }
  777. EXPORT_SYMBOL_GPL(gpio_direction_output);
  778. /* I/O calls are only valid after configuration completed; the relevant
  779. * "is this a valid GPIO" error checks should already have been done.
  780. *
  781. * "Get" operations are often inlinable as reading a pin value register,
  782. * and masking the relevant bit in that register.
  783. *
  784. * When "set" operations are inlinable, they involve writing that mask to
  785. * one register to set a low value, or a different register to set it high.
  786. * Otherwise locking is needed, so there may be little value to inlining.
  787. *
  788. *------------------------------------------------------------------------
  789. *
  790. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  791. * have requested the GPIO. That can include implicit requesting by
  792. * a direction setting call. Marking a gpio as requested locks its chip
  793. * in memory, guaranteeing that these table lookups need no more locking
  794. * and that gpiochip_remove() will fail.
  795. *
  796. * REVISIT when debugging, consider adding some instrumentation to ensure
  797. * that the GPIO was actually requested.
  798. */
  799. /**
  800. * __gpio_get_value() - return a gpio's value
  801. * @gpio: gpio whose value will be returned
  802. * Context: any
  803. *
  804. * This is used directly or indirectly to implement gpio_get_value().
  805. * It returns the zero or nonzero value provided by the associated
  806. * gpio_chip.get() method; or zero if no such method is provided.
  807. */
  808. int __gpio_get_value(unsigned gpio)
  809. {
  810. struct gpio_chip *chip;
  811. chip = gpio_to_chip(gpio);
  812. WARN_ON(extra_checks && chip->can_sleep);
  813. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  814. }
  815. EXPORT_SYMBOL_GPL(__gpio_get_value);
  816. /**
  817. * __gpio_set_value() - assign a gpio's value
  818. * @gpio: gpio whose value will be assigned
  819. * @value: value to assign
  820. * Context: any
  821. *
  822. * This is used directly or indirectly to implement gpio_set_value().
  823. * It invokes the associated gpio_chip.set() method.
  824. */
  825. void __gpio_set_value(unsigned gpio, int value)
  826. {
  827. struct gpio_chip *chip;
  828. chip = gpio_to_chip(gpio);
  829. WARN_ON(extra_checks && chip->can_sleep);
  830. chip->set(chip, gpio - chip->base, value);
  831. }
  832. EXPORT_SYMBOL_GPL(__gpio_set_value);
  833. /**
  834. * __gpio_cansleep() - report whether gpio value access will sleep
  835. * @gpio: gpio in question
  836. * Context: any
  837. *
  838. * This is used directly or indirectly to implement gpio_cansleep(). It
  839. * returns nonzero if access reading or writing the GPIO value can sleep.
  840. */
  841. int __gpio_cansleep(unsigned gpio)
  842. {
  843. struct gpio_chip *chip;
  844. /* only call this on GPIOs that are valid! */
  845. chip = gpio_to_chip(gpio);
  846. return chip->can_sleep;
  847. }
  848. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  849. /* There's no value in making it easy to inline GPIO calls that may sleep.
  850. * Common examples include ones connected to I2C or SPI chips.
  851. */
  852. int gpio_get_value_cansleep(unsigned gpio)
  853. {
  854. struct gpio_chip *chip;
  855. might_sleep_if(extra_checks);
  856. chip = gpio_to_chip(gpio);
  857. return chip->get(chip, gpio - chip->base);
  858. }
  859. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  860. void gpio_set_value_cansleep(unsigned gpio, int value)
  861. {
  862. struct gpio_chip *chip;
  863. might_sleep_if(extra_checks);
  864. chip = gpio_to_chip(gpio);
  865. chip->set(chip, gpio - chip->base, value);
  866. }
  867. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  868. #ifdef CONFIG_DEBUG_FS
  869. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  870. {
  871. unsigned i;
  872. unsigned gpio = chip->base;
  873. struct gpio_desc *gdesc = &gpio_desc[gpio];
  874. int is_out;
  875. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  876. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  877. continue;
  878. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  879. seq_printf(s, " gpio-%-3d (%-12s) %s %s",
  880. gpio, gdesc->label,
  881. is_out ? "out" : "in ",
  882. chip->get
  883. ? (chip->get(chip, i) ? "hi" : "lo")
  884. : "? ");
  885. if (!is_out) {
  886. int irq = gpio_to_irq(gpio);
  887. struct irq_desc *desc = irq_desc + irq;
  888. /* This races with request_irq(), set_irq_type(),
  889. * and set_irq_wake() ... but those are "rare".
  890. *
  891. * More significantly, trigger type flags aren't
  892. * currently maintained by genirq.
  893. */
  894. if (irq >= 0 && desc->action) {
  895. char *trigger;
  896. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  897. case IRQ_TYPE_NONE:
  898. trigger = "(default)";
  899. break;
  900. case IRQ_TYPE_EDGE_FALLING:
  901. trigger = "edge-falling";
  902. break;
  903. case IRQ_TYPE_EDGE_RISING:
  904. trigger = "edge-rising";
  905. break;
  906. case IRQ_TYPE_EDGE_BOTH:
  907. trigger = "edge-both";
  908. break;
  909. case IRQ_TYPE_LEVEL_HIGH:
  910. trigger = "level-high";
  911. break;
  912. case IRQ_TYPE_LEVEL_LOW:
  913. trigger = "level-low";
  914. break;
  915. default:
  916. trigger = "?trigger?";
  917. break;
  918. }
  919. seq_printf(s, " irq-%d %s%s",
  920. irq, trigger,
  921. (desc->status & IRQ_WAKEUP)
  922. ? " wakeup" : "");
  923. }
  924. }
  925. seq_printf(s, "\n");
  926. }
  927. }
  928. static int gpiolib_show(struct seq_file *s, void *unused)
  929. {
  930. struct gpio_chip *chip = NULL;
  931. unsigned gpio;
  932. int started = 0;
  933. /* REVISIT this isn't locked against gpio_chip removal ... */
  934. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  935. struct device *dev;
  936. if (chip == gpio_desc[gpio].chip)
  937. continue;
  938. chip = gpio_desc[gpio].chip;
  939. if (!chip)
  940. continue;
  941. seq_printf(s, "%sGPIOs %d-%d",
  942. started ? "\n" : "",
  943. chip->base, chip->base + chip->ngpio - 1);
  944. dev = chip->dev;
  945. if (dev)
  946. seq_printf(s, ", %s/%s",
  947. dev->bus ? dev->bus->name : "no-bus",
  948. dev->bus_id);
  949. if (chip->label)
  950. seq_printf(s, ", %s", chip->label);
  951. if (chip->can_sleep)
  952. seq_printf(s, ", can sleep");
  953. seq_printf(s, ":\n");
  954. started = 1;
  955. if (chip->dbg_show)
  956. chip->dbg_show(s, chip);
  957. else
  958. gpiolib_dbg_show(s, chip);
  959. }
  960. return 0;
  961. }
  962. static int gpiolib_open(struct inode *inode, struct file *file)
  963. {
  964. return single_open(file, gpiolib_show, NULL);
  965. }
  966. static struct file_operations gpiolib_operations = {
  967. .open = gpiolib_open,
  968. .read = seq_read,
  969. .llseek = seq_lseek,
  970. .release = single_release,
  971. };
  972. static int __init gpiolib_debugfs_init(void)
  973. {
  974. /* /sys/kernel/debug/gpio */
  975. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  976. NULL, NULL, &gpiolib_operations);
  977. return 0;
  978. }
  979. subsys_initcall(gpiolib_debugfs_init);
  980. #endif /* DEBUG_FS */