blob: 9d7fdaea46beda50fbb352520176c8b7f31cca78 [file] [log] [blame]
Hans Verkuilf8c08522012-01-16 05:00:26 -03001/*
2 * GemTek radio card driver
3 *
4 * Copyright 1998 Jonas Munsin <jmunsin@iki.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
Alan Coxd9b01442008-10-27 15:13:47 -030014 * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
16 *
Hans Verkuilf8c08522012-01-16 05:00:26 -030017 * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com>
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030018 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Hans Verkuilf8c08522012-01-16 05:00:26 -030019 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
25#include <linux/module.h> /* Modules */
26#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070027#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/delay.h> /* udelay */
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030029#include <linux/videodev2.h> /* kernel radio structs */
Hans Verkuil502b71b2009-03-06 13:50:42 -030030#include <linux/mutex.h>
31#include <linux/io.h> /* outb, outb_p */
Hans Verkuil502b71b2009-03-06 13:50:42 -030032#include <media/v4l2-ioctl.h>
33#include <media/v4l2-device.h>
Hans Verkuilf8c08522012-01-16 05:00:26 -030034#include "radio-isa.h"
Hans Verkuil502b71b2009-03-06 13:50:42 -030035
Pekka Seppanen47536472007-10-01 00:27:55 -030036/*
37 * Module info.
38 */
39
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030040MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>");
Pekka Seppanen47536472007-10-01 00:27:55 -030041MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
42MODULE_LICENSE("GPL");
Hans Verkuilf8c08522012-01-16 05:00:26 -030043MODULE_VERSION("1.0.0");
Pekka Seppanen47536472007-10-01 00:27:55 -030044
45/*
46 * Module params.
47 */
Mauro Carvalho Chehabd1c4ecd2006-08-08 09:10:01 -030048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifndef CONFIG_RADIO_GEMTEK_PORT
50#define CONFIG_RADIO_GEMTEK_PORT -1
51#endif
Pekka Seppanen47536472007-10-01 00:27:55 -030052#ifndef CONFIG_RADIO_GEMTEK_PROBE
53#define CONFIG_RADIO_GEMTEK_PROBE 1
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Hans Verkuilf8c08522012-01-16 05:00:26 -030056#define GEMTEK_MAX 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Hans Verkuilf8c08522012-01-16 05:00:26 -030058static bool probe = CONFIG_RADIO_GEMTEK_PROBE;
59static bool hardmute;
60static int io[GEMTEK_MAX] = { [0] = CONFIG_RADIO_GEMTEK_PORT,
61 [1 ... (GEMTEK_MAX - 1)] = -1 };
62static int radio_nr[GEMTEK_MAX] = { [0 ... (GEMTEK_MAX - 1)] = -1 };
63
64module_param(probe, bool, 0444);
65MODULE_PARM_DESC(probe, "Enable automatic device probing.");
66
67module_param(hardmute, bool, 0644);
68MODULE_PARM_DESC(hardmute, "Enable 'hard muting' by shutting down PLL, may "
69 "reduce static noise.");
70
71module_param_array(io, int, NULL, 0444);
72MODULE_PARM_DESC(io, "Force I/O ports for the GemTek Radio card if automatic "
Pekka Seppanen47536472007-10-01 00:27:55 -030073 "probing is disabled or fails. The most common I/O ports are: 0x20c "
74 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
Joe Perches13d97012007-11-19 22:48:16 -030075 "work for the combined sound/radiocard).");
Pekka Seppanen47536472007-10-01 00:27:55 -030076
Hans Verkuilf8c08522012-01-16 05:00:26 -030077module_param_array(radio_nr, int, NULL, 0444);
78MODULE_PARM_DESC(radio_nr, "Radio device numbers");
Pekka Seppanen47536472007-10-01 00:27:55 -030079
Trent Piepho857e5942007-10-01 00:32:25 -030080/*
81 * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
82 * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
83 */
84#define FSCALE 8
85#define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
86#define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
87
Pekka Seppanen47536472007-10-01 00:27:55 -030088#define GEMTEK_CK 0x01 /* Clock signal */
89#define GEMTEK_DA 0x02 /* Serial data */
90#define GEMTEK_CE 0x04 /* Chip enable */
91#define GEMTEK_NS 0x08 /* No signal */
92#define GEMTEK_MT 0x10 /* Line mute */
93#define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
94#define GEMTEK_PLL_OFF 0x07 /* PLL off */
95
96#define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
Pekka Seppanen47536472007-10-01 00:27:55 -030097
98#define SHORT_DELAY 5 /* usec */
99#define LONG_DELAY 75 /* usec */
100
Hans Verkuil502b71b2009-03-06 13:50:42 -0300101struct gemtek {
Hans Verkuilf8c08522012-01-16 05:00:26 -0300102 struct radio_isa_card isa;
103 bool muted;
Trent Piephob25be972007-10-01 00:38:30 -0300104 u32 bu2614data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Trent Piephob25be972007-10-01 00:38:30 -0300107#define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
108#define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
109#define BU2614_VOID_BITS 4 /* unused */
110#define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
111#define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
112#define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
113#define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
114#define BU2614_VOID2_BITS 1 /* unused */
115#define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
116#define BU2614_TEST_BITS 1 /* TS, Test data is input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Trent Piephob25be972007-10-01 00:38:30 -0300118#define BU2614_FREQ_SHIFT 0
119#define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
120#define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
121#define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
122#define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
123#define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
124#define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
125#define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
126#define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
127#define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
128
129#define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
130 BU2614_##field##_SHIFT)
131#define BU2614_PORT_MASK MKMASK(PORT)
132#define BU2614_FREQ_MASK MKMASK(FREQ)
133#define BU2614_VOID_MASK MKMASK(VOID)
134#define BU2614_FMES_MASK MKMASK(FMES)
135#define BU2614_STDF_MASK MKMASK(STDF)
136#define BU2614_SWIN_MASK MKMASK(SWIN)
137#define BU2614_SWAL_MASK MKMASK(SWAL)
138#define BU2614_VOID2_MASK MKMASK(VOID2)
139#define BU2614_FMUN_MASK MKMASK(FMUN)
140#define BU2614_TEST_MASK MKMASK(TEST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Pekka Seppanen47536472007-10-01 00:27:55 -0300142/*
143 * Set data which will be sent to BU2614FS.
144 */
Trent Piephob25be972007-10-01 00:38:30 -0300145#define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
146 ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
Pekka Seppanen47536472007-10-01 00:27:55 -0300147
148/*
149 * Transmit settings to BU2614FS over GemTek IC.
150 */
Hans Verkuil502b71b2009-03-06 13:50:42 -0300151static void gemtek_bu2614_transmit(struct gemtek *gt)
Pekka Seppanen47536472007-10-01 00:27:55 -0300152{
Hans Verkuilf8c08522012-01-16 05:00:26 -0300153 struct radio_isa_card *isa = &gt->isa;
Pekka Seppanen47536472007-10-01 00:27:55 -0300154 int i, bit, q, mute;
155
Hans Verkuil502b71b2009-03-06 13:50:42 -0300156 mute = gt->muted ? GEMTEK_MT : 0x00;
Pekka Seppanen47536472007-10-01 00:27:55 -0300157
Hans Verkuilf8c08522012-01-16 05:00:26 -0300158 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, isa->io);
Pekka Seppanen47536472007-10-01 00:27:55 -0300159 udelay(LONG_DELAY);
160
Hans Verkuil502b71b2009-03-06 13:50:42 -0300161 for (i = 0, q = gt->bu2614data; i < 32; i++, q >>= 1) {
162 bit = (q & 1) ? GEMTEK_DA : 0;
Hans Verkuilf8c08522012-01-16 05:00:26 -0300163 outb_p(mute | GEMTEK_CE | bit, isa->io);
Hans Verkuil502b71b2009-03-06 13:50:42 -0300164 udelay(SHORT_DELAY);
Hans Verkuilf8c08522012-01-16 05:00:26 -0300165 outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, isa->io);
Hans Verkuil502b71b2009-03-06 13:50:42 -0300166 udelay(SHORT_DELAY);
Pekka Seppanen47536472007-10-01 00:27:55 -0300167 }
168
Hans Verkuilf8c08522012-01-16 05:00:26 -0300169 outb_p(mute | GEMTEK_DA | GEMTEK_CK, isa->io);
Pekka Seppanen47536472007-10-01 00:27:55 -0300170 udelay(SHORT_DELAY);
Pekka Seppanen47536472007-10-01 00:27:55 -0300171}
172
173/*
Trent Piepho857e5942007-10-01 00:32:25 -0300174 * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
Pekka Seppanen47536472007-10-01 00:27:55 -0300175 */
Trent Piepho857e5942007-10-01 00:32:25 -0300176static unsigned long gemtek_convfreq(unsigned long freq)
Pekka Seppanen47536472007-10-01 00:27:55 -0300177{
Hans Verkuilf8c08522012-01-16 05:00:26 -0300178 return ((freq << FSCALE) + IF_OFFSET + REF_FREQ / 2) / REF_FREQ;
179}
180
181static struct radio_isa_card *gemtek_alloc(void)
182{
183 struct gemtek *gt = kzalloc(sizeof(*gt), GFP_KERNEL);
184
185 if (gt)
186 gt->muted = true;
187 return gt ? &gt->isa : NULL;
Pekka Seppanen47536472007-10-01 00:27:55 -0300188}
189
190/*
191 * Set FM-frequency.
192 */
Hans Verkuilf8c08522012-01-16 05:00:26 -0300193static int gemtek_s_frequency(struct radio_isa_card *isa, u32 freq)
Pekka Seppanen47536472007-10-01 00:27:55 -0300194{
Hans Verkuilf8c08522012-01-16 05:00:26 -0300195 struct gemtek *gt = container_of(isa, struct gemtek, isa);
Pekka Seppanen47536472007-10-01 00:27:55 -0300196
Hans Verkuilf8c08522012-01-16 05:00:26 -0300197 if (hardmute && gt->muted)
198 return 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300199
Hans Verkuil502b71b2009-03-06 13:50:42 -0300200 gemtek_bu2614_set(gt, BU2614_PORT, 0);
201 gemtek_bu2614_set(gt, BU2614_FMES, 0);
202 gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */
203 gemtek_bu2614_set(gt, BU2614_SWAL, 0);
204 gemtek_bu2614_set(gt, BU2614_FMUN, 1); /* GT bit set */
205 gemtek_bu2614_set(gt, BU2614_TEST, 0);
Hans Verkuil502b71b2009-03-06 13:50:42 -0300206 gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
207 gemtek_bu2614_set(gt, BU2614_FREQ, gemtek_convfreq(freq));
Hans Verkuil502b71b2009-03-06 13:50:42 -0300208 gemtek_bu2614_transmit(gt);
Hans Verkuilf8c08522012-01-16 05:00:26 -0300209 return 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300210}
211
212/*
213 * Set mute flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Hans Verkuilf8c08522012-01-16 05:00:26 -0300215static int gemtek_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Hans Verkuilf8c08522012-01-16 05:00:26 -0300217 struct gemtek *gt = container_of(isa, struct gemtek, isa);
Pekka Seppanen47536472007-10-01 00:27:55 -0300218 int i;
Hans Verkuil502b71b2009-03-06 13:50:42 -0300219
Hans Verkuilf8c08522012-01-16 05:00:26 -0300220 gt->muted = mute;
Pekka Seppanen47536472007-10-01 00:27:55 -0300221 if (hardmute) {
Hans Verkuilf8c08522012-01-16 05:00:26 -0300222 if (!mute)
223 return gemtek_s_frequency(isa, isa->freq);
224
Pekka Seppanen47536472007-10-01 00:27:55 -0300225 /* Turn off PLL, disable data output */
Hans Verkuil502b71b2009-03-06 13:50:42 -0300226 gemtek_bu2614_set(gt, BU2614_PORT, 0);
227 gemtek_bu2614_set(gt, BU2614_FMES, 0); /* CT bit off */
228 gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */
229 gemtek_bu2614_set(gt, BU2614_SWAL, 0);
230 gemtek_bu2614_set(gt, BU2614_FMUN, 0); /* GT bit off */
231 gemtek_bu2614_set(gt, BU2614_TEST, 0);
232 gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_PLL_OFF);
233 gemtek_bu2614_set(gt, BU2614_FREQ, 0);
234 gemtek_bu2614_transmit(gt);
Hans Verkuilf8c08522012-01-16 05:00:26 -0300235 return 0;
Pekka Seppanen47536472007-10-01 00:27:55 -0300236 }
Hans Verkuil502b71b2009-03-06 13:50:42 -0300237
Hans Verkuil502b71b2009-03-06 13:50:42 -0300238 /* Read bus contents (CE, CK and DA). */
Hans Verkuilf8c08522012-01-16 05:00:26 -0300239 i = inb_p(isa->io);
Hans Verkuil502b71b2009-03-06 13:50:42 -0300240 /* Write it back with mute flag set. */
Hans Verkuilf8c08522012-01-16 05:00:26 -0300241 outb_p((i >> 5) | (mute ? GEMTEK_MT : 0), isa->io);
Hans Verkuil502b71b2009-03-06 13:50:42 -0300242 udelay(SHORT_DELAY);
Hans Verkuilf8c08522012-01-16 05:00:26 -0300243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Hans Verkuilf8c08522012-01-16 05:00:26 -0300246static u32 gemtek_g_rxsubchans(struct radio_isa_card *isa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Hans Verkuilf8c08522012-01-16 05:00:26 -0300248 if (inb_p(isa->io) & GEMTEK_NS)
249 return V4L2_TUNER_SUB_MONO;
250 return V4L2_TUNER_SUB_STEREO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Pekka Seppanen47536472007-10-01 00:27:55 -0300253/*
254 * Check if requested card acts like GemTek Radio card.
255 */
Hans Verkuilf8c08522012-01-16 05:00:26 -0300256static bool gemtek_probe(struct radio_isa_card *isa, int io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Pekka Seppanen47536472007-10-01 00:27:55 -0300258 int i, q;
259
Hans Verkuilf8c08522012-01-16 05:00:26 -0300260 q = inb_p(io); /* Read bus contents before probing. */
Pekka Seppanen47536472007-10-01 00:27:55 -0300261 /* Try to turn on CE, CK and DA respectively and check if card responds
262 properly. */
263 for (i = 0; i < 3; ++i) {
Hans Verkuilf8c08522012-01-16 05:00:26 -0300264 outb_p(1 << i, io);
Pekka Seppanen47536472007-10-01 00:27:55 -0300265 udelay(SHORT_DELAY);
266
Hans Verkuilf8c08522012-01-16 05:00:26 -0300267 if ((inb_p(io) & ~GEMTEK_NS) != (0x17 | (1 << (i + 5))))
268 return false;
Pekka Seppanen47536472007-10-01 00:27:55 -0300269 }
Hans Verkuilf8c08522012-01-16 05:00:26 -0300270 outb_p(q >> 5, io); /* Write bus contents back. */
Pekka Seppanen47536472007-10-01 00:27:55 -0300271 udelay(SHORT_DELAY);
Hans Verkuilf8c08522012-01-16 05:00:26 -0300272 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Hans Verkuilf8c08522012-01-16 05:00:26 -0300275static const struct radio_isa_ops gemtek_ops = {
276 .alloc = gemtek_alloc,
277 .probe = gemtek_probe,
278 .s_mute_volume = gemtek_s_mute_volume,
279 .s_frequency = gemtek_s_frequency,
280 .g_rxsubchans = gemtek_g_rxsubchans,
Pekka Seppanen47536472007-10-01 00:27:55 -0300281};
282
Hans Verkuilf8c08522012-01-16 05:00:26 -0300283static const int gemtek_ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Hans Verkuilf8c08522012-01-16 05:00:26 -0300285static struct radio_isa_driver gemtek_driver = {
286 .driver = {
287 .match = radio_isa_match,
288 .probe = radio_isa_probe,
289 .remove = radio_isa_remove,
290 .driver = {
291 .name = "radio-gemtek",
292 },
293 },
294 .io_params = io,
295 .radio_nr_params = radio_nr,
296 .io_ports = gemtek_ioports,
297 .num_of_io_ports = ARRAY_SIZE(gemtek_ioports),
298 .region_size = 1,
299 .card = "GemTek Radio",
300 .ops = &gemtek_ops,
301 .has_stereo = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static int __init gemtek_init(void)
305{
Hans Verkuilf8c08522012-01-16 05:00:26 -0300306 gemtek_driver.probe = probe;
307 return isa_register_driver(&gemtek_driver.driver, GEMTEK_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Pekka Seppanen47536472007-10-01 00:27:55 -0300310static void __exit gemtek_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Hans Verkuilf8c08522012-01-16 05:00:26 -0300312 hardmute = 1; /* Turn off PLL */
313 isa_unregister_driver(&gemtek_driver.driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316module_init(gemtek_init);
Pekka Seppanen47536472007-10-01 00:27:55 -0300317module_exit(gemtek_exit);