ALSA: ASoC: TLV320AIC3X: Modify only interface related bits in aic3x_set_dai_fmt
[pandora-kernel.git] / sound / soc / codecs / tlv320aic3x.c
index 630684f..29dc0ec 100644 (file)
@@ -49,7 +49,7 @@
 #include "tlv320aic3x.h"
 
 #define AUDIO_NAME "aic3x"
-#define AIC3X_VERSION "0.1"
+#define AIC3X_VERSION "0.2"
 
 /* codec private data */
 struct aic3x_priv {
@@ -138,6 +138,20 @@ static int aic3x_write(struct snd_soc_codec *codec, unsigned int reg,
                return -EIO;
 }
 
+/*
+ * read from the aic3x register space
+ */
+static int aic3x_read(struct snd_soc_codec *codec, unsigned int reg,
+                     u8 *value)
+{
+       *value = reg & 0xff;
+       if (codec->hw_read(codec->control_data, value, 1) != 1)
+               return -EIO;
+
+       aic3x_write_reg_cache(codec, reg, *value);
+       return 0;
+}
+
 #define SOC_DAPM_SINGLE_AIC3X(xname, reg, shift, mask, invert) \
 {      .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
        .info = snd_soc_info_volsw, \
@@ -442,10 +456,12 @@ static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = {
                         &aic3x_right_line2_mux_controls),
 
        /* Mic Bias */
-       SND_SOC_DAPM_MICBIAS("Mic Bias 2V", MICBIAS_CTRL, 6, 0),
-       SND_SOC_DAPM_MICBIAS("Mic Bias 2.5V", MICBIAS_CTRL, 7, 0),
-       SND_SOC_DAPM_MICBIAS("Mic Bias AVDD", MICBIAS_CTRL, 6, 0),
-       SND_SOC_DAPM_MICBIAS("Mic Bias AVDD", MICBIAS_CTRL, 7, 0),
+       SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias 2V",
+                        MICBIAS_CTRL, 6, 3, 1, 0),
+       SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias 2.5V",
+                        MICBIAS_CTRL, 6, 3, 2, 0),
+       SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias AVDD",
+                        MICBIAS_CTRL, 6, 3, 3, 0),
 
        /* Left PGA to Left Output bypass */
        SND_SOC_DAPM_MIXER("Left PGA Bypass Mixer", SND_SOC_NOPM, 0, 0,
@@ -483,7 +499,7 @@ static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = {
        SND_SOC_DAPM_INPUT("LINE2R"),
 };
 
-static const char *intercon[][3] = {
+static const struct snd_soc_dapm_route intercon[] = {
        /* Left Output */
        {"Left DAC Mux", "DAC_L1", "Left DAC"},
        {"Left DAC Mux", "DAC_L2", "Left DAC"},
@@ -539,8 +555,8 @@ static const char *intercon[][3] = {
        {"HPRCOM", NULL, "Right HP Com"},
 
        /* Mono Output */
-       {"MONOLOUT", NULL, "Mono Out"},
-       {"MONOLOUT", NULL, "Mono Out"},
+       {"MONO_LOUT", NULL, "Mono Out"},
+       {"MONO_LOUT", NULL, "Mono Out"},
 
        /* Left Input */
        {"Left Line1L Mux", "single-ended", "LINE1L"},
@@ -627,102 +643,20 @@ static const char *intercon[][3] = {
        {"Right Line Out", NULL, "Right Line2 Bypass Mixer"},
        {"Mono Out", NULL, "Right Line2 Bypass Mixer"},
        {"Right HP Out", NULL, "Right Line2 Bypass Mixer"},
-
-       /* terminator */
-       {NULL, NULL, NULL},
 };
 
 static int aic3x_add_widgets(struct snd_soc_codec *codec)
 {
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(aic3x_dapm_widgets); i++)
-               snd_soc_dapm_new_control(codec, &aic3x_dapm_widgets[i]);
+       snd_soc_dapm_new_controls(codec, aic3x_dapm_widgets,
+                                 ARRAY_SIZE(aic3x_dapm_widgets));
 
        /* set up audio path interconnects */
-       for (i = 0; intercon[i][0] != NULL; i++)
-               snd_soc_dapm_connect_input(codec, intercon[i][0],
-                                          intercon[i][1], intercon[i][2]);
+       snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
 
        snd_soc_dapm_new_widgets(codec);
        return 0;
 }
 
-struct aic3x_rate_divs {
-       u32 mclk;
-       u32 rate;
-       u32 fsref_reg;
-       u8 sr_reg:4;
-       u8 pllj_reg;
-       u16 plld_reg;
-};
-
-/* AIC3X codec mclk clock divider coefficients */
-static const struct aic3x_rate_divs aic3x_divs[] = {
-       /* 8k */
-       {12000000, 8000, 48000, 0xa, 16, 3840},
-       {19200000, 8000, 48000, 0xa, 10, 2400},
-       {22579200, 8000, 48000, 0xa, 8, 7075},
-       {33868800, 8000, 48000, 0xa, 5, 8049},
-       /* 11.025k */
-       {12000000, 11025, 44100, 0x6, 15, 528},
-       {19200000, 11025, 44100, 0x6, 9, 4080},
-       {22579200, 11025, 44100, 0x6, 8, 0},
-       {33868800, 11025, 44100, 0x6, 5, 3333},
-       /* 16k */
-       {12000000, 16000, 48000, 0x4, 16, 3840},
-       {19200000, 16000, 48000, 0x4, 10, 2400},
-       {22579200, 16000, 48000, 0x4, 8, 7075},
-       {33868800, 16000, 48000, 0x4, 5, 8049},
-       /* 22.05k */
-       {12000000, 22050, 44100, 0x2, 15, 528},
-       {19200000, 22050, 44100, 0x2, 9, 4080},
-       {22579200, 22050, 44100, 0x2, 8, 0},
-       {33868800, 22050, 44100, 0x2, 5, 3333},
-       /* 32k */
-       {12000000, 32000, 48000, 0x1, 16, 3840},
-       {19200000, 32000, 48000, 0x1, 10, 2400},
-       {22579200, 32000, 48000, 0x1, 8, 7075},
-       {33868800, 32000, 48000, 0x1, 5, 8049},
-       /* 44.1k */
-       {12000000, 44100, 44100, 0x0, 15, 528},
-       {19200000, 44100, 44100, 0x0, 9, 4080},
-       {22579200, 44100, 44100, 0x0, 8, 0},
-       {33868800, 44100, 44100, 0x0, 5, 3333},
-       /* 48k */
-       {12000000, 48000, 48000, 0x0, 16, 3840},
-       {19200000, 48000, 48000, 0x0, 10, 2400},
-       {22579200, 48000, 48000, 0x0, 8, 7075},
-       {33868800, 48000, 48000, 0x0, 5, 8049},
-       /* 64k */
-       {12000000, 64000, 96000, 0x1, 16, 3840},
-       {19200000, 64000, 96000, 0x1, 10, 2400},
-       {22579200, 64000, 96000, 0x1, 8, 7075},
-       {33868800, 64000, 96000, 0x1, 5, 8049},
-       /* 88.2k */
-       {12000000, 88200, 88200, 0x0, 15, 528},
-       {19200000, 88200, 88200, 0x0, 9, 4080},
-       {22579200, 88200, 88200, 0x0, 8, 0},
-       {33868800, 88200, 88200, 0x0, 5, 3333},
-       /* 96k */
-       {12000000, 96000, 96000, 0x0, 16, 3840},
-       {19200000, 96000, 96000, 0x0, 10, 2400},
-       {22579200, 96000, 96000, 0x0, 8, 7075},
-       {33868800, 96000, 96000, 0x0, 5, 8049},
-};
-
-static inline int aic3x_get_divs(int mclk, int rate)
-{
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(aic3x_divs); i++) {
-               if (aic3x_divs[i].rate == rate && aic3x_divs[i].mclk == mclk)
-                       return i;
-       }
-
-       return 0;
-}
-
 static int aic3x_hw_params(struct snd_pcm_substream *substream,
                           struct snd_pcm_hw_params *params)
 {
@@ -730,49 +664,107 @@ static int aic3x_hw_params(struct snd_pcm_substream *substream,
        struct snd_soc_device *socdev = rtd->socdev;
        struct snd_soc_codec *codec = socdev->codec;
        struct aic3x_priv *aic3x = codec->private_data;
-       int i;
-       u8 data, pll_p, pll_r, pll_j;
-       u16 pll_d;
-
-       i = aic3x_get_divs(aic3x->sysclk, params_rate(params));
+       int codec_clk = 0, bypass_pll = 0, fsref, last_clk = 0;
+       u8 data, r, p, pll_q, pll_p = 1, pll_r = 1, pll_j = 1;
+       u16 pll_d = 1;
 
-       /* Route Left DAC to left channel input and
-        * right DAC to right channel input */
-       data = (LDAC2LCH | RDAC2RCH);
-       switch (aic3x_divs[i].fsref_reg) {
-       case 44100:
-               data |= FSREF_44100;
+       /* select data word length */
+       data =
+           aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLB) & (~(0x3 << 4));
+       switch (params_format(params)) {
+       case SNDRV_PCM_FORMAT_S16_LE:
                break;
-       case 48000:
-               data |= FSREF_48000;
+       case SNDRV_PCM_FORMAT_S20_3LE:
+               data |= (0x01 << 4);
                break;
-       case 88200:
-               data |= FSREF_44100 | DUAL_RATE_MODE;
+       case SNDRV_PCM_FORMAT_S24_LE:
+               data |= (0x02 << 4);
                break;
-       case 96000:
-               data |= FSREF_48000 | DUAL_RATE_MODE;
+       case SNDRV_PCM_FORMAT_S32_LE:
+               data |= (0x03 << 4);
                break;
        }
+       aic3x_write(codec, AIC3X_ASD_INTF_CTRLB, data);
+
+       /* Fsref can be 44100 or 48000 */
+       fsref = (params_rate(params) % 11025 == 0) ? 44100 : 48000;
+
+       /* Try to find a value for Q which allows us to bypass the PLL and
+        * generate CODEC_CLK directly. */
+       for (pll_q = 2; pll_q < 18; pll_q++)
+               if (aic3x->sysclk / (128 * pll_q) == fsref) {
+                       bypass_pll = 1;
+                       break;
+               }
+
+       if (bypass_pll) {
+               pll_q &= 0xf;
+               aic3x_write(codec, AIC3X_PLL_PROGA_REG, pll_q << PLLQ_SHIFT);
+               aic3x_write(codec, AIC3X_GPIOB_REG, CODEC_CLKIN_CLKDIV);
+       } else
+               aic3x_write(codec, AIC3X_GPIOB_REG, CODEC_CLKIN_PLLDIV);
+
+       /* Route Left DAC to left channel input and
+        * right DAC to right channel input */
+       data = (LDAC2LCH | RDAC2RCH);
+       data |= (fsref == 44100) ? FSREF_44100 : FSREF_48000;
+       if (params_rate(params) >= 64000)
+               data |= DUAL_RATE_MODE;
        aic3x_write(codec, AIC3X_CODEC_DATAPATH_REG, data);
 
        /* codec sample rate select */
-       data = aic3x_divs[i].sr_reg;
+       data = (fsref * 20) / params_rate(params);
+       if (params_rate(params) < 64000)
+               data /= 2;
+       data /= 5;
+       data -= 2;
        data |= (data << 4);
        aic3x_write(codec, AIC3X_SAMPLE_RATE_SEL_REG, data);
 
-       /* Use PLL for generation Fsref by equation:
-        * Fsref = (MCLK * K * R)/(2048 * P);
-        * Fix P = 2 and R = 1 and calculate K, if
-        * K = J.D, i.e. J - an interger portion of K and D is the fractional
-        * one with 4 digits of precision;
-        * Example:
-        * For MCLK = 22.5792 MHz and Fsref = 48kHz:
-        * Select P = 2, R= 1, K = 8.7074, which results in J = 8, D = 7074
+       if (bypass_pll)
+               return 0;
+
+       /* Use PLL
+        * find an apropriate setup for j, d, r and p by iterating over
+        * p and r - j and d are calculated for each fraction.
+        * Up to 128 values are probed, the closest one wins the game.
+        * The sysclk is divided by 1000 to prevent integer overflows.
         */
-       pll_p = 2;
-       pll_r = 1;
-       pll_j = aic3x_divs[i].pllj_reg;
-       pll_d = aic3x_divs[i].plld_reg;
+       codec_clk = (2048 * fsref) / (aic3x->sysclk / 1000);
+
+       for (r = 1; r <= 16; r++)
+               for (p = 1; p <= 8; p++) {
+                       int clk, tmp = (codec_clk * pll_r * 10) / pll_p;
+                       u8 j = tmp / 10000;
+                       u16 d = tmp % 10000;
+
+                       if (j > 63)
+                               continue;
+
+                       if (d != 0 && aic3x->sysclk < 10000000)
+                               continue;
+
+                       /* This is actually 1000 * ((j + (d/10000)) * r) / p
+                        * The term had to be converted to get rid of the
+                        * division by 10000 */
+                       clk = ((10000 * j * r) + (d * r)) / (10 * p);
+
+                       /* check whether this values get closer than the best
+                        * ones we had before */
+                       if (abs(codec_clk - clk) < abs(codec_clk - last_clk)) {
+                               pll_j = j; pll_d = d; pll_r = r; pll_p = p;
+                               last_clk = clk;
+                       }
+
+                       /* Early exit for exact matches */
+                       if (clk == codec_clk)
+                               break;
+               }
+
+       if (last_clk == 0) {
+               printk(KERN_ERR "%s(): unable to setup PLL\n", __func__);
+               return -EINVAL;
+       }
 
        data = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
        aic3x_write(codec, AIC3X_PLL_PROGA_REG, data | (pll_p << PLLP_SHIFT));
@@ -782,24 +774,6 @@ static int aic3x_hw_params(struct snd_pcm_substream *substream,
        aic3x_write(codec, AIC3X_PLL_PROGD_REG,
                    (pll_d & 0x3F) << PLLD_LSB_SHIFT);
 
-       /* select data word length */
-       data =
-           aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLB) & (~(0x3 << 4));
-       switch (params_format(params)) {
-       case SNDRV_PCM_FORMAT_S16_LE:
-               break;
-       case SNDRV_PCM_FORMAT_S20_3LE:
-               data |= (0x01 << 4);
-               break;
-       case SNDRV_PCM_FORMAT_S24_LE:
-               data |= (0x02 << 4);
-               break;
-       case SNDRV_PCM_FORMAT_S32_LE:
-               data |= (0x03 << 4);
-               break;
-       }
-       aic3x_write(codec, AIC3X_ASD_INTF_CTRLB, data);
-
        return 0;
 }
 
@@ -826,16 +800,8 @@ static int aic3x_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
        struct snd_soc_codec *codec = codec_dai->codec;
        struct aic3x_priv *aic3x = codec->private_data;
 
-       switch (freq) {
-       case 12000000:
-       case 19200000:
-       case 22579200:
-       case 33868800:
-               aic3x->sysclk = freq;
-               return 0;
-       }
-
-       return -EINVAL;
+       aic3x->sysclk = freq;
+       return 0;
 }
 
 static int aic3x_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
@@ -843,8 +809,10 @@ static int aic3x_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
 {
        struct snd_soc_codec *codec = codec_dai->codec;
        struct aic3x_priv *aic3x = codec->private_data;
-       u8 iface_areg = 0;
-       u8 iface_breg = 0;
+       u8 iface_areg, iface_breg;
+
+       iface_areg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLA) & 0x3f;
+       iface_breg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLB) & 0x3f;
 
        /* set master/slave audio interface */
        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
@@ -883,13 +851,14 @@ static int aic3x_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
        return 0;
 }
 
-static int aic3x_dapm_event(struct snd_soc_codec *codec, int event)
+static int aic3x_set_bias_level(struct snd_soc_codec *codec,
+                               enum snd_soc_bias_level level)
 {
        struct aic3x_priv *aic3x = codec->private_data;
        u8 reg;
 
-       switch (event) {
-       case SNDRV_CTL_POWER_D0:
+       switch (level) {
+       case SND_SOC_BIAS_ON:
                /* all power is driven by DAPM system */
                if (aic3x->master) {
                        /* enable pll */
@@ -898,10 +867,9 @@ static int aic3x_dapm_event(struct snd_soc_codec *codec, int event)
                                    reg | PLL_ENABLE);
                }
                break;
-       case SNDRV_CTL_POWER_D1:
-       case SNDRV_CTL_POWER_D2:
+       case SND_SOC_BIAS_PREPARE:
                break;
-       case SNDRV_CTL_POWER_D3hot:
+       case SND_SOC_BIAS_STANDBY:
                /*
                 * all power is driven by DAPM system,
                 * so output power is safe if bypass was set
@@ -913,7 +881,7 @@ static int aic3x_dapm_event(struct snd_soc_codec *codec, int event)
                                    reg & ~PLL_ENABLE);
                }
                break;
-       case SNDRV_CTL_POWER_D3cold:
+       case SND_SOC_BIAS_OFF:
                /* force all power off */
                reg = aic3x_read_reg_cache(codec, LINE1L_2_LADC_CTRL);
                aic3x_write(codec, LINE1L_2_LADC_CTRL, reg & ~LADC_PWR_ON);
@@ -949,11 +917,38 @@ static int aic3x_dapm_event(struct snd_soc_codec *codec, int event)
                }
                break;
        }
-       codec->dapm_state = event;
+       codec->bias_level = level;
 
        return 0;
 }
 
+void aic3x_set_gpio(struct snd_soc_codec *codec, int gpio, int state)
+{
+       u8 reg = gpio ? AIC3X_GPIO2_REG : AIC3X_GPIO1_REG;
+       u8 bit = gpio ? 3: 0;
+       u8 val = aic3x_read_reg_cache(codec, reg) & ~(1 << bit);
+       aic3x_write(codec, reg, val | (!!state << bit));
+}
+EXPORT_SYMBOL_GPL(aic3x_set_gpio);
+
+int aic3x_get_gpio(struct snd_soc_codec *codec, int gpio)
+{
+       u8 reg = gpio ? AIC3X_GPIO2_REG : AIC3X_GPIO1_REG;
+       u8 val, bit = gpio ? 2: 1;
+
+       aic3x_read(codec, reg, &val);
+       return (val >> bit) & 1;
+}
+EXPORT_SYMBOL_GPL(aic3x_get_gpio);
+
+int aic3x_headset_detected(struct snd_soc_codec *codec)
+{
+       u8 val;
+       aic3x_read(codec, AIC3X_RT_IRQ_FLAGS_REG, &val);
+       return (val >> 2) & 1;
+}
+EXPORT_SYMBOL_GPL(aic3x_headset_detected);
+
 #define AIC3X_RATES    SNDRV_PCM_RATE_8000_96000
 #define AIC3X_FORMATS  (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
                         SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
@@ -988,7 +983,7 @@ static int aic3x_suspend(struct platform_device *pdev, pm_message_t state)
        struct snd_soc_device *socdev = platform_get_drvdata(pdev);
        struct snd_soc_codec *codec = socdev->codec;
 
-       aic3x_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
+       aic3x_set_bias_level(codec, SND_SOC_BIAS_OFF);
 
        return 0;
 }
@@ -1008,7 +1003,7 @@ static int aic3x_resume(struct platform_device *pdev)
                codec->hw_write(codec->control_data, data, 2);
        }
 
-       aic3x_dapm_event(codec, codec->suspend_dapm_state);
+       aic3x_set_bias_level(codec, codec->suspend_bias_level);
 
        return 0;
 }
@@ -1020,16 +1015,17 @@ static int aic3x_resume(struct platform_device *pdev)
 static int aic3x_init(struct snd_soc_device *socdev)
 {
        struct snd_soc_codec *codec = socdev->codec;
+       struct aic3x_setup_data *setup = socdev->codec_data;
        int reg, ret = 0;
 
        codec->name = "aic3x";
        codec->owner = THIS_MODULE;
        codec->read = aic3x_read_reg_cache;
        codec->write = aic3x_write;
-       codec->dapm_event = aic3x_dapm_event;
+       codec->set_bias_level = aic3x_set_bias_level;
        codec->dai = &aic3x_dai;
        codec->num_dai = 1;
-       codec->reg_cache_size = sizeof(aic3x_reg);
+       codec->reg_cache_size = ARRAY_SIZE(aic3x_reg);
        codec->reg_cache = kmemdup(aic3x_reg, sizeof(aic3x_reg), GFP_KERNEL);
        if (codec->reg_cache == NULL)
                return -ENOMEM;
@@ -1108,7 +1104,11 @@ static int aic3x_init(struct snd_soc_device *socdev)
        aic3x_write(codec, LINE2R_2_MONOLOPM_VOL, DEFAULT_VOL);
 
        /* off, with power on */
-       aic3x_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
+       aic3x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
+
+       /* setup GPIO functions */
+       aic3x_write(codec, AIC3X_GPIO1_REG, (setup->gpio_func[0] & 0xf) << 4);
+       aic3x_write(codec, AIC3X_GPIO2_REG, (setup->gpio_func[1] & 0xf) << 4);
 
        aic3x_add_controls(codec);
        aic3x_add_widgets(codec);
@@ -1217,6 +1217,12 @@ static struct i2c_client client_template = {
        .name = "AIC3X",
        .driver = &aic3x_i2c_driver,
 };
+
+static int aic3x_i2c_read(struct i2c_client *client, u8 *value, int len)
+{
+       value[0] = i2c_smbus_read_byte_data(client, value[0]);
+       return (len == 1);
+}
 #endif
 
 static int aic3x_probe(struct platform_device *pdev)
@@ -1251,6 +1257,7 @@ static int aic3x_probe(struct platform_device *pdev)
        if (setup->i2c_address) {
                normal_i2c[0] = setup->i2c_address;
                codec->hw_write = (hw_write_t) i2c_master_send;
+               codec->hw_read = (hw_read_t) aic3x_i2c_read;
                ret = i2c_add_driver(&aic3x_i2c_driver);
                if (ret != 0)
                        printk(KERN_ERR "can't add i2c driver");
@@ -1268,7 +1275,7 @@ static int aic3x_remove(struct platform_device *pdev)
 
        /* power down chip */
        if (codec->control_data)
-               aic3x_dapm_event(codec, SNDRV_CTL_POWER_D3);
+               aic3x_set_bias_level(codec, SND_SOC_BIAS_OFF);
 
        snd_soc_free_pcms(socdev);
        snd_soc_dapm_free(socdev);