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