blob: 8272c0b9c5bf6e103294053316ed16fb0c87c317 [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>
Hans Verkuild8100f42013-12-14 08:28:30 -030036#include <linux/init.h>
37#include <linux/crc32.h>
Hans Verkuil5b73e982009-01-14 06:54:38 -030038#include <media/v4l2-device.h>
Hans Verkuilbb732cc2013-06-10 04:51:42 -030039#include <media/v4l2-ctrls.h>
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -020040#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
49MODULE_AUTHOR("Andrew de Quincey");
50MODULE_LICENSE("GPL");
51
Frederic CAND0a4c9c92005-05-05 16:15:52 -070052enum saa6752hs_videoformat {
53 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
54 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
55 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
56 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
57 SAA6752HS_VF_UNKNOWN,
58};
59
Hans Verkuil86b79d662006-06-18 16:40:10 -030060struct saa6752hs_mpeg_params {
61 /* transport streams */
62 __u16 ts_pid_pmt;
63 __u16 ts_pid_audio;
64 __u16 ts_pid_video;
65 __u16 ts_pid_pcr;
66
67 /* audio */
Hans Verkuile281db582008-08-08 12:43:59 -030068 enum v4l2_mpeg_audio_encoding au_encoding;
69 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
70 enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
Hans Verkuil86b79d662006-06-18 16:40:10 -030071
72 /* video */
73 enum v4l2_mpeg_video_aspect vi_aspect;
74 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
75 __u32 vi_bitrate;
76 __u32 vi_bitrate_peak;
77};
78
Frederic CAND0a4c9c92005-05-05 16:15:52 -070079static const struct v4l2_format v4l2_format_table[] =
80{
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -070081 [SAA6752HS_VF_D1] =
82 { .fmt = { .pix = { .width = 720, .height = 576 }}},
83 [SAA6752HS_VF_2_3_D1] =
84 { .fmt = { .pix = { .width = 480, .height = 576 }}},
85 [SAA6752HS_VF_1_2_D1] =
86 { .fmt = { .pix = { .width = 352, .height = 576 }}},
87 [SAA6752HS_VF_SIF] =
88 { .fmt = { .pix = { .width = 352, .height = 288 }}},
89 [SAA6752HS_VF_UNKNOWN] =
90 { .fmt = { .pix = { .width = 0, .height = 0}}},
Frederic CAND0a4c9c92005-05-05 16:15:52 -070091};
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct saa6752hs_state {
Hans Verkuil5b73e982009-01-14 06:54:38 -030094 struct v4l2_subdev sd;
Hans Verkuilbb732cc2013-06-10 04:51:42 -030095 struct v4l2_ctrl_handler hdl;
96 struct { /* video bitrate mode control cluster */
97 struct v4l2_ctrl *video_bitrate_mode;
98 struct v4l2_ctrl *video_bitrate;
99 struct v4l2_ctrl *video_bitrate_peak;
100 };
Hans Verkuile281db582008-08-08 12:43:59 -0300101 u32 revision;
102 int has_ac3;
Hans Verkuil86b79d662006-06-18 16:40:10 -0300103 struct saa6752hs_mpeg_params params;
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700104 enum saa6752hs_videoformat video_format;
Robert W. Boone9b715212005-11-08 21:36:45 -0800105 v4l2_std_id standard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108enum saa6752hs_command {
109 SAA6752HS_COMMAND_RESET = 0,
Trent Piepho657de3c2006-06-20 00:30:57 -0300110 SAA6752HS_COMMAND_STOP = 1,
111 SAA6752HS_COMMAND_START = 2,
112 SAA6752HS_COMMAND_PAUSE = 3,
113 SAA6752HS_COMMAND_RECONFIGURE = 4,
114 SAA6752HS_COMMAND_SLEEP = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
116
117 SAA6752HS_COMMAND_MAX
118};
119
Hans Verkuil5b73e982009-01-14 06:54:38 -0300120static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
121{
122 return container_of(sd, struct saa6752hs_state, sd);
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* ---------------------------------------------------------------------- */
126
Hans Verkuild8100f42013-12-14 08:28:30 -0300127static const u8 PAT[] = {
Robert W. Boone9b715212005-11-08 21:36:45 -0800128 0xc2, /* i2c register */
129 0x00, /* table number for encoder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Robert W. Boone9b715212005-11-08 21:36:45 -0800131 0x47, /* sync */
132 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
133 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Robert W. Boone9b715212005-11-08 21:36:45 -0800135 0x00, /* PSI pointer to start of table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Robert W. Boone9b715212005-11-08 21:36:45 -0800137 0x00, /* tid(0) */
138 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Robert W. Boone9b715212005-11-08 21:36:45 -0800140 0x00, 0x01, /* transport_stream_id(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Robert W. Boone9b715212005-11-08 21:36:45 -0800142 0xc1, /* version_number(0), current_next_indicator(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Robert W. Boone9b715212005-11-08 21:36:45 -0800144 0x00, 0x00, /* section_number(0), last_section_number(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Robert W. Boone9b715212005-11-08 21:36:45 -0800146 0x00, 0x01, /* program_number(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Robert W. Boone9b715212005-11-08 21:36:45 -0800148 0xe0, 0x00, /* PMT PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Robert W. Boone9b715212005-11-08 21:36:45 -0800150 0x00, 0x00, 0x00, 0x00 /* CRC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Hans Verkuild8100f42013-12-14 08:28:30 -0300153static const u8 PMT[] = {
Robert W. Boone9b715212005-11-08 21:36:45 -0800154 0xc2, /* i2c register */
155 0x01, /* table number for encoder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Robert W. Boone9b715212005-11-08 21:36:45 -0800157 0x47, /* sync */
158 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
159 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Robert W. Boone9b715212005-11-08 21:36:45 -0800161 0x00, /* PSI pointer to start of table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Robert W. Boone9b715212005-11-08 21:36:45 -0800163 0x02, /* tid(2) */
164 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Robert W. Boone9b715212005-11-08 21:36:45 -0800166 0x00, 0x01, /* program_number(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Robert W. Boone9b715212005-11-08 21:36:45 -0800168 0xc1, /* version_number(0), current_next_indicator(1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Robert W. Boone9b715212005-11-08 21:36:45 -0800170 0x00, 0x00, /* section_number(0), last_section_number(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Robert W. Boone9b715212005-11-08 21:36:45 -0800172 0xe0, 0x00, /* PCR_PID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Robert W. Boone9b715212005-11-08 21:36:45 -0800174 0xf0, 0x00, /* program_info_length(0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Robert W. Boone9b715212005-11-08 21:36:45 -0800176 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
177 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Robert W. Boone9b715212005-11-08 21:36:45 -0800179 0x00, 0x00, 0x00, 0x00 /* CRC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
Hans Verkuild8100f42013-12-14 08:28:30 -0300182static const u8 PMT_AC3[] = {
Hans Verkuil3eea5432008-08-08 13:27:16 -0300183 0xc2, /* i2c register */
184 0x01, /* table number for encoder(1) */
185 0x47, /* sync */
186
187 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
188 0x10, /* PMT PID (0x0010) */
189 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
190
191 0x00, /* PSI pointer to start of table */
192
193 0x02, /* TID (2) */
194 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
195
196 0x00, 0x01, /* program_number(1) */
197
198 0xc1, /* version_number(0), current_next_indicator(1) */
199
200 0x00, 0x00, /* section_number(0), last_section_number(0) */
201
202 0xe1, 0x04, /* PCR_PID (0x0104) */
203
204 0xf0, 0x00, /* program_info_length(0) */
205
206 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
207 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
208 0x6a, /* AC3 */
209 0x01, /* Descriptor_length(1) */
210 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
211
212 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
213};
214
Hans Verkuild8100f42013-12-14 08:28:30 -0300215static const struct saa6752hs_mpeg_params param_defaults =
Hans Verkuil86b79d662006-06-18 16:40:10 -0300216{
217 .ts_pid_pmt = 16,
218 .ts_pid_video = 260,
219 .ts_pid_audio = 256,
220 .ts_pid_pcr = 259,
221
222 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
223 .vi_bitrate = 4000,
224 .vi_bitrate_peak = 6000,
225 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
226
Hans Verkuile281db582008-08-08 12:43:59 -0300227 .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
Hans Verkuil86b79d662006-06-18 16:40:10 -0300228 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
Hans Verkuil3eea5432008-08-08 13:27:16 -0300229 .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
Hans Verkuil86b79d662006-06-18 16:40:10 -0300230};
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/* ---------------------------------------------------------------------- */
233
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300234static int saa6752hs_chip_command(struct i2c_client *client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 enum saa6752hs_command command)
236{
237 unsigned char buf[3];
238 unsigned long timeout;
239 int status = 0;
240
Robert W. Boone9b715212005-11-08 21:36:45 -0800241 /* execute the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 switch(command) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800243 case SAA6752HS_COMMAND_RESET:
244 buf[0] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246
247 case SAA6752HS_COMMAND_STOP:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800248 buf[0] = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250
251 case SAA6752HS_COMMAND_START:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800252 buf[0] = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 break;
254
255 case SAA6752HS_COMMAND_PAUSE:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800256 buf[0] = 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
258
259 case SAA6752HS_COMMAND_RECONFIGURE:
260 buf[0] = 0x05;
261 break;
262
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800263 case SAA6752HS_COMMAND_SLEEP:
264 buf[0] = 0x06;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 break;
266
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800267 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 buf[0] = 0x07;
269 break;
270
271 default:
272 return -EINVAL;
273 }
274
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800275 /* set it and wait for it to be so */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 i2c_master_send(client, buf, 1);
277 timeout = jiffies + HZ * 3;
278 for (;;) {
Robert W. Boone9b715212005-11-08 21:36:45 -0800279 /* get the current status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 buf[0] = 0x10;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800281 i2c_master_send(client, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 i2c_master_recv(client, buf, 1);
283
284 if (!(buf[0] & 0x20))
285 break;
286 if (time_after(jiffies,timeout)) {
287 status = -ETIMEDOUT;
288 break;
289 }
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 msleep(10);
292 }
293
Robert W. Boone9b715212005-11-08 21:36:45 -0800294 /* delay a bit to let encoder settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 msleep(50);
296
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800297 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300301static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
302{
303 u8 buf[2];
304
305 buf[0] = reg;
306 buf[1] = val;
307 i2c_master_send(client, buf, 2);
308}
309
310static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
311{
312 u8 buf[3];
313
314 buf[0] = reg;
315 buf[1] = val >> 8;
316 buf[2] = val & 0xff;
317 i2c_master_send(client, buf, 3);
318}
319
320static int saa6752hs_set_bitrate(struct i2c_client *client,
Hans Verkuile281db582008-08-08 12:43:59 -0300321 struct saa6752hs_state *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Hans Verkuile281db582008-08-08 12:43:59 -0300323 struct saa6752hs_mpeg_params *params = &h->params;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300324 int tot_bitrate;
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300325 int is_384k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Robert W. Boone9b715212005-11-08 21:36:45 -0800327 /* set the bitrate mode */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300328 set_reg8(client, 0x71,
329 params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Robert W. Boone9b715212005-11-08 21:36:45 -0800331 /* set the video bitrate */
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300332 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
Robert W. Boone9b715212005-11-08 21:36:45 -0800333 /* set the target bitrate */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300334 set_reg16(client, 0x80, params->vi_bitrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Robert W. Boone9b715212005-11-08 21:36:45 -0800336 /* set the max bitrate */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300337 set_reg16(client, 0x81, params->vi_bitrate_peak);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300338 tot_bitrate = params->vi_bitrate_peak;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 } else {
Robert W. Boone9b715212005-11-08 21:36:45 -0800340 /* set the target bitrate (no max bitrate for CBR) */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300341 set_reg16(client, 0x81, params->vi_bitrate);
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300342 tot_bitrate = params->vi_bitrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Hans Verkuile281db582008-08-08 12:43:59 -0300345 /* set the audio encoding */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300346 set_reg8(client, 0x93,
347 params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
Hans Verkuile281db582008-08-08 12:43:59 -0300348
Robert W. Boone9b715212005-11-08 21:36:45 -0800349 /* set the audio bitrate */
Hans Verkuile281db582008-08-08 12:43:59 -0300350 if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300351 is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
Hans Verkuile281db582008-08-08 12:43:59 -0300352 else
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300353 is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
354 set_reg8(client, 0x94, is_384k);
355 tot_bitrate += is_384k ? 384 : 256;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300356
357 /* Note: the total max bitrate is determined by adding the video and audio
358 bitrates together and also adding an extra 768kbit/s to stay on the
359 safe side. If more control should be required, then an extra MPEG control
360 should be added. */
361 tot_bitrate += 768;
362 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
363 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Robert W. Boone9b715212005-11-08 21:36:45 -0800365 /* set the total bitrate */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300366 set_reg16(client, 0xb1, tot_bitrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300370static int saa6752hs_try_ctrl(struct v4l2_ctrl *ctrl)
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700371{
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300372 struct saa6752hs_state *h =
373 container_of(ctrl->handler, struct saa6752hs_state, hdl);
374
Hans Verkuil86b79d662006-06-18 16:40:10 -0300375 switch (ctrl->id) {
Hans Verkuil5b73e982009-01-14 06:54:38 -0300376 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300377 /* peak bitrate shall be >= normal bitrate */
378 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
379 h->video_bitrate_peak->val < h->video_bitrate->val)
380 h->video_bitrate_peak->val = h->video_bitrate->val;
Hans Verkuil5b73e982009-01-14 06:54:38 -0300381 break;
Hans Verkuil86b79d662006-06-18 16:40:10 -0300382 }
Hans Verkuil86b79d662006-06-18 16:40:10 -0300383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300386static int saa6752hs_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil8b53b392008-06-27 21:18:15 -0300387{
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300388 struct saa6752hs_state *h =
389 container_of(ctrl->handler, struct saa6752hs_state, hdl);
Hans Verkuile281db582008-08-08 12:43:59 -0300390 struct saa6752hs_mpeg_params *params = &h->params;
Hans Verkuil8b53b392008-06-27 21:18:15 -0300391
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300392 switch (ctrl->id) {
Hans Verkuil8b53b392008-06-27 21:18:15 -0300393 case V4L2_CID_MPEG_STREAM_TYPE:
Hans Verkuil8b53b392008-06-27 21:18:15 -0300394 break;
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300395 case V4L2_CID_MPEG_STREAM_PID_PMT:
396 params->ts_pid_pmt = ctrl->val;
397 break;
398 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
399 params->ts_pid_audio = ctrl->val;
400 break;
401 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
402 params->ts_pid_video = ctrl->val;
403 break;
404 case V4L2_CID_MPEG_STREAM_PID_PCR:
405 params->ts_pid_pcr = ctrl->val;
406 break;
Hans Verkuile281db582008-08-08 12:43:59 -0300407 case V4L2_CID_MPEG_AUDIO_ENCODING:
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300408 params->au_encoding = ctrl->val;
409 break;
410 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
411 params->au_l2_bitrate = ctrl->val;
412 break;
413 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
414 params->au_ac3_bitrate = ctrl->val;
415 break;
416 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
417 break;
418 case V4L2_CID_MPEG_VIDEO_ENCODING:
419 break;
420 case V4L2_CID_MPEG_VIDEO_ASPECT:
421 params->vi_aspect = ctrl->val;
422 break;
423 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
424 params->vi_bitrate_mode = ctrl->val;
425 params->vi_bitrate = h->video_bitrate->val / 1000;
426 params->vi_bitrate_peak = h->video_bitrate_peak->val / 1000;
427 v4l2_ctrl_activate(h->video_bitrate_peak,
428 ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
429 break;
430 default:
431 return -EINVAL;
Hans Verkuile281db582008-08-08 12:43:59 -0300432 }
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300433 return 0;
Hans Verkuil8b53b392008-06-27 21:18:15 -0300434}
435
Hans Verkuil5b73e982009-01-14 06:54:38 -0300436static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 unsigned char buf[9], buf2[4];
Hans Verkuil5b73e982009-01-14 06:54:38 -0300439 struct saa6752hs_state *h = to_state(sd);
440 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil3eea5432008-08-08 13:27:16 -0300441 unsigned size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 u32 crc;
443 unsigned char localPAT[256];
444 unsigned char localPMT[256];
445
Robert W. Boone9b715212005-11-08 21:36:45 -0800446 /* Set video format - must be done first as it resets other settings */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300447 set_reg8(client, 0x41, h->video_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Robert W. Boone9b715212005-11-08 21:36:45 -0800449 /* Set number of lines in input signal */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300450 set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
Robert W. Boone9b715212005-11-08 21:36:45 -0800451
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800452 /* set bitrate */
Hans Verkuile281db582008-08-08 12:43:59 -0300453 saa6752hs_set_bitrate(client, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Robert W. Boone9b715212005-11-08 21:36:45 -0800455 /* Set GOP structure {3, 13} */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300456 set_reg16(client, 0x72, 0x030d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Trent Piepho657de3c2006-06-20 00:30:57 -0300458 /* Set minimum Q-scale {4} */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300459 set_reg8(client, 0x82, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Trent Piepho657de3c2006-06-20 00:30:57 -0300461 /* Set maximum Q-scale {12} */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300462 set_reg8(client, 0x83, 0x0c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Trent Piepho657de3c2006-06-20 00:30:57 -0300464 /* Set Output Protocol */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300465 set_reg8(client, 0xd0, 0x81);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Trent Piepho657de3c2006-06-20 00:30:57 -0300467 /* Set video output stream format {TS} */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300468 set_reg8(client, 0xb0, 0x05);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Dmitry Belimovf03813e2008-08-26 13:44:40 -0300470 /* Set leading null byte for TS */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300471 set_reg16(client, 0xf6, leading_null_bytes);
Dmitry Belimovf03813e2008-08-26 13:44:40 -0300472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* compute PAT */
474 memcpy(localPAT, PAT, sizeof(PAT));
475 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
476 localPAT[18] = h->params.ts_pid_pmt & 0xff;
477 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
478 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
479 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
480 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
481 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
482
483 /* compute PMT */
Hans Verkuil3eea5432008-08-08 13:27:16 -0300484 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
485 size = sizeof(PMT_AC3);
486 memcpy(localPMT, PMT_AC3, size);
487 } else {
488 size = sizeof(PMT);
489 memcpy(localPMT, PMT, size);
490 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800491 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
492 localPMT[4] = h->params.ts_pid_pmt & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
494 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
495 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
496 localPMT[21] = h->params.ts_pid_video & 0xFF;
497 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
498 localPMT[26] = h->params.ts_pid_audio & 0xFF;
Hans Verkuil3eea5432008-08-08 13:27:16 -0300499 crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
500 localPMT[size - 4] = (crc >> 24) & 0xFF;
501 localPMT[size - 3] = (crc >> 16) & 0xFF;
502 localPMT[size - 2] = (crc >> 8) & 0xFF;
503 localPMT[size - 1] = crc & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Trent Piepho657de3c2006-06-20 00:30:57 -0300505 /* Set Audio PID */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300506 set_reg16(client, 0xc1, h->params.ts_pid_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Robert W. Boone9b715212005-11-08 21:36:45 -0800508 /* Set Video PID */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300509 set_reg16(client, 0xc0, h->params.ts_pid_video);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800511 /* Set PCR PID */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300512 set_reg16(client, 0xc4, h->params.ts_pid_pcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Robert W. Boone9b715212005-11-08 21:36:45 -0800514 /* Send SI tables */
Hans Verkuil3eea5432008-08-08 13:27:16 -0300515 i2c_master_send(client, localPAT, sizeof(PAT));
516 i2c_master_send(client, localPMT, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Robert W. Boone9b715212005-11-08 21:36:45 -0800518 /* mute then unmute audio. This removes buzzing artefacts */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300519 set_reg8(client, 0xa4, 1);
520 set_reg8(client, 0xa4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Robert W. Boone9b715212005-11-08 21:36:45 -0800522 /* start it going */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
524
Robert W. Boone9b715212005-11-08 21:36:45 -0800525 /* readout current state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 buf[0] = 0xE1;
527 buf[1] = 0xA7;
528 buf[2] = 0xFE;
529 buf[3] = 0x82;
530 buf[4] = 0xB0;
531 i2c_master_send(client, buf, 5);
532 i2c_master_recv(client, buf2, 4);
533
Robert W. Boone9b715212005-11-08 21:36:45 -0800534 /* change aspect ratio */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 buf[0] = 0xE0;
536 buf[1] = 0xA7;
537 buf[2] = 0xFE;
538 buf[3] = 0x82;
539 buf[4] = 0xB0;
540 buf[5] = buf2[0];
Hans Verkuil5b73e982009-01-14 06:54:38 -0300541 switch (h->params.vi_aspect) {
Hans Verkuil86b79d662006-06-18 16:40:10 -0300542 case V4L2_MPEG_VIDEO_ASPECT_16x9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 buf[6] = buf2[1] | 0x40;
544 break;
Hans Verkuil86b79d662006-06-18 16:40:10 -0300545 case V4L2_MPEG_VIDEO_ASPECT_4x3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 default:
547 buf[6] = buf2[1] & 0xBF;
548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 buf[7] = buf2[2];
551 buf[8] = buf2[3];
552 i2c_master_send(client, buf, 9);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
555}
556
Hans Verkuil51623ef2010-05-09 09:39:58 -0300557static int saa6752hs_g_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
Hans Verkuil5b73e982009-01-14 06:54:38 -0300558{
559 struct saa6752hs_state *h = to_state(sd);
560
561 if (h->video_format == SAA6752HS_VF_UNKNOWN)
562 h->video_format = SAA6752HS_VF_D1;
Hans Verkuil51623ef2010-05-09 09:39:58 -0300563 f->width = v4l2_format_table[h->video_format].fmt.pix.width;
564 f->height = v4l2_format_table[h->video_format].fmt.pix.height;
565 f->code = V4L2_MBUS_FMT_FIXED;
566 f->field = V4L2_FIELD_INTERLACED;
567 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuil5b73e982009-01-14 06:54:38 -0300568 return 0;
569}
570
Hans Verkuilcabc6502013-06-01 10:02:38 -0300571static int saa6752hs_try_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
572{
573 int dist_352, dist_480, dist_720;
574
575 f->code = V4L2_MBUS_FMT_FIXED;
576
577 dist_352 = abs(f->width - 352);
578 dist_480 = abs(f->width - 480);
579 dist_720 = abs(f->width - 720);
580 if (dist_720 < dist_480) {
581 f->width = 720;
582 f->height = 576;
583 } else if (dist_480 < dist_352) {
584 f->width = 480;
585 f->height = 576;
586 } else {
587 f->width = 352;
588 if (abs(f->height - 576) < abs(f->height - 288))
589 f->height = 576;
590 else
591 f->height = 288;
592 }
593 f->field = V4L2_FIELD_INTERLACED;
594 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
595 return 0;
596}
597
Hans Verkuil51623ef2010-05-09 09:39:58 -0300598static int saa6752hs_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
Hans Verkuil5b73e982009-01-14 06:54:38 -0300599{
600 struct saa6752hs_state *h = to_state(sd);
Hans Verkuil5b73e982009-01-14 06:54:38 -0300601
Hans Verkuil51623ef2010-05-09 09:39:58 -0300602 if (f->code != V4L2_MBUS_FMT_FIXED)
603 return -EINVAL;
604
Hans Verkuil5b73e982009-01-14 06:54:38 -0300605 /*
606 FIXME: translate and round width/height into EMPRESS
607 subsample type:
608
609 type | PAL | NTSC
610 ---------------------------
611 SIF | 352x288 | 352x240
612 1/2 D1 | 352x576 | 352x480
613 2/3 D1 | 480x576 | 480x480
614 D1 | 720x576 | 720x480
615 */
616
Hans Verkuilcabc6502013-06-01 10:02:38 -0300617 saa6752hs_try_mbus_fmt(sd, f);
618 if (f->width == 720)
Hans Verkuil5b73e982009-01-14 06:54:38 -0300619 h->video_format = SAA6752HS_VF_D1;
Hans Verkuilcabc6502013-06-01 10:02:38 -0300620 else if (f->width == 480)
Hans Verkuil5b73e982009-01-14 06:54:38 -0300621 h->video_format = SAA6752HS_VF_2_3_D1;
Hans Verkuilcabc6502013-06-01 10:02:38 -0300622 else if (f->height == 576)
623 h->video_format = SAA6752HS_VF_1_2_D1;
624 else
625 h->video_format = SAA6752HS_VF_SIF;
Hans Verkuil5b73e982009-01-14 06:54:38 -0300626 return 0;
627}
628
629static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
630{
631 struct saa6752hs_state *h = to_state(sd);
632
633 h->standard = std;
634 return 0;
635}
636
Hans Verkuil5b73e982009-01-14 06:54:38 -0300637/* ----------------------------------------------------------------------- */
638
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300639static const struct v4l2_ctrl_ops saa6752hs_ctrl_ops = {
640 .try_ctrl = saa6752hs_try_ctrl,
641 .s_ctrl = saa6752hs_s_ctrl,
642};
643
Hans Verkuil5b73e982009-01-14 06:54:38 -0300644static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
Hans Verkuil5b73e982009-01-14 06:54:38 -0300645 .init = saa6752hs_init,
Hans Verkuil5b73e982009-01-14 06:54:38 -0300646 .s_std = saa6752hs_s_std,
647};
648
649static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
Hans Verkuil51623ef2010-05-09 09:39:58 -0300650 .s_mbus_fmt = saa6752hs_s_mbus_fmt,
Hans Verkuilcabc6502013-06-01 10:02:38 -0300651 .try_mbus_fmt = saa6752hs_try_mbus_fmt,
Hans Verkuil51623ef2010-05-09 09:39:58 -0300652 .g_mbus_fmt = saa6752hs_g_mbus_fmt,
Hans Verkuil5b73e982009-01-14 06:54:38 -0300653};
654
655static const struct v4l2_subdev_ops saa6752hs_ops = {
656 .core = &saa6752hs_core_ops,
Hans Verkuil5b73e982009-01-14 06:54:38 -0300657 .video = &saa6752hs_video_ops,
658};
659
Hans Verkuile281db582008-08-08 12:43:59 -0300660static int saa6752hs_probe(struct i2c_client *client,
Hans Verkuil5b73e982009-01-14 06:54:38 -0300661 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Hans Verkuile281db582008-08-08 12:43:59 -0300663 struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
Hans Verkuil5b73e982009-01-14 06:54:38 -0300664 struct v4l2_subdev *sd;
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300665 struct v4l2_ctrl_handler *hdl;
Hans Verkuile281db582008-08-08 12:43:59 -0300666 u8 addr = 0x13;
667 u8 data[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Hans Verkuile281db582008-08-08 12:43:59 -0300669 v4l_info(client, "chip found @ 0x%x (%s)\n",
670 client->addr << 1, client->adapter->name);
671 if (h == NULL)
672 return -ENOMEM;
Hans Verkuil5b73e982009-01-14 06:54:38 -0300673 sd = &h->sd;
674 v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
Hans Verkuile281db582008-08-08 12:43:59 -0300675
676 i2c_master_send(client, &addr, 1);
677 i2c_master_recv(client, data, sizeof(data));
Hans Verkuile281db582008-08-08 12:43:59 -0300678 h->revision = (data[8] << 8) | data[9];
679 h->has_ac3 = 0;
680 if (h->revision == 0x0206) {
Hans Verkuile281db582008-08-08 12:43:59 -0300681 h->has_ac3 = 1;
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300682 v4l_info(client, "supports AC-3\n");
Hans Verkuile281db582008-08-08 12:43:59 -0300683 }
684 h->params = param_defaults;
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300685
686 hdl = &h->hdl;
687 v4l2_ctrl_handler_init(hdl, 14);
688 v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
689 V4L2_CID_MPEG_AUDIO_ENCODING,
690 h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
691 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
692 0x0d, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
693
694 v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
695 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
696 V4L2_MPEG_AUDIO_L2_BITRATE_384K,
697 ~((1 << V4L2_MPEG_AUDIO_L2_BITRATE_256K) |
698 (1 << V4L2_MPEG_AUDIO_L2_BITRATE_384K)),
699 V4L2_MPEG_AUDIO_L2_BITRATE_256K);
700
701 if (h->has_ac3)
702 v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
703 V4L2_CID_MPEG_AUDIO_AC3_BITRATE,
704 V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
705 ~((1 << V4L2_MPEG_AUDIO_AC3_BITRATE_256K) |
706 (1 << V4L2_MPEG_AUDIO_AC3_BITRATE_384K)),
707 V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
708
709 v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
710 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
711 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
712 ~(1 << V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000),
713 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
714
715 v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
716 V4L2_CID_MPEG_VIDEO_ENCODING,
717 V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
718 ~(1 << V4L2_MPEG_VIDEO_ENCODING_MPEG_2),
719 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
720
721 v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
722 V4L2_CID_MPEG_VIDEO_ASPECT,
723 V4L2_MPEG_VIDEO_ASPECT_16x9, 0x01,
724 V4L2_MPEG_VIDEO_ASPECT_4x3);
725
726 h->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
727 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
728 1000000, 27000000, 1000, 8000000);
729
730 v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
731 V4L2_CID_MPEG_STREAM_TYPE,
732 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
733 ~(1 << V4L2_MPEG_STREAM_TYPE_MPEG2_TS),
734 V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
735
736 h->video_bitrate_mode = v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
737 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
738 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
739 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
740 h->video_bitrate = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
741 V4L2_CID_MPEG_VIDEO_BITRATE, 1000000, 27000000, 1000, 6000000);
742 v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
743 V4L2_CID_MPEG_STREAM_PID_PMT, 0, (1 << 14) - 1, 1, 16);
744 v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
745 V4L2_CID_MPEG_STREAM_PID_AUDIO, 0, (1 << 14) - 1, 1, 260);
746 v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
747 V4L2_CID_MPEG_STREAM_PID_VIDEO, 0, (1 << 14) - 1, 1, 256);
748 v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
749 V4L2_CID_MPEG_STREAM_PID_PCR, 0, (1 << 14) - 1, 1, 259);
750 sd->ctrl_handler = hdl;
751 if (hdl->error) {
752 int err = hdl->error;
753
754 v4l2_ctrl_handler_free(hdl);
755 kfree(h);
756 return err;
757 }
758 v4l2_ctrl_cluster(3, &h->video_bitrate_mode);
759 v4l2_ctrl_handler_setup(hdl);
Hans Verkuile281db582008-08-08 12:43:59 -0300760 h->standard = 0; /* Assume 625 input lines */
Hans Verkuile281db582008-08-08 12:43:59 -0300761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Hans Verkuile281db582008-08-08 12:43:59 -0300764static int saa6752hs_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Hans Verkuil5b73e982009-01-14 06:54:38 -0300766 struct v4l2_subdev *sd = i2c_get_clientdata(client);
767
768 v4l2_device_unregister_subdev(sd);
Hans Verkuilbb732cc2013-06-10 04:51:42 -0300769 v4l2_ctrl_handler_free(&to_state(sd)->hdl);
Hans Verkuil5b73e982009-01-14 06:54:38 -0300770 kfree(to_state(sd));
Hans Verkuile281db582008-08-08 12:43:59 -0300771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Hans Verkuile281db582008-08-08 12:43:59 -0300774static const struct i2c_device_id saa6752hs_id[] = {
775 { "saa6752hs", 0 },
776 { }
777};
778MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
779
Hans Verkuil099bd5e2010-09-15 15:14:36 -0300780static struct i2c_driver saa6752hs_driver = {
781 .driver = {
782 .owner = THIS_MODULE,
783 .name = "saa6752hs",
784 },
785 .probe = saa6752hs_probe,
786 .remove = saa6752hs_remove,
787 .id_table = saa6752hs_id,
Hans Verkuile281db582008-08-08 12:43:59 -0300788};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Axel Linc6e8d862012-02-12 06:56:32 -0300790module_i2c_driver(saa6752hs_driver);