ddd68c5352d48f6cac880df4859cf3bdca3fa8b8
[linux-flexiantxendom0-3.2.10.git] / 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     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
32 #include <linux/fs.h>
33 #include <linux/kernel.h>
34 #include <linux/major.h>
35 #include <linux/slab.h>
36 #include <linux/mm.h>
37 #include <linux/pci.h>
38 #include <linux/signal.h>
39 #include <asm/io.h>
40 #include <asm/pgtable.h>
41 #include <asm/page.h>
42 #include <linux/sched.h>
43 #include <linux/types.h>
44
45 #include <linux/videodev.h>
46 #include <linux/version.h>
47 #include <asm/uaccess.h>
48
49 #include <linux/i2c.h>
50 #include <linux/video_encoder.h>
51
52 #define DEBUG(x)   x            /* Debug driver */
53
54 /* ----------------------------------------------------------------------- */
55
56 static unsigned short normal_i2c[] = {34>>1, I2C_CLIENT_END };
57 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
58 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
59 static unsigned short probe_range[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
60 static unsigned short ignore[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
61 static unsigned short ignore_range[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
62 static unsigned short force[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
63
64 static struct i2c_client_address_data addr_data = {
65         normal_i2c , normal_i2c_range,
66         probe , probe_range,
67         ignore , ignore_range,
68         force
69 };
70
71 static struct i2c_client client_template;
72
73 struct bt856 {
74         struct i2c_client *client;
75         int addr;
76         unsigned char reg[128];
77
78         int norm;
79         int enable;
80         int bright;
81         int contrast;
82         int hue;
83         int sat;
84         struct semaphore lock;
85 };
86
87 #define   I2C_BT856        0x88
88
89 #define I2C_DELAY   10
90
91 /* ----------------------------------------------------------------------- */
92
93 static int bt856_setbit(struct bt856 *dev, int subaddr, int bit, int data)
94 {
95         return i2c_smbus_write_byte_data(dev->client, subaddr,(dev->reg[subaddr] & ~(1 << bit)) | (data ? (1 << bit) : 0));
96 }
97
98 /* ----------------------------------------------------------------------- */
99
100 static int bt856_attach(struct i2c_adapter *adap, int addr , unsigned long flags, int kind)
101 {
102         struct bt856 *encoder;
103         struct i2c_client *client;
104         
105         client = kmalloc(sizeof(*client), GFP_KERNEL);
106         if(client == NULL)
107                 return -ENOMEM;
108         memset(client, 0, sizeof(*client));
109         client_template.adapter = adap;
110         client_template.addr = addr;
111         memcpy(client, &client_template, sizeof(*client));      
112         
113         /* This chip is not on the buz card but at the same address saa7185 */
114         //if (memcmp(device->bus->name, "buz", 3) == 0 || memcmp(device->bus->name, "zr36057", 6) == 0)
115         //   return 1;
116
117         encoder = kmalloc(sizeof(struct bt856), GFP_KERNEL);
118
119         if (encoder == NULL) {
120                 MOD_DEC_USE_COUNT;
121                 return -ENOMEM;
122         }
123
124
125         memset(encoder, 0, sizeof(struct bt856));
126         strlcpy(client->dev.name, "bt856", DEVICE_NAME_SIZE);
127         encoder->client = client;
128         i2c_set_clientdata(client, encoder);
129         encoder->addr = client->addr;
130         encoder->norm = VIDEO_MODE_NTSC;
131         encoder->enable = 1;
132
133         DEBUG(printk(KERN_INFO "%s-bt856: attach\n", encoder->client->dev.name));
134
135         i2c_smbus_write_byte_data(client, 0xdc, 0x18);
136         encoder->reg[0xdc] = 0x18;
137         i2c_smbus_write_byte_data(client, 0xda, 0);
138         encoder->reg[0xda] = 0;
139         i2c_smbus_write_byte_data(client, 0xde, 0);
140         encoder->reg[0xde] = 0;
141
142         bt856_setbit(encoder, 0xdc, 3, 1);
143         //bt856_setbit(encoder, 0xdc, 6, 0);
144         bt856_setbit(encoder, 0xdc, 4, 1);
145
146         switch (encoder->norm) {
147
148         case VIDEO_MODE_NTSC:
149                 bt856_setbit(encoder, 0xdc, 2, 0);
150                 break;
151
152         case VIDEO_MODE_PAL:
153                 bt856_setbit(encoder, 0xdc, 2, 1);
154                 break;
155         }
156
157         bt856_setbit(encoder, 0xdc, 1, 1);
158         bt856_setbit(encoder, 0xde, 4, 0);
159         bt856_setbit(encoder, 0xde, 3, 1);
160         init_MUTEX(&encoder->lock);
161         i2c_attach_client(client);
162         MOD_INC_USE_COUNT;
163         return 0;
164 }
165
166 static int bt856_probe(struct i2c_adapter *adap)
167 {
168         return i2c_probe(adap, &addr_data , bt856_attach);
169 }
170
171 static int bt856_detach(struct i2c_client *client)
172 {
173         i2c_detach_client(client);
174         i2c_get_clientdata(client);
175         kfree(client);
176         MOD_DEC_USE_COUNT;
177         return 0;
178 }
179
180 static int bt856_command(struct i2c_client *client, unsigned int cmd,
181                          void *arg)
182 {
183         struct bt856 *encoder = i2c_get_clientdata(client);
184
185         switch (cmd) {
186
187         case ENCODER_GET_CAPABILITIES:
188                 {
189                         struct video_encoder_capability *cap = arg;
190
191                         DEBUG(printk
192                               (KERN_INFO "%s-bt856: get capabilities\n",
193                                encoder->client->dev.name));
194
195                         cap->flags
196                             = VIDEO_ENCODER_PAL
197                             | VIDEO_ENCODER_NTSC | VIDEO_ENCODER_CCIR;
198                         cap->inputs = 2;
199                         cap->outputs = 1;
200                 }
201                 break;
202
203         case ENCODER_SET_NORM:
204                 {
205                         int *iarg = arg;
206
207                         DEBUG(printk(KERN_INFO "%s-bt856: set norm %d\n",
208                                      encoder->client->dev.name, *iarg));
209
210                         switch (*iarg) {
211
212                         case VIDEO_MODE_NTSC:
213                                 bt856_setbit(encoder, 0xdc, 2, 0);
214                                 break;
215
216                         case VIDEO_MODE_PAL:
217                                 bt856_setbit(encoder, 0xdc, 2, 1);
218                                 bt856_setbit(encoder, 0xda, 0, 0);
219                                 //bt856_setbit(encoder, 0xda, 0, 1);
220                                 break;
221
222                         default:
223                                 return -EINVAL;
224
225                         }
226                         encoder->norm = *iarg;
227                 }
228                 break;
229
230         case ENCODER_SET_INPUT:
231                 {
232                         int *iarg = arg;
233
234                         DEBUG(printk(KERN_INFO "%s-bt856: set input %d\n",
235                                      encoder->client->dev.name, *iarg));
236
237                         /*     We only have video bus.
238                            *iarg = 0: input is from bt819
239                            *iarg = 1: input is from ZR36060 */
240
241                         switch (*iarg) {
242
243                         case 0:
244                                 bt856_setbit(encoder, 0xde, 4, 0);
245                                 bt856_setbit(encoder, 0xde, 3, 1);
246                                 bt856_setbit(encoder, 0xdc, 3, 1);
247                                 bt856_setbit(encoder, 0xdc, 6, 0);
248                                 break;
249                         case 1:
250                                 bt856_setbit(encoder, 0xde, 4, 0);
251                                 bt856_setbit(encoder, 0xde, 3, 1);
252                                 bt856_setbit(encoder, 0xdc, 3, 1);
253                                 bt856_setbit(encoder, 0xdc, 6, 1);
254                                 break;
255                         case 2: // Color bar
256                                 bt856_setbit(encoder, 0xdc, 3, 0);
257                                 bt856_setbit(encoder, 0xde, 4, 1);
258                                 break;
259                         default:
260                                 return -EINVAL;
261
262                         }
263                 }
264                 break;
265
266         case ENCODER_SET_OUTPUT:
267                 {
268                         int *iarg = arg;
269
270                         DEBUG(printk(KERN_INFO "%s-bt856: set output %d\n",
271                                      encoder->client->dev.name, *iarg));
272
273                         /* not much choice of outputs */
274                         if (*iarg != 0) {
275                                 return -EINVAL;
276                         }
277                 }
278                 break;
279
280         case ENCODER_ENABLE_OUTPUT:
281                 {
282                         int *iarg = arg;
283
284                         encoder->enable = !!*iarg;
285
286                         DEBUG(printk
287                               (KERN_INFO "%s-bt856: enable output %d\n",
288                                encoder->client->dev.name, encoder->enable));
289                 }
290                 break;
291
292         default:
293                 return -EINVAL;
294         }
295
296         return 0;
297 }
298
299 /* ----------------------------------------------------------------------- */
300
301 static struct i2c_driver i2c_driver_bt856 = {
302         .owner = THIS_MODULE,
303         .name = "bt856",                /* name */
304         .id = I2C_DRIVERID_BT856,       /* ID */
305         .flags = I2C_DF_NOTIFY,
306         .attach_adapter = bt856_probe,
307         .detach_client = bt856_detach,
308         .command = bt856_command
309 };
310
311 static struct i2c_client client_template = {
312         .id = -1,
313         .driver = &i2c_driver_bt856,
314         .dev = {
315                 .name = "bt856_client",
316         },
317 };
318
319 static int bt856_init(void)
320 {
321         return i2c_add_driver(&i2c_driver_bt856);
322 }
323
324 static void bt856_exit(void)
325 {
326         i2c_del_driver(&i2c_driver_bt856);
327 }
328
329 module_init(bt856_init);
330 module_exit(bt856_exit);
331 MODULE_LICENSE("GPL");