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/version.h> /* for KERNEL_VERSION MACRO */
26 #include <linux/pci.h>
27 #include <linux/videodev2.h>
29 #include <linux/slab.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
33 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
34 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
35 MODULE_LICENSE("GPL");
37 static int radio_nr
= -1;
38 module_param(radio_nr
, int, 0);
40 #define RADIO_VERSION KERNEL_VERSION(0, 0, 6)
41 #define DRIVER_VERSION "0.06"
43 #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
45 #define IO_MASK 4 /* mask register offset from GPIO_DATA
46 bits 1=unmask write to given bit */
47 #define IO_DIR 8 /* direction register offset from GPIO_DATA
48 bits 0/1=read/write direction */
50 #define GPIO6 0x0040 /* mask bits for GPIO lines */
55 #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
57 #define STR_WREN GPIO8
58 #define STR_MOST GPIO9
60 #define FREQ_LO 50*16000
61 #define FREQ_HI 150*16000
63 #define FREQ_IF 171200 /* 10.7*16000 */
64 #define FREQ_STEP 200 /* 12.5*16 */
66 #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
67 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
69 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
72 struct v4l2_device v4l2_dev
;
73 struct video_device vdev
;
77 u16 io
; /* base of Maestro card radio io (GPIO_DATA)*/
78 u16 muted
; /* VIDEO_AUDIO_MUTE */
79 u16 stereo
; /* VIDEO_TUNER_STEREO_ON */
80 u16 tuned
; /* signal strength (0 or 0xffff) */
83 static inline struct maestro
*to_maestro(struct v4l2_device
*v4l2_dev
)
85 return container_of(v4l2_dev
, struct maestro
, v4l2_dev
);
88 static u32
radio_bits_get(struct maestro
*dev
)
90 u16 io
= dev
->io
, l
, rdata
;
94 omask
= inw(io
+ IO_MASK
);
95 outw(~(STR_CLK
| STR_WREN
), io
+ IO_MASK
);
100 outw(STR_CLK
, io
); /* HI state */
103 dev
->tuned
= inw(io
) & STR_MOST
? 0 : 0xffff;
104 outw(0, io
); /* LO state */
106 data
<<= 1; /* shift data */
109 dev
->stereo
= (rdata
& STR_MOST
) ? 0 : 1;
110 else if (rdata
& STR_DATA
)
119 outw(omask
, io
+ IO_MASK
);
121 return data
& 0x3ffe;
124 static void radio_bits_set(struct maestro
*dev
, u32 data
)
126 u16 io
= dev
->io
, l
, bits
;
129 omask
= inw(io
+ IO_MASK
);
130 odir
= (inw(io
+ IO_DIR
) & ~STR_DATA
) | (STR_CLK
| STR_WREN
);
131 outw(odir
| STR_DATA
, io
+ IO_DIR
);
132 outw(~(STR_DATA
| STR_CLK
| STR_WREN
), io
+ IO_MASK
);
134 for (l
= 25; l
; l
--) {
135 bits
= ((data
>> 18) & STR_DATA
) | STR_WREN
;
136 data
<<= 1; /* shift data */
137 outw(bits
, io
); /* start strobe */
139 outw(bits
| STR_CLK
, io
); /* HI level */
141 outw(bits
, io
); /* LO level */
149 outw(omask
, io
+ IO_MASK
);
150 outw(odir
, io
+ IO_DIR
);
154 static int vidioc_querycap(struct file
*file
, void *priv
,
155 struct v4l2_capability
*v
)
157 struct maestro
*dev
= video_drvdata(file
);
159 strlcpy(v
->driver
, "radio-maestro", sizeof(v
->driver
));
160 strlcpy(v
->card
, "Maestro Radio", sizeof(v
->card
));
161 snprintf(v
->bus_info
, sizeof(v
->bus_info
), "PCI:%s", pci_name(dev
->pdev
));
162 v
->version
= RADIO_VERSION
;
163 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
167 static int vidioc_g_tuner(struct file
*file
, void *priv
,
168 struct v4l2_tuner
*v
)
170 struct maestro
*dev
= video_drvdata(file
);
175 mutex_lock(&dev
->lock
);
178 strlcpy(v
->name
, "FM", sizeof(v
->name
));
179 v
->type
= V4L2_TUNER_RADIO
;
180 v
->rangelow
= FREQ_LO
;
181 v
->rangehigh
= FREQ_HI
;
182 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
183 v
->capability
= V4L2_TUNER_CAP_LOW
;
185 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
187 v
->audmode
= V4L2_TUNER_MODE_MONO
;
188 v
->signal
= dev
->tuned
;
189 mutex_unlock(&dev
->lock
);
193 static int vidioc_s_tuner(struct file
*file
, void *priv
,
194 struct v4l2_tuner
*v
)
196 return v
->index
? -EINVAL
: 0;
199 static int vidioc_s_frequency(struct file
*file
, void *priv
,
200 struct v4l2_frequency
*f
)
202 struct maestro
*dev
= video_drvdata(file
);
204 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
206 if (f
->frequency
< FREQ_LO
|| f
->frequency
> FREQ_HI
)
208 mutex_lock(&dev
->lock
);
209 radio_bits_set(dev
, FREQ2BITS(f
->frequency
));
210 mutex_unlock(&dev
->lock
);
214 static int vidioc_g_frequency(struct file
*file
, void *priv
,
215 struct v4l2_frequency
*f
)
217 struct maestro
*dev
= video_drvdata(file
);
221 f
->type
= V4L2_TUNER_RADIO
;
222 mutex_lock(&dev
->lock
);
223 f
->frequency
= BITS2FREQ(radio_bits_get(dev
));
224 mutex_unlock(&dev
->lock
);
228 static int vidioc_queryctrl(struct file
*file
, void *priv
,
229 struct v4l2_queryctrl
*qc
)
232 case V4L2_CID_AUDIO_MUTE
:
233 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
238 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
239 struct v4l2_control
*ctrl
)
241 struct maestro
*dev
= video_drvdata(file
);
244 case V4L2_CID_AUDIO_MUTE
:
245 ctrl
->value
= dev
->muted
;
251 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
252 struct v4l2_control
*ctrl
)
254 struct maestro
*dev
= video_drvdata(file
);
259 case V4L2_CID_AUDIO_MUTE
:
260 mutex_lock(&dev
->lock
);
261 omask
= inw(io
+ IO_MASK
);
262 outw(~STR_WREN
, io
+ IO_MASK
);
263 dev
->muted
= ctrl
->value
;
264 outw(dev
->muted
? STR_WREN
: 0, io
);
266 outw(omask
, io
+ IO_MASK
);
268 mutex_unlock(&dev
->lock
);
274 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
280 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
282 return i
? -EINVAL
: 0;
285 static int vidioc_g_audio(struct file
*file
, void *priv
,
286 struct v4l2_audio
*a
)
289 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
290 a
->capability
= V4L2_AUDCAP_STEREO
;
294 static int vidioc_s_audio(struct file
*file
, void *priv
,
295 struct v4l2_audio
*a
)
297 return a
->index
? -EINVAL
: 0;
300 static const struct v4l2_file_operations maestro_fops
= {
301 .owner
= THIS_MODULE
,
302 .unlocked_ioctl
= video_ioctl2
,
305 static const struct v4l2_ioctl_ops maestro_ioctl_ops
= {
306 .vidioc_querycap
= vidioc_querycap
,
307 .vidioc_g_tuner
= vidioc_g_tuner
,
308 .vidioc_s_tuner
= vidioc_s_tuner
,
309 .vidioc_g_audio
= vidioc_g_audio
,
310 .vidioc_s_audio
= vidioc_s_audio
,
311 .vidioc_g_input
= vidioc_g_input
,
312 .vidioc_s_input
= vidioc_s_input
,
313 .vidioc_g_frequency
= vidioc_g_frequency
,
314 .vidioc_s_frequency
= vidioc_s_frequency
,
315 .vidioc_queryctrl
= vidioc_queryctrl
,
316 .vidioc_g_ctrl
= vidioc_g_ctrl
,
317 .vidioc_s_ctrl
= vidioc_s_ctrl
,
320 static u16 __devinit
radio_power_on(struct maestro
*dev
)
322 register u16 io
= dev
->io
;
326 omask
= inw(io
+ IO_MASK
);
327 odir
= (inw(io
+ IO_DIR
) & ~STR_DATA
) | (STR_CLK
| STR_WREN
);
328 outw(odir
& ~STR_WREN
, io
+ IO_DIR
);
329 dev
->muted
= inw(io
) & STR_WREN
? 0 : 1;
330 outw(odir
, io
+ IO_DIR
);
331 outw(~(STR_WREN
| STR_CLK
), io
+ IO_MASK
);
332 outw(dev
->muted
? 0 : STR_WREN
, io
);
334 outw(omask
, io
+ IO_MASK
);
335 ofreq
= radio_bits_get(dev
);
337 if ((ofreq
< FREQ2BITS(FREQ_LO
)) || (ofreq
> FREQ2BITS(FREQ_HI
)))
338 ofreq
= FREQ2BITS(FREQ_LO
);
339 radio_bits_set(dev
, ofreq
);
341 return (ofreq
== radio_bits_get(dev
));
344 static int __devinit
maestro_probe(struct pci_dev
*pdev
,
345 const struct pci_device_id
*ent
)
348 struct v4l2_device
*v4l2_dev
;
351 retval
= pci_enable_device(pdev
);
353 dev_err(&pdev
->dev
, "enabling pci device failed!\n");
359 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
361 dev_err(&pdev
->dev
, "not enough memory\n");
365 v4l2_dev
= &dev
->v4l2_dev
;
366 mutex_init(&dev
->lock
);
369 strlcpy(v4l2_dev
->name
, "maestro", sizeof(v4l2_dev
->name
));
371 retval
= v4l2_device_register(&pdev
->dev
, v4l2_dev
);
373 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
377 dev
->io
= pci_resource_start(pdev
, 0) + GPIO_DATA
;
379 strlcpy(dev
->vdev
.name
, v4l2_dev
->name
, sizeof(dev
->vdev
.name
));
380 dev
->vdev
.v4l2_dev
= v4l2_dev
;
381 dev
->vdev
.fops
= &maestro_fops
;
382 dev
->vdev
.ioctl_ops
= &maestro_ioctl_ops
;
383 dev
->vdev
.release
= video_device_release_empty
;
384 video_set_drvdata(&dev
->vdev
, dev
);
386 if (!radio_power_on(dev
)) {
391 retval
= video_register_device(&dev
->vdev
, VFL_TYPE_RADIO
, radio_nr
);
393 v4l2_err(v4l2_dev
, "can't register video device!\n");
397 v4l2_info(v4l2_dev
, "version " DRIVER_VERSION
"\n");
401 v4l2_device_unregister(v4l2_dev
);
409 static void __devexit
maestro_remove(struct pci_dev
*pdev
)
411 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(&pdev
->dev
);
412 struct maestro
*dev
= to_maestro(v4l2_dev
);
414 video_unregister_device(&dev
->vdev
);
415 v4l2_device_unregister(&dev
->v4l2_dev
);
418 static struct pci_device_id maestro_r_pci_tbl
[] = {
419 { PCI_DEVICE(PCI_VENDOR_ID_ESS
, PCI_DEVICE_ID_ESS_ESS1968
),
420 .class = PCI_CLASS_MULTIMEDIA_AUDIO
<< 8,
421 .class_mask
= 0xffff00 },
422 { PCI_DEVICE(PCI_VENDOR_ID_ESS
, PCI_DEVICE_ID_ESS_ESS1978
),
423 .class = PCI_CLASS_MULTIMEDIA_AUDIO
<< 8,
424 .class_mask
= 0xffff00 },
427 MODULE_DEVICE_TABLE(pci
, maestro_r_pci_tbl
);
429 static struct pci_driver maestro_r_driver
= {
430 .name
= "maestro_radio",
431 .id_table
= maestro_r_pci_tbl
,
432 .probe
= maestro_probe
,
433 .remove
= __devexit_p(maestro_remove
),
436 static int __init
maestro_radio_init(void)
438 int retval
= pci_register_driver(&maestro_r_driver
);
441 printk(KERN_ERR
"error during registration pci driver\n");
446 static void __exit
maestro_radio_exit(void)
448 pci_unregister_driver(&maestro_r_driver
);
451 module_init(maestro_radio_init
);
452 module_exit(maestro_radio_exit
);