gpiolib.c 31 KB

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