V4L/DVB (13089): cx25840: Separate set_audclk_freq functionality for the different...
[pandora-kernel.git] / drivers / media / video / cx25840 / cx25840-audio.c
index 2f846f5..45608d5 100644 (file)
 
 #include "cx25840-core.h"
 
-static int set_audclk_freq(struct i2c_client *client, u32 freq)
+/*
+ * Note: The PLL and SRC parameters are based on a reference frequency that
+ * would ideally be:
+ *
+ * NTSC Color subcarrier freq * 8 = 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
+ *
+ * However, it's not the exact reference frequency that matters, only that the
+ * firmware and modules that comprise the driver for a particular board all
+ * use the same value (close to the ideal value).
+ *
+ * Comments below will note which reference frequency is assumed for various
+ * parameters.  They will usually be one of
+ *
+ *     ref_freq = 28.636360 MHz
+ *             or
+ *     ref_freq = 28.636363 MHz
+ */
+
+static int cx25840_set_audclk_freq(struct i2c_client *client, u32 freq)
 {
        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
 
-       if (freq != 32000 && freq != 44100 && freq != 48000)
-               return -EINVAL;
-
-       /* common for all inputs and rates */
-       /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
-       if (!state->is_cx23885 && !state->is_cx231xx)
-               cx25840_write(client, 0x127, 0x50);
-
        if (state->aud_input != CX25840_AUDIO_SERIAL) {
                switch (freq) {
                case 32000:
-                       if (state->is_cx23885) {
-                               /* We don't have register values
-                                * so avoid destroying registers. */
-                               break;
-                       }
-
-                       if (!state->is_cx231xx) {
-                               /* VID_PLL and AUX_PLL */
-                               cx25840_write4(client, 0x108, 0x1006040f);
-
-                               /* AUX_PLL_FRAC */
-                               cx25840_write4(client, 0x110, 0x01bb39ee);
-                       }
-
-                       if (state->is_cx25836)
+                       /*
+                        * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
+                        * AUX_PLL Integer = 0x06, AUX PLL Post Divider = 0x10
+                        */
+                       cx25840_write4(client, 0x108, 0x1006040f);
+
+                       /*
+                        * VID_PLL Fraction (register 0x10c) = 0x2be2fe
+                        * 28636360 * 0xf.15f17f0/4 = 108 MHz
+                        * 432 MHz pre-postdivide
+                        */
+
+                       /*
+                        * AUX_PLL Fraction = 0x1bb39ee
+                        * 28636363 * 0x6.dd9cf70/0x10 = 32000 * 384
+                        * 196.6 MHz pre-postdivide
+                        * FIXME < 200 MHz is out of specified valid range
+                        * FIXME 28636363 ref_freq doesn't match VID PLL ref
+                        */
+                       cx25840_write4(client, 0x110, 0x01bb39ee);
+
+                       /*
+                        * SA_MCLK_SEL = 1
+                        * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
+                        */
+                       cx25840_write(client, 0x127, 0x50);
+
+                       if (is_cx2583x(state))
                                break;
 
-                       /* src3/4/6_ctl = 0x0801f77f */
+                       /* src3/4/6_ctl */
+                       /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
                        cx25840_write4(client, 0x900, 0x0801f77f);
                        cx25840_write4(client, 0x904, 0x0801f77f);
                        cx25840_write4(client, 0x90c, 0x0801f77f);
                        break;
 
                case 44100:
-                       if (state->is_cx23885) {
-                               /* We don't have register values
-                                * so avoid destroying registers. */
+                       /*
+                        * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
+                        * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x10
+                        */
+                       cx25840_write4(client, 0x108, 0x1009040f);
+
+                       /*
+                        * VID_PLL Fraction (register 0x10c) = 0x2be2fe
+                        * 28636360 * 0xf.15f17f0/4 = 108 MHz
+                        * 432 MHz pre-postdivide
+                        */
+
+                       /*
+                        * AUX_PLL Fraction = 0x0ec6bd6
+                        * 28636363 * 0x9.7635eb0/0x10 = 44100 * 384
+                        * 271 MHz pre-postdivide
+                        * FIXME 28636363 ref_freq doesn't match VID PLL ref
+                        */
+                       cx25840_write4(client, 0x110, 0x00ec6bd6);
+
+                       /*
+                        * SA_MCLK_SEL = 1
+                        * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
+                        */
+                       cx25840_write(client, 0x127, 0x50);
+
+                       if (is_cx2583x(state))
                                break;
-                       }
-
-                       if (!state->is_cx231xx) {
-                               /* VID_PLL and AUX_PLL */
-                               cx25840_write4(client, 0x108, 0x1009040f);
-
-                               /* AUX_PLL_FRAC */
-                               cx25840_write4(client, 0x110, 0x00ec6bd6);
-                       }
 
-                       if (state->is_cx25836)
-                               break;
-
-                       /* src3/4/6_ctl = 0x08016d59 */
+                       /* src3/4/6_ctl */
+                       /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
                        cx25840_write4(client, 0x900, 0x08016d59);
                        cx25840_write4(client, 0x904, 0x08016d59);
                        cx25840_write4(client, 0x90c, 0x08016d59);
                        break;
 
                case 48000:
-                       if (state->is_cx23885) {
-                               /* We don't have register values
-                                * so avoid destroying registers. */
-                               break;
-                       }
-
-                       if (!state->is_cx231xx) {
-                               /* VID_PLL and AUX_PLL */
-                               cx25840_write4(client, 0x108, 0x100a040f);
-
-                               /* AUX_PLL_FRAC */
-                               cx25840_write4(client, 0x110, 0x0098d6e5);
-                       }
-
-                       if (state->is_cx25836)
+                       /*
+                        * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
+                        * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x10
+                        */
+                       cx25840_write4(client, 0x108, 0x100a040f);
+
+                       /*
+                        * VID_PLL Fraction (register 0x10c) = 0x2be2fe
+                        * 28636360 * 0xf.15f17f0/4 = 108 MHz
+                        * 432 MHz pre-postdivide
+                        */
+
+                       /*
+                        * AUX_PLL Fraction = 0x098d6e5
+                        * 28636363 * 0xa.4c6b728/0x10 = 48000 * 384
+                        * 295 MHz pre-postdivide
+                        * FIXME 28636363 ref_freq doesn't match VID PLL ref
+                        */
+                       cx25840_write4(client, 0x110, 0x0098d6e5);
+
+                       /*
+                        * SA_MCLK_SEL = 1
+                        * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
+                        */
+                       cx25840_write(client, 0x127, 0x50);
+
+                       if (is_cx2583x(state))
                                break;
 
-                       /* src3/4/6_ctl = 0x08014faa */
+                       /* src3/4/6_ctl */
+                       /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
                        cx25840_write4(client, 0x900, 0x08014faa);
                        cx25840_write4(client, 0x904, 0x08014faa);
                        cx25840_write4(client, 0x90c, 0x08014faa);
@@ -112,91 +162,249 @@ static int set_audclk_freq(struct i2c_client *client, u32 freq)
        } else {
                switch (freq) {
                case 32000:
-                       if (state->is_cx23885) {
-                               /* We don't have register values
-                                * so avoid destroying registers. */
-                               break;
-                       }
-
-                       if (!state->is_cx231xx) {
-                               /* VID_PLL and AUX_PLL */
-                               cx25840_write4(client, 0x108, 0x1e08040f);
-
-                               /* AUX_PLL_FRAC */
-                               cx25840_write4(client, 0x110, 0x012a0869);
-                       }
+                       /*
+                        * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
+                        * AUX_PLL Integer = 0x08, AUX PLL Post Divider = 0x1e
+                        */
+                       cx25840_write4(client, 0x108, 0x1e08040f);
+
+                       /*
+                        * VID_PLL Fraction (register 0x10c) = 0x2be2fe
+                        * 28636360 * 0xf.15f17f0/4 = 108 MHz
+                        * 432 MHz pre-postdivide
+                        */
+
+                       /*
+                        * AUX_PLL Fraction = 0x12a0869
+                        * 28636363 * 0x8.9504348/0x1e = 32000 * 256
+                        * 246 MHz pre-postdivide
+                        * FIXME 28636363 ref_freq doesn't match VID PLL ref
+                        */
+                       cx25840_write4(client, 0x110, 0x012a0869);
+
+                       /*
+                        * SA_MCLK_SEL = 1
+                        * SA_MCLK_DIV = 0x14 = 256/384 * AUX_PLL post dvivider
+                        */
+                       cx25840_write(client, 0x127, 0x54);
 
-                       if (state->is_cx25836)
+                       if (is_cx2583x(state))
                                break;
 
-                       /* src1_ctl = 0x08010000 */
+                       /* src1_ctl */
+                       /* 0x1.0000 = 32000/32000 */
                        cx25840_write4(client, 0x8f8, 0x08010000);
 
-                       /* src3/4/6_ctl = 0x08020000 */
+                       /* src3/4/6_ctl */
+                       /* 0x2.0000 = 2 * (32000/32000) */
                        cx25840_write4(client, 0x900, 0x08020000);
                        cx25840_write4(client, 0x904, 0x08020000);
                        cx25840_write4(client, 0x90c, 0x08020000);
-
-                       /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
-                       cx25840_write(client, 0x127, 0x54);
                        break;
 
                case 44100:
-                       if (state->is_cx23885) {
-                               /* We don't have register values
-                                * so avoid destroying registers. */
-                               break;
-                       }
-
-
-                       if (!state->is_cx231xx) {
-                               /* VID_PLL and AUX_PLL */
-                               cx25840_write4(client, 0x108, 0x1809040f);
-
-                               /* AUX_PLL_FRAC */
-                               cx25840_write4(client, 0x110, 0x00ec6bd6);
-                       }
-
-                       if (state->is_cx25836)
+                       /*
+                        * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
+                        * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x18
+                        */
+                       cx25840_write4(client, 0x108, 0x1809040f);
+
+                       /*
+                        * VID_PLL Fraction (register 0x10c) = 0x2be2fe
+                        * 28636360 * 0xf.15f17f0/4 = 108 MHz
+                        * 432 MHz pre-postdivide
+                        */
+
+                       /*
+                        * AUX_PLL Fraction = 0x0ec6bd6
+                        * 28636363 * 0x9.7635eb0/0x18 = 44100 * 256
+                        * 271 MHz pre-postdivide
+                        * FIXME 28636363 ref_freq doesn't match VID PLL ref
+                        */
+                       cx25840_write4(client, 0x110, 0x00ec6bd6);
+
+                       /*
+                        * SA_MCLK_SEL = 1
+                        * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
+                        */
+                       cx25840_write(client, 0x127, 0x50);
+
+                       if (is_cx2583x(state))
                                break;
 
-                       /* src1_ctl = 0x08010000 */
+                       /* src1_ctl */
+                       /* 0x1.60cd = 44100/32000 */
                        cx25840_write4(client, 0x8f8, 0x080160cd);
 
-                       /* src3/4/6_ctl = 0x08020000 */
+                       /* src3/4/6_ctl */
+                       /* 0x1.7385 = 2 * (32000/44100) */
                        cx25840_write4(client, 0x900, 0x08017385);
                        cx25840_write4(client, 0x904, 0x08017385);
                        cx25840_write4(client, 0x90c, 0x08017385);
                        break;
 
                case 48000:
-                       if (!state->is_cx23885 && !state->is_cx231xx) {
-                               /* VID_PLL and AUX_PLL */
-                               cx25840_write4(client, 0x108, 0x180a040f);
+                       /*
+                        * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
+                        * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x18
+                        */
+                       cx25840_write4(client, 0x108, 0x180a040f);
+
+                       /*
+                        * VID_PLL Fraction (register 0x10c) = 0x2be2fe
+                        * 28636360 * 0xf.15f17f0/4 = 108 MHz
+                        * 432 MHz pre-postdivide
+                        */
+
+                       /*
+                        * AUX_PLL Fraction = 0x098d6e5
+                        * 28636363 * 0xa.4c6b728/0x18 = 48000 * 256
+                        * 295 MHz pre-postdivide
+                        * FIXME 28636363 ref_freq doesn't match VID PLL ref
+                        */
+                       cx25840_write4(client, 0x110, 0x0098d6e5);
+
+                       /*
+                        * SA_MCLK_SEL = 1
+                        * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
+                        */
+                       cx25840_write(client, 0x127, 0x50);
+
+                       if (is_cx2583x(state))
+                               break;
 
-                               /* AUX_PLL_FRAC */
-                               cx25840_write4(client, 0x110, 0x0098d6e5);
-                       }
+                       /* src1_ctl */
+                       /* 0x1.8000 = 48000/32000 */
+                       cx25840_write4(client, 0x8f8, 0x08018000);
 
-                       if (state->is_cx25836)
-                               break;
+                       /* src3/4/6_ctl */
+                       /* 0x1.5555 = 2 * (32000/48000) */
+                       cx25840_write4(client, 0x900, 0x08015555);
+                       cx25840_write4(client, 0x904, 0x08015555);
+                       cx25840_write4(client, 0x90c, 0x08015555);
+                       break;
+               }
+       }
+
+       state->audclk_freq = freq;
+
+       return 0;
+}
+
+static inline int cx25836_set_audclk_freq(struct i2c_client *client, u32 freq)
+{
+       return cx25840_set_audclk_freq(client, freq);
+}
+
+static int cx23885_set_audclk_freq(struct i2c_client *client, u32 freq)
+{
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
+
+       if (state->aud_input != CX25840_AUDIO_SERIAL) {
+               switch (freq) {
+               case 32000:
+               case 44100:
+               case 48000:
+                       /* We don't have register values
+                        * so avoid destroying registers. */
+                       /* FIXME return -EINVAL; */
+                       break;
+               }
+       } else {
+               switch (freq) {
+               case 32000:
+               case 44100:
+                       /* We don't have register values
+                        * so avoid destroying registers. */
+                       /* FIXME return -EINVAL; */
+                       break;
+
+               case 48000:
+                       /* src1_ctl */
+                       /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
+                       cx25840_write4(client, 0x8f8, 0x0801867c);
+
+                       /* src3/4/6_ctl */
+                       /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
+                       cx25840_write4(client, 0x900, 0x08014faa);
+                       cx25840_write4(client, 0x904, 0x08014faa);
+                       cx25840_write4(client, 0x90c, 0x08014faa);
+                       break;
+               }
+       }
+
+       state->audclk_freq = freq;
+
+       return 0;
+}
+
+static int cx231xx_set_audclk_freq(struct i2c_client *client, u32 freq)
+{
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
+
+       if (state->aud_input != CX25840_AUDIO_SERIAL) {
+               switch (freq) {
+               case 32000:
+                       /* src3/4/6_ctl */
+                       /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
+                       cx25840_write4(client, 0x900, 0x0801f77f);
+                       cx25840_write4(client, 0x904, 0x0801f77f);
+                       cx25840_write4(client, 0x90c, 0x0801f77f);
+                       break;
+
+               case 44100:
+                       /* src3/4/6_ctl */
+                       /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
+                       cx25840_write4(client, 0x900, 0x08016d59);
+                       cx25840_write4(client, 0x904, 0x08016d59);
+                       cx25840_write4(client, 0x90c, 0x08016d59);
+                       break;
+
+               case 48000:
+                       /* src3/4/6_ctl */
+                       /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
+                       cx25840_write4(client, 0x900, 0x08014faa);
+                       cx25840_write4(client, 0x904, 0x08014faa);
+                       cx25840_write4(client, 0x90c, 0x08014faa);
+                       break;
+               }
+       } else {
+               switch (freq) {
+               /* FIXME These cases make different assumptions about audclk */
+               case 32000:
+                       /* src1_ctl */
+                       /* 0x1.0000 = 32000/32000 */
+                       cx25840_write4(client, 0x8f8, 0x08010000);
 
-                       if (!state->is_cx23885 && !state->is_cx231xx) {
-                               /* src1_ctl */
-                               cx25840_write4(client, 0x8f8, 0x08018000);
+                       /* src3/4/6_ctl */
+                       /* 0x2.0000 = 2 * (32000/32000) */
+                       cx25840_write4(client, 0x900, 0x08020000);
+                       cx25840_write4(client, 0x904, 0x08020000);
+                       cx25840_write4(client, 0x90c, 0x08020000);
+                       break;
 
-                               /* src3/4/6_ctl */
-                               cx25840_write4(client, 0x900, 0x08015555);
-                               cx25840_write4(client, 0x904, 0x08015555);
-                               cx25840_write4(client, 0x90c, 0x08015555);
-                       } else {
+               case 44100:
+                       /* src1_ctl */
+                       /* 0x1.60cd = 44100/32000 */
+                       cx25840_write4(client, 0x8f8, 0x080160cd);
 
-                               cx25840_write4(client, 0x8f8, 0x0801867c);
+                       /* src3/4/6_ctl */
+                       /* 0x1.7385 = 2 * (32000/44100) */
+                       cx25840_write4(client, 0x900, 0x08017385);
+                       cx25840_write4(client, 0x904, 0x08017385);
+                       cx25840_write4(client, 0x90c, 0x08017385);
+                       break;
 
-                               cx25840_write4(client, 0x900, 0x08014faa);
-                               cx25840_write4(client, 0x904, 0x08014faa);
-                               cx25840_write4(client, 0x90c, 0x08014faa);
-                       }
+               case 48000:
+                       /* src1_ctl */
+                       /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
+                       cx25840_write4(client, 0x8f8, 0x0801867c);
+
+                       /* src3/4/6_ctl */
+                       /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
+                       cx25840_write4(client, 0x900, 0x08014faa);
+                       cx25840_write4(client, 0x904, 0x08014faa);
+                       cx25840_write4(client, 0x90c, 0x08014faa);
                        break;
                }
        }
@@ -206,6 +414,25 @@ static int set_audclk_freq(struct i2c_client *client, u32 freq)
        return 0;
 }
 
+static int set_audclk_freq(struct i2c_client *client, u32 freq)
+{
+       struct cx25840_state *state = to_state(i2c_get_clientdata(client));
+
+       if (freq != 32000 && freq != 44100 && freq != 48000)
+               return -EINVAL;
+
+       if (is_cx231xx(state))
+               return cx231xx_set_audclk_freq(client, freq);
+
+       if (is_cx2388x(state))
+               return cx23885_set_audclk_freq(client, freq);
+
+       if (is_cx2583x(state))
+               return cx25836_set_audclk_freq(client, freq);
+
+       return cx25840_set_audclk_freq(client, freq);
+}
+
 void cx25840_audio_set_path(struct i2c_client *client)
 {
        struct cx25840_state *state = to_state(i2c_get_clientdata(client));
@@ -243,7 +470,7 @@ void cx25840_audio_set_path(struct i2c_client *client)
        cx25840_and_or(client, 0x810, ~0x1, 0x00);
 
        /* Ensure the controller is running when we exit */
-       if (state->is_cx23885 || state->is_cx231xx)
+       if (is_cx2388x(state) || is_cx231xx(state))
                cx25840_and_or(client, 0x803, ~0x10, 0x10);
 }
 
@@ -383,7 +610,7 @@ int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
        struct cx25840_state *state = to_state(sd);
        int retval;
 
-       if (!state->is_cx25836)
+       if (!is_cx2583x(state))
                cx25840_and_or(client, 0x810, ~0x1, 1);
        if (state->aud_input != CX25840_AUDIO_SERIAL) {
                cx25840_and_or(client, 0x803, ~0x10, 0);
@@ -392,7 +619,7 @@ int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
        retval = set_audclk_freq(client, freq);
        if (state->aud_input != CX25840_AUDIO_SERIAL)
                cx25840_and_or(client, 0x803, ~0x10, 0x10);
-       if (!state->is_cx25836)
+       if (!is_cx2583x(state))
                cx25840_and_or(client, 0x810, ~0x1, 0);
        return retval;
 }