Commit | Line | Data |
---|---|---|
4286c6f6 MCC |
1 | /* |
2 | * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux | |
1da177e4 LT |
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 | |
4286c6f6 | 9 | * the windows driver (radio.dll). |
1da177e4 LT |
10 | * |
11 | * The card uses the TEA5757 chip that includes a search function but it | |
4286c6f6 | 12 | * is useless as I haven't found any way to read back the frequency. If |
1da177e4 LT |
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 | * | |
e84fef6b | 23 | * 0.75 Sun Feb 4 22:51:27 EET 2001 |
1da177e4 LT |
24 | * - tiding up |
25 | * - removed support for multiple devices as it didn't work anyway | |
26 | * | |
4286c6f6 | 27 | * BUGS: |
1da177e4 LT |
28 | * - card unmutes if you change frequency |
29 | * | |
06470ed6 MCC |
30 | * (c) 2006, 2007 by Mauro Carvalho Chehab <mchehab@infradead.org>: |
31 | * - Conversion to V4L2 API | |
32 | * - Uses video_ioctl2 for parsing and to add debug support | |
1da177e4 LT |
33 | */ |
34 | ||
35 | ||
36 | #include <linux/module.h> | |
37 | #include <linux/init.h> | |
38 | #include <linux/ioport.h> | |
39 | #include <linux/delay.h> | |
1da177e4 LT |
40 | #include <asm/io.h> |
41 | #include <asm/uaccess.h> | |
3593cab5 IM |
42 | #include <linux/mutex.h> |
43 | ||
1da177e4 | 44 | #include <linux/pci.h> |
e84fef6b | 45 | #include <linux/videodev2.h> |
5e87efa3 | 46 | #include <media/v4l2-common.h> |
1da177e4 | 47 | |
e84fef6b MCC |
48 | #define DRIVER_VERSION "0.76" |
49 | ||
50 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ | |
51 | #define RADIO_VERSION KERNEL_VERSION(0,7,6) | |
52 | ||
53 | static struct v4l2_queryctrl radio_qctrl[] = { | |
54 | { | |
55 | .id = V4L2_CID_AUDIO_MUTE, | |
56 | .name = "Mute", | |
57 | .minimum = 0, | |
58 | .maximum = 1, | |
59 | .default_value = 1, | |
60 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
61 | } | |
62 | }; | |
1da177e4 LT |
63 | |
64 | #ifndef PCI_VENDOR_ID_GUILLEMOT | |
65 | #define PCI_VENDOR_ID_GUILLEMOT 0x5046 | |
66 | #endif | |
67 | ||
68 | #ifndef PCI_DEVICE_ID_GUILLEMOT | |
69 | #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001 | |
70 | #endif | |
71 | ||
72 | ||
73 | /* TEA5757 pin mappings */ | |
74 | static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ; | |
75 | ||
76 | static int radio_nr = -1; | |
77 | module_param(radio_nr, int, 0); | |
78 | ||
79 | ||
80 | #define FREQ_LO 50*16000 | |
81 | #define FREQ_HI 150*16000 | |
82 | ||
83 | #define FREQ_IF 171200 /* 10.7*16000 */ | |
84 | #define FREQ_STEP 200 /* 12.5*16 */ | |
85 | ||
86 | #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\ | |
87 | /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */ | |
88 | ||
89 | #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF) | |
90 | ||
91 | ||
fa027c2a | 92 | static const struct file_operations maxiradio_fops = { |
1da177e4 LT |
93 | .owner = THIS_MODULE, |
94 | .open = video_exclusive_open, | |
95 | .release = video_exclusive_release, | |
06470ed6 | 96 | .ioctl = video_ioctl2, |
0d0fbf81 | 97 | .compat_ioctl = v4l_compat_ioctl32, |
1da177e4 LT |
98 | .llseek = no_llseek, |
99 | }; | |
1da177e4 LT |
100 | |
101 | static struct radio_device | |
102 | { | |
103 | __u16 io, /* base of radio io */ | |
104 | muted, /* VIDEO_AUDIO_MUTE */ | |
4286c6f6 | 105 | stereo, /* VIDEO_TUNER_STEREO_ON */ |
1da177e4 | 106 | tuned; /* signal strength (0 or 0xffff) */ |
4286c6f6 | 107 | |
1da177e4 | 108 | unsigned long freq; |
4286c6f6 | 109 | |
3593cab5 | 110 | struct mutex lock; |
1da177e4 LT |
111 | } radio_unit = {0, 0, 0, 0, }; |
112 | ||
113 | ||
114 | static void outbit(unsigned long bit, __u16 io) | |
115 | { | |
116 | if(bit != 0) | |
117 | { | |
118 | outb( power|wren|data ,io); udelay(4); | |
119 | outb( power|wren|data|clk ,io); udelay(4); | |
120 | outb( power|wren|data ,io); udelay(4); | |
121 | } | |
4286c6f6 | 122 | else |
1da177e4 LT |
123 | { |
124 | outb( power|wren ,io); udelay(4); | |
125 | outb( power|wren|clk ,io); udelay(4); | |
126 | outb( power|wren ,io); udelay(4); | |
127 | } | |
128 | } | |
129 | ||
130 | static void turn_power(__u16 io, int p) | |
131 | { | |
132 | if(p != 0) outb(power, io); else outb(0,io); | |
133 | } | |
134 | ||
135 | ||
136 | static void set_freq(__u16 io, __u32 data) | |
137 | { | |
138 | unsigned long int si; | |
139 | int bl; | |
4286c6f6 | 140 | |
1da177e4 LT |
141 | /* TEA5757 shift register bits (see pdf) */ |
142 | ||
4286c6f6 | 143 | outbit(0,io); // 24 search |
1da177e4 | 144 | outbit(1,io); // 23 search up/down |
4286c6f6 | 145 | |
1da177e4 LT |
146 | outbit(0,io); // 22 stereo/mono |
147 | ||
148 | outbit(0,io); // 21 band | |
149 | outbit(0,io); // 20 band (only 00=FM works I think) | |
150 | ||
151 | outbit(0,io); // 19 port ? | |
152 | outbit(0,io); // 18 port ? | |
4286c6f6 | 153 | |
1da177e4 LT |
154 | outbit(0,io); // 17 search level |
155 | outbit(0,io); // 16 search level | |
4286c6f6 | 156 | |
1da177e4 LT |
157 | si = 0x8000; |
158 | for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; } | |
4286c6f6 | 159 | |
1da177e4 LT |
160 | outb(power,io); |
161 | } | |
162 | ||
163 | static int get_stereo(__u16 io) | |
4286c6f6 | 164 | { |
1da177e4 LT |
165 | outb(power,io); udelay(4); |
166 | return !(inb(io) & mo_st); | |
167 | } | |
168 | ||
169 | static int get_tune(__u16 io) | |
4286c6f6 | 170 | { |
1da177e4 LT |
171 | outb(power+clk,io); udelay(4); |
172 | return !(inb(io) & mo_st); | |
173 | } | |
174 | ||
175 | ||
06470ed6 MCC |
176 | static int vidioc_querycap (struct file *file, void *priv, |
177 | struct v4l2_capability *v) | |
178 | { | |
179 | strlcpy(v->driver, "radio-maxiradio", sizeof (v->driver)); | |
180 | strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof (v->card)); | |
181 | sprintf(v->bus_info,"ISA"); | |
182 | v->version = RADIO_VERSION; | |
183 | v->capabilities = V4L2_CAP_TUNER; | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | static int vidioc_g_tuner (struct file *file, void *priv, | |
189 | struct v4l2_tuner *v) | |
1da177e4 LT |
190 | { |
191 | struct video_device *dev = video_devdata(file); | |
192 | struct radio_device *card=dev->priv; | |
193 | ||
06470ed6 MCC |
194 | if (v->index > 0) |
195 | return -EINVAL; | |
e84fef6b | 196 | |
06470ed6 MCC |
197 | memset(v,0,sizeof(*v)); |
198 | strcpy(v->name, "FM"); | |
199 | v->type = V4L2_TUNER_RADIO; | |
4286c6f6 | 200 | |
06470ed6 MCC |
201 | v->rangelow=FREQ_LO; |
202 | v->rangehigh=FREQ_HI; | |
203 | v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO; | |
204 | v->capability=V4L2_TUNER_CAP_LOW; | |
205 | if(get_stereo(card->io)) | |
206 | v->audmode = V4L2_TUNER_MODE_STEREO; | |
207 | else | |
208 | v->audmode = V4L2_TUNER_MODE_MONO; | |
209 | v->signal=0xffff*get_tune(card->io); | |
4286c6f6 | 210 | |
06470ed6 MCC |
211 | return 0; |
212 | } | |
e84fef6b | 213 | |
06470ed6 MCC |
214 | static int vidioc_s_tuner (struct file *file, void *priv, |
215 | struct v4l2_tuner *v) | |
216 | { | |
217 | if (v->index > 0) | |
218 | return -EINVAL; | |
4286c6f6 | 219 | |
06470ed6 MCC |
220 | return 0; |
221 | } | |
e84fef6b | 222 | |
140dcc46 MCC |
223 | static int vidioc_g_audio (struct file *file, void *priv, |
224 | struct v4l2_audio *a) | |
225 | { | |
226 | if (a->index > 1) | |
227 | return -EINVAL; | |
228 | ||
229 | strcpy(a->name, "Radio"); | |
230 | a->capability = V4L2_AUDCAP_STEREO; | |
231 | return 0; | |
232 | } | |
233 | ||
234 | static int vidioc_s_audio (struct file *file, void *priv, | |
235 | struct v4l2_audio *a) | |
236 | { | |
237 | if (a->index != 0) | |
238 | return -EINVAL; | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
06470ed6 MCC |
243 | static int vidioc_s_frequency (struct file *file, void *priv, |
244 | struct v4l2_frequency *f) | |
245 | { | |
246 | struct video_device *dev = video_devdata(file); | |
247 | struct radio_device *card=dev->priv; | |
4286c6f6 | 248 | |
06470ed6 MCC |
249 | if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) |
250 | return -EINVAL; | |
4286c6f6 | 251 | |
06470ed6 MCC |
252 | card->freq = f->frequency; |
253 | set_freq(card->io, FREQ2BITS(card->freq)); | |
254 | msleep(125); | |
e84fef6b | 255 | |
06470ed6 MCC |
256 | return 0; |
257 | } | |
4286c6f6 | 258 | |
06470ed6 MCC |
259 | static int vidioc_g_frequency (struct file *file, void *priv, |
260 | struct v4l2_frequency *f) | |
261 | { | |
262 | struct video_device *dev = video_devdata(file); | |
263 | struct radio_device *card=dev->priv; | |
4286c6f6 | 264 | |
06470ed6 MCC |
265 | f->type = V4L2_TUNER_RADIO; |
266 | f->frequency = card->freq; | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
271 | static int vidioc_queryctrl (struct file *file, void *priv, | |
272 | struct v4l2_queryctrl *qc) | |
273 | { | |
274 | int i; | |
275 | ||
276 | for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) { | |
277 | if (qc->id && qc->id == radio_qctrl[i].id) { | |
278 | memcpy(qc, &(radio_qctrl[i]), sizeof(*qc)); | |
279 | return (0); | |
e84fef6b | 280 | } |
06470ed6 MCC |
281 | } |
282 | return -EINVAL; | |
283 | } | |
e84fef6b | 284 | |
06470ed6 MCC |
285 | static int vidioc_g_ctrl (struct file *file, void *priv, |
286 | struct v4l2_control *ctrl) | |
287 | { | |
288 | struct video_device *dev = video_devdata(file); | |
289 | struct radio_device *card=dev->priv; | |
e84fef6b | 290 | |
06470ed6 MCC |
291 | switch (ctrl->id) { |
292 | case V4L2_CID_AUDIO_MUTE: | |
293 | ctrl->value=card->muted; | |
294 | return (0); | |
1da177e4 | 295 | } |
06470ed6 | 296 | return -EINVAL; |
1da177e4 LT |
297 | } |
298 | ||
06470ed6 MCC |
299 | static int vidioc_s_ctrl (struct file *file, void *priv, |
300 | struct v4l2_control *ctrl) | |
1da177e4 LT |
301 | { |
302 | struct video_device *dev = video_devdata(file); | |
303 | struct radio_device *card=dev->priv; | |
4286c6f6 | 304 | |
06470ed6 MCC |
305 | switch (ctrl->id) { |
306 | case V4L2_CID_AUDIO_MUTE: | |
307 | card->muted = ctrl->value; | |
308 | if(card->muted) | |
309 | turn_power(card->io, 0); | |
310 | else | |
311 | set_freq(card->io, FREQ2BITS(card->freq)); | |
312 | return 0; | |
313 | } | |
314 | return -EINVAL; | |
1da177e4 LT |
315 | } |
316 | ||
06470ed6 MCC |
317 | static struct video_device maxiradio_radio = |
318 | { | |
319 | .owner = THIS_MODULE, | |
320 | .name = "Maxi Radio FM2000 radio", | |
321 | .type = VID_TYPE_TUNER, | |
322 | .fops = &maxiradio_fops, | |
323 | ||
324 | .vidioc_querycap = vidioc_querycap, | |
325 | .vidioc_g_tuner = vidioc_g_tuner, | |
326 | .vidioc_s_tuner = vidioc_s_tuner, | |
140dcc46 MCC |
327 | .vidioc_g_audio = vidioc_g_audio, |
328 | .vidioc_s_audio = vidioc_s_audio, | |
06470ed6 MCC |
329 | .vidioc_g_frequency = vidioc_g_frequency, |
330 | .vidioc_s_frequency = vidioc_s_frequency, | |
331 | .vidioc_queryctrl = vidioc_queryctrl, | |
332 | .vidioc_g_ctrl = vidioc_g_ctrl, | |
333 | .vidioc_s_ctrl = vidioc_s_ctrl, | |
1da177e4 | 334 | |
06470ed6 | 335 | }; |
1da177e4 LT |
336 | |
337 | static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) | |
338 | { | |
339 | if(!request_region(pci_resource_start(pdev, 0), | |
4286c6f6 MCC |
340 | pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) { |
341 | printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n"); | |
342 | goto err_out; | |
1da177e4 LT |
343 | } |
344 | ||
345 | if (pci_enable_device(pdev)) | |
4286c6f6 | 346 | goto err_out_free_region; |
1da177e4 LT |
347 | |
348 | radio_unit.io = pci_resource_start(pdev, 0); | |
3593cab5 | 349 | mutex_init(&radio_unit.lock); |
1da177e4 LT |
350 | maxiradio_radio.priv = &radio_unit; |
351 | ||
352 | if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) { | |
4286c6f6 MCC |
353 | printk("radio-maxiradio: can't register device!"); |
354 | goto err_out_free_region; | |
1da177e4 LT |
355 | } |
356 | ||
357 | printk(KERN_INFO "radio-maxiradio: version " | |
358 | DRIVER_VERSION | |
359 | " time " | |
360 | __TIME__ " " | |
361 | __DATE__ | |
362 | "\n"); | |
363 | ||
364 | printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n", | |
365 | radio_unit.io); | |
366 | return 0; | |
367 | ||
368 | err_out_free_region: | |
369 | release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0)); | |
370 | err_out: | |
371 | return -ENODEV; | |
372 | } | |
373 | ||
374 | static void __devexit maxiradio_remove_one(struct pci_dev *pdev) | |
375 | { | |
376 | video_unregister_device(&maxiradio_radio); | |
377 | release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0)); | |
378 | } | |
379 | ||
380 | static struct pci_device_id maxiradio_pci_tbl[] = { | |
381 | { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO, | |
382 | PCI_ANY_ID, PCI_ANY_ID, }, | |
383 | { 0,} | |
384 | }; | |
385 | ||
386 | MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl); | |
387 | ||
388 | static struct pci_driver maxiradio_driver = { | |
389 | .name = "radio-maxiradio", | |
390 | .id_table = maxiradio_pci_tbl, | |
391 | .probe = maxiradio_init_one, | |
392 | .remove = __devexit_p(maxiradio_remove_one), | |
393 | }; | |
394 | ||
395 | static int __init maxiradio_radio_init(void) | |
396 | { | |
9bfab8ce | 397 | return pci_register_driver(&maxiradio_driver); |
1da177e4 LT |
398 | } |
399 | ||
400 | static void __exit maxiradio_radio_exit(void) | |
401 | { | |
402 | pci_unregister_driver(&maxiradio_driver); | |
403 | } | |
404 | ||
405 | module_init(maxiradio_radio_init); | |
406 | module_exit(maxiradio_radio_exit); | |
06470ed6 MCC |
407 | |
408 | MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net"); | |
409 | MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio."); | |
410 | MODULE_LICENSE("GPL"); | |
411 | ||
412 | module_param_named(debug,maxiradio_radio.debug, int, 0644); | |
413 | MODULE_PARM_DESC(debug,"activates debug info"); |