1 /* Maestro PCI sound card radio driver for Linux support
2 * (c) 2000 A. Tlalka, atlka@pg.gda.pl
3 * Notes on the hardware
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.
11 * + io port is automatically detected - only the first radio is used
13 * + thread access locking additions
16 * + VIDEO_TUNER_LOW is permanent
18 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/ioport.h>
24 #include <linux/delay.h>
26 #include <asm/uaccess.h>
27 #include <linux/pci.h>
28 #include <linux/videodev2.h>
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-ioctl.h>
32 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
33 #define RADIO_VERSION KERNEL_VERSION(0,0,6)
34 #define DRIVER_VERSION "0.06"
36 static struct v4l2_queryctrl radio_qctrl
[] = {
38 .id
= V4L2_CID_AUDIO_MUTE
,
43 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
47 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
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 */
54 #define GPIO6 0x0040 /* mask bits for GPIO lines */
59 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
61 #define STR_WREN GPIO8
62 #define STR_MOST GPIO9
64 #define FREQ_LO 50*16000
65 #define FREQ_HI 150*16000
67 #define FREQ_IF 171200 /* 10.7*16000 */
68 #define FREQ_STEP 200 /* 12.5*16 */
70 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
71 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
73 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
75 static int radio_nr
= -1;
76 module_param(radio_nr
, int, 0);
78 static unsigned long in_use
;
80 static int maestro_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
);
82 static int maestro_exclusive_open(struct file
*file
)
84 return test_and_set_bit(0, &in_use
) ? -EBUSY
: 0;
87 static int maestro_exclusive_release(struct file
*file
)
89 clear_bit(0, &in_use
);
93 static void maestro_remove(struct pci_dev
*pdev
);
95 static struct pci_device_id maestro_r_pci_tbl
[] = {
96 { PCI_DEVICE(PCI_VENDOR_ID_ESS
, PCI_DEVICE_ID_ESS_ESS1968
),
97 .class = PCI_CLASS_MULTIMEDIA_AUDIO
<< 8,
98 .class_mask
= 0xffff00 },
99 { PCI_DEVICE(PCI_VENDOR_ID_ESS
, PCI_DEVICE_ID_ESS_ESS1978
),
100 .class = PCI_CLASS_MULTIMEDIA_AUDIO
<< 8,
101 .class_mask
= 0xffff00 },
104 MODULE_DEVICE_TABLE(pci
, maestro_r_pci_tbl
);
106 static struct pci_driver maestro_r_driver
= {
107 .name
= "maestro_radio",
108 .id_table
= maestro_r_pci_tbl
,
109 .probe
= maestro_probe
,
110 .remove
= __devexit_p(maestro_remove
),
113 static const struct v4l2_file_operations maestro_fops
= {
114 .owner
= THIS_MODULE
,
115 .open
= maestro_exclusive_open
,
116 .release
= maestro_exclusive_release
,
117 .ioctl
= video_ioctl2
,
120 struct radio_device
{
121 u16 io
, /* base of Maestro card radio io (GPIO_DATA)*/
122 muted
, /* VIDEO_AUDIO_MUTE */
123 stereo
, /* VIDEO_TUNER_STEREO_ON */
124 tuned
; /* signal strength (0 or 0xffff) */
127 static u32
radio_bits_get(struct radio_device
*dev
)
129 register u16 io
=dev
->io
, l
, rdata
;
133 omask
= inw(io
+ IO_MASK
);
134 outw(~(STR_CLK
| STR_WREN
), io
+ IO_MASK
);
139 outw(STR_CLK
, io
); /* HI state */
142 dev
->tuned
= inw(io
) & STR_MOST
? 0 : 0xffff;
143 outw(0, io
); /* LO state */
145 data
<<= 1; /* shift data */
148 dev
->stereo
= rdata
& STR_MOST
?
160 outw(omask
, io
+ IO_MASK
);
162 return data
& 0x3ffe;
165 static void radio_bits_set(struct radio_device
*dev
, u32 data
)
167 register u16 io
=dev
->io
, l
, bits
;
170 omask
= inw(io
+ IO_MASK
);
171 odir
= (inw(io
+ IO_DIR
) & ~STR_DATA
) | (STR_CLK
| STR_WREN
);
172 outw(odir
| STR_DATA
, io
+ IO_DIR
);
173 outw(~(STR_DATA
| STR_CLK
| STR_WREN
), io
+ IO_MASK
);
176 bits
= ((data
>> 18) & STR_DATA
) | STR_WREN
;
177 data
<<= 1; /* shift data */
178 outw(bits
, io
); /* start strobe */
180 outw(bits
| STR_CLK
, io
); /* HI level */
182 outw(bits
, io
); /* LO level */
190 outw(omask
, io
+ IO_MASK
);
191 outw(odir
, io
+ IO_DIR
);
195 static int vidioc_querycap(struct file
*file
, void *priv
,
196 struct v4l2_capability
*v
)
198 strlcpy(v
->driver
, "radio-maestro", sizeof(v
->driver
));
199 strlcpy(v
->card
, "Maestro Radio", sizeof(v
->card
));
200 sprintf(v
->bus_info
, "PCI");
201 v
->version
= RADIO_VERSION
;
202 v
->capabilities
= V4L2_CAP_TUNER
;
206 static int vidioc_g_tuner(struct file
*file
, void *priv
,
207 struct v4l2_tuner
*v
)
209 struct radio_device
*card
= video_drvdata(file
);
214 (void)radio_bits_get(card
);
216 strcpy(v
->name
, "FM");
217 v
->type
= V4L2_TUNER_RADIO
;
218 v
->rangelow
= FREQ_LO
;
219 v
->rangehigh
= FREQ_HI
;
220 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
|V4L2_TUNER_SUB_STEREO
;
221 v
->capability
= V4L2_TUNER_CAP_LOW
;
223 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
225 v
->audmode
= V4L2_TUNER_MODE_MONO
;
226 v
->signal
= card
->tuned
;
230 static int vidioc_s_tuner(struct file
*file
, void *priv
,
231 struct v4l2_tuner
*v
)
238 static int vidioc_s_frequency(struct file
*file
, void *priv
,
239 struct v4l2_frequency
*f
)
241 struct radio_device
*card
= video_drvdata(file
);
243 if (f
->frequency
< FREQ_LO
|| f
->frequency
> FREQ_HI
)
245 radio_bits_set(card
, FREQ2BITS(f
->frequency
));
249 static int vidioc_g_frequency(struct file
*file
, void *priv
,
250 struct v4l2_frequency
*f
)
252 struct radio_device
*card
= video_drvdata(file
);
254 f
->type
= V4L2_TUNER_RADIO
;
255 f
->frequency
= BITS2FREQ(radio_bits_get(card
));
259 static int vidioc_queryctrl(struct file
*file
, void *priv
,
260 struct v4l2_queryctrl
*qc
)
264 for (i
= 0; i
< ARRAY_SIZE(radio_qctrl
); i
++) {
265 if (qc
->id
&& qc
->id
== radio_qctrl
[i
].id
) {
266 memcpy(qc
, &(radio_qctrl
[i
]),
274 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
275 struct v4l2_control
*ctrl
)
277 struct radio_device
*card
= video_drvdata(file
);
280 case V4L2_CID_AUDIO_MUTE
:
281 ctrl
->value
= card
->muted
;
287 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
288 struct v4l2_control
*ctrl
)
290 struct radio_device
*card
= video_drvdata(file
);
291 register u16 io
= card
->io
;
292 register u16 omask
= inw(io
+ IO_MASK
);
295 case V4L2_CID_AUDIO_MUTE
:
296 outw(~STR_WREN
, io
+ IO_MASK
);
297 outw((card
->muted
= ctrl
->value
) ?
300 outw(omask
, io
+ IO_MASK
);
307 static int vidioc_g_audio(struct file
*file
, void *priv
,
308 struct v4l2_audio
*a
)
313 strcpy(a
->name
, "Radio");
314 a
->capability
= V4L2_AUDCAP_STEREO
;
318 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
324 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
331 static int vidioc_s_audio(struct file
*file
, void *priv
,
332 struct v4l2_audio
*a
)
339 static u16 __devinit
radio_power_on(struct radio_device
*dev
)
341 register u16 io
= dev
->io
;
345 omask
= inw(io
+ IO_MASK
);
346 odir
= (inw(io
+ IO_DIR
) & ~STR_DATA
) | (STR_CLK
| STR_WREN
);
347 outw(odir
& ~STR_WREN
, io
+ IO_DIR
);
348 dev
->muted
= inw(io
) & STR_WREN
? 0 : 1;
349 outw(odir
, io
+ IO_DIR
);
350 outw(~(STR_WREN
| STR_CLK
), io
+ IO_MASK
);
351 outw(dev
->muted
? 0 : STR_WREN
, io
);
353 outw(omask
, io
+ IO_MASK
);
354 ofreq
= radio_bits_get(dev
);
356 if ((ofreq
< FREQ2BITS(FREQ_LO
)) || (ofreq
> FREQ2BITS(FREQ_HI
)))
357 ofreq
= FREQ2BITS(FREQ_LO
);
358 radio_bits_set(dev
, ofreq
);
360 return (ofreq
== radio_bits_get(dev
));
363 static const struct v4l2_ioctl_ops maestro_ioctl_ops
= {
364 .vidioc_querycap
= vidioc_querycap
,
365 .vidioc_g_tuner
= vidioc_g_tuner
,
366 .vidioc_s_tuner
= vidioc_s_tuner
,
367 .vidioc_g_audio
= vidioc_g_audio
,
368 .vidioc_s_audio
= vidioc_s_audio
,
369 .vidioc_g_input
= vidioc_g_input
,
370 .vidioc_s_input
= vidioc_s_input
,
371 .vidioc_g_frequency
= vidioc_g_frequency
,
372 .vidioc_s_frequency
= vidioc_s_frequency
,
373 .vidioc_queryctrl
= vidioc_queryctrl
,
374 .vidioc_g_ctrl
= vidioc_g_ctrl
,
375 .vidioc_s_ctrl
= vidioc_s_ctrl
,
378 static struct video_device maestro_radio
= {
379 .name
= "Maestro radio",
380 .fops
= &maestro_fops
,
381 .ioctl_ops
= &maestro_ioctl_ops
,
382 .release
= video_device_release
,
385 static int __devinit
maestro_probe(struct pci_dev
*pdev
,
386 const struct pci_device_id
*ent
)
388 struct radio_device
*radio_unit
;
389 struct video_device
*maestro_radio_inst
;
392 retval
= pci_enable_device(pdev
);
394 dev_err(&pdev
->dev
, "enabling pci device failed!\n");
400 radio_unit
= kzalloc(sizeof(*radio_unit
), GFP_KERNEL
);
401 if (radio_unit
== NULL
) {
402 dev_err(&pdev
->dev
, "not enough memory\n");
406 radio_unit
->io
= pci_resource_start(pdev
, 0) + GPIO_DATA
;
408 maestro_radio_inst
= video_device_alloc();
409 if (maestro_radio_inst
== NULL
) {
410 dev_err(&pdev
->dev
, "not enough memory\n");
414 memcpy(maestro_radio_inst
, &maestro_radio
, sizeof(maestro_radio
));
415 video_set_drvdata(maestro_radio_inst
, radio_unit
);
416 pci_set_drvdata(pdev
, maestro_radio_inst
);
418 retval
= video_register_device(maestro_radio_inst
, VFL_TYPE_RADIO
, radio_nr
);
420 printk(KERN_ERR
"can't register video device!\n");
424 if (!radio_power_on(radio_unit
)) {
429 dev_info(&pdev
->dev
, "version " DRIVER_VERSION
" time " __TIME__
" "
431 dev_info(&pdev
->dev
, "radio chip initialized\n");
435 video_unregister_device(maestro_radio_inst
);
437 video_device_release(maestro_radio_inst
);
445 static void __devexit
maestro_remove(struct pci_dev
*pdev
)
447 struct video_device
*vdev
= pci_get_drvdata(pdev
);
449 video_unregister_device(vdev
);
452 static int __init
maestro_radio_init(void)
454 int retval
= pci_register_driver(&maestro_r_driver
);
457 printk(KERN_ERR
"error during registration pci driver\n");
462 static void __exit
maestro_radio_exit(void)
464 pci_unregister_driver(&maestro_r_driver
);
467 module_init(maestro_radio_init
);
468 module_exit(maestro_radio_exit
);
470 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
471 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
472 MODULE_LICENSE("GPL");