1cb7a01acSMauro Carvalho Chehab /* cx25840 VBI functions 2cb7a01acSMauro Carvalho Chehab * 3cb7a01acSMauro Carvalho Chehab * This program is free software; you can redistribute it and/or 4cb7a01acSMauro Carvalho Chehab * modify it under the terms of the GNU General Public License 5cb7a01acSMauro Carvalho Chehab * as published by the Free Software Foundation; either version 2 6cb7a01acSMauro Carvalho Chehab * of the License, or (at your option) any later version. 7cb7a01acSMauro Carvalho Chehab * 8cb7a01acSMauro Carvalho Chehab * This program is distributed in the hope that it will be useful, 9cb7a01acSMauro Carvalho Chehab * but WITHOUT ANY WARRANTY; without even the implied warranty of 10cb7a01acSMauro Carvalho Chehab * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11cb7a01acSMauro Carvalho Chehab * GNU General Public License for more details. 12cb7a01acSMauro Carvalho Chehab * 13cb7a01acSMauro Carvalho Chehab * You should have received a copy of the GNU General Public License 14cb7a01acSMauro Carvalho Chehab * along with this program; if not, write to the Free Software 15cb7a01acSMauro Carvalho Chehab * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16cb7a01acSMauro Carvalho Chehab */ 17cb7a01acSMauro Carvalho Chehab 18cb7a01acSMauro Carvalho Chehab 19cb7a01acSMauro Carvalho Chehab #include <linux/videodev2.h> 20cb7a01acSMauro Carvalho Chehab #include <linux/i2c.h> 21cb7a01acSMauro Carvalho Chehab #include <media/v4l2-common.h> 22d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/cx25840.h> 23cb7a01acSMauro Carvalho Chehab 24cb7a01acSMauro Carvalho Chehab #include "cx25840-core.h" 25cb7a01acSMauro Carvalho Chehab 26cb7a01acSMauro Carvalho Chehab static int odd_parity(u8 c) 27cb7a01acSMauro Carvalho Chehab { 28cb7a01acSMauro Carvalho Chehab c ^= (c >> 4); 29cb7a01acSMauro Carvalho Chehab c ^= (c >> 2); 30cb7a01acSMauro Carvalho Chehab c ^= (c >> 1); 31cb7a01acSMauro Carvalho Chehab 32cb7a01acSMauro Carvalho Chehab return c & 1; 33cb7a01acSMauro Carvalho Chehab } 34cb7a01acSMauro Carvalho Chehab 35cb7a01acSMauro Carvalho Chehab static int decode_vps(u8 * dst, u8 * p) 36cb7a01acSMauro Carvalho Chehab { 37cb7a01acSMauro Carvalho Chehab static const u8 biphase_tbl[] = { 38cb7a01acSMauro Carvalho Chehab 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4, 39cb7a01acSMauro Carvalho Chehab 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0, 40cb7a01acSMauro Carvalho Chehab 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96, 41cb7a01acSMauro Carvalho Chehab 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2, 42cb7a01acSMauro Carvalho Chehab 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94, 43cb7a01acSMauro Carvalho Chehab 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0, 44cb7a01acSMauro Carvalho Chehab 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4, 45cb7a01acSMauro Carvalho Chehab 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0, 46cb7a01acSMauro Carvalho Chehab 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5, 47cb7a01acSMauro Carvalho Chehab 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1, 48cb7a01acSMauro Carvalho Chehab 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87, 49cb7a01acSMauro Carvalho Chehab 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3, 50cb7a01acSMauro Carvalho Chehab 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85, 51cb7a01acSMauro Carvalho Chehab 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1, 52cb7a01acSMauro Carvalho Chehab 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5, 53cb7a01acSMauro Carvalho Chehab 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1, 54cb7a01acSMauro Carvalho Chehab 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4, 55cb7a01acSMauro Carvalho Chehab 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0, 56cb7a01acSMauro Carvalho Chehab 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86, 57cb7a01acSMauro Carvalho Chehab 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2, 58cb7a01acSMauro Carvalho Chehab 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84, 59cb7a01acSMauro Carvalho Chehab 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0, 60cb7a01acSMauro Carvalho Chehab 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4, 61cb7a01acSMauro Carvalho Chehab 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0, 62cb7a01acSMauro Carvalho Chehab 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4, 63cb7a01acSMauro Carvalho Chehab 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0, 64cb7a01acSMauro Carvalho Chehab 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96, 65cb7a01acSMauro Carvalho Chehab 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2, 66cb7a01acSMauro Carvalho Chehab 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94, 67cb7a01acSMauro Carvalho Chehab 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0, 68cb7a01acSMauro Carvalho Chehab 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4, 69cb7a01acSMauro Carvalho Chehab 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0, 70cb7a01acSMauro Carvalho Chehab }; 71cb7a01acSMauro Carvalho Chehab 72cb7a01acSMauro Carvalho Chehab u8 c, err = 0; 73cb7a01acSMauro Carvalho Chehab int i; 74cb7a01acSMauro Carvalho Chehab 75cb7a01acSMauro Carvalho Chehab for (i = 0; i < 2 * 13; i += 2) { 76cb7a01acSMauro Carvalho Chehab err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]]; 77cb7a01acSMauro Carvalho Chehab c = (biphase_tbl[p[i + 1]] & 0xf) | 78cb7a01acSMauro Carvalho Chehab ((biphase_tbl[p[i]] & 0xf) << 4); 79cb7a01acSMauro Carvalho Chehab dst[i / 2] = c; 80cb7a01acSMauro Carvalho Chehab } 81cb7a01acSMauro Carvalho Chehab 82cb7a01acSMauro Carvalho Chehab return err & 0xf0; 83cb7a01acSMauro Carvalho Chehab } 84cb7a01acSMauro Carvalho Chehab 85cb7a01acSMauro Carvalho Chehab int cx25840_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi) 86cb7a01acSMauro Carvalho Chehab { 87cb7a01acSMauro Carvalho Chehab struct i2c_client *client = v4l2_get_subdevdata(sd); 88cb7a01acSMauro Carvalho Chehab struct cx25840_state *state = to_state(sd); 89cb7a01acSMauro Carvalho Chehab static const u16 lcr2vbi[] = { 90cb7a01acSMauro Carvalho Chehab 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */ 91cb7a01acSMauro Carvalho Chehab 0, V4L2_SLICED_WSS_625, 0, /* 4 */ 92cb7a01acSMauro Carvalho Chehab V4L2_SLICED_CAPTION_525, /* 6 */ 93cb7a01acSMauro Carvalho Chehab 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */ 94cb7a01acSMauro Carvalho Chehab 0, 0, 0, 0 95cb7a01acSMauro Carvalho Chehab }; 96cb7a01acSMauro Carvalho Chehab int is_pal = !(state->std & V4L2_STD_525_60); 97cb7a01acSMauro Carvalho Chehab int i; 98cb7a01acSMauro Carvalho Chehab 9930634e8eSHans Verkuil memset(svbi->service_lines, 0, sizeof(svbi->service_lines)); 10030634e8eSHans Verkuil svbi->service_set = 0; 101cb7a01acSMauro Carvalho Chehab /* we're done if raw VBI is active */ 102cb7a01acSMauro Carvalho Chehab if ((cx25840_read(client, 0x404) & 0x10) == 0) 103cb7a01acSMauro Carvalho Chehab return 0; 104cb7a01acSMauro Carvalho Chehab 105cb7a01acSMauro Carvalho Chehab if (is_pal) { 106cb7a01acSMauro Carvalho Chehab for (i = 7; i <= 23; i++) { 107*cdf472d3SHans Verkuil u8 v = cx25840_read(client, 108*cdf472d3SHans Verkuil state->vbi_regs_offset + 0x424 + i - 7); 109cb7a01acSMauro Carvalho Chehab 110cb7a01acSMauro Carvalho Chehab svbi->service_lines[0][i] = lcr2vbi[v >> 4]; 111cb7a01acSMauro Carvalho Chehab svbi->service_lines[1][i] = lcr2vbi[v & 0xf]; 112cb7a01acSMauro Carvalho Chehab svbi->service_set |= svbi->service_lines[0][i] | 113cb7a01acSMauro Carvalho Chehab svbi->service_lines[1][i]; 114cb7a01acSMauro Carvalho Chehab } 115cb7a01acSMauro Carvalho Chehab } else { 116cb7a01acSMauro Carvalho Chehab for (i = 10; i <= 21; i++) { 117*cdf472d3SHans Verkuil u8 v = cx25840_read(client, 118*cdf472d3SHans Verkuil state->vbi_regs_offset + 0x424 + i - 10); 119cb7a01acSMauro Carvalho Chehab 120cb7a01acSMauro Carvalho Chehab svbi->service_lines[0][i] = lcr2vbi[v >> 4]; 121cb7a01acSMauro Carvalho Chehab svbi->service_lines[1][i] = lcr2vbi[v & 0xf]; 122cb7a01acSMauro Carvalho Chehab svbi->service_set |= svbi->service_lines[0][i] | 123cb7a01acSMauro Carvalho Chehab svbi->service_lines[1][i]; 124cb7a01acSMauro Carvalho Chehab } 125cb7a01acSMauro Carvalho Chehab } 126cb7a01acSMauro Carvalho Chehab return 0; 127cb7a01acSMauro Carvalho Chehab } 128cb7a01acSMauro Carvalho Chehab 129cb7a01acSMauro Carvalho Chehab int cx25840_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt) 130cb7a01acSMauro Carvalho Chehab { 131cb7a01acSMauro Carvalho Chehab struct i2c_client *client = v4l2_get_subdevdata(sd); 132cb7a01acSMauro Carvalho Chehab struct cx25840_state *state = to_state(sd); 133cb7a01acSMauro Carvalho Chehab int is_pal = !(state->std & V4L2_STD_525_60); 134cb7a01acSMauro Carvalho Chehab int vbi_offset = is_pal ? 1 : 0; 135cb7a01acSMauro Carvalho Chehab 136cb7a01acSMauro Carvalho Chehab /* Setup standard */ 137cb7a01acSMauro Carvalho Chehab cx25840_std_setup(client); 138cb7a01acSMauro Carvalho Chehab 139cb7a01acSMauro Carvalho Chehab /* VBI Offset */ 140*cdf472d3SHans Verkuil if (is_cx23888(state)) 141*cdf472d3SHans Verkuil cx25840_write(client, 0x54f, vbi_offset); 142*cdf472d3SHans Verkuil else 143cb7a01acSMauro Carvalho Chehab cx25840_write(client, 0x47f, vbi_offset); 144cb7a01acSMauro Carvalho Chehab cx25840_write(client, 0x404, 0x2e); 145cb7a01acSMauro Carvalho Chehab return 0; 146cb7a01acSMauro Carvalho Chehab } 147cb7a01acSMauro Carvalho Chehab 148cb7a01acSMauro Carvalho Chehab int cx25840_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi) 149cb7a01acSMauro Carvalho Chehab { 150cb7a01acSMauro Carvalho Chehab struct i2c_client *client = v4l2_get_subdevdata(sd); 151cb7a01acSMauro Carvalho Chehab struct cx25840_state *state = to_state(sd); 152cb7a01acSMauro Carvalho Chehab int is_pal = !(state->std & V4L2_STD_525_60); 153cb7a01acSMauro Carvalho Chehab int vbi_offset = is_pal ? 1 : 0; 154cb7a01acSMauro Carvalho Chehab int i, x; 155cb7a01acSMauro Carvalho Chehab u8 lcr[24]; 156cb7a01acSMauro Carvalho Chehab 157cb7a01acSMauro Carvalho Chehab for (x = 0; x <= 23; x++) 158cb7a01acSMauro Carvalho Chehab lcr[x] = 0x00; 159cb7a01acSMauro Carvalho Chehab 160cb7a01acSMauro Carvalho Chehab /* Setup standard */ 161cb7a01acSMauro Carvalho Chehab cx25840_std_setup(client); 162cb7a01acSMauro Carvalho Chehab 163cb7a01acSMauro Carvalho Chehab /* Sliced VBI */ 164cb7a01acSMauro Carvalho Chehab cx25840_write(client, 0x404, 0x32); /* Ancillary data */ 165cb7a01acSMauro Carvalho Chehab cx25840_write(client, 0x406, 0x13); 166*cdf472d3SHans Verkuil if (is_cx23888(state)) 167*cdf472d3SHans Verkuil cx25840_write(client, 0x54f, vbi_offset); 168*cdf472d3SHans Verkuil else 169cb7a01acSMauro Carvalho Chehab cx25840_write(client, 0x47f, vbi_offset); 170cb7a01acSMauro Carvalho Chehab 171cb7a01acSMauro Carvalho Chehab if (is_pal) { 172cb7a01acSMauro Carvalho Chehab for (i = 0; i <= 6; i++) 173cb7a01acSMauro Carvalho Chehab svbi->service_lines[0][i] = 174cb7a01acSMauro Carvalho Chehab svbi->service_lines[1][i] = 0; 175cb7a01acSMauro Carvalho Chehab } else { 176cb7a01acSMauro Carvalho Chehab for (i = 0; i <= 9; i++) 177cb7a01acSMauro Carvalho Chehab svbi->service_lines[0][i] = 178cb7a01acSMauro Carvalho Chehab svbi->service_lines[1][i] = 0; 179cb7a01acSMauro Carvalho Chehab 180cb7a01acSMauro Carvalho Chehab for (i = 22; i <= 23; i++) 181cb7a01acSMauro Carvalho Chehab svbi->service_lines[0][i] = 182cb7a01acSMauro Carvalho Chehab svbi->service_lines[1][i] = 0; 183cb7a01acSMauro Carvalho Chehab } 184cb7a01acSMauro Carvalho Chehab 185cb7a01acSMauro Carvalho Chehab for (i = 7; i <= 23; i++) { 186cb7a01acSMauro Carvalho Chehab for (x = 0; x <= 1; x++) { 187cb7a01acSMauro Carvalho Chehab switch (svbi->service_lines[1-x][i]) { 188cb7a01acSMauro Carvalho Chehab case V4L2_SLICED_TELETEXT_B: 189cb7a01acSMauro Carvalho Chehab lcr[i] |= 1 << (4 * x); 190cb7a01acSMauro Carvalho Chehab break; 191cb7a01acSMauro Carvalho Chehab case V4L2_SLICED_WSS_625: 192cb7a01acSMauro Carvalho Chehab lcr[i] |= 4 << (4 * x); 193cb7a01acSMauro Carvalho Chehab break; 194cb7a01acSMauro Carvalho Chehab case V4L2_SLICED_CAPTION_525: 195cb7a01acSMauro Carvalho Chehab lcr[i] |= 6 << (4 * x); 196cb7a01acSMauro Carvalho Chehab break; 197cb7a01acSMauro Carvalho Chehab case V4L2_SLICED_VPS: 198cb7a01acSMauro Carvalho Chehab lcr[i] |= 9 << (4 * x); 199cb7a01acSMauro Carvalho Chehab break; 200cb7a01acSMauro Carvalho Chehab } 201cb7a01acSMauro Carvalho Chehab } 202cb7a01acSMauro Carvalho Chehab } 203cb7a01acSMauro Carvalho Chehab 204cb7a01acSMauro Carvalho Chehab if (is_pal) { 205*cdf472d3SHans Verkuil for (x = 1, i = state->vbi_regs_offset + 0x424; 206*cdf472d3SHans Verkuil i <= state->vbi_regs_offset + 0x434; i++, x++) 207cb7a01acSMauro Carvalho Chehab cx25840_write(client, i, lcr[6 + x]); 208cb7a01acSMauro Carvalho Chehab } else { 209*cdf472d3SHans Verkuil for (x = 1, i = state->vbi_regs_offset + 0x424; 210*cdf472d3SHans Verkuil i <= state->vbi_regs_offset + 0x430; i++, x++) 211cb7a01acSMauro Carvalho Chehab cx25840_write(client, i, lcr[9 + x]); 212*cdf472d3SHans Verkuil for (i = state->vbi_regs_offset + 0x431; 213*cdf472d3SHans Verkuil i <= state->vbi_regs_offset + 0x434; i++) 214cb7a01acSMauro Carvalho Chehab cx25840_write(client, i, 0); 215cb7a01acSMauro Carvalho Chehab } 216cb7a01acSMauro Carvalho Chehab 217*cdf472d3SHans Verkuil cx25840_write(client, state->vbi_regs_offset + 0x43c, 0x16); 218*cdf472d3SHans Verkuil if (is_cx23888(state)) 219*cdf472d3SHans Verkuil cx25840_write(client, 0x428, is_pal ? 0x2a : 0x22); 220*cdf472d3SHans Verkuil else 221cb7a01acSMauro Carvalho Chehab cx25840_write(client, 0x474, is_pal ? 0x2a : 0x22); 222cb7a01acSMauro Carvalho Chehab return 0; 223cb7a01acSMauro Carvalho Chehab } 224cb7a01acSMauro Carvalho Chehab 225cb7a01acSMauro Carvalho Chehab int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi) 226cb7a01acSMauro Carvalho Chehab { 227cb7a01acSMauro Carvalho Chehab struct cx25840_state *state = to_state(sd); 228cb7a01acSMauro Carvalho Chehab u8 *p = vbi->p; 229cb7a01acSMauro Carvalho Chehab int id1, id2, l, err = 0; 230cb7a01acSMauro Carvalho Chehab 231cb7a01acSMauro Carvalho Chehab if (p[0] || p[1] != 0xff || p[2] != 0xff || 232cb7a01acSMauro Carvalho Chehab (p[3] != 0x55 && p[3] != 0x91)) { 233cb7a01acSMauro Carvalho Chehab vbi->line = vbi->type = 0; 234cb7a01acSMauro Carvalho Chehab return 0; 235cb7a01acSMauro Carvalho Chehab } 236cb7a01acSMauro Carvalho Chehab 237cb7a01acSMauro Carvalho Chehab p += 4; 238cb7a01acSMauro Carvalho Chehab id1 = p[-1]; 239cb7a01acSMauro Carvalho Chehab id2 = p[0] & 0xf; 240cb7a01acSMauro Carvalho Chehab l = p[2] & 0x3f; 241cb7a01acSMauro Carvalho Chehab l += state->vbi_line_offset; 242cb7a01acSMauro Carvalho Chehab p += 4; 243cb7a01acSMauro Carvalho Chehab 244cb7a01acSMauro Carvalho Chehab switch (id2) { 245cb7a01acSMauro Carvalho Chehab case 1: 246cb7a01acSMauro Carvalho Chehab id2 = V4L2_SLICED_TELETEXT_B; 247cb7a01acSMauro Carvalho Chehab break; 248cb7a01acSMauro Carvalho Chehab case 4: 249cb7a01acSMauro Carvalho Chehab id2 = V4L2_SLICED_WSS_625; 250cb7a01acSMauro Carvalho Chehab break; 251cb7a01acSMauro Carvalho Chehab case 6: 252cb7a01acSMauro Carvalho Chehab id2 = V4L2_SLICED_CAPTION_525; 253cb7a01acSMauro Carvalho Chehab err = !odd_parity(p[0]) || !odd_parity(p[1]); 254cb7a01acSMauro Carvalho Chehab break; 255cb7a01acSMauro Carvalho Chehab case 9: 256cb7a01acSMauro Carvalho Chehab id2 = V4L2_SLICED_VPS; 257cb7a01acSMauro Carvalho Chehab if (decode_vps(p, p) != 0) 258cb7a01acSMauro Carvalho Chehab err = 1; 259cb7a01acSMauro Carvalho Chehab break; 260cb7a01acSMauro Carvalho Chehab default: 261cb7a01acSMauro Carvalho Chehab id2 = 0; 262cb7a01acSMauro Carvalho Chehab err = 1; 263cb7a01acSMauro Carvalho Chehab break; 264cb7a01acSMauro Carvalho Chehab } 265cb7a01acSMauro Carvalho Chehab 266cb7a01acSMauro Carvalho Chehab vbi->type = err ? 0 : id2; 267cb7a01acSMauro Carvalho Chehab vbi->line = err ? 0 : l; 268cb7a01acSMauro Carvalho Chehab vbi->is_second_field = err ? 0 : (id1 == 0x55); 269cb7a01acSMauro Carvalho Chehab vbi->p = p; 270cb7a01acSMauro Carvalho Chehab return 0; 271cb7a01acSMauro Carvalho Chehab } 272