xref: /linux/drivers/media/usb/cx231xx/cx231xx-417.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab  *
40c0d06caSMauro Carvalho Chehab  *  Support for a cx23417 mpeg encoder via cx231xx host port.
50c0d06caSMauro Carvalho Chehab  *
60c0d06caSMauro Carvalho Chehab  *    (c) 2004 Jelle Foks <jelle@foks.us>
70c0d06caSMauro Carvalho Chehab  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
80c0d06caSMauro Carvalho Chehab  *    (c) 2008 Steven Toth <stoth@linuxtv.org>
90c0d06caSMauro Carvalho Chehab  *      - CX23885/7/8 support
100c0d06caSMauro Carvalho Chehab  *
110c0d06caSMauro Carvalho Chehab  *  Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
120c0d06caSMauro Carvalho Chehab  */
130c0d06caSMauro Carvalho Chehab 
14589dadf2SMauro Carvalho Chehab #include "cx231xx.h"
15589dadf2SMauro Carvalho Chehab 
160c0d06caSMauro Carvalho Chehab #include <linux/module.h>
170c0d06caSMauro Carvalho Chehab #include <linux/moduleparam.h>
180c0d06caSMauro Carvalho Chehab #include <linux/init.h>
190c0d06caSMauro Carvalho Chehab #include <linux/fs.h>
200c0d06caSMauro Carvalho Chehab #include <linux/delay.h>
210c0d06caSMauro Carvalho Chehab #include <linux/device.h>
220c0d06caSMauro Carvalho Chehab #include <linux/firmware.h>
237cc31faeSAnders Roxell #include <linux/slab.h>
240c0d06caSMauro Carvalho Chehab #include <linux/vmalloc.h>
250c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h>
260c0d06caSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
2788b6ffedSHans Verkuil #include <media/v4l2-event.h>
28d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/cx2341x.h>
29b86d1544SHans Verkuil #include <media/tuner.h>
300c0d06caSMauro Carvalho Chehab 
310c0d06caSMauro Carvalho Chehab #define CX231xx_FIRM_IMAGE_SIZE 376836
320c0d06caSMauro Carvalho Chehab #define CX231xx_FIRM_IMAGE_NAME "v4l-cx23885-enc.fw"
330c0d06caSMauro Carvalho Chehab 
340c0d06caSMauro Carvalho Chehab /* for polaris ITVC */
350c0d06caSMauro Carvalho Chehab #define ITVC_WRITE_DIR          0x03FDFC00
360c0d06caSMauro Carvalho Chehab #define ITVC_READ_DIR            0x0001FC00
370c0d06caSMauro Carvalho Chehab 
380c0d06caSMauro Carvalho Chehab #define  MCI_MEMORY_DATA_BYTE0          0x00
390c0d06caSMauro Carvalho Chehab #define  MCI_MEMORY_DATA_BYTE1          0x08
400c0d06caSMauro Carvalho Chehab #define  MCI_MEMORY_DATA_BYTE2          0x10
410c0d06caSMauro Carvalho Chehab #define  MCI_MEMORY_DATA_BYTE3          0x18
420c0d06caSMauro Carvalho Chehab 
430c0d06caSMauro Carvalho Chehab #define  MCI_MEMORY_ADDRESS_BYTE2       0x20
440c0d06caSMauro Carvalho Chehab #define  MCI_MEMORY_ADDRESS_BYTE1       0x28
450c0d06caSMauro Carvalho Chehab #define  MCI_MEMORY_ADDRESS_BYTE0       0x30
460c0d06caSMauro Carvalho Chehab 
470c0d06caSMauro Carvalho Chehab #define  MCI_REGISTER_DATA_BYTE0        0x40
480c0d06caSMauro Carvalho Chehab #define  MCI_REGISTER_DATA_BYTE1        0x48
490c0d06caSMauro Carvalho Chehab #define  MCI_REGISTER_DATA_BYTE2        0x50
500c0d06caSMauro Carvalho Chehab #define  MCI_REGISTER_DATA_BYTE3        0x58
510c0d06caSMauro Carvalho Chehab 
520c0d06caSMauro Carvalho Chehab #define  MCI_REGISTER_ADDRESS_BYTE0     0x60
530c0d06caSMauro Carvalho Chehab #define  MCI_REGISTER_ADDRESS_BYTE1     0x68
540c0d06caSMauro Carvalho Chehab 
550c0d06caSMauro Carvalho Chehab #define  MCI_REGISTER_MODE              0x70
560c0d06caSMauro Carvalho Chehab 
570c0d06caSMauro Carvalho Chehab /* Read and write modes for polaris ITVC */
580c0d06caSMauro Carvalho Chehab #define  MCI_MODE_REGISTER_READ         0x000
590c0d06caSMauro Carvalho Chehab #define  MCI_MODE_REGISTER_WRITE        0x100
600c0d06caSMauro Carvalho Chehab #define  MCI_MODE_MEMORY_READ           0x000
610c0d06caSMauro Carvalho Chehab #define  MCI_MODE_MEMORY_WRITE          0x4000
620c0d06caSMauro Carvalho Chehab 
630c0d06caSMauro Carvalho Chehab static unsigned int mpeglines = 128;
640c0d06caSMauro Carvalho Chehab module_param(mpeglines, int, 0644);
650c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(mpeglines, "number of lines in an MPEG buffer, range 2-32");
665b8acdc5SHans Verkuil 
670c0d06caSMauro Carvalho Chehab static unsigned int mpeglinesize = 512;
680c0d06caSMauro Carvalho Chehab module_param(mpeglinesize, int, 0644);
690c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(mpeglinesize,
700c0d06caSMauro Carvalho Chehab 	"number of bytes in each line of an MPEG buffer, range 512-1024");
710c0d06caSMauro Carvalho Chehab 
720c0d06caSMauro Carvalho Chehab static unsigned int v4l_debug = 1;
730c0d06caSMauro Carvalho Chehab module_param(v4l_debug, int, 0644);
740c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(v4l_debug, "enable V4L debug messages");
755b8acdc5SHans Verkuil 
760c0d06caSMauro Carvalho Chehab #define dprintk(level, fmt, arg...)	\
77ed0e3729SMauro Carvalho Chehab 	do {				\
78ed0e3729SMauro Carvalho Chehab 		if (v4l_debug >= level) \
79ed0e3729SMauro Carvalho Chehab 			printk(KERN_DEBUG pr_fmt(fmt), ## arg); \
800c0d06caSMauro Carvalho Chehab 	} while (0)
810c0d06caSMauro Carvalho Chehab 
820c0d06caSMauro Carvalho Chehab static struct cx231xx_tvnorm cx231xx_tvnorms[] = {
830c0d06caSMauro Carvalho Chehab 	{
840c0d06caSMauro Carvalho Chehab 		.name      = "NTSC-M",
850c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_NTSC_M,
860c0d06caSMauro Carvalho Chehab 	}, {
870c0d06caSMauro Carvalho Chehab 		.name      = "NTSC-JP",
880c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_NTSC_M_JP,
890c0d06caSMauro Carvalho Chehab 	}, {
900c0d06caSMauro Carvalho Chehab 		.name      = "PAL-BG",
910c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_PAL_BG,
920c0d06caSMauro Carvalho Chehab 	}, {
930c0d06caSMauro Carvalho Chehab 		.name      = "PAL-DK",
940c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_PAL_DK,
950c0d06caSMauro Carvalho Chehab 	}, {
960c0d06caSMauro Carvalho Chehab 		.name      = "PAL-I",
970c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_PAL_I,
980c0d06caSMauro Carvalho Chehab 	}, {
990c0d06caSMauro Carvalho Chehab 		.name      = "PAL-M",
1000c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_PAL_M,
1010c0d06caSMauro Carvalho Chehab 	}, {
1020c0d06caSMauro Carvalho Chehab 		.name      = "PAL-N",
1030c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_PAL_N,
1040c0d06caSMauro Carvalho Chehab 	}, {
1050c0d06caSMauro Carvalho Chehab 		.name      = "PAL-Nc",
1060c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_PAL_Nc,
1070c0d06caSMauro Carvalho Chehab 	}, {
1080c0d06caSMauro Carvalho Chehab 		.name      = "PAL-60",
1090c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_PAL_60,
1100c0d06caSMauro Carvalho Chehab 	}, {
1110c0d06caSMauro Carvalho Chehab 		.name      = "SECAM-L",
1120c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_SECAM_L,
1130c0d06caSMauro Carvalho Chehab 	}, {
1140c0d06caSMauro Carvalho Chehab 		.name      = "SECAM-DK",
1150c0d06caSMauro Carvalho Chehab 		.id        = V4L2_STD_SECAM_DK,
1160c0d06caSMauro Carvalho Chehab 	}
1170c0d06caSMauro Carvalho Chehab };
1180c0d06caSMauro Carvalho Chehab 
1190c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
1205b8acdc5SHans Verkuil 
1210c0d06caSMauro Carvalho Chehab enum cx231xx_capture_type {
1220c0d06caSMauro Carvalho Chehab 	CX231xx_MPEG_CAPTURE,
1230c0d06caSMauro Carvalho Chehab 	CX231xx_RAW_CAPTURE,
1240c0d06caSMauro Carvalho Chehab 	CX231xx_RAW_PASSTHRU_CAPTURE
1250c0d06caSMauro Carvalho Chehab };
1265b8acdc5SHans Verkuil 
1270c0d06caSMauro Carvalho Chehab enum cx231xx_capture_bits {
1280c0d06caSMauro Carvalho Chehab 	CX231xx_RAW_BITS_NONE             = 0x00,
1290c0d06caSMauro Carvalho Chehab 	CX231xx_RAW_BITS_YUV_CAPTURE      = 0x01,
1300c0d06caSMauro Carvalho Chehab 	CX231xx_RAW_BITS_PCM_CAPTURE      = 0x02,
1310c0d06caSMauro Carvalho Chehab 	CX231xx_RAW_BITS_VBI_CAPTURE      = 0x04,
1320c0d06caSMauro Carvalho Chehab 	CX231xx_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
1330c0d06caSMauro Carvalho Chehab 	CX231xx_RAW_BITS_TO_HOST_CAPTURE  = 0x10
1340c0d06caSMauro Carvalho Chehab };
1355b8acdc5SHans Verkuil 
1360c0d06caSMauro Carvalho Chehab enum cx231xx_capture_end {
1370c0d06caSMauro Carvalho Chehab 	CX231xx_END_AT_GOP, /* stop at the end of gop, generate irq */
1380c0d06caSMauro Carvalho Chehab 	CX231xx_END_NOW, /* stop immediately, no irq */
1390c0d06caSMauro Carvalho Chehab };
1405b8acdc5SHans Verkuil 
1410c0d06caSMauro Carvalho Chehab enum cx231xx_framerate {
1420c0d06caSMauro Carvalho Chehab 	CX231xx_FRAMERATE_NTSC_30, /* NTSC: 30fps */
1430c0d06caSMauro Carvalho Chehab 	CX231xx_FRAMERATE_PAL_25   /* PAL: 25fps */
1440c0d06caSMauro Carvalho Chehab };
1455b8acdc5SHans Verkuil 
1460c0d06caSMauro Carvalho Chehab enum cx231xx_stream_port {
1470c0d06caSMauro Carvalho Chehab 	CX231xx_OUTPUT_PORT_MEMORY,
1480c0d06caSMauro Carvalho Chehab 	CX231xx_OUTPUT_PORT_STREAMING,
1490c0d06caSMauro Carvalho Chehab 	CX231xx_OUTPUT_PORT_SERIAL
1500c0d06caSMauro Carvalho Chehab };
1515b8acdc5SHans Verkuil 
1520c0d06caSMauro Carvalho Chehab enum cx231xx_data_xfer_status {
1530c0d06caSMauro Carvalho Chehab 	CX231xx_MORE_BUFFERS_FOLLOW,
1540c0d06caSMauro Carvalho Chehab 	CX231xx_LAST_BUFFER,
1550c0d06caSMauro Carvalho Chehab };
1565b8acdc5SHans Verkuil 
1570c0d06caSMauro Carvalho Chehab enum cx231xx_picture_mask {
1580c0d06caSMauro Carvalho Chehab 	CX231xx_PICTURE_MASK_NONE,
1590c0d06caSMauro Carvalho Chehab 	CX231xx_PICTURE_MASK_I_FRAMES,
1600c0d06caSMauro Carvalho Chehab 	CX231xx_PICTURE_MASK_I_P_FRAMES = 0x3,
1610c0d06caSMauro Carvalho Chehab 	CX231xx_PICTURE_MASK_ALL_FRAMES = 0x7,
1620c0d06caSMauro Carvalho Chehab };
1635b8acdc5SHans Verkuil 
1640c0d06caSMauro Carvalho Chehab enum cx231xx_vbi_mode_bits {
1650c0d06caSMauro Carvalho Chehab 	CX231xx_VBI_BITS_SLICED,
1660c0d06caSMauro Carvalho Chehab 	CX231xx_VBI_BITS_RAW,
1670c0d06caSMauro Carvalho Chehab };
1685b8acdc5SHans Verkuil 
1690c0d06caSMauro Carvalho Chehab enum cx231xx_vbi_insertion_bits {
1700c0d06caSMauro Carvalho Chehab 	CX231xx_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
1710c0d06caSMauro Carvalho Chehab 	CX231xx_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
1720c0d06caSMauro Carvalho Chehab 	CX231xx_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
1730c0d06caSMauro Carvalho Chehab 	CX231xx_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
1740c0d06caSMauro Carvalho Chehab 	CX231xx_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
1750c0d06caSMauro Carvalho Chehab };
1765b8acdc5SHans Verkuil 
1770c0d06caSMauro Carvalho Chehab enum cx231xx_dma_unit {
1780c0d06caSMauro Carvalho Chehab 	CX231xx_DMA_BYTES,
1790c0d06caSMauro Carvalho Chehab 	CX231xx_DMA_FRAMES,
1800c0d06caSMauro Carvalho Chehab };
1815b8acdc5SHans Verkuil 
1820c0d06caSMauro Carvalho Chehab enum cx231xx_dma_transfer_status_bits {
1830c0d06caSMauro Carvalho Chehab 	CX231xx_DMA_TRANSFER_BITS_DONE = 0x01,
1840c0d06caSMauro Carvalho Chehab 	CX231xx_DMA_TRANSFER_BITS_ERROR = 0x04,
1850c0d06caSMauro Carvalho Chehab 	CX231xx_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
1860c0d06caSMauro Carvalho Chehab };
1875b8acdc5SHans Verkuil 
1880c0d06caSMauro Carvalho Chehab enum cx231xx_pause {
1890c0d06caSMauro Carvalho Chehab 	CX231xx_PAUSE_ENCODING,
1900c0d06caSMauro Carvalho Chehab 	CX231xx_RESUME_ENCODING,
1910c0d06caSMauro Carvalho Chehab };
1925b8acdc5SHans Verkuil 
1930c0d06caSMauro Carvalho Chehab enum cx231xx_copyright {
1940c0d06caSMauro Carvalho Chehab 	CX231xx_COPYRIGHT_OFF,
1950c0d06caSMauro Carvalho Chehab 	CX231xx_COPYRIGHT_ON,
1960c0d06caSMauro Carvalho Chehab };
1975b8acdc5SHans Verkuil 
1980c0d06caSMauro Carvalho Chehab enum cx231xx_notification_type {
1990c0d06caSMauro Carvalho Chehab 	CX231xx_NOTIFICATION_REFRESH,
2000c0d06caSMauro Carvalho Chehab };
2015b8acdc5SHans Verkuil 
2020c0d06caSMauro Carvalho Chehab enum cx231xx_notification_status {
2030c0d06caSMauro Carvalho Chehab 	CX231xx_NOTIFICATION_OFF,
2040c0d06caSMauro Carvalho Chehab 	CX231xx_NOTIFICATION_ON,
2050c0d06caSMauro Carvalho Chehab };
2065b8acdc5SHans Verkuil 
2070c0d06caSMauro Carvalho Chehab enum cx231xx_notification_mailbox {
2080c0d06caSMauro Carvalho Chehab 	CX231xx_NOTIFICATION_NO_MAILBOX = -1,
2090c0d06caSMauro Carvalho Chehab };
2105b8acdc5SHans Verkuil 
2110c0d06caSMauro Carvalho Chehab enum cx231xx_field1_lines {
2120c0d06caSMauro Carvalho Chehab 	CX231xx_FIELD1_SAA7114 = 0x00EF, /* 239 */
2130c0d06caSMauro Carvalho Chehab 	CX231xx_FIELD1_SAA7115 = 0x00F0, /* 240 */
2140c0d06caSMauro Carvalho Chehab 	CX231xx_FIELD1_MICRONAS = 0x0105, /* 261 */
2150c0d06caSMauro Carvalho Chehab };
2165b8acdc5SHans Verkuil 
2170c0d06caSMauro Carvalho Chehab enum cx231xx_field2_lines {
2180c0d06caSMauro Carvalho Chehab 	CX231xx_FIELD2_SAA7114 = 0x00EF, /* 239 */
2190c0d06caSMauro Carvalho Chehab 	CX231xx_FIELD2_SAA7115 = 0x00F0, /* 240 */
2200c0d06caSMauro Carvalho Chehab 	CX231xx_FIELD2_MICRONAS = 0x0106, /* 262 */
2210c0d06caSMauro Carvalho Chehab };
2225b8acdc5SHans Verkuil 
2230c0d06caSMauro Carvalho Chehab enum cx231xx_custom_data_type {
2240c0d06caSMauro Carvalho Chehab 	CX231xx_CUSTOM_EXTENSION_USR_DATA,
2250c0d06caSMauro Carvalho Chehab 	CX231xx_CUSTOM_PRIVATE_PACKET,
2260c0d06caSMauro Carvalho Chehab };
2275b8acdc5SHans Verkuil 
2280c0d06caSMauro Carvalho Chehab enum cx231xx_mute {
2290c0d06caSMauro Carvalho Chehab 	CX231xx_UNMUTE,
2300c0d06caSMauro Carvalho Chehab 	CX231xx_MUTE,
2310c0d06caSMauro Carvalho Chehab };
2325b8acdc5SHans Verkuil 
2330c0d06caSMauro Carvalho Chehab enum cx231xx_mute_video_mask {
2340c0d06caSMauro Carvalho Chehab 	CX231xx_MUTE_VIDEO_V_MASK = 0x0000FF00,
2350c0d06caSMauro Carvalho Chehab 	CX231xx_MUTE_VIDEO_U_MASK = 0x00FF0000,
2360c0d06caSMauro Carvalho Chehab 	CX231xx_MUTE_VIDEO_Y_MASK = 0xFF000000,
2370c0d06caSMauro Carvalho Chehab };
2385b8acdc5SHans Verkuil 
2390c0d06caSMauro Carvalho Chehab enum cx231xx_mute_video_shift {
2400c0d06caSMauro Carvalho Chehab 	CX231xx_MUTE_VIDEO_V_SHIFT = 8,
2410c0d06caSMauro Carvalho Chehab 	CX231xx_MUTE_VIDEO_U_SHIFT = 16,
2420c0d06caSMauro Carvalho Chehab 	CX231xx_MUTE_VIDEO_Y_SHIFT = 24,
2430c0d06caSMauro Carvalho Chehab };
2440c0d06caSMauro Carvalho Chehab 
2450c0d06caSMauro Carvalho Chehab /* defines below are from ivtv-driver.h */
2460c0d06caSMauro Carvalho Chehab #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
2470c0d06caSMauro Carvalho Chehab 
2480c0d06caSMauro Carvalho Chehab /* Firmware API commands */
2490c0d06caSMauro Carvalho Chehab #define IVTV_API_STD_TIMEOUT 500
2500c0d06caSMauro Carvalho Chehab 
2510c0d06caSMauro Carvalho Chehab /* Registers */
2520c0d06caSMauro Carvalho Chehab /* IVTV_REG_OFFSET */
2530c0d06caSMauro Carvalho Chehab #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
2540c0d06caSMauro Carvalho Chehab #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
2550c0d06caSMauro Carvalho Chehab #define IVTV_REG_SPU (0x9050)
2560c0d06caSMauro Carvalho Chehab #define IVTV_REG_HW_BLOCKS (0x9054)
2570c0d06caSMauro Carvalho Chehab #define IVTV_REG_VPU (0x9058)
2580c0d06caSMauro Carvalho Chehab #define IVTV_REG_APU (0xA064)
2590c0d06caSMauro Carvalho Chehab 
2600c0d06caSMauro Carvalho Chehab /*
2610c0d06caSMauro Carvalho Chehab  * Bit definitions for MC417_RWD and MC417_OEN registers
2620c0d06caSMauro Carvalho Chehab  *
2630c0d06caSMauro Carvalho Chehab  * bits 31-16
2640c0d06caSMauro Carvalho Chehab  *+-----------+
2650c0d06caSMauro Carvalho Chehab  *| Reserved  |
2660c0d06caSMauro Carvalho Chehab  *|+-----------+
2670c0d06caSMauro Carvalho Chehab  *|  bit 15  bit 14  bit 13 bit 12  bit 11  bit 10  bit 9   bit 8
2680c0d06caSMauro Carvalho Chehab  *|+-------+-------+-------+-------+-------+-------+-------+-------+
2690c0d06caSMauro Carvalho Chehab  *|| MIWR# | MIRD# | MICS# |MIRDY# |MIADDR3|MIADDR2|MIADDR1|MIADDR0|
2700c0d06caSMauro Carvalho Chehab  *|+-------+-------+-------+-------+-------+-------+-------+-------+
2710c0d06caSMauro Carvalho Chehab  *| bit 7   bit 6   bit 5   bit 4   bit 3   bit 2   bit 1   bit 0
2720c0d06caSMauro Carvalho Chehab  *|+-------+-------+-------+-------+-------+-------+-------+-------+
2730c0d06caSMauro Carvalho Chehab  *||MIDATA7|MIDATA6|MIDATA5|MIDATA4|MIDATA3|MIDATA2|MIDATA1|MIDATA0|
2740c0d06caSMauro Carvalho Chehab  *|+-------+-------+-------+-------+-------+-------+-------+-------+
2750c0d06caSMauro Carvalho Chehab  */
2760c0d06caSMauro Carvalho Chehab #define MC417_MIWR	0x8000
2770c0d06caSMauro Carvalho Chehab #define MC417_MIRD	0x4000
2780c0d06caSMauro Carvalho Chehab #define MC417_MICS	0x2000
2790c0d06caSMauro Carvalho Chehab #define MC417_MIRDY	0x1000
2800c0d06caSMauro Carvalho Chehab #define MC417_MIADDR	0x0F00
2810c0d06caSMauro Carvalho Chehab #define MC417_MIDATA	0x00FF
2820c0d06caSMauro Carvalho Chehab 
2830c0d06caSMauro Carvalho Chehab 
2840c0d06caSMauro Carvalho Chehab /* Bit definitions for MC417_CTL register ****
2850c0d06caSMauro Carvalho Chehab  *bits 31-6   bits 5-4   bit 3    bits 2-1       Bit 0
2860c0d06caSMauro Carvalho Chehab  *+--------+-------------+--------+--------------+------------+
2870c0d06caSMauro Carvalho Chehab  *|Reserved|MC417_SPD_CTL|Reserved|MC417_GPIO_SEL|UART_GPIO_EN|
2880c0d06caSMauro Carvalho Chehab  *+--------+-------------+--------+--------------+------------+
2890c0d06caSMauro Carvalho Chehab  */
2900c0d06caSMauro Carvalho Chehab #define MC417_SPD_CTL(x)	(((x) << 4) & 0x00000030)
2910c0d06caSMauro Carvalho Chehab #define MC417_GPIO_SEL(x)	(((x) << 1) & 0x00000006)
2920c0d06caSMauro Carvalho Chehab #define MC417_UART_GPIO_EN	0x00000001
2930c0d06caSMauro Carvalho Chehab 
2940c0d06caSMauro Carvalho Chehab /* Values for speed control */
2950c0d06caSMauro Carvalho Chehab #define MC417_SPD_CTL_SLOW	0x1
2960c0d06caSMauro Carvalho Chehab #define MC417_SPD_CTL_MEDIUM	0x0
2970c0d06caSMauro Carvalho Chehab #define MC417_SPD_CTL_FAST	0x3     /* b'1x, but we use b'11 */
2980c0d06caSMauro Carvalho Chehab 
2990c0d06caSMauro Carvalho Chehab /* Values for GPIO select */
3000c0d06caSMauro Carvalho Chehab #define MC417_GPIO_SEL_GPIO3	0x3
3010c0d06caSMauro Carvalho Chehab #define MC417_GPIO_SEL_GPIO2	0x2
3020c0d06caSMauro Carvalho Chehab #define MC417_GPIO_SEL_GPIO1	0x1
3030c0d06caSMauro Carvalho Chehab #define MC417_GPIO_SEL_GPIO0	0x0
3040c0d06caSMauro Carvalho Chehab 
3050c0d06caSMauro Carvalho Chehab 
3060c0d06caSMauro Carvalho Chehab #define CX23417_GPIO_MASK 0xFC0003FF
3075b8acdc5SHans Verkuil 
set_itvc_reg(struct cx231xx * dev,u32 gpio_direction,u32 value)3085b8acdc5SHans Verkuil static int set_itvc_reg(struct cx231xx *dev, u32 gpio_direction, u32 value)
3090c0d06caSMauro Carvalho Chehab {
3100c0d06caSMauro Carvalho Chehab 	int status = 0;
3110c0d06caSMauro Carvalho Chehab 	u32 _gpio_direction = 0;
3120c0d06caSMauro Carvalho Chehab 
3130c0d06caSMauro Carvalho Chehab 	_gpio_direction = _gpio_direction & CX23417_GPIO_MASK;
3140c0d06caSMauro Carvalho Chehab 	_gpio_direction = _gpio_direction | gpio_direction;
3150c0d06caSMauro Carvalho Chehab 	status = cx231xx_send_gpio_cmd(dev, _gpio_direction,
3160c0d06caSMauro Carvalho Chehab 			 (u8 *)&value, 4, 0, 0);
3170c0d06caSMauro Carvalho Chehab 	return status;
3180c0d06caSMauro Carvalho Chehab }
3195b8acdc5SHans Verkuil 
get_itvc_reg(struct cx231xx * dev,u32 gpio_direction,u32 * val_ptr)3205b8acdc5SHans Verkuil static int get_itvc_reg(struct cx231xx *dev, u32 gpio_direction, u32 *val_ptr)
3210c0d06caSMauro Carvalho Chehab {
3220c0d06caSMauro Carvalho Chehab 	int status = 0;
3230c0d06caSMauro Carvalho Chehab 	u32 _gpio_direction = 0;
3240c0d06caSMauro Carvalho Chehab 
3250c0d06caSMauro Carvalho Chehab 	_gpio_direction = _gpio_direction & CX23417_GPIO_MASK;
3260c0d06caSMauro Carvalho Chehab 	_gpio_direction = _gpio_direction | gpio_direction;
3270c0d06caSMauro Carvalho Chehab 
3280c0d06caSMauro Carvalho Chehab 	status = cx231xx_send_gpio_cmd(dev, _gpio_direction,
3295b8acdc5SHans Verkuil 		 (u8 *)val_ptr, 4, 0, 1);
3300c0d06caSMauro Carvalho Chehab 	return status;
3310c0d06caSMauro Carvalho Chehab }
3320c0d06caSMauro Carvalho Chehab 
wait_for_mci_complete(struct cx231xx * dev)3335b8acdc5SHans Verkuil static int wait_for_mci_complete(struct cx231xx *dev)
3340c0d06caSMauro Carvalho Chehab {
3350c0d06caSMauro Carvalho Chehab 	u32 gpio;
3365b8acdc5SHans Verkuil 	u32 gpio_direction = 0;
3370c0d06caSMauro Carvalho Chehab 	u8 count = 0;
3385b8acdc5SHans Verkuil 	get_itvc_reg(dev, gpio_direction, &gpio);
3390c0d06caSMauro Carvalho Chehab 
3400c0d06caSMauro Carvalho Chehab 	while (!(gpio&0x020000)) {
3410c0d06caSMauro Carvalho Chehab 		msleep(10);
3420c0d06caSMauro Carvalho Chehab 
3435b8acdc5SHans Verkuil 		get_itvc_reg(dev, gpio_direction, &gpio);
3440c0d06caSMauro Carvalho Chehab 
3450c0d06caSMauro Carvalho Chehab 		if (count++ > 100) {
3460c0d06caSMauro Carvalho Chehab 			dprintk(3, "ERROR: Timeout - gpio=%x\n", gpio);
347ccc5429fSMauro Carvalho Chehab 			return -EIO;
3480c0d06caSMauro Carvalho Chehab 		}
3490c0d06caSMauro Carvalho Chehab 	}
3500c0d06caSMauro Carvalho Chehab 	return 0;
3510c0d06caSMauro Carvalho Chehab }
3520c0d06caSMauro Carvalho Chehab 
mc417_register_write(struct cx231xx * dev,u16 address,u32 value)3530c0d06caSMauro Carvalho Chehab static int mc417_register_write(struct cx231xx *dev, u16 address, u32 value)
3540c0d06caSMauro Carvalho Chehab {
3550c0d06caSMauro Carvalho Chehab 	u32 temp;
3560c0d06caSMauro Carvalho Chehab 	int status = 0;
3570c0d06caSMauro Carvalho Chehab 
3580c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_DATA_BYTE0 | ((value & 0x000000FF) << 8);
3590c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
3605b8acdc5SHans Verkuil 	status = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3610c0d06caSMauro Carvalho Chehab 	if (status < 0)
3620c0d06caSMauro Carvalho Chehab 		return status;
3635b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
3645b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3650c0d06caSMauro Carvalho Chehab 
3660c0d06caSMauro Carvalho Chehab 	/*write data byte 1;*/
3670c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_DATA_BYTE1 | (value & 0x0000FF00);
3680c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
3695b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3705b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
3715b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3720c0d06caSMauro Carvalho Chehab 
3730c0d06caSMauro Carvalho Chehab 	/*write data byte 2;*/
3740c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
3750c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
3765b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3775b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
3785b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3790c0d06caSMauro Carvalho Chehab 
3800c0d06caSMauro Carvalho Chehab 	/*write data byte 3;*/
3810c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
3820c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
3835b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3845b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
3855b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3860c0d06caSMauro Carvalho Chehab 
3870c0d06caSMauro Carvalho Chehab 	/*write address byte 0;*/
3880c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE0 | ((address & 0x000000FF) << 8);
3890c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
3905b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3915b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
3925b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3930c0d06caSMauro Carvalho Chehab 
3940c0d06caSMauro Carvalho Chehab 	/*write address byte 1;*/
3950c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE1 | (address & 0x0000FF00);
3960c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
3975b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
3985b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
3995b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4000c0d06caSMauro Carvalho Chehab 
4010c0d06caSMauro Carvalho Chehab 	/*Write that the mode is write.*/
4020c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_MODE | MCI_MODE_REGISTER_WRITE;
4030c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
4045b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4055b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
4065b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4070c0d06caSMauro Carvalho Chehab 
4085b8acdc5SHans Verkuil 	return wait_for_mci_complete(dev);
4090c0d06caSMauro Carvalho Chehab }
4100c0d06caSMauro Carvalho Chehab 
mc417_register_read(struct cx231xx * dev,u16 address,u32 * value)4110c0d06caSMauro Carvalho Chehab static int mc417_register_read(struct cx231xx *dev, u16 address, u32 *value)
4120c0d06caSMauro Carvalho Chehab {
4130c0d06caSMauro Carvalho Chehab 	/*write address byte 0;*/
4140c0d06caSMauro Carvalho Chehab 	u32 temp;
4150c0d06caSMauro Carvalho Chehab 	u32 return_value = 0;
4160c0d06caSMauro Carvalho Chehab 	int ret = 0;
4170c0d06caSMauro Carvalho Chehab 
4180c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
4190c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
4205b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4210c0d06caSMauro Carvalho Chehab 	temp = temp | ((0x05) << 10);
4225b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4230c0d06caSMauro Carvalho Chehab 
4240c0d06caSMauro Carvalho Chehab 	/*write address byte 1;*/
4250c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE1 | (address & 0xFF00);
4260c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
4275b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4280c0d06caSMauro Carvalho Chehab 	temp = temp | ((0x05) << 10);
4295b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4300c0d06caSMauro Carvalho Chehab 
4310c0d06caSMauro Carvalho Chehab 	/*write that the mode is read;*/
4320c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_REGISTER_MODE | MCI_MODE_REGISTER_READ;
4330c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
4345b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4350c0d06caSMauro Carvalho Chehab 	temp = temp | ((0x05) << 10);
4365b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4370c0d06caSMauro Carvalho Chehab 
4380c0d06caSMauro Carvalho Chehab 	/*wait for the MIRDY line to be asserted ,
4390c0d06caSMauro Carvalho Chehab 	signalling that the read is done;*/
4405b8acdc5SHans Verkuil 	ret = wait_for_mci_complete(dev);
4410c0d06caSMauro Carvalho Chehab 
4420c0d06caSMauro Carvalho Chehab 	/*switch the DATA- GPIO to input mode;*/
4430c0d06caSMauro Carvalho Chehab 
4440c0d06caSMauro Carvalho Chehab 	/*Read data byte 0;*/
4450c0d06caSMauro Carvalho Chehab 	temp = (0x82 | MCI_REGISTER_DATA_BYTE0) << 10;
4465b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
4470c0d06caSMauro Carvalho Chehab 	temp = ((0x81 | MCI_REGISTER_DATA_BYTE0) << 10);
4485b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
4495b8acdc5SHans Verkuil 	get_itvc_reg(dev, ITVC_READ_DIR, &temp);
4500c0d06caSMauro Carvalho Chehab 	return_value |= ((temp & 0x03FC0000) >> 18);
4515b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
4520c0d06caSMauro Carvalho Chehab 
4530c0d06caSMauro Carvalho Chehab 	/* Read data byte 1;*/
4540c0d06caSMauro Carvalho Chehab 	temp = (0x82 | MCI_REGISTER_DATA_BYTE1) << 10;
4555b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
4560c0d06caSMauro Carvalho Chehab 	temp = ((0x81 | MCI_REGISTER_DATA_BYTE1) << 10);
4575b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
4585b8acdc5SHans Verkuil 	get_itvc_reg(dev, ITVC_READ_DIR, &temp);
4590c0d06caSMauro Carvalho Chehab 
4600c0d06caSMauro Carvalho Chehab 	return_value |= ((temp & 0x03FC0000) >> 10);
4615b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
4620c0d06caSMauro Carvalho Chehab 
4630c0d06caSMauro Carvalho Chehab 	/*Read data byte 2;*/
4640c0d06caSMauro Carvalho Chehab 	temp = (0x82 | MCI_REGISTER_DATA_BYTE2) << 10;
4655b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
4660c0d06caSMauro Carvalho Chehab 	temp = ((0x81 | MCI_REGISTER_DATA_BYTE2) << 10);
4675b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
4685b8acdc5SHans Verkuil 	get_itvc_reg(dev, ITVC_READ_DIR, &temp);
4690c0d06caSMauro Carvalho Chehab 	return_value |= ((temp & 0x03FC0000) >> 2);
4705b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
4710c0d06caSMauro Carvalho Chehab 
4720c0d06caSMauro Carvalho Chehab 	/*Read data byte 3;*/
4730c0d06caSMauro Carvalho Chehab 	temp = (0x82 | MCI_REGISTER_DATA_BYTE3) << 10;
4745b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
4750c0d06caSMauro Carvalho Chehab 	temp = ((0x81 | MCI_REGISTER_DATA_BYTE3) << 10);
4765b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
4775b8acdc5SHans Verkuil 	get_itvc_reg(dev, ITVC_READ_DIR, &temp);
4780c0d06caSMauro Carvalho Chehab 	return_value |= ((temp & 0x03FC0000) << 6);
4795b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
4800c0d06caSMauro Carvalho Chehab 
4810c0d06caSMauro Carvalho Chehab 	*value  = return_value;
4820c0d06caSMauro Carvalho Chehab 	return ret;
4830c0d06caSMauro Carvalho Chehab }
4840c0d06caSMauro Carvalho Chehab 
mc417_memory_write(struct cx231xx * dev,u32 address,u32 value)4850c0d06caSMauro Carvalho Chehab static int mc417_memory_write(struct cx231xx *dev, u32 address, u32 value)
4860c0d06caSMauro Carvalho Chehab {
4870c0d06caSMauro Carvalho Chehab 	/*write data byte 0;*/
4880c0d06caSMauro Carvalho Chehab 
4890c0d06caSMauro Carvalho Chehab 	u32 temp;
4900c0d06caSMauro Carvalho Chehab 	int ret = 0;
4910c0d06caSMauro Carvalho Chehab 
4920c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_DATA_BYTE0 | ((value & 0x000000FF) << 8);
4930c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
4945b8acdc5SHans Verkuil 	ret = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4950c0d06caSMauro Carvalho Chehab 	if (ret < 0)
4960c0d06caSMauro Carvalho Chehab 		return ret;
4975b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
4985b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
4990c0d06caSMauro Carvalho Chehab 
5000c0d06caSMauro Carvalho Chehab 	/*write data byte 1;*/
5010c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_DATA_BYTE1 | (value & 0x0000FF00);
5020c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5035b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5045b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5055b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5060c0d06caSMauro Carvalho Chehab 
5070c0d06caSMauro Carvalho Chehab 	/*write data byte 2;*/
5080c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
5090c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5105b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5115b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5125b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5130c0d06caSMauro Carvalho Chehab 
5140c0d06caSMauro Carvalho Chehab 	/*write data byte 3;*/
5150c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
5160c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5175b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5185b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5195b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5200c0d06caSMauro Carvalho Chehab 
5210c0d06caSMauro Carvalho Chehab 	/* write address byte 2;*/
5220c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_WRITE |
5230c0d06caSMauro Carvalho Chehab 		((address & 0x003F0000) >> 8);
5240c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5255b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5265b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5275b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5280c0d06caSMauro Carvalho Chehab 
5290c0d06caSMauro Carvalho Chehab 	/* write address byte 1;*/
5300c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
5310c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5325b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5335b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5345b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5350c0d06caSMauro Carvalho Chehab 
5360c0d06caSMauro Carvalho Chehab 	/* write address byte 0;*/
5370c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
5380c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5395b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5405b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5415b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5420c0d06caSMauro Carvalho Chehab 
5430c0d06caSMauro Carvalho Chehab 	/*wait for MIRDY line;*/
5445b8acdc5SHans Verkuil 	wait_for_mci_complete(dev);
5450c0d06caSMauro Carvalho Chehab 
5460c0d06caSMauro Carvalho Chehab 	return 0;
5470c0d06caSMauro Carvalho Chehab }
5480c0d06caSMauro Carvalho Chehab 
mc417_memory_read(struct cx231xx * dev,u32 address,u32 * value)5490c0d06caSMauro Carvalho Chehab static int mc417_memory_read(struct cx231xx *dev, u32 address, u32 *value)
5500c0d06caSMauro Carvalho Chehab {
5510c0d06caSMauro Carvalho Chehab 	u32 temp = 0;
5520c0d06caSMauro Carvalho Chehab 	u32 return_value = 0;
5530c0d06caSMauro Carvalho Chehab 	int ret = 0;
5540c0d06caSMauro Carvalho Chehab 
5550c0d06caSMauro Carvalho Chehab 	/*write address byte 2;*/
5560c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_READ |
5570c0d06caSMauro Carvalho Chehab 		((address & 0x003F0000) >> 8);
5580c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5595b8acdc5SHans Verkuil 	ret = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5600c0d06caSMauro Carvalho Chehab 	if (ret < 0)
5610c0d06caSMauro Carvalho Chehab 		return ret;
5625b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5635b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5640c0d06caSMauro Carvalho Chehab 
5650c0d06caSMauro Carvalho Chehab 	/*write address byte 1*/
5660c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
5670c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5685b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5695b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5705b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5710c0d06caSMauro Carvalho Chehab 
5720c0d06caSMauro Carvalho Chehab 	/*write address byte 0*/
5730c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
5740c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
5755b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5765b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
5775b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
5780c0d06caSMauro Carvalho Chehab 
5790c0d06caSMauro Carvalho Chehab 	/*Wait for MIRDY line*/
5805b8acdc5SHans Verkuil 	ret = wait_for_mci_complete(dev);
5810c0d06caSMauro Carvalho Chehab 
5820c0d06caSMauro Carvalho Chehab 
5830c0d06caSMauro Carvalho Chehab 	/*Read data byte 3;*/
5840c0d06caSMauro Carvalho Chehab 	temp = (0x82 | MCI_MEMORY_DATA_BYTE3) << 10;
5855b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
5860c0d06caSMauro Carvalho Chehab 	temp = ((0x81 | MCI_MEMORY_DATA_BYTE3) << 10);
5875b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
5885b8acdc5SHans Verkuil 	get_itvc_reg(dev, ITVC_READ_DIR, &temp);
5890c0d06caSMauro Carvalho Chehab 	return_value |= ((temp & 0x03FC0000) << 6);
5905b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
5910c0d06caSMauro Carvalho Chehab 
5920c0d06caSMauro Carvalho Chehab 	/*Read data byte 2;*/
5930c0d06caSMauro Carvalho Chehab 	temp = (0x82 | MCI_MEMORY_DATA_BYTE2) << 10;
5945b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
5950c0d06caSMauro Carvalho Chehab 	temp = ((0x81 | MCI_MEMORY_DATA_BYTE2) << 10);
5965b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
5975b8acdc5SHans Verkuil 	get_itvc_reg(dev, ITVC_READ_DIR, &temp);
5980c0d06caSMauro Carvalho Chehab 	return_value |= ((temp & 0x03FC0000) >> 2);
5995b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
6000c0d06caSMauro Carvalho Chehab 
6010c0d06caSMauro Carvalho Chehab 	/* Read data byte 1;*/
6020c0d06caSMauro Carvalho Chehab 	temp = (0x82 | MCI_MEMORY_DATA_BYTE1) << 10;
6035b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
6040c0d06caSMauro Carvalho Chehab 	temp = ((0x81 | MCI_MEMORY_DATA_BYTE1) << 10);
6055b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
6065b8acdc5SHans Verkuil 	get_itvc_reg(dev, ITVC_READ_DIR, &temp);
6070c0d06caSMauro Carvalho Chehab 	return_value |= ((temp & 0x03FC0000) >> 10);
6085b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
6090c0d06caSMauro Carvalho Chehab 
6100c0d06caSMauro Carvalho Chehab 	/*Read data byte 0;*/
6110c0d06caSMauro Carvalho Chehab 	temp = (0x82 | MCI_MEMORY_DATA_BYTE0) << 10;
6125b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
6130c0d06caSMauro Carvalho Chehab 	temp = ((0x81 | MCI_MEMORY_DATA_BYTE0) << 10);
6145b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, temp);
6155b8acdc5SHans Verkuil 	get_itvc_reg(dev, ITVC_READ_DIR, &temp);
6160c0d06caSMauro Carvalho Chehab 	return_value |= ((temp & 0x03FC0000) >> 18);
6175b8acdc5SHans Verkuil 	set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
6180c0d06caSMauro Carvalho Chehab 
6190c0d06caSMauro Carvalho Chehab 	*value  = return_value;
6200c0d06caSMauro Carvalho Chehab 	return ret;
6210c0d06caSMauro Carvalho Chehab }
6220c0d06caSMauro Carvalho Chehab 
6230c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
6240c0d06caSMauro Carvalho Chehab 
6250c0d06caSMauro Carvalho Chehab /* MPEG encoder API */
cmd_to_str(int cmd)6260c0d06caSMauro Carvalho Chehab static char *cmd_to_str(int cmd)
6270c0d06caSMauro Carvalho Chehab {
6280c0d06caSMauro Carvalho Chehab 	switch (cmd) {
6290c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_PING_FW:
6300c0d06caSMauro Carvalho Chehab 		return "PING_FW";
6310c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_START_CAPTURE:
6320c0d06caSMauro Carvalho Chehab 		return "START_CAPTURE";
6330c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_STOP_CAPTURE:
6340c0d06caSMauro Carvalho Chehab 		return "STOP_CAPTURE";
6350c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_AUDIO_ID:
6360c0d06caSMauro Carvalho Chehab 		return "SET_AUDIO_ID";
6370c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_VIDEO_ID:
6380c0d06caSMauro Carvalho Chehab 		return "SET_VIDEO_ID";
6390c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_PCR_ID:
6400c0d06caSMauro Carvalho Chehab 		return "SET_PCR_PID";
6410c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_FRAME_RATE:
6420c0d06caSMauro Carvalho Chehab 		return "SET_FRAME_RATE";
6430c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_FRAME_SIZE:
6440c0d06caSMauro Carvalho Chehab 		return "SET_FRAME_SIZE";
6450c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_BIT_RATE:
6460c0d06caSMauro Carvalho Chehab 		return "SET_BIT_RATE";
6470c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_GOP_PROPERTIES:
6480c0d06caSMauro Carvalho Chehab 		return "SET_GOP_PROPERTIES";
6490c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_ASPECT_RATIO:
6500c0d06caSMauro Carvalho Chehab 		return "SET_ASPECT_RATIO";
6510c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_DNR_FILTER_MODE:
6520c0d06caSMauro Carvalho Chehab 		return "SET_DNR_FILTER_PROPS";
6530c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_DNR_FILTER_PROPS:
6540c0d06caSMauro Carvalho Chehab 		return "SET_DNR_FILTER_PROPS";
6550c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_CORING_LEVELS:
6560c0d06caSMauro Carvalho Chehab 		return "SET_CORING_LEVELS";
6570c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
6580c0d06caSMauro Carvalho Chehab 		return "SET_SPATIAL_FILTER_TYPE";
6590c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_VBI_LINE:
6600c0d06caSMauro Carvalho Chehab 		return "SET_VBI_LINE";
6610c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_STREAM_TYPE:
6620c0d06caSMauro Carvalho Chehab 		return "SET_STREAM_TYPE";
6630c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_OUTPUT_PORT:
6640c0d06caSMauro Carvalho Chehab 		return "SET_OUTPUT_PORT";
6650c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_AUDIO_PROPERTIES:
6660c0d06caSMauro Carvalho Chehab 		return "SET_AUDIO_PROPERTIES";
6670c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_HALT_FW:
6680c0d06caSMauro Carvalho Chehab 		return "HALT_FW";
6690c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_GET_VERSION:
6700c0d06caSMauro Carvalho Chehab 		return "GET_VERSION";
6710c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_GOP_CLOSURE:
6720c0d06caSMauro Carvalho Chehab 		return "SET_GOP_CLOSURE";
6730c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_GET_SEQ_END:
6740c0d06caSMauro Carvalho Chehab 		return "GET_SEQ_END";
6750c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_PGM_INDEX_INFO:
6760c0d06caSMauro Carvalho Chehab 		return "SET_PGM_INDEX_INFO";
6770c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_VBI_CONFIG:
6780c0d06caSMauro Carvalho Chehab 		return "SET_VBI_CONFIG";
6790c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_DMA_BLOCK_SIZE:
6800c0d06caSMauro Carvalho Chehab 		return "SET_DMA_BLOCK_SIZE";
6810c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_GET_PREV_DMA_INFO_MB_10:
6820c0d06caSMauro Carvalho Chehab 		return "GET_PREV_DMA_INFO_MB_10";
6830c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_GET_PREV_DMA_INFO_MB_9:
6840c0d06caSMauro Carvalho Chehab 		return "GET_PREV_DMA_INFO_MB_9";
6850c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SCHED_DMA_TO_HOST:
6860c0d06caSMauro Carvalho Chehab 		return "SCHED_DMA_TO_HOST";
6870c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_INITIALIZE_INPUT:
6880c0d06caSMauro Carvalho Chehab 		return "INITIALIZE_INPUT";
6890c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_FRAME_DROP_RATE:
6900c0d06caSMauro Carvalho Chehab 		return "SET_FRAME_DROP_RATE";
6910c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_PAUSE_ENCODER:
6920c0d06caSMauro Carvalho Chehab 		return "PAUSE_ENCODER";
6930c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_REFRESH_INPUT:
6940c0d06caSMauro Carvalho Chehab 		return "REFRESH_INPUT";
6950c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_COPYRIGHT:
6960c0d06caSMauro Carvalho Chehab 		return "SET_COPYRIGHT";
6970c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_EVENT_NOTIFICATION:
6980c0d06caSMauro Carvalho Chehab 		return "SET_EVENT_NOTIFICATION";
6990c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_NUM_VSYNC_LINES:
7000c0d06caSMauro Carvalho Chehab 		return "SET_NUM_VSYNC_LINES";
7010c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_SET_PLACEHOLDER:
7020c0d06caSMauro Carvalho Chehab 		return "SET_PLACEHOLDER";
7030c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_MUTE_VIDEO:
7040c0d06caSMauro Carvalho Chehab 		return "MUTE_VIDEO";
7050c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_MUTE_AUDIO:
7060c0d06caSMauro Carvalho Chehab 		return "MUTE_AUDIO";
7070c0d06caSMauro Carvalho Chehab 	case CX2341X_ENC_MISC:
7080c0d06caSMauro Carvalho Chehab 		return "MISC";
7090c0d06caSMauro Carvalho Chehab 	default:
7100c0d06caSMauro Carvalho Chehab 		return "UNKNOWN";
7110c0d06caSMauro Carvalho Chehab 	}
7120c0d06caSMauro Carvalho Chehab }
7130c0d06caSMauro Carvalho Chehab 
cx231xx_mbox_func(void * priv,u32 command,int in,int out,u32 data[CX2341X_MBOX_MAX_DATA])7145b8acdc5SHans Verkuil static int cx231xx_mbox_func(void *priv, u32 command, int in, int out,
7150c0d06caSMauro Carvalho Chehab 			     u32 data[CX2341X_MBOX_MAX_DATA])
7160c0d06caSMauro Carvalho Chehab {
7170c0d06caSMauro Carvalho Chehab 	struct cx231xx *dev = priv;
7180c0d06caSMauro Carvalho Chehab 	unsigned long timeout;
7190c0d06caSMauro Carvalho Chehab 	u32 value, flag, retval = 0;
7200c0d06caSMauro Carvalho Chehab 	int i;
7210c0d06caSMauro Carvalho Chehab 
7220c0d06caSMauro Carvalho Chehab 	dprintk(3, "%s: command(0x%X) = %s\n", __func__, command,
7230c0d06caSMauro Carvalho Chehab 		cmd_to_str(command));
7240c0d06caSMauro Carvalho Chehab 
7250c0d06caSMauro Carvalho Chehab 	/* this may not be 100% safe if we can't read any memory location
7260c0d06caSMauro Carvalho Chehab 	   without side effects */
7270c0d06caSMauro Carvalho Chehab 	mc417_memory_read(dev, dev->cx23417_mailbox - 4, &value);
7280c0d06caSMauro Carvalho Chehab 	if (value != 0x12345678) {
7295b8acdc5SHans Verkuil 		dprintk(3, "Firmware and/or mailbox pointer not initialized or corrupted, signature = 0x%x, cmd = %s\n",
7305b8acdc5SHans Verkuil 			value, cmd_to_str(command));
73188b6ffedSHans Verkuil 		return -EIO;
7320c0d06caSMauro Carvalho Chehab 	}
7330c0d06caSMauro Carvalho Chehab 
7340c0d06caSMauro Carvalho Chehab 	/* This read looks at 32 bits, but flag is only 8 bits.
7350c0d06caSMauro Carvalho Chehab 	 * Seems we also bail if CMD or TIMEOUT bytes are set???
7360c0d06caSMauro Carvalho Chehab 	 */
7370c0d06caSMauro Carvalho Chehab 	mc417_memory_read(dev, dev->cx23417_mailbox, &flag);
7380c0d06caSMauro Carvalho Chehab 	if (flag) {
7395b8acdc5SHans Verkuil 		dprintk(3, "ERROR: Mailbox appears to be in use (%x), cmd = %s\n",
7405b8acdc5SHans Verkuil 				flag, cmd_to_str(command));
74188b6ffedSHans Verkuil 		return -EBUSY;
7420c0d06caSMauro Carvalho Chehab 	}
7430c0d06caSMauro Carvalho Chehab 
7440c0d06caSMauro Carvalho Chehab 	flag |= 1; /* tell 'em we're working on it */
7450c0d06caSMauro Carvalho Chehab 	mc417_memory_write(dev, dev->cx23417_mailbox, flag);
7460c0d06caSMauro Carvalho Chehab 
7470c0d06caSMauro Carvalho Chehab 	/* write command + args + fill remaining with zeros */
7480c0d06caSMauro Carvalho Chehab 	/* command code */
7490c0d06caSMauro Carvalho Chehab 	mc417_memory_write(dev, dev->cx23417_mailbox + 1, command);
7500c0d06caSMauro Carvalho Chehab 	mc417_memory_write(dev, dev->cx23417_mailbox + 3,
7510c0d06caSMauro Carvalho Chehab 		IVTV_API_STD_TIMEOUT); /* timeout */
7520c0d06caSMauro Carvalho Chehab 	for (i = 0; i < in; i++) {
7530c0d06caSMauro Carvalho Chehab 		mc417_memory_write(dev, dev->cx23417_mailbox + 4 + i, data[i]);
7540c0d06caSMauro Carvalho Chehab 		dprintk(3, "API Input %d = %d\n", i, data[i]);
7550c0d06caSMauro Carvalho Chehab 	}
7560c0d06caSMauro Carvalho Chehab 	for (; i < CX2341X_MBOX_MAX_DATA; i++)
7570c0d06caSMauro Carvalho Chehab 		mc417_memory_write(dev, dev->cx23417_mailbox + 4 + i, 0);
7580c0d06caSMauro Carvalho Chehab 
7590c0d06caSMauro Carvalho Chehab 	flag |= 3; /* tell 'em we're done writing */
7600c0d06caSMauro Carvalho Chehab 	mc417_memory_write(dev, dev->cx23417_mailbox, flag);
7610c0d06caSMauro Carvalho Chehab 
7620c0d06caSMauro Carvalho Chehab 	/* wait for firmware to handle the API command */
7630c0d06caSMauro Carvalho Chehab 	timeout = jiffies + msecs_to_jiffies(10);
7640c0d06caSMauro Carvalho Chehab 	for (;;) {
7650c0d06caSMauro Carvalho Chehab 		mc417_memory_read(dev, dev->cx23417_mailbox, &flag);
7660c0d06caSMauro Carvalho Chehab 		if (0 != (flag & 4))
7670c0d06caSMauro Carvalho Chehab 			break;
7680c0d06caSMauro Carvalho Chehab 		if (time_after(jiffies, timeout)) {
7690c0d06caSMauro Carvalho Chehab 			dprintk(3, "ERROR: API Mailbox timeout\n");
77088b6ffedSHans Verkuil 			return -EIO;
7710c0d06caSMauro Carvalho Chehab 		}
7720c0d06caSMauro Carvalho Chehab 		udelay(10);
7730c0d06caSMauro Carvalho Chehab 	}
7740c0d06caSMauro Carvalho Chehab 
7750c0d06caSMauro Carvalho Chehab 	/* read output values */
7760c0d06caSMauro Carvalho Chehab 	for (i = 0; i < out; i++) {
7770c0d06caSMauro Carvalho Chehab 		mc417_memory_read(dev, dev->cx23417_mailbox + 4 + i, data + i);
7780c0d06caSMauro Carvalho Chehab 		dprintk(3, "API Output %d = %d\n", i, data[i]);
7790c0d06caSMauro Carvalho Chehab 	}
7800c0d06caSMauro Carvalho Chehab 
7810c0d06caSMauro Carvalho Chehab 	mc417_memory_read(dev, dev->cx23417_mailbox + 2, &retval);
7820c0d06caSMauro Carvalho Chehab 	dprintk(3, "API result = %d\n", retval);
7830c0d06caSMauro Carvalho Chehab 
7840c0d06caSMauro Carvalho Chehab 	flag = 0;
7850c0d06caSMauro Carvalho Chehab 	mc417_memory_write(dev, dev->cx23417_mailbox, flag);
7860c0d06caSMauro Carvalho Chehab 
78788b6ffedSHans Verkuil 	return 0;
7880c0d06caSMauro Carvalho Chehab }
7890c0d06caSMauro Carvalho Chehab 
7900c0d06caSMauro Carvalho Chehab /* We don't need to call the API often, so using just one
7910c0d06caSMauro Carvalho Chehab  * mailbox will probably suffice
7920c0d06caSMauro Carvalho Chehab  */
cx231xx_api_cmd(struct cx231xx * dev,u32 command,u32 inputcnt,u32 outputcnt,...)7935b8acdc5SHans Verkuil static int cx231xx_api_cmd(struct cx231xx *dev, u32 command,
7945b8acdc5SHans Verkuil 		u32 inputcnt, u32 outputcnt, ...)
7950c0d06caSMauro Carvalho Chehab {
7960c0d06caSMauro Carvalho Chehab 	u32 data[CX2341X_MBOX_MAX_DATA];
7970c0d06caSMauro Carvalho Chehab 	va_list vargs;
7980c0d06caSMauro Carvalho Chehab 	int i, err;
7990c0d06caSMauro Carvalho Chehab 
8000c0d06caSMauro Carvalho Chehab 	dprintk(3, "%s() cmds = 0x%08x\n", __func__, command);
8010c0d06caSMauro Carvalho Chehab 
8020c0d06caSMauro Carvalho Chehab 	va_start(vargs, outputcnt);
8030c0d06caSMauro Carvalho Chehab 	for (i = 0; i < inputcnt; i++)
8040c0d06caSMauro Carvalho Chehab 		data[i] = va_arg(vargs, int);
8050c0d06caSMauro Carvalho Chehab 
8060c0d06caSMauro Carvalho Chehab 	err = cx231xx_mbox_func(dev, command, inputcnt, outputcnt, data);
8070c0d06caSMauro Carvalho Chehab 	for (i = 0; i < outputcnt; i++) {
8080c0d06caSMauro Carvalho Chehab 		int *vptr = va_arg(vargs, int *);
8090c0d06caSMauro Carvalho Chehab 		*vptr = data[i];
8100c0d06caSMauro Carvalho Chehab 	}
8110c0d06caSMauro Carvalho Chehab 	va_end(vargs);
8120c0d06caSMauro Carvalho Chehab 
8130c0d06caSMauro Carvalho Chehab 	return err;
8140c0d06caSMauro Carvalho Chehab }
8150c0d06caSMauro Carvalho Chehab 
81688b6ffedSHans Verkuil 
cx231xx_find_mailbox(struct cx231xx * dev)8170c0d06caSMauro Carvalho Chehab static int cx231xx_find_mailbox(struct cx231xx *dev)
8180c0d06caSMauro Carvalho Chehab {
8190c0d06caSMauro Carvalho Chehab 	u32 signature[4] = {
8200c0d06caSMauro Carvalho Chehab 		0x12345678, 0x34567812, 0x56781234, 0x78123456
8210c0d06caSMauro Carvalho Chehab 	};
8220c0d06caSMauro Carvalho Chehab 	int signaturecnt = 0;
8230c0d06caSMauro Carvalho Chehab 	u32 value;
8240c0d06caSMauro Carvalho Chehab 	int i;
8250c0d06caSMauro Carvalho Chehab 	int ret = 0;
8260c0d06caSMauro Carvalho Chehab 
8270c0d06caSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
8280c0d06caSMauro Carvalho Chehab 
8290c0d06caSMauro Carvalho Chehab 	for (i = 0; i < 0x100; i++) {/*CX231xx_FIRM_IMAGE_SIZE*/
8300c0d06caSMauro Carvalho Chehab 		ret = mc417_memory_read(dev, i, &value);
8310c0d06caSMauro Carvalho Chehab 		if (ret < 0)
8320c0d06caSMauro Carvalho Chehab 			return ret;
8330c0d06caSMauro Carvalho Chehab 		if (value == signature[signaturecnt])
8340c0d06caSMauro Carvalho Chehab 			signaturecnt++;
8350c0d06caSMauro Carvalho Chehab 		else
8360c0d06caSMauro Carvalho Chehab 			signaturecnt = 0;
8370c0d06caSMauro Carvalho Chehab 		if (4 == signaturecnt) {
8380c0d06caSMauro Carvalho Chehab 			dprintk(1, "Mailbox signature found at 0x%x\n", i + 1);
8390c0d06caSMauro Carvalho Chehab 			return i + 1;
8400c0d06caSMauro Carvalho Chehab 		}
8410c0d06caSMauro Carvalho Chehab 	}
8420c0d06caSMauro Carvalho Chehab 	dprintk(3, "Mailbox signature values not found!\n");
843ccc5429fSMauro Carvalho Chehab 	return -EIO;
8440c0d06caSMauro Carvalho Chehab }
8450c0d06caSMauro Carvalho Chehab 
mci_write_memory_to_gpio(struct cx231xx * dev,u32 address,u32 value,u32 * p_fw_image)8465b8acdc5SHans Verkuil static void mci_write_memory_to_gpio(struct cx231xx *dev, u32 address, u32 value,
8470c0d06caSMauro Carvalho Chehab 		u32 *p_fw_image)
8480c0d06caSMauro Carvalho Chehab {
8490c0d06caSMauro Carvalho Chehab 	u32 temp = 0;
8500c0d06caSMauro Carvalho Chehab 	int i = 0;
8510c0d06caSMauro Carvalho Chehab 
8520c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_DATA_BYTE0 | ((value & 0x000000FF) << 8);
8530c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
8540c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8550c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8565b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
8570c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8580c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8590c0d06caSMauro Carvalho Chehab 
8600c0d06caSMauro Carvalho Chehab 	/*write data byte 1;*/
8610c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_DATA_BYTE1 | (value & 0x0000FF00);
8620c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
8630c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8640c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8655b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
8660c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8670c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8680c0d06caSMauro Carvalho Chehab 
8690c0d06caSMauro Carvalho Chehab 	/*write data byte 2;*/
8700c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
8710c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
8720c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8730c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8745b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
8750c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8760c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8770c0d06caSMauro Carvalho Chehab 
8780c0d06caSMauro Carvalho Chehab 	/*write data byte 3;*/
8790c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
8800c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
8810c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8820c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8835b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
8840c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8850c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8860c0d06caSMauro Carvalho Chehab 
8870c0d06caSMauro Carvalho Chehab 	/* write address byte 2;*/
8880c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_WRITE |
8890c0d06caSMauro Carvalho Chehab 		((address & 0x003F0000) >> 8);
8900c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
8910c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8920c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8935b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
8940c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
8950c0d06caSMauro Carvalho Chehab 	p_fw_image++;
8960c0d06caSMauro Carvalho Chehab 
8970c0d06caSMauro Carvalho Chehab 	/* write address byte 1;*/
8980c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
8990c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
9000c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
9010c0d06caSMauro Carvalho Chehab 	p_fw_image++;
9025b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
9030c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
9040c0d06caSMauro Carvalho Chehab 	p_fw_image++;
9050c0d06caSMauro Carvalho Chehab 
9060c0d06caSMauro Carvalho Chehab 	/* write address byte 0;*/
9070c0d06caSMauro Carvalho Chehab 	temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
9080c0d06caSMauro Carvalho Chehab 	temp = temp << 10;
9090c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
9100c0d06caSMauro Carvalho Chehab 	p_fw_image++;
9115b8acdc5SHans Verkuil 	temp = temp | (0x05 << 10);
9120c0d06caSMauro Carvalho Chehab 	*p_fw_image = temp;
9130c0d06caSMauro Carvalho Chehab 	p_fw_image++;
9140c0d06caSMauro Carvalho Chehab 
9150c0d06caSMauro Carvalho Chehab 	for (i = 0; i < 6; i++) {
9160c0d06caSMauro Carvalho Chehab 		*p_fw_image = 0xFFFFFFFF;
9170c0d06caSMauro Carvalho Chehab 		p_fw_image++;
9180c0d06caSMauro Carvalho Chehab 	}
9190c0d06caSMauro Carvalho Chehab }
9200c0d06caSMauro Carvalho Chehab 
9210c0d06caSMauro Carvalho Chehab 
cx231xx_load_firmware(struct cx231xx * dev)9220c0d06caSMauro Carvalho Chehab static int cx231xx_load_firmware(struct cx231xx *dev)
9230c0d06caSMauro Carvalho Chehab {
9240c0d06caSMauro Carvalho Chehab 	static const unsigned char magic[8] = {
9250c0d06caSMauro Carvalho Chehab 		0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
9260c0d06caSMauro Carvalho Chehab 	};
9270c0d06caSMauro Carvalho Chehab 	const struct firmware *firmware;
9280c0d06caSMauro Carvalho Chehab 	int i, retval = 0;
9290c0d06caSMauro Carvalho Chehab 	u32 value = 0;
9300c0d06caSMauro Carvalho Chehab 	u32 gpio_output = 0;
9310c0d06caSMauro Carvalho Chehab 	/*u32 checksum = 0;*/
9320c0d06caSMauro Carvalho Chehab 	/*u32 *dataptr;*/
9330c0d06caSMauro Carvalho Chehab 	u32 transfer_size = 0;
9340c0d06caSMauro Carvalho Chehab 	u32 fw_data = 0;
9350c0d06caSMauro Carvalho Chehab 	u32 address = 0;
9360c0d06caSMauro Carvalho Chehab 	/*u32 current_fw[800];*/
9370c0d06caSMauro Carvalho Chehab 	u32 *p_current_fw, *p_fw;
9380c0d06caSMauro Carvalho Chehab 	u32 *p_fw_data;
9390c0d06caSMauro Carvalho Chehab 	int frame = 0;
9400c0d06caSMauro Carvalho Chehab 	u8 *p_buffer;
9410c0d06caSMauro Carvalho Chehab 
9420c0d06caSMauro Carvalho Chehab 	p_current_fw = vmalloc(1884180 * 4);
9430c0d06caSMauro Carvalho Chehab 	p_fw = p_current_fw;
9440c0d06caSMauro Carvalho Chehab 	if (p_current_fw == NULL) {
9450c0d06caSMauro Carvalho Chehab 		dprintk(2, "FAIL!!!\n");
946ccc5429fSMauro Carvalho Chehab 		return -ENOMEM;
9470c0d06caSMauro Carvalho Chehab 	}
9480c0d06caSMauro Carvalho Chehab 
94963c5f1d8SJinjie Ruan 	p_buffer = vmalloc(EP5_BUF_SIZE);
9500c0d06caSMauro Carvalho Chehab 	if (p_buffer == NULL) {
9510c0d06caSMauro Carvalho Chehab 		dprintk(2, "FAIL!!!\n");
95212548808SSudip Mukherjee 		vfree(p_current_fw);
953ccc5429fSMauro Carvalho Chehab 		return -ENOMEM;
9540c0d06caSMauro Carvalho Chehab 	}
9550c0d06caSMauro Carvalho Chehab 
9560c0d06caSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
9570c0d06caSMauro Carvalho Chehab 
9580c0d06caSMauro Carvalho Chehab 	/* Save GPIO settings before reset of APU */
9590c0d06caSMauro Carvalho Chehab 	retval |= mc417_memory_read(dev, 0x9020, &gpio_output);
9600c0d06caSMauro Carvalho Chehab 	retval |= mc417_memory_read(dev, 0x900C, &value);
9610c0d06caSMauro Carvalho Chehab 
9620c0d06caSMauro Carvalho Chehab 	retval  = mc417_register_write(dev,
9630c0d06caSMauro Carvalho Chehab 		IVTV_REG_VPU, 0xFFFFFFED);
9640c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_write(dev,
9650c0d06caSMauro Carvalho Chehab 		IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
9660c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_write(dev,
9670c0d06caSMauro Carvalho Chehab 		IVTV_REG_ENC_SDRAM_REFRESH, 0x80000800);
9680c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_write(dev,
9690c0d06caSMauro Carvalho Chehab 		IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
9700c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_write(dev,
9710c0d06caSMauro Carvalho Chehab 		IVTV_REG_APU, 0);
9720c0d06caSMauro Carvalho Chehab 
9730c0d06caSMauro Carvalho Chehab 	if (retval != 0) {
974336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
975b7085c08SMauro Carvalho Chehab 			"%s: Error with mc417_register_write\n", __func__);
97612548808SSudip Mukherjee 		vfree(p_current_fw);
97712548808SSudip Mukherjee 		vfree(p_buffer);
978ccc5429fSMauro Carvalho Chehab 		return retval;
9790c0d06caSMauro Carvalho Chehab 	}
9800c0d06caSMauro Carvalho Chehab 
9810c0d06caSMauro Carvalho Chehab 	retval = request_firmware(&firmware, CX231xx_FIRM_IMAGE_NAME,
982336fea92SMauro Carvalho Chehab 				  dev->dev);
9830c0d06caSMauro Carvalho Chehab 
9840c0d06caSMauro Carvalho Chehab 	if (retval != 0) {
985336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
986b7085c08SMauro Carvalho Chehab 			"ERROR: Hotplug firmware request failed (%s).\n",
9870c0d06caSMauro Carvalho Chehab 			CX231xx_FIRM_IMAGE_NAME);
988336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
989b7085c08SMauro Carvalho Chehab 			"Please fix your hotplug setup, the board will not work without firmware loaded!\n");
99012548808SSudip Mukherjee 		vfree(p_current_fw);
99112548808SSudip Mukherjee 		vfree(p_buffer);
992ccc5429fSMauro Carvalho Chehab 		return retval;
9930c0d06caSMauro Carvalho Chehab 	}
9940c0d06caSMauro Carvalho Chehab 
9950c0d06caSMauro Carvalho Chehab 	if (firmware->size != CX231xx_FIRM_IMAGE_SIZE) {
996336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
997b7085c08SMauro Carvalho Chehab 			"ERROR: Firmware size mismatch (have %zd, expected %d)\n",
9980c0d06caSMauro Carvalho Chehab 			firmware->size, CX231xx_FIRM_IMAGE_SIZE);
9990c0d06caSMauro Carvalho Chehab 		release_firmware(firmware);
100012548808SSudip Mukherjee 		vfree(p_current_fw);
100112548808SSudip Mukherjee 		vfree(p_buffer);
1002ccc5429fSMauro Carvalho Chehab 		return -EINVAL;
10030c0d06caSMauro Carvalho Chehab 	}
10040c0d06caSMauro Carvalho Chehab 
10050c0d06caSMauro Carvalho Chehab 	if (0 != memcmp(firmware->data, magic, 8)) {
1006336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1007b7085c08SMauro Carvalho Chehab 			"ERROR: Firmware magic mismatch, wrong file?\n");
10080c0d06caSMauro Carvalho Chehab 		release_firmware(firmware);
100912548808SSudip Mukherjee 		vfree(p_current_fw);
101012548808SSudip Mukherjee 		vfree(p_buffer);
1011ccc5429fSMauro Carvalho Chehab 		return -EINVAL;
10120c0d06caSMauro Carvalho Chehab 	}
10130c0d06caSMauro Carvalho Chehab 
10140c0d06caSMauro Carvalho Chehab 	initGPIO(dev);
10150c0d06caSMauro Carvalho Chehab 
10160c0d06caSMauro Carvalho Chehab 	/* transfer to the chip */
10170c0d06caSMauro Carvalho Chehab 	dprintk(2, "Loading firmware to GPIO...\n");
10180c0d06caSMauro Carvalho Chehab 	p_fw_data = (u32 *)firmware->data;
10190c0d06caSMauro Carvalho Chehab 	dprintk(2, "firmware->size=%zd\n", firmware->size);
10200c0d06caSMauro Carvalho Chehab 	for (transfer_size = 0; transfer_size < firmware->size;
10210c0d06caSMauro Carvalho Chehab 		 transfer_size += 4) {
10220c0d06caSMauro Carvalho Chehab 		fw_data = *p_fw_data;
10230c0d06caSMauro Carvalho Chehab 
10245b8acdc5SHans Verkuil 		mci_write_memory_to_gpio(dev, address, fw_data, p_current_fw);
10250c0d06caSMauro Carvalho Chehab 		address = address + 1;
10260c0d06caSMauro Carvalho Chehab 		p_current_fw += 20;
10270c0d06caSMauro Carvalho Chehab 		p_fw_data += 1;
10280c0d06caSMauro Carvalho Chehab 	}
10290c0d06caSMauro Carvalho Chehab 
10300c0d06caSMauro Carvalho Chehab 	/*download the firmware by ep5-out*/
10310c0d06caSMauro Carvalho Chehab 
103263c5f1d8SJinjie Ruan 	for (frame = 0; frame < (int)(CX231xx_FIRM_IMAGE_SIZE*20/EP5_BUF_SIZE);
10330c0d06caSMauro Carvalho Chehab 	     frame++) {
103463c5f1d8SJinjie Ruan 		for (i = 0; i < EP5_BUF_SIZE; i++) {
10350c0d06caSMauro Carvalho Chehab 			*(p_buffer + i) = (u8)(*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x000000FF);
10360c0d06caSMauro Carvalho Chehab 			i++;
10370c0d06caSMauro Carvalho Chehab 			*(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x0000FF00) >> 8);
10380c0d06caSMauro Carvalho Chehab 			i++;
10390c0d06caSMauro Carvalho Chehab 			*(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x00FF0000) >> 16);
10400c0d06caSMauro Carvalho Chehab 			i++;
10410c0d06caSMauro Carvalho Chehab 			*(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0xFF000000) >> 24);
10420c0d06caSMauro Carvalho Chehab 		}
104363c5f1d8SJinjie Ruan 		cx231xx_ep5_bulkout(dev, p_buffer, EP5_BUF_SIZE);
10440c0d06caSMauro Carvalho Chehab 	}
10450c0d06caSMauro Carvalho Chehab 
10460c0d06caSMauro Carvalho Chehab 	p_current_fw = p_fw;
10470c0d06caSMauro Carvalho Chehab 	vfree(p_current_fw);
10480c0d06caSMauro Carvalho Chehab 	p_current_fw = NULL;
1049725a2829SWenwen Wang 	vfree(p_buffer);
10500c0d06caSMauro Carvalho Chehab 	uninitGPIO(dev);
10510c0d06caSMauro Carvalho Chehab 	release_firmware(firmware);
10520c0d06caSMauro Carvalho Chehab 	dprintk(1, "Firmware upload successful.\n");
10530c0d06caSMauro Carvalho Chehab 
10540c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_write(dev, IVTV_REG_HW_BLOCKS,
10550c0d06caSMauro Carvalho Chehab 		IVTV_CMD_HW_BLOCKS_RST);
10560c0d06caSMauro Carvalho Chehab 	if (retval < 0) {
1057336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1058b7085c08SMauro Carvalho Chehab 			"%s: Error with mc417_register_write\n",
10590c0d06caSMauro Carvalho Chehab 			__func__);
10600c0d06caSMauro Carvalho Chehab 		return retval;
10610c0d06caSMauro Carvalho Chehab 	}
10620c0d06caSMauro Carvalho Chehab 	/* F/W power up disturbs the GPIOs, restore state */
10630c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_write(dev, 0x9020, gpio_output);
10640c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_write(dev, 0x900C, value);
10650c0d06caSMauro Carvalho Chehab 
10660c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_read(dev, IVTV_REG_VPU, &value);
10670c0d06caSMauro Carvalho Chehab 	retval |= mc417_register_write(dev, IVTV_REG_VPU, value & 0xFFFFFFE8);
10680c0d06caSMauro Carvalho Chehab 
10690c0d06caSMauro Carvalho Chehab 	if (retval < 0) {
1070336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1071b7085c08SMauro Carvalho Chehab 			"%s: Error with mc417_register_write\n",
10720c0d06caSMauro Carvalho Chehab 			__func__);
10730c0d06caSMauro Carvalho Chehab 		return retval;
10740c0d06caSMauro Carvalho Chehab 	}
10750c0d06caSMauro Carvalho Chehab 	return 0;
10760c0d06caSMauro Carvalho Chehab }
10770c0d06caSMauro Carvalho Chehab 
cx231xx_codec_settings(struct cx231xx * dev)10780c0d06caSMauro Carvalho Chehab static void cx231xx_codec_settings(struct cx231xx *dev)
10790c0d06caSMauro Carvalho Chehab {
10800c0d06caSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
10810c0d06caSMauro Carvalho Chehab 
10820c0d06caSMauro Carvalho Chehab 	/* assign frame size */
10830c0d06caSMauro Carvalho Chehab 	cx231xx_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
10840c0d06caSMauro Carvalho Chehab 				dev->ts1.height, dev->ts1.width);
10850c0d06caSMauro Carvalho Chehab 
108688b6ffedSHans Verkuil 	dev->mpeg_ctrl_handler.width = dev->ts1.width;
108788b6ffedSHans Verkuil 	dev->mpeg_ctrl_handler.height = dev->ts1.height;
10880c0d06caSMauro Carvalho Chehab 
108988b6ffedSHans Verkuil 	cx2341x_handler_setup(&dev->mpeg_ctrl_handler);
10900c0d06caSMauro Carvalho Chehab 
10910c0d06caSMauro Carvalho Chehab 	cx231xx_api_cmd(dev, CX2341X_ENC_MISC, 2, 0, 3, 1);
10920c0d06caSMauro Carvalho Chehab 	cx231xx_api_cmd(dev, CX2341X_ENC_MISC, 2, 0, 4, 1);
10930c0d06caSMauro Carvalho Chehab }
10940c0d06caSMauro Carvalho Chehab 
cx231xx_initialize_codec(struct cx231xx * dev)10950c0d06caSMauro Carvalho Chehab static int cx231xx_initialize_codec(struct cx231xx *dev)
10960c0d06caSMauro Carvalho Chehab {
10970c0d06caSMauro Carvalho Chehab 	int version;
10980c0d06caSMauro Carvalho Chehab 	int retval;
10990c0d06caSMauro Carvalho Chehab 	u32 i;
11000c0d06caSMauro Carvalho Chehab 	u32 val = 0;
11010c0d06caSMauro Carvalho Chehab 
11020c0d06caSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
11030c0d06caSMauro Carvalho Chehab 	cx231xx_disable656(dev);
11040c0d06caSMauro Carvalho Chehab 	retval = cx231xx_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
11050c0d06caSMauro Carvalho Chehab 	if (retval < 0) {
1106ed0e3729SMauro Carvalho Chehab 		dprintk(2, "%s: PING OK\n", __func__);
11070c0d06caSMauro Carvalho Chehab 		retval = cx231xx_load_firmware(dev);
11080c0d06caSMauro Carvalho Chehab 		if (retval < 0) {
1109336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
1110b7085c08SMauro Carvalho Chehab 				"%s: f/w load failed\n", __func__);
11110c0d06caSMauro Carvalho Chehab 			return retval;
11120c0d06caSMauro Carvalho Chehab 		}
11130c0d06caSMauro Carvalho Chehab 		retval = cx231xx_find_mailbox(dev);
11140c0d06caSMauro Carvalho Chehab 		if (retval < 0) {
1115336fea92SMauro Carvalho Chehab 			dev_err(dev->dev, "%s: mailbox < 0, error\n",
11160c0d06caSMauro Carvalho Chehab 				__func__);
1117ccc5429fSMauro Carvalho Chehab 			return retval;
11180c0d06caSMauro Carvalho Chehab 		}
11190c0d06caSMauro Carvalho Chehab 		dev->cx23417_mailbox = retval;
11200c0d06caSMauro Carvalho Chehab 		retval = cx231xx_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0);
11210c0d06caSMauro Carvalho Chehab 		if (retval < 0) {
1122336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
1123b7085c08SMauro Carvalho Chehab 				"ERROR: cx23417 firmware ping failed!\n");
1124ccc5429fSMauro Carvalho Chehab 			return retval;
11250c0d06caSMauro Carvalho Chehab 		}
11260c0d06caSMauro Carvalho Chehab 		retval = cx231xx_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1,
11270c0d06caSMauro Carvalho Chehab 			&version);
11280c0d06caSMauro Carvalho Chehab 		if (retval < 0) {
1129336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
1130b7085c08SMauro Carvalho Chehab 				"ERROR: cx23417 firmware get encoder: version failed!\n");
1131ccc5429fSMauro Carvalho Chehab 			return retval;
11320c0d06caSMauro Carvalho Chehab 		}
11330c0d06caSMauro Carvalho Chehab 		dprintk(1, "cx23417 firmware version is 0x%08x\n", version);
11340c0d06caSMauro Carvalho Chehab 		msleep(200);
11350c0d06caSMauro Carvalho Chehab 	}
11360c0d06caSMauro Carvalho Chehab 
11370c0d06caSMauro Carvalho Chehab 	for (i = 0; i < 1; i++) {
11380c0d06caSMauro Carvalho Chehab 		retval = mc417_register_read(dev, 0x20f8, &val);
11390c0d06caSMauro Carvalho Chehab 		dprintk(3, "***before enable656() VIM Capture Lines = %d ***\n",
11400c0d06caSMauro Carvalho Chehab 				 val);
11410c0d06caSMauro Carvalho Chehab 		if (retval < 0)
11420c0d06caSMauro Carvalho Chehab 			return retval;
11430c0d06caSMauro Carvalho Chehab 	}
11440c0d06caSMauro Carvalho Chehab 
11450c0d06caSMauro Carvalho Chehab 	cx231xx_enable656(dev);
114669626853SMauro Carvalho Chehab 
11470c0d06caSMauro Carvalho Chehab 	/* stop mpeg capture */
114869626853SMauro Carvalho Chehab 	cx231xx_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, 1, 3, 4);
11490c0d06caSMauro Carvalho Chehab 
11500c0d06caSMauro Carvalho Chehab 	cx231xx_codec_settings(dev);
11510c0d06caSMauro Carvalho Chehab 	msleep(60);
11520c0d06caSMauro Carvalho Chehab 
11530c0d06caSMauro Carvalho Chehab /*	cx231xx_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
11540c0d06caSMauro Carvalho Chehab 		CX231xx_FIELD1_SAA7115, CX231xx_FIELD2_SAA7115);
11550c0d06caSMauro Carvalho Chehab 	cx231xx_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
11560c0d06caSMauro Carvalho Chehab 		CX231xx_CUSTOM_EXTENSION_USR_DATA, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11570c0d06caSMauro Carvalho Chehab 		0, 0);
11580c0d06caSMauro Carvalho Chehab */
11590c0d06caSMauro Carvalho Chehab 
11600c0d06caSMauro Carvalho Chehab #if 0
11610c0d06caSMauro Carvalho Chehab 	/* TODO */
11620c0d06caSMauro Carvalho Chehab 	u32 data[7];
11630c0d06caSMauro Carvalho Chehab 
11640c0d06caSMauro Carvalho Chehab 	/* Setup to capture VBI */
11650c0d06caSMauro Carvalho Chehab 	data[0] = 0x0001BD00;
11660c0d06caSMauro Carvalho Chehab 	data[1] = 1;          /* frames per interrupt */
11670c0d06caSMauro Carvalho Chehab 	data[2] = 4;          /* total bufs */
11680c0d06caSMauro Carvalho Chehab 	data[3] = 0x91559155; /* start codes */
11690c0d06caSMauro Carvalho Chehab 	data[4] = 0x206080C0; /* stop codes */
11700c0d06caSMauro Carvalho Chehab 	data[5] = 6;          /* lines */
11710c0d06caSMauro Carvalho Chehab 	data[6] = 64;         /* BPL */
11720c0d06caSMauro Carvalho Chehab 
11730c0d06caSMauro Carvalho Chehab 	cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_CONFIG, 7, 0, data[0], data[1],
11740c0d06caSMauro Carvalho Chehab 		data[2], data[3], data[4], data[5], data[6]);
11750c0d06caSMauro Carvalho Chehab 
11760c0d06caSMauro Carvalho Chehab 	for (i = 2; i <= 24; i++) {
11770c0d06caSMauro Carvalho Chehab 		int valid;
11780c0d06caSMauro Carvalho Chehab 
11790c0d06caSMauro Carvalho Chehab 		valid = ((i >= 19) && (i <= 21));
11800c0d06caSMauro Carvalho Chehab 		cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_LINE, 5, 0, i,
11810c0d06caSMauro Carvalho Chehab 				valid, 0 , 0, 0);
11820c0d06caSMauro Carvalho Chehab 		cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_LINE, 5, 0,
11830c0d06caSMauro Carvalho Chehab 				i | 0x80000000, valid, 0, 0, 0);
11840c0d06caSMauro Carvalho Chehab 	}
11850c0d06caSMauro Carvalho Chehab #endif
11860c0d06caSMauro Carvalho Chehab /*	cx231xx_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, CX231xx_UNMUTE);
11870c0d06caSMauro Carvalho Chehab 	msleep(60);
11880c0d06caSMauro Carvalho Chehab */
11890c0d06caSMauro Carvalho Chehab 	/* initialize the video input */
11900c0d06caSMauro Carvalho Chehab 	retval = cx231xx_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
11910c0d06caSMauro Carvalho Chehab 	if (retval < 0)
11920c0d06caSMauro Carvalho Chehab 		return retval;
11930c0d06caSMauro Carvalho Chehab 	msleep(60);
11940c0d06caSMauro Carvalho Chehab 
11950c0d06caSMauro Carvalho Chehab 	/* Enable VIP style pixel invalidation so we work with scaled mode */
11960c0d06caSMauro Carvalho Chehab 	mc417_memory_write(dev, 2120, 0x00000080);
11970c0d06caSMauro Carvalho Chehab 
11980c0d06caSMauro Carvalho Chehab 	/* start capturing to the host interface */
11990c0d06caSMauro Carvalho Chehab 	retval = cx231xx_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
12000c0d06caSMauro Carvalho Chehab 		CX231xx_MPEG_CAPTURE, CX231xx_RAW_BITS_NONE);
12010c0d06caSMauro Carvalho Chehab 	if (retval < 0)
12020c0d06caSMauro Carvalho Chehab 		return retval;
12030c0d06caSMauro Carvalho Chehab 	msleep(10);
12040c0d06caSMauro Carvalho Chehab 
12050c0d06caSMauro Carvalho Chehab 	for (i = 0; i < 1; i++) {
12060c0d06caSMauro Carvalho Chehab 		mc417_register_read(dev, 0x20f8, &val);
12070c0d06caSMauro Carvalho Chehab 		dprintk(3, "***VIM Capture Lines =%d ***\n", val);
12080c0d06caSMauro Carvalho Chehab 	}
12090c0d06caSMauro Carvalho Chehab 
12100c0d06caSMauro Carvalho Chehab 	return 0;
12110c0d06caSMauro Carvalho Chehab }
12120c0d06caSMauro Carvalho Chehab 
12130c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
12140c0d06caSMauro Carvalho Chehab 
queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])12157c617138SHans Verkuil static int queue_setup(struct vb2_queue *vq,
12167c617138SHans Verkuil 		       unsigned int *nbuffers, unsigned int *nplanes,
12177c617138SHans Verkuil 		       unsigned int sizes[], struct device *alloc_devs[])
12180c0d06caSMauro Carvalho Chehab {
12197c617138SHans Verkuil 	struct cx231xx *dev = vb2_get_drv_priv(vq);
12207c617138SHans Verkuil 	unsigned int size = mpeglinesize * mpeglines;
1221ca44d57aSBenjamin Gaignard 	unsigned int q_num_bufs = vb2_get_num_buffers(vq);
12220c0d06caSMauro Carvalho Chehab 
12237c617138SHans Verkuil 	dev->ts1.ts_packet_size  = mpeglinesize;
12247c617138SHans Verkuil 	dev->ts1.ts_packet_count = mpeglines;
12250c0d06caSMauro Carvalho Chehab 
1226ca44d57aSBenjamin Gaignard 	if (q_num_bufs + *nbuffers < CX231XX_MIN_BUF)
1227ca44d57aSBenjamin Gaignard 		*nbuffers = CX231XX_MIN_BUF - q_num_bufs;
12287c617138SHans Verkuil 
12297c617138SHans Verkuil 	if (*nplanes)
12307c617138SHans Verkuil 		return sizes[0] < size ? -EINVAL : 0;
12317c617138SHans Verkuil 	*nplanes = 1;
12327c617138SHans Verkuil 	sizes[0] = mpeglinesize * mpeglines;
12330c0d06caSMauro Carvalho Chehab 
12340c0d06caSMauro Carvalho Chehab 	return 0;
12350c0d06caSMauro Carvalho Chehab }
12365aa95991SHans Verkuil 
buffer_copy(struct cx231xx * dev,char * data,int len,struct urb * urb,struct cx231xx_dmaqueue * dma_q)12370c0d06caSMauro Carvalho Chehab static void buffer_copy(struct cx231xx *dev, char *data, int len, struct urb *urb,
12380c0d06caSMauro Carvalho Chehab 		struct cx231xx_dmaqueue *dma_q)
12390c0d06caSMauro Carvalho Chehab {
12400c0d06caSMauro Carvalho Chehab 	void *vbuf;
12410c0d06caSMauro Carvalho Chehab 	struct cx231xx_buffer *buf;
12420c0d06caSMauro Carvalho Chehab 	u32 tail_data = 0;
12430c0d06caSMauro Carvalho Chehab 	char *p_data;
12440c0d06caSMauro Carvalho Chehab 
12450c0d06caSMauro Carvalho Chehab 	if (dma_q->mpeg_buffer_done == 0) {
12460c0d06caSMauro Carvalho Chehab 		if (list_empty(&dma_q->active))
12470c0d06caSMauro Carvalho Chehab 			return;
12480c0d06caSMauro Carvalho Chehab 
12490c0d06caSMauro Carvalho Chehab 		buf = list_entry(dma_q->active.next,
12507c617138SHans Verkuil 				struct cx231xx_buffer, list);
12510c0d06caSMauro Carvalho Chehab 		dev->video_mode.isoc_ctl.buf = buf;
12520c0d06caSMauro Carvalho Chehab 		dma_q->mpeg_buffer_done = 1;
12530c0d06caSMauro Carvalho Chehab 	}
12540c0d06caSMauro Carvalho Chehab 	/* Fill buffer */
12550c0d06caSMauro Carvalho Chehab 	buf = dev->video_mode.isoc_ctl.buf;
12567c617138SHans Verkuil 	vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
12570c0d06caSMauro Carvalho Chehab 
12580c0d06caSMauro Carvalho Chehab 	if ((dma_q->mpeg_buffer_completed+len) <
12590c0d06caSMauro Carvalho Chehab 			mpeglines*mpeglinesize) {
12600c0d06caSMauro Carvalho Chehab 		if (dma_q->add_ps_package_head ==
12610c0d06caSMauro Carvalho Chehab 				CX231XX_NEED_ADD_PS_PACKAGE_HEAD) {
12620c0d06caSMauro Carvalho Chehab 			memcpy(vbuf+dma_q->mpeg_buffer_completed,
12630c0d06caSMauro Carvalho Chehab 					dma_q->ps_head, 3);
12640c0d06caSMauro Carvalho Chehab 			dma_q->mpeg_buffer_completed =
12650c0d06caSMauro Carvalho Chehab 				dma_q->mpeg_buffer_completed + 3;
12660c0d06caSMauro Carvalho Chehab 			dma_q->add_ps_package_head =
12670c0d06caSMauro Carvalho Chehab 				CX231XX_NONEED_PS_PACKAGE_HEAD;
12680c0d06caSMauro Carvalho Chehab 		}
12690c0d06caSMauro Carvalho Chehab 		memcpy(vbuf+dma_q->mpeg_buffer_completed, data, len);
12700c0d06caSMauro Carvalho Chehab 		dma_q->mpeg_buffer_completed =
12710c0d06caSMauro Carvalho Chehab 			dma_q->mpeg_buffer_completed + len;
12720c0d06caSMauro Carvalho Chehab 	} else {
12730c0d06caSMauro Carvalho Chehab 		dma_q->mpeg_buffer_done = 0;
12740c0d06caSMauro Carvalho Chehab 
12750c0d06caSMauro Carvalho Chehab 		tail_data =
12760c0d06caSMauro Carvalho Chehab 			mpeglines*mpeglinesize - dma_q->mpeg_buffer_completed;
12770c0d06caSMauro Carvalho Chehab 		memcpy(vbuf+dma_q->mpeg_buffer_completed,
12780c0d06caSMauro Carvalho Chehab 				data, tail_data);
12790c0d06caSMauro Carvalho Chehab 
12807c617138SHans Verkuil 		buf->vb.vb2_buf.timestamp = ktime_get_ns();
12817c617138SHans Verkuil 		buf->vb.sequence = dma_q->sequence++;
12827c617138SHans Verkuil 		list_del(&buf->list);
12837c617138SHans Verkuil 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
12840c0d06caSMauro Carvalho Chehab 		dma_q->mpeg_buffer_completed = 0;
12850c0d06caSMauro Carvalho Chehab 
12860c0d06caSMauro Carvalho Chehab 		if (len - tail_data > 0) {
12870c0d06caSMauro Carvalho Chehab 			p_data = data + tail_data;
12880c0d06caSMauro Carvalho Chehab 			dma_q->left_data_count = len - tail_data;
12890c0d06caSMauro Carvalho Chehab 			memcpy(dma_q->p_left_data,
12900c0d06caSMauro Carvalho Chehab 					p_data, len - tail_data);
12910c0d06caSMauro Carvalho Chehab 		}
12920c0d06caSMauro Carvalho Chehab 	}
12930c0d06caSMauro Carvalho Chehab }
12940c0d06caSMauro Carvalho Chehab 
buffer_filled(char * data,int len,struct urb * urb,struct cx231xx_dmaqueue * dma_q)12950c0d06caSMauro Carvalho Chehab static void buffer_filled(char *data, int len, struct urb *urb,
12960c0d06caSMauro Carvalho Chehab 		struct cx231xx_dmaqueue *dma_q)
12970c0d06caSMauro Carvalho Chehab {
12980c0d06caSMauro Carvalho Chehab 	void *vbuf;
12990c0d06caSMauro Carvalho Chehab 	struct cx231xx_buffer *buf;
13000c0d06caSMauro Carvalho Chehab 
13010c0d06caSMauro Carvalho Chehab 	if (list_empty(&dma_q->active))
13020c0d06caSMauro Carvalho Chehab 		return;
13030c0d06caSMauro Carvalho Chehab 
13047c617138SHans Verkuil 	buf = list_entry(dma_q->active.next, struct cx231xx_buffer, list);
13050c0d06caSMauro Carvalho Chehab 
13060c0d06caSMauro Carvalho Chehab 	/* Fill buffer */
13077c617138SHans Verkuil 	vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
13080c0d06caSMauro Carvalho Chehab 	memcpy(vbuf, data, len);
13097c617138SHans Verkuil 	buf->vb.sequence = dma_q->sequence++;
13107c617138SHans Verkuil 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
13117c617138SHans Verkuil 	list_del(&buf->list);
13127c617138SHans Verkuil 	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
13130c0d06caSMauro Carvalho Chehab }
13145b8acdc5SHans Verkuil 
cx231xx_isoc_copy(struct cx231xx * dev,struct urb * urb)13155b8acdc5SHans Verkuil static int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
13160c0d06caSMauro Carvalho Chehab {
13170c0d06caSMauro Carvalho Chehab 	struct cx231xx_dmaqueue *dma_q = urb->context;
13180c0d06caSMauro Carvalho Chehab 	unsigned char *p_buffer;
13190c0d06caSMauro Carvalho Chehab 	u32 buffer_size = 0;
13200c0d06caSMauro Carvalho Chehab 	u32 i = 0;
13210c0d06caSMauro Carvalho Chehab 
13220c0d06caSMauro Carvalho Chehab 	for (i = 0; i < urb->number_of_packets; i++) {
13230c0d06caSMauro Carvalho Chehab 		if (dma_q->left_data_count > 0) {
13240c0d06caSMauro Carvalho Chehab 			buffer_copy(dev, dma_q->p_left_data,
13250c0d06caSMauro Carvalho Chehab 				    dma_q->left_data_count, urb, dma_q);
13260c0d06caSMauro Carvalho Chehab 			dma_q->mpeg_buffer_completed = dma_q->left_data_count;
13270c0d06caSMauro Carvalho Chehab 			dma_q->left_data_count = 0;
13280c0d06caSMauro Carvalho Chehab 		}
13290c0d06caSMauro Carvalho Chehab 
13300c0d06caSMauro Carvalho Chehab 		p_buffer = urb->transfer_buffer +
13310c0d06caSMauro Carvalho Chehab 				urb->iso_frame_desc[i].offset;
13320c0d06caSMauro Carvalho Chehab 		buffer_size = urb->iso_frame_desc[i].actual_length;
13330c0d06caSMauro Carvalho Chehab 
13340c0d06caSMauro Carvalho Chehab 		if (buffer_size > 0)
13350c0d06caSMauro Carvalho Chehab 			buffer_copy(dev, p_buffer, buffer_size, urb, dma_q);
13360c0d06caSMauro Carvalho Chehab 	}
13370c0d06caSMauro Carvalho Chehab 
13380c0d06caSMauro Carvalho Chehab 	return 0;
13390c0d06caSMauro Carvalho Chehab }
13400c0d06caSMauro Carvalho Chehab 
cx231xx_bulk_copy(struct cx231xx * dev,struct urb * urb)13415b8acdc5SHans Verkuil static int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
13425b8acdc5SHans Verkuil {
13430c0d06caSMauro Carvalho Chehab 	struct cx231xx_dmaqueue *dma_q = urb->context;
13440c0d06caSMauro Carvalho Chehab 	unsigned char *p_buffer, *buffer;
13450c0d06caSMauro Carvalho Chehab 	u32 buffer_size = 0;
13460c0d06caSMauro Carvalho Chehab 
13470c0d06caSMauro Carvalho Chehab 	p_buffer = urb->transfer_buffer;
13480c0d06caSMauro Carvalho Chehab 	buffer_size = urb->actual_length;
13490c0d06caSMauro Carvalho Chehab 
13500c0d06caSMauro Carvalho Chehab 	buffer = kmalloc(buffer_size, GFP_ATOMIC);
1351f8433226SInsu Yun 	if (!buffer)
1352f8433226SInsu Yun 		return -ENOMEM;
13530c0d06caSMauro Carvalho Chehab 
13540c0d06caSMauro Carvalho Chehab 	memcpy(buffer, dma_q->ps_head, 3);
13550c0d06caSMauro Carvalho Chehab 	memcpy(buffer+3, p_buffer, buffer_size-3);
13560c0d06caSMauro Carvalho Chehab 	memcpy(dma_q->ps_head, p_buffer+buffer_size-3, 3);
13570c0d06caSMauro Carvalho Chehab 
13580c0d06caSMauro Carvalho Chehab 	p_buffer = buffer;
13590c0d06caSMauro Carvalho Chehab 	buffer_filled(p_buffer, buffer_size, urb, dma_q);
13600c0d06caSMauro Carvalho Chehab 
13610c0d06caSMauro Carvalho Chehab 	kfree(buffer);
13620c0d06caSMauro Carvalho Chehab 	return 0;
13630c0d06caSMauro Carvalho Chehab }
13640c0d06caSMauro Carvalho Chehab 
buffer_queue(struct vb2_buffer * vb)13657c617138SHans Verkuil static void buffer_queue(struct vb2_buffer *vb)
13660c0d06caSMauro Carvalho Chehab {
13670c0d06caSMauro Carvalho Chehab 	struct cx231xx_buffer *buf =
13687c617138SHans Verkuil 	    container_of(vb, struct cx231xx_buffer, vb.vb2_buf);
13697c617138SHans Verkuil 	struct cx231xx *dev = vb2_get_drv_priv(vb->vb2_queue);
13707c617138SHans Verkuil 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
13717c617138SHans Verkuil 	unsigned long flags;
13720c0d06caSMauro Carvalho Chehab 
13737c617138SHans Verkuil 	spin_lock_irqsave(&dev->video_mode.slock, flags);
13747c617138SHans Verkuil 	list_add_tail(&buf->list, &vidq->active);
13757c617138SHans Verkuil 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
13760c0d06caSMauro Carvalho Chehab }
13770c0d06caSMauro Carvalho Chehab 
return_all_buffers(struct cx231xx * dev,enum vb2_buffer_state state)13787c617138SHans Verkuil static void return_all_buffers(struct cx231xx *dev,
13797c617138SHans Verkuil 			       enum vb2_buffer_state state)
13807c617138SHans Verkuil {
13817c617138SHans Verkuil 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
13827c617138SHans Verkuil 	struct cx231xx_buffer *buf, *node;
13837c617138SHans Verkuil 	unsigned long flags;
13847c617138SHans Verkuil 
13857c617138SHans Verkuil 	spin_lock_irqsave(&dev->video_mode.slock, flags);
13867c617138SHans Verkuil 	list_for_each_entry_safe(buf, node, &vidq->active, list) {
13877c617138SHans Verkuil 		vb2_buffer_done(&buf->vb.vb2_buf, state);
13887c617138SHans Verkuil 		list_del(&buf->list);
13890c0d06caSMauro Carvalho Chehab 	}
13907c617138SHans Verkuil 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
13917c617138SHans Verkuil }
13927c617138SHans Verkuil 
start_streaming(struct vb2_queue * vq,unsigned int count)13937c617138SHans Verkuil static int start_streaming(struct vb2_queue *vq, unsigned int count)
13947c617138SHans Verkuil {
13957c617138SHans Verkuil 	struct cx231xx *dev = vb2_get_drv_priv(vq);
13967c617138SHans Verkuil 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
13977c617138SHans Verkuil 	int ret = 0;
13987c617138SHans Verkuil 
13997c617138SHans Verkuil 	vidq->sequence = 0;
14000c0d06caSMauro Carvalho Chehab 	dev->mode_tv = 1;
14010c0d06caSMauro Carvalho Chehab 
14027c617138SHans Verkuil 	cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
14037c617138SHans Verkuil 	cx231xx_set_gpio_value(dev, 2, 0);
14047c617138SHans Verkuil 
14057c617138SHans Verkuil 	cx231xx_initialize_codec(dev);
14067c617138SHans Verkuil 
14077c617138SHans Verkuil 	cx231xx_start_TS1(dev);
14087c617138SHans Verkuil 
14097c617138SHans Verkuil 	cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
14107c617138SHans Verkuil 	cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
14117c617138SHans Verkuil 	if (dev->USE_ISO)
14127c617138SHans Verkuil 		ret = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
14137c617138SHans Verkuil 					CX231XX_NUM_BUFS,
14140c0d06caSMauro Carvalho Chehab 					dev->ts1_mode.max_pkt_size,
14150c0d06caSMauro Carvalho Chehab 					cx231xx_isoc_copy);
14167c617138SHans Verkuil 	else
14177c617138SHans Verkuil 		ret = cx231xx_init_bulk(dev, 320, 5,
14180c0d06caSMauro Carvalho Chehab 					dev->ts1_mode.max_pkt_size,
14190c0d06caSMauro Carvalho Chehab 					cx231xx_bulk_copy);
14207c617138SHans Verkuil 	if (ret)
14217c617138SHans Verkuil 		return_all_buffers(dev, VB2_BUF_STATE_QUEUED);
14227c617138SHans Verkuil 
14237c617138SHans Verkuil 	call_all(dev, video, s_stream, 1);
14247c617138SHans Verkuil 	return ret;
14250c0d06caSMauro Carvalho Chehab }
14260c0d06caSMauro Carvalho Chehab 
stop_streaming(struct vb2_queue * vq)14277c617138SHans Verkuil static void stop_streaming(struct vb2_queue *vq)
14280c0d06caSMauro Carvalho Chehab {
14297c617138SHans Verkuil 	struct cx231xx *dev = vb2_get_drv_priv(vq);
14307c617138SHans Verkuil 	unsigned long flags;
14310c0d06caSMauro Carvalho Chehab 
14327c617138SHans Verkuil 	call_all(dev, video, s_stream, 0);
14330c0d06caSMauro Carvalho Chehab 
14347c617138SHans Verkuil 	cx231xx_stop_TS1(dev);
14350c0d06caSMauro Carvalho Chehab 
14367c617138SHans Verkuil 	/* do this before setting alternate! */
14377c617138SHans Verkuil 	if (dev->USE_ISO)
14387c617138SHans Verkuil 		cx231xx_uninit_isoc(dev);
14397c617138SHans Verkuil 	else
14407c617138SHans Verkuil 		cx231xx_uninit_bulk(dev);
14417c617138SHans Verkuil 	cx231xx_set_mode(dev, CX231XX_SUSPEND);
14427c617138SHans Verkuil 
14437c617138SHans Verkuil 	cx231xx_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
14447c617138SHans Verkuil 			CX231xx_END_NOW, CX231xx_MPEG_CAPTURE,
14457c617138SHans Verkuil 			CX231xx_RAW_BITS_NONE);
14467c617138SHans Verkuil 
14477c617138SHans Verkuil 	spin_lock_irqsave(&dev->video_mode.slock, flags);
14487c617138SHans Verkuil 	if (dev->USE_ISO)
14497c617138SHans Verkuil 		dev->video_mode.isoc_ctl.buf = NULL;
14507c617138SHans Verkuil 	else
14517c617138SHans Verkuil 		dev->video_mode.bulk_ctl.buf = NULL;
14527c617138SHans Verkuil 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
14537c617138SHans Verkuil 	return_all_buffers(dev, VB2_BUF_STATE_ERROR);
14540c0d06caSMauro Carvalho Chehab }
14550c0d06caSMauro Carvalho Chehab 
1456*d2ae63c2SChristophe JAILLET static const struct vb2_ops cx231xx_video_qops = {
14577c617138SHans Verkuil 	.queue_setup		= queue_setup,
14587c617138SHans Verkuil 	.buf_queue		= buffer_queue,
14597c617138SHans Verkuil 	.start_streaming	= start_streaming,
14607c617138SHans Verkuil 	.stop_streaming		= stop_streaming,
14617c617138SHans Verkuil 	.wait_prepare		= vb2_ops_wait_prepare,
14627c617138SHans Verkuil 	.wait_finish		= vb2_ops_wait_finish,
14630c0d06caSMauro Carvalho Chehab };
14640c0d06caSMauro Carvalho Chehab 
14650c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
14660c0d06caSMauro Carvalho Chehab 
vidioc_g_pixelaspect(struct file * file,void * priv,int type,struct v4l2_fract * f)14675200ab6aSHans Verkuil static int vidioc_g_pixelaspect(struct file *file, void *priv,
14685200ab6aSHans Verkuil 				int type, struct v4l2_fract *f)
1469e25cb200SHans Verkuil {
14707c617138SHans Verkuil 	struct cx231xx *dev = video_drvdata(file);
1471e25cb200SHans Verkuil 	bool is_50hz = dev->encodernorm.id & V4L2_STD_625_50;
1472e25cb200SHans Verkuil 
14735200ab6aSHans Verkuil 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1474e25cb200SHans Verkuil 		return -EINVAL;
1475e25cb200SHans Verkuil 
14765200ab6aSHans Verkuil 	f->numerator = is_50hz ? 54 : 11;
14775200ab6aSHans Verkuil 	f->denominator = is_50hz ? 59 : 10;
1478e25cb200SHans Verkuil 
1479e25cb200SHans Verkuil 	return 0;
1480e25cb200SHans Verkuil }
1481e25cb200SHans Verkuil 
vidioc_g_selection(struct file * file,void * priv,struct v4l2_selection * s)1482ee10dc36SHans Verkuil static int vidioc_g_selection(struct file *file, void *priv,
1483ee10dc36SHans Verkuil 			      struct v4l2_selection *s)
1484ee10dc36SHans Verkuil {
14857c617138SHans Verkuil 	struct cx231xx *dev = video_drvdata(file);
1486ee10dc36SHans Verkuil 
1487ee10dc36SHans Verkuil 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1488ee10dc36SHans Verkuil 		return -EINVAL;
1489ee10dc36SHans Verkuil 
1490ee10dc36SHans Verkuil 	switch (s->target) {
1491ee10dc36SHans Verkuil 	case V4L2_SEL_TGT_CROP_BOUNDS:
1492ee10dc36SHans Verkuil 	case V4L2_SEL_TGT_CROP_DEFAULT:
1493ee10dc36SHans Verkuil 		s->r.left = 0;
1494ee10dc36SHans Verkuil 		s->r.top = 0;
1495ee10dc36SHans Verkuil 		s->r.width = dev->ts1.width;
1496ee10dc36SHans Verkuil 		s->r.height = dev->ts1.height;
1497ee10dc36SHans Verkuil 		break;
1498ee10dc36SHans Verkuil 	default:
1499ee10dc36SHans Verkuil 		return -EINVAL;
1500ee10dc36SHans Verkuil 	}
1501ee10dc36SHans Verkuil 	return 0;
1502ee10dc36SHans Verkuil }
1503ee10dc36SHans Verkuil 
vidioc_g_std(struct file * file,void * fh0,v4l2_std_id * norm)15040c0d06caSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *fh0, v4l2_std_id *norm)
15050c0d06caSMauro Carvalho Chehab {
15067c617138SHans Verkuil 	struct cx231xx *dev = video_drvdata(file);
15070c0d06caSMauro Carvalho Chehab 
15080c0d06caSMauro Carvalho Chehab 	*norm = dev->encodernorm.id;
15090c0d06caSMauro Carvalho Chehab 	return 0;
15100c0d06caSMauro Carvalho Chehab }
15115b8acdc5SHans Verkuil 
vidioc_s_std(struct file * file,void * priv,v4l2_std_id id)1512314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
15130c0d06caSMauro Carvalho Chehab {
15147c617138SHans Verkuil 	struct cx231xx *dev = video_drvdata(file);
15150c0d06caSMauro Carvalho Chehab 	unsigned int i;
15160c0d06caSMauro Carvalho Chehab 
15170c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(cx231xx_tvnorms); i++)
1518314527acSHans Verkuil 		if (id & cx231xx_tvnorms[i].id)
15190c0d06caSMauro Carvalho Chehab 			break;
15200c0d06caSMauro Carvalho Chehab 	if (i == ARRAY_SIZE(cx231xx_tvnorms))
15210c0d06caSMauro Carvalho Chehab 		return -EINVAL;
15220c0d06caSMauro Carvalho Chehab 	dev->encodernorm = cx231xx_tvnorms[i];
15230c0d06caSMauro Carvalho Chehab 
15240c0d06caSMauro Carvalho Chehab 	if (dev->encodernorm.id & 0xb000) {
15250c0d06caSMauro Carvalho Chehab 		dprintk(3, "encodernorm set to NTSC\n");
15260c0d06caSMauro Carvalho Chehab 		dev->norm = V4L2_STD_NTSC;
15270c0d06caSMauro Carvalho Chehab 		dev->ts1.height = 480;
152888b6ffedSHans Verkuil 		cx2341x_handler_set_50hz(&dev->mpeg_ctrl_handler, false);
15290c0d06caSMauro Carvalho Chehab 	} else {
15300c0d06caSMauro Carvalho Chehab 		dprintk(3, "encodernorm set to PAL\n");
15310c0d06caSMauro Carvalho Chehab 		dev->norm = V4L2_STD_PAL_B;
15320c0d06caSMauro Carvalho Chehab 		dev->ts1.height = 576;
153388b6ffedSHans Verkuil 		cx2341x_handler_set_50hz(&dev->mpeg_ctrl_handler, true);
15340c0d06caSMauro Carvalho Chehab 	}
15358774bed9SLaurent Pinchart 	call_all(dev, video, s_std, dev->norm);
15360c0d06caSMauro Carvalho Chehab 	/* do mode control overrides */
15370c0d06caSMauro Carvalho Chehab 	cx231xx_do_mode_ctrl_overrides(dev);
15380c0d06caSMauro Carvalho Chehab 
15390c0d06caSMauro Carvalho Chehab 	dprintk(3, "exit vidioc_s_std() i=0x%x\n", i);
15400c0d06caSMauro Carvalho Chehab 	return 0;
15410c0d06caSMauro Carvalho Chehab }
15425b8acdc5SHans Verkuil 
vidioc_s_ctrl(struct file * file,void * priv,struct v4l2_control * ctl)15430c0d06caSMauro Carvalho Chehab static int vidioc_s_ctrl(struct file *file, void *priv,
15440c0d06caSMauro Carvalho Chehab 				struct v4l2_control *ctl)
15450c0d06caSMauro Carvalho Chehab {
15467c617138SHans Verkuil 	struct cx231xx *dev = video_drvdata(file);
1547d715758dSHans Verkuil 	struct v4l2_subdev *sd;
15485b8acdc5SHans Verkuil 
15490c0d06caSMauro Carvalho Chehab 	dprintk(3, "enter vidioc_s_ctrl()\n");
15500c0d06caSMauro Carvalho Chehab 	/* Update the A/V core */
1551d715758dSHans Verkuil 	v4l2_device_for_each_subdev(sd, &dev->v4l2_dev)
1552d715758dSHans Verkuil 		v4l2_s_ctrl(NULL, sd->ctrl_handler, ctl);
15530c0d06caSMauro Carvalho Chehab 	dprintk(3, "exit vidioc_s_ctrl()\n");
15540c0d06caSMauro Carvalho Chehab 	return 0;
15550c0d06caSMauro Carvalho Chehab }
15560c0d06caSMauro Carvalho Chehab 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)15570c0d06caSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
15580c0d06caSMauro Carvalho Chehab 					struct v4l2_fmtdesc *f)
15590c0d06caSMauro Carvalho Chehab {
15600c0d06caSMauro Carvalho Chehab 	if (f->index != 0)
15610c0d06caSMauro Carvalho Chehab 		return -EINVAL;
15620c0d06caSMauro Carvalho Chehab 
15630c0d06caSMauro Carvalho Chehab 	f->pixelformat = V4L2_PIX_FMT_MPEG;
15640c0d06caSMauro Carvalho Chehab 
15650c0d06caSMauro Carvalho Chehab 	return 0;
15660c0d06caSMauro Carvalho Chehab }
15670c0d06caSMauro Carvalho Chehab 
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)15680c0d06caSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
15690c0d06caSMauro Carvalho Chehab 				struct v4l2_format *f)
15700c0d06caSMauro Carvalho Chehab {
15717c617138SHans Verkuil 	struct cx231xx *dev = video_drvdata(file);
15725aa95991SHans Verkuil 
15730c0d06caSMauro Carvalho Chehab 	dprintk(3, "enter vidioc_g_fmt_vid_cap()\n");
15740c0d06caSMauro Carvalho Chehab 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
15750c0d06caSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = 0;
15765aa95991SHans Verkuil 	f->fmt.pix.sizeimage = mpeglines * mpeglinesize;
15775aa95991SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
15780c0d06caSMauro Carvalho Chehab 	f->fmt.pix.width = dev->ts1.width;
15790c0d06caSMauro Carvalho Chehab 	f->fmt.pix.height = dev->ts1.height;
15805aa95991SHans Verkuil 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
15815aa95991SHans Verkuil 	dprintk(1, "VIDIOC_G_FMT: w: %d, h: %d\n",
15825aa95991SHans Verkuil 		dev->ts1.width, dev->ts1.height);
15830c0d06caSMauro Carvalho Chehab 	dprintk(3, "exit vidioc_g_fmt_vid_cap()\n");
15840c0d06caSMauro Carvalho Chehab 	return 0;
15850c0d06caSMauro Carvalho Chehab }
15860c0d06caSMauro Carvalho Chehab 
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)15870c0d06caSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
15880c0d06caSMauro Carvalho Chehab 				struct v4l2_format *f)
15890c0d06caSMauro Carvalho Chehab {
15907c617138SHans Verkuil 	struct cx231xx *dev = video_drvdata(file);
15915aa95991SHans Verkuil 
15920c0d06caSMauro Carvalho Chehab 	dprintk(3, "enter vidioc_try_fmt_vid_cap()\n");
15930c0d06caSMauro Carvalho Chehab 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
15940c0d06caSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = 0;
15955aa95991SHans Verkuil 	f->fmt.pix.sizeimage = mpeglines * mpeglinesize;
15965aa95991SHans Verkuil 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
15975aa95991SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
15985aa95991SHans Verkuil 	dprintk(1, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
15995aa95991SHans Verkuil 		dev->ts1.width, dev->ts1.height);
16000c0d06caSMauro Carvalho Chehab 	dprintk(3, "exit vidioc_try_fmt_vid_cap()\n");
16010c0d06caSMauro Carvalho Chehab 	return 0;
16020c0d06caSMauro Carvalho Chehab }
16030c0d06caSMauro Carvalho Chehab 
vidioc_log_status(struct file * file,void * priv)16040c0d06caSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
16050c0d06caSMauro Carvalho Chehab {
16067c617138SHans Verkuil 	struct cx231xx *dev = video_drvdata(file);
16070c0d06caSMauro Carvalho Chehab 
16080c0d06caSMauro Carvalho Chehab 	call_all(dev, core, log_status);
160988b6ffedSHans Verkuil 	return v4l2_ctrl_log_status(file, priv);
16100c0d06caSMauro Carvalho Chehab }
16110c0d06caSMauro Carvalho Chehab 
1612ff05c984SBhumika Goyal static const struct v4l2_file_operations mpeg_fops = {
16130c0d06caSMauro Carvalho Chehab 	.owner	       = THIS_MODULE,
16147c617138SHans Verkuil 	.open	       = v4l2_fh_open,
16157c617138SHans Verkuil 	.release       = vb2_fop_release,
16167c617138SHans Verkuil 	.read	       = vb2_fop_read,
16177c617138SHans Verkuil 	.poll          = vb2_fop_poll,
16187c617138SHans Verkuil 	.mmap	       = vb2_fop_mmap,
16191265f080SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
16200c0d06caSMauro Carvalho Chehab };
16210c0d06caSMauro Carvalho Chehab 
16220c0d06caSMauro Carvalho Chehab static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
16230c0d06caSMauro Carvalho Chehab 	.vidioc_s_std		 = vidioc_s_std,
16240c0d06caSMauro Carvalho Chehab 	.vidioc_g_std		 = vidioc_g_std,
1625b86d1544SHans Verkuil 	.vidioc_g_tuner          = cx231xx_g_tuner,
1626b86d1544SHans Verkuil 	.vidioc_s_tuner          = cx231xx_s_tuner,
1627b86d1544SHans Verkuil 	.vidioc_g_frequency      = cx231xx_g_frequency,
1628b86d1544SHans Verkuil 	.vidioc_s_frequency      = cx231xx_s_frequency,
1629b86d1544SHans Verkuil 	.vidioc_enum_input	 = cx231xx_enum_input,
1630b86d1544SHans Verkuil 	.vidioc_g_input		 = cx231xx_g_input,
1631b86d1544SHans Verkuil 	.vidioc_s_input		 = cx231xx_s_input,
16320c0d06caSMauro Carvalho Chehab 	.vidioc_s_ctrl		 = vidioc_s_ctrl,
16335200ab6aSHans Verkuil 	.vidioc_g_pixelaspect	 = vidioc_g_pixelaspect,
1634ee10dc36SHans Verkuil 	.vidioc_g_selection	 = vidioc_g_selection,
1635bc08734cSHans Verkuil 	.vidioc_querycap	 = cx231xx_querycap,
16360c0d06caSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
16370c0d06caSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap	 = vidioc_g_fmt_vid_cap,
16380c0d06caSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap	 = vidioc_try_fmt_vid_cap,
16393f926e32SHans Verkuil 	.vidioc_s_fmt_vid_cap	 = vidioc_try_fmt_vid_cap,
16407c617138SHans Verkuil 	.vidioc_reqbufs		 = vb2_ioctl_reqbufs,
16417c617138SHans Verkuil 	.vidioc_querybuf	 = vb2_ioctl_querybuf,
16427c617138SHans Verkuil 	.vidioc_qbuf		 = vb2_ioctl_qbuf,
16437c617138SHans Verkuil 	.vidioc_dqbuf		 = vb2_ioctl_dqbuf,
16447c617138SHans Verkuil 	.vidioc_streamon	 = vb2_ioctl_streamon,
16457c617138SHans Verkuil 	.vidioc_streamoff	 = vb2_ioctl_streamoff,
16460c0d06caSMauro Carvalho Chehab 	.vidioc_log_status	 = vidioc_log_status,
16470c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1648b86d1544SHans Verkuil 	.vidioc_g_register	 = cx231xx_g_register,
1649b86d1544SHans Verkuil 	.vidioc_s_register	 = cx231xx_s_register,
16500c0d06caSMauro Carvalho Chehab #endif
165188b6ffedSHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
165288b6ffedSHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
16530c0d06caSMauro Carvalho Chehab };
16540c0d06caSMauro Carvalho Chehab 
16550c0d06caSMauro Carvalho Chehab static struct video_device cx231xx_mpeg_template = {
16560c0d06caSMauro Carvalho Chehab 	.name          = "cx231xx",
16570c0d06caSMauro Carvalho Chehab 	.fops          = &mpeg_fops,
16580c0d06caSMauro Carvalho Chehab 	.ioctl_ops     = &mpeg_ioctl_ops,
16590c0d06caSMauro Carvalho Chehab 	.minor         = -1,
166088b6ffedSHans Verkuil 	.tvnorms       = V4L2_STD_ALL,
16610c0d06caSMauro Carvalho Chehab };
16620c0d06caSMauro Carvalho Chehab 
cx231xx_417_unregister(struct cx231xx * dev)16630c0d06caSMauro Carvalho Chehab void cx231xx_417_unregister(struct cx231xx *dev)
16640c0d06caSMauro Carvalho Chehab {
16650c0d06caSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
16660c0d06caSMauro Carvalho Chehab 	dprintk(3, "%s()\n", __func__);
16670c0d06caSMauro Carvalho Chehab 
166860acf187SHans Verkuil 	if (video_is_registered(&dev->v4l_device)) {
166960acf187SHans Verkuil 		video_unregister_device(&dev->v4l_device);
167088b6ffedSHans Verkuil 		v4l2_ctrl_handler_free(&dev->mpeg_ctrl_handler.hdl);
16710c0d06caSMauro Carvalho Chehab 	}
16720c0d06caSMauro Carvalho Chehab }
16730c0d06caSMauro Carvalho Chehab 
cx231xx_s_video_encoding(struct cx2341x_handler * cxhdl,u32 val)167488b6ffedSHans Verkuil static int cx231xx_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
167588b6ffedSHans Verkuil {
167688b6ffedSHans Verkuil 	struct cx231xx *dev = container_of(cxhdl, struct cx231xx, mpeg_ctrl_handler);
167788b6ffedSHans Verkuil 	int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
1678ebf984bbSHans Verkuil 	struct v4l2_subdev_format format = {
1679ebf984bbSHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
1680ebf984bbSHans Verkuil 	};
168188b6ffedSHans Verkuil 
168288b6ffedSHans Verkuil 	/* fix videodecoder resolution */
1683ebf984bbSHans Verkuil 	format.format.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
1684ebf984bbSHans Verkuil 	format.format.height = cxhdl->height;
1685ebf984bbSHans Verkuil 	format.format.code = MEDIA_BUS_FMT_FIXED;
1686ebf984bbSHans Verkuil 	v4l2_subdev_call(dev->sd_cx25840, pad, set_fmt, NULL, &format);
168788b6ffedSHans Verkuil 	return 0;
168888b6ffedSHans Verkuil }
168988b6ffedSHans Verkuil 
cx231xx_s_audio_sampling_freq(struct cx2341x_handler * cxhdl,u32 idx)169088b6ffedSHans Verkuil static int cx231xx_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
169188b6ffedSHans Verkuil {
169288b6ffedSHans Verkuil 	static const u32 freqs[3] = { 44100, 48000, 32000 };
169388b6ffedSHans Verkuil 	struct cx231xx *dev = container_of(cxhdl, struct cx231xx, mpeg_ctrl_handler);
169488b6ffedSHans Verkuil 
169588b6ffedSHans Verkuil 	/* The audio clock of the digitizer must match the codec sample
169688b6ffedSHans Verkuil 	   rate otherwise you get some very strange effects. */
169788b6ffedSHans Verkuil 	if (idx < ARRAY_SIZE(freqs))
169888b6ffedSHans Verkuil 		call_all(dev, audio, s_clock_freq, freqs[idx]);
169988b6ffedSHans Verkuil 	return 0;
170088b6ffedSHans Verkuil }
170188b6ffedSHans Verkuil 
1702083206fcSJulia Lawall static const struct cx2341x_handler_ops cx231xx_ops = {
170388b6ffedSHans Verkuil 	/* needed for the video clock freq */
170488b6ffedSHans Verkuil 	.s_audio_sampling_freq = cx231xx_s_audio_sampling_freq,
170588b6ffedSHans Verkuil 	/* needed for setting up the video resolution */
170688b6ffedSHans Verkuil 	.s_video_encoding = cx231xx_s_video_encoding,
170788b6ffedSHans Verkuil };
170888b6ffedSHans Verkuil 
cx231xx_video_dev_init(struct cx231xx * dev,struct usb_device * usbdev,struct video_device * vfd,const struct video_device * template,const char * type)170960acf187SHans Verkuil static void cx231xx_video_dev_init(
17100c0d06caSMauro Carvalho Chehab 	struct cx231xx *dev,
17110c0d06caSMauro Carvalho Chehab 	struct usb_device *usbdev,
171260acf187SHans Verkuil 	struct video_device *vfd,
171360acf187SHans Verkuil 	const struct video_device *template,
171460acf187SHans Verkuil 	const char *type)
17150c0d06caSMauro Carvalho Chehab {
17160c0d06caSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
17170c0d06caSMauro Carvalho Chehab 	*vfd = *template;
17180c0d06caSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
17190c0d06caSMauro Carvalho Chehab 		type, cx231xx_boards[dev->model].name);
17200c0d06caSMauro Carvalho Chehab 
17210c0d06caSMauro Carvalho Chehab 	vfd->v4l2_dev = &dev->v4l2_dev;
17221265f080SHans Verkuil 	vfd->lock = &dev->lock;
172360acf187SHans Verkuil 	vfd->release = video_device_release_empty;
172488b6ffedSHans Verkuil 	vfd->ctrl_handler = &dev->mpeg_ctrl_handler.hdl;
1725b86d1544SHans Verkuil 	video_set_drvdata(vfd, dev);
1726b86d1544SHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT) {
1727b86d1544SHans Verkuil 		v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
1728b86d1544SHans Verkuil 		v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
1729b86d1544SHans Verkuil 		v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
1730b86d1544SHans Verkuil 		v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
1731b86d1544SHans Verkuil 	}
17320c0d06caSMauro Carvalho Chehab }
17330c0d06caSMauro Carvalho Chehab 
cx231xx_417_register(struct cx231xx * dev)17340c0d06caSMauro Carvalho Chehab int cx231xx_417_register(struct cx231xx *dev)
17350c0d06caSMauro Carvalho Chehab {
17360c0d06caSMauro Carvalho Chehab 	/* FIXME: Port1 hardcoded here */
1737414953b4SColin Ian King 	int err;
17380c0d06caSMauro Carvalho Chehab 	struct cx231xx_tsport *tsport = &dev->ts1;
17397c617138SHans Verkuil 	struct vb2_queue *q;
17400c0d06caSMauro Carvalho Chehab 
17410c0d06caSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
17420c0d06caSMauro Carvalho Chehab 
17430c0d06caSMauro Carvalho Chehab 	/* Set default TV standard */
17440c0d06caSMauro Carvalho Chehab 	dev->encodernorm = cx231xx_tvnorms[0];
17450c0d06caSMauro Carvalho Chehab 
17460c0d06caSMauro Carvalho Chehab 	if (dev->encodernorm.id & V4L2_STD_525_60)
17470c0d06caSMauro Carvalho Chehab 		tsport->height = 480;
17480c0d06caSMauro Carvalho Chehab 	else
17490c0d06caSMauro Carvalho Chehab 		tsport->height = 576;
17500c0d06caSMauro Carvalho Chehab 
17510c0d06caSMauro Carvalho Chehab 	tsport->width = 720;
175288b6ffedSHans Verkuil 	err = cx2341x_handler_init(&dev->mpeg_ctrl_handler, 50);
175388b6ffedSHans Verkuil 	if (err) {
175488b6ffedSHans Verkuil 		dprintk(3, "%s: can't init cx2341x controls\n", dev->name);
175588b6ffedSHans Verkuil 		return err;
175688b6ffedSHans Verkuil 	}
175788b6ffedSHans Verkuil 	dev->mpeg_ctrl_handler.func = cx231xx_mbox_func;
175888b6ffedSHans Verkuil 	dev->mpeg_ctrl_handler.priv = dev;
175988b6ffedSHans Verkuil 	dev->mpeg_ctrl_handler.ops = &cx231xx_ops;
176088b6ffedSHans Verkuil 	if (dev->sd_cx25840)
176188b6ffedSHans Verkuil 		v4l2_ctrl_add_handler(&dev->mpeg_ctrl_handler.hdl,
17628674fd63SHans Verkuil 				dev->sd_cx25840->ctrl_handler, NULL, true);
176388b6ffedSHans Verkuil 	if (dev->mpeg_ctrl_handler.hdl.error) {
176488b6ffedSHans Verkuil 		err = dev->mpeg_ctrl_handler.hdl.error;
176588b6ffedSHans Verkuil 		dprintk(3, "%s: can't add cx25840 controls\n", dev->name);
176688b6ffedSHans Verkuil 		v4l2_ctrl_handler_free(&dev->mpeg_ctrl_handler.hdl);
176788b6ffedSHans Verkuil 		return err;
176888b6ffedSHans Verkuil 	}
17690c0d06caSMauro Carvalho Chehab 	dev->norm = V4L2_STD_NTSC;
17700c0d06caSMauro Carvalho Chehab 
177188b6ffedSHans Verkuil 	dev->mpeg_ctrl_handler.port = CX2341X_PORT_SERIAL;
177288b6ffedSHans Verkuil 	cx2341x_handler_set_50hz(&dev->mpeg_ctrl_handler, false);
17730c0d06caSMauro Carvalho Chehab 
17740c0d06caSMauro Carvalho Chehab 	/* Allocate and initialize V4L video device */
177560acf187SHans Verkuil 	cx231xx_video_dev_init(dev, dev->udev,
177660acf187SHans Verkuil 			&dev->v4l_device, &cx231xx_mpeg_template, "mpeg");
17777c617138SHans Verkuil 	q = &dev->mpegq;
17787c617138SHans Verkuil 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
17797c617138SHans Verkuil 	q->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF | VB2_READ;
17807c617138SHans Verkuil 	q->drv_priv = dev;
17817c617138SHans Verkuil 	q->buf_struct_size = sizeof(struct cx231xx_buffer);
17827c617138SHans Verkuil 	q->ops = &cx231xx_video_qops;
17837c617138SHans Verkuil 	q->mem_ops = &vb2_vmalloc_memops;
17847c617138SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
178580c2b40aSBenjamin Gaignard 	q->min_queued_buffers = 1;
17867c617138SHans Verkuil 	q->lock = &dev->lock;
17877c617138SHans Verkuil 	err = vb2_queue_init(q);
17887c617138SHans Verkuil 	if (err)
17897c617138SHans Verkuil 		return err;
17907c617138SHans Verkuil 	dev->v4l_device.queue = q;
17917c617138SHans Verkuil 
179260acf187SHans Verkuil 	err = video_register_device(&dev->v4l_device,
17937fbbbc78SHans Verkuil 		VFL_TYPE_VIDEO, -1);
17940c0d06caSMauro Carvalho Chehab 	if (err < 0) {
17950c0d06caSMauro Carvalho Chehab 		dprintk(3, "%s: can't register mpeg device\n", dev->name);
179688b6ffedSHans Verkuil 		v4l2_ctrl_handler_free(&dev->mpeg_ctrl_handler.hdl);
17970c0d06caSMauro Carvalho Chehab 		return err;
17980c0d06caSMauro Carvalho Chehab 	}
17990c0d06caSMauro Carvalho Chehab 
18000c0d06caSMauro Carvalho Chehab 	dprintk(3, "%s: registered device video%d [mpeg]\n",
180160acf187SHans Verkuil 	       dev->name, dev->v4l_device.num);
18020c0d06caSMauro Carvalho Chehab 
18030c0d06caSMauro Carvalho Chehab 	return 0;
18040c0d06caSMauro Carvalho Chehab }
18050c0d06caSMauro Carvalho Chehab 
18060c0d06caSMauro Carvalho Chehab MODULE_FIRMWARE(CX231xx_FIRM_IMAGE_NAME);
1807