gpiolib.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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. 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. struct device *dev = NULL;
  718. if (!gpio_is_valid(gpio)) {
  719. status = -EINVAL;
  720. goto done;
  721. }
  722. mutex_lock(&sysfs_lock);
  723. desc = &gpio_desc[gpio];
  724. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  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. } else
  730. status = -ENODEV;
  731. }
  732. mutex_unlock(&sysfs_lock);
  733. if (dev) {
  734. device_unregister(dev);
  735. put_device(dev);
  736. }
  737. done:
  738. if (status)
  739. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  740. }
  741. EXPORT_SYMBOL_GPL(gpio_unexport);
  742. static int gpiochip_export(struct gpio_chip *chip)
  743. {
  744. int status;
  745. struct device *dev;
  746. /* Many systems register gpio chips for SOC support very early,
  747. * before driver model support is available. In those cases we
  748. * export this later, in gpiolib_sysfs_init() ... here we just
  749. * verify that _some_ field of gpio_class got initialized.
  750. */
  751. if (!gpio_class.p)
  752. return 0;
  753. /* use chip->base for the ID; it's already known to be unique */
  754. mutex_lock(&sysfs_lock);
  755. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  756. "gpiochip%d", chip->base);
  757. if (!IS_ERR(dev)) {
  758. status = sysfs_create_group(&dev->kobj,
  759. &gpiochip_attr_group);
  760. } else
  761. status = PTR_ERR(dev);
  762. chip->exported = (status == 0);
  763. mutex_unlock(&sysfs_lock);
  764. if (status) {
  765. unsigned long flags;
  766. unsigned gpio;
  767. spin_lock_irqsave(&gpio_lock, flags);
  768. gpio = chip->base;
  769. while (gpio_desc[gpio].chip == chip)
  770. gpio_desc[gpio++].chip = NULL;
  771. spin_unlock_irqrestore(&gpio_lock, flags);
  772. pr_debug("%s: chip %s status %d\n", __func__,
  773. chip->label, status);
  774. }
  775. return status;
  776. }
  777. static void gpiochip_unexport(struct gpio_chip *chip)
  778. {
  779. int status;
  780. struct device *dev;
  781. mutex_lock(&sysfs_lock);
  782. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  783. if (dev) {
  784. put_device(dev);
  785. device_unregister(dev);
  786. chip->exported = 0;
  787. status = 0;
  788. } else
  789. status = -ENODEV;
  790. mutex_unlock(&sysfs_lock);
  791. if (status)
  792. pr_debug("%s: chip %s status %d\n", __func__,
  793. chip->label, status);
  794. }
  795. static int __init gpiolib_sysfs_init(void)
  796. {
  797. int status;
  798. unsigned long flags;
  799. unsigned gpio;
  800. status = class_register(&gpio_class);
  801. if (status < 0)
  802. return status;
  803. /* Scan and register the gpio_chips which registered very
  804. * early (e.g. before the class_register above was called).
  805. *
  806. * We run before arch_initcall() so chip->dev nodes can have
  807. * registered, and so arch_initcall() can always gpio_export().
  808. */
  809. spin_lock_irqsave(&gpio_lock, flags);
  810. for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
  811. struct gpio_chip *chip;
  812. chip = gpio_desc[gpio].chip;
  813. if (!chip || chip->exported)
  814. continue;
  815. spin_unlock_irqrestore(&gpio_lock, flags);
  816. status = gpiochip_export(chip);
  817. spin_lock_irqsave(&gpio_lock, flags);
  818. }
  819. spin_unlock_irqrestore(&gpio_lock, flags);
  820. return status;
  821. }
  822. postcore_initcall(gpiolib_sysfs_init);
  823. #else
  824. static inline int gpiochip_export(struct gpio_chip *chip)
  825. {
  826. return 0;
  827. }
  828. static inline void gpiochip_unexport(struct gpio_chip *chip)
  829. {
  830. }
  831. #endif /* CONFIG_GPIO_SYSFS */
  832. /**
  833. * gpiochip_add() - register a gpio_chip
  834. * @chip: the chip to register, with chip->base initialized
  835. * Context: potentially before irqs or kmalloc will work
  836. *
  837. * Returns a negative errno if the chip can't be registered, such as
  838. * because the chip->base is invalid or already associated with a
  839. * different chip. Otherwise it returns zero as a success code.
  840. *
  841. * When gpiochip_add() is called very early during boot, so that GPIOs
  842. * can be freely used, the chip->dev device must be registered before
  843. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  844. * for GPIOs will fail rudely.
  845. *
  846. * If chip->base is negative, this requests dynamic assignment of
  847. * a range of valid GPIOs.
  848. */
  849. int gpiochip_add(struct gpio_chip *chip)
  850. {
  851. unsigned long flags;
  852. int status = 0;
  853. unsigned id;
  854. int base = chip->base;
  855. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  856. && base >= 0) {
  857. status = -EINVAL;
  858. goto fail;
  859. }
  860. spin_lock_irqsave(&gpio_lock, flags);
  861. if (base < 0) {
  862. base = gpiochip_find_base(chip->ngpio);
  863. if (base < 0) {
  864. status = base;
  865. goto unlock;
  866. }
  867. chip->base = base;
  868. }
  869. /* these GPIO numbers must not be managed by another gpio_chip */
  870. for (id = base; id < base + chip->ngpio; id++) {
  871. if (gpio_desc[id].chip != NULL) {
  872. status = -EBUSY;
  873. break;
  874. }
  875. }
  876. if (status == 0) {
  877. for (id = base; id < base + chip->ngpio; id++) {
  878. gpio_desc[id].chip = chip;
  879. /* REVISIT: most hardware initializes GPIOs as
  880. * inputs (often with pullups enabled) so power
  881. * usage is minimized. Linux code should set the
  882. * gpio direction first thing; but until it does,
  883. * we may expose the wrong direction in sysfs.
  884. */
  885. gpio_desc[id].flags = !chip->direction_input
  886. ? (1 << FLAG_IS_OUT)
  887. : 0;
  888. }
  889. }
  890. of_gpiochip_add(chip);
  891. unlock:
  892. spin_unlock_irqrestore(&gpio_lock, flags);
  893. if (status)
  894. goto fail;
  895. status = gpiochip_export(chip);
  896. if (status)
  897. goto fail;
  898. pr_info("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
  899. chip->base, chip->base + chip->ngpio - 1,
  900. chip->label ? : "generic");
  901. return 0;
  902. fail:
  903. /* failures here can mean systems won't boot... */
  904. pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
  905. chip->base, chip->base + chip->ngpio - 1,
  906. chip->label ? : "generic");
  907. return status;
  908. }
  909. EXPORT_SYMBOL_GPL(gpiochip_add);
  910. /**
  911. * gpiochip_remove() - unregister a gpio_chip
  912. * @chip: the chip to unregister
  913. *
  914. * A gpio_chip with any GPIOs still requested may not be removed.
  915. */
  916. int gpiochip_remove(struct gpio_chip *chip)
  917. {
  918. unsigned long flags;
  919. int status = 0;
  920. unsigned id;
  921. spin_lock_irqsave(&gpio_lock, flags);
  922. of_gpiochip_remove(chip);
  923. for (id = chip->base; id < chip->base + chip->ngpio; id++) {
  924. if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
  925. status = -EBUSY;
  926. break;
  927. }
  928. }
  929. if (status == 0) {
  930. for (id = chip->base; id < chip->base + chip->ngpio; id++)
  931. gpio_desc[id].chip = NULL;
  932. }
  933. spin_unlock_irqrestore(&gpio_lock, flags);
  934. if (status == 0)
  935. gpiochip_unexport(chip);
  936. return status;
  937. }
  938. EXPORT_SYMBOL_GPL(gpiochip_remove);
  939. /**
  940. * gpiochip_find() - iterator for locating a specific gpio_chip
  941. * @data: data to pass to match function
  942. * @callback: Callback function to check gpio_chip
  943. *
  944. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  945. * determined by a user supplied @match callback. The callback should return
  946. * 0 if the device doesn't match and non-zero if it does. If the callback is
  947. * non-zero, this function will return to the caller and not iterate over any
  948. * more gpio_chips.
  949. */
  950. struct gpio_chip *gpiochip_find(const void *data,
  951. int (*match)(struct gpio_chip *chip,
  952. const void *data))
  953. {
  954. struct gpio_chip *chip = NULL;
  955. unsigned long flags;
  956. int i;
  957. spin_lock_irqsave(&gpio_lock, flags);
  958. for (i = 0; i < ARCH_NR_GPIOS; i++) {
  959. if (!gpio_desc[i].chip)
  960. continue;
  961. if (match(gpio_desc[i].chip, data)) {
  962. chip = gpio_desc[i].chip;
  963. break;
  964. }
  965. }
  966. spin_unlock_irqrestore(&gpio_lock, flags);
  967. return chip;
  968. }
  969. EXPORT_SYMBOL_GPL(gpiochip_find);
  970. /* These "optional" allocation calls help prevent drivers from stomping
  971. * on each other, and help provide better diagnostics in debugfs.
  972. * They're called even less than the "set direction" calls.
  973. */
  974. int gpio_request(unsigned gpio, const char *label)
  975. {
  976. struct gpio_desc *desc;
  977. struct gpio_chip *chip;
  978. int status = -EINVAL;
  979. unsigned long flags;
  980. spin_lock_irqsave(&gpio_lock, flags);
  981. if (!gpio_is_valid(gpio))
  982. goto done;
  983. desc = &gpio_desc[gpio];
  984. chip = desc->chip;
  985. if (chip == NULL)
  986. goto done;
  987. if (!try_module_get(chip->owner))
  988. goto done;
  989. /* NOTE: gpio_request() can be called in early boot,
  990. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  991. */
  992. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  993. desc_set_label(desc, label ? : "?");
  994. status = 0;
  995. } else {
  996. status = -EBUSY;
  997. module_put(chip->owner);
  998. goto done;
  999. }
  1000. if (chip->request) {
  1001. /* chip->request may sleep */
  1002. spin_unlock_irqrestore(&gpio_lock, flags);
  1003. status = chip->request(chip, gpio - chip->base);
  1004. spin_lock_irqsave(&gpio_lock, flags);
  1005. if (status < 0) {
  1006. desc_set_label(desc, NULL);
  1007. module_put(chip->owner);
  1008. clear_bit(FLAG_REQUESTED, &desc->flags);
  1009. }
  1010. }
  1011. done:
  1012. if (status)
  1013. pr_debug("gpio_request: gpio-%d (%s) status %d\n",
  1014. gpio, label ? : "?", status);
  1015. spin_unlock_irqrestore(&gpio_lock, flags);
  1016. return status;
  1017. }
  1018. EXPORT_SYMBOL_GPL(gpio_request);
  1019. void gpio_free(unsigned gpio)
  1020. {
  1021. unsigned long flags;
  1022. struct gpio_desc *desc;
  1023. struct gpio_chip *chip;
  1024. might_sleep();
  1025. if (!gpio_is_valid(gpio)) {
  1026. WARN_ON(extra_checks);
  1027. return;
  1028. }
  1029. gpio_unexport(gpio);
  1030. spin_lock_irqsave(&gpio_lock, flags);
  1031. desc = &gpio_desc[gpio];
  1032. chip = desc->chip;
  1033. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1034. if (chip->free) {
  1035. spin_unlock_irqrestore(&gpio_lock, flags);
  1036. might_sleep_if(chip->can_sleep);
  1037. chip->free(chip, gpio - chip->base);
  1038. spin_lock_irqsave(&gpio_lock, flags);
  1039. }
  1040. desc_set_label(desc, NULL);
  1041. module_put(desc->chip->owner);
  1042. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1043. clear_bit(FLAG_REQUESTED, &desc->flags);
  1044. } else
  1045. WARN_ON(extra_checks);
  1046. spin_unlock_irqrestore(&gpio_lock, flags);
  1047. }
  1048. EXPORT_SYMBOL_GPL(gpio_free);
  1049. /**
  1050. * gpio_request_one - request a single GPIO with initial configuration
  1051. * @gpio: the GPIO number
  1052. * @flags: GPIO configuration as specified by GPIOF_*
  1053. * @label: a literal description string of this GPIO
  1054. */
  1055. int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  1056. {
  1057. int err;
  1058. err = gpio_request(gpio, label);
  1059. if (err)
  1060. return err;
  1061. if (flags & GPIOF_DIR_IN)
  1062. err = gpio_direction_input(gpio);
  1063. else
  1064. err = gpio_direction_output(gpio,
  1065. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  1066. if (err)
  1067. gpio_free(gpio);
  1068. return err;
  1069. }
  1070. EXPORT_SYMBOL_GPL(gpio_request_one);
  1071. /**
  1072. * gpio_request_array - request multiple GPIOs in a single call
  1073. * @array: array of the 'struct gpio'
  1074. * @num: how many GPIOs in the array
  1075. */
  1076. int gpio_request_array(const struct gpio *array, size_t num)
  1077. {
  1078. int i, err;
  1079. for (i = 0; i < num; i++, array++) {
  1080. err = gpio_request_one(array->gpio, array->flags, array->label);
  1081. if (err)
  1082. goto err_free;
  1083. }
  1084. return 0;
  1085. err_free:
  1086. while (i--)
  1087. gpio_free((--array)->gpio);
  1088. return err;
  1089. }
  1090. EXPORT_SYMBOL_GPL(gpio_request_array);
  1091. /**
  1092. * gpio_free_array - release multiple GPIOs in a single call
  1093. * @array: array of the 'struct gpio'
  1094. * @num: how many GPIOs in the array
  1095. */
  1096. void gpio_free_array(const struct gpio *array, size_t num)
  1097. {
  1098. while (num--)
  1099. gpio_free((array++)->gpio);
  1100. }
  1101. EXPORT_SYMBOL_GPL(gpio_free_array);
  1102. /**
  1103. * gpiochip_is_requested - return string iff signal was requested
  1104. * @chip: controller managing the signal
  1105. * @offset: of signal within controller's 0..(ngpio - 1) range
  1106. *
  1107. * Returns NULL if the GPIO is not currently requested, else a string.
  1108. * If debugfs support is enabled, the string returned is the label passed
  1109. * to gpio_request(); otherwise it is a meaningless constant.
  1110. *
  1111. * This function is for use by GPIO controller drivers. The label can
  1112. * help with diagnostics, and knowing that the signal is used as a GPIO
  1113. * can help avoid accidentally multiplexing it to another controller.
  1114. */
  1115. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1116. {
  1117. unsigned gpio = chip->base + offset;
  1118. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  1119. return NULL;
  1120. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  1121. return NULL;
  1122. #ifdef CONFIG_DEBUG_FS
  1123. return gpio_desc[gpio].label;
  1124. #else
  1125. return "?";
  1126. #endif
  1127. }
  1128. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1129. /* Drivers MUST set GPIO direction before making get/set calls. In
  1130. * some cases this is done in early boot, before IRQs are enabled.
  1131. *
  1132. * As a rule these aren't called more than once (except for drivers
  1133. * using the open-drain emulation idiom) so these are natural places
  1134. * to accumulate extra debugging checks. Note that we can't (yet)
  1135. * rely on gpio_request() having been called beforehand.
  1136. */
  1137. int gpio_direction_input(unsigned gpio)
  1138. {
  1139. unsigned long flags;
  1140. struct gpio_chip *chip;
  1141. struct gpio_desc *desc = &gpio_desc[gpio];
  1142. int status = -EINVAL;
  1143. spin_lock_irqsave(&gpio_lock, flags);
  1144. if (!gpio_is_valid(gpio))
  1145. goto fail;
  1146. chip = desc->chip;
  1147. if (!chip || !chip->get || !chip->direction_input)
  1148. goto fail;
  1149. gpio -= chip->base;
  1150. if (gpio >= chip->ngpio)
  1151. goto fail;
  1152. status = gpio_ensure_requested(desc, gpio);
  1153. if (status < 0)
  1154. goto fail;
  1155. /* now we know the gpio is valid and chip won't vanish */
  1156. spin_unlock_irqrestore(&gpio_lock, flags);
  1157. might_sleep_if(chip->can_sleep);
  1158. if (status) {
  1159. status = chip->request(chip, gpio);
  1160. if (status < 0) {
  1161. pr_debug("GPIO-%d: chip request fail, %d\n",
  1162. chip->base + gpio, status);
  1163. /* and it's not available to anyone else ...
  1164. * gpio_request() is the fully clean solution.
  1165. */
  1166. goto lose;
  1167. }
  1168. }
  1169. status = chip->direction_input(chip, gpio);
  1170. if (status == 0)
  1171. clear_bit(FLAG_IS_OUT, &desc->flags);
  1172. trace_gpio_direction(chip->base + gpio, 1, status);
  1173. lose:
  1174. return status;
  1175. fail:
  1176. spin_unlock_irqrestore(&gpio_lock, flags);
  1177. if (status)
  1178. pr_debug("%s: gpio-%d status %d\n",
  1179. __func__, gpio, status);
  1180. return status;
  1181. }
  1182. EXPORT_SYMBOL_GPL(gpio_direction_input);
  1183. int gpio_direction_output(unsigned gpio, int value)
  1184. {
  1185. unsigned long flags;
  1186. struct gpio_chip *chip;
  1187. struct gpio_desc *desc = &gpio_desc[gpio];
  1188. int status = -EINVAL;
  1189. spin_lock_irqsave(&gpio_lock, flags);
  1190. if (!gpio_is_valid(gpio))
  1191. goto fail;
  1192. chip = desc->chip;
  1193. if (!chip || !chip->set || !chip->direction_output)
  1194. goto fail;
  1195. gpio -= chip->base;
  1196. if (gpio >= chip->ngpio)
  1197. goto fail;
  1198. status = gpio_ensure_requested(desc, gpio);
  1199. if (status < 0)
  1200. goto fail;
  1201. /* now we know the gpio is valid and chip won't vanish */
  1202. spin_unlock_irqrestore(&gpio_lock, flags);
  1203. might_sleep_if(chip->can_sleep);
  1204. if (status) {
  1205. status = chip->request(chip, gpio);
  1206. if (status < 0) {
  1207. pr_debug("GPIO-%d: chip request fail, %d\n",
  1208. chip->base + gpio, status);
  1209. /* and it's not available to anyone else ...
  1210. * gpio_request() is the fully clean solution.
  1211. */
  1212. goto lose;
  1213. }
  1214. }
  1215. status = chip->direction_output(chip, gpio, value);
  1216. if (status == 0)
  1217. set_bit(FLAG_IS_OUT, &desc->flags);
  1218. trace_gpio_value(chip->base + gpio, 0, value);
  1219. trace_gpio_direction(chip->base + gpio, 0, status);
  1220. lose:
  1221. return status;
  1222. fail:
  1223. spin_unlock_irqrestore(&gpio_lock, flags);
  1224. if (status)
  1225. pr_debug("%s: gpio-%d status %d\n",
  1226. __func__, gpio, status);
  1227. return status;
  1228. }
  1229. EXPORT_SYMBOL_GPL(gpio_direction_output);
  1230. /**
  1231. * gpio_set_debounce - sets @debounce time for a @gpio
  1232. * @gpio: the gpio to set debounce time
  1233. * @debounce: debounce time is microseconds
  1234. */
  1235. int gpio_set_debounce(unsigned gpio, unsigned debounce)
  1236. {
  1237. unsigned long flags;
  1238. struct gpio_chip *chip;
  1239. struct gpio_desc *desc = &gpio_desc[gpio];
  1240. int status = -EINVAL;
  1241. spin_lock_irqsave(&gpio_lock, flags);
  1242. if (!gpio_is_valid(gpio))
  1243. goto fail;
  1244. chip = desc->chip;
  1245. if (!chip || !chip->set || !chip->set_debounce)
  1246. goto fail;
  1247. gpio -= chip->base;
  1248. if (gpio >= chip->ngpio)
  1249. goto fail;
  1250. status = gpio_ensure_requested(desc, gpio);
  1251. if (status < 0)
  1252. goto fail;
  1253. /* now we know the gpio is valid and chip won't vanish */
  1254. spin_unlock_irqrestore(&gpio_lock, flags);
  1255. might_sleep_if(chip->can_sleep);
  1256. return chip->set_debounce(chip, gpio, debounce);
  1257. fail:
  1258. spin_unlock_irqrestore(&gpio_lock, flags);
  1259. if (status)
  1260. pr_debug("%s: gpio-%d status %d\n",
  1261. __func__, gpio, status);
  1262. return status;
  1263. }
  1264. EXPORT_SYMBOL_GPL(gpio_set_debounce);
  1265. /* I/O calls are only valid after configuration completed; the relevant
  1266. * "is this a valid GPIO" error checks should already have been done.
  1267. *
  1268. * "Get" operations are often inlinable as reading a pin value register,
  1269. * and masking the relevant bit in that register.
  1270. *
  1271. * When "set" operations are inlinable, they involve writing that mask to
  1272. * one register to set a low value, or a different register to set it high.
  1273. * Otherwise locking is needed, so there may be little value to inlining.
  1274. *
  1275. *------------------------------------------------------------------------
  1276. *
  1277. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1278. * have requested the GPIO. That can include implicit requesting by
  1279. * a direction setting call. Marking a gpio as requested locks its chip
  1280. * in memory, guaranteeing that these table lookups need no more locking
  1281. * and that gpiochip_remove() will fail.
  1282. *
  1283. * REVISIT when debugging, consider adding some instrumentation to ensure
  1284. * that the GPIO was actually requested.
  1285. */
  1286. /**
  1287. * __gpio_get_value() - return a gpio's value
  1288. * @gpio: gpio whose value will be returned
  1289. * Context: any
  1290. *
  1291. * This is used directly or indirectly to implement gpio_get_value().
  1292. * It returns the zero or nonzero value provided by the associated
  1293. * gpio_chip.get() method; or zero if no such method is provided.
  1294. */
  1295. int __gpio_get_value(unsigned gpio)
  1296. {
  1297. struct gpio_chip *chip;
  1298. int value;
  1299. chip = gpio_to_chip(gpio);
  1300. WARN_ON(chip->can_sleep);
  1301. value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1302. trace_gpio_value(gpio, 1, value);
  1303. return value;
  1304. }
  1305. EXPORT_SYMBOL_GPL(__gpio_get_value);
  1306. /**
  1307. * __gpio_set_value() - assign a gpio's value
  1308. * @gpio: gpio whose value will be assigned
  1309. * @value: value to assign
  1310. * Context: any
  1311. *
  1312. * This is used directly or indirectly to implement gpio_set_value().
  1313. * It invokes the associated gpio_chip.set() method.
  1314. */
  1315. void __gpio_set_value(unsigned gpio, int value)
  1316. {
  1317. struct gpio_chip *chip;
  1318. chip = gpio_to_chip(gpio);
  1319. WARN_ON(chip->can_sleep);
  1320. trace_gpio_value(gpio, 0, value);
  1321. chip->set(chip, gpio - chip->base, value);
  1322. }
  1323. EXPORT_SYMBOL_GPL(__gpio_set_value);
  1324. /**
  1325. * __gpio_cansleep() - report whether gpio value access will sleep
  1326. * @gpio: gpio in question
  1327. * Context: any
  1328. *
  1329. * This is used directly or indirectly to implement gpio_cansleep(). It
  1330. * returns nonzero if access reading or writing the GPIO value can sleep.
  1331. */
  1332. int __gpio_cansleep(unsigned gpio)
  1333. {
  1334. struct gpio_chip *chip;
  1335. /* only call this on GPIOs that are valid! */
  1336. chip = gpio_to_chip(gpio);
  1337. return chip->can_sleep;
  1338. }
  1339. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  1340. /**
  1341. * __gpio_to_irq() - return the IRQ corresponding to a GPIO
  1342. * @gpio: gpio whose IRQ will be returned (already requested)
  1343. * Context: any
  1344. *
  1345. * This is used directly or indirectly to implement gpio_to_irq().
  1346. * It returns the number of the IRQ signaled by this (input) GPIO,
  1347. * or a negative errno.
  1348. */
  1349. int __gpio_to_irq(unsigned gpio)
  1350. {
  1351. struct gpio_chip *chip;
  1352. chip = gpio_to_chip(gpio);
  1353. return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
  1354. }
  1355. EXPORT_SYMBOL_GPL(__gpio_to_irq);
  1356. /* There's no value in making it easy to inline GPIO calls that may sleep.
  1357. * Common examples include ones connected to I2C or SPI chips.
  1358. */
  1359. int gpio_get_value_cansleep(unsigned gpio)
  1360. {
  1361. struct gpio_chip *chip;
  1362. int value;
  1363. might_sleep_if(extra_checks);
  1364. chip = gpio_to_chip(gpio);
  1365. value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1366. trace_gpio_value(gpio, 1, value);
  1367. return value;
  1368. }
  1369. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  1370. void gpio_set_value_cansleep(unsigned gpio, int value)
  1371. {
  1372. struct gpio_chip *chip;
  1373. might_sleep_if(extra_checks);
  1374. chip = gpio_to_chip(gpio);
  1375. trace_gpio_value(gpio, 0, value);
  1376. chip->set(chip, gpio - chip->base, value);
  1377. }
  1378. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  1379. #ifdef CONFIG_DEBUG_FS
  1380. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1381. {
  1382. unsigned i;
  1383. unsigned gpio = chip->base;
  1384. struct gpio_desc *gdesc = &gpio_desc[gpio];
  1385. int is_out;
  1386. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1387. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1388. continue;
  1389. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1390. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
  1391. gpio, gdesc->label,
  1392. is_out ? "out" : "in ",
  1393. chip->get
  1394. ? (chip->get(chip, i) ? "hi" : "lo")
  1395. : "? ");
  1396. seq_printf(s, "\n");
  1397. }
  1398. }
  1399. static int gpiolib_show(struct seq_file *s, void *unused)
  1400. {
  1401. struct gpio_chip *chip = NULL;
  1402. unsigned gpio;
  1403. int started = 0;
  1404. /* REVISIT this isn't locked against gpio_chip removal ... */
  1405. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  1406. struct device *dev;
  1407. if (chip == gpio_desc[gpio].chip)
  1408. continue;
  1409. chip = gpio_desc[gpio].chip;
  1410. if (!chip)
  1411. continue;
  1412. seq_printf(s, "%sGPIOs %d-%d",
  1413. started ? "\n" : "",
  1414. chip->base, chip->base + chip->ngpio - 1);
  1415. dev = chip->dev;
  1416. if (dev)
  1417. seq_printf(s, ", %s/%s",
  1418. dev->bus ? dev->bus->name : "no-bus",
  1419. dev_name(dev));
  1420. if (chip->label)
  1421. seq_printf(s, ", %s", chip->label);
  1422. if (chip->can_sleep)
  1423. seq_printf(s, ", can sleep");
  1424. seq_printf(s, ":\n");
  1425. started = 1;
  1426. if (chip->dbg_show)
  1427. chip->dbg_show(s, chip);
  1428. else
  1429. gpiolib_dbg_show(s, chip);
  1430. }
  1431. return 0;
  1432. }
  1433. static int gpiolib_open(struct inode *inode, struct file *file)
  1434. {
  1435. return single_open(file, gpiolib_show, NULL);
  1436. }
  1437. static const struct file_operations gpiolib_operations = {
  1438. .open = gpiolib_open,
  1439. .read = seq_read,
  1440. .llseek = seq_lseek,
  1441. .release = single_release,
  1442. };
  1443. static int __init gpiolib_debugfs_init(void)
  1444. {
  1445. /* /sys/kernel/debug/gpio */
  1446. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1447. NULL, NULL, &gpiolib_operations);
  1448. return 0;
  1449. }
  1450. subsys_initcall(gpiolib_debugfs_init);
  1451. #endif /* DEBUG_FS */