gpiolib.c 47 KB

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