Path: blob/master/drivers/media/video/cx88/cx88-core.c
17687 views
/*1*2* device driver for Conexant 2388x based TV cards3* driver core4*5* (c) 2003 Gerd Knorr <[email protected]> [SuSE Labs]6*7* (c) 2005-2006 Mauro Carvalho Chehab <[email protected]>8* - Multituner support9* - video_ioctl2 conversion10* - PAL/M fixes11*12* This program is free software; you can redistribute it and/or modify13* it under the terms of the GNU General Public License as published by14* the Free Software Foundation; either version 2 of the License, or15* (at your option) any later version.16*17* This program is distributed in the hope that it will be useful,18* but WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20* GNU General Public License for more details.21*22* You should have received a copy of the GNU General Public License23* along with this program; if not, write to the Free Software24* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.25*/2627#include <linux/init.h>28#include <linux/list.h>29#include <linux/module.h>30#include <linux/kernel.h>31#include <linux/slab.h>32#include <linux/kmod.h>33#include <linux/sound.h>34#include <linux/interrupt.h>35#include <linux/pci.h>36#include <linux/delay.h>37#include <linux/videodev2.h>38#include <linux/mutex.h>3940#include "cx88.h"41#include <media/v4l2-common.h>42#include <media/v4l2-ioctl.h>4344MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");45MODULE_AUTHOR("Gerd Knorr <[email protected]> [SuSE Labs]");46MODULE_LICENSE("GPL");4748/* ------------------------------------------------------------------ */4950static unsigned int core_debug;51module_param(core_debug,int,0644);52MODULE_PARM_DESC(core_debug,"enable debug messages [core]");5354static unsigned int nicam;55module_param(nicam,int,0644);56MODULE_PARM_DESC(nicam,"tv audio is nicam");5758static unsigned int nocomb;59module_param(nocomb,int,0644);60MODULE_PARM_DESC(nocomb,"disable comb filter");6162#define dprintk(level,fmt, arg...) if (core_debug >= level) \63printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)6465static unsigned int cx88_devcount;66static LIST_HEAD(cx88_devlist);67static DEFINE_MUTEX(devlist);6869#define NO_SYNC_LINE (-1U)7071/* @lpi: lines per IRQ, or 0 to not generate irqs. Note: IRQ to be72generated _after_ lpi lines are transferred. */73static __le32* cx88_risc_field(__le32 *rp, struct scatterlist *sglist,74unsigned int offset, u32 sync_line,75unsigned int bpl, unsigned int padding,76unsigned int lines, unsigned int lpi)77{78struct scatterlist *sg;79unsigned int line,todo,sol;8081/* sync instruction */82if (sync_line != NO_SYNC_LINE)83*(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);8485/* scan lines */86sg = sglist;87for (line = 0; line < lines; line++) {88while (offset && offset >= sg_dma_len(sg)) {89offset -= sg_dma_len(sg);90sg++;91}92if (lpi && line>0 && !(line % lpi))93sol = RISC_SOL | RISC_IRQ1 | RISC_CNT_INC;94else95sol = RISC_SOL;96if (bpl <= sg_dma_len(sg)-offset) {97/* fits into current chunk */98*(rp++)=cpu_to_le32(RISC_WRITE|sol|RISC_EOL|bpl);99*(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);100offset+=bpl;101} else {102/* scanline needs to be split */103todo = bpl;104*(rp++)=cpu_to_le32(RISC_WRITE|sol|105(sg_dma_len(sg)-offset));106*(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);107todo -= (sg_dma_len(sg)-offset);108offset = 0;109sg++;110while (todo > sg_dma_len(sg)) {111*(rp++)=cpu_to_le32(RISC_WRITE|112sg_dma_len(sg));113*(rp++)=cpu_to_le32(sg_dma_address(sg));114todo -= sg_dma_len(sg);115sg++;116}117*(rp++)=cpu_to_le32(RISC_WRITE|RISC_EOL|todo);118*(rp++)=cpu_to_le32(sg_dma_address(sg));119offset += todo;120}121offset += padding;122}123124return rp;125}126127int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,128struct scatterlist *sglist,129unsigned int top_offset, unsigned int bottom_offset,130unsigned int bpl, unsigned int padding, unsigned int lines)131{132u32 instructions,fields;133__le32 *rp;134int rc;135136fields = 0;137if (UNSET != top_offset)138fields++;139if (UNSET != bottom_offset)140fields++;141142/* estimate risc mem: worst case is one write per page border +143one write per scan line + syncs + jump (all 2 dwords). Padding144can cause next bpl to start close to a page border. First DMA145region may be smaller than PAGE_SIZE */146instructions = fields * (1 + ((bpl + padding) * lines) / PAGE_SIZE + lines);147instructions += 2;148if ((rc = btcx_riscmem_alloc(pci,risc,instructions*8)) < 0)149return rc;150151/* write risc instructions */152rp = risc->cpu;153if (UNSET != top_offset)154rp = cx88_risc_field(rp, sglist, top_offset, 0,155bpl, padding, lines, 0);156if (UNSET != bottom_offset)157rp = cx88_risc_field(rp, sglist, bottom_offset, 0x200,158bpl, padding, lines, 0);159160/* save pointer to jmp instruction address */161risc->jmp = rp;162BUG_ON((risc->jmp - risc->cpu + 2) * sizeof (*risc->cpu) > risc->size);163return 0;164}165166int cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,167struct scatterlist *sglist, unsigned int bpl,168unsigned int lines, unsigned int lpi)169{170u32 instructions;171__le32 *rp;172int rc;173174/* estimate risc mem: worst case is one write per page border +175one write per scan line + syncs + jump (all 2 dwords). Here176there is no padding and no sync. First DMA region may be smaller177than PAGE_SIZE */178instructions = 1 + (bpl * lines) / PAGE_SIZE + lines;179instructions += 1;180if ((rc = btcx_riscmem_alloc(pci,risc,instructions*8)) < 0)181return rc;182183/* write risc instructions */184rp = risc->cpu;185rp = cx88_risc_field(rp, sglist, 0, NO_SYNC_LINE, bpl, 0, lines, lpi);186187/* save pointer to jmp instruction address */188risc->jmp = rp;189BUG_ON((risc->jmp - risc->cpu + 2) * sizeof (*risc->cpu) > risc->size);190return 0;191}192193int cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,194u32 reg, u32 mask, u32 value)195{196__le32 *rp;197int rc;198199if ((rc = btcx_riscmem_alloc(pci, risc, 4*16)) < 0)200return rc;201202/* write risc instructions */203rp = risc->cpu;204*(rp++) = cpu_to_le32(RISC_WRITECR | RISC_IRQ2 | RISC_IMM);205*(rp++) = cpu_to_le32(reg);206*(rp++) = cpu_to_le32(value);207*(rp++) = cpu_to_le32(mask);208*(rp++) = cpu_to_le32(RISC_JUMP);209*(rp++) = cpu_to_le32(risc->dma);210return 0;211}212213void214cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf)215{216struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);217218BUG_ON(in_interrupt());219videobuf_waiton(q, &buf->vb, 0, 0);220videobuf_dma_unmap(q->dev, dma);221videobuf_dma_free(dma);222btcx_riscmem_free(to_pci_dev(q->dev), &buf->risc);223buf->vb.state = VIDEOBUF_NEEDS_INIT;224}225226/* ------------------------------------------------------------------ */227/* our SRAM memory layout */228229/* we are going to put all thr risc programs into host memory, so we230* can use the whole SDRAM for the DMA fifos. To simplify things, we231* use a static memory layout. That surely will waste memory in case232* we don't use all DMA channels at the same time (which will be the233* case most of the time). But that still gives us enough FIFO space234* to be able to deal with insane long pci latencies ...235*236* FIFO space allocations:237* channel 21 (y video) - 10.0k238* channel 22 (u video) - 2.0k239* channel 23 (v video) - 2.0k240* channel 24 (vbi) - 4.0k241* channels 25+26 (audio) - 4.0k242* channel 28 (mpeg) - 4.0k243* channel 27 (audio rds)- 3.0k244* TOTAL = 29.0k245*246* Every channel has 160 bytes control data (64 bytes instruction247* queue and 6 CDT entries), which is close to 2k total.248*249* Address layout:250* 0x0000 - 0x03ff CMDs / reserved251* 0x0400 - 0x0bff instruction queues + CDs252* 0x0c00 - FIFOs253*/254255const struct sram_channel const cx88_sram_channels[] = {256[SRAM_CH21] = {257.name = "video y / packed",258.cmds_start = 0x180040,259.ctrl_start = 0x180400,260.cdt = 0x180400 + 64,261.fifo_start = 0x180c00,262.fifo_size = 0x002800,263.ptr1_reg = MO_DMA21_PTR1,264.ptr2_reg = MO_DMA21_PTR2,265.cnt1_reg = MO_DMA21_CNT1,266.cnt2_reg = MO_DMA21_CNT2,267},268[SRAM_CH22] = {269.name = "video u",270.cmds_start = 0x180080,271.ctrl_start = 0x1804a0,272.cdt = 0x1804a0 + 64,273.fifo_start = 0x183400,274.fifo_size = 0x000800,275.ptr1_reg = MO_DMA22_PTR1,276.ptr2_reg = MO_DMA22_PTR2,277.cnt1_reg = MO_DMA22_CNT1,278.cnt2_reg = MO_DMA22_CNT2,279},280[SRAM_CH23] = {281.name = "video v",282.cmds_start = 0x1800c0,283.ctrl_start = 0x180540,284.cdt = 0x180540 + 64,285.fifo_start = 0x183c00,286.fifo_size = 0x000800,287.ptr1_reg = MO_DMA23_PTR1,288.ptr2_reg = MO_DMA23_PTR2,289.cnt1_reg = MO_DMA23_CNT1,290.cnt2_reg = MO_DMA23_CNT2,291},292[SRAM_CH24] = {293.name = "vbi",294.cmds_start = 0x180100,295.ctrl_start = 0x1805e0,296.cdt = 0x1805e0 + 64,297.fifo_start = 0x184400,298.fifo_size = 0x001000,299.ptr1_reg = MO_DMA24_PTR1,300.ptr2_reg = MO_DMA24_PTR2,301.cnt1_reg = MO_DMA24_CNT1,302.cnt2_reg = MO_DMA24_CNT2,303},304[SRAM_CH25] = {305.name = "audio from",306.cmds_start = 0x180140,307.ctrl_start = 0x180680,308.cdt = 0x180680 + 64,309.fifo_start = 0x185400,310.fifo_size = 0x001000,311.ptr1_reg = MO_DMA25_PTR1,312.ptr2_reg = MO_DMA25_PTR2,313.cnt1_reg = MO_DMA25_CNT1,314.cnt2_reg = MO_DMA25_CNT2,315},316[SRAM_CH26] = {317.name = "audio to",318.cmds_start = 0x180180,319.ctrl_start = 0x180720,320.cdt = 0x180680 + 64, /* same as audio IN */321.fifo_start = 0x185400, /* same as audio IN */322.fifo_size = 0x001000, /* same as audio IN */323.ptr1_reg = MO_DMA26_PTR1,324.ptr2_reg = MO_DMA26_PTR2,325.cnt1_reg = MO_DMA26_CNT1,326.cnt2_reg = MO_DMA26_CNT2,327},328[SRAM_CH28] = {329.name = "mpeg",330.cmds_start = 0x180200,331.ctrl_start = 0x1807C0,332.cdt = 0x1807C0 + 64,333.fifo_start = 0x186400,334.fifo_size = 0x001000,335.ptr1_reg = MO_DMA28_PTR1,336.ptr2_reg = MO_DMA28_PTR2,337.cnt1_reg = MO_DMA28_CNT1,338.cnt2_reg = MO_DMA28_CNT2,339},340[SRAM_CH27] = {341.name = "audio rds",342.cmds_start = 0x1801C0,343.ctrl_start = 0x180860,344.cdt = 0x180860 + 64,345.fifo_start = 0x187400,346.fifo_size = 0x000C00,347.ptr1_reg = MO_DMA27_PTR1,348.ptr2_reg = MO_DMA27_PTR2,349.cnt1_reg = MO_DMA27_CNT1,350.cnt2_reg = MO_DMA27_CNT2,351},352};353354int cx88_sram_channel_setup(struct cx88_core *core,355const struct sram_channel *ch,356unsigned int bpl, u32 risc)357{358unsigned int i,lines;359u32 cdt;360361bpl = (bpl + 7) & ~7; /* alignment */362cdt = ch->cdt;363lines = ch->fifo_size / bpl;364if (lines > 6)365lines = 6;366BUG_ON(lines < 2);367368/* write CDT */369for (i = 0; i < lines; i++)370cx_write(cdt + 16*i, ch->fifo_start + bpl*i);371372/* write CMDS */373cx_write(ch->cmds_start + 0, risc);374cx_write(ch->cmds_start + 4, cdt);375cx_write(ch->cmds_start + 8, (lines*16) >> 3);376cx_write(ch->cmds_start + 12, ch->ctrl_start);377cx_write(ch->cmds_start + 16, 64 >> 2);378for (i = 20; i < 64; i += 4)379cx_write(ch->cmds_start + i, 0);380381/* fill registers */382cx_write(ch->ptr1_reg, ch->fifo_start);383cx_write(ch->ptr2_reg, cdt);384cx_write(ch->cnt1_reg, (bpl >> 3) -1);385cx_write(ch->cnt2_reg, (lines*16) >> 3);386387dprintk(2,"sram setup %s: bpl=%d lines=%d\n", ch->name, bpl, lines);388return 0;389}390391/* ------------------------------------------------------------------ */392/* debug helper code */393394static int cx88_risc_decode(u32 risc)395{396static const char * const instr[16] = {397[ RISC_SYNC >> 28 ] = "sync",398[ RISC_WRITE >> 28 ] = "write",399[ RISC_WRITEC >> 28 ] = "writec",400[ RISC_READ >> 28 ] = "read",401[ RISC_READC >> 28 ] = "readc",402[ RISC_JUMP >> 28 ] = "jump",403[ RISC_SKIP >> 28 ] = "skip",404[ RISC_WRITERM >> 28 ] = "writerm",405[ RISC_WRITECM >> 28 ] = "writecm",406[ RISC_WRITECR >> 28 ] = "writecr",407};408static int const incr[16] = {409[ RISC_WRITE >> 28 ] = 2,410[ RISC_JUMP >> 28 ] = 2,411[ RISC_WRITERM >> 28 ] = 3,412[ RISC_WRITECM >> 28 ] = 3,413[ RISC_WRITECR >> 28 ] = 4,414};415static const char * const bits[] = {416"12", "13", "14", "resync",417"cnt0", "cnt1", "18", "19",418"20", "21", "22", "23",419"irq1", "irq2", "eol", "sol",420};421int i;422423printk("0x%08x [ %s", risc,424instr[risc >> 28] ? instr[risc >> 28] : "INVALID");425for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)426if (risc & (1 << (i + 12)))427printk(" %s",bits[i]);428printk(" count=%d ]\n", risc & 0xfff);429return incr[risc >> 28] ? incr[risc >> 28] : 1;430}431432433void cx88_sram_channel_dump(struct cx88_core *core,434const struct sram_channel *ch)435{436static const char * const name[] = {437"initial risc",438"cdt base",439"cdt size",440"iq base",441"iq size",442"risc pc",443"iq wr ptr",444"iq rd ptr",445"cdt current",446"pci target",447"line / byte",448};449u32 risc;450unsigned int i,j,n;451452printk("%s: %s - dma channel status dump\n",453core->name,ch->name);454for (i = 0; i < ARRAY_SIZE(name); i++)455printk("%s: cmds: %-12s: 0x%08x\n",456core->name,name[i],457cx_read(ch->cmds_start + 4*i));458for (n = 1, i = 0; i < 4; i++) {459risc = cx_read(ch->cmds_start + 4 * (i+11));460printk("%s: risc%d: ", core->name, i);461if (--n)462printk("0x%08x [ arg #%d ]\n", risc, n);463else464n = cx88_risc_decode(risc);465}466for (i = 0; i < 16; i += n) {467risc = cx_read(ch->ctrl_start + 4 * i);468printk("%s: iq %x: ", core->name, i);469n = cx88_risc_decode(risc);470for (j = 1; j < n; j++) {471risc = cx_read(ch->ctrl_start + 4 * (i+j));472printk("%s: iq %x: 0x%08x [ arg #%d ]\n",473core->name, i+j, risc, j);474}475}476477printk("%s: fifo: 0x%08x -> 0x%x\n",478core->name, ch->fifo_start, ch->fifo_start+ch->fifo_size);479printk("%s: ctrl: 0x%08x -> 0x%x\n",480core->name, ch->ctrl_start, ch->ctrl_start+6*16);481printk("%s: ptr1_reg: 0x%08x\n",482core->name,cx_read(ch->ptr1_reg));483printk("%s: ptr2_reg: 0x%08x\n",484core->name,cx_read(ch->ptr2_reg));485printk("%s: cnt1_reg: 0x%08x\n",486core->name,cx_read(ch->cnt1_reg));487printk("%s: cnt2_reg: 0x%08x\n",488core->name,cx_read(ch->cnt2_reg));489}490491static const char *cx88_pci_irqs[32] = {492"vid", "aud", "ts", "vip", "hst", "5", "6", "tm1",493"src_dma", "dst_dma", "risc_rd_err", "risc_wr_err",494"brdg_err", "src_dma_err", "dst_dma_err", "ipb_dma_err",495"i2c", "i2c_rack", "ir_smp", "gpio0", "gpio1"496};497498void cx88_print_irqbits(const char *name, const char *tag, const char *strings[],499int len, u32 bits, u32 mask)500{501unsigned int i;502503printk(KERN_DEBUG "%s: %s [0x%x]", name, tag, bits);504for (i = 0; i < len; i++) {505if (!(bits & (1 << i)))506continue;507if (strings[i])508printk(" %s", strings[i]);509else510printk(" %d", i);511if (!(mask & (1 << i)))512continue;513printk("*");514}515printk("\n");516}517518/* ------------------------------------------------------------------ */519520int cx88_core_irq(struct cx88_core *core, u32 status)521{522int handled = 0;523524if (status & PCI_INT_IR_SMPINT) {525cx88_ir_irq(core);526handled++;527}528if (!handled)529cx88_print_irqbits(core->name, "irq pci",530cx88_pci_irqs, ARRAY_SIZE(cx88_pci_irqs),531status, core->pci_irqmask);532return handled;533}534535void cx88_wakeup(struct cx88_core *core,536struct cx88_dmaqueue *q, u32 count)537{538struct cx88_buffer *buf;539int bc;540541for (bc = 0;; bc++) {542if (list_empty(&q->active))543break;544buf = list_entry(q->active.next,545struct cx88_buffer, vb.queue);546/* count comes from the hw and is is 16bit wide --547* this trick handles wrap-arounds correctly for548* up to 32767 buffers in flight... */549if ((s16) (count - buf->count) < 0)550break;551do_gettimeofday(&buf->vb.ts);552dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i,553count, buf->count);554buf->vb.state = VIDEOBUF_DONE;555list_del(&buf->vb.queue);556wake_up(&buf->vb.done);557}558if (list_empty(&q->active)) {559del_timer(&q->timeout);560} else {561mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);562}563if (bc != 1)564dprintk(2, "%s: %d buffers handled (should be 1)\n",565__func__, bc);566}567568void cx88_shutdown(struct cx88_core *core)569{570/* disable RISC controller + IRQs */571cx_write(MO_DEV_CNTRL2, 0);572573/* stop dma transfers */574cx_write(MO_VID_DMACNTRL, 0x0);575cx_write(MO_AUD_DMACNTRL, 0x0);576cx_write(MO_TS_DMACNTRL, 0x0);577cx_write(MO_VIP_DMACNTRL, 0x0);578cx_write(MO_GPHST_DMACNTRL, 0x0);579580/* stop interrupts */581cx_write(MO_PCI_INTMSK, 0x0);582cx_write(MO_VID_INTMSK, 0x0);583cx_write(MO_AUD_INTMSK, 0x0);584cx_write(MO_TS_INTMSK, 0x0);585cx_write(MO_VIP_INTMSK, 0x0);586cx_write(MO_GPHST_INTMSK, 0x0);587588/* stop capturing */589cx_write(VID_CAPTURE_CONTROL, 0);590}591592int cx88_reset(struct cx88_core *core)593{594dprintk(1,"%s\n",__func__);595cx88_shutdown(core);596597/* clear irq status */598cx_write(MO_VID_INTSTAT, 0xFFFFFFFF); // Clear PIV int599cx_write(MO_PCI_INTSTAT, 0xFFFFFFFF); // Clear PCI int600cx_write(MO_INT1_STAT, 0xFFFFFFFF); // Clear RISC int601602/* wait a bit */603msleep(100);604605/* init sram */606cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21], 720*4, 0);607cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH22], 128, 0);608cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH23], 128, 0);609cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH24], 128, 0);610cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH25], 128, 0);611cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH26], 128, 0);612cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28], 188*4, 0);613cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH27], 128, 0);614615/* misc init ... */616cx_write(MO_INPUT_FORMAT, ((1 << 13) | // agc enable617(1 << 12) | // agc gain618(1 << 11) | // adaptibe agc619(0 << 10) | // chroma agc620(0 << 9) | // ckillen621(7)));622623/* setup image format */624cx_andor(MO_COLOR_CTRL, 0x4000, 0x4000);625626/* setup FIFO Thresholds */627cx_write(MO_PDMA_STHRSH, 0x0807);628cx_write(MO_PDMA_DTHRSH, 0x0807);629630/* fixes flashing of image */631cx_write(MO_AGC_SYNC_TIP1, 0x0380000F);632cx_write(MO_AGC_BACK_VBI, 0x00E00555);633634cx_write(MO_VID_INTSTAT, 0xFFFFFFFF); // Clear PIV int635cx_write(MO_PCI_INTSTAT, 0xFFFFFFFF); // Clear PCI int636cx_write(MO_INT1_STAT, 0xFFFFFFFF); // Clear RISC int637638/* Reset on-board parts */639cx_write(MO_SRST_IO, 0);640msleep(10);641cx_write(MO_SRST_IO, 1);642643return 0;644}645646/* ------------------------------------------------------------------ */647648static unsigned int inline norm_swidth(v4l2_std_id norm)649{650return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 754 : 922;651}652653static unsigned int inline norm_hdelay(v4l2_std_id norm)654{655return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 135 : 186;656}657658static unsigned int inline norm_vdelay(v4l2_std_id norm)659{660return (norm & V4L2_STD_625_50) ? 0x24 : 0x18;661}662663static unsigned int inline norm_fsc8(v4l2_std_id norm)664{665if (norm & V4L2_STD_PAL_M)666return 28604892; // 3.575611 MHz667668if (norm & (V4L2_STD_PAL_Nc))669return 28656448; // 3.582056 MHz670671if (norm & V4L2_STD_NTSC) // All NTSC/M and variants672return 28636360; // 3.57954545 MHz +/- 10 Hz673674/* SECAM have also different sub carrier for chroma,675but step_db and step_dr, at cx88_set_tvnorm already handles that.676677The same FSC applies to PAL/BGDKIH, PAL/60, NTSC/4.43 and PAL/N678*/679680return 35468950; // 4.43361875 MHz +/- 5 Hz681}682683static unsigned int inline norm_htotal(v4l2_std_id norm)684{685686unsigned int fsc4=norm_fsc8(norm)/2;687688/* returns 4*FSC / vtotal / frames per seconds */689return (norm & V4L2_STD_625_50) ?690((fsc4+312)/625+12)/25 :691((fsc4+262)/525*1001+15000)/30000;692}693694static unsigned int inline norm_vbipack(v4l2_std_id norm)695{696return (norm & V4L2_STD_625_50) ? 511 : 400;697}698699int cx88_set_scale(struct cx88_core *core, unsigned int width, unsigned int height,700enum v4l2_field field)701{702unsigned int swidth = norm_swidth(core->tvnorm);703unsigned int sheight = norm_maxh(core->tvnorm);704u32 value;705706dprintk(1,"set_scale: %dx%d [%s%s,%s]\n", width, height,707V4L2_FIELD_HAS_TOP(field) ? "T" : "",708V4L2_FIELD_HAS_BOTTOM(field) ? "B" : "",709v4l2_norm_to_name(core->tvnorm));710if (!V4L2_FIELD_HAS_BOTH(field))711height *= 2;712713// recalc H delay and scale registers714value = (width * norm_hdelay(core->tvnorm)) / swidth;715value &= 0x3fe;716cx_write(MO_HDELAY_EVEN, value);717cx_write(MO_HDELAY_ODD, value);718dprintk(1,"set_scale: hdelay 0x%04x (width %d)\n", value,swidth);719720value = (swidth * 4096 / width) - 4096;721cx_write(MO_HSCALE_EVEN, value);722cx_write(MO_HSCALE_ODD, value);723dprintk(1,"set_scale: hscale 0x%04x\n", value);724725cx_write(MO_HACTIVE_EVEN, width);726cx_write(MO_HACTIVE_ODD, width);727dprintk(1,"set_scale: hactive 0x%04x\n", width);728729// recalc V scale Register (delay is constant)730cx_write(MO_VDELAY_EVEN, norm_vdelay(core->tvnorm));731cx_write(MO_VDELAY_ODD, norm_vdelay(core->tvnorm));732dprintk(1,"set_scale: vdelay 0x%04x\n", norm_vdelay(core->tvnorm));733734value = (0x10000 - (sheight * 512 / height - 512)) & 0x1fff;735cx_write(MO_VSCALE_EVEN, value);736cx_write(MO_VSCALE_ODD, value);737dprintk(1,"set_scale: vscale 0x%04x\n", value);738739cx_write(MO_VACTIVE_EVEN, sheight);740cx_write(MO_VACTIVE_ODD, sheight);741dprintk(1,"set_scale: vactive 0x%04x\n", sheight);742743// setup filters744value = 0;745value |= (1 << 19); // CFILT (default)746if (core->tvnorm & V4L2_STD_SECAM) {747value |= (1 << 15);748value |= (1 << 16);749}750if (INPUT(core->input).type == CX88_VMUX_SVIDEO)751value |= (1 << 13) | (1 << 5);752if (V4L2_FIELD_INTERLACED == field)753value |= (1 << 3); // VINT (interlaced vertical scaling)754if (width < 385)755value |= (1 << 0); // 3-tap interpolation756if (width < 193)757value |= (1 << 1); // 5-tap interpolation758if (nocomb)759value |= (3 << 5); // disable comb filter760761cx_write(MO_FILTER_EVEN, value);762cx_write(MO_FILTER_ODD, value);763dprintk(1,"set_scale: filter 0x%04x\n", value);764765return 0;766}767768static const u32 xtal = 28636363;769770static int set_pll(struct cx88_core *core, int prescale, u32 ofreq)771{772static const u32 pre[] = { 0, 0, 0, 3, 2, 1 };773u64 pll;774u32 reg;775int i;776777if (prescale < 2)778prescale = 2;779if (prescale > 5)780prescale = 5;781782pll = ofreq * 8 * prescale * (u64)(1 << 20);783do_div(pll,xtal);784reg = (pll & 0x3ffffff) | (pre[prescale] << 26);785if (((reg >> 20) & 0x3f) < 14) {786printk("%s/0: pll out of range\n",core->name);787return -1;788}789790dprintk(1,"set_pll: MO_PLL_REG 0x%08x [old=0x%08x,freq=%d]\n",791reg, cx_read(MO_PLL_REG), ofreq);792cx_write(MO_PLL_REG, reg);793for (i = 0; i < 100; i++) {794reg = cx_read(MO_DEVICE_STATUS);795if (reg & (1<<2)) {796dprintk(1,"pll locked [pre=%d,ofreq=%d]\n",797prescale,ofreq);798return 0;799}800dprintk(1,"pll not locked yet, waiting ...\n");801msleep(10);802}803dprintk(1,"pll NOT locked [pre=%d,ofreq=%d]\n",prescale,ofreq);804return -1;805}806807int cx88_start_audio_dma(struct cx88_core *core)808{809/* constant 128 made buzz in analog Nicam-stereo for bigger fifo_size */810int bpl = cx88_sram_channels[SRAM_CH25].fifo_size/4;811812int rds_bpl = cx88_sram_channels[SRAM_CH27].fifo_size/AUD_RDS_LINES;813814/* If downstream RISC is enabled, bail out; ALSA is managing DMA */815if (cx_read(MO_AUD_DMACNTRL) & 0x10)816return 0;817818/* setup fifo + format */819cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH25], bpl, 0);820cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH26], bpl, 0);821cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH27],822rds_bpl, 0);823824cx_write(MO_AUDD_LNGTH, bpl); /* fifo bpl size */825cx_write(MO_AUDR_LNGTH, rds_bpl); /* fifo bpl size */826827/* enable Up, Down and Audio RDS fifo */828cx_write(MO_AUD_DMACNTRL, 0x0007);829830return 0;831}832833int cx88_stop_audio_dma(struct cx88_core *core)834{835/* If downstream RISC is enabled, bail out; ALSA is managing DMA */836if (cx_read(MO_AUD_DMACNTRL) & 0x10)837return 0;838839/* stop dma */840cx_write(MO_AUD_DMACNTRL, 0x0000);841842return 0;843}844845static int set_tvaudio(struct cx88_core *core)846{847v4l2_std_id norm = core->tvnorm;848849if (CX88_VMUX_TELEVISION != INPUT(core->input).type &&850CX88_VMUX_CABLE != INPUT(core->input).type)851return 0;852853if (V4L2_STD_PAL_BG & norm) {854core->tvaudio = WW_BG;855856} else if (V4L2_STD_PAL_DK & norm) {857core->tvaudio = WW_DK;858859} else if (V4L2_STD_PAL_I & norm) {860core->tvaudio = WW_I;861862} else if (V4L2_STD_SECAM_L & norm) {863core->tvaudio = WW_L;864865} else if ((V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H) & norm) {866core->tvaudio = WW_BG;867868} else if (V4L2_STD_SECAM_DK & norm) {869core->tvaudio = WW_DK;870871} else if ((V4L2_STD_NTSC_M & norm) ||872(V4L2_STD_PAL_M & norm)) {873core->tvaudio = WW_BTSC;874875} else if (V4L2_STD_NTSC_M_JP & norm) {876core->tvaudio = WW_EIAJ;877878} else {879printk("%s/0: tvaudio support needs work for this tv norm [%s], sorry\n",880core->name, v4l2_norm_to_name(core->tvnorm));881core->tvaudio = WW_NONE;882return 0;883}884885cx_andor(MO_AFECFG_IO, 0x1f, 0x0);886cx88_set_tvaudio(core);887/* cx88_set_stereo(dev,V4L2_TUNER_MODE_STEREO); */888889/*890This should be needed only on cx88-alsa. It seems that some cx88 chips have891bugs and does require DMA enabled for it to work.892*/893cx88_start_audio_dma(core);894return 0;895}896897898899int cx88_set_tvnorm(struct cx88_core *core, v4l2_std_id norm)900{901u32 fsc8;902u32 adc_clock;903u32 vdec_clock;904u32 step_db,step_dr;905u64 tmp64;906u32 bdelay,agcdelay,htotal;907u32 cxiformat, cxoformat;908909core->tvnorm = norm;910fsc8 = norm_fsc8(norm);911adc_clock = xtal;912vdec_clock = fsc8;913step_db = fsc8;914step_dr = fsc8;915916if (norm & V4L2_STD_NTSC_M_JP) {917cxiformat = VideoFormatNTSCJapan;918cxoformat = 0x181f0008;919} else if (norm & V4L2_STD_NTSC_443) {920cxiformat = VideoFormatNTSC443;921cxoformat = 0x181f0008;922} else if (norm & V4L2_STD_PAL_M) {923cxiformat = VideoFormatPALM;924cxoformat = 0x1c1f0008;925} else if (norm & V4L2_STD_PAL_N) {926cxiformat = VideoFormatPALN;927cxoformat = 0x1c1f0008;928} else if (norm & V4L2_STD_PAL_Nc) {929cxiformat = VideoFormatPALNC;930cxoformat = 0x1c1f0008;931} else if (norm & V4L2_STD_PAL_60) {932cxiformat = VideoFormatPAL60;933cxoformat = 0x181f0008;934} else if (norm & V4L2_STD_NTSC) {935cxiformat = VideoFormatNTSC;936cxoformat = 0x181f0008;937} else if (norm & V4L2_STD_SECAM) {938step_db = 4250000 * 8;939step_dr = 4406250 * 8;940941cxiformat = VideoFormatSECAM;942cxoformat = 0x181f0008;943} else { /* PAL */944cxiformat = VideoFormatPAL;945cxoformat = 0x181f0008;946}947948dprintk(1,"set_tvnorm: \"%s\" fsc8=%d adc=%d vdec=%d db/dr=%d/%d\n",949v4l2_norm_to_name(core->tvnorm), fsc8, adc_clock, vdec_clock,950step_db, step_dr);951set_pll(core,2,vdec_clock);952953dprintk(1,"set_tvnorm: MO_INPUT_FORMAT 0x%08x [old=0x%08x]\n",954cxiformat, cx_read(MO_INPUT_FORMAT) & 0x0f);955/* Chroma AGC must be disabled if SECAM is used, we enable it956by default on PAL and NTSC */957cx_andor(MO_INPUT_FORMAT, 0x40f,958norm & V4L2_STD_SECAM ? cxiformat : cxiformat | 0x400);959960// FIXME: as-is from DScaler961dprintk(1,"set_tvnorm: MO_OUTPUT_FORMAT 0x%08x [old=0x%08x]\n",962cxoformat, cx_read(MO_OUTPUT_FORMAT));963cx_write(MO_OUTPUT_FORMAT, cxoformat);964965// MO_SCONV_REG = adc clock / video dec clock * 2^17966tmp64 = adc_clock * (u64)(1 << 17);967do_div(tmp64, vdec_clock);968dprintk(1,"set_tvnorm: MO_SCONV_REG 0x%08x [old=0x%08x]\n",969(u32)tmp64, cx_read(MO_SCONV_REG));970cx_write(MO_SCONV_REG, (u32)tmp64);971972// MO_SUB_STEP = 8 * fsc / video dec clock * 2^22973tmp64 = step_db * (u64)(1 << 22);974do_div(tmp64, vdec_clock);975dprintk(1,"set_tvnorm: MO_SUB_STEP 0x%08x [old=0x%08x]\n",976(u32)tmp64, cx_read(MO_SUB_STEP));977cx_write(MO_SUB_STEP, (u32)tmp64);978979// MO_SUB_STEP_DR = 8 * 4406250 / video dec clock * 2^22980tmp64 = step_dr * (u64)(1 << 22);981do_div(tmp64, vdec_clock);982dprintk(1,"set_tvnorm: MO_SUB_STEP_DR 0x%08x [old=0x%08x]\n",983(u32)tmp64, cx_read(MO_SUB_STEP_DR));984cx_write(MO_SUB_STEP_DR, (u32)tmp64);985986// bdelay + agcdelay987bdelay = vdec_clock * 65 / 20000000 + 21;988agcdelay = vdec_clock * 68 / 20000000 + 15;989dprintk(1,"set_tvnorm: MO_AGC_BURST 0x%08x [old=0x%08x,bdelay=%d,agcdelay=%d]\n",990(bdelay << 8) | agcdelay, cx_read(MO_AGC_BURST), bdelay, agcdelay);991cx_write(MO_AGC_BURST, (bdelay << 8) | agcdelay);992993// htotal994tmp64 = norm_htotal(norm) * (u64)vdec_clock;995do_div(tmp64, fsc8);996htotal = (u32)tmp64 | (HLNotchFilter4xFsc << 11);997dprintk(1,"set_tvnorm: MO_HTOTAL 0x%08x [old=0x%08x,htotal=%d]\n",998htotal, cx_read(MO_HTOTAL), (u32)tmp64);999cx_write(MO_HTOTAL, htotal);10001001// vbi stuff, set vbi offset to 10 (for 20 Clk*2 pixels), this makes1002// the effective vbi offset ~244 samples, the same as the Bt8x81003cx_write(MO_VBI_PACKET, (10<<11) | norm_vbipack(norm));10041005// this is needed as well to set all tvnorm parameter1006cx88_set_scale(core, 320, 240, V4L2_FIELD_INTERLACED);10071008// audio1009set_tvaudio(core);10101011// tell i2c chips1012call_all(core, core, s_std, norm);10131014// done1015return 0;1016}10171018/* ------------------------------------------------------------------ */10191020struct video_device *cx88_vdev_init(struct cx88_core *core,1021struct pci_dev *pci,1022const struct video_device *template_,1023const char *type)1024{1025struct video_device *vfd;10261027vfd = video_device_alloc();1028if (NULL == vfd)1029return NULL;1030*vfd = *template_;1031vfd->v4l2_dev = &core->v4l2_dev;1032vfd->parent = &pci->dev;1033vfd->release = video_device_release;1034snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",1035core->name, type, core->board.name);1036return vfd;1037}10381039struct cx88_core* cx88_core_get(struct pci_dev *pci)1040{1041struct cx88_core *core;10421043mutex_lock(&devlist);1044list_for_each_entry(core, &cx88_devlist, devlist) {1045if (pci->bus->number != core->pci_bus)1046continue;1047if (PCI_SLOT(pci->devfn) != core->pci_slot)1048continue;10491050if (0 != cx88_get_resources(core, pci)) {1051mutex_unlock(&devlist);1052return NULL;1053}1054atomic_inc(&core->refcount);1055mutex_unlock(&devlist);1056return core;1057}10581059core = cx88_core_create(pci, cx88_devcount);1060if (NULL != core) {1061cx88_devcount++;1062list_add_tail(&core->devlist, &cx88_devlist);1063}10641065mutex_unlock(&devlist);1066return core;1067}10681069void cx88_core_put(struct cx88_core *core, struct pci_dev *pci)1070{1071release_mem_region(pci_resource_start(pci,0),1072pci_resource_len(pci,0));10731074if (!atomic_dec_and_test(&core->refcount))1075return;10761077mutex_lock(&devlist);1078cx88_ir_fini(core);1079if (0 == core->i2c_rc) {1080if (core->i2c_rtc)1081i2c_unregister_device(core->i2c_rtc);1082i2c_del_adapter(&core->i2c_adap);1083}1084list_del(&core->devlist);1085iounmap(core->lmmio);1086cx88_devcount--;1087mutex_unlock(&devlist);1088v4l2_device_unregister(&core->v4l2_dev);1089kfree(core);1090}10911092/* ------------------------------------------------------------------ */10931094EXPORT_SYMBOL(cx88_print_irqbits);10951096EXPORT_SYMBOL(cx88_core_irq);1097EXPORT_SYMBOL(cx88_wakeup);1098EXPORT_SYMBOL(cx88_reset);1099EXPORT_SYMBOL(cx88_shutdown);11001101EXPORT_SYMBOL(cx88_risc_buffer);1102EXPORT_SYMBOL(cx88_risc_databuffer);1103EXPORT_SYMBOL(cx88_risc_stopper);1104EXPORT_SYMBOL(cx88_free_buffer);11051106EXPORT_SYMBOL(cx88_sram_channels);1107EXPORT_SYMBOL(cx88_sram_channel_setup);1108EXPORT_SYMBOL(cx88_sram_channel_dump);11091110EXPORT_SYMBOL(cx88_set_tvnorm);1111EXPORT_SYMBOL(cx88_set_scale);11121113EXPORT_SYMBOL(cx88_vdev_init);1114EXPORT_SYMBOL(cx88_core_get);1115EXPORT_SYMBOL(cx88_core_put);11161117EXPORT_SYMBOL(cx88_ir_start);1118EXPORT_SYMBOL(cx88_ir_stop);11191120/*1121* Local variables:1122* c-basic-offset: 81123* End:1124* kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off1125*/112611271128