gpiolib.c 40 KB

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