Merge ../linux-2.6
[linux-2.6] / sound / i2c / other / tea575x-tuner.c
1 /*
2  *   ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips
3  *
4  *      Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  */      
22
23 #include <asm/io.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <sound/core.h>
28 #include <sound/tea575x-tuner.h>
29
30 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
31 MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
32 MODULE_LICENSE("GPL");
33
34 /*
35  * definitions
36  */
37
38 #define TEA575X_BIT_SEARCH      (1<<24)         /* 1 = search action, 0 = tuned */
39 #define TEA575X_BIT_UPDOWN      (1<<23)         /* 0 = search down, 1 = search up */
40 #define TEA575X_BIT_MONO        (1<<22)         /* 0 = stereo, 1 = mono */
41 #define TEA575X_BIT_BAND_MASK   (3<<20)
42 #define TEA575X_BIT_BAND_FM     (0<<20)
43 #define TEA575X_BIT_BAND_MW     (1<<20)
44 #define TEA575X_BIT_BAND_LW     (1<<21)
45 #define TEA575X_BIT_BAND_SW     (1<<22)
46 #define TEA575X_BIT_PORT_0      (1<<19)         /* user bit */
47 #define TEA575X_BIT_PORT_1      (1<<18)         /* user bit */
48 #define TEA575X_BIT_SEARCH_MASK (3<<16)         /* search level */
49 #define TEA575X_BIT_SEARCH_5_28      (0<<16)    /* FM >5uV, AM >28uV */
50 #define TEA575X_BIT_SEARCH_10_40     (1<<16)    /* FM >10uV, AM > 40uV */
51 #define TEA575X_BIT_SEARCH_30_63     (2<<16)    /* FM >30uV, AM > 63uV */
52 #define TEA575X_BIT_SEARCH_150_1000  (3<<16)    /* FM > 150uV, AM > 1000uV */
53 #define TEA575X_BIT_DUMMY       (1<<15)         /* buffer */
54 #define TEA575X_BIT_FREQ_MASK   0x7fff
55
56 /*
57  * lowlevel part
58  */
59
60 static void snd_tea575x_set_freq(struct snd_tea575x *tea)
61 {
62         unsigned long freq;
63
64         freq = tea->freq / 16;          /* to kHz */
65         if (freq > 108000)
66                 freq = 108000;
67         if (freq < 87000)
68                 freq = 87000;
69         /* crystal fixup */
70         if (tea->tea5759)
71                 freq -= tea->freq_fixup;
72         else
73                 freq += tea->freq_fixup;
74         /* freq /= 12.5 */
75         freq *= 10;
76         freq /= 125;
77
78         tea->val &= ~TEA575X_BIT_FREQ_MASK;
79         tea->val |= freq & TEA575X_BIT_FREQ_MASK;
80         tea->ops->write(tea, tea->val);
81 }
82
83 /*
84  * Linux Video interface
85  */
86
87 static int snd_tea575x_ioctl(struct inode *inode, struct file *file,
88                              unsigned int cmd, unsigned long data)
89 {
90         struct video_device *dev = video_devdata(file);
91         struct snd_tea575x *tea = video_get_drvdata(dev);
92         void __user *arg = (void __user *)data;
93         
94         switch(cmd) {
95                 case VIDIOCGCAP:
96                 {
97                         struct video_capability v;
98                         v.type = VID_TYPE_TUNER;
99                         v.channels = 1;
100                         v.audios = 1;
101                         /* No we don't do pictures */
102                         v.maxwidth = 0;
103                         v.maxheight = 0;
104                         v.minwidth = 0;
105                         v.minheight = 0;
106                         strcpy(v.name, tea->tea5759 ? "TEA5759" : "TEA5757");
107                         if (copy_to_user(arg,&v,sizeof(v)))
108                                 return -EFAULT;
109                         return 0;
110                 }
111                 case VIDIOCGTUNER:
112                 {
113                         struct video_tuner v;
114                         if (copy_from_user(&v, arg,sizeof(v))!=0) 
115                                 return -EFAULT;
116                         if (v.tuner)    /* Only 1 tuner */ 
117                                 return -EINVAL;
118                         v.rangelow = (87*16000);
119                         v.rangehigh = (108*16000);
120                         v.flags = VIDEO_TUNER_LOW;
121                         v.mode = VIDEO_MODE_AUTO;
122                         strcpy(v.name, "FM");
123                         v.signal = 0xFFFF;
124                         if (copy_to_user(arg, &v, sizeof(v)))
125                                 return -EFAULT;
126                         return 0;
127                 }
128                 case VIDIOCSTUNER:
129                 {
130                         struct video_tuner v;
131                         if(copy_from_user(&v, arg, sizeof(v)))
132                                 return -EFAULT;
133                         if(v.tuner!=0)
134                                 return -EINVAL;
135                         /* Only 1 tuner so no setting needed ! */
136                         return 0;
137                 }
138                 case VIDIOCGFREQ:
139                         if(copy_to_user(arg, &tea->freq, sizeof(tea->freq)))
140                                 return -EFAULT;
141                         return 0;
142                 case VIDIOCSFREQ:
143                         if(copy_from_user(&tea->freq, arg, sizeof(tea->freq)))
144                                 return -EFAULT;
145                         snd_tea575x_set_freq(tea);
146                         return 0;
147                 case VIDIOCGAUDIO:
148                 {       
149                         struct video_audio v;
150                         memset(&v, 0, sizeof(v));
151                         strcpy(v.name, "Radio");
152                         if(copy_to_user(arg,&v, sizeof(v)))
153                                 return -EFAULT;
154                         return 0;                       
155                 }
156                 case VIDIOCSAUDIO:
157                 {
158                         struct video_audio v;
159                         if(copy_from_user(&v, arg, sizeof(v))) 
160                                 return -EFAULT; 
161                         if (tea->ops->mute)
162                                 tea->ops->mute(tea,
163                                                (v.flags &
164                                                 VIDEO_AUDIO_MUTE) ? 1 : 0);
165                         if(v.audio) 
166                                 return -EINVAL;
167                         return 0;
168                 }
169                 default:
170                         return -ENOIOCTLCMD;
171         }
172 }
173
174 static void snd_tea575x_release(struct video_device *vfd)
175 {
176 }
177
178 /*
179  * initialize all the tea575x chips
180  */
181 void snd_tea575x_init(struct snd_tea575x *tea)
182 {
183         unsigned int val;
184
185         val = tea->ops->read(tea);
186         if (val == 0x1ffffff || val == 0) {
187                 snd_printk(KERN_ERR "Cannot find TEA575x chip\n");
188                 return;
189         }
190
191         memset(&tea->vd, 0, sizeof(tea->vd));
192         strcpy(tea->vd.name, tea->tea5759 ? "TEA5759 radio" : "TEA5757 radio");
193         tea->vd.release = snd_tea575x_release;
194         video_set_drvdata(&tea->vd, tea);
195         tea->vd.fops = &tea->fops;
196         tea->fops.owner = tea->card->module;
197         tea->fops.open = video_exclusive_open;
198         tea->fops.release = video_exclusive_release;
199         tea->fops.ioctl = snd_tea575x_ioctl;
200         if (video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->dev_nr - 1) < 0) {
201                 snd_printk(KERN_ERR "unable to register tea575x tuner\n");
202                 return;
203         }
204         tea->vd_registered = 1;
205
206         tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
207         tea->freq = 90500 * 16;         /* 90.5Mhz default */
208
209         snd_tea575x_set_freq(tea);
210
211         /* mute on init */
212         if (tea->ops->mute)
213                 tea->ops->mute(tea, 1);
214 }
215
216 void snd_tea575x_exit(struct snd_tea575x *tea)
217 {
218         if (tea->vd_registered) {
219                 video_unregister_device(&tea->vd);
220                 tea->vd_registered = 0;
221         }
222 }
223
224 static int __init alsa_tea575x_module_init(void)
225 {
226         return 0;
227 }
228         
229 static void __exit alsa_tea575x_module_exit(void)
230 {
231 }
232         
233 module_init(alsa_tea575x_module_init)
234 module_exit(alsa_tea575x_module_exit)
235
236 EXPORT_SYMBOL(snd_tea575x_init);
237 EXPORT_SYMBOL(snd_tea575x_exit);