gpiolib.c 42 KB

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