Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Maestro PCI sound card radio driver for Linux support |
2 | * (c) 2000 A. Tlalka, atlka@pg.gda.pl | |
3 | * Notes on the hardware | |
4 | * | |
4286c6f6 | 5 | * + Frequency control is done digitally |
1da177e4 LT |
6 | * + 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 | |
b6055d7b MCC |
17 | * |
18 | * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org> | |
1da177e4 LT |
19 | */ |
20 | ||
21 | #include <linux/module.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/ioport.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/sched.h> | |
26 | #include <asm/io.h> | |
27 | #include <asm/uaccess.h> | |
3593cab5 | 28 | #include <linux/mutex.h> |
1da177e4 | 29 | #include <linux/pci.h> |
b6055d7b | 30 | #include <linux/videodev2.h> |
5e87efa3 | 31 | #include <media/v4l2-common.h> |
3593cab5 | 32 | |
b6055d7b MCC |
33 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ |
34 | #define RADIO_VERSION KERNEL_VERSION(0,0,6) | |
35 | #define DRIVER_VERSION "0.06" | |
36 | ||
37 | static struct v4l2_queryctrl radio_qctrl[] = { | |
38 | { | |
39 | .id = V4L2_CID_AUDIO_MUTE, | |
40 | .name = "Mute", | |
41 | .minimum = 0, | |
42 | .maximum = 1, | |
43 | .default_value = 1, | |
44 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
45 | } | |
46 | }; | |
1da177e4 | 47 | |
6a2cf8ee | 48 | #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */ |
1da177e4 LT |
49 | |
50 | #define IO_MASK 4 /* mask register offset from GPIO_DATA | |
51 | bits 1=unmask write to given bit */ | |
52 | #define IO_DIR 8 /* direction register offset from GPIO_DATA | |
53 | bits 0/1=read/write direction */ | |
54 | ||
6a2cf8ee JS |
55 | #define GPIO6 0x0040 /* mask bits for GPIO lines */ |
56 | #define GPIO7 0x0080 | |
57 | #define GPIO8 0x0100 | |
58 | #define GPIO9 0x0200 | |
1da177e4 | 59 | |
6a2cf8ee JS |
60 | #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */ |
61 | #define STR_CLK GPIO7 | |
62 | #define STR_WREN GPIO8 | |
63 | #define STR_MOST GPIO9 | |
1da177e4 LT |
64 | |
65 | #define FREQ_LO 50*16000 | |
66 | #define FREQ_HI 150*16000 | |
67 | ||
6a2cf8ee JS |
68 | #define FREQ_IF 171200 /* 10.7*16000 */ |
69 | #define FREQ_STEP 200 /* 12.5*16 */ | |
1da177e4 LT |
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 | static int radio_nr = -1; | |
77 | module_param(radio_nr, int, 0); | |
78 | ||
79 | static int radio_ioctl(struct inode *inode, struct file *file, | |
6a2cf8ee | 80 | unsigned int cmd, unsigned long arg); |
89dad8f0 JS |
81 | static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent); |
82 | static void maestro_remove(struct pci_dev *pdev); | |
83 | ||
84 | static struct pci_device_id maestro_r_pci_tbl[] = { | |
85 | { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968), | |
86 | .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8, | |
87 | .class_mask = 0xffff00 }, | |
88 | { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978), | |
89 | .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8, | |
90 | .class_mask = 0xffff00 }, | |
91 | { 0 } | |
92 | }; | |
93 | MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl); | |
94 | ||
95 | static struct pci_driver maestro_r_driver = { | |
96 | .name = "maestro_radio", | |
97 | .id_table = maestro_r_pci_tbl, | |
98 | .probe = maestro_probe, | |
99 | .remove = __devexit_p(maestro_remove), | |
100 | }; | |
1da177e4 LT |
101 | |
102 | static struct file_operations maestro_fops = { | |
103 | .owner = THIS_MODULE, | |
104 | .open = video_exclusive_open, | |
105 | .release = video_exclusive_release, | |
106 | .ioctl = radio_ioctl, | |
0d0fbf81 | 107 | .compat_ioctl = v4l_compat_ioctl32, |
1da177e4 LT |
108 | .llseek = no_llseek, |
109 | }; | |
110 | ||
6a2cf8ee | 111 | static struct video_device maestro_radio = { |
1da177e4 LT |
112 | .name = "Maestro radio", |
113 | .type = VID_TYPE_TUNER, | |
b6055d7b | 114 | .hardware = 0, |
6a2cf8ee | 115 | .fops = &maestro_fops, |
1da177e4 LT |
116 | }; |
117 | ||
6a2cf8ee | 118 | struct radio_device { |
0eaa21fd | 119 | u16 io, /* base of Maestro card radio io (GPIO_DATA)*/ |
1da177e4 | 120 | muted, /* VIDEO_AUDIO_MUTE */ |
4286c6f6 | 121 | stereo, /* VIDEO_TUNER_STEREO_ON */ |
1da177e4 | 122 | tuned; /* signal strength (0 or 0xffff) */ |
3593cab5 | 123 | struct mutex lock; |
89dad8f0 | 124 | }; |
1da177e4 | 125 | |
0eaa21fd | 126 | static u32 radio_bits_get(struct radio_device *dev) |
1da177e4 | 127 | { |
0eaa21fd JS |
128 | register u16 io=dev->io, l, rdata; |
129 | register u32 data=0; | |
130 | u16 omask; | |
6a2cf8ee | 131 | |
1da177e4 LT |
132 | omask = inw(io + IO_MASK); |
133 | outw(~(STR_CLK | STR_WREN), io + IO_MASK); | |
134 | outw(0, io); | |
135 | udelay(16); | |
136 | ||
137 | for (l=24;l--;) { | |
138 | outw(STR_CLK, io); /* HI state */ | |
139 | udelay(2); | |
4286c6f6 | 140 | if(!l) |
1da177e4 LT |
141 | dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff; |
142 | outw(0, io); /* LO state */ | |
143 | udelay(2); | |
144 | data <<= 1; /* shift data */ | |
145 | rdata = inw(io); | |
146 | if(!l) | |
4286c6f6 | 147 | dev->stereo = rdata & STR_MOST ? |
b6055d7b | 148 | 0 : 1; |
1da177e4 LT |
149 | else |
150 | if(rdata & STR_DATA) | |
151 | data++; | |
152 | udelay(2); | |
153 | } | |
6a2cf8ee | 154 | |
1da177e4 LT |
155 | if(dev->muted) |
156 | outw(STR_WREN, io); | |
6a2cf8ee | 157 | |
1da177e4 LT |
158 | udelay(4); |
159 | outw(omask, io + IO_MASK); | |
6a2cf8ee | 160 | |
1da177e4 LT |
161 | return data & 0x3ffe; |
162 | } | |
163 | ||
0eaa21fd | 164 | static void radio_bits_set(struct radio_device *dev, u32 data) |
1da177e4 | 165 | { |
0eaa21fd JS |
166 | register u16 io=dev->io, l, bits; |
167 | u16 omask, odir; | |
6a2cf8ee | 168 | |
1da177e4 LT |
169 | omask = inw(io + IO_MASK); |
170 | odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN); | |
171 | outw(odir | STR_DATA, io + IO_DIR); | |
172 | outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK); | |
173 | udelay(16); | |
174 | for (l=25;l;l--) { | |
175 | bits = ((data >> 18) & STR_DATA) | STR_WREN ; | |
176 | data <<= 1; /* shift data */ | |
177 | outw(bits, io); /* start strobe */ | |
178 | udelay(2); | |
179 | outw(bits | STR_CLK, io); /* HI level */ | |
180 | udelay(2); | |
181 | outw(bits, io); /* LO level */ | |
182 | udelay(4); | |
183 | } | |
6a2cf8ee | 184 | |
1da177e4 LT |
185 | if(!dev->muted) |
186 | outw(0, io); | |
6a2cf8ee | 187 | |
1da177e4 LT |
188 | udelay(4); |
189 | outw(omask, io + IO_MASK); | |
190 | outw(odir, io + IO_DIR); | |
191 | msleep(125); | |
192 | } | |
193 | ||
77933d72 | 194 | static inline int radio_function(struct inode *inode, struct file *file, |
6a2cf8ee | 195 | unsigned int cmd, void *arg) |
1da177e4 LT |
196 | { |
197 | struct video_device *dev = video_devdata(file); | |
f86e7767 | 198 | struct radio_device *card = video_get_drvdata(dev); |
6a2cf8ee JS |
199 | |
200 | switch (cmd) { | |
b6055d7b MCC |
201 | case VIDIOC_QUERYCAP: |
202 | { | |
203 | struct v4l2_capability *v = arg; | |
204 | memset(v,0,sizeof(*v)); | |
205 | strlcpy(v->driver, "radio-maestro", sizeof (v->driver)); | |
206 | strlcpy(v->card, "Maestro Radio", sizeof (v->card)); | |
207 | sprintf(v->bus_info,"PCI"); | |
208 | v->version = RADIO_VERSION; | |
209 | v->capabilities = V4L2_CAP_TUNER; | |
210 | ||
211 | return 0; | |
212 | } | |
213 | case VIDIOC_G_TUNER: | |
214 | { | |
215 | struct v4l2_tuner *v = arg; | |
216 | ||
217 | if (v->index > 0) | |
218 | return -EINVAL; | |
219 | ||
220 | (void)radio_bits_get(card); | |
221 | ||
222 | memset(v,0,sizeof(*v)); | |
223 | strcpy(v->name, "FM"); | |
224 | v->type = V4L2_TUNER_RADIO; | |
225 | ||
226 | v->rangelow = FREQ_LO; | |
227 | v->rangehigh = FREQ_HI; | |
228 | v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO; | |
229 | v->capability=V4L2_TUNER_CAP_LOW; | |
230 | if(card->stereo) | |
231 | v->audmode = V4L2_TUNER_MODE_STEREO; | |
232 | else | |
233 | v->audmode = V4L2_TUNER_MODE_MONO; | |
234 | v->signal=card->tuned; | |
235 | ||
236 | return 0; | |
237 | } | |
238 | case VIDIOC_S_TUNER: | |
239 | { | |
240 | struct v4l2_tuner *v = arg; | |
241 | ||
242 | if (v->index > 0) | |
243 | return -EINVAL; | |
244 | ||
245 | return 0; | |
246 | } | |
247 | case VIDIOC_S_FREQUENCY: | |
248 | { | |
249 | struct v4l2_frequency *f = arg; | |
250 | ||
251 | if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) | |
252 | return -EINVAL; | |
253 | radio_bits_set(card, FREQ2BITS(f->frequency)); | |
254 | ||
255 | return 0; | |
256 | } | |
257 | case VIDIOC_G_FREQUENCY: | |
258 | { | |
259 | struct v4l2_frequency *f = arg; | |
260 | ||
261 | f->type = V4L2_TUNER_RADIO; | |
262 | f->frequency = BITS2FREQ(radio_bits_get(card)); | |
263 | ||
264 | return 0; | |
265 | } | |
266 | case VIDIOC_QUERYCTRL: | |
267 | { | |
268 | struct v4l2_queryctrl *qc = arg; | |
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 | } | |
6a2cf8ee | 278 | return -EINVAL; |
b6055d7b MCC |
279 | } |
280 | case VIDIOC_G_CTRL: | |
281 | { | |
282 | struct v4l2_control *ctrl= arg; | |
283 | ||
284 | switch (ctrl->id) { | |
285 | case V4L2_CID_AUDIO_MUTE: | |
286 | ctrl->value=card->muted; | |
287 | return (0); | |
288 | } | |
6a2cf8ee | 289 | return -EINVAL; |
b6055d7b MCC |
290 | } |
291 | case VIDIOC_S_CTRL: | |
6a2cf8ee | 292 | { |
b6055d7b MCC |
293 | struct v4l2_control *ctrl= arg; |
294 | ||
295 | switch (ctrl->id) { | |
296 | case V4L2_CID_AUDIO_MUTE: | |
297 | { | |
298 | register u16 io = card->io; | |
299 | register u16 omask = inw(io + IO_MASK); | |
300 | outw(~STR_WREN, io + IO_MASK); | |
301 | outw((card->muted = ctrl->value ) ? | |
302 | STR_WREN : 0, io); | |
303 | udelay(4); | |
304 | outw(omask, io + IO_MASK); | |
305 | msleep(125); | |
306 | ||
307 | return (0); | |
308 | } | |
309 | } | |
310 | return -EINVAL; | |
1da177e4 | 311 | } |
b6055d7b MCC |
312 | default: |
313 | return v4l_compat_translate_ioctl(inode,file,cmd,arg, | |
314 | radio_function); | |
1da177e4 LT |
315 | } |
316 | } | |
317 | ||
318 | static int radio_ioctl(struct inode *inode, struct file *file, | |
6a2cf8ee | 319 | unsigned int cmd, unsigned long arg) |
1da177e4 LT |
320 | { |
321 | struct video_device *dev = video_devdata(file); | |
f86e7767 | 322 | struct radio_device *card = video_get_drvdata(dev); |
1da177e4 LT |
323 | int ret; |
324 | ||
3593cab5 | 325 | mutex_lock(&card->lock); |
1da177e4 | 326 | ret = video_usercopy(inode, file, cmd, arg, radio_function); |
3593cab5 | 327 | mutex_unlock(&card->lock); |
6a2cf8ee | 328 | |
1da177e4 LT |
329 | return ret; |
330 | } | |
331 | ||
0eaa21fd | 332 | static u16 __devinit radio_power_on(struct radio_device *dev) |
1da177e4 | 333 | { |
0eaa21fd JS |
334 | register u16 io = dev->io; |
335 | register u32 ofreq; | |
336 | u16 omask, odir; | |
6a2cf8ee | 337 | |
1da177e4 | 338 | omask = inw(io + IO_MASK); |
6a2cf8ee | 339 | odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN); |
1da177e4 | 340 | outw(odir & ~STR_WREN, io + IO_DIR); |
b6055d7b | 341 | dev->muted = inw(io) & STR_WREN ? 0 : 1; |
1da177e4 LT |
342 | outw(odir, io + IO_DIR); |
343 | outw(~(STR_WREN | STR_CLK), io + IO_MASK); | |
344 | outw(dev->muted ? 0 : STR_WREN, io); | |
345 | udelay(16); | |
346 | outw(omask, io + IO_MASK); | |
347 | ofreq = radio_bits_get(dev); | |
6a2cf8ee JS |
348 | |
349 | if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI))) | |
1da177e4 LT |
350 | ofreq = FREQ2BITS(FREQ_LO); |
351 | radio_bits_set(dev, ofreq); | |
6a2cf8ee | 352 | |
1da177e4 LT |
353 | return (ofreq == radio_bits_get(dev)); |
354 | } | |
355 | ||
89dad8f0 JS |
356 | static int __devinit maestro_probe(struct pci_dev *pdev, |
357 | const struct pci_device_id *ent) | |
1da177e4 | 358 | { |
89dad8f0 JS |
359 | struct radio_device *radio_unit; |
360 | struct video_device *maestro_radio_inst; | |
361 | int retval; | |
362 | ||
363 | retval = pci_enable_device(pdev); | |
364 | if (retval) { | |
365 | dev_err(&pdev->dev, "enabling pci device failed!\n"); | |
366 | goto err; | |
367 | } | |
368 | ||
369 | retval = -ENOMEM; | |
370 | ||
371 | radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL); | |
372 | if (radio_unit == NULL) { | |
373 | dev_err(&pdev->dev, "not enough memory\n"); | |
374 | goto err; | |
375 | } | |
376 | ||
377 | radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA; | |
3593cab5 | 378 | mutex_init(&radio_unit->lock); |
89dad8f0 JS |
379 | |
380 | maestro_radio_inst = video_device_alloc(); | |
381 | if (maestro_radio_inst == NULL) { | |
382 | dev_err(&pdev->dev, "not enough memory\n"); | |
383 | goto errfr; | |
384 | } | |
385 | ||
386 | memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio)); | |
387 | video_set_drvdata(maestro_radio_inst, radio_unit); | |
388 | pci_set_drvdata(pdev, maestro_radio_inst); | |
389 | ||
390 | retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO, | |
391 | radio_nr); | |
392 | if (retval) { | |
393 | printk(KERN_ERR "can't register video device!\n"); | |
394 | goto errfr1; | |
395 | } | |
396 | ||
397 | if (!radio_power_on(radio_unit)) { | |
398 | retval = -EIO; | |
399 | goto errunr; | |
400 | } | |
401 | ||
402 | dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " " | |
6a2cf8ee | 403 | __DATE__ "\n"); |
89dad8f0 JS |
404 | dev_info(&pdev->dev, "radio chip initialized\n"); |
405 | ||
406 | return 0; | |
407 | errunr: | |
408 | video_unregister_device(maestro_radio_inst); | |
409 | errfr1: | |
410 | kfree(maestro_radio_inst); | |
411 | errfr: | |
412 | kfree(radio_unit); | |
413 | err: | |
414 | return retval; | |
415 | ||
416 | } | |
417 | ||
418 | static void __devexit maestro_remove(struct pci_dev *pdev) | |
419 | { | |
420 | struct video_device *vdev = pci_get_drvdata(pdev); | |
421 | ||
422 | video_unregister_device(vdev); | |
1da177e4 LT |
423 | } |
424 | ||
89dad8f0 JS |
425 | static int __init maestro_radio_init(void) |
426 | { | |
427 | int retval = pci_register_driver(&maestro_r_driver); | |
428 | ||
429 | if (retval) | |
430 | printk(KERN_ERR "error during registration pci driver\n"); | |
431 | ||
432 | return retval; | |
433 | } | |
434 | ||
435 | static void __exit maestro_radio_exit(void) | |
436 | { | |
437 | pci_unregister_driver(&maestro_r_driver); | |
438 | } | |
439 | ||
440 | module_init(maestro_radio_init); | |
441 | module_exit(maestro_radio_exit); | |
6a2cf8ee JS |
442 | |
443 | MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl"); | |
444 | MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio."); | |
445 | MODULE_LICENSE("GPL"); |