blob: 57feca288d2bad25635254ee186bd296014d2040 [file] [log] [blame]
Hans Verkuilbd985162005-11-13 16:07:56 -08001/* cx25840 VBI functions
2 *
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.
7 *
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.
12 *
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.
16 */
17
18
19#include <linux/videodev2.h>
20#include <linux/i2c.h>
21#include <media/v4l2-common.h>
Hans Verkuil31bc09b2006-03-25 10:26:09 -030022#include <media/cx25840.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080023
Hans Verkuil31bc09b2006-03-25 10:26:09 -030024#include "cx25840-core.h"
Hans Verkuilbd985162005-11-13 16:07:56 -080025
Hans Verkuild92c20e2006-01-09 15:32:41 -020026static int odd_parity(u8 c)
Hans Verkuilbd985162005-11-13 16:07:56 -080027{
28 c ^= (c >> 4);
29 c ^= (c >> 2);
30 c ^= (c >> 1);
31
32 return c & 1;
33}
34
Hans Verkuild92c20e2006-01-09 15:32:41 -020035static int decode_vps(u8 * dst, u8 * p)
Hans Verkuilbd985162005-11-13 16:07:56 -080036{
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,
70 };
71
72 u8 c, err = 0;
73 int i;
74
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);
79 dst[i / 2] = c;
80 }
81
82 return err & 0xf0;
83}
84
85void cx25840_vbi_setup(struct i2c_client *client)
86{
87 v4l2_std_id std = cx25840_get_v4lstd(client);
88
89 if (std & ~V4L2_STD_NTSC) {
90 /* datasheet startup, step 8d */
91 cx25840_write(client, 0x49f, 0x11);
92
93 cx25840_write(client, 0x470, 0x84);
94 cx25840_write(client, 0x471, 0x00);
95 cx25840_write(client, 0x472, 0x2d);
96 cx25840_write(client, 0x473, 0x5d);
97
98 cx25840_write(client, 0x474, 0x24);
99 cx25840_write(client, 0x475, 0x40);
100 cx25840_write(client, 0x476, 0x24);
101 cx25840_write(client, 0x477, 0x28);
102
103 cx25840_write(client, 0x478, 0x1f);
104 cx25840_write(client, 0x479, 0x02);
105
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);
111 } else {
112 cx25840_write(client, 0x47a, 0x90);
113 cx25840_write(client, 0x47b, 0x20);
114 cx25840_write(client, 0x47c, 0x63);
115 cx25840_write(client, 0x47d, 0x82);
116 }
117
118 cx25840_write(client, 0x47e, 0x0a);
119 cx25840_write(client, 0x47f, 0x01);
120 } else {
121 /* datasheet startup, step 8d */
122 cx25840_write(client, 0x49f, 0x14);
123
124 cx25840_write(client, 0x470, 0x7a);
125 cx25840_write(client, 0x471, 0x00);
126 cx25840_write(client, 0x472, 0x2d);
127 cx25840_write(client, 0x473, 0x5b);
128
129 cx25840_write(client, 0x474, 0x1a);
130 cx25840_write(client, 0x475, 0x70);
131 cx25840_write(client, 0x476, 0x1e);
132 cx25840_write(client, 0x477, 0x1e);
133
134 cx25840_write(client, 0x478, 0x1f);
135 cx25840_write(client, 0x479, 0x02);
136 cx25840_write(client, 0x47a, 0x50);
137 cx25840_write(client, 0x47b, 0x66);
138
139 cx25840_write(client, 0x47c, 0x1f);
140 cx25840_write(client, 0x47d, 0x7c);
141 cx25840_write(client, 0x47e, 0x08);
142 cx25840_write(client, 0x47f, 0x00);
143 }
144}
145
146int cx25840_vbi(struct i2c_client *client, unsigned int cmd, void *arg)
147{
148 struct v4l2_format *fmt;
149 struct v4l2_sliced_vbi_format *svbi;
150
151 switch (cmd) {
152 case VIDIOC_G_FMT:
153 {
154 static u16 lcr2vbi[] = {
Hans Verkuil9bc74002006-03-29 18:02:51 -0300155 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */
Hans Verkuilbd985162005-11-13 16:07:56 -0800156 0, V4L2_SLICED_WSS_625, 0, /* 4 */
157 V4L2_SLICED_CAPTION_525, /* 6 */
158 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */
159 0, 0, 0, 0
160 };
161 int i;
162
163 fmt = arg;
164 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
165 return -EINVAL;
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)
170 break;
171
172 for (i = 7; i <= 23; i++) {
173 u8 v = cx25840_read(client, 0x424 + i - 7);
174
175 svbi->service_lines[0][i] = lcr2vbi[v >> 4];
176 svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
177 svbi->service_set |=
178 svbi->service_lines[0][i] | svbi->service_lines[1][i];
179 }
180 break;
181 }
182
183 case VIDIOC_S_FMT:
184 {
185 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
186 int vbi_offset = is_pal ? 1 : 0;
187 int i, x;
188 u8 lcr[24];
189
190 fmt = arg;
191 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
192 return -EINVAL;
193 svbi = &fmt->fmt.sliced;
194 if (svbi->service_set == 0) {
195 /* raw VBI */
196 memset(svbi, 0, sizeof(*svbi));
197
198 /* Setup VBI */
199 cx25840_vbi_setup(client);
200
201 /* VBI Offset */
202 cx25840_write(client, 0x47f, vbi_offset);
203 cx25840_write(client, 0x404, 0x2e);
204 break;
205 }
206
207 for (x = 0; x <= 23; x++)
208 lcr[x] = 0x00;
209
210 /* Setup VBI */
211 cx25840_vbi_setup(client);
212
213 /* Sliced VBI */
214 cx25840_write(client, 0x404, 0x36); /* Ancillery data */
215 cx25840_write(client, 0x406, 0x13);
216 cx25840_write(client, 0x47f, vbi_offset);
217
218 if (is_pal) {
219 for (i = 0; i <= 6; i++)
220 svbi->service_lines[0][i] =
221 svbi->service_lines[1][i] = 0;
222 } else {
223 for (i = 0; i <= 9; i++)
224 svbi->service_lines[0][i] =
225 svbi->service_lines[1][i] = 0;
226
227 for (i = 22; i <= 23; i++)
228 svbi->service_lines[0][i] =
229 svbi->service_lines[1][i] = 0;
230 }
231
232 for (i = 7; i <= 23; i++) {
233 for (x = 0; x <= 1; x++) {
234 switch (svbi->service_lines[1-x][i]) {
Hans Verkuil9bc74002006-03-29 18:02:51 -0300235 case V4L2_SLICED_TELETEXT_B:
Hans Verkuilbd985162005-11-13 16:07:56 -0800236 lcr[i] |= 1 << (4 * x);
237 break;
238 case V4L2_SLICED_WSS_625:
239 lcr[i] |= 4 << (4 * x);
240 break;
241 case V4L2_SLICED_CAPTION_525:
242 lcr[i] |= 6 << (4 * x);
243 break;
244 case V4L2_SLICED_VPS:
245 lcr[i] |= 9 << (4 * x);
246 break;
247 }
248 }
249 }
250
251 for (x = 1, i = 0x424; i <= 0x434; i++, x++) {
252 cx25840_write(client, i, lcr[6 + x]);
253 }
254
255 cx25840_write(client, 0x43c, 0x16);
256
257 if (is_pal) {
258 cx25840_write(client, 0x474, 0x2a);
259 } else {
260 cx25840_write(client, 0x474, 0x1a + 6);
261 }
262 break;
263 }
264
265 case VIDIOC_INT_DECODE_VBI_LINE:
266 {
267 struct v4l2_decode_vbi_line *vbi = arg;
268 u8 *p = vbi->p;
269 int id1, id2, l, err = 0;
270
271 if (p[0] || p[1] != 0xff || p[2] != 0xff ||
272 (p[3] != 0x55 && p[3] != 0x91)) {
273 vbi->line = vbi->type = 0;
274 break;
275 }
276
277 p += 4;
278 id1 = p[-1];
279 id2 = p[0] & 0xf;
280 l = p[2] & 0x3f;
281 l += 5;
282 p += 4;
283
284 switch (id2) {
285 case 1:
Hans Verkuil9bc74002006-03-29 18:02:51 -0300286 id2 = V4L2_SLICED_TELETEXT_B;
Hans Verkuilbd985162005-11-13 16:07:56 -0800287 break;
288 case 4:
289 id2 = V4L2_SLICED_WSS_625;
290 break;
291 case 6:
292 id2 = V4L2_SLICED_CAPTION_525;
293 err = !odd_parity(p[0]) || !odd_parity(p[1]);
294 break;
295 case 9:
296 id2 = V4L2_SLICED_VPS;
297 if (decode_vps(p, p) != 0) {
298 err = 1;
299 }
300 break;
301 default:
302 id2 = 0;
303 err = 1;
304 break;
305 }
306
307 vbi->type = err ? 0 : id2;
308 vbi->line = err ? 0 : l;
309 vbi->is_second_field = err ? 0 : (id1 == 0x55);
310 vbi->p = p;
311 break;
312 }
313 }
314
315 return 0;
316}