blob: 25e6ab28a0a30c3628f94136bc651f00b0d3e8f9 [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 Verkuil5b73e982009-01-14 06:54:38 -030036#include <media/v4l2-device.h>
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -020037#include <media/v4l2-common.h>
Hans Verkuile281db582008-08-08 12:43:59 -030038#include <media/v4l2-chip-ident.h>
39#include <media/v4l2-i2c-drv-legacy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/init.h>
41#include <linux/crc32.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
44#define MPEG_VIDEO_MAX_BITRATE_MAX 27000
45#define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
46#define MPEG_PID_MAX ((1 << 14) - 1)
47
48/* Addresses to scan */
49static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
Hans Verkuilf87086e2008-07-18 00:50:58 -030050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051I2C_CLIENT_INSMOD;
52
53MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
54MODULE_AUTHOR("Andrew de Quincey");
55MODULE_LICENSE("GPL");
56
Frederic CAND0a4c9c92005-05-05 16:15:52 -070057enum saa6752hs_videoformat {
58 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
59 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
60 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
61 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
62 SAA6752HS_VF_UNKNOWN,
63};
64
Hans Verkuil86b79d62006-06-18 16:40:10 -030065struct saa6752hs_mpeg_params {
66 /* transport streams */
67 __u16 ts_pid_pmt;
68 __u16 ts_pid_audio;
69 __u16 ts_pid_video;
70 __u16 ts_pid_pcr;
71
72 /* audio */
Hans Verkuile281db582008-08-08 12:43:59 -030073 enum v4l2_mpeg_audio_encoding au_encoding;
74 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
75 enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
Hans Verkuil86b79d62006-06-18 16:40:10 -030076
77 /* video */
78 enum v4l2_mpeg_video_aspect vi_aspect;
79 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
80 __u32 vi_bitrate;
81 __u32 vi_bitrate_peak;
82};
83
Frederic CAND0a4c9c92005-05-05 16:15:52 -070084static const struct v4l2_format v4l2_format_table[] =
85{
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -070086 [SAA6752HS_VF_D1] =
87 { .fmt = { .pix = { .width = 720, .height = 576 }}},
88 [SAA6752HS_VF_2_3_D1] =
89 { .fmt = { .pix = { .width = 480, .height = 576 }}},
90 [SAA6752HS_VF_1_2_D1] =
91 { .fmt = { .pix = { .width = 352, .height = 576 }}},
92 [SAA6752HS_VF_SIF] =
93 { .fmt = { .pix = { .width = 352, .height = 288 }}},
94 [SAA6752HS_VF_UNKNOWN] =
95 { .fmt = { .pix = { .width = 0, .height = 0}}},
Frederic CAND0a4c9c92005-05-05 16:15:52 -070096};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct saa6752hs_state {
Hans Verkuil5b73e982009-01-14 06:54:38 -030099 struct v4l2_subdev sd;
Hans Verkuile281db582008-08-08 12:43:59 -0300100 int chip;
101 u32 revision;
102 int has_ac3;
Hans Verkuil86b79d62006-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
127static 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
153static 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 Verkuil3eea5432008-08-08 13:27:16 -0300182static u8 PMT_AC3[] = {
183 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 Verkuil86b79d62006-06-18 16:40:10 -0300215static struct saa6752hs_mpeg_params param_defaults =
216{
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 Verkuil86b79d62006-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 Verkuil86b79d62006-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 Verkuil5b73e982009-01-14 06:54:38 -0300370
371static int get_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
372 struct v4l2_ext_control *ctrl)
Frederic CAND0a4c9c92005-05-05 16:15:52 -0700373{
Hans Verkuil86b79d62006-06-18 16:40:10 -0300374 switch (ctrl->id) {
Hans Verkuil5b73e982009-01-14 06:54:38 -0300375 case V4L2_CID_MPEG_STREAM_TYPE:
376 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
377 break;
378 case V4L2_CID_MPEG_STREAM_PID_PMT:
379 ctrl->value = params->ts_pid_pmt;
380 break;
381 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
382 ctrl->value = params->ts_pid_audio;
383 break;
384 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
385 ctrl->value = params->ts_pid_video;
386 break;
387 case V4L2_CID_MPEG_STREAM_PID_PCR:
388 ctrl->value = params->ts_pid_pcr;
389 break;
390 case V4L2_CID_MPEG_AUDIO_ENCODING:
391 ctrl->value = params->au_encoding;
392 break;
393 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
394 ctrl->value = params->au_l2_bitrate;
395 break;
396 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
397 if (!has_ac3)
Hans Verkuil86b79d62006-06-18 16:40:10 -0300398 return -EINVAL;
Hans Verkuil5b73e982009-01-14 06:54:38 -0300399 ctrl->value = params->au_ac3_bitrate;
400 break;
401 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
402 ctrl->value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
403 break;
404 case V4L2_CID_MPEG_VIDEO_ENCODING:
405 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
406 break;
407 case V4L2_CID_MPEG_VIDEO_ASPECT:
408 ctrl->value = params->vi_aspect;
409 break;
410 case V4L2_CID_MPEG_VIDEO_BITRATE:
411 ctrl->value = params->vi_bitrate * 1000;
412 break;
413 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
414 ctrl->value = params->vi_bitrate_peak * 1000;
415 break;
416 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
417 ctrl->value = params->vi_bitrate_mode;
418 break;
419 default:
420 return -EINVAL;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300421 }
Hans Verkuil86b79d62006-06-18 16:40:10 -0300422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Hans Verkuil5b73e982009-01-14 06:54:38 -0300425static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
426 struct v4l2_ext_control *ctrl, int set)
Hans Verkuil8b53b392008-06-27 21:18:15 -0300427{
Hans Verkuil5b73e982009-01-14 06:54:38 -0300428 int old = 0, new;
429
430 new = ctrl->value;
431 switch (ctrl->id) {
432 case V4L2_CID_MPEG_STREAM_TYPE:
433 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
434 if (set && new != old)
435 return -ERANGE;
436 new = old;
437 break;
438 case V4L2_CID_MPEG_STREAM_PID_PMT:
439 old = params->ts_pid_pmt;
440 if (set && new > MPEG_PID_MAX)
441 return -ERANGE;
442 if (new > MPEG_PID_MAX)
443 new = MPEG_PID_MAX;
444 params->ts_pid_pmt = new;
445 break;
446 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
447 old = params->ts_pid_audio;
448 if (set && new > MPEG_PID_MAX)
449 return -ERANGE;
450 if (new > MPEG_PID_MAX)
451 new = MPEG_PID_MAX;
452 params->ts_pid_audio = new;
453 break;
454 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
455 old = params->ts_pid_video;
456 if (set && new > MPEG_PID_MAX)
457 return -ERANGE;
458 if (new > MPEG_PID_MAX)
459 new = MPEG_PID_MAX;
460 params->ts_pid_video = new;
461 break;
462 case V4L2_CID_MPEG_STREAM_PID_PCR:
463 old = params->ts_pid_pcr;
464 if (set && new > MPEG_PID_MAX)
465 return -ERANGE;
466 if (new > MPEG_PID_MAX)
467 new = MPEG_PID_MAX;
468 params->ts_pid_pcr = new;
469 break;
470 case V4L2_CID_MPEG_AUDIO_ENCODING:
471 old = params->au_encoding;
472 if (set && new != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 &&
473 (!has_ac3 || new != V4L2_MPEG_AUDIO_ENCODING_AC3))
474 return -ERANGE;
475 new = old;
476 break;
477 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
478 old = params->au_l2_bitrate;
479 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
480 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
481 return -ERANGE;
482 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
483 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
484 else
485 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
486 params->au_l2_bitrate = new;
487 break;
488 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
489 if (!has_ac3)
490 return -EINVAL;
491 old = params->au_ac3_bitrate;
492 if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K &&
493 new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K)
494 return -ERANGE;
495 if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K)
496 new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K;
497 else
498 new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K;
499 params->au_ac3_bitrate = new;
500 break;
501 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
502 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
503 if (set && new != old)
504 return -ERANGE;
505 new = old;
506 break;
507 case V4L2_CID_MPEG_VIDEO_ENCODING:
508 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
509 if (set && new != old)
510 return -ERANGE;
511 new = old;
512 break;
513 case V4L2_CID_MPEG_VIDEO_ASPECT:
514 old = params->vi_aspect;
515 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
516 new != V4L2_MPEG_VIDEO_ASPECT_4x3)
517 return -ERANGE;
518 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
519 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
520 params->vi_aspect = new;
521 break;
522 case V4L2_CID_MPEG_VIDEO_BITRATE:
523 old = params->vi_bitrate * 1000;
524 new = 1000 * (new / 1000);
525 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
526 return -ERANGE;
527 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
528 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
529 params->vi_bitrate = new / 1000;
530 break;
531 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
532 old = params->vi_bitrate_peak * 1000;
533 new = 1000 * (new / 1000);
534 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
535 return -ERANGE;
536 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
537 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
538 params->vi_bitrate_peak = new / 1000;
539 break;
540 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
541 old = params->vi_bitrate_mode;
542 params->vi_bitrate_mode = new;
543 break;
544 default:
545 return -EINVAL;
546 }
547 ctrl->value = new;
548 return 0;
549}
550
551
552static int saa6752hs_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
553{
554 struct saa6752hs_state *h = to_state(sd);
Hans Verkuile281db582008-08-08 12:43:59 -0300555 struct saa6752hs_mpeg_params *params = &h->params;
Hans Verkuil8b53b392008-06-27 21:18:15 -0300556 int err;
557
558 switch (qctrl->id) {
559 case V4L2_CID_MPEG_AUDIO_ENCODING:
560 return v4l2_ctrl_query_fill(qctrl,
561 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
Hans Verkuile281db582008-08-08 12:43:59 -0300562 h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
563 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
564 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
Hans Verkuil8b53b392008-06-27 21:18:15 -0300565
566 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
567 return v4l2_ctrl_query_fill(qctrl,
568 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
569 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
570 V4L2_MPEG_AUDIO_L2_BITRATE_256K);
571
Hans Verkuile281db582008-08-08 12:43:59 -0300572 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
573 if (!h->has_ac3)
574 return -EINVAL;
575 return v4l2_ctrl_query_fill(qctrl,
576 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
577 V4L2_MPEG_AUDIO_AC3_BITRATE_384K, 1,
578 V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
579
Hans Verkuil8b53b392008-06-27 21:18:15 -0300580 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
581 return v4l2_ctrl_query_fill(qctrl,
582 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
583 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, 1,
584 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
585
586 case V4L2_CID_MPEG_VIDEO_ENCODING:
587 return v4l2_ctrl_query_fill(qctrl,
588 V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
589 V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
590 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
591
592 case V4L2_CID_MPEG_VIDEO_ASPECT:
593 return v4l2_ctrl_query_fill(qctrl,
594 V4L2_MPEG_VIDEO_ASPECT_4x3,
595 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
596 V4L2_MPEG_VIDEO_ASPECT_4x3);
597
598 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
599 err = v4l2_ctrl_query_fill_std(qctrl);
600 if (err == 0 &&
601 params->vi_bitrate_mode ==
602 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
603 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
604 return err;
605
606 case V4L2_CID_MPEG_STREAM_TYPE:
607 return v4l2_ctrl_query_fill(qctrl,
608 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
609 V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1,
610 V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
611
612 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
613 case V4L2_CID_MPEG_VIDEO_BITRATE:
614 case V4L2_CID_MPEG_STREAM_PID_PMT:
615 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
616 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
617 case V4L2_CID_MPEG_STREAM_PID_PCR:
618 return v4l2_ctrl_query_fill_std(qctrl);
619
620 default:
621 break;
622 }
623 return -EINVAL;
624}
625
Hans Verkuil5b73e982009-01-14 06:54:38 -0300626static int saa6752hs_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qmenu)
Hans Verkuil8b53b392008-06-27 21:18:15 -0300627{
Hans Verkuile281db582008-08-08 12:43:59 -0300628 static const u32 mpeg_audio_encoding[] = {
629 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
630 V4L2_CTRL_MENU_IDS_END
631 };
632 static const u32 mpeg_audio_ac3_encoding[] = {
633 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
634 V4L2_MPEG_AUDIO_ENCODING_AC3,
635 V4L2_CTRL_MENU_IDS_END
636 };
637 static u32 mpeg_audio_l2_bitrate[] = {
638 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
639 V4L2_MPEG_AUDIO_L2_BITRATE_384K,
640 V4L2_CTRL_MENU_IDS_END
641 };
642 static u32 mpeg_audio_ac3_bitrate[] = {
643 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
644 V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
645 V4L2_CTRL_MENU_IDS_END
Hans Verkuil8b53b392008-06-27 21:18:15 -0300646 };
Hans Verkuil5b73e982009-01-14 06:54:38 -0300647 struct saa6752hs_state *h = to_state(sd);
Hans Verkuil8b53b392008-06-27 21:18:15 -0300648 struct v4l2_queryctrl qctrl;
649 int err;
650
651 qctrl.id = qmenu->id;
Hans Verkuil5b73e982009-01-14 06:54:38 -0300652 err = saa6752hs_queryctrl(sd, &qctrl);
Hans Verkuil8b53b392008-06-27 21:18:15 -0300653 if (err)
654 return err;
Hans Verkuile281db582008-08-08 12:43:59 -0300655 switch (qmenu->id) {
656 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
657 return v4l2_ctrl_query_menu_valid_items(qmenu,
Hans Verkuil8b53b392008-06-27 21:18:15 -0300658 mpeg_audio_l2_bitrate);
Hans Verkuile281db582008-08-08 12:43:59 -0300659 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
660 if (!h->has_ac3)
661 return -EINVAL;
662 return v4l2_ctrl_query_menu_valid_items(qmenu,
663 mpeg_audio_ac3_bitrate);
664 case V4L2_CID_MPEG_AUDIO_ENCODING:
665 return v4l2_ctrl_query_menu_valid_items(qmenu,
666 h->has_ac3 ? mpeg_audio_ac3_encoding :
667 mpeg_audio_encoding);
668 }
669 return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
Hans Verkuil8b53b392008-06-27 21:18:15 -0300670}
671
Hans Verkuil5b73e982009-01-14 06:54:38 -0300672static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 unsigned char buf[9], buf2[4];
Hans Verkuil5b73e982009-01-14 06:54:38 -0300675 struct saa6752hs_state *h = to_state(sd);
676 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil3eea5432008-08-08 13:27:16 -0300677 unsigned size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 u32 crc;
679 unsigned char localPAT[256];
680 unsigned char localPMT[256];
681
Robert W. Boone9b715212005-11-08 21:36:45 -0800682 /* Set video format - must be done first as it resets other settings */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300683 set_reg8(client, 0x41, h->video_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Robert W. Boone9b715212005-11-08 21:36:45 -0800685 /* Set number of lines in input signal */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300686 set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
Robert W. Boone9b715212005-11-08 21:36:45 -0800687
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800688 /* set bitrate */
Hans Verkuile281db582008-08-08 12:43:59 -0300689 saa6752hs_set_bitrate(client, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Robert W. Boone9b715212005-11-08 21:36:45 -0800691 /* Set GOP structure {3, 13} */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300692 set_reg16(client, 0x72, 0x030d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Trent Piepho657de3c2006-06-20 00:30:57 -0300694 /* Set minimum Q-scale {4} */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300695 set_reg8(client, 0x82, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Trent Piepho657de3c2006-06-20 00:30:57 -0300697 /* Set maximum Q-scale {12} */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300698 set_reg8(client, 0x83, 0x0c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Trent Piepho657de3c2006-06-20 00:30:57 -0300700 /* Set Output Protocol */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300701 set_reg8(client, 0xd0, 0x81);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Trent Piepho657de3c2006-06-20 00:30:57 -0300703 /* Set video output stream format {TS} */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300704 set_reg8(client, 0xb0, 0x05);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Dmitry Belimovf03813e2008-08-26 13:44:40 -0300706 /* Set leading null byte for TS */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300707 set_reg16(client, 0xf6, leading_null_bytes);
Dmitry Belimovf03813e2008-08-26 13:44:40 -0300708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* compute PAT */
710 memcpy(localPAT, PAT, sizeof(PAT));
711 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
712 localPAT[18] = h->params.ts_pid_pmt & 0xff;
713 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
714 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
715 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
716 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
717 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
718
719 /* compute PMT */
Hans Verkuil3eea5432008-08-08 13:27:16 -0300720 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
721 size = sizeof(PMT_AC3);
722 memcpy(localPMT, PMT_AC3, size);
723 } else {
724 size = sizeof(PMT);
725 memcpy(localPMT, PMT, size);
726 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800727 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
728 localPMT[4] = h->params.ts_pid_pmt & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
730 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
731 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
732 localPMT[21] = h->params.ts_pid_video & 0xFF;
733 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
734 localPMT[26] = h->params.ts_pid_audio & 0xFF;
Hans Verkuil3eea5432008-08-08 13:27:16 -0300735 crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
736 localPMT[size - 4] = (crc >> 24) & 0xFF;
737 localPMT[size - 3] = (crc >> 16) & 0xFF;
738 localPMT[size - 2] = (crc >> 8) & 0xFF;
739 localPMT[size - 1] = crc & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Trent Piepho657de3c2006-06-20 00:30:57 -0300741 /* Set Audio PID */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300742 set_reg16(client, 0xc1, h->params.ts_pid_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Robert W. Boone9b715212005-11-08 21:36:45 -0800744 /* Set Video PID */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300745 set_reg16(client, 0xc0, h->params.ts_pid_video);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800747 /* Set PCR PID */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300748 set_reg16(client, 0xc4, h->params.ts_pid_pcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Robert W. Boone9b715212005-11-08 21:36:45 -0800750 /* Send SI tables */
Hans Verkuil3eea5432008-08-08 13:27:16 -0300751 i2c_master_send(client, localPAT, sizeof(PAT));
752 i2c_master_send(client, localPMT, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Robert W. Boone9b715212005-11-08 21:36:45 -0800754 /* mute then unmute audio. This removes buzzing artefacts */
Hans Verkuilff5f26b2008-09-06 07:00:24 -0300755 set_reg8(client, 0xa4, 1);
756 set_reg8(client, 0xa4, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Robert W. Boone9b715212005-11-08 21:36:45 -0800758 /* start it going */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
760
Robert W. Boone9b715212005-11-08 21:36:45 -0800761 /* readout current state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 buf[0] = 0xE1;
763 buf[1] = 0xA7;
764 buf[2] = 0xFE;
765 buf[3] = 0x82;
766 buf[4] = 0xB0;
767 i2c_master_send(client, buf, 5);
768 i2c_master_recv(client, buf2, 4);
769
Robert W. Boone9b715212005-11-08 21:36:45 -0800770 /* change aspect ratio */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 buf[0] = 0xE0;
772 buf[1] = 0xA7;
773 buf[2] = 0xFE;
774 buf[3] = 0x82;
775 buf[4] = 0xB0;
776 buf[5] = buf2[0];
Hans Verkuil5b73e982009-01-14 06:54:38 -0300777 switch (h->params.vi_aspect) {
Hans Verkuil86b79d62006-06-18 16:40:10 -0300778 case V4L2_MPEG_VIDEO_ASPECT_16x9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 buf[6] = buf2[1] | 0x40;
780 break;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300781 case V4L2_MPEG_VIDEO_ASPECT_4x3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 default:
783 buf[6] = buf2[1] & 0xBF;
784 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 buf[7] = buf2[2];
787 buf[8] = buf2[3];
788 i2c_master_send(client, buf, 9);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 0;
791}
792
Hans Verkuil5b73e982009-01-14 06:54:38 -0300793static int saa6752hs_do_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls, int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Hans Verkuil5b73e982009-01-14 06:54:38 -0300795 struct saa6752hs_state *h = to_state(sd);
Hans Verkuil86b79d62006-06-18 16:40:10 -0300796 struct saa6752hs_mpeg_params params;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300797 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Hans Verkuil5b73e982009-01-14 06:54:38 -0300799 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
800 return -EINVAL;
801
802 params = h->params;
803 for (i = 0; i < ctrls->count; i++) {
804 int err = handle_ctrl(h->has_ac3, &params, ctrls->controls + i, set);
805
806 if (err) {
807 ctrls->error_idx = i;
808 return err;
Hans Verkuil86b79d62006-06-18 16:40:10 -0300809 }
Hans Verkuil5b73e982009-01-14 06:54:38 -0300810 }
811 if (set)
Hans Verkuil86b79d62006-06-18 16:40:10 -0300812 h->params = params;
Hans Verkuil5b73e982009-01-14 06:54:38 -0300813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Hans Verkuil5b73e982009-01-14 06:54:38 -0300816static int saa6752hs_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
817{
818 return saa6752hs_do_ext_ctrls(sd, ctrls, 1);
819}
820
821static int saa6752hs_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
822{
823 return saa6752hs_do_ext_ctrls(sd, ctrls, 0);
824}
825
826static int saa6752hs_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
827{
828 struct saa6752hs_state *h = to_state(sd);
829 int i;
830
831 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
832 return -EINVAL;
833
834 for (i = 0; i < ctrls->count; i++) {
835 int err = get_ctrl(h->has_ac3, &h->params, ctrls->controls + i);
836
837 if (err) {
838 ctrls->error_idx = i;
839 return err;
840 }
841 }
842 return 0;
843}
844
845static int saa6752hs_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
846{
847 struct saa6752hs_state *h = to_state(sd);
848
849 if (h->video_format == SAA6752HS_VF_UNKNOWN)
850 h->video_format = SAA6752HS_VF_D1;
851 f->fmt.pix.width =
852 v4l2_format_table[h->video_format].fmt.pix.width;
853 f->fmt.pix.height =
854 v4l2_format_table[h->video_format].fmt.pix.height;
855 return 0;
856}
857
858static int saa6752hs_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
859{
860 struct saa6752hs_state *h = to_state(sd);
861 int dist_352, dist_480, dist_720;
862
863 /*
864 FIXME: translate and round width/height into EMPRESS
865 subsample type:
866
867 type | PAL | NTSC
868 ---------------------------
869 SIF | 352x288 | 352x240
870 1/2 D1 | 352x576 | 352x480
871 2/3 D1 | 480x576 | 480x480
872 D1 | 720x576 | 720x480
873 */
874
875 dist_352 = abs(f->fmt.pix.width - 352);
876 dist_480 = abs(f->fmt.pix.width - 480);
877 dist_720 = abs(f->fmt.pix.width - 720);
878 if (dist_720 < dist_480) {
879 f->fmt.pix.width = 720;
880 f->fmt.pix.height = 576;
881 h->video_format = SAA6752HS_VF_D1;
882 } else if (dist_480 < dist_352) {
883 f->fmt.pix.width = 480;
884 f->fmt.pix.height = 576;
885 h->video_format = SAA6752HS_VF_2_3_D1;
886 } else {
887 f->fmt.pix.width = 352;
888 if (abs(f->fmt.pix.height - 576) <
889 abs(f->fmt.pix.height - 288)) {
890 f->fmt.pix.height = 576;
891 h->video_format = SAA6752HS_VF_1_2_D1;
892 } else {
893 f->fmt.pix.height = 288;
894 h->video_format = SAA6752HS_VF_SIF;
895 }
896 }
897 return 0;
898}
899
900static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
901{
902 struct saa6752hs_state *h = to_state(sd);
903
904 h->standard = std;
905 return 0;
906}
907
908static int saa6752hs_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
909{
910 struct i2c_client *client = v4l2_get_subdevdata(sd);
911 struct saa6752hs_state *h = to_state(sd);
912
913 return v4l2_chip_ident_i2c_client(client,
914 chip, h->chip, h->revision);
915}
916
917static int saa6752hs_command(struct i2c_client *client, unsigned cmd, void *arg)
918{
919 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
920}
921
922/* ----------------------------------------------------------------------- */
923
924static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
925 .g_chip_ident = saa6752hs_g_chip_ident,
926 .init = saa6752hs_init,
927 .queryctrl = saa6752hs_queryctrl,
928 .querymenu = saa6752hs_querymenu,
929 .g_ext_ctrls = saa6752hs_g_ext_ctrls,
930 .s_ext_ctrls = saa6752hs_s_ext_ctrls,
931 .try_ext_ctrls = saa6752hs_try_ext_ctrls,
932};
933
934static const struct v4l2_subdev_tuner_ops saa6752hs_tuner_ops = {
935 .s_std = saa6752hs_s_std,
936};
937
938static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
939 .s_fmt = saa6752hs_s_fmt,
940 .g_fmt = saa6752hs_g_fmt,
941};
942
943static const struct v4l2_subdev_ops saa6752hs_ops = {
944 .core = &saa6752hs_core_ops,
945 .tuner = &saa6752hs_tuner_ops,
946 .video = &saa6752hs_video_ops,
947};
948
Hans Verkuile281db582008-08-08 12:43:59 -0300949static int saa6752hs_probe(struct i2c_client *client,
Hans Verkuil5b73e982009-01-14 06:54:38 -0300950 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Hans Verkuile281db582008-08-08 12:43:59 -0300952 struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
Hans Verkuil5b73e982009-01-14 06:54:38 -0300953 struct v4l2_subdev *sd;
Hans Verkuile281db582008-08-08 12:43:59 -0300954 u8 addr = 0x13;
955 u8 data[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Hans Verkuile281db582008-08-08 12:43:59 -0300957 v4l_info(client, "chip found @ 0x%x (%s)\n",
958 client->addr << 1, client->adapter->name);
959 if (h == NULL)
960 return -ENOMEM;
Hans Verkuil5b73e982009-01-14 06:54:38 -0300961 sd = &h->sd;
962 v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
Hans Verkuile281db582008-08-08 12:43:59 -0300963
964 i2c_master_send(client, &addr, 1);
965 i2c_master_recv(client, data, sizeof(data));
966 h->chip = V4L2_IDENT_SAA6752HS;
967 h->revision = (data[8] << 8) | data[9];
968 h->has_ac3 = 0;
969 if (h->revision == 0x0206) {
970 h->chip = V4L2_IDENT_SAA6752HS_AC3;
971 h->has_ac3 = 1;
972 v4l_info(client, "support AC-3\n");
973 }
974 h->params = param_defaults;
975 h->standard = 0; /* Assume 625 input lines */
Hans Verkuile281db582008-08-08 12:43:59 -0300976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Hans Verkuile281db582008-08-08 12:43:59 -0300979static int saa6752hs_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Hans Verkuil5b73e982009-01-14 06:54:38 -0300981 struct v4l2_subdev *sd = i2c_get_clientdata(client);
982
983 v4l2_device_unregister_subdev(sd);
984 kfree(to_state(sd));
Hans Verkuile281db582008-08-08 12:43:59 -0300985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Hans Verkuile281db582008-08-08 12:43:59 -0300988static const struct i2c_device_id saa6752hs_id[] = {
989 { "saa6752hs", 0 },
990 { }
991};
992MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
993
994static struct v4l2_i2c_driver_data v4l2_i2c_data = {
995 .name = "saa6752hs",
996 .driverid = I2C_DRIVERID_SAA6752HS,
997 .command = saa6752hs_command,
998 .probe = saa6752hs_probe,
999 .remove = saa6752hs_remove,
1000 .id_table = saa6752hs_id,
1001};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003/*
1004 * Overrides for Emacs so that we follow Linus's tabbing style.
1005 * ---------------------------------------------------------------------------
1006 * Local variables:
1007 * c-basic-offset: 8
1008 * End:
1009 */