b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * MARVELL CONFIDENTIAL |
| 5 | * Copyright 2012 Marvell International Ltd All Rights Reserved. |
| 6 | * The source code contained or described herein and all documents related to |
| 7 | * the source code ("Material") are owned by Marvell International Ltd or its |
| 8 | * suppliers or licensors. Title to the Material remains with Marvell International Ltd |
| 9 | * or its suppliers and licensors. The Material contains trade secrets and |
| 10 | * proprietary and confidential information of Marvell or its suppliers and |
| 11 | * licensors. The Material is protected by worldwide copyright and trade secret |
| 12 | * laws and treaty provisions. No part of the Material may be used, copied, |
| 13 | * reproduced, modified, published, uploaded, posted, transmitted, distributed, |
| 14 | * or disclosed in any way without Marvell's prior express written permission. |
| 15 | * |
| 16 | * No license under any patent, copyright, trade secret or other intellectual |
| 17 | * property right is granted to or conferred upon you by disclosure or delivery |
| 18 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 19 | * otherwise. Any license under such intellectual property rights must be |
| 20 | * express and approved by Marvell in writing. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #define LOG_TAG "acm_ach_elba" |
| 25 | #define LOG_NDEBUG 0 |
| 26 | |
| 27 | #include <cutils/log.h> |
| 28 | #include "acm_elba.h" |
| 29 | |
| 30 | int codec_reg_num = NUM_OF_REGS; |
| 31 | |
| 32 | static void Elba_Enable(void); |
| 33 | static void Elba_Disable(void); |
| 34 | static void Elba_Reset(void); |
| 35 | static void Elba_GetTypeAndID(int *type, unsigned char *id); |
| 36 | static ACH_ReturnCode Elba_Handle(void *setting); |
| 37 | |
| 38 | ACH_ComponentHandler elba_handler = { |
| 39 | COMPONENT_INACTIVE, |
| 40 | 0, |
| 41 | Elba_Enable, |
| 42 | Elba_Disable, |
| 43 | Elba_Reset, |
| 44 | Elba_GetTypeAndID, |
| 45 | Elba_Handle, |
| 46 | }; |
| 47 | |
| 48 | extern int I2CInit(void); |
| 49 | extern int I2CDeInit(void); |
| 50 | extern void I2CWrite(unsigned char numid, unsigned short value); |
| 51 | extern void I2CBurstWrite(unsigned char numid, unsigned char size, unsigned char *value); |
| 52 | extern void I2CRead(unsigned char numid, unsigned char *value); |
| 53 | extern void I2CBurstRead(unsigned char numid, unsigned char *value, int size, int offset); |
| 54 | |
| 55 | #ifdef PXA1826_AUDIO |
| 56 | #ifndef ACM_ALSA_INIT |
| 57 | #define ACM_ALSA_INIT |
| 58 | #endif |
| 59 | #endif |
| 60 | |
| 61 | #ifdef DEBUG_FAKE_ACH |
| 62 | #undef ACM_ALSA_INIT |
| 63 | #endif |
| 64 | |
| 65 | |
| 66 | static void Elba_RegSet(unsigned char numid, unsigned short value) { |
| 67 | #ifndef DEBUG_FAKE_ACH |
| 68 | I2CWrite(numid, value); |
| 69 | #else |
| 70 | LOGD(Elba_RegSet, " FAKE_ACH: %s(0x%x, 0x%x)", __FUNCTION__, numid, value); |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | static void Elba_RegBurstSet(unsigned char numid, unsigned char size, unsigned char *value) { |
| 75 | #ifndef DEBUG_FAKE_ACH |
| 76 | I2CBurstWrite(numid, size, value); |
| 77 | #else |
| 78 | LOGD(Elba_RegBurstSet, " FAKE_ACH: %s(0x%x, 0x%x, ..)", __FUNCTION__, numid, size); |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | void Elba_RegGet(unsigned char numid, unsigned char *value) { |
| 83 | #ifndef DEBUG_FAKE_ACH |
| 84 | I2CRead(numid, value); |
| 85 | #else |
| 86 | LOGD(Elba_RegGet, " FAKE_ACH: %s(0x%x, ..)", __FUNCTION__, numid); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | void Elba_RegBurstGet(unsigned char numid, unsigned char *value, int size, int offset) { |
| 91 | #ifndef DEBUG_FAKE_ACH |
| 92 | I2CBurstRead(numid, value, size, offset); |
| 93 | #else |
| 94 | LOGD(Elba_RegBurstGet, " FAKE_ACH: %s(0x%x, .., 0x%x, 0x%x)", __FUNCTION__, numid, size, offset); |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | static void Elba_ResetRegCache() { |
| 99 | int size = sizeof(reg_cache) / sizeof(reg_cache[0]); |
| 100 | int i = 0; |
| 101 | |
| 102 | for (i = 0; i < size; i++) { |
| 103 | reg_cache[i].reg_value = reg_cache[i].reg_default; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | unsigned char Elba_GetRegNumId(unsigned char reg_index) { |
| 108 | unsigned char i = 0; |
| 109 | |
| 110 | for (i = 0; i < NUM_OF_REGS; i++) { |
| 111 | if (reg_cache[i].reg_index == reg_index) { |
| 112 | return REG_NUMID_BASE + i; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | //-------------------------------------------------------------- |
| 120 | //-------- External ACH APIs |
| 121 | //-------------------------------------------------------------- |
| 122 | #ifdef ACM_ALSA_INIT |
| 123 | // NOTE about pair Enable/Disable |
| 124 | // On startup Disable could be called without Enable as Init/Reset sequence |
| 125 | // Let's "force" I2CInit() for first only Disable without Enable |
| 126 | // |
| 127 | static int _acm_initialized = 0; |
| 128 | #endif |
| 129 | |
| 130 | static void Elba_Enable(void) { |
| 131 | LOGI(Elba_Enable, "%s: enable elba component", __FUNCTION__); |
| 132 | |
| 133 | unsigned char elba_pu = 0; |
| 134 | unsigned int elba_max_boot_count = 100; |
| 135 | |
| 136 | #ifdef ACM_ALSA_INIT |
| 137 | LOGI(Elba_Enable, "%s: _acm_initialized=%d!", __FUNCTION__, _acm_initialized); |
| 138 | _acm_initialized = 1; |
| 139 | I2CInit(); //adaptor to alsa-mixer |
| 140 | #endif |
| 141 | reg_cache[ELBA_MAIN_POWERUP_ID].reg_value = 0x03; |
| 142 | Elba_RegSet(ELBA_MAIN_POWERUP_ID, 0x03); |
| 143 | Elba_RegGet(ELBA_AUTO_SEQUENCE_STS_1, &elba_pu); |
| 144 | while (!(elba_pu & (1 << ELBA_SEQ_PU_READY)) && elba_max_boot_count) { |
| 145 | usleep(1000); |
| 146 | Elba_RegGet(ELBA_AUTO_SEQUENCE_STS_1, &elba_pu); |
| 147 | elba_max_boot_count--; |
| 148 | } |
| 149 | |
| 150 | if (!elba_max_boot_count) { |
| 151 | LOGE(Elba_Enable1, "%s: power up elba failed", __FUNCTION__); |
| 152 | } else { |
| 153 | // Disable HP short autorecovery. |
| 154 | // Register 0x0A = 0x02. |
| 155 | reg_cache[ELBA_HP_SHRT_AR_ID].reg_value = 0x02; |
| 156 | Elba_RegSet(ELBA_HP_SHRT_AR_ID, 0x02); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void Elba_Disable(void) { |
| 161 | LOGI(Elba_Disable, "%s: disable elba component", __FUNCTION__); |
| 162 | |
| 163 | #ifdef ACM_ALSA_INIT |
| 164 | if (!_acm_initialized) { |
| 165 | _acm_initialized = 1; |
| 166 | I2CInit(); //adaptor to alsa-mixer |
| 167 | } |
| 168 | #endif |
| 169 | reg_cache[ELBA_MAIN_POWERUP_ID].reg_value = 0; |
| 170 | Elba_RegSet(ELBA_MAIN_POWERUP_ID, 0); |
| 171 | usleep(2000); |
| 172 | #ifdef ACM_ALSA_INIT |
| 173 | I2CDeInit(); //adaptor to alsa-mixer |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | static void Elba_Reset(void) { |
| 178 | LOGI(Elba_Reset, "%s: Reset elba registers!", __FUNCTION__); |
| 179 | int size = sizeof(reg_cache) / sizeof(reg_cache[0]); |
| 180 | int size_col = sizeof(reg_cache[0]) / sizeof(unsigned char); |
| 181 | int i, k = 0; |
| 182 | unsigned char j, numid, value, cache_idx; |
| 183 | Elba_RegisterCache reg_cache_inorder[size]; |
| 184 | |
| 185 | #ifdef ACM_ALSA_INIT |
| 186 | I2CInit(); //make sure to enable mixer before and close after this operation |
| 187 | #endif |
| 188 | for (j = 0x0; j <= 0xff; j++){ |
| 189 | if (k == size){ |
| 190 | LOGI(Elba_Reset1, "%s: copy completed !", __FUNCTION__); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | for (i = 0; i < size; i++) { |
| 195 | if (reg_cache[i].reg_prior == j) { |
| 196 | memcpy(®_cache_inorder[k], ®_cache[i], size_col); |
| 197 | k++; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | for (k = 0; k < size; k++) { |
| 203 | if (reg_cache_inorder[k].reg_prior == 0x0) { |
| 204 | continue; |
| 205 | } else { |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | for (; k < size; k++) { |
| 211 | numid = Elba_GetRegNumId(reg_cache_inorder[k].reg_index); |
| 212 | if (numid == 0 || numid == ELBA_COEFF_ID) { |
| 213 | LOGW(Elba_Reset2, "%s: Invalid or useless numid = %d!", __FUNCTION__, numid); |
| 214 | continue; |
| 215 | } |
| 216 | |
| 217 | if (numid == ELBA_MAIN_POWERUP_ID) { |
| 218 | value = 0x03; |
| 219 | } else { |
| 220 | value = reg_cache_inorder[k].reg_default; |
| 221 | } |
| 222 | |
| 223 | cache_idx = numid - REG_NUMID_BASE; |
| 224 | if (cache_idx < size) |
| 225 | reg_cache[cache_idx].reg_value = value; |
| 226 | Elba_RegSet(numid, value); |
| 227 | #ifdef DEBUG_ELBA |
| 228 | LOGI(Elba_Reset3, "%s: resets elba register 0x%lx to value --> 0x%lx", |
| 229 | __FUNCTION__, |
| 230 | reg_cache_inorder[k].reg_index, |
| 231 | value); |
| 232 | #endif |
| 233 | } |
| 234 | |
| 235 | Elba_RegSet(ELBA_MAIN_POWERUP_ID, 0x00); |
| 236 | #ifdef ACM_ALSA_INIT |
| 237 | I2CDeInit(); //make sure to enable mixer before and close after this operation |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | static void Elba_GetTypeAndID(int *type, unsigned char *id) { |
| 242 | #ifdef ACM_ALSA_INIT |
| 243 | I2CInit(); //make sure to enable mixer before and close after this operation |
| 244 | #endif |
| 245 | LOGI(Elba_GetTypeAndID, "%s: Get type and ID for elba component", __FUNCTION__); |
| 246 | *type = COMPONENT_TYPE_CODEC; |
| 247 | Elba_RegGet(ELBA_ID_REG, id); |
| 248 | #ifdef ACM_ALSA_INIT |
| 249 | I2CDeInit(); //make sure to enable mixer before and close after this operation |
| 250 | #endif |
| 251 | } |
| 252 | |
| 253 | static ACH_ReturnCode Elba_Handle(void *setting) { |
| 254 | ACH_ComponentParameter *param = (ACH_ComponentParameter *)setting; |
| 255 | unsigned short i = 0; |
| 256 | unsigned short numid, value, cache_idx; |
| 257 | |
| 258 | if (param && param->i2c.reg_value) { |
| 259 | numid = Elba_GetRegNumId(param->i2c.reg_index); |
| 260 | |
| 261 | LOGI(Elba_Handle, "%s: REG_NUMID_BASE=%d, numid=%d. set register 0x%lx values:0x%lx ", |
| 262 | __FUNCTION__, REG_NUMID_BASE, numid, param->i2c.reg_index, *(param->i2c.reg_value)); |
| 263 | |
| 264 | if (numid > 0) { |
| 265 | if (param->i2c.length == 2) { |
| 266 | //compare with register cache |
| 267 | cache_idx = numid - REG_NUMID_BASE; |
| 268 | value = (reg_cache[cache_idx].reg_value & ~param->i2c.reg_mask) | (*param->i2c.reg_value); |
| 269 | |
| 270 | if (((reg_cache[cache_idx].reg_value & param->i2c.reg_mask) != (*param->i2c.reg_value)) || |
| 271 | // For HP short start reset |
| 272 | ((reg_cache[cache_idx].reg_index) == 0x28)) |
| 273 | { |
| 274 | #ifdef DEBUG_ELBA |
| 275 | LOGI(Elba_Handle, "%s: cache_idx=%d, numid=%d. set register 0x%lx [0x%lx --> 0x%lx]", |
| 276 | __FUNCTION__, |
| 277 | cache_idx, |
| 278 | numid, |
| 279 | param->i2c.reg_index, |
| 280 | reg_cache[cache_idx].reg_value, |
| 281 | value); |
| 282 | #endif |
| 283 | reg_cache[cache_idx].reg_value = value; |
| 284 | //Elba_RegSet(numid, reg_cache[cache_idx].reg_value); |
| 285 | Elba_RegSet(numid, value); |
| 286 | } else { |
| 287 | #ifdef DEBUG_ELBA |
| 288 | LOGI(Elba_Handle1, "%s: set register 0x%lx [0x%lx --> 0x%lx] (unchanged)", |
| 289 | __FUNCTION__, |
| 290 | param->i2c.reg_index, |
| 291 | reg_cache[cache_idx].reg_value, |
| 292 | reg_cache[cache_idx].reg_value); |
| 293 | #endif |
| 294 | } |
| 295 | } else if (param->i2c.length > 2) { |
| 296 | //FIXME current no register cache is provided for mixer/eq configuration |
| 297 | #ifdef DEBUG_ELBA |
| 298 | LOGI(Elba_Handle2, "%s: numid=%d. set register 0x%lx values:", __FUNCTION__, numid, param->i2c.reg_index); |
| 299 | for (i = 0; i < param->i2c.length; i++) { |
| 300 | LOGI(Elba_Handle3, "%s: \t\t\t -->0x%lx", __FUNCTION__, param->i2c.reg_value[i]); |
| 301 | } |
| 302 | #endif |
| 303 | |
| 304 | Elba_RegBurstSet(numid, param->i2c.length, param->i2c.reg_value); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return ACH_RC_OK; |
| 310 | } |