2 * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
3 * (C) 2001 Dimitromanolakis Apostolos <apdim@grecian.net>
5 * Based in the radio Maestro PCI driver. Actually it uses the same chip
6 * for radio but different pci controller.
8 * I didn't have any specs I reversed engineered the protocol from
9 * the windows driver (radio.dll).
11 * The card uses the TEA5757 chip that includes a search function but it
12 * is useless as I haven't found any way to read back the frequency. If
13 * anybody does please mail me.
15 * For the pdf file see:
16 * http://www.semiconductors.philips.com/pip/TEA5757H/V1
21 * - better pci interface thanks to Francois Romieu <romieu@cogenit.fr>
23 * 0.75 Sun Feb 4 22:51:27 EET 2001
25 * - removed support for multiple devices as it didn't work anyway
28 * - card unmutes if you change frequency
30 * (c) 2006, 2007 by Mauro Carvalho Chehab <mchehab@infradead.org>:
31 * - Conversion to V4L2 API
32 * - Uses video_ioctl2 for parsing and to add debug support
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/ioport.h>
39 #include <linux/delay.h>
41 #include <asm/uaccess.h>
42 #include <linux/mutex.h>
44 #include <linux/pci.h>
45 #include <linux/videodev2.h>
46 #include <media/v4l2-common.h>
48 #define DRIVER_VERSION "0.77"
50 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
51 #define RADIO_VERSION KERNEL_VERSION(0,7,7)
53 static struct video_device maxiradio_radio;
55 #define dprintk(num, fmt, arg...) \
57 if (maxiradio_radio.debug >= num) \
58 printk(KERN_DEBUG "%s: " fmt, \
59 maxiradio_radio.name, ## arg); } while (0)
61 static struct v4l2_queryctrl radio_qctrl[] = {
63 .id = V4L2_CID_AUDIO_MUTE,
68 .type = V4L2_CTRL_TYPE_BOOLEAN,
72 #ifndef PCI_VENDOR_ID_GUILLEMOT
73 #define PCI_VENDOR_ID_GUILLEMOT 0x5046
76 #ifndef PCI_DEVICE_ID_GUILLEMOT
77 #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
81 /* TEA5757 pin mappings */
82 static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
84 static int radio_nr = -1;
85 module_param(radio_nr, int, 0);
88 #define FREQ_LO 50*16000
89 #define FREQ_HI 150*16000
91 #define FREQ_IF 171200 /* 10.7*16000 */
92 #define FREQ_STEP 200 /* 12.5*16 */
94 /* (x==fmhz*16*1000) -> bits */
95 #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1)) \
98 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
101 static const struct file_operations maxiradio_fops = {
102 .owner = THIS_MODULE,
103 .open = video_exclusive_open,
104 .release = video_exclusive_release,
105 .ioctl = video_ioctl2,
106 .compat_ioctl = v4l_compat_ioctl32,
110 static struct radio_device
112 __u16 io, /* base of radio io */
113 muted, /* VIDEO_AUDIO_MUTE */
114 stereo, /* VIDEO_TUNER_STEREO_ON */
115 tuned; /* signal strength (0 or 0xffff) */
120 } radio_unit = {0, 0, 0, 0, };
123 static void outbit(unsigned long bit, __u16 io)
127 outb( power|wren|data ,io); udelay(4);
128 outb( power|wren|data|clk ,io); udelay(4);
129 outb( power|wren|data ,io); udelay(4);
133 outb( power|wren ,io); udelay(4);
134 outb( power|wren|clk ,io); udelay(4);
135 outb( power|wren ,io); udelay(4);
139 static void turn_power(__u16 io, int p)
142 dprintk(1, "Radio powered on\n");
145 dprintk(1, "Radio powered off\n");
150 static void set_freq(__u16 io, __u32 freq)
152 unsigned long int si;
154 int data = FREQ2BITS(freq);
156 /* TEA5757 shift register bits (see pdf) */
158 outbit(0,io); // 24 search
159 outbit(1,io); // 23 search up/down
161 outbit(0,io); // 22 stereo/mono
163 outbit(0,io); // 21 band
164 outbit(0,io); // 20 band (only 00=FM works I think)
166 outbit(0,io); // 19 port ?
167 outbit(0,io); // 18 port ?
169 outbit(0,io); // 17 search level
170 outbit(0,io); // 16 search level
173 for (bl = 1; bl <= 16 ; bl++) {
174 outbit(data & si,io);
178 dprintk(1, "Radio freq set to %d.%02d MHz\n",
180 freq % 16000 * 100 / 16000);
185 static int get_stereo(__u16 io)
190 return !(inb(io) & mo_st);
193 static int get_tune(__u16 io)
198 return !(inb(io) & mo_st);
202 static int vidioc_querycap (struct file *file, void *priv,
203 struct v4l2_capability *v)
205 strlcpy(v->driver, "radio-maxiradio", sizeof (v->driver));
206 strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof (v->card));
207 sprintf(v->bus_info,"ISA");
208 v->version = RADIO_VERSION;
209 v->capabilities = V4L2_CAP_TUNER;
214 static int vidioc_g_tuner (struct file *file, void *priv,
215 struct v4l2_tuner *v)
217 struct video_device *dev = video_devdata(file);
218 struct radio_device *card=dev->priv;
223 memset(v,0,sizeof(*v));
224 strcpy(v->name, "FM");
225 v->type = V4L2_TUNER_RADIO;
228 v->rangehigh=FREQ_HI;
229 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
230 v->capability=V4L2_TUNER_CAP_LOW;
231 if(get_stereo(card->io))
232 v->audmode = V4L2_TUNER_MODE_STEREO;
234 v->audmode = V4L2_TUNER_MODE_MONO;
235 v->signal=0xffff*get_tune(card->io);
240 static int vidioc_s_tuner (struct file *file, void *priv,
241 struct v4l2_tuner *v)
249 static int vidioc_g_audio (struct file *file, void *priv,
250 struct v4l2_audio *a)
255 strcpy(a->name, "FM");
256 a->capability = V4L2_AUDCAP_STEREO;
260 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
267 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
276 static int vidioc_s_audio (struct file *file, void *priv,
277 struct v4l2_audio *a)
285 static int vidioc_s_frequency (struct file *file, void *priv,
286 struct v4l2_frequency *f)
288 struct video_device *dev = video_devdata(file);
289 struct radio_device *card=dev->priv;
291 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) {
292 dprintk(1, "radio freq (%d.%02d MHz) out of range (%d-%d)\n",
293 f->frequency / 16000,
294 f->frequency % 16000 * 100 / 16000,
295 FREQ_LO / 16000, FREQ_HI / 16000);
300 card->freq = f->frequency;
301 set_freq(card->io, card->freq);
307 static int vidioc_g_frequency (struct file *file, void *priv,
308 struct v4l2_frequency *f)
310 struct video_device *dev = video_devdata(file);
311 struct radio_device *card=dev->priv;
313 f->type = V4L2_TUNER_RADIO;
314 f->frequency = card->freq;
316 dprintk(4, "radio freq is %d.%02d MHz",
317 f->frequency / 16000,
318 f->frequency % 16000 * 100 / 16000);
323 static int vidioc_queryctrl (struct file *file, void *priv,
324 struct v4l2_queryctrl *qc)
328 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
329 if (qc->id && qc->id == radio_qctrl[i].id) {
330 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
338 static int vidioc_g_ctrl (struct file *file, void *priv,
339 struct v4l2_control *ctrl)
341 struct video_device *dev = video_devdata(file);
342 struct radio_device *card=dev->priv;
345 case V4L2_CID_AUDIO_MUTE:
346 ctrl->value=card->muted;
353 static int vidioc_s_ctrl (struct file *file, void *priv,
354 struct v4l2_control *ctrl)
356 struct video_device *dev = video_devdata(file);
357 struct radio_device *card=dev->priv;
360 case V4L2_CID_AUDIO_MUTE:
361 card->muted = ctrl->value;
363 turn_power(card->io, 0);
365 set_freq(card->io, card->freq);
372 static struct video_device maxiradio_radio =
374 .owner = THIS_MODULE,
375 .name = "Maxi Radio FM2000 radio",
376 .type = VID_TYPE_TUNER,
377 .fops = &maxiradio_fops,
379 .vidioc_querycap = vidioc_querycap,
380 .vidioc_g_tuner = vidioc_g_tuner,
381 .vidioc_s_tuner = vidioc_s_tuner,
382 .vidioc_g_audio = vidioc_g_audio,
383 .vidioc_s_audio = vidioc_s_audio,
384 .vidioc_g_input = vidioc_g_input,
385 .vidioc_s_input = vidioc_s_input,
386 .vidioc_g_frequency = vidioc_g_frequency,
387 .vidioc_s_frequency = vidioc_s_frequency,
388 .vidioc_queryctrl = vidioc_queryctrl,
389 .vidioc_g_ctrl = vidioc_g_ctrl,
390 .vidioc_s_ctrl = vidioc_s_ctrl,
393 static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
395 if(!request_region(pci_resource_start(pdev, 0),
396 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
397 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
401 if (pci_enable_device(pdev))
402 goto err_out_free_region;
404 radio_unit.io = pci_resource_start(pdev, 0);
405 mutex_init(&radio_unit.lock);
406 maxiradio_radio.priv = &radio_unit;
408 if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
409 printk("radio-maxiradio: can't register device!");
410 goto err_out_free_region;
413 printk(KERN_INFO "radio-maxiradio: version "
420 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
425 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
430 static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
432 video_unregister_device(&maxiradio_radio);
433 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
436 static struct pci_device_id maxiradio_pci_tbl[] = {
437 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
438 PCI_ANY_ID, PCI_ANY_ID, },
442 MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
444 static struct pci_driver maxiradio_driver = {
445 .name = "radio-maxiradio",
446 .id_table = maxiradio_pci_tbl,
447 .probe = maxiradio_init_one,
448 .remove = __devexit_p(maxiradio_remove_one),
451 static int __init maxiradio_radio_init(void)
453 return pci_register_driver(&maxiradio_driver);
456 static void __exit maxiradio_radio_exit(void)
458 pci_unregister_driver(&maxiradio_driver);
461 module_init(maxiradio_radio_init);
462 module_exit(maxiradio_radio_exit);
464 MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
465 MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
466 MODULE_LICENSE("GPL");
468 module_param_named(debug,maxiradio_radio.debug, int, 0644);
469 MODULE_PARM_DESC(debug,"activates debug info");