gpiolib.c 96 KB

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