This source file includes following definitions.
- platform_get_resource
- devm_platform_ioremap_resource
- __platform_get_irq
- platform_get_irq
- platform_get_irq_optional
- platform_irq_count
- platform_get_resource_byname
- __platform_get_irq_byname
- platform_get_irq_byname
- platform_get_irq_byname_optional
- platform_add_devices
- setup_pdev_dma_masks
- platform_device_put
- platform_device_release
- platform_device_alloc
- platform_device_add_resources
- platform_device_add_data
- platform_device_add_properties
- platform_device_add
- platform_device_del
- platform_device_register
- platform_device_unregister
- platform_device_register_full
- platform_drv_probe
- platform_drv_probe_fail
- platform_drv_remove
- platform_drv_shutdown
- __platform_driver_register
- platform_driver_unregister
- __platform_driver_probe
- __platform_create_bundle
- __platform_register_drivers
- platform_unregister_drivers
- modalias_show
- driver_override_store
- driver_override_show
- platform_uevent
- platform_match_id
- platform_match
- platform_legacy_suspend
- platform_legacy_resume
- platform_pm_suspend
- platform_pm_resume
- platform_pm_freeze
- platform_pm_thaw
- platform_pm_poweroff
- platform_pm_restore
- platform_dma_configure
- __platform_match
- platform_find_device_by_driver
- platform_bus_init
- early_platform_driver_register
- early_platform_add_devices
- early_platform_driver_register_all
- early_platform_match
- early_platform_left
- early_platform_driver_probe_id
- early_platform_driver_probe
- early_platform_cleanup
1
2
3
4
5
6
7
8
9
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
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
45
46
47
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
65
66
67
68
69
70
71
72 #ifdef CONFIG_HAS_IOMEM
73 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
74 unsigned int index)
75 {
76 struct resource *res;
77
78 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
79 return devm_ioremap_resource(&pdev->dev, res);
80 }
81 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
82 #endif
83
84 static int __platform_get_irq(struct platform_device *dev, unsigned int num)
85 {
86 #ifdef CONFIG_SPARC
87
88 if (!dev || num >= dev->archdata.num_irqs)
89 return -ENXIO;
90 return dev->archdata.irqs[num];
91 #else
92 struct resource *r;
93 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
94 int ret;
95
96 ret = of_irq_get(dev->dev.of_node, num);
97 if (ret > 0 || ret == -EPROBE_DEFER)
98 return ret;
99 }
100
101 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
102 if (has_acpi_companion(&dev->dev)) {
103 if (r && r->flags & IORESOURCE_DISABLED) {
104 int ret;
105
106 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
107 if (ret)
108 return ret;
109 }
110 }
111
112
113
114
115
116
117
118 if (r && r->flags & IORESOURCE_BITS) {
119 struct irq_data *irqd;
120
121 irqd = irq_get_irq_data(r->start);
122 if (!irqd)
123 return -ENXIO;
124 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
125 }
126
127 if (r)
128 return r->start;
129
130
131
132
133
134
135
136
137 if (num == 0 && has_acpi_companion(&dev->dev)) {
138 int ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
139
140
141 if (ret >= 0 || ret == -EPROBE_DEFER)
142 return ret;
143 }
144
145 return -ENXIO;
146 #endif
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 int platform_get_irq(struct platform_device *dev, unsigned int num)
166 {
167 int ret;
168
169 ret = __platform_get_irq(dev, num);
170 if (ret < 0 && ret != -EPROBE_DEFER)
171 dev_err(&dev->dev, "IRQ index %u not found\n", num);
172
173 return ret;
174 }
175 EXPORT_SYMBOL_GPL(platform_get_irq);
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
195 {
196 return __platform_get_irq(dev, num);
197 }
198 EXPORT_SYMBOL_GPL(platform_get_irq_optional);
199
200
201
202
203
204
205
206 int platform_irq_count(struct platform_device *dev)
207 {
208 int ret, nr = 0;
209
210 while ((ret = __platform_get_irq(dev, nr)) >= 0)
211 nr++;
212
213 if (ret == -EPROBE_DEFER)
214 return ret;
215
216 return nr;
217 }
218 EXPORT_SYMBOL_GPL(platform_irq_count);
219
220
221
222
223
224
225
226 struct resource *platform_get_resource_byname(struct platform_device *dev,
227 unsigned int type,
228 const char *name)
229 {
230 u32 i;
231
232 for (i = 0; i < dev->num_resources; i++) {
233 struct resource *r = &dev->resource[i];
234
235 if (unlikely(!r->name))
236 continue;
237
238 if (type == resource_type(r) && !strcmp(r->name, name))
239 return r;
240 }
241 return NULL;
242 }
243 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
244
245 static int __platform_get_irq_byname(struct platform_device *dev,
246 const char *name)
247 {
248 struct resource *r;
249
250 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
251 int ret;
252
253 ret = of_irq_get_byname(dev->dev.of_node, name);
254 if (ret > 0 || ret == -EPROBE_DEFER)
255 return ret;
256 }
257
258 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
259 if (r)
260 return r->start;
261
262 return -ENXIO;
263 }
264
265
266
267
268
269
270
271
272
273
274 int platform_get_irq_byname(struct platform_device *dev, const char *name)
275 {
276 int ret;
277
278 ret = __platform_get_irq_byname(dev, name);
279 if (ret < 0 && ret != -EPROBE_DEFER)
280 dev_err(&dev->dev, "IRQ %s not found\n", name);
281
282 return ret;
283 }
284 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
285
286
287
288
289
290
291
292
293
294
295
296 int platform_get_irq_byname_optional(struct platform_device *dev,
297 const char *name)
298 {
299 return __platform_get_irq_byname(dev, name);
300 }
301 EXPORT_SYMBOL_GPL(platform_get_irq_byname_optional);
302
303
304
305
306
307
308 int platform_add_devices(struct platform_device **devs, int num)
309 {
310 int i, ret = 0;
311
312 for (i = 0; i < num; i++) {
313 ret = platform_device_register(devs[i]);
314 if (ret) {
315 while (--i >= 0)
316 platform_device_unregister(devs[i]);
317 break;
318 }
319 }
320
321 return ret;
322 }
323 EXPORT_SYMBOL_GPL(platform_add_devices);
324
325 struct platform_object {
326 struct platform_device pdev;
327 char name[];
328 };
329
330
331
332
333
334 static void setup_pdev_dma_masks(struct platform_device *pdev)
335 {
336 if (!pdev->dev.coherent_dma_mask)
337 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
338 if (!pdev->dev.dma_mask) {
339 pdev->platform_dma_mask = DMA_BIT_MASK(32);
340 pdev->dev.dma_mask = &pdev->platform_dma_mask;
341 }
342 };
343
344
345
346
347
348
349
350
351 void platform_device_put(struct platform_device *pdev)
352 {
353 if (!IS_ERR_OR_NULL(pdev))
354 put_device(&pdev->dev);
355 }
356 EXPORT_SYMBOL_GPL(platform_device_put);
357
358 static void platform_device_release(struct device *dev)
359 {
360 struct platform_object *pa = container_of(dev, struct platform_object,
361 pdev.dev);
362
363 of_device_node_put(&pa->pdev.dev);
364 kfree(pa->pdev.dev.platform_data);
365 kfree(pa->pdev.mfd_cell);
366 kfree(pa->pdev.resource);
367 kfree(pa->pdev.driver_override);
368 kfree(pa);
369 }
370
371
372
373
374
375
376
377
378
379 struct platform_device *platform_device_alloc(const char *name, int id)
380 {
381 struct platform_object *pa;
382
383 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
384 if (pa) {
385 strcpy(pa->name, name);
386 pa->pdev.name = pa->name;
387 pa->pdev.id = id;
388 device_initialize(&pa->pdev.dev);
389 pa->pdev.dev.release = platform_device_release;
390 setup_pdev_dma_masks(&pa->pdev);
391 }
392
393 return pa ? &pa->pdev : NULL;
394 }
395 EXPORT_SYMBOL_GPL(platform_device_alloc);
396
397
398
399
400
401
402
403
404
405
406
407 int platform_device_add_resources(struct platform_device *pdev,
408 const struct resource *res, unsigned int num)
409 {
410 struct resource *r = NULL;
411
412 if (res) {
413 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
414 if (!r)
415 return -ENOMEM;
416 }
417
418 kfree(pdev->resource);
419 pdev->resource = r;
420 pdev->num_resources = num;
421 return 0;
422 }
423 EXPORT_SYMBOL_GPL(platform_device_add_resources);
424
425
426
427
428
429
430
431
432
433
434
435 int platform_device_add_data(struct platform_device *pdev, const void *data,
436 size_t size)
437 {
438 void *d = NULL;
439
440 if (data) {
441 d = kmemdup(data, size, GFP_KERNEL);
442 if (!d)
443 return -ENOMEM;
444 }
445
446 kfree(pdev->dev.platform_data);
447 pdev->dev.platform_data = d;
448 return 0;
449 }
450 EXPORT_SYMBOL_GPL(platform_device_add_data);
451
452
453
454
455
456
457
458
459
460
461 int platform_device_add_properties(struct platform_device *pdev,
462 const struct property_entry *properties)
463 {
464 return device_add_properties(&pdev->dev, properties);
465 }
466 EXPORT_SYMBOL_GPL(platform_device_add_properties);
467
468
469
470
471
472
473
474
475 int platform_device_add(struct platform_device *pdev)
476 {
477 u32 i;
478 int ret;
479
480 if (!pdev)
481 return -EINVAL;
482
483 if (!pdev->dev.parent)
484 pdev->dev.parent = &platform_bus;
485
486 pdev->dev.bus = &platform_bus_type;
487
488 switch (pdev->id) {
489 default:
490 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
491 break;
492 case PLATFORM_DEVID_NONE:
493 dev_set_name(&pdev->dev, "%s", pdev->name);
494 break;
495 case PLATFORM_DEVID_AUTO:
496
497
498
499
500
501 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
502 if (ret < 0)
503 goto err_out;
504 pdev->id = ret;
505 pdev->id_auto = true;
506 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
507 break;
508 }
509
510 for (i = 0; i < pdev->num_resources; i++) {
511 struct resource *p, *r = &pdev->resource[i];
512
513 if (r->name == NULL)
514 r->name = dev_name(&pdev->dev);
515
516 p = r->parent;
517 if (!p) {
518 if (resource_type(r) == IORESOURCE_MEM)
519 p = &iomem_resource;
520 else if (resource_type(r) == IORESOURCE_IO)
521 p = &ioport_resource;
522 }
523
524 if (p) {
525 ret = insert_resource(p, r);
526 if (ret) {
527 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
528 goto failed;
529 }
530 }
531 }
532
533 pr_debug("Registering platform device '%s'. Parent at %s\n",
534 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
535
536 ret = device_add(&pdev->dev);
537 if (ret == 0)
538 return ret;
539
540 failed:
541 if (pdev->id_auto) {
542 ida_simple_remove(&platform_devid_ida, pdev->id);
543 pdev->id = PLATFORM_DEVID_AUTO;
544 }
545
546 while (i--) {
547 struct resource *r = &pdev->resource[i];
548 if (r->parent)
549 release_resource(r);
550 }
551
552 err_out:
553 return ret;
554 }
555 EXPORT_SYMBOL_GPL(platform_device_add);
556
557
558
559
560
561
562
563
564
565 void platform_device_del(struct platform_device *pdev)
566 {
567 u32 i;
568
569 if (!IS_ERR_OR_NULL(pdev)) {
570 device_del(&pdev->dev);
571
572 if (pdev->id_auto) {
573 ida_simple_remove(&platform_devid_ida, pdev->id);
574 pdev->id = PLATFORM_DEVID_AUTO;
575 }
576
577 for (i = 0; i < pdev->num_resources; i++) {
578 struct resource *r = &pdev->resource[i];
579 if (r->parent)
580 release_resource(r);
581 }
582 }
583 }
584 EXPORT_SYMBOL_GPL(platform_device_del);
585
586
587
588
589
590 int platform_device_register(struct platform_device *pdev)
591 {
592 device_initialize(&pdev->dev);
593 setup_pdev_dma_masks(pdev);
594 return platform_device_add(pdev);
595 }
596 EXPORT_SYMBOL_GPL(platform_device_register);
597
598
599
600
601
602
603
604
605
606 void platform_device_unregister(struct platform_device *pdev)
607 {
608 platform_device_del(pdev);
609 platform_device_put(pdev);
610 }
611 EXPORT_SYMBOL_GPL(platform_device_unregister);
612
613
614
615
616
617
618
619
620
621 struct platform_device *platform_device_register_full(
622 const struct platform_device_info *pdevinfo)
623 {
624 int ret = -ENOMEM;
625 struct platform_device *pdev;
626
627 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
628 if (!pdev)
629 return ERR_PTR(-ENOMEM);
630
631 pdev->dev.parent = pdevinfo->parent;
632 pdev->dev.fwnode = pdevinfo->fwnode;
633 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
634 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
635
636 if (pdevinfo->dma_mask) {
637 pdev->platform_dma_mask = pdevinfo->dma_mask;
638 pdev->dev.dma_mask = &pdev->platform_dma_mask;
639 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
640 }
641
642 ret = platform_device_add_resources(pdev,
643 pdevinfo->res, pdevinfo->num_res);
644 if (ret)
645 goto err;
646
647 ret = platform_device_add_data(pdev,
648 pdevinfo->data, pdevinfo->size_data);
649 if (ret)
650 goto err;
651
652 if (pdevinfo->properties) {
653 ret = platform_device_add_properties(pdev,
654 pdevinfo->properties);
655 if (ret)
656 goto err;
657 }
658
659 ret = platform_device_add(pdev);
660 if (ret) {
661 err:
662 ACPI_COMPANION_SET(&pdev->dev, NULL);
663 platform_device_put(pdev);
664 return ERR_PTR(ret);
665 }
666
667 return pdev;
668 }
669 EXPORT_SYMBOL_GPL(platform_device_register_full);
670
671 static int platform_drv_probe(struct device *_dev)
672 {
673 struct platform_driver *drv = to_platform_driver(_dev->driver);
674 struct platform_device *dev = to_platform_device(_dev);
675 int ret;
676
677 ret = of_clk_set_defaults(_dev->of_node, false);
678 if (ret < 0)
679 return ret;
680
681 ret = dev_pm_domain_attach(_dev, true);
682 if (ret)
683 goto out;
684
685 if (drv->probe) {
686 ret = drv->probe(dev);
687 if (ret)
688 dev_pm_domain_detach(_dev, true);
689 }
690
691 out:
692 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
693 dev_warn(_dev, "probe deferral not supported\n");
694 ret = -ENXIO;
695 }
696
697 return ret;
698 }
699
700 static int platform_drv_probe_fail(struct device *_dev)
701 {
702 return -ENXIO;
703 }
704
705 static int platform_drv_remove(struct device *_dev)
706 {
707 struct platform_driver *drv = to_platform_driver(_dev->driver);
708 struct platform_device *dev = to_platform_device(_dev);
709 int ret = 0;
710
711 if (drv->remove)
712 ret = drv->remove(dev);
713 dev_pm_domain_detach(_dev, true);
714
715 return ret;
716 }
717
718 static void platform_drv_shutdown(struct device *_dev)
719 {
720 struct platform_driver *drv = to_platform_driver(_dev->driver);
721 struct platform_device *dev = to_platform_device(_dev);
722
723 if (drv->shutdown)
724 drv->shutdown(dev);
725 }
726
727
728
729
730
731
732 int __platform_driver_register(struct platform_driver *drv,
733 struct module *owner)
734 {
735 drv->driver.owner = owner;
736 drv->driver.bus = &platform_bus_type;
737 drv->driver.probe = platform_drv_probe;
738 drv->driver.remove = platform_drv_remove;
739 drv->driver.shutdown = platform_drv_shutdown;
740
741 return driver_register(&drv->driver);
742 }
743 EXPORT_SYMBOL_GPL(__platform_driver_register);
744
745
746
747
748
749 void platform_driver_unregister(struct platform_driver *drv)
750 {
751 driver_unregister(&drv->driver);
752 }
753 EXPORT_SYMBOL_GPL(platform_driver_unregister);
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
776 int (*probe)(struct platform_device *), struct module *module)
777 {
778 int retval, code;
779
780 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
781 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
782 drv->driver.name, __func__);
783 return -EINVAL;
784 }
785
786
787
788
789
790
791 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
792
793
794
795
796
797 drv->prevent_deferred_probe = true;
798
799
800 drv->driver.suppress_bind_attrs = true;
801
802
803 drv->probe = probe;
804 retval = code = __platform_driver_register(drv, module);
805
806
807
808
809
810
811
812 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
813 drv->probe = NULL;
814 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
815 retval = -ENODEV;
816 drv->driver.probe = platform_drv_probe_fail;
817 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
818
819 if (code != retval)
820 platform_driver_unregister(drv);
821 return retval;
822 }
823 EXPORT_SYMBOL_GPL(__platform_driver_probe);
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840 struct platform_device * __init_or_module __platform_create_bundle(
841 struct platform_driver *driver,
842 int (*probe)(struct platform_device *),
843 struct resource *res, unsigned int n_res,
844 const void *data, size_t size, struct module *module)
845 {
846 struct platform_device *pdev;
847 int error;
848
849 pdev = platform_device_alloc(driver->driver.name, -1);
850 if (!pdev) {
851 error = -ENOMEM;
852 goto err_out;
853 }
854
855 error = platform_device_add_resources(pdev, res, n_res);
856 if (error)
857 goto err_pdev_put;
858
859 error = platform_device_add_data(pdev, data, size);
860 if (error)
861 goto err_pdev_put;
862
863 error = platform_device_add(pdev);
864 if (error)
865 goto err_pdev_put;
866
867 error = __platform_driver_probe(driver, probe, module);
868 if (error)
869 goto err_pdev_del;
870
871 return pdev;
872
873 err_pdev_del:
874 platform_device_del(pdev);
875 err_pdev_put:
876 platform_device_put(pdev);
877 err_out:
878 return ERR_PTR(error);
879 }
880 EXPORT_SYMBOL_GPL(__platform_create_bundle);
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895 int __platform_register_drivers(struct platform_driver * const *drivers,
896 unsigned int count, struct module *owner)
897 {
898 unsigned int i;
899 int err;
900
901 for (i = 0; i < count; i++) {
902 pr_debug("registering platform driver %ps\n", drivers[i]);
903
904 err = __platform_driver_register(drivers[i], owner);
905 if (err < 0) {
906 pr_err("failed to register platform driver %ps: %d\n",
907 drivers[i], err);
908 goto error;
909 }
910 }
911
912 return 0;
913
914 error:
915 while (i--) {
916 pr_debug("unregistering platform driver %ps\n", drivers[i]);
917 platform_driver_unregister(drivers[i]);
918 }
919
920 return err;
921 }
922 EXPORT_SYMBOL_GPL(__platform_register_drivers);
923
924
925
926
927
928
929
930
931
932
933 void platform_unregister_drivers(struct platform_driver * const *drivers,
934 unsigned int count)
935 {
936 while (count--) {
937 pr_debug("unregistering platform driver %ps\n", drivers[count]);
938 platform_driver_unregister(drivers[count]);
939 }
940 }
941 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
942
943
944
945
946
947
948
949 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
950 char *buf)
951 {
952 struct platform_device *pdev = to_platform_device(dev);
953 int len;
954
955 len = of_device_modalias(dev, buf, PAGE_SIZE);
956 if (len != -ENODEV)
957 return len;
958
959 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
960 if (len != -ENODEV)
961 return len;
962
963 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
964
965 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
966 }
967 static DEVICE_ATTR_RO(modalias);
968
969 static ssize_t driver_override_store(struct device *dev,
970 struct device_attribute *attr,
971 const char *buf, size_t count)
972 {
973 struct platform_device *pdev = to_platform_device(dev);
974 char *driver_override, *old, *cp;
975
976
977 if (count >= (PAGE_SIZE - 1))
978 return -EINVAL;
979
980 driver_override = kstrndup(buf, count, GFP_KERNEL);
981 if (!driver_override)
982 return -ENOMEM;
983
984 cp = strchr(driver_override, '\n');
985 if (cp)
986 *cp = '\0';
987
988 device_lock(dev);
989 old = pdev->driver_override;
990 if (strlen(driver_override)) {
991 pdev->driver_override = driver_override;
992 } else {
993 kfree(driver_override);
994 pdev->driver_override = NULL;
995 }
996 device_unlock(dev);
997
998 kfree(old);
999
1000 return count;
1001 }
1002
1003 static ssize_t driver_override_show(struct device *dev,
1004 struct device_attribute *attr, char *buf)
1005 {
1006 struct platform_device *pdev = to_platform_device(dev);
1007 ssize_t len;
1008
1009 device_lock(dev);
1010 len = sprintf(buf, "%s\n", pdev->driver_override);
1011 device_unlock(dev);
1012 return len;
1013 }
1014 static DEVICE_ATTR_RW(driver_override);
1015
1016
1017 static struct attribute *platform_dev_attrs[] = {
1018 &dev_attr_modalias.attr,
1019 &dev_attr_driver_override.attr,
1020 NULL,
1021 };
1022 ATTRIBUTE_GROUPS(platform_dev);
1023
1024 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
1025 {
1026 struct platform_device *pdev = to_platform_device(dev);
1027 int rc;
1028
1029
1030 rc = of_device_uevent_modalias(dev, env);
1031 if (rc != -ENODEV)
1032 return rc;
1033
1034 rc = acpi_device_uevent_modalias(dev, env);
1035 if (rc != -ENODEV)
1036 return rc;
1037
1038 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1039 pdev->name);
1040 return 0;
1041 }
1042
1043 static const struct platform_device_id *platform_match_id(
1044 const struct platform_device_id *id,
1045 struct platform_device *pdev)
1046 {
1047 while (id->name[0]) {
1048 if (strcmp(pdev->name, id->name) == 0) {
1049 pdev->id_entry = id;
1050 return id;
1051 }
1052 id++;
1053 }
1054 return NULL;
1055 }
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070 static int platform_match(struct device *dev, struct device_driver *drv)
1071 {
1072 struct platform_device *pdev = to_platform_device(dev);
1073 struct platform_driver *pdrv = to_platform_driver(drv);
1074
1075
1076 if (pdev->driver_override)
1077 return !strcmp(pdev->driver_override, drv->name);
1078
1079
1080 if (of_driver_match_device(dev, drv))
1081 return 1;
1082
1083
1084 if (acpi_driver_match_device(dev, drv))
1085 return 1;
1086
1087
1088 if (pdrv->id_table)
1089 return platform_match_id(pdrv->id_table, pdev) != NULL;
1090
1091
1092 return (strcmp(pdev->name, drv->name) == 0);
1093 }
1094
1095 #ifdef CONFIG_PM_SLEEP
1096
1097 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1098 {
1099 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1100 struct platform_device *pdev = to_platform_device(dev);
1101 int ret = 0;
1102
1103 if (dev->driver && pdrv->suspend)
1104 ret = pdrv->suspend(pdev, mesg);
1105
1106 return ret;
1107 }
1108
1109 static int platform_legacy_resume(struct device *dev)
1110 {
1111 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1112 struct platform_device *pdev = to_platform_device(dev);
1113 int ret = 0;
1114
1115 if (dev->driver && pdrv->resume)
1116 ret = pdrv->resume(pdev);
1117
1118 return ret;
1119 }
1120
1121 #endif
1122
1123 #ifdef CONFIG_SUSPEND
1124
1125 int platform_pm_suspend(struct device *dev)
1126 {
1127 struct device_driver *drv = dev->driver;
1128 int ret = 0;
1129
1130 if (!drv)
1131 return 0;
1132
1133 if (drv->pm) {
1134 if (drv->pm->suspend)
1135 ret = drv->pm->suspend(dev);
1136 } else {
1137 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1138 }
1139
1140 return ret;
1141 }
1142
1143 int platform_pm_resume(struct device *dev)
1144 {
1145 struct device_driver *drv = dev->driver;
1146 int ret = 0;
1147
1148 if (!drv)
1149 return 0;
1150
1151 if (drv->pm) {
1152 if (drv->pm->resume)
1153 ret = drv->pm->resume(dev);
1154 } else {
1155 ret = platform_legacy_resume(dev);
1156 }
1157
1158 return ret;
1159 }
1160
1161 #endif
1162
1163 #ifdef CONFIG_HIBERNATE_CALLBACKS
1164
1165 int platform_pm_freeze(struct device *dev)
1166 {
1167 struct device_driver *drv = dev->driver;
1168 int ret = 0;
1169
1170 if (!drv)
1171 return 0;
1172
1173 if (drv->pm) {
1174 if (drv->pm->freeze)
1175 ret = drv->pm->freeze(dev);
1176 } else {
1177 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1178 }
1179
1180 return ret;
1181 }
1182
1183 int platform_pm_thaw(struct device *dev)
1184 {
1185 struct device_driver *drv = dev->driver;
1186 int ret = 0;
1187
1188 if (!drv)
1189 return 0;
1190
1191 if (drv->pm) {
1192 if (drv->pm->thaw)
1193 ret = drv->pm->thaw(dev);
1194 } else {
1195 ret = platform_legacy_resume(dev);
1196 }
1197
1198 return ret;
1199 }
1200
1201 int platform_pm_poweroff(struct device *dev)
1202 {
1203 struct device_driver *drv = dev->driver;
1204 int ret = 0;
1205
1206 if (!drv)
1207 return 0;
1208
1209 if (drv->pm) {
1210 if (drv->pm->poweroff)
1211 ret = drv->pm->poweroff(dev);
1212 } else {
1213 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1214 }
1215
1216 return ret;
1217 }
1218
1219 int platform_pm_restore(struct device *dev)
1220 {
1221 struct device_driver *drv = dev->driver;
1222 int ret = 0;
1223
1224 if (!drv)
1225 return 0;
1226
1227 if (drv->pm) {
1228 if (drv->pm->restore)
1229 ret = drv->pm->restore(dev);
1230 } else {
1231 ret = platform_legacy_resume(dev);
1232 }
1233
1234 return ret;
1235 }
1236
1237 #endif
1238
1239 int platform_dma_configure(struct device *dev)
1240 {
1241 enum dev_dma_attr attr;
1242 int ret = 0;
1243
1244 if (dev->of_node) {
1245 ret = of_dma_configure(dev, dev->of_node, true);
1246 } else if (has_acpi_companion(dev)) {
1247 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1248 ret = acpi_dma_configure(dev, attr);
1249 }
1250
1251 return ret;
1252 }
1253
1254 static const struct dev_pm_ops platform_dev_pm_ops = {
1255 .runtime_suspend = pm_generic_runtime_suspend,
1256 .runtime_resume = pm_generic_runtime_resume,
1257 USE_PLATFORM_PM_SLEEP_OPS
1258 };
1259
1260 struct bus_type platform_bus_type = {
1261 .name = "platform",
1262 .dev_groups = platform_dev_groups,
1263 .match = platform_match,
1264 .uevent = platform_uevent,
1265 .dma_configure = platform_dma_configure,
1266 .pm = &platform_dev_pm_ops,
1267 };
1268 EXPORT_SYMBOL_GPL(platform_bus_type);
1269
1270 static inline int __platform_match(struct device *dev, const void *drv)
1271 {
1272 return platform_match(dev, (struct device_driver *)drv);
1273 }
1274
1275
1276
1277
1278
1279
1280
1281 struct device *platform_find_device_by_driver(struct device *start,
1282 const struct device_driver *drv)
1283 {
1284 return bus_find_device(&platform_bus_type, start, drv,
1285 __platform_match);
1286 }
1287 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1288
1289 int __init platform_bus_init(void)
1290 {
1291 int error;
1292
1293 early_platform_cleanup();
1294
1295 error = device_register(&platform_bus);
1296 if (error) {
1297 put_device(&platform_bus);
1298 return error;
1299 }
1300 error = bus_register(&platform_bus_type);
1301 if (error)
1302 device_unregister(&platform_bus);
1303 of_platform_register_reconfig_notifier();
1304 return error;
1305 }
1306
1307 static __initdata LIST_HEAD(early_platform_driver_list);
1308 static __initdata LIST_HEAD(early_platform_device_list);
1309
1310
1311
1312
1313
1314
1315
1316
1317 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1318 char *buf)
1319 {
1320 char *tmp;
1321 int n;
1322
1323
1324
1325
1326 if (!epdrv->list.next) {
1327 INIT_LIST_HEAD(&epdrv->list);
1328 list_add_tail(&epdrv->list, &early_platform_driver_list);
1329 }
1330
1331
1332
1333
1334
1335 n = strlen(epdrv->pdrv->driver.name);
1336 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1337 list_move(&epdrv->list, &early_platform_driver_list);
1338
1339
1340 if (buf[n] == '\0' || buf[n] == ',')
1341 epdrv->requested_id = -1;
1342 else {
1343 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1344 &tmp, 10);
1345
1346 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1347 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1348 n = 0;
1349 } else
1350 n += strcspn(&buf[n + 1], ",") + 1;
1351 }
1352
1353 if (buf[n] == ',')
1354 n++;
1355
1356 if (epdrv->bufsize) {
1357 memcpy(epdrv->buffer, &buf[n],
1358 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1359 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1360 }
1361 }
1362
1363 return 0;
1364 }
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374 void __init early_platform_add_devices(struct platform_device **devs, int num)
1375 {
1376 struct device *dev;
1377 int i;
1378
1379
1380 for (i = 0; i < num; i++) {
1381 dev = &devs[i]->dev;
1382
1383 if (!dev->devres_head.next) {
1384 pm_runtime_early_init(dev);
1385 INIT_LIST_HEAD(&dev->devres_head);
1386 list_add_tail(&dev->devres_head,
1387 &early_platform_device_list);
1388 }
1389 }
1390 }
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400 void __init early_platform_driver_register_all(char *class_str)
1401 {
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416 parse_early_options(class_str);
1417 }
1418
1419
1420
1421
1422
1423
1424 static struct platform_device * __init
1425 early_platform_match(struct early_platform_driver *epdrv, int id)
1426 {
1427 struct platform_device *pd;
1428
1429 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1430 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1431 if (pd->id == id)
1432 return pd;
1433
1434 return NULL;
1435 }
1436
1437
1438
1439
1440
1441
1442 static int __init early_platform_left(struct early_platform_driver *epdrv,
1443 int id)
1444 {
1445 struct platform_device *pd;
1446
1447 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1448 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1449 if (pd->id >= id)
1450 return 1;
1451
1452 return 0;
1453 }
1454
1455
1456
1457
1458
1459
1460
1461 static int __init early_platform_driver_probe_id(char *class_str,
1462 int id,
1463 int nr_probe)
1464 {
1465 struct early_platform_driver *epdrv;
1466 struct platform_device *match;
1467 int match_id;
1468 int n = 0;
1469 int left = 0;
1470
1471 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1472
1473 if (strcmp(class_str, epdrv->class_str))
1474 continue;
1475
1476 if (id == -2) {
1477 match_id = epdrv->requested_id;
1478 left = 1;
1479
1480 } else {
1481 match_id = id;
1482 left += early_platform_left(epdrv, id);
1483
1484
1485 switch (epdrv->requested_id) {
1486 case EARLY_PLATFORM_ID_ERROR:
1487 case EARLY_PLATFORM_ID_UNSET:
1488 break;
1489 default:
1490 if (epdrv->requested_id == id)
1491 match_id = EARLY_PLATFORM_ID_UNSET;
1492 }
1493 }
1494
1495 switch (match_id) {
1496 case EARLY_PLATFORM_ID_ERROR:
1497 pr_warn("%s: unable to parse %s parameter\n",
1498 class_str, epdrv->pdrv->driver.name);
1499
1500 case EARLY_PLATFORM_ID_UNSET:
1501 match = NULL;
1502 break;
1503 default:
1504 match = early_platform_match(epdrv, match_id);
1505 }
1506
1507 if (match) {
1508
1509
1510
1511
1512
1513 if (!match->dev.init_name && slab_is_available()) {
1514 if (match->id != -1)
1515 match->dev.init_name =
1516 kasprintf(GFP_KERNEL, "%s.%d",
1517 match->name,
1518 match->id);
1519 else
1520 match->dev.init_name =
1521 kasprintf(GFP_KERNEL, "%s",
1522 match->name);
1523
1524 if (!match->dev.init_name)
1525 return -ENOMEM;
1526 }
1527
1528 if (epdrv->pdrv->probe(match))
1529 pr_warn("%s: unable to probe %s early.\n",
1530 class_str, match->name);
1531 else
1532 n++;
1533 }
1534
1535 if (n >= nr_probe)
1536 break;
1537 }
1538
1539 if (left)
1540 return n;
1541 else
1542 return -ENODEV;
1543 }
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555 int __init early_platform_driver_probe(char *class_str,
1556 int nr_probe,
1557 int user_only)
1558 {
1559 int k, n, i;
1560
1561 n = 0;
1562 for (i = -2; n < nr_probe; i++) {
1563 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1564
1565 if (k < 0)
1566 break;
1567
1568 n += k;
1569
1570 if (user_only)
1571 break;
1572 }
1573
1574 return n;
1575 }
1576
1577
1578
1579
1580 void __init early_platform_cleanup(void)
1581 {
1582 struct platform_device *pd, *pd2;
1583
1584
1585 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1586 dev.devres_head) {
1587 list_del(&pd->dev.devres_head);
1588 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1589 }
1590 }
1591