Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[sfrench/cifs-2.6.git] / 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 <linux/version.h>      /* for KERNEL_VERSION MACRO     */
23 #include <linux/videodev2.h>
24 #include <linux/io.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ioctl.h>
27
28 MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
29 MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
30 MODULE_LICENSE("GPL");
31
32 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
33
34 #ifndef CONFIG_RADIO_TRUST_PORT
35 #define CONFIG_RADIO_TRUST_PORT -1
36 #endif
37
38 static int io = CONFIG_RADIO_TRUST_PORT;
39 static int radio_nr = -1;
40
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);
44
45 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
46
47 struct trust {
48         struct v4l2_device v4l2_dev;
49         struct video_device vdev;
50         int io;
51         int ioval;
52         __u16 curvol;
53         __u16 curbass;
54         __u16 curtreble;
55         int muted;
56         unsigned long curfreq;
57         int curstereo;
58         int curmute;
59         struct mutex lock;
60 };
61
62 static struct trust trust_card;
63
64 /* i2c addresses */
65 #define TDA7318_ADDR 0x88
66 #define TSA6060T_ADDR 0xc4
67
68 #define TR_DELAY do { inb(tr->io); inb(tr->io); inb(tr->io); } while (0)
69 #define TR_SET_SCL outb(tr->ioval |= 2, tr->io)
70 #define TR_CLR_SCL outb(tr->ioval &= 0xfd, tr->io)
71 #define TR_SET_SDA outb(tr->ioval |= 1, tr->io)
72 #define TR_CLR_SDA outb(tr->ioval &= 0xfe, tr->io)
73
74 static void write_i2c(struct trust *tr, int n, ...)
75 {
76         unsigned char val, mask;
77         va_list args;
78
79         va_start(args, n);
80
81         /* start condition */
82         TR_SET_SDA;
83         TR_SET_SCL;
84         TR_DELAY;
85         TR_CLR_SDA;
86         TR_CLR_SCL;
87         TR_DELAY;
88
89         for(; n; n--) {
90                 val = va_arg(args, unsigned);
91                 for(mask = 0x80; mask; mask >>= 1) {
92                         if(val & mask)
93                                 TR_SET_SDA;
94                         else
95                                 TR_CLR_SDA;
96                         TR_SET_SCL;
97                         TR_DELAY;
98                         TR_CLR_SCL;
99                         TR_DELAY;
100                 }
101                 /* acknowledge bit */
102                 TR_SET_SDA;
103                 TR_SET_SCL;
104                 TR_DELAY;
105                 TR_CLR_SCL;
106                 TR_DELAY;
107         }
108
109         /* stop condition */
110         TR_CLR_SDA;
111         TR_DELAY;
112         TR_SET_SCL;
113         TR_DELAY;
114         TR_SET_SDA;
115         TR_DELAY;
116
117         va_end(args);
118 }
119
120 static void tr_setvol(struct trust *tr, __u16 vol)
121 {
122         mutex_lock(&tr->lock);
123         tr->curvol = vol / 2048;
124         write_i2c(tr, 2, TDA7318_ADDR, tr->curvol ^ 0x1f);
125         mutex_unlock(&tr->lock);
126 }
127
128 static int basstreble2chip[15] = {
129         0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
130 };
131
132 static void tr_setbass(struct trust *tr, __u16 bass)
133 {
134         mutex_lock(&tr->lock);
135         tr->curbass = bass / 4370;
136         write_i2c(tr, 2, TDA7318_ADDR, 0x60 | basstreble2chip[tr->curbass]);
137         mutex_unlock(&tr->lock);
138 }
139
140 static void tr_settreble(struct trust *tr, __u16 treble)
141 {
142         mutex_lock(&tr->lock);
143         tr->curtreble = treble / 4370;
144         write_i2c(tr, 2, TDA7318_ADDR, 0x70 | basstreble2chip[tr->curtreble]);
145         mutex_unlock(&tr->lock);
146 }
147
148 static void tr_setstereo(struct trust *tr, int stereo)
149 {
150         mutex_lock(&tr->lock);
151         tr->curstereo = !!stereo;
152         tr->ioval = (tr->ioval & 0xfb) | (!tr->curstereo << 2);
153         outb(tr->ioval, tr->io);
154         mutex_unlock(&tr->lock);
155 }
156
157 static void tr_setmute(struct trust *tr, int mute)
158 {
159         mutex_lock(&tr->lock);
160         tr->curmute = !!mute;
161         tr->ioval = (tr->ioval & 0xf7) | (tr->curmute << 3);
162         outb(tr->ioval, tr->io);
163         mutex_unlock(&tr->lock);
164 }
165
166 static int tr_getsigstr(struct trust *tr)
167 {
168         int i, v;
169
170         mutex_lock(&tr->lock);
171         for (i = 0, v = 0; i < 100; i++)
172                 v |= inb(tr->io);
173         mutex_unlock(&tr->lock);
174         return (v & 1) ? 0 : 0xffff;
175 }
176
177 static int tr_getstereo(struct trust *tr)
178 {
179         /* don't know how to determine it, just return the setting */
180         return tr->curstereo;
181 }
182
183 static void tr_setfreq(struct trust *tr, unsigned long f)
184 {
185         mutex_lock(&tr->lock);
186         tr->curfreq = f;
187         f /= 160;       /* Convert to 10 kHz units      */
188         f += 1070;      /* Add 10.7 MHz IF              */
189         write_i2c(tr, 5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0);
190         mutex_unlock(&tr->lock);
191 }
192
193 static int vidioc_querycap(struct file *file, void *priv,
194                                 struct v4l2_capability *v)
195 {
196         strlcpy(v->driver, "radio-trust", sizeof(v->driver));
197         strlcpy(v->card, "Trust FM Radio", sizeof(v->card));
198         strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
199         v->version = RADIO_VERSION;
200         v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
201         return 0;
202 }
203
204 static int vidioc_g_tuner(struct file *file, void *priv,
205                                 struct v4l2_tuner *v)
206 {
207         struct trust *tr = video_drvdata(file);
208
209         if (v->index > 0)
210                 return -EINVAL;
211
212         strlcpy(v->name, "FM", sizeof(v->name));
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;
218         if (tr_getstereo(tr))
219                 v->audmode = V4L2_TUNER_MODE_STEREO;
220         else
221                 v->audmode = V4L2_TUNER_MODE_MONO;
222         v->signal = tr_getsigstr(tr);
223         return 0;
224 }
225
226 static int vidioc_s_tuner(struct file *file, void *priv,
227                                 struct v4l2_tuner *v)
228 {
229         struct trust *tr = video_drvdata(file);
230
231         if (v->index)
232                 return -EINVAL;
233         tr_setstereo(tr, v->audmode == V4L2_TUNER_MODE_STEREO);
234         return 0;
235 }
236
237 static int vidioc_s_frequency(struct file *file, void *priv,
238                                 struct v4l2_frequency *f)
239 {
240         struct trust *tr = video_drvdata(file);
241
242         tr_setfreq(tr, f->frequency);
243         return 0;
244 }
245
246 static int vidioc_g_frequency(struct file *file, void *priv,
247                                 struct v4l2_frequency *f)
248 {
249         struct trust *tr = video_drvdata(file);
250
251         f->type = V4L2_TUNER_RADIO;
252         f->frequency = tr->curfreq;
253         return 0;
254 }
255
256 static int vidioc_queryctrl(struct file *file, void *priv,
257                                 struct v4l2_queryctrl *qc)
258 {
259         switch (qc->id) {
260         case V4L2_CID_AUDIO_MUTE:
261                 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
262         case V4L2_CID_AUDIO_VOLUME:
263                 return v4l2_ctrl_query_fill(qc, 0, 65535, 2048, 65535);
264         case V4L2_CID_AUDIO_BASS:
265         case V4L2_CID_AUDIO_TREBLE:
266                 return v4l2_ctrl_query_fill(qc, 0, 65535, 4370, 32768);
267         }
268         return -EINVAL;
269 }
270
271 static int vidioc_g_ctrl(struct file *file, void *priv,
272                                 struct v4l2_control *ctrl)
273 {
274         struct trust *tr = video_drvdata(file);
275
276         switch (ctrl->id) {
277         case V4L2_CID_AUDIO_MUTE:
278                 ctrl->value = tr->curmute;
279                 return 0;
280         case V4L2_CID_AUDIO_VOLUME:
281                 ctrl->value = tr->curvol * 2048;
282                 return 0;
283         case V4L2_CID_AUDIO_BASS:
284                 ctrl->value = tr->curbass * 4370;
285                 return 0;
286         case V4L2_CID_AUDIO_TREBLE:
287                 ctrl->value = tr->curtreble * 4370;
288                 return 0;
289         }
290         return -EINVAL;
291 }
292
293 static int vidioc_s_ctrl(struct file *file, void *priv,
294                                 struct v4l2_control *ctrl)
295 {
296         struct trust *tr = video_drvdata(file);
297
298         switch (ctrl->id) {
299         case V4L2_CID_AUDIO_MUTE:
300                 tr_setmute(tr, ctrl->value);
301                 return 0;
302         case V4L2_CID_AUDIO_VOLUME:
303                 tr_setvol(tr, ctrl->value);
304                 return 0;
305         case V4L2_CID_AUDIO_BASS:
306                 tr_setbass(tr, ctrl->value);
307                 return 0;
308         case V4L2_CID_AUDIO_TREBLE:
309                 tr_settreble(tr, ctrl->value);
310                 return 0;
311         }
312         return -EINVAL;
313 }
314
315 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
316 {
317         *i = 0;
318         return 0;
319 }
320
321 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
322 {
323         return i ? -EINVAL : 0;
324 }
325
326 static int vidioc_g_audio(struct file *file, void *priv,
327                                 struct v4l2_audio *a)
328 {
329         a->index = 0;
330         strlcpy(a->name, "Radio", sizeof(a->name));
331         a->capability = V4L2_AUDCAP_STEREO;
332         return 0;
333 }
334
335 static int vidioc_s_audio(struct file *file, void *priv,
336                                 struct v4l2_audio *a)
337 {
338         return a->index ? -EINVAL : 0;
339 }
340
341 static int trust_open(struct file *file)
342 {
343         return 0;
344 }
345
346 static int trust_release(struct file *file)
347 {
348         return 0;
349 }
350
351 static const struct v4l2_file_operations trust_fops = {
352         .owner          = THIS_MODULE,
353         .open           = trust_open,
354         .release        = trust_release,
355         .ioctl          = video_ioctl2,
356 };
357
358 static const struct v4l2_ioctl_ops trust_ioctl_ops = {
359         .vidioc_querycap    = vidioc_querycap,
360         .vidioc_g_tuner     = vidioc_g_tuner,
361         .vidioc_s_tuner     = vidioc_s_tuner,
362         .vidioc_g_frequency = vidioc_g_frequency,
363         .vidioc_s_frequency = vidioc_s_frequency,
364         .vidioc_queryctrl   = vidioc_queryctrl,
365         .vidioc_g_ctrl      = vidioc_g_ctrl,
366         .vidioc_s_ctrl      = vidioc_s_ctrl,
367         .vidioc_g_audio     = vidioc_g_audio,
368         .vidioc_s_audio     = vidioc_s_audio,
369         .vidioc_g_input     = vidioc_g_input,
370         .vidioc_s_input     = vidioc_s_input,
371 };
372
373 static int __init trust_init(void)
374 {
375         struct trust *tr = &trust_card;
376         struct v4l2_device *v4l2_dev = &tr->v4l2_dev;
377         int res;
378
379         strlcpy(v4l2_dev->name, "trust", sizeof(v4l2_dev->name));
380         tr->io = io;
381         tr->ioval = 0xf;
382         mutex_init(&tr->lock);
383
384         if (tr->io == -1) {
385                 v4l2_err(v4l2_dev, "You must set an I/O address with io=0x0x350 or 0x358\n");
386                 return -EINVAL;
387         }
388         if (!request_region(tr->io, 2, "Trust FM Radio")) {
389                 v4l2_err(v4l2_dev, "port 0x%x already in use\n", tr->io);
390                 return -EBUSY;
391         }
392
393         res = v4l2_device_register(NULL, v4l2_dev);
394         if (res < 0) {
395                 release_region(tr->io, 2);
396                 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
397                 return res;
398         }
399
400         strlcpy(tr->vdev.name, v4l2_dev->name, sizeof(tr->vdev.name));
401         tr->vdev.v4l2_dev = v4l2_dev;
402         tr->vdev.fops = &trust_fops;
403         tr->vdev.ioctl_ops = &trust_ioctl_ops;
404         tr->vdev.release = video_device_release_empty;
405         video_set_drvdata(&tr->vdev, tr);
406
407         if (video_register_device(&tr->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
408                 v4l2_device_unregister(v4l2_dev);
409                 release_region(tr->io, 2);
410                 return -EINVAL;
411         }
412
413         v4l2_info(v4l2_dev, "Trust FM Radio card driver v1.0.\n");
414
415         write_i2c(tr, 2, TDA7318_ADDR, 0x80);   /* speaker att. LF = 0 dB */
416         write_i2c(tr, 2, TDA7318_ADDR, 0xa0);   /* speaker att. RF = 0 dB */
417         write_i2c(tr, 2, TDA7318_ADDR, 0xc0);   /* speaker att. LR = 0 dB */
418         write_i2c(tr, 2, TDA7318_ADDR, 0xe0);   /* speaker att. RR = 0 dB */
419         write_i2c(tr, 2, TDA7318_ADDR, 0x40);   /* stereo 1 input, gain = 18.75 dB */
420
421         tr_setvol(tr, 0xffff);
422         tr_setbass(tr, 0x8000);
423         tr_settreble(tr, 0x8000);
424         tr_setstereo(tr, 1);
425
426         /* mute card - prevents noisy bootups */
427         tr_setmute(tr, 1);
428
429         return 0;
430 }
431
432 static void __exit cleanup_trust_module(void)
433 {
434         struct trust *tr = &trust_card;
435
436         video_unregister_device(&tr->vdev);
437         v4l2_device_unregister(&tr->v4l2_dev);
438         release_region(tr->io, 2);
439 }
440
441 module_init(trust_init);
442 module_exit(cleanup_trust_module);