Merge branch 'fix/hda' into for-linus
[deliverable/linux.git] / drivers / base / platform.c
1 /*
2 * platform.c - platform 'pseudo' bus for legacy devices
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 *
7 * This file is released under the GPLv2
8 *
9 * Please see Documentation/driver-model/platform.txt for more
10 * information.
11 */
12
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/bootmem.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20
21 #include "base.h"
22
23 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
24 driver))
25
26 struct device platform_bus = {
27 .init_name = "platform",
28 };
29 EXPORT_SYMBOL_GPL(platform_bus);
30
31 /**
32 * platform_get_resource - get a resource for a device
33 * @dev: platform device
34 * @type: resource type
35 * @num: resource index
36 */
37 struct resource *platform_get_resource(struct platform_device *dev,
38 unsigned int type, unsigned int num)
39 {
40 int i;
41
42 for (i = 0; i < dev->num_resources; i++) {
43 struct resource *r = &dev->resource[i];
44
45 if (type == resource_type(r) && num-- == 0)
46 return r;
47 }
48 return NULL;
49 }
50 EXPORT_SYMBOL_GPL(platform_get_resource);
51
52 /**
53 * platform_get_irq - get an IRQ for a device
54 * @dev: platform device
55 * @num: IRQ number index
56 */
57 int platform_get_irq(struct platform_device *dev, unsigned int num)
58 {
59 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
60
61 return r ? r->start : -ENXIO;
62 }
63 EXPORT_SYMBOL_GPL(platform_get_irq);
64
65 /**
66 * platform_get_resource_byname - get a resource for a device by name
67 * @dev: platform device
68 * @type: resource type
69 * @name: resource name
70 */
71 struct resource *platform_get_resource_byname(struct platform_device *dev,
72 unsigned int type,
73 const char *name)
74 {
75 int i;
76
77 for (i = 0; i < dev->num_resources; i++) {
78 struct resource *r = &dev->resource[i];
79
80 if (type == resource_type(r) && !strcmp(r->name, name))
81 return r;
82 }
83 return NULL;
84 }
85 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
86
87 /**
88 * platform_get_irq - get an IRQ for a device
89 * @dev: platform device
90 * @name: IRQ name
91 */
92 int platform_get_irq_byname(struct platform_device *dev, const char *name)
93 {
94 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
95 name);
96
97 return r ? r->start : -ENXIO;
98 }
99 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
100
101 /**
102 * platform_add_devices - add a numbers of platform devices
103 * @devs: array of platform devices to add
104 * @num: number of platform devices in array
105 */
106 int platform_add_devices(struct platform_device **devs, int num)
107 {
108 int i, ret = 0;
109
110 for (i = 0; i < num; i++) {
111 ret = platform_device_register(devs[i]);
112 if (ret) {
113 while (--i >= 0)
114 platform_device_unregister(devs[i]);
115 break;
116 }
117 }
118
119 return ret;
120 }
121 EXPORT_SYMBOL_GPL(platform_add_devices);
122
123 struct platform_object {
124 struct platform_device pdev;
125 char name[1];
126 };
127
128 /**
129 * platform_device_put
130 * @pdev: platform device to free
131 *
132 * Free all memory associated with a platform device. This function must
133 * _only_ be externally called in error cases. All other usage is a bug.
134 */
135 void platform_device_put(struct platform_device *pdev)
136 {
137 if (pdev)
138 put_device(&pdev->dev);
139 }
140 EXPORT_SYMBOL_GPL(platform_device_put);
141
142 static void platform_device_release(struct device *dev)
143 {
144 struct platform_object *pa = container_of(dev, struct platform_object,
145 pdev.dev);
146
147 kfree(pa->pdev.dev.platform_data);
148 kfree(pa->pdev.resource);
149 kfree(pa);
150 }
151
152 /**
153 * platform_device_alloc
154 * @name: base name of the device we're adding
155 * @id: instance id
156 *
157 * Create a platform device object which can have other objects attached
158 * to it, and which will have attached objects freed when it is released.
159 */
160 struct platform_device *platform_device_alloc(const char *name, int id)
161 {
162 struct platform_object *pa;
163
164 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
165 if (pa) {
166 strcpy(pa->name, name);
167 pa->pdev.name = pa->name;
168 pa->pdev.id = id;
169 device_initialize(&pa->pdev.dev);
170 pa->pdev.dev.release = platform_device_release;
171 }
172
173 return pa ? &pa->pdev : NULL;
174 }
175 EXPORT_SYMBOL_GPL(platform_device_alloc);
176
177 /**
178 * platform_device_add_resources
179 * @pdev: platform device allocated by platform_device_alloc to add resources to
180 * @res: set of resources that needs to be allocated for the device
181 * @num: number of resources
182 *
183 * Add a copy of the resources to the platform device. The memory
184 * associated with the resources will be freed when the platform device is
185 * released.
186 */
187 int platform_device_add_resources(struct platform_device *pdev,
188 struct resource *res, unsigned int num)
189 {
190 struct resource *r;
191
192 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
193 if (r) {
194 memcpy(r, res, sizeof(struct resource) * num);
195 pdev->resource = r;
196 pdev->num_resources = num;
197 }
198 return r ? 0 : -ENOMEM;
199 }
200 EXPORT_SYMBOL_GPL(platform_device_add_resources);
201
202 /**
203 * platform_device_add_data
204 * @pdev: platform device allocated by platform_device_alloc to add resources to
205 * @data: platform specific data for this platform device
206 * @size: size of platform specific data
207 *
208 * Add a copy of platform specific data to the platform device's
209 * platform_data pointer. The memory associated with the platform data
210 * will be freed when the platform device is released.
211 */
212 int platform_device_add_data(struct platform_device *pdev, const void *data,
213 size_t size)
214 {
215 void *d;
216
217 d = kmalloc(size, GFP_KERNEL);
218 if (d) {
219 memcpy(d, data, size);
220 pdev->dev.platform_data = d;
221 }
222 return d ? 0 : -ENOMEM;
223 }
224 EXPORT_SYMBOL_GPL(platform_device_add_data);
225
226 /**
227 * platform_device_add - add a platform device to device hierarchy
228 * @pdev: platform device we're adding
229 *
230 * This is part 2 of platform_device_register(), though may be called
231 * separately _iff_ pdev was allocated by platform_device_alloc().
232 */
233 int platform_device_add(struct platform_device *pdev)
234 {
235 int i, ret = 0;
236
237 if (!pdev)
238 return -EINVAL;
239
240 if (!pdev->dev.parent)
241 pdev->dev.parent = &platform_bus;
242
243 pdev->dev.bus = &platform_bus_type;
244
245 if (pdev->id != -1)
246 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
247 else
248 dev_set_name(&pdev->dev, "%s", pdev->name);
249
250 for (i = 0; i < pdev->num_resources; i++) {
251 struct resource *p, *r = &pdev->resource[i];
252
253 if (r->name == NULL)
254 r->name = dev_name(&pdev->dev);
255
256 p = r->parent;
257 if (!p) {
258 if (resource_type(r) == IORESOURCE_MEM)
259 p = &iomem_resource;
260 else if (resource_type(r) == IORESOURCE_IO)
261 p = &ioport_resource;
262 }
263
264 if (p && insert_resource(p, r)) {
265 printk(KERN_ERR
266 "%s: failed to claim resource %d\n",
267 dev_name(&pdev->dev), i);
268 ret = -EBUSY;
269 goto failed;
270 }
271 }
272
273 pr_debug("Registering platform device '%s'. Parent at %s\n",
274 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
275
276 ret = device_add(&pdev->dev);
277 if (ret == 0)
278 return ret;
279
280 failed:
281 while (--i >= 0) {
282 struct resource *r = &pdev->resource[i];
283 unsigned long type = resource_type(r);
284
285 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
286 release_resource(r);
287 }
288
289 return ret;
290 }
291 EXPORT_SYMBOL_GPL(platform_device_add);
292
293 /**
294 * platform_device_del - remove a platform-level device
295 * @pdev: platform device we're removing
296 *
297 * Note that this function will also release all memory- and port-based
298 * resources owned by the device (@dev->resource). This function must
299 * _only_ be externally called in error cases. All other usage is a bug.
300 */
301 void platform_device_del(struct platform_device *pdev)
302 {
303 int i;
304
305 if (pdev) {
306 device_del(&pdev->dev);
307
308 for (i = 0; i < pdev->num_resources; i++) {
309 struct resource *r = &pdev->resource[i];
310 unsigned long type = resource_type(r);
311
312 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
313 release_resource(r);
314 }
315 }
316 }
317 EXPORT_SYMBOL_GPL(platform_device_del);
318
319 /**
320 * platform_device_register - add a platform-level device
321 * @pdev: platform device we're adding
322 */
323 int platform_device_register(struct platform_device *pdev)
324 {
325 device_initialize(&pdev->dev);
326 return platform_device_add(pdev);
327 }
328 EXPORT_SYMBOL_GPL(platform_device_register);
329
330 /**
331 * platform_device_unregister - unregister a platform-level device
332 * @pdev: platform device we're unregistering
333 *
334 * Unregistration is done in 2 steps. First we release all resources
335 * and remove it from the subsystem, then we drop reference count by
336 * calling platform_device_put().
337 */
338 void platform_device_unregister(struct platform_device *pdev)
339 {
340 platform_device_del(pdev);
341 platform_device_put(pdev);
342 }
343 EXPORT_SYMBOL_GPL(platform_device_unregister);
344
345 /**
346 * platform_device_register_simple
347 * @name: base name of the device we're adding
348 * @id: instance id
349 * @res: set of resources that needs to be allocated for the device
350 * @num: number of resources
351 *
352 * This function creates a simple platform device that requires minimal
353 * resource and memory management. Canned release function freeing memory
354 * allocated for the device allows drivers using such devices to be
355 * unloaded without waiting for the last reference to the device to be
356 * dropped.
357 *
358 * This interface is primarily intended for use with legacy drivers which
359 * probe hardware directly. Because such drivers create sysfs device nodes
360 * themselves, rather than letting system infrastructure handle such device
361 * enumeration tasks, they don't fully conform to the Linux driver model.
362 * In particular, when such drivers are built as modules, they can't be
363 * "hotplugged".
364 */
365 struct platform_device *platform_device_register_simple(const char *name,
366 int id,
367 struct resource *res,
368 unsigned int num)
369 {
370 struct platform_device *pdev;
371 int retval;
372
373 pdev = platform_device_alloc(name, id);
374 if (!pdev) {
375 retval = -ENOMEM;
376 goto error;
377 }
378
379 if (num) {
380 retval = platform_device_add_resources(pdev, res, num);
381 if (retval)
382 goto error;
383 }
384
385 retval = platform_device_add(pdev);
386 if (retval)
387 goto error;
388
389 return pdev;
390
391 error:
392 platform_device_put(pdev);
393 return ERR_PTR(retval);
394 }
395 EXPORT_SYMBOL_GPL(platform_device_register_simple);
396
397 /**
398 * platform_device_register_data
399 * @parent: parent device for the device we're adding
400 * @name: base name of the device we're adding
401 * @id: instance id
402 * @data: platform specific data for this platform device
403 * @size: size of platform specific data
404 *
405 * This function creates a simple platform device that requires minimal
406 * resource and memory management. Canned release function freeing memory
407 * allocated for the device allows drivers using such devices to be
408 * unloaded without waiting for the last reference to the device to be
409 * dropped.
410 */
411 struct platform_device *platform_device_register_data(
412 struct device *parent,
413 const char *name, int id,
414 const void *data, size_t size)
415 {
416 struct platform_device *pdev;
417 int retval;
418
419 pdev = platform_device_alloc(name, id);
420 if (!pdev) {
421 retval = -ENOMEM;
422 goto error;
423 }
424
425 pdev->dev.parent = parent;
426
427 if (size) {
428 retval = platform_device_add_data(pdev, data, size);
429 if (retval)
430 goto error;
431 }
432
433 retval = platform_device_add(pdev);
434 if (retval)
435 goto error;
436
437 return pdev;
438
439 error:
440 platform_device_put(pdev);
441 return ERR_PTR(retval);
442 }
443
444 static int platform_drv_probe(struct device *_dev)
445 {
446 struct platform_driver *drv = to_platform_driver(_dev->driver);
447 struct platform_device *dev = to_platform_device(_dev);
448
449 return drv->probe(dev);
450 }
451
452 static int platform_drv_probe_fail(struct device *_dev)
453 {
454 return -ENXIO;
455 }
456
457 static int platform_drv_remove(struct device *_dev)
458 {
459 struct platform_driver *drv = to_platform_driver(_dev->driver);
460 struct platform_device *dev = to_platform_device(_dev);
461
462 return drv->remove(dev);
463 }
464
465 static void platform_drv_shutdown(struct device *_dev)
466 {
467 struct platform_driver *drv = to_platform_driver(_dev->driver);
468 struct platform_device *dev = to_platform_device(_dev);
469
470 drv->shutdown(dev);
471 }
472
473 /**
474 * platform_driver_register
475 * @drv: platform driver structure
476 */
477 int platform_driver_register(struct platform_driver *drv)
478 {
479 drv->driver.bus = &platform_bus_type;
480 if (drv->probe)
481 drv->driver.probe = platform_drv_probe;
482 if (drv->remove)
483 drv->driver.remove = platform_drv_remove;
484 if (drv->shutdown)
485 drv->driver.shutdown = platform_drv_shutdown;
486
487 return driver_register(&drv->driver);
488 }
489 EXPORT_SYMBOL_GPL(platform_driver_register);
490
491 /**
492 * platform_driver_unregister
493 * @drv: platform driver structure
494 */
495 void platform_driver_unregister(struct platform_driver *drv)
496 {
497 driver_unregister(&drv->driver);
498 }
499 EXPORT_SYMBOL_GPL(platform_driver_unregister);
500
501 /**
502 * platform_driver_probe - register driver for non-hotpluggable device
503 * @drv: platform driver structure
504 * @probe: the driver probe routine, probably from an __init section
505 *
506 * Use this instead of platform_driver_register() when you know the device
507 * is not hotpluggable and has already been registered, and you want to
508 * remove its run-once probe() infrastructure from memory after the driver
509 * has bound to the device.
510 *
511 * One typical use for this would be with drivers for controllers integrated
512 * into system-on-chip processors, where the controller devices have been
513 * configured as part of board setup.
514 *
515 * Returns zero if the driver registered and bound to a device, else returns
516 * a negative error code and with the driver not registered.
517 */
518 int __init_or_module platform_driver_probe(struct platform_driver *drv,
519 int (*probe)(struct platform_device *))
520 {
521 int retval, code;
522
523 /* temporary section violation during probe() */
524 drv->probe = probe;
525 retval = code = platform_driver_register(drv);
526
527 /* Fixup that section violation, being paranoid about code scanning
528 * the list of drivers in order to probe new devices. Check to see
529 * if the probe was successful, and make sure any forced probes of
530 * new devices fail.
531 */
532 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
533 drv->probe = NULL;
534 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
535 retval = -ENODEV;
536 drv->driver.probe = platform_drv_probe_fail;
537 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
538
539 if (code != retval)
540 platform_driver_unregister(drv);
541 return retval;
542 }
543 EXPORT_SYMBOL_GPL(platform_driver_probe);
544
545 /* modalias support enables more hands-off userspace setup:
546 * (a) environment variable lets new-style hotplug events work once system is
547 * fully running: "modprobe $MODALIAS"
548 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
549 * mishandled before system is fully running: "modprobe $(cat modalias)"
550 */
551 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
552 char *buf)
553 {
554 struct platform_device *pdev = to_platform_device(dev);
555 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
556
557 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
558 }
559
560 static struct device_attribute platform_dev_attrs[] = {
561 __ATTR_RO(modalias),
562 __ATTR_NULL,
563 };
564
565 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
566 {
567 struct platform_device *pdev = to_platform_device(dev);
568
569 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
570 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
571 return 0;
572 }
573
574 static const struct platform_device_id *platform_match_id(
575 struct platform_device_id *id,
576 struct platform_device *pdev)
577 {
578 while (id->name[0]) {
579 if (strcmp(pdev->name, id->name) == 0) {
580 pdev->id_entry = id;
581 return id;
582 }
583 id++;
584 }
585 return NULL;
586 }
587
588 /**
589 * platform_match - bind platform device to platform driver.
590 * @dev: device.
591 * @drv: driver.
592 *
593 * Platform device IDs are assumed to be encoded like this:
594 * "<name><instance>", where <name> is a short description of the type of
595 * device, like "pci" or "floppy", and <instance> is the enumerated
596 * instance of the device, like '0' or '42'. Driver IDs are simply
597 * "<name>". So, extract the <name> from the platform_device structure,
598 * and compare it against the name of the driver. Return whether they match
599 * or not.
600 */
601 static int platform_match(struct device *dev, struct device_driver *drv)
602 {
603 struct platform_device *pdev = to_platform_device(dev);
604 struct platform_driver *pdrv = to_platform_driver(drv);
605
606 /* match against the id table first */
607 if (pdrv->id_table)
608 return platform_match_id(pdrv->id_table, pdev) != NULL;
609
610 /* fall-back to driver name match */
611 return (strcmp(pdev->name, drv->name) == 0);
612 }
613
614 #ifdef CONFIG_PM_SLEEP
615
616 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
617 {
618 struct platform_driver *pdrv = to_platform_driver(dev->driver);
619 struct platform_device *pdev = to_platform_device(dev);
620 int ret = 0;
621
622 if (dev->driver && pdrv->suspend)
623 ret = pdrv->suspend(pdev, mesg);
624
625 return ret;
626 }
627
628 static int platform_legacy_suspend_late(struct device *dev, pm_message_t mesg)
629 {
630 struct platform_driver *pdrv = to_platform_driver(dev->driver);
631 struct platform_device *pdev = to_platform_device(dev);
632 int ret = 0;
633
634 if (dev->driver && pdrv->suspend_late)
635 ret = pdrv->suspend_late(pdev, mesg);
636
637 return ret;
638 }
639
640 static int platform_legacy_resume_early(struct device *dev)
641 {
642 struct platform_driver *pdrv = to_platform_driver(dev->driver);
643 struct platform_device *pdev = to_platform_device(dev);
644 int ret = 0;
645
646 if (dev->driver && pdrv->resume_early)
647 ret = pdrv->resume_early(pdev);
648
649 return ret;
650 }
651
652 static int platform_legacy_resume(struct device *dev)
653 {
654 struct platform_driver *pdrv = to_platform_driver(dev->driver);
655 struct platform_device *pdev = to_platform_device(dev);
656 int ret = 0;
657
658 if (dev->driver && pdrv->resume)
659 ret = pdrv->resume(pdev);
660
661 return ret;
662 }
663
664 static int platform_pm_prepare(struct device *dev)
665 {
666 struct device_driver *drv = dev->driver;
667 int ret = 0;
668
669 if (drv && drv->pm && drv->pm->prepare)
670 ret = drv->pm->prepare(dev);
671
672 return ret;
673 }
674
675 static void platform_pm_complete(struct device *dev)
676 {
677 struct device_driver *drv = dev->driver;
678
679 if (drv && drv->pm && drv->pm->complete)
680 drv->pm->complete(dev);
681 }
682
683 #ifdef CONFIG_SUSPEND
684
685 static int platform_pm_suspend(struct device *dev)
686 {
687 struct device_driver *drv = dev->driver;
688 int ret = 0;
689
690 if (!drv)
691 return 0;
692
693 if (drv->pm) {
694 if (drv->pm->suspend)
695 ret = drv->pm->suspend(dev);
696 } else {
697 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
698 }
699
700 return ret;
701 }
702
703 static int platform_pm_suspend_noirq(struct device *dev)
704 {
705 struct device_driver *drv = dev->driver;
706 int ret = 0;
707
708 if (!drv)
709 return 0;
710
711 if (drv->pm) {
712 if (drv->pm->suspend_noirq)
713 ret = drv->pm->suspend_noirq(dev);
714 } else {
715 ret = platform_legacy_suspend_late(dev, PMSG_SUSPEND);
716 }
717
718 return ret;
719 }
720
721 static int platform_pm_resume(struct device *dev)
722 {
723 struct device_driver *drv = dev->driver;
724 int ret = 0;
725
726 if (!drv)
727 return 0;
728
729 if (drv->pm) {
730 if (drv->pm->resume)
731 ret = drv->pm->resume(dev);
732 } else {
733 ret = platform_legacy_resume(dev);
734 }
735
736 return ret;
737 }
738
739 static int platform_pm_resume_noirq(struct device *dev)
740 {
741 struct device_driver *drv = dev->driver;
742 int ret = 0;
743
744 if (!drv)
745 return 0;
746
747 if (drv->pm) {
748 if (drv->pm->resume_noirq)
749 ret = drv->pm->resume_noirq(dev);
750 } else {
751 ret = platform_legacy_resume_early(dev);
752 }
753
754 return ret;
755 }
756
757 #else /* !CONFIG_SUSPEND */
758
759 #define platform_pm_suspend NULL
760 #define platform_pm_resume NULL
761 #define platform_pm_suspend_noirq NULL
762 #define platform_pm_resume_noirq NULL
763
764 #endif /* !CONFIG_SUSPEND */
765
766 #ifdef CONFIG_HIBERNATION
767
768 static int platform_pm_freeze(struct device *dev)
769 {
770 struct device_driver *drv = dev->driver;
771 int ret = 0;
772
773 if (!drv)
774 return 0;
775
776 if (drv->pm) {
777 if (drv->pm->freeze)
778 ret = drv->pm->freeze(dev);
779 } else {
780 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
781 }
782
783 return ret;
784 }
785
786 static int platform_pm_freeze_noirq(struct device *dev)
787 {
788 struct device_driver *drv = dev->driver;
789 int ret = 0;
790
791 if (!drv)
792 return 0;
793
794 if (drv->pm) {
795 if (drv->pm->freeze_noirq)
796 ret = drv->pm->freeze_noirq(dev);
797 } else {
798 ret = platform_legacy_suspend_late(dev, PMSG_FREEZE);
799 }
800
801 return ret;
802 }
803
804 static int platform_pm_thaw(struct device *dev)
805 {
806 struct device_driver *drv = dev->driver;
807 int ret = 0;
808
809 if (!drv)
810 return 0;
811
812 if (drv->pm) {
813 if (drv->pm->thaw)
814 ret = drv->pm->thaw(dev);
815 } else {
816 ret = platform_legacy_resume(dev);
817 }
818
819 return ret;
820 }
821
822 static int platform_pm_thaw_noirq(struct device *dev)
823 {
824 struct device_driver *drv = dev->driver;
825 int ret = 0;
826
827 if (!drv)
828 return 0;
829
830 if (drv->pm) {
831 if (drv->pm->thaw_noirq)
832 ret = drv->pm->thaw_noirq(dev);
833 } else {
834 ret = platform_legacy_resume_early(dev);
835 }
836
837 return ret;
838 }
839
840 static int platform_pm_poweroff(struct device *dev)
841 {
842 struct device_driver *drv = dev->driver;
843 int ret = 0;
844
845 if (!drv)
846 return 0;
847
848 if (drv->pm) {
849 if (drv->pm->poweroff)
850 ret = drv->pm->poweroff(dev);
851 } else {
852 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
853 }
854
855 return ret;
856 }
857
858 static int platform_pm_poweroff_noirq(struct device *dev)
859 {
860 struct device_driver *drv = dev->driver;
861 int ret = 0;
862
863 if (!drv)
864 return 0;
865
866 if (drv->pm) {
867 if (drv->pm->poweroff_noirq)
868 ret = drv->pm->poweroff_noirq(dev);
869 } else {
870 ret = platform_legacy_suspend_late(dev, PMSG_HIBERNATE);
871 }
872
873 return ret;
874 }
875
876 static int platform_pm_restore(struct device *dev)
877 {
878 struct device_driver *drv = dev->driver;
879 int ret = 0;
880
881 if (!drv)
882 return 0;
883
884 if (drv->pm) {
885 if (drv->pm->restore)
886 ret = drv->pm->restore(dev);
887 } else {
888 ret = platform_legacy_resume(dev);
889 }
890
891 return ret;
892 }
893
894 static int platform_pm_restore_noirq(struct device *dev)
895 {
896 struct device_driver *drv = dev->driver;
897 int ret = 0;
898
899 if (!drv)
900 return 0;
901
902 if (drv->pm) {
903 if (drv->pm->restore_noirq)
904 ret = drv->pm->restore_noirq(dev);
905 } else {
906 ret = platform_legacy_resume_early(dev);
907 }
908
909 return ret;
910 }
911
912 #else /* !CONFIG_HIBERNATION */
913
914 #define platform_pm_freeze NULL
915 #define platform_pm_thaw NULL
916 #define platform_pm_poweroff NULL
917 #define platform_pm_restore NULL
918 #define platform_pm_freeze_noirq NULL
919 #define platform_pm_thaw_noirq NULL
920 #define platform_pm_poweroff_noirq NULL
921 #define platform_pm_restore_noirq NULL
922
923 #endif /* !CONFIG_HIBERNATION */
924
925 static struct dev_pm_ops platform_dev_pm_ops = {
926 .prepare = platform_pm_prepare,
927 .complete = platform_pm_complete,
928 .suspend = platform_pm_suspend,
929 .resume = platform_pm_resume,
930 .freeze = platform_pm_freeze,
931 .thaw = platform_pm_thaw,
932 .poweroff = platform_pm_poweroff,
933 .restore = platform_pm_restore,
934 .suspend_noirq = platform_pm_suspend_noirq,
935 .resume_noirq = platform_pm_resume_noirq,
936 .freeze_noirq = platform_pm_freeze_noirq,
937 .thaw_noirq = platform_pm_thaw_noirq,
938 .poweroff_noirq = platform_pm_poweroff_noirq,
939 .restore_noirq = platform_pm_restore_noirq,
940 };
941
942 #define PLATFORM_PM_OPS_PTR (&platform_dev_pm_ops)
943
944 #else /* !CONFIG_PM_SLEEP */
945
946 #define PLATFORM_PM_OPS_PTR NULL
947
948 #endif /* !CONFIG_PM_SLEEP */
949
950 struct bus_type platform_bus_type = {
951 .name = "platform",
952 .dev_attrs = platform_dev_attrs,
953 .match = platform_match,
954 .uevent = platform_uevent,
955 .pm = PLATFORM_PM_OPS_PTR,
956 };
957 EXPORT_SYMBOL_GPL(platform_bus_type);
958
959 int __init platform_bus_init(void)
960 {
961 int error;
962
963 early_platform_cleanup();
964
965 error = device_register(&platform_bus);
966 if (error)
967 return error;
968 error = bus_register(&platform_bus_type);
969 if (error)
970 device_unregister(&platform_bus);
971 return error;
972 }
973
974 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
975 u64 dma_get_required_mask(struct device *dev)
976 {
977 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
978 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
979 u64 mask;
980
981 if (!high_totalram) {
982 /* convert to mask just covering totalram */
983 low_totalram = (1 << (fls(low_totalram) - 1));
984 low_totalram += low_totalram - 1;
985 mask = low_totalram;
986 } else {
987 high_totalram = (1 << (fls(high_totalram) - 1));
988 high_totalram += high_totalram - 1;
989 mask = (((u64)high_totalram) << 32) + 0xffffffff;
990 }
991 return mask;
992 }
993 EXPORT_SYMBOL_GPL(dma_get_required_mask);
994 #endif
995
996 static __initdata LIST_HEAD(early_platform_driver_list);
997 static __initdata LIST_HEAD(early_platform_device_list);
998
999 /**
1000 * early_platform_driver_register
1001 * @epdrv: early_platform driver structure
1002 * @buf: string passed from early_param()
1003 */
1004 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1005 char *buf)
1006 {
1007 unsigned long index;
1008 int n;
1009
1010 /* Simply add the driver to the end of the global list.
1011 * Drivers will by default be put on the list in compiled-in order.
1012 */
1013 if (!epdrv->list.next) {
1014 INIT_LIST_HEAD(&epdrv->list);
1015 list_add_tail(&epdrv->list, &early_platform_driver_list);
1016 }
1017
1018 /* If the user has specified device then make sure the driver
1019 * gets prioritized. The driver of the last device specified on
1020 * command line will be put first on the list.
1021 */
1022 n = strlen(epdrv->pdrv->driver.name);
1023 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1024 list_move(&epdrv->list, &early_platform_driver_list);
1025
1026 if (!strcmp(buf, epdrv->pdrv->driver.name))
1027 epdrv->requested_id = -1;
1028 else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10,
1029 &index) == 0)
1030 epdrv->requested_id = index;
1031 else
1032 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1033 }
1034
1035 return 0;
1036 }
1037
1038 /**
1039 * early_platform_add_devices - add a numbers of early platform devices
1040 * @devs: array of early platform devices to add
1041 * @num: number of early platform devices in array
1042 */
1043 void __init early_platform_add_devices(struct platform_device **devs, int num)
1044 {
1045 struct device *dev;
1046 int i;
1047
1048 /* simply add the devices to list */
1049 for (i = 0; i < num; i++) {
1050 dev = &devs[i]->dev;
1051
1052 if (!dev->devres_head.next) {
1053 INIT_LIST_HEAD(&dev->devres_head);
1054 list_add_tail(&dev->devres_head,
1055 &early_platform_device_list);
1056 }
1057 }
1058 }
1059
1060 /**
1061 * early_platform_driver_register_all
1062 * @class_str: string to identify early platform driver class
1063 */
1064 void __init early_platform_driver_register_all(char *class_str)
1065 {
1066 /* The "class_str" parameter may or may not be present on the kernel
1067 * command line. If it is present then there may be more than one
1068 * matching parameter.
1069 *
1070 * Since we register our early platform drivers using early_param()
1071 * we need to make sure that they also get registered in the case
1072 * when the parameter is missing from the kernel command line.
1073 *
1074 * We use parse_early_options() to make sure the early_param() gets
1075 * called at least once. The early_param() may be called more than
1076 * once since the name of the preferred device may be specified on
1077 * the kernel command line. early_platform_driver_register() handles
1078 * this case for us.
1079 */
1080 parse_early_options(class_str);
1081 }
1082
1083 /**
1084 * early_platform_match
1085 * @epdrv: early platform driver structure
1086 * @id: id to match against
1087 */
1088 static __init struct platform_device *
1089 early_platform_match(struct early_platform_driver *epdrv, int id)
1090 {
1091 struct platform_device *pd;
1092
1093 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1094 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1095 if (pd->id == id)
1096 return pd;
1097
1098 return NULL;
1099 }
1100
1101 /**
1102 * early_platform_left
1103 * @epdrv: early platform driver structure
1104 * @id: return true if id or above exists
1105 */
1106 static __init int early_platform_left(struct early_platform_driver *epdrv,
1107 int id)
1108 {
1109 struct platform_device *pd;
1110
1111 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1112 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1113 if (pd->id >= id)
1114 return 1;
1115
1116 return 0;
1117 }
1118
1119 /**
1120 * early_platform_driver_probe_id
1121 * @class_str: string to identify early platform driver class
1122 * @id: id to match against
1123 * @nr_probe: number of platform devices to successfully probe before exiting
1124 */
1125 static int __init early_platform_driver_probe_id(char *class_str,
1126 int id,
1127 int nr_probe)
1128 {
1129 struct early_platform_driver *epdrv;
1130 struct platform_device *match;
1131 int match_id;
1132 int n = 0;
1133 int left = 0;
1134
1135 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1136 /* only use drivers matching our class_str */
1137 if (strcmp(class_str, epdrv->class_str))
1138 continue;
1139
1140 if (id == -2) {
1141 match_id = epdrv->requested_id;
1142 left = 1;
1143
1144 } else {
1145 match_id = id;
1146 left += early_platform_left(epdrv, id);
1147
1148 /* skip requested id */
1149 switch (epdrv->requested_id) {
1150 case EARLY_PLATFORM_ID_ERROR:
1151 case EARLY_PLATFORM_ID_UNSET:
1152 break;
1153 default:
1154 if (epdrv->requested_id == id)
1155 match_id = EARLY_PLATFORM_ID_UNSET;
1156 }
1157 }
1158
1159 switch (match_id) {
1160 case EARLY_PLATFORM_ID_ERROR:
1161 pr_warning("%s: unable to parse %s parameter\n",
1162 class_str, epdrv->pdrv->driver.name);
1163 /* fall-through */
1164 case EARLY_PLATFORM_ID_UNSET:
1165 match = NULL;
1166 break;
1167 default:
1168 match = early_platform_match(epdrv, match_id);
1169 }
1170
1171 if (match) {
1172 if (epdrv->pdrv->probe(match))
1173 pr_warning("%s: unable to probe %s early.\n",
1174 class_str, match->name);
1175 else
1176 n++;
1177 }
1178
1179 if (n >= nr_probe)
1180 break;
1181 }
1182
1183 if (left)
1184 return n;
1185 else
1186 return -ENODEV;
1187 }
1188
1189 /**
1190 * early_platform_driver_probe
1191 * @class_str: string to identify early platform driver class
1192 * @nr_probe: number of platform devices to successfully probe before exiting
1193 * @user_only: only probe user specified early platform devices
1194 */
1195 int __init early_platform_driver_probe(char *class_str,
1196 int nr_probe,
1197 int user_only)
1198 {
1199 int k, n, i;
1200
1201 n = 0;
1202 for (i = -2; n < nr_probe; i++) {
1203 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1204
1205 if (k < 0)
1206 break;
1207
1208 n += k;
1209
1210 if (user_only)
1211 break;
1212 }
1213
1214 return n;
1215 }
1216
1217 /**
1218 * early_platform_cleanup - clean up early platform code
1219 */
1220 void __init early_platform_cleanup(void)
1221 {
1222 struct platform_device *pd, *pd2;
1223
1224 /* clean up the devres list used to chain devices */
1225 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1226 dev.devres_head) {
1227 list_del(&pd->dev.devres_head);
1228 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1229 }
1230 }
1231
This page took 0.092753 seconds and 5 git commands to generate.