PM: Introduce core framework for run-time PM of I/O devices (rev. 17)
[deliverable/linux.git] / drivers / base / platform.c
CommitLineData
1da177e4
LT
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
d052d1be 13#include <linux/platform_device.h>
1da177e4
LT
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>
4e57b681 19#include <linux/slab.h>
1da177e4 20
a1bdc7aa
BD
21#include "base.h"
22
4a3ad20c
GKH
23#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
24 driver))
00d3dcdd 25
1da177e4 26struct device platform_bus = {
1e0b2cf9 27 .init_name = "platform",
1da177e4 28};
a96b2042 29EXPORT_SYMBOL_GPL(platform_bus);
1da177e4
LT
30
31/**
4a3ad20c
GKH
32 * platform_get_resource - get a resource for a device
33 * @dev: platform device
34 * @type: resource type
35 * @num: resource index
1da177e4 36 */
4a3ad20c
GKH
37struct resource *platform_get_resource(struct platform_device *dev,
38 unsigned int type, unsigned int num)
1da177e4
LT
39{
40 int i;
41
42 for (i = 0; i < dev->num_resources; i++) {
43 struct resource *r = &dev->resource[i];
44
c9f66169
MD
45 if (type == resource_type(r) && num-- == 0)
46 return r;
1da177e4
LT
47 }
48 return NULL;
49}
a96b2042 50EXPORT_SYMBOL_GPL(platform_get_resource);
1da177e4
LT
51
52/**
4a3ad20c
GKH
53 * platform_get_irq - get an IRQ for a device
54 * @dev: platform device
55 * @num: IRQ number index
1da177e4
LT
56 */
57int platform_get_irq(struct platform_device *dev, unsigned int num)
58{
59 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
60
305b3228 61 return r ? r->start : -ENXIO;
1da177e4 62}
a96b2042 63EXPORT_SYMBOL_GPL(platform_get_irq);
1da177e4
LT
64
65/**
4a3ad20c
GKH
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
1da177e4 70 */
4a3ad20c 71struct resource *platform_get_resource_byname(struct platform_device *dev,
c0afe7ba
LW
72 unsigned int type,
73 const char *name)
1da177e4
LT
74{
75 int i;
76
77 for (i = 0; i < dev->num_resources; i++) {
78 struct resource *r = &dev->resource[i];
79
c9f66169
MD
80 if (type == resource_type(r) && !strcmp(r->name, name))
81 return r;
1da177e4
LT
82 }
83 return NULL;
84}
a96b2042 85EXPORT_SYMBOL_GPL(platform_get_resource_byname);
1da177e4
LT
86
87/**
4a3ad20c
GKH
88 * platform_get_irq - get an IRQ for a device
89 * @dev: platform device
90 * @name: IRQ name
1da177e4 91 */
c0afe7ba 92int platform_get_irq_byname(struct platform_device *dev, const char *name)
1da177e4 93{
4a3ad20c
GKH
94 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
95 name);
1da177e4 96
305b3228 97 return r ? r->start : -ENXIO;
1da177e4 98}
a96b2042 99EXPORT_SYMBOL_GPL(platform_get_irq_byname);
1da177e4
LT
100
101/**
4a3ad20c
GKH
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
1da177e4
LT
105 */
106int 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}
a96b2042 121EXPORT_SYMBOL_GPL(platform_add_devices);
1da177e4 122
37c12e74
RK
123struct platform_object {
124 struct platform_device pdev;
125 char name[1];
126};
127
1da177e4 128/**
4a3ad20c
GKH
129 * platform_device_put
130 * @pdev: platform device to free
37c12e74 131 *
4a3ad20c
GKH
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.
37c12e74
RK
134 */
135void platform_device_put(struct platform_device *pdev)
136{
137 if (pdev)
138 put_device(&pdev->dev);
139}
140EXPORT_SYMBOL_GPL(platform_device_put);
141
142static void platform_device_release(struct device *dev)
143{
4a3ad20c
GKH
144 struct platform_object *pa = container_of(dev, struct platform_object,
145 pdev.dev);
37c12e74
RK
146
147 kfree(pa->pdev.dev.platform_data);
148 kfree(pa->pdev.resource);
149 kfree(pa);
150}
151
152/**
4a3ad20c
GKH
153 * platform_device_alloc
154 * @name: base name of the device we're adding
155 * @id: instance id
37c12e74 156 *
4a3ad20c
GKH
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.
37c12e74 159 */
1359555e 160struct platform_device *platform_device_alloc(const char *name, int id)
37c12e74
RK
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
93ce3061 173 return pa ? &pa->pdev : NULL;
37c12e74
RK
174}
175EXPORT_SYMBOL_GPL(platform_device_alloc);
176
177/**
4a3ad20c
GKH
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
37c12e74 182 *
4a3ad20c
GKH
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.
37c12e74 186 */
4a3ad20c
GKH
187int platform_device_add_resources(struct platform_device *pdev,
188 struct resource *res, unsigned int num)
37c12e74
RK
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}
200EXPORT_SYMBOL_GPL(platform_device_add_resources);
201
202/**
4a3ad20c
GKH
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
37c12e74 207 *
4a3ad20c
GKH
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.
37c12e74 211 */
4a3ad20c
GKH
212int platform_device_add_data(struct platform_device *pdev, const void *data,
213 size_t size)
37c12e74
RK
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}
224EXPORT_SYMBOL_GPL(platform_device_add_data);
225
226/**
4a3ad20c
GKH
227 * platform_device_add - add a platform device to device hierarchy
228 * @pdev: platform device we're adding
1da177e4 229 *
4a3ad20c
GKH
230 * This is part 2 of platform_device_register(), though may be called
231 * separately _iff_ pdev was allocated by platform_device_alloc().
1da177e4 232 */
37c12e74 233int platform_device_add(struct platform_device *pdev)
1da177e4
LT
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)
1e0b2cf9 246 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
1da177e4 247 else
acc0e90f 248 dev_set_name(&pdev->dev, "%s", pdev->name);
1da177e4
LT
249
250 for (i = 0; i < pdev->num_resources; i++) {
251 struct resource *p, *r = &pdev->resource[i];
252
253 if (r->name == NULL)
1e0b2cf9 254 r->name = dev_name(&pdev->dev);
1da177e4
LT
255
256 p = r->parent;
257 if (!p) {
c9f66169 258 if (resource_type(r) == IORESOURCE_MEM)
1da177e4 259 p = &iomem_resource;
c9f66169 260 else if (resource_type(r) == IORESOURCE_IO)
1da177e4
LT
261 p = &ioport_resource;
262 }
263
d960bb4d 264 if (p && insert_resource(p, r)) {
1da177e4
LT
265 printk(KERN_ERR
266 "%s: failed to claim resource %d\n",
1e0b2cf9 267 dev_name(&pdev->dev), i);
1da177e4
LT
268 ret = -EBUSY;
269 goto failed;
270 }
271 }
272
273 pr_debug("Registering platform device '%s'. Parent at %s\n",
1e0b2cf9 274 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
1da177e4 275
e3915532 276 ret = device_add(&pdev->dev);
1da177e4
LT
277 if (ret == 0)
278 return ret;
279
280 failed:
c9f66169
MD
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
1da177e4
LT
289 return ret;
290}
37c12e74
RK
291EXPORT_SYMBOL_GPL(platform_device_add);
292
293/**
4a3ad20c
GKH
294 * platform_device_del - remove a platform-level device
295 * @pdev: platform device we're removing
1da177e4 296 *
4a3ad20c
GKH
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.
1da177e4 300 */
93ce3061 301void platform_device_del(struct platform_device *pdev)
1da177e4
LT
302{
303 int i;
304
305 if (pdev) {
dc4c15d4
JD
306 device_del(&pdev->dev);
307
1da177e4
LT
308 for (i = 0; i < pdev->num_resources; i++) {
309 struct resource *r = &pdev->resource[i];
c9f66169
MD
310 unsigned long type = resource_type(r);
311
312 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
1da177e4
LT
313 release_resource(r);
314 }
1da177e4
LT
315 }
316}
93ce3061
DT
317EXPORT_SYMBOL_GPL(platform_device_del);
318
319/**
4a3ad20c
GKH
320 * platform_device_register - add a platform-level device
321 * @pdev: platform device we're adding
93ce3061 322 */
4a3ad20c 323int platform_device_register(struct platform_device *pdev)
93ce3061
DT
324{
325 device_initialize(&pdev->dev);
326 return platform_device_add(pdev);
327}
a96b2042 328EXPORT_SYMBOL_GPL(platform_device_register);
93ce3061
DT
329
330/**
4a3ad20c
GKH
331 * platform_device_unregister - unregister a platform-level device
332 * @pdev: platform device we're unregistering
93ce3061 333 *
4a3ad20c
GKH
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().
93ce3061 337 */
4a3ad20c 338void platform_device_unregister(struct platform_device *pdev)
93ce3061
DT
339{
340 platform_device_del(pdev);
341 platform_device_put(pdev);
342}
a96b2042 343EXPORT_SYMBOL_GPL(platform_device_unregister);
1da177e4 344
1da177e4 345/**
4a3ad20c
GKH
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
1da177e4 351 *
4a3ad20c
GKH
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.
49a4ec18 357 *
4a3ad20c
GKH
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".
1da177e4 364 */
4a3ad20c
GKH
365struct platform_device *platform_device_register_simple(const char *name,
366 int id,
367 struct resource *res,
368 unsigned int num)
1da177e4 369{
37c12e74 370 struct platform_device *pdev;
1da177e4
LT
371 int retval;
372
37c12e74
RK
373 pdev = platform_device_alloc(name, id);
374 if (!pdev) {
1da177e4
LT
375 retval = -ENOMEM;
376 goto error;
377 }
378
1da177e4 379 if (num) {
37c12e74
RK
380 retval = platform_device_add_resources(pdev, res, num);
381 if (retval)
382 goto error;
1da177e4
LT
383 }
384
37c12e74 385 retval = platform_device_add(pdev);
1da177e4
LT
386 if (retval)
387 goto error;
388
37c12e74 389 return pdev;
1da177e4
LT
390
391error:
37c12e74 392 platform_device_put(pdev);
1da177e4
LT
393 return ERR_PTR(retval);
394}
a96b2042 395EXPORT_SYMBOL_GPL(platform_device_register_simple);
1da177e4 396
d8bf2540
DB
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 */
411struct 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
439error:
440 platform_device_put(pdev);
441 return ERR_PTR(retval);
442}
443
00d3dcdd
RK
444static 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
c67334fb
DB
452static int platform_drv_probe_fail(struct device *_dev)
453{
454 return -ENXIO;
455}
456
00d3dcdd
RK
457static 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
465static 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
00d3dcdd 473/**
4a3ad20c
GKH
474 * platform_driver_register
475 * @drv: platform driver structure
00d3dcdd
RK
476 */
477int 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;
783ea7d4 486
00d3dcdd
RK
487 return driver_register(&drv->driver);
488}
489EXPORT_SYMBOL_GPL(platform_driver_register);
490
491/**
4a3ad20c
GKH
492 * platform_driver_unregister
493 * @drv: platform driver structure
00d3dcdd
RK
494 */
495void platform_driver_unregister(struct platform_driver *drv)
496{
497 driver_unregister(&drv->driver);
498}
499EXPORT_SYMBOL_GPL(platform_driver_unregister);
500
c67334fb
DB
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 */
c63e0783 518int __init_or_module platform_driver_probe(struct platform_driver *drv,
c67334fb
DB
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 */
c6f7e72a 532 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
c67334fb 533 drv->probe = NULL;
e5dd1278 534 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
c67334fb
DB
535 retval = -ENODEV;
536 drv->driver.probe = platform_drv_probe_fail;
c6f7e72a 537 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
c67334fb
DB
538
539 if (code != retval)
540 platform_driver_unregister(drv);
541 return retval;
542}
543EXPORT_SYMBOL_GPL(platform_driver_probe);
1da177e4 544
a0245f7a
DB
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 */
4a3ad20c
GKH
551static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
552 char *buf)
a0245f7a
DB
553{
554 struct platform_device *pdev = to_platform_device(dev);
43cc71ee 555 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
a0245f7a
DB
556
557 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
558}
559
560static struct device_attribute platform_dev_attrs[] = {
561 __ATTR_RO(modalias),
562 __ATTR_NULL,
563};
564
7eff2e7a 565static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
a0245f7a
DB
566{
567 struct platform_device *pdev = to_platform_device(dev);
568
57fee4a5
EM
569 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
570 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
a0245f7a
DB
571 return 0;
572}
573
57fee4a5
EM
574static 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
1da177e4 588/**
4a3ad20c
GKH
589 * platform_match - bind platform device to platform driver.
590 * @dev: device.
591 * @drv: driver.
1da177e4 592 *
4a3ad20c
GKH
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.
1da177e4 600 */
4a3ad20c 601static int platform_match(struct device *dev, struct device_driver *drv)
1da177e4 602{
71b3e0c1 603 struct platform_device *pdev = to_platform_device(dev);
57fee4a5
EM
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;
1da177e4 609
57fee4a5 610 /* fall-back to driver name match */
1e0b2cf9 611 return (strcmp(pdev->name, drv->name) == 0);
1da177e4
LT
612}
613
25e18499
RW
614#ifdef CONFIG_PM_SLEEP
615
616static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1da177e4 617{
783ea7d4
MD
618 struct platform_driver *pdrv = to_platform_driver(dev->driver);
619 struct platform_device *pdev = to_platform_device(dev);
1da177e4
LT
620 int ret = 0;
621
783ea7d4
MD
622 if (dev->driver && pdrv->suspend)
623 ret = pdrv->suspend(pdev, mesg);
386415d8
DB
624
625 return ret;
626}
627
25e18499 628static int platform_legacy_resume(struct device *dev)
1da177e4 629{
783ea7d4
MD
630 struct platform_driver *pdrv = to_platform_driver(dev->driver);
631 struct platform_device *pdev = to_platform_device(dev);
1da177e4
LT
632 int ret = 0;
633
783ea7d4
MD
634 if (dev->driver && pdrv->resume)
635 ret = pdrv->resume(pdev);
9480e307 636
1da177e4
LT
637 return ret;
638}
639
25e18499
RW
640static int platform_pm_prepare(struct device *dev)
641{
642 struct device_driver *drv = dev->driver;
643 int ret = 0;
644
645 if (drv && drv->pm && drv->pm->prepare)
646 ret = drv->pm->prepare(dev);
647
648 return ret;
649}
650
651static void platform_pm_complete(struct device *dev)
652{
653 struct device_driver *drv = dev->driver;
654
655 if (drv && drv->pm && drv->pm->complete)
656 drv->pm->complete(dev);
657}
658
659#ifdef CONFIG_SUSPEND
660
661static int platform_pm_suspend(struct device *dev)
662{
663 struct device_driver *drv = dev->driver;
664 int ret = 0;
665
adf09493
RW
666 if (!drv)
667 return 0;
668
669 if (drv->pm) {
25e18499
RW
670 if (drv->pm->suspend)
671 ret = drv->pm->suspend(dev);
672 } else {
673 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
674 }
675
676 return ret;
677}
678
679static int platform_pm_suspend_noirq(struct device *dev)
680{
adf09493 681 struct device_driver *drv = dev->driver;
25e18499
RW
682 int ret = 0;
683
adf09493 684 if (!drv)
25e18499
RW
685 return 0;
686
adf09493
RW
687 if (drv->pm) {
688 if (drv->pm->suspend_noirq)
689 ret = drv->pm->suspend_noirq(dev);
25e18499
RW
690 }
691
692 return ret;
693}
694
695static int platform_pm_resume(struct device *dev)
696{
697 struct device_driver *drv = dev->driver;
698 int ret = 0;
699
adf09493
RW
700 if (!drv)
701 return 0;
702
703 if (drv->pm) {
25e18499
RW
704 if (drv->pm->resume)
705 ret = drv->pm->resume(dev);
706 } else {
707 ret = platform_legacy_resume(dev);
708 }
709
710 return ret;
711}
712
713static int platform_pm_resume_noirq(struct device *dev)
714{
adf09493 715 struct device_driver *drv = dev->driver;
25e18499
RW
716 int ret = 0;
717
adf09493 718 if (!drv)
25e18499
RW
719 return 0;
720
adf09493
RW
721 if (drv->pm) {
722 if (drv->pm->resume_noirq)
723 ret = drv->pm->resume_noirq(dev);
25e18499
RW
724 }
725
726 return ret;
727}
728
729#else /* !CONFIG_SUSPEND */
730
731#define platform_pm_suspend NULL
732#define platform_pm_resume NULL
733#define platform_pm_suspend_noirq NULL
734#define platform_pm_resume_noirq NULL
735
736#endif /* !CONFIG_SUSPEND */
737
738#ifdef CONFIG_HIBERNATION
739
740static int platform_pm_freeze(struct device *dev)
741{
742 struct device_driver *drv = dev->driver;
743 int ret = 0;
744
745 if (!drv)
746 return 0;
747
748 if (drv->pm) {
749 if (drv->pm->freeze)
750 ret = drv->pm->freeze(dev);
751 } else {
752 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
753 }
754
755 return ret;
756}
757
758static int platform_pm_freeze_noirq(struct device *dev)
759{
adf09493 760 struct device_driver *drv = dev->driver;
25e18499
RW
761 int ret = 0;
762
adf09493 763 if (!drv)
25e18499
RW
764 return 0;
765
adf09493
RW
766 if (drv->pm) {
767 if (drv->pm->freeze_noirq)
768 ret = drv->pm->freeze_noirq(dev);
25e18499
RW
769 }
770
771 return ret;
772}
773
774static int platform_pm_thaw(struct device *dev)
775{
776 struct device_driver *drv = dev->driver;
777 int ret = 0;
778
adf09493
RW
779 if (!drv)
780 return 0;
781
782 if (drv->pm) {
25e18499
RW
783 if (drv->pm->thaw)
784 ret = drv->pm->thaw(dev);
785 } else {
786 ret = platform_legacy_resume(dev);
787 }
788
789 return ret;
790}
791
792static int platform_pm_thaw_noirq(struct device *dev)
793{
adf09493 794 struct device_driver *drv = dev->driver;
25e18499
RW
795 int ret = 0;
796
adf09493 797 if (!drv)
25e18499
RW
798 return 0;
799
adf09493
RW
800 if (drv->pm) {
801 if (drv->pm->thaw_noirq)
802 ret = drv->pm->thaw_noirq(dev);
25e18499
RW
803 }
804
805 return ret;
806}
807
808static int platform_pm_poweroff(struct device *dev)
809{
810 struct device_driver *drv = dev->driver;
811 int ret = 0;
812
adf09493
RW
813 if (!drv)
814 return 0;
815
816 if (drv->pm) {
25e18499
RW
817 if (drv->pm->poweroff)
818 ret = drv->pm->poweroff(dev);
819 } else {
820 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
821 }
822
823 return ret;
824}
825
826static int platform_pm_poweroff_noirq(struct device *dev)
827{
adf09493 828 struct device_driver *drv = dev->driver;
25e18499
RW
829 int ret = 0;
830
adf09493 831 if (!drv)
25e18499
RW
832 return 0;
833
adf09493
RW
834 if (drv->pm) {
835 if (drv->pm->poweroff_noirq)
836 ret = drv->pm->poweroff_noirq(dev);
25e18499
RW
837 }
838
839 return ret;
840}
841
842static int platform_pm_restore(struct device *dev)
843{
844 struct device_driver *drv = dev->driver;
845 int ret = 0;
846
adf09493
RW
847 if (!drv)
848 return 0;
849
850 if (drv->pm) {
25e18499
RW
851 if (drv->pm->restore)
852 ret = drv->pm->restore(dev);
853 } else {
854 ret = platform_legacy_resume(dev);
855 }
856
857 return ret;
858}
859
860static int platform_pm_restore_noirq(struct device *dev)
861{
adf09493 862 struct device_driver *drv = dev->driver;
25e18499
RW
863 int ret = 0;
864
adf09493 865 if (!drv)
25e18499
RW
866 return 0;
867
adf09493
RW
868 if (drv->pm) {
869 if (drv->pm->restore_noirq)
870 ret = drv->pm->restore_noirq(dev);
25e18499
RW
871 }
872
873 return ret;
874}
875
876#else /* !CONFIG_HIBERNATION */
877
878#define platform_pm_freeze NULL
879#define platform_pm_thaw NULL
880#define platform_pm_poweroff NULL
881#define platform_pm_restore NULL
882#define platform_pm_freeze_noirq NULL
883#define platform_pm_thaw_noirq NULL
884#define platform_pm_poweroff_noirq NULL
885#define platform_pm_restore_noirq NULL
886
887#endif /* !CONFIG_HIBERNATION */
888
d9ab7716 889static const struct dev_pm_ops platform_dev_pm_ops = {
adf09493
RW
890 .prepare = platform_pm_prepare,
891 .complete = platform_pm_complete,
892 .suspend = platform_pm_suspend,
893 .resume = platform_pm_resume,
894 .freeze = platform_pm_freeze,
895 .thaw = platform_pm_thaw,
896 .poweroff = platform_pm_poweroff,
897 .restore = platform_pm_restore,
25e18499
RW
898 .suspend_noirq = platform_pm_suspend_noirq,
899 .resume_noirq = platform_pm_resume_noirq,
900 .freeze_noirq = platform_pm_freeze_noirq,
901 .thaw_noirq = platform_pm_thaw_noirq,
902 .poweroff_noirq = platform_pm_poweroff_noirq,
903 .restore_noirq = platform_pm_restore_noirq,
904};
905
adf09493 906#define PLATFORM_PM_OPS_PTR (&platform_dev_pm_ops)
25e18499
RW
907
908#else /* !CONFIG_PM_SLEEP */
909
910#define PLATFORM_PM_OPS_PTR NULL
911
912#endif /* !CONFIG_PM_SLEEP */
913
1da177e4
LT
914struct bus_type platform_bus_type = {
915 .name = "platform",
a0245f7a 916 .dev_attrs = platform_dev_attrs,
1da177e4 917 .match = platform_match,
a0245f7a 918 .uevent = platform_uevent,
25e18499 919 .pm = PLATFORM_PM_OPS_PTR,
1da177e4 920};
a96b2042 921EXPORT_SYMBOL_GPL(platform_bus_type);
1da177e4
LT
922
923int __init platform_bus_init(void)
924{
fbfb1445
CH
925 int error;
926
13977091
MD
927 early_platform_cleanup();
928
fbfb1445
CH
929 error = device_register(&platform_bus);
930 if (error)
931 return error;
932 error = bus_register(&platform_bus_type);
933 if (error)
934 device_unregister(&platform_bus);
935 return error;
1da177e4
LT
936}
937
938#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
939u64 dma_get_required_mask(struct device *dev)
940{
941 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
942 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
943 u64 mask;
944
945 if (!high_totalram) {
946 /* convert to mask just covering totalram */
947 low_totalram = (1 << (fls(low_totalram) - 1));
948 low_totalram += low_totalram - 1;
949 mask = low_totalram;
950 } else {
951 high_totalram = (1 << (fls(high_totalram) - 1));
952 high_totalram += high_totalram - 1;
953 mask = (((u64)high_totalram) << 32) + 0xffffffff;
954 }
e88a0c2c 955 return mask;
1da177e4
LT
956}
957EXPORT_SYMBOL_GPL(dma_get_required_mask);
958#endif
13977091
MD
959
960static __initdata LIST_HEAD(early_platform_driver_list);
961static __initdata LIST_HEAD(early_platform_device_list);
962
963/**
964 * early_platform_driver_register
d86c1302 965 * @epdrv: early_platform driver structure
13977091
MD
966 * @buf: string passed from early_param()
967 */
968int __init early_platform_driver_register(struct early_platform_driver *epdrv,
969 char *buf)
970{
971 unsigned long index;
972 int n;
973
974 /* Simply add the driver to the end of the global list.
975 * Drivers will by default be put on the list in compiled-in order.
976 */
977 if (!epdrv->list.next) {
978 INIT_LIST_HEAD(&epdrv->list);
979 list_add_tail(&epdrv->list, &early_platform_driver_list);
980 }
981
982 /* If the user has specified device then make sure the driver
983 * gets prioritized. The driver of the last device specified on
984 * command line will be put first on the list.
985 */
986 n = strlen(epdrv->pdrv->driver.name);
987 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
988 list_move(&epdrv->list, &early_platform_driver_list);
989
990 if (!strcmp(buf, epdrv->pdrv->driver.name))
991 epdrv->requested_id = -1;
992 else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10,
993 &index) == 0)
994 epdrv->requested_id = index;
995 else
996 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
997 }
998
999 return 0;
1000}
1001
1002/**
1003 * early_platform_add_devices - add a numbers of early platform devices
1004 * @devs: array of early platform devices to add
1005 * @num: number of early platform devices in array
1006 */
1007void __init early_platform_add_devices(struct platform_device **devs, int num)
1008{
1009 struct device *dev;
1010 int i;
1011
1012 /* simply add the devices to list */
1013 for (i = 0; i < num; i++) {
1014 dev = &devs[i]->dev;
1015
1016 if (!dev->devres_head.next) {
1017 INIT_LIST_HEAD(&dev->devres_head);
1018 list_add_tail(&dev->devres_head,
1019 &early_platform_device_list);
1020 }
1021 }
1022}
1023
1024/**
1025 * early_platform_driver_register_all
1026 * @class_str: string to identify early platform driver class
1027 */
1028void __init early_platform_driver_register_all(char *class_str)
1029{
1030 /* The "class_str" parameter may or may not be present on the kernel
1031 * command line. If it is present then there may be more than one
1032 * matching parameter.
1033 *
1034 * Since we register our early platform drivers using early_param()
1035 * we need to make sure that they also get registered in the case
1036 * when the parameter is missing from the kernel command line.
1037 *
1038 * We use parse_early_options() to make sure the early_param() gets
1039 * called at least once. The early_param() may be called more than
1040 * once since the name of the preferred device may be specified on
1041 * the kernel command line. early_platform_driver_register() handles
1042 * this case for us.
1043 */
1044 parse_early_options(class_str);
1045}
1046
1047/**
1048 * early_platform_match
d86c1302 1049 * @epdrv: early platform driver structure
13977091
MD
1050 * @id: id to match against
1051 */
1052static __init struct platform_device *
1053early_platform_match(struct early_platform_driver *epdrv, int id)
1054{
1055 struct platform_device *pd;
1056
1057 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1058 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1059 if (pd->id == id)
1060 return pd;
1061
1062 return NULL;
1063}
1064
1065/**
1066 * early_platform_left
d86c1302 1067 * @epdrv: early platform driver structure
13977091
MD
1068 * @id: return true if id or above exists
1069 */
1070static __init int early_platform_left(struct early_platform_driver *epdrv,
1071 int id)
1072{
1073 struct platform_device *pd;
1074
1075 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1076 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1077 if (pd->id >= id)
1078 return 1;
1079
1080 return 0;
1081}
1082
1083/**
1084 * early_platform_driver_probe_id
1085 * @class_str: string to identify early platform driver class
1086 * @id: id to match against
1087 * @nr_probe: number of platform devices to successfully probe before exiting
1088 */
1089static int __init early_platform_driver_probe_id(char *class_str,
1090 int id,
1091 int nr_probe)
1092{
1093 struct early_platform_driver *epdrv;
1094 struct platform_device *match;
1095 int match_id;
1096 int n = 0;
1097 int left = 0;
1098
1099 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1100 /* only use drivers matching our class_str */
1101 if (strcmp(class_str, epdrv->class_str))
1102 continue;
1103
1104 if (id == -2) {
1105 match_id = epdrv->requested_id;
1106 left = 1;
1107
1108 } else {
1109 match_id = id;
1110 left += early_platform_left(epdrv, id);
1111
1112 /* skip requested id */
1113 switch (epdrv->requested_id) {
1114 case EARLY_PLATFORM_ID_ERROR:
1115 case EARLY_PLATFORM_ID_UNSET:
1116 break;
1117 default:
1118 if (epdrv->requested_id == id)
1119 match_id = EARLY_PLATFORM_ID_UNSET;
1120 }
1121 }
1122
1123 switch (match_id) {
1124 case EARLY_PLATFORM_ID_ERROR:
1125 pr_warning("%s: unable to parse %s parameter\n",
1126 class_str, epdrv->pdrv->driver.name);
1127 /* fall-through */
1128 case EARLY_PLATFORM_ID_UNSET:
1129 match = NULL;
1130 break;
1131 default:
1132 match = early_platform_match(epdrv, match_id);
1133 }
1134
1135 if (match) {
1136 if (epdrv->pdrv->probe(match))
1137 pr_warning("%s: unable to probe %s early.\n",
1138 class_str, match->name);
1139 else
1140 n++;
1141 }
1142
1143 if (n >= nr_probe)
1144 break;
1145 }
1146
1147 if (left)
1148 return n;
1149 else
1150 return -ENODEV;
1151}
1152
1153/**
1154 * early_platform_driver_probe
1155 * @class_str: string to identify early platform driver class
1156 * @nr_probe: number of platform devices to successfully probe before exiting
1157 * @user_only: only probe user specified early platform devices
1158 */
1159int __init early_platform_driver_probe(char *class_str,
1160 int nr_probe,
1161 int user_only)
1162{
1163 int k, n, i;
1164
1165 n = 0;
1166 for (i = -2; n < nr_probe; i++) {
1167 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1168
1169 if (k < 0)
1170 break;
1171
1172 n += k;
1173
1174 if (user_only)
1175 break;
1176 }
1177
1178 return n;
1179}
1180
1181/**
1182 * early_platform_cleanup - clean up early platform code
1183 */
1184void __init early_platform_cleanup(void)
1185{
1186 struct platform_device *pd, *pd2;
1187
1188 /* clean up the devres list used to chain devices */
1189 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1190 dev.devres_head) {
1191 list_del(&pd->dev.devres_head);
1192 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1193 }
1194}
1195
This page took 0.543411 seconds and 5 git commands to generate.