Linux 2.4.0-test7-pre7
[davej-history.git] / drivers / media / video / tea6420.c
blob3a1d6351776f571840e6d14e2c2d64064cd3a3e4
1 /*
2 * for the TEA6420 chip (only found on 3DFX (STB) TV/FM cards to the best
3 * of my knowledge)
4 * Copyright (C) 2000 Dave Stuart <justdave@ynn.com>
5 * This code is placed under the terms of the GNU General Public License
6 * Code liberally copied from tea6300 by . . .
8 * Copyright (c) 1998 Greg Alexander <galexand@acm.org>
9 * This code is placed under the terms of the GNU General Public License
10 * Code liberally copied from msp3400.c, which is by Gerd Knorr
12 * Changes:
13 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 08/14/2000
14 * - resource allocation fixes in tea6300_attach
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/timer.h>
22 #include <linux/delay.h>
23 #include <linux/errno.h>
24 #include <linux/malloc.h>
25 #include <linux/videodev.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-algo-bit.h>
29 #include "bttv.h"
30 #include "audiochip.h"
33 /* Addresses to scan */
34 #define I2C_TEA6420 0x98
35 static unsigned short normal_i2c[] = {
36 I2C_TEA6420 >> 1,
37 I2C_CLIENT_END};
38 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
39 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
40 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
41 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
42 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
43 static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
44 static struct i2c_client_address_data addr_data = {
45 normal_i2c, normal_i2c_range,
46 probe, probe_range,
47 ignore, ignore_range,
48 force
52 MODULE_PARM(debug,"i");
53 static int debug = 0; /* insmod parameter */
55 #define dprintk if (debug) printk
58 struct tea6420 {
59 int mode; /* set to AUDIO_{OFF,TUNER,RADIO,EXTERN} */
60 int stereo;
63 static struct i2c_driver driver;
64 static struct i2c_client client_template;
66 #define TEA6420_S_SA 0x00 /* stereo A input */
67 #define TEA6420_S_SB 0x01 /* stereo B */
68 #define TEA6420_S_SC 0x02 /* stereo C */
69 #define TEA6420_S_SD 0x03 /* stereo D */
70 #define TEA6420_S_SE 0x04 /* stereo E */
71 #define TEA6420_S_GMU 0x05 /* general mute */
74 /* ******************************** *
75 * functions for talking to TEA6420 *
76 * ******************************** */
78 static int tea6420_write(struct i2c_client *client, int val)
80 unsigned char buffer[2];
81 int result;
83 /* buffer[0] = addr; */
84 buffer[0] = val;
85 result = i2c_master_send(client,buffer,1);
86 if (1 != result) {
87 printk(KERN_WARNING "tea6420: I/O error, trying (write
88 0x%x) result = %d\n", val, result);
89 return -1;
91 return 0;
95 static void do_tea6420_init(struct i2c_client *client)
97 struct tea6420 *tea = client->data;
99 tea->mode=AUDIO_OFF;
100 tea->stereo=1;
101 tea6420_write(client, TEA6420_S_GMU); /* mute */
104 static void tea6420_audio(struct i2c_client *client, int mode)
106 struct tea6420 *tea = client->data;
108 /* valid for AUDIO_TUNER, RADIO, EXTERN, OFF */
109 dprintk(KERN_DEBUG "tea6420_audio:%d (T,R,E,I,O)\n",mode);
110 tea->mode=mode;
111 if (mode==AUDIO_OFF) { /* just mute it */
112 tea6420_write(client, TEA6420_S_GMU);
113 return;
115 switch(mode) {
116 case AUDIO_TUNER:
117 tea6420_write(client, TEA6420_S_SA);
118 break;
119 case AUDIO_RADIO:
120 tea6420_write(client, TEA6420_S_SB);
121 break;
122 case AUDIO_EXTERN:
123 tea6420_write(client, TEA6420_S_SC);
124 break;
129 /* *********************** *
130 * i2c interface functions *
131 * *********************** */
133 static int tea6420_attach(struct i2c_adapter *adap, int addr,
134 unsigned short flags, int kind)
136 struct tea6420 *tea;
137 struct i2c_client *client;
139 client = kmalloc(sizeof *client,GFP_KERNEL);
140 if (!client)
141 return -ENOMEM;
142 memcpy(client,&client_template,sizeof(struct i2c_client));
143 client->adapter = adap;
144 client->addr = addr;
146 client->data = tea = kmalloc(sizeof *tea,GFP_KERNEL);
147 if (!tea) {
148 kfree(client);
149 return -ENOMEM;
151 memset(tea,0,sizeof *tea);
152 do_tea6420_init(client);
154 MOD_INC_USE_COUNT;
155 strcpy(client->name,"TEA6420");
156 printk(KERN_INFO "tea6420: initialized\n");
158 i2c_attach_client(client);
159 return 0;
162 static int tea6420_probe(struct i2c_adapter *adap)
164 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
165 return i2c_probe(adap, &addr_data, tea6420_attach);
166 return 0;
169 static int tea6420_detach(struct i2c_client *client)
171 struct tea6420 *tea = client->data;
173 do_tea6420_init(client);
174 i2c_detach_client(client);
176 kfree(tea);
177 kfree(client);
178 MOD_DEC_USE_COUNT;
179 return 0;
182 static int
183 tea6420_command(struct i2c_client *client, unsigned int cmd, void *arg)
185 __u16 *sarg = arg;
187 switch (cmd) {
188 case AUDC_SET_RADIO:
189 tea6420_audio(client,AUDIO_RADIO);
190 break;
191 case AUDC_SET_INPUT:
192 tea6420_audio(client,*sarg);
193 break;
195 /* --- v4l ioctls --- */
196 /* take care: bttv does userspace copying, we'll get a
197 kernel pointer here... */
198 case VIDIOCGAUDIO:
200 struct video_audio *va = arg;
202 va->flags |= VIDEO_AUDIO_VOLUME |
203 VIDEO_AUDIO_BASS |
204 VIDEO_AUDIO_TREBLE;
205 /* va->volume=MAX(tea->left,tea->right);
206 va->balance=(32768*MIN(tea->left,tea->right))/
207 (va->volume ? va->volume : 1);
208 va->balance=(tea->left<tea->right)?
209 (65535-va->balance) : va->balance;
210 va->bass = tea->bass;
211 va->treble = tea->treble;
212 */ break;
214 case VIDIOCSAUDIO:
217 /* tea->left = (MIN(65536 - va->balance,32768) *
218 va->volume) / 32768;
219 tea->right = (MIN(va->balance,32768) *
220 va->volume) / 32768;
221 tea->bass = va->bass;
222 tea->treble = va->treble;
223 tea6420_set(client);
224 */ break;
227 default:
228 /* nothing */
230 return 0;
233 static struct i2c_driver driver = {
234 "i2c tea6420 driver",
235 I2C_DRIVERID_TEA6420,
236 I2C_DF_NOTIFY,
237 tea6420_probe,
238 tea6420_detach,
239 tea6420_command,
242 static struct i2c_client client_template =
244 "(unset)", /* name */
248 NULL,
249 &driver
252 #ifdef MODULE
253 int init_module(void)
254 #else
255 int tea6420_init(void)
256 #endif
258 i2c_add_driver(&driver);
259 return 0;
262 #ifdef MODULE
263 void cleanup_module(void)
265 i2c_del_driver(&driver);
267 #endif
270 * Local variables:
271 * c-basic-offset: 8
272 * End: