gpiolib.c 64 KB

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