gpiolib.c 57 KB

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