1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/ioport.h>
28#include <linux/delay.h>
29#include <linux/videodev2.h>
30#include <linux/mutex.h>
31#include <linux/io.h>
32#include <linux/slab.h>
33#include <media/v4l2-ioctl.h>
34#include <media/v4l2-device.h>
35#include "radio-isa.h"
36
37
38
39
40
41MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>");
42MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
43MODULE_LICENSE("GPL");
44MODULE_VERSION("1.0.0");
45
46
47
48
49
50#ifndef CONFIG_RADIO_GEMTEK_PORT
51#define CONFIG_RADIO_GEMTEK_PORT -1
52#endif
53#ifndef CONFIG_RADIO_GEMTEK_PROBE
54#define CONFIG_RADIO_GEMTEK_PROBE 1
55#endif
56
57#define GEMTEK_MAX 4
58
59static bool probe = CONFIG_RADIO_GEMTEK_PROBE;
60static bool hardmute;
61static int io[GEMTEK_MAX] = { [0] = CONFIG_RADIO_GEMTEK_PORT,
62 [1 ... (GEMTEK_MAX - 1)] = -1 };
63static int radio_nr[GEMTEK_MAX] = { [0 ... (GEMTEK_MAX - 1)] = -1 };
64
65module_param(probe, bool, 0444);
66MODULE_PARM_DESC(probe, "Enable automatic device probing.");
67
68module_param(hardmute, bool, 0644);
69MODULE_PARM_DESC(hardmute, "Enable 'hard muting' by shutting down PLL, may "
70 "reduce static noise.");
71
72module_param_array(io, int, NULL, 0444);
73MODULE_PARM_DESC(io, "Force I/O ports for the GemTek Radio card if automatic "
74 "probing is disabled or fails. The most common I/O ports are: 0x20c "
75 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
76 "work for the combined sound/radiocard).");
77
78module_param_array(radio_nr, int, NULL, 0444);
79MODULE_PARM_DESC(radio_nr, "Radio device numbers");
80
81
82
83
84
85#define FSCALE 8
86#define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
87#define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
88
89#define GEMTEK_CK 0x01
90#define GEMTEK_DA 0x02
91#define GEMTEK_CE 0x04
92#define GEMTEK_NS 0x08
93#define GEMTEK_MT 0x10
94#define GEMTEK_STDF_3_125_KHZ 0x01
95#define GEMTEK_PLL_OFF 0x07
96
97#define BU2614_BUS_SIZE 32
98
99#define SHORT_DELAY 5
100#define LONG_DELAY 75
101
102struct gemtek {
103 struct radio_isa_card isa;
104 bool muted;
105 u32 bu2614data;
106};
107
108#define BU2614_FREQ_BITS 16
109#define BU2614_PORT_BITS 3
110#define BU2614_VOID_BITS 4
111#define BU2614_FMES_BITS 1
112#define BU2614_STDF_BITS 3
113#define BU2614_SWIN_BITS 1
114#define BU2614_SWAL_BITS 1
115#define BU2614_VOID2_BITS 1
116#define BU2614_FMUN_BITS 1
117#define BU2614_TEST_BITS 1
118
119#define BU2614_FREQ_SHIFT 0
120#define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
121#define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
122#define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
123#define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
124#define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
125#define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
126#define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
127#define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
128#define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
129
130#define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
131 BU2614_##field##_SHIFT)
132#define BU2614_PORT_MASK MKMASK(PORT)
133#define BU2614_FREQ_MASK MKMASK(FREQ)
134#define BU2614_VOID_MASK MKMASK(VOID)
135#define BU2614_FMES_MASK MKMASK(FMES)
136#define BU2614_STDF_MASK MKMASK(STDF)
137#define BU2614_SWIN_MASK MKMASK(SWIN)
138#define BU2614_SWAL_MASK MKMASK(SWAL)
139#define BU2614_VOID2_MASK MKMASK(VOID2)
140#define BU2614_FMUN_MASK MKMASK(FMUN)
141#define BU2614_TEST_MASK MKMASK(TEST)
142
143
144
145
146#define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
147 ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
148
149
150
151
152static void gemtek_bu2614_transmit(struct gemtek *gt)
153{
154 struct radio_isa_card *isa = >->isa;
155 int i, bit, q, mute;
156
157 mute = gt->muted ? GEMTEK_MT : 0x00;
158
159 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, isa->io);
160 udelay(LONG_DELAY);
161
162 for (i = 0, q = gt->bu2614data; i < 32; i++, q >>= 1) {
163 bit = (q & 1) ? GEMTEK_DA : 0;
164 outb_p(mute | GEMTEK_CE | bit, isa->io);
165 udelay(SHORT_DELAY);
166 outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, isa->io);
167 udelay(SHORT_DELAY);
168 }
169
170 outb_p(mute | GEMTEK_DA | GEMTEK_CK, isa->io);
171 udelay(SHORT_DELAY);
172}
173
174
175
176
177static unsigned long gemtek_convfreq(unsigned long freq)
178{
179 return ((freq << FSCALE) + IF_OFFSET + REF_FREQ / 2) / REF_FREQ;
180}
181
182static struct radio_isa_card *gemtek_alloc(void)
183{
184 struct gemtek *gt = kzalloc(sizeof(*gt), GFP_KERNEL);
185
186 if (gt)
187 gt->muted = true;
188 return gt ? >->isa : NULL;
189}
190
191
192
193
194static int gemtek_s_frequency(struct radio_isa_card *isa, u32 freq)
195{
196 struct gemtek *gt = container_of(isa, struct gemtek, isa);
197
198 if (hardmute && gt->muted)
199 return 0;
200
201 gemtek_bu2614_set(gt, BU2614_PORT, 0);
202 gemtek_bu2614_set(gt, BU2614_FMES, 0);
203 gemtek_bu2614_set(gt, BU2614_SWIN, 0);
204 gemtek_bu2614_set(gt, BU2614_SWAL, 0);
205 gemtek_bu2614_set(gt, BU2614_FMUN, 1);
206 gemtek_bu2614_set(gt, BU2614_TEST, 0);
207 gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
208 gemtek_bu2614_set(gt, BU2614_FREQ, gemtek_convfreq(freq));
209 gemtek_bu2614_transmit(gt);
210 return 0;
211}
212
213
214
215
216static int gemtek_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
217{
218 struct gemtek *gt = container_of(isa, struct gemtek, isa);
219 int i;
220
221 gt->muted = mute;
222 if (hardmute) {
223 if (!mute)
224 return gemtek_s_frequency(isa, isa->freq);
225
226
227 gemtek_bu2614_set(gt, BU2614_PORT, 0);
228 gemtek_bu2614_set(gt, BU2614_FMES, 0);
229 gemtek_bu2614_set(gt, BU2614_SWIN, 0);
230 gemtek_bu2614_set(gt, BU2614_SWAL, 0);
231 gemtek_bu2614_set(gt, BU2614_FMUN, 0);
232 gemtek_bu2614_set(gt, BU2614_TEST, 0);
233 gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_PLL_OFF);
234 gemtek_bu2614_set(gt, BU2614_FREQ, 0);
235 gemtek_bu2614_transmit(gt);
236 return 0;
237 }
238
239
240 i = inb_p(isa->io);
241
242 outb_p((i >> 5) | (mute ? GEMTEK_MT : 0), isa->io);
243 udelay(SHORT_DELAY);
244 return 0;
245}
246
247static u32 gemtek_g_rxsubchans(struct radio_isa_card *isa)
248{
249 if (inb_p(isa->io) & GEMTEK_NS)
250 return V4L2_TUNER_SUB_MONO;
251 return V4L2_TUNER_SUB_STEREO;
252}
253
254
255
256
257static bool gemtek_probe(struct radio_isa_card *isa, int io)
258{
259 int i, q;
260
261 q = inb_p(io);
262
263
264 for (i = 0; i < 3; ++i) {
265 outb_p(1 << i, io);
266 udelay(SHORT_DELAY);
267
268 if ((inb_p(io) & ~GEMTEK_NS) != (0x17 | (1 << (i + 5))))
269 return false;
270 }
271 outb_p(q >> 5, io);
272 udelay(SHORT_DELAY);
273 return true;
274}
275
276static const struct radio_isa_ops gemtek_ops = {
277 .alloc = gemtek_alloc,
278 .probe = gemtek_probe,
279 .s_mute_volume = gemtek_s_mute_volume,
280 .s_frequency = gemtek_s_frequency,
281 .g_rxsubchans = gemtek_g_rxsubchans,
282};
283
284static const int gemtek_ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
285
286static struct radio_isa_driver gemtek_driver = {
287 .driver = {
288 .match = radio_isa_match,
289 .probe = radio_isa_probe,
290 .remove = radio_isa_remove,
291 .driver = {
292 .name = "radio-gemtek",
293 },
294 },
295 .io_params = io,
296 .radio_nr_params = radio_nr,
297 .io_ports = gemtek_ioports,
298 .num_of_io_ports = ARRAY_SIZE(gemtek_ioports),
299 .region_size = 1,
300 .card = "GemTek Radio",
301 .ops = &gemtek_ops,
302 .has_stereo = true,
303};
304
305static int __init gemtek_init(void)
306{
307 gemtek_driver.probe = probe;
308 return isa_register_driver(&gemtek_driver.driver, GEMTEK_MAX);
309}
310
311static void __exit gemtek_exit(void)
312{
313 hardmute = 1;
314 isa_unregister_driver(&gemtek_driver.driver);
315}
316
317module_init(gemtek_init);
318module_exit(gemtek_exit);
319