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,
 
  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 __u32 radio_bits_get(struct radio_device *dev)
 
  98         register __u16 io=dev->io, l, rdata;
 
  99         register __u32 data=0;
 
 101         omask = inw(io + IO_MASK);
 
 102         outw(~(STR_CLK | STR_WREN), io + IO_MASK);
 
 107                 outw(STR_CLK, io);              /* HI state */
 
 110                         dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
 
 111                 outw(0, io);                    /* LO state */
 
 113                 data <<= 1;                     /* shift data */
 
 116                         dev->stereo =  rdata & STR_MOST ? 
 
 117                         0 : VIDEO_TUNER_STEREO_ON;
 
 126         outw(omask, io + IO_MASK);
 
 127         return data & 0x3ffe;
 
 130 static void radio_bits_set(struct radio_device *dev, __u32 data)
 
 132         register __u16 io=dev->io, l, bits;
 
 134         omask = inw(io + IO_MASK);
 
 135         odir  = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
 
 136         outw(odir | STR_DATA, io + IO_DIR);
 
 137         outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
 
 140                 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
 
 141                 data <<= 1;                     /* shift data */
 
 142                 outw(bits, io);                 /* start strobe */
 
 144                 outw(bits | STR_CLK, io);       /* HI level */
 
 146                 outw(bits, io);                 /* LO level */
 
 152         outw(omask, io + IO_MASK);
 
 153         outw(odir, io + IO_DIR);
 
 157 static inline int radio_function(struct inode *inode, struct file *file,
 
 158                                  unsigned int cmd, void *arg)
 
 160         struct video_device *dev = video_devdata(file);
 
 161         struct radio_device *card=dev->priv;
 
 165                         struct video_capability *v = arg;
 
 166                         memset(v,0,sizeof(*v));
 
 167                         strcpy(v->name, "Maestro radio");
 
 168                         v->type=VID_TYPE_TUNER;
 
 169                         v->channels=v->audios=1;
 
 173                         struct video_tuner *v = arg;
 
 176                         (void)radio_bits_get(card);
 
 177                         v->flags = VIDEO_TUNER_LOW | card->stereo;
 
 178                         v->signal = card->tuned;
 
 179                         strcpy(v->name, "FM");
 
 180                         v->rangelow = FREQ_LO;
 
 181                         v->rangehigh = FREQ_HI;
 
 182                         v->mode = VIDEO_MODE_AUTO;
 
 186                         struct video_tuner *v = arg;
 
 192                         unsigned long *freq = arg;
 
 193                         *freq = BITS2FREQ(radio_bits_get(card));
 
 197                         unsigned long *freq = arg;
 
 198                         if (*freq<FREQ_LO || *freq>FREQ_HI )
 
 200                         radio_bits_set(card, FREQ2BITS(*freq));
 
 204                         struct video_audio *v = arg;
 
 205                         memset(v,0,sizeof(*v));
 
 206                         strcpy(v->name, "Radio");
 
 207                         v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
 
 208                         v->mode=VIDEO_SOUND_STEREO;
 
 212                         struct video_audio *v = arg;
 
 216                                 register __u16 io=card->io;
 
 217                                 register __u16 omask = inw(io + IO_MASK);
 
 218                                 outw(~STR_WREN, io + IO_MASK);
 
 219                                 outw((card->muted = v->flags & VIDEO_AUDIO_MUTE)
 
 222                                 outw(omask, io + IO_MASK);
 
 228                         struct video_unit *v = arg;
 
 229                         v->video=VIDEO_NO_UNIT;
 
 230                         v->vbi=VIDEO_NO_UNIT;
 
 233                         v->teletext=VIDEO_NO_UNIT;
 
 236                 default: return -ENOIOCTLCMD;
 
 240 static int radio_ioctl(struct inode *inode, struct file *file,
 
 241                        unsigned int cmd, unsigned long arg)
 
 243         struct video_device *dev = video_devdata(file);
 
 244         struct radio_device *card=dev->priv;
 
 248         ret = video_usercopy(inode, file, cmd, arg, radio_function);
 
 253 static __u16 radio_install(struct pci_dev *pcidev);
 
 255 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
 
 256 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
 
 257 MODULE_LICENSE("GPL");
 
 259 static void __exit maestro_radio_exit(void)
 
 261         video_unregister_device(&maestro_radio);
 
 264 static int __init maestro_radio_init(void)
 
 266         register __u16 found=0;
 
 267         struct pci_dev *pcidev = NULL;
 
 268         while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS, 
 
 269                                                   PCI_DEVICE_ID_ESS_ESS1968,
 
 271                 found |= radio_install(pcidev);
 
 272         while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
 
 273                                                   PCI_DEVICE_ID_ESS_ESS1978, 
 
 275                 found |= radio_install(pcidev);
 
 277                 printk(KERN_INFO "radio-maestro: no devices found.\n");
 
 283 module_init(maestro_radio_init);
 
 284 module_exit(maestro_radio_exit);
 
 286 static inline __u16 radio_power_on(struct radio_device *dev)
 
 288         register __u16 io=dev->io;
 
 289         register __u32 ofreq;
 
 291         omask = inw(io + IO_MASK);
 
 292         odir  = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
 
 293         outw(odir & ~STR_WREN, io + IO_DIR);
 
 294         dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
 
 295         outw(odir, io + IO_DIR);
 
 296         outw(~(STR_WREN | STR_CLK), io + IO_MASK);
 
 297         outw(dev->muted ? 0 : STR_WREN, io);
 
 299         outw(omask, io + IO_MASK);
 
 300         ofreq = radio_bits_get(dev);
 
 301         if((ofreq<FREQ2BITS(FREQ_LO)) || (ofreq>FREQ2BITS(FREQ_HI)))
 
 302                 ofreq = FREQ2BITS(FREQ_LO);
 
 303         radio_bits_set(dev, ofreq);
 
 304         return (ofreq == radio_bits_get(dev));
 
 307 static __u16 radio_install(struct pci_dev *pcidev)
 
 309         if(((pcidev->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
 
 312         radio_unit.io = pcidev->resource[0].start + GPIO_DATA;
 
 313         maestro_radio.priv = &radio_unit;
 
 314         init_MUTEX(&radio_unit.lock);
 
 316         if(radio_power_on(&radio_unit)) {
 
 317                 if(video_register_device(&maestro_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
 
 318                         printk("radio-maestro: can't register device!");
 
 321                 printk(KERN_INFO "radio-maestro: version "
 
 327                 printk(KERN_INFO "radio-maestro: radio chip initialized\n");