forked from Piro77/fbsdpt3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt3_qm.c
581 lines (487 loc) · 12.2 KB
/
pt3_qm.c
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
43
44
45
46
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*******************************************************************************
earthsoft PT3 Linux driver
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 3, as published by the Free Software Foundation.
This program is distributed in the hope 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.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
*******************************************************************************/
#if defined(__FreeBSD__)
#include "pt3_misc.h"
#else
#include "version.h"
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/version.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/time.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
#include <asm/system.h>
#endif
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include "pt3_com.h"
#include "pt3_pci.h"
#include "pt3_qm.h"
#define INIT_DUMMY_RESET 0x0c
#endif
/* TUNER_S */
void
pt3_qm_get_channel_freq(__u32 channel, int *bs, __u32 *number, __u32 *freq)
{
if (channel < 12) {
*bs = 1;
*number = 1 + 2 * channel;
*freq = 104948 + 3836 * channel;
} else if (channel < 24) {
channel -= 12;
*bs = 0;
*number = 2 + 2 * channel;
*freq = 161300 + 4000 * channel;
} else {
channel -= 24;
*bs = 0;
*number = 1 + 2 * channel;
*freq = 159300 + 4000 * channel;
}
}
/* QM */
static __u8 rw_reg[0x20] = {
0x48, 0x1c, 0xa0, 0x10, 0xbc, 0xc5, 0x20, 0x33,
0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0xff, 0xf3, 0x00, 0x2a, 0x64, 0xa6, 0x86,
0x8c, 0xcf, 0xb8, 0xf1, 0xa8, 0xf2, 0x89, 0x00,
};
static __u8 flag[0x20] = {
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
static STATUS
qm_write(PT3_QM *qm, PT3_BUS *bus, __u8 addr, __u8 data)
{
STATUS ret;
ret = pt3_tc_write_tuner(qm->tc, bus, addr, &data, sizeof(data));
qm->reg[addr] = data;
return ret;
}
static STATUS
qm_read(PT3_QM *qm, PT3_BUS *bus, __u8 addr, __u8 *data)
{
STATUS status;
if ((addr == 0x00 ) || (addr == 0x0d)) {
status = pt3_tc_read_tuner(qm->tc, bus, addr, data, 1);
#if 0
if (!bus)
PT3_PRINTK(7, KERN_DEBUG "qm_read addr=0x%02x data=0x%02x\n", addr, *data);
#endif
} else
status = STATUS_OK;
return status;
}
static void
qm_sleep(PT3_QM *qm, PT3_BUS *bus, __u32 ms)
{
if (bus)
pt3_bus_sleep(bus, ms);
else
schedule_timeout_interruptible(msecs_to_jiffies(ms));
}
static STATUS
qm_set_sleep_mode(PT3_QM *qm, PT3_BUS *bus)
{
STATUS status;
PT3_QM_PARAM *param;
param = &qm->param;
if (param->standby) {
qm->reg[0x01] &= (~(1 << 3)) & 0xff;
qm->reg[0x01] |= 1 << 0;
qm->reg[0x05] |= 1 << 3;
status = qm_write(qm, bus, 0x05, qm->reg[0x05]);
if (status)
return status;
status = qm_write(qm, bus, 0x01, qm->reg[0x01]);
if (status)
return status;
} else {
qm->reg[0x01] |= 1 <<3;
qm->reg[0x01] &= (~(1 << 0)) & 0xff;
qm->reg[0x05] &= (~(1 << 3)) & 0xff;
status = qm_write(qm, bus, 0x01, qm->reg[0x01]);
if (status)
return status;
status = qm_write(qm, bus, 0x05, qm->reg[0x05]);
if (status)
return status;
}
return status;
}
static STATUS
qm_set_search_mode(PT3_QM *qm, PT3_BUS *bus)
{
STATUS status;
PT3_QM_PARAM *param;
param = &qm->param;
if (param->fast_search_mode) {
qm->reg[0x03] |= 0x01;
status = qm_write(qm, bus, 0x03, qm->reg[0x03]);
if (status)
return status;
} else {
qm->reg[0x03] &= 0xfe;
status = qm_write(qm, bus, 0x03, qm->reg[0x03]);
if (status)
return status;
}
return status;
}
static __u32 FREQ_TABLE[9][3] = {
{ 2151000, 1, 7 },
{ 1950000, 1, 6 },
{ 1800000, 1, 5 },
{ 1600000, 1, 4 },
{ 1450000, 1, 3 },
{ 1250000, 1, 2 },
{ 1200000, 0, 7 },
{ 975000, 0, 6 },
{ 950000, 0, 0 }
};
static __u32 SD_TABLE[24][2][3] = {
{{0x38fae1, 0xd, 0x5},{0x39fae1, 0xd, 0x5},},
{{0x3f570a, 0xe, 0x3},{0x570a, 0xe, 0x3},},
{{0x5b333, 0xe, 0x5},{0x6b333, 0xe, 0x5},},
{{0x3c0f5c, 0xf, 0x4},{0x3d0f5c, 0xf, 0x4},},
{{0x26b85, 0xf, 0x6},{0x36b85, 0xf, 0x6},},
{{0x38c7ae, 0x10, 0x5},{0x39c7ae, 0x10, 0x5},},
{{0x3f23d7, 0x11, 0x3},{0x23d7, 0x11, 0x3},},
{{0x58000, 0x11, 0x5},{0x68000, 0x11, 0x5},},
{{0x3bdc28, 0x12, 0x4},{0x3cdc28, 0x12, 0x4},},
{{0x23851, 0x12, 0x6},{0x33851, 0x12, 0x6},},
{{0x38947a, 0x13, 0x5},{0x39947a, 0x13, 0x5},},
{{0x3ef0a3, 0x14, 0x3},{0x3ff0a3, 0x14, 0x3},},
{{0x3c8000, 0x16, 0x4},{0x3d8000, 0x16, 0x4},},
{{0x48000, 0x16, 0x6},{0x58000, 0x16, 0x6},},
{{0x3c8000, 0x17, 0x5},{0x3d8000, 0x17, 0x5},},
{{0x48000, 0x18, 0x3},{0x58000, 0x18, 0x3},},
{{0x3c8000, 0x18, 0x6},{0x3d8000, 0x18, 0x6},},
{{0x48000, 0x19, 0x4},{0x58000, 0x19, 0x4},},
{{0x3c8000, 0x1a, 0x3},{0x3d8000, 0x1a, 0x3},},
{{0x48000, 0x1a, 0x5},{0x58000, 0x1a, 0x5},},
{{0x3c8000, 0x1b, 0x4},{0x3d8000, 0x1b, 0x4},},
{{0x48000, 0x1b, 0x6},{0x58000, 0x1b, 0x6},},
{{0x3c8000, 0x1c, 0x5},{0x3d8000, 0x1c, 0x5},},
{{0x48000, 0x1d, 0x3},{0x58000, 0x1d, 0x3},},
};
static STATUS
qm_tuning(PT3_QM *qm, PT3_BUS *bus, __u32 *sd, __u32 channel)
{
STATUS status;
PT3_QM_PARAM *param = &qm->param;
__u8 i_data;
__u32 index, i, N, A;
// __u32 a;
//double M, b; // double
qm->reg[0x08] &= 0xf0;
qm->reg[0x08] |= 0x09;
qm->reg[0x13] &= 0x9f;
qm->reg[0x13] |= 0x20;
for (i = 0; i < 8; i++) {
if ((FREQ_TABLE[i+1][0] <= param->channel_freq) &&
(param->channel_freq < FREQ_TABLE[i][0])) {
i_data = qm->reg[0x02];
i_data &= 0x0f;
i_data |= FREQ_TABLE[i][1] << 7;
i_data |= FREQ_TABLE[i][2] << 4;
status = qm_write(qm, bus, 0x02, i_data);
}
}
#if 0
//M = (double)(param->channel_freq) / (double)(param->crystal_freq);
M = param->channel_freq / param->crystal_freq;
//a = (__s32)(M + 0.5);
a = (__s32)((M * 10 + 5) / 10);
b = M - a;
N = (a - 12) >> 2;
A = a - 4 * (N + 1) - 5;
if (0 <= b)
//*sd = (__u32)(pow(2, 20.) * b);
*sd = (__u32)((2^20) * b);
else
//*sd = (__u32)(pow(2, 20.) * b + (1 << 22);
*sd = (__u32)((2 ^ 20) * b + (1 << 22));
#else
index = pt3_tc_index(qm->tc);
*sd = SD_TABLE[channel][index][0];
N = SD_TABLE[channel][index][1];
A = SD_TABLE[channel][index][2];
#endif
qm->reg[0x06] &= 0x40;
qm->reg[0x06] |= N;
status = qm_write(qm, bus, 0x06, qm->reg[0x06]);
if (status)
return status;
qm->reg[0x07] &= 0xf0;
qm->reg[0x07] |= A & 0x0f;
status = qm_write(qm, bus, 0x07, qm->reg[0x07]);
if (status)
return status;
return status;
}
static STATUS
qm_local_lpf_tuning(PT3_QM *qm, PT3_BUS *bus, int lpf, __u32 channel)
{
PT3_QM_PARAM *param = &qm->param;
__u8 i_data;
__u32 sd;
STATUS status;
sd = 0;
status = qm_tuning(qm, bus, &sd, channel);
if (status)
return status;
if (lpf) {
i_data = qm->reg[0x08] & 0xf0;
i_data |= 2;
status = qm_write(qm, bus, 0x08, i_data);
} else {
status = qm_write(qm, bus, 0x08, qm->reg[0x08]);
}
if (status)
return status;
qm->reg[0x09] &= 0xc0;
qm->reg[0x09] |= (sd >> 16) & 0x3f;
qm->reg[0x0a] = (sd >> 8) & 0xff;
qm->reg[0x0b] = (sd >> 0) & 0xff;
status = qm_write(qm, bus, 0x09, qm->reg[0x09]);
if (status)
return status;
status = qm_write(qm, bus, 0x0a, qm->reg[0x0a]);
if (status)
return status;
status = qm_write(qm, bus, 0x0b, qm->reg[0x0b]);
if (status)
return status;
if (!lpf) {
status = qm_write(qm, bus, 0x13, qm->reg[0x13]);
if (status)
return status;
}
if (lpf) {
i_data = qm->reg[0x0c];
i_data &= 0x3f;
status = qm_write(qm, bus, 0x0c, i_data);
if (status)
return status;
qm_sleep(qm, bus, 1);
i_data = qm->reg[0x0c];
i_data |= 0xc0;
status = qm_write(qm, bus, 0x0c, i_data);
if (status)
return status;
} else {
i_data = qm->reg[0x0c];
i_data &= 0x7f;
status = qm_write(qm, bus, 0x0c, i_data);
if (status)
return status;
qm_sleep(qm, bus, 2); // 1024usec
i_data = qm->reg[0x0c];
i_data |= 0x80;
status = qm_write(qm, bus, 0x0c, i_data);
if (status)
return status;
}
if (lpf) {
qm_sleep(qm, bus, param->lpf_wait_time);
} else {
if (qm->reg[0x03] & 0x01) {
qm_sleep(qm, bus, param->fast_search_wait_time);
} else {
qm_sleep(qm, bus, param->normal_search_wait_time);
}
}
if (lpf) {
status = qm_write(qm, bus, 0x08, 0x09);
if (status)
return status;
status = qm_write(qm, bus, 0x13, qm->reg[0x13]);
if (status)
return status;
}
return status;
}
static __u8 qm_address[MAX_TUNER] = { 0x63, 0x60 };
__u8
pt3_qm_address(__u32 index)
{
return qm_address[index];
}
STATUS
pt3_qm_set_sleep(PT3_QM *qm, int sleep)
{
STATUS status;
PT3_TS_PIN_MODE mode;
mode = sleep ? PT3_TS_PIN_MODE_LOW : PT3_TS_PIN_MODE_NORMAL;
qm->param.standby = sleep;
if (sleep) {
status = pt3_tc_set_agc_s(qm->tc, NULL, PT3_TC_AGC_MANUAL);
if (status)
return status;
qm_set_sleep_mode(qm, NULL);
pt3_tc_set_sleep_s(qm->tc, NULL, sleep);
} else {
pt3_tc_set_sleep_s(qm->tc, NULL, sleep);
qm_set_sleep_mode(qm, NULL);
}
qm->sleep = sleep;
return STATUS_OK;
}
void
pt3_qm_dummy_reset(PT3_QM *qm, PT3_BUS *bus)
{
qm_write(qm, bus, 0x01, INIT_DUMMY_RESET);
qm_write(qm, bus, 0x01, INIT_DUMMY_RESET);
}
void
pt3_qm_init_reg_param(PT3_QM *qm)
{
memcpy(qm->reg, rw_reg, sizeof(rw_reg));
qm->param.channel_freq = 0;
qm->param.crystal_freq = 16000;
qm->param.fast_search_mode = 0;
qm->param.standby = 0;
qm->param.lpf_wait_time = 20;
qm->param.fast_search_wait_time = 4;
qm->param.normal_search_wait_time = 15;
}
STATUS
pt3_qm_init(PT3_QM *qm, PT3_BUS *bus)
{
__u8 i_data;
__u32 i;
STATUS status;
// soft reset on
status = qm_write(qm, bus, 0x01, INIT_DUMMY_RESET);
if (status)
return status;
qm_sleep(qm, bus, 1);
// soft reset off
i_data = qm->reg[0x01];
i_data |= 0x10;
status = qm_write(qm, bus, 0x01, i_data);
if (status)
return status;
// ID check
status = qm_read(qm, bus, 0x00, &i_data);
if (status)
return status;
if ((bus == NULL) && (i_data != 0x48))
return STATUS_INVALID_PARAM_ERROR;
// LPF tuning on
qm_sleep(qm, bus, 1);
qm->reg[0x0c] |= 0x40;
status = qm_write(qm, bus, 0x0c, qm->reg[0x0c]);
if (status)
return status;
qm_sleep(qm, bus, qm->param.lpf_wait_time);
for (i = 0; i < sizeof(flag); i++) {
if (flag[i] == 1) {
status = qm_write(qm, bus, i, qm->reg[i]);
if (status)
return status;
}
}
status = qm_set_sleep_mode(qm, bus);
if (status)
return status;
status = qm_set_search_mode(qm, bus);
if (status)
return status;
return status;
}
STATUS
pt3_qm_get_locked(PT3_QM *qm, PT3_BUS *bus, int *locked)
{
STATUS status;
status = qm_read(qm, bus, 0x0d, &qm->reg[0x0d]);
if (status)
return status;
if (qm->reg[0x0d] & 0x40)
*locked = 1;
else
*locked = 0;
return status;
}
STATUS
pt3_qm_set_frequency(PT3_QM *qm, __u32 channel, __s32 offset)
{
STATUS status;
int bs, locked;
__u32 number, freq, freq_khz;
struct timeval begin, now;
status = pt3_tc_set_agc_s(qm->tc, NULL, PT3_TC_AGC_MANUAL);
if (status)
return status;
pt3_qm_get_channel_freq(channel, &bs, &number, &freq);
freq_khz = freq * 10 + offset;
if (pt3_tc_index(qm->tc) == 0)
freq_khz -= 500;
else
freq_khz += 500;
qm->param.channel_freq = freq_khz;
//PT3_PRINTK(7, KERN_DEBUG "frequency %d Khz\n", freq_khz);
status = qm_local_lpf_tuning(qm, NULL, 1, channel);
if (status)
return status;
do_gettimeofday(&begin);
while (1) {
do_gettimeofday(&now);
status = pt3_qm_get_locked(qm, NULL, &locked);
if (status)
return status;
if (locked)
break;
if (time_diff(&begin, &now) >= 100)
break;
schedule_timeout_interruptible(msecs_to_jiffies(1));
}
// PT3_PRINTK(7, KERN_DEBUG "qm_get_locked %d status=0x%x\n", locked, status);
if (!locked)
return STATUS_PLL_LOCK_TIMEOUT_ERROR;
status = pt3_tc_set_agc_s(qm->tc, NULL, PT3_TC_AGC_AUTO);
if (status)
return status;
qm->channel = channel;
qm->offset = offset;
return status;
}
PT3_QM *
create_pt3_qm(PT3_I2C *i2c, PT3_TC *tc)
{
PT3_QM *qm;
qm = NULL;
qm = pt3_vzalloc(sizeof(PT3_QM));
if (qm == NULL)
goto fail;
qm->i2c = i2c;
qm->tc = tc;
qm->sleep = 1;
return qm;
fail:
if (qm != NULL)
vfree(qm);
return NULL;
}
void
free_pt3_qm(PT3_QM *qm)
{
vfree(qm);
}