gpiolib.c 100 KB

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