V4L/DVB (3318b): sem2mutex: drivers/media/, #2
[safe/jmp/linux-2.6] / drivers / media / radio / radio-maxiradio.c
1 /* 
2  * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux 
3  * (C) 2001 Dimitromanolakis Apostolos <apdim@grecian.net>
4  *
5  * Based in the radio Maestro PCI driver. Actually it uses the same chip
6  * for radio but different pci controller.
7  *
8  * I didn't have any specs I reversed engineered the protocol from
9  * the windows driver (radio.dll). 
10  *
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.
14  *
15  * For the pdf file see:
16  * http://www.semiconductors.philips.com/pip/TEA5757H/V1
17  *
18  *
19  * CHANGES:
20  *   0.75b
21  *     - better pci interface thanks to Francois Romieu <romieu@cogenit.fr>
22  *
23  *   0.75
24  *     - tiding up
25  *     - removed support for multiple devices as it didn't work anyway
26  *
27  * BUGS: 
28  *   - card unmutes if you change frequency
29  *
30  */
31
32
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>
38 #include <asm/io.h>
39 #include <asm/uaccess.h>
40 #include <linux/mutex.h>
41
42 #include <linux/pci.h>
43 #include <linux/videodev.h>
44
45 /* version 0.75      Sun Feb  4 22:51:27 EET 2001 */
46 #define DRIVER_VERSION  "0.75"
47
48 #ifndef PCI_VENDOR_ID_GUILLEMOT
49 #define PCI_VENDOR_ID_GUILLEMOT 0x5046
50 #endif
51
52 #ifndef PCI_DEVICE_ID_GUILLEMOT
53 #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
54 #endif
55
56
57 /* TEA5757 pin mappings */
58 static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
59
60 static int radio_nr = -1;
61 module_param(radio_nr, int, 0);
62
63
64 #define FREQ_LO          50*16000
65 #define FREQ_HI         150*16000
66
67 #define FREQ_IF         171200 /* 10.7*16000   */
68 #define FREQ_STEP       200    /* 12.5*16      */
69
70 #define FREQ2BITS(x)    ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
71                         /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
72
73 #define BITS2FREQ(x)    ((x) * FREQ_STEP - FREQ_IF)
74
75
76 static int radio_ioctl(struct inode *inode, struct file *file,
77                        unsigned int cmd, unsigned long arg);
78
79 static struct file_operations maxiradio_fops = {
80         .owner          = THIS_MODULE,
81         .open           = video_exclusive_open,
82         .release        = video_exclusive_release,
83         .ioctl          = radio_ioctl,
84         .compat_ioctl   = v4l_compat_ioctl32,
85         .llseek         = no_llseek,
86 };
87 static struct video_device maxiradio_radio =
88 {
89         .owner          = THIS_MODULE,
90         .name           = "Maxi Radio FM2000 radio",
91         .type           = VID_TYPE_TUNER,
92         .hardware       = VID_HARDWARE_SF16MI,
93         .fops           = &maxiradio_fops,
94 };
95
96 static struct radio_device
97 {
98         __u16   io,     /* base of radio io */
99                 muted,  /* VIDEO_AUDIO_MUTE */
100                 stereo, /* VIDEO_TUNER_STEREO_ON */     
101                 tuned;  /* signal strength (0 or 0xffff) */
102                 
103         unsigned long freq;
104         
105         struct mutex lock;
106 } radio_unit = {0, 0, 0, 0, };
107
108
109 static void outbit(unsigned long bit, __u16 io)
110 {
111         if(bit != 0)
112                 {
113                         outb(  power|wren|data     ,io); udelay(4);
114                         outb(  power|wren|data|clk ,io); udelay(4);
115                         outb(  power|wren|data     ,io); udelay(4);
116                 }
117         else    
118                 {
119                         outb(  power|wren          ,io); udelay(4);
120                         outb(  power|wren|clk      ,io); udelay(4);
121                         outb(  power|wren          ,io); udelay(4);
122                 }
123 }
124
125 static void turn_power(__u16 io, int p)
126 {
127         if(p != 0) outb(power, io); else outb(0,io);
128 }
129
130
131 static void set_freq(__u16 io, __u32 data)
132 {
133         unsigned long int si;
134         int bl;
135         
136         /* TEA5757 shift register bits (see pdf) */
137
138         outbit(0,io); // 24  search 
139         outbit(1,io); // 23  search up/down
140         
141         outbit(0,io); // 22  stereo/mono
142
143         outbit(0,io); // 21  band
144         outbit(0,io); // 20  band (only 00=FM works I think)
145
146         outbit(0,io); // 19  port ?
147         outbit(0,io); // 18  port ?
148         
149         outbit(0,io); // 17  search level
150         outbit(0,io); // 16  search level
151  
152         si = 0x8000;
153         for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
154         
155         outb(power,io);
156 }
157
158 static int get_stereo(__u16 io)
159 {       
160         outb(power,io); udelay(4);
161         return !(inb(io) & mo_st);
162 }
163
164 static int get_tune(__u16 io)
165 {       
166         outb(power+clk,io); udelay(4);
167         return !(inb(io) & mo_st);
168 }
169
170
171 static inline int radio_function(struct inode *inode, struct file *file,
172                                  unsigned int cmd, void *arg)
173 {
174         struct video_device *dev = video_devdata(file);
175         struct radio_device *card=dev->priv;
176
177         switch(cmd) {
178                 case VIDIOCGCAP: {
179                         struct video_capability *v = arg;
180                         
181                         memset(v,0,sizeof(*v));
182                         strcpy(v->name, "Maxi Radio FM2000 radio");
183                         v->type=VID_TYPE_TUNER;
184                         v->channels=v->audios=1;
185                         return 0;
186                 }
187                 case VIDIOCGTUNER: {
188                         struct video_tuner *v = arg;
189                         
190                         if(v->tuner)
191                                 return -EINVAL;
192                                 
193                         card->stereo = 0xffff * get_stereo(card->io);
194                         card->tuned = 0xffff * get_tune(card->io);
195                         
196                         v->flags = VIDEO_TUNER_LOW | card->stereo;
197                         v->signal = card->tuned;
198                         
199                         strcpy(v->name, "FM");
200                         
201                         v->rangelow = FREQ_LO;
202                         v->rangehigh = FREQ_HI;
203                         v->mode = VIDEO_MODE_AUTO;
204                         
205                         return 0;
206                 }
207                 case VIDIOCSTUNER: {
208                         struct video_tuner *v = arg;
209                         if(v->tuner!=0)
210                                 return -EINVAL;
211                         return 0;
212                 }
213                 case VIDIOCGFREQ: {
214                         unsigned long *freq = arg;
215                         
216                         *freq = card->freq;
217                         return 0;
218                 }
219                 case VIDIOCSFREQ: {
220                         unsigned long *freq = arg;
221                         
222                         if (*freq < FREQ_LO || *freq > FREQ_HI)
223                                 return -EINVAL;
224                         card->freq = *freq;
225                         set_freq(card->io, FREQ2BITS(card->freq));
226                         msleep(125);
227                         return 0;
228                 }
229                 case VIDIOCGAUDIO: {    
230                         struct video_audio *v = arg;
231                         memset(v,0,sizeof(*v));
232                         strcpy(v->name, "Radio");
233                         v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
234                         v->mode=VIDEO_SOUND_STEREO;
235                         return 0;               
236                 }
237                 
238                 case VIDIOCSAUDIO: {
239                         struct video_audio *v = arg;
240                         
241                         if(v->audio)
242                                 return -EINVAL;
243                         card->muted = v->flags & VIDEO_AUDIO_MUTE;
244                         if(card->muted)
245                                 turn_power(card->io, 0);
246                         else
247                                 set_freq(card->io, FREQ2BITS(card->freq));
248                         return 0;
249                 }
250                 case VIDIOCGUNIT: {
251                         struct video_unit *v = arg;
252                         
253                         v->video=VIDEO_NO_UNIT;
254                         v->vbi=VIDEO_NO_UNIT;
255                         v->radio=dev->minor;
256                         v->audio=0;
257                         v->teletext=VIDEO_NO_UNIT;
258                         return 0;               
259                 }
260                 default: return -ENOIOCTLCMD;
261         }
262 }
263
264 static int radio_ioctl(struct inode *inode, struct file *file,
265                        unsigned int cmd, unsigned long arg)
266 {
267         struct video_device *dev = video_devdata(file);
268         struct radio_device *card=dev->priv;
269         int ret;
270         
271         mutex_lock(&card->lock);
272         ret = video_usercopy(inode, file, cmd, arg, radio_function);
273         mutex_unlock(&card->lock);
274         return ret;
275 }
276
277 MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
278 MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
279 MODULE_LICENSE("GPL");
280
281
282 static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
283 {
284         if(!request_region(pci_resource_start(pdev, 0),
285                            pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
286                 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
287                 goto err_out;
288         }
289
290         if (pci_enable_device(pdev))
291                 goto err_out_free_region;
292
293         radio_unit.io = pci_resource_start(pdev, 0);
294         mutex_init(&radio_unit.lock);
295         maxiradio_radio.priv = &radio_unit;
296
297         if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
298                 printk("radio-maxiradio: can't register device!");
299                 goto err_out_free_region;
300         }
301
302         printk(KERN_INFO "radio-maxiradio: version "
303                DRIVER_VERSION
304                " time "
305                __TIME__ "  "
306                __DATE__
307                "\n");
308
309         printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
310                radio_unit.io);
311         return 0;
312
313 err_out_free_region:
314         release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
315 err_out:
316         return -ENODEV;
317 }
318
319 static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
320 {
321         video_unregister_device(&maxiradio_radio);
322         release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
323 }
324
325 static struct pci_device_id maxiradio_pci_tbl[] = {
326         { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
327                 PCI_ANY_ID, PCI_ANY_ID, },
328         { 0,}
329 };
330
331 MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
332
333 static struct pci_driver maxiradio_driver = {
334         .name           = "radio-maxiradio",
335         .id_table       = maxiradio_pci_tbl,
336         .probe          = maxiradio_init_one,
337         .remove         = __devexit_p(maxiradio_remove_one),
338 };
339
340 static int __init maxiradio_radio_init(void)
341 {
342         return pci_register_driver(&maxiradio_driver);
343 }
344
345 static void __exit maxiradio_radio_exit(void)
346 {
347         pci_unregister_driver(&maxiradio_driver);
348 }
349
350 module_init(maxiradio_radio_init);
351 module_exit(maxiradio_radio_exit);