Merge branch 'bkl/procfs' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6-block.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>
3593cab5 40#include <linux/mutex.h>
1da177e4 41#include <linux/pci.h>
e84fef6b 42#include <linux/videodev2.h>
2710e6aa
HV
43#include <linux/version.h> /* for KERNEL_VERSION MACRO */
44#include <linux/io.h>
5a0e3ad6 45#include <linux/slab.h>
2710e6aa 46#include <media/v4l2-device.h>
35ea11ff 47#include <media/v4l2-ioctl.h>
1da177e4 48
2710e6aa
HV
49MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
50MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
51MODULE_LICENSE("GPL");
52
53static int radio_nr = -1;
54module_param(radio_nr, int, 0);
55
56static int debug;
57
58module_param(debug, int, 0644);
59MODULE_PARM_DESC(debug, "activates debug info");
60
f1557ceb 61#define DRIVER_VERSION "0.77"
e84fef6b 62
2710e6aa
HV
63#define RADIO_VERSION KERNEL_VERSION(0, 7, 7)
64
65#define dprintk(dev, num, fmt, arg...) \
66 v4l2_dbg(num, debug, &dev->v4l2_dev, fmt, ## arg)
1da177e4
LT
67
68#ifndef PCI_VENDOR_ID_GUILLEMOT
69#define PCI_VENDOR_ID_GUILLEMOT 0x5046
70#endif
71
72#ifndef PCI_DEVICE_ID_GUILLEMOT
73#define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
74#endif
75
76
77/* TEA5757 pin mappings */
2710e6aa 78static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16;
1da177e4 79
2710e6aa
HV
80#define FREQ_LO (50 * 16000)
81#define FREQ_HI (150 * 16000)
1da177e4
LT
82
83#define FREQ_IF 171200 /* 10.7*16000 */
84#define FREQ_STEP 200 /* 12.5*16 */
85
f1557ceb 86/* (x==fmhz*16*1000) -> bits */
2710e6aa
HV
87#define FREQ2BITS(x) \
88 ((((unsigned int)(x) + FREQ_IF + (FREQ_STEP << 1)) / (FREQ_STEP << 2)) << 2)
1da177e4
LT
89
90#define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
91
92
2710e6aa 93struct maxiradio
3ca685aa 94{
2710e6aa
HV
95 struct v4l2_device v4l2_dev;
96 struct video_device vdev;
97 struct pci_dev *pdev;
1da177e4 98
2710e6aa
HV
99 u16 io; /* base of radio io */
100 u16 muted; /* VIDEO_AUDIO_MUTE */
101 u16 stereo; /* VIDEO_TUNER_STEREO_ON */
102 u16 tuned; /* signal strength (0 or 0xffff) */
4286c6f6 103
1da177e4 104 unsigned long freq;
4286c6f6 105
3593cab5 106 struct mutex lock;
712642b8 107};
1da177e4 108
2710e6aa 109static inline struct maxiradio *to_maxiradio(struct v4l2_device *v4l2_dev)
1da177e4 110{
2710e6aa 111 return container_of(v4l2_dev, struct maxiradio, v4l2_dev);
1da177e4
LT
112}
113
2710e6aa
HV
114static void outbit(unsigned long bit, u16 io)
115{
116 int val = power | wren | (bit ? data : 0);
117
118 outb(val, io);
119 udelay(4);
120 outb(val | clk, io);
121 udelay(4);
122 outb(val, io);
123 udelay(4);
124}
125
126static void turn_power(struct maxiradio *dev, int p)
1da177e4 127{
f1557ceb 128 if (p != 0) {
2710e6aa
HV
129 dprintk(dev, 1, "Radio powered on\n");
130 outb(power, dev->io);
f1557ceb 131 } else {
2710e6aa
HV
132 dprintk(dev, 1, "Radio powered off\n");
133 outb(0, dev->io);
f1557ceb 134 }
1da177e4
LT
135}
136
2710e6aa 137static void set_freq(struct maxiradio *dev, u32 freq)
1da177e4
LT
138{
139 unsigned long int si;
140 int bl;
2710e6aa 141 int io = dev->io;
c6eb8eaf 142 int val = FREQ2BITS(freq);
4286c6f6 143
1da177e4
LT
144 /* TEA5757 shift register bits (see pdf) */
145
c6eb8eaf
HV
146 outbit(0, io); /* 24 search */
147 outbit(1, io); /* 23 search up/down */
4286c6f6 148
c6eb8eaf 149 outbit(0, io); /* 22 stereo/mono */
1da177e4 150
c6eb8eaf
HV
151 outbit(0, io); /* 21 band */
152 outbit(0, io); /* 20 band (only 00=FM works I think) */
1da177e4 153
c6eb8eaf
HV
154 outbit(0, io); /* 19 port ? */
155 outbit(0, io); /* 18 port ? */
4286c6f6 156
c6eb8eaf
HV
157 outbit(0, io); /* 17 search level */
158 outbit(0, io); /* 16 search level */
4286c6f6 159
1da177e4 160 si = 0x8000;
c6eb8eaf
HV
161 for (bl = 1; bl <= 16; bl++) {
162 outbit(val & si, io);
163 si >>= 1;
f1557ceb 164 }
4286c6f6 165
2710e6aa 166 dprintk(dev, 1, "Radio freq set to %d.%02d MHz\n",
f1557ceb
MCC
167 freq / 16000,
168 freq % 16000 * 100 / 16000);
169
2710e6aa 170 turn_power(dev, 1);
1da177e4
LT
171}
172
2710e6aa 173static int get_stereo(u16 io)
4286c6f6 174{
f1557ceb
MCC
175 outb(power,io);
176 udelay(4);
177
1da177e4
LT
178 return !(inb(io) & mo_st);
179}
180
2710e6aa 181static int get_tune(u16 io)
4286c6f6 182{
f1557ceb
MCC
183 outb(power+clk,io);
184 udelay(4);
185
1da177e4
LT
186 return !(inb(io) & mo_st);
187}
188
189
2710e6aa 190static int vidioc_querycap(struct file *file, void *priv,
06470ed6
MCC
191 struct v4l2_capability *v)
192{
2710e6aa 193 struct maxiradio *dev = video_drvdata(file);
06470ed6 194
2710e6aa
HV
195 strlcpy(v->driver, "radio-maxiradio", sizeof(v->driver));
196 strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof(v->card));
197 snprintf(v->bus_info, sizeof(v->bus_info), "PCI:%s", pci_name(dev->pdev));
198 v->version = RADIO_VERSION;
199 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
06470ed6
MCC
200 return 0;
201}
202
2710e6aa 203static int vidioc_g_tuner(struct file *file, void *priv,
06470ed6 204 struct v4l2_tuner *v)
1da177e4 205{
2710e6aa 206 struct maxiradio *dev = video_drvdata(file);
1da177e4 207
06470ed6
MCC
208 if (v->index > 0)
209 return -EINVAL;
e84fef6b 210
2710e6aa
HV
211 mutex_lock(&dev->lock);
212 strlcpy(v->name, "FM", sizeof(v->name));
06470ed6 213 v->type = V4L2_TUNER_RADIO;
2710e6aa
HV
214 v->rangelow = FREQ_LO;
215 v->rangehigh = FREQ_HI;
216 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
217 v->capability = V4L2_TUNER_CAP_LOW;
218 if (get_stereo(dev->io))
06470ed6
MCC
219 v->audmode = V4L2_TUNER_MODE_STEREO;
220 else
221 v->audmode = V4L2_TUNER_MODE_MONO;
2710e6aa
HV
222 v->signal = 0xffff * get_tune(dev->io);
223 mutex_unlock(&dev->lock);
4286c6f6 224
06470ed6
MCC
225 return 0;
226}
e84fef6b 227
2710e6aa 228static int vidioc_s_tuner(struct file *file, void *priv,
06470ed6
MCC
229 struct v4l2_tuner *v)
230{
2710e6aa 231 return v->index ? -EINVAL : 0;
140dcc46
MCC
232}
233
a0c05ab9
MCC
234static 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{
2710e6aa
HV
242 return i ? -EINVAL : 0;
243}
f1557ceb 244
2710e6aa
HV
245static int vidioc_g_audio(struct file *file, void *priv,
246 struct v4l2_audio *a)
247{
248 a->index = 0;
249 strlcpy(a->name, "Radio", sizeof(a->name));
250 a->capability = V4L2_AUDCAP_STEREO;
a0c05ab9
MCC
251 return 0;
252}
253
254
2710e6aa 255static int vidioc_s_audio(struct file *file, void *priv,
140dcc46
MCC
256 struct v4l2_audio *a)
257{
2710e6aa 258 return a->index ? -EINVAL : 0;
140dcc46
MCC
259}
260
2710e6aa 261static int vidioc_s_frequency(struct file *file, void *priv,
06470ed6
MCC
262 struct v4l2_frequency *f)
263{
2710e6aa 264 struct maxiradio *dev = video_drvdata(file);
4286c6f6 265
a3a9e287
HV
266 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
267 return -EINVAL;
f1557ceb 268 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) {
2710e6aa 269 dprintk(dev, 1, "radio freq (%d.%02d MHz) out of range (%d-%d)\n",
f1557ceb
MCC
270 f->frequency / 16000,
271 f->frequency % 16000 * 100 / 16000,
272 FREQ_LO / 16000, FREQ_HI / 16000);
273
06470ed6 274 return -EINVAL;
f1557ceb 275 }
4286c6f6 276
2710e6aa
HV
277 mutex_lock(&dev->lock);
278 dev->freq = f->frequency;
279 set_freq(dev, dev->freq);
06470ed6 280 msleep(125);
2710e6aa 281 mutex_unlock(&dev->lock);
e84fef6b 282
06470ed6
MCC
283 return 0;
284}
4286c6f6 285
2710e6aa 286static int vidioc_g_frequency(struct file *file, void *priv,
06470ed6
MCC
287 struct v4l2_frequency *f)
288{
2710e6aa 289 struct maxiradio *dev = video_drvdata(file);
4286c6f6 290
a3a9e287
HV
291 if (f->tuner != 0)
292 return -EINVAL;
06470ed6 293 f->type = V4L2_TUNER_RADIO;
2710e6aa 294 f->frequency = dev->freq;
06470ed6 295
2710e6aa 296 dprintk(dev, 4, "radio freq is %d.%02d MHz",
f1557ceb
MCC
297 f->frequency / 16000,
298 f->frequency % 16000 * 100 / 16000);
299
06470ed6
MCC
300 return 0;
301}
302
2710e6aa 303static int vidioc_queryctrl(struct file *file, void *priv,
06470ed6
MCC
304 struct v4l2_queryctrl *qc)
305{
2710e6aa
HV
306 switch (qc->id) {
307 case V4L2_CID_AUDIO_MUTE:
308 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
06470ed6
MCC
309 }
310 return -EINVAL;
311}
e84fef6b 312
2710e6aa
HV
313static int vidioc_g_ctrl(struct file *file, void *priv,
314 struct v4l2_control *ctrl)
06470ed6 315{
2710e6aa 316 struct maxiradio *dev = video_drvdata(file);
e84fef6b 317
06470ed6 318 switch (ctrl->id) {
2710e6aa
HV
319 case V4L2_CID_AUDIO_MUTE:
320 ctrl->value = dev->muted;
321 return 0;
1da177e4 322 }
f1557ceb 323
06470ed6 324 return -EINVAL;
1da177e4
LT
325}
326
2710e6aa
HV
327static int vidioc_s_ctrl(struct file *file, void *priv,
328 struct v4l2_control *ctrl)
1da177e4 329{
2710e6aa 330 struct maxiradio *dev = video_drvdata(file);
4286c6f6 331
06470ed6 332 switch (ctrl->id) {
2710e6aa
HV
333 case V4L2_CID_AUDIO_MUTE:
334 mutex_lock(&dev->lock);
335 dev->muted = ctrl->value;
336 if (dev->muted)
337 turn_power(dev, 0);
338 else
339 set_freq(dev, dev->freq);
340 mutex_unlock(&dev->lock);
341 return 0;
06470ed6 342 }
f1557ceb 343
06470ed6 344 return -EINVAL;
1da177e4
LT
345}
346
2710e6aa
HV
347static const struct v4l2_file_operations maxiradio_fops = {
348 .owner = THIS_MODULE,
2710e6aa
HV
349 .ioctl = video_ioctl2,
350};
351
a399810c 352static const struct v4l2_ioctl_ops maxiradio_ioctl_ops = {
06470ed6
MCC
353 .vidioc_querycap = vidioc_querycap,
354 .vidioc_g_tuner = vidioc_g_tuner,
355 .vidioc_s_tuner = vidioc_s_tuner,
140dcc46
MCC
356 .vidioc_g_audio = vidioc_g_audio,
357 .vidioc_s_audio = vidioc_s_audio,
a0c05ab9
MCC
358 .vidioc_g_input = vidioc_g_input,
359 .vidioc_s_input = vidioc_s_input,
06470ed6
MCC
360 .vidioc_g_frequency = vidioc_g_frequency,
361 .vidioc_s_frequency = vidioc_s_frequency,
362 .vidioc_queryctrl = vidioc_queryctrl,
363 .vidioc_g_ctrl = vidioc_g_ctrl,
364 .vidioc_s_ctrl = vidioc_s_ctrl,
06470ed6 365};
1da177e4
LT
366
367static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
368{
2710e6aa
HV
369 struct maxiradio *dev;
370 struct v4l2_device *v4l2_dev;
371 int retval = -ENOMEM;
372
373 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
374 if (dev == NULL) {
375 dev_err(&pdev->dev, "not enough memory\n");
376 return -ENOMEM;
377 }
378
379 v4l2_dev = &dev->v4l2_dev;
380 mutex_init(&dev->lock);
381 dev->pdev = pdev;
382 dev->muted = 1;
383 dev->freq = FREQ_LO;
384
385 strlcpy(v4l2_dev->name, "maxiradio", sizeof(v4l2_dev->name));
386
387 retval = v4l2_device_register(&pdev->dev, v4l2_dev);
388 if (retval < 0) {
389 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
390 goto errfr;
391 }
392
393 if (!request_region(pci_resource_start(pdev, 0),
4286c6f6 394 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
2710e6aa 395 v4l2_err(v4l2_dev, "can't reserve I/O ports\n");
4286c6f6 396 goto err_out;
1da177e4
LT
397 }
398
399 if (pci_enable_device(pdev))
4286c6f6 400 goto err_out_free_region;
1da177e4 401
2710e6aa
HV
402 dev->io = pci_resource_start(pdev, 0);
403 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
404 dev->vdev.v4l2_dev = v4l2_dev;
405 dev->vdev.fops = &maxiradio_fops;
406 dev->vdev.ioctl_ops = &maxiradio_ioctl_ops;
407 dev->vdev.release = video_device_release_empty;
408 video_set_drvdata(&dev->vdev, dev);
1da177e4 409
2710e6aa
HV
410 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
411 v4l2_err(v4l2_dev, "can't register device!");
4286c6f6 412 goto err_out_free_region;
1da177e4
LT
413 }
414
2710e6aa
HV
415 v4l2_info(v4l2_dev, "version " DRIVER_VERSION
416 " time " __TIME__ " " __DATE__ "\n");
1da177e4 417
2710e6aa
HV
418 v4l2_info(v4l2_dev, "found Guillemot MAXI Radio device (io = 0x%x)\n",
419 dev->io);
1da177e4
LT
420 return 0;
421
422err_out_free_region:
423 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
424err_out:
2710e6aa
HV
425 v4l2_device_unregister(v4l2_dev);
426errfr:
427 kfree(dev);
1da177e4
LT
428 return -ENODEV;
429}
430
431static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
432{
2710e6aa
HV
433 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
434 struct maxiradio *dev = to_maxiradio(v4l2_dev);
435
436 video_unregister_device(&dev->vdev);
437 v4l2_device_unregister(&dev->v4l2_dev);
1da177e4
LT
438 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
439}
440
441static struct pci_device_id maxiradio_pci_tbl[] = {
442 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
443 PCI_ANY_ID, PCI_ANY_ID, },
2710e6aa 444 { 0 }
1da177e4
LT
445};
446
447MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
448
449static struct pci_driver maxiradio_driver = {
450 .name = "radio-maxiradio",
451 .id_table = maxiradio_pci_tbl,
452 .probe = maxiradio_init_one,
453 .remove = __devexit_p(maxiradio_remove_one),
454};
455
456static int __init maxiradio_radio_init(void)
457{
9bfab8ce 458 return pci_register_driver(&maxiradio_driver);
1da177e4
LT
459}
460
461static void __exit maxiradio_radio_exit(void)
462{
463 pci_unregister_driver(&maxiradio_driver);
464}
465
466module_init(maxiradio_radio_init);
467module_exit(maxiradio_radio_exit);