blob: f2ed9cc3cf3b7d6fcdd462f5ee82ff6dd414558e [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 */
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -030030#include <linux/videodev2.h> /* kernel radio structs */
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030031#include <linux/mutex.h>
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030032#include <linux/io.h> /* outb, outb_p */
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030033#include <media/v4l2-device.h>
34#include <media/v4l2-ioctl.h>
35
36MODULE_AUTHOR("R.OFFERMANNS & others");
37MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
38MODULE_LICENSE("GPL");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030039MODULE_VERSION("0.0.3");
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030040
41#ifndef CONFIG_RADIO_TERRATEC_PORT
42#define CONFIG_RADIO_TERRATEC_PORT 0x590
43#endif
44
45static int io = CONFIG_RADIO_TERRATEC_PORT;
46static int radio_nr = -1;
47
48module_param(io, int, 0);
49MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
50module_param(radio_nr, int, 0);
51
Mauro Carvalho Chehab55ac7b62006-08-08 09:10:03 -030052static struct v4l2_queryctrl radio_qctrl[] = {
53 {
54 .id = V4L2_CID_AUDIO_MUTE,
55 .name = "Mute",
56 .minimum = 0,
57 .maximum = 1,
58 .default_value = 1,
59 .type = V4L2_CTRL_TYPE_BOOLEAN,
60 },{
61 .id = V4L2_CID_AUDIO_VOLUME,
62 .name = "Volume",
63 .minimum = 0,
64 .maximum = 0xff,
65 .step = 1,
66 .default_value = 0xff,
67 .type = V4L2_CTRL_TYPE_INTEGER,
68 }
69};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define WRT_DIS 0x00
72#define CLK_OFF 0x00
73#define IIC_DATA 0x01
74#define IIC_CLK 0x02
75#define DATA 0x04
76#define CLK_ON 0x08
77#define WRT_EN 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030079struct terratec
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030081 struct v4l2_device v4l2_dev;
82 struct video_device vdev;
83 int io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int curvol;
85 unsigned long curfreq;
86 int muted;
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030087 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030090static struct terratec terratec_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* local things */
93
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030094static void tt_write_vol(struct terratec *tt, int volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int i;
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -030097
98 volume = volume + (volume * 32); /* change both channels */
99 mutex_lock(&tt->lock);
100 for (i = 0; i < 8; i++) {
101 if (volume & (0x80 >> i))
102 outb(0x80, tt->io + 1);
103 else
104 outb(0x00, tt->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300106 mutex_unlock(&tt->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109
110
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300111static void tt_mute(struct terratec *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300113 tt->muted = 1;
114 tt_write_vol(tt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300117static int tt_setvol(struct terratec *tt, int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300119 if (vol == tt->curvol) { /* requested volume = current */
120 if (tt->muted) { /* user is unmuting the card */
121 tt->muted = 0;
122 tt_write_vol(tt, vol); /* enable card */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
125 }
126
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300127 if (vol == 0) { /* volume = 0 means mute the card */
128 tt_write_vol(tt, 0); /* "turn off card" by setting vol to 0 */
129 tt->curvol = vol; /* track the volume state! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
131 }
132
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300133 tt->muted = 0;
134 tt_write_vol(tt, vol);
135 tt->curvol = vol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139
140/* this is the worst part in this driver */
141/* many more or less strange things are going on here, but hey, it works :) */
142
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300143static int tt_setfreq(struct terratec *tt, unsigned long freq1)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300144{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int freq;
146 int i;
147 int p;
148 int temp;
149 long rest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned char buffer[25]; /* we have to bit shift 25 registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300152 mutex_lock(&tt->lock);
153
154 tt->curfreq = freq1;
155
156 freq = freq1 / 160; /* convert the freq. to a nice to handle value */
157 memset(buffer, 0, sizeof(buffer));
158
159 rest = freq * 10 + 10700; /* I once had understood what is going on here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* maybe some wise guy (friedhelm?) can comment this stuff */
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300161 i = 13;
162 p = 10;
163 temp = 102400;
164 while (rest != 0) {
165 if (rest % temp == rest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 buffer[i] = 0;
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300167 else {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300168 buffer[i] = 1;
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300169 rest = rest - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 i--;
172 p--;
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300173 temp = temp / 2;
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300176 for (i = 24; i > -1; i--) { /* bit shift the values to the radiocard */
177 if (buffer[i] == 1) {
178 outb(WRT_EN | DATA, tt->io);
179 outb(WRT_EN | DATA | CLK_ON, tt->io);
180 outb(WRT_EN | DATA, tt->io);
181 } else {
182 outb(WRT_EN | 0x00, tt->io);
183 outb(WRT_EN | 0x00 | CLK_ON, tt->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 }
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300186 outb(0x00, tt->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300187
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300188 mutex_unlock(&tt->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300189
190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300193static int tt_getsigstr(struct terratec *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300195 if (inb(tt->io) & 2) /* bit set = no signal present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 0;
197 return 1; /* signal present */
198}
199
Douglas Landgraf1de69232007-04-22 23:15:47 -0300200static int vidioc_querycap(struct file *file, void *priv,
201 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Douglas Landgraf1de69232007-04-22 23:15:47 -0300203 strlcpy(v->driver, "radio-terratec", sizeof(v->driver));
204 strlcpy(v->card, "ActiveRadio", sizeof(v->card));
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300205 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300206 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Douglas Landgraf1de69232007-04-22 23:15:47 -0300207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Douglas Landgraf1de69232007-04-22 23:15:47 -0300210static int vidioc_g_tuner(struct file *file, void *priv,
211 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300213 struct terratec *tt = video_drvdata(file);
Douglas Landgraf1de69232007-04-22 23:15:47 -0300214
215 if (v->index > 0)
216 return -EINVAL;
217
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300218 strlcpy(v->name, "FM", sizeof(v->name));
Douglas Landgraf1de69232007-04-22 23:15:47 -0300219 v->type = V4L2_TUNER_RADIO;
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300220 v->rangelow = 87 * 16000;
221 v->rangehigh = 108 * 16000;
Douglas Landgraf1de69232007-04-22 23:15:47 -0300222 v->rxsubchans = V4L2_TUNER_SUB_MONO;
223 v->capability = V4L2_TUNER_CAP_LOW;
224 v->audmode = V4L2_TUNER_MODE_MONO;
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300225 v->signal = 0xFFFF * tt_getsigstr(tt);
Douglas Landgraf1de69232007-04-22 23:15:47 -0300226 return 0;
227}
228
229static int vidioc_s_tuner(struct file *file, void *priv,
230 struct v4l2_tuner *v)
231{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300232 return v->index ? -EINVAL : 0;
Douglas Landgraf1de69232007-04-22 23:15:47 -0300233}
234
235static int vidioc_s_frequency(struct file *file, void *priv,
236 struct v4l2_frequency *f)
237{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300238 struct terratec *tt = video_drvdata(file);
Douglas Landgraf1de69232007-04-22 23:15:47 -0300239
Hans Verkuila3a9e282009-11-27 04:33:25 -0300240 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
241 return -EINVAL;
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300242 tt_setfreq(tt, f->frequency);
Douglas Landgraf1de69232007-04-22 23:15:47 -0300243 return 0;
244}
245
246static int vidioc_g_frequency(struct file *file, void *priv,
247 struct v4l2_frequency *f)
248{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300249 struct terratec *tt = video_drvdata(file);
Douglas Landgraf1de69232007-04-22 23:15:47 -0300250
Hans Verkuila3a9e282009-11-27 04:33:25 -0300251 if (f->tuner != 0)
252 return -EINVAL;
Douglas Landgraf1de69232007-04-22 23:15:47 -0300253 f->type = V4L2_TUNER_RADIO;
254 f->frequency = tt->curfreq;
255 return 0;
256}
257
258static int vidioc_queryctrl(struct file *file, void *priv,
259 struct v4l2_queryctrl *qc)
260{
261 int i;
262
263 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
264 if (qc->id && qc->id == radio_qctrl[i].id) {
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300265 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Douglas Landgraf1de69232007-04-22 23:15:47 -0300266 return 0;
267 }
268 }
269 return -EINVAL;
270}
271
272static int vidioc_g_ctrl(struct file *file, void *priv,
273 struct v4l2_control *ctrl)
274{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300275 struct terratec *tt = video_drvdata(file);
Douglas Landgraf1de69232007-04-22 23:15:47 -0300276
277 switch (ctrl->id) {
278 case V4L2_CID_AUDIO_MUTE:
279 if (tt->muted)
280 ctrl->value = 1;
281 else
282 ctrl->value = 0;
283 return 0;
284 case V4L2_CID_AUDIO_VOLUME:
285 ctrl->value = tt->curvol * 6554;
286 return 0;
287 }
288 return -EINVAL;
289}
290
291static int vidioc_s_ctrl(struct file *file, void *priv,
292 struct v4l2_control *ctrl)
293{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300294 struct terratec *tt = video_drvdata(file);
Douglas Landgraf1de69232007-04-22 23:15:47 -0300295
296 switch (ctrl->id) {
297 case V4L2_CID_AUDIO_MUTE:
298 if (ctrl->value)
299 tt_mute(tt);
300 else
301 tt_setvol(tt,tt->curvol);
302 return 0;
303 case V4L2_CID_AUDIO_VOLUME:
304 tt_setvol(tt,ctrl->value);
305 return 0;
306 }
307 return -EINVAL;
308}
309
Douglas Landgraf1de69232007-04-22 23:15:47 -0300310static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
311{
312 *i = 0;
313 return 0;
314}
315
316static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
317{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300318 return i ? -EINVAL : 0;
319}
320
321static int vidioc_g_audio(struct file *file, void *priv,
322 struct v4l2_audio *a)
323{
324 a->index = 0;
325 strlcpy(a->name, "Radio", sizeof(a->name));
326 a->capability = V4L2_AUDCAP_STEREO;
Douglas Landgraf1de69232007-04-22 23:15:47 -0300327 return 0;
328}
329
330static int vidioc_s_audio(struct file *file, void *priv,
331 struct v4l2_audio *a)
332{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300333 return a->index ? -EINVAL : 0;
334}
335
Hans Verkuilbec43662008-12-30 06:58:20 -0300336static const struct v4l2_file_operations terratec_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .owner = THIS_MODULE,
Hans Verkuil32958fd2010-11-14 09:36:23 -0300338 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339};
340
Hans Verkuila3998102008-07-21 02:57:38 -0300341static const struct v4l2_ioctl_ops terratec_ioctl_ops = {
Douglas Landgraf1de69232007-04-22 23:15:47 -0300342 .vidioc_querycap = vidioc_querycap,
343 .vidioc_g_tuner = vidioc_g_tuner,
344 .vidioc_s_tuner = vidioc_s_tuner,
345 .vidioc_g_frequency = vidioc_g_frequency,
346 .vidioc_s_frequency = vidioc_s_frequency,
347 .vidioc_queryctrl = vidioc_queryctrl,
348 .vidioc_g_ctrl = vidioc_g_ctrl,
349 .vidioc_s_ctrl = vidioc_s_ctrl,
350 .vidioc_g_audio = vidioc_g_audio,
351 .vidioc_s_audio = vidioc_s_audio,
352 .vidioc_g_input = vidioc_g_input,
353 .vidioc_s_input = vidioc_s_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
356static int __init terratec_init(void)
357{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300358 struct terratec *tt = &terratec_card;
359 struct v4l2_device *v4l2_dev = &tt->v4l2_dev;
360 int res;
361
362 strlcpy(v4l2_dev->name, "terratec", sizeof(v4l2_dev->name));
363 tt->io = io;
364 if (tt->io == -1) {
Hans Verkuilb24c20cc2009-03-09 08:11:21 -0300365 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x590 or 0x591\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return -EINVAL;
367 }
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300368 if (!request_region(tt->io, 2, "terratec")) {
369 v4l2_err(v4l2_dev, "port 0x%x already in use\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -EBUSY;
371 }
372
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300373 res = v4l2_device_register(NULL, v4l2_dev);
374 if (res < 0) {
375 release_region(tt->io, 2);
376 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
377 return res;
378 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300379
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300380 strlcpy(tt->vdev.name, v4l2_dev->name, sizeof(tt->vdev.name));
381 tt->vdev.v4l2_dev = v4l2_dev;
382 tt->vdev.fops = &terratec_fops;
383 tt->vdev.ioctl_ops = &terratec_ioctl_ops;
384 tt->vdev.release = video_device_release_empty;
385 video_set_drvdata(&tt->vdev, tt);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300386
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300387 mutex_init(&tt->lock);
388
Hans Verkuil32958fd2010-11-14 09:36:23 -0300389 /* mute card - prevents noisy bootups */
390 tt_write_vol(tt, 0);
391
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300392 if (video_register_device(&tt->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
393 v4l2_device_unregister(&tt->v4l2_dev);
394 release_region(tt->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EINVAL;
396 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300397
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300398 v4l2_info(v4l2_dev, "TERRATEC ActivRadio Standalone card driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0;
400}
401
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300402static void __exit terratec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300404 struct terratec *tt = &terratec_card;
405 struct v4l2_device *v4l2_dev = &tt->v4l2_dev;
406
407 video_unregister_device(&tt->vdev);
408 v4l2_device_unregister(&tt->v4l2_dev);
409 release_region(tt->io, 2);
410 v4l2_info(v4l2_dev, "TERRATEC ActivRadio Standalone card driver unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413module_init(terratec_init);
Hans Verkuil5ac3d5b2009-03-06 13:53:58 -0300414module_exit(terratec_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415