1 /*
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  */
21 
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-dev.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-event.h>
32 #include "radio-tea5777.h"
33 
34 MODULE_AUTHOR("Hans de Goede <perex@perex.cz>");
35 MODULE_DESCRIPTION("Routines for control of TEA5777 Philips AM/FM radio tuner chips");
36 MODULE_LICENSE("GPL");
37 
38 #define TEA5777_FM_IF			150 /* kHz */
39 #define TEA5777_FM_FREQ_STEP		50  /* kHz */
40 
41 #define TEA5777_AM_IF			21  /* kHz */
42 #define TEA5777_AM_FREQ_STEP		1   /* kHz */
43 
44 /* Write reg, common bits */
45 #define TEA5777_W_MUTE_MASK		(1LL << 47)
46 #define TEA5777_W_MUTE_SHIFT		47
47 #define TEA5777_W_AM_FM_MASK		(1LL << 46)
48 #define TEA5777_W_AM_FM_SHIFT		46
49 #define TEA5777_W_STB_MASK		(1LL << 45)
50 #define TEA5777_W_STB_SHIFT		45
51 
52 #define TEA5777_W_IFCE_MASK		(1LL << 29)
53 #define TEA5777_W_IFCE_SHIFT		29
54 #define TEA5777_W_IFW_MASK		(1LL << 28)
55 #define TEA5777_W_IFW_SHIFT		28
56 #define TEA5777_W_HILO_MASK		(1LL << 27)
57 #define TEA5777_W_HILO_SHIFT		27
58 #define TEA5777_W_DBUS_MASK		(1LL << 26)
59 #define TEA5777_W_DBUS_SHIFT		26
60 
61 #define TEA5777_W_INTEXT_MASK		(1LL << 24)
62 #define TEA5777_W_INTEXT_SHIFT		24
63 #define TEA5777_W_P1_MASK		(1LL << 23)
64 #define TEA5777_W_P1_SHIFT		23
65 #define TEA5777_W_P0_MASK		(1LL << 22)
66 #define TEA5777_W_P0_SHIFT		22
67 #define TEA5777_W_PEN1_MASK		(1LL << 21)
68 #define TEA5777_W_PEN1_SHIFT		21
69 #define TEA5777_W_PEN0_MASK		(1LL << 20)
70 #define TEA5777_W_PEN0_SHIFT		20
71 
72 #define TEA5777_W_CHP0_MASK		(1LL << 18)
73 #define TEA5777_W_CHP0_SHIFT		18
74 #define TEA5777_W_DEEM_MASK		(1LL << 17)
75 #define TEA5777_W_DEEM_SHIFT		17
76 
77 #define TEA5777_W_SEARCH_MASK		(1LL << 7)
78 #define TEA5777_W_SEARCH_SHIFT		7
79 #define TEA5777_W_PROGBLIM_MASK		(1LL << 6)
80 #define TEA5777_W_PROGBLIM_SHIFT	6
81 #define TEA5777_W_UPDWN_MASK		(1LL << 5)
82 #define TEA5777_W_UPDWN_SHIFT		5
83 #define TEA5777_W_SLEV_MASK		(3LL << 3)
84 #define TEA5777_W_SLEV_SHIFT		3
85 
86 /* Write reg, FM specific bits */
87 #define TEA5777_W_FM_PLL_MASK		(0x1fffLL << 32)
88 #define TEA5777_W_FM_PLL_SHIFT		32
89 #define TEA5777_W_FM_FREF_MASK		(0x03LL << 30)
90 #define TEA5777_W_FM_FREF_SHIFT		30
91 #define TEA5777_W_FM_FREF_VALUE		0LL /* 50k steps, 150k IF */
92 
93 #define TEA5777_W_FM_FORCEMONO_MASK	(1LL << 15)
94 #define TEA5777_W_FM_FORCEMONO_SHIFT	15
95 #define TEA5777_W_FM_SDSOFF_MASK	(1LL << 14)
96 #define TEA5777_W_FM_SDSOFF_SHIFT	14
97 #define TEA5777_W_FM_DOFF_MASK		(1LL << 13)
98 #define TEA5777_W_FM_DOFF_SHIFT		13
99 
100 #define TEA5777_W_FM_STEP_MASK		(3LL << 1)
101 #define TEA5777_W_FM_STEP_SHIFT		1
102 
103 /* Write reg, AM specific bits */
104 #define TEA5777_W_AM_PLL_MASK		(0x7ffLL << 34)
105 #define TEA5777_W_AM_PLL_SHIFT		34
106 #define TEA5777_W_AM_AGCRF_MASK		(1LL << 33)
107 #define TEA5777_W_AM_AGCRF_SHIFT	33
108 #define TEA5777_W_AM_AGCIF_MASK		(1LL << 32)
109 #define TEA5777_W_AM_AGCIF_SHIFT	32
110 #define TEA5777_W_AM_MWLW_MASK		(1LL << 31)
111 #define TEA5777_W_AM_MWLW_SHIFT		31
112 #define TEA5777_W_AM_LW			0LL
113 #define TEA5777_W_AM_MW			1LL
114 #define TEA5777_W_AM_LNA_MASK		(1LL << 30)
115 #define TEA5777_W_AM_LNA_SHIFT		30
116 
117 #define TEA5777_W_AM_PEAK_MASK		(1LL << 25)
118 #define TEA5777_W_AM_PEAK_SHIFT		25
119 
120 #define TEA5777_W_AM_RFB_MASK		(1LL << 16)
121 #define TEA5777_W_AM_RFB_SHIFT		16
122 #define TEA5777_W_AM_CALLIGN_MASK	(1LL << 15)
123 #define TEA5777_W_AM_CALLIGN_SHIFT	15
124 #define TEA5777_W_AM_CBANK_MASK		(0x7fLL << 8)
125 #define TEA5777_W_AM_CBANK_SHIFT	8
126 
127 #define TEA5777_W_AM_DELAY_MASK		(1LL << 2)
128 #define TEA5777_W_AM_DELAY_SHIFT	2
129 #define TEA5777_W_AM_STEP_MASK		(1LL << 1)
130 #define TEA5777_W_AM_STEP_SHIFT		1
131 
132 /* Read reg, common bits */
133 #define TEA5777_R_LEVEL_MASK		(0x0f << 17)
134 #define TEA5777_R_LEVEL_SHIFT		17
135 #define TEA5777_R_SFOUND_MASK		(0x01 << 16)
136 #define TEA5777_R_SFOUND_SHIFT		16
137 #define TEA5777_R_BLIM_MASK		(0x01 << 15)
138 #define TEA5777_R_BLIM_SHIFT		15
139 
140 /* Read reg, FM specific bits */
141 #define TEA5777_R_FM_STEREO_MASK	(0x01 << 21)
142 #define TEA5777_R_FM_STEREO_SHIFT	21
143 #define TEA5777_R_FM_PLL_MASK		0x1fff
144 #define TEA5777_R_FM_PLL_SHIFT		0
145 
146 enum { BAND_FM, BAND_AM };
147 
148 static const struct v4l2_frequency_band bands[] = {
149 	{
150 		.type = V4L2_TUNER_RADIO,
151 		.index = 0,
152 		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
153 			      V4L2_TUNER_CAP_FREQ_BANDS |
154 			      V4L2_TUNER_CAP_HWSEEK_BOUNDED |
155 			      V4L2_TUNER_CAP_HWSEEK_PROG_LIM,
156 		.rangelow   =  76000 * 16,
157 		.rangehigh  = 108000 * 16,
158 		.modulation = V4L2_BAND_MODULATION_FM,
159 	},
160 	{
161 		.type = V4L2_TUNER_RADIO,
162 		.index = 1,
163 		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS |
164 			      V4L2_TUNER_CAP_HWSEEK_BOUNDED |
165 			      V4L2_TUNER_CAP_HWSEEK_PROG_LIM,
166 		.rangelow   =  530 * 16,
167 		.rangehigh  = 1710 * 16,
168 		.modulation = V4L2_BAND_MODULATION_AM,
169 	},
170 };
171 
172 static u32 tea5777_freq_to_v4l2_freq(struct radio_tea5777 *tea, u32 freq)
173 {
174 	switch (tea->band) {
175 	case BAND_FM:
176 		return (freq * TEA5777_FM_FREQ_STEP + TEA5777_FM_IF) * 16;
177 	case BAND_AM:
178 		return (freq * TEA5777_AM_FREQ_STEP + TEA5777_AM_IF) * 16;
179 	}
180 	return 0; /* Never reached */
181 }
182 
183 int radio_tea5777_set_freq(struct radio_tea5777 *tea)
184 {
185 	u32 freq;
186 	int res;
187 
188 	freq = clamp(tea->freq, bands[tea->band].rangelow,
189 				bands[tea->band].rangehigh);
190 	freq = (freq + 8) / 16; /* to kHz */
191 
192 	switch (tea->band) {
193 	case BAND_FM:
194 		tea->write_reg &= ~TEA5777_W_AM_FM_MASK;
195 		freq = (freq - TEA5777_FM_IF) / TEA5777_FM_FREQ_STEP;
196 		tea->write_reg &= ~TEA5777_W_FM_PLL_MASK;
197 		tea->write_reg |= (u64)freq << TEA5777_W_FM_PLL_SHIFT;
198 		tea->write_reg &= ~TEA5777_W_FM_FREF_MASK;
199 		tea->write_reg |= TEA5777_W_FM_FREF_VALUE <<
200 				  TEA5777_W_FM_FREF_SHIFT;
201 		tea->write_reg &= ~TEA5777_W_FM_FORCEMONO_MASK;
202 		if (tea->audmode == V4L2_TUNER_MODE_MONO)
203 			tea->write_reg |= 1LL << TEA5777_W_FM_FORCEMONO_SHIFT;
204 		break;
205 	case BAND_AM:
206 		tea->write_reg &= ~TEA5777_W_AM_FM_MASK;
207 		tea->write_reg |= (1LL << TEA5777_W_AM_FM_SHIFT);
208 		freq = (freq - TEA5777_AM_IF) / TEA5777_AM_FREQ_STEP;
209 		tea->write_reg &= ~TEA5777_W_AM_PLL_MASK;
210 		tea->write_reg |= (u64)freq << TEA5777_W_AM_PLL_SHIFT;
211 		tea->write_reg &= ~TEA5777_W_AM_AGCRF_MASK;
212 		tea->write_reg &= ~TEA5777_W_AM_AGCRF_MASK;
213 		tea->write_reg &= ~TEA5777_W_AM_MWLW_MASK;
214 		tea->write_reg |= TEA5777_W_AM_MW << TEA5777_W_AM_MWLW_SHIFT;
215 		tea->write_reg &= ~TEA5777_W_AM_LNA_MASK;
216 		tea->write_reg |= 1LL << TEA5777_W_AM_LNA_SHIFT;
217 		tea->write_reg &= ~TEA5777_W_AM_PEAK_MASK;
218 		tea->write_reg |= 1LL << TEA5777_W_AM_PEAK_SHIFT;
219 		tea->write_reg &= ~TEA5777_W_AM_CALLIGN_MASK;
220 		break;
221 	}
222 
223 	res = tea->ops->write_reg(tea, tea->write_reg);
224 	if (res)
225 		return res;
226 
227 	tea->needs_write = false;
228 	tea->read_reg = -1;
229 	tea->freq = tea5777_freq_to_v4l2_freq(tea, freq);
230 
231 	return 0;
232 }
233 
234 static int radio_tea5777_update_read_reg(struct radio_tea5777 *tea, int wait)
235 {
236 	int res;
237 
238 	if (tea->read_reg != -1)
239 		return 0;
240 
241 	if (tea->write_before_read && tea->needs_write) {
242 		res = radio_tea5777_set_freq(tea);
243 		if (res)
244 			return res;
245 	}
246 
247 	if (wait) {
248 		if (schedule_timeout_interruptible(msecs_to_jiffies(wait)))
249 			return -ERESTARTSYS;
250 	}
251 
252 	res = tea->ops->read_reg(tea, &tea->read_reg);
253 	if (res)
254 		return res;
255 
256 	tea->needs_write = true;
257 	return 0;
258 }
259 
260 /*
261  * Linux Video interface
262  */
263 
264 static int vidioc_querycap(struct file *file, void  *priv,
265 					struct v4l2_capability *v)
266 {
267 	struct radio_tea5777 *tea = video_drvdata(file);
268 
269 	strscpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
270 	strscpy(v->card, tea->card, sizeof(v->card));
271 	strlcat(v->card, " TEA5777", sizeof(v->card));
272 	strscpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
273 	return 0;
274 }
275 
276 static int vidioc_enum_freq_bands(struct file *file, void *priv,
277 					 struct v4l2_frequency_band *band)
278 {
279 	struct radio_tea5777 *tea = video_drvdata(file);
280 
281 	if (band->tuner != 0 || band->index >= ARRAY_SIZE(bands) ||
282 	    (!tea->has_am && band->index == BAND_AM))
283 		return -EINVAL;
284 
285 	*band = bands[band->index];
286 	return 0;
287 }
288 
289 static int vidioc_g_tuner(struct file *file, void *priv,
290 					struct v4l2_tuner *v)
291 {
292 	struct radio_tea5777 *tea = video_drvdata(file);
293 	int res;
294 
295 	if (v->index > 0)
296 		return -EINVAL;
297 
298 	res = radio_tea5777_update_read_reg(tea, 0);
299 	if (res)
300 		return res;
301 
302 	memset(v, 0, sizeof(*v));
303 	if (tea->has_am)
304 		strscpy(v->name, "AM/FM", sizeof(v->name));
305 	else
306 		strscpy(v->name, "FM", sizeof(v->name));
307 	v->type = V4L2_TUNER_RADIO;
308 	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
309 			V4L2_TUNER_CAP_FREQ_BANDS |
310 			V4L2_TUNER_CAP_HWSEEK_BOUNDED |
311 			V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
312 	v->rangelow   = tea->has_am ? bands[BAND_AM].rangelow :
313 				      bands[BAND_FM].rangelow;
314 	v->rangehigh  = bands[BAND_FM].rangehigh;
315 	if (tea->band == BAND_FM &&
316 			(tea->read_reg & TEA5777_R_FM_STEREO_MASK))
317 		v->rxsubchans = V4L2_TUNER_SUB_STEREO;
318 	else
319 		v->rxsubchans = V4L2_TUNER_SUB_MONO;
320 	v->audmode = tea->audmode;
321 	/* shift - 12 to convert 4-bits (0-15) scale to 16-bits (0-65535) */
322 	v->signal = (tea->read_reg & TEA5777_R_LEVEL_MASK) >>
323 		    (TEA5777_R_LEVEL_SHIFT - 12);
324 
325 	/* Invalidate read_reg, so that next call we return up2date signal */
326 	tea->read_reg = -1;
327 
328 	return 0;
329 }
330 
331 static int vidioc_s_tuner(struct file *file, void *priv,
332 					const struct v4l2_tuner *v)
333 {
334 	struct radio_tea5777 *tea = video_drvdata(file);
335 	u32 orig_audmode = tea->audmode;
336 
337 	if (v->index)
338 		return -EINVAL;
339 
340 	tea->audmode = v->audmode;
341 	if (tea->audmode > V4L2_TUNER_MODE_STEREO)
342 		tea->audmode = V4L2_TUNER_MODE_STEREO;
343 
344 	if (tea->audmode != orig_audmode && tea->band == BAND_FM)
345 		return radio_tea5777_set_freq(tea);
346 
347 	return 0;
348 }
349 
350 static int vidioc_g_frequency(struct file *file, void *priv,
351 					struct v4l2_frequency *f)
352 {
353 	struct radio_tea5777 *tea = video_drvdata(file);
354 
355 	if (f->tuner != 0)
356 		return -EINVAL;
357 	f->type = V4L2_TUNER_RADIO;
358 	f->frequency = tea->freq;
359 	return 0;
360 }
361 
362 static int vidioc_s_frequency(struct file *file, void *priv,
363 					const struct v4l2_frequency *f)
364 {
365 	struct radio_tea5777 *tea = video_drvdata(file);
366 
367 	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
368 		return -EINVAL;
369 
370 	if (tea->has_am && f->frequency < (20000 * 16))
371 		tea->band = BAND_AM;
372 	else
373 		tea->band = BAND_FM;
374 
375 	tea->freq = f->frequency;
376 	return radio_tea5777_set_freq(tea);
377 }
378 
379 static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
380 					const struct v4l2_hw_freq_seek *a)
381 {
382 	struct radio_tea5777 *tea = video_drvdata(file);
383 	unsigned long timeout;
384 	u32 rangelow = a->rangelow;
385 	u32 rangehigh = a->rangehigh;
386 	int i, res, spacing;
387 	u32 orig_freq;
388 
389 	if (a->tuner || a->wrap_around)
390 		return -EINVAL;
391 
392 	if (file->f_flags & O_NONBLOCK)
393 		return -EWOULDBLOCK;
394 
395 	if (rangelow || rangehigh) {
396 		for (i = 0; i < ARRAY_SIZE(bands); i++) {
397 			if (i == BAND_AM && !tea->has_am)
398 				continue;
399 			if (bands[i].rangelow  >= rangelow &&
400 			    bands[i].rangehigh <= rangehigh)
401 				break;
402 		}
403 		if (i == ARRAY_SIZE(bands))
404 			return -EINVAL; /* No matching band found */
405 
406 		tea->band = i;
407 		if (tea->freq < rangelow || tea->freq > rangehigh) {
408 			tea->freq = clamp(tea->freq, rangelow,
409 						     rangehigh);
410 			res = radio_tea5777_set_freq(tea);
411 			if (res)
412 				return res;
413 		}
414 	} else {
415 		rangelow  = bands[tea->band].rangelow;
416 		rangehigh = bands[tea->band].rangehigh;
417 	}
418 
419 	spacing   = (tea->band == BAND_AM) ? (5 * 16) : (200 * 16); /* kHz */
420 	orig_freq = tea->freq;
421 
422 	tea->write_reg |= TEA5777_W_PROGBLIM_MASK;
423 	if (tea->seek_rangelow != rangelow) {
424 		tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
425 		tea->freq = rangelow;
426 		res = radio_tea5777_set_freq(tea);
427 		if (res)
428 			goto leave;
429 		tea->seek_rangelow = rangelow;
430 	}
431 	if (tea->seek_rangehigh != rangehigh) {
432 		tea->write_reg |= TEA5777_W_UPDWN_MASK;
433 		tea->freq = rangehigh;
434 		res = radio_tea5777_set_freq(tea);
435 		if (res)
436 			goto leave;
437 		tea->seek_rangehigh = rangehigh;
438 	}
439 	tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
440 
441 	tea->write_reg |= TEA5777_W_SEARCH_MASK;
442 	if (a->seek_upward) {
443 		tea->write_reg |= TEA5777_W_UPDWN_MASK;
444 		tea->freq = orig_freq + spacing;
445 	} else {
446 		tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
447 		tea->freq = orig_freq - spacing;
448 	}
449 	res = radio_tea5777_set_freq(tea);
450 	if (res)
451 		goto leave;
452 
453 	timeout = jiffies + msecs_to_jiffies(5000);
454 	for (;;) {
455 		if (time_after(jiffies, timeout)) {
456 			res = -ENODATA;
457 			break;
458 		}
459 
460 		res = radio_tea5777_update_read_reg(tea, 100);
461 		if (res)
462 			break;
463 
464 		/*
465 		 * Note we use tea->freq to track how far we've searched sofar
466 		 * this is necessary to ensure we continue seeking at the right
467 		 * point, in the write_before_read case.
468 		 */
469 		tea->freq = (tea->read_reg & TEA5777_R_FM_PLL_MASK);
470 		tea->freq = tea5777_freq_to_v4l2_freq(tea, tea->freq);
471 
472 		if ((tea->read_reg & TEA5777_R_SFOUND_MASK)) {
473 			tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
474 			return 0;
475 		}
476 
477 		if (tea->read_reg & TEA5777_R_BLIM_MASK) {
478 			res = -ENODATA;
479 			break;
480 		}
481 
482 		/* Force read_reg update */
483 		tea->read_reg = -1;
484 	}
485 leave:
486 	tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
487 	tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
488 	tea->freq = orig_freq;
489 	radio_tea5777_set_freq(tea);
490 	return res;
491 }
492 
493 static int tea575x_s_ctrl(struct v4l2_ctrl *c)
494 {
495 	struct radio_tea5777 *tea =
496 		container_of(c->handler, struct radio_tea5777, ctrl_handler);
497 
498 	switch (c->id) {
499 	case V4L2_CID_AUDIO_MUTE:
500 		if (c->val)
501 			tea->write_reg |= TEA5777_W_MUTE_MASK;
502 		else
503 			tea->write_reg &= ~TEA5777_W_MUTE_MASK;
504 
505 		return radio_tea5777_set_freq(tea);
506 	}
507 
508 	return -EINVAL;
509 }
510 
511 static const struct v4l2_file_operations tea575x_fops = {
512 	.unlocked_ioctl	= video_ioctl2,
513 	.open		= v4l2_fh_open,
514 	.release	= v4l2_fh_release,
515 	.poll		= v4l2_ctrl_poll,
516 };
517 
518 static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
519 	.vidioc_querycap    = vidioc_querycap,
520 	.vidioc_g_tuner     = vidioc_g_tuner,
521 	.vidioc_s_tuner     = vidioc_s_tuner,
522 	.vidioc_g_frequency = vidioc_g_frequency,
523 	.vidioc_s_frequency = vidioc_s_frequency,
524 	.vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
525 	.vidioc_enum_freq_bands = vidioc_enum_freq_bands,
526 	.vidioc_log_status  = v4l2_ctrl_log_status,
527 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
528 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
529 };
530 
531 static const struct video_device tea575x_radio = {
532 	.ioctl_ops	= &tea575x_ioctl_ops,
533 	.release	= video_device_release_empty,
534 };
535 
536 static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
537 	.s_ctrl = tea575x_s_ctrl,
538 };
539 
540 int radio_tea5777_init(struct radio_tea5777 *tea, struct module *owner)
541 {
542 	int res;
543 
544 	tea->write_reg = (1LL << TEA5777_W_IFCE_SHIFT) |
545 			 (1LL << TEA5777_W_IFW_SHIFT) |
546 			 (1LL << TEA5777_W_INTEXT_SHIFT) |
547 			 (1LL << TEA5777_W_CHP0_SHIFT) |
548 			 (1LL << TEA5777_W_SLEV_SHIFT);
549 	tea->freq = 90500 * 16;	/* 90.5Mhz default */
550 	tea->audmode = V4L2_TUNER_MODE_STEREO;
551 	res = radio_tea5777_set_freq(tea);
552 	if (res) {
553 		v4l2_err(tea->v4l2_dev, "can't set initial freq (%d)\n", res);
554 		return res;
555 	}
556 
557 	tea->vd = tea575x_radio;
558 	video_set_drvdata(&tea->vd, tea);
559 	mutex_init(&tea->mutex);
560 	strscpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
561 	tea->vd.lock = &tea->mutex;
562 	tea->vd.v4l2_dev = tea->v4l2_dev;
563 	tea->vd.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
564 			      V4L2_CAP_HW_FREQ_SEEK;
565 	tea->fops = tea575x_fops;
566 	tea->fops.owner = owner;
567 	tea->vd.fops = &tea->fops;
568 
569 	tea->vd.ctrl_handler = &tea->ctrl_handler;
570 	v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
571 	v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
572 			  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
573 	res = tea->ctrl_handler.error;
574 	if (res) {
575 		v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
576 		v4l2_ctrl_handler_free(&tea->ctrl_handler);
577 		return res;
578 	}
579 	v4l2_ctrl_handler_setup(&tea->ctrl_handler);
580 
581 	res = video_register_device(&tea->vd, VFL_TYPE_RADIO, -1);
582 	if (res) {
583 		v4l2_err(tea->v4l2_dev, "can't register video device!\n");
584 		v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
585 		return res;
586 	}
587 
588 	return 0;
589 }
590 EXPORT_SYMBOL_GPL(radio_tea5777_init);
591 
592 void radio_tea5777_exit(struct radio_tea5777 *tea)
593 {
594 	video_unregister_device(&tea->vd);
595 	v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
596 }
597 EXPORT_SYMBOL_GPL(radio_tea5777_exit);
598