xref: /openbmc/linux/drivers/media/pci/cx88/cx88-blackbird.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b285192aSMauro Carvalho Chehab /*
3b285192aSMauro Carvalho Chehab  *  Support for a cx23416 mpeg encoder via cx2388x host port.
4b285192aSMauro Carvalho Chehab  *  "blackbird" reference design.
5b285192aSMauro Carvalho Chehab  *
6b285192aSMauro Carvalho Chehab  *    (c) 2004 Jelle Foks <jelle@foks.us>
7b285192aSMauro Carvalho Chehab  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
8b285192aSMauro Carvalho Chehab  *
932590819SMauro Carvalho Chehab  *    (c) 2005-2006 Mauro Carvalho Chehab <mchehab@kernel.org>
10b285192aSMauro Carvalho Chehab  *        - video_ioctl2 conversion
11b285192aSMauro Carvalho Chehab  *
12b285192aSMauro Carvalho Chehab  *  Includes parts from the ivtv driver <http://sourceforge.net/projects/ivtv/>
13b285192aSMauro Carvalho Chehab  */
14b285192aSMauro Carvalho Chehab 
1565bc2fe8SMauro Carvalho Chehab #include "cx88.h"
1665bc2fe8SMauro Carvalho Chehab 
17b285192aSMauro Carvalho Chehab #include <linux/module.h>
18b285192aSMauro Carvalho Chehab #include <linux/init.h>
19b285192aSMauro Carvalho Chehab #include <linux/slab.h>
20b285192aSMauro Carvalho Chehab #include <linux/fs.h>
21b285192aSMauro Carvalho Chehab #include <linux/delay.h>
22b285192aSMauro Carvalho Chehab #include <linux/device.h>
23b285192aSMauro Carvalho Chehab #include <linux/firmware.h>
24b285192aSMauro Carvalho Chehab #include <media/v4l2-common.h>
25b285192aSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
26b285192aSMauro Carvalho Chehab #include <media/v4l2-event.h>
27d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/cx2341x.h>
28b285192aSMauro Carvalho Chehab 
29b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
30b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Jelle Foks <jelle@foks.us>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
31513dbd35SMauro Carvalho Chehab MODULE_LICENSE("GPL v2");
32b285192aSMauro Carvalho Chehab MODULE_VERSION(CX88_VERSION);
33b285192aSMauro Carvalho Chehab 
34b285192aSMauro Carvalho Chehab static unsigned int debug;
35b285192aSMauro Carvalho Chehab module_param(debug, int, 0644);
36b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(debug, "enable debug messages [blackbird]");
37b285192aSMauro Carvalho Chehab 
38db613710SMauro Carvalho Chehab #define dprintk(level, fmt, arg...) do {				\
39db613710SMauro Carvalho Chehab 	if (debug + 1 > level)						\
4065bc2fe8SMauro Carvalho Chehab 		printk(KERN_DEBUG pr_fmt("%s: blackbird:" fmt),		\
4165bc2fe8SMauro Carvalho Chehab 			__func__, ##arg);				\
42db613710SMauro Carvalho Chehab } while (0)
43b285192aSMauro Carvalho Chehab 
44b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
45b285192aSMauro Carvalho Chehab 
46b285192aSMauro Carvalho Chehab #define BLACKBIRD_FIRM_IMAGE_SIZE 376836
47b285192aSMauro Carvalho Chehab 
48b285192aSMauro Carvalho Chehab /* defines below are from ivtv-driver.h */
49b285192aSMauro Carvalho Chehab 
50b285192aSMauro Carvalho Chehab #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
51b285192aSMauro Carvalho Chehab 
52b285192aSMauro Carvalho Chehab /* Firmware API commands */
53b285192aSMauro Carvalho Chehab #define IVTV_API_STD_TIMEOUT 500
54b285192aSMauro Carvalho Chehab 
55b285192aSMauro Carvalho Chehab enum blackbird_capture_type {
56b285192aSMauro Carvalho Chehab 	BLACKBIRD_MPEG_CAPTURE,
57b285192aSMauro Carvalho Chehab 	BLACKBIRD_RAW_CAPTURE,
58b285192aSMauro Carvalho Chehab 	BLACKBIRD_RAW_PASSTHRU_CAPTURE
59b285192aSMauro Carvalho Chehab };
60399426caSMauro Carvalho Chehab 
61b285192aSMauro Carvalho Chehab enum blackbird_capture_bits {
62b285192aSMauro Carvalho Chehab 	BLACKBIRD_RAW_BITS_NONE             = 0x00,
63b285192aSMauro Carvalho Chehab 	BLACKBIRD_RAW_BITS_YUV_CAPTURE      = 0x01,
64b285192aSMauro Carvalho Chehab 	BLACKBIRD_RAW_BITS_PCM_CAPTURE      = 0x02,
65b285192aSMauro Carvalho Chehab 	BLACKBIRD_RAW_BITS_VBI_CAPTURE      = 0x04,
66b285192aSMauro Carvalho Chehab 	BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
67b285192aSMauro Carvalho Chehab 	BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE  = 0x10
68b285192aSMauro Carvalho Chehab };
69399426caSMauro Carvalho Chehab 
70b285192aSMauro Carvalho Chehab enum blackbird_capture_end {
71b285192aSMauro Carvalho Chehab 	BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
72b285192aSMauro Carvalho Chehab 	BLACKBIRD_END_NOW, /* stop immediately, no irq */
73b285192aSMauro Carvalho Chehab };
74399426caSMauro Carvalho Chehab 
75b285192aSMauro Carvalho Chehab enum blackbird_framerate {
76b285192aSMauro Carvalho Chehab 	BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
77b285192aSMauro Carvalho Chehab 	BLACKBIRD_FRAMERATE_PAL_25   /* PAL: 25fps */
78b285192aSMauro Carvalho Chehab };
79399426caSMauro Carvalho Chehab 
80b285192aSMauro Carvalho Chehab enum blackbird_stream_port {
81b285192aSMauro Carvalho Chehab 	BLACKBIRD_OUTPUT_PORT_MEMORY,
82b285192aSMauro Carvalho Chehab 	BLACKBIRD_OUTPUT_PORT_STREAMING,
83b285192aSMauro Carvalho Chehab 	BLACKBIRD_OUTPUT_PORT_SERIAL
84b285192aSMauro Carvalho Chehab };
85399426caSMauro Carvalho Chehab 
86b285192aSMauro Carvalho Chehab enum blackbird_data_xfer_status {
87b285192aSMauro Carvalho Chehab 	BLACKBIRD_MORE_BUFFERS_FOLLOW,
88b285192aSMauro Carvalho Chehab 	BLACKBIRD_LAST_BUFFER,
89b285192aSMauro Carvalho Chehab };
90399426caSMauro Carvalho Chehab 
91b285192aSMauro Carvalho Chehab enum blackbird_picture_mask {
92b285192aSMauro Carvalho Chehab 	BLACKBIRD_PICTURE_MASK_NONE,
93b285192aSMauro Carvalho Chehab 	BLACKBIRD_PICTURE_MASK_I_FRAMES,
94b285192aSMauro Carvalho Chehab 	BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
95b285192aSMauro Carvalho Chehab 	BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
96b285192aSMauro Carvalho Chehab };
97399426caSMauro Carvalho Chehab 
98b285192aSMauro Carvalho Chehab enum blackbird_vbi_mode_bits {
99b285192aSMauro Carvalho Chehab 	BLACKBIRD_VBI_BITS_SLICED,
100b285192aSMauro Carvalho Chehab 	BLACKBIRD_VBI_BITS_RAW,
101b285192aSMauro Carvalho Chehab };
102399426caSMauro Carvalho Chehab 
103b285192aSMauro Carvalho Chehab enum blackbird_vbi_insertion_bits {
104b285192aSMauro Carvalho Chehab 	BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
105b285192aSMauro Carvalho Chehab 	BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
106b285192aSMauro Carvalho Chehab 	BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
107b285192aSMauro Carvalho Chehab 	BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
108b285192aSMauro Carvalho Chehab 	BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
109b285192aSMauro Carvalho Chehab };
110399426caSMauro Carvalho Chehab 
111b285192aSMauro Carvalho Chehab enum blackbird_dma_unit {
112b285192aSMauro Carvalho Chehab 	BLACKBIRD_DMA_BYTES,
113b285192aSMauro Carvalho Chehab 	BLACKBIRD_DMA_FRAMES,
114b285192aSMauro Carvalho Chehab };
115399426caSMauro Carvalho Chehab 
116b285192aSMauro Carvalho Chehab enum blackbird_dma_transfer_status_bits {
117b285192aSMauro Carvalho Chehab 	BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
118b285192aSMauro Carvalho Chehab 	BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
119b285192aSMauro Carvalho Chehab 	BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
120b285192aSMauro Carvalho Chehab };
121399426caSMauro Carvalho Chehab 
122b285192aSMauro Carvalho Chehab enum blackbird_pause {
123b285192aSMauro Carvalho Chehab 	BLACKBIRD_PAUSE_ENCODING,
124b285192aSMauro Carvalho Chehab 	BLACKBIRD_RESUME_ENCODING,
125b285192aSMauro Carvalho Chehab };
126399426caSMauro Carvalho Chehab 
127b285192aSMauro Carvalho Chehab enum blackbird_copyright {
128b285192aSMauro Carvalho Chehab 	BLACKBIRD_COPYRIGHT_OFF,
129b285192aSMauro Carvalho Chehab 	BLACKBIRD_COPYRIGHT_ON,
130b285192aSMauro Carvalho Chehab };
131399426caSMauro Carvalho Chehab 
132b285192aSMauro Carvalho Chehab enum blackbird_notification_type {
133b285192aSMauro Carvalho Chehab 	BLACKBIRD_NOTIFICATION_REFRESH,
134b285192aSMauro Carvalho Chehab };
135399426caSMauro Carvalho Chehab 
136b285192aSMauro Carvalho Chehab enum blackbird_notification_status {
137b285192aSMauro Carvalho Chehab 	BLACKBIRD_NOTIFICATION_OFF,
138b285192aSMauro Carvalho Chehab 	BLACKBIRD_NOTIFICATION_ON,
139b285192aSMauro Carvalho Chehab };
140399426caSMauro Carvalho Chehab 
141b285192aSMauro Carvalho Chehab enum blackbird_notification_mailbox {
142b285192aSMauro Carvalho Chehab 	BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
143b285192aSMauro Carvalho Chehab };
144399426caSMauro Carvalho Chehab 
145b285192aSMauro Carvalho Chehab enum blackbird_field1_lines {
146b285192aSMauro Carvalho Chehab 	BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
147b285192aSMauro Carvalho Chehab 	BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
148b285192aSMauro Carvalho Chehab 	BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
149b285192aSMauro Carvalho Chehab };
150399426caSMauro Carvalho Chehab 
151b285192aSMauro Carvalho Chehab enum blackbird_field2_lines {
152b285192aSMauro Carvalho Chehab 	BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
153b285192aSMauro Carvalho Chehab 	BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
154b285192aSMauro Carvalho Chehab 	BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
155b285192aSMauro Carvalho Chehab };
156399426caSMauro Carvalho Chehab 
157b285192aSMauro Carvalho Chehab enum blackbird_custom_data_type {
158b285192aSMauro Carvalho Chehab 	BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
159b285192aSMauro Carvalho Chehab 	BLACKBIRD_CUSTOM_PRIVATE_PACKET,
160b285192aSMauro Carvalho Chehab };
161399426caSMauro Carvalho Chehab 
162b285192aSMauro Carvalho Chehab enum blackbird_mute {
163b285192aSMauro Carvalho Chehab 	BLACKBIRD_UNMUTE,
164b285192aSMauro Carvalho Chehab 	BLACKBIRD_MUTE,
165b285192aSMauro Carvalho Chehab };
166399426caSMauro Carvalho Chehab 
167b285192aSMauro Carvalho Chehab enum blackbird_mute_video_mask {
168b285192aSMauro Carvalho Chehab 	BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
169b285192aSMauro Carvalho Chehab 	BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
170b285192aSMauro Carvalho Chehab 	BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
171b285192aSMauro Carvalho Chehab };
172399426caSMauro Carvalho Chehab 
173b285192aSMauro Carvalho Chehab enum blackbird_mute_video_shift {
174b285192aSMauro Carvalho Chehab 	BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
175b285192aSMauro Carvalho Chehab 	BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
176b285192aSMauro Carvalho Chehab 	BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
177b285192aSMauro Carvalho Chehab };
178b285192aSMauro Carvalho Chehab 
179b285192aSMauro Carvalho Chehab /* Registers */
180b285192aSMauro Carvalho Chehab #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
181b285192aSMauro Carvalho Chehab #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
182b285192aSMauro Carvalho Chehab #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
183b285192aSMauro Carvalho Chehab #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
184b285192aSMauro Carvalho Chehab #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
185b285192aSMauro Carvalho Chehab #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
186b285192aSMauro Carvalho Chehab 
187b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
188b285192aSMauro Carvalho Chehab 
host_setup(struct cx88_core * core)189b285192aSMauro Carvalho Chehab static void host_setup(struct cx88_core *core)
190b285192aSMauro Carvalho Chehab {
191b285192aSMauro Carvalho Chehab 	/* toggle reset of the host */
192b285192aSMauro Carvalho Chehab 	cx_write(MO_GPHST_SOFT_RST, 1);
193b285192aSMauro Carvalho Chehab 	udelay(100);
194b285192aSMauro Carvalho Chehab 	cx_write(MO_GPHST_SOFT_RST, 0);
195b285192aSMauro Carvalho Chehab 	udelay(100);
196b285192aSMauro Carvalho Chehab 
197b285192aSMauro Carvalho Chehab 	/* host port setup */
198b285192aSMauro Carvalho Chehab 	cx_write(MO_GPHST_WSC, 0x44444444U);
199b285192aSMauro Carvalho Chehab 	cx_write(MO_GPHST_XFR, 0);
200b285192aSMauro Carvalho Chehab 	cx_write(MO_GPHST_WDTH, 15);
201b285192aSMauro Carvalho Chehab 	cx_write(MO_GPHST_HDSHK, 0);
202b285192aSMauro Carvalho Chehab 	cx_write(MO_GPHST_MUX16, 0x44448888U);
203b285192aSMauro Carvalho Chehab 	cx_write(MO_GPHST_MODE, 0);
204b285192aSMauro Carvalho Chehab }
205b285192aSMauro Carvalho Chehab 
206b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
207b285192aSMauro Carvalho Chehab 
208b285192aSMauro Carvalho Chehab #define P1_MDATA0 0x390000
209b285192aSMauro Carvalho Chehab #define P1_MDATA1 0x390001
210b285192aSMauro Carvalho Chehab #define P1_MDATA2 0x390002
211b285192aSMauro Carvalho Chehab #define P1_MDATA3 0x390003
212b285192aSMauro Carvalho Chehab #define P1_MADDR2 0x390004
213b285192aSMauro Carvalho Chehab #define P1_MADDR1 0x390005
214b285192aSMauro Carvalho Chehab #define P1_MADDR0 0x390006
215b285192aSMauro Carvalho Chehab #define P1_RDATA0 0x390008
216b285192aSMauro Carvalho Chehab #define P1_RDATA1 0x390009
217b285192aSMauro Carvalho Chehab #define P1_RDATA2 0x39000A
218b285192aSMauro Carvalho Chehab #define P1_RDATA3 0x39000B
219b285192aSMauro Carvalho Chehab #define P1_RADDR0 0x39000C
220b285192aSMauro Carvalho Chehab #define P1_RADDR1 0x39000D
221b285192aSMauro Carvalho Chehab #define P1_RRDWR  0x39000E
222b285192aSMauro Carvalho Chehab 
wait_ready_gpio0_bit1(struct cx88_core * core,u32 state)223b285192aSMauro Carvalho Chehab static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
224b285192aSMauro Carvalho Chehab {
225b285192aSMauro Carvalho Chehab 	unsigned long timeout = jiffies + msecs_to_jiffies(1);
226b285192aSMauro Carvalho Chehab 	u32 gpio0, need;
227b285192aSMauro Carvalho Chehab 
228b285192aSMauro Carvalho Chehab 	need = state ? 2 : 0;
229b285192aSMauro Carvalho Chehab 	for (;;) {
230b285192aSMauro Carvalho Chehab 		gpio0 = cx_read(MO_GP0_IO) & 2;
231b285192aSMauro Carvalho Chehab 		if (need == gpio0)
232b285192aSMauro Carvalho Chehab 			return 0;
233b285192aSMauro Carvalho Chehab 		if (time_after(jiffies, timeout))
234b285192aSMauro Carvalho Chehab 			return -1;
235b285192aSMauro Carvalho Chehab 		udelay(1);
236b285192aSMauro Carvalho Chehab 	}
237b285192aSMauro Carvalho Chehab }
238b285192aSMauro Carvalho Chehab 
memory_write(struct cx88_core * core,u32 address,u32 value)239b285192aSMauro Carvalho Chehab static int memory_write(struct cx88_core *core, u32 address, u32 value)
240b285192aSMauro Carvalho Chehab {
241b285192aSMauro Carvalho Chehab 	/* Warning: address is dword address (4 bytes) */
242b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MDATA0, (unsigned int)value);
243b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
244b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
245b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
246b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
247b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
248b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MADDR0, (unsigned int)address);
249b285192aSMauro Carvalho Chehab 	cx_read(P1_MDATA0);
250b285192aSMauro Carvalho Chehab 	cx_read(P1_MADDR0);
251b285192aSMauro Carvalho Chehab 
252b285192aSMauro Carvalho Chehab 	return wait_ready_gpio0_bit1(core, 1);
253b285192aSMauro Carvalho Chehab }
254b285192aSMauro Carvalho Chehab 
memory_read(struct cx88_core * core,u32 address,u32 * value)255b285192aSMauro Carvalho Chehab static int memory_read(struct cx88_core *core, u32 address, u32 *value)
256b285192aSMauro Carvalho Chehab {
257b285192aSMauro Carvalho Chehab 	int retval;
258b285192aSMauro Carvalho Chehab 	u32 val;
259b285192aSMauro Carvalho Chehab 
260b285192aSMauro Carvalho Chehab 	/* Warning: address is dword address (4 bytes) */
261b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
262b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
263b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MADDR0, (unsigned int)address);
264b285192aSMauro Carvalho Chehab 	cx_read(P1_MADDR0);
265b285192aSMauro Carvalho Chehab 
266b285192aSMauro Carvalho Chehab 	retval = wait_ready_gpio0_bit1(core, 1);
267b285192aSMauro Carvalho Chehab 
268b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MDATA3, 0);
269b285192aSMauro Carvalho Chehab 	val     = (unsigned char)cx_read(P1_MDATA3) << 24;
270b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MDATA2, 0);
271b285192aSMauro Carvalho Chehab 	val    |= (unsigned char)cx_read(P1_MDATA2) << 16;
272b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MDATA1, 0);
273b285192aSMauro Carvalho Chehab 	val    |= (unsigned char)cx_read(P1_MDATA1) << 8;
274b285192aSMauro Carvalho Chehab 	cx_writeb(P1_MDATA0, 0);
275b285192aSMauro Carvalho Chehab 	val    |= (unsigned char)cx_read(P1_MDATA0);
276b285192aSMauro Carvalho Chehab 
277b285192aSMauro Carvalho Chehab 	*value  = val;
278b285192aSMauro Carvalho Chehab 	return retval;
279b285192aSMauro Carvalho Chehab }
280b285192aSMauro Carvalho Chehab 
register_write(struct cx88_core * core,u32 address,u32 value)281b285192aSMauro Carvalho Chehab static int register_write(struct cx88_core *core, u32 address, u32 value)
282b285192aSMauro Carvalho Chehab {
283b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RDATA0, (unsigned int)value);
284b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
285b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
286b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
287b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RADDR0, (unsigned int)address);
288b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
289b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RRDWR, 1);
290b285192aSMauro Carvalho Chehab 	cx_read(P1_RDATA0);
291b285192aSMauro Carvalho Chehab 	cx_read(P1_RADDR0);
292b285192aSMauro Carvalho Chehab 
293b285192aSMauro Carvalho Chehab 	return wait_ready_gpio0_bit1(core, 1);
294b285192aSMauro Carvalho Chehab }
295b285192aSMauro Carvalho Chehab 
register_read(struct cx88_core * core,u32 address,u32 * value)296b285192aSMauro Carvalho Chehab static int register_read(struct cx88_core *core, u32 address, u32 *value)
297b285192aSMauro Carvalho Chehab {
298b285192aSMauro Carvalho Chehab 	int retval;
299b285192aSMauro Carvalho Chehab 	u32 val;
300b285192aSMauro Carvalho Chehab 
301b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RADDR0, (unsigned int)address);
302b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
303b285192aSMauro Carvalho Chehab 	cx_writeb(P1_RRDWR, 0);
304b285192aSMauro Carvalho Chehab 	cx_read(P1_RADDR0);
305b285192aSMauro Carvalho Chehab 
306b285192aSMauro Carvalho Chehab 	retval  = wait_ready_gpio0_bit1(core, 1);
307b285192aSMauro Carvalho Chehab 	val     = (unsigned char)cx_read(P1_RDATA0);
308b285192aSMauro Carvalho Chehab 	val    |= (unsigned char)cx_read(P1_RDATA1) << 8;
309b285192aSMauro Carvalho Chehab 	val    |= (unsigned char)cx_read(P1_RDATA2) << 16;
310b285192aSMauro Carvalho Chehab 	val    |= (unsigned char)cx_read(P1_RDATA3) << 24;
311b285192aSMauro Carvalho Chehab 
312b285192aSMauro Carvalho Chehab 	*value  = val;
313b285192aSMauro Carvalho Chehab 	return retval;
314b285192aSMauro Carvalho Chehab }
315b285192aSMauro Carvalho Chehab 
316b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
317b285192aSMauro Carvalho Chehab 
blackbird_mbox_func(void * priv,u32 command,int in,int out,u32 data[CX2341X_MBOX_MAX_DATA])318399426caSMauro Carvalho Chehab static int blackbird_mbox_func(void *priv, u32 command, int in,
319399426caSMauro Carvalho Chehab 			       int out, u32 data[CX2341X_MBOX_MAX_DATA])
320b285192aSMauro Carvalho Chehab {
321b285192aSMauro Carvalho Chehab 	struct cx8802_dev *dev = priv;
322b285192aSMauro Carvalho Chehab 	unsigned long timeout;
323b285192aSMauro Carvalho Chehab 	u32 value, flag, retval;
324b285192aSMauro Carvalho Chehab 	int i;
325b285192aSMauro Carvalho Chehab 
326b285192aSMauro Carvalho Chehab 	dprintk(1, "%s: 0x%X\n", __func__, command);
327b285192aSMauro Carvalho Chehab 
328399426caSMauro Carvalho Chehab 	/*
329399426caSMauro Carvalho Chehab 	 * this may not be 100% safe if we can't read any memory location
330399426caSMauro Carvalho Chehab 	 * without side effects
331399426caSMauro Carvalho Chehab 	 */
332b285192aSMauro Carvalho Chehab 	memory_read(dev->core, dev->mailbox - 4, &value);
333b285192aSMauro Carvalho Chehab 	if (value != 0x12345678) {
334399426caSMauro Carvalho Chehab 		dprintk(0,
335399426caSMauro Carvalho Chehab 			"Firmware and/or mailbox pointer not initialized or corrupted\n");
336eddd3263SHans Verkuil 		return -EIO;
337b285192aSMauro Carvalho Chehab 	}
338b285192aSMauro Carvalho Chehab 
339b285192aSMauro Carvalho Chehab 	memory_read(dev->core, dev->mailbox, &flag);
340b285192aSMauro Carvalho Chehab 	if (flag) {
341b285192aSMauro Carvalho Chehab 		dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
342eddd3263SHans Verkuil 		return -EIO;
343b285192aSMauro Carvalho Chehab 	}
344b285192aSMauro Carvalho Chehab 
345b285192aSMauro Carvalho Chehab 	flag |= 1; /* tell 'em we're working on it */
346b285192aSMauro Carvalho Chehab 	memory_write(dev->core, dev->mailbox, flag);
347b285192aSMauro Carvalho Chehab 
348b285192aSMauro Carvalho Chehab 	/* write command + args + fill remaining with zeros */
349b285192aSMauro Carvalho Chehab 	memory_write(dev->core, dev->mailbox + 1, command); /* command code */
350399426caSMauro Carvalho Chehab 	/* timeout */
351399426caSMauro Carvalho Chehab 	memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT);
352b285192aSMauro Carvalho Chehab 	for (i = 0; i < in; i++) {
353b285192aSMauro Carvalho Chehab 		memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
354b285192aSMauro Carvalho Chehab 		dprintk(1, "API Input %d = %d\n", i, data[i]);
355b285192aSMauro Carvalho Chehab 	}
356b285192aSMauro Carvalho Chehab 	for (; i < CX2341X_MBOX_MAX_DATA; i++)
357b285192aSMauro Carvalho Chehab 		memory_write(dev->core, dev->mailbox + 4 + i, 0);
358b285192aSMauro Carvalho Chehab 
359b285192aSMauro Carvalho Chehab 	flag |= 3; /* tell 'em we're done writing */
360b285192aSMauro Carvalho Chehab 	memory_write(dev->core, dev->mailbox, flag);
361b285192aSMauro Carvalho Chehab 
362b285192aSMauro Carvalho Chehab 	/* wait for firmware to handle the API command */
363b8f88416SHans Verkuil 	timeout = jiffies + msecs_to_jiffies(1000);
364b285192aSMauro Carvalho Chehab 	for (;;) {
365b285192aSMauro Carvalho Chehab 		memory_read(dev->core, dev->mailbox, &flag);
366b285192aSMauro Carvalho Chehab 		if (0 != (flag & 4))
367b285192aSMauro Carvalho Chehab 			break;
368b285192aSMauro Carvalho Chehab 		if (time_after(jiffies, timeout)) {
369eddd3263SHans Verkuil 			dprintk(0, "ERROR: API Mailbox timeout %x\n", command);
370eddd3263SHans Verkuil 			return -EIO;
371b285192aSMauro Carvalho Chehab 		}
372b285192aSMauro Carvalho Chehab 		udelay(10);
373b285192aSMauro Carvalho Chehab 	}
374b285192aSMauro Carvalho Chehab 
375b285192aSMauro Carvalho Chehab 	/* read output values */
376b285192aSMauro Carvalho Chehab 	for (i = 0; i < out; i++) {
377b285192aSMauro Carvalho Chehab 		memory_read(dev->core, dev->mailbox + 4 + i, data + i);
378b285192aSMauro Carvalho Chehab 		dprintk(1, "API Output %d = %d\n", i, data[i]);
379b285192aSMauro Carvalho Chehab 	}
380b285192aSMauro Carvalho Chehab 
381b285192aSMauro Carvalho Chehab 	memory_read(dev->core, dev->mailbox + 2, &retval);
382b285192aSMauro Carvalho Chehab 	dprintk(1, "API result = %d\n", retval);
383b285192aSMauro Carvalho Chehab 
384b285192aSMauro Carvalho Chehab 	flag = 0;
385b285192aSMauro Carvalho Chehab 	memory_write(dev->core, dev->mailbox, flag);
386b285192aSMauro Carvalho Chehab 	return retval;
387b285192aSMauro Carvalho Chehab }
388399426caSMauro Carvalho Chehab 
389b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
390b285192aSMauro Carvalho Chehab 
391399426caSMauro Carvalho Chehab /*
392399426caSMauro Carvalho Chehab  * We don't need to call the API often, so using just one mailbox
393399426caSMauro Carvalho Chehab  * will probably suffice
394399426caSMauro Carvalho Chehab  */
blackbird_api_cmd(struct cx8802_dev * dev,u32 command,u32 inputcnt,u32 outputcnt,...)395b285192aSMauro Carvalho Chehab static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
396b285192aSMauro Carvalho Chehab 			     u32 inputcnt, u32 outputcnt, ...)
397b285192aSMauro Carvalho Chehab {
398b285192aSMauro Carvalho Chehab 	u32 data[CX2341X_MBOX_MAX_DATA];
399b285192aSMauro Carvalho Chehab 	va_list vargs;
400b285192aSMauro Carvalho Chehab 	int i, err;
401b285192aSMauro Carvalho Chehab 
402b285192aSMauro Carvalho Chehab 	va_start(vargs, outputcnt);
403b285192aSMauro Carvalho Chehab 
404399426caSMauro Carvalho Chehab 	for (i = 0; i < inputcnt; i++)
405b285192aSMauro Carvalho Chehab 		data[i] = va_arg(vargs, int);
406399426caSMauro Carvalho Chehab 
407b285192aSMauro Carvalho Chehab 	err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
408b285192aSMauro Carvalho Chehab 	for (i = 0; i < outputcnt; i++) {
409b285192aSMauro Carvalho Chehab 		int *vptr = va_arg(vargs, int *);
410b285192aSMauro Carvalho Chehab 		*vptr = data[i];
411b285192aSMauro Carvalho Chehab 	}
412b285192aSMauro Carvalho Chehab 	va_end(vargs);
413b285192aSMauro Carvalho Chehab 	return err;
414b285192aSMauro Carvalho Chehab }
415b285192aSMauro Carvalho Chehab 
blackbird_find_mailbox(struct cx8802_dev * dev)416b285192aSMauro Carvalho Chehab static int blackbird_find_mailbox(struct cx8802_dev *dev)
417b285192aSMauro Carvalho Chehab {
418b285192aSMauro Carvalho Chehab 	u32 signature[4] = {0x12345678, 0x34567812, 0x56781234, 0x78123456};
419b285192aSMauro Carvalho Chehab 	int signaturecnt = 0;
420b285192aSMauro Carvalho Chehab 	u32 value;
421b285192aSMauro Carvalho Chehab 	int i;
422b285192aSMauro Carvalho Chehab 
423b285192aSMauro Carvalho Chehab 	for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
424b285192aSMauro Carvalho Chehab 		memory_read(dev->core, i, &value);
425b285192aSMauro Carvalho Chehab 		if (value == signature[signaturecnt])
426b285192aSMauro Carvalho Chehab 			signaturecnt++;
427b285192aSMauro Carvalho Chehab 		else
428b285192aSMauro Carvalho Chehab 			signaturecnt = 0;
4297b61ba8fSMauro Carvalho Chehab 		if (signaturecnt == 4) {
430b285192aSMauro Carvalho Chehab 			dprintk(1, "Mailbox signature found\n");
431b285192aSMauro Carvalho Chehab 			return i + 1;
432b285192aSMauro Carvalho Chehab 		}
433b285192aSMauro Carvalho Chehab 	}
434b285192aSMauro Carvalho Chehab 	dprintk(0, "Mailbox signature values not found!\n");
435eddd3263SHans Verkuil 	return -EIO;
436b285192aSMauro Carvalho Chehab }
437b285192aSMauro Carvalho Chehab 
blackbird_load_firmware(struct cx8802_dev * dev)438b285192aSMauro Carvalho Chehab static int blackbird_load_firmware(struct cx8802_dev *dev)
439b285192aSMauro Carvalho Chehab {
440b285192aSMauro Carvalho Chehab 	static const unsigned char magic[8] = {
441b285192aSMauro Carvalho Chehab 		0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
442b285192aSMauro Carvalho Chehab 	};
443b285192aSMauro Carvalho Chehab 	const struct firmware *firmware;
444b285192aSMauro Carvalho Chehab 	int i, retval = 0;
445b285192aSMauro Carvalho Chehab 	u32 value = 0;
446b285192aSMauro Carvalho Chehab 	u32 checksum = 0;
447c79a23f3SHans Verkuil 	__le32 *dataptr;
448b285192aSMauro Carvalho Chehab 
449b285192aSMauro Carvalho Chehab 	retval  = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
450399426caSMauro Carvalho Chehab 	retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS,
451399426caSMauro Carvalho Chehab 				 IVTV_CMD_HW_BLOCKS_RST);
452399426caSMauro Carvalho Chehab 	retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH,
453399426caSMauro Carvalho Chehab 				 0x80000640);
454399426caSMauro Carvalho Chehab 	retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE,
455399426caSMauro Carvalho Chehab 				 0x1A);
456399426caSMauro Carvalho Chehab 	usleep_range(10000, 20000);
457b285192aSMauro Carvalho Chehab 	retval |= register_write(dev->core, IVTV_REG_APU, 0);
458b285192aSMauro Carvalho Chehab 
459b285192aSMauro Carvalho Chehab 	if (retval < 0)
460b285192aSMauro Carvalho Chehab 		dprintk(0, "Error with register_write\n");
461b285192aSMauro Carvalho Chehab 
462b285192aSMauro Carvalho Chehab 	retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
463b285192aSMauro Carvalho Chehab 				  &dev->pci->dev);
464b285192aSMauro Carvalho Chehab 
465b285192aSMauro Carvalho Chehab 	if (retval != 0) {
466eddd3263SHans Verkuil 		pr_err("Hotplug firmware request failed (%s).\n",
467b285192aSMauro Carvalho Chehab 		       CX2341X_FIRM_ENC_FILENAME);
468eddd3263SHans Verkuil 		pr_err("Please fix your hotplug setup, the board will not work without firmware loaded!\n");
469eddd3263SHans Verkuil 		return -EIO;
470b285192aSMauro Carvalho Chehab 	}
471b285192aSMauro Carvalho Chehab 
472b285192aSMauro Carvalho Chehab 	if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
473eddd3263SHans Verkuil 		pr_err("Firmware size mismatch (have %zd, expected %d)\n",
474b285192aSMauro Carvalho Chehab 		       firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
475b285192aSMauro Carvalho Chehab 		release_firmware(firmware);
476eddd3263SHans Verkuil 		return -EINVAL;
477b285192aSMauro Carvalho Chehab 	}
478b285192aSMauro Carvalho Chehab 
4797b61ba8fSMauro Carvalho Chehab 	if (memcmp(firmware->data, magic, 8) != 0) {
480eddd3263SHans Verkuil 		pr_err("Firmware magic mismatch, wrong file?\n");
481b285192aSMauro Carvalho Chehab 		release_firmware(firmware);
482eddd3263SHans Verkuil 		return -EINVAL;
483b285192aSMauro Carvalho Chehab 	}
484b285192aSMauro Carvalho Chehab 
485b285192aSMauro Carvalho Chehab 	/* transfer to the chip */
486b285192aSMauro Carvalho Chehab 	dprintk(1, "Loading firmware ...\n");
487c79a23f3SHans Verkuil 	dataptr = (__le32 *)firmware->data;
488b285192aSMauro Carvalho Chehab 	for (i = 0; i < (firmware->size >> 2); i++) {
489b285192aSMauro Carvalho Chehab 		value = le32_to_cpu(*dataptr);
490b285192aSMauro Carvalho Chehab 		checksum += ~value;
491b285192aSMauro Carvalho Chehab 		memory_write(dev->core, i, value);
492b285192aSMauro Carvalho Chehab 		dataptr++;
493b285192aSMauro Carvalho Chehab 	}
494b285192aSMauro Carvalho Chehab 
495b285192aSMauro Carvalho Chehab 	/* read back to verify with the checksum */
496b285192aSMauro Carvalho Chehab 	for (i--; i >= 0; i--) {
497b285192aSMauro Carvalho Chehab 		memory_read(dev->core, i, &value);
498b285192aSMauro Carvalho Chehab 		checksum -= ~value;
499b285192aSMauro Carvalho Chehab 	}
500eddd3263SHans Verkuil 	release_firmware(firmware);
501b285192aSMauro Carvalho Chehab 	if (checksum) {
502eddd3263SHans Verkuil 		pr_err("Firmware load might have failed (checksum mismatch).\n");
503eddd3263SHans Verkuil 		return -EIO;
504b285192aSMauro Carvalho Chehab 	}
505b285192aSMauro Carvalho Chehab 	dprintk(0, "Firmware upload successful.\n");
506b285192aSMauro Carvalho Chehab 
507399426caSMauro Carvalho Chehab 	retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS,
508399426caSMauro Carvalho Chehab 				 IVTV_CMD_HW_BLOCKS_RST);
509b285192aSMauro Carvalho Chehab 	retval |= register_read(dev->core, IVTV_REG_SPU, &value);
510b285192aSMauro Carvalho Chehab 	retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
511399426caSMauro Carvalho Chehab 	usleep_range(10000, 20000);
512b285192aSMauro Carvalho Chehab 
513b285192aSMauro Carvalho Chehab 	retval |= register_read(dev->core, IVTV_REG_VPU, &value);
514b285192aSMauro Carvalho Chehab 	retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
515b285192aSMauro Carvalho Chehab 
516b285192aSMauro Carvalho Chehab 	if (retval < 0)
517b285192aSMauro Carvalho Chehab 		dprintk(0, "Error with register_write\n");
518b285192aSMauro Carvalho Chehab 	return 0;
519b285192aSMauro Carvalho Chehab }
520b285192aSMauro Carvalho Chehab 
521399426caSMauro Carvalho Chehab /*
522399426caSMauro Carvalho Chehab  * Settings used by the windows tv app for PVR2000:
523399426caSMauro Carvalho Chehab  * =================================================================================================================
524399426caSMauro Carvalho Chehab  * Profile | Codec | Resolution | CBR/VBR | Video Qlty   | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
525399426caSMauro Carvalho Chehab  * -----------------------------------------------------------------------------------------------------------------
526399426caSMauro Carvalho Chehab  * MPEG-1  | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 2000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
527399426caSMauro Carvalho Chehab  * MPEG-2  | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 4000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
528399426caSMauro Carvalho Chehab  * VCD     | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 1150 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
529399426caSMauro Carvalho Chehab  * DVD     | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
530399426caSMauro Carvalho Chehab  * DB* DVD | MPEG2 | 720x576PAL | CBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
531399426caSMauro Carvalho Chehab  * =================================================================================================================
532399426caSMauro Carvalho Chehab  * [*] DB: "DirectBurn"
533b285192aSMauro Carvalho Chehab  */
534b285192aSMauro Carvalho Chehab 
blackbird_codec_settings(struct cx8802_dev * dev)535b285192aSMauro Carvalho Chehab static void blackbird_codec_settings(struct cx8802_dev *dev)
536b285192aSMauro Carvalho Chehab {
537ccd6f1d4SHans Verkuil 	struct cx88_core *core = dev->core;
538ccd6f1d4SHans Verkuil 
539b285192aSMauro Carvalho Chehab 	/* assign frame size */
540b285192aSMauro Carvalho Chehab 	blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
541ccd6f1d4SHans Verkuil 			  core->height, core->width);
542b285192aSMauro Carvalho Chehab 
543ccd6f1d4SHans Verkuil 	dev->cxhdl.width = core->width;
544ccd6f1d4SHans Verkuil 	dev->cxhdl.height = core->height;
545399426caSMauro Carvalho Chehab 	cx2341x_handler_set_50hz(&dev->cxhdl,
546399426caSMauro Carvalho Chehab 				 dev->core->tvnorm & V4L2_STD_625_50);
547b285192aSMauro Carvalho Chehab 	cx2341x_handler_setup(&dev->cxhdl);
548b285192aSMauro Carvalho Chehab }
549b285192aSMauro Carvalho Chehab 
blackbird_initialize_codec(struct cx8802_dev * dev)550b285192aSMauro Carvalho Chehab static int blackbird_initialize_codec(struct cx8802_dev *dev)
551b285192aSMauro Carvalho Chehab {
552b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
553b285192aSMauro Carvalho Chehab 	int version;
554b285192aSMauro Carvalho Chehab 	int retval;
555b285192aSMauro Carvalho Chehab 
556b285192aSMauro Carvalho Chehab 	dprintk(1, "Initialize codec\n");
557b285192aSMauro Carvalho Chehab 	retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
558b285192aSMauro Carvalho Chehab 	if (retval < 0) {
559b285192aSMauro Carvalho Chehab 		/* ping was not successful, reset and upload firmware */
560b285192aSMauro Carvalho Chehab 		cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
561b285192aSMauro Carvalho Chehab 		cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
562b285192aSMauro Carvalho Chehab 		retval = blackbird_load_firmware(dev);
563b285192aSMauro Carvalho Chehab 		if (retval < 0)
564b285192aSMauro Carvalho Chehab 			return retval;
565b285192aSMauro Carvalho Chehab 
566b285192aSMauro Carvalho Chehab 		retval = blackbird_find_mailbox(dev);
567b285192aSMauro Carvalho Chehab 		if (retval < 0)
568b285192aSMauro Carvalho Chehab 			return -1;
569b285192aSMauro Carvalho Chehab 
570b285192aSMauro Carvalho Chehab 		dev->mailbox = retval;
571b285192aSMauro Carvalho Chehab 
572399426caSMauro Carvalho Chehab 		/* ping */
573399426caSMauro Carvalho Chehab 		retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0);
574b285192aSMauro Carvalho Chehab 		if (retval < 0) {
575b285192aSMauro Carvalho Chehab 			dprintk(0, "ERROR: Firmware ping failed!\n");
576b285192aSMauro Carvalho Chehab 			return -1;
577b285192aSMauro Carvalho Chehab 		}
578b285192aSMauro Carvalho Chehab 
579399426caSMauro Carvalho Chehab 		retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION,
580399426caSMauro Carvalho Chehab 					   0, 1, &version);
581b285192aSMauro Carvalho Chehab 		if (retval < 0) {
582399426caSMauro Carvalho Chehab 			dprintk(0,
583399426caSMauro Carvalho Chehab 				"ERROR: Firmware get encoder version failed!\n");
584b285192aSMauro Carvalho Chehab 			return -1;
585b285192aSMauro Carvalho Chehab 		}
586b285192aSMauro Carvalho Chehab 		dprintk(0, "Firmware version is 0x%08x\n", version);
587b285192aSMauro Carvalho Chehab 	}
588b285192aSMauro Carvalho Chehab 
589b285192aSMauro Carvalho Chehab 	cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
590b285192aSMauro Carvalho Chehab 	cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
591b285192aSMauro Carvalho Chehab 	cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
592b285192aSMauro Carvalho Chehab 	cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
593b285192aSMauro Carvalho Chehab 
594b285192aSMauro Carvalho Chehab 	blackbird_codec_settings(dev);
595b285192aSMauro Carvalho Chehab 
596b285192aSMauro Carvalho Chehab 	blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
597399426caSMauro Carvalho Chehab 			  BLACKBIRD_FIELD1_SAA7115, BLACKBIRD_FIELD2_SAA7115);
598b285192aSMauro Carvalho Chehab 
599b285192aSMauro Carvalho Chehab 	blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
600b285192aSMauro Carvalho Chehab 			  BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
601b285192aSMauro Carvalho Chehab 			  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
602b285192aSMauro Carvalho Chehab 
603b285192aSMauro Carvalho Chehab 	return 0;
604b285192aSMauro Carvalho Chehab }
605b285192aSMauro Carvalho Chehab 
blackbird_start_codec(struct cx8802_dev * dev)6060b6b6302SHans Verkuil static int blackbird_start_codec(struct cx8802_dev *dev)
607b285192aSMauro Carvalho Chehab {
608b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
609b285192aSMauro Carvalho Chehab 	/* start capturing to the host interface */
610b285192aSMauro Carvalho Chehab 	u32 reg;
611b285192aSMauro Carvalho Chehab 
612b285192aSMauro Carvalho Chehab 	int i;
613b285192aSMauro Carvalho Chehab 	int lastchange = -1;
614b285192aSMauro Carvalho Chehab 	int lastval = 0;
615b285192aSMauro Carvalho Chehab 
616b285192aSMauro Carvalho Chehab 	for (i = 0; (i < 10) && (i < (lastchange + 4)); i++) {
617b285192aSMauro Carvalho Chehab 		reg = cx_read(AUD_STATUS);
618b285192aSMauro Carvalho Chehab 
619b285192aSMauro Carvalho Chehab 		dprintk(1, "AUD_STATUS:%dL: 0x%x\n", i, reg);
620b285192aSMauro Carvalho Chehab 		if ((reg & 0x0F) != lastval) {
621b285192aSMauro Carvalho Chehab 			lastval = reg & 0x0F;
622b285192aSMauro Carvalho Chehab 			lastchange = i;
623b285192aSMauro Carvalho Chehab 		}
624b285192aSMauro Carvalho Chehab 		msleep(100);
625b285192aSMauro Carvalho Chehab 	}
626b285192aSMauro Carvalho Chehab 
627b285192aSMauro Carvalho Chehab 	/* unmute audio source */
628b285192aSMauro Carvalho Chehab 	cx_clear(AUD_VOL_CTL, (1 << 6));
629b285192aSMauro Carvalho Chehab 
630b285192aSMauro Carvalho Chehab 	blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0, 0);
631b285192aSMauro Carvalho Chehab 
632b285192aSMauro Carvalho Chehab 	/* initialize the video input */
633b285192aSMauro Carvalho Chehab 	blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
634b285192aSMauro Carvalho Chehab 
635b285192aSMauro Carvalho Chehab 	cx2341x_handler_set_busy(&dev->cxhdl, 1);
636b285192aSMauro Carvalho Chehab 
637b285192aSMauro Carvalho Chehab 	/* start capturing to the host interface */
638b285192aSMauro Carvalho Chehab 	blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
639399426caSMauro Carvalho Chehab 			  BLACKBIRD_MPEG_CAPTURE, BLACKBIRD_RAW_BITS_NONE);
640b285192aSMauro Carvalho Chehab 
641b285192aSMauro Carvalho Chehab 	return 0;
642b285192aSMauro Carvalho Chehab }
643b285192aSMauro Carvalho Chehab 
blackbird_stop_codec(struct cx8802_dev * dev)644b285192aSMauro Carvalho Chehab static int blackbird_stop_codec(struct cx8802_dev *dev)
645b285192aSMauro Carvalho Chehab {
646b285192aSMauro Carvalho Chehab 	blackbird_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
647b285192aSMauro Carvalho Chehab 			  BLACKBIRD_END_NOW,
648b285192aSMauro Carvalho Chehab 			  BLACKBIRD_MPEG_CAPTURE,
649399426caSMauro Carvalho Chehab 			  BLACKBIRD_RAW_BITS_NONE);
650b285192aSMauro Carvalho Chehab 
651b285192aSMauro Carvalho Chehab 	cx2341x_handler_set_busy(&dev->cxhdl, 0);
652b285192aSMauro Carvalho Chehab 
653b285192aSMauro Carvalho Chehab 	return 0;
654b285192aSMauro Carvalho Chehab }
655b285192aSMauro Carvalho Chehab 
656b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
657b285192aSMauro Carvalho Chehab 
queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])658df9ecb0cSHans Verkuil static int queue_setup(struct vb2_queue *q,
6590b6b6302SHans Verkuil 		       unsigned int *num_buffers, unsigned int *num_planes,
66036c0f8b3SHans Verkuil 		       unsigned int sizes[], struct device *alloc_devs[])
661b285192aSMauro Carvalho Chehab {
6620b6b6302SHans Verkuil 	struct cx8802_dev *dev = q->drv_priv;
663b285192aSMauro Carvalho Chehab 
6640b6b6302SHans Verkuil 	*num_planes = 1;
6650b6b6302SHans Verkuil 	dev->ts_packet_size  = 188 * 4;
6660b6b6302SHans Verkuil 	dev->ts_packet_count  = 32;
6670b6b6302SHans Verkuil 	sizes[0] = dev->ts_packet_size * dev->ts_packet_count;
668b285192aSMauro Carvalho Chehab 	return 0;
669b285192aSMauro Carvalho Chehab }
670b285192aSMauro Carvalho Chehab 
buffer_prepare(struct vb2_buffer * vb)6710b6b6302SHans Verkuil static int buffer_prepare(struct vb2_buffer *vb)
672b285192aSMauro Carvalho Chehab {
6732d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
6740b6b6302SHans Verkuil 	struct cx8802_dev *dev = vb->vb2_queue->drv_priv;
6752d700715SJunghak Sung 	struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
6760b6b6302SHans Verkuil 
677ccd6f1d4SHans Verkuil 	return cx8802_buf_prepare(vb->vb2_queue, dev, buf);
678b285192aSMauro Carvalho Chehab }
679b285192aSMauro Carvalho Chehab 
buffer_finish(struct vb2_buffer * vb)6800b6b6302SHans Verkuil static void buffer_finish(struct vb2_buffer *vb)
681b285192aSMauro Carvalho Chehab {
6822d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
6830b6b6302SHans Verkuil 	struct cx8802_dev *dev = vb->vb2_queue->drv_priv;
6842d700715SJunghak Sung 	struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
6855e7045e3SHans Verkuil 	struct cx88_riscmem *risc = &buf->risc;
6860b6b6302SHans Verkuil 
6875e7045e3SHans Verkuil 	if (risc->cpu)
688*00ae4ebcSChristophe JAILLET 		dma_free_coherent(&dev->pci->dev, risc->size, risc->cpu,
689*00ae4ebcSChristophe JAILLET 				  risc->dma);
6905e7045e3SHans Verkuil 	memset(risc, 0, sizeof(*risc));
691b285192aSMauro Carvalho Chehab }
692b285192aSMauro Carvalho Chehab 
buffer_queue(struct vb2_buffer * vb)6930b6b6302SHans Verkuil static void buffer_queue(struct vb2_buffer *vb)
694b285192aSMauro Carvalho Chehab {
6952d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
6960b6b6302SHans Verkuil 	struct cx8802_dev *dev = vb->vb2_queue->drv_priv;
6972d700715SJunghak Sung 	struct cx88_buffer    *buf = container_of(vbuf, struct cx88_buffer, vb);
6980b6b6302SHans Verkuil 
6990b6b6302SHans Verkuil 	cx8802_buf_queue(dev, buf);
700b285192aSMauro Carvalho Chehab }
701b285192aSMauro Carvalho Chehab 
start_streaming(struct vb2_queue * q,unsigned int count)7020b6b6302SHans Verkuil static int start_streaming(struct vb2_queue *q, unsigned int count)
7030b6b6302SHans Verkuil {
7040b6b6302SHans Verkuil 	struct cx8802_dev *dev = q->drv_priv;
7050b6b6302SHans Verkuil 	struct cx88_dmaqueue *dmaq = &dev->mpegq;
7060b6b6302SHans Verkuil 	struct cx8802_driver *drv;
7070b6b6302SHans Verkuil 	struct cx88_buffer *buf;
7080b6b6302SHans Verkuil 	unsigned long flags;
7090b6b6302SHans Verkuil 	int err;
7100b6b6302SHans Verkuil 
7110b6b6302SHans Verkuil 	/* Make sure we can acquire the hardware */
7120b6b6302SHans Verkuil 	drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
7130b6b6302SHans Verkuil 	if (!drv) {
7140b6b6302SHans Verkuil 		dprintk(1, "%s: blackbird driver is not loaded\n", __func__);
7150b6b6302SHans Verkuil 		err = -ENODEV;
7160b6b6302SHans Verkuil 		goto fail;
7170b6b6302SHans Verkuil 	}
7180b6b6302SHans Verkuil 
7190b6b6302SHans Verkuil 	err = drv->request_acquire(drv);
7200b6b6302SHans Verkuil 	if (err != 0) {
721399426caSMauro Carvalho Chehab 		dprintk(1, "%s: Unable to acquire hardware, %d\n", __func__,
722399426caSMauro Carvalho Chehab 			err);
7230b6b6302SHans Verkuil 		goto fail;
7240b6b6302SHans Verkuil 	}
7250b6b6302SHans Verkuil 
7260b6b6302SHans Verkuil 	if (blackbird_initialize_codec(dev) < 0) {
7270b6b6302SHans Verkuil 		drv->request_release(drv);
7280b6b6302SHans Verkuil 		err = -EINVAL;
7290b6b6302SHans Verkuil 		goto fail;
7300b6b6302SHans Verkuil 	}
7310b6b6302SHans Verkuil 
7320b6b6302SHans Verkuil 	err = blackbird_start_codec(dev);
7330b6b6302SHans Verkuil 	if (err == 0) {
7340b6b6302SHans Verkuil 		buf = list_entry(dmaq->active.next, struct cx88_buffer, list);
7350b6b6302SHans Verkuil 		cx8802_start_dma(dev, dmaq, buf);
7360b6b6302SHans Verkuil 		return 0;
7370b6b6302SHans Verkuil 	}
7380b6b6302SHans Verkuil 
7390b6b6302SHans Verkuil fail:
7400b6b6302SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
7410b6b6302SHans Verkuil 	while (!list_empty(&dmaq->active)) {
7420b6b6302SHans Verkuil 		struct cx88_buffer *buf = list_entry(dmaq->active.next,
7430b6b6302SHans Verkuil 			struct cx88_buffer, list);
7440b6b6302SHans Verkuil 
7450b6b6302SHans Verkuil 		list_del(&buf->list);
7462d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
7470b6b6302SHans Verkuil 	}
7480b6b6302SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
7490b6b6302SHans Verkuil 	return err;
7500b6b6302SHans Verkuil }
7510b6b6302SHans Verkuil 
stop_streaming(struct vb2_queue * q)7520b6b6302SHans Verkuil static void stop_streaming(struct vb2_queue *q)
7530b6b6302SHans Verkuil {
7540b6b6302SHans Verkuil 	struct cx8802_dev *dev = q->drv_priv;
7550b6b6302SHans Verkuil 	struct cx88_dmaqueue *dmaq = &dev->mpegq;
7560b6b6302SHans Verkuil 	struct cx8802_driver *drv = NULL;
7570b6b6302SHans Verkuil 	unsigned long flags;
7580b6b6302SHans Verkuil 
7590b6b6302SHans Verkuil 	cx8802_cancel_buffers(dev);
7600b6b6302SHans Verkuil 	blackbird_stop_codec(dev);
7610b6b6302SHans Verkuil 
7620b6b6302SHans Verkuil 	/* Make sure we release the hardware */
7630b6b6302SHans Verkuil 	drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
7640b6b6302SHans Verkuil 	WARN_ON(!drv);
7650b6b6302SHans Verkuil 	if (drv)
7660b6b6302SHans Verkuil 		drv->request_release(drv);
7670b6b6302SHans Verkuil 
7680b6b6302SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
7690b6b6302SHans Verkuil 	while (!list_empty(&dmaq->active)) {
7700b6b6302SHans Verkuil 		struct cx88_buffer *buf = list_entry(dmaq->active.next,
7710b6b6302SHans Verkuil 			struct cx88_buffer, list);
7720b6b6302SHans Verkuil 
7730b6b6302SHans Verkuil 		list_del(&buf->list);
7742d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
7750b6b6302SHans Verkuil 	}
7760b6b6302SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
7770b6b6302SHans Verkuil }
7780b6b6302SHans Verkuil 
77910accd2eSJulia Lawall static const struct vb2_ops blackbird_qops = {
7800b6b6302SHans Verkuil 	.queue_setup    = queue_setup,
7810b6b6302SHans Verkuil 	.buf_prepare  = buffer_prepare,
7820b6b6302SHans Verkuil 	.buf_finish = buffer_finish,
7830b6b6302SHans Verkuil 	.buf_queue    = buffer_queue,
7840b6b6302SHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
7850b6b6302SHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
7860b6b6302SHans Verkuil 	.start_streaming = start_streaming,
7870b6b6302SHans Verkuil 	.stop_streaming = stop_streaming,
788b285192aSMauro Carvalho Chehab };
789b285192aSMauro Carvalho Chehab 
790b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
791b285192aSMauro Carvalho Chehab 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)792b285192aSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, void  *priv,
793b285192aSMauro Carvalho Chehab 			   struct v4l2_capability *cap)
794b285192aSMauro Carvalho Chehab {
7950b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
796b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
797b285192aSMauro Carvalho Chehab 
798cc1e6315SMauro Carvalho Chehab 	strscpy(cap->driver, "cx88_blackbird", sizeof(cap->driver));
7994839c58fSMauro Carvalho Chehab 	return cx88_querycap(file, core, cap);
800b285192aSMauro Carvalho Chehab }
801b285192aSMauro Carvalho Chehab 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)802b285192aSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
803b285192aSMauro Carvalho Chehab 				   struct v4l2_fmtdesc *f)
804b285192aSMauro Carvalho Chehab {
805b285192aSMauro Carvalho Chehab 	if (f->index != 0)
806b285192aSMauro Carvalho Chehab 		return -EINVAL;
807b285192aSMauro Carvalho Chehab 
808b285192aSMauro Carvalho Chehab 	f->pixelformat = V4L2_PIX_FMT_MPEG;
809b285192aSMauro Carvalho Chehab 	return 0;
810b285192aSMauro Carvalho Chehab }
811b285192aSMauro Carvalho Chehab 
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)812b285192aSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
813b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
814b285192aSMauro Carvalho Chehab {
8150b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
816ccd6f1d4SHans Verkuil 	struct cx88_core *core = dev->core;
817b285192aSMauro Carvalho Chehab 
818b285192aSMauro Carvalho Chehab 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
819b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = 0;
8200b6b6302SHans Verkuil 	f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count;
821b285192aSMauro Carvalho Chehab 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
822ccd6f1d4SHans Verkuil 	f->fmt.pix.width        = core->width;
823ccd6f1d4SHans Verkuil 	f->fmt.pix.height       = core->height;
824ccd6f1d4SHans Verkuil 	f->fmt.pix.field        = core->field;
825b285192aSMauro Carvalho Chehab 	return 0;
826b285192aSMauro Carvalho Chehab }
827b285192aSMauro Carvalho Chehab 
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)828b285192aSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
829b285192aSMauro Carvalho Chehab 				  struct v4l2_format *f)
830b285192aSMauro Carvalho Chehab {
8310b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
832ccd6f1d4SHans Verkuil 	struct cx88_core *core = dev->core;
8337b61ba8fSMauro Carvalho Chehab 	unsigned int maxw, maxh;
834ccd6f1d4SHans Verkuil 	enum v4l2_field field;
835b285192aSMauro Carvalho Chehab 
836b285192aSMauro Carvalho Chehab 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
837b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = 0;
8380b6b6302SHans Verkuil 	f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count;
839b285192aSMauro Carvalho Chehab 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
840ccd6f1d4SHans Verkuil 
841ccd6f1d4SHans Verkuil 	maxw = norm_maxw(core->tvnorm);
842ccd6f1d4SHans Verkuil 	maxh = norm_maxh(core->tvnorm);
843ccd6f1d4SHans Verkuil 
844ccd6f1d4SHans Verkuil 	field = f->fmt.pix.field;
845ccd6f1d4SHans Verkuil 
846ccd6f1d4SHans Verkuil 	switch (field) {
847ccd6f1d4SHans Verkuil 	case V4L2_FIELD_TOP:
848ccd6f1d4SHans Verkuil 	case V4L2_FIELD_BOTTOM:
849ccd6f1d4SHans Verkuil 	case V4L2_FIELD_INTERLACED:
850ccd6f1d4SHans Verkuil 	case V4L2_FIELD_SEQ_BT:
851ccd6f1d4SHans Verkuil 	case V4L2_FIELD_SEQ_TB:
852ccd6f1d4SHans Verkuil 		break;
853ccd6f1d4SHans Verkuil 	default:
854ccd6f1d4SHans Verkuil 		field = (f->fmt.pix.height > maxh / 2)
855ccd6f1d4SHans Verkuil 			? V4L2_FIELD_INTERLACED
856ccd6f1d4SHans Verkuil 			: V4L2_FIELD_BOTTOM;
857ccd6f1d4SHans Verkuil 		break;
858ccd6f1d4SHans Verkuil 	}
859ccd6f1d4SHans Verkuil 	if (V4L2_FIELD_HAS_T_OR_B(field))
860ccd6f1d4SHans Verkuil 		maxh /= 2;
861ccd6f1d4SHans Verkuil 
862ccd6f1d4SHans Verkuil 	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
863ccd6f1d4SHans Verkuil 			      &f->fmt.pix.height, 32, maxh, 0, 0);
864ccd6f1d4SHans Verkuil 	f->fmt.pix.field = field;
865b285192aSMauro Carvalho Chehab 	return 0;
866b285192aSMauro Carvalho Chehab }
867b285192aSMauro Carvalho Chehab 
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)868b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
869b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
870b285192aSMauro Carvalho Chehab {
8710b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
872b285192aSMauro Carvalho Chehab 	struct cx88_core  *core = dev->core;
873b285192aSMauro Carvalho Chehab 
874078859a3SHans Verkuil 	if (vb2_is_busy(&dev->vb2_mpegq))
875078859a3SHans Verkuil 		return -EBUSY;
876078859a3SHans Verkuil 	if (core->v4ldev && (vb2_is_busy(&core->v4ldev->vb2_vidq) ||
877078859a3SHans Verkuil 			     vb2_is_busy(&core->v4ldev->vb2_vbiq)))
878078859a3SHans Verkuil 		return -EBUSY;
879ccd6f1d4SHans Verkuil 	vidioc_try_fmt_vid_cap(file, priv, f);
880ccd6f1d4SHans Verkuil 	core->width = f->fmt.pix.width;
881ccd6f1d4SHans Verkuil 	core->height = f->fmt.pix.height;
882ccd6f1d4SHans Verkuil 	core->field = f->fmt.pix.field;
883399426caSMauro Carvalho Chehab 	cx88_set_scale(core, f->fmt.pix.width, f->fmt.pix.height,
884399426caSMauro Carvalho Chehab 		       f->fmt.pix.field);
885b285192aSMauro Carvalho Chehab 	blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
886b285192aSMauro Carvalho Chehab 			  f->fmt.pix.height, f->fmt.pix.width);
887b285192aSMauro Carvalho Chehab 	return 0;
888b285192aSMauro Carvalho Chehab }
889b285192aSMauro Carvalho Chehab 
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)890b285192aSMauro Carvalho Chehab static int vidioc_s_frequency(struct file *file, void *priv,
891b530a447SHans Verkuil 			      const struct v4l2_frequency *f)
892b285192aSMauro Carvalho Chehab {
8930b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
894b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
895fb37ab3eSHans Verkuil 	bool streaming;
896b285192aSMauro Carvalho Chehab 
8977b61ba8fSMauro Carvalho Chehab 	if (unlikely(core->board.tuner_type == UNSET))
898b285192aSMauro Carvalho Chehab 		return -EINVAL;
899b285192aSMauro Carvalho Chehab 	if (unlikely(f->tuner != 0))
900b285192aSMauro Carvalho Chehab 		return -EINVAL;
9017f56a4a7SPrabhakar Lad 	streaming = vb2_start_streaming_called(&dev->vb2_mpegq);
902fb37ab3eSHans Verkuil 	if (streaming)
903b285192aSMauro Carvalho Chehab 		blackbird_stop_codec(dev);
904b285192aSMauro Carvalho Chehab 
905b285192aSMauro Carvalho Chehab 	cx88_set_freq(core, f);
906b285192aSMauro Carvalho Chehab 	blackbird_initialize_codec(dev);
907399426caSMauro Carvalho Chehab 	cx88_set_scale(core, core->width, core->height, core->field);
908fb37ab3eSHans Verkuil 	if (streaming)
909fb37ab3eSHans Verkuil 		blackbird_start_codec(dev);
910b285192aSMauro Carvalho Chehab 	return 0;
911b285192aSMauro Carvalho Chehab }
912b285192aSMauro Carvalho Chehab 
vidioc_log_status(struct file * file,void * priv)913b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
914b285192aSMauro Carvalho Chehab {
9150b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
916b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
917b285192aSMauro Carvalho Chehab 	char name[32 + 2];
918b285192aSMauro Carvalho Chehab 
919b285192aSMauro Carvalho Chehab 	snprintf(name, sizeof(name), "%s/2", core->name);
920b285192aSMauro Carvalho Chehab 	call_all(core, core, log_status);
921b285192aSMauro Carvalho Chehab 	v4l2_ctrl_handler_log_status(&dev->cxhdl.hdl, name);
922b285192aSMauro Carvalho Chehab 	return 0;
923b285192aSMauro Carvalho Chehab }
924b285192aSMauro Carvalho Chehab 
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)925b285192aSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
926b285192aSMauro Carvalho Chehab 			     struct v4l2_input *i)
927b285192aSMauro Carvalho Chehab {
9280b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
9290b6b6302SHans Verkuil 	struct cx88_core *core = dev->core;
9307b61ba8fSMauro Carvalho Chehab 
931b285192aSMauro Carvalho Chehab 	return cx88_enum_input(core, i);
932b285192aSMauro Carvalho Chehab }
933b285192aSMauro Carvalho Chehab 
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)934b285192aSMauro Carvalho Chehab static int vidioc_g_frequency(struct file *file, void *priv,
935b285192aSMauro Carvalho Chehab 			      struct v4l2_frequency *f)
936b285192aSMauro Carvalho Chehab {
9370b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
9380b6b6302SHans Verkuil 	struct cx88_core *core = dev->core;
939b285192aSMauro Carvalho Chehab 
9407b61ba8fSMauro Carvalho Chehab 	if (unlikely(core->board.tuner_type == UNSET))
941b285192aSMauro Carvalho Chehab 		return -EINVAL;
942b285192aSMauro Carvalho Chehab 	if (unlikely(f->tuner != 0))
943b285192aSMauro Carvalho Chehab 		return -EINVAL;
944b285192aSMauro Carvalho Chehab 
945b285192aSMauro Carvalho Chehab 	f->frequency = core->freq;
946b285192aSMauro Carvalho Chehab 	call_all(core, tuner, g_frequency, f);
947b285192aSMauro Carvalho Chehab 
948b285192aSMauro Carvalho Chehab 	return 0;
949b285192aSMauro Carvalho Chehab }
950b285192aSMauro Carvalho Chehab 
vidioc_g_input(struct file * file,void * priv,unsigned int * i)951b285192aSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
952b285192aSMauro Carvalho Chehab {
9530b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
9540b6b6302SHans Verkuil 	struct cx88_core *core = dev->core;
955b285192aSMauro Carvalho Chehab 
956b285192aSMauro Carvalho Chehab 	*i = core->input;
957b285192aSMauro Carvalho Chehab 	return 0;
958b285192aSMauro Carvalho Chehab }
959b285192aSMauro Carvalho Chehab 
vidioc_s_input(struct file * file,void * priv,unsigned int i)960b285192aSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
961b285192aSMauro Carvalho Chehab {
9620b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
9630b6b6302SHans Verkuil 	struct cx88_core *core = dev->core;
964b285192aSMauro Carvalho Chehab 
965b285192aSMauro Carvalho Chehab 	if (i >= 4)
966b285192aSMauro Carvalho Chehab 		return -EINVAL;
967399426caSMauro Carvalho Chehab 	if (!INPUT(i).type)
968b285192aSMauro Carvalho Chehab 		return -EINVAL;
969b285192aSMauro Carvalho Chehab 
970b285192aSMauro Carvalho Chehab 	cx88_newstation(core);
971b285192aSMauro Carvalho Chehab 	cx88_video_mux(core, i);
972b285192aSMauro Carvalho Chehab 	return 0;
973b285192aSMauro Carvalho Chehab }
974b285192aSMauro Carvalho Chehab 
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)975b285192aSMauro Carvalho Chehab static int vidioc_g_tuner(struct file *file, void *priv,
976b285192aSMauro Carvalho Chehab 			  struct v4l2_tuner *t)
977b285192aSMauro Carvalho Chehab {
9780b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
9790b6b6302SHans Verkuil 	struct cx88_core *core = dev->core;
980b285192aSMauro Carvalho Chehab 	u32 reg;
981b285192aSMauro Carvalho Chehab 
9827b61ba8fSMauro Carvalho Chehab 	if (unlikely(core->board.tuner_type == UNSET))
983b285192aSMauro Carvalho Chehab 		return -EINVAL;
9847b61ba8fSMauro Carvalho Chehab 	if (t->index != 0)
985b285192aSMauro Carvalho Chehab 		return -EINVAL;
986b285192aSMauro Carvalho Chehab 
987cc1e6315SMauro Carvalho Chehab 	strscpy(t->name, "Television", sizeof(t->name));
988b285192aSMauro Carvalho Chehab 	t->capability = V4L2_TUNER_CAP_NORM;
989b285192aSMauro Carvalho Chehab 	t->rangehigh  = 0xffffffffUL;
990b285192aSMauro Carvalho Chehab 	call_all(core, tuner, g_tuner, t);
991b285192aSMauro Carvalho Chehab 
992b285192aSMauro Carvalho Chehab 	cx88_get_stereo(core, t);
993b285192aSMauro Carvalho Chehab 	reg = cx_read(MO_DEVICE_STATUS);
994b285192aSMauro Carvalho Chehab 	t->signal = (reg & (1 << 5)) ? 0xffff : 0x0000;
995b285192aSMauro Carvalho Chehab 	return 0;
996b285192aSMauro Carvalho Chehab }
997b285192aSMauro Carvalho Chehab 
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)998b285192aSMauro Carvalho Chehab static int vidioc_s_tuner(struct file *file, void *priv,
9992f73c7c5SHans Verkuil 			  const struct v4l2_tuner *t)
1000b285192aSMauro Carvalho Chehab {
10010b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
10020b6b6302SHans Verkuil 	struct cx88_core *core = dev->core;
1003b285192aSMauro Carvalho Chehab 
10047b61ba8fSMauro Carvalho Chehab 	if (core->board.tuner_type == UNSET)
1005b285192aSMauro Carvalho Chehab 		return -EINVAL;
10067b61ba8fSMauro Carvalho Chehab 	if (t->index != 0)
1007b285192aSMauro Carvalho Chehab 		return -EINVAL;
1008b285192aSMauro Carvalho Chehab 
1009b285192aSMauro Carvalho Chehab 	cx88_set_stereo(core, t->audmode, 1);
1010b285192aSMauro Carvalho Chehab 	return 0;
1011b285192aSMauro Carvalho Chehab }
1012b285192aSMauro Carvalho Chehab 
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * tvnorm)1013b285192aSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
1014b285192aSMauro Carvalho Chehab {
10150b6b6302SHans Verkuil 	struct cx8802_dev *dev = video_drvdata(file);
10160b6b6302SHans Verkuil 	struct cx88_core *core = dev->core;
1017b285192aSMauro Carvalho Chehab 
1018b285192aSMauro Carvalho Chehab 	*tvnorm = core->tvnorm;
1019b285192aSMauro Carvalho Chehab 	return 0;
1020b285192aSMauro Carvalho Chehab }
1021b285192aSMauro Carvalho Chehab 
vidioc_s_std(struct file * file,void * priv,v4l2_std_id id)1022314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
1023b285192aSMauro Carvalho Chehab {
1024b285192aSMauro Carvalho Chehab 	struct cx8802_dev *dev = video_drvdata(file);
10250b6b6302SHans Verkuil 	struct cx88_core *core = dev->core;
1026b285192aSMauro Carvalho Chehab 
1027078859a3SHans Verkuil 	return cx88_set_tvnorm(core, id);
1028b285192aSMauro Carvalho Chehab }
1029b285192aSMauro Carvalho Chehab 
10307b61ba8fSMauro Carvalho Chehab static const struct v4l2_file_operations mpeg_fops = {
1031b285192aSMauro Carvalho Chehab 	.owner	       = THIS_MODULE,
10320b6b6302SHans Verkuil 	.open	       = v4l2_fh_open,
10330b6b6302SHans Verkuil 	.release       = vb2_fop_release,
10340b6b6302SHans Verkuil 	.read	       = vb2_fop_read,
10350b6b6302SHans Verkuil 	.poll          = vb2_fop_poll,
10360b6b6302SHans Verkuil 	.mmap	       = vb2_fop_mmap,
1037b285192aSMauro Carvalho Chehab 	.unlocked_ioctl = video_ioctl2,
1038b285192aSMauro Carvalho Chehab };
1039b285192aSMauro Carvalho Chehab 
1040b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1041b285192aSMauro Carvalho Chehab 	.vidioc_querycap      = vidioc_querycap,
1042b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1043b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1044b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1045b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
10460b6b6302SHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
10470b6b6302SHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
10480b6b6302SHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
10490b6b6302SHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
10500b6b6302SHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
10510b6b6302SHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
1052b285192aSMauro Carvalho Chehab 	.vidioc_s_frequency   = vidioc_s_frequency,
1053b285192aSMauro Carvalho Chehab 	.vidioc_log_status    = vidioc_log_status,
1054b285192aSMauro Carvalho Chehab 	.vidioc_enum_input    = vidioc_enum_input,
1055b285192aSMauro Carvalho Chehab 	.vidioc_g_frequency   = vidioc_g_frequency,
1056b285192aSMauro Carvalho Chehab 	.vidioc_g_input       = vidioc_g_input,
1057b285192aSMauro Carvalho Chehab 	.vidioc_s_input       = vidioc_s_input,
1058b285192aSMauro Carvalho Chehab 	.vidioc_g_tuner       = vidioc_g_tuner,
1059b285192aSMauro Carvalho Chehab 	.vidioc_s_tuner       = vidioc_s_tuner,
1060b285192aSMauro Carvalho Chehab 	.vidioc_g_std         = vidioc_g_std,
1061b285192aSMauro Carvalho Chehab 	.vidioc_s_std         = vidioc_s_std,
1062b285192aSMauro Carvalho Chehab 	.vidioc_subscribe_event      = v4l2_ctrl_subscribe_event,
1063b285192aSMauro Carvalho Chehab 	.vidioc_unsubscribe_event    = v4l2_event_unsubscribe,
1064b285192aSMauro Carvalho Chehab };
1065b285192aSMauro Carvalho Chehab 
1066507e1909SBhumika Goyal static const struct video_device cx8802_mpeg_template = {
1067b285192aSMauro Carvalho Chehab 	.name                 = "cx8802",
1068b285192aSMauro Carvalho Chehab 	.fops                 = &mpeg_fops,
1069b285192aSMauro Carvalho Chehab 	.ioctl_ops	      = &mpeg_ioctl_ops,
1070b285192aSMauro Carvalho Chehab 	.tvnorms              = CX88_NORMS,
1071b285192aSMauro Carvalho Chehab };
1072b285192aSMauro Carvalho Chehab 
1073b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
1074b285192aSMauro Carvalho Chehab 
1075b285192aSMauro Carvalho Chehab /* The CX8802 MPEG API will call this when we can use the hardware */
cx8802_blackbird_advise_acquire(struct cx8802_driver * drv)1076b285192aSMauro Carvalho Chehab static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
1077b285192aSMauro Carvalho Chehab {
1078b285192aSMauro Carvalho Chehab 	struct cx88_core *core = drv->core;
1079b285192aSMauro Carvalho Chehab 	int err = 0;
1080b285192aSMauro Carvalho Chehab 
1081b285192aSMauro Carvalho Chehab 	switch (core->boardnr) {
1082b285192aSMauro Carvalho Chehab 	case CX88_BOARD_HAUPPAUGE_HVR1300:
1083399426caSMauro Carvalho Chehab 		/*
1084399426caSMauro Carvalho Chehab 		 * By default, core setup will leave the cx22702 out of reset,
1085399426caSMauro Carvalho Chehab 		 * on the bus.
1086b285192aSMauro Carvalho Chehab 		 * We left the hardware on power up with the cx22702 active.
1087b285192aSMauro Carvalho Chehab 		 * We're being given access to re-arrange the GPIOs.
1088b285192aSMauro Carvalho Chehab 		 * Take the bus off the cx22702 and put the cx23416 on it.
1089b285192aSMauro Carvalho Chehab 		 */
1090b285192aSMauro Carvalho Chehab 		/* Toggle reset on cx22702 leaving i2c active */
1091b285192aSMauro Carvalho Chehab 		cx_set(MO_GP0_IO, 0x00000080);
1092b285192aSMauro Carvalho Chehab 		udelay(1000);
1093b285192aSMauro Carvalho Chehab 		cx_clear(MO_GP0_IO, 0x00000080);
1094b285192aSMauro Carvalho Chehab 		udelay(50);
1095b285192aSMauro Carvalho Chehab 		cx_set(MO_GP0_IO, 0x00000080);
1096b285192aSMauro Carvalho Chehab 		udelay(1000);
1097b285192aSMauro Carvalho Chehab 		/* tri-state the cx22702 pins */
1098b285192aSMauro Carvalho Chehab 		cx_set(MO_GP0_IO, 0x00000004);
1099b285192aSMauro Carvalho Chehab 		udelay(1000);
1100b285192aSMauro Carvalho Chehab 		break;
1101b285192aSMauro Carvalho Chehab 	default:
1102b285192aSMauro Carvalho Chehab 		err = -ENODEV;
1103b285192aSMauro Carvalho Chehab 	}
1104b285192aSMauro Carvalho Chehab 	return err;
1105b285192aSMauro Carvalho Chehab }
1106b285192aSMauro Carvalho Chehab 
1107b285192aSMauro Carvalho Chehab /* The CX8802 MPEG API will call this when we need to release the hardware */
cx8802_blackbird_advise_release(struct cx8802_driver * drv)1108b285192aSMauro Carvalho Chehab static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
1109b285192aSMauro Carvalho Chehab {
1110b285192aSMauro Carvalho Chehab 	struct cx88_core *core = drv->core;
1111b285192aSMauro Carvalho Chehab 	int err = 0;
1112b285192aSMauro Carvalho Chehab 
1113b285192aSMauro Carvalho Chehab 	switch (core->boardnr) {
1114b285192aSMauro Carvalho Chehab 	case CX88_BOARD_HAUPPAUGE_HVR1300:
1115b285192aSMauro Carvalho Chehab 		/* Exit leaving the cx23416 on the bus */
1116b285192aSMauro Carvalho Chehab 		break;
1117b285192aSMauro Carvalho Chehab 	default:
1118b285192aSMauro Carvalho Chehab 		err = -ENODEV;
1119b285192aSMauro Carvalho Chehab 	}
1120b285192aSMauro Carvalho Chehab 	return err;
1121b285192aSMauro Carvalho Chehab }
1122b285192aSMauro Carvalho Chehab 
blackbird_unregister_video(struct cx8802_dev * dev)1123b285192aSMauro Carvalho Chehab static void blackbird_unregister_video(struct cx8802_dev *dev)
1124b285192aSMauro Carvalho Chehab {
112534080bc2SHans Verkuil 	video_unregister_device(&dev->mpeg_dev);
1126b285192aSMauro Carvalho Chehab }
1127b285192aSMauro Carvalho Chehab 
blackbird_register_video(struct cx8802_dev * dev)1128b285192aSMauro Carvalho Chehab static int blackbird_register_video(struct cx8802_dev *dev)
1129b285192aSMauro Carvalho Chehab {
1130b285192aSMauro Carvalho Chehab 	int err;
1131b285192aSMauro Carvalho Chehab 
113234080bc2SHans Verkuil 	cx88_vdev_init(dev->core, dev->pci, &dev->mpeg_dev,
1133b285192aSMauro Carvalho Chehab 		       &cx8802_mpeg_template, "mpeg");
113434080bc2SHans Verkuil 	dev->mpeg_dev.ctrl_handler = &dev->cxhdl.hdl;
113534080bc2SHans Verkuil 	video_set_drvdata(&dev->mpeg_dev, dev);
113634080bc2SHans Verkuil 	dev->mpeg_dev.queue = &dev->vb2_mpegq;
113721615365SHans Verkuil 	dev->mpeg_dev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
113821615365SHans Verkuil 				    V4L2_CAP_VIDEO_CAPTURE;
113921615365SHans Verkuil 	if (dev->core->board.tuner_type != UNSET)
114021615365SHans Verkuil 		dev->mpeg_dev.device_caps |= V4L2_CAP_TUNER;
11413e30a927SHans Verkuil 	err = video_register_device(&dev->mpeg_dev, VFL_TYPE_VIDEO, -1);
1142b285192aSMauro Carvalho Chehab 	if (err < 0) {
114365bc2fe8SMauro Carvalho Chehab 		pr_info("can't register mpeg device\n");
1144b285192aSMauro Carvalho Chehab 		return err;
1145b285192aSMauro Carvalho Chehab 	}
114665bc2fe8SMauro Carvalho Chehab 	pr_info("registered device %s [mpeg]\n",
114765bc2fe8SMauro Carvalho Chehab 		video_device_node_name(&dev->mpeg_dev));
1148b285192aSMauro Carvalho Chehab 	return 0;
1149b285192aSMauro Carvalho Chehab }
1150b285192aSMauro Carvalho Chehab 
1151b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1152b285192aSMauro Carvalho Chehab 
cx8802_blackbird_probe(struct cx8802_driver * drv)1153b285192aSMauro Carvalho Chehab static int cx8802_blackbird_probe(struct cx8802_driver *drv)
1154b285192aSMauro Carvalho Chehab {
1155b285192aSMauro Carvalho Chehab 	struct cx88_core *core = drv->core;
1156b285192aSMauro Carvalho Chehab 	struct cx8802_dev *dev = core->dvbdev;
11570b6b6302SHans Verkuil 	struct vb2_queue *q;
1158b285192aSMauro Carvalho Chehab 	int err;
1159b285192aSMauro Carvalho Chehab 
1160b285192aSMauro Carvalho Chehab 	dprintk(1, "%s\n", __func__);
1161b285192aSMauro Carvalho Chehab 	dprintk(1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1162b285192aSMauro Carvalho Chehab 		core->boardnr,
1163b285192aSMauro Carvalho Chehab 		core->name,
1164b285192aSMauro Carvalho Chehab 		core->pci_bus,
1165b285192aSMauro Carvalho Chehab 		core->pci_slot);
1166b285192aSMauro Carvalho Chehab 
1167b285192aSMauro Carvalho Chehab 	err = -ENODEV;
1168b285192aSMauro Carvalho Chehab 	if (!(core->board.mpeg & CX88_MPEG_BLACKBIRD))
1169b285192aSMauro Carvalho Chehab 		goto fail_core;
1170b285192aSMauro Carvalho Chehab 
1171b285192aSMauro Carvalho Chehab 	dev->cxhdl.port = CX2341X_PORT_STREAMING;
1172ccd6f1d4SHans Verkuil 	dev->cxhdl.width = core->width;
1173ccd6f1d4SHans Verkuil 	dev->cxhdl.height = core->height;
1174b285192aSMauro Carvalho Chehab 	dev->cxhdl.func = blackbird_mbox_func;
1175b285192aSMauro Carvalho Chehab 	dev->cxhdl.priv = dev;
1176b285192aSMauro Carvalho Chehab 	err = cx2341x_handler_init(&dev->cxhdl, 36);
1177b285192aSMauro Carvalho Chehab 	if (err)
1178b285192aSMauro Carvalho Chehab 		goto fail_core;
1179da1b1aeaSHans Verkuil 	v4l2_ctrl_add_handler(&dev->cxhdl.hdl, &core->video_hdl, NULL, false);
1180b285192aSMauro Carvalho Chehab 
1181b285192aSMauro Carvalho Chehab 	/* blackbird stuff */
118265bc2fe8SMauro Carvalho Chehab 	pr_info("cx23416 based mpeg encoder (blackbird reference design)\n");
1183b285192aSMauro Carvalho Chehab 	host_setup(dev->core);
1184b285192aSMauro Carvalho Chehab 
1185b285192aSMauro Carvalho Chehab 	blackbird_initialize_codec(dev);
1186b285192aSMauro Carvalho Chehab 
1187b285192aSMauro Carvalho Chehab 	/* initial device configuration: needed ? */
1188b285192aSMauro Carvalho Chehab //	init_controls(core);
1189b285192aSMauro Carvalho Chehab 	cx88_set_tvnorm(core, core->tvnorm);
1190b285192aSMauro Carvalho Chehab 	cx88_video_mux(core, 0);
1191ccd6f1d4SHans Verkuil 	cx2341x_handler_set_50hz(&dev->cxhdl, core->height == 576);
1192b285192aSMauro Carvalho Chehab 	cx2341x_handler_setup(&dev->cxhdl);
11930b6b6302SHans Verkuil 
11940b6b6302SHans Verkuil 	q = &dev->vb2_mpegq;
11950b6b6302SHans Verkuil 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
11960b6b6302SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
11970b6b6302SHans Verkuil 	q->gfp_flags = GFP_DMA32;
11980b6b6302SHans Verkuil 	q->min_buffers_needed = 2;
11990b6b6302SHans Verkuil 	q->drv_priv = dev;
12000b6b6302SHans Verkuil 	q->buf_struct_size = sizeof(struct cx88_buffer);
12010b6b6302SHans Verkuil 	q->ops = &blackbird_qops;
12020b6b6302SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
12030b6b6302SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
12040b6b6302SHans Verkuil 	q->lock = &core->lock;
12052bc46b3aSHans Verkuil 	q->dev = &dev->pci->dev;
12060b6b6302SHans Verkuil 
12070b6b6302SHans Verkuil 	err = vb2_queue_init(q);
12080b6b6302SHans Verkuil 	if (err < 0)
12090b6b6302SHans Verkuil 		goto fail_core;
12100b6b6302SHans Verkuil 
1211b285192aSMauro Carvalho Chehab 	blackbird_register_video(dev);
1212b285192aSMauro Carvalho Chehab 
1213b285192aSMauro Carvalho Chehab 	return 0;
1214b285192aSMauro Carvalho Chehab 
1215b285192aSMauro Carvalho Chehab fail_core:
1216b285192aSMauro Carvalho Chehab 	return err;
1217b285192aSMauro Carvalho Chehab }
1218b285192aSMauro Carvalho Chehab 
cx8802_blackbird_remove(struct cx8802_driver * drv)1219b285192aSMauro Carvalho Chehab static int cx8802_blackbird_remove(struct cx8802_driver *drv)
1220b285192aSMauro Carvalho Chehab {
1221b285192aSMauro Carvalho Chehab 	struct cx88_core *core = drv->core;
1222b285192aSMauro Carvalho Chehab 	struct cx8802_dev *dev = core->dvbdev;
1223b285192aSMauro Carvalho Chehab 
1224b285192aSMauro Carvalho Chehab 	/* blackbird */
1225b285192aSMauro Carvalho Chehab 	blackbird_unregister_video(drv->core->dvbdev);
1226b285192aSMauro Carvalho Chehab 	v4l2_ctrl_handler_free(&dev->cxhdl.hdl);
1227b285192aSMauro Carvalho Chehab 
1228b285192aSMauro Carvalho Chehab 	return 0;
1229b285192aSMauro Carvalho Chehab }
1230b285192aSMauro Carvalho Chehab 
1231b285192aSMauro Carvalho Chehab static struct cx8802_driver cx8802_blackbird_driver = {
1232b285192aSMauro Carvalho Chehab 	.type_id	= CX88_MPEG_BLACKBIRD,
1233b285192aSMauro Carvalho Chehab 	.hw_access	= CX8802_DRVCTL_SHARED,
1234b285192aSMauro Carvalho Chehab 	.probe		= cx8802_blackbird_probe,
1235b285192aSMauro Carvalho Chehab 	.remove		= cx8802_blackbird_remove,
1236b285192aSMauro Carvalho Chehab 	.advise_acquire	= cx8802_blackbird_advise_acquire,
1237b285192aSMauro Carvalho Chehab 	.advise_release	= cx8802_blackbird_advise_release,
1238b285192aSMauro Carvalho Chehab };
1239b285192aSMauro Carvalho Chehab 
blackbird_init(void)1240b285192aSMauro Carvalho Chehab static int __init blackbird_init(void)
1241b285192aSMauro Carvalho Chehab {
124265bc2fe8SMauro Carvalho Chehab 	pr_info("cx2388x blackbird driver version %s loaded\n",
1243b285192aSMauro Carvalho Chehab 		CX88_VERSION);
1244b285192aSMauro Carvalho Chehab 	return cx8802_register_driver(&cx8802_blackbird_driver);
1245b285192aSMauro Carvalho Chehab }
1246b285192aSMauro Carvalho Chehab 
blackbird_fini(void)1247b285192aSMauro Carvalho Chehab static void __exit blackbird_fini(void)
1248b285192aSMauro Carvalho Chehab {
1249b285192aSMauro Carvalho Chehab 	cx8802_unregister_driver(&cx8802_blackbird_driver);
1250b285192aSMauro Carvalho Chehab }
1251b285192aSMauro Carvalho Chehab 
1252b285192aSMauro Carvalho Chehab module_init(blackbird_init);
1253b285192aSMauro Carvalho Chehab module_exit(blackbird_fini);
1254