1 /* radio-trust.c - Trust FM Radio card driver for Linux 2.2
2 * by Eric Lammerts <eric@scintilla.utwente.nl>
4 * Based on radio-aztech.c. Original notes:
6 * Adapted to support the Video for Linux API by
7 * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
11 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
12 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
13 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
15 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
23 #include <asm/uaccess.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-ioctl.h>
28 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
29 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
31 static struct v4l2_queryctrl radio_qctrl
[] = {
33 .id
= V4L2_CID_AUDIO_MUTE
,
38 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
40 .id
= V4L2_CID_AUDIO_VOLUME
,
45 .default_value
= 65535,
46 .type
= V4L2_CTRL_TYPE_INTEGER
,
48 .id
= V4L2_CID_AUDIO_BASS
,
53 .default_value
= 32768,
54 .type
= V4L2_CTRL_TYPE_INTEGER
,
56 .id
= V4L2_CID_AUDIO_TREBLE
,
61 .default_value
= 32768,
62 .type
= V4L2_CTRL_TYPE_INTEGER
,
66 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
68 #ifndef CONFIG_RADIO_TRUST_PORT
69 #define CONFIG_RADIO_TRUST_PORT -1
72 static int io
= CONFIG_RADIO_TRUST_PORT
;
73 static int radio_nr
= -1;
74 static int ioval
= 0xf;
77 static __u16 curtreble
;
78 static unsigned long curfreq
;
83 #define TDA7318_ADDR 0x88
84 #define TSA6060T_ADDR 0xc4
86 #define TR_DELAY do { inb(io); inb(io); inb(io); } while(0)
87 #define TR_SET_SCL outb(ioval |= 2, io)
88 #define TR_CLR_SCL outb(ioval &= 0xfd, io)
89 #define TR_SET_SDA outb(ioval |= 1, io)
90 #define TR_CLR_SDA outb(ioval &= 0xfe, io)
92 static void write_i2c(int n
, ...)
94 unsigned char val
, mask
;
108 val
= va_arg(args
, unsigned);
109 for(mask
= 0x80; mask
; mask
>>= 1) {
119 /* acknowledge bit */
138 static void tr_setvol(__u16 vol
)
141 write_i2c(2, TDA7318_ADDR
, curvol
^ 0x1f);
144 static int basstreble2chip
[15] = {
145 0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
148 static void tr_setbass(__u16 bass
)
150 curbass
= bass
/ 4370;
151 write_i2c(2, TDA7318_ADDR
, 0x60 | basstreble2chip
[curbass
]);
154 static void tr_settreble(__u16 treble
)
156 curtreble
= treble
/ 4370;
157 write_i2c(2, TDA7318_ADDR
, 0x70 | basstreble2chip
[curtreble
]);
160 static void tr_setstereo(int stereo
)
162 curstereo
= !!stereo
;
163 ioval
= (ioval
& 0xfb) | (!curstereo
<< 2);
167 static void tr_setmute(int mute
)
170 ioval
= (ioval
& 0xf7) | (curmute
<< 3);
174 static int tr_getsigstr(void)
178 for(i
= 0, v
= 0; i
< 100; i
++) v
|= inb(io
);
179 return (v
& 1)? 0 : 0xffff;
182 static int tr_getstereo(void)
184 /* don't know how to determine it, just return the setting */
188 static void tr_setfreq(unsigned long f
)
190 f
/= 160; /* Convert to 10 kHz units */
191 f
+= 1070; /* Add 10.7 MHz IF */
193 write_i2c(5, TSA6060T_ADDR
, (f
<< 1) | 1, f
>> 7, 0x60 | ((f
>> 15) & 1), 0);
196 static int vidioc_querycap(struct file
*file
, void *priv
,
197 struct v4l2_capability
*v
)
199 strlcpy(v
->driver
, "radio-trust", sizeof(v
->driver
));
200 strlcpy(v
->card
, "Trust FM Radio", sizeof(v
->card
));
201 sprintf(v
->bus_info
, "ISA");
202 v
->version
= RADIO_VERSION
;
203 v
->capabilities
= V4L2_CAP_TUNER
;
207 static int vidioc_g_tuner(struct file
*file
, void *priv
,
208 struct v4l2_tuner
*v
)
213 strcpy(v
->name
, "FM");
214 v
->type
= V4L2_TUNER_RADIO
;
215 v
->rangelow
= (87.5*16000);
216 v
->rangehigh
= (108*16000);
217 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
|V4L2_TUNER_SUB_STEREO
;
218 v
->capability
= V4L2_TUNER_CAP_LOW
;
220 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
222 v
->audmode
= V4L2_TUNER_MODE_MONO
;
223 v
->signal
= tr_getsigstr();
227 static int vidioc_s_tuner(struct file
*file
, void *priv
,
228 struct v4l2_tuner
*v
)
236 static int vidioc_s_frequency(struct file
*file
, void *priv
,
237 struct v4l2_frequency
*f
)
239 curfreq
= f
->frequency
;
244 static int vidioc_g_frequency(struct file
*file
, void *priv
,
245 struct v4l2_frequency
*f
)
247 f
->type
= V4L2_TUNER_RADIO
;
248 f
->frequency
= curfreq
;
252 static int vidioc_queryctrl(struct file
*file
, void *priv
,
253 struct v4l2_queryctrl
*qc
)
257 for (i
= 0; i
< ARRAY_SIZE(radio_qctrl
); i
++) {
258 if (qc
->id
&& qc
->id
== radio_qctrl
[i
].id
) {
259 memcpy(qc
, &(radio_qctrl
[i
]),
267 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
268 struct v4l2_control
*ctrl
)
271 case V4L2_CID_AUDIO_MUTE
:
272 ctrl
->value
= curmute
;
274 case V4L2_CID_AUDIO_VOLUME
:
275 ctrl
->value
= curvol
* 2048;
277 case V4L2_CID_AUDIO_BASS
:
278 ctrl
->value
= curbass
* 4370;
280 case V4L2_CID_AUDIO_TREBLE
:
281 ctrl
->value
= curtreble
* 4370;
287 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
288 struct v4l2_control
*ctrl
)
291 case V4L2_CID_AUDIO_MUTE
:
292 tr_setmute(ctrl
->value
);
294 case V4L2_CID_AUDIO_VOLUME
:
295 tr_setvol(ctrl
->value
);
297 case V4L2_CID_AUDIO_BASS
:
298 tr_setbass(ctrl
->value
);
300 case V4L2_CID_AUDIO_TREBLE
:
301 tr_settreble(ctrl
->value
);
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 const struct file_operations trust_fops
= {
340 .owner
= THIS_MODULE
,
341 .open
= video_exclusive_open
,
342 .release
= video_exclusive_release
,
343 .ioctl
= video_ioctl2
,
345 .compat_ioctl
= v4l_compat_ioctl32
,
350 static const struct v4l2_ioctl_ops trust_ioctl_ops
= {
351 .vidioc_querycap
= vidioc_querycap
,
352 .vidioc_g_tuner
= vidioc_g_tuner
,
353 .vidioc_s_tuner
= vidioc_s_tuner
,
354 .vidioc_g_frequency
= vidioc_g_frequency
,
355 .vidioc_s_frequency
= vidioc_s_frequency
,
356 .vidioc_queryctrl
= vidioc_queryctrl
,
357 .vidioc_g_ctrl
= vidioc_g_ctrl
,
358 .vidioc_s_ctrl
= vidioc_s_ctrl
,
359 .vidioc_g_audio
= vidioc_g_audio
,
360 .vidioc_s_audio
= vidioc_s_audio
,
361 .vidioc_g_input
= vidioc_g_input
,
362 .vidioc_s_input
= vidioc_s_input
,
365 static struct video_device trust_radio
= {
366 .name
= "Trust FM Radio",
368 .ioctl_ops
= &trust_ioctl_ops
,
371 static int __init
trust_init(void)
374 printk(KERN_ERR
"You must set an I/O address with io=0x???\n");
377 if(!request_region(io
, 2, "Trust FM Radio")) {
378 printk(KERN_ERR
"trust: port 0x%x already in use\n", io
);
381 if(video_register_device(&trust_radio
, VFL_TYPE_RADIO
, radio_nr
)==-1)
383 release_region(io
, 2);
387 printk(KERN_INFO
"Trust FM Radio card driver v1.0.\n");
389 write_i2c(2, TDA7318_ADDR
, 0x80); /* speaker att. LF = 0 dB */
390 write_i2c(2, TDA7318_ADDR
, 0xa0); /* speaker att. RF = 0 dB */
391 write_i2c(2, TDA7318_ADDR
, 0xc0); /* speaker att. LR = 0 dB */
392 write_i2c(2, TDA7318_ADDR
, 0xe0); /* speaker att. RR = 0 dB */
393 write_i2c(2, TDA7318_ADDR
, 0x40); /* stereo 1 input, gain = 18.75 dB */
397 tr_settreble(0x8000);
400 /* mute card - prevents noisy bootups */
406 MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
407 MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
408 MODULE_LICENSE("GPL");
410 module_param(io
, int, 0);
411 MODULE_PARM_DESC(io
, "I/O address of the Trust FM Radio card (0x350 or 0x358)");
412 module_param(radio_nr
, int, 0);
414 static void __exit
cleanup_trust_module(void)
416 video_unregister_device(&trust_radio
);
417 release_region(io
, 2);
420 module_init(trust_init
);
421 module_exit(cleanup_trust_module
);