blob: 567d7ab1663d08fd3acade9e67e8eca5aa926e20 [file] [log] [blame]
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001/*
2 *
3 * Support for a cx23417 mpeg encoder via cx231xx host port.
4 *
5 * (c) 2004 Jelle Foks <jelle@foks.us>
6 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
7 * (c) 2008 Steven Toth <stoth@linuxtv.org>
8 * - CX23885/7/8 support
9 *
10 * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/init.h>
30#include <linux/fs.h>
31#include <linux/delay.h>
32#include <linux/device.h>
33#include <linux/firmware.h>
Stephen Rothwell94399432010-10-19 18:07:30 +110034#include <linux/vmalloc.h>
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030035#include <media/v4l2-common.h>
36#include <media/v4l2-ioctl.h>
37#include <media/cx2341x.h>
Hans Verkuilb86d1542013-01-29 13:16:06 -030038#include <media/tuner.h>
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030039#include <linux/usb.h>
40
41#include "cx231xx.h"
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030042
43#define CX231xx_FIRM_IMAGE_SIZE 376836
44#define CX231xx_FIRM_IMAGE_NAME "v4l-cx23885-enc.fw"
45
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -030046/* for polaris ITVC */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030047#define ITVC_WRITE_DIR 0x03FDFC00
48#define ITVC_READ_DIR 0x0001FC00
49
50#define MCI_MEMORY_DATA_BYTE0 0x00
51#define MCI_MEMORY_DATA_BYTE1 0x08
52#define MCI_MEMORY_DATA_BYTE2 0x10
53#define MCI_MEMORY_DATA_BYTE3 0x18
54
55#define MCI_MEMORY_ADDRESS_BYTE2 0x20
56#define MCI_MEMORY_ADDRESS_BYTE1 0x28
57#define MCI_MEMORY_ADDRESS_BYTE0 0x30
58
59#define MCI_REGISTER_DATA_BYTE0 0x40
60#define MCI_REGISTER_DATA_BYTE1 0x48
61#define MCI_REGISTER_DATA_BYTE2 0x50
62#define MCI_REGISTER_DATA_BYTE3 0x58
63
64#define MCI_REGISTER_ADDRESS_BYTE0 0x60
65#define MCI_REGISTER_ADDRESS_BYTE1 0x68
66
67#define MCI_REGISTER_MODE 0x70
68
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -030069/* Read and write modes for polaris ITVC */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030070#define MCI_MODE_REGISTER_READ 0x000
71#define MCI_MODE_REGISTER_WRITE 0x100
72#define MCI_MODE_MEMORY_READ 0x000
73#define MCI_MODE_MEMORY_WRITE 0x4000
74
75static unsigned int mpegbufs = 8;
76module_param(mpegbufs, int, 0644);
77MODULE_PARM_DESC(mpegbufs, "number of mpeg buffers, range 2-32");
Hans Verkuil5b8acdc2013-01-29 12:59:50 -030078
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030079static unsigned int mpeglines = 128;
80module_param(mpeglines, int, 0644);
81MODULE_PARM_DESC(mpeglines, "number of lines in an MPEG buffer, range 2-32");
Hans Verkuil5b8acdc2013-01-29 12:59:50 -030082
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030083static unsigned int mpeglinesize = 512;
84module_param(mpeglinesize, int, 0644);
85MODULE_PARM_DESC(mpeglinesize,
86 "number of bytes in each line of an MPEG buffer, range 512-1024");
87
88static unsigned int v4l_debug = 1;
89module_param(v4l_debug, int, 0644);
90MODULE_PARM_DESC(v4l_debug, "enable V4L debug messages");
Hans Verkuil5b8acdc2013-01-29 12:59:50 -030091
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030092#define dprintk(level, fmt, arg...)\
93 do { if (v4l_debug >= level) \
Hans Verkuil5b8acdc2013-01-29 12:59:50 -030094 pr_info("%s: " fmt, \
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -030095 (dev) ? dev->name : "cx231xx[?]", ## arg); \
96 } while (0)
97
98static struct cx231xx_tvnorm cx231xx_tvnorms[] = {
99 {
100 .name = "NTSC-M",
101 .id = V4L2_STD_NTSC_M,
102 }, {
103 .name = "NTSC-JP",
104 .id = V4L2_STD_NTSC_M_JP,
105 }, {
106 .name = "PAL-BG",
107 .id = V4L2_STD_PAL_BG,
108 }, {
109 .name = "PAL-DK",
110 .id = V4L2_STD_PAL_DK,
111 }, {
112 .name = "PAL-I",
113 .id = V4L2_STD_PAL_I,
114 }, {
115 .name = "PAL-M",
116 .id = V4L2_STD_PAL_M,
117 }, {
118 .name = "PAL-N",
119 .id = V4L2_STD_PAL_N,
120 }, {
121 .name = "PAL-Nc",
122 .id = V4L2_STD_PAL_Nc,
123 }, {
124 .name = "PAL-60",
125 .id = V4L2_STD_PAL_60,
126 }, {
127 .name = "SECAM-L",
128 .id = V4L2_STD_SECAM_L,
129 }, {
130 .name = "SECAM-DK",
131 .id = V4L2_STD_SECAM_DK,
132 }
133};
134
135/* ------------------------------------------------------------------ */
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300136
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300137enum cx231xx_capture_type {
138 CX231xx_MPEG_CAPTURE,
139 CX231xx_RAW_CAPTURE,
140 CX231xx_RAW_PASSTHRU_CAPTURE
141};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300142
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300143enum cx231xx_capture_bits {
144 CX231xx_RAW_BITS_NONE = 0x00,
145 CX231xx_RAW_BITS_YUV_CAPTURE = 0x01,
146 CX231xx_RAW_BITS_PCM_CAPTURE = 0x02,
147 CX231xx_RAW_BITS_VBI_CAPTURE = 0x04,
148 CX231xx_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
149 CX231xx_RAW_BITS_TO_HOST_CAPTURE = 0x10
150};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300151
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300152enum cx231xx_capture_end {
153 CX231xx_END_AT_GOP, /* stop at the end of gop, generate irq */
154 CX231xx_END_NOW, /* stop immediately, no irq */
155};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300156
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300157enum cx231xx_framerate {
158 CX231xx_FRAMERATE_NTSC_30, /* NTSC: 30fps */
159 CX231xx_FRAMERATE_PAL_25 /* PAL: 25fps */
160};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300161
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300162enum cx231xx_stream_port {
163 CX231xx_OUTPUT_PORT_MEMORY,
164 CX231xx_OUTPUT_PORT_STREAMING,
165 CX231xx_OUTPUT_PORT_SERIAL
166};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300167
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300168enum cx231xx_data_xfer_status {
169 CX231xx_MORE_BUFFERS_FOLLOW,
170 CX231xx_LAST_BUFFER,
171};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300172
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300173enum cx231xx_picture_mask {
174 CX231xx_PICTURE_MASK_NONE,
175 CX231xx_PICTURE_MASK_I_FRAMES,
176 CX231xx_PICTURE_MASK_I_P_FRAMES = 0x3,
177 CX231xx_PICTURE_MASK_ALL_FRAMES = 0x7,
178};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300179
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300180enum cx231xx_vbi_mode_bits {
181 CX231xx_VBI_BITS_SLICED,
182 CX231xx_VBI_BITS_RAW,
183};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300184
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300185enum cx231xx_vbi_insertion_bits {
186 CX231xx_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
187 CX231xx_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
188 CX231xx_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
189 CX231xx_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
190 CX231xx_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
191};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300192
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300193enum cx231xx_dma_unit {
194 CX231xx_DMA_BYTES,
195 CX231xx_DMA_FRAMES,
196};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300197
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300198enum cx231xx_dma_transfer_status_bits {
199 CX231xx_DMA_TRANSFER_BITS_DONE = 0x01,
200 CX231xx_DMA_TRANSFER_BITS_ERROR = 0x04,
201 CX231xx_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
202};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300203
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300204enum cx231xx_pause {
205 CX231xx_PAUSE_ENCODING,
206 CX231xx_RESUME_ENCODING,
207};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300208
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300209enum cx231xx_copyright {
210 CX231xx_COPYRIGHT_OFF,
211 CX231xx_COPYRIGHT_ON,
212};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300213
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300214enum cx231xx_notification_type {
215 CX231xx_NOTIFICATION_REFRESH,
216};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300217
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300218enum cx231xx_notification_status {
219 CX231xx_NOTIFICATION_OFF,
220 CX231xx_NOTIFICATION_ON,
221};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300222
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300223enum cx231xx_notification_mailbox {
224 CX231xx_NOTIFICATION_NO_MAILBOX = -1,
225};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300226
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300227enum cx231xx_field1_lines {
228 CX231xx_FIELD1_SAA7114 = 0x00EF, /* 239 */
229 CX231xx_FIELD1_SAA7115 = 0x00F0, /* 240 */
230 CX231xx_FIELD1_MICRONAS = 0x0105, /* 261 */
231};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300232
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300233enum cx231xx_field2_lines {
234 CX231xx_FIELD2_SAA7114 = 0x00EF, /* 239 */
235 CX231xx_FIELD2_SAA7115 = 0x00F0, /* 240 */
236 CX231xx_FIELD2_MICRONAS = 0x0106, /* 262 */
237};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300238
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300239enum cx231xx_custom_data_type {
240 CX231xx_CUSTOM_EXTENSION_USR_DATA,
241 CX231xx_CUSTOM_PRIVATE_PACKET,
242};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300243
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300244enum cx231xx_mute {
245 CX231xx_UNMUTE,
246 CX231xx_MUTE,
247};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300248
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300249enum cx231xx_mute_video_mask {
250 CX231xx_MUTE_VIDEO_V_MASK = 0x0000FF00,
251 CX231xx_MUTE_VIDEO_U_MASK = 0x00FF0000,
252 CX231xx_MUTE_VIDEO_Y_MASK = 0xFF000000,
253};
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300254
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300255enum cx231xx_mute_video_shift {
256 CX231xx_MUTE_VIDEO_V_SHIFT = 8,
257 CX231xx_MUTE_VIDEO_U_SHIFT = 16,
258 CX231xx_MUTE_VIDEO_Y_SHIFT = 24,
259};
260
261/* defines below are from ivtv-driver.h */
262#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
263
264/* Firmware API commands */
265#define IVTV_API_STD_TIMEOUT 500
266
267/* Registers */
268/* IVTV_REG_OFFSET */
269#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
270#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
271#define IVTV_REG_SPU (0x9050)
272#define IVTV_REG_HW_BLOCKS (0x9054)
273#define IVTV_REG_VPU (0x9058)
274#define IVTV_REG_APU (0xA064)
275
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300276/*
277 * Bit definitions for MC417_RWD and MC417_OEN registers
278 *
279 * bits 31-16
280 *+-----------+
281 *| Reserved |
282 *|+-----------+
283 *| bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
284 *|+-------+-------+-------+-------+-------+-------+-------+-------+
285 *|| MIWR# | MIRD# | MICS# |MIRDY# |MIADDR3|MIADDR2|MIADDR1|MIADDR0|
286 *|+-------+-------+-------+-------+-------+-------+-------+-------+
287 *| bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
288 *|+-------+-------+-------+-------+-------+-------+-------+-------+
289 *||MIDATA7|MIDATA6|MIDATA5|MIDATA4|MIDATA3|MIDATA2|MIDATA1|MIDATA0|
290 *|+-------+-------+-------+-------+-------+-------+-------+-------+
291 */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300292#define MC417_MIWR 0x8000
293#define MC417_MIRD 0x4000
294#define MC417_MICS 0x2000
295#define MC417_MIRDY 0x1000
296#define MC417_MIADDR 0x0F00
297#define MC417_MIDATA 0x00FF
298
299
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300300/* Bit definitions for MC417_CTL register ****
301 *bits 31-6 bits 5-4 bit 3 bits 2-1 Bit 0
302 *+--------+-------------+--------+--------------+------------+
303 *|Reserved|MC417_SPD_CTL|Reserved|MC417_GPIO_SEL|UART_GPIO_EN|
304 *+--------+-------------+--------+--------------+------------+
305 */
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300306#define MC417_SPD_CTL(x) (((x) << 4) & 0x00000030)
307#define MC417_GPIO_SEL(x) (((x) << 1) & 0x00000006)
308#define MC417_UART_GPIO_EN 0x00000001
309
310/* Values for speed control */
311#define MC417_SPD_CTL_SLOW 0x1
312#define MC417_SPD_CTL_MEDIUM 0x0
313#define MC417_SPD_CTL_FAST 0x3 /* b'1x, but we use b'11 */
314
315/* Values for GPIO select */
316#define MC417_GPIO_SEL_GPIO3 0x3
317#define MC417_GPIO_SEL_GPIO2 0x2
318#define MC417_GPIO_SEL_GPIO1 0x1
319#define MC417_GPIO_SEL_GPIO0 0x0
320
321
322#define CX23417_GPIO_MASK 0xFC0003FF
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300323
324static int set_itvc_reg(struct cx231xx *dev, u32 gpio_direction, u32 value)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300325{
326 int status = 0;
327 u32 _gpio_direction = 0;
328
329 _gpio_direction = _gpio_direction & CX23417_GPIO_MASK;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300330 _gpio_direction = _gpio_direction | gpio_direction;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300331 status = cx231xx_send_gpio_cmd(dev, _gpio_direction,
332 (u8 *)&value, 4, 0, 0);
333 return status;
334}
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300335
336static int get_itvc_reg(struct cx231xx *dev, u32 gpio_direction, u32 *val_ptr)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300337{
338 int status = 0;
339 u32 _gpio_direction = 0;
340
341 _gpio_direction = _gpio_direction & CX23417_GPIO_MASK;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300342 _gpio_direction = _gpio_direction | gpio_direction;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300343
344 status = cx231xx_send_gpio_cmd(dev, _gpio_direction,
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300345 (u8 *)val_ptr, 4, 0, 1);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300346 return status;
347}
Mauro Carvalho Chehab82c3cca2010-10-07 21:01:31 -0300348
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300349static int wait_for_mci_complete(struct cx231xx *dev)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300350{
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300351 u32 gpio;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300352 u32 gpio_direction = 0;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300353 u8 count = 0;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300354 get_itvc_reg(dev, gpio_direction, &gpio);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300355
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300356 while (!(gpio&0x020000)) {
357 msleep(10);
358
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300359 get_itvc_reg(dev, gpio_direction, &gpio);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300360
361 if (count++ > 100) {
362 dprintk(3, "ERROR: Timeout - gpio=%x\n", gpio);
363 return -1;
364 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300365 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300366 return 0;
367}
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300368
Mauro Carvalho Chehab82c3cca2010-10-07 21:01:31 -0300369static int mc417_register_write(struct cx231xx *dev, u16 address, u32 value)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300370{
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300371 u32 temp;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300372 int status = 0;
373
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300374 temp = 0x82 | MCI_REGISTER_DATA_BYTE0 | ((value & 0x000000FF) << 8);
375 temp = temp << 10;
376 status = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300377 if (status < 0)
378 return status;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300379 temp = temp | (0x05 << 10);
380 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300381
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300382 /*write data byte 1;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300383 temp = 0x82 | MCI_REGISTER_DATA_BYTE1 | (value & 0x0000FF00);
384 temp = temp << 10;
385 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
386 temp = temp | (0x05 << 10);
387 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300388
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300389 /*write data byte 2;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300390 temp = 0x82 | MCI_REGISTER_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
391 temp = temp << 10;
392 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
393 temp = temp | (0x05 << 10);
394 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300395
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300396 /*write data byte 3;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300397 temp = 0x82 | MCI_REGISTER_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
398 temp = temp << 10;
399 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
400 temp = temp | (0x05 << 10);
401 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300402
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300403 /*write address byte 0;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300404 temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE0 | ((address & 0x000000FF) << 8);
405 temp = temp << 10;
406 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
407 temp = temp | (0x05 << 10);
408 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300409
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300410 /*write address byte 1;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300411 temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE1 | (address & 0x0000FF00);
412 temp = temp << 10;
413 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
414 temp = temp | (0x05 << 10);
415 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300416
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300417 /*Write that the mode is write.*/
418 temp = 0x82 | MCI_REGISTER_MODE | MCI_MODE_REGISTER_WRITE;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300419 temp = temp << 10;
420 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
421 temp = temp | (0x05 << 10);
422 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300423
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300424 return wait_for_mci_complete(dev);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300425}
426
Mauro Carvalho Chehab82c3cca2010-10-07 21:01:31 -0300427static int mc417_register_read(struct cx231xx *dev, u16 address, u32 *value)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300428{
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300429 /*write address byte 0;*/
430 u32 temp;
431 u32 return_value = 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300432 int ret = 0;
433
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300434 temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
435 temp = temp << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300436 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300437 temp = temp | ((0x05) << 10);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300438 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300439
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300440 /*write address byte 1;*/
441 temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE1 | (address & 0xFF00);
442 temp = temp << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300443 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300444 temp = temp | ((0x05) << 10);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300445 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300446
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300447 /*write that the mode is read;*/
448 temp = 0x82 | MCI_REGISTER_MODE | MCI_MODE_REGISTER_READ;
449 temp = temp << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300450 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300451 temp = temp | ((0x05) << 10);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300452 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300453
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300454 /*wait for the MIRDY line to be asserted ,
455 signalling that the read is done;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300456 ret = wait_for_mci_complete(dev);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300457
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300458 /*switch the DATA- GPIO to input mode;*/
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300459
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300460 /*Read data byte 0;*/
461 temp = (0x82 | MCI_REGISTER_DATA_BYTE0) << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300462 set_itvc_reg(dev, ITVC_READ_DIR, temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300463 temp = ((0x81 | MCI_REGISTER_DATA_BYTE0) << 10);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300464 set_itvc_reg(dev, ITVC_READ_DIR, temp);
465 get_itvc_reg(dev, ITVC_READ_DIR, &temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300466 return_value |= ((temp & 0x03FC0000) >> 18);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300467 set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300468
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300469 /* Read data byte 1;*/
470 temp = (0x82 | MCI_REGISTER_DATA_BYTE1) << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300471 set_itvc_reg(dev, ITVC_READ_DIR, temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300472 temp = ((0x81 | MCI_REGISTER_DATA_BYTE1) << 10);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300473 set_itvc_reg(dev, ITVC_READ_DIR, temp);
474 get_itvc_reg(dev, ITVC_READ_DIR, &temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300475
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300476 return_value |= ((temp & 0x03FC0000) >> 10);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300477 set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300478
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300479 /*Read data byte 2;*/
480 temp = (0x82 | MCI_REGISTER_DATA_BYTE2) << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300481 set_itvc_reg(dev, ITVC_READ_DIR, temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300482 temp = ((0x81 | MCI_REGISTER_DATA_BYTE2) << 10);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300483 set_itvc_reg(dev, ITVC_READ_DIR, temp);
484 get_itvc_reg(dev, ITVC_READ_DIR, &temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300485 return_value |= ((temp & 0x03FC0000) >> 2);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300486 set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300487
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300488 /*Read data byte 3;*/
489 temp = (0x82 | MCI_REGISTER_DATA_BYTE3) << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300490 set_itvc_reg(dev, ITVC_READ_DIR, temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300491 temp = ((0x81 | MCI_REGISTER_DATA_BYTE3) << 10);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300492 set_itvc_reg(dev, ITVC_READ_DIR, temp);
493 get_itvc_reg(dev, ITVC_READ_DIR, &temp);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300494 return_value |= ((temp & 0x03FC0000) << 6);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300495 set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300496
497 *value = return_value;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300498 return ret;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300499}
500
Mauro Carvalho Chehab82c3cca2010-10-07 21:01:31 -0300501static int mc417_memory_write(struct cx231xx *dev, u32 address, u32 value)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300502{
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300503 /*write data byte 0;*/
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300504
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300505 u32 temp;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300506 int ret = 0;
507
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300508 temp = 0x82 | MCI_MEMORY_DATA_BYTE0 | ((value & 0x000000FF) << 8);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300509 temp = temp << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300510 ret = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300511 if (ret < 0)
512 return ret;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300513 temp = temp | (0x05 << 10);
514 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300515
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300516 /*write data byte 1;*/
517 temp = 0x82 | MCI_MEMORY_DATA_BYTE1 | (value & 0x0000FF00);
518 temp = temp << 10;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300519 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
520 temp = temp | (0x05 << 10);
521 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300522
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300523 /*write data byte 2;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300524 temp = 0x82 | MCI_MEMORY_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
525 temp = temp << 10;
526 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
527 temp = temp | (0x05 << 10);
528 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300529
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300530 /*write data byte 3;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300531 temp = 0x82 | MCI_MEMORY_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
532 temp = temp << 10;
533 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
534 temp = temp | (0x05 << 10);
535 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300536
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300537 /* write address byte 2;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300538 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_WRITE |
539 ((address & 0x003F0000) >> 8);
540 temp = temp << 10;
541 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
542 temp = temp | (0x05 << 10);
543 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300544
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300545 /* write address byte 1;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300546 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
547 temp = temp << 10;
548 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
549 temp = temp | (0x05 << 10);
550 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300551
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300552 /* write address byte 0;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300553 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
554 temp = temp << 10;
555 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
556 temp = temp | (0x05 << 10);
557 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300558
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300559 /*wait for MIRDY line;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300560 wait_for_mci_complete(dev);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300561
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300562 return 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300563}
564
Mauro Carvalho Chehab82c3cca2010-10-07 21:01:31 -0300565static int mc417_memory_read(struct cx231xx *dev, u32 address, u32 *value)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300566{
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300567 u32 temp = 0;
568 u32 return_value = 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300569 int ret = 0;
570
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300571 /*write address byte 2;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300572 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_READ |
573 ((address & 0x003F0000) >> 8);
574 temp = temp << 10;
575 ret = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300576 if (ret < 0)
577 return ret;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300578 temp = temp | (0x05 << 10);
579 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300580
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300581 /*write address byte 1*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300582 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
583 temp = temp << 10;
584 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
585 temp = temp | (0x05 << 10);
586 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300587
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300588 /*write address byte 0*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300589 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
590 temp = temp << 10;
591 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
592 temp = temp | (0x05 << 10);
593 set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300594
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300595 /*Wait for MIRDY line*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300596 ret = wait_for_mci_complete(dev);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300597
598
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300599 /*Read data byte 3;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300600 temp = (0x82 | MCI_MEMORY_DATA_BYTE3) << 10;
601 set_itvc_reg(dev, ITVC_READ_DIR, temp);
602 temp = ((0x81 | MCI_MEMORY_DATA_BYTE3) << 10);
603 set_itvc_reg(dev, ITVC_READ_DIR, temp);
604 get_itvc_reg(dev, ITVC_READ_DIR, &temp);
605 return_value |= ((temp & 0x03FC0000) << 6);
606 set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300607
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300608 /*Read data byte 2;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300609 temp = (0x82 | MCI_MEMORY_DATA_BYTE2) << 10;
610 set_itvc_reg(dev, ITVC_READ_DIR, temp);
611 temp = ((0x81 | MCI_MEMORY_DATA_BYTE2) << 10);
612 set_itvc_reg(dev, ITVC_READ_DIR, temp);
613 get_itvc_reg(dev, ITVC_READ_DIR, &temp);
614 return_value |= ((temp & 0x03FC0000) >> 2);
615 set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300616
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300617 /* Read data byte 1;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300618 temp = (0x82 | MCI_MEMORY_DATA_BYTE1) << 10;
619 set_itvc_reg(dev, ITVC_READ_DIR, temp);
620 temp = ((0x81 | MCI_MEMORY_DATA_BYTE1) << 10);
621 set_itvc_reg(dev, ITVC_READ_DIR, temp);
622 get_itvc_reg(dev, ITVC_READ_DIR, &temp);
623 return_value |= ((temp & 0x03FC0000) >> 10);
624 set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300625
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300626 /*Read data byte 0;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300627 temp = (0x82 | MCI_MEMORY_DATA_BYTE0) << 10;
628 set_itvc_reg(dev, ITVC_READ_DIR, temp);
629 temp = ((0x81 | MCI_MEMORY_DATA_BYTE0) << 10);
630 set_itvc_reg(dev, ITVC_READ_DIR, temp);
631 get_itvc_reg(dev, ITVC_READ_DIR, &temp);
632 return_value |= ((temp & 0x03FC0000) >> 18);
633 set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300634
635 *value = return_value;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300636 return ret;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300637}
638
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300639/* ------------------------------------------------------------------ */
640
641/* MPEG encoder API */
642static char *cmd_to_str(int cmd)
643{
644 switch (cmd) {
645 case CX2341X_ENC_PING_FW:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300646 return "PING_FW";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300647 case CX2341X_ENC_START_CAPTURE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300648 return "START_CAPTURE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300649 case CX2341X_ENC_STOP_CAPTURE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300650 return "STOP_CAPTURE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300651 case CX2341X_ENC_SET_AUDIO_ID:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300652 return "SET_AUDIO_ID";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300653 case CX2341X_ENC_SET_VIDEO_ID:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300654 return "SET_VIDEO_ID";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300655 case CX2341X_ENC_SET_PCR_ID:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300656 return "SET_PCR_PID";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300657 case CX2341X_ENC_SET_FRAME_RATE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300658 return "SET_FRAME_RATE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300659 case CX2341X_ENC_SET_FRAME_SIZE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300660 return "SET_FRAME_SIZE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300661 case CX2341X_ENC_SET_BIT_RATE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300662 return "SET_BIT_RATE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300663 case CX2341X_ENC_SET_GOP_PROPERTIES:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300664 return "SET_GOP_PROPERTIES";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300665 case CX2341X_ENC_SET_ASPECT_RATIO:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300666 return "SET_ASPECT_RATIO";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300667 case CX2341X_ENC_SET_DNR_FILTER_MODE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300668 return "SET_DNR_FILTER_PROPS";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300669 case CX2341X_ENC_SET_DNR_FILTER_PROPS:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300670 return "SET_DNR_FILTER_PROPS";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300671 case CX2341X_ENC_SET_CORING_LEVELS:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300672 return "SET_CORING_LEVELS";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300673 case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300674 return "SET_SPATIAL_FILTER_TYPE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300675 case CX2341X_ENC_SET_VBI_LINE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300676 return "SET_VBI_LINE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300677 case CX2341X_ENC_SET_STREAM_TYPE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300678 return "SET_STREAM_TYPE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300679 case CX2341X_ENC_SET_OUTPUT_PORT:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300680 return "SET_OUTPUT_PORT";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300681 case CX2341X_ENC_SET_AUDIO_PROPERTIES:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300682 return "SET_AUDIO_PROPERTIES";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300683 case CX2341X_ENC_HALT_FW:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300684 return "HALT_FW";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300685 case CX2341X_ENC_GET_VERSION:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300686 return "GET_VERSION";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300687 case CX2341X_ENC_SET_GOP_CLOSURE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300688 return "SET_GOP_CLOSURE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300689 case CX2341X_ENC_GET_SEQ_END:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300690 return "GET_SEQ_END";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300691 case CX2341X_ENC_SET_PGM_INDEX_INFO:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300692 return "SET_PGM_INDEX_INFO";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300693 case CX2341X_ENC_SET_VBI_CONFIG:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300694 return "SET_VBI_CONFIG";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300695 case CX2341X_ENC_SET_DMA_BLOCK_SIZE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300696 return "SET_DMA_BLOCK_SIZE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300697 case CX2341X_ENC_GET_PREV_DMA_INFO_MB_10:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300698 return "GET_PREV_DMA_INFO_MB_10";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300699 case CX2341X_ENC_GET_PREV_DMA_INFO_MB_9:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300700 return "GET_PREV_DMA_INFO_MB_9";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300701 case CX2341X_ENC_SCHED_DMA_TO_HOST:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300702 return "SCHED_DMA_TO_HOST";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300703 case CX2341X_ENC_INITIALIZE_INPUT:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300704 return "INITIALIZE_INPUT";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300705 case CX2341X_ENC_SET_FRAME_DROP_RATE:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300706 return "SET_FRAME_DROP_RATE";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300707 case CX2341X_ENC_PAUSE_ENCODER:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300708 return "PAUSE_ENCODER";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300709 case CX2341X_ENC_REFRESH_INPUT:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300710 return "REFRESH_INPUT";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300711 case CX2341X_ENC_SET_COPYRIGHT:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300712 return "SET_COPYRIGHT";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300713 case CX2341X_ENC_SET_EVENT_NOTIFICATION:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300714 return "SET_EVENT_NOTIFICATION";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300715 case CX2341X_ENC_SET_NUM_VSYNC_LINES:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300716 return "SET_NUM_VSYNC_LINES";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300717 case CX2341X_ENC_SET_PLACEHOLDER:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300718 return "SET_PLACEHOLDER";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300719 case CX2341X_ENC_MUTE_VIDEO:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300720 return "MUTE_VIDEO";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300721 case CX2341X_ENC_MUTE_AUDIO:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300722 return "MUTE_AUDIO";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300723 case CX2341X_ENC_MISC:
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300724 return "MISC";
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300725 default:
726 return "UNKNOWN";
727 }
728}
729
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300730static int cx231xx_mbox_func(void *priv, u32 command, int in, int out,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300731 u32 data[CX2341X_MBOX_MAX_DATA])
732{
733 struct cx231xx *dev = priv;
734 unsigned long timeout;
735 u32 value, flag, retval = 0;
736 int i;
737
738 dprintk(3, "%s: command(0x%X) = %s\n", __func__, command,
739 cmd_to_str(command));
740
741 /* this may not be 100% safe if we can't read any memory location
742 without side effects */
743 mc417_memory_read(dev, dev->cx23417_mailbox - 4, &value);
744 if (value != 0x12345678) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300745 dprintk(3, "Firmware and/or mailbox pointer not initialized or corrupted, signature = 0x%x, cmd = %s\n",
746 value, cmd_to_str(command));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300747 return -1;
748 }
749
750 /* This read looks at 32 bits, but flag is only 8 bits.
751 * Seems we also bail if CMD or TIMEOUT bytes are set???
752 */
753 mc417_memory_read(dev, dev->cx23417_mailbox, &flag);
754 if (flag) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300755 dprintk(3, "ERROR: Mailbox appears to be in use (%x), cmd = %s\n",
756 flag, cmd_to_str(command));
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300757 return -1;
758 }
759
760 flag |= 1; /* tell 'em we're working on it */
761 mc417_memory_write(dev, dev->cx23417_mailbox, flag);
762
763 /* write command + args + fill remaining with zeros */
764 /* command code */
765 mc417_memory_write(dev, dev->cx23417_mailbox + 1, command);
766 mc417_memory_write(dev, dev->cx23417_mailbox + 3,
767 IVTV_API_STD_TIMEOUT); /* timeout */
768 for (i = 0; i < in; i++) {
769 mc417_memory_write(dev, dev->cx23417_mailbox + 4 + i, data[i]);
770 dprintk(3, "API Input %d = %d\n", i, data[i]);
771 }
772 for (; i < CX2341X_MBOX_MAX_DATA; i++)
773 mc417_memory_write(dev, dev->cx23417_mailbox + 4 + i, 0);
774
775 flag |= 3; /* tell 'em we're done writing */
776 mc417_memory_write(dev, dev->cx23417_mailbox, flag);
777
778 /* wait for firmware to handle the API command */
779 timeout = jiffies + msecs_to_jiffies(10);
780 for (;;) {
781 mc417_memory_read(dev, dev->cx23417_mailbox, &flag);
782 if (0 != (flag & 4))
783 break;
784 if (time_after(jiffies, timeout)) {
785 dprintk(3, "ERROR: API Mailbox timeout\n");
786 return -1;
787 }
788 udelay(10);
789 }
790
791 /* read output values */
792 for (i = 0; i < out; i++) {
793 mc417_memory_read(dev, dev->cx23417_mailbox + 4 + i, data + i);
794 dprintk(3, "API Output %d = %d\n", i, data[i]);
795 }
796
797 mc417_memory_read(dev, dev->cx23417_mailbox + 2, &retval);
798 dprintk(3, "API result = %d\n", retval);
799
800 flag = 0;
801 mc417_memory_write(dev, dev->cx23417_mailbox, flag);
802
803 return retval;
804}
805
806/* We don't need to call the API often, so using just one
807 * mailbox will probably suffice
808 */
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300809static int cx231xx_api_cmd(struct cx231xx *dev, u32 command,
810 u32 inputcnt, u32 outputcnt, ...)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300811{
812 u32 data[CX2341X_MBOX_MAX_DATA];
813 va_list vargs;
814 int i, err;
815
816 dprintk(3, "%s() cmds = 0x%08x\n", __func__, command);
817
818 va_start(vargs, outputcnt);
819 for (i = 0; i < inputcnt; i++)
820 data[i] = va_arg(vargs, int);
821
822 err = cx231xx_mbox_func(dev, command, inputcnt, outputcnt, data);
823 for (i = 0; i < outputcnt; i++) {
824 int *vptr = va_arg(vargs, int *);
825 *vptr = data[i];
826 }
827 va_end(vargs);
828
829 return err;
830}
831
832static int cx231xx_find_mailbox(struct cx231xx *dev)
833{
834 u32 signature[4] = {
835 0x12345678, 0x34567812, 0x56781234, 0x78123456
836 };
837 int signaturecnt = 0;
838 u32 value;
839 int i;
840 int ret = 0;
841
842 dprintk(2, "%s()\n", __func__);
843
844 for (i = 0; i < 0x100; i++) {/*CX231xx_FIRM_IMAGE_SIZE*/
845 ret = mc417_memory_read(dev, i, &value);
846 if (ret < 0)
847 return ret;
848 if (value == signature[signaturecnt])
849 signaturecnt++;
850 else
851 signaturecnt = 0;
852 if (4 == signaturecnt) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300853 dprintk(1, "Mailbox signature found at 0x%x\n", i + 1);
854 return i + 1;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300855 }
856 }
857 dprintk(3, "Mailbox signature values not found!\n");
858 return -1;
859}
Mauro Carvalho Chehab82c3cca2010-10-07 21:01:31 -0300860
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300861static void mci_write_memory_to_gpio(struct cx231xx *dev, u32 address, u32 value,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300862 u32 *p_fw_image)
863{
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300864 u32 temp = 0;
865 int i = 0;
866
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300867 temp = 0x82 | MCI_MEMORY_DATA_BYTE0 | ((value & 0x000000FF) << 8);
868 temp = temp << 10;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300869 *p_fw_image = temp;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300870 p_fw_image++;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300871 temp = temp | (0x05 << 10);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300872 *p_fw_image = temp;
873 p_fw_image++;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300874
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300875 /*write data byte 1;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300876 temp = 0x82 | MCI_MEMORY_DATA_BYTE1 | (value & 0x0000FF00);
877 temp = temp << 10;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300878 *p_fw_image = temp;
879 p_fw_image++;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300880 temp = temp | (0x05 << 10);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300881 *p_fw_image = temp;
882 p_fw_image++;
883
884 /*write data byte 2;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300885 temp = 0x82 | MCI_MEMORY_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
886 temp = temp << 10;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300887 *p_fw_image = temp;
888 p_fw_image++;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300889 temp = temp | (0x05 << 10);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300890 *p_fw_image = temp;
891 p_fw_image++;
892
893 /*write data byte 3;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300894 temp = 0x82 | MCI_MEMORY_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
895 temp = temp << 10;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300896 *p_fw_image = temp;
897 p_fw_image++;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300898 temp = temp | (0x05 << 10);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300899 *p_fw_image = temp;
900 p_fw_image++;
901
902 /* write address byte 2;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300903 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_WRITE |
904 ((address & 0x003F0000) >> 8);
905 temp = temp << 10;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300906 *p_fw_image = temp;
907 p_fw_image++;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300908 temp = temp | (0x05 << 10);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300909 *p_fw_image = temp;
910 p_fw_image++;
911
912 /* write address byte 1;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300913 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
914 temp = temp << 10;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300915 *p_fw_image = temp;
916 p_fw_image++;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300917 temp = temp | (0x05 << 10);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300918 *p_fw_image = temp;
919 p_fw_image++;
920
921 /* write address byte 0;*/
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300922 temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
923 temp = temp << 10;
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300924 *p_fw_image = temp;
925 p_fw_image++;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300926 temp = temp | (0x05 << 10);
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -0300927 *p_fw_image = temp;
928 p_fw_image++;
929
930 for (i = 0; i < 6; i++) {
931 *p_fw_image = 0xFFFFFFFF;
932 p_fw_image++;
933 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300934}
935
936
937static int cx231xx_load_firmware(struct cx231xx *dev)
938{
939 static const unsigned char magic[8] = {
940 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
941 };
942 const struct firmware *firmware;
943 int i, retval = 0;
944 u32 value = 0;
945 u32 gpio_output = 0;
946 /*u32 checksum = 0;*/
947 /*u32 *dataptr;*/
948 u32 transfer_size = 0;
949 u32 fw_data = 0;
950 u32 address = 0;
951 /*u32 current_fw[800];*/
952 u32 *p_current_fw, *p_fw;
953 u32 *p_fw_data;
954 int frame = 0;
955 u16 _buffer_size = 4096;
956 u8 *p_buffer;
957
Jesper Juhl9a9dcb42010-11-08 20:08:41 -0300958 p_current_fw = vmalloc(1884180 * 4);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300959 p_fw = p_current_fw;
Peter Huewef72cfd82011-01-25 17:38:52 -0300960 if (p_current_fw == NULL) {
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300961 dprintk(2, "FAIL!!!\n");
962 return -1;
963 }
964
Jesper Juhl9a9dcb42010-11-08 20:08:41 -0300965 p_buffer = vmalloc(4096);
Peter Huewef72cfd82011-01-25 17:38:52 -0300966 if (p_buffer == NULL) {
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300967 dprintk(2, "FAIL!!!\n");
968 return -1;
969 }
970
971 dprintk(2, "%s()\n", __func__);
972
973 /* Save GPIO settings before reset of APU */
974 retval |= mc417_memory_read(dev, 0x9020, &gpio_output);
975 retval |= mc417_memory_read(dev, 0x900C, &value);
976
977 retval = mc417_register_write(dev,
978 IVTV_REG_VPU, 0xFFFFFFED);
979 retval |= mc417_register_write(dev,
980 IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
981 retval |= mc417_register_write(dev,
982 IVTV_REG_ENC_SDRAM_REFRESH, 0x80000800);
983 retval |= mc417_register_write(dev,
984 IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
985 retval |= mc417_register_write(dev,
986 IVTV_REG_APU, 0);
987
988 if (retval != 0) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300989 pr_err("%s: Error with mc417_register_write\n", __func__);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300990 return -1;
991 }
992
993 retval = request_firmware(&firmware, CX231xx_FIRM_IMAGE_NAME,
994 &dev->udev->dev);
995
996 if (retval != 0) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300997 pr_err("ERROR: Hotplug firmware request failed (%s).\n",
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300998 CX231xx_FIRM_IMAGE_NAME);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -0300999 pr_err("Please fix your hotplug setup, the board will not work without firmware loaded!\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001000 return -1;
1001 }
1002
1003 if (firmware->size != CX231xx_FIRM_IMAGE_SIZE) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001004 pr_err("ERROR: Firmware size mismatch (have %zd, expected %d)\n",
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001005 firmware->size, CX231xx_FIRM_IMAGE_SIZE);
1006 release_firmware(firmware);
1007 return -1;
1008 }
1009
1010 if (0 != memcmp(firmware->data, magic, 8)) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001011 pr_err("ERROR: Firmware magic mismatch, wrong file?\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001012 release_firmware(firmware);
1013 return -1;
1014 }
1015
1016 initGPIO(dev);
1017
1018 /* transfer to the chip */
1019 dprintk(2, "Loading firmware to GPIO...\n");
1020 p_fw_data = (u32 *)firmware->data;
Mauro Carvalho Chehab62c78c92010-09-25 23:46:08 -03001021 dprintk(2, "firmware->size=%zd\n", firmware->size);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001022 for (transfer_size = 0; transfer_size < firmware->size;
1023 transfer_size += 4) {
1024 fw_data = *p_fw_data;
1025
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001026 mci_write_memory_to_gpio(dev, address, fw_data, p_current_fw);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001027 address = address + 1;
1028 p_current_fw += 20;
1029 p_fw_data += 1;
1030 }
1031
Mauro Carvalho Chehab955e6ed2010-10-07 03:23:25 -03001032 /*download the firmware by ep5-out*/
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001033
1034 for (frame = 0; frame < (int)(CX231xx_FIRM_IMAGE_SIZE*20/_buffer_size);
Mauro Carvalho Chehabbae94dc2010-10-07 03:33:00 -03001035 frame++) {
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001036 for (i = 0; i < _buffer_size; i++) {
Mauro Carvalho Chehabbae94dc2010-10-07 03:33:00 -03001037 *(p_buffer + i) = (u8)(*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x000000FF);
1038 i++;
1039 *(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x0000FF00) >> 8);
1040 i++;
1041 *(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x00FF0000) >> 16);
1042 i++;
1043 *(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0xFF000000) >> 24);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001044 }
1045 cx231xx_ep5_bulkout(dev, p_buffer, _buffer_size);
1046 }
1047
1048 p_current_fw = p_fw;
1049 vfree(p_current_fw);
1050 p_current_fw = NULL;
1051 uninitGPIO(dev);
1052 release_firmware(firmware);
1053 dprintk(1, "Firmware upload successful.\n");
1054
1055 retval |= mc417_register_write(dev, IVTV_REG_HW_BLOCKS,
1056 IVTV_CMD_HW_BLOCKS_RST);
1057 if (retval < 0) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001058 pr_err("%s: Error with mc417_register_write\n",
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001059 __func__);
1060 return retval;
1061 }
1062 /* F/W power up disturbs the GPIOs, restore state */
1063 retval |= mc417_register_write(dev, 0x9020, gpio_output);
1064 retval |= mc417_register_write(dev, 0x900C, value);
1065
1066 retval |= mc417_register_read(dev, IVTV_REG_VPU, &value);
1067 retval |= mc417_register_write(dev, IVTV_REG_VPU, value & 0xFFFFFFE8);
1068
1069 if (retval < 0) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001070 pr_err("%s: Error with mc417_register_write\n",
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001071 __func__);
1072 return retval;
1073 }
1074 return 0;
1075}
1076
Mauro Carvalho Chehab82c3cca2010-10-07 21:01:31 -03001077static void cx231xx_417_check_encoder(struct cx231xx *dev)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001078{
1079 u32 status, seq;
1080
1081 status = 0;
1082 seq = 0;
1083 cx231xx_api_cmd(dev, CX2341X_ENC_GET_SEQ_END, 0, 2, &status, &seq);
1084 dprintk(1, "%s() status = %d, seq = %d\n", __func__, status, seq);
1085}
1086
1087static void cx231xx_codec_settings(struct cx231xx *dev)
1088{
1089 dprintk(1, "%s()\n", __func__);
1090
1091 /* assign frame size */
1092 cx231xx_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
1093 dev->ts1.height, dev->ts1.width);
1094
1095 dev->mpeg_params.width = dev->ts1.width;
1096 dev->mpeg_params.height = dev->ts1.height;
1097
1098 cx2341x_update(dev, cx231xx_mbox_func, NULL, &dev->mpeg_params);
1099
1100 cx231xx_api_cmd(dev, CX2341X_ENC_MISC, 2, 0, 3, 1);
1101 cx231xx_api_cmd(dev, CX2341X_ENC_MISC, 2, 0, 4, 1);
1102}
1103
1104static int cx231xx_initialize_codec(struct cx231xx *dev)
1105{
1106 int version;
1107 int retval;
Hans Verkuilda983502012-05-14 10:14:53 -03001108 u32 i;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001109 u32 val = 0;
1110
1111 dprintk(1, "%s()\n", __func__);
1112 cx231xx_disable656(dev);
1113 retval = cx231xx_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
1114 if (retval < 0) {
1115 dprintk(2, "%s() PING OK\n", __func__);
1116 retval = cx231xx_load_firmware(dev);
1117 if (retval < 0) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001118 pr_err("%s() f/w load failed\n", __func__);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001119 return retval;
1120 }
1121 retval = cx231xx_find_mailbox(dev);
1122 if (retval < 0) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001123 pr_err("%s() mailbox < 0, error\n",
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001124 __func__);
1125 return -1;
1126 }
1127 dev->cx23417_mailbox = retval;
1128 retval = cx231xx_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0);
1129 if (retval < 0) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001130 pr_err("ERROR: cx23417 firmware ping failed!\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001131 return -1;
1132 }
1133 retval = cx231xx_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1,
1134 &version);
1135 if (retval < 0) {
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001136 pr_err("ERROR: cx23417 firmware get encoder: version failed!\n");
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001137 return -1;
1138 }
1139 dprintk(1, "cx23417 firmware version is 0x%08x\n", version);
1140 msleep(200);
1141 }
1142
1143 for (i = 0; i < 1; i++) {
1144 retval = mc417_register_read(dev, 0x20f8, &val);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001145 dprintk(3, "***before enable656() VIM Capture Lines = %d ***\n",
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001146 val);
1147 if (retval < 0)
1148 return retval;
1149 }
1150
1151 cx231xx_enable656(dev);
1152 /* stop mpeg capture */
1153 cx231xx_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE,
1154 3, 0, 1, 3, 4);
1155
1156 cx231xx_codec_settings(dev);
1157 msleep(60);
1158
1159/* cx231xx_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
1160 CX231xx_FIELD1_SAA7115, CX231xx_FIELD2_SAA7115);
1161 cx231xx_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
1162 CX231xx_CUSTOM_EXTENSION_USR_DATA, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1163 0, 0);
1164*/
Hans Verkuilda983502012-05-14 10:14:53 -03001165
1166#if 0
1167 /* TODO */
1168 u32 data[7];
1169
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001170 /* Setup to capture VBI */
1171 data[0] = 0x0001BD00;
1172 data[1] = 1; /* frames per interrupt */
1173 data[2] = 4; /* total bufs */
1174 data[3] = 0x91559155; /* start codes */
1175 data[4] = 0x206080C0; /* stop codes */
1176 data[5] = 6; /* lines */
1177 data[6] = 64; /* BPL */
Hans Verkuilda983502012-05-14 10:14:53 -03001178
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001179 cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_CONFIG, 7, 0, data[0], data[1],
1180 data[2], data[3], data[4], data[5], data[6]);
1181
1182 for (i = 2; i <= 24; i++) {
1183 int valid;
1184
1185 valid = ((i >= 19) && (i <= 21));
1186 cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_LINE, 5, 0, i,
1187 valid, 0 , 0, 0);
1188 cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_LINE, 5, 0,
1189 i | 0x80000000, valid, 0, 0, 0);
1190 }
Hans Verkuilda983502012-05-14 10:14:53 -03001191#endif
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001192/* cx231xx_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, CX231xx_UNMUTE);
1193 msleep(60);
1194*/
1195 /* initialize the video input */
1196 retval = cx231xx_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
1197 if (retval < 0)
1198 return retval;
1199 msleep(60);
1200
1201 /* Enable VIP style pixel invalidation so we work with scaled mode */
1202 mc417_memory_write(dev, 2120, 0x00000080);
1203
1204 /* start capturing to the host interface */
1205 retval = cx231xx_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
1206 CX231xx_MPEG_CAPTURE, CX231xx_RAW_BITS_NONE);
1207 if (retval < 0)
1208 return retval;
1209 msleep(10);
1210
1211 for (i = 0; i < 1; i++) {
1212 mc417_register_read(dev, 0x20f8, &val);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001213 dprintk(3, "***VIM Capture Lines =%d ***\n", val);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001214 }
1215
1216 return 0;
1217}
1218
1219/* ------------------------------------------------------------------ */
1220
1221static int bb_buf_setup(struct videobuf_queue *q,
1222 unsigned int *count, unsigned int *size)
1223{
1224 struct cx231xx_fh *fh = q->priv_data;
1225
1226 fh->dev->ts1.ts_packet_size = mpeglinesize;
1227 fh->dev->ts1.ts_packet_count = mpeglines;
1228
1229 *size = fh->dev->ts1.ts_packet_size * fh->dev->ts1.ts_packet_count;
1230 *count = mpegbufs;
1231
1232 return 0;
1233}
Hans Verkuil5aa95992013-01-29 13:02:15 -03001234
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001235static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
1236{
1237 struct cx231xx_fh *fh = vq->priv_data;
1238 struct cx231xx *dev = fh->dev;
1239 unsigned long flags = 0;
1240
1241 if (in_interrupt())
1242 BUG();
1243
1244 spin_lock_irqsave(&dev->video_mode.slock, flags);
1245 if (dev->USE_ISO) {
1246 if (dev->video_mode.isoc_ctl.buf == buf)
1247 dev->video_mode.isoc_ctl.buf = NULL;
1248 } else {
1249 if (dev->video_mode.bulk_ctl.buf == buf)
1250 dev->video_mode.bulk_ctl.buf = NULL;
1251 }
1252 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
1253 videobuf_waiton(vq, &buf->vb, 0, 0);
1254 videobuf_vmalloc_free(&buf->vb);
1255 buf->vb.state = VIDEOBUF_NEEDS_INIT;
1256}
1257
Mauro Carvalho Chehab82c3cca2010-10-07 21:01:31 -03001258static void buffer_copy(struct cx231xx *dev, char *data, int len, struct urb *urb,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001259 struct cx231xx_dmaqueue *dma_q)
1260{
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001261 void *vbuf;
1262 struct cx231xx_buffer *buf;
1263 u32 tail_data = 0;
1264 char *p_data;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001265
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001266 if (dma_q->mpeg_buffer_done == 0) {
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001267 if (list_empty(&dma_q->active))
1268 return;
1269
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001270 buf = list_entry(dma_q->active.next,
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001271 struct cx231xx_buffer, vb.queue);
1272 dev->video_mode.isoc_ctl.buf = buf;
1273 dma_q->mpeg_buffer_done = 1;
1274 }
1275 /* Fill buffer */
1276 buf = dev->video_mode.isoc_ctl.buf;
1277 vbuf = videobuf_to_vmalloc(&buf->vb);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001278
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001279 if ((dma_q->mpeg_buffer_completed+len) <
1280 mpeglines*mpeglinesize) {
1281 if (dma_q->add_ps_package_head ==
1282 CX231XX_NEED_ADD_PS_PACKAGE_HEAD) {
1283 memcpy(vbuf+dma_q->mpeg_buffer_completed,
1284 dma_q->ps_head, 3);
1285 dma_q->mpeg_buffer_completed =
1286 dma_q->mpeg_buffer_completed + 3;
1287 dma_q->add_ps_package_head =
1288 CX231XX_NONEED_PS_PACKAGE_HEAD;
1289 }
1290 memcpy(vbuf+dma_q->mpeg_buffer_completed, data, len);
1291 dma_q->mpeg_buffer_completed =
1292 dma_q->mpeg_buffer_completed + len;
1293 } else {
1294 dma_q->mpeg_buffer_done = 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001295
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001296 tail_data =
1297 mpeglines*mpeglinesize - dma_q->mpeg_buffer_completed;
1298 memcpy(vbuf+dma_q->mpeg_buffer_completed,
1299 data, tail_data);
1300
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001301 buf->vb.state = VIDEOBUF_DONE;
1302 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -03001303 v4l2_get_timestamp(&buf->vb.ts);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001304 list_del(&buf->vb.queue);
1305 wake_up(&buf->vb.done);
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001306 dma_q->mpeg_buffer_completed = 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001307
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001308 if (len - tail_data > 0) {
1309 p_data = data + tail_data;
1310 dma_q->left_data_count = len - tail_data;
1311 memcpy(dma_q->p_left_data,
1312 p_data, len - tail_data);
1313 }
1314 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001315}
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001316
1317static void buffer_filled(char *data, int len, struct urb *urb,
1318 struct cx231xx_dmaqueue *dma_q)
1319{
1320 void *vbuf;
1321 struct cx231xx_buffer *buf;
1322
1323 if (list_empty(&dma_q->active))
1324 return;
1325
1326 buf = list_entry(dma_q->active.next,
1327 struct cx231xx_buffer, vb.queue);
1328
1329 /* Fill buffer */
1330 vbuf = videobuf_to_vmalloc(&buf->vb);
1331 memcpy(vbuf, data, len);
1332 buf->vb.state = VIDEOBUF_DONE;
1333 buf->vb.field_count++;
1334 v4l2_get_timestamp(&buf->vb.ts);
1335 list_del(&buf->vb.queue);
1336 wake_up(&buf->vb.done);
1337}
1338
1339static int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001340{
1341 struct cx231xx_dmaqueue *dma_q = urb->context;
1342 unsigned char *p_buffer;
1343 u32 buffer_size = 0;
1344 u32 i = 0;
1345
1346 for (i = 0; i < urb->number_of_packets; i++) {
1347 if (dma_q->left_data_count > 0) {
1348 buffer_copy(dev, dma_q->p_left_data,
1349 dma_q->left_data_count, urb, dma_q);
1350 dma_q->mpeg_buffer_completed = dma_q->left_data_count;
1351 dma_q->left_data_count = 0;
1352 }
1353
1354 p_buffer = urb->transfer_buffer +
1355 urb->iso_frame_desc[i].offset;
1356 buffer_size = urb->iso_frame_desc[i].actual_length;
1357
1358 if (buffer_size > 0)
1359 buffer_copy(dev, p_buffer, buffer_size, urb, dma_q);
1360 }
1361
1362 return 0;
1363}
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001364
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001365static int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
1366{
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001367 struct cx231xx_dmaqueue *dma_q = urb->context;
1368 unsigned char *p_buffer, *buffer;
1369 u32 buffer_size = 0;
1370
1371 p_buffer = urb->transfer_buffer;
1372 buffer_size = urb->actual_length;
1373
1374 buffer = kmalloc(buffer_size, GFP_ATOMIC);
1375
1376 memcpy(buffer, dma_q->ps_head, 3);
1377 memcpy(buffer+3, p_buffer, buffer_size-3);
1378 memcpy(dma_q->ps_head, p_buffer+buffer_size-3, 3);
1379
1380 p_buffer = buffer;
1381 buffer_filled(p_buffer, buffer_size, urb, dma_q);
1382
1383 kfree(buffer);
1384 return 0;
1385}
1386
1387static int bb_buf_prepare(struct videobuf_queue *q,
1388 struct videobuf_buffer *vb, enum v4l2_field field)
1389{
1390 struct cx231xx_fh *fh = q->priv_data;
1391 struct cx231xx_buffer *buf =
1392 container_of(vb, struct cx231xx_buffer, vb);
1393 struct cx231xx *dev = fh->dev;
1394 int rc = 0, urb_init = 0;
1395 int size = fh->dev->ts1.ts_packet_size * fh->dev->ts1.ts_packet_count;
1396
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001397 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1398 return -EINVAL;
1399 buf->vb.width = fh->dev->ts1.ts_packet_size;
1400 buf->vb.height = fh->dev->ts1.ts_packet_count;
1401 buf->vb.size = size;
1402 buf->vb.field = field;
1403
1404 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1405 rc = videobuf_iolock(q, &buf->vb, NULL);
1406 if (rc < 0)
1407 goto fail;
1408 }
1409
1410 if (dev->USE_ISO) {
1411 if (!dev->video_mode.isoc_ctl.num_bufs)
1412 urb_init = 1;
1413 } else {
1414 if (!dev->video_mode.bulk_ctl.num_bufs)
1415 urb_init = 1;
1416 }
1417 /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
1418 urb_init, dev->video_mode.max_pkt_size);*/
1419 dev->mode_tv = 1;
1420
1421 if (urb_init) {
1422 rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
1423 rc = cx231xx_unmute_audio(dev);
1424 if (dev->USE_ISO) {
1425 cx231xx_set_alt_setting(dev, INDEX_TS1, 4);
1426 rc = cx231xx_init_isoc(dev, mpeglines,
1427 mpegbufs,
1428 dev->ts1_mode.max_pkt_size,
1429 cx231xx_isoc_copy);
1430 } else {
1431 cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
1432 rc = cx231xx_init_bulk(dev, mpeglines,
1433 mpegbufs,
1434 dev->ts1_mode.max_pkt_size,
1435 cx231xx_bulk_copy);
1436 }
1437 if (rc < 0)
1438 goto fail;
1439 }
1440
1441 buf->vb.state = VIDEOBUF_PREPARED;
1442 return 0;
1443
1444fail:
1445 free_buffer(q, buf);
1446 return rc;
1447}
1448
1449static void bb_buf_queue(struct videobuf_queue *q,
1450 struct videobuf_buffer *vb)
1451{
1452 struct cx231xx_fh *fh = q->priv_data;
1453
1454 struct cx231xx_buffer *buf =
1455 container_of(vb, struct cx231xx_buffer, vb);
1456 struct cx231xx *dev = fh->dev;
1457 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
1458
1459 buf->vb.state = VIDEOBUF_QUEUED;
1460 list_add_tail(&buf->vb.queue, &vidq->active);
1461
1462}
1463
1464static void bb_buf_release(struct videobuf_queue *q,
1465 struct videobuf_buffer *vb)
1466{
1467 struct cx231xx_buffer *buf =
1468 container_of(vb, struct cx231xx_buffer, vb);
1469 /*struct cx231xx_fh *fh = q->priv_data;*/
1470 /*struct cx231xx *dev = (struct cx231xx *)fh->dev;*/
1471
1472 free_buffer(q, buf);
1473}
1474
1475static struct videobuf_queue_ops cx231xx_qops = {
1476 .buf_setup = bb_buf_setup,
1477 .buf_prepare = bb_buf_prepare,
1478 .buf_queue = bb_buf_queue,
1479 .buf_release = bb_buf_release,
1480};
1481
1482/* ------------------------------------------------------------------ */
1483
1484static const u32 *ctrl_classes[] = {
1485 cx2341x_mpeg_ctrls,
1486 NULL
1487};
1488
1489static int cx231xx_queryctrl(struct cx231xx *dev,
1490 struct v4l2_queryctrl *qctrl)
1491{
1492 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1493 if (qctrl->id == 0)
1494 return -EINVAL;
1495
1496 /* MPEG V4L2 controls */
1497 if (cx2341x_ctrl_query(&dev->mpeg_params, qctrl))
1498 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1499
1500 return 0;
1501}
1502
1503static int cx231xx_querymenu(struct cx231xx *dev,
1504 struct v4l2_querymenu *qmenu)
1505{
1506 struct v4l2_queryctrl qctrl;
1507
1508 qctrl.id = qmenu->id;
1509 cx231xx_queryctrl(dev, &qctrl);
1510 return v4l2_ctrl_query_menu(qmenu, &qctrl,
1511 cx2341x_ctrl_get_menu(&dev->mpeg_params, qmenu->id));
1512}
1513
1514static int vidioc_g_std(struct file *file, void *fh0, v4l2_std_id *norm)
1515{
1516 struct cx231xx_fh *fh = file->private_data;
1517 struct cx231xx *dev = fh->dev;
1518
1519 *norm = dev->encodernorm.id;
1520 return 0;
1521}
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001522
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001523static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
1524{
1525 struct cx231xx_fh *fh = file->private_data;
1526 struct cx231xx *dev = fh->dev;
1527 unsigned int i;
1528
1529 for (i = 0; i < ARRAY_SIZE(cx231xx_tvnorms); i++)
1530 if (*id & cx231xx_tvnorms[i].id)
1531 break;
1532 if (i == ARRAY_SIZE(cx231xx_tvnorms))
1533 return -EINVAL;
1534 dev->encodernorm = cx231xx_tvnorms[i];
1535
1536 if (dev->encodernorm.id & 0xb000) {
1537 dprintk(3, "encodernorm set to NTSC\n");
1538 dev->norm = V4L2_STD_NTSC;
1539 dev->ts1.height = 480;
1540 dev->mpeg_params.is_50hz = 0;
1541 } else {
1542 dprintk(3, "encodernorm set to PAL\n");
1543 dev->norm = V4L2_STD_PAL_B;
1544 dev->ts1.height = 576;
1545 dev->mpeg_params.is_50hz = 1;
1546 }
1547 call_all(dev, core, s_std, dev->norm);
1548 /* do mode control overrides */
1549 cx231xx_do_mode_ctrl_overrides(dev);
1550
1551 dprintk(3, "exit vidioc_s_std() i=0x%x\n", i);
1552 return 0;
1553}
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001554
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001555static int vidioc_s_ctrl(struct file *file, void *priv,
1556 struct v4l2_control *ctl)
1557{
1558 struct cx231xx_fh *fh = file->private_data;
1559 struct cx231xx *dev = fh->dev;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001560
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001561 dprintk(3, "enter vidioc_s_ctrl()\n");
1562 /* Update the A/V core */
1563 call_all(dev, core, s_ctrl, ctl);
1564 dprintk(3, "exit vidioc_s_ctrl()\n");
1565 return 0;
1566}
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001567
1568static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1569 struct v4l2_fmtdesc *f)
1570{
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001571 if (f->index != 0)
1572 return -EINVAL;
1573
1574 strlcpy(f->description, "MPEG", sizeof(f->description));
1575 f->pixelformat = V4L2_PIX_FMT_MPEG;
1576
1577 return 0;
1578}
1579
1580static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1581 struct v4l2_format *f)
1582{
1583 struct cx231xx_fh *fh = file->private_data;
1584 struct cx231xx *dev = fh->dev;
Hans Verkuil5aa95992013-01-29 13:02:15 -03001585
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001586 dprintk(3, "enter vidioc_g_fmt_vid_cap()\n");
Hans Verkuil5aa95992013-01-29 13:02:15 -03001587 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001588 f->fmt.pix.bytesperline = 0;
Hans Verkuil5aa95992013-01-29 13:02:15 -03001589 f->fmt.pix.sizeimage = mpeglines * mpeglinesize;
1590 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1591 f->fmt.pix.width = dev->ts1.width;
1592 f->fmt.pix.height = dev->ts1.height;
1593 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1594 f->fmt.pix.priv = 0;
1595 dprintk(1, "VIDIOC_G_FMT: w: %d, h: %d\n",
1596 dev->ts1.width, dev->ts1.height);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001597 dprintk(3, "exit vidioc_g_fmt_vid_cap()\n");
1598 return 0;
1599}
1600
1601static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1602 struct v4l2_format *f)
1603{
1604 struct cx231xx_fh *fh = file->private_data;
1605 struct cx231xx *dev = fh->dev;
Hans Verkuil5aa95992013-01-29 13:02:15 -03001606
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001607 dprintk(3, "enter vidioc_try_fmt_vid_cap()\n");
Hans Verkuil5aa95992013-01-29 13:02:15 -03001608 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001609 f->fmt.pix.bytesperline = 0;
Hans Verkuil5aa95992013-01-29 13:02:15 -03001610 f->fmt.pix.sizeimage = mpeglines * mpeglinesize;
1611 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1612 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1613 f->fmt.pix.priv = 0;
1614 dprintk(1, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
1615 dev->ts1.width, dev->ts1.height);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001616 dprintk(3, "exit vidioc_try_fmt_vid_cap()\n");
1617 return 0;
1618}
1619
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001620static int vidioc_reqbufs(struct file *file, void *priv,
1621 struct v4l2_requestbuffers *p)
1622{
1623 struct cx231xx_fh *fh = file->private_data;
1624
1625 return videobuf_reqbufs(&fh->vidq, p);
1626}
1627
1628static int vidioc_querybuf(struct file *file, void *priv,
1629 struct v4l2_buffer *p)
1630{
1631 struct cx231xx_fh *fh = file->private_data;
1632
1633 return videobuf_querybuf(&fh->vidq, p);
1634}
1635
1636static int vidioc_qbuf(struct file *file, void *priv,
1637 struct v4l2_buffer *p)
1638{
1639 struct cx231xx_fh *fh = file->private_data;
1640
1641 return videobuf_qbuf(&fh->vidq, p);
1642}
1643
1644static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1645{
1646 struct cx231xx_fh *fh = priv;
1647
1648 return videobuf_dqbuf(&fh->vidq, b, file->f_flags & O_NONBLOCK);
1649}
1650
1651
1652static int vidioc_streamon(struct file *file, void *priv,
1653 enum v4l2_buf_type i)
1654{
1655 struct cx231xx_fh *fh = file->private_data;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001656 struct cx231xx *dev = fh->dev;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001657
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001658 dprintk(3, "enter vidioc_streamon()\n");
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001659 cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
1660 cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
1661 if (dev->USE_ISO)
1662 cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
1663 CX231XX_NUM_BUFS,
1664 dev->video_mode.max_pkt_size,
1665 cx231xx_isoc_copy);
1666 else {
1667 cx231xx_init_bulk(dev, 320,
1668 5,
1669 dev->ts1_mode.max_pkt_size,
1670 cx231xx_bulk_copy);
1671 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001672 dprintk(3, "exit vidioc_streamon()\n");
1673 return videobuf_streamon(&fh->vidq);
1674}
1675
1676static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1677{
1678 struct cx231xx_fh *fh = file->private_data;
1679
1680 return videobuf_streamoff(&fh->vidq);
1681}
1682
1683static int vidioc_g_ext_ctrls(struct file *file, void *priv,
1684 struct v4l2_ext_controls *f)
1685{
1686 struct cx231xx_fh *fh = priv;
1687 struct cx231xx *dev = fh->dev;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001688
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001689 dprintk(3, "enter vidioc_g_ext_ctrls()\n");
1690 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
1691 return -EINVAL;
1692 dprintk(3, "exit vidioc_g_ext_ctrls()\n");
1693 return cx2341x_ext_ctrls(&dev->mpeg_params, 0, f, VIDIOC_G_EXT_CTRLS);
1694}
1695
1696static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1697 struct v4l2_ext_controls *f)
1698{
1699 struct cx231xx_fh *fh = priv;
1700 struct cx231xx *dev = fh->dev;
1701 struct cx2341x_mpeg_params p;
1702 int err;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001703
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001704 dprintk(3, "enter vidioc_s_ext_ctrls()\n");
1705 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
1706 return -EINVAL;
1707
1708 p = dev->mpeg_params;
1709 err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_TRY_EXT_CTRLS);
1710 if (err == 0) {
1711 err = cx2341x_update(dev, cx231xx_mbox_func,
1712 &dev->mpeg_params, &p);
1713 dev->mpeg_params = p;
1714 }
1715
1716 return err;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001717}
1718
1719static int vidioc_try_ext_ctrls(struct file *file, void *priv,
1720 struct v4l2_ext_controls *f)
1721{
1722 struct cx231xx_fh *fh = priv;
1723 struct cx231xx *dev = fh->dev;
1724 struct cx2341x_mpeg_params p;
1725 int err;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001726
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001727 dprintk(3, "enter vidioc_try_ext_ctrls()\n");
1728 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
1729 return -EINVAL;
1730
1731 p = dev->mpeg_params;
1732 err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_TRY_EXT_CTRLS);
1733 dprintk(3, "exit vidioc_try_ext_ctrls() err=%d\n", err);
1734 return err;
1735}
1736
1737static int vidioc_log_status(struct file *file, void *priv)
1738{
1739 struct cx231xx_fh *fh = priv;
1740 struct cx231xx *dev = fh->dev;
1741 char name[32 + 2];
1742
1743 snprintf(name, sizeof(name), "%s/2", dev->name);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001744 call_all(dev, core, log_status);
1745 cx2341x_log_status(&dev->mpeg_params, name);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001746 return 0;
1747}
1748
1749static int vidioc_querymenu(struct file *file, void *priv,
1750 struct v4l2_querymenu *a)
1751{
1752 struct cx231xx_fh *fh = priv;
1753 struct cx231xx *dev = fh->dev;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001754
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001755 dprintk(3, "enter vidioc_querymenu()\n");
1756 dprintk(3, "exit vidioc_querymenu()\n");
1757 return cx231xx_querymenu(dev, a);
1758}
1759
1760static int vidioc_queryctrl(struct file *file, void *priv,
1761 struct v4l2_queryctrl *c)
1762{
1763 struct cx231xx_fh *fh = priv;
1764 struct cx231xx *dev = fh->dev;
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001765
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001766 dprintk(3, "enter vidioc_queryctrl()\n");
1767 dprintk(3, "exit vidioc_queryctrl()\n");
1768 return cx231xx_queryctrl(dev, c);
1769}
1770
1771static int mpeg_open(struct file *file)
1772{
Hans Verkuilb86d1542013-01-29 13:16:06 -03001773 struct video_device *vdev = video_devdata(file);
1774 struct cx231xx *dev = video_drvdata(file);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001775 struct cx231xx_fh *fh;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001776
1777 dprintk(2, "%s()\n", __func__);
1778
Hans Verkuil1265f082012-09-17 09:26:46 -03001779 if (mutex_lock_interruptible(&dev->lock))
1780 return -ERESTARTSYS;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001781
1782 /* allocate + initialize per filehandle data */
1783 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1784 if (NULL == fh) {
1785 mutex_unlock(&dev->lock);
1786 return -ENOMEM;
1787 }
1788
1789 file->private_data = fh;
Hans Verkuilb86d1542013-01-29 13:16:06 -03001790 v4l2_fh_init(&fh->fh, vdev);
1791 fh->dev = dev;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001792
1793
1794 videobuf_queue_vmalloc_init(&fh->vidq, &cx231xx_qops,
1795 NULL, &dev->video_mode.slock,
1796 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_INTERLACED,
Hans Verkuil1265f082012-09-17 09:26:46 -03001797 sizeof(struct cx231xx_buffer), fh, &dev->lock);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001798/*
1799 videobuf_queue_sg_init(&fh->vidq, &cx231xx_qops,
1800 &dev->udev->dev, &dev->ts1.slock,
1801 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1802 V4L2_FIELD_INTERLACED,
1803 sizeof(struct cx231xx_buffer),
Hans Verkuil1265f082012-09-17 09:26:46 -03001804 fh, &dev->lock);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001805*/
1806
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001807 cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
1808 cx231xx_set_gpio_value(dev, 2, 0);
1809
1810 cx231xx_initialize_codec(dev);
1811
1812 mutex_unlock(&dev->lock);
Hans Verkuilb86d1542013-01-29 13:16:06 -03001813 v4l2_fh_add(&fh->fh);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001814 cx231xx_start_TS1(dev);
1815
1816 return 0;
1817}
1818
1819static int mpeg_release(struct file *file)
1820{
1821 struct cx231xx_fh *fh = file->private_data;
1822 struct cx231xx *dev = fh->dev;
1823
Devin Heitmuellerdd067a82010-07-07 19:28:23 -03001824 dprintk(3, "mpeg_release()! dev=0x%p\n", dev);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001825
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001826 mutex_lock(&dev->lock);
1827
1828 cx231xx_stop_TS1(dev);
1829
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001830 /* do this before setting alternate! */
1831 if (dev->USE_ISO)
1832 cx231xx_uninit_isoc(dev);
1833 else
1834 cx231xx_uninit_bulk(dev);
1835 cx231xx_set_mode(dev, CX231XX_SUSPEND);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001836
Hans Verkuil5b8acdc2013-01-29 12:59:50 -03001837 cx231xx_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
1838 CX231xx_END_NOW, CX231xx_MPEG_CAPTURE,
1839 CX231xx_RAW_BITS_NONE);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001840
1841 /* FIXME: Review this crap */
1842 /* Shut device down on last close */
1843 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1844 if (atomic_dec_return(&dev->v4l_reader_count) == 0) {
1845 /* stop mpeg capture */
1846
1847 msleep(500);
1848 cx231xx_417_check_encoder(dev);
1849
1850 }
1851 }
1852
1853 if (fh->vidq.streaming)
1854 videobuf_streamoff(&fh->vidq);
1855 if (fh->vidq.reading)
1856 videobuf_read_stop(&fh->vidq);
1857
1858 videobuf_mmap_free(&fh->vidq);
Hans Verkuilb86d1542013-01-29 13:16:06 -03001859 v4l2_fh_del(&fh->fh);
1860 v4l2_fh_exit(&fh->fh);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001861 kfree(fh);
1862 mutex_unlock(&dev->lock);
1863 return 0;
1864}
1865
1866static ssize_t mpeg_read(struct file *file, char __user *data,
1867 size_t count, loff_t *ppos)
1868{
1869 struct cx231xx_fh *fh = file->private_data;
1870 struct cx231xx *dev = fh->dev;
1871
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001872 /* Deal w/ A/V decoder * and mpeg encoder sync issues. */
1873 /* Start mpeg encoder on first read. */
1874 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1875 if (atomic_inc_return(&dev->v4l_reader_count) == 1) {
1876 if (cx231xx_initialize_codec(dev) < 0)
1877 return -EINVAL;
1878 }
1879 }
1880
1881 return videobuf_read_stream(&fh->vidq, data, count, ppos, 0,
1882 file->f_flags & O_NONBLOCK);
1883}
1884
1885static unsigned int mpeg_poll(struct file *file,
1886 struct poll_table_struct *wait)
1887{
1888 struct cx231xx_fh *fh = file->private_data;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001889
1890 return videobuf_poll_stream(file, &fh->vidq, wait);
1891}
1892
1893static int mpeg_mmap(struct file *file, struct vm_area_struct *vma)
1894{
1895 struct cx231xx_fh *fh = file->private_data;
1896 struct cx231xx *dev = fh->dev;
1897
1898 dprintk(2, "%s()\n", __func__);
1899
1900 return videobuf_mmap_mapper(&fh->vidq, vma);
1901}
1902
1903static struct v4l2_file_operations mpeg_fops = {
1904 .owner = THIS_MODULE,
1905 .open = mpeg_open,
1906 .release = mpeg_release,
1907 .read = mpeg_read,
1908 .poll = mpeg_poll,
1909 .mmap = mpeg_mmap,
Hans Verkuil1265f082012-09-17 09:26:46 -03001910 .unlocked_ioctl = video_ioctl2,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001911};
1912
1913static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1914 .vidioc_s_std = vidioc_s_std,
1915 .vidioc_g_std = vidioc_g_std,
Hans Verkuilb86d1542013-01-29 13:16:06 -03001916 .vidioc_g_tuner = cx231xx_g_tuner,
1917 .vidioc_s_tuner = cx231xx_s_tuner,
1918 .vidioc_g_frequency = cx231xx_g_frequency,
1919 .vidioc_s_frequency = cx231xx_s_frequency,
1920 .vidioc_enum_input = cx231xx_enum_input,
1921 .vidioc_g_input = cx231xx_g_input,
1922 .vidioc_s_input = cx231xx_s_input,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001923 .vidioc_s_ctrl = vidioc_s_ctrl,
Hans Verkuilbc087342013-01-29 12:52:33 -03001924 .vidioc_querycap = cx231xx_querycap,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001925 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1926 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1927 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
Hans Verkuil3f926e32013-01-29 12:50:18 -03001928 .vidioc_s_fmt_vid_cap = vidioc_try_fmt_vid_cap,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001929 .vidioc_reqbufs = vidioc_reqbufs,
1930 .vidioc_querybuf = vidioc_querybuf,
1931 .vidioc_qbuf = vidioc_qbuf,
1932 .vidioc_dqbuf = vidioc_dqbuf,
1933 .vidioc_streamon = vidioc_streamon,
1934 .vidioc_streamoff = vidioc_streamoff,
1935 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1936 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1937 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1938 .vidioc_log_status = vidioc_log_status,
1939 .vidioc_querymenu = vidioc_querymenu,
1940 .vidioc_queryctrl = vidioc_queryctrl,
Hans Verkuilb86d1542013-01-29 13:16:06 -03001941 .vidioc_g_chip_ident = cx231xx_g_chip_ident,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001942#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilb86d1542013-01-29 13:16:06 -03001943 .vidioc_g_register = cx231xx_g_register,
1944 .vidioc_s_register = cx231xx_s_register,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001945#endif
1946};
1947
1948static struct video_device cx231xx_mpeg_template = {
1949 .name = "cx231xx",
1950 .fops = &mpeg_fops,
1951 .ioctl_ops = &mpeg_ioctl_ops,
1952 .minor = -1,
1953 .tvnorms = CX231xx_NORMS,
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001954};
1955
1956void cx231xx_417_unregister(struct cx231xx *dev)
1957{
1958 dprintk(1, "%s()\n", __func__);
1959 dprintk(3, "%s()\n", __func__);
1960
1961 if (dev->v4l_device) {
1962 if (-1 != dev->v4l_device->minor)
1963 video_unregister_device(dev->v4l_device);
1964 else
1965 video_device_release(dev->v4l_device);
1966 dev->v4l_device = NULL;
1967 }
1968}
1969
1970static struct video_device *cx231xx_video_dev_alloc(
1971 struct cx231xx *dev,
1972 struct usb_device *usbdev,
1973 struct video_device *template,
1974 char *type)
1975{
1976 struct video_device *vfd;
1977
1978 dprintk(1, "%s()\n", __func__);
1979 vfd = video_device_alloc();
1980 if (NULL == vfd)
1981 return NULL;
1982 *vfd = *template;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001983 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1984 type, cx231xx_boards[dev->model].name);
1985
1986 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil1265f082012-09-17 09:26:46 -03001987 vfd->lock = &dev->lock;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001988 vfd->release = video_device_release;
Hans Verkuilb86d1542013-01-29 13:16:06 -03001989 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1990 video_set_drvdata(vfd, dev);
1991 if (dev->tuner_type == TUNER_ABSENT) {
1992 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
1993 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
1994 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
1995 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
1996 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -03001997
1998 return vfd;
1999
2000}
2001
2002int cx231xx_417_register(struct cx231xx *dev)
2003{
2004 /* FIXME: Port1 hardcoded here */
2005 int err = -ENODEV;
2006 struct cx231xx_tsport *tsport = &dev->ts1;
2007
2008 dprintk(1, "%s()\n", __func__);
2009
2010 /* Set default TV standard */
2011 dev->encodernorm = cx231xx_tvnorms[0];
2012
2013 if (dev->encodernorm.id & V4L2_STD_525_60)
2014 tsport->height = 480;
2015 else
2016 tsport->height = 576;
2017
2018 tsport->width = 720;
2019 cx2341x_fill_defaults(&dev->mpeg_params);
2020 dev->norm = V4L2_STD_NTSC;
2021
2022 dev->mpeg_params.port = CX2341X_PORT_SERIAL;
2023
2024 /* Allocate and initialize V4L video device */
2025 dev->v4l_device = cx231xx_video_dev_alloc(dev,
2026 dev->udev, &cx231xx_mpeg_template, "mpeg");
2027 err = video_register_device(dev->v4l_device,
2028 VFL_TYPE_GRABBER, -1);
2029 if (err < 0) {
2030 dprintk(3, "%s: can't register mpeg device\n", dev->name);
2031 return err;
2032 }
2033
2034 dprintk(3, "%s: registered device video%d [mpeg]\n",
2035 dev->name, dev->v4l_device->num);
2036
2037 return 0;
2038}
Tim Gardnerb8320e92012-07-26 14:34:22 -03002039
2040MODULE_FIRMWARE(CX231xx_FIRM_IMAGE_NAME);