| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (C) 2025 Linaro Ltd. |
| 4 | */ |
| 5 | |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | |
| 8 | #include <linux/auxiliary_bus.h> |
| 9 | #include <linux/cleanup.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/fwnode.h> |
| 12 | #include <linux/gpio/consumer.h> |
| 13 | #include <linux/gpio/machine.h> |
| 14 | #include <linux/idr.h> |
| 15 | #include <linux/kref.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/lockdep.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/overflow.h> |
| 22 | #include <linux/printk.h> |
| 23 | #include <linux/property.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/string.h> |
| 26 | |
| 27 | #include "gpiolib.h" |
| 28 | #include "gpiolib-shared.h" |
| 29 | |
| 30 | /* Represents a single reference to a GPIO pin. */ |
| 31 | struct gpio_shared_ref { |
| 32 | struct list_head list; |
| 33 | /* Firmware node associated with this GPIO's consumer. */ |
| 34 | struct fwnode_handle *fwnode; |
| 35 | /* GPIO flags this consumer uses for the request. */ |
| 36 | enum gpiod_flags flags; |
| 37 | char *con_id; |
| 38 | int dev_id; |
| 39 | /* Protects the auxiliary device struct and the lookup table. */ |
| 40 | struct mutex lock; |
| 41 | struct lock_class_key lock_key; |
| 42 | struct auxiliary_device adev; |
| 43 | struct gpiod_lookup_table *lookup; |
| 44 | bool is_reset_gpio; |
| 45 | }; |
| 46 | |
| 47 | /* Represents a single GPIO pin. */ |
| 48 | struct gpio_shared_entry { |
| 49 | struct list_head list; |
| 50 | /* Firmware node associated with the GPIO controller. */ |
| 51 | struct fwnode_handle *fwnode; |
| 52 | /* Hardware offset of the GPIO within its chip. */ |
| 53 | unsigned int offset; |
| 54 | /* Index in the property value array. */ |
| 55 | size_t index; |
| 56 | /* Synchronizes the modification of shared_desc. */ |
| 57 | struct mutex lock; |
| 58 | struct gpio_shared_desc *shared_desc; |
| 59 | struct kref ref; |
| 60 | struct list_head refs; |
| 61 | }; |
| 62 | |
| 63 | static LIST_HEAD(gpio_shared_list); |
| 64 | static DEFINE_IDA(gpio_shared_ida); |
| 65 | |
| 66 | #if IS_ENABLED(CONFIG_OF) |
| 67 | static struct gpio_shared_entry * |
| 68 | gpio_shared_find_entry(struct fwnode_handle *controller_node, |
| 69 | unsigned int offset) |
| 70 | { |
| 71 | struct gpio_shared_entry *entry; |
| 72 | |
| 73 | list_for_each_entry(entry, &gpio_shared_list, list) { |
| 74 | if (entry->fwnode == controller_node && entry->offset == offset) |
| 75 | return entry; |
| 76 | } |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | static struct gpio_shared_ref *gpio_shared_make_ref(struct fwnode_handle *fwnode, |
| 82 | const char *con_id, |
| 83 | enum gpiod_flags flags) |
| 84 | { |
| 85 | char *con_id_cpy __free(kfree) = NULL; |
| 86 | |
| 87 | struct gpio_shared_ref *ref __free(kfree) = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 88 | if (!ref) |
| 89 | return NULL; |
| 90 | |
| 91 | if (con_id) { |
| 92 | con_id_cpy = kstrdup(s: con_id, GFP_KERNEL); |
| 93 | if (!con_id_cpy) |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | ref->dev_id = ida_alloc(ida: &gpio_shared_ida, GFP_KERNEL); |
| 98 | if (ref->dev_id < 0) |
| 99 | return NULL; |
| 100 | |
| 101 | ref->flags = flags; |
| 102 | ref->con_id = no_free_ptr(con_id_cpy); |
| 103 | ref->fwnode = fwnode; |
| 104 | lockdep_register_key(key: &ref->lock_key); |
| 105 | mutex_init_with_key(&ref->lock, &ref->lock_key); |
| 106 | |
| 107 | return no_free_ptr(ref); |
| 108 | } |
| 109 | |
| 110 | static int gpio_shared_setup_reset_proxy(struct gpio_shared_entry *entry, |
| 111 | enum gpiod_flags flags) |
| 112 | { |
| 113 | struct gpio_shared_ref *ref; |
| 114 | |
| 115 | list_for_each_entry(ref, &entry->refs, list) { |
| 116 | if (ref->is_reset_gpio) |
| 117 | /* Already set-up. */ |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | ref = gpio_shared_make_ref(NULL, con_id: "reset" , flags); |
| 122 | if (!ref) |
| 123 | return -ENOMEM; |
| 124 | |
| 125 | ref->is_reset_gpio = true; |
| 126 | |
| 127 | list_add_tail(new: &ref->list, head: &entry->refs); |
| 128 | |
| 129 | pr_debug("Created a secondary shared GPIO reference for potential reset-gpio device for GPIO %u at %s\n" , |
| 130 | entry->offset, fwnode_get_name(entry->fwnode)); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* Handle all special nodes that we should ignore. */ |
| 136 | static bool gpio_shared_of_node_ignore(struct device_node *node) |
| 137 | { |
| 138 | /* Ignore disabled devices. */ |
| 139 | if (!of_device_is_available(device: node)) |
| 140 | return true; |
| 141 | |
| 142 | /* |
| 143 | * __symbols__ is a special, internal node and should not be considered |
| 144 | * when scanning for shared GPIOs. |
| 145 | */ |
| 146 | if (of_node_name_eq(np: node, name: "__symbols__" )) |
| 147 | return true; |
| 148 | |
| 149 | /* |
| 150 | * GPIO hogs have a "gpios" property which is not a phandle and can't |
| 151 | * possibly refer to a shared GPIO. |
| 152 | */ |
| 153 | if (of_property_present(np: node, propname: "gpio-hog" )) |
| 154 | return true; |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | static int gpio_shared_of_traverse(struct device_node *curr) |
| 160 | { |
| 161 | struct gpio_shared_entry *entry; |
| 162 | size_t con_id_len, suffix_len; |
| 163 | struct fwnode_handle *fwnode; |
| 164 | struct of_phandle_args args; |
| 165 | struct gpio_shared_ref *ref; |
| 166 | struct property *prop; |
| 167 | unsigned int offset; |
| 168 | const char *suffix; |
| 169 | int ret, count, i; |
| 170 | |
| 171 | if (gpio_shared_of_node_ignore(node: curr)) |
| 172 | return 0; |
| 173 | |
| 174 | for_each_property_of_node(curr, prop) { |
| 175 | /* |
| 176 | * The standard name for a GPIO property is "foo-gpios" |
| 177 | * or "foo-gpio". Some bindings also use "gpios" or "gpio". |
| 178 | * There are some legacy device-trees which have a different |
| 179 | * naming convention and for which we have rename quirks in |
| 180 | * place in gpiolib-of.c. I don't think any of them require |
| 181 | * support for shared GPIOs so for now let's just ignore |
| 182 | * them. We can always just export the quirk list and |
| 183 | * iterate over it here. |
| 184 | */ |
| 185 | if (!strends(str: prop->name, suffix: "-gpios" ) && |
| 186 | !strends(str: prop->name, suffix: "-gpio" ) && |
| 187 | strcmp(prop->name, "gpios" ) != 0 && |
| 188 | strcmp(prop->name, "gpio" ) != 0) |
| 189 | continue; |
| 190 | |
| 191 | count = of_count_phandle_with_args(np: curr, list_name: prop->name, |
| 192 | cells_name: "#gpio-cells" ); |
| 193 | if (count <= 0) |
| 194 | continue; |
| 195 | |
| 196 | for (i = 0; i < count; i++) { |
| 197 | struct device_node *np __free(device_node) = NULL; |
| 198 | char *con_id __free(kfree) = NULL; |
| 199 | |
| 200 | ret = of_parse_phandle_with_args(np: curr, list_name: prop->name, |
| 201 | cells_name: "#gpio-cells" , index: i, |
| 202 | out_args: &args); |
| 203 | if (ret) |
| 204 | continue; |
| 205 | |
| 206 | np = args.np; |
| 207 | |
| 208 | if (!of_property_present(np, propname: "gpio-controller" )) |
| 209 | continue; |
| 210 | |
| 211 | /* |
| 212 | * We support 1, 2 and 3 cell GPIO bindings in the |
| 213 | * kernel currently. There's only one old MIPS dts that |
| 214 | * has a one-cell binding but there's no associated |
| 215 | * consumer so it may as well be an error. There don't |
| 216 | * seem to be any 3-cell users of non-exclusive GPIOs, |
| 217 | * so we can skip this as well. Let's occupy ourselves |
| 218 | * with the predominant 2-cell binding with the first |
| 219 | * cell indicating the hardware offset of the GPIO and |
| 220 | * the second defining the GPIO flags of the request. |
| 221 | */ |
| 222 | if (args.args_count != 2) |
| 223 | continue; |
| 224 | |
| 225 | fwnode = of_fwnode_handle(args.np); |
| 226 | offset = args.args[0]; |
| 227 | |
| 228 | entry = gpio_shared_find_entry(controller_node: fwnode, offset); |
| 229 | if (!entry) { |
| 230 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 231 | if (!entry) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | entry->fwnode = fwnode_handle_get(fwnode); |
| 235 | entry->offset = offset; |
| 236 | entry->index = count; |
| 237 | INIT_LIST_HEAD(list: &entry->refs); |
| 238 | mutex_init(&entry->lock); |
| 239 | |
| 240 | list_add_tail(new: &entry->list, head: &gpio_shared_list); |
| 241 | } |
| 242 | |
| 243 | if (strends(str: prop->name, suffix: "gpios" )) |
| 244 | suffix = "-gpios" ; |
| 245 | else if (strends(str: prop->name, suffix: "gpio" )) |
| 246 | suffix = "-gpio" ; |
| 247 | else |
| 248 | suffix = NULL; |
| 249 | if (!suffix) |
| 250 | continue; |
| 251 | |
| 252 | /* We only set con_id if there's actually one. */ |
| 253 | if (strcmp(prop->name, "gpios" ) && strcmp(prop->name, "gpio" )) { |
| 254 | con_id = kstrdup(s: prop->name, GFP_KERNEL); |
| 255 | if (!con_id) |
| 256 | return -ENOMEM; |
| 257 | |
| 258 | con_id_len = strlen(con_id); |
| 259 | suffix_len = strlen(suffix); |
| 260 | |
| 261 | con_id[con_id_len - suffix_len] = '\0'; |
| 262 | } |
| 263 | |
| 264 | ref = gpio_shared_make_ref(fwnode: fwnode_handle_get(of_fwnode_handle(curr)), |
| 265 | con_id, flags: args.args[1]); |
| 266 | if (!ref) |
| 267 | return -ENOMEM; |
| 268 | |
| 269 | if (!list_empty(head: &entry->refs)) |
| 270 | pr_debug("GPIO %u at %s is shared by multiple firmware nodes\n" , |
| 271 | entry->offset, fwnode_get_name(entry->fwnode)); |
| 272 | |
| 273 | list_add_tail(new: &ref->list, head: &entry->refs); |
| 274 | |
| 275 | if (strcmp(prop->name, "reset-gpios" ) == 0) { |
| 276 | ret = gpio_shared_setup_reset_proxy(entry, flags: args.args[1]); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | for_each_child_of_node_scoped(curr, child) { |
| 284 | ret = gpio_shared_of_traverse(curr: child); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int gpio_shared_of_scan(void) |
| 293 | { |
| 294 | if (of_root) |
| 295 | return gpio_shared_of_traverse(curr: of_root); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | #else |
| 300 | static int gpio_shared_of_scan(void) |
| 301 | { |
| 302 | return 0; |
| 303 | } |
| 304 | #endif /* CONFIG_OF */ |
| 305 | |
| 306 | static void gpio_shared_adev_release(struct device *dev) |
| 307 | { |
| 308 | |
| 309 | } |
| 310 | |
| 311 | static int gpio_shared_make_adev(struct gpio_device *gdev, |
| 312 | struct gpio_shared_entry *entry, |
| 313 | struct gpio_shared_ref *ref) |
| 314 | { |
| 315 | struct auxiliary_device *adev = &ref->adev; |
| 316 | int ret; |
| 317 | |
| 318 | guard(mutex)(T: &ref->lock); |
| 319 | |
| 320 | memset(adev, 0, sizeof(*adev)); |
| 321 | |
| 322 | adev->id = ref->dev_id; |
| 323 | adev->name = "proxy" ; |
| 324 | adev->dev.parent = gdev->dev.parent; |
| 325 | adev->dev.platform_data = entry; |
| 326 | adev->dev.release = gpio_shared_adev_release; |
| 327 | |
| 328 | ret = auxiliary_device_init(auxdev: adev); |
| 329 | if (ret) |
| 330 | return ret; |
| 331 | |
| 332 | ret = auxiliary_device_add(adev); |
| 333 | if (ret) { |
| 334 | auxiliary_device_uninit(auxdev: adev); |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | pr_debug("Created an auxiliary GPIO proxy %s for GPIO device %s\n" , |
| 339 | dev_name(&adev->dev), gpio_device_get_label(gdev)); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | #if IS_ENABLED(CONFIG_RESET_GPIO) |
| 345 | /* |
| 346 | * Special case: reset-gpio is an auxiliary device that's created dynamically |
| 347 | * and put in between the GPIO controller and consumers of shared GPIOs |
| 348 | * referred to by the "reset-gpios" property. |
| 349 | * |
| 350 | * If the supposed consumer of a shared GPIO didn't match any of the mappings |
| 351 | * we created when scanning the firmware nodes, it's still possible that it's |
| 352 | * the reset-gpio device which didn't exist at the time of the scan. |
| 353 | * |
| 354 | * This function verifies it an return true if it's the case. |
| 355 | */ |
| 356 | static bool gpio_shared_dev_is_reset_gpio(struct device *consumer, |
| 357 | struct gpio_shared_entry *entry, |
| 358 | struct gpio_shared_ref *ref) |
| 359 | { |
| 360 | struct fwnode_handle *reset_fwnode = dev_fwnode(consumer); |
| 361 | struct fwnode_reference_args ref_args, aux_args; |
| 362 | struct device *parent = consumer->parent; |
| 363 | struct gpio_shared_ref *real_ref; |
| 364 | bool match; |
| 365 | int ret; |
| 366 | |
| 367 | lockdep_assert_held(&ref->lock); |
| 368 | |
| 369 | /* The reset-gpio device must have a parent AND a firmware node. */ |
| 370 | if (!parent || !reset_fwnode) |
| 371 | return false; |
| 372 | |
| 373 | /* |
| 374 | * Parent of the reset-gpio auxiliary device is the GPIO chip whose |
| 375 | * fwnode we stored in the entry structure. |
| 376 | */ |
| 377 | if (!device_match_fwnode(dev: parent, fwnode: entry->fwnode)) |
| 378 | return false; |
| 379 | |
| 380 | /* |
| 381 | * Now we need to find the actual pin we want to assign to this |
| 382 | * reset-gpio device. To that end: iterate over the list of references |
| 383 | * of this entry and see if there's one, whose reset-gpios property's |
| 384 | * arguments match the ones from this consumer's node. |
| 385 | */ |
| 386 | list_for_each_entry(real_ref, &entry->refs, list) { |
| 387 | if (real_ref == ref) |
| 388 | continue; |
| 389 | |
| 390 | guard(mutex)(T: &real_ref->lock); |
| 391 | |
| 392 | if (!real_ref->fwnode) |
| 393 | continue; |
| 394 | |
| 395 | /* |
| 396 | * The device associated with the shared reference's firmware |
| 397 | * node is the consumer of the reset control exposed by the |
| 398 | * reset-gpio device. It must have a "reset-gpios" property |
| 399 | * that's referencing the entry's firmware node. |
| 400 | * |
| 401 | * The reference args must agree between the real consumer and |
| 402 | * the auxiliary reset-gpio device. |
| 403 | */ |
| 404 | ret = fwnode_property_get_reference_args(fwnode: real_ref->fwnode, |
| 405 | prop: "reset-gpios" , |
| 406 | NULL, nargs: 2, index: 0, args: &ref_args); |
| 407 | if (ret) |
| 408 | continue; |
| 409 | |
| 410 | ret = fwnode_property_get_reference_args(fwnode: reset_fwnode, prop: "reset-gpios" , |
| 411 | NULL, nargs: 2, index: 0, args: &aux_args); |
| 412 | if (ret) { |
| 413 | fwnode_handle_put(fwnode: ref_args.fwnode); |
| 414 | continue; |
| 415 | } |
| 416 | |
| 417 | match = ((ref_args.fwnode == entry->fwnode) && |
| 418 | (aux_args.fwnode == entry->fwnode) && |
| 419 | (ref_args.args[0] == aux_args.args[0])); |
| 420 | |
| 421 | fwnode_handle_put(fwnode: ref_args.fwnode); |
| 422 | fwnode_handle_put(fwnode: aux_args.fwnode); |
| 423 | |
| 424 | if (!match) |
| 425 | continue; |
| 426 | |
| 427 | /* |
| 428 | * Reuse the fwnode of the real device, next time we'll use it |
| 429 | * in the normal path. |
| 430 | */ |
| 431 | ref->fwnode = fwnode_handle_get(fwnode: reset_fwnode); |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | return false; |
| 436 | } |
| 437 | #else |
| 438 | static bool gpio_shared_dev_is_reset_gpio(struct device *consumer, |
| 439 | struct gpio_shared_entry *entry, |
| 440 | struct gpio_shared_ref *ref) |
| 441 | { |
| 442 | return false; |
| 443 | } |
| 444 | #endif /* CONFIG_RESET_GPIO */ |
| 445 | |
| 446 | int gpio_shared_add_proxy_lookup(struct device *consumer, const char *con_id, |
| 447 | unsigned long lflags) |
| 448 | { |
| 449 | const char *dev_id = dev_name(dev: consumer); |
| 450 | struct gpiod_lookup_table *lookup; |
| 451 | struct gpio_shared_entry *entry; |
| 452 | struct gpio_shared_ref *ref; |
| 453 | |
| 454 | list_for_each_entry(entry, &gpio_shared_list, list) { |
| 455 | list_for_each_entry(ref, &entry->refs, list) { |
| 456 | guard(mutex)(T: &ref->lock); |
| 457 | |
| 458 | /* |
| 459 | * FIXME: use device_is_compatible() once the reset-gpio |
| 460 | * drivers gains a compatible string which it currently |
| 461 | * does not have. |
| 462 | */ |
| 463 | if (!ref->fwnode && strstarts(str: dev_name(dev: consumer), prefix: "reset.gpio." )) { |
| 464 | if (!gpio_shared_dev_is_reset_gpio(consumer, entry, ref)) |
| 465 | continue; |
| 466 | } else if (!device_match_fwnode(dev: consumer, fwnode: ref->fwnode)) { |
| 467 | continue; |
| 468 | } |
| 469 | |
| 470 | if ((!con_id && ref->con_id) || (con_id && !ref->con_id) || |
| 471 | (con_id && ref->con_id && strcmp(con_id, ref->con_id) != 0)) |
| 472 | continue; |
| 473 | |
| 474 | /* We've already done that on a previous request. */ |
| 475 | if (ref->lookup) |
| 476 | return 0; |
| 477 | |
| 478 | char *key __free(kfree) = |
| 479 | kasprintf(GFP_KERNEL, |
| 480 | KBUILD_MODNAME ".proxy.%u" , |
| 481 | ref->adev.id); |
| 482 | if (!key) |
| 483 | return -ENOMEM; |
| 484 | |
| 485 | lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); |
| 486 | if (!lookup) |
| 487 | return -ENOMEM; |
| 488 | |
| 489 | pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n" , |
| 490 | dev_id, key, ref->con_id ?: "none" ); |
| 491 | |
| 492 | lookup->dev_id = dev_id; |
| 493 | lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0, |
| 494 | ref->con_id, lflags); |
| 495 | |
| 496 | ref->lookup = lookup; |
| 497 | gpiod_add_lookup_table(table: ref->lookup); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* We warn here because this can only happen if the programmer borked. */ |
| 504 | WARN_ON(1); |
| 505 | return -ENOENT; |
| 506 | } |
| 507 | |
| 508 | static void gpio_shared_remove_adev(struct auxiliary_device *adev) |
| 509 | { |
| 510 | auxiliary_device_delete(auxdev: adev); |
| 511 | auxiliary_device_uninit(auxdev: adev); |
| 512 | } |
| 513 | |
| 514 | int gpio_device_setup_shared(struct gpio_device *gdev) |
| 515 | { |
| 516 | struct gpio_shared_entry *entry; |
| 517 | struct gpio_shared_ref *ref; |
| 518 | struct gpio_desc *desc; |
| 519 | int ret; |
| 520 | |
| 521 | list_for_each_entry(entry, &gpio_shared_list, list) { |
| 522 | list_for_each_entry(ref, &entry->refs, list) { |
| 523 | if (gdev->dev.parent == &ref->adev.dev) { |
| 524 | /* |
| 525 | * This is a shared GPIO proxy. Mark its |
| 526 | * descriptor as such and return here. |
| 527 | */ |
| 528 | __set_bit(GPIOD_FLAG_SHARED_PROXY, |
| 529 | &gdev->descs[0].flags); |
| 530 | return 0; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * This is not a shared GPIO proxy but it still may be the device |
| 537 | * exposing shared pins. Find them and create the proxy devices. |
| 538 | */ |
| 539 | list_for_each_entry(entry, &gpio_shared_list, list) { |
| 540 | if (!device_match_fwnode(dev: &gdev->dev, fwnode: entry->fwnode)) |
| 541 | continue; |
| 542 | |
| 543 | if (list_count_nodes(head: &entry->refs) <= 1) |
| 544 | continue; |
| 545 | |
| 546 | desc = &gdev->descs[entry->offset]; |
| 547 | |
| 548 | __set_bit(GPIOD_FLAG_SHARED, &desc->flags); |
| 549 | /* |
| 550 | * Shared GPIOs are not requested via the normal path. Make |
| 551 | * them inaccessible to anyone even before we register the |
| 552 | * chip. |
| 553 | */ |
| 554 | ret = gpiod_request_commit(desc, label: "shared" ); |
| 555 | if (ret) |
| 556 | return ret; |
| 557 | |
| 558 | pr_debug("GPIO %u owned by %s is shared by multiple consumers\n" , |
| 559 | entry->offset, gpio_device_get_label(gdev)); |
| 560 | |
| 561 | list_for_each_entry(ref, &entry->refs, list) { |
| 562 | pr_debug("Setting up a shared GPIO entry for %s (con_id: '%s')\n" , |
| 563 | fwnode_get_name(ref->fwnode) ?: "(no fwnode)" , |
| 564 | ref->con_id ?: "(none)" ); |
| 565 | |
| 566 | ret = gpio_shared_make_adev(gdev, entry, ref); |
| 567 | if (ret) { |
| 568 | gpiod_free_commit(desc); |
| 569 | return ret; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | void gpio_device_teardown_shared(struct gpio_device *gdev) |
| 578 | { |
| 579 | struct gpio_shared_entry *entry; |
| 580 | struct gpio_shared_ref *ref; |
| 581 | |
| 582 | list_for_each_entry(entry, &gpio_shared_list, list) { |
| 583 | if (!device_match_fwnode(dev: &gdev->dev, fwnode: entry->fwnode)) |
| 584 | continue; |
| 585 | |
| 586 | gpiod_free_commit(desc: &gdev->descs[entry->offset]); |
| 587 | |
| 588 | list_for_each_entry(ref, &entry->refs, list) { |
| 589 | guard(mutex)(T: &ref->lock); |
| 590 | |
| 591 | if (ref->lookup) { |
| 592 | gpiod_remove_lookup_table(table: ref->lookup); |
| 593 | kfree(objp: ref->lookup->table[0].key); |
| 594 | kfree(objp: ref->lookup); |
| 595 | ref->lookup = NULL; |
| 596 | } |
| 597 | |
| 598 | gpio_shared_remove_adev(adev: &ref->adev); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | static void gpio_shared_release(struct kref *kref) |
| 604 | { |
| 605 | struct gpio_shared_entry *entry = |
| 606 | container_of(kref, struct gpio_shared_entry, ref); |
| 607 | struct gpio_shared_desc *shared_desc; |
| 608 | |
| 609 | guard(mutex)(T: &entry->lock); |
| 610 | |
| 611 | shared_desc = entry->shared_desc; |
| 612 | gpio_device_put(gdev: shared_desc->desc->gdev); |
| 613 | if (shared_desc->can_sleep) |
| 614 | mutex_destroy(lock: &shared_desc->mutex); |
| 615 | kfree(objp: shared_desc); |
| 616 | entry->shared_desc = NULL; |
| 617 | } |
| 618 | |
| 619 | static void gpiod_shared_put(void *data) |
| 620 | { |
| 621 | struct gpio_shared_entry *entry = data; |
| 622 | |
| 623 | kref_put(kref: &entry->ref, release: gpio_shared_release); |
| 624 | } |
| 625 | |
| 626 | static struct gpio_shared_desc * |
| 627 | gpiod_shared_desc_create(struct gpio_shared_entry *entry) |
| 628 | { |
| 629 | struct gpio_shared_desc *shared_desc; |
| 630 | struct gpio_device *gdev; |
| 631 | |
| 632 | lockdep_assert_held(&entry->lock); |
| 633 | |
| 634 | shared_desc = kzalloc(sizeof(*shared_desc), GFP_KERNEL); |
| 635 | if (!shared_desc) |
| 636 | return ERR_PTR(error: -ENOMEM); |
| 637 | |
| 638 | gdev = gpio_device_find_by_fwnode(fwnode: entry->fwnode); |
| 639 | if (!gdev) { |
| 640 | kfree(objp: shared_desc); |
| 641 | return ERR_PTR(error: -EPROBE_DEFER); |
| 642 | } |
| 643 | |
| 644 | shared_desc->desc = &gdev->descs[entry->offset]; |
| 645 | shared_desc->can_sleep = gpiod_cansleep(desc: shared_desc->desc); |
| 646 | if (shared_desc->can_sleep) |
| 647 | mutex_init(&shared_desc->mutex); |
| 648 | else |
| 649 | spin_lock_init(&shared_desc->spinlock); |
| 650 | |
| 651 | return shared_desc; |
| 652 | } |
| 653 | |
| 654 | struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev) |
| 655 | { |
| 656 | struct gpio_shared_desc *shared_desc; |
| 657 | struct gpio_shared_entry *entry; |
| 658 | int ret; |
| 659 | |
| 660 | entry = dev_get_platdata(dev); |
| 661 | if (WARN_ON(!entry)) |
| 662 | /* Programmer bug */ |
| 663 | return ERR_PTR(error: -ENOENT); |
| 664 | |
| 665 | scoped_guard(mutex, &entry->lock) { |
| 666 | if (entry->shared_desc) { |
| 667 | kref_get(kref: &entry->ref); |
| 668 | shared_desc = entry->shared_desc; |
| 669 | } else { |
| 670 | shared_desc = gpiod_shared_desc_create(entry); |
| 671 | if (IS_ERR(ptr: shared_desc)) |
| 672 | return ERR_CAST(ptr: shared_desc); |
| 673 | |
| 674 | kref_init(kref: &entry->ref); |
| 675 | entry->shared_desc = shared_desc; |
| 676 | } |
| 677 | |
| 678 | pr_debug("Device %s acquired a reference to the shared GPIO %u owned by %s\n" , |
| 679 | dev_name(dev), gpiod_hwgpio(shared_desc->desc), |
| 680 | gpio_device_get_label(shared_desc->desc->gdev)); |
| 681 | } |
| 682 | |
| 683 | ret = devm_add_action_or_reset(dev, gpiod_shared_put, entry); |
| 684 | if (ret) |
| 685 | return ERR_PTR(error: ret); |
| 686 | |
| 687 | return shared_desc; |
| 688 | } |
| 689 | EXPORT_SYMBOL_GPL(devm_gpiod_shared_get); |
| 690 | |
| 691 | static void gpio_shared_drop_ref(struct gpio_shared_ref *ref) |
| 692 | { |
| 693 | list_del(entry: &ref->list); |
| 694 | mutex_destroy(lock: &ref->lock); |
| 695 | lockdep_unregister_key(key: &ref->lock_key); |
| 696 | kfree(objp: ref->con_id); |
| 697 | ida_free(&gpio_shared_ida, id: ref->dev_id); |
| 698 | fwnode_handle_put(fwnode: ref->fwnode); |
| 699 | kfree(objp: ref); |
| 700 | } |
| 701 | |
| 702 | static void gpio_shared_drop_entry(struct gpio_shared_entry *entry) |
| 703 | { |
| 704 | list_del(entry: &entry->list); |
| 705 | mutex_destroy(lock: &entry->lock); |
| 706 | fwnode_handle_put(fwnode: entry->fwnode); |
| 707 | kfree(objp: entry); |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * This is only called if gpio_shared_init() fails so it's in fact __init and |
| 712 | * not __exit. |
| 713 | */ |
| 714 | static void __init gpio_shared_teardown(void) |
| 715 | { |
| 716 | struct gpio_shared_entry *entry, *epos; |
| 717 | struct gpio_shared_ref *ref, *rpos; |
| 718 | |
| 719 | list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) { |
| 720 | list_for_each_entry_safe(ref, rpos, &entry->refs, list) |
| 721 | gpio_shared_drop_ref(ref); |
| 722 | |
| 723 | gpio_shared_drop_entry(entry); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static bool gpio_shared_entry_is_really_shared(struct gpio_shared_entry *entry) |
| 728 | { |
| 729 | size_t num_nodes = list_count_nodes(head: &entry->refs); |
| 730 | struct gpio_shared_ref *ref; |
| 731 | |
| 732 | if (num_nodes <= 1) |
| 733 | return false; |
| 734 | |
| 735 | if (num_nodes > 2) |
| 736 | return true; |
| 737 | |
| 738 | /* Exactly two references: */ |
| 739 | list_for_each_entry(ref, &entry->refs, list) { |
| 740 | /* |
| 741 | * Corner-case: the second reference comes from the potential |
| 742 | * reset-gpio instance. However, this pin is not really shared |
| 743 | * as it would have three references in this case. Avoid |
| 744 | * creating unnecessary proxies. |
| 745 | */ |
| 746 | if (ref->is_reset_gpio) |
| 747 | return false; |
| 748 | } |
| 749 | |
| 750 | return true; |
| 751 | } |
| 752 | |
| 753 | static void gpio_shared_free_exclusive(void) |
| 754 | { |
| 755 | struct gpio_shared_entry *entry, *epos; |
| 756 | |
| 757 | list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) { |
| 758 | if (gpio_shared_entry_is_really_shared(entry)) |
| 759 | continue; |
| 760 | |
| 761 | gpio_shared_drop_ref(list_first_entry(&entry->refs, |
| 762 | struct gpio_shared_ref, |
| 763 | list)); |
| 764 | gpio_shared_drop_entry(entry); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | static int __init gpio_shared_init(void) |
| 769 | { |
| 770 | int ret; |
| 771 | |
| 772 | /* Right now, we only support OF-based systems. */ |
| 773 | ret = gpio_shared_of_scan(); |
| 774 | if (ret) { |
| 775 | gpio_shared_teardown(); |
| 776 | pr_err("Failed to scan OF nodes for shared GPIOs: %d\n" , ret); |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | gpio_shared_free_exclusive(); |
| 781 | |
| 782 | pr_debug("Finished scanning firmware nodes for shared GPIOs\n" ); |
| 783 | return 0; |
| 784 | } |
| 785 | postcore_initcall(gpio_shared_init); |
| 786 | |