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