blob: 33dc089f776091c8978d63791a9919c70487ff16 [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 Verkuilec632c82009-03-06 13:55:34 -030048#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030049#include <media/v4l2-ioctl.h>
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030050#include "radio-isa.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030052MODULE_AUTHOR("C. van Schaik");
Hans Verkuilec632c82009-03-06 13:55:34 -030053MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
54MODULE_LICENSE("GPL");
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030055MODULE_VERSION("0.1.99");
Mauro Carvalho Chehab2ab65292006-08-08 09:10:04 -030056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef CONFIG_RADIO_ZOLTRIX_PORT
58#define CONFIG_RADIO_ZOLTRIX_PORT -1
59#endif
60
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030061#define ZOLTRIX_MAX 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030063static int io[ZOLTRIX_MAX] = { [0] = CONFIG_RADIO_ZOLTRIX_PORT,
64 [1 ... (ZOLTRIX_MAX - 1)] = -1 };
65static int radio_nr[ZOLTRIX_MAX] = { [0 ... (ZOLTRIX_MAX - 1)] = -1 };
66
67module_param_array(io, int, NULL, 0444);
68MODULE_PARM_DESC(io, "I/O addresses of the Zoltrix Radio Plus card (0x20c or 0x30c)");
69module_param_array(radio_nr, int, NULL, 0444);
70MODULE_PARM_DESC(radio_nr, "Radio device numbers");
Hans Verkuilec632c82009-03-06 13:55:34 -030071
Hans Verkuilec632c82009-03-06 13:55:34 -030072struct zoltrix {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030073 struct radio_isa_card isa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int curvol;
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030075 bool muted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030078static struct radio_isa_card *zoltrix_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030080 struct zoltrix *zol = kzalloc(sizeof(*zol), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030082 return zol ? &zol->isa : NULL;
83}
84
85static int zoltrix_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
86{
87 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
88
89 zol->curvol = vol;
90 zol->muted = mute;
91 if (mute || vol == 0) {
92 outb(0, isa->io);
93 outb(0, isa->io);
94 inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return 0;
96 }
97
Hans Verkuil6b39246cf2012-01-16 05:18:40 -030098 outb(vol - 1, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 msleep(10);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300100 inb(isa->io + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return 0;
102}
103
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300104/* tunes the radio to the desired frequency */
105static int zoltrix_s_frequency(struct radio_isa_card *isa, u32 freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300107 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
108 struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 unsigned long long bitmask, f, m;
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300110 bool stereo = isa->stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int i;
112
Alexey Klimovb4be2042008-10-09 13:46:59 -0300113 if (freq == 0) {
Hans Verkuilec632c82009-03-06 13:55:34 -0300114 v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
Alexey Klimovb4be2042008-10-09 13:46:59 -0300115 return -EINVAL;
116 }
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 m = (freq / 160 - 8800) * 2;
Hans Verkuilec632c82009-03-06 13:55:34 -0300119 f = (unsigned long long)m + 0x4d1c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 bitmask = 0xc480402c10080000ull;
122 i = 45;
123
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300124 outb(0, isa->io);
125 outb(0, isa->io);
126 inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300127
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300128 outb(0x40, isa->io);
129 outb(0xc0, isa->io);
Hans Verkuilec632c82009-03-06 13:55:34 -0300130
131 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 while (i--) {
133 if ((bitmask & 0x8000000000000000ull) != 0) {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300134 outb(0x80, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300136 outb(0x00, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300138 outb(0x80, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 udelay(50);
140 } else {
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300141 outb(0xc0, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300143 outb(0x40, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 udelay(50);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300145 outb(0xc0, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 udelay(50);
147 }
148 bitmask *= 2;
149 }
150 /* termination sequence */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300151 outb(0x80, isa->io);
152 outb(0xc0, isa->io);
153 outb(0x40, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 udelay(1000);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300155 inb(isa->io + 2);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300156 udelay(1000);
157
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300158 return zoltrix_s_mute_volume(isa, zol->muted, zol->curvol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/* Get signal strength */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300162static u32 zoltrix_g_rxsubchans(struct radio_isa_card *isa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300164 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int a, b;
166
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300167 outb(0x00, isa->io); /* This stuff I found to do nothing */
168 outb(zol->curvol, isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 msleep(20);
170
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300171 a = inb(isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 msleep(10);
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300173 b = inb(isa->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300175 return (a == b && a == 0xcf) ?
176 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
177}
178
179static u32 zoltrix_g_signal(struct radio_isa_card *isa)
180{
181 struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
182 int a, b;
183
184 outb(0x00, isa->io); /* This stuff I found to do nothing */
185 outb(zol->curvol, isa->io);
186 msleep(20);
187
188 a = inb(isa->io);
189 msleep(10);
190 b = inb(isa->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (a != b)
Hans Verkuilec632c82009-03-06 13:55:34 -0300193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Hans Verkuilec632c82009-03-06 13:55:34 -0300195 /* I found this out by playing with a binary scanner on the card io */
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300196 return (a == 0xcf || a == 0xdf || a == 0xef) ? 0xffff : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300199static int zoltrix_s_stereo(struct radio_isa_card *isa, bool stereo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300201 return zoltrix_s_frequency(isa, isa->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300204static const struct radio_isa_ops zoltrix_ops = {
205 .alloc = zoltrix_alloc,
206 .s_mute_volume = zoltrix_s_mute_volume,
207 .s_frequency = zoltrix_s_frequency,
208 .s_stereo = zoltrix_s_stereo,
209 .g_rxsubchans = zoltrix_g_rxsubchans,
210 .g_signal = zoltrix_g_signal,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300213static const int zoltrix_ioports[] = { 0x20c, 0x30c };
214
215static struct radio_isa_driver zoltrix_driver = {
216 .driver = {
217 .match = radio_isa_match,
218 .probe = radio_isa_probe,
219 .remove = radio_isa_remove,
220 .driver = {
221 .name = "radio-zoltrix",
222 },
223 },
224 .io_params = io,
225 .radio_nr_params = radio_nr,
226 .io_ports = zoltrix_ioports,
227 .num_of_io_ports = ARRAY_SIZE(zoltrix_ioports),
228 .region_size = 2,
229 .card = "Zoltrix Radio Plus",
230 .ops = &zoltrix_ops,
231 .has_stereo = true,
232 .max_volume = 15,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
235static int __init zoltrix_init(void)
236{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300237 return isa_register_driver(&zoltrix_driver.driver, ZOLTRIX_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Hans Verkuilec632c82009-03-06 13:55:34 -0300240static void __exit zoltrix_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Hans Verkuil6b39246cf2012-01-16 05:18:40 -0300242 isa_unregister_driver(&zoltrix_driver.driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245module_init(zoltrix_init);
Hans Verkuilec632c82009-03-06 13:55:34 -0300246module_exit(zoltrix_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247