[media] Add proper audio support for Nova-S Plus with wm8775 ADC
[linux-2.6.git] / drivers / media / video / cx88 / cx88-core.c
index 0765e9d..2e145f0 100644 (file)
@@ -28,7 +28,6 @@
 #include <linux/init.h>
 #include <linux/list.h>
 #include <linux/module.h>
-#include <linux/moduleparam.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/kmod.h>
@@ -41,6 +40,7 @@
 
 #include "cx88.h"
 #include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
 
 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
@@ -48,15 +48,15 @@ MODULE_LICENSE("GPL");
 
 /* ------------------------------------------------------------------ */
 
-static unsigned int core_debug = 0;
+static unsigned int core_debug;
 module_param(core_debug,int,0644);
 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
 
-static unsigned int nicam = 0;
+static unsigned int nicam;
 module_param(nicam,int,0644);
 MODULE_PARM_DESC(nicam,"tv audio is nicam");
 
-static unsigned int nocomb = 0;
+static unsigned int nocomb;
 module_param(nocomb,int,0644);
 MODULE_PARM_DESC(nocomb,"disable comb filter");
 
@@ -69,13 +69,15 @@ static DEFINE_MUTEX(devlist);
 
 #define NO_SYNC_LINE (-1U)
 
-static u32* cx88_risc_field(u32 *rp, struct scatterlist *sglist,
+/* @lpi: lines per IRQ, or 0 to not generate irqs. Note: IRQ to be
+        generated _after_ lpi lines are transferred. */
+static __le32* cx88_risc_field(__le32 *rp, struct scatterlist *sglist,
                            unsigned int offset, u32 sync_line,
                            unsigned int bpl, unsigned int padding,
-                           unsigned int lines)
+                           unsigned int lines, unsigned int lpi)
 {
        struct scatterlist *sg;
-       unsigned int line,todo;
+       unsigned int line,todo,sol;
 
        /* sync instruction */
        if (sync_line != NO_SYNC_LINE)
@@ -88,15 +90,19 @@ static u32* cx88_risc_field(u32 *rp, struct scatterlist *sglist,
                        offset -= sg_dma_len(sg);
                        sg++;
                }
+               if (lpi && line>0 && !(line % lpi))
+                       sol = RISC_SOL | RISC_IRQ1 | RISC_CNT_INC;
+               else
+                       sol = RISC_SOL;
                if (bpl <= sg_dma_len(sg)-offset) {
                        /* fits into current chunk */
-                       *(rp++)=cpu_to_le32(RISC_WRITE|RISC_SOL|RISC_EOL|bpl);
+                       *(rp++)=cpu_to_le32(RISC_WRITE|sol|RISC_EOL|bpl);
                        *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
                        offset+=bpl;
                } else {
                        /* scanline needs to be split */
                        todo = bpl;
-                       *(rp++)=cpu_to_le32(RISC_WRITE|RISC_SOL|
+                       *(rp++)=cpu_to_le32(RISC_WRITE|sol|
                                            (sg_dma_len(sg)-offset));
                        *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
                        todo -= (sg_dma_len(sg)-offset);
@@ -125,7 +131,7 @@ int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
                     unsigned int bpl, unsigned int padding, unsigned int lines)
 {
        u32 instructions,fields;
-       u32 *rp;
+       __le32 *rp;
        int rc;
 
        fields = 0;
@@ -147,10 +153,10 @@ int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
        rp = risc->cpu;
        if (UNSET != top_offset)
                rp = cx88_risc_field(rp, sglist, top_offset, 0,
-                                    bpl, padding, lines);
+                                    bpl, padding, lines, 0);
        if (UNSET != bottom_offset)
                rp = cx88_risc_field(rp, sglist, bottom_offset, 0x200,
-                                    bpl, padding, lines);
+                                    bpl, padding, lines, 0);
 
        /* save pointer to jmp instruction address */
        risc->jmp = rp;
@@ -160,10 +166,10 @@ int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
 
 int cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
                         struct scatterlist *sglist, unsigned int bpl,
-                        unsigned int lines)
+                        unsigned int lines, unsigned int lpi)
 {
        u32 instructions;
-       u32 *rp;
+       __le32 *rp;
        int rc;
 
        /* estimate risc mem: worst case is one write per page border +
@@ -177,7 +183,7 @@ int cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
 
        /* write risc instructions */
        rp = risc->cpu;
-       rp = cx88_risc_field(rp, sglist, 0, NO_SYNC_LINE, bpl, 0, lines);
+       rp = cx88_risc_field(rp, sglist, 0, NO_SYNC_LINE, bpl, 0, lines, lpi);
 
        /* save pointer to jmp instruction address */
        risc->jmp = rp;
@@ -188,7 +194,7 @@ int cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
 int cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
                      u32 reg, u32 mask, u32 value)
 {
-       u32 *rp;
+       __le32 *rp;
        int rc;
 
        if ((rc = btcx_riscmem_alloc(pci, risc, 4*16)) < 0)
@@ -208,12 +214,14 @@ int cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
 void
 cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf)
 {
+       struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
+
        BUG_ON(in_interrupt());
-       videobuf_waiton(&buf->vb,0,0);
-       videobuf_dma_unmap(q, &buf->vb.dma);
-       videobuf_dma_free(&buf->vb.dma);
-       btcx_riscmem_free((struct pci_dev *)q->dev, &buf->risc);
-       buf->vb.state = STATE_NEEDS_INIT;
+       videobuf_waiton(q, &buf->vb, 0, 0);
+       videobuf_dma_unmap(q->dev, dma);
+       videobuf_dma_free(dma);
+       btcx_riscmem_free(to_pci_dev(q->dev), &buf->risc);
+       buf->vb.state = VIDEOBUF_NEEDS_INIT;
 }
 
 /* ------------------------------------------------------------------ */
@@ -223,7 +231,7 @@ cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf)
  * can use the whole SDRAM for the DMA fifos.  To simplify things, we
  * use a static memory layout.  That surely will waste memory in case
  * we don't use all DMA channels at the same time (which will be the
- * case most of the time).  But that still gives us enougth FIFO space
+ * case most of the time).  But that still gives us enough FIFO space
  * to be able to deal with insane long pci latencies ...
  *
  * FIFO space allocations:
@@ -233,6 +241,7 @@ cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf)
  *    channel  24    (vbi)      -  4.0k
  *    channels 25+26 (audio)    -  4.0k
  *    channel  28    (mpeg)     -  4.0k
+ *    channel  27    (audio rds)-  3.0k
  *    TOTAL                     = 29.0k
  *
  * Every channel has 160 bytes control data (64 bytes instruction
@@ -244,7 +253,7 @@ cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf)
  *    0x0c00 -           FIFOs
  */
 
-struct sram_channel cx88_sram_channels[] = {
+const struct sram_channel const cx88_sram_channels[] = {
        [SRAM_CH21] = {
                .name       = "video y / packed",
                .cmds_start = 0x180040,
@@ -329,10 +338,22 @@ struct sram_channel cx88_sram_channels[] = {
                .cnt1_reg   = MO_DMA28_CNT1,
                .cnt2_reg   = MO_DMA28_CNT2,
        },
+       [SRAM_CH27] = {
+               .name       = "audio rds",
+               .cmds_start = 0x1801C0,
+               .ctrl_start = 0x180860,
+               .cdt        = 0x180860 + 64,
+               .fifo_start = 0x187400,
+               .fifo_size  = 0x000C00,
+               .ptr1_reg   = MO_DMA27_PTR1,
+               .ptr2_reg   = MO_DMA27_PTR2,
+               .cnt1_reg   = MO_DMA27_CNT1,
+               .cnt2_reg   = MO_DMA27_CNT2,
+       },
 };
 
 int cx88_sram_channel_setup(struct cx88_core *core,
-                           struct sram_channel *ch,
+                           const struct sram_channel *ch,
                            unsigned int bpl, u32 risc)
 {
        unsigned int i,lines;
@@ -373,7 +394,7 @@ int cx88_sram_channel_setup(struct cx88_core *core,
 
 static int cx88_risc_decode(u32 risc)
 {
-       static char *instr[16] = {
+       static const char * const instr[16] = {
                [ RISC_SYNC    >> 28 ] = "sync",
                [ RISC_WRITE   >> 28 ] = "write",
                [ RISC_WRITEC  >> 28 ] = "writec",
@@ -385,14 +406,14 @@ static int cx88_risc_decode(u32 risc)
                [ RISC_WRITECM >> 28 ] = "writecm",
                [ RISC_WRITECR >> 28 ] = "writecr",
        };
-       static int incr[16] = {
+       static int const incr[16] = {
                [ RISC_WRITE   >> 28 ] = 2,
                [ RISC_JUMP    >> 28 ] = 2,
                [ RISC_WRITERM >> 28 ] = 3,
                [ RISC_WRITECM >> 28 ] = 3,
                [ RISC_WRITECR >> 28 ] = 4,
        };
-       static char *bits[] = {
+       static const char * const bits[] = {
                "12",   "13",   "14",   "resync",
                "cnt0", "cnt1", "18",   "19",
                "20",   "21",   "22",   "23",
@@ -411,9 +432,9 @@ static int cx88_risc_decode(u32 risc)
 
 
 void cx88_sram_channel_dump(struct cx88_core *core,
-                           struct sram_channel *ch)
+                           const struct sram_channel *ch)
 {
-       static char *name[] = {
+       static const char * const name[] = {
                "initial risc",
                "cdt base",
                "cdt size",
@@ -435,10 +456,13 @@ void cx88_sram_channel_dump(struct cx88_core *core,
                printk("%s:   cmds: %-12s: 0x%08x\n",
                       core->name,name[i],
                       cx_read(ch->cmds_start + 4*i));
-       for (i = 0; i < 4; i++) {
+       for (n = 1, i = 0; i < 4; i++) {
                risc = cx_read(ch->cmds_start + 4 * (i+11));
                printk("%s:   risc%d: ", core->name, i);
-               cx88_risc_decode(risc);
+               if (--n)
+                       printk("0x%08x [ arg #%d ]\n", risc, n);
+               else
+                       n = cx88_risc_decode(risc);
        }
        for (i = 0; i < 16; i += n) {
                risc = cx_read(ch->ctrl_start + 4 * i);
@@ -465,14 +489,14 @@ void cx88_sram_channel_dump(struct cx88_core *core,
               core->name,cx_read(ch->cnt2_reg));
 }
 
-static char *cx88_pci_irqs[32] = {
+static const char *cx88_pci_irqs[32] = {
        "vid", "aud", "ts", "vip", "hst", "5", "6", "tm1",
        "src_dma", "dst_dma", "risc_rd_err", "risc_wr_err",
        "brdg_err", "src_dma_err", "dst_dma_err", "ipb_dma_err",
        "i2c", "i2c_rack", "ir_smp", "gpio0", "gpio1"
 };
 
-void cx88_print_irqbits(char *name, char *tag, char **strings,
+void cx88_print_irqbits(const char *name, const char *tag, const char *strings[],
                        int len, u32 bits, u32 mask)
 {
        unsigned int i;
@@ -498,7 +522,7 @@ int cx88_core_irq(struct cx88_core *core, u32 status)
 {
        int handled = 0;
 
-       if (status & (1<<18)) {
+       if (status & PCI_INT_IR_SMPINT) {
                cx88_ir_irq(core);
                handled++;
        }
@@ -528,7 +552,7 @@ void cx88_wakeup(struct cx88_core *core,
                do_gettimeofday(&buf->vb.ts);
                dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i,
                        count, buf->count);
-               buf->vb.state = STATE_DONE;
+               buf->vb.state = VIDEOBUF_DONE;
                list_del(&buf->vb.queue);
                wake_up(&buf->vb.done);
        }
@@ -538,7 +562,8 @@ void cx88_wakeup(struct cx88_core *core,
                mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
        }
        if (bc != 1)
-               printk("%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
+               dprintk(2, "%s: %d buffers handled (should be 1)\n",
+                       __func__, bc);
 }
 
 void cx88_shutdown(struct cx88_core *core)
@@ -567,7 +592,7 @@ void cx88_shutdown(struct cx88_core *core)
 
 int cx88_reset(struct cx88_core *core)
 {
-       dprintk(1,"%s\n",__FUNCTION__);
+       dprintk(1,"%s\n",__func__);
        cx88_shutdown(core);
 
        /* clear irq status */
@@ -586,6 +611,7 @@ int cx88_reset(struct cx88_core *core)
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH25], 128, 0);
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH26], 128, 0);
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28], 188*4, 0);
+       cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH27], 128, 0);
 
        /* misc init ... */
        cx_write(MO_INPUT_FORMAT, ((1 << 13) |   // agc enable
@@ -598,7 +624,7 @@ int cx88_reset(struct cx88_core *core)
        /* setup image format */
        cx_andor(MO_COLOR_CTRL, 0x4000, 0x4000);
 
-       /* setup FIFO Threshholds */
+       /* setup FIFO Thresholds */
        cx_write(MO_PDMA_STHRSH,   0x0807);
        cx_write(MO_PDMA_DTHRSH,   0x0807);
 
@@ -744,7 +770,7 @@ static const u32 xtal = 28636363;
 
 static int set_pll(struct cx88_core *core, int prescale, u32 ofreq)
 {
-       static u32 pre[] = { 0, 0, 0, 3, 2, 1 };
+       static const u32 pre[] = { 0, 0, 0, 3, 2, 1 };
        u64 pll;
        u32 reg;
        int i;
@@ -784,6 +810,8 @@ int cx88_start_audio_dma(struct cx88_core *core)
        /* constant 128 made buzz in analog Nicam-stereo for bigger fifo_size */
        int bpl = cx88_sram_channels[SRAM_CH25].fifo_size/4;
 
+       int rds_bpl = cx88_sram_channels[SRAM_CH27].fifo_size/AUD_RDS_LINES;
+
        /* If downstream RISC is enabled, bail out; ALSA is managing DMA */
        if (cx_read(MO_AUD_DMACNTRL) & 0x10)
                return 0;
@@ -791,12 +819,14 @@ int cx88_start_audio_dma(struct cx88_core *core)
        /* setup fifo + format */
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH25], bpl, 0);
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH26], bpl, 0);
+       cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH27],
+                               rds_bpl, 0);
 
        cx_write(MO_AUDD_LNGTH, bpl); /* fifo bpl size */
-       cx_write(MO_AUDR_LNGTH, bpl); /* fifo bpl size */
+       cx_write(MO_AUDR_LNGTH, rds_bpl); /* fifo bpl size */
 
-       /* start dma */
-       cx_write(MO_AUD_DMACNTRL, 0x0003); /* Up and Down fifo enable */
+       /* enable Up, Down and Audio RDS fifo */
+       cx_write(MO_AUD_DMACNTRL, 0x0007);
 
        return 0;
 }
@@ -817,7 +847,8 @@ static int set_tvaudio(struct cx88_core *core)
 {
        v4l2_std_id norm = core->tvnorm;
 
-       if (CX88_VMUX_TELEVISION != INPUT(core->input).type)
+       if (CX88_VMUX_TELEVISION != INPUT(core->input).type &&
+           CX88_VMUX_CABLE != INPUT(core->input).type)
                return 0;
 
        if (V4L2_STD_PAL_BG & norm) {
@@ -832,6 +863,9 @@ static int set_tvaudio(struct cx88_core *core)
        } else if (V4L2_STD_SECAM_L & norm) {
                core->tvaudio = WW_L;
 
+       } else if ((V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H) & norm) {
+               core->tvaudio = WW_BG;
+
        } else if (V4L2_STD_SECAM_DK & norm) {
                core->tvaudio = WW_DK;
 
@@ -845,7 +879,7 @@ static int set_tvaudio(struct cx88_core *core)
        } else {
                printk("%s/0: tvaudio support needs work for this tv norm [%s], sorry\n",
                       core->name, v4l2_norm_to_name(core->tvnorm));
-               core->tvaudio = 0;
+               core->tvaudio = WW_NONE;
                return 0;
        }
 
@@ -919,7 +953,10 @@ int cx88_set_tvnorm(struct cx88_core *core, v4l2_std_id norm)
 
        dprintk(1,"set_tvnorm: MO_INPUT_FORMAT  0x%08x [old=0x%08x]\n",
                cxiformat, cx_read(MO_INPUT_FORMAT) & 0x0f);
-       cx_andor(MO_INPUT_FORMAT, 0xf, cxiformat);
+       /* Chroma AGC must be disabled if SECAM is used, we enable it
+          by default on PAL and NTSC */
+       cx_andor(MO_INPUT_FORMAT, 0x40f,
+                norm & V4L2_STD_SECAM ? cxiformat : cxiformat | 0x400);
 
        // FIXME: as-is from DScaler
        dprintk(1,"set_tvnorm: MO_OUTPUT_FORMAT 0x%08x [old=0x%08x]\n",
@@ -973,7 +1010,7 @@ int cx88_set_tvnorm(struct cx88_core *core, v4l2_std_id norm)
        set_tvaudio(core);
 
        // tell i2c chips
-       cx88_call_i2c_clients(core,VIDIOC_S_STD,&norm);
+       call_all(core, core, s_std, norm);
 
        // done
        return 0;
@@ -983,17 +1020,17 @@ int cx88_set_tvnorm(struct cx88_core *core, v4l2_std_id norm)
 
 struct video_device *cx88_vdev_init(struct cx88_core *core,
                                    struct pci_dev *pci,
-                                   struct video_device *template,
-                                   char *type)
+                                   const struct video_device *template_,
+                                   const char *type)
 {
        struct video_device *vfd;
 
        vfd = video_device_alloc();
        if (NULL == vfd)
                return NULL;
-       *vfd = *template;
-       vfd->minor   = -1;
-       vfd->dev     = &pci->dev;
+       *vfd = *template_;
+       vfd->v4l2_dev = &core->v4l2_dev;
+       vfd->parent = &pci->dev;
        vfd->release = video_device_release;
        snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
                 core->name, type, core->board.name);
@@ -1003,11 +1040,9 @@ struct video_device *cx88_vdev_init(struct cx88_core *core,
 struct cx88_core* cx88_core_get(struct pci_dev *pci)
 {
        struct cx88_core *core;
-       struct list_head *item;
 
        mutex_lock(&devlist);
-       list_for_each(item,&cx88_devlist) {
-               core = list_entry(item, struct cx88_core, devlist);
+       list_for_each_entry(core, &cx88_devlist, devlist) {
                if (pci->bus->number != core->pci_bus)
                        continue;
                if (PCI_SLOT(pci->devfn) != core->pci_slot)
@@ -1042,12 +1077,16 @@ void cx88_core_put(struct cx88_core *core, struct pci_dev *pci)
 
        mutex_lock(&devlist);
        cx88_ir_fini(core);
-       if (0 == core->i2c_rc)
+       if (0 == core->i2c_rc) {
+               if (core->i2c_rtc)
+                       i2c_unregister_device(core->i2c_rtc);
                i2c_del_adapter(&core->i2c_adap);
+       }
        list_del(&core->devlist);
        iounmap(core->lmmio);
        cx88_devcount--;
        mutex_unlock(&devlist);
+       v4l2_device_unregister(&core->v4l2_dev);
        kfree(core);
 }
 
@@ -1076,6 +1115,9 @@ EXPORT_SYMBOL(cx88_vdev_init);
 EXPORT_SYMBOL(cx88_core_get);
 EXPORT_SYMBOL(cx88_core_put);
 
+EXPORT_SYMBOL(cx88_ir_start);
+EXPORT_SYMBOL(cx88_ir_stop);
+
 /*
  * Local variables:
  * c-basic-offset: 8