ASoC: dapm: Use DAPM mutex for DAPM ops instead of codec mutex
[linux-2.6.git] / sound / soc / pxa / corgi.c
index 2b1c6e9..28757fb 100644 (file)
@@ -4,39 +4,32 @@
  * Copyright 2005 Wolfson Microelectronics PLC.
  * Copyright 2005 Openedhand Ltd.
  *
- * Authors: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
+ * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
  *          Richard Purdie <richard@openedhand.com>
  *
  *  This program is free software; you can redistribute  it and/or modify it
  *  under  the terms of  the GNU General  Public License as published by the
  *  Free Software Foundation;  either version 2 of the  License, or (at your
  *  option) any later version.
- *
- *  Revision history
- *    30th Nov 2005   Initial version.
- *
  */
 
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/timer.h>
+#include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/platform_device.h>
-#include <sound/driver.h>
+#include <linux/gpio.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/soc.h>
-#include <sound/soc-dapm.h>
 
 #include <asm/mach-types.h>
-#include <asm/hardware/scoop.h>
-#include <asm/arch/pxa-regs.h>
-#include <asm/arch/hardware.h>
-#include <asm/arch/corgi.h>
-#include <asm/arch/audio.h>
+#include <mach/corgi.h>
+#include <mach/audio.h>
 
 #include "../codecs/wm8731.h"
-#include "pxa2xx-pcm.h"
+#include "pxa2xx-i2s.h"
 
 #define CORGI_HP        0
 #define CORGI_MIC       1
@@ -54,73 +47,131 @@ static int corgi_spk_func;
 
 static void corgi_ext_control(struct snd_soc_codec *codec)
 {
-       int spk = 0, mic = 0, line = 0, hp = 0, hs = 0;
+       struct snd_soc_dapm_context *dapm = &codec->dapm;
 
        /* set up jack connection */
        switch (corgi_jack_func) {
        case CORGI_HP:
-               hp = 1;
                /* set = unmute headphone */
-               set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
-               set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
+               gpio_set_value(CORGI_GPIO_MUTE_L, 1);
+               gpio_set_value(CORGI_GPIO_MUTE_R, 1);
+               snd_soc_dapm_disable_pin(dapm, "Mic Jack");
+               snd_soc_dapm_disable_pin(dapm, "Line Jack");
+               snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
+               snd_soc_dapm_disable_pin(dapm, "Headset Jack");
                break;
        case CORGI_MIC:
-               mic = 1;
                /* reset = mute headphone */
-               reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
-               reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
+               gpio_set_value(CORGI_GPIO_MUTE_L, 0);
+               gpio_set_value(CORGI_GPIO_MUTE_R, 0);
+               snd_soc_dapm_enable_pin(dapm, "Mic Jack");
+               snd_soc_dapm_disable_pin(dapm, "Line Jack");
+               snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
+               snd_soc_dapm_disable_pin(dapm, "Headset Jack");
                break;
        case CORGI_LINE:
-               line = 1;
-               reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
-               reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
+               gpio_set_value(CORGI_GPIO_MUTE_L, 0);
+               gpio_set_value(CORGI_GPIO_MUTE_R, 0);
+               snd_soc_dapm_disable_pin(dapm, "Mic Jack");
+               snd_soc_dapm_enable_pin(dapm, "Line Jack");
+               snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
+               snd_soc_dapm_disable_pin(dapm, "Headset Jack");
                break;
        case CORGI_HEADSET:
-               hs = 1;
-               mic = 1;
-               reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
-               set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
+               gpio_set_value(CORGI_GPIO_MUTE_L, 0);
+               gpio_set_value(CORGI_GPIO_MUTE_R, 1);
+               snd_soc_dapm_enable_pin(dapm, "Mic Jack");
+               snd_soc_dapm_disable_pin(dapm, "Line Jack");
+               snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
+               snd_soc_dapm_enable_pin(dapm, "Headset Jack");
                break;
        }
 
        if (corgi_spk_func == CORGI_SPK_ON)
-               spk = 1;
-
-       /* set the enpoints to their new connetion states */
-       snd_soc_dapm_set_endpoint(codec, "Ext Spk", spk);
-       snd_soc_dapm_set_endpoint(codec, "Mic Jack", mic);
-       snd_soc_dapm_set_endpoint(codec, "Line Jack", line);
-       snd_soc_dapm_set_endpoint(codec, "Headphone Jack", hp);
-       snd_soc_dapm_set_endpoint(codec, "Headset Jack", hs);
+               snd_soc_dapm_enable_pin(dapm, "Ext Spk");
+       else
+               snd_soc_dapm_disable_pin(dapm, "Ext Spk");
 
        /* signal a DAPM event */
-       snd_soc_dapm_sync_endpoints(codec);
+       snd_soc_dapm_sync(dapm);
 }
 
 static int corgi_startup(struct snd_pcm_substream *substream)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
-       struct snd_soc_codec *codec = rtd->socdev->codec;
+       struct snd_soc_codec *codec = rtd->codec;
+
+       mutex_lock(&codec->mutex);
 
        /* check the jack status at stream startup */
        corgi_ext_control(codec);
+
+       mutex_unlock(&codec->mutex);
+
        return 0;
 }
 
 /* we need to unmute the HP at shutdown as the mute burns power on corgi */
-static int corgi_shutdown(struct snd_pcm_substream *substream)
+static void corgi_shutdown(struct snd_pcm_substream *substream)
+{
+       /* set = unmute headphone */
+       gpio_set_value(CORGI_GPIO_MUTE_L, 1);
+       gpio_set_value(CORGI_GPIO_MUTE_R, 1);
+}
+
+static int corgi_hw_params(struct snd_pcm_substream *substream,
+       struct snd_pcm_hw_params *params)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
-       struct snd_soc_codec *codec = rtd->socdev->codec;
+       struct snd_soc_dai *codec_dai = rtd->codec_dai;
+       struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
+       unsigned int clk = 0;
+       int ret = 0;
+
+       switch (params_rate(params)) {
+       case 8000:
+       case 16000:
+       case 48000:
+       case 96000:
+               clk = 12288000;
+               break;
+       case 11025:
+       case 22050:
+       case 44100:
+               clk = 11289600;
+               break;
+       }
+
+       /* set codec DAI configuration */
+       ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
+               SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
+       if (ret < 0)
+               return ret;
+
+       /* set cpu DAI configuration */
+       ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
+               SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
+       if (ret < 0)
+               return ret;
+
+       /* set the codec system clock for DAC and ADC */
+       ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
+               SND_SOC_CLOCK_IN);
+       if (ret < 0)
+               return ret;
+
+       /* set the I2S system clock as input (unused) */
+       ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
+               SND_SOC_CLOCK_IN);
+       if (ret < 0)
+               return ret;
 
-       /* set = unmute headphone */
-       set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
-       set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
        return 0;
 }
 
 static struct snd_soc_ops corgi_ops = {
        .startup = corgi_startup,
+       .hw_params = corgi_hw_params,
        .shutdown = corgi_shutdown,
 };
 
@@ -164,23 +215,17 @@ static int corgi_set_spk(struct snd_kcontrol *kcontrol,
        return 1;
 }
 
-static int corgi_amp_event(struct snd_soc_dapm_widget *w, int event)
+static int corgi_amp_event(struct snd_soc_dapm_widget *w,
+       struct snd_kcontrol *k, int event)
 {
-       if (SND_SOC_DAPM_EVENT_ON(event))
-               set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
-       else
-               reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
-
+       gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
        return 0;
 }
 
-static int corgi_mic_event(struct snd_soc_dapm_widget *w, int event)
+static int corgi_mic_event(struct snd_soc_dapm_widget *w,
+       struct snd_kcontrol *k, int event)
 {
-       if (SND_SOC_DAPM_EVENT_ON(event))
-               set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
-       else
-               reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
-
+       gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
        return 0;
 }
 
@@ -194,7 +239,7 @@ SND_SOC_DAPM_HP("Headset Jack", NULL),
 };
 
 /* Corgi machine audio map (connections to the codec pins) */
-static const char *audio_map[][3] = {
+static const struct snd_soc_dapm_route audio_map[] = {
 
        /* headset Jack  - in = micin, out = LHPOUT*/
        {"Headset Jack", NULL, "LHPOUT"},
@@ -212,8 +257,6 @@ static const char *audio_map[][3] = {
 
        /* Same as the above but no mic bias for line signals */
        {"MICIN", NULL, "Line Jack"},
-
-       {NULL, NULL, NULL},
 };
 
 static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
@@ -234,94 +277,49 @@ static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
 /*
  * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  */
-static int corgi_wm8731_init(struct snd_soc_codec *codec)
+static int corgi_wm8731_init(struct snd_soc_pcm_runtime *rtd)
 {
-       int i, err;
+       struct snd_soc_codec *codec = rtd->codec;
+       struct snd_soc_dapm_context *dapm = &codec->dapm;
+       int err;
 
-       snd_soc_dapm_set_endpoint(codec, "LLINEIN", 0);
-       snd_soc_dapm_set_endpoint(codec, "RLINEIN", 0);
+       snd_soc_dapm_nc_pin(dapm, "LLINEIN");
+       snd_soc_dapm_nc_pin(dapm, "RLINEIN");
 
        /* Add corgi specific controls */
-       for (i = 0; i < ARRAY_SIZE(wm8731_corgi_controls); i++) {
-               err = snd_ctl_add(codec->card,
-                       snd_soc_cnew(&wm8731_corgi_controls[i],codec, NULL));
-               if (err < 0)
-                       return err;
-       }
+       err = snd_soc_add_controls(codec, wm8731_corgi_controls,
+                               ARRAY_SIZE(wm8731_corgi_controls));
+       if (err < 0)
+               return err;
 
        /* Add corgi specific widgets */
-       for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) {
-               snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
-       }
+       snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets,
+                                 ARRAY_SIZE(wm8731_dapm_widgets));
 
        /* Set up corgi specific audio path audio_map */
-       for(i = 0; audio_map[i][0] != NULL; i++) {
-               snd_soc_dapm_connect_input(codec, audio_map[i][0],
-                       audio_map[i][1], audio_map[i][2]);
-       }
+       snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
 
-       snd_soc_dapm_sync_endpoints(codec);
+       snd_soc_dapm_sync(dapm);
        return 0;
 }
 
-static unsigned int corgi_config_sysclk(struct snd_soc_pcm_runtime *rtd,
-       struct snd_soc_clock_info *info)
-{
-       if (info->bclk_master & SND_SOC_DAIFMT_CBS_CFS) {
-               /* pxa2xx is i2s master  */
-               switch (info->rate) {
-               case 44100:
-               case 88200:
-                       /* configure codec digital filters for 44.1, 88.2 */
-                       rtd->codec_dai->config_sysclk(rtd->codec_dai, info,
-                               11289600);
-               break;
-               default:
-                       /* configure codec digital filters for all other rates */
-                       rtd->codec_dai->config_sysclk(rtd->codec_dai, info,
-                               CORGI_AUDIO_CLOCK);
-               break;
-               }
-               /* config pxa i2s as master */
-               return rtd->cpu_dai->config_sysclk(rtd->cpu_dai, info,
-                       CORGI_AUDIO_CLOCK);
-       } else {
-               /* codec is i2s master -
-                * only configure codec DAI clock and filters */
-               return rtd->codec_dai->config_sysclk(rtd->codec_dai, info,
-                       CORGI_AUDIO_CLOCK);
-       }
-}
-
 /* corgi digital audio interface glue - connects codec <--> CPU */
 static struct snd_soc_dai_link corgi_dai = {
        .name = "WM8731",
        .stream_name = "WM8731",
-       .cpu_dai = &pxa_i2s_dai,
-       .codec_dai = &wm8731_dai,
+       .cpu_dai_name = "pxa2xx-i2s",
+       .codec_dai_name = "wm8731-hifi",
+       .platform_name = "pxa-pcm-audio",
+       .codec_name = "wm8731.0-001b",
        .init = corgi_wm8731_init,
-       .config_sysclk = corgi_config_sysclk,
+       .ops = &corgi_ops,
 };
 
 /* corgi audio machine driver */
-static struct snd_soc_machine snd_soc_machine_corgi = {
+static struct snd_soc_card snd_soc_corgi = {
        .name = "Corgi",
        .dai_link = &corgi_dai,
        .num_links = 1,
-       .ops = &corgi_ops,
-};
-
-/* corgi audio private data */
-static struct wm8731_setup_data corgi_wm8731_setup = {
-       .i2c_address = 0x1b,
-};
-
-/* corgi audio subsystem */
-static struct snd_soc_device corgi_snd_devdata = {
-       .machine = &snd_soc_machine_corgi,
-       .platform = &pxa2xx_soc_platform,
-       .codec_dev = &soc_codec_dev_wm8731,
-       .codec_data = &corgi_wm8731_setup,
 };
 
 static struct platform_device *corgi_snd_device;
@@ -330,15 +328,15 @@ static int __init corgi_init(void)
 {
        int ret;
 
-       if (!(machine_is_corgi() || machine_is_shepherd() || machine_is_husky()))
+       if (!(machine_is_corgi() || machine_is_shepherd() ||
+             machine_is_husky()))
                return -ENODEV;
 
        corgi_snd_device = platform_device_alloc("soc-audio", -1);
        if (!corgi_snd_device)
                return -ENOMEM;
 
-       platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
-       corgi_snd_devdata.dev = &corgi_snd_device->dev;
+       platform_set_drvdata(corgi_snd_device, &snd_soc_corgi);
        ret = platform_device_add(corgi_snd_device);
 
        if (ret)