blob: f3dc57c751314b0a4b2997c0dc1c6bc4d12e077e [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Hans Verkuil6b39246cf2012-01-16 05:18:40 -03002/*
3 * Zoltrix Radio Plus driver
4 * Copyright 1998 C. van Schaik <carl@leg.uct.ac.za>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03006 * BUGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030019 * kinder to cpu while in delay
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * 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
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030029 *
30 * 2006-07-24 - Converted to V4L2 API
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -040031 * by Mauro Carvalho Chehab <mchehab@kernel.org>
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030032 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
42#include <linux/module.h> /* Modules */
43#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070044#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/delay.h> /* udelay, msleep */
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030046#include <linux/videodev2.h> /* kernel radio structs */
Hans Verkuilec632c82009-03-06 13:55:34 -030047#include <linux/mutex.h>
Hans Verkuilec632c82009-03-06 13:55:34 -030048#include <linux/io.h> /* outb, outb_p */
Hans Verkuil9f1dfcc2012-02-29 05:50:27 -030049#include <linux/slab.h>
Hans Verkuilec632c82009-03-06 13:55:34 -030050#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030051#include <media/v4l2-ioctl.h>
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030052#include "radio-isa.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030054MODULE_AUTHOR("C. van Schaik");
Hans Verkuilec632c82009-03-06 13:55:34 -030055MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
56MODULE_LICENSE("GPL");
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030057MODULE_VERSION("0.1.99");
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifndef CONFIG_RADIO_ZOLTRIX_PORT
60#define CONFIG_RADIO_ZOLTRIX_PORT -1
61#endif
62
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030063#define ZOLTRIX_MAX 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030065static 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 };
68
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");
Hans Verkuilec632c82009-03-06 13:55:34 -030073
Hans Verkuilec632c82009-03-06 13:55:34 -030074struct zoltrix {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030075 struct radio_isa_card isa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int curvol;
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030077 bool muted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030080static struct radio_isa_card *zoltrix_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030082 struct zoltrix *zol = kzalloc(sizeof(*zol), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030084 return zol ? &zol->isa : NULL;
85}
86
87static int zoltrix_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
88{
89 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
90
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
98 }
99
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300100 outb(vol - 1, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 msleep(10);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300102 inb(isa->io + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104}
105
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300106/* tunes the radio to the desired frequency */
107static int zoltrix_s_frequency(struct radio_isa_card *isa, u32 freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300109 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
110 struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long long bitmask, f, m;
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300112 bool stereo = isa->stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int i;
114
Alexey Klimovb4be2042008-10-09 13:46:59 -0300115 if (freq == 0) {
Hans Verkuilec632c82009-03-06 13:55:34 -0300116 v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
Alexey Klimovb4be2042008-10-09 13:46:59 -0300117 return -EINVAL;
118 }
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 m = (freq / 160 - 8800) * 2;
Hans Verkuilec632c82009-03-06 13:55:34 -0300121 f = (unsigned long long)m + 0x4d1c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 bitmask = 0xc480402c10080000ull;
124 i = 45;
125
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300126 outb(0, isa->io);
127 outb(0, isa->io);
128 inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300129
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300130 outb(0x40, isa->io);
131 outb(0xc0, isa->io);
Hans Verkuilec632c82009-03-06 13:55:34 -0300132
133 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 while (i--) {
135 if ((bitmask & 0x8000000000000000ull) != 0) {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300136 outb(0x80, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300138 outb(0x00, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300140 outb(0x80, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 udelay(50);
142 } else {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300143 outb(0xc0, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300145 outb(0x40, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300147 outb(0xc0, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 udelay(50);
149 }
150 bitmask *= 2;
151 }
152 /* termination sequence */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300153 outb(0x80, isa->io);
154 outb(0xc0, isa->io);
155 outb(0x40, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 udelay(1000);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300157 inb(isa->io + 2);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300158 udelay(1000);
159
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300160 return zoltrix_s_mute_volume(isa, zol->muted, zol->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/* Get signal strength */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300164static u32 zoltrix_g_rxsubchans(struct radio_isa_card *isa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300166 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int a, b;
168
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300169 outb(0x00, isa->io); /* This stuff I found to do nothing */
170 outb(zol->curvol, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 msleep(20);
172
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300173 a = inb(isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 msleep(10);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300175 b = inb(isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300177 return (a == b && a == 0xcf) ?
178 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
179}
180
181static u32 zoltrix_g_signal(struct radio_isa_card *isa)
182{
183 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
184 int a, b;
185
186 outb(0x00, isa->io); /* This stuff I found to do nothing */
187 outb(zol->curvol, isa->io);
188 msleep(20);
189
190 a = inb(isa->io);
191 msleep(10);
192 b = inb(isa->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (a != b)
Hans Verkuilec632c82009-03-06 13:55:34 -0300195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Hans Verkuilec632c82009-03-06 13:55:34 -0300197 /* I found this out by playing with a binary scanner on the card io */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300198 return (a == 0xcf || a == 0xdf || a == 0xef) ? 0xffff : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300201static int zoltrix_s_stereo(struct radio_isa_card *isa, bool stereo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300203 return zoltrix_s_frequency(isa, isa->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300206static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300215static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237static int __init zoltrix_init(void)
238{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300239 return isa_register_driver(&zoltrix_driver.driver, ZOLTRIX_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Hans Verkuilec632c82009-03-06 13:55:34 -0300242static void __exit zoltrix_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300244 isa_unregister_driver(&zoltrix_driver.driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247module_init(zoltrix_init);
Hans Verkuilec632c82009-03-06 13:55:34 -0300248module_exit(zoltrix_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249