blob: 37c47fb95aca43fa63058143ee4edf7fffe7be79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips
3 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 <sound/driver.h>
24#include <asm/io.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/init.h>
28#include <sound/core.h>
29#include <sound/tea575x-tuner.h>
30
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020031MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070032MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
33MODULE_LICENSE("GPL");
34
35/*
36 * definitions
37 */
38
39#define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */
40#define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */
41#define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
42#define TEA575X_BIT_BAND_MASK (3<<20)
43#define TEA575X_BIT_BAND_FM (0<<20)
44#define TEA575X_BIT_BAND_MW (1<<20)
45#define TEA575X_BIT_BAND_LW (1<<21)
46#define TEA575X_BIT_BAND_SW (1<<22)
47#define TEA575X_BIT_PORT_0 (1<<19) /* user bit */
48#define TEA575X_BIT_PORT_1 (1<<18) /* user bit */
49#define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */
50#define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */
51#define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */
52#define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */
53#define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */
54#define TEA575X_BIT_DUMMY (1<<15) /* buffer */
55#define TEA575X_BIT_FREQ_MASK 0x7fff
56
57/*
58 * lowlevel part
59 */
60
Takashi Iwai97f02e02005-11-17 14:17:19 +010061static void snd_tea575x_set_freq(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 unsigned long freq;
64
65 freq = tea->freq / 16; /* to kHz */
66 if (freq > 108000)
67 freq = 108000;
68 if (freq < 87000)
69 freq = 87000;
70 /* crystal fixup */
71 if (tea->tea5759)
72 freq -= tea->freq_fixup;
73 else
74 freq += tea->freq_fixup;
75 /* freq /= 12.5 */
76 freq *= 10;
77 freq /= 125;
78
79 tea->val &= ~TEA575X_BIT_FREQ_MASK;
80 tea->val |= freq & TEA575X_BIT_FREQ_MASK;
81 tea->ops->write(tea, tea->val);
82}
83
84/*
85 * Linux Video interface
86 */
87
88static int snd_tea575x_ioctl(struct inode *inode, struct file *file,
89 unsigned int cmd, unsigned long data)
90{
91 struct video_device *dev = video_devdata(file);
Takashi Iwai97f02e02005-11-17 14:17:19 +010092 struct snd_tea575x *tea = video_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 void __user *arg = (void __user *)data;
94
95 switch(cmd) {
96 case VIDIOCGCAP:
97 {
98 struct video_capability v;
99 v.type = VID_TYPE_TUNER;
100 v.channels = 1;
101 v.audios = 1;
102 /* No we don't do pictures */
103 v.maxwidth = 0;
104 v.maxheight = 0;
105 v.minwidth = 0;
106 v.minheight = 0;
107 strcpy(v.name, tea->tea5759 ? "TEA5759" : "TEA5757");
108 if (copy_to_user(arg,&v,sizeof(v)))
109 return -EFAULT;
110 return 0;
111 }
112 case VIDIOCGTUNER:
113 {
114 struct video_tuner v;
115 if (copy_from_user(&v, arg,sizeof(v))!=0)
116 return -EFAULT;
117 if (v.tuner) /* Only 1 tuner */
118 return -EINVAL;
119 v.rangelow = (87*16000);
120 v.rangehigh = (108*16000);
121 v.flags = VIDEO_TUNER_LOW;
122 v.mode = VIDEO_MODE_AUTO;
123 strcpy(v.name, "FM");
124 v.signal = 0xFFFF;
125 if (copy_to_user(arg, &v, sizeof(v)))
126 return -EFAULT;
127 return 0;
128 }
129 case VIDIOCSTUNER:
130 {
131 struct video_tuner v;
132 if(copy_from_user(&v, arg, sizeof(v)))
133 return -EFAULT;
134 if(v.tuner!=0)
135 return -EINVAL;
136 /* Only 1 tuner so no setting needed ! */
137 return 0;
138 }
139 case VIDIOCGFREQ:
140 if(copy_to_user(arg, &tea->freq, sizeof(tea->freq)))
141 return -EFAULT;
142 return 0;
143 case VIDIOCSFREQ:
144 if(copy_from_user(&tea->freq, arg, sizeof(tea->freq)))
145 return -EFAULT;
146 snd_tea575x_set_freq(tea);
147 return 0;
148 case VIDIOCGAUDIO:
149 {
150 struct video_audio v;
151 memset(&v, 0, sizeof(v));
152 strcpy(v.name, "Radio");
153 if(copy_to_user(arg,&v, sizeof(v)))
154 return -EFAULT;
155 return 0;
156 }
157 case VIDIOCSAUDIO:
158 {
159 struct video_audio v;
160 if(copy_from_user(&v, arg, sizeof(v)))
161 return -EFAULT;
162 if(v.audio)
163 return -EINVAL;
164 return 0;
165 }
166 default:
167 return -ENOIOCTLCMD;
168 }
169}
170
171static void snd_tea575x_release(struct video_device *vfd)
172{
173}
174
175/*
176 * initialize all the tea575x chips
177 */
Takashi Iwai97f02e02005-11-17 14:17:19 +0100178void snd_tea575x_init(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 unsigned int val;
181
182 val = tea->ops->read(tea);
183 if (val == 0x1ffffff || val == 0) {
184 snd_printk(KERN_ERR "Cannot find TEA575x chip\n");
185 return;
186 }
187
188 memset(&tea->vd, 0, sizeof(tea->vd));
189 tea->vd.owner = tea->card->module;
190 strcpy(tea->vd.name, tea->tea5759 ? "TEA5759 radio" : "TEA5757 radio");
191 tea->vd.type = VID_TYPE_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 tea->vd.release = snd_tea575x_release;
193 video_set_drvdata(&tea->vd, tea);
194 tea->vd.fops = &tea->fops;
195 tea->fops.owner = tea->card->module;
196 tea->fops.open = video_exclusive_open;
197 tea->fops.release = video_exclusive_release;
198 tea->fops.ioctl = snd_tea575x_ioctl;
199 if (video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->dev_nr - 1) < 0) {
200 snd_printk(KERN_ERR "unable to register tea575x tuner\n");
201 return;
202 }
203 tea->vd_registered = 1;
204
205 tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
206 tea->freq = 90500 * 16; /* 90.5Mhz default */
207
208 snd_tea575x_set_freq(tea);
209}
210
Takashi Iwai97f02e02005-11-17 14:17:19 +0100211void snd_tea575x_exit(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 if (tea->vd_registered) {
214 video_unregister_device(&tea->vd);
215 tea->vd_registered = 0;
216 }
217}
218
219static int __init alsa_tea575x_module_init(void)
220{
221 return 0;
222}
223
224static void __exit alsa_tea575x_module_exit(void)
225{
226}
227
228module_init(alsa_tea575x_module_init)
229module_exit(alsa_tea575x_module_exit)
230
231EXPORT_SYMBOL(snd_tea575x_init);
232EXPORT_SYMBOL(snd_tea575x_exit);