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 * ------------------------------------------------------------------------
23 #define ZR016_VERSION "v0.7"
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
30 #include <linux/types.h>
31 #include <linux/wait.h>
33 /* I/O commands, error codes */
38 /* headerfile of this module */
42 #include "videocodec.h"
44 /* it doesn't make sense to have more than 20 or so,
45 just to prevent some unwanted loops */
48 /* amount of chips attached via this driver */
49 static int zr36016_codecs;
51 /* debugging is available via module parameter */
53 module_param(debug, int, 0);
54 MODULE_PARM_DESC(debug, "Debug level (0-4)");
56 #define dprintk(num, format, args...) \
59 printk(format, ##args); \
62 /* =========================================================================
63 Local hardware I/O functions:
65 read/write via codec layer (registers are located in the master device)
66 ========================================================================= */
68 /* read and write functions */
70 zr36016_read (struct zr36016 *ptr,
75 // just in case something is wrong...
76 if (ptr->codec->master_data->readreg)
78 (ptr->codec->master_data->
79 readreg(ptr->codec, reg)) & 0xFF;
82 KERN_ERR "%s: invalid I/O setup, nothing read!\n",
85 dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr->name, reg,
92 zr36016_write (struct zr36016 *ptr,
96 dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr->name, value,
99 // just in case something is wrong...
100 if (ptr->codec->master_data->writereg) {
101 ptr->codec->master_data->writereg(ptr->codec, reg, value);
105 "%s: invalid I/O setup, nothing written!\n",
109 /* indirect read and write functions */
110 /* the 016 supports auto-addr-increment, but
111 * writing it all time cost not much and is safer... */
113 zr36016_readi (struct zr36016 *ptr,
118 // just in case something is wrong...
119 if ((ptr->codec->master_data->writereg) &&
120 (ptr->codec->master_data->readreg)) {
121 ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
122 value = (ptr->codec->master_data->readreg(ptr->codec, ZR016_IDATA)) & 0xFF; // DATA
126 "%s: invalid I/O setup, nothing read (i)!\n",
129 dprintk(4, "%s: reading indirect from 0x%04x: %02x\n", ptr->name,
135 zr36016_writei (struct zr36016 *ptr,
139 dprintk(4, "%s: writing indirect 0x%02x to 0x%04x\n", ptr->name,
142 // just in case something is wrong...
143 if (ptr->codec->master_data->writereg) {
144 ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
145 ptr->codec->master_data->writereg(ptr->codec, ZR016_IDATA, value & 0x0FF); // DATA
149 "%s: invalid I/O setup, nothing written (i)!\n",
153 /* =========================================================================
154 Local helper function:
157 ========================================================================= */
159 /* version kept in datastructure */
161 zr36016_read_version (struct zr36016 *ptr)
163 ptr->version = zr36016_read(ptr, 0) >> 4;
167 /* =========================================================================
168 Local helper function:
170 basic test of "connectivity", writes/reads to/from PAX-Lo register
171 ========================================================================= */
174 zr36016_basic_test (struct zr36016 *ptr)
178 zr36016_writei(ptr, ZR016I_PAX_LO, 0x55);
179 dprintk(1, KERN_INFO "%s: registers: ", ptr->name);
180 for (i = 0; i <= 0x0b; i++)
181 dprintk(1, "%02x ", zr36016_readi(ptr, i));
184 // for testing just write 0, then the default value to a register and read
185 // it back in both cases
186 zr36016_writei(ptr, ZR016I_PAX_LO, 0x00);
187 if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0) {
190 "%s: attach failed, can't connect to vfe processor!\n",
194 zr36016_writei(ptr, ZR016I_PAX_LO, 0x0d0);
195 if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0d0) {
198 "%s: attach failed, can't connect to vfe processor!\n",
202 // we allow version numbers from 0-3, should be enough, though
203 zr36016_read_version(ptr);
204 if (ptr->version & 0x0c) {
207 "%s: attach failed, suspicious version %d found...\n",
208 ptr->name, ptr->version);
212 return 0; /* looks good! */
215 /* =========================================================================
216 Local helper function:
218 simple loop for pushing the init datasets - NO USE --
219 ========================================================================= */
222 static int zr36016_pushit (struct zr36016 *ptr,
229 dprintk(4, "%s: write data block to 0x%04x (len=%d)\n",
230 ptr->name, startreg,len);
232 zr36016_writei(ptr, startreg++, data[i++]);
239 /* =========================================================================
240 Basic datasets & init:
243 ========================================================================= */
246 zr36016_init (struct zr36016 *ptr)
248 // stop any processing
249 zr36016_write(ptr, ZR016_GOSTOP, 0);
251 // mode setup (yuv422 in and out, compression/expansuon due to mode)
252 zr36016_write(ptr, ZR016_MODE,
253 ZR016_YUV422 | ZR016_YUV422_YUV422 |
254 (ptr->mode == CODEC_DO_COMPRESSION ?
255 ZR016_COMPRESSION : ZR016_EXPANSION));
258 zr36016_writei(ptr, ZR016I_SETUP1,
259 (ptr->xdec ? (ZR016_HRFL | ZR016_HORZ) : 0) |
260 (ptr->ydec ? ZR016_VERT : 0) | ZR016_CNTI);
261 zr36016_writei(ptr, ZR016I_SETUP2, ZR016_CCIR);
264 // (no extra offset for now, norm defines offset, default width height)
265 zr36016_writei(ptr, ZR016I_PAX_HI, ptr->width >> 8);
266 zr36016_writei(ptr, ZR016I_PAX_LO, ptr->width & 0xFF);
267 zr36016_writei(ptr, ZR016I_PAY_HI, ptr->height >> 8);
268 zr36016_writei(ptr, ZR016I_PAY_LO, ptr->height & 0xFF);
269 zr36016_writei(ptr, ZR016I_NAX_HI, ptr->xoff >> 8);
270 zr36016_writei(ptr, ZR016I_NAX_LO, ptr->xoff & 0xFF);
271 zr36016_writei(ptr, ZR016I_NAY_HI, ptr->yoff >> 8);
272 zr36016_writei(ptr, ZR016I_NAY_LO, ptr->yoff & 0xFF);
274 /* shall we continue now, please? */
275 zr36016_write(ptr, ZR016_GOSTOP, 1);
278 /* =========================================================================
281 this functions are accessed by the master via the API structure
282 ========================================================================= */
284 /* set compression/expansion mode and launches codec -
285 this should be the last call from the master before starting processing */
287 zr36016_set_mode (struct videocodec *codec,
290 struct zr36016 *ptr = (struct zr36016 *) codec->data;
292 dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
294 if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
303 /* set picture size */
305 zr36016_set_video (struct videocodec *codec,
307 struct vfe_settings *cap,
308 struct vfe_polarity *pol)
310 struct zr36016 *ptr = (struct zr36016 *) codec->data;
312 dprintk(2, "%s: set_video %d.%d, %d/%d-%dx%d (0x%x) call\n",
313 ptr->name, norm->HStart, norm->VStart,
314 cap->x, cap->y, cap->width, cap->height,
317 /* if () return -EINVAL;
318 * trust the master driver that it knows what it does - so
319 * we allow invalid startx/y for now ... */
320 ptr->width = cap->width;
321 ptr->height = cap->height;
322 /* (Ronald) This is ugly. zoran_device.c, line 387
323 * already mentions what happens if HStart is even
324 * (blue faces, etc., cr/cb inversed). There's probably
325 * some good reason why HStart is 0 instead of 1, so I'm
326 * leaving it to this for now, but really... This can be
327 * done a lot simpler */
328 ptr->xoff = (norm->HStart ? norm->HStart : 1) + cap->x;
329 /* Something to note here (I don't understand it), setting
330 * VStart too high will cause the codec to 'not work'. I
331 * really don't get it. values of 16 (VStart) already break
332 * it here. Just '0' seems to work. More testing needed! */
333 ptr->yoff = norm->VStart + cap->y;
334 /* (Ronald) dzjeeh, can't this thing do hor_decimation = 4? */
335 ptr->xdec = ((cap->decimation & 0xff) == 1) ? 0 : 1;
336 ptr->ydec = (((cap->decimation >> 8) & 0xff) == 1) ? 0 : 1;
341 /* additional control functions */
343 zr36016_control (struct videocodec *codec,
348 struct zr36016 *ptr = (struct zr36016 *) codec->data;
349 int *ival = (int *) data;
351 dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type,
355 case CODEC_G_STATUS: /* get last status - we don't know it ... */
356 if (size != sizeof(int))
361 case CODEC_G_CODEC_MODE:
362 if (size != sizeof(int))
367 case CODEC_S_CODEC_MODE:
368 if (size != sizeof(int))
372 /* not needed, do nothing */
380 /* not available, give an error */
390 /* =========================================================================
391 Exit and unregister function:
393 Deinitializes Zoran's JPEG processor
394 ========================================================================= */
397 zr36016_unset (struct videocodec *codec)
399 struct zr36016 *ptr = codec->data;
402 /* do wee need some codec deinit here, too ???? */
404 dprintk(1, "%s: finished codec #%d\n", ptr->name,
416 /* =========================================================================
417 Setup and registry function:
419 Initializes Zoran's JPEG processor
421 Also sets pixel size, average code size, mode (compr./decompr.)
422 (the given size is determined by the processor with the video interface)
423 ========================================================================= */
426 zr36016_setup (struct videocodec *codec)
431 dprintk(2, "zr36016: initializing VFE subsystem #%d.\n",
434 if (zr36016_codecs == MAX_CODECS) {
436 KERN_ERR "zr36016: Can't attach more codecs!\n");
440 codec->data = ptr = kzalloc(sizeof(struct zr36016), GFP_KERNEL);
442 dprintk(1, KERN_ERR "zr36016: Can't get enough memory!\n");
446 snprintf(ptr->name, sizeof(ptr->name), "zr36016[%d]",
448 ptr->num = zr36016_codecs++;
452 res = zr36016_basic_test(ptr);
454 zr36016_unset(codec);
458 ptr->mode = CODEC_DO_COMPRESSION;
465 dprintk(1, KERN_INFO "%s: codec v%d attached and running\n",
466 ptr->name, ptr->version);
471 static const struct videocodec zr36016_codec = {
472 .owner = THIS_MODULE,
474 .magic = 0L, // magic not used
476 CODEC_FLAG_HARDWARE | CODEC_FLAG_VFE | CODEC_FLAG_ENCODER |
478 .type = CODEC_TYPE_ZR36016,
479 .setup = zr36016_setup, // functionality
480 .unset = zr36016_unset,
481 .set_mode = zr36016_set_mode,
482 .set_video = zr36016_set_video,
483 .control = zr36016_control,
484 // others are not used
487 /* =========================================================================
488 HOOK IN DRIVER AS KERNEL MODULE
489 ========================================================================= */
492 zr36016_init_module (void)
494 //dprintk(1, "ZR36016 driver %s\n",ZR016_VERSION);
496 return videocodec_register(&zr36016_codec);
500 zr36016_cleanup_module (void)
502 if (zr36016_codecs) {
504 "zr36016: something's wrong - %d codecs left somehow.\n",
507 videocodec_unregister(&zr36016_codec);
510 module_init(zr36016_init_module);
511 module_exit(zr36016_cleanup_module);
513 MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
514 MODULE_DESCRIPTION("Driver module for ZR36016 video frontends "
516 MODULE_LICENSE("GPL");