gpiolib.c 15 KB

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