1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Zoran ZR36016 basic configuration functions
5 * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
12 /* headerfile of this module */
16 #include "videocodec.h"
18 /* it doesn't make sense to have more than 20 or so,
19 just to prevent some unwanted loops */
22 /* amount of chips attached via this driver */
23 static int zr36016_codecs;
25 static int zr36016_debug;
26 module_param(zr36016_debug, int, 0);
27 MODULE_PARM_DESC(zr36016_debug, "Debug level (0-4)");
30 #define dprintk(num, format, args...) \
32 if (zr36016_debug >= num) \
33 printk(format, ##args); \
36 /* =========================================================================
37 Local hardware I/O functions:
39 read/write via codec layer (registers are located in the master device)
40 ========================================================================= */
42 /* read and write functions */
43 static u8 zr36016_read(struct zr36016 *ptr, u16 reg)
47 /* just in case something is wrong... */
48 if (ptr->codec->master_data->readreg)
49 value = (ptr->codec->master_data->readreg(ptr->codec, reg)) & 0xFF;
51 pr_err("%s: invalid I/O setup, nothing read!\n", ptr->name);
53 dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr->name, reg, value);
58 static void zr36016_write(struct zr36016 *ptr, u16 reg, u8 value)
60 dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr->name, value, reg);
62 // just in case something is wrong...
63 if (ptr->codec->master_data->writereg)
64 ptr->codec->master_data->writereg(ptr->codec, reg, value);
66 pr_err("%s: invalid I/O setup, nothing written!\n", ptr->name);
69 /* indirect read and write functions */
70 /* the 016 supports auto-addr-increment, but
71 * writing it all time cost not much and is safer... */
72 static u8 zr36016_readi(struct zr36016 *ptr, u16 reg)
76 /* just in case something is wrong... */
77 if ((ptr->codec->master_data->writereg) && (ptr->codec->master_data->readreg)) {
78 ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
79 value = (ptr->codec->master_data->readreg(ptr->codec, ZR016_IDATA)) & 0xFF; // DATA
81 pr_err("%s: invalid I/O setup, nothing read (i)!\n", ptr->name);
84 dprintk(4, "%s: reading indirect from 0x%04x: %02x\n", ptr->name, reg, value);
88 static void zr36016_writei(struct zr36016 *ptr, u16 reg, u8 value)
90 dprintk(4, "%s: writing indirect 0x%02x to 0x%04x\n", ptr->name,
93 /* just in case something is wrong... */
94 if (ptr->codec->master_data->writereg) {
95 ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
96 ptr->codec->master_data->writereg(ptr->codec, ZR016_IDATA, value & 0x0FF); // DATA
98 pr_err("%s: invalid I/O setup, nothing written (i)!\n", ptr->name);
102 /* =========================================================================
103 Local helper function:
106 ========================================================================= */
108 /* version kept in datastructure */
109 static u8 zr36016_read_version(struct zr36016 *ptr)
111 ptr->version = zr36016_read(ptr, 0) >> 4;
115 /* =========================================================================
116 Local helper function:
118 basic test of "connectivity", writes/reads to/from PAX-Lo register
119 ========================================================================= */
121 static int zr36016_basic_test(struct zr36016 *ptr)
126 zr36016_writei(ptr, ZR016I_PAX_LO, 0x55);
127 dprintk(1, KERN_INFO "%s: registers: ", ptr->name);
128 for (i = 0; i <= 0x0b; i++)
129 dprintk(1, "%02x ", zr36016_readi(ptr, i));
132 // for testing just write 0, then the default value to a register and read
133 // it back in both cases
134 zr36016_writei(ptr, ZR016I_PAX_LO, 0x00);
135 if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0) {
136 pr_err("%s: attach failed, can't connect to vfe processor!\n", ptr->name);
139 zr36016_writei(ptr, ZR016I_PAX_LO, 0x0d0);
140 if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0d0) {
141 pr_err("%s: attach failed, can't connect to vfe processor!\n", ptr->name);
144 // we allow version numbers from 0-3, should be enough, though
145 zr36016_read_version(ptr);
146 if (ptr->version & 0x0c) {
147 pr_err("%s: attach failed, suspicious version %d found...\n", ptr->name,
152 return 0; /* looks good! */
155 /* =========================================================================
156 Local helper function:
158 simple loop for pushing the init datasets - NO USE --
159 ========================================================================= */
162 static int zr36016_pushit(struct zr36016 *ptr,
169 dprintk(4, "%s: write data block to 0x%04x (len=%d)\n",
170 ptr->name, startreg, len);
172 zr36016_writei(ptr, startreg++, data[i++]);
179 /* =========================================================================
180 Basic datasets & init:
183 ========================================================================= */
185 static void zr36016_init(struct zr36016 *ptr)
187 // stop any processing
188 zr36016_write(ptr, ZR016_GOSTOP, 0);
190 // mode setup (yuv422 in and out, compression/expansuon due to mode)
191 zr36016_write(ptr, ZR016_MODE,
192 ZR016_YUV422 | ZR016_YUV422_YUV422 |
193 (ptr->mode == CODEC_DO_COMPRESSION ?
194 ZR016_COMPRESSION : ZR016_EXPANSION));
197 zr36016_writei(ptr, ZR016I_SETUP1,
198 (ptr->xdec ? (ZR016_HRFL | ZR016_HORZ) : 0) |
199 (ptr->ydec ? ZR016_VERT : 0) | ZR016_CNTI);
200 zr36016_writei(ptr, ZR016I_SETUP2, ZR016_CCIR);
203 // (no extra offset for now, norm defines offset, default width height)
204 zr36016_writei(ptr, ZR016I_PAX_HI, ptr->width >> 8);
205 zr36016_writei(ptr, ZR016I_PAX_LO, ptr->width & 0xFF);
206 zr36016_writei(ptr, ZR016I_PAY_HI, ptr->height >> 8);
207 zr36016_writei(ptr, ZR016I_PAY_LO, ptr->height & 0xFF);
208 zr36016_writei(ptr, ZR016I_NAX_HI, ptr->xoff >> 8);
209 zr36016_writei(ptr, ZR016I_NAX_LO, ptr->xoff & 0xFF);
210 zr36016_writei(ptr, ZR016I_NAY_HI, ptr->yoff >> 8);
211 zr36016_writei(ptr, ZR016I_NAY_LO, ptr->yoff & 0xFF);
213 /* shall we continue now, please? */
214 zr36016_write(ptr, ZR016_GOSTOP, 1);
217 /* =========================================================================
220 this functions are accessed by the master via the API structure
221 ========================================================================= */
223 /* set compression/expansion mode and launches codec -
224 this should be the last call from the master before starting processing */
225 static int zr36016_set_mode(struct videocodec *codec, int mode)
227 struct zr36016 *ptr = (struct zr36016 *)codec->data;
229 dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
231 if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
240 /* set picture size */
241 static int zr36016_set_video(struct videocodec *codec, const struct tvnorm *norm,
242 struct vfe_settings *cap, struct vfe_polarity *pol)
244 struct zr36016 *ptr = (struct zr36016 *)codec->data;
246 dprintk(2, "%s: set_video %d.%d, %d/%d-%dx%d (0x%x) call\n",
247 ptr->name, norm->h_start, norm->v_start,
248 cap->x, cap->y, cap->width, cap->height,
251 /* if () return -EINVAL;
252 * trust the master driver that it knows what it does - so
253 * we allow invalid startx/y for now ... */
254 ptr->width = cap->width;
255 ptr->height = cap->height;
256 /* (Ronald) This is ugly. zoran_device.c, line 387
257 * already mentions what happens if h_start is even
258 * (blue faces, etc., cr/cb inversed). There's probably
259 * some good reason why h_start is 0 instead of 1, so I'm
260 * leaving it to this for now, but really... This can be
261 * done a lot simpler */
262 ptr->xoff = (norm->h_start ? norm->h_start : 1) + cap->x;
263 /* Something to note here (I don't understand it), setting
264 * v_start too high will cause the codec to 'not work'. I
265 * really don't get it. values of 16 (v_start) already break
266 * it here. Just '0' seems to work. More testing needed! */
267 ptr->yoff = norm->v_start + cap->y;
268 /* (Ronald) dzjeeh, can't this thing do hor_decimation = 4? */
269 ptr->xdec = ((cap->decimation & 0xff) == 1) ? 0 : 1;
270 ptr->ydec = (((cap->decimation >> 8) & 0xff) == 1) ? 0 : 1;
275 /* additional control functions */
276 static int zr36016_control(struct videocodec *codec, int type, int size, void *data)
278 struct zr36016 *ptr = (struct zr36016 *)codec->data;
279 int *ival = (int *)data;
281 dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type, size);
284 case CODEC_G_STATUS: /* get last status - we don't know it ... */
285 if (size != sizeof(int))
290 case CODEC_G_CODEC_MODE:
291 if (size != sizeof(int))
296 case CODEC_S_CODEC_MODE:
297 if (size != sizeof(int))
301 /* not needed, do nothing */
309 /* not available, give an error */
319 /* =========================================================================
320 Exit and unregister function:
322 Deinitializes Zoran's JPEG processor
323 ========================================================================= */
325 static int zr36016_unset(struct videocodec *codec)
327 struct zr36016 *ptr = codec->data;
330 /* do wee need some codec deinit here, too ???? */
332 dprintk(1, "%s: finished codec #%d\n", ptr->name, ptr->num);
343 /* =========================================================================
344 Setup and registry function:
346 Initializes Zoran's JPEG processor
348 Also sets pixel size, average code size, mode (compr./decompr.)
349 (the given size is determined by the processor with the video interface)
350 ========================================================================= */
352 static int zr36016_setup(struct videocodec *codec)
357 dprintk(2, "zr36016: initializing VFE subsystem #%d.\n", zr36016_codecs);
359 if (zr36016_codecs == MAX_CODECS) {
360 pr_err("zr36016: Can't attach more codecs!\n");
364 ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
369 snprintf(ptr->name, sizeof(ptr->name), "zr36016[%d]", zr36016_codecs);
370 ptr->num = zr36016_codecs++;
374 res = zr36016_basic_test(ptr);
376 zr36016_unset(codec);
380 ptr->mode = CODEC_DO_COMPRESSION;
387 dprintk(1, KERN_INFO "%s: codec v%d attached and running\n", ptr->name, ptr->version);
392 static const struct videocodec zr36016_codec = {
394 .magic = 0L, /* magic not used */
396 CODEC_FLAG_HARDWARE | CODEC_FLAG_VFE | CODEC_FLAG_ENCODER |
398 .type = CODEC_TYPE_ZR36016,
399 .setup = zr36016_setup, /* functionality */
400 .unset = zr36016_unset,
401 .set_mode = zr36016_set_mode,
402 .set_video = zr36016_set_video,
403 .control = zr36016_control,
404 /* others are not used */
407 /* =========================================================================
408 HOOK IN DRIVER AS KERNEL MODULE
409 ========================================================================= */
411 int zr36016_init_module(void)
414 return videocodec_register(&zr36016_codec);
417 void zr36016_cleanup_module(void)
419 if (zr36016_codecs) {
421 "zr36016: something's wrong - %d codecs left somehow.\n",
424 videocodec_unregister(&zr36016_codec);