1 /*
2  * radio-aztech.c - Aztech radio card driver
3  *
4  * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@xs4all.nl>
5  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
6  * Adapted to support the Video for Linux API by
7  * Russell Kroll <rkroll@exploits.org>.  Based on original tuner code by:
8  *
9  * Quay Ly
10  * Donald Song
11  * Jason Lewis      (jlewis@twilight.vtc.vsc.edu)
12  * Scott McGrath    (smcgrath@twilight.vtc.vsc.edu)
13  * William McGrath  (wmcgrath@twilight.vtc.vsc.edu)
14  *
15  * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
16 */
17 
18 #include <linux/module.h>	/* Modules 			*/
19 #include <linux/init.h>		/* Initdata			*/
20 #include <linux/ioport.h>	/* request_region		*/
21 #include <linux/delay.h>	/* udelay			*/
22 #include <linux/videodev2.h>	/* kernel radio structs		*/
23 #include <linux/io.h>		/* outb, outb_p			*/
24 #include <linux/slab.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-ctrls.h>
28 #include "radio-isa.h"
29 
30 MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
31 MODULE_DESCRIPTION("A driver for the Aztech radio card.");
32 MODULE_LICENSE("GPL");
33 MODULE_VERSION("1.0.0");
34 
35 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
36 #ifndef CONFIG_RADIO_AZTECH_PORT
37 #define CONFIG_RADIO_AZTECH_PORT -1
38 #endif
39 
40 #define AZTECH_MAX 2
41 
42 static int io[AZTECH_MAX] = { [0] = CONFIG_RADIO_AZTECH_PORT,
43 			      [1 ... (AZTECH_MAX - 1)] = -1 };
44 static int radio_nr[AZTECH_MAX]	= { [0 ... (AZTECH_MAX - 1)] = -1 };
45 static const int radio_wait_time = 1000;
46 
47 module_param_array(io, int, NULL, 0444);
48 MODULE_PARM_DESC(io, "I/O addresses of the Aztech card (0x350 or 0x358)");
49 module_param_array(radio_nr, int, NULL, 0444);
50 MODULE_PARM_DESC(radio_nr, "Radio device numbers");
51 
52 struct aztech {
53 	struct radio_isa_card isa;
54 	int curvol;
55 };
56 
57 static void send_0_byte(struct aztech *az)
58 {
59 	udelay(radio_wait_time);
60 	outb_p(2 + az->curvol, az->isa.io);
61 	outb_p(64 + 2 + az->curvol, az->isa.io);
62 }
63 
64 static void send_1_byte(struct aztech *az)
65 {
66 	udelay(radio_wait_time);
67 	outb_p(128 + 2 + az->curvol, az->isa.io);
68 	outb_p(128 + 64 + 2 + az->curvol, az->isa.io);
69 }
70 
71 static struct radio_isa_card *aztech_alloc(void)
72 {
73 	struct aztech *az = kzalloc(sizeof(*az), GFP_KERNEL);
74 
75 	return az ? &az->isa : NULL;
76 }
77 
78 static int aztech_s_frequency(struct radio_isa_card *isa, u32 freq)
79 {
80 	struct aztech *az = container_of(isa, struct aztech, isa);
81 	int  i;
82 
83 	freq += 171200;			/* Add 10.7 MHz IF		*/
84 	freq /= 800;			/* Convert to 50 kHz units	*/
85 
86 	send_0_byte(az);		/*  0: LSB of frequency       */
87 
88 	for (i = 0; i < 13; i++)	/*   : frequency bits (1-13)  */
89 		if (freq & (1 << i))
90 			send_1_byte(az);
91 		else
92 			send_0_byte(az);
93 
94 	send_0_byte(az);		/* 14: test bit - always 0    */
95 	send_0_byte(az);		/* 15: test bit - always 0    */
96 	send_0_byte(az);		/* 16: band data 0 - always 0 */
97 	if (isa->stereo)		/* 17: stereo (1 to enable)   */
98 		send_1_byte(az);
99 	else
100 		send_0_byte(az);
101 
102 	send_1_byte(az);		/* 18: band data 1 - unknown  */
103 	send_0_byte(az);		/* 19: time base - always 0   */
104 	send_0_byte(az);		/* 20: spacing (0 = 25 kHz)   */
105 	send_1_byte(az);		/* 21: spacing (1 = 25 kHz)   */
106 	send_0_byte(az);		/* 22: spacing (0 = 25 kHz)   */
107 	send_1_byte(az);		/* 23: AM/FM (FM = 1, always) */
108 
109 	/* latch frequency */
110 
111 	udelay(radio_wait_time);
112 	outb_p(128 + 64 + az->curvol, az->isa.io);
113 
114 	return 0;
115 }
116 
117 /* thanks to Michael Dwyer for giving me a dose of clues in
118  * the signal strength department..
119  *
120  * This card has a stereo bit - bit 0 set = mono, not set = stereo
121  */
122 static u32 aztech_g_rxsubchans(struct radio_isa_card *isa)
123 {
124 	if (inb(isa->io) & 1)
125 		return V4L2_TUNER_SUB_MONO;
126 	return V4L2_TUNER_SUB_STEREO;
127 }
128 
129 static int aztech_s_stereo(struct radio_isa_card *isa, bool stereo)
130 {
131 	return aztech_s_frequency(isa, isa->freq);
132 }
133 
134 static int aztech_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
135 {
136 	struct aztech *az = container_of(isa, struct aztech, isa);
137 
138 	if (mute)
139 		vol = 0;
140 	az->curvol = (vol & 1) + ((vol & 2) << 1);
141 	outb(az->curvol, isa->io);
142 	return 0;
143 }
144 
145 static const struct radio_isa_ops aztech_ops = {
146 	.alloc = aztech_alloc,
147 	.s_mute_volume = aztech_s_mute_volume,
148 	.s_frequency = aztech_s_frequency,
149 	.s_stereo = aztech_s_stereo,
150 	.g_rxsubchans = aztech_g_rxsubchans,
151 };
152 
153 static const int aztech_ioports[] = { 0x350, 0x358 };
154 
155 static struct radio_isa_driver aztech_driver = {
156 	.driver = {
157 		.match		= radio_isa_match,
158 		.probe		= radio_isa_probe,
159 		.remove		= radio_isa_remove,
160 		.driver		= {
161 			.name	= "radio-aztech",
162 		},
163 	},
164 	.io_params = io,
165 	.radio_nr_params = radio_nr,
166 	.io_ports = aztech_ioports,
167 	.num_of_io_ports = ARRAY_SIZE(aztech_ioports),
168 	.region_size = 2,
169 	.card = "Aztech Radio",
170 	.ops = &aztech_ops,
171 	.has_stereo = true,
172 	.max_volume = 3,
173 };
174 
175 static int __init aztech_init(void)
176 {
177 	return isa_register_driver(&aztech_driver.driver, AZTECH_MAX);
178 }
179 
180 static void __exit aztech_exit(void)
181 {
182 	isa_unregister_driver(&aztech_driver.driver);
183 }
184 
185 module_init(aztech_init);
186 module_exit(aztech_exit);
187