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.04"
32 #define PCI_VENDOR_ESS 0x125D
33 #define PCI_DEVICE_ID_ESS_ESS1968 0x1968 /* Maestro 2 */
34 #define PCI_DEVICE_ID_ESS_ESS1978 0x1978 /* Maestro 2E */
36 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
38 #define IO_MASK 4 /* mask register offset from GPIO_DATA
39 bits 1=unmask write to given bit */
40 #define IO_DIR 8 /* direction register offset from GPIO_DATA
41 bits 0/1=read/write direction */
43 #define GPIO6 0x0040 /* mask bits for GPIO lines */
48 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
50 #define STR_WREN GPIO8
51 #define STR_MOST GPIO9
53 #define FREQ_LO 50*16000
54 #define FREQ_HI 150*16000
56 #define FREQ_IF 171200 /* 10.7*16000 */
57 #define FREQ_STEP 200 /* 12.5*16 */
59 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
60 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
62 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
64 static int radio_nr = -1;
65 MODULE_PARM(radio_nr, "i");
67 static int radio_ioctl(struct inode *inode, struct file *file,
68 unsigned int cmd, unsigned long arg);
70 static struct file_operations maestro_fops = {
72 .open = video_exclusive_open,
73 .release = video_exclusive_release,
78 static struct video_device maestro_radio=
81 .name = "Maestro radio",
82 .type = VID_TYPE_TUNER,
83 .hardware = VID_HARDWARE_SF16MI,
84 .fops = &maestro_fops,
87 static struct radio_device
89 __u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
90 muted, /* VIDEO_AUDIO_MUTE */
91 stereo, /* VIDEO_TUNER_STEREO_ON */
92 tuned; /* signal strength (0 or 0xffff) */
93 struct semaphore lock;
94 } radio_unit = {0, 0, 0, 0, };
96 static void sleep_125ms(void)
98 current->state = TASK_INTERRUPTIBLE;
99 schedule_timeout(HZ >> 3);
102 static void udelay2(void)
107 static void udelay4(void)
112 static void udelay16(void)
117 static __u32 radio_bits_get(struct radio_device *dev)
119 register __u16 io=dev->io, l, rdata;
120 register __u32 data=0;
122 omask = inw(io + IO_MASK);
123 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
127 outw(STR_CLK, io); /* HI state */
130 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
131 outw(0, io); /* LO state */
133 data <<= 1; /* shift data */
136 dev->stereo = rdata & STR_MOST ?
137 0 : VIDEO_TUNER_STEREO_ON;
146 outw(omask, io + IO_MASK);
147 return data & 0x3ffe;
150 static void radio_bits_set(struct radio_device *dev, __u32 data)
152 register __u16 io=dev->io, l, bits;
154 omask = inw(io + IO_MASK);
155 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
156 outw(odir | STR_DATA, io + IO_DIR);
157 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
160 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
161 data <<= 1; /* shift data */
162 outw(bits, io); /* start strobe */
164 outw(bits | STR_CLK, io); /* HI level */
166 outw(bits, io); /* LO level */
172 outw(omask, io + IO_MASK);
173 outw(odir, io + IO_DIR);
177 inline static int radio_function(struct inode *inode, struct file *file,
178 unsigned int cmd, void *arg)
180 struct video_device *dev = video_devdata(file);
181 struct radio_device *card=dev->priv;
185 struct video_capability *v = arg;
186 memset(v,0,sizeof(*v));
187 strcpy(v->name, "Maestro radio");
188 v->type=VID_TYPE_TUNER;
189 v->channels=v->audios=1;
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;
206 struct video_tuner *v = arg;
212 unsigned long *freq = arg;
213 *freq = BITS2FREQ(radio_bits_get(card));
217 unsigned long *freq = arg;
218 if (*freq<FREQ_LO || *freq>FREQ_HI )
220 radio_bits_set(card, FREQ2BITS(*freq));
224 struct video_audio *v = arg;
225 memset(v,0,sizeof(*v));
226 strcpy(v->name, "Radio");
227 v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
228 v->mode=VIDEO_SOUND_STEREO;
232 struct video_audio *v = arg;
236 register __u16 io=card->io;
237 register __u16 omask = inw(io + IO_MASK);
238 outw(~STR_WREN, io + IO_MASK);
239 outw((card->muted = v->flags & VIDEO_AUDIO_MUTE)
242 outw(omask, io + IO_MASK);
248 struct video_unit *v = arg;
249 v->video=VIDEO_NO_UNIT;
250 v->vbi=VIDEO_NO_UNIT;
253 v->teletext=VIDEO_NO_UNIT;
256 default: return -ENOIOCTLCMD;
260 static int radio_ioctl(struct inode *inode, struct file *file,
261 unsigned int cmd, unsigned long arg)
263 struct video_device *dev = video_devdata(file);
264 struct radio_device *card=dev->priv;
268 ret = video_usercopy(inode, file, cmd, arg, radio_function);
273 inline static __u16 radio_install(struct pci_dev *pcidev);
275 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
276 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
277 MODULE_LICENSE("GPL");
279 void __exit maestro_radio_exit(void)
281 video_unregister_device(&maestro_radio);
284 int __init maestro_radio_init(void)
286 register __u16 found=0;
287 struct pci_dev *pcidev = NULL;
288 while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
289 PCI_DEVICE_ID_ESS_ESS1968,
291 found |= radio_install(pcidev);
292 while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
293 PCI_DEVICE_ID_ESS_ESS1978,
295 found |= radio_install(pcidev);
297 printk(KERN_INFO "radio-maestro: no devices found.\n");
303 module_init(maestro_radio_init);
304 module_exit(maestro_radio_exit);
306 inline static __u16 radio_power_on(struct radio_device *dev)
308 register __u16 io=dev->io;
309 register __u32 ofreq;
311 omask = inw(io + IO_MASK);
312 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
313 outw(odir & ~STR_WREN, io + IO_DIR);
314 dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
315 outw(odir, io + IO_DIR);
316 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
317 outw(dev->muted ? 0 : STR_WREN, io);
319 outw(omask, io + IO_MASK);
320 ofreq = radio_bits_get(dev);
321 if((ofreq<FREQ2BITS(FREQ_LO)) || (ofreq>FREQ2BITS(FREQ_HI)))
322 ofreq = FREQ2BITS(FREQ_LO);
323 radio_bits_set(dev, ofreq);
324 return (ofreq == radio_bits_get(dev));
327 inline static __u16 radio_install(struct pci_dev *pcidev)
329 if(((pcidev->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
332 radio_unit.io = pcidev->resource[0].start + GPIO_DATA;
333 maestro_radio.priv = &radio_unit;
334 init_MUTEX(&radio_unit.lock);
336 if(radio_power_on(&radio_unit)) {
337 if(video_register_device(&maestro_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
338 printk("radio-maestro: can't register device!");
341 printk(KERN_INFO "radio-maestro: version "
347 printk(KERN_INFO "radio-maestro: radio chip initialized\n");