gpiolib.c 15 KB

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