blob: e52d4dead57bcf597d595ba1c2620fb0c8749376 [file] [log] [blame]
c_wufeng39755e62016-01-29 10:42:44 +08001/* Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
Matthew Qin82323d82014-02-11 16:28:50 +08002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
Parth Dixit42dc2642015-06-19 16:48:22 +053012 * * Neither the name of The Linux Foundation, nor the names of its
Matthew Qin82323d82014-02-11 16:28:50 +080013 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <debug.h>
30#include <reg.h>
31#include <stdlib.h>
32#include <pm8x41.h>
33#include <pm8x41_hw.h>
34#include <kernel/timer.h>
c_wufeng637fd172015-09-21 18:53:56 +080035#include <kernel/thread.h>
Matthew Qin82323d82014-02-11 16:28:50 +080036#include <platform/timer.h>
37#include <shutdown_detect.h>
vijay kumar4f4405f2014-08-08 11:49:53 +053038#include <platform.h>
39#include <target.h>
Matthew Qin82323d82014-02-11 16:28:50 +080040
c_wufeng39755e62016-01-29 10:42:44 +080041/*
42 * Sleep clock is 32.768 khz, 0x8000 count per second.
43 * Set long press wait time to 500ms to benefit boot time.
44 */
Matthew Qin82323d82014-02-11 16:28:50 +080045#define MPM_SLEEP_TIMETICK_COUNT 0x8000
jialongjhan66e1ee02020-05-13 16:12:51 +080046//<2019/01/15-RexLu, Modify long-pressing PWR key for 2.2s to boot-up.
47#define PWRKEY_LONG_PRESS_COUNT 0x1199A //2.2sec
48//>2019/01/15-RexLu
Matthew Qin82323d82014-02-11 16:28:50 +080049#define QPNP_DEFAULT_TIMEOUT 250
50#define PWRKEY_DETECT_FREQUENCY 50
51
52static struct timer pon_timer;
53static uint32_t pon_timer_complete = 0;
54
55/*
56 * Function to check if the the power key is pressed long enough.
57 * Return 0 if boot time more than PWRKEY_LONG_PRESS_COUNT.
58 * Return 1 if boot time less than PWRKEY_LONG_PRESS_COUNT.
59 */
60static uint32_t is_pwrkey_time_expired()
61{
62 /* power on button tied in with PMIC KPDPWR. */
63 uint32_t sclk_count = platform_get_sclk_count();
64
65 /* Here check if the long press power-key lasts for 1.5s */
66 if (sclk_count > PWRKEY_LONG_PRESS_COUNT)
67 return 0;
68 else
69 return 1;
70}
71
72/*
73 * Function to check if the power on reason is power key triggered.
74 * Return 1 if it is triggered by power key.
75 * Return 0 if it is not triggered by power key.
76 */
77static uint32_t is_pwrkey_pon_reason()
78{
Parth Dixit42dc2642015-06-19 16:48:22 +053079#if PMI_CONFIGURED
80 return target_is_pwrkey_pon_reason();
81#else
Matthew Qin82323d82014-02-11 16:28:50 +080082 uint8_t pon_reason = pm8x41_get_pon_reason();
83
84 if (pm8x41_get_is_cold_boot() && (pon_reason == KPDPWR_N))
85 return 1;
86 else
87 return 0;
Parth Dixit42dc2642015-06-19 16:48:22 +053088#endif
Matthew Qin82323d82014-02-11 16:28:50 +080089}
90
91/*
92 * Main timer handle: check every PWRKEY_DETECT_FREQUENCY to see
93 * if power key is pressed.
94 * Shutdown the device if power key is release before
95 * (PWRKEY_LONG_PRESS_COUNT/MPM_SLEEP_TIMETICK_COUNT) seconds.
96 */
97static enum handler_return long_press_pwrkey_timer_func(struct timer *p_timer,
98 void *arg)
99{
100 uint32_t sclk_count = platform_get_sclk_count();
101
102 /*
103 * The following condition is treated as the power key
104 * is pressed long enough.
105 * 1. if the power key is pressed last for PWRKEY_LONG_PRESS_COUNT.
106 */
107 if (sclk_count > PWRKEY_LONG_PRESS_COUNT) {
108 timer_cancel(p_timer);
109 pon_timer_complete = 1;
110 } else {
111 if (pm8x41_get_pwrkey_is_pressed()) {
112 /*
113 * For normal man response is > 0.1 secs, so we use 0.05 secs default
114 * for software to be safely detect if there is a key release action.
115 */
116 timer_set_oneshot(p_timer, PWRKEY_DETECT_FREQUENCY,
vijay kumar4f4405f2014-08-08 11:49:53 +0530117 (timer_callback)long_press_pwrkey_timer_func, NULL);
Matthew Qin82323d82014-02-11 16:28:50 +0800118 } else {
119 shutdown_device();
120 }
121 }
122
123 return INT_RESCHEDULE;
124}
125
126/*
127 * Function to wait until the power key is pressed long enough
128 */
129static void wait_for_long_pwrkey_pressed()
130{
131 uint32_t sclk_count = 0;
132
133 while (!pon_timer_complete) {
134 sclk_count = platform_get_sclk_count();
135 if (sclk_count > PWRKEY_LONG_PRESS_COUNT) {
136 timer_cancel(&pon_timer);
137 break;
138 }
c_wufeng637fd172015-09-21 18:53:56 +0800139 thread_sleep(1);
Matthew Qin82323d82014-02-11 16:28:50 +0800140 }
141}
142
143/*
144 * Function to support for shutdown detection
145 * If below condition is met, the function will shut down
146 * the device. Otherwise it will do nothing and return to
147 * normal boot.
148 * condition:
149 * 1. it is triggered by power key &&
150 * 2. the power key is released before
151 * (PWRKEY_LONG_PRESS_COUNT/MPM_SLEEP_TIMETICK_COUNT) seconds.
152 */
153void shutdown_detect()
154{
155 /*
156 * If it is booted by power key tirigger.
157 * Initialize pon_timer and call long_press_pwrkey_timer_func
158 * function to check if the power key is last press long enough.
159 */
c_wufeng637fd172015-09-21 18:53:56 +0800160 if (is_pwrkey_pon_reason()) {
161 if(!pm8x41_get_pwrkey_is_pressed()){
162 shutdown_device();
163 }
Matthew Qin82323d82014-02-11 16:28:50 +0800164 timer_initialize(&pon_timer);
vijay kumar4f4405f2014-08-08 11:49:53 +0530165 timer_set_oneshot(&pon_timer, 0,(timer_callback)long_press_pwrkey_timer_func, NULL);
Matthew Qin82323d82014-02-11 16:28:50 +0800166
167 /*
168 * Wait until long press power key timeout
169 *
170 * It will be confused to end users if we shutdown the device
171 * after the splash screen displayed. But it can be moved the
172 * wait here if the boot time is much more considered.
173 */
174 wait_for_long_pwrkey_pressed();
175 }
176}