blob: f93d7afe7304c97d1159967f20e5ee130c41f608 [file] [log] [blame]
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03001/*
2 * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (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 Chehab4286c6f2006-04-08 16:06:16 -03009 * the windows driver (radio.dll).
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * The card uses the TEA5757 chip that includes a search function but it
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030012 * is useless as I haven't found any way to read back the frequency. If
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 Chehab4286c6f2006-04-08 16:06:16 -030027 * BUGS:
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * - 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 Molnar3593cab2006-02-07 06:49:14 -020040#include <linux/mutex.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/pci.h>
43#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030044#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
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 */
59static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
60
61static int radio_nr = -1;
62module_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
77static int radio_ioctl(struct inode *inode, struct file *file,
78 unsigned int cmd, unsigned long arg);
79
80static struct file_operations maxiradio_fops = {
81 .owner = THIS_MODULE,
82 .open = video_exclusive_open,
83 .release = video_exclusive_release,
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030084 .ioctl = radio_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020085 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .llseek = no_llseek,
87};
88static 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
97static struct radio_device
98{
99 __u16 io, /* base of radio io */
100 muted, /* VIDEO_AUDIO_MUTE */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300101 stereo, /* VIDEO_TUNER_STEREO_ON */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 tuned; /* signal strength (0 or 0xffff) */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long freq;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300105
Ingo Molnar3593cab2006-02-07 06:49:14 -0200106 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107} radio_unit = {0, 0, 0, 0, };
108
109
110static 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 Chehab4286c6f2006-04-08 16:06:16 -0300118 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 {
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
126static void turn_power(__u16 io, int p)
127{
128 if(p != 0) outb(power, io); else outb(0,io);
129}
130
131
132static void set_freq(__u16 io, __u32 data)
133{
134 unsigned long int si;
135 int bl;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* TEA5757 shift register bits (see pdf) */
138
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300139 outbit(0,io); // 24 search
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 outbit(1,io); // 23 search up/down
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 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 Chehab4286c6f2006-04-08 16:06:16 -0300149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 outbit(0,io); // 17 search level
151 outbit(0,io); // 16 search level
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 si = 0x8000;
154 for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 outb(power,io);
157}
158
159static int get_stereo(__u16 io)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 outb(power,io); udelay(4);
162 return !(inb(io) & mo_st);
163}
164
165static int get_tune(__u16 io)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 outb(power+clk,io); udelay(4);
168 return !(inb(io) & mo_st);
169}
170
171
Jesper Juhl77933d72005-07-27 11:46:09 -0700172static inline int radio_function(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 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 Chehab4286c6f2006-04-08 16:06:16 -0300181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 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 Chehab4286c6f2006-04-08 16:06:16 -0300190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if(v->tuner)
192 return -EINVAL;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 card->stereo = 0xffff * get_stereo(card->io);
195 card->tuned = 0xffff * get_tune(card->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 v->flags = VIDEO_TUNER_LOW | card->stereo;
198 v->signal = card->tuned;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 strcpy(v->name, "FM");
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 v->rangelow = FREQ_LO;
203 v->rangehigh = FREQ_HI;
204 v->mode = VIDEO_MODE_AUTO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 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 Chehab4286c6f2006-04-08 16:06:16 -0300216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *freq = card->freq;
218 return 0;
219 }
220 case VIDIOCSFREQ: {
221 unsigned long *freq = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 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 Chehab4286c6f2006-04-08 16:06:16 -0300230 case VIDIOCGAUDIO: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 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 Chehab4286c6f2006-04-08 16:06:16 -0300236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 case VIDIOCSAUDIO: {
240 struct video_audio *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 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 Chehab4286c6f2006-04-08 16:06:16 -0300253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 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 Chehab4286c6f2006-04-08 16:06:16 -0300259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 default: return -ENOIOCTLCMD;
262 }
263}
264
265static 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 Chehab4286c6f2006-04-08 16:06:16 -0300271
Ingo Molnar3593cab2006-02-07 06:49:14 -0200272 mutex_lock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 ret = video_usercopy(inode, file, cmd, arg, radio_function);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200274 mutex_unlock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return ret;
276}
277
278MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
279MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
280MODULE_LICENSE("GPL");
281
282
283static 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 Chehab4286c6f2006-04-08 16:06:16 -0300286 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 Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 if (pci_enable_device(pdev))
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300292 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 radio_unit.io = pci_resource_start(pdev, 0);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200295 mutex_init(&radio_unit.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 maxiradio_radio.priv = &radio_unit;
297
298 if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300299 printk("radio-maxiradio: can't register device!");
300 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
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
314err_out_free_region:
315 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
316err_out:
317 return -ENODEV;
318}
319
320static 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
326static 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
332MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
333
334static 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
341static int __init maxiradio_radio_init(void)
342{
Richard Knutsson9bfab8c2005-11-30 00:59:34 +0100343 return pci_register_driver(&maxiradio_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346static void __exit maxiradio_radio_exit(void)
347{
348 pci_unregister_driver(&maxiradio_driver);
349}
350
351module_init(maxiradio_radio_init);
352module_exit(maxiradio_radio_exit);