gpiolib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/irq.h>
  4. #include <linux/spinlock.h>
  5. #include <asm/gpio.h>
  6. /* Optional implementation infrastructure for GPIO interfaces.
  7. *
  8. * Platforms may want to use this if they tend to use very many GPIOs
  9. * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
  10. *
  11. * When kernel footprint or instruction count is an issue, simpler
  12. * implementations may be preferred. The GPIO programming interface
  13. * allows for inlining speed-critical get/set operations for common
  14. * cases, so that access to SOC-integrated GPIOs can sometimes cost
  15. * only an instruction or two per bit.
  16. */
  17. /* When debugging, extend minimal trust to callers and platform code.
  18. * Also emit diagnostic messages that may help initial bringup, when
  19. * board setup or driver bugs are most common.
  20. *
  21. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  22. */
  23. #ifdef DEBUG
  24. #define extra_checks 1
  25. #else
  26. #define extra_checks 0
  27. #endif
  28. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  29. * While any GPIO is requested, its gpio_chip is not removable;
  30. * each GPIO's "requested" flag serves as a lock and refcount.
  31. */
  32. static DEFINE_SPINLOCK(gpio_lock);
  33. struct gpio_desc {
  34. struct gpio_chip *chip;
  35. unsigned long flags;
  36. /* flag symbols are bit numbers */
  37. #define FLAG_REQUESTED 0
  38. #define FLAG_IS_OUT 1
  39. #define FLAG_RESERVED 2
  40. #ifdef CONFIG_DEBUG_FS
  41. const char *label;
  42. #endif
  43. };
  44. static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
  45. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  46. {
  47. #ifdef CONFIG_DEBUG_FS
  48. d->label = label;
  49. #endif
  50. }
  51. /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
  52. * when setting direction, and otherwise illegal. Until board setup code
  53. * and drivers use explicit requests everywhere (which won't happen when
  54. * those calls have no teeth) we can't avoid autorequesting. This nag
  55. * message should motivate switching to explicit requests...
  56. */
  57. static void gpio_ensure_requested(struct gpio_desc *desc)
  58. {
  59. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  60. pr_warning("GPIO-%d autorequested\n", (int)(desc - gpio_desc));
  61. desc_set_label(desc, "[auto]");
  62. if (!try_module_get(desc->chip->owner))
  63. pr_err("GPIO-%d: module can't be gotten \n",
  64. (int)(desc - gpio_desc));
  65. }
  66. }
  67. /* caller holds gpio_lock *OR* gpio is marked as requested */
  68. static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
  69. {
  70. return gpio_desc[gpio].chip;
  71. }
  72. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  73. static int gpiochip_find_base(int ngpio)
  74. {
  75. int i;
  76. int spare = 0;
  77. int base = -ENOSPC;
  78. for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
  79. struct gpio_desc *desc = &gpio_desc[i];
  80. struct gpio_chip *chip = desc->chip;
  81. if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
  82. spare++;
  83. if (spare == ngpio) {
  84. base = i;
  85. break;
  86. }
  87. } else {
  88. spare = 0;
  89. if (chip)
  90. i -= chip->ngpio - 1;
  91. }
  92. }
  93. if (gpio_is_valid(base))
  94. pr_debug("%s: found new base at %d\n", __func__, base);
  95. return base;
  96. }
  97. /**
  98. * gpiochip_reserve() - reserve range of gpios to use with platform code only
  99. * @start: starting gpio number
  100. * @ngpio: number of gpios to reserve
  101. * Context: platform init, potentially before irqs or kmalloc will work
  102. *
  103. * Returns a negative errno if any gpio within the range is already reserved
  104. * or registered, else returns zero as a success code. Use this function
  105. * to mark a range of gpios as unavailable for dynamic gpio number allocation,
  106. * for example because its driver support is not yet loaded.
  107. */
  108. int __init gpiochip_reserve(int start, int ngpio)
  109. {
  110. int ret = 0;
  111. unsigned long flags;
  112. int i;
  113. if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio))
  114. return -EINVAL;
  115. spin_lock_irqsave(&gpio_lock, flags);
  116. for (i = start; i < start + ngpio; i++) {
  117. struct gpio_desc *desc = &gpio_desc[i];
  118. if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
  119. ret = -EBUSY;
  120. goto err;
  121. }
  122. set_bit(FLAG_RESERVED, &desc->flags);
  123. }
  124. pr_debug("%s: reserved gpios from %d to %d\n",
  125. __func__, start, start + ngpio - 1);
  126. err:
  127. spin_unlock_irqrestore(&gpio_lock, flags);
  128. return ret;
  129. }
  130. /**
  131. * gpiochip_add() - register a gpio_chip
  132. * @chip: the chip to register, with chip->base initialized
  133. * Context: potentially before irqs or kmalloc will work
  134. *
  135. * Returns a negative errno if the chip can't be registered, such as
  136. * because the chip->base is invalid or already associated with a
  137. * different chip. Otherwise it returns zero as a success code.
  138. *
  139. * If chip->base is negative, this requests dynamic assignment of
  140. * a range of valid GPIOs.
  141. */
  142. int gpiochip_add(struct gpio_chip *chip)
  143. {
  144. unsigned long flags;
  145. int status = 0;
  146. unsigned id;
  147. int base = chip->base;
  148. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio))
  149. && base >= 0) {
  150. status = -EINVAL;
  151. goto fail;
  152. }
  153. spin_lock_irqsave(&gpio_lock, flags);
  154. if (base < 0) {
  155. base = gpiochip_find_base(chip->ngpio);
  156. if (base < 0) {
  157. status = base;
  158. goto fail_unlock;
  159. }
  160. chip->base = base;
  161. }
  162. /* these GPIO numbers must not be managed by another gpio_chip */
  163. for (id = base; id < base + chip->ngpio; id++) {
  164. if (gpio_desc[id].chip != NULL) {
  165. status = -EBUSY;
  166. break;
  167. }
  168. }
  169. if (status == 0) {
  170. for (id = base; id < base + chip->ngpio; id++) {
  171. gpio_desc[id].chip = chip;
  172. gpio_desc[id].flags = 0;
  173. }
  174. }
  175. fail_unlock:
  176. spin_unlock_irqrestore(&gpio_lock, flags);
  177. fail:
  178. /* failures here can mean systems won't boot... */
  179. if (status)
  180. pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
  181. chip->base, chip->base + chip->ngpio,
  182. chip->label ? : "generic");
  183. return status;
  184. }
  185. EXPORT_SYMBOL_GPL(gpiochip_add);
  186. /**
  187. * gpiochip_remove() - unregister a gpio_chip
  188. * @chip: the chip to unregister
  189. *
  190. * A gpio_chip with any GPIOs still requested may not be removed.
  191. */
  192. int gpiochip_remove(struct gpio_chip *chip)
  193. {
  194. unsigned long flags;
  195. int status = 0;
  196. unsigned id;
  197. spin_lock_irqsave(&gpio_lock, flags);
  198. for (id = chip->base; id < chip->base + chip->ngpio; id++) {
  199. if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
  200. status = -EBUSY;
  201. break;
  202. }
  203. }
  204. if (status == 0) {
  205. for (id = chip->base; id < chip->base + chip->ngpio; id++)
  206. gpio_desc[id].chip = NULL;
  207. }
  208. spin_unlock_irqrestore(&gpio_lock, flags);
  209. return status;
  210. }
  211. EXPORT_SYMBOL_GPL(gpiochip_remove);
  212. /* These "optional" allocation calls help prevent drivers from stomping
  213. * on each other, and help provide better diagnostics in debugfs.
  214. * They're called even less than the "set direction" calls.
  215. */
  216. int gpio_request(unsigned gpio, const char *label)
  217. {
  218. struct gpio_desc *desc;
  219. int status = -EINVAL;
  220. unsigned long flags;
  221. spin_lock_irqsave(&gpio_lock, flags);
  222. if (!gpio_is_valid(gpio))
  223. goto done;
  224. desc = &gpio_desc[gpio];
  225. if (desc->chip == NULL)
  226. goto done;
  227. if (!try_module_get(desc->chip->owner))
  228. goto done;
  229. /* NOTE: gpio_request() can be called in early boot,
  230. * before IRQs are enabled.
  231. */
  232. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  233. desc_set_label(desc, label ? : "?");
  234. status = 0;
  235. } else {
  236. status = -EBUSY;
  237. module_put(desc->chip->owner);
  238. }
  239. done:
  240. if (status)
  241. pr_debug("gpio_request: gpio-%d (%s) status %d\n",
  242. gpio, label ? : "?", status);
  243. spin_unlock_irqrestore(&gpio_lock, flags);
  244. return status;
  245. }
  246. EXPORT_SYMBOL_GPL(gpio_request);
  247. void gpio_free(unsigned gpio)
  248. {
  249. unsigned long flags;
  250. struct gpio_desc *desc;
  251. if (!gpio_is_valid(gpio)) {
  252. WARN_ON(extra_checks);
  253. return;
  254. }
  255. spin_lock_irqsave(&gpio_lock, flags);
  256. desc = &gpio_desc[gpio];
  257. if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags)) {
  258. desc_set_label(desc, NULL);
  259. module_put(desc->chip->owner);
  260. } else
  261. WARN_ON(extra_checks);
  262. spin_unlock_irqrestore(&gpio_lock, flags);
  263. }
  264. EXPORT_SYMBOL_GPL(gpio_free);
  265. /**
  266. * gpiochip_is_requested - return string iff signal was requested
  267. * @chip: controller managing the signal
  268. * @offset: of signal within controller's 0..(ngpio - 1) range
  269. *
  270. * Returns NULL if the GPIO is not currently requested, else a string.
  271. * If debugfs support is enabled, the string returned is the label passed
  272. * to gpio_request(); otherwise it is a meaningless constant.
  273. *
  274. * This function is for use by GPIO controller drivers. The label can
  275. * help with diagnostics, and knowing that the signal is used as a GPIO
  276. * can help avoid accidentally multiplexing it to another controller.
  277. */
  278. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  279. {
  280. unsigned gpio = chip->base + offset;
  281. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  282. return NULL;
  283. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  284. return NULL;
  285. #ifdef CONFIG_DEBUG_FS
  286. return gpio_desc[gpio].label;
  287. #else
  288. return "?";
  289. #endif
  290. }
  291. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  292. /* Drivers MUST set GPIO direction before making get/set calls. In
  293. * some cases this is done in early boot, before IRQs are enabled.
  294. *
  295. * As a rule these aren't called more than once (except for drivers
  296. * using the open-drain emulation idiom) so these are natural places
  297. * to accumulate extra debugging checks. Note that we can't (yet)
  298. * rely on gpio_request() having been called beforehand.
  299. */
  300. int gpio_direction_input(unsigned gpio)
  301. {
  302. unsigned long flags;
  303. struct gpio_chip *chip;
  304. struct gpio_desc *desc = &gpio_desc[gpio];
  305. int status = -EINVAL;
  306. spin_lock_irqsave(&gpio_lock, flags);
  307. if (!gpio_is_valid(gpio))
  308. goto fail;
  309. chip = desc->chip;
  310. if (!chip || !chip->get || !chip->direction_input)
  311. goto fail;
  312. gpio -= chip->base;
  313. if (gpio >= chip->ngpio)
  314. goto fail;
  315. gpio_ensure_requested(desc);
  316. /* now we know the gpio is valid and chip won't vanish */
  317. spin_unlock_irqrestore(&gpio_lock, flags);
  318. might_sleep_if(extra_checks && chip->can_sleep);
  319. status = chip->direction_input(chip, gpio);
  320. if (status == 0)
  321. clear_bit(FLAG_IS_OUT, &desc->flags);
  322. return status;
  323. fail:
  324. spin_unlock_irqrestore(&gpio_lock, flags);
  325. if (status)
  326. pr_debug("%s: gpio-%d status %d\n",
  327. __FUNCTION__, gpio, status);
  328. return status;
  329. }
  330. EXPORT_SYMBOL_GPL(gpio_direction_input);
  331. int gpio_direction_output(unsigned gpio, int value)
  332. {
  333. unsigned long flags;
  334. struct gpio_chip *chip;
  335. struct gpio_desc *desc = &gpio_desc[gpio];
  336. int status = -EINVAL;
  337. spin_lock_irqsave(&gpio_lock, flags);
  338. if (!gpio_is_valid(gpio))
  339. goto fail;
  340. chip = desc->chip;
  341. if (!chip || !chip->set || !chip->direction_output)
  342. goto fail;
  343. gpio -= chip->base;
  344. if (gpio >= chip->ngpio)
  345. goto fail;
  346. gpio_ensure_requested(desc);
  347. /* now we know the gpio is valid and chip won't vanish */
  348. spin_unlock_irqrestore(&gpio_lock, flags);
  349. might_sleep_if(extra_checks && chip->can_sleep);
  350. status = chip->direction_output(chip, gpio, value);
  351. if (status == 0)
  352. set_bit(FLAG_IS_OUT, &desc->flags);
  353. return status;
  354. fail:
  355. spin_unlock_irqrestore(&gpio_lock, flags);
  356. if (status)
  357. pr_debug("%s: gpio-%d status %d\n",
  358. __FUNCTION__, gpio, status);
  359. return status;
  360. }
  361. EXPORT_SYMBOL_GPL(gpio_direction_output);
  362. /* I/O calls are only valid after configuration completed; the relevant
  363. * "is this a valid GPIO" error checks should already have been done.
  364. *
  365. * "Get" operations are often inlinable as reading a pin value register,
  366. * and masking the relevant bit in that register.
  367. *
  368. * When "set" operations are inlinable, they involve writing that mask to
  369. * one register to set a low value, or a different register to set it high.
  370. * Otherwise locking is needed, so there may be little value to inlining.
  371. *
  372. *------------------------------------------------------------------------
  373. *
  374. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  375. * have requested the GPIO. That can include implicit requesting by
  376. * a direction setting call. Marking a gpio as requested locks its chip
  377. * in memory, guaranteeing that these table lookups need no more locking
  378. * and that gpiochip_remove() will fail.
  379. *
  380. * REVISIT when debugging, consider adding some instrumentation to ensure
  381. * that the GPIO was actually requested.
  382. */
  383. /**
  384. * __gpio_get_value() - return a gpio's value
  385. * @gpio: gpio whose value will be returned
  386. * Context: any
  387. *
  388. * This is used directly or indirectly to implement gpio_get_value().
  389. * It returns the zero or nonzero value provided by the associated
  390. * gpio_chip.get() method; or zero if no such method is provided.
  391. */
  392. int __gpio_get_value(unsigned gpio)
  393. {
  394. struct gpio_chip *chip;
  395. chip = gpio_to_chip(gpio);
  396. WARN_ON(extra_checks && chip->can_sleep);
  397. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  398. }
  399. EXPORT_SYMBOL_GPL(__gpio_get_value);
  400. /**
  401. * __gpio_set_value() - assign a gpio's value
  402. * @gpio: gpio whose value will be assigned
  403. * @value: value to assign
  404. * Context: any
  405. *
  406. * This is used directly or indirectly to implement gpio_set_value().
  407. * It invokes the associated gpio_chip.set() method.
  408. */
  409. void __gpio_set_value(unsigned gpio, int value)
  410. {
  411. struct gpio_chip *chip;
  412. chip = gpio_to_chip(gpio);
  413. WARN_ON(extra_checks && chip->can_sleep);
  414. chip->set(chip, gpio - chip->base, value);
  415. }
  416. EXPORT_SYMBOL_GPL(__gpio_set_value);
  417. /**
  418. * __gpio_cansleep() - report whether gpio value access will sleep
  419. * @gpio: gpio in question
  420. * Context: any
  421. *
  422. * This is used directly or indirectly to implement gpio_cansleep(). It
  423. * returns nonzero if access reading or writing the GPIO value can sleep.
  424. */
  425. int __gpio_cansleep(unsigned gpio)
  426. {
  427. struct gpio_chip *chip;
  428. /* only call this on GPIOs that are valid! */
  429. chip = gpio_to_chip(gpio);
  430. return chip->can_sleep;
  431. }
  432. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  433. /* There's no value in making it easy to inline GPIO calls that may sleep.
  434. * Common examples include ones connected to I2C or SPI chips.
  435. */
  436. int gpio_get_value_cansleep(unsigned gpio)
  437. {
  438. struct gpio_chip *chip;
  439. might_sleep_if(extra_checks);
  440. chip = gpio_to_chip(gpio);
  441. return chip->get(chip, gpio - chip->base);
  442. }
  443. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  444. void gpio_set_value_cansleep(unsigned gpio, int value)
  445. {
  446. struct gpio_chip *chip;
  447. might_sleep_if(extra_checks);
  448. chip = gpio_to_chip(gpio);
  449. chip->set(chip, gpio - chip->base, value);
  450. }
  451. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  452. #ifdef CONFIG_DEBUG_FS
  453. #include <linux/debugfs.h>
  454. #include <linux/seq_file.h>
  455. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  456. {
  457. unsigned i;
  458. unsigned gpio = chip->base;
  459. struct gpio_desc *gdesc = &gpio_desc[gpio];
  460. int is_out;
  461. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  462. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  463. continue;
  464. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  465. seq_printf(s, " gpio-%-3d (%-12s) %s %s",
  466. gpio, gdesc->label,
  467. is_out ? "out" : "in ",
  468. chip->get
  469. ? (chip->get(chip, i) ? "hi" : "lo")
  470. : "? ");
  471. if (!is_out) {
  472. int irq = gpio_to_irq(gpio);
  473. struct irq_desc *desc = irq_desc + irq;
  474. /* This races with request_irq(), set_irq_type(),
  475. * and set_irq_wake() ... but those are "rare".
  476. *
  477. * More significantly, trigger type flags aren't
  478. * currently maintained by genirq.
  479. */
  480. if (irq >= 0 && desc->action) {
  481. char *trigger;
  482. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  483. case IRQ_TYPE_NONE:
  484. trigger = "(default)";
  485. break;
  486. case IRQ_TYPE_EDGE_FALLING:
  487. trigger = "edge-falling";
  488. break;
  489. case IRQ_TYPE_EDGE_RISING:
  490. trigger = "edge-rising";
  491. break;
  492. case IRQ_TYPE_EDGE_BOTH:
  493. trigger = "edge-both";
  494. break;
  495. case IRQ_TYPE_LEVEL_HIGH:
  496. trigger = "level-high";
  497. break;
  498. case IRQ_TYPE_LEVEL_LOW:
  499. trigger = "level-low";
  500. break;
  501. default:
  502. trigger = "?trigger?";
  503. break;
  504. }
  505. seq_printf(s, " irq-%d %s%s",
  506. irq, trigger,
  507. (desc->status & IRQ_WAKEUP)
  508. ? " wakeup" : "");
  509. }
  510. }
  511. seq_printf(s, "\n");
  512. }
  513. }
  514. static int gpiolib_show(struct seq_file *s, void *unused)
  515. {
  516. struct gpio_chip *chip = NULL;
  517. unsigned gpio;
  518. int started = 0;
  519. /* REVISIT this isn't locked against gpio_chip removal ... */
  520. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  521. if (chip == gpio_desc[gpio].chip)
  522. continue;
  523. chip = gpio_desc[gpio].chip;
  524. if (!chip)
  525. continue;
  526. seq_printf(s, "%sGPIOs %d-%d, %s%s:\n",
  527. started ? "\n" : "",
  528. chip->base, chip->base + chip->ngpio - 1,
  529. chip->label ? : "generic",
  530. chip->can_sleep ? ", can sleep" : "");
  531. started = 1;
  532. if (chip->dbg_show)
  533. chip->dbg_show(s, chip);
  534. else
  535. gpiolib_dbg_show(s, chip);
  536. }
  537. return 0;
  538. }
  539. static int gpiolib_open(struct inode *inode, struct file *file)
  540. {
  541. return single_open(file, gpiolib_show, NULL);
  542. }
  543. static struct file_operations gpiolib_operations = {
  544. .open = gpiolib_open,
  545. .read = seq_read,
  546. .llseek = seq_lseek,
  547. .release = single_release,
  548. };
  549. static int __init gpiolib_debugfs_init(void)
  550. {
  551. /* /sys/kernel/debug/gpio */
  552. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  553. NULL, NULL, &gpiolib_operations);
  554. return 0;
  555. }
  556. subsys_initcall(gpiolib_debugfs_init);
  557. #endif /* DEBUG_FS */