]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | tda9840 - i2c-driver for the tda9840 by SGS Thomson | |
3 | ||
4 | Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de> | |
5 | ||
6 | The tda9840 is a stereo/dual sound processor with digital | |
7 | identification. It can be found at address 0x84 on the i2c-bus. | |
8 | ||
9 | For detailed informations download the specifications directly | |
10 | from SGS Thomson at http://www.st.com | |
11 | ||
12 | This program is free software; you can redistribute it and/or modify | |
13 | it under the terms of the GNU General Public License as published by | |
14 | the Free Software Foundation; either version 2 of the License, or | |
15 | (at your option) any later version. | |
16 | ||
17 | This program is distributed in the hope that it will be useful, | |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | GNU General Public License for more details. | |
21 | ||
22 | You should have received a copy of the GNU General Public License | |
23 | along with this program; if not, write to the Free Software | |
24 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | */ | |
26 | ||
27 | #include <linux/module.h> | |
28 | #include <linux/ioctl.h> | |
29 | #include <linux/i2c.h> | |
30 | ||
31 | #include "tda9840.h" | |
32 | ||
33 | static int debug = 0; /* insmod parameter */ | |
34 | module_param(debug, int, 0644); | |
35 | MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off)."); | |
36 | #define dprintk(args...) \ | |
367cb704 | 37 | do { if (debug) { printk("%s: %s()[%d]: ", KBUILD_MODNAME, __FUNCTION__, __LINE__); printk(args); } } while (0) |
1da177e4 LT |
38 | |
39 | #define SWITCH 0x00 | |
40 | #define LEVEL_ADJUST 0x02 | |
41 | #define STEREO_ADJUST 0x03 | |
42 | #define TEST 0x04 | |
43 | ||
44 | /* addresses to scan, found only at 0x42 (7-Bit) */ | |
45 | static unsigned short normal_i2c[] = { I2C_TDA9840, 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 | static int command(struct i2c_client *client, unsigned int cmd, void *arg) | |
54 | { | |
55 | int result; | |
56 | int byte = *(int *)arg; | |
57 | ||
58 | switch (cmd) { | |
59 | case TDA9840_SWITCH: | |
60 | ||
61 | dprintk("TDA9840_SWITCH: 0x%02x\n", byte); | |
62 | ||
63 | if (byte != TDA9840_SET_MONO | |
64 | && byte != TDA9840_SET_MUTE | |
65 | && byte != TDA9840_SET_STEREO | |
66 | && byte != TDA9840_SET_LANG1 | |
67 | && byte != TDA9840_SET_LANG2 | |
68 | && byte != TDA9840_SET_BOTH | |
69 | && byte != TDA9840_SET_BOTH_R | |
70 | && byte != TDA9840_SET_EXTERNAL) { | |
71 | return -EINVAL; | |
72 | } | |
73 | ||
74 | result = i2c_smbus_write_byte_data(client, SWITCH, byte); | |
75 | if (result) | |
76 | dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result); | |
77 | break; | |
78 | ||
79 | case TDA9840_LEVEL_ADJUST: | |
80 | ||
81 | dprintk("TDA9840_LEVEL_ADJUST: %d\n", byte); | |
82 | ||
83 | /* check for correct range */ | |
84 | if (byte > 25 || byte < -20) | |
85 | return -EINVAL; | |
86 | ||
87 | /* calculate actual value to set, see specs, page 18 */ | |
88 | byte /= 5; | |
89 | if (0 < byte) | |
90 | byte += 0x8; | |
91 | else | |
92 | byte = -byte; | |
93 | ||
94 | result = i2c_smbus_write_byte_data(client, LEVEL_ADJUST, byte); | |
95 | if (result) | |
96 | dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result); | |
97 | break; | |
98 | ||
99 | case TDA9840_STEREO_ADJUST: | |
100 | ||
101 | dprintk("TDA9840_STEREO_ADJUST: %d\n", byte); | |
102 | ||
103 | /* check for correct range */ | |
104 | if (byte > 25 || byte < -24) | |
105 | return -EINVAL; | |
106 | ||
107 | /* calculate actual value to set */ | |
108 | byte /= 5; | |
109 | if (0 < byte) | |
110 | byte += 0x20; | |
111 | else | |
112 | byte = -byte; | |
113 | ||
114 | result = i2c_smbus_write_byte_data(client, STEREO_ADJUST, byte); | |
115 | if (result) | |
116 | dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result); | |
117 | break; | |
118 | ||
119 | case TDA9840_DETECT: { | |
120 | int *ret = (int *)arg; | |
121 | ||
122 | byte = i2c_smbus_read_byte_data(client, STEREO_ADJUST); | |
123 | if (byte == -1) { | |
124 | dprintk("i2c_smbus_read_byte_data() failed\n"); | |
125 | return -EIO; | |
126 | } | |
127 | ||
128 | if (0 != (byte & 0x80)) { | |
129 | dprintk("TDA9840_DETECT: register contents invalid\n"); | |
130 | return -EINVAL; | |
131 | } | |
132 | ||
133 | dprintk("TDA9840_DETECT: byte: 0x%02x\n", byte); | |
134 | *ret = ((byte & 0x60) >> 5); | |
135 | result = 0; | |
136 | break; | |
137 | } | |
138 | case TDA9840_TEST: | |
139 | dprintk("TDA9840_TEST: 0x%02x\n", byte); | |
140 | ||
141 | /* mask out irrelevant bits */ | |
142 | byte &= 0x3; | |
143 | ||
144 | result = i2c_smbus_write_byte_data(client, TEST, byte); | |
145 | if (result) | |
146 | dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result); | |
147 | break; | |
148 | default: | |
149 | return -ENOIOCTLCMD; | |
150 | } | |
151 | ||
152 | if (result) | |
153 | return -EIO; | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
158 | static int detect(struct i2c_adapter *adapter, int address, int kind) | |
159 | { | |
160 | struct i2c_client *client; | |
161 | int result = 0; | |
162 | ||
163 | int byte = 0x0; | |
164 | ||
165 | /* let's see whether this adapter can support what we need */ | |
166 | if (0 == i2c_check_functionality(adapter, | |
167 | I2C_FUNC_SMBUS_READ_BYTE_DATA | | |
168 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) { | |
169 | return 0; | |
170 | } | |
171 | ||
172 | /* allocate memory for client structure */ | |
173 | client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); | |
174 | if (0 == client) { | |
175 | printk("not enough kernel memory\n"); | |
176 | return -ENOMEM; | |
177 | } | |
178 | ||
179 | /* fill client structure */ | |
180 | memcpy(client, &client_template, sizeof(struct i2c_client)); | |
181 | client->addr = address; | |
182 | client->adapter = adapter; | |
183 | ||
184 | /* tell the i2c layer a new client has arrived */ | |
185 | if (0 != (result = i2c_attach_client(client))) { | |
186 | kfree(client); | |
187 | return result; | |
188 | } | |
189 | ||
190 | /* set initial values for level & stereo - adjustment, mode */ | |
191 | byte = 0; | |
192 | result = command(client, TDA9840_LEVEL_ADJUST, &byte); | |
193 | result += command(client, TDA9840_STEREO_ADJUST, &byte); | |
194 | byte = TDA9840_SET_MONO; | |
195 | result = command(client, TDA9840_SWITCH, &byte); | |
196 | if (result) { | |
197 | dprintk("could not initialize tda9840\n"); | |
198 | return -ENODEV; | |
199 | } | |
200 | ||
201 | printk("tda9840: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]); | |
202 | return 0; | |
203 | } | |
204 | ||
205 | static int attach(struct i2c_adapter *adapter) | |
206 | { | |
207 | /* let's see whether this is a know adapter we can attach to */ | |
1684a984 | 208 | if (adapter->id != I2C_HW_SAA7146) { |
1da177e4 LT |
209 | dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id); |
210 | return -ENODEV; | |
211 | } | |
212 | ||
213 | return i2c_probe(adapter, &addr_data, &detect); | |
214 | } | |
215 | ||
216 | static int detach(struct i2c_client *client) | |
217 | { | |
218 | int ret = i2c_detach_client(client); | |
219 | kfree(client); | |
220 | return ret; | |
221 | } | |
222 | ||
223 | static struct i2c_driver driver = { | |
604f28e2 | 224 | .driver = { |
604f28e2 LR |
225 | .name = "tda9840", |
226 | }, | |
1da177e4 | 227 | .id = I2C_DRIVERID_TDA9840, |
1da177e4 LT |
228 | .attach_adapter = attach, |
229 | .detach_client = detach, | |
230 | .command = command, | |
231 | }; | |
232 | ||
233 | static struct i2c_client client_template = { | |
fae91e72 | 234 | .name = "tda9840", |
1da177e4 LT |
235 | .driver = &driver, |
236 | }; | |
237 | ||
238 | static int __init this_module_init(void) | |
239 | { | |
240 | return i2c_add_driver(&driver); | |
241 | } | |
242 | ||
243 | static void __exit this_module_exit(void) | |
244 | { | |
245 | i2c_del_driver(&driver); | |
246 | } | |
247 | ||
248 | module_init(this_module_init); | |
249 | module_exit(this_module_exit); | |
250 | ||
251 | MODULE_AUTHOR("Michael Hunold <michael@mihu.de>"); | |
252 | MODULE_DESCRIPTION("tda9840 driver"); | |
253 | MODULE_LICENSE("GPL"); |