linux 2.6.16.38 w/ vs2.0.3-rc1
[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.05"
31
32 #define GPIO_DATA       0x60   /* port offset from ESS_IO_BASE */
33
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 */
38
39 #define GPIO6           0x0040 /* mask bits for GPIO lines */
40 #define GPIO7           0x0080
41 #define GPIO8           0x0100
42 #define GPIO9           0x0200
43
44 #define STR_DATA        GPIO6  /* radio TEA5757 pins and GPIO bits */
45 #define STR_CLK         GPIO7
46 #define STR_WREN        GPIO8
47 #define STR_MOST        GPIO9
48
49 #define FREQ_LO          50*16000
50 #define FREQ_HI         150*16000
51
52 #define FREQ_IF         171200 /* 10.7*16000   */
53 #define FREQ_STEP       200    /* 12.5*16      */
54
55 #define FREQ2BITS(x)    ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
56                         /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
57
58 #define BITS2FREQ(x)    ((x) * FREQ_STEP - FREQ_IF)
59
60 static int radio_nr = -1;
61 module_param(radio_nr, int, 0);
62
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);
67
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 },
75         { 0 }
76 };
77 MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
78
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),
84 };
85
86 static struct file_operations maestro_fops = {
87         .owner          = THIS_MODULE,
88         .open           = video_exclusive_open,
89         .release        = video_exclusive_release,
90         .ioctl          = radio_ioctl,
91         .compat_ioctl   = v4l_compat_ioctl32,
92         .llseek         = no_llseek,
93 };
94
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,
100 };
101
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;
108 };
109
110 static u32 radio_bits_get(struct radio_device *dev)
111 {
112         register u16 io=dev->io, l, rdata;
113         register u32 data=0;
114         u16 omask;
115
116         omask = inw(io + IO_MASK);
117         outw(~(STR_CLK | STR_WREN), io + IO_MASK);
118         outw(0, io);
119         udelay(16);
120
121         for (l=24;l--;) {
122                 outw(STR_CLK, io);              /* HI state */
123                 udelay(2);
124                 if(!l) 
125                         dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
126                 outw(0, io);                    /* LO state */
127                 udelay(2);
128                 data <<= 1;                     /* shift data */
129                 rdata = inw(io);
130                 if(!l)
131                         dev->stereo =  rdata & STR_MOST ? 
132                         0 : VIDEO_TUNER_STEREO_ON;
133                 else
134                         if(rdata & STR_DATA)
135                                 data++;
136                 udelay(2);
137         }
138
139         if(dev->muted)
140                 outw(STR_WREN, io);
141
142         udelay(4);
143         outw(omask, io + IO_MASK);
144
145         return data & 0x3ffe;
146 }
147
148 static void radio_bits_set(struct radio_device *dev, u32 data)
149 {
150         register u16 io=dev->io, l, bits;
151         u16 omask, odir;
152
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);
157         udelay(16);
158         for (l=25;l;l--) {
159                 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
160                 data <<= 1;                     /* shift data */
161                 outw(bits, io);                 /* start strobe */
162                 udelay(2);
163                 outw(bits | STR_CLK, io);       /* HI level */
164                 udelay(2);
165                 outw(bits, io);                 /* LO level */
166                 udelay(4);
167         }
168
169         if(!dev->muted)
170                 outw(0, io);
171
172         udelay(4);
173         outw(omask, io + IO_MASK);
174         outw(odir, io + IO_DIR);
175         msleep(125);
176 }
177
178 static inline int radio_function(struct inode *inode, struct file *file,
179         unsigned int cmd, void *arg)
180 {
181         struct video_device *dev = video_devdata(file);
182         struct radio_device *card = video_get_drvdata(dev);
183
184         switch (cmd) {
185         case VIDIOCGCAP: {
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;
191                 return 0;
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         } case VIDIOCSTUNER: {
205                 struct video_tuner *v = arg;
206                 if (v->tuner != 0)
207                         return -EINVAL;
208                 return 0;
209         } case VIDIOCGFREQ: {
210                 unsigned long *freq = arg;
211                 *freq = BITS2FREQ(radio_bits_get(card));
212                 return 0;
213         } case VIDIOCSFREQ: {
214                 unsigned long *freq = arg;
215                 if (*freq < FREQ_LO || *freq > FREQ_HI)
216                         return -EINVAL;
217                 radio_bits_set(card, FREQ2BITS(*freq));
218                 return 0;
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;
225                 return 0;
226         } case VIDIOCSAUDIO: {
227                 struct video_audio *v = arg;
228                 if (v->audio)
229                         return -EINVAL;
230                 {
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) ?
235                                 STR_WREN : 0, io);
236                         udelay(4);
237                         outw(omask, io + IO_MASK);
238                         msleep(125);
239                         return 0;
240                 }
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;
246                 v->audio = 0;
247                 v->teletext = VIDEO_NO_UNIT;
248                 return 0;
249         } default:
250                 return -ENOIOCTLCMD;
251         }
252 }
253
254 static int radio_ioctl(struct inode *inode, struct file *file,
255         unsigned int cmd, unsigned long arg)
256 {
257         struct video_device *dev = video_devdata(file);
258         struct radio_device *card = video_get_drvdata(dev);
259         int ret;
260
261         down(&card->lock);
262         ret = video_usercopy(inode, file, cmd, arg, radio_function);
263         up(&card->lock);
264
265         return ret;
266 }
267
268 static u16 __devinit radio_power_on(struct radio_device *dev)
269 {
270         register u16 io = dev->io;
271         register u32 ofreq;
272         u16 omask, odir;
273
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);
281         udelay(16);
282         outw(omask, io + IO_MASK);
283         ofreq = radio_bits_get(dev);
284
285         if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
286                 ofreq = FREQ2BITS(FREQ_LO);
287         radio_bits_set(dev, ofreq);
288
289         return (ofreq == radio_bits_get(dev));
290 }
291
292 static int __devinit maestro_probe(struct pci_dev *pdev,
293         const struct pci_device_id *ent)
294 {
295         struct radio_device *radio_unit;
296         struct video_device *maestro_radio_inst;
297         int retval;
298
299         retval = pci_enable_device(pdev);
300         if (retval) {
301                 dev_err(&pdev->dev, "enabling pci device failed!\n");
302                 goto err;
303         }
304
305         retval = -ENOMEM;
306
307         radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
308         if (radio_unit == NULL) {
309                 dev_err(&pdev->dev, "not enough memory\n");
310                 goto err;
311         }
312
313         radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
314         init_MUTEX(&radio_unit->lock);
315
316         maestro_radio_inst = video_device_alloc();
317         if (maestro_radio_inst == NULL) {
318                 dev_err(&pdev->dev, "not enough memory\n");
319                 goto errfr;
320         }
321
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);
325
326         retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
327                 radio_nr);
328         if (retval) {
329                 printk(KERN_ERR "can't register video device!\n");
330                 goto errfr1;
331         }
332
333         if (!radio_power_on(radio_unit)) {
334                 retval = -EIO;
335                 goto errunr;
336         }
337
338         dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ "  "
339                  __DATE__ "\n");
340         dev_info(&pdev->dev, "radio chip initialized\n");
341
342         return 0;
343 errunr:
344         video_unregister_device(maestro_radio_inst);
345 errfr1:
346         kfree(maestro_radio_inst);
347 errfr:
348         kfree(radio_unit);
349 err:
350         return retval;
351
352 }
353
354 static void __devexit maestro_remove(struct pci_dev *pdev)
355 {
356         struct video_device *vdev = pci_get_drvdata(pdev);
357
358         video_unregister_device(vdev);
359 }
360
361 static int __init maestro_radio_init(void)
362 {
363         int retval = pci_register_driver(&maestro_r_driver);
364
365         if (retval)
366                 printk(KERN_ERR "error during registration pci driver\n");
367
368         return retval;
369 }
370
371 static void __exit maestro_radio_exit(void)
372 {
373         pci_unregister_driver(&maestro_r_driver);
374 }
375
376 module_init(maestro_radio_init);
377 module_exit(maestro_radio_exit);
378
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");