V4L/DVB (5154): Add some debug info, depending on debug level
[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      Sun Feb  4 22:51:27 EET 2001
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  * (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
33  */
34
35
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/ioport.h>
39 #include <linux/delay.h>
40 #include <asm/io.h>
41 #include <asm/uaccess.h>
42 #include <linux/mutex.h>
43
44 #include <linux/pci.h>
45 #include <linux/videodev2.h>
46 #include <media/v4l2-common.h>
47
48 #define DRIVER_VERSION  "0.77"
49
50 #include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
51 #define RADIO_VERSION KERNEL_VERSION(0,7,7)
52
53 static struct video_device maxiradio_radio;
54
55 #define dprintk(num, fmt, arg...)                                          \
56         do {                                                               \
57                 if (maxiradio_radio.debug >= num)                          \
58                         printk(KERN_DEBUG "%s: " fmt,                      \
59                                 maxiradio_radio.name, ## arg); } while (0)
60
61 static struct v4l2_queryctrl radio_qctrl[] = {
62         {
63                 .id            = V4L2_CID_AUDIO_MUTE,
64                 .name          = "Mute",
65                 .minimum       = 0,
66                 .maximum       = 1,
67                 .default_value = 1,
68                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
69         }
70 };
71
72 #ifndef PCI_VENDOR_ID_GUILLEMOT
73 #define PCI_VENDOR_ID_GUILLEMOT 0x5046
74 #endif
75
76 #ifndef PCI_DEVICE_ID_GUILLEMOT
77 #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
78 #endif
79
80
81 /* TEA5757 pin mappings */
82 static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
83
84 static int radio_nr = -1;
85 module_param(radio_nr, int, 0);
86
87
88 #define FREQ_LO          50*16000
89 #define FREQ_HI         150*16000
90
91 #define FREQ_IF         171200 /* 10.7*16000   */
92 #define FREQ_STEP       200    /* 12.5*16      */
93
94 /* (x==fmhz*16*1000) -> bits */
95 #define FREQ2BITS(x)    ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1)) \
96                         /(FREQ_STEP<<2))<<2)
97
98 #define BITS2FREQ(x)    ((x) * FREQ_STEP - FREQ_IF)
99
100
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,
107         .llseek         = no_llseek,
108 };
109
110 static struct radio_device
111 {
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) */
116
117         unsigned long freq;
118
119         struct mutex lock;
120 } radio_unit = {0, 0, 0, 0, };
121
122
123 static void outbit(unsigned long bit, __u16 io)
124 {
125         if (bit != 0)
126                 {
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);
130                 }
131         else
132                 {
133                         outb(  power|wren          ,io); udelay(4);
134                         outb(  power|wren|clk      ,io); udelay(4);
135                         outb(  power|wren          ,io); udelay(4);
136                 }
137 }
138
139 static void turn_power(__u16 io, int p)
140 {
141         if (p != 0) {
142                 dprintk(1, "Radio powered on\n");
143                 outb(power, io);
144         } else {
145                 dprintk(1, "Radio powered off\n");
146                 outb(0,io);
147         }
148 }
149
150 static void set_freq(__u16 io, __u32 freq)
151 {
152         unsigned long int si;
153         int bl;
154         int data = FREQ2BITS(freq);
155
156         /* TEA5757 shift register bits (see pdf) */
157
158         outbit(0,io); // 24  search
159         outbit(1,io); // 23  search up/down
160
161         outbit(0,io); // 22  stereo/mono
162
163         outbit(0,io); // 21  band
164         outbit(0,io); // 20  band (only 00=FM works I think)
165
166         outbit(0,io); // 19  port ?
167         outbit(0,io); // 18  port ?
168
169         outbit(0,io); // 17  search level
170         outbit(0,io); // 16  search level
171
172         si = 0x8000;
173         for (bl = 1; bl <= 16 ; bl++) {
174                 outbit(data & si,io);
175                 si >>=1;
176         }
177
178         dprintk(1, "Radio freq set to %d.%02d MHz\n",
179                                 freq / 16000,
180                                 freq % 16000 * 100 / 16000);
181
182         turn_power(io, 1);
183 }
184
185 static int get_stereo(__u16 io)
186 {
187         outb(power,io);
188         udelay(4);
189
190         return !(inb(io) & mo_st);
191 }
192
193 static int get_tune(__u16 io)
194 {
195         outb(power+clk,io);
196         udelay(4);
197
198         return !(inb(io) & mo_st);
199 }
200
201
202 static int vidioc_querycap (struct file *file, void  *priv,
203                             struct v4l2_capability *v)
204 {
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;
210
211         return 0;
212 }
213
214 static int vidioc_g_tuner (struct file *file, void *priv,
215                            struct v4l2_tuner *v)
216 {
217         struct video_device *dev = video_devdata(file);
218         struct radio_device *card=dev->priv;
219
220         if (v->index > 0)
221                 return -EINVAL;
222
223         memset(v,0,sizeof(*v));
224         strcpy(v->name, "FM");
225         v->type = V4L2_TUNER_RADIO;
226
227         v->rangelow=FREQ_LO;
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;
233         else
234                 v->audmode = V4L2_TUNER_MODE_MONO;
235         v->signal=0xffff*get_tune(card->io);
236
237         return 0;
238 }
239
240 static int vidioc_s_tuner (struct file *file, void *priv,
241                            struct v4l2_tuner *v)
242 {
243         if (v->index > 0)
244                 return -EINVAL;
245
246         return 0;
247 }
248
249 static int vidioc_g_audio (struct file *file, void *priv,
250                            struct v4l2_audio *a)
251 {
252         if (a->index > 1)
253                 return -EINVAL;
254
255         strcpy(a->name, "FM");
256         a->capability = V4L2_AUDCAP_STEREO;
257         return 0;
258 }
259
260 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
261 {
262         *i = 0;
263
264         return 0;
265 }
266
267 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
268 {
269         if (i != 0)
270                 return -EINVAL;
271
272         return 0;
273 }
274
275
276 static int vidioc_s_audio (struct file *file, void *priv,
277                            struct v4l2_audio *a)
278 {
279         if (a->index != 0)
280                 return -EINVAL;
281
282         return 0;
283 }
284
285 static int vidioc_s_frequency (struct file *file, void *priv,
286                                struct v4l2_frequency *f)
287 {
288         struct video_device *dev = video_devdata(file);
289         struct radio_device *card=dev->priv;
290
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);
296
297                 return -EINVAL;
298         }
299
300         card->freq = f->frequency;
301         set_freq(card->io, card->freq);
302         msleep(125);
303
304         return 0;
305 }
306
307 static int vidioc_g_frequency (struct file *file, void *priv,
308                                struct v4l2_frequency *f)
309 {
310         struct video_device *dev = video_devdata(file);
311         struct radio_device *card=dev->priv;
312
313         f->type = V4L2_TUNER_RADIO;
314         f->frequency = card->freq;
315
316         dprintk(4, "radio freq is %d.%02d MHz",
317                                 f->frequency / 16000,
318                                 f->frequency % 16000 * 100 / 16000);
319
320         return 0;
321 }
322
323 static int vidioc_queryctrl (struct file *file, void *priv,
324                              struct v4l2_queryctrl *qc)
325 {
326         int i;
327
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));
331                         return (0);
332                 }
333         }
334
335         return -EINVAL;
336 }
337
338 static int vidioc_g_ctrl (struct file *file, void *priv,
339                             struct v4l2_control *ctrl)
340 {
341         struct video_device *dev = video_devdata(file);
342         struct radio_device *card=dev->priv;
343
344         switch (ctrl->id) {
345                 case V4L2_CID_AUDIO_MUTE:
346                         ctrl->value=card->muted;
347                         return (0);
348         }
349
350         return -EINVAL;
351 }
352
353 static int vidioc_s_ctrl (struct file *file, void *priv,
354                           struct v4l2_control *ctrl)
355 {
356         struct video_device *dev = video_devdata(file);
357         struct radio_device *card=dev->priv;
358
359         switch (ctrl->id) {
360                 case V4L2_CID_AUDIO_MUTE:
361                         card->muted = ctrl->value;
362                         if(card->muted)
363                                 turn_power(card->io, 0);
364                         else
365                                 set_freq(card->io, card->freq);
366                         return 0;
367         }
368
369         return -EINVAL;
370 }
371
372 static struct video_device maxiradio_radio =
373 {
374         .owner              = THIS_MODULE,
375         .name               = "Maxi Radio FM2000 radio",
376         .type               = VID_TYPE_TUNER,
377         .fops               = &maxiradio_fops,
378
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,
391 };
392
393 static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
394 {
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");
398                 goto err_out;
399         }
400
401         if (pci_enable_device(pdev))
402                 goto err_out_free_region;
403
404         radio_unit.io = pci_resource_start(pdev, 0);
405         mutex_init(&radio_unit.lock);
406         maxiradio_radio.priv = &radio_unit;
407
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;
411         }
412
413         printk(KERN_INFO "radio-maxiradio: version "
414                DRIVER_VERSION
415                " time "
416                __TIME__ "  "
417                __DATE__
418                "\n");
419
420         printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
421                radio_unit.io);
422         return 0;
423
424 err_out_free_region:
425         release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
426 err_out:
427         return -ENODEV;
428 }
429
430 static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
431 {
432         video_unregister_device(&maxiradio_radio);
433         release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
434 }
435
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, },
439         { 0,}
440 };
441
442 MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
443
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),
449 };
450
451 static int __init maxiradio_radio_init(void)
452 {
453         return pci_register_driver(&maxiradio_driver);
454 }
455
456 static void __exit maxiradio_radio_exit(void)
457 {
458         pci_unregister_driver(&maxiradio_driver);
459 }
460
461 module_init(maxiradio_radio_init);
462 module_exit(maxiradio_radio_exit);
463
464 MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
465 MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
466 MODULE_LICENSE("GPL");
467
468 module_param_named(debug,maxiradio_radio.debug, int, 0644);
469 MODULE_PARM_DESC(debug,"activates debug info");