Initialise mutex before using it in as3525-codec.c, also use mutex for single codec register accesses.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19187 a1c6a512-1295-4272-9138-f99709370657
diff --git a/firmware/target/arm/as3525/as3525-codec.c b/firmware/target/arm/as3525/as3525-codec.c
index 86bce86..223b52d 100644
--- a/firmware/target/arm/as3525/as3525-codec.c
+++ b/firmware/target/arm/as3525/as3525-codec.c
@@ -75,6 +75,8 @@
     I2C2_SLAD0 = AS3514_I2C_ADDR << 1;
 
     I2C2_CNTRL = 0x51;
+    
+    mutex_init(&as_mtx);
 }
 
 
@@ -88,46 +90,64 @@
 /* returns 0 on success, <0 otherwise */
 int ascodec_write(unsigned int index, unsigned int value)
 {
-    if (index == 0x21) {
-        /* prevent setting of the LREG_CP_not bit */
-        value &= ~(1 << 5);
-    }
-    
+    int retval;
+
+    ascodec_lock();
+
     /* check if still busy */
     if (i2c_busy()) {
-        return -1;
+        retval = -1;
     }
-    
-    /* start transfer */
-    I2C2_SADDR = index;
-    I2C2_CNTRL &= ~(1 << 1);
-    I2C2_DATA = value;
-    I2C2_DACNT = 1;
-    
-    /* wait for transfer*/
-    while (i2c_busy());
+    else {
+        if (index == AS3514_CVDD_DCDC3) {
+            /* prevent setting of the LREG_CP_not bit */
+            value &= ~(1 << 5);
+        }
+        
+        /* start transfer */
+        I2C2_SADDR = index;
+        I2C2_CNTRL &= ~(1 << 1);
+        I2C2_DATA = value;
+        I2C2_DACNT = 1;
+        
+        /* wait for transfer*/
+        while (i2c_busy());
+        
+        retval = 0;
+    }
 
-    return 0;
+    ascodec_unlock();
+
+    return retval;
 }
 
 
 /* returns value read on success, <0 otherwise */
 int ascodec_read(unsigned int index)
 {
+    int data;
+
+    ascodec_lock();
+
     /* check if still busy */
     if (i2c_busy()) {
-        return -1;
+        data = -1;
+    }
+    else {
+        /* start transfer */
+        I2C2_SADDR = index;
+        I2C2_CNTRL |= (1 << 1);
+        I2C2_DACNT = 1;
+        
+        /* wait for transfer*/
+        while (i2c_busy());
+        
+        data = I2C2_DATA;
     }
     
-    /* start transfer */
-    I2C2_SADDR = index;
-    I2C2_CNTRL |= (1 << 1);
-    I2C2_DACNT = 1;
+    ascodec_unlock();
     
-    /* wait for transfer*/
-    while (i2c_busy());
-    
-    return I2C2_DATA;
+    return data;
 }
 
 int ascodec_readbytes(int index, int len, unsigned char *data)