2 tda9840.h - i2c-driver for the tda9840 by SGS Thomson
4 Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
6 The tda9840 is a stereo/dual sound processor with digital
7 identification. It can be found at address 0x84 on the i2c-bus.
9 For detailed informations download the specifications directly
10 from SGS Thomson at http://www.st.com
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.
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.
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.
27 #include <linux/version.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/poll.h>
31 #include <linux/slab.h>
32 #include <linux/i2c.h>
33 #include <linux/init.h>
37 static int debug = 0; /* insmod parameter */
38 MODULE_PARM(debug,"i");
39 #define dprintk if (debug) printk
42 #define LEVEL_ADJUST 0x02
43 #define STEREO_ADJUST 0x03
46 /* addresses to scan, found only at 0x42 (7-Bit) */
47 static unsigned short normal_i2c[] = {I2C_TDA9840, I2C_CLIENT_END};
48 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
50 /* magic definition of all other variables and things */
53 /* unique ID allocation */
54 static int tda9840_id = 0;
56 static struct i2c_driver driver;
58 static int tda9840_command(struct i2c_client *client, unsigned int cmd, void* arg)
65 int byte = *(int*)arg;
67 dprintk("tda9840.o: TDA9840_SWITCH: 0x%02x\n",byte);
69 if ( byte != TDA9840_SET_MONO
70 && byte != TDA9840_SET_MUTE
71 && byte != TDA9840_SET_STEREO
72 && byte != TDA9840_SET_LANG1
73 && byte != TDA9840_SET_LANG2
74 && byte != TDA9840_SET_BOTH
75 && byte != TDA9840_SET_BOTH_R
76 && byte != TDA9840_SET_EXTERNAL ) {
80 if ( 0 != (result = i2c_smbus_write_byte_data(client, SWITCH, byte))) {
81 printk("tda9840.o: TDA9840_SWITCH error.\n");
88 case TDA9840_LEVEL_ADJUST:
90 int byte = *(int*)arg;
92 dprintk("tda9840.o: TDA9840_LEVEL_ADJUST: %d\n",byte);
94 /* check for correct range */
95 if ( byte > 25 || byte < -20 )
98 /* calculate actual value to set, see specs, page 18 */
105 if ( 0 != (result = i2c_smbus_write_byte_data(client, LEVEL_ADJUST, byte))) {
106 printk("tda9840.o: TDA9840_LEVEL_ADJUST error.\n");
113 case TDA9840_STEREO_ADJUST:
115 int byte = *(int*)arg;
117 dprintk("tda9840.o: TDA9840_STEREO_ADJUST: %d\n",byte);
119 /* check for correct range */
120 if ( byte > 25 || byte < -24 )
123 /* calculate actual value to set */
130 if ( 0 != (result = i2c_smbus_write_byte_data(client, STEREO_ADJUST, byte))) {
131 printk("tda9840.o: TDA9840_STEREO_ADJUST error.\n");
142 if ( -1 == (byte = i2c_smbus_read_byte_data(client, STEREO_ADJUST))) {
143 printk("tda9840.o: TDA9840_DETECT error while reading.\n");
147 if( 0 != (byte & 0x80)) {
148 dprintk("tda9840.o: TDA9840_DETECT, register contents invalid.\n");
152 dprintk("tda9840.o: TDA9840_DETECT, result: 0x%02x (original byte)\n",byte);
154 return ((byte & 0x60) >> 5);
159 int byte = *(int*)arg;
161 dprintk("tda9840.o: TDA9840_TEST: 0x%02x\n",byte);
163 /* mask out irrelevant bits */
166 if ( 0 != (result = i2c_smbus_write_byte_data(client, TEST, byte))) {
167 printk("tda9840.o: TDA9840_TEST error.\n");
181 static int tda9840_detect(struct i2c_adapter *adapter, int address, int kind)
183 struct i2c_client *client;
188 /* let's see whether this adapter can support what we need */
189 if ( 0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA|I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
193 /* allocate memory for client structure */
194 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
196 printk("tda9840.o: not enough kernel memory.\n");
199 memset(client, 0, sizeof(struct i2c_client));
201 /* fill client structure */
202 sprintf(client->name,"tda9840 (0x%02x)", address);
203 client->id = tda9840_id++;
205 client->addr = address;
206 client->adapter = adapter;
207 client->driver = &driver;
208 i2c_set_clientdata(client, NULL);
210 /* tell the i2c layer a new client has arrived */
211 if (0 != (result = i2c_attach_client(client))) {
216 /* set initial values for level & stereo - adjustment, mode */
218 if ( 0 != (result = tda9840_command(client, TDA9840_LEVEL_ADJUST, &byte))) {
219 printk("tda9840.o: could not initialize ic #1. continuing anyway. (result:%d)\n",result);
222 if ( 0 != (result = tda9840_command(client, TDA9840_STEREO_ADJUST, &byte))) {
223 printk("tda9840.o: could not initialize ic #2. continuing anyway. (result:%d)\n",result);
226 byte = TDA9840_SET_MONO;
227 if ( 0 != (result = tda9840_command(client, TDA9840_SWITCH, &byte))) {
228 printk("tda9840.o: could not initialize ic #3. continuing anyway. (result:%d)\n",result);
231 printk("tda9840.o: detected @ 0x%02x on adapter %s\n",2*address,&client->adapter->name[0]);
236 static int tda9840_attach(struct i2c_adapter *adapter)
238 /* let's see whether this is a know adapter we can attach to */
239 if( adapter->id != I2C_ALGO_SAA7146 ) {
240 dprintk("tda9840.o: refusing to probe on unknown adapter [name='%s',id=0x%x]\n",adapter->name,adapter->id);
244 return i2c_probe(adapter,&addr_data,&tda9840_detect);
247 static int tda9840_detach(struct i2c_client *client)
251 if ( 0 != (err = i2c_detach_client(client))) {
252 printk("tda9840.o: Client deregistration failed, client not detached.\n");
261 static struct i2c_driver driver = {
262 .owner = THIS_MODULE,
263 .name = "tda9840 driver",
264 .id = I2C_DRIVERID_TDA9840,
265 .flags = I2C_DF_NOTIFY,
266 .attach_adapter = tda9840_attach,
267 .detach_client = tda9840_detach,
268 .command = tda9840_command,
271 static int __init tda9840_init_module(void)
273 return i2c_add_driver(&driver);
276 static void __exit tda9840_cleanup_module(void)
278 i2c_del_driver(&driver);
281 module_init(tda9840_init_module);
282 module_exit(tda9840_cleanup_module);
284 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
285 MODULE_DESCRIPTION("tda9840 driver");
286 MODULE_LICENSE("GPL");