blob: 95ecfc490d543f260f0ce20eb2cbdfa5b78a0636 [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};
28
29/**
30 * platform_get_resource - get a resource for a device
31 * @dev: platform device
32 * @type: resource type
33 * @num: resource index
34 */
35struct resource *
36platform_get_resource(struct platform_device *dev, unsigned int type,
37 unsigned int num)
38{
39 int i;
40
41 for (i = 0; i < dev->num_resources; i++) {
42 struct resource *r = &dev->resource[i];
43
44 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
45 IORESOURCE_IRQ|IORESOURCE_DMA))
46 == type)
47 if (num-- == 0)
48 return r;
49 }
50 return NULL;
51}
52
53/**
54 * platform_get_irq - get an IRQ for a device
55 * @dev: platform device
56 * @num: IRQ number index
57 */
58int platform_get_irq(struct platform_device *dev, unsigned int num)
59{
60 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
61
62 return r ? r->start : 0;
63}
64
65/**
66 * platform_get_resource_byname - get a resource for a device by name
67 * @dev: platform device
68 * @type: resource type
69 * @name: resource name
70 */
71struct resource *
72platform_get_resource_byname(struct platform_device *dev, unsigned int type,
73 char *name)
74{
75 int i;
76
77 for (i = 0; i < dev->num_resources; i++) {
78 struct resource *r = &dev->resource[i];
79
80 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
81 IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
82 if (!strcmp(r->name, name))
83 return r;
84 }
85 return NULL;
86}
87
88/**
89 * platform_get_irq - get an IRQ for a device
90 * @dev: platform device
91 * @name: IRQ name
92 */
93int platform_get_irq_byname(struct platform_device *dev, char *name)
94{
95 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
96
97 return r ? r->start : 0;
98}
99
100/**
101 * platform_add_devices - add a numbers of platform devices
102 * @devs: array of platform devices to add
103 * @num: number of platform devices in array
104 */
105int platform_add_devices(struct platform_device **devs, int num)
106{
107 int i, ret = 0;
108
109 for (i = 0; i < num; i++) {
110 ret = platform_device_register(devs[i]);
111 if (ret) {
112 while (--i >= 0)
113 platform_device_unregister(devs[i]);
114 break;
115 }
116 }
117
118 return ret;
119}
120
Russell King37c12e72005-11-05 21:19:33 +0000121struct platform_object {
122 struct platform_device pdev;
123 char name[1];
124};
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/**
Russell King37c12e72005-11-05 21:19:33 +0000127 * platform_device_put
128 * @pdev: platform device to free
129 *
130 * Free all memory associated with a platform device. This function
131 * must _only_ be externally called in error cases. All other usage
132 * is a bug.
133 */
134void platform_device_put(struct platform_device *pdev)
135{
136 if (pdev)
137 put_device(&pdev->dev);
138}
139EXPORT_SYMBOL_GPL(platform_device_put);
140
141static void platform_device_release(struct device *dev)
142{
143 struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
144
145 kfree(pa->pdev.dev.platform_data);
146 kfree(pa->pdev.resource);
147 kfree(pa);
148}
149
150/**
151 * platform_device_alloc
152 * @name: base name of the device we're adding
153 * @id: instance id
154 *
155 * Create a platform device object which can have other objects attached
156 * to it, and which will have attached objects freed when it is released.
157 */
158struct platform_device *platform_device_alloc(const char *name, unsigned int id)
159{
160 struct platform_object *pa;
161
162 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
163 if (pa) {
164 strcpy(pa->name, name);
165 pa->pdev.name = pa->name;
166 pa->pdev.id = id;
167 device_initialize(&pa->pdev.dev);
168 pa->pdev.dev.release = platform_device_release;
169 }
170
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500171 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000172}
173EXPORT_SYMBOL_GPL(platform_device_alloc);
174
175/**
176 * platform_device_add_resources
177 * @pdev: platform device allocated by platform_device_alloc to add resources to
178 * @res: set of resources that needs to be allocated for the device
179 * @num: number of resources
180 *
181 * Add a copy of the resources to the platform device. The memory
182 * associated with the resources will be freed when the platform
183 * device is released.
184 */
185int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
186{
187 struct resource *r;
188
189 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
190 if (r) {
191 memcpy(r, res, sizeof(struct resource) * num);
192 pdev->resource = r;
193 pdev->num_resources = num;
194 }
195 return r ? 0 : -ENOMEM;
196}
197EXPORT_SYMBOL_GPL(platform_device_add_resources);
198
199/**
200 * platform_device_add_data
201 * @pdev: platform device allocated by platform_device_alloc to add resources to
202 * @data: platform specific data for this platform device
203 * @size: size of platform specific data
204 *
205 * Add a copy of platform specific data to the platform device's platform_data
206 * pointer. The memory associated with the platform data will be freed
207 * when the platform device is released.
208 */
209int platform_device_add_data(struct platform_device *pdev, void *data, size_t size)
210{
211 void *d;
212
213 d = kmalloc(size, GFP_KERNEL);
214 if (d) {
215 memcpy(d, data, size);
216 pdev->dev.platform_data = d;
217 }
218 return d ? 0 : -ENOMEM;
219}
220EXPORT_SYMBOL_GPL(platform_device_add_data);
221
222/**
223 * platform_device_add - add a platform device to device hierarchy
Martin Waitz67be2dd2005-05-01 08:59:26 -0700224 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *
Russell King37c12e72005-11-05 21:19:33 +0000226 * This is part 2 of platform_device_register(), though may be called
227 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Russell King37c12e72005-11-05 21:19:33 +0000229int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 int i, ret = 0;
232
233 if (!pdev)
234 return -EINVAL;
235
236 if (!pdev->dev.parent)
237 pdev->dev.parent = &platform_bus;
238
239 pdev->dev.bus = &platform_bus_type;
240
241 if (pdev->id != -1)
242 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
243 else
244 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
245
246 for (i = 0; i < pdev->num_resources; i++) {
247 struct resource *p, *r = &pdev->resource[i];
248
249 if (r->name == NULL)
250 r->name = pdev->dev.bus_id;
251
252 p = r->parent;
253 if (!p) {
254 if (r->flags & IORESOURCE_MEM)
255 p = &iomem_resource;
256 else if (r->flags & IORESOURCE_IO)
257 p = &ioport_resource;
258 }
259
Kumar Galad960bb42005-11-28 10:15:39 -0600260 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 printk(KERN_ERR
262 "%s: failed to claim resource %d\n",
263 pdev->dev.bus_id, i);
264 ret = -EBUSY;
265 goto failed;
266 }
267 }
268
269 pr_debug("Registering platform device '%s'. Parent at %s\n",
270 pdev->dev.bus_id, pdev->dev.parent->bus_id);
271
272 ret = device_register(&pdev->dev);
273 if (ret == 0)
274 return ret;
275
276 failed:
277 while (--i >= 0)
278 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
279 release_resource(&pdev->resource[i]);
280 return ret;
281}
Russell King37c12e72005-11-05 21:19:33 +0000282EXPORT_SYMBOL_GPL(platform_device_add);
283
284/**
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500285 * platform_device_del - remove a platform-level device
286 * @pdev: platform device we're removing
287 *
288 * Note that this function will also release all memory- and port-based
289 * resources owned by the device (@dev->resource).
290 */
291void platform_device_del(struct platform_device *pdev)
292{
293 int i;
294
295 if (pdev) {
296 for (i = 0; i < pdev->num_resources; i++) {
297 struct resource *r = &pdev->resource[i];
298 if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
299 release_resource(r);
300 }
301
302 device_del(&pdev->dev);
303 }
304}
305EXPORT_SYMBOL_GPL(platform_device_del);
306
307/**
Russell King37c12e72005-11-05 21:19:33 +0000308 * platform_device_register - add a platform-level device
309 * @pdev: platform device we're adding
310 *
311 */
312int platform_device_register(struct platform_device * pdev)
313{
314 device_initialize(&pdev->dev);
315 return platform_device_add(pdev);
316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318/**
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500319 * platform_device_unregister - unregister a platform-level device
320 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 *
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500322 * Unregistration is done in 2 steps. Fisrt we release all resources
323 * and remove it from the sybsystem, then we drop reference count by
324 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326void platform_device_unregister(struct platform_device * pdev)
327{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500328 platform_device_del(pdev);
329 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/**
333 * platform_device_register_simple
334 * @name: base name of the device we're adding
335 * @id: instance id
336 * @res: set of resources that needs to be allocated for the device
337 * @num: number of resources
338 *
339 * This function creates a simple platform device that requires minimal
340 * resource and memory management. Canned release function freeing
341 * memory allocated for the device allows drivers using such devices
342 * to be unloaded iwithout waiting for the last reference to the device
343 * to be dropped.
344 */
345struct platform_device *platform_device_register_simple(char *name, unsigned int id,
346 struct resource *res, unsigned int num)
347{
Russell King37c12e72005-11-05 21:19:33 +0000348 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int retval;
350
Russell King37c12e72005-11-05 21:19:33 +0000351 pdev = platform_device_alloc(name, id);
352 if (!pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 retval = -ENOMEM;
354 goto error;
355 }
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (num) {
Russell King37c12e72005-11-05 21:19:33 +0000358 retval = platform_device_add_resources(pdev, res, num);
359 if (retval)
360 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Russell King37c12e72005-11-05 21:19:33 +0000363 retval = platform_device_add(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (retval)
365 goto error;
366
Russell King37c12e72005-11-05 21:19:33 +0000367 return pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369error:
Russell King37c12e72005-11-05 21:19:33 +0000370 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return ERR_PTR(retval);
372}
373
Russell King00d3dcd2005-11-09 17:23:39 +0000374static int platform_drv_probe(struct device *_dev)
375{
376 struct platform_driver *drv = to_platform_driver(_dev->driver);
377 struct platform_device *dev = to_platform_device(_dev);
378
379 return drv->probe(dev);
380}
381
382static int platform_drv_remove(struct device *_dev)
383{
384 struct platform_driver *drv = to_platform_driver(_dev->driver);
385 struct platform_device *dev = to_platform_device(_dev);
386
387 return drv->remove(dev);
388}
389
390static void platform_drv_shutdown(struct device *_dev)
391{
392 struct platform_driver *drv = to_platform_driver(_dev->driver);
393 struct platform_device *dev = to_platform_device(_dev);
394
395 drv->shutdown(dev);
396}
397
398static int platform_drv_suspend(struct device *_dev, pm_message_t state)
399{
400 struct platform_driver *drv = to_platform_driver(_dev->driver);
401 struct platform_device *dev = to_platform_device(_dev);
402
403 return drv->suspend(dev, state);
404}
405
406static int platform_drv_resume(struct device *_dev)
407{
408 struct platform_driver *drv = to_platform_driver(_dev->driver);
409 struct platform_device *dev = to_platform_device(_dev);
410
411 return drv->resume(dev);
412}
413
414/**
415 * platform_driver_register
416 * @drv: platform driver structure
417 */
418int platform_driver_register(struct platform_driver *drv)
419{
420 drv->driver.bus = &platform_bus_type;
421 if (drv->probe)
422 drv->driver.probe = platform_drv_probe;
423 if (drv->remove)
424 drv->driver.remove = platform_drv_remove;
425 if (drv->shutdown)
426 drv->driver.shutdown = platform_drv_shutdown;
427 if (drv->suspend)
428 drv->driver.suspend = platform_drv_suspend;
429 if (drv->resume)
430 drv->driver.resume = platform_drv_resume;
431 return driver_register(&drv->driver);
432}
433EXPORT_SYMBOL_GPL(platform_driver_register);
434
435/**
436 * platform_driver_unregister
437 * @drv: platform driver structure
438 */
439void platform_driver_unregister(struct platform_driver *drv)
440{
441 driver_unregister(&drv->driver);
442}
443EXPORT_SYMBOL_GPL(platform_driver_unregister);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446/**
447 * platform_match - bind platform device to platform driver.
448 * @dev: device.
449 * @drv: driver.
450 *
451 * Platform device IDs are assumed to be encoded like this:
452 * "<name><instance>", where <name> is a short description of the
453 * type of device, like "pci" or "floppy", and <instance> is the
454 * enumerated instance of the device, like '0' or '42'.
455 * Driver IDs are simply "<name>".
456 * So, extract the <name> from the platform_device structure,
457 * and compare it against the name of the driver. Return whether
458 * they match or not.
459 */
460
461static int platform_match(struct device * dev, struct device_driver * drv)
462{
463 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
464
465 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
466}
467
468static int platform_suspend(struct device * dev, pm_message_t state)
469{
470 int ret = 0;
471
Russell King9480e302005-10-28 09:52:56 -0700472 if (dev->driver && dev->driver->suspend)
473 ret = dev->driver->suspend(dev, state);
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return ret;
476}
477
478static int platform_resume(struct device * dev)
479{
480 int ret = 0;
481
Russell King9480e302005-10-28 09:52:56 -0700482 if (dev->driver && dev->driver->resume)
483 ret = dev->driver->resume(dev);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return ret;
486}
487
488struct bus_type platform_bus_type = {
489 .name = "platform",
490 .match = platform_match,
491 .suspend = platform_suspend,
492 .resume = platform_resume,
493};
494
495int __init platform_bus_init(void)
496{
497 device_register(&platform_bus);
498 return bus_register(&platform_bus_type);
499}
500
501#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
502u64 dma_get_required_mask(struct device *dev)
503{
504 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
505 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
506 u64 mask;
507
508 if (!high_totalram) {
509 /* convert to mask just covering totalram */
510 low_totalram = (1 << (fls(low_totalram) - 1));
511 low_totalram += low_totalram - 1;
512 mask = low_totalram;
513 } else {
514 high_totalram = (1 << (fls(high_totalram) - 1));
515 high_totalram += high_totalram - 1;
516 mask = (((u64)high_totalram) << 32) + 0xffffffff;
517 }
518 return mask & *dev->dma_mask;
519}
520EXPORT_SYMBOL_GPL(dma_get_required_mask);
521#endif
522
523EXPORT_SYMBOL_GPL(platform_bus);
524EXPORT_SYMBOL_GPL(platform_bus_type);
Robert Schwebel46ea0d62005-04-18 21:57:32 -0700525EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526EXPORT_SYMBOL_GPL(platform_device_register);
527EXPORT_SYMBOL_GPL(platform_device_register_simple);
528EXPORT_SYMBOL_GPL(platform_device_unregister);
529EXPORT_SYMBOL_GPL(platform_get_irq);
530EXPORT_SYMBOL_GPL(platform_get_resource);
531EXPORT_SYMBOL_GPL(platform_get_irq_byname);
532EXPORT_SYMBOL_GPL(platform_get_resource_byname);