blob: beb0e86537cd106e086c26a9f7ef9e03f271a616 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -030027#include <linux/version.h>
Ondrej Zary4522e822011-05-23 09:17:19 -030028#include <media/v4l2-dev.h>
29#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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
65/*
66 * lowlevel part
67 */
68
Ondrej Zary14219d02011-05-09 23:39:26 +020069static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
70{
71 u16 l;
72 u8 data;
73
74 tea->ops->set_direction(tea, 1);
75 udelay(16);
76
77 for (l = 25; l > 0; l--) {
78 data = (val >> 24) & TEA575X_DATA;
79 val <<= 1; /* shift data */
80 tea->ops->set_pins(tea, data | TEA575X_WREN);
81 udelay(2);
82 tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
83 udelay(2);
84 tea->ops->set_pins(tea, data | TEA575X_WREN);
85 udelay(2);
86 }
87
88 if (!tea->mute)
89 tea->ops->set_pins(tea, 0);
90}
91
92static unsigned int snd_tea575x_read(struct snd_tea575x *tea)
93{
94 u16 l, rdata;
95 u32 data = 0;
96
97 tea->ops->set_direction(tea, 0);
98 tea->ops->set_pins(tea, 0);
99 udelay(16);
100
101 for (l = 24; l--;) {
102 tea->ops->set_pins(tea, TEA575X_CLK);
103 udelay(2);
104 if (!l)
105 tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
106 tea->ops->set_pins(tea, 0);
107 udelay(2);
108 data <<= 1; /* shift data */
109 rdata = tea->ops->get_pins(tea);
110 if (!l)
111 tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
112 if (rdata & TEA575X_DATA)
113 data++;
114 udelay(2);
115 }
116
117 if (tea->mute)
118 tea->ops->set_pins(tea, TEA575X_WREN);
119
120 return data;
121}
122
Ondrej Zary375d1352011-03-19 16:32:53 +0100123static void snd_tea575x_get_freq(struct snd_tea575x *tea)
124{
125 unsigned long freq;
126
Ondrej Zary14219d02011-05-09 23:39:26 +0200127 freq = snd_tea575x_read(tea) & TEA575X_BIT_FREQ_MASK;
Ondrej Zary375d1352011-03-19 16:32:53 +0100128 /* freq *= 12.5 */
129 freq *= 125;
130 freq /= 10;
131 /* crystal fixup */
132 if (tea->tea5759)
Ondrej Zaryea273162011-05-12 22:17:56 +0200133 freq += TEA575X_FMIF;
Ondrej Zary375d1352011-03-19 16:32:53 +0100134 else
Ondrej Zaryea273162011-05-12 22:17:56 +0200135 freq -= TEA575X_FMIF;
Ondrej Zary375d1352011-03-19 16:32:53 +0100136
137 tea->freq = freq * 16; /* from kHz */
138}
139
Takashi Iwai97f02e02005-11-17 14:17:19 +0100140static void snd_tea575x_set_freq(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 unsigned long freq;
143
Ondrej Zary375d1352011-03-19 16:32:53 +0100144 freq = clamp(tea->freq, FREQ_LO, FREQ_HI);
145 freq /= 16; /* to kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* crystal fixup */
147 if (tea->tea5759)
Ondrej Zaryea273162011-05-12 22:17:56 +0200148 freq -= TEA575X_FMIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 else
Ondrej Zaryea273162011-05-12 22:17:56 +0200150 freq += TEA575X_FMIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* freq /= 12.5 */
152 freq *= 10;
153 freq /= 125;
154
155 tea->val &= ~TEA575X_BIT_FREQ_MASK;
156 tea->val |= freq & TEA575X_BIT_FREQ_MASK;
Ondrej Zary14219d02011-05-09 23:39:26 +0200157 snd_tea575x_write(tea, tea->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/*
161 * Linux Video interface
162 */
163
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300164static int vidioc_querycap(struct file *file, void *priv,
165 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300167 struct snd_tea575x *tea = video_drvdata(file);
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300168
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300169 strlcpy(v->driver, "tea575x-tuner", sizeof(v->driver));
Ondrej Zary10ca7202011-05-12 22:18:22 +0200170 strlcpy(v->card, tea->card, sizeof(v->card));
171 strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
172 strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300173 v->version = RADIO_VERSION;
Ondrej Zary375d1352011-03-19 16:32:53 +0100174 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300178static int vidioc_g_tuner(struct file *file, void *priv,
179 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Ondrej Zary375d1352011-03-19 16:32:53 +0100181 struct snd_tea575x *tea = video_drvdata(file);
182
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300183 if (v->index > 0)
184 return -EINVAL;
185
Ondrej Zary14219d02011-05-09 23:39:26 +0200186 snd_tea575x_read(tea);
Ondrej Zary375d1352011-03-19 16:32:53 +0100187
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300188 strcpy(v->name, "FM");
189 v->type = V4L2_TUNER_RADIO;
Ondrej Zary375d1352011-03-19 16:32:53 +0100190 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300191 v->rangelow = FREQ_LO;
192 v->rangehigh = FREQ_HI;
Ondrej Zary375d1352011-03-19 16:32:53 +0100193 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
194 v->audmode = tea->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
195 v->signal = tea->tuned ? 0xffff : 0;
196
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300197 return 0;
198}
199
200static int vidioc_s_tuner(struct file *file, void *priv,
201 struct v4l2_tuner *v)
202{
203 if (v->index > 0)
204 return -EINVAL;
205 return 0;
206}
207
208static int vidioc_g_frequency(struct file *file, void *priv,
209 struct v4l2_frequency *f)
210{
211 struct snd_tea575x *tea = video_drvdata(file);
212
Ondrej Zary375d1352011-03-19 16:32:53 +0100213 if (f->tuner != 0)
214 return -EINVAL;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300215 f->type = V4L2_TUNER_RADIO;
Ondrej Zary375d1352011-03-19 16:32:53 +0100216 snd_tea575x_get_freq(tea);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300217 f->frequency = tea->freq;
218 return 0;
219}
220
221static int vidioc_s_frequency(struct file *file, void *priv,
222 struct v4l2_frequency *f)
223{
224 struct snd_tea575x *tea = video_drvdata(file);
225
Ondrej Zary375d1352011-03-19 16:32:53 +0100226 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
227 return -EINVAL;
228
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300229 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
230 return -EINVAL;
231
232 tea->freq = f->frequency;
233
234 snd_tea575x_set_freq(tea);
235
236 return 0;
237}
238
239static int vidioc_g_audio(struct file *file, void *priv,
240 struct v4l2_audio *a)
241{
242 if (a->index > 1)
243 return -EINVAL;
244
245 strcpy(a->name, "Radio");
246 a->capability = V4L2_AUDCAP_STEREO;
247 return 0;
248}
249
250static int vidioc_s_audio(struct file *file, void *priv,
251 struct v4l2_audio *a)
252{
253 if (a->index != 0)
254 return -EINVAL;
255 return 0;
256}
257
Ondrej Zary4522e822011-05-23 09:17:19 -0300258static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300259{
Ondrej Zary4522e822011-05-23 09:17:19 -0300260 struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300261
262 switch (ctrl->id) {
263 case V4L2_CID_AUDIO_MUTE:
Ondrej Zary4522e822011-05-23 09:17:19 -0300264 tea->mute = ctrl->val;
265 snd_tea575x_set_freq(tea);
Ondrej Zary14219d02011-05-09 23:39:26 +0200266 return 0;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300267 }
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300268
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300269 return -EINVAL;
270}
271
272static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
273{
274 *i = 0;
275 return 0;
276}
277
278static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
279{
280 if (i != 0)
281 return -EINVAL;
282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300285static const struct v4l2_file_operations tea575x_fops = {
286 .owner = THIS_MODULE,
Ondrej Zary6a529c12011-06-11 10:28:59 -0300287 .unlocked_ioctl = video_ioctl2,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300288};
289
290static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
291 .vidioc_querycap = vidioc_querycap,
292 .vidioc_g_tuner = vidioc_g_tuner,
293 .vidioc_s_tuner = vidioc_s_tuner,
294 .vidioc_g_audio = vidioc_g_audio,
295 .vidioc_s_audio = vidioc_s_audio,
296 .vidioc_g_input = vidioc_g_input,
297 .vidioc_s_input = vidioc_s_input,
298 .vidioc_g_frequency = vidioc_g_frequency,
299 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300300};
301
302static struct video_device tea575x_radio = {
303 .name = "tea575x-tuner",
304 .fops = &tea575x_fops,
305 .ioctl_ops = &tea575x_ioctl_ops,
Ondrej Zary4522e822011-05-23 09:17:19 -0300306 .release = video_device_release_empty,
307};
308
309static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
310 .s_ctrl = tea575x_s_ctrl,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300311};
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
314 * initialize all the tea575x chips
315 */
Ondrej Zary14219d02011-05-09 23:39:26 +0200316int snd_tea575x_init(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300318 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Ondrej Zary14219d02011-05-09 23:39:26 +0200320 tea->mute = 1;
321
322 snd_tea575x_write(tea, 0x55AA);
323 if (snd_tea575x_read(tea) != 0x55AA)
324 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
327 tea->freq = 90500 * 16; /* 90.5Mhz default */
Ondrej Zary4522e822011-05-23 09:17:19 -0300328 snd_tea575x_set_freq(tea);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Ondrej Zary4522e822011-05-23 09:17:19 -0300330 tea->vd = tea575x_radio;
331 video_set_drvdata(&tea->vd, tea);
Ondrej Zary6a529c12011-06-11 10:28:59 -0300332 mutex_init(&tea->mutex);
333 tea->vd.lock = &tea->mutex;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300334
Ondrej Zary4522e822011-05-23 09:17:19 -0300335 v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
336 tea->vd.ctrl_handler = &tea->ctrl_handler;
337 v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
338 retval = tea->ctrl_handler.error;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300339 if (retval) {
Ondrej Zary4522e822011-05-23 09:17:19 -0300340 printk(KERN_ERR "tea575x-tuner: can't initialize controls\n");
341 v4l2_ctrl_handler_free(&tea->ctrl_handler);
Ondrej Zary14219d02011-05-09 23:39:26 +0200342 return retval;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300343 }
344
Ondrej Zary4522e822011-05-23 09:17:19 -0300345 if (tea->ext_init) {
346 retval = tea->ext_init(tea);
347 if (retval) {
348 v4l2_ctrl_handler_free(&tea->ctrl_handler);
349 return retval;
350 }
351 }
352
353 v4l2_ctrl_handler_setup(&tea->ctrl_handler);
354
355 retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, radio_nr);
356 if (retval) {
357 printk(KERN_ERR "tea575x-tuner: can't register video device!\n");
358 v4l2_ctrl_handler_free(&tea->ctrl_handler);
359 return retval;
360 }
Ondrej Zary14219d02011-05-09 23:39:26 +0200361
362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Takashi Iwai97f02e02005-11-17 14:17:19 +0100365void snd_tea575x_exit(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Ondrej Zary4522e822011-05-23 09:17:19 -0300367 video_unregister_device(&tea->vd);
368 v4l2_ctrl_handler_free(&tea->ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371static int __init alsa_tea575x_module_init(void)
372{
373 return 0;
374}
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static void __exit alsa_tea575x_module_exit(void)
377{
378}
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380module_init(alsa_tea575x_module_init)
381module_exit(alsa_tea575x_module_exit)
382
383EXPORT_SYMBOL(snd_tea575x_init);
384EXPORT_SYMBOL(snd_tea575x_exit);