2 * ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips
4 * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
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.
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.
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
24 #include <linux/delay.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-dev.h>
31 #include <media/v4l2-fh.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-event.h>
34 #include <sound/tea575x-tuner.h>
36 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
37 MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
38 MODULE_LICENSE("GPL");
40 #define FREQ_LO ((tea->tea5759 ? 760 : 875) * 1600U)
41 #define FREQ_HI ((tea->tea5759 ? 910 : 1080) * 1600U)
47 #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */
48 #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */
49 #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
50 #define TEA575X_BIT_BAND_MASK (3<<20)
51 #define TEA575X_BIT_BAND_FM (0<<20)
52 #define TEA575X_BIT_BAND_MW (1<<20)
53 #define TEA575X_BIT_BAND_LW (1<<21)
54 #define TEA575X_BIT_BAND_SW (1<<22)
55 #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */
56 #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */
57 #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */
58 #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */
59 #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */
60 #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */
61 #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */
62 #define TEA575X_BIT_DUMMY (1<<15) /* buffer */
63 #define TEA575X_BIT_FREQ_MASK 0x7fff
69 static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
74 if (tea->ops->write_val)
75 return tea->ops->write_val(tea, val);
77 tea->ops->set_direction(tea, 1);
80 for (l = 25; l > 0; l--) {
81 data = (val >> 24) & TEA575X_DATA;
82 val <<= 1; /* shift data */
83 tea->ops->set_pins(tea, data | TEA575X_WREN);
85 tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
87 tea->ops->set_pins(tea, data | TEA575X_WREN);
92 tea->ops->set_pins(tea, 0);
95 static u32 snd_tea575x_read(struct snd_tea575x *tea)
100 if (tea->ops->read_val)
101 return tea->ops->read_val(tea);
103 tea->ops->set_direction(tea, 0);
104 tea->ops->set_pins(tea, 0);
108 tea->ops->set_pins(tea, TEA575X_CLK);
111 tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
112 tea->ops->set_pins(tea, 0);
114 data <<= 1; /* shift data */
115 rdata = tea->ops->get_pins(tea);
117 tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
118 if (rdata & TEA575X_DATA)
124 tea->ops->set_pins(tea, TEA575X_WREN);
129 static u32 snd_tea575x_val_to_freq(struct snd_tea575x *tea, u32 val)
131 u32 freq = val & TEA575X_BIT_FREQ_MASK;
141 freq += TEA575X_FMIF;
143 freq -= TEA575X_FMIF;
145 return clamp(freq * 16, FREQ_LO, FREQ_HI); /* from kHz */
148 static u32 snd_tea575x_get_freq(struct snd_tea575x *tea)
150 return snd_tea575x_val_to_freq(tea, snd_tea575x_read(tea));
153 static void snd_tea575x_set_freq(struct snd_tea575x *tea)
155 u32 freq = tea->freq;
157 freq /= 16; /* to kHz */
160 freq -= TEA575X_FMIF;
162 freq += TEA575X_FMIF;
167 tea->val &= ~TEA575X_BIT_FREQ_MASK;
168 tea->val |= freq & TEA575X_BIT_FREQ_MASK;
169 snd_tea575x_write(tea, tea->val);
170 tea->freq = snd_tea575x_val_to_freq(tea, tea->val);
174 * Linux Video interface
177 static int vidioc_querycap(struct file *file, void *priv,
178 struct v4l2_capability *v)
180 struct snd_tea575x *tea = video_drvdata(file);
182 strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
183 strlcpy(v->card, tea->card, sizeof(v->card));
184 strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
185 strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
186 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
187 if (!tea->cannot_read_data)
188 v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
189 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
193 static int vidioc_g_tuner(struct file *file, void *priv,
194 struct v4l2_tuner *v)
196 struct snd_tea575x *tea = video_drvdata(file);
201 snd_tea575x_read(tea);
203 strcpy(v->name, "FM");
204 v->type = V4L2_TUNER_RADIO;
205 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
206 if (!tea->cannot_read_data)
207 v->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED;
208 v->rangelow = FREQ_LO;
209 v->rangehigh = FREQ_HI;
210 v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
211 v->audmode = (tea->val & TEA575X_BIT_MONO) ?
212 V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
213 v->signal = tea->tuned ? 0xffff : 0;
217 static int vidioc_s_tuner(struct file *file, void *priv,
218 struct v4l2_tuner *v)
220 struct snd_tea575x *tea = video_drvdata(file);
224 tea->val &= ~TEA575X_BIT_MONO;
225 if (v->audmode == V4L2_TUNER_MODE_MONO)
226 tea->val |= TEA575X_BIT_MONO;
227 snd_tea575x_write(tea, tea->val);
231 static int vidioc_g_frequency(struct file *file, void *priv,
232 struct v4l2_frequency *f)
234 struct snd_tea575x *tea = video_drvdata(file);
238 f->type = V4L2_TUNER_RADIO;
239 f->frequency = tea->freq;
243 static int vidioc_s_frequency(struct file *file, void *priv,
244 struct v4l2_frequency *f)
246 struct snd_tea575x *tea = video_drvdata(file);
248 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
251 tea->val &= ~TEA575X_BIT_SEARCH;
252 tea->freq = clamp(f->frequency, FREQ_LO, FREQ_HI);
253 snd_tea575x_set_freq(tea);
257 static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
258 struct v4l2_hw_freq_seek *a)
260 struct snd_tea575x *tea = video_drvdata(file);
261 unsigned long timeout;
264 if (tea->cannot_read_data)
266 if (a->tuner || a->wrap_around)
269 /* clear the frequency, HW will fill it in */
270 tea->val &= ~TEA575X_BIT_FREQ_MASK;
271 tea->val |= TEA575X_BIT_SEARCH;
273 tea->val |= TEA575X_BIT_UPDOWN;
275 tea->val &= ~TEA575X_BIT_UPDOWN;
276 snd_tea575x_write(tea, tea->val);
277 timeout = jiffies + msecs_to_jiffies(10000);
279 if (time_after(jiffies, timeout))
281 if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
282 /* some signal arrived, stop search */
283 tea->val &= ~TEA575X_BIT_SEARCH;
284 snd_tea575x_set_freq(tea);
287 if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) {
290 /* Found a frequency, wait until it can be read */
291 for (i = 0; i < 100; i++) {
293 freq = snd_tea575x_get_freq(tea);
294 if (freq) /* available */
297 if (freq == 0) /* shouldn't happen */
300 * if we moved by less than 50 kHz, or in the wrong
301 * direction, continue seeking
303 if (abs(tea->freq - freq) < 16 * 50 ||
304 (a->seek_upward && freq < tea->freq) ||
305 (!a->seek_upward && freq > tea->freq)) {
306 snd_tea575x_write(tea, tea->val);
310 tea->val &= ~TEA575X_BIT_SEARCH;
314 tea->val &= ~TEA575X_BIT_SEARCH;
315 snd_tea575x_set_freq(tea);
319 static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
321 struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
324 case V4L2_CID_AUDIO_MUTE:
325 tea->mute = ctrl->val;
326 snd_tea575x_set_freq(tea);
333 static const struct v4l2_file_operations tea575x_fops = {
334 .unlocked_ioctl = video_ioctl2,
335 .open = v4l2_fh_open,
336 .release = v4l2_fh_release,
337 .poll = v4l2_ctrl_poll,
340 static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
341 .vidioc_querycap = vidioc_querycap,
342 .vidioc_g_tuner = vidioc_g_tuner,
343 .vidioc_s_tuner = vidioc_s_tuner,
344 .vidioc_g_frequency = vidioc_g_frequency,
345 .vidioc_s_frequency = vidioc_s_frequency,
346 .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
347 .vidioc_log_status = v4l2_ctrl_log_status,
348 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
349 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
352 static const struct video_device tea575x_radio = {
353 .ioctl_ops = &tea575x_ioctl_ops,
354 .release = video_device_release_empty,
357 static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
358 .s_ctrl = tea575x_s_ctrl,
362 * initialize all the tea575x chips
364 int snd_tea575x_init(struct snd_tea575x *tea, struct module *owner)
370 /* Not all devices can or know how to read the data back.
371 Such devices can set cannot_read_data to true. */
372 if (!tea->cannot_read_data) {
373 snd_tea575x_write(tea, 0x55AA);
374 if (snd_tea575x_read(tea) != 0x55AA)
378 tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28;
379 tea->freq = 90500 * 16; /* 90.5Mhz default */
380 snd_tea575x_set_freq(tea);
382 tea->vd = tea575x_radio;
383 video_set_drvdata(&tea->vd, tea);
384 mutex_init(&tea->mutex);
385 strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
386 tea->vd.lock = &tea->mutex;
387 tea->vd.v4l2_dev = tea->v4l2_dev;
388 tea->fops = tea575x_fops;
389 tea->fops.owner = owner;
390 tea->vd.fops = &tea->fops;
391 set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
392 /* disable hw_freq_seek if we can't use it */
393 if (tea->cannot_read_data)
394 v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK);
396 if (!tea->cannot_mute) {
397 tea->vd.ctrl_handler = &tea->ctrl_handler;
398 v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
399 v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
400 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
401 retval = tea->ctrl_handler.error;
403 v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
404 v4l2_ctrl_handler_free(&tea->ctrl_handler);
409 retval = tea->ext_init(tea);
411 v4l2_ctrl_handler_free(&tea->ctrl_handler);
416 v4l2_ctrl_handler_setup(&tea->ctrl_handler);
419 retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
421 v4l2_err(tea->v4l2_dev, "can't register video device!\n");
422 v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
429 void snd_tea575x_exit(struct snd_tea575x *tea)
431 video_unregister_device(&tea->vd);
432 v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
435 static int __init alsa_tea575x_module_init(void)
440 static void __exit alsa_tea575x_module_exit(void)
444 module_init(alsa_tea575x_module_init)
445 module_exit(alsa_tea575x_module_exit)
447 EXPORT_SYMBOL(snd_tea575x_init);
448 EXPORT_SYMBOL(snd_tea575x_exit);