blob: d574b8d5dc2d28df151ec59952e500f172ffafeb [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
c_wufeng39755e62016-01-29 10:42:44 +080046#define PWRKEY_LONG_PRESS_COUNT 0x4000
Matthew Qin82323d82014-02-11 16:28:50 +080047#define QPNP_DEFAULT_TIMEOUT 250
48#define PWRKEY_DETECT_FREQUENCY 50
49
50static struct timer pon_timer;
51static uint32_t pon_timer_complete = 0;
52
53/*
54 * Function to check if the the power key is pressed long enough.
55 * Return 0 if boot time more than PWRKEY_LONG_PRESS_COUNT.
56 * Return 1 if boot time less than PWRKEY_LONG_PRESS_COUNT.
57 */
58static uint32_t is_pwrkey_time_expired()
59{
60 /* power on button tied in with PMIC KPDPWR. */
61 uint32_t sclk_count = platform_get_sclk_count();
62
63 /* Here check if the long press power-key lasts for 1.5s */
64 if (sclk_count > PWRKEY_LONG_PRESS_COUNT)
65 return 0;
66 else
67 return 1;
68}
69
70/*
71 * Function to check if the power on reason is power key triggered.
72 * Return 1 if it is triggered by power key.
73 * Return 0 if it is not triggered by power key.
74 */
75static uint32_t is_pwrkey_pon_reason()
76{
Parth Dixit42dc2642015-06-19 16:48:22 +053077#if PMI_CONFIGURED
78 return target_is_pwrkey_pon_reason();
79#else
Matthew Qin82323d82014-02-11 16:28:50 +080080 uint8_t pon_reason = pm8x41_get_pon_reason();
81
82 if (pm8x41_get_is_cold_boot() && (pon_reason == KPDPWR_N))
83 return 1;
84 else
85 return 0;
Parth Dixit42dc2642015-06-19 16:48:22 +053086#endif
Matthew Qin82323d82014-02-11 16:28:50 +080087}
88
89/*
90 * Main timer handle: check every PWRKEY_DETECT_FREQUENCY to see
91 * if power key is pressed.
92 * Shutdown the device if power key is release before
93 * (PWRKEY_LONG_PRESS_COUNT/MPM_SLEEP_TIMETICK_COUNT) seconds.
94 */
95static enum handler_return long_press_pwrkey_timer_func(struct timer *p_timer,
96 void *arg)
97{
98 uint32_t sclk_count = platform_get_sclk_count();
99
100 /*
101 * The following condition is treated as the power key
102 * is pressed long enough.
103 * 1. if the power key is pressed last for PWRKEY_LONG_PRESS_COUNT.
104 */
105 if (sclk_count > PWRKEY_LONG_PRESS_COUNT) {
106 timer_cancel(p_timer);
107 pon_timer_complete = 1;
108 } else {
109 if (pm8x41_get_pwrkey_is_pressed()) {
110 /*
111 * For normal man response is > 0.1 secs, so we use 0.05 secs default
112 * for software to be safely detect if there is a key release action.
113 */
114 timer_set_oneshot(p_timer, PWRKEY_DETECT_FREQUENCY,
vijay kumar4f4405f2014-08-08 11:49:53 +0530115 (timer_callback)long_press_pwrkey_timer_func, NULL);
Matthew Qin82323d82014-02-11 16:28:50 +0800116 } else {
117 shutdown_device();
118 }
119 }
120
121 return INT_RESCHEDULE;
122}
123
124/*
125 * Function to wait until the power key is pressed long enough
126 */
127static void wait_for_long_pwrkey_pressed()
128{
129 uint32_t sclk_count = 0;
130
131 while (!pon_timer_complete) {
132 sclk_count = platform_get_sclk_count();
133 if (sclk_count > PWRKEY_LONG_PRESS_COUNT) {
134 timer_cancel(&pon_timer);
135 break;
136 }
c_wufeng637fd172015-09-21 18:53:56 +0800137 thread_sleep(1);
Matthew Qin82323d82014-02-11 16:28:50 +0800138 }
139}
140
141/*
142 * Function to support for shutdown detection
143 * If below condition is met, the function will shut down
144 * the device. Otherwise it will do nothing and return to
145 * normal boot.
146 * condition:
147 * 1. it is triggered by power key &&
148 * 2. the power key is released before
149 * (PWRKEY_LONG_PRESS_COUNT/MPM_SLEEP_TIMETICK_COUNT) seconds.
150 */
151void shutdown_detect()
152{
153 /*
154 * If it is booted by power key tirigger.
155 * Initialize pon_timer and call long_press_pwrkey_timer_func
156 * function to check if the power key is last press long enough.
157 */
c_wufeng637fd172015-09-21 18:53:56 +0800158 if (is_pwrkey_pon_reason()) {
159 if(!pm8x41_get_pwrkey_is_pressed()){
160 shutdown_device();
161 }
Matthew Qin82323d82014-02-11 16:28:50 +0800162 timer_initialize(&pon_timer);
vijay kumar4f4405f2014-08-08 11:49:53 +0530163 timer_set_oneshot(&pon_timer, 0,(timer_callback)long_press_pwrkey_timer_func, NULL);
Matthew Qin82323d82014-02-11 16:28:50 +0800164
165 /*
166 * Wait until long press power key timeout
167 *
168 * It will be confused to end users if we shutdown the device
169 * after the splash screen displayed. But it can be moved the
170 * wait here if the boot time is much more considered.
171 */
172 wait_for_long_pwrkey_pressed();
173 }
174}