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