blob: 5ffddce800113ffbcf143649e7d1a4cdd86a2768 [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{
67 int port;
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -030068 int curvol; /* 0-15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 int mute;
70 int stereo; /* card is producing stereo audio */
71 unsigned long curfreq; /* freq in kHz */
72 int card_type;
73 __u32 flags;
74};
75
76static int io = 0x384;
77static int radio_nr = -1;
78
79/* hw precision is 12.5 kHz
Adrian Bunka58a4142006-01-10 00:08:17 +010080 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * other bits will be truncated
82 */
83#define RSF16_ENCODE(x) ((x)/200+856)
84#define RSF16_MINFREQ 87*16000
85#define RSF16_MAXFREQ 108*16000
86
87static inline void wait(int n,int port)
88{
89 for (;n;--n) inb(port);
90}
91
92static void outbits(int bits, unsigned int data, int nWait, int port)
93{
94 int bit;
95 for(;--bits>=0;) {
96 bit = (data>>bits) & 1;
97 outb(bit,port);
98 wait(nWait,port);
99 outb(bit|2,port);
100 wait(nWait,port);
101 outb(bit,port);
102 wait(nWait,port);
103 }
104}
105
106static inline void fmr2_mute(int port)
107{
108 outb(0x00, port);
109 wait(4,port);
110}
111
112static inline void fmr2_unmute(int port)
113{
114 outb(0x04, port);
115 wait(4,port);
116}
117
118static inline int fmr2_stereo_mode(int port)
119{
120 int n = inb(port);
121 outb(6,port);
122 inb(port);
123 n = ((n>>3)&1)^1;
124 debug_print((KERN_DEBUG "stereo: %d\n", n));
125 return n;
126}
127
128static int fmr2_product_info(struct fmr2_device *dev)
129{
130 int n = inb(dev->port);
131 n &= 0xC1;
132 if (n == 0)
133 {
134 /* this should support volume set */
135 dev->card_type = 12;
136 return 0;
137 }
138 /* not volume (mine is 11) */
139 dev->card_type = (n==128)?11:0;
140 return n;
141}
142
143static inline int fmr2_getsigstr(struct fmr2_device *dev)
144{
145 /* !!! work only if scanning freq */
146 int port = dev->port, res = 0xffff;
147 outb(5,port);
148 wait(4,port);
149 if (!(inb(port)&1)) res = 0;
150 debug_print((KERN_DEBUG "signal: %d\n", res));
151 return res;
152}
153
154/* set frequency and unmute card */
155static int fmr2_setfreq(struct fmr2_device *dev)
156{
157 int port = dev->port;
158 unsigned long freq = dev->curfreq;
159
160 fmr2_mute(port);
161
162 /* 0x42 for mono output
163 * 0x102 forward scanning
164 * 0x182 scansione avanti
165 */
166 outbits(9,0x2,3,port);
167 outbits(16,RSF16_ENCODE(freq),2,port);
168
169 fmr2_unmute(port);
170
171 /* wait 0.11 sec */
172 msleep(110);
173
174 /* NOTE if mute this stop radio
175 you must set freq on unmute */
176 dev->stereo = fmr2_stereo_mode(port);
177 return 0;
178}
179
180/* !!! not tested, in my card this does't work !!! */
181static int fmr2_setvolume(struct fmr2_device *dev)
182{
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300183 int vol[16] = { 0x021, 0x084, 0x090, 0x104,
184 0x110, 0x204, 0x210, 0x402,
185 0x404, 0x408, 0x410, 0x801,
186 0x802, 0x804, 0x808, 0x810 };
187 int i, a, port = dev->port;
188 int n = vol[dev->curvol & 0x0f];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300190 if (dev->card_type != 11)
191 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300193 for (i = 12; --i >= 0; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300195 outb(a | 4, port);
196 wait(4, port);
197 outb(a | 0x24, port);
198 wait(4, port);
199 outb(a | 4, port);
200 wait(4, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300202 for (i = 6; --i >= 0; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 a = ((0x18 >> i) & 1) << 6;
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300204 outb(a | 4, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 wait(4,port);
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300206 outb(a | 0x24, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 wait(4,port);
208 outb(a|4, port);
209 wait(4,port);
210 }
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300211 wait(4, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 outb(0x14, port);
213
214 return 0;
215}
216
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300217static int vidioc_querycap(struct file *file, void *priv,
218 struct v4l2_capability *v)
219{
220 strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
221 strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
222 sprintf(v->bus_info, "ISA");
223 v->version = RADIO_VERSION;
224 v->capabilities = V4L2_CAP_TUNER;
225 return 0;
226}
227
228static int vidioc_g_tuner(struct file *file, void *priv,
229 struct v4l2_tuner *v)
230{
231 int mult;
232 struct video_device *dev = video_devdata(file);
233 struct fmr2_device *fmr2 = dev->priv;
234
235 if (v->index > 0)
236 return -EINVAL;
237
238 strcpy(v->name, "FM");
239 v->type = V4L2_TUNER_RADIO;
240
241 mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
242 v->rangelow = RSF16_MINFREQ/mult;
243 v->rangehigh = RSF16_MAXFREQ/mult;
244 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
245 v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
246 v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
247 V4L2_TUNER_MODE_MONO;
248 mutex_lock(&lock);
249 v->signal = fmr2_getsigstr(fmr2);
250 mutex_unlock(&lock);
251 return 0;
252}
253
254static int vidioc_s_tuner(struct file *file, void *priv,
255 struct v4l2_tuner *v)
256{
257 if (v->index > 0)
258 return -EINVAL;
259 return 0;
260}
261
262static int vidioc_s_frequency(struct file *file, void *priv,
263 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct video_device *dev = video_devdata(file);
266 struct fmr2_device *fmr2 = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300268 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
269 f->frequency *= 1000;
270 if (f->frequency < RSF16_MINFREQ ||
271 f->frequency > RSF16_MAXFREQ )
272 return -EINVAL;
273 /*rounding in steps of 200 to match th freq
274 that will be used */
275 fmr2->curfreq = (f->frequency/200)*200;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300276
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300277 /* set card freq (if not muted) */
278 if (fmr2->curvol && !fmr2->mute) {
279 mutex_lock(&lock);
280 fmr2_setfreq(fmr2);
281 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300286static int vidioc_g_frequency(struct file *file, void *priv,
287 struct v4l2_frequency *f)
288{
289 struct video_device *dev = video_devdata(file);
290 struct fmr2_device *fmr2 = dev->priv;
291
292 f->type = V4L2_TUNER_RADIO;
293 f->frequency = fmr2->curfreq;
294 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
295 f->frequency /= 1000;
296 return 0;
297}
298
299static int vidioc_queryctrl(struct file *file, void *priv,
300 struct v4l2_queryctrl *qc)
301{
302 int i;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300303
304 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300305 if (qc->id && qc->id == radio_qctrl[i].id) {
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300306 memcpy(qc, &radio_qctrl[i], sizeof(*qc));
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300307 return 0;
308 }
309 }
310 return -EINVAL;
311}
312
313static int vidioc_g_ctrl(struct file *file, void *priv,
314 struct v4l2_control *ctrl)
315{
316 struct video_device *dev = video_devdata(file);
317 struct fmr2_device *fmr2 = dev->priv;
318
319 switch (ctrl->id) {
320 case V4L2_CID_AUDIO_MUTE:
321 ctrl->value = fmr2->mute;
322 return 0;
323 case V4L2_CID_AUDIO_VOLUME:
324 ctrl->value = fmr2->curvol;
325 return 0;
326 }
327 return -EINVAL;
328}
329
330static int vidioc_s_ctrl(struct file *file, void *priv,
331 struct v4l2_control *ctrl)
332{
333 struct video_device *dev = video_devdata(file);
334 struct fmr2_device *fmr2 = dev->priv;
335
336 switch (ctrl->id) {
337 case V4L2_CID_AUDIO_MUTE:
338 fmr2->mute = ctrl->value;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300339 break;
340 case V4L2_CID_AUDIO_VOLUME:
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300341 if (ctrl->value > radio_qctrl[AUD_VOL_INDEX].maximum)
342 fmr2->curvol = radio_qctrl[AUD_VOL_INDEX].maximum;
343 else
344 fmr2->curvol = ctrl->value;
345
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300346 break;
347 default:
348 return -EINVAL;
349 }
350
351#ifdef DEBUG
352 if (fmr2->curvol && !fmr2->mute)
353 printk(KERN_DEBUG "unmute\n");
354 else
355 printk(KERN_DEBUG "mute\n");
356#endif
357
358 mutex_lock(&lock);
359 if (fmr2->curvol && !fmr2->mute) {
360 fmr2_setvolume(fmr2);
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300361 /* Set frequency and unmute card */
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300362 fmr2_setfreq(fmr2);
363 } else
364 fmr2_mute(fmr2->port);
365 mutex_unlock(&lock);
366 return 0;
367}
368
369static int vidioc_g_audio(struct file *file, void *priv,
370 struct v4l2_audio *a)
371{
372 if (a->index > 1)
373 return -EINVAL;
374
375 strcpy(a->name, "Radio");
376 a->capability = V4L2_AUDCAP_STEREO;
377 return 0;
378}
379
380static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
381{
382 *i = 0;
383 return 0;
384}
385
386static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
387{
388 if (i != 0)
389 return -EINVAL;
390 return 0;
391}
392
393static int vidioc_s_audio(struct file *file, void *priv,
394 struct v4l2_audio *a)
395{
396 if (a->index != 0)
397 return -EINVAL;
398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401static struct fmr2_device fmr2_unit;
402
Arjan van de Venfa027c22007-02-12 00:55:33 -0800403static const struct file_operations fmr2_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .owner = THIS_MODULE,
405 .open = video_exclusive_open,
406 .release = video_exclusive_release,
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300407 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300408#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200409 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 .llseek = no_llseek,
412};
413
Hans Verkuila3998102008-07-21 02:57:38 -0300414static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300415 .vidioc_querycap = vidioc_querycap,
416 .vidioc_g_tuner = vidioc_g_tuner,
417 .vidioc_s_tuner = vidioc_s_tuner,
418 .vidioc_g_audio = vidioc_g_audio,
419 .vidioc_s_audio = vidioc_s_audio,
420 .vidioc_g_input = vidioc_g_input,
421 .vidioc_s_input = vidioc_s_input,
422 .vidioc_g_frequency = vidioc_g_frequency,
423 .vidioc_s_frequency = vidioc_s_frequency,
424 .vidioc_queryctrl = vidioc_queryctrl,
425 .vidioc_g_ctrl = vidioc_g_ctrl,
426 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Hans Verkuila3998102008-07-21 02:57:38 -0300429static struct video_device fmr2_radio = {
430 .owner = THIS_MODULE,
431 .name = "SF16FMR2 radio",
432 .type = VID_TYPE_TUNER,
433 .fops = &fmr2_fops,
434 .ioctl_ops = &fmr2_ioctl_ops,
435};
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static int __init fmr2_init(void)
438{
439 fmr2_unit.port = io;
440 fmr2_unit.curvol = 0;
441 fmr2_unit.mute = 0;
442 fmr2_unit.curfreq = 0;
443 fmr2_unit.stereo = 1;
Mauro Carvalho Chehabacda0e72006-08-08 09:10:02 -0300444 fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 fmr2_unit.card_type = 0;
446 fmr2_radio.priv = &fmr2_unit;
447
Ingo Molnar3593cab2006-02-07 06:49:14 -0200448 mutex_init(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Douglas Schilling Landgrafdd49f302008-01-27 14:29:51 -0300450 if (!request_region(io, 2, "sf16fmr2")) {
451 printk(KERN_ERR "radio-sf16fmr2: request_region failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -EBUSY;
453 }
454
Andrew Morton98512f72008-01-07 05:24:51 -0300455 if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 release_region(io, 2);
457 return -EINVAL;
458 }
459
460 printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* mute card - prevents noisy bootups */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200462 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 fmr2_mute(io);
464 fmr2_product_info(&fmr2_unit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200465 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
Mauro Carvalho Chehabb2cb2002008-04-22 14:46:03 -0300467
468 /* Only card_type == 11 implements volume */
469 if (fmr2_unit.card_type != 11)
470 radio_qctrl[AUD_VOL_INDEX].maximum = 1;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 0;
473}
474
475MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
476MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
477MODULE_LICENSE("GPL");
478
479module_param(io, int, 0);
480MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
481module_param(radio_nr, int, 0);
482
483static void __exit fmr2_cleanup_module(void)
484{
485 video_unregister_device(&fmr2_radio);
486 release_region(io,2);
487}
488
489module_init(fmr2_init);
490module_exit(fmr2_cleanup_module);
491
492#ifndef MODULE
493
494static int __init fmr2_setup_io(char *str)
495{
496 get_option(&str, &io);
497 return 1;
498}
499
500__setup("sf16fmr2=", fmr2_setup_io);
501
502#endif