gpiolib.c 75 KB

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