blob: b291bd86c4ca633c3487b4faf0c8440e71d96efc [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>
Hans Verkuild4ecc832012-02-27 05:30:13 -030028#include <linux/sched.h>
29#include <media/v4l2-device.h>
Ondrej Zary4522e822011-05-23 09:17:19 -030030#include <media/v4l2-dev.h>
Hans Verkuild4ecc832012-02-27 05:30:13 -030031#include <media/v4l2-fh.h>
Ondrej Zary4522e822011-05-23 09:17:19 -030032#include <media/v4l2-ioctl.h>
Hans Verkuild4ecc832012-02-27 05:30:13 -030033#include <media/v4l2-event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <sound/tea575x-tuner.h>
35
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020036MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
38MODULE_LICENSE("GPL");
39
Hans Verkuild4ecc832012-02-27 05:30:13 -030040#define FREQ_LO (76U * 16000)
41#define FREQ_HI (108U * 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
Takashi Iwai97f02e02005-11-17 14:17:19 +0100123static void snd_tea575x_set_freq(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Hans Verkuild4ecc832012-02-27 05:30:13 -0300125 u32 freq = tea->freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Ondrej Zary375d1352011-03-19 16:32:53 +0100127 freq /= 16; /* to kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* crystal fixup */
129 if (tea->tea5759)
Ondrej Zaryea273162011-05-12 22:17:56 +0200130 freq -= TEA575X_FMIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 else
Ondrej Zaryea273162011-05-12 22:17:56 +0200132 freq += TEA575X_FMIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* freq /= 12.5 */
134 freq *= 10;
135 freq /= 125;
136
137 tea->val &= ~TEA575X_BIT_FREQ_MASK;
138 tea->val |= freq & TEA575X_BIT_FREQ_MASK;
Ondrej Zary14219d02011-05-09 23:39:26 +0200139 snd_tea575x_write(tea, tea->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142/*
143 * Linux Video interface
144 */
145
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300146static int vidioc_querycap(struct file *file, void *priv,
147 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300149 struct snd_tea575x *tea = video_drvdata(file);
Igor M. Liplianin4b296312008-11-09 15:25:31 -0300150
Hans Verkuild4ecc832012-02-27 05:30:13 -0300151 strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
Ondrej Zary10ca7202011-05-12 22:18:22 +0200152 strlcpy(v->card, tea->card, sizeof(v->card));
153 strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
154 strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
Hans Verkuild4ecc832012-02-27 05:30:13 -0300155 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
156 if (!tea->cannot_read_data)
157 v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
158 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300162static int vidioc_g_tuner(struct file *file, void *priv,
163 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Ondrej Zary375d1352011-03-19 16:32:53 +0100165 struct snd_tea575x *tea = video_drvdata(file);
166
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300167 if (v->index > 0)
168 return -EINVAL;
169
Ondrej Zary14219d02011-05-09 23:39:26 +0200170 snd_tea575x_read(tea);
Ondrej Zary375d1352011-03-19 16:32:53 +0100171
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300172 strcpy(v->name, "FM");
173 v->type = V4L2_TUNER_RADIO;
Ondrej Zary375d1352011-03-19 16:32:53 +0100174 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300175 v->rangelow = FREQ_LO;
176 v->rangehigh = FREQ_HI;
Hans Verkuild4ecc832012-02-27 05:30:13 -0300177 v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
178 v->audmode = (tea->val & TEA575X_BIT_MONO) ?
179 V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
Ondrej Zary375d1352011-03-19 16:32:53 +0100180 v->signal = tea->tuned ? 0xffff : 0;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300181 return 0;
182}
183
184static int vidioc_s_tuner(struct file *file, void *priv,
185 struct v4l2_tuner *v)
186{
Hans Verkuild4ecc832012-02-27 05:30:13 -0300187 struct snd_tea575x *tea = video_drvdata(file);
188
189 if (v->index)
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300190 return -EINVAL;
Hans Verkuild4ecc832012-02-27 05:30:13 -0300191 tea->val &= ~TEA575X_BIT_MONO;
192 if (v->audmode == V4L2_TUNER_MODE_MONO)
193 tea->val |= TEA575X_BIT_MONO;
194 snd_tea575x_write(tea, tea->val);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300195 return 0;
196}
197
198static int vidioc_g_frequency(struct file *file, void *priv,
199 struct v4l2_frequency *f)
200{
201 struct snd_tea575x *tea = video_drvdata(file);
202
Ondrej Zary375d1352011-03-19 16:32:53 +0100203 if (f->tuner != 0)
204 return -EINVAL;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300205 f->type = V4L2_TUNER_RADIO;
206 f->frequency = tea->freq;
207 return 0;
208}
209
210static int vidioc_s_frequency(struct file *file, void *priv,
211 struct v4l2_frequency *f)
212{
213 struct snd_tea575x *tea = video_drvdata(file);
214
Ondrej Zary375d1352011-03-19 16:32:53 +0100215 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
216 return -EINVAL;
217
Hans Verkuild4ecc832012-02-27 05:30:13 -0300218 tea->val &= ~TEA575X_BIT_SEARCH;
219 tea->freq = clamp(f->frequency, FREQ_LO, FREQ_HI);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300220 snd_tea575x_set_freq(tea);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300221 return 0;
222}
223
Hans Verkuild4ecc832012-02-27 05:30:13 -0300224static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
225 struct v4l2_hw_freq_seek *a)
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300226{
Hans Verkuild4ecc832012-02-27 05:30:13 -0300227 struct snd_tea575x *tea = video_drvdata(file);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300228
Hans Verkuild4ecc832012-02-27 05:30:13 -0300229 if (tea->cannot_read_data)
230 return -ENOTTY;
231 if (a->tuner || a->wrap_around)
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300232 return -EINVAL;
Hans Verkuild4ecc832012-02-27 05:30:13 -0300233 tea->val |= TEA575X_BIT_SEARCH;
234 tea->val &= ~TEA575X_BIT_UPDOWN;
235 if (a->seek_upward)
236 tea->val |= TEA575X_BIT_UPDOWN;
237 snd_tea575x_write(tea, tea->val);
238 for (;;) {
239 unsigned val = snd_tea575x_read(tea);
240
241 if (!(val & TEA575X_BIT_SEARCH)) {
242 /* Found a frequency */
243 val &= TEA575X_BIT_FREQ_MASK;
244 val = (val * 10) / 125;
245 if (tea->tea5759)
246 val += TEA575X_FMIF;
247 else
248 val -= TEA575X_FMIF;
249 tea->freq = clamp(val * 16, FREQ_LO, FREQ_HI);
250 return 0;
251 }
252 if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
253 /* some signal arrived, stop search */
254 tea->val &= ~TEA575X_BIT_SEARCH;
255 snd_tea575x_write(tea, tea->val);
256 return -ERESTARTSYS;
257 }
258 }
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300259 return 0;
260}
261
Ondrej Zary4522e822011-05-23 09:17:19 -0300262static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300263{
Ondrej Zary4522e822011-05-23 09:17:19 -0300264 struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300265
266 switch (ctrl->id) {
267 case V4L2_CID_AUDIO_MUTE:
Ondrej Zary4522e822011-05-23 09:17:19 -0300268 tea->mute = ctrl->val;
269 snd_tea575x_set_freq(tea);
Ondrej Zary14219d02011-05-09 23:39:26 +0200270 return 0;
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300271 }
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300272
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300273 return -EINVAL;
274}
275
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300276static const struct v4l2_file_operations tea575x_fops = {
277 .owner = THIS_MODULE,
Ondrej Zary6a529c12011-06-11 10:28:59 -0300278 .unlocked_ioctl = video_ioctl2,
Hans Verkuild4ecc832012-02-27 05:30:13 -0300279 .open = v4l2_fh_open,
280 .release = v4l2_fh_release,
281 .poll = v4l2_ctrl_poll,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300282};
283
284static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
285 .vidioc_querycap = vidioc_querycap,
286 .vidioc_g_tuner = vidioc_g_tuner,
287 .vidioc_s_tuner = vidioc_s_tuner,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300288 .vidioc_g_frequency = vidioc_g_frequency,
289 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuild4ecc832012-02-27 05:30:13 -0300290 .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
291 .vidioc_log_status = v4l2_ctrl_log_status,
292 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
293 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300294};
295
Hans Verkuild4ecc832012-02-27 05:30:13 -0300296static const struct video_device tea575x_radio = {
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300297 .fops = &tea575x_fops,
298 .ioctl_ops = &tea575x_ioctl_ops,
Hans Verkuild4ecc832012-02-27 05:30:13 -0300299 .release = video_device_release_empty,
Ondrej Zary4522e822011-05-23 09:17:19 -0300300};
301
302static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
303 .s_ctrl = tea575x_s_ctrl,
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * initialize all the tea575x chips
308 */
Ondrej Zary14219d02011-05-09 23:39:26 +0200309int snd_tea575x_init(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300311 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Hans Verkuild4ecc832012-02-27 05:30:13 -0300313 tea->mute = true;
Ondrej Zary14219d02011-05-09 23:39:26 +0200314
Hans Verkuild4ecc832012-02-27 05:30:13 -0300315 /* Not all devices can or know how to read the data back.
316 Such devices can set cannot_read_data to true. */
317 if (!tea->cannot_read_data) {
318 snd_tea575x_write(tea, 0x55AA);
319 if (snd_tea575x_read(tea) != 0x55AA)
320 return -ENODEV;
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
324 tea->freq = 90500 * 16; /* 90.5Mhz default */
Ondrej Zary4522e822011-05-23 09:17:19 -0300325 snd_tea575x_set_freq(tea);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Ondrej Zary4522e822011-05-23 09:17:19 -0300327 tea->vd = tea575x_radio;
328 video_set_drvdata(&tea->vd, tea);
Ondrej Zary6a529c12011-06-11 10:28:59 -0300329 mutex_init(&tea->mutex);
Hans Verkuild4ecc832012-02-27 05:30:13 -0300330 strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
Ondrej Zary6a529c12011-06-11 10:28:59 -0300331 tea->vd.lock = &tea->mutex;
Hans Verkuild4ecc832012-02-27 05:30:13 -0300332 tea->vd.v4l2_dev = tea->v4l2_dev;
333 tea->vd.ctrl_handler = &tea->ctrl_handler;
334 set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
Mauro Carvalho Chehab9b76ede2009-02-27 11:51:24 -0300335
Ondrej Zary4522e822011-05-23 09:17:19 -0300336 v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
Ondrej Zary4522e822011-05-23 09:17:19 -0300337 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) {
Hans Verkuild4ecc832012-02-27 05:30:13 -0300340 v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
Ondrej Zary4522e822011-05-23 09:17:19 -0300341 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
Hans Verkuild4ecc832012-02-27 05:30:13 -0300355 retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
Ondrej Zary4522e822011-05-23 09:17:19 -0300356 if (retval) {
Hans Verkuild4ecc832012-02-27 05:30:13 -0300357 v4l2_err(tea->v4l2_dev, "can't register video device!\n");
Ondrej Zary4522e822011-05-23 09:17:19 -0300358 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);