gpiolib.c 116 KB

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