gpiolib.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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. if (status == -EPROBE_DEFER)
  515. status = -ENODEV;
  516. goto done;
  517. }
  518. status = gpio_export(gpio, true);
  519. if (status < 0)
  520. gpio_free(gpio);
  521. else
  522. set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
  523. done:
  524. if (status)
  525. pr_debug("%s: status %d\n", __func__, status);
  526. return status ? : len;
  527. }
  528. static ssize_t unexport_store(struct class *class,
  529. struct class_attribute *attr,
  530. const char *buf, size_t len)
  531. {
  532. long gpio;
  533. int status;
  534. status = strict_strtol(buf, 0, &gpio);
  535. if (status < 0)
  536. goto done;
  537. status = -EINVAL;
  538. /* reject bogus commands (gpio_unexport ignores them) */
  539. if (!gpio_is_valid(gpio))
  540. goto done;
  541. /* No extra locking here; FLAG_SYSFS just signifies that the
  542. * request and export were done by on behalf of userspace, so
  543. * they may be undone on its behalf too.
  544. */
  545. if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
  546. status = 0;
  547. gpio_free(gpio);
  548. }
  549. done:
  550. if (status)
  551. pr_debug("%s: status %d\n", __func__, status);
  552. return status ? : len;
  553. }
  554. static struct class_attribute gpio_class_attrs[] = {
  555. __ATTR(export, 0200, NULL, export_store),
  556. __ATTR(unexport, 0200, NULL, unexport_store),
  557. __ATTR_NULL,
  558. };
  559. static struct class gpio_class = {
  560. .name = "gpio",
  561. .owner = THIS_MODULE,
  562. .class_attrs = gpio_class_attrs,
  563. };
  564. /**
  565. * gpio_export - export a GPIO through sysfs
  566. * @gpio: gpio to make available, already requested
  567. * @direction_may_change: true if userspace may change gpio direction
  568. * Context: arch_initcall or later
  569. *
  570. * When drivers want to make a GPIO accessible to userspace after they
  571. * have requested it -- perhaps while debugging, or as part of their
  572. * public interface -- they may use this routine. If the GPIO can
  573. * change direction (some can't) and the caller allows it, userspace
  574. * will see "direction" sysfs attribute which may be used to change
  575. * the gpio's direction. A "value" attribute will always be provided.
  576. *
  577. * Returns zero on success, else an error.
  578. */
  579. int gpio_export(unsigned gpio, bool direction_may_change)
  580. {
  581. unsigned long flags;
  582. struct gpio_desc *desc;
  583. int status = -EINVAL;
  584. const char *ioname = NULL;
  585. /* can't export until sysfs is available ... */
  586. if (!gpio_class.p) {
  587. pr_debug("%s: called too early!\n", __func__);
  588. return -ENOENT;
  589. }
  590. if (!gpio_is_valid(gpio))
  591. goto done;
  592. mutex_lock(&sysfs_lock);
  593. spin_lock_irqsave(&gpio_lock, flags);
  594. desc = &gpio_desc[gpio];
  595. if (test_bit(FLAG_REQUESTED, &desc->flags)
  596. && !test_bit(FLAG_EXPORT, &desc->flags)) {
  597. status = 0;
  598. if (!desc->chip->direction_input
  599. || !desc->chip->direction_output)
  600. direction_may_change = false;
  601. }
  602. spin_unlock_irqrestore(&gpio_lock, flags);
  603. if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
  604. ioname = desc->chip->names[gpio - desc->chip->base];
  605. if (status == 0) {
  606. struct device *dev;
  607. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  608. desc, ioname ? ioname : "gpio%u", gpio);
  609. if (!IS_ERR(dev)) {
  610. status = sysfs_create_group(&dev->kobj,
  611. &gpio_attr_group);
  612. if (!status && direction_may_change)
  613. status = device_create_file(dev,
  614. &dev_attr_direction);
  615. if (!status && gpio_to_irq(gpio) >= 0
  616. && (direction_may_change
  617. || !test_bit(FLAG_IS_OUT,
  618. &desc->flags)))
  619. status = device_create_file(dev,
  620. &dev_attr_edge);
  621. if (status != 0)
  622. device_unregister(dev);
  623. } else
  624. status = PTR_ERR(dev);
  625. if (status == 0)
  626. set_bit(FLAG_EXPORT, &desc->flags);
  627. }
  628. mutex_unlock(&sysfs_lock);
  629. done:
  630. if (status)
  631. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  632. return status;
  633. }
  634. EXPORT_SYMBOL_GPL(gpio_export);
  635. static int match_export(struct device *dev, void *data)
  636. {
  637. return dev_get_drvdata(dev) == data;
  638. }
  639. /**
  640. * gpio_export_link - create a sysfs link to an exported GPIO node
  641. * @dev: device under which to create symlink
  642. * @name: name of the symlink
  643. * @gpio: gpio to create symlink to, already exported
  644. *
  645. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  646. * node. Caller is responsible for unlinking.
  647. *
  648. * Returns zero on success, else an error.
  649. */
  650. int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
  651. {
  652. struct gpio_desc *desc;
  653. int status = -EINVAL;
  654. if (!gpio_is_valid(gpio))
  655. goto done;
  656. mutex_lock(&sysfs_lock);
  657. desc = &gpio_desc[gpio];
  658. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  659. struct device *tdev;
  660. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  661. if (tdev != NULL) {
  662. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  663. name);
  664. } else {
  665. status = -ENODEV;
  666. }
  667. }
  668. mutex_unlock(&sysfs_lock);
  669. done:
  670. if (status)
  671. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  672. return status;
  673. }
  674. EXPORT_SYMBOL_GPL(gpio_export_link);
  675. /**
  676. * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
  677. * @gpio: gpio to change
  678. * @value: non-zero to use active low, i.e. inverted values
  679. *
  680. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  681. * The GPIO does not have to be exported yet. If poll(2) support has
  682. * been enabled for either rising or falling edge, it will be
  683. * reconfigured to follow the new polarity.
  684. *
  685. * Returns zero on success, else an error.
  686. */
  687. int gpio_sysfs_set_active_low(unsigned gpio, int value)
  688. {
  689. struct gpio_desc *desc;
  690. struct device *dev = NULL;
  691. int status = -EINVAL;
  692. if (!gpio_is_valid(gpio))
  693. goto done;
  694. mutex_lock(&sysfs_lock);
  695. desc = &gpio_desc[gpio];
  696. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  697. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  698. if (dev == NULL) {
  699. status = -ENODEV;
  700. goto unlock;
  701. }
  702. }
  703. status = sysfs_set_active_low(desc, dev, value);
  704. unlock:
  705. mutex_unlock(&sysfs_lock);
  706. done:
  707. if (status)
  708. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  709. return status;
  710. }
  711. EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
  712. /**
  713. * gpio_unexport - reverse effect of gpio_export()
  714. * @gpio: gpio to make unavailable
  715. *
  716. * This is implicit on gpio_free().
  717. */
  718. void gpio_unexport(unsigned gpio)
  719. {
  720. struct gpio_desc *desc;
  721. int status = 0;
  722. struct device *dev = NULL;
  723. if (!gpio_is_valid(gpio)) {
  724. status = -EINVAL;
  725. goto done;
  726. }
  727. mutex_lock(&sysfs_lock);
  728. desc = &gpio_desc[gpio];
  729. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  730. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  731. if (dev) {
  732. gpio_setup_irq(desc, dev, 0);
  733. clear_bit(FLAG_EXPORT, &desc->flags);
  734. } else
  735. status = -ENODEV;
  736. }
  737. mutex_unlock(&sysfs_lock);
  738. if (dev) {
  739. device_unregister(dev);
  740. put_device(dev);
  741. }
  742. done:
  743. if (status)
  744. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  745. }
  746. EXPORT_SYMBOL_GPL(gpio_unexport);
  747. static int gpiochip_export(struct gpio_chip *chip)
  748. {
  749. int status;
  750. struct device *dev;
  751. /* Many systems register gpio chips for SOC support very early,
  752. * before driver model support is available. In those cases we
  753. * export this later, in gpiolib_sysfs_init() ... here we just
  754. * verify that _some_ field of gpio_class got initialized.
  755. */
  756. if (!gpio_class.p)
  757. return 0;
  758. /* use chip->base for the ID; it's already known to be unique */
  759. mutex_lock(&sysfs_lock);
  760. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  761. "gpiochip%d", chip->base);
  762. if (!IS_ERR(dev)) {
  763. status = sysfs_create_group(&dev->kobj,
  764. &gpiochip_attr_group);
  765. } else
  766. status = PTR_ERR(dev);
  767. chip->exported = (status == 0);
  768. mutex_unlock(&sysfs_lock);
  769. if (status) {
  770. unsigned long flags;
  771. unsigned gpio;
  772. spin_lock_irqsave(&gpio_lock, flags);
  773. gpio = chip->base;
  774. while (gpio_desc[gpio].chip == chip)
  775. gpio_desc[gpio++].chip = NULL;
  776. spin_unlock_irqrestore(&gpio_lock, flags);
  777. pr_debug("%s: chip %s status %d\n", __func__,
  778. chip->label, status);
  779. }
  780. return status;
  781. }
  782. static void gpiochip_unexport(struct gpio_chip *chip)
  783. {
  784. int status;
  785. struct device *dev;
  786. mutex_lock(&sysfs_lock);
  787. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  788. if (dev) {
  789. put_device(dev);
  790. device_unregister(dev);
  791. chip->exported = 0;
  792. status = 0;
  793. } else
  794. status = -ENODEV;
  795. mutex_unlock(&sysfs_lock);
  796. if (status)
  797. pr_debug("%s: chip %s status %d\n", __func__,
  798. chip->label, status);
  799. }
  800. static int __init gpiolib_sysfs_init(void)
  801. {
  802. int status;
  803. unsigned long flags;
  804. unsigned gpio;
  805. status = class_register(&gpio_class);
  806. if (status < 0)
  807. return status;
  808. /* Scan and register the gpio_chips which registered very
  809. * early (e.g. before the class_register above was called).
  810. *
  811. * We run before arch_initcall() so chip->dev nodes can have
  812. * registered, and so arch_initcall() can always gpio_export().
  813. */
  814. spin_lock_irqsave(&gpio_lock, flags);
  815. for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
  816. struct gpio_chip *chip;
  817. chip = gpio_desc[gpio].chip;
  818. if (!chip || chip->exported)
  819. continue;
  820. spin_unlock_irqrestore(&gpio_lock, flags);
  821. status = gpiochip_export(chip);
  822. spin_lock_irqsave(&gpio_lock, flags);
  823. }
  824. spin_unlock_irqrestore(&gpio_lock, flags);
  825. return status;
  826. }
  827. postcore_initcall(gpiolib_sysfs_init);
  828. #else
  829. static inline int gpiochip_export(struct gpio_chip *chip)
  830. {
  831. return 0;
  832. }
  833. static inline void gpiochip_unexport(struct gpio_chip *chip)
  834. {
  835. }
  836. #endif /* CONFIG_GPIO_SYSFS */
  837. /**
  838. * gpiochip_add() - register a gpio_chip
  839. * @chip: the chip to register, with chip->base initialized
  840. * Context: potentially before irqs or kmalloc will work
  841. *
  842. * Returns a negative errno if the chip can't be registered, such as
  843. * because the chip->base is invalid or already associated with a
  844. * different chip. Otherwise it returns zero as a success code.
  845. *
  846. * When gpiochip_add() is called very early during boot, so that GPIOs
  847. * can be freely used, the chip->dev device must be registered before
  848. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  849. * for GPIOs will fail rudely.
  850. *
  851. * If chip->base is negative, this requests dynamic assignment of
  852. * a range of valid GPIOs.
  853. */
  854. int gpiochip_add(struct gpio_chip *chip)
  855. {
  856. unsigned long flags;
  857. int status = 0;
  858. unsigned id;
  859. int base = chip->base;
  860. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  861. && base >= 0) {
  862. status = -EINVAL;
  863. goto fail;
  864. }
  865. spin_lock_irqsave(&gpio_lock, flags);
  866. if (base < 0) {
  867. base = gpiochip_find_base(chip->ngpio);
  868. if (base < 0) {
  869. status = base;
  870. goto unlock;
  871. }
  872. chip->base = base;
  873. }
  874. /* these GPIO numbers must not be managed by another gpio_chip */
  875. for (id = base; id < base + chip->ngpio; id++) {
  876. if (gpio_desc[id].chip != NULL) {
  877. status = -EBUSY;
  878. break;
  879. }
  880. }
  881. if (status == 0) {
  882. for (id = base; id < base + chip->ngpio; id++) {
  883. gpio_desc[id].chip = chip;
  884. /* REVISIT: most hardware initializes GPIOs as
  885. * inputs (often with pullups enabled) so power
  886. * usage is minimized. Linux code should set the
  887. * gpio direction first thing; but until it does,
  888. * we may expose the wrong direction in sysfs.
  889. */
  890. gpio_desc[id].flags = !chip->direction_input
  891. ? (1 << FLAG_IS_OUT)
  892. : 0;
  893. }
  894. }
  895. of_gpiochip_add(chip);
  896. unlock:
  897. spin_unlock_irqrestore(&gpio_lock, flags);
  898. if (status)
  899. goto fail;
  900. status = gpiochip_export(chip);
  901. if (status)
  902. goto fail;
  903. pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
  904. chip->base, chip->base + chip->ngpio - 1,
  905. chip->label ? : "generic");
  906. return 0;
  907. fail:
  908. /* failures here can mean systems won't boot... */
  909. pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
  910. chip->base, chip->base + chip->ngpio - 1,
  911. chip->label ? : "generic");
  912. return status;
  913. }
  914. EXPORT_SYMBOL_GPL(gpiochip_add);
  915. /**
  916. * gpiochip_remove() - unregister a gpio_chip
  917. * @chip: the chip to unregister
  918. *
  919. * A gpio_chip with any GPIOs still requested may not be removed.
  920. */
  921. int gpiochip_remove(struct gpio_chip *chip)
  922. {
  923. unsigned long flags;
  924. int status = 0;
  925. unsigned id;
  926. spin_lock_irqsave(&gpio_lock, flags);
  927. of_gpiochip_remove(chip);
  928. for (id = chip->base; id < chip->base + chip->ngpio; id++) {
  929. if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
  930. status = -EBUSY;
  931. break;
  932. }
  933. }
  934. if (status == 0) {
  935. for (id = chip->base; id < chip->base + chip->ngpio; id++)
  936. gpio_desc[id].chip = NULL;
  937. }
  938. spin_unlock_irqrestore(&gpio_lock, flags);
  939. if (status == 0)
  940. gpiochip_unexport(chip);
  941. return status;
  942. }
  943. EXPORT_SYMBOL_GPL(gpiochip_remove);
  944. /**
  945. * gpiochip_find() - iterator for locating a specific gpio_chip
  946. * @data: data to pass to match function
  947. * @callback: Callback function to check gpio_chip
  948. *
  949. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  950. * determined by a user supplied @match callback. The callback should return
  951. * 0 if the device doesn't match and non-zero if it does. If the callback is
  952. * non-zero, this function will return to the caller and not iterate over any
  953. * more gpio_chips.
  954. */
  955. struct gpio_chip *gpiochip_find(void *data,
  956. int (*match)(struct gpio_chip *chip,
  957. void *data))
  958. {
  959. struct gpio_chip *chip = NULL;
  960. unsigned long flags;
  961. int i;
  962. spin_lock_irqsave(&gpio_lock, flags);
  963. for (i = 0; i < ARCH_NR_GPIOS; i++) {
  964. if (!gpio_desc[i].chip)
  965. continue;
  966. if (match(gpio_desc[i].chip, data)) {
  967. chip = gpio_desc[i].chip;
  968. break;
  969. }
  970. }
  971. spin_unlock_irqrestore(&gpio_lock, flags);
  972. return chip;
  973. }
  974. EXPORT_SYMBOL_GPL(gpiochip_find);
  975. /* These "optional" allocation calls help prevent drivers from stomping
  976. * on each other, and help provide better diagnostics in debugfs.
  977. * They're called even less than the "set direction" calls.
  978. */
  979. int gpio_request(unsigned gpio, const char *label)
  980. {
  981. struct gpio_desc *desc;
  982. struct gpio_chip *chip;
  983. int status = -EPROBE_DEFER;
  984. unsigned long flags;
  985. spin_lock_irqsave(&gpio_lock, flags);
  986. if (!gpio_is_valid(gpio)) {
  987. status = -EINVAL;
  988. goto done;
  989. }
  990. desc = &gpio_desc[gpio];
  991. chip = desc->chip;
  992. if (chip == NULL)
  993. goto done;
  994. if (!try_module_get(chip->owner))
  995. goto done;
  996. /* NOTE: gpio_request() can be called in early boot,
  997. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  998. */
  999. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1000. desc_set_label(desc, label ? : "?");
  1001. status = 0;
  1002. } else {
  1003. status = -EBUSY;
  1004. module_put(chip->owner);
  1005. goto done;
  1006. }
  1007. if (chip->request) {
  1008. /* chip->request may sleep */
  1009. spin_unlock_irqrestore(&gpio_lock, flags);
  1010. status = chip->request(chip, gpio - chip->base);
  1011. spin_lock_irqsave(&gpio_lock, flags);
  1012. if (status < 0) {
  1013. desc_set_label(desc, NULL);
  1014. module_put(chip->owner);
  1015. clear_bit(FLAG_REQUESTED, &desc->flags);
  1016. }
  1017. }
  1018. done:
  1019. if (status)
  1020. pr_debug("gpio_request: gpio-%d (%s) status %d\n",
  1021. gpio, label ? : "?", status);
  1022. spin_unlock_irqrestore(&gpio_lock, flags);
  1023. return status;
  1024. }
  1025. EXPORT_SYMBOL_GPL(gpio_request);
  1026. void gpio_free(unsigned gpio)
  1027. {
  1028. unsigned long flags;
  1029. struct gpio_desc *desc;
  1030. struct gpio_chip *chip;
  1031. might_sleep();
  1032. if (!gpio_is_valid(gpio)) {
  1033. WARN_ON(extra_checks);
  1034. return;
  1035. }
  1036. gpio_unexport(gpio);
  1037. spin_lock_irqsave(&gpio_lock, flags);
  1038. desc = &gpio_desc[gpio];
  1039. chip = desc->chip;
  1040. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1041. if (chip->free) {
  1042. spin_unlock_irqrestore(&gpio_lock, flags);
  1043. might_sleep_if(chip->can_sleep);
  1044. chip->free(chip, gpio - chip->base);
  1045. spin_lock_irqsave(&gpio_lock, flags);
  1046. }
  1047. desc_set_label(desc, NULL);
  1048. module_put(desc->chip->owner);
  1049. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1050. clear_bit(FLAG_REQUESTED, &desc->flags);
  1051. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1052. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1053. } else
  1054. WARN_ON(extra_checks);
  1055. spin_unlock_irqrestore(&gpio_lock, flags);
  1056. }
  1057. EXPORT_SYMBOL_GPL(gpio_free);
  1058. /**
  1059. * gpio_request_one - request a single GPIO with initial configuration
  1060. * @gpio: the GPIO number
  1061. * @flags: GPIO configuration as specified by GPIOF_*
  1062. * @label: a literal description string of this GPIO
  1063. */
  1064. int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  1065. {
  1066. int err;
  1067. err = gpio_request(gpio, label);
  1068. if (err)
  1069. return err;
  1070. if (flags & GPIOF_OPEN_DRAIN)
  1071. set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
  1072. if (flags & GPIOF_OPEN_SOURCE)
  1073. set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
  1074. if (flags & GPIOF_DIR_IN)
  1075. err = gpio_direction_input(gpio);
  1076. else
  1077. err = gpio_direction_output(gpio,
  1078. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  1079. if (err)
  1080. goto free_gpio;
  1081. if (flags & GPIOF_EXPORT) {
  1082. err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
  1083. if (err)
  1084. goto free_gpio;
  1085. }
  1086. return 0;
  1087. free_gpio:
  1088. gpio_free(gpio);
  1089. return err;
  1090. }
  1091. EXPORT_SYMBOL_GPL(gpio_request_one);
  1092. /**
  1093. * gpio_request_array - request multiple GPIOs in a single call
  1094. * @array: array of the 'struct gpio'
  1095. * @num: how many GPIOs in the array
  1096. */
  1097. int gpio_request_array(const struct gpio *array, size_t num)
  1098. {
  1099. int i, err;
  1100. for (i = 0; i < num; i++, array++) {
  1101. err = gpio_request_one(array->gpio, array->flags, array->label);
  1102. if (err)
  1103. goto err_free;
  1104. }
  1105. return 0;
  1106. err_free:
  1107. while (i--)
  1108. gpio_free((--array)->gpio);
  1109. return err;
  1110. }
  1111. EXPORT_SYMBOL_GPL(gpio_request_array);
  1112. /**
  1113. * gpio_free_array - release multiple GPIOs in a single call
  1114. * @array: array of the 'struct gpio'
  1115. * @num: how many GPIOs in the array
  1116. */
  1117. void gpio_free_array(const struct gpio *array, size_t num)
  1118. {
  1119. while (num--)
  1120. gpio_free((array++)->gpio);
  1121. }
  1122. EXPORT_SYMBOL_GPL(gpio_free_array);
  1123. /**
  1124. * gpiochip_is_requested - return string iff signal was requested
  1125. * @chip: controller managing the signal
  1126. * @offset: of signal within controller's 0..(ngpio - 1) range
  1127. *
  1128. * Returns NULL if the GPIO is not currently requested, else a string.
  1129. * If debugfs support is enabled, the string returned is the label passed
  1130. * to gpio_request(); otherwise it is a meaningless constant.
  1131. *
  1132. * This function is for use by GPIO controller drivers. The label can
  1133. * help with diagnostics, and knowing that the signal is used as a GPIO
  1134. * can help avoid accidentally multiplexing it to another controller.
  1135. */
  1136. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1137. {
  1138. unsigned gpio = chip->base + offset;
  1139. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  1140. return NULL;
  1141. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  1142. return NULL;
  1143. #ifdef CONFIG_DEBUG_FS
  1144. return gpio_desc[gpio].label;
  1145. #else
  1146. return "?";
  1147. #endif
  1148. }
  1149. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1150. /* Drivers MUST set GPIO direction before making get/set calls. In
  1151. * some cases this is done in early boot, before IRQs are enabled.
  1152. *
  1153. * As a rule these aren't called more than once (except for drivers
  1154. * using the open-drain emulation idiom) so these are natural places
  1155. * to accumulate extra debugging checks. Note that we can't (yet)
  1156. * rely on gpio_request() having been called beforehand.
  1157. */
  1158. int gpio_direction_input(unsigned gpio)
  1159. {
  1160. unsigned long flags;
  1161. struct gpio_chip *chip;
  1162. struct gpio_desc *desc = &gpio_desc[gpio];
  1163. int status = -EINVAL;
  1164. spin_lock_irqsave(&gpio_lock, flags);
  1165. if (!gpio_is_valid(gpio))
  1166. goto fail;
  1167. chip = desc->chip;
  1168. if (!chip || !chip->get || !chip->direction_input)
  1169. goto fail;
  1170. gpio -= chip->base;
  1171. if (gpio >= chip->ngpio)
  1172. goto fail;
  1173. status = gpio_ensure_requested(desc, gpio);
  1174. if (status < 0)
  1175. goto fail;
  1176. /* now we know the gpio is valid and chip won't vanish */
  1177. spin_unlock_irqrestore(&gpio_lock, flags);
  1178. might_sleep_if(chip->can_sleep);
  1179. if (status) {
  1180. status = chip->request(chip, gpio);
  1181. if (status < 0) {
  1182. pr_debug("GPIO-%d: chip request fail, %d\n",
  1183. chip->base + gpio, status);
  1184. /* and it's not available to anyone else ...
  1185. * gpio_request() is the fully clean solution.
  1186. */
  1187. goto lose;
  1188. }
  1189. }
  1190. status = chip->direction_input(chip, gpio);
  1191. if (status == 0)
  1192. clear_bit(FLAG_IS_OUT, &desc->flags);
  1193. trace_gpio_direction(chip->base + gpio, 1, status);
  1194. lose:
  1195. return status;
  1196. fail:
  1197. spin_unlock_irqrestore(&gpio_lock, flags);
  1198. if (status)
  1199. pr_debug("%s: gpio-%d status %d\n",
  1200. __func__, gpio, status);
  1201. return status;
  1202. }
  1203. EXPORT_SYMBOL_GPL(gpio_direction_input);
  1204. int gpio_direction_output(unsigned gpio, int value)
  1205. {
  1206. unsigned long flags;
  1207. struct gpio_chip *chip;
  1208. struct gpio_desc *desc = &gpio_desc[gpio];
  1209. int status = -EINVAL;
  1210. /* Open drain pin should not be driven to 1 */
  1211. if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1212. return gpio_direction_input(gpio);
  1213. /* Open source pin should not be driven to 0 */
  1214. if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1215. return gpio_direction_input(gpio);
  1216. spin_lock_irqsave(&gpio_lock, flags);
  1217. if (!gpio_is_valid(gpio))
  1218. goto fail;
  1219. chip = desc->chip;
  1220. if (!chip || !chip->set || !chip->direction_output)
  1221. goto fail;
  1222. gpio -= chip->base;
  1223. if (gpio >= chip->ngpio)
  1224. goto fail;
  1225. status = gpio_ensure_requested(desc, gpio);
  1226. if (status < 0)
  1227. goto fail;
  1228. /* now we know the gpio is valid and chip won't vanish */
  1229. spin_unlock_irqrestore(&gpio_lock, flags);
  1230. might_sleep_if(chip->can_sleep);
  1231. if (status) {
  1232. status = chip->request(chip, gpio);
  1233. if (status < 0) {
  1234. pr_debug("GPIO-%d: chip request fail, %d\n",
  1235. chip->base + gpio, status);
  1236. /* and it's not available to anyone else ...
  1237. * gpio_request() is the fully clean solution.
  1238. */
  1239. goto lose;
  1240. }
  1241. }
  1242. status = chip->direction_output(chip, gpio, value);
  1243. if (status == 0)
  1244. set_bit(FLAG_IS_OUT, &desc->flags);
  1245. trace_gpio_value(chip->base + gpio, 0, value);
  1246. trace_gpio_direction(chip->base + gpio, 0, status);
  1247. lose:
  1248. return status;
  1249. fail:
  1250. spin_unlock_irqrestore(&gpio_lock, flags);
  1251. if (status)
  1252. pr_debug("%s: gpio-%d status %d\n",
  1253. __func__, gpio, status);
  1254. return status;
  1255. }
  1256. EXPORT_SYMBOL_GPL(gpio_direction_output);
  1257. /**
  1258. * gpio_set_debounce - sets @debounce time for a @gpio
  1259. * @gpio: the gpio to set debounce time
  1260. * @debounce: debounce time is microseconds
  1261. */
  1262. int gpio_set_debounce(unsigned gpio, unsigned debounce)
  1263. {
  1264. unsigned long flags;
  1265. struct gpio_chip *chip;
  1266. struct gpio_desc *desc = &gpio_desc[gpio];
  1267. int status = -EINVAL;
  1268. spin_lock_irqsave(&gpio_lock, flags);
  1269. if (!gpio_is_valid(gpio))
  1270. goto fail;
  1271. chip = desc->chip;
  1272. if (!chip || !chip->set || !chip->set_debounce)
  1273. goto fail;
  1274. gpio -= chip->base;
  1275. if (gpio >= chip->ngpio)
  1276. goto fail;
  1277. status = gpio_ensure_requested(desc, gpio);
  1278. if (status < 0)
  1279. goto fail;
  1280. /* now we know the gpio is valid and chip won't vanish */
  1281. spin_unlock_irqrestore(&gpio_lock, flags);
  1282. might_sleep_if(chip->can_sleep);
  1283. return chip->set_debounce(chip, gpio, debounce);
  1284. fail:
  1285. spin_unlock_irqrestore(&gpio_lock, flags);
  1286. if (status)
  1287. pr_debug("%s: gpio-%d status %d\n",
  1288. __func__, gpio, status);
  1289. return status;
  1290. }
  1291. EXPORT_SYMBOL_GPL(gpio_set_debounce);
  1292. /* I/O calls are only valid after configuration completed; the relevant
  1293. * "is this a valid GPIO" error checks should already have been done.
  1294. *
  1295. * "Get" operations are often inlinable as reading a pin value register,
  1296. * and masking the relevant bit in that register.
  1297. *
  1298. * When "set" operations are inlinable, they involve writing that mask to
  1299. * one register to set a low value, or a different register to set it high.
  1300. * Otherwise locking is needed, so there may be little value to inlining.
  1301. *
  1302. *------------------------------------------------------------------------
  1303. *
  1304. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1305. * have requested the GPIO. That can include implicit requesting by
  1306. * a direction setting call. Marking a gpio as requested locks its chip
  1307. * in memory, guaranteeing that these table lookups need no more locking
  1308. * and that gpiochip_remove() will fail.
  1309. *
  1310. * REVISIT when debugging, consider adding some instrumentation to ensure
  1311. * that the GPIO was actually requested.
  1312. */
  1313. /**
  1314. * __gpio_get_value() - return a gpio's value
  1315. * @gpio: gpio whose value will be returned
  1316. * Context: any
  1317. *
  1318. * This is used directly or indirectly to implement gpio_get_value().
  1319. * It returns the zero or nonzero value provided by the associated
  1320. * gpio_chip.get() method; or zero if no such method is provided.
  1321. */
  1322. int __gpio_get_value(unsigned gpio)
  1323. {
  1324. struct gpio_chip *chip;
  1325. int value;
  1326. chip = gpio_to_chip(gpio);
  1327. /* Should be using gpio_get_value_cansleep() */
  1328. WARN_ON(chip->can_sleep);
  1329. value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1330. trace_gpio_value(gpio, 1, value);
  1331. return value;
  1332. }
  1333. EXPORT_SYMBOL_GPL(__gpio_get_value);
  1334. /*
  1335. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  1336. * @gpio: Gpio whose state need to be set.
  1337. * @chip: Gpio chip.
  1338. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1339. */
  1340. static void _gpio_set_open_drain_value(unsigned gpio,
  1341. struct gpio_chip *chip, int value)
  1342. {
  1343. int err = 0;
  1344. if (value) {
  1345. err = chip->direction_input(chip, gpio - chip->base);
  1346. if (!err)
  1347. clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
  1348. } else {
  1349. err = chip->direction_output(chip, gpio - chip->base, 0);
  1350. if (!err)
  1351. set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
  1352. }
  1353. trace_gpio_direction(gpio, value, err);
  1354. if (err < 0)
  1355. pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
  1356. __func__, gpio, err);
  1357. }
  1358. /*
  1359. * _gpio_set_open_source() - Set the open source gpio's value.
  1360. * @gpio: Gpio whose state need to be set.
  1361. * @chip: Gpio chip.
  1362. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1363. */
  1364. static void _gpio_set_open_source_value(unsigned gpio,
  1365. struct gpio_chip *chip, int value)
  1366. {
  1367. int err = 0;
  1368. if (value) {
  1369. err = chip->direction_output(chip, gpio - chip->base, 1);
  1370. if (!err)
  1371. set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
  1372. } else {
  1373. err = chip->direction_input(chip, gpio - chip->base);
  1374. if (!err)
  1375. clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
  1376. }
  1377. trace_gpio_direction(gpio, !value, err);
  1378. if (err < 0)
  1379. pr_err("%s: Error in set_value for open source gpio%d err %d\n",
  1380. __func__, gpio, err);
  1381. }
  1382. /**
  1383. * __gpio_set_value() - assign a gpio's value
  1384. * @gpio: gpio whose value will be assigned
  1385. * @value: value to assign
  1386. * Context: any
  1387. *
  1388. * This is used directly or indirectly to implement gpio_set_value().
  1389. * It invokes the associated gpio_chip.set() method.
  1390. */
  1391. void __gpio_set_value(unsigned gpio, int value)
  1392. {
  1393. struct gpio_chip *chip;
  1394. chip = gpio_to_chip(gpio);
  1395. /* Should be using gpio_set_value_cansleep() */
  1396. WARN_ON(chip->can_sleep);
  1397. trace_gpio_value(gpio, 0, value);
  1398. if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
  1399. _gpio_set_open_drain_value(gpio, chip, value);
  1400. else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
  1401. _gpio_set_open_source_value(gpio, chip, value);
  1402. else
  1403. chip->set(chip, gpio - chip->base, value);
  1404. }
  1405. EXPORT_SYMBOL_GPL(__gpio_set_value);
  1406. /**
  1407. * __gpio_cansleep() - report whether gpio value access will sleep
  1408. * @gpio: gpio in question
  1409. * Context: any
  1410. *
  1411. * This is used directly or indirectly to implement gpio_cansleep(). It
  1412. * returns nonzero if access reading or writing the GPIO value can sleep.
  1413. */
  1414. int __gpio_cansleep(unsigned gpio)
  1415. {
  1416. struct gpio_chip *chip;
  1417. /* only call this on GPIOs that are valid! */
  1418. chip = gpio_to_chip(gpio);
  1419. return chip->can_sleep;
  1420. }
  1421. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  1422. /**
  1423. * __gpio_to_irq() - return the IRQ corresponding to a GPIO
  1424. * @gpio: gpio whose IRQ will be returned (already requested)
  1425. * Context: any
  1426. *
  1427. * This is used directly or indirectly to implement gpio_to_irq().
  1428. * It returns the number of the IRQ signaled by this (input) GPIO,
  1429. * or a negative errno.
  1430. */
  1431. int __gpio_to_irq(unsigned gpio)
  1432. {
  1433. struct gpio_chip *chip;
  1434. chip = gpio_to_chip(gpio);
  1435. return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
  1436. }
  1437. EXPORT_SYMBOL_GPL(__gpio_to_irq);
  1438. /* There's no value in making it easy to inline GPIO calls that may sleep.
  1439. * Common examples include ones connected to I2C or SPI chips.
  1440. */
  1441. int gpio_get_value_cansleep(unsigned gpio)
  1442. {
  1443. struct gpio_chip *chip;
  1444. int value;
  1445. might_sleep_if(extra_checks);
  1446. chip = gpio_to_chip(gpio);
  1447. value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1448. trace_gpio_value(gpio, 1, value);
  1449. return value;
  1450. }
  1451. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  1452. void gpio_set_value_cansleep(unsigned gpio, int value)
  1453. {
  1454. struct gpio_chip *chip;
  1455. might_sleep_if(extra_checks);
  1456. chip = gpio_to_chip(gpio);
  1457. trace_gpio_value(gpio, 0, value);
  1458. if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
  1459. _gpio_set_open_drain_value(gpio, chip, value);
  1460. else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
  1461. _gpio_set_open_source_value(gpio, chip, value);
  1462. else
  1463. chip->set(chip, gpio - chip->base, value);
  1464. }
  1465. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  1466. #ifdef CONFIG_DEBUG_FS
  1467. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1468. {
  1469. unsigned i;
  1470. unsigned gpio = chip->base;
  1471. struct gpio_desc *gdesc = &gpio_desc[gpio];
  1472. int is_out;
  1473. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1474. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1475. continue;
  1476. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1477. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
  1478. gpio, gdesc->label,
  1479. is_out ? "out" : "in ",
  1480. chip->get
  1481. ? (chip->get(chip, i) ? "hi" : "lo")
  1482. : "? ");
  1483. seq_printf(s, "\n");
  1484. }
  1485. }
  1486. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  1487. {
  1488. struct gpio_chip *chip = NULL;
  1489. unsigned int gpio;
  1490. void *ret = NULL;
  1491. loff_t index = 0;
  1492. /* REVISIT this isn't locked against gpio_chip removal ... */
  1493. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  1494. if (gpio_desc[gpio].chip == chip)
  1495. continue;
  1496. chip = gpio_desc[gpio].chip;
  1497. if (!chip)
  1498. continue;
  1499. if (index++ >= *pos) {
  1500. ret = chip;
  1501. break;
  1502. }
  1503. }
  1504. s->private = "";
  1505. return ret;
  1506. }
  1507. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1508. {
  1509. struct gpio_chip *chip = v;
  1510. unsigned int gpio;
  1511. void *ret = NULL;
  1512. /* skip GPIOs provided by the current chip */
  1513. for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
  1514. chip = gpio_desc[gpio].chip;
  1515. if (chip) {
  1516. ret = chip;
  1517. break;
  1518. }
  1519. }
  1520. s->private = "\n";
  1521. ++*pos;
  1522. return ret;
  1523. }
  1524. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  1525. {
  1526. }
  1527. static int gpiolib_seq_show(struct seq_file *s, void *v)
  1528. {
  1529. struct gpio_chip *chip = v;
  1530. struct device *dev;
  1531. seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
  1532. chip->base, chip->base + chip->ngpio - 1);
  1533. dev = chip->dev;
  1534. if (dev)
  1535. seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
  1536. dev_name(dev));
  1537. if (chip->label)
  1538. seq_printf(s, ", %s", chip->label);
  1539. if (chip->can_sleep)
  1540. seq_printf(s, ", can sleep");
  1541. seq_printf(s, ":\n");
  1542. if (chip->dbg_show)
  1543. chip->dbg_show(s, chip);
  1544. else
  1545. gpiolib_dbg_show(s, chip);
  1546. return 0;
  1547. }
  1548. static const struct seq_operations gpiolib_seq_ops = {
  1549. .start = gpiolib_seq_start,
  1550. .next = gpiolib_seq_next,
  1551. .stop = gpiolib_seq_stop,
  1552. .show = gpiolib_seq_show,
  1553. };
  1554. static int gpiolib_open(struct inode *inode, struct file *file)
  1555. {
  1556. return seq_open(file, &gpiolib_seq_ops);
  1557. }
  1558. static const struct file_operations gpiolib_operations = {
  1559. .owner = THIS_MODULE,
  1560. .open = gpiolib_open,
  1561. .read = seq_read,
  1562. .llseek = seq_lseek,
  1563. .release = seq_release,
  1564. };
  1565. static int __init gpiolib_debugfs_init(void)
  1566. {
  1567. /* /sys/kernel/debug/gpio */
  1568. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1569. NULL, NULL, &gpiolib_operations);
  1570. return 0;
  1571. }
  1572. subsys_initcall(gpiolib_debugfs_init);
  1573. #endif /* DEBUG_FS */