blob: 5bc9fa62720b6e23355a2ae54447a95de04d9222 [file] [log] [blame]
Hans de Goede4faba762012-06-23 04:39:58 -03001/*
2 * v4l2 driver for TEA5777 Philips AM/FM radio tuner chips
3 *
4 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
5 *
6 * Based on the ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips:
7 *
8 * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26#include <linux/delay.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/sched.h>
30#include <linux/slab.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-dev.h>
33#include <media/v4l2-fh.h>
34#include <media/v4l2-ioctl.h>
35#include <media/v4l2-event.h>
Mauro Carvalho Chehabadfe1562012-07-31 16:38:41 -030036#include <asm/div64.h>
Hans de Goede4faba762012-06-23 04:39:58 -030037#include "radio-tea5777.h"
38
39MODULE_AUTHOR("Hans de Goede <perex@perex.cz>");
40MODULE_DESCRIPTION("Routines for control of TEA5777 Philips AM/FM radio tuner chips");
41MODULE_LICENSE("GPL");
42
43/* Fixed FM only band for now, will implement multi-band support when the
44 VIDIOC_ENUM_FREQ_BANDS API is upstream */
45#define TEA5777_FM_RANGELOW (76000 * 16)
46#define TEA5777_FM_RANGEHIGH (108000 * 16)
47
48#define TEA5777_FM_IF 150 /* kHz */
49#define TEA5777_FM_FREQ_STEP 50 /* kHz */
50
51/* Write reg, common bits */
52#define TEA5777_W_MUTE_MASK (1LL << 47)
53#define TEA5777_W_MUTE_SHIFT 47
54#define TEA5777_W_AM_FM_MASK (1LL << 46)
55#define TEA5777_W_AM_FM_SHIFT 46
56#define TEA5777_W_STB_MASK (1LL << 45)
57#define TEA5777_W_STB_SHIFT 45
58
59#define TEA5777_W_IFCE_MASK (1LL << 29)
60#define TEA5777_W_IFCE_SHIFT 29
61#define TEA5777_W_IFW_MASK (1LL << 28)
62#define TEA5777_W_IFW_SHIFT 28
63#define TEA5777_W_HILO_MASK (1LL << 27)
64#define TEA5777_W_HILO_SHIFT 27
65#define TEA5777_W_DBUS_MASK (1LL << 26)
66#define TEA5777_W_DBUS_SHIFT 26
67
68#define TEA5777_W_INTEXT_MASK (1LL << 24)
69#define TEA5777_W_INTEXT_SHIFT 24
70#define TEA5777_W_P1_MASK (1LL << 23)
71#define TEA5777_W_P1_SHIFT 23
72#define TEA5777_W_P0_MASK (1LL << 22)
73#define TEA5777_W_P0_SHIFT 22
74#define TEA5777_W_PEN1_MASK (1LL << 21)
75#define TEA5777_W_PEN1_SHIFT 21
76#define TEA5777_W_PEN0_MASK (1LL << 20)
77#define TEA5777_W_PEN0_SHIFT 20
78
79#define TEA5777_W_CHP0_MASK (1LL << 18)
80#define TEA5777_W_CHP0_SHIFT 18
81#define TEA5777_W_DEEM_MASK (1LL << 17)
82#define TEA5777_W_DEEM_SHIFT 17
83
84#define TEA5777_W_SEARCH_MASK (1LL << 7)
85#define TEA5777_W_SEARCH_SHIFT 7
86#define TEA5777_W_PROGBLIM_MASK (1LL << 6)
87#define TEA5777_W_PROGBLIM_SHIFT 6
88#define TEA5777_W_UPDWN_MASK (1LL << 5)
89#define TEA5777_W_UPDWN_SHIFT 5
90#define TEA5777_W_SLEV_MASK (3LL << 3)
91#define TEA5777_W_SLEV_SHIFT 3
92
93/* Write reg, FM specific bits */
94#define TEA5777_W_FM_PLL_MASK (0x1fffLL << 32)
95#define TEA5777_W_FM_PLL_SHIFT 32
96#define TEA5777_W_FM_FREF_MASK (0x03LL << 30)
97#define TEA5777_W_FM_FREF_SHIFT 30
98#define TEA5777_W_FM_FREF_VALUE 0 /* 50 kHz tune steps, 150 kHz IF */
99
100#define TEA5777_W_FM_FORCEMONO_MASK (1LL << 15)
101#define TEA5777_W_FM_FORCEMONO_SHIFT 15
102#define TEA5777_W_FM_SDSOFF_MASK (1LL << 14)
103#define TEA5777_W_FM_SDSOFF_SHIFT 14
104#define TEA5777_W_FM_DOFF_MASK (1LL << 13)
105#define TEA5777_W_FM_DOFF_SHIFT 13
106
107#define TEA5777_W_FM_STEP_MASK (3LL << 1)
108#define TEA5777_W_FM_STEP_SHIFT 1
109
110/* Write reg, AM specific bits */
111#define TEA5777_W_AM_PLL_MASK (0x7ffLL << 34)
112#define TEA5777_W_AM_PLL_SHIFT 34
113#define TEA5777_W_AM_AGCRF_MASK (1LL << 33)
114#define TEA5777_W_AM_AGCRF_SHIFT 33
115#define TEA5777_W_AM_AGCIF_MASK (1LL << 32)
116#define TEA5777_W_AM_AGCIF_SHIFT 32
117#define TEA5777_W_AM_MWLW_MASK (1LL << 31)
118#define TEA5777_W_AM_MWLW_SHIFT 31
119#define TEA5777_W_AM_LNA_MASK (1LL << 30)
120#define TEA5777_W_AM_LNA_SHIFT 30
121
122#define TEA5777_W_AM_PEAK_MASK (1LL << 25)
123#define TEA5777_W_AM_PEAK_SHIFT 25
124
125#define TEA5777_W_AM_RFB_MASK (1LL << 16)
126#define TEA5777_W_AM_RFB_SHIFT 16
127#define TEA5777_W_AM_CALLIGN_MASK (1LL << 15)
128#define TEA5777_W_AM_CALLIGN_SHIFT 15
129#define TEA5777_W_AM_CBANK_MASK (0x7fLL << 8)
130#define TEA5777_W_AM_CBANK_SHIFT 8
131
132#define TEA5777_W_AM_DELAY_MASK (1LL << 2)
133#define TEA5777_W_AM_DELAY_SHIFT 2
134#define TEA5777_W_AM_STEP_MASK (1LL << 1)
135#define TEA5777_W_AM_STEP_SHIFT 1
136
137/* Read reg, common bits */
138#define TEA5777_R_LEVEL_MASK (0x0f << 17)
139#define TEA5777_R_LEVEL_SHIFT 17
140#define TEA5777_R_SFOUND_MASK (0x01 << 16)
141#define TEA5777_R_SFOUND_SHIFT 16
142#define TEA5777_R_BLIM_MASK (0x01 << 15)
143#define TEA5777_R_BLIM_SHIFT 15
144
145/* Read reg, FM specific bits */
146#define TEA5777_R_FM_STEREO_MASK (0x01 << 21)
147#define TEA5777_R_FM_STEREO_SHIFT 21
148#define TEA5777_R_FM_PLL_MASK 0x1fff
149#define TEA5777_R_FM_PLL_SHIFT 0
150
151static u32 tea5777_freq_to_v4l2_freq(struct radio_tea5777 *tea, u32 freq)
152{
153 return (freq * TEA5777_FM_FREQ_STEP + TEA5777_FM_IF) * 16;
154}
155
156static int radio_tea5777_set_freq(struct radio_tea5777 *tea)
157{
158 u64 freq;
159 int res;
160
161 freq = clamp_t(u32, tea->freq,
Mauro Carvalho Chehabadfe1562012-07-31 16:38:41 -0300162 TEA5777_FM_RANGELOW, TEA5777_FM_RANGEHIGH) + 8;
163 do_div(freq, 16); /* to kHz */
Hans de Goede4faba762012-06-23 04:39:58 -0300164
Mauro Carvalho Chehabadfe1562012-07-31 16:38:41 -0300165 freq -= TEA5777_FM_IF;
166 do_div(freq, TEA5777_FM_FREQ_STEP);
Hans de Goede4faba762012-06-23 04:39:58 -0300167
168 tea->write_reg &= ~(TEA5777_W_FM_PLL_MASK | TEA5777_W_FM_FREF_MASK);
169 tea->write_reg |= freq << TEA5777_W_FM_PLL_SHIFT;
170 tea->write_reg |= TEA5777_W_FM_FREF_VALUE << TEA5777_W_FM_FREF_SHIFT;
171
172 res = tea->ops->write_reg(tea, tea->write_reg);
173 if (res)
174 return res;
175
176 tea->needs_write = false;
177 tea->read_reg = -1;
178 tea->freq = tea5777_freq_to_v4l2_freq(tea, freq);
179
180 return 0;
181}
182
183static int radio_tea5777_update_read_reg(struct radio_tea5777 *tea, int wait)
184{
185 int res;
186
187 if (tea->read_reg != -1)
188 return 0;
189
190 if (tea->write_before_read && tea->needs_write) {
191 res = radio_tea5777_set_freq(tea);
192 if (res)
193 return res;
194 }
195
196 if (wait) {
197 if (schedule_timeout_interruptible(msecs_to_jiffies(wait)))
198 return -ERESTARTSYS;
199 }
200
201 res = tea->ops->read_reg(tea, &tea->read_reg);
202 if (res)
203 return res;
204
205 tea->needs_write = true;
206 return 0;
207}
208
209/*
210 * Linux Video interface
211 */
212
213static int vidioc_querycap(struct file *file, void *priv,
214 struct v4l2_capability *v)
215{
216 struct radio_tea5777 *tea = video_drvdata(file);
217
218 strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
219 strlcpy(v->card, tea->card, sizeof(v->card));
220 strlcat(v->card, " TEA5777", sizeof(v->card));
221 strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
222 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
223 v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
224 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
225 return 0;
226}
227
228static int vidioc_g_tuner(struct file *file, void *priv,
229 struct v4l2_tuner *v)
230{
231 struct radio_tea5777 *tea = video_drvdata(file);
232 int res;
233
234 if (v->index > 0)
235 return -EINVAL;
236
237 res = radio_tea5777_update_read_reg(tea, 0);
238 if (res)
239 return res;
240
241 memset(v, 0, sizeof(*v));
242 if (tea->has_am)
243 strlcpy(v->name, "AM/FM", sizeof(v->name));
244 else
245 strlcpy(v->name, "FM", sizeof(v->name));
246 v->type = V4L2_TUNER_RADIO;
247 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
248 V4L2_TUNER_CAP_HWSEEK_BOUNDED;
249 v->rangelow = TEA5777_FM_RANGELOW;
250 v->rangehigh = TEA5777_FM_RANGEHIGH;
251 v->rxsubchans = (tea->read_reg & TEA5777_R_FM_STEREO_MASK) ?
252 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
253 v->audmode = (tea->write_reg & TEA5777_W_FM_FORCEMONO_MASK) ?
254 V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
255 /* shift - 12 to convert 4-bits (0-15) scale to 16-bits (0-65535) */
256 v->signal = (tea->read_reg & TEA5777_R_LEVEL_MASK) >>
257 (TEA5777_R_LEVEL_SHIFT - 12);
258
259 /* Invalidate read_reg, so that next call we return up2date signal */
260 tea->read_reg = -1;
261
262 return 0;
263}
264
265static int vidioc_s_tuner(struct file *file, void *priv,
266 struct v4l2_tuner *v)
267{
268 struct radio_tea5777 *tea = video_drvdata(file);
269
270 if (v->index)
271 return -EINVAL;
272
273 if (v->audmode == V4L2_TUNER_MODE_MONO)
274 tea->write_reg |= TEA5777_W_FM_FORCEMONO_MASK;
275 else
276 tea->write_reg &= ~TEA5777_W_FM_FORCEMONO_MASK;
277
278 return radio_tea5777_set_freq(tea);
279}
280
281static int vidioc_g_frequency(struct file *file, void *priv,
282 struct v4l2_frequency *f)
283{
284 struct radio_tea5777 *tea = video_drvdata(file);
285
286 if (f->tuner != 0)
287 return -EINVAL;
288 f->type = V4L2_TUNER_RADIO;
289 f->frequency = tea->freq;
290 return 0;
291}
292
293static int vidioc_s_frequency(struct file *file, void *priv,
294 struct v4l2_frequency *f)
295{
296 struct radio_tea5777 *tea = video_drvdata(file);
297
298 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
299 return -EINVAL;
300
301 tea->freq = f->frequency;
302 return radio_tea5777_set_freq(tea);
303}
304
305static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
306 struct v4l2_hw_freq_seek *a)
307{
308 struct radio_tea5777 *tea = video_drvdata(file);
309 u32 orig_freq = tea->freq;
310 unsigned long timeout;
311 int res, spacing = 200 * 16; /* 200 kHz */
312 /* These are fixed *for now* */
313 const u32 seek_rangelow = TEA5777_FM_RANGELOW;
314 const u32 seek_rangehigh = TEA5777_FM_RANGEHIGH;
315
316 if (a->tuner || a->wrap_around)
317 return -EINVAL;
318
319 tea->write_reg |= TEA5777_W_PROGBLIM_MASK;
320 if (seek_rangelow != tea->seek_rangelow) {
321 tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
322 tea->freq = seek_rangelow;
323 res = radio_tea5777_set_freq(tea);
324 if (res)
325 goto leave;
326 tea->seek_rangelow = tea->freq;
327 }
328 if (seek_rangehigh != tea->seek_rangehigh) {
329 tea->write_reg |= TEA5777_W_UPDWN_MASK;
330 tea->freq = seek_rangehigh;
331 res = radio_tea5777_set_freq(tea);
332 if (res)
333 goto leave;
334 tea->seek_rangehigh = tea->freq;
335 }
336 tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
337
338 tea->write_reg |= TEA5777_W_SEARCH_MASK;
339 if (a->seek_upward) {
340 tea->write_reg |= TEA5777_W_UPDWN_MASK;
341 tea->freq = orig_freq + spacing;
342 } else {
343 tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
344 tea->freq = orig_freq - spacing;
345 }
346 res = radio_tea5777_set_freq(tea);
347 if (res)
348 goto leave;
349
350 timeout = jiffies + msecs_to_jiffies(5000);
351 for (;;) {
352 if (time_after(jiffies, timeout)) {
353 res = -ENODATA;
354 break;
355 }
356
357 res = radio_tea5777_update_read_reg(tea, 100);
358 if (res)
359 break;
360
361 /*
362 * Note we use tea->freq to track how far we've searched sofar
363 * this is necessary to ensure we continue seeking at the right
364 * point, in the write_before_read case.
365 */
366 tea->freq = (tea->read_reg & TEA5777_R_FM_PLL_MASK);
367 tea->freq = tea5777_freq_to_v4l2_freq(tea, tea->freq);
368
369 if ((tea->read_reg & TEA5777_R_SFOUND_MASK)) {
370 tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
371 return 0;
372 }
373
374 if (tea->read_reg & TEA5777_R_BLIM_MASK) {
375 res = -ENODATA;
376 break;
377 }
378
379 /* Force read_reg update */
380 tea->read_reg = -1;
381 }
382leave:
383 tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
384 tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
385 tea->freq = orig_freq;
386 radio_tea5777_set_freq(tea);
387 return res;
388}
389
390static int tea575x_s_ctrl(struct v4l2_ctrl *c)
391{
392 struct radio_tea5777 *tea =
393 container_of(c->handler, struct radio_tea5777, ctrl_handler);
394
395 switch (c->id) {
396 case V4L2_CID_AUDIO_MUTE:
397 if (c->val)
398 tea->write_reg |= TEA5777_W_MUTE_MASK;
399 else
400 tea->write_reg &= ~TEA5777_W_MUTE_MASK;
401
402 return radio_tea5777_set_freq(tea);
403 }
404
405 return -EINVAL;
406}
407
408static const struct v4l2_file_operations tea575x_fops = {
409 .unlocked_ioctl = video_ioctl2,
410 .open = v4l2_fh_open,
411 .release = v4l2_fh_release,
412 .poll = v4l2_ctrl_poll,
413};
414
415static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
416 .vidioc_querycap = vidioc_querycap,
417 .vidioc_g_tuner = vidioc_g_tuner,
418 .vidioc_s_tuner = vidioc_s_tuner,
419 .vidioc_g_frequency = vidioc_g_frequency,
420 .vidioc_s_frequency = vidioc_s_frequency,
421 .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
422 .vidioc_log_status = v4l2_ctrl_log_status,
423 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
424 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
425};
426
427static const struct video_device tea575x_radio = {
428 .ioctl_ops = &tea575x_ioctl_ops,
429 .release = video_device_release_empty,
430};
431
432static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
433 .s_ctrl = tea575x_s_ctrl,
434};
435
436int radio_tea5777_init(struct radio_tea5777 *tea, struct module *owner)
437{
438 int res;
439
440 tea->write_reg = (1LL << TEA5777_W_IFCE_SHIFT) |
441 (1LL << TEA5777_W_IFW_SHIFT) |
442 (1LL << TEA5777_W_INTEXT_SHIFT) |
443 (1LL << TEA5777_W_CHP0_SHIFT) |
444 (2LL << TEA5777_W_SLEV_SHIFT);
445 tea->freq = 90500 * 16; /* 90.5Mhz default */
446 res = radio_tea5777_set_freq(tea);
447 if (res) {
448 v4l2_err(tea->v4l2_dev, "can't set initial freq (%d)\n", res);
449 return res;
450 }
451
452 tea->vd = tea575x_radio;
453 video_set_drvdata(&tea->vd, tea);
454 mutex_init(&tea->mutex);
455 strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
456 tea->vd.lock = &tea->mutex;
457 tea->vd.v4l2_dev = tea->v4l2_dev;
458 tea->fops = tea575x_fops;
459 tea->fops.owner = owner;
460 tea->vd.fops = &tea->fops;
461 set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
462
463 tea->vd.ctrl_handler = &tea->ctrl_handler;
464 v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
465 v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
466 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
467 res = tea->ctrl_handler.error;
468 if (res) {
469 v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
470 v4l2_ctrl_handler_free(&tea->ctrl_handler);
471 return res;
472 }
473 v4l2_ctrl_handler_setup(&tea->ctrl_handler);
474
475 res = video_register_device(&tea->vd, VFL_TYPE_RADIO, -1);
476 if (res) {
477 v4l2_err(tea->v4l2_dev, "can't register video device!\n");
478 v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
479 return res;
480 }
481
482 return 0;
483}
484EXPORT_SYMBOL_GPL(radio_tea5777_init);
485
486void radio_tea5777_exit(struct radio_tea5777 *tea)
487{
488 video_unregister_device(&tea->vd);
489 v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
490}
491EXPORT_SYMBOL_GPL(radio_tea5777_exit);