Merge branch 'for-linux-next' of git://people.freedesktop.org/~danvet/drm-intel into...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / radio / radio-typhoon.c
CommitLineData
1da177e4
LT
1/* Typhoon Radio Card driver for radio support
2 * (c) 1999 Dr. Henrik Seidel <Henrik.Seidel@gmx.de>
3 *
1da177e4
LT
4 * 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.
30c48305
MCC
27 *
28 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
1da177e4
LT
29 */
30
31#include <linux/module.h> /* Modules */
32#include <linux/init.h> /* Initdata */
fb911ee8 33#include <linux/ioport.h> /* request_region */
30c48305 34#include <linux/videodev2.h> /* kernel radio structs */
f1bfe89b 35#include <linux/io.h> /* outb, outb_p */
9f1dfccf 36#include <linux/slab.h>
f1bfe89b 37#include <media/v4l2-device.h>
35ea11ff 38#include <media/v4l2-ioctl.h>
da1ff351 39#include "radio-isa.h"
1da177e4 40
29834c1a
MCC
41#define DRIVER_VERSION "0.1.2"
42
f1bfe89b
HV
43MODULE_AUTHOR("Dr. Henrik Seidel");
44MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
45MODULE_LICENSE("GPL");
da1ff351 46MODULE_VERSION("0.1.99");
1da177e4
LT
47
48#ifndef CONFIG_RADIO_TYPHOON_PORT
49#define CONFIG_RADIO_TYPHOON_PORT -1
50#endif
51
52#ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
da1ff351 53#define CONFIG_RADIO_TYPHOON_MUTEFREQ 87000
1da177e4
LT
54#endif
55
da1ff351 56#define TYPHOON_MAX 2
f1bfe89b 57
da1ff351
HV
58static 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 };
f1bfe89b 61static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
da1ff351
HV
62
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");
f1bfe89b
HV
67module_param(mutefreq, ulong, 0);
68MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
69
f1bfe89b 70struct typhoon {
da1ff351 71 struct radio_isa_card isa;
1da177e4 72 int muted;
1da177e4
LT
73};
74
da1ff351 75static struct radio_isa_card *typhoon_alloc(void)
1da177e4 76{
da1ff351
HV
77 struct typhoon *ty = kzalloc(sizeof(*ty), GFP_KERNEL);
78
79 return ty ? &ty->isa : NULL;
1da177e4
LT
80}
81
da1ff351 82static int typhoon_s_frequency(struct radio_isa_card *isa, u32 freq)
1da177e4
LT
83{
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
da1ff351 98 x = freq / 160;
1da177e4
LT
99 outval = (x * x + 2500) / 5000;
100 outval = (outval * x + 5000) / 10000;
101 outval -= (10 * x * x + 10433) / 20866;
102 outval += 4 * x - 11505;
103
da1ff351
HV
104 outb_p((outval >> 8) & 0x01, isa->io + 4);
105 outb_p(outval >> 9, isa->io + 6);
106 outb_p(outval & 0xff, isa->io + 8);
3f6892ac
DL
107 return 0;
108}
30c48305 109
da1ff351 110static int typhoon_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
3f6892ac 111{
da1ff351 112 struct typhoon *ty = container_of(isa, struct typhoon, isa);
30c48305 113
da1ff351
HV
114 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. */
30c48305 120
da1ff351
HV
121 if (vol == 0 && !ty->muted) {
122 ty->muted = true;
123 return typhoon_s_frequency(isa, mutefreq << 4);
3f6892ac 124 }
da1ff351
HV
125 if (vol && ty->muted) {
126 ty->muted = false;
127 return typhoon_s_frequency(isa, isa->freq);
3f6892ac 128 }
3f6892ac 129 return 0;
1da177e4
LT
130}
131
da1ff351
HV
132static 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,
1da177e4
LT
136};
137
da1ff351
HV
138static 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,
1da177e4
LT
158};
159
f1bfe89b 160static int __init typhoon_init(void)
0b9c2b7a 161{
da1ff351
HV
162 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;
f1bfe89b 168 }
da1ff351 169 return isa_register_driver(&typhoon_driver.driver, TYPHOON_MAX);
1da177e4
LT
170}
171
f1bfe89b 172static void __exit typhoon_exit(void)
1da177e4 173{
da1ff351 174 isa_unregister_driver(&typhoon_driver.driver);
1da177e4
LT
175}
176
da1ff351 177
1da177e4 178module_init(typhoon_init);
f1bfe89b 179module_exit(typhoon_exit);
1da177e4 180