blob: 6b68c8206805107a8b6cac329f92aa2cb25b3bb7 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040025#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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>
Ondrej Zary4522e822011-05-23 09:17:19 -030029#include <media/v4l2-dev.h>
30#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/tea575x-tuner.h>
32
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020033MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070034MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
35MODULE_LICENSE("GPL");
36
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -030037static int radio_nr = -1;
38module_param(radio_nr, int, 0);
39
40#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
Ondrej Zary375d1352011-03-19 16:32:53 +010041#define FREQ_LO (50UL * 16000)
42#define FREQ_HI (150UL * 16000)
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -030043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
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
Ondrej Zary14219d02011-05-09 23:39:26 +020070static 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
93static 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
Ondrej Zary375d1352011-03-19 16:32:53 +0100124static void snd_tea575x_get_freq(struct snd_tea575x *tea)
125{
126 unsigned long freq;
127
Ondrej Zary14219d02011-05-09 23:39:26 +0200128 freq = snd_tea575x_read(tea) & TEA575X_BIT_FREQ_MASK;
Ondrej Zary375d1352011-03-19 16:32:53 +0100129 /* freq *= 12.5 */
130 freq *= 125;
131 freq /= 10;
132 /* crystal fixup */
133 if (tea->tea5759)
Ondrej Zaryea273162011-05-12 22:17:56 +0200134 freq += TEA575X_FMIF;
Ondrej Zary375d1352011-03-19 16:32:53 +0100135 else
Ondrej Zaryea273162011-05-12 22:17:56 +0200136 freq -= TEA575X_FMIF;
Ondrej Zary375d1352011-03-19 16:32:53 +0100137
138 tea->freq = freq * 16; /* from kHz */
139}
140
Takashi Iwai97f02e02005-11-17 14:17:19 +0100141static void snd_tea575x_set_freq(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 unsigned long freq;
144
Ondrej Zary375d1352011-03-19 16:32:53 +0100145 freq = clamp(tea->freq, FREQ_LO, FREQ_HI);
146 freq /= 16; /* to kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* crystal fixup */
148 if (tea->tea5759)
Ondrej Zaryea273162011-05-12 22:17:56 +0200149 freq -= TEA575X_FMIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 else
Ondrej Zaryea273162011-05-12 22:17:56 +0200151 freq += TEA575X_FMIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* 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;
Ondrej Zary14219d02011-05-09 23:39:26 +0200158 snd_tea575x_write(tea, tea->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/*
162 * Linux Video interface
163 */
164
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300165static int vidioc_querycap(struct file *file, void *priv,
166 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300168 struct snd_tea575x *tea = video_drvdata(file);
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300169
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300170 strlcpy(v->driver, "tea575x-tuner", sizeof(v->driver));
Ondrej Zary10ca7202011-05-12 22:18:22 +0200171 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));
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300174 v->version = RADIO_VERSION;
Ondrej Zary375d1352011-03-19 16:32:53 +0100175 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300179static int vidioc_g_tuner(struct file *file, void *priv,
180 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Ondrej Zary375d1352011-03-19 16:32:53 +0100182 struct snd_tea575x *tea = video_drvdata(file);
183
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300184 if (v->index > 0)
185 return -EINVAL;
186
Ondrej Zary14219d02011-05-09 23:39:26 +0200187 snd_tea575x_read(tea);
Ondrej Zary375d1352011-03-19 16:32:53 +0100188
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300189 strcpy(v->name, "FM");
190 v->type = V4L2_TUNER_RADIO;
Ondrej Zary375d1352011-03-19 16:32:53 +0100191 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300192 v->rangelow = FREQ_LO;
193 v->rangehigh = FREQ_HI;
Ondrej Zary375d1352011-03-19 16:32:53 +0100194 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
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300198 return 0;
199}
200
201static 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
209static int vidioc_g_frequency(struct file *file, void *priv,
210 struct v4l2_frequency *f)
211{
212 struct snd_tea575x *tea = video_drvdata(file);
213
Ondrej Zary375d1352011-03-19 16:32:53 +0100214 if (f->tuner != 0)
215 return -EINVAL;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300216 f->type = V4L2_TUNER_RADIO;
Ondrej Zary375d1352011-03-19 16:32:53 +0100217 snd_tea575x_get_freq(tea);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300218 f->frequency = tea->freq;
219 return 0;
220}
221
222static int vidioc_s_frequency(struct file *file, void *priv,
223 struct v4l2_frequency *f)
224{
225 struct snd_tea575x *tea = video_drvdata(file);
226
Ondrej Zary375d1352011-03-19 16:32:53 +0100227 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
228 return -EINVAL;
229
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300230 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
240static 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
251static 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
Ondrej Zary4522e822011-05-23 09:17:19 -0300259static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300260{
Ondrej Zary4522e822011-05-23 09:17:19 -0300261 struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300262
263 switch (ctrl->id) {
264 case V4L2_CID_AUDIO_MUTE:
Ondrej Zary4522e822011-05-23 09:17:19 -0300265 tea->mute = ctrl->val;
266 snd_tea575x_set_freq(tea);
Ondrej Zary14219d02011-05-09 23:39:26 +0200267 return 0;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300268 }
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300269
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300270 return -EINVAL;
271}
272
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300273static const struct v4l2_file_operations tea575x_fops = {
274 .owner = THIS_MODULE,
Ondrej Zary6a529c12011-06-11 10:28:59 -0300275 .unlocked_ioctl = video_ioctl2,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300276};
277
278static 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,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300284 .vidioc_g_frequency = vidioc_g_frequency,
285 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300286};
287
288static struct video_device tea575x_radio = {
289 .name = "tea575x-tuner",
290 .fops = &tea575x_fops,
291 .ioctl_ops = &tea575x_ioctl_ops,
Ondrej Zary4522e822011-05-23 09:17:19 -0300292 .release = video_device_release_empty,
293};
294
295static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
296 .s_ctrl = tea575x_s_ctrl,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300297};
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * initialize all the tea575x chips
301 */
Ondrej Zary14219d02011-05-09 23:39:26 +0200302int snd_tea575x_init(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300304 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Ondrej Zary14219d02011-05-09 23:39:26 +0200306 tea->mute = 1;
307
308 snd_tea575x_write(tea, 0x55AA);
309 if (snd_tea575x_read(tea) != 0x55AA)
310 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
313 tea->freq = 90500 * 16; /* 90.5Mhz default */
Ondrej Zary4522e822011-05-23 09:17:19 -0300314 snd_tea575x_set_freq(tea);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Ondrej Zary4522e822011-05-23 09:17:19 -0300316 tea->vd = tea575x_radio;
317 video_set_drvdata(&tea->vd, tea);
Ondrej Zary6a529c12011-06-11 10:28:59 -0300318 mutex_init(&tea->mutex);
319 tea->vd.lock = &tea->mutex;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300320
Ondrej Zary4522e822011-05-23 09:17:19 -0300321 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;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300325 if (retval) {
Ondrej Zary4522e822011-05-23 09:17:19 -0300326 printk(KERN_ERR "tea575x-tuner: can't initialize controls\n");
327 v4l2_ctrl_handler_free(&tea->ctrl_handler);
Ondrej Zary14219d02011-05-09 23:39:26 +0200328 return retval;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300329 }
330
Ondrej Zary4522e822011-05-23 09:17:19 -0300331 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 }
Ondrej Zary14219d02011-05-09 23:39:26 +0200347
348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Takashi Iwai97f02e02005-11-17 14:17:19 +0100351void snd_tea575x_exit(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Ondrej Zary4522e822011-05-23 09:17:19 -0300353 video_unregister_device(&tea->vd);
354 v4l2_ctrl_handler_free(&tea->ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static int __init alsa_tea575x_module_init(void)
358{
359 return 0;
360}
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static void __exit alsa_tea575x_module_exit(void)
363{
364}
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366module_init(alsa_tea575x_module_init)
367module_exit(alsa_tea575x_module_exit)
368
369EXPORT_SYMBOL(snd_tea575x_init);
370EXPORT_SYMBOL(snd_tea575x_exit);