gpiolib-sysfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
  161. "gpiolib", value_sd);
  162. if (ret < 0)
  163. goto free_id;
  164. ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
  165. if (ret < 0) {
  166. gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
  167. goto free_id;
  168. }
  169. desc->flags |= gpio_flags;
  170. return 0;
  171. free_id:
  172. idr_remove(&dirent_idr, id);
  173. desc->flags &= GPIO_FLAGS_MASK;
  174. free_sd:
  175. if (value_sd)
  176. sysfs_put(value_sd);
  177. err_out:
  178. return ret;
  179. }
  180. static const struct {
  181. const char *name;
  182. unsigned long flags;
  183. } trigger_types[] = {
  184. { "none", 0 },
  185. { "falling", BIT(FLAG_TRIG_FALL) },
  186. { "rising", BIT(FLAG_TRIG_RISE) },
  187. { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
  188. };
  189. static ssize_t gpio_edge_show(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. const struct gpio_desc *desc = dev_get_drvdata(dev);
  193. ssize_t status;
  194. mutex_lock(&sysfs_lock);
  195. if (!test_bit(FLAG_EXPORT, &desc->flags))
  196. status = -EIO;
  197. else {
  198. int i;
  199. status = 0;
  200. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  201. if ((desc->flags & GPIO_TRIGGER_MASK)
  202. == trigger_types[i].flags) {
  203. status = sprintf(buf, "%s\n",
  204. trigger_types[i].name);
  205. break;
  206. }
  207. }
  208. mutex_unlock(&sysfs_lock);
  209. return status;
  210. }
  211. static ssize_t gpio_edge_store(struct device *dev,
  212. struct device_attribute *attr, const char *buf, size_t size)
  213. {
  214. struct gpio_desc *desc = dev_get_drvdata(dev);
  215. ssize_t status;
  216. int i;
  217. for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
  218. if (sysfs_streq(trigger_types[i].name, buf))
  219. goto found;
  220. return -EINVAL;
  221. found:
  222. mutex_lock(&sysfs_lock);
  223. if (!test_bit(FLAG_EXPORT, &desc->flags))
  224. status = -EIO;
  225. else {
  226. status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
  227. if (!status)
  228. status = size;
  229. }
  230. mutex_unlock(&sysfs_lock);
  231. return status;
  232. }
  233. static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
  234. static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
  235. int value)
  236. {
  237. int status = 0;
  238. if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
  239. return 0;
  240. if (value)
  241. set_bit(FLAG_ACTIVE_LOW, &desc->flags);
  242. else
  243. clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
  244. /* reconfigure poll(2) support if enabled on one edge only */
  245. if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
  246. !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
  247. unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
  248. gpio_setup_irq(desc, dev, 0);
  249. status = gpio_setup_irq(desc, dev, trigger_flags);
  250. }
  251. return status;
  252. }
  253. static ssize_t gpio_active_low_show(struct device *dev,
  254. struct device_attribute *attr, char *buf)
  255. {
  256. const struct gpio_desc *desc = dev_get_drvdata(dev);
  257. ssize_t status;
  258. mutex_lock(&sysfs_lock);
  259. if (!test_bit(FLAG_EXPORT, &desc->flags))
  260. status = -EIO;
  261. else
  262. status = sprintf(buf, "%d\n",
  263. !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
  264. mutex_unlock(&sysfs_lock);
  265. return status;
  266. }
  267. static ssize_t gpio_active_low_store(struct device *dev,
  268. struct device_attribute *attr, const char *buf, size_t size)
  269. {
  270. struct gpio_desc *desc = dev_get_drvdata(dev);
  271. ssize_t status;
  272. mutex_lock(&sysfs_lock);
  273. if (!test_bit(FLAG_EXPORT, &desc->flags)) {
  274. status = -EIO;
  275. } else {
  276. long value;
  277. status = kstrtol(buf, 0, &value);
  278. if (status == 0)
  279. status = sysfs_set_active_low(desc, dev, value != 0);
  280. }
  281. mutex_unlock(&sysfs_lock);
  282. return status ? : size;
  283. }
  284. static DEVICE_ATTR(active_low, 0644,
  285. gpio_active_low_show, gpio_active_low_store);
  286. static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
  287. int n)
  288. {
  289. struct device *dev = container_of(kobj, struct device, kobj);
  290. struct gpio_desc *desc = dev_get_drvdata(dev);
  291. umode_t mode = attr->mode;
  292. bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
  293. if (attr == &dev_attr_direction.attr) {
  294. if (!show_direction)
  295. mode = 0;
  296. } else if (attr == &dev_attr_edge.attr) {
  297. if (gpiod_to_irq(desc) < 0)
  298. mode = 0;
  299. if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
  300. mode = 0;
  301. }
  302. return mode;
  303. }
  304. static struct attribute *gpio_attrs[] = {
  305. &dev_attr_direction.attr,
  306. &dev_attr_edge.attr,
  307. &dev_attr_value.attr,
  308. &dev_attr_active_low.attr,
  309. NULL,
  310. };
  311. static const struct attribute_group gpio_group = {
  312. .attrs = gpio_attrs,
  313. .is_visible = gpio_is_visible,
  314. };
  315. static const struct attribute_group *gpio_groups[] = {
  316. &gpio_group,
  317. NULL
  318. };
  319. /*
  320. * /sys/class/gpio/gpiochipN/
  321. * /base ... matching gpio_chip.base (N)
  322. * /label ... matching gpio_chip.label
  323. * /ngpio ... matching gpio_chip.ngpio
  324. */
  325. static ssize_t chip_base_show(struct device *dev,
  326. struct device_attribute *attr, char *buf)
  327. {
  328. const struct gpio_chip *chip = dev_get_drvdata(dev);
  329. return sprintf(buf, "%d\n", chip->base);
  330. }
  331. static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
  332. static ssize_t chip_label_show(struct device *dev,
  333. struct device_attribute *attr, char *buf)
  334. {
  335. const struct gpio_chip *chip = dev_get_drvdata(dev);
  336. return sprintf(buf, "%s\n", chip->label ? : "");
  337. }
  338. static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
  339. static ssize_t chip_ngpio_show(struct device *dev,
  340. struct device_attribute *attr, char *buf)
  341. {
  342. const struct gpio_chip *chip = dev_get_drvdata(dev);
  343. return sprintf(buf, "%u\n", chip->ngpio);
  344. }
  345. static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
  346. static struct attribute *gpiochip_attrs[] = {
  347. &dev_attr_base.attr,
  348. &dev_attr_label.attr,
  349. &dev_attr_ngpio.attr,
  350. NULL,
  351. };
  352. ATTRIBUTE_GROUPS(gpiochip);
  353. /*
  354. * /sys/class/gpio/export ... write-only
  355. * integer N ... number of GPIO to export (full access)
  356. * /sys/class/gpio/unexport ... write-only
  357. * integer N ... number of GPIO to unexport
  358. */
  359. static ssize_t export_store(struct class *class,
  360. struct class_attribute *attr,
  361. const char *buf, size_t len)
  362. {
  363. long gpio;
  364. struct gpio_desc *desc;
  365. int status;
  366. status = kstrtol(buf, 0, &gpio);
  367. if (status < 0)
  368. goto done;
  369. desc = gpio_to_desc(gpio);
  370. /* reject invalid GPIOs */
  371. if (!desc) {
  372. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  373. return -EINVAL;
  374. }
  375. /* No extra locking here; FLAG_SYSFS just signifies that the
  376. * request and export were done by on behalf of userspace, so
  377. * they may be undone on its behalf too.
  378. */
  379. status = gpiod_request(desc, "sysfs");
  380. if (status < 0) {
  381. if (status == -EPROBE_DEFER)
  382. status = -ENODEV;
  383. goto done;
  384. }
  385. status = gpiod_export(desc, true);
  386. if (status < 0)
  387. gpiod_free(desc);
  388. else
  389. set_bit(FLAG_SYSFS, &desc->flags);
  390. done:
  391. if (status)
  392. pr_debug("%s: status %d\n", __func__, status);
  393. return status ? : len;
  394. }
  395. static ssize_t unexport_store(struct class *class,
  396. struct class_attribute *attr,
  397. const char *buf, size_t len)
  398. {
  399. long gpio;
  400. struct gpio_desc *desc;
  401. int status;
  402. status = kstrtol(buf, 0, &gpio);
  403. if (status < 0)
  404. goto done;
  405. desc = gpio_to_desc(gpio);
  406. /* reject bogus commands (gpio_unexport ignores them) */
  407. if (!desc) {
  408. pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
  409. return -EINVAL;
  410. }
  411. status = -EINVAL;
  412. /* No extra locking here; FLAG_SYSFS just signifies that the
  413. * request and export were done by on behalf of userspace, so
  414. * they may be undone on its behalf too.
  415. */
  416. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
  417. status = 0;
  418. gpiod_free(desc);
  419. }
  420. done:
  421. if (status)
  422. pr_debug("%s: status %d\n", __func__, status);
  423. return status ? : len;
  424. }
  425. static struct class_attribute gpio_class_attrs[] = {
  426. __ATTR(export, 0200, NULL, export_store),
  427. __ATTR(unexport, 0200, NULL, unexport_store),
  428. __ATTR_NULL,
  429. };
  430. static struct class gpio_class = {
  431. .name = "gpio",
  432. .owner = THIS_MODULE,
  433. .class_attrs = gpio_class_attrs,
  434. };
  435. /**
  436. * gpiod_export - export a GPIO through sysfs
  437. * @gpio: gpio to make available, already requested
  438. * @direction_may_change: true if userspace may change gpio direction
  439. * Context: arch_initcall or later
  440. *
  441. * When drivers want to make a GPIO accessible to userspace after they
  442. * have requested it -- perhaps while debugging, or as part of their
  443. * public interface -- they may use this routine. If the GPIO can
  444. * change direction (some can't) and the caller allows it, userspace
  445. * will see "direction" sysfs attribute which may be used to change
  446. * the gpio's direction. A "value" attribute will always be provided.
  447. *
  448. * Returns zero on success, else an error.
  449. */
  450. int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  451. {
  452. struct gpio_chip *chip;
  453. unsigned long flags;
  454. int status;
  455. const char *ioname = NULL;
  456. struct device *dev;
  457. int offset;
  458. /* can't export until sysfs is available ... */
  459. if (!gpio_class.p) {
  460. pr_debug("%s: called too early!\n", __func__);
  461. return -ENOENT;
  462. }
  463. if (!desc) {
  464. pr_debug("%s: invalid gpio descriptor\n", __func__);
  465. return -EINVAL;
  466. }
  467. chip = desc->chip;
  468. mutex_lock(&sysfs_lock);
  469. /* check if chip is being removed */
  470. if (!chip || !chip->exported) {
  471. status = -ENODEV;
  472. goto fail_unlock;
  473. }
  474. spin_lock_irqsave(&gpio_lock, flags);
  475. if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
  476. test_bit(FLAG_EXPORT, &desc->flags)) {
  477. spin_unlock_irqrestore(&gpio_lock, flags);
  478. gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
  479. __func__,
  480. test_bit(FLAG_REQUESTED, &desc->flags),
  481. test_bit(FLAG_EXPORT, &desc->flags));
  482. status = -EPERM;
  483. goto fail_unlock;
  484. }
  485. if (desc->chip->direction_input && desc->chip->direction_output &&
  486. direction_may_change) {
  487. set_bit(FLAG_SYSFS_DIR, &desc->flags);
  488. }
  489. spin_unlock_irqrestore(&gpio_lock, flags);
  490. offset = gpio_chip_hwgpio(desc);
  491. if (desc->chip->names && desc->chip->names[offset])
  492. ioname = desc->chip->names[offset];
  493. dev = device_create_with_groups(&gpio_class, desc->chip->dev,
  494. MKDEV(0, 0), desc, gpio_groups,
  495. ioname ? ioname : "gpio%u",
  496. desc_to_gpio(desc));
  497. if (IS_ERR(dev)) {
  498. status = PTR_ERR(dev);
  499. goto fail_unlock;
  500. }
  501. set_bit(FLAG_EXPORT, &desc->flags);
  502. mutex_unlock(&sysfs_lock);
  503. return 0;
  504. fail_unlock:
  505. mutex_unlock(&sysfs_lock);
  506. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  507. return status;
  508. }
  509. EXPORT_SYMBOL_GPL(gpiod_export);
  510. static int match_export(struct device *dev, const void *data)
  511. {
  512. return dev_get_drvdata(dev) == data;
  513. }
  514. /**
  515. * gpiod_export_link - create a sysfs link to an exported GPIO node
  516. * @dev: device under which to create symlink
  517. * @name: name of the symlink
  518. * @gpio: gpio to create symlink to, already exported
  519. *
  520. * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
  521. * node. Caller is responsible for unlinking.
  522. *
  523. * Returns zero on success, else an error.
  524. */
  525. int gpiod_export_link(struct device *dev, const char *name,
  526. struct gpio_desc *desc)
  527. {
  528. int status = -EINVAL;
  529. if (!desc) {
  530. pr_warn("%s: invalid GPIO\n", __func__);
  531. return -EINVAL;
  532. }
  533. mutex_lock(&sysfs_lock);
  534. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  535. struct device *tdev;
  536. tdev = class_find_device(&gpio_class, NULL, desc, match_export);
  537. if (tdev != NULL) {
  538. status = sysfs_create_link(&dev->kobj, &tdev->kobj,
  539. name);
  540. put_device(tdev);
  541. } else {
  542. status = -ENODEV;
  543. }
  544. }
  545. mutex_unlock(&sysfs_lock);
  546. if (status)
  547. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  548. return status;
  549. }
  550. EXPORT_SYMBOL_GPL(gpiod_export_link);
  551. /**
  552. * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
  553. * @gpio: gpio to change
  554. * @value: non-zero to use active low, i.e. inverted values
  555. *
  556. * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
  557. * The GPIO does not have to be exported yet. If poll(2) support has
  558. * been enabled for either rising or falling edge, it will be
  559. * reconfigured to follow the new polarity.
  560. *
  561. * Returns zero on success, else an error.
  562. */
  563. int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
  564. {
  565. struct device *dev = NULL;
  566. int status = -EINVAL;
  567. if (!desc) {
  568. pr_warn("%s: invalid GPIO\n", __func__);
  569. return -EINVAL;
  570. }
  571. mutex_lock(&sysfs_lock);
  572. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  573. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  574. if (dev == NULL) {
  575. status = -ENODEV;
  576. goto unlock;
  577. }
  578. }
  579. status = sysfs_set_active_low(desc, dev, value);
  580. put_device(dev);
  581. unlock:
  582. mutex_unlock(&sysfs_lock);
  583. if (status)
  584. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  585. return status;
  586. }
  587. EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
  588. /**
  589. * gpiod_unexport - reverse effect of gpio_export()
  590. * @gpio: gpio to make unavailable
  591. *
  592. * This is implicit on gpio_free().
  593. */
  594. void gpiod_unexport(struct gpio_desc *desc)
  595. {
  596. int status = 0;
  597. struct device *dev = NULL;
  598. if (!desc) {
  599. pr_warn("%s: invalid GPIO\n", __func__);
  600. return;
  601. }
  602. mutex_lock(&sysfs_lock);
  603. if (test_bit(FLAG_EXPORT, &desc->flags)) {
  604. dev = class_find_device(&gpio_class, NULL, desc, match_export);
  605. if (dev) {
  606. gpio_setup_irq(desc, dev, 0);
  607. clear_bit(FLAG_SYSFS_DIR, &desc->flags);
  608. clear_bit(FLAG_EXPORT, &desc->flags);
  609. } else
  610. status = -ENODEV;
  611. }
  612. mutex_unlock(&sysfs_lock);
  613. if (dev) {
  614. device_unregister(dev);
  615. put_device(dev);
  616. }
  617. if (status)
  618. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  619. }
  620. EXPORT_SYMBOL_GPL(gpiod_unexport);
  621. int gpiochip_export(struct gpio_chip *chip)
  622. {
  623. int status;
  624. struct device *dev;
  625. /* Many systems register gpio chips for SOC support very early,
  626. * before driver model support is available. In those cases we
  627. * export this later, in gpiolib_sysfs_init() ... here we just
  628. * verify that _some_ field of gpio_class got initialized.
  629. */
  630. if (!gpio_class.p)
  631. return 0;
  632. /* use chip->base for the ID; it's already known to be unique */
  633. mutex_lock(&sysfs_lock);
  634. dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
  635. chip, gpiochip_groups,
  636. "gpiochip%d", chip->base);
  637. if (IS_ERR(dev))
  638. status = PTR_ERR(dev);
  639. else
  640. status = 0;
  641. chip->exported = (status == 0);
  642. mutex_unlock(&sysfs_lock);
  643. if (status)
  644. chip_dbg(chip, "%s: status %d\n", __func__, status);
  645. return status;
  646. }
  647. void gpiochip_unexport(struct gpio_chip *chip)
  648. {
  649. int status;
  650. struct device *dev;
  651. struct gpio_desc *desc;
  652. unsigned int i;
  653. mutex_lock(&sysfs_lock);
  654. dev = class_find_device(&gpio_class, NULL, chip, match_export);
  655. if (dev) {
  656. put_device(dev);
  657. device_unregister(dev);
  658. /* prevent further gpiod exports */
  659. chip->exported = false;
  660. status = 0;
  661. } else
  662. status = -ENODEV;
  663. mutex_unlock(&sysfs_lock);
  664. if (status)
  665. chip_dbg(chip, "%s: status %d\n", __func__, status);
  666. /* unregister gpiod class devices owned by sysfs */
  667. for (i = 0; i < chip->ngpio; i++) {
  668. desc = &chip->desc[i];
  669. if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
  670. gpiod_free(desc);
  671. }
  672. }
  673. static int __init gpiolib_sysfs_init(void)
  674. {
  675. int status;
  676. unsigned long flags;
  677. struct gpio_chip *chip;
  678. status = class_register(&gpio_class);
  679. if (status < 0)
  680. return status;
  681. /* Scan and register the gpio_chips which registered very
  682. * early (e.g. before the class_register above was called).
  683. *
  684. * We run before arch_initcall() so chip->dev nodes can have
  685. * registered, and so arch_initcall() can always gpio_export().
  686. */
  687. spin_lock_irqsave(&gpio_lock, flags);
  688. list_for_each_entry(chip, &gpio_chips, list) {
  689. if (chip->exported)
  690. continue;
  691. /*
  692. * TODO we yield gpio_lock here because gpiochip_export()
  693. * acquires a mutex. This is unsafe and needs to be fixed.
  694. *
  695. * Also it would be nice to use gpiochip_find() here so we
  696. * can keep gpio_chips local to gpiolib.c, but the yield of
  697. * gpio_lock prevents us from doing this.
  698. */
  699. spin_unlock_irqrestore(&gpio_lock, flags);
  700. status = gpiochip_export(chip);
  701. spin_lock_irqsave(&gpio_lock, flags);
  702. }
  703. spin_unlock_irqrestore(&gpio_lock, flags);
  704. return status;
  705. }
  706. postcore_initcall(gpiolib_sysfs_init);