gpiolib.c 74 KB

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