radio-tea5777.c 16.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 *   v4l2 driver for TEA5777 Philips AM/FM radio tuner chips
 *
 *	Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
 *
 *   Based on the ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips:
 *
 *	Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/delay.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-event.h>
#include "radio-tea5777.h"

MODULE_AUTHOR("Hans de Goede <perex@perex.cz>");
MODULE_DESCRIPTION("Routines for control of TEA5777 Philips AM/FM radio tuner chips");
MODULE_LICENSE("GPL");

#define TEA5777_FM_IF			150 /* kHz */
43 44 45 46
#define TEA5777_FM_FREQ_STEP		50  /* kHz */

#define TEA5777_AM_IF			21  /* kHz */
#define TEA5777_AM_FREQ_STEP		1   /* kHz */
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

/* Write reg, common bits */
#define TEA5777_W_MUTE_MASK		(1LL << 47)
#define TEA5777_W_MUTE_SHIFT		47
#define TEA5777_W_AM_FM_MASK		(1LL << 46)
#define TEA5777_W_AM_FM_SHIFT		46
#define TEA5777_W_STB_MASK		(1LL << 45)
#define TEA5777_W_STB_SHIFT		45

#define TEA5777_W_IFCE_MASK		(1LL << 29)
#define TEA5777_W_IFCE_SHIFT		29
#define TEA5777_W_IFW_MASK		(1LL << 28)
#define TEA5777_W_IFW_SHIFT		28
#define TEA5777_W_HILO_MASK		(1LL << 27)
#define TEA5777_W_HILO_SHIFT		27
#define TEA5777_W_DBUS_MASK		(1LL << 26)
#define TEA5777_W_DBUS_SHIFT		26

#define TEA5777_W_INTEXT_MASK		(1LL << 24)
#define TEA5777_W_INTEXT_SHIFT		24
#define TEA5777_W_P1_MASK		(1LL << 23)
#define TEA5777_W_P1_SHIFT		23
#define TEA5777_W_P0_MASK		(1LL << 22)
#define TEA5777_W_P0_SHIFT		22
#define TEA5777_W_PEN1_MASK		(1LL << 21)
#define TEA5777_W_PEN1_SHIFT		21
#define TEA5777_W_PEN0_MASK		(1LL << 20)
#define TEA5777_W_PEN0_SHIFT		20

#define TEA5777_W_CHP0_MASK		(1LL << 18)
#define TEA5777_W_CHP0_SHIFT		18
#define TEA5777_W_DEEM_MASK		(1LL << 17)
#define TEA5777_W_DEEM_SHIFT		17

#define TEA5777_W_SEARCH_MASK		(1LL << 7)
#define TEA5777_W_SEARCH_SHIFT		7
#define TEA5777_W_PROGBLIM_MASK		(1LL << 6)
#define TEA5777_W_PROGBLIM_SHIFT	6
#define TEA5777_W_UPDWN_MASK		(1LL << 5)
#define TEA5777_W_UPDWN_SHIFT		5
#define TEA5777_W_SLEV_MASK		(3LL << 3)
#define TEA5777_W_SLEV_SHIFT		3

/* Write reg, FM specific bits */
#define TEA5777_W_FM_PLL_MASK		(0x1fffLL << 32)
#define TEA5777_W_FM_PLL_SHIFT		32
#define TEA5777_W_FM_FREF_MASK		(0x03LL << 30)
#define TEA5777_W_FM_FREF_SHIFT		30
95
#define TEA5777_W_FM_FREF_VALUE		0LL /* 50k steps, 150k IF */
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

#define TEA5777_W_FM_FORCEMONO_MASK	(1LL << 15)
#define TEA5777_W_FM_FORCEMONO_SHIFT	15
#define TEA5777_W_FM_SDSOFF_MASK	(1LL << 14)
#define TEA5777_W_FM_SDSOFF_SHIFT	14
#define TEA5777_W_FM_DOFF_MASK		(1LL << 13)
#define TEA5777_W_FM_DOFF_SHIFT		13

#define TEA5777_W_FM_STEP_MASK		(3LL << 1)
#define TEA5777_W_FM_STEP_SHIFT		1

/* Write reg, AM specific bits */
#define TEA5777_W_AM_PLL_MASK		(0x7ffLL << 34)
#define TEA5777_W_AM_PLL_SHIFT		34
#define TEA5777_W_AM_AGCRF_MASK		(1LL << 33)
#define TEA5777_W_AM_AGCRF_SHIFT	33
#define TEA5777_W_AM_AGCIF_MASK		(1LL << 32)
#define TEA5777_W_AM_AGCIF_SHIFT	32
#define TEA5777_W_AM_MWLW_MASK		(1LL << 31)
#define TEA5777_W_AM_MWLW_SHIFT		31
116 117
#define TEA5777_W_AM_LW			0LL
#define TEA5777_W_AM_MW			1LL
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#define TEA5777_W_AM_LNA_MASK		(1LL << 30)
#define TEA5777_W_AM_LNA_SHIFT		30

#define TEA5777_W_AM_PEAK_MASK		(1LL << 25)
#define TEA5777_W_AM_PEAK_SHIFT		25

#define TEA5777_W_AM_RFB_MASK		(1LL << 16)
#define TEA5777_W_AM_RFB_SHIFT		16
#define TEA5777_W_AM_CALLIGN_MASK	(1LL << 15)
#define TEA5777_W_AM_CALLIGN_SHIFT	15
#define TEA5777_W_AM_CBANK_MASK		(0x7fLL << 8)
#define TEA5777_W_AM_CBANK_SHIFT	8

#define TEA5777_W_AM_DELAY_MASK		(1LL << 2)
#define TEA5777_W_AM_DELAY_SHIFT	2
#define TEA5777_W_AM_STEP_MASK		(1LL << 1)
#define TEA5777_W_AM_STEP_SHIFT		1

/* Read reg, common bits */
#define TEA5777_R_LEVEL_MASK		(0x0f << 17)
#define TEA5777_R_LEVEL_SHIFT		17
#define TEA5777_R_SFOUND_MASK		(0x01 << 16)
#define TEA5777_R_SFOUND_SHIFT		16
#define TEA5777_R_BLIM_MASK		(0x01 << 15)
#define TEA5777_R_BLIM_SHIFT		15

/* Read reg, FM specific bits */
#define TEA5777_R_FM_STEREO_MASK	(0x01 << 21)
#define TEA5777_R_FM_STEREO_SHIFT	21
#define TEA5777_R_FM_PLL_MASK		0x1fff
#define TEA5777_R_FM_PLL_SHIFT		0

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
enum { BAND_FM, BAND_AM };

static const struct v4l2_frequency_band bands[] = {
	{
		.type = V4L2_TUNER_RADIO,
		.index = 0,
		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
			      V4L2_TUNER_CAP_FREQ_BANDS |
			      V4L2_TUNER_CAP_HWSEEK_BOUNDED |
			      V4L2_TUNER_CAP_HWSEEK_PROG_LIM,
		.rangelow   =  76000 * 16,
		.rangehigh  = 108000 * 16,
		.modulation = V4L2_BAND_MODULATION_FM,
	},
	{
		.type = V4L2_TUNER_RADIO,
		.index = 1,
		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS |
			      V4L2_TUNER_CAP_HWSEEK_BOUNDED |
			      V4L2_TUNER_CAP_HWSEEK_PROG_LIM,
		.rangelow   =  530 * 16,
		.rangehigh  = 1710 * 16,
		.modulation = V4L2_BAND_MODULATION_AM,
	},
};

176 177
static u32 tea5777_freq_to_v4l2_freq(struct radio_tea5777 *tea, u32 freq)
{
178 179 180 181 182 183 184
	switch (tea->band) {
	case BAND_FM:
		return (freq * TEA5777_FM_FREQ_STEP + TEA5777_FM_IF) * 16;
	case BAND_AM:
		return (freq * TEA5777_AM_FREQ_STEP + TEA5777_AM_IF) * 16;
	}
	return 0; /* Never reached */
185 186
}

187
int radio_tea5777_set_freq(struct radio_tea5777 *tea)
188
{
189
	u32 freq;
190 191
	int res;

192 193
	freq = clamp(tea->freq, bands[tea->band].rangelow,
				bands[tea->band].rangehigh);
194
	freq = (freq + 8) / 16; /* to kHz */
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	switch (tea->band) {
	case BAND_FM:
		tea->write_reg &= ~TEA5777_W_AM_FM_MASK;
		freq = (freq - TEA5777_FM_IF) / TEA5777_FM_FREQ_STEP;
		tea->write_reg &= ~TEA5777_W_FM_PLL_MASK;
		tea->write_reg |= (u64)freq << TEA5777_W_FM_PLL_SHIFT;
		tea->write_reg &= ~TEA5777_W_FM_FREF_MASK;
		tea->write_reg |= TEA5777_W_FM_FREF_VALUE <<
				  TEA5777_W_FM_FREF_SHIFT;
		tea->write_reg &= ~TEA5777_W_FM_FORCEMONO_MASK;
		if (tea->audmode == V4L2_TUNER_MODE_MONO)
			tea->write_reg |= 1LL << TEA5777_W_FM_FORCEMONO_SHIFT;
		break;
	case BAND_AM:
		tea->write_reg &= ~TEA5777_W_AM_FM_MASK;
		tea->write_reg |= (1LL << TEA5777_W_AM_FM_SHIFT);
		freq = (freq - TEA5777_AM_IF) / TEA5777_AM_FREQ_STEP;
		tea->write_reg &= ~TEA5777_W_AM_PLL_MASK;
		tea->write_reg |= (u64)freq << TEA5777_W_AM_PLL_SHIFT;
		tea->write_reg &= ~TEA5777_W_AM_AGCRF_MASK;
		tea->write_reg &= ~TEA5777_W_AM_AGCRF_MASK;
		tea->write_reg &= ~TEA5777_W_AM_MWLW_MASK;
		tea->write_reg |= TEA5777_W_AM_MW << TEA5777_W_AM_MWLW_SHIFT;
		tea->write_reg &= ~TEA5777_W_AM_LNA_MASK;
		tea->write_reg |= 1LL << TEA5777_W_AM_LNA_SHIFT;
		tea->write_reg &= ~TEA5777_W_AM_PEAK_MASK;
		tea->write_reg |= 1LL << TEA5777_W_AM_PEAK_SHIFT;
		tea->write_reg &= ~TEA5777_W_AM_CALLIGN_MASK;
		break;
	}
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

	res = tea->ops->write_reg(tea, tea->write_reg);
	if (res)
		return res;

	tea->needs_write = false;
	tea->read_reg = -1;
	tea->freq = tea5777_freq_to_v4l2_freq(tea, freq);

	return 0;
}

static int radio_tea5777_update_read_reg(struct radio_tea5777 *tea, int wait)
{
	int res;

	if (tea->read_reg != -1)
		return 0;

	if (tea->write_before_read && tea->needs_write) {
		res = radio_tea5777_set_freq(tea);
		if (res)
			return res;
	}

	if (wait) {
		if (schedule_timeout_interruptible(msecs_to_jiffies(wait)))
			return -ERESTARTSYS;
	}

	res = tea->ops->read_reg(tea, &tea->read_reg);
	if (res)
		return res;

	tea->needs_write = true;
	return 0;
}

/*
 * Linux Video interface
 */

static int vidioc_querycap(struct file *file, void  *priv,
					struct v4l2_capability *v)
{
	struct radio_tea5777 *tea = video_drvdata(file);

	strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
	strlcpy(v->card, tea->card, sizeof(v->card));
	strlcat(v->card, " TEA5777", sizeof(v->card));
	strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
	v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
	v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
	v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
	return 0;
}

283 284 285 286 287 288 289 290 291 292 293 294 295
static int vidioc_enum_freq_bands(struct file *file, void *priv,
					 struct v4l2_frequency_band *band)
{
	struct radio_tea5777 *tea = video_drvdata(file);

	if (band->tuner != 0 || band->index >= ARRAY_SIZE(bands) ||
	    (!tea->has_am && band->index == BAND_AM))
		return -EINVAL;

	*band = bands[band->index];
	return 0;
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
static int vidioc_g_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
{
	struct radio_tea5777 *tea = video_drvdata(file);
	int res;

	if (v->index > 0)
		return -EINVAL;

	res = radio_tea5777_update_read_reg(tea, 0);
	if (res)
		return res;

	memset(v, 0, sizeof(*v));
	if (tea->has_am)
		strlcpy(v->name, "AM/FM", sizeof(v->name));
	else
		strlcpy(v->name, "FM", sizeof(v->name));
	v->type = V4L2_TUNER_RADIO;
	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
316 317 318 319 320 321 322 323 324 325 326 327
			V4L2_TUNER_CAP_FREQ_BANDS |
			V4L2_TUNER_CAP_HWSEEK_BOUNDED |
			V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
	v->rangelow   = tea->has_am ? bands[BAND_AM].rangelow :
				      bands[BAND_FM].rangelow;
	v->rangehigh  = bands[BAND_FM].rangehigh;
	if (tea->band == BAND_FM &&
			(tea->read_reg & TEA5777_R_FM_STEREO_MASK))
		v->rxsubchans = V4L2_TUNER_SUB_STEREO;
	else
		v->rxsubchans = V4L2_TUNER_SUB_MONO;
	v->audmode = tea->audmode;
328 329 330 331 332 333 334 335 336 337 338
	/* shift - 12 to convert 4-bits (0-15) scale to 16-bits (0-65535) */
	v->signal = (tea->read_reg & TEA5777_R_LEVEL_MASK) >>
		    (TEA5777_R_LEVEL_SHIFT - 12);

	/* Invalidate read_reg, so that next call we return up2date signal */
	tea->read_reg = -1;

	return 0;
}

static int vidioc_s_tuner(struct file *file, void *priv,
339
					const struct v4l2_tuner *v)
340 341
{
	struct radio_tea5777 *tea = video_drvdata(file);
342
	u32 orig_audmode = tea->audmode;
343 344 345 346

	if (v->index)
		return -EINVAL;

347
	tea->audmode = v->audmode;
348 349
	if (tea->audmode > V4L2_TUNER_MODE_STEREO)
		tea->audmode = V4L2_TUNER_MODE_STEREO;
350 351 352 353 354

	if (tea->audmode != orig_audmode && tea->band == BAND_FM)
		return radio_tea5777_set_freq(tea);

	return 0;
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
}

static int vidioc_g_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
	struct radio_tea5777 *tea = video_drvdata(file);

	if (f->tuner != 0)
		return -EINVAL;
	f->type = V4L2_TUNER_RADIO;
	f->frequency = tea->freq;
	return 0;
}

static int vidioc_s_frequency(struct file *file, void *priv,
370
					const struct v4l2_frequency *f)
371 372 373 374 375 376
{
	struct radio_tea5777 *tea = video_drvdata(file);

	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
		return -EINVAL;

377 378 379 380 381
	if (tea->has_am && f->frequency < (20000 * 16))
		tea->band = BAND_AM;
	else
		tea->band = BAND_FM;

382 383 384 385 386
	tea->freq = f->frequency;
	return radio_tea5777_set_freq(tea);
}

static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
387
					const struct v4l2_hw_freq_seek *a)
388 389 390
{
	struct radio_tea5777 *tea = video_drvdata(file);
	unsigned long timeout;
391 392
	u32 rangelow = a->rangelow;
	u32 rangehigh = a->rangehigh;
393 394
	int i, res, spacing;
	u32 orig_freq;
395 396 397 398

	if (a->tuner || a->wrap_around)
		return -EINVAL;

399 400 401
	if (file->f_flags & O_NONBLOCK)
		return -EWOULDBLOCK;

402
	if (rangelow || rangehigh) {
403 404 405
		for (i = 0; i < ARRAY_SIZE(bands); i++) {
			if (i == BAND_AM && !tea->has_am)
				continue;
406 407
			if (bands[i].rangelow  >= rangelow &&
			    bands[i].rangehigh <= rangehigh)
408 409 410 411 412 413
				break;
		}
		if (i == ARRAY_SIZE(bands))
			return -EINVAL; /* No matching band found */

		tea->band = i;
414 415 416
		if (tea->freq < rangelow || tea->freq > rangehigh) {
			tea->freq = clamp(tea->freq, rangelow,
						     rangehigh);
417 418 419 420 421
			res = radio_tea5777_set_freq(tea);
			if (res)
				return res;
		}
	} else {
422 423
		rangelow  = bands[tea->band].rangelow;
		rangehigh = bands[tea->band].rangehigh;
424 425 426 427 428
	}

	spacing   = (tea->band == BAND_AM) ? (5 * 16) : (200 * 16); /* kHz */
	orig_freq = tea->freq;

429
	tea->write_reg |= TEA5777_W_PROGBLIM_MASK;
430
	if (tea->seek_rangelow != rangelow) {
431
		tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
432
		tea->freq = rangelow;
433 434 435
		res = radio_tea5777_set_freq(tea);
		if (res)
			goto leave;
436
		tea->seek_rangelow = rangelow;
437
	}
438
	if (tea->seek_rangehigh != rangehigh) {
439
		tea->write_reg |= TEA5777_W_UPDWN_MASK;
440
		tea->freq = rangehigh;
441 442 443
		res = radio_tea5777_set_freq(tea);
		if (res)
			goto leave;
444
		tea->seek_rangehigh = rangehigh;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	}
	tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;

	tea->write_reg |= TEA5777_W_SEARCH_MASK;
	if (a->seek_upward) {
		tea->write_reg |= TEA5777_W_UPDWN_MASK;
		tea->freq = orig_freq + spacing;
	} else {
		tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
		tea->freq = orig_freq - spacing;
	}
	res = radio_tea5777_set_freq(tea);
	if (res)
		goto leave;

	timeout = jiffies + msecs_to_jiffies(5000);
	for (;;) {
		if (time_after(jiffies, timeout)) {
			res = -ENODATA;
			break;
		}

		res = radio_tea5777_update_read_reg(tea, 100);
		if (res)
			break;

		/*
		 * Note we use tea->freq to track how far we've searched sofar
		 * this is necessary to ensure we continue seeking at the right
		 * point, in the write_before_read case.
		 */
		tea->freq = (tea->read_reg & TEA5777_R_FM_PLL_MASK);
		tea->freq = tea5777_freq_to_v4l2_freq(tea, tea->freq);

		if ((tea->read_reg & TEA5777_R_SFOUND_MASK)) {
			tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
			return 0;
		}

		if (tea->read_reg & TEA5777_R_BLIM_MASK) {
			res = -ENODATA;
			break;
		}

		/* Force read_reg update */
		tea->read_reg = -1;
	}
leave:
	tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
	tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
	tea->freq = orig_freq;
	radio_tea5777_set_freq(tea);
	return res;
}

static int tea575x_s_ctrl(struct v4l2_ctrl *c)
{
	struct radio_tea5777 *tea =
		container_of(c->handler, struct radio_tea5777, ctrl_handler);

	switch (c->id) {
	case V4L2_CID_AUDIO_MUTE:
		if (c->val)
			tea->write_reg |= TEA5777_W_MUTE_MASK;
		else
			tea->write_reg &= ~TEA5777_W_MUTE_MASK;

		return radio_tea5777_set_freq(tea);
	}

	return -EINVAL;
}

static const struct v4l2_file_operations tea575x_fops = {
	.unlocked_ioctl	= video_ioctl2,
	.open		= v4l2_fh_open,
	.release	= v4l2_fh_release,
	.poll		= v4l2_ctrl_poll,
};

static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
	.vidioc_querycap    = vidioc_querycap,
	.vidioc_g_tuner     = vidioc_g_tuner,
	.vidioc_s_tuner     = vidioc_s_tuner,
	.vidioc_g_frequency = vidioc_g_frequency,
	.vidioc_s_frequency = vidioc_s_frequency,
	.vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
532
	.vidioc_enum_freq_bands = vidioc_enum_freq_bands,
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
	.vidioc_log_status  = v4l2_ctrl_log_status,
	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};

static const struct video_device tea575x_radio = {
	.ioctl_ops	= &tea575x_ioctl_ops,
	.release	= video_device_release_empty,
};

static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
	.s_ctrl = tea575x_s_ctrl,
};

int radio_tea5777_init(struct radio_tea5777 *tea, struct module *owner)
{
	int res;

	tea->write_reg = (1LL << TEA5777_W_IFCE_SHIFT) |
			 (1LL << TEA5777_W_IFW_SHIFT) |
			 (1LL << TEA5777_W_INTEXT_SHIFT) |
			 (1LL << TEA5777_W_CHP0_SHIFT) |
555
			 (1LL << TEA5777_W_SLEV_SHIFT);
556
	tea->freq = 90500 * 16;	/* 90.5Mhz default */
557
	tea->audmode = V4L2_TUNER_MODE_STEREO;
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	res = radio_tea5777_set_freq(tea);
	if (res) {
		v4l2_err(tea->v4l2_dev, "can't set initial freq (%d)\n", res);
		return res;
	}

	tea->vd = tea575x_radio;
	video_set_drvdata(&tea->vd, tea);
	mutex_init(&tea->mutex);
	strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
	tea->vd.lock = &tea->mutex;
	tea->vd.v4l2_dev = tea->v4l2_dev;
	tea->fops = tea575x_fops;
	tea->fops.owner = owner;
	tea->vd.fops = &tea->fops;
	set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);

	tea->vd.ctrl_handler = &tea->ctrl_handler;
	v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
	v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
			  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
	res = tea->ctrl_handler.error;
	if (res) {
		v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
		v4l2_ctrl_handler_free(&tea->ctrl_handler);
		return res;
	}
	v4l2_ctrl_handler_setup(&tea->ctrl_handler);

	res = video_register_device(&tea->vd, VFL_TYPE_RADIO, -1);
	if (res) {
		v4l2_err(tea->v4l2_dev, "can't register video device!\n");
		v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
		return res;
	}

	return 0;
}
EXPORT_SYMBOL_GPL(radio_tea5777_init);

void radio_tea5777_exit(struct radio_tea5777 *tea)
{
	video_unregister_device(&tea->vd);
	v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
}
EXPORT_SYMBOL_GPL(radio_tea5777_exit);