xref: /linux/drivers/gpio/gpiolib-sysfs.c (revision 0cf6d425d39cfc1b676fbf9dea36ecd68eeb27ee)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/bitops.h>
4 #include <linux/cleanup.h>
5 #include <linux/device.h>
6 #include <linux/init.h>
7 #include <linux/interrupt.h>
8 #include <linux/kdev_t.h>
9 #include <linux/kstrtox.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/printk.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/srcu.h>
16 #include <linux/sysfs.h>
17 #include <linux/types.h>
18 
19 #include <linux/gpio/consumer.h>
20 #include <linux/gpio/driver.h>
21 
22 #include <uapi/linux/gpio.h>
23 
24 #include "gpiolib.h"
25 #include "gpiolib-sysfs.h"
26 
27 struct kernfs_node;
28 
29 #define GPIO_IRQF_TRIGGER_NONE		0
30 #define GPIO_IRQF_TRIGGER_FALLING	BIT(0)
31 #define GPIO_IRQF_TRIGGER_RISING	BIT(1)
32 #define GPIO_IRQF_TRIGGER_BOTH		(GPIO_IRQF_TRIGGER_FALLING | \
33 					 GPIO_IRQF_TRIGGER_RISING)
34 
35 struct gpiod_data {
36 	struct gpio_desc *desc;
37 
38 	struct mutex mutex;
39 	struct kernfs_node *value_kn;
40 	int irq;
41 	unsigned char irq_flags;
42 
43 	bool direction_can_change;
44 };
45 
46 struct gpiodev_data {
47 	struct gpio_device *gdev;
48 	struct device *cdev_base; /* Class device by GPIO base */
49 };
50 
51 /*
52  * Lock to serialise gpiod export and unexport, and prevent re-export of
53  * gpiod whose chip is being unregistered.
54  */
55 static DEFINE_MUTEX(sysfs_lock);
56 
57 /*
58  * /sys/class/gpio/gpioN... only for GPIOs that are exported
59  *   /direction
60  *      * MAY BE OMITTED if kernel won't allow direction changes
61  *      * is read/write as "in" or "out"
62  *      * may also be written as "high" or "low", initializing
63  *        output value as specified ("out" implies "low")
64  *   /value
65  *      * always readable, subject to hardware behavior
66  *      * may be writable, as zero/nonzero
67  *   /edge
68  *      * configures behavior of poll(2) on /value
69  *      * available only if pin can generate IRQs on input
70  *      * is read/write as "none", "falling", "rising", or "both"
71  *   /active_low
72  *      * configures polarity of /value
73  *      * is read/write as zero/nonzero
74  *      * also affects existing and subsequent "falling" and "rising"
75  *        /edge configuration
76  */
77 
78 static ssize_t direction_show(struct device *dev,
79 			      struct device_attribute *attr, char *buf)
80 {
81 	struct gpiod_data *data = dev_get_drvdata(dev);
82 	struct gpio_desc *desc = data->desc;
83 	int value;
84 
85 	scoped_guard(mutex, &data->mutex) {
86 		gpiod_get_direction(desc);
87 		value = !!test_bit(FLAG_IS_OUT, &desc->flags);
88 	}
89 
90 	return sysfs_emit(buf, "%s\n", value ? "out" : "in");
91 }
92 
93 static ssize_t direction_store(struct device *dev,
94 			       struct device_attribute *attr, const char *buf,
95 			       size_t size)
96 {
97 	struct gpiod_data *data = dev_get_drvdata(dev);
98 	struct gpio_desc *desc = data->desc;
99 	ssize_t status;
100 
101 	guard(mutex)(&data->mutex);
102 
103 	if (sysfs_streq(buf, "high"))
104 		status = gpiod_direction_output_raw(desc, 1);
105 	else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
106 		status = gpiod_direction_output_raw(desc, 0);
107 	else if (sysfs_streq(buf, "in"))
108 		status = gpiod_direction_input(desc);
109 	else
110 		status = -EINVAL;
111 
112 	return status ? : size;
113 }
114 static DEVICE_ATTR_RW(direction);
115 
116 static ssize_t value_show(struct device *dev, struct device_attribute *attr,
117 			  char *buf)
118 {
119 	struct gpiod_data *data = dev_get_drvdata(dev);
120 	struct gpio_desc *desc = data->desc;
121 	ssize_t status;
122 
123 	scoped_guard(mutex, &data->mutex)
124 		status = gpiod_get_value_cansleep(desc);
125 
126 	if (status < 0)
127 		return status;
128 
129 	return sysfs_emit(buf, "%zd\n", status);
130 }
131 
132 static ssize_t value_store(struct device *dev, struct device_attribute *attr,
133 			   const char *buf, size_t size)
134 {
135 	struct gpiod_data *data = dev_get_drvdata(dev);
136 	struct gpio_desc *desc = data->desc;
137 	ssize_t status;
138 	long value;
139 
140 	status = kstrtol(buf, 0, &value);
141 	if (status)
142 		return status;
143 
144 	guard(mutex)(&data->mutex);
145 
146 	status = gpiod_set_value_cansleep(desc, value);
147 	if (status)
148 		return status;
149 
150 	return size;
151 }
152 static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
153 
154 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
155 {
156 	struct gpiod_data *data = priv;
157 
158 	sysfs_notify_dirent(data->value_kn);
159 
160 	return IRQ_HANDLED;
161 }
162 
163 /* Caller holds gpiod-data mutex. */
164 static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
165 {
166 	struct gpiod_data *data = dev_get_drvdata(dev);
167 	struct gpio_desc *desc = data->desc;
168 	unsigned long irq_flags;
169 	int ret;
170 
171 	CLASS(gpio_chip_guard, guard)(desc);
172 	if (!guard.gc)
173 		return -ENODEV;
174 
175 	data->irq = gpiod_to_irq(desc);
176 	if (data->irq < 0)
177 		return -EIO;
178 
179 	data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
180 	if (!data->value_kn)
181 		return -ENODEV;
182 
183 	irq_flags = IRQF_SHARED;
184 	if (flags & GPIO_IRQF_TRIGGER_FALLING) {
185 		irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
186 				IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
187 		set_bit(FLAG_EDGE_FALLING, &desc->flags);
188 	}
189 	if (flags & GPIO_IRQF_TRIGGER_RISING) {
190 		irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
191 				IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
192 		set_bit(FLAG_EDGE_RISING, &desc->flags);
193 	}
194 
195 	/*
196 	 * FIXME: This should be done in the irq_request_resources callback
197 	 * when the irq is requested, but a few drivers currently fail to do
198 	 * so.
199 	 *
200 	 * Remove this redundant call (along with the corresponding unlock)
201 	 * when those drivers have been fixed.
202 	 */
203 	ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
204 	if (ret < 0)
205 		goto err_put_kn;
206 
207 	ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
208 				"gpiolib", data);
209 	if (ret < 0)
210 		goto err_unlock;
211 
212 	data->irq_flags = flags;
213 
214 	return 0;
215 
216 err_unlock:
217 	gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
218 err_put_kn:
219 	clear_bit(FLAG_EDGE_RISING, &desc->flags);
220 	clear_bit(FLAG_EDGE_FALLING, &desc->flags);
221 	sysfs_put(data->value_kn);
222 
223 	return ret;
224 }
225 
226 /*
227  * Caller holds gpiod-data mutex (unless called after class-device
228  * deregistration).
229  */
230 static void gpio_sysfs_free_irq(struct device *dev)
231 {
232 	struct gpiod_data *data = dev_get_drvdata(dev);
233 	struct gpio_desc *desc = data->desc;
234 
235 	CLASS(gpio_chip_guard, guard)(desc);
236 	if (!guard.gc)
237 		return;
238 
239 	data->irq_flags = 0;
240 	free_irq(data->irq, data);
241 	gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
242 	clear_bit(FLAG_EDGE_RISING, &desc->flags);
243 	clear_bit(FLAG_EDGE_FALLING, &desc->flags);
244 	sysfs_put(data->value_kn);
245 }
246 
247 static const char *const trigger_names[] = {
248 	[GPIO_IRQF_TRIGGER_NONE]	= "none",
249 	[GPIO_IRQF_TRIGGER_FALLING]	= "falling",
250 	[GPIO_IRQF_TRIGGER_RISING]	= "rising",
251 	[GPIO_IRQF_TRIGGER_BOTH]	= "both",
252 };
253 
254 static ssize_t edge_show(struct device *dev, struct device_attribute *attr,
255 			 char *buf)
256 {
257 	struct gpiod_data *data = dev_get_drvdata(dev);
258 	int flags;
259 
260 	scoped_guard(mutex, &data->mutex)
261 		flags = data->irq_flags;
262 
263 	if (flags >= ARRAY_SIZE(trigger_names))
264 		return 0;
265 
266 	return sysfs_emit(buf, "%s\n", trigger_names[flags]);
267 }
268 
269 static ssize_t edge_store(struct device *dev, struct device_attribute *attr,
270 			  const char *buf, size_t size)
271 {
272 	struct gpiod_data *data = dev_get_drvdata(dev);
273 	ssize_t status = size;
274 	int flags;
275 
276 	flags = sysfs_match_string(trigger_names, buf);
277 	if (flags < 0)
278 		return flags;
279 
280 	guard(mutex)(&data->mutex);
281 
282 	if (flags == data->irq_flags)
283 		return size;
284 
285 	if (data->irq_flags)
286 		gpio_sysfs_free_irq(dev);
287 
288 	if (!flags)
289 		return size;
290 
291 	status = gpio_sysfs_request_irq(dev, flags);
292 	if (status)
293 		return status;
294 
295 	gpiod_line_state_notify(data->desc, GPIO_V2_LINE_CHANGED_CONFIG);
296 
297 	return size;
298 }
299 static DEVICE_ATTR_RW(edge);
300 
301 /* Caller holds gpiod-data mutex. */
302 static int gpio_sysfs_set_active_low(struct device *dev, int value)
303 {
304 	struct gpiod_data *data = dev_get_drvdata(dev);
305 	unsigned int flags = data->irq_flags;
306 	struct gpio_desc *desc = data->desc;
307 	int status = 0;
308 
309 	if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
310 		return 0;
311 
312 	assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
313 
314 	/* reconfigure poll(2) support if enabled on one edge only */
315 	if (flags == GPIO_IRQF_TRIGGER_FALLING ||
316 	    flags == GPIO_IRQF_TRIGGER_RISING) {
317 		gpio_sysfs_free_irq(dev);
318 		status = gpio_sysfs_request_irq(dev, flags);
319 	}
320 
321 	gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
322 
323 	return status;
324 }
325 
326 static ssize_t active_low_show(struct device *dev,
327 			       struct device_attribute *attr, char *buf)
328 {
329 	struct gpiod_data *data = dev_get_drvdata(dev);
330 	struct gpio_desc *desc = data->desc;
331 	int value;
332 
333 	scoped_guard(mutex, &data->mutex)
334 		value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags);
335 
336 	return sysfs_emit(buf, "%d\n", value);
337 }
338 
339 static ssize_t active_low_store(struct device *dev,
340 				struct device_attribute *attr,
341 				const char *buf, size_t size)
342 {
343 	struct gpiod_data *data = dev_get_drvdata(dev);
344 	ssize_t status;
345 	long value;
346 
347 	status = kstrtol(buf, 0, &value);
348 	if (status)
349 		return status;
350 
351 	guard(mutex)(&data->mutex);
352 
353 	return gpio_sysfs_set_active_low(dev, value) ?: size;
354 }
355 static DEVICE_ATTR_RW(active_low);
356 
357 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
358 			       int n)
359 {
360 	struct device *dev = kobj_to_dev(kobj);
361 	struct gpiod_data *data = dev_get_drvdata(dev);
362 	struct gpio_desc *desc = data->desc;
363 	umode_t mode = attr->mode;
364 	bool show_direction = data->direction_can_change;
365 
366 	if (attr == &dev_attr_direction.attr) {
367 		if (!show_direction)
368 			mode = 0;
369 	} else if (attr == &dev_attr_edge.attr) {
370 		if (gpiod_to_irq(desc) < 0)
371 			mode = 0;
372 		if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
373 			mode = 0;
374 	}
375 
376 	return mode;
377 }
378 
379 static struct attribute *gpio_attrs[] = {
380 	&dev_attr_direction.attr,
381 	&dev_attr_edge.attr,
382 	&dev_attr_value.attr,
383 	&dev_attr_active_low.attr,
384 	NULL,
385 };
386 
387 static const struct attribute_group gpio_group = {
388 	.attrs = gpio_attrs,
389 	.is_visible = gpio_is_visible,
390 };
391 
392 static const struct attribute_group *gpio_groups[] = {
393 	&gpio_group,
394 	NULL
395 };
396 
397 /*
398  * /sys/class/gpio/gpiochipN/
399  *   /base ... matching gpio_chip.base (N)
400  *   /label ... matching gpio_chip.label
401  *   /ngpio ... matching gpio_chip.ngpio
402  */
403 
404 static ssize_t base_show(struct device *dev, struct device_attribute *attr,
405 			 char *buf)
406 {
407 	const struct gpiodev_data *data = dev_get_drvdata(dev);
408 
409 	return sysfs_emit(buf, "%u\n", data->gdev->base);
410 }
411 static DEVICE_ATTR_RO(base);
412 
413 static ssize_t label_show(struct device *dev, struct device_attribute *attr,
414 			  char *buf)
415 {
416 	const struct gpiodev_data *data = dev_get_drvdata(dev);
417 
418 	return sysfs_emit(buf, "%s\n", data->gdev->label);
419 }
420 static DEVICE_ATTR_RO(label);
421 
422 static ssize_t ngpio_show(struct device *dev, struct device_attribute *attr,
423 			  char *buf)
424 {
425 	const struct gpiodev_data *data = dev_get_drvdata(dev);
426 
427 	return sysfs_emit(buf, "%u\n", data->gdev->ngpio);
428 }
429 static DEVICE_ATTR_RO(ngpio);
430 
431 static struct attribute *gpiochip_attrs[] = {
432 	&dev_attr_base.attr,
433 	&dev_attr_label.attr,
434 	&dev_attr_ngpio.attr,
435 	NULL,
436 };
437 ATTRIBUTE_GROUPS(gpiochip);
438 
439 /*
440  * /sys/class/gpio/export ... write-only
441  *	integer N ... number of GPIO to export (full access)
442  * /sys/class/gpio/unexport ... write-only
443  *	integer N ... number of GPIO to unexport
444  */
445 static ssize_t export_store(const struct class *class,
446 			    const struct class_attribute *attr,
447 			    const char *buf, size_t len)
448 {
449 	struct gpio_desc *desc;
450 	int status, offset;
451 	long gpio;
452 
453 	status = kstrtol(buf, 0, &gpio);
454 	if (status)
455 		return status;
456 
457 	desc = gpio_to_desc(gpio);
458 	/* reject invalid GPIOs */
459 	if (!desc) {
460 		pr_debug_ratelimited("%s: invalid GPIO %ld\n", __func__, gpio);
461 		return -EINVAL;
462 	}
463 
464 	CLASS(gpio_chip_guard, guard)(desc);
465 	if (!guard.gc)
466 		return -ENODEV;
467 
468 	offset = gpio_chip_hwgpio(desc);
469 	if (!gpiochip_line_is_valid(guard.gc, offset)) {
470 		pr_debug_ratelimited("%s: GPIO %ld masked\n", __func__, gpio);
471 		return -EINVAL;
472 	}
473 
474 	/* No extra locking here; FLAG_SYSFS just signifies that the
475 	 * request and export were done by on behalf of userspace, so
476 	 * they may be undone on its behalf too.
477 	 */
478 
479 	status = gpiod_request_user(desc, "sysfs");
480 	if (status)
481 		goto done;
482 
483 	status = gpiod_set_transitory(desc, false);
484 	if (status) {
485 		gpiod_free(desc);
486 		goto done;
487 	}
488 
489 	status = gpiod_export(desc, true);
490 	if (status < 0) {
491 		gpiod_free(desc);
492 	} else {
493 		set_bit(FLAG_SYSFS, &desc->flags);
494 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
495 	}
496 
497 done:
498 	if (status)
499 		pr_debug("%s: status %d\n", __func__, status);
500 	return status ? : len;
501 }
502 static CLASS_ATTR_WO(export);
503 
504 static ssize_t unexport_store(const struct class *class,
505 			      const struct class_attribute *attr,
506 			      const char *buf, size_t len)
507 {
508 	struct gpio_desc *desc;
509 	int status;
510 	long gpio;
511 
512 	status = kstrtol(buf, 0, &gpio);
513 	if (status < 0)
514 		goto done;
515 
516 	desc = gpio_to_desc(gpio);
517 	/* reject bogus commands (gpiod_unexport() ignores them) */
518 	if (!desc) {
519 		pr_debug_ratelimited("%s: invalid GPIO %ld\n", __func__, gpio);
520 		return -EINVAL;
521 	}
522 
523 	status = -EINVAL;
524 
525 	/* No extra locking here; FLAG_SYSFS just signifies that the
526 	 * request and export were done by on behalf of userspace, so
527 	 * they may be undone on its behalf too.
528 	 */
529 	if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
530 		gpiod_unexport(desc);
531 		gpiod_free(desc);
532 		status = 0;
533 	}
534 done:
535 	if (status)
536 		pr_debug("%s: status %d\n", __func__, status);
537 	return status ? : len;
538 }
539 static CLASS_ATTR_WO(unexport);
540 
541 static struct attribute *gpio_class_attrs[] = {
542 	&class_attr_export.attr,
543 	&class_attr_unexport.attr,
544 	NULL,
545 };
546 ATTRIBUTE_GROUPS(gpio_class);
547 
548 static const struct class gpio_class = {
549 	.name =		"gpio",
550 	.class_groups =	gpio_class_groups,
551 };
552 
553 static int match_gdev(struct device *dev, const void *desc)
554 {
555 	struct gpiodev_data *data = dev_get_drvdata(dev);
556 	const struct gpio_device *gdev = desc;
557 
558 	return data && data->gdev == gdev;
559 }
560 
561 static struct gpiodev_data *
562 gdev_get_data(struct gpio_device *gdev) __must_hold(&sysfs_lock)
563 {
564 	struct device *cdev __free(put_device) = class_find_device(&gpio_class,
565 								   NULL, gdev,
566 								   match_gdev);
567 	if (!cdev)
568 		return NULL;
569 
570 	return dev_get_drvdata(cdev);
571 };
572 
573 /**
574  * gpiod_export - export a GPIO through sysfs
575  * @desc: GPIO to make available, already requested
576  * @direction_may_change: true if userspace may change GPIO direction
577  * Context: arch_initcall or later
578  *
579  * When drivers want to make a GPIO accessible to userspace after they
580  * have requested it -- perhaps while debugging, or as part of their
581  * public interface -- they may use this routine.  If the GPIO can
582  * change direction (some can't) and the caller allows it, userspace
583  * will see "direction" sysfs attribute which may be used to change
584  * the gpio's direction.  A "value" attribute will always be provided.
585  *
586  * Returns:
587  * 0 on success, or negative errno on failure.
588  */
589 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
590 {
591 	struct gpio_device *gdev;
592 	struct gpiod_data *data;
593 	struct device *dev;
594 	int status;
595 
596 	/* can't export until sysfs is available ... */
597 	if (!class_is_registered(&gpio_class)) {
598 		pr_debug("%s: called too early!\n", __func__);
599 		return -ENOENT;
600 	}
601 
602 	if (!desc) {
603 		pr_debug("%s: invalid gpio descriptor\n", __func__);
604 		return -EINVAL;
605 	}
606 
607 	CLASS(gpio_chip_guard, guard)(desc);
608 	if (!guard.gc)
609 		return -ENODEV;
610 
611 	if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
612 		return -EPERM;
613 
614 	gdev = desc->gdev;
615 
616 	guard(mutex)(&sysfs_lock);
617 
618 	if (!test_bit(FLAG_REQUESTED, &desc->flags)) {
619 		gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__);
620 		status = -EPERM;
621 		goto err_clear_bit;
622 	}
623 
624 	data = kzalloc(sizeof(*data), GFP_KERNEL);
625 	if (!data) {
626 		status = -ENOMEM;
627 		goto err_clear_bit;
628 	}
629 
630 	data->desc = desc;
631 	mutex_init(&data->mutex);
632 	if (guard.gc->direction_input && guard.gc->direction_output)
633 		data->direction_can_change = direction_may_change;
634 	else
635 		data->direction_can_change = false;
636 
637 	dev = device_create_with_groups(&gpio_class, &gdev->dev,
638 					MKDEV(0, 0), data, gpio_groups,
639 					"gpio%u", desc_to_gpio(desc));
640 	if (IS_ERR(dev)) {
641 		status = PTR_ERR(dev);
642 		goto err_free_data;
643 	}
644 
645 	return 0;
646 
647 err_free_data:
648 	kfree(data);
649 err_clear_bit:
650 	clear_bit(FLAG_EXPORT, &desc->flags);
651 	gpiod_dbg(desc, "%s: status %d\n", __func__, status);
652 	return status;
653 }
654 EXPORT_SYMBOL_GPL(gpiod_export);
655 
656 static int match_export(struct device *dev, const void *desc)
657 {
658 	struct gpiod_data *data = dev_get_drvdata(dev);
659 
660 	return data->desc == desc;
661 }
662 
663 /**
664  * gpiod_export_link - create a sysfs link to an exported GPIO node
665  * @dev: device under which to create symlink
666  * @name: name of the symlink
667  * @desc: GPIO to create symlink to, already exported
668  *
669  * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
670  * node. Caller is responsible for unlinking.
671  *
672  * Returns:
673  * 0 on success, or negative errno on failure.
674  */
675 int gpiod_export_link(struct device *dev, const char *name,
676 		      struct gpio_desc *desc)
677 {
678 	struct device *cdev;
679 	int ret;
680 
681 	if (!desc) {
682 		pr_warn("%s: invalid GPIO\n", __func__);
683 		return -EINVAL;
684 	}
685 
686 	cdev = class_find_device(&gpio_class, NULL, desc, match_export);
687 	if (!cdev)
688 		return -ENODEV;
689 
690 	ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
691 	put_device(cdev);
692 
693 	return ret;
694 }
695 EXPORT_SYMBOL_GPL(gpiod_export_link);
696 
697 /**
698  * gpiod_unexport - reverse effect of gpiod_export()
699  * @desc: GPIO to make unavailable
700  *
701  * This is implicit on gpiod_free().
702  */
703 void gpiod_unexport(struct gpio_desc *desc)
704 {
705 	struct gpiod_data *data;
706 	struct device *dev;
707 
708 	if (!desc) {
709 		pr_warn("%s: invalid GPIO\n", __func__);
710 		return;
711 	}
712 
713 	scoped_guard(mutex, &sysfs_lock) {
714 		if (!test_bit(FLAG_EXPORT, &desc->flags))
715 			return;
716 
717 		dev = class_find_device(&gpio_class, NULL, desc, match_export);
718 		if (!dev)
719 			return;
720 
721 		data = dev_get_drvdata(dev);
722 		clear_bit(FLAG_EXPORT, &desc->flags);
723 		device_unregister(dev);
724 
725 		/*
726 		 * Release irq after deregistration to prevent race with
727 		 * edge_store.
728 		 */
729 		if (data->irq_flags)
730 			gpio_sysfs_free_irq(dev);
731 	}
732 
733 	put_device(dev);
734 	mutex_destroy(&data->mutex);
735 	kfree(data);
736 }
737 EXPORT_SYMBOL_GPL(gpiod_unexport);
738 
739 int gpiochip_sysfs_register(struct gpio_device *gdev)
740 {
741 	struct gpiodev_data *data;
742 	struct gpio_chip *chip;
743 	struct device *parent;
744 	int err;
745 
746 	/*
747 	 * Many systems add gpio chips for SOC support very early,
748 	 * before driver model support is available.  In those cases we
749 	 * register later, in gpiolib_sysfs_init() ... here we just
750 	 * verify that _some_ field of gpio_class got initialized.
751 	 */
752 	if (!class_is_registered(&gpio_class))
753 		return 0;
754 
755 	guard(srcu)(&gdev->srcu);
756 
757 	chip = srcu_dereference(gdev->chip, &gdev->srcu);
758 	if (!chip)
759 		return -ENODEV;
760 
761 	/*
762 	 * For sysfs backward compatibility we need to preserve this
763 	 * preferred parenting to the gpio_chip parent field, if set.
764 	 */
765 	if (chip->parent)
766 		parent = chip->parent;
767 	else
768 		parent = &gdev->dev;
769 
770 	data = kmalloc(sizeof(*data), GFP_KERNEL);
771 	if (!data)
772 		return -ENOMEM;
773 
774 	data->gdev = gdev;
775 
776 	guard(mutex)(&sysfs_lock);
777 
778 	/* use chip->base for the ID; it's already known to be unique */
779 	data->cdev_base = device_create_with_groups(&gpio_class, parent,
780 						    MKDEV(0, 0), data,
781 						    gpiochip_groups,
782 						    GPIOCHIP_NAME "%d",
783 						    chip->base);
784 	if (IS_ERR(data->cdev_base)) {
785 		err = PTR_ERR(data->cdev_base);
786 		kfree(data);
787 		return err;
788 	}
789 
790 	return 0;
791 }
792 
793 void gpiochip_sysfs_unregister(struct gpio_device *gdev)
794 {
795 	struct gpiodev_data *data;
796 	struct gpio_desc *desc;
797 	struct gpio_chip *chip;
798 
799 	scoped_guard(mutex, &sysfs_lock) {
800 		data = gdev_get_data(gdev);
801 		if (!data)
802 			return;
803 
804 		device_unregister(data->cdev_base);
805 		kfree(data);
806 	}
807 
808 	guard(srcu)(&gdev->srcu);
809 
810 	chip = srcu_dereference(gdev->chip, &gdev->srcu);
811 	if (!chip)
812 		return;
813 
814 	/* unregister gpiod class devices owned by sysfs */
815 	for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) {
816 		gpiod_unexport(desc);
817 		gpiod_free(desc);
818 	}
819 }
820 
821 /*
822  * We're not really looking for a device - we just want to iterate over the
823  * list and call this callback for each GPIO device. This is why this function
824  * always returns 0.
825  */
826 static int gpiofind_sysfs_register(struct gpio_chip *gc, const void *data)
827 {
828 	struct gpio_device *gdev = gc->gpiodev;
829 	int ret;
830 
831 	ret = gpiochip_sysfs_register(gdev);
832 	if (ret)
833 		chip_err(gc, "failed to register the sysfs entry: %d\n", ret);
834 
835 	return 0;
836 }
837 
838 static int __init gpiolib_sysfs_init(void)
839 {
840 	int status;
841 
842 	status = class_register(&gpio_class);
843 	if (status < 0)
844 		return status;
845 
846 	/* Scan and register the gpio_chips which registered very
847 	 * early (e.g. before the class_register above was called).
848 	 *
849 	 * We run before arch_initcall() so chip->dev nodes can have
850 	 * registered, and so arch_initcall() can always gpiod_export().
851 	 */
852 	(void)gpio_device_find(NULL, gpiofind_sysfs_register);
853 
854 	return 0;
855 }
856 postcore_initcall(gpiolib_sysfs_init);
857