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