gpiolib-sysfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. #include <linux/idr.h>
  2. #include <linux/mutex.h>
  3. #include <linux/device.h>
  4. #include <linux/sysfs.h>
  5. #include <linux/gpio/consumer.h>
  6. #include <linux/gpio/driver.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/kdev_t.h>
  9. #include "gpiolib.h"
  10. static DEFINE_IDR(dirent_idr);
  11. /* lock protects against unexport_gpio() being called while
  12. * sysfs files are active.
  13. */
  14. static DEFINE_MUTEX(sysfs_lock);
  15. /*
  16. * /sys/class/gpio/gpioN... only for GPIOs that are exported
  17. * /direction
  18. * * MAY BE OMITTED if kernel won't allow direction changes
  19. * * is read/write as "in" or "out"
  20. * * may also be written as "high" or "low", initializing
  21. * output value as specified ("out" implies "low")
  22. * /value
  23. * * always readable, subject to hardware behavior
  24. * * may be writable, as zero/nonzero
  25. * /edge
  26. * * configures behavior of poll(2) on /value
  27. * * available only if pin can generate IRQs on input
  28. * * is read/write as "none", "falling", "rising", or "both"
  29. * /active_low
  30. * * configures polarity of /value
  31. * * is read/write as zero/nonzero
  32. * * also affects existing and subsequent "falling" and "rising"
  33. * /edge configuration
  34. */
  35. static ssize_t gpio_direction_show(struct device *dev,
  36. struct device_attribute *attr, char *buf)
  37. {
  38. struct gpio_desc *desc = dev_get_drvdata(dev);
  39. ssize_t status;
  40. mutex_lock(&sysfs_lock);
  41. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  42. status = -EIO;
  43. } else {
  44. gpiod_get_direction(desc);
  45. status = sprintf(buf, "%s\n",
  46. test_bit(FLAG_IS_OUT, &desc->flags)
  47. ? "out" : "in");
  48. }
  49. mutex_unlock(&sysfs_lock);
  50. return status;
  51. }
  52. static ssize_t gpio_direction_store(struct device *dev,
  53. struct device_attribute *attr, const char *buf, size_t size)
  54. {
  55. struct gpio_desc *desc = dev_get_drvdata(dev);
  56. ssize_t status;
  57. mutex_lock(&sysfs_lock);
  58. if (!test_bit(FLAG_EXPORT, &desc->flags))
  59. status = -EIO;
  60. else if (sysfs_streq(buf, "high"))
  61. status = gpiod_direction_output_raw(desc, 1);
  62. else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
  63. status = gpiod_direction_output_raw(desc, 0);
  64. else if (sysfs_streq(buf, "in"))
  65. status = gpiod_direction_input(desc);
  66. else
  67. status = -EINVAL;
  68. mutex_unlock(&sysfs_lock);
  69. return status ? : size;
  70. }
  71. static /* const */ DEVICE_ATTR(direction, 0644,
  72. gpio_direction_show, gpio_direction_store);
  73. static ssize_t gpio_value_show(struct device *dev,
  74. struct device_attribute *attr, char *buf)
  75. {
  76. struct gpio_desc *desc = dev_get_drvdata(dev);
  77. ssize_t status;
  78. mutex_lock(&sysfs_lock);
  79. if (!test_bit(FLAG_EXPORT, &desc->flags))
  80. status = -EIO;
  81. else
  82. status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
  83. mutex_unlock(&sysfs_lock);
  84. return status;
  85. }
  86. static ssize_t gpio_value_store(struct device *dev,
  87. struct device_attribute *attr, const char *buf, size_t size)
  88. {
  89. struct gpio_desc *desc = dev_get_drvdata(dev);
  90. ssize_t status;
  91. mutex_lock(&sysfs_lock);
  92. if (!test_bit(FLAG_EXPORT, &desc->flags))
  93. status = -EIO;
  94. else if (!test_bit(FLAG_IS_OUT, &desc->flags))
  95. status = -EPERM;
  96. else {
  97. long value;
  98. status = kstrtol(buf, 0, &value);
  99. if (status == 0) {
  100. gpiod_set_value_cansleep(desc, value);
  101. status = size;
  102. }
  103. }
  104. mutex_unlock(&sysfs_lock);
  105. return status;
  106. }
  107. static DEVICE_ATTR(value, 0644,
  108. gpio_value_show, gpio_value_store);
  109. static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
  110. {
  111. struct kernfs_node *value_sd = priv;
  112. sysfs_notify_dirent(value_sd);
  113. return IRQ_HANDLED;
  114. }
  115. static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
  116. unsigned long gpio_flags)
  117. {
  118. struct kernfs_node *value_sd;
  119. unsigned long irq_flags;
  120. int ret, irq, id;
  121. if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
  122. return 0;
  123. irq = gpiod_to_irq(desc);
  124. if (irq < 0)
  125. return -EIO;
  126. id = desc->flags >> ID_SHIFT;
  127. value_sd = idr_find(&dirent_idr, id);
  128. if (value_sd)
  129. free_irq(irq, value_sd);
  130. desc->flags &= ~GPIO_TRIGGER_MASK;
  131. if (!gpio_flags) {
  132. gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
  133. ret = 0;
  134. goto free_id;
  135. }
  136. irq_flags = IRQF_SHARED;
  137. if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
  138. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  139. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  140. if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
  141. irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  142. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  143. if (!value_sd) {
  144. value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
  145. if (!value_sd) {
  146. ret = -ENODEV;
  147. goto err_out;
  148. }
  149. ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
  150. if (ret < 0)
  151. goto free_sd;
  152. id = ret;
  153. desc->flags &= GPIO_FLAGS_MASK;
  154. desc->flags |= (unsigned long)id << ID_SHIFT;
  155. if (desc->flags >> ID_SHIFT != id) {
  156. ret = -ERANGE;
  157. goto free_id;
  158. }
  159. }
  160. /*
  161. * FIXME: This should be done in the irq_request_resources callback
  162. * when the irq is requested, but a few drivers currently fail
  163. * to do so.
  164. *
  165. * Remove this redundant call (along with the corresponding
  166. * unlock) when those drivers have been fixed.
  167. */
  168. ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
  169. if (ret < 0)
  170. goto free_id;
  171. ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
  172. "gpiolib", value_sd);
  173. if (ret < 0)
  174. goto err_unlock;
  175. desc->flags |= gpio_flags;
  176. return 0;
  177. err_unlock:
  178. gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
  179. free_id:
  180. idr_remove(&dirent_idr, id);
  181. desc->flags &= GPIO_FLAGS_MASK;
  182. free_sd:
  183. if (value_sd)
  184. sysfs_put(value_sd);
  185. err_out:
  186. return ret;
  187. }
  188. static const struct {
  189. const char *name;
  190. unsigned long flags;
  191. } trigger_types[] = {
  192. { "none", 0 },
  193. { "falling", BIT(FLAG_TRIG_FALL) },
  194. { "rising", BIT(FLAG_TRIG_RISE) },
  195. { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
  196. };
  197. static ssize_t gpio_edge_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. const struct gpio_desc *desc = dev_get_drvdata(dev);
  201. ssize_t status;
  202. mutex_lock(&sysfs_lock);
  203. if (!test_bit(FLAG_EXPORT, &desc->flags))
  204. status = -EIO;
  205. else {
  206. int i;
  207. status = 0;
  208. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  209. if ((desc->flags & GPIO_TRIGGER_MASK)
  210. == trigger_types[i].flags) {
  211. status = sprintf(buf, "%s\n",
  212. trigger_types[i].name);
  213. break;
  214. }
  215. }
  216. mutex_unlock(&sysfs_lock);
  217. return status;
  218. }
  219. static ssize_t gpio_edge_store(struct device *dev,
  220. struct device_attribute *attr, const char *buf, size_t size)
  221. {
  222. struct gpio_desc *desc = dev_get_drvdata(dev);
  223. ssize_t status;
  224. int i;
  225. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  226. if (sysfs_streq(trigger_types[i].name, buf))
  227. goto found;
  228. return -EINVAL;
  229. found:
  230. mutex_lock(&sysfs_lock);
  231. if (!test_bit(FLAG_EXPORT, &desc->flags))
  232. status = -EIO;
  233. else {
  234. status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
  235. if (!status)
  236. status = size;
  237. }
  238. mutex_unlock(&sysfs_lock);
  239. return status;
  240. }
  241. static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
  242. static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
  243. int value)
  244. {
  245. int status = 0;
  246. if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
  247. return 0;
  248. if (value)
  249. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  250. else
  251. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  252. /* reconfigure poll(2) support if enabled on one edge only */
  253. if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
  254. !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
  255. unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
  256. gpio_setup_irq(desc, dev, 0);
  257. status = gpio_setup_irq(desc, dev, trigger_flags);
  258. }
  259. return status;
  260. }
  261. static ssize_t gpio_active_low_show(struct device *dev,
  262. struct device_attribute *attr, char *buf)
  263. {
  264. const struct gpio_desc *desc = dev_get_drvdata(dev);
  265. ssize_t status;
  266. mutex_lock(&sysfs_lock);
  267. if (!test_bit(FLAG_EXPORT, &desc->flags))
  268. status = -EIO;
  269. else
  270. status = sprintf(buf, "%d\n",
  271. !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
  272. mutex_unlock(&sysfs_lock);
  273. return status;
  274. }
  275. static ssize_t gpio_active_low_store(struct device *dev,
  276. struct device_attribute *attr, const char *buf, size_t size)
  277. {
  278. struct gpio_desc *desc = dev_get_drvdata(dev);
  279. ssize_t status;
  280. mutex_lock(&sysfs_lock);
  281. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  282. status = -EIO;
  283. } else {
  284. long value;
  285. status = kstrtol(buf, 0, &value);
  286. if (status == 0)
  287. status = sysfs_set_active_low(desc, dev, value != 0);
  288. }
  289. mutex_unlock(&sysfs_lock);
  290. return status ? : size;
  291. }
  292. static DEVICE_ATTR(active_low, 0644,
  293. gpio_active_low_show, gpio_active_low_store);
  294. static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
  295. int n)
  296. {
  297. struct device *dev = container_of(kobj, struct device, kobj);
  298. struct gpio_desc *desc = dev_get_drvdata(dev);
  299. umode_t mode = attr->mode;
  300. bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
  301. if (attr == &dev_attr_direction.attr) {
  302. if (!show_direction)
  303. mode = 0;
  304. } else if (attr == &dev_attr_edge.attr) {
  305. if (gpiod_to_irq(desc) < 0)
  306. mode = 0;
  307. if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
  308. mode = 0;
  309. }
  310. return mode;
  311. }
  312. static struct attribute *gpio_attrs[] = {
  313. &dev_attr_direction.attr,
  314. &dev_attr_edge.attr,
  315. &dev_attr_value.attr,
  316. &dev_attr_active_low.attr,
  317. NULL,
  318. };
  319. static const struct attribute_group gpio_group = {
  320. .attrs = gpio_attrs,
  321. .is_visible = gpio_is_visible,
  322. };
  323. static const struct attribute_group *gpio_groups[] = {
  324. &gpio_group,
  325. NULL
  326. };
  327. /*
  328. * /sys/class/gpio/gpiochipN/
  329. * /base ... matching gpio_chip.base (N)
  330. * /label ... matching gpio_chip.label
  331. * /ngpio ... matching gpio_chip.ngpio
  332. */
  333. static ssize_t chip_base_show(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. const struct gpio_chip *chip = dev_get_drvdata(dev);
  337. return sprintf(buf, "%d\n", chip->base);
  338. }
  339. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  340. static ssize_t chip_label_show(struct device *dev,
  341. struct device_attribute *attr, char *buf)
  342. {
  343. const struct gpio_chip *chip = dev_get_drvdata(dev);
  344. return sprintf(buf, "%s\n", chip->label ? : "");
  345. }
  346. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  347. static ssize_t chip_ngpio_show(struct device *dev,
  348. struct device_attribute *attr, char *buf)
  349. {
  350. const struct gpio_chip *chip = dev_get_drvdata(dev);
  351. return sprintf(buf, "%u\n", chip->ngpio);
  352. }
  353. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  354. static struct attribute *gpiochip_attrs[] = {
  355. &dev_attr_base.attr,
  356. &dev_attr_label.attr,
  357. &dev_attr_ngpio.attr,
  358. NULL,
  359. };
  360. ATTRIBUTE_GROUPS(gpiochip);
  361. /*
  362. * /sys/class/gpio/export ... write-only
  363. * integer N ... number of GPIO to export (full access)
  364. * /sys/class/gpio/unexport ... write-only
  365. * integer N ... number of GPIO to unexport
  366. */
  367. static ssize_t export_store(struct class *class,
  368. struct class_attribute *attr,
  369. const char *buf, size_t len)
  370. {
  371. long gpio;
  372. struct gpio_desc *desc;
  373. int status;
  374. status = kstrtol(buf, 0, &gpio);
  375. if (status < 0)
  376. goto done;
  377. desc = gpio_to_desc(gpio);
  378. /* reject invalid GPIOs */
  379. if (!desc) {
  380. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  381. return -EINVAL;
  382. }
  383. /* No extra locking here; FLAG_SYSFS just signifies that the
  384. * request and export were done by on behalf of userspace, so
  385. * they may be undone on its behalf too.
  386. */
  387. status = gpiod_request(desc, "sysfs");
  388. if (status < 0) {
  389. if (status == -EPROBE_DEFER)
  390. status = -ENODEV;
  391. goto done;
  392. }
  393. status = gpiod_export(desc, true);
  394. if (status < 0)
  395. gpiod_free(desc);
  396. else
  397. set_bit(FLAG_SYSFS, &desc->flags);
  398. done:
  399. if (status)
  400. pr_debug("%s: status %d\n", __func__, status);
  401. return status ? : len;
  402. }
  403. static ssize_t unexport_store(struct class *class,
  404. struct class_attribute *attr,
  405. const char *buf, size_t len)
  406. {
  407. long gpio;
  408. struct gpio_desc *desc;
  409. int status;
  410. status = kstrtol(buf, 0, &gpio);
  411. if (status < 0)
  412. goto done;
  413. desc = gpio_to_desc(gpio);
  414. /* reject bogus commands (gpio_unexport ignores them) */
  415. if (!desc) {
  416. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  417. return -EINVAL;
  418. }
  419. status = -EINVAL;
  420. /* No extra locking here; FLAG_SYSFS just signifies that the
  421. * request and export were done by on behalf of userspace, so
  422. * they may be undone on its behalf too.
  423. */
  424. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
  425. status = 0;
  426. gpiod_free(desc);
  427. }
  428. done:
  429. if (status)
  430. pr_debug("%s: status %d\n", __func__, status);
  431. return status ? : len;
  432. }
  433. static struct class_attribute gpio_class_attrs[] = {
  434. __ATTR(export, 0200, NULL, export_store),
  435. __ATTR(unexport, 0200, NULL, unexport_store),
  436. __ATTR_NULL,
  437. };
  438. static struct class gpio_class = {
  439. .name = "gpio",
  440. .owner = THIS_MODULE,
  441. .class_attrs = gpio_class_attrs,
  442. };
  443. /**
  444. * gpiod_export - export a GPIO through sysfs
  445. * @gpio: gpio to make available, already requested
  446. * @direction_may_change: true if userspace may change gpio direction
  447. * Context: arch_initcall or later
  448. *
  449. * When drivers want to make a GPIO accessible to userspace after they
  450. * have requested it -- perhaps while debugging, or as part of their
  451. * public interface -- they may use this routine. If the GPIO can
  452. * change direction (some can't) and the caller allows it, userspace
  453. * will see "direction" sysfs attribute which may be used to change
  454. * the gpio's direction. A "value" attribute will always be provided.
  455. *
  456. * Returns zero on success, else an error.
  457. */
  458. int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  459. {
  460. struct gpio_chip *chip;
  461. unsigned long flags;
  462. int status;
  463. const char *ioname = NULL;
  464. struct device *dev;
  465. int offset;
  466. /* can't export until sysfs is available ... */
  467. if (!gpio_class.p) {
  468. pr_debug("%s: called too early!\n", __func__);
  469. return -ENOENT;
  470. }
  471. if (!desc) {
  472. pr_debug("%s: invalid gpio descriptor\n", __func__);
  473. return -EINVAL;
  474. }
  475. chip = desc->chip;
  476. mutex_lock(&sysfs_lock);
  477. /* check if chip is being removed */
  478. if (!chip || !chip->exported) {
  479. status = -ENODEV;
  480. goto fail_unlock;
  481. }
  482. spin_lock_irqsave(&gpio_lock, flags);
  483. if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
  484. test_bit(FLAG_EXPORT, &desc->flags)) {
  485. spin_unlock_irqrestore(&gpio_lock, flags);
  486. gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
  487. __func__,
  488. test_bit(FLAG_REQUESTED, &desc->flags),
  489. test_bit(FLAG_EXPORT, &desc->flags));
  490. status = -EPERM;
  491. goto fail_unlock;
  492. }
  493. if (chip->direction_input && chip->direction_output &&
  494. direction_may_change) {
  495. set_bit(FLAG_SYSFS_DIR, &desc->flags);
  496. }
  497. spin_unlock_irqrestore(&gpio_lock, flags);
  498. offset = gpio_chip_hwgpio(desc);
  499. if (chip->names && chip->names[offset])
  500. ioname = chip->names[offset];
  501. dev = device_create_with_groups(&gpio_class, chip->dev,
  502. MKDEV(0, 0), desc, gpio_groups,
  503. ioname ? ioname : "gpio%u",
  504. desc_to_gpio(desc));
  505. if (IS_ERR(dev)) {
  506. status = PTR_ERR(dev);
  507. goto fail_unlock;
  508. }
  509. set_bit(FLAG_EXPORT, &desc->flags);
  510. mutex_unlock(&sysfs_lock);
  511. return 0;
  512. fail_unlock:
  513. mutex_unlock(&sysfs_lock);
  514. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  515. return status;
  516. }
  517. EXPORT_SYMBOL_GPL(gpiod_export);
  518. static int match_export(struct device *dev, const void *data)
  519. {
  520. return dev_get_drvdata(dev) == data;
  521. }
  522. /**
  523. * gpiod_export_link - create a sysfs link to an exported GPIO node
  524. * @dev: device under which to create symlink
  525. * @name: name of the symlink
  526. * @gpio: gpio to create symlink to, already exported
  527. *
  528. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  529. * node. Caller is responsible for unlinking.
  530. *
  531. * Returns zero on success, else an error.
  532. */
  533. int gpiod_export_link(struct device *dev, const char *name,
  534. struct gpio_desc *desc)
  535. {
  536. int status = -EINVAL;
  537. if (!desc) {
  538. pr_warn("%s: invalid GPIO\n", __func__);
  539. return -EINVAL;
  540. }
  541. mutex_lock(&sysfs_lock);
  542. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  543. struct device *tdev;
  544. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  545. if (tdev != NULL) {
  546. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  547. name);
  548. put_device(tdev);
  549. } else {
  550. status = -ENODEV;
  551. }
  552. }
  553. mutex_unlock(&sysfs_lock);
  554. if (status)
  555. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  556. return status;
  557. }
  558. EXPORT_SYMBOL_GPL(gpiod_export_link);
  559. /**
  560. * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
  561. * @gpio: gpio to change
  562. * @value: non-zero to use active low, i.e. inverted values
  563. *
  564. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  565. * The GPIO does not have to be exported yet. If poll(2) support has
  566. * been enabled for either rising or falling edge, it will be
  567. * reconfigured to follow the new polarity.
  568. *
  569. * Returns zero on success, else an error.
  570. */
  571. int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  572. {
  573. struct device *dev = NULL;
  574. int status = -EINVAL;
  575. if (!desc) {
  576. pr_warn("%s: invalid GPIO\n", __func__);
  577. return -EINVAL;
  578. }
  579. mutex_lock(&sysfs_lock);
  580. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  581. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  582. if (dev == NULL) {
  583. status = -ENODEV;
  584. goto unlock;
  585. }
  586. }
  587. status = sysfs_set_active_low(desc, dev, value);
  588. put_device(dev);
  589. unlock:
  590. mutex_unlock(&sysfs_lock);
  591. if (status)
  592. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  593. return status;
  594. }
  595. EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
  596. /**
  597. * gpiod_unexport - reverse effect of gpio_export()
  598. * @gpio: gpio to make unavailable
  599. *
  600. * This is implicit on gpio_free().
  601. */
  602. void gpiod_unexport(struct gpio_desc *desc)
  603. {
  604. int status = 0;
  605. struct device *dev = NULL;
  606. if (!desc) {
  607. pr_warn("%s: invalid GPIO\n", __func__);
  608. return;
  609. }
  610. mutex_lock(&sysfs_lock);
  611. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  612. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  613. if (dev) {
  614. gpio_setup_irq(desc, dev, 0);
  615. clear_bit(FLAG_SYSFS_DIR, &desc->flags);
  616. clear_bit(FLAG_EXPORT, &desc->flags);
  617. } else
  618. status = -ENODEV;
  619. }
  620. mutex_unlock(&sysfs_lock);
  621. if (dev) {
  622. device_unregister(dev);
  623. put_device(dev);
  624. }
  625. if (status)
  626. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  627. }
  628. EXPORT_SYMBOL_GPL(gpiod_unexport);
  629. int gpiochip_export(struct gpio_chip *chip)
  630. {
  631. int status;
  632. struct device *dev;
  633. /* Many systems register gpio chips for SOC support very early,
  634. * before driver model support is available. In those cases we
  635. * export this later, in gpiolib_sysfs_init() ... here we just
  636. * verify that _some_ field of gpio_class got initialized.
  637. */
  638. if (!gpio_class.p)
  639. return 0;
  640. /* use chip->base for the ID; it's already known to be unique */
  641. mutex_lock(&sysfs_lock);
  642. dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
  643. chip, gpiochip_groups,
  644. "gpiochip%d", chip->base);
  645. if (IS_ERR(dev))
  646. status = PTR_ERR(dev);
  647. else
  648. status = 0;
  649. chip->exported = (status == 0);
  650. mutex_unlock(&sysfs_lock);
  651. if (status)
  652. chip_dbg(chip, "%s: status %d\n", __func__, status);
  653. return status;
  654. }
  655. void gpiochip_unexport(struct gpio_chip *chip)
  656. {
  657. int status;
  658. struct device *dev;
  659. struct gpio_desc *desc;
  660. unsigned int i;
  661. mutex_lock(&sysfs_lock);
  662. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  663. if (dev) {
  664. put_device(dev);
  665. device_unregister(dev);
  666. /* prevent further gpiod exports */
  667. chip->exported = false;
  668. status = 0;
  669. } else
  670. status = -ENODEV;
  671. mutex_unlock(&sysfs_lock);
  672. if (status)
  673. chip_dbg(chip, "%s: status %d\n", __func__, status);
  674. /* unregister gpiod class devices owned by sysfs */
  675. for (i = 0; i < chip->ngpio; i++) {
  676. desc = &chip->desc[i];
  677. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
  678. gpiod_free(desc);
  679. }
  680. }
  681. static int __init gpiolib_sysfs_init(void)
  682. {
  683. int status;
  684. unsigned long flags;
  685. struct gpio_chip *chip;
  686. status = class_register(&gpio_class);
  687. if (status < 0)
  688. return status;
  689. /* Scan and register the gpio_chips which registered very
  690. * early (e.g. before the class_register above was called).
  691. *
  692. * We run before arch_initcall() so chip->dev nodes can have
  693. * registered, and so arch_initcall() can always gpio_export().
  694. */
  695. spin_lock_irqsave(&gpio_lock, flags);
  696. list_for_each_entry(chip, &gpio_chips, list) {
  697. if (chip->exported)
  698. continue;
  699. /*
  700. * TODO we yield gpio_lock here because gpiochip_export()
  701. * acquires a mutex. This is unsafe and needs to be fixed.
  702. *
  703. * Also it would be nice to use gpiochip_find() here so we
  704. * can keep gpio_chips local to gpiolib.c, but the yield of
  705. * gpio_lock prevents us from doing this.
  706. */
  707. spin_unlock_irqrestore(&gpio_lock, flags);
  708. status = gpiochip_export(chip);
  709. spin_lock_irqsave(&gpio_lock, flags);
  710. }
  711. spin_unlock_irqrestore(&gpio_lock, flags);
  712. return status;
  713. }
  714. postcore_initcall(gpiolib_sysfs_init);