gpiolib.c 76 KB

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