gpiolib.c 80 KB

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