gpiolib.c 64 KB

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