gpiolib.c 42 KB

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