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>
25 #include <linux/sched.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>
33 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
34 #define RADIO_VERSION KERNEL_VERSION(0,0,6)
35 #define DRIVER_VERSION "0.06"
37 static struct v4l2_queryctrl radio_qctrl
[] = {
39 .id
= V4L2_CID_AUDIO_MUTE
,
44 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
48 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
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 */
55 #define GPIO6 0x0040 /* mask bits for GPIO lines */
60 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
62 #define STR_WREN GPIO8
63 #define STR_MOST GPIO9
65 #define FREQ_LO 50*16000
66 #define FREQ_HI 150*16000
68 #define FREQ_IF 171200 /* 10.7*16000 */
69 #define FREQ_STEP 200 /* 12.5*16 */
71 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
72 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
74 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
76 static int radio_nr
= -1;
77 module_param(radio_nr
, int, 0);
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
);
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 },
93 MODULE_DEVICE_TABLE(pci
, maestro_r_pci_tbl
);
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
),
102 static 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
,
111 static struct video_device maestro_radio
= {
112 .name
= "Maestro radio",
113 .type
= VID_TYPE_TUNER
,
115 .fops
= &maestro_fops
,
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) */
126 static u32
radio_bits_get(struct radio_device
*dev
)
128 register u16 io
=dev
->io
, l
, rdata
;
132 omask
= inw(io
+ IO_MASK
);
133 outw(~(STR_CLK
| STR_WREN
), io
+ IO_MASK
);
138 outw(STR_CLK
, io
); /* HI state */
141 dev
->tuned
= inw(io
) & STR_MOST
? 0 : 0xffff;
142 outw(0, io
); /* LO state */
144 data
<<= 1; /* shift data */
147 dev
->stereo
= rdata
& STR_MOST
?
159 outw(omask
, io
+ IO_MASK
);
161 return data
& 0x3ffe;
164 static void radio_bits_set(struct radio_device
*dev
, u32 data
)
166 register u16 io
=dev
->io
, l
, bits
;
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
);
175 bits
= ((data
>> 18) & STR_DATA
) | STR_WREN
;
176 data
<<= 1; /* shift data */
177 outw(bits
, io
); /* start strobe */
179 outw(bits
| STR_CLK
, io
); /* HI level */
181 outw(bits
, io
); /* LO level */
189 outw(omask
, io
+ IO_MASK
);
190 outw(odir
, io
+ IO_DIR
);
194 static inline int radio_function(struct inode
*inode
, struct file
*file
,
195 unsigned int cmd
, void *arg
)
197 struct video_device
*dev
= video_devdata(file
);
198 struct radio_device
*card
= video_get_drvdata(dev
);
201 case VIDIOC_QUERYCAP
:
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
;
215 struct v4l2_tuner
*v
= arg
;
220 (void)radio_bits_get(card
);
222 memset(v
,0,sizeof(*v
));
223 strcpy(v
->name
, "FM");
224 v
->type
= V4L2_TUNER_RADIO
;
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
;
231 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
233 v
->audmode
= V4L2_TUNER_MODE_MONO
;
234 v
->signal
=card
->tuned
;
240 struct v4l2_tuner
*v
= arg
;
247 case VIDIOC_S_FREQUENCY
:
249 struct v4l2_frequency
*f
= arg
;
251 if (f
->frequency
< FREQ_LO
|| f
->frequency
> FREQ_HI
)
253 radio_bits_set(card
, FREQ2BITS(f
->frequency
));
257 case VIDIOC_G_FREQUENCY
:
259 struct v4l2_frequency
*f
= arg
;
261 f
->type
= V4L2_TUNER_RADIO
;
262 f
->frequency
= BITS2FREQ(radio_bits_get(card
));
266 case VIDIOC_QUERYCTRL
:
268 struct v4l2_queryctrl
*qc
= arg
;
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
]),
282 struct v4l2_control
*ctrl
= arg
;
285 case V4L2_CID_AUDIO_MUTE
:
286 ctrl
->value
=card
->muted
;
293 struct v4l2_control
*ctrl
= arg
;
296 case V4L2_CID_AUDIO_MUTE
:
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
) ?
304 outw(omask
, io
+ IO_MASK
);
313 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
318 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
319 unsigned int cmd
, unsigned long arg
)
321 struct video_device
*dev
= video_devdata(file
);
322 struct radio_device
*card
= video_get_drvdata(dev
);
325 mutex_lock(&card
->lock
);
326 ret
= video_usercopy(inode
, file
, cmd
, arg
, radio_function
);
327 mutex_unlock(&card
->lock
);
332 static u16 __devinit
radio_power_on(struct radio_device
*dev
)
334 register u16 io
= dev
->io
;
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
);
346 outw(omask
, io
+ IO_MASK
);
347 ofreq
= radio_bits_get(dev
);
349 if ((ofreq
< FREQ2BITS(FREQ_LO
)) || (ofreq
> FREQ2BITS(FREQ_HI
)))
350 ofreq
= FREQ2BITS(FREQ_LO
);
351 radio_bits_set(dev
, ofreq
);
353 return (ofreq
== radio_bits_get(dev
));
356 static int __devinit
maestro_probe(struct pci_dev
*pdev
,
357 const struct pci_device_id
*ent
)
359 struct radio_device
*radio_unit
;
360 struct video_device
*maestro_radio_inst
;
363 retval
= pci_enable_device(pdev
);
365 dev_err(&pdev
->dev
, "enabling pci device failed!\n");
371 radio_unit
= kzalloc(sizeof(*radio_unit
), GFP_KERNEL
);
372 if (radio_unit
== NULL
) {
373 dev_err(&pdev
->dev
, "not enough memory\n");
377 radio_unit
->io
= pci_resource_start(pdev
, 0) + GPIO_DATA
;
378 mutex_init(&radio_unit
->lock
);
380 maestro_radio_inst
= video_device_alloc();
381 if (maestro_radio_inst
== NULL
) {
382 dev_err(&pdev
->dev
, "not enough memory\n");
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
);
390 retval
= video_register_device(maestro_radio_inst
, VFL_TYPE_RADIO
,
393 printk(KERN_ERR
"can't register video device!\n");
397 if (!radio_power_on(radio_unit
)) {
402 dev_info(&pdev
->dev
, "version " DRIVER_VERSION
" time " __TIME__
" "
404 dev_info(&pdev
->dev
, "radio chip initialized\n");
408 video_unregister_device(maestro_radio_inst
);
410 kfree(maestro_radio_inst
);
418 static void __devexit
maestro_remove(struct pci_dev
*pdev
)
420 struct video_device
*vdev
= pci_get_drvdata(pdev
);
422 video_unregister_device(vdev
);
425 static int __init
maestro_radio_init(void)
427 int retval
= pci_register_driver(&maestro_r_driver
);
430 printk(KERN_ERR
"error during registration pci driver\n");
435 static void __exit
maestro_radio_exit(void)
437 pci_unregister_driver(&maestro_r_driver
);
440 module_init(maestro_radio_init
);
441 module_exit(maestro_radio_exit
);
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");