blob: e5bfbd15820cbefe92ee6a1782d72acf1b5d5d3e [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 *
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030023 * 0.75 Sun Feb 4 22:51:27 EET 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * - 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 *
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -030030 * (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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/ioport.h>
39#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
41#include <asm/uaccess.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020042#include <linux/mutex.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/pci.h>
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030045#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030046#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -030048#define DRIVER_VERSION "0.77"
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030049
50#include <linux/version.h> /* for KERNEL_VERSION MACRO */
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -030051#define RADIO_VERSION KERNEL_VERSION(0,7,7)
52
53static struct video_device maxiradio_radio;
54
55#define dprintk(num, fmt, arg...) \
56 do { \
57 if (maxiradio_radio.debug >= num) \
58 printk(KERN_DEBUG "%s: " fmt, \
59 maxiradio_radio.name, ## arg); } while (0)
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030060
61static struct v4l2_queryctrl radio_qctrl[] = {
62 {
63 .id = V4L2_CID_AUDIO_MUTE,
64 .name = "Mute",
65 .minimum = 0,
66 .maximum = 1,
67 .default_value = 1,
68 .type = V4L2_CTRL_TYPE_BOOLEAN,
69 }
70};
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#ifndef PCI_VENDOR_ID_GUILLEMOT
73#define PCI_VENDOR_ID_GUILLEMOT 0x5046
74#endif
75
76#ifndef PCI_DEVICE_ID_GUILLEMOT
77#define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
78#endif
79
80
81/* TEA5757 pin mappings */
82static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
83
84static int radio_nr = -1;
85module_param(radio_nr, int, 0);
86
87
88#define FREQ_LO 50*16000
89#define FREQ_HI 150*16000
90
91#define FREQ_IF 171200 /* 10.7*16000 */
92#define FREQ_STEP 200 /* 12.5*16 */
93
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -030094/* (x==fmhz*16*1000) -> bits */
95#define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1)) \
96 /(FREQ_STEP<<2))<<2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
99
100
Arjan van de Venfa027c22007-02-12 00:55:33 -0800101static const struct file_operations maxiradio_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .owner = THIS_MODULE,
103 .open = video_exclusive_open,
104 .release = video_exclusive_release,
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300105 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200106 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .llseek = no_llseek,
108};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct radio_device
111{
112 __u16 io, /* base of radio io */
113 muted, /* VIDEO_AUDIO_MUTE */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300114 stereo, /* VIDEO_TUNER_STEREO_ON */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 tuned; /* signal strength (0 or 0xffff) */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long freq;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300118
Ingo Molnar3593cab2006-02-07 06:49:14 -0200119 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120} radio_unit = {0, 0, 0, 0, };
121
122
123static void outbit(unsigned long bit, __u16 io)
124{
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300125 if (bit != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 {
127 outb( power|wren|data ,io); udelay(4);
128 outb( power|wren|data|clk ,io); udelay(4);
129 outb( power|wren|data ,io); udelay(4);
130 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300131 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 {
133 outb( power|wren ,io); udelay(4);
134 outb( power|wren|clk ,io); udelay(4);
135 outb( power|wren ,io); udelay(4);
136 }
137}
138
139static void turn_power(__u16 io, int p)
140{
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300141 if (p != 0) {
142 dprintk(1, "Radio powered on\n");
143 outb(power, io);
144 } else {
145 dprintk(1, "Radio powered off\n");
146 outb(0,io);
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300150static void set_freq(__u16 io, __u32 freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 unsigned long int si;
153 int bl;
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300154 int data = FREQ2BITS(freq);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* TEA5757 shift register bits (see pdf) */
157
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300158 outbit(0,io); // 24 search
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 outbit(1,io); // 23 search up/down
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 outbit(0,io); // 22 stereo/mono
162
163 outbit(0,io); // 21 band
164 outbit(0,io); // 20 band (only 00=FM works I think)
165
166 outbit(0,io); // 19 port ?
167 outbit(0,io); // 18 port ?
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 outbit(0,io); // 17 search level
170 outbit(0,io); // 16 search level
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 si = 0x8000;
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300173 for (bl = 1; bl <= 16 ; bl++) {
174 outbit(data & si,io);
175 si >>=1;
176 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300177
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300178 dprintk(1, "Radio freq set to %d.%02d MHz\n",
179 freq / 16000,
180 freq % 16000 * 100 / 16000);
181
182 turn_power(io, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185static int get_stereo(__u16 io)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300186{
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300187 outb(power,io);
188 udelay(4);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return !(inb(io) & mo_st);
191}
192
193static int get_tune(__u16 io)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300194{
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300195 outb(power+clk,io);
196 udelay(4);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return !(inb(io) & mo_st);
199}
200
201
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300202static int vidioc_querycap (struct file *file, void *priv,
203 struct v4l2_capability *v)
204{
205 strlcpy(v->driver, "radio-maxiradio", sizeof (v->driver));
206 strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof (v->card));
207 sprintf(v->bus_info,"ISA");
208 v->version = RADIO_VERSION;
209 v->capabilities = V4L2_CAP_TUNER;
210
211 return 0;
212}
213
214static int vidioc_g_tuner (struct file *file, void *priv,
215 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct video_device *dev = video_devdata(file);
218 struct radio_device *card=dev->priv;
219
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300220 if (v->index > 0)
221 return -EINVAL;
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -0300222
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300223 memset(v,0,sizeof(*v));
224 strcpy(v->name, "FM");
225 v->type = V4L2_TUNER_RADIO;
226
227 v->rangelow=FREQ_LO;
228 v->rangehigh=FREQ_HI;
229 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
230 v->capability=V4L2_TUNER_CAP_LOW;
231 if(get_stereo(card->io))
232 v->audmode = V4L2_TUNER_MODE_STEREO;
233 else
234 v->audmode = V4L2_TUNER_MODE_MONO;
235 v->signal=0xffff*get_tune(card->io);
236
237 return 0;
238}
239
240static int vidioc_s_tuner (struct file *file, void *priv,
241 struct v4l2_tuner *v)
242{
243 if (v->index > 0)
244 return -EINVAL;
245
246 return 0;
247}
248
Mauro Carvalho Chehab140dcc42007-01-25 15:00:45 -0300249static int vidioc_g_audio (struct file *file, void *priv,
250 struct v4l2_audio *a)
251{
252 if (a->index > 1)
253 return -EINVAL;
254
Mauro Carvalho Chehabb61f8d62007-01-26 07:07:12 -0300255 strcpy(a->name, "FM");
Mauro Carvalho Chehab140dcc42007-01-25 15:00:45 -0300256 a->capability = V4L2_AUDCAP_STEREO;
257 return 0;
258}
259
Mauro Carvalho Chehaba0c05ab2007-01-25 16:48:13 -0300260static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
261{
262 *i = 0;
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300263
Mauro Carvalho Chehaba0c05ab2007-01-25 16:48:13 -0300264 return 0;
265}
266
267static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
268{
269 if (i != 0)
270 return -EINVAL;
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300271
Mauro Carvalho Chehaba0c05ab2007-01-25 16:48:13 -0300272 return 0;
273}
274
275
Mauro Carvalho Chehab140dcc42007-01-25 15:00:45 -0300276static int vidioc_s_audio (struct file *file, void *priv,
277 struct v4l2_audio *a)
278{
279 if (a->index != 0)
280 return -EINVAL;
281
282 return 0;
283}
284
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300285static int vidioc_s_frequency (struct file *file, void *priv,
286 struct v4l2_frequency *f)
287{
288 struct video_device *dev = video_devdata(file);
289 struct radio_device *card=dev->priv;
290
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300291 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) {
292 dprintk(1, "radio freq (%d.%02d MHz) out of range (%d-%d)\n",
293 f->frequency / 16000,
294 f->frequency % 16000 * 100 / 16000,
295 FREQ_LO / 16000, FREQ_HI / 16000);
296
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300297 return -EINVAL;
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300298 }
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300299
300 card->freq = f->frequency;
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300301 set_freq(card->io, card->freq);
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300302 msleep(125);
303
304 return 0;
305}
306
307static int vidioc_g_frequency (struct file *file, void *priv,
308 struct v4l2_frequency *f)
309{
310 struct video_device *dev = video_devdata(file);
311 struct radio_device *card=dev->priv;
312
313 f->type = V4L2_TUNER_RADIO;
314 f->frequency = card->freq;
315
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300316 dprintk(4, "radio freq is %d.%02d MHz",
317 f->frequency / 16000,
318 f->frequency % 16000 * 100 / 16000);
319
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300320 return 0;
321}
322
323static int vidioc_queryctrl (struct file *file, void *priv,
324 struct v4l2_queryctrl *qc)
325{
326 int i;
327
328 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
329 if (qc->id && qc->id == radio_qctrl[i].id) {
330 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
331 return (0);
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -0300332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300334
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300335 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300338static int vidioc_g_ctrl (struct file *file, void *priv,
339 struct v4l2_control *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct video_device *dev = video_devdata(file);
342 struct radio_device *card=dev->priv;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300343
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300344 switch (ctrl->id) {
345 case V4L2_CID_AUDIO_MUTE:
346 ctrl->value=card->muted;
347 return (0);
348 }
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300349
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300350 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300353static int vidioc_s_ctrl (struct file *file, void *priv,
354 struct v4l2_control *ctrl)
355{
356 struct video_device *dev = video_devdata(file);
357 struct radio_device *card=dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300359 switch (ctrl->id) {
360 case V4L2_CID_AUDIO_MUTE:
361 card->muted = ctrl->value;
362 if(card->muted)
363 turn_power(card->io, 0);
364 else
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300365 set_freq(card->io, card->freq);
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300366 return 0;
367 }
Mauro Carvalho Chehabf1557ce2007-01-26 07:23:44 -0300368
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300369 return -EINVAL;
370}
371
372static struct video_device maxiradio_radio =
373{
374 .owner = THIS_MODULE,
375 .name = "Maxi Radio FM2000 radio",
376 .type = VID_TYPE_TUNER,
377 .fops = &maxiradio_fops,
378
379 .vidioc_querycap = vidioc_querycap,
380 .vidioc_g_tuner = vidioc_g_tuner,
381 .vidioc_s_tuner = vidioc_s_tuner,
Mauro Carvalho Chehab140dcc42007-01-25 15:00:45 -0300382 .vidioc_g_audio = vidioc_g_audio,
383 .vidioc_s_audio = vidioc_s_audio,
Mauro Carvalho Chehaba0c05ab2007-01-25 16:48:13 -0300384 .vidioc_g_input = vidioc_g_input,
385 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300386 .vidioc_g_frequency = vidioc_g_frequency,
387 .vidioc_s_frequency = vidioc_s_frequency,
388 .vidioc_queryctrl = vidioc_queryctrl,
389 .vidioc_g_ctrl = vidioc_g_ctrl,
390 .vidioc_s_ctrl = vidioc_s_ctrl,
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300391};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
394{
395 if(!request_region(pci_resource_start(pdev, 0),
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300396 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
397 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
398 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 if (pci_enable_device(pdev))
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300402 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 radio_unit.io = pci_resource_start(pdev, 0);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200405 mutex_init(&radio_unit.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 maxiradio_radio.priv = &radio_unit;
407
408 if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300409 printk("radio-maxiradio: can't register device!");
410 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 printk(KERN_INFO "radio-maxiradio: version "
414 DRIVER_VERSION
415 " time "
416 __TIME__ " "
417 __DATE__
418 "\n");
419
420 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
421 radio_unit.io);
422 return 0;
423
424err_out_free_region:
425 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
426err_out:
427 return -ENODEV;
428}
429
430static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
431{
432 video_unregister_device(&maxiradio_radio);
433 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
434}
435
436static struct pci_device_id maxiradio_pci_tbl[] = {
437 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
438 PCI_ANY_ID, PCI_ANY_ID, },
439 { 0,}
440};
441
442MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
443
444static struct pci_driver maxiradio_driver = {
445 .name = "radio-maxiradio",
446 .id_table = maxiradio_pci_tbl,
447 .probe = maxiradio_init_one,
448 .remove = __devexit_p(maxiradio_remove_one),
449};
450
451static int __init maxiradio_radio_init(void)
452{
Richard Knutsson9bfab8c2005-11-30 00:59:34 +0100453 return pci_register_driver(&maxiradio_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456static void __exit maxiradio_radio_exit(void)
457{
458 pci_unregister_driver(&maxiradio_driver);
459}
460
461module_init(maxiradio_radio_init);
462module_exit(maxiradio_radio_exit);
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -0300463
464MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
465MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
466MODULE_LICENSE("GPL");
467
468module_param_named(debug,maxiradio_radio.debug, int, 0644);
469MODULE_PARM_DESC(debug,"activates debug info");