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