vserver 1.9.3
[linux-2.6.git] / drivers / media / video / dpc7146.c
1 /*
2     dpc7146.c - v4l2 driver for the dpc7146 demonstration board
3     
4     Copyright (C) 2000-2003 Michael Hunold <michael@mihu.de>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #define DEBUG_VARIABLE debug
22
23 #include <media/saa7146_vv.h>
24 #include <linux/video_decoder.h>        /* for saa7111a */
25
26 #define I2C_SAA7111A            0x24
27
28 /* All unused bytes are reserverd. */
29 #define SAA711X_CHIP_VERSION            0x00
30 #define SAA711X_ANALOG_INPUT_CONTROL_1  0x02
31 #define SAA711X_ANALOG_INPUT_CONTROL_2  0x03
32 #define SAA711X_ANALOG_INPUT_CONTROL_3  0x04
33 #define SAA711X_ANALOG_INPUT_CONTROL_4  0x05
34 #define SAA711X_HORIZONTAL_SYNC_START   0x06
35 #define SAA711X_HORIZONTAL_SYNC_STOP    0x07
36 #define SAA711X_SYNC_CONTROL            0x08
37 #define SAA711X_LUMINANCE_CONTROL       0x09
38 #define SAA711X_LUMINANCE_BRIGHTNESS    0x0A
39 #define SAA711X_LUMINANCE_CONTRAST      0x0B
40 #define SAA711X_CHROMA_SATURATION       0x0C
41 #define SAA711X_CHROMA_HUE_CONTROL      0x0D
42 #define SAA711X_CHROMA_CONTROL          0x0E
43 #define SAA711X_FORMAT_DELAY_CONTROL    0x10
44 #define SAA711X_OUTPUT_CONTROL_1        0x11
45 #define SAA711X_OUTPUT_CONTROL_2        0x12
46 #define SAA711X_OUTPUT_CONTROL_3        0x13
47 #define SAA711X_V_GATE_1_START          0x15
48 #define SAA711X_V_GATE_1_STOP           0x16
49 #define SAA711X_V_GATE_1_MSB            0x17
50 #define SAA711X_TEXT_SLICER_STATUS      0x1A
51 #define SAA711X_DECODED_BYTES_OF_TS_1   0x1B
52 #define SAA711X_DECODED_BYTES_OF_TS_2   0x1C
53 #define SAA711X_STATUS_BYTE             0x1F
54
55 #define DPC_BOARD_CAN_DO_VBI(dev)   (dev->revision != 0) 
56
57 static int debug = 0;
58 MODULE_PARM(debug,"i");
59 MODULE_PARM_DESC(debug, "debug verbosity");
60
61 /* global variables */
62 int dpc_num = 0;
63
64 #define DPC_INPUTS      2
65 static struct v4l2_input dpc_inputs[DPC_INPUTS] = {
66         { 0, "Port A",  V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0 },
67         { 1, "Port B",  V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0 },
68 };
69
70 #define DPC_AUDIOS      0
71
72 static struct saa7146_extension_ioctls ioctls[] = {
73         { VIDIOC_G_INPUT,       SAA7146_EXCLUSIVE },
74         { VIDIOC_S_INPUT,       SAA7146_EXCLUSIVE },
75         { VIDIOC_ENUMINPUT,     SAA7146_EXCLUSIVE },
76         { VIDIOC_S_STD,         SAA7146_AFTER },
77         { 0,                    0 }
78 };
79
80 struct dpc
81 {
82         struct video_device     video_dev;
83         struct video_device     vbi_dev;
84
85         struct i2c_adapter      i2c_adapter;    
86         struct i2c_client       *saa7111a;
87         
88         int cur_input;  /* current input */
89 };
90
91 /* fixme: add vbi stuff here */
92 static int dpc_probe(struct saa7146_dev* dev)
93 {
94         struct dpc* dpc = NULL; 
95         struct i2c_client *client;
96         struct list_head *item;
97
98         dpc = (struct dpc*)kmalloc(sizeof(struct dpc), GFP_KERNEL);
99         if( NULL == dpc ) {
100                 printk("dpc_v4l2.o: dpc_probe: not enough kernel memory.\n");
101                 return -ENOMEM;
102         }
103         memset(dpc, 0x0, sizeof(struct dpc));   
104
105         /* FIXME: enable i2c-port pins, video-port-pins
106            video port pins should be enabled here ?! */
107         saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
108
109         saa7146_i2c_adapter_prepare(dev, &dpc->i2c_adapter, I2C_CLASS_TV_ANALOG, SAA7146_I2C_BUS_BIT_RATE_480);
110         if(i2c_add_adapter(&dpc->i2c_adapter) < 0) {
111                 DEB_S(("cannot register i2c-device. skipping.\n"));
112                 kfree(dpc);
113                 return -EFAULT;
114         }
115
116         /* loop through all i2c-devices on the bus and look who is there */
117         list_for_each(item,&dpc->i2c_adapter.clients) {
118                 client = list_entry(item, struct i2c_client, list);
119                 if( I2C_SAA7111A == client->addr ) 
120                         dpc->saa7111a = client;
121         }
122
123         /* check if all devices are present */
124         if( 0 == dpc->saa7111a ) {
125                 DEB_D(("dpc_v4l2.o: dpc_attach failed for this device.\n"));    
126                 i2c_del_adapter(&dpc->i2c_adapter);
127                 kfree(dpc);
128                 return -ENODEV;
129         }
130         
131         /* all devices are present, probe was successful */     
132         DEB_D(("dpc_v4l2.o: dpc_probe succeeded for this device.\n"));  
133
134         /* we store the pointer in our private data field */
135         dev->ext_priv = dpc;
136
137         return 0;
138 }
139
140 /* bring hardware to a sane state. this has to be done, just in case someone
141    wants to capture from this device before it has been properly initialized.
142    the capture engine would badly fail, because no valid signal arrives on the
143    saa7146, thus leading to timeouts and stuff. */
144 static int dpc_init_done(struct saa7146_dev* dev)
145 {
146         struct dpc* dpc = (struct dpc*)dev->ext_priv;
147
148         DEB_D(("dpc_v4l2.o: dpc_init_done called.\n"));
149
150         /* initialize the helper ics to useful values */
151         i2c_smbus_write_byte_data(dpc->saa7111a, 0x00, 0x11);
152
153         i2c_smbus_write_byte_data(dpc->saa7111a, 0x02, 0xc0);
154         i2c_smbus_write_byte_data(dpc->saa7111a, 0x03, 0x30);
155         i2c_smbus_write_byte_data(dpc->saa7111a, 0x04, 0x00);
156         i2c_smbus_write_byte_data(dpc->saa7111a, 0x05, 0x00);
157         i2c_smbus_write_byte_data(dpc->saa7111a, 0x06, 0xde);
158         i2c_smbus_write_byte_data(dpc->saa7111a, 0x07, 0xad);
159         i2c_smbus_write_byte_data(dpc->saa7111a, 0x08, 0xa8);
160         i2c_smbus_write_byte_data(dpc->saa7111a, 0x09, 0x00);
161         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0a, 0x80);
162         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0b, 0x47);
163         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0c, 0x40);
164         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0d, 0x00);
165         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0e, 0x03);
166
167         i2c_smbus_write_byte_data(dpc->saa7111a, 0x10, 0xd0);
168         i2c_smbus_write_byte_data(dpc->saa7111a, 0x11, 0x1c);
169         i2c_smbus_write_byte_data(dpc->saa7111a, 0x12, 0xc1);
170         i2c_smbus_write_byte_data(dpc->saa7111a, 0x13, 0x30);
171
172         i2c_smbus_write_byte_data(dpc->saa7111a, 0x1f, 0x81);
173
174         return 0;
175 }
176
177 static struct saa7146_ext_vv vv_data;
178
179 /* this function only gets called when the probing was successful */
180 static int dpc_attach(struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
181 {
182         struct dpc* dpc = (struct dpc*)dev->ext_priv;
183         
184         DEB_D(("dpc_v4l2.o: dpc_attach called.\n"));
185
186         /* checking for i2c-devices can be omitted here, because we
187            already did this in "dpc_vl42_probe" */
188
189         saa7146_vv_init(dev,&vv_data);
190         if( 0 != saa7146_register_device(&dpc->video_dev, dev, "dpc", VFL_TYPE_GRABBER)) {
191                 ERR(("cannot register capture v4l2 device. skipping.\n"));
192                 return -1;
193         }
194         
195         /* initialization stuff (vbi) (only for revision > 0 and for extensions which want it)*/
196         if( 0 != DPC_BOARD_CAN_DO_VBI(dev)) {
197                 if( 0 != saa7146_register_device(&dpc->vbi_dev, dev, "dpc", VFL_TYPE_VBI)) {
198                         ERR(("cannot register vbi v4l2 device. skipping.\n"));
199                 }
200         }
201
202         i2c_use_client(dpc->saa7111a);
203
204         printk("dpc: found 'dpc7146 demonstration board'-%d.\n",dpc_num);
205         dpc_num++;
206         
207         /* the rest */
208         dpc->cur_input = 0;
209         dpc_init_done(dev);
210         
211         return 0;
212 }
213
214 static int dpc_detach(struct saa7146_dev* dev)
215 {
216         struct dpc* dpc = (struct dpc*)dev->ext_priv;
217         
218         DEB_EE(("dev:%p\n",dev));
219
220         i2c_release_client(dpc->saa7111a);
221
222         saa7146_unregister_device(&dpc->video_dev,dev);
223         if( 0 != DPC_BOARD_CAN_DO_VBI(dev)) {
224                 saa7146_unregister_device(&dpc->vbi_dev,dev);
225         }
226         saa7146_vv_release(dev);
227
228         dpc_num--;
229
230         i2c_del_adapter(&dpc->i2c_adapter);
231         kfree(dpc);
232         return 0;
233 }
234
235 #ifdef axa
236 int dpc_vbi_bypass(struct saa7146_dev* dev)
237 {
238         struct dpc* dpc = (struct dpc*)dev->ext_priv;
239         
240         int i = 1;
241
242         /* switch bypass in saa7111a */
243         if ( 0 != dpc->saa7111a->driver->command(dpc->saa7111a,SAA711X_VBI_BYPASS, &i)) {
244                 printk("dpc_v4l2.o: VBI_BYPASS: could not address saa7111a.\n");
245                 return -1;
246         }                       
247
248         return 0;
249 }
250 #endif
251
252 static int dpc_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg) 
253 {
254         struct saa7146_dev *dev = fh->dev;
255         struct dpc* dpc = (struct dpc*)dev->ext_priv;
256 /*
257         struct saa7146_vv *vv = dev->vv_data; 
258 */
259         switch(cmd)
260         {
261         case VIDIOC_ENUMINPUT:
262         {
263                 struct v4l2_input *i = arg;
264                 DEB_EE(("VIDIOC_ENUMINPUT %d.\n",i->index));
265                 
266                 if( i->index < 0 || i->index >= DPC_INPUTS) {
267                         return -EINVAL;
268                 }
269                 
270                 memcpy(i, &dpc_inputs[i->index], sizeof(struct v4l2_input));
271
272                 DEB_D(("dpc_v4l2.o: v4l2_ioctl: VIDIOC_ENUMINPUT %d.\n",i->index));
273                 return 0;
274         }
275         case VIDIOC_G_INPUT:
276         {
277                 int *input = (int *)arg;
278                 *input = dpc->cur_input;
279
280                 DEB_D(("dpc_v4l2.o: VIDIOC_G_INPUT: %d\n",*input));
281                 return 0;
282         }
283         case VIDIOC_S_INPUT:
284         {
285                 int     input = *(int *)arg;
286
287                 if (input < 0 || input >= DPC_INPUTS) {
288                         return -EINVAL;
289                 }
290         
291                 dpc->cur_input = input;
292
293                 /* fixme: switch input here, switch audio, too! */
294 //              saa7146_set_hps_source_and_sync(dev, input_port_selection[input].hps_source, input_port_selection[input].hps_sync);
295                 printk("dpc_v4l2.o: VIDIOC_S_INPUT: fixme switch input.\n");
296                 
297                 return 0;
298         }
299         default:
300 /*
301                 DEB_D(("dpc_v4l2.o: v4l2_ioctl does not handle this ioctl.\n"));
302 */
303                 return -ENOIOCTLCMD;
304         }
305         return 0;
306 }
307
308 static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
309 {
310         return 0;
311 }
312
313 static struct saa7146_standard standard[] = {
314         {
315                 .name   = "PAL",        .id     = V4L2_STD_PAL,
316                 .v_offset       = 0x17, .v_field        = 288,
317                 .h_offset       = 0x14, .h_pixels       = 680,
318                 .v_max_out      = 576,  .h_max_out      = 768,
319         }, {
320                 .name   = "NTSC",       .id     = V4L2_STD_NTSC,
321                 .v_offset       = 0x16, .v_field        = 240,
322                 .h_offset       = 0x06, .h_pixels       = 708,
323                 .v_max_out      = 480,  .h_max_out      = 640,
324         }, {
325                 .name   = "SECAM",      .id     = V4L2_STD_SECAM,
326                 .v_offset       = 0x14, .v_field        = 288,
327                 .h_offset       = 0x14, .h_pixels       = 720,
328                 .v_max_out      = 576,  .h_max_out      = 768,
329         }
330 };
331
332 static struct saa7146_extension extension;
333
334 static struct saa7146_pci_extension_data dpc = {
335         .ext_priv = "Multimedia eXtension Board",
336         .ext = &extension,
337 };
338
339 static struct pci_device_id pci_tbl[] = {
340         {
341                 .vendor    = PCI_VENDOR_ID_PHILIPS,
342                 .device    = PCI_DEVICE_ID_PHILIPS_SAA7146,
343                 .subvendor = 0x0000,
344                 .subdevice = 0x0000,
345                 .driver_data = (unsigned long)&dpc,
346         }, {
347                 .vendor = 0,
348         }
349 };
350
351 MODULE_DEVICE_TABLE(pci, pci_tbl);
352
353 static struct saa7146_ext_vv vv_data = {
354         .inputs         = DPC_INPUTS,
355         .capabilities   = V4L2_CAP_VBI_CAPTURE,
356         .stds           = &standard[0],
357         .num_stds       = sizeof(standard)/sizeof(struct saa7146_standard),
358         .std_callback   = &std_callback, 
359         .ioctls         = &ioctls[0],
360         .ioctl          = dpc_ioctl,
361 };
362
363 static struct saa7146_extension extension = {
364         .name           = "dpc7146 demonstration board",
365         .flags          = SAA7146_USE_I2C_IRQ,
366         
367         .pci_tbl        = &pci_tbl[0],
368         .module         = THIS_MODULE,
369
370         .probe          = dpc_probe,
371         .attach         = dpc_attach,
372         .detach         = dpc_detach,
373
374         .irq_mask       = 0,
375         .irq_func       = NULL,
376 };      
377
378 int __init dpc_init_module(void) 
379 {
380         if( 0 != saa7146_register_extension(&extension)) {
381                 DEB_S(("failed to register extension.\n"));
382                 return -ENODEV;
383         }
384         
385         return 0;
386 }
387
388 void __exit dpc_cleanup_module(void) 
389 {
390         saa7146_unregister_extension(&extension);
391 }
392
393 module_init(dpc_init_module);
394 module_exit(dpc_cleanup_module);
395
396 MODULE_DESCRIPTION("video4linux-2 driver for the 'dpc7146 demonstration board'");
397 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
398 MODULE_LICENSE("GPL");