gpiolib.c 67 KB

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