1 /* Maestro PCI sound card radio driver for Linux support
2 * (c) 2000 A. Tlalka, atlka@pg.gda.pl
3 * Notes on the hardware
5 * + Frequency control is done digitally
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.
11 * + io port is automatically detected - only the first radio is used
13 * + thread access locking additions
16 * + VIDEO_TUNER_LOW is permanent
18 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/ioport.h>
24 #include <linux/delay.h>
26 #include <asm/uaccess.h>
27 #include <linux/pci.h>
28 #include <linux/videodev2.h>
29 #include <media/v4l2-common.h>
31 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
32 #define RADIO_VERSION KERNEL_VERSION(0,0,6)
33 #define DRIVER_VERSION "0.06"
35 static struct v4l2_queryctrl radio_qctrl[] = {
37 .id = V4L2_CID_AUDIO_MUTE,
42 .type = V4L2_CTRL_TYPE_BOOLEAN,
46 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
48 #define IO_MASK 4 /* mask register offset from GPIO_DATA
49 bits 1=unmask write to given bit */
50 #define IO_DIR 8 /* direction register offset from GPIO_DATA
51 bits 0/1=read/write direction */
53 #define GPIO6 0x0040 /* mask bits for GPIO lines */
58 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
60 #define STR_WREN GPIO8
61 #define STR_MOST GPIO9
63 #define FREQ_LO 50*16000
64 #define FREQ_HI 150*16000
66 #define FREQ_IF 171200 /* 10.7*16000 */
67 #define FREQ_STEP 200 /* 12.5*16 */
69 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
70 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
72 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
74 static int radio_nr = -1;
75 module_param(radio_nr, int, 0);
77 static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
78 static void maestro_remove(struct pci_dev *pdev);
80 static struct pci_device_id maestro_r_pci_tbl[] = {
81 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
82 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
83 .class_mask = 0xffff00 },
84 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
85 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
86 .class_mask = 0xffff00 },
89 MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
91 static struct pci_driver maestro_r_driver = {
92 .name = "maestro_radio",
93 .id_table = maestro_r_pci_tbl,
94 .probe = maestro_probe,
95 .remove = __devexit_p(maestro_remove),
98 static const struct file_operations maestro_fops = {
100 .open = video_exclusive_open,
101 .release = video_exclusive_release,
102 .ioctl = video_ioctl2,
104 .compat_ioctl = v4l_compat_ioctl32,
109 struct radio_device {
110 u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
111 muted, /* VIDEO_AUDIO_MUTE */
112 stereo, /* VIDEO_TUNER_STEREO_ON */
113 tuned; /* signal strength (0 or 0xffff) */
116 static u32 radio_bits_get(struct radio_device *dev)
118 register u16 io=dev->io, l, rdata;
122 omask = inw(io + IO_MASK);
123 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
128 outw(STR_CLK, io); /* HI state */
131 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
132 outw(0, io); /* LO state */
134 data <<= 1; /* shift data */
137 dev->stereo = rdata & STR_MOST ?
149 outw(omask, io + IO_MASK);
151 return data & 0x3ffe;
154 static void radio_bits_set(struct radio_device *dev, u32 data)
156 register u16 io=dev->io, l, bits;
159 omask = inw(io + IO_MASK);
160 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
161 outw(odir | STR_DATA, io + IO_DIR);
162 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
165 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
166 data <<= 1; /* shift data */
167 outw(bits, io); /* start strobe */
169 outw(bits | STR_CLK, io); /* HI level */
171 outw(bits, io); /* LO level */
179 outw(omask, io + IO_MASK);
180 outw(odir, io + IO_DIR);
184 static int vidioc_querycap(struct file *file, void *priv,
185 struct v4l2_capability *v)
187 strlcpy(v->driver, "radio-maestro", sizeof(v->driver));
188 strlcpy(v->card, "Maestro Radio", sizeof(v->card));
189 sprintf(v->bus_info, "PCI");
190 v->version = RADIO_VERSION;
191 v->capabilities = V4L2_CAP_TUNER;
195 static int vidioc_g_tuner(struct file *file, void *priv,
196 struct v4l2_tuner *v)
198 struct video_device *dev = video_devdata(file);
199 struct radio_device *card = video_get_drvdata(dev);
204 (void)radio_bits_get(card);
206 strcpy(v->name, "FM");
207 v->type = V4L2_TUNER_RADIO;
208 v->rangelow = FREQ_LO;
209 v->rangehigh = FREQ_HI;
210 v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
211 v->capability = V4L2_TUNER_CAP_LOW;
213 v->audmode = V4L2_TUNER_MODE_STEREO;
215 v->audmode = V4L2_TUNER_MODE_MONO;
216 v->signal = card->tuned;
220 static int vidioc_s_tuner(struct file *file, void *priv,
221 struct v4l2_tuner *v)
228 static int vidioc_s_frequency(struct file *file, void *priv,
229 struct v4l2_frequency *f)
231 struct video_device *dev = video_devdata(file);
232 struct radio_device *card = video_get_drvdata(dev);
234 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
236 radio_bits_set(card, FREQ2BITS(f->frequency));
240 static int vidioc_g_frequency(struct file *file, void *priv,
241 struct v4l2_frequency *f)
243 struct video_device *dev = video_devdata(file);
244 struct radio_device *card = video_get_drvdata(dev);
246 f->type = V4L2_TUNER_RADIO;
247 f->frequency = BITS2FREQ(radio_bits_get(card));
251 static int vidioc_queryctrl(struct file *file, void *priv,
252 struct v4l2_queryctrl *qc)
256 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
257 if (qc->id && qc->id == radio_qctrl[i].id) {
258 memcpy(qc, &(radio_qctrl[i]),
266 static int vidioc_g_ctrl(struct file *file, void *priv,
267 struct v4l2_control *ctrl)
269 struct video_device *dev = video_devdata(file);
270 struct radio_device *card = video_get_drvdata(dev);
273 case V4L2_CID_AUDIO_MUTE:
274 ctrl->value = card->muted;
280 static int vidioc_s_ctrl(struct file *file, void *priv,
281 struct v4l2_control *ctrl)
283 struct video_device *dev = video_devdata(file);
284 struct radio_device *card = video_get_drvdata(dev);
285 register u16 io = card->io;
286 register u16 omask = inw(io + IO_MASK);
289 case V4L2_CID_AUDIO_MUTE:
290 outw(~STR_WREN, io + IO_MASK);
291 outw((card->muted = ctrl->value ) ?
294 outw(omask, io + IO_MASK);
301 static int vidioc_g_audio(struct file *file, void *priv,
302 struct v4l2_audio *a)
307 strcpy(a->name, "Radio");
308 a->capability = V4L2_AUDCAP_STEREO;
312 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
318 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
325 static int vidioc_s_audio(struct file *file, void *priv,
326 struct v4l2_audio *a)
333 static u16 __devinit radio_power_on(struct radio_device *dev)
335 register u16 io = dev->io;
339 omask = inw(io + IO_MASK);
340 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
341 outw(odir & ~STR_WREN, io + IO_DIR);
342 dev->muted = inw(io) & STR_WREN ? 0 : 1;
343 outw(odir, io + IO_DIR);
344 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
345 outw(dev->muted ? 0 : STR_WREN, io);
347 outw(omask, io + IO_MASK);
348 ofreq = radio_bits_get(dev);
350 if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
351 ofreq = FREQ2BITS(FREQ_LO);
352 radio_bits_set(dev, ofreq);
354 return (ofreq == radio_bits_get(dev));
357 static struct video_device maestro_radio = {
358 .name = "Maestro radio",
359 .type = VID_TYPE_TUNER,
360 .fops = &maestro_fops,
361 .vidioc_querycap = vidioc_querycap,
362 .vidioc_g_tuner = vidioc_g_tuner,
363 .vidioc_s_tuner = vidioc_s_tuner,
364 .vidioc_g_audio = vidioc_g_audio,
365 .vidioc_s_audio = vidioc_s_audio,
366 .vidioc_g_input = vidioc_g_input,
367 .vidioc_s_input = vidioc_s_input,
368 .vidioc_g_frequency = vidioc_g_frequency,
369 .vidioc_s_frequency = vidioc_s_frequency,
370 .vidioc_queryctrl = vidioc_queryctrl,
371 .vidioc_g_ctrl = vidioc_g_ctrl,
372 .vidioc_s_ctrl = vidioc_s_ctrl,
375 static int __devinit maestro_probe(struct pci_dev *pdev,
376 const struct pci_device_id *ent)
378 struct radio_device *radio_unit;
379 struct video_device *maestro_radio_inst;
382 retval = pci_enable_device(pdev);
384 dev_err(&pdev->dev, "enabling pci device failed!\n");
390 radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
391 if (radio_unit == NULL) {
392 dev_err(&pdev->dev, "not enough memory\n");
396 radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
398 maestro_radio_inst = video_device_alloc();
399 if (maestro_radio_inst == NULL) {
400 dev_err(&pdev->dev, "not enough memory\n");
404 memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
405 video_set_drvdata(maestro_radio_inst, radio_unit);
406 pci_set_drvdata(pdev, maestro_radio_inst);
408 retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
411 printk(KERN_ERR "can't register video device!\n");
415 if (!radio_power_on(radio_unit)) {
420 dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
422 dev_info(&pdev->dev, "radio chip initialized\n");
426 video_unregister_device(maestro_radio_inst);
428 video_device_release(maestro_radio_inst);
436 static void __devexit maestro_remove(struct pci_dev *pdev)
438 struct video_device *vdev = pci_get_drvdata(pdev);
440 video_unregister_device(vdev);
443 static int __init maestro_radio_init(void)
445 int retval = pci_register_driver(&maestro_r_driver);
448 printk(KERN_ERR "error during registration pci driver\n");
453 static void __exit maestro_radio_exit(void)
455 pci_unregister_driver(&maestro_r_driver);
458 module_init(maestro_radio_init);
459 module_exit(maestro_radio_exit);
461 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
462 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
463 MODULE_LICENSE("GPL");