gpiolib.c 117 KB

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