Import 2.3.25pre1
[davej-history.git] / drivers / char / radio-terratec.c
blob1dda1618e0980ddf4a311a225ce252ecc4572916
1 /* Terratec ActiveRadio ISA Standalone card driver for Linux radio support
2 * (c) 1999 R. Offermanns (rolf@offermanns.de)
3 * based on the aimslab radio driver from M. Kirkwood
4 * many thanks to Michael Becker and Friedhelm Birth (from TerraTec)
5 *
7 * History:
8 * 1999-05-21 First preview release
9 *
10 * Notes on the hardware:
11 * There are two "main" chips on the card:
12 * - Philips OM5610 (http://www-us.semiconductors.philips.com/acrobat/datasheets/OM5610_2.pdf)
13 * - Philips SAA6588 (http://www-us.semiconductors.philips.com/acrobat/datasheets/SAA6588_1.pdf)
14 * (you can get the datasheet at the above links)
16 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
17 * Volume Control is done digitally
19 * there is a I2C controlled RDS decoder (SAA6588) onboard, which i would like to support someday
20 * (as soon i have understand how to get started :)
21 * If you can help me out with that, please contact me!!
26 #include <linux/module.h> /* Modules */
27 #include <linux/init.h> /* Initdata */
28 #include <linux/ioport.h> /* check_region, request_region */
29 #include <linux/delay.h> /* udelay */
30 #include <asm/io.h> /* outb, outb_p */
31 #include <asm/uaccess.h> /* copy to/from user */
32 #include <linux/videodev.h> /* kernel radio structs */
33 #include <linux/config.h> /* CONFIG_RADIO_TERRATEC_PORT */
34 #include <linux/spinlock.h>
36 #ifndef CONFIG_RADIO_TERRATEC_PORT
37 #define CONFIG_RADIO_TERRATEC_PORT 0x590
38 #endif
40 /**************** this ones are for the terratec *******************/
41 #define BASEPORT 0x590
42 #define VOLPORT 0x591
43 #define WRT_DIS 0x00
44 #define CLK_OFF 0x00
45 #define IIC_DATA 0x01
46 #define IIC_CLK 0x02
47 #define DATA 0x04
48 #define CLK_ON 0x08
49 #define WRT_EN 0x10
50 /*******************************************************************/
52 static int io = CONFIG_RADIO_TERRATEC_PORT;
53 static int users = 0;
54 static spinlock_t lock;
56 struct tt_device
58 int port;
59 int curvol;
60 unsigned long curfreq;
61 int muted;
65 /* local things */
67 static void cardWriteVol(int volume)
69 int i;
70 volume = volume+(volume * 32); // change both channels
71 spin_lock(&lock);
72 for (i=0;i<8;i++)
74 if (volume & (0x80>>i))
75 outb(0x80, VOLPORT);
76 else outb(0x00, VOLPORT);
78 spin_unlock(&lock);
83 static void tt_mute(struct tt_device *dev)
85 dev->muted = 1;
86 cardWriteVol(0);
89 static int tt_setvol(struct tt_device *dev, int vol)
92 // printk(KERN_ERR "setvol called, vol = %d\n", vol);
94 if(vol == dev->curvol) { /* requested volume = current */
95 if (dev->muted) { /* user is unmuting the card */
96 dev->muted = 0;
97 cardWriteVol(vol); /* enable card */
100 return 0;
103 if(vol == 0) { /* volume = 0 means mute the card */
104 cardWriteVol(0); /* "turn off card" by setting vol to 0 */
105 dev->curvol = vol; /* track the volume state! */
106 return 0;
109 dev->muted = 0;
111 cardWriteVol(vol);
113 dev->curvol = vol;
115 return 0;
120 /* this is the worst part in this driver */
121 /* many more or less strange things are going on here, but hey, it works :) */
123 static int tt_setfreq(struct tt_device *dev, unsigned long freq1)
125 int freq;
126 int i;
127 int p;
128 int temp;
129 long rest;
131 unsigned char buffer[25]; /* we have to bit shift 25 registers */
132 freq = freq1/160; /* convert the freq. to a nice to handel value */
133 for(i=24;i>-1;i--)
134 buffer[i]=0;
136 rest = freq*10+10700; /* i once had understood what is going on here */
137 /* maybe some wise guy (friedhelm?) can comment this stuff */
138 i=13;
139 p=10;
140 temp=102400;
141 while (rest!=0)
143 if (rest%temp == rest)
144 buffer[i] = 0;
145 else
147 buffer[i] = 1;
148 rest = rest-temp;
150 i--;
151 p--;
152 temp = temp/2;
155 spin_lock(&lock);
157 for (i=24;i>-1;i--) /* bit shift the values to the radiocard */
159 if (buffer[i]==1)
161 outb(WRT_EN|DATA, BASEPORT);
162 outb(WRT_EN|DATA|CLK_ON , BASEPORT);
163 outb(WRT_EN|DATA, BASEPORT);
165 else
167 outb(WRT_EN|0x00, BASEPORT);
168 outb(WRT_EN|0x00|CLK_ON , BASEPORT);
171 outb(0x00, BASEPORT);
173 spin_unlock(&lock);
175 return 0;
178 int tt_getsigstr(struct tt_device *dev) /* TODO */
180 if (inb(io) & 2) /* bit set = no signal present */
181 return 0;
182 return 1; /* signal present */
186 /* implement the video4linux api */
188 static int tt_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
190 struct tt_device *tt=dev->priv;
192 switch(cmd)
194 case VIDIOCGCAP:
196 struct video_capability v;
197 v.type=VID_TYPE_TUNER;
198 v.channels=1;
199 v.audios=1;
200 /* No we don't do pictures */
201 v.maxwidth=0;
202 v.maxheight=0;
203 v.minwidth=0;
204 v.minheight=0;
205 strcpy(v.name, "ActiveRadio");
206 if(copy_to_user(arg,&v,sizeof(v)))
207 return -EFAULT;
208 return 0;
210 case VIDIOCGTUNER:
212 struct video_tuner v;
213 if(copy_from_user(&v, arg,sizeof(v))!=0)
214 return -EFAULT;
215 if(v.tuner) /* Only 1 tuner */
216 return -EINVAL;
217 v.rangelow=(87*16000);
218 v.rangehigh=(108*16000);
219 v.flags=VIDEO_TUNER_LOW;
220 v.mode=VIDEO_MODE_AUTO;
221 strcpy(v.name, "FM");
222 v.signal=0xFFFF*tt_getsigstr(tt);
223 if(copy_to_user(arg,&v, sizeof(v)))
224 return -EFAULT;
225 return 0;
227 case VIDIOCSTUNER:
229 struct video_tuner v;
230 if(copy_from_user(&v, arg, sizeof(v)))
231 return -EFAULT;
232 if(v.tuner!=0)
233 return -EINVAL;
234 /* Only 1 tuner so no setting needed ! */
235 return 0;
237 case VIDIOCGFREQ:
238 if(copy_to_user(arg, &tt->curfreq, sizeof(tt->curfreq)))
239 return -EFAULT;
240 return 0;
241 case VIDIOCSFREQ:
242 if(copy_from_user(&tt->curfreq, arg,sizeof(tt->curfreq)))
243 return -EFAULT;
244 tt_setfreq(tt, tt->curfreq);
245 return 0;
246 case VIDIOCGAUDIO:
248 struct video_audio v;
249 memset(&v,0, sizeof(v));
250 v.flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
251 v.volume=tt->curvol * 6554;
252 v.step=6554;
253 strcpy(v.name, "Radio");
254 if(copy_to_user(arg,&v, sizeof(v)))
255 return -EFAULT;
256 return 0;
258 case VIDIOCSAUDIO:
260 struct video_audio v;
261 if(copy_from_user(&v, arg, sizeof(v)))
262 return -EFAULT;
263 if(v.audio)
264 return -EINVAL;
266 if(v.flags&VIDEO_AUDIO_MUTE)
267 tt_mute(tt);
268 else
269 tt_setvol(tt,v.volume/6554);
271 return 0;
273 default:
274 return -ENOIOCTLCMD;
278 static int tt_open(struct video_device *dev, int flags)
280 if(users)
281 return -EBUSY;
282 users++;
283 MOD_INC_USE_COUNT;
284 return 0;
287 static void tt_close(struct video_device *dev)
289 users--;
290 MOD_DEC_USE_COUNT;
293 static struct tt_device terratec_unit;
295 static struct video_device terratec_radio=
297 "TerraTec ActiveRadio",
298 VID_TYPE_TUNER,
299 VID_HARDWARE_TERRATEC,
300 tt_open,
301 tt_close,
302 NULL, /* Can't read (no capture ability) */
303 NULL, /* Can't write */
304 NULL, /* No poll */
305 tt_ioctl,
306 NULL,
307 NULL
310 static int __init terratec_init(void)
312 if(io==-1)
314 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
315 return -EINVAL;
317 if (check_region(io, 2))
319 printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
320 return -EBUSY;
323 terratec_radio.priv=&terratec_unit;
325 spin_lock_init(&lock);
327 if(video_register_device(&terratec_radio, VFL_TYPE_RADIO)==-1)
328 return -EINVAL;
330 request_region(io, 2, "terratec");
331 printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
333 /* mute card - prevents noisy bootups */
335 /* this ensures that the volume is all the way down */
336 cardWriteVol(0);
337 terratec_unit.curvol = 0;
339 return 0;
342 MODULE_AUTHOR("R.OFFERMANNS & others");
343 MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
344 MODULE_PARM(io, "i");
345 MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
347 EXPORT_NO_SYMBOLS;
349 static void __exit terratec_cleanup_module(void)
351 video_unregister_device(&terratec_radio);
352 release_region(io,2);
353 printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
356 module_init(terratec_init);
357 module_exit(terratec_cleanup_module);