Merge branch 'upstream-fixes' of git://git.tuxdriver.com/git/netdev-jwl
[linux-2.6] / 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
23 #include "cx25840.h"
24
25 static inline int odd_parity(u8 c)
26 {
27         c ^= (c >> 4);
28         c ^= (c >> 2);
29         c ^= (c >> 1);
30
31         return c & 1;
32 }
33
34 static inline int decode_vps(u8 * dst, u8 * p)
35 {
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,
69         };
70
71         u8 c, err = 0;
72         int i;
73
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;
79         }
80
81         return err & 0xf0;
82 }
83
84 void cx25840_vbi_setup(struct i2c_client *client)
85 {
86         v4l2_std_id std = cx25840_get_v4lstd(client);
87
88         if (std & ~V4L2_STD_NTSC) {
89                 /* datasheet startup, step 8d */
90                 cx25840_write(client, 0x49f, 0x11);
91
92                 cx25840_write(client, 0x470, 0x84);
93                 cx25840_write(client, 0x471, 0x00);
94                 cx25840_write(client, 0x472, 0x2d);
95                 cx25840_write(client, 0x473, 0x5d);
96
97                 cx25840_write(client, 0x474, 0x24);
98                 cx25840_write(client, 0x475, 0x40);
99                 cx25840_write(client, 0x476, 0x24);
100                 cx25840_write(client, 0x477, 0x28);
101
102                 cx25840_write(client, 0x478, 0x1f);
103                 cx25840_write(client, 0x479, 0x02);
104
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);
115                 }
116
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);
122
123                 cx25840_write(client, 0x470, 0x7a);
124                 cx25840_write(client, 0x471, 0x00);
125                 cx25840_write(client, 0x472, 0x2d);
126                 cx25840_write(client, 0x473, 0x5b);
127
128                 cx25840_write(client, 0x474, 0x1a);
129                 cx25840_write(client, 0x475, 0x70);
130                 cx25840_write(client, 0x476, 0x1e);
131                 cx25840_write(client, 0x477, 0x1e);
132
133                 cx25840_write(client, 0x478, 0x1f);
134                 cx25840_write(client, 0x479, 0x02);
135                 cx25840_write(client, 0x47a, 0x50);
136                 cx25840_write(client, 0x47b, 0x66);
137
138                 cx25840_write(client, 0x47c, 0x1f);
139                 cx25840_write(client, 0x47d, 0x7c);
140                 cx25840_write(client, 0x47e, 0x08);
141                 cx25840_write(client, 0x47f, 0x00);
142         }
143 }
144
145 int cx25840_vbi(struct i2c_client *client, unsigned int cmd, void *arg)
146 {
147         struct v4l2_format *fmt;
148         struct v4l2_sliced_vbi_format *svbi;
149
150         switch (cmd) {
151         case VIDIOC_G_FMT:
152         {
153                 static u16 lcr2vbi[] = {
154                         0, V4L2_SLICED_TELETEXT_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
159                 };
160                 int i;
161
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;
170
171                 for (i = 7; i <= 23; i++) {
172                         u8 v = cx25840_read(client, 0x424 + i - 7);
173
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];
178                 }
179                 break;
180         }
181
182         case VIDIOC_S_FMT:
183         {
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];
188
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));
196
197                         /* Setup VBI */
198                         cx25840_vbi_setup(client);
199
200                         /* VBI Offset */
201                         cx25840_write(client, 0x47f, vbi_offset);
202                         cx25840_write(client, 0x404, 0x2e);
203                         break;
204                 }
205
206                 for (x = 0; x <= 23; x++)
207                         lcr[x] = 0x00;
208
209                 /* Setup VBI */
210                 cx25840_vbi_setup(client);
211
212                 /* Sliced VBI */
213                 cx25840_write(client, 0x404, 0x36);     /* Ancillery data */
214                 cx25840_write(client, 0x406, 0x13);
215                 cx25840_write(client, 0x47f, vbi_offset);
216
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;
225
226                         for (i = 22; i <= 23; i++)
227                                 svbi->service_lines[0][i] =
228                                         svbi->service_lines[1][i] = 0;
229                 }
230
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_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;
246                                 }
247                         }
248                 }
249
250                 for (x = 1, i = 0x424; i <= 0x434; i++, x++) {
251                         cx25840_write(client, i, lcr[6 + x]);
252                 }
253
254                 cx25840_write(client, 0x43c, 0x16);
255
256                 if (is_pal) {
257                         cx25840_write(client, 0x474, 0x2a);
258                 } else {
259                         cx25840_write(client, 0x474, 0x1a + 6);
260                 }
261                 break;
262         }
263
264         case VIDIOC_INT_DECODE_VBI_LINE:
265         {
266                 struct v4l2_decode_vbi_line *vbi = arg;
267                 u8 *p = vbi->p;
268                 int id1, id2, l, err = 0;
269
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;
274                 }
275
276                 p += 4;
277                 id1 = p[-1];
278                 id2 = p[0] & 0xf;
279                 l = p[2] & 0x3f;
280                 l += 5;
281                 p += 4;
282
283                 switch (id2) {
284                 case 1:
285                         id2 = V4L2_SLICED_TELETEXT_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;
298                         }
299                         break;
300                 default:
301                         id2 = 0;
302                         err = 1;
303                         break;
304                 }
305
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;
311         }
312         }
313
314         return 0;
315 }