gpiolib.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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. /* Optional implementation infrastructure for GPIO interfaces.
  15. *
  16. * Platforms may want to use this if they tend to use very many GPIOs
  17. * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
  18. *
  19. * When kernel footprint or instruction count is an issue, simpler
  20. * implementations may be preferred. The GPIO programming interface
  21. * allows for inlining speed-critical get/set operations for common
  22. * cases, so that access to SOC-integrated GPIOs can sometimes cost
  23. * only an instruction or two per bit.
  24. */
  25. /* When debugging, extend minimal trust to callers and platform code.
  26. * Also emit diagnostic messages that may help initial bringup, when
  27. * board setup or driver bugs are most common.
  28. *
  29. * Otherwise, minimize overhead in what may be bitbanging codepaths.
  30. */
  31. #ifdef DEBUG
  32. #define extra_checks 1
  33. #else
  34. #define extra_checks 0
  35. #endif
  36. /* gpio_lock prevents conflicts during gpio_desc[] table updates.
  37. * While any GPIO is requested, its gpio_chip is not removable;
  38. * each GPIO's "requested" flag serves as a lock and refcount.
  39. */
  40. static DEFINE_SPINLOCK(gpio_lock);
  41. struct gpio_desc {
  42. struct gpio_chip *chip;
  43. unsigned long flags;
  44. /* flag symbols are bit numbers */
  45. #define FLAG_REQUESTED 0
  46. #define FLAG_IS_OUT 1
  47. #define FLAG_RESERVED 2
  48. #define FLAG_EXPORT 3 /* protected by sysfs_lock */
  49. #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
  50. #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
  51. #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
  52. #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
  53. #define PDESC_ID_SHIFT 16 /* add new flags before this one */
  54. #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
  55. #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
  56. #ifdef CONFIG_DEBUG_FS
  57. const char *label;
  58. #endif
  59. };
  60. static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
  61. #ifdef CONFIG_GPIO_SYSFS
  62. struct poll_desc {
  63. struct work_struct work;
  64. struct sysfs_dirent *value_sd;
  65. };
  66. static struct idr pdesc_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. static inline 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 work_struct *work = priv;
  277. schedule_work(work);
  278. return IRQ_HANDLED;
  279. }
  280. static void gpio_notify_sysfs(struct work_struct *work)
  281. {
  282. struct poll_desc *pdesc;
  283. pdesc = container_of(work, struct poll_desc, work);
  284. sysfs_notify_dirent(pdesc->value_sd);
  285. }
  286. static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
  287. unsigned long gpio_flags)
  288. {
  289. struct poll_desc *pdesc;
  290. unsigned long irq_flags;
  291. int ret, irq, id;
  292. if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
  293. return 0;
  294. irq = gpio_to_irq(desc - gpio_desc);
  295. if (irq < 0)
  296. return -EIO;
  297. id = desc->flags >> PDESC_ID_SHIFT;
  298. pdesc = idr_find(&pdesc_idr, id);
  299. if (pdesc) {
  300. free_irq(irq, &pdesc->work);
  301. cancel_work_sync(&pdesc->work);
  302. }
  303. desc->flags &= ~GPIO_TRIGGER_MASK;
  304. if (!gpio_flags) {
  305. ret = 0;
  306. goto free_sd;
  307. }
  308. irq_flags = IRQF_SHARED;
  309. if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
  310. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  311. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  312. if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
  313. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  314. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  315. if (!pdesc) {
  316. pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
  317. if (!pdesc) {
  318. ret = -ENOMEM;
  319. goto err_out;
  320. }
  321. do {
  322. ret = -ENOMEM;
  323. if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
  324. ret = idr_get_new_above(&pdesc_idr,
  325. pdesc, 1, &id);
  326. } while (ret == -EAGAIN);
  327. if (ret)
  328. goto free_mem;
  329. desc->flags &= GPIO_FLAGS_MASK;
  330. desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
  331. if (desc->flags >> PDESC_ID_SHIFT != id) {
  332. ret = -ERANGE;
  333. goto free_id;
  334. }
  335. pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
  336. if (!pdesc->value_sd) {
  337. ret = -ENODEV;
  338. goto free_id;
  339. }
  340. INIT_WORK(&pdesc->work, gpio_notify_sysfs);
  341. }
  342. ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
  343. "gpiolib", &pdesc->work);
  344. if (ret)
  345. goto free_sd;
  346. desc->flags |= gpio_flags;
  347. return 0;
  348. free_sd:
  349. if (pdesc)
  350. sysfs_put(pdesc->value_sd);
  351. free_id:
  352. idr_remove(&pdesc_idr, id);
  353. desc->flags &= GPIO_FLAGS_MASK;
  354. free_mem:
  355. kfree(pdesc);
  356. err_out:
  357. return ret;
  358. }
  359. static const struct {
  360. const char *name;
  361. unsigned long flags;
  362. } trigger_types[] = {
  363. { "none", 0 },
  364. { "falling", BIT(FLAG_TRIG_FALL) },
  365. { "rising", BIT(FLAG_TRIG_RISE) },
  366. { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
  367. };
  368. static ssize_t gpio_edge_show(struct device *dev,
  369. struct device_attribute *attr, char *buf)
  370. {
  371. const struct gpio_desc *desc = dev_get_drvdata(dev);
  372. ssize_t status;
  373. mutex_lock(&sysfs_lock);
  374. if (!test_bit(FLAG_EXPORT, &desc->flags))
  375. status = -EIO;
  376. else {
  377. int i;
  378. status = 0;
  379. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  380. if ((desc->flags & GPIO_TRIGGER_MASK)
  381. == trigger_types[i].flags) {
  382. status = sprintf(buf, "%s\n",
  383. trigger_types[i].name);
  384. break;
  385. }
  386. }
  387. mutex_unlock(&sysfs_lock);
  388. return status;
  389. }
  390. static ssize_t gpio_edge_store(struct device *dev,
  391. struct device_attribute *attr, const char *buf, size_t size)
  392. {
  393. struct gpio_desc *desc = dev_get_drvdata(dev);
  394. ssize_t status;
  395. int i;
  396. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  397. if (sysfs_streq(trigger_types[i].name, buf))
  398. goto found;
  399. return -EINVAL;
  400. found:
  401. mutex_lock(&sysfs_lock);
  402. if (!test_bit(FLAG_EXPORT, &desc->flags))
  403. status = -EIO;
  404. else {
  405. status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
  406. if (!status)
  407. status = size;
  408. }
  409. mutex_unlock(&sysfs_lock);
  410. return status;
  411. }
  412. static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
  413. static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
  414. int value)
  415. {
  416. int status = 0;
  417. if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
  418. return 0;
  419. if (value)
  420. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  421. else
  422. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  423. /* reconfigure poll(2) support if enabled on one edge only */
  424. if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
  425. !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
  426. unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
  427. gpio_setup_irq(desc, dev, 0);
  428. status = gpio_setup_irq(desc, dev, trigger_flags);
  429. }
  430. return status;
  431. }
  432. static ssize_t gpio_active_low_show(struct device *dev,
  433. struct device_attribute *attr, char *buf)
  434. {
  435. const struct gpio_desc *desc = dev_get_drvdata(dev);
  436. ssize_t status;
  437. mutex_lock(&sysfs_lock);
  438. if (!test_bit(FLAG_EXPORT, &desc->flags))
  439. status = -EIO;
  440. else
  441. status = sprintf(buf, "%d\n",
  442. !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
  443. mutex_unlock(&sysfs_lock);
  444. return status;
  445. }
  446. static ssize_t gpio_active_low_store(struct device *dev,
  447. struct device_attribute *attr, const char *buf, size_t size)
  448. {
  449. struct gpio_desc *desc = dev_get_drvdata(dev);
  450. ssize_t status;
  451. mutex_lock(&sysfs_lock);
  452. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  453. status = -EIO;
  454. } else {
  455. long value;
  456. status = strict_strtol(buf, 0, &value);
  457. if (status == 0)
  458. status = sysfs_set_active_low(desc, dev, value != 0);
  459. }
  460. mutex_unlock(&sysfs_lock);
  461. return status ? : size;
  462. }
  463. static const DEVICE_ATTR(active_low, 0644,
  464. gpio_active_low_show, gpio_active_low_store);
  465. static const struct attribute *gpio_attrs[] = {
  466. &dev_attr_value.attr,
  467. &dev_attr_active_low.attr,
  468. NULL,
  469. };
  470. static const struct attribute_group gpio_attr_group = {
  471. .attrs = (struct attribute **) gpio_attrs,
  472. };
  473. /*
  474. * /sys/class/gpio/gpiochipN/
  475. * /base ... matching gpio_chip.base (N)
  476. * /label ... matching gpio_chip.label
  477. * /ngpio ... matching gpio_chip.ngpio
  478. */
  479. static ssize_t chip_base_show(struct device *dev,
  480. struct device_attribute *attr, char *buf)
  481. {
  482. const struct gpio_chip *chip = dev_get_drvdata(dev);
  483. return sprintf(buf, "%d\n", chip->base);
  484. }
  485. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  486. static ssize_t chip_label_show(struct device *dev,
  487. struct device_attribute *attr, char *buf)
  488. {
  489. const struct gpio_chip *chip = dev_get_drvdata(dev);
  490. return sprintf(buf, "%s\n", chip->label ? : "");
  491. }
  492. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  493. static ssize_t chip_ngpio_show(struct device *dev,
  494. struct device_attribute *attr, char *buf)
  495. {
  496. const struct gpio_chip *chip = dev_get_drvdata(dev);
  497. return sprintf(buf, "%u\n", chip->ngpio);
  498. }
  499. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  500. static const struct attribute *gpiochip_attrs[] = {
  501. &dev_attr_base.attr,
  502. &dev_attr_label.attr,
  503. &dev_attr_ngpio.attr,
  504. NULL,
  505. };
  506. static const struct attribute_group gpiochip_attr_group = {
  507. .attrs = (struct attribute **) gpiochip_attrs,
  508. };
  509. /*
  510. * /sys/class/gpio/export ... write-only
  511. * integer N ... number of GPIO to export (full access)
  512. * /sys/class/gpio/unexport ... write-only
  513. * integer N ... number of GPIO to unexport
  514. */
  515. static ssize_t export_store(struct class *class,
  516. struct class_attribute *attr,
  517. const char *buf, size_t len)
  518. {
  519. long gpio;
  520. int status;
  521. status = strict_strtol(buf, 0, &gpio);
  522. if (status < 0)
  523. goto done;
  524. /* No extra locking here; FLAG_SYSFS just signifies that the
  525. * request and export were done by on behalf of userspace, so
  526. * they may be undone on its behalf too.
  527. */
  528. status = gpio_request(gpio, "sysfs");
  529. if (status < 0)
  530. goto done;
  531. status = gpio_export(gpio, true);
  532. if (status < 0)
  533. gpio_free(gpio);
  534. else
  535. set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
  536. done:
  537. if (status)
  538. pr_debug("%s: status %d\n", __func__, status);
  539. return status ? : len;
  540. }
  541. static ssize_t unexport_store(struct class *class,
  542. struct class_attribute *attr,
  543. const char *buf, size_t len)
  544. {
  545. long gpio;
  546. int status;
  547. status = strict_strtol(buf, 0, &gpio);
  548. if (status < 0)
  549. goto done;
  550. status = -EINVAL;
  551. /* reject bogus commands (gpio_unexport ignores them) */
  552. if (!gpio_is_valid(gpio))
  553. goto done;
  554. /* No extra locking here; FLAG_SYSFS just signifies that the
  555. * request and export were done by on behalf of userspace, so
  556. * they may be undone on its behalf too.
  557. */
  558. if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
  559. status = 0;
  560. gpio_free(gpio);
  561. }
  562. done:
  563. if (status)
  564. pr_debug("%s: status %d\n", __func__, status);
  565. return status ? : len;
  566. }
  567. static struct class_attribute gpio_class_attrs[] = {
  568. __ATTR(export, 0200, NULL, export_store),
  569. __ATTR(unexport, 0200, NULL, unexport_store),
  570. __ATTR_NULL,
  571. };
  572. static struct class gpio_class = {
  573. .name = "gpio",
  574. .owner = THIS_MODULE,
  575. .class_attrs = gpio_class_attrs,
  576. };
  577. /**
  578. * gpio_export - export a GPIO through sysfs
  579. * @gpio: gpio to make available, already requested
  580. * @direction_may_change: true if userspace may change gpio direction
  581. * Context: arch_initcall or later
  582. *
  583. * When drivers want to make a GPIO accessible to userspace after they
  584. * have requested it -- perhaps while debugging, or as part of their
  585. * public interface -- they may use this routine. If the GPIO can
  586. * change direction (some can't) and the caller allows it, userspace
  587. * will see "direction" sysfs attribute which may be used to change
  588. * the gpio's direction. A "value" attribute will always be provided.
  589. *
  590. * Returns zero on success, else an error.
  591. */
  592. int gpio_export(unsigned gpio, bool direction_may_change)
  593. {
  594. unsigned long flags;
  595. struct gpio_desc *desc;
  596. int status = -EINVAL;
  597. const char *ioname = NULL;
  598. /* can't export until sysfs is available ... */
  599. if (!gpio_class.p) {
  600. pr_debug("%s: called too early!\n", __func__);
  601. return -ENOENT;
  602. }
  603. if (!gpio_is_valid(gpio))
  604. goto done;
  605. mutex_lock(&sysfs_lock);
  606. spin_lock_irqsave(&gpio_lock, flags);
  607. desc = &gpio_desc[gpio];
  608. if (test_bit(FLAG_REQUESTED, &desc->flags)
  609. && !test_bit(FLAG_EXPORT, &desc->flags)) {
  610. status = 0;
  611. if (!desc->chip->direction_input
  612. || !desc->chip->direction_output)
  613. direction_may_change = false;
  614. }
  615. spin_unlock_irqrestore(&gpio_lock, flags);
  616. if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
  617. ioname = desc->chip->names[gpio - desc->chip->base];
  618. if (status == 0) {
  619. struct device *dev;
  620. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  621. desc, ioname ? ioname : "gpio%u", gpio);
  622. if (!IS_ERR(dev)) {
  623. status = sysfs_create_group(&dev->kobj,
  624. &gpio_attr_group);
  625. if (!status && direction_may_change)
  626. status = device_create_file(dev,
  627. &dev_attr_direction);
  628. if (!status && gpio_to_irq(gpio) >= 0
  629. && (direction_may_change
  630. || !test_bit(FLAG_IS_OUT,
  631. &desc->flags)))
  632. status = device_create_file(dev,
  633. &dev_attr_edge);
  634. if (status != 0)
  635. device_unregister(dev);
  636. } else
  637. status = PTR_ERR(dev);
  638. if (status == 0)
  639. set_bit(FLAG_EXPORT, &desc->flags);
  640. }
  641. mutex_unlock(&sysfs_lock);
  642. done:
  643. if (status)
  644. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  645. return status;
  646. }
  647. EXPORT_SYMBOL_GPL(gpio_export);
  648. static int match_export(struct device *dev, void *data)
  649. {
  650. return dev_get_drvdata(dev) == data;
  651. }
  652. /**
  653. * gpio_export_link - create a sysfs link to an exported GPIO node
  654. * @dev: device under which to create symlink
  655. * @name: name of the symlink
  656. * @gpio: gpio to create symlink to, already exported
  657. *
  658. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  659. * node. Caller is responsible for unlinking.
  660. *
  661. * Returns zero on success, else an error.
  662. */
  663. int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
  664. {
  665. struct gpio_desc *desc;
  666. int status = -EINVAL;
  667. if (!gpio_is_valid(gpio))
  668. goto done;
  669. mutex_lock(&sysfs_lock);
  670. desc = &gpio_desc[gpio];
  671. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  672. struct device *tdev;
  673. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  674. if (tdev != NULL) {
  675. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  676. name);
  677. } else {
  678. status = -ENODEV;
  679. }
  680. }
  681. mutex_unlock(&sysfs_lock);
  682. done:
  683. if (status)
  684. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  685. return status;
  686. }
  687. EXPORT_SYMBOL_GPL(gpio_export_link);
  688. /**
  689. * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
  690. * @gpio: gpio to change
  691. * @value: non-zero to use active low, i.e. inverted values
  692. *
  693. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  694. * The GPIO does not have to be exported yet. If poll(2) support has
  695. * been enabled for either rising or falling edge, it will be
  696. * reconfigured to follow the new polarity.
  697. *
  698. * Returns zero on success, else an error.
  699. */
  700. int gpio_sysfs_set_active_low(unsigned gpio, int value)
  701. {
  702. struct gpio_desc *desc;
  703. struct device *dev = NULL;
  704. int status = -EINVAL;
  705. if (!gpio_is_valid(gpio))
  706. goto done;
  707. mutex_lock(&sysfs_lock);
  708. desc = &gpio_desc[gpio];
  709. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  710. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  711. if (dev == NULL) {
  712. status = -ENODEV;
  713. goto unlock;
  714. }
  715. }
  716. status = sysfs_set_active_low(desc, dev, value);
  717. unlock:
  718. mutex_unlock(&sysfs_lock);
  719. done:
  720. if (status)
  721. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  722. return status;
  723. }
  724. EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
  725. /**
  726. * gpio_unexport - reverse effect of gpio_export()
  727. * @gpio: gpio to make unavailable
  728. *
  729. * This is implicit on gpio_free().
  730. */
  731. void gpio_unexport(unsigned gpio)
  732. {
  733. struct gpio_desc *desc;
  734. int status = 0;
  735. if (!gpio_is_valid(gpio)) {
  736. status = -EINVAL;
  737. goto done;
  738. }
  739. mutex_lock(&sysfs_lock);
  740. desc = &gpio_desc[gpio];
  741. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  742. struct device *dev = NULL;
  743. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  744. if (dev) {
  745. gpio_setup_irq(desc, dev, 0);
  746. clear_bit(FLAG_EXPORT, &desc->flags);
  747. put_device(dev);
  748. device_unregister(dev);
  749. } else
  750. status = -ENODEV;
  751. }
  752. mutex_unlock(&sysfs_lock);
  753. done:
  754. if (status)
  755. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  756. }
  757. EXPORT_SYMBOL_GPL(gpio_unexport);
  758. static int gpiochip_export(struct gpio_chip *chip)
  759. {
  760. int status;
  761. struct device *dev;
  762. /* Many systems register gpio chips for SOC support very early,
  763. * before driver model support is available. In those cases we
  764. * export this later, in gpiolib_sysfs_init() ... here we just
  765. * verify that _some_ field of gpio_class got initialized.
  766. */
  767. if (!gpio_class.p)
  768. return 0;
  769. /* use chip->base for the ID; it's already known to be unique */
  770. mutex_lock(&sysfs_lock);
  771. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  772. "gpiochip%d", chip->base);
  773. if (!IS_ERR(dev)) {
  774. status = sysfs_create_group(&dev->kobj,
  775. &gpiochip_attr_group);
  776. } else
  777. status = PTR_ERR(dev);
  778. chip->exported = (status == 0);
  779. mutex_unlock(&sysfs_lock);
  780. if (status) {
  781. unsigned long flags;
  782. unsigned gpio;
  783. spin_lock_irqsave(&gpio_lock, flags);
  784. gpio = chip->base;
  785. while (gpio_desc[gpio].chip == chip)
  786. gpio_desc[gpio++].chip = NULL;
  787. spin_unlock_irqrestore(&gpio_lock, flags);
  788. pr_debug("%s: chip %s status %d\n", __func__,
  789. chip->label, status);
  790. }
  791. return status;
  792. }
  793. static void gpiochip_unexport(struct gpio_chip *chip)
  794. {
  795. int status;
  796. struct device *dev;
  797. mutex_lock(&sysfs_lock);
  798. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  799. if (dev) {
  800. put_device(dev);
  801. device_unregister(dev);
  802. chip->exported = 0;
  803. status = 0;
  804. } else
  805. status = -ENODEV;
  806. mutex_unlock(&sysfs_lock);
  807. if (status)
  808. pr_debug("%s: chip %s status %d\n", __func__,
  809. chip->label, status);
  810. }
  811. static int __init gpiolib_sysfs_init(void)
  812. {
  813. int status;
  814. unsigned long flags;
  815. unsigned gpio;
  816. idr_init(&pdesc_idr);
  817. status = class_register(&gpio_class);
  818. if (status < 0)
  819. return status;
  820. /* Scan and register the gpio_chips which registered very
  821. * early (e.g. before the class_register above was called).
  822. *
  823. * We run before arch_initcall() so chip->dev nodes can have
  824. * registered, and so arch_initcall() can always gpio_export().
  825. */
  826. spin_lock_irqsave(&gpio_lock, flags);
  827. for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
  828. struct gpio_chip *chip;
  829. chip = gpio_desc[gpio].chip;
  830. if (!chip || chip->exported)
  831. continue;
  832. spin_unlock_irqrestore(&gpio_lock, flags);
  833. status = gpiochip_export(chip);
  834. spin_lock_irqsave(&gpio_lock, flags);
  835. }
  836. spin_unlock_irqrestore(&gpio_lock, flags);
  837. return status;
  838. }
  839. postcore_initcall(gpiolib_sysfs_init);
  840. #else
  841. static inline int gpiochip_export(struct gpio_chip *chip)
  842. {
  843. return 0;
  844. }
  845. static inline void gpiochip_unexport(struct gpio_chip *chip)
  846. {
  847. }
  848. #endif /* CONFIG_GPIO_SYSFS */
  849. /**
  850. * gpiochip_add() - register a gpio_chip
  851. * @chip: the chip to register, with chip->base initialized
  852. * Context: potentially before irqs or kmalloc will work
  853. *
  854. * Returns a negative errno if the chip can't be registered, such as
  855. * because the chip->base is invalid or already associated with a
  856. * different chip. Otherwise it returns zero as a success code.
  857. *
  858. * When gpiochip_add() is called very early during boot, so that GPIOs
  859. * can be freely used, the chip->dev device must be registered before
  860. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  861. * for GPIOs will fail rudely.
  862. *
  863. * If chip->base is negative, this requests dynamic assignment of
  864. * a range of valid GPIOs.
  865. */
  866. int gpiochip_add(struct gpio_chip *chip)
  867. {
  868. unsigned long flags;
  869. int status = 0;
  870. unsigned id;
  871. int base = chip->base;
  872. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  873. && base >= 0) {
  874. status = -EINVAL;
  875. goto fail;
  876. }
  877. spin_lock_irqsave(&gpio_lock, flags);
  878. if (base < 0) {
  879. base = gpiochip_find_base(chip->ngpio);
  880. if (base < 0) {
  881. status = base;
  882. goto unlock;
  883. }
  884. chip->base = base;
  885. }
  886. /* these GPIO numbers must not be managed by another gpio_chip */
  887. for (id = base; id < base + chip->ngpio; id++) {
  888. if (gpio_desc[id].chip != NULL) {
  889. status = -EBUSY;
  890. break;
  891. }
  892. }
  893. if (status == 0) {
  894. for (id = base; id < base + chip->ngpio; id++) {
  895. gpio_desc[id].chip = chip;
  896. /* REVISIT: most hardware initializes GPIOs as
  897. * inputs (often with pullups enabled) so power
  898. * usage is minimized. Linux code should set the
  899. * gpio direction first thing; but until it does,
  900. * we may expose the wrong direction in sysfs.
  901. */
  902. gpio_desc[id].flags = !chip->direction_input
  903. ? (1 << FLAG_IS_OUT)
  904. : 0;
  905. }
  906. }
  907. of_gpiochip_add(chip);
  908. unlock:
  909. spin_unlock_irqrestore(&gpio_lock, flags);
  910. if (status)
  911. goto fail;
  912. status = gpiochip_export(chip);
  913. if (status)
  914. goto fail;
  915. return 0;
  916. fail:
  917. /* failures here can mean systems won't boot... */
  918. pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
  919. chip->base, chip->base + chip->ngpio - 1,
  920. chip->label ? : "generic");
  921. return status;
  922. }
  923. EXPORT_SYMBOL_GPL(gpiochip_add);
  924. /**
  925. * gpiochip_remove() - unregister a gpio_chip
  926. * @chip: the chip to unregister
  927. *
  928. * A gpio_chip with any GPIOs still requested may not be removed.
  929. */
  930. int gpiochip_remove(struct gpio_chip *chip)
  931. {
  932. unsigned long flags;
  933. int status = 0;
  934. unsigned id;
  935. spin_lock_irqsave(&gpio_lock, flags);
  936. of_gpiochip_remove(chip);
  937. for (id = chip->base; id < chip->base + chip->ngpio; id++) {
  938. if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
  939. status = -EBUSY;
  940. break;
  941. }
  942. }
  943. if (status == 0) {
  944. for (id = chip->base; id < chip->base + chip->ngpio; id++)
  945. gpio_desc[id].chip = NULL;
  946. }
  947. spin_unlock_irqrestore(&gpio_lock, flags);
  948. if (status == 0)
  949. gpiochip_unexport(chip);
  950. return status;
  951. }
  952. EXPORT_SYMBOL_GPL(gpiochip_remove);
  953. /**
  954. * gpiochip_find() - iterator for locating a specific gpio_chip
  955. * @data: data to pass to match function
  956. * @callback: Callback function to check gpio_chip
  957. *
  958. * Similar to bus_find_device. It returns a reference to a gpio_chip as
  959. * determined by a user supplied @match callback. The callback should return
  960. * 0 if the device doesn't match and non-zero if it does. If the callback is
  961. * non-zero, this function will return to the caller and not iterate over any
  962. * more gpio_chips.
  963. */
  964. struct gpio_chip *gpiochip_find(void *data,
  965. int (*match)(struct gpio_chip *chip, void *data))
  966. {
  967. struct gpio_chip *chip = NULL;
  968. unsigned long flags;
  969. int i;
  970. spin_lock_irqsave(&gpio_lock, flags);
  971. for (i = 0; i < ARCH_NR_GPIOS; i++) {
  972. if (!gpio_desc[i].chip)
  973. continue;
  974. if (match(gpio_desc[i].chip, data)) {
  975. chip = gpio_desc[i].chip;
  976. break;
  977. }
  978. }
  979. spin_unlock_irqrestore(&gpio_lock, flags);
  980. return chip;
  981. }
  982. /* These "optional" allocation calls help prevent drivers from stomping
  983. * on each other, and help provide better diagnostics in debugfs.
  984. * They're called even less than the "set direction" calls.
  985. */
  986. int gpio_request(unsigned gpio, const char *label)
  987. {
  988. struct gpio_desc *desc;
  989. struct gpio_chip *chip;
  990. int status = -EINVAL;
  991. unsigned long flags;
  992. spin_lock_irqsave(&gpio_lock, flags);
  993. if (!gpio_is_valid(gpio))
  994. goto done;
  995. desc = &gpio_desc[gpio];
  996. chip = desc->chip;
  997. if (chip == NULL)
  998. goto done;
  999. if (!try_module_get(chip->owner))
  1000. goto done;
  1001. /* NOTE: gpio_request() can be called in early boot,
  1002. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  1003. */
  1004. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  1005. desc_set_label(desc, label ? : "?");
  1006. status = 0;
  1007. } else {
  1008. status = -EBUSY;
  1009. module_put(chip->owner);
  1010. goto done;
  1011. }
  1012. if (chip->request) {
  1013. /* chip->request may sleep */
  1014. spin_unlock_irqrestore(&gpio_lock, flags);
  1015. status = chip->request(chip, gpio - chip->base);
  1016. spin_lock_irqsave(&gpio_lock, flags);
  1017. if (status < 0) {
  1018. desc_set_label(desc, NULL);
  1019. module_put(chip->owner);
  1020. clear_bit(FLAG_REQUESTED, &desc->flags);
  1021. }
  1022. }
  1023. done:
  1024. if (status)
  1025. pr_debug("gpio_request: gpio-%d (%s) status %d\n",
  1026. gpio, label ? : "?", status);
  1027. spin_unlock_irqrestore(&gpio_lock, flags);
  1028. return status;
  1029. }
  1030. EXPORT_SYMBOL_GPL(gpio_request);
  1031. void gpio_free(unsigned gpio)
  1032. {
  1033. unsigned long flags;
  1034. struct gpio_desc *desc;
  1035. struct gpio_chip *chip;
  1036. might_sleep();
  1037. if (!gpio_is_valid(gpio)) {
  1038. WARN_ON(extra_checks);
  1039. return;
  1040. }
  1041. gpio_unexport(gpio);
  1042. spin_lock_irqsave(&gpio_lock, flags);
  1043. desc = &gpio_desc[gpio];
  1044. chip = desc->chip;
  1045. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1046. if (chip->free) {
  1047. spin_unlock_irqrestore(&gpio_lock, flags);
  1048. might_sleep_if(chip->can_sleep);
  1049. chip->free(chip, gpio - chip->base);
  1050. spin_lock_irqsave(&gpio_lock, flags);
  1051. }
  1052. desc_set_label(desc, NULL);
  1053. module_put(desc->chip->owner);
  1054. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1055. clear_bit(FLAG_REQUESTED, &desc->flags);
  1056. } else
  1057. WARN_ON(extra_checks);
  1058. spin_unlock_irqrestore(&gpio_lock, flags);
  1059. }
  1060. EXPORT_SYMBOL_GPL(gpio_free);
  1061. /**
  1062. * gpio_request_one - request a single GPIO with initial configuration
  1063. * @gpio: the GPIO number
  1064. * @flags: GPIO configuration as specified by GPIOF_*
  1065. * @label: a literal description string of this GPIO
  1066. */
  1067. int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  1068. {
  1069. int err;
  1070. err = gpio_request(gpio, label);
  1071. if (err)
  1072. return err;
  1073. if (flags & GPIOF_DIR_IN)
  1074. err = gpio_direction_input(gpio);
  1075. else
  1076. err = gpio_direction_output(gpio,
  1077. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  1078. return err;
  1079. }
  1080. EXPORT_SYMBOL_GPL(gpio_request_one);
  1081. /**
  1082. * gpio_request_array - request multiple GPIOs in a single call
  1083. * @array: array of the 'struct gpio'
  1084. * @num: how many GPIOs in the array
  1085. */
  1086. int gpio_request_array(struct gpio *array, size_t num)
  1087. {
  1088. int i, err;
  1089. for (i = 0; i < num; i++, array++) {
  1090. err = gpio_request_one(array->gpio, array->flags, array->label);
  1091. if (err)
  1092. goto err_free;
  1093. }
  1094. return 0;
  1095. err_free:
  1096. while (i--)
  1097. gpio_free((--array)->gpio);
  1098. return err;
  1099. }
  1100. EXPORT_SYMBOL_GPL(gpio_request_array);
  1101. /**
  1102. * gpio_free_array - release multiple GPIOs in a single call
  1103. * @array: array of the 'struct gpio'
  1104. * @num: how many GPIOs in the array
  1105. */
  1106. void gpio_free_array(struct gpio *array, size_t num)
  1107. {
  1108. while (num--)
  1109. gpio_free((array++)->gpio);
  1110. }
  1111. EXPORT_SYMBOL_GPL(gpio_free_array);
  1112. /**
  1113. * gpiochip_is_requested - return string iff signal was requested
  1114. * @chip: controller managing the signal
  1115. * @offset: of signal within controller's 0..(ngpio - 1) range
  1116. *
  1117. * Returns NULL if the GPIO is not currently requested, else a string.
  1118. * If debugfs support is enabled, the string returned is the label passed
  1119. * to gpio_request(); otherwise it is a meaningless constant.
  1120. *
  1121. * This function is for use by GPIO controller drivers. The label can
  1122. * help with diagnostics, and knowing that the signal is used as a GPIO
  1123. * can help avoid accidentally multiplexing it to another controller.
  1124. */
  1125. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1126. {
  1127. unsigned gpio = chip->base + offset;
  1128. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  1129. return NULL;
  1130. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  1131. return NULL;
  1132. #ifdef CONFIG_DEBUG_FS
  1133. return gpio_desc[gpio].label;
  1134. #else
  1135. return "?";
  1136. #endif
  1137. }
  1138. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1139. /* Drivers MUST set GPIO direction before making get/set calls. In
  1140. * some cases this is done in early boot, before IRQs are enabled.
  1141. *
  1142. * As a rule these aren't called more than once (except for drivers
  1143. * using the open-drain emulation idiom) so these are natural places
  1144. * to accumulate extra debugging checks. Note that we can't (yet)
  1145. * rely on gpio_request() having been called beforehand.
  1146. */
  1147. int gpio_direction_input(unsigned gpio)
  1148. {
  1149. unsigned long flags;
  1150. struct gpio_chip *chip;
  1151. struct gpio_desc *desc = &gpio_desc[gpio];
  1152. int status = -EINVAL;
  1153. spin_lock_irqsave(&gpio_lock, flags);
  1154. if (!gpio_is_valid(gpio))
  1155. goto fail;
  1156. chip = desc->chip;
  1157. if (!chip || !chip->get || !chip->direction_input)
  1158. goto fail;
  1159. gpio -= chip->base;
  1160. if (gpio >= chip->ngpio)
  1161. goto fail;
  1162. status = gpio_ensure_requested(desc, gpio);
  1163. if (status < 0)
  1164. goto fail;
  1165. /* now we know the gpio is valid and chip won't vanish */
  1166. spin_unlock_irqrestore(&gpio_lock, flags);
  1167. might_sleep_if(chip->can_sleep);
  1168. if (status) {
  1169. status = chip->request(chip, gpio);
  1170. if (status < 0) {
  1171. pr_debug("GPIO-%d: chip request fail, %d\n",
  1172. chip->base + gpio, status);
  1173. /* and it's not available to anyone else ...
  1174. * gpio_request() is the fully clean solution.
  1175. */
  1176. goto lose;
  1177. }
  1178. }
  1179. status = chip->direction_input(chip, gpio);
  1180. if (status == 0)
  1181. clear_bit(FLAG_IS_OUT, &desc->flags);
  1182. lose:
  1183. return status;
  1184. fail:
  1185. spin_unlock_irqrestore(&gpio_lock, flags);
  1186. if (status)
  1187. pr_debug("%s: gpio-%d status %d\n",
  1188. __func__, gpio, status);
  1189. return status;
  1190. }
  1191. EXPORT_SYMBOL_GPL(gpio_direction_input);
  1192. int gpio_direction_output(unsigned gpio, int value)
  1193. {
  1194. unsigned long flags;
  1195. struct gpio_chip *chip;
  1196. struct gpio_desc *desc = &gpio_desc[gpio];
  1197. int status = -EINVAL;
  1198. spin_lock_irqsave(&gpio_lock, flags);
  1199. if (!gpio_is_valid(gpio))
  1200. goto fail;
  1201. chip = desc->chip;
  1202. if (!chip || !chip->set || !chip->direction_output)
  1203. goto fail;
  1204. gpio -= chip->base;
  1205. if (gpio >= chip->ngpio)
  1206. goto fail;
  1207. status = gpio_ensure_requested(desc, gpio);
  1208. if (status < 0)
  1209. goto fail;
  1210. /* now we know the gpio is valid and chip won't vanish */
  1211. spin_unlock_irqrestore(&gpio_lock, flags);
  1212. might_sleep_if(chip->can_sleep);
  1213. if (status) {
  1214. status = chip->request(chip, gpio);
  1215. if (status < 0) {
  1216. pr_debug("GPIO-%d: chip request fail, %d\n",
  1217. chip->base + gpio, status);
  1218. /* and it's not available to anyone else ...
  1219. * gpio_request() is the fully clean solution.
  1220. */
  1221. goto lose;
  1222. }
  1223. }
  1224. status = chip->direction_output(chip, gpio, value);
  1225. if (status == 0)
  1226. set_bit(FLAG_IS_OUT, &desc->flags);
  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. chip = gpio_to_chip(gpio);
  1306. WARN_ON(chip->can_sleep);
  1307. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1308. }
  1309. EXPORT_SYMBOL_GPL(__gpio_get_value);
  1310. /**
  1311. * __gpio_set_value() - assign a gpio's value
  1312. * @gpio: gpio whose value will be assigned
  1313. * @value: value to assign
  1314. * Context: any
  1315. *
  1316. * This is used directly or indirectly to implement gpio_set_value().
  1317. * It invokes the associated gpio_chip.set() method.
  1318. */
  1319. void __gpio_set_value(unsigned gpio, int value)
  1320. {
  1321. struct gpio_chip *chip;
  1322. chip = gpio_to_chip(gpio);
  1323. WARN_ON(chip->can_sleep);
  1324. chip->set(chip, gpio - chip->base, value);
  1325. }
  1326. EXPORT_SYMBOL_GPL(__gpio_set_value);
  1327. /**
  1328. * __gpio_cansleep() - report whether gpio value access will sleep
  1329. * @gpio: gpio in question
  1330. * Context: any
  1331. *
  1332. * This is used directly or indirectly to implement gpio_cansleep(). It
  1333. * returns nonzero if access reading or writing the GPIO value can sleep.
  1334. */
  1335. int __gpio_cansleep(unsigned gpio)
  1336. {
  1337. struct gpio_chip *chip;
  1338. /* only call this on GPIOs that are valid! */
  1339. chip = gpio_to_chip(gpio);
  1340. return chip->can_sleep;
  1341. }
  1342. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  1343. /**
  1344. * __gpio_to_irq() - return the IRQ corresponding to a GPIO
  1345. * @gpio: gpio whose IRQ will be returned (already requested)
  1346. * Context: any
  1347. *
  1348. * This is used directly or indirectly to implement gpio_to_irq().
  1349. * It returns the number of the IRQ signaled by this (input) GPIO,
  1350. * or a negative errno.
  1351. */
  1352. int __gpio_to_irq(unsigned gpio)
  1353. {
  1354. struct gpio_chip *chip;
  1355. chip = gpio_to_chip(gpio);
  1356. return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
  1357. }
  1358. EXPORT_SYMBOL_GPL(__gpio_to_irq);
  1359. /* There's no value in making it easy to inline GPIO calls that may sleep.
  1360. * Common examples include ones connected to I2C or SPI chips.
  1361. */
  1362. int gpio_get_value_cansleep(unsigned gpio)
  1363. {
  1364. struct gpio_chip *chip;
  1365. might_sleep_if(extra_checks);
  1366. chip = gpio_to_chip(gpio);
  1367. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1368. }
  1369. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  1370. void gpio_set_value_cansleep(unsigned gpio, int value)
  1371. {
  1372. struct gpio_chip *chip;
  1373. might_sleep_if(extra_checks);
  1374. chip = gpio_to_chip(gpio);
  1375. chip->set(chip, gpio - chip->base, value);
  1376. }
  1377. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  1378. #ifdef CONFIG_DEBUG_FS
  1379. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1380. {
  1381. unsigned i;
  1382. unsigned gpio = chip->base;
  1383. struct gpio_desc *gdesc = &gpio_desc[gpio];
  1384. int is_out;
  1385. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1386. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1387. continue;
  1388. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1389. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
  1390. gpio, gdesc->label,
  1391. is_out ? "out" : "in ",
  1392. chip->get
  1393. ? (chip->get(chip, i) ? "hi" : "lo")
  1394. : "? ");
  1395. if (!is_out) {
  1396. int irq = gpio_to_irq(gpio);
  1397. struct irq_desc *desc = irq_to_desc(irq);
  1398. /* This races with request_irq(), set_irq_type(),
  1399. * and set_irq_wake() ... but those are "rare".
  1400. *
  1401. * More significantly, trigger type flags aren't
  1402. * currently maintained by genirq.
  1403. */
  1404. if (irq >= 0 && desc->action) {
  1405. char *trigger;
  1406. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  1407. case IRQ_TYPE_NONE:
  1408. trigger = "(default)";
  1409. break;
  1410. case IRQ_TYPE_EDGE_FALLING:
  1411. trigger = "edge-falling";
  1412. break;
  1413. case IRQ_TYPE_EDGE_RISING:
  1414. trigger = "edge-rising";
  1415. break;
  1416. case IRQ_TYPE_EDGE_BOTH:
  1417. trigger = "edge-both";
  1418. break;
  1419. case IRQ_TYPE_LEVEL_HIGH:
  1420. trigger = "level-high";
  1421. break;
  1422. case IRQ_TYPE_LEVEL_LOW:
  1423. trigger = "level-low";
  1424. break;
  1425. default:
  1426. trigger = "?trigger?";
  1427. break;
  1428. }
  1429. seq_printf(s, " irq-%d %s%s",
  1430. irq, trigger,
  1431. (desc->status & IRQ_WAKEUP)
  1432. ? " wakeup" : "");
  1433. }
  1434. }
  1435. seq_printf(s, "\n");
  1436. }
  1437. }
  1438. static int gpiolib_show(struct seq_file *s, void *unused)
  1439. {
  1440. struct gpio_chip *chip = NULL;
  1441. unsigned gpio;
  1442. int started = 0;
  1443. /* REVISIT this isn't locked against gpio_chip removal ... */
  1444. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  1445. struct device *dev;
  1446. if (chip == gpio_desc[gpio].chip)
  1447. continue;
  1448. chip = gpio_desc[gpio].chip;
  1449. if (!chip)
  1450. continue;
  1451. seq_printf(s, "%sGPIOs %d-%d",
  1452. started ? "\n" : "",
  1453. chip->base, chip->base + chip->ngpio - 1);
  1454. dev = chip->dev;
  1455. if (dev)
  1456. seq_printf(s, ", %s/%s",
  1457. dev->bus ? dev->bus->name : "no-bus",
  1458. dev_name(dev));
  1459. if (chip->label)
  1460. seq_printf(s, ", %s", chip->label);
  1461. if (chip->can_sleep)
  1462. seq_printf(s, ", can sleep");
  1463. seq_printf(s, ":\n");
  1464. started = 1;
  1465. if (chip->dbg_show)
  1466. chip->dbg_show(s, chip);
  1467. else
  1468. gpiolib_dbg_show(s, chip);
  1469. }
  1470. return 0;
  1471. }
  1472. static int gpiolib_open(struct inode *inode, struct file *file)
  1473. {
  1474. return single_open(file, gpiolib_show, NULL);
  1475. }
  1476. static const struct file_operations gpiolib_operations = {
  1477. .open = gpiolib_open,
  1478. .read = seq_read,
  1479. .llseek = seq_lseek,
  1480. .release = single_release,
  1481. };
  1482. static int __init gpiolib_debugfs_init(void)
  1483. {
  1484. /* /sys/kernel/debug/gpio */
  1485. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1486. NULL, NULL, &gpiolib_operations);
  1487. return 0;
  1488. }
  1489. subsys_initcall(gpiolib_debugfs_init);
  1490. #endif /* DEBUG_FS */