ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / tea6415c.c
1  /*
2     tea6415c.h - i2c-driver for the tea6415c by SGS Thomson   
3
4     Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
5
6     The tea6415c is a bus controlled video-matrix-switch
7     with 8 inputs and 6 outputs.
8     It is cascadable, i.e. it can be found at the addresses
9     0x86 and 0x06 on the i2c-bus.
10     
11     For detailed informations download the specifications directly
12     from SGS Thomson at http://www.st.com
13         
14     This program is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License vs published by
16     the Free Software Foundation; either version 2 of the License, or
17     (at your option) any later version.
18
19     This program is distributed in the hope that it will be useful,
20     but WITHOUT ANY WARRANTY; without even the implied warranty of
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22     GNU General Public License for more details.
23
24     You should have received a copy of the GNU General Public License
25     along with this program; if not, write to the Free Software
26     Foundation, Inc., 675 Mvss Ave, Cambridge, MA 02139, USA.
27  */
28
29 #include <linux/version.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/poll.h>
33 #include <linux/slab.h>
34 #include <linux/i2c.h>
35 #include <linux/init.h>
36 #include "tea6415c.h"
37
38 static int debug = 0;   /* insmod parameter */
39 MODULE_PARM(debug,"i");
40 #define dprintk if (debug) printk
41
42 #define TEA6415C_NUM_INPUTS     8
43 #define TEA6415C_NUM_OUTPUTS    6
44
45 /* addresses to scan, found only at 0x03 and/or 0x43 (7-bit) */
46 static unsigned short normal_i2c[] = {I2C_TEA6415C_1, I2C_TEA6415C_2, I2C_CLIENT_END};
47 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
48
49 /* magic definition of all other variables and things */
50 I2C_CLIENT_INSMOD;
51
52 static struct i2c_driver driver;
53
54 /* unique ID allocation */
55 static int tea6415c_id = 0;
56
57 /* this function is called by i2c_probe */
58 static int tea6415c_detect(struct i2c_adapter *adapter, int address, int kind)
59 {
60         struct  i2c_client *client = 0;
61         int err = 0;
62
63         /* let's see whether this adapter can support what we need */
64         if ( 0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) {
65                 return 0;
66         }
67
68         /* allocate memory for client structure */
69         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
70         if (0 == client) {
71                 return -ENOMEM;
72         }
73         memset(client, 0, sizeof(struct i2c_client));
74
75         /* fill client structure */
76         sprintf(client->name,"tea6415c (0x%02x)", address);
77         client->id = tea6415c_id++;
78         client->flags = 0;
79         client->addr = address;
80         client->adapter = adapter;
81         client->driver = &driver;
82
83         /* tell the i2c layer a new client has arrived */
84         if (0 != (err = i2c_attach_client(client))) {
85                 kfree(client);
86                 return err;
87         }
88
89         printk("tea6415c.o: detected @ 0x%02x on adapter %s\n",2*address,&client->adapter->name[0]);
90
91         return 0;
92 }
93
94 static int tea6415c_attach(struct i2c_adapter *adapter)
95 {
96         /* let's see whether this is a know adapter we can attach to */
97         if( adapter->id != I2C_ALGO_SAA7146 ) {
98                 dprintk("tea6415c.o: refusing to probe on unknown adapter [name='%s',id=0x%x]\n",adapter->name,adapter->id);
99                 return -ENODEV;
100         }
101
102         return i2c_probe(adapter,&addr_data,&tea6415c_detect);
103 }
104
105 static int tea6415c_detach(struct i2c_client *client)
106 {
107         int err = 0;
108
109         if ( 0 != (err = i2c_detach_client(client))) {
110                 printk("tea6415c.o: Client deregistration failed, client not detached.\n");
111                 return err;
112         }
113         
114         kfree(client);
115
116         return 0;
117 }
118
119 /* makes a connection between the input-pin 'i' and the output-pin 'o'
120    for the tea6415c-client 'client' */
121 static int tea6415c_switch(struct i2c_client *client, int i, int o)
122 {
123         u8      byte = 0;
124         
125         dprintk("tea6415c.o: tea6415c_switch: adr:0x%02x, i:%d, o:%d\n", client->addr, i, o);
126                 
127         /* check if the pins are valid */
128         if ( 0 == ((  1 == i ||  3 == i ||  5 == i ||  6 == i ||  8 == i || 10 == i || 20 == i || 11 == i ) &&
129                     (18 == o || 17 == o || 16 == o || 15 == o || 14 == o || 13 == o )))
130                 return -1;
131
132         /* to understand this, have a look at the tea6415c-specs (p.5) */
133         switch(o) {
134                 case 18:
135                         byte = 0x00;
136                         break;
137                 case 14:
138                         byte = 0x20;
139                         break;
140                 case 16:
141                         byte = 0x10;
142                         break;
143                 case 17:
144                         byte = 0x08;
145                         break;
146                 case 15:
147                         byte = 0x18;
148                         break;
149                 case 13:
150                         byte = 0x28;
151                         break;
152         };
153                 
154         switch(i) {
155                 case 5:
156                         byte |= 0x00;
157                         break;
158                 case 8:
159                         byte |= 0x04;
160                         break;
161                 case 3:
162                         byte |= 0x02;
163                         break;
164                 case 20:
165                         byte |= 0x06;
166                         break;
167                 case 6:
168                         byte |= 0x01;
169                         break;
170                 case 10:
171                         byte |= 0x05;
172                         break;
173                 case 1:
174                         byte |= 0x03;
175                         break;
176                 case 11:
177                         byte |= 0x07;
178                         break;
179         };
180
181         if ( 0 != i2c_smbus_write_byte(client,byte)) {
182                 dprintk("tea6415c.o: tea6415c_switch: could not write to tea6415c\n");
183                 return -1;
184         }
185
186         return 0;
187 }
188
189 static int tea6415c_command(struct i2c_client *client, unsigned int cmd, void* arg)
190 {
191         struct tea6415c_multiplex *v = (struct tea6415c_multiplex*)arg;
192         int result = 0;
193
194         switch (cmd) {
195                 case TEA6415C_SWITCH: {
196                         result = tea6415c_switch(client,v->in,v->out);
197                         break;
198                 }
199                 default: {
200                         return -ENOIOCTLCMD;
201                 }
202         }
203
204         if ( 0 != result )
205                 return result;
206         
207         return 0;
208 }
209
210 static struct i2c_driver driver = {
211         .owner          = THIS_MODULE,
212         .name           = "tea6415c driver",
213         .id             = I2C_DRIVERID_TEA6415C,
214         .flags          = I2C_DF_NOTIFY,
215         .attach_adapter = tea6415c_attach,
216         .detach_client  = tea6415c_detach,
217         .command        = tea6415c_command,
218 };
219
220 static int tea6415c_init_module(void)
221 {
222         i2c_add_driver(&driver);
223         return 0;
224 }
225
226 static void tea6415c_cleanup_module(void)
227 {
228         i2c_del_driver(&driver);
229 }
230
231 module_init(tea6415c_init_module);
232 module_exit(tea6415c_cleanup_module);
233
234 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
235 MODULE_DESCRIPTION("tea6415c driver");
236 MODULE_LICENSE("GPL");
237