treewide: Add SPDX license identifier for more missed files
[linux-2.6-block.git] / drivers / media / radio / radio-zoltrix.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
6b39246c
HV
2/*
3 * Zoltrix Radio Plus driver
4 * Copyright 1998 C. van Schaik <carl@leg.uct.ac.za>
1da177e4 5 *
4286c6f6 6 * BUGS
1da177e4
LT
7 * Due to the inconsistency in reading from the signal flags
8 * it is difficult to get an accurate tuned signal.
9 *
10 * It seems that the card is not linear to 0 volume. It cuts off
11 * at a low volume, and it is not possible (at least I have not found)
12 * to get fine volume control over the low volume range.
13 *
14 * Some code derived from code by Romolo Manfredini
15 * romolo@bicnet.it
16 *
17 * 1999-05-06 - (C. van Schaik)
18 * - Make signal strength and stereo scans
4286c6f6 19 * kinder to cpu while in delay
1da177e4
LT
20 * 1999-01-05 - (C. van Schaik)
21 * - Changed tuning to 1/160Mhz accuracy
22 * - Added stereo support
23 * (card defaults to stereo)
24 * (can explicitly force mono on the card)
25 * (can detect if station is in stereo)
26 * - Added unmute function
27 * - Reworked ioctl functions
28 * 2002-07-15 - Fix Stereo typo
2ab65299
MCC
29 *
30 * 2006-07-24 - Converted to V4L2 API
32590819 31 * by Mauro Carvalho Chehab <mchehab@kernel.org>
6b39246c
HV
32 *
33 * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com>
34 *
35 * Note that this is the driver for the Zoltrix Radio Plus.
36 * This driver does not work for the Zoltrix Radio Plus 108 or the
37 * Zoltrix Radio Plus for Windows.
38 *
39 * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
1da177e4
LT
40 */
41
42#include <linux/module.h> /* Modules */
43#include <linux/init.h> /* Initdata */
fb911ee8 44#include <linux/ioport.h> /* request_region */
1da177e4 45#include <linux/delay.h> /* udelay, msleep */
2ab65299 46#include <linux/videodev2.h> /* kernel radio structs */
ec632c8a 47#include <linux/mutex.h>
ec632c8a 48#include <linux/io.h> /* outb, outb_p */
9f1dfccf 49#include <linux/slab.h>
ec632c8a 50#include <media/v4l2-device.h>
35ea11ff 51#include <media/v4l2-ioctl.h>
6b39246c 52#include "radio-isa.h"
1da177e4 53
6b39246c 54MODULE_AUTHOR("C. van Schaik");
ec632c8a
HV
55MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
56MODULE_LICENSE("GPL");
6b39246c 57MODULE_VERSION("0.1.99");
2ab65299 58
1da177e4
LT
59#ifndef CONFIG_RADIO_ZOLTRIX_PORT
60#define CONFIG_RADIO_ZOLTRIX_PORT -1
61#endif
62
6b39246c
HV
63#define ZOLTRIX_MAX 2
64
65static int io[ZOLTRIX_MAX] = { [0] = CONFIG_RADIO_ZOLTRIX_PORT,
66 [1 ... (ZOLTRIX_MAX - 1)] = -1 };
67static int radio_nr[ZOLTRIX_MAX] = { [0 ... (ZOLTRIX_MAX - 1)] = -1 };
1da177e4 68
6b39246c
HV
69module_param_array(io, int, NULL, 0444);
70MODULE_PARM_DESC(io, "I/O addresses of the Zoltrix Radio Plus card (0x20c or 0x30c)");
71module_param_array(radio_nr, int, NULL, 0444);
72MODULE_PARM_DESC(radio_nr, "Radio device numbers");
ec632c8a 73
ec632c8a 74struct zoltrix {
6b39246c 75 struct radio_isa_card isa;
1da177e4 76 int curvol;
6b39246c 77 bool muted;
1da177e4
LT
78};
79
6b39246c
HV
80static struct radio_isa_card *zoltrix_alloc(void)
81{
82 struct zoltrix *zol = kzalloc(sizeof(*zol), GFP_KERNEL);
83
84 return zol ? &zol->isa : NULL;
85}
ec632c8a 86
6b39246c 87static int zoltrix_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
1da177e4 88{
6b39246c 89 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
1da177e4 90
6b39246c
HV
91 zol->curvol = vol;
92 zol->muted = mute;
93 if (mute || vol == 0) {
94 outb(0, isa->io);
95 outb(0, isa->io);
96 inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
1da177e4
LT
97 return 0;
98 }
99
6b39246c 100 outb(vol - 1, isa->io);
1da177e4 101 msleep(10);
6b39246c 102 inb(isa->io + 2);
1da177e4
LT
103 return 0;
104}
105
6b39246c
HV
106/* tunes the radio to the desired frequency */
107static int zoltrix_s_frequency(struct radio_isa_card *isa, u32 freq)
1da177e4 108{
6b39246c
HV
109 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
110 struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
1da177e4 111 unsigned long long bitmask, f, m;
6b39246c 112 bool stereo = isa->stereo;
1da177e4
LT
113 int i;
114
b4be2048 115 if (freq == 0) {
ec632c8a 116 v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
b4be2048
AK
117 return -EINVAL;
118 }
119
1da177e4 120 m = (freq / 160 - 8800) * 2;
ec632c8a 121 f = (unsigned long long)m + 0x4d1c;
1da177e4
LT
122
123 bitmask = 0xc480402c10080000ull;
124 i = 45;
125
6b39246c
HV
126 outb(0, isa->io);
127 outb(0, isa->io);
128 inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
1da177e4 129
6b39246c
HV
130 outb(0x40, isa->io);
131 outb(0xc0, isa->io);
ec632c8a
HV
132
133 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
1da177e4
LT
134 while (i--) {
135 if ((bitmask & 0x8000000000000000ull) != 0) {
6b39246c 136 outb(0x80, isa->io);
1da177e4 137 udelay(50);
6b39246c 138 outb(0x00, isa->io);
1da177e4 139 udelay(50);
6b39246c 140 outb(0x80, isa->io);
1da177e4
LT
141 udelay(50);
142 } else {
6b39246c 143 outb(0xc0, isa->io);
1da177e4 144 udelay(50);
6b39246c 145 outb(0x40, isa->io);
1da177e4 146 udelay(50);
6b39246c 147 outb(0xc0, isa->io);
1da177e4
LT
148 udelay(50);
149 }
150 bitmask *= 2;
151 }
152 /* termination sequence */
6b39246c
HV
153 outb(0x80, isa->io);
154 outb(0xc0, isa->io);
155 outb(0x40, isa->io);
1da177e4 156 udelay(1000);
6b39246c 157 inb(isa->io + 2);
4286c6f6
MCC
158 udelay(1000);
159
6b39246c 160 return zoltrix_s_mute_volume(isa, zol->muted, zol->curvol);
1da177e4
LT
161}
162
163/* Get signal strength */
6b39246c 164static u32 zoltrix_g_rxsubchans(struct radio_isa_card *isa)
1da177e4 165{
6b39246c 166 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
1da177e4
LT
167 int a, b;
168
6b39246c
HV
169 outb(0x00, isa->io); /* This stuff I found to do nothing */
170 outb(zol->curvol, isa->io);
1da177e4
LT
171 msleep(20);
172
6b39246c 173 a = inb(isa->io);
1da177e4 174 msleep(10);
6b39246c 175 b = inb(isa->io);
1da177e4 176
6b39246c
HV
177 return (a == b && a == 0xcf) ?
178 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1da177e4
LT
179}
180
6b39246c 181static u32 zoltrix_g_signal(struct radio_isa_card *isa)
1da177e4 182{
6b39246c
HV
183 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
184 int a, b;
4286c6f6 185
6b39246c
HV
186 outb(0x00, isa->io); /* This stuff I found to do nothing */
187 outb(zol->curvol, isa->io);
1da177e4
LT
188 msleep(20);
189
6b39246c 190 a = inb(isa->io);
1da177e4 191 msleep(10);
6b39246c 192 b = inb(isa->io);
a1314b1a 193
6b39246c 194 if (a != b)
a1314b1a 195 return 0;
a1314b1a 196
6b39246c
HV
197 /* I found this out by playing with a binary scanner on the card io */
198 return (a == 0xcf || a == 0xdf || a == 0xef) ? 0xffff : 0;
1da177e4
LT
199}
200
6b39246c 201static int zoltrix_s_stereo(struct radio_isa_card *isa, bool stereo)
ec632c8a 202{
6b39246c 203 return zoltrix_s_frequency(isa, isa->freq);
ec632c8a 204}
1da177e4 205
6b39246c
HV
206static const struct radio_isa_ops zoltrix_ops = {
207 .alloc = zoltrix_alloc,
208 .s_mute_volume = zoltrix_s_mute_volume,
209 .s_frequency = zoltrix_s_frequency,
210 .s_stereo = zoltrix_s_stereo,
211 .g_rxsubchans = zoltrix_g_rxsubchans,
212 .g_signal = zoltrix_g_signal,
1da177e4
LT
213};
214
6b39246c
HV
215static const int zoltrix_ioports[] = { 0x20c, 0x30c };
216
217static struct radio_isa_driver zoltrix_driver = {
218 .driver = {
219 .match = radio_isa_match,
220 .probe = radio_isa_probe,
221 .remove = radio_isa_remove,
222 .driver = {
223 .name = "radio-zoltrix",
224 },
225 },
226 .io_params = io,
227 .radio_nr_params = radio_nr,
228 .io_ports = zoltrix_ioports,
229 .num_of_io_ports = ARRAY_SIZE(zoltrix_ioports),
230 .region_size = 2,
231 .card = "Zoltrix Radio Plus",
232 .ops = &zoltrix_ops,
233 .has_stereo = true,
234 .max_volume = 15,
1da177e4
LT
235};
236
237static int __init zoltrix_init(void)
238{
6b39246c 239 return isa_register_driver(&zoltrix_driver.driver, ZOLTRIX_MAX);
1da177e4
LT
240}
241
ec632c8a 242static void __exit zoltrix_exit(void)
1da177e4 243{
6b39246c 244 isa_unregister_driver(&zoltrix_driver.driver);
1da177e4
LT
245}
246
247module_init(zoltrix_init);
ec632c8a 248module_exit(zoltrix_exit);
1da177e4 249