GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / staging / media / zoran / zr36016.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Zoran ZR36016 basic configuration functions
4  *
5  * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
6  */
7
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11
12 /* headerfile of this module */
13 #include "zr36016.h"
14
15 /* codec io API */
16 #include "videocodec.h"
17
18 /* it doesn't make sense to have more than 20 or so,
19   just to prevent some unwanted loops */
20 #define MAX_CODECS 20
21
22 /* amount of chips attached via this driver */
23 static int zr36016_codecs;
24
25 static int zr36016_debug;
26 module_param(zr36016_debug, int, 0);
27 MODULE_PARM_DESC(zr36016_debug, "Debug level (0-4)");
28
29
30 #define dprintk(num, format, args...) \
31         do { \
32                 if (zr36016_debug >= num) \
33                         printk(format, ##args); \
34         } while (0)
35
36 /* =========================================================================
37    Local hardware I/O functions:
38
39    read/write via codec layer (registers are located in the master device)
40    ========================================================================= */
41
42 /* read and write functions */
43 static u8 zr36016_read(struct zr36016 *ptr, u16 reg)
44 {
45         u8 value = 0;
46
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;
50         else
51                 pr_err("%s: invalid I/O setup, nothing read!\n", ptr->name);
52
53         dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr->name, reg, value);
54
55         return value;
56 }
57
58 static void zr36016_write(struct zr36016 *ptr, u16 reg, u8 value)
59 {
60         dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr->name, value, reg);
61
62         // just in case something is wrong...
63         if (ptr->codec->master_data->writereg)
64                 ptr->codec->master_data->writereg(ptr->codec, reg, value);
65         else
66                 pr_err("%s: invalid I/O setup, nothing written!\n", ptr->name);
67 }
68
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)
73 {
74         u8 value = 0;
75
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
80         } else {
81                 pr_err("%s: invalid I/O setup, nothing read (i)!\n", ptr->name);
82         }
83
84         dprintk(4, "%s: reading indirect from 0x%04x: %02x\n", ptr->name, reg, value);
85         return value;
86 }
87
88 static void zr36016_writei(struct zr36016 *ptr, u16 reg, u8 value)
89 {
90         dprintk(4, "%s: writing indirect 0x%02x to 0x%04x\n", ptr->name,
91                 value, reg);
92
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
97         } else {
98                 pr_err("%s: invalid I/O setup, nothing written (i)!\n", ptr->name);
99         }
100 }
101
102 /* =========================================================================
103    Local helper function:
104
105    version read
106    ========================================================================= */
107
108 /* version kept in datastructure */
109 static u8 zr36016_read_version(struct zr36016 *ptr)
110 {
111         ptr->version = zr36016_read(ptr, 0) >> 4;
112         return ptr->version;
113 }
114
115 /* =========================================================================
116    Local helper function:
117
118    basic test of "connectivity", writes/reads to/from PAX-Lo register
119    ========================================================================= */
120
121 static int zr36016_basic_test(struct zr36016 *ptr)
122 {
123         if (zr36016_debug) {
124                 int i;
125
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));
130                 dprintk(1, "\n");
131         }
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);
137                 return -ENXIO;
138         }
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);
142                 return -ENXIO;
143         }
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,
148                        ptr->version);
149                 return -ENXIO;
150         }
151
152         return 0;               /* looks good! */
153 }
154
155 /* =========================================================================
156    Local helper function:
157
158    simple loop for pushing the init datasets - NO USE --
159    ========================================================================= */
160
161 #if 0
162 static int zr36016_pushit(struct zr36016 *ptr,
163                           u16             startreg,
164                            u16             len,
165                            const char     *data)
166 {
167         int i = 0;
168
169         dprintk(4, "%s: write data block to 0x%04x (len=%d)\n",
170                 ptr->name, startreg, len);
171         while (i < len) {
172                 zr36016_writei(ptr, startreg++,  data[i++]);
173         }
174
175         return i;
176 }
177 #endif
178
179 /* =========================================================================
180    Basic datasets & init:
181
182    //TODO//
183    ========================================================================= */
184
185 static void zr36016_init(struct zr36016 *ptr)
186 {
187         // stop any processing
188         zr36016_write(ptr, ZR016_GOSTOP, 0);
189
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));
195
196         // misc setup
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);
201
202         // Window setup
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);
212
213         /* shall we continue now, please? */
214         zr36016_write(ptr, ZR016_GOSTOP, 1);
215 }
216
217 /* =========================================================================
218    CODEC API FUNCTIONS
219
220    this functions are accessed by the master via the API structure
221    ========================================================================= */
222
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)
226 {
227         struct zr36016 *ptr = (struct zr36016 *)codec->data;
228
229         dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
230
231         if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
232                 return -EINVAL;
233
234         ptr->mode = mode;
235         zr36016_init(ptr);
236
237         return 0;
238 }
239
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)
243 {
244         struct zr36016 *ptr = (struct zr36016 *)codec->data;
245
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,
249                 cap->decimation);
250
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;
271
272         return 0;
273 }
274
275 /* additional control functions */
276 static int zr36016_control(struct videocodec *codec, int type, int size, void *data)
277 {
278         struct zr36016 *ptr = (struct zr36016 *)codec->data;
279         int *ival = (int *)data;
280
281         dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type, size);
282
283         switch (type) {
284         case CODEC_G_STATUS:    /* get last status - we don't know it ... */
285                 if (size != sizeof(int))
286                         return -EFAULT;
287                 *ival = 0;
288                 break;
289
290         case CODEC_G_CODEC_MODE:
291                 if (size != sizeof(int))
292                         return -EFAULT;
293                 *ival = 0;
294                 break;
295
296         case CODEC_S_CODEC_MODE:
297                 if (size != sizeof(int))
298                         return -EFAULT;
299                 if (*ival != 0)
300                         return -EINVAL;
301                 /* not needed, do nothing */
302                 return 0;
303
304         case CODEC_G_VFE:
305         case CODEC_S_VFE:
306                 return 0;
307
308         case CODEC_S_MMAP:
309                 /* not available, give an error */
310                 return -ENXIO;
311
312         default:
313                 return -EINVAL;
314         }
315
316         return size;
317 }
318
319 /* =========================================================================
320    Exit and unregister function:
321
322    Deinitializes Zoran's JPEG processor
323    ========================================================================= */
324
325 static int zr36016_unset(struct videocodec *codec)
326 {
327         struct zr36016 *ptr = codec->data;
328
329         if (ptr) {
330                 /* do wee need some codec deinit here, too ???? */
331
332                 dprintk(1, "%s: finished codec #%d\n", ptr->name, ptr->num);
333                 kfree(ptr);
334                 codec->data = NULL;
335
336                 zr36016_codecs--;
337                 return 0;
338         }
339
340         return -EFAULT;
341 }
342
343 /* =========================================================================
344    Setup and registry function:
345
346    Initializes Zoran's JPEG processor
347
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    ========================================================================= */
351
352 static int zr36016_setup(struct videocodec *codec)
353 {
354         struct zr36016 *ptr;
355         int res;
356
357         dprintk(2, "zr36016: initializing VFE subsystem #%d.\n", zr36016_codecs);
358
359         if (zr36016_codecs == MAX_CODECS) {
360                 pr_err("zr36016: Can't attach more codecs!\n");
361                 return -ENOSPC;
362         }
363         //mem structure init
364         ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
365         codec->data = ptr;
366         if (!ptr)
367                 return -ENOMEM;
368
369         snprintf(ptr->name, sizeof(ptr->name), "zr36016[%d]", zr36016_codecs);
370         ptr->num = zr36016_codecs++;
371         ptr->codec = codec;
372
373         //testing
374         res = zr36016_basic_test(ptr);
375         if (res < 0) {
376                 zr36016_unset(codec);
377                 return res;
378         }
379         //final setup
380         ptr->mode = CODEC_DO_COMPRESSION;
381         ptr->width = 768;
382         ptr->height = 288;
383         ptr->xdec = 1;
384         ptr->ydec = 0;
385         zr36016_init(ptr);
386
387         dprintk(1, KERN_INFO "%s: codec v%d attached and running\n", ptr->name, ptr->version);
388
389         return 0;
390 }
391
392 static const struct videocodec zr36016_codec = {
393         .name = "zr36016",
394         .magic = 0L,            /* magic not used */
395         .flags =
396             CODEC_FLAG_HARDWARE | CODEC_FLAG_VFE | CODEC_FLAG_ENCODER |
397             CODEC_FLAG_DECODER,
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 */
405 };
406
407 /* =========================================================================
408    HOOK IN DRIVER AS KERNEL MODULE
409    ========================================================================= */
410
411 int zr36016_init_module(void)
412 {
413         zr36016_codecs = 0;
414         return videocodec_register(&zr36016_codec);
415 }
416
417 void zr36016_cleanup_module(void)
418 {
419         if (zr36016_codecs) {
420                 dprintk(1,
421                         "zr36016: something's wrong - %d codecs left somehow.\n",
422                         zr36016_codecs);
423         }
424         videocodec_unregister(&zr36016_codec);
425 }