18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * AimsLab RadioTrack (aka RadioVeveal) driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 1997 M. Kirkwood
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com>
88c2ecf20Sopenharmony_ci * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org>
98c2ecf20Sopenharmony_ci * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
108c2ecf20Sopenharmony_ci * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Notes on the hardware (reverse engineered from other peoples'
138c2ecf20Sopenharmony_ci * reverse engineering of AIMS' code :-)
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci *  The signal strength query is unsurprisingly inaccurate.  And it seems
188c2ecf20Sopenharmony_ci *  to indicate that (on my card, at least) the frequency setting isn't
198c2ecf20Sopenharmony_ci *  too great.  (I have to tune up .025MHz from what the freq should be
208c2ecf20Sopenharmony_ci *  to get a report that the thing is tuned.)
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci *  Volume control is (ugh) analogue:
238c2ecf20Sopenharmony_ci *   out(port, start_increasing_volume);
248c2ecf20Sopenharmony_ci *   wait(a_wee_while);
258c2ecf20Sopenharmony_ci *   out(port, stop_changing_the_volume);
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/module.h>	/* Modules			*/
318c2ecf20Sopenharmony_ci#include <linux/init.h>		/* Initdata			*/
328c2ecf20Sopenharmony_ci#include <linux/ioport.h>	/* request_region		*/
338c2ecf20Sopenharmony_ci#include <linux/delay.h>	/* msleep			*/
348c2ecf20Sopenharmony_ci#include <linux/videodev2.h>	/* kernel radio structs		*/
358c2ecf20Sopenharmony_ci#include <linux/io.h>		/* outb, outb_p			*/
368c2ecf20Sopenharmony_ci#include <linux/slab.h>
378c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
388c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h>
398c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
408c2ecf20Sopenharmony_ci#include "radio-isa.h"
418c2ecf20Sopenharmony_ci#include "lm7000.h"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciMODULE_AUTHOR("M. Kirkwood");
448c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
458c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
468c2ecf20Sopenharmony_ciMODULE_VERSION("1.0.0");
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#ifndef CONFIG_RADIO_RTRACK_PORT
498c2ecf20Sopenharmony_ci#define CONFIG_RADIO_RTRACK_PORT -1
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define RTRACK_MAX 2
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int io[RTRACK_MAX] = { [0] = CONFIG_RADIO_RTRACK_PORT,
558c2ecf20Sopenharmony_ci			      [1 ... (RTRACK_MAX - 1)] = -1 };
568c2ecf20Sopenharmony_cistatic int radio_nr[RTRACK_MAX]	= { [0 ... (RTRACK_MAX - 1)] = -1 };
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cimodule_param_array(io, int, NULL, 0444);
598c2ecf20Sopenharmony_ciMODULE_PARM_DESC(io, "I/O addresses of the RadioTrack card (0x20f or 0x30f)");
608c2ecf20Sopenharmony_cimodule_param_array(radio_nr, int, NULL, 0444);
618c2ecf20Sopenharmony_ciMODULE_PARM_DESC(radio_nr, "Radio device numbers");
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct rtrack {
648c2ecf20Sopenharmony_ci	struct radio_isa_card isa;
658c2ecf20Sopenharmony_ci	int curvol;
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic struct radio_isa_card *rtrack_alloc(void)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct rtrack *rt = kzalloc(sizeof(struct rtrack), GFP_KERNEL);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (rt)
738c2ecf20Sopenharmony_ci		rt->curvol = 0xff;
748c2ecf20Sopenharmony_ci	return rt ? &rt->isa : NULL;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define AIMS_BIT_TUN_CE		(1 << 0)
788c2ecf20Sopenharmony_ci#define AIMS_BIT_TUN_CLK	(1 << 1)
798c2ecf20Sopenharmony_ci#define AIMS_BIT_TUN_DATA	(1 << 2)
808c2ecf20Sopenharmony_ci#define AIMS_BIT_VOL_CE		(1 << 3)
818c2ecf20Sopenharmony_ci#define AIMS_BIT_TUN_STRQ	(1 << 4)
828c2ecf20Sopenharmony_ci/* bit 5 is not connected */
838c2ecf20Sopenharmony_ci#define AIMS_BIT_VOL_UP		(1 << 6)	/* active low */
848c2ecf20Sopenharmony_ci#define AIMS_BIT_VOL_DN		(1 << 7)	/* active low */
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic void rtrack_set_pins(void *handle, u8 pins)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct radio_isa_card *isa = handle;
898c2ecf20Sopenharmony_ci	struct rtrack *rt = container_of(isa, struct rtrack, isa);
908c2ecf20Sopenharmony_ci	u8 bits = AIMS_BIT_VOL_DN | AIMS_BIT_VOL_UP | AIMS_BIT_TUN_STRQ;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (!v4l2_ctrl_g_ctrl(rt->isa.mute))
938c2ecf20Sopenharmony_ci		bits |= AIMS_BIT_VOL_CE;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if (pins & LM7000_DATA)
968c2ecf20Sopenharmony_ci		bits |= AIMS_BIT_TUN_DATA;
978c2ecf20Sopenharmony_ci	if (pins & LM7000_CLK)
988c2ecf20Sopenharmony_ci		bits |= AIMS_BIT_TUN_CLK;
998c2ecf20Sopenharmony_ci	if (pins & LM7000_CE)
1008c2ecf20Sopenharmony_ci		bits |= AIMS_BIT_TUN_CE;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	outb_p(bits, rt->isa.io);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic int rtrack_s_frequency(struct radio_isa_card *isa, u32 freq)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	lm7000_set_freq(freq, isa, rtrack_set_pins);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	return 0;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic u32 rtrack_g_signal(struct radio_isa_card *isa)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	/* bit set = no signal present */
1158c2ecf20Sopenharmony_ci	return 0xffff * !(inb(isa->io) & 2);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int rtrack_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct rtrack *rt = container_of(isa, struct rtrack, isa);
1218c2ecf20Sopenharmony_ci	int curvol = rt->curvol;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (mute) {
1248c2ecf20Sopenharmony_ci		outb(0xd0, isa->io);	/* volume steady + sigstr + off	*/
1258c2ecf20Sopenharmony_ci		return 0;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci	if (vol == 0) {			/* volume = 0 means mute the card */
1288c2ecf20Sopenharmony_ci		outb(0x48, isa->io);	/* volume down but still "on"	*/
1298c2ecf20Sopenharmony_ci		msleep(curvol * 3);	/* make sure it's totally down	*/
1308c2ecf20Sopenharmony_ci	} else if (curvol < vol) {
1318c2ecf20Sopenharmony_ci		outb(0x98, isa->io);	/* volume up + sigstr + on	*/
1328c2ecf20Sopenharmony_ci		for (; curvol < vol; curvol++)
1338c2ecf20Sopenharmony_ci			mdelay(3);
1348c2ecf20Sopenharmony_ci	} else if (curvol > vol) {
1358c2ecf20Sopenharmony_ci		outb(0x58, isa->io);	/* volume down + sigstr + on	*/
1368c2ecf20Sopenharmony_ci		for (; curvol > vol; curvol--)
1378c2ecf20Sopenharmony_ci			mdelay(3);
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci	outb(0xd8, isa->io);		/* volume steady + sigstr + on	*/
1408c2ecf20Sopenharmony_ci	rt->curvol = vol;
1418c2ecf20Sopenharmony_ci	return 0;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/* Mute card - prevents noisy bootups */
1458c2ecf20Sopenharmony_cistatic int rtrack_initialize(struct radio_isa_card *isa)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	/* this ensures that the volume is all the way up  */
1488c2ecf20Sopenharmony_ci	outb(0x90, isa->io);	/* volume up but still "on"	*/
1498c2ecf20Sopenharmony_ci	msleep(3000);		/* make sure it's totally up	*/
1508c2ecf20Sopenharmony_ci	outb(0xc0, isa->io);	/* steady volume, mute card	*/
1518c2ecf20Sopenharmony_ci	return 0;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic const struct radio_isa_ops rtrack_ops = {
1558c2ecf20Sopenharmony_ci	.alloc = rtrack_alloc,
1568c2ecf20Sopenharmony_ci	.init = rtrack_initialize,
1578c2ecf20Sopenharmony_ci	.s_mute_volume = rtrack_s_mute_volume,
1588c2ecf20Sopenharmony_ci	.s_frequency = rtrack_s_frequency,
1598c2ecf20Sopenharmony_ci	.g_signal = rtrack_g_signal,
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic const int rtrack_ioports[] = { 0x20f, 0x30f };
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic struct radio_isa_driver rtrack_driver = {
1658c2ecf20Sopenharmony_ci	.driver = {
1668c2ecf20Sopenharmony_ci		.match		= radio_isa_match,
1678c2ecf20Sopenharmony_ci		.probe		= radio_isa_probe,
1688c2ecf20Sopenharmony_ci		.remove		= radio_isa_remove,
1698c2ecf20Sopenharmony_ci		.driver		= {
1708c2ecf20Sopenharmony_ci			.name	= "radio-aimslab",
1718c2ecf20Sopenharmony_ci		},
1728c2ecf20Sopenharmony_ci	},
1738c2ecf20Sopenharmony_ci	.io_params = io,
1748c2ecf20Sopenharmony_ci	.radio_nr_params = radio_nr,
1758c2ecf20Sopenharmony_ci	.io_ports = rtrack_ioports,
1768c2ecf20Sopenharmony_ci	.num_of_io_ports = ARRAY_SIZE(rtrack_ioports),
1778c2ecf20Sopenharmony_ci	.region_size = 2,
1788c2ecf20Sopenharmony_ci	.card = "AIMSlab RadioTrack/RadioReveal",
1798c2ecf20Sopenharmony_ci	.ops = &rtrack_ops,
1808c2ecf20Sopenharmony_ci	.has_stereo = true,
1818c2ecf20Sopenharmony_ci	.max_volume = 0xff,
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int __init rtrack_init(void)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	return isa_register_driver(&rtrack_driver.driver, RTRACK_MAX);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic void __exit rtrack_exit(void)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	isa_unregister_driver(&rtrack_driver.driver);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cimodule_init(rtrack_init);
1958c2ecf20Sopenharmony_cimodule_exit(rtrack_exit);
196