Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
425 | giacomo | 1 | /* |
2 | tea6420.o - i2c-driver for the tea6420 by SGS Thomson |
||
3 | |||
4 | Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de> |
||
5 | |||
6 | The tea6420 is a bus controlled audio-matrix with 5 stereo inputs, |
||
7 | 4 stereo outputs and gain control for each output. |
||
8 | It is cascadable, i.e. it can be found at the adresses 0x98 |
||
9 | and 0x9a 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 as 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 Mass 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 | |||
37 | #include "tea6420.h" |
||
38 | |||
39 | static int debug = 0; /* insmod parameter */ |
||
40 | MODULE_PARM(debug,"i"); |
||
41 | #define dprintk if (debug) printk |
||
42 | |||
43 | /* addresses to scan, found only at 0x4c and/or 0x4d (7-Bit) */ |
||
44 | static unsigned short normal_i2c[] = {I2C_TEA6420_1, I2C_TEA6420_2, I2C_CLIENT_END}; |
||
45 | static unsigned short normal_i2c_range[] = {I2C_CLIENT_END}; |
||
46 | |||
47 | /* magic definition of all other variables and things */ |
||
48 | I2C_CLIENT_INSMOD; |
||
49 | |||
50 | static struct i2c_driver driver; |
||
51 | |||
52 | /* unique ID allocation */ |
||
53 | static int tea6420_id = 0; |
||
54 | |||
55 | /* make a connection between the input 'i' and the output 'o' |
||
56 | with gain 'g' for the tea6420-client 'client' (note: i = 6 means 'mute') */ |
||
57 | static int tea6420_switch(struct i2c_client *client, int i, int o, int g) |
||
58 | { |
||
59 | u8 byte = 0; |
||
60 | |||
61 | int result = 0; |
||
62 | |||
63 | dprintk("tea6420.o: tea6420_switch: adr:0x%02x, i:%d, o:%d, g:%d\n",client->addr,i,o,g); |
||
64 | |||
65 | /* check if the paramters are valid */ |
||
66 | if ( i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g%2 != 0 ) |
||
67 | return -1; |
||
68 | |||
69 | byte = ((o-1)<<5); |
||
70 | byte |= (i-1); |
||
71 | |||
72 | /* to understand this, have a look at the tea6420-specs (p.5) */ |
||
73 | switch(g) { |
||
74 | case 0: |
||
75 | byte |= (3<<3); |
||
76 | break; |
||
77 | case 2: |
||
78 | byte |= (2<<3); |
||
79 | break; |
||
80 | case 4: |
||
81 | byte |= (1<<3); |
||
82 | break; |
||
83 | case 6: |
||
84 | break; |
||
85 | } |
||
86 | |||
87 | /* fixme?: 1 != ... => 0 != */ |
||
88 | if ( 0 != (result = i2c_smbus_write_byte(client,byte))) { |
||
89 | printk("tea6402:%d\n",result); |
||
90 | dprintk(KERN_ERR "tea6420.o: could not switch, result:%d\n",result); |
||
91 | return -EFAULT; |
||
92 | } |
||
93 | |||
94 | return 0; |
||
95 | } |
||
96 | |||
97 | /* this function is called by i2c_probe */ |
||
98 | static int tea6420_detect(struct i2c_adapter *adapter, int address, unsigned short flags, int kind) |
||
99 | { |
||
100 | struct i2c_client *client; |
||
101 | int err = 0, i = 0; |
||
102 | |||
103 | /* let's see whether this adapter can support what we need */ |
||
104 | if ( 0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) { |
||
105 | return 0; |
||
106 | } |
||
107 | |||
108 | /* allocate memory for client structure */ |
||
109 | client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); |
||
110 | if (0 == client) { |
||
111 | return -ENOMEM; |
||
112 | } |
||
113 | memset(client, 0, sizeof(struct i2c_client)); |
||
114 | |||
115 | /* fill client structure */ |
||
116 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)) |
||
117 | sprintf(client->name,"tea6420 (0x%02x)", address); |
||
118 | #else |
||
119 | sprintf(client->dev.name,"tea6420 (0x%02x)", address); |
||
120 | #endif |
||
121 | client->id = tea6420_id++; |
||
122 | client->flags = 0; |
||
123 | client->addr = address; |
||
124 | client->adapter = adapter; |
||
125 | client->driver = &driver; |
||
126 | |||
127 | /* tell the i2c layer a new client has arrived */ |
||
128 | if (0 != (err = i2c_attach_client(client))) { |
||
129 | kfree(client); |
||
130 | return err; |
||
131 | } |
||
132 | |||
133 | /* set initial values: set "mute"-input to all outputs at gain 0 */ |
||
134 | err = 0; |
||
135 | for(i = 1; i < 5; i++) { |
||
136 | err += tea6420_switch(client, 6, i, 0); |
||
137 | } |
||
138 | if( 0 != err) { |
||
139 | printk("tea6420.o: could not initialize chipset. continuing anyway.\n"); |
||
140 | } |
||
141 | |||
142 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)) |
||
143 | printk("tea6420.o: detected @ 0x%02x on adapter %s\n",address,&client->adapter->name[0]); |
||
144 | #else |
||
145 | printk("tea6420.o: detected @ 0x%02x on adapter %s\n",address,&client->adapter->dev.name[0]); |
||
146 | #endif |
||
147 | return 0; |
||
148 | } |
||
149 | |||
150 | static int tea6420_attach(struct i2c_adapter *adapter) |
||
151 | { |
||
152 | /* let's see whether this is a know adapter we can attach to */ |
||
153 | if( adapter->id != I2C_ALGO_SAA7146 ) { |
||
154 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)) |
||
155 | dprintk("tea6420.o: refusing to probe on unknown adapter [name='%s',id=0x%x]\n",adapter->name,adapter->id); |
||
156 | #else |
||
157 | dprintk("tea6420.o: refusing to probe on unknown adapter [name='%s',id=0x%x]\n",adapter->dev.name,adapter->id); |
||
158 | #endif |
||
159 | return -ENODEV; |
||
160 | } |
||
161 | |||
162 | return i2c_probe(adapter,&addr_data,&tea6420_detect); |
||
163 | } |
||
164 | |||
165 | static int tea6420_detach(struct i2c_client *client) |
||
166 | { |
||
167 | int err = 0; |
||
168 | |||
169 | if ( 0 != (err = i2c_detach_client(client))) { |
||
170 | printk("tea6420.o: Client deregistration failed, client not detached.\n"); |
||
171 | return err; |
||
172 | } |
||
173 | |||
174 | kfree(client); |
||
175 | |||
176 | return 0; |
||
177 | } |
||
178 | |||
179 | static int tea6420_command(struct i2c_client *client, unsigned int cmd, void* arg) |
||
180 | { |
||
181 | struct tea6420_multiplex *a = (struct tea6420_multiplex*)arg; |
||
182 | int result = 0; |
||
183 | |||
184 | switch (cmd) { |
||
185 | case TEA6420_SWITCH: { |
||
186 | result = tea6420_switch(client,a->in,a->out,a->gain); |
||
187 | break; |
||
188 | } |
||
189 | default: { |
||
190 | return -ENOIOCTLCMD; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | if ( 0 != result ) |
||
195 | return result; |
||
196 | |||
197 | return 0; |
||
198 | } |
||
199 | static void tea6420_inc_use(struct i2c_client *client) |
||
200 | { |
||
201 | #ifdef MODULE |
||
202 | MOD_INC_USE_COUNT; |
||
203 | #endif |
||
204 | } |
||
205 | |||
206 | static void tea6420_dec_use(struct i2c_client *client) |
||
207 | { |
||
208 | #ifdef MODULE |
||
209 | MOD_DEC_USE_COUNT; |
||
210 | #endif |
||
211 | } |
||
212 | |||
213 | static struct i2c_driver driver = { |
||
214 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,54) |
||
215 | .owner = THIS_MODULE, |
||
216 | #endif |
||
217 | .name = "tea6420 driver", |
||
218 | .id = I2C_DRIVERID_TEA6420, |
||
219 | .flags = I2C_DF_NOTIFY, |
||
220 | .attach_adapter = tea6420_attach, |
||
221 | .detach_client = tea6420_detach, |
||
222 | .command = tea6420_command, |
||
223 | .inc_use = tea6420_inc_use, |
||
224 | .dec_use = tea6420_dec_use, |
||
225 | }; |
||
226 | |||
227 | static int tea6420_init_module(void) |
||
228 | { |
||
229 | i2c_add_driver(&driver); |
||
230 | return 0; |
||
231 | } |
||
232 | |||
233 | static void tea6420_cleanup_module(void) |
||
234 | { |
||
235 | i2c_del_driver(&driver); |
||
236 | } |
||
237 | |||
238 | module_init(tea6420_init_module); |
||
239 | module_exit(tea6420_cleanup_module); |
||
240 | |||
241 | MODULE_AUTHOR("Michael Hunold <michael@mihu.de>"); |
||
242 | MODULE_DESCRIPTION("tea6420 driver"); |
||
243 | MODULE_LICENSE("GPL"); |