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 struct auxiliary_device adev; 40 struct gpiod_lookup_table *lookup; 41 }; 42 43 /* Represents a single GPIO pin. */ 44 struct gpio_shared_entry { 45 struct list_head list; 46 /* Firmware node associated with the GPIO controller. */ 47 struct fwnode_handle *fwnode; 48 /* Hardware offset of the GPIO within its chip. */ 49 unsigned int offset; 50 /* Index in the property value array. */ 51 size_t index; 52 struct gpio_shared_desc *shared_desc; 53 struct kref ref; 54 struct list_head refs; 55 }; 56 57 static LIST_HEAD(gpio_shared_list); 58 static DEFINE_MUTEX(gpio_shared_lock); 59 static DEFINE_IDA(gpio_shared_ida); 60 61 static struct gpio_shared_entry * 62 gpio_shared_find_entry(struct fwnode_handle *controller_node, 63 unsigned int offset) 64 { 65 struct gpio_shared_entry *entry; 66 67 list_for_each_entry(entry, &gpio_shared_list, list) { 68 if (entry->fwnode == controller_node && entry->offset == offset) 69 return entry; 70 } 71 72 return NULL; 73 } 74 75 #if IS_ENABLED(CONFIG_OF) 76 static int gpio_shared_of_traverse(struct device_node *curr) 77 { 78 struct gpio_shared_entry *entry; 79 size_t con_id_len, suffix_len; 80 struct fwnode_handle *fwnode; 81 struct of_phandle_args args; 82 struct property *prop; 83 unsigned int offset; 84 const char *suffix; 85 int ret, count, i; 86 87 for_each_property_of_node(curr, prop) { 88 /* 89 * The standard name for a GPIO property is "foo-gpios" 90 * or "foo-gpio". Some bindings also use "gpios" or "gpio". 91 * There are some legacy device-trees which have a different 92 * naming convention and for which we have rename quirks in 93 * place in gpiolib-of.c. I don't think any of them require 94 * support for shared GPIOs so for now let's just ignore 95 * them. We can always just export the quirk list and 96 * iterate over it here. 97 */ 98 if (!strends(prop->name, "-gpios") && 99 !strends(prop->name, "-gpio") && 100 strcmp(prop->name, "gpios") != 0 && 101 strcmp(prop->name, "gpio") != 0) 102 continue; 103 104 count = of_count_phandle_with_args(curr, prop->name, 105 "#gpio-cells"); 106 if (count <= 0) 107 continue; 108 109 for (i = 0; i < count; i++) { 110 struct device_node *np __free(device_node) = NULL; 111 112 ret = of_parse_phandle_with_args(curr, prop->name, 113 "#gpio-cells", i, 114 &args); 115 if (ret) 116 continue; 117 118 np = args.np; 119 120 if (!of_property_present(np, "gpio-controller")) 121 continue; 122 123 /* 124 * We support 1, 2 and 3 cell GPIO bindings in the 125 * kernel currently. There's only one old MIPS dts that 126 * has a one-cell binding but there's no associated 127 * consumer so it may as well be an error. There don't 128 * seem to be any 3-cell users of non-exclusive GPIOs, 129 * so we can skip this as well. Let's occupy ourselves 130 * with the predominant 2-cell binding with the first 131 * cell indicating the hardware offset of the GPIO and 132 * the second defining the GPIO flags of the request. 133 */ 134 if (args.args_count != 2) 135 continue; 136 137 fwnode = of_fwnode_handle(args.np); 138 offset = args.args[0]; 139 140 entry = gpio_shared_find_entry(fwnode, offset); 141 if (!entry) { 142 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 143 if (!entry) 144 return -ENOMEM; 145 146 entry->fwnode = fwnode_handle_get(fwnode); 147 entry->offset = offset; 148 entry->index = count; 149 INIT_LIST_HEAD(&entry->refs); 150 151 list_add_tail(&entry->list, &gpio_shared_list); 152 } 153 154 struct gpio_shared_ref *ref __free(kfree) = 155 kzalloc(sizeof(*ref), GFP_KERNEL); 156 if (!ref) 157 return -ENOMEM; 158 159 ref->fwnode = fwnode_handle_get(of_fwnode_handle(curr)); 160 ref->flags = args.args[1]; 161 162 if (strends(prop->name, "gpios")) 163 suffix = "-gpios"; 164 else if (strends(prop->name, "gpio")) 165 suffix = "-gpio"; 166 else 167 suffix = NULL; 168 if (!suffix) 169 continue; 170 171 /* We only set con_id if there's actually one. */ 172 if (strcmp(prop->name, "gpios") && strcmp(prop->name, "gpio")) { 173 ref->con_id = kstrdup(prop->name, GFP_KERNEL); 174 if (!ref->con_id) 175 return -ENOMEM; 176 177 con_id_len = strlen(ref->con_id); 178 suffix_len = strlen(suffix); 179 180 ref->con_id[con_id_len - suffix_len] = '\0'; 181 } 182 183 ref->dev_id = ida_alloc(&gpio_shared_ida, GFP_KERNEL); 184 if (ref->dev_id < 0) { 185 kfree(ref->con_id); 186 return -ENOMEM; 187 } 188 189 if (!list_empty(&entry->refs)) 190 pr_debug("GPIO %u at %s is shared by multiple firmware nodes\n", 191 entry->offset, fwnode_get_name(entry->fwnode)); 192 193 list_add_tail(&no_free_ptr(ref)->list, &entry->refs); 194 } 195 } 196 197 for_each_child_of_node_scoped(curr, child) { 198 ret = gpio_shared_of_traverse(child); 199 if (ret) 200 return ret; 201 } 202 203 return 0; 204 } 205 206 static int gpio_shared_of_scan(void) 207 { 208 return gpio_shared_of_traverse(of_root); 209 } 210 #else 211 static int gpio_shared_of_scan(void) 212 { 213 return 0; 214 } 215 #endif /* CONFIG_OF */ 216 217 static void gpio_shared_adev_release(struct device *dev) 218 { 219 220 } 221 222 static int gpio_shared_make_adev(struct gpio_device *gdev, 223 struct gpio_shared_ref *ref) 224 { 225 struct auxiliary_device *adev = &ref->adev; 226 int ret; 227 228 lockdep_assert_held(&gpio_shared_lock); 229 230 memset(adev, 0, sizeof(*adev)); 231 232 adev->id = ref->dev_id; 233 adev->name = "proxy"; 234 adev->dev.parent = gdev->dev.parent; 235 adev->dev.release = gpio_shared_adev_release; 236 237 ret = auxiliary_device_init(adev); 238 if (ret) 239 return ret; 240 241 ret = auxiliary_device_add(adev); 242 if (ret) { 243 auxiliary_device_uninit(adev); 244 return ret; 245 } 246 247 pr_debug("Created an auxiliary GPIO proxy %s for GPIO device %s\n", 248 dev_name(&adev->dev), gpio_device_get_label(gdev)); 249 250 return 0; 251 } 252 253 int gpio_shared_add_proxy_lookup(struct device *consumer, unsigned long lflags) 254 { 255 const char *dev_id = dev_name(consumer); 256 struct gpio_shared_entry *entry; 257 struct gpio_shared_ref *ref; 258 259 struct gpiod_lookup_table *lookup __free(kfree) = 260 kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); 261 if (!lookup) 262 return -ENOMEM; 263 264 guard(mutex)(&gpio_shared_lock); 265 266 list_for_each_entry(entry, &gpio_shared_list, list) { 267 list_for_each_entry(ref, &entry->refs, list) { 268 if (!device_match_fwnode(consumer, ref->fwnode)) 269 continue; 270 271 /* We've already done that on a previous request. */ 272 if (ref->lookup) 273 return 0; 274 275 char *key __free(kfree) = 276 kasprintf(GFP_KERNEL, 277 KBUILD_MODNAME ".proxy.%u", 278 ref->adev.id); 279 if (!key) 280 return -ENOMEM; 281 282 pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n", 283 dev_id, key, ref->con_id ?: "none"); 284 285 lookup->dev_id = dev_id; 286 lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0, 287 ref->con_id, lflags); 288 289 gpiod_add_lookup_table(no_free_ptr(lookup)); 290 291 return 0; 292 } 293 } 294 295 /* We warn here because this can only happen if the programmer borked. */ 296 WARN_ON(1); 297 return -ENOENT; 298 } 299 300 static void gpio_shared_remove_adev(struct auxiliary_device *adev) 301 { 302 lockdep_assert_held(&gpio_shared_lock); 303 304 auxiliary_device_uninit(adev); 305 auxiliary_device_delete(adev); 306 } 307 308 int gpio_device_setup_shared(struct gpio_device *gdev) 309 { 310 struct gpio_shared_entry *entry; 311 struct gpio_shared_ref *ref; 312 unsigned long *flags; 313 int ret; 314 315 guard(mutex)(&gpio_shared_lock); 316 317 list_for_each_entry(entry, &gpio_shared_list, list) { 318 list_for_each_entry(ref, &entry->refs, list) { 319 if (gdev->dev.parent == &ref->adev.dev) { 320 /* 321 * This is a shared GPIO proxy. Mark its 322 * descriptor as such and return here. 323 */ 324 __set_bit(GPIOD_FLAG_SHARED_PROXY, 325 &gdev->descs[0].flags); 326 return 0; 327 } 328 } 329 } 330 331 /* 332 * This is not a shared GPIO proxy but it still may be the device 333 * exposing shared pins. Find them and create the proxy devices. 334 */ 335 list_for_each_entry(entry, &gpio_shared_list, list) { 336 if (!device_match_fwnode(&gdev->dev, entry->fwnode)) 337 continue; 338 339 if (list_count_nodes(&entry->refs) <= 1) 340 continue; 341 342 flags = &gdev->descs[entry->offset].flags; 343 344 __set_bit(GPIOD_FLAG_SHARED, flags); 345 /* 346 * Shared GPIOs are not requested via the normal path. Make 347 * them inaccessible to anyone even before we register the 348 * chip. 349 */ 350 __set_bit(GPIOD_FLAG_REQUESTED, flags); 351 352 pr_debug("GPIO %u owned by %s is shared by multiple consumers\n", 353 entry->offset, gpio_device_get_label(gdev)); 354 355 list_for_each_entry(ref, &entry->refs, list) { 356 pr_debug("Setting up a shared GPIO entry for %s\n", 357 fwnode_get_name(ref->fwnode)); 358 359 ret = gpio_shared_make_adev(gdev, ref); 360 if (ret) 361 return ret; 362 } 363 } 364 365 return 0; 366 } 367 368 void gpio_device_teardown_shared(struct gpio_device *gdev) 369 { 370 struct gpio_shared_entry *entry; 371 struct gpio_shared_ref *ref; 372 373 guard(mutex)(&gpio_shared_lock); 374 375 list_for_each_entry(entry, &gpio_shared_list, list) { 376 if (!device_match_fwnode(&gdev->dev, entry->fwnode)) 377 continue; 378 379 list_for_each_entry(ref, &entry->refs, list) { 380 gpiod_remove_lookup_table(ref->lookup); 381 kfree(ref->lookup->table[0].key); 382 kfree(ref->lookup); 383 ref->lookup = NULL; 384 gpio_shared_remove_adev(&ref->adev); 385 } 386 } 387 } 388 389 static void gpio_shared_release(struct kref *kref) 390 { 391 struct gpio_shared_entry *entry = 392 container_of(kref, struct gpio_shared_entry, ref); 393 struct gpio_shared_desc *shared_desc = entry->shared_desc; 394 395 guard(mutex)(&gpio_shared_lock); 396 397 gpio_device_put(shared_desc->desc->gdev); 398 if (shared_desc->can_sleep) 399 mutex_destroy(&shared_desc->mutex); 400 kfree(shared_desc); 401 entry->shared_desc = NULL; 402 } 403 404 static void gpiod_shared_put(void *data) 405 { 406 struct gpio_shared_entry *entry = data; 407 408 lockdep_assert_not_held(&gpio_shared_lock); 409 410 kref_put(&entry->ref, gpio_shared_release); 411 } 412 413 static struct gpio_shared_desc * 414 gpiod_shared_desc_create(struct gpio_shared_entry *entry) 415 { 416 struct gpio_shared_desc *shared_desc; 417 struct gpio_device *gdev; 418 419 shared_desc = kzalloc(sizeof(*shared_desc), GFP_KERNEL); 420 if (!shared_desc) 421 return ERR_PTR(-ENOMEM); 422 423 gdev = gpio_device_find_by_fwnode(entry->fwnode); 424 if (!gdev) { 425 kfree(shared_desc); 426 return ERR_PTR(-EPROBE_DEFER); 427 } 428 429 shared_desc->desc = &gdev->descs[entry->offset]; 430 shared_desc->can_sleep = gpiod_cansleep(shared_desc->desc); 431 if (shared_desc->can_sleep) 432 mutex_init(&shared_desc->mutex); 433 else 434 spin_lock_init(&shared_desc->spinlock); 435 436 return shared_desc; 437 } 438 439 static struct gpio_shared_entry *gpiod_shared_find(struct auxiliary_device *adev) 440 { 441 struct gpio_shared_desc *shared_desc; 442 struct gpio_shared_entry *entry; 443 struct gpio_shared_ref *ref; 444 445 guard(mutex)(&gpio_shared_lock); 446 447 list_for_each_entry(entry, &gpio_shared_list, list) { 448 list_for_each_entry(ref, &entry->refs, list) { 449 if (adev != &ref->adev) 450 continue; 451 452 if (entry->shared_desc) { 453 kref_get(&entry->ref); 454 return entry; 455 } 456 457 shared_desc = gpiod_shared_desc_create(entry); 458 if (IS_ERR(shared_desc)) 459 return ERR_CAST(shared_desc); 460 461 kref_init(&entry->ref); 462 entry->shared_desc = shared_desc; 463 464 pr_debug("Device %s acquired a reference to the shared GPIO %u owned by %s\n", 465 dev_name(&adev->dev), gpio_chip_hwgpio(shared_desc->desc), 466 gpio_device_get_label(shared_desc->desc->gdev)); 467 468 469 return entry; 470 } 471 } 472 473 return ERR_PTR(-ENOENT); 474 } 475 476 struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev) 477 { 478 struct gpio_shared_entry *entry; 479 int ret; 480 481 entry = gpiod_shared_find(to_auxiliary_dev(dev)); 482 if (IS_ERR(entry)) 483 return ERR_CAST(entry); 484 485 ret = devm_add_action_or_reset(dev, gpiod_shared_put, entry); 486 if (ret) 487 return ERR_PTR(ret); 488 489 return entry->shared_desc; 490 } 491 EXPORT_SYMBOL_GPL(devm_gpiod_shared_get); 492 493 static void gpio_shared_drop_ref(struct gpio_shared_ref *ref) 494 { 495 list_del(&ref->list); 496 kfree(ref->con_id); 497 ida_free(&gpio_shared_ida, ref->dev_id); 498 fwnode_handle_put(ref->fwnode); 499 kfree(ref); 500 } 501 502 static void gpio_shared_drop_entry(struct gpio_shared_entry *entry) 503 { 504 list_del(&entry->list); 505 fwnode_handle_put(entry->fwnode); 506 kfree(entry); 507 } 508 509 /* 510 * This is only called if gpio_shared_init() fails so it's in fact __init and 511 * not __exit. 512 */ 513 static void __init gpio_shared_teardown(void) 514 { 515 struct gpio_shared_entry *entry, *epos; 516 struct gpio_shared_ref *ref, *rpos; 517 518 list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) { 519 list_for_each_entry_safe(ref, rpos, &entry->refs, list) 520 gpio_shared_drop_ref(ref); 521 522 gpio_shared_drop_entry(entry); 523 } 524 } 525 526 static void gpio_shared_free_exclusive(void) 527 { 528 struct gpio_shared_entry *entry, *epos; 529 530 list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) { 531 if (list_count_nodes(&entry->refs) > 1) 532 continue; 533 534 gpio_shared_drop_ref(list_first_entry(&entry->refs, 535 struct gpio_shared_ref, 536 list)); 537 gpio_shared_drop_entry(entry); 538 } 539 } 540 541 static int __init gpio_shared_init(void) 542 { 543 int ret; 544 545 /* Right now, we only support OF-based systems. */ 546 ret = gpio_shared_of_scan(); 547 if (ret) { 548 gpio_shared_teardown(); 549 pr_err("Failed to scan OF nodes for shared GPIOs: %d\n", ret); 550 return ret; 551 } 552 553 gpio_shared_free_exclusive(); 554 555 pr_debug("Finished scanning firmware nodes for shared GPIOs\n"); 556 return 0; 557 } 558 postcore_initcall(gpio_shared_init); 559