gpiolib.c 43 KB

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