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>
27 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
28 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
30 static struct v4l2_queryctrl radio_qctrl
[] = {
32 .id
= V4L2_CID_AUDIO_MUTE
,
37 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
39 .id
= V4L2_CID_AUDIO_VOLUME
,
44 .default_value
= 65535,
45 .type
= V4L2_CTRL_TYPE_INTEGER
,
47 .id
= V4L2_CID_AUDIO_BASS
,
52 .default_value
= 32768,
53 .type
= V4L2_CTRL_TYPE_INTEGER
,
55 .id
= V4L2_CID_AUDIO_TREBLE
,
60 .default_value
= 32768,
61 .type
= V4L2_CTRL_TYPE_INTEGER
,
65 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
67 #ifndef CONFIG_RADIO_TRUST_PORT
68 #define CONFIG_RADIO_TRUST_PORT -1
71 static int io
= CONFIG_RADIO_TRUST_PORT
;
72 static int radio_nr
= -1;
73 static int ioval
= 0xf;
76 static __u16 curtreble
;
77 static unsigned long curfreq
;
82 #define TDA7318_ADDR 0x88
83 #define TSA6060T_ADDR 0xc4
85 #define TR_DELAY do { inb(io); inb(io); inb(io); } while(0)
86 #define TR_SET_SCL outb(ioval |= 2, io)
87 #define TR_CLR_SCL outb(ioval &= 0xfd, io)
88 #define TR_SET_SDA outb(ioval |= 1, io)
89 #define TR_CLR_SDA outb(ioval &= 0xfe, io)
91 static void write_i2c(int n
, ...)
93 unsigned char val
, mask
;
107 val
= va_arg(args
, unsigned);
108 for(mask
= 0x80; mask
; mask
>>= 1) {
118 /* acknowledge bit */
137 static void tr_setvol(__u16 vol
)
140 write_i2c(2, TDA7318_ADDR
, curvol
^ 0x1f);
143 static int basstreble2chip
[15] = {
144 0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
147 static void tr_setbass(__u16 bass
)
149 curbass
= bass
/ 4370;
150 write_i2c(2, TDA7318_ADDR
, 0x60 | basstreble2chip
[curbass
]);
153 static void tr_settreble(__u16 treble
)
155 curtreble
= treble
/ 4370;
156 write_i2c(2, TDA7318_ADDR
, 0x70 | basstreble2chip
[curtreble
]);
159 static void tr_setstereo(int stereo
)
161 curstereo
= !!stereo
;
162 ioval
= (ioval
& 0xfb) | (!curstereo
<< 2);
166 static void tr_setmute(int mute
)
169 ioval
= (ioval
& 0xf7) | (curmute
<< 3);
173 static int tr_getsigstr(void)
177 for(i
= 0, v
= 0; i
< 100; i
++) v
|= inb(io
);
178 return (v
& 1)? 0 : 0xffff;
181 static int tr_getstereo(void)
183 /* don't know how to determine it, just return the setting */
187 static void tr_setfreq(unsigned long f
)
189 f
/= 160; /* Convert to 10 kHz units */
190 f
+= 1070; /* Add 10.7 MHz IF */
192 write_i2c(5, TSA6060T_ADDR
, (f
<< 1) | 1, f
>> 7, 0x60 | ((f
>> 15) & 1), 0);
195 static int vidioc_querycap(struct file
*file
, void *priv
,
196 struct v4l2_capability
*v
)
198 strlcpy(v
->driver
, "radio-trust", sizeof(v
->driver
));
199 strlcpy(v
->card
, "Trust FM Radio", sizeof(v
->card
));
200 sprintf(v
->bus_info
, "ISA");
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
)
212 strcpy(v
->name
, "FM");
213 v
->type
= V4L2_TUNER_RADIO
;
214 v
->rangelow
= (87.5*16000);
215 v
->rangehigh
= (108*16000);
216 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
|V4L2_TUNER_SUB_STEREO
;
217 v
->capability
= V4L2_TUNER_CAP_LOW
;
219 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
221 v
->audmode
= V4L2_TUNER_MODE_MONO
;
222 v
->signal
= tr_getsigstr();
226 static int vidioc_s_tuner(struct file
*file
, void *priv
,
227 struct v4l2_tuner
*v
)
235 static int vidioc_s_frequency(struct file
*file
, void *priv
,
236 struct v4l2_frequency
*f
)
238 curfreq
= f
->frequency
;
243 static int vidioc_g_frequency(struct file
*file
, void *priv
,
244 struct v4l2_frequency
*f
)
246 f
->type
= V4L2_TUNER_RADIO
;
247 f
->frequency
= curfreq
;
251 static int vidioc_queryctrl(struct file
*file
, void *priv
,
252 struct v4l2_queryctrl
*qc
)
256 for (i
= 0; i
< ARRAY_SIZE(radio_qctrl
); i
++) {
257 if (qc
->id
&& qc
->id
== radio_qctrl
[i
].id
) {
258 memcpy(qc
, &(radio_qctrl
[i
]),
266 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
267 struct v4l2_control
*ctrl
)
270 case V4L2_CID_AUDIO_MUTE
:
271 ctrl
->value
= curmute
;
273 case V4L2_CID_AUDIO_VOLUME
:
274 ctrl
->value
= curvol
* 2048;
276 case V4L2_CID_AUDIO_BASS
:
277 ctrl
->value
= curbass
* 4370;
279 case V4L2_CID_AUDIO_TREBLE
:
280 ctrl
->value
= curtreble
* 4370;
286 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
287 struct v4l2_control
*ctrl
)
290 case V4L2_CID_AUDIO_MUTE
:
291 tr_setmute(ctrl
->value
);
293 case V4L2_CID_AUDIO_VOLUME
:
294 tr_setvol(ctrl
->value
);
296 case V4L2_CID_AUDIO_BASS
:
297 tr_setbass(ctrl
->value
);
299 case V4L2_CID_AUDIO_TREBLE
:
300 tr_settreble(ctrl
->value
);
306 static int vidioc_g_audio(struct file
*file
, void *priv
,
307 struct v4l2_audio
*a
)
312 strcpy(a
->name
, "Radio");
313 a
->capability
= V4L2_AUDCAP_STEREO
;
317 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
323 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
330 static int vidioc_s_audio(struct file
*file
, void *priv
,
331 struct v4l2_audio
*a
)
338 static const struct file_operations trust_fops
= {
339 .owner
= THIS_MODULE
,
340 .open
= video_exclusive_open
,
341 .release
= video_exclusive_release
,
342 .ioctl
= video_ioctl2
,
343 .compat_ioctl
= v4l_compat_ioctl32
,
347 static struct video_device trust_radio
=
349 .owner
= THIS_MODULE
,
350 .name
= "Trust FM Radio",
351 .type
= VID_TYPE_TUNER
,
354 .vidioc_querycap
= vidioc_querycap
,
355 .vidioc_g_tuner
= vidioc_g_tuner
,
356 .vidioc_s_tuner
= vidioc_s_tuner
,
357 .vidioc_g_frequency
= vidioc_g_frequency
,
358 .vidioc_s_frequency
= vidioc_s_frequency
,
359 .vidioc_queryctrl
= vidioc_queryctrl
,
360 .vidioc_g_ctrl
= vidioc_g_ctrl
,
361 .vidioc_s_ctrl
= vidioc_s_ctrl
,
362 .vidioc_g_audio
= vidioc_g_audio
,
363 .vidioc_s_audio
= vidioc_s_audio
,
364 .vidioc_g_input
= vidioc_g_input
,
365 .vidioc_s_input
= vidioc_s_input
,
368 static int __init
trust_init(void)
371 printk(KERN_ERR
"You must set an I/O address with io=0x???\n");
374 if(!request_region(io
, 2, "Trust FM Radio")) {
375 printk(KERN_ERR
"trust: port 0x%x already in use\n", io
);
378 if(video_register_device(&trust_radio
, VFL_TYPE_RADIO
, radio_nr
)==-1)
380 release_region(io
, 2);
384 printk(KERN_INFO
"Trust FM Radio card driver v1.0.\n");
386 write_i2c(2, TDA7318_ADDR
, 0x80); /* speaker att. LF = 0 dB */
387 write_i2c(2, TDA7318_ADDR
, 0xa0); /* speaker att. RF = 0 dB */
388 write_i2c(2, TDA7318_ADDR
, 0xc0); /* speaker att. LR = 0 dB */
389 write_i2c(2, TDA7318_ADDR
, 0xe0); /* speaker att. RR = 0 dB */
390 write_i2c(2, TDA7318_ADDR
, 0x40); /* stereo 1 input, gain = 18.75 dB */
394 tr_settreble(0x8000);
397 /* mute card - prevents noisy bootups */
403 MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
404 MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
405 MODULE_LICENSE("GPL");
407 module_param(io
, int, 0);
408 MODULE_PARM_DESC(io
, "I/O address of the Trust FM Radio card (0x350 or 0x358)");
409 module_param(radio_nr
, int, 0);
411 static void __exit
cleanup_trust_module(void)
413 video_unregister_device(&trust_radio
);
414 release_region(io
, 2);
417 module_init(trust_init
);
418 module_exit(cleanup_trust_module
);