3 * Support for a cx23417 mpeg encoder via cx23885 host port.
5 * (c) 2004 Jelle Foks <jelle@foks.us>
6 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
7 * (c) 2008 Steven Toth <stoth@linuxtv.org>
8 * - CX23885/7/8 support
10 * Includes parts from the ivtv driver <http://sourceforge.net/projects/ivtv/>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
24 #include "cx23885-ioctl.h"
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/firmware.h>
33 #include <linux/slab.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-ioctl.h>
36 #include <media/drv-intf/cx2341x.h>
38 #define CX23885_FIRM_IMAGE_SIZE 376836
39 #define CX23885_FIRM_IMAGE_NAME "v4l-cx23885-enc.fw"
41 static unsigned int mpegbufs
= 32;
42 module_param(mpegbufs
, int, 0644);
43 MODULE_PARM_DESC(mpegbufs
, "number of mpeg buffers, range 2-32");
44 static unsigned int mpeglines
= 32;
45 module_param(mpeglines
, int, 0644);
46 MODULE_PARM_DESC(mpeglines
, "number of lines in an MPEG buffer, range 2-32");
47 static unsigned int mpeglinesize
= 512;
48 module_param(mpeglinesize
, int, 0644);
49 MODULE_PARM_DESC(mpeglinesize
,
50 "number of bytes in each line of an MPEG buffer, range 512-1024");
52 static unsigned int v4l_debug
;
53 module_param(v4l_debug
, int, 0644);
54 MODULE_PARM_DESC(v4l_debug
, "enable V4L debug messages");
56 #define dprintk(level, fmt, arg...)\
57 do { if (v4l_debug >= level) \
58 printk(KERN_DEBUG pr_fmt("%s: 417:" fmt), \
62 static struct cx23885_tvnorm cx23885_tvnorms
[] = {
65 .id
= V4L2_STD_NTSC_M
,
68 .id
= V4L2_STD_NTSC_M_JP
,
71 .id
= V4L2_STD_PAL_BG
,
74 .id
= V4L2_STD_PAL_DK
,
86 .id
= V4L2_STD_PAL_Nc
,
89 .id
= V4L2_STD_PAL_60
,
92 .id
= V4L2_STD_SECAM_L
,
95 .id
= V4L2_STD_SECAM_DK
,
99 /* ------------------------------------------------------------------ */
100 enum cx23885_capture_type
{
101 CX23885_MPEG_CAPTURE
,
103 CX23885_RAW_PASSTHRU_CAPTURE
105 enum cx23885_capture_bits
{
106 CX23885_RAW_BITS_NONE
= 0x00,
107 CX23885_RAW_BITS_YUV_CAPTURE
= 0x01,
108 CX23885_RAW_BITS_PCM_CAPTURE
= 0x02,
109 CX23885_RAW_BITS_VBI_CAPTURE
= 0x04,
110 CX23885_RAW_BITS_PASSTHRU_CAPTURE
= 0x08,
111 CX23885_RAW_BITS_TO_HOST_CAPTURE
= 0x10
113 enum cx23885_capture_end
{
114 CX23885_END_AT_GOP
, /* stop at the end of gop, generate irq */
115 CX23885_END_NOW
, /* stop immediately, no irq */
117 enum cx23885_framerate
{
118 CX23885_FRAMERATE_NTSC_30
, /* NTSC: 30fps */
119 CX23885_FRAMERATE_PAL_25
/* PAL: 25fps */
121 enum cx23885_stream_port
{
122 CX23885_OUTPUT_PORT_MEMORY
,
123 CX23885_OUTPUT_PORT_STREAMING
,
124 CX23885_OUTPUT_PORT_SERIAL
126 enum cx23885_data_xfer_status
{
127 CX23885_MORE_BUFFERS_FOLLOW
,
130 enum cx23885_picture_mask
{
131 CX23885_PICTURE_MASK_NONE
,
132 CX23885_PICTURE_MASK_I_FRAMES
,
133 CX23885_PICTURE_MASK_I_P_FRAMES
= 0x3,
134 CX23885_PICTURE_MASK_ALL_FRAMES
= 0x7,
136 enum cx23885_vbi_mode_bits
{
137 CX23885_VBI_BITS_SLICED
,
138 CX23885_VBI_BITS_RAW
,
140 enum cx23885_vbi_insertion_bits
{
141 CX23885_VBI_BITS_INSERT_IN_XTENSION_USR_DATA
,
142 CX23885_VBI_BITS_INSERT_IN_PRIVATE_PACKETS
= 0x1 << 1,
143 CX23885_VBI_BITS_SEPARATE_STREAM
= 0x2 << 1,
144 CX23885_VBI_BITS_SEPARATE_STREAM_USR_DATA
= 0x4 << 1,
145 CX23885_VBI_BITS_SEPARATE_STREAM_PRV_DATA
= 0x5 << 1,
147 enum cx23885_dma_unit
{
151 enum cx23885_dma_transfer_status_bits
{
152 CX23885_DMA_TRANSFER_BITS_DONE
= 0x01,
153 CX23885_DMA_TRANSFER_BITS_ERROR
= 0x04,
154 CX23885_DMA_TRANSFER_BITS_LL_ERROR
= 0x10,
157 CX23885_PAUSE_ENCODING
,
158 CX23885_RESUME_ENCODING
,
160 enum cx23885_copyright
{
161 CX23885_COPYRIGHT_OFF
,
162 CX23885_COPYRIGHT_ON
,
164 enum cx23885_notification_type
{
165 CX23885_NOTIFICATION_REFRESH
,
167 enum cx23885_notification_status
{
168 CX23885_NOTIFICATION_OFF
,
169 CX23885_NOTIFICATION_ON
,
171 enum cx23885_notification_mailbox
{
172 CX23885_NOTIFICATION_NO_MAILBOX
= -1,
174 enum cx23885_field1_lines
{
175 CX23885_FIELD1_SAA7114
= 0x00EF, /* 239 */
176 CX23885_FIELD1_SAA7115
= 0x00F0, /* 240 */
177 CX23885_FIELD1_MICRONAS
= 0x0105, /* 261 */
179 enum cx23885_field2_lines
{
180 CX23885_FIELD2_SAA7114
= 0x00EF, /* 239 */
181 CX23885_FIELD2_SAA7115
= 0x00F0, /* 240 */
182 CX23885_FIELD2_MICRONAS
= 0x0106, /* 262 */
184 enum cx23885_custom_data_type
{
185 CX23885_CUSTOM_EXTENSION_USR_DATA
,
186 CX23885_CUSTOM_PRIVATE_PACKET
,
192 enum cx23885_mute_video_mask
{
193 CX23885_MUTE_VIDEO_V_MASK
= 0x0000FF00,
194 CX23885_MUTE_VIDEO_U_MASK
= 0x00FF0000,
195 CX23885_MUTE_VIDEO_Y_MASK
= 0xFF000000,
197 enum cx23885_mute_video_shift
{
198 CX23885_MUTE_VIDEO_V_SHIFT
= 8,
199 CX23885_MUTE_VIDEO_U_SHIFT
= 16,
200 CX23885_MUTE_VIDEO_Y_SHIFT
= 24,
203 /* defines below are from ivtv-driver.h */
204 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
206 /* Firmware API commands */
207 #define IVTV_API_STD_TIMEOUT 500
210 /* IVTV_REG_OFFSET */
211 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
212 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
213 #define IVTV_REG_SPU (0x9050)
214 #define IVTV_REG_HW_BLOCKS (0x9054)
215 #define IVTV_REG_VPU (0x9058)
216 #define IVTV_REG_APU (0xA064)
218 /**** Bit definitions for MC417_RWD and MC417_OEN registers ***
223 bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
224 +-------+-------+-------+-------+-------+-------+-------+-------+
225 | MIWR# | MIRD# | MICS# |MIRDY# |MIADDR3|MIADDR2|MIADDR1|MIADDR0|
226 +-------+-------+-------+-------+-------+-------+-------+-------+
227 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
228 +-------+-------+-------+-------+-------+-------+-------+-------+
229 |MIDATA7|MIDATA6|MIDATA5|MIDATA4|MIDATA3|MIDATA2|MIDATA1|MIDATA0|
230 +-------+-------+-------+-------+-------+-------+-------+-------+
232 #define MC417_MIWR 0x8000
233 #define MC417_MIRD 0x4000
234 #define MC417_MICS 0x2000
235 #define MC417_MIRDY 0x1000
236 #define MC417_MIADDR 0x0F00
237 #define MC417_MIDATA 0x00FF
239 /* MIADDR* nibble definitions */
240 #define MCI_MEMORY_DATA_BYTE0 0x000
241 #define MCI_MEMORY_DATA_BYTE1 0x100
242 #define MCI_MEMORY_DATA_BYTE2 0x200
243 #define MCI_MEMORY_DATA_BYTE3 0x300
244 #define MCI_MEMORY_ADDRESS_BYTE2 0x400
245 #define MCI_MEMORY_ADDRESS_BYTE1 0x500
246 #define MCI_MEMORY_ADDRESS_BYTE0 0x600
247 #define MCI_REGISTER_DATA_BYTE0 0x800
248 #define MCI_REGISTER_DATA_BYTE1 0x900
249 #define MCI_REGISTER_DATA_BYTE2 0xA00
250 #define MCI_REGISTER_DATA_BYTE3 0xB00
251 #define MCI_REGISTER_ADDRESS_BYTE0 0xC00
252 #define MCI_REGISTER_ADDRESS_BYTE1 0xD00
253 #define MCI_REGISTER_MODE 0xE00
255 /* Read and write modes */
256 #define MCI_MODE_REGISTER_READ 0
257 #define MCI_MODE_REGISTER_WRITE 1
258 #define MCI_MODE_MEMORY_READ 0
259 #define MCI_MODE_MEMORY_WRITE 0x40
261 /*** Bit definitions for MC417_CTL register ****
262 bits 31-6 bits 5-4 bit 3 bits 2-1 Bit 0
263 +--------+-------------+--------+--------------+------------+
264 |Reserved|MC417_SPD_CTL|Reserved|MC417_GPIO_SEL|UART_GPIO_EN|
265 +--------+-------------+--------+--------------+------------+
267 #define MC417_SPD_CTL(x) (((x) << 4) & 0x00000030)
268 #define MC417_GPIO_SEL(x) (((x) << 1) & 0x00000006)
269 #define MC417_UART_GPIO_EN 0x00000001
271 /* Values for speed control */
272 #define MC417_SPD_CTL_SLOW 0x1
273 #define MC417_SPD_CTL_MEDIUM 0x0
274 #define MC417_SPD_CTL_FAST 0x3 /* b'1x, but we use b'11 */
276 /* Values for GPIO select */
277 #define MC417_GPIO_SEL_GPIO3 0x3
278 #define MC417_GPIO_SEL_GPIO2 0x2
279 #define MC417_GPIO_SEL_GPIO1 0x1
280 #define MC417_GPIO_SEL_GPIO0 0x0
282 void cx23885_mc417_init(struct cx23885_dev
*dev
)
286 dprintk(2, "%s()\n", __func__
);
288 /* Configure MC417_CTL register to defaults. */
289 regval
= MC417_SPD_CTL(MC417_SPD_CTL_FAST
) |
290 MC417_GPIO_SEL(MC417_GPIO_SEL_GPIO3
) |
292 cx_write(MC417_CTL
, regval
);
294 /* Configure MC417_OEN to defaults. */
295 regval
= MC417_MIRDY
;
296 cx_write(MC417_OEN
, regval
);
298 /* Configure MC417_RWD to defaults. */
299 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
;
300 cx_write(MC417_RWD
, regval
);
303 static int mc417_wait_ready(struct cx23885_dev
*dev
)
306 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1);
309 mi_ready
= cx_read(MC417_RWD
) & MC417_MIRDY
;
312 if (time_after(jiffies
, timeout
))
318 int mc417_register_write(struct cx23885_dev
*dev
, u16 address
, u32 value
)
322 /* Enable MC417 GPIO outputs except for MC417_MIRDY,
325 cx_write(MC417_OEN
, MC417_MIRDY
);
327 /* Write data byte 0 */
328 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE0
|
329 (value
& 0x000000FF);
330 cx_write(MC417_RWD
, regval
);
332 /* Transition CS/WR to effect write transaction across bus. */
333 regval
|= MC417_MICS
| MC417_MIWR
;
334 cx_write(MC417_RWD
, regval
);
336 /* Write data byte 1 */
337 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE1
|
338 ((value
>> 8) & 0x000000FF);
339 cx_write(MC417_RWD
, regval
);
340 regval
|= MC417_MICS
| MC417_MIWR
;
341 cx_write(MC417_RWD
, regval
);
343 /* Write data byte 2 */
344 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE2
|
345 ((value
>> 16) & 0x000000FF);
346 cx_write(MC417_RWD
, regval
);
347 regval
|= MC417_MICS
| MC417_MIWR
;
348 cx_write(MC417_RWD
, regval
);
350 /* Write data byte 3 */
351 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE3
|
352 ((value
>> 24) & 0x000000FF);
353 cx_write(MC417_RWD
, regval
);
354 regval
|= MC417_MICS
| MC417_MIWR
;
355 cx_write(MC417_RWD
, regval
);
357 /* Write address byte 0 */
358 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_ADDRESS_BYTE0
|
360 cx_write(MC417_RWD
, regval
);
361 regval
|= MC417_MICS
| MC417_MIWR
;
362 cx_write(MC417_RWD
, regval
);
364 /* Write address byte 1 */
365 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_ADDRESS_BYTE1
|
366 ((address
>> 8) & 0xFF);
367 cx_write(MC417_RWD
, regval
);
368 regval
|= MC417_MICS
| MC417_MIWR
;
369 cx_write(MC417_RWD
, regval
);
371 /* Indicate that this is a write. */
372 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_MODE
|
373 MCI_MODE_REGISTER_WRITE
;
374 cx_write(MC417_RWD
, regval
);
375 regval
|= MC417_MICS
| MC417_MIWR
;
376 cx_write(MC417_RWD
, regval
);
378 /* Wait for the trans to complete (MC417_MIRDY asserted). */
379 return mc417_wait_ready(dev
);
382 int mc417_register_read(struct cx23885_dev
*dev
, u16 address
, u32
*value
)
389 /* Enable MC417 GPIO outputs except for MC417_MIRDY,
392 cx_write(MC417_OEN
, MC417_MIRDY
);
394 /* Write address byte 0 */
395 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_ADDRESS_BYTE0
|
396 ((address
& 0x00FF));
397 cx_write(MC417_RWD
, regval
);
398 regval
|= MC417_MICS
| MC417_MIWR
;
399 cx_write(MC417_RWD
, regval
);
401 /* Write address byte 1 */
402 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_ADDRESS_BYTE1
|
403 ((address
>> 8) & 0xFF);
404 cx_write(MC417_RWD
, regval
);
405 regval
|= MC417_MICS
| MC417_MIWR
;
406 cx_write(MC417_RWD
, regval
);
408 /* Indicate that this is a register read. */
409 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_MODE
|
410 MCI_MODE_REGISTER_READ
;
411 cx_write(MC417_RWD
, regval
);
412 regval
|= MC417_MICS
| MC417_MIWR
;
413 cx_write(MC417_RWD
, regval
);
415 /* Wait for the trans to complete (MC417_MIRDY asserted). */
416 retval
= mc417_wait_ready(dev
);
418 /* switch the DAT0-7 GPIO[10:3] to input mode */
419 cx_write(MC417_OEN
, MC417_MIRDY
| MC417_MIDATA
);
421 /* Read data byte 0 */
422 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE0
;
423 cx_write(MC417_RWD
, regval
);
425 /* Transition RD to effect read transaction across bus.
426 * Transition 0x5000 -> 0x9000 correct (RD/RDY -> WR/RDY)?
427 * Should it be 0x9000 -> 0xF000 (also why is RDY being set, its
430 regval
= MC417_MIWR
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE0
;
431 cx_write(MC417_RWD
, regval
);
434 tempval
= cx_read(MC417_RWD
);
435 dataval
= tempval
& 0x000000FF;
437 /* Bring CS and RD high. */
438 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
| MC417_MIRDY
;
439 cx_write(MC417_RWD
, regval
);
441 /* Read data byte 1 */
442 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE1
;
443 cx_write(MC417_RWD
, regval
);
444 regval
= MC417_MIWR
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE1
;
445 cx_write(MC417_RWD
, regval
);
446 tempval
= cx_read(MC417_RWD
);
447 dataval
|= ((tempval
& 0x000000FF) << 8);
448 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
| MC417_MIRDY
;
449 cx_write(MC417_RWD
, regval
);
451 /* Read data byte 2 */
452 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE2
;
453 cx_write(MC417_RWD
, regval
);
454 regval
= MC417_MIWR
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE2
;
455 cx_write(MC417_RWD
, regval
);
456 tempval
= cx_read(MC417_RWD
);
457 dataval
|= ((tempval
& 0x000000FF) << 16);
458 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
| MC417_MIRDY
;
459 cx_write(MC417_RWD
, regval
);
461 /* Read data byte 3 */
462 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE3
;
463 cx_write(MC417_RWD
, regval
);
464 regval
= MC417_MIWR
| MC417_MIRDY
| MCI_REGISTER_DATA_BYTE3
;
465 cx_write(MC417_RWD
, regval
);
466 tempval
= cx_read(MC417_RWD
);
467 dataval
|= ((tempval
& 0x000000FF) << 24);
468 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
| MC417_MIRDY
;
469 cx_write(MC417_RWD
, regval
);
476 int mc417_memory_write(struct cx23885_dev
*dev
, u32 address
, u32 value
)
480 /* Enable MC417 GPIO outputs except for MC417_MIRDY,
483 cx_write(MC417_OEN
, MC417_MIRDY
);
485 /* Write data byte 0 */
486 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE0
|
487 (value
& 0x000000FF);
488 cx_write(MC417_RWD
, regval
);
490 /* Transition CS/WR to effect write transaction across bus. */
491 regval
|= MC417_MICS
| MC417_MIWR
;
492 cx_write(MC417_RWD
, regval
);
494 /* Write data byte 1 */
495 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE1
|
496 ((value
>> 8) & 0x000000FF);
497 cx_write(MC417_RWD
, regval
);
498 regval
|= MC417_MICS
| MC417_MIWR
;
499 cx_write(MC417_RWD
, regval
);
501 /* Write data byte 2 */
502 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE2
|
503 ((value
>> 16) & 0x000000FF);
504 cx_write(MC417_RWD
, regval
);
505 regval
|= MC417_MICS
| MC417_MIWR
;
506 cx_write(MC417_RWD
, regval
);
508 /* Write data byte 3 */
509 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE3
|
510 ((value
>> 24) & 0x000000FF);
511 cx_write(MC417_RWD
, regval
);
512 regval
|= MC417_MICS
| MC417_MIWR
;
513 cx_write(MC417_RWD
, regval
);
515 /* Write address byte 2 */
516 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_ADDRESS_BYTE2
|
517 MCI_MODE_MEMORY_WRITE
| ((address
>> 16) & 0x3F);
518 cx_write(MC417_RWD
, regval
);
519 regval
|= MC417_MICS
| MC417_MIWR
;
520 cx_write(MC417_RWD
, regval
);
522 /* Write address byte 1 */
523 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_ADDRESS_BYTE1
|
524 ((address
>> 8) & 0xFF);
525 cx_write(MC417_RWD
, regval
);
526 regval
|= MC417_MICS
| MC417_MIWR
;
527 cx_write(MC417_RWD
, regval
);
529 /* Write address byte 0 */
530 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_ADDRESS_BYTE0
|
532 cx_write(MC417_RWD
, regval
);
533 regval
|= MC417_MICS
| MC417_MIWR
;
534 cx_write(MC417_RWD
, regval
);
536 /* Wait for the trans to complete (MC417_MIRDY asserted). */
537 return mc417_wait_ready(dev
);
540 int mc417_memory_read(struct cx23885_dev
*dev
, u32 address
, u32
*value
)
547 /* Enable MC417 GPIO outputs except for MC417_MIRDY,
550 cx_write(MC417_OEN
, MC417_MIRDY
);
552 /* Write address byte 2 */
553 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_ADDRESS_BYTE2
|
554 MCI_MODE_MEMORY_READ
| ((address
>> 16) & 0x3F);
555 cx_write(MC417_RWD
, regval
);
556 regval
|= MC417_MICS
| MC417_MIWR
;
557 cx_write(MC417_RWD
, regval
);
559 /* Write address byte 1 */
560 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_ADDRESS_BYTE1
|
561 ((address
>> 8) & 0xFF);
562 cx_write(MC417_RWD
, regval
);
563 regval
|= MC417_MICS
| MC417_MIWR
;
564 cx_write(MC417_RWD
, regval
);
566 /* Write address byte 0 */
567 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_ADDRESS_BYTE0
|
569 cx_write(MC417_RWD
, regval
);
570 regval
|= MC417_MICS
| MC417_MIWR
;
571 cx_write(MC417_RWD
, regval
);
573 /* Wait for the trans to complete (MC417_MIRDY asserted). */
574 retval
= mc417_wait_ready(dev
);
576 /* switch the DAT0-7 GPIO[10:3] to input mode */
577 cx_write(MC417_OEN
, MC417_MIRDY
| MC417_MIDATA
);
579 /* Read data byte 3 */
580 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE3
;
581 cx_write(MC417_RWD
, regval
);
583 /* Transition RD to effect read transaction across bus. */
584 regval
= MC417_MIWR
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE3
;
585 cx_write(MC417_RWD
, regval
);
588 tempval
= cx_read(MC417_RWD
);
589 dataval
= ((tempval
& 0x000000FF) << 24);
591 /* Bring CS and RD high. */
592 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
| MC417_MIRDY
;
593 cx_write(MC417_RWD
, regval
);
595 /* Read data byte 2 */
596 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE2
;
597 cx_write(MC417_RWD
, regval
);
598 regval
= MC417_MIWR
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE2
;
599 cx_write(MC417_RWD
, regval
);
600 tempval
= cx_read(MC417_RWD
);
601 dataval
|= ((tempval
& 0x000000FF) << 16);
602 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
| MC417_MIRDY
;
603 cx_write(MC417_RWD
, regval
);
605 /* Read data byte 1 */
606 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE1
;
607 cx_write(MC417_RWD
, regval
);
608 regval
= MC417_MIWR
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE1
;
609 cx_write(MC417_RWD
, regval
);
610 tempval
= cx_read(MC417_RWD
);
611 dataval
|= ((tempval
& 0x000000FF) << 8);
612 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
| MC417_MIRDY
;
613 cx_write(MC417_RWD
, regval
);
615 /* Read data byte 0 */
616 regval
= MC417_MIRD
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE0
;
617 cx_write(MC417_RWD
, regval
);
618 regval
= MC417_MIWR
| MC417_MIRDY
| MCI_MEMORY_DATA_BYTE0
;
619 cx_write(MC417_RWD
, regval
);
620 tempval
= cx_read(MC417_RWD
);
621 dataval
|= (tempval
& 0x000000FF);
622 regval
= MC417_MIWR
| MC417_MIRD
| MC417_MICS
| MC417_MIRDY
;
623 cx_write(MC417_RWD
, regval
);
630 void mc417_gpio_set(struct cx23885_dev
*dev
, u32 mask
)
634 /* Set the gpio value */
635 mc417_register_read(dev
, 0x900C, &val
);
636 val
|= (mask
& 0x000ffff);
637 mc417_register_write(dev
, 0x900C, val
);
640 void mc417_gpio_clear(struct cx23885_dev
*dev
, u32 mask
)
644 /* Clear the gpio value */
645 mc417_register_read(dev
, 0x900C, &val
);
646 val
&= ~(mask
& 0x0000ffff);
647 mc417_register_write(dev
, 0x900C, val
);
650 void mc417_gpio_enable(struct cx23885_dev
*dev
, u32 mask
, int asoutput
)
654 /* Enable GPIO direction bits */
655 mc417_register_read(dev
, 0x9020, &val
);
657 val
|= (mask
& 0x0000ffff);
659 val
&= ~(mask
& 0x0000ffff);
661 mc417_register_write(dev
, 0x9020, val
);
663 /* ------------------------------------------------------------------ */
665 /* MPEG encoder API */
666 static char *cmd_to_str(int cmd
)
669 case CX2341X_ENC_PING_FW
:
671 case CX2341X_ENC_START_CAPTURE
:
672 return "START_CAPTURE";
673 case CX2341X_ENC_STOP_CAPTURE
:
674 return "STOP_CAPTURE";
675 case CX2341X_ENC_SET_AUDIO_ID
:
676 return "SET_AUDIO_ID";
677 case CX2341X_ENC_SET_VIDEO_ID
:
678 return "SET_VIDEO_ID";
679 case CX2341X_ENC_SET_PCR_ID
:
681 case CX2341X_ENC_SET_FRAME_RATE
:
682 return "SET_FRAME_RATE";
683 case CX2341X_ENC_SET_FRAME_SIZE
:
684 return "SET_FRAME_SIZE";
685 case CX2341X_ENC_SET_BIT_RATE
:
686 return "SET_BIT_RATE";
687 case CX2341X_ENC_SET_GOP_PROPERTIES
:
688 return "SET_GOP_PROPERTIES";
689 case CX2341X_ENC_SET_ASPECT_RATIO
:
690 return "SET_ASPECT_RATIO";
691 case CX2341X_ENC_SET_DNR_FILTER_MODE
:
692 return "SET_DNR_FILTER_MODE";
693 case CX2341X_ENC_SET_DNR_FILTER_PROPS
:
694 return "SET_DNR_FILTER_PROPS";
695 case CX2341X_ENC_SET_CORING_LEVELS
:
696 return "SET_CORING_LEVELS";
697 case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE
:
698 return "SET_SPATIAL_FILTER_TYPE";
699 case CX2341X_ENC_SET_VBI_LINE
:
700 return "SET_VBI_LINE";
701 case CX2341X_ENC_SET_STREAM_TYPE
:
702 return "SET_STREAM_TYPE";
703 case CX2341X_ENC_SET_OUTPUT_PORT
:
704 return "SET_OUTPUT_PORT";
705 case CX2341X_ENC_SET_AUDIO_PROPERTIES
:
706 return "SET_AUDIO_PROPERTIES";
707 case CX2341X_ENC_HALT_FW
:
709 case CX2341X_ENC_GET_VERSION
:
710 return "GET_VERSION";
711 case CX2341X_ENC_SET_GOP_CLOSURE
:
712 return "SET_GOP_CLOSURE";
713 case CX2341X_ENC_GET_SEQ_END
:
714 return "GET_SEQ_END";
715 case CX2341X_ENC_SET_PGM_INDEX_INFO
:
716 return "SET_PGM_INDEX_INFO";
717 case CX2341X_ENC_SET_VBI_CONFIG
:
718 return "SET_VBI_CONFIG";
719 case CX2341X_ENC_SET_DMA_BLOCK_SIZE
:
720 return "SET_DMA_BLOCK_SIZE";
721 case CX2341X_ENC_GET_PREV_DMA_INFO_MB_10
:
722 return "GET_PREV_DMA_INFO_MB_10";
723 case CX2341X_ENC_GET_PREV_DMA_INFO_MB_9
:
724 return "GET_PREV_DMA_INFO_MB_9";
725 case CX2341X_ENC_SCHED_DMA_TO_HOST
:
726 return "SCHED_DMA_TO_HOST";
727 case CX2341X_ENC_INITIALIZE_INPUT
:
728 return "INITIALIZE_INPUT";
729 case CX2341X_ENC_SET_FRAME_DROP_RATE
:
730 return "SET_FRAME_DROP_RATE";
731 case CX2341X_ENC_PAUSE_ENCODER
:
732 return "PAUSE_ENCODER";
733 case CX2341X_ENC_REFRESH_INPUT
:
734 return "REFRESH_INPUT";
735 case CX2341X_ENC_SET_COPYRIGHT
:
736 return "SET_COPYRIGHT";
737 case CX2341X_ENC_SET_EVENT_NOTIFICATION
:
738 return "SET_EVENT_NOTIFICATION";
739 case CX2341X_ENC_SET_NUM_VSYNC_LINES
:
740 return "SET_NUM_VSYNC_LINES";
741 case CX2341X_ENC_SET_PLACEHOLDER
:
742 return "SET_PLACEHOLDER";
743 case CX2341X_ENC_MUTE_VIDEO
:
745 case CX2341X_ENC_MUTE_AUDIO
:
747 case CX2341X_ENC_MISC
:
754 static int cx23885_mbox_func(void *priv
,
758 u32 data
[CX2341X_MBOX_MAX_DATA
])
760 struct cx23885_dev
*dev
= priv
;
761 unsigned long timeout
;
762 u32 value
, flag
, retval
= 0;
765 dprintk(3, "%s: command(0x%X) = %s\n", __func__
, command
,
766 cmd_to_str(command
));
768 /* this may not be 100% safe if we can't read any memory location
769 without side effects */
770 mc417_memory_read(dev
, dev
->cx23417_mailbox
- 4, &value
);
771 if (value
!= 0x12345678) {
772 pr_err("Firmware and/or mailbox pointer not initialized or corrupted, signature = 0x%x, cmd = %s\n",
773 value
, cmd_to_str(command
));
777 /* This read looks at 32 bits, but flag is only 8 bits.
778 * Seems we also bail if CMD or TIMEOUT bytes are set???
780 mc417_memory_read(dev
, dev
->cx23417_mailbox
, &flag
);
782 pr_err("ERROR: Mailbox appears to be in use (%x), cmd = %s\n",
783 flag
, cmd_to_str(command
));
787 flag
|= 1; /* tell 'em we're working on it */
788 mc417_memory_write(dev
, dev
->cx23417_mailbox
, flag
);
790 /* write command + args + fill remaining with zeros */
792 mc417_memory_write(dev
, dev
->cx23417_mailbox
+ 1, command
);
793 mc417_memory_write(dev
, dev
->cx23417_mailbox
+ 3,
794 IVTV_API_STD_TIMEOUT
); /* timeout */
795 for (i
= 0; i
< in
; i
++) {
796 mc417_memory_write(dev
, dev
->cx23417_mailbox
+ 4 + i
, data
[i
]);
797 dprintk(3, "API Input %d = %d\n", i
, data
[i
]);
799 for (; i
< CX2341X_MBOX_MAX_DATA
; i
++)
800 mc417_memory_write(dev
, dev
->cx23417_mailbox
+ 4 + i
, 0);
802 flag
|= 3; /* tell 'em we're done writing */
803 mc417_memory_write(dev
, dev
->cx23417_mailbox
, flag
);
805 /* wait for firmware to handle the API command */
806 timeout
= jiffies
+ msecs_to_jiffies(10);
808 mc417_memory_read(dev
, dev
->cx23417_mailbox
, &flag
);
811 if (time_after(jiffies
, timeout
)) {
812 pr_err("ERROR: API Mailbox timeout\n");
818 /* read output values */
819 for (i
= 0; i
< out
; i
++) {
820 mc417_memory_read(dev
, dev
->cx23417_mailbox
+ 4 + i
, data
+ i
);
821 dprintk(3, "API Output %d = %d\n", i
, data
[i
]);
824 mc417_memory_read(dev
, dev
->cx23417_mailbox
+ 2, &retval
);
825 dprintk(3, "API result = %d\n", retval
);
828 mc417_memory_write(dev
, dev
->cx23417_mailbox
, flag
);
833 /* We don't need to call the API often, so using just one
834 * mailbox will probably suffice
836 static int cx23885_api_cmd(struct cx23885_dev
*dev
,
842 u32 data
[CX2341X_MBOX_MAX_DATA
];
846 dprintk(3, "%s() cmds = 0x%08x\n", __func__
, command
);
848 va_start(vargs
, outputcnt
);
849 for (i
= 0; i
< inputcnt
; i
++)
850 data
[i
] = va_arg(vargs
, int);
852 err
= cx23885_mbox_func(dev
, command
, inputcnt
, outputcnt
, data
);
853 for (i
= 0; i
< outputcnt
; i
++) {
854 int *vptr
= va_arg(vargs
, int *);
862 static int cx23885_api_func(void *priv
, u32 cmd
, int in
, int out
, u32 data
[CX2341X_MBOX_MAX_DATA
])
864 return cx23885_mbox_func(priv
, cmd
, in
, out
, data
);
867 static int cx23885_find_mailbox(struct cx23885_dev
*dev
)
870 0x12345678, 0x34567812, 0x56781234, 0x78123456
872 int signaturecnt
= 0;
876 dprintk(2, "%s()\n", __func__
);
878 for (i
= 0; i
< CX23885_FIRM_IMAGE_SIZE
; i
++) {
879 mc417_memory_read(dev
, i
, &value
);
880 if (value
== signature
[signaturecnt
])
884 if (4 == signaturecnt
) {
885 dprintk(1, "Mailbox signature found at 0x%x\n", i
+1);
889 pr_err("Mailbox signature values not found!\n");
893 static int cx23885_load_firmware(struct cx23885_dev
*dev
)
895 static const unsigned char magic
[8] = {
896 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
898 const struct firmware
*firmware
;
906 dprintk(2, "%s()\n", __func__
);
908 /* Save GPIO settings before reset of APU */
909 retval
|= mc417_memory_read(dev
, 0x9020, &gpio_output
);
910 retval
|= mc417_memory_read(dev
, 0x900C, &gpio_value
);
912 retval
= mc417_register_write(dev
,
913 IVTV_REG_VPU
, 0xFFFFFFED);
914 retval
|= mc417_register_write(dev
,
915 IVTV_REG_HW_BLOCKS
, IVTV_CMD_HW_BLOCKS_RST
);
916 retval
|= mc417_register_write(dev
,
917 IVTV_REG_ENC_SDRAM_REFRESH
, 0x80000800);
918 retval
|= mc417_register_write(dev
,
919 IVTV_REG_ENC_SDRAM_PRECHARGE
, 0x1A);
920 retval
|= mc417_register_write(dev
,
924 pr_err("%s: Error with mc417_register_write\n",
929 retval
= request_firmware(&firmware
, CX23885_FIRM_IMAGE_NAME
,
933 pr_err("ERROR: Hotplug firmware request failed (%s).\n",
934 CX23885_FIRM_IMAGE_NAME
);
935 pr_err("Please fix your hotplug setup, the board will not work without firmware loaded!\n");
939 if (firmware
->size
!= CX23885_FIRM_IMAGE_SIZE
) {
940 pr_err("ERROR: Firmware size mismatch (have %zu, expected %d)\n",
941 firmware
->size
, CX23885_FIRM_IMAGE_SIZE
);
942 release_firmware(firmware
);
946 if (0 != memcmp(firmware
->data
, magic
, 8)) {
947 pr_err("ERROR: Firmware magic mismatch, wrong file?\n");
948 release_firmware(firmware
);
952 /* transfer to the chip */
953 dprintk(2, "Loading firmware ...\n");
954 dataptr
= (u32
*)firmware
->data
;
955 for (i
= 0; i
< (firmware
->size
>> 2); i
++) {
958 if (mc417_memory_write(dev
, i
, value
) != 0) {
959 pr_err("ERROR: Loading firmware failed!\n");
960 release_firmware(firmware
);
966 /* read back to verify with the checksum */
967 dprintk(1, "Verifying firmware ...\n");
968 for (i
--; i
>= 0; i
--) {
969 if (mc417_memory_read(dev
, i
, &value
) != 0) {
970 pr_err("ERROR: Reading firmware failed!\n");
971 release_firmware(firmware
);
977 pr_err("ERROR: Firmware load failed (checksum mismatch).\n");
978 release_firmware(firmware
);
981 release_firmware(firmware
);
982 dprintk(1, "Firmware upload successful.\n");
984 retval
|= mc417_register_write(dev
, IVTV_REG_HW_BLOCKS
,
985 IVTV_CMD_HW_BLOCKS_RST
);
987 /* F/W power up disturbs the GPIOs, restore state */
988 retval
|= mc417_register_write(dev
, 0x9020, gpio_output
);
989 retval
|= mc417_register_write(dev
, 0x900C, gpio_value
);
991 retval
|= mc417_register_read(dev
, IVTV_REG_VPU
, &value
);
992 retval
|= mc417_register_write(dev
, IVTV_REG_VPU
, value
& 0xFFFFFFE8);
994 /* Hardcoded GPIO's here */
995 retval
|= mc417_register_write(dev
, 0x9020, 0x4000);
996 retval
|= mc417_register_write(dev
, 0x900C, 0x4000);
998 mc417_register_read(dev
, 0x9020, &gpio_output
);
999 mc417_register_read(dev
, 0x900C, &gpio_value
);
1002 pr_err("%s: Error with mc417_register_write\n",
1007 void cx23885_417_check_encoder(struct cx23885_dev
*dev
)
1012 cx23885_api_cmd(dev
, CX2341X_ENC_GET_SEQ_END
, 0, 2, &status
, &seq
);
1013 dprintk(1, "%s() status = %d, seq = %d\n", __func__
, status
, seq
);
1016 static void cx23885_codec_settings(struct cx23885_dev
*dev
)
1018 dprintk(1, "%s()\n", __func__
);
1020 /* Dynamically change the height based on video standard */
1021 if (dev
->encodernorm
.id
& V4L2_STD_525_60
)
1022 dev
->ts1
.height
= 480;
1024 dev
->ts1
.height
= 576;
1026 /* assign frame size */
1027 cx23885_api_cmd(dev
, CX2341X_ENC_SET_FRAME_SIZE
, 2, 0,
1028 dev
->ts1
.height
, dev
->ts1
.width
);
1030 dev
->cxhdl
.width
= dev
->ts1
.width
;
1031 dev
->cxhdl
.height
= dev
->ts1
.height
;
1032 dev
->cxhdl
.is_50hz
=
1033 (dev
->encodernorm
.id
& V4L2_STD_625_50
) != 0;
1035 cx2341x_handler_setup(&dev
->cxhdl
);
1037 cx23885_api_cmd(dev
, CX2341X_ENC_MISC
, 2, 0, 3, 1);
1038 cx23885_api_cmd(dev
, CX2341X_ENC_MISC
, 2, 0, 4, 1);
1041 static int cx23885_initialize_codec(struct cx23885_dev
*dev
, int startencoder
)
1047 dprintk(1, "%s()\n", __func__
);
1049 retval
= cx23885_api_cmd(dev
, CX2341X_ENC_PING_FW
, 0, 0); /* ping */
1051 dprintk(2, "%s() PING OK\n", __func__
);
1052 retval
= cx23885_load_firmware(dev
);
1054 pr_err("%s() f/w load failed\n", __func__
);
1057 retval
= cx23885_find_mailbox(dev
);
1059 pr_err("%s() mailbox < 0, error\n",
1063 dev
->cx23417_mailbox
= retval
;
1064 retval
= cx23885_api_cmd(dev
, CX2341X_ENC_PING_FW
, 0, 0);
1066 pr_err("ERROR: cx23417 firmware ping failed!\n");
1069 retval
= cx23885_api_cmd(dev
, CX2341X_ENC_GET_VERSION
, 0, 1,
1072 pr_err("ERROR: cx23417 firmware get encoder :version failed!\n");
1075 dprintk(1, "cx23417 firmware version is 0x%08x\n", version
);
1079 cx23885_codec_settings(dev
);
1082 cx23885_api_cmd(dev
, CX2341X_ENC_SET_NUM_VSYNC_LINES
, 2, 0,
1083 CX23885_FIELD1_SAA7115
, CX23885_FIELD2_SAA7115
);
1084 cx23885_api_cmd(dev
, CX2341X_ENC_SET_PLACEHOLDER
, 12, 0,
1085 CX23885_CUSTOM_EXTENSION_USR_DATA
, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1088 /* Setup to capture VBI */
1089 data
[0] = 0x0001BD00;
1090 data
[1] = 1; /* frames per interrupt */
1091 data
[2] = 4; /* total bufs */
1092 data
[3] = 0x91559155; /* start codes */
1093 data
[4] = 0x206080C0; /* stop codes */
1094 data
[5] = 6; /* lines */
1095 data
[6] = 64; /* BPL */
1097 cx23885_api_cmd(dev
, CX2341X_ENC_SET_VBI_CONFIG
, 7, 0, data
[0], data
[1],
1098 data
[2], data
[3], data
[4], data
[5], data
[6]);
1100 for (i
= 2; i
<= 24; i
++) {
1103 valid
= ((i
>= 19) && (i
<= 21));
1104 cx23885_api_cmd(dev
, CX2341X_ENC_SET_VBI_LINE
, 5, 0, i
,
1106 cx23885_api_cmd(dev
, CX2341X_ENC_SET_VBI_LINE
, 5, 0,
1107 i
| 0x80000000, valid
, 0, 0, 0);
1110 cx23885_api_cmd(dev
, CX2341X_ENC_MUTE_AUDIO
, 1, 0, CX23885_UNMUTE
);
1113 /* initialize the video input */
1114 cx23885_api_cmd(dev
, CX2341X_ENC_INITIALIZE_INPUT
, 0, 0);
1117 /* Enable VIP style pixel invalidation so we work with scaled mode */
1118 mc417_memory_write(dev
, 2120, 0x00000080);
1120 /* start capturing to the host interface */
1122 cx23885_api_cmd(dev
, CX2341X_ENC_START_CAPTURE
, 2, 0,
1123 CX23885_MPEG_CAPTURE
, CX23885_RAW_BITS_NONE
);
1130 /* ------------------------------------------------------------------ */
1132 static int queue_setup(struct vb2_queue
*q
,
1133 unsigned int *num_buffers
, unsigned int *num_planes
,
1134 unsigned int sizes
[], struct device
*alloc_devs
[])
1136 struct cx23885_dev
*dev
= q
->drv_priv
;
1138 dev
->ts1
.ts_packet_size
= mpeglinesize
;
1139 dev
->ts1
.ts_packet_count
= mpeglines
;
1141 sizes
[0] = mpeglinesize
* mpeglines
;
1142 *num_buffers
= mpegbufs
;
1146 static int buffer_prepare(struct vb2_buffer
*vb
)
1148 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1149 struct cx23885_dev
*dev
= vb
->vb2_queue
->drv_priv
;
1150 struct cx23885_buffer
*buf
=
1151 container_of(vbuf
, struct cx23885_buffer
, vb
);
1153 return cx23885_buf_prepare(buf
, &dev
->ts1
);
1156 static void buffer_finish(struct vb2_buffer
*vb
)
1158 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1159 struct cx23885_dev
*dev
= vb
->vb2_queue
->drv_priv
;
1160 struct cx23885_buffer
*buf
= container_of(vbuf
,
1161 struct cx23885_buffer
, vb
);
1163 cx23885_free_buffer(dev
, buf
);
1166 static void buffer_queue(struct vb2_buffer
*vb
)
1168 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1169 struct cx23885_dev
*dev
= vb
->vb2_queue
->drv_priv
;
1170 struct cx23885_buffer
*buf
= container_of(vbuf
,
1171 struct cx23885_buffer
, vb
);
1173 cx23885_buf_queue(&dev
->ts1
, buf
);
1176 static int cx23885_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1178 struct cx23885_dev
*dev
= q
->drv_priv
;
1179 struct cx23885_dmaqueue
*dmaq
= &dev
->ts1
.mpegq
;
1180 unsigned long flags
;
1183 ret
= cx23885_initialize_codec(dev
, 1);
1185 struct cx23885_buffer
*buf
= list_entry(dmaq
->active
.next
,
1186 struct cx23885_buffer
, queue
);
1188 cx23885_start_dma(&dev
->ts1
, dmaq
, buf
);
1191 spin_lock_irqsave(&dev
->slock
, flags
);
1192 while (!list_empty(&dmaq
->active
)) {
1193 struct cx23885_buffer
*buf
= list_entry(dmaq
->active
.next
,
1194 struct cx23885_buffer
, queue
);
1196 list_del(&buf
->queue
);
1197 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_QUEUED
);
1199 spin_unlock_irqrestore(&dev
->slock
, flags
);
1203 static void cx23885_stop_streaming(struct vb2_queue
*q
)
1205 struct cx23885_dev
*dev
= q
->drv_priv
;
1207 /* stop mpeg capture */
1208 cx23885_api_cmd(dev
, CX2341X_ENC_STOP_CAPTURE
, 3, 0,
1209 CX23885_END_NOW
, CX23885_MPEG_CAPTURE
,
1210 CX23885_RAW_BITS_NONE
);
1213 cx23885_417_check_encoder(dev
);
1214 cx23885_cancel_buffers(&dev
->ts1
);
1217 static const struct vb2_ops cx23885_qops
= {
1218 .queue_setup
= queue_setup
,
1219 .buf_prepare
= buffer_prepare
,
1220 .buf_finish
= buffer_finish
,
1221 .buf_queue
= buffer_queue
,
1222 .wait_prepare
= vb2_ops_wait_prepare
,
1223 .wait_finish
= vb2_ops_wait_finish
,
1224 .start_streaming
= cx23885_start_streaming
,
1225 .stop_streaming
= cx23885_stop_streaming
,
1228 /* ------------------------------------------------------------------ */
1230 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1232 struct cx23885_dev
*dev
= video_drvdata(file
);
1238 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1240 struct cx23885_dev
*dev
= video_drvdata(file
);
1244 for (i
= 0; i
< ARRAY_SIZE(cx23885_tvnorms
); i
++)
1245 if (id
& cx23885_tvnorms
[i
].id
)
1247 if (i
== ARRAY_SIZE(cx23885_tvnorms
))
1250 ret
= cx23885_set_tvnorm(dev
, id
);
1252 dev
->encodernorm
= cx23885_tvnorms
[i
];
1256 static int vidioc_enum_input(struct file
*file
, void *priv
,
1257 struct v4l2_input
*i
)
1259 struct cx23885_dev
*dev
= video_drvdata(file
);
1260 dprintk(1, "%s()\n", __func__
);
1261 return cx23885_enum_input(dev
, i
);
1264 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1266 return cx23885_get_input(file
, priv
, i
);
1269 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1271 return cx23885_set_input(file
, priv
, i
);
1274 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1275 struct v4l2_tuner
*t
)
1277 struct cx23885_dev
*dev
= video_drvdata(file
);
1279 if (dev
->tuner_type
== TUNER_ABSENT
)
1283 strcpy(t
->name
, "Television");
1284 call_all(dev
, tuner
, g_tuner
, t
);
1286 dprintk(1, "VIDIOC_G_TUNER: tuner type %d\n", t
->type
);
1291 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1292 const struct v4l2_tuner
*t
)
1294 struct cx23885_dev
*dev
= video_drvdata(file
);
1296 if (dev
->tuner_type
== TUNER_ABSENT
)
1299 /* Update the A/V core */
1300 call_all(dev
, tuner
, s_tuner
, t
);
1305 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1306 struct v4l2_frequency
*f
)
1308 struct cx23885_dev
*dev
= video_drvdata(file
);
1310 if (dev
->tuner_type
== TUNER_ABSENT
)
1312 f
->type
= V4L2_TUNER_ANALOG_TV
;
1313 f
->frequency
= dev
->freq
;
1315 call_all(dev
, tuner
, g_frequency
, f
);
1320 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1321 const struct v4l2_frequency
*f
)
1323 return cx23885_set_frequency(file
, priv
, f
);
1326 static int vidioc_querycap(struct file
*file
, void *priv
,
1327 struct v4l2_capability
*cap
)
1329 struct cx23885_dev
*dev
= video_drvdata(file
);
1330 struct cx23885_tsport
*tsport
= &dev
->ts1
;
1332 strlcpy(cap
->driver
, dev
->name
, sizeof(cap
->driver
));
1333 strlcpy(cap
->card
, cx23885_boards
[tsport
->dev
->board
].name
,
1335 sprintf(cap
->bus_info
, "PCIe:%s", pci_name(dev
->pci
));
1336 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
1338 if (dev
->tuner_type
!= TUNER_ABSENT
)
1339 cap
->device_caps
|= V4L2_CAP_TUNER
;
1340 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_VBI_CAPTURE
|
1341 V4L2_CAP_AUDIO
| V4L2_CAP_DEVICE_CAPS
;
1346 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1347 struct v4l2_fmtdesc
*f
)
1352 strlcpy(f
->description
, "MPEG", sizeof(f
->description
));
1353 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
1358 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1359 struct v4l2_format
*f
)
1361 struct cx23885_dev
*dev
= video_drvdata(file
);
1363 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1364 f
->fmt
.pix
.bytesperline
= 0;
1365 f
->fmt
.pix
.sizeimage
=
1366 dev
->ts1
.ts_packet_size
* dev
->ts1
.ts_packet_count
;
1367 f
->fmt
.pix
.colorspace
= 0;
1368 f
->fmt
.pix
.width
= dev
->ts1
.width
;
1369 f
->fmt
.pix
.height
= dev
->ts1
.height
;
1370 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1371 dprintk(1, "VIDIOC_G_FMT: w: %d, h: %d\n",
1372 dev
->ts1
.width
, dev
->ts1
.height
);
1376 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1377 struct v4l2_format
*f
)
1379 struct cx23885_dev
*dev
= video_drvdata(file
);
1381 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1382 f
->fmt
.pix
.bytesperline
= 0;
1383 f
->fmt
.pix
.sizeimage
=
1384 dev
->ts1
.ts_packet_size
* dev
->ts1
.ts_packet_count
;
1385 f
->fmt
.pix
.colorspace
= 0;
1386 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1387 dprintk(1, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
1388 dev
->ts1
.width
, dev
->ts1
.height
);
1392 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1393 struct v4l2_format
*f
)
1395 struct cx23885_dev
*dev
= video_drvdata(file
);
1397 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1398 f
->fmt
.pix
.bytesperline
= 0;
1399 f
->fmt
.pix
.sizeimage
=
1400 dev
->ts1
.ts_packet_size
* dev
->ts1
.ts_packet_count
;
1401 f
->fmt
.pix
.colorspace
= 0;
1402 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1403 dprintk(1, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
1404 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
, f
->fmt
.pix
.field
);
1408 static int vidioc_log_status(struct file
*file
, void *priv
)
1410 struct cx23885_dev
*dev
= video_drvdata(file
);
1413 snprintf(name
, sizeof(name
), "%s/2", dev
->name
);
1414 call_all(dev
, core
, log_status
);
1415 v4l2_ctrl_handler_log_status(&dev
->cxhdl
.hdl
, name
);
1419 static const struct v4l2_file_operations mpeg_fops
= {
1420 .owner
= THIS_MODULE
,
1421 .open
= v4l2_fh_open
,
1422 .release
= vb2_fop_release
,
1423 .read
= vb2_fop_read
,
1424 .poll
= vb2_fop_poll
,
1425 .unlocked_ioctl
= video_ioctl2
,
1426 .mmap
= vb2_fop_mmap
,
1429 static const struct v4l2_ioctl_ops mpeg_ioctl_ops
= {
1430 .vidioc_g_std
= vidioc_g_std
,
1431 .vidioc_s_std
= vidioc_s_std
,
1432 .vidioc_enum_input
= vidioc_enum_input
,
1433 .vidioc_g_input
= vidioc_g_input
,
1434 .vidioc_s_input
= vidioc_s_input
,
1435 .vidioc_g_tuner
= vidioc_g_tuner
,
1436 .vidioc_s_tuner
= vidioc_s_tuner
,
1437 .vidioc_g_frequency
= vidioc_g_frequency
,
1438 .vidioc_s_frequency
= vidioc_s_frequency
,
1439 .vidioc_querycap
= vidioc_querycap
,
1440 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1441 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1442 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1443 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1444 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1445 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1446 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1447 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1448 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1449 .vidioc_streamon
= vb2_ioctl_streamon
,
1450 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1451 .vidioc_log_status
= vidioc_log_status
,
1452 #ifdef CONFIG_VIDEO_ADV_DEBUG
1453 .vidioc_g_chip_info
= cx23885_g_chip_info
,
1454 .vidioc_g_register
= cx23885_g_register
,
1455 .vidioc_s_register
= cx23885_s_register
,
1459 static struct video_device cx23885_mpeg_template
= {
1462 .ioctl_ops
= &mpeg_ioctl_ops
,
1463 .tvnorms
= CX23885_NORMS
,
1466 void cx23885_417_unregister(struct cx23885_dev
*dev
)
1468 dprintk(1, "%s()\n", __func__
);
1470 if (dev
->v4l_device
) {
1471 if (video_is_registered(dev
->v4l_device
))
1472 video_unregister_device(dev
->v4l_device
);
1474 video_device_release(dev
->v4l_device
);
1475 v4l2_ctrl_handler_free(&dev
->cxhdl
.hdl
);
1476 dev
->v4l_device
= NULL
;
1480 static struct video_device
*cx23885_video_dev_alloc(
1481 struct cx23885_tsport
*tsport
,
1482 struct pci_dev
*pci
,
1483 struct video_device
*template,
1486 struct video_device
*vfd
;
1487 struct cx23885_dev
*dev
= tsport
->dev
;
1489 dprintk(1, "%s()\n", __func__
);
1491 vfd
= video_device_alloc();
1495 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s (%s)",
1496 cx23885_boards
[tsport
->dev
->board
].name
, type
);
1497 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1498 vfd
->release
= video_device_release
;
1502 int cx23885_417_register(struct cx23885_dev
*dev
)
1504 /* FIXME: Port1 hardcoded here */
1506 struct cx23885_tsport
*tsport
= &dev
->ts1
;
1507 struct vb2_queue
*q
;
1509 dprintk(1, "%s()\n", __func__
);
1511 if (cx23885_boards
[dev
->board
].portb
!= CX23885_MPEG_ENCODER
)
1514 /* Set default TV standard */
1515 dev
->encodernorm
= cx23885_tvnorms
[0];
1517 if (dev
->encodernorm
.id
& V4L2_STD_525_60
)
1518 tsport
->height
= 480;
1520 tsport
->height
= 576;
1522 tsport
->width
= 720;
1523 dev
->cxhdl
.port
= CX2341X_PORT_SERIAL
;
1524 err
= cx2341x_handler_init(&dev
->cxhdl
, 50);
1527 dev
->cxhdl
.priv
= dev
;
1528 dev
->cxhdl
.func
= cx23885_api_func
;
1529 cx2341x_handler_set_50hz(&dev
->cxhdl
, tsport
->height
== 576);
1530 v4l2_ctrl_add_handler(&dev
->ctrl_handler
, &dev
->cxhdl
.hdl
, NULL
);
1532 /* Allocate and initialize V4L video device */
1533 dev
->v4l_device
= cx23885_video_dev_alloc(tsport
,
1534 dev
->pci
, &cx23885_mpeg_template
, "mpeg");
1535 q
= &dev
->vb2_mpegq
;
1536 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1537 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
;
1538 q
->gfp_flags
= GFP_DMA32
;
1539 q
->min_buffers_needed
= 2;
1541 q
->buf_struct_size
= sizeof(struct cx23885_buffer
);
1542 q
->ops
= &cx23885_qops
;
1543 q
->mem_ops
= &vb2_dma_sg_memops
;
1544 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1545 q
->lock
= &dev
->lock
;
1546 q
->dev
= &dev
->pci
->dev
;
1548 err
= vb2_queue_init(q
);
1551 video_set_drvdata(dev
->v4l_device
, dev
);
1552 dev
->v4l_device
->lock
= &dev
->lock
;
1553 dev
->v4l_device
->queue
= q
;
1554 err
= video_register_device(dev
->v4l_device
,
1555 VFL_TYPE_GRABBER
, -1);
1557 pr_info("%s: can't register mpeg device\n", dev
->name
);
1561 pr_info("%s: registered device %s [mpeg]\n",
1562 dev
->name
, video_device_node_name(dev
->v4l_device
));
1564 /* ST: Configure the encoder paramaters, but don't begin
1565 * encoding, this resolves an issue where the first time the
1566 * encoder is started video can be choppy.
1568 cx23885_initialize_codec(dev
, 0);
1573 MODULE_FIRMWARE(CX23885_FIRM_IMAGE_NAME
);