Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6] / drivers / media / radio / radio-trust.c
1 /* radio-trust.c - Trust FM Radio card driver for Linux 2.2
2  * by Eric Lammerts <eric@scintilla.utwente.nl>
3  *
4  * Based on radio-aztech.c. Original notes:
5  *
6  * Adapted to support the Video for Linux API by
7  * Russell Kroll <rkroll@exploits.org>.  Based on original tuner code by:
8  *
9  * Quay Ly
10  * Donald Song
11  * Jason Lewis      (jlewis@twilight.vtc.vsc.edu)
12  * Scott McGrath    (smcgrath@twilight.vtc.vsc.edu)
13  * William McGrath  (wmcgrath@twilight.vtc.vsc.edu)
14  *
15  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
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/videodev2.h>
25 #include <media/v4l2-common.h>
26
27 #include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
28 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
29
30 static struct v4l2_queryctrl radio_qctrl[] = {
31         {
32                 .id            = V4L2_CID_AUDIO_MUTE,
33                 .name          = "Mute",
34                 .minimum       = 0,
35                 .maximum       = 1,
36                 .default_value = 1,
37                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
38         },{
39                 .id            = V4L2_CID_AUDIO_VOLUME,
40                 .name          = "Volume",
41                 .minimum       = 0,
42                 .maximum       = 65535,
43                 .step          = 2048,
44                 .default_value = 65535,
45                 .type          = V4L2_CTRL_TYPE_INTEGER,
46         },{
47                 .id            = V4L2_CID_AUDIO_BASS,
48                 .name          = "Bass",
49                 .minimum       = 0,
50                 .maximum       = 65535,
51                 .step          = 4370,
52                 .default_value = 32768,
53                 .type          = V4L2_CTRL_TYPE_INTEGER,
54         },{
55                 .id            = V4L2_CID_AUDIO_TREBLE,
56                 .name          = "Treble",
57                 .minimum       = 0,
58                 .maximum       = 65535,
59                 .step          = 4370,
60                 .default_value = 32768,
61                 .type          = V4L2_CTRL_TYPE_INTEGER,
62         },
63 };
64
65 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
66
67 #ifndef CONFIG_RADIO_TRUST_PORT
68 #define CONFIG_RADIO_TRUST_PORT -1
69 #endif
70
71 static int io = CONFIG_RADIO_TRUST_PORT;
72 static int radio_nr = -1;
73 static int ioval = 0xf;
74 static __u16 curvol;
75 static __u16 curbass;
76 static __u16 curtreble;
77 static unsigned long curfreq;
78 static int curstereo;
79 static int curmute;
80
81 /* i2c addresses */
82 #define TDA7318_ADDR 0x88
83 #define TSA6060T_ADDR 0xc4
84
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)
90
91 static void write_i2c(int n, ...)
92 {
93         unsigned char val, mask;
94         va_list args;
95
96         va_start(args, n);
97
98         /* start condition */
99         TR_SET_SDA;
100         TR_SET_SCL;
101         TR_DELAY;
102         TR_CLR_SDA;
103         TR_CLR_SCL;
104         TR_DELAY;
105
106         for(; n; n--) {
107                 val = va_arg(args, unsigned);
108                 for(mask = 0x80; mask; mask >>= 1) {
109                         if(val & mask)
110                                 TR_SET_SDA;
111                         else
112                                 TR_CLR_SDA;
113                         TR_SET_SCL;
114                         TR_DELAY;
115                         TR_CLR_SCL;
116                         TR_DELAY;
117                 }
118                 /* acknowledge bit */
119                 TR_SET_SDA;
120                 TR_SET_SCL;
121                 TR_DELAY;
122                 TR_CLR_SCL;
123                 TR_DELAY;
124         }
125
126         /* stop condition */
127         TR_CLR_SDA;
128         TR_DELAY;
129         TR_SET_SCL;
130         TR_DELAY;
131         TR_SET_SDA;
132         TR_DELAY;
133
134         va_end(args);
135 }
136
137 static void tr_setvol(__u16 vol)
138 {
139         curvol = vol / 2048;
140         write_i2c(2, TDA7318_ADDR, curvol ^ 0x1f);
141 }
142
143 static int basstreble2chip[15] = {
144         0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
145 };
146
147 static void tr_setbass(__u16 bass)
148 {
149         curbass = bass / 4370;
150         write_i2c(2, TDA7318_ADDR, 0x60 | basstreble2chip[curbass]);
151 }
152
153 static void tr_settreble(__u16 treble)
154 {
155         curtreble = treble / 4370;
156         write_i2c(2, TDA7318_ADDR, 0x70 | basstreble2chip[curtreble]);
157 }
158
159 static void tr_setstereo(int stereo)
160 {
161         curstereo = !!stereo;
162         ioval = (ioval & 0xfb) | (!curstereo << 2);
163         outb(ioval, io);
164 }
165
166 static void tr_setmute(int mute)
167 {
168         curmute = !!mute;
169         ioval = (ioval & 0xf7) | (curmute << 3);
170         outb(ioval, io);
171 }
172
173 static int tr_getsigstr(void)
174 {
175         int i, v;
176
177         for(i = 0, v = 0; i < 100; i++) v |= inb(io);
178         return (v & 1)? 0 : 0xffff;
179 }
180
181 static int tr_getstereo(void)
182 {
183         /* don't know how to determine it, just return the setting */
184         return curstereo;
185 }
186
187 static void tr_setfreq(unsigned long f)
188 {
189         f /= 160;       /* Convert to 10 kHz units      */
190         f += 1070;      /* Add 10.7 MHz IF                      */
191
192         write_i2c(5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0);
193 }
194
195 static int tr_do_ioctl(struct inode *inode, struct file *file,
196                        unsigned int cmd, void *arg)
197 {
198         switch(cmd)
199         {
200                 case VIDIOC_QUERYCAP:
201                 {
202                         struct v4l2_capability *v = arg;
203                         memset(v,0,sizeof(*v));
204                         strlcpy(v->driver, "radio-trust", sizeof (v->driver));
205                         strlcpy(v->card, "Trust FM Radio", sizeof (v->card));
206                         sprintf(v->bus_info,"ISA");
207                         v->version = RADIO_VERSION;
208                         v->capabilities = V4L2_CAP_TUNER;
209
210                         return 0;
211                 }
212                 case VIDIOC_G_TUNER:
213                 {
214                         struct v4l2_tuner *v = arg;
215
216                         if (v->index > 0)
217                                 return -EINVAL;
218
219                         memset(v,0,sizeof(*v));
220                         strcpy(v->name, "FM");
221                         v->type = V4L2_TUNER_RADIO;
222
223                         v->rangelow=(87.5*16000);
224                         v->rangehigh=(108*16000);
225                         v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
226                         v->capability=V4L2_TUNER_CAP_LOW;
227                         if(tr_getstereo())
228                                 v->audmode = V4L2_TUNER_MODE_STEREO;
229                         else
230                                 v->audmode = V4L2_TUNER_MODE_MONO;
231                         v->signal=tr_getsigstr();
232
233                         return 0;
234                 }
235                 case VIDIOC_S_TUNER:
236                 {
237                         struct v4l2_tuner *v = arg;
238
239                         if (v->index > 0)
240                                 return -EINVAL;
241
242                         return 0;
243                 }
244                 case VIDIOC_S_FREQUENCY:
245                 {
246                         struct v4l2_frequency *f = arg;
247
248                         curfreq = f->frequency;
249                         tr_setfreq(curfreq);
250                         return 0;
251                 }
252                 case VIDIOC_G_FREQUENCY:
253                 {
254                         struct v4l2_frequency *f = arg;
255
256                         f->type = V4L2_TUNER_RADIO;
257                         f->frequency = curfreq;
258
259                         return 0;
260                 }
261                 case VIDIOC_QUERYCTRL:
262                 {
263                         struct v4l2_queryctrl *qc = arg;
264                         int i;
265
266                         for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
267                                 if (qc->id && qc->id == radio_qctrl[i].id) {
268                                         memcpy(qc, &(radio_qctrl[i]),
269                                                                 sizeof(*qc));
270                                         return (0);
271                                 }
272                         }
273                         return -EINVAL;
274                 }
275                 case VIDIOC_G_CTRL:
276                 {
277                         struct v4l2_control *ctrl= arg;
278
279                         switch (ctrl->id) {
280                                 case V4L2_CID_AUDIO_MUTE:
281                                         ctrl->value=curmute;
282                                         return (0);
283                                 case V4L2_CID_AUDIO_VOLUME:
284                                         ctrl->value= curvol * 2048;
285                                         return (0);
286                                 case V4L2_CID_AUDIO_BASS:
287                                         ctrl->value= curbass * 4370;
288                                         return (0);
289                                 case V4L2_CID_AUDIO_TREBLE:
290                                         ctrl->value= curtreble * 4370;
291                                         return (0);
292                         }
293                         return -EINVAL;
294                 }
295                 case VIDIOC_S_CTRL:
296                 {
297                         struct v4l2_control *ctrl= arg;
298
299                         switch (ctrl->id) {
300                                 case V4L2_CID_AUDIO_MUTE:
301                                         tr_setmute(ctrl->value);
302                                         return 0;
303                                 case V4L2_CID_AUDIO_VOLUME:
304                                         tr_setvol(ctrl->value);
305                                         return 0;
306                                 case V4L2_CID_AUDIO_BASS:
307                                         tr_setbass(ctrl->value);
308                                         return 0;
309                                 case V4L2_CID_AUDIO_TREBLE:
310                                         tr_settreble(ctrl->value);
311                                         return (0);
312                         }
313                         return -EINVAL;
314                 }
315
316                 default:
317                         return v4l_compat_translate_ioctl(inode,file,cmd,arg,
318                                                           tr_do_ioctl);
319         }
320 }
321
322 static int tr_ioctl(struct inode *inode, struct file *file,
323                     unsigned int cmd, unsigned long arg)
324 {
325         return video_usercopy(inode, file, cmd, arg, tr_do_ioctl);
326 }
327
328 static struct file_operations trust_fops = {
329         .owner          = THIS_MODULE,
330         .open           = video_exclusive_open,
331         .release        = video_exclusive_release,
332         .ioctl          = tr_ioctl,
333         .compat_ioctl   = v4l_compat_ioctl32,
334         .llseek         = no_llseek,
335 };
336
337 static struct video_device trust_radio=
338 {
339         .owner          = THIS_MODULE,
340         .name           = "Trust FM Radio",
341         .type           = VID_TYPE_TUNER,
342         .hardware       = 0,
343         .fops           = &trust_fops,
344 };
345
346 static int __init trust_init(void)
347 {
348         if(io == -1) {
349                 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
350                 return -EINVAL;
351         }
352         if(!request_region(io, 2, "Trust FM Radio")) {
353                 printk(KERN_ERR "trust: port 0x%x already in use\n", io);
354                 return -EBUSY;
355         }
356         if(video_register_device(&trust_radio, VFL_TYPE_RADIO, radio_nr)==-1)
357         {
358                 release_region(io, 2);
359                 return -EINVAL;
360         }
361
362         printk(KERN_INFO "Trust FM Radio card driver v1.0.\n");
363
364         write_i2c(2, TDA7318_ADDR, 0x80);       /* speaker att. LF = 0 dB */
365         write_i2c(2, TDA7318_ADDR, 0xa0);       /* speaker att. RF = 0 dB */
366         write_i2c(2, TDA7318_ADDR, 0xc0);       /* speaker att. LR = 0 dB */
367         write_i2c(2, TDA7318_ADDR, 0xe0);       /* speaker att. RR = 0 dB */
368         write_i2c(2, TDA7318_ADDR, 0x40);       /* stereo 1 input, gain = 18.75 dB */
369
370         tr_setvol(0x8000);
371         tr_setbass(0x8000);
372         tr_settreble(0x8000);
373         tr_setstereo(1);
374
375         /* mute card - prevents noisy bootups */
376         tr_setmute(1);
377
378         return 0;
379 }
380
381 MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
382 MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
383 MODULE_LICENSE("GPL");
384
385 module_param(io, int, 0);
386 MODULE_PARM_DESC(io, "I/O address of the Trust FM Radio card (0x350 or 0x358)");
387 module_param(radio_nr, int, 0);
388
389 static void __exit cleanup_trust_module(void)
390 {
391         video_unregister_device(&trust_radio);
392         release_region(io, 2);
393 }
394
395 module_init(trust_init);
396 module_exit(cleanup_trust_module);