2 * Zoran ZR36016 basic configuration functions
4 * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
6 * $Id: zr36016.c,v 1.1.2.14 2003/08/20 19:46:55 rbultje Exp $
8 * ------------------------------------------------------------------------
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.
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.
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.
24 * ------------------------------------------------------------------------
27 #define ZR016_VERSION "v0.7"
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/delay.h>
34 #include <linux/types.h>
35 #include <linux/wait.h>
37 /* I/O commands, error codes */
42 /* headerfile of this module */
46 #include"videocodec.h"
48 /* it doesn't make sense to have more than 20 or so,
49 just to prevent some unwanted loops */
52 /* amount of chips attached via this driver */
53 static int zr36016_codecs
;
55 /* debugging is available via module parameter */
57 module_param(debug
, int, 0);
58 MODULE_PARM_DESC(debug
, "Debug level (0-4)");
60 #define dprintk(num, format, args...) \
63 printk(format, ##args); \
66 /* =========================================================================
67 Local hardware I/O functions:
69 read/write via codec layer (registers are located in the master device)
70 ========================================================================= */
72 /* read and write functions */
74 zr36016_read (struct zr36016
*ptr
,
79 // just in case something is wrong...
80 if (ptr
->codec
->master_data
->readreg
)
82 (ptr
->codec
->master_data
->
83 readreg(ptr
->codec
, reg
)) & 0xFF;
86 KERN_ERR
"%s: invalid I/O setup, nothing read!\n",
89 dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr
->name
, reg
,
96 zr36016_write (struct zr36016
*ptr
,
100 dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr
->name
, value
,
103 // just in case something is wrong...
104 if (ptr
->codec
->master_data
->writereg
) {
105 ptr
->codec
->master_data
->writereg(ptr
->codec
, reg
, value
);
109 "%s: invalid I/O setup, nothing written!\n",
113 /* indirect read and write functions */
114 /* the 016 supports auto-addr-increment, but
115 * writing it all time cost not much and is safer... */
117 zr36016_readi (struct zr36016
*ptr
,
122 // just in case something is wrong...
123 if ((ptr
->codec
->master_data
->writereg
) &&
124 (ptr
->codec
->master_data
->readreg
)) {
125 ptr
->codec
->master_data
->writereg(ptr
->codec
, ZR016_IADDR
, reg
& 0x0F); // ADDR
126 value
= (ptr
->codec
->master_data
->readreg(ptr
->codec
, ZR016_IDATA
)) & 0xFF; // DATA
130 "%s: invalid I/O setup, nothing read (i)!\n",
133 dprintk(4, "%s: reading indirect from 0x%04x: %02x\n", ptr
->name
,
139 zr36016_writei (struct zr36016
*ptr
,
143 dprintk(4, "%s: writing indirect 0x%02x to 0x%04x\n", ptr
->name
,
146 // just in case something is wrong...
147 if (ptr
->codec
->master_data
->writereg
) {
148 ptr
->codec
->master_data
->writereg(ptr
->codec
, ZR016_IADDR
, reg
& 0x0F); // ADDR
149 ptr
->codec
->master_data
->writereg(ptr
->codec
, ZR016_IDATA
, value
& 0x0FF); // DATA
153 "%s: invalid I/O setup, nothing written (i)!\n",
157 /* =========================================================================
158 Local helper function:
161 ========================================================================= */
163 /* version kept in datastructure */
165 zr36016_read_version (struct zr36016
*ptr
)
167 ptr
->version
= zr36016_read(ptr
, 0) >> 4;
171 /* =========================================================================
172 Local helper function:
174 basic test of "connectivity", writes/reads to/from PAX-Lo register
175 ========================================================================= */
178 zr36016_basic_test (struct zr36016
*ptr
)
182 zr36016_writei(ptr
, ZR016I_PAX_LO
, 0x55);
183 dprintk(1, KERN_INFO
"%s: registers: ", ptr
->name
);
184 for (i
= 0; i
<= 0x0b; i
++)
185 dprintk(1, "%02x ", zr36016_readi(ptr
, i
));
188 // for testing just write 0, then the default value to a register and read
189 // it back in both cases
190 zr36016_writei(ptr
, ZR016I_PAX_LO
, 0x00);
191 if (zr36016_readi(ptr
, ZR016I_PAX_LO
) != 0x0) {
194 "%s: attach failed, can't connect to vfe processor!\n",
198 zr36016_writei(ptr
, ZR016I_PAX_LO
, 0x0d0);
199 if (zr36016_readi(ptr
, ZR016I_PAX_LO
) != 0x0d0) {
202 "%s: attach failed, can't connect to vfe processor!\n",
206 // we allow version numbers from 0-3, should be enough, though
207 zr36016_read_version(ptr
);
208 if (ptr
->version
& 0x0c) {
211 "%s: attach failed, suspicious version %d found...\n",
212 ptr
->name
, ptr
->version
);
216 return 0; /* looks good! */
219 /* =========================================================================
220 Local helper function:
222 simple loop for pushing the init datasets - NO USE --
223 ========================================================================= */
226 static int zr36016_pushit (struct zr36016
*ptr
,
233 dprintk(4, "%s: write data block to 0x%04x (len=%d)\n",
234 ptr
->name
, startreg
,len
);
236 zr36016_writei(ptr
, startreg
++, data
[i
++]);
243 /* =========================================================================
244 Basic datasets & init:
247 ========================================================================= */
249 // needed offset values PAL NTSC SECAM
250 static const int zr016_xoff
[] = { 20, 20, 20 };
251 static const int zr016_yoff
[] = { 8, 9, 7 };
254 zr36016_init (struct zr36016
*ptr
)
256 // stop any processing
257 zr36016_write(ptr
, ZR016_GOSTOP
, 0);
259 // mode setup (yuv422 in and out, compression/expansuon due to mode)
260 zr36016_write(ptr
, ZR016_MODE
,
261 ZR016_YUV422
| ZR016_YUV422_YUV422
|
262 (ptr
->mode
== CODEC_DO_COMPRESSION
?
263 ZR016_COMPRESSION
: ZR016_EXPANSION
));
266 zr36016_writei(ptr
, ZR016I_SETUP1
,
267 (ptr
->xdec
? (ZR016_HRFL
| ZR016_HORZ
) : 0) |
268 (ptr
->ydec
? ZR016_VERT
: 0) | ZR016_CNTI
);
269 zr36016_writei(ptr
, ZR016I_SETUP2
, ZR016_CCIR
);
272 // (no extra offset for now, norm defines offset, default width height)
273 zr36016_writei(ptr
, ZR016I_PAX_HI
, ptr
->width
>> 8);
274 zr36016_writei(ptr
, ZR016I_PAX_LO
, ptr
->width
& 0xFF);
275 zr36016_writei(ptr
, ZR016I_PAY_HI
, ptr
->height
>> 8);
276 zr36016_writei(ptr
, ZR016I_PAY_LO
, ptr
->height
& 0xFF);
277 zr36016_writei(ptr
, ZR016I_NAX_HI
, ptr
->xoff
>> 8);
278 zr36016_writei(ptr
, ZR016I_NAX_LO
, ptr
->xoff
& 0xFF);
279 zr36016_writei(ptr
, ZR016I_NAY_HI
, ptr
->yoff
>> 8);
280 zr36016_writei(ptr
, ZR016I_NAY_LO
, ptr
->yoff
& 0xFF);
282 /* shall we continue now, please? */
283 zr36016_write(ptr
, ZR016_GOSTOP
, 1);
286 /* =========================================================================
289 this functions are accessed by the master via the API structure
290 ========================================================================= */
292 /* set compression/expansion mode and launches codec -
293 this should be the last call from the master before starting processing */
295 zr36016_set_mode (struct videocodec
*codec
,
298 struct zr36016
*ptr
= (struct zr36016
*) codec
->data
;
300 dprintk(2, "%s: set_mode %d call\n", ptr
->name
, mode
);
302 if ((mode
!= CODEC_DO_EXPANSION
) && (mode
!= CODEC_DO_COMPRESSION
))
311 /* set picture size */
313 zr36016_set_video (struct videocodec
*codec
,
315 struct vfe_settings
*cap
,
316 struct vfe_polarity
*pol
)
318 struct zr36016
*ptr
= (struct zr36016
*) codec
->data
;
320 dprintk(2, "%s: set_video %d.%d, %d/%d-%dx%d (0x%x) call\n",
321 ptr
->name
, norm
->HStart
, norm
->VStart
,
322 cap
->x
, cap
->y
, cap
->width
, cap
->height
,
325 /* if () return -EINVAL;
326 * trust the master driver that it knows what it does - so
327 * we allow invalid startx/y for now ... */
328 ptr
->width
= cap
->width
;
329 ptr
->height
= cap
->height
;
330 /* (Ronald) This is ugly. zoran_device.c, line 387
331 * already mentions what happens if HStart is even
332 * (blue faces, etc., cr/cb inversed). There's probably
333 * some good reason why HStart is 0 instead of 1, so I'm
334 * leaving it to this for now, but really... This can be
335 * done a lot simpler */
336 ptr
->xoff
= (norm
->HStart
? norm
->HStart
: 1) + cap
->x
;
337 /* Something to note here (I don't understand it), setting
338 * VStart too high will cause the codec to 'not work'. I
339 * really don't get it. values of 16 (VStart) already break
340 * it here. Just '0' seems to work. More testing needed! */
341 ptr
->yoff
= norm
->VStart
+ cap
->y
;
342 /* (Ronald) dzjeeh, can't this thing do hor_decimation = 4? */
343 ptr
->xdec
= ((cap
->decimation
& 0xff) == 1) ? 0 : 1;
344 ptr
->ydec
= (((cap
->decimation
>> 8) & 0xff) == 1) ? 0 : 1;
349 /* additional control functions */
351 zr36016_control (struct videocodec
*codec
,
356 struct zr36016
*ptr
= (struct zr36016
*) codec
->data
;
357 int *ival
= (int *) data
;
359 dprintk(2, "%s: control %d call with %d byte\n", ptr
->name
, type
,
363 case CODEC_G_STATUS
: /* get last status - we don't know it ... */
364 if (size
!= sizeof(int))
369 case CODEC_G_CODEC_MODE
:
370 if (size
!= sizeof(int))
375 case CODEC_S_CODEC_MODE
:
376 if (size
!= sizeof(int))
380 /* not needed, do nothing */
388 /* not available, give an error */
398 /* =========================================================================
399 Exit and unregister function:
401 Deinitializes Zoran's JPEG processor
402 ========================================================================= */
405 zr36016_unset (struct videocodec
*codec
)
407 struct zr36016
*ptr
= codec
->data
;
410 /* do wee need some codec deinit here, too ???? */
412 dprintk(1, "%s: finished codec #%d\n", ptr
->name
,
424 /* =========================================================================
425 Setup and registry function:
427 Initializes Zoran's JPEG processor
429 Also sets pixel size, average code size, mode (compr./decompr.)
430 (the given size is determined by the processor with the video interface)
431 ========================================================================= */
434 zr36016_setup (struct videocodec
*codec
)
439 dprintk(2, "zr36016: initializing VFE subsystem #%d.\n",
442 if (zr36016_codecs
== MAX_CODECS
) {
444 KERN_ERR
"zr36016: Can't attach more codecs!\n");
448 codec
->data
= ptr
= kzalloc(sizeof(struct zr36016
), GFP_KERNEL
);
450 dprintk(1, KERN_ERR
"zr36016: Can't get enough memory!\n");
454 snprintf(ptr
->name
, sizeof(ptr
->name
), "zr36016[%d]",
456 ptr
->num
= zr36016_codecs
++;
460 res
= zr36016_basic_test(ptr
);
462 zr36016_unset(codec
);
466 ptr
->mode
= CODEC_DO_COMPRESSION
;
473 dprintk(1, KERN_INFO
"%s: codec v%d attached and running\n",
474 ptr
->name
, ptr
->version
);
479 static const struct videocodec zr36016_codec
= {
480 .owner
= THIS_MODULE
,
482 .magic
= 0L, // magic not used
484 CODEC_FLAG_HARDWARE
| CODEC_FLAG_VFE
| CODEC_FLAG_ENCODER
|
486 .type
= CODEC_TYPE_ZR36016
,
487 .setup
= zr36016_setup
, // functionality
488 .unset
= zr36016_unset
,
489 .set_mode
= zr36016_set_mode
,
490 .set_video
= zr36016_set_video
,
491 .control
= zr36016_control
,
492 // others are not used
495 /* =========================================================================
496 HOOK IN DRIVER AS KERNEL MODULE
497 ========================================================================= */
500 zr36016_init_module (void)
502 //dprintk(1, "ZR36016 driver %s\n",ZR016_VERSION);
504 return videocodec_register(&zr36016_codec
);
508 zr36016_cleanup_module (void)
510 if (zr36016_codecs
) {
512 "zr36016: something's wrong - %d codecs left somehow.\n",
515 videocodec_unregister(&zr36016_codec
);
518 module_init(zr36016_init_module
);
519 module_exit(zr36016_cleanup_module
);
521 MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
522 MODULE_DESCRIPTION("Driver module for ZR36016 video frontends "
524 MODULE_LICENSE("GPL");