1 /* 2 * platform.c - platform 'pseudo' bus for legacy devices 3 * 4 * Copyright (c) 2002-3 Patrick Mochel 5 * Copyright (c) 2002-3 Open Source Development Labs 6 * 7 * This file is released under the GPLv2 8 * 9 * Please see Documentation/driver-model/platform.txt for more 10 * information. 11 */ 12 13 #include <linux/string.h> 14 #include <linux/platform_device.h> 15 #include <linux/of_device.h> 16 #include <linux/module.h> 17 #include <linux/init.h> 18 #include <linux/dma-mapping.h> 19 #include <linux/bootmem.h> 20 #include <linux/err.h> 21 #include <linux/slab.h> 22 #include <linux/pm_runtime.h> 23 24 #include "base.h" 25 26 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \ 27 driver)) 28 29 struct device platform_bus = { 30 .init_name = "platform", 31 }; 32 EXPORT_SYMBOL_GPL(platform_bus); 33 34 /** 35 * platform_get_resource - get a resource for a device 36 * @dev: platform device 37 * @type: resource type 38 * @num: resource index 39 */ 40 struct resource *platform_get_resource(struct platform_device *dev, 41 unsigned int type, unsigned int num) 42 { 43 int i; 44 45 for (i = 0; i < dev->num_resources; i++) { 46 struct resource *r = &dev->resource[i]; 47 48 if (type == resource_type(r) && num-- == 0) 49 return r; 50 } 51 return NULL; 52 } 53 EXPORT_SYMBOL_GPL(platform_get_resource); 54 55 /** 56 * platform_get_irq - get an IRQ for a device 57 * @dev: platform device 58 * @num: IRQ number index 59 */ 60 int platform_get_irq(struct platform_device *dev, unsigned int num) 61 { 62 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num); 63 64 return r ? r->start : -ENXIO; 65 } 66 EXPORT_SYMBOL_GPL(platform_get_irq); 67 68 /** 69 * platform_get_resource_byname - get a resource for a device by name 70 * @dev: platform device 71 * @type: resource type 72 * @name: resource name 73 */ 74 struct resource *platform_get_resource_byname(struct platform_device *dev, 75 unsigned int type, 76 const char *name) 77 { 78 int i; 79 80 for (i = 0; i < dev->num_resources; i++) { 81 struct resource *r = &dev->resource[i]; 82 83 if (type == resource_type(r) && !strcmp(r->name, name)) 84 return r; 85 } 86 return NULL; 87 } 88 EXPORT_SYMBOL_GPL(platform_get_resource_byname); 89 90 /** 91 * platform_get_irq - get an IRQ for a device 92 * @dev: platform device 93 * @name: IRQ name 94 */ 95 int platform_get_irq_byname(struct platform_device *dev, const char *name) 96 { 97 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, 98 name); 99 100 return r ? r->start : -ENXIO; 101 } 102 EXPORT_SYMBOL_GPL(platform_get_irq_byname); 103 104 /** 105 * platform_add_devices - add a numbers of platform devices 106 * @devs: array of platform devices to add 107 * @num: number of platform devices in array 108 */ 109 int platform_add_devices(struct platform_device **devs, int num) 110 { 111 int i, ret = 0; 112 113 for (i = 0; i < num; i++) { 114 ret = platform_device_register(devs[i]); 115 if (ret) { 116 while (--i >= 0) 117 platform_device_unregister(devs[i]); 118 break; 119 } 120 } 121 122 return ret; 123 } 124 EXPORT_SYMBOL_GPL(platform_add_devices); 125 126 struct platform_object { 127 struct platform_device pdev; 128 char name[1]; 129 }; 130 131 /** 132 * platform_device_put - destroy a platform device 133 * @pdev: platform device to free 134 * 135 * Free all memory associated with a platform device. This function must 136 * _only_ be externally called in error cases. All other usage is a bug. 137 */ 138 void platform_device_put(struct platform_device *pdev) 139 { 140 if (pdev) 141 put_device(&pdev->dev); 142 } 143 EXPORT_SYMBOL_GPL(platform_device_put); 144 145 static void platform_device_release(struct device *dev) 146 { 147 struct platform_object *pa = container_of(dev, struct platform_object, 148 pdev.dev); 149 150 kfree(pa->pdev.dev.platform_data); 151 kfree(pa->pdev.resource); 152 kfree(pa); 153 } 154 155 /** 156 * platform_device_alloc - create a platform device 157 * @name: base name of the device we're adding 158 * @id: instance id 159 * 160 * Create a platform device object which can have other objects attached 161 * to it, and which will have attached objects freed when it is released. 162 */ 163 struct platform_device *platform_device_alloc(const char *name, int id) 164 { 165 struct platform_object *pa; 166 167 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL); 168 if (pa) { 169 strcpy(pa->name, name); 170 pa->pdev.name = pa->name; 171 pa->pdev.id = id; 172 device_initialize(&pa->pdev.dev); 173 pa->pdev.dev.release = platform_device_release; 174 } 175 176 return pa ? &pa->pdev : NULL; 177 } 178 EXPORT_SYMBOL_GPL(platform_device_alloc); 179 180 /** 181 * platform_device_add_resources - add resources to a platform device 182 * @pdev: platform device allocated by platform_device_alloc to add resources to 183 * @res: set of resources that needs to be allocated for the device 184 * @num: number of resources 185 * 186 * Add a copy of the resources to the platform device. The memory 187 * associated with the resources will be freed when the platform device is 188 * released. 189 */ 190 int platform_device_add_resources(struct platform_device *pdev, 191 const struct resource *res, unsigned int num) 192 { 193 struct resource *r; 194 195 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL); 196 if (r) { 197 pdev->resource = r; 198 pdev->num_resources = num; 199 return 0; 200 } 201 return -ENOMEM; 202 } 203 EXPORT_SYMBOL_GPL(platform_device_add_resources); 204 205 /** 206 * platform_device_add_data - add platform-specific data to a platform device 207 * @pdev: platform device allocated by platform_device_alloc to add resources to 208 * @data: platform specific data for this platform device 209 * @size: size of platform specific data 210 * 211 * Add a copy of platform specific data to the platform device's 212 * platform_data pointer. The memory associated with the platform data 213 * will be freed when the platform device is released. 214 */ 215 int platform_device_add_data(struct platform_device *pdev, const void *data, 216 size_t size) 217 { 218 void *d = kmemdup(data, size, GFP_KERNEL); 219 220 if (d) { 221 pdev->dev.platform_data = d; 222 return 0; 223 } 224 return -ENOMEM; 225 } 226 EXPORT_SYMBOL_GPL(platform_device_add_data); 227 228 /** 229 * platform_device_add - add a platform device to device hierarchy 230 * @pdev: platform device we're adding 231 * 232 * This is part 2 of platform_device_register(), though may be called 233 * separately _iff_ pdev was allocated by platform_device_alloc(). 234 */ 235 int platform_device_add(struct platform_device *pdev) 236 { 237 int i, ret = 0; 238 239 if (!pdev) 240 return -EINVAL; 241 242 if (!pdev->dev.parent) 243 pdev->dev.parent = &platform_bus; 244 245 pdev->dev.bus = &platform_bus_type; 246 247 if (pdev->id != -1) 248 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); 249 else 250 dev_set_name(&pdev->dev, "%s", pdev->name); 251 252 for (i = 0; i < pdev->num_resources; i++) { 253 struct resource *p, *r = &pdev->resource[i]; 254 255 if (r->name == NULL) 256 r->name = dev_name(&pdev->dev); 257 258 p = r->parent; 259 if (!p) { 260 if (resource_type(r) == IORESOURCE_MEM) 261 p = &iomem_resource; 262 else if (resource_type(r) == IORESOURCE_IO) 263 p = &ioport_resource; 264 } 265 266 if (p && insert_resource(p, r)) { 267 printk(KERN_ERR 268 "%s: failed to claim resource %d\n", 269 dev_name(&pdev->dev), i); 270 ret = -EBUSY; 271 goto failed; 272 } 273 } 274 275 pr_debug("Registering platform device '%s'. Parent at %s\n", 276 dev_name(&pdev->dev), dev_name(pdev->dev.parent)); 277 278 ret = device_add(&pdev->dev); 279 if (ret == 0) 280 return ret; 281 282 failed: 283 while (--i >= 0) { 284 struct resource *r = &pdev->resource[i]; 285 unsigned long type = resource_type(r); 286 287 if (type == IORESOURCE_MEM || type == IORESOURCE_IO) 288 release_resource(r); 289 } 290 291 return ret; 292 } 293 EXPORT_SYMBOL_GPL(platform_device_add); 294 295 /** 296 * platform_device_del - remove a platform-level device 297 * @pdev: platform device we're removing 298 * 299 * Note that this function will also release all memory- and port-based 300 * resources owned by the device (@dev->resource). This function must 301 * _only_ be externally called in error cases. All other usage is a bug. 302 */ 303 void platform_device_del(struct platform_device *pdev) 304 { 305 int i; 306 307 if (pdev) { 308 device_del(&pdev->dev); 309 310 for (i = 0; i < pdev->num_resources; i++) { 311 struct resource *r = &pdev->resource[i]; 312 unsigned long type = resource_type(r); 313 314 if (type == IORESOURCE_MEM || type == IORESOURCE_IO) 315 release_resource(r); 316 } 317 } 318 } 319 EXPORT_SYMBOL_GPL(platform_device_del); 320 321 /** 322 * platform_device_register - add a platform-level device 323 * @pdev: platform device we're adding 324 */ 325 int platform_device_register(struct platform_device *pdev) 326 { 327 device_initialize(&pdev->dev); 328 return platform_device_add(pdev); 329 } 330 EXPORT_SYMBOL_GPL(platform_device_register); 331 332 /** 333 * platform_device_unregister - unregister a platform-level device 334 * @pdev: platform device we're unregistering 335 * 336 * Unregistration is done in 2 steps. First we release all resources 337 * and remove it from the subsystem, then we drop reference count by 338 * calling platform_device_put(). 339 */ 340 void platform_device_unregister(struct platform_device *pdev) 341 { 342 platform_device_del(pdev); 343 platform_device_put(pdev); 344 } 345 EXPORT_SYMBOL_GPL(platform_device_unregister); 346 347 /** 348 * platform_device_register_resndata - add a platform-level device with 349 * resources and platform-specific data 350 * 351 * @parent: parent device for the device we're adding 352 * @name: base name of the device we're adding 353 * @id: instance id 354 * @res: set of resources that needs to be allocated for the device 355 * @num: number of resources 356 * @data: platform specific data for this platform device 357 * @size: size of platform specific data 358 * 359 * Returns &struct platform_device pointer on success, or ERR_PTR() on error. 360 */ 361 struct platform_device *__init_or_module platform_device_register_resndata( 362 struct device *parent, 363 const char *name, int id, 364 const struct resource *res, unsigned int num, 365 const void *data, size_t size) 366 { 367 int ret = -ENOMEM; 368 struct platform_device *pdev; 369 370 pdev = platform_device_alloc(name, id); 371 if (!pdev) 372 goto err; 373 374 pdev->dev.parent = parent; 375 376 if (res) { 377 ret = platform_device_add_resources(pdev, res, num); 378 if (ret) 379 goto err; 380 } 381 382 if (data) { 383 ret = platform_device_add_data(pdev, data, size); 384 if (ret) 385 goto err; 386 } 387 388 ret = platform_device_add(pdev); 389 if (ret) { 390 err: 391 platform_device_put(pdev); 392 return ERR_PTR(ret); 393 } 394 395 return pdev; 396 } 397 EXPORT_SYMBOL_GPL(platform_device_register_resndata); 398 399 static int platform_drv_probe(struct device *_dev) 400 { 401 struct platform_driver *drv = to_platform_driver(_dev->driver); 402 struct platform_device *dev = to_platform_device(_dev); 403 404 return drv->probe(dev); 405 } 406 407 static int platform_drv_probe_fail(struct device *_dev) 408 { 409 return -ENXIO; 410 } 411 412 static int platform_drv_remove(struct device *_dev) 413 { 414 struct platform_driver *drv = to_platform_driver(_dev->driver); 415 struct platform_device *dev = to_platform_device(_dev); 416 417 return drv->remove(dev); 418 } 419 420 static void platform_drv_shutdown(struct device *_dev) 421 { 422 struct platform_driver *drv = to_platform_driver(_dev->driver); 423 struct platform_device *dev = to_platform_device(_dev); 424 425 drv->shutdown(dev); 426 } 427 428 /** 429 * platform_driver_register - register a driver for platform-level devices 430 * @drv: platform driver structure 431 */ 432 int platform_driver_register(struct platform_driver *drv) 433 { 434 drv->driver.bus = &platform_bus_type; 435 if (drv->probe) 436 drv->driver.probe = platform_drv_probe; 437 if (drv->remove) 438 drv->driver.remove = platform_drv_remove; 439 if (drv->shutdown) 440 drv->driver.shutdown = platform_drv_shutdown; 441 442 return driver_register(&drv->driver); 443 } 444 EXPORT_SYMBOL_GPL(platform_driver_register); 445 446 /** 447 * platform_driver_unregister - unregister a driver for platform-level devices 448 * @drv: platform driver structure 449 */ 450 void platform_driver_unregister(struct platform_driver *drv) 451 { 452 driver_unregister(&drv->driver); 453 } 454 EXPORT_SYMBOL_GPL(platform_driver_unregister); 455 456 /** 457 * platform_driver_probe - register driver for non-hotpluggable device 458 * @drv: platform driver structure 459 * @probe: the driver probe routine, probably from an __init section 460 * 461 * Use this instead of platform_driver_register() when you know the device 462 * is not hotpluggable and has already been registered, and you want to 463 * remove its run-once probe() infrastructure from memory after the driver 464 * has bound to the device. 465 * 466 * One typical use for this would be with drivers for controllers integrated 467 * into system-on-chip processors, where the controller devices have been 468 * configured as part of board setup. 469 * 470 * Returns zero if the driver registered and bound to a device, else returns 471 * a negative error code and with the driver not registered. 472 */ 473 int __init_or_module platform_driver_probe(struct platform_driver *drv, 474 int (*probe)(struct platform_device *)) 475 { 476 int retval, code; 477 478 /* make sure driver won't have bind/unbind attributes */ 479 drv->driver.suppress_bind_attrs = true; 480 481 /* temporary section violation during probe() */ 482 drv->probe = probe; 483 retval = code = platform_driver_register(drv); 484 485 /* 486 * Fixup that section violation, being paranoid about code scanning 487 * the list of drivers in order to probe new devices. Check to see 488 * if the probe was successful, and make sure any forced probes of 489 * new devices fail. 490 */ 491 spin_lock(&platform_bus_type.p->klist_drivers.k_lock); 492 drv->probe = NULL; 493 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) 494 retval = -ENODEV; 495 drv->driver.probe = platform_drv_probe_fail; 496 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock); 497 498 if (code != retval) 499 platform_driver_unregister(drv); 500 return retval; 501 } 502 EXPORT_SYMBOL_GPL(platform_driver_probe); 503 504 /** 505 * platform_create_bundle - register driver and create corresponding device 506 * @driver: platform driver structure 507 * @probe: the driver probe routine, probably from an __init section 508 * @res: set of resources that needs to be allocated for the device 509 * @n_res: number of resources 510 * @data: platform specific data for this platform device 511 * @size: size of platform specific data 512 * 513 * Use this in legacy-style modules that probe hardware directly and 514 * register a single platform device and corresponding platform driver. 515 * 516 * Returns &struct platform_device pointer on success, or ERR_PTR() on error. 517 */ 518 struct platform_device * __init_or_module platform_create_bundle( 519 struct platform_driver *driver, 520 int (*probe)(struct platform_device *), 521 struct resource *res, unsigned int n_res, 522 const void *data, size_t size) 523 { 524 struct platform_device *pdev; 525 int error; 526 527 pdev = platform_device_alloc(driver->driver.name, -1); 528 if (!pdev) { 529 error = -ENOMEM; 530 goto err_out; 531 } 532 533 if (res) { 534 error = platform_device_add_resources(pdev, res, n_res); 535 if (error) 536 goto err_pdev_put; 537 } 538 539 if (data) { 540 error = platform_device_add_data(pdev, data, size); 541 if (error) 542 goto err_pdev_put; 543 } 544 545 error = platform_device_add(pdev); 546 if (error) 547 goto err_pdev_put; 548 549 error = platform_driver_probe(driver, probe); 550 if (error) 551 goto err_pdev_del; 552 553 return pdev; 554 555 err_pdev_del: 556 platform_device_del(pdev); 557 err_pdev_put: 558 platform_device_put(pdev); 559 err_out: 560 return ERR_PTR(error); 561 } 562 EXPORT_SYMBOL_GPL(platform_create_bundle); 563 564 /* modalias support enables more hands-off userspace setup: 565 * (a) environment variable lets new-style hotplug events work once system is 566 * fully running: "modprobe $MODALIAS" 567 * (b) sysfs attribute lets new-style coldplug recover from hotplug events 568 * mishandled before system is fully running: "modprobe $(cat modalias)" 569 */ 570 static ssize_t modalias_show(struct device *dev, struct device_attribute *a, 571 char *buf) 572 { 573 struct platform_device *pdev = to_platform_device(dev); 574 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); 575 576 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; 577 } 578 579 static struct device_attribute platform_dev_attrs[] = { 580 __ATTR_RO(modalias), 581 __ATTR_NULL, 582 }; 583 584 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) 585 { 586 struct platform_device *pdev = to_platform_device(dev); 587 int rc; 588 589 /* Some devices have extra OF data and an OF-style MODALIAS */ 590 rc = of_device_uevent(dev,env); 591 if (rc != -ENODEV) 592 return rc; 593 594 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, 595 (pdev->id_entry) ? pdev->id_entry->name : pdev->name); 596 return 0; 597 } 598 599 static const struct platform_device_id *platform_match_id( 600 const struct platform_device_id *id, 601 struct platform_device *pdev) 602 { 603 while (id->name[0]) { 604 if (strcmp(pdev->name, id->name) == 0) { 605 pdev->id_entry = id; 606 return id; 607 } 608 id++; 609 } 610 return NULL; 611 } 612 613 /** 614 * platform_match - bind platform device to platform driver. 615 * @dev: device. 616 * @drv: driver. 617 * 618 * Platform device IDs are assumed to be encoded like this: 619 * "<name><instance>", where <name> is a short description of the type of 620 * device, like "pci" or "floppy", and <instance> is the enumerated 621 * instance of the device, like '0' or '42'. Driver IDs are simply 622 * "<name>". So, extract the <name> from the platform_device structure, 623 * and compare it against the name of the driver. Return whether they match 624 * or not. 625 */ 626 static int platform_match(struct device *dev, struct device_driver *drv) 627 { 628 struct platform_device *pdev = to_platform_device(dev); 629 struct platform_driver *pdrv = to_platform_driver(drv); 630 631 /* Attempt an OF style match first */ 632 if (of_driver_match_device(dev, drv)) 633 return 1; 634 635 /* Then try to match against the id table */ 636 if (pdrv->id_table) 637 return platform_match_id(pdrv->id_table, pdev) != NULL; 638 639 /* fall-back to driver name match */ 640 return (strcmp(pdev->name, drv->name) == 0); 641 } 642 643 #ifdef CONFIG_PM_SLEEP 644 645 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) 646 { 647 struct platform_driver *pdrv = to_platform_driver(dev->driver); 648 struct platform_device *pdev = to_platform_device(dev); 649 int ret = 0; 650 651 if (dev->driver && pdrv->suspend) 652 ret = pdrv->suspend(pdev, mesg); 653 654 return ret; 655 } 656 657 static int platform_legacy_resume(struct device *dev) 658 { 659 struct platform_driver *pdrv = to_platform_driver(dev->driver); 660 struct platform_device *pdev = to_platform_device(dev); 661 int ret = 0; 662 663 if (dev->driver && pdrv->resume) 664 ret = pdrv->resume(pdev); 665 666 return ret; 667 } 668 669 static int platform_pm_prepare(struct device *dev) 670 { 671 struct device_driver *drv = dev->driver; 672 int ret = 0; 673 674 if (drv && drv->pm && drv->pm->prepare) 675 ret = drv->pm->prepare(dev); 676 677 return ret; 678 } 679 680 static void platform_pm_complete(struct device *dev) 681 { 682 struct device_driver *drv = dev->driver; 683 684 if (drv && drv->pm && drv->pm->complete) 685 drv->pm->complete(dev); 686 } 687 688 #else /* !CONFIG_PM_SLEEP */ 689 690 #define platform_pm_prepare NULL 691 #define platform_pm_complete NULL 692 693 #endif /* !CONFIG_PM_SLEEP */ 694 695 #ifdef CONFIG_SUSPEND 696 697 int __weak platform_pm_suspend(struct device *dev) 698 { 699 struct device_driver *drv = dev->driver; 700 int ret = 0; 701 702 if (!drv) 703 return 0; 704 705 if (drv->pm) { 706 if (drv->pm->suspend) 707 ret = drv->pm->suspend(dev); 708 } else { 709 ret = platform_legacy_suspend(dev, PMSG_SUSPEND); 710 } 711 712 return ret; 713 } 714 715 int __weak platform_pm_suspend_noirq(struct device *dev) 716 { 717 struct device_driver *drv = dev->driver; 718 int ret = 0; 719 720 if (!drv) 721 return 0; 722 723 if (drv->pm) { 724 if (drv->pm->suspend_noirq) 725 ret = drv->pm->suspend_noirq(dev); 726 } 727 728 return ret; 729 } 730 731 int __weak platform_pm_resume(struct device *dev) 732 { 733 struct device_driver *drv = dev->driver; 734 int ret = 0; 735 736 if (!drv) 737 return 0; 738 739 if (drv->pm) { 740 if (drv->pm->resume) 741 ret = drv->pm->resume(dev); 742 } else { 743 ret = platform_legacy_resume(dev); 744 } 745 746 return ret; 747 } 748 749 int __weak platform_pm_resume_noirq(struct device *dev) 750 { 751 struct device_driver *drv = dev->driver; 752 int ret = 0; 753 754 if (!drv) 755 return 0; 756 757 if (drv->pm) { 758 if (drv->pm->resume_noirq) 759 ret = drv->pm->resume_noirq(dev); 760 } 761 762 return ret; 763 } 764 765 #else /* !CONFIG_SUSPEND */ 766 767 #define platform_pm_suspend NULL 768 #define platform_pm_resume NULL 769 #define platform_pm_suspend_noirq NULL 770 #define platform_pm_resume_noirq NULL 771 772 #endif /* !CONFIG_SUSPEND */ 773 774 #ifdef CONFIG_HIBERNATION 775 776 static int platform_pm_freeze(struct device *dev) 777 { 778 struct device_driver *drv = dev->driver; 779 int ret = 0; 780 781 if (!drv) 782 return 0; 783 784 if (drv->pm) { 785 if (drv->pm->freeze) 786 ret = drv->pm->freeze(dev); 787 } else { 788 ret = platform_legacy_suspend(dev, PMSG_FREEZE); 789 } 790 791 return ret; 792 } 793 794 static int platform_pm_freeze_noirq(struct device *dev) 795 { 796 struct device_driver *drv = dev->driver; 797 int ret = 0; 798 799 if (!drv) 800 return 0; 801 802 if (drv->pm) { 803 if (drv->pm->freeze_noirq) 804 ret = drv->pm->freeze_noirq(dev); 805 } 806 807 return ret; 808 } 809 810 static int platform_pm_thaw(struct device *dev) 811 { 812 struct device_driver *drv = dev->driver; 813 int ret = 0; 814 815 if (!drv) 816 return 0; 817 818 if (drv->pm) { 819 if (drv->pm->thaw) 820 ret = drv->pm->thaw(dev); 821 } else { 822 ret = platform_legacy_resume(dev); 823 } 824 825 return ret; 826 } 827 828 static int platform_pm_thaw_noirq(struct device *dev) 829 { 830 struct device_driver *drv = dev->driver; 831 int ret = 0; 832 833 if (!drv) 834 return 0; 835 836 if (drv->pm) { 837 if (drv->pm->thaw_noirq) 838 ret = drv->pm->thaw_noirq(dev); 839 } 840 841 return ret; 842 } 843 844 static int platform_pm_poweroff(struct device *dev) 845 { 846 struct device_driver *drv = dev->driver; 847 int ret = 0; 848 849 if (!drv) 850 return 0; 851 852 if (drv->pm) { 853 if (drv->pm->poweroff) 854 ret = drv->pm->poweroff(dev); 855 } else { 856 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE); 857 } 858 859 return ret; 860 } 861 862 static int platform_pm_poweroff_noirq(struct device *dev) 863 { 864 struct device_driver *drv = dev->driver; 865 int ret = 0; 866 867 if (!drv) 868 return 0; 869 870 if (drv->pm) { 871 if (drv->pm->poweroff_noirq) 872 ret = drv->pm->poweroff_noirq(dev); 873 } 874 875 return ret; 876 } 877 878 static int platform_pm_restore(struct device *dev) 879 { 880 struct device_driver *drv = dev->driver; 881 int ret = 0; 882 883 if (!drv) 884 return 0; 885 886 if (drv->pm) { 887 if (drv->pm->restore) 888 ret = drv->pm->restore(dev); 889 } else { 890 ret = platform_legacy_resume(dev); 891 } 892 893 return ret; 894 } 895 896 static int platform_pm_restore_noirq(struct device *dev) 897 { 898 struct device_driver *drv = dev->driver; 899 int ret = 0; 900 901 if (!drv) 902 return 0; 903 904 if (drv->pm) { 905 if (drv->pm->restore_noirq) 906 ret = drv->pm->restore_noirq(dev); 907 } 908 909 return ret; 910 } 911 912 #else /* !CONFIG_HIBERNATION */ 913 914 #define platform_pm_freeze NULL 915 #define platform_pm_thaw NULL 916 #define platform_pm_poweroff NULL 917 #define platform_pm_restore NULL 918 #define platform_pm_freeze_noirq NULL 919 #define platform_pm_thaw_noirq NULL 920 #define platform_pm_poweroff_noirq NULL 921 #define platform_pm_restore_noirq NULL 922 923 #endif /* !CONFIG_HIBERNATION */ 924 925 #ifdef CONFIG_PM_RUNTIME 926 927 int __weak platform_pm_runtime_suspend(struct device *dev) 928 { 929 return pm_generic_runtime_suspend(dev); 930 }; 931 932 int __weak platform_pm_runtime_resume(struct device *dev) 933 { 934 return pm_generic_runtime_resume(dev); 935 }; 936 937 int __weak platform_pm_runtime_idle(struct device *dev) 938 { 939 return pm_generic_runtime_idle(dev); 940 }; 941 942 #else /* !CONFIG_PM_RUNTIME */ 943 944 #define platform_pm_runtime_suspend NULL 945 #define platform_pm_runtime_resume NULL 946 #define platform_pm_runtime_idle NULL 947 948 #endif /* !CONFIG_PM_RUNTIME */ 949 950 static const struct dev_pm_ops platform_dev_pm_ops = { 951 .prepare = platform_pm_prepare, 952 .complete = platform_pm_complete, 953 .suspend = platform_pm_suspend, 954 .resume = platform_pm_resume, 955 .freeze = platform_pm_freeze, 956 .thaw = platform_pm_thaw, 957 .poweroff = platform_pm_poweroff, 958 .restore = platform_pm_restore, 959 .suspend_noirq = platform_pm_suspend_noirq, 960 .resume_noirq = platform_pm_resume_noirq, 961 .freeze_noirq = platform_pm_freeze_noirq, 962 .thaw_noirq = platform_pm_thaw_noirq, 963 .poweroff_noirq = platform_pm_poweroff_noirq, 964 .restore_noirq = platform_pm_restore_noirq, 965 .runtime_suspend = platform_pm_runtime_suspend, 966 .runtime_resume = platform_pm_runtime_resume, 967 .runtime_idle = platform_pm_runtime_idle, 968 }; 969 970 struct bus_type platform_bus_type = { 971 .name = "platform", 972 .dev_attrs = platform_dev_attrs, 973 .match = platform_match, 974 .uevent = platform_uevent, 975 .pm = &platform_dev_pm_ops, 976 }; 977 EXPORT_SYMBOL_GPL(platform_bus_type); 978 979 int __init platform_bus_init(void) 980 { 981 int error; 982 983 early_platform_cleanup(); 984 985 error = device_register(&platform_bus); 986 if (error) 987 return error; 988 error = bus_register(&platform_bus_type); 989 if (error) 990 device_unregister(&platform_bus); 991 return error; 992 } 993 994 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK 995 u64 dma_get_required_mask(struct device *dev) 996 { 997 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); 998 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); 999 u64 mask; 1000 1001 if (!high_totalram) { 1002 /* convert to mask just covering totalram */ 1003 low_totalram = (1 << (fls(low_totalram) - 1)); 1004 low_totalram += low_totalram - 1; 1005 mask = low_totalram; 1006 } else { 1007 high_totalram = (1 << (fls(high_totalram) - 1)); 1008 high_totalram += high_totalram - 1; 1009 mask = (((u64)high_totalram) << 32) + 0xffffffff; 1010 } 1011 return mask; 1012 } 1013 EXPORT_SYMBOL_GPL(dma_get_required_mask); 1014 #endif 1015 1016 static __initdata LIST_HEAD(early_platform_driver_list); 1017 static __initdata LIST_HEAD(early_platform_device_list); 1018 1019 /** 1020 * early_platform_driver_register - register early platform driver 1021 * @epdrv: early_platform driver structure 1022 * @buf: string passed from early_param() 1023 * 1024 * Helper function for early_platform_init() / early_platform_init_buffer() 1025 */ 1026 int __init early_platform_driver_register(struct early_platform_driver *epdrv, 1027 char *buf) 1028 { 1029 char *tmp; 1030 int n; 1031 1032 /* Simply add the driver to the end of the global list. 1033 * Drivers will by default be put on the list in compiled-in order. 1034 */ 1035 if (!epdrv->list.next) { 1036 INIT_LIST_HEAD(&epdrv->list); 1037 list_add_tail(&epdrv->list, &early_platform_driver_list); 1038 } 1039 1040 /* If the user has specified device then make sure the driver 1041 * gets prioritized. The driver of the last device specified on 1042 * command line will be put first on the list. 1043 */ 1044 n = strlen(epdrv->pdrv->driver.name); 1045 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) { 1046 list_move(&epdrv->list, &early_platform_driver_list); 1047 1048 /* Allow passing parameters after device name */ 1049 if (buf[n] == '\0' || buf[n] == ',') 1050 epdrv->requested_id = -1; 1051 else { 1052 epdrv->requested_id = simple_strtoul(&buf[n + 1], 1053 &tmp, 10); 1054 1055 if (buf[n] != '.' || (tmp == &buf[n + 1])) { 1056 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR; 1057 n = 0; 1058 } else 1059 n += strcspn(&buf[n + 1], ",") + 1; 1060 } 1061 1062 if (buf[n] == ',') 1063 n++; 1064 1065 if (epdrv->bufsize) { 1066 memcpy(epdrv->buffer, &buf[n], 1067 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1)); 1068 epdrv->buffer[epdrv->bufsize - 1] = '\0'; 1069 } 1070 } 1071 1072 return 0; 1073 } 1074 1075 /** 1076 * early_platform_add_devices - adds a number of early platform devices 1077 * @devs: array of early platform devices to add 1078 * @num: number of early platform devices in array 1079 * 1080 * Used by early architecture code to register early platform devices and 1081 * their platform data. 1082 */ 1083 void __init early_platform_add_devices(struct platform_device **devs, int num) 1084 { 1085 struct device *dev; 1086 int i; 1087 1088 /* simply add the devices to list */ 1089 for (i = 0; i < num; i++) { 1090 dev = &devs[i]->dev; 1091 1092 if (!dev->devres_head.next) { 1093 INIT_LIST_HEAD(&dev->devres_head); 1094 list_add_tail(&dev->devres_head, 1095 &early_platform_device_list); 1096 } 1097 } 1098 } 1099 1100 /** 1101 * early_platform_driver_register_all - register early platform drivers 1102 * @class_str: string to identify early platform driver class 1103 * 1104 * Used by architecture code to register all early platform drivers 1105 * for a certain class. If omitted then only early platform drivers 1106 * with matching kernel command line class parameters will be registered. 1107 */ 1108 void __init early_platform_driver_register_all(char *class_str) 1109 { 1110 /* The "class_str" parameter may or may not be present on the kernel 1111 * command line. If it is present then there may be more than one 1112 * matching parameter. 1113 * 1114 * Since we register our early platform drivers using early_param() 1115 * we need to make sure that they also get registered in the case 1116 * when the parameter is missing from the kernel command line. 1117 * 1118 * We use parse_early_options() to make sure the early_param() gets 1119 * called at least once. The early_param() may be called more than 1120 * once since the name of the preferred device may be specified on 1121 * the kernel command line. early_platform_driver_register() handles 1122 * this case for us. 1123 */ 1124 parse_early_options(class_str); 1125 } 1126 1127 /** 1128 * early_platform_match - find early platform device matching driver 1129 * @epdrv: early platform driver structure 1130 * @id: id to match against 1131 */ 1132 static __init struct platform_device * 1133 early_platform_match(struct early_platform_driver *epdrv, int id) 1134 { 1135 struct platform_device *pd; 1136 1137 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) 1138 if (platform_match(&pd->dev, &epdrv->pdrv->driver)) 1139 if (pd->id == id) 1140 return pd; 1141 1142 return NULL; 1143 } 1144 1145 /** 1146 * early_platform_left - check if early platform driver has matching devices 1147 * @epdrv: early platform driver structure 1148 * @id: return true if id or above exists 1149 */ 1150 static __init int early_platform_left(struct early_platform_driver *epdrv, 1151 int id) 1152 { 1153 struct platform_device *pd; 1154 1155 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) 1156 if (platform_match(&pd->dev, &epdrv->pdrv->driver)) 1157 if (pd->id >= id) 1158 return 1; 1159 1160 return 0; 1161 } 1162 1163 /** 1164 * early_platform_driver_probe_id - probe drivers matching class_str and id 1165 * @class_str: string to identify early platform driver class 1166 * @id: id to match against 1167 * @nr_probe: number of platform devices to successfully probe before exiting 1168 */ 1169 static int __init early_platform_driver_probe_id(char *class_str, 1170 int id, 1171 int nr_probe) 1172 { 1173 struct early_platform_driver *epdrv; 1174 struct platform_device *match; 1175 int match_id; 1176 int n = 0; 1177 int left = 0; 1178 1179 list_for_each_entry(epdrv, &early_platform_driver_list, list) { 1180 /* only use drivers matching our class_str */ 1181 if (strcmp(class_str, epdrv->class_str)) 1182 continue; 1183 1184 if (id == -2) { 1185 match_id = epdrv->requested_id; 1186 left = 1; 1187 1188 } else { 1189 match_id = id; 1190 left += early_platform_left(epdrv, id); 1191 1192 /* skip requested id */ 1193 switch (epdrv->requested_id) { 1194 case EARLY_PLATFORM_ID_ERROR: 1195 case EARLY_PLATFORM_ID_UNSET: 1196 break; 1197 default: 1198 if (epdrv->requested_id == id) 1199 match_id = EARLY_PLATFORM_ID_UNSET; 1200 } 1201 } 1202 1203 switch (match_id) { 1204 case EARLY_PLATFORM_ID_ERROR: 1205 pr_warning("%s: unable to parse %s parameter\n", 1206 class_str, epdrv->pdrv->driver.name); 1207 /* fall-through */ 1208 case EARLY_PLATFORM_ID_UNSET: 1209 match = NULL; 1210 break; 1211 default: 1212 match = early_platform_match(epdrv, match_id); 1213 } 1214 1215 if (match) { 1216 /* 1217 * Set up a sensible init_name to enable 1218 * dev_name() and others to be used before the 1219 * rest of the driver core is initialized. 1220 */ 1221 if (!match->dev.init_name && slab_is_available()) { 1222 if (match->id != -1) 1223 match->dev.init_name = 1224 kasprintf(GFP_KERNEL, "%s.%d", 1225 match->name, 1226 match->id); 1227 else 1228 match->dev.init_name = 1229 kasprintf(GFP_KERNEL, "%s", 1230 match->name); 1231 1232 if (!match->dev.init_name) 1233 return -ENOMEM; 1234 } 1235 1236 if (epdrv->pdrv->probe(match)) 1237 pr_warning("%s: unable to probe %s early.\n", 1238 class_str, match->name); 1239 else 1240 n++; 1241 } 1242 1243 if (n >= nr_probe) 1244 break; 1245 } 1246 1247 if (left) 1248 return n; 1249 else 1250 return -ENODEV; 1251 } 1252 1253 /** 1254 * early_platform_driver_probe - probe a class of registered drivers 1255 * @class_str: string to identify early platform driver class 1256 * @nr_probe: number of platform devices to successfully probe before exiting 1257 * @user_only: only probe user specified early platform devices 1258 * 1259 * Used by architecture code to probe registered early platform drivers 1260 * within a certain class. For probe to happen a registered early platform 1261 * device matching a registered early platform driver is needed. 1262 */ 1263 int __init early_platform_driver_probe(char *class_str, 1264 int nr_probe, 1265 int user_only) 1266 { 1267 int k, n, i; 1268 1269 n = 0; 1270 for (i = -2; n < nr_probe; i++) { 1271 k = early_platform_driver_probe_id(class_str, i, nr_probe - n); 1272 1273 if (k < 0) 1274 break; 1275 1276 n += k; 1277 1278 if (user_only) 1279 break; 1280 } 1281 1282 return n; 1283 } 1284 1285 /** 1286 * early_platform_cleanup - clean up early platform code 1287 */ 1288 void __init early_platform_cleanup(void) 1289 { 1290 struct platform_device *pd, *pd2; 1291 1292 /* clean up the devres list used to chain devices */ 1293 list_for_each_entry_safe(pd, pd2, &early_platform_device_list, 1294 dev.devres_head) { 1295 list_del(&pd->dev.devres_head); 1296 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head)); 1297 } 1298 } 1299 1300