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