1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Support for a cx23417 mpeg encoder via cx231xx host port.
6 * (c) 2004 Jelle Foks <jelle@foks.us>
7 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
8 * (c) 2008 Steven Toth <stoth@linuxtv.org>
9 * - CX23885/7/8 support
11 * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/firmware.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-event.h>
28 #include <media/drv-intf/cx2341x.h>
29 #include <media/tuner.h>
31 #define CX231xx_FIRM_IMAGE_SIZE 376836
32 #define CX231xx_FIRM_IMAGE_NAME "v4l-cx23885-enc.fw"
34 /* for polaris ITVC */
35 #define ITVC_WRITE_DIR 0x03FDFC00
36 #define ITVC_READ_DIR 0x0001FC00
38 #define MCI_MEMORY_DATA_BYTE0 0x00
39 #define MCI_MEMORY_DATA_BYTE1 0x08
40 #define MCI_MEMORY_DATA_BYTE2 0x10
41 #define MCI_MEMORY_DATA_BYTE3 0x18
43 #define MCI_MEMORY_ADDRESS_BYTE2 0x20
44 #define MCI_MEMORY_ADDRESS_BYTE1 0x28
45 #define MCI_MEMORY_ADDRESS_BYTE0 0x30
47 #define MCI_REGISTER_DATA_BYTE0 0x40
48 #define MCI_REGISTER_DATA_BYTE1 0x48
49 #define MCI_REGISTER_DATA_BYTE2 0x50
50 #define MCI_REGISTER_DATA_BYTE3 0x58
52 #define MCI_REGISTER_ADDRESS_BYTE0 0x60
53 #define MCI_REGISTER_ADDRESS_BYTE1 0x68
55 #define MCI_REGISTER_MODE 0x70
57 /* Read and write modes for polaris ITVC */
58 #define MCI_MODE_REGISTER_READ 0x000
59 #define MCI_MODE_REGISTER_WRITE 0x100
60 #define MCI_MODE_MEMORY_READ 0x000
61 #define MCI_MODE_MEMORY_WRITE 0x4000
63 static unsigned int mpeglines
= 128;
64 module_param(mpeglines
, int, 0644);
65 MODULE_PARM_DESC(mpeglines
, "number of lines in an MPEG buffer, range 2-32");
67 static unsigned int mpeglinesize
= 512;
68 module_param(mpeglinesize
, int, 0644);
69 MODULE_PARM_DESC(mpeglinesize
,
70 "number of bytes in each line of an MPEG buffer, range 512-1024");
72 static unsigned int v4l_debug
= 1;
73 module_param(v4l_debug
, int, 0644);
74 MODULE_PARM_DESC(v4l_debug
, "enable V4L debug messages");
76 #define dprintk(level, fmt, arg...) \
78 if (v4l_debug >= level) \
79 printk(KERN_DEBUG pr_fmt(fmt), ## arg); \
82 static struct cx231xx_tvnorm cx231xx_tvnorms
[] = {
85 .id
= V4L2_STD_NTSC_M
,
88 .id
= V4L2_STD_NTSC_M_JP
,
91 .id
= V4L2_STD_PAL_BG
,
94 .id
= V4L2_STD_PAL_DK
,
100 .id
= V4L2_STD_PAL_M
,
103 .id
= V4L2_STD_PAL_N
,
106 .id
= V4L2_STD_PAL_Nc
,
109 .id
= V4L2_STD_PAL_60
,
112 .id
= V4L2_STD_SECAM_L
,
115 .id
= V4L2_STD_SECAM_DK
,
119 /* ------------------------------------------------------------------ */
121 enum cx231xx_capture_type
{
122 CX231xx_MPEG_CAPTURE
,
124 CX231xx_RAW_PASSTHRU_CAPTURE
127 enum cx231xx_capture_bits
{
128 CX231xx_RAW_BITS_NONE
= 0x00,
129 CX231xx_RAW_BITS_YUV_CAPTURE
= 0x01,
130 CX231xx_RAW_BITS_PCM_CAPTURE
= 0x02,
131 CX231xx_RAW_BITS_VBI_CAPTURE
= 0x04,
132 CX231xx_RAW_BITS_PASSTHRU_CAPTURE
= 0x08,
133 CX231xx_RAW_BITS_TO_HOST_CAPTURE
= 0x10
136 enum cx231xx_capture_end
{
137 CX231xx_END_AT_GOP
, /* stop at the end of gop, generate irq */
138 CX231xx_END_NOW
, /* stop immediately, no irq */
141 enum cx231xx_framerate
{
142 CX231xx_FRAMERATE_NTSC_30
, /* NTSC: 30fps */
143 CX231xx_FRAMERATE_PAL_25
/* PAL: 25fps */
146 enum cx231xx_stream_port
{
147 CX231xx_OUTPUT_PORT_MEMORY
,
148 CX231xx_OUTPUT_PORT_STREAMING
,
149 CX231xx_OUTPUT_PORT_SERIAL
152 enum cx231xx_data_xfer_status
{
153 CX231xx_MORE_BUFFERS_FOLLOW
,
157 enum cx231xx_picture_mask
{
158 CX231xx_PICTURE_MASK_NONE
,
159 CX231xx_PICTURE_MASK_I_FRAMES
,
160 CX231xx_PICTURE_MASK_I_P_FRAMES
= 0x3,
161 CX231xx_PICTURE_MASK_ALL_FRAMES
= 0x7,
164 enum cx231xx_vbi_mode_bits
{
165 CX231xx_VBI_BITS_SLICED
,
166 CX231xx_VBI_BITS_RAW
,
169 enum cx231xx_vbi_insertion_bits
{
170 CX231xx_VBI_BITS_INSERT_IN_XTENSION_USR_DATA
,
171 CX231xx_VBI_BITS_INSERT_IN_PRIVATE_PACKETS
= 0x1 << 1,
172 CX231xx_VBI_BITS_SEPARATE_STREAM
= 0x2 << 1,
173 CX231xx_VBI_BITS_SEPARATE_STREAM_USR_DATA
= 0x4 << 1,
174 CX231xx_VBI_BITS_SEPARATE_STREAM_PRV_DATA
= 0x5 << 1,
177 enum cx231xx_dma_unit
{
182 enum cx231xx_dma_transfer_status_bits
{
183 CX231xx_DMA_TRANSFER_BITS_DONE
= 0x01,
184 CX231xx_DMA_TRANSFER_BITS_ERROR
= 0x04,
185 CX231xx_DMA_TRANSFER_BITS_LL_ERROR
= 0x10,
189 CX231xx_PAUSE_ENCODING
,
190 CX231xx_RESUME_ENCODING
,
193 enum cx231xx_copyright
{
194 CX231xx_COPYRIGHT_OFF
,
195 CX231xx_COPYRIGHT_ON
,
198 enum cx231xx_notification_type
{
199 CX231xx_NOTIFICATION_REFRESH
,
202 enum cx231xx_notification_status
{
203 CX231xx_NOTIFICATION_OFF
,
204 CX231xx_NOTIFICATION_ON
,
207 enum cx231xx_notification_mailbox
{
208 CX231xx_NOTIFICATION_NO_MAILBOX
= -1,
211 enum cx231xx_field1_lines
{
212 CX231xx_FIELD1_SAA7114
= 0x00EF, /* 239 */
213 CX231xx_FIELD1_SAA7115
= 0x00F0, /* 240 */
214 CX231xx_FIELD1_MICRONAS
= 0x0105, /* 261 */
217 enum cx231xx_field2_lines
{
218 CX231xx_FIELD2_SAA7114
= 0x00EF, /* 239 */
219 CX231xx_FIELD2_SAA7115
= 0x00F0, /* 240 */
220 CX231xx_FIELD2_MICRONAS
= 0x0106, /* 262 */
223 enum cx231xx_custom_data_type
{
224 CX231xx_CUSTOM_EXTENSION_USR_DATA
,
225 CX231xx_CUSTOM_PRIVATE_PACKET
,
233 enum cx231xx_mute_video_mask
{
234 CX231xx_MUTE_VIDEO_V_MASK
= 0x0000FF00,
235 CX231xx_MUTE_VIDEO_U_MASK
= 0x00FF0000,
236 CX231xx_MUTE_VIDEO_Y_MASK
= 0xFF000000,
239 enum cx231xx_mute_video_shift
{
240 CX231xx_MUTE_VIDEO_V_SHIFT
= 8,
241 CX231xx_MUTE_VIDEO_U_SHIFT
= 16,
242 CX231xx_MUTE_VIDEO_Y_SHIFT
= 24,
245 /* defines below are from ivtv-driver.h */
246 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
248 /* Firmware API commands */
249 #define IVTV_API_STD_TIMEOUT 500
252 /* IVTV_REG_OFFSET */
253 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
254 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
255 #define IVTV_REG_SPU (0x9050)
256 #define IVTV_REG_HW_BLOCKS (0x9054)
257 #define IVTV_REG_VPU (0x9058)
258 #define IVTV_REG_APU (0xA064)
261 * Bit definitions for MC417_RWD and MC417_OEN registers
267 *| bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
268 *|+-------+-------+-------+-------+-------+-------+-------+-------+
269 *|| MIWR# | MIRD# | MICS# |MIRDY# |MIADDR3|MIADDR2|MIADDR1|MIADDR0|
270 *|+-------+-------+-------+-------+-------+-------+-------+-------+
271 *| bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
272 *|+-------+-------+-------+-------+-------+-------+-------+-------+
273 *||MIDATA7|MIDATA6|MIDATA5|MIDATA4|MIDATA3|MIDATA2|MIDATA1|MIDATA0|
274 *|+-------+-------+-------+-------+-------+-------+-------+-------+
276 #define MC417_MIWR 0x8000
277 #define MC417_MIRD 0x4000
278 #define MC417_MICS 0x2000
279 #define MC417_MIRDY 0x1000
280 #define MC417_MIADDR 0x0F00
281 #define MC417_MIDATA 0x00FF
284 /* Bit definitions for MC417_CTL register ****
285 *bits 31-6 bits 5-4 bit 3 bits 2-1 Bit 0
286 *+--------+-------------+--------+--------------+------------+
287 *|Reserved|MC417_SPD_CTL|Reserved|MC417_GPIO_SEL|UART_GPIO_EN|
288 *+--------+-------------+--------+--------------+------------+
290 #define MC417_SPD_CTL(x) (((x) << 4) & 0x00000030)
291 #define MC417_GPIO_SEL(x) (((x) << 1) & 0x00000006)
292 #define MC417_UART_GPIO_EN 0x00000001
294 /* Values for speed control */
295 #define MC417_SPD_CTL_SLOW 0x1
296 #define MC417_SPD_CTL_MEDIUM 0x0
297 #define MC417_SPD_CTL_FAST 0x3 /* b'1x, but we use b'11 */
299 /* Values for GPIO select */
300 #define MC417_GPIO_SEL_GPIO3 0x3
301 #define MC417_GPIO_SEL_GPIO2 0x2
302 #define MC417_GPIO_SEL_GPIO1 0x1
303 #define MC417_GPIO_SEL_GPIO0 0x0
306 #define CX23417_GPIO_MASK 0xFC0003FF
308 static int set_itvc_reg(struct cx231xx
*dev
, u32 gpio_direction
, u32 value
)
311 u32 _gpio_direction
= 0;
313 _gpio_direction
= _gpio_direction
& CX23417_GPIO_MASK
;
314 _gpio_direction
= _gpio_direction
| gpio_direction
;
315 status
= cx231xx_send_gpio_cmd(dev
, _gpio_direction
,
316 (u8
*)&value
, 4, 0, 0);
320 static int get_itvc_reg(struct cx231xx
*dev
, u32 gpio_direction
, u32
*val_ptr
)
323 u32 _gpio_direction
= 0;
325 _gpio_direction
= _gpio_direction
& CX23417_GPIO_MASK
;
326 _gpio_direction
= _gpio_direction
| gpio_direction
;
328 status
= cx231xx_send_gpio_cmd(dev
, _gpio_direction
,
329 (u8
*)val_ptr
, 4, 0, 1);
333 static int wait_for_mci_complete(struct cx231xx
*dev
)
336 u32 gpio_direction
= 0;
338 get_itvc_reg(dev
, gpio_direction
, &gpio
);
340 while (!(gpio
&0x020000)) {
343 get_itvc_reg(dev
, gpio_direction
, &gpio
);
346 dprintk(3, "ERROR: Timeout - gpio=%x\n", gpio
);
353 static int mc417_register_write(struct cx231xx
*dev
, u16 address
, u32 value
)
358 temp
= 0x82 | MCI_REGISTER_DATA_BYTE0
| ((value
& 0x000000FF) << 8);
360 status
= set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
363 temp
= temp
| (0x05 << 10);
364 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
366 /*write data byte 1;*/
367 temp
= 0x82 | MCI_REGISTER_DATA_BYTE1
| (value
& 0x0000FF00);
369 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
370 temp
= temp
| (0x05 << 10);
371 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
373 /*write data byte 2;*/
374 temp
= 0x82 | MCI_REGISTER_DATA_BYTE2
| ((value
& 0x00FF0000) >> 8);
376 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
377 temp
= temp
| (0x05 << 10);
378 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
380 /*write data byte 3;*/
381 temp
= 0x82 | MCI_REGISTER_DATA_BYTE3
| ((value
& 0xFF000000) >> 16);
383 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
384 temp
= temp
| (0x05 << 10);
385 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
387 /*write address byte 0;*/
388 temp
= 0x82 | MCI_REGISTER_ADDRESS_BYTE0
| ((address
& 0x000000FF) << 8);
390 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
391 temp
= temp
| (0x05 << 10);
392 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
394 /*write address byte 1;*/
395 temp
= 0x82 | MCI_REGISTER_ADDRESS_BYTE1
| (address
& 0x0000FF00);
397 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
398 temp
= temp
| (0x05 << 10);
399 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
401 /*Write that the mode is write.*/
402 temp
= 0x82 | MCI_REGISTER_MODE
| MCI_MODE_REGISTER_WRITE
;
404 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
405 temp
= temp
| (0x05 << 10);
406 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
408 return wait_for_mci_complete(dev
);
411 static int mc417_register_read(struct cx231xx
*dev
, u16 address
, u32
*value
)
413 /*write address byte 0;*/
415 u32 return_value
= 0;
418 temp
= 0x82 | MCI_REGISTER_ADDRESS_BYTE0
| ((address
& 0x00FF) << 8);
420 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
421 temp
= temp
| ((0x05) << 10);
422 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
424 /*write address byte 1;*/
425 temp
= 0x82 | MCI_REGISTER_ADDRESS_BYTE1
| (address
& 0xFF00);
427 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
428 temp
= temp
| ((0x05) << 10);
429 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
431 /*write that the mode is read;*/
432 temp
= 0x82 | MCI_REGISTER_MODE
| MCI_MODE_REGISTER_READ
;
434 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
435 temp
= temp
| ((0x05) << 10);
436 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
438 /*wait for the MIRDY line to be asserted ,
439 signalling that the read is done;*/
440 ret
= wait_for_mci_complete(dev
);
442 /*switch the DATA- GPIO to input mode;*/
444 /*Read data byte 0;*/
445 temp
= (0x82 | MCI_REGISTER_DATA_BYTE0
) << 10;
446 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
447 temp
= ((0x81 | MCI_REGISTER_DATA_BYTE0
) << 10);
448 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
449 get_itvc_reg(dev
, ITVC_READ_DIR
, &temp
);
450 return_value
|= ((temp
& 0x03FC0000) >> 18);
451 set_itvc_reg(dev
, ITVC_READ_DIR
, (0x87 << 10));
453 /* Read data byte 1;*/
454 temp
= (0x82 | MCI_REGISTER_DATA_BYTE1
) << 10;
455 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
456 temp
= ((0x81 | MCI_REGISTER_DATA_BYTE1
) << 10);
457 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
458 get_itvc_reg(dev
, ITVC_READ_DIR
, &temp
);
460 return_value
|= ((temp
& 0x03FC0000) >> 10);
461 set_itvc_reg(dev
, ITVC_READ_DIR
, (0x87 << 10));
463 /*Read data byte 2;*/
464 temp
= (0x82 | MCI_REGISTER_DATA_BYTE2
) << 10;
465 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
466 temp
= ((0x81 | MCI_REGISTER_DATA_BYTE2
) << 10);
467 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
468 get_itvc_reg(dev
, ITVC_READ_DIR
, &temp
);
469 return_value
|= ((temp
& 0x03FC0000) >> 2);
470 set_itvc_reg(dev
, ITVC_READ_DIR
, (0x87 << 10));
472 /*Read data byte 3;*/
473 temp
= (0x82 | MCI_REGISTER_DATA_BYTE3
) << 10;
474 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
475 temp
= ((0x81 | MCI_REGISTER_DATA_BYTE3
) << 10);
476 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
477 get_itvc_reg(dev
, ITVC_READ_DIR
, &temp
);
478 return_value
|= ((temp
& 0x03FC0000) << 6);
479 set_itvc_reg(dev
, ITVC_READ_DIR
, (0x87 << 10));
481 *value
= return_value
;
485 static int mc417_memory_write(struct cx231xx
*dev
, u32 address
, u32 value
)
487 /*write data byte 0;*/
492 temp
= 0x82 | MCI_MEMORY_DATA_BYTE0
| ((value
& 0x000000FF) << 8);
494 ret
= set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
497 temp
= temp
| (0x05 << 10);
498 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
500 /*write data byte 1;*/
501 temp
= 0x82 | MCI_MEMORY_DATA_BYTE1
| (value
& 0x0000FF00);
503 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
504 temp
= temp
| (0x05 << 10);
505 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
507 /*write data byte 2;*/
508 temp
= 0x82 | MCI_MEMORY_DATA_BYTE2
| ((value
& 0x00FF0000) >> 8);
510 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
511 temp
= temp
| (0x05 << 10);
512 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
514 /*write data byte 3;*/
515 temp
= 0x82 | MCI_MEMORY_DATA_BYTE3
| ((value
& 0xFF000000) >> 16);
517 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
518 temp
= temp
| (0x05 << 10);
519 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
521 /* write address byte 2;*/
522 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE2
| MCI_MODE_MEMORY_WRITE
|
523 ((address
& 0x003F0000) >> 8);
525 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
526 temp
= temp
| (0x05 << 10);
527 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
529 /* write address byte 1;*/
530 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE1
| (address
& 0xFF00);
532 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
533 temp
= temp
| (0x05 << 10);
534 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
536 /* write address byte 0;*/
537 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE0
| ((address
& 0x00FF) << 8);
539 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
540 temp
= temp
| (0x05 << 10);
541 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
543 /*wait for MIRDY line;*/
544 wait_for_mci_complete(dev
);
549 static int mc417_memory_read(struct cx231xx
*dev
, u32 address
, u32
*value
)
552 u32 return_value
= 0;
555 /*write address byte 2;*/
556 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE2
| MCI_MODE_MEMORY_READ
|
557 ((address
& 0x003F0000) >> 8);
559 ret
= set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
562 temp
= temp
| (0x05 << 10);
563 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
565 /*write address byte 1*/
566 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE1
| (address
& 0xFF00);
568 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
569 temp
= temp
| (0x05 << 10);
570 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
572 /*write address byte 0*/
573 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE0
| ((address
& 0x00FF) << 8);
575 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
576 temp
= temp
| (0x05 << 10);
577 set_itvc_reg(dev
, ITVC_WRITE_DIR
, temp
);
579 /*Wait for MIRDY line*/
580 ret
= wait_for_mci_complete(dev
);
583 /*Read data byte 3;*/
584 temp
= (0x82 | MCI_MEMORY_DATA_BYTE3
) << 10;
585 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
586 temp
= ((0x81 | MCI_MEMORY_DATA_BYTE3
) << 10);
587 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
588 get_itvc_reg(dev
, ITVC_READ_DIR
, &temp
);
589 return_value
|= ((temp
& 0x03FC0000) << 6);
590 set_itvc_reg(dev
, ITVC_READ_DIR
, (0x87 << 10));
592 /*Read data byte 2;*/
593 temp
= (0x82 | MCI_MEMORY_DATA_BYTE2
) << 10;
594 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
595 temp
= ((0x81 | MCI_MEMORY_DATA_BYTE2
) << 10);
596 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
597 get_itvc_reg(dev
, ITVC_READ_DIR
, &temp
);
598 return_value
|= ((temp
& 0x03FC0000) >> 2);
599 set_itvc_reg(dev
, ITVC_READ_DIR
, (0x87 << 10));
601 /* Read data byte 1;*/
602 temp
= (0x82 | MCI_MEMORY_DATA_BYTE1
) << 10;
603 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
604 temp
= ((0x81 | MCI_MEMORY_DATA_BYTE1
) << 10);
605 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
606 get_itvc_reg(dev
, ITVC_READ_DIR
, &temp
);
607 return_value
|= ((temp
& 0x03FC0000) >> 10);
608 set_itvc_reg(dev
, ITVC_READ_DIR
, (0x87 << 10));
610 /*Read data byte 0;*/
611 temp
= (0x82 | MCI_MEMORY_DATA_BYTE0
) << 10;
612 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
613 temp
= ((0x81 | MCI_MEMORY_DATA_BYTE0
) << 10);
614 set_itvc_reg(dev
, ITVC_READ_DIR
, temp
);
615 get_itvc_reg(dev
, ITVC_READ_DIR
, &temp
);
616 return_value
|= ((temp
& 0x03FC0000) >> 18);
617 set_itvc_reg(dev
, ITVC_READ_DIR
, (0x87 << 10));
619 *value
= return_value
;
623 /* ------------------------------------------------------------------ */
625 /* MPEG encoder API */
626 static char *cmd_to_str(int cmd
)
629 case CX2341X_ENC_PING_FW
:
631 case CX2341X_ENC_START_CAPTURE
:
632 return "START_CAPTURE";
633 case CX2341X_ENC_STOP_CAPTURE
:
634 return "STOP_CAPTURE";
635 case CX2341X_ENC_SET_AUDIO_ID
:
636 return "SET_AUDIO_ID";
637 case CX2341X_ENC_SET_VIDEO_ID
:
638 return "SET_VIDEO_ID";
639 case CX2341X_ENC_SET_PCR_ID
:
640 return "SET_PCR_PID";
641 case CX2341X_ENC_SET_FRAME_RATE
:
642 return "SET_FRAME_RATE";
643 case CX2341X_ENC_SET_FRAME_SIZE
:
644 return "SET_FRAME_SIZE";
645 case CX2341X_ENC_SET_BIT_RATE
:
646 return "SET_BIT_RATE";
647 case CX2341X_ENC_SET_GOP_PROPERTIES
:
648 return "SET_GOP_PROPERTIES";
649 case CX2341X_ENC_SET_ASPECT_RATIO
:
650 return "SET_ASPECT_RATIO";
651 case CX2341X_ENC_SET_DNR_FILTER_MODE
:
652 return "SET_DNR_FILTER_PROPS";
653 case CX2341X_ENC_SET_DNR_FILTER_PROPS
:
654 return "SET_DNR_FILTER_PROPS";
655 case CX2341X_ENC_SET_CORING_LEVELS
:
656 return "SET_CORING_LEVELS";
657 case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE
:
658 return "SET_SPATIAL_FILTER_TYPE";
659 case CX2341X_ENC_SET_VBI_LINE
:
660 return "SET_VBI_LINE";
661 case CX2341X_ENC_SET_STREAM_TYPE
:
662 return "SET_STREAM_TYPE";
663 case CX2341X_ENC_SET_OUTPUT_PORT
:
664 return "SET_OUTPUT_PORT";
665 case CX2341X_ENC_SET_AUDIO_PROPERTIES
:
666 return "SET_AUDIO_PROPERTIES";
667 case CX2341X_ENC_HALT_FW
:
669 case CX2341X_ENC_GET_VERSION
:
670 return "GET_VERSION";
671 case CX2341X_ENC_SET_GOP_CLOSURE
:
672 return "SET_GOP_CLOSURE";
673 case CX2341X_ENC_GET_SEQ_END
:
674 return "GET_SEQ_END";
675 case CX2341X_ENC_SET_PGM_INDEX_INFO
:
676 return "SET_PGM_INDEX_INFO";
677 case CX2341X_ENC_SET_VBI_CONFIG
:
678 return "SET_VBI_CONFIG";
679 case CX2341X_ENC_SET_DMA_BLOCK_SIZE
:
680 return "SET_DMA_BLOCK_SIZE";
681 case CX2341X_ENC_GET_PREV_DMA_INFO_MB_10
:
682 return "GET_PREV_DMA_INFO_MB_10";
683 case CX2341X_ENC_GET_PREV_DMA_INFO_MB_9
:
684 return "GET_PREV_DMA_INFO_MB_9";
685 case CX2341X_ENC_SCHED_DMA_TO_HOST
:
686 return "SCHED_DMA_TO_HOST";
687 case CX2341X_ENC_INITIALIZE_INPUT
:
688 return "INITIALIZE_INPUT";
689 case CX2341X_ENC_SET_FRAME_DROP_RATE
:
690 return "SET_FRAME_DROP_RATE";
691 case CX2341X_ENC_PAUSE_ENCODER
:
692 return "PAUSE_ENCODER";
693 case CX2341X_ENC_REFRESH_INPUT
:
694 return "REFRESH_INPUT";
695 case CX2341X_ENC_SET_COPYRIGHT
:
696 return "SET_COPYRIGHT";
697 case CX2341X_ENC_SET_EVENT_NOTIFICATION
:
698 return "SET_EVENT_NOTIFICATION";
699 case CX2341X_ENC_SET_NUM_VSYNC_LINES
:
700 return "SET_NUM_VSYNC_LINES";
701 case CX2341X_ENC_SET_PLACEHOLDER
:
702 return "SET_PLACEHOLDER";
703 case CX2341X_ENC_MUTE_VIDEO
:
705 case CX2341X_ENC_MUTE_AUDIO
:
707 case CX2341X_ENC_MISC
:
714 static int cx231xx_mbox_func(void *priv
, u32 command
, int in
, int out
,
715 u32 data
[CX2341X_MBOX_MAX_DATA
])
717 struct cx231xx
*dev
= priv
;
718 unsigned long timeout
;
719 u32 value
, flag
, retval
= 0;
722 dprintk(3, "%s: command(0x%X) = %s\n", __func__
, command
,
723 cmd_to_str(command
));
725 /* this may not be 100% safe if we can't read any memory location
726 without side effects */
727 mc417_memory_read(dev
, dev
->cx23417_mailbox
- 4, &value
);
728 if (value
!= 0x12345678) {
729 dprintk(3, "Firmware and/or mailbox pointer not initialized or corrupted, signature = 0x%x, cmd = %s\n",
730 value
, cmd_to_str(command
));
734 /* This read looks at 32 bits, but flag is only 8 bits.
735 * Seems we also bail if CMD or TIMEOUT bytes are set???
737 mc417_memory_read(dev
, dev
->cx23417_mailbox
, &flag
);
739 dprintk(3, "ERROR: Mailbox appears to be in use (%x), cmd = %s\n",
740 flag
, cmd_to_str(command
));
744 flag
|= 1; /* tell 'em we're working on it */
745 mc417_memory_write(dev
, dev
->cx23417_mailbox
, flag
);
747 /* write command + args + fill remaining with zeros */
749 mc417_memory_write(dev
, dev
->cx23417_mailbox
+ 1, command
);
750 mc417_memory_write(dev
, dev
->cx23417_mailbox
+ 3,
751 IVTV_API_STD_TIMEOUT
); /* timeout */
752 for (i
= 0; i
< in
; i
++) {
753 mc417_memory_write(dev
, dev
->cx23417_mailbox
+ 4 + i
, data
[i
]);
754 dprintk(3, "API Input %d = %d\n", i
, data
[i
]);
756 for (; i
< CX2341X_MBOX_MAX_DATA
; i
++)
757 mc417_memory_write(dev
, dev
->cx23417_mailbox
+ 4 + i
, 0);
759 flag
|= 3; /* tell 'em we're done writing */
760 mc417_memory_write(dev
, dev
->cx23417_mailbox
, flag
);
762 /* wait for firmware to handle the API command */
763 timeout
= jiffies
+ msecs_to_jiffies(10);
765 mc417_memory_read(dev
, dev
->cx23417_mailbox
, &flag
);
768 if (time_after(jiffies
, timeout
)) {
769 dprintk(3, "ERROR: API Mailbox timeout\n");
775 /* read output values */
776 for (i
= 0; i
< out
; i
++) {
777 mc417_memory_read(dev
, dev
->cx23417_mailbox
+ 4 + i
, data
+ i
);
778 dprintk(3, "API Output %d = %d\n", i
, data
[i
]);
781 mc417_memory_read(dev
, dev
->cx23417_mailbox
+ 2, &retval
);
782 dprintk(3, "API result = %d\n", retval
);
785 mc417_memory_write(dev
, dev
->cx23417_mailbox
, flag
);
790 /* We don't need to call the API often, so using just one
791 * mailbox will probably suffice
793 static int cx231xx_api_cmd(struct cx231xx
*dev
, u32 command
,
794 u32 inputcnt
, u32 outputcnt
, ...)
796 u32 data
[CX2341X_MBOX_MAX_DATA
];
800 dprintk(3, "%s() cmds = 0x%08x\n", __func__
, command
);
802 va_start(vargs
, outputcnt
);
803 for (i
= 0; i
< inputcnt
; i
++)
804 data
[i
] = va_arg(vargs
, int);
806 err
= cx231xx_mbox_func(dev
, command
, inputcnt
, outputcnt
, data
);
807 for (i
= 0; i
< outputcnt
; i
++) {
808 int *vptr
= va_arg(vargs
, int *);
817 static int cx231xx_find_mailbox(struct cx231xx
*dev
)
820 0x12345678, 0x34567812, 0x56781234, 0x78123456
822 int signaturecnt
= 0;
827 dprintk(2, "%s()\n", __func__
);
829 for (i
= 0; i
< 0x100; i
++) {/*CX231xx_FIRM_IMAGE_SIZE*/
830 ret
= mc417_memory_read(dev
, i
, &value
);
833 if (value
== signature
[signaturecnt
])
837 if (4 == signaturecnt
) {
838 dprintk(1, "Mailbox signature found at 0x%x\n", i
+ 1);
842 dprintk(3, "Mailbox signature values not found!\n");
846 static void mci_write_memory_to_gpio(struct cx231xx
*dev
, u32 address
, u32 value
,
852 temp
= 0x82 | MCI_MEMORY_DATA_BYTE0
| ((value
& 0x000000FF) << 8);
856 temp
= temp
| (0x05 << 10);
860 /*write data byte 1;*/
861 temp
= 0x82 | MCI_MEMORY_DATA_BYTE1
| (value
& 0x0000FF00);
865 temp
= temp
| (0x05 << 10);
869 /*write data byte 2;*/
870 temp
= 0x82 | MCI_MEMORY_DATA_BYTE2
| ((value
& 0x00FF0000) >> 8);
874 temp
= temp
| (0x05 << 10);
878 /*write data byte 3;*/
879 temp
= 0x82 | MCI_MEMORY_DATA_BYTE3
| ((value
& 0xFF000000) >> 16);
883 temp
= temp
| (0x05 << 10);
887 /* write address byte 2;*/
888 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE2
| MCI_MODE_MEMORY_WRITE
|
889 ((address
& 0x003F0000) >> 8);
893 temp
= temp
| (0x05 << 10);
897 /* write address byte 1;*/
898 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE1
| (address
& 0xFF00);
902 temp
= temp
| (0x05 << 10);
906 /* write address byte 0;*/
907 temp
= 0x82 | MCI_MEMORY_ADDRESS_BYTE0
| ((address
& 0x00FF) << 8);
911 temp
= temp
| (0x05 << 10);
915 for (i
= 0; i
< 6; i
++) {
916 *p_fw_image
= 0xFFFFFFFF;
922 static int cx231xx_load_firmware(struct cx231xx
*dev
)
924 static const unsigned char magic
[8] = {
925 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
927 const struct firmware
*firmware
;
931 /*u32 checksum = 0;*/
933 u32 transfer_size
= 0;
936 /*u32 current_fw[800];*/
937 u32
*p_current_fw
, *p_fw
;
940 u16 _buffer_size
= 4096;
943 p_current_fw
= vmalloc(1884180 * 4);
945 if (p_current_fw
== NULL
) {
946 dprintk(2, "FAIL!!!\n");
950 p_buffer
= vmalloc(4096);
951 if (p_buffer
== NULL
) {
952 dprintk(2, "FAIL!!!\n");
957 dprintk(2, "%s()\n", __func__
);
959 /* Save GPIO settings before reset of APU */
960 retval
|= mc417_memory_read(dev
, 0x9020, &gpio_output
);
961 retval
|= mc417_memory_read(dev
, 0x900C, &value
);
963 retval
= mc417_register_write(dev
,
964 IVTV_REG_VPU
, 0xFFFFFFED);
965 retval
|= mc417_register_write(dev
,
966 IVTV_REG_HW_BLOCKS
, IVTV_CMD_HW_BLOCKS_RST
);
967 retval
|= mc417_register_write(dev
,
968 IVTV_REG_ENC_SDRAM_REFRESH
, 0x80000800);
969 retval
|= mc417_register_write(dev
,
970 IVTV_REG_ENC_SDRAM_PRECHARGE
, 0x1A);
971 retval
|= mc417_register_write(dev
,
976 "%s: Error with mc417_register_write\n", __func__
);
982 retval
= request_firmware(&firmware
, CX231xx_FIRM_IMAGE_NAME
,
987 "ERROR: Hotplug firmware request failed (%s).\n",
988 CX231xx_FIRM_IMAGE_NAME
);
990 "Please fix your hotplug setup, the board will not work without firmware loaded!\n");
996 if (firmware
->size
!= CX231xx_FIRM_IMAGE_SIZE
) {
998 "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
999 firmware
->size
, CX231xx_FIRM_IMAGE_SIZE
);
1000 release_firmware(firmware
);
1001 vfree(p_current_fw
);
1006 if (0 != memcmp(firmware
->data
, magic
, 8)) {
1008 "ERROR: Firmware magic mismatch, wrong file?\n");
1009 release_firmware(firmware
);
1010 vfree(p_current_fw
);
1017 /* transfer to the chip */
1018 dprintk(2, "Loading firmware to GPIO...\n");
1019 p_fw_data
= (u32
*)firmware
->data
;
1020 dprintk(2, "firmware->size=%zd\n", firmware
->size
);
1021 for (transfer_size
= 0; transfer_size
< firmware
->size
;
1022 transfer_size
+= 4) {
1023 fw_data
= *p_fw_data
;
1025 mci_write_memory_to_gpio(dev
, address
, fw_data
, p_current_fw
);
1026 address
= address
+ 1;
1031 /*download the firmware by ep5-out*/
1033 for (frame
= 0; frame
< (int)(CX231xx_FIRM_IMAGE_SIZE
*20/_buffer_size
);
1035 for (i
= 0; i
< _buffer_size
; i
++) {
1036 *(p_buffer
+ i
) = (u8
)(*(p_fw
+ (frame
* 128 * 8 + (i
/ 4))) & 0x000000FF);
1038 *(p_buffer
+ i
) = (u8
)((*(p_fw
+ (frame
* 128 * 8 + (i
/ 4))) & 0x0000FF00) >> 8);
1040 *(p_buffer
+ i
) = (u8
)((*(p_fw
+ (frame
* 128 * 8 + (i
/ 4))) & 0x00FF0000) >> 16);
1042 *(p_buffer
+ i
) = (u8
)((*(p_fw
+ (frame
* 128 * 8 + (i
/ 4))) & 0xFF000000) >> 24);
1044 cx231xx_ep5_bulkout(dev
, p_buffer
, _buffer_size
);
1047 p_current_fw
= p_fw
;
1048 vfree(p_current_fw
);
1049 p_current_fw
= NULL
;
1052 release_firmware(firmware
);
1053 dprintk(1, "Firmware upload successful.\n");
1055 retval
|= mc417_register_write(dev
, IVTV_REG_HW_BLOCKS
,
1056 IVTV_CMD_HW_BLOCKS_RST
);
1059 "%s: Error with mc417_register_write\n",
1063 /* F/W power up disturbs the GPIOs, restore state */
1064 retval
|= mc417_register_write(dev
, 0x9020, gpio_output
);
1065 retval
|= mc417_register_write(dev
, 0x900C, value
);
1067 retval
|= mc417_register_read(dev
, IVTV_REG_VPU
, &value
);
1068 retval
|= mc417_register_write(dev
, IVTV_REG_VPU
, value
& 0xFFFFFFE8);
1072 "%s: Error with mc417_register_write\n",
1079 static void cx231xx_codec_settings(struct cx231xx
*dev
)
1081 dprintk(1, "%s()\n", __func__
);
1083 /* assign frame size */
1084 cx231xx_api_cmd(dev
, CX2341X_ENC_SET_FRAME_SIZE
, 2, 0,
1085 dev
->ts1
.height
, dev
->ts1
.width
);
1087 dev
->mpeg_ctrl_handler
.width
= dev
->ts1
.width
;
1088 dev
->mpeg_ctrl_handler
.height
= dev
->ts1
.height
;
1090 cx2341x_handler_setup(&dev
->mpeg_ctrl_handler
);
1092 cx231xx_api_cmd(dev
, CX2341X_ENC_MISC
, 2, 0, 3, 1);
1093 cx231xx_api_cmd(dev
, CX2341X_ENC_MISC
, 2, 0, 4, 1);
1096 static int cx231xx_initialize_codec(struct cx231xx
*dev
)
1103 dprintk(1, "%s()\n", __func__
);
1104 cx231xx_disable656(dev
);
1105 retval
= cx231xx_api_cmd(dev
, CX2341X_ENC_PING_FW
, 0, 0); /* ping */
1107 dprintk(2, "%s: PING OK\n", __func__
);
1108 retval
= cx231xx_load_firmware(dev
);
1111 "%s: f/w load failed\n", __func__
);
1114 retval
= cx231xx_find_mailbox(dev
);
1116 dev_err(dev
->dev
, "%s: mailbox < 0, error\n",
1120 dev
->cx23417_mailbox
= retval
;
1121 retval
= cx231xx_api_cmd(dev
, CX2341X_ENC_PING_FW
, 0, 0);
1124 "ERROR: cx23417 firmware ping failed!\n");
1127 retval
= cx231xx_api_cmd(dev
, CX2341X_ENC_GET_VERSION
, 0, 1,
1131 "ERROR: cx23417 firmware get encoder: version failed!\n");
1134 dprintk(1, "cx23417 firmware version is 0x%08x\n", version
);
1138 for (i
= 0; i
< 1; i
++) {
1139 retval
= mc417_register_read(dev
, 0x20f8, &val
);
1140 dprintk(3, "***before enable656() VIM Capture Lines = %d ***\n",
1146 cx231xx_enable656(dev
);
1148 /* stop mpeg capture */
1149 cx231xx_api_cmd(dev
, CX2341X_ENC_STOP_CAPTURE
, 3, 0, 1, 3, 4);
1151 cx231xx_codec_settings(dev
);
1154 /* cx231xx_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
1155 CX231xx_FIELD1_SAA7115, CX231xx_FIELD2_SAA7115);
1156 cx231xx_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
1157 CX231xx_CUSTOM_EXTENSION_USR_DATA, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1165 /* Setup to capture VBI */
1166 data
[0] = 0x0001BD00;
1167 data
[1] = 1; /* frames per interrupt */
1168 data
[2] = 4; /* total bufs */
1169 data
[3] = 0x91559155; /* start codes */
1170 data
[4] = 0x206080C0; /* stop codes */
1171 data
[5] = 6; /* lines */
1172 data
[6] = 64; /* BPL */
1174 cx231xx_api_cmd(dev
, CX2341X_ENC_SET_VBI_CONFIG
, 7, 0, data
[0], data
[1],
1175 data
[2], data
[3], data
[4], data
[5], data
[6]);
1177 for (i
= 2; i
<= 24; i
++) {
1180 valid
= ((i
>= 19) && (i
<= 21));
1181 cx231xx_api_cmd(dev
, CX2341X_ENC_SET_VBI_LINE
, 5, 0, i
,
1183 cx231xx_api_cmd(dev
, CX2341X_ENC_SET_VBI_LINE
, 5, 0,
1184 i
| 0x80000000, valid
, 0, 0, 0);
1187 /* cx231xx_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, CX231xx_UNMUTE);
1190 /* initialize the video input */
1191 retval
= cx231xx_api_cmd(dev
, CX2341X_ENC_INITIALIZE_INPUT
, 0, 0);
1196 /* Enable VIP style pixel invalidation so we work with scaled mode */
1197 mc417_memory_write(dev
, 2120, 0x00000080);
1199 /* start capturing to the host interface */
1200 retval
= cx231xx_api_cmd(dev
, CX2341X_ENC_START_CAPTURE
, 2, 0,
1201 CX231xx_MPEG_CAPTURE
, CX231xx_RAW_BITS_NONE
);
1206 for (i
= 0; i
< 1; i
++) {
1207 mc417_register_read(dev
, 0x20f8, &val
);
1208 dprintk(3, "***VIM Capture Lines =%d ***\n", val
);
1214 /* ------------------------------------------------------------------ */
1216 static int queue_setup(struct vb2_queue
*vq
,
1217 unsigned int *nbuffers
, unsigned int *nplanes
,
1218 unsigned int sizes
[], struct device
*alloc_devs
[])
1220 struct cx231xx
*dev
= vb2_get_drv_priv(vq
);
1221 unsigned int size
= mpeglinesize
* mpeglines
;
1223 dev
->ts1
.ts_packet_size
= mpeglinesize
;
1224 dev
->ts1
.ts_packet_count
= mpeglines
;
1226 if (vq
->num_buffers
+ *nbuffers
< CX231XX_MIN_BUF
)
1227 *nbuffers
= CX231XX_MIN_BUF
- vq
->num_buffers
;
1230 return sizes
[0] < size
? -EINVAL
: 0;
1232 sizes
[0] = mpeglinesize
* mpeglines
;
1237 static void buffer_copy(struct cx231xx
*dev
, char *data
, int len
, struct urb
*urb
,
1238 struct cx231xx_dmaqueue
*dma_q
)
1241 struct cx231xx_buffer
*buf
;
1245 if (dma_q
->mpeg_buffer_done
== 0) {
1246 if (list_empty(&dma_q
->active
))
1249 buf
= list_entry(dma_q
->active
.next
,
1250 struct cx231xx_buffer
, list
);
1251 dev
->video_mode
.isoc_ctl
.buf
= buf
;
1252 dma_q
->mpeg_buffer_done
= 1;
1255 buf
= dev
->video_mode
.isoc_ctl
.buf
;
1256 vbuf
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
1258 if ((dma_q
->mpeg_buffer_completed
+len
) <
1259 mpeglines
*mpeglinesize
) {
1260 if (dma_q
->add_ps_package_head
==
1261 CX231XX_NEED_ADD_PS_PACKAGE_HEAD
) {
1262 memcpy(vbuf
+dma_q
->mpeg_buffer_completed
,
1264 dma_q
->mpeg_buffer_completed
=
1265 dma_q
->mpeg_buffer_completed
+ 3;
1266 dma_q
->add_ps_package_head
=
1267 CX231XX_NONEED_PS_PACKAGE_HEAD
;
1269 memcpy(vbuf
+dma_q
->mpeg_buffer_completed
, data
, len
);
1270 dma_q
->mpeg_buffer_completed
=
1271 dma_q
->mpeg_buffer_completed
+ len
;
1273 dma_q
->mpeg_buffer_done
= 0;
1276 mpeglines
*mpeglinesize
- dma_q
->mpeg_buffer_completed
;
1277 memcpy(vbuf
+dma_q
->mpeg_buffer_completed
,
1280 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
1281 buf
->vb
.sequence
= dma_q
->sequence
++;
1282 list_del(&buf
->list
);
1283 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
1284 dma_q
->mpeg_buffer_completed
= 0;
1286 if (len
- tail_data
> 0) {
1287 p_data
= data
+ tail_data
;
1288 dma_q
->left_data_count
= len
- tail_data
;
1289 memcpy(dma_q
->p_left_data
,
1290 p_data
, len
- tail_data
);
1295 static void buffer_filled(char *data
, int len
, struct urb
*urb
,
1296 struct cx231xx_dmaqueue
*dma_q
)
1299 struct cx231xx_buffer
*buf
;
1301 if (list_empty(&dma_q
->active
))
1304 buf
= list_entry(dma_q
->active
.next
, struct cx231xx_buffer
, list
);
1307 vbuf
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
1308 memcpy(vbuf
, data
, len
);
1309 buf
->vb
.sequence
= dma_q
->sequence
++;
1310 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
1311 list_del(&buf
->list
);
1312 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
1315 static int cx231xx_isoc_copy(struct cx231xx
*dev
, struct urb
*urb
)
1317 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
1318 unsigned char *p_buffer
;
1319 u32 buffer_size
= 0;
1322 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1323 if (dma_q
->left_data_count
> 0) {
1324 buffer_copy(dev
, dma_q
->p_left_data
,
1325 dma_q
->left_data_count
, urb
, dma_q
);
1326 dma_q
->mpeg_buffer_completed
= dma_q
->left_data_count
;
1327 dma_q
->left_data_count
= 0;
1330 p_buffer
= urb
->transfer_buffer
+
1331 urb
->iso_frame_desc
[i
].offset
;
1332 buffer_size
= urb
->iso_frame_desc
[i
].actual_length
;
1334 if (buffer_size
> 0)
1335 buffer_copy(dev
, p_buffer
, buffer_size
, urb
, dma_q
);
1341 static int cx231xx_bulk_copy(struct cx231xx
*dev
, struct urb
*urb
)
1343 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
1344 unsigned char *p_buffer
, *buffer
;
1345 u32 buffer_size
= 0;
1347 p_buffer
= urb
->transfer_buffer
;
1348 buffer_size
= urb
->actual_length
;
1350 buffer
= kmalloc(buffer_size
, GFP_ATOMIC
);
1354 memcpy(buffer
, dma_q
->ps_head
, 3);
1355 memcpy(buffer
+3, p_buffer
, buffer_size
-3);
1356 memcpy(dma_q
->ps_head
, p_buffer
+buffer_size
-3, 3);
1359 buffer_filled(p_buffer
, buffer_size
, urb
, dma_q
);
1365 static void buffer_queue(struct vb2_buffer
*vb
)
1367 struct cx231xx_buffer
*buf
=
1368 container_of(vb
, struct cx231xx_buffer
, vb
.vb2_buf
);
1369 struct cx231xx
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
1370 struct cx231xx_dmaqueue
*vidq
= &dev
->video_mode
.vidq
;
1371 unsigned long flags
;
1373 spin_lock_irqsave(&dev
->video_mode
.slock
, flags
);
1374 list_add_tail(&buf
->list
, &vidq
->active
);
1375 spin_unlock_irqrestore(&dev
->video_mode
.slock
, flags
);
1378 static void return_all_buffers(struct cx231xx
*dev
,
1379 enum vb2_buffer_state state
)
1381 struct cx231xx_dmaqueue
*vidq
= &dev
->video_mode
.vidq
;
1382 struct cx231xx_buffer
*buf
, *node
;
1383 unsigned long flags
;
1385 spin_lock_irqsave(&dev
->video_mode
.slock
, flags
);
1386 list_for_each_entry_safe(buf
, node
, &vidq
->active
, list
) {
1387 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
1388 list_del(&buf
->list
);
1390 spin_unlock_irqrestore(&dev
->video_mode
.slock
, flags
);
1393 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
1395 struct cx231xx
*dev
= vb2_get_drv_priv(vq
);
1396 struct cx231xx_dmaqueue
*vidq
= &dev
->video_mode
.vidq
;
1402 cx231xx_set_alt_setting(dev
, INDEX_VANC
, 1);
1403 cx231xx_set_gpio_value(dev
, 2, 0);
1405 cx231xx_initialize_codec(dev
);
1407 cx231xx_start_TS1(dev
);
1409 cx231xx_set_alt_setting(dev
, INDEX_TS1
, 0);
1410 cx231xx_set_mode(dev
, CX231XX_DIGITAL_MODE
);
1412 ret
= cx231xx_init_isoc(dev
, CX231XX_NUM_PACKETS
,
1414 dev
->ts1_mode
.max_pkt_size
,
1417 ret
= cx231xx_init_bulk(dev
, 320, 5,
1418 dev
->ts1_mode
.max_pkt_size
,
1421 return_all_buffers(dev
, VB2_BUF_STATE_QUEUED
);
1423 call_all(dev
, video
, s_stream
, 1);
1427 static void stop_streaming(struct vb2_queue
*vq
)
1429 struct cx231xx
*dev
= vb2_get_drv_priv(vq
);
1430 unsigned long flags
;
1432 call_all(dev
, video
, s_stream
, 0);
1434 cx231xx_stop_TS1(dev
);
1436 /* do this before setting alternate! */
1438 cx231xx_uninit_isoc(dev
);
1440 cx231xx_uninit_bulk(dev
);
1441 cx231xx_set_mode(dev
, CX231XX_SUSPEND
);
1443 cx231xx_api_cmd(dev
, CX2341X_ENC_STOP_CAPTURE
, 3, 0,
1444 CX231xx_END_NOW
, CX231xx_MPEG_CAPTURE
,
1445 CX231xx_RAW_BITS_NONE
);
1447 spin_lock_irqsave(&dev
->video_mode
.slock
, flags
);
1449 dev
->video_mode
.isoc_ctl
.buf
= NULL
;
1451 dev
->video_mode
.bulk_ctl
.buf
= NULL
;
1452 spin_unlock_irqrestore(&dev
->video_mode
.slock
, flags
);
1453 return_all_buffers(dev
, VB2_BUF_STATE_ERROR
);
1456 static struct vb2_ops cx231xx_video_qops
= {
1457 .queue_setup
= queue_setup
,
1458 .buf_queue
= buffer_queue
,
1459 .start_streaming
= start_streaming
,
1460 .stop_streaming
= stop_streaming
,
1461 .wait_prepare
= vb2_ops_wait_prepare
,
1462 .wait_finish
= vb2_ops_wait_finish
,
1465 /* ------------------------------------------------------------------ */
1467 static int vidioc_g_pixelaspect(struct file
*file
, void *priv
,
1468 int type
, struct v4l2_fract
*f
)
1470 struct cx231xx
*dev
= video_drvdata(file
);
1471 bool is_50hz
= dev
->encodernorm
.id
& V4L2_STD_625_50
;
1473 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1476 f
->numerator
= is_50hz
? 54 : 11;
1477 f
->denominator
= is_50hz
? 59 : 10;
1482 static int vidioc_g_selection(struct file
*file
, void *priv
,
1483 struct v4l2_selection
*s
)
1485 struct cx231xx
*dev
= video_drvdata(file
);
1487 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1490 switch (s
->target
) {
1491 case V4L2_SEL_TGT_CROP_BOUNDS
:
1492 case V4L2_SEL_TGT_CROP_DEFAULT
:
1495 s
->r
.width
= dev
->ts1
.width
;
1496 s
->r
.height
= dev
->ts1
.height
;
1504 static int vidioc_g_std(struct file
*file
, void *fh0
, v4l2_std_id
*norm
)
1506 struct cx231xx
*dev
= video_drvdata(file
);
1508 *norm
= dev
->encodernorm
.id
;
1512 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1514 struct cx231xx
*dev
= video_drvdata(file
);
1517 for (i
= 0; i
< ARRAY_SIZE(cx231xx_tvnorms
); i
++)
1518 if (id
& cx231xx_tvnorms
[i
].id
)
1520 if (i
== ARRAY_SIZE(cx231xx_tvnorms
))
1522 dev
->encodernorm
= cx231xx_tvnorms
[i
];
1524 if (dev
->encodernorm
.id
& 0xb000) {
1525 dprintk(3, "encodernorm set to NTSC\n");
1526 dev
->norm
= V4L2_STD_NTSC
;
1527 dev
->ts1
.height
= 480;
1528 cx2341x_handler_set_50hz(&dev
->mpeg_ctrl_handler
, false);
1530 dprintk(3, "encodernorm set to PAL\n");
1531 dev
->norm
= V4L2_STD_PAL_B
;
1532 dev
->ts1
.height
= 576;
1533 cx2341x_handler_set_50hz(&dev
->mpeg_ctrl_handler
, true);
1535 call_all(dev
, video
, s_std
, dev
->norm
);
1536 /* do mode control overrides */
1537 cx231xx_do_mode_ctrl_overrides(dev
);
1539 dprintk(3, "exit vidioc_s_std() i=0x%x\n", i
);
1543 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1544 struct v4l2_control
*ctl
)
1546 struct cx231xx
*dev
= video_drvdata(file
);
1547 struct v4l2_subdev
*sd
;
1549 dprintk(3, "enter vidioc_s_ctrl()\n");
1550 /* Update the A/V core */
1551 v4l2_device_for_each_subdev(sd
, &dev
->v4l2_dev
)
1552 v4l2_s_ctrl(NULL
, sd
->ctrl_handler
, ctl
);
1553 dprintk(3, "exit vidioc_s_ctrl()\n");
1557 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1558 struct v4l2_fmtdesc
*f
)
1563 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
1568 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1569 struct v4l2_format
*f
)
1571 struct cx231xx
*dev
= video_drvdata(file
);
1573 dprintk(3, "enter vidioc_g_fmt_vid_cap()\n");
1574 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1575 f
->fmt
.pix
.bytesperline
= 0;
1576 f
->fmt
.pix
.sizeimage
= mpeglines
* mpeglinesize
;
1577 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1578 f
->fmt
.pix
.width
= dev
->ts1
.width
;
1579 f
->fmt
.pix
.height
= dev
->ts1
.height
;
1580 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1581 dprintk(1, "VIDIOC_G_FMT: w: %d, h: %d\n",
1582 dev
->ts1
.width
, dev
->ts1
.height
);
1583 dprintk(3, "exit vidioc_g_fmt_vid_cap()\n");
1587 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1588 struct v4l2_format
*f
)
1590 struct cx231xx
*dev
= video_drvdata(file
);
1592 dprintk(3, "enter vidioc_try_fmt_vid_cap()\n");
1593 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1594 f
->fmt
.pix
.bytesperline
= 0;
1595 f
->fmt
.pix
.sizeimage
= mpeglines
* mpeglinesize
;
1596 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1597 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1598 dprintk(1, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
1599 dev
->ts1
.width
, dev
->ts1
.height
);
1600 dprintk(3, "exit vidioc_try_fmt_vid_cap()\n");
1604 static int vidioc_log_status(struct file
*file
, void *priv
)
1606 struct cx231xx
*dev
= video_drvdata(file
);
1608 call_all(dev
, core
, log_status
);
1609 return v4l2_ctrl_log_status(file
, priv
);
1612 static const struct v4l2_file_operations mpeg_fops
= {
1613 .owner
= THIS_MODULE
,
1614 .open
= v4l2_fh_open
,
1615 .release
= vb2_fop_release
,
1616 .read
= vb2_fop_read
,
1617 .poll
= vb2_fop_poll
,
1618 .mmap
= vb2_fop_mmap
,
1619 .unlocked_ioctl
= video_ioctl2
,
1622 static const struct v4l2_ioctl_ops mpeg_ioctl_ops
= {
1623 .vidioc_s_std
= vidioc_s_std
,
1624 .vidioc_g_std
= vidioc_g_std
,
1625 .vidioc_g_tuner
= cx231xx_g_tuner
,
1626 .vidioc_s_tuner
= cx231xx_s_tuner
,
1627 .vidioc_g_frequency
= cx231xx_g_frequency
,
1628 .vidioc_s_frequency
= cx231xx_s_frequency
,
1629 .vidioc_enum_input
= cx231xx_enum_input
,
1630 .vidioc_g_input
= cx231xx_g_input
,
1631 .vidioc_s_input
= cx231xx_s_input
,
1632 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1633 .vidioc_g_pixelaspect
= vidioc_g_pixelaspect
,
1634 .vidioc_g_selection
= vidioc_g_selection
,
1635 .vidioc_querycap
= cx231xx_querycap
,
1636 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1637 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1638 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1639 .vidioc_s_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1640 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1641 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1642 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1643 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1644 .vidioc_streamon
= vb2_ioctl_streamon
,
1645 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1646 .vidioc_log_status
= vidioc_log_status
,
1647 #ifdef CONFIG_VIDEO_ADV_DEBUG
1648 .vidioc_g_register
= cx231xx_g_register
,
1649 .vidioc_s_register
= cx231xx_s_register
,
1651 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1652 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1655 static struct video_device cx231xx_mpeg_template
= {
1658 .ioctl_ops
= &mpeg_ioctl_ops
,
1660 .tvnorms
= V4L2_STD_ALL
,
1663 void cx231xx_417_unregister(struct cx231xx
*dev
)
1665 dprintk(1, "%s()\n", __func__
);
1666 dprintk(3, "%s()\n", __func__
);
1668 if (video_is_registered(&dev
->v4l_device
)) {
1669 video_unregister_device(&dev
->v4l_device
);
1670 v4l2_ctrl_handler_free(&dev
->mpeg_ctrl_handler
.hdl
);
1674 static int cx231xx_s_video_encoding(struct cx2341x_handler
*cxhdl
, u32 val
)
1676 struct cx231xx
*dev
= container_of(cxhdl
, struct cx231xx
, mpeg_ctrl_handler
);
1677 int is_mpeg1
= val
== V4L2_MPEG_VIDEO_ENCODING_MPEG_1
;
1678 struct v4l2_subdev_format format
= {
1679 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1682 /* fix videodecoder resolution */
1683 format
.format
.width
= cxhdl
->width
/ (is_mpeg1
? 2 : 1);
1684 format
.format
.height
= cxhdl
->height
;
1685 format
.format
.code
= MEDIA_BUS_FMT_FIXED
;
1686 v4l2_subdev_call(dev
->sd_cx25840
, pad
, set_fmt
, NULL
, &format
);
1690 static int cx231xx_s_audio_sampling_freq(struct cx2341x_handler
*cxhdl
, u32 idx
)
1692 static const u32 freqs
[3] = { 44100, 48000, 32000 };
1693 struct cx231xx
*dev
= container_of(cxhdl
, struct cx231xx
, mpeg_ctrl_handler
);
1695 /* The audio clock of the digitizer must match the codec sample
1696 rate otherwise you get some very strange effects. */
1697 if (idx
< ARRAY_SIZE(freqs
))
1698 call_all(dev
, audio
, s_clock_freq
, freqs
[idx
]);
1702 static const struct cx2341x_handler_ops cx231xx_ops
= {
1703 /* needed for the video clock freq */
1704 .s_audio_sampling_freq
= cx231xx_s_audio_sampling_freq
,
1705 /* needed for setting up the video resolution */
1706 .s_video_encoding
= cx231xx_s_video_encoding
,
1709 static void cx231xx_video_dev_init(
1710 struct cx231xx
*dev
,
1711 struct usb_device
*usbdev
,
1712 struct video_device
*vfd
,
1713 const struct video_device
*template,
1716 dprintk(1, "%s()\n", __func__
);
1718 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)", dev
->name
,
1719 type
, cx231xx_boards
[dev
->model
].name
);
1721 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1722 vfd
->lock
= &dev
->lock
;
1723 vfd
->release
= video_device_release_empty
;
1724 vfd
->ctrl_handler
= &dev
->mpeg_ctrl_handler
.hdl
;
1725 video_set_drvdata(vfd
, dev
);
1726 if (dev
->tuner_type
== TUNER_ABSENT
) {
1727 v4l2_disable_ioctl(vfd
, VIDIOC_G_FREQUENCY
);
1728 v4l2_disable_ioctl(vfd
, VIDIOC_S_FREQUENCY
);
1729 v4l2_disable_ioctl(vfd
, VIDIOC_G_TUNER
);
1730 v4l2_disable_ioctl(vfd
, VIDIOC_S_TUNER
);
1734 int cx231xx_417_register(struct cx231xx
*dev
)
1736 /* FIXME: Port1 hardcoded here */
1738 struct cx231xx_tsport
*tsport
= &dev
->ts1
;
1739 struct vb2_queue
*q
;
1741 dprintk(1, "%s()\n", __func__
);
1743 /* Set default TV standard */
1744 dev
->encodernorm
= cx231xx_tvnorms
[0];
1746 if (dev
->encodernorm
.id
& V4L2_STD_525_60
)
1747 tsport
->height
= 480;
1749 tsport
->height
= 576;
1751 tsport
->width
= 720;
1752 err
= cx2341x_handler_init(&dev
->mpeg_ctrl_handler
, 50);
1754 dprintk(3, "%s: can't init cx2341x controls\n", dev
->name
);
1757 dev
->mpeg_ctrl_handler
.func
= cx231xx_mbox_func
;
1758 dev
->mpeg_ctrl_handler
.priv
= dev
;
1759 dev
->mpeg_ctrl_handler
.ops
= &cx231xx_ops
;
1760 if (dev
->sd_cx25840
)
1761 v4l2_ctrl_add_handler(&dev
->mpeg_ctrl_handler
.hdl
,
1762 dev
->sd_cx25840
->ctrl_handler
, NULL
, false);
1763 if (dev
->mpeg_ctrl_handler
.hdl
.error
) {
1764 err
= dev
->mpeg_ctrl_handler
.hdl
.error
;
1765 dprintk(3, "%s: can't add cx25840 controls\n", dev
->name
);
1766 v4l2_ctrl_handler_free(&dev
->mpeg_ctrl_handler
.hdl
);
1769 dev
->norm
= V4L2_STD_NTSC
;
1771 dev
->mpeg_ctrl_handler
.port
= CX2341X_PORT_SERIAL
;
1772 cx2341x_handler_set_50hz(&dev
->mpeg_ctrl_handler
, false);
1774 /* Allocate and initialize V4L video device */
1775 cx231xx_video_dev_init(dev
, dev
->udev
,
1776 &dev
->v4l_device
, &cx231xx_mpeg_template
, "mpeg");
1778 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1779 q
->io_modes
= VB2_USERPTR
| VB2_MMAP
| VB2_DMABUF
| VB2_READ
;
1781 q
->buf_struct_size
= sizeof(struct cx231xx_buffer
);
1782 q
->ops
= &cx231xx_video_qops
;
1783 q
->mem_ops
= &vb2_vmalloc_memops
;
1784 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1785 q
->min_buffers_needed
= 1;
1786 q
->lock
= &dev
->lock
;
1787 err
= vb2_queue_init(q
);
1790 dev
->v4l_device
.queue
= q
;
1792 err
= video_register_device(&dev
->v4l_device
,
1793 VFL_TYPE_GRABBER
, -1);
1795 dprintk(3, "%s: can't register mpeg device\n", dev
->name
);
1796 v4l2_ctrl_handler_free(&dev
->mpeg_ctrl_handler
.hdl
);
1800 dprintk(3, "%s: registered device video%d [mpeg]\n",
1801 dev
->name
, dev
->v4l_device
.num
);
1806 MODULE_FIRMWARE(CX231xx_FIRM_IMAGE_NAME
);