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