Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips | |
3 | * | |
c1017a4c | 4 | * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz> |
1da177e4 LT |
5 | * |
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 | * | |
4b29631d | 17 | */ |
1da177e4 | 18 | |
1da177e4 | 19 | #include <linux/delay.h> |
da155d5b | 20 | #include <linux/module.h> |
1da177e4 | 21 | #include <linux/init.h> |
5a0e3ad6 | 22 | #include <linux/slab.h> |
d4ecc83b | 23 | #include <linux/sched.h> |
eab924d0 | 24 | #include <asm/io.h> |
d4ecc83b | 25 | #include <media/v4l2-device.h> |
4522e825 | 26 | #include <media/v4l2-dev.h> |
d4ecc83b | 27 | #include <media/v4l2-fh.h> |
4522e825 | 28 | #include <media/v4l2-ioctl.h> |
d4ecc83b | 29 | #include <media/v4l2-event.h> |
d647f0b7 | 30 | #include <media/drv-intf/tea575x.h> |
1da177e4 | 31 | |
c1017a4c | 32 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
1da177e4 LT |
33 | MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips"); |
34 | MODULE_LICENSE("GPL"); | |
35 | ||
36 | /* | |
37 | * definitions | |
38 | */ | |
39 | ||
40 | #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */ | |
41 | #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */ | |
42 | #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */ | |
43 | #define TEA575X_BIT_BAND_MASK (3<<20) | |
44 | #define TEA575X_BIT_BAND_FM (0<<20) | |
45 | #define TEA575X_BIT_BAND_MW (1<<20) | |
fc488517 HG |
46 | #define TEA575X_BIT_BAND_LW (2<<20) |
47 | #define TEA575X_BIT_BAND_SW (3<<20) | |
1da177e4 LT |
48 | #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */ |
49 | #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */ | |
50 | #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */ | |
51 | #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */ | |
52 | #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */ | |
53 | #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */ | |
54 | #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */ | |
55 | #define TEA575X_BIT_DUMMY (1<<15) /* buffer */ | |
56 | #define TEA575X_BIT_FREQ_MASK 0x7fff | |
57 | ||
fc488517 HG |
58 | enum { BAND_FM, BAND_FM_JAPAN, BAND_AM }; |
59 | ||
60 | static const struct v4l2_frequency_band bands[] = { | |
61 | { | |
62 | .type = V4L2_TUNER_RADIO, | |
63 | .index = 0, | |
64 | .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | | |
65 | V4L2_TUNER_CAP_FREQ_BANDS, | |
66 | .rangelow = 87500 * 16, | |
67 | .rangehigh = 108000 * 16, | |
68 | .modulation = V4L2_BAND_MODULATION_FM, | |
69 | }, | |
70 | { | |
71 | .type = V4L2_TUNER_RADIO, | |
72 | .index = 0, | |
73 | .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | | |
74 | V4L2_TUNER_CAP_FREQ_BANDS, | |
75 | .rangelow = 76000 * 16, | |
76 | .rangehigh = 91000 * 16, | |
77 | .modulation = V4L2_BAND_MODULATION_FM, | |
78 | }, | |
79 | { | |
80 | .type = V4L2_TUNER_RADIO, | |
81 | .index = 1, | |
82 | .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS, | |
83 | .rangelow = 530 * 16, | |
84 | .rangehigh = 1710 * 16, | |
85 | .modulation = V4L2_BAND_MODULATION_AM, | |
86 | }, | |
87 | }; | |
88 | ||
1da177e4 LT |
89 | /* |
90 | * lowlevel part | |
91 | */ | |
92 | ||
14219d06 OZ |
93 | static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val) |
94 | { | |
95 | u16 l; | |
96 | u8 data; | |
97 | ||
31a62d41 HG |
98 | if (tea->ops->write_val) |
99 | return tea->ops->write_val(tea, val); | |
100 | ||
14219d06 OZ |
101 | tea->ops->set_direction(tea, 1); |
102 | udelay(16); | |
103 | ||
104 | for (l = 25; l > 0; l--) { | |
105 | data = (val >> 24) & TEA575X_DATA; | |
106 | val <<= 1; /* shift data */ | |
107 | tea->ops->set_pins(tea, data | TEA575X_WREN); | |
108 | udelay(2); | |
109 | tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK); | |
110 | udelay(2); | |
111 | tea->ops->set_pins(tea, data | TEA575X_WREN); | |
112 | udelay(2); | |
113 | } | |
114 | ||
115 | if (!tea->mute) | |
116 | tea->ops->set_pins(tea, 0); | |
117 | } | |
118 | ||
bc2b395c | 119 | static u32 snd_tea575x_read(struct snd_tea575x *tea) |
14219d06 OZ |
120 | { |
121 | u16 l, rdata; | |
122 | u32 data = 0; | |
123 | ||
31a62d41 HG |
124 | if (tea->ops->read_val) |
125 | return tea->ops->read_val(tea); | |
126 | ||
14219d06 OZ |
127 | tea->ops->set_direction(tea, 0); |
128 | tea->ops->set_pins(tea, 0); | |
129 | udelay(16); | |
130 | ||
131 | for (l = 24; l--;) { | |
132 | tea->ops->set_pins(tea, TEA575X_CLK); | |
133 | udelay(2); | |
134 | if (!l) | |
135 | tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1; | |
136 | tea->ops->set_pins(tea, 0); | |
137 | udelay(2); | |
138 | data <<= 1; /* shift data */ | |
139 | rdata = tea->ops->get_pins(tea); | |
140 | if (!l) | |
141 | tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1; | |
142 | if (rdata & TEA575X_DATA) | |
143 | data++; | |
144 | udelay(2); | |
145 | } | |
146 | ||
147 | if (tea->mute) | |
148 | tea->ops->set_pins(tea, TEA575X_WREN); | |
149 | ||
150 | return data; | |
151 | } | |
152 | ||
40e006ae | 153 | static u32 snd_tea575x_val_to_freq(struct snd_tea575x *tea, u32 val) |
bc2b395c | 154 | { |
40e006ae | 155 | u32 freq = val & TEA575X_BIT_FREQ_MASK; |
bc2b395c HV |
156 | |
157 | if (freq == 0) | |
158 | return freq; | |
159 | ||
fc488517 HG |
160 | switch (tea->band) { |
161 | case BAND_FM: | |
162 | /* freq *= 12.5 */ | |
163 | freq *= 125; | |
164 | freq /= 10; | |
165 | /* crystal fixup */ | |
bc2b395c | 166 | freq -= TEA575X_FMIF; |
fc488517 HG |
167 | break; |
168 | case BAND_FM_JAPAN: | |
169 | /* freq *= 12.5 */ | |
170 | freq *= 125; | |
171 | freq /= 10; | |
172 | /* crystal fixup */ | |
173 | freq += TEA575X_FMIF; | |
174 | break; | |
175 | case BAND_AM: | |
176 | /* crystal fixup */ | |
177 | freq -= TEA575X_AMIF; | |
178 | break; | |
179 | } | |
bc2b395c | 180 | |
fc488517 HG |
181 | return clamp(freq * 16, bands[tea->band].rangelow, |
182 | bands[tea->band].rangehigh); /* from kHz */ | |
bc2b395c HV |
183 | } |
184 | ||
40e006ae HG |
185 | static u32 snd_tea575x_get_freq(struct snd_tea575x *tea) |
186 | { | |
187 | return snd_tea575x_val_to_freq(tea, snd_tea575x_read(tea)); | |
188 | } | |
189 | ||
559c2009 | 190 | void snd_tea575x_set_freq(struct snd_tea575x *tea) |
1da177e4 | 191 | { |
fc488517 HG |
192 | u32 freq = tea->freq / 16; /* to kHz */ |
193 | u32 band = 0; | |
1da177e4 | 194 | |
fc488517 HG |
195 | switch (tea->band) { |
196 | case BAND_FM: | |
197 | band = TEA575X_BIT_BAND_FM; | |
198 | /* crystal fixup */ | |
ea27316e | 199 | freq += TEA575X_FMIF; |
fc488517 HG |
200 | /* freq /= 12.5 */ |
201 | freq *= 10; | |
202 | freq /= 125; | |
203 | break; | |
204 | case BAND_FM_JAPAN: | |
205 | band = TEA575X_BIT_BAND_FM; | |
206 | /* crystal fixup */ | |
207 | freq -= TEA575X_FMIF; | |
208 | /* freq /= 12.5 */ | |
209 | freq *= 10; | |
210 | freq /= 125; | |
211 | break; | |
212 | case BAND_AM: | |
213 | band = TEA575X_BIT_BAND_MW; | |
214 | /* crystal fixup */ | |
215 | freq += TEA575X_AMIF; | |
216 | break; | |
217 | } | |
1da177e4 | 218 | |
fc488517 HG |
219 | tea->val &= ~(TEA575X_BIT_FREQ_MASK | TEA575X_BIT_BAND_MASK); |
220 | tea->val |= band; | |
1da177e4 | 221 | tea->val |= freq & TEA575X_BIT_FREQ_MASK; |
14219d06 | 222 | snd_tea575x_write(tea, tea->val); |
40e006ae | 223 | tea->freq = snd_tea575x_val_to_freq(tea, tea->val); |
1da177e4 | 224 | } |
0e2a706b | 225 | EXPORT_SYMBOL(snd_tea575x_set_freq); |
1da177e4 LT |
226 | |
227 | /* | |
228 | * Linux Video interface | |
229 | */ | |
230 | ||
9b76ede4 MCC |
231 | static int vidioc_querycap(struct file *file, void *priv, |
232 | struct v4l2_capability *v) | |
1da177e4 | 233 | { |
c170ecf4 | 234 | struct snd_tea575x *tea = video_drvdata(file); |
9b76ede4 | 235 | |
d4ecc83b | 236 | strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver)); |
10ca7201 OZ |
237 | strlcpy(v->card, tea->card, sizeof(v->card)); |
238 | strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card)); | |
239 | strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info)); | |
d4ecc83b HV |
240 | v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
241 | if (!tea->cannot_read_data) | |
242 | v->device_caps |= V4L2_CAP_HW_FREQ_SEEK; | |
243 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; | |
9b76ede4 MCC |
244 | return 0; |
245 | } | |
246 | ||
6994ca3d OZ |
247 | int snd_tea575x_enum_freq_bands(struct snd_tea575x *tea, |
248 | struct v4l2_frequency_band *band) | |
fc488517 | 249 | { |
fc488517 HG |
250 | int index; |
251 | ||
252 | if (band->tuner != 0) | |
253 | return -EINVAL; | |
254 | ||
255 | switch (band->index) { | |
256 | case 0: | |
257 | if (tea->tea5759) | |
258 | index = BAND_FM_JAPAN; | |
259 | else | |
260 | index = BAND_FM; | |
261 | break; | |
262 | case 1: | |
263 | if (tea->has_am) { | |
264 | index = BAND_AM; | |
265 | break; | |
266 | } | |
267 | /* Fall through */ | |
268 | default: | |
269 | return -EINVAL; | |
270 | } | |
271 | ||
272 | *band = bands[index]; | |
273 | if (!tea->cannot_read_data) | |
274 | band->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED; | |
275 | ||
276 | return 0; | |
277 | } | |
6994ca3d | 278 | EXPORT_SYMBOL(snd_tea575x_enum_freq_bands); |
fc488517 | 279 | |
6994ca3d OZ |
280 | static int vidioc_enum_freq_bands(struct file *file, void *priv, |
281 | struct v4l2_frequency_band *band) | |
9b76ede4 | 282 | { |
375d1358 | 283 | struct snd_tea575x *tea = video_drvdata(file); |
6994ca3d OZ |
284 | |
285 | return snd_tea575x_enum_freq_bands(tea, band); | |
286 | } | |
287 | ||
288 | int snd_tea575x_g_tuner(struct snd_tea575x *tea, struct v4l2_tuner *v) | |
289 | { | |
fc488517 | 290 | struct v4l2_frequency_band band_fm = { 0, }; |
375d1358 | 291 | |
9b76ede4 MCC |
292 | if (v->index > 0) |
293 | return -EINVAL; | |
294 | ||
14219d06 | 295 | snd_tea575x_read(tea); |
6994ca3d | 296 | snd_tea575x_enum_freq_bands(tea, &band_fm); |
375d1358 | 297 | |
fc488517 HG |
298 | memset(v, 0, sizeof(*v)); |
299 | strlcpy(v->name, tea->has_am ? "FM/AM" : "FM", sizeof(v->name)); | |
9b76ede4 | 300 | v->type = V4L2_TUNER_RADIO; |
fc488517 HG |
301 | v->capability = band_fm.capability; |
302 | v->rangelow = tea->has_am ? bands[BAND_AM].rangelow : band_fm.rangelow; | |
303 | v->rangehigh = band_fm.rangehigh; | |
d4ecc83b HV |
304 | v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; |
305 | v->audmode = (tea->val & TEA575X_BIT_MONO) ? | |
306 | V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO; | |
375d1358 | 307 | v->signal = tea->tuned ? 0xffff : 0; |
9b76ede4 MCC |
308 | return 0; |
309 | } | |
6994ca3d OZ |
310 | EXPORT_SYMBOL(snd_tea575x_g_tuner); |
311 | ||
312 | static int vidioc_g_tuner(struct file *file, void *priv, | |
313 | struct v4l2_tuner *v) | |
314 | { | |
315 | struct snd_tea575x *tea = video_drvdata(file); | |
316 | ||
317 | return snd_tea575x_g_tuner(tea, v); | |
318 | } | |
9b76ede4 MCC |
319 | |
320 | static int vidioc_s_tuner(struct file *file, void *priv, | |
2f73c7c5 | 321 | const struct v4l2_tuner *v) |
9b76ede4 | 322 | { |
d4ecc83b | 323 | struct snd_tea575x *tea = video_drvdata(file); |
fc488517 | 324 | u32 orig_val = tea->val; |
d4ecc83b HV |
325 | |
326 | if (v->index) | |
9b76ede4 | 327 | return -EINVAL; |
d4ecc83b HV |
328 | tea->val &= ~TEA575X_BIT_MONO; |
329 | if (v->audmode == V4L2_TUNER_MODE_MONO) | |
330 | tea->val |= TEA575X_BIT_MONO; | |
fc488517 HG |
331 | /* Only apply changes if currently tuning FM */ |
332 | if (tea->band != BAND_AM && tea->val != orig_val) | |
333 | snd_tea575x_set_freq(tea); | |
334 | ||
9b76ede4 MCC |
335 | return 0; |
336 | } | |
337 | ||
338 | static int vidioc_g_frequency(struct file *file, void *priv, | |
339 | struct v4l2_frequency *f) | |
340 | { | |
341 | struct snd_tea575x *tea = video_drvdata(file); | |
342 | ||
375d1358 OZ |
343 | if (f->tuner != 0) |
344 | return -EINVAL; | |
9b76ede4 MCC |
345 | f->type = V4L2_TUNER_RADIO; |
346 | f->frequency = tea->freq; | |
347 | return 0; | |
348 | } | |
349 | ||
350 | static int vidioc_s_frequency(struct file *file, void *priv, | |
b530a447 | 351 | const struct v4l2_frequency *f) |
9b76ede4 MCC |
352 | { |
353 | struct snd_tea575x *tea = video_drvdata(file); | |
354 | ||
375d1358 OZ |
355 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
356 | return -EINVAL; | |
357 | ||
fc488517 HG |
358 | if (tea->has_am && f->frequency < (20000 * 16)) |
359 | tea->band = BAND_AM; | |
360 | else if (tea->tea5759) | |
361 | tea->band = BAND_FM_JAPAN; | |
362 | else | |
363 | tea->band = BAND_FM; | |
364 | ||
b530a447 | 365 | tea->freq = clamp_t(u32, f->frequency, bands[tea->band].rangelow, |
fc488517 | 366 | bands[tea->band].rangehigh); |
9b76ede4 | 367 | snd_tea575x_set_freq(tea); |
9b76ede4 MCC |
368 | return 0; |
369 | } | |
370 | ||
6994ca3d OZ |
371 | int snd_tea575x_s_hw_freq_seek(struct file *file, struct snd_tea575x *tea, |
372 | const struct v4l2_hw_freq_seek *a) | |
9b76ede4 | 373 | { |
bc2b395c | 374 | unsigned long timeout; |
fc488517 | 375 | int i, spacing; |
9b76ede4 | 376 | |
d4ecc83b HV |
377 | if (tea->cannot_read_data) |
378 | return -ENOTTY; | |
379 | if (a->tuner || a->wrap_around) | |
9b76ede4 | 380 | return -EINVAL; |
bc2b395c | 381 | |
617ade61 HV |
382 | if (file->f_flags & O_NONBLOCK) |
383 | return -EWOULDBLOCK; | |
384 | ||
fc488517 HG |
385 | if (a->rangelow || a->rangehigh) { |
386 | for (i = 0; i < ARRAY_SIZE(bands); i++) { | |
387 | if ((i == BAND_FM && tea->tea5759) || | |
388 | (i == BAND_FM_JAPAN && !tea->tea5759) || | |
389 | (i == BAND_AM && !tea->has_am)) | |
390 | continue; | |
391 | if (bands[i].rangelow == a->rangelow && | |
392 | bands[i].rangehigh == a->rangehigh) | |
393 | break; | |
394 | } | |
395 | if (i == ARRAY_SIZE(bands)) | |
396 | return -EINVAL; /* No matching band found */ | |
397 | if (i != tea->band) { | |
398 | tea->band = i; | |
399 | tea->freq = clamp(tea->freq, bands[i].rangelow, | |
400 | bands[i].rangehigh); | |
401 | snd_tea575x_set_freq(tea); | |
402 | } | |
403 | } | |
404 | ||
405 | spacing = (tea->band == BAND_AM) ? 5 : 50; /* kHz */ | |
406 | ||
bc2b395c HV |
407 | /* clear the frequency, HW will fill it in */ |
408 | tea->val &= ~TEA575X_BIT_FREQ_MASK; | |
d4ecc83b | 409 | tea->val |= TEA575X_BIT_SEARCH; |
d4ecc83b HV |
410 | if (a->seek_upward) |
411 | tea->val |= TEA575X_BIT_UPDOWN; | |
bc2b395c HV |
412 | else |
413 | tea->val &= ~TEA575X_BIT_UPDOWN; | |
d4ecc83b | 414 | snd_tea575x_write(tea, tea->val); |
bc2b395c | 415 | timeout = jiffies + msecs_to_jiffies(10000); |
d4ecc83b | 416 | for (;;) { |
bc2b395c HV |
417 | if (time_after(jiffies, timeout)) |
418 | break; | |
d4ecc83b HV |
419 | if (schedule_timeout_interruptible(msecs_to_jiffies(10))) { |
420 | /* some signal arrived, stop search */ | |
421 | tea->val &= ~TEA575X_BIT_SEARCH; | |
bc2b395c | 422 | snd_tea575x_set_freq(tea); |
d4ecc83b HV |
423 | return -ERESTARTSYS; |
424 | } | |
bc2b395c HV |
425 | if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) { |
426 | u32 freq; | |
427 | ||
428 | /* Found a frequency, wait until it can be read */ | |
429 | for (i = 0; i < 100; i++) { | |
430 | msleep(10); | |
431 | freq = snd_tea575x_get_freq(tea); | |
432 | if (freq) /* available */ | |
433 | break; | |
434 | } | |
435 | if (freq == 0) /* shouldn't happen */ | |
436 | break; | |
437 | /* | |
fc488517 HG |
438 | * if we moved by less than the spacing, or in the |
439 | * wrong direction, continue seeking | |
bc2b395c | 440 | */ |
fc488517 | 441 | if (abs(tea->freq - freq) < 16 * spacing || |
bc2b395c HV |
442 | (a->seek_upward && freq < tea->freq) || |
443 | (!a->seek_upward && freq > tea->freq)) { | |
444 | snd_tea575x_write(tea, tea->val); | |
445 | continue; | |
446 | } | |
447 | tea->freq = freq; | |
448 | tea->val &= ~TEA575X_BIT_SEARCH; | |
449 | return 0; | |
450 | } | |
d4ecc83b | 451 | } |
bc2b395c HV |
452 | tea->val &= ~TEA575X_BIT_SEARCH; |
453 | snd_tea575x_set_freq(tea); | |
54f6019b | 454 | return -ENODATA; |
9b76ede4 | 455 | } |
6994ca3d OZ |
456 | EXPORT_SYMBOL(snd_tea575x_s_hw_freq_seek); |
457 | ||
458 | static int vidioc_s_hw_freq_seek(struct file *file, void *fh, | |
459 | const struct v4l2_hw_freq_seek *a) | |
460 | { | |
461 | struct snd_tea575x *tea = video_drvdata(file); | |
462 | ||
463 | return snd_tea575x_s_hw_freq_seek(file, tea, a); | |
464 | } | |
9b76ede4 | 465 | |
4522e825 | 466 | static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl) |
9b76ede4 | 467 | { |
4522e825 | 468 | struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler); |
9b76ede4 MCC |
469 | |
470 | switch (ctrl->id) { | |
471 | case V4L2_CID_AUDIO_MUTE: | |
4522e825 OZ |
472 | tea->mute = ctrl->val; |
473 | snd_tea575x_set_freq(tea); | |
14219d06 | 474 | return 0; |
9b76ede4 | 475 | } |
9b76ede4 | 476 | |
9b76ede4 MCC |
477 | return -EINVAL; |
478 | } | |
479 | ||
9b76ede4 | 480 | static const struct v4l2_file_operations tea575x_fops = { |
6a529c1a | 481 | .unlocked_ioctl = video_ioctl2, |
d4ecc83b HV |
482 | .open = v4l2_fh_open, |
483 | .release = v4l2_fh_release, | |
484 | .poll = v4l2_ctrl_poll, | |
9b76ede4 MCC |
485 | }; |
486 | ||
487 | static const struct v4l2_ioctl_ops tea575x_ioctl_ops = { | |
488 | .vidioc_querycap = vidioc_querycap, | |
489 | .vidioc_g_tuner = vidioc_g_tuner, | |
490 | .vidioc_s_tuner = vidioc_s_tuner, | |
9b76ede4 MCC |
491 | .vidioc_g_frequency = vidioc_g_frequency, |
492 | .vidioc_s_frequency = vidioc_s_frequency, | |
d4ecc83b | 493 | .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek, |
fc488517 | 494 | .vidioc_enum_freq_bands = vidioc_enum_freq_bands, |
d4ecc83b HV |
495 | .vidioc_log_status = v4l2_ctrl_log_status, |
496 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
497 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
9b76ede4 MCC |
498 | }; |
499 | ||
d4ecc83b | 500 | static const struct video_device tea575x_radio = { |
9b76ede4 | 501 | .ioctl_ops = &tea575x_ioctl_ops, |
d4ecc83b | 502 | .release = video_device_release_empty, |
4522e825 OZ |
503 | }; |
504 | ||
505 | static const struct v4l2_ctrl_ops tea575x_ctrl_ops = { | |
506 | .s_ctrl = tea575x_s_ctrl, | |
9b76ede4 MCC |
507 | }; |
508 | ||
1da177e4 | 509 | |
8fd79579 OZ |
510 | int snd_tea575x_hw_init(struct snd_tea575x *tea) |
511 | { | |
d4ecc83b | 512 | tea->mute = true; |
14219d06 | 513 | |
d4ecc83b HV |
514 | /* Not all devices can or know how to read the data back. |
515 | Such devices can set cannot_read_data to true. */ | |
516 | if (!tea->cannot_read_data) { | |
517 | snd_tea575x_write(tea, 0x55AA); | |
518 | if (snd_tea575x_read(tea) != 0x55AA) | |
519 | return -ENODEV; | |
520 | } | |
1da177e4 | 521 | |
bc2b395c | 522 | tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28; |
9b76ede4 | 523 | tea->freq = 90500 * 16; /* 90.5Mhz default */ |
4522e825 | 524 | snd_tea575x_set_freq(tea); |
9b76ede4 | 525 | |
8fd79579 OZ |
526 | return 0; |
527 | } | |
528 | EXPORT_SYMBOL(snd_tea575x_hw_init); | |
529 | ||
530 | int snd_tea575x_init(struct snd_tea575x *tea, struct module *owner) | |
531 | { | |
532 | int retval = snd_tea575x_hw_init(tea); | |
533 | ||
534 | if (retval) | |
535 | return retval; | |
536 | ||
4522e825 OZ |
537 | tea->vd = tea575x_radio; |
538 | video_set_drvdata(&tea->vd, tea); | |
6a529c1a | 539 | mutex_init(&tea->mutex); |
d4ecc83b | 540 | strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name)); |
6a529c1a | 541 | tea->vd.lock = &tea->mutex; |
d4ecc83b | 542 | tea->vd.v4l2_dev = tea->v4l2_dev; |
5daf53a6 HG |
543 | tea->fops = tea575x_fops; |
544 | tea->fops.owner = owner; | |
545 | tea->vd.fops = &tea->fops; | |
65397995 HV |
546 | /* disable hw_freq_seek if we can't use it */ |
547 | if (tea->cannot_read_data) | |
152a3a73 | 548 | v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK); |
1da177e4 | 549 | |
3d0fe51c HG |
550 | if (!tea->cannot_mute) { |
551 | tea->vd.ctrl_handler = &tea->ctrl_handler; | |
552 | v4l2_ctrl_handler_init(&tea->ctrl_handler, 1); | |
553 | v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, | |
554 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); | |
555 | retval = tea->ctrl_handler.error; | |
4522e825 | 556 | if (retval) { |
3d0fe51c | 557 | v4l2_err(tea->v4l2_dev, "can't initialize controls\n"); |
4522e825 OZ |
558 | v4l2_ctrl_handler_free(&tea->ctrl_handler); |
559 | return retval; | |
560 | } | |
9b76ede4 | 561 | |
3d0fe51c HG |
562 | if (tea->ext_init) { |
563 | retval = tea->ext_init(tea); | |
564 | if (retval) { | |
565 | v4l2_ctrl_handler_free(&tea->ctrl_handler); | |
566 | return retval; | |
567 | } | |
568 | } | |
569 | ||
570 | v4l2_ctrl_handler_setup(&tea->ctrl_handler); | |
571 | } | |
9b76ede4 | 572 | |
d4ecc83b | 573 | retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr); |
9b76ede4 | 574 | if (retval) { |
d4ecc83b | 575 | v4l2_err(tea->v4l2_dev, "can't register video device!\n"); |
3d0fe51c | 576 | v4l2_ctrl_handler_free(tea->vd.ctrl_handler); |
14219d06 | 577 | return retval; |
9b76ede4 | 578 | } |
1da177e4 | 579 | |
14219d06 | 580 | return 0; |
1da177e4 | 581 | } |
0e2a706b | 582 | EXPORT_SYMBOL(snd_tea575x_init); |
1da177e4 | 583 | |
97f02e05 | 584 | void snd_tea575x_exit(struct snd_tea575x *tea) |
1da177e4 | 585 | { |
4522e825 | 586 | video_unregister_device(&tea->vd); |
3d0fe51c | 587 | v4l2_ctrl_handler_free(tea->vd.ctrl_handler); |
1da177e4 | 588 | } |
1da177e4 | 589 | EXPORT_SYMBOL(snd_tea575x_exit); |