gpiolib.c 67 KB

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