gpiolib.c 47 KB

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