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