1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* Typhoon Radio Card driver for radio support
3*4882a593Smuzhiyun * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Notes on the hardware
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This card has two output sockets, one for speakers and one for line.
8*4882a593Smuzhiyun * The speaker output has volume control, but only in four discrete
9*4882a593Smuzhiyun * steps. The line output has neither volume control nor mute.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The card has auto-stereo according to its manual, although it all
12*4882a593Smuzhiyun * sounds mono to me (even with the Win/DOS drivers). Maybe it's my
13*4882a593Smuzhiyun * antenna - I really don't know for sure.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Frequency control is done digitally.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Volume control is done digitally, but there are only four different
18*4882a593Smuzhiyun * possible values. So you should better always turn the volume up and
19*4882a593Smuzhiyun * use line control. I got the best results by connecting line output
20*4882a593Smuzhiyun * to the sound card microphone input. For such a configuration the
21*4882a593Smuzhiyun * volume control has no effect, since volume control only influences
22*4882a593Smuzhiyun * the speaker output.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * There is no explicit mute/unmute. So I set the radio frequency to a
25*4882a593Smuzhiyun * value where I do expect just noise and turn the speaker volume down.
26*4882a593Smuzhiyun * The frequency change is necessary since the card never seems to be
27*4882a593Smuzhiyun * completely silent.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org>
30*4882a593Smuzhiyun */
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #include <linux/module.h> /* Modules */
33*4882a593Smuzhiyun #include <linux/init.h> /* Initdata */
34*4882a593Smuzhiyun #include <linux/ioport.h> /* request_region */
35*4882a593Smuzhiyun #include <linux/videodev2.h> /* kernel radio structs */
36*4882a593Smuzhiyun #include <linux/io.h> /* outb, outb_p */
37*4882a593Smuzhiyun #include <linux/slab.h>
38*4882a593Smuzhiyun #include <media/v4l2-device.h>
39*4882a593Smuzhiyun #include <media/v4l2-ioctl.h>
40*4882a593Smuzhiyun #include "radio-isa.h"
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define DRIVER_VERSION "0.1.2"
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun MODULE_AUTHOR("Dr. Henrik Seidel");
45*4882a593Smuzhiyun MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
46*4882a593Smuzhiyun MODULE_LICENSE("GPL");
47*4882a593Smuzhiyun MODULE_VERSION("0.1.99");
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #ifndef CONFIG_RADIO_TYPHOON_PORT
50*4882a593Smuzhiyun #define CONFIG_RADIO_TYPHOON_PORT -1
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
54*4882a593Smuzhiyun #define CONFIG_RADIO_TYPHOON_MUTEFREQ 87000
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #define TYPHOON_MAX 2
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun static int io[TYPHOON_MAX] = { [0] = CONFIG_RADIO_TYPHOON_PORT,
60*4882a593Smuzhiyun [1 ... (TYPHOON_MAX - 1)] = -1 };
61*4882a593Smuzhiyun static int radio_nr[TYPHOON_MAX] = { [0 ... (TYPHOON_MAX - 1)] = -1 };
62*4882a593Smuzhiyun static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun module_param_array(io, int, NULL, 0444);
65*4882a593Smuzhiyun MODULE_PARM_DESC(io, "I/O addresses of the Typhoon card (0x316 or 0x336)");
66*4882a593Smuzhiyun module_param_array(radio_nr, int, NULL, 0444);
67*4882a593Smuzhiyun MODULE_PARM_DESC(radio_nr, "Radio device numbers");
68*4882a593Smuzhiyun module_param(mutefreq, ulong, 0);
69*4882a593Smuzhiyun MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun struct typhoon {
72*4882a593Smuzhiyun struct radio_isa_card isa;
73*4882a593Smuzhiyun int muted;
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun
typhoon_alloc(void)76*4882a593Smuzhiyun static struct radio_isa_card *typhoon_alloc(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun struct typhoon *ty = kzalloc(sizeof(*ty), GFP_KERNEL);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun return ty ? &ty->isa : NULL;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
typhoon_s_frequency(struct radio_isa_card * isa,u32 freq)83*4882a593Smuzhiyun static int typhoon_s_frequency(struct radio_isa_card *isa, u32 freq)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun unsigned long outval;
86*4882a593Smuzhiyun unsigned long x;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * The frequency transfer curve is not linear. The best fit I could
90*4882a593Smuzhiyun * get is
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * outval = -155 + exp((f + 15.55) * 0.057))
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * where frequency f is in MHz. Since we don't have exp in the kernel,
95*4882a593Smuzhiyun * I approximate this function by a third order polynomial.
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun x = freq / 160;
100*4882a593Smuzhiyun outval = (x * x + 2500) / 5000;
101*4882a593Smuzhiyun outval = (outval * x + 5000) / 10000;
102*4882a593Smuzhiyun outval -= (10 * x * x + 10433) / 20866;
103*4882a593Smuzhiyun outval += 4 * x - 11505;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun outb_p((outval >> 8) & 0x01, isa->io + 4);
106*4882a593Smuzhiyun outb_p(outval >> 9, isa->io + 6);
107*4882a593Smuzhiyun outb_p(outval & 0xff, isa->io + 8);
108*4882a593Smuzhiyun return 0;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
typhoon_s_mute_volume(struct radio_isa_card * isa,bool mute,int vol)111*4882a593Smuzhiyun static int typhoon_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun struct typhoon *ty = container_of(isa, struct typhoon, isa);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun if (mute)
116*4882a593Smuzhiyun vol = 0;
117*4882a593Smuzhiyun vol >>= 14; /* Map 16 bit to 2 bit */
118*4882a593Smuzhiyun vol &= 3;
119*4882a593Smuzhiyun outb_p(vol / 2, isa->io); /* Set the volume, high bit. */
120*4882a593Smuzhiyun outb_p(vol % 2, isa->io + 2); /* Set the volume, low bit. */
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun if (vol == 0 && !ty->muted) {
123*4882a593Smuzhiyun ty->muted = true;
124*4882a593Smuzhiyun return typhoon_s_frequency(isa, mutefreq << 4);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun if (vol && ty->muted) {
127*4882a593Smuzhiyun ty->muted = false;
128*4882a593Smuzhiyun return typhoon_s_frequency(isa, isa->freq);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun return 0;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun static const struct radio_isa_ops typhoon_ops = {
134*4882a593Smuzhiyun .alloc = typhoon_alloc,
135*4882a593Smuzhiyun .s_mute_volume = typhoon_s_mute_volume,
136*4882a593Smuzhiyun .s_frequency = typhoon_s_frequency,
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun static const int typhoon_ioports[] = { 0x316, 0x336 };
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun static struct radio_isa_driver typhoon_driver = {
142*4882a593Smuzhiyun .driver = {
143*4882a593Smuzhiyun .match = radio_isa_match,
144*4882a593Smuzhiyun .probe = radio_isa_probe,
145*4882a593Smuzhiyun .remove = radio_isa_remove,
146*4882a593Smuzhiyun .driver = {
147*4882a593Smuzhiyun .name = "radio-typhoon",
148*4882a593Smuzhiyun },
149*4882a593Smuzhiyun },
150*4882a593Smuzhiyun .io_params = io,
151*4882a593Smuzhiyun .radio_nr_params = radio_nr,
152*4882a593Smuzhiyun .io_ports = typhoon_ioports,
153*4882a593Smuzhiyun .num_of_io_ports = ARRAY_SIZE(typhoon_ioports),
154*4882a593Smuzhiyun .region_size = 8,
155*4882a593Smuzhiyun .card = "Typhoon Radio",
156*4882a593Smuzhiyun .ops = &typhoon_ops,
157*4882a593Smuzhiyun .has_stereo = true,
158*4882a593Smuzhiyun .max_volume = 3,
159*4882a593Smuzhiyun };
160*4882a593Smuzhiyun
typhoon_init(void)161*4882a593Smuzhiyun static int __init typhoon_init(void)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun if (mutefreq < 87000 || mutefreq > 108000) {
164*4882a593Smuzhiyun printk(KERN_ERR "%s: You must set a frequency (in kHz) used when muting the card,\n",
165*4882a593Smuzhiyun typhoon_driver.driver.driver.name);
166*4882a593Smuzhiyun printk(KERN_ERR "%s: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108000)\n",
167*4882a593Smuzhiyun typhoon_driver.driver.driver.name);
168*4882a593Smuzhiyun return -ENODEV;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun return isa_register_driver(&typhoon_driver.driver, TYPHOON_MAX);
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
typhoon_exit(void)173*4882a593Smuzhiyun static void __exit typhoon_exit(void)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun isa_unregister_driver(&typhoon_driver.driver);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun module_init(typhoon_init);
180*4882a593Smuzhiyun module_exit(typhoon_exit);
181*4882a593Smuzhiyun
182