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