ASR_BASE

Change-Id: Icf3719cc0afe3eeb3edc7fa80a2eb5199ca9dda1
diff --git a/marvell/services/audio/libacm/acm/src/acm_ustica.c b/marvell/services/audio/libacm/acm/src/acm_ustica.c
new file mode 100644
index 0000000..37170ac
--- /dev/null
+++ b/marvell/services/audio/libacm/acm/src/acm_ustica.c
@@ -0,0 +1,228 @@
+/*
+* 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_ustica"
+#define LOG_NDEBUG 0
+
+#include <cutils/log.h>
+#include "acm_ustica.h"
+
+#ifdef PXA1826_AUDIO
+#ifndef ACM_ALSA_INIT
+#define ACM_ALSA_INIT
+#endif
+#endif
+
+extern int codec_reg_num;
+
+static void Ustica_Enable(void);
+static void Ustica_Disable(void);
+static void Ustica_Reset(void);
+static void Ustica_GetTypeAndID(int *type, unsigned char *id);
+static ACH_ReturnCode Ustica_Handle(void *setting);
+
+ACH_ComponentHandler ustica_handler = {
+    COMPONENT_INACTIVE,
+    0,
+    Ustica_Enable,
+    Ustica_Disable,
+    Ustica_Reset,
+    Ustica_GetTypeAndID,
+    Ustica_Handle,
+};
+
+extern int I2CInit(void);
+extern int I2CDeInit(void);
+extern void I2CWrite(unsigned char numid, unsigned short value);
+extern void I2CRead(unsigned char numid, unsigned char *value);
+
+static void Ustica_RegSet(unsigned char numid, unsigned char value) {
+#ifndef DEBUG_FAKE_ACH
+    I2CWrite(numid, value);
+#else
+	LOGD(Ustica_RegSet, "  FAKE_ACH: %s(0x%x, 0x%x)", __FUNCTION__, numid, value);
+#endif
+}
+
+void Ustica_RegGet(unsigned char numid, unsigned char *value) {
+#ifndef DEBUG_FAKE_ACH
+    I2CRead(numid, value);
+#else
+	LOGD(Ustica_RegGet, "  FAKE_ACH: %s(0x%x, ..)", __FUNCTION__, numid);
+#endif
+}
+
+static void Ustica_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 Ustica_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 codec_reg_num + 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
+//  Skip DeInit for first Disable without Enable
+//
+static int _acm_initialized;
+#endif
+
+static void Ustica_Enable(void) {
+    LOGI(Ustica_Enable, "%s: enable ustica component", __FUNCTION__);
+#ifdef ACM_ALSA_INIT
+	_acm_initialized = 1;
+	I2CInit(); //adaptor to alsa-mixer
+#endif
+}
+
+static void Ustica_Disable(void) {
+    LOGI(Ustica_Disable, "%s: disable ustica component", __FUNCTION__);
+#ifdef ACM_ALSA_INIT
+	if (_acm_initialized) {
+		_acm_initialized = 1;
+		I2CDeInit(); //adaptor to alsa-mixer
+	}
+#endif
+}
+
+static void Ustica_Reset(void) {
+    LOGI(Ustica_Reset, "%s: Reset ustica 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;
+    Ustica_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(Ustica_Reset0, "%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 = Ustica_GetRegNumId(reg_cache_inorder[k].reg_index);
+        if (numid == 0) {
+            LOGW(Ustica_Reset1, "%s: Invalid numid = %d!", __FUNCTION__, numid);
+            continue;
+        }
+
+        value = reg_cache_inorder[k].reg_default;
+        cache_idx = numid - codec_reg_num;
+        if (cache_idx < size)
+            reg_cache[cache_idx].reg_value = value;
+        Ustica_RegSet(numid, value);
+#ifdef DEBUG_USTICA
+        LOGI(Ustica_Reset2, "%s: resets ustica register 0x%lx to value --> 0x%lx",
+            __FUNCTION__,
+            reg_cache_inorder[k].reg_index,
+            value);
+#endif
+    }
+#ifdef ACM_ALSA_INIT
+	I2CDeInit(); //make sure to enable mixer before and close after this operation
+#endif
+}
+
+static void Ustica_GetTypeAndID(int *type, unsigned char *id){
+    LOGI(Ustica_GetTypeAndID, "%s: Get type and ID for ustica component", __FUNCTION__);
+    *type = COMPONENT_TYPE_POWER;
+#ifdef ACM_ALSA_INIT
+	I2CInit(); //make sure to enable mixer before and close after this operation
+#endif
+    Ustica_RegGet(USTICA_ID_REG, id);
+#ifdef ACM_ALSA_INIT
+	I2CDeInit(); //make sure to enable mixer before and close after this operation
+#endif
+}
+
+static ACH_ReturnCode Ustica_Handle(void *setting) {
+    ACH_ComponentParameter *param = (ACH_ComponentParameter *)setting;
+    unsigned char numid, value, cache_idx;
+
+    if (param && param->i2c.reg_value) {
+        numid = Ustica_GetRegNumId(param->i2c.reg_index);
+        if (numid > 0) {
+            //compare with register cache
+            cache_idx = numid - codec_reg_num;
+            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)) {
+#ifdef DEBUG_USTICA
+                LOGI(Ustica_Handle, "%s: set register 0x%lx [0x%lx --> 0x%lx]",
+                    __FUNCTION__,
+                    param->i2c.reg_index,
+                    reg_cache[cache_idx].reg_value,
+                    value);
+#endif
+
+                reg_cache[cache_idx].reg_value = value;
+                Ustica_RegSet(numid, reg_cache[cache_idx].reg_value);
+            } else {
+#ifdef DEBUG_USTICA
+                LOGI(Ustica_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
+            }
+        }
+    }
+
+    return ACH_RC_OK;
+}