Merge ARM fixes
[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  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
19  */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/ioport.h>
24 #include <linux/delay.h>
25 #include <asm/io.h>
26 #include <asm/uaccess.h>
27 #include <linux/mutex.h>
28 #include <linux/pci.h>
29 #include <linux/videodev2.h>
30 #include <media/v4l2-common.h>
31
32 #include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
33 #define RADIO_VERSION KERNEL_VERSION(0,0,6)
34 #define DRIVER_VERSION  "0.06"
35
36 static struct v4l2_queryctrl radio_qctrl[] = {
37         {
38                 .id            = V4L2_CID_AUDIO_MUTE,
39                 .name          = "Mute",
40                 .minimum       = 0,
41                 .maximum       = 1,
42                 .default_value = 1,
43                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
44         }
45 };
46
47 #define GPIO_DATA       0x60   /* port offset from ESS_IO_BASE */
48
49 #define IO_MASK         4      /* mask      register offset from GPIO_DATA
50                                 bits 1=unmask write to given bit */
51 #define IO_DIR          8      /* direction register offset from GPIO_DATA
52                                 bits 0/1=read/write direction */
53
54 #define GPIO6           0x0040 /* mask bits for GPIO lines */
55 #define GPIO7           0x0080
56 #define GPIO8           0x0100
57 #define GPIO9           0x0200
58
59 #define STR_DATA        GPIO6  /* radio TEA5757 pins and GPIO bits */
60 #define STR_CLK         GPIO7
61 #define STR_WREN        GPIO8
62 #define STR_MOST        GPIO9
63
64 #define FREQ_LO          50*16000
65 #define FREQ_HI         150*16000
66
67 #define FREQ_IF         171200 /* 10.7*16000   */
68 #define FREQ_STEP       200    /* 12.5*16      */
69
70 #define FREQ2BITS(x)    ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
71                         /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
72
73 #define BITS2FREQ(x)    ((x) * FREQ_STEP - FREQ_IF)
74
75 static int radio_nr = -1;
76 module_param(radio_nr, int, 0);
77
78 static int radio_ioctl(struct inode *inode, struct file *file,
79         unsigned int cmd, unsigned long arg);
80 static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
81 static void maestro_remove(struct pci_dev *pdev);
82
83 static struct pci_device_id maestro_r_pci_tbl[] = {
84         { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
85                 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
86                 .class_mask = 0xffff00 },
87         { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
88                 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
89                 .class_mask = 0xffff00 },
90         { 0 }
91 };
92 MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
93
94 static struct pci_driver maestro_r_driver = {
95         .name           = "maestro_radio",
96         .id_table       = maestro_r_pci_tbl,
97         .probe          = maestro_probe,
98         .remove         = __devexit_p(maestro_remove),
99 };
100
101 static const struct file_operations maestro_fops = {
102         .owner          = THIS_MODULE,
103         .open           = video_exclusive_open,
104         .release        = video_exclusive_release,
105         .ioctl          = radio_ioctl,
106         .compat_ioctl   = v4l_compat_ioctl32,
107         .llseek         = no_llseek,
108 };
109
110 static struct video_device maestro_radio = {
111         .name           = "Maestro radio",
112         .type           = VID_TYPE_TUNER,
113         .hardware       = 0,
114         .fops           = &maestro_fops,
115 };
116
117 struct radio_device {
118         u16     io,     /* base of Maestro card radio io (GPIO_DATA)*/
119                 muted,  /* VIDEO_AUDIO_MUTE */
120                 stereo, /* VIDEO_TUNER_STEREO_ON */
121                 tuned;  /* signal strength (0 or 0xffff) */
122         struct mutex lock;
123 };
124
125 static u32 radio_bits_get(struct radio_device *dev)
126 {
127         register u16 io=dev->io, l, rdata;
128         register u32 data=0;
129         u16 omask;
130
131         omask = inw(io + IO_MASK);
132         outw(~(STR_CLK | STR_WREN), io + IO_MASK);
133         outw(0, io);
134         udelay(16);
135
136         for (l=24;l--;) {
137                 outw(STR_CLK, io);              /* HI state */
138                 udelay(2);
139                 if(!l)
140                         dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
141                 outw(0, io);                    /* LO state */
142                 udelay(2);
143                 data <<= 1;                     /* shift data */
144                 rdata = inw(io);
145                 if(!l)
146                         dev->stereo =  rdata & STR_MOST ?
147                         0 : 1;
148                 else
149                         if(rdata & STR_DATA)
150                                 data++;
151                 udelay(2);
152         }
153
154         if(dev->muted)
155                 outw(STR_WREN, io);
156
157         udelay(4);
158         outw(omask, io + IO_MASK);
159
160         return data & 0x3ffe;
161 }
162
163 static void radio_bits_set(struct radio_device *dev, u32 data)
164 {
165         register u16 io=dev->io, l, bits;
166         u16 omask, odir;
167
168         omask = inw(io + IO_MASK);
169         odir  = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
170         outw(odir | STR_DATA, io + IO_DIR);
171         outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
172         udelay(16);
173         for (l=25;l;l--) {
174                 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
175                 data <<= 1;                     /* shift data */
176                 outw(bits, io);                 /* start strobe */
177                 udelay(2);
178                 outw(bits | STR_CLK, io);       /* HI level */
179                 udelay(2);
180                 outw(bits, io);                 /* LO level */
181                 udelay(4);
182         }
183
184         if(!dev->muted)
185                 outw(0, io);
186
187         udelay(4);
188         outw(omask, io + IO_MASK);
189         outw(odir, io + IO_DIR);
190         msleep(125);
191 }
192
193 static inline int radio_function(struct inode *inode, struct file *file,
194         unsigned int cmd, void *arg)
195 {
196         struct video_device *dev = video_devdata(file);
197         struct radio_device *card = video_get_drvdata(dev);
198
199         switch (cmd) {
200                 case VIDIOC_QUERYCAP:
201                 {
202                         struct v4l2_capability *v = arg;
203                         memset(v,0,sizeof(*v));
204                         strlcpy(v->driver, "radio-maestro", sizeof (v->driver));
205                         strlcpy(v->card, "Maestro Radio", sizeof (v->card));
206                         sprintf(v->bus_info,"PCI");
207                         v->version = RADIO_VERSION;
208                         v->capabilities = V4L2_CAP_TUNER;
209
210                         return 0;
211                 }
212                 case VIDIOC_G_TUNER:
213                 {
214                         struct v4l2_tuner *v = arg;
215
216                         if (v->index > 0)
217                                 return -EINVAL;
218
219                         (void)radio_bits_get(card);
220
221                         memset(v,0,sizeof(*v));
222                         strcpy(v->name, "FM");
223                         v->type = V4L2_TUNER_RADIO;
224
225                         v->rangelow = FREQ_LO;
226                         v->rangehigh = FREQ_HI;
227                         v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
228                         v->capability=V4L2_TUNER_CAP_LOW;
229                         if(card->stereo)
230                                 v->audmode = V4L2_TUNER_MODE_STEREO;
231                         else
232                                 v->audmode = V4L2_TUNER_MODE_MONO;
233                         v->signal=card->tuned;
234
235                         return 0;
236                 }
237                 case VIDIOC_S_TUNER:
238                 {
239                         struct v4l2_tuner *v = arg;
240
241                         if (v->index > 0)
242                                 return -EINVAL;
243
244                         return 0;
245                 }
246                 case VIDIOC_S_FREQUENCY:
247                 {
248                         struct v4l2_frequency *f = arg;
249
250                         if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
251                                 return -EINVAL;
252                         radio_bits_set(card, FREQ2BITS(f->frequency));
253
254                         return 0;
255                 }
256                 case VIDIOC_G_FREQUENCY:
257                 {
258                         struct v4l2_frequency *f = arg;
259
260                         f->type = V4L2_TUNER_RADIO;
261                         f->frequency = BITS2FREQ(radio_bits_get(card));
262
263                         return 0;
264                 }
265                 case VIDIOC_QUERYCTRL:
266                 {
267                         struct v4l2_queryctrl *qc = arg;
268                         int i;
269
270                         for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
271                                 if (qc->id && qc->id == radio_qctrl[i].id) {
272                                         memcpy(qc, &(radio_qctrl[i]),
273                                                                 sizeof(*qc));
274                                         return (0);
275                                 }
276                         }
277                         return -EINVAL;
278                 }
279                 case VIDIOC_G_CTRL:
280                 {
281                         struct v4l2_control *ctrl= arg;
282
283                         switch (ctrl->id) {
284                                 case V4L2_CID_AUDIO_MUTE:
285                                         ctrl->value=card->muted;
286                                         return (0);
287                         }
288                         return -EINVAL;
289                 }
290                 case VIDIOC_S_CTRL:
291                 {
292                         struct v4l2_control *ctrl= arg;
293
294                         switch (ctrl->id) {
295                                 case V4L2_CID_AUDIO_MUTE:
296                                 {
297                                         register u16 io = card->io;
298                                         register u16 omask = inw(io + IO_MASK);
299                                         outw(~STR_WREN, io + IO_MASK);
300                                         outw((card->muted = ctrl->value ) ?
301                                                 STR_WREN : 0, io);
302                                         udelay(4);
303                                         outw(omask, io + IO_MASK);
304                                         msleep(125);
305
306                                         return (0);
307                                 }
308                         }
309                         return -EINVAL;
310                 }
311                 default:
312                         return v4l_compat_translate_ioctl(inode,file,cmd,arg,
313                                                           radio_function);
314         }
315 }
316
317 static int radio_ioctl(struct inode *inode, struct file *file,
318         unsigned int cmd, unsigned long arg)
319 {
320         struct video_device *dev = video_devdata(file);
321         struct radio_device *card = video_get_drvdata(dev);
322         int ret;
323
324         mutex_lock(&card->lock);
325         ret = video_usercopy(inode, file, cmd, arg, radio_function);
326         mutex_unlock(&card->lock);
327
328         return ret;
329 }
330
331 static u16 __devinit radio_power_on(struct radio_device *dev)
332 {
333         register u16 io = dev->io;
334         register u32 ofreq;
335         u16 omask, odir;
336
337         omask = inw(io + IO_MASK);
338         odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
339         outw(odir & ~STR_WREN, io + IO_DIR);
340         dev->muted = inw(io) & STR_WREN ? 0 : 1;
341         outw(odir, io + IO_DIR);
342         outw(~(STR_WREN | STR_CLK), io + IO_MASK);
343         outw(dev->muted ? 0 : STR_WREN, io);
344         udelay(16);
345         outw(omask, io + IO_MASK);
346         ofreq = radio_bits_get(dev);
347
348         if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
349                 ofreq = FREQ2BITS(FREQ_LO);
350         radio_bits_set(dev, ofreq);
351
352         return (ofreq == radio_bits_get(dev));
353 }
354
355 static int __devinit maestro_probe(struct pci_dev *pdev,
356         const struct pci_device_id *ent)
357 {
358         struct radio_device *radio_unit;
359         struct video_device *maestro_radio_inst;
360         int retval;
361
362         retval = pci_enable_device(pdev);
363         if (retval) {
364                 dev_err(&pdev->dev, "enabling pci device failed!\n");
365                 goto err;
366         }
367
368         retval = -ENOMEM;
369
370         radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
371         if (radio_unit == NULL) {
372                 dev_err(&pdev->dev, "not enough memory\n");
373                 goto err;
374         }
375
376         radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
377         mutex_init(&radio_unit->lock);
378
379         maestro_radio_inst = video_device_alloc();
380         if (maestro_radio_inst == NULL) {
381                 dev_err(&pdev->dev, "not enough memory\n");
382                 goto errfr;
383         }
384
385         memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
386         video_set_drvdata(maestro_radio_inst, radio_unit);
387         pci_set_drvdata(pdev, maestro_radio_inst);
388
389         retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
390                 radio_nr);
391         if (retval) {
392                 printk(KERN_ERR "can't register video device!\n");
393                 goto errfr1;
394         }
395
396         if (!radio_power_on(radio_unit)) {
397                 retval = -EIO;
398                 goto errunr;
399         }
400
401         dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ "  "
402                  __DATE__ "\n");
403         dev_info(&pdev->dev, "radio chip initialized\n");
404
405         return 0;
406 errunr:
407         video_unregister_device(maestro_radio_inst);
408 errfr1:
409         kfree(maestro_radio_inst);
410 errfr:
411         kfree(radio_unit);
412 err:
413         return retval;
414
415 }
416
417 static void __devexit maestro_remove(struct pci_dev *pdev)
418 {
419         struct video_device *vdev = pci_get_drvdata(pdev);
420
421         video_unregister_device(vdev);
422 }
423
424 static int __init maestro_radio_init(void)
425 {
426         int retval = pci_register_driver(&maestro_r_driver);
427
428         if (retval)
429                 printk(KERN_ERR "error during registration pci driver\n");
430
431         return retval;
432 }
433
434 static void __exit maestro_radio_exit(void)
435 {
436         pci_unregister_driver(&maestro_r_driver);
437 }
438
439 module_init(maestro_radio_init);
440 module_exit(maestro_radio_exit);
441
442 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
443 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
444 MODULE_LICENSE("GPL");