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>
47 #include <media/v4l2-ioctl.h>
49 #define DRIVER_VERSION "0.77"
51 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
52 #define RADIO_VERSION KERNEL_VERSION(0,7,7)
54 static struct video_device maxiradio_radio;
56 #define dprintk(num, fmt, arg...) \
58 if (maxiradio_radio.debug >= num) \
59 printk(KERN_DEBUG "%s: " fmt, \
60 maxiradio_radio.name, ## arg); } while (0)
62 static struct v4l2_queryctrl radio_qctrl[] = {
64 .id = V4L2_CID_AUDIO_MUTE,
69 .type = V4L2_CTRL_TYPE_BOOLEAN,
73 #ifndef PCI_VENDOR_ID_GUILLEMOT
74 #define PCI_VENDOR_ID_GUILLEMOT 0x5046
77 #ifndef PCI_DEVICE_ID_GUILLEMOT
78 #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
82 /* TEA5757 pin mappings */
83 static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
85 static int radio_nr = -1;
86 module_param(radio_nr, int, 0);
89 #define FREQ_LO 50*16000
90 #define FREQ_HI 150*16000
92 #define FREQ_IF 171200 /* 10.7*16000 */
93 #define FREQ_STEP 200 /* 12.5*16 */
95 /* (x==fmhz*16*1000) -> bits */
96 #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1)) \
99 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
102 static const struct file_operations maxiradio_fops = {
103 .owner = THIS_MODULE,
104 .open = video_exclusive_open,
105 .release = video_exclusive_release,
106 .ioctl = video_ioctl2,
108 .compat_ioctl = v4l_compat_ioctl32,
113 static struct radio_device
115 __u16 io, /* base of radio io */
116 muted, /* VIDEO_AUDIO_MUTE */
117 stereo, /* VIDEO_TUNER_STEREO_ON */
118 tuned; /* signal strength (0 or 0xffff) */
128 static void outbit(unsigned long bit, __u16 io)
132 outb( power|wren|data ,io); udelay(4);
133 outb( power|wren|data|clk ,io); udelay(4);
134 outb( power|wren|data ,io); udelay(4);
138 outb( power|wren ,io); udelay(4);
139 outb( power|wren|clk ,io); udelay(4);
140 outb( power|wren ,io); udelay(4);
144 static void turn_power(__u16 io, int p)
147 dprintk(1, "Radio powered on\n");
150 dprintk(1, "Radio powered off\n");
155 static void set_freq(__u16 io, __u32 freq)
157 unsigned long int si;
159 int data = FREQ2BITS(freq);
161 /* TEA5757 shift register bits (see pdf) */
163 outbit(0,io); // 24 search
164 outbit(1,io); // 23 search up/down
166 outbit(0,io); // 22 stereo/mono
168 outbit(0,io); // 21 band
169 outbit(0,io); // 20 band (only 00=FM works I think)
171 outbit(0,io); // 19 port ?
172 outbit(0,io); // 18 port ?
174 outbit(0,io); // 17 search level
175 outbit(0,io); // 16 search level
178 for (bl = 1; bl <= 16 ; bl++) {
179 outbit(data & si,io);
183 dprintk(1, "Radio freq set to %d.%02d MHz\n",
185 freq % 16000 * 100 / 16000);
190 static int get_stereo(__u16 io)
195 return !(inb(io) & mo_st);
198 static int get_tune(__u16 io)
203 return !(inb(io) & mo_st);
207 static int vidioc_querycap (struct file *file, void *priv,
208 struct v4l2_capability *v)
210 strlcpy(v->driver, "radio-maxiradio", sizeof (v->driver));
211 strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof (v->card));
212 sprintf(v->bus_info,"ISA");
213 v->version = RADIO_VERSION;
214 v->capabilities = V4L2_CAP_TUNER;
219 static int vidioc_g_tuner (struct file *file, void *priv,
220 struct v4l2_tuner *v)
222 struct video_device *dev = video_devdata(file);
223 struct radio_device *card=dev->priv;
228 memset(v,0,sizeof(*v));
229 strcpy(v->name, "FM");
230 v->type = V4L2_TUNER_RADIO;
233 v->rangehigh=FREQ_HI;
234 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
235 v->capability=V4L2_TUNER_CAP_LOW;
236 if(get_stereo(card->io))
237 v->audmode = V4L2_TUNER_MODE_STEREO;
239 v->audmode = V4L2_TUNER_MODE_MONO;
240 v->signal=0xffff*get_tune(card->io);
245 static int vidioc_s_tuner (struct file *file, void *priv,
246 struct v4l2_tuner *v)
254 static int vidioc_g_audio (struct file *file, void *priv,
255 struct v4l2_audio *a)
260 strcpy(a->name, "FM");
261 a->capability = V4L2_AUDCAP_STEREO;
265 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
272 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
281 static int vidioc_s_audio (struct file *file, void *priv,
282 struct v4l2_audio *a)
290 static int vidioc_s_frequency (struct file *file, void *priv,
291 struct v4l2_frequency *f)
293 struct video_device *dev = video_devdata(file);
294 struct radio_device *card=dev->priv;
296 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) {
297 dprintk(1, "radio freq (%d.%02d MHz) out of range (%d-%d)\n",
298 f->frequency / 16000,
299 f->frequency % 16000 * 100 / 16000,
300 FREQ_LO / 16000, FREQ_HI / 16000);
305 card->freq = f->frequency;
306 set_freq(card->io, card->freq);
312 static int vidioc_g_frequency (struct file *file, void *priv,
313 struct v4l2_frequency *f)
315 struct video_device *dev = video_devdata(file);
316 struct radio_device *card=dev->priv;
318 f->type = V4L2_TUNER_RADIO;
319 f->frequency = card->freq;
321 dprintk(4, "radio freq is %d.%02d MHz",
322 f->frequency / 16000,
323 f->frequency % 16000 * 100 / 16000);
328 static int vidioc_queryctrl (struct file *file, void *priv,
329 struct v4l2_queryctrl *qc)
333 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
334 if (qc->id && qc->id == radio_qctrl[i].id) {
335 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
343 static int vidioc_g_ctrl (struct file *file, void *priv,
344 struct v4l2_control *ctrl)
346 struct video_device *dev = video_devdata(file);
347 struct radio_device *card=dev->priv;
350 case V4L2_CID_AUDIO_MUTE:
351 ctrl->value=card->muted;
358 static int vidioc_s_ctrl (struct file *file, void *priv,
359 struct v4l2_control *ctrl)
361 struct video_device *dev = video_devdata(file);
362 struct radio_device *card=dev->priv;
365 case V4L2_CID_AUDIO_MUTE:
366 card->muted = ctrl->value;
368 turn_power(card->io, 0);
370 set_freq(card->io, card->freq);
377 static const struct v4l2_ioctl_ops maxiradio_ioctl_ops = {
378 .vidioc_querycap = vidioc_querycap,
379 .vidioc_g_tuner = vidioc_g_tuner,
380 .vidioc_s_tuner = vidioc_s_tuner,
381 .vidioc_g_audio = vidioc_g_audio,
382 .vidioc_s_audio = vidioc_s_audio,
383 .vidioc_g_input = vidioc_g_input,
384 .vidioc_s_input = vidioc_s_input,
385 .vidioc_g_frequency = vidioc_g_frequency,
386 .vidioc_s_frequency = vidioc_s_frequency,
387 .vidioc_queryctrl = vidioc_queryctrl,
388 .vidioc_g_ctrl = vidioc_g_ctrl,
389 .vidioc_s_ctrl = vidioc_s_ctrl,
392 static struct video_device maxiradio_radio = {
393 .name = "Maxi Radio FM2000 radio",
394 .fops = &maxiradio_fops,
395 .ioctl_ops = &maxiradio_ioctl_ops,
398 static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
400 if(!request_region(pci_resource_start(pdev, 0),
401 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
402 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
406 if (pci_enable_device(pdev))
407 goto err_out_free_region;
409 radio_unit.io = pci_resource_start(pdev, 0);
410 mutex_init(&radio_unit.lock);
411 maxiradio_radio.priv = &radio_unit;
413 if (video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
414 printk("radio-maxiradio: can't register device!");
415 goto err_out_free_region;
418 printk(KERN_INFO "radio-maxiradio: version "
425 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
430 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
435 static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
437 video_unregister_device(&maxiradio_radio);
438 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
441 static struct pci_device_id maxiradio_pci_tbl[] = {
442 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
443 PCI_ANY_ID, PCI_ANY_ID, },
447 MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
449 static struct pci_driver maxiradio_driver = {
450 .name = "radio-maxiradio",
451 .id_table = maxiradio_pci_tbl,
452 .probe = maxiradio_init_one,
453 .remove = __devexit_p(maxiradio_remove_one),
456 static int __init maxiradio_radio_init(void)
458 return pci_register_driver(&maxiradio_driver);
461 static void __exit maxiradio_radio_exit(void)
463 pci_unregister_driver(&maxiradio_driver);
466 module_init(maxiradio_radio_init);
467 module_exit(maxiradio_radio_exit);
469 MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
470 MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
471 MODULE_LICENSE("GPL");
473 module_param_named(debug,maxiradio_radio.debug, int, 0644);
474 MODULE_PARM_DESC(debug,"activates debug info");