Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/i2c-2.6
[linux-2.6] / drivers / media / video / saa7114.c
1 /* 
2  * saa7114 - Philips SAA7114H video decoder driver version 0.0.1
3  *
4  * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
5  *
6  * Based on saa7111 driver by Dave Perks
7  *
8  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
9  *
10  * Slight changes for video timing and attachment output by
11  * Wolfgang Scherr <scherr@net4you.net>
12  *
13  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14  *    - moved over to linux>=2.4.x i2c protocol (1/1/2003)
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
30
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
35 #include <linux/fs.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38
39 #include <linux/slab.h>
40
41 #include <linux/mm.h>
42 #include <linux/pci.h>
43 #include <linux/signal.h>
44 #include <asm/io.h>
45 #include <asm/pgtable.h>
46 #include <asm/page.h>
47 #include <linux/sched.h>
48 #include <linux/types.h>
49
50 #include <linux/videodev.h>
51 #include <asm/uaccess.h>
52
53 MODULE_DESCRIPTION("Philips SAA7114H video decoder driver");
54 MODULE_AUTHOR("Maxim Yevtyushkin");
55 MODULE_LICENSE("GPL");
56
57 #include <linux/i2c.h>
58
59 #define I2C_NAME(x) (x)->name
60
61 #include <linux/video_decoder.h>
62
63 static int debug = 0;
64 module_param(debug, int, 0);
65 MODULE_PARM_DESC(debug, "Debug level (0-1)");
66
67 #define dprintk(num, format, args...) \
68         do { \
69                 if (debug >= num) \
70                         printk(format, ##args); \
71         } while (0)
72
73 /* ----------------------------------------------------------------------- */
74
75 struct saa7114 {
76         unsigned char reg[0xf0 * 2];
77
78         int norm;
79         int input;
80         int enable;
81         int bright;
82         int contrast;
83         int hue;
84         int sat;
85         int playback;
86 };
87
88 #define   I2C_SAA7114        0x42
89 #define   I2C_SAA7114A       0x40
90
91 #define   I2C_DELAY   10
92
93
94 //#define SAA_7114_NTSC_HSYNC_START       (-3)
95 //#define SAA_7114_NTSC_HSYNC_STOP        (-18)
96
97 #define SAA_7114_NTSC_HSYNC_START  (-17)
98 #define SAA_7114_NTSC_HSYNC_STOP   (-32)
99
100 //#define SAA_7114_NTSC_HOFFSET           (5)
101 #define SAA_7114_NTSC_HOFFSET           (6)
102 #define SAA_7114_NTSC_VOFFSET           (10)
103 #define SAA_7114_NTSC_WIDTH             (720)
104 #define SAA_7114_NTSC_HEIGHT            (250)
105
106 #define SAA_7114_SECAM_HSYNC_START      (-17)
107 #define SAA_7114_SECAM_HSYNC_STOP       (-32)
108
109 #define SAA_7114_SECAM_HOFFSET          (2)
110 #define SAA_7114_SECAM_VOFFSET          (10)
111 #define SAA_7114_SECAM_WIDTH            (720)
112 #define SAA_7114_SECAM_HEIGHT           (300)
113
114 #define SAA_7114_PAL_HSYNC_START        (-17)
115 #define SAA_7114_PAL_HSYNC_STOP         (-32)
116
117 #define SAA_7114_PAL_HOFFSET            (2)
118 #define SAA_7114_PAL_VOFFSET            (10)
119 #define SAA_7114_PAL_WIDTH              (720)
120 #define SAA_7114_PAL_HEIGHT             (300)
121
122
123
124 #define SAA_7114_VERTICAL_CHROMA_OFFSET         0       //0x50504040
125 #define SAA_7114_VERTICAL_LUMA_OFFSET           0
126
127 #define REG_ADDR(x) (((x) << 1) + 1)
128 #define LOBYTE(x) ((unsigned char)((x) & 0xff))
129 #define HIBYTE(x) ((unsigned char)(((x) >> 8) & 0xff))
130 #define LOWORD(x) ((unsigned short int)((x) & 0xffff))
131 #define HIWORD(x) ((unsigned short int)(((x) >> 16) & 0xffff))
132
133
134 /* ----------------------------------------------------------------------- */
135
136 static inline int
137 saa7114_write (struct i2c_client *client,
138                u8                 reg,
139                u8                 value)
140 {
141         /*struct saa7114 *decoder = i2c_get_clientdata(client);*/
142
143         /*decoder->reg[reg] = value;*/
144         return i2c_smbus_write_byte_data(client, reg, value);
145 }
146
147 static int
148 saa7114_write_block (struct i2c_client *client,
149                      const u8          *data,
150                      unsigned int       len)
151 {
152         int ret = -1;
153         u8 reg;
154
155         /* the saa7114 has an autoincrement function, use it if
156          * the adapter understands raw I2C */
157         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
158                 /* do raw I2C, not smbus compatible */
159                 /*struct saa7114 *decoder = i2c_get_clientdata(client);*/
160                 struct i2c_msg msg;
161                 u8 block_data[32];
162
163                 msg.addr = client->addr;
164                 msg.flags = 0;
165                 while (len >= 2) {
166                         msg.buf = (char *) block_data;
167                         msg.len = 0;
168                         block_data[msg.len++] = reg = data[0];
169                         do {
170                                 block_data[msg.len++] =
171                                     /*decoder->reg[reg++] =*/ data[1];
172                                 len -= 2;
173                                 data += 2;
174                         } while (len >= 2 && data[0] == reg &&
175                                  msg.len < 32);
176                         if ((ret = i2c_transfer(client->adapter,
177                                                 &msg, 1)) < 0)
178                                 break;
179                 }
180         } else {
181                 /* do some slow I2C emulation kind of thing */
182                 while (len >= 2) {
183                         reg = *data++;
184                         if ((ret = saa7114_write(client, reg,
185                                                  *data++)) < 0)
186                                 break;
187                         len -= 2;
188                 }
189         }
190
191         return ret;
192 }
193
194 static inline int
195 saa7114_read (struct i2c_client *client,
196               u8                 reg)
197 {
198         return i2c_smbus_read_byte_data(client, reg);
199 }
200
201 /* ----------------------------------------------------------------------- */
202
203 // initially set NTSC, composite
204
205
206 static const unsigned char init[] = {
207         0x00, 0x00,             /* 00 - ID byte , chip version,
208                                  * read only */
209         0x01, 0x08,             /* 01 - X,X,X,X, IDEL3 to IDEL0 -
210                                  * horizontal increment delay,
211                                  * recommended position */
212         0x02, 0x00,             /* 02 - FUSE=3, GUDL=2, MODE=0 ;
213                                  * input control */
214         0x03, 0x10,             /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
215                                  * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
216         0x04, 0x90,             /* 04 - GAI1=256 */
217         0x05, 0x90,             /* 05 - GAI2=256 */
218         0x06, SAA_7114_NTSC_HSYNC_START,        /* 06 - HSB: hsync start,
219                                  * depends on the video standard */
220         0x07, SAA_7114_NTSC_HSYNC_STOP, /* 07 - HSS: hsync stop, depends
221                                  *on the video standard */
222         0x08, 0xb8,             /* 08 - AUFD=1, FSEL=1, EXFIL=0, VTRC=1,
223                                  * HPLL: free running in playback, locked
224                                  * in capture, VNOI=0 */
225         0x09, 0x80,             /* 09 - BYPS=0, PREF=0, BPSS=0, VBLB=0,
226                                  * UPTCV=0, APER=1; depends from input */
227         0x0a, 0x80,             /* 0a - BRIG=128 */
228         0x0b, 0x44,             /* 0b - CONT=1.109 */
229         0x0c, 0x40,             /* 0c - SATN=1.0 */
230         0x0d, 0x00,             /* 0d - HUE=0 */
231         0x0e, 0x84,             /* 0e - CDTO, CSTD2 to 0, DCVF, FCTC,
232                                  * CCOMB; depends from video standard */
233         0x0f, 0x24,             /* 0f - ACGC,CGAIN6 to CGAIN0; depends
234                                  * from video standard */
235         0x10, 0x03,             /* 10 - OFFU1 to 0, OFFV1 to 0, CHBW,
236                                  * LCBW2 to 0 */
237         0x11, 0x59,             /* 11 - COLO, RTP1, HEDL1 to 0, RTP0,
238                                  * YDEL2 to 0 */
239         0x12, 0xc9,             /* 12 - RT signal control RTSE13 to 10
240                                  * and 03 to 00 */
241         0x13, 0x80,             /* 13 - RT/X port output control  */
242         0x14, 0x00,             /* 14 - analog, ADC, compatibility control */
243         0x15, 0x00,             /* 15 - VGATE start FID change  */
244         0x16, 0xfe,             /* 16 - VGATE stop */
245         0x17, 0x00,             /* 17 - Misc., VGATE MSBs */
246         0x18, 0x40,             /* RAWG */
247         0x19, 0x80,             /* RAWO */
248         0x1a, 0x00,
249         0x1b, 0x00,
250         0x1c, 0x00,
251         0x1d, 0x00,
252         0x1e, 0x00,
253         0x1f, 0x00,             /* status byte, read only */
254         0x20, 0x00,             /* video decoder reserved part */
255         0x21, 0x00,
256         0x22, 0x00,
257         0x23, 0x00,
258         0x24, 0x00,
259         0x25, 0x00,
260         0x26, 0x00,
261         0x27, 0x00,
262         0x28, 0x00,
263         0x29, 0x00,
264         0x2a, 0x00,
265         0x2b, 0x00,
266         0x2c, 0x00,
267         0x2d, 0x00,
268         0x2e, 0x00,
269         0x2f, 0x00,
270         0x30, 0xbc,             /* audio clock generator */
271         0x31, 0xdf,
272         0x32, 0x02,
273         0x33, 0x00,
274         0x34, 0xcd,
275         0x35, 0xcc,
276         0x36, 0x3a,
277         0x37, 0x00,
278         0x38, 0x03,
279         0x39, 0x10,
280         0x3a, 0x00,
281         0x3b, 0x00,
282         0x3c, 0x00,
283         0x3d, 0x00,
284         0x3e, 0x00,
285         0x3f, 0x00,
286         0x40, 0x00,             /* VBI data slicer */
287         0x41, 0xff,
288         0x42, 0xff,
289         0x43, 0xff,
290         0x44, 0xff,
291         0x45, 0xff,
292         0x46, 0xff,
293         0x47, 0xff,
294         0x48, 0xff,
295         0x49, 0xff,
296         0x4a, 0xff,
297         0x4b, 0xff,
298         0x4c, 0xff,
299         0x4d, 0xff,
300         0x4e, 0xff,
301         0x4f, 0xff,
302         0x50, 0xff,
303         0x51, 0xff,
304         0x52, 0xff,
305         0x53, 0xff,
306         0x54, 0xff,
307         0x55, 0xff,
308         0x56, 0xff,
309         0x57, 0xff,
310         0x58, 0x40,             // framing code 
311         0x59, 0x47,             // horizontal offset
312         0x5a, 0x06,             // vertical offset
313         0x5b, 0x83,             // field offset
314         0x5c, 0x00,             // reserved
315         0x5d, 0x3e,             // header and data
316         0x5e, 0x00,             // sliced data
317         0x5f, 0x00,             // reserved
318         0x60, 0x00,             /* video decoder reserved part */
319         0x61, 0x00,
320         0x62, 0x00,
321         0x63, 0x00,
322         0x64, 0x00,
323         0x65, 0x00,
324         0x66, 0x00,
325         0x67, 0x00,
326         0x68, 0x00,
327         0x69, 0x00,
328         0x6a, 0x00,
329         0x6b, 0x00,
330         0x6c, 0x00,
331         0x6d, 0x00,
332         0x6e, 0x00,
333         0x6f, 0x00,
334         0x70, 0x00,             /* video decoder reserved part */
335         0x71, 0x00,
336         0x72, 0x00,
337         0x73, 0x00,
338         0x74, 0x00,
339         0x75, 0x00,
340         0x76, 0x00,
341         0x77, 0x00,
342         0x78, 0x00,
343         0x79, 0x00,
344         0x7a, 0x00,
345         0x7b, 0x00,
346         0x7c, 0x00,
347         0x7d, 0x00,
348         0x7e, 0x00,
349         0x7f, 0x00,
350         0x80, 0x00,             /* X-port, I-port and scaler */
351         0x81, 0x00,
352         0x82, 0x00,
353         0x83, 0x00,
354         0x84, 0xc5,
355         0x85, 0x0d,             // hsync and vsync ? 
356         0x86, 0x40,
357         0x87, 0x01,
358         0x88, 0x00,
359         0x89, 0x00,
360         0x8a, 0x00,
361         0x8b, 0x00,
362         0x8c, 0x00,
363         0x8d, 0x00,
364         0x8e, 0x00,
365         0x8f, 0x00,
366         0x90, 0x03,             /* Task A definition           */
367         0x91, 0x08,
368         0x92, 0x00,
369         0x93, 0x40,
370         0x94, 0x00,             // window settings
371         0x95, 0x00,
372         0x96, 0x00,
373         0x97, 0x00,
374         0x98, 0x00,
375         0x99, 0x00,
376         0x9a, 0x00,
377         0x9b, 0x00,
378         0x9c, 0x00,
379         0x9d, 0x00,
380         0x9e, 0x00,
381         0x9f, 0x00,
382         0xa0, 0x01,             /* horizontal integer prescaling ratio */
383         0xa1, 0x00,             /* horizontal prescaler accumulation
384                                  * sequence length */
385         0xa2, 0x00,             /* UV FIR filter, Y FIR filter, prescaler
386                                  * DC gain */
387         0xa3, 0x00,
388         0xa4, 0x80,             // luminance brightness
389         0xa5, 0x40,             // luminance gain
390         0xa6, 0x40,             // chrominance saturation
391         0xa7, 0x00,
392         0xa8, 0x00,             // horizontal luminance scaling increment
393         0xa9, 0x04,
394         0xaa, 0x00,             // horizontal luminance phase offset
395         0xab, 0x00,
396         0xac, 0x00,             // horizontal chrominance scaling increment
397         0xad, 0x02,
398         0xae, 0x00,             // horizontal chrominance phase offset
399         0xaf, 0x00,
400         0xb0, 0x00,             // vertical luminance scaling increment
401         0xb1, 0x04,
402         0xb2, 0x00,             // vertical chrominance scaling increment
403         0xb3, 0x04,
404         0xb4, 0x00,
405         0xb5, 0x00,
406         0xb6, 0x00,
407         0xb7, 0x00,
408         0xb8, 0x00,
409         0xb9, 0x00,
410         0xba, 0x00,
411         0xbb, 0x00,
412         0xbc, 0x00,
413         0xbd, 0x00,
414         0xbe, 0x00,
415         0xbf, 0x00,
416         0xc0, 0x02,             // Task B definition
417         0xc1, 0x08,
418         0xc2, 0x00,
419         0xc3, 0x40,
420         0xc4, 0x00,             // window settings
421         0xc5, 0x00,
422         0xc6, 0x00,
423         0xc7, 0x00,
424         0xc8, 0x00,
425         0xc9, 0x00,
426         0xca, 0x00,
427         0xcb, 0x00,
428         0xcc, 0x00,
429         0xcd, 0x00,
430         0xce, 0x00,
431         0xcf, 0x00,
432         0xd0, 0x01,             // horizontal integer prescaling ratio
433         0xd1, 0x00,             // horizontal prescaler accumulation sequence length
434         0xd2, 0x00,             // UV FIR filter, Y FIR filter, prescaler DC gain
435         0xd3, 0x00,
436         0xd4, 0x80,             // luminance brightness
437         0xd5, 0x40,             // luminance gain
438         0xd6, 0x40,             // chrominance saturation
439         0xd7, 0x00,
440         0xd8, 0x00,             // horizontal luminance scaling increment
441         0xd9, 0x04,
442         0xda, 0x00,             // horizontal luminance phase offset
443         0xdb, 0x00,
444         0xdc, 0x00,             // horizontal chrominance scaling increment 
445         0xdd, 0x02,
446         0xde, 0x00,             // horizontal chrominance phase offset
447         0xdf, 0x00,
448         0xe0, 0x00,             // vertical luminance scaling increment
449         0xe1, 0x04,
450         0xe2, 0x00,             // vertical chrominance scaling increment
451         0xe3, 0x04,
452         0xe4, 0x00,
453         0xe5, 0x00,
454         0xe6, 0x00,
455         0xe7, 0x00,
456         0xe8, 0x00,
457         0xe9, 0x00,
458         0xea, 0x00,
459         0xeb, 0x00,
460         0xec, 0x00,
461         0xed, 0x00,
462         0xee, 0x00,
463         0xef, 0x00
464 };
465
466 static int
467 saa7114_command (struct i2c_client *client,
468                  unsigned int       cmd,
469                  void              *arg)
470 {
471         struct saa7114 *decoder = i2c_get_clientdata(client);
472
473         switch (cmd) {
474
475         case 0:
476                 //dprintk(1, KERN_INFO "%s: writing init\n", I2C_NAME(client));
477                 //saa7114_write_block(client, init, sizeof(init));
478                 break;
479
480         case DECODER_DUMP:
481         {
482                 int i;
483
484                 dprintk(1, KERN_INFO "%s: decoder dump\n", I2C_NAME(client));
485
486                 for (i = 0; i < 32; i += 16) {
487                         int j;
488
489                         printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
490                         for (j = 0; j < 16; ++j) {
491                                 printk(" %02x",
492                                        saa7114_read(client, i + j));
493                         }
494                         printk("\n");
495                 }
496         }
497                 break;
498
499         case DECODER_GET_CAPABILITIES:
500         {
501                 struct video_decoder_capability *cap = arg;
502
503                 dprintk(1, KERN_DEBUG "%s: decoder get capabilities\n",
504                         I2C_NAME(client));
505
506                 cap->flags = VIDEO_DECODER_PAL |
507                              VIDEO_DECODER_NTSC |
508                              VIDEO_DECODER_AUTO |
509                              VIDEO_DECODER_CCIR;
510                 cap->inputs = 8;
511                 cap->outputs = 1;
512         }
513                 break;
514
515         case DECODER_GET_STATUS:
516         {
517                 int *iarg = arg;
518                 int status;
519                 int res;
520
521                 status = saa7114_read(client, 0x1f);
522
523                 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
524                         status);
525                 res = 0;
526                 if ((status & (1 << 6)) == 0) {
527                         res |= DECODER_STATUS_GOOD;
528                 }
529                 switch (decoder->norm) {
530                 case VIDEO_MODE_NTSC:
531                         res |= DECODER_STATUS_NTSC;
532                         break;
533                 case VIDEO_MODE_PAL:
534                         res |= DECODER_STATUS_PAL;
535                         break;
536                 case VIDEO_MODE_SECAM:
537                         res |= DECODER_STATUS_SECAM;
538                         break;
539                 default:
540                 case VIDEO_MODE_AUTO:
541                         if ((status & (1 << 5)) != 0) {
542                                 res |= DECODER_STATUS_NTSC;
543                         } else {
544                                 res |= DECODER_STATUS_PAL;
545                         }
546                         break;
547                 }
548                 if ((status & (1 << 0)) != 0) {
549                         res |= DECODER_STATUS_COLOR;
550                 }
551                 *iarg = res;
552         }
553                 break;
554
555         case DECODER_SET_NORM:
556         {
557                 int *iarg = arg;
558
559                 short int hoff = 0, voff = 0, w = 0, h = 0;
560
561                 dprintk(1, KERN_DEBUG "%s: decoder set norm ",
562                         I2C_NAME(client));
563                 switch (*iarg) {
564
565                 case VIDEO_MODE_NTSC:
566                         dprintk(1, "NTSC\n");
567                         decoder->reg[REG_ADDR(0x06)] =
568                             SAA_7114_NTSC_HSYNC_START;
569                         decoder->reg[REG_ADDR(0x07)] =
570                             SAA_7114_NTSC_HSYNC_STOP;
571
572                         decoder->reg[REG_ADDR(0x08)] = decoder->playback ? 0x7c : 0xb8; // PLL free when playback, PLL close when capture
573
574                         decoder->reg[REG_ADDR(0x0e)] = 0x85;
575                         decoder->reg[REG_ADDR(0x0f)] = 0x24;
576
577                         hoff = SAA_7114_NTSC_HOFFSET;
578                         voff = SAA_7114_NTSC_VOFFSET;
579                         w = SAA_7114_NTSC_WIDTH;
580                         h = SAA_7114_NTSC_HEIGHT;
581
582                         break;
583
584                 case VIDEO_MODE_PAL:
585                         dprintk(1, "PAL\n");
586                         decoder->reg[REG_ADDR(0x06)] =
587                             SAA_7114_PAL_HSYNC_START;
588                         decoder->reg[REG_ADDR(0x07)] =
589                             SAA_7114_PAL_HSYNC_STOP;
590
591                         decoder->reg[REG_ADDR(0x08)] = decoder->playback ? 0x7c : 0xb8; // PLL free when playback, PLL close when capture
592
593                         decoder->reg[REG_ADDR(0x0e)] = 0x81;
594                         decoder->reg[REG_ADDR(0x0f)] = 0x24;
595
596                         hoff = SAA_7114_PAL_HOFFSET;
597                         voff = SAA_7114_PAL_VOFFSET;
598                         w = SAA_7114_PAL_WIDTH;
599                         h = SAA_7114_PAL_HEIGHT;
600
601                         break;
602
603                 default:
604                         dprintk(1, " Unknown video mode!!!\n");
605                         return -EINVAL;
606
607                 }
608
609
610                 decoder->reg[REG_ADDR(0x94)] = LOBYTE(hoff);    // hoffset low
611                 decoder->reg[REG_ADDR(0x95)] = HIBYTE(hoff) & 0x0f;     // hoffset high
612                 decoder->reg[REG_ADDR(0x96)] = LOBYTE(w);       // width low
613                 decoder->reg[REG_ADDR(0x97)] = HIBYTE(w) & 0x0f;        // width high
614                 decoder->reg[REG_ADDR(0x98)] = LOBYTE(voff);    // voffset low
615                 decoder->reg[REG_ADDR(0x99)] = HIBYTE(voff) & 0x0f;     // voffset high
616                 decoder->reg[REG_ADDR(0x9a)] = LOBYTE(h + 2);   // height low
617                 decoder->reg[REG_ADDR(0x9b)] = HIBYTE(h + 2) & 0x0f;    // height high
618                 decoder->reg[REG_ADDR(0x9c)] = LOBYTE(w);       // out width low
619                 decoder->reg[REG_ADDR(0x9d)] = HIBYTE(w) & 0x0f;        // out width high
620                 decoder->reg[REG_ADDR(0x9e)] = LOBYTE(h);       // out height low
621                 decoder->reg[REG_ADDR(0x9f)] = HIBYTE(h) & 0x0f;        // out height high
622
623                 decoder->reg[REG_ADDR(0xc4)] = LOBYTE(hoff);    // hoffset low
624                 decoder->reg[REG_ADDR(0xc5)] = HIBYTE(hoff) & 0x0f;     // hoffset high
625                 decoder->reg[REG_ADDR(0xc6)] = LOBYTE(w);       // width low
626                 decoder->reg[REG_ADDR(0xc7)] = HIBYTE(w) & 0x0f;        // width high
627                 decoder->reg[REG_ADDR(0xc8)] = LOBYTE(voff);    // voffset low
628                 decoder->reg[REG_ADDR(0xc9)] = HIBYTE(voff) & 0x0f;     // voffset high
629                 decoder->reg[REG_ADDR(0xca)] = LOBYTE(h + 2);   // height low
630                 decoder->reg[REG_ADDR(0xcb)] = HIBYTE(h + 2) & 0x0f;    // height high
631                 decoder->reg[REG_ADDR(0xcc)] = LOBYTE(w);       // out width low
632                 decoder->reg[REG_ADDR(0xcd)] = HIBYTE(w) & 0x0f;        // out width high
633                 decoder->reg[REG_ADDR(0xce)] = LOBYTE(h);       // out height low
634                 decoder->reg[REG_ADDR(0xcf)] = HIBYTE(h) & 0x0f;        // out height high
635
636
637                 saa7114_write(client, 0x80, 0x06);      // i-port and scaler back end clock selection, task A&B off
638                 saa7114_write(client, 0x88, 0xd8);      // sw reset scaler
639                 saa7114_write(client, 0x88, 0xf8);      // sw reset scaler release
640
641                 saa7114_write_block(client, decoder->reg + (0x06 << 1),
642                                     3 << 1);
643                 saa7114_write_block(client, decoder->reg + (0x0e << 1),
644                                     2 << 1);
645                 saa7114_write_block(client, decoder->reg + (0x5a << 1),
646                                     2 << 1);
647
648                 saa7114_write_block(client, decoder->reg + (0x94 << 1),
649                                     (0x9f + 1 - 0x94) << 1);
650                 saa7114_write_block(client, decoder->reg + (0xc4 << 1),
651                                     (0xcf + 1 - 0xc4) << 1);
652
653                 saa7114_write(client, 0x88, 0xd8);      // sw reset scaler
654                 saa7114_write(client, 0x88, 0xf8);      // sw reset scaler release
655                 saa7114_write(client, 0x80, 0x36);      // i-port and scaler back end clock selection
656
657                 decoder->norm = *iarg;
658         }
659                 break;
660
661         case DECODER_SET_INPUT:
662         {
663                 int *iarg = arg;
664
665                 dprintk(1, KERN_DEBUG "%s: decoder set input (%d)\n",
666                         I2C_NAME(client), *iarg);
667                 if (*iarg < 0 || *iarg > 7) {
668                         return -EINVAL;
669                 }
670
671                 if (decoder->input != *iarg) {
672                         dprintk(1, KERN_DEBUG "%s: now setting %s input\n",
673                                 I2C_NAME(client),
674                                 *iarg >= 6 ? "S-Video" : "Composite");
675                         decoder->input = *iarg;
676
677                         /* select mode */
678                         decoder->reg[REG_ADDR(0x02)] =
679                             (decoder->
680                              reg[REG_ADDR(0x02)] & 0xf0) | (decoder->
681                                                             input <
682                                                             6 ? 0x0 : 0x9);
683                         saa7114_write(client, 0x02,
684                                       decoder->reg[REG_ADDR(0x02)]);
685
686                         /* bypass chrominance trap for modes 6..9 */
687                         decoder->reg[REG_ADDR(0x09)] =
688                             (decoder->
689                              reg[REG_ADDR(0x09)] & 0x7f) | (decoder->
690                                                             input <
691                                                             6 ? 0x0 :
692                                                             0x80);
693                         saa7114_write(client, 0x09,
694                                       decoder->reg[REG_ADDR(0x09)]);
695
696                         decoder->reg[REG_ADDR(0x0e)] =
697                             decoder->input <
698                             6 ? decoder->
699                             reg[REG_ADDR(0x0e)] | 1 : decoder->
700                             reg[REG_ADDR(0x0e)] & ~1;
701                         saa7114_write(client, 0x0e,
702                                       decoder->reg[REG_ADDR(0x0e)]);
703                 }
704         }
705                 break;
706
707         case DECODER_SET_OUTPUT:
708         {
709                 int *iarg = arg;
710
711                 dprintk(1, KERN_DEBUG "%s: decoder set output\n",
712                         I2C_NAME(client));
713
714                 /* not much choice of outputs */
715                 if (*iarg != 0) {
716                         return -EINVAL;
717                 }
718         }
719                 break;
720
721         case DECODER_ENABLE_OUTPUT:
722         {
723                 int *iarg = arg;
724                 int enable = (*iarg != 0);
725
726                 dprintk(1, KERN_DEBUG "%s: decoder %s output\n",
727                         I2C_NAME(client), enable ? "enable" : "disable");
728
729                 decoder->playback = !enable;
730
731                 if (decoder->enable != enable) {
732                         decoder->enable = enable;
733
734                         /* RJ: If output should be disabled (for
735                          * playing videos), we also need a open PLL.
736                          * The input is set to 0 (where no input
737                          * source is connected), although this
738                          * is not necessary.
739                          *
740                          * If output should be enabled, we have to
741                          * reverse the above.
742                          */
743
744                         if (decoder->enable) {
745                                 decoder->reg[REG_ADDR(0x08)] = 0xb8;
746                                 decoder->reg[REG_ADDR(0x12)] = 0xc9;
747                                 decoder->reg[REG_ADDR(0x13)] = 0x80;
748                                 decoder->reg[REG_ADDR(0x87)] = 0x01;
749                         } else {
750                                 decoder->reg[REG_ADDR(0x08)] = 0x7c;
751                                 decoder->reg[REG_ADDR(0x12)] = 0x00;
752                                 decoder->reg[REG_ADDR(0x13)] = 0x00;
753                                 decoder->reg[REG_ADDR(0x87)] = 0x00;
754                         }
755
756                         saa7114_write_block(client,
757                                             decoder->reg + (0x12 << 1),
758                                             2 << 1);
759                         saa7114_write(client, 0x08,
760                                       decoder->reg[REG_ADDR(0x08)]);
761                         saa7114_write(client, 0x87,
762                                       decoder->reg[REG_ADDR(0x87)]);
763                         saa7114_write(client, 0x88, 0xd8);      // sw reset scaler
764                         saa7114_write(client, 0x88, 0xf8);      // sw reset scaler release            
765                         saa7114_write(client, 0x80, 0x36);
766
767                 }
768         }
769                 break;
770
771         case DECODER_SET_PICTURE:
772         {
773                 struct video_picture *pic = arg;
774
775                 dprintk(1,
776                         KERN_DEBUG
777                         "%s: decoder set picture bright=%d contrast=%d saturation=%d hue=%d\n",
778                         I2C_NAME(client), pic->brightness, pic->contrast,
779                         pic->colour, pic->hue);
780
781                 if (decoder->bright != pic->brightness) {
782                         /* We want 0 to 255 we get 0-65535 */
783                         decoder->bright = pic->brightness;
784                         saa7114_write(client, 0x0a, decoder->bright >> 8);
785                 }
786                 if (decoder->contrast != pic->contrast) {
787                         /* We want 0 to 127 we get 0-65535 */
788                         decoder->contrast = pic->contrast;
789                         saa7114_write(client, 0x0b,
790                                       decoder->contrast >> 9);
791                 }
792                 if (decoder->sat != pic->colour) {
793                         /* We want 0 to 127 we get 0-65535 */
794                         decoder->sat = pic->colour;
795                         saa7114_write(client, 0x0c, decoder->sat >> 9);
796                 }
797                 if (decoder->hue != pic->hue) {
798                         /* We want -128 to 127 we get 0-65535 */
799                         decoder->hue = pic->hue;
800                         saa7114_write(client, 0x0d,
801                                       (decoder->hue - 32768) >> 8);
802                 }
803         }
804                 break;
805
806         default:
807                 return -EINVAL;
808         }
809
810         return 0;
811 }
812
813 /* ----------------------------------------------------------------------- */
814
815 /*
816  * Generic i2c probe
817  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
818  */
819 static unsigned short normal_i2c[] =
820     { I2C_SAA7114 >> 1, I2C_SAA7114A >> 1, I2C_CLIENT_END };
821
822 static unsigned short ignore = I2C_CLIENT_END;
823                                                                                 
824 static struct i2c_client_address_data addr_data = {
825         .normal_i2c             = normal_i2c,
826         .probe                  = &ignore,
827         .ignore                 = &ignore,
828 };
829
830 static struct i2c_driver i2c_driver_saa7114;
831
832 static int
833 saa7114_detect_client (struct i2c_adapter *adapter,
834                        int                 address,
835                        int                 kind)
836 {
837         int i, err[30];
838         short int hoff = SAA_7114_NTSC_HOFFSET;
839         short int voff = SAA_7114_NTSC_VOFFSET;
840         short int w = SAA_7114_NTSC_WIDTH;
841         short int h = SAA_7114_NTSC_HEIGHT;
842         struct i2c_client *client;
843         struct saa7114 *decoder;
844
845         dprintk(1,
846                 KERN_INFO
847                 "saa7114.c: detecting saa7114 client on address 0x%x\n",
848                 address << 1);
849
850         /* Check if the adapter supports the needed features */
851         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
852                 return 0;
853
854         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
855         if (client == 0)
856                 return -ENOMEM;
857         client->addr = address;
858         client->adapter = adapter;
859         client->driver = &i2c_driver_saa7114;
860         strlcpy(I2C_NAME(client), "saa7114", sizeof(I2C_NAME(client)));
861
862         decoder = kzalloc(sizeof(struct saa7114), GFP_KERNEL);
863         if (decoder == NULL) {
864                 kfree(client);
865                 return -ENOMEM;
866         }
867         decoder->norm = VIDEO_MODE_NTSC;
868         decoder->input = -1;
869         decoder->enable = 1;
870         decoder->bright = 32768;
871         decoder->contrast = 32768;
872         decoder->hue = 32768;
873         decoder->sat = 32768;
874         decoder->playback = 0;  // initially capture mode useda
875         i2c_set_clientdata(client, decoder);
876
877         memcpy(decoder->reg, init, sizeof(init));
878
879         decoder->reg[REG_ADDR(0x94)] = LOBYTE(hoff);    // hoffset low
880         decoder->reg[REG_ADDR(0x95)] = HIBYTE(hoff) & 0x0f;     // hoffset high
881         decoder->reg[REG_ADDR(0x96)] = LOBYTE(w);       // width low
882         decoder->reg[REG_ADDR(0x97)] = HIBYTE(w) & 0x0f;        // width high
883         decoder->reg[REG_ADDR(0x98)] = LOBYTE(voff);    // voffset low
884         decoder->reg[REG_ADDR(0x99)] = HIBYTE(voff) & 0x0f;     // voffset high
885         decoder->reg[REG_ADDR(0x9a)] = LOBYTE(h + 2);   // height low
886         decoder->reg[REG_ADDR(0x9b)] = HIBYTE(h + 2) & 0x0f;    // height high
887         decoder->reg[REG_ADDR(0x9c)] = LOBYTE(w);       // out width low
888         decoder->reg[REG_ADDR(0x9d)] = HIBYTE(w) & 0x0f;        // out width high
889         decoder->reg[REG_ADDR(0x9e)] = LOBYTE(h);       // out height low
890         decoder->reg[REG_ADDR(0x9f)] = HIBYTE(h) & 0x0f;        // out height high
891
892         decoder->reg[REG_ADDR(0xc4)] = LOBYTE(hoff);    // hoffset low
893         decoder->reg[REG_ADDR(0xc5)] = HIBYTE(hoff) & 0x0f;     // hoffset high
894         decoder->reg[REG_ADDR(0xc6)] = LOBYTE(w);       // width low
895         decoder->reg[REG_ADDR(0xc7)] = HIBYTE(w) & 0x0f;        // width high
896         decoder->reg[REG_ADDR(0xc8)] = LOBYTE(voff);    // voffset low
897         decoder->reg[REG_ADDR(0xc9)] = HIBYTE(voff) & 0x0f;     // voffset high
898         decoder->reg[REG_ADDR(0xca)] = LOBYTE(h + 2);   // height low
899         decoder->reg[REG_ADDR(0xcb)] = HIBYTE(h + 2) & 0x0f;    // height high
900         decoder->reg[REG_ADDR(0xcc)] = LOBYTE(w);       // out width low
901         decoder->reg[REG_ADDR(0xcd)] = HIBYTE(w) & 0x0f;        // out width high
902         decoder->reg[REG_ADDR(0xce)] = LOBYTE(h);       // out height low
903         decoder->reg[REG_ADDR(0xcf)] = HIBYTE(h) & 0x0f;        // out height high
904
905         decoder->reg[REG_ADDR(0xb8)] =
906             LOBYTE(LOWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
907         decoder->reg[REG_ADDR(0xb9)] =
908             HIBYTE(LOWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
909         decoder->reg[REG_ADDR(0xba)] =
910             LOBYTE(HIWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
911         decoder->reg[REG_ADDR(0xbb)] =
912             HIBYTE(HIWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
913
914         decoder->reg[REG_ADDR(0xbc)] =
915             LOBYTE(LOWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
916         decoder->reg[REG_ADDR(0xbd)] =
917             HIBYTE(LOWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
918         decoder->reg[REG_ADDR(0xbe)] =
919             LOBYTE(HIWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
920         decoder->reg[REG_ADDR(0xbf)] =
921             HIBYTE(HIWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
922
923         decoder->reg[REG_ADDR(0xe8)] =
924             LOBYTE(LOWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
925         decoder->reg[REG_ADDR(0xe9)] =
926             HIBYTE(LOWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
927         decoder->reg[REG_ADDR(0xea)] =
928             LOBYTE(HIWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
929         decoder->reg[REG_ADDR(0xeb)] =
930             HIBYTE(HIWORD(SAA_7114_VERTICAL_CHROMA_OFFSET));
931
932         decoder->reg[REG_ADDR(0xec)] =
933             LOBYTE(LOWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
934         decoder->reg[REG_ADDR(0xed)] =
935             HIBYTE(LOWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
936         decoder->reg[REG_ADDR(0xee)] =
937             LOBYTE(HIWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
938         decoder->reg[REG_ADDR(0xef)] =
939             HIBYTE(HIWORD(SAA_7114_VERTICAL_LUMA_OFFSET));
940
941
942         decoder->reg[REG_ADDR(0x13)] = 0x80;    // RTC0 on
943         decoder->reg[REG_ADDR(0x87)] = 0x01;    // I-Port
944         decoder->reg[REG_ADDR(0x12)] = 0xc9;    // RTS0
945
946         decoder->reg[REG_ADDR(0x02)] = 0xc0;    // set composite1 input, aveasy
947         decoder->reg[REG_ADDR(0x09)] = 0x00;    // chrominance trap
948         decoder->reg[REG_ADDR(0x0e)] |= 1;      // combfilter on
949
950
951         dprintk(1, KERN_DEBUG "%s_attach: starting decoder init\n",
952                 I2C_NAME(client));
953
954         err[0] =
955             saa7114_write_block(client, decoder->reg + (0x20 << 1),
956                                 0x10 << 1);
957         err[1] =
958             saa7114_write_block(client, decoder->reg + (0x30 << 1),
959                                 0x10 << 1);
960         err[2] =
961             saa7114_write_block(client, decoder->reg + (0x63 << 1),
962                                 (0x7f + 1 - 0x63) << 1);
963         err[3] =
964             saa7114_write_block(client, decoder->reg + (0x89 << 1),
965                                 6 << 1);
966         err[4] =
967             saa7114_write_block(client, decoder->reg + (0xb8 << 1),
968                                 8 << 1);
969         err[5] =
970             saa7114_write_block(client, decoder->reg + (0xe8 << 1),
971                                 8 << 1);
972
973
974         for (i = 0; i <= 5; i++) {
975                 if (err[i] < 0) {
976                         dprintk(1,
977                                 KERN_ERR
978                                 "%s_attach: init error %d at stage %d, leaving attach.\n",
979                                 I2C_NAME(client), i, err[i]);
980                         kfree(decoder);
981                         kfree(client);
982                         return 0;
983                 }
984         }
985
986         for (i = 6; i < 8; i++) {
987                 dprintk(1,
988                         KERN_DEBUG
989                         "%s_attach: reg[0x%02x] = 0x%02x (0x%02x)\n",
990                         I2C_NAME(client), i, saa7114_read(client, i),
991                         decoder->reg[REG_ADDR(i)]);
992         }
993
994         dprintk(1,
995                 KERN_DEBUG
996                 "%s_attach: performing decoder reset sequence\n",
997                 I2C_NAME(client));
998
999         err[6] = saa7114_write(client, 0x80, 0x06);     // i-port and scaler backend clock selection, task A&B off
1000         err[7] = saa7114_write(client, 0x88, 0xd8);     // sw reset scaler
1001         err[8] = saa7114_write(client, 0x88, 0xf8);     // sw reset scaler release
1002
1003         for (i = 6; i <= 8; i++) {
1004                 if (err[i] < 0) {
1005                         dprintk(1,
1006                                 KERN_ERR
1007                                 "%s_attach: init error %d at stage %d, leaving attach.\n",
1008                                 I2C_NAME(client), i, err[i]);
1009                         kfree(decoder);
1010                         kfree(client);
1011                         return 0;
1012                 }
1013         }
1014
1015         dprintk(1, KERN_INFO "%s_attach: performing the rest of init\n",
1016                 I2C_NAME(client));
1017
1018
1019         err[9] = saa7114_write(client, 0x01, decoder->reg[REG_ADDR(0x01)]);
1020         err[10] = saa7114_write_block(client, decoder->reg + (0x03 << 1), (0x1e + 1 - 0x03) << 1);      // big seq
1021         err[11] = saa7114_write_block(client, decoder->reg + (0x40 << 1), (0x5f + 1 - 0x40) << 1);      // slicer
1022         err[12] = saa7114_write_block(client, decoder->reg + (0x81 << 1), 2 << 1);      // ?
1023         err[13] = saa7114_write_block(client, decoder->reg + (0x83 << 1), 5 << 1);      // ?
1024         err[14] = saa7114_write_block(client, decoder->reg + (0x90 << 1), 4 << 1);      // Task A
1025         err[15] =
1026             saa7114_write_block(client, decoder->reg + (0x94 << 1),
1027                                 12 << 1);
1028         err[16] =
1029             saa7114_write_block(client, decoder->reg + (0xa0 << 1),
1030                                 8 << 1);
1031         err[17] =
1032             saa7114_write_block(client, decoder->reg + (0xa8 << 1),
1033                                 8 << 1);
1034         err[18] =
1035             saa7114_write_block(client, decoder->reg + (0xb0 << 1),
1036                                 8 << 1);
1037         err[19] = saa7114_write_block(client, decoder->reg + (0xc0 << 1), 4 << 1);      // Task B
1038         err[15] =
1039             saa7114_write_block(client, decoder->reg + (0xc4 << 1),
1040                                 12 << 1);
1041         err[16] =
1042             saa7114_write_block(client, decoder->reg + (0xd0 << 1),
1043                                 8 << 1);
1044         err[17] =
1045             saa7114_write_block(client, decoder->reg + (0xd8 << 1),
1046                                 8 << 1);
1047         err[18] =
1048             saa7114_write_block(client, decoder->reg + (0xe0 << 1),
1049                                 8 << 1);
1050
1051         for (i = 9; i <= 18; i++) {
1052                 if (err[i] < 0) {
1053                         dprintk(1,
1054                                 KERN_ERR
1055                                 "%s_attach: init error %d at stage %d, leaving attach.\n",
1056                                 I2C_NAME(client), i, err[i]);
1057                         kfree(decoder);
1058                         kfree(client);
1059                         return 0;
1060                 }
1061         }
1062
1063
1064         for (i = 6; i < 8; i++) {
1065                 dprintk(1,
1066                         KERN_DEBUG
1067                         "%s_attach: reg[0x%02x] = 0x%02x (0x%02x)\n",
1068                         I2C_NAME(client), i, saa7114_read(client, i),
1069                         decoder->reg[REG_ADDR(i)]);
1070         }
1071
1072
1073         for (i = 0x11; i <= 0x13; i++) {
1074                 dprintk(1,
1075                         KERN_DEBUG
1076                         "%s_attach: reg[0x%02x] = 0x%02x (0x%02x)\n",
1077                         I2C_NAME(client), i, saa7114_read(client, i),
1078                         decoder->reg[REG_ADDR(i)]);
1079         }
1080
1081
1082         dprintk(1, KERN_DEBUG "%s_attach: setting video input\n",
1083                 I2C_NAME(client));
1084
1085         err[19] =
1086             saa7114_write(client, 0x02, decoder->reg[REG_ADDR(0x02)]);
1087         err[20] =
1088             saa7114_write(client, 0x09, decoder->reg[REG_ADDR(0x09)]);
1089         err[21] =
1090             saa7114_write(client, 0x0e, decoder->reg[REG_ADDR(0x0e)]);
1091
1092         for (i = 19; i <= 21; i++) {
1093                 if (err[i] < 0) {
1094                         dprintk(1,
1095                                 KERN_ERR
1096                                 "%s_attach: init error %d at stage %d, leaving attach.\n",
1097                                 I2C_NAME(client), i, err[i]);
1098                         kfree(decoder);
1099                         kfree(client);
1100                         return 0;
1101                 }
1102         }
1103
1104         dprintk(1,
1105                 KERN_DEBUG
1106                 "%s_attach: performing decoder reset sequence\n",
1107                 I2C_NAME(client));
1108
1109         err[22] = saa7114_write(client, 0x88, 0xd8);    // sw reset scaler
1110         err[23] = saa7114_write(client, 0x88, 0xf8);    // sw reset scaler release
1111         err[24] = saa7114_write(client, 0x80, 0x36);    // i-port and scaler backend clock selection, task A&B off
1112
1113
1114         for (i = 22; i <= 24; i++) {
1115                 if (err[i] < 0) {
1116                         dprintk(1,
1117                                 KERN_ERR
1118                                 "%s_attach: init error %d at stage %d, leaving attach.\n",
1119                                 I2C_NAME(client), i, err[i]);
1120                         kfree(decoder);
1121                         kfree(client);
1122                         return 0;
1123                 }
1124         }
1125
1126         err[25] = saa7114_write(client, 0x06, init[REG_ADDR(0x06)]);
1127         err[26] = saa7114_write(client, 0x07, init[REG_ADDR(0x07)]);
1128         err[27] = saa7114_write(client, 0x10, init[REG_ADDR(0x10)]);
1129
1130         dprintk(1,
1131                 KERN_INFO
1132                 "%s_attach: chip version %x, decoder status 0x%02x\n",
1133                 I2C_NAME(client), saa7114_read(client, 0x00) >> 4,
1134                 saa7114_read(client, 0x1f));
1135         dprintk(1,
1136                 KERN_DEBUG
1137                 "%s_attach: power save control: 0x%02x, scaler status: 0x%02x\n",
1138                 I2C_NAME(client), saa7114_read(client, 0x88),
1139                 saa7114_read(client, 0x8f));
1140
1141
1142         for (i = 0x94; i < 0x96; i++) {
1143                 dprintk(1,
1144                         KERN_DEBUG
1145                         "%s_attach: reg[0x%02x] = 0x%02x (0x%02x)\n",
1146                         I2C_NAME(client), i, saa7114_read(client, i),
1147                         decoder->reg[REG_ADDR(i)]);
1148         }
1149
1150         i = i2c_attach_client(client);
1151         if (i) {
1152                 kfree(client);
1153                 kfree(decoder);
1154                 return i;
1155         }
1156
1157         //i = saa7114_write_block(client, init, sizeof(init));
1158         i = 0;
1159         if (i < 0) {
1160                 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
1161                         I2C_NAME(client), i);
1162         } else {
1163                 dprintk(1,
1164                         KERN_INFO
1165                         "%s_attach: chip version %x at address 0x%x\n",
1166                         I2C_NAME(client), saa7114_read(client, 0x00) >> 4,
1167                         client->addr << 1);
1168         }
1169
1170         return 0;
1171 }
1172
1173 static int
1174 saa7114_attach_adapter (struct i2c_adapter *adapter)
1175 {
1176         dprintk(1,
1177                 KERN_INFO
1178                 "saa7114.c: starting probe for adapter %s (0x%x)\n",
1179                 I2C_NAME(adapter), adapter->id);
1180         return i2c_probe(adapter, &addr_data, &saa7114_detect_client);
1181 }
1182
1183 static int
1184 saa7114_detach_client (struct i2c_client *client)
1185 {
1186         struct saa7114 *decoder = i2c_get_clientdata(client);
1187         int err;
1188
1189         err = i2c_detach_client(client);
1190         if (err) {
1191                 return err;
1192         }
1193
1194         kfree(decoder);
1195         kfree(client);
1196
1197         return 0;
1198 }
1199
1200 /* ----------------------------------------------------------------------- */
1201
1202 static struct i2c_driver i2c_driver_saa7114 = {
1203         .driver = {
1204                 .name = "saa7114",
1205         },
1206
1207         .id = I2C_DRIVERID_SAA7114,
1208
1209         .attach_adapter = saa7114_attach_adapter,
1210         .detach_client = saa7114_detach_client,
1211         .command = saa7114_command,
1212 };
1213
1214 static int __init
1215 saa7114_init (void)
1216 {
1217         return i2c_add_driver(&i2c_driver_saa7114);
1218 }
1219
1220 static void __exit
1221 saa7114_exit (void)
1222 {
1223         i2c_del_driver(&i2c_driver_saa7114);
1224 }
1225
1226 module_init(saa7114_init);
1227 module_exit(saa7114_exit);