1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * AimsLab RadioTrack (aka RadioVeveal) driver
4 *
5 * Copyright 1997 M. Kirkwood
6 *
7 * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com>
8 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org>
9 * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
10 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
11 *
12 * Notes on the hardware (reverse engineered from other peoples'
13 * reverse engineering of AIMS' code :-)
14 *
15 *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
16 *
17 *  The signal strength query is unsurprisingly inaccurate.  And it seems
18 *  to indicate that (on my card, at least) the frequency setting isn't
19 *  too great.  (I have to tune up .025MHz from what the freq should be
20 *  to get a report that the thing is tuned.)
21 *
22 *  Volume control is (ugh) analogue:
23 *   out(port, start_increasing_volume);
24 *   wait(a_wee_while);
25 *   out(port, stop_changing_the_volume);
26 *
27 * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
28 */
29
30#include <linux/module.h>	/* Modules			*/
31#include <linux/init.h>		/* Initdata			*/
32#include <linux/ioport.h>	/* request_region		*/
33#include <linux/delay.h>	/* msleep			*/
34#include <linux/videodev2.h>	/* kernel radio structs		*/
35#include <linux/io.h>		/* outb, outb_p			*/
36#include <linux/slab.h>
37#include <media/v4l2-device.h>
38#include <media/v4l2-ioctl.h>
39#include <media/v4l2-ctrls.h>
40#include "radio-isa.h"
41#include "lm7000.h"
42
43MODULE_AUTHOR("M. Kirkwood");
44MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
45MODULE_LICENSE("GPL");
46MODULE_VERSION("1.0.0");
47
48#ifndef CONFIG_RADIO_RTRACK_PORT
49#define CONFIG_RADIO_RTRACK_PORT -1
50#endif
51
52#define RTRACK_MAX 2
53
54static int io[RTRACK_MAX] = { [0] = CONFIG_RADIO_RTRACK_PORT,
55			      [1 ... (RTRACK_MAX - 1)] = -1 };
56static int radio_nr[RTRACK_MAX]	= { [0 ... (RTRACK_MAX - 1)] = -1 };
57
58module_param_array(io, int, NULL, 0444);
59MODULE_PARM_DESC(io, "I/O addresses of the RadioTrack card (0x20f or 0x30f)");
60module_param_array(radio_nr, int, NULL, 0444);
61MODULE_PARM_DESC(radio_nr, "Radio device numbers");
62
63struct rtrack {
64	struct radio_isa_card isa;
65	int curvol;
66};
67
68static struct radio_isa_card *rtrack_alloc(void)
69{
70	struct rtrack *rt = kzalloc(sizeof(struct rtrack), GFP_KERNEL);
71
72	if (rt)
73		rt->curvol = 0xff;
74	return rt ? &rt->isa : NULL;
75}
76
77#define AIMS_BIT_TUN_CE		(1 << 0)
78#define AIMS_BIT_TUN_CLK	(1 << 1)
79#define AIMS_BIT_TUN_DATA	(1 << 2)
80#define AIMS_BIT_VOL_CE		(1 << 3)
81#define AIMS_BIT_TUN_STRQ	(1 << 4)
82/* bit 5 is not connected */
83#define AIMS_BIT_VOL_UP		(1 << 6)	/* active low */
84#define AIMS_BIT_VOL_DN		(1 << 7)	/* active low */
85
86static void rtrack_set_pins(void *handle, u8 pins)
87{
88	struct radio_isa_card *isa = handle;
89	struct rtrack *rt = container_of(isa, struct rtrack, isa);
90	u8 bits = AIMS_BIT_VOL_DN | AIMS_BIT_VOL_UP | AIMS_BIT_TUN_STRQ;
91
92	if (!v4l2_ctrl_g_ctrl(rt->isa.mute))
93		bits |= AIMS_BIT_VOL_CE;
94
95	if (pins & LM7000_DATA)
96		bits |= AIMS_BIT_TUN_DATA;
97	if (pins & LM7000_CLK)
98		bits |= AIMS_BIT_TUN_CLK;
99	if (pins & LM7000_CE)
100		bits |= AIMS_BIT_TUN_CE;
101
102	outb_p(bits, rt->isa.io);
103}
104
105static int rtrack_s_frequency(struct radio_isa_card *isa, u32 freq)
106{
107	lm7000_set_freq(freq, isa, rtrack_set_pins);
108
109	return 0;
110}
111
112static u32 rtrack_g_signal(struct radio_isa_card *isa)
113{
114	/* bit set = no signal present */
115	return 0xffff * !(inb(isa->io) & 2);
116}
117
118static int rtrack_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
119{
120	struct rtrack *rt = container_of(isa, struct rtrack, isa);
121	int curvol = rt->curvol;
122
123	if (mute) {
124		outb(0xd0, isa->io);	/* volume steady + sigstr + off	*/
125		return 0;
126	}
127	if (vol == 0) {			/* volume = 0 means mute the card */
128		outb(0x48, isa->io);	/* volume down but still "on"	*/
129		msleep(curvol * 3);	/* make sure it's totally down	*/
130	} else if (curvol < vol) {
131		outb(0x98, isa->io);	/* volume up + sigstr + on	*/
132		for (; curvol < vol; curvol++)
133			mdelay(3);
134	} else if (curvol > vol) {
135		outb(0x58, isa->io);	/* volume down + sigstr + on	*/
136		for (; curvol > vol; curvol--)
137			mdelay(3);
138	}
139	outb(0xd8, isa->io);		/* volume steady + sigstr + on	*/
140	rt->curvol = vol;
141	return 0;
142}
143
144/* Mute card - prevents noisy bootups */
145static int rtrack_initialize(struct radio_isa_card *isa)
146{
147	/* this ensures that the volume is all the way up  */
148	outb(0x90, isa->io);	/* volume up but still "on"	*/
149	msleep(3000);		/* make sure it's totally up	*/
150	outb(0xc0, isa->io);	/* steady volume, mute card	*/
151	return 0;
152}
153
154static const struct radio_isa_ops rtrack_ops = {
155	.alloc = rtrack_alloc,
156	.init = rtrack_initialize,
157	.s_mute_volume = rtrack_s_mute_volume,
158	.s_frequency = rtrack_s_frequency,
159	.g_signal = rtrack_g_signal,
160};
161
162static const int rtrack_ioports[] = { 0x20f, 0x30f };
163
164static struct radio_isa_driver rtrack_driver = {
165	.driver = {
166		.match		= radio_isa_match,
167		.probe		= radio_isa_probe,
168		.remove		= radio_isa_remove,
169		.driver		= {
170			.name	= "radio-aimslab",
171		},
172	},
173	.io_params = io,
174	.radio_nr_params = radio_nr,
175	.io_ports = rtrack_ioports,
176	.num_of_io_ports = ARRAY_SIZE(rtrack_ioports),
177	.region_size = 2,
178	.card = "AIMSlab RadioTrack/RadioReveal",
179	.ops = &rtrack_ops,
180	.has_stereo = true,
181	.max_volume = 0xff,
182};
183
184static int __init rtrack_init(void)
185{
186	return isa_register_driver(&rtrack_driver.driver, RTRACK_MAX);
187}
188
189static void __exit rtrack_exit(void)
190{
191	isa_unregister_driver(&rtrack_driver.driver);
192}
193
194module_init(rtrack_init);
195module_exit(rtrack_exit);
196