Subversion Repositories shark

Rev

Rev 425 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
425 giacomo 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
    0x43 and 0x03 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, unsigned short flags, 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
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
77
        sprintf(client->name,"tea6415c (0x%02x)", address);
78
#else
79
        sprintf(client->dev.name,"tea6415c (0x%02x)", address);
80
#endif
81
        client->id = tea6415c_id++;
82
        client->flags = 0;
83
        client->addr = address;
84
        client->adapter = adapter;
85
        client->driver = &driver;
86
 
87
        /* tell the i2c layer a new client has arrived */
88
        if (0 != (err = i2c_attach_client(client))) {
89
                kfree(client);
90
                return err;
91
        }
92
 
93
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
94
        printk("tea6415c.o: detected @ 0x%02x on adapter %s\n",address,&client->adapter->name[0]);
95
#else
96
        printk("tea6415c.o: detected @ 0x%02x on adapter %s\n",address,&client->adapter->dev.name[0]);
97
#endif
98
 
99
        return 0;
100
}
101
 
102
static int tea6415c_attach(struct i2c_adapter *adapter)
103
{
104
        /* let's see whether this is a know adapter we can attach to */
105
        if( adapter->id != I2C_ALGO_SAA7146 ) {
106
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
107
                dprintk("tea6415c.o: refusing to probe on unknown adapter [name='%s',id=0x%x]\n",adapter->name,adapter->id);
108
#else
109
                dprintk("tea6415c.o: refusing to probe on unknown adapter [name='%s',id=0x%x]\n",adapter->dev.name,adapter->id);
110
#endif
111
                return -ENODEV;
112
        }
113
 
114
        return i2c_probe(adapter,&addr_data,&tea6415c_detect);
115
}
116
 
117
static int tea6415c_detach(struct i2c_client *client)
118
{
119
        int err = 0;
120
 
121
        if ( 0 != (err = i2c_detach_client(client))) {
122
                printk("tea6415c.o: Client deregistration failed, client not detached.\n");
123
                return err;
124
        }
125
 
126
        kfree(client);
127
 
128
        return 0;
129
}
130
 
131
/* makes a connection between the input-pin 'i' and the output-pin 'o'
132
   for the tea6415c-client 'client' */
133
static int tea6415c_switch(struct i2c_client *client, int i, int o)
134
{
135
        u8      byte = 0;
136
 
137
        dprintk("tea6415c.o: tea6415c_switch: adr:0x%02x, i:%d, o:%d\n", client->addr, i, o);
138
 
139
        /* check if the pins are valid */
140
        if ( 0 == ((  1 == i ||  3 == i ||  5 == i ||  6 == i ||  8 == i || 10 == i || 20 == i || 11 == i ) &&
141
                    (18 == o || 17 == o || 16 == o || 15 == o || 14 == o || 13 == o )))
142
                return -1;
143
 
144
        /* to understand this, have a look at the tea6415c-specs (p.5) */
145
        switch(o) {
146
                case 18:
147
                        byte = 0x00;
148
                        break;
149
                case 14:
150
                        byte = 0x20;
151
                        break;
152
                case 16:
153
                        byte = 0x10;
154
                        break;
155
                case 17:
156
                        byte = 0x08;
157
                        break;
158
                case 15:
159
                        byte = 0x18;
160
                        break;
161
                case 13:
162
                        byte = 0x28;
163
                        break;
164
        };
165
 
166
        switch(i) {
167
                case 5:
168
                        byte |= 0x00;
169
                        break;
170
                case 8:
171
                        byte |= 0x04;
172
                        break;
173
                case 3:
174
                        byte |= 0x02;
175
                        break;
176
                case 20:
177
                        byte |= 0x06;
178
                        break;
179
                case 6:
180
                        byte |= 0x01;
181
                        break;
182
                case 10:
183
                        byte |= 0x05;
184
                        break;
185
                case 1:
186
                        byte |= 0x03;
187
                        break;
188
                case 11:
189
                        byte |= 0x07;
190
                        break;
191
        };
192
 
193
        if ( 0 != i2c_smbus_write_byte(client,byte)) {
194
                dprintk("tea6415c.o: tea6415c_switch: could not write to tea6415c\n");
195
                return -1;
196
        }
197
 
198
        return 0;
199
}
200
 
201
static int tea6415c_command(struct i2c_client *client, unsigned int cmd, void* arg)
202
{
203
        struct tea6415c_multiplex *v = (struct tea6415c_multiplex*)arg;
204
        int result = 0;
205
 
206
        switch (cmd) {
207
                case TEA6415C_SWITCH: {
208
                        result = tea6415c_switch(client,v->in,v->out);
209
                        break;
210
                }
211
                default: {
212
                        return -ENOIOCTLCMD;
213
                }
214
        }
215
 
216
        if ( 0 != result )
217
                return result;
218
 
219
        return 0;
220
}
221
static void tea6415c_inc_use(struct i2c_client *client)
222
{
223
#ifdef MODULE
224
        MOD_INC_USE_COUNT;
225
#endif
226
}
227
 
228
static void tea6415c_dec_use(struct i2c_client *client)
229
{
230
#ifdef MODULE
231
        MOD_DEC_USE_COUNT;
232
#endif
233
}
234
 
235
static struct i2c_driver driver = {
236
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,54)
237
        .owner          = THIS_MODULE,
238
#endif
239
        .name           = "tea6415c driver",
240
        .id             = I2C_DRIVERID_TEA6415C,
241
        .flags          = I2C_DF_NOTIFY,
242
        .attach_adapter = tea6415c_attach,
243
        .detach_client  = tea6415c_detach,
244
        .command        = tea6415c_command,
245
        .inc_use        = tea6415c_inc_use,
246
        .dec_use        = tea6415c_dec_use,
247
};
248
 
249
static int tea6415c_init_module(void)
250
{
251
        i2c_add_driver(&driver);
252
        return 0;
253
}
254
 
255
static void tea6415c_cleanup_module(void)
256
{
257
        i2c_del_driver(&driver);
258
}
259
 
260
module_init(tea6415c_init_module);
261
module_exit(tea6415c_cleanup_module);
262
 
263
MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
264
MODULE_DESCRIPTION("tea6415c driver");
265
MODULE_LICENSE("GPL");
266