blob: eb72a4d13758934493127b5866914ba573fed994 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Typhoon Radio Card driver for radio support
2 * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Notes on the hardware
5 *
6 * This card has two output sockets, one for speakers and one for line.
7 * The speaker output has volume control, but only in four discrete
8 * steps. The line output has neither volume control nor mute.
9 *
10 * The card has auto-stereo according to its manual, although it all
11 * sounds mono to me (even with the Win/DOS drivers). Maybe it's my
12 * antenna - I really don't know for sure.
13 *
14 * Frequency control is done digitally.
15 *
16 * Volume control is done digitally, but there are only four different
17 * possible values. So you should better always turn the volume up and
18 * use line control. I got the best results by connecting line output
19 * to the sound card microphone input. For such a configuration the
20 * volume control has no effect, since volume control only influences
21 * the speaker output.
22 *
23 * There is no explicit mute/unmute. So I set the radio frequency to a
24 * value where I do expect just noise and turn the speaker volume down.
25 * The frequency change is necessary since the card never seems to be
26 * completely silent.
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030027 *
28 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31#include <linux/module.h> /* Modules */
32#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070033#include <linux/ioport.h> /* request_region */
Mauro Carvalho Chehab30c48302006-08-08 09:10:04 -030034#include <linux/videodev2.h> /* kernel radio structs */
Hans Verkuilf1bfe892009-03-06 13:54:52 -030035#include <linux/io.h> /* outb, outb_p */
Hans Verkuil9f1dfcc2012-02-29 05:50:27 -030036#include <linux/slab.h>
Hans Verkuilf1bfe892009-03-06 13:54:52 -030037#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030038#include <media/v4l2-ioctl.h>
Hans Verkuilda1ff352012-01-16 05:17:32 -030039#include "radio-isa.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030041#define DRIVER_VERSION "0.1.2"
42
Hans Verkuilf1bfe892009-03-06 13:54:52 -030043MODULE_AUTHOR("Dr. Henrik Seidel");
44MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
45MODULE_LICENSE("GPL");
Hans Verkuilda1ff352012-01-16 05:17:32 -030046MODULE_VERSION("0.1.99");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#ifndef CONFIG_RADIO_TYPHOON_PORT
49#define CONFIG_RADIO_TYPHOON_PORT -1
50#endif
51
52#ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
Hans Verkuilda1ff352012-01-16 05:17:32 -030053#define CONFIG_RADIO_TYPHOON_MUTEFREQ 87000
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#endif
55
Hans Verkuilda1ff352012-01-16 05:17:32 -030056#define TYPHOON_MAX 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Hans Verkuilda1ff352012-01-16 05:17:32 -030058static int io[TYPHOON_MAX] = { [0] = CONFIG_RADIO_TYPHOON_PORT,
59 [1 ... (TYPHOON_MAX - 1)] = -1 };
60static int radio_nr[TYPHOON_MAX] = { [0 ... (TYPHOON_MAX - 1)] = -1 };
Hans Verkuilf1bfe892009-03-06 13:54:52 -030061static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
Hans Verkuilda1ff352012-01-16 05:17:32 -030062
63module_param_array(io, int, NULL, 0444);
64MODULE_PARM_DESC(io, "I/O addresses of the Typhoon card (0x316 or 0x336)");
65module_param_array(radio_nr, int, NULL, 0444);
66MODULE_PARM_DESC(radio_nr, "Radio device numbers");
Hans Verkuilf1bfe892009-03-06 13:54:52 -030067module_param(mutefreq, ulong, 0);
68MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
69
Hans Verkuilf1bfe892009-03-06 13:54:52 -030070struct typhoon {
Hans Verkuilda1ff352012-01-16 05:17:32 -030071 struct radio_isa_card isa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int muted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Hans Verkuilda1ff352012-01-16 05:17:32 -030075static struct radio_isa_card *typhoon_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Hans Verkuilda1ff352012-01-16 05:17:32 -030077 struct typhoon *ty = kzalloc(sizeof(*ty), GFP_KERNEL);
78
79 return ty ? &ty->isa : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Hans Verkuilda1ff352012-01-16 05:17:32 -030082static int typhoon_s_frequency(struct radio_isa_card *isa, u32 freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 unsigned long outval;
85 unsigned long x;
86
87 /*
88 * The frequency transfer curve is not linear. The best fit I could
89 * get is
90 *
91 * outval = -155 + exp((f + 15.55) * 0.057))
92 *
93 * where frequency f is in MHz. Since we don't have exp in the kernel,
94 * I approximate this function by a third order polynomial.
95 *
96 */
97
Hans Verkuilda1ff352012-01-16 05:17:32 -030098 x = freq / 160;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 outval = (x * x + 2500) / 5000;
100 outval = (outval * x + 5000) / 10000;
101 outval -= (10 * x * x + 10433) / 20866;
102 outval += 4 * x - 11505;
103
Hans Verkuilda1ff352012-01-16 05:17:32 -0300104 outb_p((outval >> 8) & 0x01, isa->io + 4);
105 outb_p(outval >> 9, isa->io + 6);
106 outb_p(outval & 0xff, isa->io + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108}
109
Hans Verkuilda1ff352012-01-16 05:17:32 -0300110static int typhoon_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Hans Verkuilda1ff352012-01-16 05:17:32 -0300112 struct typhoon *ty = container_of(isa, struct typhoon, isa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Hans Verkuilda1ff352012-01-16 05:17:32 -0300114 if (mute)
115 vol = 0;
116 vol >>= 14; /* Map 16 bit to 2 bit */
117 vol &= 3;
118 outb_p(vol / 2, isa->io); /* Set the volume, high bit. */
119 outb_p(vol % 2, isa->io + 2); /* Set the volume, low bit. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Hans Verkuilda1ff352012-01-16 05:17:32 -0300121 if (vol == 0 && !ty->muted) {
122 ty->muted = true;
123 return typhoon_s_frequency(isa, mutefreq << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Hans Verkuilda1ff352012-01-16 05:17:32 -0300125 if (vol && ty->muted) {
126 ty->muted = false;
127 return typhoon_s_frequency(isa, isa->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130}
131
Hans Verkuilda1ff352012-01-16 05:17:32 -0300132static const struct radio_isa_ops typhoon_ops = {
133 .alloc = typhoon_alloc,
134 .s_mute_volume = typhoon_s_mute_volume,
135 .s_frequency = typhoon_s_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Hans Verkuilda1ff352012-01-16 05:17:32 -0300138static const int typhoon_ioports[] = { 0x316, 0x336 };
139
140static struct radio_isa_driver typhoon_driver = {
141 .driver = {
142 .match = radio_isa_match,
143 .probe = radio_isa_probe,
144 .remove = radio_isa_remove,
145 .driver = {
146 .name = "radio-typhoon",
147 },
148 },
149 .io_params = io,
150 .radio_nr_params = radio_nr,
151 .io_ports = typhoon_ioports,
152 .num_of_io_ports = ARRAY_SIZE(typhoon_ioports),
153 .region_size = 8,
154 .card = "Typhoon Radio",
155 .ops = &typhoon_ops,
156 .has_stereo = true,
157 .max_volume = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static int __init typhoon_init(void)
161{
Hans Verkuilda1ff352012-01-16 05:17:32 -0300162 if (mutefreq < 87000 || mutefreq > 108000) {
163 printk(KERN_ERR "%s: You must set a frequency (in kHz) used when muting the card,\n",
164 typhoon_driver.driver.driver.name);
165 printk(KERN_ERR "%s: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108000)\n",
166 typhoon_driver.driver.driver.name);
167 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Hans Verkuilda1ff352012-01-16 05:17:32 -0300169 return isa_register_driver(&typhoon_driver.driver, TYPHOON_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300172static void __exit typhoon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Hans Verkuilda1ff352012-01-16 05:17:32 -0300174 isa_unregister_driver(&typhoon_driver.driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Hans Verkuilda1ff352012-01-16 05:17:32 -0300177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178module_init(typhoon_init);
Hans Verkuilf1bfe892009-03-06 13:54:52 -0300179module_exit(typhoon_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180