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