109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23088fba8SHans Verkuil /*
33088fba8SHans Verkuil  * radio-aztech.c - Aztech radio card driver
41da177e4SLinus Torvalds  *
53088fba8SHans Verkuil  * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@xs4all.nl>
632590819SMauro Carvalho Chehab  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org>
71da177e4SLinus Torvalds  * Adapted to support the Video for Linux API by
81da177e4SLinus Torvalds  * Russell Kroll <rkroll@exploits.org>.  Based on original tuner code by:
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Quay Ly
111da177e4SLinus Torvalds  * Donald Song
121da177e4SLinus Torvalds  * Jason Lewis      (jlewis@twilight.vtc.vsc.edu)
131da177e4SLinus Torvalds  * Scott McGrath    (smcgrath@twilight.vtc.vsc.edu)
141da177e4SLinus Torvalds  * William McGrath  (wmcgrath@twilight.vtc.vsc.edu)
151da177e4SLinus Torvalds  *
163088fba8SHans Verkuil  * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
171da177e4SLinus Torvalds */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/module.h>	/* Modules			*/
201da177e4SLinus Torvalds #include <linux/init.h>		/* Initdata			*/
21fb911ee8SPeter Osterlund #include <linux/ioport.h>	/* request_region		*/
221da177e4SLinus Torvalds #include <linux/delay.h>	/* udelay			*/
23a4366af4SMauro Carvalho Chehab #include <linux/videodev2.h>	/* kernel radio structs		*/
24e697e12eSHans Verkuil #include <linux/io.h>		/* outb, outb_p			*/
259f1dfccfSHans Verkuil #include <linux/slab.h>
26e697e12eSHans Verkuil #include <media/v4l2-device.h>
2735ea11ffSHans Verkuil #include <media/v4l2-ioctl.h>
283088fba8SHans Verkuil #include <media/v4l2-ctrls.h>
293088fba8SHans Verkuil #include "radio-isa.h"
30eb27fafeSOndrej Zary #include "lm7000.h"
311da177e4SLinus Torvalds 
32e697e12eSHans Verkuil MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
33e697e12eSHans Verkuil MODULE_DESCRIPTION("A driver for the Aztech radio card.");
34e697e12eSHans Verkuil MODULE_LICENSE("GPL");
353088fba8SHans Verkuil MODULE_VERSION("1.0.0");
36a4366af4SMauro Carvalho Chehab 
371da177e4SLinus Torvalds /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
381da177e4SLinus Torvalds #ifndef CONFIG_RADIO_AZTECH_PORT
391da177e4SLinus Torvalds #define CONFIG_RADIO_AZTECH_PORT -1
401da177e4SLinus Torvalds #endif
411da177e4SLinus Torvalds 
423088fba8SHans Verkuil #define AZTECH_MAX 2
431da177e4SLinus Torvalds 
443088fba8SHans Verkuil static int io[AZTECH_MAX] = { [0] = CONFIG_RADIO_AZTECH_PORT,
453088fba8SHans Verkuil 			      [1 ... (AZTECH_MAX - 1)] = -1 };
463088fba8SHans Verkuil static int radio_nr[AZTECH_MAX]	= { [0 ... (AZTECH_MAX - 1)] = -1 };
47e697e12eSHans Verkuil 
483088fba8SHans Verkuil module_param_array(io, int, NULL, 0444);
493088fba8SHans Verkuil MODULE_PARM_DESC(io, "I/O addresses of the Aztech card (0x350 or 0x358)");
503088fba8SHans Verkuil module_param_array(radio_nr, int, NULL, 0444);
513088fba8SHans Verkuil MODULE_PARM_DESC(radio_nr, "Radio device numbers");
523088fba8SHans Verkuil 
533088fba8SHans Verkuil struct aztech {
543088fba8SHans Verkuil 	struct radio_isa_card isa;
551da177e4SLinus Torvalds 	int curvol;
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
58eb27fafeSOndrej Zary /* bit definitions for register read */
59eb27fafeSOndrej Zary #define AZTECH_BIT_NOT_TUNED	(1 << 0)
60eb27fafeSOndrej Zary #define AZTECH_BIT_MONO		(1 << 1)
61eb27fafeSOndrej Zary /* bit definitions for register write */
62eb27fafeSOndrej Zary #define AZTECH_BIT_TUN_CE	(1 << 1)
63eb27fafeSOndrej Zary #define AZTECH_BIT_TUN_CLK	(1 << 6)
64eb27fafeSOndrej Zary #define AZTECH_BIT_TUN_DATA	(1 << 7)
65eb27fafeSOndrej Zary /* bits 0 and 2 are volume control, bits 3..5 are not connected */
661da177e4SLinus Torvalds 
aztech_set_pins(void * handle,u8 pins)67eb27fafeSOndrej Zary static void aztech_set_pins(void *handle, u8 pins)
681da177e4SLinus Torvalds {
69eb27fafeSOndrej Zary 	struct radio_isa_card *isa = handle;
70eb27fafeSOndrej Zary 	struct aztech *az = container_of(isa, struct aztech, isa);
71eb27fafeSOndrej Zary 	u8 bits = az->curvol;
72eb27fafeSOndrej Zary 
73eb27fafeSOndrej Zary 	if (pins & LM7000_DATA)
74eb27fafeSOndrej Zary 		bits |= AZTECH_BIT_TUN_DATA;
75eb27fafeSOndrej Zary 	if (pins & LM7000_CLK)
76eb27fafeSOndrej Zary 		bits |= AZTECH_BIT_TUN_CLK;
77eb27fafeSOndrej Zary 	if (pins & LM7000_CE)
78eb27fafeSOndrej Zary 		bits |= AZTECH_BIT_TUN_CE;
79eb27fafeSOndrej Zary 
80eb27fafeSOndrej Zary 	outb_p(bits, az->isa.io);
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
aztech_alloc(void)833088fba8SHans Verkuil static struct radio_isa_card *aztech_alloc(void)
841da177e4SLinus Torvalds {
853088fba8SHans Verkuil 	struct aztech *az = kzalloc(sizeof(*az), GFP_KERNEL);
863088fba8SHans Verkuil 
873088fba8SHans Verkuil 	return az ? &az->isa : NULL;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
aztech_s_frequency(struct radio_isa_card * isa,u32 freq)903088fba8SHans Verkuil static int aztech_s_frequency(struct radio_isa_card *isa, u32 freq)
911da177e4SLinus Torvalds {
92eb27fafeSOndrej Zary 	lm7000_set_freq(freq, isa, aztech_set_pins);
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	return 0;
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
aztech_g_rxsubchans(struct radio_isa_card * isa)973088fba8SHans Verkuil static u32 aztech_g_rxsubchans(struct radio_isa_card *isa)
981da177e4SLinus Torvalds {
991c9f11edSOndrej Zary 	if (inb(isa->io) & AZTECH_BIT_MONO)
1003088fba8SHans Verkuil 		return V4L2_TUNER_SUB_MONO;
1013088fba8SHans Verkuil 	return V4L2_TUNER_SUB_STEREO;
1023088fba8SHans Verkuil }
1033088fba8SHans Verkuil 
aztech_g_signal(struct radio_isa_card * isa)1041c9f11edSOndrej Zary static u32 aztech_g_signal(struct radio_isa_card *isa)
1053088fba8SHans Verkuil {
1061c9f11edSOndrej Zary 	return (inb(isa->io) & AZTECH_BIT_NOT_TUNED) ? 0 : 0xffff;
1073088fba8SHans Verkuil }
1083088fba8SHans Verkuil 
aztech_s_mute_volume(struct radio_isa_card * isa,bool mute,int vol)1093088fba8SHans Verkuil static int aztech_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
1103088fba8SHans Verkuil {
1113088fba8SHans Verkuil 	struct aztech *az = container_of(isa, struct aztech, isa);
1123088fba8SHans Verkuil 
1133088fba8SHans Verkuil 	if (mute)
1143088fba8SHans Verkuil 		vol = 0;
1153088fba8SHans Verkuil 	az->curvol = (vol & 1) + ((vol & 2) << 1);
1163088fba8SHans Verkuil 	outb(az->curvol, isa->io);
1171da177e4SLinus Torvalds 	return 0;
1181da177e4SLinus Torvalds }
11999218fe4SMauro Carvalho Chehab 
1203088fba8SHans Verkuil static const struct radio_isa_ops aztech_ops = {
1213088fba8SHans Verkuil 	.alloc = aztech_alloc,
1223088fba8SHans Verkuil 	.s_mute_volume = aztech_s_mute_volume,
1233088fba8SHans Verkuil 	.s_frequency = aztech_s_frequency,
1243088fba8SHans Verkuil 	.g_rxsubchans = aztech_g_rxsubchans,
1251c9f11edSOndrej Zary 	.g_signal = aztech_g_signal,
1261da177e4SLinus Torvalds };
1271da177e4SLinus Torvalds 
1283088fba8SHans Verkuil static const int aztech_ioports[] = { 0x350, 0x358 };
1293088fba8SHans Verkuil 
1303088fba8SHans Verkuil static struct radio_isa_driver aztech_driver = {
1313088fba8SHans Verkuil 	.driver = {
1323088fba8SHans Verkuil 		.match		= radio_isa_match,
1333088fba8SHans Verkuil 		.probe		= radio_isa_probe,
1343088fba8SHans Verkuil 		.remove		= radio_isa_remove,
1353088fba8SHans Verkuil 		.driver		= {
1363088fba8SHans Verkuil 			.name	= "radio-aztech",
1373088fba8SHans Verkuil 		},
1383088fba8SHans Verkuil 	},
1393088fba8SHans Verkuil 	.io_params = io,
1403088fba8SHans Verkuil 	.radio_nr_params = radio_nr,
1413088fba8SHans Verkuil 	.io_ports = aztech_ioports,
1423088fba8SHans Verkuil 	.num_of_io_ports = ARRAY_SIZE(aztech_ioports),
143eb27fafeSOndrej Zary 	.region_size = 8,
1443088fba8SHans Verkuil 	.card = "Aztech Radio",
1453088fba8SHans Verkuil 	.ops = &aztech_ops,
1463088fba8SHans Verkuil 	.has_stereo = true,
1473088fba8SHans Verkuil 	.max_volume = 3,
1481da177e4SLinus Torvalds };
1491da177e4SLinus Torvalds 
aztech_init(void)1501da177e4SLinus Torvalds static int __init aztech_init(void)
1511da177e4SLinus Torvalds {
1523088fba8SHans Verkuil 	return isa_register_driver(&aztech_driver.driver, AZTECH_MAX);
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
aztech_exit(void)155e697e12eSHans Verkuil static void __exit aztech_exit(void)
1561da177e4SLinus Torvalds {
1573088fba8SHans Verkuil 	isa_unregister_driver(&aztech_driver.driver);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds module_init(aztech_init);
161e697e12eSHans Verkuil module_exit(aztech_exit);
162