PageRenderTime 59ms CodeModel.GetById 2ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/base/power/main.c

https://bitbucket.org/codefirex/kernel_lge_geehrc
C | 1194 lines | 814 code | 154 blank | 226 comment | 146 complexity | af9b2d28e259b58bf7d6cf56445c51f8 MD5 | raw file
   1/*
   2 * drivers/base/power/main.c - Where the driver meets power management.
   3 *
   4 * Copyright (c) 2003 Patrick Mochel
   5 * Copyright (c) 2003 Open Source Development Lab
   6 *
   7 * This file is released under the GPLv2
   8 *
   9 *
  10 * The driver model core calls device_pm_add() when a device is registered.
  11 * This will initialize the embedded device_pm_info object in the device
  12 * and add it to the list of power-controlled devices. sysfs entries for
  13 * controlling device power management will also be added.
  14 *
  15 * A separate list is used for keeping track of power info, because the power
  16 * domain dependencies may differ from the ancestral dependencies that the
  17 * subsystem list maintains.
  18 */
  19
  20#include <linux/device.h>
  21#include <linux/kallsyms.h>
  22#include <linux/mutex.h>
  23#include <linux/pm.h>
  24#include <linux/pm_runtime.h>
  25#include <linux/resume-trace.h>
  26#include <linux/interrupt.h>
  27#include <linux/sched.h>
  28#include <linux/async.h>
  29#include <linux/suspend.h>
  30#include <linux/timer.h>
  31
  32#include "../base.h"
  33#include "power.h"
  34
  35/*
  36 * The entries in the dpm_list list are in a depth first order, simply
  37 * because children are guaranteed to be discovered after parents, and
  38 * are inserted at the back of the list on discovery.
  39 *
  40 * Since device_pm_add() may be called with a device lock held,
  41 * we must never try to acquire a device lock while holding
  42 * dpm_list_mutex.
  43 */
  44
  45LIST_HEAD(dpm_list);
  46LIST_HEAD(dpm_prepared_list);
  47LIST_HEAD(dpm_suspended_list);
  48LIST_HEAD(dpm_noirq_list);
  49
  50//LGE_CHANGE_S, [inho.oh@lge.com] , 2012-04-10, SuspendEarlySuspend debugfs
  51struct suspend_stats suspend_stats;
  52//LGE_CHANGE_E, [inho.oh@lge.com] , 2012-04-10, SuspendEarlySuspend debugfs
  53static DEFINE_MUTEX(dpm_list_mtx);
  54static pm_message_t pm_transition;
  55
  56static void dpm_drv_timeout(unsigned long data);
  57struct dpm_drv_wd_data {
  58	struct device *dev;
  59	struct task_struct *tsk;
  60};
  61
  62static int async_error;
  63
  64/**
  65 * device_pm_init - Initialize the PM-related part of a device object.
  66 * @dev: Device object being initialized.
  67 */
  68void device_pm_init(struct device *dev)
  69{
  70	dev->power.is_prepared = false;
  71	dev->power.is_suspended = false;
  72	init_completion(&dev->power.completion);
  73	complete_all(&dev->power.completion);
  74	dev->power.wakeup = NULL;
  75	spin_lock_init(&dev->power.lock);
  76	pm_runtime_init(dev);
  77	INIT_LIST_HEAD(&dev->power.entry);
  78}
  79
  80/**
  81 * device_pm_lock - Lock the list of active devices used by the PM core.
  82 */
  83void device_pm_lock(void)
  84{
  85	mutex_lock(&dpm_list_mtx);
  86}
  87
  88/**
  89 * device_pm_unlock - Unlock the list of active devices used by the PM core.
  90 */
  91void device_pm_unlock(void)
  92{
  93	mutex_unlock(&dpm_list_mtx);
  94}
  95
  96/**
  97 * device_pm_add - Add a device to the PM core's list of active devices.
  98 * @dev: Device to add to the list.
  99 */
 100void device_pm_add(struct device *dev)
 101{
 102	pr_debug("PM: Adding info for %s:%s\n",
 103		 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
 104	mutex_lock(&dpm_list_mtx);
 105	if (dev->parent && dev->parent->power.is_prepared)
 106		dev_warn(dev, "parent %s should not be sleeping\n",
 107			dev_name(dev->parent));
 108	list_add_tail(&dev->power.entry, &dpm_list);
 109	mutex_unlock(&dpm_list_mtx);
 110}
 111
 112/**
 113 * device_pm_remove - Remove a device from the PM core's list of active devices.
 114 * @dev: Device to be removed from the list.
 115 */
 116void device_pm_remove(struct device *dev)
 117{
 118	pr_debug("PM: Removing info for %s:%s\n",
 119		 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
 120	complete_all(&dev->power.completion);
 121	mutex_lock(&dpm_list_mtx);
 122	list_del_init(&dev->power.entry);
 123	mutex_unlock(&dpm_list_mtx);
 124	device_wakeup_disable(dev);
 125	pm_runtime_remove(dev);
 126}
 127
 128/**
 129 * device_pm_move_before - Move device in the PM core's list of active devices.
 130 * @deva: Device to move in dpm_list.
 131 * @devb: Device @deva should come before.
 132 */
 133void device_pm_move_before(struct device *deva, struct device *devb)
 134{
 135	pr_debug("PM: Moving %s:%s before %s:%s\n",
 136		 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
 137		 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
 138	/* Delete deva from dpm_list and reinsert before devb. */
 139	list_move_tail(&deva->power.entry, &devb->power.entry);
 140}
 141
 142/**
 143 * device_pm_move_after - Move device in the PM core's list of active devices.
 144 * @deva: Device to move in dpm_list.
 145 * @devb: Device @deva should come after.
 146 */
 147void device_pm_move_after(struct device *deva, struct device *devb)
 148{
 149	pr_debug("PM: Moving %s:%s after %s:%s\n",
 150		 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
 151		 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
 152	/* Delete deva from dpm_list and reinsert after devb. */
 153	list_move(&deva->power.entry, &devb->power.entry);
 154}
 155
 156/**
 157 * device_pm_move_last - Move device to end of the PM core's list of devices.
 158 * @dev: Device to move in dpm_list.
 159 */
 160void device_pm_move_last(struct device *dev)
 161{
 162	pr_debug("PM: Moving %s:%s to end of list\n",
 163		 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
 164	list_move_tail(&dev->power.entry, &dpm_list);
 165}
 166
 167static ktime_t initcall_debug_start(struct device *dev)
 168{
 169	ktime_t calltime = ktime_set(0, 0);
 170
 171	if (initcall_debug) {
 172		pr_info("calling  %s+ @ %i\n",
 173				dev_name(dev), task_pid_nr(current));
 174		calltime = ktime_get();
 175	}
 176
 177	return calltime;
 178}
 179
 180static void initcall_debug_report(struct device *dev, ktime_t calltime,
 181				  int error)
 182{
 183	ktime_t delta, rettime;
 184
 185	if (initcall_debug) {
 186		rettime = ktime_get();
 187		delta = ktime_sub(rettime, calltime);
 188		pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
 189			error, (unsigned long long)ktime_to_ns(delta) >> 10);
 190	}
 191}
 192
 193/**
 194 * dpm_wait - Wait for a PM operation to complete.
 195 * @dev: Device to wait for.
 196 * @async: If unset, wait only if the device's power.async_suspend flag is set.
 197 */
 198static void dpm_wait(struct device *dev, bool async)
 199{
 200	if (!dev)
 201		return;
 202
 203	if (async || (pm_async_enabled && dev->power.async_suspend))
 204		wait_for_completion(&dev->power.completion);
 205}
 206
 207static int dpm_wait_fn(struct device *dev, void *async_ptr)
 208{
 209	dpm_wait(dev, *((bool *)async_ptr));
 210	return 0;
 211}
 212
 213static void dpm_wait_for_children(struct device *dev, bool async)
 214{
 215       device_for_each_child(dev, &async, dpm_wait_fn);
 216}
 217
 218/**
 219 * pm_op - Execute the PM operation appropriate for given PM event.
 220 * @dev: Device to handle.
 221 * @ops: PM operations to choose from.
 222 * @state: PM transition of the system being carried out.
 223 */
 224static int pm_op(struct device *dev,
 225		 const struct dev_pm_ops *ops,
 226		 pm_message_t state)
 227{
 228	int error = 0;
 229	ktime_t calltime;
 230
 231	calltime = initcall_debug_start(dev);
 232
 233	switch (state.event) {
 234#ifdef CONFIG_SUSPEND
 235	case PM_EVENT_SUSPEND:
 236		if (ops->suspend) {
 237			error = ops->suspend(dev);
 238			suspend_report_result(ops->suspend, error);
 239		}
 240		break;
 241	case PM_EVENT_RESUME:
 242		if (ops->resume) {
 243			error = ops->resume(dev);
 244			suspend_report_result(ops->resume, error);
 245		}
 246		break;
 247#endif /* CONFIG_SUSPEND */
 248#ifdef CONFIG_HIBERNATE_CALLBACKS
 249	case PM_EVENT_FREEZE:
 250	case PM_EVENT_QUIESCE:
 251		if (ops->freeze) {
 252			error = ops->freeze(dev);
 253			suspend_report_result(ops->freeze, error);
 254		}
 255		break;
 256	case PM_EVENT_HIBERNATE:
 257		if (ops->poweroff) {
 258			error = ops->poweroff(dev);
 259			suspend_report_result(ops->poweroff, error);
 260		}
 261		break;
 262	case PM_EVENT_THAW:
 263	case PM_EVENT_RECOVER:
 264		if (ops->thaw) {
 265			error = ops->thaw(dev);
 266			suspend_report_result(ops->thaw, error);
 267		}
 268		break;
 269	case PM_EVENT_RESTORE:
 270		if (ops->restore) {
 271			error = ops->restore(dev);
 272			suspend_report_result(ops->restore, error);
 273		}
 274		break;
 275#endif /* CONFIG_HIBERNATE_CALLBACKS */
 276	default:
 277		error = -EINVAL;
 278	}
 279
 280	initcall_debug_report(dev, calltime, error);
 281
 282	return error;
 283}
 284
 285/**
 286 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
 287 * @dev: Device to handle.
 288 * @ops: PM operations to choose from.
 289 * @state: PM transition of the system being carried out.
 290 *
 291 * The driver of @dev will not receive interrupts while this function is being
 292 * executed.
 293 */
 294static int pm_noirq_op(struct device *dev,
 295			const struct dev_pm_ops *ops,
 296			pm_message_t state)
 297{
 298	int error = 0;
 299	ktime_t calltime = ktime_set(0, 0), delta, rettime;
 300
 301	if (initcall_debug) {
 302		pr_info("calling  %s+ @ %i, parent: %s\n",
 303				dev_name(dev), task_pid_nr(current),
 304				dev->parent ? dev_name(dev->parent) : "none");
 305		calltime = ktime_get();
 306	}
 307
 308	switch (state.event) {
 309#ifdef CONFIG_SUSPEND
 310	case PM_EVENT_SUSPEND:
 311		if (ops->suspend_noirq) {
 312			error = ops->suspend_noirq(dev);
 313			suspend_report_result(ops->suspend_noirq, error);
 314		}
 315		break;
 316	case PM_EVENT_RESUME:
 317		if (ops->resume_noirq) {
 318			error = ops->resume_noirq(dev);
 319			suspend_report_result(ops->resume_noirq, error);
 320		}
 321		break;
 322#endif /* CONFIG_SUSPEND */
 323#ifdef CONFIG_HIBERNATE_CALLBACKS
 324	case PM_EVENT_FREEZE:
 325	case PM_EVENT_QUIESCE:
 326		if (ops->freeze_noirq) {
 327			error = ops->freeze_noirq(dev);
 328			suspend_report_result(ops->freeze_noirq, error);
 329		}
 330		break;
 331	case PM_EVENT_HIBERNATE:
 332		if (ops->poweroff_noirq) {
 333			error = ops->poweroff_noirq(dev);
 334			suspend_report_result(ops->poweroff_noirq, error);
 335		}
 336		break;
 337	case PM_EVENT_THAW:
 338	case PM_EVENT_RECOVER:
 339		if (ops->thaw_noirq) {
 340			error = ops->thaw_noirq(dev);
 341			suspend_report_result(ops->thaw_noirq, error);
 342		}
 343		break;
 344	case PM_EVENT_RESTORE:
 345		if (ops->restore_noirq) {
 346			error = ops->restore_noirq(dev);
 347			suspend_report_result(ops->restore_noirq, error);
 348		}
 349		break;
 350#endif /* CONFIG_HIBERNATE_CALLBACKS */
 351	default:
 352		error = -EINVAL;
 353	}
 354
 355	if (initcall_debug) {
 356		rettime = ktime_get();
 357		delta = ktime_sub(rettime, calltime);
 358		printk("initcall %s_i+ returned %d after %Ld usecs\n",
 359			dev_name(dev), error,
 360			(unsigned long long)ktime_to_ns(delta) >> 10);
 361	}
 362
 363	return error;
 364}
 365
 366static char *pm_verb(int event)
 367{
 368	switch (event) {
 369	case PM_EVENT_SUSPEND:
 370		return "suspend";
 371	case PM_EVENT_RESUME:
 372		return "resume";
 373	case PM_EVENT_FREEZE:
 374		return "freeze";
 375	case PM_EVENT_QUIESCE:
 376		return "quiesce";
 377	case PM_EVENT_HIBERNATE:
 378		return "hibernate";
 379	case PM_EVENT_THAW:
 380		return "thaw";
 381	case PM_EVENT_RESTORE:
 382		return "restore";
 383	case PM_EVENT_RECOVER:
 384		return "recover";
 385	default:
 386		return "(unknown PM event)";
 387	}
 388}
 389
 390static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
 391{
 392	dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
 393		((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
 394		", may wakeup" : "");
 395}
 396
 397static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
 398			int error)
 399{
 400	printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
 401		dev_name(dev), pm_verb(state.event), info, error);
 402}
 403
 404static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
 405{
 406	ktime_t calltime;
 407	u64 usecs64;
 408	int usecs;
 409
 410	calltime = ktime_get();
 411	usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
 412	do_div(usecs64, NSEC_PER_USEC);
 413	usecs = usecs64;
 414	if (usecs == 0)
 415		usecs = 1;
 416	pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
 417		info ?: "", info ? " " : "", pm_verb(state.event),
 418		usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
 419}
 420
 421/*------------------------- Resume routines -------------------------*/
 422
 423/**
 424 * device_resume_noirq - Execute an "early resume" callback for given device.
 425 * @dev: Device to handle.
 426 * @state: PM transition of the system being carried out.
 427 *
 428 * The driver of @dev will not receive interrupts while this function is being
 429 * executed.
 430 */
 431static int device_resume_noirq(struct device *dev, pm_message_t state)
 432{
 433	int error = 0;
 434
 435	TRACE_DEVICE(dev);
 436	TRACE_RESUME(0);
 437
 438	if (dev->pwr_domain) {
 439		pm_dev_dbg(dev, state, "EARLY power domain ");
 440		error = pm_noirq_op(dev, &dev->pwr_domain->ops, state);
 441	} else if (dev->type && dev->type->pm) {
 442		pm_dev_dbg(dev, state, "EARLY type ");
 443		error = pm_noirq_op(dev, dev->type->pm, state);
 444	} else if (dev->class && dev->class->pm) {
 445		pm_dev_dbg(dev, state, "EARLY class ");
 446		error = pm_noirq_op(dev, dev->class->pm, state);
 447	} else if (dev->bus && dev->bus->pm) {
 448		pm_dev_dbg(dev, state, "EARLY ");
 449		error = pm_noirq_op(dev, dev->bus->pm, state);
 450	}
 451
 452	TRACE_RESUME(error);
 453	return error;
 454}
 455
 456/**
 457 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
 458 * @state: PM transition of the system being carried out.
 459 *
 460 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
 461 * enable device drivers to receive interrupts.
 462 */
 463void dpm_resume_noirq(pm_message_t state)
 464{
 465	ktime_t starttime = ktime_get();
 466
 467	mutex_lock(&dpm_list_mtx);
 468	while (!list_empty(&dpm_noirq_list)) {
 469		struct device *dev = to_device(dpm_noirq_list.next);
 470		int error;
 471
 472		get_device(dev);
 473		list_move_tail(&dev->power.entry, &dpm_suspended_list);
 474		mutex_unlock(&dpm_list_mtx);
 475
 476		error = device_resume_noirq(dev, state);
 477//LGE_CHANGE_S, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
 478		if (error) {
 479			suspend_stats.failed_resume_noirq++;
 480			dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
 481			dpm_save_failed_dev(dev_name(dev));
 482			pm_dev_err(dev, state, " early", error);
 483		}
 484//LGE_CHANGE_E, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
 485
 486		mutex_lock(&dpm_list_mtx);
 487		put_device(dev);
 488	}
 489	mutex_unlock(&dpm_list_mtx);
 490	dpm_show_time(starttime, state, "early");
 491	resume_device_irqs();
 492}
 493EXPORT_SYMBOL_GPL(dpm_resume_noirq);
 494
 495/**
 496 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
 497 * @dev: Device to resume.
 498 * @cb: Resume callback to execute.
 499 */
 500static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
 501{
 502	int error;
 503	ktime_t calltime;
 504
 505	calltime = initcall_debug_start(dev);
 506
 507	error = cb(dev);
 508	suspend_report_result(cb, error);
 509
 510	initcall_debug_report(dev, calltime, error);
 511
 512	return error;
 513}
 514
 515/**
 516 * device_resume - Execute "resume" callbacks for given device.
 517 * @dev: Device to handle.
 518 * @state: PM transition of the system being carried out.
 519 * @async: If true, the device is being resumed asynchronously.
 520 */
 521static int device_resume(struct device *dev, pm_message_t state, bool async)
 522{
 523	int error = 0;
 524	bool put = false;
 525
 526	TRACE_DEVICE(dev);
 527	TRACE_RESUME(0);
 528
 529	dpm_wait(dev->parent, async);
 530	device_lock(dev);
 531
 532	/*
 533	 * This is a fib.  But we'll allow new children to be added below
 534	 * a resumed device, even if the device hasn't been completed yet.
 535	 */
 536	dev->power.is_prepared = false;
 537
 538	if (!dev->power.is_suspended)
 539		goto Unlock;
 540
 541	pm_runtime_enable(dev);
 542	put = true;
 543
 544	if (dev->pwr_domain) {
 545		pm_dev_dbg(dev, state, "power domain ");
 546		error = pm_op(dev, &dev->pwr_domain->ops, state);
 547		goto End;
 548	}
 549
 550	if (dev->type && dev->type->pm) {
 551		pm_dev_dbg(dev, state, "type ");
 552		error = pm_op(dev, dev->type->pm, state);
 553		goto End;
 554	}
 555
 556	if (dev->class) {
 557		if (dev->class->pm) {
 558			pm_dev_dbg(dev, state, "class ");
 559			error = pm_op(dev, dev->class->pm, state);
 560			goto End;
 561		} else if (dev->class->resume) {
 562			pm_dev_dbg(dev, state, "legacy class ");
 563			error = legacy_resume(dev, dev->class->resume);
 564			goto End;
 565		}
 566	}
 567
 568	if (dev->bus) {
 569		if (dev->bus->pm) {
 570			pm_dev_dbg(dev, state, "");
 571			error = pm_op(dev, dev->bus->pm, state);
 572		} else if (dev->bus->resume) {
 573			pm_dev_dbg(dev, state, "legacy ");
 574			error = legacy_resume(dev, dev->bus->resume);
 575		}
 576	}
 577
 578 End:
 579	dev->power.is_suspended = false;
 580
 581 Unlock:
 582	device_unlock(dev);
 583	complete_all(&dev->power.completion);
 584
 585	TRACE_RESUME(error);
 586
 587	if (put)
 588		pm_runtime_put_sync(dev);
 589
 590	return error;
 591}
 592
 593static void async_resume(void *data, async_cookie_t cookie)
 594{
 595	struct device *dev = (struct device *)data;
 596	int error;
 597
 598	error = device_resume(dev, pm_transition, true);
 599	if (error)
 600		pm_dev_err(dev, pm_transition, " async", error);
 601	put_device(dev);
 602}
 603
 604static bool is_async(struct device *dev)
 605{
 606	return dev->power.async_suspend && pm_async_enabled
 607		&& !pm_trace_is_enabled();
 608}
 609
 610/**
 611 *	dpm_drv_timeout - Driver suspend / resume watchdog handler
 612 *	@data: struct device which timed out
 613 *
 614 * 	Called when a driver has timed out suspending or resuming.
 615 * 	There's not much we can do here to recover so
 616 * 	BUG() out for a crash-dump
 617 *
 618 */
 619static void dpm_drv_timeout(unsigned long data)
 620{
 621	struct dpm_drv_wd_data *wd_data = (void *)data;
 622	struct device *dev = wd_data->dev;
 623	struct task_struct *tsk = wd_data->tsk;
 624
 625	printk(KERN_EMERG "**** DPM device timeout: %s (%s)\n", dev_name(dev),
 626	       (dev->driver ? dev->driver->name : "no driver"));
 627
 628	printk(KERN_EMERG "dpm suspend stack:\n");
 629	show_stack(tsk, NULL);
 630
 631	BUG();
 632}
 633
 634/**
 635 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
 636 * @state: PM transition of the system being carried out.
 637 *
 638 * Execute the appropriate "resume" callback for all devices whose status
 639 * indicates that they are suspended.
 640 */
 641void dpm_resume(pm_message_t state)
 642{
 643	struct device *dev;
 644	ktime_t starttime = ktime_get();
 645
 646	might_sleep();
 647
 648	mutex_lock(&dpm_list_mtx);
 649	pm_transition = state;
 650	async_error = 0;
 651
 652	list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
 653		INIT_COMPLETION(dev->power.completion);
 654		if (is_async(dev)) {
 655			get_device(dev);
 656			async_schedule(async_resume, dev);
 657		}
 658	}
 659
 660	while (!list_empty(&dpm_suspended_list)) {
 661		dev = to_device(dpm_suspended_list.next);
 662		get_device(dev);
 663		if (!is_async(dev)) {
 664			int error;
 665
 666			mutex_unlock(&dpm_list_mtx);
 667
 668			error = device_resume(dev, state, false);
 669//LGE_CHANGE_S, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
 670			if (error) {
 671				suspend_stats.failed_resume++;
 672				dpm_save_failed_step(SUSPEND_RESUME);
 673				dpm_save_failed_dev(dev_name(dev));
 674				pm_dev_err(dev, state, "", error);
 675			}
 676//LGE_CHANGE_E, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
 677
 678			mutex_lock(&dpm_list_mtx);
 679		}
 680		if (!list_empty(&dev->power.entry))
 681			list_move_tail(&dev->power.entry, &dpm_prepared_list);
 682		put_device(dev);
 683	}
 684	mutex_unlock(&dpm_list_mtx);
 685	async_synchronize_full();
 686	dpm_show_time(starttime, state, NULL);
 687}
 688
 689/**
 690 * device_complete - Complete a PM transition for given device.
 691 * @dev: Device to handle.
 692 * @state: PM transition of the system being carried out.
 693 */
 694static void device_complete(struct device *dev, pm_message_t state)
 695{
 696	device_lock(dev);
 697
 698	if (dev->pwr_domain) {
 699		pm_dev_dbg(dev, state, "completing power domain ");
 700		if (dev->pwr_domain->ops.complete)
 701			dev->pwr_domain->ops.complete(dev);
 702	} else if (dev->type && dev->type->pm) {
 703		pm_dev_dbg(dev, state, "completing type ");
 704		if (dev->type->pm->complete)
 705			dev->type->pm->complete(dev);
 706	} else if (dev->class && dev->class->pm) {
 707		pm_dev_dbg(dev, state, "completing class ");
 708		if (dev->class->pm->complete)
 709			dev->class->pm->complete(dev);
 710	} else if (dev->bus && dev->bus->pm) {
 711		pm_dev_dbg(dev, state, "completing ");
 712		if (dev->bus->pm->complete)
 713			dev->bus->pm->complete(dev);
 714	}
 715
 716	device_unlock(dev);
 717}
 718
 719/**
 720 * dpm_complete - Complete a PM transition for all non-sysdev devices.
 721 * @state: PM transition of the system being carried out.
 722 *
 723 * Execute the ->complete() callbacks for all devices whose PM status is not
 724 * DPM_ON (this allows new devices to be registered).
 725 */
 726void dpm_complete(pm_message_t state)
 727{
 728	struct list_head list;
 729
 730	might_sleep();
 731
 732	INIT_LIST_HEAD(&list);
 733	mutex_lock(&dpm_list_mtx);
 734	while (!list_empty(&dpm_prepared_list)) {
 735		struct device *dev = to_device(dpm_prepared_list.prev);
 736
 737		get_device(dev);
 738		dev->power.is_prepared = false;
 739		list_move(&dev->power.entry, &list);
 740		mutex_unlock(&dpm_list_mtx);
 741
 742		device_complete(dev, state);
 743
 744		mutex_lock(&dpm_list_mtx);
 745		put_device(dev);
 746	}
 747	list_splice(&list, &dpm_list);
 748	mutex_unlock(&dpm_list_mtx);
 749}
 750
 751/**
 752 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
 753 * @state: PM transition of the system being carried out.
 754 *
 755 * Execute "resume" callbacks for all devices and complete the PM transition of
 756 * the system.
 757 */
 758void dpm_resume_end(pm_message_t state)
 759{
 760	dpm_resume(state);
 761	dpm_complete(state);
 762}
 763EXPORT_SYMBOL_GPL(dpm_resume_end);
 764
 765
 766/*------------------------- Suspend routines -------------------------*/
 767
 768/**
 769 * resume_event - Return a "resume" message for given "suspend" sleep state.
 770 * @sleep_state: PM message representing a sleep state.
 771 *
 772 * Return a PM message representing the resume event corresponding to given
 773 * sleep state.
 774 */
 775static pm_message_t resume_event(pm_message_t sleep_state)
 776{
 777	switch (sleep_state.event) {
 778	case PM_EVENT_SUSPEND:
 779		return PMSG_RESUME;
 780	case PM_EVENT_FREEZE:
 781	case PM_EVENT_QUIESCE:
 782		return PMSG_RECOVER;
 783	case PM_EVENT_HIBERNATE:
 784		return PMSG_RESTORE;
 785	}
 786	return PMSG_ON;
 787}
 788
 789/**
 790 * device_suspend_noirq - Execute a "late suspend" callback for given device.
 791 * @dev: Device to handle.
 792 * @state: PM transition of the system being carried out.
 793 *
 794 * The driver of @dev will not receive interrupts while this function is being
 795 * executed.
 796 */
 797static int device_suspend_noirq(struct device *dev, pm_message_t state)
 798{
 799	int error;
 800
 801	if (dev->pwr_domain) {
 802		pm_dev_dbg(dev, state, "LATE power domain ");
 803		error = pm_noirq_op(dev, &dev->pwr_domain->ops, state);
 804		if (error)
 805			return error;
 806	} else if (dev->type && dev->type->pm) {
 807		pm_dev_dbg(dev, state, "LATE type ");
 808		error = pm_noirq_op(dev, dev->type->pm, state);
 809		if (error)
 810			return error;
 811	} else if (dev->class && dev->class->pm) {
 812		pm_dev_dbg(dev, state, "LATE class ");
 813		error = pm_noirq_op(dev, dev->class->pm, state);
 814		if (error)
 815			return error;
 816	} else if (dev->bus && dev->bus->pm) {
 817		pm_dev_dbg(dev, state, "LATE ");
 818		error = pm_noirq_op(dev, dev->bus->pm, state);
 819		if (error)
 820			return error;
 821	}
 822
 823	return 0;
 824}
 825
 826/**
 827 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
 828 * @state: PM transition of the system being carried out.
 829 *
 830 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
 831 * handlers for all non-sysdev devices.
 832 */
 833int dpm_suspend_noirq(pm_message_t state)
 834{
 835	ktime_t starttime = ktime_get();
 836	int error = 0;
 837
 838	suspend_device_irqs();
 839	mutex_lock(&dpm_list_mtx);
 840	while (!list_empty(&dpm_suspended_list)) {
 841		struct device *dev = to_device(dpm_suspended_list.prev);
 842
 843		get_device(dev);
 844		mutex_unlock(&dpm_list_mtx);
 845
 846		error = device_suspend_noirq(dev, state);
 847
 848		mutex_lock(&dpm_list_mtx);
 849		if (error) {
 850			pm_dev_err(dev, state, " late", error);
 851//LGE_CHANGE_S, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
 852			suspend_stats.failed_suspend_noirq++;
 853			dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
 854			dpm_save_failed_dev(dev_name(dev));
 855//LGE_CHANGE_E, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
 856			put_device(dev);
 857			break;
 858		}
 859		if (!list_empty(&dev->power.entry))
 860			list_move(&dev->power.entry, &dpm_noirq_list);
 861		put_device(dev);
 862	}
 863	mutex_unlock(&dpm_list_mtx);
 864	if (error)
 865		dpm_resume_noirq(resume_event(state));
 866	else
 867		dpm_show_time(starttime, state, "late");
 868	return error;
 869}
 870EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
 871
 872/**
 873 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
 874 * @dev: Device to suspend.
 875 * @state: PM transition of the system being carried out.
 876 * @cb: Suspend callback to execute.
 877 */
 878static int legacy_suspend(struct device *dev, pm_message_t state,
 879			  int (*cb)(struct device *dev, pm_message_t state))
 880{
 881	int error;
 882	ktime_t calltime;
 883
 884	calltime = initcall_debug_start(dev);
 885
 886	error = cb(dev, state);
 887	suspend_report_result(cb, error);
 888
 889	initcall_debug_report(dev, calltime, error);
 890
 891	return error;
 892}
 893
 894/**
 895 * device_suspend - Execute "suspend" callbacks for given device.
 896 * @dev: Device to handle.
 897 * @state: PM transition of the system being carried out.
 898 * @async: If true, the device is being suspended asynchronously.
 899 */
 900static int __device_suspend(struct device *dev, pm_message_t state, bool async)
 901{
 902	int error = 0;
 903	struct timer_list timer;
 904	struct dpm_drv_wd_data data;
 905
 906	dpm_wait_for_children(dev, async);
 907
 908	if (async_error)
 909		return 0;
 910
 911	pm_runtime_get_noresume(dev);
 912	if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
 913		pm_wakeup_event(dev, 0);
 914
 915	if (pm_wakeup_pending()) {
 916		pm_runtime_put_sync(dev);
 917		async_error = -EBUSY;
 918		return 0;
 919	}
 920
 921	data.dev = dev;
 922	data.tsk = get_current();
 923	init_timer_on_stack(&timer);
 924	timer.expires = jiffies + HZ * 12;
 925	timer.function = dpm_drv_timeout;
 926	timer.data = (unsigned long)&data;
 927	add_timer(&timer);
 928
 929	device_lock(dev);
 930
 931	if (dev->pwr_domain) {
 932		pm_dev_dbg(dev, state, "power domain ");
 933		error = pm_op(dev, &dev->pwr_domain->ops, state);
 934		goto End;
 935	}
 936
 937	if (dev->type && dev->type->pm) {
 938		pm_dev_dbg(dev, state, "type ");
 939		error = pm_op(dev, dev->type->pm, state);
 940		goto End;
 941	}
 942
 943	if (dev->class) {
 944		if (dev->class->pm) {
 945			pm_dev_dbg(dev, state, "class ");
 946			error = pm_op(dev, dev->class->pm, state);
 947			goto End;
 948		} else if (dev->class->suspend) {
 949			pm_dev_dbg(dev, state, "legacy class ");
 950			error = legacy_suspend(dev, state, dev->class->suspend);
 951			goto End;
 952		}
 953	}
 954
 955	if (dev->bus) {
 956		if (dev->bus->pm) {
 957			pm_dev_dbg(dev, state, "");
 958			error = pm_op(dev, dev->bus->pm, state);
 959		} else if (dev->bus->suspend) {
 960			pm_dev_dbg(dev, state, "legacy ");
 961			error = legacy_suspend(dev, state, dev->bus->suspend);
 962		}
 963	}
 964
 965 End:
 966	dev->power.is_suspended = !error;
 967
 968	device_unlock(dev);
 969
 970	del_timer_sync(&timer);
 971	destroy_timer_on_stack(&timer);
 972
 973	complete_all(&dev->power.completion);
 974
 975	if (error) {
 976		pm_runtime_put_sync(dev);
 977		async_error = error;
 978	} else if (dev->power.is_suspended) {
 979		__pm_runtime_disable(dev, false);
 980	}
 981
 982	return error;
 983}
 984
 985static void async_suspend(void *data, async_cookie_t cookie)
 986{
 987	struct device *dev = (struct device *)data;
 988	int error;
 989
 990	error = __device_suspend(dev, pm_transition, true);
 991//LGE_CHANGE_S, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
 992	if (error) {
 993		dpm_save_failed_dev(dev_name(dev));
 994		pm_dev_err(dev, pm_transition, " async", error);
 995	}
 996//LGE_CHANGE_E, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
 997
 998	put_device(dev);
 999}
1000
1001static int device_suspend(struct device *dev)
1002{
1003	INIT_COMPLETION(dev->power.completion);
1004
1005	if (pm_async_enabled && dev->power.async_suspend) {
1006		get_device(dev);
1007		async_schedule(async_suspend, dev);
1008		return 0;
1009	}
1010
1011	return __device_suspend(dev, pm_transition, false);
1012}
1013
1014/**
1015 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1016 * @state: PM transition of the system being carried out.
1017 */
1018int dpm_suspend(pm_message_t state)
1019{
1020	ktime_t starttime = ktime_get();
1021	int error = 0;
1022
1023	might_sleep();
1024
1025	mutex_lock(&dpm_list_mtx);
1026	pm_transition = state;
1027	async_error = 0;
1028	while (!list_empty(&dpm_prepared_list)) {
1029		struct device *dev = to_device(dpm_prepared_list.prev);
1030
1031		get_device(dev);
1032		mutex_unlock(&dpm_list_mtx);
1033
1034		error = device_suspend(dev);
1035
1036		mutex_lock(&dpm_list_mtx);
1037		if (error) {
1038			pm_dev_err(dev, state, "", error);
1039			dpm_save_failed_dev(dev_name(dev));	//LGE_CHANGE_S, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
1040			put_device(dev);
1041			break;
1042		}
1043		if (!list_empty(&dev->power.entry))
1044			list_move(&dev->power.entry, &dpm_suspended_list);
1045		put_device(dev);
1046		if (async_error)
1047			break;
1048	}
1049	mutex_unlock(&dpm_list_mtx);
1050	async_synchronize_full();
1051	if (!error)
1052		error = async_error;
1053//LGE_CHANGE_S, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
1054	if (error) {
1055		suspend_stats.failed_suspend++;
1056		dpm_save_failed_step(SUSPEND_SUSPEND);
1057	} else
1058//LGE_CHANGE_E, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
1059		dpm_show_time(starttime, state, NULL);
1060	return error;
1061}
1062
1063/**
1064 * device_prepare - Prepare a device for system power transition.
1065 * @dev: Device to handle.
1066 * @state: PM transition of the system being carried out.
1067 *
1068 * Execute the ->prepare() callback(s) for given device.  No new children of the
1069 * device may be registered after this function has returned.
1070 */
1071static int device_prepare(struct device *dev, pm_message_t state)
1072{
1073	int error = 0;
1074
1075	device_lock(dev);
1076
1077	if (dev->pwr_domain) {
1078		pm_dev_dbg(dev, state, "preparing power domain ");
1079		if (dev->pwr_domain->ops.prepare)
1080			error = dev->pwr_domain->ops.prepare(dev);
1081		suspend_report_result(dev->pwr_domain->ops.prepare, error);
1082		if (error)
1083			goto End;
1084	} else if (dev->type && dev->type->pm) {
1085		pm_dev_dbg(dev, state, "preparing type ");
1086		if (dev->type->pm->prepare)
1087			error = dev->type->pm->prepare(dev);
1088		suspend_report_result(dev->type->pm->prepare, error);
1089		if (error)
1090			goto End;
1091	} else if (dev->class && dev->class->pm) {
1092		pm_dev_dbg(dev, state, "preparing class ");
1093		if (dev->class->pm->prepare)
1094			error = dev->class->pm->prepare(dev);
1095		suspend_report_result(dev->class->pm->prepare, error);
1096		if (error)
1097			goto End;
1098	} else if (dev->bus && dev->bus->pm) {
1099		pm_dev_dbg(dev, state, "preparing ");
1100		if (dev->bus->pm->prepare)
1101			error = dev->bus->pm->prepare(dev);
1102		suspend_report_result(dev->bus->pm->prepare, error);
1103	}
1104
1105 End:
1106	device_unlock(dev);
1107
1108	return error;
1109}
1110
1111/**
1112 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1113 * @state: PM transition of the system being carried out.
1114 *
1115 * Execute the ->prepare() callback(s) for all devices.
1116 */
1117int dpm_prepare(pm_message_t state)
1118{
1119	int error = 0;
1120
1121	might_sleep();
1122
1123	mutex_lock(&dpm_list_mtx);
1124	while (!list_empty(&dpm_list)) {
1125		struct device *dev = to_device(dpm_list.next);
1126
1127		get_device(dev);
1128		mutex_unlock(&dpm_list_mtx);
1129
1130		error = device_prepare(dev, state);
1131
1132		mutex_lock(&dpm_list_mtx);
1133		if (error) {
1134			if (error == -EAGAIN) {
1135				put_device(dev);
1136				error = 0;
1137				continue;
1138			}
1139			printk(KERN_INFO "PM: Device %s not prepared "
1140				"for power transition: code %d\n",
1141				dev_name(dev), error);
1142			put_device(dev);
1143			break;
1144		}
1145		dev->power.is_prepared = true;
1146		if (!list_empty(&dev->power.entry))
1147			list_move_tail(&dev->power.entry, &dpm_prepared_list);
1148		put_device(dev);
1149	}
1150	mutex_unlock(&dpm_list_mtx);
1151	return error;
1152}
1153
1154/**
1155 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1156 * @state: PM transition of the system being carried out.
1157 *
1158 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1159 * callbacks for them.
1160 */
1161int dpm_suspend_start(pm_message_t state)
1162{
1163	int error;
1164
1165	error = dpm_prepare(state);
1166//LGE_CHANGE_S, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
1167	if (error) {
1168		suspend_stats.failed_prepare++;
1169		dpm_save_failed_step(SUSPEND_PREPARE);
1170	} else
1171//LGE_CHANGE_E, [inho.oh@lge.com] , 2012-04-10, SuspenEarlySuspend debugfs
1172		error = dpm_suspend(state);
1173	return error;
1174}
1175EXPORT_SYMBOL_GPL(dpm_suspend_start);
1176
1177void __suspend_report_result(const char *function, void *fn, int ret)
1178{
1179	if (ret)
1180		printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1181}
1182EXPORT_SYMBOL_GPL(__suspend_report_result);
1183
1184/**
1185 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1186 * @dev: Device to wait for.
1187 * @subordinate: Device that needs to wait for @dev.
1188 */
1189int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1190{
1191	dpm_wait(dev, subordinate->power.async_suspend);
1192	return async_error;
1193}
1194EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);