📄 saa7191.c
字号:
/* * saa7191.c - Philips SAA7191 video decoder driver * * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org> * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/delay.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/major.h>#include <linux/module.h>#include <linux/mm.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/videodev.h>#include <linux/video_decoder.h>#include <linux/i2c.h>#include "saa7191.h"#define SAA7191_MODULE_VERSION "0.0.5"MODULE_DESCRIPTION("Philips SAA7191 video decoder driver");MODULE_VERSION(SAA7191_MODULE_VERSION);MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");MODULE_LICENSE("GPL");// #define SAA7191_DEBUG#ifdef SAA7191_DEBUG#define dprintk(x...) printk("SAA7191: " x);#else#define dprintk(x...)#endif#define SAA7191_SYNC_COUNT 30#define SAA7191_SYNC_DELAY 100 /* milliseconds */struct saa7191 { struct i2c_client *client; /* the register values are stored here as the actual * I2C-registers are write-only */ u8 reg[25]; int input; int norm;};static struct i2c_driver i2c_driver_saa7191;static const u8 initseq[] = { 0, /* Subaddress */ 0x50, /* (0x50) SAA7191_REG_IDEL */ /* 50 Hz signal timing */ 0x30, /* (0x30) SAA7191_REG_HSYB */ 0x00, /* (0x00) SAA7191_REG_HSYS */ 0xe8, /* (0xe8) SAA7191_REG_HCLB */ 0xb6, /* (0xb6) SAA7191_REG_HCLS */ 0xf4, /* (0xf4) SAA7191_REG_HPHI */ /* control */ SAA7191_LUMA_APER_1, /* (0x01) SAA7191_REG_LUMA - CVBS mode */ 0x00, /* (0x00) SAA7191_REG_HUEC */ 0xf8, /* (0xf8) SAA7191_REG_CKTQ */ 0xf8, /* (0xf8) SAA7191_REG_CKTS */ 0x90, /* (0x90) SAA7191_REG_PLSE */ 0x90, /* (0x90) SAA7191_REG_SESE */ 0x00, /* (0x00) SAA7191_REG_GAIN */ SAA7191_STDC_NFEN | SAA7191_STDC_HRMV, /* (0x0c) SAA7191_REG_STDC * - not SECAM, * slow time constant */ SAA7191_IOCK_OEDC | SAA7191_IOCK_OEHS | SAA7191_IOCK_OEVS | SAA7191_IOCK_OEDY, /* (0x78) SAA7191_REG_IOCK * - chroma from CVBS, GPSW1 & 2 off */ SAA7191_CTL3_AUFD | SAA7191_CTL3_SCEN | SAA7191_CTL3_OFTS | SAA7191_CTL3_YDEL0, /* (0x99) SAA7191_REG_CTL3 * - automatic field detection */ 0x00, /* (0x00) SAA7191_REG_CTL4 */ 0x2c, /* (0x2c) SAA7191_REG_CHCV - PAL nominal value */ 0x00, /* unused */ 0x00, /* unused */ /* 60 Hz signal timing */ 0x34, /* (0x34) SAA7191_REG_HS6B */ 0x0a, /* (0x0a) SAA7191_REG_HS6S */ 0xf4, /* (0xf4) SAA7191_REG_HC6B */ 0xce, /* (0xce) SAA7191_REG_HC6S */ 0xf4, /* (0xf4) SAA7191_REG_HP6I */};/* SAA7191 register handling */static u8 saa7191_read_reg(struct i2c_client *client, u8 reg){ return ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg];}static int saa7191_read_status(struct i2c_client *client, u8 *value){ int ret; ret = i2c_master_recv(client, value, 1); if (ret < 0) { printk(KERN_ERR "SAA7191: saa7191_read_status(): read failed\n"); return ret; } return 0;}static int saa7191_write_reg(struct i2c_client *client, u8 reg, u8 value){ ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg] = value; return i2c_smbus_write_byte_data(client, reg, value);}/* the first byte of data must be the first subaddress number (register) */static int saa7191_write_block(struct i2c_client *client, u8 length, u8 *data){ int i; int ret; struct saa7191 *decoder = (struct saa7191 *)i2c_get_clientdata(client); for (i = 0; i < (length - 1); i++) { decoder->reg[data[0] + i] = data[i + 1]; } ret = i2c_master_send(client, data, length); if (ret < 0) { printk(KERN_ERR "SAA7191: saa7191_write_block(): " "write failed\n"); return ret; } return 0;}/* Helper functions */static int saa7191_set_input(struct i2c_client *client, int input){ struct saa7191 *decoder = i2c_get_clientdata(client); u8 luma = saa7191_read_reg(client, SAA7191_REG_LUMA); u8 iock = saa7191_read_reg(client, SAA7191_REG_IOCK); int err; switch (input) { case SAA7191_INPUT_COMPOSITE: /* Set Composite input */ iock &= ~(SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW1 | SAA7191_IOCK_GPSW2); /* Chrominance trap active */ luma &= ~SAA7191_LUMA_BYPS; break; case SAA7191_INPUT_SVIDEO: /* Set S-Video input */ iock |= SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW2; /* Chrominance trap bypassed */ luma |= SAA7191_LUMA_BYPS; break; default: return -EINVAL; } err = saa7191_write_reg(client, SAA7191_REG_LUMA, luma); if (err) return -EIO; err = saa7191_write_reg(client, SAA7191_REG_IOCK, iock); if (err) return -EIO; decoder->input = input; return 0;}static int saa7191_set_norm(struct i2c_client *client, int norm){ struct saa7191 *decoder = i2c_get_clientdata(client); u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC); u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3); u8 chcv = saa7191_read_reg(client, SAA7191_REG_CHCV); int err; switch(norm) { case SAA7191_NORM_PAL: stdc &= ~SAA7191_STDC_SECS; ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL); chcv = SAA7191_CHCV_PAL; break; case SAA7191_NORM_NTSC: stdc &= ~SAA7191_STDC_SECS; ctl3 &= ~SAA7191_CTL3_AUFD; ctl3 |= SAA7191_CTL3_FSEL; chcv = SAA7191_CHCV_NTSC; break; case SAA7191_NORM_SECAM: stdc |= SAA7191_STDC_SECS; ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL); chcv = SAA7191_CHCV_PAL; break; default: return -EINVAL; } err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); if (err) return -EIO; err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc); if (err) return -EIO; err = saa7191_write_reg(client, SAA7191_REG_CHCV, chcv); if (err) return -EIO; decoder->norm = norm; dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3, stdc, chcv); dprintk("norm: %d\n", norm); return 0;}static int saa7191_wait_for_signal(struct i2c_client *client, u8 *status){ int i = 0; dprintk("Checking for signal...\n"); for (i = 0; i < SAA7191_SYNC_COUNT; i++) { if (saa7191_read_status(client, status)) return -EIO; if (((*status) & SAA7191_STATUS_HLCK) == 0) { dprintk("Signal found\n"); return 0; } msleep(SAA7191_SYNC_DELAY); } dprintk("No signal\n"); return -EBUSY;}static int saa7191_autodetect_norm_extended(struct i2c_client *client){ u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC); u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3); u8 status; int err = 0; dprintk("SAA7191 extended signal auto-detection...\n"); stdc &= ~SAA7191_STDC_SECS; ctl3 &= ~(SAA7191_CTL3_FSEL); err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc); if (err) { err = -EIO; goto out; } err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); if (err) { err = -EIO; goto out; } ctl3 |= SAA7191_CTL3_AUFD; err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); if (err) { err = -EIO; goto out; } msleep(SAA7191_SYNC_DELAY); err = saa7191_wait_for_signal(client, &status); if (err) goto out; if (status & SAA7191_STATUS_FIDT) { /* 60Hz signal -> NTSC */ dprintk("60Hz signal: NTSC\n"); return saa7191_set_norm(client, SAA7191_NORM_NTSC); } /* 50Hz signal */ dprintk("50Hz signal: Trying PAL...\n"); /* try PAL first */ err = saa7191_set_norm(client, SAA7191_NORM_PAL); if (err) goto out; msleep(SAA7191_SYNC_DELAY); err = saa7191_wait_for_signal(client, &status); if (err) goto out; /* not 50Hz ? */ if (status & SAA7191_STATUS_FIDT) { dprintk("No 50Hz signal\n"); err = -EAGAIN; goto out; } if (status & SAA7191_STATUS_CODE) { dprintk("PAL\n"); return 0; } dprintk("No color detected with PAL - Trying SECAM...\n"); /* no color detected ? -> try SECAM */ err = saa7191_set_norm(client, SAA7191_NORM_SECAM); if (err) goto out; msleep(SAA7191_SYNC_DELAY); err = saa7191_wait_for_signal(client, &status); if (err) goto out; /* not 50Hz ? */ if (status & SAA7191_STATUS_FIDT) { dprintk("No 50Hz signal\n"); err = -EAGAIN; goto out; } if (status & SAA7191_STATUS_CODE) { /* Color detected -> SECAM */ dprintk("SECAM\n"); return 0; } dprintk("No color detected with SECAM - Going back to PAL.\n"); /* still no color detected ? * -> set norm back to PAL */ err = saa7191_set_norm(client, SAA7191_NORM_PAL); if (err) goto out;out: ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3); if (ctl3 & SAA7191_CTL3_AUFD) { ctl3 &= ~(SAA7191_CTL3_AUFD); err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3); if (err) { err = -EIO; } } return err;}static int saa7191_autodetect_norm(struct i2c_client *client){ u8 status; dprintk("SAA7191 signal auto-detection...\n"); dprintk("Reading status...\n"); if (saa7191_read_status(client, &status)) return -EIO; dprintk("Checking for signal...\n"); /* no signal ? */ if (status & SAA7191_STATUS_HLCK) { dprintk("No signal\n"); return -EBUSY; } dprintk("Signal found\n"); if (status & SAA7191_STATUS_FIDT) { /* 60hz signal -> NTSC */ dprintk("NTSC\n"); return saa7191_set_norm(client, SAA7191_NORM_NTSC); } else { /* 50hz signal -> PAL */ dprintk("PAL\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -