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 /* TODO: this will have to be changed for generic_mode VBI */ 90 if ((cx25840_read(client, 0x404) & 0x10) == 0) 91 return 0; 92 93 if (is_pal) { 94 for (i = 7; i <= 23; i++) { 95 u8 v = cx25840_read(client, 96 state->vbi_regs_offset + 0x424 + i - 7); 97 98 svbi->service_lines[0][i] = lcr2vbi[v >> 4]; 99 svbi->service_lines[1][i] = lcr2vbi[v & 0xf]; 100 svbi->service_set |= svbi->service_lines[0][i] | 101 svbi->service_lines[1][i]; 102 } 103 } else { 104 for (i = 10; i <= 21; i++) { 105 u8 v = cx25840_read(client, 106 state->vbi_regs_offset + 0x424 + i - 10); 107 108 svbi->service_lines[0][i] = lcr2vbi[v >> 4]; 109 svbi->service_lines[1][i] = lcr2vbi[v & 0xf]; 110 svbi->service_set |= svbi->service_lines[0][i] | 111 svbi->service_lines[1][i]; 112 } 113 } 114 return 0; 115 } 116 117 int cx25840_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt) 118 { 119 struct i2c_client *client = v4l2_get_subdevdata(sd); 120 struct cx25840_state *state = to_state(sd); 121 int is_pal = !(state->std & V4L2_STD_525_60); 122 int vbi_offset = is_pal ? 1 : 0; 123 124 /* Setup standard */ 125 cx25840_std_setup(client); 126 127 /* VBI Offset */ 128 if (is_cx23888(state)) 129 cx25840_write(client, 0x54f, vbi_offset); 130 else 131 cx25840_write(client, 0x47f, vbi_offset); 132 /* TODO: this will have to be changed for generic_mode VBI */ 133 cx25840_write(client, 0x404, 0x2e); 134 return 0; 135 } 136 137 int cx25840_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi) 138 { 139 struct i2c_client *client = v4l2_get_subdevdata(sd); 140 struct cx25840_state *state = to_state(sd); 141 int is_pal = !(state->std & V4L2_STD_525_60); 142 int vbi_offset = is_pal ? 1 : 0; 143 int i, x; 144 u8 lcr[24]; 145 146 for (x = 0; x <= 23; x++) 147 lcr[x] = 0x00; 148 149 /* Setup standard */ 150 cx25840_std_setup(client); 151 152 /* Sliced VBI */ 153 /* TODO: this will have to be changed for generic_mode VBI */ 154 cx25840_write(client, 0x404, 0x32); /* Ancillary data */ 155 cx25840_write(client, 0x406, 0x13); 156 if (is_cx23888(state)) 157 cx25840_write(client, 0x54f, vbi_offset); 158 else 159 cx25840_write(client, 0x47f, vbi_offset); 160 161 if (is_pal) { 162 for (i = 0; i <= 6; i++) 163 svbi->service_lines[0][i] = 164 svbi->service_lines[1][i] = 0; 165 } else { 166 for (i = 0; i <= 9; i++) 167 svbi->service_lines[0][i] = 168 svbi->service_lines[1][i] = 0; 169 170 for (i = 22; i <= 23; i++) 171 svbi->service_lines[0][i] = 172 svbi->service_lines[1][i] = 0; 173 } 174 175 for (i = 7; i <= 23; i++) { 176 for (x = 0; x <= 1; x++) { 177 switch (svbi->service_lines[1-x][i]) { 178 case V4L2_SLICED_TELETEXT_B: 179 lcr[i] |= 1 << (4 * x); 180 break; 181 case V4L2_SLICED_WSS_625: 182 lcr[i] |= 4 << (4 * x); 183 break; 184 case V4L2_SLICED_CAPTION_525: 185 lcr[i] |= 6 << (4 * x); 186 break; 187 case V4L2_SLICED_VPS: 188 lcr[i] |= 9 << (4 * x); 189 break; 190 } 191 } 192 } 193 194 if (is_pal) { 195 for (x = 1, i = state->vbi_regs_offset + 0x424; 196 i <= state->vbi_regs_offset + 0x434; i++, x++) 197 cx25840_write(client, i, lcr[6 + x]); 198 } else { 199 for (x = 1, i = state->vbi_regs_offset + 0x424; 200 i <= state->vbi_regs_offset + 0x430; i++, x++) 201 cx25840_write(client, i, lcr[9 + x]); 202 for (i = state->vbi_regs_offset + 0x431; 203 i <= state->vbi_regs_offset + 0x434; i++) 204 cx25840_write(client, i, 0); 205 } 206 207 cx25840_write(client, state->vbi_regs_offset + 0x43c, 0x16); 208 /* TODO: this will have to be changed for generic_mode VBI */ 209 if (is_cx23888(state)) 210 cx25840_write(client, 0x428, is_pal ? 0x2a : 0x22); 211 else 212 cx25840_write(client, 0x474, is_pal ? 0x2a : 0x22); 213 return 0; 214 } 215 216 int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi) 217 { 218 struct cx25840_state *state = to_state(sd); 219 u8 *p = vbi->p; 220 int id1, id2, l, err = 0; 221 222 if (p[0] || p[1] != 0xff || p[2] != 0xff || 223 (p[3] != 0x55 && p[3] != 0x91)) { 224 vbi->line = vbi->type = 0; 225 return 0; 226 } 227 228 p += 4; 229 id1 = p[-1]; 230 id2 = p[0] & 0xf; 231 l = p[2] & 0x3f; 232 l += state->vbi_line_offset; 233 p += 4; 234 235 switch (id2) { 236 case 1: 237 id2 = V4L2_SLICED_TELETEXT_B; 238 break; 239 case 4: 240 id2 = V4L2_SLICED_WSS_625; 241 break; 242 case 6: 243 id2 = V4L2_SLICED_CAPTION_525; 244 err = !odd_parity(p[0]) || !odd_parity(p[1]); 245 break; 246 case 9: 247 id2 = V4L2_SLICED_VPS; 248 if (decode_vps(p, p) != 0) 249 err = 1; 250 break; 251 default: 252 id2 = 0; 253 err = 1; 254 break; 255 } 256 257 vbi->type = err ? 0 : id2; 258 vbi->line = err ? 0 : l; 259 vbi->is_second_field = err ? 0 : (id1 == 0x55); 260 vbi->p = p; 261 return 0; 262 } 263