1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <asm/io.h>
24#include <linux/delay.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/sched.h>
29#include <media/v4l2-device.h>
30#include <media/v4l2-dev.h>
31#include <media/v4l2-fh.h>
32#include <media/v4l2-ioctl.h>
33#include <media/v4l2-event.h>
34#include <sound/tea575x-tuner.h>
35
36MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
37MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
38MODULE_LICENSE("GPL");
39
40#define FREQ_LO (76U * 16000)
41#define FREQ_HI (108U * 16000)
42
43
44
45
46
47#define TEA575X_BIT_SEARCH (1<<24)
48#define TEA575X_BIT_UPDOWN (1<<23)
49#define TEA575X_BIT_MONO (1<<22)
50#define TEA575X_BIT_BAND_MASK (3<<20)
51#define TEA575X_BIT_BAND_FM (0<<20)
52#define TEA575X_BIT_BAND_MW (1<<20)
53#define TEA575X_BIT_BAND_LW (1<<21)
54#define TEA575X_BIT_BAND_SW (1<<22)
55#define TEA575X_BIT_PORT_0 (1<<19)
56#define TEA575X_BIT_PORT_1 (1<<18)
57#define TEA575X_BIT_SEARCH_MASK (3<<16)
58#define TEA575X_BIT_SEARCH_5_28 (0<<16)
59#define TEA575X_BIT_SEARCH_10_40 (1<<16)
60#define TEA575X_BIT_SEARCH_30_63 (2<<16)
61#define TEA575X_BIT_SEARCH_150_1000 (3<<16)
62#define TEA575X_BIT_DUMMY (1<<15)
63#define TEA575X_BIT_FREQ_MASK 0x7fff
64
65
66
67
68
69static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
70{
71 u16 l;
72 u8 data;
73
74 tea->ops->set_direction(tea, 1);
75 udelay(16);
76
77 for (l = 25; l > 0; l--) {
78 data = (val >> 24) & TEA575X_DATA;
79 val <<= 1;
80 tea->ops->set_pins(tea, data | TEA575X_WREN);
81 udelay(2);
82 tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
83 udelay(2);
84 tea->ops->set_pins(tea, data | TEA575X_WREN);
85 udelay(2);
86 }
87
88 if (!tea->mute)
89 tea->ops->set_pins(tea, 0);
90}
91
92static u32 snd_tea575x_read(struct snd_tea575x *tea)
93{
94 u16 l, rdata;
95 u32 data = 0;
96
97 tea->ops->set_direction(tea, 0);
98 tea->ops->set_pins(tea, 0);
99 udelay(16);
100
101 for (l = 24; l--;) {
102 tea->ops->set_pins(tea, TEA575X_CLK);
103 udelay(2);
104 if (!l)
105 tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
106 tea->ops->set_pins(tea, 0);
107 udelay(2);
108 data <<= 1;
109 rdata = tea->ops->get_pins(tea);
110 if (!l)
111 tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
112 if (rdata & TEA575X_DATA)
113 data++;
114 udelay(2);
115 }
116
117 if (tea->mute)
118 tea->ops->set_pins(tea, TEA575X_WREN);
119
120 return data;
121}
122
123static u32 snd_tea575x_get_freq(struct snd_tea575x *tea)
124{
125 u32 freq = snd_tea575x_read(tea) & TEA575X_BIT_FREQ_MASK;
126
127 if (freq == 0)
128 return freq;
129
130
131 freq *= 125;
132 freq /= 10;
133
134 if (tea->tea5759)
135 freq += TEA575X_FMIF;
136 else
137 freq -= TEA575X_FMIF;
138
139 return clamp(freq * 16, FREQ_LO, FREQ_HI);
140}
141
142static void snd_tea575x_set_freq(struct snd_tea575x *tea)
143{
144 u32 freq = tea->freq;
145
146 freq /= 16;
147
148 if (tea->tea5759)
149 freq -= TEA575X_FMIF;
150 else
151 freq += TEA575X_FMIF;
152
153 freq *= 10;
154 freq /= 125;
155
156 tea->val &= ~TEA575X_BIT_FREQ_MASK;
157 tea->val |= freq & TEA575X_BIT_FREQ_MASK;
158 snd_tea575x_write(tea, tea->val);
159}
160
161
162
163
164
165static int vidioc_querycap(struct file *file, void *priv,
166 struct v4l2_capability *v)
167{
168 struct snd_tea575x *tea = video_drvdata(file);
169
170 strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
171 strlcpy(v->card, tea->card, sizeof(v->card));
172 strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
173 strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
174 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
175 if (!tea->cannot_read_data)
176 v->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
177 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
178 return 0;
179}
180
181static int vidioc_g_tuner(struct file *file, void *priv,
182 struct v4l2_tuner *v)
183{
184 struct snd_tea575x *tea = video_drvdata(file);
185
186 if (v->index > 0)
187 return -EINVAL;
188
189 snd_tea575x_read(tea);
190
191 strcpy(v->name, "FM");
192 v->type = V4L2_TUNER_RADIO;
193 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
194 v->rangelow = FREQ_LO;
195 v->rangehigh = FREQ_HI;
196 v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
197 v->audmode = (tea->val & TEA575X_BIT_MONO) ?
198 V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
199 v->signal = tea->tuned ? 0xffff : 0;
200 return 0;
201}
202
203static int vidioc_s_tuner(struct file *file, void *priv,
204 struct v4l2_tuner *v)
205{
206 struct snd_tea575x *tea = video_drvdata(file);
207
208 if (v->index)
209 return -EINVAL;
210 tea->val &= ~TEA575X_BIT_MONO;
211 if (v->audmode == V4L2_TUNER_MODE_MONO)
212 tea->val |= TEA575X_BIT_MONO;
213 snd_tea575x_write(tea, tea->val);
214 return 0;
215}
216
217static int vidioc_g_frequency(struct file *file, void *priv,
218 struct v4l2_frequency *f)
219{
220 struct snd_tea575x *tea = video_drvdata(file);
221
222 if (f->tuner != 0)
223 return -EINVAL;
224 f->type = V4L2_TUNER_RADIO;
225 f->frequency = tea->freq;
226 return 0;
227}
228
229static int vidioc_s_frequency(struct file *file, void *priv,
230 struct v4l2_frequency *f)
231{
232 struct snd_tea575x *tea = video_drvdata(file);
233
234 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
235 return -EINVAL;
236
237 tea->val &= ~TEA575X_BIT_SEARCH;
238 tea->freq = clamp(f->frequency, FREQ_LO, FREQ_HI);
239 snd_tea575x_set_freq(tea);
240 return 0;
241}
242
243static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
244 struct v4l2_hw_freq_seek *a)
245{
246 struct snd_tea575x *tea = video_drvdata(file);
247 unsigned long timeout;
248 int i;
249
250 if (tea->cannot_read_data)
251 return -ENOTTY;
252 if (a->tuner || a->wrap_around)
253 return -EINVAL;
254
255
256 tea->val &= ~TEA575X_BIT_FREQ_MASK;
257 tea->val |= TEA575X_BIT_SEARCH;
258 if (a->seek_upward)
259 tea->val |= TEA575X_BIT_UPDOWN;
260 else
261 tea->val &= ~TEA575X_BIT_UPDOWN;
262 snd_tea575x_write(tea, tea->val);
263 timeout = jiffies + msecs_to_jiffies(10000);
264 for (;;) {
265 if (time_after(jiffies, timeout))
266 break;
267 if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
268
269 tea->val &= ~TEA575X_BIT_SEARCH;
270 snd_tea575x_set_freq(tea);
271 return -ERESTARTSYS;
272 }
273 if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) {
274 u32 freq;
275
276
277 for (i = 0; i < 100; i++) {
278 msleep(10);
279 freq = snd_tea575x_get_freq(tea);
280 if (freq)
281 break;
282 }
283 if (freq == 0)
284 break;
285
286
287
288
289 if (abs(tea->freq - freq) < 16 * 50 ||
290 (a->seek_upward && freq < tea->freq) ||
291 (!a->seek_upward && freq > tea->freq)) {
292 snd_tea575x_write(tea, tea->val);
293 continue;
294 }
295 tea->freq = freq;
296 tea->val &= ~TEA575X_BIT_SEARCH;
297 return 0;
298 }
299 }
300 tea->val &= ~TEA575X_BIT_SEARCH;
301 snd_tea575x_set_freq(tea);
302 return -EAGAIN;
303}
304
305static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
306{
307 struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
308
309 switch (ctrl->id) {
310 case V4L2_CID_AUDIO_MUTE:
311 tea->mute = ctrl->val;
312 snd_tea575x_set_freq(tea);
313 return 0;
314 }
315
316 return -EINVAL;
317}
318
319static const struct v4l2_file_operations tea575x_fops = {
320 .owner = THIS_MODULE,
321 .unlocked_ioctl = video_ioctl2,
322 .open = v4l2_fh_open,
323 .release = v4l2_fh_release,
324 .poll = v4l2_ctrl_poll,
325};
326
327static const struct v4l2_ioctl_ops tea575x_ioctl_ops = {
328 .vidioc_querycap = vidioc_querycap,
329 .vidioc_g_tuner = vidioc_g_tuner,
330 .vidioc_s_tuner = vidioc_s_tuner,
331 .vidioc_g_frequency = vidioc_g_frequency,
332 .vidioc_s_frequency = vidioc_s_frequency,
333 .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
334 .vidioc_log_status = v4l2_ctrl_log_status,
335 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
336 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
337};
338
339static const struct video_device tea575x_radio = {
340 .fops = &tea575x_fops,
341 .ioctl_ops = &tea575x_ioctl_ops,
342 .release = video_device_release_empty,
343};
344
345static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
346 .s_ctrl = tea575x_s_ctrl,
347};
348
349
350
351
352int snd_tea575x_init(struct snd_tea575x *tea)
353{
354 int retval;
355
356 tea->mute = true;
357
358
359
360 if (!tea->cannot_read_data) {
361 snd_tea575x_write(tea, 0x55AA);
362 if (snd_tea575x_read(tea) != 0x55AA)
363 return -ENODEV;
364 }
365
366 tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28;
367 tea->freq = 90500 * 16;
368 snd_tea575x_set_freq(tea);
369
370 tea->vd = tea575x_radio;
371 video_set_drvdata(&tea->vd, tea);
372 mutex_init(&tea->mutex);
373 strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
374 tea->vd.lock = &tea->mutex;
375 tea->vd.v4l2_dev = tea->v4l2_dev;
376 tea->vd.ctrl_handler = &tea->ctrl_handler;
377 set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags);
378
379 v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
380 v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
381 retval = tea->ctrl_handler.error;
382 if (retval) {
383 v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
384 v4l2_ctrl_handler_free(&tea->ctrl_handler);
385 return retval;
386 }
387
388 if (tea->ext_init) {
389 retval = tea->ext_init(tea);
390 if (retval) {
391 v4l2_ctrl_handler_free(&tea->ctrl_handler);
392 return retval;
393 }
394 }
395
396 v4l2_ctrl_handler_setup(&tea->ctrl_handler);
397
398 retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
399 if (retval) {
400 v4l2_err(tea->v4l2_dev, "can't register video device!\n");
401 v4l2_ctrl_handler_free(&tea->ctrl_handler);
402 return retval;
403 }
404
405 return 0;
406}
407
408void snd_tea575x_exit(struct snd_tea575x *tea)
409{
410 video_unregister_device(&tea->vd);
411 v4l2_ctrl_handler_free(&tea->ctrl_handler);
412}
413
414static int __init alsa_tea575x_module_init(void)
415{
416 return 0;
417}
418
419static void __exit alsa_tea575x_module_exit(void)
420{
421}
422
423module_init(alsa_tea575x_module_init)
424module_exit(alsa_tea575x_module_exit)
425
426EXPORT_SYMBOL(snd_tea575x_init);
427EXPORT_SYMBOL(snd_tea575x_exit);
428