gpiolib.c 109 KB

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