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
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/delay.h>
23 #include <linux/sched.h>
25 #include <asm/uaccess.h>
26 #include <linux/mutex.h>
27 #include <linux/pci.h>
28 #include <linux/videodev.h>
31 #define DRIVER_VERSION "0.05"
33 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
35 #define IO_MASK 4 /* mask register offset from GPIO_DATA
36 bits 1=unmask write to given bit */
37 #define IO_DIR 8 /* direction register offset from GPIO_DATA
38 bits 0/1=read/write direction */
40 #define GPIO6 0x0040 /* mask bits for GPIO lines */
45 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
47 #define STR_WREN GPIO8
48 #define STR_MOST GPIO9
50 #define FREQ_LO 50*16000
51 #define FREQ_HI 150*16000
53 #define FREQ_IF 171200 /* 10.7*16000 */
54 #define FREQ_STEP 200 /* 12.5*16 */
56 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
57 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
59 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
61 static int radio_nr
= -1;
62 module_param(radio_nr
, int, 0);
64 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
65 unsigned int cmd
, unsigned long arg
);
66 static int maestro_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
);
67 static void maestro_remove(struct pci_dev
*pdev
);
69 static struct pci_device_id maestro_r_pci_tbl
[] = {
70 { PCI_DEVICE(PCI_VENDOR_ID_ESS
, PCI_DEVICE_ID_ESS_ESS1968
),
71 .class = PCI_CLASS_MULTIMEDIA_AUDIO
<< 8,
72 .class_mask
= 0xffff00 },
73 { PCI_DEVICE(PCI_VENDOR_ID_ESS
, PCI_DEVICE_ID_ESS_ESS1978
),
74 .class = PCI_CLASS_MULTIMEDIA_AUDIO
<< 8,
75 .class_mask
= 0xffff00 },
78 MODULE_DEVICE_TABLE(pci
, maestro_r_pci_tbl
);
80 static struct pci_driver maestro_r_driver
= {
81 .name
= "maestro_radio",
82 .id_table
= maestro_r_pci_tbl
,
83 .probe
= maestro_probe
,
84 .remove
= __devexit_p(maestro_remove
),
87 static struct file_operations maestro_fops
= {
89 .open
= video_exclusive_open
,
90 .release
= video_exclusive_release
,
92 .compat_ioctl
= v4l_compat_ioctl32
,
96 static struct video_device maestro_radio
= {
97 .name
= "Maestro radio",
98 .type
= VID_TYPE_TUNER
,
99 .hardware
= VID_HARDWARE_SF16MI
,
100 .fops
= &maestro_fops
,
103 struct radio_device
{
104 u16 io
, /* base of Maestro card radio io (GPIO_DATA)*/
105 muted
, /* VIDEO_AUDIO_MUTE */
106 stereo
, /* VIDEO_TUNER_STEREO_ON */
107 tuned
; /* signal strength (0 or 0xffff) */
111 static u32
radio_bits_get(struct radio_device
*dev
)
113 register u16 io
=dev
->io
, l
, rdata
;
117 omask
= inw(io
+ IO_MASK
);
118 outw(~(STR_CLK
| STR_WREN
), io
+ IO_MASK
);
123 outw(STR_CLK
, io
); /* HI state */
126 dev
->tuned
= inw(io
) & STR_MOST
? 0 : 0xffff;
127 outw(0, io
); /* LO state */
129 data
<<= 1; /* shift data */
132 dev
->stereo
= rdata
& STR_MOST
?
133 0 : VIDEO_TUNER_STEREO_ON
;
144 outw(omask
, io
+ IO_MASK
);
146 return data
& 0x3ffe;
149 static void radio_bits_set(struct radio_device
*dev
, u32 data
)
151 register u16 io
=dev
->io
, l
, bits
;
154 omask
= inw(io
+ IO_MASK
);
155 odir
= (inw(io
+ IO_DIR
) & ~STR_DATA
) | (STR_CLK
| STR_WREN
);
156 outw(odir
| STR_DATA
, io
+ IO_DIR
);
157 outw(~(STR_DATA
| STR_CLK
| STR_WREN
), io
+ IO_MASK
);
160 bits
= ((data
>> 18) & STR_DATA
) | STR_WREN
;
161 data
<<= 1; /* shift data */
162 outw(bits
, io
); /* start strobe */
164 outw(bits
| STR_CLK
, io
); /* HI level */
166 outw(bits
, io
); /* LO level */
174 outw(omask
, io
+ IO_MASK
);
175 outw(odir
, io
+ IO_DIR
);
179 static inline int radio_function(struct inode
*inode
, struct file
*file
,
180 unsigned int cmd
, void *arg
)
182 struct video_device
*dev
= video_devdata(file
);
183 struct radio_device
*card
= video_get_drvdata(dev
);
187 struct video_capability
*v
= arg
;
188 memset(v
, 0, sizeof(*v
));
189 strcpy(v
->name
, "Maestro radio");
190 v
->type
= VID_TYPE_TUNER
;
191 v
->channels
= v
->audios
= 1;
193 } case VIDIOCGTUNER
: {
194 struct video_tuner
*v
= arg
;
197 (void)radio_bits_get(card
);
198 v
->flags
= VIDEO_TUNER_LOW
| card
->stereo
;
199 v
->signal
= card
->tuned
;
200 strcpy(v
->name
, "FM");
201 v
->rangelow
= FREQ_LO
;
202 v
->rangehigh
= FREQ_HI
;
203 v
->mode
= VIDEO_MODE_AUTO
;
205 } case VIDIOCSTUNER
: {
206 struct video_tuner
*v
= arg
;
210 } case VIDIOCGFREQ
: {
211 unsigned long *freq
= arg
;
212 *freq
= BITS2FREQ(radio_bits_get(card
));
214 } case VIDIOCSFREQ
: {
215 unsigned long *freq
= arg
;
216 if (*freq
< FREQ_LO
|| *freq
> FREQ_HI
)
218 radio_bits_set(card
, FREQ2BITS(*freq
));
220 } case VIDIOCGAUDIO
: {
221 struct video_audio
*v
= arg
;
222 memset(v
, 0, sizeof(*v
));
223 strcpy(v
->name
, "Radio");
224 v
->flags
= VIDEO_AUDIO_MUTABLE
| card
->muted
;
225 v
->mode
= VIDEO_SOUND_STEREO
;
227 } case VIDIOCSAUDIO
: {
228 struct video_audio
*v
= arg
;
232 register u16 io
= card
->io
;
233 register u16 omask
= inw(io
+ IO_MASK
);
234 outw(~STR_WREN
, io
+ IO_MASK
);
235 outw((card
->muted
= v
->flags
& VIDEO_AUDIO_MUTE
) ?
238 outw(omask
, io
+ IO_MASK
);
242 } case VIDIOCGUNIT
: {
243 struct video_unit
*v
= arg
;
244 v
->video
= VIDEO_NO_UNIT
;
245 v
->vbi
= VIDEO_NO_UNIT
;
246 v
->radio
= dev
->minor
;
248 v
->teletext
= VIDEO_NO_UNIT
;
255 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
256 unsigned int cmd
, unsigned long arg
)
258 struct video_device
*dev
= video_devdata(file
);
259 struct radio_device
*card
= video_get_drvdata(dev
);
262 mutex_lock(&card
->lock
);
263 ret
= video_usercopy(inode
, file
, cmd
, arg
, radio_function
);
264 mutex_unlock(&card
->lock
);
269 static u16 __devinit
radio_power_on(struct radio_device
*dev
)
271 register u16 io
= dev
->io
;
275 omask
= inw(io
+ IO_MASK
);
276 odir
= (inw(io
+ IO_DIR
) & ~STR_DATA
) | (STR_CLK
| STR_WREN
);
277 outw(odir
& ~STR_WREN
, io
+ IO_DIR
);
278 dev
->muted
= inw(io
) & STR_WREN
? 0 : VIDEO_AUDIO_MUTE
;
279 outw(odir
, io
+ IO_DIR
);
280 outw(~(STR_WREN
| STR_CLK
), io
+ IO_MASK
);
281 outw(dev
->muted
? 0 : STR_WREN
, io
);
283 outw(omask
, io
+ IO_MASK
);
284 ofreq
= radio_bits_get(dev
);
286 if ((ofreq
< FREQ2BITS(FREQ_LO
)) || (ofreq
> FREQ2BITS(FREQ_HI
)))
287 ofreq
= FREQ2BITS(FREQ_LO
);
288 radio_bits_set(dev
, ofreq
);
290 return (ofreq
== radio_bits_get(dev
));
293 static int __devinit
maestro_probe(struct pci_dev
*pdev
,
294 const struct pci_device_id
*ent
)
296 struct radio_device
*radio_unit
;
297 struct video_device
*maestro_radio_inst
;
300 retval
= pci_enable_device(pdev
);
302 dev_err(&pdev
->dev
, "enabling pci device failed!\n");
308 radio_unit
= kzalloc(sizeof(*radio_unit
), GFP_KERNEL
);
309 if (radio_unit
== NULL
) {
310 dev_err(&pdev
->dev
, "not enough memory\n");
314 radio_unit
->io
= pci_resource_start(pdev
, 0) + GPIO_DATA
;
315 mutex_init(&radio_unit
->lock
);
317 maestro_radio_inst
= video_device_alloc();
318 if (maestro_radio_inst
== NULL
) {
319 dev_err(&pdev
->dev
, "not enough memory\n");
323 memcpy(maestro_radio_inst
, &maestro_radio
, sizeof(maestro_radio
));
324 video_set_drvdata(maestro_radio_inst
, radio_unit
);
325 pci_set_drvdata(pdev
, maestro_radio_inst
);
327 retval
= video_register_device(maestro_radio_inst
, VFL_TYPE_RADIO
,
330 printk(KERN_ERR
"can't register video device!\n");
334 if (!radio_power_on(radio_unit
)) {
339 dev_info(&pdev
->dev
, "version " DRIVER_VERSION
" time " __TIME__
" "
341 dev_info(&pdev
->dev
, "radio chip initialized\n");
345 video_unregister_device(maestro_radio_inst
);
347 kfree(maestro_radio_inst
);
355 static void __devexit
maestro_remove(struct pci_dev
*pdev
)
357 struct video_device
*vdev
= pci_get_drvdata(pdev
);
359 video_unregister_device(vdev
);
362 static int __init
maestro_radio_init(void)
364 int retval
= pci_register_driver(&maestro_r_driver
);
367 printk(KERN_ERR
"error during registration pci driver\n");
372 static void __exit
maestro_radio_exit(void)
374 pci_unregister_driver(&maestro_r_driver
);
377 module_init(maestro_radio_init
);
378 module_exit(maestro_radio_exit
);
380 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
381 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
382 MODULE_LICENSE("GPL");