Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Maestro PCI sound card radio driver for Linux support |
| 2 | * (c) 2000 A. Tlalka, atlka@pg.gda.pl |
| 3 | * Notes on the hardware |
| 4 | * |
| 5 | * + Frequency control is done digitally |
| 6 | * + No volume control - only mute/unmute - you have to use Aux line volume |
| 7 | * control on Maestro card to set the volume |
| 8 | * + Radio status (tuned/not_tuned and stereo/mono) is valid some time after |
| 9 | * frequency setting (>100ms) and only when the radio is unmuted. |
| 10 | * version 0.02 |
| 11 | * + io port is automatically detected - only the first radio is used |
| 12 | * version 0.03 |
| 13 | * + thread access locking additions |
| 14 | * version 0.04 |
| 15 | * + code improvements |
| 16 | * + VIDEO_TUNER_LOW is permanent |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/ioport.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <asm/io.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <asm/semaphore.h> |
| 27 | #include <linux/pci.h> |
| 28 | #include <linux/videodev.h> |
| 29 | |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame^] | 30 | #define DRIVER_VERSION "0.05" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */ |
| 33 | |
| 34 | #define IO_MASK 4 /* mask register offset from GPIO_DATA |
| 35 | bits 1=unmask write to given bit */ |
| 36 | #define IO_DIR 8 /* direction register offset from GPIO_DATA |
| 37 | bits 0/1=read/write direction */ |
| 38 | |
| 39 | #define GPIO6 0x0040 /* mask bits for GPIO lines */ |
| 40 | #define GPIO7 0x0080 |
| 41 | #define GPIO8 0x0100 |
| 42 | #define GPIO9 0x0200 |
| 43 | |
| 44 | #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */ |
| 45 | #define STR_CLK GPIO7 |
| 46 | #define STR_WREN GPIO8 |
| 47 | #define STR_MOST GPIO9 |
| 48 | |
| 49 | #define FREQ_LO 50*16000 |
| 50 | #define FREQ_HI 150*16000 |
| 51 | |
| 52 | #define FREQ_IF 171200 /* 10.7*16000 */ |
| 53 | #define FREQ_STEP 200 /* 12.5*16 */ |
| 54 | |
| 55 | #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\ |
| 56 | /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */ |
| 57 | |
| 58 | #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF) |
| 59 | |
| 60 | static int radio_nr = -1; |
| 61 | module_param(radio_nr, int, 0); |
| 62 | |
| 63 | static int radio_ioctl(struct inode *inode, struct file *file, |
| 64 | unsigned int cmd, unsigned long arg); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame^] | 65 | static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent); |
| 66 | static void maestro_remove(struct pci_dev *pdev); |
| 67 | |
| 68 | static struct pci_device_id maestro_r_pci_tbl[] = { |
| 69 | { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968), |
| 70 | .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8, |
| 71 | .class_mask = 0xffff00 }, |
| 72 | { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978), |
| 73 | .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8, |
| 74 | .class_mask = 0xffff00 }, |
| 75 | { 0 } |
| 76 | }; |
| 77 | MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl); |
| 78 | |
| 79 | static struct pci_driver maestro_r_driver = { |
| 80 | .name = "maestro_radio", |
| 81 | .id_table = maestro_r_pci_tbl, |
| 82 | .probe = maestro_probe, |
| 83 | .remove = __devexit_p(maestro_remove), |
| 84 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | static struct file_operations maestro_fops = { |
| 87 | .owner = THIS_MODULE, |
| 88 | .open = video_exclusive_open, |
| 89 | .release = video_exclusive_release, |
| 90 | .ioctl = radio_ioctl, |
Arnd Bergmann | 0d0fbf8 | 2006-01-09 15:24:57 -0200 | [diff] [blame] | 91 | .compat_ioctl = v4l_compat_ioctl32, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | .llseek = no_llseek, |
| 93 | }; |
| 94 | |
| 95 | static struct video_device maestro_radio= |
| 96 | { |
| 97 | .owner = THIS_MODULE, |
| 98 | .name = "Maestro radio", |
| 99 | .type = VID_TYPE_TUNER, |
| 100 | .hardware = VID_HARDWARE_SF16MI, |
| 101 | .fops = &maestro_fops, |
| 102 | }; |
| 103 | |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame^] | 104 | struct radio_device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | __u16 io, /* base of Maestro card radio io (GPIO_DATA)*/ |
| 107 | muted, /* VIDEO_AUDIO_MUTE */ |
| 108 | stereo, /* VIDEO_TUNER_STEREO_ON */ |
| 109 | tuned; /* signal strength (0 or 0xffff) */ |
| 110 | struct semaphore lock; |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame^] | 111 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | static __u32 radio_bits_get(struct radio_device *dev) |
| 114 | { |
| 115 | register __u16 io=dev->io, l, rdata; |
| 116 | register __u32 data=0; |
| 117 | __u16 omask; |
| 118 | omask = inw(io + IO_MASK); |
| 119 | outw(~(STR_CLK | STR_WREN), io + IO_MASK); |
| 120 | outw(0, io); |
| 121 | udelay(16); |
| 122 | |
| 123 | for (l=24;l--;) { |
| 124 | outw(STR_CLK, io); /* HI state */ |
| 125 | udelay(2); |
| 126 | if(!l) |
| 127 | dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff; |
| 128 | outw(0, io); /* LO state */ |
| 129 | udelay(2); |
| 130 | data <<= 1; /* shift data */ |
| 131 | rdata = inw(io); |
| 132 | if(!l) |
| 133 | dev->stereo = rdata & STR_MOST ? |
| 134 | 0 : VIDEO_TUNER_STEREO_ON; |
| 135 | else |
| 136 | if(rdata & STR_DATA) |
| 137 | data++; |
| 138 | udelay(2); |
| 139 | } |
| 140 | if(dev->muted) |
| 141 | outw(STR_WREN, io); |
| 142 | udelay(4); |
| 143 | outw(omask, io + IO_MASK); |
| 144 | return data & 0x3ffe; |
| 145 | } |
| 146 | |
| 147 | static void radio_bits_set(struct radio_device *dev, __u32 data) |
| 148 | { |
| 149 | register __u16 io=dev->io, l, bits; |
| 150 | __u16 omask, odir; |
| 151 | omask = inw(io + IO_MASK); |
| 152 | odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN); |
| 153 | outw(odir | STR_DATA, io + IO_DIR); |
| 154 | outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK); |
| 155 | udelay(16); |
| 156 | for (l=25;l;l--) { |
| 157 | bits = ((data >> 18) & STR_DATA) | STR_WREN ; |
| 158 | data <<= 1; /* shift data */ |
| 159 | outw(bits, io); /* start strobe */ |
| 160 | udelay(2); |
| 161 | outw(bits | STR_CLK, io); /* HI level */ |
| 162 | udelay(2); |
| 163 | outw(bits, io); /* LO level */ |
| 164 | udelay(4); |
| 165 | } |
| 166 | if(!dev->muted) |
| 167 | outw(0, io); |
| 168 | udelay(4); |
| 169 | outw(omask, io + IO_MASK); |
| 170 | outw(odir, io + IO_DIR); |
| 171 | msleep(125); |
| 172 | } |
| 173 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 174 | static inline int radio_function(struct inode *inode, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | unsigned int cmd, void *arg) |
| 176 | { |
| 177 | struct video_device *dev = video_devdata(file); |
| 178 | struct radio_device *card=dev->priv; |
| 179 | |
| 180 | switch(cmd) { |
| 181 | case VIDIOCGCAP: { |
| 182 | struct video_capability *v = arg; |
| 183 | memset(v,0,sizeof(*v)); |
| 184 | strcpy(v->name, "Maestro radio"); |
| 185 | v->type=VID_TYPE_TUNER; |
| 186 | v->channels=v->audios=1; |
| 187 | return 0; |
| 188 | } |
| 189 | case VIDIOCGTUNER: { |
| 190 | struct video_tuner *v = arg; |
| 191 | if(v->tuner) |
| 192 | return -EINVAL; |
| 193 | (void)radio_bits_get(card); |
| 194 | v->flags = VIDEO_TUNER_LOW | card->stereo; |
| 195 | v->signal = card->tuned; |
| 196 | strcpy(v->name, "FM"); |
| 197 | v->rangelow = FREQ_LO; |
| 198 | v->rangehigh = FREQ_HI; |
| 199 | v->mode = VIDEO_MODE_AUTO; |
| 200 | return 0; |
| 201 | } |
| 202 | case VIDIOCSTUNER: { |
| 203 | struct video_tuner *v = arg; |
| 204 | if(v->tuner!=0) |
| 205 | return -EINVAL; |
| 206 | return 0; |
| 207 | } |
| 208 | case VIDIOCGFREQ: { |
| 209 | unsigned long *freq = arg; |
| 210 | *freq = BITS2FREQ(radio_bits_get(card)); |
| 211 | return 0; |
| 212 | } |
| 213 | case VIDIOCSFREQ: { |
| 214 | unsigned long *freq = arg; |
| 215 | if (*freq<FREQ_LO || *freq>FREQ_HI ) |
| 216 | return -EINVAL; |
| 217 | radio_bits_set(card, FREQ2BITS(*freq)); |
| 218 | return 0; |
| 219 | } |
| 220 | case VIDIOCGAUDIO: { |
| 221 | struct video_audio *v = arg; |
| 222 | memset(v,0,sizeof(*v)); |
| 223 | strcpy(v->name, "Radio"); |
| 224 | v->flags=VIDEO_AUDIO_MUTABLE | card->muted; |
| 225 | v->mode=VIDEO_SOUND_STEREO; |
| 226 | return 0; |
| 227 | } |
| 228 | case VIDIOCSAUDIO: { |
| 229 | struct video_audio *v = arg; |
| 230 | if(v->audio) |
| 231 | return -EINVAL; |
| 232 | { |
| 233 | register __u16 io=card->io; |
| 234 | register __u16 omask = inw(io + IO_MASK); |
| 235 | outw(~STR_WREN, io + IO_MASK); |
| 236 | outw((card->muted = v->flags & VIDEO_AUDIO_MUTE) |
| 237 | ? STR_WREN : 0, io); |
| 238 | udelay(4); |
| 239 | outw(omask, io + IO_MASK); |
| 240 | msleep(125); |
| 241 | return 0; |
| 242 | } |
| 243 | } |
| 244 | case VIDIOCGUNIT: { |
| 245 | struct video_unit *v = arg; |
| 246 | v->video=VIDEO_NO_UNIT; |
| 247 | v->vbi=VIDEO_NO_UNIT; |
| 248 | v->radio=dev->minor; |
| 249 | v->audio=0; |
| 250 | v->teletext=VIDEO_NO_UNIT; |
| 251 | return 0; |
| 252 | } |
| 253 | default: return -ENOIOCTLCMD; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static int radio_ioctl(struct inode *inode, struct file *file, |
| 258 | unsigned int cmd, unsigned long arg) |
| 259 | { |
| 260 | struct video_device *dev = video_devdata(file); |
| 261 | struct radio_device *card=dev->priv; |
| 262 | int ret; |
| 263 | |
| 264 | down(&card->lock); |
| 265 | ret = video_usercopy(inode, file, cmd, arg, radio_function); |
| 266 | up(&card->lock); |
| 267 | return ret; |
| 268 | } |
| 269 | |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame^] | 270 | static __u16 __devinit radio_power_on(struct radio_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | register __u16 io=dev->io; |
| 273 | register __u32 ofreq; |
| 274 | __u16 omask, odir; |
| 275 | omask = inw(io + IO_MASK); |
| 276 | odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN); |
| 277 | outw(odir & ~STR_WREN, io + IO_DIR); |
| 278 | dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE; |
| 279 | outw(odir, io + IO_DIR); |
| 280 | outw(~(STR_WREN | STR_CLK), io + IO_MASK); |
| 281 | outw(dev->muted ? 0 : STR_WREN, io); |
| 282 | udelay(16); |
| 283 | outw(omask, io + IO_MASK); |
| 284 | ofreq = radio_bits_get(dev); |
| 285 | if((ofreq<FREQ2BITS(FREQ_LO)) || (ofreq>FREQ2BITS(FREQ_HI))) |
| 286 | ofreq = FREQ2BITS(FREQ_LO); |
| 287 | radio_bits_set(dev, ofreq); |
| 288 | return (ofreq == radio_bits_get(dev)); |
| 289 | } |
| 290 | |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame^] | 291 | static int __devinit maestro_probe(struct pci_dev *pdev, |
| 292 | const struct pci_device_id *ent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame^] | 294 | struct radio_device *radio_unit; |
| 295 | struct video_device *maestro_radio_inst; |
| 296 | int retval; |
| 297 | |
| 298 | retval = pci_enable_device(pdev); |
| 299 | if (retval) { |
| 300 | dev_err(&pdev->dev, "enabling pci device failed!\n"); |
| 301 | goto err; |
| 302 | } |
| 303 | |
| 304 | retval = -ENOMEM; |
| 305 | |
| 306 | radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL); |
| 307 | if (radio_unit == NULL) { |
| 308 | dev_err(&pdev->dev, "not enough memory\n"); |
| 309 | goto err; |
| 310 | } |
| 311 | |
| 312 | radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA; |
| 313 | init_MUTEX(&radio_unit->lock); |
| 314 | |
| 315 | maestro_radio_inst = video_device_alloc(); |
| 316 | if (maestro_radio_inst == NULL) { |
| 317 | dev_err(&pdev->dev, "not enough memory\n"); |
| 318 | goto errfr; |
| 319 | } |
| 320 | |
| 321 | memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio)); |
| 322 | video_set_drvdata(maestro_radio_inst, radio_unit); |
| 323 | pci_set_drvdata(pdev, maestro_radio_inst); |
| 324 | |
| 325 | retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO, |
| 326 | radio_nr); |
| 327 | if (retval) { |
| 328 | printk(KERN_ERR "can't register video device!\n"); |
| 329 | goto errfr1; |
| 330 | } |
| 331 | |
| 332 | if (!radio_power_on(radio_unit)) { |
| 333 | retval = -EIO; |
| 334 | goto errunr; |
| 335 | } |
| 336 | |
| 337 | dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " " |
| 338 | __DATE__ "\n"); |
| 339 | dev_info(&pdev->dev, "radio chip initialized\n"); |
| 340 | |
| 341 | return 0; |
| 342 | errunr: |
| 343 | video_unregister_device(maestro_radio_inst); |
| 344 | errfr1: |
| 345 | kfree(maestro_radio_inst); |
| 346 | errfr: |
| 347 | kfree(radio_unit); |
| 348 | err: |
| 349 | return retval; |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame^] | 353 | static void __devexit maestro_remove(struct pci_dev *pdev) |
| 354 | { |
| 355 | struct video_device *vdev = pci_get_drvdata(pdev); |
| 356 | |
| 357 | video_unregister_device(vdev); |
| 358 | } |
| 359 | |
| 360 | MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl"); |
| 361 | MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio."); |
| 362 | MODULE_LICENSE("GPL"); |
| 363 | |
| 364 | static int __init maestro_radio_init(void) |
| 365 | { |
| 366 | int retval = pci_register_driver(&maestro_r_driver); |
| 367 | |
| 368 | if (retval) |
| 369 | printk(KERN_ERR "error during registration pci driver\n"); |
| 370 | |
| 371 | return retval; |
| 372 | } |
| 373 | |
| 374 | static void __exit maestro_radio_exit(void) |
| 375 | { |
| 376 | pci_unregister_driver(&maestro_r_driver); |
| 377 | } |
| 378 | |
| 379 | module_init(maestro_radio_init); |
| 380 | module_exit(maestro_radio_exit); |