blob: fe2552569690f4d3b3dee86b9cd22b805b617f96 [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>
44
45/* version 0.75 Sun Feb 4 22:51:27 EET 2001 */
46#define DRIVER_VERSION "0.75"
47
48#ifndef PCI_VENDOR_ID_GUILLEMOT
49#define PCI_VENDOR_ID_GUILLEMOT 0x5046
50#endif
51
52#ifndef PCI_DEVICE_ID_GUILLEMOT
53#define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
54#endif
55
56
57/* TEA5757 pin mappings */
58static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
59
60static int radio_nr = -1;
61module_param(radio_nr, int, 0);
62
63
64#define FREQ_LO 50*16000
65#define FREQ_HI 150*16000
66
67#define FREQ_IF 171200 /* 10.7*16000 */
68#define FREQ_STEP 200 /* 12.5*16 */
69
70#define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
71 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
72
73#define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
74
75
76static int radio_ioctl(struct inode *inode, struct file *file,
77 unsigned int cmd, unsigned long arg);
78
79static struct file_operations maxiradio_fops = {
80 .owner = THIS_MODULE,
81 .open = video_exclusive_open,
82 .release = video_exclusive_release,
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030083 .ioctl = radio_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020084 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .llseek = no_llseek,
86};
87static struct video_device maxiradio_radio =
88{
89 .owner = THIS_MODULE,
90 .name = "Maxi Radio FM2000 radio",
91 .type = VID_TYPE_TUNER,
92 .hardware = VID_HARDWARE_SF16MI,
93 .fops = &maxiradio_fops,
94};
95
96static struct radio_device
97{
98 __u16 io, /* base of radio io */
99 muted, /* VIDEO_AUDIO_MUTE */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300100 stereo, /* VIDEO_TUNER_STEREO_ON */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 tuned; /* signal strength (0 or 0xffff) */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long freq;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300104
Ingo Molnar3593cab2006-02-07 06:49:14 -0200105 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106} radio_unit = {0, 0, 0, 0, };
107
108
109static void outbit(unsigned long bit, __u16 io)
110{
111 if(bit != 0)
112 {
113 outb( power|wren|data ,io); udelay(4);
114 outb( power|wren|data|clk ,io); udelay(4);
115 outb( power|wren|data ,io); udelay(4);
116 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300117 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 {
119 outb( power|wren ,io); udelay(4);
120 outb( power|wren|clk ,io); udelay(4);
121 outb( power|wren ,io); udelay(4);
122 }
123}
124
125static void turn_power(__u16 io, int p)
126{
127 if(p != 0) outb(power, io); else outb(0,io);
128}
129
130
131static void set_freq(__u16 io, __u32 data)
132{
133 unsigned long int si;
134 int bl;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* TEA5757 shift register bits (see pdf) */
137
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300138 outbit(0,io); // 24 search
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 outbit(1,io); // 23 search up/down
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 outbit(0,io); // 22 stereo/mono
142
143 outbit(0,io); // 21 band
144 outbit(0,io); // 20 band (only 00=FM works I think)
145
146 outbit(0,io); // 19 port ?
147 outbit(0,io); // 18 port ?
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 outbit(0,io); // 17 search level
150 outbit(0,io); // 16 search level
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 si = 0x8000;
153 for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 outb(power,io);
156}
157
158static int get_stereo(__u16 io)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 outb(power,io); udelay(4);
161 return !(inb(io) & mo_st);
162}
163
164static int get_tune(__u16 io)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 outb(power+clk,io); udelay(4);
167 return !(inb(io) & mo_st);
168}
169
170
Jesper Juhl77933d72005-07-27 11:46:09 -0700171static inline int radio_function(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int cmd, void *arg)
173{
174 struct video_device *dev = video_devdata(file);
175 struct radio_device *card=dev->priv;
176
177 switch(cmd) {
178 case VIDIOCGCAP: {
179 struct video_capability *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 memset(v,0,sizeof(*v));
182 strcpy(v->name, "Maxi Radio FM2000 radio");
183 v->type=VID_TYPE_TUNER;
184 v->channels=v->audios=1;
185 return 0;
186 }
187 case VIDIOCGTUNER: {
188 struct video_tuner *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if(v->tuner)
191 return -EINVAL;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 card->stereo = 0xffff * get_stereo(card->io);
194 card->tuned = 0xffff * get_tune(card->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 v->flags = VIDEO_TUNER_LOW | card->stereo;
197 v->signal = card->tuned;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 strcpy(v->name, "FM");
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 v->rangelow = FREQ_LO;
202 v->rangehigh = FREQ_HI;
203 v->mode = VIDEO_MODE_AUTO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 0;
206 }
207 case VIDIOCSTUNER: {
208 struct video_tuner *v = arg;
209 if(v->tuner!=0)
210 return -EINVAL;
211 return 0;
212 }
213 case VIDIOCGFREQ: {
214 unsigned long *freq = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *freq = card->freq;
217 return 0;
218 }
219 case VIDIOCSFREQ: {
220 unsigned long *freq = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (*freq < FREQ_LO || *freq > FREQ_HI)
223 return -EINVAL;
224 card->freq = *freq;
225 set_freq(card->io, FREQ2BITS(card->freq));
226 msleep(125);
227 return 0;
228 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300229 case VIDIOCGAUDIO: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct video_audio *v = arg;
231 memset(v,0,sizeof(*v));
232 strcpy(v->name, "Radio");
233 v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
234 v->mode=VIDEO_SOUND_STEREO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 case VIDIOCSAUDIO: {
239 struct video_audio *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if(v->audio)
242 return -EINVAL;
243 card->muted = v->flags & VIDEO_AUDIO_MUTE;
244 if(card->muted)
245 turn_power(card->io, 0);
246 else
247 set_freq(card->io, FREQ2BITS(card->freq));
248 return 0;
249 }
250 case VIDIOCGUNIT: {
251 struct video_unit *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 v->video=VIDEO_NO_UNIT;
254 v->vbi=VIDEO_NO_UNIT;
255 v->radio=dev->minor;
256 v->audio=0;
257 v->teletext=VIDEO_NO_UNIT;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 default: return -ENOIOCTLCMD;
261 }
262}
263
264static int radio_ioctl(struct inode *inode, struct file *file,
265 unsigned int cmd, unsigned long arg)
266{
267 struct video_device *dev = video_devdata(file);
268 struct radio_device *card=dev->priv;
269 int ret;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300270
Ingo Molnar3593cab2006-02-07 06:49:14 -0200271 mutex_lock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 ret = video_usercopy(inode, file, cmd, arg, radio_function);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200273 mutex_unlock(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return ret;
275}
276
277MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
278MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
279MODULE_LICENSE("GPL");
280
281
282static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
283{
284 if(!request_region(pci_resource_start(pdev, 0),
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300285 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
286 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
287 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 if (pci_enable_device(pdev))
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300291 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 radio_unit.io = pci_resource_start(pdev, 0);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200294 mutex_init(&radio_unit.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 maxiradio_radio.priv = &radio_unit;
296
297 if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300298 printk("radio-maxiradio: can't register device!");
299 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
302 printk(KERN_INFO "radio-maxiradio: version "
303 DRIVER_VERSION
304 " time "
305 __TIME__ " "
306 __DATE__
307 "\n");
308
309 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
310 radio_unit.io);
311 return 0;
312
313err_out_free_region:
314 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
315err_out:
316 return -ENODEV;
317}
318
319static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
320{
321 video_unregister_device(&maxiradio_radio);
322 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
323}
324
325static struct pci_device_id maxiradio_pci_tbl[] = {
326 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
327 PCI_ANY_ID, PCI_ANY_ID, },
328 { 0,}
329};
330
331MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
332
333static struct pci_driver maxiradio_driver = {
334 .name = "radio-maxiradio",
335 .id_table = maxiradio_pci_tbl,
336 .probe = maxiradio_init_one,
337 .remove = __devexit_p(maxiradio_remove_one),
338};
339
340static int __init maxiradio_radio_init(void)
341{
Richard Knutsson9bfab8c2005-11-30 00:59:34 +0100342 return pci_register_driver(&maxiradio_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345static void __exit maxiradio_radio_exit(void)
346{
347 pci_unregister_driver(&maxiradio_driver);
348}
349
350module_init(maxiradio_radio_init);
351module_exit(maxiradio_radio_exit);