blob: dfba4ae596cd342abb3e737234274d4488ae4e23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * History:
8 * 1999-05-21 First preview release
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
26#include <linux/module.h> /* Modules */
27#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070028#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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 */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030033#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030053static int io = CONFIG_RADIO_TERRATEC_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int radio_nr = -1;
55static spinlock_t lock;
56
57struct tt_device
58{
59 int port;
60 int curvol;
61 unsigned long curfreq;
62 int muted;
63};
64
65
66/* local things */
67
68static 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
84static void tt_mute(struct tt_device *dev)
85{
86 dev->muted = 1;
87 cardWriteVol(0);
88}
89
90static int tt_setvol(struct tt_device *dev, int vol)
91{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093// 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 */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030099 }
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 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;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 cardWriteVol(vol);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 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
124static int tt_setfreq(struct tt_device *dev, unsigned long freq1)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300125{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int freq;
127 int i;
128 int p;
129 int temp;
130 long rest;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 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;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300146 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300148 buffer[i] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 rest = rest-temp;
150 }
151 i--;
152 p--;
153 temp = temp/2;
154 }
155
156 spin_lock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 for (i=24;i>-1;i--) /* bit shift the values to the radiocard */
159 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300160 if (buffer[i]==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 {
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 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300172 outb(0x00, BASEPORT);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 spin_unlock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300175
176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static 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
189static 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;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 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;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300210 if(v->tuner) /* Only 1 tuner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 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:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300242 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 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");
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 case VIDIOCSAUDIO:
252 {
253 struct video_audio *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300254 if(v->audio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EINVAL;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300256 if(v->flags&VIDEO_AUDIO_MUTE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 tt_mute(tt);
258 else
259 tt_setvol(tt,v->volume/6554);
260 return 0;
261 }
262 default:
263 return -ENOIOCTLCMD;
264 }
265}
266
267static 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
273static struct tt_device terratec_unit;
274
275static struct file_operations terratec_fops = {
276 .owner = THIS_MODULE,
277 .open = video_exclusive_open,
278 .release = video_exclusive_release,
279 .ioctl = tt_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200280 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 .llseek = no_llseek,
282};
283
284static 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
293static 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 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300300 if (!request_region(io, 2, "terratec"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 {
302 printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
303 return -EBUSY;
304 }
305
306 terratec_radio.priv=&terratec_unit;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_lock_init(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if(video_register_device(&terratec_radio, VFL_TYPE_RADIO, radio_nr)==-1)
311 {
312 release_region(io,2);
313 return -EINVAL;
314 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
317
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300318 /* mute card - prevents noisy bootups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
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
327MODULE_AUTHOR("R.OFFERMANNS & others");
328MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
329MODULE_LICENSE("GPL");
330module_param(io, int, 0);
331MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
332module_param(radio_nr, int, 0);
333
334static void __exit terratec_cleanup_module(void)
335{
336 video_unregister_device(&terratec_radio);
337 release_region(io,2);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300338 printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341module_init(terratec_init);
342module_exit(terratec_cleanup_module);
343