blob: 28499e591925e681740c37c6bebb4105b27185a4 [file] [log] [blame]
Hans Verkuile281db582008-08-08 12:43:59 -03001 /*
2 saa6752hs - i2c-driver for the saa6752hs by Philips
3
4 Copyright (C) 2004 Andrew de Quincey
5
6 AC-3 support:
7
8 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License vs published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mvss Ave, Cambridge, MA 02139, USA.
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/string.h>
28#include <linux/timer.h>
29#include <linux/delay.h>
30#include <linux/errno.h>
31#include <linux/slab.h>
32#include <linux/poll.h>
33#include <linux/i2c.h>
34#include <linux/types.h>
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -020035#include <linux/videodev2.h>
36#include <media/v4l2-common.h>
Hans Verkuile281db582008-08-08 12:43:59 -030037#include <media/v4l2-chip-ident.h>
38#include <media/v4l2-i2c-drv-legacy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
40#include <linux/crc32.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
43#define MPEG_VIDEO_MAX_BITRATE_MAX 27000
44#define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
45#define MPEG_PID_MAX ((1 << 14) - 1)
46
47/* Addresses to scan */
48static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
Hans Verkuilf87086e2008-07-18 00:50:58 -030049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050I2C_CLIENT_INSMOD;
51
52MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
53MODULE_AUTHOR("Andrew de Quincey");
54MODULE_LICENSE("GPL");
55
Frederic CAND0a4c9c92005-05-05 16:15:52 -070056enum saa6752hs_videoformat {
57 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
58 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
59 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
60 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
61 SAA6752HS_VF_UNKNOWN,
62};
63
Hans Verkuil86b79d62006-06-18 16:40:10 -030064struct saa6752hs_mpeg_params {
65 /* transport streams */
66 __u16 ts_pid_pmt;
67 __u16 ts_pid_audio;
68 __u16 ts_pid_video;
69 __u16 ts_pid_pcr;
70
71 /* audio */
Hans Verkuile281db582008-08-08 12:43:59 -030072 enum v4l2_mpeg_audio_encoding au_encoding;
73 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
74 enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
Hans Verkuil86b79d62006-06-18 16:40:10 -030075
76 /* video */
77 enum v4l2_mpeg_video_aspect vi_aspect;
78 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
79 __u32 vi_bitrate;
80 __u32 vi_bitrate_peak;
81};
82
Frederic CAND0a4c9c92005-05-05 16:15:52 -070083static const struct v4l2_format v4l2_format_table[] =
84{
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -070085 [SAA6752HS_VF_D1] =
86 { .fmt = { .pix = { .width = 720, .height = 576 }}},
87 [SAA6752HS_VF_2_3_D1] =
88 { .fmt = { .pix = { .width = 480, .height = 576 }}},
89 [SAA6752HS_VF_1_2_D1] =
90 { .fmt = { .pix = { .width = 352, .height = 576 }}},
91 [SAA6752HS_VF_SIF] =
92 { .fmt = { .pix = { .width = 352, .height = 288 }}},
93 [SAA6752HS_VF_UNKNOWN] =
94 { .fmt = { .pix = { .width = 0, .height = 0}}},
Frederic CAND0a4c9c92005-05-05 16:15:52 -070095};
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097struct saa6752hs_state {
Hans Verkuile281db582008-08-08 12:43:59 -030098 int chip;
99 u32 revision;
100 int has_ac3;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300101 struct saa6752hs_mpeg_params params;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700102 enum saa6752hs_videoformat video_format;
Robert W. Boone9b715212005-11-08 21:36:45 -0800103 v4l2_std_id standard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106enum saa6752hs_command {
107 SAA6752HS_COMMAND_RESET = 0,
Trent Piepho657de3c2006-06-20 00:30:57 -0300108 SAA6752HS_COMMAND_STOP = 1,
109 SAA6752HS_COMMAND_START = 2,
110 SAA6752HS_COMMAND_PAUSE = 3,
111 SAA6752HS_COMMAND_RECONFIGURE = 4,
112 SAA6752HS_COMMAND_SLEEP = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
114
115 SAA6752HS_COMMAND_MAX
116};
117
118/* ---------------------------------------------------------------------- */
119
120static u8 PAT[] = {
Robert W. Boone9b715212005-11-08 21:36:45 -0800121 0xc2, /* i2c register */
122 0x00, /* table number for encoder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Robert W. Boone9b715212005-11-08 21:36:45 -0800124 0x47, /* sync */
125 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
126 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Robert W. Boone9b715212005-11-08 21:36:45 -0800128 0x00, /* PSI pointer to start of table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Robert W. Boone9b715212005-11-08 21:36:45 -0800130 0x00, /* tid(0) */
131 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Robert W. Boone9b715212005-11-08 21:36:45 -0800133 0x00, 0x01, /* transport_stream_id(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Robert W. Boone9b715212005-11-08 21:36:45 -0800135 0xc1, /* version_number(0), current_next_indicator(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Robert W. Boone9b715212005-11-08 21:36:45 -0800137 0x00, 0x00, /* section_number(0), last_section_number(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Robert W. Boone9b715212005-11-08 21:36:45 -0800139 0x00, 0x01, /* program_number(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Robert W. Boone9b715212005-11-08 21:36:45 -0800141 0xe0, 0x00, /* PMT PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Robert W. Boone9b715212005-11-08 21:36:45 -0800143 0x00, 0x00, 0x00, 0x00 /* CRC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146static u8 PMT[] = {
Robert W. Boone9b715212005-11-08 21:36:45 -0800147 0xc2, /* i2c register */
148 0x01, /* table number for encoder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Robert W. Boone9b715212005-11-08 21:36:45 -0800150 0x47, /* sync */
151 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
152 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Robert W. Boone9b715212005-11-08 21:36:45 -0800154 0x00, /* PSI pointer to start of table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Robert W. Boone9b715212005-11-08 21:36:45 -0800156 0x02, /* tid(2) */
157 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Robert W. Boone9b715212005-11-08 21:36:45 -0800159 0x00, 0x01, /* program_number(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Robert W. Boone9b715212005-11-08 21:36:45 -0800161 0xc1, /* version_number(0), current_next_indicator(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Robert W. Boone9b715212005-11-08 21:36:45 -0800163 0x00, 0x00, /* section_number(0), last_section_number(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Robert W. Boone9b715212005-11-08 21:36:45 -0800165 0xe0, 0x00, /* PCR_PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Robert W. Boone9b715212005-11-08 21:36:45 -0800167 0xf0, 0x00, /* program_info_length(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Robert W. Boone9b715212005-11-08 21:36:45 -0800169 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
170 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Robert W. Boone9b715212005-11-08 21:36:45 -0800172 0x00, 0x00, 0x00, 0x00 /* CRC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173};
174
Hans Verkuil3eea5432008-08-08 13:27:16 -0300175static u8 PMT_AC3[] = {
176 0xc2, /* i2c register */
177 0x01, /* table number for encoder(1) */
178 0x47, /* sync */
179
180 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
181 0x10, /* PMT PID (0x0010) */
182 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
183
184 0x00, /* PSI pointer to start of table */
185
186 0x02, /* TID (2) */
187 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
188
189 0x00, 0x01, /* program_number(1) */
190
191 0xc1, /* version_number(0), current_next_indicator(1) */
192
193 0x00, 0x00, /* section_number(0), last_section_number(0) */
194
195 0xe1, 0x04, /* PCR_PID (0x0104) */
196
197 0xf0, 0x00, /* program_info_length(0) */
198
199 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
200 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
201 0x6a, /* AC3 */
202 0x01, /* Descriptor_length(1) */
203 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
204
205 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
206};
207
Hans Verkuil86b79d62006-06-18 16:40:10 -0300208static struct saa6752hs_mpeg_params param_defaults =
209{
210 .ts_pid_pmt = 16,
211 .ts_pid_video = 260,
212 .ts_pid_audio = 256,
213 .ts_pid_pcr = 259,
214
215 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
216 .vi_bitrate = 4000,
217 .vi_bitrate_peak = 6000,
218 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
219
Hans Verkuile281db582008-08-08 12:43:59 -0300220 .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
Hans Verkuil86b79d62006-06-18 16:40:10 -0300221 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
Hans Verkuil3eea5432008-08-08 13:27:16 -0300222 .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
Hans Verkuil86b79d62006-06-18 16:40:10 -0300223};
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/* ---------------------------------------------------------------------- */
226
227static int saa6752hs_chip_command(struct i2c_client* client,
228 enum saa6752hs_command command)
229{
230 unsigned char buf[3];
231 unsigned long timeout;
232 int status = 0;
233
Robert W. Boone9b715212005-11-08 21:36:45 -0800234 /* execute the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 switch(command) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800236 case SAA6752HS_COMMAND_RESET:
237 buf[0] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
239
240 case SAA6752HS_COMMAND_STOP:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800241 buf[0] = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 break;
243
244 case SAA6752HS_COMMAND_START:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800245 buf[0] = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
247
248 case SAA6752HS_COMMAND_PAUSE:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800249 buf[0] = 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251
252 case SAA6752HS_COMMAND_RECONFIGURE:
253 buf[0] = 0x05;
254 break;
255
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800256 case SAA6752HS_COMMAND_SLEEP:
257 buf[0] = 0x06;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
259
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800260 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 buf[0] = 0x07;
262 break;
263
264 default:
265 return -EINVAL;
266 }
267
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800268 /* set it and wait for it to be so */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 i2c_master_send(client, buf, 1);
270 timeout = jiffies + HZ * 3;
271 for (;;) {
Robert W. Boone9b715212005-11-08 21:36:45 -0800272 /* get the current status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 buf[0] = 0x10;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800274 i2c_master_send(client, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 i2c_master_recv(client, buf, 1);
276
277 if (!(buf[0] & 0x20))
278 break;
279 if (time_after(jiffies,timeout)) {
280 status = -ETIMEDOUT;
281 break;
282 }
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 msleep(10);
285 }
286
Robert W. Boone9b715212005-11-08 21:36:45 -0800287 /* delay a bit to let encoder settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 msleep(50);
289
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293
294static int saa6752hs_set_bitrate(struct i2c_client* client,
Hans Verkuile281db582008-08-08 12:43:59 -0300295 struct saa6752hs_state *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Hans Verkuile281db582008-08-08 12:43:59 -0300297 struct saa6752hs_mpeg_params *params = &h->params;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800298 u8 buf[3];
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300299 int tot_bitrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Robert W. Boone9b715212005-11-08 21:36:45 -0800301 /* set the bitrate mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 buf[0] = 0x71;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300303 buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 i2c_master_send(client, buf, 2);
305
Robert W. Boone9b715212005-11-08 21:36:45 -0800306 /* set the video bitrate */
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300307 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
Robert W. Boone9b715212005-11-08 21:36:45 -0800308 /* set the target bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 buf[0] = 0x80;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300310 buf[1] = params->vi_bitrate >> 8;
311 buf[2] = params->vi_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 i2c_master_send(client, buf, 3);
313
Robert W. Boone9b715212005-11-08 21:36:45 -0800314 /* set the max bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 buf[0] = 0x81;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300316 buf[1] = params->vi_bitrate_peak >> 8;
317 buf[2] = params->vi_bitrate_peak & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 i2c_master_send(client, buf, 3);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300319 tot_bitrate = params->vi_bitrate_peak;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 } else {
Robert W. Boone9b715212005-11-08 21:36:45 -0800321 /* set the target bitrate (no max bitrate for CBR) */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800322 buf[0] = 0x81;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300323 buf[1] = params->vi_bitrate >> 8;
324 buf[2] = params->vi_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 i2c_master_send(client, buf, 3);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300326 tot_bitrate = params->vi_bitrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Hans Verkuile281db582008-08-08 12:43:59 -0300329 /* set the audio encoding */
330 buf[0] = 0x93;
Hans Verkuil3eea5432008-08-08 13:27:16 -0300331 buf[1] = (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
Hans Verkuile281db582008-08-08 12:43:59 -0300332 i2c_master_send(client, buf, 2);
333
Robert W. Boone9b715212005-11-08 21:36:45 -0800334 /* set the audio bitrate */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800335 buf[0] = 0x94;
Hans Verkuile281db582008-08-08 12:43:59 -0300336 if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
337 buf[1] = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
338 else
339 buf[1] = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
340 tot_bitrate += buf[1] ? 384 : 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 i2c_master_send(client, buf, 2);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300342
343 /* Note: the total max bitrate is determined by adding the video and audio
344 bitrates together and also adding an extra 768kbit/s to stay on the
345 safe side. If more control should be required, then an extra MPEG control
346 should be added. */
347 tot_bitrate += 768;
348 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
349 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Robert W. Boone9b715212005-11-08 21:36:45 -0800351 /* set the total bitrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 buf[0] = 0xb1;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300353 buf[1] = tot_bitrate >> 8;
354 buf[2] = tot_bitrate & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 i2c_master_send(client, buf, 3);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700360static void saa6752hs_set_subsampling(struct i2c_client* client,
361 struct v4l2_format* f)
362{
363 struct saa6752hs_state *h = i2c_get_clientdata(client);
364 int dist_352, dist_480, dist_720;
365
366 /*
367 FIXME: translate and round width/height into EMPRESS
368 subsample type:
369
370 type | PAL | NTSC
371 ---------------------------
372 SIF | 352x288 | 352x240
373 1/2 D1 | 352x576 | 352x480
374 2/3 D1 | 480x576 | 480x480
375 D1 | 720x576 | 720x480
376 */
377
378 dist_352 = abs(f->fmt.pix.width - 352);
379 dist_480 = abs(f->fmt.pix.width - 480);
380 dist_720 = abs(f->fmt.pix.width - 720);
381 if (dist_720 < dist_480) {
382 f->fmt.pix.width = 720;
383 f->fmt.pix.height = 576;
384 h->video_format = SAA6752HS_VF_D1;
385 }
386 else if (dist_480 < dist_352) {
387 f->fmt.pix.width = 480;
388 f->fmt.pix.height = 576;
389 h->video_format = SAA6752HS_VF_2_3_D1;
390 }
391 else {
392 f->fmt.pix.width = 352;
393 if (abs(f->fmt.pix.height - 576) <
394 abs(f->fmt.pix.height - 288)) {
395 f->fmt.pix.height = 576;
396 h->video_format = SAA6752HS_VF_1_2_D1;
397 }
398 else {
399 f->fmt.pix.height = 288;
400 h->video_format = SAA6752HS_VF_SIF;
401 }
402 }
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Hans Verkuile281db582008-08-08 12:43:59 -0300406static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
Hans Verkuil4d6b5ae2006-06-26 09:31:18 -0300407 struct v4l2_ext_control *ctrl, unsigned int cmd)
Hans Verkuil86b79d62006-06-18 16:40:10 -0300408{
409 int old = 0, new;
Hans Verkuil4d6b5ae2006-06-26 09:31:18 -0300410 int set = (cmd == VIDIOC_S_EXT_CTRLS);
Hans Verkuil86b79d62006-06-18 16:40:10 -0300411
412 new = ctrl->value;
413 switch (ctrl->id) {
414 case V4L2_CID_MPEG_STREAM_TYPE:
415 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
416 if (set && new != old)
417 return -ERANGE;
418 new = old;
419 break;
420 case V4L2_CID_MPEG_STREAM_PID_PMT:
421 old = params->ts_pid_pmt;
422 if (set && new > MPEG_PID_MAX)
423 return -ERANGE;
424 if (new > MPEG_PID_MAX)
425 new = MPEG_PID_MAX;
426 params->ts_pid_pmt = new;
427 break;
428 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
429 old = params->ts_pid_audio;
430 if (set && new > MPEG_PID_MAX)
431 return -ERANGE;
432 if (new > MPEG_PID_MAX)
433 new = MPEG_PID_MAX;
434 params->ts_pid_audio = new;
435 break;
436 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
437 old = params->ts_pid_video;
438 if (set && new > MPEG_PID_MAX)
439 return -ERANGE;
440 if (new > MPEG_PID_MAX)
441 new = MPEG_PID_MAX;
442 params->ts_pid_video = new;
443 break;
444 case V4L2_CID_MPEG_STREAM_PID_PCR:
445 old = params->ts_pid_pcr;
446 if (set && new > MPEG_PID_MAX)
447 return -ERANGE;
448 if (new > MPEG_PID_MAX)
449 new = MPEG_PID_MAX;
450 params->ts_pid_pcr = new;
451 break;
452 case V4L2_CID_MPEG_AUDIO_ENCODING:
Hans Verkuile281db582008-08-08 12:43:59 -0300453 old = params->au_encoding;
454 if (set && new != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 &&
455 (!has_ac3 || new != V4L2_MPEG_AUDIO_ENCODING_AC3))
Hans Verkuil86b79d62006-06-18 16:40:10 -0300456 return -ERANGE;
457 new = old;
458 break;
459 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
460 old = params->au_l2_bitrate;
461 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
462 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
463 return -ERANGE;
464 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
465 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
466 else
467 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
468 params->au_l2_bitrate = new;
469 break;
Hans Verkuile281db582008-08-08 12:43:59 -0300470 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
471 if (!has_ac3)
472 return -EINVAL;
473 old = params->au_ac3_bitrate;
474 if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K &&
475 new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K)
476 return -ERANGE;
477 if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K)
478 new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K;
479 else
480 new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K;
481 params->au_ac3_bitrate = new;
482 break;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300483 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
484 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
485 if (set && new != old)
486 return -ERANGE;
487 new = old;
488 break;
489 case V4L2_CID_MPEG_VIDEO_ENCODING:
490 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
491 if (set && new != old)
492 return -ERANGE;
493 new = old;
494 break;
495 case V4L2_CID_MPEG_VIDEO_ASPECT:
496 old = params->vi_aspect;
497 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
498 new != V4L2_MPEG_VIDEO_ASPECT_4x3)
499 return -ERANGE;
500 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
501 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
502 params->vi_aspect = new;
503 break;
504 case V4L2_CID_MPEG_VIDEO_BITRATE:
505 old = params->vi_bitrate * 1000;
506 new = 1000 * (new / 1000);
507 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
508 return -ERANGE;
509 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
510 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
511 params->vi_bitrate = new / 1000;
512 break;
513 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
514 old = params->vi_bitrate_peak * 1000;
515 new = 1000 * (new / 1000);
516 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
517 return -ERANGE;
518 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
519 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
520 params->vi_bitrate_peak = new / 1000;
521 break;
522 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
523 old = params->vi_bitrate_mode;
524 params->vi_bitrate_mode = new;
525 break;
526 default:
527 return -EINVAL;
528 }
529 if (cmd == VIDIOC_G_EXT_CTRLS)
530 ctrl->value = old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 else
Hans Verkuil86b79d62006-06-18 16:40:10 -0300532 ctrl->value = new;
533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Hans Verkuile281db582008-08-08 12:43:59 -0300536static int saa6752hs_qctrl(struct saa6752hs_state *h,
Hans Verkuil8b53b392008-06-27 21:18:15 -0300537 struct v4l2_queryctrl *qctrl)
538{
Hans Verkuile281db582008-08-08 12:43:59 -0300539 struct saa6752hs_mpeg_params *params = &h->params;
Hans Verkuil8b53b392008-06-27 21:18:15 -0300540 int err;
541
542 switch (qctrl->id) {
543 case V4L2_CID_MPEG_AUDIO_ENCODING:
544 return v4l2_ctrl_query_fill(qctrl,
545 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
Hans Verkuile281db582008-08-08 12:43:59 -0300546 h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
547 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
548 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
Hans Verkuil8b53b392008-06-27 21:18:15 -0300549
550 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
551 return v4l2_ctrl_query_fill(qctrl,
552 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
553 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
554 V4L2_MPEG_AUDIO_L2_BITRATE_256K);
555
Hans Verkuile281db582008-08-08 12:43:59 -0300556 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
557 if (!h->has_ac3)
558 return -EINVAL;
559 return v4l2_ctrl_query_fill(qctrl,
560 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
561 V4L2_MPEG_AUDIO_AC3_BITRATE_384K, 1,
562 V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
563
Hans Verkuil8b53b392008-06-27 21:18:15 -0300564 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
565 return v4l2_ctrl_query_fill(qctrl,
566 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
567 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, 1,
568 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
569
570 case V4L2_CID_MPEG_VIDEO_ENCODING:
571 return v4l2_ctrl_query_fill(qctrl,
572 V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
573 V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
574 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
575
576 case V4L2_CID_MPEG_VIDEO_ASPECT:
577 return v4l2_ctrl_query_fill(qctrl,
578 V4L2_MPEG_VIDEO_ASPECT_4x3,
579 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
580 V4L2_MPEG_VIDEO_ASPECT_4x3);
581
582 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
583 err = v4l2_ctrl_query_fill_std(qctrl);
584 if (err == 0 &&
585 params->vi_bitrate_mode ==
586 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
587 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
588 return err;
589
590 case V4L2_CID_MPEG_STREAM_TYPE:
591 return v4l2_ctrl_query_fill(qctrl,
592 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
593 V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1,
594 V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
595
596 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
597 case V4L2_CID_MPEG_VIDEO_BITRATE:
598 case V4L2_CID_MPEG_STREAM_PID_PMT:
599 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
600 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
601 case V4L2_CID_MPEG_STREAM_PID_PCR:
602 return v4l2_ctrl_query_fill_std(qctrl);
603
604 default:
605 break;
606 }
607 return -EINVAL;
608}
609
Hans Verkuile281db582008-08-08 12:43:59 -0300610static int saa6752hs_qmenu(struct saa6752hs_state *h,
Hans Verkuil8b53b392008-06-27 21:18:15 -0300611 struct v4l2_querymenu *qmenu)
612{
Hans Verkuile281db582008-08-08 12:43:59 -0300613 static const u32 mpeg_audio_encoding[] = {
614 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
615 V4L2_CTRL_MENU_IDS_END
616 };
617 static const u32 mpeg_audio_ac3_encoding[] = {
618 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
619 V4L2_MPEG_AUDIO_ENCODING_AC3,
620 V4L2_CTRL_MENU_IDS_END
621 };
622 static u32 mpeg_audio_l2_bitrate[] = {
623 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
624 V4L2_MPEG_AUDIO_L2_BITRATE_384K,
625 V4L2_CTRL_MENU_IDS_END
626 };
627 static u32 mpeg_audio_ac3_bitrate[] = {
628 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
629 V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
630 V4L2_CTRL_MENU_IDS_END
Hans Verkuil8b53b392008-06-27 21:18:15 -0300631 };
632 struct v4l2_queryctrl qctrl;
633 int err;
634
635 qctrl.id = qmenu->id;
Hans Verkuile281db582008-08-08 12:43:59 -0300636 err = saa6752hs_qctrl(h, &qctrl);
Hans Verkuil8b53b392008-06-27 21:18:15 -0300637 if (err)
638 return err;
Hans Verkuile281db582008-08-08 12:43:59 -0300639 switch (qmenu->id) {
640 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
641 return v4l2_ctrl_query_menu_valid_items(qmenu,
Hans Verkuil8b53b392008-06-27 21:18:15 -0300642 mpeg_audio_l2_bitrate);
Hans Verkuile281db582008-08-08 12:43:59 -0300643 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
644 if (!h->has_ac3)
645 return -EINVAL;
646 return v4l2_ctrl_query_menu_valid_items(qmenu,
647 mpeg_audio_ac3_bitrate);
648 case V4L2_CID_MPEG_AUDIO_ENCODING:
649 return v4l2_ctrl_query_menu_valid_items(qmenu,
650 h->has_ac3 ? mpeg_audio_ac3_encoding :
651 mpeg_audio_encoding);
652 }
653 return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
Hans Verkuil8b53b392008-06-27 21:18:15 -0300654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static int saa6752hs_init(struct i2c_client* client)
657{
658 unsigned char buf[9], buf2[4];
659 struct saa6752hs_state *h;
Hans Verkuil3eea5432008-08-08 13:27:16 -0300660 unsigned size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 u32 crc;
662 unsigned char localPAT[256];
663 unsigned char localPMT[256];
664
665 h = i2c_get_clientdata(client);
666
Robert W. Boone9b715212005-11-08 21:36:45 -0800667 /* Set video format - must be done first as it resets other settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 buf[0] = 0x41;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700669 buf[1] = h->video_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 i2c_master_send(client, buf, 2);
671
Robert W. Boone9b715212005-11-08 21:36:45 -0800672 /* Set number of lines in input signal */
673 buf[0] = 0x40;
674 buf[1] = 0x00;
675 if (h->standard & V4L2_STD_525_60)
676 buf[1] = 0x01;
677 i2c_master_send(client, buf, 2);
678
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800679 /* set bitrate */
Hans Verkuile281db582008-08-08 12:43:59 -0300680 saa6752hs_set_bitrate(client, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Robert W. Boone9b715212005-11-08 21:36:45 -0800682 /* Set GOP structure {3, 13} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 buf[0] = 0x72;
684 buf[1] = 0x03;
685 buf[2] = 0x0D;
686 i2c_master_send(client,buf,3);
687
Trent Piepho657de3c2006-06-20 00:30:57 -0300688 /* Set minimum Q-scale {4} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 buf[0] = 0x82;
690 buf[1] = 0x04;
691 i2c_master_send(client,buf,2);
692
Trent Piepho657de3c2006-06-20 00:30:57 -0300693 /* Set maximum Q-scale {12} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 buf[0] = 0x83;
695 buf[1] = 0x0C;
696 i2c_master_send(client,buf,2);
697
Trent Piepho657de3c2006-06-20 00:30:57 -0300698 /* Set Output Protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 buf[0] = 0xD0;
700 buf[1] = 0x81;
701 i2c_master_send(client,buf,2);
702
Trent Piepho657de3c2006-06-20 00:30:57 -0300703 /* Set video output stream format {TS} */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 buf[0] = 0xB0;
705 buf[1] = 0x05;
706 i2c_master_send(client,buf,2);
707
708 /* compute PAT */
709 memcpy(localPAT, PAT, sizeof(PAT));
710 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
711 localPAT[18] = h->params.ts_pid_pmt & 0xff;
712 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
713 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
714 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
715 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
716 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
717
718 /* compute PMT */
Hans Verkuil3eea5432008-08-08 13:27:16 -0300719 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
720 size = sizeof(PMT_AC3);
721 memcpy(localPMT, PMT_AC3, size);
722 } else {
723 size = sizeof(PMT);
724 memcpy(localPMT, PMT, size);
725 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800726 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
727 localPMT[4] = h->params.ts_pid_pmt & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
729 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
730 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
731 localPMT[21] = h->params.ts_pid_video & 0xFF;
732 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
733 localPMT[26] = h->params.ts_pid_audio & 0xFF;
Hans Verkuil3eea5432008-08-08 13:27:16 -0300734 crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
735 localPMT[size - 4] = (crc >> 24) & 0xFF;
736 localPMT[size - 3] = (crc >> 16) & 0xFF;
737 localPMT[size - 2] = (crc >> 8) & 0xFF;
738 localPMT[size - 1] = crc & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Trent Piepho657de3c2006-06-20 00:30:57 -0300740 /* Set Audio PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 buf[0] = 0xC1;
742 buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
743 buf[2] = h->params.ts_pid_audio & 0xFF;
744 i2c_master_send(client,buf,3);
745
Robert W. Boone9b715212005-11-08 21:36:45 -0800746 /* Set Video PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 buf[0] = 0xC0;
748 buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
749 buf[2] = h->params.ts_pid_video & 0xFF;
750 i2c_master_send(client,buf,3);
751
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800752 /* Set PCR PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 buf[0] = 0xC4;
754 buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
755 buf[2] = h->params.ts_pid_pcr & 0xFF;
756 i2c_master_send(client,buf,3);
757
Robert W. Boone9b715212005-11-08 21:36:45 -0800758 /* Send SI tables */
Hans Verkuil3eea5432008-08-08 13:27:16 -0300759 i2c_master_send(client, localPAT, sizeof(PAT));
760 i2c_master_send(client, localPMT, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Robert W. Boone9b715212005-11-08 21:36:45 -0800762 /* mute then unmute audio. This removes buzzing artefacts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 buf[0] = 0xa4;
764 buf[1] = 1;
765 i2c_master_send(client, buf, 2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800766 buf[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 i2c_master_send(client, buf, 2);
768
Robert W. Boone9b715212005-11-08 21:36:45 -0800769 /* start it going */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
771
Robert W. Boone9b715212005-11-08 21:36:45 -0800772 /* readout current state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 buf[0] = 0xE1;
774 buf[1] = 0xA7;
775 buf[2] = 0xFE;
776 buf[3] = 0x82;
777 buf[4] = 0xB0;
778 i2c_master_send(client, buf, 5);
779 i2c_master_recv(client, buf2, 4);
780
Robert W. Boone9b715212005-11-08 21:36:45 -0800781 /* change aspect ratio */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 buf[0] = 0xE0;
783 buf[1] = 0xA7;
784 buf[2] = 0xFE;
785 buf[3] = 0x82;
786 buf[4] = 0xB0;
787 buf[5] = buf2[0];
Hans Verkuil86b79d62006-06-18 16:40:10 -0300788 switch(h->params.vi_aspect) {
789 case V4L2_MPEG_VIDEO_ASPECT_16x9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 buf[6] = buf2[1] | 0x40;
791 break;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300792 case V4L2_MPEG_VIDEO_ASPECT_4x3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 default:
794 buf[6] = buf2[1] & 0xBF;
795 break;
796 break;
797 }
798 buf[7] = buf2[2];
799 buf[8] = buf2[3];
800 i2c_master_send(client, buf, 9);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static int
806saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
807{
808 struct saa6752hs_state *h = i2c_get_clientdata(client);
Hans Verkuil86b79d62006-06-18 16:40:10 -0300809 struct v4l2_ext_controls *ctrls = arg;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300810 struct saa6752hs_mpeg_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int err = 0;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300812 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800814 switch (cmd) {
Hans Verkuil86b79d62006-06-18 16:40:10 -0300815 case VIDIOC_S_EXT_CTRLS:
816 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
817 return -EINVAL;
818 if (ctrls->count == 0) {
819 /* apply settings and start encoder */
820 saa6752hs_init(client);
821 break;
822 }
823 /* fall through */
824 case VIDIOC_TRY_EXT_CTRLS:
825 case VIDIOC_G_EXT_CTRLS:
826 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
827 return -EINVAL;
828 params = h->params;
829 for (i = 0; i < ctrls->count; i++) {
Hans Verkuile281db582008-08-08 12:43:59 -0300830 err = handle_ctrl(h->has_ac3, &params, ctrls->controls + i, cmd);
831 if (err) {
Hans Verkuil86b79d62006-06-18 16:40:10 -0300832 ctrls->error_idx = i;
833 return err;
834 }
835 }
836 h->params = params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
Hans Verkuil8b53b392008-06-27 21:18:15 -0300838 case VIDIOC_QUERYCTRL:
Hans Verkuile281db582008-08-08 12:43:59 -0300839 return saa6752hs_qctrl(h, arg);
Hans Verkuil8b53b392008-06-27 21:18:15 -0300840 case VIDIOC_QUERYMENU:
Hans Verkuile281db582008-08-08 12:43:59 -0300841 return saa6752hs_qmenu(h, arg);
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700842 case VIDIOC_G_FMT:
843 {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800844 struct v4l2_format *f = arg;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700845
846 if (h->video_format == SAA6752HS_VF_UNKNOWN)
847 h->video_format = SAA6752HS_VF_D1;
848 f->fmt.pix.width =
849 v4l2_format_table[h->video_format].fmt.pix.width;
850 f->fmt.pix.height =
851 v4l2_format_table[h->video_format].fmt.pix.height;
852 break ;
853 }
854 case VIDIOC_S_FMT:
855 {
856 struct v4l2_format *f = arg;
857
858 saa6752hs_set_subsampling(client, f);
859 break;
860 }
Robert W. Boone9b715212005-11-08 21:36:45 -0800861 case VIDIOC_S_STD:
862 h->standard = *((v4l2_std_id *) arg);
863 break;
Hans Verkuile281db582008-08-08 12:43:59 -0300864
865 case VIDIOC_G_CHIP_IDENT:
866 return v4l2_chip_ident_i2c_client(client,
867 arg, h->chip, h->revision);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 default:
870 /* nothing */
871 break;
872 }
873
874 return err;
875}
876
Hans Verkuile281db582008-08-08 12:43:59 -0300877static int saa6752hs_probe(struct i2c_client *client,
878 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Hans Verkuile281db582008-08-08 12:43:59 -0300880 struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
881 u8 addr = 0x13;
882 u8 data[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Hans Verkuile281db582008-08-08 12:43:59 -0300884 v4l_info(client, "chip found @ 0x%x (%s)\n",
885 client->addr << 1, client->adapter->name);
886 if (h == NULL)
887 return -ENOMEM;
888
889 i2c_master_send(client, &addr, 1);
890 i2c_master_recv(client, data, sizeof(data));
891 h->chip = V4L2_IDENT_SAA6752HS;
892 h->revision = (data[8] << 8) | data[9];
893 h->has_ac3 = 0;
894 if (h->revision == 0x0206) {
895 h->chip = V4L2_IDENT_SAA6752HS_AC3;
896 h->has_ac3 = 1;
897 v4l_info(client, "support AC-3\n");
898 }
899 h->params = param_defaults;
900 h->standard = 0; /* Assume 625 input lines */
901
902 i2c_set_clientdata(client, h);
903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Hans Verkuile281db582008-08-08 12:43:59 -0300906static int saa6752hs_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Hans Verkuile281db582008-08-08 12:43:59 -0300908 kfree(i2c_get_clientdata(client));
909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Hans Verkuile281db582008-08-08 12:43:59 -0300912static const struct i2c_device_id saa6752hs_id[] = {
913 { "saa6752hs", 0 },
914 { }
915};
916MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
917
918static struct v4l2_i2c_driver_data v4l2_i2c_data = {
919 .name = "saa6752hs",
920 .driverid = I2C_DRIVERID_SAA6752HS,
921 .command = saa6752hs_command,
922 .probe = saa6752hs_probe,
923 .remove = saa6752hs_remove,
924 .id_table = saa6752hs_id,
925};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927/*
928 * Overrides for Emacs so that we follow Linus's tabbing style.
929 * ---------------------------------------------------------------------------
930 * Local variables:
931 * c-basic-offset: 8
932 * End:
933 */