gpiolib.c 68 KB

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