V4L/DVB (3408): Included new sliced VBI types to videodev2.h and tvp5150
[linux-2.6/btrfs-unstable.git] / drivers / media / video / cx25840 / cx25840-vbi.c
blobe96fd1f1d6dc35f653181fe29e3241ddca23748f
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>
23 #include "cx25840.h"
25 static int odd_parity(u8 c)
27 c ^= (c >> 4);
28 c ^= (c >> 2);
29 c ^= (c >> 1);
31 return c & 1;
34 static int decode_vps(u8 * dst, u8 * p)
36 static const u8 biphase_tbl[] = {
37 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
38 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
39 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
40 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
41 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
42 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
43 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
44 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
45 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
46 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
47 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
48 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
49 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
50 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
51 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
52 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
53 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
54 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
55 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
56 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
57 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
58 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
59 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
60 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
61 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
62 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
63 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
64 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
65 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
66 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
67 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
68 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
71 u8 c, err = 0;
72 int i;
74 for (i = 0; i < 2 * 13; i += 2) {
75 err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];
76 c = (biphase_tbl[p[i + 1]] & 0xf) |
77 ((biphase_tbl[p[i]] & 0xf) << 4);
78 dst[i / 2] = c;
81 return err & 0xf0;
84 void cx25840_vbi_setup(struct i2c_client *client)
86 v4l2_std_id std = cx25840_get_v4lstd(client);
88 if (std & ~V4L2_STD_NTSC) {
89 /* datasheet startup, step 8d */
90 cx25840_write(client, 0x49f, 0x11);
92 cx25840_write(client, 0x470, 0x84);
93 cx25840_write(client, 0x471, 0x00);
94 cx25840_write(client, 0x472, 0x2d);
95 cx25840_write(client, 0x473, 0x5d);
97 cx25840_write(client, 0x474, 0x24);
98 cx25840_write(client, 0x475, 0x40);
99 cx25840_write(client, 0x476, 0x24);
100 cx25840_write(client, 0x477, 0x28);
102 cx25840_write(client, 0x478, 0x1f);
103 cx25840_write(client, 0x479, 0x02);
105 if (std & V4L2_STD_SECAM) {
106 cx25840_write(client, 0x47a, 0x80);
107 cx25840_write(client, 0x47b, 0x00);
108 cx25840_write(client, 0x47c, 0x5f);
109 cx25840_write(client, 0x47d, 0x42);
110 } else {
111 cx25840_write(client, 0x47a, 0x90);
112 cx25840_write(client, 0x47b, 0x20);
113 cx25840_write(client, 0x47c, 0x63);
114 cx25840_write(client, 0x47d, 0x82);
117 cx25840_write(client, 0x47e, 0x0a);
118 cx25840_write(client, 0x47f, 0x01);
119 } else {
120 /* datasheet startup, step 8d */
121 cx25840_write(client, 0x49f, 0x14);
123 cx25840_write(client, 0x470, 0x7a);
124 cx25840_write(client, 0x471, 0x00);
125 cx25840_write(client, 0x472, 0x2d);
126 cx25840_write(client, 0x473, 0x5b);
128 cx25840_write(client, 0x474, 0x1a);
129 cx25840_write(client, 0x475, 0x70);
130 cx25840_write(client, 0x476, 0x1e);
131 cx25840_write(client, 0x477, 0x1e);
133 cx25840_write(client, 0x478, 0x1f);
134 cx25840_write(client, 0x479, 0x02);
135 cx25840_write(client, 0x47a, 0x50);
136 cx25840_write(client, 0x47b, 0x66);
138 cx25840_write(client, 0x47c, 0x1f);
139 cx25840_write(client, 0x47d, 0x7c);
140 cx25840_write(client, 0x47e, 0x08);
141 cx25840_write(client, 0x47f, 0x00);
145 int cx25840_vbi(struct i2c_client *client, unsigned int cmd, void *arg)
147 struct v4l2_format *fmt;
148 struct v4l2_sliced_vbi_format *svbi;
150 switch (cmd) {
151 case VIDIOC_G_FMT:
153 static u16 lcr2vbi[] = {
154 0, V4L2_SLICED_TELETEXT_PAL_B, 0, /* 1 */
155 0, V4L2_SLICED_WSS_625, 0, /* 4 */
156 V4L2_SLICED_CAPTION_525, /* 6 */
157 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */
158 0, 0, 0, 0
160 int i;
162 fmt = arg;
163 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
164 return -EINVAL;
165 svbi = &fmt->fmt.sliced;
166 memset(svbi, 0, sizeof(*svbi));
167 /* we're done if raw VBI is active */
168 if ((cx25840_read(client, 0x404) & 0x10) == 0)
169 break;
171 for (i = 7; i <= 23; i++) {
172 u8 v = cx25840_read(client, 0x424 + i - 7);
174 svbi->service_lines[0][i] = lcr2vbi[v >> 4];
175 svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
176 svbi->service_set |=
177 svbi->service_lines[0][i] | svbi->service_lines[1][i];
179 break;
182 case VIDIOC_S_FMT:
184 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
185 int vbi_offset = is_pal ? 1 : 0;
186 int i, x;
187 u8 lcr[24];
189 fmt = arg;
190 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
191 return -EINVAL;
192 svbi = &fmt->fmt.sliced;
193 if (svbi->service_set == 0) {
194 /* raw VBI */
195 memset(svbi, 0, sizeof(*svbi));
197 /* Setup VBI */
198 cx25840_vbi_setup(client);
200 /* VBI Offset */
201 cx25840_write(client, 0x47f, vbi_offset);
202 cx25840_write(client, 0x404, 0x2e);
203 break;
206 for (x = 0; x <= 23; x++)
207 lcr[x] = 0x00;
209 /* Setup VBI */
210 cx25840_vbi_setup(client);
212 /* Sliced VBI */
213 cx25840_write(client, 0x404, 0x36); /* Ancillery data */
214 cx25840_write(client, 0x406, 0x13);
215 cx25840_write(client, 0x47f, vbi_offset);
217 if (is_pal) {
218 for (i = 0; i <= 6; i++)
219 svbi->service_lines[0][i] =
220 svbi->service_lines[1][i] = 0;
221 } else {
222 for (i = 0; i <= 9; i++)
223 svbi->service_lines[0][i] =
224 svbi->service_lines[1][i] = 0;
226 for (i = 22; i <= 23; i++)
227 svbi->service_lines[0][i] =
228 svbi->service_lines[1][i] = 0;
231 for (i = 7; i <= 23; i++) {
232 for (x = 0; x <= 1; x++) {
233 switch (svbi->service_lines[1-x][i]) {
234 case V4L2_SLICED_TELETEXT_PAL_B:
235 lcr[i] |= 1 << (4 * x);
236 break;
237 case V4L2_SLICED_WSS_625:
238 lcr[i] |= 4 << (4 * x);
239 break;
240 case V4L2_SLICED_CAPTION_525:
241 lcr[i] |= 6 << (4 * x);
242 break;
243 case V4L2_SLICED_VPS:
244 lcr[i] |= 9 << (4 * x);
245 break;
250 for (x = 1, i = 0x424; i <= 0x434; i++, x++) {
251 cx25840_write(client, i, lcr[6 + x]);
254 cx25840_write(client, 0x43c, 0x16);
256 if (is_pal) {
257 cx25840_write(client, 0x474, 0x2a);
258 } else {
259 cx25840_write(client, 0x474, 0x1a + 6);
261 break;
264 case VIDIOC_INT_DECODE_VBI_LINE:
266 struct v4l2_decode_vbi_line *vbi = arg;
267 u8 *p = vbi->p;
268 int id1, id2, l, err = 0;
270 if (p[0] || p[1] != 0xff || p[2] != 0xff ||
271 (p[3] != 0x55 && p[3] != 0x91)) {
272 vbi->line = vbi->type = 0;
273 break;
276 p += 4;
277 id1 = p[-1];
278 id2 = p[0] & 0xf;
279 l = p[2] & 0x3f;
280 l += 5;
281 p += 4;
283 switch (id2) {
284 case 1:
285 id2 = V4L2_SLICED_TELETEXT_PAL_B;
286 break;
287 case 4:
288 id2 = V4L2_SLICED_WSS_625;
289 break;
290 case 6:
291 id2 = V4L2_SLICED_CAPTION_525;
292 err = !odd_parity(p[0]) || !odd_parity(p[1]);
293 break;
294 case 9:
295 id2 = V4L2_SLICED_VPS;
296 if (decode_vps(p, p) != 0) {
297 err = 1;
299 break;
300 default:
301 id2 = 0;
302 err = 1;
303 break;
306 vbi->type = err ? 0 : id2;
307 vbi->line = err ? 0 : l;
308 vbi->is_second_field = err ? 0 : (id1 == 0x55);
309 vbi->p = p;
310 break;
314 return 0;