gpiolib.c 99 KB

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