V4L/DVB (4050): Add NTSC sliced VBI support to the cx25840 module.
[powerpc.git] / drivers / media / video / cx25840 / cx25840-vbi.c
1 /* 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>
22 #include <media/cx25840.h>
23
24 #include "cx25840-core.h"
25
26 static int odd_parity(u8 c)
27 {
28         c ^= (c >> 4);
29         c ^= (c >> 2);
30         c ^= (c >> 1);
31
32         return c & 1;
33 }
34
35 static int decode_vps(u8 * dst, u8 * p)
36 {
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
85 void cx25840_vbi_setup(struct i2c_client *client)
86 {
87         struct cx25840_state *state = i2c_get_clientdata(client);
88         v4l2_std_id std = cx25840_get_v4lstd(client);
89
90         if (std & ~V4L2_STD_NTSC) {
91                 /* datasheet startup, step 8d */
92                 cx25840_write(client, 0x49f, 0x11);
93
94                 cx25840_write(client, 0x470, 0x84);
95                 cx25840_write(client, 0x471, 0x00);
96                 cx25840_write(client, 0x472, 0x2d);
97                 cx25840_write(client, 0x473, 0x5d);
98
99                 cx25840_write(client, 0x474, 0x24);
100                 cx25840_write(client, 0x475, 0x40);
101                 cx25840_write(client, 0x476, 0x24);
102                 cx25840_write(client, 0x477, 0x28);
103
104                 cx25840_write(client, 0x478, 0x1f);
105                 cx25840_write(client, 0x479, 0x02);
106
107                 if (std & V4L2_STD_SECAM) {
108                         cx25840_write(client, 0x47a, 0x80);
109                         cx25840_write(client, 0x47b, 0x00);
110                         cx25840_write(client, 0x47c, 0x5f);
111                         cx25840_write(client, 0x47d, 0x42);
112                 } else {
113                         cx25840_write(client, 0x47a, 0x90);
114                         cx25840_write(client, 0x47b, 0x20);
115                         cx25840_write(client, 0x47c, 0x63);
116                         cx25840_write(client, 0x47d, 0x82);
117                 }
118
119                 cx25840_write(client, 0x47e, 0x0a);
120                 cx25840_write(client, 0x47f, 0x01);
121                 state->vbi_line_offset = 5;
122         } else {
123                 /* datasheet startup, step 8d */
124                 cx25840_write(client, 0x49f, 0x14);
125
126                 cx25840_write(client, 0x470, 0x7a);
127                 cx25840_write(client, 0x471, 0x00);
128                 cx25840_write(client, 0x472, 0x2d);
129                 cx25840_write(client, 0x473, 0x5b);
130
131                 cx25840_write(client, 0x474, 0x1a);
132                 cx25840_write(client, 0x475, 0x70);
133                 cx25840_write(client, 0x476, 0x1e);
134                 cx25840_write(client, 0x477, 0x1e);
135
136                 cx25840_write(client, 0x478, 0x1f);
137                 cx25840_write(client, 0x479, 0x02);
138                 cx25840_write(client, 0x47a, 0x50);
139                 cx25840_write(client, 0x47b, 0x66);
140
141                 cx25840_write(client, 0x47c, 0x1f);
142                 cx25840_write(client, 0x47d, 0x7c);
143                 cx25840_write(client, 0x47e, 0x08);
144                 cx25840_write(client, 0x47f, 0x00);
145                 state->vbi_line_offset = 8;
146         }
147 }
148
149 int cx25840_vbi(struct i2c_client *client, unsigned int cmd, void *arg)
150 {
151         struct cx25840_state *state = i2c_get_clientdata(client);
152         struct v4l2_format *fmt;
153         struct v4l2_sliced_vbi_format *svbi;
154
155         switch (cmd) {
156         case VIDIOC_G_FMT:
157         {
158                 static u16 lcr2vbi[] = {
159                         0, V4L2_SLICED_TELETEXT_B, 0,   /* 1 */
160                         0, V4L2_SLICED_WSS_625, 0,      /* 4 */
161                         V4L2_SLICED_CAPTION_525,        /* 6 */
162                         0, 0, V4L2_SLICED_VPS, 0, 0,    /* 9 */
163                         0, 0, 0, 0
164                 };
165                 int i;
166
167                 fmt = arg;
168                 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
169                         return -EINVAL;
170                 svbi = &fmt->fmt.sliced;
171                 memset(svbi, 0, sizeof(*svbi));
172                 /* we're done if raw VBI is active */
173                 if ((cx25840_read(client, 0x404) & 0x10) == 0)
174                         break;
175
176                 for (i = 7; i <= 23; i++) {
177                         u8 v = cx25840_read(client, 0x424 + i - 7);
178
179                         svbi->service_lines[0][i] = lcr2vbi[v >> 4];
180                         svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
181                         svbi->service_set |=
182                                  svbi->service_lines[0][i] | svbi->service_lines[1][i];
183                 }
184                 break;
185         }
186
187         case VIDIOC_S_FMT:
188         {
189                 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
190                 int vbi_offset = is_pal ? 1 : 0;
191                 int i, x;
192                 u8 lcr[24];
193
194                 fmt = arg;
195                 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
196                         return -EINVAL;
197                 svbi = &fmt->fmt.sliced;
198                 if (svbi->service_set == 0) {
199                         /* raw VBI */
200                         memset(svbi, 0, sizeof(*svbi));
201
202                         /* Setup VBI */
203                         cx25840_vbi_setup(client);
204
205                         /* VBI Offset */
206                         cx25840_write(client, 0x47f, vbi_offset);
207                         cx25840_write(client, 0x404, 0x2e);
208                         break;
209                 }
210
211                 for (x = 0; x <= 23; x++)
212                         lcr[x] = 0x00;
213
214                 /* Setup VBI */
215                 cx25840_vbi_setup(client);
216
217                 /* Sliced VBI */
218                 cx25840_write(client, 0x404, 0x32);     /* Ancillary data */
219                 cx25840_write(client, 0x406, 0x13);
220                 cx25840_write(client, 0x47f, vbi_offset);
221
222                 if (is_pal) {
223                         for (i = 0; i <= 6; i++)
224                                 svbi->service_lines[0][i] =
225                                         svbi->service_lines[1][i] = 0;
226                 } else {
227                         for (i = 0; i <= 9; i++)
228                                 svbi->service_lines[0][i] =
229                                         svbi->service_lines[1][i] = 0;
230
231                         for (i = 22; i <= 23; i++)
232                                 svbi->service_lines[0][i] =
233                                         svbi->service_lines[1][i] = 0;
234                 }
235
236                 for (i = 7; i <= 23; i++) {
237                         for (x = 0; x <= 1; x++) {
238                                 switch (svbi->service_lines[1-x][i]) {
239                                 case V4L2_SLICED_TELETEXT_B:
240                                         lcr[i] |= 1 << (4 * x);
241                                         break;
242                                 case V4L2_SLICED_WSS_625:
243                                         lcr[i] |= 4 << (4 * x);
244                                         break;
245                                 case V4L2_SLICED_CAPTION_525:
246                                         lcr[i] |= 6 << (4 * x);
247                                         break;
248                                 case V4L2_SLICED_VPS:
249                                         lcr[i] |= 9 << (4 * x);
250                                         break;
251                                 }
252                         }
253                 }
254
255                 if (is_pal) {
256                         for (x = 1, i = 0x424; i <= 0x434; i++, x++) {
257                                 cx25840_write(client, i, lcr[6 + x]);
258                         }
259                 }
260                 else {
261                         for (x = 1, i = 0x424; i <= 0x430; i++, x++) {
262                                 cx25840_write(client, i, lcr[9 + x]);
263                         }
264                         for (i = 0x431; i <= 0x434; i++) {
265                                 cx25840_write(client, i, 0);
266                         }
267                 }
268
269                 cx25840_write(client, 0x43c, 0x16);
270
271                 if (is_pal) {
272                         cx25840_write(client, 0x474, 0x2a);
273                 } else {
274                         cx25840_write(client, 0x474, 0x22);
275                 }
276                 break;
277         }
278
279         case VIDIOC_INT_DECODE_VBI_LINE:
280         {
281                 struct v4l2_decode_vbi_line *vbi = arg;
282                 u8 *p = vbi->p;
283                 int id1, id2, l, err = 0;
284
285                 if (p[0] || p[1] != 0xff || p[2] != 0xff ||
286                     (p[3] != 0x55 && p[3] != 0x91)) {
287                         vbi->line = vbi->type = 0;
288                         break;
289                 }
290
291                 p += 4;
292                 id1 = p[-1];
293                 id2 = p[0] & 0xf;
294                 l = p[2] & 0x3f;
295                 l += state->vbi_line_offset;
296                 p += 4;
297
298                 switch (id2) {
299                 case 1:
300                         id2 = V4L2_SLICED_TELETEXT_B;
301                         break;
302                 case 4:
303                         id2 = V4L2_SLICED_WSS_625;
304                         break;
305                 case 6:
306                         id2 = V4L2_SLICED_CAPTION_525;
307                         err = !odd_parity(p[0]) || !odd_parity(p[1]);
308                         break;
309                 case 9:
310                         id2 = V4L2_SLICED_VPS;
311                         if (decode_vps(p, p) != 0) {
312                                 err = 1;
313                         }
314                         break;
315                 default:
316                         id2 = 0;
317                         err = 1;
318                         break;
319                 }
320
321                 vbi->type = err ? 0 : id2;
322                 vbi->line = err ? 0 : l;
323                 vbi->is_second_field = err ? 0 : (id1 == 0x55);
324                 vbi->p = p;
325                 break;
326         }
327         }
328
329         return 0;
330 }