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>
16 #include <media/saa6752hs.h>
18 /* Addresses to scan */
19 static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
20 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
23 MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
24 MODULE_AUTHOR("Andrew de Quincey");
25 MODULE_LICENSE("GPL");
27 static struct i2c_driver driver;
28 static struct i2c_client client_template;
31 enum saa6752hs_command {
32 SAA6752HS_COMMAND_RESET = 0,
33 SAA6752HS_COMMAND_STOP = 1,
34 SAA6752HS_COMMAND_START = 2,
35 SAA6752HS_COMMAND_PAUSE = 3,
36 SAA6752HS_COMMAND_RECONFIGURE = 4,
37 SAA6752HS_COMMAND_SLEEP = 5,
38 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
44 /* ---------------------------------------------------------------------- */
48 0x00, // table number for encoder
51 0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0)
52 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
54 0x00, // PSI pointer to start of table
57 0xb0, 0x0d, // section_syntax_indicator(1), section_length(13)
59 0x00, 0x01, // transport_stream_id(1)
61 0xc1, // version_number(0), current_next_indicator(1)
63 0x00, 0x00, // section_number(0), last_section_number(0)
65 0x00, 0x01, // program_number(1)
67 0xe0, 0x10, // PMT PID(0x10)
69 0x76, 0xf1, 0x44, 0xd1 // CRC32
74 0x01, // table number for encoder
77 0x40, 0x10, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0x10)
78 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0)
80 0x00, // PSI pointer to start of table
83 0xb0, 0x17, // section_syntax_indicator(1), section_length(23)
85 0x00, 0x01, // program_number(1)
87 0xc1, // version_number(0), current_next_indicator(1)
89 0x00, 0x00, // section_number(0), last_section_number(0)
91 0xe1, 0x04, // PCR_PID (0x104)
93 0xf0, 0x00, // program_info_length(0)
95 0x02, 0xe1, 0x00, 0xf0, 0x00, // video stream type(2), pid(0x100)
96 0x04, 0xe1, 0x03, 0xf0, 0x00, // audio stream type(4), pid(0x103)
98 0xa1, 0xca, 0x0f, 0x82 // CRC32
101 static struct mpeg_params mpeg_params_template =
103 .bitrate_mode = MPEG_BITRATE_MODE_CBR,
104 .video_target_bitrate = 5000,
105 .audio_bitrate = MPEG_AUDIO_BITRATE_256,
106 .total_bitrate = 6000,
110 /* ---------------------------------------------------------------------- */
113 static int saa6752hs_chip_command(struct i2c_client* client,
114 enum saa6752hs_command command)
116 unsigned char buf[3];
117 unsigned long timeout;
120 // execute the command
122 case SAA6752HS_COMMAND_RESET:
126 case SAA6752HS_COMMAND_STOP:
130 case SAA6752HS_COMMAND_START:
134 case SAA6752HS_COMMAND_PAUSE:
138 case SAA6752HS_COMMAND_RECONFIGURE:
142 case SAA6752HS_COMMAND_SLEEP:
146 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
154 // set it and wait for it to be so
155 i2c_master_send(client, buf, 1);
156 timeout = jiffies + HZ * 3;
158 // get the current status
160 i2c_master_send(client, buf, 1);
161 i2c_master_recv(client, buf, 1);
163 if (!(buf[0] & 0x20))
165 if (time_after(jiffies,timeout)) {
174 // delay a bit to let encoder settle
182 static int saa6752hs_set_bitrate(struct i2c_client* client,
183 struct mpeg_params* params)
187 // set the bitrate mode
189 buf[1] = params->bitrate_mode;
190 i2c_master_send(client, buf, 2);
192 // set the video bitrate
193 if (params->bitrate_mode == MPEG_BITRATE_MODE_VBR) {
194 // set the target bitrate
196 buf[1] = params->video_target_bitrate >> 8;
197 buf[2] = params->video_target_bitrate & 0xff;
198 i2c_master_send(client, buf, 3);
200 // set the max bitrate
202 buf[1] = params->video_max_bitrate >> 8;
203 buf[2] = params->video_max_bitrate & 0xff;
204 i2c_master_send(client, buf, 3);
206 // set the target bitrate (no max bitrate for CBR)
208 buf[1] = params->video_target_bitrate >> 8;
209 buf[2] = params->video_target_bitrate & 0xff;
210 i2c_master_send(client, buf, 3);
213 // set the audio bitrate
215 buf[1] = params->audio_bitrate;
216 i2c_master_send(client, buf, 2);
218 // set the total bitrate
220 buf[1] = params->total_bitrate >> 8;
221 buf[2] = params->total_bitrate & 0xff;
222 i2c_master_send(client, buf, 3);
228 static int saa6752hs_init(struct i2c_client* client, struct mpeg_params* params)
230 unsigned char buf[3];
233 // check the bitrate parameters first
234 if (params != NULL) {
235 if (params->bitrate_mode >= MPEG_BITRATE_MODE_MAX)
237 if (params->video_target_bitrate >= MPEG_VIDEO_TARGET_BITRATE_MAX)
239 if (params->video_max_bitrate >= MPEG_VIDEO_MAX_BITRATE_MAX)
241 if (params->audio_bitrate >= MPEG_AUDIO_BITRATE_MAX)
243 if (params->total_bitrate >= MPEG_TOTAL_BITRATE_MAX)
245 if (params->bitrate_mode == MPEG_BITRATE_MODE_MAX &&
246 params->video_target_bitrate <= params->video_max_bitrate)
250 // Set GOP structure {3, 13}
254 i2c_master_send(client,buf,3);
256 // Set minimum Q-scale {4}
259 i2c_master_send(client,buf,2);
261 // Set maximum Q-scale {12}
264 i2c_master_send(client,buf,2);
266 // Set Output Protocol
269 i2c_master_send(client,buf,2);
271 // Set video output stream format {TS}
274 i2c_master_send(client,buf,2);
276 // Set Audio PID {0x103}
280 i2c_master_send(client,buf,3);
282 // setup bitrate settings
283 data = i2c_get_clientdata(client);
285 saa6752hs_set_bitrate(client, params);
286 memcpy(data, params, sizeof(struct mpeg_params));
288 // parameters were not supplied. use the previous set
289 saa6752hs_set_bitrate(client, (struct mpeg_params*) data);
293 i2c_master_send(client,PAT,sizeof(PAT));
294 i2c_master_send(client,PMT,sizeof(PMT));
296 // mute then unmute audio. This removes buzzing artefacts
299 i2c_master_send(client, buf, 2);
301 i2c_master_send(client, buf, 2);
304 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
309 static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
311 struct i2c_client *client;
312 struct mpeg_params* params;
314 client_template.adapter = adap;
315 client_template.addr = addr;
317 printk("saa6752hs: chip found @ 0x%x\n", addr<<1);
319 if (NULL == (client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL)))
321 memcpy(client,&client_template,sizeof(struct i2c_client));
322 strlcpy(client->name, "saa6752hs", sizeof(client->name));
324 if (NULL == (params = kmalloc(sizeof(struct mpeg_params), GFP_KERNEL)))
326 memcpy(params,&mpeg_params_template,sizeof(struct mpeg_params));
327 i2c_set_clientdata(client, params);
329 i2c_attach_client(client);
334 static int saa6752hs_probe(struct i2c_adapter *adap)
336 if (adap->class & I2C_CLASS_TV_ANALOG)
337 return i2c_probe(adap, &addr_data, saa6752hs_attach);
341 static int saa6752hs_detach(struct i2c_client *client)
345 data = i2c_get_clientdata(client);
346 i2c_detach_client(client);
353 saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
355 struct mpeg_params* init_arg = arg;
359 return saa6752hs_init(client, init_arg);
369 /* ----------------------------------------------------------------------- */
371 static struct i2c_driver driver = {
372 .owner = THIS_MODULE,
373 .name = "i2c saa6752hs MPEG encoder",
374 .id = I2C_DRIVERID_SAA6752HS,
375 .flags = I2C_DF_NOTIFY,
376 .attach_adapter = saa6752hs_probe,
377 .detach_client = saa6752hs_detach,
378 .command = saa6752hs_command,
381 static struct i2c_client client_template =
383 I2C_DEVNAME("(saa6752hs unset)"),
384 .flags = I2C_CLIENT_ALLOW_USE,
388 static int __init saa6752hs_init_module(void)
390 return i2c_add_driver(&driver);
393 static void __exit saa6752hs_cleanup_module(void)
395 i2c_del_driver(&driver);
398 module_init(saa6752hs_init_module);
399 module_exit(saa6752hs_cleanup_module);
402 * Overrides for Emacs so that we follow Linus's tabbing style.
403 * ---------------------------------------------------------------------------