gpiolib.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bitmap.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/irq.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/list.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/gpio.h>
  14. #include <linux/idr.h>
  15. #include <linux/slab.h>
  16. #include <linux/acpi.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/gpio/machine.h>
  19. #include <linux/pinctrl/consumer.h>
  20. #include <linux/fs.h>
  21. #include <linux/compat.h>
  22. #include <linux/file.h>
  23. #include <uapi/linux/gpio.h>
  24. #include "gpiolib.h"
  25. #include "gpiolib-of.h"
  26. #include "gpiolib-acpi.h"
  27. #include "gpiolib-cdev.h"
  28. #include "gpiolib-sysfs.h"
  29. #define CREATE_TRACE_POINTS
  30. #include <trace/events/gpio.h>
  31. /* Implementation infrastructure for GPIO interfaces.
  32. *
  33. * The GPIO programming interface allows for inlining speed-critical
  34. * get/set operations for common cases, so that access to SOC-integrated
  35. * GPIOs can sometimes cost only an instruction or two per bit.
  36. */
  37. /* When debugging, extend minimal trust to callers and platform code.
  38. * Also emit diagnostic messages that may help initial bringup, when
  39. * board setup or driver bugs are most common.
  40. *
  41. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  42. */
  43. #ifdef DEBUG
  44. #define extra_checks 1
  45. #else
  46. #define extra_checks 0
  47. #endif
  48. /* Device and char device-related information */
  49. static DEFINE_IDA(gpio_ida);
  50. static dev_t gpio_devt;
  51. #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
  52. static int gpio_bus_match(struct device *dev, struct device_driver *drv);
  53. static struct bus_type gpio_bus_type = {
  54. .name = "gpio",
  55. .match = gpio_bus_match,
  56. };
  57. /*
  58. * Number of GPIOs to use for the fast path in set array
  59. */
  60. #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
  61. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  62. * While any GPIO is requested, its gpio_chip is not removable;
  63. * each GPIO's "requested" flag serves as a lock and refcount.
  64. */
  65. DEFINE_SPINLOCK(gpio_lock);
  66. static DEFINE_MUTEX(gpio_lookup_lock);
  67. static LIST_HEAD(gpio_lookup_list);
  68. LIST_HEAD(gpio_devices);
  69. static DEFINE_MUTEX(gpio_machine_hogs_mutex);
  70. static LIST_HEAD(gpio_machine_hogs);
  71. static void gpiochip_free_hogs(struct gpio_chip *gc);
  72. static int gpiochip_add_irqchip(struct gpio_chip *gc,
  73. struct lock_class_key *lock_key,
  74. struct lock_class_key *request_key);
  75. static void gpiochip_irqchip_remove(struct gpio_chip *gc);
  76. static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
  77. static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
  78. static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
  79. static bool gpiolib_initialized;
  80. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  81. {
  82. d->label = label;
  83. }
  84. /**
  85. * gpio_to_desc - Convert a GPIO number to its descriptor
  86. * @gpio: global GPIO number
  87. *
  88. * Returns:
  89. * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
  90. * with the given number exists in the system.
  91. */
  92. struct gpio_desc *gpio_to_desc(unsigned gpio)
  93. {
  94. struct gpio_device *gdev;
  95. unsigned long flags;
  96. spin_lock_irqsave(&gpio_lock, flags);
  97. list_for_each_entry(gdev, &gpio_devices, list) {
  98. if (gdev->base <= gpio &&
  99. gdev->base + gdev->ngpio > gpio) {
  100. spin_unlock_irqrestore(&gpio_lock, flags);
  101. return &gdev->descs[gpio - gdev->base];
  102. }
  103. }
  104. spin_unlock_irqrestore(&gpio_lock, flags);
  105. if (!gpio_is_valid(gpio))
  106. pr_warn("invalid GPIO %d\n", gpio);
  107. return NULL;
  108. }
  109. EXPORT_SYMBOL_GPL(gpio_to_desc);
  110. /**
  111. * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
  112. * hardware number for this chip
  113. * @gc: GPIO chip
  114. * @hwnum: hardware number of the GPIO for this chip
  115. *
  116. * Returns:
  117. * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
  118. * in the given chip for the specified hardware number.
  119. */
  120. struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
  121. unsigned int hwnum)
  122. {
  123. struct gpio_device *gdev = gc->gpiodev;
  124. if (hwnum >= gdev->ngpio)
  125. return ERR_PTR(-EINVAL);
  126. return &gdev->descs[hwnum];
  127. }
  128. EXPORT_SYMBOL_GPL(gpiochip_get_desc);
  129. /**
  130. * desc_to_gpio - convert a GPIO descriptor to the integer namespace
  131. * @desc: GPIO descriptor
  132. *
  133. * This should disappear in the future but is needed since we still
  134. * use GPIO numbers for error messages and sysfs nodes.
  135. *
  136. * Returns:
  137. * The global GPIO number for the GPIO specified by its descriptor.
  138. */
  139. int desc_to_gpio(const struct gpio_desc *desc)
  140. {
  141. return desc->gdev->base + (desc - &desc->gdev->descs[0]);
  142. }
  143. EXPORT_SYMBOL_GPL(desc_to_gpio);
  144. /**
  145. * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
  146. * @desc: descriptor to return the chip of
  147. */
  148. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  149. {
  150. if (!desc || !desc->gdev)
  151. return NULL;
  152. return desc->gdev->chip;
  153. }
  154. EXPORT_SYMBOL_GPL(gpiod_to_chip);
  155. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  156. static int gpiochip_find_base(int ngpio)
  157. {
  158. struct gpio_device *gdev;
  159. int base = ARCH_NR_GPIOS - ngpio;
  160. list_for_each_entry_reverse(gdev, &gpio_devices, list) {
  161. /* found a free space? */
  162. if (gdev->base + gdev->ngpio <= base)
  163. break;
  164. /* nope, check the space right before the chip */
  165. base = gdev->base - ngpio;
  166. }
  167. if (gpio_is_valid(base)) {
  168. pr_debug("%s: found new base at %d\n", __func__, base);
  169. return base;
  170. } else {
  171. pr_err("%s: cannot find free range\n", __func__);
  172. return -ENOSPC;
  173. }
  174. }
  175. /**
  176. * gpiod_get_direction - return the current direction of a GPIO
  177. * @desc: GPIO to get the direction of
  178. *
  179. * Returns 0 for output, 1 for input, or an error code in case of error.
  180. *
  181. * This function may sleep if gpiod_cansleep() is true.
  182. */
  183. int gpiod_get_direction(struct gpio_desc *desc)
  184. {
  185. struct gpio_chip *gc;
  186. unsigned int offset;
  187. int ret;
  188. gc = gpiod_to_chip(desc);
  189. offset = gpio_chip_hwgpio(desc);
  190. /*
  191. * Open drain emulation using input mode may incorrectly report
  192. * input here, fix that up.
  193. */
  194. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
  195. test_bit(FLAG_IS_OUT, &desc->flags))
  196. return 0;
  197. if (!gc->get_direction)
  198. return -ENOTSUPP;
  199. ret = gc->get_direction(gc, offset);
  200. if (ret < 0)
  201. return ret;
  202. /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
  203. if (ret > 0)
  204. ret = 1;
  205. assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL_GPL(gpiod_get_direction);
  209. /*
  210. * Add a new chip to the global chips list, keeping the list of chips sorted
  211. * by range(means [base, base + ngpio - 1]) order.
  212. *
  213. * Return -EBUSY if the new chip overlaps with some other chip's integer
  214. * space.
  215. */
  216. static int gpiodev_add_to_list(struct gpio_device *gdev)
  217. {
  218. struct gpio_device *prev, *next;
  219. if (list_empty(&gpio_devices)) {
  220. /* initial entry in list */
  221. list_add_tail(&gdev->list, &gpio_devices);
  222. return 0;
  223. }
  224. next = list_first_entry(&gpio_devices, struct gpio_device, list);
  225. if (gdev->base + gdev->ngpio <= next->base) {
  226. /* add before first entry */
  227. list_add(&gdev->list, &gpio_devices);
  228. return 0;
  229. }
  230. prev = list_last_entry(&gpio_devices, struct gpio_device, list);
  231. if (prev->base + prev->ngpio <= gdev->base) {
  232. /* add behind last entry */
  233. list_add_tail(&gdev->list, &gpio_devices);
  234. return 0;
  235. }
  236. list_for_each_entry_safe(prev, next, &gpio_devices, list) {
  237. /* at the end of the list */
  238. if (&next->list == &gpio_devices)
  239. break;
  240. /* add between prev and next */
  241. if (prev->base + prev->ngpio <= gdev->base
  242. && gdev->base + gdev->ngpio <= next->base) {
  243. list_add(&gdev->list, &prev->list);
  244. return 0;
  245. }
  246. }
  247. return -EBUSY;
  248. }
  249. /*
  250. * Convert a GPIO name to its descriptor
  251. * Note that there is no guarantee that GPIO names are globally unique!
  252. * Hence this function will return, if it exists, a reference to the first GPIO
  253. * line found that matches the given name.
  254. */
  255. static struct gpio_desc *gpio_name_to_desc(const char * const name)
  256. {
  257. struct gpio_device *gdev;
  258. unsigned long flags;
  259. if (!name)
  260. return NULL;
  261. spin_lock_irqsave(&gpio_lock, flags);
  262. list_for_each_entry(gdev, &gpio_devices, list) {
  263. struct gpio_desc *desc;
  264. for_each_gpio_desc(gdev->chip, desc) {
  265. if (desc->name && !strcmp(desc->name, name)) {
  266. spin_unlock_irqrestore(&gpio_lock, flags);
  267. return desc;
  268. }
  269. }
  270. }
  271. spin_unlock_irqrestore(&gpio_lock, flags);
  272. return NULL;
  273. }
  274. /*
  275. * Take the names from gc->names and assign them to their GPIO descriptors.
  276. * Warn if a name is already used for a GPIO line on a different GPIO chip.
  277. *
  278. * Note that:
  279. * 1. Non-unique names are still accepted,
  280. * 2. Name collisions within the same GPIO chip are not reported.
  281. */
  282. static int gpiochip_set_desc_names(struct gpio_chip *gc)
  283. {
  284. struct gpio_device *gdev = gc->gpiodev;
  285. int i;
  286. /* First check all names if they are unique */
  287. for (i = 0; i != gc->ngpio; ++i) {
  288. struct gpio_desc *gpio;
  289. gpio = gpio_name_to_desc(gc->names[i]);
  290. if (gpio)
  291. dev_warn(&gdev->dev,
  292. "Detected name collision for GPIO name '%s'\n",
  293. gc->names[i]);
  294. }
  295. /* Then add all names to the GPIO descriptors */
  296. for (i = 0; i != gc->ngpio; ++i)
  297. gdev->descs[i].name = gc->names[i];
  298. return 0;
  299. }
  300. /*
  301. * devprop_gpiochip_set_names - Set GPIO line names using device properties
  302. * @chip: GPIO chip whose lines should be named, if possible
  303. *
  304. * Looks for device property "gpio-line-names" and if it exists assigns
  305. * GPIO line names for the chip. The memory allocated for the assigned
  306. * names belong to the underlying firmware node and should not be released
  307. * by the caller.
  308. */
  309. static int devprop_gpiochip_set_names(struct gpio_chip *chip)
  310. {
  311. struct gpio_device *gdev = chip->gpiodev;
  312. struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
  313. const char **names;
  314. int ret, i;
  315. int count;
  316. count = fwnode_property_string_array_count(fwnode, "gpio-line-names");
  317. if (count < 0)
  318. return 0;
  319. /*
  320. * When offset is set in the driver side we assume the driver internally
  321. * is using more than one gpiochip per the same device. We have to stop
  322. * setting friendly names if the specified ones with 'gpio-line-names'
  323. * are less than the offset in the device itself. This means all the
  324. * lines are not present for every single pin within all the internal
  325. * gpiochips.
  326. */
  327. if (count <= chip->offset) {
  328. dev_warn(&gdev->dev, "gpio-line-names too short (length %d), cannot map names for the gpiochip at offset %u\n",
  329. count, chip->offset);
  330. return 0;
  331. }
  332. names = kcalloc(count, sizeof(*names), GFP_KERNEL);
  333. if (!names)
  334. return -ENOMEM;
  335. ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
  336. names, count);
  337. if (ret < 0) {
  338. dev_warn(&gdev->dev, "failed to read GPIO line names\n");
  339. kfree(names);
  340. return ret;
  341. }
  342. /*
  343. * When more that one gpiochip per device is used, 'count' can
  344. * contain at most number gpiochips x chip->ngpio. We have to
  345. * correctly distribute all defined lines taking into account
  346. * chip->offset as starting point from where we will assign
  347. * the names to pins from the 'names' array. Since property
  348. * 'gpio-line-names' cannot contains gaps, we have to be sure
  349. * we only assign those pins that really exists since chip->ngpio
  350. * can be different of the chip->offset.
  351. */
  352. count = (count > chip->offset) ? count - chip->offset : count;
  353. if (count > chip->ngpio)
  354. count = chip->ngpio;
  355. for (i = 0; i < count; i++) {
  356. /*
  357. * Allow overriding "fixed" names provided by the GPIO
  358. * provider. The "fixed" names are more often than not
  359. * generic and less informative than the names given in
  360. * device properties.
  361. */
  362. if (names[chip->offset + i] && names[chip->offset + i][0])
  363. gdev->descs[i].name = names[chip->offset + i];
  364. }
  365. kfree(names);
  366. return 0;
  367. }
  368. static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
  369. {
  370. unsigned long *p;
  371. p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
  372. if (!p)
  373. return NULL;
  374. /* Assume by default all GPIOs are valid */
  375. bitmap_fill(p, gc->ngpio);
  376. return p;
  377. }
  378. static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
  379. {
  380. if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
  381. return 0;
  382. gc->valid_mask = gpiochip_allocate_mask(gc);
  383. if (!gc->valid_mask)
  384. return -ENOMEM;
  385. return 0;
  386. }
  387. static int gpiochip_init_valid_mask(struct gpio_chip *gc)
  388. {
  389. if (gc->init_valid_mask)
  390. return gc->init_valid_mask(gc,
  391. gc->valid_mask,
  392. gc->ngpio);
  393. return 0;
  394. }
  395. static void gpiochip_free_valid_mask(struct gpio_chip *gc)
  396. {
  397. bitmap_free(gc->valid_mask);
  398. gc->valid_mask = NULL;
  399. }
  400. static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
  401. {
  402. if (gc->add_pin_ranges)
  403. return gc->add_pin_ranges(gc);
  404. return 0;
  405. }
  406. bool gpiochip_line_is_valid(const struct gpio_chip *gc,
  407. unsigned int offset)
  408. {
  409. /* No mask means all valid */
  410. if (likely(!gc->valid_mask))
  411. return true;
  412. return test_bit(offset, gc->valid_mask);
  413. }
  414. EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
  415. static void gpiodevice_release(struct device *dev)
  416. {
  417. struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
  418. unsigned long flags;
  419. spin_lock_irqsave(&gpio_lock, flags);
  420. list_del(&gdev->list);
  421. spin_unlock_irqrestore(&gpio_lock, flags);
  422. ida_free(&gpio_ida, gdev->id);
  423. kfree_const(gdev->label);
  424. kfree(gdev->descs);
  425. kfree(gdev);
  426. }
  427. #ifdef CONFIG_GPIO_CDEV
  428. #define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
  429. #define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
  430. #else
  431. /*
  432. * gpiolib_cdev_register() indirectly calls device_add(), which is still
  433. * required even when cdev is not selected.
  434. */
  435. #define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
  436. #define gcdev_unregister(gdev) device_del(&(gdev)->dev)
  437. #endif
  438. static int gpiochip_setup_dev(struct gpio_device *gdev)
  439. {
  440. int ret;
  441. ret = gcdev_register(gdev, gpio_devt);
  442. if (ret)
  443. return ret;
  444. /* From this point, the .release() function cleans up gpio_device */
  445. gdev->dev.release = gpiodevice_release;
  446. ret = gpiochip_sysfs_register(gdev);
  447. if (ret)
  448. goto err_remove_device;
  449. dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
  450. gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
  451. return 0;
  452. err_remove_device:
  453. gcdev_unregister(gdev);
  454. return ret;
  455. }
  456. static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
  457. {
  458. struct gpio_desc *desc;
  459. int rv;
  460. desc = gpiochip_get_desc(gc, hog->chip_hwnum);
  461. if (IS_ERR(desc)) {
  462. chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
  463. PTR_ERR(desc));
  464. return;
  465. }
  466. if (test_bit(FLAG_IS_HOGGED, &desc->flags))
  467. return;
  468. rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
  469. if (rv)
  470. gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
  471. __func__, gc->label, hog->chip_hwnum, rv);
  472. }
  473. static void machine_gpiochip_add(struct gpio_chip *gc)
  474. {
  475. struct gpiod_hog *hog;
  476. mutex_lock(&gpio_machine_hogs_mutex);
  477. list_for_each_entry(hog, &gpio_machine_hogs, list) {
  478. if (!strcmp(gc->label, hog->chip_label))
  479. gpiochip_machine_hog(gc, hog);
  480. }
  481. mutex_unlock(&gpio_machine_hogs_mutex);
  482. }
  483. static void gpiochip_setup_devs(void)
  484. {
  485. struct gpio_device *gdev;
  486. int ret;
  487. list_for_each_entry(gdev, &gpio_devices, list) {
  488. ret = gpiochip_setup_dev(gdev);
  489. if (ret)
  490. dev_err(&gdev->dev,
  491. "Failed to initialize gpio device (%d)\n", ret);
  492. }
  493. }
  494. int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
  495. struct lock_class_key *lock_key,
  496. struct lock_class_key *request_key)
  497. {
  498. struct fwnode_handle *fwnode = NULL;
  499. struct gpio_device *gdev;
  500. unsigned long flags;
  501. unsigned int i;
  502. u32 ngpios = 0;
  503. int base = 0;
  504. int ret = 0;
  505. if (gc->fwnode)
  506. fwnode = gc->fwnode;
  507. else if (gc->parent)
  508. fwnode = dev_fwnode(gc->parent);
  509. /*
  510. * First: allocate and populate the internal stat container, and
  511. * set up the struct device.
  512. */
  513. gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  514. if (!gdev)
  515. return -ENOMEM;
  516. gdev->dev.bus = &gpio_bus_type;
  517. gdev->dev.parent = gc->parent;
  518. gdev->chip = gc;
  519. gc->gpiodev = gdev;
  520. of_gpio_dev_init(gc, gdev);
  521. acpi_gpio_dev_init(gc, gdev);
  522. /*
  523. * Assign fwnode depending on the result of the previous calls,
  524. * if none of them succeed, assign it to the parent's one.
  525. */
  526. gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode;
  527. gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
  528. if (gdev->id < 0) {
  529. ret = gdev->id;
  530. goto err_free_gdev;
  531. }
  532. ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
  533. if (ret)
  534. goto err_free_ida;
  535. device_initialize(&gdev->dev);
  536. if (gc->parent && gc->parent->driver)
  537. gdev->owner = gc->parent->driver->owner;
  538. else if (gc->owner)
  539. /* TODO: remove chip->owner */
  540. gdev->owner = gc->owner;
  541. else
  542. gdev->owner = THIS_MODULE;
  543. /*
  544. * Try the device properties if the driver didn't supply the number
  545. * of GPIO lines.
  546. */
  547. ngpios = gc->ngpio;
  548. if (ngpios == 0) {
  549. ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
  550. if (ret == -ENODATA)
  551. /*
  552. * -ENODATA means that there is no property found and
  553. * we want to issue the error message to the user.
  554. * Besides that, we want to return different error code
  555. * to state that supplied value is not valid.
  556. */
  557. ngpios = 0;
  558. else if (ret)
  559. goto err_free_dev_name;
  560. gc->ngpio = ngpios;
  561. }
  562. if (gc->ngpio == 0) {
  563. chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
  564. ret = -EINVAL;
  565. goto err_free_dev_name;
  566. }
  567. if (gc->ngpio > FASTPATH_NGPIO)
  568. chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
  569. gc->ngpio, FASTPATH_NGPIO);
  570. gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
  571. if (!gdev->descs) {
  572. ret = -ENOMEM;
  573. goto err_free_dev_name;
  574. }
  575. gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
  576. if (!gdev->label) {
  577. ret = -ENOMEM;
  578. goto err_free_descs;
  579. }
  580. gdev->ngpio = gc->ngpio;
  581. gdev->data = data;
  582. spin_lock_irqsave(&gpio_lock, flags);
  583. /*
  584. * TODO: this allocates a Linux GPIO number base in the global
  585. * GPIO numberspace for this chip. In the long run we want to
  586. * get *rid* of this numberspace and use only descriptors, but
  587. * it may be a pipe dream. It will not happen before we get rid
  588. * of the sysfs interface anyways.
  589. */
  590. base = gc->base;
  591. if (base < 0) {
  592. base = gpiochip_find_base(gc->ngpio);
  593. if (base < 0) {
  594. spin_unlock_irqrestore(&gpio_lock, flags);
  595. ret = base;
  596. base = 0;
  597. goto err_free_label;
  598. }
  599. /*
  600. * TODO: it should not be necessary to reflect the assigned
  601. * base outside of the GPIO subsystem. Go over drivers and
  602. * see if anyone makes use of this, else drop this and assign
  603. * a poison instead.
  604. */
  605. gc->base = base;
  606. }
  607. gdev->base = base;
  608. ret = gpiodev_add_to_list(gdev);
  609. if (ret) {
  610. spin_unlock_irqrestore(&gpio_lock, flags);
  611. chip_err(gc, "GPIO integer space overlap, cannot add chip\n");
  612. goto err_free_label;
  613. }
  614. for (i = 0; i < gc->ngpio; i++)
  615. gdev->descs[i].gdev = gdev;
  616. spin_unlock_irqrestore(&gpio_lock, flags);
  617. BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
  618. init_rwsem(&gdev->sem);
  619. #ifdef CONFIG_PINCTRL
  620. INIT_LIST_HEAD(&gdev->pin_ranges);
  621. #endif
  622. if (gc->names) {
  623. ret = gpiochip_set_desc_names(gc);
  624. if (ret)
  625. goto err_remove_from_list;
  626. }
  627. ret = devprop_gpiochip_set_names(gc);
  628. if (ret)
  629. goto err_remove_from_list;
  630. ret = gpiochip_alloc_valid_mask(gc);
  631. if (ret)
  632. goto err_remove_from_list;
  633. ret = of_gpiochip_add(gc);
  634. if (ret)
  635. goto err_free_gpiochip_mask;
  636. ret = gpiochip_init_valid_mask(gc);
  637. if (ret)
  638. goto err_remove_of_chip;
  639. for (i = 0; i < gc->ngpio; i++) {
  640. struct gpio_desc *desc = &gdev->descs[i];
  641. if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
  642. assign_bit(FLAG_IS_OUT,
  643. &desc->flags, !gc->get_direction(gc, i));
  644. } else {
  645. assign_bit(FLAG_IS_OUT,
  646. &desc->flags, !gc->direction_input);
  647. }
  648. }
  649. ret = gpiochip_add_pin_ranges(gc);
  650. if (ret)
  651. goto err_remove_of_chip;
  652. acpi_gpiochip_add(gc);
  653. machine_gpiochip_add(gc);
  654. ret = gpiochip_irqchip_init_valid_mask(gc);
  655. if (ret)
  656. goto err_remove_acpi_chip;
  657. ret = gpiochip_irqchip_init_hw(gc);
  658. if (ret)
  659. goto err_remove_acpi_chip;
  660. ret = gpiochip_add_irqchip(gc, lock_key, request_key);
  661. if (ret)
  662. goto err_remove_irqchip_mask;
  663. /*
  664. * By first adding the chardev, and then adding the device,
  665. * we get a device node entry in sysfs under
  666. * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
  667. * coldplug of device nodes and other udev business.
  668. * We can do this only if gpiolib has been initialized.
  669. * Otherwise, defer until later.
  670. */
  671. if (gpiolib_initialized) {
  672. ret = gpiochip_setup_dev(gdev);
  673. if (ret)
  674. goto err_remove_irqchip;
  675. }
  676. return 0;
  677. err_remove_irqchip:
  678. gpiochip_irqchip_remove(gc);
  679. err_remove_irqchip_mask:
  680. gpiochip_irqchip_free_valid_mask(gc);
  681. err_remove_acpi_chip:
  682. acpi_gpiochip_remove(gc);
  683. err_remove_of_chip:
  684. gpiochip_free_hogs(gc);
  685. of_gpiochip_remove(gc);
  686. err_free_gpiochip_mask:
  687. gpiochip_remove_pin_ranges(gc);
  688. gpiochip_free_valid_mask(gc);
  689. if (gdev->dev.release) {
  690. /* release() has been registered by gpiochip_setup_dev() */
  691. put_device(&gdev->dev);
  692. goto err_print_message;
  693. }
  694. err_remove_from_list:
  695. spin_lock_irqsave(&gpio_lock, flags);
  696. list_del(&gdev->list);
  697. spin_unlock_irqrestore(&gpio_lock, flags);
  698. err_free_label:
  699. kfree_const(gdev->label);
  700. err_free_descs:
  701. kfree(gdev->descs);
  702. err_free_dev_name:
  703. kfree(dev_name(&gdev->dev));
  704. err_free_ida:
  705. ida_free(&gpio_ida, gdev->id);
  706. err_free_gdev:
  707. kfree(gdev);
  708. err_print_message:
  709. /* failures here can mean systems won't boot... */
  710. if (ret != -EPROBE_DEFER) {
  711. pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
  712. base, base + (int)ngpios - 1,
  713. gc->label ? : "generic", ret);
  714. }
  715. return ret;
  716. }
  717. EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
  718. /**
  719. * gpiochip_get_data() - get per-subdriver data for the chip
  720. * @gc: GPIO chip
  721. *
  722. * Returns:
  723. * The per-subdriver data for the chip.
  724. */
  725. void *gpiochip_get_data(struct gpio_chip *gc)
  726. {
  727. return gc->gpiodev->data;
  728. }
  729. EXPORT_SYMBOL_GPL(gpiochip_get_data);
  730. /**
  731. * gpiochip_remove() - unregister a gpio_chip
  732. * @gc: the chip to unregister
  733. *
  734. * A gpio_chip with any GPIOs still requested may not be removed.
  735. */
  736. void gpiochip_remove(struct gpio_chip *gc)
  737. {
  738. struct gpio_device *gdev = gc->gpiodev;
  739. unsigned long flags;
  740. unsigned int i;
  741. down_write(&gdev->sem);
  742. /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
  743. gpiochip_sysfs_unregister(gdev);
  744. gpiochip_free_hogs(gc);
  745. /* Numb the device, cancelling all outstanding operations */
  746. gdev->chip = NULL;
  747. gpiochip_irqchip_remove(gc);
  748. acpi_gpiochip_remove(gc);
  749. of_gpiochip_remove(gc);
  750. gpiochip_remove_pin_ranges(gc);
  751. gpiochip_free_valid_mask(gc);
  752. /*
  753. * We accept no more calls into the driver from this point, so
  754. * NULL the driver data pointer
  755. */
  756. gdev->data = NULL;
  757. spin_lock_irqsave(&gpio_lock, flags);
  758. for (i = 0; i < gdev->ngpio; i++) {
  759. if (gpiochip_is_requested(gc, i))
  760. break;
  761. }
  762. spin_unlock_irqrestore(&gpio_lock, flags);
  763. if (i != gdev->ngpio)
  764. dev_crit(&gdev->dev,
  765. "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
  766. /*
  767. * The gpiochip side puts its use of the device to rest here:
  768. * if there are no userspace clients, the chardev and device will
  769. * be removed, else it will be dangling until the last user is
  770. * gone.
  771. */
  772. gcdev_unregister(gdev);
  773. up_write(&gdev->sem);
  774. put_device(&gdev->dev);
  775. }
  776. EXPORT_SYMBOL_GPL(gpiochip_remove);
  777. /**
  778. * gpiochip_find() - iterator for locating a specific gpio_chip
  779. * @data: data to pass to match function
  780. * @match: Callback function to check gpio_chip
  781. *
  782. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  783. * determined by a user supplied @match callback. The callback should return
  784. * 0 if the device doesn't match and non-zero if it does. If the callback is
  785. * non-zero, this function will return to the caller and not iterate over any
  786. * more gpio_chips.
  787. */
  788. struct gpio_chip *gpiochip_find(void *data,
  789. int (*match)(struct gpio_chip *gc,
  790. void *data))
  791. {
  792. struct gpio_device *gdev;
  793. struct gpio_chip *gc = NULL;
  794. unsigned long flags;
  795. spin_lock_irqsave(&gpio_lock, flags);
  796. list_for_each_entry(gdev, &gpio_devices, list)
  797. if (gdev->chip && match(gdev->chip, data)) {
  798. gc = gdev->chip;
  799. break;
  800. }
  801. spin_unlock_irqrestore(&gpio_lock, flags);
  802. return gc;
  803. }
  804. EXPORT_SYMBOL_GPL(gpiochip_find);
  805. static int gpiochip_match_name(struct gpio_chip *gc, void *data)
  806. {
  807. const char *name = data;
  808. return !strcmp(gc->label, name);
  809. }
  810. static struct gpio_chip *find_chip_by_name(const char *name)
  811. {
  812. return gpiochip_find((void *)name, gpiochip_match_name);
  813. }
  814. #ifdef CONFIG_GPIOLIB_IRQCHIP
  815. /*
  816. * The following is irqchip helper code for gpiochips.
  817. */
  818. static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
  819. {
  820. struct gpio_irq_chip *girq = &gc->irq;
  821. if (!girq->init_hw)
  822. return 0;
  823. return girq->init_hw(gc);
  824. }
  825. static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
  826. {
  827. struct gpio_irq_chip *girq = &gc->irq;
  828. if (!girq->init_valid_mask)
  829. return 0;
  830. girq->valid_mask = gpiochip_allocate_mask(gc);
  831. if (!girq->valid_mask)
  832. return -ENOMEM;
  833. girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
  834. return 0;
  835. }
  836. static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
  837. {
  838. bitmap_free(gc->irq.valid_mask);
  839. gc->irq.valid_mask = NULL;
  840. }
  841. bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
  842. unsigned int offset)
  843. {
  844. if (!gpiochip_line_is_valid(gc, offset))
  845. return false;
  846. /* No mask means all valid */
  847. if (likely(!gc->irq.valid_mask))
  848. return true;
  849. return test_bit(offset, gc->irq.valid_mask);
  850. }
  851. EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
  852. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  853. /**
  854. * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
  855. * to a gpiochip
  856. * @gc: the gpiochip to set the irqchip hierarchical handler to
  857. * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
  858. * will then percolate up to the parent
  859. */
  860. static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
  861. struct irq_chip *irqchip)
  862. {
  863. /* DT will deal with mapping each IRQ as we go along */
  864. if (is_of_node(gc->irq.fwnode))
  865. return;
  866. /*
  867. * This is for legacy and boardfile "irqchip" fwnodes: allocate
  868. * irqs upfront instead of dynamically since we don't have the
  869. * dynamic type of allocation that hardware description languages
  870. * provide. Once all GPIO drivers using board files are gone from
  871. * the kernel we can delete this code, but for a transitional period
  872. * it is necessary to keep this around.
  873. */
  874. if (is_fwnode_irqchip(gc->irq.fwnode)) {
  875. int i;
  876. int ret;
  877. for (i = 0; i < gc->ngpio; i++) {
  878. struct irq_fwspec fwspec;
  879. unsigned int parent_hwirq;
  880. unsigned int parent_type;
  881. struct gpio_irq_chip *girq = &gc->irq;
  882. /*
  883. * We call the child to parent translation function
  884. * only to check if the child IRQ is valid or not.
  885. * Just pick the rising edge type here as that is what
  886. * we likely need to support.
  887. */
  888. ret = girq->child_to_parent_hwirq(gc, i,
  889. IRQ_TYPE_EDGE_RISING,
  890. &parent_hwirq,
  891. &parent_type);
  892. if (ret) {
  893. chip_err(gc, "skip set-up on hwirq %d\n",
  894. i);
  895. continue;
  896. }
  897. fwspec.fwnode = gc->irq.fwnode;
  898. /* This is the hwirq for the GPIO line side of things */
  899. fwspec.param[0] = girq->child_offset_to_irq(gc, i);
  900. /* Just pick something */
  901. fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  902. fwspec.param_count = 2;
  903. ret = __irq_domain_alloc_irqs(gc->irq.domain,
  904. /* just pick something */
  905. -1,
  906. 1,
  907. NUMA_NO_NODE,
  908. &fwspec,
  909. false,
  910. NULL);
  911. if (ret < 0) {
  912. chip_err(gc,
  913. "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
  914. i, parent_hwirq,
  915. ret);
  916. }
  917. }
  918. }
  919. chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
  920. return;
  921. }
  922. static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
  923. struct irq_fwspec *fwspec,
  924. unsigned long *hwirq,
  925. unsigned int *type)
  926. {
  927. /* We support standard DT translation */
  928. if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
  929. return irq_domain_translate_twocell(d, fwspec, hwirq, type);
  930. }
  931. /* This is for board files and others not using DT */
  932. if (is_fwnode_irqchip(fwspec->fwnode)) {
  933. int ret;
  934. ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
  935. if (ret)
  936. return ret;
  937. WARN_ON(*type == IRQ_TYPE_NONE);
  938. return 0;
  939. }
  940. return -EINVAL;
  941. }
  942. static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
  943. unsigned int irq,
  944. unsigned int nr_irqs,
  945. void *data)
  946. {
  947. struct gpio_chip *gc = d->host_data;
  948. irq_hw_number_t hwirq;
  949. unsigned int type = IRQ_TYPE_NONE;
  950. struct irq_fwspec *fwspec = data;
  951. union gpio_irq_fwspec gpio_parent_fwspec = {};
  952. unsigned int parent_hwirq;
  953. unsigned int parent_type;
  954. struct gpio_irq_chip *girq = &gc->irq;
  955. int ret;
  956. /*
  957. * The nr_irqs parameter is always one except for PCI multi-MSI
  958. * so this should not happen.
  959. */
  960. WARN_ON(nr_irqs != 1);
  961. ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
  962. if (ret)
  963. return ret;
  964. chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
  965. ret = girq->child_to_parent_hwirq(gc, hwirq, type,
  966. &parent_hwirq, &parent_type);
  967. if (ret) {
  968. chip_err(gc, "can't look up hwirq %lu\n", hwirq);
  969. return ret;
  970. }
  971. chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
  972. /*
  973. * We set handle_bad_irq because the .set_type() should
  974. * always be invoked and set the right type of handler.
  975. */
  976. irq_domain_set_info(d,
  977. irq,
  978. hwirq,
  979. gc->irq.chip,
  980. gc,
  981. girq->handler,
  982. NULL, NULL);
  983. irq_set_probe(irq);
  984. /* This parent only handles asserted level IRQs */
  985. ret = girq->populate_parent_alloc_arg(gc, &gpio_parent_fwspec,
  986. parent_hwirq, parent_type);
  987. if (ret)
  988. return ret;
  989. chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
  990. irq, parent_hwirq);
  991. irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
  992. ret = irq_domain_alloc_irqs_parent(d, irq, 1, &gpio_parent_fwspec);
  993. /*
  994. * If the parent irqdomain is msi, the interrupts have already
  995. * been allocated, so the EEXIST is good.
  996. */
  997. if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
  998. ret = 0;
  999. if (ret)
  1000. chip_err(gc,
  1001. "failed to allocate parent hwirq %d for hwirq %lu\n",
  1002. parent_hwirq, hwirq);
  1003. return ret;
  1004. }
  1005. static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
  1006. unsigned int offset)
  1007. {
  1008. return offset;
  1009. }
  1010. static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
  1011. {
  1012. ops->activate = gpiochip_irq_domain_activate;
  1013. ops->deactivate = gpiochip_irq_domain_deactivate;
  1014. ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
  1015. /*
  1016. * We only allow overriding the translate() and free() functions for
  1017. * hierarchical chips, and this should only be done if the user
  1018. * really need something other than 1:1 translation for translate()
  1019. * callback and free if user wants to free up any resources which
  1020. * were allocated during callbacks, for example populate_parent_alloc_arg.
  1021. */
  1022. if (!ops->translate)
  1023. ops->translate = gpiochip_hierarchy_irq_domain_translate;
  1024. if (!ops->free)
  1025. ops->free = irq_domain_free_irqs_common;
  1026. }
  1027. static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
  1028. {
  1029. if (!gc->irq.child_to_parent_hwirq ||
  1030. !gc->irq.fwnode) {
  1031. chip_err(gc, "missing irqdomain vital data\n");
  1032. return -EINVAL;
  1033. }
  1034. if (!gc->irq.child_offset_to_irq)
  1035. gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
  1036. if (!gc->irq.populate_parent_alloc_arg)
  1037. gc->irq.populate_parent_alloc_arg =
  1038. gpiochip_populate_parent_fwspec_twocell;
  1039. gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
  1040. gc->irq.domain = irq_domain_create_hierarchy(
  1041. gc->irq.parent_domain,
  1042. 0,
  1043. gc->ngpio,
  1044. gc->irq.fwnode,
  1045. &gc->irq.child_irq_domain_ops,
  1046. gc);
  1047. if (!gc->irq.domain)
  1048. return -ENOMEM;
  1049. gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
  1050. return 0;
  1051. }
  1052. static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
  1053. {
  1054. return !!gc->irq.parent_domain;
  1055. }
  1056. int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
  1057. union gpio_irq_fwspec *gfwspec,
  1058. unsigned int parent_hwirq,
  1059. unsigned int parent_type)
  1060. {
  1061. struct irq_fwspec *fwspec = &gfwspec->fwspec;
  1062. fwspec->fwnode = gc->irq.parent_domain->fwnode;
  1063. fwspec->param_count = 2;
  1064. fwspec->param[0] = parent_hwirq;
  1065. fwspec->param[1] = parent_type;
  1066. return 0;
  1067. }
  1068. EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
  1069. int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
  1070. union gpio_irq_fwspec *gfwspec,
  1071. unsigned int parent_hwirq,
  1072. unsigned int parent_type)
  1073. {
  1074. struct irq_fwspec *fwspec = &gfwspec->fwspec;
  1075. fwspec->fwnode = gc->irq.parent_domain->fwnode;
  1076. fwspec->param_count = 4;
  1077. fwspec->param[0] = 0;
  1078. fwspec->param[1] = parent_hwirq;
  1079. fwspec->param[2] = 0;
  1080. fwspec->param[3] = parent_type;
  1081. return 0;
  1082. }
  1083. EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
  1084. #else
  1085. static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
  1086. {
  1087. return -EINVAL;
  1088. }
  1089. static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
  1090. {
  1091. return false;
  1092. }
  1093. #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  1094. /**
  1095. * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
  1096. * @d: the irqdomain used by this irqchip
  1097. * @irq: the global irq number used by this GPIO irqchip irq
  1098. * @hwirq: the local IRQ/GPIO line offset on this gpiochip
  1099. *
  1100. * This function will set up the mapping for a certain IRQ line on a
  1101. * gpiochip by assigning the gpiochip as chip data, and using the irqchip
  1102. * stored inside the gpiochip.
  1103. */
  1104. int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
  1105. irq_hw_number_t hwirq)
  1106. {
  1107. struct gpio_chip *gc = d->host_data;
  1108. int ret = 0;
  1109. if (!gpiochip_irqchip_irq_valid(gc, hwirq))
  1110. return -ENXIO;
  1111. irq_set_chip_data(irq, gc);
  1112. /*
  1113. * This lock class tells lockdep that GPIO irqs are in a different
  1114. * category than their parents, so it won't report false recursion.
  1115. */
  1116. irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
  1117. irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
  1118. /* Chips that use nested thread handlers have them marked */
  1119. if (gc->irq.threaded)
  1120. irq_set_nested_thread(irq, 1);
  1121. irq_set_noprobe(irq);
  1122. if (gc->irq.num_parents == 1)
  1123. ret = irq_set_parent(irq, gc->irq.parents[0]);
  1124. else if (gc->irq.map)
  1125. ret = irq_set_parent(irq, gc->irq.map[hwirq]);
  1126. if (ret < 0)
  1127. return ret;
  1128. /*
  1129. * No set-up of the hardware will happen if IRQ_TYPE_NONE
  1130. * is passed as default type.
  1131. */
  1132. if (gc->irq.default_type != IRQ_TYPE_NONE)
  1133. irq_set_irq_type(irq, gc->irq.default_type);
  1134. return 0;
  1135. }
  1136. EXPORT_SYMBOL_GPL(gpiochip_irq_map);
  1137. void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
  1138. {
  1139. struct gpio_chip *gc = d->host_data;
  1140. if (gc->irq.threaded)
  1141. irq_set_nested_thread(irq, 0);
  1142. irq_set_chip_and_handler(irq, NULL, NULL);
  1143. irq_set_chip_data(irq, NULL);
  1144. }
  1145. EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
  1146. static const struct irq_domain_ops gpiochip_domain_ops = {
  1147. .map = gpiochip_irq_map,
  1148. .unmap = gpiochip_irq_unmap,
  1149. /* Virtually all GPIO irqchips are twocell:ed */
  1150. .xlate = irq_domain_xlate_twocell,
  1151. };
  1152. /*
  1153. * TODO: move these activate/deactivate in under the hierarchicial
  1154. * irqchip implementation as static once SPMI and SSBI (all external
  1155. * users) are phased over.
  1156. */
  1157. /**
  1158. * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
  1159. * @domain: The IRQ domain used by this IRQ chip
  1160. * @data: Outermost irq_data associated with the IRQ
  1161. * @reserve: If set, only reserve an interrupt vector instead of assigning one
  1162. *
  1163. * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
  1164. * used as the activate function for the &struct irq_domain_ops. The host_data
  1165. * for the IRQ domain must be the &struct gpio_chip.
  1166. */
  1167. int gpiochip_irq_domain_activate(struct irq_domain *domain,
  1168. struct irq_data *data, bool reserve)
  1169. {
  1170. struct gpio_chip *gc = domain->host_data;
  1171. return gpiochip_lock_as_irq(gc, data->hwirq);
  1172. }
  1173. EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
  1174. /**
  1175. * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
  1176. * @domain: The IRQ domain used by this IRQ chip
  1177. * @data: Outermost irq_data associated with the IRQ
  1178. *
  1179. * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
  1180. * be used as the deactivate function for the &struct irq_domain_ops. The
  1181. * host_data for the IRQ domain must be the &struct gpio_chip.
  1182. */
  1183. void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
  1184. struct irq_data *data)
  1185. {
  1186. struct gpio_chip *gc = domain->host_data;
  1187. return gpiochip_unlock_as_irq(gc, data->hwirq);
  1188. }
  1189. EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
  1190. static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
  1191. {
  1192. struct irq_domain *domain = gc->irq.domain;
  1193. #ifdef CONFIG_GPIOLIB_IRQCHIP
  1194. /*
  1195. * Avoid race condition with other code, which tries to lookup
  1196. * an IRQ before the irqchip has been properly registered,
  1197. * i.e. while gpiochip is still being brought up.
  1198. */
  1199. if (!gc->irq.initialized)
  1200. return -EPROBE_DEFER;
  1201. #endif
  1202. if (!gpiochip_irqchip_irq_valid(gc, offset))
  1203. return -ENXIO;
  1204. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  1205. if (irq_domain_is_hierarchy(domain)) {
  1206. struct irq_fwspec spec;
  1207. spec.fwnode = domain->fwnode;
  1208. spec.param_count = 2;
  1209. spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
  1210. spec.param[1] = IRQ_TYPE_NONE;
  1211. return irq_create_fwspec_mapping(&spec);
  1212. }
  1213. #endif
  1214. return irq_create_mapping(domain, offset);
  1215. }
  1216. int gpiochip_irq_reqres(struct irq_data *d)
  1217. {
  1218. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1219. return gpiochip_reqres_irq(gc, d->hwirq);
  1220. }
  1221. EXPORT_SYMBOL(gpiochip_irq_reqres);
  1222. void gpiochip_irq_relres(struct irq_data *d)
  1223. {
  1224. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1225. gpiochip_relres_irq(gc, d->hwirq);
  1226. }
  1227. EXPORT_SYMBOL(gpiochip_irq_relres);
  1228. static void gpiochip_irq_mask(struct irq_data *d)
  1229. {
  1230. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1231. if (gc->irq.irq_mask)
  1232. gc->irq.irq_mask(d);
  1233. gpiochip_disable_irq(gc, d->hwirq);
  1234. }
  1235. static void gpiochip_irq_unmask(struct irq_data *d)
  1236. {
  1237. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1238. gpiochip_enable_irq(gc, d->hwirq);
  1239. if (gc->irq.irq_unmask)
  1240. gc->irq.irq_unmask(d);
  1241. }
  1242. static void gpiochip_irq_enable(struct irq_data *d)
  1243. {
  1244. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1245. gpiochip_enable_irq(gc, d->hwirq);
  1246. gc->irq.irq_enable(d);
  1247. }
  1248. static void gpiochip_irq_disable(struct irq_data *d)
  1249. {
  1250. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1251. gc->irq.irq_disable(d);
  1252. gpiochip_disable_irq(gc, d->hwirq);
  1253. }
  1254. static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
  1255. {
  1256. struct irq_chip *irqchip = gc->irq.chip;
  1257. if (irqchip->flags & IRQCHIP_IMMUTABLE)
  1258. return;
  1259. chip_warn(gc, "not an immutable chip, please consider fixing it!\n");
  1260. if (!irqchip->irq_request_resources &&
  1261. !irqchip->irq_release_resources) {
  1262. irqchip->irq_request_resources = gpiochip_irq_reqres;
  1263. irqchip->irq_release_resources = gpiochip_irq_relres;
  1264. }
  1265. if (WARN_ON(gc->irq.irq_enable))
  1266. return;
  1267. /* Check if the irqchip already has this hook... */
  1268. if (irqchip->irq_enable == gpiochip_irq_enable ||
  1269. irqchip->irq_mask == gpiochip_irq_mask) {
  1270. /*
  1271. * ...and if so, give a gentle warning that this is bad
  1272. * practice.
  1273. */
  1274. chip_info(gc,
  1275. "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
  1276. return;
  1277. }
  1278. if (irqchip->irq_disable) {
  1279. gc->irq.irq_disable = irqchip->irq_disable;
  1280. irqchip->irq_disable = gpiochip_irq_disable;
  1281. } else {
  1282. gc->irq.irq_mask = irqchip->irq_mask;
  1283. irqchip->irq_mask = gpiochip_irq_mask;
  1284. }
  1285. if (irqchip->irq_enable) {
  1286. gc->irq.irq_enable = irqchip->irq_enable;
  1287. irqchip->irq_enable = gpiochip_irq_enable;
  1288. } else {
  1289. gc->irq.irq_unmask = irqchip->irq_unmask;
  1290. irqchip->irq_unmask = gpiochip_irq_unmask;
  1291. }
  1292. }
  1293. /**
  1294. * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
  1295. * @gc: the GPIO chip to add the IRQ chip to
  1296. * @lock_key: lockdep class for IRQ lock
  1297. * @request_key: lockdep class for IRQ request
  1298. */
  1299. static int gpiochip_add_irqchip(struct gpio_chip *gc,
  1300. struct lock_class_key *lock_key,
  1301. struct lock_class_key *request_key)
  1302. {
  1303. struct fwnode_handle *fwnode = dev_fwnode(&gc->gpiodev->dev);
  1304. struct irq_chip *irqchip = gc->irq.chip;
  1305. unsigned int type;
  1306. unsigned int i;
  1307. if (!irqchip)
  1308. return 0;
  1309. if (gc->irq.parent_handler && gc->can_sleep) {
  1310. chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
  1311. return -EINVAL;
  1312. }
  1313. type = gc->irq.default_type;
  1314. /*
  1315. * Specifying a default trigger is a terrible idea if DT or ACPI is
  1316. * used to configure the interrupts, as you may end up with
  1317. * conflicting triggers. Tell the user, and reset to NONE.
  1318. */
  1319. if (WARN(fwnode && type != IRQ_TYPE_NONE,
  1320. "%pfw: Ignoring %u default trigger\n", fwnode, type))
  1321. type = IRQ_TYPE_NONE;
  1322. if (gc->to_irq)
  1323. chip_warn(gc, "to_irq is redefined in %s and you shouldn't rely on it\n", __func__);
  1324. gc->to_irq = gpiochip_to_irq;
  1325. gc->irq.default_type = type;
  1326. gc->irq.lock_key = lock_key;
  1327. gc->irq.request_key = request_key;
  1328. /* If a parent irqdomain is provided, let's build a hierarchy */
  1329. if (gpiochip_hierarchy_is_hierarchical(gc)) {
  1330. int ret = gpiochip_hierarchy_add_domain(gc);
  1331. if (ret)
  1332. return ret;
  1333. } else {
  1334. /* Some drivers provide custom irqdomain ops */
  1335. gc->irq.domain = irq_domain_create_simple(fwnode,
  1336. gc->ngpio,
  1337. gc->irq.first,
  1338. gc->irq.domain_ops ?: &gpiochip_domain_ops,
  1339. gc);
  1340. if (!gc->irq.domain)
  1341. return -EINVAL;
  1342. }
  1343. if (gc->irq.parent_handler) {
  1344. for (i = 0; i < gc->irq.num_parents; i++) {
  1345. void *data;
  1346. if (gc->irq.per_parent_data)
  1347. data = gc->irq.parent_handler_data_array[i];
  1348. else
  1349. data = gc->irq.parent_handler_data ?: gc;
  1350. /*
  1351. * The parent IRQ chip is already using the chip_data
  1352. * for this IRQ chip, so our callbacks simply use the
  1353. * handler_data.
  1354. */
  1355. irq_set_chained_handler_and_data(gc->irq.parents[i],
  1356. gc->irq.parent_handler,
  1357. data);
  1358. }
  1359. }
  1360. gpiochip_set_irq_hooks(gc);
  1361. /*
  1362. * Using barrier() here to prevent compiler from reordering
  1363. * gc->irq.initialized before initialization of above
  1364. * GPIO chip irq members.
  1365. */
  1366. barrier();
  1367. gc->irq.initialized = true;
  1368. acpi_gpiochip_request_interrupts(gc);
  1369. return 0;
  1370. }
  1371. /**
  1372. * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
  1373. * @gc: the gpiochip to remove the irqchip from
  1374. *
  1375. * This is called only from gpiochip_remove()
  1376. */
  1377. static void gpiochip_irqchip_remove(struct gpio_chip *gc)
  1378. {
  1379. struct irq_chip *irqchip = gc->irq.chip;
  1380. unsigned int offset;
  1381. acpi_gpiochip_free_interrupts(gc);
  1382. if (irqchip && gc->irq.parent_handler) {
  1383. struct gpio_irq_chip *irq = &gc->irq;
  1384. unsigned int i;
  1385. for (i = 0; i < irq->num_parents; i++)
  1386. irq_set_chained_handler_and_data(irq->parents[i],
  1387. NULL, NULL);
  1388. }
  1389. /* Remove all IRQ mappings and delete the domain */
  1390. if (gc->irq.domain) {
  1391. unsigned int irq;
  1392. for (offset = 0; offset < gc->ngpio; offset++) {
  1393. if (!gpiochip_irqchip_irq_valid(gc, offset))
  1394. continue;
  1395. irq = irq_find_mapping(gc->irq.domain, offset);
  1396. irq_dispose_mapping(irq);
  1397. }
  1398. irq_domain_remove(gc->irq.domain);
  1399. }
  1400. if (irqchip && !(irqchip->flags & IRQCHIP_IMMUTABLE)) {
  1401. if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
  1402. irqchip->irq_request_resources = NULL;
  1403. irqchip->irq_release_resources = NULL;
  1404. }
  1405. if (irqchip->irq_enable == gpiochip_irq_enable) {
  1406. irqchip->irq_enable = gc->irq.irq_enable;
  1407. irqchip->irq_disable = gc->irq.irq_disable;
  1408. }
  1409. }
  1410. gc->irq.irq_enable = NULL;
  1411. gc->irq.irq_disable = NULL;
  1412. gc->irq.chip = NULL;
  1413. gpiochip_irqchip_free_valid_mask(gc);
  1414. }
  1415. /**
  1416. * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
  1417. * @gc: the gpiochip to add the irqchip to
  1418. * @domain: the irqdomain to add to the gpiochip
  1419. *
  1420. * This function adds an IRQ domain to the gpiochip.
  1421. */
  1422. int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
  1423. struct irq_domain *domain)
  1424. {
  1425. if (!domain)
  1426. return -EINVAL;
  1427. gc->to_irq = gpiochip_to_irq;
  1428. gc->irq.domain = domain;
  1429. /*
  1430. * Using barrier() here to prevent compiler from reordering
  1431. * gc->irq.initialized before adding irqdomain.
  1432. */
  1433. barrier();
  1434. gc->irq.initialized = true;
  1435. return 0;
  1436. }
  1437. EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
  1438. #else /* CONFIG_GPIOLIB_IRQCHIP */
  1439. static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
  1440. struct lock_class_key *lock_key,
  1441. struct lock_class_key *request_key)
  1442. {
  1443. return 0;
  1444. }
  1445. static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
  1446. static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
  1447. {
  1448. return 0;
  1449. }
  1450. static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
  1451. {
  1452. return 0;
  1453. }
  1454. static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
  1455. { }
  1456. #endif /* CONFIG_GPIOLIB_IRQCHIP */
  1457. /**
  1458. * gpiochip_generic_request() - request the gpio function for a pin
  1459. * @gc: the gpiochip owning the GPIO
  1460. * @offset: the offset of the GPIO to request for GPIO function
  1461. */
  1462. int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
  1463. {
  1464. #ifdef CONFIG_PINCTRL
  1465. if (list_empty(&gc->gpiodev->pin_ranges))
  1466. return 0;
  1467. #endif
  1468. return pinctrl_gpio_request(gc->gpiodev->base + offset);
  1469. }
  1470. EXPORT_SYMBOL_GPL(gpiochip_generic_request);
  1471. /**
  1472. * gpiochip_generic_free() - free the gpio function from a pin
  1473. * @gc: the gpiochip to request the gpio function for
  1474. * @offset: the offset of the GPIO to free from GPIO function
  1475. */
  1476. void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
  1477. {
  1478. #ifdef CONFIG_PINCTRL
  1479. if (list_empty(&gc->gpiodev->pin_ranges))
  1480. return;
  1481. #endif
  1482. pinctrl_gpio_free(gc->gpiodev->base + offset);
  1483. }
  1484. EXPORT_SYMBOL_GPL(gpiochip_generic_free);
  1485. /**
  1486. * gpiochip_generic_config() - apply configuration for a pin
  1487. * @gc: the gpiochip owning the GPIO
  1488. * @offset: the offset of the GPIO to apply the configuration
  1489. * @config: the configuration to be applied
  1490. */
  1491. int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
  1492. unsigned long config)
  1493. {
  1494. return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
  1495. }
  1496. EXPORT_SYMBOL_GPL(gpiochip_generic_config);
  1497. #ifdef CONFIG_PINCTRL
  1498. /**
  1499. * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
  1500. * @gc: the gpiochip to add the range for
  1501. * @pctldev: the pin controller to map to
  1502. * @gpio_offset: the start offset in the current gpio_chip number space
  1503. * @pin_group: name of the pin group inside the pin controller
  1504. *
  1505. * Calling this function directly from a DeviceTree-supported
  1506. * pinctrl driver is DEPRECATED. Please see Section 2.1 of
  1507. * Documentation/devicetree/bindings/gpio/gpio.txt on how to
  1508. * bind pinctrl and gpio drivers via the "gpio-ranges" property.
  1509. */
  1510. int gpiochip_add_pingroup_range(struct gpio_chip *gc,
  1511. struct pinctrl_dev *pctldev,
  1512. unsigned int gpio_offset, const char *pin_group)
  1513. {
  1514. struct gpio_pin_range *pin_range;
  1515. struct gpio_device *gdev = gc->gpiodev;
  1516. int ret;
  1517. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1518. if (!pin_range) {
  1519. chip_err(gc, "failed to allocate pin ranges\n");
  1520. return -ENOMEM;
  1521. }
  1522. /* Use local offset as range ID */
  1523. pin_range->range.id = gpio_offset;
  1524. pin_range->range.gc = gc;
  1525. pin_range->range.name = gc->label;
  1526. pin_range->range.base = gdev->base + gpio_offset;
  1527. pin_range->pctldev = pctldev;
  1528. ret = pinctrl_get_group_pins(pctldev, pin_group,
  1529. &pin_range->range.pins,
  1530. &pin_range->range.npins);
  1531. if (ret < 0) {
  1532. kfree(pin_range);
  1533. return ret;
  1534. }
  1535. pinctrl_add_gpio_range(pctldev, &pin_range->range);
  1536. chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
  1537. gpio_offset, gpio_offset + pin_range->range.npins - 1,
  1538. pinctrl_dev_get_devname(pctldev), pin_group);
  1539. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1540. return 0;
  1541. }
  1542. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  1543. /**
  1544. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  1545. * @gc: the gpiochip to add the range for
  1546. * @pinctl_name: the dev_name() of the pin controller to map to
  1547. * @gpio_offset: the start offset in the current gpio_chip number space
  1548. * @pin_offset: the start offset in the pin controller number space
  1549. * @npins: the number of pins from the offset of each pin space (GPIO and
  1550. * pin controller) to accumulate in this range
  1551. *
  1552. * Returns:
  1553. * 0 on success, or a negative error-code on failure.
  1554. *
  1555. * Calling this function directly from a DeviceTree-supported
  1556. * pinctrl driver is DEPRECATED. Please see Section 2.1 of
  1557. * Documentation/devicetree/bindings/gpio/gpio.txt on how to
  1558. * bind pinctrl and gpio drivers via the "gpio-ranges" property.
  1559. */
  1560. int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
  1561. unsigned int gpio_offset, unsigned int pin_offset,
  1562. unsigned int npins)
  1563. {
  1564. struct gpio_pin_range *pin_range;
  1565. struct gpio_device *gdev = gc->gpiodev;
  1566. int ret;
  1567. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1568. if (!pin_range) {
  1569. chip_err(gc, "failed to allocate pin ranges\n");
  1570. return -ENOMEM;
  1571. }
  1572. /* Use local offset as range ID */
  1573. pin_range->range.id = gpio_offset;
  1574. pin_range->range.gc = gc;
  1575. pin_range->range.name = gc->label;
  1576. pin_range->range.base = gdev->base + gpio_offset;
  1577. pin_range->range.pin_base = pin_offset;
  1578. pin_range->range.npins = npins;
  1579. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1580. &pin_range->range);
  1581. if (IS_ERR(pin_range->pctldev)) {
  1582. ret = PTR_ERR(pin_range->pctldev);
  1583. chip_err(gc, "could not create pin range\n");
  1584. kfree(pin_range);
  1585. return ret;
  1586. }
  1587. chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1588. gpio_offset, gpio_offset + npins - 1,
  1589. pinctl_name,
  1590. pin_offset, pin_offset + npins - 1);
  1591. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  1592. return 0;
  1593. }
  1594. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1595. /**
  1596. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1597. * @gc: the chip to remove all the mappings for
  1598. */
  1599. void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
  1600. {
  1601. struct gpio_pin_range *pin_range, *tmp;
  1602. struct gpio_device *gdev = gc->gpiodev;
  1603. list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
  1604. list_del(&pin_range->node);
  1605. pinctrl_remove_gpio_range(pin_range->pctldev,
  1606. &pin_range->range);
  1607. kfree(pin_range);
  1608. }
  1609. }
  1610. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1611. #endif /* CONFIG_PINCTRL */
  1612. /* These "optional" allocation calls help prevent drivers from stomping
  1613. * on each other, and help provide better diagnostics in debugfs.
  1614. * They're called even less than the "set direction" calls.
  1615. */
  1616. static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
  1617. {
  1618. struct gpio_chip *gc = desc->gdev->chip;
  1619. int ret;
  1620. unsigned long flags;
  1621. unsigned offset;
  1622. if (label) {
  1623. label = kstrdup_const(label, GFP_KERNEL);
  1624. if (!label)
  1625. return -ENOMEM;
  1626. }
  1627. spin_lock_irqsave(&gpio_lock, flags);
  1628. /* NOTE: gpio_request() can be called in early boot,
  1629. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1630. */
  1631. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1632. desc_set_label(desc, label ? : "?");
  1633. } else {
  1634. ret = -EBUSY;
  1635. goto out_free_unlock;
  1636. }
  1637. if (gc->request) {
  1638. /* gc->request may sleep */
  1639. spin_unlock_irqrestore(&gpio_lock, flags);
  1640. offset = gpio_chip_hwgpio(desc);
  1641. if (gpiochip_line_is_valid(gc, offset))
  1642. ret = gc->request(gc, offset);
  1643. else
  1644. ret = -EINVAL;
  1645. spin_lock_irqsave(&gpio_lock, flags);
  1646. if (ret) {
  1647. desc_set_label(desc, NULL);
  1648. clear_bit(FLAG_REQUESTED, &desc->flags);
  1649. goto out_free_unlock;
  1650. }
  1651. }
  1652. if (gc->get_direction) {
  1653. /* gc->get_direction may sleep */
  1654. spin_unlock_irqrestore(&gpio_lock, flags);
  1655. gpiod_get_direction(desc);
  1656. spin_lock_irqsave(&gpio_lock, flags);
  1657. }
  1658. spin_unlock_irqrestore(&gpio_lock, flags);
  1659. return 0;
  1660. out_free_unlock:
  1661. spin_unlock_irqrestore(&gpio_lock, flags);
  1662. kfree_const(label);
  1663. return ret;
  1664. }
  1665. /*
  1666. * This descriptor validation needs to be inserted verbatim into each
  1667. * function taking a descriptor, so we need to use a preprocessor
  1668. * macro to avoid endless duplication. If the desc is NULL it is an
  1669. * optional GPIO and calls should just bail out.
  1670. */
  1671. static int validate_desc(const struct gpio_desc *desc, const char *func)
  1672. {
  1673. if (!desc)
  1674. return 0;
  1675. if (IS_ERR(desc)) {
  1676. pr_warn("%s: invalid GPIO (errorpointer)\n", func);
  1677. return PTR_ERR(desc);
  1678. }
  1679. if (!desc->gdev) {
  1680. pr_warn("%s: invalid GPIO (no device)\n", func);
  1681. return -EINVAL;
  1682. }
  1683. if (!desc->gdev->chip) {
  1684. dev_warn(&desc->gdev->dev,
  1685. "%s: backing chip is gone\n", func);
  1686. return 0;
  1687. }
  1688. return 1;
  1689. }
  1690. #define VALIDATE_DESC(desc) do { \
  1691. int __valid = validate_desc(desc, __func__); \
  1692. if (__valid <= 0) \
  1693. return __valid; \
  1694. } while (0)
  1695. #define VALIDATE_DESC_VOID(desc) do { \
  1696. int __valid = validate_desc(desc, __func__); \
  1697. if (__valid <= 0) \
  1698. return; \
  1699. } while (0)
  1700. int gpiod_request(struct gpio_desc *desc, const char *label)
  1701. {
  1702. int ret = -EPROBE_DEFER;
  1703. struct gpio_device *gdev;
  1704. VALIDATE_DESC(desc);
  1705. gdev = desc->gdev;
  1706. if (try_module_get(gdev->owner)) {
  1707. ret = gpiod_request_commit(desc, label);
  1708. if (ret)
  1709. module_put(gdev->owner);
  1710. else
  1711. get_device(&gdev->dev);
  1712. }
  1713. if (ret)
  1714. gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
  1715. return ret;
  1716. }
  1717. static bool gpiod_free_commit(struct gpio_desc *desc)
  1718. {
  1719. bool ret = false;
  1720. unsigned long flags;
  1721. struct gpio_chip *gc;
  1722. might_sleep();
  1723. gpiod_unexport(desc);
  1724. spin_lock_irqsave(&gpio_lock, flags);
  1725. gc = desc->gdev->chip;
  1726. if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1727. if (gc->free) {
  1728. spin_unlock_irqrestore(&gpio_lock, flags);
  1729. might_sleep_if(gc->can_sleep);
  1730. gc->free(gc, gpio_chip_hwgpio(desc));
  1731. spin_lock_irqsave(&gpio_lock, flags);
  1732. }
  1733. kfree_const(desc->label);
  1734. desc_set_label(desc, NULL);
  1735. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1736. clear_bit(FLAG_REQUESTED, &desc->flags);
  1737. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1738. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1739. clear_bit(FLAG_PULL_UP, &desc->flags);
  1740. clear_bit(FLAG_PULL_DOWN, &desc->flags);
  1741. clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
  1742. clear_bit(FLAG_EDGE_RISING, &desc->flags);
  1743. clear_bit(FLAG_EDGE_FALLING, &desc->flags);
  1744. clear_bit(FLAG_IS_HOGGED, &desc->flags);
  1745. #ifdef CONFIG_OF_DYNAMIC
  1746. desc->hog = NULL;
  1747. #endif
  1748. #ifdef CONFIG_GPIO_CDEV
  1749. WRITE_ONCE(desc->debounce_period_us, 0);
  1750. #endif
  1751. ret = true;
  1752. }
  1753. spin_unlock_irqrestore(&gpio_lock, flags);
  1754. blocking_notifier_call_chain(&desc->gdev->notifier,
  1755. GPIOLINE_CHANGED_RELEASED, desc);
  1756. return ret;
  1757. }
  1758. void gpiod_free(struct gpio_desc *desc)
  1759. {
  1760. if (desc && desc->gdev && gpiod_free_commit(desc)) {
  1761. module_put(desc->gdev->owner);
  1762. put_device(&desc->gdev->dev);
  1763. } else {
  1764. WARN_ON(extra_checks);
  1765. }
  1766. }
  1767. /**
  1768. * gpiochip_is_requested - return string iff signal was requested
  1769. * @gc: controller managing the signal
  1770. * @offset: of signal within controller's 0..(ngpio - 1) range
  1771. *
  1772. * Returns NULL if the GPIO is not currently requested, else a string.
  1773. * The string returned is the label passed to gpio_request(); if none has been
  1774. * passed it is a meaningless, non-NULL constant.
  1775. *
  1776. * This function is for use by GPIO controller drivers. The label can
  1777. * help with diagnostics, and knowing that the signal is used as a GPIO
  1778. * can help avoid accidentally multiplexing it to another controller.
  1779. */
  1780. const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
  1781. {
  1782. struct gpio_desc *desc;
  1783. desc = gpiochip_get_desc(gc, offset);
  1784. if (IS_ERR(desc))
  1785. return NULL;
  1786. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  1787. return NULL;
  1788. return desc->label;
  1789. }
  1790. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1791. /**
  1792. * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
  1793. * @gc: GPIO chip
  1794. * @hwnum: hardware number of the GPIO for which to request the descriptor
  1795. * @label: label for the GPIO
  1796. * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
  1797. * specify things like line inversion semantics with the machine flags
  1798. * such as GPIO_OUT_LOW
  1799. * @dflags: descriptor request flags for this GPIO or 0 if default, this
  1800. * can be used to specify consumer semantics such as open drain
  1801. *
  1802. * Function allows GPIO chip drivers to request and use their own GPIO
  1803. * descriptors via gpiolib API. Difference to gpiod_request() is that this
  1804. * function will not increase reference count of the GPIO chip module. This
  1805. * allows the GPIO chip module to be unloaded as needed (we assume that the
  1806. * GPIO chip driver handles freeing the GPIOs it has requested).
  1807. *
  1808. * Returns:
  1809. * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
  1810. * code on failure.
  1811. */
  1812. struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
  1813. unsigned int hwnum,
  1814. const char *label,
  1815. enum gpio_lookup_flags lflags,
  1816. enum gpiod_flags dflags)
  1817. {
  1818. struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
  1819. int ret;
  1820. if (IS_ERR(desc)) {
  1821. chip_err(gc, "failed to get GPIO descriptor\n");
  1822. return desc;
  1823. }
  1824. ret = gpiod_request_commit(desc, label);
  1825. if (ret < 0)
  1826. return ERR_PTR(ret);
  1827. ret = gpiod_configure_flags(desc, label, lflags, dflags);
  1828. if (ret) {
  1829. chip_err(gc, "setup of own GPIO %s failed\n", label);
  1830. gpiod_free_commit(desc);
  1831. return ERR_PTR(ret);
  1832. }
  1833. return desc;
  1834. }
  1835. EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
  1836. /**
  1837. * gpiochip_free_own_desc - Free GPIO requested by the chip driver
  1838. * @desc: GPIO descriptor to free
  1839. *
  1840. * Function frees the given GPIO requested previously with
  1841. * gpiochip_request_own_desc().
  1842. */
  1843. void gpiochip_free_own_desc(struct gpio_desc *desc)
  1844. {
  1845. if (desc)
  1846. gpiod_free_commit(desc);
  1847. }
  1848. EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
  1849. /*
  1850. * Drivers MUST set GPIO direction before making get/set calls. In
  1851. * some cases this is done in early boot, before IRQs are enabled.
  1852. *
  1853. * As a rule these aren't called more than once (except for drivers
  1854. * using the open-drain emulation idiom) so these are natural places
  1855. * to accumulate extra debugging checks. Note that we can't (yet)
  1856. * rely on gpio_request() having been called beforehand.
  1857. */
  1858. static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
  1859. unsigned long config)
  1860. {
  1861. if (!gc->set_config)
  1862. return -ENOTSUPP;
  1863. return gc->set_config(gc, offset, config);
  1864. }
  1865. static int gpio_set_config_with_argument(struct gpio_desc *desc,
  1866. enum pin_config_param mode,
  1867. u32 argument)
  1868. {
  1869. struct gpio_chip *gc = desc->gdev->chip;
  1870. unsigned long config;
  1871. config = pinconf_to_config_packed(mode, argument);
  1872. return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
  1873. }
  1874. static int gpio_set_config_with_argument_optional(struct gpio_desc *desc,
  1875. enum pin_config_param mode,
  1876. u32 argument)
  1877. {
  1878. struct device *dev = &desc->gdev->dev;
  1879. int gpio = gpio_chip_hwgpio(desc);
  1880. int ret;
  1881. ret = gpio_set_config_with_argument(desc, mode, argument);
  1882. if (ret != -ENOTSUPP)
  1883. return ret;
  1884. switch (mode) {
  1885. case PIN_CONFIG_PERSIST_STATE:
  1886. dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio);
  1887. break;
  1888. default:
  1889. break;
  1890. }
  1891. return 0;
  1892. }
  1893. static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
  1894. {
  1895. return gpio_set_config_with_argument(desc, mode, 0);
  1896. }
  1897. static int gpio_set_bias(struct gpio_desc *desc)
  1898. {
  1899. enum pin_config_param bias;
  1900. unsigned int arg;
  1901. if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
  1902. bias = PIN_CONFIG_BIAS_DISABLE;
  1903. else if (test_bit(FLAG_PULL_UP, &desc->flags))
  1904. bias = PIN_CONFIG_BIAS_PULL_UP;
  1905. else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
  1906. bias = PIN_CONFIG_BIAS_PULL_DOWN;
  1907. else
  1908. return 0;
  1909. switch (bias) {
  1910. case PIN_CONFIG_BIAS_PULL_DOWN:
  1911. case PIN_CONFIG_BIAS_PULL_UP:
  1912. arg = 1;
  1913. break;
  1914. default:
  1915. arg = 0;
  1916. break;
  1917. }
  1918. return gpio_set_config_with_argument_optional(desc, bias, arg);
  1919. }
  1920. /**
  1921. * gpio_set_debounce_timeout() - Set debounce timeout
  1922. * @desc: GPIO descriptor to set the debounce timeout
  1923. * @debounce: Debounce timeout in microseconds
  1924. *
  1925. * The function calls the certain GPIO driver to set debounce timeout
  1926. * in the hardware.
  1927. *
  1928. * Returns 0 on success, or negative error code otherwise.
  1929. */
  1930. int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
  1931. {
  1932. return gpio_set_config_with_argument_optional(desc,
  1933. PIN_CONFIG_INPUT_DEBOUNCE,
  1934. debounce);
  1935. }
  1936. /**
  1937. * gpiod_direction_input - set the GPIO direction to input
  1938. * @desc: GPIO to set to input
  1939. *
  1940. * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
  1941. * be called safely on it.
  1942. *
  1943. * Return 0 in case of success, else an error code.
  1944. */
  1945. int gpiod_direction_input(struct gpio_desc *desc)
  1946. {
  1947. struct gpio_chip *gc;
  1948. int ret = 0;
  1949. VALIDATE_DESC(desc);
  1950. gc = desc->gdev->chip;
  1951. /*
  1952. * It is legal to have no .get() and .direction_input() specified if
  1953. * the chip is output-only, but you can't specify .direction_input()
  1954. * and not support the .get() operation, that doesn't make sense.
  1955. */
  1956. if (!gc->get && gc->direction_input) {
  1957. gpiod_warn(desc,
  1958. "%s: missing get() but have direction_input()\n",
  1959. __func__);
  1960. return -EIO;
  1961. }
  1962. /*
  1963. * If we have a .direction_input() callback, things are simple,
  1964. * just call it. Else we are some input-only chip so try to check the
  1965. * direction (if .get_direction() is supported) else we silently
  1966. * assume we are in input mode after this.
  1967. */
  1968. if (gc->direction_input) {
  1969. ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
  1970. } else if (gc->get_direction &&
  1971. (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
  1972. gpiod_warn(desc,
  1973. "%s: missing direction_input() operation and line is output\n",
  1974. __func__);
  1975. return -EIO;
  1976. }
  1977. if (ret == 0) {
  1978. clear_bit(FLAG_IS_OUT, &desc->flags);
  1979. ret = gpio_set_bias(desc);
  1980. }
  1981. trace_gpio_direction(desc_to_gpio(desc), 1, ret);
  1982. return ret;
  1983. }
  1984. EXPORT_SYMBOL_GPL(gpiod_direction_input);
  1985. static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
  1986. {
  1987. struct gpio_chip *gc = desc->gdev->chip;
  1988. int val = !!value;
  1989. int ret = 0;
  1990. /*
  1991. * It's OK not to specify .direction_output() if the gpiochip is
  1992. * output-only, but if there is then not even a .set() operation it
  1993. * is pretty tricky to drive the output line.
  1994. */
  1995. if (!gc->set && !gc->direction_output) {
  1996. gpiod_warn(desc,
  1997. "%s: missing set() and direction_output() operations\n",
  1998. __func__);
  1999. return -EIO;
  2000. }
  2001. if (gc->direction_output) {
  2002. ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
  2003. } else {
  2004. /* Check that we are in output mode if we can */
  2005. if (gc->get_direction &&
  2006. gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
  2007. gpiod_warn(desc,
  2008. "%s: missing direction_output() operation\n",
  2009. __func__);
  2010. return -EIO;
  2011. }
  2012. /*
  2013. * If we can't actively set the direction, we are some
  2014. * output-only chip, so just drive the output as desired.
  2015. */
  2016. gc->set(gc, gpio_chip_hwgpio(desc), val);
  2017. }
  2018. if (!ret)
  2019. set_bit(FLAG_IS_OUT, &desc->flags);
  2020. trace_gpio_value(desc_to_gpio(desc), 0, val);
  2021. trace_gpio_direction(desc_to_gpio(desc), 0, ret);
  2022. return ret;
  2023. }
  2024. /**
  2025. * gpiod_direction_output_raw - set the GPIO direction to output
  2026. * @desc: GPIO to set to output
  2027. * @value: initial output value of the GPIO
  2028. *
  2029. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  2030. * be called safely on it. The initial value of the output must be specified
  2031. * as raw value on the physical line without regard for the ACTIVE_LOW status.
  2032. *
  2033. * Return 0 in case of success, else an error code.
  2034. */
  2035. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  2036. {
  2037. VALIDATE_DESC(desc);
  2038. return gpiod_direction_output_raw_commit(desc, value);
  2039. }
  2040. EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
  2041. /**
  2042. * gpiod_direction_output - set the GPIO direction to output
  2043. * @desc: GPIO to set to output
  2044. * @value: initial output value of the GPIO
  2045. *
  2046. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  2047. * be called safely on it. The initial value of the output must be specified
  2048. * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  2049. * account.
  2050. *
  2051. * Return 0 in case of success, else an error code.
  2052. */
  2053. int gpiod_direction_output(struct gpio_desc *desc, int value)
  2054. {
  2055. int ret;
  2056. VALIDATE_DESC(desc);
  2057. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2058. value = !value;
  2059. else
  2060. value = !!value;
  2061. /* GPIOs used for enabled IRQs shall not be set as output */
  2062. if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
  2063. test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
  2064. gpiod_err(desc,
  2065. "%s: tried to set a GPIO tied to an IRQ as output\n",
  2066. __func__);
  2067. return -EIO;
  2068. }
  2069. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
  2070. /* First see if we can enable open drain in hardware */
  2071. ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
  2072. if (!ret)
  2073. goto set_output_value;
  2074. /* Emulate open drain by not actively driving the line high */
  2075. if (value) {
  2076. ret = gpiod_direction_input(desc);
  2077. goto set_output_flag;
  2078. }
  2079. } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
  2080. ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
  2081. if (!ret)
  2082. goto set_output_value;
  2083. /* Emulate open source by not actively driving the line low */
  2084. if (!value) {
  2085. ret = gpiod_direction_input(desc);
  2086. goto set_output_flag;
  2087. }
  2088. } else {
  2089. gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
  2090. }
  2091. set_output_value:
  2092. ret = gpio_set_bias(desc);
  2093. if (ret)
  2094. return ret;
  2095. return gpiod_direction_output_raw_commit(desc, value);
  2096. set_output_flag:
  2097. /*
  2098. * When emulating open-source or open-drain functionalities by not
  2099. * actively driving the line (setting mode to input) we still need to
  2100. * set the IS_OUT flag or otherwise we won't be able to set the line
  2101. * value anymore.
  2102. */
  2103. if (ret == 0)
  2104. set_bit(FLAG_IS_OUT, &desc->flags);
  2105. return ret;
  2106. }
  2107. EXPORT_SYMBOL_GPL(gpiod_direction_output);
  2108. /**
  2109. * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
  2110. *
  2111. * @desc: GPIO to enable.
  2112. * @flags: Flags related to GPIO edge.
  2113. *
  2114. * Return 0 in case of success, else negative error code.
  2115. */
  2116. int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
  2117. {
  2118. int ret = 0;
  2119. struct gpio_chip *gc;
  2120. VALIDATE_DESC(desc);
  2121. gc = desc->gdev->chip;
  2122. if (!gc->en_hw_timestamp) {
  2123. gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
  2124. return -ENOTSUPP;
  2125. }
  2126. ret = gc->en_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
  2127. if (ret)
  2128. gpiod_warn(desc, "%s: hw ts request failed\n", __func__);
  2129. return ret;
  2130. }
  2131. EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns);
  2132. /**
  2133. * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp.
  2134. *
  2135. * @desc: GPIO to disable.
  2136. * @flags: Flags related to GPIO edge, same value as used during enable call.
  2137. *
  2138. * Return 0 in case of success, else negative error code.
  2139. */
  2140. int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
  2141. {
  2142. int ret = 0;
  2143. struct gpio_chip *gc;
  2144. VALIDATE_DESC(desc);
  2145. gc = desc->gdev->chip;
  2146. if (!gc->dis_hw_timestamp) {
  2147. gpiod_warn(desc, "%s: hw ts not supported\n", __func__);
  2148. return -ENOTSUPP;
  2149. }
  2150. ret = gc->dis_hw_timestamp(gc, gpio_chip_hwgpio(desc), flags);
  2151. if (ret)
  2152. gpiod_warn(desc, "%s: hw ts release failed\n", __func__);
  2153. return ret;
  2154. }
  2155. EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
  2156. /**
  2157. * gpiod_set_config - sets @config for a GPIO
  2158. * @desc: descriptor of the GPIO for which to set the configuration
  2159. * @config: Same packed config format as generic pinconf
  2160. *
  2161. * Returns:
  2162. * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
  2163. * configuration.
  2164. */
  2165. int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
  2166. {
  2167. struct gpio_chip *gc;
  2168. VALIDATE_DESC(desc);
  2169. gc = desc->gdev->chip;
  2170. return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
  2171. }
  2172. EXPORT_SYMBOL_GPL(gpiod_set_config);
  2173. /**
  2174. * gpiod_set_debounce - sets @debounce time for a GPIO
  2175. * @desc: descriptor of the GPIO for which to set debounce time
  2176. * @debounce: debounce time in microseconds
  2177. *
  2178. * Returns:
  2179. * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
  2180. * debounce time.
  2181. */
  2182. int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
  2183. {
  2184. unsigned long config;
  2185. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
  2186. return gpiod_set_config(desc, config);
  2187. }
  2188. EXPORT_SYMBOL_GPL(gpiod_set_debounce);
  2189. /**
  2190. * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
  2191. * @desc: descriptor of the GPIO for which to configure persistence
  2192. * @transitory: True to lose state on suspend or reset, false for persistence
  2193. *
  2194. * Returns:
  2195. * 0 on success, otherwise a negative error code.
  2196. */
  2197. int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
  2198. {
  2199. VALIDATE_DESC(desc);
  2200. /*
  2201. * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
  2202. * persistence state.
  2203. */
  2204. assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
  2205. /* If the driver supports it, set the persistence state now */
  2206. return gpio_set_config_with_argument_optional(desc,
  2207. PIN_CONFIG_PERSIST_STATE,
  2208. !transitory);
  2209. }
  2210. EXPORT_SYMBOL_GPL(gpiod_set_transitory);
  2211. /**
  2212. * gpiod_is_active_low - test whether a GPIO is active-low or not
  2213. * @desc: the gpio descriptor to test
  2214. *
  2215. * Returns 1 if the GPIO is active-low, 0 otherwise.
  2216. */
  2217. int gpiod_is_active_low(const struct gpio_desc *desc)
  2218. {
  2219. VALIDATE_DESC(desc);
  2220. return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2221. }
  2222. EXPORT_SYMBOL_GPL(gpiod_is_active_low);
  2223. /**
  2224. * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
  2225. * @desc: the gpio descriptor to change
  2226. */
  2227. void gpiod_toggle_active_low(struct gpio_desc *desc)
  2228. {
  2229. VALIDATE_DESC_VOID(desc);
  2230. change_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2231. }
  2232. EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
  2233. static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc)
  2234. {
  2235. return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO;
  2236. }
  2237. /* I/O calls are only valid after configuration completed; the relevant
  2238. * "is this a valid GPIO" error checks should already have been done.
  2239. *
  2240. * "Get" operations are often inlinable as reading a pin value register,
  2241. * and masking the relevant bit in that register.
  2242. *
  2243. * When "set" operations are inlinable, they involve writing that mask to
  2244. * one register to set a low value, or a different register to set it high.
  2245. * Otherwise locking is needed, so there may be little value to inlining.
  2246. *
  2247. *------------------------------------------------------------------------
  2248. *
  2249. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  2250. * have requested the GPIO. That can include implicit requesting by
  2251. * a direction setting call. Marking a gpio as requested locks its chip
  2252. * in memory, guaranteeing that these table lookups need no more locking
  2253. * and that gpiochip_remove() will fail.
  2254. *
  2255. * REVISIT when debugging, consider adding some instrumentation to ensure
  2256. * that the GPIO was actually requested.
  2257. */
  2258. static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
  2259. {
  2260. struct gpio_chip *gc;
  2261. int value;
  2262. gc = desc->gdev->chip;
  2263. value = gpio_chip_get_value(gc, desc);
  2264. value = value < 0 ? value : !!value;
  2265. trace_gpio_value(desc_to_gpio(desc), 1, value);
  2266. return value;
  2267. }
  2268. static int gpio_chip_get_multiple(struct gpio_chip *gc,
  2269. unsigned long *mask, unsigned long *bits)
  2270. {
  2271. if (gc->get_multiple)
  2272. return gc->get_multiple(gc, mask, bits);
  2273. if (gc->get) {
  2274. int i, value;
  2275. for_each_set_bit(i, mask, gc->ngpio) {
  2276. value = gc->get(gc, i);
  2277. if (value < 0)
  2278. return value;
  2279. __assign_bit(i, bits, value);
  2280. }
  2281. return 0;
  2282. }
  2283. return -EIO;
  2284. }
  2285. int gpiod_get_array_value_complex(bool raw, bool can_sleep,
  2286. unsigned int array_size,
  2287. struct gpio_desc **desc_array,
  2288. struct gpio_array *array_info,
  2289. unsigned long *value_bitmap)
  2290. {
  2291. int ret, i = 0;
  2292. /*
  2293. * Validate array_info against desc_array and its size.
  2294. * It should immediately follow desc_array if both
  2295. * have been obtained from the same gpiod_get_array() call.
  2296. */
  2297. if (array_info && array_info->desc == desc_array &&
  2298. array_size <= array_info->size &&
  2299. (void *)array_info == desc_array + array_info->size) {
  2300. if (!can_sleep)
  2301. WARN_ON(array_info->chip->can_sleep);
  2302. ret = gpio_chip_get_multiple(array_info->chip,
  2303. array_info->get_mask,
  2304. value_bitmap);
  2305. if (ret)
  2306. return ret;
  2307. if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
  2308. bitmap_xor(value_bitmap, value_bitmap,
  2309. array_info->invert_mask, array_size);
  2310. i = find_first_zero_bit(array_info->get_mask, array_size);
  2311. if (i == array_size)
  2312. return 0;
  2313. } else {
  2314. array_info = NULL;
  2315. }
  2316. while (i < array_size) {
  2317. struct gpio_chip *gc = desc_array[i]->gdev->chip;
  2318. DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
  2319. DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
  2320. unsigned long *mask, *bits;
  2321. int first, j;
  2322. if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
  2323. mask = fastpath_mask;
  2324. bits = fastpath_bits;
  2325. } else {
  2326. gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
  2327. mask = bitmap_alloc(gc->ngpio, flags);
  2328. if (!mask)
  2329. return -ENOMEM;
  2330. bits = bitmap_alloc(gc->ngpio, flags);
  2331. if (!bits) {
  2332. bitmap_free(mask);
  2333. return -ENOMEM;
  2334. }
  2335. }
  2336. bitmap_zero(mask, gc->ngpio);
  2337. if (!can_sleep)
  2338. WARN_ON(gc->can_sleep);
  2339. /* collect all inputs belonging to the same chip */
  2340. first = i;
  2341. do {
  2342. const struct gpio_desc *desc = desc_array[i];
  2343. int hwgpio = gpio_chip_hwgpio(desc);
  2344. __set_bit(hwgpio, mask);
  2345. i++;
  2346. if (array_info)
  2347. i = find_next_zero_bit(array_info->get_mask,
  2348. array_size, i);
  2349. } while ((i < array_size) &&
  2350. (desc_array[i]->gdev->chip == gc));
  2351. ret = gpio_chip_get_multiple(gc, mask, bits);
  2352. if (ret) {
  2353. if (mask != fastpath_mask)
  2354. bitmap_free(mask);
  2355. if (bits != fastpath_bits)
  2356. bitmap_free(bits);
  2357. return ret;
  2358. }
  2359. for (j = first; j < i; ) {
  2360. const struct gpio_desc *desc = desc_array[j];
  2361. int hwgpio = gpio_chip_hwgpio(desc);
  2362. int value = test_bit(hwgpio, bits);
  2363. if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2364. value = !value;
  2365. __assign_bit(j, value_bitmap, value);
  2366. trace_gpio_value(desc_to_gpio(desc), 1, value);
  2367. j++;
  2368. if (array_info)
  2369. j = find_next_zero_bit(array_info->get_mask, i,
  2370. j);
  2371. }
  2372. if (mask != fastpath_mask)
  2373. bitmap_free(mask);
  2374. if (bits != fastpath_bits)
  2375. bitmap_free(bits);
  2376. }
  2377. return 0;
  2378. }
  2379. /**
  2380. * gpiod_get_raw_value() - return a gpio's raw value
  2381. * @desc: gpio whose value will be returned
  2382. *
  2383. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  2384. * its ACTIVE_LOW status, or negative errno on failure.
  2385. *
  2386. * This function can be called from contexts where we cannot sleep, and will
  2387. * complain if the GPIO chip functions potentially sleep.
  2388. */
  2389. int gpiod_get_raw_value(const struct gpio_desc *desc)
  2390. {
  2391. VALIDATE_DESC(desc);
  2392. /* Should be using gpiod_get_raw_value_cansleep() */
  2393. WARN_ON(desc->gdev->chip->can_sleep);
  2394. return gpiod_get_raw_value_commit(desc);
  2395. }
  2396. EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
  2397. /**
  2398. * gpiod_get_value() - return a gpio's value
  2399. * @desc: gpio whose value will be returned
  2400. *
  2401. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  2402. * account, or negative errno on failure.
  2403. *
  2404. * This function can be called from contexts where we cannot sleep, and will
  2405. * complain if the GPIO chip functions potentially sleep.
  2406. */
  2407. int gpiod_get_value(const struct gpio_desc *desc)
  2408. {
  2409. int value;
  2410. VALIDATE_DESC(desc);
  2411. /* Should be using gpiod_get_value_cansleep() */
  2412. WARN_ON(desc->gdev->chip->can_sleep);
  2413. value = gpiod_get_raw_value_commit(desc);
  2414. if (value < 0)
  2415. return value;
  2416. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2417. value = !value;
  2418. return value;
  2419. }
  2420. EXPORT_SYMBOL_GPL(gpiod_get_value);
  2421. /**
  2422. * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
  2423. * @array_size: number of elements in the descriptor array / value bitmap
  2424. * @desc_array: array of GPIO descriptors whose values will be read
  2425. * @array_info: information on applicability of fast bitmap processing path
  2426. * @value_bitmap: bitmap to store the read values
  2427. *
  2428. * Read the raw values of the GPIOs, i.e. the values of the physical lines
  2429. * without regard for their ACTIVE_LOW status. Return 0 in case of success,
  2430. * else an error code.
  2431. *
  2432. * This function can be called from contexts where we cannot sleep,
  2433. * and it will complain if the GPIO chip functions potentially sleep.
  2434. */
  2435. int gpiod_get_raw_array_value(unsigned int array_size,
  2436. struct gpio_desc **desc_array,
  2437. struct gpio_array *array_info,
  2438. unsigned long *value_bitmap)
  2439. {
  2440. if (!desc_array)
  2441. return -EINVAL;
  2442. return gpiod_get_array_value_complex(true, false, array_size,
  2443. desc_array, array_info,
  2444. value_bitmap);
  2445. }
  2446. EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
  2447. /**
  2448. * gpiod_get_array_value() - read values from an array of GPIOs
  2449. * @array_size: number of elements in the descriptor array / value bitmap
  2450. * @desc_array: array of GPIO descriptors whose values will be read
  2451. * @array_info: information on applicability of fast bitmap processing path
  2452. * @value_bitmap: bitmap to store the read values
  2453. *
  2454. * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2455. * into account. Return 0 in case of success, else an error code.
  2456. *
  2457. * This function can be called from contexts where we cannot sleep,
  2458. * and it will complain if the GPIO chip functions potentially sleep.
  2459. */
  2460. int gpiod_get_array_value(unsigned int array_size,
  2461. struct gpio_desc **desc_array,
  2462. struct gpio_array *array_info,
  2463. unsigned long *value_bitmap)
  2464. {
  2465. if (!desc_array)
  2466. return -EINVAL;
  2467. return gpiod_get_array_value_complex(false, false, array_size,
  2468. desc_array, array_info,
  2469. value_bitmap);
  2470. }
  2471. EXPORT_SYMBOL_GPL(gpiod_get_array_value);
  2472. /*
  2473. * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
  2474. * @desc: gpio descriptor whose state need to be set.
  2475. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  2476. */
  2477. static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
  2478. {
  2479. int ret = 0;
  2480. struct gpio_chip *gc = desc->gdev->chip;
  2481. int offset = gpio_chip_hwgpio(desc);
  2482. if (value) {
  2483. ret = gc->direction_input(gc, offset);
  2484. } else {
  2485. ret = gc->direction_output(gc, offset, 0);
  2486. if (!ret)
  2487. set_bit(FLAG_IS_OUT, &desc->flags);
  2488. }
  2489. trace_gpio_direction(desc_to_gpio(desc), value, ret);
  2490. if (ret < 0)
  2491. gpiod_err(desc,
  2492. "%s: Error in set_value for open drain err %d\n",
  2493. __func__, ret);
  2494. }
  2495. /*
  2496. * _gpio_set_open_source_value() - Set the open source gpio's value.
  2497. * @desc: gpio descriptor whose state need to be set.
  2498. * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
  2499. */
  2500. static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
  2501. {
  2502. int ret = 0;
  2503. struct gpio_chip *gc = desc->gdev->chip;
  2504. int offset = gpio_chip_hwgpio(desc);
  2505. if (value) {
  2506. ret = gc->direction_output(gc, offset, 1);
  2507. if (!ret)
  2508. set_bit(FLAG_IS_OUT, &desc->flags);
  2509. } else {
  2510. ret = gc->direction_input(gc, offset);
  2511. }
  2512. trace_gpio_direction(desc_to_gpio(desc), !value, ret);
  2513. if (ret < 0)
  2514. gpiod_err(desc,
  2515. "%s: Error in set_value for open source err %d\n",
  2516. __func__, ret);
  2517. }
  2518. static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
  2519. {
  2520. struct gpio_chip *gc;
  2521. gc = desc->gdev->chip;
  2522. trace_gpio_value(desc_to_gpio(desc), 0, value);
  2523. gc->set(gc, gpio_chip_hwgpio(desc), value);
  2524. }
  2525. /*
  2526. * set multiple outputs on the same chip;
  2527. * use the chip's set_multiple function if available;
  2528. * otherwise set the outputs sequentially;
  2529. * @chip: the GPIO chip we operate on
  2530. * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
  2531. * defines which outputs are to be changed
  2532. * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
  2533. * defines the values the outputs specified by mask are to be set to
  2534. */
  2535. static void gpio_chip_set_multiple(struct gpio_chip *gc,
  2536. unsigned long *mask, unsigned long *bits)
  2537. {
  2538. if (gc->set_multiple) {
  2539. gc->set_multiple(gc, mask, bits);
  2540. } else {
  2541. unsigned int i;
  2542. /* set outputs if the corresponding mask bit is set */
  2543. for_each_set_bit(i, mask, gc->ngpio)
  2544. gc->set(gc, i, test_bit(i, bits));
  2545. }
  2546. }
  2547. int gpiod_set_array_value_complex(bool raw, bool can_sleep,
  2548. unsigned int array_size,
  2549. struct gpio_desc **desc_array,
  2550. struct gpio_array *array_info,
  2551. unsigned long *value_bitmap)
  2552. {
  2553. int i = 0;
  2554. /*
  2555. * Validate array_info against desc_array and its size.
  2556. * It should immediately follow desc_array if both
  2557. * have been obtained from the same gpiod_get_array() call.
  2558. */
  2559. if (array_info && array_info->desc == desc_array &&
  2560. array_size <= array_info->size &&
  2561. (void *)array_info == desc_array + array_info->size) {
  2562. if (!can_sleep)
  2563. WARN_ON(array_info->chip->can_sleep);
  2564. if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
  2565. bitmap_xor(value_bitmap, value_bitmap,
  2566. array_info->invert_mask, array_size);
  2567. gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
  2568. value_bitmap);
  2569. i = find_first_zero_bit(array_info->set_mask, array_size);
  2570. if (i == array_size)
  2571. return 0;
  2572. } else {
  2573. array_info = NULL;
  2574. }
  2575. while (i < array_size) {
  2576. struct gpio_chip *gc = desc_array[i]->gdev->chip;
  2577. DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO);
  2578. DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO);
  2579. unsigned long *mask, *bits;
  2580. int count = 0;
  2581. if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
  2582. mask = fastpath_mask;
  2583. bits = fastpath_bits;
  2584. } else {
  2585. gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC;
  2586. mask = bitmap_alloc(gc->ngpio, flags);
  2587. if (!mask)
  2588. return -ENOMEM;
  2589. bits = bitmap_alloc(gc->ngpio, flags);
  2590. if (!bits) {
  2591. bitmap_free(mask);
  2592. return -ENOMEM;
  2593. }
  2594. }
  2595. bitmap_zero(mask, gc->ngpio);
  2596. if (!can_sleep)
  2597. WARN_ON(gc->can_sleep);
  2598. do {
  2599. struct gpio_desc *desc = desc_array[i];
  2600. int hwgpio = gpio_chip_hwgpio(desc);
  2601. int value = test_bit(i, value_bitmap);
  2602. /*
  2603. * Pins applicable for fast input but not for
  2604. * fast output processing may have been already
  2605. * inverted inside the fast path, skip them.
  2606. */
  2607. if (!raw && !(array_info &&
  2608. test_bit(i, array_info->invert_mask)) &&
  2609. test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2610. value = !value;
  2611. trace_gpio_value(desc_to_gpio(desc), 0, value);
  2612. /*
  2613. * collect all normal outputs belonging to the same chip
  2614. * open drain and open source outputs are set individually
  2615. */
  2616. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
  2617. gpio_set_open_drain_value_commit(desc, value);
  2618. } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
  2619. gpio_set_open_source_value_commit(desc, value);
  2620. } else {
  2621. __set_bit(hwgpio, mask);
  2622. __assign_bit(hwgpio, bits, value);
  2623. count++;
  2624. }
  2625. i++;
  2626. if (array_info)
  2627. i = find_next_zero_bit(array_info->set_mask,
  2628. array_size, i);
  2629. } while ((i < array_size) &&
  2630. (desc_array[i]->gdev->chip == gc));
  2631. /* push collected bits to outputs */
  2632. if (count != 0)
  2633. gpio_chip_set_multiple(gc, mask, bits);
  2634. if (mask != fastpath_mask)
  2635. bitmap_free(mask);
  2636. if (bits != fastpath_bits)
  2637. bitmap_free(bits);
  2638. }
  2639. return 0;
  2640. }
  2641. /**
  2642. * gpiod_set_raw_value() - assign a gpio's raw value
  2643. * @desc: gpio whose value will be assigned
  2644. * @value: value to assign
  2645. *
  2646. * Set the raw value of the GPIO, i.e. the value of its physical line without
  2647. * regard for its ACTIVE_LOW status.
  2648. *
  2649. * This function can be called from contexts where we cannot sleep, and will
  2650. * complain if the GPIO chip functions potentially sleep.
  2651. */
  2652. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  2653. {
  2654. VALIDATE_DESC_VOID(desc);
  2655. /* Should be using gpiod_set_raw_value_cansleep() */
  2656. WARN_ON(desc->gdev->chip->can_sleep);
  2657. gpiod_set_raw_value_commit(desc, value);
  2658. }
  2659. EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
  2660. /**
  2661. * gpiod_set_value_nocheck() - set a GPIO line value without checking
  2662. * @desc: the descriptor to set the value on
  2663. * @value: value to set
  2664. *
  2665. * This sets the value of a GPIO line backing a descriptor, applying
  2666. * different semantic quirks like active low and open drain/source
  2667. * handling.
  2668. */
  2669. static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
  2670. {
  2671. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2672. value = !value;
  2673. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  2674. gpio_set_open_drain_value_commit(desc, value);
  2675. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  2676. gpio_set_open_source_value_commit(desc, value);
  2677. else
  2678. gpiod_set_raw_value_commit(desc, value);
  2679. }
  2680. /**
  2681. * gpiod_set_value() - assign a gpio's value
  2682. * @desc: gpio whose value will be assigned
  2683. * @value: value to assign
  2684. *
  2685. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
  2686. * OPEN_DRAIN and OPEN_SOURCE flags into account.
  2687. *
  2688. * This function can be called from contexts where we cannot sleep, and will
  2689. * complain if the GPIO chip functions potentially sleep.
  2690. */
  2691. void gpiod_set_value(struct gpio_desc *desc, int value)
  2692. {
  2693. VALIDATE_DESC_VOID(desc);
  2694. /* Should be using gpiod_set_value_cansleep() */
  2695. WARN_ON(desc->gdev->chip->can_sleep);
  2696. gpiod_set_value_nocheck(desc, value);
  2697. }
  2698. EXPORT_SYMBOL_GPL(gpiod_set_value);
  2699. /**
  2700. * gpiod_set_raw_array_value() - assign values to an array of GPIOs
  2701. * @array_size: number of elements in the descriptor array / value bitmap
  2702. * @desc_array: array of GPIO descriptors whose values will be assigned
  2703. * @array_info: information on applicability of fast bitmap processing path
  2704. * @value_bitmap: bitmap of values to assign
  2705. *
  2706. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  2707. * without regard for their ACTIVE_LOW status.
  2708. *
  2709. * This function can be called from contexts where we cannot sleep, and will
  2710. * complain if the GPIO chip functions potentially sleep.
  2711. */
  2712. int gpiod_set_raw_array_value(unsigned int array_size,
  2713. struct gpio_desc **desc_array,
  2714. struct gpio_array *array_info,
  2715. unsigned long *value_bitmap)
  2716. {
  2717. if (!desc_array)
  2718. return -EINVAL;
  2719. return gpiod_set_array_value_complex(true, false, array_size,
  2720. desc_array, array_info, value_bitmap);
  2721. }
  2722. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
  2723. /**
  2724. * gpiod_set_array_value() - assign values to an array of GPIOs
  2725. * @array_size: number of elements in the descriptor array / value bitmap
  2726. * @desc_array: array of GPIO descriptors whose values will be assigned
  2727. * @array_info: information on applicability of fast bitmap processing path
  2728. * @value_bitmap: bitmap of values to assign
  2729. *
  2730. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  2731. * into account.
  2732. *
  2733. * This function can be called from contexts where we cannot sleep, and will
  2734. * complain if the GPIO chip functions potentially sleep.
  2735. */
  2736. int gpiod_set_array_value(unsigned int array_size,
  2737. struct gpio_desc **desc_array,
  2738. struct gpio_array *array_info,
  2739. unsigned long *value_bitmap)
  2740. {
  2741. if (!desc_array)
  2742. return -EINVAL;
  2743. return gpiod_set_array_value_complex(false, false, array_size,
  2744. desc_array, array_info,
  2745. value_bitmap);
  2746. }
  2747. EXPORT_SYMBOL_GPL(gpiod_set_array_value);
  2748. /**
  2749. * gpiod_cansleep() - report whether gpio value access may sleep
  2750. * @desc: gpio to check
  2751. *
  2752. */
  2753. int gpiod_cansleep(const struct gpio_desc *desc)
  2754. {
  2755. VALIDATE_DESC(desc);
  2756. return desc->gdev->chip->can_sleep;
  2757. }
  2758. EXPORT_SYMBOL_GPL(gpiod_cansleep);
  2759. /**
  2760. * gpiod_set_consumer_name() - set the consumer name for the descriptor
  2761. * @desc: gpio to set the consumer name on
  2762. * @name: the new consumer name
  2763. */
  2764. int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
  2765. {
  2766. VALIDATE_DESC(desc);
  2767. if (name) {
  2768. name = kstrdup_const(name, GFP_KERNEL);
  2769. if (!name)
  2770. return -ENOMEM;
  2771. }
  2772. kfree_const(desc->label);
  2773. desc_set_label(desc, name);
  2774. return 0;
  2775. }
  2776. EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
  2777. /**
  2778. * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  2779. * @desc: gpio whose IRQ will be returned (already requested)
  2780. *
  2781. * Return the IRQ corresponding to the passed GPIO, or an error code in case of
  2782. * error.
  2783. */
  2784. int gpiod_to_irq(const struct gpio_desc *desc)
  2785. {
  2786. struct gpio_chip *gc;
  2787. int offset;
  2788. /*
  2789. * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
  2790. * requires this function to not return zero on an invalid descriptor
  2791. * but rather a negative error number.
  2792. */
  2793. if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
  2794. return -EINVAL;
  2795. gc = desc->gdev->chip;
  2796. offset = gpio_chip_hwgpio(desc);
  2797. if (gc->to_irq) {
  2798. int retirq = gc->to_irq(gc, offset);
  2799. /* Zero means NO_IRQ */
  2800. if (!retirq)
  2801. return -ENXIO;
  2802. return retirq;
  2803. }
  2804. #ifdef CONFIG_GPIOLIB_IRQCHIP
  2805. if (gc->irq.chip) {
  2806. /*
  2807. * Avoid race condition with other code, which tries to lookup
  2808. * an IRQ before the irqchip has been properly registered,
  2809. * i.e. while gpiochip is still being brought up.
  2810. */
  2811. return -EPROBE_DEFER;
  2812. }
  2813. #endif
  2814. return -ENXIO;
  2815. }
  2816. EXPORT_SYMBOL_GPL(gpiod_to_irq);
  2817. /**
  2818. * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
  2819. * @gc: the chip the GPIO to lock belongs to
  2820. * @offset: the offset of the GPIO to lock as IRQ
  2821. *
  2822. * This is used directly by GPIO drivers that want to lock down
  2823. * a certain GPIO line to be used for IRQs.
  2824. */
  2825. int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
  2826. {
  2827. struct gpio_desc *desc;
  2828. desc = gpiochip_get_desc(gc, offset);
  2829. if (IS_ERR(desc))
  2830. return PTR_ERR(desc);
  2831. /*
  2832. * If it's fast: flush the direction setting if something changed
  2833. * behind our back
  2834. */
  2835. if (!gc->can_sleep && gc->get_direction) {
  2836. int dir = gpiod_get_direction(desc);
  2837. if (dir < 0) {
  2838. chip_err(gc, "%s: cannot get GPIO direction\n",
  2839. __func__);
  2840. return dir;
  2841. }
  2842. }
  2843. /* To be valid for IRQ the line needs to be input or open drain */
  2844. if (test_bit(FLAG_IS_OUT, &desc->flags) &&
  2845. !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
  2846. chip_err(gc,
  2847. "%s: tried to flag a GPIO set as output for IRQ\n",
  2848. __func__);
  2849. return -EIO;
  2850. }
  2851. set_bit(FLAG_USED_AS_IRQ, &desc->flags);
  2852. set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
  2853. /*
  2854. * If the consumer has not set up a label (such as when the
  2855. * IRQ is referenced from .to_irq()) we set up a label here
  2856. * so it is clear this is used as an interrupt.
  2857. */
  2858. if (!desc->label)
  2859. desc_set_label(desc, "interrupt");
  2860. return 0;
  2861. }
  2862. EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
  2863. /**
  2864. * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
  2865. * @gc: the chip the GPIO to lock belongs to
  2866. * @offset: the offset of the GPIO to lock as IRQ
  2867. *
  2868. * This is used directly by GPIO drivers that want to indicate
  2869. * that a certain GPIO is no longer used exclusively for IRQ.
  2870. */
  2871. void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
  2872. {
  2873. struct gpio_desc *desc;
  2874. desc = gpiochip_get_desc(gc, offset);
  2875. if (IS_ERR(desc))
  2876. return;
  2877. clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
  2878. clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
  2879. /* If we only had this marking, erase it */
  2880. if (desc->label && !strcmp(desc->label, "interrupt"))
  2881. desc_set_label(desc, NULL);
  2882. }
  2883. EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
  2884. void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
  2885. {
  2886. struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
  2887. if (!IS_ERR(desc) &&
  2888. !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
  2889. clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
  2890. }
  2891. EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
  2892. void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
  2893. {
  2894. struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
  2895. if (!IS_ERR(desc) &&
  2896. !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
  2897. /*
  2898. * We must not be output when using IRQ UNLESS we are
  2899. * open drain.
  2900. */
  2901. WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
  2902. !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
  2903. set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
  2904. }
  2905. }
  2906. EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
  2907. bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
  2908. {
  2909. if (offset >= gc->ngpio)
  2910. return false;
  2911. return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
  2912. }
  2913. EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
  2914. int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
  2915. {
  2916. int ret;
  2917. if (!try_module_get(gc->gpiodev->owner))
  2918. return -ENODEV;
  2919. ret = gpiochip_lock_as_irq(gc, offset);
  2920. if (ret) {
  2921. chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
  2922. module_put(gc->gpiodev->owner);
  2923. return ret;
  2924. }
  2925. return 0;
  2926. }
  2927. EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
  2928. void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
  2929. {
  2930. gpiochip_unlock_as_irq(gc, offset);
  2931. module_put(gc->gpiodev->owner);
  2932. }
  2933. EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
  2934. bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
  2935. {
  2936. if (offset >= gc->ngpio)
  2937. return false;
  2938. return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
  2939. }
  2940. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
  2941. bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
  2942. {
  2943. if (offset >= gc->ngpio)
  2944. return false;
  2945. return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
  2946. }
  2947. EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
  2948. bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
  2949. {
  2950. if (offset >= gc->ngpio)
  2951. return false;
  2952. return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
  2953. }
  2954. EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
  2955. /**
  2956. * gpiod_get_raw_value_cansleep() - return a gpio's raw value
  2957. * @desc: gpio whose value will be returned
  2958. *
  2959. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  2960. * its ACTIVE_LOW status, or negative errno on failure.
  2961. *
  2962. * This function is to be called from contexts that can sleep.
  2963. */
  2964. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  2965. {
  2966. might_sleep_if(extra_checks);
  2967. VALIDATE_DESC(desc);
  2968. return gpiod_get_raw_value_commit(desc);
  2969. }
  2970. EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
  2971. /**
  2972. * gpiod_get_value_cansleep() - return a gpio's value
  2973. * @desc: gpio whose value will be returned
  2974. *
  2975. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  2976. * account, or negative errno on failure.
  2977. *
  2978. * This function is to be called from contexts that can sleep.
  2979. */
  2980. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  2981. {
  2982. int value;
  2983. might_sleep_if(extra_checks);
  2984. VALIDATE_DESC(desc);
  2985. value = gpiod_get_raw_value_commit(desc);
  2986. if (value < 0)
  2987. return value;
  2988. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  2989. value = !value;
  2990. return value;
  2991. }
  2992. EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
  2993. /**
  2994. * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
  2995. * @array_size: number of elements in the descriptor array / value bitmap
  2996. * @desc_array: array of GPIO descriptors whose values will be read
  2997. * @array_info: information on applicability of fast bitmap processing path
  2998. * @value_bitmap: bitmap to store the read values
  2999. *
  3000. * Read the raw values of the GPIOs, i.e. the values of the physical lines
  3001. * without regard for their ACTIVE_LOW status. Return 0 in case of success,
  3002. * else an error code.
  3003. *
  3004. * This function is to be called from contexts that can sleep.
  3005. */
  3006. int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
  3007. struct gpio_desc **desc_array,
  3008. struct gpio_array *array_info,
  3009. unsigned long *value_bitmap)
  3010. {
  3011. might_sleep_if(extra_checks);
  3012. if (!desc_array)
  3013. return -EINVAL;
  3014. return gpiod_get_array_value_complex(true, true, array_size,
  3015. desc_array, array_info,
  3016. value_bitmap);
  3017. }
  3018. EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
  3019. /**
  3020. * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
  3021. * @array_size: number of elements in the descriptor array / value bitmap
  3022. * @desc_array: array of GPIO descriptors whose values will be read
  3023. * @array_info: information on applicability of fast bitmap processing path
  3024. * @value_bitmap: bitmap to store the read values
  3025. *
  3026. * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  3027. * into account. Return 0 in case of success, else an error code.
  3028. *
  3029. * This function is to be called from contexts that can sleep.
  3030. */
  3031. int gpiod_get_array_value_cansleep(unsigned int array_size,
  3032. struct gpio_desc **desc_array,
  3033. struct gpio_array *array_info,
  3034. unsigned long *value_bitmap)
  3035. {
  3036. might_sleep_if(extra_checks);
  3037. if (!desc_array)
  3038. return -EINVAL;
  3039. return gpiod_get_array_value_complex(false, true, array_size,
  3040. desc_array, array_info,
  3041. value_bitmap);
  3042. }
  3043. EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
  3044. /**
  3045. * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
  3046. * @desc: gpio whose value will be assigned
  3047. * @value: value to assign
  3048. *
  3049. * Set the raw value of the GPIO, i.e. the value of its physical line without
  3050. * regard for its ACTIVE_LOW status.
  3051. *
  3052. * This function is to be called from contexts that can sleep.
  3053. */
  3054. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  3055. {
  3056. might_sleep_if(extra_checks);
  3057. VALIDATE_DESC_VOID(desc);
  3058. gpiod_set_raw_value_commit(desc, value);
  3059. }
  3060. EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
  3061. /**
  3062. * gpiod_set_value_cansleep() - assign a gpio's value
  3063. * @desc: gpio whose value will be assigned
  3064. * @value: value to assign
  3065. *
  3066. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  3067. * account
  3068. *
  3069. * This function is to be called from contexts that can sleep.
  3070. */
  3071. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  3072. {
  3073. might_sleep_if(extra_checks);
  3074. VALIDATE_DESC_VOID(desc);
  3075. gpiod_set_value_nocheck(desc, value);
  3076. }
  3077. EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
  3078. /**
  3079. * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
  3080. * @array_size: number of elements in the descriptor array / value bitmap
  3081. * @desc_array: array of GPIO descriptors whose values will be assigned
  3082. * @array_info: information on applicability of fast bitmap processing path
  3083. * @value_bitmap: bitmap of values to assign
  3084. *
  3085. * Set the raw values of the GPIOs, i.e. the values of the physical lines
  3086. * without regard for their ACTIVE_LOW status.
  3087. *
  3088. * This function is to be called from contexts that can sleep.
  3089. */
  3090. int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  3091. struct gpio_desc **desc_array,
  3092. struct gpio_array *array_info,
  3093. unsigned long *value_bitmap)
  3094. {
  3095. might_sleep_if(extra_checks);
  3096. if (!desc_array)
  3097. return -EINVAL;
  3098. return gpiod_set_array_value_complex(true, true, array_size, desc_array,
  3099. array_info, value_bitmap);
  3100. }
  3101. EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
  3102. /**
  3103. * gpiod_add_lookup_tables() - register GPIO device consumers
  3104. * @tables: list of tables of consumers to register
  3105. * @n: number of tables in the list
  3106. */
  3107. void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
  3108. {
  3109. unsigned int i;
  3110. mutex_lock(&gpio_lookup_lock);
  3111. for (i = 0; i < n; i++)
  3112. list_add_tail(&tables[i]->list, &gpio_lookup_list);
  3113. mutex_unlock(&gpio_lookup_lock);
  3114. }
  3115. /**
  3116. * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
  3117. * @array_size: number of elements in the descriptor array / value bitmap
  3118. * @desc_array: array of GPIO descriptors whose values will be assigned
  3119. * @array_info: information on applicability of fast bitmap processing path
  3120. * @value_bitmap: bitmap of values to assign
  3121. *
  3122. * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
  3123. * into account.
  3124. *
  3125. * This function is to be called from contexts that can sleep.
  3126. */
  3127. int gpiod_set_array_value_cansleep(unsigned int array_size,
  3128. struct gpio_desc **desc_array,
  3129. struct gpio_array *array_info,
  3130. unsigned long *value_bitmap)
  3131. {
  3132. might_sleep_if(extra_checks);
  3133. if (!desc_array)
  3134. return -EINVAL;
  3135. return gpiod_set_array_value_complex(false, true, array_size,
  3136. desc_array, array_info,
  3137. value_bitmap);
  3138. }
  3139. EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
  3140. /**
  3141. * gpiod_add_lookup_table() - register GPIO device consumers
  3142. * @table: table of consumers to register
  3143. */
  3144. void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
  3145. {
  3146. gpiod_add_lookup_tables(&table, 1);
  3147. }
  3148. EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
  3149. /**
  3150. * gpiod_remove_lookup_table() - unregister GPIO device consumers
  3151. * @table: table of consumers to unregister
  3152. */
  3153. void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
  3154. {
  3155. /* Nothing to remove */
  3156. if (!table)
  3157. return;
  3158. mutex_lock(&gpio_lookup_lock);
  3159. list_del(&table->list);
  3160. mutex_unlock(&gpio_lookup_lock);
  3161. }
  3162. EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
  3163. /**
  3164. * gpiod_add_hogs() - register a set of GPIO hogs from machine code
  3165. * @hogs: table of gpio hog entries with a zeroed sentinel at the end
  3166. */
  3167. void gpiod_add_hogs(struct gpiod_hog *hogs)
  3168. {
  3169. struct gpio_chip *gc;
  3170. struct gpiod_hog *hog;
  3171. mutex_lock(&gpio_machine_hogs_mutex);
  3172. for (hog = &hogs[0]; hog->chip_label; hog++) {
  3173. list_add_tail(&hog->list, &gpio_machine_hogs);
  3174. /*
  3175. * The chip may have been registered earlier, so check if it
  3176. * exists and, if so, try to hog the line now.
  3177. */
  3178. gc = find_chip_by_name(hog->chip_label);
  3179. if (gc)
  3180. gpiochip_machine_hog(gc, hog);
  3181. }
  3182. mutex_unlock(&gpio_machine_hogs_mutex);
  3183. }
  3184. EXPORT_SYMBOL_GPL(gpiod_add_hogs);
  3185. void gpiod_remove_hogs(struct gpiod_hog *hogs)
  3186. {
  3187. struct gpiod_hog *hog;
  3188. mutex_lock(&gpio_machine_hogs_mutex);
  3189. for (hog = &hogs[0]; hog->chip_label; hog++)
  3190. list_del(&hog->list);
  3191. mutex_unlock(&gpio_machine_hogs_mutex);
  3192. }
  3193. EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
  3194. static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
  3195. {
  3196. const char *dev_id = dev ? dev_name(dev) : NULL;
  3197. struct gpiod_lookup_table *table;
  3198. mutex_lock(&gpio_lookup_lock);
  3199. list_for_each_entry(table, &gpio_lookup_list, list) {
  3200. if (table->dev_id && dev_id) {
  3201. /*
  3202. * Valid strings on both ends, must be identical to have
  3203. * a match
  3204. */
  3205. if (!strcmp(table->dev_id, dev_id))
  3206. goto found;
  3207. } else {
  3208. /*
  3209. * One of the pointers is NULL, so both must be to have
  3210. * a match
  3211. */
  3212. if (dev_id == table->dev_id)
  3213. goto found;
  3214. }
  3215. }
  3216. table = NULL;
  3217. found:
  3218. mutex_unlock(&gpio_lookup_lock);
  3219. return table;
  3220. }
  3221. static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
  3222. unsigned int idx, unsigned long *flags)
  3223. {
  3224. struct gpio_desc *desc = ERR_PTR(-ENOENT);
  3225. struct gpiod_lookup_table *table;
  3226. struct gpiod_lookup *p;
  3227. table = gpiod_find_lookup_table(dev);
  3228. if (!table)
  3229. return desc;
  3230. for (p = &table->table[0]; p->key; p++) {
  3231. struct gpio_chip *gc;
  3232. /* idx must always match exactly */
  3233. if (p->idx != idx)
  3234. continue;
  3235. /* If the lookup entry has a con_id, require exact match */
  3236. if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
  3237. continue;
  3238. if (p->chip_hwnum == U16_MAX) {
  3239. desc = gpio_name_to_desc(p->key);
  3240. if (desc) {
  3241. *flags = p->flags;
  3242. return desc;
  3243. }
  3244. dev_warn(dev, "cannot find GPIO line %s, deferring\n",
  3245. p->key);
  3246. return ERR_PTR(-EPROBE_DEFER);
  3247. }
  3248. gc = find_chip_by_name(p->key);
  3249. if (!gc) {
  3250. /*
  3251. * As the lookup table indicates a chip with
  3252. * p->key should exist, assume it may
  3253. * still appear later and let the interested
  3254. * consumer be probed again or let the Deferred
  3255. * Probe infrastructure handle the error.
  3256. */
  3257. dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
  3258. p->key);
  3259. return ERR_PTR(-EPROBE_DEFER);
  3260. }
  3261. if (gc->ngpio <= p->chip_hwnum) {
  3262. dev_err(dev,
  3263. "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
  3264. idx, p->chip_hwnum, gc->ngpio - 1,
  3265. gc->label);
  3266. return ERR_PTR(-EINVAL);
  3267. }
  3268. desc = gpiochip_get_desc(gc, p->chip_hwnum);
  3269. *flags = p->flags;
  3270. return desc;
  3271. }
  3272. return desc;
  3273. }
  3274. static int platform_gpio_count(struct device *dev, const char *con_id)
  3275. {
  3276. struct gpiod_lookup_table *table;
  3277. struct gpiod_lookup *p;
  3278. unsigned int count = 0;
  3279. table = gpiod_find_lookup_table(dev);
  3280. if (!table)
  3281. return -ENOENT;
  3282. for (p = &table->table[0]; p->key; p++) {
  3283. if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
  3284. (!con_id && !p->con_id))
  3285. count++;
  3286. }
  3287. if (!count)
  3288. return -ENOENT;
  3289. return count;
  3290. }
  3291. /**
  3292. * fwnode_get_named_gpiod - obtain a GPIO from firmware node
  3293. * @fwnode: handle of the firmware node
  3294. * @propname: name of the firmware property representing the GPIO
  3295. * @index: index of the GPIO to obtain for the consumer
  3296. * @dflags: GPIO initialization flags
  3297. * @label: label to attach to the requested GPIO
  3298. *
  3299. * This function can be used for drivers that get their configuration
  3300. * from opaque firmware.
  3301. *
  3302. * The function properly finds the corresponding GPIO using whatever is the
  3303. * underlying firmware interface and then makes sure that the GPIO
  3304. * descriptor is requested before it is returned to the caller.
  3305. *
  3306. * Returns:
  3307. * On successful request the GPIO pin is configured in accordance with
  3308. * provided @dflags.
  3309. *
  3310. * In case of error an ERR_PTR() is returned.
  3311. */
  3312. static struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
  3313. const char *propname, int index,
  3314. enum gpiod_flags dflags,
  3315. const char *label)
  3316. {
  3317. unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
  3318. struct gpio_desc *desc = ERR_PTR(-ENODEV);
  3319. int ret;
  3320. if (is_of_node(fwnode)) {
  3321. desc = gpiod_get_from_of_node(to_of_node(fwnode),
  3322. propname, index,
  3323. dflags,
  3324. label);
  3325. return desc;
  3326. } else if (is_acpi_node(fwnode)) {
  3327. struct acpi_gpio_info info;
  3328. desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
  3329. if (IS_ERR(desc))
  3330. return desc;
  3331. acpi_gpio_update_gpiod_flags(&dflags, &info);
  3332. acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
  3333. } else {
  3334. return ERR_PTR(-EINVAL);
  3335. }
  3336. /* Currently only ACPI takes this path */
  3337. ret = gpiod_request(desc, label);
  3338. if (ret)
  3339. return ERR_PTR(ret);
  3340. ret = gpiod_configure_flags(desc, propname, lflags, dflags);
  3341. if (ret < 0) {
  3342. gpiod_put(desc);
  3343. return ERR_PTR(ret);
  3344. }
  3345. blocking_notifier_call_chain(&desc->gdev->notifier,
  3346. GPIOLINE_CHANGED_REQUESTED, desc);
  3347. return desc;
  3348. }
  3349. /**
  3350. * fwnode_gpiod_get_index - obtain a GPIO from firmware node
  3351. * @fwnode: handle of the firmware node
  3352. * @con_id: function within the GPIO consumer
  3353. * @index: index of the GPIO to obtain for the consumer
  3354. * @flags: GPIO initialization flags
  3355. * @label: label to attach to the requested GPIO
  3356. *
  3357. * This function can be used for drivers that get their configuration
  3358. * from opaque firmware.
  3359. *
  3360. * The function properly finds the corresponding GPIO using whatever is the
  3361. * underlying firmware interface and then makes sure that the GPIO
  3362. * descriptor is requested before it is returned to the caller.
  3363. *
  3364. * Returns:
  3365. * On successful request the GPIO pin is configured in accordance with
  3366. * provided @flags.
  3367. *
  3368. * In case of error an ERR_PTR() is returned.
  3369. */
  3370. struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
  3371. const char *con_id, int index,
  3372. enum gpiod_flags flags,
  3373. const char *label)
  3374. {
  3375. struct gpio_desc *desc;
  3376. char prop_name[32]; /* 32 is max size of property name */
  3377. unsigned int i;
  3378. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  3379. if (con_id)
  3380. snprintf(prop_name, sizeof(prop_name), "%s-%s",
  3381. con_id, gpio_suffixes[i]);
  3382. else
  3383. snprintf(prop_name, sizeof(prop_name), "%s",
  3384. gpio_suffixes[i]);
  3385. desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
  3386. label);
  3387. if (!gpiod_not_found(desc))
  3388. break;
  3389. }
  3390. return desc;
  3391. }
  3392. EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
  3393. /**
  3394. * gpiod_count - return the number of GPIOs associated with a device / function
  3395. * or -ENOENT if no GPIO has been assigned to the requested function
  3396. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3397. * @con_id: function within the GPIO consumer
  3398. */
  3399. int gpiod_count(struct device *dev, const char *con_id)
  3400. {
  3401. const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
  3402. int count = -ENOENT;
  3403. if (is_of_node(fwnode))
  3404. count = of_gpio_get_count(dev, con_id);
  3405. else if (is_acpi_node(fwnode))
  3406. count = acpi_gpio_count(dev, con_id);
  3407. if (count < 0)
  3408. count = platform_gpio_count(dev, con_id);
  3409. return count;
  3410. }
  3411. EXPORT_SYMBOL_GPL(gpiod_count);
  3412. /**
  3413. * gpiod_get - obtain a GPIO for a given GPIO function
  3414. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3415. * @con_id: function within the GPIO consumer
  3416. * @flags: optional GPIO initialization flags
  3417. *
  3418. * Return the GPIO descriptor corresponding to the function con_id of device
  3419. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  3420. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  3421. */
  3422. struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
  3423. enum gpiod_flags flags)
  3424. {
  3425. return gpiod_get_index(dev, con_id, 0, flags);
  3426. }
  3427. EXPORT_SYMBOL_GPL(gpiod_get);
  3428. /**
  3429. * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
  3430. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3431. * @con_id: function within the GPIO consumer
  3432. * @flags: optional GPIO initialization flags
  3433. *
  3434. * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
  3435. * the requested function it will return NULL. This is convenient for drivers
  3436. * that need to handle optional GPIOs.
  3437. */
  3438. struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
  3439. const char *con_id,
  3440. enum gpiod_flags flags)
  3441. {
  3442. return gpiod_get_index_optional(dev, con_id, 0, flags);
  3443. }
  3444. EXPORT_SYMBOL_GPL(gpiod_get_optional);
  3445. /**
  3446. * gpiod_configure_flags - helper function to configure a given GPIO
  3447. * @desc: gpio whose value will be assigned
  3448. * @con_id: function within the GPIO consumer
  3449. * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
  3450. * of_find_gpio() or of_get_gpio_hog()
  3451. * @dflags: gpiod_flags - optional GPIO initialization flags
  3452. *
  3453. * Return 0 on success, -ENOENT if no GPIO has been assigned to the
  3454. * requested function and/or index, or another IS_ERR() code if an error
  3455. * occurred while trying to acquire the GPIO.
  3456. */
  3457. int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
  3458. unsigned long lflags, enum gpiod_flags dflags)
  3459. {
  3460. int ret;
  3461. if (lflags & GPIO_ACTIVE_LOW)
  3462. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  3463. if (lflags & GPIO_OPEN_DRAIN)
  3464. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  3465. else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
  3466. /*
  3467. * This enforces open drain mode from the consumer side.
  3468. * This is necessary for some busses like I2C, but the lookup
  3469. * should *REALLY* have specified them as open drain in the
  3470. * first place, so print a little warning here.
  3471. */
  3472. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  3473. gpiod_warn(desc,
  3474. "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
  3475. }
  3476. if (lflags & GPIO_OPEN_SOURCE)
  3477. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  3478. if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) ||
  3479. ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) ||
  3480. ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) {
  3481. gpiod_err(desc,
  3482. "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n");
  3483. return -EINVAL;
  3484. }
  3485. if (lflags & GPIO_PULL_UP)
  3486. set_bit(FLAG_PULL_UP, &desc->flags);
  3487. else if (lflags & GPIO_PULL_DOWN)
  3488. set_bit(FLAG_PULL_DOWN, &desc->flags);
  3489. else if (lflags & GPIO_PULL_DISABLE)
  3490. set_bit(FLAG_BIAS_DISABLE, &desc->flags);
  3491. ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
  3492. if (ret < 0)
  3493. return ret;
  3494. /* No particular flag request, return here... */
  3495. if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
  3496. gpiod_dbg(desc, "no flags found for %s\n", con_id);
  3497. return 0;
  3498. }
  3499. /* Process flags */
  3500. if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
  3501. ret = gpiod_direction_output(desc,
  3502. !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
  3503. else
  3504. ret = gpiod_direction_input(desc);
  3505. return ret;
  3506. }
  3507. /**
  3508. * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
  3509. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3510. * @con_id: function within the GPIO consumer
  3511. * @idx: index of the GPIO to obtain in the consumer
  3512. * @flags: optional GPIO initialization flags
  3513. *
  3514. * This variant of gpiod_get() allows to access GPIOs other than the first
  3515. * defined one for functions that define several GPIOs.
  3516. *
  3517. * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
  3518. * requested function and/or index, or another IS_ERR() code if an error
  3519. * occurred while trying to acquire the GPIO.
  3520. */
  3521. struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
  3522. const char *con_id,
  3523. unsigned int idx,
  3524. enum gpiod_flags flags)
  3525. {
  3526. unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
  3527. struct gpio_desc *desc = NULL;
  3528. int ret;
  3529. /* Maybe we have a device name, maybe not */
  3530. const char *devname = dev ? dev_name(dev) : "?";
  3531. const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
  3532. dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
  3533. /* Using device tree? */
  3534. if (is_of_node(fwnode)) {
  3535. dev_dbg(dev, "using device tree for GPIO lookup\n");
  3536. desc = of_find_gpio(dev, con_id, idx, &lookupflags);
  3537. } else if (is_acpi_node(fwnode)) {
  3538. dev_dbg(dev, "using ACPI for GPIO lookup\n");
  3539. desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
  3540. }
  3541. /*
  3542. * Either we are not using DT or ACPI, or their lookup did not return
  3543. * a result. In that case, use platform lookup as a fallback.
  3544. */
  3545. if (!desc || gpiod_not_found(desc)) {
  3546. dev_dbg(dev, "using lookup tables for GPIO lookup\n");
  3547. desc = gpiod_find(dev, con_id, idx, &lookupflags);
  3548. }
  3549. if (IS_ERR(desc)) {
  3550. dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
  3551. return desc;
  3552. }
  3553. /*
  3554. * If a connection label was passed use that, else attempt to use
  3555. * the device name as label
  3556. */
  3557. ret = gpiod_request(desc, con_id ?: devname);
  3558. if (ret) {
  3559. if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
  3560. return ERR_PTR(ret);
  3561. /*
  3562. * This happens when there are several consumers for
  3563. * the same GPIO line: we just return here without
  3564. * further initialization. It is a bit of a hack.
  3565. * This is necessary to support fixed regulators.
  3566. *
  3567. * FIXME: Make this more sane and safe.
  3568. */
  3569. dev_info(dev, "nonexclusive access to GPIO for %s\n", con_id ?: devname);
  3570. return desc;
  3571. }
  3572. ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
  3573. if (ret < 0) {
  3574. dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
  3575. gpiod_put(desc);
  3576. return ERR_PTR(ret);
  3577. }
  3578. blocking_notifier_call_chain(&desc->gdev->notifier,
  3579. GPIOLINE_CHANGED_REQUESTED, desc);
  3580. return desc;
  3581. }
  3582. EXPORT_SYMBOL_GPL(gpiod_get_index);
  3583. /**
  3584. * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
  3585. * function
  3586. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3587. * @con_id: function within the GPIO consumer
  3588. * @index: index of the GPIO to obtain in the consumer
  3589. * @flags: optional GPIO initialization flags
  3590. *
  3591. * This is equivalent to gpiod_get_index(), except that when no GPIO with the
  3592. * specified index was assigned to the requested function it will return NULL.
  3593. * This is convenient for drivers that need to handle optional GPIOs.
  3594. */
  3595. struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
  3596. const char *con_id,
  3597. unsigned int index,
  3598. enum gpiod_flags flags)
  3599. {
  3600. struct gpio_desc *desc;
  3601. desc = gpiod_get_index(dev, con_id, index, flags);
  3602. if (gpiod_not_found(desc))
  3603. return NULL;
  3604. return desc;
  3605. }
  3606. EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
  3607. /**
  3608. * gpiod_hog - Hog the specified GPIO desc given the provided flags
  3609. * @desc: gpio whose value will be assigned
  3610. * @name: gpio line name
  3611. * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
  3612. * of_find_gpio() or of_get_gpio_hog()
  3613. * @dflags: gpiod_flags - optional GPIO initialization flags
  3614. */
  3615. int gpiod_hog(struct gpio_desc *desc, const char *name,
  3616. unsigned long lflags, enum gpiod_flags dflags)
  3617. {
  3618. struct gpio_chip *gc;
  3619. struct gpio_desc *local_desc;
  3620. int hwnum;
  3621. int ret;
  3622. gc = gpiod_to_chip(desc);
  3623. hwnum = gpio_chip_hwgpio(desc);
  3624. local_desc = gpiochip_request_own_desc(gc, hwnum, name,
  3625. lflags, dflags);
  3626. if (IS_ERR(local_desc)) {
  3627. ret = PTR_ERR(local_desc);
  3628. pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
  3629. name, gc->label, hwnum, ret);
  3630. return ret;
  3631. }
  3632. /* Mark GPIO as hogged so it can be identified and removed later */
  3633. set_bit(FLAG_IS_HOGGED, &desc->flags);
  3634. gpiod_info(desc, "hogged as %s%s\n",
  3635. (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
  3636. (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
  3637. (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
  3638. return 0;
  3639. }
  3640. /**
  3641. * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
  3642. * @gc: gpio chip to act on
  3643. */
  3644. static void gpiochip_free_hogs(struct gpio_chip *gc)
  3645. {
  3646. struct gpio_desc *desc;
  3647. for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED)
  3648. gpiochip_free_own_desc(desc);
  3649. }
  3650. /**
  3651. * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
  3652. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3653. * @con_id: function within the GPIO consumer
  3654. * @flags: optional GPIO initialization flags
  3655. *
  3656. * This function acquires all the GPIOs defined under a given function.
  3657. *
  3658. * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
  3659. * no GPIO has been assigned to the requested function, or another IS_ERR()
  3660. * code if an error occurred while trying to acquire the GPIOs.
  3661. */
  3662. struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
  3663. const char *con_id,
  3664. enum gpiod_flags flags)
  3665. {
  3666. struct gpio_desc *desc;
  3667. struct gpio_descs *descs;
  3668. struct gpio_array *array_info = NULL;
  3669. struct gpio_chip *gc;
  3670. int count, bitmap_size;
  3671. count = gpiod_count(dev, con_id);
  3672. if (count < 0)
  3673. return ERR_PTR(count);
  3674. descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
  3675. if (!descs)
  3676. return ERR_PTR(-ENOMEM);
  3677. for (descs->ndescs = 0; descs->ndescs < count; ) {
  3678. desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
  3679. if (IS_ERR(desc)) {
  3680. gpiod_put_array(descs);
  3681. return ERR_CAST(desc);
  3682. }
  3683. descs->desc[descs->ndescs] = desc;
  3684. gc = gpiod_to_chip(desc);
  3685. /*
  3686. * If pin hardware number of array member 0 is also 0, select
  3687. * its chip as a candidate for fast bitmap processing path.
  3688. */
  3689. if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
  3690. struct gpio_descs *array;
  3691. bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
  3692. gc->ngpio : count);
  3693. array = kzalloc(struct_size(descs, desc, count) +
  3694. struct_size(array_info, invert_mask,
  3695. 3 * bitmap_size), GFP_KERNEL);
  3696. if (!array) {
  3697. gpiod_put_array(descs);
  3698. return ERR_PTR(-ENOMEM);
  3699. }
  3700. memcpy(array, descs,
  3701. struct_size(descs, desc, descs->ndescs + 1));
  3702. kfree(descs);
  3703. descs = array;
  3704. array_info = (void *)(descs->desc + count);
  3705. array_info->get_mask = array_info->invert_mask +
  3706. bitmap_size;
  3707. array_info->set_mask = array_info->get_mask +
  3708. bitmap_size;
  3709. array_info->desc = descs->desc;
  3710. array_info->size = count;
  3711. array_info->chip = gc;
  3712. bitmap_set(array_info->get_mask, descs->ndescs,
  3713. count - descs->ndescs);
  3714. bitmap_set(array_info->set_mask, descs->ndescs,
  3715. count - descs->ndescs);
  3716. descs->info = array_info;
  3717. }
  3718. /* Unmark array members which don't belong to the 'fast' chip */
  3719. if (array_info && array_info->chip != gc) {
  3720. __clear_bit(descs->ndescs, array_info->get_mask);
  3721. __clear_bit(descs->ndescs, array_info->set_mask);
  3722. }
  3723. /*
  3724. * Detect array members which belong to the 'fast' chip
  3725. * but their pins are not in hardware order.
  3726. */
  3727. else if (array_info &&
  3728. gpio_chip_hwgpio(desc) != descs->ndescs) {
  3729. /*
  3730. * Don't use fast path if all array members processed so
  3731. * far belong to the same chip as this one but its pin
  3732. * hardware number is different from its array index.
  3733. */
  3734. if (bitmap_full(array_info->get_mask, descs->ndescs)) {
  3735. array_info = NULL;
  3736. } else {
  3737. __clear_bit(descs->ndescs,
  3738. array_info->get_mask);
  3739. __clear_bit(descs->ndescs,
  3740. array_info->set_mask);
  3741. }
  3742. } else if (array_info) {
  3743. /* Exclude open drain or open source from fast output */
  3744. if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
  3745. gpiochip_line_is_open_source(gc, descs->ndescs))
  3746. __clear_bit(descs->ndescs,
  3747. array_info->set_mask);
  3748. /* Identify 'fast' pins which require invertion */
  3749. if (gpiod_is_active_low(desc))
  3750. __set_bit(descs->ndescs,
  3751. array_info->invert_mask);
  3752. }
  3753. descs->ndescs++;
  3754. }
  3755. if (array_info)
  3756. dev_dbg(dev,
  3757. "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
  3758. array_info->chip->label, array_info->size,
  3759. *array_info->get_mask, *array_info->set_mask,
  3760. *array_info->invert_mask);
  3761. return descs;
  3762. }
  3763. EXPORT_SYMBOL_GPL(gpiod_get_array);
  3764. /**
  3765. * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
  3766. * function
  3767. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  3768. * @con_id: function within the GPIO consumer
  3769. * @flags: optional GPIO initialization flags
  3770. *
  3771. * This is equivalent to gpiod_get_array(), except that when no GPIO was
  3772. * assigned to the requested function it will return NULL.
  3773. */
  3774. struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
  3775. const char *con_id,
  3776. enum gpiod_flags flags)
  3777. {
  3778. struct gpio_descs *descs;
  3779. descs = gpiod_get_array(dev, con_id, flags);
  3780. if (gpiod_not_found(descs))
  3781. return NULL;
  3782. return descs;
  3783. }
  3784. EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
  3785. /**
  3786. * gpiod_put - dispose of a GPIO descriptor
  3787. * @desc: GPIO descriptor to dispose of
  3788. *
  3789. * No descriptor can be used after gpiod_put() has been called on it.
  3790. */
  3791. void gpiod_put(struct gpio_desc *desc)
  3792. {
  3793. if (desc)
  3794. gpiod_free(desc);
  3795. }
  3796. EXPORT_SYMBOL_GPL(gpiod_put);
  3797. /**
  3798. * gpiod_put_array - dispose of multiple GPIO descriptors
  3799. * @descs: struct gpio_descs containing an array of descriptors
  3800. */
  3801. void gpiod_put_array(struct gpio_descs *descs)
  3802. {
  3803. unsigned int i;
  3804. for (i = 0; i < descs->ndescs; i++)
  3805. gpiod_put(descs->desc[i]);
  3806. kfree(descs);
  3807. }
  3808. EXPORT_SYMBOL_GPL(gpiod_put_array);
  3809. static int gpio_bus_match(struct device *dev, struct device_driver *drv)
  3810. {
  3811. struct fwnode_handle *fwnode = dev_fwnode(dev);
  3812. /*
  3813. * Only match if the fwnode doesn't already have a proper struct device
  3814. * created for it.
  3815. */
  3816. if (fwnode && fwnode->dev != dev)
  3817. return 0;
  3818. return 1;
  3819. }
  3820. static int gpio_stub_drv_probe(struct device *dev)
  3821. {
  3822. /*
  3823. * The DT node of some GPIO chips have a "compatible" property, but
  3824. * never have a struct device added and probed by a driver to register
  3825. * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
  3826. * the consumers of the GPIO chip to get probe deferred forever because
  3827. * they will be waiting for a device associated with the GPIO chip
  3828. * firmware node to get added and bound to a driver.
  3829. *
  3830. * To allow these consumers to probe, we associate the struct
  3831. * gpio_device of the GPIO chip with the firmware node and then simply
  3832. * bind it to this stub driver.
  3833. */
  3834. return 0;
  3835. }
  3836. static struct device_driver gpio_stub_drv = {
  3837. .name = "gpio_stub_drv",
  3838. .bus = &gpio_bus_type,
  3839. .probe = gpio_stub_drv_probe,
  3840. };
  3841. static int __init gpiolib_dev_init(void)
  3842. {
  3843. int ret;
  3844. /* Register GPIO sysfs bus */
  3845. ret = bus_register(&gpio_bus_type);
  3846. if (ret < 0) {
  3847. pr_err("gpiolib: could not register GPIO bus type\n");
  3848. return ret;
  3849. }
  3850. ret = driver_register(&gpio_stub_drv);
  3851. if (ret < 0) {
  3852. pr_err("gpiolib: could not register GPIO stub driver\n");
  3853. bus_unregister(&gpio_bus_type);
  3854. return ret;
  3855. }
  3856. ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
  3857. if (ret < 0) {
  3858. pr_err("gpiolib: failed to allocate char dev region\n");
  3859. driver_unregister(&gpio_stub_drv);
  3860. bus_unregister(&gpio_bus_type);
  3861. return ret;
  3862. }
  3863. gpiolib_initialized = true;
  3864. gpiochip_setup_devs();
  3865. #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
  3866. WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
  3867. #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
  3868. return ret;
  3869. }
  3870. core_initcall(gpiolib_dev_init);
  3871. #ifdef CONFIG_DEBUG_FS
  3872. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
  3873. {
  3874. struct gpio_chip *gc = gdev->chip;
  3875. struct gpio_desc *desc;
  3876. unsigned gpio = gdev->base;
  3877. int value;
  3878. bool is_out;
  3879. bool is_irq;
  3880. bool active_low;
  3881. for_each_gpio_desc(gc, desc) {
  3882. if (test_bit(FLAG_REQUESTED, &desc->flags)) {
  3883. gpiod_get_direction(desc);
  3884. is_out = test_bit(FLAG_IS_OUT, &desc->flags);
  3885. value = gpio_chip_get_value(gc, desc);
  3886. is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags);
  3887. active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  3888. seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n",
  3889. gpio, desc->name ?: "", desc->label,
  3890. is_out ? "out" : "in ",
  3891. value >= 0 ? (value ? "hi" : "lo") : "? ",
  3892. is_irq ? "IRQ " : "",
  3893. active_low ? "ACTIVE LOW" : "");
  3894. } else if (desc->name) {
  3895. seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name);
  3896. }
  3897. gpio++;
  3898. }
  3899. }
  3900. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  3901. {
  3902. unsigned long flags;
  3903. struct gpio_device *gdev = NULL;
  3904. loff_t index = *pos;
  3905. s->private = "";
  3906. spin_lock_irqsave(&gpio_lock, flags);
  3907. list_for_each_entry(gdev, &gpio_devices, list)
  3908. if (index-- == 0) {
  3909. spin_unlock_irqrestore(&gpio_lock, flags);
  3910. return gdev;
  3911. }
  3912. spin_unlock_irqrestore(&gpio_lock, flags);
  3913. return NULL;
  3914. }
  3915. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  3916. {
  3917. unsigned long flags;
  3918. struct gpio_device *gdev = v;
  3919. void *ret = NULL;
  3920. spin_lock_irqsave(&gpio_lock, flags);
  3921. if (list_is_last(&gdev->list, &gpio_devices))
  3922. ret = NULL;
  3923. else
  3924. ret = list_first_entry(&gdev->list, struct gpio_device, list);
  3925. spin_unlock_irqrestore(&gpio_lock, flags);
  3926. s->private = "\n";
  3927. ++*pos;
  3928. return ret;
  3929. }
  3930. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  3931. {
  3932. }
  3933. static int gpiolib_seq_show(struct seq_file *s, void *v)
  3934. {
  3935. struct gpio_device *gdev = v;
  3936. struct gpio_chip *gc = gdev->chip;
  3937. struct device *parent;
  3938. if (!gc) {
  3939. seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
  3940. dev_name(&gdev->dev));
  3941. return 0;
  3942. }
  3943. seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
  3944. dev_name(&gdev->dev),
  3945. gdev->base, gdev->base + gdev->ngpio - 1);
  3946. parent = gc->parent;
  3947. if (parent)
  3948. seq_printf(s, ", parent: %s/%s",
  3949. parent->bus ? parent->bus->name : "no-bus",
  3950. dev_name(parent));
  3951. if (gc->label)
  3952. seq_printf(s, ", %s", gc->label);
  3953. if (gc->can_sleep)
  3954. seq_printf(s, ", can sleep");
  3955. seq_printf(s, ":\n");
  3956. if (gc->dbg_show)
  3957. gc->dbg_show(s, gc);
  3958. else
  3959. gpiolib_dbg_show(s, gdev);
  3960. return 0;
  3961. }
  3962. static const struct seq_operations gpiolib_sops = {
  3963. .start = gpiolib_seq_start,
  3964. .next = gpiolib_seq_next,
  3965. .stop = gpiolib_seq_stop,
  3966. .show = gpiolib_seq_show,
  3967. };
  3968. DEFINE_SEQ_ATTRIBUTE(gpiolib);
  3969. static int __init gpiolib_debugfs_init(void)
  3970. {
  3971. /* /sys/kernel/debug/gpio */
  3972. debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
  3973. return 0;
  3974. }
  3975. subsys_initcall(gpiolib_debugfs_init);
  3976. #endif /* DEBUG_FS */