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