gpiolib.c 96 KB

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