1/*
2 * Copyright (c) 2004-2007 Marcus Overhagen <marcus@overhagen.de>
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify,
8 * merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <KernelExport.h>
26#include "dtt7592.h"
27#include "config.h"
28
29#define TRACE_DTT7592
30#ifdef TRACE_DTT7592
31  #define TRACE dprintf
32#else
33  #define TRACE(a...)
34#endif
35
36
37status_t
38dtt7592_write(i2c_bus *bus, const uint8 data[4])
39{
40	status_t res;
41	TRACE("dtt7592_write values 0x%02x 0x%02x 0x%02x 0x%02x\n", data[0], data[1], data[2], data[3]);
42	res = i2c_write(bus, I2C_ADDR_PLL, data, 4);
43	if (res != B_OK)
44		TRACE("dtt7592_write error, values 0x%02x 0x%02x 0x%02x 0x%02x\n", data[0], data[1], data[2], data[3]);
45	return res;
46}
47
48
49status_t
50dtt7592_read(i2c_bus *bus, uint8 *data)
51{
52	status_t res;
53	res = i2c_read(bus, I2C_ADDR_PLL, data, 1);
54	if (res != B_OK)
55		TRACE("dtt7592_read error\n");
56	return res;
57}
58
59
60status_t
61dtt7592_set_frequency(i2c_bus *bus, uint32 frequency, dvb_bandwidth_t bandwidth)
62{
63	status_t res;
64	uint32 divider;
65	uint8 data[4];
66
67	divider = (frequency + 36083333) / 166666;
68	if (divider > 0x7fff)
69		divider = 0x7fff;
70
71	TRACE("dtt7592_set_frequency frequency %" B_PRId32 ", divider 0x%"
72		B_PRIx32 " (%" B_PRId32 ")\n", frequency, divider, divider);
73
74	data[0] = divider >> 8;
75	data[1] = (uint8)divider;
76
77	if (frequency > 835000000)
78		data[2] = 0xfc;
79	else if (frequency > 735000000)
80		data[2] = 0xf4;
81	else if (frequency > 465000000)
82		data[2] = 0xbc;
83	else if (frequency > 445000000)
84		data[2] = 0xfc;
85	else if (frequency > 405000000)
86		data[2] = 0xf4;
87	else if (frequency > 305000000)
88		data[2] = 0xbc;
89	else
90		data[2] = 0xb4;
91
92	if (frequency > 429000000)
93		data[3] = 0x08; // select UHF IV/V
94	else
95		data[3] = 0x02; // select VHF III
96
97	// only used in Germany right now, where VHF channels
98	// are 7 MHz wide, while UHF are 8 MHz.
99	if (bandwidth == DVB_BANDWIDTH_5_MHZ
100		|| bandwidth == DVB_BANDWIDTH_6_MHZ
101		|| bandwidth == DVB_BANDWIDTH_7_MHZ) {
102		data[3] |= 0x10; // activate 7 Mhz filter
103	}
104
105	res = dtt7592_write(bus, data);
106	if (res != B_OK) {
107		dprintf("dtt7592_set_frequency step 1 failed\n");
108		return res;
109	}
110
111	// enable AGC
112	data[2] = (data[2] & 0x40) | 0x9c;
113	data[3] = 0xa0;
114	res = dtt7592_write(bus, data);
115	if (res != B_OK) {
116		dprintf("dtt7592_set_frequency step 2 failed\n");
117		return res;
118	}
119
120	// wait 100 ms
121	snooze(100000);
122
123	// disable AGC
124	data[3] = 0x20;
125	res = dtt7592_write(bus, data);
126	if (res != B_OK) {
127		dprintf("dtt7592_set_frequency step 3 failed\n");
128		return res;
129	}
130
131	return B_OK;
132}
133
134
135#if 0
136void
137dtt7582_dump(i2c_bus *bus)
138{
139	uint8 data;
140	if (B_OK != dtt7592_read(bus, &data)) {
141		TRACE("dtt7582_dump failed\n");
142	}
143	TRACE("dtt7582_dump: 0x%02x, PLL %s, AGC %s\n", data, (data & 0x40) ? "locked" : "open", (data & 0x08) ? "active" : "off");
144}
145
146
147void
148dtt7582_test(i2c_bus *bus)
149{
150	TRACE("dtt7582_test start\n");
151	dtt7582_dump(bus);
152	TRACE("dtt7582_test freq 1\n");
153	dtt7592_set_frequency(bus, 150000000, DVB_BANDWIDTH_7_MHZ);
154	dtt7582_dump(bus);
155	TRACE("dtt7582_test freq 2\n");
156	dtt7592_set_frequency(bus, 746000000, DVB_BANDWIDTH_8_MHZ); // Kabel 1
157	dtt7582_dump(bus);
158	TRACE("dtt7582_test freq 3\n");
159	dtt7592_set_frequency(bus, 538000000, DVB_BANDWIDTH_7_MHZ); // VOX
160	dtt7582_dump(bus);
161	TRACE("dtt7582_test freq 4\n");
162	dtt7592_set_frequency(bus, 896000000, DVB_BANDWIDTH_8_MHZ);
163	dtt7582_dump(bus);
164	TRACE("dtt7582_test freq 5\n");
165	dtt7592_set_frequency(bus, 333000000, DVB_BANDWIDTH_7_MHZ);
166	dtt7582_dump(bus);
167}
168#endif
169