gpiolib.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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, const char *buf, size_t len)
  513. {
  514. long gpio;
  515. int status;
  516. status = strict_strtol(buf, 0, &gpio);
  517. if (status < 0)
  518. goto done;
  519. /* No extra locking here; FLAG_SYSFS just signifies that the
  520. * request and export were done by on behalf of userspace, so
  521. * they may be undone on its behalf too.
  522. */
  523. status = gpio_request(gpio, "sysfs");
  524. if (status < 0)
  525. goto done;
  526. status = gpio_export(gpio, true);
  527. if (status < 0)
  528. gpio_free(gpio);
  529. else
  530. set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
  531. done:
  532. if (status)
  533. pr_debug("%s: status %d\n", __func__, status);
  534. return status ? : len;
  535. }
  536. static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
  537. {
  538. long gpio;
  539. int status;
  540. status = strict_strtol(buf, 0, &gpio);
  541. if (status < 0)
  542. goto done;
  543. status = -EINVAL;
  544. /* reject bogus commands (gpio_unexport ignores them) */
  545. if (!gpio_is_valid(gpio))
  546. goto done;
  547. /* No extra locking here; FLAG_SYSFS just signifies that the
  548. * request and export were done by on behalf of userspace, so
  549. * they may be undone on its behalf too.
  550. */
  551. if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
  552. status = 0;
  553. gpio_free(gpio);
  554. }
  555. done:
  556. if (status)
  557. pr_debug("%s: status %d\n", __func__, status);
  558. return status ? : len;
  559. }
  560. static struct class_attribute gpio_class_attrs[] = {
  561. __ATTR(export, 0200, NULL, export_store),
  562. __ATTR(unexport, 0200, NULL, unexport_store),
  563. __ATTR_NULL,
  564. };
  565. static struct class gpio_class = {
  566. .name = "gpio",
  567. .owner = THIS_MODULE,
  568. .class_attrs = gpio_class_attrs,
  569. };
  570. /**
  571. * gpio_export - export a GPIO through sysfs
  572. * @gpio: gpio to make available, already requested
  573. * @direction_may_change: true if userspace may change gpio direction
  574. * Context: arch_initcall or later
  575. *
  576. * When drivers want to make a GPIO accessible to userspace after they
  577. * have requested it -- perhaps while debugging, or as part of their
  578. * public interface -- they may use this routine. If the GPIO can
  579. * change direction (some can't) and the caller allows it, userspace
  580. * will see "direction" sysfs attribute which may be used to change
  581. * the gpio's direction. A "value" attribute will always be provided.
  582. *
  583. * Returns zero on success, else an error.
  584. */
  585. int gpio_export(unsigned gpio, bool direction_may_change)
  586. {
  587. unsigned long flags;
  588. struct gpio_desc *desc;
  589. int status = -EINVAL;
  590. char *ioname = NULL;
  591. /* can't export until sysfs is available ... */
  592. if (!gpio_class.p) {
  593. pr_debug("%s: called too early!\n", __func__);
  594. return -ENOENT;
  595. }
  596. if (!gpio_is_valid(gpio))
  597. goto done;
  598. mutex_lock(&sysfs_lock);
  599. spin_lock_irqsave(&gpio_lock, flags);
  600. desc = &gpio_desc[gpio];
  601. if (test_bit(FLAG_REQUESTED, &desc->flags)
  602. && !test_bit(FLAG_EXPORT, &desc->flags)) {
  603. status = 0;
  604. if (!desc->chip->direction_input
  605. || !desc->chip->direction_output)
  606. direction_may_change = false;
  607. }
  608. spin_unlock_irqrestore(&gpio_lock, flags);
  609. if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
  610. ioname = desc->chip->names[gpio - desc->chip->base];
  611. if (status == 0) {
  612. struct device *dev;
  613. dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
  614. desc, ioname ? ioname : "gpio%d", gpio);
  615. if (!IS_ERR(dev)) {
  616. status = sysfs_create_group(&dev->kobj,
  617. &gpio_attr_group);
  618. if (!status && direction_may_change)
  619. status = device_create_file(dev,
  620. &dev_attr_direction);
  621. if (!status && gpio_to_irq(gpio) >= 0
  622. && (direction_may_change
  623. || !test_bit(FLAG_IS_OUT,
  624. &desc->flags)))
  625. status = device_create_file(dev,
  626. &dev_attr_edge);
  627. if (status != 0)
  628. device_unregister(dev);
  629. } else
  630. status = PTR_ERR(dev);
  631. if (status == 0)
  632. set_bit(FLAG_EXPORT, &desc->flags);
  633. }
  634. mutex_unlock(&sysfs_lock);
  635. done:
  636. if (status)
  637. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  638. return status;
  639. }
  640. EXPORT_SYMBOL_GPL(gpio_export);
  641. static int match_export(struct device *dev, void *data)
  642. {
  643. return dev_get_drvdata(dev) == data;
  644. }
  645. /**
  646. * gpio_export_link - create a sysfs link to an exported GPIO node
  647. * @dev: device under which to create symlink
  648. * @name: name of the symlink
  649. * @gpio: gpio to create symlink to, already exported
  650. *
  651. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  652. * node. Caller is responsible for unlinking.
  653. *
  654. * Returns zero on success, else an error.
  655. */
  656. int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
  657. {
  658. struct gpio_desc *desc;
  659. int status = -EINVAL;
  660. if (!gpio_is_valid(gpio))
  661. goto done;
  662. mutex_lock(&sysfs_lock);
  663. desc = &gpio_desc[gpio];
  664. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  665. struct device *tdev;
  666. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  667. if (tdev != NULL) {
  668. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  669. name);
  670. } else {
  671. status = -ENODEV;
  672. }
  673. }
  674. mutex_unlock(&sysfs_lock);
  675. done:
  676. if (status)
  677. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  678. return status;
  679. }
  680. EXPORT_SYMBOL_GPL(gpio_export_link);
  681. /**
  682. * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
  683. * @gpio: gpio to change
  684. * @value: non-zero to use active low, i.e. inverted values
  685. *
  686. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  687. * The GPIO does not have to be exported yet. If poll(2) support has
  688. * been enabled for either rising or falling edge, it will be
  689. * reconfigured to follow the new polarity.
  690. *
  691. * Returns zero on success, else an error.
  692. */
  693. int gpio_sysfs_set_active_low(unsigned gpio, int value)
  694. {
  695. struct gpio_desc *desc;
  696. struct device *dev = NULL;
  697. int status = -EINVAL;
  698. if (!gpio_is_valid(gpio))
  699. goto done;
  700. mutex_lock(&sysfs_lock);
  701. desc = &gpio_desc[gpio];
  702. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  703. struct device *dev;
  704. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  705. if (dev == NULL) {
  706. status = -ENODEV;
  707. goto unlock;
  708. }
  709. }
  710. status = sysfs_set_active_low(desc, dev, value);
  711. unlock:
  712. mutex_unlock(&sysfs_lock);
  713. done:
  714. if (status)
  715. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  716. return status;
  717. }
  718. EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
  719. /**
  720. * gpio_unexport - reverse effect of gpio_export()
  721. * @gpio: gpio to make unavailable
  722. *
  723. * This is implicit on gpio_free().
  724. */
  725. void gpio_unexport(unsigned gpio)
  726. {
  727. struct gpio_desc *desc;
  728. int status = -EINVAL;
  729. if (!gpio_is_valid(gpio))
  730. goto done;
  731. mutex_lock(&sysfs_lock);
  732. desc = &gpio_desc[gpio];
  733. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  734. struct device *dev = NULL;
  735. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  736. if (dev) {
  737. gpio_setup_irq(desc, dev, 0);
  738. clear_bit(FLAG_EXPORT, &desc->flags);
  739. put_device(dev);
  740. device_unregister(dev);
  741. status = 0;
  742. } else
  743. status = -ENODEV;
  744. }
  745. mutex_unlock(&sysfs_lock);
  746. done:
  747. if (status)
  748. pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
  749. }
  750. EXPORT_SYMBOL_GPL(gpio_unexport);
  751. static int gpiochip_export(struct gpio_chip *chip)
  752. {
  753. int status;
  754. struct device *dev;
  755. /* Many systems register gpio chips for SOC support very early,
  756. * before driver model support is available. In those cases we
  757. * export this later, in gpiolib_sysfs_init() ... here we just
  758. * verify that _some_ field of gpio_class got initialized.
  759. */
  760. if (!gpio_class.p)
  761. return 0;
  762. /* use chip->base for the ID; it's already known to be unique */
  763. mutex_lock(&sysfs_lock);
  764. dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
  765. "gpiochip%d", chip->base);
  766. if (!IS_ERR(dev)) {
  767. status = sysfs_create_group(&dev->kobj,
  768. &gpiochip_attr_group);
  769. } else
  770. status = PTR_ERR(dev);
  771. chip->exported = (status == 0);
  772. mutex_unlock(&sysfs_lock);
  773. if (status) {
  774. unsigned long flags;
  775. unsigned gpio;
  776. spin_lock_irqsave(&gpio_lock, flags);
  777. gpio = chip->base;
  778. while (gpio_desc[gpio].chip == chip)
  779. gpio_desc[gpio++].chip = NULL;
  780. spin_unlock_irqrestore(&gpio_lock, flags);
  781. pr_debug("%s: chip %s status %d\n", __func__,
  782. chip->label, status);
  783. }
  784. return status;
  785. }
  786. static void gpiochip_unexport(struct gpio_chip *chip)
  787. {
  788. int status;
  789. struct device *dev;
  790. mutex_lock(&sysfs_lock);
  791. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  792. if (dev) {
  793. put_device(dev);
  794. device_unregister(dev);
  795. chip->exported = 0;
  796. status = 0;
  797. } else
  798. status = -ENODEV;
  799. mutex_unlock(&sysfs_lock);
  800. if (status)
  801. pr_debug("%s: chip %s status %d\n", __func__,
  802. chip->label, status);
  803. }
  804. static int __init gpiolib_sysfs_init(void)
  805. {
  806. int status;
  807. unsigned long flags;
  808. unsigned gpio;
  809. idr_init(&pdesc_idr);
  810. status = class_register(&gpio_class);
  811. if (status < 0)
  812. return status;
  813. /* Scan and register the gpio_chips which registered very
  814. * early (e.g. before the class_register above was called).
  815. *
  816. * We run before arch_initcall() so chip->dev nodes can have
  817. * registered, and so arch_initcall() can always gpio_export().
  818. */
  819. spin_lock_irqsave(&gpio_lock, flags);
  820. for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
  821. struct gpio_chip *chip;
  822. chip = gpio_desc[gpio].chip;
  823. if (!chip || chip->exported)
  824. continue;
  825. spin_unlock_irqrestore(&gpio_lock, flags);
  826. status = gpiochip_export(chip);
  827. spin_lock_irqsave(&gpio_lock, flags);
  828. }
  829. spin_unlock_irqrestore(&gpio_lock, flags);
  830. return status;
  831. }
  832. postcore_initcall(gpiolib_sysfs_init);
  833. #else
  834. static inline int gpiochip_export(struct gpio_chip *chip)
  835. {
  836. return 0;
  837. }
  838. static inline void gpiochip_unexport(struct gpio_chip *chip)
  839. {
  840. }
  841. #endif /* CONFIG_GPIO_SYSFS */
  842. /**
  843. * gpiochip_add() - register a gpio_chip
  844. * @chip: the chip to register, with chip->base initialized
  845. * Context: potentially before irqs or kmalloc will work
  846. *
  847. * Returns a negative errno if the chip can't be registered, such as
  848. * because the chip->base is invalid or already associated with a
  849. * different chip. Otherwise it returns zero as a success code.
  850. *
  851. * When gpiochip_add() is called very early during boot, so that GPIOs
  852. * can be freely used, the chip->dev device must be registered before
  853. * the gpio framework's arch_initcall(). Otherwise sysfs initialization
  854. * for GPIOs will fail rudely.
  855. *
  856. * If chip->base is negative, this requests dynamic assignment of
  857. * a range of valid GPIOs.
  858. */
  859. int gpiochip_add(struct gpio_chip *chip)
  860. {
  861. unsigned long flags;
  862. int status = 0;
  863. unsigned id;
  864. int base = chip->base;
  865. if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
  866. && base >= 0) {
  867. status = -EINVAL;
  868. goto fail;
  869. }
  870. spin_lock_irqsave(&gpio_lock, flags);
  871. if (base < 0) {
  872. base = gpiochip_find_base(chip->ngpio);
  873. if (base < 0) {
  874. status = base;
  875. goto unlock;
  876. }
  877. chip->base = base;
  878. }
  879. /* these GPIO numbers must not be managed by another gpio_chip */
  880. for (id = base; id < base + chip->ngpio; id++) {
  881. if (gpio_desc[id].chip != NULL) {
  882. status = -EBUSY;
  883. break;
  884. }
  885. }
  886. if (status == 0) {
  887. for (id = base; id < base + chip->ngpio; id++) {
  888. gpio_desc[id].chip = chip;
  889. /* REVISIT: most hardware initializes GPIOs as
  890. * inputs (often with pullups enabled) so power
  891. * usage is minimized. Linux code should set the
  892. * gpio direction first thing; but until it does,
  893. * we may expose the wrong direction in sysfs.
  894. */
  895. gpio_desc[id].flags = !chip->direction_input
  896. ? (1 << FLAG_IS_OUT)
  897. : 0;
  898. }
  899. }
  900. unlock:
  901. spin_unlock_irqrestore(&gpio_lock, flags);
  902. if (status == 0)
  903. status = gpiochip_export(chip);
  904. fail:
  905. /* failures here can mean systems won't boot... */
  906. if (status)
  907. pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
  908. chip->base, chip->base + chip->ngpio - 1,
  909. chip->label ? : "generic");
  910. return status;
  911. }
  912. EXPORT_SYMBOL_GPL(gpiochip_add);
  913. /**
  914. * gpiochip_remove() - unregister a gpio_chip
  915. * @chip: the chip to unregister
  916. *
  917. * A gpio_chip with any GPIOs still requested may not be removed.
  918. */
  919. int gpiochip_remove(struct gpio_chip *chip)
  920. {
  921. unsigned long flags;
  922. int status = 0;
  923. unsigned id;
  924. spin_lock_irqsave(&gpio_lock, flags);
  925. for (id = chip->base; id < chip->base + chip->ngpio; id++) {
  926. if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
  927. status = -EBUSY;
  928. break;
  929. }
  930. }
  931. if (status == 0) {
  932. for (id = chip->base; id < chip->base + chip->ngpio; id++)
  933. gpio_desc[id].chip = NULL;
  934. }
  935. spin_unlock_irqrestore(&gpio_lock, flags);
  936. if (status == 0)
  937. gpiochip_unexport(chip);
  938. return status;
  939. }
  940. EXPORT_SYMBOL_GPL(gpiochip_remove);
  941. /* These "optional" allocation calls help prevent drivers from stomping
  942. * on each other, and help provide better diagnostics in debugfs.
  943. * They're called even less than the "set direction" calls.
  944. */
  945. int gpio_request(unsigned gpio, const char *label)
  946. {
  947. struct gpio_desc *desc;
  948. struct gpio_chip *chip;
  949. int status = -EINVAL;
  950. unsigned long flags;
  951. spin_lock_irqsave(&gpio_lock, flags);
  952. if (!gpio_is_valid(gpio))
  953. goto done;
  954. desc = &gpio_desc[gpio];
  955. chip = desc->chip;
  956. if (chip == NULL)
  957. goto done;
  958. if (!try_module_get(chip->owner))
  959. goto done;
  960. /* NOTE: gpio_request() can be called in early boot,
  961. * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
  962. */
  963. if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
  964. desc_set_label(desc, label ? : "?");
  965. status = 0;
  966. } else {
  967. status = -EBUSY;
  968. module_put(chip->owner);
  969. goto done;
  970. }
  971. if (chip->request) {
  972. /* chip->request may sleep */
  973. spin_unlock_irqrestore(&gpio_lock, flags);
  974. status = chip->request(chip, gpio - chip->base);
  975. spin_lock_irqsave(&gpio_lock, flags);
  976. if (status < 0) {
  977. desc_set_label(desc, NULL);
  978. module_put(chip->owner);
  979. clear_bit(FLAG_REQUESTED, &desc->flags);
  980. }
  981. }
  982. done:
  983. if (status)
  984. pr_debug("gpio_request: gpio-%d (%s) status %d\n",
  985. gpio, label ? : "?", status);
  986. spin_unlock_irqrestore(&gpio_lock, flags);
  987. return status;
  988. }
  989. EXPORT_SYMBOL_GPL(gpio_request);
  990. void gpio_free(unsigned gpio)
  991. {
  992. unsigned long flags;
  993. struct gpio_desc *desc;
  994. struct gpio_chip *chip;
  995. might_sleep();
  996. if (!gpio_is_valid(gpio)) {
  997. WARN_ON(extra_checks);
  998. return;
  999. }
  1000. gpio_unexport(gpio);
  1001. spin_lock_irqsave(&gpio_lock, flags);
  1002. desc = &gpio_desc[gpio];
  1003. chip = desc->chip;
  1004. if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
  1005. if (chip->free) {
  1006. spin_unlock_irqrestore(&gpio_lock, flags);
  1007. might_sleep_if(extra_checks && chip->can_sleep);
  1008. chip->free(chip, gpio - chip->base);
  1009. spin_lock_irqsave(&gpio_lock, flags);
  1010. }
  1011. desc_set_label(desc, NULL);
  1012. module_put(desc->chip->owner);
  1013. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  1014. clear_bit(FLAG_REQUESTED, &desc->flags);
  1015. } else
  1016. WARN_ON(extra_checks);
  1017. spin_unlock_irqrestore(&gpio_lock, flags);
  1018. }
  1019. EXPORT_SYMBOL_GPL(gpio_free);
  1020. /**
  1021. * gpiochip_is_requested - return string iff signal was requested
  1022. * @chip: controller managing the signal
  1023. * @offset: of signal within controller's 0..(ngpio - 1) range
  1024. *
  1025. * Returns NULL if the GPIO is not currently requested, else a string.
  1026. * If debugfs support is enabled, the string returned is the label passed
  1027. * to gpio_request(); otherwise it is a meaningless constant.
  1028. *
  1029. * This function is for use by GPIO controller drivers. The label can
  1030. * help with diagnostics, and knowing that the signal is used as a GPIO
  1031. * can help avoid accidentally multiplexing it to another controller.
  1032. */
  1033. const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
  1034. {
  1035. unsigned gpio = chip->base + offset;
  1036. if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
  1037. return NULL;
  1038. if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
  1039. return NULL;
  1040. #ifdef CONFIG_DEBUG_FS
  1041. return gpio_desc[gpio].label;
  1042. #else
  1043. return "?";
  1044. #endif
  1045. }
  1046. EXPORT_SYMBOL_GPL(gpiochip_is_requested);
  1047. /* Drivers MUST set GPIO direction before making get/set calls. In
  1048. * some cases this is done in early boot, before IRQs are enabled.
  1049. *
  1050. * As a rule these aren't called more than once (except for drivers
  1051. * using the open-drain emulation idiom) so these are natural places
  1052. * to accumulate extra debugging checks. Note that we can't (yet)
  1053. * rely on gpio_request() having been called beforehand.
  1054. */
  1055. int gpio_direction_input(unsigned gpio)
  1056. {
  1057. unsigned long flags;
  1058. struct gpio_chip *chip;
  1059. struct gpio_desc *desc = &gpio_desc[gpio];
  1060. int status = -EINVAL;
  1061. spin_lock_irqsave(&gpio_lock, flags);
  1062. if (!gpio_is_valid(gpio))
  1063. goto fail;
  1064. chip = desc->chip;
  1065. if (!chip || !chip->get || !chip->direction_input)
  1066. goto fail;
  1067. gpio -= chip->base;
  1068. if (gpio >= chip->ngpio)
  1069. goto fail;
  1070. status = gpio_ensure_requested(desc, gpio);
  1071. if (status < 0)
  1072. goto fail;
  1073. /* now we know the gpio is valid and chip won't vanish */
  1074. spin_unlock_irqrestore(&gpio_lock, flags);
  1075. might_sleep_if(extra_checks && chip->can_sleep);
  1076. if (status) {
  1077. status = chip->request(chip, gpio);
  1078. if (status < 0) {
  1079. pr_debug("GPIO-%d: chip request fail, %d\n",
  1080. chip->base + gpio, status);
  1081. /* and it's not available to anyone else ...
  1082. * gpio_request() is the fully clean solution.
  1083. */
  1084. goto lose;
  1085. }
  1086. }
  1087. status = chip->direction_input(chip, gpio);
  1088. if (status == 0)
  1089. clear_bit(FLAG_IS_OUT, &desc->flags);
  1090. lose:
  1091. return status;
  1092. fail:
  1093. spin_unlock_irqrestore(&gpio_lock, flags);
  1094. if (status)
  1095. pr_debug("%s: gpio-%d status %d\n",
  1096. __func__, gpio, status);
  1097. return status;
  1098. }
  1099. EXPORT_SYMBOL_GPL(gpio_direction_input);
  1100. int gpio_direction_output(unsigned gpio, int value)
  1101. {
  1102. unsigned long flags;
  1103. struct gpio_chip *chip;
  1104. struct gpio_desc *desc = &gpio_desc[gpio];
  1105. int status = -EINVAL;
  1106. spin_lock_irqsave(&gpio_lock, flags);
  1107. if (!gpio_is_valid(gpio))
  1108. goto fail;
  1109. chip = desc->chip;
  1110. if (!chip || !chip->set || !chip->direction_output)
  1111. goto fail;
  1112. gpio -= chip->base;
  1113. if (gpio >= chip->ngpio)
  1114. goto fail;
  1115. status = gpio_ensure_requested(desc, gpio);
  1116. if (status < 0)
  1117. goto fail;
  1118. /* now we know the gpio is valid and chip won't vanish */
  1119. spin_unlock_irqrestore(&gpio_lock, flags);
  1120. might_sleep_if(extra_checks && chip->can_sleep);
  1121. if (status) {
  1122. status = chip->request(chip, gpio);
  1123. if (status < 0) {
  1124. pr_debug("GPIO-%d: chip request fail, %d\n",
  1125. chip->base + gpio, status);
  1126. /* and it's not available to anyone else ...
  1127. * gpio_request() is the fully clean solution.
  1128. */
  1129. goto lose;
  1130. }
  1131. }
  1132. status = chip->direction_output(chip, gpio, value);
  1133. if (status == 0)
  1134. set_bit(FLAG_IS_OUT, &desc->flags);
  1135. lose:
  1136. return status;
  1137. fail:
  1138. spin_unlock_irqrestore(&gpio_lock, flags);
  1139. if (status)
  1140. pr_debug("%s: gpio-%d status %d\n",
  1141. __func__, gpio, status);
  1142. return status;
  1143. }
  1144. EXPORT_SYMBOL_GPL(gpio_direction_output);
  1145. /* I/O calls are only valid after configuration completed; the relevant
  1146. * "is this a valid GPIO" error checks should already have been done.
  1147. *
  1148. * "Get" operations are often inlinable as reading a pin value register,
  1149. * and masking the relevant bit in that register.
  1150. *
  1151. * When "set" operations are inlinable, they involve writing that mask to
  1152. * one register to set a low value, or a different register to set it high.
  1153. * Otherwise locking is needed, so there may be little value to inlining.
  1154. *
  1155. *------------------------------------------------------------------------
  1156. *
  1157. * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
  1158. * have requested the GPIO. That can include implicit requesting by
  1159. * a direction setting call. Marking a gpio as requested locks its chip
  1160. * in memory, guaranteeing that these table lookups need no more locking
  1161. * and that gpiochip_remove() will fail.
  1162. *
  1163. * REVISIT when debugging, consider adding some instrumentation to ensure
  1164. * that the GPIO was actually requested.
  1165. */
  1166. /**
  1167. * __gpio_get_value() - return a gpio's value
  1168. * @gpio: gpio whose value will be returned
  1169. * Context: any
  1170. *
  1171. * This is used directly or indirectly to implement gpio_get_value().
  1172. * It returns the zero or nonzero value provided by the associated
  1173. * gpio_chip.get() method; or zero if no such method is provided.
  1174. */
  1175. int __gpio_get_value(unsigned gpio)
  1176. {
  1177. struct gpio_chip *chip;
  1178. chip = gpio_to_chip(gpio);
  1179. WARN_ON(extra_checks && chip->can_sleep);
  1180. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1181. }
  1182. EXPORT_SYMBOL_GPL(__gpio_get_value);
  1183. /**
  1184. * __gpio_set_value() - assign a gpio's value
  1185. * @gpio: gpio whose value will be assigned
  1186. * @value: value to assign
  1187. * Context: any
  1188. *
  1189. * This is used directly or indirectly to implement gpio_set_value().
  1190. * It invokes the associated gpio_chip.set() method.
  1191. */
  1192. void __gpio_set_value(unsigned gpio, int value)
  1193. {
  1194. struct gpio_chip *chip;
  1195. chip = gpio_to_chip(gpio);
  1196. WARN_ON(extra_checks && chip->can_sleep);
  1197. chip->set(chip, gpio - chip->base, value);
  1198. }
  1199. EXPORT_SYMBOL_GPL(__gpio_set_value);
  1200. /**
  1201. * __gpio_cansleep() - report whether gpio value access will sleep
  1202. * @gpio: gpio in question
  1203. * Context: any
  1204. *
  1205. * This is used directly or indirectly to implement gpio_cansleep(). It
  1206. * returns nonzero if access reading or writing the GPIO value can sleep.
  1207. */
  1208. int __gpio_cansleep(unsigned gpio)
  1209. {
  1210. struct gpio_chip *chip;
  1211. /* only call this on GPIOs that are valid! */
  1212. chip = gpio_to_chip(gpio);
  1213. return chip->can_sleep;
  1214. }
  1215. EXPORT_SYMBOL_GPL(__gpio_cansleep);
  1216. /**
  1217. * __gpio_to_irq() - return the IRQ corresponding to a GPIO
  1218. * @gpio: gpio whose IRQ will be returned (already requested)
  1219. * Context: any
  1220. *
  1221. * This is used directly or indirectly to implement gpio_to_irq().
  1222. * It returns the number of the IRQ signaled by this (input) GPIO,
  1223. * or a negative errno.
  1224. */
  1225. int __gpio_to_irq(unsigned gpio)
  1226. {
  1227. struct gpio_chip *chip;
  1228. chip = gpio_to_chip(gpio);
  1229. return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
  1230. }
  1231. EXPORT_SYMBOL_GPL(__gpio_to_irq);
  1232. /* There's no value in making it easy to inline GPIO calls that may sleep.
  1233. * Common examples include ones connected to I2C or SPI chips.
  1234. */
  1235. int gpio_get_value_cansleep(unsigned gpio)
  1236. {
  1237. struct gpio_chip *chip;
  1238. might_sleep_if(extra_checks);
  1239. chip = gpio_to_chip(gpio);
  1240. return chip->get ? chip->get(chip, gpio - chip->base) : 0;
  1241. }
  1242. EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
  1243. void gpio_set_value_cansleep(unsigned gpio, int value)
  1244. {
  1245. struct gpio_chip *chip;
  1246. might_sleep_if(extra_checks);
  1247. chip = gpio_to_chip(gpio);
  1248. chip->set(chip, gpio - chip->base, value);
  1249. }
  1250. EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
  1251. #ifdef CONFIG_DEBUG_FS
  1252. static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1253. {
  1254. unsigned i;
  1255. unsigned gpio = chip->base;
  1256. struct gpio_desc *gdesc = &gpio_desc[gpio];
  1257. int is_out;
  1258. for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
  1259. if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
  1260. continue;
  1261. is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
  1262. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
  1263. gpio, gdesc->label,
  1264. is_out ? "out" : "in ",
  1265. chip->get
  1266. ? (chip->get(chip, i) ? "hi" : "lo")
  1267. : "? ");
  1268. if (!is_out) {
  1269. int irq = gpio_to_irq(gpio);
  1270. struct irq_desc *desc = irq_to_desc(irq);
  1271. /* This races with request_irq(), set_irq_type(),
  1272. * and set_irq_wake() ... but those are "rare".
  1273. *
  1274. * More significantly, trigger type flags aren't
  1275. * currently maintained by genirq.
  1276. */
  1277. if (irq >= 0 && desc->action) {
  1278. char *trigger;
  1279. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  1280. case IRQ_TYPE_NONE:
  1281. trigger = "(default)";
  1282. break;
  1283. case IRQ_TYPE_EDGE_FALLING:
  1284. trigger = "edge-falling";
  1285. break;
  1286. case IRQ_TYPE_EDGE_RISING:
  1287. trigger = "edge-rising";
  1288. break;
  1289. case IRQ_TYPE_EDGE_BOTH:
  1290. trigger = "edge-both";
  1291. break;
  1292. case IRQ_TYPE_LEVEL_HIGH:
  1293. trigger = "level-high";
  1294. break;
  1295. case IRQ_TYPE_LEVEL_LOW:
  1296. trigger = "level-low";
  1297. break;
  1298. default:
  1299. trigger = "?trigger?";
  1300. break;
  1301. }
  1302. seq_printf(s, " irq-%d %s%s",
  1303. irq, trigger,
  1304. (desc->status & IRQ_WAKEUP)
  1305. ? " wakeup" : "");
  1306. }
  1307. }
  1308. seq_printf(s, "\n");
  1309. }
  1310. }
  1311. static int gpiolib_show(struct seq_file *s, void *unused)
  1312. {
  1313. struct gpio_chip *chip = NULL;
  1314. unsigned gpio;
  1315. int started = 0;
  1316. /* REVISIT this isn't locked against gpio_chip removal ... */
  1317. for (gpio = 0; gpio_is_valid(gpio); gpio++) {
  1318. struct device *dev;
  1319. if (chip == gpio_desc[gpio].chip)
  1320. continue;
  1321. chip = gpio_desc[gpio].chip;
  1322. if (!chip)
  1323. continue;
  1324. seq_printf(s, "%sGPIOs %d-%d",
  1325. started ? "\n" : "",
  1326. chip->base, chip->base + chip->ngpio - 1);
  1327. dev = chip->dev;
  1328. if (dev)
  1329. seq_printf(s, ", %s/%s",
  1330. dev->bus ? dev->bus->name : "no-bus",
  1331. dev_name(dev));
  1332. if (chip->label)
  1333. seq_printf(s, ", %s", chip->label);
  1334. if (chip->can_sleep)
  1335. seq_printf(s, ", can sleep");
  1336. seq_printf(s, ":\n");
  1337. started = 1;
  1338. if (chip->dbg_show)
  1339. chip->dbg_show(s, chip);
  1340. else
  1341. gpiolib_dbg_show(s, chip);
  1342. }
  1343. return 0;
  1344. }
  1345. static int gpiolib_open(struct inode *inode, struct file *file)
  1346. {
  1347. return single_open(file, gpiolib_show, NULL);
  1348. }
  1349. static const struct file_operations gpiolib_operations = {
  1350. .open = gpiolib_open,
  1351. .read = seq_read,
  1352. .llseek = seq_lseek,
  1353. .release = single_release,
  1354. };
  1355. static int __init gpiolib_debugfs_init(void)
  1356. {
  1357. /* /sys/kernel/debug/gpio */
  1358. (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
  1359. NULL, NULL, &gpiolib_operations);
  1360. return 0;
  1361. }
  1362. subsys_initcall(gpiolib_debugfs_init);
  1363. #endif /* DEBUG_FS */