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> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/of_platform.h> |
| 19 | |
Olaf Hering | 140b932 | 2008-04-24 23:16:00 +1000 | [diff] [blame] | 20 | extern struct device_attribute of_platform_device_attrs[]; |
| 21 | |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 22 | static int of_platform_bus_match(struct device *dev, struct device_driver *drv) |
| 23 | { |
Grant Likely | 597b9d1 | 2010-04-13 16:13:01 -0700 | [diff] [blame] | 24 | const struct of_device_id *matches = drv->of_match_table; |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 25 | |
| 26 | if (!matches) |
| 27 | return 0; |
| 28 | |
Grant Likely | 44504b2 | 2010-04-13 16:13:22 -0700 | [diff] [blame] | 29 | return of_match_device(matches, dev) != NULL; |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static int of_platform_device_probe(struct device *dev) |
| 33 | { |
| 34 | int error = -ENODEV; |
| 35 | struct of_platform_driver *drv; |
| 36 | struct of_device *of_dev; |
| 37 | const struct of_device_id *match; |
| 38 | |
| 39 | drv = to_of_platform_driver(dev->driver); |
| 40 | of_dev = to_of_device(dev); |
| 41 | |
| 42 | if (!drv->probe) |
| 43 | return error; |
| 44 | |
| 45 | of_dev_get(of_dev); |
| 46 | |
Grant Likely | 44504b2 | 2010-04-13 16:13:22 -0700 | [diff] [blame] | 47 | match = of_match_device(drv->driver.of_match_table, dev); |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 48 | if (match) |
| 49 | error = drv->probe(of_dev, match); |
| 50 | if (error) |
| 51 | of_dev_put(of_dev); |
| 52 | |
| 53 | return error; |
| 54 | } |
| 55 | |
| 56 | static int of_platform_device_remove(struct device *dev) |
| 57 | { |
| 58 | struct of_device *of_dev = to_of_device(dev); |
| 59 | struct of_platform_driver *drv = to_of_platform_driver(dev->driver); |
| 60 | |
| 61 | if (dev->driver && drv->remove) |
| 62 | drv->remove(of_dev); |
| 63 | return 0; |
| 64 | } |
| 65 | |
Michael Ellerman | a09ad3c | 2008-01-25 16:59:13 +1100 | [diff] [blame] | 66 | static void of_platform_device_shutdown(struct device *dev) |
| 67 | { |
| 68 | struct of_device *of_dev = to_of_device(dev); |
| 69 | struct of_platform_driver *drv = to_of_platform_driver(dev->driver); |
| 70 | |
| 71 | if (dev->driver && drv->shutdown) |
| 72 | drv->shutdown(of_dev); |
| 73 | } |
| 74 | |
Anton Vorontsov | d35ef90 | 2009-10-12 05:50:41 +0000 | [diff] [blame] | 75 | #ifdef CONFIG_PM_SLEEP |
| 76 | |
| 77 | static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg) |
| 78 | { |
| 79 | struct of_device *of_dev = to_of_device(dev); |
| 80 | struct of_platform_driver *drv = to_of_platform_driver(dev->driver); |
| 81 | int ret = 0; |
| 82 | |
| 83 | if (dev->driver && drv->suspend) |
| 84 | ret = drv->suspend(of_dev, mesg); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static int of_platform_legacy_resume(struct device *dev) |
| 89 | { |
| 90 | struct of_device *of_dev = to_of_device(dev); |
| 91 | struct of_platform_driver *drv = to_of_platform_driver(dev->driver); |
| 92 | int ret = 0; |
| 93 | |
| 94 | if (dev->driver && drv->resume) |
| 95 | ret = drv->resume(of_dev); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static int of_platform_pm_prepare(struct device *dev) |
| 100 | { |
| 101 | struct device_driver *drv = dev->driver; |
| 102 | int ret = 0; |
| 103 | |
| 104 | if (drv && drv->pm && drv->pm->prepare) |
| 105 | ret = drv->pm->prepare(dev); |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | static void of_platform_pm_complete(struct device *dev) |
| 111 | { |
| 112 | struct device_driver *drv = dev->driver; |
| 113 | |
| 114 | if (drv && drv->pm && drv->pm->complete) |
| 115 | drv->pm->complete(dev); |
| 116 | } |
| 117 | |
| 118 | #ifdef CONFIG_SUSPEND |
| 119 | |
| 120 | static int of_platform_pm_suspend(struct device *dev) |
| 121 | { |
| 122 | struct device_driver *drv = dev->driver; |
| 123 | int ret = 0; |
| 124 | |
| 125 | if (!drv) |
| 126 | return 0; |
| 127 | |
| 128 | if (drv->pm) { |
| 129 | if (drv->pm->suspend) |
| 130 | ret = drv->pm->suspend(dev); |
| 131 | } else { |
| 132 | ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND); |
| 133 | } |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | static int of_platform_pm_suspend_noirq(struct device *dev) |
| 139 | { |
| 140 | struct device_driver *drv = dev->driver; |
| 141 | int ret = 0; |
| 142 | |
| 143 | if (!drv) |
| 144 | return 0; |
| 145 | |
| 146 | if (drv->pm) { |
| 147 | if (drv->pm->suspend_noirq) |
| 148 | ret = drv->pm->suspend_noirq(dev); |
| 149 | } |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | static int of_platform_pm_resume(struct device *dev) |
| 155 | { |
| 156 | struct device_driver *drv = dev->driver; |
| 157 | int ret = 0; |
| 158 | |
| 159 | if (!drv) |
| 160 | return 0; |
| 161 | |
| 162 | if (drv->pm) { |
| 163 | if (drv->pm->resume) |
| 164 | ret = drv->pm->resume(dev); |
| 165 | } else { |
| 166 | ret = of_platform_legacy_resume(dev); |
| 167 | } |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int of_platform_pm_resume_noirq(struct device *dev) |
| 173 | { |
| 174 | struct device_driver *drv = dev->driver; |
| 175 | int ret = 0; |
| 176 | |
| 177 | if (!drv) |
| 178 | return 0; |
| 179 | |
| 180 | if (drv->pm) { |
| 181 | if (drv->pm->resume_noirq) |
| 182 | ret = drv->pm->resume_noirq(dev); |
| 183 | } |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | #else /* !CONFIG_SUSPEND */ |
| 189 | |
| 190 | #define of_platform_pm_suspend NULL |
| 191 | #define of_platform_pm_resume NULL |
| 192 | #define of_platform_pm_suspend_noirq NULL |
| 193 | #define of_platform_pm_resume_noirq NULL |
| 194 | |
| 195 | #endif /* !CONFIG_SUSPEND */ |
| 196 | |
| 197 | #ifdef CONFIG_HIBERNATION |
| 198 | |
| 199 | static int of_platform_pm_freeze(struct device *dev) |
| 200 | { |
| 201 | struct device_driver *drv = dev->driver; |
| 202 | int ret = 0; |
| 203 | |
| 204 | if (!drv) |
| 205 | return 0; |
| 206 | |
| 207 | if (drv->pm) { |
| 208 | if (drv->pm->freeze) |
| 209 | ret = drv->pm->freeze(dev); |
| 210 | } else { |
| 211 | ret = of_platform_legacy_suspend(dev, PMSG_FREEZE); |
| 212 | } |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static int of_platform_pm_freeze_noirq(struct device *dev) |
| 218 | { |
| 219 | struct device_driver *drv = dev->driver; |
| 220 | int ret = 0; |
| 221 | |
| 222 | if (!drv) |
| 223 | return 0; |
| 224 | |
| 225 | if (drv->pm) { |
| 226 | if (drv->pm->freeze_noirq) |
| 227 | ret = drv->pm->freeze_noirq(dev); |
| 228 | } |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static int of_platform_pm_thaw(struct device *dev) |
| 234 | { |
| 235 | struct device_driver *drv = dev->driver; |
| 236 | int ret = 0; |
| 237 | |
| 238 | if (!drv) |
| 239 | return 0; |
| 240 | |
| 241 | if (drv->pm) { |
| 242 | if (drv->pm->thaw) |
| 243 | ret = drv->pm->thaw(dev); |
| 244 | } else { |
| 245 | ret = of_platform_legacy_resume(dev); |
| 246 | } |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int of_platform_pm_thaw_noirq(struct device *dev) |
| 252 | { |
| 253 | struct device_driver *drv = dev->driver; |
| 254 | int ret = 0; |
| 255 | |
| 256 | if (!drv) |
| 257 | return 0; |
| 258 | |
| 259 | if (drv->pm) { |
| 260 | if (drv->pm->thaw_noirq) |
| 261 | ret = drv->pm->thaw_noirq(dev); |
| 262 | } |
| 263 | |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static int of_platform_pm_poweroff(struct device *dev) |
| 268 | { |
| 269 | struct device_driver *drv = dev->driver; |
| 270 | int ret = 0; |
| 271 | |
| 272 | if (!drv) |
| 273 | return 0; |
| 274 | |
| 275 | if (drv->pm) { |
| 276 | if (drv->pm->poweroff) |
| 277 | ret = drv->pm->poweroff(dev); |
| 278 | } else { |
| 279 | ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE); |
| 280 | } |
| 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static int of_platform_pm_poweroff_noirq(struct device *dev) |
| 286 | { |
| 287 | struct device_driver *drv = dev->driver; |
| 288 | int ret = 0; |
| 289 | |
| 290 | if (!drv) |
| 291 | return 0; |
| 292 | |
| 293 | if (drv->pm) { |
| 294 | if (drv->pm->poweroff_noirq) |
| 295 | ret = drv->pm->poweroff_noirq(dev); |
| 296 | } |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | static int of_platform_pm_restore(struct device *dev) |
| 302 | { |
| 303 | struct device_driver *drv = dev->driver; |
| 304 | int ret = 0; |
| 305 | |
| 306 | if (!drv) |
| 307 | return 0; |
| 308 | |
| 309 | if (drv->pm) { |
| 310 | if (drv->pm->restore) |
| 311 | ret = drv->pm->restore(dev); |
| 312 | } else { |
| 313 | ret = of_platform_legacy_resume(dev); |
| 314 | } |
| 315 | |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static int of_platform_pm_restore_noirq(struct device *dev) |
| 320 | { |
| 321 | struct device_driver *drv = dev->driver; |
| 322 | int ret = 0; |
| 323 | |
| 324 | if (!drv) |
| 325 | return 0; |
| 326 | |
| 327 | if (drv->pm) { |
| 328 | if (drv->pm->restore_noirq) |
| 329 | ret = drv->pm->restore_noirq(dev); |
| 330 | } |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | #else /* !CONFIG_HIBERNATION */ |
| 336 | |
| 337 | #define of_platform_pm_freeze NULL |
| 338 | #define of_platform_pm_thaw NULL |
| 339 | #define of_platform_pm_poweroff NULL |
| 340 | #define of_platform_pm_restore NULL |
| 341 | #define of_platform_pm_freeze_noirq NULL |
| 342 | #define of_platform_pm_thaw_noirq NULL |
| 343 | #define of_platform_pm_poweroff_noirq NULL |
| 344 | #define of_platform_pm_restore_noirq NULL |
| 345 | |
| 346 | #endif /* !CONFIG_HIBERNATION */ |
| 347 | |
| 348 | static struct dev_pm_ops of_platform_dev_pm_ops = { |
| 349 | .prepare = of_platform_pm_prepare, |
| 350 | .complete = of_platform_pm_complete, |
| 351 | .suspend = of_platform_pm_suspend, |
| 352 | .resume = of_platform_pm_resume, |
| 353 | .freeze = of_platform_pm_freeze, |
| 354 | .thaw = of_platform_pm_thaw, |
| 355 | .poweroff = of_platform_pm_poweroff, |
| 356 | .restore = of_platform_pm_restore, |
| 357 | .suspend_noirq = of_platform_pm_suspend_noirq, |
| 358 | .resume_noirq = of_platform_pm_resume_noirq, |
| 359 | .freeze_noirq = of_platform_pm_freeze_noirq, |
| 360 | .thaw_noirq = of_platform_pm_thaw_noirq, |
| 361 | .poweroff_noirq = of_platform_pm_poweroff_noirq, |
| 362 | .restore_noirq = of_platform_pm_restore_noirq, |
| 363 | }; |
| 364 | |
| 365 | #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops) |
| 366 | |
| 367 | #else /* !CONFIG_PM_SLEEP */ |
| 368 | |
| 369 | #define OF_PLATFORM_PM_OPS_PTR NULL |
| 370 | |
| 371 | #endif /* !CONFIG_PM_SLEEP */ |
| 372 | |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 373 | int of_bus_type_init(struct bus_type *bus, const char *name) |
| 374 | { |
| 375 | bus->name = name; |
| 376 | bus->match = of_platform_bus_match; |
| 377 | bus->probe = of_platform_device_probe; |
| 378 | bus->remove = of_platform_device_remove; |
Michael Ellerman | a09ad3c | 2008-01-25 16:59:13 +1100 | [diff] [blame] | 379 | bus->shutdown = of_platform_device_shutdown; |
Olaf Hering | 140b932 | 2008-04-24 23:16:00 +1000 | [diff] [blame] | 380 | bus->dev_attrs = of_platform_device_attrs; |
Anton Vorontsov | d35ef90 | 2009-10-12 05:50:41 +0000 | [diff] [blame] | 381 | bus->pm = OF_PLATFORM_PM_OPS_PTR; |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 382 | return bus_register(bus); |
| 383 | } |
Stephen Rothwell | 5c45708 | 2007-10-17 21:17:42 -0700 | [diff] [blame] | 384 | |
| 385 | int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) |
| 386 | { |
Stephen Rothwell | 5c45708 | 2007-10-17 21:17:42 -0700 | [diff] [blame] | 387 | drv->driver.bus = bus; |
| 388 | |
| 389 | /* register with core */ |
| 390 | return driver_register(&drv->driver); |
| 391 | } |
| 392 | EXPORT_SYMBOL(of_register_driver); |
| 393 | |
| 394 | void of_unregister_driver(struct of_platform_driver *drv) |
| 395 | { |
| 396 | driver_unregister(&drv->driver); |
| 397 | } |
| 398 | EXPORT_SYMBOL(of_unregister_driver); |