gpiolib.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025
  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 <linux/idr.h>
  20. #include <linux/cdev.h>
  21. #include <linux/fs.h>
  22. #include <linux/uaccess.h>
  23. #include <uapi/linux/gpio.h>
  24. #include "gpiolib.h"
  25. #define CREATE_TRACE_POINTS
  26. #include <trace/events/gpio.h>
  27. /* Implementation infrastructure for GPIO interfaces.
  28. *
  29. * The GPIO programming interface allows for inlining speed-critical
  30. * get/set operations for common cases, so that access to SOC-integrated
  31. * GPIOs can sometimes cost only an instruction or two per bit.
  32. */
  33. /* When debugging, extend minimal trust to callers and platform code.
  34. * Also emit diagnostic messages that may help initial bringup, when
  35. * board setup or driver bugs are most common.
  36. *
  37. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  38. */
  39. #ifdef DEBUG
  40. #define extra_checks 1
  41. #else
  42. #define extra_checks 0
  43. #endif
  44. /* Device and char device-related information */
  45. static DEFINE_IDA(gpio_ida);
  46. static dev_t gpio_devt;
  47. #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
  48. static struct bus_type gpio_bus_type = {
  49. .name = "gpio",
  50. };
  51. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  52. * While any GPIO is requested, its gpio_chip is not removable;
  53. * each GPIO's "requested" flag serves as a lock and refcount.
  54. */
  55. DEFINE_SPINLOCK(gpio_lock);
  56. static DEFINE_MUTEX(gpio_lookup_lock);
  57. static LIST_HEAD(gpio_lookup_list);
  58. LIST_HEAD(gpio_devices);
  59. static void gpiochip_free_hogs(struct gpio_chip *chip);
  60. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
  61. static bool gpiolib_initialized;
  62. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  63. {
  64. d->label = label;
  65. }
  66. /**
  67. * Convert a GPIO number to its descriptor
  68. */
  69. struct gpio_desc *gpio_to_desc(unsigned gpio)
  70. {
  71. struct gpio_device *gdev;
  72. unsigned long flags;
  73. spin_lock_irqsave(&gpio_lock, flags);
  74. list_for_each_entry(gdev, &gpio_devices, list) {
  75. if (gdev->base <= gpio &&
  76. gdev->base + gdev->ngpio > gpio) {
  77. spin_unlock_irqrestore(&gpio_lock, flags);
  78. return &gdev->descs[gpio - gdev->base];
  79. }
  80. }
  81. spin_unlock_irqrestore(&gpio_lock, flags);
  82. if (!gpio_is_valid(gpio))
  83. WARN(1, "invalid GPIO %d\n", gpio);
  84. return NULL;
  85. }
  86. EXPORT_SYMBOL_GPL(gpio_to_desc);
  87. /**
  88. * Get the GPIO descriptor corresponding to the given hw number for this chip.
  89. */
  90. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
  91. u16 hwnum)
  92. {
  93. struct gpio_device *gdev = chip->gpiodev;
  94. if (hwnum >= gdev->ngpio)
  95. return ERR_PTR(-EINVAL);
  96. return &gdev->descs[hwnum];
  97. }
  98. /**
  99. * Convert a GPIO descriptor to the integer namespace.
  100. * This should disappear in the future but is needed since we still
  101. * use GPIO numbers for error messages and sysfs nodes
  102. */
  103. int desc_to_gpio(const struct gpio_desc *desc)
  104. {
  105. return desc->gdev->base + (desc - &desc->gdev->descs[0]);
  106. }
  107. EXPORT_SYMBOL_GPL(desc_to_gpio);
  108. /**
  109. * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
  110. * @desc: descriptor to return the chip of
  111. */
  112. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  113. {
  114. if (!desc || !desc->gdev || !desc->gdev->chip)
  115. return NULL;
  116. return desc->gdev->chip;
  117. }
  118. EXPORT_SYMBOL_GPL(gpiod_to_chip);
  119. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  120. static int gpiochip_find_base(int ngpio)
  121. {
  122. struct gpio_device *gdev;
  123. int base = ARCH_NR_GPIOS - ngpio;
  124. list_for_each_entry_reverse(gdev, &gpio_devices, list) {
  125. /* found a free space? */
  126. if (gdev->base + gdev->ngpio <= base)
  127. break;
  128. else
  129. /* nope, check the space right before the chip */
  130. base = gdev->base - ngpio;
  131. }
  132. if (gpio_is_valid(base)) {
  133. pr_debug("%s: found new base at %d\n", __func__, base);
  134. return base;
  135. } else {
  136. pr_err("%s: cannot find free range\n", __func__);
  137. return -ENOSPC;
  138. }
  139. }
  140. /**
  141. * gpiod_get_direction - return the current direction of a GPIO
  142. * @desc: GPIO to get the direction of
  143. *
  144. * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
  145. *
  146. * This function may sleep if gpiod_cansleep() is true.
  147. */
  148. int gpiod_get_direction(struct gpio_desc *desc)
  149. {
  150. struct gpio_chip *chip;
  151. unsigned offset;
  152. int status = -EINVAL;
  153. chip = gpiod_to_chip(desc);
  154. offset = gpio_chip_hwgpio(desc);
  155. if (!chip->get_direction)
  156. return status;
  157. status = chip->get_direction(chip, offset);
  158. if (status > 0) {
  159. /* GPIOF_DIR_IN, or other positive */
  160. status = 1;
  161. clear_bit(FLAG_IS_OUT, &desc->flags);
  162. }
  163. if (status == 0) {
  164. /* GPIOF_DIR_OUT */
  165. set_bit(FLAG_IS_OUT, &desc->flags);
  166. }
  167. return status;
  168. }
  169. EXPORT_SYMBOL_GPL(gpiod_get_direction);
  170. /*
  171. * Add a new chip to the global chips list, keeping the list of chips sorted
  172. * by range(means [base, base + ngpio - 1]) order.
  173. *
  174. * Return -EBUSY if the new chip overlaps with some other chip's integer
  175. * space.
  176. */
  177. static int gpiodev_add_to_list(struct gpio_device *gdev)
  178. {
  179. struct gpio_device *prev, *next;
  180. if (list_empty(&gpio_devices)) {
  181. /* initial entry in list */
  182. list_add_tail(&gdev->list, &gpio_devices);
  183. return 0;
  184. }
  185. next = list_entry(gpio_devices.next, struct gpio_device, list);
  186. if (gdev->base + gdev->ngpio <= next->base) {
  187. /* add before first entry */
  188. list_add(&gdev->list, &gpio_devices);
  189. return 0;
  190. }
  191. prev = list_entry(gpio_devices.prev, struct gpio_device, list);
  192. if (prev->base + prev->ngpio <= gdev->base) {
  193. /* add behind last entry */
  194. list_add_tail(&gdev->list, &gpio_devices);
  195. return 0;
  196. }
  197. list_for_each_entry_safe(prev, next, &gpio_devices, list) {
  198. /* at the end of the list */
  199. if (&next->list == &gpio_devices)
  200. break;
  201. /* add between prev and next */
  202. if (prev->base + prev->ngpio <= gdev->base
  203. && gdev->base + gdev->ngpio <= next->base) {
  204. list_add(&gdev->list, &prev->list);
  205. return 0;
  206. }
  207. }
  208. dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
  209. return -EBUSY;
  210. }
  211. /**
  212. * Convert a GPIO name to its descriptor
  213. */
  214. static struct gpio_desc *gpio_name_to_desc(const char * const name)
  215. {
  216. struct gpio_device *gdev;
  217. unsigned long flags;
  218. spin_lock_irqsave(&gpio_lock, flags);
  219. list_for_each_entry(gdev, &gpio_devices, list) {
  220. int i;
  221. for (i = 0; i != gdev->ngpio; ++i) {
  222. struct gpio_desc *desc = &gdev->descs[i];
  223. if (!desc->name || !name)
  224. continue;
  225. if (!strcmp(desc->name, name)) {
  226. spin_unlock_irqrestore(&gpio_lock, flags);
  227. return desc;
  228. }
  229. }
  230. }
  231. spin_unlock_irqrestore(&gpio_lock, flags);
  232. return NULL;
  233. }
  234. /*
  235. * Takes the names from gc->names and checks if they are all unique. If they
  236. * are, they are assigned to their gpio descriptors.
  237. *
  238. * Warning if one of the names is already used for a different GPIO.
  239. */
  240. static int gpiochip_set_desc_names(struct gpio_chip *gc)
  241. {
  242. struct gpio_device *gdev = gc->gpiodev;
  243. int i;
  244. if (!gc->names)
  245. return 0;
  246. /* First check all names if they are unique */
  247. for (i = 0; i != gc->ngpio; ++i) {
  248. struct gpio_desc *gpio;
  249. gpio = gpio_name_to_desc(gc->names[i]);
  250. if (gpio)
  251. dev_warn(&gdev->dev,
  252. "Detected name collision for GPIO name '%s'\n",
  253. gc->names[i]);
  254. }
  255. /* Then add all names to the GPIO descriptors */
  256. for (i = 0; i != gc->ngpio; ++i)
  257. gdev->descs[i].name = gc->names[i];
  258. return 0;
  259. }
  260. /**
  261. * gpio_ioctl() - ioctl handler for the GPIO chardev
  262. */
  263. static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  264. {
  265. struct gpio_device *gdev = filp->private_data;
  266. struct gpio_chip *chip = gdev->chip;
  267. int __user *ip = (int __user *)arg;
  268. /* We fail any subsequent ioctl():s when the chip is gone */
  269. if (!chip)
  270. return -ENODEV;
  271. /* Fill in the struct and pass to userspace */
  272. if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
  273. struct gpiochip_info chipinfo;
  274. strncpy(chipinfo.name, dev_name(&gdev->dev),
  275. sizeof(chipinfo.name));
  276. chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
  277. strncpy(chipinfo.label, gdev->label,
  278. sizeof(chipinfo.label));
  279. chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
  280. chipinfo.lines = gdev->ngpio;
  281. if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
  282. return -EFAULT;
  283. return 0;
  284. } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
  285. struct gpioline_info lineinfo;
  286. struct gpio_desc *desc;
  287. if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
  288. return -EFAULT;
  289. if (lineinfo.line_offset > gdev->ngpio)
  290. return -EINVAL;
  291. desc = &gdev->descs[lineinfo.line_offset];
  292. if (desc->name) {
  293. strncpy(lineinfo.name, desc->name,
  294. sizeof(lineinfo.name));
  295. lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
  296. } else {
  297. lineinfo.name[0] = '\0';
  298. }
  299. if (desc->label) {
  300. strncpy(lineinfo.consumer, desc->label,
  301. sizeof(lineinfo.consumer));
  302. lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
  303. } else {
  304. lineinfo.consumer[0] = '\0';
  305. }
  306. /*
  307. * Userspace only need to know that the kernel is using
  308. * this GPIO so it can't use it.
  309. */
  310. lineinfo.flags = 0;
  311. if (test_bit(FLAG_REQUESTED, &desc->flags) ||
  312. test_bit(FLAG_IS_HOGGED, &desc->flags) ||
  313. test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
  314. test_bit(FLAG_EXPORT, &desc->flags) ||
  315. test_bit(FLAG_SYSFS, &desc->flags))
  316. lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
  317. if (test_bit(FLAG_IS_OUT, &desc->flags))
  318. lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
  319. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  320. lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
  321. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  322. lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
  323. if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  324. lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
  325. if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
  326. return -EFAULT;
  327. return 0;
  328. }
  329. return -EINVAL;
  330. }
  331. /**
  332. * gpio_chrdev_open() - open the chardev for ioctl operations
  333. * @inode: inode for this chardev
  334. * @filp: file struct for storing private data
  335. * Returns 0 on success
  336. */
  337. static int gpio_chrdev_open(struct inode *inode, struct file *filp)
  338. {
  339. struct gpio_device *gdev = container_of(inode->i_cdev,
  340. struct gpio_device, chrdev);
  341. /* Fail on open if the backing gpiochip is gone */
  342. if (!gdev || !gdev->chip)
  343. return -ENODEV;
  344. get_device(&gdev->dev);
  345. filp->private_data = gdev;
  346. return 0;
  347. }
  348. /**
  349. * gpio_chrdev_release() - close chardev after ioctl operations
  350. * @inode: inode for this chardev
  351. * @filp: file struct for storing private data
  352. * Returns 0 on success
  353. */
  354. static int gpio_chrdev_release(struct inode *inode, struct file *filp)
  355. {
  356. struct gpio_device *gdev = container_of(inode->i_cdev,
  357. struct gpio_device, chrdev);
  358. if (!gdev)
  359. return -ENODEV;
  360. put_device(&gdev->dev);
  361. return 0;
  362. }
  363. static const struct file_operations gpio_fileops = {
  364. .release = gpio_chrdev_release,
  365. .open = gpio_chrdev_open,
  366. .owner = THIS_MODULE,
  367. .llseek = noop_llseek,
  368. .unlocked_ioctl = gpio_ioctl,
  369. .compat_ioctl = gpio_ioctl,
  370. };
  371. static void gpiodevice_release(struct device *dev)
  372. {
  373. struct gpio_device *gdev = dev_get_drvdata(dev);
  374. cdev_del(&gdev->chrdev);
  375. list_del(&gdev->list);
  376. ida_simple_remove(&gpio_ida, gdev->id);
  377. kfree(gdev->label);
  378. kfree(gdev->descs);
  379. kfree(gdev);
  380. }
  381. static int gpiochip_setup_dev(struct gpio_device *gdev)
  382. {
  383. int status;
  384. cdev_init(&gdev->chrdev, &gpio_fileops);
  385. gdev->chrdev.owner = THIS_MODULE;
  386. gdev->chrdev.kobj.parent = &gdev->dev.kobj;
  387. gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
  388. status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
  389. if (status < 0)
  390. chip_warn(gdev->chip, "failed to add char device %d:%d\n",
  391. MAJOR(gpio_devt), gdev->id);
  392. else
  393. chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
  394. MAJOR(gpio_devt), gdev->id);
  395. status = device_add(&gdev->dev);
  396. if (status)
  397. goto err_remove_chardev;
  398. status = gpiochip_sysfs_register(gdev);
  399. if (status)
  400. goto err_remove_device;
  401. /* From this point, the .release() function cleans up gpio_device */
  402. gdev->dev.release = gpiodevice_release;
  403. get_device(&gdev->dev);
  404. pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
  405. __func__, gdev->base, gdev->base + gdev->ngpio - 1,
  406. dev_name(&gdev->dev), gdev->chip->label ? : "generic");
  407. return 0;
  408. err_remove_device:
  409. device_del(&gdev->dev);
  410. err_remove_chardev:
  411. cdev_del(&gdev->chrdev);
  412. return status;
  413. }
  414. static void gpiochip_setup_devs(void)
  415. {
  416. struct gpio_device *gdev;
  417. int err;
  418. list_for_each_entry(gdev, &gpio_devices, list) {
  419. err = gpiochip_setup_dev(gdev);
  420. if (err)
  421. pr_err("%s: Failed to initialize gpio device (%d)\n",
  422. dev_name(&gdev->dev), err);
  423. }
  424. }
  425. /**
  426. * gpiochip_add_data() - register a gpio_chip
  427. * @chip: the chip to register, with chip->base initialized
  428. * Context: potentially before irqs will work
  429. *
  430. * Returns a negative errno if the chip can't be registered, such as
  431. * because the chip->base is invalid or already associated with a
  432. * different chip. Otherwise it returns zero as a success code.
  433. *
  434. * When gpiochip_add_data() is called very early during boot, so that GPIOs
  435. * can be freely used, the chip->parent device must be registered before
  436. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  437. * for GPIOs will fail rudely.
  438. *
  439. * gpiochip_add_data() must only be called after gpiolib initialization,
  440. * ie after core_initcall().
  441. *
  442. * If chip->base is negative, this requests dynamic assignment of
  443. * a range of valid GPIOs.
  444. */
  445. int gpiochip_add_data(struct gpio_chip *chip, void *data)
  446. {
  447. unsigned long flags;
  448. int status = 0;
  449. unsigned i;
  450. int base = chip->base;
  451. struct gpio_device *gdev;
  452. /*
  453. * First: allocate and populate the internal stat container, and
  454. * set up the struct device.
  455. */
  456. gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  457. if (!gdev)
  458. return -ENOMEM;
  459. gdev->dev.bus = &gpio_bus_type;
  460. gdev->chip = chip;
  461. chip->gpiodev = gdev;
  462. if (chip->parent) {
  463. gdev->dev.parent = chip->parent;
  464. gdev->dev.of_node = chip->parent->of_node;
  465. } else {
  466. #ifdef CONFIG_OF_GPIO
  467. /* If the gpiochip has an assigned OF node this takes precedence */
  468. if (chip->of_node)
  469. gdev->dev.of_node = chip->of_node;
  470. #endif
  471. }
  472. gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
  473. if (gdev->id < 0) {
  474. status = gdev->id;
  475. goto err_free_gdev;
  476. }
  477. dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
  478. device_initialize(&gdev->dev);
  479. dev_set_drvdata(&gdev->dev, gdev);
  480. if (chip->parent && chip->parent->driver)
  481. gdev->owner = chip->parent->driver->owner;
  482. else if (chip->owner)
  483. /* TODO: remove chip->owner */
  484. gdev->owner = chip->owner;
  485. else
  486. gdev->owner = THIS_MODULE;
  487. gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
  488. if (!gdev->descs) {
  489. status = -ENOMEM;
  490. goto err_free_gdev;
  491. }
  492. if (chip->ngpio == 0) {
  493. chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
  494. status = -EINVAL;
  495. goto err_free_descs;
  496. }
  497. if (chip->label)
  498. gdev->label = kstrdup(chip->label, GFP_KERNEL);
  499. else
  500. gdev->label = kstrdup("unknown", GFP_KERNEL);
  501. if (!gdev->label) {
  502. status = -ENOMEM;
  503. goto err_free_descs;
  504. }
  505. gdev->ngpio = chip->ngpio;
  506. gdev->data = data;
  507. spin_lock_irqsave(&gpio_lock, flags);
  508. /*
  509. * TODO: this allocates a Linux GPIO number base in the global
  510. * GPIO numberspace for this chip. In the long run we want to
  511. * get *rid* of this numberspace and use only descriptors, but
  512. * it may be a pipe dream. It will not happen before we get rid
  513. * of the sysfs interface anyways.
  514. */
  515. if (base < 0) {
  516. base = gpiochip_find_base(chip->ngpio);
  517. if (base < 0) {
  518. status = base;
  519. spin_unlock_irqrestore(&gpio_lock, flags);
  520. goto err_free_label;
  521. }
  522. /*
  523. * TODO: it should not be necessary to reflect the assigned
  524. * base outside of the GPIO subsystem. Go over drivers and
  525. * see if anyone makes use of this, else drop this and assign
  526. * a poison instead.
  527. */
  528. chip->base = base;
  529. }
  530. gdev->base = base;
  531. status = gpiodev_add_to_list(gdev);
  532. if (status) {
  533. spin_unlock_irqrestore(&gpio_lock, flags);
  534. goto err_free_label;
  535. }
  536. for (i = 0; i < chip->ngpio; i++) {
  537. struct gpio_desc *desc = &gdev->descs[i];
  538. desc->gdev = gdev;
  539. /* REVISIT: most hardware initializes GPIOs as inputs (often
  540. * with pullups enabled) so power usage is minimized. Linux
  541. * code should set the gpio direction first thing; but until
  542. * it does, and in case chip->get_direction is not set, we may
  543. * expose the wrong direction in sysfs.
  544. */
  545. desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
  546. }
  547. spin_unlock_irqrestore(&gpio_lock, flags);
  548. #ifdef CONFIG_PINCTRL
  549. INIT_LIST_HEAD(&gdev->pin_ranges);
  550. #endif
  551. status = gpiochip_set_desc_names(chip);
  552. if (status)
  553. goto err_remove_from_list;
  554. status = of_gpiochip_add(chip);
  555. if (status)
  556. goto err_remove_chip;
  557. acpi_gpiochip_add(chip);
  558. /*
  559. * By first adding the chardev, and then adding the device,
  560. * we get a device node entry in sysfs under
  561. * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
  562. * coldplug of device nodes and other udev business.
  563. * We can do this only if gpiolib has been initialized.
  564. * Otherwise, defer until later.
  565. */
  566. if (gpiolib_initialized) {
  567. status = gpiochip_setup_dev(gdev);
  568. if (status)
  569. goto err_remove_chip;
  570. }
  571. return 0;
  572. err_remove_chip:
  573. acpi_gpiochip_remove(chip);
  574. gpiochip_free_hogs(chip);
  575. of_gpiochip_remove(chip);
  576. err_remove_from_list:
  577. spin_lock_irqsave(&gpio_lock, flags);
  578. list_del(&gdev->list);
  579. spin_unlock_irqrestore(&gpio_lock, flags);
  580. err_free_label:
  581. kfree(gdev->label);
  582. err_free_descs:
  583. kfree(gdev->descs);
  584. err_free_gdev:
  585. ida_simple_remove(&gpio_ida, gdev->id);
  586. /* failures here can mean systems won't boot... */
  587. pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
  588. gdev->base, gdev->base + gdev->ngpio - 1,
  589. chip->label ? : "generic");
  590. kfree(gdev);
  591. return status;
  592. }
  593. EXPORT_SYMBOL_GPL(gpiochip_add_data);
  594. /**
  595. * gpiochip_get_data() - get per-subdriver data for the chip
  596. */
  597. void *gpiochip_get_data(struct gpio_chip *chip)
  598. {
  599. return chip->gpiodev->data;
  600. }
  601. EXPORT_SYMBOL_GPL(gpiochip_get_data);
  602. /**
  603. * gpiochip_remove() - unregister a gpio_chip
  604. * @chip: the chip to unregister
  605. *
  606. * A gpio_chip with any GPIOs still requested may not be removed.
  607. */
  608. void gpiochip_remove(struct gpio_chip *chip)
  609. {
  610. struct gpio_device *gdev = chip->gpiodev;
  611. struct gpio_desc *desc;
  612. unsigned long flags;
  613. unsigned i;
  614. bool requested = false;
  615. /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
  616. gpiochip_sysfs_unregister(gdev);
  617. /* Numb the device, cancelling all outstanding operations */
  618. gdev->chip = NULL;
  619. gpiochip_irqchip_remove(chip);
  620. acpi_gpiochip_remove(chip);
  621. gpiochip_remove_pin_ranges(chip);
  622. gpiochip_free_hogs(chip);
  623. of_gpiochip_remove(chip);
  624. /*
  625. * We accept no more calls into the driver from this point, so
  626. * NULL the driver data pointer
  627. */
  628. gdev->data = NULL;
  629. spin_lock_irqsave(&gpio_lock, flags);
  630. for (i = 0; i < gdev->ngpio; i++) {
  631. desc = &gdev->descs[i];
  632. if (test_bit(FLAG_REQUESTED, &desc->flags))
  633. requested = true;
  634. }
  635. spin_unlock_irqrestore(&gpio_lock, flags);
  636. if (requested)
  637. dev_crit(&gdev->dev,
  638. "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
  639. /*
  640. * The gpiochip side puts its use of the device to rest here:
  641. * if there are no userspace clients, the chardev and device will
  642. * be removed, else it will be dangling until the last user is
  643. * gone.
  644. */
  645. put_device(&gdev->dev);
  646. }
  647. EXPORT_SYMBOL_GPL(gpiochip_remove);
  648. static void devm_gpio_chip_release(struct device *dev, void *res)
  649. {
  650. struct gpio_chip *chip = *(struct gpio_chip **)res;
  651. gpiochip_remove(chip);
  652. }
  653. static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
  654. {
  655. struct gpio_chip **r = res;
  656. if (!r || !*r) {
  657. WARN_ON(!r || !*r);
  658. return 0;
  659. }
  660. return *r == data;
  661. }
  662. /**
  663. * devm_gpiochip_add_data() - Resource manager piochip_add_data()
  664. * @dev: the device pointer on which irq_chip belongs to.
  665. * @chip: the chip to register, with chip->base initialized
  666. * Context: potentially before irqs will work
  667. *
  668. * Returns a negative errno if the chip can't be registered, such as
  669. * because the chip->base is invalid or already associated with a
  670. * different chip. Otherwise it returns zero as a success code.
  671. *
  672. * The gpio chip automatically be released when the device is unbound.
  673. */
  674. int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
  675. void *data)
  676. {
  677. struct gpio_chip **ptr;
  678. int ret;
  679. ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
  680. GFP_KERNEL);
  681. if (!ptr)
  682. return -ENOMEM;
  683. ret = gpiochip_add_data(chip, data);
  684. if (ret < 0) {
  685. devres_free(ptr);
  686. return ret;
  687. }
  688. *ptr = chip;
  689. devres_add(dev, ptr);
  690. return 0;
  691. }
  692. EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
  693. /**
  694. * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
  695. * @dev: device for which which resource was allocated
  696. * @chip: the chip to remove
  697. *
  698. * A gpio_chip with any GPIOs still requested may not be removed.
  699. */
  700. void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
  701. {
  702. int ret;
  703. ret = devres_release(dev, devm_gpio_chip_release,
  704. devm_gpio_chip_match, chip);
  705. if (!ret)
  706. WARN_ON(ret);
  707. }
  708. EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
  709. /**
  710. * gpiochip_find() - iterator for locating a specific gpio_chip
  711. * @data: data to pass to match function
  712. * @callback: Callback function to check gpio_chip
  713. *
  714. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  715. * determined by a user supplied @match callback. The callback should return
  716. * 0 if the device doesn't match and non-zero if it does. If the callback is
  717. * non-zero, this function will return to the caller and not iterate over any
  718. * more gpio_chips.
  719. */
  720. struct gpio_chip *gpiochip_find(void *data,
  721. int (*match)(struct gpio_chip *chip,
  722. void *data))
  723. {
  724. struct gpio_device *gdev;
  725. struct gpio_chip *chip;
  726. unsigned long flags;
  727. spin_lock_irqsave(&gpio_lock, flags);
  728. list_for_each_entry(gdev, &gpio_devices, list)
  729. if (match(gdev->chip, data))
  730. break;
  731. /* No match? */
  732. if (&gdev->list == &gpio_devices)
  733. chip = NULL;
  734. else
  735. chip = gdev->chip;
  736. spin_unlock_irqrestore(&gpio_lock, flags);
  737. return chip;
  738. }
  739. EXPORT_SYMBOL_GPL(gpiochip_find);
  740. static int gpiochip_match_name(struct gpio_chip *chip, void *data)
  741. {
  742. const char *name = data;
  743. return !strcmp(chip->label, name);
  744. }
  745. static struct gpio_chip *find_chip_by_name(const char *name)
  746. {
  747. return gpiochip_find((void *)name, gpiochip_match_name);
  748. }
  749. #ifdef CONFIG_GPIOLIB_IRQCHIP
  750. /*
  751. * The following is irqchip helper code for gpiochips.
  752. */
  753. /**
  754. * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
  755. * @gpiochip: the gpiochip to set the irqchip chain to
  756. * @irqchip: the irqchip to chain to the gpiochip
  757. * @parent_irq: the irq number corresponding to the parent IRQ for this
  758. * chained irqchip
  759. * @parent_handler: the parent interrupt handler for the accumulated IRQ
  760. * coming out of the gpiochip. If the interrupt is nested rather than
  761. * cascaded, pass NULL in this handler argument
  762. */
  763. void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
  764. struct irq_chip *irqchip,
  765. int parent_irq,
  766. irq_flow_handler_t parent_handler)
  767. {
  768. unsigned int offset;
  769. if (!gpiochip->irqdomain) {
  770. chip_err(gpiochip, "called %s before setting up irqchip\n",
  771. __func__);
  772. return;
  773. }
  774. if (parent_handler) {
  775. if (gpiochip->can_sleep) {
  776. chip_err(gpiochip,
  777. "you cannot have chained interrupts on a "
  778. "chip that may sleep\n");
  779. return;
  780. }
  781. /*
  782. * The parent irqchip is already using the chip_data for this
  783. * irqchip, so our callbacks simply use the handler_data.
  784. */
  785. irq_set_chained_handler_and_data(parent_irq, parent_handler,
  786. gpiochip);
  787. gpiochip->irq_parent = parent_irq;
  788. }
  789. /* Set the parent IRQ for all affected IRQs */
  790. for (offset = 0; offset < gpiochip->ngpio; offset++)
  791. irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
  792. parent_irq);
  793. }
  794. EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
  795. /**
  796. * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
  797. * @d: the irqdomain used by this irqchip
  798. * @irq: the global irq number used by this GPIO irqchip irq
  799. * @hwirq: the local IRQ/GPIO line offset on this gpiochip
  800. *
  801. * This function will set up the mapping for a certain IRQ line on a
  802. * gpiochip by assigning the gpiochip as chip data, and using the irqchip
  803. * stored inside the gpiochip.
  804. */
  805. static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  806. irq_hw_number_t hwirq)
  807. {
  808. struct gpio_chip *chip = d->host_data;
  809. irq_set_chip_data(irq, chip);
  810. /*
  811. * This lock class tells lockdep that GPIO irqs are in a different
  812. * category than their parents, so it won't report false recursion.
  813. */
  814. irq_set_lockdep_class(irq, chip->lock_key);
  815. irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
  816. /* Chips that can sleep need nested thread handlers */
  817. if (chip->can_sleep && !chip->irq_not_threaded)
  818. irq_set_nested_thread(irq, 1);
  819. irq_set_noprobe(irq);
  820. /*
  821. * No set-up of the hardware will happen if IRQ_TYPE_NONE
  822. * is passed as default type.
  823. */
  824. if (chip->irq_default_type != IRQ_TYPE_NONE)
  825. irq_set_irq_type(irq, chip->irq_default_type);
  826. return 0;
  827. }
  828. static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
  829. {
  830. struct gpio_chip *chip = d->host_data;
  831. if (chip->can_sleep)
  832. irq_set_nested_thread(irq, 0);
  833. irq_set_chip_and_handler(irq, NULL, NULL);
  834. irq_set_chip_data(irq, NULL);
  835. }
  836. static const struct irq_domain_ops gpiochip_domain_ops = {
  837. .map = gpiochip_irq_map,
  838. .unmap = gpiochip_irq_unmap,
  839. /* Virtually all GPIO irqchips are twocell:ed */
  840. .xlate = irq_domain_xlate_twocell,
  841. };
  842. static int gpiochip_irq_reqres(struct irq_data *d)
  843. {
  844. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  845. if (!try_module_get(chip->gpiodev->owner))
  846. return -ENODEV;
  847. if (gpiochip_lock_as_irq(chip, d->hwirq)) {
  848. chip_err(chip,
  849. "unable to lock HW IRQ %lu for IRQ\n",
  850. d->hwirq);
  851. module_put(chip->gpiodev->owner);
  852. return -EINVAL;
  853. }
  854. return 0;
  855. }
  856. static void gpiochip_irq_relres(struct irq_data *d)
  857. {
  858. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  859. gpiochip_unlock_as_irq(chip, d->hwirq);
  860. module_put(chip->gpiodev->owner);
  861. }
  862. static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
  863. {
  864. return irq_find_mapping(chip->irqdomain, offset);
  865. }
  866. /**
  867. * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
  868. * @gpiochip: the gpiochip to remove the irqchip from
  869. *
  870. * This is called only from gpiochip_remove()
  871. */
  872. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
  873. {
  874. unsigned int offset;
  875. acpi_gpiochip_free_interrupts(gpiochip);
  876. if (gpiochip->irq_parent) {
  877. irq_set_chained_handler(gpiochip->irq_parent, NULL);
  878. irq_set_handler_data(gpiochip->irq_parent, NULL);
  879. }
  880. /* Remove all IRQ mappings and delete the domain */
  881. if (gpiochip->irqdomain) {
  882. for (offset = 0; offset < gpiochip->ngpio; offset++)
  883. irq_dispose_mapping(
  884. irq_find_mapping(gpiochip->irqdomain, offset));
  885. irq_domain_remove(gpiochip->irqdomain);
  886. }
  887. if (gpiochip->irqchip) {
  888. gpiochip->irqchip->irq_request_resources = NULL;
  889. gpiochip->irqchip->irq_release_resources = NULL;
  890. gpiochip->irqchip = NULL;
  891. }
  892. }
  893. /**
  894. * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
  895. * @gpiochip: the gpiochip to add the irqchip to
  896. * @irqchip: the irqchip to add to the gpiochip
  897. * @first_irq: if not dynamically assigned, the base (first) IRQ to
  898. * allocate gpiochip irqs from
  899. * @handler: the irq handler to use (often a predefined irq core function)
  900. * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
  901. * to have the core avoid setting up any default type in the hardware.
  902. * @lock_key: lockdep class
  903. *
  904. * This function closely associates a certain irqchip with a certain
  905. * gpiochip, providing an irq domain to translate the local IRQs to
  906. * global irqs in the gpiolib core, and making sure that the gpiochip
  907. * is passed as chip data to all related functions. Driver callbacks
  908. * need to use gpiochip_get_data() to get their local state containers back
  909. * from the gpiochip passed as chip data. An irqdomain will be stored
  910. * in the gpiochip that shall be used by the driver to handle IRQ number
  911. * translation. The gpiochip will need to be initialized and registered
  912. * before calling this function.
  913. *
  914. * This function will handle two cell:ed simple IRQs and assumes all
  915. * the pins on the gpiochip can generate a unique IRQ. Everything else
  916. * need to be open coded.
  917. */
  918. int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
  919. struct irq_chip *irqchip,
  920. unsigned int first_irq,
  921. irq_flow_handler_t handler,
  922. unsigned int type,
  923. struct lock_class_key *lock_key)
  924. {
  925. struct device_node *of_node;
  926. unsigned int offset;
  927. unsigned irq_base = 0;
  928. if (!gpiochip || !irqchip)
  929. return -EINVAL;
  930. if (!gpiochip->parent) {
  931. pr_err("missing gpiochip .dev parent pointer\n");
  932. return -EINVAL;
  933. }
  934. of_node = gpiochip->parent->of_node;
  935. #ifdef CONFIG_OF_GPIO
  936. /*
  937. * If the gpiochip has an assigned OF node this takes precedence
  938. * FIXME: get rid of this and use gpiochip->parent->of_node
  939. * everywhere
  940. */
  941. if (gpiochip->of_node)
  942. of_node = gpiochip->of_node;
  943. #endif
  944. gpiochip->irqchip = irqchip;
  945. gpiochip->irq_handler = handler;
  946. gpiochip->irq_default_type = type;
  947. gpiochip->to_irq = gpiochip_to_irq;
  948. gpiochip->lock_key = lock_key;
  949. gpiochip->irqdomain = irq_domain_add_simple(of_node,
  950. gpiochip->ngpio, first_irq,
  951. &gpiochip_domain_ops, gpiochip);
  952. if (!gpiochip->irqdomain) {
  953. gpiochip->irqchip = NULL;
  954. return -EINVAL;
  955. }
  956. /*
  957. * It is possible for a driver to override this, but only if the
  958. * alternative functions are both implemented.
  959. */
  960. if (!irqchip->irq_request_resources &&
  961. !irqchip->irq_release_resources) {
  962. irqchip->irq_request_resources = gpiochip_irq_reqres;
  963. irqchip->irq_release_resources = gpiochip_irq_relres;
  964. }
  965. /*
  966. * Prepare the mapping since the irqchip shall be orthogonal to
  967. * any gpiochip calls. If the first_irq was zero, this is
  968. * necessary to allocate descriptors for all IRQs.
  969. */
  970. for (offset = 0; offset < gpiochip->ngpio; offset++) {
  971. irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
  972. if (offset == 0)
  973. /*
  974. * Store the base into the gpiochip to be used when
  975. * unmapping the irqs.
  976. */
  977. gpiochip->irq_base = irq_base;
  978. }
  979. acpi_gpiochip_request_interrupts(gpiochip);
  980. return 0;
  981. }
  982. EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
  983. #else /* CONFIG_GPIOLIB_IRQCHIP */
  984. static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
  985. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  986. /**
  987. * gpiochip_generic_request() - request the gpio function for a pin
  988. * @chip: the gpiochip owning the GPIO
  989. * @offset: the offset of the GPIO to request for GPIO function
  990. */
  991. int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
  992. {
  993. return pinctrl_request_gpio(chip->gpiodev->base + offset);
  994. }
  995. EXPORT_SYMBOL_GPL(gpiochip_generic_request);
  996. /**
  997. * gpiochip_generic_free() - free the gpio function from a pin
  998. * @chip: the gpiochip to request the gpio function for
  999. * @offset: the offset of the GPIO to free from GPIO function
  1000. */
  1001. void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
  1002. {
  1003. pinctrl_free_gpio(chip->gpiodev->base + offset);
  1004. }
  1005. EXPORT_SYMBOL_GPL(gpiochip_generic_free);
  1006. #ifdef CONFIG_PINCTRL
  1007. /**
  1008. * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
  1009. * @chip: the gpiochip to add the range for
  1010. * @pctldev: the pin controller to map to
  1011. * @gpio_offset: the start offset in the current gpio_chip number space
  1012. * @pin_group: name of the pin group inside the pin controller
  1013. */
  1014. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  1015. struct pinctrl_dev *pctldev,
  1016. unsigned int gpio_offset, const char *pin_group)
  1017. {
  1018. struct gpio_pin_range *pin_range;
  1019. struct gpio_device *gdev = chip->gpiodev;
  1020. int ret;
  1021. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1022. if (!pin_range) {
  1023. chip_err(chip, "failed to allocate pin ranges\n");
  1024. return -ENOMEM;
  1025. }
  1026. /* Use local offset as range ID */
  1027. pin_range->range.id = gpio_offset;
  1028. pin_range->range.gc = chip;
  1029. pin_range->range.name = chip->label;
  1030. pin_range->range.base = gdev->base + gpio_offset;
  1031. pin_range->pctldev = pctldev;
  1032. ret = pinctrl_get_group_pins(pctldev, pin_group,
  1033. &pin_range->range.pins,
  1034. &pin_range->range.npins);
  1035. if (ret < 0) {
  1036. kfree(pin_range);
  1037. return ret;
  1038. }
  1039. pinctrl_add_gpio_range(pctldev, &pin_range->range);
  1040. chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
  1041. gpio_offset, gpio_offset + pin_range->range.npins - 1,
  1042. pinctrl_dev_get_devname(pctldev), pin_group);
  1043. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1044. return 0;
  1045. }
  1046. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  1047. /**
  1048. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  1049. * @chip: the gpiochip to add the range for
  1050. * @pinctrl_name: the dev_name() of the pin controller to map to
  1051. * @gpio_offset: the start offset in the current gpio_chip number space
  1052. * @pin_offset: the start offset in the pin controller number space
  1053. * @npins: the number of pins from the offset of each pin space (GPIO and
  1054. * pin controller) to accumulate in this range
  1055. */
  1056. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  1057. unsigned int gpio_offset, unsigned int pin_offset,
  1058. unsigned int npins)
  1059. {
  1060. struct gpio_pin_range *pin_range;
  1061. struct gpio_device *gdev = chip->gpiodev;
  1062. int ret;
  1063. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1064. if (!pin_range) {
  1065. chip_err(chip, "failed to allocate pin ranges\n");
  1066. return -ENOMEM;
  1067. }
  1068. /* Use local offset as range ID */
  1069. pin_range->range.id = gpio_offset;
  1070. pin_range->range.gc = chip;
  1071. pin_range->range.name = chip->label;
  1072. pin_range->range.base = gdev->base + gpio_offset;
  1073. pin_range->range.pin_base = pin_offset;
  1074. pin_range->range.npins = npins;
  1075. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1076. &pin_range->range);
  1077. if (IS_ERR(pin_range->pctldev)) {
  1078. ret = PTR_ERR(pin_range->pctldev);
  1079. chip_err(chip, "could not create pin range\n");
  1080. kfree(pin_range);
  1081. return ret;
  1082. }
  1083. chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1084. gpio_offset, gpio_offset + npins - 1,
  1085. pinctl_name,
  1086. pin_offset, pin_offset + npins - 1);
  1087. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1088. return 0;
  1089. }
  1090. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1091. /**
  1092. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1093. * @chip: the chip to remove all the mappings for
  1094. */
  1095. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  1096. {
  1097. struct gpio_pin_range *pin_range, *tmp;
  1098. struct gpio_device *gdev = chip->gpiodev;
  1099. list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
  1100. list_del(&pin_range->node);
  1101. pinctrl_remove_gpio_range(pin_range->pctldev,
  1102. &pin_range->range);
  1103. kfree(pin_range);
  1104. }
  1105. }
  1106. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1107. #endif /* CONFIG_PINCTRL */
  1108. /* These "optional" allocation calls help prevent drivers from stomping
  1109. * on each other, and help provide better diagnostics in debugfs.
  1110. * They're called even less than the "set direction" calls.
  1111. */
  1112. static int __gpiod_request(struct gpio_desc *desc, const char *label)
  1113. {
  1114. struct gpio_chip *chip = desc->gdev->chip;
  1115. int status;
  1116. unsigned long flags;
  1117. spin_lock_irqsave(&gpio_lock, flags);
  1118. /* NOTE: gpio_request() can be called in early boot,
  1119. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1120. */
  1121. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1122. desc_set_label(desc, label ? : "?");
  1123. status = 0;
  1124. } else {
  1125. status = -EBUSY;
  1126. goto done;
  1127. }
  1128. if (chip->request) {
  1129. /* chip->request may sleep */
  1130. spin_unlock_irqrestore(&gpio_lock, flags);
  1131. status = chip->request(chip, gpio_chip_hwgpio(desc));
  1132. spin_lock_irqsave(&gpio_lock, flags);
  1133. if (status < 0) {
  1134. desc_set_label(desc, NULL);
  1135. clear_bit(FLAG_REQUESTED, &desc->flags);
  1136. goto done;
  1137. }
  1138. }
  1139. if (chip->get_direction) {
  1140. /* chip->get_direction may sleep */
  1141. spin_unlock_irqrestore(&gpio_lock, flags);
  1142. gpiod_get_direction(desc);
  1143. spin_lock_irqsave(&gpio_lock, flags);
  1144. }
  1145. done:
  1146. if (status < 0) {
  1147. /* Clear flags that might have been set by the caller before
  1148. * requesting the GPIO.
  1149. */
  1150. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1151. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1152. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1153. }
  1154. spin_unlock_irqrestore(&gpio_lock, flags);
  1155. return status;
  1156. }
  1157. /*
  1158. * This descriptor validation needs to be inserted verbatim into each
  1159. * function taking a descriptor, so we need to use a preprocessor
  1160. * macro to avoid endless duplication.
  1161. */
  1162. #define VALIDATE_DESC(desc) do { \
  1163. if (!desc || !desc->gdev) { \
  1164. pr_warn("%s: invalid GPIO\n", __func__); \
  1165. return -EINVAL; \
  1166. } \
  1167. if ( !desc->gdev->chip ) { \
  1168. dev_warn(&desc->gdev->dev, \
  1169. "%s: backing chip is gone\n", __func__); \
  1170. return 0; \
  1171. } } while (0)
  1172. #define VALIDATE_DESC_VOID(desc) do { \
  1173. if (!desc || !desc->gdev) { \
  1174. pr_warn("%s: invalid GPIO\n", __func__); \
  1175. return; \
  1176. } \
  1177. if (!desc->gdev->chip) { \
  1178. dev_warn(&desc->gdev->dev, \
  1179. "%s: backing chip is gone\n", __func__); \
  1180. return; \
  1181. } } while (0)
  1182. int gpiod_request(struct gpio_desc *desc, const char *label)
  1183. {
  1184. int status = -EPROBE_DEFER;
  1185. struct gpio_device *gdev;
  1186. VALIDATE_DESC(desc);
  1187. gdev = desc->gdev;
  1188. if (try_module_get(gdev->owner)) {
  1189. status = __gpiod_request(desc, label);
  1190. if (status < 0)
  1191. module_put(gdev->owner);
  1192. else
  1193. get_device(&gdev->dev);
  1194. }
  1195. if (status)
  1196. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  1197. return status;
  1198. }
  1199. static bool __gpiod_free(struct gpio_desc *desc)
  1200. {
  1201. bool ret = false;
  1202. unsigned long flags;
  1203. struct gpio_chip *chip;
  1204. might_sleep();
  1205. gpiod_unexport(desc);
  1206. spin_lock_irqsave(&gpio_lock, flags);
  1207. chip = desc->gdev->chip;
  1208. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1209. if (chip->free) {
  1210. spin_unlock_irqrestore(&gpio_lock, flags);
  1211. might_sleep_if(chip->can_sleep);
  1212. chip->free(chip, gpio_chip_hwgpio(desc));
  1213. spin_lock_irqsave(&gpio_lock, flags);
  1214. }
  1215. desc_set_label(desc, NULL);
  1216. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1217. clear_bit(FLAG_REQUESTED, &desc->flags);
  1218. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1219. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1220. clear_bit(FLAG_IS_HOGGED, &desc->flags);
  1221. ret = true;
  1222. }
  1223. spin_unlock_irqrestore(&gpio_lock, flags);
  1224. return ret;
  1225. }
  1226. void gpiod_free(struct gpio_desc *desc)
  1227. {
  1228. if (desc && desc->gdev && __gpiod_free(desc)) {
  1229. module_put(desc->gdev->owner);
  1230. put_device(&desc->gdev->dev);
  1231. } else {
  1232. WARN_ON(extra_checks);
  1233. }
  1234. }
  1235. /**
  1236. * gpiochip_is_requested - return string iff signal was requested
  1237. * @chip: controller managing the signal
  1238. * @offset: of signal within controller's 0..(ngpio - 1) range
  1239. *
  1240. * Returns NULL if the GPIO is not currently requested, else a string.
  1241. * The string returned is the label passed to gpio_request(); if none has been
  1242. * passed it is a meaningless, non-NULL constant.
  1243. *
  1244. * This function is for use by GPIO controller drivers. The label can
  1245. * help with diagnostics, and knowing that the signal is used as a GPIO
  1246. * can help avoid accidentally multiplexing it to another controller.
  1247. */
  1248. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1249. {
  1250. struct gpio_desc *desc;
  1251. if (offset >= chip->ngpio)
  1252. return NULL;
  1253. desc = &chip->gpiodev->descs[offset];
  1254. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  1255. return NULL;
  1256. return desc->label;
  1257. }
  1258. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1259. /**
  1260. * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
  1261. * @desc: GPIO descriptor to request
  1262. * @label: label for the GPIO
  1263. *
  1264. * Function allows GPIO chip drivers to request and use their own GPIO
  1265. * descriptors via gpiolib API. Difference to gpiod_request() is that this
  1266. * function will not increase reference count of the GPIO chip module. This
  1267. * allows the GPIO chip module to be unloaded as needed (we assume that the
  1268. * GPIO chip driver handles freeing the GPIOs it has requested).
  1269. */
  1270. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
  1271. const char *label)
  1272. {
  1273. struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
  1274. int err;
  1275. if (IS_ERR(desc)) {
  1276. chip_err(chip, "failed to get GPIO descriptor\n");
  1277. return desc;
  1278. }
  1279. err = __gpiod_request(desc, label);
  1280. if (err < 0)
  1281. return ERR_PTR(err);
  1282. return desc;
  1283. }
  1284. EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
  1285. /**
  1286. * gpiochip_free_own_desc - Free GPIO requested by the chip driver
  1287. * @desc: GPIO descriptor to free
  1288. *
  1289. * Function frees the given GPIO requested previously with
  1290. * gpiochip_request_own_desc().
  1291. */
  1292. void gpiochip_free_own_desc(struct gpio_desc *desc)
  1293. {
  1294. if (desc)
  1295. __gpiod_free(desc);
  1296. }
  1297. EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
  1298. /*
  1299. * Drivers MUST set GPIO direction before making get/set calls. In
  1300. * some cases this is done in early boot, before IRQs are enabled.
  1301. *
  1302. * As a rule these aren't called more than once (except for drivers
  1303. * using the open-drain emulation idiom) so these are natural places
  1304. * to accumulate extra debugging checks. Note that we can't (yet)
  1305. * rely on gpio_request() having been called beforehand.
  1306. */
  1307. /**
  1308. * gpiod_direction_input - set the GPIO direction to input
  1309. * @desc: GPIO to set to input
  1310. *
  1311. * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
  1312. * be called safely on it.
  1313. *
  1314. * Return 0 in case of success, else an error code.
  1315. */
  1316. int gpiod_direction_input(struct gpio_desc *desc)
  1317. {
  1318. struct gpio_chip *chip;
  1319. int status = -EINVAL;
  1320. VALIDATE_DESC(desc);
  1321. chip = desc->gdev->chip;
  1322. if (!chip->get || !chip->direction_input) {
  1323. gpiod_warn(desc,
  1324. "%s: missing get() or direction_input() operations\n",
  1325. __func__);
  1326. return -EIO;
  1327. }
  1328. status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
  1329. if (status == 0)
  1330. clear_bit(FLAG_IS_OUT, &desc->flags);
  1331. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  1332. return status;
  1333. }
  1334. EXPORT_SYMBOL_GPL(gpiod_direction_input);
  1335. static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  1336. {
  1337. struct gpio_chip *chip;
  1338. int status = -EINVAL;
  1339. /* GPIOs used for IRQs shall not be set as output */
  1340. if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
  1341. gpiod_err(desc,
  1342. "%s: tried to set a GPIO tied to an IRQ as output\n",
  1343. __func__);
  1344. return -EIO;
  1345. }
  1346. /* Open drain pin should not be driven to 1 */
  1347. if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1348. return gpiod_direction_input(desc);
  1349. /* Open source pin should not be driven to 0 */
  1350. if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1351. return gpiod_direction_input(desc);
  1352. chip = desc->gdev->chip;
  1353. if (!chip->set || !chip->direction_output) {
  1354. gpiod_warn(desc,
  1355. "%s: missing set() or direction_output() operations\n",
  1356. __func__);
  1357. return -EIO;
  1358. }
  1359. status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
  1360. if (status == 0)
  1361. set_bit(FLAG_IS_OUT, &desc->flags);
  1362. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1363. trace_gpio_direction(desc_to_gpio(desc), 0, status);
  1364. return status;
  1365. }
  1366. /**
  1367. * gpiod_direction_output_raw - set the GPIO direction to output
  1368. * @desc: GPIO to set to output
  1369. * @value: initial output value of the GPIO
  1370. *
  1371. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  1372. * be called safely on it. The initial value of the output must be specified
  1373. * as raw value on the physical line without regard for the ACTIVE_LOW status.
  1374. *
  1375. * Return 0 in case of success, else an error code.
  1376. */
  1377. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  1378. {
  1379. VALIDATE_DESC(desc);
  1380. return _gpiod_direction_output_raw(desc, value);
  1381. }
  1382. EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
  1383. /**
  1384. * gpiod_direction_output - set the GPIO direction to output
  1385. * @desc: GPIO to set to output
  1386. * @value: initial output value of the GPIO
  1387. *
  1388. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  1389. * be called safely on it. The initial value of the output must be specified
  1390. * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1391. * account.
  1392. *
  1393. * Return 0 in case of success, else an error code.
  1394. */
  1395. int gpiod_direction_output(struct gpio_desc *desc, int value)
  1396. {
  1397. VALIDATE_DESC(desc);
  1398. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1399. value = !value;
  1400. return _gpiod_direction_output_raw(desc, value);
  1401. }
  1402. EXPORT_SYMBOL_GPL(gpiod_direction_output);
  1403. /**
  1404. * gpiod_set_debounce - sets @debounce time for a @gpio
  1405. * @gpio: the gpio to set debounce time
  1406. * @debounce: debounce time is microseconds
  1407. *
  1408. * returns -ENOTSUPP if the controller does not support setting
  1409. * debounce.
  1410. */
  1411. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  1412. {
  1413. struct gpio_chip *chip;
  1414. VALIDATE_DESC(desc);
  1415. chip = desc->gdev->chip;
  1416. if (!chip->set || !chip->set_debounce) {
  1417. gpiod_dbg(desc,
  1418. "%s: missing set() or set_debounce() operations\n",
  1419. __func__);
  1420. return -ENOTSUPP;
  1421. }
  1422. return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
  1423. }
  1424. EXPORT_SYMBOL_GPL(gpiod_set_debounce);
  1425. /**
  1426. * gpiod_is_active_low - test whether a GPIO is active-low or not
  1427. * @desc: the gpio descriptor to test
  1428. *
  1429. * Returns 1 if the GPIO is active-low, 0 otherwise.
  1430. */
  1431. int gpiod_is_active_low(const struct gpio_desc *desc)
  1432. {
  1433. VALIDATE_DESC(desc);
  1434. return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1435. }
  1436. EXPORT_SYMBOL_GPL(gpiod_is_active_low);
  1437. /* I/O calls are only valid after configuration completed; the relevant
  1438. * "is this a valid GPIO" error checks should already have been done.
  1439. *
  1440. * "Get" operations are often inlinable as reading a pin value register,
  1441. * and masking the relevant bit in that register.
  1442. *
  1443. * When "set" operations are inlinable, they involve writing that mask to
  1444. * one register to set a low value, or a different register to set it high.
  1445. * Otherwise locking is needed, so there may be little value to inlining.
  1446. *
  1447. *------------------------------------------------------------------------
  1448. *
  1449. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1450. * have requested the GPIO. That can include implicit requesting by
  1451. * a direction setting call. Marking a gpio as requested locks its chip
  1452. * in memory, guaranteeing that these table lookups need no more locking
  1453. * and that gpiochip_remove() will fail.
  1454. *
  1455. * REVISIT when debugging, consider adding some instrumentation to ensure
  1456. * that the GPIO was actually requested.
  1457. */
  1458. static int _gpiod_get_raw_value(const struct gpio_desc *desc)
  1459. {
  1460. struct gpio_chip *chip;
  1461. int offset;
  1462. int value;
  1463. chip = desc->gdev->chip;
  1464. offset = gpio_chip_hwgpio(desc);
  1465. value = chip->get ? chip->get(chip, offset) : -EIO;
  1466. value = value < 0 ? value : !!value;
  1467. trace_gpio_value(desc_to_gpio(desc), 1, value);
  1468. return value;
  1469. }
  1470. /**
  1471. * gpiod_get_raw_value() - return a gpio's raw value
  1472. * @desc: gpio whose value will be returned
  1473. *
  1474. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  1475. * its ACTIVE_LOW status, or negative errno on failure.
  1476. *
  1477. * This function should be called from contexts where we cannot sleep, and will
  1478. * complain if the GPIO chip functions potentially sleep.
  1479. */
  1480. int gpiod_get_raw_value(const struct gpio_desc *desc)
  1481. {
  1482. VALIDATE_DESC(desc);
  1483. /* Should be using gpio_get_value_cansleep() */
  1484. WARN_ON(desc->gdev->chip->can_sleep);
  1485. return _gpiod_get_raw_value(desc);
  1486. }
  1487. EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
  1488. /**
  1489. * gpiod_get_value() - return a gpio's value
  1490. * @desc: gpio whose value will be returned
  1491. *
  1492. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  1493. * account, or negative errno on failure.
  1494. *
  1495. * This function should be called from contexts where we cannot sleep, and will
  1496. * complain if the GPIO chip functions potentially sleep.
  1497. */
  1498. int gpiod_get_value(const struct gpio_desc *desc)
  1499. {
  1500. int value;
  1501. VALIDATE_DESC(desc);
  1502. /* Should be using gpio_get_value_cansleep() */
  1503. WARN_ON(desc->gdev->chip->can_sleep);
  1504. value = _gpiod_get_raw_value(desc);
  1505. if (value < 0)
  1506. return value;
  1507. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1508. value = !value;
  1509. return value;
  1510. }
  1511. EXPORT_SYMBOL_GPL(gpiod_get_value);
  1512. /*
  1513. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  1514. * @desc: gpio descriptor whose state need to be set.
  1515. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  1516. */
  1517. static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
  1518. {
  1519. int err = 0;
  1520. struct gpio_chip *chip = desc->gdev->chip;
  1521. int offset = gpio_chip_hwgpio(desc);
  1522. if (value) {
  1523. err = chip->direction_input(chip, offset);
  1524. if (!err)
  1525. clear_bit(FLAG_IS_OUT, &desc->flags);
  1526. } else {
  1527. err = chip->direction_output(chip, offset, 0);
  1528. if (!err)
  1529. set_bit(FLAG_IS_OUT, &desc->flags);
  1530. }
  1531. trace_gpio_direction(desc_to_gpio(desc), value, err);
  1532. if (err < 0)
  1533. gpiod_err(desc,
  1534. "%s: Error in set_value for open drain err %d\n",
  1535. __func__, err);
  1536. }
  1537. /*
  1538. * _gpio_set_open_source_value() - Set the open source gpio's value.
  1539. * @desc: gpio descriptor whose state need to be set.
  1540. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  1541. */
  1542. static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
  1543. {
  1544. int err = 0;
  1545. struct gpio_chip *chip = desc->gdev->chip;
  1546. int offset = gpio_chip_hwgpio(desc);
  1547. if (value) {
  1548. err = chip->direction_output(chip, offset, 1);
  1549. if (!err)
  1550. set_bit(FLAG_IS_OUT, &desc->flags);
  1551. } else {
  1552. err = chip->direction_input(chip, offset);
  1553. if (!err)
  1554. clear_bit(FLAG_IS_OUT, &desc->flags);
  1555. }
  1556. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  1557. if (err < 0)
  1558. gpiod_err(desc,
  1559. "%s: Error in set_value for open source err %d\n",
  1560. __func__, err);
  1561. }
  1562. static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
  1563. {
  1564. struct gpio_chip *chip;
  1565. chip = desc->gdev->chip;
  1566. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1567. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1568. _gpio_set_open_drain_value(desc, value);
  1569. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1570. _gpio_set_open_source_value(desc, value);
  1571. else
  1572. chip->set(chip, gpio_chip_hwgpio(desc), value);
  1573. }
  1574. /*
  1575. * set multiple outputs on the same chip;
  1576. * use the chip's set_multiple function if available;
  1577. * otherwise set the outputs sequentially;
  1578. * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
  1579. * defines which outputs are to be changed
  1580. * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
  1581. * defines the values the outputs specified by mask are to be set to
  1582. */
  1583. static void gpio_chip_set_multiple(struct gpio_chip *chip,
  1584. unsigned long *mask, unsigned long *bits)
  1585. {
  1586. if (chip->set_multiple) {
  1587. chip->set_multiple(chip, mask, bits);
  1588. } else {
  1589. int i;
  1590. for (i = 0; i < chip->ngpio; i++) {
  1591. if (mask[BIT_WORD(i)] == 0) {
  1592. /* no more set bits in this mask word;
  1593. * skip ahead to the next word */
  1594. i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
  1595. continue;
  1596. }
  1597. /* set outputs if the corresponding mask bit is set */
  1598. if (__test_and_clear_bit(i, mask))
  1599. chip->set(chip, i, test_bit(i, bits));
  1600. }
  1601. }
  1602. }
  1603. static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
  1604. unsigned int array_size,
  1605. struct gpio_desc **desc_array,
  1606. int *value_array)
  1607. {
  1608. int i = 0;
  1609. while (i < array_size) {
  1610. struct gpio_chip *chip = desc_array[i]->gdev->chip;
  1611. unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
  1612. unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
  1613. int count = 0;
  1614. if (!can_sleep)
  1615. WARN_ON(chip->can_sleep);
  1616. memset(mask, 0, sizeof(mask));
  1617. do {
  1618. struct gpio_desc *desc = desc_array[i];
  1619. int hwgpio = gpio_chip_hwgpio(desc);
  1620. int value = value_array[i];
  1621. if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1622. value = !value;
  1623. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1624. /*
  1625. * collect all normal outputs belonging to the same chip
  1626. * open drain and open source outputs are set individually
  1627. */
  1628. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
  1629. _gpio_set_open_drain_value(desc, value);
  1630. } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
  1631. _gpio_set_open_source_value(desc, value);
  1632. } else {
  1633. __set_bit(hwgpio, mask);
  1634. if (value)
  1635. __set_bit(hwgpio, bits);
  1636. else
  1637. __clear_bit(hwgpio, bits);
  1638. count++;
  1639. }
  1640. i++;
  1641. } while ((i < array_size) &&
  1642. (desc_array[i]->gdev->chip == chip));
  1643. /* push collected bits to outputs */
  1644. if (count != 0)
  1645. gpio_chip_set_multiple(chip, mask, bits);
  1646. }
  1647. }
  1648. /**
  1649. * gpiod_set_raw_value() - assign a gpio's raw value
  1650. * @desc: gpio whose value will be assigned
  1651. * @value: value to assign
  1652. *
  1653. * Set the raw value of the GPIO, i.e. the value of its physical line without
  1654. * regard for its ACTIVE_LOW status.
  1655. *
  1656. * This function should be called from contexts where we cannot sleep, and will
  1657. * complain if the GPIO chip functions potentially sleep.
  1658. */
  1659. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  1660. {
  1661. VALIDATE_DESC_VOID(desc);
  1662. /* Should be using gpiod_set_value_cansleep() */
  1663. WARN_ON(desc->gdev->chip->can_sleep);
  1664. _gpiod_set_raw_value(desc, value);
  1665. }
  1666. EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
  1667. /**
  1668. * gpiod_set_value() - assign a gpio's value
  1669. * @desc: gpio whose value will be assigned
  1670. * @value: value to assign
  1671. *
  1672. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1673. * account
  1674. *
  1675. * This function should be called from contexts where we cannot sleep, and will
  1676. * complain if the GPIO chip functions potentially sleep.
  1677. */
  1678. void gpiod_set_value(struct gpio_desc *desc, int value)
  1679. {
  1680. VALIDATE_DESC_VOID(desc);
  1681. /* Should be using gpiod_set_value_cansleep() */
  1682. WARN_ON(desc->gdev->chip->can_sleep);
  1683. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1684. value = !value;
  1685. _gpiod_set_raw_value(desc, value);
  1686. }
  1687. EXPORT_SYMBOL_GPL(gpiod_set_value);
  1688. /**
  1689. * gpiod_set_raw_array_value() - assign values to an array of GPIOs
  1690. * @array_size: number of elements in the descriptor / value arrays
  1691. * @desc_array: array of GPIO descriptors whose values will be assigned
  1692. * @value_array: array of values to assign
  1693. *
  1694. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  1695. * without regard for their ACTIVE_LOW status.
  1696. *
  1697. * This function should be called from contexts where we cannot sleep, and will
  1698. * complain if the GPIO chip functions potentially sleep.
  1699. */
  1700. void gpiod_set_raw_array_value(unsigned int array_size,
  1701. struct gpio_desc **desc_array, int *value_array)
  1702. {
  1703. if (!desc_array)
  1704. return;
  1705. gpiod_set_array_value_priv(true, false, array_size, desc_array,
  1706. value_array);
  1707. }
  1708. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
  1709. /**
  1710. * gpiod_set_array_value() - assign values to an array of GPIOs
  1711. * @array_size: number of elements in the descriptor / value arrays
  1712. * @desc_array: array of GPIO descriptors whose values will be assigned
  1713. * @value_array: array of values to assign
  1714. *
  1715. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  1716. * into account.
  1717. *
  1718. * This function should be called from contexts where we cannot sleep, and will
  1719. * complain if the GPIO chip functions potentially sleep.
  1720. */
  1721. void gpiod_set_array_value(unsigned int array_size,
  1722. struct gpio_desc **desc_array, int *value_array)
  1723. {
  1724. if (!desc_array)
  1725. return;
  1726. gpiod_set_array_value_priv(false, false, array_size, desc_array,
  1727. value_array);
  1728. }
  1729. EXPORT_SYMBOL_GPL(gpiod_set_array_value);
  1730. /**
  1731. * gpiod_cansleep() - report whether gpio value access may sleep
  1732. * @desc: gpio to check
  1733. *
  1734. */
  1735. int gpiod_cansleep(const struct gpio_desc *desc)
  1736. {
  1737. VALIDATE_DESC(desc);
  1738. return desc->gdev->chip->can_sleep;
  1739. }
  1740. EXPORT_SYMBOL_GPL(gpiod_cansleep);
  1741. /**
  1742. * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  1743. * @desc: gpio whose IRQ will be returned (already requested)
  1744. *
  1745. * Return the IRQ corresponding to the passed GPIO, or an error code in case of
  1746. * error.
  1747. */
  1748. int gpiod_to_irq(const struct gpio_desc *desc)
  1749. {
  1750. struct gpio_chip *chip;
  1751. int offset;
  1752. VALIDATE_DESC(desc);
  1753. chip = desc->gdev->chip;
  1754. offset = gpio_chip_hwgpio(desc);
  1755. return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
  1756. }
  1757. EXPORT_SYMBOL_GPL(gpiod_to_irq);
  1758. /**
  1759. * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
  1760. * @chip: the chip the GPIO to lock belongs to
  1761. * @offset: the offset of the GPIO to lock as IRQ
  1762. *
  1763. * This is used directly by GPIO drivers that want to lock down
  1764. * a certain GPIO line to be used for IRQs.
  1765. */
  1766. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
  1767. {
  1768. if (offset >= chip->ngpio)
  1769. return -EINVAL;
  1770. if (test_bit(FLAG_IS_OUT, &chip->gpiodev->descs[offset].flags)) {
  1771. chip_err(chip,
  1772. "%s: tried to flag a GPIO set as output for IRQ\n",
  1773. __func__);
  1774. return -EIO;
  1775. }
  1776. set_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
  1777. return 0;
  1778. }
  1779. EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
  1780. /**
  1781. * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
  1782. * @chip: the chip the GPIO to lock belongs to
  1783. * @offset: the offset of the GPIO to lock as IRQ
  1784. *
  1785. * This is used directly by GPIO drivers that want to indicate
  1786. * that a certain GPIO is no longer used exclusively for IRQ.
  1787. */
  1788. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
  1789. {
  1790. if (offset >= chip->ngpio)
  1791. return;
  1792. clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
  1793. }
  1794. EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
  1795. bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
  1796. {
  1797. if (offset >= chip->ngpio)
  1798. return false;
  1799. return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
  1800. }
  1801. EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
  1802. bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
  1803. {
  1804. if (offset >= chip->ngpio)
  1805. return false;
  1806. return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
  1807. }
  1808. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
  1809. bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
  1810. {
  1811. if (offset >= chip->ngpio)
  1812. return false;
  1813. return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
  1814. }
  1815. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
  1816. /**
  1817. * gpiod_get_raw_value_cansleep() - return a gpio's raw value
  1818. * @desc: gpio whose value will be returned
  1819. *
  1820. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  1821. * its ACTIVE_LOW status, or negative errno on failure.
  1822. *
  1823. * This function is to be called from contexts that can sleep.
  1824. */
  1825. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  1826. {
  1827. might_sleep_if(extra_checks);
  1828. VALIDATE_DESC(desc);
  1829. return _gpiod_get_raw_value(desc);
  1830. }
  1831. EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
  1832. /**
  1833. * gpiod_get_value_cansleep() - return a gpio's value
  1834. * @desc: gpio whose value will be returned
  1835. *
  1836. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  1837. * account, or negative errno on failure.
  1838. *
  1839. * This function is to be called from contexts that can sleep.
  1840. */
  1841. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  1842. {
  1843. int value;
  1844. might_sleep_if(extra_checks);
  1845. VALIDATE_DESC(desc);
  1846. value = _gpiod_get_raw_value(desc);
  1847. if (value < 0)
  1848. return value;
  1849. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1850. value = !value;
  1851. return value;
  1852. }
  1853. EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
  1854. /**
  1855. * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
  1856. * @desc: gpio whose value will be assigned
  1857. * @value: value to assign
  1858. *
  1859. * Set the raw value of the GPIO, i.e. the value of its physical line without
  1860. * regard for its ACTIVE_LOW status.
  1861. *
  1862. * This function is to be called from contexts that can sleep.
  1863. */
  1864. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  1865. {
  1866. might_sleep_if(extra_checks);
  1867. VALIDATE_DESC_VOID(desc);
  1868. _gpiod_set_raw_value(desc, value);
  1869. }
  1870. EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
  1871. /**
  1872. * gpiod_set_value_cansleep() - assign a gpio's value
  1873. * @desc: gpio whose value will be assigned
  1874. * @value: value to assign
  1875. *
  1876. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1877. * account
  1878. *
  1879. * This function is to be called from contexts that can sleep.
  1880. */
  1881. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  1882. {
  1883. might_sleep_if(extra_checks);
  1884. VALIDATE_DESC_VOID(desc);
  1885. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1886. value = !value;
  1887. _gpiod_set_raw_value(desc, value);
  1888. }
  1889. EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
  1890. /**
  1891. * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
  1892. * @array_size: number of elements in the descriptor / value arrays
  1893. * @desc_array: array of GPIO descriptors whose values will be assigned
  1894. * @value_array: array of values to assign
  1895. *
  1896. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  1897. * without regard for their ACTIVE_LOW status.
  1898. *
  1899. * This function is to be called from contexts that can sleep.
  1900. */
  1901. void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  1902. struct gpio_desc **desc_array,
  1903. int *value_array)
  1904. {
  1905. might_sleep_if(extra_checks);
  1906. if (!desc_array)
  1907. return;
  1908. gpiod_set_array_value_priv(true, true, array_size, desc_array,
  1909. value_array);
  1910. }
  1911. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
  1912. /**
  1913. * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
  1914. * @array_size: number of elements in the descriptor / value arrays
  1915. * @desc_array: array of GPIO descriptors whose values will be assigned
  1916. * @value_array: array of values to assign
  1917. *
  1918. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  1919. * into account.
  1920. *
  1921. * This function is to be called from contexts that can sleep.
  1922. */
  1923. void gpiod_set_array_value_cansleep(unsigned int array_size,
  1924. struct gpio_desc **desc_array,
  1925. int *value_array)
  1926. {
  1927. might_sleep_if(extra_checks);
  1928. if (!desc_array)
  1929. return;
  1930. gpiod_set_array_value_priv(false, true, array_size, desc_array,
  1931. value_array);
  1932. }
  1933. EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
  1934. /**
  1935. * gpiod_add_lookup_table() - register GPIO device consumers
  1936. * @table: table of consumers to register
  1937. */
  1938. void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
  1939. {
  1940. mutex_lock(&gpio_lookup_lock);
  1941. list_add_tail(&table->list, &gpio_lookup_list);
  1942. mutex_unlock(&gpio_lookup_lock);
  1943. }
  1944. /**
  1945. * gpiod_remove_lookup_table() - unregister GPIO device consumers
  1946. * @table: table of consumers to unregister
  1947. */
  1948. void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
  1949. {
  1950. mutex_lock(&gpio_lookup_lock);
  1951. list_del(&table->list);
  1952. mutex_unlock(&gpio_lookup_lock);
  1953. }
  1954. static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  1955. unsigned int idx,
  1956. enum gpio_lookup_flags *flags)
  1957. {
  1958. char prop_name[32]; /* 32 is max size of property name */
  1959. enum of_gpio_flags of_flags;
  1960. struct gpio_desc *desc;
  1961. unsigned int i;
  1962. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  1963. if (con_id)
  1964. snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
  1965. gpio_suffixes[i]);
  1966. else
  1967. snprintf(prop_name, sizeof(prop_name), "%s",
  1968. gpio_suffixes[i]);
  1969. desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
  1970. &of_flags);
  1971. if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
  1972. break;
  1973. }
  1974. if (IS_ERR(desc))
  1975. return desc;
  1976. if (of_flags & OF_GPIO_ACTIVE_LOW)
  1977. *flags |= GPIO_ACTIVE_LOW;
  1978. if (of_flags & OF_GPIO_SINGLE_ENDED) {
  1979. if (of_flags & OF_GPIO_ACTIVE_LOW)
  1980. *flags |= GPIO_OPEN_DRAIN;
  1981. else
  1982. *flags |= GPIO_OPEN_SOURCE;
  1983. }
  1984. return desc;
  1985. }
  1986. static struct gpio_desc *acpi_find_gpio(struct device *dev,
  1987. const char *con_id,
  1988. unsigned int idx,
  1989. enum gpiod_flags flags,
  1990. enum gpio_lookup_flags *lookupflags)
  1991. {
  1992. struct acpi_device *adev = ACPI_COMPANION(dev);
  1993. struct acpi_gpio_info info;
  1994. struct gpio_desc *desc;
  1995. char propname[32];
  1996. int i;
  1997. /* Try first from _DSD */
  1998. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  1999. if (con_id && strcmp(con_id, "gpios")) {
  2000. snprintf(propname, sizeof(propname), "%s-%s",
  2001. con_id, gpio_suffixes[i]);
  2002. } else {
  2003. snprintf(propname, sizeof(propname), "%s",
  2004. gpio_suffixes[i]);
  2005. }
  2006. desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
  2007. if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
  2008. break;
  2009. }
  2010. /* Then from plain _CRS GPIOs */
  2011. if (IS_ERR(desc)) {
  2012. if (!acpi_can_fallback_to_crs(adev, con_id))
  2013. return ERR_PTR(-ENOENT);
  2014. desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
  2015. if (IS_ERR(desc))
  2016. return desc;
  2017. if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
  2018. info.gpioint) {
  2019. dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
  2020. return ERR_PTR(-ENOENT);
  2021. }
  2022. }
  2023. if (info.polarity == GPIO_ACTIVE_LOW)
  2024. *lookupflags |= GPIO_ACTIVE_LOW;
  2025. return desc;
  2026. }
  2027. static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
  2028. {
  2029. const char *dev_id = dev ? dev_name(dev) : NULL;
  2030. struct gpiod_lookup_table *table;
  2031. mutex_lock(&gpio_lookup_lock);
  2032. list_for_each_entry(table, &gpio_lookup_list, list) {
  2033. if (table->dev_id && dev_id) {
  2034. /*
  2035. * Valid strings on both ends, must be identical to have
  2036. * a match
  2037. */
  2038. if (!strcmp(table->dev_id, dev_id))
  2039. goto found;
  2040. } else {
  2041. /*
  2042. * One of the pointers is NULL, so both must be to have
  2043. * a match
  2044. */
  2045. if (dev_id == table->dev_id)
  2046. goto found;
  2047. }
  2048. }
  2049. table = NULL;
  2050. found:
  2051. mutex_unlock(&gpio_lookup_lock);
  2052. return table;
  2053. }
  2054. static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
  2055. unsigned int idx,
  2056. enum gpio_lookup_flags *flags)
  2057. {
  2058. struct gpio_desc *desc = ERR_PTR(-ENOENT);
  2059. struct gpiod_lookup_table *table;
  2060. struct gpiod_lookup *p;
  2061. table = gpiod_find_lookup_table(dev);
  2062. if (!table)
  2063. return desc;
  2064. for (p = &table->table[0]; p->chip_label; p++) {
  2065. struct gpio_chip *chip;
  2066. /* idx must always match exactly */
  2067. if (p->idx != idx)
  2068. continue;
  2069. /* If the lookup entry has a con_id, require exact match */
  2070. if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
  2071. continue;
  2072. chip = find_chip_by_name(p->chip_label);
  2073. if (!chip) {
  2074. dev_err(dev, "cannot find GPIO chip %s\n",
  2075. p->chip_label);
  2076. return ERR_PTR(-ENODEV);
  2077. }
  2078. if (chip->ngpio <= p->chip_hwnum) {
  2079. dev_err(dev,
  2080. "requested GPIO %d is out of range [0..%d] for chip %s\n",
  2081. idx, chip->ngpio, chip->label);
  2082. return ERR_PTR(-EINVAL);
  2083. }
  2084. desc = gpiochip_get_desc(chip, p->chip_hwnum);
  2085. *flags = p->flags;
  2086. return desc;
  2087. }
  2088. return desc;
  2089. }
  2090. static int dt_gpio_count(struct device *dev, const char *con_id)
  2091. {
  2092. int ret;
  2093. char propname[32];
  2094. unsigned int i;
  2095. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  2096. if (con_id)
  2097. snprintf(propname, sizeof(propname), "%s-%s",
  2098. con_id, gpio_suffixes[i]);
  2099. else
  2100. snprintf(propname, sizeof(propname), "%s",
  2101. gpio_suffixes[i]);
  2102. ret = of_gpio_named_count(dev->of_node, propname);
  2103. if (ret >= 0)
  2104. break;
  2105. }
  2106. return ret;
  2107. }
  2108. static int platform_gpio_count(struct device *dev, const char *con_id)
  2109. {
  2110. struct gpiod_lookup_table *table;
  2111. struct gpiod_lookup *p;
  2112. unsigned int count = 0;
  2113. table = gpiod_find_lookup_table(dev);
  2114. if (!table)
  2115. return -ENOENT;
  2116. for (p = &table->table[0]; p->chip_label; p++) {
  2117. if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
  2118. (!con_id && !p->con_id))
  2119. count++;
  2120. }
  2121. if (!count)
  2122. return -ENOENT;
  2123. return count;
  2124. }
  2125. /**
  2126. * gpiod_count - return the number of GPIOs associated with a device / function
  2127. * or -ENOENT if no GPIO has been assigned to the requested function
  2128. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2129. * @con_id: function within the GPIO consumer
  2130. */
  2131. int gpiod_count(struct device *dev, const char *con_id)
  2132. {
  2133. int count = -ENOENT;
  2134. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  2135. count = dt_gpio_count(dev, con_id);
  2136. else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
  2137. count = acpi_gpio_count(dev, con_id);
  2138. if (count < 0)
  2139. count = platform_gpio_count(dev, con_id);
  2140. return count;
  2141. }
  2142. EXPORT_SYMBOL_GPL(gpiod_count);
  2143. /**
  2144. * gpiod_get - obtain a GPIO for a given GPIO function
  2145. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2146. * @con_id: function within the GPIO consumer
  2147. * @flags: optional GPIO initialization flags
  2148. *
  2149. * Return the GPIO descriptor corresponding to the function con_id of device
  2150. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  2151. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  2152. */
  2153. struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
  2154. enum gpiod_flags flags)
  2155. {
  2156. return gpiod_get_index(dev, con_id, 0, flags);
  2157. }
  2158. EXPORT_SYMBOL_GPL(gpiod_get);
  2159. /**
  2160. * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
  2161. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2162. * @con_id: function within the GPIO consumer
  2163. * @flags: optional GPIO initialization flags
  2164. *
  2165. * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
  2166. * the requested function it will return NULL. This is convenient for drivers
  2167. * that need to handle optional GPIOs.
  2168. */
  2169. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  2170. const char *con_id,
  2171. enum gpiod_flags flags)
  2172. {
  2173. return gpiod_get_index_optional(dev, con_id, 0, flags);
  2174. }
  2175. EXPORT_SYMBOL_GPL(gpiod_get_optional);
  2176. /**
  2177. * gpiod_parse_flags - helper function to parse GPIO lookup flags
  2178. * @desc: gpio to be setup
  2179. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  2180. * of_get_gpio_hog()
  2181. *
  2182. * Set the GPIO descriptor flags based on the given GPIO lookup flags.
  2183. */
  2184. static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
  2185. {
  2186. if (lflags & GPIO_ACTIVE_LOW)
  2187. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2188. if (lflags & GPIO_OPEN_DRAIN)
  2189. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  2190. if (lflags & GPIO_OPEN_SOURCE)
  2191. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  2192. }
  2193. /**
  2194. * gpiod_configure_flags - helper function to configure a given GPIO
  2195. * @desc: gpio whose value will be assigned
  2196. * @con_id: function within the GPIO consumer
  2197. * @dflags: gpiod_flags - optional GPIO initialization flags
  2198. *
  2199. * Return 0 on success, -ENOENT if no GPIO has been assigned to the
  2200. * requested function and/or index, or another IS_ERR() code if an error
  2201. * occurred while trying to acquire the GPIO.
  2202. */
  2203. static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  2204. enum gpiod_flags dflags)
  2205. {
  2206. int status;
  2207. /* No particular flag request, return here... */
  2208. if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
  2209. pr_debug("no flags found for %s\n", con_id);
  2210. return 0;
  2211. }
  2212. /* Process flags */
  2213. if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
  2214. status = gpiod_direction_output(desc,
  2215. dflags & GPIOD_FLAGS_BIT_DIR_VAL);
  2216. else
  2217. status = gpiod_direction_input(desc);
  2218. return status;
  2219. }
  2220. /**
  2221. * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
  2222. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2223. * @con_id: function within the GPIO consumer
  2224. * @idx: index of the GPIO to obtain in the consumer
  2225. * @flags: optional GPIO initialization flags
  2226. *
  2227. * This variant of gpiod_get() allows to access GPIOs other than the first
  2228. * defined one for functions that define several GPIOs.
  2229. *
  2230. * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
  2231. * requested function and/or index, or another IS_ERR() code if an error
  2232. * occurred while trying to acquire the GPIO.
  2233. */
  2234. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  2235. const char *con_id,
  2236. unsigned int idx,
  2237. enum gpiod_flags flags)
  2238. {
  2239. struct gpio_desc *desc = NULL;
  2240. int status;
  2241. enum gpio_lookup_flags lookupflags = 0;
  2242. dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
  2243. if (dev) {
  2244. /* Using device tree? */
  2245. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  2246. dev_dbg(dev, "using device tree for GPIO lookup\n");
  2247. desc = of_find_gpio(dev, con_id, idx, &lookupflags);
  2248. } else if (ACPI_COMPANION(dev)) {
  2249. dev_dbg(dev, "using ACPI for GPIO lookup\n");
  2250. desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
  2251. }
  2252. }
  2253. /*
  2254. * Either we are not using DT or ACPI, or their lookup did not return
  2255. * a result. In that case, use platform lookup as a fallback.
  2256. */
  2257. if (!desc || desc == ERR_PTR(-ENOENT)) {
  2258. dev_dbg(dev, "using lookup tables for GPIO lookup\n");
  2259. desc = gpiod_find(dev, con_id, idx, &lookupflags);
  2260. }
  2261. if (IS_ERR(desc)) {
  2262. dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
  2263. return desc;
  2264. }
  2265. gpiod_parse_flags(desc, lookupflags);
  2266. status = gpiod_request(desc, con_id);
  2267. if (status < 0)
  2268. return ERR_PTR(status);
  2269. status = gpiod_configure_flags(desc, con_id, flags);
  2270. if (status < 0) {
  2271. dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
  2272. gpiod_put(desc);
  2273. return ERR_PTR(status);
  2274. }
  2275. return desc;
  2276. }
  2277. EXPORT_SYMBOL_GPL(gpiod_get_index);
  2278. /**
  2279. * fwnode_get_named_gpiod - obtain a GPIO from firmware node
  2280. * @fwnode: handle of the firmware node
  2281. * @propname: name of the firmware property representing the GPIO
  2282. *
  2283. * This function can be used for drivers that get their configuration
  2284. * from firmware.
  2285. *
  2286. * Function properly finds the corresponding GPIO using whatever is the
  2287. * underlying firmware interface and then makes sure that the GPIO
  2288. * descriptor is requested before it is returned to the caller.
  2289. *
  2290. * In case of error an ERR_PTR() is returned.
  2291. */
  2292. struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  2293. const char *propname)
  2294. {
  2295. struct gpio_desc *desc = ERR_PTR(-ENODEV);
  2296. bool active_low = false;
  2297. bool single_ended = false;
  2298. int ret;
  2299. if (!fwnode)
  2300. return ERR_PTR(-EINVAL);
  2301. if (is_of_node(fwnode)) {
  2302. enum of_gpio_flags flags;
  2303. desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
  2304. &flags);
  2305. if (!IS_ERR(desc)) {
  2306. active_low = flags & OF_GPIO_ACTIVE_LOW;
  2307. single_ended = flags & OF_GPIO_SINGLE_ENDED;
  2308. }
  2309. } else if (is_acpi_node(fwnode)) {
  2310. struct acpi_gpio_info info;
  2311. desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
  2312. if (!IS_ERR(desc))
  2313. active_low = info.polarity == GPIO_ACTIVE_LOW;
  2314. }
  2315. if (IS_ERR(desc))
  2316. return desc;
  2317. if (active_low)
  2318. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2319. if (single_ended) {
  2320. if (active_low)
  2321. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  2322. else
  2323. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  2324. }
  2325. ret = gpiod_request(desc, NULL);
  2326. if (ret)
  2327. return ERR_PTR(ret);
  2328. return desc;
  2329. }
  2330. EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
  2331. /**
  2332. * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
  2333. * function
  2334. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2335. * @con_id: function within the GPIO consumer
  2336. * @index: index of the GPIO to obtain in the consumer
  2337. * @flags: optional GPIO initialization flags
  2338. *
  2339. * This is equivalent to gpiod_get_index(), except that when no GPIO with the
  2340. * specified index was assigned to the requested function it will return NULL.
  2341. * This is convenient for drivers that need to handle optional GPIOs.
  2342. */
  2343. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  2344. const char *con_id,
  2345. unsigned int index,
  2346. enum gpiod_flags flags)
  2347. {
  2348. struct gpio_desc *desc;
  2349. desc = gpiod_get_index(dev, con_id, index, flags);
  2350. if (IS_ERR(desc)) {
  2351. if (PTR_ERR(desc) == -ENOENT)
  2352. return NULL;
  2353. }
  2354. return desc;
  2355. }
  2356. EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
  2357. /**
  2358. * gpiod_hog - Hog the specified GPIO desc given the provided flags
  2359. * @desc: gpio whose value will be assigned
  2360. * @name: gpio line name
  2361. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  2362. * of_get_gpio_hog()
  2363. * @dflags: gpiod_flags - optional GPIO initialization flags
  2364. */
  2365. int gpiod_hog(struct gpio_desc *desc, const char *name,
  2366. unsigned long lflags, enum gpiod_flags dflags)
  2367. {
  2368. struct gpio_chip *chip;
  2369. struct gpio_desc *local_desc;
  2370. int hwnum;
  2371. int status;
  2372. chip = gpiod_to_chip(desc);
  2373. hwnum = gpio_chip_hwgpio(desc);
  2374. gpiod_parse_flags(desc, lflags);
  2375. local_desc = gpiochip_request_own_desc(chip, hwnum, name);
  2376. if (IS_ERR(local_desc)) {
  2377. pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
  2378. name, chip->label, hwnum);
  2379. return PTR_ERR(local_desc);
  2380. }
  2381. status = gpiod_configure_flags(desc, name, dflags);
  2382. if (status < 0) {
  2383. pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
  2384. name, chip->label, hwnum);
  2385. gpiochip_free_own_desc(desc);
  2386. return status;
  2387. }
  2388. /* Mark GPIO as hogged so it can be identified and removed later */
  2389. set_bit(FLAG_IS_HOGGED, &desc->flags);
  2390. pr_info("GPIO line %d (%s) hogged as %s%s\n",
  2391. desc_to_gpio(desc), name,
  2392. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
  2393. (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
  2394. (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
  2395. return 0;
  2396. }
  2397. /**
  2398. * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
  2399. * @chip: gpio chip to act on
  2400. *
  2401. * This is only used by of_gpiochip_remove to free hogged gpios
  2402. */
  2403. static void gpiochip_free_hogs(struct gpio_chip *chip)
  2404. {
  2405. int id;
  2406. for (id = 0; id < chip->ngpio; id++) {
  2407. if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
  2408. gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
  2409. }
  2410. }
  2411. /**
  2412. * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
  2413. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2414. * @con_id: function within the GPIO consumer
  2415. * @flags: optional GPIO initialization flags
  2416. *
  2417. * This function acquires all the GPIOs defined under a given function.
  2418. *
  2419. * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
  2420. * no GPIO has been assigned to the requested function, or another IS_ERR()
  2421. * code if an error occurred while trying to acquire the GPIOs.
  2422. */
  2423. struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
  2424. const char *con_id,
  2425. enum gpiod_flags flags)
  2426. {
  2427. struct gpio_desc *desc;
  2428. struct gpio_descs *descs;
  2429. int count;
  2430. count = gpiod_count(dev, con_id);
  2431. if (count < 0)
  2432. return ERR_PTR(count);
  2433. descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
  2434. GFP_KERNEL);
  2435. if (!descs)
  2436. return ERR_PTR(-ENOMEM);
  2437. for (descs->ndescs = 0; descs->ndescs < count; ) {
  2438. desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
  2439. if (IS_ERR(desc)) {
  2440. gpiod_put_array(descs);
  2441. return ERR_CAST(desc);
  2442. }
  2443. descs->desc[descs->ndescs] = desc;
  2444. descs->ndescs++;
  2445. }
  2446. return descs;
  2447. }
  2448. EXPORT_SYMBOL_GPL(gpiod_get_array);
  2449. /**
  2450. * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
  2451. * function
  2452. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2453. * @con_id: function within the GPIO consumer
  2454. * @flags: optional GPIO initialization flags
  2455. *
  2456. * This is equivalent to gpiod_get_array(), except that when no GPIO was
  2457. * assigned to the requested function it will return NULL.
  2458. */
  2459. struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
  2460. const char *con_id,
  2461. enum gpiod_flags flags)
  2462. {
  2463. struct gpio_descs *descs;
  2464. descs = gpiod_get_array(dev, con_id, flags);
  2465. if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
  2466. return NULL;
  2467. return descs;
  2468. }
  2469. EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
  2470. /**
  2471. * gpiod_put - dispose of a GPIO descriptor
  2472. * @desc: GPIO descriptor to dispose of
  2473. *
  2474. * No descriptor can be used after gpiod_put() has been called on it.
  2475. */
  2476. void gpiod_put(struct gpio_desc *desc)
  2477. {
  2478. gpiod_free(desc);
  2479. }
  2480. EXPORT_SYMBOL_GPL(gpiod_put);
  2481. /**
  2482. * gpiod_put_array - dispose of multiple GPIO descriptors
  2483. * @descs: struct gpio_descs containing an array of descriptors
  2484. */
  2485. void gpiod_put_array(struct gpio_descs *descs)
  2486. {
  2487. unsigned int i;
  2488. for (i = 0; i < descs->ndescs; i++)
  2489. gpiod_put(descs->desc[i]);
  2490. kfree(descs);
  2491. }
  2492. EXPORT_SYMBOL_GPL(gpiod_put_array);
  2493. static int __init gpiolib_dev_init(void)
  2494. {
  2495. int ret;
  2496. /* Register GPIO sysfs bus */
  2497. ret = bus_register(&gpio_bus_type);
  2498. if (ret < 0) {
  2499. pr_err("gpiolib: could not register GPIO bus type\n");
  2500. return ret;
  2501. }
  2502. ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
  2503. if (ret < 0) {
  2504. pr_err("gpiolib: failed to allocate char dev region\n");
  2505. bus_unregister(&gpio_bus_type);
  2506. } else {
  2507. gpiolib_initialized = true;
  2508. gpiochip_setup_devs();
  2509. }
  2510. return ret;
  2511. }
  2512. core_initcall(gpiolib_dev_init);
  2513. #ifdef CONFIG_DEBUG_FS
  2514. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
  2515. {
  2516. unsigned i;
  2517. struct gpio_chip *chip = gdev->chip;
  2518. unsigned gpio = gdev->base;
  2519. struct gpio_desc *gdesc = &gdev->descs[0];
  2520. int is_out;
  2521. int is_irq;
  2522. for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
  2523. if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
  2524. if (gdesc->name) {
  2525. seq_printf(s, " gpio-%-3d (%-20.20s)\n",
  2526. gpio, gdesc->name);
  2527. }
  2528. continue;
  2529. }
  2530. gpiod_get_direction(gdesc);
  2531. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  2532. is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
  2533. seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
  2534. gpio, gdesc->name ? gdesc->name : "", gdesc->label,
  2535. is_out ? "out" : "in ",
  2536. chip->get
  2537. ? (chip->get(chip, i) ? "hi" : "lo")
  2538. : "? ",
  2539. is_irq ? "IRQ" : " ");
  2540. seq_printf(s, "\n");
  2541. }
  2542. }
  2543. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  2544. {
  2545. unsigned long flags;
  2546. struct gpio_device *gdev = NULL;
  2547. loff_t index = *pos;
  2548. s->private = "";
  2549. spin_lock_irqsave(&gpio_lock, flags);
  2550. list_for_each_entry(gdev, &gpio_devices, list)
  2551. if (index-- == 0) {
  2552. spin_unlock_irqrestore(&gpio_lock, flags);
  2553. return gdev;
  2554. }
  2555. spin_unlock_irqrestore(&gpio_lock, flags);
  2556. return NULL;
  2557. }
  2558. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2559. {
  2560. unsigned long flags;
  2561. struct gpio_device *gdev = v;
  2562. void *ret = NULL;
  2563. spin_lock_irqsave(&gpio_lock, flags);
  2564. if (list_is_last(&gdev->list, &gpio_devices))
  2565. ret = NULL;
  2566. else
  2567. ret = list_entry(gdev->list.next, struct gpio_device, list);
  2568. spin_unlock_irqrestore(&gpio_lock, flags);
  2569. s->private = "\n";
  2570. ++*pos;
  2571. return ret;
  2572. }
  2573. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  2574. {
  2575. }
  2576. static int gpiolib_seq_show(struct seq_file *s, void *v)
  2577. {
  2578. struct gpio_device *gdev = v;
  2579. struct gpio_chip *chip = gdev->chip;
  2580. struct device *parent;
  2581. if (!chip) {
  2582. seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
  2583. dev_name(&gdev->dev));
  2584. return 0;
  2585. }
  2586. seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
  2587. dev_name(&gdev->dev),
  2588. gdev->base, gdev->base + gdev->ngpio - 1);
  2589. parent = chip->parent;
  2590. if (parent)
  2591. seq_printf(s, ", parent: %s/%s",
  2592. parent->bus ? parent->bus->name : "no-bus",
  2593. dev_name(parent));
  2594. if (chip->label)
  2595. seq_printf(s, ", %s", chip->label);
  2596. if (chip->can_sleep)
  2597. seq_printf(s, ", can sleep");
  2598. seq_printf(s, ":\n");
  2599. if (chip->dbg_show)
  2600. chip->dbg_show(s, chip);
  2601. else
  2602. gpiolib_dbg_show(s, gdev);
  2603. return 0;
  2604. }
  2605. static const struct seq_operations gpiolib_seq_ops = {
  2606. .start = gpiolib_seq_start,
  2607. .next = gpiolib_seq_next,
  2608. .stop = gpiolib_seq_stop,
  2609. .show = gpiolib_seq_show,
  2610. };
  2611. static int gpiolib_open(struct inode *inode, struct file *file)
  2612. {
  2613. return seq_open(file, &gpiolib_seq_ops);
  2614. }
  2615. static const struct file_operations gpiolib_operations = {
  2616. .owner = THIS_MODULE,
  2617. .open = gpiolib_open,
  2618. .read = seq_read,
  2619. .llseek = seq_lseek,
  2620. .release = seq_release,
  2621. };
  2622. static int __init gpiolib_debugfs_init(void)
  2623. {
  2624. /* /sys/kernel/debug/gpio */
  2625. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  2626. NULL, NULL, &gpiolib_operations);
  2627. return 0;
  2628. }
  2629. subsys_initcall(gpiolib_debugfs_init);
  2630. #endif /* DEBUG_FS */