개념 설명 전체 · v6.18.37 / drivers/base/platform.c
1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * platform.c - platform 'pseudo' bus for legacy devices 4 * 5 * Copyright (c) 2002-3 Patrick Mochel 6 * Copyright (c) 2002-3 Open Source Development Labs 7 * 8 * Please see Documentation/driver-api/driver-model/platform.rst for more 9 * information. 10 */ 11 12 #include <linux/string.h> 13 #include <linux/platform_device.h> 14 #include <linux/of_device.h> 15 #include <linux/of_irq.h> 16 #include <linux/module.h> 17 #include <linux/init.h> 18 #include <linux/interrupt.h> 19 #include <linux/ioport.h> 20 #include <linux/dma-mapping.h> 21 #include <linux/memblock.h> 22 #include <linux/err.h> 23 #include <linux/slab.h> 24 #include <linux/pm_runtime.h> 25 #include <linux/pm_domain.h> 26 #include <linux/idr.h> 27 #include <linux/acpi.h> 28 #include <linux/clk/clk-conf.h> 29 #include <linux/limits.h> 30 #include <linux/property.h> 31 #include <linux/kmemleak.h> 32 #include <linux/types.h> 33 #include <linux/iommu.h> 34 #include <linux/dma-map-ops.h> 35 36 #include "base.h" 37 #include "power/power.h" 38 39 /* For automatically allocated device IDs */ 40 static DEFINE_IDA(platform_devid_ida); 41 42 struct device platform_bus = { 43 .init_name = "platform", 44 }; 45 EXPORT_SYMBOL_GPL(platform_bus); 46 47 /** 48 * platform_get_resource - get a resource for a device 49 * @dev: platform device 50 * @type: resource type 51 * @num: resource index 52 * 53 * Return: a pointer to the resource or NULL on failure. 54 */ 55 struct resource *platform_get_resource(struct platform_device *dev, 56 unsigned int type, unsigned int num) 57 { 58 u32 i; 59 60 for (i = 0; i < dev->num_resources; i++) { 61 struct resource *r = &dev->resource[i]; 62 63 if (type == resource_type(r) && num-- == 0) 64 return r; 65 } 66 return NULL; 67 } 68 EXPORT_SYMBOL_GPL(platform_get_resource); 69 70 struct resource *platform_get_mem_or_io(struct platform_device *dev, 71 unsigned int num) 72 { 73 u32 i; 74 75 for (i = 0; i < dev->num_resources; i++) { 76 struct resource *r = &dev->resource[i]; 77 78 if ((resource_type(r) & (IORESOURCE_MEM|IORESOURCE_IO)) && num-- == 0) 79 return r; 80 } 81 return NULL; 82 } 83 EXPORT_SYMBOL_GPL(platform_get_mem_or_io); 84 85 #ifdef CONFIG_HAS_IOMEM 86 /** 87 * devm_platform_get_and_ioremap_resource - call devm_ioremap_resource() for a 88 * platform device and get resource 89 * 90 * @pdev: platform device to use both for memory resource lookup as well as 91 * resource management 92 * @index: resource index 93 * @res: optional output parameter to store a pointer to the obtained resource. 94 * 95 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code 96 * on failure. 97 */ 98 void __iomem * 99 devm_platform_get_and_ioremap_resource(struct platform_device *pdev, 100 unsigned int index, struct resource **res) 101 { 102 struct resource *r; 103 104 r = platform_get_resource(pdev, IORESOURCE_MEM, index); 105 if (res) 106 *res = r; 107 return devm_ioremap_resource(&pdev->dev, r); 108 } 109 EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource); 110 111 /** 112 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform 113 * device 114 * 115 * @pdev: platform device to use both for memory resource lookup as well as 116 * resource management 117 * @index: resource index 118 * 119 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code 120 * on failure. 121 */ 122 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev, 123 unsigned int index) 124 { 125 return devm_platform_get_and_ioremap_resource(pdev, index, NULL); 126 } 127 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource); 128 129 /** 130 * devm_platform_ioremap_resource_byname - call devm_ioremap_resource for 131 * a platform device, retrieve the 132 * resource by name 133 * 134 * @pdev: platform device to use both for memory resource lookup as well as 135 * resource management 136 * @name: name of the resource 137 * 138 * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code 139 * on failure. 140 */ 141 void __iomem * 142 devm_platform_ioremap_resource_byname(struct platform_device *pdev, 143 const char *name) 144 { 145 struct resource *res; 146 147 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); 148 return devm_ioremap_resource(&pdev->dev, res); 149 } 150 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource_byname); 151 #endif /* CONFIG_HAS_IOMEM */ 152 153 /** 154 * platform_get_irq_optional - get an optional IRQ for a device 155 * @dev: platform device 156 * @num: IRQ number index 157 * 158 * Gets an IRQ for a platform device. Device drivers should check the return 159 * value for errors so as to not pass a negative integer value to the 160 * request_irq() APIs. This is the same as platform_get_irq(), except that it 161 * does not print an error message if an IRQ can not be obtained. 162 * 163 * For example:: 164 * 165 * int irq = platform_get_irq_optional(pdev, 0); 166 * if (irq < 0) 167 * return irq; 168 * 169 * Return: non-zero IRQ number on success, negative error number on failure. 170 */ 171 int platform_get_irq_optional(struct platform_device *dev, unsigned int num) 172 { 173 int ret; 174 #ifdef CONFIG_SPARC 175 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */ 176 if (!dev || num >= dev->archdata.num_irqs) 177 goto out_not_found; 178 ret = dev->archdata.irqs[num]; 179 goto out; 180 #else 181 struct fwnode_handle *fwnode = dev_fwnode(&dev->dev); 182 struct resource *r; 183 184 if (is_of_node(fwnode)) { 185 ret = of_irq_get(to_of_node(fwnode), num); 186 if (ret > 0 || ret == -EPROBE_DEFER) 187 goto out; 188 } 189 190 r = platform_get_resource(dev, IORESOURCE_IRQ, num); 191 if (is_acpi_device_node(fwnode)) { 192 if (r && r->flags & IORESOURCE_DISABLED) { 193 ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), num, r); 194 if (ret) 195 goto out; 196 } 197 } 198 199 /* 200 * The resources may pass trigger flags to the irqs that need 201 * to be set up. It so happens that the trigger flags for 202 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER* 203 * settings. 204 */ 205 if (r && r->flags & IORESOURCE_BITS) { 206 struct irq_data *irqd; 207 208 irqd = irq_get_irq_data(r->start); 209 if (!irqd) 210 goto out_not_found; 211 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); 212 } 213 214 if (r) { 215 ret = r->start; 216 goto out; 217 } 218 219 /* 220 * For the index 0 interrupt, allow falling back to GpioInt 221 * resources. While a device could have both Interrupt and GpioInt 222 * resources, making this fallback ambiguous, in many common cases 223 * the device will only expose one IRQ, and this fallback 224 * allows a common code path across either kind of resource. 225 */ 226 if (num == 0 && is_acpi_device_node(fwnode)) { 227 ret = acpi_dev_gpio_irq_get(to_acpi_device_node(fwnode), num); 228 /* Our callers expect -ENXIO for missing IRQs. */ 229 if (ret >= 0 || ret == -EPROBE_DEFER) 230 goto out; 231 } 232 233 #endif 234 out_not_found: 235 ret = -ENXIO; 236 out: 237 if (WARN(!ret, "0 is an invalid IRQ number\n")) 238 return -EINVAL; 239 return ret; 240 } 241 EXPORT_SYMBOL_GPL(platform_get_irq_optional); 242 243 /** 244 * platform_get_irq - get an IRQ for a device 245 * @dev: platform device 246 * @num: IRQ number index 247 * 248 * Gets an IRQ for a platform device and prints an error message if finding the 249 * IRQ fails. Device drivers should check the return value for errors so as to 250 * not pass a negative integer value to the request_irq() APIs. 251 * 252 * For example:: 253 * 254 * int irq = platform_get_irq(pdev, 0); 255 * if (irq < 0) 256 * return irq; 257 * 258 * Return: non-zero IRQ number on success, negative error number on failure. 259 */ 260 int platform_get_irq(struct platform_device *dev, unsigned int num) 261 { 262 int ret; 263 264 ret = platform_get_irq_optional(dev, num); 265 if (ret < 0) 266 return dev_err_probe(&dev->dev, ret, 267 "IRQ index %u not found\n", num); 268 269 return ret; 270 } 271 EXPORT_SYMBOL_GPL(platform_get_irq); 272 273 /** 274 * platform_irq_count - Count the number of IRQs a platform device uses 275 * @dev: platform device 276 * 277 * Return: Number of IRQs a platform device uses or EPROBE_DEFER 278 */ 279 int platform_irq_count(struct platform_device *dev) 280 { 281 int ret, nr = 0; 282 283 while ((ret = platform_get_irq_optional(dev, nr)) >= 0) 284 nr++; 285 286 if (ret == -EPROBE_DEFER) 287 return ret; 288 289 return nr; 290 } 291 EXPORT_SYMBOL_GPL(platform_irq_count); 292 293 struct irq_affinity_devres { 294 unsigned int count; 295 unsigned int irq[] __counted_by(count); 296 }; 297 298 static void platform_disable_acpi_irq(struct platform_device *pdev, int index) 299 { 300 struct resource *r; 301 302 r = platform_get_resource(pdev, IORESOURCE_IRQ, index); 303 if (r) 304 irqresource_disabled(r, 0); 305 } 306 307 static void devm_platform_get_irqs_affinity_release(struct device *dev, 308 void *res) 309 { 310 struct irq_affinity_devres *ptr = res; 311 int i; 312 313 for (i = 0; i < ptr->count; i++) { 314 irq_dispose_mapping(ptr->irq[i]); 315 316 if (is_acpi_device_node(dev_fwnode(dev))) 317 platform_disable_acpi_irq(to_platform_device(dev), i); 318 } 319 } 320 321 /** 322 * devm_platform_get_irqs_affinity - devm method to get a set of IRQs for a 323 * device using an interrupt affinity descriptor 324 * @dev: platform device pointer 325 * @affd: affinity descriptor 326 * @minvec: minimum count of interrupt vectors 327 * @maxvec: maximum count of interrupt vectors 328 * @irqs: pointer holder for IRQ numbers 329 * 330 * Gets a set of IRQs for a platform device, and updates IRQ afffinty according 331 * to the passed affinity descriptor 332 * 333 * Return: Number of vectors on success, negative error number on failure. 334 */ 335 int devm_platform_get_irqs_affinity(struct platform_device *dev, 336 struct irq_affinity *affd, 337 unsigned int minvec, 338 unsigned int maxvec, 339 int **irqs) 340 { 341 struct irq_affinity_devres *ptr; 342 struct irq_affinity_desc *desc; 343 size_t size; 344 int i, ret, nvec; 345 346 if (!affd) 347 return -EPERM; 348 349 if (maxvec < minvec) 350 return -ERANGE; 351 352 nvec = platform_irq_count(dev); 353 if (nvec < 0) 354 return nvec; 355 356 if (nvec < minvec) 357 return -ENOSPC; 358 359 nvec = irq_calc_affinity_vectors(minvec, nvec, affd); 360 if (nvec < minvec) 361 return -ENOSPC; 362 363 if (nvec > maxvec) 364 nvec = maxvec; 365 366 size = sizeof(*ptr) + sizeof(unsigned int) * nvec; 367 ptr = devres_alloc(devm_platform_get_irqs_affinity_release, size, 368 GFP_KERNEL); 369 if (!ptr) 370 return -ENOMEM; 371 372 ptr->count = nvec; 373 374 for (i = 0; i < nvec; i++) { 375 int irq = platform_get_irq(dev, i); 376 if (irq < 0) { 377 ret = irq; 378 goto err_free_devres; 379 } 380 ptr->irq[i] = irq; 381 } 382 383 desc = irq_create_affinity_masks(nvec, affd); 384 if (!desc) { 385 ret = -ENOMEM; 386 goto err_free_devres; 387 } 388 389 for (i = 0; i < nvec; i++) { 390 ret = irq_update_affinity_desc(ptr->irq[i], &desc[i]); 391 if (ret) { 392 dev_err(&dev->dev, "failed to update irq%d affinity descriptor (%d)\n", 393 ptr->irq[i], ret); 394 goto err_free_desc; 395 } 396 } 397 398 devres_add(&dev->dev, ptr); 399 400 kfree(desc); 401 402 *irqs = ptr->irq; 403 404 return nvec; 405 406 err_free_desc: 407 kfree(desc); 408 err_free_devres: 409 devres_free(ptr); 410 return ret; 411 } 412 EXPORT_SYMBOL_GPL(devm_platform_get_irqs_affinity); 413 414 /** 415 * platform_get_resource_byname - get a resource for a device by name 416 * @dev: platform device 417 * @type: resource type 418 * @name: resource name 419 */ 420 struct resource *platform_get_resource_byname(struct platform_device *dev, 421 unsigned int type, 422 const char *name) 423 { 424 u32 i; 425 426 for (i = 0; i < dev->num_resources; i++) { 427 struct resource *r = &dev->resource[i]; 428 429 if (unlikely(!r->name)) 430 continue; 431 432 if (type == resource_type(r) && !strcmp(r->name, name)) 433 return r; 434 } 435 return NULL; 436 } 437 EXPORT_SYMBOL_GPL(platform_get_resource_byname); 438 439 static int __platform_get_irq_byname(struct platform_device *dev, 440 const char *name) 441 { 442 struct resource *r; 443 int ret; 444 445 ret = fwnode_irq_get_byname(dev_fwnode(&dev->dev), name); 446 if (ret > 0 || ret == -EPROBE_DEFER) 447 return ret; 448 449 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name); 450 if (r) { 451 if (WARN(!r->start, "0 is an invalid IRQ number\n")) 452 return -EINVAL; 453 return r->start; 454 } 455 456 return -ENXIO; 457 } 458 459 /** 460 * platform_get_irq_byname - get an IRQ for a device by name 461 * @dev: platform device 462 * @name: IRQ name 463 * 464 * Get an IRQ like platform_get_irq(), but then by name rather then by index. 465 * 466 * Return: non-zero IRQ number on success, negative error number on failure. 467 */ 468 int platform_get_irq_byname(struct platform_device *dev, const char *name) 469 { 470 int ret; 471 472 ret = __platform_get_irq_byname(dev, name); 473 if (ret < 0) 474 return dev_err_probe(&dev->dev, ret, "IRQ %s not found\n", 475 name); 476 return ret; 477 } 478 EXPORT_SYMBOL_GPL(platform_get_irq_byname); 479 480 /** 481 * platform_get_irq_byname_optional - get an optional IRQ for a device by name 482 * @dev: platform device 483 * @name: IRQ name 484 * 485 * Get an optional IRQ by name like platform_get_irq_byname(). Except that it 486 * does not print an error message if an IRQ can not be obtained. 487 * 488 * Return: non-zero IRQ number on success, negative error number on failure. 489 */ 490 int platform_get_irq_byname_optional(struct platform_device *dev, 491 const char *name) 492 { 493 return __platform_get_irq_byname(dev, name); 494 } 495 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional); 496 497 /** 498 * platform_add_devices - add a numbers of platform devices 499 * @devs: array of platform devices to add 500 * @num: number of platform devices in array 501 * 502 * Return: 0 on success, negative error number on failure. 503 */ 504 int platform_add_devices(struct platform_device **devs, int num) 505 { 506 int i, ret = 0; 507 508 for (i = 0; i < num; i++) { 509 ret = platform_device_register(devs[i]); 510 if (ret) { 511 while (--i >= 0) 512 platform_device_unregister(devs[i]); 513 break; 514 } 515 } 516 517 return ret; 518 } 519 EXPORT_SYMBOL_GPL(platform_add_devices); 520 521 struct platform_object { 522 struct platform_device pdev; 523 char name[]; 524 }; 525 526 /* 527 * Set up default DMA mask for platform devices if the they weren't 528 * previously set by the architecture / DT. 529 */ 530 static void setup_pdev_dma_masks(struct platform_device *pdev) 531 { 532 pdev->dev.dma_parms = &pdev->dma_parms; 533 534 if (!pdev->dev.coherent_dma_mask) 535 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); 536 if (!pdev->dev.dma_mask) { 537 pdev->platform_dma_mask = DMA_BIT_MASK(32); 538 pdev->dev.dma_mask = &pdev->platform_dma_mask; 539 } 540 }; 541 542 /** 543 * platform_device_put - destroy a platform device 544 * @pdev: platform device to free 545 * 546 * Free all memory associated with a platform device. This function must 547 * _only_ be externally called in error cases. All other usage is a bug. 548 */ 549 void platform_device_put(struct platform_device *pdev) 550 { 551 if (!IS_ERR_OR_NULL(pdev)) 552 put_device(&pdev->dev); 553 } 554 EXPORT_SYMBOL_GPL(platform_device_put); 555 556 static void platform_device_release(struct device *dev) 557 { 558 struct platform_object *pa = container_of(dev, struct platform_object, 559 pdev.dev); 560 561 of_node_put(pa->pdev.dev.of_node); 562 kfree(pa->pdev.dev.platform_data); 563 kfree(pa->pdev.mfd_cell); 564 kfree(pa->pdev.resource); 565 kfree(pa); 566 } 567 568 /** 569 * platform_device_alloc - create a platform device 570 * @name: base name of the device we're adding 571 * @id: instance id 572 * 573 * Create a platform device object which can have other objects attached 574 * to it, and which will have attached objects freed when it is released. 575 */ 576 struct platform_device *platform_device_alloc(const char *name, int id) 577 { 578 struct platform_object *pa; 579 580 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL); 581 if (pa) { 582 strcpy(pa->name, name); 583 pa->pdev.name = pa->name; 584 pa->pdev.id = id; 585 device_initialize(&pa->pdev.dev); 586 pa->pdev.dev.release = platform_device_release; 587 setup_pdev_dma_masks(&pa->pdev); 588 } 589 590 return pa ? &pa->pdev : NULL; 591 } 592 EXPORT_SYMBOL_GPL(platform_device_alloc); 593 594 /** 595 * platform_device_add_resources - add resources to a platform device 596 * @pdev: platform device allocated by platform_device_alloc to add resources to 597 * @res: set of resources that needs to be allocated for the device 598 * @num: number of resources 599 * 600 * Add a copy of the resources to the platform device. The memory 601 * associated with the resources will be freed when the platform device is 602 * released. 603 */ 604 int platform_device_add_resources(struct platform_device *pdev, 605 const struct resource *res, unsigned int num) 606 { 607 struct resource *r = NULL; 608 609 if (res) { 610 r = kmemdup_array(res, num, sizeof(*r), GFP_KERNEL); 611 if (!r) 612 return -ENOMEM; 613 } 614 615 kfree(pdev->resource); 616 pdev->resource = r; 617 pdev->num_resources = num; 618 return 0; 619 } 620 EXPORT_SYMBOL_GPL(platform_device_add_resources); 621 622 /** 623 * platform_device_add_data - add platform-specific data to a platform device 624 * @pdev: platform device allocated by platform_device_alloc to add resources to 625 * @data: platform specific data for this platform device 626 * @size: size of platform specific data 627 * 628 * Add a copy of platform specific data to the platform device's 629 * platform_data pointer. The memory associated with the platform data 630 * will be freed when the platform device is released. 631 */ 632 int platform_device_add_data(struct platform_device *pdev, const void *data, 633 size_t size) 634 { 635 void *d = NULL; 636 637 if (data) { 638 d = kmemdup(data, size, GFP_KERNEL); 639 if (!d) 640 return -ENOMEM; 641 } 642 643 kfree(pdev->dev.platform_data); 644 pdev->dev.platform_data = d; 645 return 0; 646 } 647 EXPORT_SYMBOL_GPL(platform_device_add_data); 648 649 /** 650 * platform_device_add - add a platform device to device hierarchy 651 * @pdev: platform device we're adding 652 * 653 * This is part 2 of platform_device_register(), though may be called 654 * separately _iff_ pdev was allocated by platform_device_alloc(). 655 */ 656 int platform_device_add(struct platform_device *pdev) 657 { 658 struct device *dev = &pdev->dev; 659 u32 i; 660 int ret; 661 662 if (!dev->parent) 663 dev->parent = &platform_bus; 664 665 dev->bus = &platform_bus_type; 666 667 switch (pdev->id) { 668 default: 669 dev_set_name(dev, "%s.%d", pdev->name, pdev->id); 670 break; 671 case PLATFORM_DEVID_NONE: 672 dev_set_name(dev, "%s", pdev->name); 673 break; 674 case PLATFORM_DEVID_AUTO: 675 /* 676 * Automatically allocated device ID. We mark it as such so 677 * that we remember it must be freed, and we append a suffix 678 * to avoid namespace collision with explicit IDs. 679 */ 680 ret = ida_alloc(&platform_devid_ida, GFP_KERNEL); 681 if (ret < 0) 682 return ret; 683 pdev->id = ret; 684 pdev->id_auto = true; 685 dev_set_name(dev, "%s.%d.auto", pdev->name, pdev->id); 686 break; 687 } 688 689 for (i = 0; i < pdev->num_resources; i++) { 690 struct resource *p, *r = &pdev->resource[i]; 691 692 if (r->name == NULL) 693 r->name = dev_name(dev); 694 695 p = r->parent; 696 if (!p) { 697 if (resource_type(r) == IORESOURCE_MEM) 698 p = &iomem_resource; 699 else if (resource_type(r) == IORESOURCE_IO) 700 p = &ioport_resource; 701 } 702 703 if (p) { 704 ret = insert_resource(p, r); 705 if (ret) { 706 dev_err(dev, "failed to claim resource %d: %pR\n", i, r); 707 goto failed; 708 } 709 } 710 } 711 712 pr_debug("Registering platform device '%s'. Parent at %s\n", dev_name(dev), 713 dev_name(dev->parent)); 714 715 ret = device_add(dev); 716 if (ret) 717 goto failed; 718 719 return 0; 720 721 failed: 722 if (pdev->id_auto) { 723 ida_free(&platform_devid_ida, pdev->id); 724 pdev->id = PLATFORM_DEVID_AUTO; 725 } 726 727 while (i--) { 728 struct resource *r = &pdev->resource[i]; 729 if (r->parent) 730 release_resource(r); 731 } 732 733 return ret; 734 } 735 EXPORT_SYMBOL_GPL(platform_device_add); 736 737 /** 738 * platform_device_del - remove a platform-level device 739 * @pdev: platform device we're removing 740 * 741 * Note that this function will also release all memory- and port-based 742 * resources owned by the device (@dev->resource). This function must 743 * _only_ be externally called in error cases. All other usage is a bug. 744 */ 745 void platform_device_del(struct platform_device *pdev) 746 { 747 u32 i; 748 749 if (!IS_ERR_OR_NULL(pdev)) { 750 device_del(&pdev->dev); 751 752 if (pdev->id_auto) { 753 ida_free(&platform_devid_ida, pdev->id); 754 pdev->id = PLATFORM_DEVID_AUTO; 755 } 756 757 for (i = 0; i < pdev->num_resources; i++) { 758 struct resource *r = &pdev->resource[i]; 759 if (r->parent) 760 release_resource(r); 761 } 762 } 763 } 764 EXPORT_SYMBOL_GPL(platform_device_del); 765 766 /** 767 * platform_device_register - add a platform-level device 768 * @pdev: platform device we're adding 769 * 770 * NOTE: _Never_ directly free @pdev after calling this function, even if it 771 * returned an error! Always use platform_device_put() to give up the 772 * reference initialised in this function instead. 773 */ 774 int platform_device_register(struct platform_device *pdev) 775 { 776 device_initialize(&pdev->dev); 777 setup_pdev_dma_masks(pdev); 778 return platform_device_add(pdev); 779 } 780 EXPORT_SYMBOL_GPL(platform_device_register); 781 782 /** 783 * platform_device_unregister - unregister a platform-level device 784 * @pdev: platform device we're unregistering 785 * 786 * Unregistration is done in 2 steps. First we release all resources 787 * and remove it from the subsystem, then we drop reference count by 788 * calling platform_device_put(). 789 */ 790 void platform_device_unregister(struct platform_device *pdev) 791 { 792 platform_device_del(pdev); 793 platform_device_put(pdev); 794 } 795 EXPORT_SYMBOL_GPL(platform_device_unregister); 796 797 /** 798 * platform_device_register_full - add a platform-level device with 799 * resources and platform-specific data 800 * 801 * @pdevinfo: data used to create device 802 * 803 * Returns &struct platform_device pointer on success, or ERR_PTR() on error. 804 */ 805 struct platform_device *platform_device_register_full( 806 const struct platform_device_info *pdevinfo) 807 { 808 int ret; 809 struct platform_device *pdev; 810 811 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id); 812 if (!pdev) 813 return ERR_PTR(-ENOMEM); 814 815 pdev->dev.parent = pdevinfo->parent; 816 pdev->dev.fwnode = pdevinfo->fwnode; 817 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode)); 818 pdev->dev.of_node_reused = pdevinfo->of_node_reused; 819 820 if (pdevinfo->dma_mask) { 821 pdev->platform_dma_mask = pdevinfo->dma_mask; 822 pdev->dev.dma_mask = &pdev->platform_dma_mask; 823 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask; 824 } 825 826 ret = platform_device_add_resources(pdev, 827 pdevinfo->res, pdevinfo->num_res); 828 if (ret) 829 goto err; 830 831 ret = platform_device_add_data(pdev, 832 pdevinfo->data, pdevinfo->size_data); 833 if (ret) 834 goto err; 835 836 if (pdevinfo->properties) { 837 ret = device_create_managed_software_node(&pdev->dev, 838 pdevinfo->properties, NULL); 839 if (ret) 840 goto err; 841 } 842 843 ret = platform_device_add(pdev); 844 if (ret) { 845 err: 846 ACPI_COMPANION_SET(&pdev->dev, NULL); 847 platform_device_put(pdev); 848 return ERR_PTR(ret); 849 } 850 851 return pdev; 852 } 853 EXPORT_SYMBOL_GPL(platform_device_register_full); 854 855 /** 856 * __platform_driver_register - register a driver for platform-level devices 857 * @drv: platform driver structure 858 * @owner: owning module/driver 859 */ 860 int __platform_driver_register(struct platform_driver *drv, 861 struct module *owner) 862 { 863 drv->driver.owner = owner; 864 drv->driver.bus = &platform_bus_type; 865 866 return driver_register(&drv->driver); 867 } 868 EXPORT_SYMBOL_GPL(__platform_driver_register); 869 870 /** 871 * platform_driver_unregister - unregister a driver for platform-level devices 872 * @drv: platform driver structure 873 */ 874 void platform_driver_unregister(struct platform_driver *drv) 875 { 876 driver_unregister(&drv->driver); 877 } 878 EXPORT_SYMBOL_GPL(platform_driver_unregister); 879 880 static int platform_probe_fail(struct platform_device *pdev) 881 { 882 return -ENXIO; 883 } 884 885 static int is_bound_to_driver(struct device *dev, void *driver) 886 { 887 if (dev->driver == driver) 888 return 1; 889 return 0; 890 } 891 892 /** 893 * __platform_driver_probe - register driver for non-hotpluggable device 894 * @drv: platform driver structure 895 * @probe: the driver probe routine, probably from an __init section 896 * @module: module which will be the owner of the driver 897 * 898 * Use this instead of platform_driver_register() when you know the device 899 * is not hotpluggable and has already been registered, and you want to 900 * remove its run-once probe() infrastructure from memory after the driver 901 * has bound to the device. 902 * 903 * One typical use for this would be with drivers for controllers integrated 904 * into system-on-chip processors, where the controller devices have been 905 * configured as part of board setup. 906 * 907 * Note that this is incompatible with deferred probing. 908 * 909 * Returns zero if the driver registered and bound to a device, else returns 910 * a negative error code and with the driver not registered. 911 */ 912 int __init_or_module __platform_driver_probe(struct platform_driver *drv, 913 int (*probe)(struct platform_device *), struct module *module) 914 { 915 int retval; 916 917 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) { 918 pr_err("%s: drivers registered with %s can not be probed asynchronously\n", 919 drv->driver.name, __func__); 920 return -EINVAL; 921 } 922 923 /* 924 * We have to run our probes synchronously because we check if 925 * we find any devices to bind to and exit with error if there 926 * are any. 927 */ 928 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS; 929 930 /* 931 * Prevent driver from requesting probe deferral to avoid further 932 * futile probe attempts. 933 */ 934 drv->prevent_deferred_probe = true; 935 936 /* make sure driver won't have bind/unbind attributes */ 937 drv->driver.suppress_bind_attrs = true; 938 939 /* temporary section violation during probe() */ 940 drv->probe = probe; 941 retval = __platform_driver_register(drv, module); 942 if (retval) 943 return retval; 944 945 /* Force all new probes of this driver to fail */ 946 drv->probe = platform_probe_fail; 947 948 /* Walk all platform devices and see if any actually bound to this driver. 949 * If not, return an error as the device should have done so by now. 950 */ 951 if (!bus_for_each_dev(&platform_bus_type, NULL, &drv->driver, is_bound_to_driver)) { 952 retval = -ENODEV; 953 platform_driver_unregister(drv); 954 } 955 956 return retval; 957 } 958 EXPORT_SYMBOL_GPL(__platform_driver_probe); 959 960 /** 961 * __platform_create_bundle - register driver and create corresponding device 962 * @driver: platform driver structure 963 * @probe: the driver probe routine, probably from an __init section 964 * @res: set of resources that needs to be allocated for the device 965 * @n_res: number of resources 966 * @data: platform specific data for this platform device 967 * @size: size of platform specific data 968 * @module: module which will be the owner of the driver 969 * 970 * Use this in legacy-style modules that probe hardware directly and 971 * register a single platform device and corresponding platform driver. 972 * 973 * Returns &struct platform_device pointer on success, or ERR_PTR() on error. 974 */ 975 struct platform_device * __init_or_module __platform_create_bundle( 976 struct platform_driver *driver, 977 int (*probe)(struct platform_device *), 978 struct resource *res, unsigned int n_res, 979 const void *data, size_t size, struct module *module) 980 { 981 struct platform_device *pdev; 982 int error; 983 984 pdev = platform_device_alloc(driver->driver.name, PLATFORM_DEVID_NONE); 985 if (!pdev) { 986 error = -ENOMEM; 987 goto err_out; 988 } 989 990 error = platform_device_add_resources(pdev, res, n_res); 991 if (error) 992 goto err_pdev_put; 993 994 error = platform_device_add_data(pdev, data, size); 995 if (error) 996 goto err_pdev_put; 997 998 error = platform_device_add(pdev); 999 if (error) 1000 goto err_pdev_put; 1001 1002 error = __platform_driver_probe(driver, probe, module); 1003 if (error) 1004 goto err_pdev_del; 1005 1006 return pdev; 1007 1008 err_pdev_del: 1009 platform_device_del(pdev); 1010 err_pdev_put: 1011 platform_device_put(pdev); 1012 err_out: 1013 return ERR_PTR(error); 1014 } 1015 EXPORT_SYMBOL_GPL(__platform_create_bundle); 1016 1017 /** 1018 * __platform_register_drivers - register an array of platform drivers 1019 * @drivers: an array of drivers to register 1020 * @count: the number of drivers to register 1021 * @owner: module owning the drivers 1022 * 1023 * Registers platform drivers specified by an array. On failure to register a 1024 * driver, all previously registered drivers will be unregistered. Callers of 1025 * this API should use platform_unregister_drivers() to unregister drivers in 1026 * the reverse order. 1027 * 1028 * Returns: 0 on success or a negative error code on failure. 1029 */ 1030 int __platform_register_drivers(struct platform_driver * const *drivers, 1031 unsigned int count, struct module *owner) 1032 { 1033 unsigned int i; 1034 int err; 1035 1036 for (i = 0; i < count; i++) { 1037 pr_debug("registering platform driver %ps\n", drivers[i]); 1038 1039 err = __platform_driver_register(drivers[i], owner); 1040 if (err < 0) { 1041 pr_err("failed to register platform driver %ps: %d\n", 1042 drivers[i], err); 1043 goto error; 1044 } 1045 } 1046 1047 return 0; 1048 1049 error: 1050 while (i--) { 1051 pr_debug("unregistering platform driver %ps\n", drivers[i]); 1052 platform_driver_unregister(drivers[i]); 1053 } 1054 1055 return err; 1056 } 1057 EXPORT_SYMBOL_GPL(__platform_register_drivers); 1058 1059 /** 1060 * platform_unregister_drivers - unregister an array of platform drivers 1061 * @drivers: an array of drivers to unregister 1062 * @count: the number of drivers to unregister 1063 * 1064 * Unregisters platform drivers specified by an array. This is typically used 1065 * to complement an earlier call to platform_register_drivers(). Drivers are 1066 * unregistered in the reverse order in which they were registered. 1067 */ 1068 void platform_unregister_drivers(struct platform_driver * const *drivers, 1069 unsigned int count) 1070 { 1071 while (count--) { 1072 pr_debug("unregistering platform driver %ps\n", drivers[count]); 1073 platform_driver_unregister(drivers[count]); 1074 } 1075 } 1076 EXPORT_SYMBOL_GPL(platform_unregister_drivers); 1077 1078 static const struct platform_device_id *platform_match_id( 1079 const struct platform_device_id *id, 1080 struct platform_device *pdev) 1081 { 1082 while (id->name[0]) { 1083 if (strcmp(pdev->name, id->name) == 0) { 1084 pdev->id_entry = id; 1085 return id; 1086 } 1087 id++; 1088 } 1089 return NULL; 1090 } 1091 1092 #ifdef CONFIG_PM_SLEEP 1093 1094 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) 1095 { 1096 struct platform_driver *pdrv = to_platform_driver(dev->driver); 1097 struct platform_device *pdev = to_platform_device(dev); 1098 int ret = 0; 1099 1100 if (dev->driver && pdrv->suspend) 1101 ret = pdrv->suspend(pdev, mesg); 1102 1103 return ret; 1104 } 1105 1106 static int platform_legacy_resume(struct device *dev) 1107 { 1108 struct platform_driver *pdrv = to_platform_driver(dev->driver); 1109 struct platform_device *pdev = to_platform_device(dev); 1110 int ret = 0; 1111 1112 if (dev->driver && pdrv->resume) 1113 ret = pdrv->resume(pdev); 1114 1115 return ret; 1116 } 1117 1118 #endif /* CONFIG_PM_SLEEP */ 1119 1120 #ifdef CONFIG_SUSPEND 1121 1122 int platform_pm_suspend(struct device *dev) 1123 { 1124 const struct device_driver *drv = dev->driver; 1125 int ret = 0; 1126 1127 if (!drv) 1128 return 0; 1129 1130 if (drv->pm) { 1131 if (drv->pm->suspend) 1132 ret = drv->pm->suspend(dev); 1133 } else { 1134 ret = platform_legacy_suspend(dev, PMSG_SUSPEND); 1135 } 1136 1137 return ret; 1138 } 1139 1140 int platform_pm_resume(struct device *dev) 1141 { 1142 const struct device_driver *drv = dev->driver; 1143 int ret = 0; 1144 1145 if (!drv) 1146 return 0; 1147 1148 if (drv->pm) { 1149 if (drv->pm->resume) 1150 ret = drv->pm->resume(dev); 1151 } else { 1152 ret = platform_legacy_resume(dev); 1153 } 1154 1155 return ret; 1156 } 1157 1158 #endif /* CONFIG_SUSPEND */ 1159 1160 #ifdef CONFIG_HIBERNATE_CALLBACKS 1161 1162 int platform_pm_freeze(struct device *dev) 1163 { 1164 const struct device_driver *drv = dev->driver; 1165 int ret = 0; 1166 1167 if (!drv) 1168 return 0; 1169 1170 if (drv->pm) { 1171 if (drv->pm->freeze) 1172 ret = drv->pm->freeze(dev); 1173 } else { 1174 ret = platform_legacy_suspend(dev, PMSG_FREEZE); 1175 } 1176 1177 return ret; 1178 } 1179 1180 int platform_pm_thaw(struct device *dev) 1181 { 1182 const struct device_driver *drv = dev->driver; 1183 int ret = 0; 1184 1185 if (!drv) 1186 return 0; 1187 1188 if (drv->pm) { 1189 if (drv->pm->thaw) 1190 ret = drv->pm->thaw(dev); 1191 } else { 1192 ret = platform_legacy_resume(dev); 1193 } 1194 1195 return ret; 1196 } 1197 1198 int platform_pm_poweroff(struct device *dev) 1199 { 1200 const struct device_driver *drv = dev->driver; 1201 int ret = 0; 1202 1203 if (!drv) 1204 return 0; 1205 1206 if (drv->pm) { 1207 if (drv->pm->poweroff) 1208 ret = drv->pm->poweroff(dev); 1209 } else { 1210 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE); 1211 } 1212 1213 return ret; 1214 } 1215 1216 int platform_pm_restore(struct device *dev) 1217 { 1218 const struct device_driver *drv = dev->driver; 1219 int ret = 0; 1220 1221 if (!drv) 1222 return 0; 1223 1224 if (drv->pm) { 1225 if (drv->pm->restore) 1226 ret = drv->pm->restore(dev); 1227 } else { 1228 ret = platform_legacy_resume(dev); 1229 } 1230 1231 return ret; 1232 } 1233 1234 #endif /* CONFIG_HIBERNATE_CALLBACKS */ 1235 1236 /* modalias support enables more hands-off userspace setup: 1237 * (a) environment variable lets new-style hotplug events work once system is 1238 * fully running: "modprobe $MODALIAS" 1239 * (b) sysfs attribute lets new-style coldplug recover from hotplug events 1240 * mishandled before system is fully running: "modprobe $(cat modalias)" 1241 */ 1242 static ssize_t modalias_show(struct device *dev, 1243 struct device_attribute *attr, char *buf) 1244 { 1245 struct platform_device *pdev = to_platform_device(dev); 1246 int len; 1247 1248 len = of_device_modalias(dev, buf, PAGE_SIZE); 1249 if (len != -ENODEV) 1250 return len; 1251 1252 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 1253 if (len != -ENODEV) 1254 return len; 1255 1256 return sysfs_emit(buf, "platform:%s\n", pdev->name); 1257 } 1258 static DEVICE_ATTR_RO(modalias); 1259 1260 static ssize_t numa_node_show(struct device *dev, 1261 struct device_attribute *attr, char *buf) 1262 { 1263 return sysfs_emit(buf, "%d\n", dev_to_node(dev)); 1264 } 1265 static DEVICE_ATTR_RO(numa_node); 1266 1267 static struct attribute *platform_dev_attrs[] = { 1268 &dev_attr_modalias.attr, 1269 &dev_attr_numa_node.attr, 1270 NULL, 1271 }; 1272 1273 static umode_t platform_dev_attrs_visible(struct kobject *kobj, struct attribute *a, 1274 int n) 1275 { 1276 struct device *dev = container_of(kobj, typeof(*dev), kobj); 1277 1278 if (a == &dev_attr_numa_node.attr && 1279 dev_to_node(dev) == NUMA_NO_NODE) 1280 return 0; 1281 1282 return a->mode; 1283 } 1284 1285 static const struct attribute_group platform_dev_group = { 1286 .attrs = platform_dev_attrs, 1287 .is_visible = platform_dev_attrs_visible, 1288 }; 1289 __ATTRIBUTE_GROUPS(platform_dev); 1290 1291 1292 /** 1293 * platform_match - bind platform device to platform driver. 1294 * @dev: device. 1295 * @drv: driver. 1296 * 1297 * Platform device IDs are assumed to be encoded like this: 1298 * "<name><instance>", where <name> is a short description of the type of 1299 * device, like "pci" or "floppy", and <instance> is the enumerated 1300 * instance of the device, like '0' or '42'. Driver IDs are simply 1301 * "<name>". So, extract the <name> from the platform_device structure, 1302 * and compare it against the name of the driver. Return whether they match 1303 * or not. 1304 */ 1305 static int platform_match(struct device *dev, const struct device_driver *drv) 1306 { 1307 struct platform_device *pdev = to_platform_device(dev); 1308 struct platform_driver *pdrv = to_platform_driver(drv); 1309 int ret; 1310 1311 /* When driver_override is set, only bind to the matching driver */ 1312 ret = device_match_driver_override(dev, drv); 1313 if (ret >= 0) 1314 return ret; 1315 1316 /* Attempt an OF style match first */ 1317 if (of_driver_match_device(dev, drv)) 1318 return 1; 1319 1320 /* Then try ACPI style match */ 1321 if (acpi_driver_match_device(dev, drv)) 1322 return 1; 1323 1324 /* Then try to match against the id table */ 1325 if (pdrv->id_table) 1326 return platform_match_id(pdrv->id_table, pdev) != NULL; 1327 1328 /* fall-back to driver name match */ 1329 return (strcmp(pdev->name, drv->name) == 0); 1330 } 1331 1332 static int platform_uevent(const struct device *dev, struct kobj_uevent_env *env) 1333 { 1334 const struct platform_device *pdev = to_platform_device(dev); 1335 int rc; 1336 1337 /* Some devices have extra OF data and an OF-style MODALIAS */ 1338 rc = of_device_uevent_modalias(dev, env); 1339 if (rc != -ENODEV) 1340 return rc; 1341 1342 rc = acpi_device_uevent_modalias(dev, env); 1343 if (rc != -ENODEV) 1344 return rc; 1345 1346 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, 1347 pdev->name); 1348 return 0; 1349 } 1350 1351 static int platform_probe(struct device *_dev) 1352 { 1353 struct platform_driver *drv = to_platform_driver(_dev->driver); 1354 struct platform_device *dev = to_platform_device(_dev); 1355 int ret; 1356 1357 /* 1358 * A driver registered using platform_driver_probe() cannot be bound 1359 * again later because the probe function usually lives in __init code 1360 * and so is gone. For these drivers .probe is set to 1361 * platform_probe_fail in __platform_driver_probe(). Don't even prepare 1362 * clocks and PM domains for these to match the traditional behaviour. 1363 */ 1364 if (unlikely(drv->probe == platform_probe_fail)) 1365 return -ENXIO; 1366 1367 ret = of_clk_set_defaults(_dev->of_node, false); 1368 if (ret < 0) 1369 return ret; 1370 1371 ret = dev_pm_domain_attach(_dev, PD_FLAG_ATTACH_POWER_ON | 1372 PD_FLAG_DETACH_POWER_OFF); 1373 if (ret) 1374 goto out; 1375 1376 if (drv->probe) 1377 ret = drv->probe(dev); 1378 1379 out: 1380 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) { 1381 dev_warn(_dev, "probe deferral not supported\n"); 1382 ret = -ENXIO; 1383 } 1384 1385 return ret; 1386 } 1387 1388 static void platform_remove(struct device *_dev) 1389 { 1390 struct platform_driver *drv = to_platform_driver(_dev->driver); 1391 struct platform_device *dev = to_platform_device(_dev); 1392 1393 if (drv->remove) 1394 drv->remove(dev); 1395 } 1396 1397 static void platform_shutdown(struct device *_dev) 1398 { 1399 struct platform_device *dev = to_platform_device(_dev); 1400 struct platform_driver *drv; 1401 1402 if (!_dev->driver) 1403 return; 1404 1405 drv = to_platform_driver(_dev->driver); 1406 if (drv->shutdown) 1407 drv->shutdown(dev); 1408 } 1409 1410 static int platform_dma_configure(struct device *dev) 1411 { 1412 struct device_driver *drv = READ_ONCE(dev->driver); 1413 struct fwnode_handle *fwnode = dev_fwnode(dev); 1414 enum dev_dma_attr attr; 1415 int ret = 0; 1416 1417 if (is_of_node(fwnode)) { 1418 ret = of_dma_configure(dev, to_of_node(fwnode), true); 1419 } else if (is_acpi_device_node(fwnode)) { 1420 attr = acpi_get_dma_attr(to_acpi_device_node(fwnode)); 1421 ret = acpi_dma_configure(dev, attr); 1422 } 1423 /* @dev->driver may not be valid when we're called from the IOMMU layer */ 1424 if (ret || !drv || to_platform_driver(drv)->driver_managed_dma) 1425 return ret; 1426 1427 ret = iommu_device_use_default_domain(dev); 1428 if (ret) 1429 arch_teardown_dma_ops(dev); 1430 1431 return ret; 1432 } 1433 1434 static void platform_dma_cleanup(struct device *dev) 1435 { 1436 struct platform_driver *drv = to_platform_driver(dev->driver); 1437 1438 if (!drv->driver_managed_dma) 1439 iommu_device_unuse_default_domain(dev); 1440 } 1441 1442 static const struct dev_pm_ops platform_dev_pm_ops = { 1443 SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL) 1444 USE_PLATFORM_PM_SLEEP_OPS 1445 }; 1446 1447 const struct bus_type platform_bus_type = { 1448 .name = "platform", 1449 .dev_groups = platform_dev_groups, 1450 .driver_override = true, 1451 .match = platform_match, 1452 .uevent = platform_uevent, 1453 .probe = platform_probe, 1454 .remove = platform_remove, 1455 .shutdown = platform_shutdown, 1456 .dma_configure = platform_dma_configure, 1457 .dma_cleanup = platform_dma_cleanup, 1458 .pm = &platform_dev_pm_ops, 1459 }; 1460 EXPORT_SYMBOL_GPL(platform_bus_type); 1461 1462 static inline int __platform_match(struct device *dev, const void *drv) 1463 { 1464 return platform_match(dev, (struct device_driver *)drv); 1465 } 1466 1467 /** 1468 * platform_find_device_by_driver - Find a platform device with a given 1469 * driver. 1470 * @start: The device to start the search from. 1471 * @drv: The device driver to look for. 1472 */ 1473 struct device *platform_find_device_by_driver(struct device *start, 1474 const struct device_driver *drv) 1475 { 1476 return bus_find_device(&platform_bus_type, start, drv, 1477 __platform_match); 1478 } 1479 EXPORT_SYMBOL_GPL(platform_find_device_by_driver); 1480 1481 void __weak __init early_platform_cleanup(void) { } 1482 1483 int __init platform_bus_init(void) 1484 { 1485 int error; 1486 1487 early_platform_cleanup(); 1488 1489 error = device_register(&platform_bus); 1490 if (error) { 1491 put_device(&platform_bus); 1492 return error; 1493 } 1494 error = bus_register(&platform_bus_type); 1495 if (error) 1496 device_unregister(&platform_bus); 1497 1498 return error; 1499 }