blob: 182b433981ca69d97e65533279181785a01b38a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Maestro PCI sound card radio driver for Linux support
2 * (c) 2000 A. Tlalka, atlka@pg.gda.pl
3 * Notes on the hardware
4 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03005 * + Frequency control is done digitally
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * + No volume control - only mute/unmute - you have to use Aux line volume
7 * control on Maestro card to set the volume
8 * + Radio status (tuned/not_tuned and stereo/mono) is valid some time after
9 * frequency setting (>100ms) and only when the radio is unmuted.
10 * version 0.02
11 * + io port is automatically detected - only the first radio is used
12 * version 0.03
13 * + thread access locking additions
14 * version 0.04
15 * + code improvements
16 * + VIDEO_TUNER_LOW is permanent
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -030017 *
18 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
24#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
26#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pci.h>
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -030028#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030029#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030030#include <media/v4l2-ioctl.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020031
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -030032#include <linux/version.h> /* for KERNEL_VERSION MACRO */
33#define RADIO_VERSION KERNEL_VERSION(0,0,6)
34#define DRIVER_VERSION "0.06"
35
36static struct v4l2_queryctrl radio_qctrl[] = {
37 {
38 .id = V4L2_CID_AUDIO_MUTE,
39 .name = "Mute",
40 .minimum = 0,
41 .maximum = 1,
42 .default_value = 1,
43 .type = V4L2_CTRL_TYPE_BOOLEAN,
44 }
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -080047#define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define IO_MASK 4 /* mask register offset from GPIO_DATA
50 bits 1=unmask write to given bit */
51#define IO_DIR 8 /* direction register offset from GPIO_DATA
52 bits 0/1=read/write direction */
53
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -080054#define GPIO6 0x0040 /* mask bits for GPIO lines */
55#define GPIO7 0x0080
56#define GPIO8 0x0100
57#define GPIO9 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -080059#define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
60#define STR_CLK GPIO7
61#define STR_WREN GPIO8
62#define STR_MOST GPIO9
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define FREQ_LO 50*16000
65#define FREQ_HI 150*16000
66
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -080067#define FREQ_IF 171200 /* 10.7*16000 */
68#define FREQ_STEP 200 /* 12.5*16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
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
75static int radio_nr = -1;
76module_param(radio_nr, int, 0);
77
Hans Verkuil3ca685a2008-08-23 04:49:13 -030078static unsigned long in_use;
79
Jiri Slaby89dad8f2006-01-09 20:52:47 -080080static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
Hans Verkuil3ca685a2008-08-23 04:49:13 -030081
82static int maestro_exclusive_open(struct inode *inode, struct file *file)
83{
84 return test_and_set_bit(0, &in_use) ? -EBUSY : 0;
85}
86
87static int maestro_exclusive_release(struct inode *inode, struct file *file)
88{
89 clear_bit(0, &in_use);
90 return 0;
91}
92
Jiri Slaby89dad8f2006-01-09 20:52:47 -080093static void maestro_remove(struct pci_dev *pdev);
94
95static struct pci_device_id maestro_r_pci_tbl[] = {
96 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
97 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
98 .class_mask = 0xffff00 },
99 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
100 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
101 .class_mask = 0xffff00 },
102 { 0 }
103};
104MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
105
106static struct pci_driver maestro_r_driver = {
107 .name = "maestro_radio",
108 .id_table = maestro_r_pci_tbl,
109 .probe = maestro_probe,
110 .remove = __devexit_p(maestro_remove),
111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Arjan van de Venfa027c22007-02-12 00:55:33 -0800113static const struct file_operations maestro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .owner = THIS_MODULE,
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300115 .open = maestro_exclusive_open,
116 .release = maestro_exclusive_release,
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300117 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300118#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200119 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .llseek = no_llseek,
122};
123
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800124struct radio_device {
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800125 u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 muted, /* VIDEO_AUDIO_MUTE */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300127 stereo, /* VIDEO_TUNER_STEREO_ON */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 tuned; /* signal strength (0 or 0xffff) */
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800129};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800131static u32 radio_bits_get(struct radio_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800133 register u16 io=dev->io, l, rdata;
134 register u32 data=0;
135 u16 omask;
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 omask = inw(io + IO_MASK);
138 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
139 outw(0, io);
140 udelay(16);
141
142 for (l=24;l--;) {
143 outw(STR_CLK, io); /* HI state */
144 udelay(2);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300145 if(!l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
147 outw(0, io); /* LO state */
148 udelay(2);
149 data <<= 1; /* shift data */
150 rdata = inw(io);
151 if(!l)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300152 dev->stereo = rdata & STR_MOST ?
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -0300153 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 else
155 if(rdata & STR_DATA)
156 data++;
157 udelay(2);
158 }
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if(dev->muted)
161 outw(STR_WREN, io);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 udelay(4);
164 outw(omask, io + IO_MASK);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return data & 0x3ffe;
167}
168
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800169static void radio_bits_set(struct radio_device *dev, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800171 register u16 io=dev->io, l, bits;
172 u16 omask, odir;
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 omask = inw(io + IO_MASK);
175 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
176 outw(odir | STR_DATA, io + IO_DIR);
177 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
178 udelay(16);
179 for (l=25;l;l--) {
180 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
181 data <<= 1; /* shift data */
182 outw(bits, io); /* start strobe */
183 udelay(2);
184 outw(bits | STR_CLK, io); /* HI level */
185 udelay(2);
186 outw(bits, io); /* LO level */
187 udelay(4);
188 }
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if(!dev->muted)
191 outw(0, io);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 udelay(4);
194 outw(omask, io + IO_MASK);
195 outw(odir, io + IO_DIR);
196 msleep(125);
197}
198
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300199static int vidioc_querycap(struct file *file, void *priv,
200 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300202 strlcpy(v->driver, "radio-maestro", sizeof(v->driver));
203 strlcpy(v->card, "Maestro Radio", sizeof(v->card));
204 sprintf(v->bus_info, "PCI");
205 v->version = RADIO_VERSION;
206 v->capabilities = V4L2_CAP_TUNER;
207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300210static int vidioc_g_tuner(struct file *file, void *priv,
211 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct video_device *dev = video_devdata(file);
Jiri Slabyf86e7762006-01-09 20:52:50 -0800214 struct radio_device *card = video_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300216 if (v->index > 0)
217 return -EINVAL;
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800218
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300219 (void)radio_bits_get(card);
220
221 strcpy(v->name, "FM");
222 v->type = V4L2_TUNER_RADIO;
223 v->rangelow = FREQ_LO;
224 v->rangehigh = FREQ_HI;
225 v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
226 v->capability = V4L2_TUNER_CAP_LOW;
227 if(card->stereo)
228 v->audmode = V4L2_TUNER_MODE_STEREO;
229 else
230 v->audmode = V4L2_TUNER_MODE_MONO;
231 v->signal = card->tuned;
232 return 0;
233}
234
235static int vidioc_s_tuner(struct file *file, void *priv,
236 struct v4l2_tuner *v)
237{
238 if (v->index > 0)
239 return -EINVAL;
240 return 0;
241}
242
243static 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 = video_get_drvdata(dev);
248
249 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
250 return -EINVAL;
251 radio_bits_set(card, FREQ2BITS(f->frequency));
252 return 0;
253}
254
255static int vidioc_g_frequency(struct file *file, void *priv,
256 struct v4l2_frequency *f)
257{
258 struct video_device *dev = video_devdata(file);
259 struct radio_device *card = video_get_drvdata(dev);
260
261 f->type = V4L2_TUNER_RADIO;
262 f->frequency = BITS2FREQ(radio_bits_get(card));
263 return 0;
264}
265
266static int vidioc_queryctrl(struct file *file, void *priv,
267 struct v4l2_queryctrl *qc)
268{
269 int i;
270
271 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
272 if (qc->id && qc->id == radio_qctrl[i].id) {
273 memcpy(qc, &(radio_qctrl[i]),
274 sizeof(*qc));
275 return 0;
276 }
277 }
278 return -EINVAL;
279}
280
281static int vidioc_g_ctrl(struct file *file, void *priv,
282 struct v4l2_control *ctrl)
283{
284 struct video_device *dev = video_devdata(file);
285 struct radio_device *card = video_get_drvdata(dev);
286
287 switch (ctrl->id) {
288 case V4L2_CID_AUDIO_MUTE:
289 ctrl->value = card->muted;
290 return 0;
291 }
292 return -EINVAL;
293}
294
295static int vidioc_s_ctrl(struct file *file, void *priv,
296 struct v4l2_control *ctrl)
297{
298 struct video_device *dev = video_devdata(file);
299 struct radio_device *card = video_get_drvdata(dev);
300 register u16 io = card->io;
301 register u16 omask = inw(io + IO_MASK);
302
303 switch (ctrl->id) {
304 case V4L2_CID_AUDIO_MUTE:
305 outw(~STR_WREN, io + IO_MASK);
306 outw((card->muted = ctrl->value ) ?
307 STR_WREN : 0, io);
308 udelay(4);
309 outw(omask, io + IO_MASK);
310 msleep(125);
311 return 0;
312 }
313 return -EINVAL;
314}
315
316static int vidioc_g_audio(struct file *file, void *priv,
317 struct v4l2_audio *a)
318{
319 if (a->index > 1)
320 return -EINVAL;
321
322 strcpy(a->name, "Radio");
323 a->capability = V4L2_AUDCAP_STEREO;
324 return 0;
325}
326
327static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
328{
329 *i = 0;
330 return 0;
331}
332
333static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
334{
335 if (i != 0)
336 return -EINVAL;
337 return 0;
338}
339
340static int vidioc_s_audio(struct file *file, void *priv,
341 struct v4l2_audio *a)
342{
343 if (a->index != 0)
344 return -EINVAL;
345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800348static u16 __devinit radio_power_on(struct radio_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Jiri Slaby0eaa21f2006-01-09 20:52:49 -0800350 register u16 io = dev->io;
351 register u32 ofreq;
352 u16 omask, odir;
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 omask = inw(io + IO_MASK);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800355 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 outw(odir & ~STR_WREN, io + IO_DIR);
Mauro Carvalho Chehabb6055d72006-08-08 09:10:02 -0300357 dev->muted = inw(io) & STR_WREN ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 outw(odir, io + IO_DIR);
359 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
360 outw(dev->muted ? 0 : STR_WREN, io);
361 udelay(16);
362 outw(omask, io + IO_MASK);
363 ofreq = radio_bits_get(dev);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800364
365 if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 ofreq = FREQ2BITS(FREQ_LO);
367 radio_bits_set(dev, ofreq);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return (ofreq == radio_bits_get(dev));
370}
371
Hans Verkuila3998102008-07-21 02:57:38 -0300372static const struct v4l2_ioctl_ops maestro_ioctl_ops = {
Douglas Landgrafd455cf52007-04-26 16:44:55 -0300373 .vidioc_querycap = vidioc_querycap,
374 .vidioc_g_tuner = vidioc_g_tuner,
375 .vidioc_s_tuner = vidioc_s_tuner,
376 .vidioc_g_audio = vidioc_g_audio,
377 .vidioc_s_audio = vidioc_s_audio,
378 .vidioc_g_input = vidioc_g_input,
379 .vidioc_s_input = vidioc_s_input,
380 .vidioc_g_frequency = vidioc_g_frequency,
381 .vidioc_s_frequency = vidioc_s_frequency,
382 .vidioc_queryctrl = vidioc_queryctrl,
383 .vidioc_g_ctrl = vidioc_g_ctrl,
384 .vidioc_s_ctrl = vidioc_s_ctrl,
385};
386
Hans Verkuila3998102008-07-21 02:57:38 -0300387static struct video_device maestro_radio = {
388 .name = "Maestro radio",
Hans Verkuila3998102008-07-21 02:57:38 -0300389 .fops = &maestro_fops,
390 .ioctl_ops = &maestro_ioctl_ops,
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300391 .release = video_device_release,
Hans Verkuila3998102008-07-21 02:57:38 -0300392};
393
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800394static int __devinit maestro_probe(struct pci_dev *pdev,
395 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800397 struct radio_device *radio_unit;
398 struct video_device *maestro_radio_inst;
399 int retval;
400
401 retval = pci_enable_device(pdev);
402 if (retval) {
403 dev_err(&pdev->dev, "enabling pci device failed!\n");
404 goto err;
405 }
406
407 retval = -ENOMEM;
408
409 radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
410 if (radio_unit == NULL) {
411 dev_err(&pdev->dev, "not enough memory\n");
412 goto err;
413 }
414
415 radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800416
417 maestro_radio_inst = video_device_alloc();
418 if (maestro_radio_inst == NULL) {
419 dev_err(&pdev->dev, "not enough memory\n");
420 goto errfr;
421 }
422
423 memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
424 video_set_drvdata(maestro_radio_inst, radio_unit);
425 pci_set_drvdata(pdev, maestro_radio_inst);
426
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300427 retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO, radio_nr);
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800428 if (retval) {
429 printk(KERN_ERR "can't register video device!\n");
430 goto errfr1;
431 }
432
433 if (!radio_power_on(radio_unit)) {
434 retval = -EIO;
435 goto errunr;
436 }
437
438 dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800439 __DATE__ "\n");
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800440 dev_info(&pdev->dev, "radio chip initialized\n");
441
442 return 0;
443errunr:
444 video_unregister_device(maestro_radio_inst);
445errfr1:
Julia Lawallf37fdf32008-01-01 18:08:10 -0300446 video_device_release(maestro_radio_inst);
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800447errfr:
448 kfree(radio_unit);
449err:
450 return retval;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800454static void __devexit maestro_remove(struct pci_dev *pdev)
455{
456 struct video_device *vdev = pci_get_drvdata(pdev);
457
458 video_unregister_device(vdev);
459}
460
Jiri Slaby89dad8f2006-01-09 20:52:47 -0800461static int __init maestro_radio_init(void)
462{
463 int retval = pci_register_driver(&maestro_r_driver);
464
465 if (retval)
466 printk(KERN_ERR "error during registration pci driver\n");
467
468 return retval;
469}
470
471static void __exit maestro_radio_exit(void)
472{
473 pci_unregister_driver(&maestro_r_driver);
474}
475
476module_init(maestro_radio_init);
477module_exit(maestro_radio_exit);
Jiri Slaby6a2cf8e2006-01-09 20:52:48 -0800478
479MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
480MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
481MODULE_LICENSE("GPL");