gpiolib.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/irq.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/gpio.h>
  11. #include <linux/idr.h>
  12. /* Optional implementation infrastructure for GPIO interfaces.
  13. *
  14. * Platforms may want to use this if they tend to use very many GPIOs
  15. * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
  16. *
  17. * When kernel footprint or instruction count is an issue, simpler
  18. * implementations may be preferred. The GPIO programming interface
  19. * allows for inlining speed-critical get/set operations for common
  20. * cases, so that access to SOC-integrated GPIOs can sometimes cost
  21. * only an instruction or two per bit.
  22. */
  23. /* When debugging, extend minimal trust to callers and platform code.
  24. * Also emit diagnostic messages that may help initial bringup, when
  25. * board setup or driver bugs are most common.
  26. *
  27. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  28. */
  29. #ifdef DEBUG
  30. #define extra_checks 1
  31. #else
  32. #define extra_checks 0
  33. #endif
  34. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  35. * While any GPIO is requested, its gpio_chip is not removable;
  36. * each GPIO's "requested" flag serves as a lock and refcount.
  37. */
  38. static DEFINE_SPINLOCK(gpio_lock);
  39. struct gpio_desc {
  40. struct gpio_chip *chip;
  41. unsigned long flags;
  42. /* flag symbols are bit numbers */
  43. #define FLAG_REQUESTED 0
  44. #define FLAG_IS_OUT 1
  45. #define FLAG_RESERVED 2
  46. #define FLAG_EXPORT 3 /* protected by sysfs_lock */
  47. #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
  48. #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
  49. #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
  50. #define PDESC_ID_SHIFT 16 /* add new flags before this one */
  51. #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
  52. #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
  53. #ifdef CONFIG_DEBUG_FS
  54. const char *label;
  55. #endif
  56. };
  57. static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
  58. #ifdef CONFIG_GPIO_SYSFS
  59. struct poll_desc {
  60. struct work_struct work;
  61. struct sysfs_dirent *value_sd;
  62. };
  63. static struct idr pdesc_idr;
  64. #endif
  65. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  66. {
  67. #ifdef CONFIG_DEBUG_FS
  68. d->label = label;
  69. #endif
  70. }
  71. /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
  72. * when setting direction, and otherwise illegal. Until board setup code
  73. * and drivers use explicit requests everywhere (which won't happen when
  74. * those calls have no teeth) we can't avoid autorequesting. This nag
  75. * message should motivate switching to explicit requests... so should
  76. * the weaker cleanup after faults, compared to gpio_request().
  77. *
  78. * NOTE: the autorequest mechanism is going away; at this point it's
  79. * only "legal" in the sense that (old) code using it won't break yet,
  80. * but instead only triggers a WARN() stack dump.
  81. */
  82. static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
  83. {
  84. const struct gpio_chip *chip = desc->chip;
  85. const int gpio = chip->base + offset;
  86. if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
  87. "autorequest GPIO-%d\n", gpio)) {
  88. if (!try_module_get(chip->owner)) {
  89. pr_err("GPIO-%d: module can't be gotten \n", gpio);
  90. clear_bit(FLAG_REQUESTED, &desc->flags);
  91. /* lose */
  92. return -EIO;
  93. }
  94. desc_set_label(desc, "[auto]");
  95. /* caller must chip->request() w/o spinlock */
  96. if (chip->request)
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. /* caller holds gpio_lock *OR* gpio is marked as requested */
  102. static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
  103. {
  104. return gpio_desc[gpio].chip;
  105. }
  106. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  107. static int gpiochip_find_base(int ngpio)
  108. {
  109. int i;
  110. int spare = 0;
  111. int base = -ENOSPC;
  112. for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
  113. struct gpio_desc *desc = &gpio_desc[i];
  114. struct gpio_chip *chip = desc->chip;
  115. if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
  116. spare++;
  117. if (spare == ngpio) {
  118. base = i;
  119. break;
  120. }
  121. } else {
  122. spare = 0;
  123. if (chip)
  124. i -= chip->ngpio - 1;
  125. }
  126. }
  127. if (gpio_is_valid(base))
  128. pr_debug("%s: found new base at %d\n", __func__, base);
  129. return base;
  130. }
  131. /**
  132. * gpiochip_reserve() - reserve range of gpios to use with platform code only
  133. * @start: starting gpio number
  134. * @ngpio: number of gpios to reserve
  135. * Context: platform init, potentially before irqs or kmalloc will work
  136. *
  137. * Returns a negative errno if any gpio within the range is already reserved
  138. * or registered, else returns zero as a success code. Use this function
  139. * to mark a range of gpios as unavailable for dynamic gpio number allocation,
  140. * for example because its driver support is not yet loaded.
  141. */
  142. int __init gpiochip_reserve(int start, int ngpio)
  143. {
  144. int ret = 0;
  145. unsigned long flags;
  146. int i;
  147. if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
  148. return -EINVAL;
  149. spin_lock_irqsave(&gpio_lock, flags);
  150. for (i = start; i < start + ngpio; i++) {
  151. struct gpio_desc *desc = &gpio_desc[i];
  152. if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
  153. ret = -EBUSY;
  154. goto err;
  155. }
  156. set_bit(FLAG_RESERVED, &desc->flags);
  157. }
  158. pr_debug("%s: reserved gpios from %d to %d\n",
  159. __func__, start, start + ngpio - 1);
  160. err:
  161. spin_unlock_irqrestore(&gpio_lock, flags);
  162. return ret;
  163. }
  164. #ifdef CONFIG_GPIO_SYSFS
  165. /* lock protects against unexport_gpio() being called while
  166. * sysfs files are active.
  167. */
  168. static DEFINE_MUTEX(sysfs_lock);
  169. /*
  170. * /sys/class/gpio/gpioN... only for GPIOs that are exported
  171. * /direction
  172. * * MAY BE OMITTED if kernel won't allow direction changes
  173. * * is read/write as "in" or "out"
  174. * * may also be written as "high" or "low", initializing
  175. * output value as specified ("out" implies "low")
  176. * /value
  177. * * always readable, subject to hardware behavior
  178. * * may be writable, as zero/nonzero
  179. * /edge
  180. * * configures behavior of poll(2) on /value
  181. * * available only if pin can generate IRQs on input
  182. * * is read/write as "none", "falling", "rising", or "both"
  183. */
  184. static ssize_t gpio_direction_show(struct device *dev,
  185. struct device_attribute *attr, char *buf)
  186. {
  187. const struct gpio_desc *desc = dev_get_drvdata(dev);
  188. ssize_t status;
  189. mutex_lock(&sysfs_lock);
  190. if (!test_bit(FLAG_EXPORT, &desc->flags))
  191. status = -EIO;
  192. else
  193. status = sprintf(buf, "%s\n",
  194. test_bit(FLAG_IS_OUT, &desc->flags)
  195. ? "out" : "in");
  196. mutex_unlock(&sysfs_lock);
  197. return status;
  198. }
  199. static ssize_t gpio_direction_store(struct device *dev,
  200. struct device_attribute *attr, const char *buf, size_t size)
  201. {
  202. const struct gpio_desc *desc = dev_get_drvdata(dev);
  203. unsigned gpio = desc - gpio_desc;
  204. ssize_t status;
  205. mutex_lock(&sysfs_lock);
  206. if (!test_bit(FLAG_EXPORT, &desc->flags))
  207. status = -EIO;
  208. else if (sysfs_streq(buf, "high"))
  209. status = gpio_direction_output(gpio, 1);
  210. else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
  211. status = gpio_direction_output(gpio, 0);
  212. else if (sysfs_streq(buf, "in"))
  213. status = gpio_direction_input(gpio);
  214. else
  215. status = -EINVAL;
  216. mutex_unlock(&sysfs_lock);
  217. return status ? : size;
  218. }
  219. static const DEVICE_ATTR(direction, 0644,
  220. gpio_direction_show, gpio_direction_store);
  221. static ssize_t gpio_value_show(struct device *dev,
  222. struct device_attribute *attr, char *buf)
  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
  231. status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio));
  232. mutex_unlock(&sysfs_lock);
  233. return status;
  234. }
  235. static ssize_t gpio_value_store(struct device *dev,
  236. struct device_attribute *attr, const char *buf, size_t size)
  237. {
  238. const struct gpio_desc *desc = dev_get_drvdata(dev);
  239. unsigned gpio = desc - gpio_desc;
  240. ssize_t status;
  241. mutex_lock(&sysfs_lock);
  242. if (!test_bit(FLAG_EXPORT, &desc->flags))
  243. status = -EIO;
  244. else if (!test_bit(FLAG_IS_OUT, &desc->flags))
  245. status = -EPERM;
  246. else {
  247. long value;
  248. status = strict_strtol(buf, 0, &value);
  249. if (status == 0) {
  250. gpio_set_value_cansleep(gpio, value != 0);
  251. status = size;
  252. }
  253. }
  254. mutex_unlock(&sysfs_lock);
  255. return status;
  256. }
  257. static /*const*/ DEVICE_ATTR(value, 0644,
  258. gpio_value_show, gpio_value_store);
  259. static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
  260. {
  261. struct work_struct *work = priv;
  262. schedule_work(work);
  263. return IRQ_HANDLED;
  264. }
  265. static void gpio_notify_sysfs(struct work_struct *work)
  266. {
  267. struct poll_desc *pdesc;
  268. pdesc = container_of(work, struct poll_desc, work);
  269. sysfs_notify_dirent(pdesc->value_sd);
  270. }
  271. static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
  272. unsigned long gpio_flags)
  273. {
  274. struct poll_desc *pdesc;
  275. unsigned long irq_flags;
  276. int ret, irq, id;
  277. if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
  278. return 0;
  279. irq = gpio_to_irq(desc - gpio_desc);
  280. if (irq < 0)
  281. return -EIO;
  282. id = desc->flags >> PDESC_ID_SHIFT;
  283. pdesc = idr_find(&pdesc_idr, id);
  284. if (pdesc) {
  285. free_irq(irq, &pdesc->work);
  286. cancel_work_sync(&pdesc->work);
  287. }
  288. desc->flags &= ~GPIO_TRIGGER_MASK;
  289. if (!gpio_flags) {
  290. ret = 0;
  291. goto free_sd;
  292. }
  293. irq_flags = IRQF_SHARED;
  294. if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
  295. irq_flags |= IRQF_TRIGGER_FALLING;
  296. if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
  297. irq_flags |= IRQF_TRIGGER_RISING;
  298. if (!pdesc) {
  299. pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
  300. if (!pdesc) {
  301. ret = -ENOMEM;
  302. goto err_out;
  303. }
  304. do {
  305. ret = -ENOMEM;
  306. if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
  307. ret = idr_get_new_above(&pdesc_idr,
  308. pdesc, 1, &id);
  309. } while (ret == -EAGAIN);
  310. if (ret)
  311. goto free_mem;
  312. desc->flags &= GPIO_FLAGS_MASK;
  313. desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
  314. if (desc->flags >> PDESC_ID_SHIFT != id) {
  315. ret = -ERANGE;
  316. goto free_id;
  317. }
  318. pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
  319. if (!pdesc->value_sd) {
  320. ret = -ENODEV;
  321. goto free_id;
  322. }
  323. INIT_WORK(&pdesc->work, gpio_notify_sysfs);
  324. }
  325. ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
  326. "gpiolib", &pdesc->work);
  327. if (ret)
  328. goto free_sd;
  329. desc->flags |= gpio_flags;
  330. return 0;
  331. free_sd:
  332. sysfs_put(pdesc->value_sd);
  333. free_id:
  334. idr_remove(&pdesc_idr, id);
  335. desc->flags &= GPIO_FLAGS_MASK;
  336. free_mem:
  337. kfree(pdesc);
  338. err_out:
  339. return ret;
  340. }
  341. static const struct {
  342. const char *name;
  343. unsigned long flags;
  344. } trigger_types[] = {
  345. { "none", 0 },
  346. { "falling", BIT(FLAG_TRIG_FALL) },
  347. { "rising", BIT(FLAG_TRIG_RISE) },
  348. { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
  349. };
  350. static ssize_t gpio_edge_show(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. const struct gpio_desc *desc = dev_get_drvdata(dev);
  354. ssize_t status;
  355. mutex_lock(&sysfs_lock);
  356. if (!test_bit(FLAG_EXPORT, &desc->flags))
  357. status = -EIO;
  358. else {
  359. int i;
  360. status = 0;
  361. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  362. if ((desc->flags & GPIO_TRIGGER_MASK)
  363. == trigger_types[i].flags) {
  364. status = sprintf(buf, "%s\n",
  365. trigger_types[i].name);
  366. break;
  367. }
  368. }
  369. mutex_unlock(&sysfs_lock);
  370. return status;
  371. }
  372. static ssize_t gpio_edge_store(struct device *dev,
  373. struct device_attribute *attr, const char *buf, size_t size)
  374. {
  375. struct gpio_desc *desc = dev_get_drvdata(dev);
  376. ssize_t status;
  377. int i;
  378. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  379. if (sysfs_streq(trigger_types[i].name, buf))
  380. goto found;
  381. return -EINVAL;
  382. found:
  383. mutex_lock(&sysfs_lock);
  384. if (!test_bit(FLAG_EXPORT, &desc->flags))
  385. status = -EIO;
  386. else {
  387. status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
  388. if (!status)
  389. status = size;
  390. }
  391. mutex_unlock(&sysfs_lock);
  392. return status;
  393. }
  394. static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
  395. static const struct attribute *gpio_attrs[] = {
  396. &dev_attr_direction.attr,
  397. &dev_attr_value.attr,
  398. NULL,
  399. };
  400. static const struct attribute_group gpio_attr_group = {
  401. .attrs = (struct attribute **) gpio_attrs,
  402. };
  403. /*
  404. * /sys/class/gpio/gpiochipN/
  405. * /base ... matching gpio_chip.base (N)
  406. * /label ... matching gpio_chip.label
  407. * /ngpio ... matching gpio_chip.ngpio
  408. */
  409. static ssize_t chip_base_show(struct device *dev,
  410. struct device_attribute *attr, char *buf)
  411. {
  412. const struct gpio_chip *chip = dev_get_drvdata(dev);
  413. return sprintf(buf, "%d\n", chip->base);
  414. }
  415. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  416. static ssize_t chip_label_show(struct device *dev,
  417. struct device_attribute *attr, char *buf)
  418. {
  419. const struct gpio_chip *chip = dev_get_drvdata(dev);
  420. return sprintf(buf, "%s\n", chip->label ? : "");
  421. }
  422. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  423. static ssize_t chip_ngpio_show(struct device *dev,
  424. struct device_attribute *attr, char *buf)
  425. {
  426. const struct gpio_chip *chip = dev_get_drvdata(dev);
  427. return sprintf(buf, "%u\n", chip->ngpio);
  428. }
  429. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  430. static const struct attribute *gpiochip_attrs[] = {
  431. &dev_attr_base.attr,
  432. &dev_attr_label.attr,
  433. &dev_attr_ngpio.attr,
  434. NULL,
  435. };
  436. static const struct attribute_group gpiochip_attr_group = {
  437. .attrs = (struct attribute **) gpiochip_attrs,
  438. };
  439. /*
  440. * /sys/class/gpio/export ... write-only
  441. * integer N ... number of GPIO to export (full access)
  442. * /sys/class/gpio/unexport ... write-only
  443. * integer N ... number of GPIO to unexport
  444. */
  445. static ssize_t export_store(struct class *class, const char *buf, size_t len)
  446. {
  447. long gpio;
  448. int status;
  449. status = strict_strtol(buf, 0, &gpio);
  450. if (status < 0)
  451. goto done;
  452. /* No extra locking here; FLAG_SYSFS just signifies that the
  453. * request and export were done by on behalf of userspace, so
  454. * they may be undone on its behalf too.
  455. */
  456. status = gpio_request(gpio, "sysfs");
  457. if (status < 0)
  458. goto done;
  459. status = gpio_export(gpio, true);
  460. if (status < 0)
  461. gpio_free(gpio);
  462. else
  463. set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
  464. done:
  465. if (status)
  466. pr_debug("%s: status %d\n", __func__, status);
  467. return status ? : len;
  468. }
  469. static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
  470. {
  471. long gpio;
  472. int status;
  473. status = strict_strtol(buf, 0, &gpio);
  474. if (status < 0)
  475. goto done;
  476. status = -EINVAL;
  477. /* reject bogus commands (gpio_unexport ignores them) */
  478. if (!gpio_is_valid(gpio))
  479. goto done;
  480. /* No extra locking here; FLAG_SYSFS just signifies that the
  481. * request and export were done by on behalf of userspace, so
  482. * they may be undone on its behalf too.
  483. */
  484. if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
  485. status = 0;
  486. gpio_free(gpio);
  487. }
  488. done:
  489. if (status)
  490. pr_debug("%s: status %d\n", __func__, status);
  491. return status ? : len;
  492. }
  493. static struct class_attribute gpio_class_attrs[] = {
  494. __ATTR(export, 0200, NULL, export_store),
  495. __ATTR(unexport, 0200, NULL, unexport_store),
  496. __ATTR_NULL,
  497. };
  498. static struct class gpio_class = {
  499. .name = "gpio",
  500. .owner = THIS_MODULE,
  501. .class_attrs = gpio_class_attrs,
  502. };
  503. /**
  504. * gpio_export - export a GPIO through sysfs
  505. * @gpio: gpio to make available, already requested
  506. * @direction_may_change: true if userspace may change gpio direction
  507. * Context: arch_initcall or later
  508. *
  509. * When drivers want to make a GPIO accessible to userspace after they
  510. * have requested it -- perhaps while debugging, or as part of their
  511. * public interface -- they may use this routine. If the GPIO can
  512. * change direction (some can't) and the caller allows it, userspace
  513. * will see "direction" sysfs attribute which may be used to change
  514. * the gpio's direction. A "value" attribute will always be provided.
  515. *
  516. * Returns zero on success, else an error.
  517. */
  518. int gpio_export(unsigned gpio, bool direction_may_change)
  519. {
  520. unsigned long flags;
  521. struct gpio_desc *desc;
  522. int status = -EINVAL;
  523. char *ioname = NULL;
  524. /* can't export until sysfs is available ... */
  525. if (!gpio_class.p) {
  526. pr_debug("%s: called too early!\n", __func__);
  527. return -ENOENT;
  528. }
  529. if (!gpio_is_valid(gpio))
  530. goto done;
  531. mutex_lock(&sysfs_lock);
  532. spin_lock_irqsave(&gpio_lock, flags);
  533. desc = &gpio_desc[gpio];
  534. if (test_bit(FLAG_REQUESTED, &desc->flags)
  535. && !test_bit(FLAG_EXPORT, &desc->flags)) {
  536. status = 0;
  537. if (!desc->chip->direction_input
  538. || !desc->chip->direction_output)
  539. direction_may_change = false;
  540. }
  541. spin_unlock_irqrestore(&gpio_lock, flags);
  542. if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
  543. ioname = desc->chip->names[gpio - desc->chip->base];
  544. if (status == 0) {
  545. struct device *dev;
  546. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  547. desc, ioname ? ioname : "gpio%d", gpio);
  548. if (!IS_ERR(dev)) {
  549. if (direction_may_change)
  550. status = sysfs_create_group(&dev->kobj,
  551. &gpio_attr_group);
  552. else
  553. status = device_create_file(dev,
  554. &dev_attr_value);
  555. if (!status && gpio_to_irq(gpio) >= 0
  556. && (direction_may_change
  557. || !test_bit(FLAG_IS_OUT,
  558. &desc->flags)))
  559. status = device_create_file(dev,
  560. &dev_attr_edge);
  561. if (status != 0)
  562. device_unregister(dev);
  563. } else
  564. status = PTR_ERR(dev);
  565. if (status == 0)
  566. set_bit(FLAG_EXPORT, &desc->flags);
  567. }
  568. mutex_unlock(&sysfs_lock);
  569. done:
  570. if (status)
  571. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  572. return status;
  573. }
  574. EXPORT_SYMBOL_GPL(gpio_export);
  575. static int match_export(struct device *dev, void *data)
  576. {
  577. return dev_get_drvdata(dev) == data;
  578. }
  579. /**
  580. * gpio_export_link - create a sysfs link to an exported GPIO node
  581. * @dev: device under which to create symlink
  582. * @name: name of the symlink
  583. * @gpio: gpio to create symlink to, already exported
  584. *
  585. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  586. * node. Caller is responsible for unlinking.
  587. *
  588. * Returns zero on success, else an error.
  589. */
  590. int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
  591. {
  592. struct gpio_desc *desc;
  593. int status = -EINVAL;
  594. if (!gpio_is_valid(gpio))
  595. goto done;
  596. mutex_lock(&sysfs_lock);
  597. desc = &gpio_desc[gpio];
  598. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  599. struct device *tdev;
  600. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  601. if (tdev != NULL) {
  602. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  603. name);
  604. } else {
  605. status = -ENODEV;
  606. }
  607. }
  608. mutex_unlock(&sysfs_lock);
  609. done:
  610. if (status)
  611. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  612. return status;
  613. }
  614. EXPORT_SYMBOL_GPL(gpio_export_link);
  615. /**
  616. * gpio_unexport - reverse effect of gpio_export()
  617. * @gpio: gpio to make unavailable
  618. *
  619. * This is implicit on gpio_free().
  620. */
  621. void gpio_unexport(unsigned gpio)
  622. {
  623. struct gpio_desc *desc;
  624. int status = -EINVAL;
  625. if (!gpio_is_valid(gpio))
  626. goto done;
  627. mutex_lock(&sysfs_lock);
  628. desc = &gpio_desc[gpio];
  629. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  630. struct device *dev = NULL;
  631. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  632. if (dev) {
  633. gpio_setup_irq(desc, dev, 0);
  634. clear_bit(FLAG_EXPORT, &desc->flags);
  635. put_device(dev);
  636. device_unregister(dev);
  637. status = 0;
  638. } else
  639. status = -ENODEV;
  640. }
  641. mutex_unlock(&sysfs_lock);
  642. done:
  643. if (status)
  644. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  645. }
  646. EXPORT_SYMBOL_GPL(gpio_unexport);
  647. static int gpiochip_export(struct gpio_chip *chip)
  648. {
  649. int status;
  650. struct device *dev;
  651. /* Many systems register gpio chips for SOC support very early,
  652. * before driver model support is available. In those cases we
  653. * export this later, in gpiolib_sysfs_init() ... here we just
  654. * verify that _some_ field of gpio_class got initialized.
  655. */
  656. if (!gpio_class.p)
  657. return 0;
  658. /* use chip->base for the ID; it's already known to be unique */
  659. mutex_lock(&sysfs_lock);
  660. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  661. "gpiochip%d", chip->base);
  662. if (!IS_ERR(dev)) {
  663. status = sysfs_create_group(&dev->kobj,
  664. &gpiochip_attr_group);
  665. } else
  666. status = PTR_ERR(dev);
  667. chip->exported = (status == 0);
  668. mutex_unlock(&sysfs_lock);
  669. if (status) {
  670. unsigned long flags;
  671. unsigned gpio;
  672. spin_lock_irqsave(&gpio_lock, flags);
  673. gpio = chip->base;
  674. while (gpio_desc[gpio].chip == chip)
  675. gpio_desc[gpio++].chip = NULL;
  676. spin_unlock_irqrestore(&gpio_lock, flags);
  677. pr_debug("%s: chip %s status %d\n", __func__,
  678. chip->label, status);
  679. }
  680. return status;
  681. }
  682. static void gpiochip_unexport(struct gpio_chip *chip)
  683. {
  684. int status;
  685. struct device *dev;
  686. mutex_lock(&sysfs_lock);
  687. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  688. if (dev) {
  689. put_device(dev);
  690. device_unregister(dev);
  691. chip->exported = 0;
  692. status = 0;
  693. } else
  694. status = -ENODEV;
  695. mutex_unlock(&sysfs_lock);
  696. if (status)
  697. pr_debug("%s: chip %s status %d\n", __func__,
  698. chip->label, status);
  699. }
  700. static int __init gpiolib_sysfs_init(void)
  701. {
  702. int status;
  703. unsigned long flags;
  704. unsigned gpio;
  705. idr_init(&pdesc_idr);
  706. status = class_register(&gpio_class);
  707. if (status < 0)
  708. return status;
  709. /* Scan and register the gpio_chips which registered very
  710. * early (e.g. before the class_register above was called).
  711. *
  712. * We run before arch_initcall() so chip->dev nodes can have
  713. * registered, and so arch_initcall() can always gpio_export().
  714. */
  715. spin_lock_irqsave(&gpio_lock, flags);
  716. for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
  717. struct gpio_chip *chip;
  718. chip = gpio_desc[gpio].chip;
  719. if (!chip || chip->exported)
  720. continue;
  721. spin_unlock_irqrestore(&gpio_lock, flags);
  722. status = gpiochip_export(chip);
  723. spin_lock_irqsave(&gpio_lock, flags);
  724. }
  725. spin_unlock_irqrestore(&gpio_lock, flags);
  726. return status;
  727. }
  728. postcore_initcall(gpiolib_sysfs_init);
  729. #else
  730. static inline int gpiochip_export(struct gpio_chip *chip)
  731. {
  732. return 0;
  733. }
  734. static inline void gpiochip_unexport(struct gpio_chip *chip)
  735. {
  736. }
  737. #endif /* CONFIG_GPIO_SYSFS */
  738. /**
  739. * gpiochip_add() - register a gpio_chip
  740. * @chip: the chip to register, with chip->base initialized
  741. * Context: potentially before irqs or kmalloc will work
  742. *
  743. * Returns a negative errno if the chip can't be registered, such as
  744. * because the chip->base is invalid or already associated with a
  745. * different chip. Otherwise it returns zero as a success code.
  746. *
  747. * When gpiochip_add() is called very early during boot, so that GPIOs
  748. * can be freely used, the chip->dev device must be registered before
  749. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  750. * for GPIOs will fail rudely.
  751. *
  752. * If chip->base is negative, this requests dynamic assignment of
  753. * a range of valid GPIOs.
  754. */
  755. int gpiochip_add(struct gpio_chip *chip)
  756. {
  757. unsigned long flags;
  758. int status = 0;
  759. unsigned id;
  760. int base = chip->base;
  761. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  762. && base >= 0) {
  763. status = -EINVAL;
  764. goto fail;
  765. }
  766. spin_lock_irqsave(&gpio_lock, flags);
  767. if (base < 0) {
  768. base = gpiochip_find_base(chip->ngpio);
  769. if (base < 0) {
  770. status = base;
  771. goto unlock;
  772. }
  773. chip->base = base;
  774. }
  775. /* these GPIO numbers must not be managed by another gpio_chip */
  776. for (id = base; id < base + chip->ngpio; id++) {
  777. if (gpio_desc[id].chip != NULL) {
  778. status = -EBUSY;
  779. break;
  780. }
  781. }
  782. if (status == 0) {
  783. for (id = base; id < base + chip->ngpio; id++) {
  784. gpio_desc[id].chip = chip;
  785. /* REVISIT: most hardware initializes GPIOs as
  786. * inputs (often with pullups enabled) so power
  787. * usage is minimized. Linux code should set the
  788. * gpio direction first thing; but until it does,
  789. * we may expose the wrong direction in sysfs.
  790. */
  791. gpio_desc[id].flags = !chip->direction_input
  792. ? (1 << FLAG_IS_OUT)
  793. : 0;
  794. }
  795. }
  796. unlock:
  797. spin_unlock_irqrestore(&gpio_lock, flags);
  798. if (status == 0)
  799. status = gpiochip_export(chip);
  800. fail:
  801. /* failures here can mean systems won't boot... */
  802. if (status)
  803. pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
  804. chip->base, chip->base + chip->ngpio - 1,
  805. chip->label ? : "generic");
  806. return status;
  807. }
  808. EXPORT_SYMBOL_GPL(gpiochip_add);
  809. /**
  810. * gpiochip_remove() - unregister a gpio_chip
  811. * @chip: the chip to unregister
  812. *
  813. * A gpio_chip with any GPIOs still requested may not be removed.
  814. */
  815. int gpiochip_remove(struct gpio_chip *chip)
  816. {
  817. unsigned long flags;
  818. int status = 0;
  819. unsigned id;
  820. spin_lock_irqsave(&gpio_lock, flags);
  821. for (id = chip->base; id < chip->base + chip->ngpio; id++) {
  822. if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
  823. status = -EBUSY;
  824. break;
  825. }
  826. }
  827. if (status == 0) {
  828. for (id = chip->base; id < chip->base + chip->ngpio; id++)
  829. gpio_desc[id].chip = NULL;
  830. }
  831. spin_unlock_irqrestore(&gpio_lock, flags);
  832. if (status == 0)
  833. gpiochip_unexport(chip);
  834. return status;
  835. }
  836. EXPORT_SYMBOL_GPL(gpiochip_remove);
  837. /* These "optional" allocation calls help prevent drivers from stomping
  838. * on each other, and help provide better diagnostics in debugfs.
  839. * They're called even less than the "set direction" calls.
  840. */
  841. int gpio_request(unsigned gpio, const char *label)
  842. {
  843. struct gpio_desc *desc;
  844. struct gpio_chip *chip;
  845. int status = -EINVAL;
  846. unsigned long flags;
  847. spin_lock_irqsave(&gpio_lock, flags);
  848. if (!gpio_is_valid(gpio))
  849. goto done;
  850. desc = &gpio_desc[gpio];
  851. chip = desc->chip;
  852. if (chip == NULL)
  853. goto done;
  854. if (!try_module_get(chip->owner))
  855. goto done;
  856. /* NOTE: gpio_request() can be called in early boot,
  857. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  858. */
  859. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  860. desc_set_label(desc, label ? : "?");
  861. status = 0;
  862. } else {
  863. status = -EBUSY;
  864. module_put(chip->owner);
  865. goto done;
  866. }
  867. if (chip->request) {
  868. /* chip->request may sleep */
  869. spin_unlock_irqrestore(&gpio_lock, flags);
  870. status = chip->request(chip, gpio - chip->base);
  871. spin_lock_irqsave(&gpio_lock, flags);
  872. if (status < 0) {
  873. desc_set_label(desc, NULL);
  874. module_put(chip->owner);
  875. clear_bit(FLAG_REQUESTED, &desc->flags);
  876. }
  877. }
  878. done:
  879. if (status)
  880. pr_debug("gpio_request: gpio-%d (%s) status %d\n",
  881. gpio, label ? : "?", status);
  882. spin_unlock_irqrestore(&gpio_lock, flags);
  883. return status;
  884. }
  885. EXPORT_SYMBOL_GPL(gpio_request);
  886. void gpio_free(unsigned gpio)
  887. {
  888. unsigned long flags;
  889. struct gpio_desc *desc;
  890. struct gpio_chip *chip;
  891. might_sleep();
  892. if (!gpio_is_valid(gpio)) {
  893. WARN_ON(extra_checks);
  894. return;
  895. }
  896. gpio_unexport(gpio);
  897. spin_lock_irqsave(&gpio_lock, flags);
  898. desc = &gpio_desc[gpio];
  899. chip = desc->chip;
  900. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  901. if (chip->free) {
  902. spin_unlock_irqrestore(&gpio_lock, flags);
  903. might_sleep_if(extra_checks && chip->can_sleep);
  904. chip->free(chip, gpio - chip->base);
  905. spin_lock_irqsave(&gpio_lock, flags);
  906. }
  907. desc_set_label(desc, NULL);
  908. module_put(desc->chip->owner);
  909. clear_bit(FLAG_REQUESTED, &desc->flags);
  910. } else
  911. WARN_ON(extra_checks);
  912. spin_unlock_irqrestore(&gpio_lock, flags);
  913. }
  914. EXPORT_SYMBOL_GPL(gpio_free);
  915. /**
  916. * gpiochip_is_requested - return string iff signal was requested
  917. * @chip: controller managing the signal
  918. * @offset: of signal within controller's 0..(ngpio - 1) range
  919. *
  920. * Returns NULL if the GPIO is not currently requested, else a string.
  921. * If debugfs support is enabled, the string returned is the label passed
  922. * to gpio_request(); otherwise it is a meaningless constant.
  923. *
  924. * This function is for use by GPIO controller drivers. The label can
  925. * help with diagnostics, and knowing that the signal is used as a GPIO
  926. * can help avoid accidentally multiplexing it to another controller.
  927. */
  928. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  929. {
  930. unsigned gpio = chip->base + offset;
  931. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  932. return NULL;
  933. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  934. return NULL;
  935. #ifdef CONFIG_DEBUG_FS
  936. return gpio_desc[gpio].label;
  937. #else
  938. return "?";
  939. #endif
  940. }
  941. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  942. /* Drivers MUST set GPIO direction before making get/set calls. In
  943. * some cases this is done in early boot, before IRQs are enabled.
  944. *
  945. * As a rule these aren't called more than once (except for drivers
  946. * using the open-drain emulation idiom) so these are natural places
  947. * to accumulate extra debugging checks. Note that we can't (yet)
  948. * rely on gpio_request() having been called beforehand.
  949. */
  950. int gpio_direction_input(unsigned gpio)
  951. {
  952. unsigned long flags;
  953. struct gpio_chip *chip;
  954. struct gpio_desc *desc = &gpio_desc[gpio];
  955. int status = -EINVAL;
  956. spin_lock_irqsave(&gpio_lock, flags);
  957. if (!gpio_is_valid(gpio))
  958. goto fail;
  959. chip = desc->chip;
  960. if (!chip || !chip->get || !chip->direction_input)
  961. goto fail;
  962. gpio -= chip->base;
  963. if (gpio >= chip->ngpio)
  964. goto fail;
  965. status = gpio_ensure_requested(desc, gpio);
  966. if (status < 0)
  967. goto fail;
  968. /* now we know the gpio is valid and chip won't vanish */
  969. spin_unlock_irqrestore(&gpio_lock, flags);
  970. might_sleep_if(extra_checks && chip->can_sleep);
  971. if (status) {
  972. status = chip->request(chip, gpio);
  973. if (status < 0) {
  974. pr_debug("GPIO-%d: chip request fail, %d\n",
  975. chip->base + gpio, status);
  976. /* and it's not available to anyone else ...
  977. * gpio_request() is the fully clean solution.
  978. */
  979. goto lose;
  980. }
  981. }
  982. status = chip->direction_input(chip, gpio);
  983. if (status == 0)
  984. clear_bit(FLAG_IS_OUT, &desc->flags);
  985. lose:
  986. return status;
  987. fail:
  988. spin_unlock_irqrestore(&gpio_lock, flags);
  989. if (status)
  990. pr_debug("%s: gpio-%d status %d\n",
  991. __func__, gpio, status);
  992. return status;
  993. }
  994. EXPORT_SYMBOL_GPL(gpio_direction_input);
  995. int gpio_direction_output(unsigned gpio, int value)
  996. {
  997. unsigned long flags;
  998. struct gpio_chip *chip;
  999. struct gpio_desc *desc = &gpio_desc[gpio];
  1000. int status = -EINVAL;
  1001. spin_lock_irqsave(&gpio_lock, flags);
  1002. if (!gpio_is_valid(gpio))
  1003. goto fail;
  1004. chip = desc->chip;
  1005. if (!chip || !chip->set || !chip->direction_output)
  1006. goto fail;
  1007. gpio -= chip->base;
  1008. if (gpio >= chip->ngpio)
  1009. goto fail;
  1010. status = gpio_ensure_requested(desc, gpio);
  1011. if (status < 0)
  1012. goto fail;
  1013. /* now we know the gpio is valid and chip won't vanish */
  1014. spin_unlock_irqrestore(&gpio_lock, flags);
  1015. might_sleep_if(extra_checks && chip->can_sleep);
  1016. if (status) {
  1017. status = chip->request(chip, gpio);
  1018. if (status < 0) {
  1019. pr_debug("GPIO-%d: chip request fail, %d\n",
  1020. chip->base + gpio, status);
  1021. /* and it's not available to anyone else ...
  1022. * gpio_request() is the fully clean solution.
  1023. */
  1024. goto lose;
  1025. }
  1026. }
  1027. status = chip->direction_output(chip, gpio, value);
  1028. if (status == 0)
  1029. set_bit(FLAG_IS_OUT, &desc->flags);
  1030. lose:
  1031. return status;
  1032. fail:
  1033. spin_unlock_irqrestore(&gpio_lock, flags);
  1034. if (status)
  1035. pr_debug("%s: gpio-%d status %d\n",
  1036. __func__, gpio, status);
  1037. return status;
  1038. }
  1039. EXPORT_SYMBOL_GPL(gpio_direction_output);
  1040. /* I/O calls are only valid after configuration completed; the relevant
  1041. * "is this a valid GPIO" error checks should already have been done.
  1042. *
  1043. * "Get" operations are often inlinable as reading a pin value register,
  1044. * and masking the relevant bit in that register.
  1045. *
  1046. * When "set" operations are inlinable, they involve writing that mask to
  1047. * one register to set a low value, or a different register to set it high.
  1048. * Otherwise locking is needed, so there may be little value to inlining.
  1049. *
  1050. *------------------------------------------------------------------------
  1051. *
  1052. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1053. * have requested the GPIO. That can include implicit requesting by
  1054. * a direction setting call. Marking a gpio as requested locks its chip
  1055. * in memory, guaranteeing that these table lookups need no more locking
  1056. * and that gpiochip_remove() will fail.
  1057. *
  1058. * REVISIT when debugging, consider adding some instrumentation to ensure
  1059. * that the GPIO was actually requested.
  1060. */
  1061. /**
  1062. * __gpio_get_value() - return a gpio's value
  1063. * @gpio: gpio whose value will be returned
  1064. * Context: any
  1065. *
  1066. * This is used directly or indirectly to implement gpio_get_value().
  1067. * It returns the zero or nonzero value provided by the associated
  1068. * gpio_chip.get() method; or zero if no such method is provided.
  1069. */
  1070. int __gpio_get_value(unsigned gpio)
  1071. {
  1072. struct gpio_chip *chip;
  1073. chip = gpio_to_chip(gpio);
  1074. WARN_ON(extra_checks && chip->can_sleep);
  1075. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1076. }
  1077. EXPORT_SYMBOL_GPL(__gpio_get_value);
  1078. /**
  1079. * __gpio_set_value() - assign a gpio's value
  1080. * @gpio: gpio whose value will be assigned
  1081. * @value: value to assign
  1082. * Context: any
  1083. *
  1084. * This is used directly or indirectly to implement gpio_set_value().
  1085. * It invokes the associated gpio_chip.set() method.
  1086. */
  1087. void __gpio_set_value(unsigned gpio, int value)
  1088. {
  1089. struct gpio_chip *chip;
  1090. chip = gpio_to_chip(gpio);
  1091. WARN_ON(extra_checks && chip->can_sleep);
  1092. chip->set(chip, gpio - chip->base, value);
  1093. }
  1094. EXPORT_SYMBOL_GPL(__gpio_set_value);
  1095. /**
  1096. * __gpio_cansleep() - report whether gpio value access will sleep
  1097. * @gpio: gpio in question
  1098. * Context: any
  1099. *
  1100. * This is used directly or indirectly to implement gpio_cansleep(). It
  1101. * returns nonzero if access reading or writing the GPIO value can sleep.
  1102. */
  1103. int __gpio_cansleep(unsigned gpio)
  1104. {
  1105. struct gpio_chip *chip;
  1106. /* only call this on GPIOs that are valid! */
  1107. chip = gpio_to_chip(gpio);
  1108. return chip->can_sleep;
  1109. }
  1110. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  1111. /**
  1112. * __gpio_to_irq() - return the IRQ corresponding to a GPIO
  1113. * @gpio: gpio whose IRQ will be returned (already requested)
  1114. * Context: any
  1115. *
  1116. * This is used directly or indirectly to implement gpio_to_irq().
  1117. * It returns the number of the IRQ signaled by this (input) GPIO,
  1118. * or a negative errno.
  1119. */
  1120. int __gpio_to_irq(unsigned gpio)
  1121. {
  1122. struct gpio_chip *chip;
  1123. chip = gpio_to_chip(gpio);
  1124. return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
  1125. }
  1126. EXPORT_SYMBOL_GPL(__gpio_to_irq);
  1127. /* There's no value in making it easy to inline GPIO calls that may sleep.
  1128. * Common examples include ones connected to I2C or SPI chips.
  1129. */
  1130. int gpio_get_value_cansleep(unsigned gpio)
  1131. {
  1132. struct gpio_chip *chip;
  1133. might_sleep_if(extra_checks);
  1134. chip = gpio_to_chip(gpio);
  1135. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1136. }
  1137. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  1138. void gpio_set_value_cansleep(unsigned gpio, int value)
  1139. {
  1140. struct gpio_chip *chip;
  1141. might_sleep_if(extra_checks);
  1142. chip = gpio_to_chip(gpio);
  1143. chip->set(chip, gpio - chip->base, value);
  1144. }
  1145. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  1146. #ifdef CONFIG_DEBUG_FS
  1147. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1148. {
  1149. unsigned i;
  1150. unsigned gpio = chip->base;
  1151. struct gpio_desc *gdesc = &gpio_desc[gpio];
  1152. int is_out;
  1153. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1154. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1155. continue;
  1156. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1157. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
  1158. gpio, gdesc->label,
  1159. is_out ? "out" : "in ",
  1160. chip->get
  1161. ? (chip->get(chip, i) ? "hi" : "lo")
  1162. : "? ");
  1163. if (!is_out) {
  1164. int irq = gpio_to_irq(gpio);
  1165. struct irq_desc *desc = irq_to_desc(irq);
  1166. /* This races with request_irq(), set_irq_type(),
  1167. * and set_irq_wake() ... but those are "rare".
  1168. *
  1169. * More significantly, trigger type flags aren't
  1170. * currently maintained by genirq.
  1171. */
  1172. if (irq >= 0 && desc->action) {
  1173. char *trigger;
  1174. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  1175. case IRQ_TYPE_NONE:
  1176. trigger = "(default)";
  1177. break;
  1178. case IRQ_TYPE_EDGE_FALLING:
  1179. trigger = "edge-falling";
  1180. break;
  1181. case IRQ_TYPE_EDGE_RISING:
  1182. trigger = "edge-rising";
  1183. break;
  1184. case IRQ_TYPE_EDGE_BOTH:
  1185. trigger = "edge-both";
  1186. break;
  1187. case IRQ_TYPE_LEVEL_HIGH:
  1188. trigger = "level-high";
  1189. break;
  1190. case IRQ_TYPE_LEVEL_LOW:
  1191. trigger = "level-low";
  1192. break;
  1193. default:
  1194. trigger = "?trigger?";
  1195. break;
  1196. }
  1197. seq_printf(s, " irq-%d %s%s",
  1198. irq, trigger,
  1199. (desc->status & IRQ_WAKEUP)
  1200. ? " wakeup" : "");
  1201. }
  1202. }
  1203. seq_printf(s, "\n");
  1204. }
  1205. }
  1206. static int gpiolib_show(struct seq_file *s, void *unused)
  1207. {
  1208. struct gpio_chip *chip = NULL;
  1209. unsigned gpio;
  1210. int started = 0;
  1211. /* REVISIT this isn't locked against gpio_chip removal ... */
  1212. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  1213. struct device *dev;
  1214. if (chip == gpio_desc[gpio].chip)
  1215. continue;
  1216. chip = gpio_desc[gpio].chip;
  1217. if (!chip)
  1218. continue;
  1219. seq_printf(s, "%sGPIOs %d-%d",
  1220. started ? "\n" : "",
  1221. chip->base, chip->base + chip->ngpio - 1);
  1222. dev = chip->dev;
  1223. if (dev)
  1224. seq_printf(s, ", %s/%s",
  1225. dev->bus ? dev->bus->name : "no-bus",
  1226. dev_name(dev));
  1227. if (chip->label)
  1228. seq_printf(s, ", %s", chip->label);
  1229. if (chip->can_sleep)
  1230. seq_printf(s, ", can sleep");
  1231. seq_printf(s, ":\n");
  1232. started = 1;
  1233. if (chip->dbg_show)
  1234. chip->dbg_show(s, chip);
  1235. else
  1236. gpiolib_dbg_show(s, chip);
  1237. }
  1238. return 0;
  1239. }
  1240. static int gpiolib_open(struct inode *inode, struct file *file)
  1241. {
  1242. return single_open(file, gpiolib_show, NULL);
  1243. }
  1244. static const struct file_operations gpiolib_operations = {
  1245. .open = gpiolib_open,
  1246. .read = seq_read,
  1247. .llseek = seq_lseek,
  1248. .release = single_release,
  1249. };
  1250. static int __init gpiolib_debugfs_init(void)
  1251. {
  1252. /* /sys/kernel/debug/gpio */
  1253. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1254. NULL, NULL, &gpiolib_operations);
  1255. return 0;
  1256. }
  1257. subsys_initcall(gpiolib_debugfs_init);
  1258. #endif /* DEBUG_FS */