blob: f539491a0d76f31e7cdfa874a075c8e24e71a043 [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 *
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -030024 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
27#include <linux/module.h> /* Modules */
28#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070029#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h> /* udelay */
31#include <asm/io.h> /* outb, outb_p */
32#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -030033#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030034#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/spinlock.h>
36
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -030037#include <linux/version.h> /* for KERNEL_VERSION MACRO */
38#define RADIO_VERSION KERNEL_VERSION(0,0,2)
39
40static struct v4l2_queryctrl radio_qctrl[] = {
41 {
42 .id = V4L2_CID_AUDIO_MUTE,
43 .name = "Mute",
44 .minimum = 0,
45 .maximum = 1,
46 .default_value = 1,
47 .type = V4L2_CTRL_TYPE_BOOLEAN,
48 },{
49 .id = V4L2_CID_AUDIO_VOLUME,
50 .name = "Volume",
51 .minimum = 0,
52 .maximum = 0xff,
53 .step = 1,
54 .default_value = 0xff,
55 .type = V4L2_CTRL_TYPE_INTEGER,
56 }
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifndef CONFIG_RADIO_TERRATEC_PORT
60#define CONFIG_RADIO_TERRATEC_PORT 0x590
61#endif
62
63/**************** this ones are for the terratec *******************/
64#define BASEPORT 0x590
65#define VOLPORT 0x591
66#define WRT_DIS 0x00
67#define CLK_OFF 0x00
68#define IIC_DATA 0x01
69#define IIC_CLK 0x02
70#define DATA 0x04
71#define CLK_ON 0x08
72#define WRT_EN 0x10
73/*******************************************************************/
74
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030075static int io = CONFIG_RADIO_TERRATEC_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int radio_nr = -1;
77static spinlock_t lock;
78
79struct tt_device
80{
81 int port;
82 int curvol;
83 unsigned long curfreq;
84 int muted;
85};
86
87
88/* local things */
89
90static void cardWriteVol(int volume)
91{
92 int i;
93 volume = volume+(volume * 32); // change both channels
94 spin_lock(&lock);
95 for (i=0;i<8;i++)
96 {
97 if (volume & (0x80>>i))
98 outb(0x80, VOLPORT);
99 else outb(0x00, VOLPORT);
100 }
101 spin_unlock(&lock);
102}
103
104
105
106static void tt_mute(struct tt_device *dev)
107{
108 dev->muted = 1;
109 cardWriteVol(0);
110}
111
112static int tt_setvol(struct tt_device *dev, int vol)
113{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115// printk(KERN_ERR "setvol called, vol = %d\n", vol);
116
117 if(vol == dev->curvol) { /* requested volume = current */
118 if (dev->muted) { /* user is unmuting the card */
119 dev->muted = 0;
120 cardWriteVol(vol); /* enable card */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300121 }
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124 }
125
126 if(vol == 0) { /* volume = 0 means mute the card */
127 cardWriteVol(0); /* "turn off card" by setting vol to 0 */
128 dev->curvol = vol; /* track the volume state! */
129 return 0;
130 }
131
132 dev->muted = 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 cardWriteVol(vol);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 dev->curvol = vol;
137
138 return 0;
139
140}
141
142
143/* this is the worst part in this driver */
144/* many more or less strange things are going on here, but hey, it works :) */
145
146static int tt_setfreq(struct tt_device *dev, unsigned long freq1)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300147{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int freq;
149 int i;
150 int p;
151 int temp;
152 long rest;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 unsigned char buffer[25]; /* we have to bit shift 25 registers */
155 freq = freq1/160; /* convert the freq. to a nice to handle value */
156 for(i=24;i>-1;i--)
157 buffer[i]=0;
158
159 rest = freq*10+10700; /* i once had understood what is going on here */
160 /* maybe some wise guy (friedhelm?) can comment this stuff */
161 i=13;
162 p=10;
163 temp=102400;
164 while (rest!=0)
165 {
166 if (rest%temp == rest)
167 buffer[i] = 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300168 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300170 buffer[i] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 rest = rest-temp;
172 }
173 i--;
174 p--;
175 temp = temp/2;
176 }
177
178 spin_lock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 for (i=24;i>-1;i--) /* bit shift the values to the radiocard */
181 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300182 if (buffer[i]==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 {
184 outb(WRT_EN|DATA, BASEPORT);
185 outb(WRT_EN|DATA|CLK_ON , BASEPORT);
186 outb(WRT_EN|DATA, BASEPORT);
187 }
188 else
189 {
190 outb(WRT_EN|0x00, BASEPORT);
191 outb(WRT_EN|0x00|CLK_ON , BASEPORT);
192 }
193 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300194 outb(0x00, BASEPORT);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 spin_unlock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300197
198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static int tt_getsigstr(struct tt_device *dev) /* TODO */
202{
203 if (inb(io) & 2) /* bit set = no signal present */
204 return 0;
205 return 1; /* signal present */
206}
207
208
209/* implement the video4linux api */
210
211static int tt_do_ioctl(struct inode *inode, struct file *file,
212 unsigned int cmd, void *arg)
213{
214 struct video_device *dev = video_devdata(file);
215 struct tt_device *tt=dev->priv;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 switch(cmd)
218 {
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300219 case VIDIOC_QUERYCAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 {
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300221 struct v4l2_capability *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 memset(v,0,sizeof(*v));
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300223 strlcpy(v->driver, "radio-terratec", sizeof (v->driver));
224 strlcpy(v->card, "ActiveRadio", sizeof (v->card));
225 sprintf(v->bus_info,"ISA");
226 v->version = RADIO_VERSION;
227 v->capabilities = V4L2_CAP_TUNER;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230 }
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300231 case VIDIOC_G_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 {
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300233 struct v4l2_tuner *v = arg;
234
235 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -EINVAL;
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300237
238 memset(v,0,sizeof(*v));
239 strcpy(v->name, "FM");
240 v->type = V4L2_TUNER_RADIO;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 v->rangelow=(87*16000);
243 v->rangehigh=(108*16000);
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300244 v->rxsubchans =V4L2_TUNER_SUB_MONO;
245 v->capability=V4L2_TUNER_CAP_LOW;
246 v->audmode = V4L2_TUNER_MODE_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 v->signal=0xFFFF*tt_getsigstr(tt);
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250 }
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300251 case VIDIOC_S_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 {
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300253 struct v4l2_tuner *v = arg;
254
255 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return -EINVAL;
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259 }
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300260 case VIDIOC_S_FREQUENCY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 {
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300262 struct v4l2_frequency *f = arg;
263
264 tt->curfreq = f->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 tt_setfreq(tt, tt->curfreq);
266 return 0;
267 }
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300268 case VIDIOC_G_FREQUENCY:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300269 {
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300270 struct v4l2_frequency *f = arg;
271
272 f->type = V4L2_TUNER_RADIO;
273 f->frequency = tt->curfreq;
274
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300277 case VIDIOC_QUERYCTRL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 {
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300279 struct v4l2_queryctrl *qc = arg;
280 int i;
281
282 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
283 if (qc->id && qc->id == radio_qctrl[i].id) {
284 memcpy(qc, &(radio_qctrl[i]),
285 sizeof(*qc));
286 return (0);
287 }
288 }
289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300291 case VIDIOC_G_CTRL:
292 {
293 struct v4l2_control *ctrl= arg;
294
295 switch (ctrl->id) {
296 case V4L2_CID_AUDIO_MUTE:
297 if (tt->muted)
298 ctrl->value=1;
299 else
300 ctrl->value=0;
301 return (0);
302 case V4L2_CID_AUDIO_VOLUME:
303 ctrl->value=tt->curvol * 6554;
304 return (0);
305 }
306 return -EINVAL;
307 }
308 case VIDIOC_S_CTRL:
309 {
310 struct v4l2_control *ctrl= arg;
311
312 switch (ctrl->id) {
313 case V4L2_CID_AUDIO_MUTE:
314 if (ctrl->value) {
315 tt_mute(tt);
316 } else {
317 tt_setvol(tt,tt->curvol);
318 }
319 return (0);
320 case V4L2_CID_AUDIO_VOLUME:
321 tt_setvol(tt,ctrl->value);
322 return (0);
323 }
324 return -EINVAL;
325 }
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 default:
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300328 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
329 tt_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331}
332
333static int tt_ioctl(struct inode *inode, struct file *file,
334 unsigned int cmd, unsigned long arg)
335{
336 return video_usercopy(inode, file, cmd, arg, tt_do_ioctl);
337}
338
339static struct tt_device terratec_unit;
340
341static struct file_operations terratec_fops = {
342 .owner = THIS_MODULE,
343 .open = video_exclusive_open,
344 .release = video_exclusive_release,
345 .ioctl = tt_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200346 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 .llseek = no_llseek,
348};
349
350static struct video_device terratec_radio=
351{
352 .owner = THIS_MODULE,
353 .name = "TerraTec ActiveRadio",
354 .type = VID_TYPE_TUNER,
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -0300355 .hardware = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 .fops = &terratec_fops,
357};
358
359static int __init terratec_init(void)
360{
361 if(io==-1)
362 {
363 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
364 return -EINVAL;
365 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300366 if (!request_region(io, 2, "terratec"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 {
368 printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
369 return -EBUSY;
370 }
371
372 terratec_radio.priv=&terratec_unit;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_lock_init(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if(video_register_device(&terratec_radio, VFL_TYPE_RADIO, radio_nr)==-1)
377 {
378 release_region(io,2);
379 return -EINVAL;
380 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
383
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300384 /* mute card - prevents noisy bootups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* this ensures that the volume is all the way down */
387 cardWriteVol(0);
388 terratec_unit.curvol = 0;
389
390 return 0;
391}
392
393MODULE_AUTHOR("R.OFFERMANNS & others");
394MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
395MODULE_LICENSE("GPL");
396module_param(io, int, 0);
397MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
398module_param(radio_nr, int, 0);
399
400static void __exit terratec_cleanup_module(void)
401{
402 video_unregister_device(&terratec_radio);
403 release_region(io,2);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300404 printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407module_init(terratec_init);
408module_exit(terratec_cleanup_module);
409