gpiolib.c 31 KB

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