blob: 11f80cacd6edef44f0f224a768962a288f63cfa2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Maestro PCI sound card radio driver for Linux support
2 * (c) 2000 A. Tlalka, atlka@pg.gda.pl
3 * Notes on the hardware
4 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03005 * + Frequency control is done digitally
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * + 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
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -030017 *
18 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
24#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
26#include <asm/uaccess.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/pci.h>
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -030029#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030030#include <media/v4l2-common.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020031
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -030032#include <linux/version.h> /* for KERNEL_VERSION MACRO */
33#define RADIO_VERSION KERNEL_VERSION(0,0,6)
34#define DRIVER_VERSION "0.06"
35
36static 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -080047#define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
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
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -080054#define GPIO6 0x0040 /* mask bits for GPIO lines */
55#define GPIO7 0x0080
56#define GPIO8 0x0100
57#define GPIO9 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -080059#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define FREQ_LO 50*16000
65#define FREQ_HI 150*16000
66
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -080067#define FREQ_IF 171200 /* 10.7*16000 */
68#define FREQ_STEP 200 /* 12.5*16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
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
75static int radio_nr = -1;
76module_param(radio_nr, int, 0);
77
Jiri Slaby89dad8f2006-01-09 20:52:47 -080078static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
79static void maestro_remove(struct pci_dev *pdev);
80
81static struct pci_device_id maestro_r_pci_tbl[] = {
82 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
83 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
84 .class_mask = 0xffff00 },
85 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
86 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
87 .class_mask = 0xffff00 },
88 { 0 }
89};
90MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
91
92static struct pci_driver maestro_r_driver = {
93 .name = "maestro_radio",
94 .id_table = maestro_r_pci_tbl,
95 .probe = maestro_probe,
96 .remove = __devexit_p(maestro_remove),
97};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Arjan van de Venfa027c22007-02-12 00:55:33 -080099static const struct file_operations maestro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .owner = THIS_MODULE,
101 .open = video_exclusive_open,
102 .release = video_exclusive_release,
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300103 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200104 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .llseek = no_llseek,
106};
107
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800108struct radio_device {
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800109 u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 muted, /* VIDEO_AUDIO_MUTE */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300111 stereo, /* VIDEO_TUNER_STEREO_ON */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 tuned; /* signal strength (0 or 0xffff) */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200113 struct mutex lock;
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800114};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800116static u32 radio_bits_get(struct radio_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800118 register u16 io=dev->io, l, rdata;
119 register u32 data=0;
120 u16 omask;
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 omask = inw(io + IO_MASK);
123 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
124 outw(0, io);
125 udelay(16);
126
127 for (l=24;l--;) {
128 outw(STR_CLK, io); /* HI state */
129 udelay(2);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300130 if(!l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
132 outw(0, io); /* LO state */
133 udelay(2);
134 data <<= 1; /* shift data */
135 rdata = inw(io);
136 if(!l)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300137 dev->stereo = rdata & STR_MOST ?
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -0300138 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 else
140 if(rdata & STR_DATA)
141 data++;
142 udelay(2);
143 }
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if(dev->muted)
146 outw(STR_WREN, io);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 udelay(4);
149 outw(omask, io + IO_MASK);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return data & 0x3ffe;
152}
153
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800154static void radio_bits_set(struct radio_device *dev, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800156 register u16 io=dev->io, l, bits;
157 u16 omask, odir;
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 omask = inw(io + IO_MASK);
160 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
161 outw(odir | STR_DATA, io + IO_DIR);
162 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
163 udelay(16);
164 for (l=25;l;l--) {
165 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
166 data <<= 1; /* shift data */
167 outw(bits, io); /* start strobe */
168 udelay(2);
169 outw(bits | STR_CLK, io); /* HI level */
170 udelay(2);
171 outw(bits, io); /* LO level */
172 udelay(4);
173 }
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if(!dev->muted)
176 outw(0, io);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 udelay(4);
179 outw(omask, io + IO_MASK);
180 outw(odir, io + IO_DIR);
181 msleep(125);
182}
183
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300184static int vidioc_querycap(struct file *file, void *priv,
185 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300187 strlcpy(v->driver, "radio-maestro", sizeof(v->driver));
188 strlcpy(v->card, "Maestro Radio", sizeof(v->card));
189 sprintf(v->bus_info, "PCI");
190 v->version = RADIO_VERSION;
191 v->capabilities = V4L2_CAP_TUNER;
192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300195static int vidioc_g_tuner(struct file *file, void *priv,
196 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 struct video_device *dev = video_devdata(file);
Jiri Slabyf86e7762006-01-09 20:52:50 -0800199 struct radio_device *card = video_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300201 if (v->index > 0)
202 return -EINVAL;
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800203
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300204 (void)radio_bits_get(card);
205
206 strcpy(v->name, "FM");
207 v->type = V4L2_TUNER_RADIO;
208 v->rangelow = FREQ_LO;
209 v->rangehigh = FREQ_HI;
210 v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
211 v->capability = V4L2_TUNER_CAP_LOW;
212 if(card->stereo)
213 v->audmode = V4L2_TUNER_MODE_STEREO;
214 else
215 v->audmode = V4L2_TUNER_MODE_MONO;
216 v->signal = card->tuned;
217 return 0;
218}
219
220static int vidioc_s_tuner(struct file *file, void *priv,
221 struct v4l2_tuner *v)
222{
223 if (v->index > 0)
224 return -EINVAL;
225 return 0;
226}
227
228static int vidioc_s_frequency(struct file *file, void *priv,
229 struct v4l2_frequency *f)
230{
231 struct video_device *dev = video_devdata(file);
232 struct radio_device *card = video_get_drvdata(dev);
233
234 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
235 return -EINVAL;
236 radio_bits_set(card, FREQ2BITS(f->frequency));
237 return 0;
238}
239
240static int vidioc_g_frequency(struct file *file, void *priv,
241 struct v4l2_frequency *f)
242{
243 struct video_device *dev = video_devdata(file);
244 struct radio_device *card = video_get_drvdata(dev);
245
246 f->type = V4L2_TUNER_RADIO;
247 f->frequency = BITS2FREQ(radio_bits_get(card));
248 return 0;
249}
250
251static int vidioc_queryctrl(struct file *file, void *priv,
252 struct v4l2_queryctrl *qc)
253{
254 int i;
255
256 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
257 if (qc->id && qc->id == radio_qctrl[i].id) {
258 memcpy(qc, &(radio_qctrl[i]),
259 sizeof(*qc));
260 return 0;
261 }
262 }
263 return -EINVAL;
264}
265
266static int vidioc_g_ctrl(struct file *file, void *priv,
267 struct v4l2_control *ctrl)
268{
269 struct video_device *dev = video_devdata(file);
270 struct radio_device *card = video_get_drvdata(dev);
271
272 switch (ctrl->id) {
273 case V4L2_CID_AUDIO_MUTE:
274 ctrl->value = card->muted;
275 return 0;
276 }
277 return -EINVAL;
278}
279
280static int vidioc_s_ctrl(struct file *file, void *priv,
281 struct v4l2_control *ctrl)
282{
283 struct video_device *dev = video_devdata(file);
284 struct radio_device *card = video_get_drvdata(dev);
285 register u16 io = card->io;
286 register u16 omask = inw(io + IO_MASK);
287
288 switch (ctrl->id) {
289 case V4L2_CID_AUDIO_MUTE:
290 outw(~STR_WREN, io + IO_MASK);
291 outw((card->muted = ctrl->value ) ?
292 STR_WREN : 0, io);
293 udelay(4);
294 outw(omask, io + IO_MASK);
295 msleep(125);
296 return 0;
297 }
298 return -EINVAL;
299}
300
301static int vidioc_g_audio(struct file *file, void *priv,
302 struct v4l2_audio *a)
303{
304 if (a->index > 1)
305 return -EINVAL;
306
307 strcpy(a->name, "Radio");
308 a->capability = V4L2_AUDCAP_STEREO;
309 return 0;
310}
311
312static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
313{
314 *i = 0;
315 return 0;
316}
317
318static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
319{
320 if (i != 0)
321 return -EINVAL;
322 return 0;
323}
324
325static int vidioc_s_audio(struct file *file, void *priv,
326 struct v4l2_audio *a)
327{
328 if (a->index != 0)
329 return -EINVAL;
330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800333static u16 __devinit radio_power_on(struct radio_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800335 register u16 io = dev->io;
336 register u32 ofreq;
337 u16 omask, odir;
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 omask = inw(io + IO_MASK);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800340 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 outw(odir & ~STR_WREN, io + IO_DIR);
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -0300342 dev->muted = inw(io) & STR_WREN ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 outw(odir, io + IO_DIR);
344 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
345 outw(dev->muted ? 0 : STR_WREN, io);
346 udelay(16);
347 outw(omask, io + IO_MASK);
348 ofreq = radio_bits_get(dev);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800349
350 if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ofreq = FREQ2BITS(FREQ_LO);
352 radio_bits_set(dev, ofreq);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return (ofreq == radio_bits_get(dev));
355}
356
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300357static struct video_device maestro_radio = {
358 .name = "Maestro radio",
359 .type = VID_TYPE_TUNER,
360 .fops = &maestro_fops,
361 .vidioc_querycap = vidioc_querycap,
362 .vidioc_g_tuner = vidioc_g_tuner,
363 .vidioc_s_tuner = vidioc_s_tuner,
364 .vidioc_g_audio = vidioc_g_audio,
365 .vidioc_s_audio = vidioc_s_audio,
366 .vidioc_g_input = vidioc_g_input,
367 .vidioc_s_input = vidioc_s_input,
368 .vidioc_g_frequency = vidioc_g_frequency,
369 .vidioc_s_frequency = vidioc_s_frequency,
370 .vidioc_queryctrl = vidioc_queryctrl,
371 .vidioc_g_ctrl = vidioc_g_ctrl,
372 .vidioc_s_ctrl = vidioc_s_ctrl,
373};
374
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800375static int __devinit maestro_probe(struct pci_dev *pdev,
376 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800378 struct radio_device *radio_unit;
379 struct video_device *maestro_radio_inst;
380 int retval;
381
382 retval = pci_enable_device(pdev);
383 if (retval) {
384 dev_err(&pdev->dev, "enabling pci device failed!\n");
385 goto err;
386 }
387
388 retval = -ENOMEM;
389
390 radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
391 if (radio_unit == NULL) {
392 dev_err(&pdev->dev, "not enough memory\n");
393 goto err;
394 }
395
396 radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200397 mutex_init(&radio_unit->lock);
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800398
399 maestro_radio_inst = video_device_alloc();
400 if (maestro_radio_inst == NULL) {
401 dev_err(&pdev->dev, "not enough memory\n");
402 goto errfr;
403 }
404
405 memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
406 video_set_drvdata(maestro_radio_inst, radio_unit);
407 pci_set_drvdata(pdev, maestro_radio_inst);
408
409 retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
410 radio_nr);
411 if (retval) {
412 printk(KERN_ERR "can't register video device!\n");
413 goto errfr1;
414 }
415
416 if (!radio_power_on(radio_unit)) {
417 retval = -EIO;
418 goto errunr;
419 }
420
421 dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800422 __DATE__ "\n");
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800423 dev_info(&pdev->dev, "radio chip initialized\n");
424
425 return 0;
426errunr:
427 video_unregister_device(maestro_radio_inst);
428errfr1:
429 kfree(maestro_radio_inst);
430errfr:
431 kfree(radio_unit);
432err:
433 return retval;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800437static void __devexit maestro_remove(struct pci_dev *pdev)
438{
439 struct video_device *vdev = pci_get_drvdata(pdev);
440
441 video_unregister_device(vdev);
442}
443
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800444static int __init maestro_radio_init(void)
445{
446 int retval = pci_register_driver(&maestro_r_driver);
447
448 if (retval)
449 printk(KERN_ERR "error during registration pci driver\n");
450
451 return retval;
452}
453
454static void __exit maestro_radio_exit(void)
455{
456 pci_unregister_driver(&maestro_r_driver);
457}
458
459module_init(maestro_radio_init);
460module_exit(maestro_radio_exit);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800461
462MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
463MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
464MODULE_LICENSE("GPL");