blob: 4100c5efae72876b28d9268eb0c9923667e03aa4 [file] [log] [blame]
Kiran Gunda11938522018-10-16 17:54:00 +05301/* Copyright (c) 2014-2016, 2018, The Linux Foundation. All rights reserved.
Matthew Qineded2232014-02-20 18:30:31 +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.
12 * * Neither the name of The Linux Foundation, Inc. nor the names of its
13 * 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
Matthew Qineded2232014-02-20 18:30:31 +080029#include <stdlib.h>
30#include <kernel/timer.h>
Matthew Qindbcfddd2015-06-10 21:27:43 +080031#include <kernel/thread.h>
Matthew Qineded2232014-02-20 18:30:31 +080032#include <pm_vib.h>
Matthew Qindbcfddd2015-06-10 21:27:43 +080033#include <vibrator.h>
Kiran Gunda11938522018-10-16 17:54:00 +053034#include <pm8x41.h>
35#include <platform.h>
36#include <smem.h>
37#include <target.h>
Matthew Qineded2232014-02-20 18:30:31 +080038
39#define CHECK_VIB_TIMER_FREQUENCY 50
40
c_wufenge4c12552015-12-15 15:38:55 +080041/*
42 * USE_VIB_THREAD is a micro that use a thread to turn off the vibrator,
43 * USE_VIB_THREAD should be define when use ldo to turn on/off vibrator.
44 * Note: define USE_VIB_THREAD will reduce the accuracy of vib time.
45 */
46#if !USE_VIB_THREAD
Matthew Qineded2232014-02-20 18:30:31 +080047static struct timer vib_timer;
c_wufenge4c12552015-12-15 15:38:55 +080048#else
49static uint32_t vib_time;
50#endif
51static uint32_t vib_timeout = 1;
Matthew Qineded2232014-02-20 18:30:31 +080052
53/* Function to turn on vibrator */
54void vib_turn_on()
55{
Kiran Gunda11938522018-10-16 17:54:00 +053056 uint32_t pmic = target_get_pmic();
57
58 if (pmic == PMIC_IS_PM8916)
59 pm8x41_vib_turn_on();
60 else
61 pm_vib_turn_on();
Matthew Qineded2232014-02-20 18:30:31 +080062}
63
64/* Function to turn off vibrator */
65void vib_turn_off()
66{
Kiran Gunda11938522018-10-16 17:54:00 +053067 uint32_t pmic = target_get_pmic();
68
69 if (pmic == PMIC_IS_PM8916)
70 pm8x41_vib_turn_off();
71 else
72 pm_vib_turn_off();
Matthew Qineded2232014-02-20 18:30:31 +080073}
74
c_wufenge4c12552015-12-15 15:38:55 +080075#if !USE_VIB_THREAD
Matthew Qineded2232014-02-20 18:30:31 +080076/* Function to turn off vibrator when the vib_timer is expired. */
Matthew Qindbcfddd2015-06-10 21:27:43 +080077static enum handler_return vib_timer_func(struct timer *v_timer, time_t t, void *arg)
Matthew Qineded2232014-02-20 18:30:31 +080078{
79 timer_cancel(&vib_timer);
c_wufeng4b41f272016-01-28 10:46:24 +080080 if(!vib_timeout){
81 vib_turn_off();
82 vib_timeout = 1;
83 }
Matthew Qineded2232014-02-20 18:30:31 +080084
85 return INT_RESCHEDULE;
86}
c_wufenge4c12552015-12-15 15:38:55 +080087#else
88int vibrator_thread(void *arg)
89{
90 while((--vib_time)){
91 thread_sleep(CHECK_VIB_TIMER_FREQUENCY);
92 }
c_wufeng4b41f272016-01-28 10:46:24 +080093 if(!vib_timeout){
94 vib_turn_off();
95 vib_timeout = 1;
96 }
c_wufenge4c12552015-12-15 15:38:55 +080097 return 0;
98}
99#endif
Matthew Qineded2232014-02-20 18:30:31 +0800100
101/*
102 * Function to turn on vibrator.
103 * vibrate_time - the time of phone vibrate.
104 */
105void vib_timed_turn_on(const uint32_t vibrate_time)
106{
Parth Dixit826f5752016-08-16 17:23:32 +0530107#if USE_VIB_THREAD
108 thread_t *thr;
109#endif
110
c_wufenge4c12552015-12-15 15:38:55 +0800111 if(!vib_timeout){
112 dprintf(CRITICAL,"vibrator already turn on\n");
113 return;
114 }
Matthew Qineded2232014-02-20 18:30:31 +0800115 vib_turn_on();
c_wufenge4c12552015-12-15 15:38:55 +0800116 vib_timeout = 0;
117#if !USE_VIB_THREAD
Matthew Qineded2232014-02-20 18:30:31 +0800118 timer_initialize(&vib_timer);
119 timer_set_oneshot(&vib_timer, vibrate_time, vib_timer_func, NULL);
c_wufenge4c12552015-12-15 15:38:55 +0800120#else
121 vib_time = (vibrate_time/CHECK_VIB_TIMER_FREQUENCY)+1;
Vivek Kumarf03b6c12017-10-10 21:34:16 +0530122 thr = thread_create("vibrator_thread", &vibrator_thread,
Parth Dixit826f5752016-08-16 17:23:32 +0530123 NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
124 if (!thr)
125 {
126 panic("failed to create vibrator thread\n");
127 }
128 thread_resume(thr);
129
c_wufenge4c12552015-12-15 15:38:55 +0800130#endif
Matthew Qineded2232014-02-20 18:30:31 +0800131}
132
133/* Wait for vibrator timer expired */
134void wait_vib_timeout(void)
135{
136 while (!vib_timeout) {
137 /* every 50ms to check if the vibrator timer is timeout*/
138 thread_sleep(CHECK_VIB_TIMER_FREQUENCY);
139 }
140}
c_wufeng4b41f272016-01-28 10:46:24 +0800141
142void turn_off_vib_early(void)
143{
144 if(vib_timeout) {
145 return;
146 }
147 vib_turn_off();
148 vib_timeout = 1;
149}