blob: f8071976fe07591c0def1cb2a8d4de18d94b219d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Russell Kingd052d1b2005-10-29 19:07:23 +010013#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010021#include "base.h"
22
Russell King00d3dcd2005-11-09 17:23:39 +000023#define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct device platform_bus = {
26 .bus_id = "platform",
27};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050028EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/**
31 * platform_get_resource - get a resource for a device
32 * @dev: platform device
33 * @type: resource type
34 * @num: resource index
35 */
36struct resource *
37platform_get_resource(struct platform_device *dev, unsigned int type,
38 unsigned int num)
39{
40 int i;
41
42 for (i = 0; i < dev->num_resources; i++) {
43 struct resource *r = &dev->resource[i];
44
45 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
46 IORESOURCE_IRQ|IORESOURCE_DMA))
47 == type)
48 if (num-- == 0)
49 return r;
50 }
51 return NULL;
52}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050053EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/**
56 * platform_get_irq - get an IRQ for a device
57 * @dev: platform device
58 * @num: IRQ number index
59 */
60int platform_get_irq(struct platform_device *dev, unsigned int num)
61{
62 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
63
David Vrabel305b3222006-01-19 17:52:27 +000064 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050066EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/**
69 * platform_get_resource_byname - get a resource for a device by name
70 * @dev: platform device
71 * @type: resource type
72 * @name: resource name
73 */
74struct resource *
75platform_get_resource_byname(struct platform_device *dev, unsigned int type,
76 char *name)
77{
78 int i;
79
80 for (i = 0; i < dev->num_resources; i++) {
81 struct resource *r = &dev->resource[i];
82
83 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
84 IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
85 if (!strcmp(r->name, name))
86 return r;
87 }
88 return NULL;
89}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050090EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/**
93 * platform_get_irq - get an IRQ for a device
94 * @dev: platform device
95 * @name: IRQ name
96 */
97int platform_get_irq_byname(struct platform_device *dev, char *name)
98{
99 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
100
David Vrabel305b3222006-01-19 17:52:27 +0000101 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500103EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/**
106 * platform_add_devices - add a numbers of platform devices
107 * @devs: array of platform devices to add
108 * @num: number of platform devices in array
109 */
110int platform_add_devices(struct platform_device **devs, int num)
111{
112 int i, ret = 0;
113
114 for (i = 0; i < num; i++) {
115 ret = platform_device_register(devs[i]);
116 if (ret) {
117 while (--i >= 0)
118 platform_device_unregister(devs[i]);
119 break;
120 }
121 }
122
123 return ret;
124}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500125EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Russell King37c12e72005-11-05 21:19:33 +0000127struct platform_object {
128 struct platform_device pdev;
129 char name[1];
130};
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/**
Russell King37c12e72005-11-05 21:19:33 +0000133 * platform_device_put
134 * @pdev: platform device to free
135 *
136 * Free all memory associated with a platform device. This function
137 * must _only_ be externally called in error cases. All other usage
138 * is a bug.
139 */
140void platform_device_put(struct platform_device *pdev)
141{
142 if (pdev)
143 put_device(&pdev->dev);
144}
145EXPORT_SYMBOL_GPL(platform_device_put);
146
147static void platform_device_release(struct device *dev)
148{
149 struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
150
151 kfree(pa->pdev.dev.platform_data);
152 kfree(pa->pdev.resource);
153 kfree(pa);
154}
155
156/**
157 * platform_device_alloc
158 * @name: base name of the device we're adding
159 * @id: instance id
160 *
161 * Create a platform device object which can have other objects attached
162 * to it, and which will have attached objects freed when it is released.
163 */
164struct platform_device *platform_device_alloc(const char *name, unsigned int id)
165{
166 struct platform_object *pa;
167
168 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
169 if (pa) {
170 strcpy(pa->name, name);
171 pa->pdev.name = pa->name;
172 pa->pdev.id = id;
173 device_initialize(&pa->pdev.dev);
174 pa->pdev.dev.release = platform_device_release;
175 }
176
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500177 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000178}
179EXPORT_SYMBOL_GPL(platform_device_alloc);
180
181/**
182 * platform_device_add_resources
183 * @pdev: platform device allocated by platform_device_alloc to add resources to
184 * @res: set of resources that needs to be allocated for the device
185 * @num: number of resources
186 *
187 * Add a copy of the resources to the platform device. The memory
188 * associated with the resources will be freed when the platform
189 * device is released.
190 */
191int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
192{
193 struct resource *r;
194
195 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
196 if (r) {
197 memcpy(r, res, sizeof(struct resource) * num);
198 pdev->resource = r;
199 pdev->num_resources = num;
200 }
201 return r ? 0 : -ENOMEM;
202}
203EXPORT_SYMBOL_GPL(platform_device_add_resources);
204
205/**
206 * platform_device_add_data
207 * @pdev: platform device allocated by platform_device_alloc to add resources to
208 * @data: platform specific data for this platform device
209 * @size: size of platform specific data
210 *
211 * Add a copy of platform specific data to the platform device's platform_data
212 * pointer. The memory associated with the platform data will be freed
213 * when the platform device is released.
214 */
215int platform_device_add_data(struct platform_device *pdev, void *data, size_t size)
216{
217 void *d;
218
219 d = kmalloc(size, GFP_KERNEL);
220 if (d) {
221 memcpy(d, data, size);
222 pdev->dev.platform_data = d;
223 }
224 return d ? 0 : -ENOMEM;
225}
226EXPORT_SYMBOL_GPL(platform_device_add_data);
227
228/**
229 * platform_device_add - add a platform device to device hierarchy
Martin Waitz67be2dd2005-05-01 08:59:26 -0700230 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *
Russell King37c12e72005-11-05 21:19:33 +0000232 * This is part 2 of platform_device_register(), though may be called
233 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Russell King37c12e72005-11-05 21:19:33 +0000235int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i, ret = 0;
238
239 if (!pdev)
240 return -EINVAL;
241
242 if (!pdev->dev.parent)
243 pdev->dev.parent = &platform_bus;
244
245 pdev->dev.bus = &platform_bus_type;
246
247 if (pdev->id != -1)
248 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
249 else
250 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
251
252 for (i = 0; i < pdev->num_resources; i++) {
253 struct resource *p, *r = &pdev->resource[i];
254
255 if (r->name == NULL)
256 r->name = pdev->dev.bus_id;
257
258 p = r->parent;
259 if (!p) {
260 if (r->flags & IORESOURCE_MEM)
261 p = &iomem_resource;
262 else if (r->flags & IORESOURCE_IO)
263 p = &ioport_resource;
264 }
265
Kumar Galad960bb42005-11-28 10:15:39 -0600266 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_ERR
268 "%s: failed to claim resource %d\n",
269 pdev->dev.bus_id, i);
270 ret = -EBUSY;
271 goto failed;
272 }
273 }
274
275 pr_debug("Registering platform device '%s'. Parent at %s\n",
276 pdev->dev.bus_id, pdev->dev.parent->bus_id);
277
278 ret = device_register(&pdev->dev);
279 if (ret == 0)
280 return ret;
281
282 failed:
283 while (--i >= 0)
284 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
285 release_resource(&pdev->resource[i]);
286 return ret;
287}
Russell King37c12e72005-11-05 21:19:33 +0000288EXPORT_SYMBOL_GPL(platform_device_add);
289
290/**
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500291 * platform_device_del - remove a platform-level device
292 * @pdev: platform device we're removing
293 *
294 * Note that this function will also release all memory- and port-based
295 * resources owned by the device (@dev->resource).
296 */
297void platform_device_del(struct platform_device *pdev)
298{
299 int i;
300
301 if (pdev) {
302 for (i = 0; i < pdev->num_resources; i++) {
303 struct resource *r = &pdev->resource[i];
304 if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
305 release_resource(r);
306 }
307
308 device_del(&pdev->dev);
309 }
310}
311EXPORT_SYMBOL_GPL(platform_device_del);
312
313/**
Russell King37c12e72005-11-05 21:19:33 +0000314 * platform_device_register - add a platform-level device
315 * @pdev: platform device we're adding
316 *
317 */
318int platform_device_register(struct platform_device * pdev)
319{
320 device_initialize(&pdev->dev);
321 return platform_device_add(pdev);
322}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500323EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325/**
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500326 * platform_device_unregister - unregister a platform-level device
327 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *
Uwe Zeisberger80682fa2006-03-22 00:21:33 +0100329 * Unregistration is done in 2 steps. First we release all resources
Jean Delvare2d7b5a72005-12-27 19:45:58 +0100330 * and remove it from the subsystem, then we drop reference count by
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500331 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
333void platform_device_unregister(struct platform_device * pdev)
334{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500335 platform_device_del(pdev);
336 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500338EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/**
341 * platform_device_register_simple
342 * @name: base name of the device we're adding
343 * @id: instance id
344 * @res: set of resources that needs to be allocated for the device
345 * @num: number of resources
346 *
347 * This function creates a simple platform device that requires minimal
348 * resource and memory management. Canned release function freeing
349 * memory allocated for the device allows drivers using such devices
350 * to be unloaded iwithout waiting for the last reference to the device
351 * to be dropped.
352 */
353struct platform_device *platform_device_register_simple(char *name, unsigned int id,
354 struct resource *res, unsigned int num)
355{
Russell King37c12e72005-11-05 21:19:33 +0000356 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int retval;
358
Russell King37c12e72005-11-05 21:19:33 +0000359 pdev = platform_device_alloc(name, id);
360 if (!pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 retval = -ENOMEM;
362 goto error;
363 }
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (num) {
Russell King37c12e72005-11-05 21:19:33 +0000366 retval = platform_device_add_resources(pdev, res, num);
367 if (retval)
368 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
Russell King37c12e72005-11-05 21:19:33 +0000371 retval = platform_device_add(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (retval)
373 goto error;
374
Russell King37c12e72005-11-05 21:19:33 +0000375 return pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377error:
Russell King37c12e72005-11-05 21:19:33 +0000378 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return ERR_PTR(retval);
380}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500381EXPORT_SYMBOL_GPL(platform_device_register_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Russell King00d3dcd2005-11-09 17:23:39 +0000383static int platform_drv_probe(struct device *_dev)
384{
385 struct platform_driver *drv = to_platform_driver(_dev->driver);
386 struct platform_device *dev = to_platform_device(_dev);
387
388 return drv->probe(dev);
389}
390
391static int platform_drv_remove(struct device *_dev)
392{
393 struct platform_driver *drv = to_platform_driver(_dev->driver);
394 struct platform_device *dev = to_platform_device(_dev);
395
396 return drv->remove(dev);
397}
398
399static void platform_drv_shutdown(struct device *_dev)
400{
401 struct platform_driver *drv = to_platform_driver(_dev->driver);
402 struct platform_device *dev = to_platform_device(_dev);
403
404 drv->shutdown(dev);
405}
406
407static int platform_drv_suspend(struct device *_dev, pm_message_t state)
408{
409 struct platform_driver *drv = to_platform_driver(_dev->driver);
410 struct platform_device *dev = to_platform_device(_dev);
411
412 return drv->suspend(dev, state);
413}
414
415static int platform_drv_resume(struct device *_dev)
416{
417 struct platform_driver *drv = to_platform_driver(_dev->driver);
418 struct platform_device *dev = to_platform_device(_dev);
419
420 return drv->resume(dev);
421}
422
423/**
424 * platform_driver_register
425 * @drv: platform driver structure
426 */
427int platform_driver_register(struct platform_driver *drv)
428{
429 drv->driver.bus = &platform_bus_type;
430 if (drv->probe)
431 drv->driver.probe = platform_drv_probe;
432 if (drv->remove)
433 drv->driver.remove = platform_drv_remove;
434 if (drv->shutdown)
435 drv->driver.shutdown = platform_drv_shutdown;
436 if (drv->suspend)
437 drv->driver.suspend = platform_drv_suspend;
438 if (drv->resume)
439 drv->driver.resume = platform_drv_resume;
440 return driver_register(&drv->driver);
441}
442EXPORT_SYMBOL_GPL(platform_driver_register);
443
444/**
445 * platform_driver_unregister
446 * @drv: platform driver structure
447 */
448void platform_driver_unregister(struct platform_driver *drv)
449{
450 driver_unregister(&drv->driver);
451}
452EXPORT_SYMBOL_GPL(platform_driver_unregister);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
David Brownella0245f72006-05-29 10:37:33 -0700455/* modalias support enables more hands-off userspace setup:
456 * (a) environment variable lets new-style hotplug events work once system is
457 * fully running: "modprobe $MODALIAS"
458 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
459 * mishandled before system is fully running: "modprobe $(cat modalias)"
460 */
461static ssize_t
462modalias_show(struct device *dev, struct device_attribute *a, char *buf)
463{
464 struct platform_device *pdev = to_platform_device(dev);
465 int len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->name);
466
467 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
468}
469
470static struct device_attribute platform_dev_attrs[] = {
471 __ATTR_RO(modalias),
472 __ATTR_NULL,
473};
474
475static int platform_uevent(struct device *dev, char **envp, int num_envp,
476 char *buffer, int buffer_size)
477{
478 struct platform_device *pdev = to_platform_device(dev);
479
480 envp[0] = buffer;
481 snprintf(buffer, buffer_size, "MODALIAS=%s", pdev->name);
482 return 0;
483}
484
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/**
487 * platform_match - bind platform device to platform driver.
488 * @dev: device.
489 * @drv: driver.
490 *
491 * Platform device IDs are assumed to be encoded like this:
492 * "<name><instance>", where <name> is a short description of the
493 * type of device, like "pci" or "floppy", and <instance> is the
494 * enumerated instance of the device, like '0' or '42'.
495 * Driver IDs are simply "<name>".
496 * So, extract the <name> from the platform_device structure,
497 * and compare it against the name of the driver. Return whether
498 * they match or not.
499 */
500
501static int platform_match(struct device * dev, struct device_driver * drv)
502{
503 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
504
505 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
506}
507
508static int platform_suspend(struct device * dev, pm_message_t state)
509{
510 int ret = 0;
511
Russell King9480e302005-10-28 09:52:56 -0700512 if (dev->driver && dev->driver->suspend)
513 ret = dev->driver->suspend(dev, state);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return ret;
516}
517
518static int platform_resume(struct device * dev)
519{
520 int ret = 0;
521
Russell King9480e302005-10-28 09:52:56 -0700522 if (dev->driver && dev->driver->resume)
523 ret = dev->driver->resume(dev);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return ret;
526}
527
528struct bus_type platform_bus_type = {
529 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700530 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700532 .uevent = platform_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 .suspend = platform_suspend,
534 .resume = platform_resume,
535};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500536EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538int __init platform_bus_init(void)
539{
540 device_register(&platform_bus);
541 return bus_register(&platform_bus_type);
542}
543
544#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
545u64 dma_get_required_mask(struct device *dev)
546{
547 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
548 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
549 u64 mask;
550
551 if (!high_totalram) {
552 /* convert to mask just covering totalram */
553 low_totalram = (1 << (fls(low_totalram) - 1));
554 low_totalram += low_totalram - 1;
555 mask = low_totalram;
556 } else {
557 high_totalram = (1 << (fls(high_totalram) - 1));
558 high_totalram += high_totalram - 1;
559 mask = (((u64)high_totalram) << 32) + 0xffffffff;
560 }
561 return mask & *dev->dma_mask;
562}
563EXPORT_SYMBOL_GPL(dma_get_required_mask);
564#endif