Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Zoltrix Radio Plus driver |
| 3 | * Copyright 1998 C. van Schaik <carl@leg.uct.ac.za> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 5 | * BUGS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * 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 Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 18 | * kinder to cpu while in delay |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * 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 Chehab | 2ab6529 | 2006-08-08 09:10:04 -0300 | [diff] [blame] | 28 | * |
| 29 | * 2006-07-24 - Converted to V4L2 API |
| 30 | * by Mauro Carvalho Chehab <mchehab@infradead.org> |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 31 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #include <linux/module.h> /* Modules */ |
| 42 | #include <linux/init.h> /* Initdata */ |
Peter Osterlund | fb911ee | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 43 | #include <linux/ioport.h> /* request_region */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/delay.h> /* udelay, msleep */ |
Mauro Carvalho Chehab | 2ab6529 | 2006-08-08 09:10:04 -0300 | [diff] [blame] | 45 | #include <linux/videodev2.h> /* kernel radio structs */ |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 46 | #include <linux/mutex.h> |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 47 | #include <linux/io.h> /* outb, outb_p */ |
Hans Verkuil | 9f1dfcc | 2012-02-29 05:50:27 -0300 | [diff] [blame] | 48 | #include <linux/slab.h> |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 49 | #include <media/v4l2-device.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 50 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 51 | #include "radio-isa.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 53 | MODULE_AUTHOR("C. van Schaik"); |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 54 | MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus."); |
| 55 | MODULE_LICENSE("GPL"); |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 56 | MODULE_VERSION("0.1.99"); |
Mauro Carvalho Chehab | 2ab6529 | 2006-08-08 09:10:04 -0300 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifndef CONFIG_RADIO_ZOLTRIX_PORT |
| 59 | #define CONFIG_RADIO_ZOLTRIX_PORT -1 |
| 60 | #endif |
| 61 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 62 | #define ZOLTRIX_MAX 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 64 | static int io[ZOLTRIX_MAX] = { [0] = CONFIG_RADIO_ZOLTRIX_PORT, |
| 65 | [1 ... (ZOLTRIX_MAX - 1)] = -1 }; |
| 66 | static int radio_nr[ZOLTRIX_MAX] = { [0 ... (ZOLTRIX_MAX - 1)] = -1 }; |
| 67 | |
| 68 | module_param_array(io, int, NULL, 0444); |
| 69 | MODULE_PARM_DESC(io, "I/O addresses of the Zoltrix Radio Plus card (0x20c or 0x30c)"); |
| 70 | module_param_array(radio_nr, int, NULL, 0444); |
| 71 | MODULE_PARM_DESC(radio_nr, "Radio device numbers"); |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 72 | |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 73 | struct zoltrix { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 74 | struct radio_isa_card isa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int curvol; |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 76 | bool muted; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 79 | static struct radio_isa_card *zoltrix_alloc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 81 | struct zoltrix *zol = kzalloc(sizeof(*zol), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 83 | return zol ? &zol->isa : NULL; |
| 84 | } |
| 85 | |
| 86 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 99 | outb(vol - 1, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | msleep(10); |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 101 | inb(isa->io + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 105 | /* tunes the radio to the desired frequency */ |
| 106 | static int zoltrix_s_frequency(struct radio_isa_card *isa, u32 freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 108 | struct zoltrix *zol = container_of(isa, struct zoltrix, isa); |
| 109 | struct v4l2_device *v4l2_dev = &isa->v4l2_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | unsigned long long bitmask, f, m; |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 111 | bool stereo = isa->stereo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | int i; |
| 113 | |
Alexey Klimov | b4be204 | 2008-10-09 13:46:59 -0300 | [diff] [blame] | 114 | if (freq == 0) { |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 115 | v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n"); |
Alexey Klimov | b4be204 | 2008-10-09 13:46:59 -0300 | [diff] [blame] | 116 | return -EINVAL; |
| 117 | } |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | m = (freq / 160 - 8800) * 2; |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 120 | f = (unsigned long long)m + 0x4d1c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | bitmask = 0xc480402c10080000ull; |
| 123 | i = 45; |
| 124 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 125 | outb(0, isa->io); |
| 126 | outb(0, isa->io); |
| 127 | inb(isa->io + 3); /* Zoltrix needs to be read to confirm */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 128 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 129 | outb(0x40, isa->io); |
| 130 | outb(0xc0, isa->io); |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 131 | |
| 132 | bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | while (i--) { |
| 134 | if ((bitmask & 0x8000000000000000ull) != 0) { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 135 | outb(0x80, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | udelay(50); |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 137 | outb(0x00, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | udelay(50); |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 139 | outb(0x80, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | udelay(50); |
| 141 | } else { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 142 | outb(0xc0, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | udelay(50); |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 144 | outb(0x40, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | udelay(50); |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 146 | outb(0xc0, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | udelay(50); |
| 148 | } |
| 149 | bitmask *= 2; |
| 150 | } |
| 151 | /* termination sequence */ |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 152 | outb(0x80, isa->io); |
| 153 | outb(0xc0, isa->io); |
| 154 | outb(0x40, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | udelay(1000); |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 156 | inb(isa->io + 2); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 157 | udelay(1000); |
| 158 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 159 | return zoltrix_s_mute_volume(isa, zol->muted, zol->curvol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* Get signal strength */ |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 163 | static u32 zoltrix_g_rxsubchans(struct radio_isa_card *isa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 165 | struct zoltrix *zol = container_of(isa, struct zoltrix, isa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | int a, b; |
| 167 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 168 | outb(0x00, isa->io); /* This stuff I found to do nothing */ |
| 169 | outb(zol->curvol, isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | msleep(20); |
| 171 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 172 | a = inb(isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | msleep(10); |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 174 | b = inb(isa->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 176 | return (a == b && a == 0xcf) ? |
| 177 | V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; |
| 178 | } |
| 179 | |
| 180 | static 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 Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (a != b) |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 194 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 196 | /* I found this out by playing with a binary scanner on the card io */ |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 197 | return (a == 0xcf || a == 0xdf || a == 0xef) ? 0xffff : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 200 | static int zoltrix_s_stereo(struct radio_isa_card *isa, bool stereo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 202 | return zoltrix_s_frequency(isa, isa->freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 205 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 214 | static const int zoltrix_ioports[] = { 0x20c, 0x30c }; |
| 215 | |
| 216 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | static int __init zoltrix_init(void) |
| 237 | { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 238 | return isa_register_driver(&zoltrix_driver.driver, ZOLTRIX_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 241 | static void __exit zoltrix_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Hans Verkuil | 6b39246cf | 2012-01-16 05:18:40 -0300 | [diff] [blame] | 243 | isa_unregister_driver(&zoltrix_driver.driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | module_init(zoltrix_init); |
Hans Verkuil | ec632c8 | 2009-03-06 13:55:34 -0300 | [diff] [blame] | 247 | module_exit(zoltrix_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |