]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/base/platform.c
Merge Paulus' tree
[karo-tx-linux.git] / drivers / base / platform.c
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
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/bootmem.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20
21 #include "base.h"
22
23 struct device platform_bus = {
24         .bus_id         = "platform",
25 };
26
27 /**
28  *      platform_get_resource - get a resource for a device
29  *      @dev: platform device
30  *      @type: resource type
31  *      @num: resource index
32  */
33 struct resource *
34 platform_get_resource(struct platform_device *dev, unsigned int type,
35                       unsigned int num)
36 {
37         int i;
38
39         for (i = 0; i < dev->num_resources; i++) {
40                 struct resource *r = &dev->resource[i];
41
42                 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
43                                  IORESOURCE_IRQ|IORESOURCE_DMA))
44                     == type)
45                         if (num-- == 0)
46                                 return r;
47         }
48         return NULL;
49 }
50
51 /**
52  *      platform_get_irq - get an IRQ for a device
53  *      @dev: platform device
54  *      @num: IRQ number index
55  */
56 int platform_get_irq(struct platform_device *dev, unsigned int num)
57 {
58         struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
59
60         return r ? r->start : 0;
61 }
62
63 /**
64  *      platform_get_resource_byname - get a resource for a device by name
65  *      @dev: platform device
66  *      @type: resource type
67  *      @name: resource name
68  */
69 struct resource *
70 platform_get_resource_byname(struct platform_device *dev, unsigned int type,
71                       char *name)
72 {
73         int i;
74
75         for (i = 0; i < dev->num_resources; i++) {
76                 struct resource *r = &dev->resource[i];
77
78                 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
79                                  IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
80                         if (!strcmp(r->name, name))
81                                 return r;
82         }
83         return NULL;
84 }
85
86 /**
87  *      platform_get_irq - get an IRQ for a device
88  *      @dev: platform device
89  *      @name: IRQ name
90  */
91 int platform_get_irq_byname(struct platform_device *dev, char *name)
92 {
93         struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
94
95         return r ? r->start : 0;
96 }
97
98 /**
99  *      platform_add_devices - add a numbers of platform devices
100  *      @devs: array of platform devices to add
101  *      @num: number of platform devices in array
102  */
103 int platform_add_devices(struct platform_device **devs, int num)
104 {
105         int i, ret = 0;
106
107         for (i = 0; i < num; i++) {
108                 ret = platform_device_register(devs[i]);
109                 if (ret) {
110                         while (--i >= 0)
111                                 platform_device_unregister(devs[i]);
112                         break;
113                 }
114         }
115
116         return ret;
117 }
118
119 /**
120  *      platform_device_register - add a platform-level device
121  *      @pdev:  platform device we're adding
122  *
123  */
124 int platform_device_register(struct platform_device * pdev)
125 {
126         int i, ret = 0;
127
128         if (!pdev)
129                 return -EINVAL;
130
131         if (!pdev->dev.parent)
132                 pdev->dev.parent = &platform_bus;
133
134         pdev->dev.bus = &platform_bus_type;
135
136         if (pdev->id != -1)
137                 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
138         else
139                 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
140
141         for (i = 0; i < pdev->num_resources; i++) {
142                 struct resource *p, *r = &pdev->resource[i];
143
144                 if (r->name == NULL)
145                         r->name = pdev->dev.bus_id;
146
147                 p = r->parent;
148                 if (!p) {
149                         if (r->flags & IORESOURCE_MEM)
150                                 p = &iomem_resource;
151                         else if (r->flags & IORESOURCE_IO)
152                                 p = &ioport_resource;
153                 }
154
155                 if (p && request_resource(p, r)) {
156                         printk(KERN_ERR
157                                "%s: failed to claim resource %d\n",
158                                pdev->dev.bus_id, i);
159                         ret = -EBUSY;
160                         goto failed;
161                 }
162         }
163
164         pr_debug("Registering platform device '%s'. Parent at %s\n",
165                  pdev->dev.bus_id, pdev->dev.parent->bus_id);
166
167         ret = device_register(&pdev->dev);
168         if (ret == 0)
169                 return ret;
170
171  failed:
172         while (--i >= 0)
173                 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
174                         release_resource(&pdev->resource[i]);
175         return ret;
176 }
177
178 /**
179  *      platform_device_unregister - remove a platform-level device
180  *      @pdev:  platform device we're removing
181  *
182  *      Note that this function will also release all memory- and port-based
183  *      resources owned by the device (@dev->resource).
184  */
185 void platform_device_unregister(struct platform_device * pdev)
186 {
187         int i;
188
189         if (pdev) {
190                 for (i = 0; i < pdev->num_resources; i++) {
191                         struct resource *r = &pdev->resource[i];
192                         if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
193                                 release_resource(r);
194                 }
195
196                 device_unregister(&pdev->dev);
197         }
198 }
199
200 struct platform_object {
201         struct platform_device pdev;
202         struct resource resources[0];
203 };
204
205 static void platform_device_release_simple(struct device *dev)
206 {
207         struct platform_device *pdev = to_platform_device(dev);
208
209         kfree(container_of(pdev, struct platform_object, pdev));
210 }
211
212 /**
213  *      platform_device_register_simple
214  *      @name:  base name of the device we're adding
215  *      @id:    instance id
216  *      @res:   set of resources that needs to be allocated for the device
217  *      @num:   number of resources
218  *
219  *      This function creates a simple platform device that requires minimal
220  *      resource and memory management. Canned release function freeing
221  *      memory allocated for the device allows drivers using such devices
222  *      to be unloaded iwithout waiting for the last reference to the device
223  *      to be dropped.
224  */
225 struct platform_device *platform_device_register_simple(char *name, unsigned int id,
226                                                         struct resource *res, unsigned int num)
227 {
228         struct platform_object *pobj;
229         int retval;
230
231         pobj = kzalloc(sizeof(*pobj) + sizeof(struct resource) * num, GFP_KERNEL);
232         if (!pobj) {
233                 retval = -ENOMEM;
234                 goto error;
235         }
236
237         pobj->pdev.name = name;
238         pobj->pdev.id = id;
239         pobj->pdev.dev.release = platform_device_release_simple;
240
241         if (num) {
242                 memcpy(pobj->resources, res, sizeof(struct resource) * num);
243                 pobj->pdev.resource = pobj->resources;
244                 pobj->pdev.num_resources = num;
245         }
246
247         retval = platform_device_register(&pobj->pdev);
248         if (retval)
249                 goto error;
250
251         return &pobj->pdev;
252
253 error:
254         kfree(pobj);
255         return ERR_PTR(retval);
256 }
257
258
259 /**
260  *      platform_match - bind platform device to platform driver.
261  *      @dev:   device.
262  *      @drv:   driver.
263  *
264  *      Platform device IDs are assumed to be encoded like this:
265  *      "<name><instance>", where <name> is a short description of the
266  *      type of device, like "pci" or "floppy", and <instance> is the
267  *      enumerated instance of the device, like '0' or '42'.
268  *      Driver IDs are simply "<name>".
269  *      So, extract the <name> from the platform_device structure,
270  *      and compare it against the name of the driver. Return whether
271  *      they match or not.
272  */
273
274 static int platform_match(struct device * dev, struct device_driver * drv)
275 {
276         struct platform_device *pdev = container_of(dev, struct platform_device, dev);
277
278         return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
279 }
280
281 static int platform_suspend(struct device * dev, pm_message_t state)
282 {
283         int ret = 0;
284
285         if (dev->driver && dev->driver->suspend)
286                 ret = dev->driver->suspend(dev, state);
287
288         return ret;
289 }
290
291 static int platform_resume(struct device * dev)
292 {
293         int ret = 0;
294
295         if (dev->driver && dev->driver->resume)
296                 ret = dev->driver->resume(dev);
297
298         return ret;
299 }
300
301 struct bus_type platform_bus_type = {
302         .name           = "platform",
303         .match          = platform_match,
304         .suspend        = platform_suspend,
305         .resume         = platform_resume,
306 };
307
308 int __init platform_bus_init(void)
309 {
310         device_register(&platform_bus);
311         return bus_register(&platform_bus_type);
312 }
313
314 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
315 u64 dma_get_required_mask(struct device *dev)
316 {
317         u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
318         u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
319         u64 mask;
320
321         if (!high_totalram) {
322                 /* convert to mask just covering totalram */
323                 low_totalram = (1 << (fls(low_totalram) - 1));
324                 low_totalram += low_totalram - 1;
325                 mask = low_totalram;
326         } else {
327                 high_totalram = (1 << (fls(high_totalram) - 1));
328                 high_totalram += high_totalram - 1;
329                 mask = (((u64)high_totalram) << 32) + 0xffffffff;
330         }
331         return mask & *dev->dma_mask;
332 }
333 EXPORT_SYMBOL_GPL(dma_get_required_mask);
334 #endif
335
336 EXPORT_SYMBOL_GPL(platform_bus);
337 EXPORT_SYMBOL_GPL(platform_bus_type);
338 EXPORT_SYMBOL_GPL(platform_add_devices);
339 EXPORT_SYMBOL_GPL(platform_device_register);
340 EXPORT_SYMBOL_GPL(platform_device_register_simple);
341 EXPORT_SYMBOL_GPL(platform_device_unregister);
342 EXPORT_SYMBOL_GPL(platform_get_irq);
343 EXPORT_SYMBOL_GPL(platform_get_resource);
344 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
345 EXPORT_SYMBOL_GPL(platform_get_resource_byname);