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> |
| 12 | #include <linux/videodev.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/crc32.h> |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #define MPEG_VIDEO_TARGET_BITRATE_MAX 27000 |
| 18 | #define MPEG_VIDEO_MAX_BITRATE_MAX 27000 |
| 19 | #define MPEG_TOTAL_TARGET_BITRATE_MAX 27000 |
| 20 | #define MPEG_PID_MAX ((1 << 14) - 1) |
| 21 | |
| 22 | /* Addresses to scan */ |
| 23 | static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | I2C_CLIENT_INSMOD; |
| 25 | |
| 26 | MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder"); |
| 27 | MODULE_AUTHOR("Andrew de Quincey"); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
| 30 | static struct i2c_driver driver; |
| 31 | static struct i2c_client client_template; |
| 32 | |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 33 | enum saa6752hs_videoformat { |
| 34 | SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */ |
| 35 | SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */ |
| 36 | SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */ |
| 37 | SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */ |
| 38 | SAA6752HS_VF_UNKNOWN, |
| 39 | }; |
| 40 | |
| 41 | static const struct v4l2_format v4l2_format_table[] = |
| 42 | { |
Mauro Carvalho Chehab | ac19ecc | 2005-06-23 22:05:09 -0700 | [diff] [blame] | 43 | [SAA6752HS_VF_D1] = |
| 44 | { .fmt = { .pix = { .width = 720, .height = 576 }}}, |
| 45 | [SAA6752HS_VF_2_3_D1] = |
| 46 | { .fmt = { .pix = { .width = 480, .height = 576 }}}, |
| 47 | [SAA6752HS_VF_1_2_D1] = |
| 48 | { .fmt = { .pix = { .width = 352, .height = 576 }}}, |
| 49 | [SAA6752HS_VF_SIF] = |
| 50 | { .fmt = { .pix = { .width = 352, .height = 288 }}}, |
| 51 | [SAA6752HS_VF_UNKNOWN] = |
| 52 | { .fmt = { .pix = { .width = 0, .height = 0}}}, |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | struct saa6752hs_state { |
| 56 | struct i2c_client client; |
| 57 | struct v4l2_mpeg_compression params; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 58 | enum saa6752hs_videoformat video_format; |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 59 | v4l2_std_id standard; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | enum saa6752hs_command { |
| 63 | SAA6752HS_COMMAND_RESET = 0, |
| 64 | SAA6752HS_COMMAND_STOP = 1, |
| 65 | SAA6752HS_COMMAND_START = 2, |
| 66 | SAA6752HS_COMMAND_PAUSE = 3, |
| 67 | SAA6752HS_COMMAND_RECONFIGURE = 4, |
| 68 | SAA6752HS_COMMAND_SLEEP = 5, |
| 69 | SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6, |
| 70 | |
| 71 | SAA6752HS_COMMAND_MAX |
| 72 | }; |
| 73 | |
| 74 | /* ---------------------------------------------------------------------- */ |
| 75 | |
| 76 | static u8 PAT[] = { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 77 | 0xc2, /* i2c register */ |
| 78 | 0x00, /* table number for encoder */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 80 | 0x47, /* sync */ |
| 81 | 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */ |
| 82 | 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 84 | 0x00, /* PSI pointer to start of table */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 86 | 0x00, /* tid(0) */ |
| 87 | 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 89 | 0x00, 0x01, /* transport_stream_id(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 91 | 0xc1, /* version_number(0), current_next_indicator(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 93 | 0x00, 0x00, /* section_number(0), last_section_number(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 95 | 0x00, 0x01, /* program_number(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 97 | 0xe0, 0x00, /* PMT PID */ |
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 | 0x00, 0x00, 0x00, 0x00 /* CRC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | static u8 PMT[] = { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 103 | 0xc2, /* i2c register */ |
| 104 | 0x01, /* table number for encoder */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 106 | 0x47, /* sync */ |
| 107 | 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */ |
| 108 | 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */ |
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 | 0x00, /* PSI pointer to start of table */ |
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 | 0x02, /* tid(2) */ |
| 113 | 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 115 | 0x00, 0x01, /* program_number(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 117 | 0xc1, /* version_number(0), current_next_indicator(1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 119 | 0x00, 0x00, /* section_number(0), last_section_number(0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 121 | 0xe0, 0x00, /* PCR_PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 123 | 0xf0, 0x00, /* program_info_length(0) */ |
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 | 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */ |
| 126 | 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 128 | 0x00, 0x00, 0x00, 0x00 /* CRC32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static struct v4l2_mpeg_compression param_defaults = |
| 132 | { |
| 133 | .st_type = V4L2_MPEG_TS_2, |
| 134 | .st_bitrate = { |
| 135 | .mode = V4L2_BITRATE_CBR, |
| 136 | .target = 7000, |
| 137 | }, |
| 138 | |
| 139 | .ts_pid_pmt = 16, |
| 140 | .ts_pid_video = 260, |
| 141 | .ts_pid_audio = 256, |
| 142 | .ts_pid_pcr = 259, |
| 143 | |
| 144 | .vi_type = V4L2_MPEG_VI_2, |
| 145 | .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3, |
| 146 | .vi_bitrate = { |
| 147 | .mode = V4L2_BITRATE_VBR, |
| 148 | .target = 4000, |
| 149 | .max = 6000, |
| 150 | }, |
| 151 | |
| 152 | .au_type = V4L2_MPEG_AU_2_II, |
| 153 | .au_bitrate = { |
| 154 | .mode = V4L2_BITRATE_CBR, |
| 155 | .target = 256, |
| 156 | }, |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | /* ---------------------------------------------------------------------- */ |
| 161 | |
| 162 | static int saa6752hs_chip_command(struct i2c_client* client, |
| 163 | enum saa6752hs_command command) |
| 164 | { |
| 165 | unsigned char buf[3]; |
| 166 | unsigned long timeout; |
| 167 | int status = 0; |
| 168 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 169 | /* execute the command */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | switch(command) { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 171 | case SAA6752HS_COMMAND_RESET: |
| 172 | buf[0] = 0x00; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | break; |
| 174 | |
| 175 | case SAA6752HS_COMMAND_STOP: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 176 | buf[0] = 0x03; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | break; |
| 178 | |
| 179 | case SAA6752HS_COMMAND_START: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 180 | buf[0] = 0x02; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | break; |
| 182 | |
| 183 | case SAA6752HS_COMMAND_PAUSE: |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 184 | buf[0] = 0x04; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | break; |
| 186 | |
| 187 | case SAA6752HS_COMMAND_RECONFIGURE: |
| 188 | buf[0] = 0x05; |
| 189 | break; |
| 190 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 191 | case SAA6752HS_COMMAND_SLEEP: |
| 192 | buf[0] = 0x06; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | break; |
| 194 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 195 | case SAA6752HS_COMMAND_RECONFIGURE_FORCE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | buf[0] = 0x07; |
| 197 | break; |
| 198 | |
| 199 | default: |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 203 | /* set it and wait for it to be so */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | i2c_master_send(client, buf, 1); |
| 205 | timeout = jiffies + HZ * 3; |
| 206 | for (;;) { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 207 | /* get the current status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | buf[0] = 0x10; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 209 | i2c_master_send(client, buf, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | i2c_master_recv(client, buf, 1); |
| 211 | |
| 212 | if (!(buf[0] & 0x20)) |
| 213 | break; |
| 214 | if (time_after(jiffies,timeout)) { |
| 215 | status = -ETIMEDOUT; |
| 216 | break; |
| 217 | } |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | msleep(10); |
| 220 | } |
| 221 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 222 | /* delay a bit to let encoder settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | msleep(50); |
| 224 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 225 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | static int saa6752hs_set_bitrate(struct i2c_client* client, |
| 230 | struct v4l2_mpeg_compression* params) |
| 231 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 232 | u8 buf[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 234 | /* set the bitrate mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | buf[0] = 0x71; |
| 236 | buf[1] = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ? 0 : 1; |
| 237 | i2c_master_send(client, buf, 2); |
| 238 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 239 | /* set the video bitrate */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 241 | /* set the target bitrate */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | buf[0] = 0x80; |
| 243 | buf[1] = params->vi_bitrate.target >> 8; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 244 | buf[2] = params->vi_bitrate.target & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | i2c_master_send(client, buf, 3); |
| 246 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 247 | /* set the max bitrate */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | buf[0] = 0x81; |
| 249 | buf[1] = params->vi_bitrate.max >> 8; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 250 | buf[2] = params->vi_bitrate.max & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | i2c_master_send(client, buf, 3); |
| 252 | } else { |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 253 | /* set the target bitrate (no max bitrate for CBR) */ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 254 | buf[0] = 0x81; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | buf[1] = params->vi_bitrate.target >> 8; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 256 | buf[2] = params->vi_bitrate.target & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | i2c_master_send(client, buf, 3); |
| 258 | } |
| 259 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 260 | /* set the audio bitrate */ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 261 | buf[0] = 0x94; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | buf[1] = (256 == params->au_bitrate.target) ? 0 : 1; |
| 263 | i2c_master_send(client, buf, 2); |
| 264 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 265 | /* set the total bitrate */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | buf[0] = 0xb1; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 267 | buf[1] = params->st_bitrate.target >> 8; |
| 268 | buf[2] = params->st_bitrate.target & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | i2c_master_send(client, buf, 3); |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 274 | static void saa6752hs_set_subsampling(struct i2c_client* client, |
| 275 | struct v4l2_format* f) |
| 276 | { |
| 277 | struct saa6752hs_state *h = i2c_get_clientdata(client); |
| 278 | int dist_352, dist_480, dist_720; |
| 279 | |
| 280 | /* |
| 281 | FIXME: translate and round width/height into EMPRESS |
| 282 | subsample type: |
| 283 | |
| 284 | type | PAL | NTSC |
| 285 | --------------------------- |
| 286 | SIF | 352x288 | 352x240 |
| 287 | 1/2 D1 | 352x576 | 352x480 |
| 288 | 2/3 D1 | 480x576 | 480x480 |
| 289 | D1 | 720x576 | 720x480 |
| 290 | */ |
| 291 | |
| 292 | dist_352 = abs(f->fmt.pix.width - 352); |
| 293 | dist_480 = abs(f->fmt.pix.width - 480); |
| 294 | dist_720 = abs(f->fmt.pix.width - 720); |
| 295 | if (dist_720 < dist_480) { |
| 296 | f->fmt.pix.width = 720; |
| 297 | f->fmt.pix.height = 576; |
| 298 | h->video_format = SAA6752HS_VF_D1; |
| 299 | } |
| 300 | else if (dist_480 < dist_352) { |
| 301 | f->fmt.pix.width = 480; |
| 302 | f->fmt.pix.height = 576; |
| 303 | h->video_format = SAA6752HS_VF_2_3_D1; |
| 304 | } |
| 305 | else { |
| 306 | f->fmt.pix.width = 352; |
| 307 | if (abs(f->fmt.pix.height - 576) < |
| 308 | abs(f->fmt.pix.height - 288)) { |
| 309 | f->fmt.pix.height = 576; |
| 310 | h->video_format = SAA6752HS_VF_1_2_D1; |
| 311 | } |
| 312 | else { |
| 313 | f->fmt.pix.height = 288; |
| 314 | h->video_format = SAA6752HS_VF_SIF; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | static void saa6752hs_set_params(struct i2c_client* client, |
| 321 | struct v4l2_mpeg_compression* params) |
| 322 | { |
| 323 | struct saa6752hs_state *h = i2c_get_clientdata(client); |
| 324 | |
| 325 | /* check PIDs */ |
| 326 | if (params->ts_pid_pmt <= MPEG_PID_MAX) |
| 327 | h->params.ts_pid_pmt = params->ts_pid_pmt; |
| 328 | if (params->ts_pid_pcr <= MPEG_PID_MAX) |
| 329 | h->params.ts_pid_pcr = params->ts_pid_pcr; |
| 330 | if (params->ts_pid_video <= MPEG_PID_MAX) |
| 331 | h->params.ts_pid_video = params->ts_pid_video; |
| 332 | if (params->ts_pid_audio <= MPEG_PID_MAX) |
| 333 | h->params.ts_pid_audio = params->ts_pid_audio; |
| 334 | |
| 335 | /* check bitrate parameters */ |
| 336 | if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) || |
| 337 | (params->vi_bitrate.mode == V4L2_BITRATE_VBR)) |
| 338 | h->params.vi_bitrate.mode = params->vi_bitrate.mode; |
| 339 | if (params->vi_bitrate.mode != V4L2_BITRATE_NONE) |
| 340 | h->params.st_bitrate.target = params->st_bitrate.target; |
| 341 | if (params->vi_bitrate.mode != V4L2_BITRATE_NONE) |
| 342 | h->params.vi_bitrate.target = params->vi_bitrate.target; |
| 343 | if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) |
| 344 | h->params.vi_bitrate.max = params->vi_bitrate.max; |
| 345 | if (params->au_bitrate.mode != V4L2_BITRATE_NONE) |
| 346 | h->params.au_bitrate.target = params->au_bitrate.target; |
| 347 | |
| 348 | /* aspect ratio */ |
| 349 | if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 || |
| 350 | params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9) |
| 351 | h->params.vi_aspect_ratio = params->vi_aspect_ratio; |
| 352 | |
| 353 | /* range checks */ |
| 354 | if (h->params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX) |
| 355 | h->params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX; |
| 356 | if (h->params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX) |
| 357 | h->params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX; |
| 358 | if (h->params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX) |
| 359 | h->params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX; |
| 360 | if (h->params.au_bitrate.target <= 256) |
| 361 | h->params.au_bitrate.target = 256; |
| 362 | else |
| 363 | h->params.au_bitrate.target = 384; |
| 364 | } |
| 365 | |
| 366 | static int saa6752hs_init(struct i2c_client* client) |
| 367 | { |
| 368 | unsigned char buf[9], buf2[4]; |
| 369 | struct saa6752hs_state *h; |
| 370 | u32 crc; |
| 371 | unsigned char localPAT[256]; |
| 372 | unsigned char localPMT[256]; |
| 373 | |
| 374 | h = i2c_get_clientdata(client); |
| 375 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 376 | /* Set video format - must be done first as it resets other settings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | buf[0] = 0x41; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 378 | buf[1] = h->video_format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | i2c_master_send(client, buf, 2); |
| 380 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 381 | /* Set number of lines in input signal */ |
| 382 | buf[0] = 0x40; |
| 383 | buf[1] = 0x00; |
| 384 | if (h->standard & V4L2_STD_525_60) |
| 385 | buf[1] = 0x01; |
| 386 | i2c_master_send(client, buf, 2); |
| 387 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 388 | /* set bitrate */ |
| 389 | saa6752hs_set_bitrate(client, &h->params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 391 | /* Set GOP structure {3, 13} */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | buf[0] = 0x72; |
| 393 | buf[1] = 0x03; |
| 394 | buf[2] = 0x0D; |
| 395 | i2c_master_send(client,buf,3); |
| 396 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 397 | /* Set minimum Q-scale {4} */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | buf[0] = 0x82; |
| 399 | buf[1] = 0x04; |
| 400 | i2c_master_send(client,buf,2); |
| 401 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 402 | /* Set maximum Q-scale {12} */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | buf[0] = 0x83; |
| 404 | buf[1] = 0x0C; |
| 405 | i2c_master_send(client,buf,2); |
| 406 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 407 | /* Set Output Protocol */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | buf[0] = 0xD0; |
| 409 | buf[1] = 0x81; |
| 410 | i2c_master_send(client,buf,2); |
| 411 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 412 | /* Set video output stream format {TS} */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | buf[0] = 0xB0; |
| 414 | buf[1] = 0x05; |
| 415 | i2c_master_send(client,buf,2); |
| 416 | |
| 417 | /* compute PAT */ |
| 418 | memcpy(localPAT, PAT, sizeof(PAT)); |
| 419 | localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f); |
| 420 | localPAT[18] = h->params.ts_pid_pmt & 0xff; |
| 421 | crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4); |
| 422 | localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF; |
| 423 | localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF; |
| 424 | localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF; |
| 425 | localPAT[sizeof(PAT) - 1] = crc & 0xFF; |
| 426 | |
| 427 | /* compute PMT */ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 428 | memcpy(localPMT, PMT, sizeof(PMT)); |
| 429 | localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f); |
| 430 | localPMT[4] = h->params.ts_pid_pmt & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F); |
| 432 | localPMT[16] = h->params.ts_pid_pcr & 0xFF; |
| 433 | localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F); |
| 434 | localPMT[21] = h->params.ts_pid_video & 0xFF; |
| 435 | localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F); |
| 436 | localPMT[26] = h->params.ts_pid_audio & 0xFF; |
| 437 | crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4); |
| 438 | localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF; |
| 439 | localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF; |
| 440 | localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF; |
| 441 | localPMT[sizeof(PMT) - 1] = crc & 0xFF; |
| 442 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 443 | /* Set Audio PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | buf[0] = 0xC1; |
| 445 | buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF; |
| 446 | buf[2] = h->params.ts_pid_audio & 0xFF; |
| 447 | i2c_master_send(client,buf,3); |
| 448 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 449 | /* Set Video PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | buf[0] = 0xC0; |
| 451 | buf[1] = (h->params.ts_pid_video >> 8) & 0xFF; |
| 452 | buf[2] = h->params.ts_pid_video & 0xFF; |
| 453 | i2c_master_send(client,buf,3); |
| 454 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 455 | /* Set PCR PID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | buf[0] = 0xC4; |
| 457 | buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF; |
| 458 | buf[2] = h->params.ts_pid_pcr & 0xFF; |
| 459 | i2c_master_send(client,buf,3); |
| 460 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 461 | /* Send SI tables */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | i2c_master_send(client,localPAT,sizeof(PAT)); |
| 463 | i2c_master_send(client,localPMT,sizeof(PMT)); |
| 464 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 465 | /* mute then unmute audio. This removes buzzing artefacts */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | buf[0] = 0xa4; |
| 467 | buf[1] = 1; |
| 468 | i2c_master_send(client, buf, 2); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 469 | buf[1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | i2c_master_send(client, buf, 2); |
| 471 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 472 | /* start it going */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | saa6752hs_chip_command(client, SAA6752HS_COMMAND_START); |
| 474 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 475 | /* readout current state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | buf[0] = 0xE1; |
| 477 | buf[1] = 0xA7; |
| 478 | buf[2] = 0xFE; |
| 479 | buf[3] = 0x82; |
| 480 | buf[4] = 0xB0; |
| 481 | i2c_master_send(client, buf, 5); |
| 482 | i2c_master_recv(client, buf2, 4); |
| 483 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 484 | /* change aspect ratio */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | buf[0] = 0xE0; |
| 486 | buf[1] = 0xA7; |
| 487 | buf[2] = 0xFE; |
| 488 | buf[3] = 0x82; |
| 489 | buf[4] = 0xB0; |
| 490 | buf[5] = buf2[0]; |
| 491 | switch(h->params.vi_aspect_ratio) { |
| 492 | case V4L2_MPEG_ASPECT_16_9: |
| 493 | buf[6] = buf2[1] | 0x40; |
| 494 | break; |
| 495 | case V4L2_MPEG_ASPECT_4_3: |
| 496 | default: |
| 497 | buf[6] = buf2[1] & 0xBF; |
| 498 | break; |
| 499 | break; |
| 500 | } |
| 501 | buf[7] = buf2[2]; |
| 502 | buf[8] = buf2[3]; |
| 503 | i2c_master_send(client, buf, 9); |
| 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind) |
| 509 | { |
| 510 | struct saa6752hs_state *h; |
| 511 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 512 | printk("saa6752hs: chip found @ 0x%x\n", addr<<1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 514 | if (NULL == (h = kmalloc(sizeof(*h), GFP_KERNEL))) |
| 515 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | memset(h,0,sizeof(*h)); |
| 517 | h->client = client_template; |
| 518 | h->params = param_defaults; |
| 519 | h->client.adapter = adap; |
| 520 | h->client.addr = addr; |
| 521 | |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 522 | /* Assume 625 input lines */ |
| 523 | h->standard = 0; |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | i2c_set_clientdata(&h->client, h); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 526 | i2c_attach_client(&h->client); |
Mauro Carvalho Chehab | 674434c | 2005-12-12 00:37:28 -0800 | [diff] [blame] | 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int saa6752hs_probe(struct i2c_adapter *adap) |
| 532 | { |
| 533 | if (adap->class & I2C_CLASS_TV_ANALOG) |
| 534 | return i2c_probe(adap, &addr_data, saa6752hs_attach); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static int saa6752hs_detach(struct i2c_client *client) |
| 539 | { |
| 540 | struct saa6752hs_state *h; |
| 541 | |
| 542 | h = i2c_get_clientdata(client); |
| 543 | i2c_detach_client(client); |
| 544 | kfree(h); |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int |
| 549 | saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg) |
| 550 | { |
| 551 | struct saa6752hs_state *h = i2c_get_clientdata(client); |
| 552 | struct v4l2_mpeg_compression *params = arg; |
| 553 | int err = 0; |
| 554 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 555 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | case VIDIOC_S_MPEGCOMP: |
| 557 | if (NULL == params) { |
| 558 | /* apply settings and start encoder */ |
| 559 | saa6752hs_init(client); |
| 560 | break; |
| 561 | } |
| 562 | saa6752hs_set_params(client, params); |
| 563 | /* fall through */ |
| 564 | case VIDIOC_G_MPEGCOMP: |
| 565 | *params = h->params; |
| 566 | break; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 567 | case VIDIOC_G_FMT: |
| 568 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 569 | struct v4l2_format *f = arg; |
Frederic CAND | 0a4c9c9 | 2005-05-05 16:15:52 -0700 | [diff] [blame] | 570 | |
| 571 | if (h->video_format == SAA6752HS_VF_UNKNOWN) |
| 572 | h->video_format = SAA6752HS_VF_D1; |
| 573 | f->fmt.pix.width = |
| 574 | v4l2_format_table[h->video_format].fmt.pix.width; |
| 575 | f->fmt.pix.height = |
| 576 | v4l2_format_table[h->video_format].fmt.pix.height; |
| 577 | break ; |
| 578 | } |
| 579 | case VIDIOC_S_FMT: |
| 580 | { |
| 581 | struct v4l2_format *f = arg; |
| 582 | |
| 583 | saa6752hs_set_subsampling(client, f); |
| 584 | break; |
| 585 | } |
Robert W. Boone | 9b71521 | 2005-11-08 21:36:45 -0800 | [diff] [blame] | 586 | case VIDIOC_S_STD: |
| 587 | h->standard = *((v4l2_std_id *) arg); |
| 588 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | default: |
| 590 | /* nothing */ |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | return err; |
| 595 | } |
| 596 | |
| 597 | /* ----------------------------------------------------------------------- */ |
| 598 | |
| 599 | static struct i2c_driver driver = { |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 600 | .driver = { |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 601 | .name = "i2c saa6752hs MPEG encoder", |
| 602 | }, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 603 | .id = I2C_DRIVERID_SAA6752HS, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 604 | .attach_adapter = saa6752hs_probe, |
| 605 | .detach_client = saa6752hs_detach, |
| 606 | .command = saa6752hs_command, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | }; |
| 608 | |
| 609 | static struct i2c_client client_template = |
| 610 | { |
Jean Delvare | fae91e7 | 2005-08-15 19:57:04 +0200 | [diff] [blame] | 611 | .name = "saa6752hs", |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 612 | .driver = &driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | static int __init saa6752hs_init_module(void) |
| 616 | { |
| 617 | return i2c_add_driver(&driver); |
| 618 | } |
| 619 | |
| 620 | static void __exit saa6752hs_cleanup_module(void) |
| 621 | { |
| 622 | i2c_del_driver(&driver); |
| 623 | } |
| 624 | |
| 625 | module_init(saa6752hs_init_module); |
| 626 | module_exit(saa6752hs_cleanup_module); |
| 627 | |
| 628 | /* |
| 629 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 630 | * --------------------------------------------------------------------------- |
| 631 | * Local variables: |
| 632 | * c-basic-offset: 8 |
| 633 | * End: |
| 634 | */ |