V4L/DVB (10885): radio-maestro: convert to v4l2_device.
[deliverable/linux.git] / drivers / media / radio / radio-maxiradio.c
CommitLineData
4286c6f6
MCC
1/*
2 * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
1da177e4
LT
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
4286c6f6 9 * the windows driver (radio.dll).
1da177e4
LT
10 *
11 * The card uses the TEA5757 chip that includes a search function but it
4286c6f6 12 * is useless as I haven't found any way to read back the frequency. If
1da177e4
LT
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 *
e84fef6b 23 * 0.75 Sun Feb 4 22:51:27 EET 2001
1da177e4
LT
24 * - tiding up
25 * - removed support for multiple devices as it didn't work anyway
26 *
4286c6f6 27 * BUGS:
1da177e4
LT
28 * - card unmutes if you change frequency
29 *
06470ed6
MCC
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
1da177e4
LT
33 */
34
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/ioport.h>
39#include <linux/delay.h>
1da177e4
LT
40#include <asm/io.h>
41#include <asm/uaccess.h>
3593cab5
IM
42#include <linux/mutex.h>
43
1da177e4 44#include <linux/pci.h>
e84fef6b 45#include <linux/videodev2.h>
5e87efa3 46#include <media/v4l2-common.h>
35ea11ff 47#include <media/v4l2-ioctl.h>
1da177e4 48
f1557ceb 49#define DRIVER_VERSION "0.77"
e84fef6b
MCC
50
51#include <linux/version.h> /* for KERNEL_VERSION MACRO */
f1557ceb
MCC
52#define RADIO_VERSION KERNEL_VERSION(0,7,7)
53
54static struct video_device maxiradio_radio;
55
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)
e84fef6b
MCC
61
62static struct v4l2_queryctrl radio_qctrl[] = {
63 {
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,
70 }
71};
1da177e4
LT
72
73#ifndef PCI_VENDOR_ID_GUILLEMOT
74#define PCI_VENDOR_ID_GUILLEMOT 0x5046
75#endif
76
77#ifndef PCI_DEVICE_ID_GUILLEMOT
78#define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
79#endif
80
81
82/* TEA5757 pin mappings */
83static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
84
85static int radio_nr = -1;
86module_param(radio_nr, int, 0);
87
3ca685aa 88static unsigned long in_use;
1da177e4
LT
89
90#define FREQ_LO 50*16000
91#define FREQ_HI 150*16000
92
93#define FREQ_IF 171200 /* 10.7*16000 */
94#define FREQ_STEP 200 /* 12.5*16 */
95
f1557ceb
MCC
96/* (x==fmhz*16*1000) -> bits */
97#define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1)) \
98 /(FREQ_STEP<<2))<<2)
1da177e4
LT
99
100#define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
101
102
bec43661 103static int maxiradio_exclusive_open(struct file *file)
3ca685aa
HV
104{
105 return test_and_set_bit(0, &in_use) ? -EBUSY : 0;
106}
107
bec43661 108static int maxiradio_exclusive_release(struct file *file)
3ca685aa
HV
109{
110 clear_bit(0, &in_use);
111 return 0;
112}
113
bec43661 114static const struct v4l2_file_operations maxiradio_fops = {
1da177e4 115 .owner = THIS_MODULE,
3ca685aa
HV
116 .open = maxiradio_exclusive_open,
117 .release = maxiradio_exclusive_release,
06470ed6 118 .ioctl = video_ioctl2,
1da177e4 119};
1da177e4
LT
120
121static struct radio_device
122{
123 __u16 io, /* base of radio io */
124 muted, /* VIDEO_AUDIO_MUTE */
4286c6f6 125 stereo, /* VIDEO_TUNER_STEREO_ON */
1da177e4 126 tuned; /* signal strength (0 or 0xffff) */
4286c6f6 127
1da177e4 128 unsigned long freq;
4286c6f6 129
3593cab5 130 struct mutex lock;
712642b8
MCC
131} radio_unit = {
132 .muted =1,
133 .freq = FREQ_LO,
134};
1da177e4
LT
135
136static void outbit(unsigned long bit, __u16 io)
137{
f1557ceb 138 if (bit != 0)
1da177e4
LT
139 {
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);
143 }
4286c6f6 144 else
1da177e4
LT
145 {
146 outb( power|wren ,io); udelay(4);
147 outb( power|wren|clk ,io); udelay(4);
148 outb( power|wren ,io); udelay(4);
149 }
150}
151
152static void turn_power(__u16 io, int p)
153{
f1557ceb
MCC
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);
160 }
1da177e4
LT
161}
162
f1557ceb 163static void set_freq(__u16 io, __u32 freq)
1da177e4
LT
164{
165 unsigned long int si;
166 int bl;
c6eb8eaf 167 int val = FREQ2BITS(freq);
4286c6f6 168
1da177e4
LT
169 /* TEA5757 shift register bits (see pdf) */
170
c6eb8eaf
HV
171 outbit(0, io); /* 24 search */
172 outbit(1, io); /* 23 search up/down */
4286c6f6 173
c6eb8eaf 174 outbit(0, io); /* 22 stereo/mono */
1da177e4 175
c6eb8eaf
HV
176 outbit(0, io); /* 21 band */
177 outbit(0, io); /* 20 band (only 00=FM works I think) */
1da177e4 178
c6eb8eaf
HV
179 outbit(0, io); /* 19 port ? */
180 outbit(0, io); /* 18 port ? */
4286c6f6 181
c6eb8eaf
HV
182 outbit(0, io); /* 17 search level */
183 outbit(0, io); /* 16 search level */
4286c6f6 184
1da177e4 185 si = 0x8000;
c6eb8eaf
HV
186 for (bl = 1; bl <= 16; bl++) {
187 outbit(val & si, io);
188 si >>= 1;
f1557ceb 189 }
4286c6f6 190
f1557ceb
MCC
191 dprintk(1, "Radio freq set to %d.%02d MHz\n",
192 freq / 16000,
193 freq % 16000 * 100 / 16000);
194
195 turn_power(io, 1);
1da177e4
LT
196}
197
198static int get_stereo(__u16 io)
4286c6f6 199{
f1557ceb
MCC
200 outb(power,io);
201 udelay(4);
202
1da177e4
LT
203 return !(inb(io) & mo_st);
204}
205
206static int get_tune(__u16 io)
4286c6f6 207{
f1557ceb
MCC
208 outb(power+clk,io);
209 udelay(4);
210
1da177e4
LT
211 return !(inb(io) & mo_st);
212}
213
214
06470ed6
MCC
215static int vidioc_querycap (struct file *file, void *priv,
216 struct v4l2_capability *v)
217{
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;
223
224 return 0;
225}
226
227static int vidioc_g_tuner (struct file *file, void *priv,
228 struct v4l2_tuner *v)
1da177e4 229{
c170ecf4 230 struct radio_device *card = video_drvdata(file);
1da177e4 231
06470ed6
MCC
232 if (v->index > 0)
233 return -EINVAL;
e84fef6b 234
06470ed6
MCC
235 memset(v,0,sizeof(*v));
236 strcpy(v->name, "FM");
237 v->type = V4L2_TUNER_RADIO;
4286c6f6 238
06470ed6
MCC
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);
4286c6f6 248
06470ed6
MCC
249 return 0;
250}
e84fef6b 251
06470ed6
MCC
252static int vidioc_s_tuner (struct file *file, void *priv,
253 struct v4l2_tuner *v)
254{
255 if (v->index > 0)
256 return -EINVAL;
4286c6f6 257
06470ed6
MCC
258 return 0;
259}
e84fef6b 260
140dcc46
MCC
261static int vidioc_g_audio (struct file *file, void *priv,
262 struct v4l2_audio *a)
263{
264 if (a->index > 1)
265 return -EINVAL;
266
b61f8d69 267 strcpy(a->name, "FM");
140dcc46
MCC
268 a->capability = V4L2_AUDCAP_STEREO;
269 return 0;
270}
271
a0c05ab9
MCC
272static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
273{
274 *i = 0;
f1557ceb 275
a0c05ab9
MCC
276 return 0;
277}
278
279static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
280{
281 if (i != 0)
282 return -EINVAL;
f1557ceb 283
a0c05ab9
MCC
284 return 0;
285}
286
287
140dcc46
MCC
288static int vidioc_s_audio (struct file *file, void *priv,
289 struct v4l2_audio *a)
290{
291 if (a->index != 0)
292 return -EINVAL;
293
294 return 0;
295}
296
06470ed6
MCC
297static int vidioc_s_frequency (struct file *file, void *priv,
298 struct v4l2_frequency *f)
299{
c170ecf4 300 struct radio_device *card = video_drvdata(file);
4286c6f6 301
f1557ceb
MCC
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);
307
06470ed6 308 return -EINVAL;
f1557ceb 309 }
4286c6f6 310
06470ed6 311 card->freq = f->frequency;
f1557ceb 312 set_freq(card->io, card->freq);
06470ed6 313 msleep(125);
e84fef6b 314
06470ed6
MCC
315 return 0;
316}
4286c6f6 317
06470ed6
MCC
318static int vidioc_g_frequency (struct file *file, void *priv,
319 struct v4l2_frequency *f)
320{
c170ecf4 321 struct radio_device *card = video_drvdata(file);
4286c6f6 322
06470ed6
MCC
323 f->type = V4L2_TUNER_RADIO;
324 f->frequency = card->freq;
325
f1557ceb
MCC
326 dprintk(4, "radio freq is %d.%02d MHz",
327 f->frequency / 16000,
328 f->frequency % 16000 * 100 / 16000);
329
06470ed6
MCC
330 return 0;
331}
332
333static int vidioc_queryctrl (struct file *file, void *priv,
334 struct v4l2_queryctrl *qc)
335{
336 int i;
337
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);
e84fef6b 342 }
06470ed6 343 }
f1557ceb 344
06470ed6
MCC
345 return -EINVAL;
346}
e84fef6b 347
06470ed6
MCC
348static int vidioc_g_ctrl (struct file *file, void *priv,
349 struct v4l2_control *ctrl)
350{
c170ecf4 351 struct radio_device *card = video_drvdata(file);
e84fef6b 352
06470ed6
MCC
353 switch (ctrl->id) {
354 case V4L2_CID_AUDIO_MUTE:
355 ctrl->value=card->muted;
356 return (0);
1da177e4 357 }
f1557ceb 358
06470ed6 359 return -EINVAL;
1da177e4
LT
360}
361
06470ed6
MCC
362static int vidioc_s_ctrl (struct file *file, void *priv,
363 struct v4l2_control *ctrl)
1da177e4 364{
c170ecf4 365 struct radio_device *card = video_drvdata(file);
4286c6f6 366
06470ed6
MCC
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
f1557ceb 373 set_freq(card->io, card->freq);
06470ed6
MCC
374 return 0;
375 }
f1557ceb 376
06470ed6 377 return -EINVAL;
1da177e4
LT
378}
379
a399810c 380static const struct v4l2_ioctl_ops maxiradio_ioctl_ops = {
06470ed6
MCC
381 .vidioc_querycap = vidioc_querycap,
382 .vidioc_g_tuner = vidioc_g_tuner,
383 .vidioc_s_tuner = vidioc_s_tuner,
140dcc46
MCC
384 .vidioc_g_audio = vidioc_g_audio,
385 .vidioc_s_audio = vidioc_s_audio,
a0c05ab9
MCC
386 .vidioc_g_input = vidioc_g_input,
387 .vidioc_s_input = vidioc_s_input,
06470ed6
MCC
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,
06470ed6 393};
1da177e4 394
a399810c 395static struct video_device maxiradio_radio = {
aa5e90af
HV
396 .name = "Maxi Radio FM2000 radio",
397 .fops = &maxiradio_fops,
398 .ioctl_ops = &maxiradio_ioctl_ops,
399 .release = video_device_release_empty,
a399810c
HV
400};
401
1da177e4
LT
402static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
403{
404 if(!request_region(pci_resource_start(pdev, 0),
4286c6f6
MCC
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;
1da177e4
LT
408 }
409
410 if (pci_enable_device(pdev))
4286c6f6 411 goto err_out_free_region;
1da177e4
LT
412
413 radio_unit.io = pci_resource_start(pdev, 0);
3593cab5 414 mutex_init(&radio_unit.lock);
601e9444 415 video_set_drvdata(&maxiradio_radio, &radio_unit);
1da177e4 416
cba99ae8 417 if (video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
4286c6f6
MCC
418 printk("radio-maxiradio: can't register device!");
419 goto err_out_free_region;
1da177e4
LT
420 }
421
422 printk(KERN_INFO "radio-maxiradio: version "
423 DRIVER_VERSION
424 " time "
425 __TIME__ " "
426 __DATE__
427 "\n");
428
429 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
430 radio_unit.io);
431 return 0;
432
433err_out_free_region:
434 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
435err_out:
436 return -ENODEV;
437}
438
439static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
440{
441 video_unregister_device(&maxiradio_radio);
442 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
443}
444
445static 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,}
449};
450
451MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
452
453static 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),
458};
459
460static int __init maxiradio_radio_init(void)
461{
9bfab8ce 462 return pci_register_driver(&maxiradio_driver);
1da177e4
LT
463}
464
465static void __exit maxiradio_radio_exit(void)
466{
467 pci_unregister_driver(&maxiradio_driver);
468}
469
470module_init(maxiradio_radio_init);
471module_exit(maxiradio_radio_exit);
06470ed6
MCC
472
473MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
474MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
475MODULE_LICENSE("GPL");
476
477module_param_named(debug,maxiradio_radio.debug, int, 0644);
478MODULE_PARM_DESC(debug,"activates debug info");
This page took 0.439033 seconds and 5 git commands to generate.