4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 * @brief Power manager main loop.
24 * This file includes Main loop, the FSM, signal processing.
35 #include <sys/types.h>
40 #include <vconf-keys.h>
42 #include <libsyscommon/list.h>
43 #include <hal/device/hal-device-power.h>
45 #include "ambient-mode.h"
49 #include "lock-detector.h"
50 #include "display-ops.h"
51 #include "shared/devices.h"
52 #include "shared/device-notifier.h"
53 #include "core/udev.h"
54 #include "shared/common.h"
55 #include "shared/apps.h"
56 #include "extcon/extcon.h"
57 #include "battery/power-supply.h"
58 #include "power/power.h"
59 #include "power/power-off.h"
60 #include "power/power-suspend.h"
61 #include "power/power-boot.h"
62 #include "power/power-doze.h"
63 #include "device-interface.h"
64 #include "display-plugin.h"
65 #include "display-signal.h"
66 #include "display-lock.h"
67 #include "display-backlight.h"
68 #include "display-misc.h"
69 #include "display-panel.h"
70 #include "display-config.h"
71 #include "display-state-transition.h"
72 #include "shared/plugin.h"
73 #include "led/touch-key.h"
76 * @addtogroup POWER_MANAGER
80 #define LOCK_SCREEN_CONTROL_TIMEOUT 5000
82 #define GESTURE_STR "gesture"
83 #define POWER_KEY_STR "powerkey"
84 #define TOUCH_STR "touch"
85 #define EVENT_STR "event"
86 #define TIMEOUT_STR "timeout"
87 #define PROXI_STR "proximity"
88 #define PALM_STR "palm"
89 #define UNKNOWN_STR "unknown"
91 extern void init_save_userlock(void);
93 static struct display_plugin *disp_plgn;
94 static struct display_backlight_ops *backlight_ops;
95 static struct battery_plugin *battery_plgn;
96 static int (*fp_get_charging_status) (int *val);
98 static void (*power_saving_func) (int onoff);
99 static enum device_ops_status status = DEVICE_OPS_STATUS_UNINIT;
101 static int system_wakeup_flag = false;
102 static char *custom_change_name;
103 static guint lock_timeout_id;
104 static struct timeval lcdon_tv;
106 * The two variables(lcdon_broadcast, pmstate_suspend) must be set initial
107 * state because it should be sent from previous state at booting time.
109 static bool lcdon_broadcast = true;
111 static bool touch_blocked = false;
113 /* default transition, action fuctions */
114 static int default_action(int timeout);
116 static int default_proc_change_state(unsigned int cond, pid_t pid);
117 static int (*proc_change_state)(unsigned int cond, pid_t pid) = default_proc_change_state;
119 static struct state states[S_END] = {
120 { S_START, "S_START", NULL, NULL, NULL, NULL },
121 { S_NORMAL, "S_NORMAL", NULL, default_action, NULL, NULL },
122 { S_LCDDIM, "S_LCDDIM", NULL, default_action, NULL, NULL },
123 { S_LCDOFF, "S_LCDOFF", NULL, default_action, NULL, NULL },
124 { S_STANDBY, "S_STANDBY", NULL, NULL, NULL, NULL },
125 { S_SLEEP, "S_SLEEP", NULL, default_action, NULL, NULL },
126 { S_POWEROFF, "S_POWEROFF", NULL, NULL, NULL, NULL },
129 #define TIMEOUT_NONE (-1)
131 #define DELAYED_INIT_WATING_TIME 60000 /* 1 minute */
133 #define LOCK_SCREEN_WATING_TIME 300 /* 0.3 second */
134 #define LONG_PRESS_INTERVAL 2 /* 2 seconds */
135 #define SAMPLING_INTERVAL 1 /* 1 sec */
136 #define BRIGHTNESS_CHANGE_STEP 10
137 #define LCD_ALWAYS_ON 0
138 #define ACCEL_SENSOR_ON 1
139 #define CONTINUOUS_SAMPLING 1
140 #define LCDOFF_TIMEOUT 300 /* milli second */
142 #define DIFF_TIMEVAL_MS(a, b) \
143 (((a.tv_sec * 1000000 + a.tv_usec) - \
144 (b.tv_sec * 1000000 + b.tv_usec)) \
147 static struct display_config display_conf = {
148 .lock_wait_time = LOCK_SCREEN_WATING_TIME,
149 .longpress_interval = LONG_PRESS_INTERVAL,
150 .lightsensor_interval = SAMPLING_INTERVAL,
151 .lcdoff_timeout = LCDOFF_TIMEOUT,
152 .pm_default_brightness = 80,
153 .brightness_change_step = BRIGHTNESS_CHANGE_STEP,
154 .lcd_always_on = LCD_ALWAYS_ON,
156 .framerate_app = {0, 0, 0, 0},
157 .control_display = 0,
158 .powerkey_doublepress = 0,
159 .accel_sensor_on = ACCEL_SENSOR_ON,
160 .continuous_sampling = CONTINUOUS_SAMPLING,
161 .timeout_enable = true,
162 .input_support = true,
163 .display_init_direction = DISPLAY_INIT_DIRECTION_HORIZONTAL,
164 .lockcheck_timeout = 600,
165 .aod_enter_level = 40,
167 .touch_wakeup = false,
168 .display_on_usb_conn_changed = true,
169 .display_dpms_type = DISPLAY_DPMS_TYPE_WINDOW_MANAGER,
172 struct display_function_info display_info = {
173 .update_auto_brightness = NULL,
174 .set_autobrightness_min = NULL,
175 .reset_autobrightness_min = NULL,
176 .face_detection = NULL,
179 inline const struct display_config* get_var_display_config()
181 return &display_conf;
184 inline struct state* state_st(enum state_t state)
186 return &states[state];
189 static const char* __device_flags_to_string(enum device_flags flags)
191 if (flags & LCD_ON_BY_GESTURE)
193 else if (flags & (LCD_ON_BY_POWER_KEY | LCD_OFF_BY_POWER_KEY))
194 return POWER_KEY_STR;
195 else if (flags & (LCD_ON_BY_EVENT | LCD_OFF_BY_EVENT))
197 else if (flags & LCD_ON_BY_TOUCH)
199 else if (flags & LCD_OFF_BY_TIMEOUT)
205 bool touch_event_blocked(void)
207 return touch_blocked;
210 void lcd_on_procedure(int state, enum device_flags flag)
212 unsigned long flags = NORMAL_MODE;
213 display_plugin_get_device_flags(&flags);
217 * Display on procedure
219 * step 2. broadcast lcd on signal with cause
220 * step 3. set brightness
221 * step 4. set pmstate of vconf
222 * step 5. display on operate
224 * - b. TSP(touch screen) and touchkey enable
225 * step 6. broadcast lcd on complete signal
226 * step 7. key backlight enable
230 _I("[lcdstep] 0x%lx", flags);
232 /* send LCDOn dbus signal */
233 if (!lcdon_broadcast)
234 broadcast_lcd_on(SIGNAL_PRE, flags);
236 /* Update brightness level */
237 if (state == LCD_DIM)
238 display_backlight_set_brightness_by_dim_brightness();
239 else if (state == LCD_NORMAL)
240 display_backlight_update_by_default_brightness();
242 if (state == LCD_NORMAL)
243 set_setting_pmstate(S_NORMAL);
244 else if (state == LCD_DIM)
245 set_setting_pmstate(S_LCDDIM);
247 display_start_dependent_device(flags);
249 if (!lcdon_broadcast) {
250 broadcast_lcd_on(SIGNAL_POST, flags);
251 lcdon_broadcast = true;
254 touchled_control_backlight(TOUCHLED_DIRECT_ON);
256 touch_blocked = false;
259 inline void lcd_off_procedure(enum device_flags flag)
261 unsigned long flags = NORMAL_MODE;
262 display_plugin_get_device_flags(&flags);
266 * Display off procedure
267 * step 0. enhance mode off using nofity (e.g mdnie, HBM, LBM)
268 * step 1. broadcast lcd off signal with cause
269 * step 2. set pmstate of vconf
270 * step 3. display off operate
272 * - b. TSP(touch screen) and touchkey disable
273 * step 4. broadcast lcd off complete siganl
274 * step 5. enter doze mode if it is enabled
276 _I("[lcdstep] 0x%lx", flags);
280 device_notify(DEVICE_NOTIFIER_LCD_OFF, NULL);
282 if (lcdon_broadcast) {
283 broadcast_lcd_off(SIGNAL_PRE, flags);
284 lcdon_broadcast = false;
289 touch_blocked = true;
291 set_setting_pmstate(S_LCDOFF);
293 touchled_control_backlight(TOUCHLED_DIRECT_OFF);
295 display_stop_dependent_device(flags);
297 broadcast_lcd_off(SIGNAL_POST, flags);
298 device_notify(DEVICE_NOTIFIER_LCD_OFF_COMPLETE, NULL);
303 static void update_display_locktime(int time)
305 display_state_transition_set_lock_screen_timeout(time);
306 display_state_transition_update_display_state_timeout_by_priority();
309 void lcd_on_direct(enum device_flags flags)
311 enum hal_device_power_transition_reason reason;
313 if (flags & LCD_ON_BY_POWER_KEY)
314 reason = HAL_DEVICE_POWER_TRANSITION_REASON_POWER_KEY;
315 else if (flags & LCD_ON_BY_TOUCH)
316 reason = HAL_DEVICE_POWER_TRANSITION_REASON_TOUCH_SCREEN;
318 reason = HAL_DEVICE_POWER_TRANSITION_REASON_UNKNOWN;
320 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL, reason, NULL);
321 set_pm_cur_state(S_NORMAL);
323 _D("lcd is on directly");
324 gettimeofday(&lcdon_tv, NULL);
325 lcd_on_procedure(LCD_NORMAL, flags);
327 update_display_locktime(LOCK_SCREEN_INPUT_TIMEOUT);
330 static inline bool check_lcd_is_on(void)
332 if (display_panel_get_dpms_cached_state() != DPMS_ON)
338 static gboolean timer_refresh_cb(gpointer data)
342 /* state transition */
343 set_pm_old_state(get_pm_cur_state());
344 set_pm_cur_state(S_NORMAL);
345 st = &states[get_pm_cur_state()];
348 st->action(st->timeout);
353 int custom_lcdon(int timeout)
360 if (check_lcd_is_on() == false)
361 lcd_on_direct(LCD_ON_BY_GESTURE);
363 _I("Custom lcd on timeout(%d ms).", timeout);
364 if (set_custom_lcdon_timeout(timeout) == true)
365 display_state_transition_update_display_state_timeout_by_priority();
367 /* state transition */
368 set_pm_old_state(get_pm_cur_state());
369 set_pm_cur_state(S_NORMAL);
370 st = &states[get_pm_cur_state()];
374 st->action(st->timeout);
376 g_idle_add(timer_refresh_cb, NULL);
381 int custom_lcdoff(enum device_flags flag)
385 check_processes(S_NORMAL);
386 check_processes(S_LCDDIM);
388 /* check holdkey block flag in lock node */
389 if (check_lock_condition(S_NORMAL) || check_lock_condition(S_LCDDIM)) {
391 * When another proccess is normal lock, device is received call then,
392 * call app can be changed to lcd state by proximity.
393 * If proximity is near then normal lock will be unlocked.
395 if (flag & LCD_OFF_BY_PROXIMITY) {
396 _I("custom lcd off by proximity, delete normal lock");
397 delete_condition(S_NORMAL);
399 _I("skip custom lcd off");
404 _I("custom lcd off by flag(%d)", flag);
405 if (display_panel_get_dpms_cached_state() == DPMS_ON)
406 lcd_off_procedure(flag);
408 if (set_custom_lcdon_timeout(0) == true)
409 display_state_transition_update_display_state_timeout_by_priority();
411 /* state transition */
412 set_pm_old_state(get_pm_cur_state());
413 set_pm_cur_state(S_LCDOFF);
414 st = &states[get_pm_cur_state()];
418 st->action(st->timeout);
423 int display_on_by_reason(const char *reason, int timeout)
429 str_len = strlen(reason);
431 if (!strncmp(reason, GESTURE_STR, str_len))
432 flag = LCD_ON_BY_GESTURE;
433 else if (!strncmp(reason, EVENT_STR, str_len))
434 flag = LCD_ON_BY_EVENT;
436 _E("Reason is unknown(%s)", reason);
441 _E("Cannot setting timeout %d", timeout);
445 if (check_lcd_is_on() == false)
448 _I("platform lcd on by %s (%d ms)", reason, timeout);
449 if (set_custom_lcdon_timeout(timeout) == true)
450 display_state_transition_update_display_state_timeout_by_priority();
452 /* state transition */
453 set_pm_old_state(get_pm_cur_state());
454 set_pm_cur_state(S_NORMAL);
455 st = &states[get_pm_cur_state()];
459 st->action(st->timeout);
464 int display_off_by_reason(const char *reason)
470 str_len = strlen(reason);
472 if (!strncmp(reason, GESTURE_STR, str_len)) {
473 check_processes(S_NORMAL);
474 check_processes(S_LCDDIM);
476 /* check holdkey block flag in lock node */
477 if (check_lock_condition(S_NORMAL) || check_lock_condition(S_LCDDIM)) {
478 _I("skip platform lcd off by gesture");
481 flag = LCD_OFF_BY_GESTURE;
482 } else if (!strncmp(reason, PALM_STR, str_len)) {
483 delete_condition(S_NORMAL);
484 delete_condition(S_LCDDIM);
486 flag = LCD_OFF_BY_PALM;
488 _E("Reason is unknown(%s)", reason);
492 _I("platform lcd off by %s", reason);
493 if (display_panel_get_dpms_cached_state() == DPMS_ON)
494 lcd_off_procedure(flag);
496 /* state transition */
497 set_pm_old_state(get_pm_cur_state());
498 set_pm_cur_state(S_LCDOFF);
499 st = &states[get_pm_cur_state()];
503 st->action(st->timeout);
508 static void default_proc_change_state_action(enum state_t next, int timeout)
512 set_pm_old_state(get_pm_cur_state());
513 set_pm_cur_state(next);
515 st = &states[get_pm_cur_state()];
517 if (st && st->action) {
519 st->action(st->timeout);
525 static int default_proc_change_state(unsigned int cond, pid_t pid)
529 next = GET_COND_STATE(cond);
530 _I("Change process(%d) state to %s.", pid, states[next].name);
534 if (check_lcd_is_on() == false)
535 lcd_on_direct(LCD_ON_BY_EVENT);
536 update_display_locktime(LOCK_SCREEN_CONTROL_TIMEOUT);
537 default_proc_change_state_action(next, -1);
540 default_proc_change_state_action(next, -1);
543 if (display_panel_get_dpms_cached_state() == DPMS_ON)
544 lcd_off_procedure(LCD_OFF_BY_EVENT);
545 if (set_custom_lcdon_timeout(0))
546 display_state_transition_update_display_state_timeout_by_priority();
547 default_proc_change_state_action(next, -1);
550 _I("Dangerous requests.");
551 /* at first LCD_OFF and then goto sleep */
552 /* state transition */
553 default_proc_change_state_action(S_LCDOFF, TIMEOUT_NONE);
554 delete_condition(S_NORMAL);
555 delete_condition(S_LCDDIM);
556 delete_condition(S_LCDOFF);
557 if (lcdon_broadcast) {
558 _I("broadcast lcd off signal at non-lcd device");
559 broadcast_lcd_off(SIGNAL_PRE, 0);
560 broadcast_lcd_off(SIGNAL_POST, 0);
562 default_proc_change_state_action(S_SLEEP, TIMEOUT_NONE);
572 static void proc_condition_lock(PMMsg *data)
575 char pname[PATH_MAX];
576 pid_t pid = data->pid;
578 int holdkey_block, ret;
581 const char *lock_type = NULL;
583 state = GET_COND_STATE(data->cond);
584 if (state == S_START)
587 flags = GET_COND_FLAG(data->cond);
588 display_misc_get_process_name(pid, pname);
590 if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) &&
591 (pm_get_power_lock() == POWER_UNLOCK))
592 proc_change_state(data->cond, INTERNAL_LOCK_PM);
594 holdkey_block = GET_COND_FLAG(data->cond) & PM_FLAG_BLOCK_HOLDKEY;
595 tmp = display_lock_add_pmlock_node(state, pid, holdkey_block, data->timeout);
597 _E("Failed to acquire lock, state: %d, pid: %d.", state, pid);
601 if (state == S_LCDOFF)
602 set_process_active(true, pid);
604 _I("[%s] locked by %5d with %u ms", states[state].name, pid, data->timeout);
606 if (pid < INTERNAL_LOCK_BASE) {
608 if (state == S_LCDOFF)
609 lock_type = PM_LCDOFF_STR;
610 else if (state == S_LCDDIM)
611 lock_type = PM_LCDDIM_STR;
612 else if (state == S_NORMAL)
613 lock_type = PM_LCDON_STR;
616 /* power lock signal */
617 ret = gdbus_signal_emit(NULL,
618 DEVICED_PATH_DISPLAY,
619 DEVICED_INTERFACE_DISPLAY,
621 g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
623 _E("Failed to send dbus signal PowerLock.");
627 _SD("be requested LOCK info pname(%s), holdkeyblock(%d) flags(%d)",
628 pname, holdkey_block, flags);
629 set_lock_time(pid, pname, state);
631 device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
634 static void proc_condition_unlock(PMMsg *data)
636 pid_t pid = data->pid;
639 char pname[PATH_MAX];
643 const char *lock_type = NULL;
645 state = GET_COND_STATE(data->cond);
649 flags = GET_COND_FLAG(data->cond);
650 display_misc_get_process_name(pid, pname);
652 tmp = find_node(state, pid);
653 del_node(state, tmp);
655 if (state == S_LCDOFF)
656 set_process_active(false, pid);
658 _I("[%s] unlocked by %5d", states[state].name, pid);
660 if (pid < INTERNAL_LOCK_BASE) {
662 if (state == S_LCDOFF)
663 lock_type = PM_LCDOFF_STR;
664 else if (state == S_LCDDIM)
665 lock_type = PM_LCDDIM_STR;
666 else if (state == S_NORMAL)
667 lock_type = PM_LCDON_STR;
670 /* power unlock signal */
671 ret = gdbus_signal_emit(NULL,
672 DEVICED_PATH_DISPLAY,
673 DEVICED_INTERFACE_DISPLAY,
675 g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
677 _E("Failed to send dbus signal PowerUnlock.");
681 _SD("be requested UNLOCK info pname(%s) flag(%d)", pname, flags);
682 set_unlock_time(pid, state);
684 device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
687 static int proc_condition(PMMsg *data)
691 if (IS_COND_REQUEST_LOCK(data->cond))
692 proc_condition_lock(data);
694 if (IS_COND_REQUEST_UNLOCK(data->cond))
695 proc_condition_unlock(data);
697 if (!display_conf.timeout_enable)
700 flags = GET_COND_FLAG(data->cond);
702 /* guard time for suspend */
703 if (get_pm_cur_state() == S_LCDOFF)
704 display_state_transition_reset_state_transition_timeout(states[S_LCDOFF].timeout);
706 if (flags & PM_FLAG_RESET_TIMER)
707 display_state_transition_reset_state_transition_timeout(states[get_pm_cur_state()].timeout);
710 if (!display_state_transition_is_there_state_transition_timer())
711 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_TIMEOUT);
716 /* SIGHUP signal handler
717 * For debug... print info to syslog
719 static void sig_hup(int signo)
721 _I("received sig hub %d", signo);
726 static gboolean lcd_on_expired(void *data)
733 /* check state of lock */
734 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
735 if (ret > 0 && lock_state == VCONFKEY_IDLE_LOCK)
736 return G_SOURCE_REMOVE;
738 _E("Failed to get vconf value for idle lock state: %d", vconf_get_ext_errno());
740 /* lock screen is not launched yet, but lcd is on */
741 if (check_lcd_is_on() == false)
742 lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
744 return G_SOURCE_REMOVE;
747 static inline void stop_lock_timer(void)
749 if (lock_timeout_id) {
750 g_source_remove(lock_timeout_id);
755 static void check_lock_screen(void)
761 /* check state of lock */
762 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
763 if (ret < 0 || lock_state == VCONFKEY_IDLE_LOCK)
766 /* Use time to check lock is done. */
767 lock_timeout_id = g_timeout_add(display_conf.lock_wait_time,
768 lcd_on_expired, NULL);
771 /* default enter action function */
772 static int default_action(int timeout)
774 int wakeup_count = -1, pm_cur_state;
777 static time_t last_update_time = 0;
778 static int last_timeout = 0;
779 struct timeval now_tv;
782 bool lcd_paneloff_mode = false;
784 if (status != DEVICE_OPS_STATUS_START) {
785 _E("Display is not started.");
789 if (get_pm_cur_state() != S_SLEEP) {
790 if ((get_pm_cur_state() == S_NORMAL) &&
791 lcdon_tv.tv_sec != 0) {
792 gettimeofday(&now_tv, NULL);
793 timeout -= DIFF_TIMEVAL_MS(now_tv, lcdon_tv);
796 /* set timer with current state timeout */
797 display_state_transition_reset_state_transition_timeout(timeout);
799 if (get_pm_cur_state() == S_NORMAL) {
800 time(&last_update_time);
801 last_timeout = timeout;
803 _I("Timout set: %s state %d ms",
804 states[get_pm_cur_state()].name, timeout);
808 if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) {
809 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL,
810 power_get_wakeup_reason(), NULL);
811 set_setting_pmstate(get_pm_cur_state());
812 pm_cur_state = get_pm_cur_state();
813 device_notify(DEVICE_NOTIFIER_LCD, (void *)&pm_cur_state);
816 if ((get_pm_old_state() == S_NORMAL) && (get_pm_cur_state() != S_NORMAL)) {
818 diff = difftime(now, last_update_time);
819 _I("S_NORMAL is changed to %s (timeout=%d ms diff=%.0f s).",
820 states[get_pm_cur_state()].name, last_timeout, diff);
823 switch (get_pm_cur_state()) {
826 * normal state : backlight on and restore
827 * the previous brightness
829 if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
831 else if (get_pm_old_state() == S_LCDDIM)
832 display_backlight_update_by_default_brightness();
834 if (check_lcd_is_on() == false)
835 lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
839 display_backlight_get_custom_status(&custom_status);
840 if ((get_pm_old_state() == S_NORMAL) && custom_status) {
841 display_backlight_get_brightness(&brightness);
842 display_backlight_set_custom_brightness(brightness);
844 /* lcd dim state : dim the brightness */
845 display_backlight_set_brightness_by_dim_brightness();
847 if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
848 lcd_on_procedure(LCD_DIM, NORMAL_MODE);
852 if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF)) {
854 /* lcd off state : turn off the backlight */
855 if (display_panel_get_dpms_cached_state() == DPMS_ON)
856 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
859 display_panel_get_lcd_paneloff_mode(&lcd_paneloff_mode);
860 if (display_panel_get_dpms_cached_state() == DPMS_ON
861 || lcd_paneloff_mode)
862 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
866 if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF))
869 if (display_panel_get_dpms_cached_state() == DPMS_ON)
870 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
872 if (!pm_get_power_lock_support()) {
873 /* sleep state : set system mode to SUSPEND */
874 if (get_wakeup_count(&wakeup_count) < 0)
875 _E("Wakeup count read error.");
877 if (wakeup_count < 0) {
878 _I("Wakup Event. Can not enter suspend mode.");
882 if (set_wakeup_count(wakeup_count) < 0) {
883 _E("Wakeup count write error.");
894 pm_history_save(PM_LOG_SLEEP, get_pm_cur_state());
896 power_request_change_state(DEVICED_POWER_STATE_SLEEP, HAL_DEVICE_POWER_TRANSITION_REASON_DISPLAY_OFF_TIMEOUT);
900 if (!pm_get_power_lock_support()) {
902 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_DEVICE);
907 static void default_saving_mode(int onoff)
910 set_pm_status_flag(PWRSV_FLAG);
912 clear_pm_status_flag(PWRSV_FLAG);
914 if (get_pm_cur_state() == S_NORMAL)
915 display_backlight_update_by_default_brightness();
918 int poll_callback(int condition, PMMsg *data)
920 static time_t last_t;
923 if (status != DEVICE_OPS_STATUS_START) {
924 _E("Display logic is not started.");
928 if (condition == INPUT_POLL_EVENT) {
929 if ((get_pm_cur_state() == S_LCDOFF) || (get_pm_cur_state() == S_SLEEP))
930 _I("Input event signal at Display Off");
932 if ((last_t != now) ||
933 (get_pm_cur_state() == S_LCDOFF) ||
934 (get_pm_cur_state() == S_SLEEP)) {
935 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_INPUT);
940 if (condition == PM_CONTROL_EVENT) {
941 proc_condition(data);
943 if (IS_COND_REQUEST_CHANGE(data->cond))
944 proc_change_state(data->cond, data->pid);
950 static int update_setting(int key_idx, int val)
955 case SETTING_TO_NORMAL:
956 display_state_transition_update_display_state_timeout_by_priority();
957 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_INPUT);
959 case SETTING_LOW_BATT:
960 if (display_misc_is_low_battery_state(val)) {
961 if (!(get_pm_status_flag() & CHRGR_FLAG))
962 power_saving_func(true);
963 set_pm_status_flag(LOWBT_FLAG);
965 if (get_pm_status_flag() & PWRSV_FLAG)
966 power_saving_func(false);
967 clear_pm_status_flag(LOWBT_FLAG);
968 clear_pm_status_flag(BRTCH_FLAG);
969 ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, false);
971 _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
974 case SETTING_CHARGING:
976 if (get_pm_status_flag() & LOWBT_FLAG) {
977 power_saving_func(false);
978 clear_pm_status_flag(LOWBT_FLAG);
980 set_pm_status_flag(CHRGR_FLAG);
983 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
986 bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
987 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
989 if (display_misc_is_low_battery_state(bat_state)) {
990 power_saving_func(true);
991 set_pm_status_flag(LOWBT_FLAG);
993 clear_pm_status_flag(CHRGR_FLAG);
996 case SETTING_BRT_LEVEL:
997 if (get_pm_status_flag() & PWRSV_FLAG) {
998 set_pm_status_flag(BRTCH_FLAG);
999 ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, true);
1001 _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
1002 _I("Brightness changed in low battery,"
1003 "escape dim state.");
1005 display_backlight_set_default_brightness(val);
1007 case SETTING_LOCK_SCREEN:
1008 set_lock_screen_state(val);
1009 if (val == VCONFKEY_IDLE_UNLOCK)
1010 touchled_control_backlight(TOUCHLED_DIRECT_OFF);
1012 /* LCD on if lock screen show before waiting time */
1013 if ((get_pm_cur_state() == S_NORMAL) &&
1014 val == VCONFKEY_IDLE_LOCK &&
1015 display_panel_get_dpms_cached_state() != DPMS_ON &&
1016 is_lcdon_blocked() == LCDON_BLOCK_NONE)
1017 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1019 display_state_transition_update_display_state_timeout_by_priority();
1020 if (get_pm_cur_state() == S_NORMAL)
1021 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_INPUT);
1023 case SETTING_LOCK_SCREEN_BG:
1024 set_lock_screen_bg_state(val);
1025 display_state_transition_update_display_state_timeout_by_priority();
1026 if (get_pm_cur_state() == S_NORMAL)
1027 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_INPUT);
1029 case SETTING_POWER_CUSTOM_BRIGHTNESS:
1030 if (val == VCONFKEY_PM_CUSTOM_BRIGHTNESS_ON)
1031 display_backlight_set_custom_status(true);
1033 display_backlight_set_custom_status(false);
1042 static void check_seed_status(void)
1049 int lock_screen_timeout = 0;
1051 /* Charging check */
1052 if (fp_get_charging_status && (fp_get_charging_status(&tmp) == 0) && (tmp > 0))
1053 set_pm_status_flag(CHRGR_FLAG);
1055 ret = get_setting_brightness(&tmp);
1056 if (ret != 0 || (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS)) {
1057 _I("Failed to read vconf value for brightness.");
1058 brt = display_conf.pm_default_brightness;
1059 if (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS) {
1060 ret = vconf_set_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, brt);
1062 _E("Failed to set vconf value for lcd brightness: %d", vconf_get_ext_errno());
1066 _I("Set brightness(%d) from setting app.", tmp);
1067 display_backlight_set_default_brightness(tmp);
1068 display_backlight_set_brightness(tmp);
1070 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &bat_state);
1072 bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
1073 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
1075 if (display_misc_is_low_battery_state(bat_state)) {
1076 if (!(get_pm_status_flag() & CHRGR_FLAG)) {
1077 power_saving_func(true);
1078 set_pm_status_flag(LOWBT_FLAG);
1082 /* lock screen check */
1083 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1086 _E("Failed to get vconf value for idle lock state: %d", vconf_get_ext_errno());
1088 set_lock_screen_state(lock_state);
1089 display_state_transition_get_lock_screen_timeout(&lock_screen_timeout);
1090 if (lock_state == VCONFKEY_IDLE_LOCK) {
1091 states[S_NORMAL].timeout = lock_screen_timeout;
1092 _I("LCD NORMAL timeout(%d ms) is set"
1093 " for lock screen.", lock_screen_timeout);
1099 static void init_lcd_operation(void)
1101 const struct device_ops *ops = NULL;
1103 ops = find_device("display");
1104 if (!check_default(ops))
1105 display_register_dependent_device(ops);
1107 ops = find_device("touchkey");
1108 if (!check_default(ops))
1109 display_register_dependent_device(ops);
1111 ops = find_device("touchscreen");
1112 if (!check_default(ops))
1113 display_register_dependent_device(ops);
1116 static void exit_lcd_operation(void)
1118 display_unregister_dependent_device();
1130 static const char *errMSG[INIT_END] = {
1131 [INIT_SETTING] = "setting init error",
1132 [INIT_INTERFACE] = "lowlevel interface(sysfs or others) init error",
1133 [INIT_POLL] = "input devices poll init error",
1134 [INIT_FIFO] = "FIFO poll init error",
1135 [INIT_DBUS] = "d-bus init error",
1138 int set_lcd_timeout(int on, int dim, int holdkey_block, const char *name)
1140 unsigned int custom_normal_timeout = 0;
1141 unsigned int custom_dim_timeout = 0;
1143 if (on == 0 && dim == 0) {
1144 _I("LCD timeout changed: default setting");
1145 display_state_transition_set_custom_timeout(S_NORMAL, 0);
1146 display_state_transition_set_custom_timeout(S_LCDDIM, 0);
1147 } else if (on < 0 || dim < 0) {
1148 _E("Failed to set value(on=%d dim=%d).", on, dim);
1151 _I("LCD timeout changed: on=%ds dim=%ds", on, dim);
1152 display_state_transition_set_custom_timeout(S_NORMAL, SEC_TO_MSEC(on));
1153 display_state_transition_set_custom_timeout(S_LCDDIM, SEC_TO_MSEC(dim));
1155 /* Apply new backlight time */
1156 display_state_transition_update_display_state_timeout_by_priority();
1157 if (get_pm_cur_state() == S_NORMAL)
1158 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_INPUT);
1160 if (holdkey_block) {
1161 display_lock_set_custom_holdkey_block(true);
1162 _I("Hold key disabled.");
1164 display_lock_set_custom_holdkey_block(false);
1165 _I("Hold key enabled.");
1168 if (custom_change_name) {
1169 free(custom_change_name);
1170 custom_change_name = 0;
1173 display_state_transition_get_custom_timeout(S_NORMAL, &custom_normal_timeout);
1174 display_state_transition_get_custom_timeout(S_LCDDIM, &custom_dim_timeout);
1175 if (custom_normal_timeout == 0 &&
1176 custom_dim_timeout == 0 &&
1180 custom_change_name = strndup(name, strlen(name));
1181 if (!custom_change_name) {
1182 _E("Failed to malloc.");
1183 display_state_transition_set_custom_timeout(S_NORMAL, 0);
1184 display_state_transition_set_custom_timeout(S_LCDDIM, 0);
1185 display_lock_set_custom_holdkey_block(false);
1192 void reset_lcd_timeout(GDBusConnection *conn,
1193 const gchar *sender,
1194 const gchar *unique_name,
1200 if (!custom_change_name)
1203 if (strcmp(sender, custom_change_name))
1206 _I("reset lcd timeout: Set default timeout. sender=%s", sender);
1208 free(custom_change_name);
1209 custom_change_name = 0;
1210 display_state_transition_set_custom_timeout(S_NORMAL, 0);
1211 display_state_transition_set_custom_timeout(S_LCDDIM, 0);
1212 display_lock_set_custom_holdkey_block(false);
1214 display_state_transition_update_display_state_timeout_by_priority();
1215 if (get_pm_cur_state() == S_NORMAL)
1216 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_INPUT);
1219 static int delayed_init_done(void *data)
1221 static bool done = false;
1230 _I("Booting done, release booting lock.");
1231 if (disp_plgn->pm_unlock_internal) {
1232 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL, PM_SLEEP_MARGIN);
1233 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF, PM_SLEEP_MARGIN);
1239 static gboolean delayed_dpms_init_done(gpointer data)
1243 if (!display_panel_init_dpms())
1244 return G_SOURCE_CONTINUE;
1246 switch (get_pm_cur_state()) {
1249 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1250 if (display_conf.timeout_enable) {
1251 timeout = states[S_NORMAL].timeout;
1252 /* check minimun lcd on time */
1253 if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
1254 timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
1255 display_state_transition_reset_state_transition_timeout(timeout);
1259 lcd_off_procedure(LCD_OFF_BY_EVENT);
1260 timeout = display_conf.lcdoff_timeout;
1261 display_state_transition_reset_state_transition_timeout(timeout);
1267 return G_SOURCE_REMOVE;
1270 static void add_timer_for_dpms_init(void)
1272 guint id = g_timeout_add(500/* milliseconds */, delayed_dpms_init_done, NULL);
1274 _E("Failed to add display_panel_init_dpms timeout.");
1277 static void init_display_states(void *data)
1279 struct display_plugin *dp = (struct display_plugin *) data;
1280 for(int i = 0; i < S_END; i++)
1281 dp->display_states[i] = &states[i];
1284 * Power manager Main
1288 static int display_probe(void *data)
1291 struct display_plugin *dp = (struct display_plugin *) data;
1294 dp->config = &display_conf;
1295 init_display_states(dp);
1296 setup_display_plugin_backlight_ops(dp);
1297 dp->set_dim_state = NULL;
1298 dp->get_device_flags = NULL;
1299 setup_display_plugin_backlight_ops(dp);
1302 * load display service
1303 * if there is no display shared library,
1304 * deviced does not provide any method and function of display.
1306 ret = display_service_load();
1310 /* display_plugin instance initialization */
1311 init_pm_internal(data);
1312 disp_plgn->device_flags_to_string = __device_flags_to_string;
1314 if (battery_plgn->handle) {
1315 fp_get_charging_status = dlsym(battery_plgn->handle, "get_charging_status");
1316 if (!fp_get_charging_status)
1317 _E("Failed to obtain address of get_charging_status");
1319 _I("There is no battery module.");
1324 static int input_init_handler(void)
1326 if (!display_conf.input_support)
1327 remove_device_by_devname("input");
1332 static int power_resume_from_echomem_callback(void *data)
1334 system_wakeup_flag = true;
1335 if (check_wakeup_src() == EVENT_DEVICE)
1336 /* system waked up by devices */
1337 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_DEVICE);
1339 /* system waked up by user input */
1340 display_state_transition_do_state_transition(get_pm_cur_state(), EVENT_INPUT);
1345 static int poweroff_triggered_callback(void *udata)
1347 int val = (int)(intptr_t) udata;
1350 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
1351 clear_pm_status_flag(PWROFF_FLAG);
1353 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
1354 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
1355 set_pm_status_flag(PWROFF_FLAG);
1362 static void esd_action(void)
1364 const struct device_ops *touchscreen_ops = NULL;
1368 touchscreen_ops = find_device("touchscreen");
1370 if (!check_default(touchscreen_ops))
1371 touchscreen_ops->stop(NORMAL_MODE);
1372 display_panel_set_panel_state_by_off_state(NORMAL_MODE);
1373 display_panel_set_panel_state_by_on_state(NORMAL_MODE);
1374 if (!check_default(touchscreen_ops))
1375 touchscreen_ops->start(NORMAL_MODE);
1378 static void lcd_uevent_changed(struct udev_device *dev)
1380 const char *devpath;
1383 devpath = udev_device_get_devpath(dev);
1387 if (!fnmatch(LCD_ESD_PATH, devpath, 0)) {
1388 action = udev_device_get_action(dev);
1389 if (!strcmp(action, UDEV_CHANGE))
1394 static const struct uevent_handler lcd_uevent_ops = {
1395 .subsystem = LCD_EVENT_SUBSYSTEM,
1396 .uevent_func = lcd_uevent_changed,
1400 static void display_init(void *data)
1403 unsigned int flags = (WITHOUT_STARTNOTI | FLAG_X_DPMS);
1406 _I("Start power manager.");
1408 signal(SIGHUP, sig_hup);
1410 power_saving_func = default_saving_mode;
1412 /* load configutation */
1413 ret = display_load_config(&display_conf);
1415 _W("Failed to load '%s', use default value: %d",
1416 DISPLAY_CONF_FILE, ret);
1418 register_kernel_uevent_control(&lcd_uevent_ops);
1420 register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
1421 register_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
1422 register_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
1423 register_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
1424 register_notifier(DEVICE_NOTIFIER_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
1425 register_notifier(DEVICE_NOTIFIER_POWEROFF_TRIGGERED, poweroff_triggered_callback);
1427 init_save_userlock();
1429 for (i = INIT_SETTING; i < INIT_END; i++) {
1432 ret = init_setting(update_setting);
1434 case INIT_INTERFACE:
1435 if (display_conf.timeout_enable)
1436 display_initialize_display_state_timeout_from_setting();
1437 ret = init_sysfs(flags);
1441 ret = input_init_handler();
1443 pm_lock_detector_init();
1447 ret = init_pm_dbus();
1451 _E("Failed to init: %s", errMSG[i]);
1456 if (i == INIT_END) {
1457 display_ops_init(NULL);
1458 #ifdef ENABLE_PM_LOG
1461 init_lcd_operation();
1462 check_seed_status();
1464 /* In smd test, TSP should be turned off if display panel is not existed. */
1465 if (display_panel_get_dpms_cached_state() == -ENOENT) {
1466 _I("Display panel is not existed.");
1467 lcd_direct_control(DPMS_OFF, NORMAL_MODE);
1468 exit_lcd_operation();
1471 /* wm_ready needs to be checked
1472 * since display manager can be launched later than deviced.
1473 * In the case, display cannot be turned on at the first booting */
1474 // wm_ready = check_wm_ready();
1475 if (display_panel_init_dpms()) {
1476 if (is_lcdon_blocked() != LCDON_BLOCK_NONE)
1477 lcd_off_procedure(LCD_OFF_BY_EVENT);
1479 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1481 add_timer_for_dpms_init();
1484 if (display_conf.lcd_always_on) {
1485 _I("LCD always on.");
1486 display_state_transition_set_transition_table_display_state(S_NORMAL, S_NORMAL, EVENT_TIMEOUT);
1489 if (flags & WITHOUT_STARTNOTI) { /* start without noti */
1490 _I("Start Power managing without noti");
1491 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL,
1492 HAL_DEVICE_POWER_TRANSITION_REASON_UNKNOWN, NULL);
1494 * Lock lcd off until booting is done.
1495 * deviced guarantees all booting script is executing.
1496 * Last script of booting unlocks this suspend blocking state.
1498 if (disp_plgn->pm_lock_internal)
1499 disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF,
1500 STAY_CUR_STATE, DELAYED_INIT_WATING_TIME);
1502 /* Initial display state right after the booting done */
1503 if (is_lcdon_blocked())
1504 set_pm_cur_state(S_LCDOFF);
1506 set_pm_cur_state(S_NORMAL);
1507 ret = vconf_set_int(VCONFKEY_PM_STATE, get_pm_cur_state());
1509 _E("Failed to set vconf value for pm cur state: %d", vconf_get_ext_errno());
1511 status = DEVICE_OPS_STATUS_START;
1512 if (display_conf.timeout_enable) {
1513 timeout = states[S_NORMAL].timeout;
1514 /* check minimun lcd on time */
1515 if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
1516 timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
1518 if (disp_plgn->pm_lock_internal)
1519 disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL,
1520 STAY_CUR_STATE, timeout);
1525 set_display_init_direction(display_conf.display_init_direction);
1529 static void display_exit(void *data)
1533 status = DEVICE_OPS_STATUS_STOP;
1535 /* Set current state to S_NORMAL */
1536 set_pm_cur_state(S_NORMAL);
1537 set_setting_pmstate(get_pm_cur_state());
1538 /* timeout is not needed */
1539 display_state_transition_reset_state_transition_timeout(TIMEOUT_NONE);
1541 unregister_kernel_uevent_control(&lcd_uevent_ops);
1543 display_ops_exit(NULL);
1545 for (i = i - 1; i >= INIT_SETTING; i--) {
1550 case INIT_INTERFACE:
1554 unregister_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
1555 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
1556 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
1557 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
1558 unregister_notifier(DEVICE_NOTIFIER_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
1564 exit_lcd_operation();
1565 free_lock_info_list();
1567 /* free display service */
1568 display_service_free();
1570 _I("Stop power manager.");
1573 static int display_start(enum device_flags flags)
1576 if (flags & NORMAL_MODE) {
1577 if (flags & LCD_PANEL_OFF_MODE)
1579 display_panel_set_panel_state_by_standby_state(true);
1582 display_panel_set_panel_state_by_on_state(flags);
1587 /* CORE LOGIC MODE */
1588 if (!(flags & CORE_LOGIC_MODE))
1591 if (status == DEVICE_OPS_STATUS_START)
1594 if (display_probe(NULL) < 0)
1602 static int display_stop(enum device_flags flags)
1605 if (flags & NORMAL_MODE || flags & FORCE_OFF_MODE) {
1606 display_panel_set_panel_state_by_off_state(flags);
1610 /* CORE LOGIC MODE */
1611 if (!(flags & CORE_LOGIC_MODE))
1614 if (status == DEVICE_OPS_STATUS_STOP)
1622 static int display_status(void)
1627 static const struct device_ops display_plugin_device_ops = {
1628 .disable_auto_init = true,
1629 DECLARE_NAME_LEN("display-plugin"),
1630 .probe = display_probe,
1631 .init = display_init,
1632 .exit = display_exit,
1633 .start = display_start,
1634 .stop = display_stop,
1635 .status = display_status,
1638 DEVICE_OPS_REGISTER(&display_plugin_device_ops)
1640 static void __CONSTRUCTOR__ initialize(void)
1642 disp_plgn = get_var_display_plugin();
1644 _E("Failed to get display plugin variable.");
1646 backlight_ops = get_var_backlight_ops();
1648 _E("Failed to get backlight operator variable.");
1650 battery_plgn = get_var_battery_plugin();
1652 _E("Failed to get battery plugin variable.");