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> |
Hans Verkuil | fac6986 | 2009-01-17 12:17:14 -0300 | [diff] [blame] | 39 | #include <media/v4l2-i2c-drv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/init.h> |
| 41 | #include <linux/crc32.h> |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder"); |
| 50 | MODULE_AUTHOR("Andrew de Quincey"); |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 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 | |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 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 */ |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 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 | |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 80 | static const struct v4l2_format v4l2_format_table[] = |
| 81 | { |
Mauro Carvalho Chehab | ac19ecc | 2005-06-23 22:05:09 -0700 | [diff] [blame] | 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}}}, |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | struct saa6752hs_state { |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 95 | struct v4l2_subdev sd; |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 96 | int chip; |
| 97 | u32 revision; |
| 98 | int has_ac3; |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 99 | struct saa6752hs_mpeg_params params; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 100 | enum saa6752hs_videoformat video_format; |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 101 | v4l2_std_id standard; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | enum saa6752hs_command { |
| 105 | SAA6752HS_COMMAND_RESET = 0, |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6, |
| 112 | |
| 113 | SAA6752HS_COMMAND_MAX |
| 114 | }; |
| 115 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 116 | static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd) |
| 117 | { |
| 118 | return container_of(sd, struct saa6752hs_state, sd); |
| 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* ---------------------------------------------------------------------- */ |
| 122 | |
| 123 | static u8 PAT[] = { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 124 | 0xc2, /* i2c register */ |
| 125 | 0x00, /* table number for encoder */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 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) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 131 | 0x00, /* PSI pointer to start of table */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 133 | 0x00, /* tid(0) */ |
| 134 | 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 136 | 0x00, 0x01, /* transport_stream_id(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 138 | 0xc1, /* version_number(0), current_next_indicator(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 140 | 0x00, 0x00, /* section_number(0), last_section_number(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 142 | 0x00, 0x01, /* program_number(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 144 | 0xe0, 0x00, /* PMT PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 146 | 0x00, 0x00, 0x00, 0x00 /* CRC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | static u8 PMT[] = { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 150 | 0xc2, /* i2c register */ |
| 151 | 0x01, /* table number for encoder */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 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) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 157 | 0x00, /* PSI pointer to start of table */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 159 | 0x02, /* tid(2) */ |
| 160 | 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 162 | 0x00, 0x01, /* program_number(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 164 | 0xc1, /* version_number(0), current_next_indicator(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 166 | 0x00, 0x00, /* section_number(0), last_section_number(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 168 | 0xe0, 0x00, /* PCR_PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 170 | 0xf0, 0x00, /* program_info_length(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 172 | 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */ |
| 173 | 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 175 | 0x00, 0x00, 0x00, 0x00 /* CRC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Hans Verkuil | 3eea543 | 2008-08-08 13:27:16 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 223 | .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2, |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 224 | .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K, |
Hans Verkuil | 3eea543 | 2008-08-08 13:27:16 -0300 | [diff] [blame] | 225 | .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K, |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 226 | }; |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* ---------------------------------------------------------------------- */ |
| 229 | |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 230 | static int saa6752hs_chip_command(struct i2c_client *client, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | enum saa6752hs_command command) |
| 232 | { |
| 233 | unsigned char buf[3]; |
| 234 | unsigned long timeout; |
| 235 | int status = 0; |
| 236 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 237 | /* execute the command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | switch(command) { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 239 | case SAA6752HS_COMMAND_RESET: |
| 240 | buf[0] = 0x00; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | case SAA6752HS_COMMAND_STOP: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 244 | buf[0] = 0x03; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | case SAA6752HS_COMMAND_START: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 248 | buf[0] = 0x02; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | break; |
| 250 | |
| 251 | case SAA6752HS_COMMAND_PAUSE: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 252 | buf[0] = 0x04; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | break; |
| 254 | |
| 255 | case SAA6752HS_COMMAND_RECONFIGURE: |
| 256 | buf[0] = 0x05; |
| 257 | break; |
| 258 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 259 | case SAA6752HS_COMMAND_SLEEP: |
| 260 | buf[0] = 0x06; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | break; |
| 262 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 263 | case SAA6752HS_COMMAND_RECONFIGURE_FORCE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | buf[0] = 0x07; |
| 265 | break; |
| 266 | |
| 267 | default: |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 271 | /* set it and wait for it to be so */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | i2c_master_send(client, buf, 1); |
| 273 | timeout = jiffies + HZ * 3; |
| 274 | for (;;) { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 275 | /* get the current status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | buf[0] = 0x10; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 277 | i2c_master_send(client, buf, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | msleep(10); |
| 288 | } |
| 289 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 290 | /* delay a bit to let encoder settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | msleep(50); |
| 292 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 293 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 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, |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 317 | struct saa6752hs_state *h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 319 | struct saa6752hs_mpeg_params *params = &h->params; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 320 | int tot_bitrate; |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 321 | int is_384k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 323 | /* set the bitrate mode */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 324 | set_reg8(client, 0x71, |
| 325 | params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 327 | /* set the video bitrate */ |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 328 | if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 329 | /* set the target bitrate */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 330 | set_reg16(client, 0x80, params->vi_bitrate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 332 | /* set the max bitrate */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 333 | set_reg16(client, 0x81, params->vi_bitrate_peak); |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 334 | tot_bitrate = params->vi_bitrate_peak; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } else { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 336 | /* set the target bitrate (no max bitrate for CBR) */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 337 | set_reg16(client, 0x81, params->vi_bitrate); |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 338 | tot_bitrate = params->vi_bitrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 341 | /* set the audio encoding */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 342 | set_reg8(client, 0x93, |
| 343 | params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3); |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 344 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 345 | /* set the audio bitrate */ |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 346 | if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 347 | is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate; |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 348 | else |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 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; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 361 | /* set the total bitrate */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 362 | set_reg16(client, 0xb1, tot_bitrate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 366 | |
| 367 | static int get_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params, |
| 368 | struct v4l2_ext_control *ctrl) |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 369 | { |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 370 | switch (ctrl->id) { |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 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) |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 394 | return -EINVAL; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 417 | } |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 418 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 421 | static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params, |
| 422 | struct v4l2_ext_control *ctrl, int set) |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 423 | { |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 424 | int old = 0, new; |
| 425 | |
| 426 | new = ctrl->value; |
| 427 | switch (ctrl->id) { |
| 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; |
Dmitri Belimov | 7ae2688 | 2009-08-10 04:48:16 -0300 | [diff] [blame] | 471 | params->au_encoding = new; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 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) |
| 486 | return -EINVAL; |
| 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; |
| 542 | } |
| 543 | ctrl->value = new; |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | static int saa6752hs_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl) |
| 549 | { |
| 550 | struct saa6752hs_state *h = to_state(sd); |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 551 | struct saa6752hs_mpeg_params *params = &h->params; |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 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, |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 558 | h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : |
| 559 | V4L2_MPEG_AUDIO_ENCODING_LAYER_2, |
| 560 | 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 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: |
Hans Verkuil | 10afbef | 2009-02-21 18:47:24 -0300 | [diff] [blame] | 595 | err = v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 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: |
Hans Verkuil | 10afbef | 2009-02-21 18:47:24 -0300 | [diff] [blame] | 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); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 613 | case V4L2_CID_MPEG_VIDEO_BITRATE: |
Hans Verkuil | 10afbef | 2009-02-21 18:47:24 -0300 | [diff] [blame] | 614 | return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 615 | case V4L2_CID_MPEG_STREAM_PID_PMT: |
Hans Verkuil | 10afbef | 2009-02-21 18:47:24 -0300 | [diff] [blame] | 616 | return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 617 | case V4L2_CID_MPEG_STREAM_PID_AUDIO: |
Hans Verkuil | 10afbef | 2009-02-21 18:47:24 -0300 | [diff] [blame] | 618 | return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 619 | case V4L2_CID_MPEG_STREAM_PID_VIDEO: |
Hans Verkuil | 10afbef | 2009-02-21 18:47:24 -0300 | [diff] [blame] | 620 | return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 621 | case V4L2_CID_MPEG_STREAM_PID_PCR: |
Hans Verkuil | 10afbef | 2009-02-21 18:47:24 -0300 | [diff] [blame] | 622 | return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 623 | |
| 624 | default: |
| 625 | break; |
| 626 | } |
| 627 | return -EINVAL; |
| 628 | } |
| 629 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 630 | static int saa6752hs_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qmenu) |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 631 | { |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 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 |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 650 | }; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 651 | struct saa6752hs_state *h = to_state(sd); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 652 | struct v4l2_queryctrl qctrl; |
| 653 | int err; |
| 654 | |
| 655 | qctrl.id = qmenu->id; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 656 | err = saa6752hs_queryctrl(sd, &qctrl); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 657 | if (err) |
| 658 | return err; |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 659 | switch (qmenu->id) { |
| 660 | case V4L2_CID_MPEG_AUDIO_L2_BITRATE: |
| 661 | return v4l2_ctrl_query_menu_valid_items(qmenu, |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 662 | mpeg_audio_l2_bitrate); |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 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); |
Hans Verkuil | 8b53b39 | 2008-06-27 21:18:15 -0300 | [diff] [blame] | 674 | } |
| 675 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 676 | static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
| 678 | unsigned char buf[9], buf2[4]; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 679 | struct saa6752hs_state *h = to_state(sd); |
| 680 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
Hans Verkuil | 3eea543 | 2008-08-08 13:27:16 -0300 | [diff] [blame] | 681 | unsigned size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | u32 crc; |
| 683 | unsigned char localPAT[256]; |
| 684 | unsigned char localPMT[256]; |
| 685 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 686 | /* Set video format - must be done first as it resets other settings */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 687 | set_reg8(client, 0x41, h->video_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 689 | /* Set number of lines in input signal */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 690 | 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] | 691 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 692 | /* set bitrate */ |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 693 | saa6752hs_set_bitrate(client, h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 695 | /* Set GOP structure {3, 13} */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 696 | set_reg16(client, 0x72, 0x030d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 698 | /* Set minimum Q-scale {4} */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 699 | set_reg8(client, 0x82, 0x04); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 701 | /* Set maximum Q-scale {12} */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 702 | set_reg8(client, 0x83, 0x0c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 704 | /* Set Output Protocol */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 705 | set_reg8(client, 0xd0, 0x81); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 707 | /* Set video output stream format {TS} */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 708 | set_reg8(client, 0xb0, 0x05); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
Dmitry Belimov | f03813e | 2008-08-26 13:44:40 -0300 | [diff] [blame] | 710 | /* Set leading null byte for TS */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 711 | set_reg16(client, 0xf6, leading_null_bytes); |
Dmitry Belimov | f03813e | 2008-08-26 13:44:40 -0300 | [diff] [blame] | 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Hans Verkuil | 3eea543 | 2008-08-08 13:27:16 -0300 | [diff] [blame] | 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 | } |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 731 | localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f); |
| 732 | localPMT[4] = h->params.ts_pid_pmt & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Hans Verkuil | 3eea543 | 2008-08-08 13:27:16 -0300 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 745 | /* Set Audio PID */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 746 | set_reg16(client, 0xc1, h->params.ts_pid_audio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 748 | /* Set Video PID */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 749 | set_reg16(client, 0xc0, h->params.ts_pid_video); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 751 | /* Set PCR PID */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 752 | set_reg16(client, 0xc4, h->params.ts_pid_pcr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 754 | /* Send SI tables */ |
Hans Verkuil | 3eea543 | 2008-08-08 13:27:16 -0300 | [diff] [blame] | 755 | i2c_master_send(client, localPAT, sizeof(PAT)); |
| 756 | i2c_master_send(client, localPMT, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 758 | /* mute then unmute audio. This removes buzzing artefacts */ |
Hans Verkuil | ff5f26b | 2008-09-06 07:00:24 -0300 | [diff] [blame] | 759 | set_reg8(client, 0xa4, 1); |
| 760 | set_reg8(client, 0xa4, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 762 | /* start it going */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | saa6752hs_chip_command(client, SAA6752HS_COMMAND_START); |
| 764 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 765 | /* readout current state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 774 | /* change aspect ratio */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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]; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 781 | switch (h->params.vi_aspect) { |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 782 | case V4L2_MPEG_VIDEO_ASPECT_16x9: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | buf[6] = buf2[1] | 0x40; |
| 784 | break; |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 785 | case V4L2_MPEG_VIDEO_ASPECT_4x3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | default: |
| 787 | buf[6] = buf2[1] & 0xBF; |
| 788 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | buf[7] = buf2[2]; |
| 791 | buf[8] = buf2[3]; |
| 792 | i2c_master_send(client, buf, 9); |
| 793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | return 0; |
| 795 | } |
| 796 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 797 | 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] | 798 | { |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 799 | struct saa6752hs_state *h = to_state(sd); |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 800 | struct saa6752hs_mpeg_params params; |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 801 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 813 | } |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 814 | } |
| 815 | if (set) |
Hans Verkuil | 86b79d66 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 816 | h->params = params; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 817 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 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 | } |
| 845 | } |
| 846 | return 0; |
| 847 | } |
| 848 | |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 849 | 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] | 850 | { |
| 851 | struct saa6752hs_state *h = to_state(sd); |
| 852 | |
| 853 | if (h->video_format == SAA6752HS_VF_UNKNOWN) |
| 854 | h->video_format = SAA6752HS_VF_D1; |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 860 | return 0; |
| 861 | } |
| 862 | |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 863 | 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] | 864 | { |
| 865 | struct saa6752hs_state *h = to_state(sd); |
| 866 | int dist_352, dist_480, dist_720; |
| 867 | |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 868 | if (f->code != V4L2_MBUS_FMT_FIXED) |
| 869 | return -EINVAL; |
| 870 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 883 | dist_352 = abs(f->width - 352); |
| 884 | dist_480 = abs(f->width - 480); |
| 885 | dist_720 = abs(f->width - 720); |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 886 | if (dist_720 < dist_480) { |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 887 | f->width = 720; |
| 888 | f->height = 576; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 889 | h->video_format = SAA6752HS_VF_D1; |
| 890 | } else if (dist_480 < dist_352) { |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 891 | f->width = 480; |
| 892 | f->height = 576; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 893 | h->video_format = SAA6752HS_VF_2_3_D1; |
| 894 | } else { |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 895 | f->width = 352; |
| 896 | if (abs(f->height - 576) < |
| 897 | abs(f->height - 288)) { |
| 898 | f->height = 576; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 899 | h->video_format = SAA6752HS_VF_1_2_D1; |
| 900 | } else { |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 901 | f->height = 288; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 902 | h->video_format = SAA6752HS_VF_SIF; |
| 903 | } |
| 904 | } |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 905 | f->field = V4L2_FIELD_INTERLACED; |
| 906 | f->colorspace = V4L2_COLORSPACE_SMPTE170M; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 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 | } |
| 926 | |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 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, |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 937 | .s_std = saa6752hs_s_std, |
| 938 | }; |
| 939 | |
| 940 | static const struct v4l2_subdev_video_ops saa6752hs_video_ops = { |
Hans Verkuil | 51623ef | 2010-05-09 09:39:58 -0300 | [diff] [blame] | 941 | .s_mbus_fmt = saa6752hs_s_mbus_fmt, |
| 942 | .g_mbus_fmt = saa6752hs_g_mbus_fmt, |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 943 | }; |
| 944 | |
| 945 | static const struct v4l2_subdev_ops saa6752hs_ops = { |
| 946 | .core = &saa6752hs_core_ops, |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 947 | .video = &saa6752hs_video_ops, |
| 948 | }; |
| 949 | |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 950 | static int saa6752hs_probe(struct i2c_client *client, |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 951 | const struct i2c_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | { |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 953 | struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL); |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 954 | struct v4l2_subdev *sd; |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 955 | u8 addr = 0x13; |
| 956 | u8 data[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 958 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 959 | client->addr << 1, client->adapter->name); |
| 960 | if (h == NULL) |
| 961 | return -ENOMEM; |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 962 | sd = &h->sd; |
| 963 | v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops); |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 964 | |
| 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 */ |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 977 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 980 | static int saa6752hs_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | { |
Hans Verkuil | 5b73e98 | 2009-01-14 06:54:38 -0300 | [diff] [blame] | 982 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 983 | |
| 984 | v4l2_device_unregister_subdev(sd); |
| 985 | kfree(to_state(sd)); |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 986 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 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", |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 997 | .probe = saa6752hs_probe, |
| 998 | .remove = saa6752hs_remove, |
| 999 | .id_table = saa6752hs_id, |
| 1000 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |