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/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/version.h>
29 #include <media/v4l2-dev.h>
30 #include <media/v4l2-ioctl.h>
31 #include <sound/tea575x-tuner.h>
32 
33 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
34 MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
35 MODULE_LICENSE("GPL");
36 
37 static int radio_nr = -1;
38 module_param(radio_nr, int, 0);
39 
40 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
41 #define FREQ_LO		 (50UL * 16000)
42 #define FREQ_HI		(150UL * 16000)
43 
44 /*
45  * definitions
46  */
47 
48 #define TEA575X_BIT_SEARCH	(1<<24)		/* 1 = search action, 0 = tuned */
49 #define TEA575X_BIT_UPDOWN	(1<<23)		/* 0 = search down, 1 = search up */
50 #define TEA575X_BIT_MONO	(1<<22)		/* 0 = stereo, 1 = mono */
51 #define TEA575X_BIT_BAND_MASK	(3<<20)
52 #define TEA575X_BIT_BAND_FM	(0<<20)
53 #define TEA575X_BIT_BAND_MW	(1<<20)
54 #define TEA575X_BIT_BAND_LW	(1<<21)
55 #define TEA575X_BIT_BAND_SW	(1<<22)
56 #define TEA575X_BIT_PORT_0	(1<<19)		/* user bit */
57 #define TEA575X_BIT_PORT_1	(1<<18)		/* user bit */
58 #define TEA575X_BIT_SEARCH_MASK	(3<<16)		/* search level */
59 #define TEA575X_BIT_SEARCH_5_28	     (0<<16)	/* FM >5uV, AM >28uV */
60 #define TEA575X_BIT_SEARCH_10_40     (1<<16)	/* FM >10uV, AM > 40uV */
61 #define TEA575X_BIT_SEARCH_30_63     (2<<16)	/* FM >30uV, AM > 63uV */
62 #define TEA575X_BIT_SEARCH_150_1000  (3<<16)	/* FM > 150uV, AM > 1000uV */
63 #define TEA575X_BIT_DUMMY	(1<<15)		/* buffer */
64 #define TEA575X_BIT_FREQ_MASK	0x7fff
65 
66 /*
67  * lowlevel part
68  */
69 
snd_tea575x_write(struct snd_tea575x * tea,unsigned int val)70 static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
71 {
72 	u16 l;
73 	u8 data;
74 
75 	tea->ops->set_direction(tea, 1);
76 	udelay(16);
77 
78 	for (l = 25; l > 0; l--) {
79 		data = (val >> 24) & TEA575X_DATA;
80 		val <<= 1;			/* shift data */
81 		tea->ops->set_pins(tea, data | TEA575X_WREN);
82 		udelay(2);
83 		tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
84 		udelay(2);
85 		tea->ops->set_pins(tea, data | TEA575X_WREN);
86 		udelay(2);
87 	}
88 
89 	if (!tea->mute)
90 		tea->ops->set_pins(tea, 0);
91 }
92 
snd_tea575x_read(struct snd_tea575x * tea)93 static unsigned int snd_tea575x_read(struct snd_tea575x *tea)
94 {
95 	u16 l, rdata;
96 	u32 data = 0;
97 
98 	tea->ops->set_direction(tea, 0);
99 	tea->ops->set_pins(tea, 0);
100 	udelay(16);
101 
102 	for (l = 24; l--;) {
103 		tea->ops->set_pins(tea, TEA575X_CLK);
104 		udelay(2);
105 		if (!l)
106 			tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
107 		tea->ops->set_pins(tea, 0);
108 		udelay(2);
109 		data <<= 1;			/* shift data */
110 		rdata = tea->ops->get_pins(tea);
111 		if (!l)
112 			tea->stereo = (rdata & TEA575X_MOST) ?  0 : 1;
113 		if (rdata & TEA575X_DATA)
114 			data++;
115 		udelay(2);
116 	}
117 
118 	if (tea->mute)
119 		tea->ops->set_pins(tea, TEA575X_WREN);
120 
121 	return data;
122 }
123 
snd_tea575x_get_freq(struct snd_tea575x * tea)124 static void snd_tea575x_get_freq(struct snd_tea575x *tea)
125 {
126 	unsigned long freq;
127 
128 	freq = snd_tea575x_read(tea) & TEA575X_BIT_FREQ_MASK;
129 	/* freq *= 12.5 */
130 	freq *= 125;
131 	freq /= 10;
132 	/* crystal fixup */
133 	if (tea->tea5759)
134 		freq += TEA575X_FMIF;
135 	else
136 		freq -= TEA575X_FMIF;
137 
138 	tea->freq = freq * 16;		/* from kHz */
139 }
140 
snd_tea575x_set_freq(struct snd_tea575x * tea)141 static void snd_tea575x_set_freq(struct snd_tea575x *tea)
142 {
143 	unsigned long freq;
144 
145 	freq = clamp(tea->freq, FREQ_LO, FREQ_HI);
146 	freq /= 16;		/* to kHz */
147 	/* crystal fixup */
148 	if (tea->tea5759)
149 		freq -= TEA575X_FMIF;
150 	else
151 		freq += TEA575X_FMIF;
152 	/* freq /= 12.5 */
153 	freq *= 10;
154 	freq /= 125;
155 
156 	tea->val &= ~TEA575X_BIT_FREQ_MASK;
157 	tea->val |= freq & TEA575X_BIT_FREQ_MASK;
158 	snd_tea575x_write(tea, tea->val);
159 }
160 
161 /*
162  * Linux Video interface
163  */
164 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * v)165 static int vidioc_querycap(struct file *file, void  *priv,
166 					struct v4l2_capability *v)
167 {
168 	struct snd_tea575x *tea = video_drvdata(file);
169 
170 	strlcpy(v->driver, "tea575x-tuner", sizeof(v->driver));
171 	strlcpy(v->card, tea->card, sizeof(v->card));
172 	strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
173 	strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
174 	v->version = RADIO_VERSION;
175 	v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
176 	return 0;
177 }
178 
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * v)179 static int vidioc_g_tuner(struct file *file, void *priv,
180 					struct v4l2_tuner *v)
181 {
182 	struct snd_tea575x *tea = video_drvdata(file);
183 
184 	if (v->index > 0)
185 		return -EINVAL;
186 
187 	snd_tea575x_read(tea);
188 
189 	strcpy(v->name, "FM");
190 	v->type = V4L2_TUNER_RADIO;
191 	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
192 	v->rangelow = FREQ_LO;
193 	v->rangehigh = FREQ_HI;
194 	v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
195 	v->audmode = tea->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
196 	v->signal = tea->tuned ? 0xffff : 0;
197 
198 	return 0;
199 }
200 
vidioc_s_tuner(struct file * file,void * priv,struct v4l2_tuner * v)201 static int vidioc_s_tuner(struct file *file, void *priv,
202 					struct v4l2_tuner *v)
203 {
204 	if (v->index > 0)
205 		return -EINVAL;
206 	return 0;
207 }
208 
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)209 static int vidioc_g_frequency(struct file *file, void *priv,
210 					struct v4l2_frequency *f)
211 {
212 	struct snd_tea575x *tea = video_drvdata(file);
213 
214 	if (f->tuner != 0)
215 		return -EINVAL;
216 	f->type = V4L2_TUNER_RADIO;
217 	snd_tea575x_get_freq(tea);
218 	f->frequency = tea->freq;
219 	return 0;
220 }
221 
vidioc_s_frequency(struct file * file,void * priv,struct v4l2_frequency * f)222 static int vidioc_s_frequency(struct file *file, void *priv,
223 					struct v4l2_frequency *f)
224 {
225 	struct snd_tea575x *tea = video_drvdata(file);
226 
227 	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
228 		return -EINVAL;
229 
230 	if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
231 		return -EINVAL;
232 
233 	tea->freq = f->frequency;
234 
235 	snd_tea575x_set_freq(tea);
236 
237 	return 0;
238 }
239 
vidioc_g_audio(struct file * file,void * priv,struct v4l2_audio * a)240 static int vidioc_g_audio(struct file *file, void *priv,
241 					struct v4l2_audio *a)
242 {
243 	if (a->index > 1)
244 		return -EINVAL;
245 
246 	strcpy(a->name, "Radio");
247 	a->capability = V4L2_AUDCAP_STEREO;
248 	return 0;
249 }
250 
vidioc_s_audio(struct file * file,void * priv,struct v4l2_audio * a)251 static int vidioc_s_audio(struct file *file, void *priv,
252 					struct v4l2_audio *a)
253 {
254 	if (a->index != 0)
255 		return -EINVAL;
256 	return 0;
257 }
258 
tea575x_s_ctrl(struct v4l2_ctrl * ctrl)259 static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
260 {
261 	struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
262 
263 	switch (ctrl->id) {
264 	case V4L2_CID_AUDIO_MUTE:
265 		tea->mute = ctrl->val;
266 		snd_tea575x_set_freq(tea);
267 		return 0;
268 	}
269 
270 	return -EINVAL;
271 }
272 
273 static const struct v4l2_file_operations tea575x_fops = {
274 	.owner		= THIS_MODULE,
275 	.unlocked_ioctl	= video_ioctl2,
276 };
277 
278 static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
279 	.vidioc_querycap    = vidioc_querycap,
280 	.vidioc_g_tuner     = vidioc_g_tuner,
281 	.vidioc_s_tuner     = vidioc_s_tuner,
282 	.vidioc_g_audio     = vidioc_g_audio,
283 	.vidioc_s_audio     = vidioc_s_audio,
284 	.vidioc_g_frequency = vidioc_g_frequency,
285 	.vidioc_s_frequency = vidioc_s_frequency,
286 };
287 
288 static struct video_device tea575x_radio = {
289 	.name           = "tea575x-tuner",
290 	.fops           = &tea575x_fops,
291 	.ioctl_ops 	= &tea575x_ioctl_ops,
292 	.release	= video_device_release_empty,
293 };
294 
295 static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
296 	.s_ctrl = tea575x_s_ctrl,
297 };
298 
299 /*
300  * initialize all the tea575x chips
301  */
snd_tea575x_init(struct snd_tea575x * tea)302 int snd_tea575x_init(struct snd_tea575x *tea)
303 {
304 	int retval;
305 
306 	tea->mute = 1;
307 
308 	snd_tea575x_write(tea, 0x55AA);
309 	if (snd_tea575x_read(tea) != 0x55AA)
310 		return -ENODEV;
311 
312 	tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
313 	tea->freq = 90500 * 16;		/* 90.5Mhz default */
314 	snd_tea575x_set_freq(tea);
315 
316 	tea->vd = tea575x_radio;
317 	video_set_drvdata(&tea->vd, tea);
318 	mutex_init(&tea->mutex);
319 	tea->vd.lock = &tea->mutex;
320 
321 	v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
322 	tea->vd.ctrl_handler = &tea->ctrl_handler;
323 	v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
324 	retval = tea->ctrl_handler.error;
325 	if (retval) {
326 		printk(KERN_ERR "tea575x-tuner: can't initialize controls\n");
327 		v4l2_ctrl_handler_free(&tea->ctrl_handler);
328 		return retval;
329 	}
330 
331 	if (tea->ext_init) {
332 		retval = tea->ext_init(tea);
333 		if (retval) {
334 			v4l2_ctrl_handler_free(&tea->ctrl_handler);
335 			return retval;
336 		}
337 	}
338 
339 	v4l2_ctrl_handler_setup(&tea->ctrl_handler);
340 
341 	retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, radio_nr);
342 	if (retval) {
343 		printk(KERN_ERR "tea575x-tuner: can't register video device!\n");
344 		v4l2_ctrl_handler_free(&tea->ctrl_handler);
345 		return retval;
346 	}
347 
348 	return 0;
349 }
350 
snd_tea575x_exit(struct snd_tea575x * tea)351 void snd_tea575x_exit(struct snd_tea575x *tea)
352 {
353 	video_unregister_device(&tea->vd);
354 	v4l2_ctrl_handler_free(&tea->ctrl_handler);
355 }
356 
alsa_tea575x_module_init(void)357 static int __init alsa_tea575x_module_init(void)
358 {
359 	return 0;
360 }
361 
alsa_tea575x_module_exit(void)362 static void __exit alsa_tea575x_module_exit(void)
363 {
364 }
365 
366 module_init(alsa_tea575x_module_init)
367 module_exit(alsa_tea575x_module_exit)
368 
369 EXPORT_SYMBOL(snd_tea575x_init);
370 EXPORT_SYMBOL(snd_tea575x_exit);
371