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