Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[pandora-kernel.git] / drivers / media / radio / radio-terratec.c
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  *
6  *
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)
15  *
16  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
17  *  Volume Control is done digitally
18  *
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!!
22  *
23  *
24  */
25
26 #include <linux/module.h>       /* Modules                      */
27 #include <linux/init.h>         /* Initdata                     */
28 #include <linux/ioport.h>       /* 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 <media/v4l2-common.h>
34 #include <linux/config.h>       /* CONFIG_RADIO_TERRATEC_PORT   */
35 #include <linux/spinlock.h>
36
37 #ifndef CONFIG_RADIO_TERRATEC_PORT
38 #define CONFIG_RADIO_TERRATEC_PORT 0x590
39 #endif
40
41 /**************** this ones are for the terratec *******************/
42 #define BASEPORT        0x590
43 #define VOLPORT         0x591
44 #define WRT_DIS         0x00
45 #define CLK_OFF         0x00
46 #define IIC_DATA        0x01
47 #define IIC_CLK         0x02
48 #define DATA            0x04
49 #define CLK_ON          0x08
50 #define WRT_EN          0x10
51 /*******************************************************************/
52
53 static int io = CONFIG_RADIO_TERRATEC_PORT;
54 static int radio_nr = -1;
55 static spinlock_t lock;
56
57 struct tt_device
58 {
59         int port;
60         int curvol;
61         unsigned long curfreq;
62         int muted;
63 };
64
65
66 /* local things */
67
68 static void cardWriteVol(int volume)
69 {
70         int i;
71         volume = volume+(volume * 32); // change both channels
72         spin_lock(&lock);
73         for (i=0;i<8;i++)
74         {
75                 if (volume & (0x80>>i))
76                         outb(0x80, VOLPORT);
77                 else outb(0x00, VOLPORT);
78         }
79         spin_unlock(&lock);
80 }
81
82
83
84 static void tt_mute(struct tt_device *dev)
85 {
86         dev->muted = 1;
87         cardWriteVol(0);
88 }
89
90 static int tt_setvol(struct tt_device *dev, int vol)
91 {
92
93 //      printk(KERN_ERR "setvol called, vol = %d\n", vol);
94
95         if(vol == dev->curvol) {        /* requested volume = current */
96                 if (dev->muted) {       /* user is unmuting the card  */
97                         dev->muted = 0;
98                         cardWriteVol(vol);      /* enable card */
99                 }
100
101                 return 0;
102         }
103
104         if(vol == 0) {                  /* volume = 0 means mute the card */
105                 cardWriteVol(0);        /* "turn off card" by setting vol to 0 */
106                 dev->curvol = vol;      /* track the volume state!      */
107                 return 0;
108         }
109
110         dev->muted = 0;
111
112         cardWriteVol(vol);
113
114         dev->curvol = vol;
115
116         return 0;
117
118 }
119
120
121 /* this is the worst part in this driver */
122 /* many more or less strange things are going on here, but hey, it works :) */
123
124 static int tt_setfreq(struct tt_device *dev, unsigned long freq1)
125 {
126         int freq;
127         int i;
128         int p;
129         int  temp;
130         long rest;
131
132         unsigned char buffer[25];               /* we have to bit shift 25 registers */
133         freq = freq1/160;                       /* convert the freq. to a nice to handle value */
134         for(i=24;i>-1;i--)
135                 buffer[i]=0;
136
137         rest = freq*10+10700;           /* i once had understood what is going on here */
138                                         /* maybe some wise guy (friedhelm?) can comment this stuff */
139         i=13;
140         p=10;
141         temp=102400;
142         while (rest!=0)
143         {
144                 if (rest%temp  == rest)
145                         buffer[i] = 0;
146                 else
147                 {
148                         buffer[i] = 1;
149                         rest = rest-temp;
150                 }
151                 i--;
152                 p--;
153                 temp = temp/2;
154        }
155
156         spin_lock(&lock);
157
158         for (i=24;i>-1;i--)                     /* bit shift the values to the radiocard */
159         {
160                 if (buffer[i]==1)
161                 {
162                         outb(WRT_EN|DATA, BASEPORT);
163                         outb(WRT_EN|DATA|CLK_ON  , BASEPORT);
164                         outb(WRT_EN|DATA, BASEPORT);
165                 }
166                 else
167                 {
168                         outb(WRT_EN|0x00, BASEPORT);
169                         outb(WRT_EN|0x00|CLK_ON  , BASEPORT);
170                 }
171         }
172         outb(0x00, BASEPORT);
173
174         spin_unlock(&lock);
175
176         return 0;
177 }
178
179 static int tt_getsigstr(struct tt_device *dev)          /* TODO */
180 {
181         if (inb(io) & 2)        /* bit set = no signal present  */
182                 return 0;
183         return 1;               /* signal present               */
184 }
185
186
187 /* implement the video4linux api */
188
189 static int tt_do_ioctl(struct inode *inode, struct file *file,
190                        unsigned int cmd, void *arg)
191 {
192         struct video_device *dev = video_devdata(file);
193         struct tt_device *tt=dev->priv;
194
195         switch(cmd)
196         {
197                 case VIDIOCGCAP:
198                 {
199                         struct video_capability *v = arg;
200                         memset(v,0,sizeof(*v));
201                         v->type=VID_TYPE_TUNER;
202                         v->channels=1;
203                         v->audios=1;
204                         strcpy(v->name, "ActiveRadio");
205                         return 0;
206                 }
207                 case VIDIOCGTUNER:
208                 {
209                         struct video_tuner *v = arg;
210                         if(v->tuner)    /* Only 1 tuner */
211                                 return -EINVAL;
212                         v->rangelow=(87*16000);
213                         v->rangehigh=(108*16000);
214                         v->flags=VIDEO_TUNER_LOW;
215                         v->mode=VIDEO_MODE_AUTO;
216                         strcpy(v->name, "FM");
217                         v->signal=0xFFFF*tt_getsigstr(tt);
218                         return 0;
219                 }
220                 case VIDIOCSTUNER:
221                 {
222                         struct video_tuner *v = arg;
223                         if(v->tuner!=0)
224                                 return -EINVAL;
225                         /* Only 1 tuner so no setting needed ! */
226                         return 0;
227                 }
228                 case VIDIOCGFREQ:
229                 {
230                         unsigned long *freq = arg;
231                         *freq = tt->curfreq;
232                         return 0;
233                 }
234                 case VIDIOCSFREQ:
235                 {
236                         unsigned long *freq = arg;
237                         tt->curfreq = *freq;
238                         tt_setfreq(tt, tt->curfreq);
239                         return 0;
240                 }
241                 case VIDIOCGAUDIO:
242                 {
243                         struct video_audio *v = arg;
244                         memset(v,0, sizeof(*v));
245                         v->flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
246                         v->volume=tt->curvol * 6554;
247                         v->step=6554;
248                         strcpy(v->name, "Radio");
249                         return 0;
250                 }
251                 case VIDIOCSAUDIO:
252                 {
253                         struct video_audio *v = arg;
254                         if(v->audio)
255                                 return -EINVAL;
256                         if(v->flags&VIDEO_AUDIO_MUTE)
257                                 tt_mute(tt);
258                         else
259                                 tt_setvol(tt,v->volume/6554);
260                         return 0;
261                 }
262                 default:
263                         return -ENOIOCTLCMD;
264         }
265 }
266
267 static int tt_ioctl(struct inode *inode, struct file *file,
268                     unsigned int cmd, unsigned long arg)
269 {
270         return video_usercopy(inode, file, cmd, arg, tt_do_ioctl);
271 }
272
273 static struct tt_device terratec_unit;
274
275 static struct file_operations terratec_fops = {
276         .owner          = THIS_MODULE,
277         .open           = video_exclusive_open,
278         .release        = video_exclusive_release,
279         .ioctl          = tt_ioctl,
280         .compat_ioctl   = v4l_compat_ioctl32,
281         .llseek         = no_llseek,
282 };
283
284 static struct video_device terratec_radio=
285 {
286         .owner          = THIS_MODULE,
287         .name           = "TerraTec ActiveRadio",
288         .type           = VID_TYPE_TUNER,
289         .hardware       = VID_HARDWARE_TERRATEC,
290         .fops           = &terratec_fops,
291 };
292
293 static int __init terratec_init(void)
294 {
295         if(io==-1)
296         {
297                 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
298                 return -EINVAL;
299         }
300         if (!request_region(io, 2, "terratec"))
301         {
302                 printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
303                 return -EBUSY;
304         }
305
306         terratec_radio.priv=&terratec_unit;
307
308         spin_lock_init(&lock);
309
310         if(video_register_device(&terratec_radio, VFL_TYPE_RADIO, radio_nr)==-1)
311         {
312                 release_region(io,2);
313                 return -EINVAL;
314         }
315
316         printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
317
318         /* mute card - prevents noisy bootups */
319
320         /* this ensures that the volume is all the way down  */
321         cardWriteVol(0);
322         terratec_unit.curvol = 0;
323
324         return 0;
325 }
326
327 MODULE_AUTHOR("R.OFFERMANNS & others");
328 MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
329 MODULE_LICENSE("GPL");
330 module_param(io, int, 0);
331 MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
332 module_param(radio_nr, int, 0);
333
334 static void __exit terratec_cleanup_module(void)
335 {
336         video_unregister_device(&terratec_radio);
337         release_region(io,2);
338         printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
339 }
340
341 module_init(terratec_init);
342 module_exit(terratec_cleanup_module);
343