Commit | Line | Data |
---|---|---|
f8c08524 HV |
1 | /* |
2 | * GemTek radio card driver | |
3 | * | |
4 | * Copyright 1998 Jonas Munsin <jmunsin@iki.fi> | |
1da177e4 LT |
5 | * |
6 | * GemTek hasn't released any specs on the card, so the protocol had to | |
7 | * be reverse engineered with dosemu. | |
8 | * | |
9 | * Besides the protocol changes, this is mostly a copy of: | |
10 | * | |
11 | * RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff | |
4286c6f6 | 12 | * |
1da177e4 | 13 | * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood |
d9b01449 | 14 | * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
15 | * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org> |
16 | * | |
f8c08524 | 17 | * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com> |
d1c4ecde | 18 | * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org> |
f8c08524 HV |
19 | * |
20 | * Note: this card seems to swap the left and right audio channels! | |
21 | * | |
22 | * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool. | |
1da177e4 LT |
23 | */ |
24 | ||
25 | #include <linux/module.h> /* Modules */ | |
26 | #include <linux/init.h> /* Initdata */ | |
fb911ee8 | 27 | #include <linux/ioport.h> /* request_region */ |
1da177e4 | 28 | #include <linux/delay.h> /* udelay */ |
d1c4ecde | 29 | #include <linux/videodev2.h> /* kernel radio structs */ |
502b71b5 HV |
30 | #include <linux/mutex.h> |
31 | #include <linux/io.h> /* outb, outb_p */ | |
38ed1aef | 32 | #include <linux/pnp.h> |
9f1dfccf | 33 | #include <linux/slab.h> |
35ea11ff | 34 | #include <media/v4l2-ioctl.h> |
502b71b5 | 35 | #include <media/v4l2-device.h> |
f8c08524 | 36 | #include "radio-isa.h" |
1da177e4 | 37 | |
4753647e PS |
38 | /* |
39 | * Module info. | |
40 | */ | |
41 | ||
29834c1a | 42 | MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>"); |
4753647e PS |
43 | MODULE_DESCRIPTION("A driver for the GemTek Radio card."); |
44 | MODULE_LICENSE("GPL"); | |
f8c08524 | 45 | MODULE_VERSION("1.0.0"); |
4753647e PS |
46 | |
47 | /* | |
48 | * Module params. | |
49 | */ | |
d1c4ecde | 50 | |
1da177e4 LT |
51 | #ifndef CONFIG_RADIO_GEMTEK_PORT |
52 | #define CONFIG_RADIO_GEMTEK_PORT -1 | |
53 | #endif | |
4753647e PS |
54 | #ifndef CONFIG_RADIO_GEMTEK_PROBE |
55 | #define CONFIG_RADIO_GEMTEK_PROBE 1 | |
56 | #endif | |
1da177e4 | 57 | |
f8c08524 | 58 | #define GEMTEK_MAX 4 |
1da177e4 | 59 | |
f8c08524 HV |
60 | static bool probe = CONFIG_RADIO_GEMTEK_PROBE; |
61 | static bool hardmute; | |
62 | static int io[GEMTEK_MAX] = { [0] = CONFIG_RADIO_GEMTEK_PORT, | |
63 | [1 ... (GEMTEK_MAX - 1)] = -1 }; | |
64 | static int radio_nr[GEMTEK_MAX] = { [0 ... (GEMTEK_MAX - 1)] = -1 }; | |
4753647e PS |
65 | |
66 | module_param(probe, bool, 0444); | |
f8c08524 | 67 | MODULE_PARM_DESC(probe, "Enable automatic device probing."); |
4753647e PS |
68 | |
69 | module_param(hardmute, bool, 0644); | |
f8c08524 | 70 | MODULE_PARM_DESC(hardmute, "Enable 'hard muting' by shutting down PLL, may " |
4753647e PS |
71 | "reduce static noise."); |
72 | ||
f8c08524 HV |
73 | module_param_array(io, int, NULL, 0444); |
74 | MODULE_PARM_DESC(io, "Force I/O ports for the GemTek Radio card if automatic " | |
75 | "probing is disabled or fails. The most common I/O ports are: 0x20c " | |
76 | "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to " | |
77 | "work for the combined sound/radiocard)."); | |
4753647e | 78 | |
f8c08524 HV |
79 | module_param_array(radio_nr, int, NULL, 0444); |
80 | MODULE_PARM_DESC(radio_nr, "Radio device numbers"); | |
4753647e | 81 | |
857e594a TP |
82 | /* |
83 | * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal | |
84 | * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz). | |
85 | */ | |
86 | #define FSCALE 8 | |
87 | #define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE))) | |
88 | #define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE))) | |
89 | ||
4753647e PS |
90 | #define GEMTEK_CK 0x01 /* Clock signal */ |
91 | #define GEMTEK_DA 0x02 /* Serial data */ | |
92 | #define GEMTEK_CE 0x04 /* Chip enable */ | |
93 | #define GEMTEK_NS 0x08 /* No signal */ | |
94 | #define GEMTEK_MT 0x10 /* Line mute */ | |
95 | #define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */ | |
96 | #define GEMTEK_PLL_OFF 0x07 /* PLL off */ | |
97 | ||
98 | #define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */ | |
4753647e PS |
99 | |
100 | #define SHORT_DELAY 5 /* usec */ | |
101 | #define LONG_DELAY 75 /* usec */ | |
102 | ||
502b71b5 | 103 | struct gemtek { |
f8c08524 HV |
104 | struct radio_isa_card isa; |
105 | bool muted; | |
b25be979 | 106 | u32 bu2614data; |
1da177e4 LT |
107 | }; |
108 | ||
b25be979 TP |
109 | #define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */ |
110 | #define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */ | |
111 | #define BU2614_VOID_BITS 4 /* unused */ | |
112 | #define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */ | |
113 | #define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */ | |
114 | #define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */ | |
115 | #define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/ | |
116 | #define BU2614_VOID2_BITS 1 /* unused */ | |
117 | #define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */ | |
118 | #define BU2614_TEST_BITS 1 /* TS, Test data is input */ | |
119 | ||
120 | #define BU2614_FREQ_SHIFT 0 | |
121 | #define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT) | |
122 | #define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT) | |
123 | #define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT) | |
124 | #define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT) | |
125 | #define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT) | |
126 | #define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT) | |
127 | #define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT) | |
128 | #define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT) | |
129 | #define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT) | |
130 | ||
131 | #define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \ | |
132 | BU2614_##field##_SHIFT) | |
133 | #define BU2614_PORT_MASK MKMASK(PORT) | |
134 | #define BU2614_FREQ_MASK MKMASK(FREQ) | |
135 | #define BU2614_VOID_MASK MKMASK(VOID) | |
136 | #define BU2614_FMES_MASK MKMASK(FMES) | |
137 | #define BU2614_STDF_MASK MKMASK(STDF) | |
138 | #define BU2614_SWIN_MASK MKMASK(SWIN) | |
139 | #define BU2614_SWAL_MASK MKMASK(SWAL) | |
140 | #define BU2614_VOID2_MASK MKMASK(VOID2) | |
141 | #define BU2614_FMUN_MASK MKMASK(FMUN) | |
142 | #define BU2614_TEST_MASK MKMASK(TEST) | |
4753647e | 143 | |
4753647e PS |
144 | /* |
145 | * Set data which will be sent to BU2614FS. | |
1da177e4 | 146 | */ |
b25be979 TP |
147 | #define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \ |
148 | ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT)) | |
4753647e PS |
149 | |
150 | /* | |
151 | * Transmit settings to BU2614FS over GemTek IC. | |
152 | */ | |
502b71b5 | 153 | static void gemtek_bu2614_transmit(struct gemtek *gt) |
4753647e | 154 | { |
f8c08524 | 155 | struct radio_isa_card *isa = >->isa; |
4753647e PS |
156 | int i, bit, q, mute; |
157 | ||
502b71b5 | 158 | mute = gt->muted ? GEMTEK_MT : 0x00; |
4753647e | 159 | |
f8c08524 | 160 | outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, isa->io); |
4753647e PS |
161 | udelay(LONG_DELAY); |
162 | ||
502b71b5 HV |
163 | for (i = 0, q = gt->bu2614data; i < 32; i++, q >>= 1) { |
164 | bit = (q & 1) ? GEMTEK_DA : 0; | |
f8c08524 | 165 | outb_p(mute | GEMTEK_CE | bit, isa->io); |
502b71b5 | 166 | udelay(SHORT_DELAY); |
f8c08524 | 167 | outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, isa->io); |
502b71b5 | 168 | udelay(SHORT_DELAY); |
4753647e PS |
169 | } |
170 | ||
f8c08524 | 171 | outb_p(mute | GEMTEK_DA | GEMTEK_CK, isa->io); |
4753647e | 172 | udelay(SHORT_DELAY); |
1da177e4 LT |
173 | } |
174 | ||
4753647e | 175 | /* |
857e594a | 176 | * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected). |
4753647e | 177 | */ |
857e594a | 178 | static unsigned long gemtek_convfreq(unsigned long freq) |
1da177e4 | 179 | { |
f8c08524 HV |
180 | return ((freq << FSCALE) + IF_OFFSET + REF_FREQ / 2) / REF_FREQ; |
181 | } | |
182 | ||
183 | static struct radio_isa_card *gemtek_alloc(void) | |
184 | { | |
185 | struct gemtek *gt = kzalloc(sizeof(*gt), GFP_KERNEL); | |
186 | ||
187 | if (gt) | |
188 | gt->muted = true; | |
189 | return gt ? >->isa : NULL; | |
4753647e PS |
190 | } |
191 | ||
192 | /* | |
193 | * Set FM-frequency. | |
194 | */ | |
f8c08524 | 195 | static int gemtek_s_frequency(struct radio_isa_card *isa, u32 freq) |
4753647e | 196 | { |
f8c08524 | 197 | struct gemtek *gt = container_of(isa, struct gemtek, isa); |
4753647e | 198 | |
f8c08524 HV |
199 | if (hardmute && gt->muted) |
200 | return 0; | |
4753647e | 201 | |
502b71b5 HV |
202 | gemtek_bu2614_set(gt, BU2614_PORT, 0); |
203 | gemtek_bu2614_set(gt, BU2614_FMES, 0); | |
204 | gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */ | |
205 | gemtek_bu2614_set(gt, BU2614_SWAL, 0); | |
206 | gemtek_bu2614_set(gt, BU2614_FMUN, 1); /* GT bit set */ | |
207 | gemtek_bu2614_set(gt, BU2614_TEST, 0); | |
502b71b5 HV |
208 | gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_STDF_3_125_KHZ); |
209 | gemtek_bu2614_set(gt, BU2614_FREQ, gemtek_convfreq(freq)); | |
502b71b5 | 210 | gemtek_bu2614_transmit(gt); |
f8c08524 | 211 | return 0; |
1da177e4 LT |
212 | } |
213 | ||
4753647e PS |
214 | /* |
215 | * Set mute flag. | |
216 | */ | |
f8c08524 | 217 | static int gemtek_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol) |
1da177e4 | 218 | { |
f8c08524 | 219 | struct gemtek *gt = container_of(isa, struct gemtek, isa); |
4753647e | 220 | int i; |
502b71b5 | 221 | |
f8c08524 | 222 | gt->muted = mute; |
4753647e | 223 | if (hardmute) { |
f8c08524 HV |
224 | if (!mute) |
225 | return gemtek_s_frequency(isa, isa->freq); | |
226 | ||
4753647e | 227 | /* Turn off PLL, disable data output */ |
502b71b5 HV |
228 | gemtek_bu2614_set(gt, BU2614_PORT, 0); |
229 | gemtek_bu2614_set(gt, BU2614_FMES, 0); /* CT bit off */ | |
230 | gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */ | |
231 | gemtek_bu2614_set(gt, BU2614_SWAL, 0); | |
232 | gemtek_bu2614_set(gt, BU2614_FMUN, 0); /* GT bit off */ | |
233 | gemtek_bu2614_set(gt, BU2614_TEST, 0); | |
234 | gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_PLL_OFF); | |
235 | gemtek_bu2614_set(gt, BU2614_FREQ, 0); | |
236 | gemtek_bu2614_transmit(gt); | |
f8c08524 | 237 | return 0; |
502b71b5 | 238 | } |
4753647e | 239 | |
502b71b5 | 240 | /* Read bus contents (CE, CK and DA). */ |
f8c08524 | 241 | i = inb_p(isa->io); |
502b71b5 | 242 | /* Write it back with mute flag set. */ |
f8c08524 | 243 | outb_p((i >> 5) | (mute ? GEMTEK_MT : 0), isa->io); |
502b71b5 | 244 | udelay(SHORT_DELAY); |
f8c08524 | 245 | return 0; |
1da177e4 LT |
246 | } |
247 | ||
f8c08524 | 248 | static u32 gemtek_g_rxsubchans(struct radio_isa_card *isa) |
1da177e4 | 249 | { |
f8c08524 HV |
250 | if (inb_p(isa->io) & GEMTEK_NS) |
251 | return V4L2_TUNER_SUB_MONO; | |
252 | return V4L2_TUNER_SUB_STEREO; | |
4753647e | 253 | } |
1da177e4 | 254 | |
4753647e PS |
255 | /* |
256 | * Check if requested card acts like GemTek Radio card. | |
257 | */ | |
f8c08524 | 258 | static bool gemtek_probe(struct radio_isa_card *isa, int io) |
4753647e | 259 | { |
4753647e | 260 | int i, q; |
1da177e4 | 261 | |
f8c08524 | 262 | q = inb_p(io); /* Read bus contents before probing. */ |
4753647e PS |
263 | /* Try to turn on CE, CK and DA respectively and check if card responds |
264 | properly. */ | |
265 | for (i = 0; i < 3; ++i) { | |
f8c08524 | 266 | outb_p(1 << i, io); |
4753647e | 267 | udelay(SHORT_DELAY); |
1da177e4 | 268 | |
f8c08524 HV |
269 | if ((inb_p(io) & ~GEMTEK_NS) != (0x17 | (1 << (i + 5)))) |
270 | return false; | |
4753647e | 271 | } |
f8c08524 | 272 | outb_p(q >> 5, io); /* Write bus contents back. */ |
4753647e | 273 | udelay(SHORT_DELAY); |
f8c08524 | 274 | return true; |
1da177e4 LT |
275 | } |
276 | ||
f8c08524 HV |
277 | static const struct radio_isa_ops gemtek_ops = { |
278 | .alloc = gemtek_alloc, | |
279 | .probe = gemtek_probe, | |
280 | .s_mute_volume = gemtek_s_mute_volume, | |
281 | .s_frequency = gemtek_s_frequency, | |
282 | .g_rxsubchans = gemtek_g_rxsubchans, | |
4753647e PS |
283 | }; |
284 | ||
f8c08524 HV |
285 | static const int gemtek_ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c }; |
286 | ||
38ed1aef OZ |
287 | #ifdef CONFIG_PNP |
288 | static struct pnp_device_id gemtek_pnp_devices[] = { | |
289 | /* AOpen FX-3D/Pro Radio */ | |
290 | {.id = "ADS7183", .driver_data = 0}, | |
291 | {.id = ""} | |
292 | }; | |
293 | ||
294 | MODULE_DEVICE_TABLE(pnp, gemtek_pnp_devices); | |
295 | #endif | |
296 | ||
f8c08524 HV |
297 | static struct radio_isa_driver gemtek_driver = { |
298 | .driver = { | |
299 | .match = radio_isa_match, | |
300 | .probe = radio_isa_probe, | |
301 | .remove = radio_isa_remove, | |
302 | .driver = { | |
303 | .name = "radio-gemtek", | |
304 | }, | |
305 | }, | |
38ed1aef OZ |
306 | #ifdef CONFIG_PNP |
307 | .pnp_driver = { | |
308 | .name = "radio-gemtek", | |
309 | .id_table = gemtek_pnp_devices, | |
310 | .probe = radio_isa_pnp_probe, | |
311 | .remove = radio_isa_pnp_remove, | |
312 | }, | |
313 | #endif | |
f8c08524 HV |
314 | .io_params = io, |
315 | .radio_nr_params = radio_nr, | |
316 | .io_ports = gemtek_ioports, | |
317 | .num_of_io_ports = ARRAY_SIZE(gemtek_ioports), | |
318 | .region_size = 1, | |
319 | .card = "GemTek Radio", | |
320 | .ops = &gemtek_ops, | |
321 | .has_stereo = true, | |
1da177e4 LT |
322 | }; |
323 | ||
1da177e4 LT |
324 | static int __init gemtek_init(void) |
325 | { | |
f8c08524 | 326 | gemtek_driver.probe = probe; |
38ed1aef OZ |
327 | #ifdef CONFIG_PNP |
328 | pnp_register_driver(&gemtek_driver.pnp_driver); | |
329 | #endif | |
f8c08524 | 330 | return isa_register_driver(&gemtek_driver.driver, GEMTEK_MAX); |
1da177e4 LT |
331 | } |
332 | ||
4753647e | 333 | static void __exit gemtek_exit(void) |
1da177e4 | 334 | { |
8b4b6818 | 335 | hardmute = true; /* Turn off PLL */ |
38ed1aef OZ |
336 | #ifdef CONFIG_PNP |
337 | pnp_unregister_driver(&gemtek_driver.pnp_driver); | |
338 | #endif | |
f8c08524 | 339 | isa_unregister_driver(&gemtek_driver.driver); |
1da177e4 LT |
340 | } |
341 | ||
342 | module_init(gemtek_init); | |
4753647e | 343 | module_exit(gemtek_exit); |