ASoC: pxa-ssp.c fix clock/frame invert
[safe/jmp/linux-2.6] / sound / soc / pxa / pxa2xx-ac97.c
index b45a4f1..01c21c6 100644 (file)
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
-#include <linux/interrupt.h>
-#include <linux/wait.h>
-#include <linux/clk.h>
-#include <linux/delay.h>
 
 #include <sound/core.h>
-#include <sound/pcm.h>
 #include <sound/ac97_codec.h>
-#include <sound/initval.h>
 #include <sound/soc.h>
+#include <sound/pxa2xx-lib.h>
 
-#include <asm/irq.h>
-#include <linux/mutex.h>
-#include <asm/arch/hardware.h>
-#include <asm/arch/pxa-regs.h>
-#include <asm/arch/pxa2xx-gpio.h>
-#include <asm/arch/audio.h>
+#include <mach/hardware.h>
+#include <mach/pxa-regs.h>
+#include <mach/regs-ac97.h>
 
 #include "pxa2xx-pcm.h"
 #include "pxa2xx-ac97.h"
 
-static DEFINE_MUTEX(car_mutex);
-static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
-static volatile long gsr_bits;
-static struct clk *ac97_clk;
-#ifdef CONFIG_PXA27x
-static struct clk *ac97conf_clk;
-#endif
-
-/*
- * Beware PXA27x bugs:
- *
- *   o Slot 12 read from modem space will hang controller.
- *   o CDONE, SDONE interrupt fails after any slot 12 IO.
- *
- * We therefore have an hybrid approach for waiting on SDONE (interrupt or
- * 1 jiffy timeout if interrupt never comes).
- */
-
-static unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97,
-       unsigned short reg)
-{
-       unsigned short val = -1;
-       volatile u32 *reg_addr;
-
-       mutex_lock(&car_mutex);
-
-       /* set up primary or secondary codec/modem space */
-#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
-       reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
-#else
-       if (reg == AC97_GPIO_STATUS)
-               reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
-       else
-               reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
-#endif
-       reg_addr += (reg >> 1);
-
-#ifndef CONFIG_PXA27x
-       if (reg == AC97_GPIO_STATUS) {
-               /* read from controller cache */
-               val = *reg_addr;
-               goto out;
-       }
-#endif
-
-       /* start read access across the ac97 link */
-       GSR = GSR_CDONE | GSR_SDONE;
-       gsr_bits = 0;
-       val = *reg_addr;
-
-       wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
-       if (!((GSR | gsr_bits) & GSR_SDONE)) {
-               printk(KERN_ERR "%s: read error (ac97_reg=%x GSR=%#lx)\n",
-                               __func__, reg, GSR | gsr_bits);
-               val = -1;
-               goto out;
-       }
-
-       /* valid data now */
-       GSR = GSR_CDONE | GSR_SDONE;
-       gsr_bits = 0;
-       val = *reg_addr;
-       /* but we've just started another cycle... */
-       wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
-
-out:   mutex_unlock(&car_mutex);
-       return val;
-}
-
-static void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
-       unsigned short val)
-{
-       volatile u32 *reg_addr;
-
-       mutex_lock(&car_mutex);
-
-       /* set up primary or secondary codec/modem space */
-#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
-       reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
-#else
-       if (reg == AC97_GPIO_STATUS)
-               reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
-       else
-               reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
-#endif
-       reg_addr += (reg >> 1);
-
-       GSR = GSR_CDONE | GSR_SDONE;
-       gsr_bits = 0;
-       *reg_addr = val;
-       wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1);
-       if (!((GSR | gsr_bits) & GSR_CDONE))
-               printk(KERN_ERR "%s: write error (ac97_reg=%x GSR=%#lx)\n",
-                               __func__, reg, GSR | gsr_bits);
-
-       mutex_unlock(&car_mutex);
-}
-
 static void pxa2xx_ac97_warm_reset(struct snd_ac97 *ac97)
 {
-#ifdef CONFIG_PXA3xx
-       int timeout = 100;
-#endif
-       gsr_bits = 0;
-
-#ifdef CONFIG_PXA27x
-       /* warm reset broken on Bulverde,
-          so manually keep AC97 reset high */
-       pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
-       udelay(10);
-       GCR |= GCR_WARM_RST;
-       pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
-       udelay(500);
-#elif defined(CONFIG_PXA3xx)
-       /* Can't use interrupts */
-       GCR |= GCR_WARM_RST;
-       while (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)) && timeout--)
-               mdelay(1);
-#else
-       GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
-       wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
-#endif
-
-       if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
-               printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
-                                __func__, gsr_bits);
+       pxa2xx_ac97_try_warm_reset(ac97);
 
-       GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
-       GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
+       pxa2xx_ac97_finish_reset(ac97);
 }
 
 static void pxa2xx_ac97_cold_reset(struct snd_ac97 *ac97)
 {
-#ifdef CONFIG_PXA3xx
-       int timeout = 1000;
-
-       /* Hold CLKBPB for 100us */
-       GCR = 0;
-       GCR = GCR_CLKBPB;
-       udelay(100);
-       GCR = 0;
-#endif
-
-       GCR &=  GCR_COLD_RST;  /* clear everything but nCRST */
-       GCR &= ~GCR_COLD_RST;  /* then assert nCRST */
-
-       gsr_bits = 0;
-#ifdef CONFIG_PXA27x
-       /* PXA27x Developers Manual section 13.5.2.2.1 */
-       clk_enable(ac97conf_clk);
-       udelay(5);
-       clk_disable(ac97conf_clk);
-       GCR = GCR_COLD_RST;
-       udelay(50);
-#elif defined(CONFIG_PXA3xx)
-       /* Can't use interrupts on PXA3xx */
-       GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
-
-       GCR = GCR_WARM_RST | GCR_COLD_RST;
-       while (!(GSR & (GSR_PCR | GSR_SCR)) && timeout--)
-               mdelay(10);
-#else
-       GCR = GCR_COLD_RST;
-       GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
-       wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
-#endif
-
-       if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
-               printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
-                                __func__, gsr_bits);
-
-       GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
-       GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
-}
-
-static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
-{
-       long status;
-
-       status = GSR;
-       if (status) {
-               GSR = status;
-               gsr_bits |= status;
-               wake_up(&gsr_wq);
-
-#ifdef CONFIG_PXA27x
-               /* Although we don't use those we still need to clear them
-                  since they tend to spuriously trigger when MMC is used
-                  (hardware bug? go figure)... */
-               MISR = MISR_EOC;
-               PISR = PISR_EOC;
-               MCSR = MCSR_EOC;
-#endif
-
-               return IRQ_HANDLED;
-       }
+       pxa2xx_ac97_try_cold_reset(ac97);
 
-       return IRQ_NONE;
+       pxa2xx_ac97_finish_reset(ac97);
 }
 
 struct snd_ac97_bus_ops soc_ac97_ops = {
@@ -244,7 +50,7 @@ struct snd_ac97_bus_ops soc_ac97_ops = {
 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_out = {
        .name                   = "AC97 PCM Stereo out",
        .dev_addr               = __PREG(PCDR),
-       .drcmr                  = &DRCMRTXPCDR,
+       .drcmr                  = &DRCMR(12),
        .dcmd                   = DCMD_INCSRCADDR | DCMD_FLOWTRG |
                                  DCMD_BURST32 | DCMD_WIDTH4,
 };
@@ -252,7 +58,7 @@ static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_out = {
 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_in = {
        .name                   = "AC97 PCM Stereo in",
        .dev_addr               = __PREG(PCDR),
-       .drcmr                  = &DRCMRRXPCDR,
+       .drcmr                  = &DRCMR(11),
        .dcmd                   = DCMD_INCTRGADDR | DCMD_FLOWSRC |
                                  DCMD_BURST32 | DCMD_WIDTH4,
 };
@@ -260,7 +66,7 @@ static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_in = {
 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_out = {
        .name                   = "AC97 Aux PCM (Slot 5) Mono out",
        .dev_addr               = __PREG(MODR),
-       .drcmr                  = &DRCMRTXMODR,
+       .drcmr                  = &DRCMR(10),
        .dcmd                   = DCMD_INCSRCADDR | DCMD_FLOWTRG |
                                  DCMD_BURST16 | DCMD_WIDTH2,
 };
@@ -268,7 +74,7 @@ static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_out = {
 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_in = {
        .name                   = "AC97 Aux PCM (Slot 5) Mono in",
        .dev_addr               = __PREG(MODR),
-       .drcmr                  = &DRCMRRXMODR,
+       .drcmr                  = &DRCMR(9),
        .dcmd                   = DCMD_INCTRGADDR | DCMD_FLOWSRC |
                                  DCMD_BURST16 | DCMD_WIDTH2,
 };
@@ -276,33 +82,20 @@ static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_in = {
 static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_mic_mono_in = {
        .name                   = "AC97 Mic PCM (Slot 6) Mono in",
        .dev_addr               = __PREG(MCDR),
-       .drcmr                  = &DRCMRRXMCDR,
+       .drcmr                  = &DRCMR(8),
        .dcmd                   = DCMD_INCTRGADDR | DCMD_FLOWSRC |
                                  DCMD_BURST16 | DCMD_WIDTH2,
 };
 
 #ifdef CONFIG_PM
-static int pxa2xx_ac97_suspend(struct platform_device *pdev,
-       struct snd_soc_dai *dai)
+static int pxa2xx_ac97_suspend(struct snd_soc_dai *dai)
 {
-       GCR |= GCR_ACLINK_OFF;
-       clk_disable(ac97_clk);
-       return 0;
+       return pxa2xx_ac97_hw_suspend();
 }
 
-static int pxa2xx_ac97_resume(struct platform_device *pdev,
-       struct snd_soc_dai *dai)
+static int pxa2xx_ac97_resume(struct snd_soc_dai *dai)
 {
-       pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
-       pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
-       pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
-       pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
-#ifdef CONFIG_PXA27x
-       /* Use GPIO 113 as AC97 Reset on Bulverde */
-       pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
-#endif
-       clk_enable(ac97_clk);
-       return 0;
+       return pxa2xx_ac97_hw_resume();
 }
 
 #else
@@ -313,65 +106,18 @@ static int pxa2xx_ac97_resume(struct platform_device *pdev,
 static int pxa2xx_ac97_probe(struct platform_device *pdev,
                             struct snd_soc_dai *dai)
 {
-       int ret;
-
-       ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
-       if (ret < 0)
-               goto err;
-
-       pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
-       pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
-       pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
-       pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
-#ifdef CONFIG_PXA27x
-       /* Use GPIO 113 as AC97 Reset on Bulverde */
-       pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
-
-       ac97conf_clk = clk_get(&pdev->dev, "AC97CONFCLK");
-       if (IS_ERR(ac97conf_clk)) {
-               ret = PTR_ERR(ac97conf_clk);
-               ac97conf_clk = NULL;
-               goto err_irq;
-       }
-#endif
-       ac97_clk = clk_get(&pdev->dev, "AC97CLK");
-       if (IS_ERR(ac97_clk)) {
-               ret = PTR_ERR(ac97_clk);
-               ac97_clk = NULL;
-               goto err_irq;
-       }
-       clk_enable(ac97_clk);
-       return 0;
-
- err_irq:
-       GCR |= GCR_ACLINK_OFF;
-#ifdef CONFIG_PXA27x
-       if (ac97conf_clk) {
-               clk_put(ac97conf_clk);
-               ac97conf_clk = NULL;
-       }
-#endif
-       free_irq(IRQ_AC97, NULL);
- err:
-       return ret;
+       return pxa2xx_ac97_hw_probe(to_platform_device(dai->dev));
 }
 
 static void pxa2xx_ac97_remove(struct platform_device *pdev,
                               struct snd_soc_dai *dai)
 {
-       GCR |= GCR_ACLINK_OFF;
-       free_irq(IRQ_AC97, NULL);
-#ifdef CONFIG_PXA27x
-       clk_put(ac97conf_clk);
-       ac97conf_clk = NULL;
-#endif
-       clk_disable(ac97_clk);
-       clk_put(ac97_clk);
-       ac97_clk = NULL;
+       pxa2xx_ac97_hw_remove(to_platform_device(dai->dev));
 }
 
 static int pxa2xx_ac97_hw_params(struct snd_pcm_substream *substream,
-                               struct snd_pcm_hw_params *params)
+                                struct snd_pcm_hw_params *params,
+                                struct snd_soc_dai *dai)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
@@ -385,7 +131,8 @@ static int pxa2xx_ac97_hw_params(struct snd_pcm_substream *substream,
 }
 
 static int pxa2xx_ac97_hw_aux_params(struct snd_pcm_substream *substream,
-       struct snd_pcm_hw_params *params)
+                                    struct snd_pcm_hw_params *params,
+                                    struct snd_soc_dai *dai)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
@@ -399,7 +146,8 @@ static int pxa2xx_ac97_hw_aux_params(struct snd_pcm_substream *substream,
 }
 
 static int pxa2xx_ac97_hw_mic_params(struct snd_pcm_substream *substream,
-       struct snd_pcm_hw_params *params)
+                                    struct snd_pcm_hw_params *params,
+                                    struct snd_soc_dai *dai)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
@@ -416,6 +164,18 @@ static int pxa2xx_ac97_hw_mic_params(struct snd_pcm_substream *substream,
                SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
                SNDRV_PCM_RATE_48000)
 
+static struct snd_soc_dai_ops pxa_ac97_hifi_dai_ops = {
+       .hw_params      = pxa2xx_ac97_hw_params,
+};
+
+static struct snd_soc_dai_ops pxa_ac97_aux_dai_ops = {
+       .hw_params      = pxa2xx_ac97_hw_aux_params,
+};
+
+static struct snd_soc_dai_ops pxa_ac97_mic_dai_ops = {
+       .hw_params      = pxa2xx_ac97_hw_mic_params,
+};
+
 /*
  * There is only 1 physical AC97 interface for pxa2xx, but it
  * has extra fifo's that can be used for aux DACs and ADCs.
@@ -424,7 +184,7 @@ struct snd_soc_dai pxa_ac97_dai[] = {
 {
        .name = "pxa2xx-ac97",
        .id = 0,
-       .type = SND_SOC_DAI_AC97,
+       .ac97_control = 1,
        .probe = pxa2xx_ac97_probe,
        .remove = pxa2xx_ac97_remove,
        .suspend = pxa2xx_ac97_suspend,
@@ -441,13 +201,12 @@ struct snd_soc_dai pxa_ac97_dai[] = {
                .channels_max = 2,
                .rates = PXA2XX_AC97_RATES,
                .formats = SNDRV_PCM_FMTBIT_S16_LE,},
-       .ops = {
-               .hw_params = pxa2xx_ac97_hw_params,},
+       .ops = &pxa_ac97_hifi_dai_ops,
 },
 {
        .name = "pxa2xx-ac97-aux",
        .id = 1,
-       .type = SND_SOC_DAI_AC97,
+       .ac97_control = 1,
        .playback = {
                .stream_name = "AC97 Aux Playback",
                .channels_min = 1,
@@ -460,27 +219,67 @@ struct snd_soc_dai pxa_ac97_dai[] = {
                .channels_max = 1,
                .rates = PXA2XX_AC97_RATES,
                .formats = SNDRV_PCM_FMTBIT_S16_LE,},
-       .ops = {
-               .hw_params = pxa2xx_ac97_hw_aux_params,},
+       .ops = &pxa_ac97_aux_dai_ops,
 },
 {
        .name = "pxa2xx-ac97-mic",
        .id = 2,
-       .type = SND_SOC_DAI_AC97,
+       .ac97_control = 1,
        .capture = {
                .stream_name = "AC97 Mic Capture",
                .channels_min = 1,
                .channels_max = 1,
                .rates = PXA2XX_AC97_RATES,
                .formats = SNDRV_PCM_FMTBIT_S16_LE,},
-       .ops = {
-               .hw_params = pxa2xx_ac97_hw_mic_params,},
+       .ops = &pxa_ac97_mic_dai_ops,
 },
 };
 
 EXPORT_SYMBOL_GPL(pxa_ac97_dai);
 EXPORT_SYMBOL_GPL(soc_ac97_ops);
 
+static int __devinit pxa2xx_ac97_dev_probe(struct platform_device *pdev)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(pxa_ac97_dai); i++)
+               pxa_ac97_dai[i].dev = &pdev->dev;
+
+       /* Punt most of the init to the SoC probe; we may need the machine
+        * driver to do interesting things with the clocking to get us up
+        * and running.
+        */
+       return snd_soc_register_dais(pxa_ac97_dai, ARRAY_SIZE(pxa_ac97_dai));
+}
+
+static int __devexit pxa2xx_ac97_dev_remove(struct platform_device *pdev)
+{
+       snd_soc_unregister_dais(pxa_ac97_dai, ARRAY_SIZE(pxa_ac97_dai));
+
+       return 0;
+}
+
+static struct platform_driver pxa2xx_ac97_driver = {
+       .probe          = pxa2xx_ac97_dev_probe,
+       .remove         = __devexit_p(pxa2xx_ac97_dev_remove),
+       .driver         = {
+               .name   = "pxa2xx-ac97",
+               .owner  = THIS_MODULE,
+       },
+};
+
+static int __init pxa_ac97_init(void)
+{
+       return platform_driver_register(&pxa2xx_ac97_driver);
+}
+module_init(pxa_ac97_init);
+
+static void __exit pxa_ac97_exit(void)
+{
+       platform_driver_unregister(&pxa2xx_ac97_driver);
+}
+module_exit(pxa_ac97_exit);
+
 MODULE_AUTHOR("Nicolas Pitre");
 MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
 MODULE_LICENSE("GPL");