Commit | Line | Data |
---|---|---|
989d42e8 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * platform.c - platform 'pseudo' bus for legacy devices | |
4 | * | |
5 | * Copyright (c) 2002-3 Patrick Mochel | |
6 | * Copyright (c) 2002-3 Open Source Development Labs | |
7 | * | |
1da177e4 LT |
8 | * Please see Documentation/driver-model/platform.txt for more |
9 | * information. | |
10 | */ | |
11 | ||
daa41226 | 12 | #include <linux/string.h> |
d052d1be | 13 | #include <linux/platform_device.h> |
05212157 | 14 | #include <linux/of_device.h> |
9ec36caf | 15 | #include <linux/of_irq.h> |
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/init.h> | |
18 | #include <linux/dma-mapping.h> | |
57c8a661 | 19 | #include <linux/memblock.h> |
1da177e4 | 20 | #include <linux/err.h> |
4e57b681 | 21 | #include <linux/slab.h> |
9d730229 | 22 | #include <linux/pm_runtime.h> |
f48c767c | 23 | #include <linux/pm_domain.h> |
689ae231 | 24 | #include <linux/idr.h> |
91e56878 | 25 | #include <linux/acpi.h> |
86be408b | 26 | #include <linux/clk/clk-conf.h> |
3d713e0e | 27 | #include <linux/limits.h> |
00bbc1d8 | 28 | #include <linux/property.h> |
967d3010 | 29 | #include <linux/kmemleak.h> |
1da177e4 | 30 | |
a1bdc7aa | 31 | #include "base.h" |
bed2b42d | 32 | #include "power/power.h" |
a1bdc7aa | 33 | |
689ae231 JD |
34 | /* For automatically allocated device IDs */ |
35 | static DEFINE_IDA(platform_devid_ida); | |
36 | ||
1da177e4 | 37 | struct device platform_bus = { |
1e0b2cf9 | 38 | .init_name = "platform", |
1da177e4 | 39 | }; |
a96b2042 | 40 | EXPORT_SYMBOL_GPL(platform_bus); |
1da177e4 | 41 | |
a77ce816 KG |
42 | /** |
43 | * arch_setup_pdev_archdata - Allow manipulation of archdata before its used | |
7de636fa | 44 | * @pdev: platform device |
a77ce816 KG |
45 | * |
46 | * This is called before platform_device_add() such that any pdev_archdata may | |
47 | * be setup before the platform_notifier is called. So if a user needs to | |
48 | * manipulate any relevant information in the pdev_archdata they can do: | |
49 | * | |
b1d6d822 | 50 | * platform_device_alloc() |
0258e182 FP |
51 | * ... manipulate ... |
52 | * platform_device_add() | |
a77ce816 KG |
53 | * |
54 | * And if they don't care they can just call platform_device_register() and | |
55 | * everything will just work out. | |
56 | */ | |
57 | void __weak arch_setup_pdev_archdata(struct platform_device *pdev) | |
58 | { | |
59 | } | |
60 | ||
1da177e4 | 61 | /** |
4a3ad20c GKH |
62 | * platform_get_resource - get a resource for a device |
63 | * @dev: platform device | |
64 | * @type: resource type | |
65 | * @num: resource index | |
1da177e4 | 66 | */ |
4a3ad20c GKH |
67 | struct resource *platform_get_resource(struct platform_device *dev, |
68 | unsigned int type, unsigned int num) | |
1da177e4 LT |
69 | { |
70 | int i; | |
71 | ||
72 | for (i = 0; i < dev->num_resources; i++) { | |
73 | struct resource *r = &dev->resource[i]; | |
74 | ||
c9f66169 MD |
75 | if (type == resource_type(r) && num-- == 0) |
76 | return r; | |
1da177e4 LT |
77 | } |
78 | return NULL; | |
79 | } | |
a96b2042 | 80 | EXPORT_SYMBOL_GPL(platform_get_resource); |
1da177e4 LT |
81 | |
82 | /** | |
4a3ad20c GKH |
83 | * platform_get_irq - get an IRQ for a device |
84 | * @dev: platform device | |
85 | * @num: IRQ number index | |
1da177e4 LT |
86 | */ |
87 | int platform_get_irq(struct platform_device *dev, unsigned int num) | |
88 | { | |
5cf8f7db AL |
89 | #ifdef CONFIG_SPARC |
90 | /* sparc does not have irqs represented as IORESOURCE_IRQ resources */ | |
91 | if (!dev || num >= dev->archdata.num_irqs) | |
92 | return -ENXIO; | |
93 | return dev->archdata.irqs[num]; | |
94 | #else | |
9ec36caf | 95 | struct resource *r; |
aff008ad GR |
96 | if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) { |
97 | int ret; | |
98 | ||
99 | ret = of_irq_get(dev->dev.of_node, num); | |
e330b9a6 | 100 | if (ret > 0 || ret == -EPROBE_DEFER) |
aff008ad GR |
101 | return ret; |
102 | } | |
9ec36caf RH |
103 | |
104 | r = platform_get_resource(dev, IORESOURCE_IRQ, num); | |
d44fa3d4 AVF |
105 | if (has_acpi_companion(&dev->dev)) { |
106 | if (r && r->flags & IORESOURCE_DISABLED) { | |
107 | int ret; | |
108 | ||
109 | ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r); | |
110 | if (ret) | |
111 | return ret; | |
112 | } | |
113 | } | |
114 | ||
7085a740 LW |
115 | /* |
116 | * The resources may pass trigger flags to the irqs that need | |
117 | * to be set up. It so happens that the trigger flags for | |
118 | * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER* | |
119 | * settings. | |
120 | */ | |
60ca5e0d GR |
121 | if (r && r->flags & IORESOURCE_BITS) { |
122 | struct irq_data *irqd; | |
123 | ||
124 | irqd = irq_get_irq_data(r->start); | |
125 | if (!irqd) | |
126 | return -ENXIO; | |
127 | irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); | |
128 | } | |
1da177e4 | 129 | |
daaef255 EG |
130 | if (r) |
131 | return r->start; | |
132 | ||
133 | /* | |
134 | * For the index 0 interrupt, allow falling back to GpioInt | |
135 | * resources. While a device could have both Interrupt and GpioInt | |
136 | * resources, making this fallback ambiguous, in many common cases | |
137 | * the device will only expose one IRQ, and this fallback | |
138 | * allows a common code path across either kind of resource. | |
139 | */ | |
140 | if (num == 0 && has_acpi_companion(&dev->dev)) | |
141 | return acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num); | |
142 | ||
143 | return -ENXIO; | |
5cf8f7db | 144 | #endif |
1da177e4 | 145 | } |
a96b2042 | 146 | EXPORT_SYMBOL_GPL(platform_get_irq); |
1da177e4 | 147 | |
4b83555d SB |
148 | /** |
149 | * platform_irq_count - Count the number of IRQs a platform device uses | |
150 | * @dev: platform device | |
151 | * | |
152 | * Return: Number of IRQs a platform device uses or EPROBE_DEFER | |
153 | */ | |
154 | int platform_irq_count(struct platform_device *dev) | |
155 | { | |
156 | int ret, nr = 0; | |
157 | ||
158 | while ((ret = platform_get_irq(dev, nr)) >= 0) | |
159 | nr++; | |
160 | ||
161 | if (ret == -EPROBE_DEFER) | |
162 | return ret; | |
163 | ||
164 | return nr; | |
165 | } | |
166 | EXPORT_SYMBOL_GPL(platform_irq_count); | |
167 | ||
1da177e4 | 168 | /** |
4a3ad20c GKH |
169 | * platform_get_resource_byname - get a resource for a device by name |
170 | * @dev: platform device | |
171 | * @type: resource type | |
172 | * @name: resource name | |
1da177e4 | 173 | */ |
4a3ad20c | 174 | struct resource *platform_get_resource_byname(struct platform_device *dev, |
c0afe7ba LW |
175 | unsigned int type, |
176 | const char *name) | |
1da177e4 LT |
177 | { |
178 | int i; | |
179 | ||
180 | for (i = 0; i < dev->num_resources; i++) { | |
181 | struct resource *r = &dev->resource[i]; | |
182 | ||
1b8cb929 PU |
183 | if (unlikely(!r->name)) |
184 | continue; | |
185 | ||
c9f66169 MD |
186 | if (type == resource_type(r) && !strcmp(r->name, name)) |
187 | return r; | |
1da177e4 LT |
188 | } |
189 | return NULL; | |
190 | } | |
a96b2042 | 191 | EXPORT_SYMBOL_GPL(platform_get_resource_byname); |
1da177e4 LT |
192 | |
193 | /** | |
d6ff8551 | 194 | * platform_get_irq_byname - get an IRQ for a device by name |
4a3ad20c GKH |
195 | * @dev: platform device |
196 | * @name: IRQ name | |
1da177e4 | 197 | */ |
c0afe7ba | 198 | int platform_get_irq_byname(struct platform_device *dev, const char *name) |
1da177e4 | 199 | { |
ad69674e GS |
200 | struct resource *r; |
201 | ||
aff008ad GR |
202 | if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) { |
203 | int ret; | |
204 | ||
205 | ret = of_irq_get_byname(dev->dev.of_node, name); | |
e330b9a6 | 206 | if (ret > 0 || ret == -EPROBE_DEFER) |
aff008ad GR |
207 | return ret; |
208 | } | |
1da177e4 | 209 | |
ad69674e | 210 | r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name); |
305b3228 | 211 | return r ? r->start : -ENXIO; |
1da177e4 | 212 | } |
a96b2042 | 213 | EXPORT_SYMBOL_GPL(platform_get_irq_byname); |
1da177e4 LT |
214 | |
215 | /** | |
4a3ad20c GKH |
216 | * platform_add_devices - add a numbers of platform devices |
217 | * @devs: array of platform devices to add | |
218 | * @num: number of platform devices in array | |
1da177e4 LT |
219 | */ |
220 | int platform_add_devices(struct platform_device **devs, int num) | |
221 | { | |
222 | int i, ret = 0; | |
223 | ||
224 | for (i = 0; i < num; i++) { | |
225 | ret = platform_device_register(devs[i]); | |
226 | if (ret) { | |
227 | while (--i >= 0) | |
228 | platform_device_unregister(devs[i]); | |
229 | break; | |
230 | } | |
231 | } | |
232 | ||
233 | return ret; | |
234 | } | |
a96b2042 | 235 | EXPORT_SYMBOL_GPL(platform_add_devices); |
1da177e4 | 236 | |
37c12e74 RK |
237 | struct platform_object { |
238 | struct platform_device pdev; | |
1cec24c5 | 239 | char name[]; |
37c12e74 RK |
240 | }; |
241 | ||
1da177e4 | 242 | /** |
3c31f07a | 243 | * platform_device_put - destroy a platform device |
4a3ad20c | 244 | * @pdev: platform device to free |
37c12e74 | 245 | * |
4a3ad20c GKH |
246 | * Free all memory associated with a platform device. This function must |
247 | * _only_ be externally called in error cases. All other usage is a bug. | |
37c12e74 RK |
248 | */ |
249 | void platform_device_put(struct platform_device *pdev) | |
250 | { | |
99fef587 | 251 | if (!IS_ERR_OR_NULL(pdev)) |
37c12e74 RK |
252 | put_device(&pdev->dev); |
253 | } | |
254 | EXPORT_SYMBOL_GPL(platform_device_put); | |
255 | ||
256 | static void platform_device_release(struct device *dev) | |
257 | { | |
4a3ad20c GKH |
258 | struct platform_object *pa = container_of(dev, struct platform_object, |
259 | pdev.dev); | |
37c12e74 | 260 | |
7096d042 | 261 | of_device_node_put(&pa->pdev.dev); |
37c12e74 | 262 | kfree(pa->pdev.dev.platform_data); |
e710d7d5 | 263 | kfree(pa->pdev.mfd_cell); |
37c12e74 | 264 | kfree(pa->pdev.resource); |
3d713e0e | 265 | kfree(pa->pdev.driver_override); |
37c12e74 RK |
266 | kfree(pa); |
267 | } | |
268 | ||
269 | /** | |
3c31f07a | 270 | * platform_device_alloc - create a platform device |
4a3ad20c GKH |
271 | * @name: base name of the device we're adding |
272 | * @id: instance id | |
37c12e74 | 273 | * |
4a3ad20c GKH |
274 | * Create a platform device object which can have other objects attached |
275 | * to it, and which will have attached objects freed when it is released. | |
37c12e74 | 276 | */ |
1359555e | 277 | struct platform_device *platform_device_alloc(const char *name, int id) |
37c12e74 RK |
278 | { |
279 | struct platform_object *pa; | |
280 | ||
1cec24c5 | 281 | pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL); |
37c12e74 RK |
282 | if (pa) { |
283 | strcpy(pa->name, name); | |
284 | pa->pdev.name = pa->name; | |
285 | pa->pdev.id = id; | |
286 | device_initialize(&pa->pdev.dev); | |
287 | pa->pdev.dev.release = platform_device_release; | |
a77ce816 | 288 | arch_setup_pdev_archdata(&pa->pdev); |
37c12e74 RK |
289 | } |
290 | ||
93ce3061 | 291 | return pa ? &pa->pdev : NULL; |
37c12e74 RK |
292 | } |
293 | EXPORT_SYMBOL_GPL(platform_device_alloc); | |
294 | ||
295 | /** | |
3c31f07a | 296 | * platform_device_add_resources - add resources to a platform device |
4a3ad20c GKH |
297 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
298 | * @res: set of resources that needs to be allocated for the device | |
299 | * @num: number of resources | |
37c12e74 | 300 | * |
4a3ad20c GKH |
301 | * Add a copy of the resources to the platform device. The memory |
302 | * associated with the resources will be freed when the platform device is | |
303 | * released. | |
37c12e74 | 304 | */ |
4a3ad20c | 305 | int platform_device_add_resources(struct platform_device *pdev, |
0b7f1a7e | 306 | const struct resource *res, unsigned int num) |
37c12e74 | 307 | { |
cea89623 | 308 | struct resource *r = NULL; |
37c12e74 | 309 | |
cea89623 UKK |
310 | if (res) { |
311 | r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL); | |
312 | if (!r) | |
313 | return -ENOMEM; | |
37c12e74 | 314 | } |
cea89623 | 315 | |
4a03d6f7 | 316 | kfree(pdev->resource); |
cea89623 UKK |
317 | pdev->resource = r; |
318 | pdev->num_resources = num; | |
319 | return 0; | |
37c12e74 RK |
320 | } |
321 | EXPORT_SYMBOL_GPL(platform_device_add_resources); | |
322 | ||
323 | /** | |
3c31f07a | 324 | * platform_device_add_data - add platform-specific data to a platform device |
4a3ad20c GKH |
325 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
326 | * @data: platform specific data for this platform device | |
327 | * @size: size of platform specific data | |
37c12e74 | 328 | * |
4a3ad20c GKH |
329 | * Add a copy of platform specific data to the platform device's |
330 | * platform_data pointer. The memory associated with the platform data | |
331 | * will be freed when the platform device is released. | |
37c12e74 | 332 | */ |
4a3ad20c GKH |
333 | int platform_device_add_data(struct platform_device *pdev, const void *data, |
334 | size_t size) | |
37c12e74 | 335 | { |
27a33f9e | 336 | void *d = NULL; |
5cfc64ce | 337 | |
27a33f9e UKK |
338 | if (data) { |
339 | d = kmemdup(data, size, GFP_KERNEL); | |
340 | if (!d) | |
341 | return -ENOMEM; | |
37c12e74 | 342 | } |
27a33f9e | 343 | |
251e031d | 344 | kfree(pdev->dev.platform_data); |
27a33f9e UKK |
345 | pdev->dev.platform_data = d; |
346 | return 0; | |
37c12e74 RK |
347 | } |
348 | EXPORT_SYMBOL_GPL(platform_device_add_data); | |
349 | ||
00bbc1d8 MW |
350 | /** |
351 | * platform_device_add_properties - add built-in properties to a platform device | |
352 | * @pdev: platform device to add properties to | |
f4d05266 | 353 | * @properties: null terminated array of properties to add |
00bbc1d8 | 354 | * |
f4d05266 HK |
355 | * The function will take deep copy of @properties and attach the copy to the |
356 | * platform device. The memory associated with properties will be freed when the | |
357 | * platform device is released. | |
00bbc1d8 MW |
358 | */ |
359 | int platform_device_add_properties(struct platform_device *pdev, | |
277036f0 | 360 | const struct property_entry *properties) |
00bbc1d8 | 361 | { |
f4d05266 | 362 | return device_add_properties(&pdev->dev, properties); |
00bbc1d8 MW |
363 | } |
364 | EXPORT_SYMBOL_GPL(platform_device_add_properties); | |
365 | ||
37c12e74 | 366 | /** |
4a3ad20c GKH |
367 | * platform_device_add - add a platform device to device hierarchy |
368 | * @pdev: platform device we're adding | |
1da177e4 | 369 | * |
4a3ad20c GKH |
370 | * This is part 2 of platform_device_register(), though may be called |
371 | * separately _iff_ pdev was allocated by platform_device_alloc(). | |
1da177e4 | 372 | */ |
37c12e74 | 373 | int platform_device_add(struct platform_device *pdev) |
1da177e4 | 374 | { |
689ae231 | 375 | int i, ret; |
1da177e4 LT |
376 | |
377 | if (!pdev) | |
378 | return -EINVAL; | |
379 | ||
380 | if (!pdev->dev.parent) | |
381 | pdev->dev.parent = &platform_bus; | |
382 | ||
383 | pdev->dev.bus = &platform_bus_type; | |
384 | ||
689ae231 JD |
385 | switch (pdev->id) { |
386 | default: | |
1e0b2cf9 | 387 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); |
689ae231 JD |
388 | break; |
389 | case PLATFORM_DEVID_NONE: | |
acc0e90f | 390 | dev_set_name(&pdev->dev, "%s", pdev->name); |
689ae231 JD |
391 | break; |
392 | case PLATFORM_DEVID_AUTO: | |
393 | /* | |
394 | * Automatically allocated device ID. We mark it as such so | |
395 | * that we remember it must be freed, and we append a suffix | |
396 | * to avoid namespace collision with explicit IDs. | |
397 | */ | |
398 | ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL); | |
399 | if (ret < 0) | |
5da7f709 | 400 | goto err_out; |
689ae231 JD |
401 | pdev->id = ret; |
402 | pdev->id_auto = true; | |
403 | dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id); | |
404 | break; | |
405 | } | |
1da177e4 LT |
406 | |
407 | for (i = 0; i < pdev->num_resources; i++) { | |
5da7f709 | 408 | struct resource *p, *r = &pdev->resource[i]; |
1da177e4 LT |
409 | |
410 | if (r->name == NULL) | |
1e0b2cf9 | 411 | r->name = dev_name(&pdev->dev); |
1da177e4 LT |
412 | |
413 | p = r->parent; | |
414 | if (!p) { | |
0e6c861f | 415 | if (resource_type(r) == IORESOURCE_MEM) |
1da177e4 | 416 | p = &iomem_resource; |
0e6c861f | 417 | else if (resource_type(r) == IORESOURCE_IO) |
1da177e4 LT |
418 | p = &ioport_resource; |
419 | } | |
420 | ||
0e6c861f | 421 | if (p && insert_resource(p, r)) { |
8a18f428 | 422 | dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r); |
5da7f709 GKH |
423 | ret = -EBUSY; |
424 | goto failed; | |
425 | } | |
1da177e4 LT |
426 | } |
427 | ||
428 | pr_debug("Registering platform device '%s'. Parent at %s\n", | |
1e0b2cf9 | 429 | dev_name(&pdev->dev), dev_name(pdev->dev.parent)); |
1da177e4 | 430 | |
e3915532 | 431 | ret = device_add(&pdev->dev); |
8b2dceba GKH |
432 | if (ret == 0) |
433 | return ret; | |
434 | ||
5da7f709 | 435 | failed: |
8b2dceba GKH |
436 | if (pdev->id_auto) { |
437 | ida_simple_remove(&platform_devid_ida, pdev->id); | |
438 | pdev->id = PLATFORM_DEVID_AUTO; | |
439 | } | |
440 | ||
441 | while (--i >= 0) { | |
442 | struct resource *r = &pdev->resource[i]; | |
7f5dcaf1 | 443 | if (r->parent) |
8b2dceba GKH |
444 | release_resource(r); |
445 | } | |
c9f66169 | 446 | |
5da7f709 | 447 | err_out: |
1da177e4 LT |
448 | return ret; |
449 | } | |
37c12e74 RK |
450 | EXPORT_SYMBOL_GPL(platform_device_add); |
451 | ||
452 | /** | |
4a3ad20c GKH |
453 | * platform_device_del - remove a platform-level device |
454 | * @pdev: platform device we're removing | |
1da177e4 | 455 | * |
4a3ad20c GKH |
456 | * Note that this function will also release all memory- and port-based |
457 | * resources owned by the device (@dev->resource). This function must | |
458 | * _only_ be externally called in error cases. All other usage is a bug. | |
1da177e4 | 459 | */ |
93ce3061 | 460 | void platform_device_del(struct platform_device *pdev) |
1da177e4 | 461 | { |
8b2dceba | 462 | int i; |
c9f66169 | 463 | |
99fef587 | 464 | if (!IS_ERR_OR_NULL(pdev)) { |
8b2dceba GKH |
465 | device_del(&pdev->dev); |
466 | ||
467 | if (pdev->id_auto) { | |
468 | ida_simple_remove(&platform_devid_ida, pdev->id); | |
469 | pdev->id = PLATFORM_DEVID_AUTO; | |
470 | } | |
471 | ||
472 | for (i = 0; i < pdev->num_resources; i++) { | |
473 | struct resource *r = &pdev->resource[i]; | |
7f5dcaf1 | 474 | if (r->parent) |
8b2dceba GKH |
475 | release_resource(r); |
476 | } | |
477 | } | |
1da177e4 | 478 | } |
93ce3061 DT |
479 | EXPORT_SYMBOL_GPL(platform_device_del); |
480 | ||
481 | /** | |
4a3ad20c GKH |
482 | * platform_device_register - add a platform-level device |
483 | * @pdev: platform device we're adding | |
93ce3061 | 484 | */ |
4a3ad20c | 485 | int platform_device_register(struct platform_device *pdev) |
93ce3061 DT |
486 | { |
487 | device_initialize(&pdev->dev); | |
a77ce816 | 488 | arch_setup_pdev_archdata(pdev); |
93ce3061 DT |
489 | return platform_device_add(pdev); |
490 | } | |
a96b2042 | 491 | EXPORT_SYMBOL_GPL(platform_device_register); |
93ce3061 DT |
492 | |
493 | /** | |
4a3ad20c GKH |
494 | * platform_device_unregister - unregister a platform-level device |
495 | * @pdev: platform device we're unregistering | |
93ce3061 | 496 | * |
4a3ad20c GKH |
497 | * Unregistration is done in 2 steps. First we release all resources |
498 | * and remove it from the subsystem, then we drop reference count by | |
499 | * calling platform_device_put(). | |
93ce3061 | 500 | */ |
4a3ad20c | 501 | void platform_device_unregister(struct platform_device *pdev) |
93ce3061 DT |
502 | { |
503 | platform_device_del(pdev); | |
504 | platform_device_put(pdev); | |
505 | } | |
a96b2042 | 506 | EXPORT_SYMBOL_GPL(platform_device_unregister); |
1da177e4 | 507 | |
1da177e4 | 508 | /** |
01dcc60a | 509 | * platform_device_register_full - add a platform-level device with |
44f28bde | 510 | * resources and platform-specific data |
49a4ec18 | 511 | * |
01dcc60a | 512 | * @pdevinfo: data used to create device |
d8bf2540 | 513 | * |
f0eae0ed | 514 | * Returns &struct platform_device pointer on success, or ERR_PTR() on error. |
d8bf2540 | 515 | */ |
01dcc60a | 516 | struct platform_device *platform_device_register_full( |
5a3072be | 517 | const struct platform_device_info *pdevinfo) |
d8bf2540 | 518 | { |
44f28bde | 519 | int ret = -ENOMEM; |
d8bf2540 | 520 | struct platform_device *pdev; |
d8bf2540 | 521 | |
01dcc60a | 522 | pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id); |
44f28bde | 523 | if (!pdev) |
01dcc60a UKK |
524 | goto err_alloc; |
525 | ||
526 | pdev->dev.parent = pdevinfo->parent; | |
ce793486 | 527 | pdev->dev.fwnode = pdevinfo->fwnode; |
2c1ea6ab MR |
528 | pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode)); |
529 | pdev->dev.of_node_reused = pdevinfo->of_node_reused; | |
01dcc60a UKK |
530 | |
531 | if (pdevinfo->dma_mask) { | |
532 | /* | |
533 | * This memory isn't freed when the device is put, | |
534 | * I don't have a nice idea for that though. Conceptually | |
535 | * dma_mask in struct device should not be a pointer. | |
536 | * See http://thread.gmane.org/gmane.linux.kernel.pci/9081 | |
537 | */ | |
538 | pdev->dev.dma_mask = | |
539 | kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL); | |
540 | if (!pdev->dev.dma_mask) | |
541 | goto err; | |
542 | ||
967d3010 QC |
543 | kmemleak_ignore(pdev->dev.dma_mask); |
544 | ||
01dcc60a UKK |
545 | *pdev->dev.dma_mask = pdevinfo->dma_mask; |
546 | pdev->dev.coherent_dma_mask = pdevinfo->dma_mask; | |
547 | } | |
d8bf2540 | 548 | |
01dcc60a UKK |
549 | ret = platform_device_add_resources(pdev, |
550 | pdevinfo->res, pdevinfo->num_res); | |
807508c8 AV |
551 | if (ret) |
552 | goto err; | |
44f28bde | 553 | |
01dcc60a UKK |
554 | ret = platform_device_add_data(pdev, |
555 | pdevinfo->data, pdevinfo->size_data); | |
807508c8 AV |
556 | if (ret) |
557 | goto err; | |
d8bf2540 | 558 | |
f4d05266 HK |
559 | if (pdevinfo->properties) { |
560 | ret = platform_device_add_properties(pdev, | |
561 | pdevinfo->properties); | |
00bbc1d8 MW |
562 | if (ret) |
563 | goto err; | |
564 | } | |
565 | ||
44f28bde UKK |
566 | ret = platform_device_add(pdev); |
567 | if (ret) { | |
568 | err: | |
7b199811 | 569 | ACPI_COMPANION_SET(&pdev->dev, NULL); |
01dcc60a UKK |
570 | kfree(pdev->dev.dma_mask); |
571 | ||
572 | err_alloc: | |
44f28bde UKK |
573 | platform_device_put(pdev); |
574 | return ERR_PTR(ret); | |
575 | } | |
d8bf2540 DB |
576 | |
577 | return pdev; | |
d8bf2540 | 578 | } |
01dcc60a | 579 | EXPORT_SYMBOL_GPL(platform_device_register_full); |
d8bf2540 | 580 | |
00d3dcdd RK |
581 | static int platform_drv_probe(struct device *_dev) |
582 | { | |
583 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
584 | struct platform_device *dev = to_platform_device(_dev); | |
94d76d5d | 585 | int ret; |
00d3dcdd | 586 | |
86be408b SN |
587 | ret = of_clk_set_defaults(_dev->of_node, false); |
588 | if (ret < 0) | |
589 | return ret; | |
590 | ||
cb518413 | 591 | ret = dev_pm_domain_attach(_dev, true); |
88a9769e UH |
592 | if (ret) |
593 | goto out; | |
594 | ||
595 | if (drv->probe) { | |
596 | ret = drv->probe(dev); | |
597 | if (ret) | |
598 | dev_pm_domain_detach(_dev, true); | |
cb518413 | 599 | } |
94d76d5d | 600 | |
88a9769e | 601 | out: |
3f9120b0 JH |
602 | if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) { |
603 | dev_warn(_dev, "probe deferral not supported\n"); | |
604 | ret = -ENXIO; | |
605 | } | |
606 | ||
94d76d5d | 607 | return ret; |
00d3dcdd RK |
608 | } |
609 | ||
c67334fb DB |
610 | static int platform_drv_probe_fail(struct device *_dev) |
611 | { | |
612 | return -ENXIO; | |
613 | } | |
614 | ||
00d3dcdd RK |
615 | static int platform_drv_remove(struct device *_dev) |
616 | { | |
617 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
618 | struct platform_device *dev = to_platform_device(_dev); | |
b8b2c7d8 | 619 | int ret = 0; |
00d3dcdd | 620 | |
b8b2c7d8 UKK |
621 | if (drv->remove) |
622 | ret = drv->remove(dev); | |
cb518413 | 623 | dev_pm_domain_detach(_dev, true); |
94d76d5d RW |
624 | |
625 | return ret; | |
00d3dcdd RK |
626 | } |
627 | ||
628 | static void platform_drv_shutdown(struct device *_dev) | |
629 | { | |
630 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
631 | struct platform_device *dev = to_platform_device(_dev); | |
632 | ||
b8b2c7d8 UKK |
633 | if (drv->shutdown) |
634 | drv->shutdown(dev); | |
00d3dcdd RK |
635 | } |
636 | ||
00d3dcdd | 637 | /** |
9447057e | 638 | * __platform_driver_register - register a driver for platform-level devices |
4a3ad20c | 639 | * @drv: platform driver structure |
08801f96 | 640 | * @owner: owning module/driver |
00d3dcdd | 641 | */ |
9447057e LC |
642 | int __platform_driver_register(struct platform_driver *drv, |
643 | struct module *owner) | |
00d3dcdd | 644 | { |
9447057e | 645 | drv->driver.owner = owner; |
00d3dcdd | 646 | drv->driver.bus = &platform_bus_type; |
b8b2c7d8 UKK |
647 | drv->driver.probe = platform_drv_probe; |
648 | drv->driver.remove = platform_drv_remove; | |
649 | drv->driver.shutdown = platform_drv_shutdown; | |
783ea7d4 | 650 | |
00d3dcdd RK |
651 | return driver_register(&drv->driver); |
652 | } | |
9447057e | 653 | EXPORT_SYMBOL_GPL(__platform_driver_register); |
00d3dcdd RK |
654 | |
655 | /** | |
3c31f07a | 656 | * platform_driver_unregister - unregister a driver for platform-level devices |
4a3ad20c | 657 | * @drv: platform driver structure |
00d3dcdd RK |
658 | */ |
659 | void platform_driver_unregister(struct platform_driver *drv) | |
660 | { | |
661 | driver_unregister(&drv->driver); | |
662 | } | |
663 | EXPORT_SYMBOL_GPL(platform_driver_unregister); | |
664 | ||
c67334fb | 665 | /** |
c3b50dc2 | 666 | * __platform_driver_probe - register driver for non-hotpluggable device |
c67334fb | 667 | * @drv: platform driver structure |
3f9120b0 | 668 | * @probe: the driver probe routine, probably from an __init section |
c3b50dc2 | 669 | * @module: module which will be the owner of the driver |
c67334fb DB |
670 | * |
671 | * Use this instead of platform_driver_register() when you know the device | |
672 | * is not hotpluggable and has already been registered, and you want to | |
673 | * remove its run-once probe() infrastructure from memory after the driver | |
674 | * has bound to the device. | |
675 | * | |
676 | * One typical use for this would be with drivers for controllers integrated | |
677 | * into system-on-chip processors, where the controller devices have been | |
678 | * configured as part of board setup. | |
679 | * | |
3f9120b0 | 680 | * Note that this is incompatible with deferred probing. |
647c86d0 | 681 | * |
c67334fb DB |
682 | * Returns zero if the driver registered and bound to a device, else returns |
683 | * a negative error code and with the driver not registered. | |
684 | */ | |
c3b50dc2 WS |
685 | int __init_or_module __platform_driver_probe(struct platform_driver *drv, |
686 | int (*probe)(struct platform_device *), struct module *module) | |
c67334fb DB |
687 | { |
688 | int retval, code; | |
689 | ||
5c36eb2a DT |
690 | if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) { |
691 | pr_err("%s: drivers registered with %s can not be probed asynchronously\n", | |
692 | drv->driver.name, __func__); | |
693 | return -EINVAL; | |
694 | } | |
695 | ||
696 | /* | |
697 | * We have to run our probes synchronously because we check if | |
698 | * we find any devices to bind to and exit with error if there | |
699 | * are any. | |
700 | */ | |
701 | drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS; | |
702 | ||
3f9120b0 JH |
703 | /* |
704 | * Prevent driver from requesting probe deferral to avoid further | |
705 | * futile probe attempts. | |
706 | */ | |
707 | drv->prevent_deferred_probe = true; | |
708 | ||
1a6f2a75 DT |
709 | /* make sure driver won't have bind/unbind attributes */ |
710 | drv->driver.suppress_bind_attrs = true; | |
711 | ||
c67334fb DB |
712 | /* temporary section violation during probe() */ |
713 | drv->probe = probe; | |
c3b50dc2 | 714 | retval = code = __platform_driver_register(drv, module); |
c67334fb | 715 | |
1a6f2a75 DT |
716 | /* |
717 | * Fixup that section violation, being paranoid about code scanning | |
c67334fb DB |
718 | * the list of drivers in order to probe new devices. Check to see |
719 | * if the probe was successful, and make sure any forced probes of | |
720 | * new devices fail. | |
721 | */ | |
d79d3244 | 722 | spin_lock(&drv->driver.bus->p->klist_drivers.k_lock); |
c67334fb | 723 | drv->probe = NULL; |
e5dd1278 | 724 | if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) |
c67334fb DB |
725 | retval = -ENODEV; |
726 | drv->driver.probe = platform_drv_probe_fail; | |
d79d3244 | 727 | spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock); |
c67334fb DB |
728 | |
729 | if (code != retval) | |
730 | platform_driver_unregister(drv); | |
731 | return retval; | |
732 | } | |
c3b50dc2 | 733 | EXPORT_SYMBOL_GPL(__platform_driver_probe); |
1da177e4 | 734 | |
ecdf6ceb | 735 | /** |
291f653a | 736 | * __platform_create_bundle - register driver and create corresponding device |
ecdf6ceb DT |
737 | * @driver: platform driver structure |
738 | * @probe: the driver probe routine, probably from an __init section | |
739 | * @res: set of resources that needs to be allocated for the device | |
740 | * @n_res: number of resources | |
741 | * @data: platform specific data for this platform device | |
742 | * @size: size of platform specific data | |
291f653a | 743 | * @module: module which will be the owner of the driver |
ecdf6ceb DT |
744 | * |
745 | * Use this in legacy-style modules that probe hardware directly and | |
746 | * register a single platform device and corresponding platform driver. | |
f0eae0ed JN |
747 | * |
748 | * Returns &struct platform_device pointer on success, or ERR_PTR() on error. | |
ecdf6ceb | 749 | */ |
291f653a | 750 | struct platform_device * __init_or_module __platform_create_bundle( |
ecdf6ceb DT |
751 | struct platform_driver *driver, |
752 | int (*probe)(struct platform_device *), | |
753 | struct resource *res, unsigned int n_res, | |
291f653a | 754 | const void *data, size_t size, struct module *module) |
ecdf6ceb DT |
755 | { |
756 | struct platform_device *pdev; | |
757 | int error; | |
758 | ||
759 | pdev = platform_device_alloc(driver->driver.name, -1); | |
760 | if (!pdev) { | |
761 | error = -ENOMEM; | |
762 | goto err_out; | |
763 | } | |
764 | ||
807508c8 AV |
765 | error = platform_device_add_resources(pdev, res, n_res); |
766 | if (error) | |
767 | goto err_pdev_put; | |
ecdf6ceb | 768 | |
807508c8 AV |
769 | error = platform_device_add_data(pdev, data, size); |
770 | if (error) | |
771 | goto err_pdev_put; | |
ecdf6ceb DT |
772 | |
773 | error = platform_device_add(pdev); | |
774 | if (error) | |
775 | goto err_pdev_put; | |
776 | ||
291f653a | 777 | error = __platform_driver_probe(driver, probe, module); |
ecdf6ceb DT |
778 | if (error) |
779 | goto err_pdev_del; | |
780 | ||
781 | return pdev; | |
782 | ||
783 | err_pdev_del: | |
784 | platform_device_del(pdev); | |
785 | err_pdev_put: | |
786 | platform_device_put(pdev); | |
787 | err_out: | |
788 | return ERR_PTR(error); | |
789 | } | |
291f653a | 790 | EXPORT_SYMBOL_GPL(__platform_create_bundle); |
ecdf6ceb | 791 | |
dbe2256d TR |
792 | /** |
793 | * __platform_register_drivers - register an array of platform drivers | |
794 | * @drivers: an array of drivers to register | |
795 | * @count: the number of drivers to register | |
796 | * @owner: module owning the drivers | |
797 | * | |
798 | * Registers platform drivers specified by an array. On failure to register a | |
799 | * driver, all previously registered drivers will be unregistered. Callers of | |
800 | * this API should use platform_unregister_drivers() to unregister drivers in | |
801 | * the reverse order. | |
802 | * | |
803 | * Returns: 0 on success or a negative error code on failure. | |
804 | */ | |
805 | int __platform_register_drivers(struct platform_driver * const *drivers, | |
806 | unsigned int count, struct module *owner) | |
807 | { | |
808 | unsigned int i; | |
809 | int err; | |
810 | ||
811 | for (i = 0; i < count; i++) { | |
812 | pr_debug("registering platform driver %ps\n", drivers[i]); | |
813 | ||
814 | err = __platform_driver_register(drivers[i], owner); | |
815 | if (err < 0) { | |
816 | pr_err("failed to register platform driver %ps: %d\n", | |
817 | drivers[i], err); | |
818 | goto error; | |
819 | } | |
820 | } | |
821 | ||
822 | return 0; | |
823 | ||
824 | error: | |
825 | while (i--) { | |
826 | pr_debug("unregistering platform driver %ps\n", drivers[i]); | |
827 | platform_driver_unregister(drivers[i]); | |
828 | } | |
829 | ||
830 | return err; | |
831 | } | |
832 | EXPORT_SYMBOL_GPL(__platform_register_drivers); | |
833 | ||
834 | /** | |
835 | * platform_unregister_drivers - unregister an array of platform drivers | |
836 | * @drivers: an array of drivers to unregister | |
837 | * @count: the number of drivers to unregister | |
838 | * | |
839 | * Unegisters platform drivers specified by an array. This is typically used | |
840 | * to complement an earlier call to platform_register_drivers(). Drivers are | |
841 | * unregistered in the reverse order in which they were registered. | |
842 | */ | |
843 | void platform_unregister_drivers(struct platform_driver * const *drivers, | |
844 | unsigned int count) | |
845 | { | |
846 | while (count--) { | |
847 | pr_debug("unregistering platform driver %ps\n", drivers[count]); | |
848 | platform_driver_unregister(drivers[count]); | |
849 | } | |
850 | } | |
851 | EXPORT_SYMBOL_GPL(platform_unregister_drivers); | |
852 | ||
a0245f7a DB |
853 | /* modalias support enables more hands-off userspace setup: |
854 | * (a) environment variable lets new-style hotplug events work once system is | |
855 | * fully running: "modprobe $MODALIAS" | |
856 | * (b) sysfs attribute lets new-style coldplug recover from hotplug events | |
857 | * mishandled before system is fully running: "modprobe $(cat modalias)" | |
858 | */ | |
4a3ad20c GKH |
859 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
860 | char *buf) | |
a0245f7a DB |
861 | { |
862 | struct platform_device *pdev = to_platform_device(dev); | |
8c4ff6d0 ZR |
863 | int len; |
864 | ||
0634c295 | 865 | len = of_device_modalias(dev, buf, PAGE_SIZE); |
b9f73067 ZR |
866 | if (len != -ENODEV) |
867 | return len; | |
868 | ||
8c4ff6d0 ZR |
869 | len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); |
870 | if (len != -ENODEV) | |
871 | return len; | |
872 | ||
873 | len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); | |
a0245f7a DB |
874 | |
875 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; | |
876 | } | |
d06262e5 | 877 | static DEVICE_ATTR_RO(modalias); |
a0245f7a | 878 | |
3d713e0e KP |
879 | static ssize_t driver_override_store(struct device *dev, |
880 | struct device_attribute *attr, | |
881 | const char *buf, size_t count) | |
882 | { | |
883 | struct platform_device *pdev = to_platform_device(dev); | |
62655397 | 884 | char *driver_override, *old, *cp; |
3d713e0e | 885 | |
bf563b01 NS |
886 | /* We need to keep extra room for a newline */ |
887 | if (count >= (PAGE_SIZE - 1)) | |
3d713e0e KP |
888 | return -EINVAL; |
889 | ||
890 | driver_override = kstrndup(buf, count, GFP_KERNEL); | |
891 | if (!driver_override) | |
892 | return -ENOMEM; | |
893 | ||
894 | cp = strchr(driver_override, '\n'); | |
895 | if (cp) | |
896 | *cp = '\0'; | |
897 | ||
62655397 AS |
898 | device_lock(dev); |
899 | old = pdev->driver_override; | |
3d713e0e KP |
900 | if (strlen(driver_override)) { |
901 | pdev->driver_override = driver_override; | |
902 | } else { | |
903 | kfree(driver_override); | |
904 | pdev->driver_override = NULL; | |
905 | } | |
62655397 | 906 | device_unlock(dev); |
3d713e0e KP |
907 | |
908 | kfree(old); | |
909 | ||
910 | return count; | |
911 | } | |
912 | ||
913 | static ssize_t driver_override_show(struct device *dev, | |
914 | struct device_attribute *attr, char *buf) | |
915 | { | |
916 | struct platform_device *pdev = to_platform_device(dev); | |
62655397 | 917 | ssize_t len; |
3d713e0e | 918 | |
62655397 AS |
919 | device_lock(dev); |
920 | len = sprintf(buf, "%s\n", pdev->driver_override); | |
921 | device_unlock(dev); | |
922 | return len; | |
3d713e0e KP |
923 | } |
924 | static DEVICE_ATTR_RW(driver_override); | |
925 | ||
926 | ||
d06262e5 GKH |
927 | static struct attribute *platform_dev_attrs[] = { |
928 | &dev_attr_modalias.attr, | |
3d713e0e | 929 | &dev_attr_driver_override.attr, |
d06262e5 | 930 | NULL, |
a0245f7a | 931 | }; |
d06262e5 | 932 | ATTRIBUTE_GROUPS(platform_dev); |
a0245f7a | 933 | |
7eff2e7a | 934 | static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) |
a0245f7a DB |
935 | { |
936 | struct platform_device *pdev = to_platform_device(dev); | |
eca39301 GL |
937 | int rc; |
938 | ||
939 | /* Some devices have extra OF data and an OF-style MODALIAS */ | |
0258e182 | 940 | rc = of_device_uevent_modalias(dev, env); |
eca39301 GL |
941 | if (rc != -ENODEV) |
942 | return rc; | |
a0245f7a | 943 | |
8c4ff6d0 ZR |
944 | rc = acpi_device_uevent_modalias(dev, env); |
945 | if (rc != -ENODEV) | |
946 | return rc; | |
947 | ||
57fee4a5 | 948 | add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, |
0a26813c | 949 | pdev->name); |
a0245f7a DB |
950 | return 0; |
951 | } | |
952 | ||
57fee4a5 | 953 | static const struct platform_device_id *platform_match_id( |
831fad2f | 954 | const struct platform_device_id *id, |
57fee4a5 EM |
955 | struct platform_device *pdev) |
956 | { | |
957 | while (id->name[0]) { | |
958 | if (strcmp(pdev->name, id->name) == 0) { | |
959 | pdev->id_entry = id; | |
960 | return id; | |
961 | } | |
962 | id++; | |
963 | } | |
964 | return NULL; | |
965 | } | |
966 | ||
1da177e4 | 967 | /** |
4a3ad20c GKH |
968 | * platform_match - bind platform device to platform driver. |
969 | * @dev: device. | |
970 | * @drv: driver. | |
1da177e4 | 971 | * |
4a3ad20c GKH |
972 | * Platform device IDs are assumed to be encoded like this: |
973 | * "<name><instance>", where <name> is a short description of the type of | |
974 | * device, like "pci" or "floppy", and <instance> is the enumerated | |
975 | * instance of the device, like '0' or '42'. Driver IDs are simply | |
976 | * "<name>". So, extract the <name> from the platform_device structure, | |
977 | * and compare it against the name of the driver. Return whether they match | |
978 | * or not. | |
1da177e4 | 979 | */ |
4a3ad20c | 980 | static int platform_match(struct device *dev, struct device_driver *drv) |
1da177e4 | 981 | { |
71b3e0c1 | 982 | struct platform_device *pdev = to_platform_device(dev); |
57fee4a5 EM |
983 | struct platform_driver *pdrv = to_platform_driver(drv); |
984 | ||
3d713e0e KP |
985 | /* When driver_override is set, only bind to the matching driver */ |
986 | if (pdev->driver_override) | |
987 | return !strcmp(pdev->driver_override, drv->name); | |
988 | ||
05212157 GL |
989 | /* Attempt an OF style match first */ |
990 | if (of_driver_match_device(dev, drv)) | |
991 | return 1; | |
992 | ||
91e56878 MW |
993 | /* Then try ACPI style match */ |
994 | if (acpi_driver_match_device(dev, drv)) | |
995 | return 1; | |
996 | ||
05212157 | 997 | /* Then try to match against the id table */ |
57fee4a5 EM |
998 | if (pdrv->id_table) |
999 | return platform_match_id(pdrv->id_table, pdev) != NULL; | |
1da177e4 | 1000 | |
57fee4a5 | 1001 | /* fall-back to driver name match */ |
1e0b2cf9 | 1002 | return (strcmp(pdev->name, drv->name) == 0); |
1da177e4 LT |
1003 | } |
1004 | ||
25e18499 RW |
1005 | #ifdef CONFIG_PM_SLEEP |
1006 | ||
1007 | static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) | |
1da177e4 | 1008 | { |
783ea7d4 MD |
1009 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
1010 | struct platform_device *pdev = to_platform_device(dev); | |
1da177e4 LT |
1011 | int ret = 0; |
1012 | ||
783ea7d4 MD |
1013 | if (dev->driver && pdrv->suspend) |
1014 | ret = pdrv->suspend(pdev, mesg); | |
386415d8 DB |
1015 | |
1016 | return ret; | |
1017 | } | |
1018 | ||
25e18499 | 1019 | static int platform_legacy_resume(struct device *dev) |
1da177e4 | 1020 | { |
783ea7d4 MD |
1021 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
1022 | struct platform_device *pdev = to_platform_device(dev); | |
1da177e4 LT |
1023 | int ret = 0; |
1024 | ||
783ea7d4 MD |
1025 | if (dev->driver && pdrv->resume) |
1026 | ret = pdrv->resume(pdev); | |
9480e307 | 1027 | |
1da177e4 LT |
1028 | return ret; |
1029 | } | |
1030 | ||
69c9dd1e | 1031 | #endif /* CONFIG_PM_SLEEP */ |
9d730229 | 1032 | |
25e18499 RW |
1033 | #ifdef CONFIG_SUSPEND |
1034 | ||
69c9dd1e | 1035 | int platform_pm_suspend(struct device *dev) |
25e18499 RW |
1036 | { |
1037 | struct device_driver *drv = dev->driver; | |
1038 | int ret = 0; | |
1039 | ||
adf09493 RW |
1040 | if (!drv) |
1041 | return 0; | |
1042 | ||
1043 | if (drv->pm) { | |
25e18499 RW |
1044 | if (drv->pm->suspend) |
1045 | ret = drv->pm->suspend(dev); | |
1046 | } else { | |
1047 | ret = platform_legacy_suspend(dev, PMSG_SUSPEND); | |
1048 | } | |
1049 | ||
1050 | return ret; | |
1051 | } | |
1052 | ||
69c9dd1e | 1053 | int platform_pm_resume(struct device *dev) |
25e18499 RW |
1054 | { |
1055 | struct device_driver *drv = dev->driver; | |
1056 | int ret = 0; | |
1057 | ||
adf09493 RW |
1058 | if (!drv) |
1059 | return 0; | |
1060 | ||
1061 | if (drv->pm) { | |
25e18499 RW |
1062 | if (drv->pm->resume) |
1063 | ret = drv->pm->resume(dev); | |
1064 | } else { | |
1065 | ret = platform_legacy_resume(dev); | |
1066 | } | |
1067 | ||
1068 | return ret; | |
1069 | } | |
1070 | ||
69c9dd1e | 1071 | #endif /* CONFIG_SUSPEND */ |
25e18499 | 1072 | |
1f112cee | 1073 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
25e18499 | 1074 | |
69c9dd1e | 1075 | int platform_pm_freeze(struct device *dev) |
25e18499 RW |
1076 | { |
1077 | struct device_driver *drv = dev->driver; | |
1078 | int ret = 0; | |
1079 | ||
1080 | if (!drv) | |
1081 | return 0; | |
1082 | ||
1083 | if (drv->pm) { | |
1084 | if (drv->pm->freeze) | |
1085 | ret = drv->pm->freeze(dev); | |
1086 | } else { | |
1087 | ret = platform_legacy_suspend(dev, PMSG_FREEZE); | |
1088 | } | |
1089 | ||
1090 | return ret; | |
1091 | } | |
1092 | ||
69c9dd1e | 1093 | int platform_pm_thaw(struct device *dev) |
25e18499 RW |
1094 | { |
1095 | struct device_driver *drv = dev->driver; | |
1096 | int ret = 0; | |
1097 | ||
adf09493 RW |
1098 | if (!drv) |
1099 | return 0; | |
1100 | ||
1101 | if (drv->pm) { | |
25e18499 RW |
1102 | if (drv->pm->thaw) |
1103 | ret = drv->pm->thaw(dev); | |
1104 | } else { | |
1105 | ret = platform_legacy_resume(dev); | |
1106 | } | |
1107 | ||
1108 | return ret; | |
1109 | } | |
1110 | ||
69c9dd1e | 1111 | int platform_pm_poweroff(struct device *dev) |
25e18499 RW |
1112 | { |
1113 | struct device_driver *drv = dev->driver; | |
1114 | int ret = 0; | |
1115 | ||
adf09493 RW |
1116 | if (!drv) |
1117 | return 0; | |
1118 | ||
1119 | if (drv->pm) { | |
25e18499 RW |
1120 | if (drv->pm->poweroff) |
1121 | ret = drv->pm->poweroff(dev); | |
1122 | } else { | |
1123 | ret = platform_legacy_suspend(dev, PMSG_HIBERNATE); | |
1124 | } | |
1125 | ||
1126 | return ret; | |
1127 | } | |
1128 | ||
69c9dd1e | 1129 | int platform_pm_restore(struct device *dev) |
25e18499 RW |
1130 | { |
1131 | struct device_driver *drv = dev->driver; | |
1132 | int ret = 0; | |
1133 | ||
adf09493 RW |
1134 | if (!drv) |
1135 | return 0; | |
1136 | ||
1137 | if (drv->pm) { | |
25e18499 RW |
1138 | if (drv->pm->restore) |
1139 | ret = drv->pm->restore(dev); | |
1140 | } else { | |
1141 | ret = platform_legacy_resume(dev); | |
1142 | } | |
1143 | ||
1144 | return ret; | |
1145 | } | |
1146 | ||
69c9dd1e | 1147 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
25e18499 | 1148 | |
07397df2 NG |
1149 | int platform_dma_configure(struct device *dev) |
1150 | { | |
1151 | enum dev_dma_attr attr; | |
1152 | int ret = 0; | |
1153 | ||
1154 | if (dev->of_node) { | |
3d6ce86e | 1155 | ret = of_dma_configure(dev, dev->of_node, true); |
07397df2 NG |
1156 | } else if (has_acpi_companion(dev)) { |
1157 | attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode)); | |
e5361ca2 | 1158 | ret = acpi_dma_configure(dev, attr); |
07397df2 NG |
1159 | } |
1160 | ||
1161 | return ret; | |
1162 | } | |
1163 | ||
d9ab7716 | 1164 | static const struct dev_pm_ops platform_dev_pm_ops = { |
8b313a38 RW |
1165 | .runtime_suspend = pm_generic_runtime_suspend, |
1166 | .runtime_resume = pm_generic_runtime_resume, | |
69c9dd1e | 1167 | USE_PLATFORM_PM_SLEEP_OPS |
25e18499 RW |
1168 | }; |
1169 | ||
1da177e4 LT |
1170 | struct bus_type platform_bus_type = { |
1171 | .name = "platform", | |
d06262e5 | 1172 | .dev_groups = platform_dev_groups, |
1da177e4 | 1173 | .match = platform_match, |
a0245f7a | 1174 | .uevent = platform_uevent, |
07397df2 | 1175 | .dma_configure = platform_dma_configure, |
9d730229 | 1176 | .pm = &platform_dev_pm_ops, |
1da177e4 | 1177 | }; |
a96b2042 | 1178 | EXPORT_SYMBOL_GPL(platform_bus_type); |
1da177e4 LT |
1179 | |
1180 | int __init platform_bus_init(void) | |
1181 | { | |
fbfb1445 CH |
1182 | int error; |
1183 | ||
13977091 MD |
1184 | early_platform_cleanup(); |
1185 | ||
fbfb1445 | 1186 | error = device_register(&platform_bus); |
c8ae1674 AY |
1187 | if (error) { |
1188 | put_device(&platform_bus); | |
fbfb1445 | 1189 | return error; |
c8ae1674 | 1190 | } |
fbfb1445 CH |
1191 | error = bus_register(&platform_bus_type); |
1192 | if (error) | |
1193 | device_unregister(&platform_bus); | |
801d728c | 1194 | of_platform_register_reconfig_notifier(); |
fbfb1445 | 1195 | return error; |
1da177e4 LT |
1196 | } |
1197 | ||
13977091 MD |
1198 | static __initdata LIST_HEAD(early_platform_driver_list); |
1199 | static __initdata LIST_HEAD(early_platform_device_list); | |
1200 | ||
1201 | /** | |
4d26e139 | 1202 | * early_platform_driver_register - register early platform driver |
d86c1302 | 1203 | * @epdrv: early_platform driver structure |
13977091 | 1204 | * @buf: string passed from early_param() |
4d26e139 MD |
1205 | * |
1206 | * Helper function for early_platform_init() / early_platform_init_buffer() | |
13977091 MD |
1207 | */ |
1208 | int __init early_platform_driver_register(struct early_platform_driver *epdrv, | |
1209 | char *buf) | |
1210 | { | |
c60e0504 | 1211 | char *tmp; |
13977091 MD |
1212 | int n; |
1213 | ||
1214 | /* Simply add the driver to the end of the global list. | |
1215 | * Drivers will by default be put on the list in compiled-in order. | |
1216 | */ | |
1217 | if (!epdrv->list.next) { | |
1218 | INIT_LIST_HEAD(&epdrv->list); | |
1219 | list_add_tail(&epdrv->list, &early_platform_driver_list); | |
1220 | } | |
1221 | ||
1222 | /* If the user has specified device then make sure the driver | |
1223 | * gets prioritized. The driver of the last device specified on | |
1224 | * command line will be put first on the list. | |
1225 | */ | |
1226 | n = strlen(epdrv->pdrv->driver.name); | |
1227 | if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) { | |
1228 | list_move(&epdrv->list, &early_platform_driver_list); | |
1229 | ||
c60e0504 MD |
1230 | /* Allow passing parameters after device name */ |
1231 | if (buf[n] == '\0' || buf[n] == ',') | |
13977091 | 1232 | epdrv->requested_id = -1; |
c60e0504 MD |
1233 | else { |
1234 | epdrv->requested_id = simple_strtoul(&buf[n + 1], | |
1235 | &tmp, 10); | |
1236 | ||
1237 | if (buf[n] != '.' || (tmp == &buf[n + 1])) { | |
1238 | epdrv->requested_id = EARLY_PLATFORM_ID_ERROR; | |
1239 | n = 0; | |
1240 | } else | |
1241 | n += strcspn(&buf[n + 1], ",") + 1; | |
1242 | } | |
1243 | ||
1244 | if (buf[n] == ',') | |
1245 | n++; | |
1246 | ||
1247 | if (epdrv->bufsize) { | |
1248 | memcpy(epdrv->buffer, &buf[n], | |
1249 | min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1)); | |
1250 | epdrv->buffer[epdrv->bufsize - 1] = '\0'; | |
1251 | } | |
13977091 MD |
1252 | } |
1253 | ||
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | /** | |
4d26e139 | 1258 | * early_platform_add_devices - adds a number of early platform devices |
13977091 MD |
1259 | * @devs: array of early platform devices to add |
1260 | * @num: number of early platform devices in array | |
4d26e139 MD |
1261 | * |
1262 | * Used by early architecture code to register early platform devices and | |
1263 | * their platform data. | |
13977091 MD |
1264 | */ |
1265 | void __init early_platform_add_devices(struct platform_device **devs, int num) | |
1266 | { | |
1267 | struct device *dev; | |
1268 | int i; | |
1269 | ||
1270 | /* simply add the devices to list */ | |
1271 | for (i = 0; i < num; i++) { | |
1272 | dev = &devs[i]->dev; | |
1273 | ||
1274 | if (!dev->devres_head.next) { | |
bed2b42d | 1275 | pm_runtime_early_init(dev); |
13977091 MD |
1276 | INIT_LIST_HEAD(&dev->devres_head); |
1277 | list_add_tail(&dev->devres_head, | |
1278 | &early_platform_device_list); | |
1279 | } | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | /** | |
4d26e139 | 1284 | * early_platform_driver_register_all - register early platform drivers |
13977091 | 1285 | * @class_str: string to identify early platform driver class |
4d26e139 MD |
1286 | * |
1287 | * Used by architecture code to register all early platform drivers | |
1288 | * for a certain class. If omitted then only early platform drivers | |
1289 | * with matching kernel command line class parameters will be registered. | |
13977091 MD |
1290 | */ |
1291 | void __init early_platform_driver_register_all(char *class_str) | |
1292 | { | |
1293 | /* The "class_str" parameter may or may not be present on the kernel | |
1294 | * command line. If it is present then there may be more than one | |
1295 | * matching parameter. | |
1296 | * | |
1297 | * Since we register our early platform drivers using early_param() | |
1298 | * we need to make sure that they also get registered in the case | |
1299 | * when the parameter is missing from the kernel command line. | |
1300 | * | |
1301 | * We use parse_early_options() to make sure the early_param() gets | |
1302 | * called at least once. The early_param() may be called more than | |
1303 | * once since the name of the preferred device may be specified on | |
1304 | * the kernel command line. early_platform_driver_register() handles | |
1305 | * this case for us. | |
1306 | */ | |
1307 | parse_early_options(class_str); | |
1308 | } | |
1309 | ||
1310 | /** | |
4d26e139 | 1311 | * early_platform_match - find early platform device matching driver |
d86c1302 | 1312 | * @epdrv: early platform driver structure |
13977091 MD |
1313 | * @id: id to match against |
1314 | */ | |
a8257910 | 1315 | static struct platform_device * __init |
13977091 MD |
1316 | early_platform_match(struct early_platform_driver *epdrv, int id) |
1317 | { | |
1318 | struct platform_device *pd; | |
1319 | ||
1320 | list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) | |
1321 | if (platform_match(&pd->dev, &epdrv->pdrv->driver)) | |
1322 | if (pd->id == id) | |
1323 | return pd; | |
1324 | ||
1325 | return NULL; | |
1326 | } | |
1327 | ||
1328 | /** | |
4d26e139 | 1329 | * early_platform_left - check if early platform driver has matching devices |
d86c1302 | 1330 | * @epdrv: early platform driver structure |
13977091 MD |
1331 | * @id: return true if id or above exists |
1332 | */ | |
a8257910 | 1333 | static int __init early_platform_left(struct early_platform_driver *epdrv, |
13977091 MD |
1334 | int id) |
1335 | { | |
1336 | struct platform_device *pd; | |
1337 | ||
1338 | list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) | |
1339 | if (platform_match(&pd->dev, &epdrv->pdrv->driver)) | |
1340 | if (pd->id >= id) | |
1341 | return 1; | |
1342 | ||
1343 | return 0; | |
1344 | } | |
1345 | ||
1346 | /** | |
4d26e139 | 1347 | * early_platform_driver_probe_id - probe drivers matching class_str and id |
13977091 MD |
1348 | * @class_str: string to identify early platform driver class |
1349 | * @id: id to match against | |
1350 | * @nr_probe: number of platform devices to successfully probe before exiting | |
1351 | */ | |
1352 | static int __init early_platform_driver_probe_id(char *class_str, | |
1353 | int id, | |
1354 | int nr_probe) | |
1355 | { | |
1356 | struct early_platform_driver *epdrv; | |
1357 | struct platform_device *match; | |
1358 | int match_id; | |
1359 | int n = 0; | |
1360 | int left = 0; | |
1361 | ||
1362 | list_for_each_entry(epdrv, &early_platform_driver_list, list) { | |
1363 | /* only use drivers matching our class_str */ | |
1364 | if (strcmp(class_str, epdrv->class_str)) | |
1365 | continue; | |
1366 | ||
1367 | if (id == -2) { | |
1368 | match_id = epdrv->requested_id; | |
1369 | left = 1; | |
1370 | ||
1371 | } else { | |
1372 | match_id = id; | |
1373 | left += early_platform_left(epdrv, id); | |
1374 | ||
1375 | /* skip requested id */ | |
1376 | switch (epdrv->requested_id) { | |
1377 | case EARLY_PLATFORM_ID_ERROR: | |
1378 | case EARLY_PLATFORM_ID_UNSET: | |
1379 | break; | |
1380 | default: | |
1381 | if (epdrv->requested_id == id) | |
1382 | match_id = EARLY_PLATFORM_ID_UNSET; | |
1383 | } | |
1384 | } | |
1385 | ||
1386 | switch (match_id) { | |
1387 | case EARLY_PLATFORM_ID_ERROR: | |
0258e182 FP |
1388 | pr_warn("%s: unable to parse %s parameter\n", |
1389 | class_str, epdrv->pdrv->driver.name); | |
13977091 MD |
1390 | /* fall-through */ |
1391 | case EARLY_PLATFORM_ID_UNSET: | |
1392 | match = NULL; | |
1393 | break; | |
1394 | default: | |
1395 | match = early_platform_match(epdrv, match_id); | |
1396 | } | |
1397 | ||
1398 | if (match) { | |
a636ee7f PM |
1399 | /* |
1400 | * Set up a sensible init_name to enable | |
1401 | * dev_name() and others to be used before the | |
1402 | * rest of the driver core is initialized. | |
1403 | */ | |
06fe53be | 1404 | if (!match->dev.init_name && slab_is_available()) { |
a636ee7f | 1405 | if (match->id != -1) |
bd05086b PM |
1406 | match->dev.init_name = |
1407 | kasprintf(GFP_KERNEL, "%s.%d", | |
1408 | match->name, | |
1409 | match->id); | |
a636ee7f | 1410 | else |
bd05086b PM |
1411 | match->dev.init_name = |
1412 | kasprintf(GFP_KERNEL, "%s", | |
1413 | match->name); | |
a636ee7f | 1414 | |
a636ee7f PM |
1415 | if (!match->dev.init_name) |
1416 | return -ENOMEM; | |
1417 | } | |
bd05086b | 1418 | |
13977091 | 1419 | if (epdrv->pdrv->probe(match)) |
0258e182 FP |
1420 | pr_warn("%s: unable to probe %s early.\n", |
1421 | class_str, match->name); | |
13977091 MD |
1422 | else |
1423 | n++; | |
1424 | } | |
1425 | ||
1426 | if (n >= nr_probe) | |
1427 | break; | |
1428 | } | |
1429 | ||
1430 | if (left) | |
1431 | return n; | |
1432 | else | |
1433 | return -ENODEV; | |
1434 | } | |
1435 | ||
1436 | /** | |
4d26e139 | 1437 | * early_platform_driver_probe - probe a class of registered drivers |
13977091 MD |
1438 | * @class_str: string to identify early platform driver class |
1439 | * @nr_probe: number of platform devices to successfully probe before exiting | |
1440 | * @user_only: only probe user specified early platform devices | |
4d26e139 MD |
1441 | * |
1442 | * Used by architecture code to probe registered early platform drivers | |
1443 | * within a certain class. For probe to happen a registered early platform | |
1444 | * device matching a registered early platform driver is needed. | |
13977091 MD |
1445 | */ |
1446 | int __init early_platform_driver_probe(char *class_str, | |
1447 | int nr_probe, | |
1448 | int user_only) | |
1449 | { | |
1450 | int k, n, i; | |
1451 | ||
1452 | n = 0; | |
1453 | for (i = -2; n < nr_probe; i++) { | |
1454 | k = early_platform_driver_probe_id(class_str, i, nr_probe - n); | |
1455 | ||
1456 | if (k < 0) | |
1457 | break; | |
1458 | ||
1459 | n += k; | |
1460 | ||
1461 | if (user_only) | |
1462 | break; | |
1463 | } | |
1464 | ||
1465 | return n; | |
1466 | } | |
1467 | ||
1468 | /** | |
1469 | * early_platform_cleanup - clean up early platform code | |
1470 | */ | |
1471 | void __init early_platform_cleanup(void) | |
1472 | { | |
1473 | struct platform_device *pd, *pd2; | |
1474 | ||
1475 | /* clean up the devres list used to chain devices */ | |
1476 | list_for_each_entry_safe(pd, pd2, &early_platform_device_list, | |
1477 | dev.devres_head) { | |
1478 | list_del(&pd->dev.devres_head); | |
1479 | memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head)); | |
1480 | } | |
1481 | } | |
1482 |