gpiolib.c 41 KB

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