1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * GPIO testing driver based on configfs.
5 * Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/bitmap.h>
11 #include <linux/cleanup.h>
12 #include <linux/completion.h>
13 #include <linux/configfs.h>
14 #include <linux/device.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/gpio/machine.h>
17 #include <linux/idr.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/irq_sim.h>
21 #include <linux/list.h>
22 #include <linux/minmax.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/notifier.h>
27 #include <linux/platform_device.h>
28 #include <linux/property.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/string_helpers.h>
32 #include <linux/sysfs.h>
36 #define GPIO_SIM_NGPIO_MAX 1024
37 #define GPIO_SIM_PROP_MAX 4 /* Max 3 properties + sentinel. */
38 #define GPIO_SIM_NUM_ATTRS 3 /* value, pull and sentinel */
40 static DEFINE_IDA(gpio_sim_ida);
42 struct gpio_sim_chip {
44 unsigned long *direction_map;
45 unsigned long *value_map;
46 unsigned long *pull_map;
47 struct irq_domain *irq_sim;
49 const struct attribute_group **attr_groups;
52 struct gpio_sim_attribute {
53 struct device_attribute dev_attr;
57 static struct gpio_sim_attribute *
58 to_gpio_sim_attr(struct device_attribute *dev_attr)
60 return container_of(dev_attr, struct gpio_sim_attribute, dev_attr);
63 static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
64 unsigned int offset, int value)
66 int irq, irq_type, ret;
67 struct gpio_desc *desc;
71 desc = &gc->gpiodev->descs[offset];
73 guard(mutex)(&chip->lock);
75 if (test_bit(FLAG_REQUESTED, &desc->flags) &&
76 !test_bit(FLAG_IS_OUT, &desc->flags)) {
77 if (value == !!test_bit(offset, chip->value_map))
81 * This is fine - it just means, nobody is listening
82 * for interrupts on this line, otherwise
83 * irq_create_mapping() would have been called from
84 * the to_irq() callback.
86 irq = irq_find_mapping(chip->irq_sim, offset);
90 irq_type = irq_get_trigger_type(irq);
92 if ((value && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
93 (!value && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
94 ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
102 /* Change the value unless we're actively driving the line. */
103 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
104 !test_bit(FLAG_IS_OUT, &desc->flags))
105 __assign_bit(offset, chip->value_map, value);
108 __assign_bit(offset, chip->pull_map, value);
112 static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset)
114 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
116 guard(mutex)(&chip->lock);
118 return !!test_bit(offset, chip->value_map);
121 static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value)
123 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
125 scoped_guard(mutex, &chip->lock)
126 __assign_bit(offset, chip->value_map, value);
129 static int gpio_sim_get_multiple(struct gpio_chip *gc,
130 unsigned long *mask, unsigned long *bits)
132 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
134 scoped_guard(mutex, &chip->lock)
135 bitmap_replace(bits, bits, chip->value_map, mask, gc->ngpio);
140 static void gpio_sim_set_multiple(struct gpio_chip *gc,
141 unsigned long *mask, unsigned long *bits)
143 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
145 scoped_guard(mutex, &chip->lock)
146 bitmap_replace(chip->value_map, chip->value_map, bits, mask,
150 static int gpio_sim_direction_output(struct gpio_chip *gc,
151 unsigned int offset, int value)
153 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
155 scoped_guard(mutex, &chip->lock) {
156 __clear_bit(offset, chip->direction_map);
157 __assign_bit(offset, chip->value_map, value);
163 static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset)
165 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
167 scoped_guard(mutex, &chip->lock)
168 __set_bit(offset, chip->direction_map);
173 static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset)
175 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
178 scoped_guard(mutex, &chip->lock)
179 direction = !!test_bit(offset, chip->direction_map);
181 return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
184 static int gpio_sim_set_config(struct gpio_chip *gc,
185 unsigned int offset, unsigned long config)
187 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
189 switch (pinconf_to_config_param(config)) {
190 case PIN_CONFIG_BIAS_PULL_UP:
191 return gpio_sim_apply_pull(chip, offset, 1);
192 case PIN_CONFIG_BIAS_PULL_DOWN:
193 return gpio_sim_apply_pull(chip, offset, 0);
201 static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset)
203 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
205 return irq_create_mapping(chip->irq_sim, offset);
208 static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset)
210 struct gpio_sim_chip *chip = gpiochip_get_data(gc);
212 scoped_guard(mutex, &chip->lock)
213 __assign_bit(offset, chip->value_map,
214 !!test_bit(offset, chip->pull_map));
217 static ssize_t gpio_sim_sysfs_val_show(struct device *dev,
218 struct device_attribute *attr, char *buf)
220 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
221 struct gpio_sim_chip *chip = dev_get_drvdata(dev);
224 scoped_guard(mutex, &chip->lock)
225 val = !!test_bit(line_attr->offset, chip->value_map);
227 return sysfs_emit(buf, "%d\n", val);
230 static ssize_t gpio_sim_sysfs_val_store(struct device *dev,
231 struct device_attribute *attr,
232 const char *buf, size_t count)
235 * Not assigning this function will result in write() returning -EIO
236 * which is confusing. Return -EPERM explicitly.
241 static const char *const gpio_sim_sysfs_pull_strings[] = {
246 static ssize_t gpio_sim_sysfs_pull_show(struct device *dev,
247 struct device_attribute *attr,
250 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
251 struct gpio_sim_chip *chip = dev_get_drvdata(dev);
254 scoped_guard(mutex, &chip->lock)
255 pull = !!test_bit(line_attr->offset, chip->pull_map);
257 return sysfs_emit(buf, "%s\n", gpio_sim_sysfs_pull_strings[pull]);
260 static ssize_t gpio_sim_sysfs_pull_store(struct device *dev,
261 struct device_attribute *attr,
262 const char *buf, size_t len)
264 struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
265 struct gpio_sim_chip *chip = dev_get_drvdata(dev);
268 pull = sysfs_match_string(gpio_sim_sysfs_pull_strings, buf);
272 ret = gpio_sim_apply_pull(chip, line_attr->offset, pull);
279 static void gpio_sim_mutex_destroy(void *data)
281 struct mutex *lock = data;
286 static void gpio_sim_dispose_mappings(void *data)
288 struct gpio_sim_chip *chip = data;
291 for (i = 0; i < chip->gc.ngpio; i++)
292 irq_dispose_mapping(irq_find_mapping(chip->irq_sim, i));
295 static void gpio_sim_sysfs_remove(void *data)
297 struct gpio_sim_chip *chip = data;
299 sysfs_remove_groups(&chip->gc.gpiodev->dev.kobj, chip->attr_groups);
302 static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip)
304 struct device_attribute *val_dev_attr, *pull_dev_attr;
305 struct gpio_sim_attribute *val_attr, *pull_attr;
306 unsigned int num_lines = chip->gc.ngpio;
307 struct device *dev = chip->gc.parent;
308 struct attribute_group *attr_group;
309 struct attribute **attrs;
312 chip->attr_groups = devm_kcalloc(dev, sizeof(*chip->attr_groups),
313 num_lines + 1, GFP_KERNEL);
314 if (!chip->attr_groups)
317 for (i = 0; i < num_lines; i++) {
318 attr_group = devm_kzalloc(dev, sizeof(*attr_group), GFP_KERNEL);
319 attrs = devm_kcalloc(dev, GPIO_SIM_NUM_ATTRS, sizeof(*attrs),
321 val_attr = devm_kzalloc(dev, sizeof(*val_attr), GFP_KERNEL);
322 pull_attr = devm_kzalloc(dev, sizeof(*pull_attr), GFP_KERNEL);
323 if (!attr_group || !attrs || !val_attr || !pull_attr)
326 attr_group->name = devm_kasprintf(dev, GFP_KERNEL,
328 if (!attr_group->name)
331 val_attr->offset = pull_attr->offset = i;
333 val_dev_attr = &val_attr->dev_attr;
334 pull_dev_attr = &pull_attr->dev_attr;
336 sysfs_attr_init(&val_dev_attr->attr);
337 sysfs_attr_init(&pull_dev_attr->attr);
339 val_dev_attr->attr.name = "value";
340 pull_dev_attr->attr.name = "pull";
342 val_dev_attr->attr.mode = pull_dev_attr->attr.mode = 0644;
344 val_dev_attr->show = gpio_sim_sysfs_val_show;
345 val_dev_attr->store = gpio_sim_sysfs_val_store;
346 pull_dev_attr->show = gpio_sim_sysfs_pull_show;
347 pull_dev_attr->store = gpio_sim_sysfs_pull_store;
349 attrs[0] = &val_dev_attr->attr;
350 attrs[1] = &pull_dev_attr->attr;
352 attr_group->attrs = attrs;
353 chip->attr_groups[i] = attr_group;
356 ret = sysfs_create_groups(&chip->gc.gpiodev->dev.kobj,
361 return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip);
364 static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev)
366 struct gpio_sim_chip *chip;
367 struct gpio_chip *gc;
372 ret = fwnode_property_read_u32(swnode, "ngpios", &num_lines);
376 if (num_lines > GPIO_SIM_NGPIO_MAX)
379 ret = fwnode_property_read_string(swnode, "gpio-sim,label", &label);
381 label = devm_kasprintf(dev, GFP_KERNEL, "%s-%pfwP",
382 dev_name(dev), swnode);
387 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
391 chip->direction_map = devm_bitmap_alloc(dev, num_lines, GFP_KERNEL);
392 if (!chip->direction_map)
395 /* Default to input mode. */
396 bitmap_fill(chip->direction_map, num_lines);
398 chip->value_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
399 if (!chip->value_map)
402 chip->pull_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
406 chip->irq_sim = devm_irq_domain_create_sim(dev, swnode, num_lines);
407 if (IS_ERR(chip->irq_sim))
408 return PTR_ERR(chip->irq_sim);
410 ret = devm_add_action_or_reset(dev, gpio_sim_dispose_mappings, chip);
414 mutex_init(&chip->lock);
415 ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy,
422 gc->ngpio = num_lines;
424 gc->owner = THIS_MODULE;
427 gc->get = gpio_sim_get;
428 gc->set = gpio_sim_set;
429 gc->get_multiple = gpio_sim_get_multiple;
430 gc->set_multiple = gpio_sim_set_multiple;
431 gc->direction_output = gpio_sim_direction_output;
432 gc->direction_input = gpio_sim_direction_input;
433 gc->get_direction = gpio_sim_get_direction;
434 gc->set_config = gpio_sim_set_config;
435 gc->to_irq = gpio_sim_to_irq;
436 gc->free = gpio_sim_free;
437 gc->can_sleep = true;
439 ret = devm_gpiochip_add_data(dev, gc, chip);
443 /* Used by sysfs and configfs callbacks. */
444 dev_set_drvdata(&gc->gpiodev->dev, chip);
446 return gpio_sim_setup_sysfs(chip);
449 static int gpio_sim_probe(struct platform_device *pdev)
451 struct device *dev = &pdev->dev;
452 struct fwnode_handle *swnode;
455 device_for_each_child_node(dev, swnode) {
456 ret = gpio_sim_add_bank(swnode, dev);
458 fwnode_handle_put(swnode);
466 static const struct of_device_id gpio_sim_of_match[] = {
467 { .compatible = "gpio-simulator" },
470 MODULE_DEVICE_TABLE(of, gpio_sim_of_match);
472 static struct platform_driver gpio_sim_driver = {
475 .of_match_table = gpio_sim_of_match,
477 .probe = gpio_sim_probe,
480 struct gpio_sim_device {
481 struct config_group group;
484 * If pdev is NULL, the device is 'pending' (waiting for configuration).
485 * Once the pointer is assigned, the device has been created and the
488 struct platform_device *pdev;
492 * Each configfs filesystem operation is protected with the subsystem
493 * mutex. Each separate attribute is protected with the buffer mutex.
494 * This structure however can be modified by callbacks of different
495 * attributes so we need another lock.
497 * We use this lock for protecting all data structures owned by this
503 * This is used to synchronously wait for the driver's probe to complete
504 * and notify the user-space about any errors.
506 struct notifier_block bus_notifier;
507 struct completion probe_completion;
510 struct gpiod_hog *hogs;
512 struct list_head bank_list;
515 /* This is called with dev->lock already taken. */
516 static int gpio_sim_bus_notifier_call(struct notifier_block *nb,
517 unsigned long action, void *data)
519 struct gpio_sim_device *simdev = container_of(nb,
520 struct gpio_sim_device,
522 struct device *dev = data;
525 snprintf(devname, sizeof(devname), "gpio-sim.%u", simdev->id);
527 if (strcmp(dev_name(dev), devname) == 0) {
528 if (action == BUS_NOTIFY_BOUND_DRIVER)
529 simdev->driver_bound = true;
530 else if (action == BUS_NOTIFY_DRIVER_NOT_BOUND)
531 simdev->driver_bound = false;
535 complete(&simdev->probe_completion);
542 static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item)
544 struct config_group *group = to_config_group(item);
546 return container_of(group, struct gpio_sim_device, group);
549 struct gpio_sim_bank {
550 struct config_group group;
553 * We could have used the ci_parent field of the config_item but
554 * configfs is stupid and calls the item's release callback after
555 * already having cleared the parent pointer even though the parent
556 * is guaranteed to survive the child...
558 * So we need to store the pointer to the parent struct here. We can
559 * dereference it anywhere we need with no checks and no locking as
560 * it's guaranteed to survive the children and protected by configfs
563 * Same for other structures.
565 struct gpio_sim_device *parent;
566 struct list_head siblings;
569 unsigned int num_lines;
571 struct list_head line_list;
573 struct fwnode_handle *swnode;
576 static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item)
578 struct config_group *group = to_config_group(item);
580 return container_of(group, struct gpio_sim_bank, group);
583 static bool gpio_sim_bank_has_label(struct gpio_sim_bank *bank)
585 return bank->label && *bank->label;
588 static struct gpio_sim_device *
589 gpio_sim_bank_get_device(struct gpio_sim_bank *bank)
596 struct gpio_sim_line {
597 struct config_group group;
599 struct gpio_sim_bank *parent;
600 struct list_head siblings;
605 /* There can only be one hog per line. */
606 struct gpio_sim_hog *hog;
609 static struct gpio_sim_line *to_gpio_sim_line(struct config_item *item)
611 struct config_group *group = to_config_group(item);
613 return container_of(group, struct gpio_sim_line, group);
616 static struct gpio_sim_device *
617 gpio_sim_line_get_device(struct gpio_sim_line *line)
619 struct gpio_sim_bank *bank = line->parent;
621 return gpio_sim_bank_get_device(bank);
624 struct gpio_sim_hog {
625 struct config_item item;
626 struct gpio_sim_line *parent;
632 static struct gpio_sim_hog *to_gpio_sim_hog(struct config_item *item)
634 return container_of(item, struct gpio_sim_hog, item);
637 static struct gpio_sim_device *gpio_sim_hog_get_device(struct gpio_sim_hog *hog)
639 struct gpio_sim_line *line = hog->parent;
641 return gpio_sim_line_get_device(line);
644 static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev)
649 static char *gpio_sim_strdup_trimmed(const char *str, size_t count)
653 trimmed = kstrndup(skip_spaces(str), count, GFP_KERNEL);
657 return strim(trimmed);
660 static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item,
663 struct gpio_sim_device *dev = to_gpio_sim_device(item);
664 struct platform_device *pdev;
666 guard(mutex)(&dev->lock);
670 return sprintf(page, "%s\n", dev_name(&pdev->dev));
672 return sprintf(page, "gpio-sim.%d\n", dev->id);
675 CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name);
678 gpio_sim_device_config_live_show(struct config_item *item, char *page)
680 struct gpio_sim_device *dev = to_gpio_sim_device(item);
683 scoped_guard(mutex, &dev->lock)
684 live = gpio_sim_device_is_live_unlocked(dev);
686 return sprintf(page, "%c\n", live ? '1' : '0');
689 static unsigned int gpio_sim_get_line_names_size(struct gpio_sim_bank *bank)
691 struct gpio_sim_line *line;
692 unsigned int size = 0;
694 list_for_each_entry(line, &bank->line_list, siblings) {
695 if (!line->name || (line->offset >= bank->num_lines))
698 size = max(size, line->offset + 1);
705 gpio_sim_set_line_names(struct gpio_sim_bank *bank, char **line_names)
707 struct gpio_sim_line *line;
709 list_for_each_entry(line, &bank->line_list, siblings) {
710 if (!line->name || (line->offset >= bank->num_lines))
713 line_names[line->offset] = line->name;
717 static void gpio_sim_remove_hogs(struct gpio_sim_device *dev)
719 struct gpiod_hog *hog;
724 gpiod_remove_hogs(dev->hogs);
726 for (hog = dev->hogs; hog->chip_label; hog++) {
727 kfree(hog->chip_label);
728 kfree(hog->line_name);
735 static int gpio_sim_add_hogs(struct gpio_sim_device *dev)
737 unsigned int num_hogs = 0, idx = 0;
738 struct gpio_sim_bank *bank;
739 struct gpio_sim_line *line;
740 struct gpiod_hog *hog;
742 list_for_each_entry(bank, &dev->bank_list, siblings) {
743 list_for_each_entry(line, &bank->line_list, siblings) {
744 if (line->offset >= bank->num_lines)
755 /* Allocate one more for the sentinel. */
756 dev->hogs = kcalloc(num_hogs + 1, sizeof(*dev->hogs), GFP_KERNEL);
760 list_for_each_entry(bank, &dev->bank_list, siblings) {
761 list_for_each_entry(line, &bank->line_list, siblings) {
762 if (line->offset >= bank->num_lines)
768 hog = &dev->hogs[idx++];
771 * We need to make this string manually because at this
772 * point the device doesn't exist yet and so dev_name()
775 if (gpio_sim_bank_has_label(bank))
776 hog->chip_label = kstrdup(bank->label,
779 hog->chip_label = kasprintf(GFP_KERNEL,
783 if (!hog->chip_label) {
784 gpio_sim_remove_hogs(dev);
789 * We need to duplicate this because the hog config
790 * item can be removed at any time (and we can't block
791 * it) and gpiolib doesn't make a deep copy of the hog
794 if (line->hog->name) {
795 hog->line_name = kstrdup(line->hog->name,
797 if (!hog->line_name) {
798 gpio_sim_remove_hogs(dev);
803 hog->chip_hwnum = line->offset;
804 hog->dflags = line->hog->dir;
808 gpiod_add_hogs(dev->hogs);
813 static struct fwnode_handle *
814 gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
815 struct fwnode_handle *parent)
817 struct property_entry properties[GPIO_SIM_PROP_MAX];
818 unsigned int prop_idx = 0, line_names_size;
819 char **line_names __free(kfree) = NULL;
821 memset(properties, 0, sizeof(properties));
823 properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines);
825 if (gpio_sim_bank_has_label(bank))
826 properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
829 line_names_size = gpio_sim_get_line_names_size(bank);
830 if (line_names_size) {
831 line_names = kcalloc(line_names_size, sizeof(*line_names),
834 return ERR_PTR(-ENOMEM);
836 gpio_sim_set_line_names(bank, line_names);
838 properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
840 line_names, line_names_size);
843 return fwnode_create_software_node(properties, parent);
846 static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode)
848 struct fwnode_handle *child;
850 fwnode_for_each_child_node(swnode, child)
851 fwnode_remove_software_node(child);
853 fwnode_remove_software_node(swnode);
856 static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev)
858 struct gpio_sim_bank *this, *pos;
860 list_for_each_entry(this, &dev->bank_list, siblings) {
861 list_for_each_entry(pos, &dev->bank_list, siblings) {
862 if (this == pos || (!this->label || !pos->label))
865 if (strcmp(this->label, pos->label) == 0)
873 static int gpio_sim_device_activate_unlocked(struct gpio_sim_device *dev)
875 struct platform_device_info pdevinfo;
876 struct fwnode_handle *swnode;
877 struct platform_device *pdev;
878 struct gpio_sim_bank *bank;
881 if (list_empty(&dev->bank_list))
885 * Non-unique GPIO device labels are a corner-case we don't support
886 * as it would interfere with machine hogging mechanism and has little
889 if (gpio_sim_bank_labels_non_unique(dev))
892 memset(&pdevinfo, 0, sizeof(pdevinfo));
894 swnode = fwnode_create_software_node(NULL, NULL);
896 return PTR_ERR(swnode);
898 list_for_each_entry(bank, &dev->bank_list, siblings) {
899 bank->swnode = gpio_sim_make_bank_swnode(bank, swnode);
900 if (IS_ERR(bank->swnode)) {
901 ret = PTR_ERR(bank->swnode);
902 gpio_sim_remove_swnode_recursive(swnode);
907 ret = gpio_sim_add_hogs(dev);
909 gpio_sim_remove_swnode_recursive(swnode);
913 pdevinfo.name = "gpio-sim";
914 pdevinfo.fwnode = swnode;
915 pdevinfo.id = dev->id;
917 reinit_completion(&dev->probe_completion);
918 dev->driver_bound = false;
919 bus_register_notifier(&platform_bus_type, &dev->bus_notifier);
921 pdev = platform_device_register_full(&pdevinfo);
923 bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
924 gpio_sim_remove_hogs(dev);
925 gpio_sim_remove_swnode_recursive(swnode);
926 return PTR_ERR(pdev);
929 wait_for_completion(&dev->probe_completion);
930 bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
932 if (!dev->driver_bound) {
933 /* Probe failed, check kernel log. */
934 platform_device_unregister(pdev);
935 gpio_sim_remove_hogs(dev);
936 gpio_sim_remove_swnode_recursive(swnode);
945 static void gpio_sim_device_deactivate_unlocked(struct gpio_sim_device *dev)
947 struct fwnode_handle *swnode;
949 swnode = dev_fwnode(&dev->pdev->dev);
950 platform_device_unregister(dev->pdev);
951 gpio_sim_remove_hogs(dev);
952 gpio_sim_remove_swnode_recursive(swnode);
957 gpio_sim_device_config_live_store(struct config_item *item,
958 const char *page, size_t count)
960 struct gpio_sim_device *dev = to_gpio_sim_device(item);
964 ret = kstrtobool(page, &live);
968 guard(mutex)(&dev->lock);
970 if (live == gpio_sim_device_is_live_unlocked(dev))
973 ret = gpio_sim_device_activate_unlocked(dev);
975 gpio_sim_device_deactivate_unlocked(dev);
980 CONFIGFS_ATTR(gpio_sim_device_config_, live);
982 static struct configfs_attribute *gpio_sim_device_config_attrs[] = {
983 &gpio_sim_device_config_attr_dev_name,
984 &gpio_sim_device_config_attr_live,
988 struct gpio_sim_chip_name_ctx {
989 struct fwnode_handle *swnode;
993 static int gpio_sim_emit_chip_name(struct device *dev, void *data)
995 struct gpio_sim_chip_name_ctx *ctx = data;
997 /* This would be the sysfs device exported in /sys/class/gpio. */
1001 if (device_match_fwnode(dev, ctx->swnode))
1002 return sprintf(ctx->page, "%s\n", dev_name(dev));
1007 static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item,
1010 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1011 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1012 struct gpio_sim_chip_name_ctx ctx = { bank->swnode, page };
1014 guard(mutex)(&dev->lock);
1016 if (gpio_sim_device_is_live_unlocked(dev))
1017 return device_for_each_child(&dev->pdev->dev, &ctx,
1018 gpio_sim_emit_chip_name);
1020 return sprintf(page, "none\n");
1023 CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name);
1026 gpio_sim_bank_config_label_show(struct config_item *item, char *page)
1028 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1029 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1031 guard(mutex)(&dev->lock);
1033 return sprintf(page, "%s\n", bank->label ?: "");
1036 static ssize_t gpio_sim_bank_config_label_store(struct config_item *item,
1037 const char *page, size_t count)
1039 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1040 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1043 guard(mutex)(&dev->lock);
1045 if (gpio_sim_device_is_live_unlocked(dev))
1048 trimmed = gpio_sim_strdup_trimmed(page, count);
1053 bank->label = trimmed;
1058 CONFIGFS_ATTR(gpio_sim_bank_config_, label);
1061 gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page)
1063 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1064 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1066 guard(mutex)(&dev->lock);
1068 return sprintf(page, "%u\n", bank->num_lines);
1072 gpio_sim_bank_config_num_lines_store(struct config_item *item,
1073 const char *page, size_t count)
1075 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1076 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1077 unsigned int num_lines;
1080 ret = kstrtouint(page, 0, &num_lines);
1087 guard(mutex)(&dev->lock);
1089 if (gpio_sim_device_is_live_unlocked(dev))
1092 bank->num_lines = num_lines;
1097 CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines);
1099 static struct configfs_attribute *gpio_sim_bank_config_attrs[] = {
1100 &gpio_sim_bank_config_attr_chip_name,
1101 &gpio_sim_bank_config_attr_label,
1102 &gpio_sim_bank_config_attr_num_lines,
1107 gpio_sim_line_config_name_show(struct config_item *item, char *page)
1109 struct gpio_sim_line *line = to_gpio_sim_line(item);
1110 struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1112 guard(mutex)(&dev->lock);
1114 return sprintf(page, "%s\n", line->name ?: "");
1117 static ssize_t gpio_sim_line_config_name_store(struct config_item *item,
1118 const char *page, size_t count)
1120 struct gpio_sim_line *line = to_gpio_sim_line(item);
1121 struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1124 guard(mutex)(&dev->lock);
1126 if (gpio_sim_device_is_live_unlocked(dev))
1129 trimmed = gpio_sim_strdup_trimmed(page, count);
1134 line->name = trimmed;
1139 CONFIGFS_ATTR(gpio_sim_line_config_, name);
1141 static struct configfs_attribute *gpio_sim_line_config_attrs[] = {
1142 &gpio_sim_line_config_attr_name,
1146 static ssize_t gpio_sim_hog_config_name_show(struct config_item *item,
1149 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1150 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1152 guard(mutex)(&dev->lock);
1154 return sprintf(page, "%s\n", hog->name ?: "");
1157 static ssize_t gpio_sim_hog_config_name_store(struct config_item *item,
1158 const char *page, size_t count)
1160 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1161 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1164 guard(mutex)(&dev->lock);
1166 if (gpio_sim_device_is_live_unlocked(dev))
1169 trimmed = gpio_sim_strdup_trimmed(page, count);
1174 hog->name = trimmed;
1179 CONFIGFS_ATTR(gpio_sim_hog_config_, name);
1181 static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item,
1184 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1185 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1189 scoped_guard(mutex, &dev->lock)
1196 case GPIOD_OUT_HIGH:
1197 repr = "output-high";
1200 repr = "output-low";
1203 /* This would be a programmer bug. */
1204 WARN(1, "Unexpected hog direction value: %d", dir);
1208 return sprintf(page, "%s\n", repr);
1212 gpio_sim_hog_config_direction_store(struct config_item *item,
1213 const char *page, size_t count)
1215 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1216 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1219 guard(mutex)(&dev->lock);
1221 if (gpio_sim_device_is_live_unlocked(dev))
1224 if (sysfs_streq(page, "input"))
1226 else if (sysfs_streq(page, "output-high"))
1227 dir = GPIOD_OUT_HIGH;
1228 else if (sysfs_streq(page, "output-low"))
1229 dir = GPIOD_OUT_LOW;
1238 CONFIGFS_ATTR(gpio_sim_hog_config_, direction);
1240 static struct configfs_attribute *gpio_sim_hog_config_attrs[] = {
1241 &gpio_sim_hog_config_attr_name,
1242 &gpio_sim_hog_config_attr_direction,
1246 static void gpio_sim_hog_config_item_release(struct config_item *item)
1248 struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1249 struct gpio_sim_line *line = hog->parent;
1250 struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1252 scoped_guard(mutex, &dev->lock)
1259 static struct configfs_item_operations gpio_sim_hog_config_item_ops = {
1260 .release = gpio_sim_hog_config_item_release,
1263 static const struct config_item_type gpio_sim_hog_config_type = {
1264 .ct_item_ops = &gpio_sim_hog_config_item_ops,
1265 .ct_attrs = gpio_sim_hog_config_attrs,
1266 .ct_owner = THIS_MODULE,
1269 static struct config_item *
1270 gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name)
1272 struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item);
1273 struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1274 struct gpio_sim_hog *hog;
1276 if (strcmp(name, "hog") != 0)
1277 return ERR_PTR(-EINVAL);
1279 guard(mutex)(&dev->lock);
1281 hog = kzalloc(sizeof(*hog), GFP_KERNEL);
1283 return ERR_PTR(-ENOMEM);
1285 config_item_init_type_name(&hog->item, name,
1286 &gpio_sim_hog_config_type);
1288 hog->dir = GPIOD_IN;
1296 static void gpio_sim_line_config_group_release(struct config_item *item)
1298 struct gpio_sim_line *line = to_gpio_sim_line(item);
1299 struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1301 scoped_guard(mutex, &dev->lock)
1302 list_del(&line->siblings);
1308 static struct configfs_item_operations gpio_sim_line_config_item_ops = {
1309 .release = gpio_sim_line_config_group_release,
1312 static struct configfs_group_operations gpio_sim_line_config_group_ops = {
1313 .make_item = gpio_sim_line_config_make_hog_item,
1316 static const struct config_item_type gpio_sim_line_config_type = {
1317 .ct_item_ops = &gpio_sim_line_config_item_ops,
1318 .ct_group_ops = &gpio_sim_line_config_group_ops,
1319 .ct_attrs = gpio_sim_line_config_attrs,
1320 .ct_owner = THIS_MODULE,
1323 static struct config_group *
1324 gpio_sim_bank_config_make_line_group(struct config_group *group,
1327 struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item);
1328 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1329 struct gpio_sim_line *line;
1330 unsigned int offset;
1333 ret = sscanf(name, "line%u%n", &offset, &nchar);
1334 if (ret != 1 || nchar != strlen(name))
1335 return ERR_PTR(-EINVAL);
1337 guard(mutex)(&dev->lock);
1339 if (gpio_sim_device_is_live_unlocked(dev))
1340 return ERR_PTR(-EBUSY);
1342 line = kzalloc(sizeof(*line), GFP_KERNEL);
1344 return ERR_PTR(-ENOMEM);
1346 config_group_init_type_name(&line->group, name,
1347 &gpio_sim_line_config_type);
1349 line->parent = bank;
1350 line->offset = offset;
1351 list_add_tail(&line->siblings, &bank->line_list);
1353 return &line->group;
1356 static void gpio_sim_bank_config_group_release(struct config_item *item)
1358 struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1359 struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1361 scoped_guard(mutex, &dev->lock)
1362 list_del(&bank->siblings);
1368 static struct configfs_item_operations gpio_sim_bank_config_item_ops = {
1369 .release = gpio_sim_bank_config_group_release,
1372 static struct configfs_group_operations gpio_sim_bank_config_group_ops = {
1373 .make_group = gpio_sim_bank_config_make_line_group,
1376 static const struct config_item_type gpio_sim_bank_config_group_type = {
1377 .ct_item_ops = &gpio_sim_bank_config_item_ops,
1378 .ct_group_ops = &gpio_sim_bank_config_group_ops,
1379 .ct_attrs = gpio_sim_bank_config_attrs,
1380 .ct_owner = THIS_MODULE,
1383 static struct config_group *
1384 gpio_sim_device_config_make_bank_group(struct config_group *group,
1387 struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item);
1388 struct gpio_sim_bank *bank;
1390 guard(mutex)(&dev->lock);
1392 if (gpio_sim_device_is_live_unlocked(dev))
1393 return ERR_PTR(-EBUSY);
1395 bank = kzalloc(sizeof(*bank), GFP_KERNEL);
1397 return ERR_PTR(-ENOMEM);
1399 config_group_init_type_name(&bank->group, name,
1400 &gpio_sim_bank_config_group_type);
1401 bank->num_lines = 1;
1403 INIT_LIST_HEAD(&bank->line_list);
1404 list_add_tail(&bank->siblings, &dev->bank_list);
1406 return &bank->group;
1409 static void gpio_sim_device_config_group_release(struct config_item *item)
1411 struct gpio_sim_device *dev = to_gpio_sim_device(item);
1413 scoped_guard(mutex, &dev->lock) {
1414 if (gpio_sim_device_is_live_unlocked(dev))
1415 gpio_sim_device_deactivate_unlocked(dev);
1418 mutex_destroy(&dev->lock);
1419 ida_free(&gpio_sim_ida, dev->id);
1423 static struct configfs_item_operations gpio_sim_device_config_item_ops = {
1424 .release = gpio_sim_device_config_group_release,
1427 static struct configfs_group_operations gpio_sim_device_config_group_ops = {
1428 .make_group = gpio_sim_device_config_make_bank_group,
1431 static const struct config_item_type gpio_sim_device_config_group_type = {
1432 .ct_item_ops = &gpio_sim_device_config_item_ops,
1433 .ct_group_ops = &gpio_sim_device_config_group_ops,
1434 .ct_attrs = gpio_sim_device_config_attrs,
1435 .ct_owner = THIS_MODULE,
1438 static struct config_group *
1439 gpio_sim_config_make_device_group(struct config_group *group, const char *name)
1441 struct gpio_sim_device *dev __free(kfree) = NULL;
1444 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1446 return ERR_PTR(-ENOMEM);
1448 id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
1452 config_group_init_type_name(&dev->group, name,
1453 &gpio_sim_device_config_group_type);
1455 mutex_init(&dev->lock);
1456 INIT_LIST_HEAD(&dev->bank_list);
1458 dev->bus_notifier.notifier_call = gpio_sim_bus_notifier_call;
1459 init_completion(&dev->probe_completion);
1461 return &no_free_ptr(dev)->group;
1464 static struct configfs_group_operations gpio_sim_config_group_ops = {
1465 .make_group = gpio_sim_config_make_device_group,
1468 static const struct config_item_type gpio_sim_config_type = {
1469 .ct_group_ops = &gpio_sim_config_group_ops,
1470 .ct_owner = THIS_MODULE,
1473 static struct configfs_subsystem gpio_sim_config_subsys = {
1476 .ci_namebuf = "gpio-sim",
1477 .ci_type = &gpio_sim_config_type,
1482 static int __init gpio_sim_init(void)
1486 ret = platform_driver_register(&gpio_sim_driver);
1488 pr_err("Error %d while registering the platform driver\n", ret);
1492 config_group_init(&gpio_sim_config_subsys.su_group);
1493 mutex_init(&gpio_sim_config_subsys.su_mutex);
1494 ret = configfs_register_subsystem(&gpio_sim_config_subsys);
1496 pr_err("Error %d while registering the configfs subsystem %s\n",
1497 ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf);
1498 mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1499 platform_driver_unregister(&gpio_sim_driver);
1505 module_init(gpio_sim_init);
1507 static void __exit gpio_sim_exit(void)
1509 configfs_unregister_subsystem(&gpio_sim_config_subsys);
1510 mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1511 platform_driver_unregister(&gpio_sim_driver);
1513 module_exit(gpio_sim_exit);
1515 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl");
1516 MODULE_DESCRIPTION("GPIO Simulator Module");
1517 MODULE_LICENSE("GPL");