Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6-block.git] / drivers / media / radio / radio-maestro.c
CommitLineData
1da177e4
LT
1/* Maestro PCI sound card radio driver for Linux support
2 * (c) 2000 A. Tlalka, atlka@pg.gda.pl
3 * Notes on the hardware
4 *
4286c6f6 5 * + Frequency control is done digitally
1da177e4
LT
6 * + No volume control - only mute/unmute - you have to use Aux line volume
7 * control on Maestro card to set the volume
8 * + Radio status (tuned/not_tuned and stereo/mono) is valid some time after
9 * frequency setting (>100ms) and only when the radio is unmuted.
10 * version 0.02
11 * + io port is automatically detected - only the first radio is used
12 * version 0.03
13 * + thread access locking additions
14 * version 0.04
15 * + code improvements
16 * + VIDEO_TUNER_LOW is permanent
b6055d7b
MCC
17 *
18 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
1da177e4
LT
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
24#include <linux/delay.h>
1da177e4
LT
25#include <asm/io.h>
26#include <asm/uaccess.h>
1da177e4 27#include <linux/pci.h>
b6055d7b 28#include <linux/videodev2.h>
5e87efa3 29#include <media/v4l2-common.h>
35ea11ff 30#include <media/v4l2-ioctl.h>
3593cab5 31
b6055d7b
MCC
32#include <linux/version.h> /* for KERNEL_VERSION MACRO */
33#define RADIO_VERSION KERNEL_VERSION(0,0,6)
34#define DRIVER_VERSION "0.06"
35
36static struct v4l2_queryctrl radio_qctrl[] = {
37 {
38 .id = V4L2_CID_AUDIO_MUTE,
39 .name = "Mute",
40 .minimum = 0,
41 .maximum = 1,
42 .default_value = 1,
43 .type = V4L2_CTRL_TYPE_BOOLEAN,
44 }
45};
1da177e4 46
6a2cf8ee 47#define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
1da177e4
LT
48
49#define IO_MASK 4 /* mask register offset from GPIO_DATA
50 bits 1=unmask write to given bit */
51#define IO_DIR 8 /* direction register offset from GPIO_DATA
52 bits 0/1=read/write direction */
53
6a2cf8ee
JS
54#define GPIO6 0x0040 /* mask bits for GPIO lines */
55#define GPIO7 0x0080
56#define GPIO8 0x0100
57#define GPIO9 0x0200
1da177e4 58
6a2cf8ee
JS
59#define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
60#define STR_CLK GPIO7
61#define STR_WREN GPIO8
62#define STR_MOST GPIO9
1da177e4
LT
63
64#define FREQ_LO 50*16000
65#define FREQ_HI 150*16000
66
6a2cf8ee
JS
67#define FREQ_IF 171200 /* 10.7*16000 */
68#define FREQ_STEP 200 /* 12.5*16 */
1da177e4
LT
69
70#define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
71 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
72
73#define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
74
75static int radio_nr = -1;
76module_param(radio_nr, int, 0);
77
3ca685aa
HV
78static unsigned long in_use;
79
89dad8f0 80static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
3ca685aa
HV
81
82static int maestro_exclusive_open(struct inode *inode, struct file *file)
83{
84 return test_and_set_bit(0, &in_use) ? -EBUSY : 0;
85}
86
87static int maestro_exclusive_release(struct inode *inode, struct file *file)
88{
89 clear_bit(0, &in_use);
90 return 0;
91}
92
89dad8f0
JS
93static void maestro_remove(struct pci_dev *pdev);
94
95static struct pci_device_id maestro_r_pci_tbl[] = {
96 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
97 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
98 .class_mask = 0xffff00 },
99 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
100 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
101 .class_mask = 0xffff00 },
102 { 0 }
103};
104MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
105
106static struct pci_driver maestro_r_driver = {
107 .name = "maestro_radio",
108 .id_table = maestro_r_pci_tbl,
109 .probe = maestro_probe,
110 .remove = __devexit_p(maestro_remove),
111};
1da177e4 112
fa027c2a 113static const struct file_operations maestro_fops = {
1da177e4 114 .owner = THIS_MODULE,
3ca685aa
HV
115 .open = maestro_exclusive_open,
116 .release = maestro_exclusive_release,
d455cf5d 117 .ioctl = video_ioctl2,
078ff795 118#ifdef CONFIG_COMPAT
0d0fbf81 119 .compat_ioctl = v4l_compat_ioctl32,
078ff795 120#endif
1da177e4
LT
121 .llseek = no_llseek,
122};
123
6a2cf8ee 124struct radio_device {
0eaa21fd 125 u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
1da177e4 126 muted, /* VIDEO_AUDIO_MUTE */
4286c6f6 127 stereo, /* VIDEO_TUNER_STEREO_ON */
1da177e4 128 tuned; /* signal strength (0 or 0xffff) */
89dad8f0 129};
1da177e4 130
0eaa21fd 131static u32 radio_bits_get(struct radio_device *dev)
1da177e4 132{
0eaa21fd
JS
133 register u16 io=dev->io, l, rdata;
134 register u32 data=0;
135 u16 omask;
6a2cf8ee 136
1da177e4
LT
137 omask = inw(io + IO_MASK);
138 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
139 outw(0, io);
140 udelay(16);
141
142 for (l=24;l--;) {
143 outw(STR_CLK, io); /* HI state */
144 udelay(2);
4286c6f6 145 if(!l)
1da177e4
LT
146 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
147 outw(0, io); /* LO state */
148 udelay(2);
149 data <<= 1; /* shift data */
150 rdata = inw(io);
151 if(!l)
4286c6f6 152 dev->stereo = rdata & STR_MOST ?
b6055d7b 153 0 : 1;
1da177e4
LT
154 else
155 if(rdata & STR_DATA)
156 data++;
157 udelay(2);
158 }
6a2cf8ee 159
1da177e4
LT
160 if(dev->muted)
161 outw(STR_WREN, io);
6a2cf8ee 162
1da177e4
LT
163 udelay(4);
164 outw(omask, io + IO_MASK);
6a2cf8ee 165
1da177e4
LT
166 return data & 0x3ffe;
167}
168
0eaa21fd 169static void radio_bits_set(struct radio_device *dev, u32 data)
1da177e4 170{
0eaa21fd
JS
171 register u16 io=dev->io, l, bits;
172 u16 omask, odir;
6a2cf8ee 173
1da177e4
LT
174 omask = inw(io + IO_MASK);
175 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
176 outw(odir | STR_DATA, io + IO_DIR);
177 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
178 udelay(16);
179 for (l=25;l;l--) {
180 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
181 data <<= 1; /* shift data */
182 outw(bits, io); /* start strobe */
183 udelay(2);
184 outw(bits | STR_CLK, io); /* HI level */
185 udelay(2);
186 outw(bits, io); /* LO level */
187 udelay(4);
188 }
6a2cf8ee 189
1da177e4
LT
190 if(!dev->muted)
191 outw(0, io);
6a2cf8ee 192
1da177e4
LT
193 udelay(4);
194 outw(omask, io + IO_MASK);
195 outw(odir, io + IO_DIR);
196 msleep(125);
197}
198
d455cf5d
DL
199static int vidioc_querycap(struct file *file, void *priv,
200 struct v4l2_capability *v)
201{
202 strlcpy(v->driver, "radio-maestro", sizeof(v->driver));
203 strlcpy(v->card, "Maestro Radio", sizeof(v->card));
204 sprintf(v->bus_info, "PCI");
205 v->version = RADIO_VERSION;
206 v->capabilities = V4L2_CAP_TUNER;
207 return 0;
208}
209
210static int vidioc_g_tuner(struct file *file, void *priv,
211 struct v4l2_tuner *v)
1da177e4 212{
c170ecf4 213 struct radio_device *card = video_drvdata(file);
6a2cf8ee 214
d455cf5d
DL
215 if (v->index > 0)
216 return -EINVAL;
217
218 (void)radio_bits_get(card);
219
220 strcpy(v->name, "FM");
221 v->type = V4L2_TUNER_RADIO;
222 v->rangelow = FREQ_LO;
223 v->rangehigh = FREQ_HI;
224 v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
225 v->capability = V4L2_TUNER_CAP_LOW;
226 if(card->stereo)
227 v->audmode = V4L2_TUNER_MODE_STEREO;
228 else
229 v->audmode = V4L2_TUNER_MODE_MONO;
230 v->signal = card->tuned;
231 return 0;
232}
b6055d7b 233
d455cf5d
DL
234static int vidioc_s_tuner(struct file *file, void *priv,
235 struct v4l2_tuner *v)
236{
237 if (v->index > 0)
238 return -EINVAL;
239 return 0;
240}
b6055d7b 241
d455cf5d
DL
242static int vidioc_s_frequency(struct file *file, void *priv,
243 struct v4l2_frequency *f)
244{
c170ecf4 245 struct radio_device *card = video_drvdata(file);
b6055d7b 246
d455cf5d
DL
247 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
248 return -EINVAL;
249 radio_bits_set(card, FREQ2BITS(f->frequency));
250 return 0;
251}
b6055d7b 252
d455cf5d
DL
253static int vidioc_g_frequency(struct file *file, void *priv,
254 struct v4l2_frequency *f)
255{
c170ecf4 256 struct radio_device *card = video_drvdata(file);
b6055d7b 257
d455cf5d
DL
258 f->type = V4L2_TUNER_RADIO;
259 f->frequency = BITS2FREQ(radio_bits_get(card));
260 return 0;
261}
b6055d7b 262
d455cf5d
DL
263static int vidioc_queryctrl(struct file *file, void *priv,
264 struct v4l2_queryctrl *qc)
265{
266 int i;
b6055d7b 267
d455cf5d
DL
268 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
269 if (qc->id && qc->id == radio_qctrl[i].id) {
270 memcpy(qc, &(radio_qctrl[i]),
271 sizeof(*qc));
b6055d7b
MCC
272 return 0;
273 }
d455cf5d
DL
274 }
275 return -EINVAL;
276}
b6055d7b 277
d455cf5d
DL
278static int vidioc_g_ctrl(struct file *file, void *priv,
279 struct v4l2_control *ctrl)
280{
c170ecf4 281 struct radio_device *card = video_drvdata(file);
b6055d7b 282
d455cf5d
DL
283 switch (ctrl->id) {
284 case V4L2_CID_AUDIO_MUTE:
285 ctrl->value = card->muted;
286 return 0;
1da177e4 287 }
d455cf5d 288 return -EINVAL;
1da177e4
LT
289}
290
d455cf5d
DL
291static int vidioc_s_ctrl(struct file *file, void *priv,
292 struct v4l2_control *ctrl)
1da177e4 293{
c170ecf4 294 struct radio_device *card = video_drvdata(file);
d455cf5d
DL
295 register u16 io = card->io;
296 register u16 omask = inw(io + IO_MASK);
297
298 switch (ctrl->id) {
299 case V4L2_CID_AUDIO_MUTE:
300 outw(~STR_WREN, io + IO_MASK);
301 outw((card->muted = ctrl->value ) ?
302 STR_WREN : 0, io);
303 udelay(4);
304 outw(omask, io + IO_MASK);
305 msleep(125);
306 return 0;
307 }
308 return -EINVAL;
309}
1da177e4 310
d455cf5d
DL
311static int vidioc_g_audio(struct file *file, void *priv,
312 struct v4l2_audio *a)
313{
314 if (a->index > 1)
315 return -EINVAL;
316
317 strcpy(a->name, "Radio");
318 a->capability = V4L2_AUDCAP_STEREO;
319 return 0;
320}
6a2cf8ee 321
d455cf5d
DL
322static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
323{
324 *i = 0;
325 return 0;
326}
327
328static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
329{
330 if (i != 0)
331 return -EINVAL;
332 return 0;
333}
334
335static int vidioc_s_audio(struct file *file, void *priv,
336 struct v4l2_audio *a)
337{
338 if (a->index != 0)
339 return -EINVAL;
340 return 0;
1da177e4
LT
341}
342
0eaa21fd 343static u16 __devinit radio_power_on(struct radio_device *dev)
1da177e4 344{
0eaa21fd
JS
345 register u16 io = dev->io;
346 register u32 ofreq;
347 u16 omask, odir;
6a2cf8ee 348
1da177e4 349 omask = inw(io + IO_MASK);
6a2cf8ee 350 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
1da177e4 351 outw(odir & ~STR_WREN, io + IO_DIR);
b6055d7b 352 dev->muted = inw(io) & STR_WREN ? 0 : 1;
1da177e4
LT
353 outw(odir, io + IO_DIR);
354 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
355 outw(dev->muted ? 0 : STR_WREN, io);
356 udelay(16);
357 outw(omask, io + IO_MASK);
358 ofreq = radio_bits_get(dev);
6a2cf8ee
JS
359
360 if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
1da177e4
LT
361 ofreq = FREQ2BITS(FREQ_LO);
362 radio_bits_set(dev, ofreq);
6a2cf8ee 363
1da177e4
LT
364 return (ofreq == radio_bits_get(dev));
365}
366
a399810c 367static const struct v4l2_ioctl_ops maestro_ioctl_ops = {
d455cf5d
DL
368 .vidioc_querycap = vidioc_querycap,
369 .vidioc_g_tuner = vidioc_g_tuner,
370 .vidioc_s_tuner = vidioc_s_tuner,
371 .vidioc_g_audio = vidioc_g_audio,
372 .vidioc_s_audio = vidioc_s_audio,
373 .vidioc_g_input = vidioc_g_input,
374 .vidioc_s_input = vidioc_s_input,
375 .vidioc_g_frequency = vidioc_g_frequency,
376 .vidioc_s_frequency = vidioc_s_frequency,
377 .vidioc_queryctrl = vidioc_queryctrl,
378 .vidioc_g_ctrl = vidioc_g_ctrl,
379 .vidioc_s_ctrl = vidioc_s_ctrl,
380};
381
a399810c
HV
382static struct video_device maestro_radio = {
383 .name = "Maestro radio",
a399810c
HV
384 .fops = &maestro_fops,
385 .ioctl_ops = &maestro_ioctl_ops,
aa5e90af 386 .release = video_device_release,
a399810c
HV
387};
388
89dad8f0
JS
389static int __devinit maestro_probe(struct pci_dev *pdev,
390 const struct pci_device_id *ent)
1da177e4 391{
89dad8f0
JS
392 struct radio_device *radio_unit;
393 struct video_device *maestro_radio_inst;
394 int retval;
395
396 retval = pci_enable_device(pdev);
397 if (retval) {
398 dev_err(&pdev->dev, "enabling pci device failed!\n");
399 goto err;
400 }
401
402 retval = -ENOMEM;
403
404 radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
405 if (radio_unit == NULL) {
406 dev_err(&pdev->dev, "not enough memory\n");
407 goto err;
408 }
409
410 radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
89dad8f0
JS
411
412 maestro_radio_inst = video_device_alloc();
413 if (maestro_radio_inst == NULL) {
414 dev_err(&pdev->dev, "not enough memory\n");
415 goto errfr;
416 }
417
418 memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
419 video_set_drvdata(maestro_radio_inst, radio_unit);
420 pci_set_drvdata(pdev, maestro_radio_inst);
421
cba99ae8 422 retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO, radio_nr);
89dad8f0
JS
423 if (retval) {
424 printk(KERN_ERR "can't register video device!\n");
425 goto errfr1;
426 }
427
428 if (!radio_power_on(radio_unit)) {
429 retval = -EIO;
430 goto errunr;
431 }
432
433 dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
6a2cf8ee 434 __DATE__ "\n");
89dad8f0
JS
435 dev_info(&pdev->dev, "radio chip initialized\n");
436
437 return 0;
438errunr:
439 video_unregister_device(maestro_radio_inst);
440errfr1:
f37fdf3f 441 video_device_release(maestro_radio_inst);
89dad8f0
JS
442errfr:
443 kfree(radio_unit);
444err:
445 return retval;
446
447}
448
449static void __devexit maestro_remove(struct pci_dev *pdev)
450{
451 struct video_device *vdev = pci_get_drvdata(pdev);
452
453 video_unregister_device(vdev);
1da177e4
LT
454}
455
89dad8f0
JS
456static int __init maestro_radio_init(void)
457{
458 int retval = pci_register_driver(&maestro_r_driver);
459
460 if (retval)
461 printk(KERN_ERR "error during registration pci driver\n");
462
463 return retval;
464}
465
466static void __exit maestro_radio_exit(void)
467{
468 pci_unregister_driver(&maestro_r_driver);
469}
470
471module_init(maestro_radio_init);
472module_exit(maestro_radio_exit);
6a2cf8ee
JS
473
474MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
475MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
476MODULE_LICENSE("GPL");