Merge master.kernel.org:/home/rmk/linux-2.6-arm
[sfrench/cifs-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_param(debug, int, 0);
59 MODULE_PARM_DESC(debug, "debug verbosity");
60
61 static int dpc_num = 0;
62
63 #define DPC_INPUTS      2
64 static struct v4l2_input dpc_inputs[DPC_INPUTS] = {
65         { 0, "Port A",  V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0 },
66         { 1, "Port B",  V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0 },
67 };
68
69 #define DPC_AUDIOS      0
70
71 static struct saa7146_extension_ioctls ioctls[] = {
72         { VIDIOC_G_INPUT,       SAA7146_EXCLUSIVE },
73         { VIDIOC_S_INPUT,       SAA7146_EXCLUSIVE },
74         { VIDIOC_ENUMINPUT,     SAA7146_EXCLUSIVE },
75         { VIDIOC_S_STD,         SAA7146_AFTER },
76         { 0,                    0 }
77 };
78
79 struct dpc
80 {
81         struct video_device     *video_dev;
82         struct video_device     *vbi_dev;
83
84         struct i2c_adapter      i2c_adapter;    
85         struct i2c_client       *saa7111a;
86         
87         int cur_input;  /* current input */
88 };
89
90 /* fixme: add vbi stuff here */
91 static int dpc_probe(struct saa7146_dev* dev)
92 {
93         struct dpc* dpc = NULL; 
94         struct i2c_client *client;
95         struct list_head *item;
96
97         dpc = kzalloc(sizeof(struct dpc), GFP_KERNEL);
98         if( NULL == dpc ) {
99                 printk("dpc_v4l2.o: dpc_probe: not enough kernel memory.\n");
100                 return -ENOMEM;
101         }
102
103         /* FIXME: enable i2c-port pins, video-port-pins
104            video port pins should be enabled here ?! */
105         saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
106
107         dpc->i2c_adapter = (struct i2c_adapter) {
108                 .class = I2C_CLASS_TV_ANALOG,
109                 .name = "dpc7146",
110         };
111         saa7146_i2c_adapter_prepare(dev, &dpc->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
112         if(i2c_add_adapter(&dpc->i2c_adapter) < 0) {
113                 DEB_S(("cannot register i2c-device. skipping.\n"));
114                 kfree(dpc);
115                 return -EFAULT;
116         }
117
118         /* loop through all i2c-devices on the bus and look who is there */
119         list_for_each(item,&dpc->i2c_adapter.clients) {
120                 client = list_entry(item, struct i2c_client, list);
121                 if( I2C_SAA7111A == client->addr ) 
122                         dpc->saa7111a = client;
123         }
124
125         /* check if all devices are present */
126         if( 0 == dpc->saa7111a ) {
127                 DEB_D(("dpc_v4l2.o: dpc_attach failed for this device.\n"));    
128                 i2c_del_adapter(&dpc->i2c_adapter);
129                 kfree(dpc);
130                 return -ENODEV;
131         }
132         
133         /* all devices are present, probe was successful */     
134         DEB_D(("dpc_v4l2.o: dpc_probe succeeded for this device.\n"));  
135
136         /* we store the pointer in our private data field */
137         dev->ext_priv = dpc;
138
139         return 0;
140 }
141
142 /* bring hardware to a sane state. this has to be done, just in case someone
143    wants to capture from this device before it has been properly initialized.
144    the capture engine would badly fail, because no valid signal arrives on the
145    saa7146, thus leading to timeouts and stuff. */
146 static int dpc_init_done(struct saa7146_dev* dev)
147 {
148         struct dpc* dpc = (struct dpc*)dev->ext_priv;
149
150         DEB_D(("dpc_v4l2.o: dpc_init_done called.\n"));
151
152         /* initialize the helper ics to useful values */
153         i2c_smbus_write_byte_data(dpc->saa7111a, 0x00, 0x11);
154
155         i2c_smbus_write_byte_data(dpc->saa7111a, 0x02, 0xc0);
156         i2c_smbus_write_byte_data(dpc->saa7111a, 0x03, 0x30);
157         i2c_smbus_write_byte_data(dpc->saa7111a, 0x04, 0x00);
158         i2c_smbus_write_byte_data(dpc->saa7111a, 0x05, 0x00);
159         i2c_smbus_write_byte_data(dpc->saa7111a, 0x06, 0xde);
160         i2c_smbus_write_byte_data(dpc->saa7111a, 0x07, 0xad);
161         i2c_smbus_write_byte_data(dpc->saa7111a, 0x08, 0xa8);
162         i2c_smbus_write_byte_data(dpc->saa7111a, 0x09, 0x00);
163         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0a, 0x80);
164         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0b, 0x47);
165         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0c, 0x40);
166         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0d, 0x00);
167         i2c_smbus_write_byte_data(dpc->saa7111a, 0x0e, 0x03);
168
169         i2c_smbus_write_byte_data(dpc->saa7111a, 0x10, 0xd0);
170         i2c_smbus_write_byte_data(dpc->saa7111a, 0x11, 0x1c);
171         i2c_smbus_write_byte_data(dpc->saa7111a, 0x12, 0xc1);
172         i2c_smbus_write_byte_data(dpc->saa7111a, 0x13, 0x30);
173
174         i2c_smbus_write_byte_data(dpc->saa7111a, 0x1f, 0x81);
175
176         return 0;
177 }
178
179 static struct saa7146_ext_vv vv_data;
180
181 /* this function only gets called when the probing was successful */
182 static int dpc_attach(struct saa7146_dev* dev, struct saa7146_pci_extension_data *info)
183 {
184         struct dpc* dpc = (struct dpc*)dev->ext_priv;
185         
186         DEB_D(("dpc_v4l2.o: dpc_attach called.\n"));
187
188         /* checking for i2c-devices can be omitted here, because we
189            already did this in "dpc_vl42_probe" */
190
191         saa7146_vv_init(dev,&vv_data);
192         if( 0 != saa7146_register_device(&dpc->video_dev, dev, "dpc", VFL_TYPE_GRABBER)) {
193                 ERR(("cannot register capture v4l2 device. skipping.\n"));
194                 return -1;
195         }
196         
197         /* initialization stuff (vbi) (only for revision > 0 and for extensions which want it)*/
198         if( 0 != DPC_BOARD_CAN_DO_VBI(dev)) {
199                 if( 0 != saa7146_register_device(&dpc->vbi_dev, dev, "dpc", VFL_TYPE_VBI)) {
200                         ERR(("cannot register vbi v4l2 device. skipping.\n"));
201                 }
202         }
203
204         i2c_use_client(dpc->saa7111a);
205
206         printk("dpc: found 'dpc7146 demonstration board'-%d.\n",dpc_num);
207         dpc_num++;
208         
209         /* the rest */
210         dpc->cur_input = 0;
211         dpc_init_done(dev);
212         
213         return 0;
214 }
215
216 static int dpc_detach(struct saa7146_dev* dev)
217 {
218         struct dpc* dpc = (struct dpc*)dev->ext_priv;
219         
220         DEB_EE(("dev:%p\n",dev));
221
222         i2c_release_client(dpc->saa7111a);
223
224         saa7146_unregister_device(&dpc->video_dev,dev);
225         if( 0 != DPC_BOARD_CAN_DO_VBI(dev)) {
226                 saa7146_unregister_device(&dpc->vbi_dev,dev);
227         }
228         saa7146_vv_release(dev);
229
230         dpc_num--;
231
232         i2c_del_adapter(&dpc->i2c_adapter);
233         kfree(dpc);
234         return 0;
235 }
236
237 #ifdef axa
238 int dpc_vbi_bypass(struct saa7146_dev* dev)
239 {
240         struct dpc* dpc = (struct dpc*)dev->ext_priv;
241         
242         int i = 1;
243
244         /* switch bypass in saa7111a */
245         if ( 0 != dpc->saa7111a->driver->command(dpc->saa7111a,SAA711X_VBI_BYPASS, &i)) {
246                 printk("dpc_v4l2.o: VBI_BYPASS: could not address saa7111a.\n");
247                 return -1;
248         }                       
249
250         return 0;
251 }
252 #endif
253
254 static int dpc_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg) 
255 {
256         struct saa7146_dev *dev = fh->dev;
257         struct dpc* dpc = (struct dpc*)dev->ext_priv;
258 /*
259         struct saa7146_vv *vv = dev->vv_data; 
260 */
261         switch(cmd)
262         {
263         case VIDIOC_ENUMINPUT:
264         {
265                 struct v4l2_input *i = arg;
266                 DEB_EE(("VIDIOC_ENUMINPUT %d.\n",i->index));
267                 
268                 if( i->index < 0 || i->index >= DPC_INPUTS) {
269                         return -EINVAL;
270                 }
271                 
272                 memcpy(i, &dpc_inputs[i->index], sizeof(struct v4l2_input));
273
274                 DEB_D(("dpc_v4l2.o: v4l2_ioctl: VIDIOC_ENUMINPUT %d.\n",i->index));
275                 return 0;
276         }
277         case VIDIOC_G_INPUT:
278         {
279                 int *input = (int *)arg;
280                 *input = dpc->cur_input;
281
282                 DEB_D(("dpc_v4l2.o: VIDIOC_G_INPUT: %d\n",*input));
283                 return 0;
284         }
285         case VIDIOC_S_INPUT:
286         {
287                 int     input = *(int *)arg;
288
289                 if (input < 0 || input >= DPC_INPUTS) {
290                         return -EINVAL;
291                 }
292         
293                 dpc->cur_input = input;
294
295                 /* fixme: switch input here, switch audio, too! */
296 //              saa7146_set_hps_source_and_sync(dev, input_port_selection[input].hps_source, input_port_selection[input].hps_sync);
297                 printk("dpc_v4l2.o: VIDIOC_S_INPUT: fixme switch input.\n");
298                 
299                 return 0;
300         }
301         default:
302 /*
303                 DEB_D(("dpc_v4l2.o: v4l2_ioctl does not handle this ioctl.\n"));
304 */
305                 return -ENOIOCTLCMD;
306         }
307         return 0;
308 }
309
310 static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
311 {
312         return 0;
313 }
314
315 static struct saa7146_standard standard[] = {
316         {
317                 .name   = "PAL",        .id     = V4L2_STD_PAL,
318                 .v_offset       = 0x17, .v_field        = 288,
319                 .h_offset       = 0x14, .h_pixels       = 680,
320                 .v_max_out      = 576,  .h_max_out      = 768,
321         }, {
322                 .name   = "NTSC",       .id     = V4L2_STD_NTSC,
323                 .v_offset       = 0x16, .v_field        = 240,
324                 .h_offset       = 0x06, .h_pixels       = 708,
325                 .v_max_out      = 480,  .h_max_out      = 640,
326         }, {
327                 .name   = "SECAM",      .id     = V4L2_STD_SECAM,
328                 .v_offset       = 0x14, .v_field        = 288,
329                 .h_offset       = 0x14, .h_pixels       = 720,
330                 .v_max_out      = 576,  .h_max_out      = 768,
331         }
332 };
333
334 static struct saa7146_extension extension;
335
336 static struct saa7146_pci_extension_data dpc = {
337         .ext_priv = "Multimedia eXtension Board",
338         .ext = &extension,
339 };
340
341 static struct pci_device_id pci_tbl[] = {
342         {
343                 .vendor    = PCI_VENDOR_ID_PHILIPS,
344                 .device    = PCI_DEVICE_ID_PHILIPS_SAA7146,
345                 .subvendor = 0x0000,
346                 .subdevice = 0x0000,
347                 .driver_data = (unsigned long)&dpc,
348         }, {
349                 .vendor = 0,
350         }
351 };
352
353 MODULE_DEVICE_TABLE(pci, pci_tbl);
354
355 static struct saa7146_ext_vv vv_data = {
356         .inputs         = DPC_INPUTS,
357         .capabilities   = V4L2_CAP_VBI_CAPTURE,
358         .stds           = &standard[0],
359         .num_stds       = sizeof(standard)/sizeof(struct saa7146_standard),
360         .std_callback   = &std_callback, 
361         .ioctls         = &ioctls[0],
362         .ioctl          = dpc_ioctl,
363 };
364
365 static struct saa7146_extension extension = {
366         .name           = "dpc7146 demonstration board",
367         .flags          = SAA7146_USE_I2C_IRQ,
368         
369         .pci_tbl        = &pci_tbl[0],
370         .module         = THIS_MODULE,
371
372         .probe          = dpc_probe,
373         .attach         = dpc_attach,
374         .detach         = dpc_detach,
375
376         .irq_mask       = 0,
377         .irq_func       = NULL,
378 };      
379
380 static int __init dpc_init_module(void)
381 {
382         if( 0 != saa7146_register_extension(&extension)) {
383                 DEB_S(("failed to register extension.\n"));
384                 return -ENODEV;
385         }
386         
387         return 0;
388 }
389
390 static void __exit dpc_cleanup_module(void)
391 {
392         saa7146_unregister_extension(&extension);
393 }
394
395 module_init(dpc_init_module);
396 module_exit(dpc_cleanup_module);
397
398 MODULE_DESCRIPTION("video4linux-2 driver for the 'dpc7146 demonstration board'");
399 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
400 MODULE_LICENSE("GPL");