gpiolib.c 63 KB

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