gpiolib.c 53 KB

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