vserver 1.9.5.x5
[linux-2.6.git] / drivers / media / video / tda9840.c
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...) \
37             do { if (debug) { printk("%s: %s()[%d]: ",__stringify(KBUILD_MODNAME), __FUNCTION__, __LINE__); printk(args); } } while (0)
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 };
46 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
47
48 /* magic definition of all other variables and things */
49 I2C_CLIENT_INSMOD;
50
51 static struct i2c_driver driver;
52 static struct i2c_client client_template;
53
54 /* unique ID allocation */
55 static int tda9840_id = 0;
56
57 static int command(struct i2c_client *client, unsigned int cmd, void *arg)
58 {
59         int result;
60         int byte = *(int *)arg;
61
62         switch (cmd) {
63         case TDA9840_SWITCH:
64
65                 dprintk("TDA9840_SWITCH: 0x%02x\n", byte);
66
67                 if (byte != TDA9840_SET_MONO
68                     && byte != TDA9840_SET_MUTE
69                     && byte != TDA9840_SET_STEREO
70                     && byte != TDA9840_SET_LANG1
71                     && byte != TDA9840_SET_LANG2
72                     && byte != TDA9840_SET_BOTH
73                     && byte != TDA9840_SET_BOTH_R
74                     && byte != TDA9840_SET_EXTERNAL) {
75                         return -EINVAL;
76                 }
77
78                 result = i2c_smbus_write_byte_data(client, SWITCH, byte);
79                 if (result)
80                         dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
81                 break;
82
83         case TDA9840_LEVEL_ADJUST:
84
85                 dprintk("TDA9840_LEVEL_ADJUST: %d\n", byte);
86
87                 /* check for correct range */
88                 if (byte > 25 || byte < -20)
89                         return -EINVAL;
90
91                 /* calculate actual value to set, see specs, page 18 */
92                 byte /= 5;
93                 if (0 < byte)
94                         byte += 0x8;
95                 else
96                         byte = -byte;
97
98                 result = i2c_smbus_write_byte_data(client, LEVEL_ADJUST, byte);
99                 if (result)
100                         dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
101                 break;
102
103         case TDA9840_STEREO_ADJUST:
104
105                 dprintk("TDA9840_STEREO_ADJUST: %d\n", byte);
106
107                 /* check for correct range */
108                 if (byte > 25 || byte < -24)
109                         return -EINVAL;
110
111                 /* calculate actual value to set */
112                 byte /= 5;
113                 if (0 < byte)
114                         byte += 0x20;
115                 else
116                         byte = -byte;
117
118                 result = i2c_smbus_write_byte_data(client, STEREO_ADJUST, byte);
119                 if (result)
120                         dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
121                 break;
122
123         case TDA9840_DETECT:
124
125                 byte = i2c_smbus_read_byte_data(client, STEREO_ADJUST);
126                 if (byte == -1) {
127                         dprintk("i2c_smbus_read_byte_data() failed\n");
128                         return -EIO;
129                 }
130
131                 if (0 != (byte & 0x80)) {
132                         dprintk("TDA9840_DETECT: register contents invalid\n");
133                         return -EINVAL;
134                 }
135
136                 dprintk("TDA9840_DETECT: byte: 0x%02x\n", byte);
137                 return ((byte & 0x60) >> 5);
138
139         case TDA9840_TEST:
140                 dprintk("TDA9840_TEST: 0x%02x\n", byte);
141
142                 /* mask out irrelevant bits */
143                 byte &= 0x3;
144
145                 result = i2c_smbus_write_byte_data(client, TEST, byte);
146                 if (result)
147                         dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
148                 break;
149         default:
150                 return -ENOIOCTLCMD;
151         }
152
153         if (result)
154                 return -EIO;
155
156         return 0;
157 }
158
159 static int detect(struct i2c_adapter *adapter, int address, int kind)
160 {
161         struct i2c_client *client;
162         int result = 0;
163
164         int byte = 0x0;
165
166         /* let's see whether this adapter can support what we need */
167         if (0 == i2c_check_functionality(adapter,
168                                     I2C_FUNC_SMBUS_READ_BYTE_DATA |
169                                     I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
170                 return 0;
171         }
172
173         /* allocate memory for client structure */
174         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
175         if (0 == client) {
176                 printk("not enough kernel memory\n");
177                 return -ENOMEM;
178         }
179
180         /* fill client structure */
181         memcpy(client, &client_template, sizeof(struct i2c_client));
182         client->id = tda9840_id++;
183         client->addr = address;
184         client->adapter = adapter;
185
186         /* tell the i2c layer a new client has arrived */
187         if (0 != (result = i2c_attach_client(client))) {
188                 kfree(client);
189                 return result;
190         }
191
192         /* set initial values for level & stereo - adjustment, mode */
193         byte = 0;
194         result  = command(client, TDA9840_LEVEL_ADJUST, &byte);
195         result += command(client, TDA9840_STEREO_ADJUST, &byte);
196         byte = TDA9840_SET_MONO;
197         result = command(client, TDA9840_SWITCH, &byte);
198         if (result) {
199                 dprintk("could not initialize tda9840\n");
200                 return -ENODEV;
201         }
202
203         printk("tda9840: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]);
204         return 0;
205 }
206
207 static int attach(struct i2c_adapter *adapter)
208 {
209         /* let's see whether this is a know adapter we can attach to */
210         if (adapter->id != I2C_ALGO_SAA7146) {
211                 dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id);
212                 return -ENODEV;
213         }
214
215         return i2c_probe(adapter, &addr_data, &detect);
216 }
217
218 static int detach(struct i2c_client *client)
219 {
220         int ret = i2c_detach_client(client);
221         kfree(client);
222         return ret;
223 }
224
225 static struct i2c_driver driver = {
226         .owner  = THIS_MODULE,
227         .name   = "tda9840",
228         .id     = I2C_DRIVERID_TDA9840,
229         .flags  = I2C_DF_NOTIFY,
230         .attach_adapter = attach,
231         .detach_client  = detach,
232         .command        = command,
233 };
234
235 static struct i2c_client client_template = {
236         I2C_DEVNAME("tda9840"),
237         .driver = &driver,
238 };
239
240 static int __init this_module_init(void)
241 {
242         return i2c_add_driver(&driver);
243 }
244
245 static void __exit this_module_exit(void)
246 {
247         i2c_del_driver(&driver);
248 }
249
250 module_init(this_module_init);
251 module_exit(this_module_exit);
252
253 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
254 MODULE_DESCRIPTION("tda9840 driver");
255 MODULE_LICENSE("GPL");