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/mutex.h>
28 #include <linux/pci.h>
29 #include <linux/videodev2.h>
30 #include <media/v4l2-common.h>
32 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
33 #define RADIO_VERSION KERNEL_VERSION(0,0,6)
34 #define DRIVER_VERSION "0.06"
36 static struct v4l2_queryctrl radio_qctrl[] = {
38 .id = V4L2_CID_AUDIO_MUTE,
43 .type = V4L2_CTRL_TYPE_BOOLEAN,
47 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
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 */
54 #define GPIO6 0x0040 /* mask bits for GPIO lines */
59 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
61 #define STR_WREN GPIO8
62 #define STR_MOST GPIO9
64 #define FREQ_LO 50*16000
65 #define FREQ_HI 150*16000
67 #define FREQ_IF 171200 /* 10.7*16000 */
68 #define FREQ_STEP 200 /* 12.5*16 */
70 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
71 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
73 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
75 static int radio_nr = -1;
76 module_param(radio_nr, int, 0);
78 static int radio_ioctl(struct inode *inode, struct file *file,
79 unsigned int cmd, unsigned long arg);
80 static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
81 static void maestro_remove(struct pci_dev *pdev);
83 static struct pci_device_id maestro_r_pci_tbl[] = {
84 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
85 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
86 .class_mask = 0xffff00 },
87 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
88 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
89 .class_mask = 0xffff00 },
92 MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
94 static struct pci_driver maestro_r_driver = {
95 .name = "maestro_radio",
96 .id_table = maestro_r_pci_tbl,
97 .probe = maestro_probe,
98 .remove = __devexit_p(maestro_remove),
101 static const struct file_operations maestro_fops = {
102 .owner = THIS_MODULE,
103 .open = video_exclusive_open,
104 .release = video_exclusive_release,
105 .ioctl = radio_ioctl,
106 .compat_ioctl = v4l_compat_ioctl32,
110 static struct video_device maestro_radio = {
111 .name = "Maestro radio",
112 .type = VID_TYPE_TUNER,
114 .fops = &maestro_fops,
117 struct radio_device {
118 u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
119 muted, /* VIDEO_AUDIO_MUTE */
120 stereo, /* VIDEO_TUNER_STEREO_ON */
121 tuned; /* signal strength (0 or 0xffff) */
125 static u32 radio_bits_get(struct radio_device *dev)
127 register u16 io=dev->io, l, rdata;
131 omask = inw(io + IO_MASK);
132 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
137 outw(STR_CLK, io); /* HI state */
140 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
141 outw(0, io); /* LO state */
143 data <<= 1; /* shift data */
146 dev->stereo = rdata & STR_MOST ?
158 outw(omask, io + IO_MASK);
160 return data & 0x3ffe;
163 static void radio_bits_set(struct radio_device *dev, u32 data)
165 register u16 io=dev->io, l, bits;
168 omask = inw(io + IO_MASK);
169 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
170 outw(odir | STR_DATA, io + IO_DIR);
171 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
174 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
175 data <<= 1; /* shift data */
176 outw(bits, io); /* start strobe */
178 outw(bits | STR_CLK, io); /* HI level */
180 outw(bits, io); /* LO level */
188 outw(omask, io + IO_MASK);
189 outw(odir, io + IO_DIR);
193 static inline int radio_function(struct inode *inode, struct file *file,
194 unsigned int cmd, void *arg)
196 struct video_device *dev = video_devdata(file);
197 struct radio_device *card = video_get_drvdata(dev);
200 case VIDIOC_QUERYCAP:
202 struct v4l2_capability *v = arg;
203 memset(v,0,sizeof(*v));
204 strlcpy(v->driver, "radio-maestro", sizeof (v->driver));
205 strlcpy(v->card, "Maestro Radio", sizeof (v->card));
206 sprintf(v->bus_info,"PCI");
207 v->version = RADIO_VERSION;
208 v->capabilities = V4L2_CAP_TUNER;
214 struct v4l2_tuner *v = arg;
219 (void)radio_bits_get(card);
221 memset(v,0,sizeof(*v));
222 strcpy(v->name, "FM");
223 v->type = V4L2_TUNER_RADIO;
225 v->rangelow = FREQ_LO;
226 v->rangehigh = FREQ_HI;
227 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
228 v->capability=V4L2_TUNER_CAP_LOW;
230 v->audmode = V4L2_TUNER_MODE_STEREO;
232 v->audmode = V4L2_TUNER_MODE_MONO;
233 v->signal=card->tuned;
239 struct v4l2_tuner *v = arg;
246 case VIDIOC_S_FREQUENCY:
248 struct v4l2_frequency *f = arg;
250 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
252 radio_bits_set(card, FREQ2BITS(f->frequency));
256 case VIDIOC_G_FREQUENCY:
258 struct v4l2_frequency *f = arg;
260 f->type = V4L2_TUNER_RADIO;
261 f->frequency = BITS2FREQ(radio_bits_get(card));
265 case VIDIOC_QUERYCTRL:
267 struct v4l2_queryctrl *qc = arg;
270 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
271 if (qc->id && qc->id == radio_qctrl[i].id) {
272 memcpy(qc, &(radio_qctrl[i]),
281 struct v4l2_control *ctrl= arg;
284 case V4L2_CID_AUDIO_MUTE:
285 ctrl->value=card->muted;
292 struct v4l2_control *ctrl= arg;
295 case V4L2_CID_AUDIO_MUTE:
297 register u16 io = card->io;
298 register u16 omask = inw(io + IO_MASK);
299 outw(~STR_WREN, io + IO_MASK);
300 outw((card->muted = ctrl->value ) ?
303 outw(omask, io + IO_MASK);
312 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
317 static int radio_ioctl(struct inode *inode, struct file *file,
318 unsigned int cmd, unsigned long arg)
320 struct video_device *dev = video_devdata(file);
321 struct radio_device *card = video_get_drvdata(dev);
324 mutex_lock(&card->lock);
325 ret = video_usercopy(inode, file, cmd, arg, radio_function);
326 mutex_unlock(&card->lock);
331 static u16 __devinit radio_power_on(struct radio_device *dev)
333 register u16 io = dev->io;
337 omask = inw(io + IO_MASK);
338 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
339 outw(odir & ~STR_WREN, io + IO_DIR);
340 dev->muted = inw(io) & STR_WREN ? 0 : 1;
341 outw(odir, io + IO_DIR);
342 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
343 outw(dev->muted ? 0 : STR_WREN, io);
345 outw(omask, io + IO_MASK);
346 ofreq = radio_bits_get(dev);
348 if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
349 ofreq = FREQ2BITS(FREQ_LO);
350 radio_bits_set(dev, ofreq);
352 return (ofreq == radio_bits_get(dev));
355 static int __devinit maestro_probe(struct pci_dev *pdev,
356 const struct pci_device_id *ent)
358 struct radio_device *radio_unit;
359 struct video_device *maestro_radio_inst;
362 retval = pci_enable_device(pdev);
364 dev_err(&pdev->dev, "enabling pci device failed!\n");
370 radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
371 if (radio_unit == NULL) {
372 dev_err(&pdev->dev, "not enough memory\n");
376 radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
377 mutex_init(&radio_unit->lock);
379 maestro_radio_inst = video_device_alloc();
380 if (maestro_radio_inst == NULL) {
381 dev_err(&pdev->dev, "not enough memory\n");
385 memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
386 video_set_drvdata(maestro_radio_inst, radio_unit);
387 pci_set_drvdata(pdev, maestro_radio_inst);
389 retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
392 printk(KERN_ERR "can't register video device!\n");
396 if (!radio_power_on(radio_unit)) {
401 dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
403 dev_info(&pdev->dev, "radio chip initialized\n");
407 video_unregister_device(maestro_radio_inst);
409 kfree(maestro_radio_inst);
417 static void __devexit maestro_remove(struct pci_dev *pdev)
419 struct video_device *vdev = pci_get_drvdata(pdev);
421 video_unregister_device(vdev);
424 static int __init maestro_radio_init(void)
426 int retval = pci_register_driver(&maestro_r_driver);
429 printk(KERN_ERR "error during registration pci driver\n");
434 static void __exit maestro_radio_exit(void)
436 pci_unregister_driver(&maestro_r_driver);
439 module_init(maestro_radio_init);
440 module_exit(maestro_radio_exit);
442 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
443 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
444 MODULE_LICENSE("GPL");