]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - sound/soc/atmel/sam9g20_wm8731.c
ASoC: Actively manage MCLK for AT91SAM9G20-EK
[linux-2.6-omap-h63xx.git] / sound / soc / atmel / sam9g20_wm8731.c
index 1fb59a9d3719471fb82c530abef85ce37875c4b6..ab32514a858deec551916b7d871151e5ad7cd0ca 100644 (file)
@@ -45,6 +45,7 @@
 #include <sound/soc.h>
 #include <sound/soc-dapm.h>
 
+#include <asm/mach-types.h>
 #include <mach/hardware.h>
 #include <mach/gpio.h>
 
@@ -52,6 +53,9 @@
 #include "atmel-pcm.h"
 #include "atmel_ssc_dai.h"
 
+#define MCLK_RATE 12000000
+
+static struct clk *mclk;
 
 static int at91sam9g20ek_startup(struct snd_pcm_substream *substream)
 {
@@ -59,11 +63,12 @@ static int at91sam9g20ek_startup(struct snd_pcm_substream *substream)
        struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
        int ret;
 
-       /* codec system clock is supplied by PCK0, set to 12MHz */
        ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK,
-               12000000, SND_SOC_CLOCK_IN);
-       if (ret < 0)
+               MCLK_RATE, SND_SOC_CLOCK_IN);
+       if (ret < 0) {
+               clk_disable(mclk);
                return ret;
+       }
 
        return 0;
 }
@@ -189,6 +194,31 @@ static struct snd_soc_ops at91sam9g20ek_ops = {
        .shutdown = at91sam9g20ek_shutdown,
 };
 
+static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
+                                       enum snd_soc_bias_level level)
+{
+       static int mclk_on;
+       int ret = 0;
+
+       switch (level) {
+       case SND_SOC_BIAS_ON:
+       case SND_SOC_BIAS_PREPARE:
+               if (!mclk_on)
+                       ret = clk_enable(mclk);
+               if (ret == 0)
+                       mclk_on = 1;
+               break;
+
+       case SND_SOC_BIAS_OFF:
+       case SND_SOC_BIAS_STANDBY:
+               if (mclk_on)
+                       clk_disable(mclk);
+               mclk_on = 0;
+               break;
+       }
+
+       return ret;
+}
 
 static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
        SND_SOC_DAPM_MIC("Int Mic", NULL),
@@ -221,8 +251,8 @@ static int at91sam9g20ek_wm8731_init(struct snd_soc_codec *codec)
        snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
 
        /* not connected */
-       snd_soc_dapm_disable_pin(codec, "RLINEIN");
-       snd_soc_dapm_disable_pin(codec, "LLINEIN");
+       snd_soc_dapm_nc_pin(codec, "RLINEIN");
+       snd_soc_dapm_nc_pin(codec, "LLINEIN");
 
        /* always connected */
        snd_soc_dapm_enable_pin(codec, "Int Mic");
@@ -247,6 +277,7 @@ static struct snd_soc_card snd_soc_at91sam9g20ek = {
        .platform = &atmel_soc_platform,
        .dai_link = &at91sam9g20ek_dai,
        .num_links = 1,
+       .set_bias_level = at91sam9g20ek_set_bias_level,
 };
 
 static struct wm8731_setup_data at91sam9g20ek_wm8731_setup = {
@@ -266,13 +297,43 @@ static int __init at91sam9g20ek_init(void)
 {
        struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
        struct ssc_device *ssc = NULL;
+       struct clk *pllb;
        int ret;
 
+       if (!machine_is_at91sam9g20ek())
+               return -ENODEV;
+
+       /*
+        * Codec MCLK is supplied by PCK0 - set it up.
+        */
+       mclk = clk_get(NULL, "pck0");
+       if (IS_ERR(mclk)) {
+               printk(KERN_ERR "ASoC: Failed to get MCLK\n");
+               ret = PTR_ERR(mclk);
+               goto err;
+       }
+
+       pllb = clk_get(NULL, "pllb");
+       if (IS_ERR(mclk)) {
+               printk(KERN_ERR "ASoC: Failed to get PLLB\n");
+               ret = PTR_ERR(mclk);
+               goto err_mclk;
+       }
+       ret = clk_set_parent(mclk, pllb);
+       clk_put(pllb);
+       if (ret != 0) {
+               printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
+               goto err_mclk;
+       }
+
+       clk_set_rate(mclk, MCLK_RATE);
+
        /*
         * Request SSC device
         */
        ssc = ssc_request(0);
        if (IS_ERR(ssc)) {
+               printk(KERN_ERR "ASoC: Failed to request SSC 0\n");
                ret = PTR_ERR(ssc);
                ssc = NULL;
                goto err_ssc;
@@ -281,8 +342,7 @@ static int __init at91sam9g20ek_init(void)
 
        at91sam9g20ek_snd_device = platform_device_alloc("soc-audio", -1);
        if (!at91sam9g20ek_snd_device) {
-               printk(KERN_DEBUG
-                               "platform device allocation failed\n");
+               printk(KERN_ERR "ASoC: Platform device allocation failed\n");
                ret = -ENOMEM;
        }
 
@@ -292,14 +352,17 @@ static int __init at91sam9g20ek_init(void)
 
        ret = platform_device_add(at91sam9g20ek_snd_device);
        if (ret) {
-               printk(KERN_DEBUG
-                               "platform device allocation failed\n");
+               printk(KERN_ERR "ASoC: Platform device allocation failed\n");
                platform_device_put(at91sam9g20ek_snd_device);
        }
 
        return ret;
 
 err_ssc:
+err_mclk:
+       clk_put(mclk);
+       mclk = NULL;
+err:
        return ret;
 }
 
@@ -317,6 +380,8 @@ static void __exit at91sam9g20ek_exit(void)
 
        platform_device_unregister(at91sam9g20ek_snd_device);
        at91sam9g20ek_snd_device = NULL;
+       clk_put(mclk);
+       mclk = NULL;
 }
 
 module_init(at91sam9g20ek_init);