Kiran Gunda | 1193852 | 2018-10-16 17:54:00 +0530 | [diff] [blame^] | 1 | /* Copyright (c) 2014-2016, 2018, The Linux Foundation. All rights reserved. |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 2 | * |
| 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 Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <kernel/timer.h> |
Matthew Qin | dbcfddd | 2015-06-10 21:27:43 +0800 | [diff] [blame] | 31 | #include <kernel/thread.h> |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 32 | #include <pm_vib.h> |
Matthew Qin | dbcfddd | 2015-06-10 21:27:43 +0800 | [diff] [blame] | 33 | #include <vibrator.h> |
Kiran Gunda | 1193852 | 2018-10-16 17:54:00 +0530 | [diff] [blame^] | 34 | #include <pm8x41.h> |
| 35 | #include <platform.h> |
| 36 | #include <smem.h> |
| 37 | #include <target.h> |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 38 | |
| 39 | #define CHECK_VIB_TIMER_FREQUENCY 50 |
| 40 | |
c_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 41 | /* |
| 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 Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 47 | static struct timer vib_timer; |
c_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 48 | #else |
| 49 | static uint32_t vib_time; |
| 50 | #endif |
| 51 | static uint32_t vib_timeout = 1; |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 52 | |
| 53 | /* Function to turn on vibrator */ |
| 54 | void vib_turn_on() |
| 55 | { |
Kiran Gunda | 1193852 | 2018-10-16 17:54:00 +0530 | [diff] [blame^] | 56 | 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 Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* Function to turn off vibrator */ |
| 65 | void vib_turn_off() |
| 66 | { |
Kiran Gunda | 1193852 | 2018-10-16 17:54:00 +0530 | [diff] [blame^] | 67 | 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 Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 73 | } |
| 74 | |
c_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 75 | #if !USE_VIB_THREAD |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 76 | /* Function to turn off vibrator when the vib_timer is expired. */ |
Matthew Qin | dbcfddd | 2015-06-10 21:27:43 +0800 | [diff] [blame] | 77 | static enum handler_return vib_timer_func(struct timer *v_timer, time_t t, void *arg) |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 78 | { |
| 79 | timer_cancel(&vib_timer); |
c_wufeng | 4b41f27 | 2016-01-28 10:46:24 +0800 | [diff] [blame] | 80 | if(!vib_timeout){ |
| 81 | vib_turn_off(); |
| 82 | vib_timeout = 1; |
| 83 | } |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 84 | |
| 85 | return INT_RESCHEDULE; |
| 86 | } |
c_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 87 | #else |
| 88 | int vibrator_thread(void *arg) |
| 89 | { |
| 90 | while((--vib_time)){ |
| 91 | thread_sleep(CHECK_VIB_TIMER_FREQUENCY); |
| 92 | } |
c_wufeng | 4b41f27 | 2016-01-28 10:46:24 +0800 | [diff] [blame] | 93 | if(!vib_timeout){ |
| 94 | vib_turn_off(); |
| 95 | vib_timeout = 1; |
| 96 | } |
c_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | #endif |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * Function to turn on vibrator. |
| 103 | * vibrate_time - the time of phone vibrate. |
| 104 | */ |
| 105 | void vib_timed_turn_on(const uint32_t vibrate_time) |
| 106 | { |
Parth Dixit | 826f575 | 2016-08-16 17:23:32 +0530 | [diff] [blame] | 107 | #if USE_VIB_THREAD |
| 108 | thread_t *thr; |
| 109 | #endif |
| 110 | |
c_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 111 | if(!vib_timeout){ |
| 112 | dprintf(CRITICAL,"vibrator already turn on\n"); |
| 113 | return; |
| 114 | } |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 115 | vib_turn_on(); |
c_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 116 | vib_timeout = 0; |
| 117 | #if !USE_VIB_THREAD |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 118 | timer_initialize(&vib_timer); |
| 119 | timer_set_oneshot(&vib_timer, vibrate_time, vib_timer_func, NULL); |
c_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 120 | #else |
| 121 | vib_time = (vibrate_time/CHECK_VIB_TIMER_FREQUENCY)+1; |
Vivek Kumar | f03b6c1 | 2017-10-10 21:34:16 +0530 | [diff] [blame] | 122 | thr = thread_create("vibrator_thread", &vibrator_thread, |
Parth Dixit | 826f575 | 2016-08-16 17:23:32 +0530 | [diff] [blame] | 123 | 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_wufeng | e4c1255 | 2015-12-15 15:38:55 +0800 | [diff] [blame] | 130 | #endif |
Matthew Qin | eded223 | 2014-02-20 18:30:31 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* Wait for vibrator timer expired */ |
| 134 | void 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_wufeng | 4b41f27 | 2016-01-28 10:46:24 +0800 | [diff] [blame] | 141 | |
| 142 | void turn_off_vib_early(void) |
| 143 | { |
| 144 | if(vib_timeout) { |
| 145 | return; |
| 146 | } |
| 147 | vib_turn_off(); |
| 148 | vib_timeout = 1; |
| 149 | } |