gpiolib.c 97 KB

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