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
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/delay.h>
23 #include <linux/sched.h>
25 #include <asm/uaccess.h>
26 #include <asm/semaphore.h>
27 #include <linux/pci.h>
28 #include <linux/videodev.h>
30 #define DRIVER_VERSION "0.05"
32 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
34 #define IO_MASK 4 /* mask register offset from GPIO_DATA
35 bits 1=unmask write to given bit */
36 #define IO_DIR 8 /* direction register offset from GPIO_DATA
37 bits 0/1=read/write direction */
39 #define GPIO6 0x0040 /* mask bits for GPIO lines */
44 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
46 #define STR_WREN GPIO8
47 #define STR_MOST GPIO9
49 #define FREQ_LO 50*16000
50 #define FREQ_HI 150*16000
52 #define FREQ_IF 171200 /* 10.7*16000 */
53 #define FREQ_STEP 200 /* 12.5*16 */
55 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
56 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
58 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
60 static int radio_nr = -1;
61 module_param(radio_nr, int, 0);
63 static int radio_ioctl(struct inode *inode, struct file *file,
64 unsigned int cmd, unsigned long arg);
65 static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
66 static void maestro_remove(struct pci_dev *pdev);
68 static struct pci_device_id maestro_r_pci_tbl[] = {
69 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
70 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
71 .class_mask = 0xffff00 },
72 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
73 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
74 .class_mask = 0xffff00 },
77 MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
79 static struct pci_driver maestro_r_driver = {
80 .name = "maestro_radio",
81 .id_table = maestro_r_pci_tbl,
82 .probe = maestro_probe,
83 .remove = __devexit_p(maestro_remove),
86 static struct file_operations maestro_fops = {
88 .open = video_exclusive_open,
89 .release = video_exclusive_release,
91 .compat_ioctl = v4l_compat_ioctl32,
95 static struct video_device maestro_radio = {
96 .name = "Maestro radio",
97 .type = VID_TYPE_TUNER,
98 .hardware = VID_HARDWARE_SF16MI,
99 .fops = &maestro_fops,
102 struct radio_device {
103 u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
104 muted, /* VIDEO_AUDIO_MUTE */
105 stereo, /* VIDEO_TUNER_STEREO_ON */
106 tuned; /* signal strength (0 or 0xffff) */
107 struct semaphore lock;
110 static u32 radio_bits_get(struct radio_device *dev)
112 register u16 io=dev->io, l, rdata;
116 omask = inw(io + IO_MASK);
117 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
122 outw(STR_CLK, io); /* HI state */
125 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
126 outw(0, io); /* LO state */
128 data <<= 1; /* shift data */
131 dev->stereo = rdata & STR_MOST ?
132 0 : VIDEO_TUNER_STEREO_ON;
143 outw(omask, io + IO_MASK);
145 return data & 0x3ffe;
148 static void radio_bits_set(struct radio_device *dev, u32 data)
150 register u16 io=dev->io, l, bits;
153 omask = inw(io + IO_MASK);
154 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
155 outw(odir | STR_DATA, io + IO_DIR);
156 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
159 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
160 data <<= 1; /* shift data */
161 outw(bits, io); /* start strobe */
163 outw(bits | STR_CLK, io); /* HI level */
165 outw(bits, io); /* LO level */
173 outw(omask, io + IO_MASK);
174 outw(odir, io + IO_DIR);
178 static inline int radio_function(struct inode *inode, struct file *file,
179 unsigned int cmd, void *arg)
181 struct video_device *dev = video_devdata(file);
182 struct radio_device *card = video_get_drvdata(dev);
186 struct video_capability *v = arg;
187 memset(v, 0, sizeof(*v));
188 strcpy(v->name, "Maestro radio");
189 v->type = VID_TYPE_TUNER;
190 v->channels = v->audios = 1;
192 } case VIDIOCGTUNER: {
193 struct video_tuner *v = arg;
196 (void)radio_bits_get(card);
197 v->flags = VIDEO_TUNER_LOW | card->stereo;
198 v->signal = card->tuned;
199 strcpy(v->name, "FM");
200 v->rangelow = FREQ_LO;
201 v->rangehigh = FREQ_HI;
202 v->mode = VIDEO_MODE_AUTO;
204 } case VIDIOCSTUNER: {
205 struct video_tuner *v = arg;
209 } case VIDIOCGFREQ: {
210 unsigned long *freq = arg;
211 *freq = BITS2FREQ(radio_bits_get(card));
213 } case VIDIOCSFREQ: {
214 unsigned long *freq = arg;
215 if (*freq < FREQ_LO || *freq > FREQ_HI)
217 radio_bits_set(card, FREQ2BITS(*freq));
219 } case VIDIOCGAUDIO: {
220 struct video_audio *v = arg;
221 memset(v, 0, sizeof(*v));
222 strcpy(v->name, "Radio");
223 v->flags = VIDEO_AUDIO_MUTABLE | card->muted;
224 v->mode = VIDEO_SOUND_STEREO;
226 } case VIDIOCSAUDIO: {
227 struct video_audio *v = arg;
231 register u16 io = card->io;
232 register u16 omask = inw(io + IO_MASK);
233 outw(~STR_WREN, io + IO_MASK);
234 outw((card->muted = v->flags & VIDEO_AUDIO_MUTE) ?
237 outw(omask, io + IO_MASK);
241 } case VIDIOCGUNIT: {
242 struct video_unit *v = arg;
243 v->video = VIDEO_NO_UNIT;
244 v->vbi = VIDEO_NO_UNIT;
245 v->radio = dev->minor;
247 v->teletext = VIDEO_NO_UNIT;
254 static int radio_ioctl(struct inode *inode, struct file *file,
255 unsigned int cmd, unsigned long arg)
257 struct video_device *dev = video_devdata(file);
258 struct radio_device *card = video_get_drvdata(dev);
262 ret = video_usercopy(inode, file, cmd, arg, radio_function);
268 static u16 __devinit radio_power_on(struct radio_device *dev)
270 register u16 io = dev->io;
274 omask = inw(io + IO_MASK);
275 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
276 outw(odir & ~STR_WREN, io + IO_DIR);
277 dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
278 outw(odir, io + IO_DIR);
279 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
280 outw(dev->muted ? 0 : STR_WREN, io);
282 outw(omask, io + IO_MASK);
283 ofreq = radio_bits_get(dev);
285 if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
286 ofreq = FREQ2BITS(FREQ_LO);
287 radio_bits_set(dev, ofreq);
289 return (ofreq == radio_bits_get(dev));
292 static int __devinit maestro_probe(struct pci_dev *pdev,
293 const struct pci_device_id *ent)
295 struct radio_device *radio_unit;
296 struct video_device *maestro_radio_inst;
299 retval = pci_enable_device(pdev);
301 dev_err(&pdev->dev, "enabling pci device failed!\n");
307 radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
308 if (radio_unit == NULL) {
309 dev_err(&pdev->dev, "not enough memory\n");
313 radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
314 init_MUTEX(&radio_unit->lock);
316 maestro_radio_inst = video_device_alloc();
317 if (maestro_radio_inst == NULL) {
318 dev_err(&pdev->dev, "not enough memory\n");
322 memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
323 video_set_drvdata(maestro_radio_inst, radio_unit);
324 pci_set_drvdata(pdev, maestro_radio_inst);
326 retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
329 printk(KERN_ERR "can't register video device!\n");
333 if (!radio_power_on(radio_unit)) {
338 dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
340 dev_info(&pdev->dev, "radio chip initialized\n");
344 video_unregister_device(maestro_radio_inst);
346 kfree(maestro_radio_inst);
354 static void __devexit maestro_remove(struct pci_dev *pdev)
356 struct video_device *vdev = pci_get_drvdata(pdev);
358 video_unregister_device(vdev);
361 static int __init maestro_radio_init(void)
363 int retval = pci_register_driver(&maestro_r_driver);
366 printk(KERN_ERR "error during registration pci driver\n");
371 static void __exit maestro_radio_exit(void)
373 pci_unregister_driver(&maestro_r_driver);
376 module_init(maestro_radio_init);
377 module_exit(maestro_radio_exit);
379 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
380 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
381 MODULE_LICENSE("GPL");