gpiolib.c 41 KB

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