ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / radio / radio-maestro.c
1 /* Maestro PCI sound card radio driver for Linux support
2  * (c) 2000 A. Tlalka, atlka@pg.gda.pl
3  * Notes on the hardware
4  *
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.
10  *  version 0.02
11  *  + io port is automatically detected - only the first radio is used
12  *  version 0.03
13  *  + thread access locking additions
14  *  version 0.04
15  * + code improvements
16  * + VIDEO_TUNER_LOW is permanent
17  */
18
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>
24 #include <asm/io.h>
25 #include <asm/uaccess.h>
26 #include <asm/semaphore.h>
27 #include <linux/pci.h>
28 #include <linux/videodev.h>
29
30 #define DRIVER_VERSION  "0.04"
31
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   */
35
36 #define GPIO_DATA       0x60   /* port offset from ESS_IO_BASE */
37
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 */
42
43 #define GPIO6           0x0040 /* mask bits for GPIO lines */
44 #define GPIO7           0x0080
45 #define GPIO8           0x0100
46 #define GPIO9           0x0200
47
48 #define STR_DATA        GPIO6  /* radio TEA5757 pins and GPIO bits */
49 #define STR_CLK         GPIO7
50 #define STR_WREN        GPIO8
51 #define STR_MOST        GPIO9
52
53 #define FREQ_LO          50*16000
54 #define FREQ_HI         150*16000
55
56 #define FREQ_IF         171200 /* 10.7*16000   */
57 #define FREQ_STEP       200    /* 12.5*16      */
58
59 #define FREQ2BITS(x)    ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
60                         /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
61
62 #define BITS2FREQ(x)    ((x) * FREQ_STEP - FREQ_IF)
63
64 static int radio_nr = -1;
65 MODULE_PARM(radio_nr, "i");
66
67 static int radio_ioctl(struct inode *inode, struct file *file,
68                        unsigned int cmd, unsigned long arg);
69
70 static struct file_operations maestro_fops = {
71         .owner          = THIS_MODULE,
72         .open           = video_exclusive_open,
73         .release        = video_exclusive_release,
74         .ioctl          = radio_ioctl,
75         .llseek         = no_llseek,
76 };
77
78 static struct video_device maestro_radio=
79 {
80         .owner          = THIS_MODULE,
81         .name           = "Maestro radio",
82         .type           = VID_TYPE_TUNER,
83         .hardware       = VID_HARDWARE_SF16MI,
84         .fops           = &maestro_fops,
85 };
86
87 static struct radio_device
88 {
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, };
95
96 static void sleep_125ms(void)
97 {
98         current->state = TASK_INTERRUPTIBLE;
99         schedule_timeout(HZ >> 3);
100 }
101
102 static void udelay2(void)
103 {
104         udelay(2);
105 }
106
107 static void udelay4(void)
108 {
109         udelay(4);
110 }
111
112 static void udelay16(void)
113 {
114         udelay(16);
115 }
116
117 static __u32 radio_bits_get(struct radio_device *dev)
118 {
119         register __u16 io=dev->io, l, rdata;
120         register __u32 data=0;
121         __u16 omask;
122         omask = inw(io + IO_MASK);
123         outw(~(STR_CLK | STR_WREN), io + IO_MASK);
124         outw(0, io);
125         udelay16();
126         for (l=24;l--;) {
127                 outw(STR_CLK, io);              /* HI state */
128                 udelay2();
129                 if(!l) 
130                         dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
131                 outw(0, io);                    /* LO state */
132                 udelay2();
133                 data <<= 1;                     /* shift data */
134                 rdata = inw(io);
135                 if(!l)
136                         dev->stereo =  rdata & STR_MOST ? 
137                         0 : VIDEO_TUNER_STEREO_ON;
138                 else
139                         if(rdata & STR_DATA)
140                                 data++;
141                 udelay2();
142         }
143         if(dev->muted)
144                 outw(STR_WREN, io);
145         udelay4();
146         outw(omask, io + IO_MASK);
147         return data & 0x3ffe;
148 }
149
150 static void radio_bits_set(struct radio_device *dev, __u32 data)
151 {
152         register __u16 io=dev->io, l, bits;
153         __u16 omask, odir;
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);
158         udelay16();
159         for (l=25;l;l--) {
160                 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
161                 data <<= 1;                     /* shift data */
162                 outw(bits, io);                 /* start strobe */
163                 udelay2();
164                 outw(bits | STR_CLK, io);       /* HI level */
165                 udelay2();   
166                 outw(bits, io);                 /* LO level */
167                 udelay4();
168         }
169         if(!dev->muted)
170                 outw(0, io);
171         udelay4();
172         outw(omask, io + IO_MASK);
173         outw(odir, io + IO_DIR);
174         sleep_125ms();
175 }
176
177 inline static int radio_function(struct inode *inode, struct file *file,
178                                  unsigned int cmd, void *arg)
179 {
180         struct video_device *dev = video_devdata(file);
181         struct radio_device *card=dev->priv;
182         
183         switch(cmd) {
184                 case VIDIOCGCAP: {
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;
190                         return 0;
191                 }
192                 case VIDIOCGTUNER: {
193                         struct video_tuner *v = arg;
194                         if(v->tuner)
195                                 return -EINVAL;
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;
203                         return 0;
204                 }
205                 case VIDIOCSTUNER: {
206                         struct video_tuner *v = arg;
207                         if(v->tuner!=0)
208                                 return -EINVAL;
209                         return 0;
210                 }
211                 case VIDIOCGFREQ: {
212                         unsigned long *freq = arg;
213                         *freq = BITS2FREQ(radio_bits_get(card));
214                         return 0;
215                 }
216                 case VIDIOCSFREQ: {
217                         unsigned long *freq = arg;
218                         if (*freq<FREQ_LO || *freq>FREQ_HI )
219                                 return -EINVAL;
220                         radio_bits_set(card, FREQ2BITS(*freq));
221                         return 0;
222                 }
223                 case VIDIOCGAUDIO: {    
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;
229                         return 0;               
230                 }
231                 case VIDIOCSAUDIO: {
232                         struct video_audio *v = arg;
233                         if(v->audio)
234                                 return -EINVAL;
235                         {
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)
240                                      ? STR_WREN : 0, io);
241                                 udelay4();
242                                 outw(omask, io + IO_MASK);
243                                 sleep_125ms();
244                                 return 0;
245                         }
246                 }
247                 case VIDIOCGUNIT: {
248                         struct video_unit *v = arg;
249                         v->video=VIDEO_NO_UNIT;
250                         v->vbi=VIDEO_NO_UNIT;
251                         v->radio=dev->minor;
252                         v->audio=0;
253                         v->teletext=VIDEO_NO_UNIT;
254                         return 0;               
255                 }
256                 default: return -ENOIOCTLCMD;
257         }
258 }
259
260 static int radio_ioctl(struct inode *inode, struct file *file,
261                        unsigned int cmd, unsigned long arg)
262 {
263         struct video_device *dev = video_devdata(file);
264         struct radio_device *card=dev->priv;
265         int ret;
266
267         down(&card->lock);
268         ret = video_usercopy(inode, file, cmd, arg, radio_function);
269         up(&card->lock);
270         return ret;
271 }
272
273 inline static __u16 radio_install(struct pci_dev *pcidev);
274
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");
278
279 void __exit maestro_radio_exit(void)
280 {
281         video_unregister_device(&maestro_radio);
282 }
283
284 int __init maestro_radio_init(void)
285 {
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,
290                                                   pcidev)))
291                 found |= radio_install(pcidev);
292         while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
293                                                   PCI_DEVICE_ID_ESS_ESS1978, 
294                                                   pcidev)))
295                 found |= radio_install(pcidev);
296         if(!found) {
297                 printk(KERN_INFO "radio-maestro: no devices found.\n");
298                 return -ENODEV;
299         }
300         return 0;
301 }
302
303 module_init(maestro_radio_init);
304 module_exit(maestro_radio_exit);
305
306 inline static __u16 radio_power_on(struct radio_device *dev)
307 {
308         register __u16 io=dev->io;
309         register __u32 ofreq;
310         __u16 omask, odir;
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);
318         udelay16();
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));
325 }
326
327 inline static __u16 radio_install(struct pci_dev *pcidev)
328 {
329         if(((pcidev->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
330                 return 0;
331         
332         radio_unit.io = pcidev->resource[0].start + GPIO_DATA;
333         maestro_radio.priv = &radio_unit;
334         init_MUTEX(&radio_unit.lock);
335         
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!");
339                         return 0;
340                 }
341                 printk(KERN_INFO "radio-maestro: version "
342                        DRIVER_VERSION 
343                        " time " 
344                        __TIME__ "  "
345                        __DATE__
346                        "\n");
347                 printk(KERN_INFO "radio-maestro: radio chip initialized\n");
348                 return 1;
349         } else
350                 return 0;   
351 }
352