Android porting series 11, Power Management

Source: Internet
Author: User

Http://source.android.com/porting/power_management.html

Introduction

 

Wake_locks

Wake locks type

Exploring the wake lock example

Powermanager class

PM driver registration driver

Early Suspension

 

Introduction

Android supports its own power supply (standard Linux power management). The premise is that the CPU should not consume power if no application or service requires power. For more information about standard Linux power management, see Linux Power Management Support http: // kernel.org.

 

Android requires applications and services to request CPU resources through the android framework and native Linux libraries using "Wake locks. If no active wake locks exists, the CPU is disabled by Android.

 

The following figure shows the android power management architecture.

 

The solid line indicates that the block elements and dotted lines of Android represent partner-specific components.

Wake locks

Applications and Services use wake locks to request CPU resources.

The locked wakelock depends on its type to prevent systems from entering the paused or other low-power state. This document describes how to use wakelocks.

There are two one wakelock settings:

• Wake_lock_suspend: prevents a complete system from being suspended.

• Wake_lock_idle: low power consumption, which often results in large-scale interruption delay or disabled interruption settings, and will no longer enter the Idle State until the wakelocks is released.

Unless the type is specified, this type of file refers to wake_lock_suspend.
Wakelocks.

If the stop job has started to lock the wakelock, the system will stop the job as long as it has not reached suspend_latestage. This means that wakelock is always working from an interrupt handler lock or freezeable thread, but if you lock wakelock from the suspend_late handler, you must also return an error abort pause from the handler. You can use wakelocks to allow the user space to determine which keys should wake up the entire system to open on the screen. Use set_irq_wake or platform-specific API to ensure that the CPU is awakened by keyboard interruption. Once the keyboard driver has been restored, the event sequence can be as follows ::

1. The keyboard driver receives an interrupt, locks the keyboard to scan wakelock, and starts scanning the matrix keyboard.

2. The Keyboard Scan code detection report shows a key change and its input-event animation.

3. The input event driver considers the key change as an enqueues event and locks the input event queue wakelock.

4. Keyboard Scan code, no key hold or unlock Keyboard Scan wakelock detection.

5. The user space input event selection/investigation thread returns, the input event wakelock is locked during the process, and the input device is called to read the event.

6. This input event drives the output key event queue and unlocks the input event queue wakelock because it is currently empty.

7. The input events of the user space are returned from the read thread. It decides that the key should not wake up the whole system, input the event wakelock during the release process, and require selection or round robin.

The following simple sequence diagram illustrates these steps:

                              Key pressed      Key released
                                         |             |
      keypad-scan                    ++++++++++++++++++++++
      input-event-queue               +++          +++
      process-input-events                      +++          +++
      

Driver API

The driver can use the wakelock API by adding the status of the wakelock variable and calling the following part of wake_lock_init:

  struct state {
  struct wakelock wakelock;
  }
  init() {
  wake_lock_init(&state->wakelock, WAKE_LOCK_SUSPEND, "wakelockname");
  }
  Before freeing the memory, wake_lock_destroy must be called:
  uninit() {
  wake_lock_destroy(&state->wakelock);
  }
  

When the driver determines that it needs to run (usually in an interrupt handler), it calls wake_lock:

  wake_lock(&state->wakelock);
  

When it no longer needs to run, it calls wake_unlock:

  wake_unlock(&state->wakelock);
  

It can also call wake_lock_timeout to release wakelock after delay:

  wake_lock_timeout(&state->wakelock, HZ);
It is useful no matter whether or not wakelock works. If the driver wake up system does not use wakelocks, other parts still need to be run. Avoid this possibility, because if the timeout is long, it will waste power, or if the timeout is short, it may not be able to complete the required work. The lockname or lockname written by the user space API times out to the/sys/power/wake_lock lock. If necessary, create a wakelock. The timeout value is specified in the nanoseconds. Write lockname to/sys/power/wake_unlock to unlock the user wakelock. Do not use the randomly generated wakelock name, because the wakelock name does not have an API to release a user space. Wake locks type

Wake
Lock

Description

Acquire_causes_wakeup

Normally
Wake locks don't actually wake the device, they just cause it to remain on
Once it's already on. Think of the Video Player app as the normal behavior.
Configurications that pop up and want the device to be on are the exception; Use
This flag to be like them.

Full_wake_lock

Wake lock
That ensures that the screen and keyboard are on at full brightness.

On_after_release

When this
Wake lock is released, poke the user activity timer so the screen stays on
For a little longer.

Partial_wake_lock

Wake lock
That ensures that the CPU is running. The screen might not be on.

Screen_bright_wake_lock

Wake lock
That ensures that the screen is on at full brightness; the keyboard Backlight
Will be allowed to go off.

Screen_dim_wake_lock

Wake lock
That ensures that the screen is on, but the keyboard backlight will be
Allowed to go off, and the screen backlight will be allowed to go dim.

Exploring the wake lock example

All power management requirements follow the same basic format:

1. The service that obtains the handle powermanager.

2. Creates a wake-up lock, and specifies screen power management signs, timeout, etc.

3. Obtain the wake lock ..

4. Perform operations (play MP3, open a webpage, and so on ).

5. Release the wake lock.

 

The following code snippet illustrates this process.

PowerManager pm = (PowerManager)mContext.getSystemService(
                                          Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
                                      PowerManager.SCREEN_DIM_WAKE_LOCK
                                      | PowerManager.ON_AFTER_RELEASE,
                                      TAG);
wl.acquire();
 // ...
wl.release();

Powermanager class

 

Android framework provides power management to services and applications through the powermanager class.

 

User space native Library (any in // device/lib/hardware/means, as the supported Android Runtime Library) android power management should never be called directly (see the picture hardware function above ). Bypassing the power management policy during Android operation will damage the system.

 

All call power management should be performed through Android runtime powermanager
APIS.

For example, visit:

Http://code.google.com/android/reference/android/ OS /powermanager.html.

 

PM driver registers the driver

You can register many Android Power Manager drivers for kernel-level drivers so that all kernel-level drivers are notified immediately before or after the power is turned off. For example, you may set a display driver in the user space to be completely powered off, and the request enters the power-off (see the example to implement the display driver of Android's MSM mddi ).

 

To register the android PM driver, use the android PM driver callback handler to implement registration, as shown in the following code segment:

android_register_early_suspend(android_early_suspend_t *handler)
android_register_early_resume(android_early_resume_t *handler)

Immediate return is crucial, rather than waiting for a response.

 

Early Suspension

 

Early on, suspending the API allows the driver to write the user space to/sys/power/request_state when being notified, indicating that the user's visible sleep status should change. The pause handler is called a low-to-high order (4-1), and the recovery handler is called a high-to-low (1-4 ).

 

1.
Early_suspend_level_blank_screen:

Suspend: it should be disabled on the screen, but it must still be accessed by framebuffer.

Resume: it can be opened again on the screen.

 

2. Early_suspend_level_stop_drawing:

Suspend: This level notifies the user space that it should stop accessing frame buffering and wait for it to complete.

Resume: It notifies the user space and should restore screen access. Both methods provide a console switch or a sysfs interface.

 

3.
Early_suspend_level_disable_fb: Disable frame buffering.

Suspend: Disable frame buffering

Resume: re-enable frame buffering.

 

4. Early_suspend_level_stop_input:

Suspend: disabling the input device does not mean that the device can be awakened or disabled.

Resume: re-open the same device.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.