ASR_BASE

Change-Id: Icf3719cc0afe3eeb3edc7fa80a2eb5199ca9dda1
diff --git a/marvell/services/audio/libacm/acm/src/acm_elba.c b/marvell/services/audio/libacm/acm/src/acm_elba.c
new file mode 100644
index 0000000..9290954
--- /dev/null
+++ b/marvell/services/audio/libacm/acm/src/acm_elba.c
@@ -0,0 +1,310 @@
+/*
+* All Rights Reserved
+*
+* MARVELL CONFIDENTIAL
+* Copyright 2012 Marvell International Ltd All Rights Reserved.
+* The source code contained or described herein and all documents related to
+* the source code ("Material") are owned by Marvell International Ltd or its
+* suppliers or licensors. Title to the Material remains with Marvell International Ltd
+* or its suppliers and licensors. The Material contains trade secrets and
+* proprietary and confidential information of Marvell or its suppliers and
+* licensors. The Material is protected by worldwide copyright and trade secret
+* laws and treaty provisions. No part of the Material may be used, copied,
+* reproduced, modified, published, uploaded, posted, transmitted, distributed,
+* or disclosed in any way without Marvell's prior express written permission.
+*
+* No license under any patent, copyright, trade secret or other intellectual
+* property right is granted to or conferred upon you by disclosure or delivery
+* of the Materials, either expressly, by implication, inducement, estoppel or
+* otherwise. Any license under such intellectual property rights must be
+* express and approved by Marvell in writing.
+*
+*/
+
+#define LOG_TAG "acm_ach_elba"
+#define LOG_NDEBUG 0
+
+#include <cutils/log.h>
+#include "acm_elba.h"
+
+int codec_reg_num = NUM_OF_REGS;
+
+static void Elba_Enable(void);
+static void Elba_Disable(void);
+static void Elba_Reset(void);
+static void Elba_GetTypeAndID(int *type, unsigned char *id);
+static ACH_ReturnCode Elba_Handle(void *setting);
+
+ACH_ComponentHandler elba_handler = {
+    COMPONENT_INACTIVE,
+    0,
+    Elba_Enable,
+    Elba_Disable,
+    Elba_Reset,
+    Elba_GetTypeAndID,
+    Elba_Handle,
+};
+
+extern int I2CInit(void);
+extern int I2CDeInit(void);
+extern void I2CWrite(unsigned char numid, unsigned short value);
+extern void I2CBurstWrite(unsigned char numid, unsigned char size, unsigned char *value);
+extern void I2CRead(unsigned char numid, unsigned char *value);
+extern void I2CBurstRead(unsigned char numid, unsigned char *value, int size, int offset);
+
+#ifdef PXA1826_AUDIO
+#ifndef ACM_ALSA_INIT
+#define ACM_ALSA_INIT
+#endif
+#endif
+
+#ifdef DEBUG_FAKE_ACH
+#undef ACM_ALSA_INIT
+#endif
+
+
+static void Elba_RegSet(unsigned char numid, unsigned short value) {
+#ifndef DEBUG_FAKE_ACH
+    I2CWrite(numid, value);
+#else
+	LOGD(Elba_RegSet, "  FAKE_ACH: %s(0x%x, 0x%x)", __FUNCTION__, numid, value);
+#endif
+}
+
+static void Elba_RegBurstSet(unsigned char numid, unsigned char size, unsigned char *value) {
+#ifndef DEBUG_FAKE_ACH
+    I2CBurstWrite(numid, size, value);
+#else
+	LOGD(Elba_RegBurstSet, "  FAKE_ACH: %s(0x%x, 0x%x, ..)", __FUNCTION__, numid, size);
+#endif
+}
+
+void Elba_RegGet(unsigned char numid, unsigned char *value) {
+#ifndef DEBUG_FAKE_ACH
+    I2CRead(numid, value);
+#else
+	LOGD(Elba_RegGet, "  FAKE_ACH: %s(0x%x, ..)", __FUNCTION__, numid);
+#endif
+}
+
+void Elba_RegBurstGet(unsigned char numid, unsigned char *value, int size, int offset) {
+#ifndef DEBUG_FAKE_ACH
+    I2CBurstRead(numid, value, size, offset);
+#else
+	LOGD(Elba_RegBurstGet, "  FAKE_ACH: %s(0x%x, .., 0x%x, 0x%x)", __FUNCTION__, numid, size, offset);
+#endif
+}
+
+static void Elba_ResetRegCache() {
+    int size = sizeof(reg_cache) / sizeof(reg_cache[0]);
+    int i = 0;
+
+    for (i = 0; i < size; i++) {
+        reg_cache[i].reg_value = reg_cache[i].reg_default;
+    }
+}
+
+unsigned char Elba_GetRegNumId(unsigned char reg_index) {
+    unsigned char i = 0;
+
+    for (i = 0; i < NUM_OF_REGS; i++) {
+        if (reg_cache[i].reg_index == reg_index) {
+            return REG_NUMID_BASE + i;
+        }
+    }
+
+    return 0;
+}
+
+//--------------------------------------------------------------
+//-------- External ACH APIs
+//--------------------------------------------------------------
+#ifdef ACM_ALSA_INIT
+// NOTE about pair Enable/Disable
+//  On startup Disable could be called without Enable as Init/Reset sequence
+//  Let's "force" I2CInit() for first only Disable without Enable
+//
+static int _acm_initialized = 0;
+#endif
+
+static void Elba_Enable(void) {
+    LOGI(Elba_Enable, "%s: enable elba component", __FUNCTION__);
+
+    unsigned char elba_pu = 0;
+    unsigned int elba_max_boot_count = 100;
+
+#ifdef ACM_ALSA_INIT
+    LOGI(Elba_Enable, "%s: _acm_initialized=%d!", __FUNCTION__, _acm_initialized);
+	_acm_initialized = 1;
+	I2CInit(); //adaptor to alsa-mixer
+#endif
+    reg_cache[ELBA_MAIN_POWERUP_ID].reg_value = 0x03;
+    Elba_RegSet(ELBA_MAIN_POWERUP_ID, 0x03);
+    Elba_RegGet(ELBA_AUTO_SEQUENCE_STS_1, &elba_pu);
+    while (!(elba_pu & (1 << ELBA_SEQ_PU_READY)) && elba_max_boot_count) {
+        usleep(1000);
+        Elba_RegGet(ELBA_AUTO_SEQUENCE_STS_1, &elba_pu);
+        elba_max_boot_count--;
+    }
+
+    if (!elba_max_boot_count) {
+        LOGE(Elba_Enable1, "%s: power up elba failed", __FUNCTION__);
+    } else {
+        // Disable HP short autorecovery.
+        // Register 0x0A = 0x02.
+        reg_cache[ELBA_HP_SHRT_AR_ID].reg_value = 0x02;
+        Elba_RegSet(ELBA_HP_SHRT_AR_ID, 0x02);
+    }
+}
+
+static void Elba_Disable(void) {
+    LOGI(Elba_Disable, "%s: disable elba component", __FUNCTION__);
+
+#ifdef ACM_ALSA_INIT
+	if (!_acm_initialized) {
+		_acm_initialized = 1;
+		I2CInit(); //adaptor to alsa-mixer
+	}
+#endif
+    reg_cache[ELBA_MAIN_POWERUP_ID].reg_value = 0;
+    Elba_RegSet(ELBA_MAIN_POWERUP_ID, 0);
+    usleep(2000);
+#ifdef ACM_ALSA_INIT
+	I2CDeInit(); //adaptor to alsa-mixer
+#endif
+}
+
+static void Elba_Reset(void) {
+    LOGI(Elba_Reset, "%s: Reset elba registers!", __FUNCTION__);
+    int size = sizeof(reg_cache) / sizeof(reg_cache[0]);
+    int size_col = sizeof(reg_cache[0]) / sizeof(unsigned char);
+    int i, k = 0;
+    unsigned char j, numid, value, cache_idx;
+    Elba_RegisterCache reg_cache_inorder[size];
+
+#ifdef ACM_ALSA_INIT
+	I2CInit(); //make sure to enable mixer before and close after this operation
+#endif
+    for (j = 0x0; j <= 0xff; j++){
+        if (k == size){
+            LOGI(Elba_Reset1, "%s: copy completed !", __FUNCTION__);
+            break;
+        }
+
+        for (i = 0; i < size; i++) {
+            if (reg_cache[i].reg_prior == j) {
+                memcpy(&reg_cache_inorder[k], &reg_cache[i], size_col);
+                k++;
+            }
+        }
+    }
+
+    for (k = 0; k < size; k++) {
+        if (reg_cache_inorder[k].reg_prior == 0x0) {
+            continue;
+        } else {
+            break;
+        }
+    }
+
+    for (; k < size; k++) {
+        numid = Elba_GetRegNumId(reg_cache_inorder[k].reg_index);
+        if (numid == 0 || numid == ELBA_COEFF_ID) {
+            LOGW(Elba_Reset2, "%s: Invalid or useless numid = %d!", __FUNCTION__, numid);
+            continue;
+        }
+
+        if (numid == ELBA_MAIN_POWERUP_ID) {
+            value = 0x03;
+        } else {
+            value = reg_cache_inorder[k].reg_default;
+        }
+
+        cache_idx = numid - REG_NUMID_BASE;
+        if (cache_idx < size)
+            reg_cache[cache_idx].reg_value = value;
+        Elba_RegSet(numid, value);
+#ifdef DEBUG_ELBA
+        LOGI(Elba_Reset3, "%s: resets elba register 0x%lx to value --> 0x%lx",
+            __FUNCTION__,
+            reg_cache_inorder[k].reg_index,
+            value);
+#endif
+    }
+
+    Elba_RegSet(ELBA_MAIN_POWERUP_ID, 0x00);
+#ifdef ACM_ALSA_INIT
+	I2CDeInit(); //make sure to enable mixer before and close after this operation
+#endif
+}
+
+static void Elba_GetTypeAndID(int *type, unsigned char *id) {
+#ifdef ACM_ALSA_INIT
+	I2CInit(); //make sure to enable mixer before and close after this operation
+#endif
+    LOGI(Elba_GetTypeAndID, "%s: Get type and ID for elba component", __FUNCTION__);
+    *type = COMPONENT_TYPE_CODEC;
+    Elba_RegGet(ELBA_ID_REG, id);
+#ifdef ACM_ALSA_INIT
+	I2CDeInit(); //make sure to enable mixer before and close after this operation
+#endif
+}
+
+static ACH_ReturnCode Elba_Handle(void *setting) {
+    ACH_ComponentParameter *param = (ACH_ComponentParameter *)setting;
+    unsigned short i = 0;
+    unsigned short numid, value, cache_idx;
+
+    if (param && param->i2c.reg_value) {
+        numid = Elba_GetRegNumId(param->i2c.reg_index);
+
+        LOGI(Elba_Handle, "%s: REG_NUMID_BASE=%d, numid=%d. set register 0x%lx values:0x%lx ",
+                    __FUNCTION__, REG_NUMID_BASE, numid, param->i2c.reg_index,  *(param->i2c.reg_value));
+
+        if (numid > 0) {
+            if (param->i2c.length == 2) {
+                //compare with register cache
+                cache_idx = numid - REG_NUMID_BASE;
+                value = (reg_cache[cache_idx].reg_value & ~param->i2c.reg_mask) | (*param->i2c.reg_value);
+
+                if (((reg_cache[cache_idx].reg_value & param->i2c.reg_mask) != (*param->i2c.reg_value)) ||
+                    // For HP short start reset
+                    ((reg_cache[cache_idx].reg_index) == 0x28))
+                {
+#ifdef DEBUG_ELBA
+                    LOGI(Elba_Handle, "%s: cache_idx=%d, numid=%d. set register 0x%lx [0x%lx --> 0x%lx]",
+                        __FUNCTION__,
+                        cache_idx,
+                        numid,
+                        param->i2c.reg_index,
+                        reg_cache[cache_idx].reg_value,
+                        value);
+#endif
+                    reg_cache[cache_idx].reg_value = value;
+                    //Elba_RegSet(numid, reg_cache[cache_idx].reg_value);
+                    Elba_RegSet(numid, value);
+                } else {
+#ifdef DEBUG_ELBA
+                    LOGI(Elba_Handle1, "%s: set register 0x%lx [0x%lx --> 0x%lx] (unchanged)",
+                        __FUNCTION__,
+                        param->i2c.reg_index,
+                        reg_cache[cache_idx].reg_value,
+                        reg_cache[cache_idx].reg_value);
+#endif
+                }
+            } else if (param->i2c.length > 2) {
+                //FIXME current no register cache is provided for mixer/eq configuration
+#ifdef DEBUG_ELBA
+                LOGI(Elba_Handle2, "%s: numid=%d. set register 0x%lx values:", __FUNCTION__, numid, param->i2c.reg_index);
+                for (i = 0; i < param->i2c.length; i++) {
+                    LOGI(Elba_Handle3, "%s: \t\t\t    -->0x%lx", __FUNCTION__, param->i2c.reg_value[i]);
+                }
+#endif
+
+                Elba_RegBurstSet(numid, param->i2c.length, param->i2c.reg_value);
+            }
+        }
+    }
+
+    return ACH_RC_OK;
+}