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