gpiolib.c 64 KB

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