patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / media / video / saa7134 / saa6752hs.c
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
15 #include <media/id.h>
16 #include <media/saa6752hs.h>
17
18 /* Addresses to scan */
19 static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
20 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
21 I2C_CLIENT_INSMOD;
22
23 MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
24 MODULE_AUTHOR("Andrew de Quincey");
25 MODULE_LICENSE("GPL");
26
27 static struct i2c_driver driver;
28 static struct i2c_client client_template;
29
30
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,
39     
40         SAA6752HS_COMMAND_MAX
41 };
42
43
44 /* ---------------------------------------------------------------------- */
45
46 static u8 PAT[] = {
47         0xc2, // i2c register
48         0x00, // table number for encoder
49   
50         0x47, // sync
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)
53      
54         0x00, // PSI pointer to start of table
55     
56         0x00, // tid(0)
57         0xb0, 0x0d, // section_syntax_indicator(1), section_length(13)
58     
59         0x00, 0x01, // transport_stream_id(1)
60     
61         0xc1, // version_number(0), current_next_indicator(1)
62     
63         0x00, 0x00, // section_number(0), last_section_number(0)
64
65         0x00, 0x01, // program_number(1)
66         
67         0xe0, 0x10, // PMT PID(0x10)
68
69         0x76, 0xf1, 0x44, 0xd1 // CRC32
70 };
71
72 static u8 PMT[] = {
73         0xc2, // i2c register
74         0x01, // table number for encoder
75   
76         0x47, // sync
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)
79      
80         0x00, // PSI pointer to start of table
81     
82         0x02, // tid(2)
83         0xb0, 0x17, // section_syntax_indicator(1), section_length(23)
84
85         0x00, 0x01, // program_number(1)
86     
87         0xc1, // version_number(0), current_next_indicator(1)
88     
89         0x00, 0x00, // section_number(0), last_section_number(0)
90     
91         0xe1, 0x04, // PCR_PID (0x104)
92    
93         0xf0, 0x00, // program_info_length(0)
94     
95         0x02, 0xe1, 0x00, 0xf0, 0x00, // video stream type(2), pid(0x100)
96         0x04, 0xe1, 0x03, 0xf0, 0x00, // audio stream type(4), pid(0x103)
97     
98         0xa1, 0xca, 0x0f, 0x82 // CRC32
99 };
100
101 static struct mpeg_params mpeg_params_template =
102 {
103         .bitrate_mode = MPEG_BITRATE_MODE_CBR,
104         .video_target_bitrate = 5000,
105         .audio_bitrate = MPEG_AUDIO_BITRATE_256,
106         .total_bitrate = 6000,
107 };
108
109   
110 /* ---------------------------------------------------------------------- */
111
112
113 static int saa6752hs_chip_command(struct i2c_client* client,
114                                   enum saa6752hs_command command)
115 {
116         unsigned char buf[3];
117         unsigned long timeout;
118         int status = 0;
119
120         // execute the command
121         switch(command) {
122         case SAA6752HS_COMMAND_RESET:
123                 buf[0] = 0x00;
124                 break;
125           
126         case SAA6752HS_COMMAND_STOP:
127                         buf[0] = 0x03;
128                 break;
129           
130         case SAA6752HS_COMMAND_START:
131                 buf[0] = 0x02;
132                 break;
133
134         case SAA6752HS_COMMAND_PAUSE:
135                 buf[0] = 0x04;
136                 break;
137           
138         case SAA6752HS_COMMAND_RECONFIGURE:
139                 buf[0] = 0x05;
140                 break;
141           
142         case SAA6752HS_COMMAND_SLEEP:
143                 buf[0] = 0x06;
144                 break;
145
146         case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
147                 buf[0] = 0x07;
148                 break;
149         
150         default:
151                 return -EINVAL;  
152         }
153         
154         // set it and wait for it to be so
155         i2c_master_send(client, buf, 1);
156         timeout = jiffies + HZ * 3;
157         for (;;) {
158                 // get the current status
159                 buf[0] = 0x10;
160                 i2c_master_send(client, buf, 1);
161                 i2c_master_recv(client, buf, 1);
162
163                 if (!(buf[0] & 0x20))
164                         break;
165                 if (time_after(jiffies,timeout)) {
166                         status = -ETIMEDOUT;
167                         break;
168                 }
169         
170                 // wait a bit
171                 set_current_state(TASK_INTERRUPTIBLE);
172                 schedule_timeout(HZ/100);
173         }
174
175         // delay a bit to let encoder settle
176         set_current_state(TASK_INTERRUPTIBLE);
177         schedule_timeout(HZ/20);
178         
179         // done
180         return status;
181 }
182
183
184 static int saa6752hs_set_bitrate(struct i2c_client* client,
185                                  struct mpeg_params* params)
186 {
187         u8 buf[3];
188   
189         // set the bitrate mode
190         buf[0] = 0x71;
191         buf[1] = params->bitrate_mode;
192         i2c_master_send(client, buf, 2);
193           
194         // set the video bitrate
195         if (params->bitrate_mode == MPEG_BITRATE_MODE_VBR) {
196                 // set the target bitrate
197                 buf[0] = 0x80;
198                 buf[1] = params->video_target_bitrate >> 8;
199                 buf[2] = params->video_target_bitrate & 0xff;
200                 i2c_master_send(client, buf, 3);
201
202                 // set the max bitrate
203                 buf[0] = 0x81;
204                 buf[1] = params->video_max_bitrate >> 8;
205                 buf[2] = params->video_max_bitrate & 0xff;
206                 i2c_master_send(client, buf, 3);
207         } else {
208                 // set the target bitrate (no max bitrate for CBR)
209                 buf[0] = 0x81;
210                 buf[1] = params->video_target_bitrate >> 8;
211                 buf[2] = params->video_target_bitrate & 0xff;
212                 i2c_master_send(client, buf, 3);
213         }
214           
215         // set the audio bitrate
216         buf[0] = 0x94;
217         buf[1] = params->audio_bitrate;
218         i2c_master_send(client, buf, 2);
219         
220         // set the total bitrate
221         buf[0] = 0xb1;
222         buf[1] = params->total_bitrate >> 8;
223         buf[2] = params->total_bitrate & 0xff;
224         i2c_master_send(client, buf, 3);
225   
226         return 0;
227 }
228
229
230 static int saa6752hs_init(struct i2c_client* client, struct mpeg_params* params)
231 {  
232         unsigned char buf[3];
233         void *data;
234
235         // check the bitrate parameters first
236         if (params != NULL) {
237                 if (params->bitrate_mode >= MPEG_BITRATE_MODE_MAX)
238                         return -EINVAL;
239                 if (params->video_target_bitrate >= MPEG_VIDEO_TARGET_BITRATE_MAX)
240                         return -EINVAL;
241                 if (params->video_max_bitrate >= MPEG_VIDEO_MAX_BITRATE_MAX)
242                         return -EINVAL;
243                 if (params->audio_bitrate >= MPEG_AUDIO_BITRATE_MAX)
244                         return -EINVAL;
245                 if (params->total_bitrate >= MPEG_TOTAL_BITRATE_MAX)
246                         return -EINVAL;
247                 if (params->bitrate_mode         == MPEG_BITRATE_MODE_MAX &&
248                     params->video_target_bitrate <= params->video_max_bitrate)
249                         return -EINVAL; 
250         }
251   
252         // Set GOP structure {3, 13}
253         buf[0] = 0x72;
254         buf[1] = 0x03;
255         buf[2] = 0x0D;
256         i2c_master_send(client,buf,3);
257   
258         // Set minimum Q-scale {4}
259         buf[0] = 0x82;
260         buf[1] = 0x04;
261         i2c_master_send(client,buf,2);
262   
263         // Set maximum Q-scale {12}
264         buf[0] = 0x83;
265         buf[1] = 0x0C;
266         i2c_master_send(client,buf,2);
267   
268         // Set Output Protocol
269         buf[0] = 0xD0;
270         buf[1] = 0x01;
271         i2c_master_send(client,buf,2);
272   
273         // Set video output stream format {TS}
274         buf[0] = 0xB0;
275         buf[1] = 0x05;
276         i2c_master_send(client,buf,2);
277   
278         // Set Audio PID {0x103}
279         buf[0] = 0xC1;
280         buf[1] = 0x01;
281         buf[2] = 0x03;
282         i2c_master_send(client,buf,3);
283   
284         // setup bitrate settings
285         data = i2c_get_clientdata(client);
286         if (params) {
287                 saa6752hs_set_bitrate(client, params);
288                 memcpy(data, params, sizeof(struct mpeg_params));
289         } else {
290                 // parameters were not supplied. use the previous set
291                 saa6752hs_set_bitrate(client, (struct mpeg_params*) data);
292         }
293           
294         // Send SI tables
295         i2c_master_send(client,PAT,sizeof(PAT));
296         i2c_master_send(client,PMT,sizeof(PMT));
297           
298         // mute then unmute audio. This removes buzzing artefacts
299         buf[0] = 0xa4;
300         buf[1] = 1;
301         i2c_master_send(client, buf, 2);
302         buf[1] = 0;
303         i2c_master_send(client, buf, 2);
304           
305         // start it going
306         saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
307
308         return 0;
309 }
310
311 static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
312 {
313         struct i2c_client *client;
314         struct mpeg_params* params;
315
316         client_template.adapter = adap;
317         client_template.addr = addr;
318
319         printk("saa6752hs: chip found @ 0x%x\n", addr<<1);
320
321         if (NULL == (client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL)))
322                 return -ENOMEM;
323         memcpy(client,&client_template,sizeof(struct i2c_client));
324         strlcpy(client->name, "saa6752hs", sizeof(client->name));
325    
326         if (NULL == (params = kmalloc(sizeof(struct mpeg_params), GFP_KERNEL)))
327                 return -ENOMEM;
328         memcpy(params,&mpeg_params_template,sizeof(struct mpeg_params));
329         i2c_set_clientdata(client, params);
330
331         i2c_attach_client(client);
332   
333         return 0;
334 }
335
336 static int saa6752hs_probe(struct i2c_adapter *adap)
337 {
338         if (adap->class & I2C_CLASS_TV_ANALOG)
339                 return i2c_probe(adap, &addr_data, saa6752hs_attach);
340
341         return 0;
342 }
343
344 static int saa6752hs_detach(struct i2c_client *client)
345 {
346         void *data;
347
348         data = i2c_get_clientdata(client);
349         i2c_detach_client(client);
350         kfree(data);
351         kfree(client);
352         return 0;
353 }
354
355 static int
356 saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
357 {
358         struct mpeg_params* init_arg = arg;
359
360         switch (cmd) {
361         case MPEG_SETPARAMS:
362                 return saa6752hs_init(client, init_arg);
363
364         default:
365                 /* nothing */
366                 break;
367         }
368         
369         return 0;
370 }
371
372 /* ----------------------------------------------------------------------- */
373
374 static struct i2c_driver driver = {
375         .owner          = THIS_MODULE,
376         .name           = "i2c saa6752hs MPEG encoder",
377         .id             = I2C_DRIVERID_SAA6752HS,
378         .flags          = I2C_DF_NOTIFY,
379         .attach_adapter = saa6752hs_probe,
380         .detach_client  = saa6752hs_detach,
381         .command        = saa6752hs_command,
382 };
383
384 static struct i2c_client client_template =
385 {
386         I2C_DEVNAME("(saa6752hs unset)"),
387         .flags      = I2C_CLIENT_ALLOW_USE,
388         .driver     = &driver,
389 };
390
391 static int saa6752hs_init_module(void)
392 {
393         i2c_add_driver(&driver);
394         return 0;
395 }
396
397 static void saa6752hs_cleanup_module(void)
398 {
399         i2c_del_driver(&driver);
400 }
401
402 module_init(saa6752hs_init_module);
403 module_exit(saa6752hs_cleanup_module);
404
405 /*
406  * Overrides for Emacs so that we follow Linus's tabbing style.
407  * ---------------------------------------------------------------------------
408  * Local variables:
409  * c-basic-offset: 8
410  * End:
411  */