gpiolib.c 45 KB

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