gpiolib.c 58 KB

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