Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[sfrench/cifs-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 <linux/mutex.h>
27 #include <linux/pci.h>
28 #include <linux/videodev.h>
29
30
31 #define DRIVER_VERSION  "0.05"
32
33 #define GPIO_DATA       0x60   /* port offset from ESS_IO_BASE */
34
35 #define IO_MASK         4      /* mask      register offset from GPIO_DATA
36                                 bits 1=unmask write to given bit */
37 #define IO_DIR          8      /* direction register offset from GPIO_DATA
38                                 bits 0/1=read/write direction */
39
40 #define GPIO6           0x0040 /* mask bits for GPIO lines */
41 #define GPIO7           0x0080
42 #define GPIO8           0x0100
43 #define GPIO9           0x0200
44
45 #define STR_DATA        GPIO6  /* radio TEA5757 pins and GPIO bits */
46 #define STR_CLK         GPIO7
47 #define STR_WREN        GPIO8
48 #define STR_MOST        GPIO9
49
50 #define FREQ_LO          50*16000
51 #define FREQ_HI         150*16000
52
53 #define FREQ_IF         171200 /* 10.7*16000   */
54 #define FREQ_STEP       200    /* 12.5*16      */
55
56 #define FREQ2BITS(x)    ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
57                         /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
58
59 #define BITS2FREQ(x)    ((x) * FREQ_STEP - FREQ_IF)
60
61 static int radio_nr = -1;
62 module_param(radio_nr, int, 0);
63
64 static int radio_ioctl(struct inode *inode, struct file *file,
65         unsigned int cmd, unsigned long arg);
66 static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
67 static void maestro_remove(struct pci_dev *pdev);
68
69 static struct pci_device_id maestro_r_pci_tbl[] = {
70         { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
71                 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
72                 .class_mask = 0xffff00 },
73         { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
74                 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
75                 .class_mask = 0xffff00 },
76         { 0 }
77 };
78 MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
79
80 static struct pci_driver maestro_r_driver = {
81         .name           = "maestro_radio",
82         .id_table       = maestro_r_pci_tbl,
83         .probe          = maestro_probe,
84         .remove         = __devexit_p(maestro_remove),
85 };
86
87 static struct file_operations maestro_fops = {
88         .owner          = THIS_MODULE,
89         .open           = video_exclusive_open,
90         .release        = video_exclusive_release,
91         .ioctl          = radio_ioctl,
92         .compat_ioctl   = v4l_compat_ioctl32,
93         .llseek         = no_llseek,
94 };
95
96 static struct video_device maestro_radio = {
97         .name           = "Maestro radio",
98         .type           = VID_TYPE_TUNER,
99         .hardware       = VID_HARDWARE_SF16MI,
100         .fops           = &maestro_fops,
101 };
102
103 struct radio_device {
104         u16     io,     /* base of Maestro card radio io (GPIO_DATA)*/
105                 muted,  /* VIDEO_AUDIO_MUTE */
106                 stereo, /* VIDEO_TUNER_STEREO_ON */     
107                 tuned;  /* signal strength (0 or 0xffff) */
108         struct mutex lock;
109 };
110
111 static u32 radio_bits_get(struct radio_device *dev)
112 {
113         register u16 io=dev->io, l, rdata;
114         register u32 data=0;
115         u16 omask;
116
117         omask = inw(io + IO_MASK);
118         outw(~(STR_CLK | STR_WREN), io + IO_MASK);
119         outw(0, io);
120         udelay(16);
121
122         for (l=24;l--;) {
123                 outw(STR_CLK, io);              /* HI state */
124                 udelay(2);
125                 if(!l) 
126                         dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
127                 outw(0, io);                    /* LO state */
128                 udelay(2);
129                 data <<= 1;                     /* shift data */
130                 rdata = inw(io);
131                 if(!l)
132                         dev->stereo =  rdata & STR_MOST ? 
133                         0 : VIDEO_TUNER_STEREO_ON;
134                 else
135                         if(rdata & STR_DATA)
136                                 data++;
137                 udelay(2);
138         }
139
140         if(dev->muted)
141                 outw(STR_WREN, io);
142
143         udelay(4);
144         outw(omask, io + IO_MASK);
145
146         return data & 0x3ffe;
147 }
148
149 static void radio_bits_set(struct radio_device *dev, u32 data)
150 {
151         register u16 io=dev->io, l, bits;
152         u16 omask, odir;
153
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         udelay(16);
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                 udelay(2);
164                 outw(bits | STR_CLK, io);       /* HI level */
165                 udelay(2);
166                 outw(bits, io);                 /* LO level */
167                 udelay(4);
168         }
169
170         if(!dev->muted)
171                 outw(0, io);
172
173         udelay(4);
174         outw(omask, io + IO_MASK);
175         outw(odir, io + IO_DIR);
176         msleep(125);
177 }
178
179 static inline int radio_function(struct inode *inode, struct file *file,
180         unsigned int cmd, void *arg)
181 {
182         struct video_device *dev = video_devdata(file);
183         struct radio_device *card = video_get_drvdata(dev);
184
185         switch (cmd) {
186         case VIDIOCGCAP: {
187                 struct video_capability *v = arg;
188                 memset(v, 0, sizeof(*v));
189                 strcpy(v->name, "Maestro radio");
190                 v->type = VID_TYPE_TUNER;
191                 v->channels = v->audios = 1;
192                 return 0;
193         } case VIDIOCGTUNER: {
194                 struct video_tuner *v = arg;
195                 if (v->tuner)
196                         return -EINVAL;
197                 (void)radio_bits_get(card);
198                 v->flags = VIDEO_TUNER_LOW | card->stereo;
199                 v->signal = card->tuned;
200                 strcpy(v->name, "FM");
201                 v->rangelow = FREQ_LO;
202                 v->rangehigh = FREQ_HI;
203                 v->mode = VIDEO_MODE_AUTO;
204                 return 0;
205         } case VIDIOCSTUNER: {
206                 struct video_tuner *v = arg;
207                 if (v->tuner != 0)
208                         return -EINVAL;
209                 return 0;
210         } case VIDIOCGFREQ: {
211                 unsigned long *freq = arg;
212                 *freq = BITS2FREQ(radio_bits_get(card));
213                 return 0;
214         } case VIDIOCSFREQ: {
215                 unsigned long *freq = arg;
216                 if (*freq < FREQ_LO || *freq > FREQ_HI)
217                         return -EINVAL;
218                 radio_bits_set(card, FREQ2BITS(*freq));
219                 return 0;
220         } case VIDIOCGAUDIO: {
221                 struct video_audio *v = arg;
222                 memset(v, 0, sizeof(*v));
223                 strcpy(v->name, "Radio");
224                 v->flags = VIDEO_AUDIO_MUTABLE | card->muted;
225                 v->mode = VIDEO_SOUND_STEREO;
226                 return 0;
227         } case VIDIOCSAUDIO: {
228                 struct video_audio *v = arg;
229                 if (v->audio)
230                         return -EINVAL;
231                 {
232                         register u16 io = card->io;
233                         register u16 omask = inw(io + IO_MASK);
234                         outw(~STR_WREN, io + IO_MASK);
235                         outw((card->muted = v->flags & VIDEO_AUDIO_MUTE) ?
236                                 STR_WREN : 0, io);
237                         udelay(4);
238                         outw(omask, io + IO_MASK);
239                         msleep(125);
240                         return 0;
241                 }
242         } case VIDIOCGUNIT: {
243                 struct video_unit *v = arg;
244                 v->video = VIDEO_NO_UNIT;
245                 v->vbi = VIDEO_NO_UNIT;
246                 v->radio = dev->minor;
247                 v->audio = 0;
248                 v->teletext = VIDEO_NO_UNIT;
249                 return 0;
250         } default:
251                 return -ENOIOCTLCMD;
252         }
253 }
254
255 static int radio_ioctl(struct inode *inode, struct file *file,
256         unsigned int cmd, unsigned long arg)
257 {
258         struct video_device *dev = video_devdata(file);
259         struct radio_device *card = video_get_drvdata(dev);
260         int ret;
261
262         mutex_lock(&card->lock);
263         ret = video_usercopy(inode, file, cmd, arg, radio_function);
264         mutex_unlock(&card->lock);
265
266         return ret;
267 }
268
269 static u16 __devinit radio_power_on(struct radio_device *dev)
270 {
271         register u16 io = dev->io;
272         register u32 ofreq;
273         u16 omask, odir;
274
275         omask = inw(io + IO_MASK);
276         odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
277         outw(odir & ~STR_WREN, io + IO_DIR);
278         dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
279         outw(odir, io + IO_DIR);
280         outw(~(STR_WREN | STR_CLK), io + IO_MASK);
281         outw(dev->muted ? 0 : STR_WREN, io);
282         udelay(16);
283         outw(omask, io + IO_MASK);
284         ofreq = radio_bits_get(dev);
285
286         if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
287                 ofreq = FREQ2BITS(FREQ_LO);
288         radio_bits_set(dev, ofreq);
289
290         return (ofreq == radio_bits_get(dev));
291 }
292
293 static int __devinit maestro_probe(struct pci_dev *pdev,
294         const struct pci_device_id *ent)
295 {
296         struct radio_device *radio_unit;
297         struct video_device *maestro_radio_inst;
298         int retval;
299
300         retval = pci_enable_device(pdev);
301         if (retval) {
302                 dev_err(&pdev->dev, "enabling pci device failed!\n");
303                 goto err;
304         }
305
306         retval = -ENOMEM;
307
308         radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
309         if (radio_unit == NULL) {
310                 dev_err(&pdev->dev, "not enough memory\n");
311                 goto err;
312         }
313
314         radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
315         mutex_init(&radio_unit->lock);
316
317         maestro_radio_inst = video_device_alloc();
318         if (maestro_radio_inst == NULL) {
319                 dev_err(&pdev->dev, "not enough memory\n");
320                 goto errfr;
321         }
322
323         memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
324         video_set_drvdata(maestro_radio_inst, radio_unit);
325         pci_set_drvdata(pdev, maestro_radio_inst);
326
327         retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
328                 radio_nr);
329         if (retval) {
330                 printk(KERN_ERR "can't register video device!\n");
331                 goto errfr1;
332         }
333
334         if (!radio_power_on(radio_unit)) {
335                 retval = -EIO;
336                 goto errunr;
337         }
338
339         dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ "  "
340                  __DATE__ "\n");
341         dev_info(&pdev->dev, "radio chip initialized\n");
342
343         return 0;
344 errunr:
345         video_unregister_device(maestro_radio_inst);
346 errfr1:
347         kfree(maestro_radio_inst);
348 errfr:
349         kfree(radio_unit);
350 err:
351         return retval;
352
353 }
354
355 static void __devexit maestro_remove(struct pci_dev *pdev)
356 {
357         struct video_device *vdev = pci_get_drvdata(pdev);
358
359         video_unregister_device(vdev);
360 }
361
362 static int __init maestro_radio_init(void)
363 {
364         int retval = pci_register_driver(&maestro_r_driver);
365
366         if (retval)
367                 printk(KERN_ERR "error during registration pci driver\n");
368
369         return retval;
370 }
371
372 static void __exit maestro_radio_exit(void)
373 {
374         pci_unregister_driver(&maestro_r_driver);
375 }
376
377 module_init(maestro_radio_init);
378 module_exit(maestro_radio_exit);
379
380 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
381 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
382 MODULE_LICENSE("GPL");