gpiolib.c 68 KB

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