Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * (C) 2001 Dimitromanolakis Apostolos <apdim@grecian.net> |
| 4 | * |
| 5 | * Based in the radio Maestro PCI driver. Actually it uses the same chip |
| 6 | * for radio but different pci controller. |
| 7 | * |
| 8 | * I didn't have any specs I reversed engineered the protocol from |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 9 | * the windows driver (radio.dll). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * The card uses the TEA5757 chip that includes a search function but it |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 12 | * is useless as I haven't found any way to read back the frequency. If |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * anybody does please mail me. |
| 14 | * |
| 15 | * For the pdf file see: |
| 16 | * http://www.semiconductors.philips.com/pip/TEA5757H/V1 |
| 17 | * |
| 18 | * |
| 19 | * CHANGES: |
| 20 | * 0.75b |
| 21 | * - better pci interface thanks to Francois Romieu <romieu@cogenit.fr> |
| 22 | * |
| 23 | * 0.75 |
| 24 | * - tiding up |
| 25 | * - removed support for multiple devices as it didn't work anyway |
| 26 | * |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 27 | * BUGS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * - card unmutes if you change frequency |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/ioport.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/sched.h> |
| 38 | #include <asm/io.h> |
| 39 | #include <asm/uaccess.h> |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 40 | #include <linux/mutex.h> |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/pci.h> |
| 43 | #include <linux/videodev.h> |
Mauro Carvalho Chehab | 5e87efa | 2006-06-05 10:26:32 -0300 | [diff] [blame] | 44 | #include <media/v4l2-common.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* version 0.75 Sun Feb 4 22:51:27 EET 2001 */ |
| 47 | #define DRIVER_VERSION "0.75" |
| 48 | |
| 49 | #ifndef PCI_VENDOR_ID_GUILLEMOT |
| 50 | #define PCI_VENDOR_ID_GUILLEMOT 0x5046 |
| 51 | #endif |
| 52 | |
| 53 | #ifndef PCI_DEVICE_ID_GUILLEMOT |
| 54 | #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001 |
| 55 | #endif |
| 56 | |
| 57 | |
| 58 | /* TEA5757 pin mappings */ |
| 59 | static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ; |
| 60 | |
| 61 | static int radio_nr = -1; |
| 62 | module_param(radio_nr, int, 0); |
| 63 | |
| 64 | |
| 65 | #define FREQ_LO 50*16000 |
| 66 | #define FREQ_HI 150*16000 |
| 67 | |
| 68 | #define FREQ_IF 171200 /* 10.7*16000 */ |
| 69 | #define FREQ_STEP 200 /* 12.5*16 */ |
| 70 | |
| 71 | #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\ |
| 72 | /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */ |
| 73 | |
| 74 | #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF) |
| 75 | |
| 76 | |
| 77 | static int radio_ioctl(struct inode *inode, struct file *file, |
| 78 | unsigned int cmd, unsigned long arg); |
| 79 | |
| 80 | static struct file_operations maxiradio_fops = { |
| 81 | .owner = THIS_MODULE, |
| 82 | .open = video_exclusive_open, |
| 83 | .release = video_exclusive_release, |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 84 | .ioctl = radio_ioctl, |
Arnd Bergmann | 0d0fbf8 | 2006-01-09 15:24:57 -0200 | [diff] [blame] | 85 | .compat_ioctl = v4l_compat_ioctl32, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | .llseek = no_llseek, |
| 87 | }; |
| 88 | static struct video_device maxiradio_radio = |
| 89 | { |
| 90 | .owner = THIS_MODULE, |
| 91 | .name = "Maxi Radio FM2000 radio", |
| 92 | .type = VID_TYPE_TUNER, |
| 93 | .hardware = VID_HARDWARE_SF16MI, |
| 94 | .fops = &maxiradio_fops, |
| 95 | }; |
| 96 | |
| 97 | static struct radio_device |
| 98 | { |
| 99 | __u16 io, /* base of radio io */ |
| 100 | muted, /* VIDEO_AUDIO_MUTE */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 101 | stereo, /* VIDEO_TUNER_STEREO_ON */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | tuned; /* signal strength (0 or 0xffff) */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | unsigned long freq; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 105 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 106 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } radio_unit = {0, 0, 0, 0, }; |
| 108 | |
| 109 | |
| 110 | static void outbit(unsigned long bit, __u16 io) |
| 111 | { |
| 112 | if(bit != 0) |
| 113 | { |
| 114 | outb( power|wren|data ,io); udelay(4); |
| 115 | outb( power|wren|data|clk ,io); udelay(4); |
| 116 | outb( power|wren|data ,io); udelay(4); |
| 117 | } |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 118 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | outb( power|wren ,io); udelay(4); |
| 121 | outb( power|wren|clk ,io); udelay(4); |
| 122 | outb( power|wren ,io); udelay(4); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static void turn_power(__u16 io, int p) |
| 127 | { |
| 128 | if(p != 0) outb(power, io); else outb(0,io); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static void set_freq(__u16 io, __u32 data) |
| 133 | { |
| 134 | unsigned long int si; |
| 135 | int bl; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | /* TEA5757 shift register bits (see pdf) */ |
| 138 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 139 | outbit(0,io); // 24 search |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | outbit(1,io); // 23 search up/down |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | outbit(0,io); // 22 stereo/mono |
| 143 | |
| 144 | outbit(0,io); // 21 band |
| 145 | outbit(0,io); // 20 band (only 00=FM works I think) |
| 146 | |
| 147 | outbit(0,io); // 19 port ? |
| 148 | outbit(0,io); // 18 port ? |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | outbit(0,io); // 17 search level |
| 151 | outbit(0,io); // 16 search level |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | si = 0x8000; |
| 154 | for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; } |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | outb(power,io); |
| 157 | } |
| 158 | |
| 159 | static int get_stereo(__u16 io) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 160 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | outb(power,io); udelay(4); |
| 162 | return !(inb(io) & mo_st); |
| 163 | } |
| 164 | |
| 165 | static int get_tune(__u16 io) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 166 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | outb(power+clk,io); udelay(4); |
| 168 | return !(inb(io) & mo_st); |
| 169 | } |
| 170 | |
| 171 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 172 | static inline int radio_function(struct inode *inode, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | unsigned int cmd, void *arg) |
| 174 | { |
| 175 | struct video_device *dev = video_devdata(file); |
| 176 | struct radio_device *card=dev->priv; |
| 177 | |
| 178 | switch(cmd) { |
| 179 | case VIDIOCGCAP: { |
| 180 | struct video_capability *v = arg; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | memset(v,0,sizeof(*v)); |
| 183 | strcpy(v->name, "Maxi Radio FM2000 radio"); |
| 184 | v->type=VID_TYPE_TUNER; |
| 185 | v->channels=v->audios=1; |
| 186 | return 0; |
| 187 | } |
| 188 | case VIDIOCGTUNER: { |
| 189 | struct video_tuner *v = arg; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if(v->tuner) |
| 192 | return -EINVAL; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | card->stereo = 0xffff * get_stereo(card->io); |
| 195 | card->tuned = 0xffff * get_tune(card->io); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | v->flags = VIDEO_TUNER_LOW | card->stereo; |
| 198 | v->signal = card->tuned; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | strcpy(v->name, "FM"); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | v->rangelow = FREQ_LO; |
| 203 | v->rangehigh = FREQ_HI; |
| 204 | v->mode = VIDEO_MODE_AUTO; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | case VIDIOCSTUNER: { |
| 209 | struct video_tuner *v = arg; |
| 210 | if(v->tuner!=0) |
| 211 | return -EINVAL; |
| 212 | return 0; |
| 213 | } |
| 214 | case VIDIOCGFREQ: { |
| 215 | unsigned long *freq = arg; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | *freq = card->freq; |
| 218 | return 0; |
| 219 | } |
| 220 | case VIDIOCSFREQ: { |
| 221 | unsigned long *freq = arg; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (*freq < FREQ_LO || *freq > FREQ_HI) |
| 224 | return -EINVAL; |
| 225 | card->freq = *freq; |
| 226 | set_freq(card->io, FREQ2BITS(card->freq)); |
| 227 | msleep(125); |
| 228 | return 0; |
| 229 | } |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 230 | case VIDIOCGAUDIO: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | struct video_audio *v = arg; |
| 232 | memset(v,0,sizeof(*v)); |
| 233 | strcpy(v->name, "Radio"); |
| 234 | v->flags=VIDEO_AUDIO_MUTABLE | card->muted; |
| 235 | v->mode=VIDEO_SOUND_STEREO; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 236 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | case VIDIOCSAUDIO: { |
| 240 | struct video_audio *v = arg; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if(v->audio) |
| 243 | return -EINVAL; |
| 244 | card->muted = v->flags & VIDEO_AUDIO_MUTE; |
| 245 | if(card->muted) |
| 246 | turn_power(card->io, 0); |
| 247 | else |
| 248 | set_freq(card->io, FREQ2BITS(card->freq)); |
| 249 | return 0; |
| 250 | } |
| 251 | case VIDIOCGUNIT: { |
| 252 | struct video_unit *v = arg; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | v->video=VIDEO_NO_UNIT; |
| 255 | v->vbi=VIDEO_NO_UNIT; |
| 256 | v->radio=dev->minor; |
| 257 | v->audio=0; |
| 258 | v->teletext=VIDEO_NO_UNIT; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 259 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | default: return -ENOIOCTLCMD; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | static int radio_ioctl(struct inode *inode, struct file *file, |
| 266 | unsigned int cmd, unsigned long arg) |
| 267 | { |
| 268 | struct video_device *dev = video_devdata(file); |
| 269 | struct radio_device *card=dev->priv; |
| 270 | int ret; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 271 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 272 | mutex_lock(&card->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | ret = video_usercopy(inode, file, cmd, arg, radio_function); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 274 | mutex_unlock(&card->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net"); |
| 279 | MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio."); |
| 280 | MODULE_LICENSE("GPL"); |
| 281 | |
| 282 | |
| 283 | static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 284 | { |
| 285 | if(!request_region(pci_resource_start(pdev, 0), |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 286 | pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) { |
| 287 | printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n"); |
| 288 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (pci_enable_device(pdev)) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 292 | goto err_out_free_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | radio_unit.io = pci_resource_start(pdev, 0); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 295 | mutex_init(&radio_unit.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | maxiradio_radio.priv = &radio_unit; |
| 297 | |
| 298 | if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 299 | printk("radio-maxiradio: can't register device!"); |
| 300 | goto err_out_free_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | printk(KERN_INFO "radio-maxiradio: version " |
| 304 | DRIVER_VERSION |
| 305 | " time " |
| 306 | __TIME__ " " |
| 307 | __DATE__ |
| 308 | "\n"); |
| 309 | |
| 310 | printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n", |
| 311 | radio_unit.io); |
| 312 | return 0; |
| 313 | |
| 314 | err_out_free_region: |
| 315 | release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0)); |
| 316 | err_out: |
| 317 | return -ENODEV; |
| 318 | } |
| 319 | |
| 320 | static void __devexit maxiradio_remove_one(struct pci_dev *pdev) |
| 321 | { |
| 322 | video_unregister_device(&maxiradio_radio); |
| 323 | release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0)); |
| 324 | } |
| 325 | |
| 326 | static struct pci_device_id maxiradio_pci_tbl[] = { |
| 327 | { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO, |
| 328 | PCI_ANY_ID, PCI_ANY_ID, }, |
| 329 | { 0,} |
| 330 | }; |
| 331 | |
| 332 | MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl); |
| 333 | |
| 334 | static struct pci_driver maxiradio_driver = { |
| 335 | .name = "radio-maxiradio", |
| 336 | .id_table = maxiradio_pci_tbl, |
| 337 | .probe = maxiradio_init_one, |
| 338 | .remove = __devexit_p(maxiradio_remove_one), |
| 339 | }; |
| 340 | |
| 341 | static int __init maxiradio_radio_init(void) |
| 342 | { |
Richard Knutsson | 9bfab8c | 2005-11-30 00:59:34 +0100 | [diff] [blame] | 343 | return pci_register_driver(&maxiradio_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void __exit maxiradio_radio_exit(void) |
| 347 | { |
| 348 | pci_unregister_driver(&maxiradio_driver); |
| 349 | } |
| 350 | |
| 351 | module_init(maxiradio_radio_init); |
| 352 | module_exit(maxiradio_radio_exit); |