[POWERPC] Add DTS file for the Motorola PrPMC2800 platform
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / radio / radio-sf16fmr2.c
blobe6c125def5cb860dec082c66c91e067e9651a7b1
1 /* SF16FMR2 radio driver for Linux radio support
2 * heavily based on fmi driver...
3 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
5 * Notes on the hardware
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
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)
14 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17 #include <linux/module.h> /* Modules */
18 #include <linux/init.h> /* Initdata */
19 #include <linux/ioport.h> /* request_region */
20 #include <linux/delay.h> /* udelay */
21 #include <asm/io.h> /* outb, outb_p */
22 #include <asm/uaccess.h> /* copy to/from user */
23 #include <linux/videodev2.h> /* kernel radio structs */
24 #include <media/v4l2-common.h>
25 #include <linux/mutex.h>
27 static struct mutex lock;
29 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
30 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
32 static struct v4l2_queryctrl radio_qctrl[] = {
34 .id = V4L2_CID_AUDIO_MUTE,
35 .name = "Mute",
36 .minimum = 0,
37 .maximum = 1,
38 .default_value = 1,
39 .type = V4L2_CTRL_TYPE_BOOLEAN,
40 },{
41 .id = V4L2_CID_AUDIO_VOLUME,
42 .name = "Volume",
43 .minimum = 0,
44 .maximum = 65535,
45 .step = 1<<12,
46 .default_value = 0xff,
47 .type = V4L2_CTRL_TYPE_INTEGER,
51 #undef DEBUG
52 //#define DEBUG 1
54 #ifdef DEBUG
55 # define debug_print(s) printk s
56 #else
57 # define debug_print(s)
58 #endif
60 /* this should be static vars for module size */
61 struct fmr2_device
63 int port;
64 int curvol; /* 0-65535, if not volume 0 or 65535 */
65 int mute;
66 int stereo; /* card is producing stereo audio */
67 unsigned long curfreq; /* freq in kHz */
68 int card_type;
69 __u32 flags;
72 static int io = 0x384;
73 static int radio_nr = -1;
75 /* hw precision is 12.5 kHz
76 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
77 * other bits will be truncated
79 #define RSF16_ENCODE(x) ((x)/200+856)
80 #define RSF16_MINFREQ 87*16000
81 #define RSF16_MAXFREQ 108*16000
83 static inline void wait(int n,int port)
85 for (;n;--n) inb(port);
88 static void outbits(int bits, unsigned int data, int nWait, int port)
90 int bit;
91 for(;--bits>=0;) {
92 bit = (data>>bits) & 1;
93 outb(bit,port);
94 wait(nWait,port);
95 outb(bit|2,port);
96 wait(nWait,port);
97 outb(bit,port);
98 wait(nWait,port);
102 static inline void fmr2_mute(int port)
104 outb(0x00, port);
105 wait(4,port);
108 static inline void fmr2_unmute(int port)
110 outb(0x04, port);
111 wait(4,port);
114 static inline int fmr2_stereo_mode(int port)
116 int n = inb(port);
117 outb(6,port);
118 inb(port);
119 n = ((n>>3)&1)^1;
120 debug_print((KERN_DEBUG "stereo: %d\n", n));
121 return n;
124 static int fmr2_product_info(struct fmr2_device *dev)
126 int n = inb(dev->port);
127 n &= 0xC1;
128 if (n == 0)
130 /* this should support volume set */
131 dev->card_type = 12;
132 return 0;
134 /* not volume (mine is 11) */
135 dev->card_type = (n==128)?11:0;
136 return n;
139 static inline int fmr2_getsigstr(struct fmr2_device *dev)
141 /* !!! work only if scanning freq */
142 int port = dev->port, res = 0xffff;
143 outb(5,port);
144 wait(4,port);
145 if (!(inb(port)&1)) res = 0;
146 debug_print((KERN_DEBUG "signal: %d\n", res));
147 return res;
150 /* set frequency and unmute card */
151 static int fmr2_setfreq(struct fmr2_device *dev)
153 int port = dev->port;
154 unsigned long freq = dev->curfreq;
156 fmr2_mute(port);
158 /* 0x42 for mono output
159 * 0x102 forward scanning
160 * 0x182 scansione avanti
162 outbits(9,0x2,3,port);
163 outbits(16,RSF16_ENCODE(freq),2,port);
165 fmr2_unmute(port);
167 /* wait 0.11 sec */
168 msleep(110);
170 /* NOTE if mute this stop radio
171 you must set freq on unmute */
172 dev->stereo = fmr2_stereo_mode(port);
173 return 0;
176 /* !!! not tested, in my card this does't work !!! */
177 static int fmr2_setvolume(struct fmr2_device *dev)
179 int i,a,n, port = dev->port;
181 if (dev->card_type != 11) return 1;
183 switch( (dev->curvol+(1<<11)) >> 12 )
185 case 0: case 1: n = 0x21; break;
186 case 2: n = 0x84; break;
187 case 3: n = 0x90; break;
188 case 4: n = 0x104; break;
189 case 5: n = 0x110; break;
190 case 6: n = 0x204; break;
191 case 7: n = 0x210; break;
192 case 8: n = 0x402; break;
193 case 9: n = 0x404; break;
194 default:
195 case 10: n = 0x408; break;
196 case 11: n = 0x410; break;
197 case 12: n = 0x801; break;
198 case 13: n = 0x802; break;
199 case 14: n = 0x804; break;
200 case 15: n = 0x808; break;
201 case 16: n = 0x810; break;
203 for(i=12;--i>=0;)
205 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
206 outb(a|4, port);
207 wait(4,port);
208 outb(a|0x24, port);
209 wait(4,port);
210 outb(a|4, port);
211 wait(4,port);
213 for(i=6;--i>=0;)
215 a = ((0x18 >> i) & 1) << 6;
216 outb(a|4, port);
217 wait(4,port);
218 outb(a|0x24, port);
219 wait(4,port);
220 outb(a|4, port);
221 wait(4,port);
223 wait(4,port);
224 outb(0x14, port);
226 return 0;
229 static int vidioc_querycap(struct file *file, void *priv,
230 struct v4l2_capability *v)
232 strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
233 strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
234 sprintf(v->bus_info, "ISA");
235 v->version = RADIO_VERSION;
236 v->capabilities = V4L2_CAP_TUNER;
237 return 0;
240 static int vidioc_g_tuner(struct file *file, void *priv,
241 struct v4l2_tuner *v)
243 int mult;
244 struct video_device *dev = video_devdata(file);
245 struct fmr2_device *fmr2 = dev->priv;
247 if (v->index > 0)
248 return -EINVAL;
250 strcpy(v->name, "FM");
251 v->type = V4L2_TUNER_RADIO;
253 mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
254 v->rangelow = RSF16_MINFREQ/mult;
255 v->rangehigh = RSF16_MAXFREQ/mult;
256 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
257 v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
258 v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
259 V4L2_TUNER_MODE_MONO;
260 mutex_lock(&lock);
261 v->signal = fmr2_getsigstr(fmr2);
262 mutex_unlock(&lock);
263 return 0;
266 static int vidioc_s_tuner(struct file *file, void *priv,
267 struct v4l2_tuner *v)
269 if (v->index > 0)
270 return -EINVAL;
271 return 0;
274 static int vidioc_s_frequency(struct file *file, void *priv,
275 struct v4l2_frequency *f)
277 struct video_device *dev = video_devdata(file);
278 struct fmr2_device *fmr2 = dev->priv;
280 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
281 f->frequency *= 1000;
282 if (f->frequency < RSF16_MINFREQ ||
283 f->frequency > RSF16_MAXFREQ )
284 return -EINVAL;
285 /*rounding in steps of 200 to match th freq
286 that will be used */
287 fmr2->curfreq = (f->frequency/200)*200;
289 /* set card freq (if not muted) */
290 if (fmr2->curvol && !fmr2->mute) {
291 mutex_lock(&lock);
292 fmr2_setfreq(fmr2);
293 mutex_unlock(&lock);
295 return 0;
298 static int vidioc_g_frequency(struct file *file, void *priv,
299 struct v4l2_frequency *f)
301 struct video_device *dev = video_devdata(file);
302 struct fmr2_device *fmr2 = dev->priv;
304 f->type = V4L2_TUNER_RADIO;
305 f->frequency = fmr2->curfreq;
306 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
307 f->frequency /= 1000;
308 return 0;
311 static int vidioc_queryctrl(struct file *file, void *priv,
312 struct v4l2_queryctrl *qc)
314 int i;
315 struct video_device *dev = video_devdata(file);
316 struct fmr2_device *fmr2 = dev->priv;
318 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
319 if ((fmr2->card_type != 11)
320 && V4L2_CID_AUDIO_VOLUME)
321 radio_qctrl[i].step = 65535;
322 if (qc->id && qc->id == radio_qctrl[i].id) {
323 memcpy(qc, &(radio_qctrl[i]),
324 sizeof(*qc));
325 return 0;
328 return -EINVAL;
331 static int vidioc_g_ctrl(struct file *file, void *priv,
332 struct v4l2_control *ctrl)
334 struct video_device *dev = video_devdata(file);
335 struct fmr2_device *fmr2 = dev->priv;
337 switch (ctrl->id) {
338 case V4L2_CID_AUDIO_MUTE:
339 ctrl->value = fmr2->mute;
340 return 0;
341 case V4L2_CID_AUDIO_VOLUME:
342 ctrl->value = fmr2->curvol;
343 return 0;
345 return -EINVAL;
348 static int vidioc_s_ctrl(struct file *file, void *priv,
349 struct v4l2_control *ctrl)
351 struct video_device *dev = video_devdata(file);
352 struct fmr2_device *fmr2 = dev->priv;
354 switch (ctrl->id) {
355 case V4L2_CID_AUDIO_MUTE:
356 fmr2->mute = ctrl->value;
357 if (fmr2->card_type != 11) {
358 if (!fmr2->mute)
359 fmr2->curvol = 65535;
360 else
361 fmr2->curvol = 0;
363 break;
364 case V4L2_CID_AUDIO_VOLUME:
365 fmr2->curvol = ctrl->value;
366 if (fmr2->card_type != 11) {
367 if (fmr2->curvol) {
368 fmr2->curvol = 65535;
369 fmr2->mute = 0;
370 } else {
371 fmr2->curvol = 0;
372 fmr2->mute = 1;
375 break;
376 default:
377 return -EINVAL;
380 #ifdef DEBUG
381 if (fmr2->curvol && !fmr2->mute)
382 printk(KERN_DEBUG "unmute\n");
383 else
384 printk(KERN_DEBUG "mute\n");
385 #endif
387 mutex_lock(&lock);
388 if (fmr2->curvol && !fmr2->mute) {
389 fmr2_setvolume(fmr2);
390 fmr2_setfreq(fmr2);
391 } else
392 fmr2_mute(fmr2->port);
393 mutex_unlock(&lock);
394 return 0;
397 static int vidioc_g_audio(struct file *file, void *priv,
398 struct v4l2_audio *a)
400 if (a->index > 1)
401 return -EINVAL;
403 strcpy(a->name, "Radio");
404 a->capability = V4L2_AUDCAP_STEREO;
405 return 0;
408 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
410 *i = 0;
411 return 0;
414 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
416 if (i != 0)
417 return -EINVAL;
418 return 0;
421 static int vidioc_s_audio(struct file *file, void *priv,
422 struct v4l2_audio *a)
424 if (a->index != 0)
425 return -EINVAL;
426 return 0;
429 static struct fmr2_device fmr2_unit;
431 static const struct file_operations fmr2_fops = {
432 .owner = THIS_MODULE,
433 .open = video_exclusive_open,
434 .release = video_exclusive_release,
435 .ioctl = video_ioctl2,
436 .compat_ioctl = v4l_compat_ioctl32,
437 .llseek = no_llseek,
440 static struct video_device fmr2_radio=
442 .owner = THIS_MODULE,
443 .name = "SF16FMR2 radio",
444 . type = VID_TYPE_TUNER,
445 .hardware = 0,
446 .fops = &fmr2_fops,
447 .vidioc_querycap = vidioc_querycap,
448 .vidioc_g_tuner = vidioc_g_tuner,
449 .vidioc_s_tuner = vidioc_s_tuner,
450 .vidioc_g_audio = vidioc_g_audio,
451 .vidioc_s_audio = vidioc_s_audio,
452 .vidioc_g_input = vidioc_g_input,
453 .vidioc_s_input = vidioc_s_input,
454 .vidioc_g_frequency = vidioc_g_frequency,
455 .vidioc_s_frequency = vidioc_s_frequency,
456 .vidioc_queryctrl = vidioc_queryctrl,
457 .vidioc_g_ctrl = vidioc_g_ctrl,
458 .vidioc_s_ctrl = vidioc_s_ctrl,
461 static int __init fmr2_init(void)
463 fmr2_unit.port = io;
464 fmr2_unit.curvol = 0;
465 fmr2_unit.mute = 0;
466 fmr2_unit.curfreq = 0;
467 fmr2_unit.stereo = 1;
468 fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
469 fmr2_unit.card_type = 0;
470 fmr2_radio.priv = &fmr2_unit;
472 mutex_init(&lock);
474 if (request_region(io, 2, "sf16fmr2"))
476 printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
477 return -EBUSY;
480 if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
482 release_region(io, 2);
483 return -EINVAL;
486 printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
487 /* mute card - prevents noisy bootups */
488 mutex_lock(&lock);
489 fmr2_mute(io);
490 fmr2_product_info(&fmr2_unit);
491 mutex_unlock(&lock);
492 debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
493 return 0;
496 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
497 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
498 MODULE_LICENSE("GPL");
500 module_param(io, int, 0);
501 MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
502 module_param(radio_nr, int, 0);
504 static void __exit fmr2_cleanup_module(void)
506 video_unregister_device(&fmr2_radio);
507 release_region(io,2);
510 module_init(fmr2_init);
511 module_exit(fmr2_cleanup_module);
513 #ifndef MODULE
515 static int __init fmr2_setup_io(char *str)
517 get_option(&str, &io);
518 return 1;
521 __setup("sf16fmr2=", fmr2_setup_io);
523 #endif