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