at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * GPIO testing driver based on configfs. |
| 4 | * |
| 5 | * Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl> |
| 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
| 10 | #include <linux/array_size.h> |
| 11 | #include <linux/bitmap.h> |
| 12 | #include <linux/cleanup.h> |
| 13 | #include <linux/configfs.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/gpio/consumer.h> |
| 17 | #include <linux/gpio/driver.h> |
| 18 | #include <linux/gpio/machine.h> |
| 19 | #include <linux/idr.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/irq.h> |
| 22 | #include <linux/irq_sim.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/lockdep.h> |
| 25 | #include <linux/minmax.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/mutex.h> |
| 28 | #include <linux/notifier.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/property.h> |
| 31 | #include <linux/seq_file.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/string_helpers.h> |
| 35 | #include <linux/sysfs.h> |
| 36 | #include <linux/types.h> |
| 37 | |
| 38 | #define GPIO_SIM_NGPIO_MAX 1024 |
| 39 | #define GPIO_SIM_PROP_MAX 5 /* Max 4 properties + sentinel. */ |
| 40 | #define GPIO_SIM_HOG_PROP_MAX 5 |
| 41 | #define GPIO_SIM_NUM_ATTRS 3 /* value, pull and sentinel */ |
| 42 | |
| 43 | static DEFINE_IDA(gpio_sim_ida); |
| 44 | |
| 45 | struct gpio_sim_chip { |
| 46 | struct gpio_chip gc; |
| 47 | struct device *dev; |
| 48 | unsigned long *request_map; |
| 49 | unsigned long *direction_map; |
| 50 | unsigned long *value_map; |
| 51 | unsigned long *pull_map; |
| 52 | struct irq_domain *irq_sim; |
| 53 | struct mutex lock; |
| 54 | const struct attribute_group **attr_groups; |
| 55 | }; |
| 56 | |
| 57 | struct gpio_sim_attribute { |
| 58 | struct device_attribute dev_attr; |
| 59 | unsigned int offset; |
| 60 | }; |
| 61 | |
| 62 | static struct gpio_sim_attribute * |
| 63 | to_gpio_sim_attr(struct device_attribute *dev_attr) |
| 64 | { |
| 65 | return container_of(dev_attr, struct gpio_sim_attribute, dev_attr); |
| 66 | } |
| 67 | |
| 68 | static int gpio_sim_apply_pull(struct gpio_sim_chip *chip, |
| 69 | unsigned int offset, int value) |
| 70 | { |
| 71 | int irq, irq_type, ret; |
| 72 | |
| 73 | guard(mutex)(&chip->lock); |
| 74 | |
| 75 | if (test_bit(offset, chip->request_map) && |
| 76 | test_bit(offset, chip->direction_map)) { |
| 77 | if (value == !!test_bit(offset, chip->value_map)) |
| 78 | goto set_pull; |
| 79 | |
| 80 | /* |
| 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. |
| 85 | */ |
| 86 | irq = irq_find_mapping(chip->irq_sim, offset); |
| 87 | if (!irq) |
| 88 | goto set_value; |
| 89 | |
| 90 | irq_type = irq_get_trigger_type(irq); |
| 91 | |
| 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, |
| 95 | true); |
| 96 | if (ret) |
| 97 | goto set_pull; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | set_value: |
| 102 | /* Change the value unless we're actively driving the line. */ |
| 103 | if (!test_bit(offset, chip->request_map) || |
| 104 | test_bit(offset, chip->direction_map)) |
| 105 | __assign_bit(offset, chip->value_map, value); |
| 106 | |
| 107 | set_pull: |
| 108 | __assign_bit(offset, chip->pull_map, value); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset) |
| 113 | { |
| 114 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 115 | |
| 116 | guard(mutex)(&chip->lock); |
| 117 | |
| 118 | return !!test_bit(offset, chip->value_map); |
| 119 | } |
| 120 | |
| 121 | static int gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value) |
| 122 | { |
| 123 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 124 | |
| 125 | scoped_guard(mutex, &chip->lock) |
| 126 | __assign_bit(offset, chip->value_map, value); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int gpio_sim_get_multiple(struct gpio_chip *gc, |
| 132 | unsigned long *mask, unsigned long *bits) |
| 133 | { |
| 134 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 135 | |
| 136 | scoped_guard(mutex, &chip->lock) |
| 137 | bitmap_replace(bits, bits, chip->value_map, mask, gc->ngpio); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int gpio_sim_set_multiple(struct gpio_chip *gc, |
| 143 | unsigned long *mask, unsigned long *bits) |
| 144 | { |
| 145 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 146 | |
| 147 | scoped_guard(mutex, &chip->lock) |
| 148 | bitmap_replace(chip->value_map, chip->value_map, bits, mask, |
| 149 | gc->ngpio); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int gpio_sim_direction_output(struct gpio_chip *gc, |
| 155 | unsigned int offset, int value) |
| 156 | { |
| 157 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 158 | |
| 159 | scoped_guard(mutex, &chip->lock) { |
| 160 | __clear_bit(offset, chip->direction_map); |
| 161 | __assign_bit(offset, chip->value_map, value); |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset) |
| 168 | { |
| 169 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 170 | |
| 171 | scoped_guard(mutex, &chip->lock) |
| 172 | __set_bit(offset, chip->direction_map); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset) |
| 178 | { |
| 179 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 180 | int direction; |
| 181 | |
| 182 | scoped_guard(mutex, &chip->lock) |
| 183 | direction = !!test_bit(offset, chip->direction_map); |
| 184 | |
| 185 | return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT; |
| 186 | } |
| 187 | |
| 188 | static int gpio_sim_set_config(struct gpio_chip *gc, unsigned int offset, |
| 189 | unsigned long config) |
| 190 | { |
| 191 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 192 | |
| 193 | switch (pinconf_to_config_param(config)) { |
| 194 | case PIN_CONFIG_BIAS_PULL_UP: |
| 195 | return gpio_sim_apply_pull(chip, offset, 1); |
| 196 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 197 | return gpio_sim_apply_pull(chip, offset, 0); |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | return -ENOTSUPP; |
| 203 | } |
| 204 | |
| 205 | static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset) |
| 206 | { |
| 207 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 208 | |
| 209 | return irq_create_mapping(chip->irq_sim, offset); |
| 210 | } |
| 211 | |
| 212 | static int gpio_sim_request(struct gpio_chip *gc, unsigned int offset) |
| 213 | { |
| 214 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 215 | |
| 216 | scoped_guard(mutex, &chip->lock) |
| 217 | __set_bit(offset, chip->request_map); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset) |
| 223 | { |
| 224 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 225 | |
| 226 | scoped_guard(mutex, &chip->lock) { |
| 227 | __assign_bit(offset, chip->value_map, |
| 228 | !!test_bit(offset, chip->pull_map)); |
| 229 | __clear_bit(offset, chip->request_map); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static int gpio_sim_irq_requested(struct irq_domain *domain, |
| 234 | irq_hw_number_t hwirq, void *data) |
| 235 | { |
| 236 | struct gpio_sim_chip *chip = data; |
| 237 | |
| 238 | return gpiochip_lock_as_irq(&chip->gc, hwirq); |
| 239 | } |
| 240 | |
| 241 | static void gpio_sim_irq_released(struct irq_domain *domain, |
| 242 | irq_hw_number_t hwirq, void *data) |
| 243 | { |
| 244 | struct gpio_sim_chip *chip = data; |
| 245 | |
| 246 | gpiochip_unlock_as_irq(&chip->gc, hwirq); |
| 247 | } |
| 248 | |
| 249 | static const struct irq_sim_ops gpio_sim_irq_sim_ops = { |
| 250 | .irq_sim_irq_requested = gpio_sim_irq_requested, |
| 251 | .irq_sim_irq_released = gpio_sim_irq_released, |
| 252 | }; |
| 253 | |
| 254 | static void gpio_sim_dbg_show(struct seq_file *seq, struct gpio_chip *gc) |
| 255 | { |
| 256 | struct gpio_sim_chip *chip = gpiochip_get_data(gc); |
| 257 | const char *label; |
| 258 | int i; |
| 259 | |
| 260 | guard(mutex)(&chip->lock); |
| 261 | |
| 262 | for_each_hwgpio(gc, i, label) |
| 263 | seq_printf(seq, " gpio-%-3d (%s) %s,%s\n", i, |
| 264 | label ?: "<unused>", |
| 265 | test_bit(i, chip->direction_map) ? "input" : |
| 266 | test_bit(i, chip->value_map) ? "output-high" : |
| 267 | "output-low", |
| 268 | test_bit(i, chip->pull_map) ? "pull-up" : |
| 269 | "pull-down"); |
| 270 | } |
| 271 | |
| 272 | static ssize_t gpio_sim_sysfs_val_show(struct device *dev, |
| 273 | struct device_attribute *attr, char *buf) |
| 274 | { |
| 275 | struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); |
| 276 | struct gpio_sim_chip *chip = dev_get_drvdata(dev); |
| 277 | int val; |
| 278 | |
| 279 | scoped_guard(mutex, &chip->lock) |
| 280 | val = !!test_bit(line_attr->offset, chip->value_map); |
| 281 | |
| 282 | return sysfs_emit(buf, "%d\n", val); |
| 283 | } |
| 284 | |
| 285 | static ssize_t gpio_sim_sysfs_val_store(struct device *dev, |
| 286 | struct device_attribute *attr, |
| 287 | const char *buf, size_t count) |
| 288 | { |
| 289 | /* |
| 290 | * Not assigning this function will result in write() returning -EIO |
| 291 | * which is confusing. Return -EPERM explicitly. |
| 292 | */ |
| 293 | return -EPERM; |
| 294 | } |
| 295 | |
| 296 | static const char *const gpio_sim_sysfs_pull_strings[] = { |
| 297 | [0] = "pull-down", |
| 298 | [1] = "pull-up", |
| 299 | }; |
| 300 | |
| 301 | static ssize_t gpio_sim_sysfs_pull_show(struct device *dev, |
| 302 | struct device_attribute *attr, |
| 303 | char *buf) |
| 304 | { |
| 305 | struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); |
| 306 | struct gpio_sim_chip *chip = dev_get_drvdata(dev); |
| 307 | int pull; |
| 308 | |
| 309 | scoped_guard(mutex, &chip->lock) |
| 310 | pull = !!test_bit(line_attr->offset, chip->pull_map); |
| 311 | |
| 312 | return sysfs_emit(buf, "%s\n", gpio_sim_sysfs_pull_strings[pull]); |
| 313 | } |
| 314 | |
| 315 | static ssize_t gpio_sim_sysfs_pull_store(struct device *dev, |
| 316 | struct device_attribute *attr, |
| 317 | const char *buf, size_t len) |
| 318 | { |
| 319 | struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); |
| 320 | struct gpio_sim_chip *chip = dev_get_drvdata(dev); |
| 321 | int ret, pull; |
| 322 | |
| 323 | pull = sysfs_match_string(gpio_sim_sysfs_pull_strings, buf); |
| 324 | if (pull < 0) |
| 325 | return pull; |
| 326 | |
| 327 | ret = gpio_sim_apply_pull(chip, line_attr->offset, pull); |
| 328 | if (ret) |
| 329 | return ret; |
| 330 | |
| 331 | return len; |
| 332 | } |
| 333 | |
| 334 | static void gpio_sim_put_device(void *data) |
| 335 | { |
| 336 | struct device *dev = data; |
| 337 | |
| 338 | put_device(dev); |
| 339 | } |
| 340 | |
| 341 | static void gpio_sim_dispose_mappings(void *data) |
| 342 | { |
| 343 | struct gpio_sim_chip *chip = data; |
| 344 | unsigned int i; |
| 345 | |
| 346 | for (i = 0; i < chip->gc.ngpio; i++) |
| 347 | irq_dispose_mapping(irq_find_mapping(chip->irq_sim, i)); |
| 348 | } |
| 349 | |
| 350 | static void gpio_sim_sysfs_remove(void *data) |
| 351 | { |
| 352 | struct gpio_sim_chip *chip = data; |
| 353 | |
| 354 | sysfs_remove_groups(&chip->dev->kobj, chip->attr_groups); |
| 355 | } |
| 356 | |
| 357 | static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip) |
| 358 | { |
| 359 | struct device_attribute *val_dev_attr, *pull_dev_attr; |
| 360 | struct gpio_sim_attribute *val_attr, *pull_attr; |
| 361 | unsigned int num_lines = chip->gc.ngpio; |
| 362 | struct device *dev = chip->gc.parent; |
| 363 | struct attribute_group *attr_group; |
| 364 | struct attribute **attrs; |
| 365 | int i, ret; |
| 366 | |
| 367 | chip->attr_groups = devm_kcalloc(dev, sizeof(*chip->attr_groups), |
| 368 | num_lines + 1, GFP_KERNEL); |
| 369 | if (!chip->attr_groups) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | for (i = 0; i < num_lines; i++) { |
| 373 | attr_group = devm_kzalloc(dev, sizeof(*attr_group), GFP_KERNEL); |
| 374 | attrs = devm_kcalloc(dev, GPIO_SIM_NUM_ATTRS, sizeof(*attrs), |
| 375 | GFP_KERNEL); |
| 376 | val_attr = devm_kzalloc(dev, sizeof(*val_attr), GFP_KERNEL); |
| 377 | pull_attr = devm_kzalloc(dev, sizeof(*pull_attr), GFP_KERNEL); |
| 378 | if (!attr_group || !attrs || !val_attr || !pull_attr) |
| 379 | return -ENOMEM; |
| 380 | |
| 381 | attr_group->name = devm_kasprintf(dev, GFP_KERNEL, |
| 382 | "sim_gpio%u", i); |
| 383 | if (!attr_group->name) |
| 384 | return -ENOMEM; |
| 385 | |
| 386 | val_attr->offset = pull_attr->offset = i; |
| 387 | |
| 388 | val_dev_attr = &val_attr->dev_attr; |
| 389 | pull_dev_attr = &pull_attr->dev_attr; |
| 390 | |
| 391 | sysfs_attr_init(&val_dev_attr->attr); |
| 392 | sysfs_attr_init(&pull_dev_attr->attr); |
| 393 | |
| 394 | val_dev_attr->attr.name = "value"; |
| 395 | pull_dev_attr->attr.name = "pull"; |
| 396 | |
| 397 | val_dev_attr->attr.mode = pull_dev_attr->attr.mode = 0644; |
| 398 | |
| 399 | val_dev_attr->show = gpio_sim_sysfs_val_show; |
| 400 | val_dev_attr->store = gpio_sim_sysfs_val_store; |
| 401 | pull_dev_attr->show = gpio_sim_sysfs_pull_show; |
| 402 | pull_dev_attr->store = gpio_sim_sysfs_pull_store; |
| 403 | |
| 404 | attrs[0] = &val_dev_attr->attr; |
| 405 | attrs[1] = &pull_dev_attr->attr; |
| 406 | |
| 407 | attr_group->attrs = attrs; |
| 408 | chip->attr_groups[i] = attr_group; |
| 409 | } |
| 410 | |
| 411 | ret = sysfs_create_groups(&chip->dev->kobj, chip->attr_groups); |
| 412 | if (ret) |
| 413 | return ret; |
| 414 | |
| 415 | return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip); |
| 416 | } |
| 417 | |
| 418 | static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev) |
| 419 | { |
| 420 | struct gpio_sim_chip *chip; |
| 421 | struct gpio_chip *gc; |
| 422 | const char *label; |
| 423 | u32 num_lines; |
| 424 | int ret; |
| 425 | |
| 426 | ret = fwnode_property_read_u32(swnode, "ngpios", &num_lines); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | |
| 430 | if (num_lines > GPIO_SIM_NGPIO_MAX) |
| 431 | return -ERANGE; |
| 432 | |
| 433 | ret = fwnode_property_read_string(swnode, "gpio-sim,label", &label); |
| 434 | if (ret) { |
| 435 | label = devm_kasprintf(dev, GFP_KERNEL, "%s:%pfwP", |
| 436 | dev_name(dev), swnode); |
| 437 | if (!label) |
| 438 | return -ENOMEM; |
| 439 | } |
| 440 | |
| 441 | chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); |
| 442 | if (!chip) |
| 443 | return -ENOMEM; |
| 444 | |
| 445 | chip->request_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); |
| 446 | if (!chip->request_map) |
| 447 | return -ENOMEM; |
| 448 | |
| 449 | chip->direction_map = devm_bitmap_alloc(dev, num_lines, GFP_KERNEL); |
| 450 | if (!chip->direction_map) |
| 451 | return -ENOMEM; |
| 452 | |
| 453 | /* Default to input mode. */ |
| 454 | bitmap_fill(chip->direction_map, num_lines); |
| 455 | |
| 456 | chip->value_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); |
| 457 | if (!chip->value_map) |
| 458 | return -ENOMEM; |
| 459 | |
| 460 | chip->pull_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); |
| 461 | if (!chip->pull_map) |
| 462 | return -ENOMEM; |
| 463 | |
| 464 | chip->irq_sim = devm_irq_domain_create_sim_full(dev, swnode, num_lines, |
| 465 | &gpio_sim_irq_sim_ops, |
| 466 | chip); |
| 467 | if (IS_ERR(chip->irq_sim)) |
| 468 | return PTR_ERR(chip->irq_sim); |
| 469 | |
| 470 | ret = devm_add_action_or_reset(dev, gpio_sim_dispose_mappings, chip); |
| 471 | if (ret) |
| 472 | return ret; |
| 473 | |
| 474 | ret = devm_mutex_init(dev, &chip->lock); |
| 475 | if (ret) |
| 476 | return ret; |
| 477 | |
| 478 | gc = &chip->gc; |
| 479 | gc->base = -1; |
| 480 | gc->ngpio = num_lines; |
| 481 | gc->label = label; |
| 482 | gc->owner = THIS_MODULE; |
| 483 | gc->parent = dev; |
| 484 | gc->fwnode = swnode; |
| 485 | gc->get = gpio_sim_get; |
| 486 | gc->set = gpio_sim_set; |
| 487 | gc->get_multiple = gpio_sim_get_multiple; |
| 488 | gc->set_multiple = gpio_sim_set_multiple; |
| 489 | gc->direction_output = gpio_sim_direction_output; |
| 490 | gc->direction_input = gpio_sim_direction_input; |
| 491 | gc->get_direction = gpio_sim_get_direction; |
| 492 | gc->set_config = gpio_sim_set_config; |
| 493 | gc->to_irq = gpio_sim_to_irq; |
| 494 | gc->request = gpio_sim_request; |
| 495 | gc->free = gpio_sim_free; |
| 496 | gc->dbg_show = PTR_IF(IS_ENABLED(CONFIG_DEBUG_FS), gpio_sim_dbg_show); |
| 497 | gc->can_sleep = true; |
| 498 | |
| 499 | ret = devm_gpiochip_add_data(dev, gc, chip); |
| 500 | if (ret) |
| 501 | return ret; |
| 502 | |
| 503 | chip->dev = device_find_child(dev, swnode, device_match_fwnode); |
| 504 | if (!chip->dev) |
| 505 | return -ENODEV; |
| 506 | |
| 507 | ret = devm_add_action_or_reset(dev, gpio_sim_put_device, chip->dev); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | |
| 511 | /* Used by sysfs callbacks. */ |
| 512 | dev_set_drvdata(chip->dev, chip); |
| 513 | |
| 514 | return gpio_sim_setup_sysfs(chip); |
| 515 | } |
| 516 | |
| 517 | static int gpio_sim_probe(struct platform_device *pdev) |
| 518 | { |
| 519 | struct device *dev = &pdev->dev; |
| 520 | int ret; |
| 521 | |
| 522 | device_for_each_child_node_scoped(dev, swnode) { |
| 523 | ret = gpio_sim_add_bank(swnode, dev); |
| 524 | if (ret) |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static const struct of_device_id gpio_sim_of_match[] = { |
| 532 | { .compatible = "gpio-simulator" }, |
| 533 | { } |
| 534 | }; |
| 535 | MODULE_DEVICE_TABLE(of, gpio_sim_of_match); |
| 536 | |
| 537 | static struct platform_driver gpio_sim_driver = { |
| 538 | .driver = { |
| 539 | .name = "gpio-sim", |
| 540 | .of_match_table = gpio_sim_of_match, |
| 541 | }, |
| 542 | .probe = gpio_sim_probe, |
| 543 | }; |
| 544 | |
| 545 | struct gpio_sim_device { |
| 546 | struct platform_device *pdev; |
| 547 | struct config_group group; |
| 548 | |
| 549 | int id; |
| 550 | |
| 551 | /* |
| 552 | * Each configfs filesystem operation is protected with the subsystem |
| 553 | * mutex. Each separate attribute is protected with the buffer mutex. |
| 554 | * This structure however can be modified by callbacks of different |
| 555 | * attributes so we need another lock. |
| 556 | * |
| 557 | * We use this lock for protecting all data structures owned by this |
| 558 | * object too. |
| 559 | */ |
| 560 | struct mutex lock; |
| 561 | |
| 562 | struct list_head bank_list; |
| 563 | }; |
| 564 | |
| 565 | static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item) |
| 566 | { |
| 567 | struct config_group *group = to_config_group(item); |
| 568 | |
| 569 | return container_of(group, struct gpio_sim_device, group); |
| 570 | } |
| 571 | |
| 572 | struct gpio_sim_bank { |
| 573 | struct config_group group; |
| 574 | |
| 575 | /* |
| 576 | * We could have used the ci_parent field of the config_item but |
| 577 | * configfs is stupid and calls the item's release callback after |
| 578 | * already having cleared the parent pointer even though the parent |
| 579 | * is guaranteed to survive the child... |
| 580 | * |
| 581 | * So we need to store the pointer to the parent struct here. We can |
| 582 | * dereference it anywhere we need with no checks and no locking as |
| 583 | * it's guaranteed to survive the children and protected by configfs |
| 584 | * locks. |
| 585 | * |
| 586 | * Same for other structures. |
| 587 | */ |
| 588 | struct gpio_sim_device *parent; |
| 589 | struct list_head siblings; |
| 590 | |
| 591 | char *label; |
| 592 | unsigned int num_lines; |
| 593 | |
| 594 | struct list_head line_list; |
| 595 | |
| 596 | struct fwnode_handle *swnode; |
| 597 | }; |
| 598 | |
| 599 | static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item) |
| 600 | { |
| 601 | struct config_group *group = to_config_group(item); |
| 602 | |
| 603 | return container_of(group, struct gpio_sim_bank, group); |
| 604 | } |
| 605 | |
| 606 | static bool gpio_sim_bank_has_label(struct gpio_sim_bank *bank) |
| 607 | { |
| 608 | return bank->label && *bank->label; |
| 609 | } |
| 610 | |
| 611 | static struct gpio_sim_device * |
| 612 | gpio_sim_bank_get_device(struct gpio_sim_bank *bank) |
| 613 | { |
| 614 | return bank->parent; |
| 615 | } |
| 616 | |
| 617 | struct gpio_sim_hog; |
| 618 | |
| 619 | struct gpio_sim_line { |
| 620 | struct config_group group; |
| 621 | |
| 622 | struct gpio_sim_bank *parent; |
| 623 | struct list_head siblings; |
| 624 | |
| 625 | unsigned int offset; |
| 626 | char *name; |
| 627 | bool valid; |
| 628 | |
| 629 | /* There can only be one hog per line. */ |
| 630 | struct gpio_sim_hog *hog; |
| 631 | }; |
| 632 | |
| 633 | static struct gpio_sim_line *to_gpio_sim_line(struct config_item *item) |
| 634 | { |
| 635 | struct config_group *group = to_config_group(item); |
| 636 | |
| 637 | return container_of(group, struct gpio_sim_line, group); |
| 638 | } |
| 639 | |
| 640 | static struct gpio_sim_device * |
| 641 | gpio_sim_line_get_device(struct gpio_sim_line *line) |
| 642 | { |
| 643 | struct gpio_sim_bank *bank = line->parent; |
| 644 | |
| 645 | return gpio_sim_bank_get_device(bank); |
| 646 | } |
| 647 | |
| 648 | struct gpio_sim_hog { |
| 649 | struct config_item item; |
| 650 | struct gpio_sim_line *parent; |
| 651 | |
| 652 | char *name; |
| 653 | int dir; |
| 654 | bool active_low; |
| 655 | }; |
| 656 | |
| 657 | static struct gpio_sim_hog *to_gpio_sim_hog(struct config_item *item) |
| 658 | { |
| 659 | return container_of(item, struct gpio_sim_hog, item); |
| 660 | } |
| 661 | |
| 662 | static struct gpio_sim_device *gpio_sim_hog_get_device(struct gpio_sim_hog *hog) |
| 663 | { |
| 664 | struct gpio_sim_line *line = hog->parent; |
| 665 | |
| 666 | return gpio_sim_line_get_device(line); |
| 667 | } |
| 668 | |
| 669 | static bool gpio_sim_device_is_live(struct gpio_sim_device *dev) |
| 670 | { |
| 671 | lockdep_assert_held(&dev->lock); |
| 672 | |
| 673 | return !!dev->pdev; |
| 674 | } |
| 675 | |
| 676 | static char *gpio_sim_strdup_trimmed(const char *str, size_t count) |
| 677 | { |
| 678 | char *trimmed; |
| 679 | |
| 680 | trimmed = kstrndup(skip_spaces(str), count, GFP_KERNEL); |
| 681 | if (!trimmed) |
| 682 | return NULL; |
| 683 | |
| 684 | return strim(trimmed); |
| 685 | } |
| 686 | |
| 687 | static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item, |
| 688 | char *page) |
| 689 | { |
| 690 | struct gpio_sim_device *dev = to_gpio_sim_device(item); |
| 691 | struct platform_device *pdev; |
| 692 | |
| 693 | guard(mutex)(&dev->lock); |
| 694 | |
| 695 | pdev = dev->pdev; |
| 696 | if (pdev) |
| 697 | return sysfs_emit(page, "%s\n", dev_name(&pdev->dev)); |
| 698 | |
| 699 | return sysfs_emit(page, "gpio-sim.%d\n", dev->id); |
| 700 | } |
| 701 | |
| 702 | CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name); |
| 703 | |
| 704 | static ssize_t |
| 705 | gpio_sim_device_config_live_show(struct config_item *item, char *page) |
| 706 | { |
| 707 | struct gpio_sim_device *dev = to_gpio_sim_device(item); |
| 708 | bool live; |
| 709 | |
| 710 | scoped_guard(mutex, &dev->lock) |
| 711 | live = gpio_sim_device_is_live(dev); |
| 712 | |
| 713 | return sysfs_emit(page, "%c\n", live ? '1' : '0'); |
| 714 | } |
| 715 | |
| 716 | static unsigned int gpio_sim_get_line_names_size(struct gpio_sim_bank *bank) |
| 717 | { |
| 718 | struct gpio_sim_line *line; |
| 719 | unsigned int size = 0; |
| 720 | |
| 721 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 722 | if (!line->name || (line->offset >= bank->num_lines)) |
| 723 | continue; |
| 724 | |
| 725 | size = max(size, line->offset + 1); |
| 726 | } |
| 727 | |
| 728 | return size; |
| 729 | } |
| 730 | |
| 731 | static void |
| 732 | gpio_sim_set_line_names(struct gpio_sim_bank *bank, char **line_names) |
| 733 | { |
| 734 | struct gpio_sim_line *line; |
| 735 | |
| 736 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 737 | if (!line->name || (line->offset >= bank->num_lines)) |
| 738 | continue; |
| 739 | |
| 740 | line_names[line->offset] = line->name; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | static unsigned int gpio_sim_get_reserved_ranges_size(struct gpio_sim_bank *bank) |
| 745 | { |
| 746 | struct gpio_sim_line *line; |
| 747 | unsigned int size = 0; |
| 748 | |
| 749 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 750 | if (line->valid) |
| 751 | continue; |
| 752 | |
| 753 | size += 2; |
| 754 | } |
| 755 | |
| 756 | return size; |
| 757 | } |
| 758 | |
| 759 | static void gpio_sim_set_reserved_ranges(struct gpio_sim_bank *bank, |
| 760 | u32 *ranges) |
| 761 | { |
| 762 | struct gpio_sim_line *line; |
| 763 | int i = 0; |
| 764 | |
| 765 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 766 | if (line->valid) |
| 767 | continue; |
| 768 | |
| 769 | ranges[i++] = line->offset; |
| 770 | ranges[i++] = 1; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | static struct fwnode_handle * |
| 775 | gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank, |
| 776 | struct fwnode_handle *parent) |
| 777 | { |
| 778 | unsigned int prop_idx = 0, line_names_size, ranges_size; |
| 779 | struct property_entry properties[GPIO_SIM_PROP_MAX]; |
| 780 | char **line_names __free(kfree) = NULL; |
| 781 | u32 *ranges __free(kfree) = NULL; |
| 782 | |
| 783 | memset(properties, 0, sizeof(properties)); |
| 784 | |
| 785 | properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines); |
| 786 | |
| 787 | if (gpio_sim_bank_has_label(bank)) |
| 788 | properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label", |
| 789 | bank->label); |
| 790 | |
| 791 | line_names_size = gpio_sim_get_line_names_size(bank); |
| 792 | if (line_names_size) { |
| 793 | line_names = kzalloc_objs(*line_names, line_names_size); |
| 794 | if (!line_names) |
| 795 | return ERR_PTR(-ENOMEM); |
| 796 | |
| 797 | gpio_sim_set_line_names(bank, line_names); |
| 798 | |
| 799 | properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( |
| 800 | "gpio-line-names", |
| 801 | line_names, line_names_size); |
| 802 | } |
| 803 | |
| 804 | ranges_size = gpio_sim_get_reserved_ranges_size(bank); |
| 805 | if (ranges_size) { |
| 806 | ranges = kcalloc(ranges_size, sizeof(u32), GFP_KERNEL); |
| 807 | if (!ranges) |
| 808 | return ERR_PTR(-ENOMEM); |
| 809 | |
| 810 | gpio_sim_set_reserved_ranges(bank, ranges); |
| 811 | |
| 812 | properties[prop_idx++] = PROPERTY_ENTRY_U32_ARRAY_LEN( |
| 813 | "gpio-reserved-ranges", |
| 814 | ranges, ranges_size); |
| 815 | } |
| 816 | |
| 817 | return fwnode_create_software_node(properties, parent); |
| 818 | } |
| 819 | |
| 820 | static int gpio_sim_bank_add_hogs(struct gpio_sim_bank *bank) |
| 821 | { |
| 822 | struct property_entry properties[GPIO_SIM_HOG_PROP_MAX]; |
| 823 | struct fwnode_handle *swnode; |
| 824 | struct gpio_sim_line *line; |
| 825 | struct gpio_sim_hog *hog; |
| 826 | unsigned int idx; |
| 827 | u32 gpios[2]; |
| 828 | |
| 829 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 830 | if (!line->hog) |
| 831 | continue; |
| 832 | |
| 833 | hog = line->hog; |
| 834 | |
| 835 | gpios[0] = line->offset; |
| 836 | gpios[1] = hog->active_low ? 1 : 0; |
| 837 | |
| 838 | memset(properties, 0, sizeof(properties)); |
| 839 | |
| 840 | idx = 0; |
| 841 | properties[idx++] = PROPERTY_ENTRY_BOOL("gpio-hog"); |
| 842 | properties[idx++] = PROPERTY_ENTRY_U32_ARRAY("gpios", gpios); |
| 843 | properties[idx++] = PROPERTY_ENTRY_STRING("line-name", hog->name); |
| 844 | |
| 845 | switch (hog->dir) { |
| 846 | case GPIOD_IN: |
| 847 | properties[idx++] = PROPERTY_ENTRY_BOOL("input"); |
| 848 | break; |
| 849 | case GPIOD_OUT_HIGH: |
| 850 | properties[idx++] = PROPERTY_ENTRY_BOOL("output-high"); |
| 851 | break; |
| 852 | case GPIOD_OUT_LOW: |
| 853 | properties[idx++] = PROPERTY_ENTRY_BOOL("output-low"); |
| 854 | break; |
| 855 | default: |
| 856 | /* Would have been validated at configfs store. */ |
| 857 | WARN(1, "Unexpected hog direction value: %d", hog->dir); |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | |
| 861 | swnode = fwnode_create_software_node(properties, bank->swnode); |
| 862 | if (IS_ERR(swnode)) |
| 863 | return PTR_ERR(swnode); |
| 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode) |
| 870 | { |
| 871 | struct fwnode_handle *child; |
| 872 | |
| 873 | fwnode_for_each_child_node(swnode, child) |
| 874 | gpio_sim_remove_swnode_recursive(child); |
| 875 | |
| 876 | fwnode_remove_software_node(swnode); |
| 877 | } |
| 878 | |
| 879 | static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev) |
| 880 | { |
| 881 | struct gpio_sim_bank *this, *pos; |
| 882 | |
| 883 | list_for_each_entry(this, &dev->bank_list, siblings) { |
| 884 | list_for_each_entry(pos, &dev->bank_list, siblings) { |
| 885 | if (this == pos || (!this->label || !pos->label)) |
| 886 | continue; |
| 887 | |
| 888 | if (strcmp(this->label, pos->label) == 0) |
| 889 | return true; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | static int gpio_sim_device_activate(struct gpio_sim_device *dev) |
| 897 | { |
| 898 | struct platform_device_info pdevinfo; |
| 899 | struct platform_device *pdev; |
| 900 | struct fwnode_handle *swnode; |
| 901 | struct gpio_sim_bank *bank; |
| 902 | int ret = 0; |
| 903 | |
| 904 | lockdep_assert_held(&dev->lock); |
| 905 | |
| 906 | if (list_empty(&dev->bank_list)) |
| 907 | return -ENODATA; |
| 908 | |
| 909 | /* |
| 910 | * Non-unique GPIO device labels are a corner-case we don't support |
| 911 | * as it would interfere with machine hogging mechanism and has little |
| 912 | * use in real life. |
| 913 | */ |
| 914 | if (gpio_sim_bank_labels_non_unique(dev)) |
| 915 | return -EINVAL; |
| 916 | |
| 917 | memset(&pdevinfo, 0, sizeof(pdevinfo)); |
| 918 | |
| 919 | swnode = fwnode_create_software_node(NULL, NULL); |
| 920 | if (IS_ERR(swnode)) |
| 921 | return PTR_ERR(swnode); |
| 922 | |
| 923 | list_for_each_entry(bank, &dev->bank_list, siblings) { |
| 924 | bank->swnode = gpio_sim_make_bank_swnode(bank, swnode); |
| 925 | if (IS_ERR(bank->swnode)) { |
| 926 | ret = PTR_ERR(bank->swnode); |
| 927 | goto err_remove_swnode; |
| 928 | } |
| 929 | |
| 930 | ret = gpio_sim_bank_add_hogs(bank); |
| 931 | if (ret) |
| 932 | goto err_remove_swnode; |
| 933 | } |
| 934 | |
| 935 | pdevinfo.name = "gpio-sim"; |
| 936 | pdevinfo.fwnode = swnode; |
| 937 | pdevinfo.id = dev->id; |
| 938 | |
| 939 | pdev = platform_device_register_full(&pdevinfo); |
| 940 | if (IS_ERR(pdev)) { |
| 941 | ret = PTR_ERR(pdev); |
| 942 | goto err_remove_swnode; |
| 943 | } |
| 944 | |
| 945 | wait_for_device_probe(); |
| 946 | |
| 947 | scoped_guard(device, &pdev->dev) { |
| 948 | if (!device_is_bound(&pdev->dev)) { |
| 949 | ret = -ENXIO; |
| 950 | goto err_unregister_pdev; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | dev->pdev = pdev; |
| 955 | return 0; |
| 956 | |
| 957 | err_unregister_pdev: |
| 958 | platform_device_unregister(pdev); |
| 959 | err_remove_swnode: |
| 960 | gpio_sim_remove_swnode_recursive(swnode); |
| 961 | |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | static void gpio_sim_device_deactivate(struct gpio_sim_device *dev) |
| 966 | { |
| 967 | struct fwnode_handle *swnode; |
| 968 | |
| 969 | lockdep_assert_held(&dev->lock); |
| 970 | |
| 971 | swnode = dev_fwnode(&dev->pdev->dev); |
| 972 | platform_device_unregister(dev->pdev); |
| 973 | dev->pdev = NULL; |
| 974 | gpio_sim_remove_swnode_recursive(swnode); |
| 975 | } |
| 976 | |
| 977 | static void |
| 978 | gpio_sim_device_lockup_configfs(struct gpio_sim_device *dev, bool lock) |
| 979 | { |
| 980 | struct configfs_subsystem *subsys = dev->group.cg_subsys; |
| 981 | struct gpio_sim_bank *bank; |
| 982 | struct gpio_sim_line *line; |
| 983 | struct config_item *item; |
| 984 | |
| 985 | /* |
| 986 | * The device only needs to depend on leaf entries. This is |
| 987 | * sufficient to lock up all the configfs entries that the |
| 988 | * instantiated, alive device depends on. |
| 989 | */ |
| 990 | list_for_each_entry(bank, &dev->bank_list, siblings) { |
| 991 | list_for_each_entry(line, &bank->line_list, siblings) { |
| 992 | item = line->hog ? &line->hog->item |
| 993 | : &line->group.cg_item; |
| 994 | |
| 995 | if (lock) |
| 996 | WARN_ON(configfs_depend_item_unlocked(subsys, |
| 997 | item)); |
| 998 | else |
| 999 | configfs_undepend_item_unlocked(item); |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | static ssize_t |
| 1005 | gpio_sim_device_config_live_store(struct config_item *item, |
| 1006 | const char *page, size_t count) |
| 1007 | { |
| 1008 | struct gpio_sim_device *dev = to_gpio_sim_device(item); |
| 1009 | bool live; |
| 1010 | int ret; |
| 1011 | |
| 1012 | ret = kstrtobool(page, &live); |
| 1013 | if (ret) |
| 1014 | return ret; |
| 1015 | |
| 1016 | if (live) |
| 1017 | gpio_sim_device_lockup_configfs(dev, true); |
| 1018 | |
| 1019 | scoped_guard(mutex, &dev->lock) { |
| 1020 | if (live == gpio_sim_device_is_live(dev)) |
| 1021 | ret = -EPERM; |
| 1022 | else if (live) |
| 1023 | ret = gpio_sim_device_activate(dev); |
| 1024 | else |
| 1025 | gpio_sim_device_deactivate(dev); |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Undepend is required only if device disablement (live == 0) |
| 1030 | * succeeds or if device enablement (live == 1) fails. |
| 1031 | */ |
| 1032 | if (live == !!ret) |
| 1033 | gpio_sim_device_lockup_configfs(dev, false); |
| 1034 | |
| 1035 | return ret ?: count; |
| 1036 | } |
| 1037 | |
| 1038 | CONFIGFS_ATTR(gpio_sim_device_config_, live); |
| 1039 | |
| 1040 | static struct configfs_attribute *gpio_sim_device_config_attrs[] = { |
| 1041 | &gpio_sim_device_config_attr_dev_name, |
| 1042 | &gpio_sim_device_config_attr_live, |
| 1043 | NULL |
| 1044 | }; |
| 1045 | |
| 1046 | struct gpio_sim_chip_name_ctx { |
| 1047 | struct fwnode_handle *swnode; |
| 1048 | char *page; |
| 1049 | }; |
| 1050 | |
| 1051 | static int gpio_sim_emit_chip_name(struct device *dev, void *data) |
| 1052 | { |
| 1053 | struct gpio_sim_chip_name_ctx *ctx = data; |
| 1054 | |
| 1055 | /* This would be the sysfs device exported in /sys/class/gpio. */ |
| 1056 | if (dev->class) |
| 1057 | return 0; |
| 1058 | |
| 1059 | if (device_match_fwnode(dev, ctx->swnode)) |
| 1060 | return sysfs_emit(ctx->page, "%s\n", dev_name(dev)); |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item, |
| 1066 | char *page) |
| 1067 | { |
| 1068 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1069 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1070 | struct gpio_sim_chip_name_ctx ctx = { bank->swnode, page }; |
| 1071 | |
| 1072 | guard(mutex)(&dev->lock); |
| 1073 | |
| 1074 | if (gpio_sim_device_is_live(dev)) |
| 1075 | return device_for_each_child(&dev->pdev->dev, &ctx, |
| 1076 | gpio_sim_emit_chip_name); |
| 1077 | |
| 1078 | return sysfs_emit(page, "none\n"); |
| 1079 | } |
| 1080 | |
| 1081 | CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name); |
| 1082 | |
| 1083 | static ssize_t |
| 1084 | gpio_sim_bank_config_label_show(struct config_item *item, char *page) |
| 1085 | { |
| 1086 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1087 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1088 | |
| 1089 | guard(mutex)(&dev->lock); |
| 1090 | |
| 1091 | return sysfs_emit(page, "%s\n", bank->label ?: ""); |
| 1092 | } |
| 1093 | |
| 1094 | static ssize_t gpio_sim_bank_config_label_store(struct config_item *item, |
| 1095 | const char *page, size_t count) |
| 1096 | { |
| 1097 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1098 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1099 | char *trimmed; |
| 1100 | |
| 1101 | guard(mutex)(&dev->lock); |
| 1102 | |
| 1103 | if (gpio_sim_device_is_live(dev)) |
| 1104 | return -EBUSY; |
| 1105 | |
| 1106 | trimmed = gpio_sim_strdup_trimmed(page, count); |
| 1107 | if (!trimmed) |
| 1108 | return -ENOMEM; |
| 1109 | |
| 1110 | kfree(bank->label); |
| 1111 | bank->label = trimmed; |
| 1112 | |
| 1113 | return count; |
| 1114 | } |
| 1115 | |
| 1116 | CONFIGFS_ATTR(gpio_sim_bank_config_, label); |
| 1117 | |
| 1118 | static ssize_t |
| 1119 | gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page) |
| 1120 | { |
| 1121 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1122 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1123 | |
| 1124 | guard(mutex)(&dev->lock); |
| 1125 | |
| 1126 | return sysfs_emit(page, "%u\n", bank->num_lines); |
| 1127 | } |
| 1128 | |
| 1129 | static ssize_t |
| 1130 | gpio_sim_bank_config_num_lines_store(struct config_item *item, |
| 1131 | const char *page, size_t count) |
| 1132 | { |
| 1133 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1134 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1135 | unsigned int num_lines; |
| 1136 | int ret; |
| 1137 | |
| 1138 | ret = kstrtouint(page, 0, &num_lines); |
| 1139 | if (ret) |
| 1140 | return ret; |
| 1141 | |
| 1142 | if (num_lines == 0) |
| 1143 | return -EINVAL; |
| 1144 | |
| 1145 | guard(mutex)(&dev->lock); |
| 1146 | |
| 1147 | if (gpio_sim_device_is_live(dev)) |
| 1148 | return -EBUSY; |
| 1149 | |
| 1150 | bank->num_lines = num_lines; |
| 1151 | |
| 1152 | return count; |
| 1153 | } |
| 1154 | |
| 1155 | CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines); |
| 1156 | |
| 1157 | static struct configfs_attribute *gpio_sim_bank_config_attrs[] = { |
| 1158 | &gpio_sim_bank_config_attr_chip_name, |
| 1159 | &gpio_sim_bank_config_attr_label, |
| 1160 | &gpio_sim_bank_config_attr_num_lines, |
| 1161 | NULL |
| 1162 | }; |
| 1163 | |
| 1164 | static ssize_t |
| 1165 | gpio_sim_line_config_name_show(struct config_item *item, char *page) |
| 1166 | { |
| 1167 | struct gpio_sim_line *line = to_gpio_sim_line(item); |
| 1168 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1169 | |
| 1170 | guard(mutex)(&dev->lock); |
| 1171 | |
| 1172 | return sysfs_emit(page, "%s\n", line->name ?: ""); |
| 1173 | } |
| 1174 | |
| 1175 | static ssize_t gpio_sim_line_config_name_store(struct config_item *item, |
| 1176 | const char *page, size_t count) |
| 1177 | { |
| 1178 | struct gpio_sim_line *line = to_gpio_sim_line(item); |
| 1179 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1180 | char *trimmed; |
| 1181 | |
| 1182 | guard(mutex)(&dev->lock); |
| 1183 | |
| 1184 | if (gpio_sim_device_is_live(dev)) |
| 1185 | return -EBUSY; |
| 1186 | |
| 1187 | trimmed = gpio_sim_strdup_trimmed(page, count); |
| 1188 | if (!trimmed) |
| 1189 | return -ENOMEM; |
| 1190 | |
| 1191 | kfree(line->name); |
| 1192 | line->name = trimmed; |
| 1193 | |
| 1194 | return count; |
| 1195 | } |
| 1196 | |
| 1197 | CONFIGFS_ATTR(gpio_sim_line_config_, name); |
| 1198 | |
| 1199 | static ssize_t |
| 1200 | gpio_sim_line_config_valid_show(struct config_item *item, char *page) |
| 1201 | { |
| 1202 | struct gpio_sim_line *line = to_gpio_sim_line(item); |
| 1203 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1204 | |
| 1205 | guard(mutex)(&dev->lock); |
| 1206 | |
| 1207 | return sysfs_emit(page, "%c\n", line->valid ? '1' : '0'); |
| 1208 | } |
| 1209 | |
| 1210 | static ssize_t gpio_sim_line_config_valid_store(struct config_item *item, |
| 1211 | const char *page, size_t count) |
| 1212 | { |
| 1213 | struct gpio_sim_line *line = to_gpio_sim_line(item); |
| 1214 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1215 | bool valid; |
| 1216 | int ret; |
| 1217 | |
| 1218 | ret = kstrtobool(page, &valid); |
| 1219 | if (ret) |
| 1220 | return ret; |
| 1221 | |
| 1222 | guard(mutex)(&dev->lock); |
| 1223 | |
| 1224 | line->valid = valid; |
| 1225 | |
| 1226 | return count; |
| 1227 | } |
| 1228 | |
| 1229 | CONFIGFS_ATTR(gpio_sim_line_config_, valid); |
| 1230 | |
| 1231 | static struct configfs_attribute *gpio_sim_line_config_attrs[] = { |
| 1232 | &gpio_sim_line_config_attr_name, |
| 1233 | &gpio_sim_line_config_attr_valid, |
| 1234 | NULL |
| 1235 | }; |
| 1236 | |
| 1237 | static ssize_t gpio_sim_hog_config_name_show(struct config_item *item, |
| 1238 | char *page) |
| 1239 | { |
| 1240 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1241 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1242 | |
| 1243 | guard(mutex)(&dev->lock); |
| 1244 | |
| 1245 | return sysfs_emit(page, "%s\n", hog->name ?: ""); |
| 1246 | } |
| 1247 | |
| 1248 | static ssize_t gpio_sim_hog_config_name_store(struct config_item *item, |
| 1249 | const char *page, size_t count) |
| 1250 | { |
| 1251 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1252 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1253 | char *trimmed; |
| 1254 | |
| 1255 | guard(mutex)(&dev->lock); |
| 1256 | |
| 1257 | if (gpio_sim_device_is_live(dev)) |
| 1258 | return -EBUSY; |
| 1259 | |
| 1260 | trimmed = gpio_sim_strdup_trimmed(page, count); |
| 1261 | if (!trimmed) |
| 1262 | return -ENOMEM; |
| 1263 | |
| 1264 | kfree(hog->name); |
| 1265 | hog->name = trimmed; |
| 1266 | |
| 1267 | return count; |
| 1268 | } |
| 1269 | |
| 1270 | CONFIGFS_ATTR(gpio_sim_hog_config_, name); |
| 1271 | |
| 1272 | static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item, |
| 1273 | char *page) |
| 1274 | { |
| 1275 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1276 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1277 | char *repr; |
| 1278 | int dir; |
| 1279 | |
| 1280 | scoped_guard(mutex, &dev->lock) |
| 1281 | dir = hog->dir; |
| 1282 | |
| 1283 | switch (dir) { |
| 1284 | case GPIOD_IN: |
| 1285 | repr = "input"; |
| 1286 | break; |
| 1287 | case GPIOD_OUT_HIGH: |
| 1288 | repr = "output-high"; |
| 1289 | break; |
| 1290 | case GPIOD_OUT_LOW: |
| 1291 | repr = "output-low"; |
| 1292 | break; |
| 1293 | default: |
| 1294 | /* This would be a programmer bug. */ |
| 1295 | WARN(1, "Unexpected hog direction value: %d", dir); |
| 1296 | return -EINVAL; |
| 1297 | } |
| 1298 | |
| 1299 | return sysfs_emit(page, "%s\n", repr); |
| 1300 | } |
| 1301 | |
| 1302 | static ssize_t |
| 1303 | gpio_sim_hog_config_direction_store(struct config_item *item, |
| 1304 | const char *page, size_t count) |
| 1305 | { |
| 1306 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1307 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1308 | int dir; |
| 1309 | |
| 1310 | guard(mutex)(&dev->lock); |
| 1311 | |
| 1312 | if (gpio_sim_device_is_live(dev)) |
| 1313 | return -EBUSY; |
| 1314 | |
| 1315 | if (sysfs_streq(page, "input")) |
| 1316 | dir = GPIOD_IN; |
| 1317 | else if (sysfs_streq(page, "output-high")) |
| 1318 | dir = GPIOD_OUT_HIGH; |
| 1319 | else if (sysfs_streq(page, "output-low")) |
| 1320 | dir = GPIOD_OUT_LOW; |
| 1321 | else |
| 1322 | return -EINVAL; |
| 1323 | |
| 1324 | hog->dir = dir; |
| 1325 | |
| 1326 | return count; |
| 1327 | } |
| 1328 | |
| 1329 | CONFIGFS_ATTR(gpio_sim_hog_config_, direction); |
| 1330 | |
| 1331 | static ssize_t gpio_sim_hog_config_active_low_show(struct config_item *item, |
| 1332 | char *page) |
| 1333 | { |
| 1334 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1335 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1336 | |
| 1337 | guard(mutex)(&dev->lock); |
| 1338 | |
| 1339 | return sysfs_emit(page, "%c\n", hog->active_low ? '1' : '0'); |
| 1340 | } |
| 1341 | |
| 1342 | static ssize_t |
| 1343 | gpio_sim_hog_config_active_low_store(struct config_item *item, |
| 1344 | const char *page, size_t count) |
| 1345 | { |
| 1346 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1347 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1348 | bool active_low; |
| 1349 | int ret; |
| 1350 | |
| 1351 | guard(mutex)(&dev->lock); |
| 1352 | |
| 1353 | if (gpio_sim_device_is_live(dev)) |
| 1354 | return -EBUSY; |
| 1355 | |
| 1356 | ret = kstrtobool(page, &active_low); |
| 1357 | if (ret) |
| 1358 | return ret; |
| 1359 | |
| 1360 | hog->active_low = active_low; |
| 1361 | |
| 1362 | return count; |
| 1363 | } |
| 1364 | |
| 1365 | CONFIGFS_ATTR(gpio_sim_hog_config_, active_low); |
| 1366 | |
| 1367 | static struct configfs_attribute *gpio_sim_hog_config_attrs[] = { |
| 1368 | &gpio_sim_hog_config_attr_name, |
| 1369 | &gpio_sim_hog_config_attr_direction, |
| 1370 | &gpio_sim_hog_config_attr_active_low, |
| 1371 | NULL |
| 1372 | }; |
| 1373 | |
| 1374 | static void gpio_sim_hog_config_item_release(struct config_item *item) |
| 1375 | { |
| 1376 | struct gpio_sim_hog *hog = to_gpio_sim_hog(item); |
| 1377 | struct gpio_sim_line *line = hog->parent; |
| 1378 | struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog); |
| 1379 | |
| 1380 | scoped_guard(mutex, &dev->lock) |
| 1381 | line->hog = NULL; |
| 1382 | |
| 1383 | kfree(hog->name); |
| 1384 | kfree(hog); |
| 1385 | } |
| 1386 | |
| 1387 | static const struct configfs_item_operations gpio_sim_hog_config_item_ops = { |
| 1388 | .release = gpio_sim_hog_config_item_release, |
| 1389 | }; |
| 1390 | |
| 1391 | static const struct config_item_type gpio_sim_hog_config_type = { |
| 1392 | .ct_item_ops = &gpio_sim_hog_config_item_ops, |
| 1393 | .ct_attrs = gpio_sim_hog_config_attrs, |
| 1394 | .ct_owner = THIS_MODULE, |
| 1395 | }; |
| 1396 | |
| 1397 | static struct config_item * |
| 1398 | gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name) |
| 1399 | { |
| 1400 | struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item); |
| 1401 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1402 | struct gpio_sim_hog *hog; |
| 1403 | |
| 1404 | if (strcmp(name, "hog") != 0) |
| 1405 | return ERR_PTR(-EINVAL); |
| 1406 | |
| 1407 | guard(mutex)(&dev->lock); |
| 1408 | |
| 1409 | hog = kzalloc_obj(*hog); |
| 1410 | if (!hog) |
| 1411 | return ERR_PTR(-ENOMEM); |
| 1412 | |
| 1413 | config_item_init_type_name(&hog->item, name, |
| 1414 | &gpio_sim_hog_config_type); |
| 1415 | |
| 1416 | hog->dir = GPIOD_IN; |
| 1417 | hog->name = NULL; |
| 1418 | hog->parent = line; |
| 1419 | line->hog = hog; |
| 1420 | |
| 1421 | return &hog->item; |
| 1422 | } |
| 1423 | |
| 1424 | static void gpio_sim_line_config_group_release(struct config_item *item) |
| 1425 | { |
| 1426 | struct gpio_sim_line *line = to_gpio_sim_line(item); |
| 1427 | struct gpio_sim_device *dev = gpio_sim_line_get_device(line); |
| 1428 | |
| 1429 | scoped_guard(mutex, &dev->lock) |
| 1430 | list_del(&line->siblings); |
| 1431 | |
| 1432 | kfree(line->name); |
| 1433 | kfree(line); |
| 1434 | } |
| 1435 | |
| 1436 | static const struct configfs_item_operations gpio_sim_line_config_item_ops = { |
| 1437 | .release = gpio_sim_line_config_group_release, |
| 1438 | }; |
| 1439 | |
| 1440 | static const struct configfs_group_operations gpio_sim_line_config_group_ops = { |
| 1441 | .make_item = gpio_sim_line_config_make_hog_item, |
| 1442 | }; |
| 1443 | |
| 1444 | static const struct config_item_type gpio_sim_line_config_type = { |
| 1445 | .ct_item_ops = &gpio_sim_line_config_item_ops, |
| 1446 | .ct_group_ops = &gpio_sim_line_config_group_ops, |
| 1447 | .ct_attrs = gpio_sim_line_config_attrs, |
| 1448 | .ct_owner = THIS_MODULE, |
| 1449 | }; |
| 1450 | |
| 1451 | static struct config_group * |
| 1452 | gpio_sim_bank_config_make_line_group(struct config_group *group, |
| 1453 | const char *name) |
| 1454 | { |
| 1455 | struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item); |
| 1456 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1457 | struct gpio_sim_line *line; |
| 1458 | unsigned int offset; |
| 1459 | int ret, nchar; |
| 1460 | |
| 1461 | ret = sscanf(name, "line%u%n", &offset, &nchar); |
| 1462 | if (ret != 1 || nchar != strlen(name)) |
| 1463 | return ERR_PTR(-EINVAL); |
| 1464 | |
| 1465 | guard(mutex)(&dev->lock); |
| 1466 | |
| 1467 | if (gpio_sim_device_is_live(dev)) |
| 1468 | return ERR_PTR(-EBUSY); |
| 1469 | |
| 1470 | line = kzalloc_obj(*line); |
| 1471 | if (!line) |
| 1472 | return ERR_PTR(-ENOMEM); |
| 1473 | |
| 1474 | config_group_init_type_name(&line->group, name, |
| 1475 | &gpio_sim_line_config_type); |
| 1476 | |
| 1477 | line->parent = bank; |
| 1478 | line->offset = offset; |
| 1479 | line->valid = true; |
| 1480 | list_add_tail(&line->siblings, &bank->line_list); |
| 1481 | |
| 1482 | return &line->group; |
| 1483 | } |
| 1484 | |
| 1485 | static void gpio_sim_bank_config_group_release(struct config_item *item) |
| 1486 | { |
| 1487 | struct gpio_sim_bank *bank = to_gpio_sim_bank(item); |
| 1488 | struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank); |
| 1489 | |
| 1490 | scoped_guard(mutex, &dev->lock) |
| 1491 | list_del(&bank->siblings); |
| 1492 | |
| 1493 | kfree(bank->label); |
| 1494 | kfree(bank); |
| 1495 | } |
| 1496 | |
| 1497 | static const struct configfs_item_operations gpio_sim_bank_config_item_ops = { |
| 1498 | .release = gpio_sim_bank_config_group_release, |
| 1499 | }; |
| 1500 | |
| 1501 | static const struct configfs_group_operations gpio_sim_bank_config_group_ops = { |
| 1502 | .make_group = gpio_sim_bank_config_make_line_group, |
| 1503 | }; |
| 1504 | |
| 1505 | static const struct config_item_type gpio_sim_bank_config_group_type = { |
| 1506 | .ct_item_ops = &gpio_sim_bank_config_item_ops, |
| 1507 | .ct_group_ops = &gpio_sim_bank_config_group_ops, |
| 1508 | .ct_attrs = gpio_sim_bank_config_attrs, |
| 1509 | .ct_owner = THIS_MODULE, |
| 1510 | }; |
| 1511 | |
| 1512 | static struct config_group * |
| 1513 | gpio_sim_device_config_make_bank_group(struct config_group *group, |
| 1514 | const char *name) |
| 1515 | { |
| 1516 | struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item); |
| 1517 | struct gpio_sim_bank *bank; |
| 1518 | |
| 1519 | guard(mutex)(&dev->lock); |
| 1520 | |
| 1521 | if (gpio_sim_device_is_live(dev)) |
| 1522 | return ERR_PTR(-EBUSY); |
| 1523 | |
| 1524 | bank = kzalloc_obj(*bank); |
| 1525 | if (!bank) |
| 1526 | return ERR_PTR(-ENOMEM); |
| 1527 | |
| 1528 | config_group_init_type_name(&bank->group, name, |
| 1529 | &gpio_sim_bank_config_group_type); |
| 1530 | bank->num_lines = 1; |
| 1531 | bank->parent = dev; |
| 1532 | INIT_LIST_HEAD(&bank->line_list); |
| 1533 | list_add_tail(&bank->siblings, &dev->bank_list); |
| 1534 | |
| 1535 | return &bank->group; |
| 1536 | } |
| 1537 | |
| 1538 | static void gpio_sim_device_config_group_release(struct config_item *item) |
| 1539 | { |
| 1540 | struct gpio_sim_device *dev = to_gpio_sim_device(item); |
| 1541 | |
| 1542 | scoped_guard(mutex, &dev->lock) { |
| 1543 | if (gpio_sim_device_is_live(dev)) |
| 1544 | gpio_sim_device_deactivate(dev); |
| 1545 | } |
| 1546 | |
| 1547 | mutex_destroy(&dev->lock); |
| 1548 | ida_free(&gpio_sim_ida, dev->id); |
| 1549 | kfree(dev); |
| 1550 | } |
| 1551 | |
| 1552 | static const struct configfs_item_operations gpio_sim_device_config_item_ops = { |
| 1553 | .release = gpio_sim_device_config_group_release, |
| 1554 | }; |
| 1555 | |
| 1556 | static const struct configfs_group_operations gpio_sim_device_config_group_ops = { |
| 1557 | .make_group = gpio_sim_device_config_make_bank_group, |
| 1558 | }; |
| 1559 | |
| 1560 | static const struct config_item_type gpio_sim_device_config_group_type = { |
| 1561 | .ct_item_ops = &gpio_sim_device_config_item_ops, |
| 1562 | .ct_group_ops = &gpio_sim_device_config_group_ops, |
| 1563 | .ct_attrs = gpio_sim_device_config_attrs, |
| 1564 | .ct_owner = THIS_MODULE, |
| 1565 | }; |
| 1566 | |
| 1567 | static struct config_group * |
| 1568 | gpio_sim_config_make_device_group(struct config_group *group, const char *name) |
| 1569 | { |
| 1570 | int id; |
| 1571 | |
| 1572 | struct gpio_sim_device *dev __free(kfree) = kzalloc_obj(*dev); |
| 1573 | if (!dev) |
| 1574 | return ERR_PTR(-ENOMEM); |
| 1575 | |
| 1576 | id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); |
| 1577 | if (id < 0) |
| 1578 | return ERR_PTR(id); |
| 1579 | |
| 1580 | config_group_init_type_name(&dev->group, name, |
| 1581 | &gpio_sim_device_config_group_type); |
| 1582 | dev->id = id; |
| 1583 | mutex_init(&dev->lock); |
| 1584 | INIT_LIST_HEAD(&dev->bank_list); |
| 1585 | |
| 1586 | return &no_free_ptr(dev)->group; |
| 1587 | } |
| 1588 | |
| 1589 | static const struct configfs_group_operations gpio_sim_config_group_ops = { |
| 1590 | .make_group = gpio_sim_config_make_device_group, |
| 1591 | }; |
| 1592 | |
| 1593 | static const struct config_item_type gpio_sim_config_type = { |
| 1594 | .ct_group_ops = &gpio_sim_config_group_ops, |
| 1595 | .ct_owner = THIS_MODULE, |
| 1596 | }; |
| 1597 | |
| 1598 | static struct configfs_subsystem gpio_sim_config_subsys = { |
| 1599 | .su_group = { |
| 1600 | .cg_item = { |
| 1601 | .ci_namebuf = "gpio-sim", |
| 1602 | .ci_type = &gpio_sim_config_type, |
| 1603 | }, |
| 1604 | }, |
| 1605 | }; |
| 1606 | |
| 1607 | static int __init gpio_sim_init(void) |
| 1608 | { |
| 1609 | int ret; |
| 1610 | |
| 1611 | ret = platform_driver_register(&gpio_sim_driver); |
| 1612 | if (ret) { |
| 1613 | pr_err("Error %d while registering the platform driver\n", ret); |
| 1614 | return ret; |
| 1615 | } |
| 1616 | |
| 1617 | config_group_init(&gpio_sim_config_subsys.su_group); |
| 1618 | mutex_init(&gpio_sim_config_subsys.su_mutex); |
| 1619 | ret = configfs_register_subsystem(&gpio_sim_config_subsys); |
| 1620 | if (ret) { |
| 1621 | pr_err("Error %d while registering the configfs subsystem %s\n", |
| 1622 | ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf); |
| 1623 | mutex_destroy(&gpio_sim_config_subsys.su_mutex); |
| 1624 | platform_driver_unregister(&gpio_sim_driver); |
| 1625 | return ret; |
| 1626 | } |
| 1627 | |
| 1628 | return 0; |
| 1629 | } |
| 1630 | module_init(gpio_sim_init); |
| 1631 | |
| 1632 | static void __exit gpio_sim_exit(void) |
| 1633 | { |
| 1634 | configfs_unregister_subsystem(&gpio_sim_config_subsys); |
| 1635 | mutex_destroy(&gpio_sim_config_subsys.su_mutex); |
| 1636 | platform_driver_unregister(&gpio_sim_driver); |
| 1637 | } |
| 1638 | module_exit(gpio_sim_exit); |
| 1639 | |
| 1640 | MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); |
| 1641 | MODULE_DESCRIPTION("GPIO Simulator Module"); |
| 1642 | MODULE_LICENSE("GPL"); |