xref: /linux/drivers/media/radio/radio-maxiradio.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
1 /*
2  * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
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
9  * the windows driver (radio.dll).
10  *
11  * The card uses the TEA5757 chip that includes a search function but it
12  * is useless as I haven't found any way to read back the frequency. If
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  *
27  * BUGS:
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>
40 #include <linux/mutex.h>
41 
42 #include <linux/pci.h>
43 #include <linux/videodev.h>
44 #include <media/v4l2-common.h>
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,
84 	.ioctl		= radio_ioctl,
85 	.compat_ioctl	= v4l_compat_ioctl32,
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 */
101 		stereo,	/* VIDEO_TUNER_STEREO_ON */
102 		tuned;	/* signal strength (0 or 0xffff) */
103 
104 	unsigned long freq;
105 
106 	struct mutex lock;
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 		}
118 	else
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;
136 
137 	/* TEA5757 shift register bits (see pdf) */
138 
139 	outbit(0,io); // 24  search
140 	outbit(1,io); // 23  search up/down
141 
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 ?
149 
150 	outbit(0,io); // 17  search level
151 	outbit(0,io); // 16  search level
152 
153 	si = 0x8000;
154 	for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
155 
156 	outb(power,io);
157 }
158 
159 static int get_stereo(__u16 io)
160 {
161 	outb(power,io); udelay(4);
162 	return !(inb(io) & mo_st);
163 }
164 
165 static int get_tune(__u16 io)
166 {
167 	outb(power+clk,io); udelay(4);
168 	return !(inb(io) & mo_st);
169 }
170 
171 
172 static inline int radio_function(struct inode *inode, struct file *file,
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;
181 
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;
190 
191 			if(v->tuner)
192 				return -EINVAL;
193 
194 			card->stereo = 0xffff * get_stereo(card->io);
195 			card->tuned = 0xffff * get_tune(card->io);
196 
197 			v->flags = VIDEO_TUNER_LOW | card->stereo;
198 			v->signal = card->tuned;
199 
200 			strcpy(v->name, "FM");
201 
202 			v->rangelow = FREQ_LO;
203 			v->rangehigh = FREQ_HI;
204 			v->mode = VIDEO_MODE_AUTO;
205 
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;
216 
217 			*freq = card->freq;
218 			return 0;
219 		}
220 		case VIDIOCSFREQ: {
221 			unsigned long *freq = arg;
222 
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 		}
230 		case VIDIOCGAUDIO: {
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;
236 			return 0;
237 		}
238 
239 		case VIDIOCSAUDIO: {
240 			struct video_audio *v = arg;
241 
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;
253 
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;
259 			return 0;
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;
271 
272 	mutex_lock(&card->lock);
273 	ret = video_usercopy(inode, file, cmd, arg, radio_function);
274 	mutex_unlock(&card->lock);
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),
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;
289 	}
290 
291 	if (pci_enable_device(pdev))
292 		goto err_out_free_region;
293 
294 	radio_unit.io = pci_resource_start(pdev, 0);
295 	mutex_init(&radio_unit.lock);
296 	maxiradio_radio.priv = &radio_unit;
297 
298 	if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
299 		printk("radio-maxiradio: can't register device!");
300 		goto err_out_free_region;
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 {
343 	return pci_register_driver(&maxiradio_driver);
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);
353