V4L/DVB (4638): Basic DVB-T and analog TV support for the HVR1300.
[linux-2.6] / drivers / media / video / bt856.c
1 /*
2  * bt856 - BT856A Digital Video Encoder (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  * This code was modify/ported from the saa7111 driver written
11  * by Dave Perks.
12  *
13  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14  *   - moved over to linux>=2.4.x i2c protocol (9/9/2002)
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-856A video encoder 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_encoder.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 #define REG_OFFSET      0xDA
74 #define BT856_NR_REG    6
75
76 struct bt856 {
77         unsigned char reg[BT856_NR_REG];
78
79         int norm;
80         int enable;
81 };
82
83 #define   I2C_BT856        0x88
84
85 /* ----------------------------------------------------------------------- */
86
87 static inline int
88 bt856_write (struct i2c_client *client,
89              u8                 reg,
90              u8                 value)
91 {
92         struct bt856 *encoder = i2c_get_clientdata(client);
93
94         encoder->reg[reg - REG_OFFSET] = value;
95         return i2c_smbus_write_byte_data(client, reg, value);
96 }
97
98 static inline int
99 bt856_setbit (struct i2c_client *client,
100               u8                 reg,
101               u8                 bit,
102               u8                 value)
103 {
104         struct bt856 *encoder = i2c_get_clientdata(client);
105
106         return bt856_write(client, reg,
107                            (encoder->
108                             reg[reg - REG_OFFSET] & ~(1 << bit)) |
109                             (value ? (1 << bit) : 0));
110 }
111
112 static void
113 bt856_dump (struct i2c_client *client)
114 {
115         int i;
116         struct bt856 *encoder = i2c_get_clientdata(client);
117
118         printk(KERN_INFO "%s: register dump:", I2C_NAME(client));
119         for (i = 0; i < BT856_NR_REG; i += 2)
120                 printk(" %02x", encoder->reg[i]);
121         printk("\n");
122 }
123
124 /* ----------------------------------------------------------------------- */
125
126 static int
127 bt856_command (struct i2c_client *client,
128                unsigned int       cmd,
129                void              *arg)
130 {
131         struct bt856 *encoder = i2c_get_clientdata(client);
132
133         switch (cmd) {
134
135         case 0:
136                 /* This is just for testing!!! */
137                 dprintk(1, KERN_INFO "bt856: init\n");
138                 bt856_write(client, 0xdc, 0x18);
139                 bt856_write(client, 0xda, 0);
140                 bt856_write(client, 0xde, 0);
141
142                 bt856_setbit(client, 0xdc, 3, 1);
143                 //bt856_setbit(client, 0xdc, 6, 0);
144                 bt856_setbit(client, 0xdc, 4, 1);
145
146                 switch (encoder->norm) {
147
148                 case VIDEO_MODE_NTSC:
149                         bt856_setbit(client, 0xdc, 2, 0);
150                         break;
151
152                 case VIDEO_MODE_PAL:
153                         bt856_setbit(client, 0xdc, 2, 1);
154                         break;
155                 }
156
157                 bt856_setbit(client, 0xdc, 1, 1);
158                 bt856_setbit(client, 0xde, 4, 0);
159                 bt856_setbit(client, 0xde, 3, 1);
160                 if (debug != 0)
161                         bt856_dump(client);
162                 break;
163
164         case ENCODER_GET_CAPABILITIES:
165         {
166                 struct video_encoder_capability *cap = arg;
167
168                 dprintk(1, KERN_INFO "%s: get capabilities\n",
169                         I2C_NAME(client));
170
171                 cap->flags = VIDEO_ENCODER_PAL |
172                              VIDEO_ENCODER_NTSC |
173                              VIDEO_ENCODER_CCIR;
174                 cap->inputs = 2;
175                 cap->outputs = 1;
176         }
177                 break;
178
179         case ENCODER_SET_NORM:
180         {
181                 int *iarg = arg;
182
183                 dprintk(1, KERN_INFO "%s: set norm %d\n", I2C_NAME(client),
184                         *iarg);
185
186                 switch (*iarg) {
187
188                 case VIDEO_MODE_NTSC:
189                         bt856_setbit(client, 0xdc, 2, 0);
190                         break;
191
192                 case VIDEO_MODE_PAL:
193                         bt856_setbit(client, 0xdc, 2, 1);
194                         bt856_setbit(client, 0xda, 0, 0);
195                         //bt856_setbit(client, 0xda, 0, 1);
196                         break;
197
198                 default:
199                         return -EINVAL;
200
201                 }
202                 encoder->norm = *iarg;
203                 if (debug != 0)
204                         bt856_dump(client);
205         }
206                 break;
207
208         case ENCODER_SET_INPUT:
209         {
210                 int *iarg = arg;
211
212                 dprintk(1, KERN_INFO "%s: set input %d\n", I2C_NAME(client),
213                         *iarg);
214
215                 /* We only have video bus.
216                  * iarg = 0: input is from bt819
217                  * iarg = 1: input is from ZR36060 */
218
219                 switch (*iarg) {
220
221                 case 0:
222                         bt856_setbit(client, 0xde, 4, 0);
223                         bt856_setbit(client, 0xde, 3, 1);
224                         bt856_setbit(client, 0xdc, 3, 1);
225                         bt856_setbit(client, 0xdc, 6, 0);
226                         break;
227                 case 1:
228                         bt856_setbit(client, 0xde, 4, 0);
229                         bt856_setbit(client, 0xde, 3, 1);
230                         bt856_setbit(client, 0xdc, 3, 1);
231                         bt856_setbit(client, 0xdc, 6, 1);
232                         break;
233                 case 2: // Color bar
234                         bt856_setbit(client, 0xdc, 3, 0);
235                         bt856_setbit(client, 0xde, 4, 1);
236                         break;
237                 default:
238                         return -EINVAL;
239
240                 }
241
242                 if (debug != 0)
243                         bt856_dump(client);
244         }
245                 break;
246
247         case ENCODER_SET_OUTPUT:
248         {
249                 int *iarg = arg;
250
251                 dprintk(1, KERN_INFO "%s: set output %d\n", I2C_NAME(client),
252                         *iarg);
253
254                 /* not much choice of outputs */
255                 if (*iarg != 0) {
256                         return -EINVAL;
257                 }
258         }
259                 break;
260
261         case ENCODER_ENABLE_OUTPUT:
262         {
263                 int *iarg = arg;
264
265                 encoder->enable = !!*iarg;
266
267                 dprintk(1, KERN_INFO "%s: enable output %d\n",
268                         I2C_NAME(client), encoder->enable);
269         }
270                 break;
271
272         default:
273                 return -EINVAL;
274         }
275
276         return 0;
277 }
278
279 /* ----------------------------------------------------------------------- */
280
281 /*
282  * Generic i2c probe
283  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
284  */
285 static unsigned short normal_i2c[] = { I2C_BT856 >> 1, I2C_CLIENT_END };
286
287 static unsigned short ignore = I2C_CLIENT_END;
288
289 static struct i2c_client_address_data addr_data = {
290         .normal_i2c             = normal_i2c,
291         .probe                  = &ignore,
292         .ignore                 = &ignore,
293 };
294
295 static struct i2c_driver i2c_driver_bt856;
296
297 static int
298 bt856_detect_client (struct i2c_adapter *adapter,
299                      int                 address,
300                      int                 kind)
301 {
302         int i;
303         struct i2c_client *client;
304         struct bt856 *encoder;
305
306         dprintk(1,
307                 KERN_INFO
308                 "bt856.c: detecting bt856 client on address 0x%x\n",
309                 address << 1);
310
311         /* Check if the adapter supports the needed features */
312         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
313                 return 0;
314
315         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
316         if (client == 0)
317                 return -ENOMEM;
318         client->addr = address;
319         client->adapter = adapter;
320         client->driver = &i2c_driver_bt856;
321         strlcpy(I2C_NAME(client), "bt856", sizeof(I2C_NAME(client)));
322
323         encoder = kzalloc(sizeof(struct bt856), GFP_KERNEL);
324         if (encoder == NULL) {
325                 kfree(client);
326                 return -ENOMEM;
327         }
328         encoder->norm = VIDEO_MODE_NTSC;
329         encoder->enable = 1;
330         i2c_set_clientdata(client, encoder);
331
332         i = i2c_attach_client(client);
333         if (i) {
334                 kfree(client);
335                 kfree(encoder);
336                 return i;
337         }
338
339         bt856_write(client, 0xdc, 0x18);
340         bt856_write(client, 0xda, 0);
341         bt856_write(client, 0xde, 0);
342
343         bt856_setbit(client, 0xdc, 3, 1);
344         //bt856_setbit(client, 0xdc, 6, 0);
345         bt856_setbit(client, 0xdc, 4, 1);
346
347         switch (encoder->norm) {
348
349         case VIDEO_MODE_NTSC:
350                 bt856_setbit(client, 0xdc, 2, 0);
351                 break;
352
353         case VIDEO_MODE_PAL:
354                 bt856_setbit(client, 0xdc, 2, 1);
355                 break;
356         }
357
358         bt856_setbit(client, 0xdc, 1, 1);
359         bt856_setbit(client, 0xde, 4, 0);
360         bt856_setbit(client, 0xde, 3, 1);
361
362         if (debug != 0)
363                 bt856_dump(client);
364
365         dprintk(1, KERN_INFO "%s_attach: at address 0x%x\n", I2C_NAME(client),
366                 client->addr << 1);
367
368         return 0;
369 }
370
371 static int
372 bt856_attach_adapter (struct i2c_adapter *adapter)
373 {
374         dprintk(1,
375                 KERN_INFO
376                 "bt856.c: starting probe for adapter %s (0x%x)\n",
377                 I2C_NAME(adapter), adapter->id);
378         return i2c_probe(adapter, &addr_data, &bt856_detect_client);
379 }
380
381 static int
382 bt856_detach_client (struct i2c_client *client)
383 {
384         struct bt856 *encoder = i2c_get_clientdata(client);
385         int err;
386
387         err = i2c_detach_client(client);
388         if (err) {
389                 return err;
390         }
391
392         kfree(encoder);
393         kfree(client);
394
395         return 0;
396 }
397
398 /* ----------------------------------------------------------------------- */
399
400 static struct i2c_driver i2c_driver_bt856 = {
401         .driver = {
402                 .name = "bt856",
403         },
404
405         .id = I2C_DRIVERID_BT856,
406
407         .attach_adapter = bt856_attach_adapter,
408         .detach_client = bt856_detach_client,
409         .command = bt856_command,
410 };
411
412 static int __init
413 bt856_init (void)
414 {
415         return i2c_add_driver(&i2c_driver_bt856);
416 }
417
418 static void __exit
419 bt856_exit (void)
420 {
421         i2c_del_driver(&i2c_driver_bt856);
422 }
423
424 module_init(bt856_init);
425 module_exit(bt856_exit);