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.nxp.com/acrobat_download2/expired_datasheets/TEA5757_5759_3.pdf
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>
40 #include <linux/mutex.h>
41 #include <linux/pci.h>
42 #include <linux/videodev2.h>
44 #include <linux/slab.h>
45 #include <media/v4l2-device.h>
46 #include <media/v4l2-ioctl.h>
48 #define DRIVER_VERSION "0.7.8"
51 MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
52 MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
53 MODULE_LICENSE("GPL");
54 MODULE_VERSION(DRIVER_VERSION);
56 static int radio_nr = -1;
57 module_param(radio_nr, int, 0);
61 module_param(debug, int, 0644);
62 MODULE_PARM_DESC(debug, "activates debug info");
64 #define dprintk(dev, num, fmt, arg...) \
65 v4l2_dbg(num, debug, &dev->v4l2_dev, fmt, ## arg)
67 #ifndef PCI_VENDOR_ID_GUILLEMOT
68 #define PCI_VENDOR_ID_GUILLEMOT 0x5046
71 #ifndef PCI_DEVICE_ID_GUILLEMOT
72 #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
76 /* TEA5757 pin mappings */
77 static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16;
79 #define FREQ_LO (87 * 16000)
80 #define FREQ_HI (108 * 16000)
82 #define FREQ_IF 171200 /* 10.7*16000 */
83 #define FREQ_STEP 200 /* 12.5*16 */
85 /* (x==fmhz*16*1000) -> bits */
86 #define FREQ2BITS(x) \
87 ((((unsigned int)(x) + FREQ_IF + (FREQ_STEP << 1)) / (FREQ_STEP << 2)) << 2)
89 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
94 struct v4l2_device v4l2_dev;
95 struct video_device vdev;
98 u16 io; /* base of radio io */
99 u16 muted; /* VIDEO_AUDIO_MUTE */
100 u16 stereo; /* VIDEO_TUNER_STEREO_ON */
101 u16 tuned; /* signal strength (0 or 0xffff) */
108 static inline struct maxiradio *to_maxiradio(struct v4l2_device *v4l2_dev)
110 return container_of(v4l2_dev, struct maxiradio, v4l2_dev);
113 static void outbit(unsigned long bit, u16 io)
115 int val = power | wren | (bit ? data : 0);
125 static void turn_power(struct maxiradio *dev, int p)
128 dprintk(dev, 1, "Radio powered on\n");
129 outb(power, dev->io);
131 dprintk(dev, 1, "Radio powered off\n");
136 static void set_freq(struct maxiradio *dev, u32 freq)
138 unsigned long int si;
141 int val = FREQ2BITS(freq);
143 /* TEA5757 shift register bits (see pdf) */
145 outbit(0, io); /* 24 search */
146 outbit(1, io); /* 23 search up/down */
148 outbit(0, io); /* 22 stereo/mono */
150 outbit(0, io); /* 21 band */
151 outbit(0, io); /* 20 band (only 00=FM works I think) */
153 outbit(0, io); /* 19 port ? */
154 outbit(0, io); /* 18 port ? */
156 outbit(0, io); /* 17 search level */
157 outbit(0, io); /* 16 search level */
160 for (bl = 1; bl <= 16; bl++) {
161 outbit(val & si, io);
165 dprintk(dev, 1, "Radio freq set to %d.%02d MHz\n",
167 freq % 16000 * 100 / 16000);
172 static int get_stereo(u16 io)
177 return !(inb(io) & mo_st);
180 static int get_tune(u16 io)
185 return !(inb(io) & mo_st);
189 static int vidioc_querycap(struct file *file, void *priv,
190 struct v4l2_capability *v)
192 struct maxiradio *dev = video_drvdata(file);
194 strlcpy(v->driver, "radio-maxiradio", sizeof(v->driver));
195 strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof(v->card));
196 snprintf(v->bus_info, sizeof(v->bus_info), "PCI:%s", pci_name(dev->pdev));
197 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
201 static int vidioc_g_tuner(struct file *file, void *priv,
202 struct v4l2_tuner *v)
204 struct maxiradio *dev = video_drvdata(file);
209 mutex_lock(&dev->lock);
210 strlcpy(v->name, "FM", sizeof(v->name));
211 v->type = V4L2_TUNER_RADIO;
212 v->rangelow = FREQ_LO;
213 v->rangehigh = FREQ_HI;
214 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
215 v->capability = V4L2_TUNER_CAP_LOW;
216 if (get_stereo(dev->io))
217 v->audmode = V4L2_TUNER_MODE_STEREO;
219 v->audmode = V4L2_TUNER_MODE_MONO;
220 v->signal = 0xffff * get_tune(dev->io);
221 mutex_unlock(&dev->lock);
226 static int vidioc_s_tuner(struct file *file, void *priv,
227 struct v4l2_tuner *v)
229 return v->index ? -EINVAL : 0;
232 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
238 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
240 return i ? -EINVAL : 0;
243 static int vidioc_g_audio(struct file *file, void *priv,
244 struct v4l2_audio *a)
247 strlcpy(a->name, "Radio", sizeof(a->name));
248 a->capability = V4L2_AUDCAP_STEREO;
253 static int vidioc_s_audio(struct file *file, void *priv,
254 struct v4l2_audio *a)
256 return a->index ? -EINVAL : 0;
259 static int vidioc_s_frequency(struct file *file, void *priv,
260 struct v4l2_frequency *f)
262 struct maxiradio *dev = video_drvdata(file);
264 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
266 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) {
267 dprintk(dev, 1, "radio freq (%d.%02d MHz) out of range (%d-%d)\n",
268 f->frequency / 16000,
269 f->frequency % 16000 * 100 / 16000,
270 FREQ_LO / 16000, FREQ_HI / 16000);
275 mutex_lock(&dev->lock);
276 dev->freq = f->frequency;
277 set_freq(dev, dev->freq);
279 mutex_unlock(&dev->lock);
284 static int vidioc_g_frequency(struct file *file, void *priv,
285 struct v4l2_frequency *f)
287 struct maxiradio *dev = video_drvdata(file);
291 f->type = V4L2_TUNER_RADIO;
292 f->frequency = dev->freq;
294 dprintk(dev, 4, "radio freq is %d.%02d MHz",
295 f->frequency / 16000,
296 f->frequency % 16000 * 100 / 16000);
301 static int vidioc_queryctrl(struct file *file, void *priv,
302 struct v4l2_queryctrl *qc)
305 case V4L2_CID_AUDIO_MUTE:
306 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
311 static int vidioc_g_ctrl(struct file *file, void *priv,
312 struct v4l2_control *ctrl)
314 struct maxiradio *dev = video_drvdata(file);
317 case V4L2_CID_AUDIO_MUTE:
318 ctrl->value = dev->muted;
325 static int vidioc_s_ctrl(struct file *file, void *priv,
326 struct v4l2_control *ctrl)
328 struct maxiradio *dev = video_drvdata(file);
331 case V4L2_CID_AUDIO_MUTE:
332 mutex_lock(&dev->lock);
333 dev->muted = ctrl->value;
337 set_freq(dev, dev->freq);
338 mutex_unlock(&dev->lock);
345 static const struct v4l2_file_operations maxiradio_fops = {
346 .owner = THIS_MODULE,
347 .unlocked_ioctl = video_ioctl2,
350 static const struct v4l2_ioctl_ops maxiradio_ioctl_ops = {
351 .vidioc_querycap = vidioc_querycap,
352 .vidioc_g_tuner = vidioc_g_tuner,
353 .vidioc_s_tuner = vidioc_s_tuner,
354 .vidioc_g_audio = vidioc_g_audio,
355 .vidioc_s_audio = vidioc_s_audio,
356 .vidioc_g_input = vidioc_g_input,
357 .vidioc_s_input = vidioc_s_input,
358 .vidioc_g_frequency = vidioc_g_frequency,
359 .vidioc_s_frequency = vidioc_s_frequency,
360 .vidioc_queryctrl = vidioc_queryctrl,
361 .vidioc_g_ctrl = vidioc_g_ctrl,
362 .vidioc_s_ctrl = vidioc_s_ctrl,
365 static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
367 struct maxiradio *dev;
368 struct v4l2_device *v4l2_dev;
369 int retval = -ENOMEM;
371 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
373 dev_err(&pdev->dev, "not enough memory\n");
377 v4l2_dev = &dev->v4l2_dev;
378 mutex_init(&dev->lock);
383 strlcpy(v4l2_dev->name, "maxiradio", sizeof(v4l2_dev->name));
385 retval = v4l2_device_register(&pdev->dev, v4l2_dev);
387 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
391 if (!request_region(pci_resource_start(pdev, 0),
392 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
393 v4l2_err(v4l2_dev, "can't reserve I/O ports\n");
397 if (pci_enable_device(pdev))
398 goto err_out_free_region;
400 dev->io = pci_resource_start(pdev, 0);
401 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
402 dev->vdev.v4l2_dev = v4l2_dev;
403 dev->vdev.fops = &maxiradio_fops;
404 dev->vdev.ioctl_ops = &maxiradio_ioctl_ops;
405 dev->vdev.release = video_device_release_empty;
406 video_set_drvdata(&dev->vdev, dev);
408 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
409 v4l2_err(v4l2_dev, "can't register device!");
410 goto err_out_free_region;
413 v4l2_info(v4l2_dev, "version " DRIVER_VERSION "\n");
415 v4l2_info(v4l2_dev, "found Guillemot MAXI Radio device (io = 0x%x)\n",
420 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
422 v4l2_device_unregister(v4l2_dev);
428 static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
430 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
431 struct maxiradio *dev = to_maxiradio(v4l2_dev);
433 video_unregister_device(&dev->vdev);
434 v4l2_device_unregister(&dev->v4l2_dev);
435 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
438 static struct pci_device_id maxiradio_pci_tbl[] = {
439 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
440 PCI_ANY_ID, PCI_ANY_ID, },
444 MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
446 static struct pci_driver maxiradio_driver = {
447 .name = "radio-maxiradio",
448 .id_table = maxiradio_pci_tbl,
449 .probe = maxiradio_init_one,
450 .remove = __devexit_p(maxiradio_remove_one),
453 static int __init maxiradio_radio_init(void)
455 return pci_register_driver(&maxiradio_driver);
458 static void __exit maxiradio_radio_exit(void)
460 pci_unregister_driver(&maxiradio_driver);
463 module_init(maxiradio_radio_init);
464 module_exit(maxiradio_radio_exit);