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>
25 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
26 #include <linux/pci.h>
27 #include <linux/videodev2.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
32 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
33 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
34 MODULE_LICENSE("GPL");
36 static int radio_nr = -1;
37 module_param(radio_nr, int, 0);
39 #define RADIO_VERSION KERNEL_VERSION(0, 0, 6)
40 #define DRIVER_VERSION "0.06"
42 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
44 #define IO_MASK 4 /* mask register offset from GPIO_DATA
45 bits 1=unmask write to given bit */
46 #define IO_DIR 8 /* direction register offset from GPIO_DATA
47 bits 0/1=read/write direction */
49 #define GPIO6 0x0040 /* mask bits for GPIO lines */
54 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
56 #define STR_WREN GPIO8
57 #define STR_MOST GPIO9
59 #define FREQ_LO 50*16000
60 #define FREQ_HI 150*16000
62 #define FREQ_IF 171200 /* 10.7*16000 */
63 #define FREQ_STEP 200 /* 12.5*16 */
65 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
66 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
68 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
71 struct v4l2_device v4l2_dev;
72 struct video_device vdev;
76 u16 io; /* base of Maestro card radio io (GPIO_DATA)*/
77 u16 muted; /* VIDEO_AUDIO_MUTE */
78 u16 stereo; /* VIDEO_TUNER_STEREO_ON */
79 u16 tuned; /* signal strength (0 or 0xffff) */
82 static inline struct maestro *to_maestro(struct v4l2_device *v4l2_dev)
84 return container_of(v4l2_dev, struct maestro, v4l2_dev);
87 static u32 radio_bits_get(struct maestro *dev)
89 u16 io = dev->io, l, rdata;
93 omask = inw(io + IO_MASK);
94 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
99 outw(STR_CLK, io); /* HI state */
102 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
103 outw(0, io); /* LO state */
105 data <<= 1; /* shift data */
108 dev->stereo = (rdata & STR_MOST) ? 0 : 1;
109 else if (rdata & STR_DATA)
118 outw(omask, io + IO_MASK);
120 return data & 0x3ffe;
123 static void radio_bits_set(struct maestro *dev, u32 data)
125 u16 io = dev->io, l, bits;
128 omask = inw(io + IO_MASK);
129 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
130 outw(odir | STR_DATA, io + IO_DIR);
131 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
133 for (l = 25; l; l--) {
134 bits = ((data >> 18) & STR_DATA) | STR_WREN;
135 data <<= 1; /* shift data */
136 outw(bits, io); /* start strobe */
138 outw(bits | STR_CLK, io); /* HI level */
140 outw(bits, io); /* LO level */
148 outw(omask, io + IO_MASK);
149 outw(odir, io + IO_DIR);
153 static int vidioc_querycap(struct file *file, void *priv,
154 struct v4l2_capability *v)
156 struct maestro *dev = video_drvdata(file);
158 strlcpy(v->driver, "radio-maestro", sizeof(v->driver));
159 strlcpy(v->card, "Maestro Radio", sizeof(v->card));
160 snprintf(v->bus_info, sizeof(v->bus_info), "PCI:%s", pci_name(dev->pdev));
161 v->version = RADIO_VERSION;
162 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
166 static int vidioc_g_tuner(struct file *file, void *priv,
167 struct v4l2_tuner *v)
169 struct maestro *dev = video_drvdata(file);
174 mutex_lock(&dev->lock);
177 strlcpy(v->name, "FM", sizeof(v->name));
178 v->type = V4L2_TUNER_RADIO;
179 v->rangelow = FREQ_LO;
180 v->rangehigh = FREQ_HI;
181 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
182 v->capability = V4L2_TUNER_CAP_LOW;
184 v->audmode = V4L2_TUNER_MODE_STEREO;
186 v->audmode = V4L2_TUNER_MODE_MONO;
187 v->signal = dev->tuned;
188 mutex_unlock(&dev->lock);
192 static int vidioc_s_tuner(struct file *file, void *priv,
193 struct v4l2_tuner *v)
195 return v->index ? -EINVAL : 0;
198 static int vidioc_s_frequency(struct file *file, void *priv,
199 struct v4l2_frequency *f)
201 struct maestro *dev = video_drvdata(file);
203 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
205 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
207 mutex_lock(&dev->lock);
208 radio_bits_set(dev, FREQ2BITS(f->frequency));
209 mutex_unlock(&dev->lock);
213 static int vidioc_g_frequency(struct file *file, void *priv,
214 struct v4l2_frequency *f)
216 struct maestro *dev = video_drvdata(file);
220 f->type = V4L2_TUNER_RADIO;
221 mutex_lock(&dev->lock);
222 f->frequency = BITS2FREQ(radio_bits_get(dev));
223 mutex_unlock(&dev->lock);
227 static int vidioc_queryctrl(struct file *file, void *priv,
228 struct v4l2_queryctrl *qc)
231 case V4L2_CID_AUDIO_MUTE:
232 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
237 static int vidioc_g_ctrl(struct file *file, void *priv,
238 struct v4l2_control *ctrl)
240 struct maestro *dev = video_drvdata(file);
243 case V4L2_CID_AUDIO_MUTE:
244 ctrl->value = dev->muted;
250 static int vidioc_s_ctrl(struct file *file, void *priv,
251 struct v4l2_control *ctrl)
253 struct maestro *dev = video_drvdata(file);
258 case V4L2_CID_AUDIO_MUTE:
259 mutex_lock(&dev->lock);
260 omask = inw(io + IO_MASK);
261 outw(~STR_WREN, io + IO_MASK);
262 dev->muted = ctrl->value;
263 outw(dev->muted ? STR_WREN : 0, io);
265 outw(omask, io + IO_MASK);
267 mutex_unlock(&dev->lock);
273 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
279 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
281 return i ? -EINVAL : 0;
284 static int vidioc_g_audio(struct file *file, void *priv,
285 struct v4l2_audio *a)
288 strlcpy(a->name, "Radio", sizeof(a->name));
289 a->capability = V4L2_AUDCAP_STEREO;
293 static int vidioc_s_audio(struct file *file, void *priv,
294 struct v4l2_audio *a)
296 return a->index ? -EINVAL : 0;
299 static const struct v4l2_file_operations maestro_fops = {
300 .owner = THIS_MODULE,
301 .ioctl = video_ioctl2,
304 static const struct v4l2_ioctl_ops maestro_ioctl_ops = {
305 .vidioc_querycap = vidioc_querycap,
306 .vidioc_g_tuner = vidioc_g_tuner,
307 .vidioc_s_tuner = vidioc_s_tuner,
308 .vidioc_g_audio = vidioc_g_audio,
309 .vidioc_s_audio = vidioc_s_audio,
310 .vidioc_g_input = vidioc_g_input,
311 .vidioc_s_input = vidioc_s_input,
312 .vidioc_g_frequency = vidioc_g_frequency,
313 .vidioc_s_frequency = vidioc_s_frequency,
314 .vidioc_queryctrl = vidioc_queryctrl,
315 .vidioc_g_ctrl = vidioc_g_ctrl,
316 .vidioc_s_ctrl = vidioc_s_ctrl,
319 static u16 __devinit radio_power_on(struct maestro *dev)
321 register u16 io = dev->io;
325 omask = inw(io + IO_MASK);
326 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
327 outw(odir & ~STR_WREN, io + IO_DIR);
328 dev->muted = inw(io) & STR_WREN ? 0 : 1;
329 outw(odir, io + IO_DIR);
330 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
331 outw(dev->muted ? 0 : STR_WREN, io);
333 outw(omask, io + IO_MASK);
334 ofreq = radio_bits_get(dev);
336 if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
337 ofreq = FREQ2BITS(FREQ_LO);
338 radio_bits_set(dev, ofreq);
340 return (ofreq == radio_bits_get(dev));
343 static int __devinit maestro_probe(struct pci_dev *pdev,
344 const struct pci_device_id *ent)
347 struct v4l2_device *v4l2_dev;
350 retval = pci_enable_device(pdev);
352 dev_err(&pdev->dev, "enabling pci device failed!\n");
358 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
360 dev_err(&pdev->dev, "not enough memory\n");
364 v4l2_dev = &dev->v4l2_dev;
365 mutex_init(&dev->lock);
368 strlcpy(v4l2_dev->name, "maestro", sizeof(v4l2_dev->name));
370 retval = v4l2_device_register(&pdev->dev, v4l2_dev);
372 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
376 dev->io = pci_resource_start(pdev, 0) + GPIO_DATA;
378 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
379 dev->vdev.v4l2_dev = v4l2_dev;
380 dev->vdev.fops = &maestro_fops;
381 dev->vdev.ioctl_ops = &maestro_ioctl_ops;
382 dev->vdev.release = video_device_release_empty;
383 video_set_drvdata(&dev->vdev, dev);
385 retval = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
387 v4l2_err(v4l2_dev, "can't register video device!\n");
391 if (!radio_power_on(dev)) {
396 v4l2_info(v4l2_dev, "version " DRIVER_VERSION "\n");
400 video_unregister_device(&dev->vdev);
402 v4l2_device_unregister(v4l2_dev);
410 static void __devexit maestro_remove(struct pci_dev *pdev)
412 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
413 struct maestro *dev = to_maestro(v4l2_dev);
415 video_unregister_device(&dev->vdev);
416 v4l2_device_unregister(&dev->v4l2_dev);
419 static struct pci_device_id maestro_r_pci_tbl[] = {
420 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
421 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
422 .class_mask = 0xffff00 },
423 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
424 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
425 .class_mask = 0xffff00 },
428 MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
430 static struct pci_driver maestro_r_driver = {
431 .name = "maestro_radio",
432 .id_table = maestro_r_pci_tbl,
433 .probe = maestro_probe,
434 .remove = __devexit_p(maestro_remove),
437 static int __init maestro_radio_init(void)
439 int retval = pci_register_driver(&maestro_r_driver);
442 printk(KERN_ERR "error during registration pci driver\n");
447 static void __exit maestro_radio_exit(void)
449 pci_unregister_driver(&maestro_r_driver);
452 module_init(maestro_radio_init);
453 module_exit(maestro_radio_exit);