gpiolib.c 75 KB

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