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