blob: c30effdf711feb6ba8e5457b5c908ca472abeb52 [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 *
5 * + Frequency control is done digitally
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
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/ioport.h>
22#include <linux/delay.h>
23#include <linux/sched.h>
24#include <asm/io.h>
25#include <asm/uaccess.h>
26#include <asm/semaphore.h>
27#include <linux/pci.h>
28#include <linux/videodev.h>
29
30#define DRIVER_VERSION "0.04"
31
32#define PCI_VENDOR_ESS 0x125D
33#define PCI_DEVICE_ID_ESS_ESS1968 0x1968 /* Maestro 2 */
34#define PCI_DEVICE_ID_ESS_ESS1978 0x1978 /* Maestro 2E */
35
36#define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
37
38#define IO_MASK 4 /* mask register offset from GPIO_DATA
39 bits 1=unmask write to given bit */
40#define IO_DIR 8 /* direction register offset from GPIO_DATA
41 bits 0/1=read/write direction */
42
43#define GPIO6 0x0040 /* mask bits for GPIO lines */
44#define GPIO7 0x0080
45#define GPIO8 0x0100
46#define GPIO9 0x0200
47
48#define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
49#define STR_CLK GPIO7
50#define STR_WREN GPIO8
51#define STR_MOST GPIO9
52
53#define FREQ_LO 50*16000
54#define FREQ_HI 150*16000
55
56#define FREQ_IF 171200 /* 10.7*16000 */
57#define FREQ_STEP 200 /* 12.5*16 */
58
59#define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
60 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
61
62#define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
63
64static int radio_nr = -1;
65module_param(radio_nr, int, 0);
66
67static int radio_ioctl(struct inode *inode, struct file *file,
68 unsigned int cmd, unsigned long arg);
69
70static struct file_operations maestro_fops = {
71 .owner = THIS_MODULE,
72 .open = video_exclusive_open,
73 .release = video_exclusive_release,
74 .ioctl = radio_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020075 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 .llseek = no_llseek,
77};
78
79static struct video_device maestro_radio=
80{
81 .owner = THIS_MODULE,
82 .name = "Maestro radio",
83 .type = VID_TYPE_TUNER,
84 .hardware = VID_HARDWARE_SF16MI,
85 .fops = &maestro_fops,
86};
87
88static struct radio_device
89{
90 __u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
91 muted, /* VIDEO_AUDIO_MUTE */
92 stereo, /* VIDEO_TUNER_STEREO_ON */
93 tuned; /* signal strength (0 or 0xffff) */
94 struct semaphore lock;
95} radio_unit = {0, 0, 0, 0, };
96
97static __u32 radio_bits_get(struct radio_device *dev)
98{
99 register __u16 io=dev->io, l, rdata;
100 register __u32 data=0;
101 __u16 omask;
102 omask = inw(io + IO_MASK);
103 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
104 outw(0, io);
105 udelay(16);
106
107 for (l=24;l--;) {
108 outw(STR_CLK, io); /* HI state */
109 udelay(2);
110 if(!l)
111 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
112 outw(0, io); /* LO state */
113 udelay(2);
114 data <<= 1; /* shift data */
115 rdata = inw(io);
116 if(!l)
117 dev->stereo = rdata & STR_MOST ?
118 0 : VIDEO_TUNER_STEREO_ON;
119 else
120 if(rdata & STR_DATA)
121 data++;
122 udelay(2);
123 }
124 if(dev->muted)
125 outw(STR_WREN, io);
126 udelay(4);
127 outw(omask, io + IO_MASK);
128 return data & 0x3ffe;
129}
130
131static void radio_bits_set(struct radio_device *dev, __u32 data)
132{
133 register __u16 io=dev->io, l, bits;
134 __u16 omask, odir;
135 omask = inw(io + IO_MASK);
136 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
137 outw(odir | STR_DATA, io + IO_DIR);
138 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
139 udelay(16);
140 for (l=25;l;l--) {
141 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
142 data <<= 1; /* shift data */
143 outw(bits, io); /* start strobe */
144 udelay(2);
145 outw(bits | STR_CLK, io); /* HI level */
146 udelay(2);
147 outw(bits, io); /* LO level */
148 udelay(4);
149 }
150 if(!dev->muted)
151 outw(0, io);
152 udelay(4);
153 outw(omask, io + IO_MASK);
154 outw(odir, io + IO_DIR);
155 msleep(125);
156}
157
Jesper Juhl77933d72005-07-27 11:46:09 -0700158static inline int radio_function(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 unsigned int cmd, void *arg)
160{
161 struct video_device *dev = video_devdata(file);
162 struct radio_device *card=dev->priv;
163
164 switch(cmd) {
165 case VIDIOCGCAP: {
166 struct video_capability *v = arg;
167 memset(v,0,sizeof(*v));
168 strcpy(v->name, "Maestro radio");
169 v->type=VID_TYPE_TUNER;
170 v->channels=v->audios=1;
171 return 0;
172 }
173 case VIDIOCGTUNER: {
174 struct video_tuner *v = arg;
175 if(v->tuner)
176 return -EINVAL;
177 (void)radio_bits_get(card);
178 v->flags = VIDEO_TUNER_LOW | card->stereo;
179 v->signal = card->tuned;
180 strcpy(v->name, "FM");
181 v->rangelow = FREQ_LO;
182 v->rangehigh = FREQ_HI;
183 v->mode = VIDEO_MODE_AUTO;
184 return 0;
185 }
186 case VIDIOCSTUNER: {
187 struct video_tuner *v = arg;
188 if(v->tuner!=0)
189 return -EINVAL;
190 return 0;
191 }
192 case VIDIOCGFREQ: {
193 unsigned long *freq = arg;
194 *freq = BITS2FREQ(radio_bits_get(card));
195 return 0;
196 }
197 case VIDIOCSFREQ: {
198 unsigned long *freq = arg;
199 if (*freq<FREQ_LO || *freq>FREQ_HI )
200 return -EINVAL;
201 radio_bits_set(card, FREQ2BITS(*freq));
202 return 0;
203 }
204 case VIDIOCGAUDIO: {
205 struct video_audio *v = arg;
206 memset(v,0,sizeof(*v));
207 strcpy(v->name, "Radio");
208 v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
209 v->mode=VIDEO_SOUND_STEREO;
210 return 0;
211 }
212 case VIDIOCSAUDIO: {
213 struct video_audio *v = arg;
214 if(v->audio)
215 return -EINVAL;
216 {
217 register __u16 io=card->io;
218 register __u16 omask = inw(io + IO_MASK);
219 outw(~STR_WREN, io + IO_MASK);
220 outw((card->muted = v->flags & VIDEO_AUDIO_MUTE)
221 ? STR_WREN : 0, io);
222 udelay(4);
223 outw(omask, io + IO_MASK);
224 msleep(125);
225 return 0;
226 }
227 }
228 case VIDIOCGUNIT: {
229 struct video_unit *v = arg;
230 v->video=VIDEO_NO_UNIT;
231 v->vbi=VIDEO_NO_UNIT;
232 v->radio=dev->minor;
233 v->audio=0;
234 v->teletext=VIDEO_NO_UNIT;
235 return 0;
236 }
237 default: return -ENOIOCTLCMD;
238 }
239}
240
241static int radio_ioctl(struct inode *inode, struct file *file,
242 unsigned int cmd, unsigned long arg)
243{
244 struct video_device *dev = video_devdata(file);
245 struct radio_device *card=dev->priv;
246 int ret;
247
248 down(&card->lock);
249 ret = video_usercopy(inode, file, cmd, arg, radio_function);
250 up(&card->lock);
251 return ret;
252}
253
254static __u16 radio_install(struct pci_dev *pcidev);
255
256MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
257MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
258MODULE_LICENSE("GPL");
259
260static void __exit maestro_radio_exit(void)
261{
262 video_unregister_device(&maestro_radio);
263}
264
265static int __init maestro_radio_init(void)
266{
267 register __u16 found=0;
268 struct pci_dev *pcidev = NULL;
269 while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
270 PCI_DEVICE_ID_ESS_ESS1968,
271 pcidev)))
272 found |= radio_install(pcidev);
273 while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
274 PCI_DEVICE_ID_ESS_ESS1978,
275 pcidev)))
276 found |= radio_install(pcidev);
277 if(!found) {
278 printk(KERN_INFO "radio-maestro: no devices found.\n");
279 return -ENODEV;
280 }
281 return 0;
282}
283
284module_init(maestro_radio_init);
285module_exit(maestro_radio_exit);
286
Jesper Juhl77933d72005-07-27 11:46:09 -0700287static inline __u16 radio_power_on(struct radio_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 register __u16 io=dev->io;
290 register __u32 ofreq;
291 __u16 omask, odir;
292 omask = inw(io + IO_MASK);
293 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
294 outw(odir & ~STR_WREN, io + IO_DIR);
295 dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
296 outw(odir, io + IO_DIR);
297 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
298 outw(dev->muted ? 0 : STR_WREN, io);
299 udelay(16);
300 outw(omask, io + IO_MASK);
301 ofreq = radio_bits_get(dev);
302 if((ofreq<FREQ2BITS(FREQ_LO)) || (ofreq>FREQ2BITS(FREQ_HI)))
303 ofreq = FREQ2BITS(FREQ_LO);
304 radio_bits_set(dev, ofreq);
305 return (ofreq == radio_bits_get(dev));
306}
307
308static __u16 radio_install(struct pci_dev *pcidev)
309{
310 if(((pcidev->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
311 return 0;
312
313 radio_unit.io = pcidev->resource[0].start + GPIO_DATA;
314 maestro_radio.priv = &radio_unit;
315 init_MUTEX(&radio_unit.lock);
316
317 if(radio_power_on(&radio_unit)) {
318 if(video_register_device(&maestro_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
319 printk("radio-maestro: can't register device!");
320 return 0;
321 }
322 printk(KERN_INFO "radio-maestro: version "
323 DRIVER_VERSION
324 " time "
325 __TIME__ " "
326 __DATE__
327 "\n");
328 printk(KERN_INFO "radio-maestro: radio chip initialized\n");
329 return 1;
330 } else
331 return 0;
332}
333