xref: /haiku/src/add-ons/kernel/drivers/dvb/cx23882/dtt7592.c (revision 4f2fd49bdc6078128b1391191e4edac647044c3d)
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 
37 status_t
38 dtt7592_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 
49 status_t
50 dtt7592_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 
60 status_t
61 dtt7592_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 %ld, divider 0x%lx (%ld)\n", frequency, divider, divider);
72 
73 	data[0] = divider >> 8;
74 	data[1] = (uint8)divider;
75 
76 	if (frequency > 835000000)
77 		data[2] = 0xfc;
78 	else if (frequency > 735000000)
79 		data[2] = 0xf4;
80 	else if (frequency > 465000000)
81 		data[2] = 0xbc;
82 	else if (frequency > 445000000)
83 		data[2] = 0xfc;
84 	else if (frequency > 405000000)
85 		data[2] = 0xf4;
86 	else if (frequency > 305000000)
87 		data[2] = 0xbc;
88 	else
89 		data[2] = 0xb4;
90 
91 	if (frequency > 429000000)
92 		data[3] = 0x08; // select UHF IV/V
93 	else
94 		data[3] = 0x02; // select VHF III
95 
96 	// only used in Germany right now, where VHF channels
97 	// are 7 MHz wide, while UHF are 8 MHz.
98 	if (bandwidth == DVB_BANDWIDTH_5_MHZ
99 		|| bandwidth == DVB_BANDWIDTH_6_MHZ
100 		|| bandwidth == DVB_BANDWIDTH_7_MHZ) {
101 		data[3] |= 0x10; // activate 7 Mhz filter
102 	}
103 
104 	res = dtt7592_write(bus, data);
105 	if (res != B_OK) {
106 		dprintf("dtt7592_set_frequency step 1 failed\n");
107 		return res;
108 	}
109 
110 	// enable AGC
111 	data[2] = (data[2] & 0x40) | 0x9c;
112 	data[3] = 0xa0;
113 	res = dtt7592_write(bus, data);
114 	if (res != B_OK) {
115 		dprintf("dtt7592_set_frequency step 2 failed\n");
116 		return res;
117 	}
118 
119 	// wait 100 ms
120 	snooze(100000);
121 
122 	// disable AGC
123 	data[3] = 0x20;
124 	res = dtt7592_write(bus, data);
125 	if (res != B_OK) {
126 		dprintf("dtt7592_set_frequency step 3 failed\n");
127 		return res;
128 	}
129 
130 	return B_OK;
131 }
132 
133 
134 #if 0
135 void
136 dtt7582_dump(i2c_bus *bus)
137 {
138 	uint8 data;
139 	if (B_OK != dtt7592_read(bus, &data)) {
140 		TRACE("dtt7582_dump failed\n");
141 	}
142 	TRACE("dtt7582_dump: 0x%02x, PLL %s, AGC %s\n", data, (data & 0x40) ? "locked" : "open", (data & 0x08) ? "active" : "off");
143 }
144 
145 
146 void
147 dtt7582_test(i2c_bus *bus)
148 {
149 	TRACE("dtt7582_test start\n");
150 	dtt7582_dump(bus);
151 	TRACE("dtt7582_test freq 1\n");
152 	dtt7592_set_frequency(bus, 150000000, DVB_BANDWIDTH_7_MHZ);
153 	dtt7582_dump(bus);
154 	TRACE("dtt7582_test freq 2\n");
155 	dtt7592_set_frequency(bus, 746000000, DVB_BANDWIDTH_8_MHZ); // Kabel 1
156 	dtt7582_dump(bus);
157 	TRACE("dtt7582_test freq 3\n");
158 	dtt7592_set_frequency(bus, 538000000, DVB_BANDWIDTH_7_MHZ); // VOX
159 	dtt7582_dump(bus);
160 	TRACE("dtt7582_test freq 4\n");
161 	dtt7592_set_frequency(bus, 896000000, DVB_BANDWIDTH_8_MHZ);
162 	dtt7582_dump(bus);
163 	TRACE("dtt7582_test freq 5\n");
164 	dtt7592_set_frequency(bus, 333000000, DVB_BANDWIDTH_7_MHZ);
165 	dtt7582_dump(bus);
166 }
167 #endif
168