[PATCH] clean up inline static vs static inline
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / radio / radio-maestro.c
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 *
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
64 static int radio_nr = -1;
65 module_param(radio_nr, int, 0);
66
67 static int radio_ioctl(struct inode *inode, struct file *file,
68 unsigned int cmd, unsigned long arg);
69
70 static struct file_operations maestro_fops = {
71 .owner = THIS_MODULE,
72 .open = video_exclusive_open,
73 .release = video_exclusive_release,
74 .ioctl = radio_ioctl,
75 .llseek = no_llseek,
76 };
77
78 static struct video_device maestro_radio=
79 {
80 .owner = THIS_MODULE,
81 .name = "Maestro radio",
82 .type = VID_TYPE_TUNER,
83 .hardware = VID_HARDWARE_SF16MI,
84 .fops = &maestro_fops,
85 };
86
87 static struct radio_device
88 {
89 __u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
90 muted, /* VIDEO_AUDIO_MUTE */
91 stereo, /* VIDEO_TUNER_STEREO_ON */
92 tuned; /* signal strength (0 or 0xffff) */
93 struct semaphore lock;
94 } radio_unit = {0, 0, 0, 0, };
95
96 static __u32 radio_bits_get(struct radio_device *dev)
97 {
98 register __u16 io=dev->io, l, rdata;
99 register __u32 data=0;
100 __u16 omask;
101 omask = inw(io + IO_MASK);
102 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
103 outw(0, io);
104 udelay(16);
105
106 for (l=24;l--;) {
107 outw(STR_CLK, io); /* HI state */
108 udelay(2);
109 if(!l)
110 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
111 outw(0, io); /* LO state */
112 udelay(2);
113 data <<= 1; /* shift data */
114 rdata = inw(io);
115 if(!l)
116 dev->stereo = rdata & STR_MOST ?
117 0 : VIDEO_TUNER_STEREO_ON;
118 else
119 if(rdata & STR_DATA)
120 data++;
121 udelay(2);
122 }
123 if(dev->muted)
124 outw(STR_WREN, io);
125 udelay(4);
126 outw(omask, io + IO_MASK);
127 return data & 0x3ffe;
128 }
129
130 static void radio_bits_set(struct radio_device *dev, __u32 data)
131 {
132 register __u16 io=dev->io, l, bits;
133 __u16 omask, odir;
134 omask = inw(io + IO_MASK);
135 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
136 outw(odir | STR_DATA, io + IO_DIR);
137 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
138 udelay(16);
139 for (l=25;l;l--) {
140 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
141 data <<= 1; /* shift data */
142 outw(bits, io); /* start strobe */
143 udelay(2);
144 outw(bits | STR_CLK, io); /* HI level */
145 udelay(2);
146 outw(bits, io); /* LO level */
147 udelay(4);
148 }
149 if(!dev->muted)
150 outw(0, io);
151 udelay(4);
152 outw(omask, io + IO_MASK);
153 outw(odir, io + IO_DIR);
154 msleep(125);
155 }
156
157 static inline int radio_function(struct inode *inode, struct file *file,
158 unsigned int cmd, void *arg)
159 {
160 struct video_device *dev = video_devdata(file);
161 struct radio_device *card=dev->priv;
162
163 switch(cmd) {
164 case VIDIOCGCAP: {
165 struct video_capability *v = arg;
166 memset(v,0,sizeof(*v));
167 strcpy(v->name, "Maestro radio");
168 v->type=VID_TYPE_TUNER;
169 v->channels=v->audios=1;
170 return 0;
171 }
172 case VIDIOCGTUNER: {
173 struct video_tuner *v = arg;
174 if(v->tuner)
175 return -EINVAL;
176 (void)radio_bits_get(card);
177 v->flags = VIDEO_TUNER_LOW | card->stereo;
178 v->signal = card->tuned;
179 strcpy(v->name, "FM");
180 v->rangelow = FREQ_LO;
181 v->rangehigh = FREQ_HI;
182 v->mode = VIDEO_MODE_AUTO;
183 return 0;
184 }
185 case VIDIOCSTUNER: {
186 struct video_tuner *v = arg;
187 if(v->tuner!=0)
188 return -EINVAL;
189 return 0;
190 }
191 case VIDIOCGFREQ: {
192 unsigned long *freq = arg;
193 *freq = BITS2FREQ(radio_bits_get(card));
194 return 0;
195 }
196 case VIDIOCSFREQ: {
197 unsigned long *freq = arg;
198 if (*freq<FREQ_LO || *freq>FREQ_HI )
199 return -EINVAL;
200 radio_bits_set(card, FREQ2BITS(*freq));
201 return 0;
202 }
203 case VIDIOCGAUDIO: {
204 struct video_audio *v = arg;
205 memset(v,0,sizeof(*v));
206 strcpy(v->name, "Radio");
207 v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
208 v->mode=VIDEO_SOUND_STEREO;
209 return 0;
210 }
211 case VIDIOCSAUDIO: {
212 struct video_audio *v = arg;
213 if(v->audio)
214 return -EINVAL;
215 {
216 register __u16 io=card->io;
217 register __u16 omask = inw(io + IO_MASK);
218 outw(~STR_WREN, io + IO_MASK);
219 outw((card->muted = v->flags & VIDEO_AUDIO_MUTE)
220 ? STR_WREN : 0, io);
221 udelay(4);
222 outw(omask, io + IO_MASK);
223 msleep(125);
224 return 0;
225 }
226 }
227 case VIDIOCGUNIT: {
228 struct video_unit *v = arg;
229 v->video=VIDEO_NO_UNIT;
230 v->vbi=VIDEO_NO_UNIT;
231 v->radio=dev->minor;
232 v->audio=0;
233 v->teletext=VIDEO_NO_UNIT;
234 return 0;
235 }
236 default: return -ENOIOCTLCMD;
237 }
238 }
239
240 static int radio_ioctl(struct inode *inode, struct file *file,
241 unsigned int cmd, unsigned long arg)
242 {
243 struct video_device *dev = video_devdata(file);
244 struct radio_device *card=dev->priv;
245 int ret;
246
247 down(&card->lock);
248 ret = video_usercopy(inode, file, cmd, arg, radio_function);
249 up(&card->lock);
250 return ret;
251 }
252
253 static __u16 radio_install(struct pci_dev *pcidev);
254
255 MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
256 MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
257 MODULE_LICENSE("GPL");
258
259 static void __exit maestro_radio_exit(void)
260 {
261 video_unregister_device(&maestro_radio);
262 }
263
264 static int __init maestro_radio_init(void)
265 {
266 register __u16 found=0;
267 struct pci_dev *pcidev = NULL;
268 while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
269 PCI_DEVICE_ID_ESS_ESS1968,
270 pcidev)))
271 found |= radio_install(pcidev);
272 while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
273 PCI_DEVICE_ID_ESS_ESS1978,
274 pcidev)))
275 found |= radio_install(pcidev);
276 if(!found) {
277 printk(KERN_INFO "radio-maestro: no devices found.\n");
278 return -ENODEV;
279 }
280 return 0;
281 }
282
283 module_init(maestro_radio_init);
284 module_exit(maestro_radio_exit);
285
286 static inline __u16 radio_power_on(struct radio_device *dev)
287 {
288 register __u16 io=dev->io;
289 register __u32 ofreq;
290 __u16 omask, odir;
291 omask = inw(io + IO_MASK);
292 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
293 outw(odir & ~STR_WREN, io + IO_DIR);
294 dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
295 outw(odir, io + IO_DIR);
296 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
297 outw(dev->muted ? 0 : STR_WREN, io);
298 udelay(16);
299 outw(omask, io + IO_MASK);
300 ofreq = radio_bits_get(dev);
301 if((ofreq<FREQ2BITS(FREQ_LO)) || (ofreq>FREQ2BITS(FREQ_HI)))
302 ofreq = FREQ2BITS(FREQ_LO);
303 radio_bits_set(dev, ofreq);
304 return (ofreq == radio_bits_get(dev));
305 }
306
307 static __u16 radio_install(struct pci_dev *pcidev)
308 {
309 if(((pcidev->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
310 return 0;
311
312 radio_unit.io = pcidev->resource[0].start + GPIO_DATA;
313 maestro_radio.priv = &radio_unit;
314 init_MUTEX(&radio_unit.lock);
315
316 if(radio_power_on(&radio_unit)) {
317 if(video_register_device(&maestro_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
318 printk("radio-maestro: can't register device!");
319 return 0;
320 }
321 printk(KERN_INFO "radio-maestro: version "
322 DRIVER_VERSION
323 " time "
324 __TIME__ " "
325 __DATE__
326 "\n");
327 printk(KERN_INFO "radio-maestro: radio chip initialized\n");
328 return 1;
329 } else
330 return 0;
331 }
332