Commit | Line | Data |
---|---|---|
e281db58 HV |
1 | /* |
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 | ||
1da177e4 LT |
25 | #include <linux/module.h> |
26 | #include <linux/kernel.h> | |
1da177e4 LT |
27 | #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> | |
cab462f7 | 35 | #include <linux/videodev2.h> |
5b73e98c | 36 | #include <media/v4l2-device.h> |
cab462f7 | 37 | #include <media/v4l2-common.h> |
1da177e4 LT |
38 | #include <linux/init.h> |
39 | #include <linux/crc32.h> | |
40 | ||
1da177e4 LT |
41 | #define MPEG_VIDEO_TARGET_BITRATE_MAX 27000 |
42 | #define MPEG_VIDEO_MAX_BITRATE_MAX 27000 | |
43 | #define MPEG_TOTAL_TARGET_BITRATE_MAX 27000 | |
44 | #define MPEG_PID_MAX ((1 << 14) - 1) | |
45 | ||
1da177e4 LT |
46 | |
47 | MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder"); | |
48 | MODULE_AUTHOR("Andrew de Quincey"); | |
49 | MODULE_LICENSE("GPL"); | |
50 | ||
0a4c9c93 FC |
51 | enum saa6752hs_videoformat { |
52 | SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */ | |
53 | SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */ | |
54 | SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */ | |
55 | SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */ | |
56 | SAA6752HS_VF_UNKNOWN, | |
57 | }; | |
58 | ||
86b79d66 HV |
59 | struct saa6752hs_mpeg_params { |
60 | /* transport streams */ | |
61 | __u16 ts_pid_pmt; | |
62 | __u16 ts_pid_audio; | |
63 | __u16 ts_pid_video; | |
64 | __u16 ts_pid_pcr; | |
65 | ||
66 | /* audio */ | |
e281db58 HV |
67 | enum v4l2_mpeg_audio_encoding au_encoding; |
68 | enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate; | |
69 | enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate; | |
86b79d66 HV |
70 | |
71 | /* video */ | |
72 | enum v4l2_mpeg_video_aspect vi_aspect; | |
73 | enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode; | |
74 | __u32 vi_bitrate; | |
75 | __u32 vi_bitrate_peak; | |
76 | }; | |
77 | ||
0a4c9c93 FC |
78 | static const struct v4l2_format v4l2_format_table[] = |
79 | { | |
ac19ecc6 MCC |
80 | [SAA6752HS_VF_D1] = |
81 | { .fmt = { .pix = { .width = 720, .height = 576 }}}, | |
82 | [SAA6752HS_VF_2_3_D1] = | |
83 | { .fmt = { .pix = { .width = 480, .height = 576 }}}, | |
84 | [SAA6752HS_VF_1_2_D1] = | |
85 | { .fmt = { .pix = { .width = 352, .height = 576 }}}, | |
86 | [SAA6752HS_VF_SIF] = | |
87 | { .fmt = { .pix = { .width = 352, .height = 288 }}}, | |
88 | [SAA6752HS_VF_UNKNOWN] = | |
89 | { .fmt = { .pix = { .width = 0, .height = 0}}}, | |
0a4c9c93 FC |
90 | }; |
91 | ||
1da177e4 | 92 | struct saa6752hs_state { |
5b73e98c | 93 | struct v4l2_subdev sd; |
e281db58 HV |
94 | u32 revision; |
95 | int has_ac3; | |
86b79d66 | 96 | struct saa6752hs_mpeg_params params; |
0a4c9c93 | 97 | enum saa6752hs_videoformat video_format; |
9b71521b | 98 | v4l2_std_id standard; |
1da177e4 LT |
99 | }; |
100 | ||
101 | enum saa6752hs_command { | |
102 | SAA6752HS_COMMAND_RESET = 0, | |
657de3cd TP |
103 | SAA6752HS_COMMAND_STOP = 1, |
104 | SAA6752HS_COMMAND_START = 2, | |
105 | SAA6752HS_COMMAND_PAUSE = 3, | |
106 | SAA6752HS_COMMAND_RECONFIGURE = 4, | |
107 | SAA6752HS_COMMAND_SLEEP = 5, | |
1da177e4 LT |
108 | SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6, |
109 | ||
110 | SAA6752HS_COMMAND_MAX | |
111 | }; | |
112 | ||
5b73e98c HV |
113 | static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd) |
114 | { | |
115 | return container_of(sd, struct saa6752hs_state, sd); | |
116 | } | |
117 | ||
1da177e4 LT |
118 | /* ---------------------------------------------------------------------- */ |
119 | ||
120 | static u8 PAT[] = { | |
9b71521b RB |
121 | 0xc2, /* i2c register */ |
122 | 0x00, /* table number for encoder */ | |
1da177e4 | 123 | |
9b71521b RB |
124 | 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) */ | |
1da177e4 | 127 | |
9b71521b | 128 | 0x00, /* PSI pointer to start of table */ |
1da177e4 | 129 | |
9b71521b RB |
130 | 0x00, /* tid(0) */ |
131 | 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */ | |
1da177e4 | 132 | |
9b71521b | 133 | 0x00, 0x01, /* transport_stream_id(1) */ |
1da177e4 | 134 | |
9b71521b | 135 | 0xc1, /* version_number(0), current_next_indicator(1) */ |
1da177e4 | 136 | |
9b71521b | 137 | 0x00, 0x00, /* section_number(0), last_section_number(0) */ |
1da177e4 | 138 | |
9b71521b | 139 | 0x00, 0x01, /* program_number(1) */ |
1da177e4 | 140 | |
9b71521b | 141 | 0xe0, 0x00, /* PMT PID */ |
1da177e4 | 142 | |
9b71521b | 143 | 0x00, 0x00, 0x00, 0x00 /* CRC32 */ |
1da177e4 LT |
144 | }; |
145 | ||
146 | static u8 PMT[] = { | |
9b71521b RB |
147 | 0xc2, /* i2c register */ |
148 | 0x01, /* table number for encoder */ | |
1da177e4 | 149 | |
9b71521b RB |
150 | 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) */ | |
1da177e4 | 153 | |
9b71521b | 154 | 0x00, /* PSI pointer to start of table */ |
1da177e4 | 155 | |
9b71521b RB |
156 | 0x02, /* tid(2) */ |
157 | 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */ | |
1da177e4 | 158 | |
9b71521b | 159 | 0x00, 0x01, /* program_number(1) */ |
1da177e4 | 160 | |
9b71521b | 161 | 0xc1, /* version_number(0), current_next_indicator(1) */ |
1da177e4 | 162 | |
9b71521b | 163 | 0x00, 0x00, /* section_number(0), last_section_number(0) */ |
1da177e4 | 164 | |
9b71521b | 165 | 0xe0, 0x00, /* PCR_PID */ |
1da177e4 | 166 | |
9b71521b | 167 | 0xf0, 0x00, /* program_info_length(0) */ |
1da177e4 | 168 | |
9b71521b RB |
169 | 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */ |
170 | 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */ | |
1da177e4 | 171 | |
9b71521b | 172 | 0x00, 0x00, 0x00, 0x00 /* CRC32 */ |
1da177e4 LT |
173 | }; |
174 | ||
3eea543b HV |
175 | static 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 | ||
86b79d66 HV |
208 | static 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 | ||
e281db58 | 220 | .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2, |
86b79d66 | 221 | .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K, |
3eea543b | 222 | .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K, |
86b79d66 HV |
223 | }; |
224 | ||
1da177e4 LT |
225 | /* ---------------------------------------------------------------------- */ |
226 | ||
ff5f26b4 | 227 | static int saa6752hs_chip_command(struct i2c_client *client, |
1da177e4 LT |
228 | enum saa6752hs_command command) |
229 | { | |
230 | unsigned char buf[3]; | |
231 | unsigned long timeout; | |
232 | int status = 0; | |
233 | ||
9b71521b | 234 | /* execute the command */ |
1da177e4 | 235 | switch(command) { |
4ac97914 MCC |
236 | case SAA6752HS_COMMAND_RESET: |
237 | buf[0] = 0x00; | |
1da177e4 LT |
238 | break; |
239 | ||
240 | case SAA6752HS_COMMAND_STOP: | |
4ac97914 | 241 | buf[0] = 0x03; |
1da177e4 LT |
242 | break; |
243 | ||
244 | case SAA6752HS_COMMAND_START: | |
4ac97914 | 245 | buf[0] = 0x02; |
1da177e4 LT |
246 | break; |
247 | ||
248 | case SAA6752HS_COMMAND_PAUSE: | |
4ac97914 | 249 | buf[0] = 0x04; |
1da177e4 LT |
250 | break; |
251 | ||
252 | case SAA6752HS_COMMAND_RECONFIGURE: | |
253 | buf[0] = 0x05; | |
254 | break; | |
255 | ||
4ac97914 MCC |
256 | case SAA6752HS_COMMAND_SLEEP: |
257 | buf[0] = 0x06; | |
1da177e4 LT |
258 | break; |
259 | ||
4ac97914 | 260 | case SAA6752HS_COMMAND_RECONFIGURE_FORCE: |
1da177e4 LT |
261 | buf[0] = 0x07; |
262 | break; | |
263 | ||
264 | default: | |
265 | return -EINVAL; | |
266 | } | |
267 | ||
4ac97914 | 268 | /* set it and wait for it to be so */ |
1da177e4 LT |
269 | i2c_master_send(client, buf, 1); |
270 | timeout = jiffies + HZ * 3; | |
271 | for (;;) { | |
9b71521b | 272 | /* get the current status */ |
1da177e4 | 273 | buf[0] = 0x10; |
4ac97914 | 274 | i2c_master_send(client, buf, 1); |
1da177e4 LT |
275 | 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 | ||
1da177e4 LT |
284 | msleep(10); |
285 | } | |
286 | ||
9b71521b | 287 | /* delay a bit to let encoder settle */ |
1da177e4 LT |
288 | msleep(50); |
289 | ||
4ac97914 | 290 | return status; |
1da177e4 LT |
291 | } |
292 | ||
293 | ||
ff5f26b4 HV |
294 | static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val) |
295 | { | |
296 | u8 buf[2]; | |
297 | ||
298 | buf[0] = reg; | |
299 | buf[1] = val; | |
300 | i2c_master_send(client, buf, 2); | |
301 | } | |
302 | ||
303 | static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val) | |
304 | { | |
305 | u8 buf[3]; | |
306 | ||
307 | buf[0] = reg; | |
308 | buf[1] = val >> 8; | |
309 | buf[2] = val & 0xff; | |
310 | i2c_master_send(client, buf, 3); | |
311 | } | |
312 | ||
313 | static int saa6752hs_set_bitrate(struct i2c_client *client, | |
e281db58 | 314 | struct saa6752hs_state *h) |
1da177e4 | 315 | { |
e281db58 | 316 | struct saa6752hs_mpeg_params *params = &h->params; |
b57e5578 | 317 | int tot_bitrate; |
ff5f26b4 | 318 | int is_384k; |
1da177e4 | 319 | |
9b71521b | 320 | /* set the bitrate mode */ |
ff5f26b4 HV |
321 | set_reg8(client, 0x71, |
322 | params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR); | |
1da177e4 | 323 | |
9b71521b | 324 | /* set the video bitrate */ |
b57e5578 | 325 | if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) { |
9b71521b | 326 | /* set the target bitrate */ |
ff5f26b4 | 327 | set_reg16(client, 0x80, params->vi_bitrate); |
1da177e4 | 328 | |
9b71521b | 329 | /* set the max bitrate */ |
ff5f26b4 | 330 | set_reg16(client, 0x81, params->vi_bitrate_peak); |
b57e5578 | 331 | tot_bitrate = params->vi_bitrate_peak; |
1da177e4 | 332 | } else { |
9b71521b | 333 | /* set the target bitrate (no max bitrate for CBR) */ |
ff5f26b4 | 334 | set_reg16(client, 0x81, params->vi_bitrate); |
b57e5578 | 335 | tot_bitrate = params->vi_bitrate; |
1da177e4 LT |
336 | } |
337 | ||
e281db58 | 338 | /* set the audio encoding */ |
ff5f26b4 HV |
339 | set_reg8(client, 0x93, |
340 | params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3); | |
e281db58 | 341 | |
9b71521b | 342 | /* set the audio bitrate */ |
e281db58 | 343 | if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) |
ff5f26b4 | 344 | is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate; |
e281db58 | 345 | else |
ff5f26b4 HV |
346 | is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate; |
347 | set_reg8(client, 0x94, is_384k); | |
348 | tot_bitrate += is_384k ? 384 : 256; | |
b57e5578 MCC |
349 | |
350 | /* Note: the total max bitrate is determined by adding the video and audio | |
351 | bitrates together and also adding an extra 768kbit/s to stay on the | |
352 | safe side. If more control should be required, then an extra MPEG control | |
353 | should be added. */ | |
354 | tot_bitrate += 768; | |
355 | if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX) | |
356 | tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX; | |
1da177e4 | 357 | |
9b71521b | 358 | /* set the total bitrate */ |
ff5f26b4 | 359 | set_reg16(client, 0xb1, tot_bitrate); |
1da177e4 LT |
360 | return 0; |
361 | } | |
362 | ||
0a4c9c93 | 363 | |
5b73e98c HV |
364 | static int get_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params, |
365 | struct v4l2_ext_control *ctrl) | |
366 | { | |
367 | switch (ctrl->id) { | |
368 | case V4L2_CID_MPEG_STREAM_TYPE: | |
369 | ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS; | |
370 | break; | |
371 | case V4L2_CID_MPEG_STREAM_PID_PMT: | |
372 | ctrl->value = params->ts_pid_pmt; | |
373 | break; | |
374 | case V4L2_CID_MPEG_STREAM_PID_AUDIO: | |
375 | ctrl->value = params->ts_pid_audio; | |
376 | break; | |
377 | case V4L2_CID_MPEG_STREAM_PID_VIDEO: | |
378 | ctrl->value = params->ts_pid_video; | |
379 | break; | |
380 | case V4L2_CID_MPEG_STREAM_PID_PCR: | |
381 | ctrl->value = params->ts_pid_pcr; | |
382 | break; | |
383 | case V4L2_CID_MPEG_AUDIO_ENCODING: | |
384 | ctrl->value = params->au_encoding; | |
385 | break; | |
386 | case V4L2_CID_MPEG_AUDIO_L2_BITRATE: | |
387 | ctrl->value = params->au_l2_bitrate; | |
388 | break; | |
389 | case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: | |
390 | if (!has_ac3) | |
391 | return -EINVAL; | |
392 | ctrl->value = params->au_ac3_bitrate; | |
393 | break; | |
394 | case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: | |
395 | ctrl->value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000; | |
396 | break; | |
397 | case V4L2_CID_MPEG_VIDEO_ENCODING: | |
398 | ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2; | |
399 | break; | |
400 | case V4L2_CID_MPEG_VIDEO_ASPECT: | |
401 | ctrl->value = params->vi_aspect; | |
402 | break; | |
403 | case V4L2_CID_MPEG_VIDEO_BITRATE: | |
404 | ctrl->value = params->vi_bitrate * 1000; | |
405 | break; | |
406 | case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: | |
407 | ctrl->value = params->vi_bitrate_peak * 1000; | |
408 | break; | |
409 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: | |
410 | ctrl->value = params->vi_bitrate_mode; | |
411 | break; | |
412 | default: | |
413 | return -EINVAL; | |
0a4c9c93 | 414 | } |
5b73e98c | 415 | return 0; |
0a4c9c93 FC |
416 | } |
417 | ||
e281db58 | 418 | static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params, |
5b73e98c | 419 | struct v4l2_ext_control *ctrl, int set) |
86b79d66 HV |
420 | { |
421 | int old = 0, new; | |
86b79d66 HV |
422 | |
423 | new = ctrl->value; | |
424 | switch (ctrl->id) { | |
5b73e98c HV |
425 | case V4L2_CID_MPEG_STREAM_TYPE: |
426 | old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS; | |
427 | if (set && new != old) | |
428 | return -ERANGE; | |
429 | new = old; | |
430 | break; | |
431 | case V4L2_CID_MPEG_STREAM_PID_PMT: | |
432 | old = params->ts_pid_pmt; | |
433 | if (set && new > MPEG_PID_MAX) | |
434 | return -ERANGE; | |
435 | if (new > MPEG_PID_MAX) | |
436 | new = MPEG_PID_MAX; | |
437 | params->ts_pid_pmt = new; | |
438 | break; | |
439 | case V4L2_CID_MPEG_STREAM_PID_AUDIO: | |
440 | old = params->ts_pid_audio; | |
441 | if (set && new > MPEG_PID_MAX) | |
442 | return -ERANGE; | |
443 | if (new > MPEG_PID_MAX) | |
444 | new = MPEG_PID_MAX; | |
445 | params->ts_pid_audio = new; | |
446 | break; | |
447 | case V4L2_CID_MPEG_STREAM_PID_VIDEO: | |
448 | old = params->ts_pid_video; | |
449 | if (set && new > MPEG_PID_MAX) | |
450 | return -ERANGE; | |
451 | if (new > MPEG_PID_MAX) | |
452 | new = MPEG_PID_MAX; | |
453 | params->ts_pid_video = new; | |
454 | break; | |
455 | case V4L2_CID_MPEG_STREAM_PID_PCR: | |
456 | old = params->ts_pid_pcr; | |
457 | if (set && new > MPEG_PID_MAX) | |
458 | return -ERANGE; | |
459 | if (new > MPEG_PID_MAX) | |
460 | new = MPEG_PID_MAX; | |
461 | params->ts_pid_pcr = new; | |
462 | break; | |
463 | case V4L2_CID_MPEG_AUDIO_ENCODING: | |
464 | old = params->au_encoding; | |
465 | if (set && new != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 && | |
466 | (!has_ac3 || new != V4L2_MPEG_AUDIO_ENCODING_AC3)) | |
467 | return -ERANGE; | |
7ae26889 | 468 | params->au_encoding = new; |
5b73e98c HV |
469 | break; |
470 | case V4L2_CID_MPEG_AUDIO_L2_BITRATE: | |
471 | old = params->au_l2_bitrate; | |
472 | if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K && | |
473 | new != V4L2_MPEG_AUDIO_L2_BITRATE_384K) | |
474 | return -ERANGE; | |
475 | if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K) | |
476 | new = V4L2_MPEG_AUDIO_L2_BITRATE_256K; | |
477 | else | |
478 | new = V4L2_MPEG_AUDIO_L2_BITRATE_384K; | |
479 | params->au_l2_bitrate = new; | |
480 | break; | |
481 | case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: | |
482 | if (!has_ac3) | |
86b79d66 | 483 | return -EINVAL; |
5b73e98c HV |
484 | old = params->au_ac3_bitrate; |
485 | if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K && | |
486 | new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K) | |
487 | return -ERANGE; | |
488 | if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K) | |
489 | new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K; | |
490 | else | |
491 | new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K; | |
492 | params->au_ac3_bitrate = new; | |
493 | break; | |
494 | case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: | |
495 | old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000; | |
496 | if (set && new != old) | |
497 | return -ERANGE; | |
498 | new = old; | |
499 | break; | |
500 | case V4L2_CID_MPEG_VIDEO_ENCODING: | |
501 | old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2; | |
502 | if (set && new != old) | |
503 | return -ERANGE; | |
504 | new = old; | |
505 | break; | |
506 | case V4L2_CID_MPEG_VIDEO_ASPECT: | |
507 | old = params->vi_aspect; | |
508 | if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 && | |
509 | new != V4L2_MPEG_VIDEO_ASPECT_4x3) | |
510 | return -ERANGE; | |
511 | if (new != V4L2_MPEG_VIDEO_ASPECT_16x9) | |
512 | new = V4L2_MPEG_VIDEO_ASPECT_4x3; | |
513 | params->vi_aspect = new; | |
514 | break; | |
515 | case V4L2_CID_MPEG_VIDEO_BITRATE: | |
516 | old = params->vi_bitrate * 1000; | |
517 | new = 1000 * (new / 1000); | |
518 | if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000) | |
519 | return -ERANGE; | |
520 | if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000) | |
521 | new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000; | |
522 | params->vi_bitrate = new / 1000; | |
523 | break; | |
524 | case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: | |
525 | old = params->vi_bitrate_peak * 1000; | |
526 | new = 1000 * (new / 1000); | |
527 | if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000) | |
528 | return -ERANGE; | |
529 | if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000) | |
530 | new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000; | |
531 | params->vi_bitrate_peak = new / 1000; | |
532 | break; | |
533 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: | |
534 | old = params->vi_bitrate_mode; | |
535 | params->vi_bitrate_mode = new; | |
536 | break; | |
537 | default: | |
538 | return -EINVAL; | |
86b79d66 | 539 | } |
5b73e98c | 540 | ctrl->value = new; |
86b79d66 | 541 | return 0; |
1da177e4 LT |
542 | } |
543 | ||
5b73e98c HV |
544 | |
545 | static int saa6752hs_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl) | |
8b53b39d | 546 | { |
5b73e98c | 547 | struct saa6752hs_state *h = to_state(sd); |
e281db58 | 548 | struct saa6752hs_mpeg_params *params = &h->params; |
8b53b39d HV |
549 | int err; |
550 | ||
551 | switch (qctrl->id) { | |
552 | case V4L2_CID_MPEG_AUDIO_ENCODING: | |
553 | return v4l2_ctrl_query_fill(qctrl, | |
554 | V4L2_MPEG_AUDIO_ENCODING_LAYER_2, | |
e281db58 HV |
555 | h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : |
556 | V4L2_MPEG_AUDIO_ENCODING_LAYER_2, | |
557 | 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2); | |
8b53b39d HV |
558 | |
559 | case V4L2_CID_MPEG_AUDIO_L2_BITRATE: | |
560 | return v4l2_ctrl_query_fill(qctrl, | |
561 | V4L2_MPEG_AUDIO_L2_BITRATE_256K, | |
562 | V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1, | |
563 | V4L2_MPEG_AUDIO_L2_BITRATE_256K); | |
564 | ||
e281db58 HV |
565 | case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: |
566 | if (!h->has_ac3) | |
567 | return -EINVAL; | |
568 | return v4l2_ctrl_query_fill(qctrl, | |
569 | V4L2_MPEG_AUDIO_AC3_BITRATE_256K, | |
570 | V4L2_MPEG_AUDIO_AC3_BITRATE_384K, 1, | |
571 | V4L2_MPEG_AUDIO_AC3_BITRATE_256K); | |
572 | ||
8b53b39d HV |
573 | case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: |
574 | return v4l2_ctrl_query_fill(qctrl, | |
575 | V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, | |
576 | V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, 1, | |
577 | V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000); | |
578 | ||
579 | case V4L2_CID_MPEG_VIDEO_ENCODING: | |
580 | return v4l2_ctrl_query_fill(qctrl, | |
581 | V4L2_MPEG_VIDEO_ENCODING_MPEG_2, | |
582 | V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1, | |
583 | V4L2_MPEG_VIDEO_ENCODING_MPEG_2); | |
584 | ||
585 | case V4L2_CID_MPEG_VIDEO_ASPECT: | |
586 | return v4l2_ctrl_query_fill(qctrl, | |
587 | V4L2_MPEG_VIDEO_ASPECT_4x3, | |
588 | V4L2_MPEG_VIDEO_ASPECT_16x9, 1, | |
589 | V4L2_MPEG_VIDEO_ASPECT_4x3); | |
590 | ||
591 | case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: | |
10afbef1 | 592 | err = v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000); |
8b53b39d HV |
593 | if (err == 0 && |
594 | params->vi_bitrate_mode == | |
595 | V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) | |
596 | qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; | |
597 | return err; | |
598 | ||
599 | case V4L2_CID_MPEG_STREAM_TYPE: | |
600 | return v4l2_ctrl_query_fill(qctrl, | |
601 | V4L2_MPEG_STREAM_TYPE_MPEG2_TS, | |
602 | V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1, | |
603 | V4L2_MPEG_STREAM_TYPE_MPEG2_TS); | |
604 | ||
605 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: | |
10afbef1 HV |
606 | return v4l2_ctrl_query_fill(qctrl, |
607 | V4L2_MPEG_VIDEO_BITRATE_MODE_VBR, | |
608 | V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1, | |
609 | V4L2_MPEG_VIDEO_BITRATE_MODE_VBR); | |
8b53b39d | 610 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
10afbef1 | 611 | return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000); |
8b53b39d | 612 | case V4L2_CID_MPEG_STREAM_PID_PMT: |
10afbef1 | 613 | return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16); |
8b53b39d | 614 | case V4L2_CID_MPEG_STREAM_PID_AUDIO: |
10afbef1 | 615 | return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260); |
8b53b39d | 616 | case V4L2_CID_MPEG_STREAM_PID_VIDEO: |
10afbef1 | 617 | return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256); |
8b53b39d | 618 | case V4L2_CID_MPEG_STREAM_PID_PCR: |
10afbef1 | 619 | return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259); |
8b53b39d HV |
620 | |
621 | default: | |
622 | break; | |
623 | } | |
624 | return -EINVAL; | |
625 | } | |
626 | ||
5b73e98c | 627 | static int saa6752hs_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qmenu) |
8b53b39d | 628 | { |
e281db58 HV |
629 | static const u32 mpeg_audio_encoding[] = { |
630 | V4L2_MPEG_AUDIO_ENCODING_LAYER_2, | |
631 | V4L2_CTRL_MENU_IDS_END | |
632 | }; | |
633 | static const u32 mpeg_audio_ac3_encoding[] = { | |
634 | V4L2_MPEG_AUDIO_ENCODING_LAYER_2, | |
635 | V4L2_MPEG_AUDIO_ENCODING_AC3, | |
636 | V4L2_CTRL_MENU_IDS_END | |
637 | }; | |
638 | static u32 mpeg_audio_l2_bitrate[] = { | |
639 | V4L2_MPEG_AUDIO_L2_BITRATE_256K, | |
640 | V4L2_MPEG_AUDIO_L2_BITRATE_384K, | |
641 | V4L2_CTRL_MENU_IDS_END | |
642 | }; | |
643 | static u32 mpeg_audio_ac3_bitrate[] = { | |
644 | V4L2_MPEG_AUDIO_AC3_BITRATE_256K, | |
645 | V4L2_MPEG_AUDIO_AC3_BITRATE_384K, | |
646 | V4L2_CTRL_MENU_IDS_END | |
8b53b39d | 647 | }; |
5b73e98c | 648 | struct saa6752hs_state *h = to_state(sd); |
8b53b39d HV |
649 | struct v4l2_queryctrl qctrl; |
650 | int err; | |
651 | ||
652 | qctrl.id = qmenu->id; | |
5b73e98c | 653 | err = saa6752hs_queryctrl(sd, &qctrl); |
8b53b39d HV |
654 | if (err) |
655 | return err; | |
e281db58 HV |
656 | switch (qmenu->id) { |
657 | case V4L2_CID_MPEG_AUDIO_L2_BITRATE: | |
658 | return v4l2_ctrl_query_menu_valid_items(qmenu, | |
8b53b39d | 659 | mpeg_audio_l2_bitrate); |
e281db58 HV |
660 | case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: |
661 | if (!h->has_ac3) | |
662 | return -EINVAL; | |
663 | return v4l2_ctrl_query_menu_valid_items(qmenu, | |
664 | mpeg_audio_ac3_bitrate); | |
665 | case V4L2_CID_MPEG_AUDIO_ENCODING: | |
666 | return v4l2_ctrl_query_menu_valid_items(qmenu, | |
667 | h->has_ac3 ? mpeg_audio_ac3_encoding : | |
668 | mpeg_audio_encoding); | |
669 | } | |
670 | return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL); | |
8b53b39d HV |
671 | } |
672 | ||
5b73e98c | 673 | static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes) |
1da177e4 LT |
674 | { |
675 | unsigned char buf[9], buf2[4]; | |
5b73e98c HV |
676 | struct saa6752hs_state *h = to_state(sd); |
677 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
3eea543b | 678 | unsigned size; |
1da177e4 LT |
679 | u32 crc; |
680 | unsigned char localPAT[256]; | |
681 | unsigned char localPMT[256]; | |
682 | ||
9b71521b | 683 | /* Set video format - must be done first as it resets other settings */ |
ff5f26b4 | 684 | set_reg8(client, 0x41, h->video_format); |
1da177e4 | 685 | |
9b71521b | 686 | /* Set number of lines in input signal */ |
ff5f26b4 | 687 | set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0); |
9b71521b | 688 | |
4ac97914 | 689 | /* set bitrate */ |
e281db58 | 690 | saa6752hs_set_bitrate(client, h); |
1da177e4 | 691 | |
9b71521b | 692 | /* Set GOP structure {3, 13} */ |
ff5f26b4 | 693 | set_reg16(client, 0x72, 0x030d); |
1da177e4 | 694 | |
657de3cd | 695 | /* Set minimum Q-scale {4} */ |
ff5f26b4 | 696 | set_reg8(client, 0x82, 0x04); |
1da177e4 | 697 | |
657de3cd | 698 | /* Set maximum Q-scale {12} */ |
ff5f26b4 | 699 | set_reg8(client, 0x83, 0x0c); |
1da177e4 | 700 | |
657de3cd | 701 | /* Set Output Protocol */ |
ff5f26b4 | 702 | set_reg8(client, 0xd0, 0x81); |
1da177e4 | 703 | |
657de3cd | 704 | /* Set video output stream format {TS} */ |
ff5f26b4 | 705 | set_reg8(client, 0xb0, 0x05); |
1da177e4 | 706 | |
f03813e4 | 707 | /* Set leading null byte for TS */ |
ff5f26b4 | 708 | set_reg16(client, 0xf6, leading_null_bytes); |
f03813e4 | 709 | |
1da177e4 LT |
710 | /* compute PAT */ |
711 | memcpy(localPAT, PAT, sizeof(PAT)); | |
712 | localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f); | |
713 | localPAT[18] = h->params.ts_pid_pmt & 0xff; | |
714 | crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4); | |
715 | localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF; | |
716 | localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF; | |
717 | localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF; | |
718 | localPAT[sizeof(PAT) - 1] = crc & 0xFF; | |
719 | ||
720 | /* compute PMT */ | |
3eea543b HV |
721 | if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) { |
722 | size = sizeof(PMT_AC3); | |
723 | memcpy(localPMT, PMT_AC3, size); | |
724 | } else { | |
725 | size = sizeof(PMT); | |
726 | memcpy(localPMT, PMT, size); | |
727 | } | |
4ac97914 MCC |
728 | localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f); |
729 | localPMT[4] = h->params.ts_pid_pmt & 0xff; | |
1da177e4 LT |
730 | localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F); |
731 | localPMT[16] = h->params.ts_pid_pcr & 0xFF; | |
732 | localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F); | |
733 | localPMT[21] = h->params.ts_pid_video & 0xFF; | |
734 | localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F); | |
735 | localPMT[26] = h->params.ts_pid_audio & 0xFF; | |
3eea543b HV |
736 | crc = crc32_be(~0, &localPMT[7], size - 7 - 4); |
737 | localPMT[size - 4] = (crc >> 24) & 0xFF; | |
738 | localPMT[size - 3] = (crc >> 16) & 0xFF; | |
739 | localPMT[size - 2] = (crc >> 8) & 0xFF; | |
740 | localPMT[size - 1] = crc & 0xFF; | |
1da177e4 | 741 | |
657de3cd | 742 | /* Set Audio PID */ |
ff5f26b4 | 743 | set_reg16(client, 0xc1, h->params.ts_pid_audio); |
1da177e4 | 744 | |
9b71521b | 745 | /* Set Video PID */ |
ff5f26b4 | 746 | set_reg16(client, 0xc0, h->params.ts_pid_video); |
1da177e4 | 747 | |
4ac97914 | 748 | /* Set PCR PID */ |
ff5f26b4 | 749 | set_reg16(client, 0xc4, h->params.ts_pid_pcr); |
1da177e4 | 750 | |
9b71521b | 751 | /* Send SI tables */ |
3eea543b HV |
752 | i2c_master_send(client, localPAT, sizeof(PAT)); |
753 | i2c_master_send(client, localPMT, size); | |
1da177e4 | 754 | |
9b71521b | 755 | /* mute then unmute audio. This removes buzzing artefacts */ |
ff5f26b4 HV |
756 | set_reg8(client, 0xa4, 1); |
757 | set_reg8(client, 0xa4, 0); | |
1da177e4 | 758 | |
9b71521b | 759 | /* start it going */ |
1da177e4 LT |
760 | saa6752hs_chip_command(client, SAA6752HS_COMMAND_START); |
761 | ||
9b71521b | 762 | /* readout current state */ |
1da177e4 LT |
763 | buf[0] = 0xE1; |
764 | buf[1] = 0xA7; | |
765 | buf[2] = 0xFE; | |
766 | buf[3] = 0x82; | |
767 | buf[4] = 0xB0; | |
768 | i2c_master_send(client, buf, 5); | |
769 | i2c_master_recv(client, buf2, 4); | |
770 | ||
9b71521b | 771 | /* change aspect ratio */ |
1da177e4 LT |
772 | buf[0] = 0xE0; |
773 | buf[1] = 0xA7; | |
774 | buf[2] = 0xFE; | |
775 | buf[3] = 0x82; | |
776 | buf[4] = 0xB0; | |
777 | buf[5] = buf2[0]; | |
5b73e98c | 778 | switch (h->params.vi_aspect) { |
86b79d66 | 779 | case V4L2_MPEG_VIDEO_ASPECT_16x9: |
1da177e4 LT |
780 | buf[6] = buf2[1] | 0x40; |
781 | break; | |
86b79d66 | 782 | case V4L2_MPEG_VIDEO_ASPECT_4x3: |
1da177e4 LT |
783 | default: |
784 | buf[6] = buf2[1] & 0xBF; | |
785 | break; | |
1da177e4 LT |
786 | } |
787 | buf[7] = buf2[2]; | |
788 | buf[8] = buf2[3]; | |
789 | i2c_master_send(client, buf, 9); | |
790 | ||
1da177e4 LT |
791 | return 0; |
792 | } | |
793 | ||
5b73e98c | 794 | static int saa6752hs_do_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls, int set) |
1da177e4 | 795 | { |
5b73e98c | 796 | struct saa6752hs_state *h = to_state(sd); |
86b79d66 | 797 | struct saa6752hs_mpeg_params params; |
86b79d66 | 798 | int i; |
1da177e4 | 799 | |
5b73e98c HV |
800 | if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) |
801 | return -EINVAL; | |
802 | ||
803 | params = h->params; | |
804 | for (i = 0; i < ctrls->count; i++) { | |
805 | int err = handle_ctrl(h->has_ac3, ¶ms, ctrls->controls + i, set); | |
806 | ||
807 | if (err) { | |
808 | ctrls->error_idx = i; | |
809 | return err; | |
86b79d66 | 810 | } |
0a4c9c93 | 811 | } |
5b73e98c HV |
812 | if (set) |
813 | h->params = params; | |
814 | return 0; | |
815 | } | |
0a4c9c93 | 816 | |
5b73e98c HV |
817 | static int saa6752hs_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls) |
818 | { | |
819 | return saa6752hs_do_ext_ctrls(sd, ctrls, 1); | |
820 | } | |
821 | ||
822 | static int saa6752hs_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls) | |
823 | { | |
824 | return saa6752hs_do_ext_ctrls(sd, ctrls, 0); | |
825 | } | |
826 | ||
827 | static int saa6752hs_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls) | |
828 | { | |
829 | struct saa6752hs_state *h = to_state(sd); | |
830 | int i; | |
831 | ||
832 | if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) | |
833 | return -EINVAL; | |
834 | ||
835 | for (i = 0; i < ctrls->count; i++) { | |
836 | int err = get_ctrl(h->has_ac3, &h->params, ctrls->controls + i); | |
837 | ||
838 | if (err) { | |
839 | ctrls->error_idx = i; | |
840 | return err; | |
841 | } | |
0a4c9c93 | 842 | } |
5b73e98c HV |
843 | return 0; |
844 | } | |
e281db58 | 845 | |
51623ef9 | 846 | static int saa6752hs_g_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f) |
5b73e98c HV |
847 | { |
848 | struct saa6752hs_state *h = to_state(sd); | |
e281db58 | 849 | |
5b73e98c HV |
850 | if (h->video_format == SAA6752HS_VF_UNKNOWN) |
851 | h->video_format = SAA6752HS_VF_D1; | |
51623ef9 HV |
852 | f->width = v4l2_format_table[h->video_format].fmt.pix.width; |
853 | f->height = v4l2_format_table[h->video_format].fmt.pix.height; | |
854 | f->code = V4L2_MBUS_FMT_FIXED; | |
855 | f->field = V4L2_FIELD_INTERLACED; | |
856 | f->colorspace = V4L2_COLORSPACE_SMPTE170M; | |
5b73e98c HV |
857 | return 0; |
858 | } | |
859 | ||
51623ef9 | 860 | static int saa6752hs_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f) |
5b73e98c HV |
861 | { |
862 | struct saa6752hs_state *h = to_state(sd); | |
863 | int dist_352, dist_480, dist_720; | |
864 | ||
51623ef9 HV |
865 | if (f->code != V4L2_MBUS_FMT_FIXED) |
866 | return -EINVAL; | |
867 | ||
5b73e98c HV |
868 | /* |
869 | FIXME: translate and round width/height into EMPRESS | |
870 | subsample type: | |
871 | ||
872 | type | PAL | NTSC | |
873 | --------------------------- | |
874 | SIF | 352x288 | 352x240 | |
875 | 1/2 D1 | 352x576 | 352x480 | |
876 | 2/3 D1 | 480x576 | 480x480 | |
877 | D1 | 720x576 | 720x480 | |
878 | */ | |
879 | ||
51623ef9 HV |
880 | dist_352 = abs(f->width - 352); |
881 | dist_480 = abs(f->width - 480); | |
882 | dist_720 = abs(f->width - 720); | |
5b73e98c | 883 | if (dist_720 < dist_480) { |
51623ef9 HV |
884 | f->width = 720; |
885 | f->height = 576; | |
5b73e98c HV |
886 | h->video_format = SAA6752HS_VF_D1; |
887 | } else if (dist_480 < dist_352) { | |
51623ef9 HV |
888 | f->width = 480; |
889 | f->height = 576; | |
5b73e98c HV |
890 | h->video_format = SAA6752HS_VF_2_3_D1; |
891 | } else { | |
51623ef9 HV |
892 | f->width = 352; |
893 | if (abs(f->height - 576) < | |
894 | abs(f->height - 288)) { | |
895 | f->height = 576; | |
5b73e98c HV |
896 | h->video_format = SAA6752HS_VF_1_2_D1; |
897 | } else { | |
51623ef9 | 898 | f->height = 288; |
5b73e98c HV |
899 | h->video_format = SAA6752HS_VF_SIF; |
900 | } | |
1da177e4 | 901 | } |
51623ef9 HV |
902 | f->field = V4L2_FIELD_INTERLACED; |
903 | f->colorspace = V4L2_COLORSPACE_SMPTE170M; | |
5b73e98c HV |
904 | return 0; |
905 | } | |
906 | ||
907 | static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std) | |
908 | { | |
909 | struct saa6752hs_state *h = to_state(sd); | |
910 | ||
911 | h->standard = std; | |
912 | return 0; | |
913 | } | |
914 | ||
5b73e98c HV |
915 | /* ----------------------------------------------------------------------- */ |
916 | ||
917 | static const struct v4l2_subdev_core_ops saa6752hs_core_ops = { | |
5b73e98c HV |
918 | .init = saa6752hs_init, |
919 | .queryctrl = saa6752hs_queryctrl, | |
920 | .querymenu = saa6752hs_querymenu, | |
921 | .g_ext_ctrls = saa6752hs_g_ext_ctrls, | |
922 | .s_ext_ctrls = saa6752hs_s_ext_ctrls, | |
923 | .try_ext_ctrls = saa6752hs_try_ext_ctrls, | |
5b73e98c HV |
924 | .s_std = saa6752hs_s_std, |
925 | }; | |
926 | ||
927 | static const struct v4l2_subdev_video_ops saa6752hs_video_ops = { | |
51623ef9 HV |
928 | .s_mbus_fmt = saa6752hs_s_mbus_fmt, |
929 | .g_mbus_fmt = saa6752hs_g_mbus_fmt, | |
5b73e98c HV |
930 | }; |
931 | ||
932 | static const struct v4l2_subdev_ops saa6752hs_ops = { | |
933 | .core = &saa6752hs_core_ops, | |
5b73e98c HV |
934 | .video = &saa6752hs_video_ops, |
935 | }; | |
936 | ||
e281db58 | 937 | static int saa6752hs_probe(struct i2c_client *client, |
5b73e98c | 938 | const struct i2c_device_id *id) |
e281db58 HV |
939 | { |
940 | struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL); | |
5b73e98c | 941 | struct v4l2_subdev *sd; |
e281db58 HV |
942 | u8 addr = 0x13; |
943 | u8 data[12]; | |
1da177e4 | 944 | |
e281db58 HV |
945 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
946 | client->addr << 1, client->adapter->name); | |
947 | if (h == NULL) | |
948 | return -ENOMEM; | |
5b73e98c HV |
949 | sd = &h->sd; |
950 | v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops); | |
1da177e4 | 951 | |
e281db58 HV |
952 | i2c_master_send(client, &addr, 1); |
953 | i2c_master_recv(client, data, sizeof(data)); | |
e281db58 HV |
954 | h->revision = (data[8] << 8) | data[9]; |
955 | h->has_ac3 = 0; | |
956 | if (h->revision == 0x0206) { | |
e281db58 HV |
957 | h->has_ac3 = 1; |
958 | v4l_info(client, "support AC-3\n"); | |
959 | } | |
960 | h->params = param_defaults; | |
961 | h->standard = 0; /* Assume 625 input lines */ | |
e281db58 | 962 | return 0; |
1da177e4 LT |
963 | } |
964 | ||
e281db58 | 965 | static int saa6752hs_remove(struct i2c_client *client) |
1da177e4 | 966 | { |
5b73e98c HV |
967 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
968 | ||
969 | v4l2_device_unregister_subdev(sd); | |
970 | kfree(to_state(sd)); | |
e281db58 | 971 | return 0; |
1da177e4 LT |
972 | } |
973 | ||
e281db58 HV |
974 | static const struct i2c_device_id saa6752hs_id[] = { |
975 | { "saa6752hs", 0 }, | |
976 | { } | |
977 | }; | |
978 | MODULE_DEVICE_TABLE(i2c, saa6752hs_id); | |
979 | ||
099bd5e3 HV |
980 | static struct i2c_driver saa6752hs_driver = { |
981 | .driver = { | |
982 | .owner = THIS_MODULE, | |
983 | .name = "saa6752hs", | |
984 | }, | |
985 | .probe = saa6752hs_probe, | |
986 | .remove = saa6752hs_remove, | |
987 | .id_table = saa6752hs_id, | |
e281db58 | 988 | }; |
1da177e4 | 989 | |
c6e8d86f | 990 | module_i2c_driver(saa6752hs_driver); |
099bd5e3 | 991 | |
1da177e4 LT |
992 | /* |
993 | * Overrides for Emacs so that we follow Linus's tabbing style. | |
994 | * --------------------------------------------------------------------------- | |
995 | * Local variables: | |
996 | * c-basic-offset: 8 | |
997 | * End: | |
998 | */ |