]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/media/video/zr36016.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[mirror_ubuntu-zesty-kernel.git] / drivers / media / video / zr36016.c
1 /*
2 * Zoran ZR36016 basic configuration functions
3 *
4 * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
5 *
6 * $Id: zr36016.c,v 1.1.2.14 2003/08/20 19:46:55 rbultje Exp $
7 *
8 * ------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * ------------------------------------------------------------------------
25 */
26
27 #define ZR016_VERSION "v0.7"
28
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/delay.h>
33
34 #include <linux/types.h>
35 #include <linux/wait.h>
36
37 /* includes for structures and defines regarding video
38 #include<linux/videodev.h> */
39
40 /* I/O commands, error codes */
41 #include <asm/io.h>
42 //#include<errno.h>
43
44 /* v4l API */
45 #include <linux/videodev.h>
46
47 /* headerfile of this module */
48 #include"zr36016.h"
49
50 /* codec io API */
51 #include"videocodec.h"
52
53 /* it doesn't make sense to have more than 20 or so,
54 just to prevent some unwanted loops */
55 #define MAX_CODECS 20
56
57 /* amount of chips attached via this driver */
58 static int zr36016_codecs;
59
60 /* debugging is available via module parameter */
61 static int debug;
62 module_param(debug, int, 0);
63 MODULE_PARM_DESC(debug, "Debug level (0-4)");
64
65 #define dprintk(num, format, args...) \
66 do { \
67 if (debug >= num) \
68 printk(format, ##args); \
69 } while (0)
70
71 /* =========================================================================
72 Local hardware I/O functions:
73
74 read/write via codec layer (registers are located in the master device)
75 ========================================================================= */
76
77 /* read and write functions */
78 static u8
79 zr36016_read (struct zr36016 *ptr,
80 u16 reg)
81 {
82 u8 value = 0;
83
84 // just in case something is wrong...
85 if (ptr->codec->master_data->readreg)
86 value =
87 (ptr->codec->master_data->
88 readreg(ptr->codec, reg)) & 0xFF;
89 else
90 dprintk(1,
91 KERN_ERR "%s: invalid I/O setup, nothing read!\n",
92 ptr->name);
93
94 dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr->name, reg,
95 value);
96
97 return value;
98 }
99
100 static void
101 zr36016_write (struct zr36016 *ptr,
102 u16 reg,
103 u8 value)
104 {
105 dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr->name, value,
106 reg);
107
108 // just in case something is wrong...
109 if (ptr->codec->master_data->writereg) {
110 ptr->codec->master_data->writereg(ptr->codec, reg, value);
111 } else
112 dprintk(1,
113 KERN_ERR
114 "%s: invalid I/O setup, nothing written!\n",
115 ptr->name);
116 }
117
118 /* indirect read and write functions */
119 /* the 016 supports auto-addr-increment, but
120 * writing it all time cost not much and is safer... */
121 static u8
122 zr36016_readi (struct zr36016 *ptr,
123 u16 reg)
124 {
125 u8 value = 0;
126
127 // just in case something is wrong...
128 if ((ptr->codec->master_data->writereg) &&
129 (ptr->codec->master_data->readreg)) {
130 ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
131 value = (ptr->codec->master_data->readreg(ptr->codec, ZR016_IDATA)) & 0xFF; // DATA
132 } else
133 dprintk(1,
134 KERN_ERR
135 "%s: invalid I/O setup, nothing read (i)!\n",
136 ptr->name);
137
138 dprintk(4, "%s: reading indirect from 0x%04x: %02x\n", ptr->name,
139 reg, value);
140 return value;
141 }
142
143 static void
144 zr36016_writei (struct zr36016 *ptr,
145 u16 reg,
146 u8 value)
147 {
148 dprintk(4, "%s: writing indirect 0x%02x to 0x%04x\n", ptr->name,
149 value, reg);
150
151 // just in case something is wrong...
152 if (ptr->codec->master_data->writereg) {
153 ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
154 ptr->codec->master_data->writereg(ptr->codec, ZR016_IDATA, value & 0x0FF); // DATA
155 } else
156 dprintk(1,
157 KERN_ERR
158 "%s: invalid I/O setup, nothing written (i)!\n",
159 ptr->name);
160 }
161
162 /* =========================================================================
163 Local helper function:
164
165 version read
166 ========================================================================= */
167
168 /* version kept in datastructure */
169 static u8
170 zr36016_read_version (struct zr36016 *ptr)
171 {
172 ptr->version = zr36016_read(ptr, 0) >> 4;
173 return ptr->version;
174 }
175
176 /* =========================================================================
177 Local helper function:
178
179 basic test of "connectivity", writes/reads to/from PAX-Lo register
180 ========================================================================= */
181
182 static int
183 zr36016_basic_test (struct zr36016 *ptr)
184 {
185 if (debug) {
186 int i;
187 zr36016_writei(ptr, ZR016I_PAX_LO, 0x55);
188 dprintk(1, KERN_INFO "%s: registers: ", ptr->name);
189 for (i = 0; i <= 0x0b; i++)
190 dprintk(1, "%02x ", zr36016_readi(ptr, i));
191 dprintk(1, "\n");
192 }
193 // for testing just write 0, then the default value to a register and read
194 // it back in both cases
195 zr36016_writei(ptr, ZR016I_PAX_LO, 0x00);
196 if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0) {
197 dprintk(1,
198 KERN_ERR
199 "%s: attach failed, can't connect to vfe processor!\n",
200 ptr->name);
201 return -ENXIO;
202 }
203 zr36016_writei(ptr, ZR016I_PAX_LO, 0x0d0);
204 if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0d0) {
205 dprintk(1,
206 KERN_ERR
207 "%s: attach failed, can't connect to vfe processor!\n",
208 ptr->name);
209 return -ENXIO;
210 }
211 // we allow version numbers from 0-3, should be enough, though
212 zr36016_read_version(ptr);
213 if (ptr->version & 0x0c) {
214 dprintk(1,
215 KERN_ERR
216 "%s: attach failed, suspicious version %d found...\n",
217 ptr->name, ptr->version);
218 return -ENXIO;
219 }
220
221 return 0; /* looks good! */
222 }
223
224 /* =========================================================================
225 Local helper function:
226
227 simple loop for pushing the init datasets - NO USE --
228 ========================================================================= */
229
230 #if 0
231 static int zr36016_pushit (struct zr36016 *ptr,
232 u16 startreg,
233 u16 len,
234 const char *data)
235 {
236 int i=0;
237
238 dprintk(4, "%s: write data block to 0x%04x (len=%d)\n",
239 ptr->name, startreg,len);
240 while (i<len) {
241 zr36016_writei(ptr, startreg++, data[i++]);
242 }
243
244 return i;
245 }
246 #endif
247
248 /* =========================================================================
249 Basic datasets & init:
250
251 //TODO//
252 ========================================================================= */
253
254 // needed offset values PAL NTSC SECAM
255 static const int zr016_xoff[] = { 20, 20, 20 };
256 static const int zr016_yoff[] = { 8, 9, 7 };
257
258 static void
259 zr36016_init (struct zr36016 *ptr)
260 {
261 // stop any processing
262 zr36016_write(ptr, ZR016_GOSTOP, 0);
263
264 // mode setup (yuv422 in and out, compression/expansuon due to mode)
265 zr36016_write(ptr, ZR016_MODE,
266 ZR016_YUV422 | ZR016_YUV422_YUV422 |
267 (ptr->mode == CODEC_DO_COMPRESSION ?
268 ZR016_COMPRESSION : ZR016_EXPANSION));
269
270 // misc setup
271 zr36016_writei(ptr, ZR016I_SETUP1,
272 (ptr->xdec ? (ZR016_HRFL | ZR016_HORZ) : 0) |
273 (ptr->ydec ? ZR016_VERT : 0) | ZR016_CNTI);
274 zr36016_writei(ptr, ZR016I_SETUP2, ZR016_CCIR);
275
276 // Window setup
277 // (no extra offset for now, norm defines offset, default width height)
278 zr36016_writei(ptr, ZR016I_PAX_HI, ptr->width >> 8);
279 zr36016_writei(ptr, ZR016I_PAX_LO, ptr->width & 0xFF);
280 zr36016_writei(ptr, ZR016I_PAY_HI, ptr->height >> 8);
281 zr36016_writei(ptr, ZR016I_PAY_LO, ptr->height & 0xFF);
282 zr36016_writei(ptr, ZR016I_NAX_HI, ptr->xoff >> 8);
283 zr36016_writei(ptr, ZR016I_NAX_LO, ptr->xoff & 0xFF);
284 zr36016_writei(ptr, ZR016I_NAY_HI, ptr->yoff >> 8);
285 zr36016_writei(ptr, ZR016I_NAY_LO, ptr->yoff & 0xFF);
286
287 /* shall we continue now, please? */
288 zr36016_write(ptr, ZR016_GOSTOP, 1);
289 }
290
291 /* =========================================================================
292 CODEC API FUNCTIONS
293
294 this functions are accessed by the master via the API structure
295 ========================================================================= */
296
297 /* set compression/expansion mode and launches codec -
298 this should be the last call from the master before starting processing */
299 static int
300 zr36016_set_mode (struct videocodec *codec,
301 int mode)
302 {
303 struct zr36016 *ptr = (struct zr36016 *) codec->data;
304
305 dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
306
307 if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
308 return -EINVAL;
309
310 ptr->mode = mode;
311 zr36016_init(ptr);
312
313 return 0;
314 }
315
316 /* set picture size */
317 static int
318 zr36016_set_video (struct videocodec *codec,
319 struct tvnorm *norm,
320 struct vfe_settings *cap,
321 struct vfe_polarity *pol)
322 {
323 struct zr36016 *ptr = (struct zr36016 *) codec->data;
324
325 dprintk(2, "%s: set_video %d.%d, %d/%d-%dx%d (0x%x) call\n",
326 ptr->name, norm->HStart, norm->VStart,
327 cap->x, cap->y, cap->width, cap->height,
328 cap->decimation);
329
330 /* if () return -EINVAL;
331 * trust the master driver that it knows what it does - so
332 * we allow invalid startx/y for now ... */
333 ptr->width = cap->width;
334 ptr->height = cap->height;
335 /* (Ronald) This is ugly. zoran_device.c, line 387
336 * already mentions what happens if HStart is even
337 * (blue faces, etc., cr/cb inversed). There's probably
338 * some good reason why HStart is 0 instead of 1, so I'm
339 * leaving it to this for now, but really... This can be
340 * done a lot simpler */
341 ptr->xoff = (norm->HStart ? norm->HStart : 1) + cap->x;
342 /* Something to note here (I don't understand it), setting
343 * VStart too high will cause the codec to 'not work'. I
344 * really don't get it. values of 16 (VStart) already break
345 * it here. Just '0' seems to work. More testing needed! */
346 ptr->yoff = norm->VStart + cap->y;
347 /* (Ronald) dzjeeh, can't this thing do hor_decimation = 4? */
348 ptr->xdec = ((cap->decimation & 0xff) == 1) ? 0 : 1;
349 ptr->ydec = (((cap->decimation >> 8) & 0xff) == 1) ? 0 : 1;
350
351 return 0;
352 }
353
354 /* additional control functions */
355 static int
356 zr36016_control (struct videocodec *codec,
357 int type,
358 int size,
359 void *data)
360 {
361 struct zr36016 *ptr = (struct zr36016 *) codec->data;
362 int *ival = (int *) data;
363
364 dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type,
365 size);
366
367 switch (type) {
368 case CODEC_G_STATUS: /* get last status - we don't know it ... */
369 if (size != sizeof(int))
370 return -EFAULT;
371 *ival = 0;
372 break;
373
374 case CODEC_G_CODEC_MODE:
375 if (size != sizeof(int))
376 return -EFAULT;
377 *ival = 0;
378 break;
379
380 case CODEC_S_CODEC_MODE:
381 if (size != sizeof(int))
382 return -EFAULT;
383 if (*ival != 0)
384 return -EINVAL;
385 /* not needed, do nothing */
386 return 0;
387
388 case CODEC_G_VFE:
389 case CODEC_S_VFE:
390 return 0;
391
392 case CODEC_S_MMAP:
393 /* not available, give an error */
394 return -ENXIO;
395
396 default:
397 return -EINVAL;
398 }
399
400 return size;
401 }
402
403 /* =========================================================================
404 Exit and unregister function:
405
406 Deinitializes Zoran's JPEG processor
407 ========================================================================= */
408
409 static int
410 zr36016_unset (struct videocodec *codec)
411 {
412 struct zr36016 *ptr = codec->data;
413
414 if (ptr) {
415 /* do wee need some codec deinit here, too ???? */
416
417 dprintk(1, "%s: finished codec #%d\n", ptr->name,
418 ptr->num);
419 kfree(ptr);
420 codec->data = NULL;
421
422 zr36016_codecs--;
423 return 0;
424 }
425
426 return -EFAULT;
427 }
428
429 /* =========================================================================
430 Setup and registry function:
431
432 Initializes Zoran's JPEG processor
433
434 Also sets pixel size, average code size, mode (compr./decompr.)
435 (the given size is determined by the processor with the video interface)
436 ========================================================================= */
437
438 static int
439 zr36016_setup (struct videocodec *codec)
440 {
441 struct zr36016 *ptr;
442 int res;
443
444 dprintk(2, "zr36016: initializing VFE subsystem #%d.\n",
445 zr36016_codecs);
446
447 if (zr36016_codecs == MAX_CODECS) {
448 dprintk(1,
449 KERN_ERR "zr36016: Can't attach more codecs!\n");
450 return -ENOSPC;
451 }
452 //mem structure init
453 codec->data = ptr = kzalloc(sizeof(struct zr36016), GFP_KERNEL);
454 if (NULL == ptr) {
455 dprintk(1, KERN_ERR "zr36016: Can't get enough memory!\n");
456 return -ENOMEM;
457 }
458
459 snprintf(ptr->name, sizeof(ptr->name), "zr36016[%d]",
460 zr36016_codecs);
461 ptr->num = zr36016_codecs++;
462 ptr->codec = codec;
463
464 //testing
465 res = zr36016_basic_test(ptr);
466 if (res < 0) {
467 zr36016_unset(codec);
468 return res;
469 }
470 //final setup
471 ptr->mode = CODEC_DO_COMPRESSION;
472 ptr->width = 768;
473 ptr->height = 288;
474 ptr->xdec = 1;
475 ptr->ydec = 0;
476 zr36016_init(ptr);
477
478 dprintk(1, KERN_INFO "%s: codec v%d attached and running\n",
479 ptr->name, ptr->version);
480
481 return 0;
482 }
483
484 static const struct videocodec zr36016_codec = {
485 .owner = THIS_MODULE,
486 .name = "zr36016",
487 .magic = 0L, // magic not used
488 .flags =
489 CODEC_FLAG_HARDWARE | CODEC_FLAG_VFE | CODEC_FLAG_ENCODER |
490 CODEC_FLAG_DECODER,
491 .type = CODEC_TYPE_ZR36016,
492 .setup = zr36016_setup, // functionality
493 .unset = zr36016_unset,
494 .set_mode = zr36016_set_mode,
495 .set_video = zr36016_set_video,
496 .control = zr36016_control,
497 // others are not used
498 };
499
500 /* =========================================================================
501 HOOK IN DRIVER AS KERNEL MODULE
502 ========================================================================= */
503
504 static int __init
505 zr36016_init_module (void)
506 {
507 //dprintk(1, "ZR36016 driver %s\n",ZR016_VERSION);
508 zr36016_codecs = 0;
509 return videocodec_register(&zr36016_codec);
510 }
511
512 static void __exit
513 zr36016_cleanup_module (void)
514 {
515 if (zr36016_codecs) {
516 dprintk(1,
517 "zr36016: something's wrong - %d codecs left somehow.\n",
518 zr36016_codecs);
519 }
520 videocodec_unregister(&zr36016_codec);
521 }
522
523 module_init(zr36016_init_module);
524 module_exit(zr36016_cleanup_module);
525
526 MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
527 MODULE_DESCRIPTION("Driver module for ZR36016 video frontends "
528 ZR016_VERSION);
529 MODULE_LICENSE("GPL");