blob: 82a2f83fe2e201148cf993c767fd6e4255066966 [file] [log] [blame]
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03001/*
2 * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (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
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03009 * the windows driver (radio.dll).
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * The card uses the TEA5757 chip that includes a search function but it
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030012 * is useless as I haven't found any way to read back the frequency. If
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 *
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030023 * 0.75 Sun Feb 4 22:51:27 EET 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * - tiding up
25 * - removed support for multiple devices as it didn't work anyway
26 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030027 * BUGS:
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * - card unmutes if you change frequency
29 *
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -030030 * (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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/ioport.h>
39#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
41#include <asm/uaccess.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020042#include <linux/mutex.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/pci.h>
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030045#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030046#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030048#define DRIVER_VERSION "0.76"
49
50#include <linux/version.h> /* for KERNEL_VERSION MACRO */
51#define RADIO_VERSION KERNEL_VERSION(0,7,6)
52
53static struct v4l2_queryctrl radio_qctrl[] = {
54 {
55 .id = V4L2_CID_AUDIO_MUTE,
56 .name = "Mute",
57 .minimum = 0,
58 .maximum = 1,
59 .default_value = 1,
60 .type = V4L2_CTRL_TYPE_BOOLEAN,
61 }
62};
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#ifndef PCI_VENDOR_ID_GUILLEMOT
65#define PCI_VENDOR_ID_GUILLEMOT 0x5046
66#endif
67
68#ifndef PCI_DEVICE_ID_GUILLEMOT
69#define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
70#endif
71
72
73/* TEA5757 pin mappings */
74static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
75
76static int radio_nr = -1;
77module_param(radio_nr, int, 0);
78
79
80#define FREQ_LO 50*16000
81#define FREQ_HI 150*16000
82
83#define FREQ_IF 171200 /* 10.7*16000 */
84#define FREQ_STEP 200 /* 12.5*16 */
85
86#define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
87 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
88
89#define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
90
91
Arjan van de Venfa027c22007-02-12 00:55:33 -080092static const struct file_operations maxiradio_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .owner = THIS_MODULE,
94 .open = video_exclusive_open,
95 .release = video_exclusive_release,
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -030096 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020097 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .llseek = no_llseek,
99};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101static struct radio_device
102{
103 __u16 io, /* base of radio io */
104 muted, /* VIDEO_AUDIO_MUTE */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300105 stereo, /* VIDEO_TUNER_STEREO_ON */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 tuned; /* signal strength (0 or 0xffff) */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned long freq;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300109
Ingo Molnar3593cab2006-02-07 06:49:14 -0200110 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111} radio_unit = {0, 0, 0, 0, };
112
113
114static void outbit(unsigned long bit, __u16 io)
115{
116 if(bit != 0)
117 {
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);
121 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300122 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 {
124 outb( power|wren ,io); udelay(4);
125 outb( power|wren|clk ,io); udelay(4);
126 outb( power|wren ,io); udelay(4);
127 }
128}
129
130static void turn_power(__u16 io, int p)
131{
132 if(p != 0) outb(power, io); else outb(0,io);
133}
134
135
136static void set_freq(__u16 io, __u32 data)
137{
138 unsigned long int si;
139 int bl;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* TEA5757 shift register bits (see pdf) */
142
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300143 outbit(0,io); // 24 search
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 outbit(1,io); // 23 search up/down
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 outbit(0,io); // 22 stereo/mono
147
148 outbit(0,io); // 21 band
149 outbit(0,io); // 20 band (only 00=FM works I think)
150
151 outbit(0,io); // 19 port ?
152 outbit(0,io); // 18 port ?
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 outbit(0,io); // 17 search level
155 outbit(0,io); // 16 search level
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 si = 0x8000;
158 for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 outb(power,io);
161}
162
163static int get_stereo(__u16 io)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 outb(power,io); udelay(4);
166 return !(inb(io) & mo_st);
167}
168
169static int get_tune(__u16 io)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 outb(power+clk,io); udelay(4);
172 return !(inb(io) & mo_st);
173}
174
175
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300176static int vidioc_querycap (struct file *file, void *priv,
177 struct v4l2_capability *v)
178{
179 strlcpy(v->driver, "radio-maxiradio", sizeof (v->driver));
180 strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof (v->card));
181 sprintf(v->bus_info,"ISA");
182 v->version = RADIO_VERSION;
183 v->capabilities = V4L2_CAP_TUNER;
184
185 return 0;
186}
187
188static int vidioc_g_tuner (struct file *file, void *priv,
189 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct video_device *dev = video_devdata(file);
192 struct radio_device *card=dev->priv;
193
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300194 if (v->index > 0)
195 return -EINVAL;
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -0300196
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300197 memset(v,0,sizeof(*v));
198 strcpy(v->name, "FM");
199 v->type = V4L2_TUNER_RADIO;
200
201 v->rangelow=FREQ_LO;
202 v->rangehigh=FREQ_HI;
203 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
204 v->capability=V4L2_TUNER_CAP_LOW;
205 if(get_stereo(card->io))
206 v->audmode = V4L2_TUNER_MODE_STEREO;
207 else
208 v->audmode = V4L2_TUNER_MODE_MONO;
209 v->signal=0xffff*get_tune(card->io);
210
211 return 0;
212}
213
214static int vidioc_s_tuner (struct file *file, void *priv,
215 struct v4l2_tuner *v)
216{
217 if (v->index > 0)
218 return -EINVAL;
219
220 return 0;
221}
222
Mauro Carvalho Chehab140dcc42007-01-25 15:00:45 -0300223static int vidioc_g_audio (struct file *file, void *priv,
224 struct v4l2_audio *a)
225{
226 if (a->index > 1)
227 return -EINVAL;
228
Mauro Carvalho Chehabb61f8d62007-01-26 07:07:12 -0300229 strcpy(a->name, "FM");
Mauro Carvalho Chehab140dcc42007-01-25 15:00:45 -0300230 a->capability = V4L2_AUDCAP_STEREO;
231 return 0;
232}
233
Mauro Carvalho Chehaba0c05ab2007-01-25 16:48:13 -0300234static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
235{
236 *i = 0;
237 return 0;
238}
239
240static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
241{
242 if (i != 0)
243 return -EINVAL;
244 return 0;
245}
246
247
Mauro Carvalho Chehab140dcc42007-01-25 15:00:45 -0300248static int vidioc_s_audio (struct file *file, void *priv,
249 struct v4l2_audio *a)
250{
251 if (a->index != 0)
252 return -EINVAL;
253
254 return 0;
255}
256
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300257static int vidioc_s_frequency (struct file *file, void *priv,
258 struct v4l2_frequency *f)
259{
260 struct video_device *dev = video_devdata(file);
261 struct radio_device *card=dev->priv;
262
263 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
264 return -EINVAL;
265
266 card->freq = f->frequency;
267 set_freq(card->io, FREQ2BITS(card->freq));
268 msleep(125);
269
270 return 0;
271}
272
273static int vidioc_g_frequency (struct file *file, void *priv,
274 struct v4l2_frequency *f)
275{
276 struct video_device *dev = video_devdata(file);
277 struct radio_device *card=dev->priv;
278
279 f->type = V4L2_TUNER_RADIO;
280 f->frequency = card->freq;
281
282 return 0;
283}
284
285static int vidioc_queryctrl (struct file *file, void *priv,
286 struct v4l2_queryctrl *qc)
287{
288 int i;
289
290 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
291 if (qc->id && qc->id == radio_qctrl[i].id) {
292 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
293 return (0);
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -0300294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300296 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300299static int vidioc_g_ctrl (struct file *file, void *priv,
300 struct v4l2_control *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct video_device *dev = video_devdata(file);
303 struct radio_device *card=dev->priv;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300304
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300305 switch (ctrl->id) {
306 case V4L2_CID_AUDIO_MUTE:
307 ctrl->value=card->muted;
308 return (0);
309 }
310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300313static int vidioc_s_ctrl (struct file *file, void *priv,
314 struct v4l2_control *ctrl)
315{
316 struct video_device *dev = video_devdata(file);
317 struct radio_device *card=dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300319 switch (ctrl->id) {
320 case V4L2_CID_AUDIO_MUTE:
321 card->muted = ctrl->value;
322 if(card->muted)
323 turn_power(card->io, 0);
324 else
325 set_freq(card->io, FREQ2BITS(card->freq));
326 return 0;
327 }
328 return -EINVAL;
329}
330
331static struct video_device maxiradio_radio =
332{
333 .owner = THIS_MODULE,
334 .name = "Maxi Radio FM2000 radio",
335 .type = VID_TYPE_TUNER,
336 .fops = &maxiradio_fops,
337
338 .vidioc_querycap = vidioc_querycap,
339 .vidioc_g_tuner = vidioc_g_tuner,
340 .vidioc_s_tuner = vidioc_s_tuner,
Mauro Carvalho Chehab140dcc42007-01-25 15:00:45 -0300341 .vidioc_g_audio = vidioc_g_audio,
342 .vidioc_s_audio = vidioc_s_audio,
Mauro Carvalho Chehaba0c05ab2007-01-25 16:48:13 -0300343 .vidioc_g_input = vidioc_g_input,
344 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300345 .vidioc_g_frequency = vidioc_g_frequency,
346 .vidioc_s_frequency = vidioc_s_frequency,
347 .vidioc_queryctrl = vidioc_queryctrl,
348 .vidioc_g_ctrl = vidioc_g_ctrl,
349 .vidioc_s_ctrl = vidioc_s_ctrl,
350
351};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
354{
355 if(!request_region(pci_resource_start(pdev, 0),
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300356 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
357 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
358 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
361 if (pci_enable_device(pdev))
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300362 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 radio_unit.io = pci_resource_start(pdev, 0);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200365 mutex_init(&radio_unit.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 maxiradio_radio.priv = &radio_unit;
367
368 if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300369 printk("radio-maxiradio: can't register device!");
370 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 printk(KERN_INFO "radio-maxiradio: version "
374 DRIVER_VERSION
375 " time "
376 __TIME__ " "
377 __DATE__
378 "\n");
379
380 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
381 radio_unit.io);
382 return 0;
383
384err_out_free_region:
385 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
386err_out:
387 return -ENODEV;
388}
389
390static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
391{
392 video_unregister_device(&maxiradio_radio);
393 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
394}
395
396static struct pci_device_id maxiradio_pci_tbl[] = {
397 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
398 PCI_ANY_ID, PCI_ANY_ID, },
399 { 0,}
400};
401
402MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
403
404static struct pci_driver maxiradio_driver = {
405 .name = "radio-maxiradio",
406 .id_table = maxiradio_pci_tbl,
407 .probe = maxiradio_init_one,
408 .remove = __devexit_p(maxiradio_remove_one),
409};
410
411static int __init maxiradio_radio_init(void)
412{
Richard Knutsson9bfab8c2005-11-30 00:59:34 +0100413 return pci_register_driver(&maxiradio_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416static void __exit maxiradio_radio_exit(void)
417{
418 pci_unregister_driver(&maxiradio_driver);
419}
420
421module_init(maxiradio_radio_init);
422module_exit(maxiradio_radio_exit);
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300423
424MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
425MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
426MODULE_LICENSE("GPL");
427
428module_param_named(debug,maxiradio_radio.debug, int, 0644);
429MODULE_PARM_DESC(debug,"activates debug info");