gpiolib.c 49 KB

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