Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/kernel.h> |
| 3 | #include <linux/sched.h> |
| 4 | #include <linux/string.h> |
| 5 | #include <linux/timer.h> |
| 6 | #include <linux/delay.h> |
| 7 | #include <linux/errno.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/poll.h> |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/types.h> |
Mauro Carvalho Chehab | cab462f | 2006-01-09 15:53:26 -0200 | [diff] [blame] | 12 | #include <linux/videodev2.h> |
| 13 | #include <media/v4l2-common.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/crc32.h> |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #define MPEG_VIDEO_TARGET_BITRATE_MAX 27000 |
| 19 | #define MPEG_VIDEO_MAX_BITRATE_MAX 27000 |
| 20 | #define MPEG_TOTAL_TARGET_BITRATE_MAX 27000 |
| 21 | #define MPEG_PID_MAX ((1 << 14) - 1) |
| 22 | |
| 23 | /* Addresses to scan */ |
| 24 | static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | I2C_CLIENT_INSMOD; |
| 26 | |
| 27 | MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder"); |
| 28 | MODULE_AUTHOR("Andrew de Quincey"); |
| 29 | MODULE_LICENSE("GPL"); |
| 30 | |
| 31 | static struct i2c_driver driver; |
| 32 | static struct i2c_client client_template; |
| 33 | |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 34 | enum saa6752hs_videoformat { |
| 35 | SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */ |
| 36 | SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */ |
| 37 | SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */ |
| 38 | SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */ |
| 39 | SAA6752HS_VF_UNKNOWN, |
| 40 | }; |
| 41 | |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 42 | struct saa6752hs_mpeg_params { |
| 43 | /* transport streams */ |
| 44 | __u16 ts_pid_pmt; |
| 45 | __u16 ts_pid_audio; |
| 46 | __u16 ts_pid_video; |
| 47 | __u16 ts_pid_pcr; |
| 48 | |
| 49 | /* audio */ |
| 50 | enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate; |
| 51 | |
| 52 | /* video */ |
| 53 | enum v4l2_mpeg_video_aspect vi_aspect; |
| 54 | enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode; |
| 55 | __u32 vi_bitrate; |
| 56 | __u32 vi_bitrate_peak; |
| 57 | }; |
| 58 | |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 59 | static const struct v4l2_format v4l2_format_table[] = |
| 60 | { |
Mauro Carvalho Chehab | ac19ecc | 2005-06-23 22:05:09 -0700 | [diff] [blame] | 61 | [SAA6752HS_VF_D1] = |
| 62 | { .fmt = { .pix = { .width = 720, .height = 576 }}}, |
| 63 | [SAA6752HS_VF_2_3_D1] = |
| 64 | { .fmt = { .pix = { .width = 480, .height = 576 }}}, |
| 65 | [SAA6752HS_VF_1_2_D1] = |
| 66 | { .fmt = { .pix = { .width = 352, .height = 576 }}}, |
| 67 | [SAA6752HS_VF_SIF] = |
| 68 | { .fmt = { .pix = { .width = 352, .height = 288 }}}, |
| 69 | [SAA6752HS_VF_UNKNOWN] = |
| 70 | { .fmt = { .pix = { .width = 0, .height = 0}}}, |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct saa6752hs_state { |
| 74 | struct i2c_client client; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 75 | struct v4l2_mpeg_compression old_params; |
| 76 | struct saa6752hs_mpeg_params params; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 77 | enum saa6752hs_videoformat video_format; |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 78 | v4l2_std_id standard; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | enum saa6752hs_command { |
| 82 | SAA6752HS_COMMAND_RESET = 0, |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 83 | SAA6752HS_COMMAND_STOP = 1, |
| 84 | SAA6752HS_COMMAND_START = 2, |
| 85 | SAA6752HS_COMMAND_PAUSE = 3, |
| 86 | SAA6752HS_COMMAND_RECONFIGURE = 4, |
| 87 | SAA6752HS_COMMAND_SLEEP = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6, |
| 89 | |
| 90 | SAA6752HS_COMMAND_MAX |
| 91 | }; |
| 92 | |
| 93 | /* ---------------------------------------------------------------------- */ |
| 94 | |
| 95 | static u8 PAT[] = { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 96 | 0xc2, /* i2c register */ |
| 97 | 0x00, /* table number for encoder */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 99 | 0x47, /* sync */ |
| 100 | 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */ |
| 101 | 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 103 | 0x00, /* PSI pointer to start of table */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 105 | 0x00, /* tid(0) */ |
| 106 | 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 108 | 0x00, 0x01, /* transport_stream_id(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 110 | 0xc1, /* version_number(0), current_next_indicator(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 112 | 0x00, 0x00, /* section_number(0), last_section_number(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 114 | 0x00, 0x01, /* program_number(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 116 | 0xe0, 0x00, /* PMT PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 118 | 0x00, 0x00, 0x00, 0x00 /* CRC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static u8 PMT[] = { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 122 | 0xc2, /* i2c register */ |
| 123 | 0x01, /* table number for encoder */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 125 | 0x47, /* sync */ |
| 126 | 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */ |
| 127 | 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 129 | 0x00, /* PSI pointer to start of table */ |
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 | 0x02, /* tid(2) */ |
| 132 | 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 134 | 0x00, 0x01, /* program_number(1) */ |
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 | 0xc1, /* version_number(0), current_next_indicator(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 | 0x00, 0x00, /* section_number(0), last_section_number(0) */ |
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 | 0xe0, 0x00, /* PCR_PID */ |
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 | 0xf0, 0x00, /* program_info_length(0) */ |
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 | 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */ |
| 145 | 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 147 | 0x00, 0x00, 0x00, 0x00 /* CRC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 150 | static struct saa6752hs_mpeg_params param_defaults = |
| 151 | { |
| 152 | .ts_pid_pmt = 16, |
| 153 | .ts_pid_video = 260, |
| 154 | .ts_pid_audio = 256, |
| 155 | .ts_pid_pcr = 259, |
| 156 | |
| 157 | .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3, |
| 158 | .vi_bitrate = 4000, |
| 159 | .vi_bitrate_peak = 6000, |
| 160 | .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR, |
| 161 | |
| 162 | .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K, |
| 163 | }; |
| 164 | |
| 165 | static struct v4l2_mpeg_compression old_param_defaults = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | .st_type = V4L2_MPEG_TS_2, |
| 168 | .st_bitrate = { |
| 169 | .mode = V4L2_BITRATE_CBR, |
| 170 | .target = 7000, |
| 171 | }, |
| 172 | |
| 173 | .ts_pid_pmt = 16, |
| 174 | .ts_pid_video = 260, |
| 175 | .ts_pid_audio = 256, |
| 176 | .ts_pid_pcr = 259, |
| 177 | |
| 178 | .vi_type = V4L2_MPEG_VI_2, |
| 179 | .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3, |
| 180 | .vi_bitrate = { |
| 181 | .mode = V4L2_BITRATE_VBR, |
| 182 | .target = 4000, |
| 183 | .max = 6000, |
| 184 | }, |
| 185 | |
| 186 | .au_type = V4L2_MPEG_AU_2_II, |
| 187 | .au_bitrate = { |
| 188 | .mode = V4L2_BITRATE_CBR, |
| 189 | .target = 256, |
| 190 | }, |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | /* ---------------------------------------------------------------------- */ |
| 195 | |
| 196 | static int saa6752hs_chip_command(struct i2c_client* client, |
| 197 | enum saa6752hs_command command) |
| 198 | { |
| 199 | unsigned char buf[3]; |
| 200 | unsigned long timeout; |
| 201 | int status = 0; |
| 202 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 203 | /* execute the command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | switch(command) { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 205 | case SAA6752HS_COMMAND_RESET: |
| 206 | buf[0] = 0x00; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | case SAA6752HS_COMMAND_STOP: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 210 | buf[0] = 0x03; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | break; |
| 212 | |
| 213 | case SAA6752HS_COMMAND_START: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 214 | buf[0] = 0x02; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | break; |
| 216 | |
| 217 | case SAA6752HS_COMMAND_PAUSE: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 218 | buf[0] = 0x04; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | break; |
| 220 | |
| 221 | case SAA6752HS_COMMAND_RECONFIGURE: |
| 222 | buf[0] = 0x05; |
| 223 | break; |
| 224 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 225 | case SAA6752HS_COMMAND_SLEEP: |
| 226 | buf[0] = 0x06; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | break; |
| 228 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 229 | case SAA6752HS_COMMAND_RECONFIGURE_FORCE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | buf[0] = 0x07; |
| 231 | break; |
| 232 | |
| 233 | default: |
| 234 | return -EINVAL; |
| 235 | } |
| 236 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 237 | /* set it and wait for it to be so */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | i2c_master_send(client, buf, 1); |
| 239 | timeout = jiffies + HZ * 3; |
| 240 | for (;;) { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 241 | /* get the current status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | buf[0] = 0x10; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 243 | i2c_master_send(client, buf, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | i2c_master_recv(client, buf, 1); |
| 245 | |
| 246 | if (!(buf[0] & 0x20)) |
| 247 | break; |
| 248 | if (time_after(jiffies,timeout)) { |
| 249 | status = -ETIMEDOUT; |
| 250 | break; |
| 251 | } |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | msleep(10); |
| 254 | } |
| 255 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 256 | /* delay a bit to let encoder settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | msleep(50); |
| 258 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 259 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | static int saa6752hs_set_bitrate(struct i2c_client* client, |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 264 | struct saa6752hs_mpeg_params* params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 266 | u8 buf[3]; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 267 | int tot_bitrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 269 | /* set the bitrate mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | buf[0] = 0x71; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 271 | buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | i2c_master_send(client, buf, 2); |
| 273 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 274 | /* set the video bitrate */ |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 275 | if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 276 | /* set the target bitrate */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | buf[0] = 0x80; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 278 | buf[1] = params->vi_bitrate >> 8; |
| 279 | buf[2] = params->vi_bitrate & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | i2c_master_send(client, buf, 3); |
| 281 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 282 | /* set the max bitrate */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | buf[0] = 0x81; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 284 | buf[1] = params->vi_bitrate_peak >> 8; |
| 285 | buf[2] = params->vi_bitrate_peak & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | i2c_master_send(client, buf, 3); |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 287 | tot_bitrate = params->vi_bitrate_peak; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } else { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 289 | /* set the target bitrate (no max bitrate for CBR) */ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 290 | buf[0] = 0x81; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 291 | buf[1] = params->vi_bitrate >> 8; |
| 292 | buf[2] = params->vi_bitrate & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | i2c_master_send(client, buf, 3); |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 294 | tot_bitrate = params->vi_bitrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 297 | /* set the audio bitrate */ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 298 | buf[0] = 0x94; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 299 | buf[1] = (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | i2c_master_send(client, buf, 2); |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 301 | tot_bitrate += (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 256 : 384; |
| 302 | |
| 303 | /* Note: the total max bitrate is determined by adding the video and audio |
| 304 | bitrates together and also adding an extra 768kbit/s to stay on the |
| 305 | safe side. If more control should be required, then an extra MPEG control |
| 306 | should be added. */ |
| 307 | tot_bitrate += 768; |
| 308 | if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX) |
| 309 | tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 311 | /* set the total bitrate */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | buf[0] = 0xb1; |
Mauro Carvalho Chehab | b57e557 | 2006-06-23 16:13:56 -0300 | [diff] [blame] | 313 | buf[1] = tot_bitrate >> 8; |
| 314 | buf[2] = tot_bitrate & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | i2c_master_send(client, buf, 3); |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 320 | static void saa6752hs_set_subsampling(struct i2c_client* client, |
| 321 | struct v4l2_format* f) |
| 322 | { |
| 323 | struct saa6752hs_state *h = i2c_get_clientdata(client); |
| 324 | int dist_352, dist_480, dist_720; |
| 325 | |
| 326 | /* |
| 327 | FIXME: translate and round width/height into EMPRESS |
| 328 | subsample type: |
| 329 | |
| 330 | type | PAL | NTSC |
| 331 | --------------------------- |
| 332 | SIF | 352x288 | 352x240 |
| 333 | 1/2 D1 | 352x576 | 352x480 |
| 334 | 2/3 D1 | 480x576 | 480x480 |
| 335 | D1 | 720x576 | 720x480 |
| 336 | */ |
| 337 | |
| 338 | dist_352 = abs(f->fmt.pix.width - 352); |
| 339 | dist_480 = abs(f->fmt.pix.width - 480); |
| 340 | dist_720 = abs(f->fmt.pix.width - 720); |
| 341 | if (dist_720 < dist_480) { |
| 342 | f->fmt.pix.width = 720; |
| 343 | f->fmt.pix.height = 576; |
| 344 | h->video_format = SAA6752HS_VF_D1; |
| 345 | } |
| 346 | else if (dist_480 < dist_352) { |
| 347 | f->fmt.pix.width = 480; |
| 348 | f->fmt.pix.height = 576; |
| 349 | h->video_format = SAA6752HS_VF_2_3_D1; |
| 350 | } |
| 351 | else { |
| 352 | f->fmt.pix.width = 352; |
| 353 | if (abs(f->fmt.pix.height - 576) < |
| 354 | abs(f->fmt.pix.height - 288)) { |
| 355 | f->fmt.pix.height = 576; |
| 356 | h->video_format = SAA6752HS_VF_1_2_D1; |
| 357 | } |
| 358 | else { |
| 359 | f->fmt.pix.height = 288; |
| 360 | h->video_format = SAA6752HS_VF_SIF; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 366 | static void saa6752hs_old_set_params(struct i2c_client* client, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | struct v4l2_mpeg_compression* params) |
| 368 | { |
| 369 | struct saa6752hs_state *h = i2c_get_clientdata(client); |
| 370 | |
| 371 | /* check PIDs */ |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 372 | if (params->ts_pid_pmt <= MPEG_PID_MAX) { |
| 373 | h->old_params.ts_pid_pmt = params->ts_pid_pmt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | h->params.ts_pid_pmt = params->ts_pid_pmt; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 375 | } |
| 376 | if (params->ts_pid_pcr <= MPEG_PID_MAX) { |
| 377 | h->old_params.ts_pid_pcr = params->ts_pid_pcr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | h->params.ts_pid_pcr = params->ts_pid_pcr; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 379 | } |
| 380 | if (params->ts_pid_video <= MPEG_PID_MAX) { |
| 381 | h->old_params.ts_pid_video = params->ts_pid_video; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | h->params.ts_pid_video = params->ts_pid_video; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 383 | } |
| 384 | if (params->ts_pid_audio <= MPEG_PID_MAX) { |
| 385 | h->old_params.ts_pid_audio = params->ts_pid_audio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | h->params.ts_pid_audio = params->ts_pid_audio; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | /* check bitrate parameters */ |
| 390 | if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) || |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 391 | (params->vi_bitrate.mode == V4L2_BITRATE_VBR)) { |
| 392 | h->old_params.vi_bitrate.mode = params->vi_bitrate.mode; |
| 393 | h->params.vi_bitrate_mode = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ? |
| 394 | V4L2_MPEG_VIDEO_BITRATE_MODE_VBR : V4L2_MPEG_VIDEO_BITRATE_MODE_CBR; |
| 395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if (params->vi_bitrate.mode != V4L2_BITRATE_NONE) |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 397 | h->old_params.st_bitrate.target = params->st_bitrate.target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (params->vi_bitrate.mode != V4L2_BITRATE_NONE) |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 399 | h->old_params.vi_bitrate.target = params->vi_bitrate.target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 401 | h->old_params.vi_bitrate.max = params->vi_bitrate.max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (params->au_bitrate.mode != V4L2_BITRATE_NONE) |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 403 | h->old_params.au_bitrate.target = params->au_bitrate.target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | /* aspect ratio */ |
| 406 | if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 || |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 407 | params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9) { |
| 408 | h->old_params.vi_aspect_ratio = params->vi_aspect_ratio; |
| 409 | if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3) |
| 410 | h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3; |
| 411 | else |
| 412 | h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_16x9; |
| 413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | /* range checks */ |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 416 | if (h->old_params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX) |
| 417 | h->old_params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX; |
| 418 | if (h->old_params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX) |
| 419 | h->old_params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX; |
| 420 | if (h->old_params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX) |
| 421 | h->old_params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX; |
| 422 | h->params.vi_bitrate = params->vi_bitrate.target; |
| 423 | h->params.vi_bitrate_peak = params->vi_bitrate.max; |
| 424 | if (h->old_params.au_bitrate.target <= 256) { |
| 425 | h->old_params.au_bitrate.target = 256; |
| 426 | h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K; |
| 427 | } |
| 428 | else { |
| 429 | h->old_params.au_bitrate.target = 384; |
| 430 | h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_384K; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | static int handle_ctrl(struct saa6752hs_mpeg_params *params, |
| 435 | struct v4l2_ext_control *ctrl, int cmd) |
| 436 | { |
| 437 | int old = 0, new; |
| 438 | int set = cmd == VIDIOC_S_EXT_CTRLS; |
| 439 | |
| 440 | new = ctrl->value; |
| 441 | switch (ctrl->id) { |
| 442 | case V4L2_CID_MPEG_STREAM_TYPE: |
| 443 | old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS; |
| 444 | if (set && new != old) |
| 445 | return -ERANGE; |
| 446 | new = old; |
| 447 | break; |
| 448 | case V4L2_CID_MPEG_STREAM_PID_PMT: |
| 449 | old = params->ts_pid_pmt; |
| 450 | if (set && new > MPEG_PID_MAX) |
| 451 | return -ERANGE; |
| 452 | if (new > MPEG_PID_MAX) |
| 453 | new = MPEG_PID_MAX; |
| 454 | params->ts_pid_pmt = new; |
| 455 | break; |
| 456 | case V4L2_CID_MPEG_STREAM_PID_AUDIO: |
| 457 | old = params->ts_pid_audio; |
| 458 | if (set && new > MPEG_PID_MAX) |
| 459 | return -ERANGE; |
| 460 | if (new > MPEG_PID_MAX) |
| 461 | new = MPEG_PID_MAX; |
| 462 | params->ts_pid_audio = new; |
| 463 | break; |
| 464 | case V4L2_CID_MPEG_STREAM_PID_VIDEO: |
| 465 | old = params->ts_pid_video; |
| 466 | if (set && new > MPEG_PID_MAX) |
| 467 | return -ERANGE; |
| 468 | if (new > MPEG_PID_MAX) |
| 469 | new = MPEG_PID_MAX; |
| 470 | params->ts_pid_video = new; |
| 471 | break; |
| 472 | case V4L2_CID_MPEG_STREAM_PID_PCR: |
| 473 | old = params->ts_pid_pcr; |
| 474 | if (set && new > MPEG_PID_MAX) |
| 475 | return -ERANGE; |
| 476 | if (new > MPEG_PID_MAX) |
| 477 | new = MPEG_PID_MAX; |
| 478 | params->ts_pid_pcr = new; |
| 479 | break; |
| 480 | case V4L2_CID_MPEG_AUDIO_ENCODING: |
| 481 | old = V4L2_MPEG_AUDIO_ENCODING_LAYER_2; |
| 482 | if (set && new != old) |
| 483 | return -ERANGE; |
| 484 | new = old; |
| 485 | break; |
| 486 | case V4L2_CID_MPEG_AUDIO_L2_BITRATE: |
| 487 | old = params->au_l2_bitrate; |
| 488 | if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K && |
| 489 | new != V4L2_MPEG_AUDIO_L2_BITRATE_384K) |
| 490 | return -ERANGE; |
| 491 | if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K) |
| 492 | new = V4L2_MPEG_AUDIO_L2_BITRATE_256K; |
| 493 | else |
| 494 | new = V4L2_MPEG_AUDIO_L2_BITRATE_384K; |
| 495 | params->au_l2_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 | if (cmd == VIDIOC_G_EXT_CTRLS) |
| 544 | ctrl->value = old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | else |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 546 | ctrl->value = new; |
| 547 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static int saa6752hs_init(struct i2c_client* client) |
| 551 | { |
| 552 | unsigned char buf[9], buf2[4]; |
| 553 | struct saa6752hs_state *h; |
| 554 | u32 crc; |
| 555 | unsigned char localPAT[256]; |
| 556 | unsigned char localPMT[256]; |
| 557 | |
| 558 | h = i2c_get_clientdata(client); |
| 559 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 560 | /* Set video format - must be done first as it resets other settings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | buf[0] = 0x41; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 562 | buf[1] = h->video_format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | i2c_master_send(client, buf, 2); |
| 564 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 565 | /* Set number of lines in input signal */ |
| 566 | buf[0] = 0x40; |
| 567 | buf[1] = 0x00; |
| 568 | if (h->standard & V4L2_STD_525_60) |
| 569 | buf[1] = 0x01; |
| 570 | i2c_master_send(client, buf, 2); |
| 571 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 572 | /* set bitrate */ |
| 573 | saa6752hs_set_bitrate(client, &h->params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 575 | /* Set GOP structure {3, 13} */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | buf[0] = 0x72; |
| 577 | buf[1] = 0x03; |
| 578 | buf[2] = 0x0D; |
| 579 | i2c_master_send(client,buf,3); |
| 580 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 581 | /* Set minimum Q-scale {4} */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | buf[0] = 0x82; |
| 583 | buf[1] = 0x04; |
| 584 | i2c_master_send(client,buf,2); |
| 585 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 586 | /* Set maximum Q-scale {12} */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | buf[0] = 0x83; |
| 588 | buf[1] = 0x0C; |
| 589 | i2c_master_send(client,buf,2); |
| 590 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 591 | /* Set Output Protocol */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | buf[0] = 0xD0; |
| 593 | buf[1] = 0x81; |
| 594 | i2c_master_send(client,buf,2); |
| 595 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 596 | /* Set video output stream format {TS} */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | buf[0] = 0xB0; |
| 598 | buf[1] = 0x05; |
| 599 | i2c_master_send(client,buf,2); |
| 600 | |
| 601 | /* compute PAT */ |
| 602 | memcpy(localPAT, PAT, sizeof(PAT)); |
| 603 | localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f); |
| 604 | localPAT[18] = h->params.ts_pid_pmt & 0xff; |
| 605 | crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4); |
| 606 | localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF; |
| 607 | localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF; |
| 608 | localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF; |
| 609 | localPAT[sizeof(PAT) - 1] = crc & 0xFF; |
| 610 | |
| 611 | /* compute PMT */ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 612 | memcpy(localPMT, PMT, sizeof(PMT)); |
| 613 | localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f); |
| 614 | localPMT[4] = h->params.ts_pid_pmt & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F); |
| 616 | localPMT[16] = h->params.ts_pid_pcr & 0xFF; |
| 617 | localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F); |
| 618 | localPMT[21] = h->params.ts_pid_video & 0xFF; |
| 619 | localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F); |
| 620 | localPMT[26] = h->params.ts_pid_audio & 0xFF; |
| 621 | crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4); |
| 622 | localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF; |
| 623 | localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF; |
| 624 | localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF; |
| 625 | localPMT[sizeof(PMT) - 1] = crc & 0xFF; |
| 626 | |
Trent Piepho | 657de3c | 2006-06-20 00:30:57 -0300 | [diff] [blame] | 627 | /* Set Audio PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | buf[0] = 0xC1; |
| 629 | buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF; |
| 630 | buf[2] = h->params.ts_pid_audio & 0xFF; |
| 631 | i2c_master_send(client,buf,3); |
| 632 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 633 | /* Set Video PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | buf[0] = 0xC0; |
| 635 | buf[1] = (h->params.ts_pid_video >> 8) & 0xFF; |
| 636 | buf[2] = h->params.ts_pid_video & 0xFF; |
| 637 | i2c_master_send(client,buf,3); |
| 638 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 639 | /* Set PCR PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | buf[0] = 0xC4; |
| 641 | buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF; |
| 642 | buf[2] = h->params.ts_pid_pcr & 0xFF; |
| 643 | i2c_master_send(client,buf,3); |
| 644 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 645 | /* Send SI tables */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | i2c_master_send(client,localPAT,sizeof(PAT)); |
| 647 | i2c_master_send(client,localPMT,sizeof(PMT)); |
| 648 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 649 | /* mute then unmute audio. This removes buzzing artefacts */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | buf[0] = 0xa4; |
| 651 | buf[1] = 1; |
| 652 | i2c_master_send(client, buf, 2); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 653 | buf[1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | i2c_master_send(client, buf, 2); |
| 655 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 656 | /* start it going */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | saa6752hs_chip_command(client, SAA6752HS_COMMAND_START); |
| 658 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 659 | /* readout current state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | buf[0] = 0xE1; |
| 661 | buf[1] = 0xA7; |
| 662 | buf[2] = 0xFE; |
| 663 | buf[3] = 0x82; |
| 664 | buf[4] = 0xB0; |
| 665 | i2c_master_send(client, buf, 5); |
| 666 | i2c_master_recv(client, buf2, 4); |
| 667 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 668 | /* change aspect ratio */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | buf[0] = 0xE0; |
| 670 | buf[1] = 0xA7; |
| 671 | buf[2] = 0xFE; |
| 672 | buf[3] = 0x82; |
| 673 | buf[4] = 0xB0; |
| 674 | buf[5] = buf2[0]; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 675 | switch(h->params.vi_aspect) { |
| 676 | case V4L2_MPEG_VIDEO_ASPECT_16x9: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | buf[6] = buf2[1] | 0x40; |
| 678 | break; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 679 | case V4L2_MPEG_VIDEO_ASPECT_4x3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | default: |
| 681 | buf[6] = buf2[1] & 0xBF; |
| 682 | break; |
| 683 | break; |
| 684 | } |
| 685 | buf[7] = buf2[2]; |
| 686 | buf[8] = buf2[3]; |
| 687 | i2c_master_send(client, buf, 9); |
| 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind) |
| 693 | { |
| 694 | struct saa6752hs_state *h; |
| 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 697 | if (NULL == (h = kzalloc(sizeof(*h), GFP_KERNEL))) |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 698 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | h->client = client_template; |
| 700 | h->params = param_defaults; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 701 | h->old_params = old_param_defaults; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | h->client.adapter = adap; |
| 703 | h->client.addr = addr; |
| 704 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 705 | /* Assume 625 input lines */ |
| 706 | h->standard = 0; |
| 707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | i2c_set_clientdata(&h->client, h); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 709 | i2c_attach_client(&h->client); |
Mauro Carvalho Chehab | 674434c | 2005-12-12 00:37:28 -0800 | [diff] [blame] | 710 | |
Mauro Carvalho Chehab | cab462f | 2006-01-09 15:53:26 -0200 | [diff] [blame] | 711 | v4l_info(&h->client,"saa6752hs: chip found @ 0x%x\n", addr<<1); |
| 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static int saa6752hs_probe(struct i2c_adapter *adap) |
| 717 | { |
| 718 | if (adap->class & I2C_CLASS_TV_ANALOG) |
| 719 | return i2c_probe(adap, &addr_data, saa6752hs_attach); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static int saa6752hs_detach(struct i2c_client *client) |
| 724 | { |
| 725 | struct saa6752hs_state *h; |
| 726 | |
| 727 | h = i2c_get_clientdata(client); |
| 728 | i2c_detach_client(client); |
| 729 | kfree(h); |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static int |
| 734 | saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg) |
| 735 | { |
| 736 | struct saa6752hs_state *h = i2c_get_clientdata(client); |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 737 | struct v4l2_ext_controls *ctrls = arg; |
| 738 | struct v4l2_mpeg_compression *old_params = arg; |
| 739 | struct saa6752hs_mpeg_params params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | int err = 0; |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 741 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 743 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | case VIDIOC_S_MPEGCOMP: |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 745 | if (NULL == old_params) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | /* apply settings and start encoder */ |
| 747 | saa6752hs_init(client); |
| 748 | break; |
| 749 | } |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 750 | saa6752hs_old_set_params(client, old_params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | /* fall through */ |
| 752 | case VIDIOC_G_MPEGCOMP: |
Hans Verkuil | 86b79d6 | 2006-06-18 16:40:10 -0300 | [diff] [blame] | 753 | *old_params = h->old_params; |
| 754 | break; |
| 755 | case VIDIOC_S_EXT_CTRLS: |
| 756 | if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) |
| 757 | return -EINVAL; |
| 758 | if (ctrls->count == 0) { |
| 759 | /* apply settings and start encoder */ |
| 760 | saa6752hs_init(client); |
| 761 | break; |
| 762 | } |
| 763 | /* fall through */ |
| 764 | case VIDIOC_TRY_EXT_CTRLS: |
| 765 | case VIDIOC_G_EXT_CTRLS: |
| 766 | if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) |
| 767 | return -EINVAL; |
| 768 | params = h->params; |
| 769 | for (i = 0; i < ctrls->count; i++) { |
| 770 | if ((err = handle_ctrl(¶ms, ctrls->controls + i, cmd))) { |
| 771 | ctrls->error_idx = i; |
| 772 | return err; |
| 773 | } |
| 774 | } |
| 775 | h->params = params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | break; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 777 | case VIDIOC_G_FMT: |
| 778 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 779 | struct v4l2_format *f = arg; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 780 | |
| 781 | if (h->video_format == SAA6752HS_VF_UNKNOWN) |
| 782 | h->video_format = SAA6752HS_VF_D1; |
| 783 | f->fmt.pix.width = |
| 784 | v4l2_format_table[h->video_format].fmt.pix.width; |
| 785 | f->fmt.pix.height = |
| 786 | v4l2_format_table[h->video_format].fmt.pix.height; |
| 787 | break ; |
| 788 | } |
| 789 | case VIDIOC_S_FMT: |
| 790 | { |
| 791 | struct v4l2_format *f = arg; |
| 792 | |
| 793 | saa6752hs_set_subsampling(client, f); |
| 794 | break; |
| 795 | } |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 796 | case VIDIOC_S_STD: |
| 797 | h->standard = *((v4l2_std_id *) arg); |
| 798 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | default: |
| 800 | /* nothing */ |
| 801 | break; |
| 802 | } |
| 803 | |
| 804 | return err; |
| 805 | } |
| 806 | |
| 807 | /* ----------------------------------------------------------------------- */ |
| 808 | |
| 809 | static struct i2c_driver driver = { |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 810 | .driver = { |
Mauro Carvalho Chehab | cab462f | 2006-01-09 15:53:26 -0200 | [diff] [blame] | 811 | .name = "saa6752hs", |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 812 | }, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 813 | .id = I2C_DRIVERID_SAA6752HS, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 814 | .attach_adapter = saa6752hs_probe, |
| 815 | .detach_client = saa6752hs_detach, |
| 816 | .command = saa6752hs_command, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | }; |
| 818 | |
| 819 | static struct i2c_client client_template = |
| 820 | { |
Jean Delvare | fae91e7 | 2005-08-15 19:57:04 +0200 | [diff] [blame] | 821 | .name = "saa6752hs", |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 822 | .driver = &driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | }; |
| 824 | |
| 825 | static int __init saa6752hs_init_module(void) |
| 826 | { |
| 827 | return i2c_add_driver(&driver); |
| 828 | } |
| 829 | |
| 830 | static void __exit saa6752hs_cleanup_module(void) |
| 831 | { |
| 832 | i2c_del_driver(&driver); |
| 833 | } |
| 834 | |
| 835 | module_init(saa6752hs_init_module); |
| 836 | module_exit(saa6752hs_cleanup_module); |
| 837 | |
| 838 | /* |
| 839 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 840 | * --------------------------------------------------------------------------- |
| 841 | * Local variables: |
| 842 | * c-basic-offset: 8 |
| 843 | * End: |
| 844 | */ |