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>
22 #include <linux/videodev2.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
27 MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
28 MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
29 MODULE_LICENSE("GPL");
30 MODULE_VERSION("0.0.3");
32 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
34 #ifndef CONFIG_RADIO_TRUST_PORT
35 #define CONFIG_RADIO_TRUST_PORT -1
38 static int io
= CONFIG_RADIO_TRUST_PORT
;
39 static int radio_nr
= -1;
41 module_param(io
, int, 0);
42 MODULE_PARM_DESC(io
, "I/O address of the Trust FM Radio card (0x350 or 0x358)");
43 module_param(radio_nr
, int, 0);
46 struct v4l2_device v4l2_dev
;
47 struct video_device vdev
;
54 unsigned long curfreq
;
60 static struct trust trust_card
;
63 #define TDA7318_ADDR 0x88
64 #define TSA6060T_ADDR 0xc4
66 #define TR_DELAY do { inb(tr->io); inb(tr->io); inb(tr->io); } while (0)
67 #define TR_SET_SCL outb(tr->ioval |= 2, tr->io)
68 #define TR_CLR_SCL outb(tr->ioval &= 0xfd, tr->io)
69 #define TR_SET_SDA outb(tr->ioval |= 1, tr->io)
70 #define TR_CLR_SDA outb(tr->ioval &= 0xfe, tr->io)
72 static void write_i2c(struct trust
*tr
, int n
, ...)
74 unsigned char val
, mask
;
88 val
= va_arg(args
, unsigned);
89 for(mask
= 0x80; mask
; mask
>>= 1) {
118 static void tr_setvol(struct trust
*tr
, __u16 vol
)
120 mutex_lock(&tr
->lock
);
121 tr
->curvol
= vol
/ 2048;
122 write_i2c(tr
, 2, TDA7318_ADDR
, tr
->curvol
^ 0x1f);
123 mutex_unlock(&tr
->lock
);
126 static int basstreble2chip
[15] = {
127 0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
130 static void tr_setbass(struct trust
*tr
, __u16 bass
)
132 mutex_lock(&tr
->lock
);
133 tr
->curbass
= bass
/ 4370;
134 write_i2c(tr
, 2, TDA7318_ADDR
, 0x60 | basstreble2chip
[tr
->curbass
]);
135 mutex_unlock(&tr
->lock
);
138 static void tr_settreble(struct trust
*tr
, __u16 treble
)
140 mutex_lock(&tr
->lock
);
141 tr
->curtreble
= treble
/ 4370;
142 write_i2c(tr
, 2, TDA7318_ADDR
, 0x70 | basstreble2chip
[tr
->curtreble
]);
143 mutex_unlock(&tr
->lock
);
146 static void tr_setstereo(struct trust
*tr
, int stereo
)
148 mutex_lock(&tr
->lock
);
149 tr
->curstereo
= !!stereo
;
150 tr
->ioval
= (tr
->ioval
& 0xfb) | (!tr
->curstereo
<< 2);
151 outb(tr
->ioval
, tr
->io
);
152 mutex_unlock(&tr
->lock
);
155 static void tr_setmute(struct trust
*tr
, int mute
)
157 mutex_lock(&tr
->lock
);
158 tr
->curmute
= !!mute
;
159 tr
->ioval
= (tr
->ioval
& 0xf7) | (tr
->curmute
<< 3);
160 outb(tr
->ioval
, tr
->io
);
161 mutex_unlock(&tr
->lock
);
164 static int tr_getsigstr(struct trust
*tr
)
168 mutex_lock(&tr
->lock
);
169 for (i
= 0, v
= 0; i
< 100; i
++)
171 mutex_unlock(&tr
->lock
);
172 return (v
& 1) ? 0 : 0xffff;
175 static int tr_getstereo(struct trust
*tr
)
177 /* don't know how to determine it, just return the setting */
178 return tr
->curstereo
;
181 static void tr_setfreq(struct trust
*tr
, unsigned long f
)
183 mutex_lock(&tr
->lock
);
185 f
/= 160; /* Convert to 10 kHz units */
186 f
+= 1070; /* Add 10.7 MHz IF */
187 write_i2c(tr
, 5, TSA6060T_ADDR
, (f
<< 1) | 1, f
>> 7, 0x60 | ((f
>> 15) & 1), 0);
188 mutex_unlock(&tr
->lock
);
191 static int vidioc_querycap(struct file
*file
, void *priv
,
192 struct v4l2_capability
*v
)
194 strlcpy(v
->driver
, "radio-trust", sizeof(v
->driver
));
195 strlcpy(v
->card
, "Trust FM Radio", sizeof(v
->card
));
196 strlcpy(v
->bus_info
, "ISA", sizeof(v
->bus_info
));
197 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
201 static int vidioc_g_tuner(struct file
*file
, void *priv
,
202 struct v4l2_tuner
*v
)
204 struct trust
*tr
= video_drvdata(file
);
209 strlcpy(v
->name
, "FM", sizeof(v
->name
));
210 v
->type
= V4L2_TUNER_RADIO
;
211 v
->rangelow
= 87.5 * 16000;
212 v
->rangehigh
= 108 * 16000;
213 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
214 v
->capability
= V4L2_TUNER_CAP_LOW
;
215 if (tr_getstereo(tr
))
216 v
->audmode
= V4L2_TUNER_MODE_STEREO
;
218 v
->audmode
= V4L2_TUNER_MODE_MONO
;
219 v
->signal
= tr_getsigstr(tr
);
223 static int vidioc_s_tuner(struct file
*file
, void *priv
,
224 struct v4l2_tuner
*v
)
226 struct trust
*tr
= video_drvdata(file
);
230 tr_setstereo(tr
, v
->audmode
== V4L2_TUNER_MODE_STEREO
);
234 static int vidioc_s_frequency(struct file
*file
, void *priv
,
235 struct v4l2_frequency
*f
)
237 struct trust
*tr
= video_drvdata(file
);
239 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
241 tr_setfreq(tr
, f
->frequency
);
245 static int vidioc_g_frequency(struct file
*file
, void *priv
,
246 struct v4l2_frequency
*f
)
248 struct trust
*tr
= video_drvdata(file
);
252 f
->type
= V4L2_TUNER_RADIO
;
253 f
->frequency
= tr
->curfreq
;
257 static int vidioc_queryctrl(struct file
*file
, void *priv
,
258 struct v4l2_queryctrl
*qc
)
261 case V4L2_CID_AUDIO_MUTE
:
262 return v4l2_ctrl_query_fill(qc
, 0, 1, 1, 1);
263 case V4L2_CID_AUDIO_VOLUME
:
264 return v4l2_ctrl_query_fill(qc
, 0, 65535, 2048, 65535);
265 case V4L2_CID_AUDIO_BASS
:
266 case V4L2_CID_AUDIO_TREBLE
:
267 return v4l2_ctrl_query_fill(qc
, 0, 65535, 4370, 32768);
272 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
273 struct v4l2_control
*ctrl
)
275 struct trust
*tr
= video_drvdata(file
);
278 case V4L2_CID_AUDIO_MUTE
:
279 ctrl
->value
= tr
->curmute
;
281 case V4L2_CID_AUDIO_VOLUME
:
282 ctrl
->value
= tr
->curvol
* 2048;
284 case V4L2_CID_AUDIO_BASS
:
285 ctrl
->value
= tr
->curbass
* 4370;
287 case V4L2_CID_AUDIO_TREBLE
:
288 ctrl
->value
= tr
->curtreble
* 4370;
294 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
295 struct v4l2_control
*ctrl
)
297 struct trust
*tr
= video_drvdata(file
);
300 case V4L2_CID_AUDIO_MUTE
:
301 tr_setmute(tr
, ctrl
->value
);
303 case V4L2_CID_AUDIO_VOLUME
:
304 tr_setvol(tr
, ctrl
->value
);
306 case V4L2_CID_AUDIO_BASS
:
307 tr_setbass(tr
, ctrl
->value
);
309 case V4L2_CID_AUDIO_TREBLE
:
310 tr_settreble(tr
, ctrl
->value
);
316 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
322 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
324 return i
? -EINVAL
: 0;
327 static int vidioc_g_audio(struct file
*file
, void *priv
,
328 struct v4l2_audio
*a
)
331 strlcpy(a
->name
, "Radio", sizeof(a
->name
));
332 a
->capability
= V4L2_AUDCAP_STEREO
;
336 static int vidioc_s_audio(struct file
*file
, void *priv
,
337 struct v4l2_audio
*a
)
339 return a
->index
? -EINVAL
: 0;
342 static const struct v4l2_file_operations trust_fops
= {
343 .owner
= THIS_MODULE
,
344 .unlocked_ioctl
= video_ioctl2
,
347 static const struct v4l2_ioctl_ops trust_ioctl_ops
= {
348 .vidioc_querycap
= vidioc_querycap
,
349 .vidioc_g_tuner
= vidioc_g_tuner
,
350 .vidioc_s_tuner
= vidioc_s_tuner
,
351 .vidioc_g_frequency
= vidioc_g_frequency
,
352 .vidioc_s_frequency
= vidioc_s_frequency
,
353 .vidioc_queryctrl
= vidioc_queryctrl
,
354 .vidioc_g_ctrl
= vidioc_g_ctrl
,
355 .vidioc_s_ctrl
= vidioc_s_ctrl
,
356 .vidioc_g_audio
= vidioc_g_audio
,
357 .vidioc_s_audio
= vidioc_s_audio
,
358 .vidioc_g_input
= vidioc_g_input
,
359 .vidioc_s_input
= vidioc_s_input
,
362 static int __init
trust_init(void)
364 struct trust
*tr
= &trust_card
;
365 struct v4l2_device
*v4l2_dev
= &tr
->v4l2_dev
;
368 strlcpy(v4l2_dev
->name
, "trust", sizeof(v4l2_dev
->name
));
371 mutex_init(&tr
->lock
);
374 v4l2_err(v4l2_dev
, "You must set an I/O address with io=0x0x350 or 0x358\n");
377 if (!request_region(tr
->io
, 2, "Trust FM Radio")) {
378 v4l2_err(v4l2_dev
, "port 0x%x already in use\n", tr
->io
);
382 res
= v4l2_device_register(NULL
, v4l2_dev
);
384 release_region(tr
->io
, 2);
385 v4l2_err(v4l2_dev
, "Could not register v4l2_device\n");
389 strlcpy(tr
->vdev
.name
, v4l2_dev
->name
, sizeof(tr
->vdev
.name
));
390 tr
->vdev
.v4l2_dev
= v4l2_dev
;
391 tr
->vdev
.fops
= &trust_fops
;
392 tr
->vdev
.ioctl_ops
= &trust_ioctl_ops
;
393 tr
->vdev
.release
= video_device_release_empty
;
394 video_set_drvdata(&tr
->vdev
, tr
);
396 write_i2c(tr
, 2, TDA7318_ADDR
, 0x80); /* speaker att. LF = 0 dB */
397 write_i2c(tr
, 2, TDA7318_ADDR
, 0xa0); /* speaker att. RF = 0 dB */
398 write_i2c(tr
, 2, TDA7318_ADDR
, 0xc0); /* speaker att. LR = 0 dB */
399 write_i2c(tr
, 2, TDA7318_ADDR
, 0xe0); /* speaker att. RR = 0 dB */
400 write_i2c(tr
, 2, TDA7318_ADDR
, 0x40); /* stereo 1 input, gain = 18.75 dB */
402 tr_setvol(tr
, 0xffff);
403 tr_setbass(tr
, 0x8000);
404 tr_settreble(tr
, 0x8000);
407 /* mute card - prevents noisy bootups */
410 if (video_register_device(&tr
->vdev
, VFL_TYPE_RADIO
, radio_nr
) < 0) {
411 v4l2_device_unregister(v4l2_dev
);
412 release_region(tr
->io
, 2);
416 v4l2_info(v4l2_dev
, "Trust FM Radio card driver v1.0.\n");
421 static void __exit
cleanup_trust_module(void)
423 struct trust
*tr
= &trust_card
;
425 video_unregister_device(&tr
->vdev
);
426 v4l2_device_unregister(&tr
->v4l2_dev
);
427 release_region(tr
->io
, 2);
430 module_init(trust_init
);
431 module_exit(cleanup_trust_module
);