]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/bt819.c
[PATCH] I2C: Drop unneeded i2c-dev.h includes
[linux-2.6-omap-h63xx.git] / drivers / media / video / bt819.c
1 /* 
2  *  bt819 - BT819A VideoStream Decoder (Rockwell Part)
3  *
4  * Copyright (C) 1999 Mike Bernson <mike@mlb.org>
5  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6  *
7  * Modifications for LML33/DC10plus unified driver
8  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9  *  
10  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
11  *    - moved over to linux>=2.4.x i2c protocol (9/9/2002)
12  *
13  * This code was modify/ported from the saa7111 driver written
14  * by Dave Perks.
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 #include <linux/slab.h>
39 #include <linux/mm.h>
40 #include <linux/pci.h>
41 #include <linux/signal.h>
42 #include <asm/io.h>
43 #include <asm/pgtable.h>
44 #include <asm/page.h>
45 #include <linux/sched.h>
46 #include <linux/types.h>
47
48 #include <linux/videodev.h>
49 #include <asm/uaccess.h>
50
51 MODULE_DESCRIPTION("Brooktree-819 video decoder driver");
52 MODULE_AUTHOR("Mike Bernson & Dave Perks");
53 MODULE_LICENSE("GPL");
54
55 #include <linux/i2c.h>
56
57 #define I2C_NAME(s) (s)->name
58
59 #include <linux/video_decoder.h>
60
61 static int debug = 0;
62 module_param(debug, int, 0);
63 MODULE_PARM_DESC(debug, "Debug level (0-1)");
64
65 #define dprintk(num, format, args...) \
66         do { \
67                 if (debug >= num) \
68                         printk(format, ##args); \
69         } while (0)
70
71 /* ----------------------------------------------------------------------- */
72
73 struct bt819 {
74         unsigned char reg[32];
75
76         int initialized;
77         int norm;
78         int input;
79         int enable;
80         int bright;
81         int contrast;
82         int hue;
83         int sat;
84 };
85
86 struct timing {
87         int hactive;
88         int hdelay;
89         int vactive;
90         int vdelay;
91         int hscale;
92         int vscale;
93 };
94
95 /* for values, see the bt819 datasheet */
96 static struct timing timing_data[] = {
97         {864 - 24, 20, 625 - 2, 1, 0x0504, 0x0000},
98         {858 - 24, 20, 525 - 2, 1, 0x00f8, 0x0000},
99 };
100
101 #define   I2C_BT819        0x8a
102
103 /* ----------------------------------------------------------------------- */
104
105 static inline int
106 bt819_write (struct i2c_client *client,
107              u8                 reg,
108              u8                 value)
109 {
110         struct bt819 *decoder = i2c_get_clientdata(client);
111
112         decoder->reg[reg] = value;
113         return i2c_smbus_write_byte_data(client, reg, value);
114 }
115
116 static inline int
117 bt819_setbit (struct i2c_client *client,
118               u8                 reg,
119               u8                 bit,
120               u8                 value)
121 {
122         struct bt819 *decoder = i2c_get_clientdata(client);
123
124         return bt819_write(client, reg,
125                            (decoder->
126                             reg[reg] & ~(1 << bit)) |
127                             (value ? (1 << bit) : 0));
128 }
129
130 static int
131 bt819_write_block (struct i2c_client *client,
132                    const u8          *data,
133                    unsigned int       len)
134 {
135         int ret = -1;
136         u8 reg;
137
138         /* the bt819 has an autoincrement function, use it if
139          * the adapter understands raw I2C */
140         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
141                 /* do raw I2C, not smbus compatible */
142                 struct bt819 *decoder = i2c_get_clientdata(client);
143                 struct i2c_msg msg;
144                 u8 block_data[32];
145
146                 msg.addr = client->addr;
147                 msg.flags = 0;
148                 while (len >= 2) {
149                         msg.buf = (char *) block_data;
150                         msg.len = 0;
151                         block_data[msg.len++] = reg = data[0];
152                         do {
153                                 block_data[msg.len++] =
154                                     decoder->reg[reg++] = data[1];
155                                 len -= 2;
156                                 data += 2;
157                         } while (len >= 2 && data[0] == reg &&
158                                  msg.len < 32);
159                         if ((ret = i2c_transfer(client->adapter,
160                                                 &msg, 1)) < 0)
161                                 break;
162                 }
163         } else {
164                 /* do some slow I2C emulation kind of thing */
165                 while (len >= 2) {
166                         reg = *data++;
167                         if ((ret = bt819_write(client, reg, *data++)) < 0)
168                                 break;
169                         len -= 2;
170                 }
171         }
172
173         return ret;
174 }
175
176 static inline int
177 bt819_read (struct i2c_client *client,
178             u8                 reg)
179 {
180         return i2c_smbus_read_byte_data(client, reg);
181 }
182
183 static int
184 bt819_init (struct i2c_client *client)
185 {
186         struct bt819 *decoder = i2c_get_clientdata(client);
187
188         static unsigned char init[] = {
189                 //0x1f, 0x00,     /* Reset */
190                 0x01, 0x59,     /* 0x01 input format */
191                 0x02, 0x00,     /* 0x02 temporal decimation */
192                 0x03, 0x12,     /* 0x03 Cropping msb */
193                 0x04, 0x16,     /* 0x04 Vertical Delay, lsb */
194                 0x05, 0xe0,     /* 0x05 Vertical Active lsb */
195                 0x06, 0x80,     /* 0x06 Horizontal Delay lsb */
196                 0x07, 0xd0,     /* 0x07 Horizontal Active lsb */
197                 0x08, 0x00,     /* 0x08 Horizontal Scaling msb */
198                 0x09, 0xf8,     /* 0x09 Horizontal Scaling lsb */
199                 0x0a, 0x00,     /* 0x0a Brightness control */
200                 0x0b, 0x30,     /* 0x0b Miscellaneous control */
201                 0x0c, 0xd8,     /* 0x0c Luma Gain lsb */
202                 0x0d, 0xfe,     /* 0x0d Chroma Gain (U) lsb */
203                 0x0e, 0xb4,     /* 0x0e Chroma Gain (V) msb */
204                 0x0f, 0x00,     /* 0x0f Hue control */
205                 0x12, 0x04,     /* 0x12 Output Format */
206                 0x13, 0x20,     /* 0x13 Vertial Scaling msb 0x00
207                                            chroma comb OFF, line drop scaling, interlace scaling
208                                            BUG? Why does turning the chroma comb on fuck up color?
209                                            Bug in the bt819 stepping on my board?
210                                         */
211                 0x14, 0x00,     /* 0x14 Vertial Scaling lsb */
212                 0x16, 0x07,     /* 0x16 Video Timing Polarity 
213                                            ACTIVE=active low
214                                            FIELD: high=odd, 
215                                            vreset=active high,
216                                            hreset=active high */
217                 0x18, 0x68,     /* 0x18 AGC Delay */
218                 0x19, 0x5d,     /* 0x19 Burst Gate Delay */
219                 0x1a, 0x80,     /* 0x1a ADC Interface */
220         };
221
222         struct timing *timing = &timing_data[decoder->norm];
223
224         init[0x03 * 2 - 1] =
225             (((timing->vdelay >> 8) & 0x03) << 6) | (((timing->
226                                                        vactive >> 8) &
227                                                       0x03) << 4) |
228             (((timing->hdelay >> 8) & 0x03) << 2) | ((timing->
229                                                       hactive >> 8) &
230                                                      0x03);
231         init[0x04 * 2 - 1] = timing->vdelay & 0xff;
232         init[0x05 * 2 - 1] = timing->vactive & 0xff;
233         init[0x06 * 2 - 1] = timing->hdelay & 0xff;
234         init[0x07 * 2 - 1] = timing->hactive & 0xff;
235         init[0x08 * 2 - 1] = timing->hscale >> 8;
236         init[0x09 * 2 - 1] = timing->hscale & 0xff;
237         /* 0x15 in array is address 0x19 */
238         init[0x15 * 2 - 1] = (decoder->norm == 0) ? 115 : 93;   /* Chroma burst delay */
239         /* reset */
240         bt819_write(client, 0x1f, 0x00);
241         mdelay(1);
242
243         /* init */
244         return bt819_write_block(client, init, sizeof(init));
245
246 }
247
248 /* ----------------------------------------------------------------------- */
249
250 static int
251 bt819_command (struct i2c_client *client,
252                unsigned int       cmd,
253                void              *arg)
254 {
255         int temp;
256
257         struct bt819 *decoder = i2c_get_clientdata(client);
258
259         if (!decoder->initialized) {    // First call to bt819_init could be
260                 bt819_init(client);     // without #FRST = 0
261                 decoder->initialized = 1;
262         }
263
264         switch (cmd) {
265
266         case 0:
267                 /* This is just for testing!!! */
268                 bt819_init(client);
269                 break;
270
271         case DECODER_GET_CAPABILITIES:
272         {
273                 struct video_decoder_capability *cap = arg;
274
275                 cap->flags = VIDEO_DECODER_PAL |
276                              VIDEO_DECODER_NTSC |
277                              VIDEO_DECODER_AUTO |
278                              VIDEO_DECODER_CCIR;
279                 cap->inputs = 8;
280                 cap->outputs = 1;
281         }
282                 break;
283
284         case DECODER_GET_STATUS:
285         {
286                 int *iarg = arg;
287                 int status;
288                 int res;
289
290                 status = bt819_read(client, 0x00);
291                 res = 0;
292                 if ((status & 0x80)) {
293                         res |= DECODER_STATUS_GOOD;
294                 }
295                 switch (decoder->norm) {
296                 case VIDEO_MODE_NTSC:
297                         res |= DECODER_STATUS_NTSC;
298                         break;
299                 case VIDEO_MODE_PAL:
300                         res |= DECODER_STATUS_PAL;
301                         break;
302                 default:
303                 case VIDEO_MODE_AUTO:
304                         if ((status & 0x10)) {
305                                 res |= DECODER_STATUS_PAL;
306                         } else {
307                                 res |= DECODER_STATUS_NTSC;
308                         }
309                         break;
310                 }
311                 res |= DECODER_STATUS_COLOR;
312                 *iarg = res;
313
314                 dprintk(1, KERN_INFO "%s: get status %x\n", I2C_NAME(client),
315                         *iarg);
316         }
317                 break;
318
319         case DECODER_SET_NORM:
320         {
321                 int *iarg = arg;
322                 struct timing *timing = NULL;
323
324                 dprintk(1, KERN_INFO "%s: set norm %x\n", I2C_NAME(client),
325                         *iarg);
326
327                 switch (*iarg) {
328                 case VIDEO_MODE_NTSC:
329                         bt819_setbit(client, 0x01, 0, 1);
330                         bt819_setbit(client, 0x01, 1, 0);
331                         bt819_setbit(client, 0x01, 5, 0);
332                         bt819_write(client, 0x18, 0x68);
333                         bt819_write(client, 0x19, 0x5d);
334                         //bt819_setbit(client, 0x1a,  5, 1);
335                         timing = &timing_data[VIDEO_MODE_NTSC];
336                         break;
337                 case VIDEO_MODE_PAL:
338                         bt819_setbit(client, 0x01, 0, 1);
339                         bt819_setbit(client, 0x01, 1, 1);
340                         bt819_setbit(client, 0x01, 5, 1);
341                         bt819_write(client, 0x18, 0x7f);
342                         bt819_write(client, 0x19, 0x72);
343                         //bt819_setbit(client, 0x1a,  5, 0);
344                         timing = &timing_data[VIDEO_MODE_PAL];
345                         break;
346                 case VIDEO_MODE_AUTO:
347                         bt819_setbit(client, 0x01, 0, 0);
348                         bt819_setbit(client, 0x01, 1, 0);
349                         break;
350                 default:
351                         dprintk(1,
352                                 KERN_ERR
353                                 "%s: unsupported norm %d\n",
354                                 I2C_NAME(client), *iarg);
355                         return -EINVAL;
356                 }
357
358                 if (timing) {
359                         bt819_write(client, 0x03,
360                                     (((timing->vdelay >> 8) & 0x03) << 6) |
361                                     (((timing->vactive >> 8) & 0x03) << 4) |
362                                     (((timing->hdelay >> 8) & 0x03) << 2) |
363                                      ((timing->hactive >> 8) & 0x03) );
364                         bt819_write(client, 0x04, timing->vdelay & 0xff);
365                         bt819_write(client, 0x05, timing->vactive & 0xff);
366                         bt819_write(client, 0x06, timing->hdelay & 0xff);
367                         bt819_write(client, 0x07, timing->hactive & 0xff);
368                         bt819_write(client, 0x08, (timing->hscale >> 8) & 0xff);
369                         bt819_write(client, 0x09, timing->hscale & 0xff);
370                 }
371
372                 decoder->norm = *iarg;
373         }
374                 break;
375
376         case DECODER_SET_INPUT:
377         {
378                 int *iarg = arg;
379
380                 dprintk(1, KERN_INFO "%s: set input %x\n", I2C_NAME(client),
381                         *iarg);
382
383                 if (*iarg < 0 || *iarg > 7) {
384                         return -EINVAL;
385                 }
386
387                 if (decoder->input != *iarg) {
388                         decoder->input = *iarg;
389                         /* select mode */
390                         if (decoder->input == 0) {
391                                 bt819_setbit(client, 0x0b, 6, 0);
392                                 bt819_setbit(client, 0x1a, 1, 1);
393                         } else {
394                                 bt819_setbit(client, 0x0b, 6, 1);
395                                 bt819_setbit(client, 0x1a, 1, 0);
396                         }
397                 }
398         }
399                 break;
400
401         case DECODER_SET_OUTPUT:
402         {
403                 int *iarg = arg;
404
405                 dprintk(1, KERN_INFO "%s: set output %x\n", I2C_NAME(client),
406                         *iarg);
407
408                 /* not much choice of outputs */
409                 if (*iarg != 0) {
410                         return -EINVAL;
411                 }
412         }
413                 break;
414
415         case DECODER_ENABLE_OUTPUT:
416         {
417                 int *iarg = arg;
418                 int enable = (*iarg != 0);
419
420                 dprintk(1, KERN_INFO "%s: enable output %x\n",
421                         I2C_NAME(client), *iarg);
422
423                 if (decoder->enable != enable) {
424                         decoder->enable = enable;
425
426                         if (decoder->enable) {
427                                 bt819_setbit(client, 0x16, 7, 0);
428                         } else {
429                                 bt819_setbit(client, 0x16, 7, 1);
430                         }
431                 }
432         }
433                 break;
434
435         case DECODER_SET_PICTURE:
436         {
437                 struct video_picture *pic = arg;
438
439                 dprintk(1,
440                         KERN_INFO
441                         "%s: set picture brightness %d contrast %d colour %d\n",
442                         I2C_NAME(client), pic->brightness, pic->contrast,
443                         pic->colour);
444
445
446                 if (decoder->bright != pic->brightness) {
447                         /* We want -128 to 127 we get 0-65535 */
448                         decoder->bright = pic->brightness;
449                         bt819_write(client, 0x0a,
450                                     (decoder->bright >> 8) - 128);
451                 }
452
453                 if (decoder->contrast != pic->contrast) {
454                         /* We want 0 to 511 we get 0-65535 */
455                         decoder->contrast = pic->contrast;
456                         bt819_write(client, 0x0c,
457                                     (decoder->contrast >> 7) & 0xff);
458                         bt819_setbit(client, 0x0b, 2,
459                                      ((decoder->contrast >> 15) & 0x01));
460                 }
461
462                 if (decoder->sat != pic->colour) {
463                         /* We want 0 to 511 we get 0-65535 */
464                         decoder->sat = pic->colour;
465                         bt819_write(client, 0x0d,
466                                     (decoder->sat >> 7) & 0xff);
467                         bt819_setbit(client, 0x0b, 1,
468                                      ((decoder->sat >> 15) & 0x01));
469
470                         temp = (decoder->sat * 201) / 237;
471                         bt819_write(client, 0x0e, (temp >> 7) & 0xff);
472                         bt819_setbit(client, 0x0b, 0, (temp >> 15) & 0x01);
473                 }
474
475                 if (decoder->hue != pic->hue) {
476                         /* We want -128 to 127 we get 0-65535 */
477                         decoder->hue = pic->hue;
478                         bt819_write(client, 0x0f,
479                                     128 - (decoder->hue >> 8));
480                 }
481         }
482                 break;
483
484         default:
485                 return -EINVAL;
486         }
487
488         return 0;
489 }
490
491 /* ----------------------------------------------------------------------- */
492
493 /*
494  * Generic i2c probe
495  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
496  */
497 static unsigned short normal_i2c[] = {
498         I2C_BT819 >> 1,
499         I2C_CLIENT_END,
500 };
501
502 static unsigned short ignore = I2C_CLIENT_END;
503                                                                                 
504 static struct i2c_client_address_data addr_data = {
505         .normal_i2c             = normal_i2c,
506         .probe                  = &ignore,
507         .ignore                 = &ignore,
508 };
509
510 static struct i2c_driver i2c_driver_bt819;
511
512 static int
513 bt819_detect_client (struct i2c_adapter *adapter,
514                      int                 address,
515                      int                 kind)
516 {
517         int i, id;
518         struct bt819 *decoder;
519         struct i2c_client *client;
520
521         dprintk(1,
522                 KERN_INFO
523                 "saa7111.c: detecting bt819 client on address 0x%x\n",
524                 address << 1);
525
526         /* Check if the adapter supports the needed features */
527         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
528                 return 0;
529
530         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
531         if (client == 0)
532                 return -ENOMEM;
533         client->addr = address;
534         client->adapter = adapter;
535         client->driver = &i2c_driver_bt819;
536
537         decoder = kzalloc(sizeof(struct bt819), GFP_KERNEL);
538         if (decoder == NULL) {
539                 kfree(client);
540                 return -ENOMEM;
541         }
542         decoder->norm = VIDEO_MODE_NTSC;
543         decoder->input = 0;
544         decoder->enable = 1;
545         decoder->bright = 32768;
546         decoder->contrast = 32768;
547         decoder->hue = 32768;
548         decoder->sat = 32768;
549         decoder->initialized = 0;
550         i2c_set_clientdata(client, decoder);
551
552         id = bt819_read(client, 0x17);
553         switch (id & 0xf0) {
554         case 0x70:
555                 strlcpy(I2C_NAME(client), "bt819a", sizeof(I2C_NAME(client)));
556                 break;
557         case 0x60:
558                 strlcpy(I2C_NAME(client), "bt817a", sizeof(I2C_NAME(client)));
559                 break;
560         case 0x20:
561                 strlcpy(I2C_NAME(client), "bt815a", sizeof(I2C_NAME(client)));
562                 break;
563         default:
564                 dprintk(1,
565                         KERN_ERR
566                         "bt819: unknown chip version 0x%x (ver 0x%x)\n",
567                         id & 0xf0, id & 0x0f);
568                 kfree(decoder);
569                 kfree(client);
570                 return 0;
571         }
572
573         i = i2c_attach_client(client);
574         if (i) {
575                 kfree(client);
576                 kfree(decoder);
577                 return i;
578         }
579
580         i = bt819_init(client);
581         if (i < 0) {
582                 dprintk(1, KERN_ERR "%s_attach: init status %d\n",
583                         I2C_NAME(client), i);
584         } else {
585                 dprintk(1,
586                         KERN_INFO
587                         "%s_attach: chip version 0x%x at address 0x%x\n",
588                         I2C_NAME(client), id & 0x0f,
589                         client->addr << 1);
590         }
591
592         return 0;
593 }
594
595 static int
596 bt819_attach_adapter (struct i2c_adapter *adapter)
597 {
598         return i2c_probe(adapter, &addr_data, &bt819_detect_client);
599 }
600
601 static int
602 bt819_detach_client (struct i2c_client *client)
603 {
604         struct bt819 *decoder = i2c_get_clientdata(client);
605         int err;
606
607         err = i2c_detach_client(client);
608         if (err) {
609                 return err;
610         }
611
612         kfree(decoder);
613         kfree(client);
614
615         return 0;
616 }
617
618 /* ----------------------------------------------------------------------- */
619
620 static struct i2c_driver i2c_driver_bt819 = {
621         .driver = {
622                 .name = "bt819",
623         },
624
625         .id = I2C_DRIVERID_BT819,
626
627         .attach_adapter = bt819_attach_adapter,
628         .detach_client = bt819_detach_client,
629         .command = bt819_command,
630 };
631
632 static int __init
633 bt819_init_module (void)
634 {
635         return i2c_add_driver(&i2c_driver_bt819);
636 }
637
638 static void __exit
639 bt819_exit (void)
640 {
641         i2c_del_driver(&i2c_driver_bt819);
642 }
643
644 module_init(bt819_init_module);
645 module_exit(bt819_exit);