gpiolib.c 53 KB

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