42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/i2c.h>
46 #include <linux/videodev2.h>
51 #include <asm/div64.h>
53 #define VRES_60HZ (480+16)
55 MODULE_DESCRIPTION(
"Philips SAA7111/SAA7113/SAA7114/SAA7115/SAA7118 video decoder driver");
56 MODULE_AUTHOR(
"Maxim Yevtyushkin, Kevin Thayer, Chris Kennedy, "
57 "Hans Verkuil, Mauro Carvalho Chehab");
111 static int saa711x_has_reg(
const int id,
const u8 reg)
114 return reg < 0x20 && reg != 0x01 && reg != 0x0f &&
117 return reg < 0x20 && reg != 0x01 && reg != 0x0f &&
118 reg != 0x14 && reg != 0x18 && reg != 0x19 &&
119 reg != 0x1d && reg != 0x1e;
122 if (
unlikely((reg >= 0x3b && reg <= 0x3f) || reg == 0x5c || reg == 0x5f ||
123 reg == 0xa3 || reg == 0xa7 || reg == 0xab || reg == 0xaf || (reg >= 0xb5 && reg <= 0xb7) ||
124 reg == 0xd3 || reg == 0xd7 || reg == 0xdb || reg == 0xdf || (reg >= 0xe5 && reg <= 0xe7) ||
125 reg == 0x82 || (reg >= 0x89 && reg <= 0x8e)))
131 reg != 0x5d && reg < 0x63;
135 reg != 0x81 && reg < 0xf0;
151 while (*regs != 0x00) {
157 if (saa711x_has_reg(state->
ident, reg)) {
158 if (saa711x_write(sd, reg, data) < 0)
161 v4l2_dbg(1,
debug, sd,
"tried to access reserved reg 0x%02x\n", reg);
167 static inline int saa711x_read(
struct v4l2_subdev *sd,
u8 reg)
177 static const unsigned char saa7111_init[] = {
217 static const unsigned char saa7113_init[] = {
250 static const unsigned char saa7115_init_auto_input[] = {
287 static const unsigned char saa7115_cfg_reset_scaler[] = {
297 static const unsigned char saa7115_cfg_60hz_video[] = {
370 static const unsigned char saa7115_cfg_50hz_video[] = {
450 static const unsigned char saa7115_cfg_vbi_on[] = {
460 static const unsigned char saa7115_cfg_vbi_off[] = {
471 static const unsigned char saa7115_init_misc[] = {
576 R_41_LCR_BASE+1, 0xff,
577 R_41_LCR_BASE+2, 0xff,
578 R_41_LCR_BASE+3, 0xff,
579 R_41_LCR_BASE+4, 0xff,
580 R_41_LCR_BASE+5, 0xff,
581 R_41_LCR_BASE+6, 0xff,
582 R_41_LCR_BASE+7, 0xff,
583 R_41_LCR_BASE+8, 0xff,
584 R_41_LCR_BASE+9, 0xff,
585 R_41_LCR_BASE+10, 0xff,
586 R_41_LCR_BASE+11, 0xff,
587 R_41_LCR_BASE+12, 0xff,
588 R_41_LCR_BASE+13, 0xff,
589 R_41_LCR_BASE+14, 0xff,
590 R_41_LCR_BASE+15, 0xff,
591 R_41_LCR_BASE+16, 0xff,
592 R_41_LCR_BASE+17, 0xff,
593 R_41_LCR_BASE+18, 0xff,
594 R_41_LCR_BASE+19, 0xff,
595 R_41_LCR_BASE+20, 0xff,
596 R_41_LCR_BASE+21, 0xff,
597 R_41_LCR_BASE+22, 0xff,
612 static int saa711x_odd_parity(
u8 c)
621 static int saa711x_decode_vps(
u8 *
dst,
u8 *
p)
623 static const u8 biphase_tbl[] = {
624 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
625 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
626 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
627 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
628 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
629 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
630 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
631 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
632 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
633 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
634 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
635 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
636 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
637 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
638 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
639 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
640 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
641 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
642 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
643 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
644 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
645 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
646 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
647 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
648 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
649 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
650 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
651 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
652 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
653 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
654 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
655 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
660 for (i = 0; i < 2 * 13; i += 2) {
661 err |= biphase_tbl[p[
i]] | biphase_tbl[p[i + 1]];
662 c = (biphase_tbl[p[i + 1]] & 0xf) | ((biphase_tbl[p[i]] & 0xf) << 4);
668 static int saa711x_decode_wss(
u8 *p)
670 static const int wss_bits[8] = {
671 0, 0, 0, 1, 0, 1, 1, 1
677 for (i = 0; i < 16; i++) {
678 int b1 = wss_bits[p[
i] & 7];
679 int b2 = wss_bits[(p[
i] >> 3) & 7];
686 parity ^= parity >> 2;
687 parity ^= parity >> 1;
711 if (freq < 32000 || freq > 48000)
717 acpf = (25600 *
freq) / hz;
726 acpf = acpf * state->
cgcdiv / 16;
727 acni = acni * state->
cgcdiv / 16;
743 (acpf >> 16) & 0x03);
768 static int saa711x_s_ctrl(
struct v4l2_ctrl *ctrl)
812 int Vsrc = is_50hz ? 576 : 480;
814 v4l2_dbg(1,
debug, sd,
"decoder set size to %ix%i\n", width, height);
817 if ((width < 1) || (width > 1440))
819 if ((height < 1) || (height > Vsrc))
841 (
u8) (width & 0xff));
843 (
u8) ((width >> 8) & 0xff));
856 (
u8) ((res >> 8) & 0xff));
860 HPSC = (
int)(720 / width);
862 HPSC = HPSC ? HPSC : 1;
863 HFSC = (
int)((1024 * 720) / (HPSC *
width));
869 v4l2_dbg(1,
debug, sd,
"Hpsc: 0x%05x, Hfsc: 0x%05x\n", HPSC, HFSC);
874 (
u8) ((HFSC >> 8) & 0xff));
878 (
u8) ((HFSC >> 1) & 0xff));
880 (
u8) ((HFSC >> 9) & 0xff));
883 v4l2_dbg(1,
debug, sd,
"Vsrc: %d, Vscy: 0x%05x\n", Vsrc, VSCY);
887 (
u8) (64 * 1024 / VSCY));
889 (
u8) (64 * 1024 / VSCY));
895 (
u8) ((VSCY >> 8) & 0xff));
900 (
u8) ((VSCY >> 8) & 0xff));
902 saa711x_writeregs(sd, saa7115_cfg_reset_scaler);
922 if (std == state->
std)
930 saa711x_writeregs(sd, saa7115_cfg_60hz_video);
931 saa711x_set_size(sd, 720, 480);
934 saa711x_writeregs(sd, saa7115_cfg_50hz_video);
935 saa711x_set_size(sd, 720, 576);
967 saa711x_writeregs(sd, saa7115_cfg_vbi_on);
994 for (i = 0; i <= 23; i++)
1000 for (i = 6; i <= 23; i++)
1003 for (i = 10; i <= 21; i++)
1009 for (i = 0; i <= 5; i++)
1014 for (i = 0; i <= 9; i++)
1017 for (i = 22; i <= 23; i++)
1023 for (i = 6; i <= 23; i++) {
1025 for (x = 0; x <= 1; x++) {
1028 lcr[
i] |= 0xf << (4 *
x);
1031 lcr[
i] |= 1 << (4 *
x);
1034 lcr[
i] |= 4 << (4 *
x);
1037 lcr[
i] |= 5 << (4 *
x);
1040 lcr[
i] |= 7 << (4 *
x);
1048 for (i = 2; i <= 23; i++) {
1053 saa711x_writeregs(sd, fmt ==
NULL ?
1054 saa7115_cfg_vbi_on :
1055 saa7115_cfg_vbi_off);
1060 static u16 lcr2vbi[] = {
1074 for (i = 2; i <= 23; i++) {
1087 saa711x_set_lcr(sd,
NULL);
1093 saa711x_set_lcr(sd, fmt);
1097 static int saa711x_s_mbus_fmt(
struct v4l2_subdev *sd,
struct v4l2_mbus_framefmt *fmt)
1103 return saa711x_set_size(sd, fmt->width, fmt->height);
1115 static const char vbi_no_data_pattern[] = {
1116 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0
1126 if (state->
std & V4L2_STD_525_60)
1135 vbi->
line = (id1 & 0x3f) << 3;
1136 vbi->
line |= (id2 & 0x70) >> 4;
1143 if (!
memcmp(p, vbi_no_data_pattern,
sizeof(vbi_no_data_pattern)))
1152 if (!saa711x_odd_parity(p[0]) || !saa711x_odd_parity(p[1]))
1157 wss = saa711x_decode_wss(p);
1165 if (saa711x_decode_vps(p, p) != 0)
1187 vt->
signal = ((status & (1 << 6)) == 0) ? 0xffff : 0x0;
1196 saa711x_set_v4lstd(sd, std);
1200 static int saa711x_s_radio(
struct v4l2_subdev *sd)
1208 static int saa711x_s_routing(
struct v4l2_subdev *sd,
1225 if (state->
input == input && state->
output == output)
1239 ((output & 0xc0) ^ 0x40));
1242 ((output & 2) ? 0x0a : 0));
1271 saa711x_write(sd, 0x11, (saa711x_read(sd, 0x11) & 0x7f) |
1281 enable ?
"enable" :
"disable");
1283 if (state->
enable == enable)
1309 saa711x_writeregs(sd, saa7115_cfg_reset_scaler);
1320 if (saa711x_read(sd, 0x6b) & 0xc0)
1322 data->
data[0] = saa711x_read(sd, 0x6c);
1323 data->
data[1] = saa711x_read(sd, 0x6d);
1326 if (data->
field == 0) {
1328 if (saa711x_read(sd, 0x66) & 0x30)
1330 data->
data[0] = saa711x_read(sd, 0x69);
1331 data->
data[1] = saa711x_read(sd, 0x6a);
1335 if (saa711x_read(sd, 0x66) & 0xc0)
1337 data->
data[0] = saa711x_read(sd, 0x67);
1338 data->
data[1] = saa711x_read(sd, 0x68);
1357 v4l2_dbg(1,
debug, sd,
"Status byte 2 (0x1f)=0x%02x\n", reg1f);
1373 switch (reg1e & 0x03) {
1394 v4l2_dbg(1,
debug, sd,
"Status byte 1 (0x1e)=0x%02x\n", reg1e);
1397 v4l2_dbg(1,
debug, sd,
"detected std mask = %08Lx\n", *std);
1402 static int saa711x_g_input_status(
struct v4l2_subdev *sd,
u32 *status)
1412 if ((reg1f & 0xc1) == 0x81 && (reg1e & 0xc0) == 0x80)
1417 #ifdef CONFIG_VIDEO_ADV_DEBUG
1426 reg->
val = saa711x_read(sd, reg->
reg & 0xff);
1433 struct i2c_client *client = v4l2_get_subdevdata(sd);
1439 saa711x_write(sd, reg->
reg & 0xff, reg->
val & 0xff);
1447 struct i2c_client *client = v4l2_get_subdevdata(sd);
1452 static int saa711x_log_status(
struct v4l2_subdev *sd)
1463 signalOk = (reg1f & 0xc1) == 0x81;
1464 v4l2_info(sd,
"Video signal: %s\n", signalOk ?
"ok" :
"bad");
1465 v4l2_info(sd,
"Frequency: %s\n", (reg1f & 0x20) ?
"60 Hz" :
"50 Hz");
1473 signalOk = (reg1f & 0xc1) == 0x81 && (reg1e & 0xc0) == 0x80;
1474 vcr = !(reg1f & 0x10);
1476 if (state->
input >= 6)
1480 v4l2_info(sd,
"Video signal: %s\n", signalOk ? (vcr ?
"VCR" :
"broadcast/DVD") :
"bad");
1481 v4l2_info(sd,
"Frequency: %s\n", (reg1f & 0x20) ?
"60 Hz" :
"50 Hz");
1483 switch (reg1e & 0x03) {
1485 v4l2_info(sd,
"Detected format: NTSC\n");
1488 v4l2_info(sd,
"Detected format: PAL\n");
1491 v4l2_info(sd,
"Detected format: SECAM\n");
1494 v4l2_info(sd,
"Detected format: BW/No color\n");
1505 .s_ctrl = saa711x_s_ctrl,
1506 .g_volatile_ctrl = saa711x_g_volatile_ctrl,
1510 .log_status = saa711x_log_status,
1511 .g_chip_ident = saa711x_g_chip_ident,
1519 .s_std = saa711x_s_std,
1520 .reset = saa711x_reset,
1521 .s_gpio = saa711x_s_gpio,
1522 #ifdef CONFIG_VIDEO_ADV_DEBUG
1523 .g_register = saa711x_g_register,
1524 .s_register = saa711x_s_register,
1529 .s_radio = saa711x_s_radio,
1530 .g_tuner = saa711x_g_tuner,
1534 .s_clock_freq = saa711x_s_clock_freq,
1538 .s_routing = saa711x_s_routing,
1539 .s_crystal_freq = saa711x_s_crystal_freq,
1540 .s_mbus_fmt = saa711x_s_mbus_fmt,
1541 .s_stream = saa711x_s_stream,
1542 .querystd = saa711x_querystd,
1543 .g_input_status = saa711x_g_input_status,
1547 .g_vbi_data = saa711x_g_vbi_data,
1548 .decode_vbi_line = saa711x_decode_vbi_line,
1549 .g_sliced_fmt = saa711x_g_sliced_fmt,
1550 .s_sliced_fmt = saa711x_s_sliced_fmt,
1551 .s_raw_fmt = saa711x_s_raw_fmt,
1555 .core = &saa711x_core_ops,
1556 .tuner = &saa711x_tuner_ops,
1557 .audio = &saa711x_audio_ops,
1558 .video = &saa711x_video_ops,
1559 .vbi = &saa711x_vbi_ops,
1564 static int saa711x_probe(
struct i2c_client *client,
1573 int autodetect = !
id ||
id->driver_data == 1;
1579 for (i = 0; i < 0x0f; i++) {
1583 name[
i] +=
'a' -
'9' - 1;
1590 if (
memcmp(name + 1,
"f711", 4)) {
1591 v4l_dbg(1,
debug, client,
"chip found @ 0x%x (ID %s) does not match a known saa711x chip.\n",
1592 client->
addr << 1, name);
1597 if (!autodetect && id->
name[6] != chip_id) {
1598 v4l_warn(client,
"found saa711%c while %s was expected\n",
1602 v4l_info(client,
"saa711%c found (%s) @ 0x%x (%s)\n", chip_id, name,
1628 int err = hdl->
error;
1644 v4l_info(client,
"saa7111a variant found\n");
1662 v4l2_info(sd,
"WARNING: Chip is not known - Falling back to saa7111\n");
1672 switch (state->
ident) {
1675 saa711x_writeregs(sd, saa7111_init);
1678 saa711x_writeregs(sd, saa7113_init);
1682 saa711x_writeregs(sd, saa7115_init_auto_input);
1685 saa711x_writeregs(sd, saa7115_init_misc);
1689 v4l2_dbg(1,
debug, sd,
"status: (1E) 0x%02x, (1F) 0x%02x\n",
1697 static int saa711x_remove(
struct i2c_client *client)
1699 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1703 kfree(to_state(sd));
1708 {
"saa7115_auto", 1 },
1723 .probe = saa711x_probe,
1724 .remove = saa711x_remove,
1725 .id_table = saa711x_id,