1 // SPDX-License-Identifier: GPL-2.0-only
2 /* radio-trust.c - Trust FM Radio card driver for Linux 2.2
3  * by Eric Lammerts <eric@scintilla.utwente.nl>
4  *
5  * Based on radio-aztech.c. Original notes:
6  *
7  * Adapted to support the Video for Linux API by
8  * Russell Kroll <rkroll@exploits.org>.  Based on original tuner code by:
9  *
10  * Quay Ly
11  * Donald Song
12  * Jason Lewis      (jlewis@twilight.vtc.vsc.edu)
13  * Scott McGrath    (smcgrath@twilight.vtc.vsc.edu)
14  * William McGrath  (wmcgrath@twilight.vtc.vsc.edu)
15  *
16  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org>
17  */
18 
19 #include <stdarg.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/ioport.h>
23 #include <linux/videodev2.h>
24 #include <linux/io.h>
25 #include <linux/slab.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ioctl.h>
28 #include "radio-isa.h"
29 
30 MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
31 MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
32 MODULE_LICENSE("GPL");
33 MODULE_VERSION("0.1.99");
34 
35 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
36 
37 #ifndef CONFIG_RADIO_TRUST_PORT
38 #define CONFIG_RADIO_TRUST_PORT -1
39 #endif
40 
41 #define TRUST_MAX 2
42 
43 static int io[TRUST_MAX] = { [0] = CONFIG_RADIO_TRUST_PORT,
44 			      [1 ... (TRUST_MAX - 1)] = -1 };
45 static int radio_nr[TRUST_MAX] = { [0 ... (TRUST_MAX - 1)] = -1 };
46 
47 module_param_array(io, int, NULL, 0444);
48 MODULE_PARM_DESC(io, "I/O addresses of the Trust FM Radio 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 trust {
53 	struct radio_isa_card isa;
54 	int ioval;
55 };
56 
57 static struct radio_isa_card *trust_alloc(void)
58 {
59 	struct trust *tr = kzalloc(sizeof(*tr), GFP_KERNEL);
60 
61 	return tr ? &tr->isa : NULL;
62 }
63 
64 /* i2c addresses */
65 #define TDA7318_ADDR 0x88
66 #define TSA6060T_ADDR 0xc4
67 
68 #define TR_DELAY do { inb(tr->isa.io); inb(tr->isa.io); inb(tr->isa.io); } while (0)
69 #define TR_SET_SCL outb(tr->ioval |= 2, tr->isa.io)
70 #define TR_CLR_SCL outb(tr->ioval &= 0xfd, tr->isa.io)
71 #define TR_SET_SDA outb(tr->ioval |= 1, tr->isa.io)
72 #define TR_CLR_SDA outb(tr->ioval &= 0xfe, tr->isa.io)
73 
74 static void write_i2c(struct trust *tr, int n, ...)
75 {
76 	unsigned char val, mask;
77 	va_list args;
78 
79 	va_start(args, n);
80 
81 	/* start condition */
82 	TR_SET_SDA;
83 	TR_SET_SCL;
84 	TR_DELAY;
85 	TR_CLR_SDA;
86 	TR_CLR_SCL;
87 	TR_DELAY;
88 
89 	for (; n; n--) {
90 		val = va_arg(args, unsigned);
91 		for (mask = 0x80; mask; mask >>= 1) {
92 			if (val & mask)
93 				TR_SET_SDA;
94 			else
95 				TR_CLR_SDA;
96 			TR_SET_SCL;
97 			TR_DELAY;
98 			TR_CLR_SCL;
99 			TR_DELAY;
100 		}
101 		/* acknowledge bit */
102 		TR_SET_SDA;
103 		TR_SET_SCL;
104 		TR_DELAY;
105 		TR_CLR_SCL;
106 		TR_DELAY;
107 	}
108 
109 	/* stop condition */
110 	TR_CLR_SDA;
111 	TR_DELAY;
112 	TR_SET_SCL;
113 	TR_DELAY;
114 	TR_SET_SDA;
115 	TR_DELAY;
116 
117 	va_end(args);
118 }
119 
120 static int trust_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
121 {
122 	struct trust *tr = container_of(isa, struct trust, isa);
123 
124 	tr->ioval = (tr->ioval & 0xf7) | (mute << 3);
125 	outb(tr->ioval, isa->io);
126 	write_i2c(tr, 2, TDA7318_ADDR, vol ^ 0x1f);
127 	return 0;
128 }
129 
130 static int trust_s_stereo(struct radio_isa_card *isa, bool stereo)
131 {
132 	struct trust *tr = container_of(isa, struct trust, isa);
133 
134 	tr->ioval = (tr->ioval & 0xfb) | (!stereo << 2);
135 	outb(tr->ioval, isa->io);
136 	return 0;
137 }
138 
139 static u32 trust_g_signal(struct radio_isa_card *isa)
140 {
141 	int i, v;
142 
143 	for (i = 0, v = 0; i < 100; i++)
144 		v |= inb(isa->io);
145 	return (v & 1) ? 0 : 0xffff;
146 }
147 
148 static int trust_s_frequency(struct radio_isa_card *isa, u32 freq)
149 {
150 	struct trust *tr = container_of(isa, struct trust, isa);
151 
152 	freq /= 160;	/* Convert to 10 kHz units	*/
153 	freq += 1070;	/* Add 10.7 MHz IF		*/
154 	write_i2c(tr, 5, TSA6060T_ADDR, (freq << 1) | 1,
155 			freq >> 7, 0x60 | ((freq >> 15) & 1), 0);
156 	return 0;
157 }
158 
159 static int basstreble2chip[15] = {
160 	0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
161 };
162 
163 static int trust_s_ctrl(struct v4l2_ctrl *ctrl)
164 {
165 	struct radio_isa_card *isa =
166 		container_of(ctrl->handler, struct radio_isa_card, hdl);
167 	struct trust *tr = container_of(isa, struct trust, isa);
168 
169 	switch (ctrl->id) {
170 	case V4L2_CID_AUDIO_BASS:
171 		write_i2c(tr, 2, TDA7318_ADDR, 0x60 | basstreble2chip[ctrl->val]);
172 		return 0;
173 	case V4L2_CID_AUDIO_TREBLE:
174 		write_i2c(tr, 2, TDA7318_ADDR, 0x70 | basstreble2chip[ctrl->val]);
175 		return 0;
176 	}
177 	return -EINVAL;
178 }
179 
180 static const struct v4l2_ctrl_ops trust_ctrl_ops = {
181 	.s_ctrl = trust_s_ctrl,
182 };
183 
184 static int trust_initialize(struct radio_isa_card *isa)
185 {
186 	struct trust *tr = container_of(isa, struct trust, isa);
187 
188 	tr->ioval = 0xf;
189 	write_i2c(tr, 2, TDA7318_ADDR, 0x80);	/* speaker att. LF = 0 dB */
190 	write_i2c(tr, 2, TDA7318_ADDR, 0xa0);	/* speaker att. RF = 0 dB */
191 	write_i2c(tr, 2, TDA7318_ADDR, 0xc0);	/* speaker att. LR = 0 dB */
192 	write_i2c(tr, 2, TDA7318_ADDR, 0xe0);	/* speaker att. RR = 0 dB */
193 	write_i2c(tr, 2, TDA7318_ADDR, 0x40);	/* stereo 1 input, gain = 18.75 dB */
194 
195 	v4l2_ctrl_new_std(&isa->hdl, &trust_ctrl_ops,
196 				V4L2_CID_AUDIO_BASS, 0, 15, 1, 8);
197 	v4l2_ctrl_new_std(&isa->hdl, &trust_ctrl_ops,
198 				V4L2_CID_AUDIO_TREBLE, 0, 15, 1, 8);
199 	return isa->hdl.error;
200 }
201 
202 static const struct radio_isa_ops trust_ops = {
203 	.init = trust_initialize,
204 	.alloc = trust_alloc,
205 	.s_mute_volume = trust_s_mute_volume,
206 	.s_frequency = trust_s_frequency,
207 	.s_stereo = trust_s_stereo,
208 	.g_signal = trust_g_signal,
209 };
210 
211 static const int trust_ioports[] = { 0x350, 0x358 };
212 
213 static struct radio_isa_driver trust_driver = {
214 	.driver = {
215 		.match		= radio_isa_match,
216 		.probe		= radio_isa_probe,
217 		.remove		= radio_isa_remove,
218 		.driver		= {
219 			.name	= "radio-trust",
220 		},
221 	},
222 	.io_params = io,
223 	.radio_nr_params = radio_nr,
224 	.io_ports = trust_ioports,
225 	.num_of_io_ports = ARRAY_SIZE(trust_ioports),
226 	.region_size = 2,
227 	.card = "Trust FM Radio",
228 	.ops = &trust_ops,
229 	.has_stereo = true,
230 	.max_volume = 31,
231 };
232 
233 static int __init trust_init(void)
234 {
235 	return isa_register_driver(&trust_driver.driver, TRUST_MAX);
236 }
237 
238 static void __exit trust_exit(void)
239 {
240 	isa_unregister_driver(&trust_driver.driver);
241 }
242 
243 module_init(trust_init);
244 module_exit(trust_exit);
245