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