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