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