Sansa AMS: use the AS3514 ADC driver
Move the ADC defines to as3514.h, and make adc-target.h only include as3514.h
Implement the missing API (ascodec_readbytes, ascodec_(un)lock)

Revert the changes to the PP-specific arm/ascodec-target.h in r19073,
use a AS3525 specific ascodec-target.h while moving the AS3514 specific
code in export/as3514.h

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19076 a1c6a512-1295-4272-9138-f99709370657
diff --git a/firmware/SOURCES b/firmware/SOURCES
index ff75a6c..0065749 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -339,7 +339,7 @@
 target/arm/as3525/power-as3525.c
 target/arm/as3525/usb-as3525.c
 #ifndef BOOTLOADER
-target/arm/as3525/adc-as3525.c
+target/arm/adc-as3514.c
 target/arm/as3525/pcm-as3525.c
 target/arm/as3525/audio-as3525.c
 target/arm/as3525/debug-as3525.c
diff --git a/firmware/export/as3514.h b/firmware/export/as3514.h
index 2bbdc2e..f3c5c61 100644
--- a/firmware/export/as3514.h
+++ b/firmware/export/as3514.h
@@ -22,6 +22,7 @@
 #ifndef _AS3514_H
 #define _AS3514_H
 
+#include "config.h"
 #include <stdbool.h>
 
 extern int tenthdb2master(int db);
@@ -80,4 +81,30 @@
 #define VOLUME_MIN -735
 #define VOLUME_MAX   60
 
+/* ADC channels */
+#define NUM_ADC_CHANNELS 13
+
+#define ADC_BVDD         0  /* Battery voltage of 4V LiIo accumulator */
+#define ADC_RTCSUP       1  /* RTC backup battery voltage */
+#define ADC_UVDD         2  /* USB host voltage */
+#define ADC_CHG_IN       3  /* Charger input voltage */
+#define ADC_CVDD         4  /* Charger pump output voltage */
+#define ADC_BATTEMP      5  /* Battery charging temperature */
+#define ADC_MICSUP1      6  /* Voltage on MicSup1 for remote control
+                               or external voltage measurement */
+#define ADC_MICSUP2      7  /* Voltage on MicSup1 for remote control
+                               or external voltage measurement */
+#define ADC_VBE1         8  /* Measuring junction temperature @ 2uA */
+#define ADC_VBE2         9  /* Measuring junction temperature @ 1uA */
+#define ADC_I_MICSUP1    10 /* Current of MicSup1 for remote control detection */
+#define ADC_I_MICSUP2    11 /* Current of MicSup2 for remote control detection */
+#define ADC_VBAT         12 /* Single cell battery voltage */
+
+#if defined(SANSA_E200) || defined(SANSA_C200) || defined(PHILIPS_SA9200) \
+    || CONFIG_CPU == AS3525
+#define AS3514_I2C_ADDR 0x46
+#else
+#error Unknown target!
+#endif
+
 #endif /* _AS3514_H */
diff --git a/firmware/target/arm/as3525/adc-as3525.c b/firmware/target/arm/as3525/adc-target.h
similarity index 83%
rename from firmware/target/arm/as3525/adc-as3525.c
rename to firmware/target/arm/as3525/adc-target.h
index 6e1aac5..007a11a 100644
--- a/firmware/target/arm/as3525/adc-as3525.c
+++ b/firmware/target/arm/as3525/adc-target.h
@@ -7,7 +7,7 @@
  *                     \/            \/     \/    \/            \/
  * $Id$
  *
- * Copyright © 2008 Rafaël Carré
+ * Copyright (C) 2006 by Barry Wardell
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -18,16 +18,10 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
+#ifndef _ADC_TARGET_H_
+#define _ADC_TARGET_H_
 
-#include "adc.h"
+/* The ADC sources and channels are common to all targets with AS3514 */
+#include "as3514.h"
 
-/* TODO */
-
-unsigned short adc_read(int channel)
-{
-    return 0;
-}
-
-void adc_init(void)
-{
-}
+#endif
diff --git a/firmware/target/arm/as3525/as3525-codec.c b/firmware/target/arm/as3525/as3525-codec.c
index 4d8598e..86bce86 100644
--- a/firmware/target/arm/as3525/as3525-codec.c
+++ b/firmware/target/arm/as3525/as3525-codec.c
@@ -38,6 +38,7 @@
  */
 
 #include "ascodec-target.h"
+#include "kernel.h"
 #include "as3525.h"
 
 #define I2C2_DATA       *((volatile unsigned int *)(I2C_AUDIO_BASE + 0x00))
@@ -53,6 +54,7 @@
 #define I2C2_INT_CLR    *((volatile unsigned int *)(I2C_AUDIO_BASE + 0x40))
 #define I2C2_SADDR      *((volatile unsigned int *)(I2C_AUDIO_BASE + 0x44))
 
+static struct mutex as_mtx SHAREDBSS_ATTR;
 
 /* initialises the internal i2c bus and prepares for transfers to the codec */
 void ascodec_init(void)
@@ -84,7 +86,7 @@
 
 
 /* returns 0 on success, <0 otherwise */
-int ascodec_write(int index, int value)
+int ascodec_write(unsigned int index, unsigned int value)
 {
     if (index == 0x21) {
         /* prevent setting of the LREG_CP_not bit */
@@ -110,7 +112,7 @@
 
 
 /* returns value read on success, <0 otherwise */
-int ascodec_read(int index)
+int ascodec_read(unsigned int index)
 {
     /* check if still busy */
     if (i2c_busy()) {
@@ -128,3 +130,32 @@
     return I2C2_DATA;
 }
 
+int ascodec_readbytes(int index, int len, unsigned char *data)
+{
+    int i;
+
+    ascodec_lock();
+
+    for(i=0; i<len; i++)
+    {
+        int temp = ascodec_read(index+i);
+        if(temp == -1)
+            break;
+        else
+            data[i] = temp;
+    }
+
+    ascodec_unlock();
+
+    return i;
+}
+
+void ascodec_lock(void)
+{
+    mutex_lock(&as_mtx);
+}
+
+void ascodec_unlock(void)
+{
+    mutex_unlock(&as_mtx);
+}
diff --git a/firmware/target/arm/as3525/adc-as3525.c b/firmware/target/arm/as3525/ascodec-target.h
similarity index 68%
copy from firmware/target/arm/as3525/adc-as3525.c
copy to firmware/target/arm/as3525/ascodec-target.h
index 6e1aac5..46f6e11 100644
--- a/firmware/target/arm/as3525/adc-as3525.c
+++ b/firmware/target/arm/as3525/ascodec-target.h
@@ -7,7 +7,10 @@
  *                     \/            \/     \/    \/            \/
  * $Id$
  *
- * Copyright © 2008 Rafaël Carré
+ * Driver for AS3514 audio codec
+ *
+ * Copyright (c) 2007 Daniel Ankers
+ * Copyright (c) 2007 Christian Gmeiner
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -19,15 +22,19 @@
  *
  ****************************************************************************/
 
-#include "adc.h"
+#ifndef _ASCODEC_TARGET_H
+#define _ASCODEC_TARGET_H
 
-/* TODO */
+#include "as3514.h"
 
-unsigned short adc_read(int channel)
-{
-    return 0;
-}
+int ascodec_write(unsigned int index, unsigned int value);
 
-void adc_init(void)
-{
-}
+int ascodec_read(unsigned int index);
+
+int ascodec_readbytes(int index, int len, unsigned char *data);
+
+void ascodec_lock(void);
+
+void ascodec_unlock(void);
+
+#endif /* !_ASCODEC_TARGET_H */
diff --git a/firmware/target/arm/ascodec-target.h b/firmware/target/arm/ascodec-target.h
index afd19ff..3337cb7 100644
--- a/firmware/target/arm/ascodec-target.h
+++ b/firmware/target/arm/ascodec-target.h
@@ -27,14 +27,11 @@
 
 #include "config.h"
 
-#if defined(SANSA_E200) || defined(SANSA_C200) || defined(PHILIPS_SA9200) || \
-    CONFIG_CPU==AS3525
-#define AS3514_I2C_ADDR 0x46
-#else
-#error Unknown target!
-#endif
-
 #ifdef CPU_PP
+/* TODO: This header is actually portalplayer specific, and should be
+ * moved into an appropriate subdir  */
+
+#include "as3514.h"
 #include "i2c-pp.h"
 
 static inline int ascodec_write(unsigned int reg, unsigned int value)
@@ -61,12 +58,7 @@
 {
     i2c_unlock();
 }
-#elif CONFIG_CPU==AS3525
-void ascodec_init(void);
 
-int ascodec_write(int index, int value);
-
-int ascodec_read(int index);
-#endif
+#endif /* CPU_PP */
 
 #endif /* !_ASCODEC_TARGET_H */
diff --git a/firmware/target/arm/sandisk/adc-target.h b/firmware/target/arm/sandisk/adc-target.h
index ab7d5d9..007a11a 100644
--- a/firmware/target/arm/sandisk/adc-target.h
+++ b/firmware/target/arm/sandisk/adc-target.h
@@ -21,25 +21,7 @@
 #ifndef _ADC_TARGET_H_
 #define _ADC_TARGET_H_
 
-/* ADC channels */
-#define NUM_ADC_CHANNELS 13
-
-#define ADC_BVDD         0  /* Battery voltage of 4V LiIo accumulator */
-#define ADC_RTCSUP       1  /* RTC backup battery voltage */
-#define ADC_UVDD         2  /* USB host voltage */
-#define ADC_CHG_IN       3  /* Charger input voltage */
-#define ADC_CVDD         4  /* Charger pump output voltage */
-#define ADC_BATTEMP      5  /* Battery charging temperature */
-#define ADC_MICSUP1      6  /* Voltage on MicSup1 for remote control
-                                or external voltage measurement */
-#define ADC_MICSUP2      7  /* Voltage on MicSup1 for remote control
-                                or external voltage measurement */
-#define ADC_VBE1         8  /* Measuring junction temperature @ 2uA */
-#define ADC_VBE2         9  /* Measuring junction temperature @ 1uA */
-#define ADC_I_MICSUP1    10 /* Current of MicSup1 for remote control detection */
-#define ADC_I_MICSUP2    11 /* Current of MicSup2 for remote control detection */
-#define ADC_VBAT         12 /* Single cell battery voltage */
-
-#define ADC_UNREG_POWER  ADC_BVDD   /* For compatibility */
+/* The ADC sources and channels are common to all targets with AS3514 */
+#include "as3514.h"
 
 #endif