blob: 4831800239d3b786081de5b17b06bff2e2d8caa9 [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 *
Igor M. Liplianin4b296312008-11-09 15:25:31 -030021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/io.h>
24#include <linux/delay.h>
25#include <linux/interrupt.h>
26#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -030028#include <linux/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include <sound/tea575x-tuner.h>
31
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020032MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
34MODULE_LICENSE("GPL");
35
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -030036static int radio_nr = -1;
37module_param(radio_nr, int, 0);
38
39#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
Ondrej Zary375d1352011-03-19 16:32:53 +010040#define FREQ_LO (50UL * 16000)
41#define FREQ_HI (150UL * 16000)
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -030042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * definitions
45 */
46
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
64
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -030065static struct v4l2_queryctrl radio_qctrl[] = {
66 {
67 .id = V4L2_CID_AUDIO_MUTE,
68 .name = "Mute",
69 .minimum = 0,
70 .maximum = 1,
71 .default_value = 1,
72 .type = V4L2_CTRL_TYPE_BOOLEAN,
73 }
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * lowlevel part
78 */
79
Ondrej Zary14219d02011-05-09 23:39:26 +020080static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
81{
82 u16 l;
83 u8 data;
84
85 tea->ops->set_direction(tea, 1);
86 udelay(16);
87
88 for (l = 25; l > 0; l--) {
89 data = (val >> 24) & TEA575X_DATA;
90 val <<= 1; /* shift data */
91 tea->ops->set_pins(tea, data | TEA575X_WREN);
92 udelay(2);
93 tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
94 udelay(2);
95 tea->ops->set_pins(tea, data | TEA575X_WREN);
96 udelay(2);
97 }
98
99 if (!tea->mute)
100 tea->ops->set_pins(tea, 0);
101}
102
103static unsigned int snd_tea575x_read(struct snd_tea575x *tea)
104{
105 u16 l, rdata;
106 u32 data = 0;
107
108 tea->ops->set_direction(tea, 0);
109 tea->ops->set_pins(tea, 0);
110 udelay(16);
111
112 for (l = 24; l--;) {
113 tea->ops->set_pins(tea, TEA575X_CLK);
114 udelay(2);
115 if (!l)
116 tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
117 tea->ops->set_pins(tea, 0);
118 udelay(2);
119 data <<= 1; /* shift data */
120 rdata = tea->ops->get_pins(tea);
121 if (!l)
122 tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
123 if (rdata & TEA575X_DATA)
124 data++;
125 udelay(2);
126 }
127
128 if (tea->mute)
129 tea->ops->set_pins(tea, TEA575X_WREN);
130
131 return data;
132}
133
Ondrej Zary375d1352011-03-19 16:32:53 +0100134static void snd_tea575x_get_freq(struct snd_tea575x *tea)
135{
136 unsigned long freq;
137
Ondrej Zary14219d02011-05-09 23:39:26 +0200138 freq = snd_tea575x_read(tea) & TEA575X_BIT_FREQ_MASK;
Ondrej Zary375d1352011-03-19 16:32:53 +0100139 /* freq *= 12.5 */
140 freq *= 125;
141 freq /= 10;
142 /* crystal fixup */
143 if (tea->tea5759)
Ondrej Zaryea273162011-05-12 22:17:56 +0200144 freq += TEA575X_FMIF;
Ondrej Zary375d1352011-03-19 16:32:53 +0100145 else
Ondrej Zaryea273162011-05-12 22:17:56 +0200146 freq -= TEA575X_FMIF;
Ondrej Zary375d1352011-03-19 16:32:53 +0100147
148 tea->freq = freq * 16; /* from kHz */
149}
150
Takashi Iwai97f02e02005-11-17 14:17:19 +0100151static void snd_tea575x_set_freq(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 unsigned long freq;
154
Ondrej Zary375d1352011-03-19 16:32:53 +0100155 freq = clamp(tea->freq, FREQ_LO, FREQ_HI);
156 freq /= 16; /* to kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* crystal fixup */
158 if (tea->tea5759)
Ondrej Zaryea273162011-05-12 22:17:56 +0200159 freq -= TEA575X_FMIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 else
Ondrej Zaryea273162011-05-12 22:17:56 +0200161 freq += TEA575X_FMIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* freq /= 12.5 */
163 freq *= 10;
164 freq /= 125;
165
166 tea->val &= ~TEA575X_BIT_FREQ_MASK;
167 tea->val |= freq & TEA575X_BIT_FREQ_MASK;
Ondrej Zary14219d02011-05-09 23:39:26 +0200168 snd_tea575x_write(tea, tea->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/*
172 * Linux Video interface
173 */
174
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300175static int vidioc_querycap(struct file *file, void *priv,
176 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300178 struct snd_tea575x *tea = video_drvdata(file);
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300179
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300180 strlcpy(v->driver, "tea575x-tuner", sizeof(v->driver));
Ondrej Zary10ca7202011-05-12 22:18:22 +0200181 strlcpy(v->card, tea->card, sizeof(v->card));
182 strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
183 strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300184 v->version = RADIO_VERSION;
Ondrej Zary375d1352011-03-19 16:32:53 +0100185 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300189static int vidioc_g_tuner(struct file *file, void *priv,
190 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Ondrej Zary375d1352011-03-19 16:32:53 +0100192 struct snd_tea575x *tea = video_drvdata(file);
193
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300194 if (v->index > 0)
195 return -EINVAL;
196
Ondrej Zary14219d02011-05-09 23:39:26 +0200197 snd_tea575x_read(tea);
Ondrej Zary375d1352011-03-19 16:32:53 +0100198
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300199 strcpy(v->name, "FM");
200 v->type = V4L2_TUNER_RADIO;
Ondrej Zary375d1352011-03-19 16:32:53 +0100201 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300202 v->rangelow = FREQ_LO;
203 v->rangehigh = FREQ_HI;
Ondrej Zary375d1352011-03-19 16:32:53 +0100204 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
205 v->audmode = tea->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
206 v->signal = tea->tuned ? 0xffff : 0;
207
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300208 return 0;
209}
210
211static int vidioc_s_tuner(struct file *file, void *priv,
212 struct v4l2_tuner *v)
213{
214 if (v->index > 0)
215 return -EINVAL;
216 return 0;
217}
218
219static int vidioc_g_frequency(struct file *file, void *priv,
220 struct v4l2_frequency *f)
221{
222 struct snd_tea575x *tea = video_drvdata(file);
223
Ondrej Zary375d1352011-03-19 16:32:53 +0100224 if (f->tuner != 0)
225 return -EINVAL;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300226 f->type = V4L2_TUNER_RADIO;
Ondrej Zary375d1352011-03-19 16:32:53 +0100227 snd_tea575x_get_freq(tea);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300228 f->frequency = tea->freq;
229 return 0;
230}
231
232static int vidioc_s_frequency(struct file *file, void *priv,
233 struct v4l2_frequency *f)
234{
235 struct snd_tea575x *tea = video_drvdata(file);
236
Ondrej Zary375d1352011-03-19 16:32:53 +0100237 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
238 return -EINVAL;
239
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300240 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
241 return -EINVAL;
242
243 tea->freq = f->frequency;
244
245 snd_tea575x_set_freq(tea);
246
247 return 0;
248}
249
250static int vidioc_g_audio(struct file *file, void *priv,
251 struct v4l2_audio *a)
252{
253 if (a->index > 1)
254 return -EINVAL;
255
256 strcpy(a->name, "Radio");
257 a->capability = V4L2_AUDCAP_STEREO;
258 return 0;
259}
260
261static int vidioc_s_audio(struct file *file, void *priv,
262 struct v4l2_audio *a)
263{
264 if (a->index != 0)
265 return -EINVAL;
266 return 0;
267}
268
269static int vidioc_queryctrl(struct file *file, void *priv,
270 struct v4l2_queryctrl *qc)
271{
272 int i;
273
274 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
275 if (qc->id && qc->id == radio_qctrl[i].id) {
276 memcpy(qc, &(radio_qctrl[i]),
277 sizeof(*qc));
278 return 0;
279 }
280 }
281 return -EINVAL;
282}
283
284static int vidioc_g_ctrl(struct file *file, void *priv,
285 struct v4l2_control *ctrl)
286{
287 struct snd_tea575x *tea = video_drvdata(file);
288
289 switch (ctrl->id) {
290 case V4L2_CID_AUDIO_MUTE:
Ondrej Zary14219d02011-05-09 23:39:26 +0200291 ctrl->value = tea->mute;
292 return 0;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300293 }
294 return -EINVAL;
295}
296
297static int vidioc_s_ctrl(struct file *file, void *priv,
298 struct v4l2_control *ctrl)
299{
300 struct snd_tea575x *tea = video_drvdata(file);
301
302 switch (ctrl->id) {
303 case V4L2_CID_AUDIO_MUTE:
Ondrej Zary14219d02011-05-09 23:39:26 +0200304 if (tea->mute != ctrl->value) {
Ondrej Zary1233faa2009-11-27 18:19:28 +0100305 tea->mute = ctrl->value;
Ondrej Zary14219d02011-05-09 23:39:26 +0200306 snd_tea575x_set_freq(tea);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300307 }
Ondrej Zary14219d02011-05-09 23:39:26 +0200308 return 0;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300309 }
310 return -EINVAL;
311}
312
313static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
314{
315 *i = 0;
316 return 0;
317}
318
319static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
320{
321 if (i != 0)
322 return -EINVAL;
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Hans Verkuilbec43662008-12-30 06:58:20 -0300326static int snd_tea575x_exclusive_open(struct file *file)
Hans Verkuil2f3d0022008-08-23 04:52:00 -0300327{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300328 struct snd_tea575x *tea = video_drvdata(file);
Hans Verkuil2f3d0022008-08-23 04:52:00 -0300329
330 return test_and_set_bit(0, &tea->in_use) ? -EBUSY : 0;
331}
332
Hans Verkuilbec43662008-12-30 06:58:20 -0300333static int snd_tea575x_exclusive_release(struct file *file)
Hans Verkuil2f3d0022008-08-23 04:52:00 -0300334{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300335 struct snd_tea575x *tea = video_drvdata(file);
Hans Verkuil2f3d0022008-08-23 04:52:00 -0300336
337 clear_bit(0, &tea->in_use);
338 return 0;
339}
340
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300341static const struct v4l2_file_operations tea575x_fops = {
342 .owner = THIS_MODULE,
343 .open = snd_tea575x_exclusive_open,
344 .release = snd_tea575x_exclusive_release,
345 .ioctl = video_ioctl2,
346};
347
348static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
349 .vidioc_querycap = vidioc_querycap,
350 .vidioc_g_tuner = vidioc_g_tuner,
351 .vidioc_s_tuner = vidioc_s_tuner,
352 .vidioc_g_audio = vidioc_g_audio,
353 .vidioc_s_audio = vidioc_s_audio,
354 .vidioc_g_input = vidioc_g_input,
355 .vidioc_s_input = vidioc_s_input,
356 .vidioc_g_frequency = vidioc_g_frequency,
357 .vidioc_s_frequency = vidioc_s_frequency,
358 .vidioc_queryctrl = vidioc_queryctrl,
359 .vidioc_g_ctrl = vidioc_g_ctrl,
360 .vidioc_s_ctrl = vidioc_s_ctrl,
361};
362
363static struct video_device tea575x_radio = {
364 .name = "tea575x-tuner",
365 .fops = &tea575x_fops,
366 .ioctl_ops = &tea575x_ioctl_ops,
367 .release = video_device_release,
368};
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/*
371 * initialize all the tea575x chips
372 */
Ondrej Zary14219d02011-05-09 23:39:26 +0200373int snd_tea575x_init(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300375 int retval;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300376 struct video_device *tea575x_radio_inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Ondrej Zary14219d02011-05-09 23:39:26 +0200378 tea->mute = 1;
379
380 snd_tea575x_write(tea, 0x55AA);
381 if (snd_tea575x_read(tea) != 0x55AA)
382 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Hans Verkuil2f3d0022008-08-23 04:52:00 -0300384 tea->in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
386 tea->freq = 90500 * 16; /* 90.5Mhz default */
387
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300388 tea575x_radio_inst = video_device_alloc();
389 if (tea575x_radio_inst == NULL) {
390 printk(KERN_ERR "tea575x-tuner: not enough memory\n");
Ondrej Zary14219d02011-05-09 23:39:26 +0200391 return -ENOMEM;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300392 }
393
394 memcpy(tea575x_radio_inst, &tea575x_radio, sizeof(tea575x_radio));
395
396 strcpy(tea575x_radio.name, tea->tea5759 ?
397 "TEA5759 radio" : "TEA5757 radio");
398
399 video_set_drvdata(tea575x_radio_inst, tea);
400
401 retval = video_register_device(tea575x_radio_inst,
402 VFL_TYPE_RADIO, radio_nr);
403 if (retval) {
404 printk(KERN_ERR "tea575x-tuner: can't register video device!\n");
405 kfree(tea575x_radio_inst);
Ondrej Zary14219d02011-05-09 23:39:26 +0200406 return retval;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300407 }
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 snd_tea575x_set_freq(tea);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300410 tea->vd = tea575x_radio_inst;
Ondrej Zary14219d02011-05-09 23:39:26 +0200411
412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Takashi Iwai97f02e02005-11-17 14:17:19 +0100415void snd_tea575x_exit(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300417 if (tea->vd) {
418 video_unregister_device(tea->vd);
419 tea->vd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421}
422
423static int __init alsa_tea575x_module_init(void)
424{
425 return 0;
426}
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void __exit alsa_tea575x_module_exit(void)
429{
430}
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432module_init(alsa_tea575x_module_init)
433module_exit(alsa_tea575x_module_exit)
434
435EXPORT_SYMBOL(snd_tea575x_init);
436EXPORT_SYMBOL(snd_tea575x_exit);