gpiolib.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/irq.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/list.h>
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/gpio.h>
  12. #include <linux/of_gpio.h>
  13. #include <linux/idr.h>
  14. #include <linux/slab.h>
  15. #define CREATE_TRACE_POINTS
  16. #include <trace/events/gpio.h>
  17. /* Optional implementation infrastructure for GPIO interfaces.
  18. *
  19. * Platforms may want to use this if they tend to use very many GPIOs
  20. * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
  21. *
  22. * When kernel footprint or instruction count is an issue, simpler
  23. * implementations may be preferred. The GPIO programming interface
  24. * allows for inlining speed-critical get/set operations for common
  25. * cases, so that access to SOC-integrated GPIOs can sometimes cost
  26. * only an instruction or two per bit.
  27. */
  28. /* When debugging, extend minimal trust to callers and platform code.
  29. * Also emit diagnostic messages that may help initial bringup, when
  30. * board setup or driver bugs are most common.
  31. *
  32. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  33. */
  34. #ifdef DEBUG
  35. #define extra_checks 1
  36. #else
  37. #define extra_checks 0
  38. #endif
  39. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  40. * While any GPIO is requested, its gpio_chip is not removable;
  41. * each GPIO's "requested" flag serves as a lock and refcount.
  42. */
  43. static DEFINE_SPINLOCK(gpio_lock);
  44. struct gpio_desc {
  45. struct gpio_chip *chip;
  46. unsigned long flags;
  47. /* flag symbols are bit numbers */
  48. #define FLAG_REQUESTED 0
  49. #define FLAG_IS_OUT 1
  50. #define FLAG_EXPORT 2 /* protected by sysfs_lock */
  51. #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
  52. #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
  53. #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
  54. #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
  55. #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
  56. #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
  57. #define ID_SHIFT 16 /* add new flags before this one */
  58. #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
  59. #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
  60. #ifdef CONFIG_DEBUG_FS
  61. const char *label;
  62. #endif
  63. };
  64. static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
  65. #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
  66. static LIST_HEAD(gpio_chips);
  67. #ifdef CONFIG_GPIO_SYSFS
  68. static DEFINE_IDR(dirent_idr);
  69. #endif
  70. /*
  71. * Internal gpiod_* API using descriptors instead of the integer namespace.
  72. * Most of this should eventually go public.
  73. */
  74. static int gpiod_request(struct gpio_desc *desc, const char *label);
  75. static void gpiod_free(struct gpio_desc *desc);
  76. static int gpiod_direction_input(struct gpio_desc *desc);
  77. static int gpiod_direction_output(struct gpio_desc *desc, int value);
  78. static int gpiod_get_direction(const struct gpio_desc *desc);
  79. static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
  80. static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
  81. static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
  82. static int gpiod_get_value(const struct gpio_desc *desc);
  83. static void gpiod_set_value(struct gpio_desc *desc, int value);
  84. static int gpiod_cansleep(const struct gpio_desc *desc);
  85. static int gpiod_to_irq(const struct gpio_desc *desc);
  86. static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  87. static int gpiod_export_link(struct device *dev, const char *name,
  88. struct gpio_desc *desc);
  89. static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
  90. static void gpiod_unexport(struct gpio_desc *desc);
  91. static inline void desc_set_label(struct gpio_desc *d, const char *label)
  92. {
  93. #ifdef CONFIG_DEBUG_FS
  94. d->label = label;
  95. #endif
  96. }
  97. /*
  98. * Return the GPIO number of the passed descriptor relative to its chip
  99. */
  100. static int gpio_chip_hwgpio(const struct gpio_desc *desc)
  101. {
  102. return desc - &desc->chip->desc[0];
  103. }
  104. /**
  105. * Convert a GPIO number to its descriptor
  106. */
  107. static struct gpio_desc *gpio_to_desc(unsigned gpio)
  108. {
  109. if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
  110. return NULL;
  111. else
  112. return &gpio_desc[gpio];
  113. }
  114. /**
  115. * Convert a GPIO descriptor to the integer namespace.
  116. * This should disappear in the future but is needed since we still
  117. * use GPIO numbers for error messages and sysfs nodes
  118. */
  119. static int desc_to_gpio(const struct gpio_desc *desc)
  120. {
  121. return desc - &gpio_desc[0];
  122. }
  123. /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
  124. * when setting direction, and otherwise illegal. Until board setup code
  125. * and drivers use explicit requests everywhere (which won't happen when
  126. * those calls have no teeth) we can't avoid autorequesting. This nag
  127. * message should motivate switching to explicit requests... so should
  128. * the weaker cleanup after faults, compared to gpio_request().
  129. *
  130. * NOTE: the autorequest mechanism is going away; at this point it's
  131. * only "legal" in the sense that (old) code using it won't break yet,
  132. * but instead only triggers a WARN() stack dump.
  133. */
  134. static int gpio_ensure_requested(struct gpio_desc *desc)
  135. {
  136. const struct gpio_chip *chip = desc->chip;
  137. const int gpio = desc_to_gpio(desc);
  138. if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
  139. "autorequest GPIO-%d\n", gpio)) {
  140. if (!try_module_get(chip->owner)) {
  141. pr_err("GPIO-%d: module can't be gotten \n", gpio);
  142. clear_bit(FLAG_REQUESTED, &desc->flags);
  143. /* lose */
  144. return -EIO;
  145. }
  146. desc_set_label(desc, "[auto]");
  147. /* caller must chip->request() w/o spinlock */
  148. if (chip->request)
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
  154. {
  155. return desc ? desc->chip : NULL;
  156. }
  157. /* caller holds gpio_lock *OR* gpio is marked as requested */
  158. struct gpio_chip *gpio_to_chip(unsigned gpio)
  159. {
  160. return gpiod_to_chip(gpio_to_desc(gpio));
  161. }
  162. /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
  163. static int gpiochip_find_base(int ngpio)
  164. {
  165. struct gpio_chip *chip;
  166. int base = ARCH_NR_GPIOS - ngpio;
  167. list_for_each_entry_reverse(chip, &gpio_chips, list) {
  168. /* found a free space? */
  169. if (chip->base + chip->ngpio <= base)
  170. break;
  171. else
  172. /* nope, check the space right before the chip */
  173. base = chip->base - ngpio;
  174. }
  175. if (gpio_is_valid(base)) {
  176. pr_debug("%s: found new base at %d\n", __func__, base);
  177. return base;
  178. } else {
  179. pr_err("%s: cannot find free range\n", __func__);
  180. return -ENOSPC;
  181. }
  182. }
  183. /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
  184. static int gpiod_get_direction(const struct gpio_desc *desc)
  185. {
  186. struct gpio_chip *chip;
  187. unsigned offset;
  188. int status = -EINVAL;
  189. chip = gpiod_to_chip(desc);
  190. offset = gpio_chip_hwgpio(desc);
  191. if (!chip->get_direction)
  192. return status;
  193. status = chip->get_direction(chip, offset);
  194. if (status > 0) {
  195. /* GPIOF_DIR_IN, or other positive */
  196. status = 1;
  197. /* FLAG_IS_OUT is just a cache of the result of get_direction(),
  198. * so it does not affect constness per se */
  199. clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
  200. }
  201. if (status == 0) {
  202. /* GPIOF_DIR_OUT */
  203. set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
  204. }
  205. return status;
  206. }
  207. #ifdef CONFIG_GPIO_SYSFS
  208. /* lock protects against unexport_gpio() being called while
  209. * sysfs files are active.
  210. */
  211. static DEFINE_MUTEX(sysfs_lock);
  212. /*
  213. * /sys/class/gpio/gpioN... only for GPIOs that are exported
  214. * /direction
  215. * * MAY BE OMITTED if kernel won't allow direction changes
  216. * * is read/write as "in" or "out"
  217. * * may also be written as "high" or "low", initializing
  218. * output value as specified ("out" implies "low")
  219. * /value
  220. * * always readable, subject to hardware behavior
  221. * * may be writable, as zero/nonzero
  222. * /edge
  223. * * configures behavior of poll(2) on /value
  224. * * available only if pin can generate IRQs on input
  225. * * is read/write as "none", "falling", "rising", or "both"
  226. * /active_low
  227. * * configures polarity of /value
  228. * * is read/write as zero/nonzero
  229. * * also affects existing and subsequent "falling" and "rising"
  230. * /edge configuration
  231. */
  232. static ssize_t gpio_direction_show(struct device *dev,
  233. struct device_attribute *attr, char *buf)
  234. {
  235. const struct gpio_desc *desc = dev_get_drvdata(dev);
  236. ssize_t status;
  237. mutex_lock(&sysfs_lock);
  238. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  239. status = -EIO;
  240. } else {
  241. gpiod_get_direction(desc);
  242. status = sprintf(buf, "%s\n",
  243. test_bit(FLAG_IS_OUT, &desc->flags)
  244. ? "out" : "in");
  245. }
  246. mutex_unlock(&sysfs_lock);
  247. return status;
  248. }
  249. static ssize_t gpio_direction_store(struct device *dev,
  250. struct device_attribute *attr, const char *buf, size_t size)
  251. {
  252. struct gpio_desc *desc = dev_get_drvdata(dev);
  253. ssize_t status;
  254. mutex_lock(&sysfs_lock);
  255. if (!test_bit(FLAG_EXPORT, &desc->flags))
  256. status = -EIO;
  257. else if (sysfs_streq(buf, "high"))
  258. status = gpiod_direction_output(desc, 1);
  259. else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
  260. status = gpiod_direction_output(desc, 0);
  261. else if (sysfs_streq(buf, "in"))
  262. status = gpiod_direction_input(desc);
  263. else
  264. status = -EINVAL;
  265. mutex_unlock(&sysfs_lock);
  266. return status ? : size;
  267. }
  268. static /* const */ DEVICE_ATTR(direction, 0644,
  269. gpio_direction_show, gpio_direction_store);
  270. static ssize_t gpio_value_show(struct device *dev,
  271. struct device_attribute *attr, char *buf)
  272. {
  273. struct gpio_desc *desc = dev_get_drvdata(dev);
  274. ssize_t status;
  275. mutex_lock(&sysfs_lock);
  276. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  277. status = -EIO;
  278. } else {
  279. int value;
  280. value = !!gpiod_get_value_cansleep(desc);
  281. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  282. value = !value;
  283. status = sprintf(buf, "%d\n", value);
  284. }
  285. mutex_unlock(&sysfs_lock);
  286. return status;
  287. }
  288. static ssize_t gpio_value_store(struct device *dev,
  289. struct device_attribute *attr, const char *buf, size_t size)
  290. {
  291. struct gpio_desc *desc = dev_get_drvdata(dev);
  292. ssize_t status;
  293. mutex_lock(&sysfs_lock);
  294. if (!test_bit(FLAG_EXPORT, &desc->flags))
  295. status = -EIO;
  296. else if (!test_bit(FLAG_IS_OUT, &desc->flags))
  297. status = -EPERM;
  298. else {
  299. long value;
  300. status = kstrtol(buf, 0, &value);
  301. if (status == 0) {
  302. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  303. value = !value;
  304. gpiod_set_value_cansleep(desc, value != 0);
  305. status = size;
  306. }
  307. }
  308. mutex_unlock(&sysfs_lock);
  309. return status;
  310. }
  311. static const DEVICE_ATTR(value, 0644,
  312. gpio_value_show, gpio_value_store);
  313. static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
  314. {
  315. struct sysfs_dirent *value_sd = priv;
  316. sysfs_notify_dirent(value_sd);
  317. return IRQ_HANDLED;
  318. }
  319. static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
  320. unsigned long gpio_flags)
  321. {
  322. struct sysfs_dirent *value_sd;
  323. unsigned long irq_flags;
  324. int ret, irq, id;
  325. if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
  326. return 0;
  327. irq = gpiod_to_irq(desc);
  328. if (irq < 0)
  329. return -EIO;
  330. id = desc->flags >> ID_SHIFT;
  331. value_sd = idr_find(&dirent_idr, id);
  332. if (value_sd)
  333. free_irq(irq, value_sd);
  334. desc->flags &= ~GPIO_TRIGGER_MASK;
  335. if (!gpio_flags) {
  336. ret = 0;
  337. goto free_id;
  338. }
  339. irq_flags = IRQF_SHARED;
  340. if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
  341. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  342. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  343. if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
  344. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  345. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  346. if (!value_sd) {
  347. value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
  348. if (!value_sd) {
  349. ret = -ENODEV;
  350. goto err_out;
  351. }
  352. ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
  353. if (ret < 0)
  354. goto free_sd;
  355. id = ret;
  356. desc->flags &= GPIO_FLAGS_MASK;
  357. desc->flags |= (unsigned long)id << ID_SHIFT;
  358. if (desc->flags >> ID_SHIFT != id) {
  359. ret = -ERANGE;
  360. goto free_id;
  361. }
  362. }
  363. ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
  364. "gpiolib", value_sd);
  365. if (ret < 0)
  366. goto free_id;
  367. desc->flags |= gpio_flags;
  368. return 0;
  369. free_id:
  370. idr_remove(&dirent_idr, id);
  371. desc->flags &= GPIO_FLAGS_MASK;
  372. free_sd:
  373. if (value_sd)
  374. sysfs_put(value_sd);
  375. err_out:
  376. return ret;
  377. }
  378. static const struct {
  379. const char *name;
  380. unsigned long flags;
  381. } trigger_types[] = {
  382. { "none", 0 },
  383. { "falling", BIT(FLAG_TRIG_FALL) },
  384. { "rising", BIT(FLAG_TRIG_RISE) },
  385. { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
  386. };
  387. static ssize_t gpio_edge_show(struct device *dev,
  388. struct device_attribute *attr, char *buf)
  389. {
  390. const struct gpio_desc *desc = dev_get_drvdata(dev);
  391. ssize_t status;
  392. mutex_lock(&sysfs_lock);
  393. if (!test_bit(FLAG_EXPORT, &desc->flags))
  394. status = -EIO;
  395. else {
  396. int i;
  397. status = 0;
  398. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  399. if ((desc->flags & GPIO_TRIGGER_MASK)
  400. == trigger_types[i].flags) {
  401. status = sprintf(buf, "%s\n",
  402. trigger_types[i].name);
  403. break;
  404. }
  405. }
  406. mutex_unlock(&sysfs_lock);
  407. return status;
  408. }
  409. static ssize_t gpio_edge_store(struct device *dev,
  410. struct device_attribute *attr, const char *buf, size_t size)
  411. {
  412. struct gpio_desc *desc = dev_get_drvdata(dev);
  413. ssize_t status;
  414. int i;
  415. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  416. if (sysfs_streq(trigger_types[i].name, buf))
  417. goto found;
  418. return -EINVAL;
  419. found:
  420. mutex_lock(&sysfs_lock);
  421. if (!test_bit(FLAG_EXPORT, &desc->flags))
  422. status = -EIO;
  423. else {
  424. status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
  425. if (!status)
  426. status = size;
  427. }
  428. mutex_unlock(&sysfs_lock);
  429. return status;
  430. }
  431. static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
  432. static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
  433. int value)
  434. {
  435. int status = 0;
  436. if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
  437. return 0;
  438. if (value)
  439. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  440. else
  441. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  442. /* reconfigure poll(2) support if enabled on one edge only */
  443. if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
  444. !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
  445. unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
  446. gpio_setup_irq(desc, dev, 0);
  447. status = gpio_setup_irq(desc, dev, trigger_flags);
  448. }
  449. return status;
  450. }
  451. static ssize_t gpio_active_low_show(struct device *dev,
  452. struct device_attribute *attr, char *buf)
  453. {
  454. const struct gpio_desc *desc = dev_get_drvdata(dev);
  455. ssize_t status;
  456. mutex_lock(&sysfs_lock);
  457. if (!test_bit(FLAG_EXPORT, &desc->flags))
  458. status = -EIO;
  459. else
  460. status = sprintf(buf, "%d\n",
  461. !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
  462. mutex_unlock(&sysfs_lock);
  463. return status;
  464. }
  465. static ssize_t gpio_active_low_store(struct device *dev,
  466. struct device_attribute *attr, const char *buf, size_t size)
  467. {
  468. struct gpio_desc *desc = dev_get_drvdata(dev);
  469. ssize_t status;
  470. mutex_lock(&sysfs_lock);
  471. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  472. status = -EIO;
  473. } else {
  474. long value;
  475. status = kstrtol(buf, 0, &value);
  476. if (status == 0)
  477. status = sysfs_set_active_low(desc, dev, value != 0);
  478. }
  479. mutex_unlock(&sysfs_lock);
  480. return status ? : size;
  481. }
  482. static const DEVICE_ATTR(active_low, 0644,
  483. gpio_active_low_show, gpio_active_low_store);
  484. static const struct attribute *gpio_attrs[] = {
  485. &dev_attr_value.attr,
  486. &dev_attr_active_low.attr,
  487. NULL,
  488. };
  489. static const struct attribute_group gpio_attr_group = {
  490. .attrs = (struct attribute **) gpio_attrs,
  491. };
  492. /*
  493. * /sys/class/gpio/gpiochipN/
  494. * /base ... matching gpio_chip.base (N)
  495. * /label ... matching gpio_chip.label
  496. * /ngpio ... matching gpio_chip.ngpio
  497. */
  498. static ssize_t chip_base_show(struct device *dev,
  499. struct device_attribute *attr, char *buf)
  500. {
  501. const struct gpio_chip *chip = dev_get_drvdata(dev);
  502. return sprintf(buf, "%d\n", chip->base);
  503. }
  504. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  505. static ssize_t chip_label_show(struct device *dev,
  506. struct device_attribute *attr, char *buf)
  507. {
  508. const struct gpio_chip *chip = dev_get_drvdata(dev);
  509. return sprintf(buf, "%s\n", chip->label ? : "");
  510. }
  511. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  512. static ssize_t chip_ngpio_show(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. const struct gpio_chip *chip = dev_get_drvdata(dev);
  516. return sprintf(buf, "%u\n", chip->ngpio);
  517. }
  518. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  519. static const struct attribute *gpiochip_attrs[] = {
  520. &dev_attr_base.attr,
  521. &dev_attr_label.attr,
  522. &dev_attr_ngpio.attr,
  523. NULL,
  524. };
  525. static const struct attribute_group gpiochip_attr_group = {
  526. .attrs = (struct attribute **) gpiochip_attrs,
  527. };
  528. /*
  529. * /sys/class/gpio/export ... write-only
  530. * integer N ... number of GPIO to export (full access)
  531. * /sys/class/gpio/unexport ... write-only
  532. * integer N ... number of GPIO to unexport
  533. */
  534. static ssize_t export_store(struct class *class,
  535. struct class_attribute *attr,
  536. const char *buf, size_t len)
  537. {
  538. long gpio;
  539. struct gpio_desc *desc;
  540. int status;
  541. status = kstrtol(buf, 0, &gpio);
  542. if (status < 0)
  543. goto done;
  544. desc = gpio_to_desc(gpio);
  545. /* reject invalid GPIOs */
  546. if (!desc) {
  547. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  548. return -EINVAL;
  549. }
  550. /* No extra locking here; FLAG_SYSFS just signifies that the
  551. * request and export were done by on behalf of userspace, so
  552. * they may be undone on its behalf too.
  553. */
  554. status = gpiod_request(desc, "sysfs");
  555. if (status < 0) {
  556. if (status == -EPROBE_DEFER)
  557. status = -ENODEV;
  558. goto done;
  559. }
  560. status = gpiod_export(desc, true);
  561. if (status < 0)
  562. gpiod_free(desc);
  563. else
  564. set_bit(FLAG_SYSFS, &desc->flags);
  565. done:
  566. if (status)
  567. pr_debug("%s: status %d\n", __func__, status);
  568. return status ? : len;
  569. }
  570. static ssize_t unexport_store(struct class *class,
  571. struct class_attribute *attr,
  572. const char *buf, size_t len)
  573. {
  574. long gpio;
  575. struct gpio_desc *desc;
  576. int status;
  577. status = kstrtol(buf, 0, &gpio);
  578. if (status < 0)
  579. goto done;
  580. desc = gpio_to_desc(gpio);
  581. /* reject bogus commands (gpio_unexport ignores them) */
  582. if (!desc) {
  583. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  584. return -EINVAL;
  585. }
  586. status = -EINVAL;
  587. /* No extra locking here; FLAG_SYSFS just signifies that the
  588. * request and export were done by on behalf of userspace, so
  589. * they may be undone on its behalf too.
  590. */
  591. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
  592. status = 0;
  593. gpiod_free(desc);
  594. }
  595. done:
  596. if (status)
  597. pr_debug("%s: status %d\n", __func__, status);
  598. return status ? : len;
  599. }
  600. static struct class_attribute gpio_class_attrs[] = {
  601. __ATTR(export, 0200, NULL, export_store),
  602. __ATTR(unexport, 0200, NULL, unexport_store),
  603. __ATTR_NULL,
  604. };
  605. static struct class gpio_class = {
  606. .name = "gpio",
  607. .owner = THIS_MODULE,
  608. .class_attrs = gpio_class_attrs,
  609. };
  610. /**
  611. * gpio_export - export a GPIO through sysfs
  612. * @gpio: gpio to make available, already requested
  613. * @direction_may_change: true if userspace may change gpio direction
  614. * Context: arch_initcall or later
  615. *
  616. * When drivers want to make a GPIO accessible to userspace after they
  617. * have requested it -- perhaps while debugging, or as part of their
  618. * public interface -- they may use this routine. If the GPIO can
  619. * change direction (some can't) and the caller allows it, userspace
  620. * will see "direction" sysfs attribute which may be used to change
  621. * the gpio's direction. A "value" attribute will always be provided.
  622. *
  623. * Returns zero on success, else an error.
  624. */
  625. static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  626. {
  627. unsigned long flags;
  628. int status;
  629. const char *ioname = NULL;
  630. struct device *dev;
  631. int offset;
  632. /* can't export until sysfs is available ... */
  633. if (!gpio_class.p) {
  634. pr_debug("%s: called too early!\n", __func__);
  635. return -ENOENT;
  636. }
  637. if (!desc) {
  638. pr_debug("%s: invalid gpio descriptor\n", __func__);
  639. return -EINVAL;
  640. }
  641. mutex_lock(&sysfs_lock);
  642. spin_lock_irqsave(&gpio_lock, flags);
  643. if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
  644. test_bit(FLAG_EXPORT, &desc->flags)) {
  645. spin_unlock_irqrestore(&gpio_lock, flags);
  646. pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
  647. __func__, desc_to_gpio(desc),
  648. test_bit(FLAG_REQUESTED, &desc->flags),
  649. test_bit(FLAG_EXPORT, &desc->flags));
  650. status = -EPERM;
  651. goto fail_unlock;
  652. }
  653. if (!desc->chip->direction_input || !desc->chip->direction_output)
  654. direction_may_change = false;
  655. spin_unlock_irqrestore(&gpio_lock, flags);
  656. offset = gpio_chip_hwgpio(desc);
  657. if (desc->chip->names && desc->chip->names[offset])
  658. ioname = desc->chip->names[offset];
  659. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  660. desc, ioname ? ioname : "gpio%u",
  661. desc_to_gpio(desc));
  662. if (IS_ERR(dev)) {
  663. status = PTR_ERR(dev);
  664. goto fail_unlock;
  665. }
  666. status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
  667. if (status)
  668. goto fail_unregister_device;
  669. if (direction_may_change) {
  670. status = device_create_file(dev, &dev_attr_direction);
  671. if (status)
  672. goto fail_unregister_device;
  673. }
  674. if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
  675. !test_bit(FLAG_IS_OUT, &desc->flags))) {
  676. status = device_create_file(dev, &dev_attr_edge);
  677. if (status)
  678. goto fail_unregister_device;
  679. }
  680. set_bit(FLAG_EXPORT, &desc->flags);
  681. mutex_unlock(&sysfs_lock);
  682. return 0;
  683. fail_unregister_device:
  684. device_unregister(dev);
  685. fail_unlock:
  686. mutex_unlock(&sysfs_lock);
  687. pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
  688. status);
  689. return status;
  690. }
  691. int gpio_export(unsigned gpio, bool direction_may_change)
  692. {
  693. return gpiod_export(gpio_to_desc(gpio), direction_may_change);
  694. }
  695. EXPORT_SYMBOL_GPL(gpio_export);
  696. static int match_export(struct device *dev, const void *data)
  697. {
  698. return dev_get_drvdata(dev) == data;
  699. }
  700. /**
  701. * gpio_export_link - create a sysfs link to an exported GPIO node
  702. * @dev: device under which to create symlink
  703. * @name: name of the symlink
  704. * @gpio: gpio to create symlink to, already exported
  705. *
  706. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  707. * node. Caller is responsible for unlinking.
  708. *
  709. * Returns zero on success, else an error.
  710. */
  711. static int gpiod_export_link(struct device *dev, const char *name,
  712. struct gpio_desc *desc)
  713. {
  714. int status = -EINVAL;
  715. if (!desc) {
  716. pr_warn("%s: invalid GPIO\n", __func__);
  717. return -EINVAL;
  718. }
  719. mutex_lock(&sysfs_lock);
  720. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  721. struct device *tdev;
  722. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  723. if (tdev != NULL) {
  724. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  725. name);
  726. } else {
  727. status = -ENODEV;
  728. }
  729. }
  730. mutex_unlock(&sysfs_lock);
  731. if (status)
  732. pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
  733. status);
  734. return status;
  735. }
  736. int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
  737. {
  738. return gpiod_export_link(dev, name, gpio_to_desc(gpio));
  739. }
  740. EXPORT_SYMBOL_GPL(gpio_export_link);
  741. /**
  742. * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
  743. * @gpio: gpio to change
  744. * @value: non-zero to use active low, i.e. inverted values
  745. *
  746. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  747. * The GPIO does not have to be exported yet. If poll(2) support has
  748. * been enabled for either rising or falling edge, it will be
  749. * reconfigured to follow the new polarity.
  750. *
  751. * Returns zero on success, else an error.
  752. */
  753. static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  754. {
  755. struct device *dev = NULL;
  756. int status = -EINVAL;
  757. if (!desc) {
  758. pr_warn("%s: invalid GPIO\n", __func__);
  759. return -EINVAL;
  760. }
  761. mutex_lock(&sysfs_lock);
  762. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  763. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  764. if (dev == NULL) {
  765. status = -ENODEV;
  766. goto unlock;
  767. }
  768. }
  769. status = sysfs_set_active_low(desc, dev, value);
  770. unlock:
  771. mutex_unlock(&sysfs_lock);
  772. if (status)
  773. pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
  774. status);
  775. return status;
  776. }
  777. int gpio_sysfs_set_active_low(unsigned gpio, int value)
  778. {
  779. return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
  780. }
  781. EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
  782. /**
  783. * gpio_unexport - reverse effect of gpio_export()
  784. * @gpio: gpio to make unavailable
  785. *
  786. * This is implicit on gpio_free().
  787. */
  788. static void gpiod_unexport(struct gpio_desc *desc)
  789. {
  790. int status = 0;
  791. struct device *dev = NULL;
  792. if (!desc) {
  793. pr_warn("%s: invalid GPIO\n", __func__);
  794. return;
  795. }
  796. mutex_lock(&sysfs_lock);
  797. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  798. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  799. if (dev) {
  800. gpio_setup_irq(desc, dev, 0);
  801. clear_bit(FLAG_EXPORT, &desc->flags);
  802. } else
  803. status = -ENODEV;
  804. }
  805. mutex_unlock(&sysfs_lock);
  806. if (dev) {
  807. device_unregister(dev);
  808. put_device(dev);
  809. }
  810. if (status)
  811. pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
  812. status);
  813. }
  814. void gpio_unexport(unsigned gpio)
  815. {
  816. gpiod_unexport(gpio_to_desc(gpio));
  817. }
  818. EXPORT_SYMBOL_GPL(gpio_unexport);
  819. static int gpiochip_export(struct gpio_chip *chip)
  820. {
  821. int status;
  822. struct device *dev;
  823. /* Many systems register gpio chips for SOC support very early,
  824. * before driver model support is available. In those cases we
  825. * export this later, in gpiolib_sysfs_init() ... here we just
  826. * verify that _some_ field of gpio_class got initialized.
  827. */
  828. if (!gpio_class.p)
  829. return 0;
  830. /* use chip->base for the ID; it's already known to be unique */
  831. mutex_lock(&sysfs_lock);
  832. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  833. "gpiochip%d", chip->base);
  834. if (!IS_ERR(dev)) {
  835. status = sysfs_create_group(&dev->kobj,
  836. &gpiochip_attr_group);
  837. } else
  838. status = PTR_ERR(dev);
  839. chip->exported = (status == 0);
  840. mutex_unlock(&sysfs_lock);
  841. if (status) {
  842. unsigned long flags;
  843. unsigned gpio;
  844. spin_lock_irqsave(&gpio_lock, flags);
  845. gpio = 0;
  846. while (gpio < chip->ngpio)
  847. chip->desc[gpio++].chip = NULL;
  848. spin_unlock_irqrestore(&gpio_lock, flags);
  849. pr_debug("%s: chip %s status %d\n", __func__,
  850. chip->label, status);
  851. }
  852. return status;
  853. }
  854. static void gpiochip_unexport(struct gpio_chip *chip)
  855. {
  856. int status;
  857. struct device *dev;
  858. mutex_lock(&sysfs_lock);
  859. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  860. if (dev) {
  861. put_device(dev);
  862. device_unregister(dev);
  863. chip->exported = 0;
  864. status = 0;
  865. } else
  866. status = -ENODEV;
  867. mutex_unlock(&sysfs_lock);
  868. if (status)
  869. pr_debug("%s: chip %s status %d\n", __func__,
  870. chip->label, status);
  871. }
  872. static int __init gpiolib_sysfs_init(void)
  873. {
  874. int status;
  875. unsigned long flags;
  876. struct gpio_chip *chip;
  877. status = class_register(&gpio_class);
  878. if (status < 0)
  879. return status;
  880. /* Scan and register the gpio_chips which registered very
  881. * early (e.g. before the class_register above was called).
  882. *
  883. * We run before arch_initcall() so chip->dev nodes can have
  884. * registered, and so arch_initcall() can always gpio_export().
  885. */
  886. spin_lock_irqsave(&gpio_lock, flags);
  887. list_for_each_entry(chip, &gpio_chips, list) {
  888. if (!chip || chip->exported)
  889. continue;
  890. spin_unlock_irqrestore(&gpio_lock, flags);
  891. status = gpiochip_export(chip);
  892. spin_lock_irqsave(&gpio_lock, flags);
  893. }
  894. spin_unlock_irqrestore(&gpio_lock, flags);
  895. return status;
  896. }
  897. postcore_initcall(gpiolib_sysfs_init);
  898. #else
  899. static inline int gpiochip_export(struct gpio_chip *chip)
  900. {
  901. return 0;
  902. }
  903. static inline void gpiochip_unexport(struct gpio_chip *chip)
  904. {
  905. }
  906. static inline int gpiod_export(struct gpio_desc *desc,
  907. bool direction_may_change)
  908. {
  909. return -ENOSYS;
  910. }
  911. static inline int gpiod_export_link(struct device *dev, const char *name,
  912. struct gpio_desc *desc)
  913. {
  914. return -ENOSYS;
  915. }
  916. static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  917. {
  918. return -ENOSYS;
  919. }
  920. static inline void gpiod_unexport(struct gpio_desc *desc)
  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. #ifdef CONFIG_PINCTRL
  1095. /**
  1096. * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
  1097. * @chip: the gpiochip to add the range for
  1098. * @pinctrl_name: the dev_name() of the pin controller to map to
  1099. * @gpio_offset: the start offset in the current gpio_chip number space
  1100. * @pin_offset: the start offset in the pin controller number space
  1101. * @npins: the number of pins from the offset of each pin space (GPIO and
  1102. * pin controller) to accumulate in this range
  1103. */
  1104. int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
  1105. unsigned int gpio_offset, unsigned int pin_offset,
  1106. unsigned int npins)
  1107. {
  1108. struct gpio_pin_range *pin_range;
  1109. int ret;
  1110. pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
  1111. if (!pin_range) {
  1112. pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
  1113. chip->label);
  1114. return -ENOMEM;
  1115. }
  1116. /* Use local offset as range ID */
  1117. pin_range->range.id = gpio_offset;
  1118. pin_range->range.gc = chip;
  1119. pin_range->range.name = chip->label;
  1120. pin_range->range.base = chip->base + gpio_offset;
  1121. pin_range->range.pin_base = pin_offset;
  1122. pin_range->range.npins = npins;
  1123. pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
  1124. &pin_range->range);
  1125. if (IS_ERR(pin_range->pctldev)) {
  1126. ret = PTR_ERR(pin_range->pctldev);
  1127. pr_err("%s: GPIO chip: could not create pin range\n",
  1128. chip->label);
  1129. kfree(pin_range);
  1130. return ret;
  1131. }
  1132. pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
  1133. chip->label, gpio_offset, gpio_offset + npins - 1,
  1134. pinctl_name,
  1135. pin_offset, pin_offset + npins - 1);
  1136. list_add_tail(&pin_range->node, &chip->pin_ranges);
  1137. return 0;
  1138. }
  1139. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  1140. /**
  1141. * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
  1142. * @chip: the chip to remove all the mappings for
  1143. */
  1144. void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
  1145. {
  1146. struct gpio_pin_range *pin_range, *tmp;
  1147. list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
  1148. list_del(&pin_range->node);
  1149. pinctrl_remove_gpio_range(pin_range->pctldev,
  1150. &pin_range->range);
  1151. kfree(pin_range);
  1152. }
  1153. }
  1154. EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
  1155. #endif /* CONFIG_PINCTRL */
  1156. /* These "optional" allocation calls help prevent drivers from stomping
  1157. * on each other, and help provide better diagnostics in debugfs.
  1158. * They're called even less than the "set direction" calls.
  1159. */
  1160. static int gpiod_request(struct gpio_desc *desc, const char *label)
  1161. {
  1162. struct gpio_chip *chip;
  1163. int status = -EPROBE_DEFER;
  1164. unsigned long flags;
  1165. if (!desc) {
  1166. pr_warn("%s: invalid GPIO\n", __func__);
  1167. return -EINVAL;
  1168. }
  1169. spin_lock_irqsave(&gpio_lock, flags);
  1170. chip = desc->chip;
  1171. if (chip == NULL)
  1172. goto done;
  1173. if (!try_module_get(chip->owner))
  1174. goto done;
  1175. /* NOTE: gpio_request() can be called in early boot,
  1176. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1177. */
  1178. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1179. desc_set_label(desc, label ? : "?");
  1180. status = 0;
  1181. } else {
  1182. status = -EBUSY;
  1183. module_put(chip->owner);
  1184. goto done;
  1185. }
  1186. if (chip->request) {
  1187. /* chip->request may sleep */
  1188. spin_unlock_irqrestore(&gpio_lock, flags);
  1189. status = chip->request(chip, gpio_chip_hwgpio(desc));
  1190. spin_lock_irqsave(&gpio_lock, flags);
  1191. if (status < 0) {
  1192. desc_set_label(desc, NULL);
  1193. module_put(chip->owner);
  1194. clear_bit(FLAG_REQUESTED, &desc->flags);
  1195. goto done;
  1196. }
  1197. }
  1198. if (chip->get_direction) {
  1199. /* chip->get_direction may sleep */
  1200. spin_unlock_irqrestore(&gpio_lock, flags);
  1201. gpiod_get_direction(desc);
  1202. spin_lock_irqsave(&gpio_lock, flags);
  1203. }
  1204. done:
  1205. if (status)
  1206. pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
  1207. desc_to_gpio(desc), label ? : "?", status);
  1208. spin_unlock_irqrestore(&gpio_lock, flags);
  1209. return status;
  1210. }
  1211. int gpio_request(unsigned gpio, const char *label)
  1212. {
  1213. return gpiod_request(gpio_to_desc(gpio), label);
  1214. }
  1215. EXPORT_SYMBOL_GPL(gpio_request);
  1216. static void gpiod_free(struct gpio_desc *desc)
  1217. {
  1218. unsigned long flags;
  1219. struct gpio_chip *chip;
  1220. might_sleep();
  1221. if (!desc) {
  1222. WARN_ON(extra_checks);
  1223. return;
  1224. }
  1225. gpiod_unexport(desc);
  1226. spin_lock_irqsave(&gpio_lock, flags);
  1227. chip = desc->chip;
  1228. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1229. if (chip->free) {
  1230. spin_unlock_irqrestore(&gpio_lock, flags);
  1231. might_sleep_if(chip->can_sleep);
  1232. chip->free(chip, gpio_chip_hwgpio(desc));
  1233. spin_lock_irqsave(&gpio_lock, flags);
  1234. }
  1235. desc_set_label(desc, NULL);
  1236. module_put(desc->chip->owner);
  1237. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1238. clear_bit(FLAG_REQUESTED, &desc->flags);
  1239. clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1240. clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1241. } else
  1242. WARN_ON(extra_checks);
  1243. spin_unlock_irqrestore(&gpio_lock, flags);
  1244. }
  1245. void gpio_free(unsigned gpio)
  1246. {
  1247. gpiod_free(gpio_to_desc(gpio));
  1248. }
  1249. EXPORT_SYMBOL_GPL(gpio_free);
  1250. /**
  1251. * gpio_request_one - request a single GPIO with initial configuration
  1252. * @gpio: the GPIO number
  1253. * @flags: GPIO configuration as specified by GPIOF_*
  1254. * @label: a literal description string of this GPIO
  1255. */
  1256. int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  1257. {
  1258. struct gpio_desc *desc;
  1259. int err;
  1260. desc = gpio_to_desc(gpio);
  1261. err = gpiod_request(desc, label);
  1262. if (err)
  1263. return err;
  1264. if (flags & GPIOF_OPEN_DRAIN)
  1265. set_bit(FLAG_OPEN_DRAIN, &desc->flags);
  1266. if (flags & GPIOF_OPEN_SOURCE)
  1267. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  1268. if (flags & GPIOF_DIR_IN)
  1269. err = gpiod_direction_input(desc);
  1270. else
  1271. err = gpiod_direction_output(desc,
  1272. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  1273. if (err)
  1274. goto free_gpio;
  1275. if (flags & GPIOF_EXPORT) {
  1276. err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
  1277. if (err)
  1278. goto free_gpio;
  1279. }
  1280. return 0;
  1281. free_gpio:
  1282. gpiod_free(desc);
  1283. return err;
  1284. }
  1285. EXPORT_SYMBOL_GPL(gpio_request_one);
  1286. /**
  1287. * gpio_request_array - request multiple GPIOs in a single call
  1288. * @array: array of the 'struct gpio'
  1289. * @num: how many GPIOs in the array
  1290. */
  1291. int gpio_request_array(const struct gpio *array, size_t num)
  1292. {
  1293. int i, err;
  1294. for (i = 0; i < num; i++, array++) {
  1295. err = gpio_request_one(array->gpio, array->flags, array->label);
  1296. if (err)
  1297. goto err_free;
  1298. }
  1299. return 0;
  1300. err_free:
  1301. while (i--)
  1302. gpio_free((--array)->gpio);
  1303. return err;
  1304. }
  1305. EXPORT_SYMBOL_GPL(gpio_request_array);
  1306. /**
  1307. * gpio_free_array - release multiple GPIOs in a single call
  1308. * @array: array of the 'struct gpio'
  1309. * @num: how many GPIOs in the array
  1310. */
  1311. void gpio_free_array(const struct gpio *array, size_t num)
  1312. {
  1313. while (num--)
  1314. gpio_free((array++)->gpio);
  1315. }
  1316. EXPORT_SYMBOL_GPL(gpio_free_array);
  1317. /**
  1318. * gpiochip_is_requested - return string iff signal was requested
  1319. * @chip: controller managing the signal
  1320. * @offset: of signal within controller's 0..(ngpio - 1) range
  1321. *
  1322. * Returns NULL if the GPIO is not currently requested, else a string.
  1323. * If debugfs support is enabled, the string returned is the label passed
  1324. * to gpio_request(); otherwise it is a meaningless constant.
  1325. *
  1326. * This function is for use by GPIO controller drivers. The label can
  1327. * help with diagnostics, and knowing that the signal is used as a GPIO
  1328. * can help avoid accidentally multiplexing it to another controller.
  1329. */
  1330. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1331. {
  1332. struct gpio_desc *desc;
  1333. if (!GPIO_OFFSET_VALID(chip, offset))
  1334. return NULL;
  1335. desc = &chip->desc[offset];
  1336. if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
  1337. return NULL;
  1338. #ifdef CONFIG_DEBUG_FS
  1339. return desc->label;
  1340. #else
  1341. return "?";
  1342. #endif
  1343. }
  1344. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1345. /* Drivers MUST set GPIO direction before making get/set calls. In
  1346. * some cases this is done in early boot, before IRQs are enabled.
  1347. *
  1348. * As a rule these aren't called more than once (except for drivers
  1349. * using the open-drain emulation idiom) so these are natural places
  1350. * to accumulate extra debugging checks. Note that we can't (yet)
  1351. * rely on gpio_request() having been called beforehand.
  1352. */
  1353. static int gpiod_direction_input(struct gpio_desc *desc)
  1354. {
  1355. unsigned long flags;
  1356. struct gpio_chip *chip;
  1357. int status = -EINVAL;
  1358. int offset;
  1359. if (!desc || !desc->chip) {
  1360. pr_warn("%s: invalid GPIO\n", __func__);
  1361. return -EINVAL;
  1362. }
  1363. chip = desc->chip;
  1364. if (!chip->get || !chip->direction_input) {
  1365. pr_warn("%s: missing get() or direction_input() operations\n",
  1366. __func__);
  1367. return -EIO;
  1368. }
  1369. spin_lock_irqsave(&gpio_lock, flags);
  1370. status = gpio_ensure_requested(desc);
  1371. if (status < 0)
  1372. goto fail;
  1373. /* now we know the gpio is valid and chip won't vanish */
  1374. spin_unlock_irqrestore(&gpio_lock, flags);
  1375. might_sleep_if(chip->can_sleep);
  1376. offset = gpio_chip_hwgpio(desc);
  1377. if (status) {
  1378. status = chip->request(chip, offset);
  1379. if (status < 0) {
  1380. pr_debug("GPIO-%d: chip request fail, %d\n",
  1381. desc_to_gpio(desc), status);
  1382. /* and it's not available to anyone else ...
  1383. * gpio_request() is the fully clean solution.
  1384. */
  1385. goto lose;
  1386. }
  1387. }
  1388. status = chip->direction_input(chip, offset);
  1389. if (status == 0)
  1390. clear_bit(FLAG_IS_OUT, &desc->flags);
  1391. trace_gpio_direction(desc_to_gpio(desc), 1, status);
  1392. lose:
  1393. return status;
  1394. fail:
  1395. spin_unlock_irqrestore(&gpio_lock, flags);
  1396. if (status)
  1397. pr_debug("%s: gpio-%d status %d\n", __func__,
  1398. desc_to_gpio(desc), status);
  1399. return status;
  1400. }
  1401. int gpio_direction_input(unsigned gpio)
  1402. {
  1403. return gpiod_direction_input(gpio_to_desc(gpio));
  1404. }
  1405. EXPORT_SYMBOL_GPL(gpio_direction_input);
  1406. static int gpiod_direction_output(struct gpio_desc *desc, int value)
  1407. {
  1408. unsigned long flags;
  1409. struct gpio_chip *chip;
  1410. int status = -EINVAL;
  1411. int offset;
  1412. if (!desc || !desc->chip) {
  1413. pr_warn("%s: invalid GPIO\n", __func__);
  1414. return -EINVAL;
  1415. }
  1416. /* Open drain pin should not be driven to 1 */
  1417. if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1418. return gpiod_direction_input(desc);
  1419. /* Open source pin should not be driven to 0 */
  1420. if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1421. return gpiod_direction_input(desc);
  1422. chip = desc->chip;
  1423. if (!chip->set || !chip->direction_output) {
  1424. pr_warn("%s: missing set() or direction_output() operations\n",
  1425. __func__);
  1426. return -EIO;
  1427. }
  1428. spin_lock_irqsave(&gpio_lock, flags);
  1429. status = gpio_ensure_requested(desc);
  1430. if (status < 0)
  1431. goto fail;
  1432. /* now we know the gpio is valid and chip won't vanish */
  1433. spin_unlock_irqrestore(&gpio_lock, flags);
  1434. might_sleep_if(chip->can_sleep);
  1435. offset = gpio_chip_hwgpio(desc);
  1436. if (status) {
  1437. status = chip->request(chip, offset);
  1438. if (status < 0) {
  1439. pr_debug("GPIO-%d: chip request fail, %d\n",
  1440. desc_to_gpio(desc), 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_output(chip, offset, value);
  1448. if (status == 0)
  1449. set_bit(FLAG_IS_OUT, &desc->flags);
  1450. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1451. trace_gpio_direction(desc_to_gpio(desc), 0, status);
  1452. lose:
  1453. return status;
  1454. fail:
  1455. spin_unlock_irqrestore(&gpio_lock, flags);
  1456. if (status)
  1457. pr_debug("%s: gpio-%d status %d\n", __func__,
  1458. desc_to_gpio(desc), status);
  1459. return status;
  1460. }
  1461. int gpio_direction_output(unsigned gpio, int value)
  1462. {
  1463. return gpiod_direction_output(gpio_to_desc(gpio), value);
  1464. }
  1465. EXPORT_SYMBOL_GPL(gpio_direction_output);
  1466. /**
  1467. * gpio_set_debounce - sets @debounce time for a @gpio
  1468. * @gpio: the gpio to set debounce time
  1469. * @debounce: debounce time is microseconds
  1470. *
  1471. * returns -ENOTSUPP if the controller does not support setting
  1472. * debounce.
  1473. */
  1474. static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
  1475. {
  1476. unsigned long flags;
  1477. struct gpio_chip *chip;
  1478. int status = -EINVAL;
  1479. int offset;
  1480. if (!desc || !desc->chip) {
  1481. pr_warn("%s: invalid GPIO\n", __func__);
  1482. return -EINVAL;
  1483. }
  1484. chip = desc->chip;
  1485. if (!chip->set || !chip->set_debounce) {
  1486. pr_debug("%s: missing set() or set_debounce() operations\n",
  1487. __func__);
  1488. return -ENOTSUPP;
  1489. }
  1490. spin_lock_irqsave(&gpio_lock, flags);
  1491. status = gpio_ensure_requested(desc);
  1492. if (status < 0)
  1493. goto fail;
  1494. /* now we know the gpio is valid and chip won't vanish */
  1495. spin_unlock_irqrestore(&gpio_lock, flags);
  1496. might_sleep_if(chip->can_sleep);
  1497. offset = gpio_chip_hwgpio(desc);
  1498. return chip->set_debounce(chip, offset, debounce);
  1499. fail:
  1500. spin_unlock_irqrestore(&gpio_lock, flags);
  1501. if (status)
  1502. pr_debug("%s: gpio-%d status %d\n", __func__,
  1503. desc_to_gpio(desc), status);
  1504. return status;
  1505. }
  1506. int gpio_set_debounce(unsigned gpio, unsigned debounce)
  1507. {
  1508. return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
  1509. }
  1510. EXPORT_SYMBOL_GPL(gpio_set_debounce);
  1511. /* I/O calls are only valid after configuration completed; the relevant
  1512. * "is this a valid GPIO" error checks should already have been done.
  1513. *
  1514. * "Get" operations are often inlinable as reading a pin value register,
  1515. * and masking the relevant bit in that register.
  1516. *
  1517. * When "set" operations are inlinable, they involve writing that mask to
  1518. * one register to set a low value, or a different register to set it high.
  1519. * Otherwise locking is needed, so there may be little value to inlining.
  1520. *
  1521. *------------------------------------------------------------------------
  1522. *
  1523. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1524. * have requested the GPIO. That can include implicit requesting by
  1525. * a direction setting call. Marking a gpio as requested locks its chip
  1526. * in memory, guaranteeing that these table lookups need no more locking
  1527. * and that gpiochip_remove() will fail.
  1528. *
  1529. * REVISIT when debugging, consider adding some instrumentation to ensure
  1530. * that the GPIO was actually requested.
  1531. */
  1532. /**
  1533. * __gpio_get_value() - return a gpio's value
  1534. * @gpio: gpio whose value will be returned
  1535. * Context: any
  1536. *
  1537. * This is used directly or indirectly to implement gpio_get_value().
  1538. * It returns the zero or nonzero value provided by the associated
  1539. * gpio_chip.get() method; or zero if no such method is provided.
  1540. */
  1541. static int gpiod_get_value(const struct gpio_desc *desc)
  1542. {
  1543. struct gpio_chip *chip;
  1544. int value;
  1545. int offset;
  1546. if (!desc)
  1547. return 0;
  1548. chip = desc->chip;
  1549. offset = gpio_chip_hwgpio(desc);
  1550. /* Should be using gpio_get_value_cansleep() */
  1551. WARN_ON(chip->can_sleep);
  1552. value = chip->get ? chip->get(chip, offset) : 0;
  1553. trace_gpio_value(desc_to_gpio(desc), 1, value);
  1554. return value;
  1555. }
  1556. int __gpio_get_value(unsigned gpio)
  1557. {
  1558. return gpiod_get_value(gpio_to_desc(gpio));
  1559. }
  1560. EXPORT_SYMBOL_GPL(__gpio_get_value);
  1561. /*
  1562. * _gpio_set_open_drain_value() - Set the open drain gpio's value.
  1563. * @gpio: Gpio whose state need to be set.
  1564. * @chip: Gpio chip.
  1565. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1566. */
  1567. static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
  1568. {
  1569. int err = 0;
  1570. struct gpio_chip *chip = desc->chip;
  1571. int offset = gpio_chip_hwgpio(desc);
  1572. if (value) {
  1573. err = chip->direction_input(chip, offset);
  1574. if (!err)
  1575. clear_bit(FLAG_IS_OUT, &desc->flags);
  1576. } else {
  1577. err = chip->direction_output(chip, offset, 0);
  1578. if (!err)
  1579. set_bit(FLAG_IS_OUT, &desc->flags);
  1580. }
  1581. trace_gpio_direction(desc_to_gpio(desc), value, err);
  1582. if (err < 0)
  1583. pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
  1584. __func__, desc_to_gpio(desc), err);
  1585. }
  1586. /*
  1587. * _gpio_set_open_source() - Set the open source gpio's value.
  1588. * @gpio: Gpio whose state need to be set.
  1589. * @chip: Gpio chip.
  1590. * @value: Non-zero for setting it HIGH otherise it will set to LOW.
  1591. */
  1592. static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
  1593. {
  1594. int err = 0;
  1595. struct gpio_chip *chip = desc->chip;
  1596. int offset = gpio_chip_hwgpio(desc);
  1597. if (value) {
  1598. err = chip->direction_output(chip, offset, 1);
  1599. if (!err)
  1600. set_bit(FLAG_IS_OUT, &desc->flags);
  1601. } else {
  1602. err = chip->direction_input(chip, offset);
  1603. if (!err)
  1604. clear_bit(FLAG_IS_OUT, &desc->flags);
  1605. }
  1606. trace_gpio_direction(desc_to_gpio(desc), !value, err);
  1607. if (err < 0)
  1608. pr_err("%s: Error in set_value for open source gpio%d err %d\n",
  1609. __func__, desc_to_gpio(desc), err);
  1610. }
  1611. /**
  1612. * __gpio_set_value() - assign a gpio's value
  1613. * @gpio: gpio whose value will be assigned
  1614. * @value: value to assign
  1615. * Context: any
  1616. *
  1617. * This is used directly or indirectly to implement gpio_set_value().
  1618. * It invokes the associated gpio_chip.set() method.
  1619. */
  1620. static void gpiod_set_value(struct gpio_desc *desc, int value)
  1621. {
  1622. struct gpio_chip *chip;
  1623. if (!desc)
  1624. return;
  1625. chip = desc->chip;
  1626. /* Should be using gpio_set_value_cansleep() */
  1627. WARN_ON(chip->can_sleep);
  1628. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1629. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1630. _gpio_set_open_drain_value(desc, value);
  1631. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1632. _gpio_set_open_source_value(desc, value);
  1633. else
  1634. chip->set(chip, gpio_chip_hwgpio(desc), value);
  1635. }
  1636. void __gpio_set_value(unsigned gpio, int value)
  1637. {
  1638. return gpiod_set_value(gpio_to_desc(gpio), value);
  1639. }
  1640. EXPORT_SYMBOL_GPL(__gpio_set_value);
  1641. /**
  1642. * __gpio_cansleep() - report whether gpio value access will sleep
  1643. * @gpio: gpio in question
  1644. * Context: any
  1645. *
  1646. * This is used directly or indirectly to implement gpio_cansleep(). It
  1647. * returns nonzero if access reading or writing the GPIO value can sleep.
  1648. */
  1649. static int gpiod_cansleep(const struct gpio_desc *desc)
  1650. {
  1651. if (!desc)
  1652. return 0;
  1653. /* only call this on GPIOs that are valid! */
  1654. return desc->chip->can_sleep;
  1655. }
  1656. int __gpio_cansleep(unsigned gpio)
  1657. {
  1658. return gpiod_cansleep(gpio_to_desc(gpio));
  1659. }
  1660. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  1661. /**
  1662. * __gpio_to_irq() - return the IRQ corresponding to a GPIO
  1663. * @gpio: gpio whose IRQ will be returned (already requested)
  1664. * Context: any
  1665. *
  1666. * This is used directly or indirectly to implement gpio_to_irq().
  1667. * It returns the number of the IRQ signaled by this (input) GPIO,
  1668. * or a negative errno.
  1669. */
  1670. static int gpiod_to_irq(const struct gpio_desc *desc)
  1671. {
  1672. struct gpio_chip *chip;
  1673. int offset;
  1674. if (!desc)
  1675. return -EINVAL;
  1676. chip = desc->chip;
  1677. offset = gpio_chip_hwgpio(desc);
  1678. return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
  1679. }
  1680. int __gpio_to_irq(unsigned gpio)
  1681. {
  1682. return gpiod_to_irq(gpio_to_desc(gpio));
  1683. }
  1684. EXPORT_SYMBOL_GPL(__gpio_to_irq);
  1685. /* There's no value in making it easy to inline GPIO calls that may sleep.
  1686. * Common examples include ones connected to I2C or SPI chips.
  1687. */
  1688. static int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  1689. {
  1690. struct gpio_chip *chip;
  1691. int value;
  1692. int offset;
  1693. might_sleep_if(extra_checks);
  1694. if (!desc)
  1695. return 0;
  1696. chip = desc->chip;
  1697. offset = gpio_chip_hwgpio(desc);
  1698. value = chip->get ? chip->get(chip, offset) : 0;
  1699. trace_gpio_value(desc_to_gpio(desc), 1, value);
  1700. return value;
  1701. }
  1702. int gpio_get_value_cansleep(unsigned gpio)
  1703. {
  1704. return gpiod_get_value_cansleep(gpio_to_desc(gpio));
  1705. }
  1706. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  1707. static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  1708. {
  1709. struct gpio_chip *chip;
  1710. might_sleep_if(extra_checks);
  1711. if (!desc)
  1712. return;
  1713. chip = desc->chip;
  1714. trace_gpio_value(desc_to_gpio(desc), 0, value);
  1715. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1716. _gpio_set_open_drain_value(desc, value);
  1717. else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1718. _gpio_set_open_source_value(desc, value);
  1719. else
  1720. chip->set(chip, gpio_chip_hwgpio(desc), value);
  1721. }
  1722. void gpio_set_value_cansleep(unsigned gpio, int value)
  1723. {
  1724. return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
  1725. }
  1726. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  1727. #ifdef CONFIG_DEBUG_FS
  1728. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1729. {
  1730. unsigned i;
  1731. unsigned gpio = chip->base;
  1732. struct gpio_desc *gdesc = &chip->desc[0];
  1733. int is_out;
  1734. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1735. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1736. continue;
  1737. gpiod_get_direction(gdesc);
  1738. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1739. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
  1740. gpio, gdesc->label,
  1741. is_out ? "out" : "in ",
  1742. chip->get
  1743. ? (chip->get(chip, i) ? "hi" : "lo")
  1744. : "? ");
  1745. seq_printf(s, "\n");
  1746. }
  1747. }
  1748. static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
  1749. {
  1750. unsigned long flags;
  1751. struct gpio_chip *chip = NULL;
  1752. loff_t index = *pos;
  1753. s->private = "";
  1754. spin_lock_irqsave(&gpio_lock, flags);
  1755. list_for_each_entry(chip, &gpio_chips, list)
  1756. if (index-- == 0) {
  1757. spin_unlock_irqrestore(&gpio_lock, flags);
  1758. return chip;
  1759. }
  1760. spin_unlock_irqrestore(&gpio_lock, flags);
  1761. return NULL;
  1762. }
  1763. static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1764. {
  1765. unsigned long flags;
  1766. struct gpio_chip *chip = v;
  1767. void *ret = NULL;
  1768. spin_lock_irqsave(&gpio_lock, flags);
  1769. if (list_is_last(&chip->list, &gpio_chips))
  1770. ret = NULL;
  1771. else
  1772. ret = list_entry(chip->list.next, struct gpio_chip, list);
  1773. spin_unlock_irqrestore(&gpio_lock, flags);
  1774. s->private = "\n";
  1775. ++*pos;
  1776. return ret;
  1777. }
  1778. static void gpiolib_seq_stop(struct seq_file *s, void *v)
  1779. {
  1780. }
  1781. static int gpiolib_seq_show(struct seq_file *s, void *v)
  1782. {
  1783. struct gpio_chip *chip = v;
  1784. struct device *dev;
  1785. seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
  1786. chip->base, chip->base + chip->ngpio - 1);
  1787. dev = chip->dev;
  1788. if (dev)
  1789. seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
  1790. dev_name(dev));
  1791. if (chip->label)
  1792. seq_printf(s, ", %s", chip->label);
  1793. if (chip->can_sleep)
  1794. seq_printf(s, ", can sleep");
  1795. seq_printf(s, ":\n");
  1796. if (chip->dbg_show)
  1797. chip->dbg_show(s, chip);
  1798. else
  1799. gpiolib_dbg_show(s, chip);
  1800. return 0;
  1801. }
  1802. static const struct seq_operations gpiolib_seq_ops = {
  1803. .start = gpiolib_seq_start,
  1804. .next = gpiolib_seq_next,
  1805. .stop = gpiolib_seq_stop,
  1806. .show = gpiolib_seq_show,
  1807. };
  1808. static int gpiolib_open(struct inode *inode, struct file *file)
  1809. {
  1810. return seq_open(file, &gpiolib_seq_ops);
  1811. }
  1812. static const struct file_operations gpiolib_operations = {
  1813. .owner = THIS_MODULE,
  1814. .open = gpiolib_open,
  1815. .read = seq_read,
  1816. .llseek = seq_lseek,
  1817. .release = seq_release,
  1818. };
  1819. static int __init gpiolib_debugfs_init(void)
  1820. {
  1821. /* /sys/kernel/debug/gpio */
  1822. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1823. NULL, NULL, &gpiolib_operations);
  1824. return 0;
  1825. }
  1826. subsys_initcall(gpiolib_debugfs_init);
  1827. #endif /* DEBUG_FS */