blob: 026e88eef29ce0ef4a577e692d7512f4893ef9b9 [file] [log] [blame]
Hans Verkuil6b39246cf2012-01-16 05:18:40 -03001/*
2 * Zoltrix Radio Plus driver
3 * Copyright 1998 C. van Schaik <carl@leg.uct.ac.za>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03005 * BUGS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Due to the inconsistency in reading from the signal flags
7 * it is difficult to get an accurate tuned signal.
8 *
9 * It seems that the card is not linear to 0 volume. It cuts off
10 * at a low volume, and it is not possible (at least I have not found)
11 * to get fine volume control over the low volume range.
12 *
13 * Some code derived from code by Romolo Manfredini
14 * romolo@bicnet.it
15 *
16 * 1999-05-06 - (C. van Schaik)
17 * - Make signal strength and stereo scans
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030018 * kinder to cpu while in delay
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 1999-01-05 - (C. van Schaik)
20 * - Changed tuning to 1/160Mhz accuracy
21 * - Added stereo support
22 * (card defaults to stereo)
23 * (can explicitly force mono on the card)
24 * (can detect if station is in stereo)
25 * - Added unmute function
26 * - Reworked ioctl functions
27 * 2002-07-15 - Fix Stereo typo
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030028 *
29 * 2006-07-24 - Converted to V4L2 API
30 * by Mauro Carvalho Chehab <mchehab@infradead.org>
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030031 *
32 * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com>
33 *
34 * Note that this is the driver for the Zoltrix Radio Plus.
35 * This driver does not work for the Zoltrix Radio Plus 108 or the
36 * Zoltrix Radio Plus for Windows.
37 *
38 * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
41#include <linux/module.h> /* Modules */
42#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070043#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h> /* udelay, msleep */
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030045#include <linux/videodev2.h> /* kernel radio structs */
Hans Verkuilec632c82009-03-06 13:55:34 -030046#include <linux/mutex.h>
Hans Verkuilec632c82009-03-06 13:55:34 -030047#include <linux/io.h> /* outb, outb_p */
Hans Verkuil9f1dfcc2012-02-29 05:50:27 -030048#include <linux/slab.h>
Hans Verkuilec632c82009-03-06 13:55:34 -030049#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030050#include <media/v4l2-ioctl.h>
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030051#include "radio-isa.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030053MODULE_AUTHOR("C. van Schaik");
Hans Verkuilec632c82009-03-06 13:55:34 -030054MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
55MODULE_LICENSE("GPL");
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030056MODULE_VERSION("0.1.99");
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifndef CONFIG_RADIO_ZOLTRIX_PORT
59#define CONFIG_RADIO_ZOLTRIX_PORT -1
60#endif
61
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030062#define ZOLTRIX_MAX 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030064static int io[ZOLTRIX_MAX] = { [0] = CONFIG_RADIO_ZOLTRIX_PORT,
65 [1 ... (ZOLTRIX_MAX - 1)] = -1 };
66static int radio_nr[ZOLTRIX_MAX] = { [0 ... (ZOLTRIX_MAX - 1)] = -1 };
67
68module_param_array(io, int, NULL, 0444);
69MODULE_PARM_DESC(io, "I/O addresses of the Zoltrix Radio Plus card (0x20c or 0x30c)");
70module_param_array(radio_nr, int, NULL, 0444);
71MODULE_PARM_DESC(radio_nr, "Radio device numbers");
Hans Verkuilec632c82009-03-06 13:55:34 -030072
Hans Verkuilec632c82009-03-06 13:55:34 -030073struct zoltrix {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030074 struct radio_isa_card isa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int curvol;
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030076 bool muted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030079static struct radio_isa_card *zoltrix_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030081 struct zoltrix *zol = kzalloc(sizeof(*zol), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030083 return zol ? &zol->isa : NULL;
84}
85
86static int zoltrix_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
87{
88 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
89
90 zol->curvol = vol;
91 zol->muted = mute;
92 if (mute || vol == 0) {
93 outb(0, isa->io);
94 outb(0, isa->io);
95 inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97 }
98
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030099 outb(vol - 1, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 msleep(10);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300101 inb(isa->io + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103}
104
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300105/* tunes the radio to the desired frequency */
106static int zoltrix_s_frequency(struct radio_isa_card *isa, u32 freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300108 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
109 struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned long long bitmask, f, m;
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300111 bool stereo = isa->stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int i;
113
Alexey Klimovb4be2042008-10-09 13:46:59 -0300114 if (freq == 0) {
Hans Verkuilec632c82009-03-06 13:55:34 -0300115 v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
Alexey Klimovb4be2042008-10-09 13:46:59 -0300116 return -EINVAL;
117 }
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 m = (freq / 160 - 8800) * 2;
Hans Verkuilec632c82009-03-06 13:55:34 -0300120 f = (unsigned long long)m + 0x4d1c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 bitmask = 0xc480402c10080000ull;
123 i = 45;
124
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300125 outb(0, isa->io);
126 outb(0, isa->io);
127 inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300128
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300129 outb(0x40, isa->io);
130 outb(0xc0, isa->io);
Hans Verkuilec632c82009-03-06 13:55:34 -0300131
132 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 while (i--) {
134 if ((bitmask & 0x8000000000000000ull) != 0) {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300135 outb(0x80, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300137 outb(0x00, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300139 outb(0x80, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 udelay(50);
141 } else {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300142 outb(0xc0, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300144 outb(0x40, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300146 outb(0xc0, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 udelay(50);
148 }
149 bitmask *= 2;
150 }
151 /* termination sequence */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300152 outb(0x80, isa->io);
153 outb(0xc0, isa->io);
154 outb(0x40, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 udelay(1000);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300156 inb(isa->io + 2);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300157 udelay(1000);
158
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300159 return zoltrix_s_mute_volume(isa, zol->muted, zol->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162/* Get signal strength */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300163static u32 zoltrix_g_rxsubchans(struct radio_isa_card *isa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300165 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int a, b;
167
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300168 outb(0x00, isa->io); /* This stuff I found to do nothing */
169 outb(zol->curvol, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 msleep(20);
171
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300172 a = inb(isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 msleep(10);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300174 b = inb(isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300176 return (a == b && a == 0xcf) ?
177 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
178}
179
180static u32 zoltrix_g_signal(struct radio_isa_card *isa)
181{
182 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
183 int a, b;
184
185 outb(0x00, isa->io); /* This stuff I found to do nothing */
186 outb(zol->curvol, isa->io);
187 msleep(20);
188
189 a = inb(isa->io);
190 msleep(10);
191 b = inb(isa->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (a != b)
Hans Verkuilec632c82009-03-06 13:55:34 -0300194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Hans Verkuilec632c82009-03-06 13:55:34 -0300196 /* I found this out by playing with a binary scanner on the card io */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300197 return (a == 0xcf || a == 0xdf || a == 0xef) ? 0xffff : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300200static int zoltrix_s_stereo(struct radio_isa_card *isa, bool stereo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300202 return zoltrix_s_frequency(isa, isa->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300205static const struct radio_isa_ops zoltrix_ops = {
206 .alloc = zoltrix_alloc,
207 .s_mute_volume = zoltrix_s_mute_volume,
208 .s_frequency = zoltrix_s_frequency,
209 .s_stereo = zoltrix_s_stereo,
210 .g_rxsubchans = zoltrix_g_rxsubchans,
211 .g_signal = zoltrix_g_signal,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300214static const int zoltrix_ioports[] = { 0x20c, 0x30c };
215
216static struct radio_isa_driver zoltrix_driver = {
217 .driver = {
218 .match = radio_isa_match,
219 .probe = radio_isa_probe,
220 .remove = radio_isa_remove,
221 .driver = {
222 .name = "radio-zoltrix",
223 },
224 },
225 .io_params = io,
226 .radio_nr_params = radio_nr,
227 .io_ports = zoltrix_ioports,
228 .num_of_io_ports = ARRAY_SIZE(zoltrix_ioports),
229 .region_size = 2,
230 .card = "Zoltrix Radio Plus",
231 .ops = &zoltrix_ops,
232 .has_stereo = true,
233 .max_volume = 15,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
236static int __init zoltrix_init(void)
237{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300238 return isa_register_driver(&zoltrix_driver.driver, ZOLTRIX_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Hans Verkuilec632c82009-03-06 13:55:34 -0300241static void __exit zoltrix_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300243 isa_unregister_driver(&zoltrix_driver.driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246module_init(zoltrix_init);
Hans Verkuilec632c82009-03-06 13:55:34 -0300247module_exit(zoltrix_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248