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