1 /*
2 // Copyright (C) 2022 Beken Corporation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <common/bk_include.h>
16 #include "adc_hal.h"
17 #include "power_driver.h"
18 #include "clock_driver.h"
19 #include "icu_driver.h"
20 #include "bk_sys_ctrl.h"
21 #include "bk_drv_model.h"
22
23 //TODO the adc_hal should reset the ADC => by Frank!!!
adc_hal_init(adc_hal_t * hal)24 bk_err_t adc_hal_init(adc_hal_t *hal)
25 {
26 hal->hw = (adc_hw_t *)ADC_LL_REG_BASE();
27
28 adc_ll_init(hal->hw);
29 adc_ll_wait_4_cycle(hal->hw);
30 adc_ll_disable_bypass_calib(hal->hw);
31 adc_hal_set_saturate_mode(hal, ADC_INIT_SATURATE_MODE);
32 return BK_OK;
33 }
34
adc_hal_deinit(adc_hal_t * hal)35 bk_err_t adc_hal_deinit(adc_hal_t *hal)
36 {
37 adc_ll_deinit(hal->hw);
38
39 return BK_OK;
40 }
41
adc_hal_set_clk(adc_hal_t * hal,adc_src_clk_t src_clk,uint32_t adc_clk)42 bk_err_t adc_hal_set_clk(adc_hal_t *hal, adc_src_clk_t src_clk, uint32_t adc_clk)
43 {
44 uint32_t pre_div = 0;
45
46 if (src_clk == ADC_SCLK_32M) {
47 adc_ll_enable_32m_clk(hal->hw);
48 pre_div = ADC_SRC_32M_CLK/2/adc_clk -1;
49 } else {
50 adc_ll_disable_32m_clk(hal->hw);
51 }
52
53 if (src_clk == ADC_SCLK_XTAL_26M) {
54 clk_set_saradc_clk_26m();
55 pre_div = ADC_SRC_26M_CLK/2/adc_clk -1;
56 } else if (src_clk == ADC_SCLK_DCO) {
57 clk_disable_saradc_audio_pll();
58 clk_set_saradc_clk_dco();
59 pre_div = ADC_SRC_DCO_CLK/2/adc_clk -1;
60 } else if (src_clk == ADC_SCLK_DPLL) {
61 clk_set_saradc_clk_dco();
62 clk_enable_saradc_audio_pll();
63 pre_div = ADC_SRC_DPLL_CLK/2/adc_clk -1;
64 }
65
66 adc_ll_set_pre_div(hal->hw, pre_div);
67
68 return BK_OK;
69 }
70
71 #if SARADC_AUTOTEST
adc_hal_set_div(adc_hal_t * hal,uint32_t div)72 bk_err_t adc_hal_set_div(adc_hal_t *hal, uint32_t div)
73 {
74 uint32_t pre_div = div;
75 adc_ll_disable_32m_clk(hal->hw);
76 clk_set_saradc_clk_26m();
77 adc_ll_set_pre_div(hal->hw, pre_div);
78 return BK_OK;
79 }
80 #endif
81
adc_hal_set_mode(adc_hal_t * hal,adc_mode_t adc_mode)82 bk_err_t adc_hal_set_mode(adc_hal_t *hal, adc_mode_t adc_mode)
83 {
84 if(adc_mode == ADC_SOFTWARE_CONTRL_MODE) {
85 adc_ll_set_software_control_mode(hal->hw);
86 } else if (adc_mode == ADC_CONTINUOUS_MODE) {
87 adc_ll_set_continuous_mode(hal->hw);
88 }else {
89 adc_ll_set_single_step_mode(hal->hw);
90 }
91
92 return BK_OK;
93 }
94
adc_hal_set_saturate_mode(adc_hal_t * hal,adc_saturate_mode_t mode)95 bk_err_t adc_hal_set_saturate_mode(adc_hal_t *hal, adc_saturate_mode_t mode)
96 {
97 uint32 saturate_mode = 0;
98
99 if (mode == ADC_SATURATE_MODE_NONE) {
100 adc_ll_disable_sat_process(hal->hw);
101 return BK_OK;
102 }
103
104 if (mode == ADC_SATURATE_MODE_0) {
105 saturate_mode = 0;
106 } else if (mode == ADC_SATURATE_MODE_1) {
107 saturate_mode = 1;
108 }
109 #if ADC_ACURACY_12_BIT
110 else if (mode == ADC_SATURATE_MODE_2) {
111 saturate_mode = 2;
112 }
113 #endif
114 else {
115 saturate_mode = 3;
116 }
117
118 adc_ll_set_sat_ctrl(hal->hw, saturate_mode);
119 adc_ll_enable_sat_process(hal->hw);
120
121 return BK_OK;
122 }
123
adc_hal_start_commom(adc_hal_t * hal)124 bk_err_t adc_hal_start_commom(adc_hal_t *hal)
125 {
126 adc_ll_enable(hal->hw);
127
128 return BK_OK;
129 }
130
adc_hal_stop_commom(adc_hal_t * hal)131 bk_err_t adc_hal_stop_commom(adc_hal_t *hal)
132 {
133 adc_ll_disable(hal->hw);
134
135 return BK_OK;
136 }
137
adc_hal_get_single_step_adc_data(adc_hal_t * hal)138 uint16_t adc_hal_get_single_step_adc_data(adc_hal_t *hal)
139 {
140 uint16_t value = 0;
141 uint32_t mode = 0;
142 mode = adc_hal_get_mode(hal);
143
144 BK_WHILE_DO(mode, mode = adc_hal_get_mode(hal));
145
146 value = adc_hal_get_adc_data(hal);
147
148 return value;
149 }
150
151