drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / gpio / gpio-sim.c
blobdcca1d7f173e5f186219aa2897dfc0be50a3d2b2
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * GPIO testing driver based on configfs.
5 * Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl>
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/array_size.h>
11 #include <linux/bitmap.h>
12 #include <linux/cleanup.h>
13 #include <linux/completion.h>
14 #include <linux/configfs.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/gpio/machine.h>
20 #include <linux/idr.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/irq_sim.h>
24 #include <linux/list.h>
25 #include <linux/lockdep.h>
26 #include <linux/minmax.h>
27 #include <linux/mod_devicetable.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/notifier.h>
31 #include <linux/platform_device.h>
32 #include <linux/property.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/string_helpers.h>
37 #include <linux/sysfs.h>
38 #include <linux/types.h>
40 #define GPIO_SIM_NGPIO_MAX 1024
41 #define GPIO_SIM_PROP_MAX 4 /* Max 3 properties + sentinel. */
42 #define GPIO_SIM_NUM_ATTRS 3 /* value, pull and sentinel */
44 static DEFINE_IDA(gpio_sim_ida);
46 struct gpio_sim_chip {
47 struct gpio_chip gc;
48 struct device *dev;
49 unsigned long *request_map;
50 unsigned long *direction_map;
51 unsigned long *value_map;
52 unsigned long *pull_map;
53 struct irq_domain *irq_sim;
54 struct mutex lock;
55 const struct attribute_group **attr_groups;
58 struct gpio_sim_attribute {
59 struct device_attribute dev_attr;
60 unsigned int offset;
63 static struct gpio_sim_attribute *
64 to_gpio_sim_attr(struct device_attribute *dev_attr)
66 return container_of(dev_attr, struct gpio_sim_attribute, dev_attr);
69 static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
70 unsigned int offset, int value)
72 int irq, irq_type, ret;
74 guard(mutex)(&chip->lock);
76 if (test_bit(offset, chip->request_map) &&
77 test_bit(offset, chip->direction_map)) {
78 if (value == !!test_bit(offset, chip->value_map))
79 goto set_pull;
82 * This is fine - it just means, nobody is listening
83 * for interrupts on this line, otherwise
84 * irq_create_mapping() would have been called from
85 * the to_irq() callback.
87 irq = irq_find_mapping(chip->irq_sim, offset);
88 if (!irq)
89 goto set_value;
91 irq_type = irq_get_trigger_type(irq);
93 if ((value && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
94 (!value && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
95 ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
96 true);
97 if (ret)
98 goto set_pull;
102 set_value:
103 /* Change the value unless we're actively driving the line. */
104 if (!test_bit(offset, chip->request_map) ||
105 test_bit(offset, chip->direction_map))
106 __assign_bit(offset, chip->value_map, value);
108 set_pull:
109 __assign_bit(offset, chip->pull_map, value);
110 return 0;
113 static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset)
115 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
117 guard(mutex)(&chip->lock);
119 return !!test_bit(offset, chip->value_map);
122 static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value)
124 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
126 scoped_guard(mutex, &chip->lock)
127 __assign_bit(offset, chip->value_map, value);
130 static int gpio_sim_get_multiple(struct gpio_chip *gc,
131 unsigned long *mask, unsigned long *bits)
133 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
135 scoped_guard(mutex, &chip->lock)
136 bitmap_replace(bits, bits, chip->value_map, mask, gc->ngpio);
138 return 0;
141 static void gpio_sim_set_multiple(struct gpio_chip *gc,
142 unsigned long *mask, unsigned long *bits)
144 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
146 scoped_guard(mutex, &chip->lock)
147 bitmap_replace(chip->value_map, chip->value_map, bits, mask,
148 gc->ngpio);
151 static int gpio_sim_direction_output(struct gpio_chip *gc,
152 unsigned int offset, int value)
154 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
156 scoped_guard(mutex, &chip->lock) {
157 __clear_bit(offset, chip->direction_map);
158 __assign_bit(offset, chip->value_map, value);
161 return 0;
164 static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset)
166 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
168 scoped_guard(mutex, &chip->lock)
169 __set_bit(offset, chip->direction_map);
171 return 0;
174 static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset)
176 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
177 int direction;
179 scoped_guard(mutex, &chip->lock)
180 direction = !!test_bit(offset, chip->direction_map);
182 return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
185 static int gpio_sim_set_config(struct gpio_chip *gc, unsigned int offset,
186 unsigned long config)
188 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
190 switch (pinconf_to_config_param(config)) {
191 case PIN_CONFIG_BIAS_PULL_UP:
192 return gpio_sim_apply_pull(chip, offset, 1);
193 case PIN_CONFIG_BIAS_PULL_DOWN:
194 return gpio_sim_apply_pull(chip, offset, 0);
195 default:
196 break;
199 return -ENOTSUPP;
202 static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset)
204 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
206 return irq_create_mapping(chip->irq_sim, offset);
209 static int gpio_sim_request(struct gpio_chip *gc, unsigned int offset)
211 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
213 scoped_guard(mutex, &chip->lock)
214 __set_bit(offset, chip->request_map);
216 return 0;
219 static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset)
221 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
223 scoped_guard(mutex, &chip->lock) {
224 __assign_bit(offset, chip->value_map,
225 !!test_bit(offset, chip->pull_map));
226 __clear_bit(offset, chip->request_map);
230 static int gpio_sim_irq_requested(struct irq_domain *domain,
231 irq_hw_number_t hwirq, void *data)
233 struct gpio_sim_chip *chip = data;
235 return gpiochip_lock_as_irq(&chip->gc, hwirq);
238 static void gpio_sim_irq_released(struct irq_domain *domain,
239 irq_hw_number_t hwirq, void *data)
241 struct gpio_sim_chip *chip = data;
243 gpiochip_unlock_as_irq(&chip->gc, hwirq);
246 static const struct irq_sim_ops gpio_sim_irq_sim_ops = {
247 .irq_sim_irq_requested = gpio_sim_irq_requested,
248 .irq_sim_irq_released = gpio_sim_irq_released,
251 static void gpio_sim_dbg_show(struct seq_file *seq, struct gpio_chip *gc)
253 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
254 const char *label;
255 int i;
257 guard(mutex)(&chip->lock);
259 for_each_hwgpio(gc, i, label)
260 seq_printf(seq, " gpio-%-3d (%s) %s,%s\n",
261 gc->base + i,
262 label ?: "<unused>",
263 test_bit(i, chip->direction_map) ? "input" :
264 test_bit(i, chip->value_map) ? "output-high" :
265 "output-low",
266 test_bit(i, chip->pull_map) ? "pull-up" :
267 "pull-down");
270 static ssize_t gpio_sim_sysfs_val_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
274 struct gpio_sim_chip *chip = dev_get_drvdata(dev);
275 int val;
277 scoped_guard(mutex, &chip->lock)
278 val = !!test_bit(line_attr->offset, chip->value_map);
280 return sysfs_emit(buf, "%d\n", val);
283 static ssize_t gpio_sim_sysfs_val_store(struct device *dev,
284 struct device_attribute *attr,
285 const char *buf, size_t count)
288 * Not assigning this function will result in write() returning -EIO
289 * which is confusing. Return -EPERM explicitly.
291 return -EPERM;
294 static const char *const gpio_sim_sysfs_pull_strings[] = {
295 [0] = "pull-down",
296 [1] = "pull-up",
299 static ssize_t gpio_sim_sysfs_pull_show(struct device *dev,
300 struct device_attribute *attr,
301 char *buf)
303 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
304 struct gpio_sim_chip *chip = dev_get_drvdata(dev);
305 int pull;
307 scoped_guard(mutex, &chip->lock)
308 pull = !!test_bit(line_attr->offset, chip->pull_map);
310 return sysfs_emit(buf, "%s\n", gpio_sim_sysfs_pull_strings[pull]);
313 static ssize_t gpio_sim_sysfs_pull_store(struct device *dev,
314 struct device_attribute *attr,
315 const char *buf, size_t len)
317 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
318 struct gpio_sim_chip *chip = dev_get_drvdata(dev);
319 int ret, pull;
321 pull = sysfs_match_string(gpio_sim_sysfs_pull_strings, buf);
322 if (pull < 0)
323 return pull;
325 ret = gpio_sim_apply_pull(chip, line_attr->offset, pull);
326 if (ret)
327 return ret;
329 return len;
332 static void gpio_sim_put_device(void *data)
334 struct device *dev = data;
336 put_device(dev);
339 static void gpio_sim_dispose_mappings(void *data)
341 struct gpio_sim_chip *chip = data;
342 unsigned int i;
344 for (i = 0; i < chip->gc.ngpio; i++)
345 irq_dispose_mapping(irq_find_mapping(chip->irq_sim, i));
348 static void gpio_sim_sysfs_remove(void *data)
350 struct gpio_sim_chip *chip = data;
352 sysfs_remove_groups(&chip->dev->kobj, chip->attr_groups);
355 static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip)
357 struct device_attribute *val_dev_attr, *pull_dev_attr;
358 struct gpio_sim_attribute *val_attr, *pull_attr;
359 unsigned int num_lines = chip->gc.ngpio;
360 struct device *dev = chip->gc.parent;
361 struct attribute_group *attr_group;
362 struct attribute **attrs;
363 int i, ret;
365 chip->attr_groups = devm_kcalloc(dev, sizeof(*chip->attr_groups),
366 num_lines + 1, GFP_KERNEL);
367 if (!chip->attr_groups)
368 return -ENOMEM;
370 for (i = 0; i < num_lines; i++) {
371 attr_group = devm_kzalloc(dev, sizeof(*attr_group), GFP_KERNEL);
372 attrs = devm_kcalloc(dev, GPIO_SIM_NUM_ATTRS, sizeof(*attrs),
373 GFP_KERNEL);
374 val_attr = devm_kzalloc(dev, sizeof(*val_attr), GFP_KERNEL);
375 pull_attr = devm_kzalloc(dev, sizeof(*pull_attr), GFP_KERNEL);
376 if (!attr_group || !attrs || !val_attr || !pull_attr)
377 return -ENOMEM;
379 attr_group->name = devm_kasprintf(dev, GFP_KERNEL,
380 "sim_gpio%u", i);
381 if (!attr_group->name)
382 return -ENOMEM;
384 val_attr->offset = pull_attr->offset = i;
386 val_dev_attr = &val_attr->dev_attr;
387 pull_dev_attr = &pull_attr->dev_attr;
389 sysfs_attr_init(&val_dev_attr->attr);
390 sysfs_attr_init(&pull_dev_attr->attr);
392 val_dev_attr->attr.name = "value";
393 pull_dev_attr->attr.name = "pull";
395 val_dev_attr->attr.mode = pull_dev_attr->attr.mode = 0644;
397 val_dev_attr->show = gpio_sim_sysfs_val_show;
398 val_dev_attr->store = gpio_sim_sysfs_val_store;
399 pull_dev_attr->show = gpio_sim_sysfs_pull_show;
400 pull_dev_attr->store = gpio_sim_sysfs_pull_store;
402 attrs[0] = &val_dev_attr->attr;
403 attrs[1] = &pull_dev_attr->attr;
405 attr_group->attrs = attrs;
406 chip->attr_groups[i] = attr_group;
409 ret = sysfs_create_groups(&chip->dev->kobj, chip->attr_groups);
410 if (ret)
411 return ret;
413 return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip);
416 static int gpio_sim_dev_match_fwnode(struct device *dev, void *data)
418 return device_match_fwnode(dev, data);
421 static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev)
423 struct gpio_sim_chip *chip;
424 struct gpio_chip *gc;
425 const char *label;
426 u32 num_lines;
427 int ret;
429 ret = fwnode_property_read_u32(swnode, "ngpios", &num_lines);
430 if (ret)
431 return ret;
433 if (num_lines > GPIO_SIM_NGPIO_MAX)
434 return -ERANGE;
436 ret = fwnode_property_read_string(swnode, "gpio-sim,label", &label);
437 if (ret) {
438 label = devm_kasprintf(dev, GFP_KERNEL, "%s:%pfwP",
439 dev_name(dev), swnode);
440 if (!label)
441 return -ENOMEM;
444 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
445 if (!chip)
446 return -ENOMEM;
448 chip->request_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
449 if (!chip->request_map)
450 return -ENOMEM;
452 chip->direction_map = devm_bitmap_alloc(dev, num_lines, GFP_KERNEL);
453 if (!chip->direction_map)
454 return -ENOMEM;
456 /* Default to input mode. */
457 bitmap_fill(chip->direction_map, num_lines);
459 chip->value_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
460 if (!chip->value_map)
461 return -ENOMEM;
463 chip->pull_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
464 if (!chip->pull_map)
465 return -ENOMEM;
467 chip->irq_sim = devm_irq_domain_create_sim_full(dev, swnode, num_lines,
468 &gpio_sim_irq_sim_ops,
469 chip);
470 if (IS_ERR(chip->irq_sim))
471 return PTR_ERR(chip->irq_sim);
473 ret = devm_add_action_or_reset(dev, gpio_sim_dispose_mappings, chip);
474 if (ret)
475 return ret;
477 ret = devm_mutex_init(dev, &chip->lock);
478 if (ret)
479 return ret;
481 gc = &chip->gc;
482 gc->base = -1;
483 gc->ngpio = num_lines;
484 gc->label = label;
485 gc->owner = THIS_MODULE;
486 gc->parent = dev;
487 gc->fwnode = swnode;
488 gc->get = gpio_sim_get;
489 gc->set = gpio_sim_set;
490 gc->get_multiple = gpio_sim_get_multiple;
491 gc->set_multiple = gpio_sim_set_multiple;
492 gc->direction_output = gpio_sim_direction_output;
493 gc->direction_input = gpio_sim_direction_input;
494 gc->get_direction = gpio_sim_get_direction;
495 gc->set_config = gpio_sim_set_config;
496 gc->to_irq = gpio_sim_to_irq;
497 gc->request = gpio_sim_request;
498 gc->free = gpio_sim_free;
499 gc->dbg_show = PTR_IF(IS_ENABLED(CONFIG_DEBUG_FS), gpio_sim_dbg_show);
500 gc->can_sleep = true;
502 ret = devm_gpiochip_add_data(dev, gc, chip);
503 if (ret)
504 return ret;
506 chip->dev = device_find_child(dev, swnode, gpio_sim_dev_match_fwnode);
507 if (!chip->dev)
508 return -ENODEV;
510 ret = devm_add_action_or_reset(dev, gpio_sim_put_device, chip->dev);
511 if (ret)
512 return ret;
514 /* Used by sysfs callbacks. */
515 dev_set_drvdata(chip->dev, chip);
517 return gpio_sim_setup_sysfs(chip);
520 static int gpio_sim_probe(struct platform_device *pdev)
522 struct device *dev = &pdev->dev;
523 struct fwnode_handle *swnode;
524 int ret;
526 device_for_each_child_node(dev, swnode) {
527 ret = gpio_sim_add_bank(swnode, dev);
528 if (ret) {
529 fwnode_handle_put(swnode);
530 return ret;
534 return 0;
537 static const struct of_device_id gpio_sim_of_match[] = {
538 { .compatible = "gpio-simulator" },
541 MODULE_DEVICE_TABLE(of, gpio_sim_of_match);
543 static struct platform_driver gpio_sim_driver = {
544 .driver = {
545 .name = "gpio-sim",
546 .of_match_table = gpio_sim_of_match,
548 .probe = gpio_sim_probe,
551 struct gpio_sim_device {
552 struct config_group group;
555 * If pdev is NULL, the device is 'pending' (waiting for configuration).
556 * Once the pointer is assigned, the device has been created and the
557 * item is 'live'.
559 struct platform_device *pdev;
560 int id;
563 * Each configfs filesystem operation is protected with the subsystem
564 * mutex. Each separate attribute is protected with the buffer mutex.
565 * This structure however can be modified by callbacks of different
566 * attributes so we need another lock.
568 * We use this lock for protecting all data structures owned by this
569 * object too.
571 struct mutex lock;
574 * This is used to synchronously wait for the driver's probe to complete
575 * and notify the user-space about any errors.
577 struct notifier_block bus_notifier;
578 struct completion probe_completion;
579 bool driver_bound;
581 struct gpiod_hog *hogs;
583 struct list_head bank_list;
586 /* This is called with dev->lock already taken. */
587 static int gpio_sim_bus_notifier_call(struct notifier_block *nb,
588 unsigned long action, void *data)
590 struct gpio_sim_device *simdev = container_of(nb,
591 struct gpio_sim_device,
592 bus_notifier);
593 struct device *dev = data;
594 char devname[32];
596 snprintf(devname, sizeof(devname), "gpio-sim.%u", simdev->id);
598 if (!device_match_name(dev, devname))
599 return NOTIFY_DONE;
601 if (action == BUS_NOTIFY_BOUND_DRIVER)
602 simdev->driver_bound = true;
603 else if (action == BUS_NOTIFY_DRIVER_NOT_BOUND)
604 simdev->driver_bound = false;
605 else
606 return NOTIFY_DONE;
608 complete(&simdev->probe_completion);
610 return NOTIFY_OK;
613 static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item)
615 struct config_group *group = to_config_group(item);
617 return container_of(group, struct gpio_sim_device, group);
620 struct gpio_sim_bank {
621 struct config_group group;
624 * We could have used the ci_parent field of the config_item but
625 * configfs is stupid and calls the item's release callback after
626 * already having cleared the parent pointer even though the parent
627 * is guaranteed to survive the child...
629 * So we need to store the pointer to the parent struct here. We can
630 * dereference it anywhere we need with no checks and no locking as
631 * it's guaranteed to survive the children and protected by configfs
632 * locks.
634 * Same for other structures.
636 struct gpio_sim_device *parent;
637 struct list_head siblings;
639 char *label;
640 unsigned int num_lines;
642 struct list_head line_list;
644 struct fwnode_handle *swnode;
647 static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item)
649 struct config_group *group = to_config_group(item);
651 return container_of(group, struct gpio_sim_bank, group);
654 static bool gpio_sim_bank_has_label(struct gpio_sim_bank *bank)
656 return bank->label && *bank->label;
659 static struct gpio_sim_device *
660 gpio_sim_bank_get_device(struct gpio_sim_bank *bank)
662 return bank->parent;
665 struct gpio_sim_hog;
667 struct gpio_sim_line {
668 struct config_group group;
670 struct gpio_sim_bank *parent;
671 struct list_head siblings;
673 unsigned int offset;
674 char *name;
676 /* There can only be one hog per line. */
677 struct gpio_sim_hog *hog;
680 static struct gpio_sim_line *to_gpio_sim_line(struct config_item *item)
682 struct config_group *group = to_config_group(item);
684 return container_of(group, struct gpio_sim_line, group);
687 static struct gpio_sim_device *
688 gpio_sim_line_get_device(struct gpio_sim_line *line)
690 struct gpio_sim_bank *bank = line->parent;
692 return gpio_sim_bank_get_device(bank);
695 struct gpio_sim_hog {
696 struct config_item item;
697 struct gpio_sim_line *parent;
699 char *name;
700 int dir;
703 static struct gpio_sim_hog *to_gpio_sim_hog(struct config_item *item)
705 return container_of(item, struct gpio_sim_hog, item);
708 static struct gpio_sim_device *gpio_sim_hog_get_device(struct gpio_sim_hog *hog)
710 struct gpio_sim_line *line = hog->parent;
712 return gpio_sim_line_get_device(line);
715 static bool gpio_sim_device_is_live(struct gpio_sim_device *dev)
717 lockdep_assert_held(&dev->lock);
719 return !!dev->pdev;
722 static char *gpio_sim_strdup_trimmed(const char *str, size_t count)
724 char *trimmed;
726 trimmed = kstrndup(skip_spaces(str), count, GFP_KERNEL);
727 if (!trimmed)
728 return NULL;
730 return strim(trimmed);
733 static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item,
734 char *page)
736 struct gpio_sim_device *dev = to_gpio_sim_device(item);
737 struct platform_device *pdev;
739 guard(mutex)(&dev->lock);
741 pdev = dev->pdev;
742 if (pdev)
743 return sprintf(page, "%s\n", dev_name(&pdev->dev));
745 return sprintf(page, "gpio-sim.%d\n", dev->id);
748 CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name);
750 static ssize_t
751 gpio_sim_device_config_live_show(struct config_item *item, char *page)
753 struct gpio_sim_device *dev = to_gpio_sim_device(item);
754 bool live;
756 scoped_guard(mutex, &dev->lock)
757 live = gpio_sim_device_is_live(dev);
759 return sprintf(page, "%c\n", live ? '1' : '0');
762 static unsigned int gpio_sim_get_line_names_size(struct gpio_sim_bank *bank)
764 struct gpio_sim_line *line;
765 unsigned int size = 0;
767 list_for_each_entry(line, &bank->line_list, siblings) {
768 if (!line->name || (line->offset >= bank->num_lines))
769 continue;
771 size = max(size, line->offset + 1);
774 return size;
777 static void
778 gpio_sim_set_line_names(struct gpio_sim_bank *bank, char **line_names)
780 struct gpio_sim_line *line;
782 list_for_each_entry(line, &bank->line_list, siblings) {
783 if (!line->name || (line->offset >= bank->num_lines))
784 continue;
786 line_names[line->offset] = line->name;
790 static void gpio_sim_remove_hogs(struct gpio_sim_device *dev)
792 struct gpiod_hog *hog;
794 if (!dev->hogs)
795 return;
797 gpiod_remove_hogs(dev->hogs);
799 for (hog = dev->hogs; hog->chip_label; hog++) {
800 kfree(hog->chip_label);
801 kfree(hog->line_name);
804 kfree(dev->hogs);
805 dev->hogs = NULL;
808 static int gpio_sim_add_hogs(struct gpio_sim_device *dev)
810 unsigned int num_hogs = 0, idx = 0;
811 struct gpio_sim_bank *bank;
812 struct gpio_sim_line *line;
813 struct gpiod_hog *hog;
815 list_for_each_entry(bank, &dev->bank_list, siblings) {
816 list_for_each_entry(line, &bank->line_list, siblings) {
817 if (line->offset >= bank->num_lines)
818 continue;
820 if (line->hog)
821 num_hogs++;
825 if (!num_hogs)
826 return 0;
828 /* Allocate one more for the sentinel. */
829 dev->hogs = kcalloc(num_hogs + 1, sizeof(*dev->hogs), GFP_KERNEL);
830 if (!dev->hogs)
831 return -ENOMEM;
833 list_for_each_entry(bank, &dev->bank_list, siblings) {
834 list_for_each_entry(line, &bank->line_list, siblings) {
835 if (line->offset >= bank->num_lines)
836 continue;
838 if (!line->hog)
839 continue;
841 hog = &dev->hogs[idx++];
844 * We need to make this string manually because at this
845 * point the device doesn't exist yet and so dev_name()
846 * is not available.
848 if (gpio_sim_bank_has_label(bank))
849 hog->chip_label = kstrdup(bank->label,
850 GFP_KERNEL);
851 else
852 hog->chip_label = kasprintf(GFP_KERNEL,
853 "gpio-sim.%u:%pfwP",
854 dev->id,
855 bank->swnode);
856 if (!hog->chip_label) {
857 gpio_sim_remove_hogs(dev);
858 return -ENOMEM;
862 * We need to duplicate this because the hog config
863 * item can be removed at any time (and we can't block
864 * it) and gpiolib doesn't make a deep copy of the hog
865 * data.
867 if (line->hog->name) {
868 hog->line_name = kstrdup(line->hog->name,
869 GFP_KERNEL);
870 if (!hog->line_name) {
871 gpio_sim_remove_hogs(dev);
872 return -ENOMEM;
876 hog->chip_hwnum = line->offset;
877 hog->dflags = line->hog->dir;
881 gpiod_add_hogs(dev->hogs);
883 return 0;
886 static struct fwnode_handle *
887 gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
888 struct fwnode_handle *parent)
890 struct property_entry properties[GPIO_SIM_PROP_MAX];
891 unsigned int prop_idx = 0, line_names_size;
892 char **line_names __free(kfree) = NULL;
894 memset(properties, 0, sizeof(properties));
896 properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines);
898 if (gpio_sim_bank_has_label(bank))
899 properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
900 bank->label);
902 line_names_size = gpio_sim_get_line_names_size(bank);
903 if (line_names_size) {
904 line_names = kcalloc(line_names_size, sizeof(*line_names),
905 GFP_KERNEL);
906 if (!line_names)
907 return ERR_PTR(-ENOMEM);
909 gpio_sim_set_line_names(bank, line_names);
911 properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
912 "gpio-line-names",
913 line_names, line_names_size);
916 return fwnode_create_software_node(properties, parent);
919 static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode)
921 struct fwnode_handle *child;
923 fwnode_for_each_child_node(swnode, child)
924 fwnode_remove_software_node(child);
926 fwnode_remove_software_node(swnode);
929 static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev)
931 struct gpio_sim_bank *this, *pos;
933 list_for_each_entry(this, &dev->bank_list, siblings) {
934 list_for_each_entry(pos, &dev->bank_list, siblings) {
935 if (this == pos || (!this->label || !pos->label))
936 continue;
938 if (strcmp(this->label, pos->label) == 0)
939 return true;
943 return false;
946 static int gpio_sim_device_activate(struct gpio_sim_device *dev)
948 struct platform_device_info pdevinfo;
949 struct fwnode_handle *swnode;
950 struct platform_device *pdev;
951 struct gpio_sim_bank *bank;
952 int ret;
954 lockdep_assert_held(&dev->lock);
956 if (list_empty(&dev->bank_list))
957 return -ENODATA;
960 * Non-unique GPIO device labels are a corner-case we don't support
961 * as it would interfere with machine hogging mechanism and has little
962 * use in real life.
964 if (gpio_sim_bank_labels_non_unique(dev))
965 return -EINVAL;
967 memset(&pdevinfo, 0, sizeof(pdevinfo));
969 swnode = fwnode_create_software_node(NULL, NULL);
970 if (IS_ERR(swnode))
971 return PTR_ERR(swnode);
973 list_for_each_entry(bank, &dev->bank_list, siblings) {
974 bank->swnode = gpio_sim_make_bank_swnode(bank, swnode);
975 if (IS_ERR(bank->swnode)) {
976 ret = PTR_ERR(bank->swnode);
977 gpio_sim_remove_swnode_recursive(swnode);
978 return ret;
982 ret = gpio_sim_add_hogs(dev);
983 if (ret) {
984 gpio_sim_remove_swnode_recursive(swnode);
985 return ret;
988 pdevinfo.name = "gpio-sim";
989 pdevinfo.fwnode = swnode;
990 pdevinfo.id = dev->id;
992 reinit_completion(&dev->probe_completion);
993 dev->driver_bound = false;
994 bus_register_notifier(&platform_bus_type, &dev->bus_notifier);
996 pdev = platform_device_register_full(&pdevinfo);
997 if (IS_ERR(pdev)) {
998 bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
999 gpio_sim_remove_hogs(dev);
1000 gpio_sim_remove_swnode_recursive(swnode);
1001 return PTR_ERR(pdev);
1004 wait_for_completion(&dev->probe_completion);
1005 bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
1007 if (!dev->driver_bound) {
1008 /* Probe failed, check kernel log. */
1009 platform_device_unregister(pdev);
1010 gpio_sim_remove_hogs(dev);
1011 gpio_sim_remove_swnode_recursive(swnode);
1012 return -ENXIO;
1015 dev->pdev = pdev;
1017 return 0;
1020 static void gpio_sim_device_deactivate(struct gpio_sim_device *dev)
1022 struct fwnode_handle *swnode;
1024 lockdep_assert_held(&dev->lock);
1026 swnode = dev_fwnode(&dev->pdev->dev);
1027 platform_device_unregister(dev->pdev);
1028 gpio_sim_remove_hogs(dev);
1029 gpio_sim_remove_swnode_recursive(swnode);
1030 dev->pdev = NULL;
1033 static ssize_t
1034 gpio_sim_device_config_live_store(struct config_item *item,
1035 const char *page, size_t count)
1037 struct gpio_sim_device *dev = to_gpio_sim_device(item);
1038 bool live;
1039 int ret;
1041 ret = kstrtobool(page, &live);
1042 if (ret)
1043 return ret;
1045 guard(mutex)(&dev->lock);
1047 if (live == gpio_sim_device_is_live(dev))
1048 ret = -EPERM;
1049 else if (live)
1050 ret = gpio_sim_device_activate(dev);
1051 else
1052 gpio_sim_device_deactivate(dev);
1054 return ret ?: count;
1057 CONFIGFS_ATTR(gpio_sim_device_config_, live);
1059 static struct configfs_attribute *gpio_sim_device_config_attrs[] = {
1060 &gpio_sim_device_config_attr_dev_name,
1061 &gpio_sim_device_config_attr_live,
1062 NULL
1065 struct gpio_sim_chip_name_ctx {
1066 struct fwnode_handle *swnode;
1067 char *page;
1070 static int gpio_sim_emit_chip_name(struct device *dev, void *data)
1072 struct gpio_sim_chip_name_ctx *ctx = data;
1074 /* This would be the sysfs device exported in /sys/class/gpio. */
1075 if (dev->class)
1076 return 0;
1078 if (device_match_fwnode(dev, ctx->swnode))
1079 return sprintf(ctx->page, "%s\n", dev_name(dev));
1081 return 0;
1084 static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item,
1085 char *page)
1087 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1088 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1089 struct gpio_sim_chip_name_ctx ctx = { bank->swnode, page };
1091 guard(mutex)(&dev->lock);
1093 if (gpio_sim_device_is_live(dev))
1094 return device_for_each_child(&dev->pdev->dev, &ctx,
1095 gpio_sim_emit_chip_name);
1097 return sprintf(page, "none\n");
1100 CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name);
1102 static ssize_t
1103 gpio_sim_bank_config_label_show(struct config_item *item, char *page)
1105 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1106 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1108 guard(mutex)(&dev->lock);
1110 return sprintf(page, "%s\n", bank->label ?: "");
1113 static ssize_t gpio_sim_bank_config_label_store(struct config_item *item,
1114 const char *page, size_t count)
1116 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1117 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1118 char *trimmed;
1120 guard(mutex)(&dev->lock);
1122 if (gpio_sim_device_is_live(dev))
1123 return -EBUSY;
1125 trimmed = gpio_sim_strdup_trimmed(page, count);
1126 if (!trimmed)
1127 return -ENOMEM;
1129 kfree(bank->label);
1130 bank->label = trimmed;
1132 return count;
1135 CONFIGFS_ATTR(gpio_sim_bank_config_, label);
1137 static ssize_t
1138 gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page)
1140 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1141 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1143 guard(mutex)(&dev->lock);
1145 return sprintf(page, "%u\n", bank->num_lines);
1148 static ssize_t
1149 gpio_sim_bank_config_num_lines_store(struct config_item *item,
1150 const char *page, size_t count)
1152 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1153 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1154 unsigned int num_lines;
1155 int ret;
1157 ret = kstrtouint(page, 0, &num_lines);
1158 if (ret)
1159 return ret;
1161 if (num_lines == 0)
1162 return -EINVAL;
1164 guard(mutex)(&dev->lock);
1166 if (gpio_sim_device_is_live(dev))
1167 return -EBUSY;
1169 bank->num_lines = num_lines;
1171 return count;
1174 CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines);
1176 static struct configfs_attribute *gpio_sim_bank_config_attrs[] = {
1177 &gpio_sim_bank_config_attr_chip_name,
1178 &gpio_sim_bank_config_attr_label,
1179 &gpio_sim_bank_config_attr_num_lines,
1180 NULL
1183 static ssize_t
1184 gpio_sim_line_config_name_show(struct config_item *item, char *page)
1186 struct gpio_sim_line *line = to_gpio_sim_line(item);
1187 struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1189 guard(mutex)(&dev->lock);
1191 return sprintf(page, "%s\n", line->name ?: "");
1194 static ssize_t gpio_sim_line_config_name_store(struct config_item *item,
1195 const char *page, size_t count)
1197 struct gpio_sim_line *line = to_gpio_sim_line(item);
1198 struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1199 char *trimmed;
1201 guard(mutex)(&dev->lock);
1203 if (gpio_sim_device_is_live(dev))
1204 return -EBUSY;
1206 trimmed = gpio_sim_strdup_trimmed(page, count);
1207 if (!trimmed)
1208 return -ENOMEM;
1210 kfree(line->name);
1211 line->name = trimmed;
1213 return count;
1216 CONFIGFS_ATTR(gpio_sim_line_config_, name);
1218 static struct configfs_attribute *gpio_sim_line_config_attrs[] = {
1219 &gpio_sim_line_config_attr_name,
1220 NULL
1223 static ssize_t gpio_sim_hog_config_name_show(struct config_item *item,
1224 char *page)
1226 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1227 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1229 guard(mutex)(&dev->lock);
1231 return sprintf(page, "%s\n", hog->name ?: "");
1234 static ssize_t gpio_sim_hog_config_name_store(struct config_item *item,
1235 const char *page, size_t count)
1237 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1238 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1239 char *trimmed;
1241 guard(mutex)(&dev->lock);
1243 if (gpio_sim_device_is_live(dev))
1244 return -EBUSY;
1246 trimmed = gpio_sim_strdup_trimmed(page, count);
1247 if (!trimmed)
1248 return -ENOMEM;
1250 kfree(hog->name);
1251 hog->name = trimmed;
1253 return count;
1256 CONFIGFS_ATTR(gpio_sim_hog_config_, name);
1258 static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item,
1259 char *page)
1261 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1262 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1263 char *repr;
1264 int dir;
1266 scoped_guard(mutex, &dev->lock)
1267 dir = hog->dir;
1269 switch (dir) {
1270 case GPIOD_IN:
1271 repr = "input";
1272 break;
1273 case GPIOD_OUT_HIGH:
1274 repr = "output-high";
1275 break;
1276 case GPIOD_OUT_LOW:
1277 repr = "output-low";
1278 break;
1279 default:
1280 /* This would be a programmer bug. */
1281 WARN(1, "Unexpected hog direction value: %d", dir);
1282 return -EINVAL;
1285 return sprintf(page, "%s\n", repr);
1288 static ssize_t
1289 gpio_sim_hog_config_direction_store(struct config_item *item,
1290 const char *page, size_t count)
1292 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1293 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1294 int dir;
1296 guard(mutex)(&dev->lock);
1298 if (gpio_sim_device_is_live(dev))
1299 return -EBUSY;
1301 if (sysfs_streq(page, "input"))
1302 dir = GPIOD_IN;
1303 else if (sysfs_streq(page, "output-high"))
1304 dir = GPIOD_OUT_HIGH;
1305 else if (sysfs_streq(page, "output-low"))
1306 dir = GPIOD_OUT_LOW;
1307 else
1308 return -EINVAL;
1310 hog->dir = dir;
1312 return count;
1315 CONFIGFS_ATTR(gpio_sim_hog_config_, direction);
1317 static struct configfs_attribute *gpio_sim_hog_config_attrs[] = {
1318 &gpio_sim_hog_config_attr_name,
1319 &gpio_sim_hog_config_attr_direction,
1320 NULL
1323 static void gpio_sim_hog_config_item_release(struct config_item *item)
1325 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1326 struct gpio_sim_line *line = hog->parent;
1327 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1329 scoped_guard(mutex, &dev->lock)
1330 line->hog = NULL;
1332 kfree(hog->name);
1333 kfree(hog);
1336 static struct configfs_item_operations gpio_sim_hog_config_item_ops = {
1337 .release = gpio_sim_hog_config_item_release,
1340 static const struct config_item_type gpio_sim_hog_config_type = {
1341 .ct_item_ops = &gpio_sim_hog_config_item_ops,
1342 .ct_attrs = gpio_sim_hog_config_attrs,
1343 .ct_owner = THIS_MODULE,
1346 static struct config_item *
1347 gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name)
1349 struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item);
1350 struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1351 struct gpio_sim_hog *hog;
1353 if (strcmp(name, "hog") != 0)
1354 return ERR_PTR(-EINVAL);
1356 guard(mutex)(&dev->lock);
1358 hog = kzalloc(sizeof(*hog), GFP_KERNEL);
1359 if (!hog)
1360 return ERR_PTR(-ENOMEM);
1362 config_item_init_type_name(&hog->item, name,
1363 &gpio_sim_hog_config_type);
1365 hog->dir = GPIOD_IN;
1366 hog->name = NULL;
1367 hog->parent = line;
1368 line->hog = hog;
1370 return &hog->item;
1373 static void gpio_sim_line_config_group_release(struct config_item *item)
1375 struct gpio_sim_line *line = to_gpio_sim_line(item);
1376 struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1378 scoped_guard(mutex, &dev->lock)
1379 list_del(&line->siblings);
1381 kfree(line->name);
1382 kfree(line);
1385 static struct configfs_item_operations gpio_sim_line_config_item_ops = {
1386 .release = gpio_sim_line_config_group_release,
1389 static struct configfs_group_operations gpio_sim_line_config_group_ops = {
1390 .make_item = gpio_sim_line_config_make_hog_item,
1393 static const struct config_item_type gpio_sim_line_config_type = {
1394 .ct_item_ops = &gpio_sim_line_config_item_ops,
1395 .ct_group_ops = &gpio_sim_line_config_group_ops,
1396 .ct_attrs = gpio_sim_line_config_attrs,
1397 .ct_owner = THIS_MODULE,
1400 static struct config_group *
1401 gpio_sim_bank_config_make_line_group(struct config_group *group,
1402 const char *name)
1404 struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item);
1405 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1406 struct gpio_sim_line *line;
1407 unsigned int offset;
1408 int ret, nchar;
1410 ret = sscanf(name, "line%u%n", &offset, &nchar);
1411 if (ret != 1 || nchar != strlen(name))
1412 return ERR_PTR(-EINVAL);
1414 guard(mutex)(&dev->lock);
1416 if (gpio_sim_device_is_live(dev))
1417 return ERR_PTR(-EBUSY);
1419 line = kzalloc(sizeof(*line), GFP_KERNEL);
1420 if (!line)
1421 return ERR_PTR(-ENOMEM);
1423 config_group_init_type_name(&line->group, name,
1424 &gpio_sim_line_config_type);
1426 line->parent = bank;
1427 line->offset = offset;
1428 list_add_tail(&line->siblings, &bank->line_list);
1430 return &line->group;
1433 static void gpio_sim_bank_config_group_release(struct config_item *item)
1435 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1436 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1438 scoped_guard(mutex, &dev->lock)
1439 list_del(&bank->siblings);
1441 kfree(bank->label);
1442 kfree(bank);
1445 static struct configfs_item_operations gpio_sim_bank_config_item_ops = {
1446 .release = gpio_sim_bank_config_group_release,
1449 static struct configfs_group_operations gpio_sim_bank_config_group_ops = {
1450 .make_group = gpio_sim_bank_config_make_line_group,
1453 static const struct config_item_type gpio_sim_bank_config_group_type = {
1454 .ct_item_ops = &gpio_sim_bank_config_item_ops,
1455 .ct_group_ops = &gpio_sim_bank_config_group_ops,
1456 .ct_attrs = gpio_sim_bank_config_attrs,
1457 .ct_owner = THIS_MODULE,
1460 static struct config_group *
1461 gpio_sim_device_config_make_bank_group(struct config_group *group,
1462 const char *name)
1464 struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item);
1465 struct gpio_sim_bank *bank;
1467 guard(mutex)(&dev->lock);
1469 if (gpio_sim_device_is_live(dev))
1470 return ERR_PTR(-EBUSY);
1472 bank = kzalloc(sizeof(*bank), GFP_KERNEL);
1473 if (!bank)
1474 return ERR_PTR(-ENOMEM);
1476 config_group_init_type_name(&bank->group, name,
1477 &gpio_sim_bank_config_group_type);
1478 bank->num_lines = 1;
1479 bank->parent = dev;
1480 INIT_LIST_HEAD(&bank->line_list);
1481 list_add_tail(&bank->siblings, &dev->bank_list);
1483 return &bank->group;
1486 static void gpio_sim_device_config_group_release(struct config_item *item)
1488 struct gpio_sim_device *dev = to_gpio_sim_device(item);
1490 scoped_guard(mutex, &dev->lock) {
1491 if (gpio_sim_device_is_live(dev))
1492 gpio_sim_device_deactivate(dev);
1495 mutex_destroy(&dev->lock);
1496 ida_free(&gpio_sim_ida, dev->id);
1497 kfree(dev);
1500 static struct configfs_item_operations gpio_sim_device_config_item_ops = {
1501 .release = gpio_sim_device_config_group_release,
1504 static struct configfs_group_operations gpio_sim_device_config_group_ops = {
1505 .make_group = gpio_sim_device_config_make_bank_group,
1508 static const struct config_item_type gpio_sim_device_config_group_type = {
1509 .ct_item_ops = &gpio_sim_device_config_item_ops,
1510 .ct_group_ops = &gpio_sim_device_config_group_ops,
1511 .ct_attrs = gpio_sim_device_config_attrs,
1512 .ct_owner = THIS_MODULE,
1515 static struct config_group *
1516 gpio_sim_config_make_device_group(struct config_group *group, const char *name)
1518 int id;
1520 struct gpio_sim_device *dev __free(kfree) = kzalloc(sizeof(*dev),
1521 GFP_KERNEL);
1522 if (!dev)
1523 return ERR_PTR(-ENOMEM);
1525 id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
1526 if (id < 0)
1527 return ERR_PTR(id);
1529 config_group_init_type_name(&dev->group, name,
1530 &gpio_sim_device_config_group_type);
1531 dev->id = id;
1532 mutex_init(&dev->lock);
1533 INIT_LIST_HEAD(&dev->bank_list);
1535 dev->bus_notifier.notifier_call = gpio_sim_bus_notifier_call;
1536 init_completion(&dev->probe_completion);
1538 return &no_free_ptr(dev)->group;
1541 static struct configfs_group_operations gpio_sim_config_group_ops = {
1542 .make_group = gpio_sim_config_make_device_group,
1545 static const struct config_item_type gpio_sim_config_type = {
1546 .ct_group_ops = &gpio_sim_config_group_ops,
1547 .ct_owner = THIS_MODULE,
1550 static struct configfs_subsystem gpio_sim_config_subsys = {
1551 .su_group = {
1552 .cg_item = {
1553 .ci_namebuf = "gpio-sim",
1554 .ci_type = &gpio_sim_config_type,
1559 static int __init gpio_sim_init(void)
1561 int ret;
1563 ret = platform_driver_register(&gpio_sim_driver);
1564 if (ret) {
1565 pr_err("Error %d while registering the platform driver\n", ret);
1566 return ret;
1569 config_group_init(&gpio_sim_config_subsys.su_group);
1570 mutex_init(&gpio_sim_config_subsys.su_mutex);
1571 ret = configfs_register_subsystem(&gpio_sim_config_subsys);
1572 if (ret) {
1573 pr_err("Error %d while registering the configfs subsystem %s\n",
1574 ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf);
1575 mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1576 platform_driver_unregister(&gpio_sim_driver);
1577 return ret;
1580 return 0;
1582 module_init(gpio_sim_init);
1584 static void __exit gpio_sim_exit(void)
1586 configfs_unregister_subsystem(&gpio_sim_config_subsys);
1587 mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1588 platform_driver_unregister(&gpio_sim_driver);
1590 module_exit(gpio_sim_exit);
1592 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
1593 MODULE_DESCRIPTION("GPIO Simulator Module");
1594 MODULE_LICENSE("GPL");