gpiolib.c 57 KB

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