gpiolib.c 43 KB

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