|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +/* |
| 3 | + * Copyright(c) 2024 MediaTek. All rights reserved. |
| 4 | + * |
| 5 | + * Author: Andrew Perepech <andrew.perepech@mediatek.com> |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdint.h> |
| 9 | + |
| 10 | +#include <sof/drivers/afe-sgen.h> |
| 11 | +#include <sof/lib/io.h> |
| 12 | +#include <sof/lib/uuid.h> |
| 13 | +#include <sof/trace/trace.h> |
| 14 | + |
| 15 | +#include <mt8365-afe-regs.h> |
| 16 | +#include <mt8365-afe-common.h> |
| 17 | + |
| 18 | +SOF_DEFINE_REG_UUID(sgen_mt8365); |
| 19 | + |
| 20 | +DECLARE_TR_CTX(sgen_tr, SOF_UUID(sgen_mt8365_uuid), LOG_LEVEL_INFO); |
| 21 | + |
| 22 | +/* |
| 23 | + * Note: TEST_SGEN for test only |
| 24 | + * Define this TEST_SGEN to enable sine tone generator |
| 25 | + * then output data to audio memory interface(memif), |
| 26 | + * you can set TEST_SGEN_ID to choose output to which memif. |
| 27 | + * e.g. set TEST_SGEN as '1' and TEST_SGEN_ID as "MT8365_MEMIF_DL1", |
| 28 | + * the data source of DL2 will from sine generator. |
| 29 | + */ |
| 30 | +#define TEST_SGEN_ID MT8365_MEMIF_DL1 |
| 31 | +#define AUDIO_TML_PD_MASK 1 |
| 32 | +#define AUDIO_TML_PD_SHIFT 27 |
| 33 | + |
| 34 | +#define AFE_SGEN_FREQ_DIV_CH1_MASK 0x1f |
| 35 | +#define AFE_SGEN_FREQ_DIV_CH1_SHIFT 0 |
| 36 | +#define AFE_SGEN_FREQ_DIV_CH2_MASK 0x1f |
| 37 | +#define AFE_SGEN_FREQ_DIV_CH2_SHIFT 12 |
| 38 | +#define AFE_SGEN_AMP_DIV_CH1_MASK 0x7 |
| 39 | +#define AFE_SGEN_AMP_DIV_CH1_SHIFT 5 |
| 40 | +#define AFE_SGEN_AMP_DIV_CH2_MASK 0x7 |
| 41 | +#define AFE_SGEN_AMP_DIV_CH2_SHIFT 17 |
| 42 | +#define AFE_SGEN_MUTE_CH1_MASK 0x1 |
| 43 | +#define AFE_SGEN_MUTE_CH1_SHIFT 24 |
| 44 | +#define AFE_SGEN_MUTE_CH2_MASK 0x1 |
| 45 | +#define AFE_SGEN_MUTE_CH2_SHIFT 25 |
| 46 | +#define AFE_SGEN_ENABLE_MASK 0x1 |
| 47 | +#define AFE_SGEN_ENABLE_SHIFT 26 |
| 48 | + |
| 49 | +#define AFE_SGEN_TIMING_CH1_MASK 0x0f |
| 50 | +#define AFE_SGEN_TIMING_CH1_SHIFT 8 |
| 51 | +#define AFE_SGEN_TIMING_CH2_MASK 0x0f |
| 52 | +#define AFE_SGEN_TIMING_CH2_SHIFT 20 |
| 53 | + |
| 54 | +#define AFE_SINEGEN_LB_MODE_MSK 0x1f |
| 55 | +#define AFE_SINEGEN_LB_MODE_SHIFT 27 |
| 56 | + |
| 57 | +enum { |
| 58 | + MT8365_SGEN_AWB = (0x3 << 1) | 1, // o5 + o6 + SGEN to out |
| 59 | + MT8365_SGEN_VUL = (0x5 << 1) | 1, // o9 + o10 + SGEN to out |
| 60 | + MT8365_SGEN_DL1 = (0x3 << 1) | 0, // i5 + i6 + SGEN to in |
| 61 | + MT8365_SGEN_DL2 = (0x4 << 1) | 0 // i7 + i8 + SGEN to in |
| 62 | +}; |
| 63 | + |
| 64 | +/*sgen freq div*/ |
| 65 | +enum { |
| 66 | + SGEN_FREQ_64D1 = 1, |
| 67 | + SGEN_FREQ_64D2 = 2, |
| 68 | + SGEN_FREQ_64D3 = 3, |
| 69 | + SGEN_FREQ_64D4 = 4, |
| 70 | + SGEN_FREQ_64D5 = 5, |
| 71 | + SGEN_FREQ_64D6 = 6, |
| 72 | + SGEN_FREQ_64D7 = 7, |
| 73 | + SGEN_FREQ_64D8 = 8, |
| 74 | +}; |
| 75 | + |
| 76 | +/*sgen amp div*/ |
| 77 | +enum { |
| 78 | + SGEN_AMP_D1 = 0, |
| 79 | + SGEN_AMP_D2 = 1, |
| 80 | + SGEN_AMP_D4 = 2, |
| 81 | + SGEN_AMP_D8 = 3, |
| 82 | + SGEN_AMP_D16 = 4, |
| 83 | + SGEN_AMP_D32 = 5, |
| 84 | + SGEN_AMP_D64 = 6, |
| 85 | + SGEN_AMP_D128 = 7, |
| 86 | +}; |
| 87 | + |
| 88 | +enum { |
| 89 | + SGEN_CH_TIMING_8K = 0, |
| 90 | + SGEN_CH_TIMING_11P025K = 1, |
| 91 | + SGEN_CH_TIMING_12K = 2, |
| 92 | + SGEN_CH_TIMING_16K = 4, |
| 93 | + SGEN_CH_TIMING_22P05K = 5, |
| 94 | + SGEN_CH_TIMING_24K = 6, |
| 95 | + SGEN_CH_TIMING_32K = 8, |
| 96 | + SGEN_CH_TIMING_44P1K = 9, |
| 97 | + SGEN_CH_TIMING_48K = 10, |
| 98 | + SGEN_CH_TIMING_88P2K = 11, |
| 99 | + SGEN_CH_TIMING_96K = 12, |
| 100 | + SGEN_CH_TIMING_176P4K = 13, |
| 101 | + SGEN_CH_TIMING_192K = 14, |
| 102 | +}; |
| 103 | + |
| 104 | +static uint32_t mt8365_sinegen_timing(uint32_t rate) |
| 105 | +{ |
| 106 | + uint32_t sinegen_timing; |
| 107 | + |
| 108 | + switch (rate) { |
| 109 | + case 8000: |
| 110 | + sinegen_timing = SGEN_CH_TIMING_8K; |
| 111 | + break; |
| 112 | + case 11025: |
| 113 | + sinegen_timing = SGEN_CH_TIMING_11P025K; |
| 114 | + break; |
| 115 | + case 12000: |
| 116 | + sinegen_timing = SGEN_CH_TIMING_12K; |
| 117 | + break; |
| 118 | + case 16000: |
| 119 | + sinegen_timing = SGEN_CH_TIMING_16K; |
| 120 | + break; |
| 121 | + case 22050: |
| 122 | + sinegen_timing = SGEN_CH_TIMING_22P05K; |
| 123 | + break; |
| 124 | + case 24000: |
| 125 | + sinegen_timing = SGEN_CH_TIMING_24K; |
| 126 | + break; |
| 127 | + case 32000: |
| 128 | + sinegen_timing = SGEN_CH_TIMING_32K; |
| 129 | + break; |
| 130 | + case 44100: |
| 131 | + sinegen_timing = SGEN_CH_TIMING_44P1K; |
| 132 | + break; |
| 133 | + case 48000: |
| 134 | + sinegen_timing = SGEN_CH_TIMING_48K; |
| 135 | + break; |
| 136 | + case 88200: |
| 137 | + sinegen_timing = SGEN_CH_TIMING_88P2K; |
| 138 | + break; |
| 139 | + case 96000: |
| 140 | + sinegen_timing = SGEN_CH_TIMING_96K; |
| 141 | + break; |
| 142 | + case 176400: |
| 143 | + sinegen_timing = SGEN_CH_TIMING_176P4K; |
| 144 | + break; |
| 145 | + case 192000: |
| 146 | + sinegen_timing = SGEN_CH_TIMING_192K; |
| 147 | + break; |
| 148 | + default: |
| 149 | + sinegen_timing = SGEN_CH_TIMING_48K; |
| 150 | + tr_err(&sgen_tr, "invalid rate %d, set default 48k ", rate); |
| 151 | + } |
| 152 | + tr_dbg(&sgen_tr, "rate %d, sinegen_timing %d ", rate, sinegen_timing); |
| 153 | + return sinegen_timing; |
| 154 | +} |
| 155 | + |
| 156 | +static void mtk_afe_reg_update_bits(uint32_t addr_offset, uint32_t mask, uint32_t val, int shift) |
| 157 | +{ |
| 158 | + io_reg_update_bits(AFE_REG_BASE + addr_offset, mask << shift, val << shift); |
| 159 | +} |
| 160 | + |
| 161 | +static uint32_t mtk_afe_reg_read(uint32_t addr_offset) |
| 162 | +{ |
| 163 | + return io_reg_read(AFE_REG_BASE + addr_offset); |
| 164 | +} |
| 165 | + |
| 166 | +static void mt8365_afe_sinegen_enable(uint32_t sgen_id, uint32_t rate, int enable) |
| 167 | +{ |
| 168 | + uint32_t loopback_mode, reg_1, sinegen_timing; |
| 169 | + |
| 170 | + tr_dbg(&sgen_tr, "sgen_id %d, enable %d", sgen_id, enable); |
| 171 | + |
| 172 | + sinegen_timing = mt8365_sinegen_timing(rate); |
| 173 | + |
| 174 | + if (enable == 1) { |
| 175 | + /* set loopback mode */ |
| 176 | + switch (sgen_id) { |
| 177 | + case MT8365_MEMIF_AWB: |
| 178 | + loopback_mode = MT8365_SGEN_AWB; |
| 179 | + break; |
| 180 | + case MT8365_MEMIF_VUL: |
| 181 | + loopback_mode = MT8365_SGEN_VUL; |
| 182 | + break; |
| 183 | + case MT8365_MEMIF_DL1: |
| 184 | + loopback_mode = MT8365_SGEN_DL1; |
| 185 | + break; |
| 186 | + case MT8365_MEMIF_DL2: |
| 187 | + loopback_mode = MT8365_SGEN_DL2; |
| 188 | + break; |
| 189 | + default: |
| 190 | + tr_err(&sgen_tr, "invalid sgen_id %d", sgen_id); |
| 191 | + return; |
| 192 | + } |
| 193 | + /* enable sinegen clock*/ |
| 194 | + mtk_afe_reg_update_bits(AUDIO_TOP_CON0, AUDIO_TML_PD_MASK, 0, AUDIO_TML_PD_SHIFT); |
| 195 | + |
| 196 | + /*loopback source*/ |
| 197 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SINEGEN_LB_MODE_MSK, loopback_mode, |
| 198 | + AFE_SINEGEN_LB_MODE_SHIFT); |
| 199 | + |
| 200 | + /* sine gen timing*/ |
| 201 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SGEN_TIMING_CH1_MASK, |
| 202 | + sinegen_timing, AFE_SGEN_TIMING_CH1_SHIFT); |
| 203 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SGEN_TIMING_CH2_MASK, |
| 204 | + sinegen_timing, AFE_SGEN_TIMING_CH2_SHIFT); |
| 205 | + |
| 206 | + /*freq div*/ |
| 207 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SGEN_FREQ_DIV_CH1_MASK, |
| 208 | + SGEN_FREQ_64D1, AFE_SGEN_FREQ_DIV_CH1_SHIFT); |
| 209 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SGEN_FREQ_DIV_CH2_MASK, |
| 210 | + SGEN_FREQ_64D2, AFE_SGEN_FREQ_DIV_CH2_SHIFT); |
| 211 | + |
| 212 | + /*amp div*/ |
| 213 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SGEN_AMP_DIV_CH1_MASK, SGEN_AMP_D2, |
| 214 | + AFE_SGEN_AMP_DIV_CH1_SHIFT); |
| 215 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SGEN_AMP_DIV_CH2_MASK, SGEN_AMP_D2, |
| 216 | + AFE_SGEN_AMP_DIV_CH2_SHIFT); |
| 217 | + /* enable sgen*/ |
| 218 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SGEN_ENABLE_MASK, 1, |
| 219 | + AFE_SGEN_ENABLE_SHIFT); |
| 220 | + } else { |
| 221 | + /* disable sgen*/ |
| 222 | + mtk_afe_reg_update_bits(AFE_SGEN_CON0, AFE_SGEN_ENABLE_MASK, 0, |
| 223 | + AFE_SGEN_ENABLE_SHIFT); |
| 224 | + |
| 225 | + /* disable sgen clock */ |
| 226 | + mtk_afe_reg_update_bits(AUDIO_TOP_CON0, AUDIO_TML_PD_MASK, 1, AUDIO_TML_PD_SHIFT); |
| 227 | + } |
| 228 | + |
| 229 | + reg_1 = mtk_afe_reg_read(AFE_SGEN_CON0); |
| 230 | + tr_dbg(&sgen_tr, "AFE_SGEN_CON0 0x%x", reg_1); |
| 231 | +} |
| 232 | + |
| 233 | +void afe_sinegen_enable(void) |
| 234 | +{ |
| 235 | + mt8365_afe_sinegen_enable(TEST_SGEN_ID, 48000, 1); |
| 236 | +} |
| 237 | + |
| 238 | +void afe_sinegen_disable(void) |
| 239 | +{ |
| 240 | + mt8365_afe_sinegen_enable(TEST_SGEN_ID, 48000, 0); |
| 241 | +} |
0 commit comments