blob: 8da4badc22b47f399ae8c3ce3f9e80e60a3f127c [file] [log] [blame]
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03001/* radio-trust.c - Trust FM Radio card driver for Linux 2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * by Eric Lammerts <eric@scintilla.utwente.nl>
3 *
4 * Based on radio-aztech.c. Original notes:
5 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03006 * Adapted to support the Video for Linux API by
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
8 *
9 * Quay Ly
10 * Donald Song
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030011 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
13 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
14 *
15 * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
16 */
17
18#include <stdarg.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/ioport.h>
22#include <asm/io.h>
23#include <asm/uaccess.h>
24#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030025#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/config.h> /* CONFIG_RADIO_TRUST_PORT */
27
28/* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
29
30#ifndef CONFIG_RADIO_TRUST_PORT
31#define CONFIG_RADIO_TRUST_PORT -1
32#endif
33
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030034static int io = CONFIG_RADIO_TRUST_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int radio_nr = -1;
36static int ioval = 0xf;
37static __u16 curvol;
38static __u16 curbass;
39static __u16 curtreble;
40static unsigned long curfreq;
41static int curstereo;
42static int curmute;
43
44/* i2c addresses */
45#define TDA7318_ADDR 0x88
46#define TSA6060T_ADDR 0xc4
47
48#define TR_DELAY do { inb(io); inb(io); inb(io); } while(0)
49#define TR_SET_SCL outb(ioval |= 2, io)
50#define TR_CLR_SCL outb(ioval &= 0xfd, io)
51#define TR_SET_SDA outb(ioval |= 1, io)
52#define TR_CLR_SDA outb(ioval &= 0xfe, io)
53
54static void write_i2c(int n, ...)
55{
56 unsigned char val, mask;
57 va_list args;
58
59 va_start(args, n);
60
61 /* start condition */
62 TR_SET_SDA;
63 TR_SET_SCL;
64 TR_DELAY;
65 TR_CLR_SDA;
66 TR_CLR_SCL;
67 TR_DELAY;
68
69 for(; n; n--) {
70 val = va_arg(args, unsigned);
71 for(mask = 0x80; mask; mask >>= 1) {
72 if(val & mask)
73 TR_SET_SDA;
74 else
75 TR_CLR_SDA;
76 TR_SET_SCL;
77 TR_DELAY;
78 TR_CLR_SCL;
79 TR_DELAY;
80 }
81 /* acknowledge bit */
82 TR_SET_SDA;
83 TR_SET_SCL;
84 TR_DELAY;
85 TR_CLR_SCL;
86 TR_DELAY;
87 }
88
89 /* stop condition */
90 TR_CLR_SDA;
91 TR_DELAY;
92 TR_SET_SCL;
93 TR_DELAY;
94 TR_SET_SDA;
95 TR_DELAY;
96
97 va_end(args);
98}
99
100static void tr_setvol(__u16 vol)
101{
102 curvol = vol / 2048;
103 write_i2c(2, TDA7318_ADDR, curvol ^ 0x1f);
104}
105
106static int basstreble2chip[15] = {
107 0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
108};
109
110static void tr_setbass(__u16 bass)
111{
112 curbass = bass / 4370;
113 write_i2c(2, TDA7318_ADDR, 0x60 | basstreble2chip[curbass]);
114}
115
116static void tr_settreble(__u16 treble)
117{
118 curtreble = treble / 4370;
119 write_i2c(2, TDA7318_ADDR, 0x70 | basstreble2chip[curtreble]);
120}
121
122static void tr_setstereo(int stereo)
123{
124 curstereo = !!stereo;
125 ioval = (ioval & 0xfb) | (!curstereo << 2);
126 outb(ioval, io);
127}
128
129static void tr_setmute(int mute)
130{
131 curmute = !!mute;
132 ioval = (ioval & 0xf7) | (curmute << 3);
133 outb(ioval, io);
134}
135
136static int tr_getsigstr(void)
137{
138 int i, v;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 for(i = 0, v = 0; i < 100; i++) v |= inb(io);
141 return (v & 1)? 0 : 0xffff;
142}
143
144static int tr_getstereo(void)
145{
146 /* don't know how to determine it, just return the setting */
147 return curstereo;
148}
149
150static void tr_setfreq(unsigned long f)
151{
152 f /= 160; /* Convert to 10 kHz units */
153 f += 1070; /* Add 10.7 MHz IF */
154
155 write_i2c(5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0);
156}
157
158static int tr_do_ioctl(struct inode *inode, struct file *file,
159 unsigned int cmd, void *arg)
160{
161 switch(cmd)
162 {
163 case VIDIOCGCAP:
164 {
165 struct video_capability *v = arg;
166
167 memset(v,0,sizeof(*v));
168 v->type=VID_TYPE_TUNER;
169 v->channels=1;
170 v->audios=1;
171 strcpy(v->name, "Trust FM Radio");
172
173 return 0;
174 }
175 case VIDIOCGTUNER:
176 {
177 struct video_tuner *v = arg;
178
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300179 if(v->tuner) /* Only 1 tuner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -EINVAL;
181
182 v->rangelow = 87500 * 16;
183 v->rangehigh = 108000 * 16;
184 v->flags = VIDEO_TUNER_LOW;
185 v->mode = VIDEO_MODE_AUTO;
186
187 v->signal = tr_getsigstr();
188 if(tr_getstereo())
189 v->flags |= VIDEO_TUNER_STEREO_ON;
190
191 strcpy(v->name, "FM");
192
193 return 0;
194 }
195 case VIDIOCSTUNER:
196 {
197 struct video_tuner *v = arg;
198 if(v->tuner != 0)
199 return -EINVAL;
200 return 0;
201 }
202 case VIDIOCGFREQ:
203 {
204 unsigned long *freq = arg;
205 *freq = curfreq;
206 return 0;
207 }
208 case VIDIOCSFREQ:
209 {
210 unsigned long *freq = arg;
211 tr_setfreq(*freq);
212 return 0;
213 }
214 case VIDIOCGAUDIO:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300215 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct video_audio *v = arg;
217
218 memset(v,0, sizeof(*v));
219 v->flags = VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME |
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300220 VIDEO_AUDIO_BASS | VIDEO_AUDIO_TREBLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 v->mode = curstereo? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
222 v->volume = curvol * 2048;
223 v->step = 2048;
224 v->bass = curbass * 4370;
225 v->treble = curtreble * 4370;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 strcpy(v->name, "Trust FM Radio");
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 case VIDIOCSAUDIO:
231 {
232 struct video_audio *v = arg;
233
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300234 if(v->audio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EINVAL;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300236 tr_setvol(v->volume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 tr_setbass(v->bass);
238 tr_settreble(v->treble);
239 tr_setstereo(v->mode & VIDEO_SOUND_STEREO);
240 tr_setmute(v->flags & VIDEO_AUDIO_MUTE);
241 return 0;
242 }
243 default:
244 return -ENOIOCTLCMD;
245 }
246}
247
248static int tr_ioctl(struct inode *inode, struct file *file,
249 unsigned int cmd, unsigned long arg)
250{
251 return video_usercopy(inode, file, cmd, arg, tr_do_ioctl);
252}
253
254static struct file_operations trust_fops = {
255 .owner = THIS_MODULE,
256 .open = video_exclusive_open,
257 .release = video_exclusive_release,
258 .ioctl = tr_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200259 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .llseek = no_llseek,
261};
262
263static struct video_device trust_radio=
264{
265 .owner = THIS_MODULE,
266 .name = "Trust FM Radio",
267 .type = VID_TYPE_TUNER,
268 .hardware = VID_HARDWARE_TRUST,
269 .fops = &trust_fops,
270};
271
272static int __init trust_init(void)
273{
274 if(io == -1) {
275 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
276 return -EINVAL;
277 }
278 if(!request_region(io, 2, "Trust FM Radio")) {
279 printk(KERN_ERR "trust: port 0x%x already in use\n", io);
280 return -EBUSY;
281 }
282 if(video_register_device(&trust_radio, VFL_TYPE_RADIO, radio_nr)==-1)
283 {
284 release_region(io, 2);
285 return -EINVAL;
286 }
287
288 printk(KERN_INFO "Trust FM Radio card driver v1.0.\n");
289
290 write_i2c(2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */
291 write_i2c(2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */
292 write_i2c(2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */
293 write_i2c(2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */
294 write_i2c(2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */
295
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300296 tr_setvol(0x8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 tr_setbass(0x8000);
298 tr_settreble(0x8000);
299 tr_setstereo(1);
300
301 /* mute card - prevents noisy bootups */
302 tr_setmute(1);
303
304 return 0;
305}
306
307MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
308MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
309MODULE_LICENSE("GPL");
310
311module_param(io, int, 0);
312MODULE_PARM_DESC(io, "I/O address of the Trust FM Radio card (0x350 or 0x358)");
313module_param(radio_nr, int, 0);
314
315static void __exit cleanup_trust_module(void)
316{
317 video_unregister_device(&trust_radio);
318 release_region(io, 2);
319}
320
321module_init(trust_init);
322module_exit(cleanup_trust_module);