1 /* cx25840 VBI functions
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <linux/videodev2.h>
20 #include <linux/i2c.h>
21 #include <media/v4l2-common.h>
22 #include <media/cx25840.h>
24 #include "cx25840-core.h"
26 static int odd_parity(u8 c)
35 static int decode_vps(u8 * dst, u8 * p)
37 static const u8 biphase_tbl[] = {
38 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
39 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
40 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
41 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
42 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
43 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
44 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
45 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
46 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
47 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
48 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
49 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
50 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
51 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
52 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
53 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
54 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
55 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
56 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
57 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
58 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
59 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
60 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
61 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
62 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
63 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
64 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
65 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
66 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
67 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
68 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
69 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
75 for (i = 0; i < 2 * 13; i += 2) {
76 err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];
77 c = (biphase_tbl[p[i + 1]] & 0xf) |
78 ((biphase_tbl[p[i]] & 0xf) << 4);
85 void cx25840_vbi_setup(struct i2c_client *client)
87 v4l2_std_id std = cx25840_get_v4lstd(client);
89 if (std & ~V4L2_STD_NTSC) {
90 /* datasheet startup, step 8d */
91 cx25840_write(client, 0x49f, 0x11);
93 cx25840_write(client, 0x470, 0x84);
94 cx25840_write(client, 0x471, 0x00);
95 cx25840_write(client, 0x472, 0x2d);
96 cx25840_write(client, 0x473, 0x5d);
98 cx25840_write(client, 0x474, 0x24);
99 cx25840_write(client, 0x475, 0x40);
100 cx25840_write(client, 0x476, 0x24);
101 cx25840_write(client, 0x477, 0x28);
103 cx25840_write(client, 0x478, 0x1f);
104 cx25840_write(client, 0x479, 0x02);
106 if (std & V4L2_STD_SECAM) {
107 cx25840_write(client, 0x47a, 0x80);
108 cx25840_write(client, 0x47b, 0x00);
109 cx25840_write(client, 0x47c, 0x5f);
110 cx25840_write(client, 0x47d, 0x42);
112 cx25840_write(client, 0x47a, 0x90);
113 cx25840_write(client, 0x47b, 0x20);
114 cx25840_write(client, 0x47c, 0x63);
115 cx25840_write(client, 0x47d, 0x82);
118 cx25840_write(client, 0x47e, 0x0a);
119 cx25840_write(client, 0x47f, 0x01);
121 /* datasheet startup, step 8d */
122 cx25840_write(client, 0x49f, 0x14);
124 cx25840_write(client, 0x470, 0x7a);
125 cx25840_write(client, 0x471, 0x00);
126 cx25840_write(client, 0x472, 0x2d);
127 cx25840_write(client, 0x473, 0x5b);
129 cx25840_write(client, 0x474, 0x1a);
130 cx25840_write(client, 0x475, 0x70);
131 cx25840_write(client, 0x476, 0x1e);
132 cx25840_write(client, 0x477, 0x1e);
134 cx25840_write(client, 0x478, 0x1f);
135 cx25840_write(client, 0x479, 0x02);
136 cx25840_write(client, 0x47a, 0x50);
137 cx25840_write(client, 0x47b, 0x66);
139 cx25840_write(client, 0x47c, 0x1f);
140 cx25840_write(client, 0x47d, 0x7c);
141 cx25840_write(client, 0x47e, 0x08);
142 cx25840_write(client, 0x47f, 0x00);
146 int cx25840_vbi(struct i2c_client *client, unsigned int cmd, void *arg)
148 struct v4l2_format *fmt;
149 struct v4l2_sliced_vbi_format *svbi;
154 static u16 lcr2vbi[] = {
155 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */
156 0, V4L2_SLICED_WSS_625, 0, /* 4 */
157 V4L2_SLICED_CAPTION_525, /* 6 */
158 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */
164 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
166 svbi = &fmt->fmt.sliced;
167 memset(svbi, 0, sizeof(*svbi));
168 /* we're done if raw VBI is active */
169 if ((cx25840_read(client, 0x404) & 0x10) == 0)
172 for (i = 7; i <= 23; i++) {
173 u8 v = cx25840_read(client, 0x424 + i - 7);
175 svbi->service_lines[0][i] = lcr2vbi[v >> 4];
176 svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
178 svbi->service_lines[0][i] | svbi->service_lines[1][i];
185 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
186 int vbi_offset = is_pal ? 1 : 0;
191 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
193 svbi = &fmt->fmt.sliced;
194 if (svbi->service_set == 0) {
196 memset(svbi, 0, sizeof(*svbi));
199 cx25840_vbi_setup(client);
202 cx25840_write(client, 0x47f, vbi_offset);
203 cx25840_write(client, 0x404, 0x2e);
207 for (x = 0; x <= 23; x++)
211 cx25840_vbi_setup(client);
214 cx25840_write(client, 0x404, 0x36); /* Ancillery data */
215 cx25840_write(client, 0x406, 0x13);
216 cx25840_write(client, 0x47f, vbi_offset);
219 for (i = 0; i <= 6; i++)
220 svbi->service_lines[0][i] =
221 svbi->service_lines[1][i] = 0;
223 for (i = 0; i <= 9; i++)
224 svbi->service_lines[0][i] =
225 svbi->service_lines[1][i] = 0;
227 for (i = 22; i <= 23; i++)
228 svbi->service_lines[0][i] =
229 svbi->service_lines[1][i] = 0;
232 for (i = 7; i <= 23; i++) {
233 for (x = 0; x <= 1; x++) {
234 switch (svbi->service_lines[1-x][i]) {
235 case V4L2_SLICED_TELETEXT_B:
236 lcr[i] |= 1 << (4 * x);
238 case V4L2_SLICED_WSS_625:
239 lcr[i] |= 4 << (4 * x);
241 case V4L2_SLICED_CAPTION_525:
242 lcr[i] |= 6 << (4 * x);
244 case V4L2_SLICED_VPS:
245 lcr[i] |= 9 << (4 * x);
251 for (x = 1, i = 0x424; i <= 0x434; i++, x++) {
252 cx25840_write(client, i, lcr[6 + x]);
255 cx25840_write(client, 0x43c, 0x16);
258 cx25840_write(client, 0x474, 0x2a);
260 cx25840_write(client, 0x474, 0x1a + 6);
265 case VIDIOC_INT_DECODE_VBI_LINE:
267 struct v4l2_decode_vbi_line *vbi = arg;
269 int id1, id2, l, err = 0;
271 if (p[0] || p[1] != 0xff || p[2] != 0xff ||
272 (p[3] != 0x55 && p[3] != 0x91)) {
273 vbi->line = vbi->type = 0;
286 id2 = V4L2_SLICED_TELETEXT_B;
289 id2 = V4L2_SLICED_WSS_625;
292 id2 = V4L2_SLICED_CAPTION_525;
293 err = !odd_parity(p[0]) || !odd_parity(p[1]);
296 id2 = V4L2_SLICED_VPS;
297 if (decode_vps(p, p) != 0) {
307 vbi->type = err ? 0 : id2;
308 vbi->line = err ? 0 : l;
309 vbi->is_second_field = err ? 0 : (id1 == 0x55);