Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | tea6415c - 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/module.h> | |
30 | #include <linux/ioctl.h> | |
31 | #include <linux/i2c.h> | |
32 | ||
33 | #include "tea6415c.h" | |
34 | ||
35 | static int debug = 0; /* insmod parameter */ | |
36 | module_param(debug, int, 0644); | |
37 | MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off)."); | |
38 | #define dprintk(args...) \ | |
39 | do { if (debug) { printk("%s: %s()[%d]: ",__stringify(KBUILD_MODNAME), __FUNCTION__, __LINE__); printk(args); } } while (0) | |
40 | ||
41 | #define TEA6415C_NUM_INPUTS 8 | |
42 | #define TEA6415C_NUM_OUTPUTS 6 | |
43 | ||
44 | /* addresses to scan, found only at 0x03 and/or 0x43 (7-bit) */ | |
45 | static unsigned short normal_i2c[] = { I2C_TEA6415C_1, I2C_TEA6415C_2, I2C_CLIENT_END }; | |
1da177e4 LT |
46 | |
47 | /* magic definition of all other variables and things */ | |
48 | I2C_CLIENT_INSMOD; | |
49 | ||
50 | static struct i2c_driver driver; | |
51 | static struct i2c_client client_template; | |
52 | ||
53 | /* this function is called by i2c_probe */ | |
54 | static int detect(struct i2c_adapter *adapter, int address, int kind) | |
55 | { | |
56 | struct i2c_client *client = NULL; | |
57 | int err = 0; | |
58 | ||
59 | /* let's see whether this adapter can support what we need */ | |
60 | if (0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) { | |
61 | return 0; | |
62 | } | |
63 | ||
64 | /* allocate memory for client structure */ | |
65 | client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); | |
66 | if (0 == client) { | |
67 | return -ENOMEM; | |
68 | } | |
69 | ||
70 | /* fill client structure */ | |
71 | memcpy(client, &client_template, sizeof(struct i2c_client)); | |
72 | client->addr = address; | |
73 | client->adapter = adapter; | |
74 | ||
75 | /* tell the i2c layer a new client has arrived */ | |
76 | if (0 != (err = i2c_attach_client(client))) { | |
77 | kfree(client); | |
78 | return err; | |
79 | } | |
80 | ||
81 | printk("tea6415c: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]); | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
86 | static int attach(struct i2c_adapter *adapter) | |
87 | { | |
88 | /* let's see whether this is a know adapter we can attach to */ | |
1684a984 | 89 | if (adapter->id != I2C_HW_SAA7146) { |
1da177e4 LT |
90 | dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id); |
91 | return -ENODEV; | |
92 | } | |
93 | ||
94 | return i2c_probe(adapter, &addr_data, &detect); | |
95 | } | |
96 | ||
97 | static int detach(struct i2c_client *client) | |
98 | { | |
99 | int ret = i2c_detach_client(client); | |
100 | kfree(client); | |
101 | return ret; | |
102 | } | |
103 | ||
104 | /* makes a connection between the input-pin 'i' and the output-pin 'o' | |
105 | for the tea6415c-client 'client' */ | |
106 | static int switch_matrix(struct i2c_client *client, int i, int o) | |
107 | { | |
108 | u8 byte = 0; | |
109 | int ret; | |
110 | ||
111 | dprintk("adr:0x%02x, i:%d, o:%d\n", client->addr, i, o); | |
112 | ||
113 | /* check if the pins are valid */ | |
114 | if (0 == ((1 == i || 3 == i || 5 == i || 6 == i || 8 == i || 10 == i || 20 == i || 11 == i) | |
115 | && (18 == o || 17 == o || 16 == o || 15 == o || 14 == o || 13 == o))) | |
116 | return -1; | |
117 | ||
118 | /* to understand this, have a look at the tea6415c-specs (p.5) */ | |
119 | switch (o) { | |
120 | case 18: | |
121 | byte = 0x00; | |
122 | break; | |
123 | case 14: | |
124 | byte = 0x20; | |
125 | break; | |
126 | case 16: | |
127 | byte = 0x10; | |
128 | break; | |
129 | case 17: | |
130 | byte = 0x08; | |
131 | break; | |
132 | case 15: | |
133 | byte = 0x18; | |
134 | break; | |
135 | case 13: | |
136 | byte = 0x28; | |
137 | break; | |
138 | }; | |
139 | ||
140 | switch (i) { | |
141 | case 5: | |
142 | byte |= 0x00; | |
143 | break; | |
144 | case 8: | |
145 | byte |= 0x04; | |
146 | break; | |
147 | case 3: | |
148 | byte |= 0x02; | |
149 | break; | |
150 | case 20: | |
151 | byte |= 0x06; | |
152 | break; | |
153 | case 6: | |
154 | byte |= 0x01; | |
155 | break; | |
156 | case 10: | |
157 | byte |= 0x05; | |
158 | break; | |
159 | case 1: | |
160 | byte |= 0x03; | |
161 | break; | |
162 | case 11: | |
163 | byte |= 0x07; | |
164 | break; | |
165 | }; | |
166 | ||
167 | ret = i2c_smbus_write_byte(client, byte); | |
168 | if (ret) { | |
169 | dprintk("i2c_smbus_write_byte() failed, ret:%d\n", ret); | |
170 | return -EIO; | |
171 | } | |
172 | ||
173 | return ret; | |
174 | } | |
175 | ||
176 | static int command(struct i2c_client *client, unsigned int cmd, void *arg) | |
177 | { | |
178 | struct tea6415c_multiplex *v = (struct tea6415c_multiplex *)arg; | |
179 | int result = 0; | |
180 | ||
181 | switch (cmd) { | |
182 | case TEA6415C_SWITCH: | |
183 | result = switch_matrix(client, v->in, v->out); | |
184 | break; | |
185 | default: | |
186 | return -ENOIOCTLCMD; | |
187 | } | |
188 | ||
189 | return result; | |
190 | } | |
191 | ||
192 | static struct i2c_driver driver = { | |
604f28e2 LR |
193 | .driver = { |
194 | .owner = THIS_MODULE, | |
195 | .name = "tea6415c", | |
196 | }, | |
1da177e4 | 197 | .id = I2C_DRIVERID_TEA6415C, |
1da177e4 LT |
198 | .attach_adapter = attach, |
199 | .detach_client = detach, | |
200 | .command = command, | |
201 | }; | |
202 | ||
203 | static struct i2c_client client_template = { | |
fae91e72 | 204 | .name = "tea6415c", |
1da177e4 LT |
205 | .driver = &driver, |
206 | }; | |
207 | ||
208 | static int __init this_module_init(void) | |
209 | { | |
210 | return i2c_add_driver(&driver); | |
211 | } | |
212 | ||
213 | static void __exit this_module_exit(void) | |
214 | { | |
215 | i2c_del_driver(&driver); | |
216 | } | |
217 | ||
218 | module_init(this_module_init); | |
219 | module_exit(this_module_exit); | |
220 | ||
221 | MODULE_AUTHOR("Michael Hunold <michael@mihu.de>"); | |
222 | MODULE_DESCRIPTION("tea6415c driver"); | |
223 | MODULE_LICENSE("GPL"); |