gpiolib.c 42 KB

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