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>
25 * - removed support for multiple devices as it didn't work anyway
28 * - card unmutes if you change frequency
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/ioport.h>
36 #include <linux/delay.h>
37 #include <linux/sched.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include <linux/pci.h>
42 #include <linux/videodev.h>
44 /* version 0.75 Sun Feb 4 22:51:27 EET 2001 */
45 #define DRIVER_VERSION "0.75"
47 #ifndef PCI_VENDOR_ID_GUILLEMOT
48 #define PCI_VENDOR_ID_GUILLEMOT 0x5046
51 #ifndef PCI_DEVICE_ID_GUILLEMOT
52 #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
56 /* TEA5757 pin mappings */
57 static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
59 static int radio_nr = -1;
60 MODULE_PARM(radio_nr, "i");
63 #define FREQ_LO 50*16000
64 #define FREQ_HI 150*16000
66 #define FREQ_IF 171200 /* 10.7*16000 */
67 #define FREQ_STEP 200 /* 12.5*16 */
69 #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
70 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
72 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
75 static int radio_ioctl(struct inode *inode, struct file *file,
76 unsigned int cmd, unsigned long arg);
78 static struct file_operations maxiradio_fops = {
80 .open = video_exclusive_open,
81 .release = video_exclusive_release,
85 static struct video_device maxiradio_radio =
88 .name = "Maxi Radio FM2000 radio",
89 .type = VID_TYPE_TUNER,
90 .hardware = VID_HARDWARE_SF16MI,
91 .fops = &maxiradio_fops,
94 static struct radio_device
96 __u16 io, /* base of radio io */
97 muted, /* VIDEO_AUDIO_MUTE */
98 stereo, /* VIDEO_TUNER_STEREO_ON */
99 tuned; /* signal strength (0 or 0xffff) */
103 struct semaphore lock;
104 } radio_unit = {0, 0, 0, 0, };
107 static void sleep_125ms(void)
109 current->state = TASK_INTERRUPTIBLE;
110 schedule_timeout(HZ >> 3);
114 static void outbit(unsigned long bit, __u16 io)
118 outb( power|wren|data ,io); udelay(4);
119 outb( power|wren|data|clk ,io); udelay(4);
120 outb( power|wren|data ,io); udelay(4);
124 outb( power|wren ,io); udelay(4);
125 outb( power|wren|clk ,io); udelay(4);
126 outb( power|wren ,io); udelay(4);
130 static void turn_power(__u16 io, int p)
132 if(p != 0) outb(power, io); else outb(0,io);
136 static void set_freq(__u16 io, __u32 data)
138 unsigned long int si;
141 /* TEA5757 shift register bits (see pdf) */
143 outbit(0,io); // 24 search
144 outbit(1,io); // 23 search up/down
146 outbit(0,io); // 22 stereo/mono
148 outbit(0,io); // 21 band
149 outbit(0,io); // 20 band (only 00=FM works I think)
151 outbit(0,io); // 19 port ?
152 outbit(0,io); // 18 port ?
154 outbit(0,io); // 17 search level
155 outbit(0,io); // 16 search level
158 for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
163 static int get_stereo(__u16 io)
165 outb(power,io); udelay(4);
166 return !(inb(io) & mo_st);
169 static int get_tune(__u16 io)
171 outb(power+clk,io); udelay(4);
172 return !(inb(io) & mo_st);
176 inline static int radio_function(struct inode *inode, struct file *file,
177 unsigned int cmd, void *arg)
179 struct video_device *dev = video_devdata(file);
180 struct radio_device *card=dev->priv;
184 struct video_capability *v = arg;
186 memset(v,0,sizeof(*v));
187 strcpy(v->name, "Maxi Radio FM2000 radio");
188 v->type=VID_TYPE_TUNER;
189 v->channels=v->audios=1;
193 struct video_tuner *v = arg;
198 card->stereo = 0xffff * get_stereo(card->io);
199 card->tuned = 0xffff * get_tune(card->io);
201 v->flags = VIDEO_TUNER_LOW | card->stereo;
202 v->signal = card->tuned;
204 strcpy(v->name, "FM");
206 v->rangelow = FREQ_LO;
207 v->rangehigh = FREQ_HI;
208 v->mode = VIDEO_MODE_AUTO;
213 struct video_tuner *v = arg;
219 unsigned long *freq = arg;
225 unsigned long *freq = arg;
227 if (*freq < FREQ_LO || *freq > FREQ_HI)
230 set_freq(card->io, FREQ2BITS(card->freq));
235 struct video_audio *v = arg;
236 memset(v,0,sizeof(*v));
237 strcpy(v->name, "Radio");
238 v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
239 v->mode=VIDEO_SOUND_STEREO;
244 struct video_audio *v = arg;
248 card->muted = v->flags & VIDEO_AUDIO_MUTE;
250 turn_power(card->io, 0);
252 set_freq(card->io, FREQ2BITS(card->freq));
256 struct video_unit *v = arg;
258 v->video=VIDEO_NO_UNIT;
259 v->vbi=VIDEO_NO_UNIT;
262 v->teletext=VIDEO_NO_UNIT;
265 default: return -ENOIOCTLCMD;
269 static int radio_ioctl(struct inode *inode, struct file *file,
270 unsigned int cmd, unsigned long arg)
272 struct video_device *dev = video_devdata(file);
273 struct radio_device *card=dev->priv;
277 ret = video_usercopy(inode, file, cmd, arg, radio_function);
282 MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
283 MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
284 MODULE_LICENSE("GPL");
287 static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
289 if(!request_region(pci_resource_start(pdev, 0),
290 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
291 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
295 if (pci_enable_device(pdev))
296 goto err_out_free_region;
298 radio_unit.io = pci_resource_start(pdev, 0);
299 init_MUTEX(&radio_unit.lock);
300 maxiradio_radio.priv = &radio_unit;
302 if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
303 printk("radio-maxiradio: can't register device!");
304 goto err_out_free_region;
307 printk(KERN_INFO "radio-maxiradio: version "
314 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
319 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
324 static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
326 video_unregister_device(&maxiradio_radio);
327 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
330 static struct pci_device_id maxiradio_pci_tbl[] = {
331 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
332 PCI_ANY_ID, PCI_ANY_ID, },
336 MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
338 static struct pci_driver maxiradio_driver = {
339 .name = "radio-maxiradio",
340 .id_table = maxiradio_pci_tbl,
341 .probe = maxiradio_init_one,
342 .remove = __devexit_p(maxiradio_remove_one),
345 int __init maxiradio_radio_init(void)
347 return pci_module_init(&maxiradio_driver);
350 void __exit maxiradio_radio_exit(void)
352 pci_unregister_driver(&maxiradio_driver);
355 module_init(maxiradio_radio_init);
356 module_exit(maxiradio_radio_exit);