gpiolib.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  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 "gpiolib.h"
  18. #define CREATE_TRACE_POINTS
  19. #include <trace/events/gpio.h>
  20. /* Implementation infrastructure for GPIO interfaces.
  21. *
  22. * The GPIO programming interface allows for inlining speed-critical
  23. * get/set operations for common cases, so that access to SOC-integrated
  24. * GPIOs can sometimes cost only an instruction or two per bit.
  25. */
  26. /* When debugging, extend minimal trust to callers and platform code.
  27. * Also emit diagnostic messages that may help initial bringup, when
  28. * board setup or driver bugs are most common.
  29. *
  30. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  31. */
  32. #ifdef DEBUG
  33. #define extra_checks 1
  34. #else
  35. #define extra_checks 0
  36. #endif
  37. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  38. * While any GPIO is requested, its gpio_chip is not removable;
  39. * each GPIO's "requested" flag serves as a lock and refcount.
  40. */
  41. static DEFINE_SPINLOCK(gpio_lock);
  42. struct gpio_desc {
  43. struct gpio_chip *chip;
  44. unsigned long flags;
  45. /* flag symbols are bit numbers */
  46. #define FLAG_REQUESTED 0
  47. #define FLAG_IS_OUT 1
  48. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  49. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  50. #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
  51. #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
  52. #define FLAG_ACTIVE_LOW 6 /* value has active low */
  53. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  54. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  55. #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
  56. #define ID_SHIFT 16 /* add new flags before this one */
  57. #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
  58. #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
  59. #ifdef CONFIG_DEBUG_FS
  60. const char *label;
  61. #endif
  62. };
  63. static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
  64. #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
  65. static DEFINE_MUTEX(gpio_lookup_lock);
  66. static LIST_HEAD(gpio_lookup_list);
  67. static LIST_HEAD(gpio_chips);
  68. #ifdef CONFIG_GPIO_SYSFS
  69. static DEFINE_IDR(dirent_idr);
  70. #endif
  71. static int gpiod_request(struct gpio_desc *desc, const char *label);
  72. static void gpiod_free(struct gpio_desc *desc);
  73. /* With descriptor prefix */
  74. #ifdef CONFIG_DEBUG_FS
  75. #define gpiod_emerg(desc, fmt, ...) \
  76. pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  77. ##__VA_ARGS__)
  78. #define gpiod_crit(desc, fmt, ...) \
  79. pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  80. ##__VA_ARGS__)
  81. #define gpiod_err(desc, fmt, ...) \
  82. pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  83. ##__VA_ARGS__)
  84. #define gpiod_warn(desc, fmt, ...) \
  85. pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  86. ##__VA_ARGS__)
  87. #define gpiod_info(desc, fmt, ...) \
  88. pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
  89. ##__VA_ARGS__)
  90. #define gpiod_dbg(desc, fmt, ...) \
  91. pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
  92. ##__VA_ARGS__)
  93. #else
  94. #define gpiod_emerg(desc, fmt, ...) \
  95. pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  96. #define gpiod_crit(desc, fmt, ...) \
  97. pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  98. #define gpiod_err(desc, fmt, ...) \
  99. pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  100. #define gpiod_warn(desc, fmt, ...) \
  101. pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  102. #define gpiod_info(desc, fmt, ...) \
  103. pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  104. #define gpiod_dbg(desc, fmt, ...) \
  105. pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
  106. #endif
  107. /* With chip prefix */
  108. #define chip_emerg(chip, fmt, ...) \
  109. pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  110. #define chip_crit(chip, fmt, ...) \
  111. pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  112. #define chip_err(chip, fmt, ...) \
  113. pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  114. #define chip_warn(chip, fmt, ...) \
  115. pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  116. #define chip_info(chip, fmt, ...) \
  117. pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  118. #define chip_dbg(chip, fmt, ...) \
  119. pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
  120. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  121. {
  122. #ifdef CONFIG_DEBUG_FS
  123. d->label = label;
  124. #endif
  125. }
  126. /*
  127. * Return the GPIO number of the passed descriptor relative to its chip
  128. */
  129. static int gpio_chip_hwgpio(const struct gpio_desc *desc)
  130. {
  131. return desc - &desc->chip->desc[0];
  132. }
  133. /**
  134. * Convert a GPIO number to its descriptor
  135. */
  136. struct gpio_desc *gpio_to_desc(unsigned gpio)
  137. {
  138. if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
  139. return NULL;
  140. else
  141. return &gpio_desc[gpio];
  142. }
  143. EXPORT_SYMBOL_GPL(gpio_to_desc);
  144. /**
  145. * Convert an offset on a certain chip to a corresponding descriptor
  146. */
  147. static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
  148. unsigned int offset)
  149. {
  150. if (offset >= chip->ngpio)
  151. return ERR_PTR(-EINVAL);
  152. return &chip->desc[offset];
  153. }
  154. /**
  155. * Convert a GPIO descriptor to the integer namespace.
  156. * This should disappear in the future but is needed since we still
  157. * use GPIO numbers for error messages and sysfs nodes
  158. */
  159. int desc_to_gpio(const struct gpio_desc *desc)
  160. {
  161. return desc - &gpio_desc[0];
  162. }
  163. EXPORT_SYMBOL_GPL(desc_to_gpio);
  164. /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
  165. * when setting direction, and otherwise illegal. Until board setup code
  166. * and drivers use explicit requests everywhere (which won't happen when
  167. * those calls have no teeth) we can't avoid autorequesting. This nag
  168. * message should motivate switching to explicit requests... so should
  169. * the weaker cleanup after faults, compared to gpio_request().
  170. *
  171. * NOTE: the autorequest mechanism is going away; at this point it's
  172. * only "legal" in the sense that (old) code using it won't break yet,
  173. * but instead only triggers a WARN() stack dump.
  174. */
  175. static int gpio_ensure_requested(struct gpio_desc *desc)
  176. {
  177. const struct gpio_chip *chip = desc->chip;
  178. const int gpio = desc_to_gpio(desc);
  179. if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
  180. "autorequest GPIO-%d\n", gpio)) {
  181. if (!try_module_get(chip->owner)) {
  182. gpiod_err(desc, "%s: module can't be gotten\n",
  183. __func__);
  184. clear_bit(FLAG_REQUESTED, &desc->flags);
  185. /* lose */
  186. return -EIO;
  187. }
  188. desc_set_label(desc, "[auto]");
  189. /* caller must chip->request() w/o spinlock */
  190. if (chip->request)
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. /**
  196. * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
  197. * @desc: descriptor to return the chip of
  198. */
  199. struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  200. {
  201. return desc ? desc->chip : NULL;
  202. }
  203. EXPORT_SYMBOL_GPL(gpiod_to_chip);
  204. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  205. static int gpiochip_find_base(int ngpio)
  206. {
  207. struct gpio_chip *chip;
  208. int base = ARCH_NR_GPIOS - ngpio;
  209. list_for_each_entry_reverse(chip, &gpio_chips, list) {
  210. /* found a free space? */
  211. if (chip->base + chip->ngpio <= base)
  212. break;
  213. else
  214. /* nope, check the space right before the chip */
  215. base = chip->base - ngpio;
  216. }
  217. if (gpio_is_valid(base)) {
  218. pr_debug("%s: found new base at %d\n", __func__, base);
  219. return base;
  220. } else {
  221. pr_err("%s: cannot find free range\n", __func__);
  222. return -ENOSPC;
  223. }
  224. }
  225. /**
  226. * gpiod_get_direction - return the current direction of a GPIO
  227. * @desc: GPIO to get the direction of
  228. *
  229. * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
  230. *
  231. * This function may sleep if gpiod_cansleep() is true.
  232. */
  233. int gpiod_get_direction(const struct gpio_desc *desc)
  234. {
  235. struct gpio_chip *chip;
  236. unsigned offset;
  237. int status = -EINVAL;
  238. chip = gpiod_to_chip(desc);
  239. offset = gpio_chip_hwgpio(desc);
  240. if (!chip->get_direction)
  241. return status;
  242. status = chip->get_direction(chip, offset);
  243. if (status > 0) {
  244. /* GPIOF_DIR_IN, or other positive */
  245. status = 1;
  246. /* FLAG_IS_OUT is just a cache of the result of get_direction(),
  247. * so it does not affect constness per se */
  248. clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
  249. }
  250. if (status == 0) {
  251. /* GPIOF_DIR_OUT */
  252. set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
  253. }
  254. return status;
  255. }
  256. EXPORT_SYMBOL_GPL(gpiod_get_direction);
  257. #ifdef CONFIG_GPIO_SYSFS
  258. /* lock protects against unexport_gpio() being called while
  259. * sysfs files are active.
  260. */
  261. static DEFINE_MUTEX(sysfs_lock);
  262. /*
  263. * /sys/class/gpio/gpioN... only for GPIOs that are exported
  264. * /direction
  265. * * MAY BE OMITTED if kernel won't allow direction changes
  266. * * is read/write as "in" or "out"
  267. * * may also be written as "high" or "low", initializing
  268. * output value as specified ("out" implies "low")
  269. * /value
  270. * * always readable, subject to hardware behavior
  271. * * may be writable, as zero/nonzero
  272. * /edge
  273. * * configures behavior of poll(2) on /value
  274. * * available only if pin can generate IRQs on input
  275. * * is read/write as "none", "falling", "rising", or "both"
  276. * /active_low
  277. * * configures polarity of /value
  278. * * is read/write as zero/nonzero
  279. * * also affects existing and subsequent "falling" and "rising"
  280. * /edge configuration
  281. */
  282. static ssize_t gpio_direction_show(struct device *dev,
  283. struct device_attribute *attr, char *buf)
  284. {
  285. const struct gpio_desc *desc = dev_get_drvdata(dev);
  286. ssize_t status;
  287. mutex_lock(&sysfs_lock);
  288. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  289. status = -EIO;
  290. } else {
  291. gpiod_get_direction(desc);
  292. status = sprintf(buf, "%s\n",
  293. test_bit(FLAG_IS_OUT, &desc->flags)
  294. ? "out" : "in");
  295. }
  296. mutex_unlock(&sysfs_lock);
  297. return status;
  298. }
  299. static ssize_t gpio_direction_store(struct device *dev,
  300. struct device_attribute *attr, const char *buf, size_t size)
  301. {
  302. struct gpio_desc *desc = dev_get_drvdata(dev);
  303. ssize_t status;
  304. mutex_lock(&sysfs_lock);
  305. if (!test_bit(FLAG_EXPORT, &desc->flags))
  306. status = -EIO;
  307. else if (sysfs_streq(buf, "high"))
  308. status = gpiod_direction_output(desc, 1);
  309. else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
  310. status = gpiod_direction_output(desc, 0);
  311. else if (sysfs_streq(buf, "in"))
  312. status = gpiod_direction_input(desc);
  313. else
  314. status = -EINVAL;
  315. mutex_unlock(&sysfs_lock);
  316. return status ? : size;
  317. }
  318. static /* const */ DEVICE_ATTR(direction, 0644,
  319. gpio_direction_show, gpio_direction_store);
  320. static ssize_t gpio_value_show(struct device *dev,
  321. struct device_attribute *attr, char *buf)
  322. {
  323. struct gpio_desc *desc = dev_get_drvdata(dev);
  324. ssize_t status;
  325. mutex_lock(&sysfs_lock);
  326. if (!test_bit(FLAG_EXPORT, &desc->flags))
  327. status = -EIO;
  328. else
  329. status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
  330. mutex_unlock(&sysfs_lock);
  331. return status;
  332. }
  333. static ssize_t gpio_value_store(struct device *dev,
  334. struct device_attribute *attr, const char *buf, size_t size)
  335. {
  336. struct gpio_desc *desc = dev_get_drvdata(dev);
  337. ssize_t status;
  338. mutex_lock(&sysfs_lock);
  339. if (!test_bit(FLAG_EXPORT, &desc->flags))
  340. status = -EIO;
  341. else if (!test_bit(FLAG_IS_OUT, &desc->flags))
  342. status = -EPERM;
  343. else {
  344. long value;
  345. status = kstrtol(buf, 0, &value);
  346. if (status == 0) {
  347. gpiod_set_value_cansleep(desc, value);
  348. status = size;
  349. }
  350. }
  351. mutex_unlock(&sysfs_lock);
  352. return status;
  353. }
  354. static const DEVICE_ATTR(value, 0644,
  355. gpio_value_show, gpio_value_store);
  356. static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
  357. {
  358. struct kernfs_node *value_sd = priv;
  359. sysfs_notify_dirent(value_sd);
  360. return IRQ_HANDLED;
  361. }
  362. static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
  363. unsigned long gpio_flags)
  364. {
  365. struct kernfs_node *value_sd;
  366. unsigned long irq_flags;
  367. int ret, irq, id;
  368. if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
  369. return 0;
  370. irq = gpiod_to_irq(desc);
  371. if (irq < 0)
  372. return -EIO;
  373. id = desc->flags >> ID_SHIFT;
  374. value_sd = idr_find(&dirent_idr, id);
  375. if (value_sd)
  376. free_irq(irq, value_sd);
  377. desc->flags &= ~GPIO_TRIGGER_MASK;
  378. if (!gpio_flags) {
  379. gpiod_unlock_as_irq(desc);
  380. ret = 0;
  381. goto free_id;
  382. }
  383. irq_flags = IRQF_SHARED;
  384. if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
  385. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  386. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  387. if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
  388. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  389. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  390. if (!value_sd) {
  391. value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
  392. if (!value_sd) {
  393. ret = -ENODEV;
  394. goto err_out;
  395. }
  396. ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
  397. if (ret < 0)
  398. goto free_sd;
  399. id = ret;
  400. desc->flags &= GPIO_FLAGS_MASK;
  401. desc->flags |= (unsigned long)id << ID_SHIFT;
  402. if (desc->flags >> ID_SHIFT != id) {
  403. ret = -ERANGE;
  404. goto free_id;
  405. }
  406. }
  407. ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
  408. "gpiolib", value_sd);
  409. if (ret < 0)
  410. goto free_id;
  411. ret = gpiod_lock_as_irq(desc);
  412. if (ret < 0) {
  413. gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
  414. goto free_id;
  415. }
  416. desc->flags |= gpio_flags;
  417. return 0;
  418. free_id:
  419. idr_remove(&dirent_idr, id);
  420. desc->flags &= GPIO_FLAGS_MASK;
  421. free_sd:
  422. if (value_sd)
  423. sysfs_put(value_sd);
  424. err_out:
  425. return ret;
  426. }
  427. static const struct {
  428. const char *name;
  429. unsigned long flags;
  430. } trigger_types[] = {
  431. { "none", 0 },
  432. { "falling", BIT(FLAG_TRIG_FALL) },
  433. { "rising", BIT(FLAG_TRIG_RISE) },
  434. { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
  435. };
  436. static ssize_t gpio_edge_show(struct device *dev,
  437. struct device_attribute *attr, char *buf)
  438. {
  439. const struct gpio_desc *desc = dev_get_drvdata(dev);
  440. ssize_t status;
  441. mutex_lock(&sysfs_lock);
  442. if (!test_bit(FLAG_EXPORT, &desc->flags))
  443. status = -EIO;
  444. else {
  445. int i;
  446. status = 0;
  447. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  448. if ((desc->flags & GPIO_TRIGGER_MASK)
  449. == trigger_types[i].flags) {
  450. status = sprintf(buf, "%s\n",
  451. trigger_types[i].name);
  452. break;
  453. }
  454. }
  455. mutex_unlock(&sysfs_lock);
  456. return status;
  457. }
  458. static ssize_t gpio_edge_store(struct device *dev,
  459. struct device_attribute *attr, const char *buf, size_t size)
  460. {
  461. struct gpio_desc *desc = dev_get_drvdata(dev);
  462. ssize_t status;
  463. int i;
  464. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  465. if (sysfs_streq(trigger_types[i].name, buf))
  466. goto found;
  467. return -EINVAL;
  468. found:
  469. mutex_lock(&sysfs_lock);
  470. if (!test_bit(FLAG_EXPORT, &desc->flags))
  471. status = -EIO;
  472. else {
  473. status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
  474. if (!status)
  475. status = size;
  476. }
  477. mutex_unlock(&sysfs_lock);
  478. return status;
  479. }
  480. static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
  481. static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
  482. int value)
  483. {
  484. int status = 0;
  485. if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
  486. return 0;
  487. if (value)
  488. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  489. else
  490. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  491. /* reconfigure poll(2) support if enabled on one edge only */
  492. if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
  493. !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
  494. unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
  495. gpio_setup_irq(desc, dev, 0);
  496. status = gpio_setup_irq(desc, dev, trigger_flags);
  497. }
  498. return status;
  499. }
  500. static ssize_t gpio_active_low_show(struct device *dev,
  501. struct device_attribute *attr, char *buf)
  502. {
  503. const struct gpio_desc *desc = dev_get_drvdata(dev);
  504. ssize_t status;
  505. mutex_lock(&sysfs_lock);
  506. if (!test_bit(FLAG_EXPORT, &desc->flags))
  507. status = -EIO;
  508. else
  509. status = sprintf(buf, "%d\n",
  510. !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
  511. mutex_unlock(&sysfs_lock);
  512. return status;
  513. }
  514. static ssize_t gpio_active_low_store(struct device *dev,
  515. struct device_attribute *attr, const char *buf, size_t size)
  516. {
  517. struct gpio_desc *desc = dev_get_drvdata(dev);
  518. ssize_t status;
  519. mutex_lock(&sysfs_lock);
  520. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  521. status = -EIO;
  522. } else {
  523. long value;
  524. status = kstrtol(buf, 0, &value);
  525. if (status == 0)
  526. status = sysfs_set_active_low(desc, dev, value != 0);
  527. }
  528. mutex_unlock(&sysfs_lock);
  529. return status ? : size;
  530. }
  531. static const DEVICE_ATTR(active_low, 0644,
  532. gpio_active_low_show, gpio_active_low_store);
  533. static const struct attribute *gpio_attrs[] = {
  534. &dev_attr_value.attr,
  535. &dev_attr_active_low.attr,
  536. NULL,
  537. };
  538. static const struct attribute_group gpio_attr_group = {
  539. .attrs = (struct attribute **) gpio_attrs,
  540. };
  541. /*
  542. * /sys/class/gpio/gpiochipN/
  543. * /base ... matching gpio_chip.base (N)
  544. * /label ... matching gpio_chip.label
  545. * /ngpio ... matching gpio_chip.ngpio
  546. */
  547. static ssize_t chip_base_show(struct device *dev,
  548. struct device_attribute *attr, char *buf)
  549. {
  550. const struct gpio_chip *chip = dev_get_drvdata(dev);
  551. return sprintf(buf, "%d\n", chip->base);
  552. }
  553. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  554. static ssize_t chip_label_show(struct device *dev,
  555. struct device_attribute *attr, char *buf)
  556. {
  557. const struct gpio_chip *chip = dev_get_drvdata(dev);
  558. return sprintf(buf, "%s\n", chip->label ? : "");
  559. }
  560. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  561. static ssize_t chip_ngpio_show(struct device *dev,
  562. struct device_attribute *attr, char *buf)
  563. {
  564. const struct gpio_chip *chip = dev_get_drvdata(dev);
  565. return sprintf(buf, "%u\n", chip->ngpio);
  566. }
  567. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  568. static const struct attribute *gpiochip_attrs[] = {
  569. &dev_attr_base.attr,
  570. &dev_attr_label.attr,
  571. &dev_attr_ngpio.attr,
  572. NULL,
  573. };
  574. static const struct attribute_group gpiochip_attr_group = {
  575. .attrs = (struct attribute **) gpiochip_attrs,
  576. };
  577. /*
  578. * /sys/class/gpio/export ... write-only
  579. * integer N ... number of GPIO to export (full access)
  580. * /sys/class/gpio/unexport ... write-only
  581. * integer N ... number of GPIO to unexport
  582. */
  583. static ssize_t export_store(struct class *class,
  584. struct class_attribute *attr,
  585. const char *buf, size_t len)
  586. {
  587. long gpio;
  588. struct gpio_desc *desc;
  589. int status;
  590. status = kstrtol(buf, 0, &gpio);
  591. if (status < 0)
  592. goto done;
  593. desc = gpio_to_desc(gpio);
  594. /* reject invalid GPIOs */
  595. if (!desc) {
  596. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  597. return -EINVAL;
  598. }
  599. /* No extra locking here; FLAG_SYSFS just signifies that the
  600. * request and export were done by on behalf of userspace, so
  601. * they may be undone on its behalf too.
  602. */
  603. status = gpiod_request(desc, "sysfs");
  604. if (status < 0) {
  605. if (status == -EPROBE_DEFER)
  606. status = -ENODEV;
  607. goto done;
  608. }
  609. status = gpiod_export(desc, true);
  610. if (status < 0)
  611. gpiod_free(desc);
  612. else
  613. set_bit(FLAG_SYSFS, &desc->flags);
  614. done:
  615. if (status)
  616. pr_debug("%s: status %d\n", __func__, status);
  617. return status ? : len;
  618. }
  619. static ssize_t unexport_store(struct class *class,
  620. struct class_attribute *attr,
  621. const char *buf, size_t len)
  622. {
  623. long gpio;
  624. struct gpio_desc *desc;
  625. int status;
  626. status = kstrtol(buf, 0, &gpio);
  627. if (status < 0)
  628. goto done;
  629. desc = gpio_to_desc(gpio);
  630. /* reject bogus commands (gpio_unexport ignores them) */
  631. if (!desc) {
  632. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  633. return -EINVAL;
  634. }
  635. status = -EINVAL;
  636. /* No extra locking here; FLAG_SYSFS just signifies that the
  637. * request and export were done by on behalf of userspace, so
  638. * they may be undone on its behalf too.
  639. */
  640. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
  641. status = 0;
  642. gpiod_free(desc);
  643. }
  644. done:
  645. if (status)
  646. pr_debug("%s: status %d\n", __func__, status);
  647. return status ? : len;
  648. }
  649. static struct class_attribute gpio_class_attrs[] = {
  650. __ATTR(export, 0200, NULL, export_store),
  651. __ATTR(unexport, 0200, NULL, unexport_store),
  652. __ATTR_NULL,
  653. };
  654. static struct class gpio_class = {
  655. .name = "gpio",
  656. .owner = THIS_MODULE,
  657. .class_attrs = gpio_class_attrs,
  658. };
  659. /**
  660. * gpiod_export - export a GPIO through sysfs
  661. * @gpio: gpio to make available, already requested
  662. * @direction_may_change: true if userspace may change gpio direction
  663. * Context: arch_initcall or later
  664. *
  665. * When drivers want to make a GPIO accessible to userspace after they
  666. * have requested it -- perhaps while debugging, or as part of their
  667. * public interface -- they may use this routine. If the GPIO can
  668. * change direction (some can't) and the caller allows it, userspace
  669. * will see "direction" sysfs attribute which may be used to change
  670. * the gpio's direction. A "value" attribute will always be provided.
  671. *
  672. * Returns zero on success, else an error.
  673. */
  674. int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  675. {
  676. unsigned long flags;
  677. int status;
  678. const char *ioname = NULL;
  679. struct device *dev;
  680. int offset;
  681. /* can't export until sysfs is available ... */
  682. if (!gpio_class.p) {
  683. pr_debug("%s: called too early!\n", __func__);
  684. return -ENOENT;
  685. }
  686. if (!desc) {
  687. pr_debug("%s: invalid gpio descriptor\n", __func__);
  688. return -EINVAL;
  689. }
  690. mutex_lock(&sysfs_lock);
  691. spin_lock_irqsave(&gpio_lock, flags);
  692. if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
  693. test_bit(FLAG_EXPORT, &desc->flags)) {
  694. spin_unlock_irqrestore(&gpio_lock, flags);
  695. gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
  696. __func__,
  697. test_bit(FLAG_REQUESTED, &desc->flags),
  698. test_bit(FLAG_EXPORT, &desc->flags));
  699. status = -EPERM;
  700. goto fail_unlock;
  701. }
  702. if (!desc->chip->direction_input || !desc->chip->direction_output)
  703. direction_may_change = false;
  704. spin_unlock_irqrestore(&gpio_lock, flags);
  705. offset = gpio_chip_hwgpio(desc);
  706. if (desc->chip->names && desc->chip->names[offset])
  707. ioname = desc->chip->names[offset];
  708. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  709. desc, ioname ? ioname : "gpio%u",
  710. desc_to_gpio(desc));
  711. if (IS_ERR(dev)) {
  712. status = PTR_ERR(dev);
  713. goto fail_unlock;
  714. }
  715. status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
  716. if (status)
  717. goto fail_unregister_device;
  718. if (direction_may_change) {
  719. status = device_create_file(dev, &dev_attr_direction);
  720. if (status)
  721. goto fail_unregister_device;
  722. }
  723. if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
  724. !test_bit(FLAG_IS_OUT, &desc->flags))) {
  725. status = device_create_file(dev, &dev_attr_edge);
  726. if (status)
  727. goto fail_unregister_device;
  728. }
  729. set_bit(FLAG_EXPORT, &desc->flags);
  730. mutex_unlock(&sysfs_lock);
  731. return 0;
  732. fail_unregister_device:
  733. device_unregister(dev);
  734. fail_unlock:
  735. mutex_unlock(&sysfs_lock);
  736. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  737. return status;
  738. }
  739. EXPORT_SYMBOL_GPL(gpiod_export);
  740. static int match_export(struct device *dev, const void *data)
  741. {
  742. return dev_get_drvdata(dev) == data;
  743. }
  744. /**
  745. * gpiod_export_link - create a sysfs link to an exported GPIO node
  746. * @dev: device under which to create symlink
  747. * @name: name of the symlink
  748. * @gpio: gpio to create symlink to, already exported
  749. *
  750. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  751. * node. Caller is responsible for unlinking.
  752. *
  753. * Returns zero on success, else an error.
  754. */
  755. int gpiod_export_link(struct device *dev, const char *name,
  756. struct gpio_desc *desc)
  757. {
  758. int status = -EINVAL;
  759. if (!desc) {
  760. pr_warn("%s: invalid GPIO\n", __func__);
  761. return -EINVAL;
  762. }
  763. mutex_lock(&sysfs_lock);
  764. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  765. struct device *tdev;
  766. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  767. if (tdev != NULL) {
  768. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  769. name);
  770. } else {
  771. status = -ENODEV;
  772. }
  773. }
  774. mutex_unlock(&sysfs_lock);
  775. if (status)
  776. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  777. return status;
  778. }
  779. EXPORT_SYMBOL_GPL(gpiod_export_link);
  780. /**
  781. * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
  782. * @gpio: gpio to change
  783. * @value: non-zero to use active low, i.e. inverted values
  784. *
  785. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  786. * The GPIO does not have to be exported yet. If poll(2) support has
  787. * been enabled for either rising or falling edge, it will be
  788. * reconfigured to follow the new polarity.
  789. *
  790. * Returns zero on success, else an error.
  791. */
  792. int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  793. {
  794. struct device *dev = NULL;
  795. int status = -EINVAL;
  796. if (!desc) {
  797. pr_warn("%s: invalid GPIO\n", __func__);
  798. return -EINVAL;
  799. }
  800. mutex_lock(&sysfs_lock);
  801. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  802. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  803. if (dev == NULL) {
  804. status = -ENODEV;
  805. goto unlock;
  806. }
  807. }
  808. status = sysfs_set_active_low(desc, dev, value);
  809. unlock:
  810. mutex_unlock(&sysfs_lock);
  811. if (status)
  812. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  813. return status;
  814. }
  815. EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
  816. /**
  817. * gpiod_unexport - reverse effect of gpio_export()
  818. * @gpio: gpio to make unavailable
  819. *
  820. * This is implicit on gpio_free().
  821. */
  822. void gpiod_unexport(struct gpio_desc *desc)
  823. {
  824. int status = 0;
  825. struct device *dev = NULL;
  826. if (!desc) {
  827. pr_warn("%s: invalid GPIO\n", __func__);
  828. return;
  829. }
  830. mutex_lock(&sysfs_lock);
  831. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  832. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  833. if (dev) {
  834. gpio_setup_irq(desc, dev, 0);
  835. clear_bit(FLAG_EXPORT, &desc->flags);
  836. } else
  837. status = -ENODEV;
  838. }
  839. mutex_unlock(&sysfs_lock);
  840. if (dev) {
  841. device_unregister(dev);
  842. put_device(dev);
  843. }
  844. if (status)
  845. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  846. }
  847. EXPORT_SYMBOL_GPL(gpiod_unexport);
  848. static int gpiochip_export(struct gpio_chip *chip)
  849. {
  850. int status;
  851. struct device *dev;
  852. /* Many systems register gpio chips for SOC support very early,
  853. * before driver model support is available. In those cases we
  854. * export this later, in gpiolib_sysfs_init() ... here we just
  855. * verify that _some_ field of gpio_class got initialized.
  856. */
  857. if (!gpio_class.p)
  858. return 0;
  859. /* use chip->base for the ID; it's already known to be unique */
  860. mutex_lock(&sysfs_lock);
  861. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  862. "gpiochip%d", chip->base);
  863. if (!IS_ERR(dev)) {
  864. status = sysfs_create_group(&dev->kobj,
  865. &gpiochip_attr_group);
  866. } else
  867. status = PTR_ERR(dev);
  868. chip->exported = (status == 0);
  869. mutex_unlock(&sysfs_lock);
  870. if (status) {
  871. unsigned long flags;
  872. unsigned gpio;
  873. spin_lock_irqsave(&gpio_lock, flags);
  874. gpio = 0;
  875. while (gpio < chip->ngpio)
  876. chip->desc[gpio++].chip = NULL;
  877. spin_unlock_irqrestore(&gpio_lock, flags);
  878. chip_dbg(chip, "%s: status %d\n", __func__, status);
  879. }
  880. return status;
  881. }
  882. static void gpiochip_unexport(struct gpio_chip *chip)
  883. {
  884. int status;
  885. struct device *dev;
  886. mutex_lock(&sysfs_lock);
  887. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  888. if (dev) {
  889. put_device(dev);
  890. device_unregister(dev);
  891. chip->exported = false;
  892. status = 0;
  893. } else
  894. status = -ENODEV;
  895. mutex_unlock(&sysfs_lock);
  896. if (status)
  897. chip_dbg(chip, "%s: status %d\n", __func__, status);
  898. }
  899. static int __init gpiolib_sysfs_init(void)
  900. {
  901. int status;
  902. unsigned long flags;
  903. struct gpio_chip *chip;
  904. status = class_register(&gpio_class);
  905. if (status < 0)
  906. return status;
  907. /* Scan and register the gpio_chips which registered very
  908. * early (e.g. before the class_register above was called).
  909. *
  910. * We run before arch_initcall() so chip->dev nodes can have
  911. * registered, and so arch_initcall() can always gpio_export().
  912. */
  913. spin_lock_irqsave(&gpio_lock, flags);
  914. list_for_each_entry(chip, &gpio_chips, list) {
  915. if (!chip || chip->exported)
  916. continue;
  917. spin_unlock_irqrestore(&gpio_lock, flags);
  918. status = gpiochip_export(chip);
  919. spin_lock_irqsave(&gpio_lock, flags);
  920. }
  921. spin_unlock_irqrestore(&gpio_lock, flags);
  922. return status;
  923. }
  924. postcore_initcall(gpiolib_sysfs_init);
  925. #else
  926. static inline int gpiochip_export(struct gpio_chip *chip)
  927. {
  928. return 0;
  929. }
  930. static inline void gpiochip_unexport(struct gpio_chip *chip)
  931. {
  932. }
  933. #endif /* CONFIG_GPIO_SYSFS */
  934. /*
  935. * Add a new chip to the global chips list, keeping the list of chips sorted
  936. * by base order.
  937. *
  938. * Return -EBUSY if the new chip overlaps with some other chip's integer
  939. * space.
  940. */
  941. static int gpiochip_add_to_list(struct gpio_chip *chip)
  942. {
  943. struct list_head *pos = &gpio_chips;
  944. struct gpio_chip *_chip;
  945. int err = 0;
  946. /* find where to insert our chip */
  947. list_for_each(pos, &gpio_chips) {
  948. _chip = list_entry(pos, struct gpio_chip, list);
  949. /* shall we insert before _chip? */
  950. if (_chip->base >= chip->base + chip->ngpio)
  951. break;
  952. }
  953. /* are we stepping on the chip right before? */
  954. if (pos != &gpio_chips && pos->prev != &gpio_chips) {
  955. _chip = list_entry(pos->prev, struct gpio_chip, list);
  956. if (_chip->base + _chip->ngpio > chip->base) {
  957. dev_err(chip->dev,
  958. "GPIO integer space overlap, cannot add chip\n");
  959. err = -EBUSY;
  960. }
  961. }
  962. if (!err)
  963. list_add_tail(&chip->list, pos);
  964. return err;
  965. }
  966. /**
  967. * gpiochip_add() - register a gpio_chip
  968. * @chip: the chip to register, with chip->base initialized
  969. * Context: potentially before irqs or kmalloc will work
  970. *
  971. * Returns a negative errno if the chip can't be registered, such as
  972. * because the chip->base is invalid or already associated with a
  973. * different chip. Otherwise it returns zero as a success code.
  974. *
  975. * When gpiochip_add() is called very early during boot, so that GPIOs
  976. * can be freely used, the chip->dev device must be registered before
  977. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  978. * for GPIOs will fail rudely.
  979. *
  980. * If chip->base is negative, this requests dynamic assignment of
  981. * a range of valid GPIOs.
  982. */
  983. int gpiochip_add(struct gpio_chip *chip)
  984. {
  985. unsigned long flags;
  986. int status = 0;
  987. unsigned id;
  988. int base = chip->base;
  989. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  990. && base >= 0) {
  991. status = -EINVAL;
  992. goto fail;
  993. }
  994. spin_lock_irqsave(&gpio_lock, flags);
  995. if (base < 0) {
  996. base = gpiochip_find_base(chip->ngpio);
  997. if (base < 0) {
  998. status = base;
  999. goto unlock;
  1000. }
  1001. chip->base = base;
  1002. }
  1003. status = gpiochip_add_to_list(chip);
  1004. if (status == 0) {
  1005. chip->desc = &gpio_desc[chip->base];
  1006. for (id = 0; id < chip->ngpio; id++) {
  1007. struct gpio_desc *desc = &chip->desc[id];
  1008. desc->chip = chip;
  1009. /* REVISIT: most hardware initializes GPIOs as
  1010. * inputs (often with pullups enabled) so power
  1011. * usage is minimized. Linux code should set the
  1012. * gpio direction first thing; but until it does,
  1013. * and in case chip->get_direction is not set,
  1014. * we may expose the wrong direction in sysfs.
  1015. */
  1016. desc->flags = !chip->direction_input
  1017. ? (1 << FLAG_IS_OUT)
  1018. : 0;
  1019. }
  1020. }
  1021. spin_unlock_irqrestore(&gpio_lock, flags);
  1022. #ifdef CONFIG_PINCTRL
  1023. INIT_LIST_HEAD(&chip->pin_ranges);
  1024. #endif
  1025. of_gpiochip_add(chip);
  1026. acpi_gpiochip_add(chip);
  1027. if (status)
  1028. goto fail;
  1029. status = gpiochip_export(chip);
  1030. if (status)
  1031. goto fail;
  1032. pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
  1033. chip->base, chip->base + chip->ngpio - 1,
  1034. chip->label ? : "generic");
  1035. return 0;
  1036. unlock:
  1037. spin_unlock_irqrestore(&gpio_lock, flags);
  1038. fail:
  1039. /* failures here can mean systems won't boot... */
  1040. pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
  1041. chip->base, chip->base + chip->ngpio - 1,
  1042. chip->label ? : "generic");
  1043. return status;
  1044. }
  1045. EXPORT_SYMBOL_GPL(gpiochip_add);
  1046. /**
  1047. * gpiochip_remove() - unregister a gpio_chip
  1048. * @chip: the chip to unregister
  1049. *
  1050. * A gpio_chip with any GPIOs still requested may not be removed.
  1051. */
  1052. int gpiochip_remove(struct gpio_chip *chip)
  1053. {
  1054. unsigned long flags;
  1055. int status = 0;
  1056. unsigned id;
  1057. spin_lock_irqsave(&gpio_lock, flags);
  1058. gpiochip_remove_pin_ranges(chip);
  1059. of_gpiochip_remove(chip);
  1060. acpi_gpiochip_remove(chip);
  1061. for (id = 0; id < chip->ngpio; id++) {
  1062. if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
  1063. status = -EBUSY;
  1064. break;
  1065. }
  1066. }
  1067. if (status == 0) {
  1068. for (id = 0; id < chip->ngpio; id++)
  1069. chip->desc[id].chip = NULL;
  1070. list_del(&chip->list);
  1071. }
  1072. spin_unlock_irqrestore(&gpio_lock, flags);
  1073. if (status == 0)
  1074. gpiochip_unexport(chip);
  1075. return status;
  1076. }
  1077. EXPORT_SYMBOL_GPL(gpiochip_remove);
  1078. /**
  1079. * gpiochip_find() - iterator for locating a specific gpio_chip
  1080. * @data: data to pass to match function
  1081. * @callback: Callback function to check gpio_chip
  1082. *
  1083. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  1084. * determined by a user supplied @match callback. The callback should return
  1085. * 0 if the device doesn't match and non-zero if it does. If the callback is
  1086. * non-zero, this function will return to the caller and not iterate over any
  1087. * more gpio_chips.
  1088. */
  1089. struct gpio_chip *gpiochip_find(void *data,
  1090. int (*match)(struct gpio_chip *chip,
  1091. void *data))
  1092. {
  1093. struct gpio_chip *chip;
  1094. unsigned long flags;
  1095. spin_lock_irqsave(&gpio_lock, flags);
  1096. list_for_each_entry(chip, &gpio_chips, list)
  1097. if (match(chip, data))
  1098. break;
  1099. /* No match? */
  1100. if (&chip->list == &gpio_chips)
  1101. chip = NULL;
  1102. spin_unlock_irqrestore(&gpio_lock, flags);
  1103. return chip;
  1104. }
  1105. EXPORT_SYMBOL_GPL(gpiochip_find);
  1106. static int gpiochip_match_name(struct gpio_chip *chip, void *data)
  1107. {
  1108. const char *name = data;
  1109. return !strcmp(chip->label, name);
  1110. }
  1111. static struct gpio_chip *find_chip_by_name(const char *name)
  1112. {
  1113. return gpiochip_find((void *)name, gpiochip_match_name);
  1114. }
  1115. #ifdef CONFIG_PINCTRL
  1116. /**
  1117. * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
  1118. * @chip: the gpiochip to add the range for
  1119. * @pinctrl: the dev_name() of the pin controller to map to
  1120. * @gpio_offset: the start offset in the current gpio_chip number space
  1121. * @pin_group: name of the pin group inside the pin controller
  1122. */
  1123. int gpiochip_add_pingroup_range(struct gpio_chip *chip,
  1124. struct pinctrl_dev *pctldev,
  1125. unsigned int gpio_offset, const char *pin_group)
  1126. {
  1127. struct gpio_pin_range *pin_range;
  1128. int ret;
  1129. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1130. if (!pin_range) {
  1131. chip_err(chip, "failed to allocate pin ranges\n");
  1132. return -ENOMEM;
  1133. }
  1134. /* Use local offset as range ID */
  1135. pin_range->range.id = gpio_offset;
  1136. pin_range->range.gc = chip;
  1137. pin_range->range.name = chip->label;
  1138. pin_range->range.base = chip->base + gpio_offset;
  1139. pin_range->pctldev = pctldev;
  1140. ret = pinctrl_get_group_pins(pctldev, pin_group,
  1141. &pin_range->range.pins,
  1142. &pin_range->range.npins);
  1143. if (ret < 0) {
  1144. kfree(pin_range);
  1145. return ret;
  1146. }
  1147. pinctrl_add_gpio_range(pctldev, &pin_range->range);
  1148. chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
  1149. gpio_offset, gpio_offset + pin_range->range.npins - 1,
  1150. pinctrl_dev_get_devname(pctldev), pin_group);
  1151. list_add_tail(&pin_range->node, &chip->pin_ranges);
  1152. return 0;
  1153. }
  1154. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  1155. /**
  1156. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  1157. * @chip: the gpiochip to add the range for
  1158. * @pinctrl_name: the dev_name() of the pin controller to map to
  1159. * @gpio_offset: the start offset in the current gpio_chip number space
  1160. * @pin_offset: the start offset in the pin controller number space
  1161. * @npins: the number of pins from the offset of each pin space (GPIO and
  1162. * pin controller) to accumulate in this range
  1163. */
  1164. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  1165. unsigned int gpio_offset, unsigned int pin_offset,
  1166. unsigned int npins)
  1167. {
  1168. struct gpio_pin_range *pin_range;
  1169. int ret;
  1170. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1171. if (!pin_range) {
  1172. chip_err(chip, "failed to allocate pin ranges\n");
  1173. return -ENOMEM;
  1174. }
  1175. /* Use local offset as range ID */
  1176. pin_range->range.id = gpio_offset;
  1177. pin_range->range.gc = chip;
  1178. pin_range->range.name = chip->label;
  1179. pin_range->range.base = chip->base + gpio_offset;
  1180. pin_range->range.pin_base = pin_offset;
  1181. pin_range->range.npins = npins;
  1182. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1183. &pin_range->range);
  1184. if (IS_ERR(pin_range->pctldev)) {
  1185. ret = PTR_ERR(pin_range->pctldev);
  1186. chip_err(chip, "could not create pin range\n");
  1187. kfree(pin_range);
  1188. return ret;
  1189. }
  1190. chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1191. gpio_offset, gpio_offset + npins - 1,
  1192. pinctl_name,
  1193. pin_offset, pin_offset + npins - 1);
  1194. list_add_tail(&pin_range->node, &chip->pin_ranges);
  1195. return 0;
  1196. }
  1197. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1198. /**
  1199. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1200. * @chip: the chip to remove all the mappings for
  1201. */
  1202. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  1203. {
  1204. struct gpio_pin_range *pin_range, *tmp;
  1205. list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
  1206. list_del(&pin_range->node);
  1207. pinctrl_remove_gpio_range(pin_range->pctldev,
  1208. &pin_range->range);
  1209. kfree(pin_range);
  1210. }
  1211. }
  1212. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1213. #endif /* CONFIG_PINCTRL */
  1214. /* These "optional" allocation calls help prevent drivers from stomping
  1215. * on each other, and help provide better diagnostics in debugfs.
  1216. * They're called even less than the "set direction" calls.
  1217. */
  1218. static int gpiod_request(struct gpio_desc *desc, const char *label)
  1219. {
  1220. struct gpio_chip *chip;
  1221. int status = -EPROBE_DEFER;
  1222. unsigned long flags;
  1223. if (!desc) {
  1224. pr_warn("%s: invalid GPIO\n", __func__);
  1225. return -EINVAL;
  1226. }
  1227. spin_lock_irqsave(&gpio_lock, flags);
  1228. chip = desc->chip;
  1229. if (chip == NULL)
  1230. goto done;
  1231. if (!try_module_get(chip->owner))
  1232. goto done;
  1233. /* NOTE: gpio_request() can be called in early boot,
  1234. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1235. */
  1236. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1237. desc_set_label(desc, label ? : "?");
  1238. status = 0;
  1239. } else {
  1240. status = -EBUSY;
  1241. module_put(chip->owner);
  1242. goto done;
  1243. }
  1244. if (chip->request) {
  1245. /* chip->request may sleep */
  1246. spin_unlock_irqrestore(&gpio_lock, flags);
  1247. status = chip->request(chip, gpio_chip_hwgpio(desc));
  1248. spin_lock_irqsave(&gpio_lock, flags);
  1249. if (status < 0) {
  1250. desc_set_label(desc, NULL);
  1251. module_put(chip->owner);
  1252. clear_bit(FLAG_REQUESTED, &desc->flags);
  1253. goto done;
  1254. }
  1255. }
  1256. if (chip->get_direction) {
  1257. /* chip->get_direction may sleep */
  1258. spin_unlock_irqrestore(&gpio_lock, flags);
  1259. gpiod_get_direction(desc);
  1260. spin_lock_irqsave(&gpio_lock, flags);
  1261. }
  1262. done:
  1263. if (status)
  1264. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  1265. spin_unlock_irqrestore(&gpio_lock, flags);
  1266. return status;
  1267. }
  1268. int gpio_request(unsigned gpio, const char *label)
  1269. {
  1270. return gpiod_request(gpio_to_desc(gpio), label);
  1271. }
  1272. EXPORT_SYMBOL_GPL(gpio_request);
  1273. static void gpiod_free(struct gpio_desc *desc)
  1274. {
  1275. unsigned long flags;
  1276. struct gpio_chip *chip;
  1277. might_sleep();
  1278. if (!desc) {
  1279. WARN_ON(extra_checks);
  1280. return;
  1281. }
  1282. gpiod_unexport(desc);
  1283. spin_lock_irqsave(&gpio_lock, flags);
  1284. chip = desc->chip;
  1285. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1286. if (chip->free) {
  1287. spin_unlock_irqrestore(&gpio_lock, flags);
  1288. might_sleep_if(chip->can_sleep);
  1289. chip->free(chip, gpio_chip_hwgpio(desc));
  1290. spin_lock_irqsave(&gpio_lock, flags);
  1291. }
  1292. desc_set_label(desc, NULL);
  1293. module_put(desc->chip->owner);
  1294. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1295. clear_bit(FLAG_REQUESTED, &desc->flags);
  1296. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1297. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1298. } else
  1299. WARN_ON(extra_checks);
  1300. spin_unlock_irqrestore(&gpio_lock, flags);
  1301. }
  1302. void gpio_free(unsigned gpio)
  1303. {
  1304. gpiod_free(gpio_to_desc(gpio));
  1305. }
  1306. EXPORT_SYMBOL_GPL(gpio_free);
  1307. /**
  1308. * gpio_request_one - request a single GPIO with initial configuration
  1309. * @gpio: the GPIO number
  1310. * @flags: GPIO configuration as specified by GPIOF_*
  1311. * @label: a literal description string of this GPIO
  1312. */
  1313. int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  1314. {
  1315. struct gpio_desc *desc;
  1316. int err;
  1317. desc = gpio_to_desc(gpio);
  1318. err = gpiod_request(desc, label);
  1319. if (err)
  1320. return err;
  1321. if (flags & GPIOF_OPEN_DRAIN)
  1322. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1323. if (flags & GPIOF_OPEN_SOURCE)
  1324. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1325. if (flags & GPIOF_DIR_IN)
  1326. err = gpiod_direction_input(desc);
  1327. else
  1328. err = gpiod_direction_output(desc,
  1329. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  1330. if (err)
  1331. goto free_gpio;
  1332. if (flags & GPIOF_EXPORT) {
  1333. err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
  1334. if (err)
  1335. goto free_gpio;
  1336. }
  1337. return 0;
  1338. free_gpio:
  1339. gpiod_free(desc);
  1340. return err;
  1341. }
  1342. EXPORT_SYMBOL_GPL(gpio_request_one);
  1343. /**
  1344. * gpio_request_array - request multiple GPIOs in a single call
  1345. * @array: array of the 'struct gpio'
  1346. * @num: how many GPIOs in the array
  1347. */
  1348. int gpio_request_array(const struct gpio *array, size_t num)
  1349. {
  1350. int i, err;
  1351. for (i = 0; i < num; i++, array++) {
  1352. err = gpio_request_one(array->gpio, array->flags, array->label);
  1353. if (err)
  1354. goto err_free;
  1355. }
  1356. return 0;
  1357. err_free:
  1358. while (i--)
  1359. gpio_free((--array)->gpio);
  1360. return err;
  1361. }
  1362. EXPORT_SYMBOL_GPL(gpio_request_array);
  1363. /**
  1364. * gpio_free_array - release multiple GPIOs in a single call
  1365. * @array: array of the 'struct gpio'
  1366. * @num: how many GPIOs in the array
  1367. */
  1368. void gpio_free_array(const struct gpio *array, size_t num)
  1369. {
  1370. while (num--)
  1371. gpio_free((array++)->gpio);
  1372. }
  1373. EXPORT_SYMBOL_GPL(gpio_free_array);
  1374. /**
  1375. * gpiochip_is_requested - return string iff signal was requested
  1376. * @chip: controller managing the signal
  1377. * @offset: of signal within controller's 0..(ngpio - 1) range
  1378. *
  1379. * Returns NULL if the GPIO is not currently requested, else a string.
  1380. * If debugfs support is enabled, the string returned is the label passed
  1381. * to gpio_request(); otherwise it is a meaningless constant.
  1382. *
  1383. * This function is for use by GPIO controller drivers. The label can
  1384. * help with diagnostics, and knowing that the signal is used as a GPIO
  1385. * can help avoid accidentally multiplexing it to another controller.
  1386. */
  1387. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1388. {
  1389. struct gpio_desc *desc;
  1390. if (!GPIO_OFFSET_VALID(chip, offset))
  1391. return NULL;
  1392. desc = &chip->desc[offset];
  1393. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  1394. return NULL;
  1395. #ifdef CONFIG_DEBUG_FS
  1396. return desc->label;
  1397. #else
  1398. return "?";
  1399. #endif
  1400. }
  1401. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1402. /* Drivers MUST set GPIO direction before making get/set calls. In
  1403. * some cases this is done in early boot, before IRQs are enabled.
  1404. *
  1405. * As a rule these aren't called more than once (except for drivers
  1406. * using the open-drain emulation idiom) so these are natural places
  1407. * to accumulate extra debugging checks. Note that we can't (yet)
  1408. * rely on gpio_request() having been called beforehand.
  1409. */
  1410. /**
  1411. * gpiod_direction_input - set the GPIO direction to input
  1412. * @desc: GPIO to set to input
  1413. *
  1414. * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
  1415. * be called safely on it.
  1416. *
  1417. * Return 0 in case of success, else an error code.
  1418. */
  1419. int gpiod_direction_input(struct gpio_desc *desc)
  1420. {
  1421. unsigned long flags;
  1422. struct gpio_chip *chip;
  1423. int status = -EINVAL;
  1424. int offset;
  1425. if (!desc || !desc->chip) {
  1426. pr_warn("%s: invalid GPIO\n", __func__);
  1427. return -EINVAL;
  1428. }
  1429. chip = desc->chip;
  1430. if (!chip->get || !chip->direction_input) {
  1431. gpiod_warn(desc,
  1432. "%s: missing get() or direction_input() operations\n",
  1433. __func__);
  1434. return -EIO;
  1435. }
  1436. spin_lock_irqsave(&gpio_lock, flags);
  1437. status = gpio_ensure_requested(desc);
  1438. if (status < 0)
  1439. goto fail;
  1440. /* now we know the gpio is valid and chip won't vanish */
  1441. spin_unlock_irqrestore(&gpio_lock, flags);
  1442. might_sleep_if(chip->can_sleep);
  1443. offset = gpio_chip_hwgpio(desc);
  1444. if (status) {
  1445. status = chip->request(chip, offset);
  1446. if (status < 0) {
  1447. gpiod_dbg(desc, "%s: chip request fail, %d\n",
  1448. __func__, status);
  1449. /* and it's not available to anyone else ...
  1450. * gpio_request() is the fully clean solution.
  1451. */
  1452. goto lose;
  1453. }
  1454. }
  1455. status = chip->direction_input(chip, offset);
  1456. if (status == 0)
  1457. clear_bit(FLAG_IS_OUT, &desc->flags);
  1458. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  1459. lose:
  1460. return status;
  1461. fail:
  1462. spin_unlock_irqrestore(&gpio_lock, flags);
  1463. if (status)
  1464. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  1465. return status;
  1466. }
  1467. EXPORT_SYMBOL_GPL(gpiod_direction_input);
  1468. /**
  1469. * gpiod_direction_output - set the GPIO direction to input
  1470. * @desc: GPIO to set to output
  1471. * @value: initial output value of the GPIO
  1472. *
  1473. * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
  1474. * be called safely on it. The initial value of the output must be specified.
  1475. *
  1476. * Return 0 in case of success, else an error code.
  1477. */
  1478. int gpiod_direction_output(struct gpio_desc *desc, int value)
  1479. {
  1480. unsigned long flags;
  1481. struct gpio_chip *chip;
  1482. int status = -EINVAL;
  1483. int offset;
  1484. if (!desc || !desc->chip) {
  1485. pr_warn("%s: invalid GPIO\n", __func__);
  1486. return -EINVAL;
  1487. }
  1488. /* GPIOs used for IRQs shall not be set as output */
  1489. if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
  1490. gpiod_err(desc,
  1491. "%s: tried to set a GPIO tied to an IRQ as output\n",
  1492. __func__);
  1493. return -EIO;
  1494. }
  1495. /* Open drain pin should not be driven to 1 */
  1496. if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1497. return gpiod_direction_input(desc);
  1498. /* Open source pin should not be driven to 0 */
  1499. if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1500. return gpiod_direction_input(desc);
  1501. chip = desc->chip;
  1502. if (!chip->set || !chip->direction_output) {
  1503. gpiod_warn(desc,
  1504. "%s: missing set() or direction_output() operations\n",
  1505. __func__);
  1506. return -EIO;
  1507. }
  1508. spin_lock_irqsave(&gpio_lock, flags);
  1509. status = gpio_ensure_requested(desc);
  1510. if (status < 0)
  1511. goto fail;
  1512. /* now we know the gpio is valid and chip won't vanish */
  1513. spin_unlock_irqrestore(&gpio_lock, flags);
  1514. might_sleep_if(chip->can_sleep);
  1515. offset = gpio_chip_hwgpio(desc);
  1516. if (status) {
  1517. status = chip->request(chip, offset);
  1518. if (status < 0) {
  1519. gpiod_dbg(desc, "%s: chip request fail, %d\n",
  1520. __func__, status);
  1521. /* and it's not available to anyone else ...
  1522. * gpio_request() is the fully clean solution.
  1523. */
  1524. goto lose;
  1525. }
  1526. }
  1527. status = chip->direction_output(chip, offset, value);
  1528. if (status == 0)
  1529. set_bit(FLAG_IS_OUT, &desc->flags);
  1530. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1531. trace_gpio_direction(desc_to_gpio(desc), 0, status);
  1532. lose:
  1533. return status;
  1534. fail:
  1535. spin_unlock_irqrestore(&gpio_lock, flags);
  1536. if (status)
  1537. gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
  1538. return status;
  1539. }
  1540. EXPORT_SYMBOL_GPL(gpiod_direction_output);
  1541. /**
  1542. * gpiod_set_debounce - sets @debounce time for a @gpio
  1543. * @gpio: the gpio to set debounce time
  1544. * @debounce: debounce time is microseconds
  1545. *
  1546. * returns -ENOTSUPP if the controller does not support setting
  1547. * debounce.
  1548. */
  1549. int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  1550. {
  1551. unsigned long flags;
  1552. struct gpio_chip *chip;
  1553. int status = -EINVAL;
  1554. int offset;
  1555. if (!desc || !desc->chip) {
  1556. pr_warn("%s: invalid GPIO\n", __func__);
  1557. return -EINVAL;
  1558. }
  1559. chip = desc->chip;
  1560. if (!chip->set || !chip->set_debounce) {
  1561. gpiod_dbg(desc,
  1562. "%s: missing set() or set_debounce() operations\n",
  1563. __func__);
  1564. return -ENOTSUPP;
  1565. }
  1566. spin_lock_irqsave(&gpio_lock, flags);
  1567. status = gpio_ensure_requested(desc);
  1568. if (status < 0)
  1569. goto fail;
  1570. /* now we know the gpio is valid and chip won't vanish */
  1571. spin_unlock_irqrestore(&gpio_lock, flags);
  1572. might_sleep_if(chip->can_sleep);
  1573. offset = gpio_chip_hwgpio(desc);
  1574. return chip->set_debounce(chip, offset, debounce);
  1575. fail:
  1576. spin_unlock_irqrestore(&gpio_lock, flags);
  1577. if (status)
  1578. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  1579. return status;
  1580. }
  1581. EXPORT_SYMBOL_GPL(gpiod_set_debounce);
  1582. /**
  1583. * gpiod_is_active_low - test whether a GPIO is active-low or not
  1584. * @desc: the gpio descriptor to test
  1585. *
  1586. * Returns 1 if the GPIO is active-low, 0 otherwise.
  1587. */
  1588. int gpiod_is_active_low(const struct gpio_desc *desc)
  1589. {
  1590. return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1591. }
  1592. EXPORT_SYMBOL_GPL(gpiod_is_active_low);
  1593. /* I/O calls are only valid after configuration completed; the relevant
  1594. * "is this a valid GPIO" error checks should already have been done.
  1595. *
  1596. * "Get" operations are often inlinable as reading a pin value register,
  1597. * and masking the relevant bit in that register.
  1598. *
  1599. * When "set" operations are inlinable, they involve writing that mask to
  1600. * one register to set a low value, or a different register to set it high.
  1601. * Otherwise locking is needed, so there may be little value to inlining.
  1602. *
  1603. *------------------------------------------------------------------------
  1604. *
  1605. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1606. * have requested the GPIO. That can include implicit requesting by
  1607. * a direction setting call. Marking a gpio as requested locks its chip
  1608. * in memory, guaranteeing that these table lookups need no more locking
  1609. * and that gpiochip_remove() will fail.
  1610. *
  1611. * REVISIT when debugging, consider adding some instrumentation to ensure
  1612. * that the GPIO was actually requested.
  1613. */
  1614. static int _gpiod_get_raw_value(const struct gpio_desc *desc)
  1615. {
  1616. struct gpio_chip *chip;
  1617. int value;
  1618. int offset;
  1619. chip = desc->chip;
  1620. offset = gpio_chip_hwgpio(desc);
  1621. value = chip->get ? chip->get(chip, offset) : 0;
  1622. trace_gpio_value(desc_to_gpio(desc), 1, value);
  1623. return value;
  1624. }
  1625. /**
  1626. * gpiod_get_raw_value() - return a gpio's raw value
  1627. * @desc: gpio whose value will be returned
  1628. *
  1629. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  1630. * its ACTIVE_LOW status.
  1631. *
  1632. * This function should be called from contexts where we cannot sleep, and will
  1633. * complain if the GPIO chip functions potentially sleep.
  1634. */
  1635. int gpiod_get_raw_value(const struct gpio_desc *desc)
  1636. {
  1637. if (!desc)
  1638. return 0;
  1639. /* Should be using gpio_get_value_cansleep() */
  1640. WARN_ON(desc->chip->can_sleep);
  1641. return _gpiod_get_raw_value(desc);
  1642. }
  1643. EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
  1644. /**
  1645. * gpiod_get_value() - return a gpio's value
  1646. * @desc: gpio whose value will be returned
  1647. *
  1648. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  1649. * account.
  1650. *
  1651. * This function should be called from contexts where we cannot sleep, and will
  1652. * complain if the GPIO chip functions potentially sleep.
  1653. */
  1654. int gpiod_get_value(const struct gpio_desc *desc)
  1655. {
  1656. int value;
  1657. if (!desc)
  1658. return 0;
  1659. /* Should be using gpio_get_value_cansleep() */
  1660. WARN_ON(desc->chip->can_sleep);
  1661. value = _gpiod_get_raw_value(desc);
  1662. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1663. value = !value;
  1664. return value;
  1665. }
  1666. EXPORT_SYMBOL_GPL(gpiod_get_value);
  1667. /*
  1668. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  1669. * @desc: gpio descriptor whose state need to be set.
  1670. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1671. */
  1672. static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
  1673. {
  1674. int err = 0;
  1675. struct gpio_chip *chip = desc->chip;
  1676. int offset = gpio_chip_hwgpio(desc);
  1677. if (value) {
  1678. err = chip->direction_input(chip, offset);
  1679. if (!err)
  1680. clear_bit(FLAG_IS_OUT, &desc->flags);
  1681. } else {
  1682. err = chip->direction_output(chip, offset, 0);
  1683. if (!err)
  1684. set_bit(FLAG_IS_OUT, &desc->flags);
  1685. }
  1686. trace_gpio_direction(desc_to_gpio(desc), value, err);
  1687. if (err < 0)
  1688. gpiod_err(desc,
  1689. "%s: Error in set_value for open drain err %d\n",
  1690. __func__, err);
  1691. }
  1692. /*
  1693. * _gpio_set_open_source_value() - Set the open source gpio's value.
  1694. * @desc: gpio descriptor whose state need to be set.
  1695. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1696. */
  1697. static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
  1698. {
  1699. int err = 0;
  1700. struct gpio_chip *chip = desc->chip;
  1701. int offset = gpio_chip_hwgpio(desc);
  1702. if (value) {
  1703. err = chip->direction_output(chip, offset, 1);
  1704. if (!err)
  1705. set_bit(FLAG_IS_OUT, &desc->flags);
  1706. } else {
  1707. err = chip->direction_input(chip, offset);
  1708. if (!err)
  1709. clear_bit(FLAG_IS_OUT, &desc->flags);
  1710. }
  1711. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  1712. if (err < 0)
  1713. gpiod_err(desc,
  1714. "%s: Error in set_value for open source err %d\n",
  1715. __func__, err);
  1716. }
  1717. static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
  1718. {
  1719. struct gpio_chip *chip;
  1720. chip = desc->chip;
  1721. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1722. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1723. _gpio_set_open_drain_value(desc, value);
  1724. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1725. _gpio_set_open_source_value(desc, value);
  1726. else
  1727. chip->set(chip, gpio_chip_hwgpio(desc), value);
  1728. }
  1729. /**
  1730. * gpiod_set_raw_value() - assign a gpio's raw value
  1731. * @desc: gpio whose value will be assigned
  1732. * @value: value to assign
  1733. *
  1734. * Set the raw value of the GPIO, i.e. the value of its physical line without
  1735. * regard for its ACTIVE_LOW status.
  1736. *
  1737. * This function should be called from contexts where we cannot sleep, and will
  1738. * complain if the GPIO chip functions potentially sleep.
  1739. */
  1740. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  1741. {
  1742. if (!desc)
  1743. return;
  1744. /* Should be using gpio_set_value_cansleep() */
  1745. WARN_ON(desc->chip->can_sleep);
  1746. _gpiod_set_raw_value(desc, value);
  1747. }
  1748. EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
  1749. /**
  1750. * gpiod_set_value() - assign a gpio's value
  1751. * @desc: gpio whose value will be assigned
  1752. * @value: value to assign
  1753. *
  1754. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1755. * account
  1756. *
  1757. * This function should be called from contexts where we cannot sleep, and will
  1758. * complain if the GPIO chip functions potentially sleep.
  1759. */
  1760. void gpiod_set_value(struct gpio_desc *desc, int value)
  1761. {
  1762. if (!desc)
  1763. return;
  1764. /* Should be using gpio_set_value_cansleep() */
  1765. WARN_ON(desc->chip->can_sleep);
  1766. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1767. value = !value;
  1768. _gpiod_set_raw_value(desc, value);
  1769. }
  1770. EXPORT_SYMBOL_GPL(gpiod_set_value);
  1771. /**
  1772. * gpiod_cansleep() - report whether gpio value access may sleep
  1773. * @desc: gpio to check
  1774. *
  1775. */
  1776. int gpiod_cansleep(const struct gpio_desc *desc)
  1777. {
  1778. if (!desc)
  1779. return 0;
  1780. return desc->chip->can_sleep;
  1781. }
  1782. EXPORT_SYMBOL_GPL(gpiod_cansleep);
  1783. /**
  1784. * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  1785. * @desc: gpio whose IRQ will be returned (already requested)
  1786. *
  1787. * Return the IRQ corresponding to the passed GPIO, or an error code in case of
  1788. * error.
  1789. */
  1790. int gpiod_to_irq(const struct gpio_desc *desc)
  1791. {
  1792. struct gpio_chip *chip;
  1793. int offset;
  1794. if (!desc)
  1795. return -EINVAL;
  1796. chip = desc->chip;
  1797. offset = gpio_chip_hwgpio(desc);
  1798. return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
  1799. }
  1800. EXPORT_SYMBOL_GPL(gpiod_to_irq);
  1801. /**
  1802. * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
  1803. * @gpio: the GPIO line to lock as used for IRQ
  1804. *
  1805. * This is used directly by GPIO drivers that want to lock down
  1806. * a certain GPIO line to be used as IRQs, for example in the
  1807. * .to_irq() callback of their gpio_chip, or in the .irq_enable()
  1808. * of its irq_chip implementation if the GPIO is known from that
  1809. * code.
  1810. */
  1811. int gpiod_lock_as_irq(struct gpio_desc *desc)
  1812. {
  1813. if (!desc)
  1814. return -EINVAL;
  1815. if (test_bit(FLAG_IS_OUT, &desc->flags)) {
  1816. gpiod_err(desc,
  1817. "%s: tried to flag a GPIO set as output for IRQ\n",
  1818. __func__);
  1819. return -EIO;
  1820. }
  1821. set_bit(FLAG_USED_AS_IRQ, &desc->flags);
  1822. return 0;
  1823. }
  1824. EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
  1825. int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
  1826. {
  1827. return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
  1828. }
  1829. EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
  1830. /**
  1831. * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
  1832. * @gpio: the GPIO line to unlock from IRQ usage
  1833. *
  1834. * This is used directly by GPIO drivers that want to indicate
  1835. * that a certain GPIO is no longer used exclusively for IRQ.
  1836. */
  1837. void gpiod_unlock_as_irq(struct gpio_desc *desc)
  1838. {
  1839. if (!desc)
  1840. return;
  1841. clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
  1842. }
  1843. EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
  1844. void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
  1845. {
  1846. return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
  1847. }
  1848. EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
  1849. /**
  1850. * gpiod_get_raw_value_cansleep() - return a gpio's raw value
  1851. * @desc: gpio whose value will be returned
  1852. *
  1853. * Return the GPIO's raw value, i.e. the value of the physical line disregarding
  1854. * its ACTIVE_LOW status.
  1855. *
  1856. * This function is to be called from contexts that can sleep.
  1857. */
  1858. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  1859. {
  1860. might_sleep_if(extra_checks);
  1861. if (!desc)
  1862. return 0;
  1863. return _gpiod_get_raw_value(desc);
  1864. }
  1865. EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
  1866. /**
  1867. * gpiod_get_value_cansleep() - return a gpio's value
  1868. * @desc: gpio whose value will be returned
  1869. *
  1870. * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
  1871. * account.
  1872. *
  1873. * This function is to be called from contexts that can sleep.
  1874. */
  1875. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  1876. {
  1877. int value;
  1878. might_sleep_if(extra_checks);
  1879. if (!desc)
  1880. return 0;
  1881. value = _gpiod_get_raw_value(desc);
  1882. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1883. value = !value;
  1884. return value;
  1885. }
  1886. EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
  1887. /**
  1888. * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
  1889. * @desc: gpio whose value will be assigned
  1890. * @value: value to assign
  1891. *
  1892. * Set the raw value of the GPIO, i.e. the value of its physical line without
  1893. * regard for its ACTIVE_LOW status.
  1894. *
  1895. * This function is to be called from contexts that can sleep.
  1896. */
  1897. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  1898. {
  1899. might_sleep_if(extra_checks);
  1900. if (!desc)
  1901. return;
  1902. _gpiod_set_raw_value(desc, value);
  1903. }
  1904. EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
  1905. /**
  1906. * gpiod_set_value_cansleep() - assign a gpio's value
  1907. * @desc: gpio whose value will be assigned
  1908. * @value: value to assign
  1909. *
  1910. * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
  1911. * account
  1912. *
  1913. * This function is to be called from contexts that can sleep.
  1914. */
  1915. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  1916. {
  1917. might_sleep_if(extra_checks);
  1918. if (!desc)
  1919. return;
  1920. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1921. value = !value;
  1922. _gpiod_set_raw_value(desc, value);
  1923. }
  1924. EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
  1925. /**
  1926. * gpiod_add_lookup_table() - register GPIO device consumers
  1927. * @table: table of consumers to register
  1928. */
  1929. void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
  1930. {
  1931. mutex_lock(&gpio_lookup_lock);
  1932. list_add_tail(&table->list, &gpio_lookup_list);
  1933. mutex_unlock(&gpio_lookup_lock);
  1934. }
  1935. #ifdef CONFIG_OF
  1936. static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  1937. unsigned int idx,
  1938. enum gpio_lookup_flags *flags)
  1939. {
  1940. char prop_name[32]; /* 32 is max size of property name */
  1941. enum of_gpio_flags of_flags;
  1942. struct gpio_desc *desc;
  1943. if (con_id)
  1944. snprintf(prop_name, 32, "%s-gpios", con_id);
  1945. else
  1946. snprintf(prop_name, 32, "gpios");
  1947. desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
  1948. &of_flags);
  1949. if (IS_ERR(desc))
  1950. return desc;
  1951. if (of_flags & OF_GPIO_ACTIVE_LOW)
  1952. *flags |= GPIO_ACTIVE_LOW;
  1953. return desc;
  1954. }
  1955. #else
  1956. static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  1957. unsigned int idx,
  1958. enum gpio_lookup_flags *flags)
  1959. {
  1960. return ERR_PTR(-ENODEV);
  1961. }
  1962. #endif
  1963. static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
  1964. unsigned int idx,
  1965. enum gpio_lookup_flags *flags)
  1966. {
  1967. struct acpi_gpio_info info;
  1968. struct gpio_desc *desc;
  1969. desc = acpi_get_gpiod_by_index(dev, idx, &info);
  1970. if (IS_ERR(desc))
  1971. return desc;
  1972. if (info.gpioint && info.active_low)
  1973. *flags |= GPIO_ACTIVE_LOW;
  1974. return desc;
  1975. }
  1976. static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
  1977. {
  1978. const char *dev_id = dev ? dev_name(dev) : NULL;
  1979. struct gpiod_lookup_table *table;
  1980. mutex_lock(&gpio_lookup_lock);
  1981. list_for_each_entry(table, &gpio_lookup_list, list) {
  1982. if (table->dev_id && dev_id) {
  1983. /*
  1984. * Valid strings on both ends, must be identical to have
  1985. * a match
  1986. */
  1987. if (!strcmp(table->dev_id, dev_id))
  1988. goto found;
  1989. } else {
  1990. /*
  1991. * One of the pointers is NULL, so both must be to have
  1992. * a match
  1993. */
  1994. if (dev_id == table->dev_id)
  1995. goto found;
  1996. }
  1997. }
  1998. table = NULL;
  1999. found:
  2000. mutex_unlock(&gpio_lookup_lock);
  2001. return table;
  2002. }
  2003. static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
  2004. unsigned int idx,
  2005. enum gpio_lookup_flags *flags)
  2006. {
  2007. struct gpio_desc *desc = ERR_PTR(-ENOENT);
  2008. struct gpiod_lookup_table *table;
  2009. struct gpiod_lookup *p;
  2010. table = gpiod_find_lookup_table(dev);
  2011. if (!table)
  2012. return desc;
  2013. for (p = &table->table[0]; p->chip_label; p++) {
  2014. struct gpio_chip *chip;
  2015. /* idx must always match exactly */
  2016. if (p->idx != idx)
  2017. continue;
  2018. /* If the lookup entry has a con_id, require exact match */
  2019. if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
  2020. continue;
  2021. chip = find_chip_by_name(p->chip_label);
  2022. if (!chip) {
  2023. dev_err(dev, "cannot find GPIO chip %s\n",
  2024. p->chip_label);
  2025. return ERR_PTR(-ENODEV);
  2026. }
  2027. if (chip->ngpio <= p->chip_hwnum) {
  2028. dev_err(dev,
  2029. "requested GPIO %d is out of range [0..%d] for chip %s\n",
  2030. idx, chip->ngpio, chip->label);
  2031. return ERR_PTR(-EINVAL);
  2032. }
  2033. desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
  2034. *flags = p->flags;
  2035. return desc;
  2036. }
  2037. return desc;
  2038. }
  2039. /**
  2040. * gpio_get - obtain a GPIO for a given GPIO function
  2041. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2042. * @con_id: function within the GPIO consumer
  2043. *
  2044. * Return the GPIO descriptor corresponding to the function con_id of device
  2045. * dev, -ENOENT if no GPIO has been assigned to the requested function, or
  2046. * another IS_ERR() code if an error occured while trying to acquire the GPIO.
  2047. */
  2048. struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
  2049. {
  2050. return gpiod_get_index(dev, con_id, 0);
  2051. }
  2052. EXPORT_SYMBOL_GPL(gpiod_get);
  2053. /**
  2054. * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
  2055. * @dev: GPIO consumer, can be NULL for system-global GPIOs
  2056. * @con_id: function within the GPIO consumer
  2057. * @idx: index of the GPIO to obtain in the consumer
  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. * occured 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. {
  2070. struct gpio_desc *desc = NULL;
  2071. int status;
  2072. enum gpio_lookup_flags flags = 0;
  2073. dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
  2074. /* Using device tree? */
  2075. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
  2076. dev_dbg(dev, "using device tree for GPIO lookup\n");
  2077. desc = of_find_gpio(dev, con_id, idx, &flags);
  2078. } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
  2079. dev_dbg(dev, "using ACPI for GPIO lookup\n");
  2080. desc = acpi_find_gpio(dev, con_id, idx, &flags);
  2081. }
  2082. /*
  2083. * Either we are not using DT or ACPI, or their lookup did not return
  2084. * a result. In that case, use platform lookup as a fallback.
  2085. */
  2086. if (!desc || desc == ERR_PTR(-ENOENT)) {
  2087. dev_dbg(dev, "using lookup tables for GPIO lookup");
  2088. desc = gpiod_find(dev, con_id, idx, &flags);
  2089. }
  2090. if (IS_ERR(desc)) {
  2091. dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
  2092. return desc;
  2093. }
  2094. status = gpiod_request(desc, con_id);
  2095. if (status < 0)
  2096. return ERR_PTR(status);
  2097. if (flags & GPIO_ACTIVE_LOW)
  2098. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  2099. if (flags & GPIO_OPEN_DRAIN)
  2100. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  2101. if (flags & GPIO_OPEN_SOURCE)
  2102. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  2103. return desc;
  2104. }
  2105. EXPORT_SYMBOL_GPL(gpiod_get_index);
  2106. /**
  2107. * gpiod_put - dispose of a GPIO descriptor
  2108. * @desc: GPIO descriptor to dispose of
  2109. *
  2110. * No descriptor can be used after gpiod_put() has been called on it.
  2111. */
  2112. void gpiod_put(struct gpio_desc *desc)
  2113. {
  2114. gpiod_free(desc);
  2115. }
  2116. EXPORT_SYMBOL_GPL(gpiod_put);
  2117. #ifdef CONFIG_DEBUG_FS
  2118. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  2119. {
  2120. unsigned i;
  2121. unsigned gpio = chip->base;
  2122. struct gpio_desc *gdesc = &chip->desc[0];
  2123. int is_out;
  2124. int is_irq;
  2125. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  2126. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  2127. continue;
  2128. gpiod_get_direction(gdesc);
  2129. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  2130. is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
  2131. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
  2132. gpio, gdesc->label,
  2133. is_out ? "out" : "in ",
  2134. chip->get
  2135. ? (chip->get(chip, i) ? "hi" : "lo")
  2136. : "? ",
  2137. is_irq ? "IRQ" : " ");
  2138. seq_printf(s, "\n");
  2139. }
  2140. }
  2141. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  2142. {
  2143. unsigned long flags;
  2144. struct gpio_chip *chip = NULL;
  2145. loff_t index = *pos;
  2146. s->private = "";
  2147. spin_lock_irqsave(&gpio_lock, flags);
  2148. list_for_each_entry(chip, &gpio_chips, list)
  2149. if (index-- == 0) {
  2150. spin_unlock_irqrestore(&gpio_lock, flags);
  2151. return chip;
  2152. }
  2153. spin_unlock_irqrestore(&gpio_lock, flags);
  2154. return NULL;
  2155. }
  2156. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2157. {
  2158. unsigned long flags;
  2159. struct gpio_chip *chip = v;
  2160. void *ret = NULL;
  2161. spin_lock_irqsave(&gpio_lock, flags);
  2162. if (list_is_last(&chip->list, &gpio_chips))
  2163. ret = NULL;
  2164. else
  2165. ret = list_entry(chip->list.next, struct gpio_chip, list);
  2166. spin_unlock_irqrestore(&gpio_lock, flags);
  2167. s->private = "\n";
  2168. ++*pos;
  2169. return ret;
  2170. }
  2171. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  2172. {
  2173. }
  2174. static int gpiolib_seq_show(struct seq_file *s, void *v)
  2175. {
  2176. struct gpio_chip *chip = v;
  2177. struct device *dev;
  2178. seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
  2179. chip->base, chip->base + chip->ngpio - 1);
  2180. dev = chip->dev;
  2181. if (dev)
  2182. seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
  2183. dev_name(dev));
  2184. if (chip->label)
  2185. seq_printf(s, ", %s", chip->label);
  2186. if (chip->can_sleep)
  2187. seq_printf(s, ", can sleep");
  2188. seq_printf(s, ":\n");
  2189. if (chip->dbg_show)
  2190. chip->dbg_show(s, chip);
  2191. else
  2192. gpiolib_dbg_show(s, chip);
  2193. return 0;
  2194. }
  2195. static const struct seq_operations gpiolib_seq_ops = {
  2196. .start = gpiolib_seq_start,
  2197. .next = gpiolib_seq_next,
  2198. .stop = gpiolib_seq_stop,
  2199. .show = gpiolib_seq_show,
  2200. };
  2201. static int gpiolib_open(struct inode *inode, struct file *file)
  2202. {
  2203. return seq_open(file, &gpiolib_seq_ops);
  2204. }
  2205. static const struct file_operations gpiolib_operations = {
  2206. .owner = THIS_MODULE,
  2207. .open = gpiolib_open,
  2208. .read = seq_read,
  2209. .llseek = seq_lseek,
  2210. .release = seq_release,
  2211. };
  2212. static int __init gpiolib_debugfs_init(void)
  2213. {
  2214. /* /sys/kernel/debug/gpio */
  2215. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  2216. NULL, NULL, &gpiolib_operations);
  2217. return 0;
  2218. }
  2219. subsys_initcall(gpiolib_debugfs_init);
  2220. #endif /* DEBUG_FS */