added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / media / radio / radio-maxiradio.c
blobc5dc00aa9c9f467f2704ca3f13ef531b36248e93
1 /*
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
19 * CHANGES:
20 * 0.75b
21 * - better pci interface thanks to Francois Romieu <romieu@cogenit.fr>
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
27 * BUGS:
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 <asm/io.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...) \
57 do { \
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,
65 .name = "Mute",
66 .minimum = 0,
67 .maximum = 1,
68 .default_value = 1,
69 .type = V4L2_CTRL_TYPE_BOOLEAN,
73 #ifndef PCI_VENDOR_ID_GUILLEMOT
74 #define PCI_VENDOR_ID_GUILLEMOT 0x5046
75 #endif
77 #ifndef PCI_DEVICE_ID_GUILLEMOT
78 #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
79 #endif
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);
88 static unsigned long in_use;
90 #define FREQ_LO 50*16000
91 #define FREQ_HI 150*16000
93 #define FREQ_IF 171200 /* 10.7*16000 */
94 #define FREQ_STEP 200 /* 12.5*16 */
96 /* (x==fmhz*16*1000) -> bits */
97 #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1)) \
98 /(FREQ_STEP<<2))<<2)
100 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
103 static int maxiradio_exclusive_open(struct file *file)
105 return test_and_set_bit(0, &in_use) ? -EBUSY : 0;
108 static int maxiradio_exclusive_release(struct file *file)
110 clear_bit(0, &in_use);
111 return 0;
114 static const struct v4l2_file_operations maxiradio_fops = {
115 .owner = THIS_MODULE,
116 .open = maxiradio_exclusive_open,
117 .release = maxiradio_exclusive_release,
118 .ioctl = video_ioctl2,
121 static struct radio_device
123 __u16 io, /* base of radio io */
124 muted, /* VIDEO_AUDIO_MUTE */
125 stereo, /* VIDEO_TUNER_STEREO_ON */
126 tuned; /* signal strength (0 or 0xffff) */
128 unsigned long freq;
130 struct mutex lock;
131 } radio_unit = {
132 .muted =1,
133 .freq = FREQ_LO,
136 static void outbit(unsigned long bit, __u16 io)
138 if (bit != 0)
140 outb( power|wren|data ,io); udelay(4);
141 outb( power|wren|data|clk ,io); udelay(4);
142 outb( power|wren|data ,io); udelay(4);
144 else
146 outb( power|wren ,io); udelay(4);
147 outb( power|wren|clk ,io); udelay(4);
148 outb( power|wren ,io); udelay(4);
152 static void turn_power(__u16 io, int p)
154 if (p != 0) {
155 dprintk(1, "Radio powered on\n");
156 outb(power, io);
157 } else {
158 dprintk(1, "Radio powered off\n");
159 outb(0,io);
163 static void set_freq(__u16 io, __u32 freq)
165 unsigned long int si;
166 int bl;
167 int val = FREQ2BITS(freq);
169 /* TEA5757 shift register bits (see pdf) */
171 outbit(0, io); /* 24 search */
172 outbit(1, io); /* 23 search up/down */
174 outbit(0, io); /* 22 stereo/mono */
176 outbit(0, io); /* 21 band */
177 outbit(0, io); /* 20 band (only 00=FM works I think) */
179 outbit(0, io); /* 19 port ? */
180 outbit(0, io); /* 18 port ? */
182 outbit(0, io); /* 17 search level */
183 outbit(0, io); /* 16 search level */
185 si = 0x8000;
186 for (bl = 1; bl <= 16; bl++) {
187 outbit(val & si, io);
188 si >>= 1;
191 dprintk(1, "Radio freq set to %d.%02d MHz\n",
192 freq / 16000,
193 freq % 16000 * 100 / 16000);
195 turn_power(io, 1);
198 static int get_stereo(__u16 io)
200 outb(power,io);
201 udelay(4);
203 return !(inb(io) & mo_st);
206 static int get_tune(__u16 io)
208 outb(power+clk,io);
209 udelay(4);
211 return !(inb(io) & mo_st);
215 static int vidioc_querycap (struct file *file, void *priv,
216 struct v4l2_capability *v)
218 strlcpy(v->driver, "radio-maxiradio", sizeof (v->driver));
219 strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof (v->card));
220 sprintf(v->bus_info,"ISA");
221 v->version = RADIO_VERSION;
222 v->capabilities = V4L2_CAP_TUNER;
224 return 0;
227 static int vidioc_g_tuner (struct file *file, void *priv,
228 struct v4l2_tuner *v)
230 struct radio_device *card = video_drvdata(file);
232 if (v->index > 0)
233 return -EINVAL;
235 memset(v,0,sizeof(*v));
236 strcpy(v->name, "FM");
237 v->type = V4L2_TUNER_RADIO;
239 v->rangelow=FREQ_LO;
240 v->rangehigh=FREQ_HI;
241 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
242 v->capability=V4L2_TUNER_CAP_LOW;
243 if(get_stereo(card->io))
244 v->audmode = V4L2_TUNER_MODE_STEREO;
245 else
246 v->audmode = V4L2_TUNER_MODE_MONO;
247 v->signal=0xffff*get_tune(card->io);
249 return 0;
252 static int vidioc_s_tuner (struct file *file, void *priv,
253 struct v4l2_tuner *v)
255 if (v->index > 0)
256 return -EINVAL;
258 return 0;
261 static int vidioc_g_audio (struct file *file, void *priv,
262 struct v4l2_audio *a)
264 if (a->index > 1)
265 return -EINVAL;
267 strcpy(a->name, "FM");
268 a->capability = V4L2_AUDCAP_STEREO;
269 return 0;
272 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
274 *i = 0;
276 return 0;
279 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
281 if (i != 0)
282 return -EINVAL;
284 return 0;
288 static int vidioc_s_audio (struct file *file, void *priv,
289 struct v4l2_audio *a)
291 if (a->index != 0)
292 return -EINVAL;
294 return 0;
297 static int vidioc_s_frequency (struct file *file, void *priv,
298 struct v4l2_frequency *f)
300 struct radio_device *card = video_drvdata(file);
302 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) {
303 dprintk(1, "radio freq (%d.%02d MHz) out of range (%d-%d)\n",
304 f->frequency / 16000,
305 f->frequency % 16000 * 100 / 16000,
306 FREQ_LO / 16000, FREQ_HI / 16000);
308 return -EINVAL;
311 card->freq = f->frequency;
312 set_freq(card->io, card->freq);
313 msleep(125);
315 return 0;
318 static int vidioc_g_frequency (struct file *file, void *priv,
319 struct v4l2_frequency *f)
321 struct radio_device *card = video_drvdata(file);
323 f->type = V4L2_TUNER_RADIO;
324 f->frequency = card->freq;
326 dprintk(4, "radio freq is %d.%02d MHz",
327 f->frequency / 16000,
328 f->frequency % 16000 * 100 / 16000);
330 return 0;
333 static int vidioc_queryctrl (struct file *file, void *priv,
334 struct v4l2_queryctrl *qc)
336 int i;
338 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
339 if (qc->id && qc->id == radio_qctrl[i].id) {
340 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
341 return (0);
345 return -EINVAL;
348 static int vidioc_g_ctrl (struct file *file, void *priv,
349 struct v4l2_control *ctrl)
351 struct radio_device *card = video_drvdata(file);
353 switch (ctrl->id) {
354 case V4L2_CID_AUDIO_MUTE:
355 ctrl->value=card->muted;
356 return (0);
359 return -EINVAL;
362 static int vidioc_s_ctrl (struct file *file, void *priv,
363 struct v4l2_control *ctrl)
365 struct radio_device *card = video_drvdata(file);
367 switch (ctrl->id) {
368 case V4L2_CID_AUDIO_MUTE:
369 card->muted = ctrl->value;
370 if(card->muted)
371 turn_power(card->io, 0);
372 else
373 set_freq(card->io, card->freq);
374 return 0;
377 return -EINVAL;
380 static const struct v4l2_ioctl_ops maxiradio_ioctl_ops = {
381 .vidioc_querycap = vidioc_querycap,
382 .vidioc_g_tuner = vidioc_g_tuner,
383 .vidioc_s_tuner = vidioc_s_tuner,
384 .vidioc_g_audio = vidioc_g_audio,
385 .vidioc_s_audio = vidioc_s_audio,
386 .vidioc_g_input = vidioc_g_input,
387 .vidioc_s_input = vidioc_s_input,
388 .vidioc_g_frequency = vidioc_g_frequency,
389 .vidioc_s_frequency = vidioc_s_frequency,
390 .vidioc_queryctrl = vidioc_queryctrl,
391 .vidioc_g_ctrl = vidioc_g_ctrl,
392 .vidioc_s_ctrl = vidioc_s_ctrl,
395 static struct video_device maxiradio_radio = {
396 .name = "Maxi Radio FM2000 radio",
397 .fops = &maxiradio_fops,
398 .ioctl_ops = &maxiradio_ioctl_ops,
399 .release = video_device_release_empty,
402 static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
404 if(!request_region(pci_resource_start(pdev, 0),
405 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
406 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
407 goto err_out;
410 if (pci_enable_device(pdev))
411 goto err_out_free_region;
413 radio_unit.io = pci_resource_start(pdev, 0);
414 mutex_init(&radio_unit.lock);
415 video_set_drvdata(&maxiradio_radio, &radio_unit);
417 if (video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
418 printk("radio-maxiradio: can't register device!");
419 goto err_out_free_region;
422 printk(KERN_INFO "radio-maxiradio: version "
423 DRIVER_VERSION
424 " time "
425 __TIME__ " "
426 __DATE__
427 "\n");
429 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
430 radio_unit.io);
431 return 0;
433 err_out_free_region:
434 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
435 err_out:
436 return -ENODEV;
439 static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
441 video_unregister_device(&maxiradio_radio);
442 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
445 static struct pci_device_id maxiradio_pci_tbl[] = {
446 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
447 PCI_ANY_ID, PCI_ANY_ID, },
448 { 0,}
451 MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
453 static struct pci_driver maxiradio_driver = {
454 .name = "radio-maxiradio",
455 .id_table = maxiradio_pci_tbl,
456 .probe = maxiradio_init_one,
457 .remove = __devexit_p(maxiradio_remove_one),
460 static int __init maxiradio_radio_init(void)
462 return pci_register_driver(&maxiradio_driver);
465 static void __exit maxiradio_radio_exit(void)
467 pci_unregister_driver(&maxiradio_driver);
470 module_init(maxiradio_radio_init);
471 module_exit(maxiradio_radio_exit);
473 MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
474 MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
475 MODULE_LICENSE("GPL");
477 module_param_named(debug,maxiradio_radio.debug, int, 0644);
478 MODULE_PARM_DESC(debug,"activates debug info");