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