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_param(radio_nr, int, 0);
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,
75 .compat_ioctl = v4l_compat_ioctl32,
79 static struct video_device maestro_radio=
82 .name = "Maestro radio",
83 .type = VID_TYPE_TUNER,
84 .hardware = VID_HARDWARE_SF16MI,
85 .fops = &maestro_fops,
88 static struct radio_device
90 __u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
91 muted, /* VIDEO_AUDIO_MUTE */
92 stereo, /* VIDEO_TUNER_STEREO_ON */
93 tuned; /* signal strength (0 or 0xffff) */
94 struct semaphore lock;
95 } radio_unit = {0, 0, 0, 0, };
97 static __u32 radio_bits_get(struct radio_device *dev)
99 register __u16 io=dev->io, l, rdata;
100 register __u32 data=0;
102 omask = inw(io + IO_MASK);
103 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
108 outw(STR_CLK, io); /* HI state */
111 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
112 outw(0, io); /* LO state */
114 data <<= 1; /* shift data */
117 dev->stereo = rdata & STR_MOST ?
118 0 : VIDEO_TUNER_STEREO_ON;
127 outw(omask, io + IO_MASK);
128 return data & 0x3ffe;
131 static void radio_bits_set(struct radio_device *dev, __u32 data)
133 register __u16 io=dev->io, l, bits;
135 omask = inw(io + IO_MASK);
136 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
137 outw(odir | STR_DATA, io + IO_DIR);
138 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
141 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
142 data <<= 1; /* shift data */
143 outw(bits, io); /* start strobe */
145 outw(bits | STR_CLK, io); /* HI level */
147 outw(bits, io); /* LO level */
153 outw(omask, io + IO_MASK);
154 outw(odir, io + IO_DIR);
158 static inline int radio_function(struct inode *inode, struct file *file,
159 unsigned int cmd, void *arg)
161 struct video_device *dev = video_devdata(file);
162 struct radio_device *card=dev->priv;
166 struct video_capability *v = arg;
167 memset(v,0,sizeof(*v));
168 strcpy(v->name, "Maestro radio");
169 v->type=VID_TYPE_TUNER;
170 v->channels=v->audios=1;
174 struct video_tuner *v = arg;
177 (void)radio_bits_get(card);
178 v->flags = VIDEO_TUNER_LOW | card->stereo;
179 v->signal = card->tuned;
180 strcpy(v->name, "FM");
181 v->rangelow = FREQ_LO;
182 v->rangehigh = FREQ_HI;
183 v->mode = VIDEO_MODE_AUTO;
187 struct video_tuner *v = arg;
193 unsigned long *freq = arg;
194 *freq = BITS2FREQ(radio_bits_get(card));
198 unsigned long *freq = arg;
199 if (*freq<FREQ_LO || *freq>FREQ_HI )
201 radio_bits_set(card, FREQ2BITS(*freq));
205 struct video_audio *v = arg;
206 memset(v,0,sizeof(*v));
207 strcpy(v->name, "Radio");
208 v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
209 v->mode=VIDEO_SOUND_STEREO;
213 struct video_audio *v = arg;
217 register __u16 io=card->io;
218 register __u16 omask = inw(io + IO_MASK);
219 outw(~STR_WREN, io + IO_MASK);
220 outw((card->muted = v->flags & VIDEO_AUDIO_MUTE)
223 outw(omask, io + IO_MASK);
229 struct video_unit *v = arg;
230 v->video=VIDEO_NO_UNIT;
231 v->vbi=VIDEO_NO_UNIT;
234 v->teletext=VIDEO_NO_UNIT;
237 default: return -ENOIOCTLCMD;
241 static int radio_ioctl(struct inode *inode, struct file *file,
242 unsigned int cmd, unsigned long arg)
244 struct video_device *dev = video_devdata(file);
245 struct radio_device *card=dev->priv;
249 ret = video_usercopy(inode, file, cmd, arg, radio_function);
254 static __u16 radio_install(struct pci_dev *pcidev);
256 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
257 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
258 MODULE_LICENSE("GPL");
260 static void __exit maestro_radio_exit(void)
262 video_unregister_device(&maestro_radio);
265 static int __init maestro_radio_init(void)
267 register __u16 found=0;
268 struct pci_dev *pcidev = NULL;
269 while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
270 PCI_DEVICE_ID_ESS_ESS1968,
272 found |= radio_install(pcidev);
273 while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
274 PCI_DEVICE_ID_ESS_ESS1978,
276 found |= radio_install(pcidev);
278 printk(KERN_INFO "radio-maestro: no devices found.\n");
284 module_init(maestro_radio_init);
285 module_exit(maestro_radio_exit);
287 static inline __u16 radio_power_on(struct radio_device *dev)
289 register __u16 io=dev->io;
290 register __u32 ofreq;
292 omask = inw(io + IO_MASK);
293 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
294 outw(odir & ~STR_WREN, io + IO_DIR);
295 dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
296 outw(odir, io + IO_DIR);
297 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
298 outw(dev->muted ? 0 : STR_WREN, io);
300 outw(omask, io + IO_MASK);
301 ofreq = radio_bits_get(dev);
302 if((ofreq<FREQ2BITS(FREQ_LO)) || (ofreq>FREQ2BITS(FREQ_HI)))
303 ofreq = FREQ2BITS(FREQ_LO);
304 radio_bits_set(dev, ofreq);
305 return (ofreq == radio_bits_get(dev));
308 static __u16 radio_install(struct pci_dev *pcidev)
310 if(((pcidev->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
313 radio_unit.io = pcidev->resource[0].start + GPIO_DATA;
314 maestro_radio.priv = &radio_unit;
315 init_MUTEX(&radio_unit.lock);
317 if(radio_power_on(&radio_unit)) {
318 if(video_register_device(&maestro_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
319 printk("radio-maestro: can't register device!");
322 printk(KERN_INFO "radio-maestro: version "
328 printk(KERN_INFO "radio-maestro: radio chip initialized\n");