blob: 9bde63d6a52227f02282f8ddffdae171f93e9a40 [file] [log] [blame]
Parth Dixit42dc2642015-06-19 16:48:22 +05301/* Copyright (c) 2014-2015, 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
41/* sleep clock is 32.768 khz, 0x8000 count per second */
42#define MPM_SLEEP_TIMETICK_COUNT 0x8000
43#define PWRKEY_LONG_PRESS_COUNT 0xC000
44#define QPNP_DEFAULT_TIMEOUT 250
45#define PWRKEY_DETECT_FREQUENCY 50
46
47static struct timer pon_timer;
48static uint32_t pon_timer_complete = 0;
49
50/*
51 * Function to check if the the power key is pressed long enough.
52 * Return 0 if boot time more than PWRKEY_LONG_PRESS_COUNT.
53 * Return 1 if boot time less than PWRKEY_LONG_PRESS_COUNT.
54 */
55static uint32_t is_pwrkey_time_expired()
56{
57 /* power on button tied in with PMIC KPDPWR. */
58 uint32_t sclk_count = platform_get_sclk_count();
59
60 /* Here check if the long press power-key lasts for 1.5s */
61 if (sclk_count > PWRKEY_LONG_PRESS_COUNT)
62 return 0;
63 else
64 return 1;
65}
66
67/*
68 * Function to check if the power on reason is power key triggered.
69 * Return 1 if it is triggered by power key.
70 * Return 0 if it is not triggered by power key.
71 */
72static uint32_t is_pwrkey_pon_reason()
73{
Parth Dixit42dc2642015-06-19 16:48:22 +053074#if PMI_CONFIGURED
75 return target_is_pwrkey_pon_reason();
76#else
Matthew Qin82323d82014-02-11 16:28:50 +080077 uint8_t pon_reason = pm8x41_get_pon_reason();
78
79 if (pm8x41_get_is_cold_boot() && (pon_reason == KPDPWR_N))
80 return 1;
81 else
82 return 0;
Parth Dixit42dc2642015-06-19 16:48:22 +053083#endif
Matthew Qin82323d82014-02-11 16:28:50 +080084}
85
86/*
87 * Main timer handle: check every PWRKEY_DETECT_FREQUENCY to see
88 * if power key is pressed.
89 * Shutdown the device if power key is release before
90 * (PWRKEY_LONG_PRESS_COUNT/MPM_SLEEP_TIMETICK_COUNT) seconds.
91 */
92static enum handler_return long_press_pwrkey_timer_func(struct timer *p_timer,
93 void *arg)
94{
95 uint32_t sclk_count = platform_get_sclk_count();
96
97 /*
98 * The following condition is treated as the power key
99 * is pressed long enough.
100 * 1. if the power key is pressed last for PWRKEY_LONG_PRESS_COUNT.
101 */
102 if (sclk_count > PWRKEY_LONG_PRESS_COUNT) {
103 timer_cancel(p_timer);
104 pon_timer_complete = 1;
105 } else {
106 if (pm8x41_get_pwrkey_is_pressed()) {
107 /*
108 * For normal man response is > 0.1 secs, so we use 0.05 secs default
109 * for software to be safely detect if there is a key release action.
110 */
111 timer_set_oneshot(p_timer, PWRKEY_DETECT_FREQUENCY,
vijay kumar4f4405f2014-08-08 11:49:53 +0530112 (timer_callback)long_press_pwrkey_timer_func, NULL);
Matthew Qin82323d82014-02-11 16:28:50 +0800113 } else {
114 shutdown_device();
115 }
116 }
117
118 return INT_RESCHEDULE;
119}
120
121/*
122 * Function to wait until the power key is pressed long enough
123 */
124static void wait_for_long_pwrkey_pressed()
125{
126 uint32_t sclk_count = 0;
127
128 while (!pon_timer_complete) {
129 sclk_count = platform_get_sclk_count();
130 if (sclk_count > PWRKEY_LONG_PRESS_COUNT) {
131 timer_cancel(&pon_timer);
132 break;
133 }
c_wufeng637fd172015-09-21 18:53:56 +0800134 thread_sleep(1);
Matthew Qin82323d82014-02-11 16:28:50 +0800135 }
136}
137
138/*
139 * Function to support for shutdown detection
140 * If below condition is met, the function will shut down
141 * the device. Otherwise it will do nothing and return to
142 * normal boot.
143 * condition:
144 * 1. it is triggered by power key &&
145 * 2. the power key is released before
146 * (PWRKEY_LONG_PRESS_COUNT/MPM_SLEEP_TIMETICK_COUNT) seconds.
147 */
148void shutdown_detect()
149{
150 /*
151 * If it is booted by power key tirigger.
152 * Initialize pon_timer and call long_press_pwrkey_timer_func
153 * function to check if the power key is last press long enough.
154 */
c_wufeng637fd172015-09-21 18:53:56 +0800155 if (is_pwrkey_pon_reason()) {
156 if(!pm8x41_get_pwrkey_is_pressed()){
157 shutdown_device();
158 }
Matthew Qin82323d82014-02-11 16:28:50 +0800159 timer_initialize(&pon_timer);
vijay kumar4f4405f2014-08-08 11:49:53 +0530160 timer_set_oneshot(&pon_timer, 0,(timer_callback)long_press_pwrkey_timer_func, NULL);
Matthew Qin82323d82014-02-11 16:28:50 +0800161
162 /*
163 * Wait until long press power key timeout
164 *
165 * It will be confused to end users if we shutdown the device
166 * after the splash screen displayed. But it can be moved the
167 * wait here if the boot time is much more considered.
168 */
169 wait_for_long_pwrkey_pressed();
170 }
171}