gpiolib.c 64 KB

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