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