Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp. |
| 3 | * <benh@kernel.crashing.org> |
| 4 | * and Arnd Bergmann, IBM Corp. |
| 5 | * Merged from powerpc/kernel/of_platform.c and |
| 6 | * sparc{,64}/kernel/of_device.c by Stephen Rothwell |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | #include <linux/errno.h> |
Stephen Rothwell | 5c45708 | 2007-10-17 21:17:42 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 16 | #include <linux/device.h> |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame^] | 17 | #include <linux/dma-mapping.h> |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 18 | #include <linux/of_device.h> |
| 19 | #include <linux/of_platform.h> |
| 20 | |
Olaf Hering | 140b932 | 2008-04-24 23:16:00 +1000 | [diff] [blame] | 21 | extern struct device_attribute of_platform_device_attrs[]; |
| 22 | |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 23 | static int of_platform_bus_match(struct device *dev, struct device_driver *drv) |
| 24 | { |
Grant Likely | 597b9d1 | 2010-04-13 16:13:01 -0700 | [diff] [blame] | 25 | const struct of_device_id *matches = drv->of_match_table; |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 26 | |
| 27 | if (!matches) |
| 28 | return 0; |
| 29 | |
Grant Likely | 44504b2 | 2010-04-13 16:13:22 -0700 | [diff] [blame] | 30 | return of_match_device(matches, dev) != NULL; |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static int of_platform_device_probe(struct device *dev) |
| 34 | { |
| 35 | int error = -ENODEV; |
| 36 | struct of_platform_driver *drv; |
| 37 | struct of_device *of_dev; |
| 38 | const struct of_device_id *match; |
| 39 | |
| 40 | drv = to_of_platform_driver(dev->driver); |
| 41 | of_dev = to_of_device(dev); |
| 42 | |
| 43 | if (!drv->probe) |
| 44 | return error; |
| 45 | |
| 46 | of_dev_get(of_dev); |
| 47 | |
Grant Likely | 44504b2 | 2010-04-13 16:13:22 -0700 | [diff] [blame] | 48 | match = of_match_device(drv->driver.of_match_table, dev); |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 49 | if (match) |
| 50 | error = drv->probe(of_dev, match); |
| 51 | if (error) |
| 52 | of_dev_put(of_dev); |
| 53 | |
| 54 | return error; |
| 55 | } |
| 56 | |
| 57 | static int of_platform_device_remove(struct device *dev) |
| 58 | { |
| 59 | struct of_device *of_dev = to_of_device(dev); |
| 60 | struct of_platform_driver *drv = to_of_platform_driver(dev->driver); |
| 61 | |
| 62 | if (dev->driver && drv->remove) |
| 63 | drv->remove(of_dev); |
| 64 | return 0; |
| 65 | } |
| 66 | |
Michael Ellerman | a09ad3c | 2008-01-25 16:59:13 +1100 | [diff] [blame] | 67 | static void of_platform_device_shutdown(struct device *dev) |
| 68 | { |
| 69 | struct of_device *of_dev = to_of_device(dev); |
| 70 | struct of_platform_driver *drv = to_of_platform_driver(dev->driver); |
| 71 | |
| 72 | if (dev->driver && drv->shutdown) |
| 73 | drv->shutdown(of_dev); |
| 74 | } |
| 75 | |
Anton Vorontsov | d35ef90 | 2009-10-12 05:50:41 +0000 | [diff] [blame] | 76 | #ifdef CONFIG_PM_SLEEP |
| 77 | |
| 78 | static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg) |
| 79 | { |
| 80 | struct of_device *of_dev = to_of_device(dev); |
| 81 | struct of_platform_driver *drv = to_of_platform_driver(dev->driver); |
| 82 | int ret = 0; |
| 83 | |
| 84 | if (dev->driver && drv->suspend) |
| 85 | ret = drv->suspend(of_dev, mesg); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | static int of_platform_legacy_resume(struct device *dev) |
| 90 | { |
| 91 | struct of_device *of_dev = to_of_device(dev); |
| 92 | struct of_platform_driver *drv = to_of_platform_driver(dev->driver); |
| 93 | int ret = 0; |
| 94 | |
| 95 | if (dev->driver && drv->resume) |
| 96 | ret = drv->resume(of_dev); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int of_platform_pm_prepare(struct device *dev) |
| 101 | { |
| 102 | struct device_driver *drv = dev->driver; |
| 103 | int ret = 0; |
| 104 | |
| 105 | if (drv && drv->pm && drv->pm->prepare) |
| 106 | ret = drv->pm->prepare(dev); |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | static void of_platform_pm_complete(struct device *dev) |
| 112 | { |
| 113 | struct device_driver *drv = dev->driver; |
| 114 | |
| 115 | if (drv && drv->pm && drv->pm->complete) |
| 116 | drv->pm->complete(dev); |
| 117 | } |
| 118 | |
| 119 | #ifdef CONFIG_SUSPEND |
| 120 | |
| 121 | static int of_platform_pm_suspend(struct device *dev) |
| 122 | { |
| 123 | struct device_driver *drv = dev->driver; |
| 124 | int ret = 0; |
| 125 | |
| 126 | if (!drv) |
| 127 | return 0; |
| 128 | |
| 129 | if (drv->pm) { |
| 130 | if (drv->pm->suspend) |
| 131 | ret = drv->pm->suspend(dev); |
| 132 | } else { |
| 133 | ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND); |
| 134 | } |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static int of_platform_pm_suspend_noirq(struct device *dev) |
| 140 | { |
| 141 | struct device_driver *drv = dev->driver; |
| 142 | int ret = 0; |
| 143 | |
| 144 | if (!drv) |
| 145 | return 0; |
| 146 | |
| 147 | if (drv->pm) { |
| 148 | if (drv->pm->suspend_noirq) |
| 149 | ret = drv->pm->suspend_noirq(dev); |
| 150 | } |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | static int of_platform_pm_resume(struct device *dev) |
| 156 | { |
| 157 | struct device_driver *drv = dev->driver; |
| 158 | int ret = 0; |
| 159 | |
| 160 | if (!drv) |
| 161 | return 0; |
| 162 | |
| 163 | if (drv->pm) { |
| 164 | if (drv->pm->resume) |
| 165 | ret = drv->pm->resume(dev); |
| 166 | } else { |
| 167 | ret = of_platform_legacy_resume(dev); |
| 168 | } |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static int of_platform_pm_resume_noirq(struct device *dev) |
| 174 | { |
| 175 | struct device_driver *drv = dev->driver; |
| 176 | int ret = 0; |
| 177 | |
| 178 | if (!drv) |
| 179 | return 0; |
| 180 | |
| 181 | if (drv->pm) { |
| 182 | if (drv->pm->resume_noirq) |
| 183 | ret = drv->pm->resume_noirq(dev); |
| 184 | } |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | #else /* !CONFIG_SUSPEND */ |
| 190 | |
| 191 | #define of_platform_pm_suspend NULL |
| 192 | #define of_platform_pm_resume NULL |
| 193 | #define of_platform_pm_suspend_noirq NULL |
| 194 | #define of_platform_pm_resume_noirq NULL |
| 195 | |
| 196 | #endif /* !CONFIG_SUSPEND */ |
| 197 | |
| 198 | #ifdef CONFIG_HIBERNATION |
| 199 | |
| 200 | static int of_platform_pm_freeze(struct device *dev) |
| 201 | { |
| 202 | struct device_driver *drv = dev->driver; |
| 203 | int ret = 0; |
| 204 | |
| 205 | if (!drv) |
| 206 | return 0; |
| 207 | |
| 208 | if (drv->pm) { |
| 209 | if (drv->pm->freeze) |
| 210 | ret = drv->pm->freeze(dev); |
| 211 | } else { |
| 212 | ret = of_platform_legacy_suspend(dev, PMSG_FREEZE); |
| 213 | } |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static int of_platform_pm_freeze_noirq(struct device *dev) |
| 219 | { |
| 220 | struct device_driver *drv = dev->driver; |
| 221 | int ret = 0; |
| 222 | |
| 223 | if (!drv) |
| 224 | return 0; |
| 225 | |
| 226 | if (drv->pm) { |
| 227 | if (drv->pm->freeze_noirq) |
| 228 | ret = drv->pm->freeze_noirq(dev); |
| 229 | } |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static int of_platform_pm_thaw(struct device *dev) |
| 235 | { |
| 236 | struct device_driver *drv = dev->driver; |
| 237 | int ret = 0; |
| 238 | |
| 239 | if (!drv) |
| 240 | return 0; |
| 241 | |
| 242 | if (drv->pm) { |
| 243 | if (drv->pm->thaw) |
| 244 | ret = drv->pm->thaw(dev); |
| 245 | } else { |
| 246 | ret = of_platform_legacy_resume(dev); |
| 247 | } |
| 248 | |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | static int of_platform_pm_thaw_noirq(struct device *dev) |
| 253 | { |
| 254 | struct device_driver *drv = dev->driver; |
| 255 | int ret = 0; |
| 256 | |
| 257 | if (!drv) |
| 258 | return 0; |
| 259 | |
| 260 | if (drv->pm) { |
| 261 | if (drv->pm->thaw_noirq) |
| 262 | ret = drv->pm->thaw_noirq(dev); |
| 263 | } |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | static int of_platform_pm_poweroff(struct device *dev) |
| 269 | { |
| 270 | struct device_driver *drv = dev->driver; |
| 271 | int ret = 0; |
| 272 | |
| 273 | if (!drv) |
| 274 | return 0; |
| 275 | |
| 276 | if (drv->pm) { |
| 277 | if (drv->pm->poweroff) |
| 278 | ret = drv->pm->poweroff(dev); |
| 279 | } else { |
| 280 | ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE); |
| 281 | } |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static int of_platform_pm_poweroff_noirq(struct device *dev) |
| 287 | { |
| 288 | struct device_driver *drv = dev->driver; |
| 289 | int ret = 0; |
| 290 | |
| 291 | if (!drv) |
| 292 | return 0; |
| 293 | |
| 294 | if (drv->pm) { |
| 295 | if (drv->pm->poweroff_noirq) |
| 296 | ret = drv->pm->poweroff_noirq(dev); |
| 297 | } |
| 298 | |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static int of_platform_pm_restore(struct device *dev) |
| 303 | { |
| 304 | struct device_driver *drv = dev->driver; |
| 305 | int ret = 0; |
| 306 | |
| 307 | if (!drv) |
| 308 | return 0; |
| 309 | |
| 310 | if (drv->pm) { |
| 311 | if (drv->pm->restore) |
| 312 | ret = drv->pm->restore(dev); |
| 313 | } else { |
| 314 | ret = of_platform_legacy_resume(dev); |
| 315 | } |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static int of_platform_pm_restore_noirq(struct device *dev) |
| 321 | { |
| 322 | struct device_driver *drv = dev->driver; |
| 323 | int ret = 0; |
| 324 | |
| 325 | if (!drv) |
| 326 | return 0; |
| 327 | |
| 328 | if (drv->pm) { |
| 329 | if (drv->pm->restore_noirq) |
| 330 | ret = drv->pm->restore_noirq(dev); |
| 331 | } |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | #else /* !CONFIG_HIBERNATION */ |
| 337 | |
| 338 | #define of_platform_pm_freeze NULL |
| 339 | #define of_platform_pm_thaw NULL |
| 340 | #define of_platform_pm_poweroff NULL |
| 341 | #define of_platform_pm_restore NULL |
| 342 | #define of_platform_pm_freeze_noirq NULL |
| 343 | #define of_platform_pm_thaw_noirq NULL |
| 344 | #define of_platform_pm_poweroff_noirq NULL |
| 345 | #define of_platform_pm_restore_noirq NULL |
| 346 | |
| 347 | #endif /* !CONFIG_HIBERNATION */ |
| 348 | |
| 349 | static struct dev_pm_ops of_platform_dev_pm_ops = { |
| 350 | .prepare = of_platform_pm_prepare, |
| 351 | .complete = of_platform_pm_complete, |
| 352 | .suspend = of_platform_pm_suspend, |
| 353 | .resume = of_platform_pm_resume, |
| 354 | .freeze = of_platform_pm_freeze, |
| 355 | .thaw = of_platform_pm_thaw, |
| 356 | .poweroff = of_platform_pm_poweroff, |
| 357 | .restore = of_platform_pm_restore, |
| 358 | .suspend_noirq = of_platform_pm_suspend_noirq, |
| 359 | .resume_noirq = of_platform_pm_resume_noirq, |
| 360 | .freeze_noirq = of_platform_pm_freeze_noirq, |
| 361 | .thaw_noirq = of_platform_pm_thaw_noirq, |
| 362 | .poweroff_noirq = of_platform_pm_poweroff_noirq, |
| 363 | .restore_noirq = of_platform_pm_restore_noirq, |
| 364 | }; |
| 365 | |
| 366 | #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops) |
| 367 | |
| 368 | #else /* !CONFIG_PM_SLEEP */ |
| 369 | |
| 370 | #define OF_PLATFORM_PM_OPS_PTR NULL |
| 371 | |
| 372 | #endif /* !CONFIG_PM_SLEEP */ |
| 373 | |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 374 | int of_bus_type_init(struct bus_type *bus, const char *name) |
| 375 | { |
| 376 | bus->name = name; |
| 377 | bus->match = of_platform_bus_match; |
| 378 | bus->probe = of_platform_device_probe; |
| 379 | bus->remove = of_platform_device_remove; |
Michael Ellerman | a09ad3c | 2008-01-25 16:59:13 +1100 | [diff] [blame] | 380 | bus->shutdown = of_platform_device_shutdown; |
Olaf Hering | 140b932 | 2008-04-24 23:16:00 +1000 | [diff] [blame] | 381 | bus->dev_attrs = of_platform_device_attrs; |
Anton Vorontsov | d35ef90 | 2009-10-12 05:50:41 +0000 | [diff] [blame] | 382 | bus->pm = OF_PLATFORM_PM_OPS_PTR; |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 383 | return bus_register(bus); |
| 384 | } |
Stephen Rothwell | 5c45708 | 2007-10-17 21:17:42 -0700 | [diff] [blame] | 385 | |
| 386 | int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) |
| 387 | { |
Stephen Rothwell | 5c45708 | 2007-10-17 21:17:42 -0700 | [diff] [blame] | 388 | drv->driver.bus = bus; |
| 389 | |
| 390 | /* register with core */ |
| 391 | return driver_register(&drv->driver); |
| 392 | } |
| 393 | EXPORT_SYMBOL(of_register_driver); |
| 394 | |
| 395 | void of_unregister_driver(struct of_platform_driver *drv) |
| 396 | { |
| 397 | driver_unregister(&drv->driver); |
| 398 | } |
| 399 | EXPORT_SYMBOL(of_unregister_driver); |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame^] | 400 | |
| 401 | #if !defined(CONFIG_SPARC) |
| 402 | /* |
| 403 | * The following routines scan a subtree and registers a device for |
| 404 | * each applicable node. |
| 405 | * |
| 406 | * Note: sparc doesn't use these routines because it has a different |
| 407 | * mechanism for creating devices from device tree nodes. |
| 408 | */ |
| 409 | |
| 410 | /** |
| 411 | * of_platform_device_create - Alloc, initialize and register an of_device |
| 412 | * @np: pointer to node to create device for |
| 413 | * @bus_id: name to assign device |
| 414 | * @parent: Linux device model parent device. |
| 415 | */ |
| 416 | struct of_device *of_platform_device_create(struct device_node *np, |
| 417 | const char *bus_id, |
| 418 | struct device *parent) |
| 419 | { |
| 420 | struct of_device *dev; |
| 421 | |
| 422 | dev = of_device_alloc(np, bus_id, parent); |
| 423 | if (!dev) |
| 424 | return NULL; |
| 425 | |
| 426 | dev->archdata.dma_mask = 0xffffffffUL; |
| 427 | dev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
| 428 | dev->dev.bus = &of_platform_bus_type; |
| 429 | |
| 430 | /* We do not fill the DMA ops for platform devices by default. |
| 431 | * This is currently the responsibility of the platform code |
| 432 | * to do such, possibly using a device notifier |
| 433 | */ |
| 434 | |
| 435 | if (of_device_register(dev) != 0) { |
| 436 | of_device_free(dev); |
| 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | return dev; |
| 441 | } |
| 442 | EXPORT_SYMBOL(of_platform_device_create); |
| 443 | |
| 444 | /** |
| 445 | * of_platform_bus_create - Create an OF device for a bus node and all its |
| 446 | * children. Optionally recursively instantiate matching busses. |
| 447 | * @bus: device node of the bus to instantiate |
| 448 | * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to |
| 449 | * disallow recursive creation of child busses |
| 450 | */ |
| 451 | static int of_platform_bus_create(const struct device_node *bus, |
| 452 | const struct of_device_id *matches, |
| 453 | struct device *parent) |
| 454 | { |
| 455 | struct device_node *child; |
| 456 | struct of_device *dev; |
| 457 | int rc = 0; |
| 458 | |
| 459 | for_each_child_of_node(bus, child) { |
| 460 | pr_debug(" create child: %s\n", child->full_name); |
| 461 | dev = of_platform_device_create(child, NULL, parent); |
| 462 | if (dev == NULL) |
| 463 | rc = -ENOMEM; |
| 464 | else if (!of_match_node(matches, child)) |
| 465 | continue; |
| 466 | if (rc == 0) { |
| 467 | pr_debug(" and sub busses\n"); |
| 468 | rc = of_platform_bus_create(child, matches, &dev->dev); |
| 469 | } |
| 470 | if (rc) { |
| 471 | of_node_put(child); |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | return rc; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * of_platform_bus_probe - Probe the device-tree for platform busses |
| 480 | * @root: parent of the first level to probe or NULL for the root of the tree |
| 481 | * @matches: match table, NULL to use the default |
| 482 | * @parent: parent to hook devices from, NULL for toplevel |
| 483 | * |
| 484 | * Note that children of the provided root are not instantiated as devices |
| 485 | * unless the specified root itself matches the bus list and is not NULL. |
| 486 | */ |
| 487 | int of_platform_bus_probe(struct device_node *root, |
| 488 | const struct of_device_id *matches, |
| 489 | struct device *parent) |
| 490 | { |
| 491 | struct device_node *child; |
| 492 | struct of_device *dev; |
| 493 | int rc = 0; |
| 494 | |
| 495 | if (matches == NULL) |
| 496 | matches = of_default_bus_ids; |
| 497 | if (matches == OF_NO_DEEP_PROBE) |
| 498 | return -EINVAL; |
| 499 | if (root == NULL) |
| 500 | root = of_find_node_by_path("/"); |
| 501 | else |
| 502 | of_node_get(root); |
| 503 | |
| 504 | pr_debug("of_platform_bus_probe()\n"); |
| 505 | pr_debug(" starting at: %s\n", root->full_name); |
| 506 | |
| 507 | /* Do a self check of bus type, if there's a match, create |
| 508 | * children |
| 509 | */ |
| 510 | if (of_match_node(matches, root)) { |
| 511 | pr_debug(" root match, create all sub devices\n"); |
| 512 | dev = of_platform_device_create(root, NULL, parent); |
| 513 | if (dev == NULL) { |
| 514 | rc = -ENOMEM; |
| 515 | goto bail; |
| 516 | } |
| 517 | pr_debug(" create all sub busses\n"); |
| 518 | rc = of_platform_bus_create(root, matches, &dev->dev); |
| 519 | goto bail; |
| 520 | } |
| 521 | for_each_child_of_node(root, child) { |
| 522 | if (!of_match_node(matches, child)) |
| 523 | continue; |
| 524 | |
| 525 | pr_debug(" match: %s\n", child->full_name); |
| 526 | dev = of_platform_device_create(child, NULL, parent); |
| 527 | if (dev == NULL) |
| 528 | rc = -ENOMEM; |
| 529 | else |
| 530 | rc = of_platform_bus_create(child, matches, &dev->dev); |
| 531 | if (rc) { |
| 532 | of_node_put(child); |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | bail: |
| 537 | of_node_put(root); |
| 538 | return rc; |
| 539 | } |
| 540 | EXPORT_SYMBOL(of_platform_bus_probe); |
| 541 | #endif /* !CONFIG_SPARC */ |