blob: c47501ae8b0e588ae0aed184700f61b877b9ea6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001 /*
2 Driver for Philips tda1004xh OFDM Demodulator
3
4 (c) 2003, 2004 Andrew de Quincey & Robert Schlabbach
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 */
22/*
23 * This driver needs external firmware. Please use the commands
24 * "<kerneldir>/Documentation/dvb/get_dvb_firmware tda10045",
25 * "<kerneldir>/Documentation/dvb/get_dvb_firmware tda10046" to
Ville Skytt\รค12e66f62006-01-09 15:25:38 -020026 * download/extract them, and then copy them to /usr/lib/hotplug/firmware
27 * or /lib/firmware (depending on configuration of firmware hotplug).
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 */
29#define TDA10045_DEFAULT_FIRMWARE "dvb-fe-tda10045.fw"
30#define TDA10046_DEFAULT_FIRMWARE "dvb-fe-tda10046.fw"
31
32#include <linux/init.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080036#include <linux/jiffies.h>
37#include <linux/string.h>
38#include <linux/slab.h>
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "dvb_frontend.h"
41#include "tda1004x.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static int debug;
44#define dprintk(args...) \
45 do { \
46 if (debug) printk(KERN_DEBUG "tda1004x: " args); \
47 } while (0)
48
49#define TDA1004X_CHIPID 0x00
50#define TDA1004X_AUTO 0x01
51#define TDA1004X_IN_CONF1 0x02
52#define TDA1004X_IN_CONF2 0x03
53#define TDA1004X_OUT_CONF1 0x04
54#define TDA1004X_OUT_CONF2 0x05
55#define TDA1004X_STATUS_CD 0x06
56#define TDA1004X_CONFC4 0x07
57#define TDA1004X_DSSPARE2 0x0C
58#define TDA10045H_CODE_IN 0x0D
59#define TDA10045H_FWPAGE 0x0E
60#define TDA1004X_SCAN_CPT 0x10
61#define TDA1004X_DSP_CMD 0x11
62#define TDA1004X_DSP_ARG 0x12
63#define TDA1004X_DSP_DATA1 0x13
64#define TDA1004X_DSP_DATA2 0x14
65#define TDA1004X_CONFADC1 0x15
66#define TDA1004X_CONFC1 0x16
67#define TDA10045H_S_AGC 0x1a
68#define TDA10046H_AGC_TUN_LEVEL 0x1a
69#define TDA1004X_SNR 0x1c
70#define TDA1004X_CONF_TS1 0x1e
71#define TDA1004X_CONF_TS2 0x1f
72#define TDA1004X_CBER_RESET 0x20
73#define TDA1004X_CBER_MSB 0x21
74#define TDA1004X_CBER_LSB 0x22
75#define TDA1004X_CVBER_LUT 0x23
76#define TDA1004X_VBER_MSB 0x24
77#define TDA1004X_VBER_MID 0x25
78#define TDA1004X_VBER_LSB 0x26
79#define TDA1004X_UNCOR 0x27
80
81#define TDA10045H_CONFPLL_P 0x2D
82#define TDA10045H_CONFPLL_M_MSB 0x2E
83#define TDA10045H_CONFPLL_M_LSB 0x2F
84#define TDA10045H_CONFPLL_N 0x30
85
86#define TDA10046H_CONFPLL1 0x2D
87#define TDA10046H_CONFPLL2 0x2F
88#define TDA10046H_CONFPLL3 0x30
89#define TDA10046H_TIME_WREF1 0x31
90#define TDA10046H_TIME_WREF2 0x32
91#define TDA10046H_TIME_WREF3 0x33
92#define TDA10046H_TIME_WREF4 0x34
93#define TDA10046H_TIME_WREF5 0x35
94
95#define TDA10045H_UNSURW_MSB 0x31
96#define TDA10045H_UNSURW_LSB 0x32
97#define TDA10045H_WREF_MSB 0x33
98#define TDA10045H_WREF_MID 0x34
99#define TDA10045H_WREF_LSB 0x35
100#define TDA10045H_MUXOUT 0x36
101#define TDA1004X_CONFADC2 0x37
102
103#define TDA10045H_IOFFSET 0x38
104
105#define TDA10046H_CONF_TRISTATE1 0x3B
106#define TDA10046H_CONF_TRISTATE2 0x3C
107#define TDA10046H_CONF_POLARITY 0x3D
108#define TDA10046H_FREQ_OFFSET 0x3E
109#define TDA10046H_GPIO_OUT_SEL 0x41
110#define TDA10046H_GPIO_SELECT 0x42
111#define TDA10046H_AGC_CONF 0x43
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700112#define TDA10046H_AGC_THR 0x44
113#define TDA10046H_AGC_RENORM 0x45
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define TDA10046H_AGC_GAINS 0x46
115#define TDA10046H_AGC_TUN_MIN 0x47
116#define TDA10046H_AGC_TUN_MAX 0x48
117#define TDA10046H_AGC_IF_MIN 0x49
118#define TDA10046H_AGC_IF_MAX 0x4A
119
120#define TDA10046H_FREQ_PHY2_MSB 0x4D
121#define TDA10046H_FREQ_PHY2_LSB 0x4E
122
123#define TDA10046H_CVBER_CTRL 0x4F
124#define TDA10046H_AGC_IF_LEVEL 0x52
125#define TDA10046H_CODE_CPT 0x57
126#define TDA10046H_CODE_IN 0x58
127
128
129static int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
130{
131 int ret;
132 u8 buf[] = { reg, data };
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700133 struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 dprintk("%s: reg=0x%x, data=0x%x\n", __FUNCTION__, reg, data);
136
137 msg.addr = state->config->demod_address;
138 ret = i2c_transfer(state->i2c, &msg, 1);
139
140 if (ret != 1)
141 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
142 __FUNCTION__, reg, data, ret);
143
144 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __FUNCTION__,
145 reg, data, ret);
146 return (ret != 1) ? -1 : 0;
147}
148
149static int tda1004x_read_byte(struct tda1004x_state *state, int reg)
150{
151 int ret;
152 u8 b0[] = { reg };
153 u8 b1[] = { 0 };
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700154 struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
155 { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 dprintk("%s: reg=0x%x\n", __FUNCTION__, reg);
158
159 msg[0].addr = state->config->demod_address;
160 msg[1].addr = state->config->demod_address;
161 ret = i2c_transfer(state->i2c, msg, 2);
162
163 if (ret != 2) {
164 dprintk("%s: error reg=0x%x, ret=%i\n", __FUNCTION__, reg,
165 ret);
166 return -1;
167 }
168
169 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __FUNCTION__,
170 reg, b1[0], ret);
171 return b1[0];
172}
173
174static int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
175{
176 int val;
177 dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __FUNCTION__, reg,
178 mask, data);
179
180 // read a byte and check
181 val = tda1004x_read_byte(state, reg);
182 if (val < 0)
183 return val;
184
185 // mask if off
186 val = val & ~mask;
187 val |= data & 0xff;
188
189 // write it out again
190 return tda1004x_write_byteI(state, reg, val);
191}
192
193static int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
194{
195 int i;
196 int result;
197
198 dprintk("%s: reg=0x%x, len=0x%x\n", __FUNCTION__, reg, len);
199
200 result = 0;
201 for (i = 0; i < len; i++) {
202 result = tda1004x_write_byteI(state, reg + i, buf[i]);
203 if (result != 0)
204 break;
205 }
206
207 return result;
208}
209
210static int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
211{
212 int result;
213 dprintk("%s\n", __FUNCTION__);
214
215 result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200216 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return result;
218}
219
220static int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
221{
222 dprintk("%s\n", __FUNCTION__);
223
224 return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
225}
226
227static int tda10045h_set_bandwidth(struct tda1004x_state *state,
228 fe_bandwidth_t bandwidth)
229{
230 static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
231 static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
232 static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
233
234 switch (bandwidth) {
235 case BANDWIDTH_6_MHZ:
236 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
237 break;
238
239 case BANDWIDTH_7_MHZ:
240 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
241 break;
242
243 case BANDWIDTH_8_MHZ:
244 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
245 break;
246
247 default:
248 return -EINVAL;
249 }
250
251 tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
252
253 return 0;
254}
255
256static int tda10046h_set_bandwidth(struct tda1004x_state *state,
257 fe_bandwidth_t bandwidth)
258{
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200259 static u8 bandwidth_6mhz_53M[] = { 0x7b, 0x2e, 0x11, 0xf0, 0xd2 };
260 static u8 bandwidth_7mhz_53M[] = { 0x6a, 0x02, 0x6a, 0x43, 0x9f };
261 static u8 bandwidth_8mhz_53M[] = { 0x5c, 0x32, 0xc2, 0x96, 0x6d };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200263 static u8 bandwidth_6mhz_48M[] = { 0x70, 0x02, 0x49, 0x24, 0x92 };
264 static u8 bandwidth_7mhz_48M[] = { 0x60, 0x02, 0xaa, 0xaa, 0xab };
265 static u8 bandwidth_8mhz_48M[] = { 0x54, 0x03, 0x0c, 0x30, 0xc3 };
266 int tda10046_clk53m;
267
268 if ((state->config->if_freq == TDA10046_FREQ_045) ||
269 (state->config->if_freq == TDA10046_FREQ_052))
270 tda10046_clk53m = 0;
271 else
272 tda10046_clk53m = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 switch (bandwidth) {
274 case BANDWIDTH_6_MHZ:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200275 if (tda10046_clk53m)
276 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_53M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200277 sizeof(bandwidth_6mhz_53M));
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200278 else
279 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_48M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200280 sizeof(bandwidth_6mhz_48M));
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700281 if (state->config->if_freq == TDA10046_FREQ_045) {
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200282 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
283 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xab);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286
287 case BANDWIDTH_7_MHZ:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200288 if (tda10046_clk53m)
289 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_53M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200290 sizeof(bandwidth_7mhz_53M));
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200291 else
292 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_48M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200293 sizeof(bandwidth_7mhz_48M));
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700294 if (state->config->if_freq == TDA10046_FREQ_045) {
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200295 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
296 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 break;
299
300 case BANDWIDTH_8_MHZ:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200301 if (tda10046_clk53m)
302 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_53M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200303 sizeof(bandwidth_8mhz_53M));
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200304 else
305 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_48M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200306 sizeof(bandwidth_8mhz_48M));
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700307 if (state->config->if_freq == TDA10046_FREQ_045) {
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200308 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
309 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x55);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312
313 default:
314 return -EINVAL;
315 }
316
317 return 0;
318}
319
320static int tda1004x_do_upload(struct tda1004x_state *state,
321 unsigned char *mem, unsigned int len,
322 u8 dspCodeCounterReg, u8 dspCodeInReg)
323{
324 u8 buf[65];
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700325 struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int tx_size;
327 int pos = 0;
328
329 /* clear code counter */
330 tda1004x_write_byteI(state, dspCodeCounterReg, 0);
331 fw_msg.addr = state->config->demod_address;
332
333 buf[0] = dspCodeInReg;
334 while (pos != len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 // work out how much to send this time
336 tx_size = len - pos;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700337 if (tx_size > 0x10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 tx_size = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 // send the chunk
341 memcpy(buf + 1, mem + pos, tx_size);
342 fw_msg.len = tx_size + 1;
343 if (i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700344 printk(KERN_ERR "tda1004x: Error during firmware upload\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -EIO;
346 }
347 pos += tx_size;
348
349 dprintk("%s: fw_pos=0x%x\n", __FUNCTION__, pos);
350 }
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700351 // give the DSP a chance to settle 03/10/05 Hac
352 msleep(100);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700357static int tda1004x_check_upload_ok(struct tda1004x_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 u8 data1, data2;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700360 unsigned long timeout;
361
362 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
363 timeout = jiffies + 2 * HZ;
364 while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
365 if (time_after(jiffies, timeout)) {
366 printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
367 break;
368 }
369 msleep(1);
370 }
371 } else
372 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 // check upload was OK
375 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
376 tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
377
378 data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
379 data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
Hartmut Hackmann3faadbb2005-07-07 17:57:42 -0700380 if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700381 printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EIO;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700383 }
384 printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386}
387
388static int tda10045_fwupload(struct dvb_frontend* fe)
389{
390 struct tda1004x_state* state = fe->demodulator_priv;
391 int ret;
392 const struct firmware *fw;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* don't re-upload unless necessary */
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700395 if (tda1004x_check_upload_ok(state) == 0)
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /* request the firmware, this will block until someone uploads it */
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700399 printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", TDA10045_DEFAULT_FIRMWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
401 if (ret) {
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700402 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return ret;
404 }
405
406 /* reset chip */
407 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
408 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
409 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
410 msleep(10);
411
412 /* set parameters */
413 tda10045h_set_bandwidth(state, BANDWIDTH_8_MHZ);
414
415 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
Anssi Hannula0c744b02005-07-07 17:57:42 -0700416 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (ret)
418 return ret;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700419 printk(KERN_INFO "tda1004x: firmware upload complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* wait for DSP to initialise */
422 /* DSPREADY doesn't seem to work on the TDA10045H */
423 msleep(100);
424
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700425 return tda1004x_check_upload_ok(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700428static void tda10046_init_plls(struct dvb_frontend* fe)
Johannes Stezenbach71e34202005-05-16 21:54:36 -0700429{
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700430 struct tda1004x_state* state = fe->demodulator_priv;
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200431 int tda10046_clk53m;
432
433 if ((state->config->if_freq == TDA10046_FREQ_045) ||
434 (state->config->if_freq == TDA10046_FREQ_052))
435 tda10046_clk53m = 0;
436 else
437 tda10046_clk53m = 1;
Johannes Stezenbach71e34202005-05-16 21:54:36 -0700438
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700439 tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200440 if(tda10046_clk53m) {
441 printk(KERN_INFO "tda1004x: setting up plls for 53MHz sampling clock\n");
442 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x08); // PLL M = 8
443 } else {
444 printk(KERN_INFO "tda1004x: setting up plls for 48MHz sampling clock\n");
445 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x03); // PLL M = 3
446 }
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700447 if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
448 dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __FUNCTION__);
449 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
450 } else {
451 dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __FUNCTION__);
452 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
Johannes Stezenbach71e34202005-05-16 21:54:36 -0700453 }
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200454 if(tda10046_clk53m)
455 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x67);
456 else
457 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x72);
458 /* Note clock frequency is handled implicitly */
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700459 switch (state->config->if_freq) {
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700460 case TDA10046_FREQ_045:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200461 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
462 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700463 break;
464 case TDA10046_FREQ_052:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200465 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
466 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xc7);
467 break;
468 case TDA10046_FREQ_3617:
469 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
470 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x59);
471 break;
472 case TDA10046_FREQ_3613:
473 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
474 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x3f);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700475 break;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700476 }
477 tda10046h_set_bandwidth(state, BANDWIDTH_8_MHZ); // default bandwidth 8 MHz
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200478 /* let the PLLs settle */
479 msleep(120);
Johannes Stezenbach71e34202005-05-16 21:54:36 -0700480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static int tda10046_fwupload(struct dvb_frontend* fe)
483{
484 struct tda1004x_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int ret;
486 const struct firmware *fw;
487
488 /* reset + wake up chip */
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200489 if (state->config->xtal_freq == TDA10046_XTAL_4M) {
490 tda1004x_write_byteI(state, TDA1004X_CONFC4, 0);
491 } else {
492 dprintk("%s: 16MHz Xtal, reducing I2C speed\n", __FUNCTION__);
493 tda1004x_write_byteI(state, TDA1004X_CONFC4, 0x80);
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300496 /* set GPIO 1 and 3 */
497 if (state->config->gpio_config != TDA10046_GPTRI) {
498 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE2, 0x33);
499 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f, state->config->gpio_config &0x0f);
500 }
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700501 /* let the clocks recover from sleep */
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300502 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200504 /* The PLLs need to be reprogrammed after sleep */
505 tda10046_init_plls(fe);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* don't re-upload unless necessary */
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700508 if (tda1004x_check_upload_ok(state) == 0)
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300511 printk(KERN_INFO "tda1004x: trying to boot from eeprom\n");
512 tda1004x_write_mask(state, TDA1004X_CONFC4, 4, 4);
513 msleep(300);
514 /* don't re-upload unless necessary */
515 if (tda1004x_check_upload_ok(state) == 0)
516 return 0;
517
518 /* request the firmware, this will block until someone uploads it */
519 printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
520 ret = state->config->request_firmware(fe, &fw, TDA10046_DEFAULT_FIRMWARE);
521 if (ret) {
522 /* remain compatible to old bug: try to load with tda10045 image name */
523 ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700524 if (ret) {
525 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800526 return ret;
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300527 } else
528 printk(KERN_INFO "tda1004x: please rename the firmware file to %s\n",
529 TDA10046_DEFAULT_FIRMWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300531 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
532 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
533 release_firmware(fw);
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700534 return tda1004x_check_upload_ok(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static int tda1004x_encode_fec(int fec)
538{
539 // convert known FEC values
540 switch (fec) {
541 case FEC_1_2:
542 return 0;
543 case FEC_2_3:
544 return 1;
545 case FEC_3_4:
546 return 2;
547 case FEC_5_6:
548 return 3;
549 case FEC_7_8:
550 return 4;
551 }
552
553 // unsupported
554 return -EINVAL;
555}
556
557static int tda1004x_decode_fec(int tdafec)
558{
559 // convert known FEC values
560 switch (tdafec) {
561 case 0:
562 return FEC_1_2;
563 case 1:
564 return FEC_2_3;
565 case 2:
566 return FEC_3_4;
567 case 3:
568 return FEC_5_6;
569 case 4:
570 return FEC_7_8;
571 }
572
573 // unsupported
574 return -1;
575}
576
Adrian Bunk34630402007-02-06 21:50:36 -0300577static int tda1004x_write(struct dvb_frontend* fe, u8 *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct tda1004x_state* state = fe->demodulator_priv;
580
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300581 if (len != 2)
582 return -EINVAL;
583
584 return tda1004x_write_byteI(state, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587static int tda10045_init(struct dvb_frontend* fe)
588{
589 struct tda1004x_state* state = fe->demodulator_priv;
590
591 dprintk("%s\n", __FUNCTION__);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (tda10045_fwupload(fe)) {
594 printk("tda1004x: firmware upload failed\n");
595 return -EIO;
596 }
597
598 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 // tda setup
601 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
602 tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
603 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
604 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
605 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
606 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
607 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
608 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
609 tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
610 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
611 tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
612
613 tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 0;
616}
617
618static int tda10046_init(struct dvb_frontend* fe)
619{
620 struct tda1004x_state* state = fe->demodulator_priv;
621 dprintk("%s\n", __FUNCTION__);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (tda10046_fwupload(fe)) {
624 printk("tda1004x: firmware upload failed\n");
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700625 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 // tda setup
629 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200630 tda1004x_write_byteI(state, TDA1004X_AUTO, 0x87); // 100 ppm crystal, select HP stream
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200631 tda1004x_write_byteI(state, TDA1004X_CONFC1, 0x88); // enable pulse killer
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700632
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700633 switch (state->config->agc_config) {
634 case TDA10046_AGC_DEFAULT:
635 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x00); // AGC setup
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300636 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700637 break;
638 case TDA10046_AGC_IFO_AUTO_NEG:
639 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300640 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700641 break;
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700642 case TDA10046_AGC_IFO_AUTO_POS:
643 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300644 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x00); // set AGC polarities
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700645 break;
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300646 case TDA10046_AGC_TDA827X:
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700647 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02); // AGC setup
648 tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70); // AGC Threshold
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200649 tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x08); // Gain Renormalize
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300650 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
Hartmut Hackmann550a9a52006-11-15 21:31:54 -0300651 break;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700652 }
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200653 tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300654 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0x79); // Turn IF AGC output on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0); // }
656 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
657 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0); // }
658 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff); // }
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200659 tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 0x12); // IF gain 2, TUN gain 1
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700660 tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700662 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200663 // tda1004x_write_mask(state, 0x50, 0x80, 0x80); // handle out of guard echoes
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700664 tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
669static int tda1004x_set_fe(struct dvb_frontend* fe,
670 struct dvb_frontend_parameters *fe_params)
671{
672 struct tda1004x_state* state = fe->demodulator_priv;
673 int tmp;
674 int inversion;
675
676 dprintk("%s\n", __FUNCTION__);
677
678 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
679 // setup auto offset
680 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
681 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
682 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
683
684 // disable agc_conf[2]
685 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
686 }
687
688 // set frequency
Patrick Boettcherdea74862006-05-14 05:01:31 -0300689 if (fe->ops.tuner_ops.set_params) {
690 fe->ops.tuner_ops.set_params(fe, fe_params);
691 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
Hartmut Hackmann634623d2005-11-08 21:35:13 -0800692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 // Hardcoded to use auto as much as possible on the TDA10045 as it
695 // is very unreliable if AUTO mode is _not_ used.
696 if (state->demod_type == TDA1004X_DEMOD_TDA10045) {
697 fe_params->u.ofdm.code_rate_HP = FEC_AUTO;
698 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
699 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
700 }
701
702 // Set standard params.. or put them to auto
703 if ((fe_params->u.ofdm.code_rate_HP == FEC_AUTO) ||
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200704 (fe_params->u.ofdm.code_rate_LP == FEC_AUTO) ||
705 (fe_params->u.ofdm.constellation == QAM_AUTO) ||
706 (fe_params->u.ofdm.hierarchy_information == HIERARCHY_AUTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 1); // enable auto
708 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x03, 0); // turn off constellation bits
709 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0); // turn off hierarchy bits
710 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x3f, 0); // turn off FEC bits
711 } else {
712 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 0); // disable auto
713
714 // set HP FEC
715 tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_HP);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700716 if (tmp < 0)
717 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
719
720 // set LP FEC
721 tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_LP);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700722 if (tmp < 0)
723 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
725
726 // set constellation
727 switch (fe_params->u.ofdm.constellation) {
728 case QPSK:
729 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 0);
730 break;
731
732 case QAM_16:
733 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 1);
734 break;
735
736 case QAM_64:
737 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 2);
738 break;
739
740 default:
741 return -EINVAL;
742 }
743
744 // set hierarchy
745 switch (fe_params->u.ofdm.hierarchy_information) {
746 case HIERARCHY_NONE:
747 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0 << 5);
748 break;
749
750 case HIERARCHY_1:
751 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 1 << 5);
752 break;
753
754 case HIERARCHY_2:
755 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 2 << 5);
756 break;
757
758 case HIERARCHY_4:
759 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 3 << 5);
760 break;
761
762 default:
763 return -EINVAL;
764 }
765 }
766
767 // set bandwidth
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700768 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 case TDA1004X_DEMOD_TDA10045:
770 tda10045h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
771 break;
772
773 case TDA1004X_DEMOD_TDA10046:
774 tda10046h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
775 break;
776 }
777
778 // set inversion
779 inversion = fe_params->inversion;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700780 if (state->config->invert)
781 inversion = inversion ? INVERSION_OFF : INVERSION_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 switch (inversion) {
783 case INVERSION_OFF:
784 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0);
785 break;
786
787 case INVERSION_ON:
788 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0x20);
789 break;
790
791 default:
792 return -EINVAL;
793 }
794
795 // set guard interval
796 switch (fe_params->u.ofdm.guard_interval) {
797 case GUARD_INTERVAL_1_32:
798 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
799 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
800 break;
801
802 case GUARD_INTERVAL_1_16:
803 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
804 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 1 << 2);
805 break;
806
807 case GUARD_INTERVAL_1_8:
808 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
809 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 2 << 2);
810 break;
811
812 case GUARD_INTERVAL_1_4:
813 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
814 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 3 << 2);
815 break;
816
817 case GUARD_INTERVAL_AUTO:
818 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 2);
819 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
820 break;
821
822 default:
823 return -EINVAL;
824 }
825
826 // set transmission mode
827 switch (fe_params->u.ofdm.transmission_mode) {
828 case TRANSMISSION_MODE_2K:
829 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
830 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0 << 4);
831 break;
832
833 case TRANSMISSION_MODE_8K:
834 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
835 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 1 << 4);
836 break;
837
838 case TRANSMISSION_MODE_AUTO:
839 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 4);
840 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0);
841 break;
842
843 default:
844 return -EINVAL;
845 }
846
847 // start the lock
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700848 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 case TDA1004X_DEMOD_TDA10045:
850 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
851 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 break;
853
854 case TDA1004X_DEMOD_TDA10046:
855 tda1004x_write_mask(state, TDA1004X_AUTO, 0x40, 0x40);
Hartmut Hackmann634623d2005-11-08 21:35:13 -0800856 msleep(1);
857 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 }
860
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700861 msleep(10);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
864}
865
866static int tda1004x_get_fe(struct dvb_frontend* fe, struct dvb_frontend_parameters *fe_params)
867{
868 struct tda1004x_state* state = fe->demodulator_priv;
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 dprintk("%s\n", __FUNCTION__);
871
872 // inversion status
873 fe_params->inversion = INVERSION_OFF;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700874 if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 fe_params->inversion = INVERSION_ON;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700876 if (state->config->invert)
877 fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 // bandwidth
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700880 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 case TDA1004X_DEMOD_TDA10045:
882 switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
883 case 0x14:
884 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
885 break;
886 case 0xdb:
887 fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
888 break;
889 case 0x4f:
890 fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
891 break;
892 }
893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 case TDA1004X_DEMOD_TDA10046:
895 switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200896 case 0x5c:
897 case 0x54:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
899 break;
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200900 case 0x6a:
901 case 0x60:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
903 break;
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200904 case 0x7b:
905 case 0x70:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
907 break;
908 }
909 break;
910 }
911
912 // FEC
913 fe_params->u.ofdm.code_rate_HP =
914 tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
915 fe_params->u.ofdm.code_rate_LP =
916 tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
917
918 // constellation
919 switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
920 case 0:
921 fe_params->u.ofdm.constellation = QPSK;
922 break;
923 case 1:
924 fe_params->u.ofdm.constellation = QAM_16;
925 break;
926 case 2:
927 fe_params->u.ofdm.constellation = QAM_64;
928 break;
929 }
930
931 // transmission mode
932 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700933 if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 // guard interval
937 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
938 case 0:
939 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
940 break;
941 case 1:
942 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
943 break;
944 case 2:
945 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
946 break;
947 case 3:
948 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
949 break;
950 }
951
952 // hierarchy
953 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
954 case 0:
955 fe_params->u.ofdm.hierarchy_information = HIERARCHY_NONE;
956 break;
957 case 1:
958 fe_params->u.ofdm.hierarchy_information = HIERARCHY_1;
959 break;
960 case 2:
961 fe_params->u.ofdm.hierarchy_information = HIERARCHY_2;
962 break;
963 case 3:
964 fe_params->u.ofdm.hierarchy_information = HIERARCHY_4;
965 break;
966 }
967
968 return 0;
969}
970
971static int tda1004x_read_status(struct dvb_frontend* fe, fe_status_t * fe_status)
972{
973 struct tda1004x_state* state = fe->demodulator_priv;
974 int status;
975 int cber;
976 int vber;
977
978 dprintk("%s\n", __FUNCTION__);
979
980 // read status
981 status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700982 if (status == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 // decode
986 *fe_status = 0;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700987 if (status & 4)
988 *fe_status |= FE_HAS_SIGNAL;
989 if (status & 2)
990 *fe_status |= FE_HAS_CARRIER;
991 if (status & 8)
992 *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 // if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
995 // is getting anything valid
996 if (!(*fe_status & FE_HAS_VITERBI)) {
997 // read the CBER
998 cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700999 if (cber == -1)
1000 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001002 if (status == -1)
1003 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 cber |= (status << 8);
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -02001005 // The address 0x20 should be read to cope with a TDA10046 bug
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1007
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001008 if (cber != 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 *fe_status |= FE_HAS_VITERBI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
1012 // if we DO have some valid VITERBI output, but don't already have SYNC
1013 // bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
1014 if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
1015 // read the VBER
1016 vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001017 if (vber == -1)
1018 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001020 if (status == -1)
1021 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 vber |= (status << 8);
1023 status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001024 if (status == -1)
1025 return -EIO;
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -02001026 vber |= (status & 0x0f) << 16;
1027 // The CVBER_LUT should be read to cope with TDA10046 hardware bug
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
1029
1030 // if RS has passed some valid TS packets, then we must be
1031 // getting some SYNC bytes
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001032 if (vber < 16632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *fe_status |= FE_HAS_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036 // success
1037 dprintk("%s: fe_status=0x%x\n", __FUNCTION__, *fe_status);
1038 return 0;
1039}
1040
1041static int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
1042{
1043 struct tda1004x_state* state = fe->demodulator_priv;
1044 int tmp;
1045 int reg = 0;
1046
1047 dprintk("%s\n", __FUNCTION__);
1048
1049 // determine the register to use
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001050 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 case TDA1004X_DEMOD_TDA10045:
1052 reg = TDA10045H_S_AGC;
1053 break;
1054
1055 case TDA1004X_DEMOD_TDA10046:
1056 reg = TDA10046H_AGC_IF_LEVEL;
1057 break;
1058 }
1059
1060 // read it
1061 tmp = tda1004x_read_byte(state, reg);
1062 if (tmp < 0)
1063 return -EIO;
1064
1065 *signal = (tmp << 8) | tmp;
1066 dprintk("%s: signal=0x%x\n", __FUNCTION__, *signal);
1067 return 0;
1068}
1069
1070static int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
1071{
1072 struct tda1004x_state* state = fe->demodulator_priv;
1073 int tmp;
1074
1075 dprintk("%s\n", __FUNCTION__);
1076
1077 // read it
1078 tmp = tda1004x_read_byte(state, TDA1004X_SNR);
1079 if (tmp < 0)
1080 return -EIO;
Andrew de Quinceyc2026b32005-09-09 13:02:33 -07001081 tmp = 255 - tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 *snr = ((tmp << 8) | tmp);
1084 dprintk("%s: snr=0x%x\n", __FUNCTION__, *snr);
1085 return 0;
1086}
1087
1088static int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
1089{
1090 struct tda1004x_state* state = fe->demodulator_priv;
1091 int tmp;
1092 int tmp2;
1093 int counter;
1094
1095 dprintk("%s\n", __FUNCTION__);
1096
1097 // read the UCBLOCKS and reset
1098 counter = 0;
1099 tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
1100 if (tmp < 0)
1101 return -EIO;
1102 tmp &= 0x7f;
1103 while (counter++ < 5) {
1104 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1105 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1106 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1107
1108 tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
1109 if (tmp2 < 0)
1110 return -EIO;
1111 tmp2 &= 0x7f;
1112 if ((tmp2 < tmp) || (tmp2 == 0))
1113 break;
1114 }
1115
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001116 if (tmp != 0x7f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *ucblocks = tmp;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001118 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *ucblocks = 0xffffffff;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 dprintk("%s: ucblocks=0x%x\n", __FUNCTION__, *ucblocks);
1122 return 0;
1123}
1124
1125static int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
1126{
1127 struct tda1004x_state* state = fe->demodulator_priv;
1128 int tmp;
1129
1130 dprintk("%s\n", __FUNCTION__);
1131
1132 // read it in
1133 tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001134 if (tmp < 0)
1135 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 *ber = tmp << 1;
1137 tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001138 if (tmp < 0)
1139 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 *ber |= (tmp << 9);
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -02001141 // The address 0x20 should be read to cope with a TDA10046 bug
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1143
1144 dprintk("%s: ber=0x%x\n", __FUNCTION__, *ber);
1145 return 0;
1146}
1147
1148static int tda1004x_sleep(struct dvb_frontend* fe)
1149{
1150 struct tda1004x_state* state = fe->demodulator_priv;
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -03001151 int gpio_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001153 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 case TDA1004X_DEMOD_TDA10045:
1155 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
1156 break;
1157
1158 case TDA1004X_DEMOD_TDA10046:
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -02001159 /* set outputs to tristate */
1160 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -03001161 /* invert GPIO 1 and 3 if desired*/
1162 gpio_conf = state->config->gpio_config;
1163 if (gpio_conf >= TDA10046_GP00_I)
1164 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f,
1165 (gpio_conf & 0x0f) ^ 0x0a);
1166
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -07001167 tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 break;
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 return 0;
1172}
1173
Andrew de Quincey74349be2006-04-18 17:47:10 -03001174static int tda1004x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
1175{
1176 struct tda1004x_state* state = fe->demodulator_priv;
1177
1178 if (enable) {
1179 return tda1004x_enable_tuner_i2c(state);
1180 } else {
1181 return tda1004x_disable_tuner_i2c(state);
1182 }
1183}
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1186{
1187 fesettings->min_delay_ms = 800;
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -07001188 /* Drift compensation makes no sense for DVB-T */
1189 fesettings->step_size = 0;
1190 fesettings->max_drift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return 0;
1192}
1193
Andrew de Quincey2a514de2006-08-08 09:10:09 -03001194static void tda1004x_release(struct dvb_frontend* fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001196 struct tda1004x_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 kfree(state);
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200static struct dvb_frontend_ops tda10045_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 .info = {
1202 .name = "Philips TDA10045H DVB-T",
1203 .type = FE_OFDM,
1204 .frequency_min = 51000000,
1205 .frequency_max = 858000000,
1206 .frequency_stepsize = 166667,
1207 .caps =
1208 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1209 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1210 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1211 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1212 },
1213
Andrew de Quincey2a514de2006-08-08 09:10:09 -03001214 .release = tda1004x_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 .init = tda10045_init,
1217 .sleep = tda1004x_sleep,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -03001218 .write = tda1004x_write,
Andrew de Quincey74349be2006-04-18 17:47:10 -03001219 .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 .set_frontend = tda1004x_set_fe,
1222 .get_frontend = tda1004x_get_fe,
1223 .get_tune_settings = tda1004x_get_tune_settings,
1224
1225 .read_status = tda1004x_read_status,
1226 .read_ber = tda1004x_read_ber,
1227 .read_signal_strength = tda1004x_read_signal_strength,
1228 .read_snr = tda1004x_read_snr,
1229 .read_ucblocks = tda1004x_read_ucblocks,
1230};
1231
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001232struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
1233 struct i2c_adapter* i2c)
1234{
1235 struct tda1004x_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001237 /* allocate memory for the internal state */
1238 state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1239 if (!state)
1240 return NULL;
1241
1242 /* setup the state */
1243 state->config = config;
1244 state->i2c = i2c;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001245 state->demod_type = TDA1004X_DEMOD_TDA10045;
1246
1247 /* check if the demod is there */
1248 if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x25) {
1249 kfree(state);
1250 return NULL;
1251 }
1252
1253 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001254 memcpy(&state->frontend.ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001255 state->frontend.demodulator_priv = state;
1256 return &state->frontend;
1257}
1258
1259static struct dvb_frontend_ops tda10046_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 .info = {
1261 .name = "Philips TDA10046H DVB-T",
1262 .type = FE_OFDM,
1263 .frequency_min = 51000000,
1264 .frequency_max = 858000000,
1265 .frequency_stepsize = 166667,
1266 .caps =
1267 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1268 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1269 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1270 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1271 },
1272
Andrew de Quincey2a514de2006-08-08 09:10:09 -03001273 .release = tda1004x_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 .init = tda10046_init,
1276 .sleep = tda1004x_sleep,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -03001277 .write = tda1004x_write,
Andrew de Quincey159f8a62006-04-19 18:31:03 -03001278 .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 .set_frontend = tda1004x_set_fe,
1281 .get_frontend = tda1004x_get_fe,
1282 .get_tune_settings = tda1004x_get_tune_settings,
1283
1284 .read_status = tda1004x_read_status,
1285 .read_ber = tda1004x_read_ber,
1286 .read_signal_strength = tda1004x_read_signal_strength,
1287 .read_snr = tda1004x_read_snr,
1288 .read_ucblocks = tda1004x_read_ucblocks,
1289};
1290
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001291struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
1292 struct i2c_adapter* i2c)
1293{
1294 struct tda1004x_state *state;
1295
1296 /* allocate memory for the internal state */
1297 state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1298 if (!state)
1299 return NULL;
1300
1301 /* setup the state */
1302 state->config = config;
1303 state->i2c = i2c;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001304 state->demod_type = TDA1004X_DEMOD_TDA10046;
1305
1306 /* check if the demod is there */
1307 if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x46) {
1308 kfree(state);
1309 return NULL;
1310 }
1311
1312 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001313 memcpy(&state->frontend.ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001314 state->frontend.demodulator_priv = state;
1315 return &state->frontend;
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318module_param(debug, int, 0644);
1319MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1320
1321MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
1322MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
1323MODULE_LICENSE("GPL");
1324
1325EXPORT_SYMBOL(tda10045_attach);
1326EXPORT_SYMBOL(tda10046_attach);