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