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