blob: 14153ed939d01559c1e223d4cc6373b717e3a997 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* SF16FMR2 radio driver for Linux radio support
2 * heavily based on fmi driver...
3 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
4 *
5 * Notes on the hardware
6 *
7 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
8 * No volume control - only mute/unmute - you have to use line volume
9 *
10 * For read stereo/mono you must wait 0.1 sec after set frequency and
11 * card unmuted so I set frequency on unmute
12 * Signal handling seem to work only on autoscanning (not implemented)
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030013 *
14 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#include <linux/module.h> /* Modules */
18#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070019#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h> /* udelay */
21#include <asm/io.h> /* outb, outb_p */
22#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030023#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030024#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030025#include <media/v4l2-ioctl.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Mauro Carvalho Chehab1ddf5bc2006-08-08 09:10:06 -030028static struct mutex lock;
29
30#include <linux/version.h> /* for KERNEL_VERSION MACRO */
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030031#define RADIO_VERSION KERNEL_VERSION(0,0,2)
32
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -030033#define AUD_VOL_INDEX 1
34
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030035static struct v4l2_queryctrl radio_qctrl[] = {
36 {
37 .id = V4L2_CID_AUDIO_MUTE,
38 .name = "Mute",
39 .minimum = 0,
40 .maximum = 1,
41 .default_value = 1,
42 .type = V4L2_CTRL_TYPE_BOOLEAN,
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -030043 },
44 [AUD_VOL_INDEX] = {
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030045 .id = V4L2_CID_AUDIO_VOLUME,
46 .name = "Volume",
47 .minimum = 0,
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -030048 .maximum = 15,
49 .step = 1,
50 .default_value = 0,
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -030051 .type = V4L2_CTRL_TYPE_INTEGER,
52 }
53};
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#undef DEBUG
56//#define DEBUG 1
57
58#ifdef DEBUG
59# define debug_print(s) printk s
60#else
61# define debug_print(s)
62#endif
63
64/* this should be static vars for module size */
65struct fmr2_device
66{
Hans Verkuil3ca685a2008-08-23 04:49:13 -030067 unsigned long in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int port;
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -030069 int curvol; /* 0-15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 int mute;
71 int stereo; /* card is producing stereo audio */
72 unsigned long curfreq; /* freq in kHz */
73 int card_type;
74 __u32 flags;
75};
76
77static int io = 0x384;
78static int radio_nr = -1;
79
80/* hw precision is 12.5 kHz
Adrian Bunka58a4142006-01-10 00:08:17 +010081 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * other bits will be truncated
83 */
84#define RSF16_ENCODE(x) ((x)/200+856)
85#define RSF16_MINFREQ 87*16000
86#define RSF16_MAXFREQ 108*16000
87
88static inline void wait(int n,int port)
89{
90 for (;n;--n) inb(port);
91}
92
93static void outbits(int bits, unsigned int data, int nWait, int port)
94{
95 int bit;
96 for(;--bits>=0;) {
97 bit = (data>>bits) & 1;
98 outb(bit,port);
99 wait(nWait,port);
100 outb(bit|2,port);
101 wait(nWait,port);
102 outb(bit,port);
103 wait(nWait,port);
104 }
105}
106
107static inline void fmr2_mute(int port)
108{
109 outb(0x00, port);
110 wait(4,port);
111}
112
113static inline void fmr2_unmute(int port)
114{
115 outb(0x04, port);
116 wait(4,port);
117}
118
119static inline int fmr2_stereo_mode(int port)
120{
121 int n = inb(port);
122 outb(6,port);
123 inb(port);
124 n = ((n>>3)&1)^1;
125 debug_print((KERN_DEBUG "stereo: %d\n", n));
126 return n;
127}
128
129static int fmr2_product_info(struct fmr2_device *dev)
130{
131 int n = inb(dev->port);
132 n &= 0xC1;
133 if (n == 0)
134 {
135 /* this should support volume set */
136 dev->card_type = 12;
137 return 0;
138 }
139 /* not volume (mine is 11) */
140 dev->card_type = (n==128)?11:0;
141 return n;
142}
143
144static inline int fmr2_getsigstr(struct fmr2_device *dev)
145{
146 /* !!! work only if scanning freq */
147 int port = dev->port, res = 0xffff;
148 outb(5,port);
149 wait(4,port);
150 if (!(inb(port)&1)) res = 0;
151 debug_print((KERN_DEBUG "signal: %d\n", res));
152 return res;
153}
154
155/* set frequency and unmute card */
156static int fmr2_setfreq(struct fmr2_device *dev)
157{
158 int port = dev->port;
159 unsigned long freq = dev->curfreq;
160
161 fmr2_mute(port);
162
163 /* 0x42 for mono output
164 * 0x102 forward scanning
165 * 0x182 scansione avanti
166 */
167 outbits(9,0x2,3,port);
168 outbits(16,RSF16_ENCODE(freq),2,port);
169
170 fmr2_unmute(port);
171
172 /* wait 0.11 sec */
173 msleep(110);
174
175 /* NOTE if mute this stop radio
176 you must set freq on unmute */
177 dev->stereo = fmr2_stereo_mode(port);
178 return 0;
179}
180
181/* !!! not tested, in my card this does't work !!! */
182static int fmr2_setvolume(struct fmr2_device *dev)
183{
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300184 int vol[16] = { 0x021, 0x084, 0x090, 0x104,
185 0x110, 0x204, 0x210, 0x402,
186 0x404, 0x408, 0x410, 0x801,
187 0x802, 0x804, 0x808, 0x810 };
188 int i, a, port = dev->port;
189 int n = vol[dev->curvol & 0x0f];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300191 if (dev->card_type != 11)
192 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300194 for (i = 12; --i >= 0; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300196 outb(a | 4, port);
197 wait(4, port);
198 outb(a | 0x24, port);
199 wait(4, port);
200 outb(a | 4, port);
201 wait(4, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300203 for (i = 6; --i >= 0; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 a = ((0x18 >> i) & 1) << 6;
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300205 outb(a | 4, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 wait(4,port);
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300207 outb(a | 0x24, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 wait(4,port);
209 outb(a|4, port);
210 wait(4,port);
211 }
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300212 wait(4, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 outb(0x14, port);
214
215 return 0;
216}
217
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300218static int vidioc_querycap(struct file *file, void *priv,
219 struct v4l2_capability *v)
220{
221 strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
222 strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
223 sprintf(v->bus_info, "ISA");
224 v->version = RADIO_VERSION;
225 v->capabilities = V4L2_CAP_TUNER;
226 return 0;
227}
228
229static int vidioc_g_tuner(struct file *file, void *priv,
230 struct v4l2_tuner *v)
231{
232 int mult;
233 struct video_device *dev = video_devdata(file);
234 struct fmr2_device *fmr2 = dev->priv;
235
236 if (v->index > 0)
237 return -EINVAL;
238
239 strcpy(v->name, "FM");
240 v->type = V4L2_TUNER_RADIO;
241
242 mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
243 v->rangelow = RSF16_MINFREQ/mult;
244 v->rangehigh = RSF16_MAXFREQ/mult;
245 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
246 v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
247 v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
248 V4L2_TUNER_MODE_MONO;
249 mutex_lock(&lock);
250 v->signal = fmr2_getsigstr(fmr2);
251 mutex_unlock(&lock);
252 return 0;
253}
254
255static int vidioc_s_tuner(struct file *file, void *priv,
256 struct v4l2_tuner *v)
257{
258 if (v->index > 0)
259 return -EINVAL;
260 return 0;
261}
262
263static int vidioc_s_frequency(struct file *file, void *priv,
264 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct video_device *dev = video_devdata(file);
267 struct fmr2_device *fmr2 = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300269 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
270 f->frequency *= 1000;
271 if (f->frequency < RSF16_MINFREQ ||
272 f->frequency > RSF16_MAXFREQ )
273 return -EINVAL;
274 /*rounding in steps of 200 to match th freq
275 that will be used */
276 fmr2->curfreq = (f->frequency/200)*200;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300277
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300278 /* set card freq (if not muted) */
279 if (fmr2->curvol && !fmr2->mute) {
280 mutex_lock(&lock);
281 fmr2_setfreq(fmr2);
282 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300287static int vidioc_g_frequency(struct file *file, void *priv,
288 struct v4l2_frequency *f)
289{
290 struct video_device *dev = video_devdata(file);
291 struct fmr2_device *fmr2 = dev->priv;
292
293 f->type = V4L2_TUNER_RADIO;
294 f->frequency = fmr2->curfreq;
295 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
296 f->frequency /= 1000;
297 return 0;
298}
299
300static int vidioc_queryctrl(struct file *file, void *priv,
301 struct v4l2_queryctrl *qc)
302{
303 int i;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300304
305 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300306 if (qc->id && qc->id == radio_qctrl[i].id) {
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300307 memcpy(qc, &radio_qctrl[i], sizeof(*qc));
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300308 return 0;
309 }
310 }
311 return -EINVAL;
312}
313
314static int vidioc_g_ctrl(struct file *file, void *priv,
315 struct v4l2_control *ctrl)
316{
317 struct video_device *dev = video_devdata(file);
318 struct fmr2_device *fmr2 = dev->priv;
319
320 switch (ctrl->id) {
321 case V4L2_CID_AUDIO_MUTE:
322 ctrl->value = fmr2->mute;
323 return 0;
324 case V4L2_CID_AUDIO_VOLUME:
325 ctrl->value = fmr2->curvol;
326 return 0;
327 }
328 return -EINVAL;
329}
330
331static int vidioc_s_ctrl(struct file *file, void *priv,
332 struct v4l2_control *ctrl)
333{
334 struct video_device *dev = video_devdata(file);
335 struct fmr2_device *fmr2 = dev->priv;
336
337 switch (ctrl->id) {
338 case V4L2_CID_AUDIO_MUTE:
339 fmr2->mute = ctrl->value;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300340 break;
341 case V4L2_CID_AUDIO_VOLUME:
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300342 if (ctrl->value > radio_qctrl[AUD_VOL_INDEX].maximum)
343 fmr2->curvol = radio_qctrl[AUD_VOL_INDEX].maximum;
344 else
345 fmr2->curvol = ctrl->value;
346
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300347 break;
348 default:
349 return -EINVAL;
350 }
351
352#ifdef DEBUG
353 if (fmr2->curvol && !fmr2->mute)
354 printk(KERN_DEBUG "unmute\n");
355 else
356 printk(KERN_DEBUG "mute\n");
357#endif
358
359 mutex_lock(&lock);
360 if (fmr2->curvol && !fmr2->mute) {
361 fmr2_setvolume(fmr2);
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300362 /* Set frequency and unmute card */
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300363 fmr2_setfreq(fmr2);
364 } else
365 fmr2_mute(fmr2->port);
366 mutex_unlock(&lock);
367 return 0;
368}
369
370static int vidioc_g_audio(struct file *file, void *priv,
371 struct v4l2_audio *a)
372{
373 if (a->index > 1)
374 return -EINVAL;
375
376 strcpy(a->name, "Radio");
377 a->capability = V4L2_AUDCAP_STEREO;
378 return 0;
379}
380
381static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
382{
383 *i = 0;
384 return 0;
385}
386
387static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
388{
389 if (i != 0)
390 return -EINVAL;
391 return 0;
392}
393
394static int vidioc_s_audio(struct file *file, void *priv,
395 struct v4l2_audio *a)
396{
397 if (a->index != 0)
398 return -EINVAL;
399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static struct fmr2_device fmr2_unit;
403
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300404static int fmr2_exclusive_open(struct inode *inode, struct file *file)
405{
406 return test_and_set_bit(0, &fmr2_unit.in_use) ? -EBUSY : 0;
407}
408
409static int fmr2_exclusive_release(struct inode *inode, struct file *file)
410{
411 clear_bit(0, &fmr2_unit.in_use);
412 return 0;
413}
414
Arjan van de Venfa027c22007-02-12 00:55:33 -0800415static const struct file_operations fmr2_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .owner = THIS_MODULE,
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300417 .open = fmr2_exclusive_open,
418 .release = fmr2_exclusive_release,
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300419 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300420#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200421 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300422#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .llseek = no_llseek,
424};
425
Hans Verkuila3998102008-07-21 02:57:38 -0300426static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300427 .vidioc_querycap = vidioc_querycap,
428 .vidioc_g_tuner = vidioc_g_tuner,
429 .vidioc_s_tuner = vidioc_s_tuner,
430 .vidioc_g_audio = vidioc_g_audio,
431 .vidioc_s_audio = vidioc_s_audio,
432 .vidioc_g_input = vidioc_g_input,
433 .vidioc_s_input = vidioc_s_input,
434 .vidioc_g_frequency = vidioc_g_frequency,
435 .vidioc_s_frequency = vidioc_s_frequency,
436 .vidioc_queryctrl = vidioc_queryctrl,
437 .vidioc_g_ctrl = vidioc_g_ctrl,
438 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
440
Hans Verkuila3998102008-07-21 02:57:38 -0300441static struct video_device fmr2_radio = {
Hans Verkuila3998102008-07-21 02:57:38 -0300442 .name = "SF16FMR2 radio",
Hans Verkuila3998102008-07-21 02:57:38 -0300443 .fops = &fmr2_fops,
444 .ioctl_ops = &fmr2_ioctl_ops,
445};
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static int __init fmr2_init(void)
448{
449 fmr2_unit.port = io;
450 fmr2_unit.curvol = 0;
451 fmr2_unit.mute = 0;
452 fmr2_unit.curfreq = 0;
453 fmr2_unit.stereo = 1;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300454 fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 fmr2_unit.card_type = 0;
456 fmr2_radio.priv = &fmr2_unit;
457
Ingo Molnar3593cab2006-02-07 06:49:14 -0200458 mutex_init(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Douglas Schilling Landgrafdd49f302008-01-27 14:29:51 -0300460 if (!request_region(io, 2, "sf16fmr2")) {
461 printk(KERN_ERR "radio-sf16fmr2: request_region failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return -EBUSY;
463 }
464
Andrew Morton98512f72008-01-07 05:24:51 -0300465 if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 release_region(io, 2);
467 return -EINVAL;
468 }
469
470 printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* mute card - prevents noisy bootups */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200472 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 fmr2_mute(io);
474 fmr2_product_info(&fmr2_unit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200475 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300477
478 /* Only card_type == 11 implements volume */
479 if (fmr2_unit.card_type != 11)
480 radio_qctrl[AUD_VOL_INDEX].maximum = 1;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483}
484
485MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
486MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
487MODULE_LICENSE("GPL");
488
489module_param(io, int, 0);
490MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
491module_param(radio_nr, int, 0);
492
493static void __exit fmr2_cleanup_module(void)
494{
495 video_unregister_device(&fmr2_radio);
496 release_region(io,2);
497}
498
499module_init(fmr2_init);
500module_exit(fmr2_cleanup_module);
501
502#ifndef MODULE
503
504static int __init fmr2_setup_io(char *str)
505{
506 get_option(&str, &io);
507 return 1;
508}
509
510__setup("sf16fmr2=", fmr2_setup_io);
511
512#endif