blob: 3993d1ce334aa9dd3ffe12a4332baddef71da0b6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080035#include <linux/jiffies.h>
36#include <linux/string.h>
37#include <linux/slab.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "dvb_frontend.h"
40#include "tda1004x.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int debug;
43#define dprintk(args...) \
44 do { \
45 if (debug) printk(KERN_DEBUG "tda1004x: " args); \
46 } while (0)
47
48#define TDA1004X_CHIPID 0x00
49#define TDA1004X_AUTO 0x01
50#define TDA1004X_IN_CONF1 0x02
51#define TDA1004X_IN_CONF2 0x03
52#define TDA1004X_OUT_CONF1 0x04
53#define TDA1004X_OUT_CONF2 0x05
54#define TDA1004X_STATUS_CD 0x06
55#define TDA1004X_CONFC4 0x07
56#define TDA1004X_DSSPARE2 0x0C
57#define TDA10045H_CODE_IN 0x0D
58#define TDA10045H_FWPAGE 0x0E
59#define TDA1004X_SCAN_CPT 0x10
60#define TDA1004X_DSP_CMD 0x11
61#define TDA1004X_DSP_ARG 0x12
62#define TDA1004X_DSP_DATA1 0x13
63#define TDA1004X_DSP_DATA2 0x14
64#define TDA1004X_CONFADC1 0x15
65#define TDA1004X_CONFC1 0x16
66#define TDA10045H_S_AGC 0x1a
67#define TDA10046H_AGC_TUN_LEVEL 0x1a
68#define TDA1004X_SNR 0x1c
69#define TDA1004X_CONF_TS1 0x1e
70#define TDA1004X_CONF_TS2 0x1f
71#define TDA1004X_CBER_RESET 0x20
72#define TDA1004X_CBER_MSB 0x21
73#define TDA1004X_CBER_LSB 0x22
74#define TDA1004X_CVBER_LUT 0x23
75#define TDA1004X_VBER_MSB 0x24
76#define TDA1004X_VBER_MID 0x25
77#define TDA1004X_VBER_LSB 0x26
78#define TDA1004X_UNCOR 0x27
79
80#define TDA10045H_CONFPLL_P 0x2D
81#define TDA10045H_CONFPLL_M_MSB 0x2E
82#define TDA10045H_CONFPLL_M_LSB 0x2F
83#define TDA10045H_CONFPLL_N 0x30
84
85#define TDA10046H_CONFPLL1 0x2D
86#define TDA10046H_CONFPLL2 0x2F
87#define TDA10046H_CONFPLL3 0x30
88#define TDA10046H_TIME_WREF1 0x31
89#define TDA10046H_TIME_WREF2 0x32
90#define TDA10046H_TIME_WREF3 0x33
91#define TDA10046H_TIME_WREF4 0x34
92#define TDA10046H_TIME_WREF5 0x35
93
94#define TDA10045H_UNSURW_MSB 0x31
95#define TDA10045H_UNSURW_LSB 0x32
96#define TDA10045H_WREF_MSB 0x33
97#define TDA10045H_WREF_MID 0x34
98#define TDA10045H_WREF_LSB 0x35
99#define TDA10045H_MUXOUT 0x36
100#define TDA1004X_CONFADC2 0x37
101
102#define TDA10045H_IOFFSET 0x38
103
104#define TDA10046H_CONF_TRISTATE1 0x3B
105#define TDA10046H_CONF_TRISTATE2 0x3C
106#define TDA10046H_CONF_POLARITY 0x3D
107#define TDA10046H_FREQ_OFFSET 0x3E
108#define TDA10046H_GPIO_OUT_SEL 0x41
109#define TDA10046H_GPIO_SELECT 0x42
110#define TDA10046H_AGC_CONF 0x43
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700111#define TDA10046H_AGC_THR 0x44
112#define TDA10046H_AGC_RENORM 0x45
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define TDA10046H_AGC_GAINS 0x46
114#define TDA10046H_AGC_TUN_MIN 0x47
115#define TDA10046H_AGC_TUN_MAX 0x48
116#define TDA10046H_AGC_IF_MIN 0x49
117#define TDA10046H_AGC_IF_MAX 0x4A
118
119#define TDA10046H_FREQ_PHY2_MSB 0x4D
120#define TDA10046H_FREQ_PHY2_LSB 0x4E
121
122#define TDA10046H_CVBER_CTRL 0x4F
123#define TDA10046H_AGC_IF_LEVEL 0x52
124#define TDA10046H_CODE_CPT 0x57
125#define TDA10046H_CODE_IN 0x58
126
127
128static int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
129{
130 int ret;
131 u8 buf[] = { reg, data };
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700132 struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300134 dprintk("%s: reg=0x%x, data=0x%x\n", __func__, reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 msg.addr = state->config->demod_address;
137 ret = i2c_transfer(state->i2c, &msg, 1);
138
139 if (ret != 1)
140 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300141 __func__, reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300143 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 reg, data, ret);
145 return (ret != 1) ? -1 : 0;
146}
147
148static int tda1004x_read_byte(struct tda1004x_state *state, int reg)
149{
150 int ret;
151 u8 b0[] = { reg };
152 u8 b1[] = { 0 };
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700153 struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
154 { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300156 dprintk("%s: reg=0x%x\n", __func__, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 msg[0].addr = state->config->demod_address;
159 msg[1].addr = state->config->demod_address;
160 ret = i2c_transfer(state->i2c, msg, 2);
161
162 if (ret != 2) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300163 dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ret);
165 return -1;
166 }
167
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300168 dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 reg, b1[0], ret);
170 return b1[0];
171}
172
173static int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
174{
175 int val;
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300176 dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __func__, reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mask, data);
178
179 // read a byte and check
180 val = tda1004x_read_byte(state, reg);
181 if (val < 0)
182 return val;
183
184 // mask if off
185 val = val & ~mask;
186 val |= data & 0xff;
187
188 // write it out again
189 return tda1004x_write_byteI(state, reg, val);
190}
191
192static int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
193{
194 int i;
195 int result;
196
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300197 dprintk("%s: reg=0x%x, len=0x%x\n", __func__, reg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 result = 0;
200 for (i = 0; i < len; i++) {
201 result = tda1004x_write_byteI(state, reg + i, buf[i]);
202 if (result != 0)
203 break;
204 }
205
206 return result;
207}
208
209static int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
210{
211 int result;
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300212 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200215 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return result;
217}
218
219static int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
220{
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300221 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
224}
225
226static int tda10045h_set_bandwidth(struct tda1004x_state *state,
227 fe_bandwidth_t bandwidth)
228{
229 static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
230 static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
231 static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
232
233 switch (bandwidth) {
234 case BANDWIDTH_6_MHZ:
235 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
236 break;
237
238 case BANDWIDTH_7_MHZ:
239 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
240 break;
241
242 case BANDWIDTH_8_MHZ:
243 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
244 break;
245
246 default:
247 return -EINVAL;
248 }
249
250 tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
251
252 return 0;
253}
254
255static int tda10046h_set_bandwidth(struct tda1004x_state *state,
256 fe_bandwidth_t bandwidth)
257{
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200258 static u8 bandwidth_6mhz_53M[] = { 0x7b, 0x2e, 0x11, 0xf0, 0xd2 };
259 static u8 bandwidth_7mhz_53M[] = { 0x6a, 0x02, 0x6a, 0x43, 0x9f };
260 static u8 bandwidth_8mhz_53M[] = { 0x5c, 0x32, 0xc2, 0x96, 0x6d };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200262 static u8 bandwidth_6mhz_48M[] = { 0x70, 0x02, 0x49, 0x24, 0x92 };
263 static u8 bandwidth_7mhz_48M[] = { 0x60, 0x02, 0xaa, 0xaa, 0xab };
264 static u8 bandwidth_8mhz_48M[] = { 0x54, 0x03, 0x0c, 0x30, 0xc3 };
265 int tda10046_clk53m;
266
267 if ((state->config->if_freq == TDA10046_FREQ_045) ||
268 (state->config->if_freq == TDA10046_FREQ_052))
269 tda10046_clk53m = 0;
270 else
271 tda10046_clk53m = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 switch (bandwidth) {
273 case BANDWIDTH_6_MHZ:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200274 if (tda10046_clk53m)
275 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_53M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200276 sizeof(bandwidth_6mhz_53M));
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200277 else
278 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_48M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200279 sizeof(bandwidth_6mhz_48M));
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700280 if (state->config->if_freq == TDA10046_FREQ_045) {
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200281 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
282 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xab);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285
286 case BANDWIDTH_7_MHZ:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200287 if (tda10046_clk53m)
288 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_53M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200289 sizeof(bandwidth_7mhz_53M));
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200290 else
291 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_48M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200292 sizeof(bandwidth_7mhz_48M));
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700293 if (state->config->if_freq == TDA10046_FREQ_045) {
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200294 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
295 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
298
299 case BANDWIDTH_8_MHZ:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200300 if (tda10046_clk53m)
301 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_53M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200302 sizeof(bandwidth_8mhz_53M));
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200303 else
304 tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_48M,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200305 sizeof(bandwidth_8mhz_48M));
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700306 if (state->config->if_freq == TDA10046_FREQ_045) {
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200307 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
308 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x55);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311
312 default:
313 return -EINVAL;
314 }
315
316 return 0;
317}
318
319static int tda1004x_do_upload(struct tda1004x_state *state,
320 unsigned char *mem, unsigned int len,
321 u8 dspCodeCounterReg, u8 dspCodeInReg)
322{
323 u8 buf[65];
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700324 struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int tx_size;
326 int pos = 0;
327
328 /* clear code counter */
329 tda1004x_write_byteI(state, dspCodeCounterReg, 0);
330 fw_msg.addr = state->config->demod_address;
331
332 buf[0] = dspCodeInReg;
333 while (pos != len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 // work out how much to send this time
335 tx_size = len - pos;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700336 if (tx_size > 0x10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 tx_size = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 // send the chunk
340 memcpy(buf + 1, mem + pos, tx_size);
341 fw_msg.len = tx_size + 1;
342 if (i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700343 printk(KERN_ERR "tda1004x: Error during firmware upload\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EIO;
345 }
346 pos += tx_size;
347
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300348 dprintk("%s: fw_pos=0x%x\n", __func__, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700350 // give the DSP a chance to settle 03/10/05 Hac
351 msleep(100);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
355
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700356static int tda1004x_check_upload_ok(struct tda1004x_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 u8 data1, data2;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700359 unsigned long timeout;
360
361 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
362 timeout = jiffies + 2 * HZ;
363 while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
364 if (time_after(jiffies, timeout)) {
365 printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
366 break;
367 }
368 msleep(1);
369 }
370 } else
371 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 // check upload was OK
374 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
375 tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
376
377 data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
378 data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
Hartmut Hackmann3faadbb2005-07-07 17:57:42 -0700379 if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700380 printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -EIO;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700382 }
383 printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
385}
386
387static int tda10045_fwupload(struct dvb_frontend* fe)
388{
389 struct tda1004x_state* state = fe->demodulator_priv;
390 int ret;
391 const struct firmware *fw;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* don't re-upload unless necessary */
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700394 if (tda1004x_check_upload_ok(state) == 0)
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* request the firmware, this will block until someone uploads it */
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700398 printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", TDA10045_DEFAULT_FIRMWARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
400 if (ret) {
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700401 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return ret;
403 }
404
405 /* reset chip */
406 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
407 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
408 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
409 msleep(10);
410
411 /* set parameters */
412 tda10045h_set_bandwidth(state, BANDWIDTH_8_MHZ);
413
414 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
Anssi Hannula0c744b02005-07-07 17:57:42 -0700415 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (ret)
417 return ret;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700418 printk(KERN_INFO "tda1004x: firmware upload complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* wait for DSP to initialise */
421 /* DSPREADY doesn't seem to work on the TDA10045H */
422 msleep(100);
423
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700424 return tda1004x_check_upload_ok(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700427static void tda10046_init_plls(struct dvb_frontend* fe)
Johannes Stezenbach71e34202005-05-16 21:54:36 -0700428{
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700429 struct tda1004x_state* state = fe->demodulator_priv;
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200430 int tda10046_clk53m;
431
432 if ((state->config->if_freq == TDA10046_FREQ_045) ||
433 (state->config->if_freq == TDA10046_FREQ_052))
434 tda10046_clk53m = 0;
435 else
436 tda10046_clk53m = 1;
Johannes Stezenbach71e34202005-05-16 21:54:36 -0700437
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700438 tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200439 if(tda10046_clk53m) {
440 printk(KERN_INFO "tda1004x: setting up plls for 53MHz sampling clock\n");
441 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x08); // PLL M = 8
442 } else {
443 printk(KERN_INFO "tda1004x: setting up plls for 48MHz sampling clock\n");
444 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x03); // PLL M = 3
445 }
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700446 if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300447 dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __func__);
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700448 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
449 } else {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300450 dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __func__);
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700451 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
Johannes Stezenbach71e34202005-05-16 21:54:36 -0700452 }
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200453 if(tda10046_clk53m)
454 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x67);
455 else
456 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x72);
457 /* Note clock frequency is handled implicitly */
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700458 switch (state->config->if_freq) {
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700459 case TDA10046_FREQ_045:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200460 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
461 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700462 break;
463 case TDA10046_FREQ_052:
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200464 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
465 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xc7);
466 break;
467 case TDA10046_FREQ_3617:
468 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
469 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x59);
470 break;
471 case TDA10046_FREQ_3613:
472 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
473 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x3f);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700474 break;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700475 }
476 tda10046h_set_bandwidth(state, BANDWIDTH_8_MHZ); // default bandwidth 8 MHz
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200477 /* let the PLLs settle */
478 msleep(120);
Johannes Stezenbach71e34202005-05-16 21:54:36 -0700479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static int tda10046_fwupload(struct dvb_frontend* fe)
482{
483 struct tda1004x_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int ret;
485 const struct firmware *fw;
486
487 /* reset + wake up chip */
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200488 if (state->config->xtal_freq == TDA10046_XTAL_4M) {
489 tda1004x_write_byteI(state, TDA1004X_CONFC4, 0);
490 } else {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300491 dprintk("%s: 16MHz Xtal, reducing I2C speed\n", __func__);
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200492 tda1004x_write_byteI(state, TDA1004X_CONFC4, 0x80);
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300495 /* set GPIO 1 and 3 */
496 if (state->config->gpio_config != TDA10046_GPTRI) {
497 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE2, 0x33);
498 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f, state->config->gpio_config &0x0f);
499 }
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700500 /* let the clocks recover from sleep */
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300501 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200503 /* The PLLs need to be reprogrammed after sleep */
504 tda10046_init_plls(fe);
Hartmut Hackmann68717582007-04-27 12:31:15 -0300505 tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0);
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200506
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
Hartmut Hackmannf4546e72007-04-27 12:31:13 -0300518 if (state->config->request_firmware != NULL) {
519 /* request the firmware, this will block until someone uploads it */
520 printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
521 ret = state->config->request_firmware(fe, &fw, TDA10046_DEFAULT_FIRMWARE);
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700522 if (ret) {
Hartmut Hackmannf4546e72007-04-27 12:31:13 -0300523 /* remain compatible to old bug: try to load with tda10045 image name */
524 ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
525 if (ret) {
526 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
527 return ret;
528 } else {
529 printk(KERN_INFO "tda1004x: please rename the firmware file to %s\n",
530 TDA10046_DEFAULT_FIRMWARE);
531 }
532 }
533 } else {
534 printk(KERN_ERR "tda1004x: no request function defined, can't upload from file\n");
535 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300537 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
538 ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
539 release_firmware(fw);
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700540 return tda1004x_check_upload_ok(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543static int tda1004x_encode_fec(int fec)
544{
545 // convert known FEC values
546 switch (fec) {
547 case FEC_1_2:
548 return 0;
549 case FEC_2_3:
550 return 1;
551 case FEC_3_4:
552 return 2;
553 case FEC_5_6:
554 return 3;
555 case FEC_7_8:
556 return 4;
557 }
558
559 // unsupported
560 return -EINVAL;
561}
562
563static int tda1004x_decode_fec(int tdafec)
564{
565 // convert known FEC values
566 switch (tdafec) {
567 case 0:
568 return FEC_1_2;
569 case 1:
570 return FEC_2_3;
571 case 2:
572 return FEC_3_4;
573 case 3:
574 return FEC_5_6;
575 case 4:
576 return FEC_7_8;
577 }
578
579 // unsupported
580 return -1;
581}
582
Adrian Bunk34630402007-02-06 21:50:36 -0300583static int tda1004x_write(struct dvb_frontend* fe, u8 *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct tda1004x_state* state = fe->demodulator_priv;
586
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300587 if (len != 2)
588 return -EINVAL;
589
590 return tda1004x_write_byteI(state, buf[0], buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static int tda10045_init(struct dvb_frontend* fe)
594{
595 struct tda1004x_state* state = fe->demodulator_priv;
596
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300597 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (tda10045_fwupload(fe)) {
600 printk("tda1004x: firmware upload failed\n");
601 return -EIO;
602 }
603
604 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 // tda setup
607 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
608 tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
609 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
610 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
611 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
612 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
613 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
614 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
615 tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
616 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
617 tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
618
619 tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622}
623
624static int tda10046_init(struct dvb_frontend* fe)
625{
626 struct tda1004x_state* state = fe->demodulator_priv;
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300627 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (tda10046_fwupload(fe)) {
630 printk("tda1004x: firmware upload failed\n");
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700631 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 // tda setup
635 tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200636 tda1004x_write_byteI(state, TDA1004X_AUTO, 0x87); // 100 ppm crystal, select HP stream
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200637 tda1004x_write_byteI(state, TDA1004X_CONFC1, 0x88); // enable pulse killer
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700638
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700639 switch (state->config->agc_config) {
640 case TDA10046_AGC_DEFAULT:
641 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x00); // AGC setup
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300642 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700643 break;
644 case TDA10046_AGC_IFO_AUTO_NEG:
645 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300646 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700647 break;
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700648 case TDA10046_AGC_IFO_AUTO_POS:
649 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300650 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x00); // set AGC polarities
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700651 break;
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300652 case TDA10046_AGC_TDA827X:
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -0700653 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02); // AGC setup
654 tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70); // AGC Threshold
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200655 tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x08); // Gain Renormalize
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -0300656 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
Hartmut Hackmann550a9a52006-11-15 21:31:54 -0300657 break;
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700658 }
Hartmut Hackmann08cdf942007-03-18 19:23:20 -0300659 if (state->config->ts_mode == 0) {
660 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x40);
661 tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
662 } else {
663 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x80);
664 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x10,
665 state->config->invert_oclk << 4);
666 }
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200667 tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
Hartmut Hackmann08cdf942007-03-18 19:23:20 -0300668 tda1004x_write_mask (state, TDA10046H_CONF_TRISTATE1, 0x3e, 0x38); // Turn IF AGC output on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0); // }
670 tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
671 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0); // }
672 tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff); // }
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200673 tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 0x12); // IF gain 2, TUN gain 1
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700674 tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700676 tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -0200677 // tda1004x_write_mask(state, 0x50, 0x80, 0x80); // handle out of guard echoes
Hartmut Hackmannecb60de2005-07-07 17:57:40 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return 0;
680}
681
682static int tda1004x_set_fe(struct dvb_frontend* fe,
683 struct dvb_frontend_parameters *fe_params)
684{
685 struct tda1004x_state* state = fe->demodulator_priv;
686 int tmp;
687 int inversion;
688
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300689 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
692 // setup auto offset
693 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
694 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
695 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
696
697 // disable agc_conf[2]
698 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
699 }
700
701 // set frequency
Patrick Boettcherdea74862006-05-14 05:01:31 -0300702 if (fe->ops.tuner_ops.set_params) {
703 fe->ops.tuner_ops.set_params(fe, fe_params);
Hartmut Hackmannede22002007-04-27 12:31:32 -0300704 if (fe->ops.i2c_gate_ctrl)
705 fe->ops.i2c_gate_ctrl(fe, 0);
Hartmut Hackmann634623d2005-11-08 21:35:13 -0800706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 // Hardcoded to use auto as much as possible on the TDA10045 as it
709 // is very unreliable if AUTO mode is _not_ used.
710 if (state->demod_type == TDA1004X_DEMOD_TDA10045) {
711 fe_params->u.ofdm.code_rate_HP = FEC_AUTO;
712 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
713 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
714 }
715
716 // Set standard params.. or put them to auto
717 if ((fe_params->u.ofdm.code_rate_HP == FEC_AUTO) ||
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200718 (fe_params->u.ofdm.code_rate_LP == FEC_AUTO) ||
719 (fe_params->u.ofdm.constellation == QAM_AUTO) ||
720 (fe_params->u.ofdm.hierarchy_information == HIERARCHY_AUTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 1); // enable auto
722 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x03, 0); // turn off constellation bits
723 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0); // turn off hierarchy bits
724 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x3f, 0); // turn off FEC bits
725 } else {
726 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 0); // disable auto
727
728 // set HP FEC
729 tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_HP);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700730 if (tmp < 0)
731 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
733
734 // set LP FEC
735 tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_LP);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700736 if (tmp < 0)
737 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
739
740 // set constellation
741 switch (fe_params->u.ofdm.constellation) {
742 case QPSK:
743 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 0);
744 break;
745
746 case QAM_16:
747 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 1);
748 break;
749
750 case QAM_64:
751 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 2);
752 break;
753
754 default:
755 return -EINVAL;
756 }
757
758 // set hierarchy
759 switch (fe_params->u.ofdm.hierarchy_information) {
760 case HIERARCHY_NONE:
761 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0 << 5);
762 break;
763
764 case HIERARCHY_1:
765 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 1 << 5);
766 break;
767
768 case HIERARCHY_2:
769 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 2 << 5);
770 break;
771
772 case HIERARCHY_4:
773 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 3 << 5);
774 break;
775
776 default:
777 return -EINVAL;
778 }
779 }
780
781 // set bandwidth
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700782 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 case TDA1004X_DEMOD_TDA10045:
784 tda10045h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
785 break;
786
787 case TDA1004X_DEMOD_TDA10046:
788 tda10046h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
789 break;
790 }
791
792 // set inversion
793 inversion = fe_params->inversion;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700794 if (state->config->invert)
795 inversion = inversion ? INVERSION_OFF : INVERSION_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 switch (inversion) {
797 case INVERSION_OFF:
798 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0);
799 break;
800
801 case INVERSION_ON:
802 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0x20);
803 break;
804
805 default:
806 return -EINVAL;
807 }
808
809 // set guard interval
810 switch (fe_params->u.ofdm.guard_interval) {
811 case GUARD_INTERVAL_1_32:
812 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
813 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
814 break;
815
816 case GUARD_INTERVAL_1_16:
817 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
818 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 1 << 2);
819 break;
820
821 case GUARD_INTERVAL_1_8:
822 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
823 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 2 << 2);
824 break;
825
826 case GUARD_INTERVAL_1_4:
827 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
828 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 3 << 2);
829 break;
830
831 case GUARD_INTERVAL_AUTO:
832 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 2);
833 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
834 break;
835
836 default:
837 return -EINVAL;
838 }
839
840 // set transmission mode
841 switch (fe_params->u.ofdm.transmission_mode) {
842 case TRANSMISSION_MODE_2K:
843 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
844 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0 << 4);
845 break;
846
847 case TRANSMISSION_MODE_8K:
848 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
849 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 1 << 4);
850 break;
851
852 case TRANSMISSION_MODE_AUTO:
853 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 4);
854 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0);
855 break;
856
857 default:
858 return -EINVAL;
859 }
860
861 // start the lock
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700862 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 case TDA1004X_DEMOD_TDA10045:
864 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
865 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 break;
867
868 case TDA1004X_DEMOD_TDA10046:
869 tda1004x_write_mask(state, TDA1004X_AUTO, 0x40, 0x40);
Hartmut Hackmann634623d2005-11-08 21:35:13 -0800870 msleep(1);
871 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
873 }
874
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700875 msleep(10);
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 0;
878}
879
880static int tda1004x_get_fe(struct dvb_frontend* fe, struct dvb_frontend_parameters *fe_params)
881{
882 struct tda1004x_state* state = fe->demodulator_priv;
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200883
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300884 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 // inversion status
887 fe_params->inversion = INVERSION_OFF;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700888 if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 fe_params->inversion = INVERSION_ON;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700890 if (state->config->invert)
891 fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 // bandwidth
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700894 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 case TDA1004X_DEMOD_TDA10045:
896 switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
897 case 0x14:
898 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
899 break;
900 case 0xdb:
901 fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
902 break;
903 case 0x4f:
904 fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
905 break;
906 }
907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 case TDA1004X_DEMOD_TDA10046:
909 switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200910 case 0x5c:
911 case 0x54:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
913 break;
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200914 case 0x6a:
915 case 0x60:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
917 break;
Hartmut Hackmann8a8e9c22006-01-09 15:25:04 -0200918 case 0x7b:
919 case 0x70:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
921 break;
922 }
923 break;
924 }
925
926 // FEC
927 fe_params->u.ofdm.code_rate_HP =
928 tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
929 fe_params->u.ofdm.code_rate_LP =
930 tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
931
932 // constellation
933 switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
934 case 0:
935 fe_params->u.ofdm.constellation = QPSK;
936 break;
937 case 1:
938 fe_params->u.ofdm.constellation = QAM_16;
939 break;
940 case 2:
941 fe_params->u.ofdm.constellation = QAM_64;
942 break;
943 }
944
945 // transmission mode
946 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700947 if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 // guard interval
951 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
952 case 0:
953 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
954 break;
955 case 1:
956 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
957 break;
958 case 2:
959 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
960 break;
961 case 3:
962 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
963 break;
964 }
965
966 // hierarchy
967 switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
968 case 0:
969 fe_params->u.ofdm.hierarchy_information = HIERARCHY_NONE;
970 break;
971 case 1:
972 fe_params->u.ofdm.hierarchy_information = HIERARCHY_1;
973 break;
974 case 2:
975 fe_params->u.ofdm.hierarchy_information = HIERARCHY_2;
976 break;
977 case 3:
978 fe_params->u.ofdm.hierarchy_information = HIERARCHY_4;
979 break;
980 }
981
982 return 0;
983}
984
985static int tda1004x_read_status(struct dvb_frontend* fe, fe_status_t * fe_status)
986{
987 struct tda1004x_state* state = fe->demodulator_priv;
988 int status;
989 int cber;
990 int vber;
991
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300992 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 // read status
995 status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -0700996 if (status == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 // decode
1000 *fe_status = 0;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001001 if (status & 4)
1002 *fe_status |= FE_HAS_SIGNAL;
1003 if (status & 2)
1004 *fe_status |= FE_HAS_CARRIER;
1005 if (status & 8)
1006 *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 // if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
1009 // is getting anything valid
1010 if (!(*fe_status & FE_HAS_VITERBI)) {
1011 // read the CBER
1012 cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001013 if (cber == -1)
1014 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001016 if (status == -1)
1017 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 cber |= (status << 8);
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -02001019 // The address 0x20 should be read to cope with a TDA10046 bug
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1021
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001022 if (cber != 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 *fe_status |= FE_HAS_VITERBI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
1026 // if we DO have some valid VITERBI output, but don't already have SYNC
1027 // bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
1028 if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
1029 // read the VBER
1030 vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001031 if (vber == -1)
1032 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001034 if (status == -1)
1035 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 vber |= (status << 8);
1037 status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001038 if (status == -1)
1039 return -EIO;
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -02001040 vber |= (status & 0x0f) << 16;
1041 // The CVBER_LUT should be read to cope with TDA10046 hardware bug
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
1043
1044 // if RS has passed some valid TS packets, then we must be
1045 // getting some SYNC bytes
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001046 if (vber < 16632)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 *fe_status |= FE_HAS_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
1050 // success
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001051 dprintk("%s: fe_status=0x%x\n", __func__, *fe_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
1053}
1054
1055static int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
1056{
1057 struct tda1004x_state* state = fe->demodulator_priv;
1058 int tmp;
1059 int reg = 0;
1060
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001061 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 // determine the register to use
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001064 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 case TDA1004X_DEMOD_TDA10045:
1066 reg = TDA10045H_S_AGC;
1067 break;
1068
1069 case TDA1004X_DEMOD_TDA10046:
1070 reg = TDA10046H_AGC_IF_LEVEL;
1071 break;
1072 }
1073
1074 // read it
1075 tmp = tda1004x_read_byte(state, reg);
1076 if (tmp < 0)
1077 return -EIO;
1078
1079 *signal = (tmp << 8) | tmp;
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001080 dprintk("%s: signal=0x%x\n", __func__, *signal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082}
1083
1084static int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
1085{
1086 struct tda1004x_state* state = fe->demodulator_priv;
1087 int tmp;
1088
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001089 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 // read it
1092 tmp = tda1004x_read_byte(state, TDA1004X_SNR);
1093 if (tmp < 0)
1094 return -EIO;
Andrew de Quinceyc2026b32005-09-09 13:02:33 -07001095 tmp = 255 - tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 *snr = ((tmp << 8) | tmp);
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001098 dprintk("%s: snr=0x%x\n", __func__, *snr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return 0;
1100}
1101
1102static int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
1103{
1104 struct tda1004x_state* state = fe->demodulator_priv;
1105 int tmp;
1106 int tmp2;
1107 int counter;
1108
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001109 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 // read the UCBLOCKS and reset
1112 counter = 0;
1113 tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
1114 if (tmp < 0)
1115 return -EIO;
1116 tmp &= 0x7f;
1117 while (counter++ < 5) {
1118 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1119 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1120 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1121
1122 tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
1123 if (tmp2 < 0)
1124 return -EIO;
1125 tmp2 &= 0x7f;
1126 if ((tmp2 < tmp) || (tmp2 == 0))
1127 break;
1128 }
1129
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001130 if (tmp != 0x7f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 *ucblocks = tmp;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001132 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *ucblocks = 0xffffffff;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001134
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001135 dprintk("%s: ucblocks=0x%x\n", __func__, *ucblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return 0;
1137}
1138
1139static int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
1140{
1141 struct tda1004x_state* state = fe->demodulator_priv;
1142 int tmp;
1143
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001144 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 // read it in
1147 tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001148 if (tmp < 0)
1149 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 *ber = tmp << 1;
1151 tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001152 if (tmp < 0)
1153 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 *ber |= (tmp << 9);
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -02001155 // The address 0x20 should be read to cope with a TDA10046 bug
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1157
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001158 dprintk("%s: ber=0x%x\n", __func__, *ber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
1160}
1161
1162static int tda1004x_sleep(struct dvb_frontend* fe)
1163{
1164 struct tda1004x_state* state = fe->demodulator_priv;
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -03001165 int gpio_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001167 switch (state->demod_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 case TDA1004X_DEMOD_TDA10045:
1169 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
1170 break;
1171
1172 case TDA1004X_DEMOD_TDA10046:
Hartmut Hackmann0eb3de22006-02-07 06:49:10 -02001173 /* set outputs to tristate */
1174 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
Hartmut Hackmann1bb0e862007-04-27 12:31:10 -03001175 /* invert GPIO 1 and 3 if desired*/
1176 gpio_conf = state->config->gpio_config;
1177 if (gpio_conf >= TDA10046_GP00_I)
1178 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f,
1179 (gpio_conf & 0x0f) ^ 0x0a);
1180
Hartmut Hackmann68717582007-04-27 12:31:15 -03001181 tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0xc0);
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -07001182 tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 return 0;
1187}
1188
Andrew de Quincey74349bef2006-04-18 17:47:10 -03001189static int tda1004x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
1190{
1191 struct tda1004x_state* state = fe->demodulator_priv;
1192
1193 if (enable) {
1194 return tda1004x_enable_tuner_i2c(state);
1195 } else {
1196 return tda1004x_disable_tuner_i2c(state);
1197 }
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1201{
1202 fesettings->min_delay_ms = 800;
Hartmut Hackmannf03cbea2005-07-07 17:57:43 -07001203 /* Drift compensation makes no sense for DVB-T */
1204 fesettings->step_size = 0;
1205 fesettings->max_drift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return 0;
1207}
1208
Andrew de Quincey2a514de2006-08-08 09:10:09 -03001209static void tda1004x_release(struct dvb_frontend* fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001211 struct tda1004x_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 kfree(state);
1213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215static struct dvb_frontend_ops tda10045_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 .info = {
1217 .name = "Philips TDA10045H DVB-T",
1218 .type = FE_OFDM,
1219 .frequency_min = 51000000,
1220 .frequency_max = 858000000,
1221 .frequency_stepsize = 166667,
1222 .caps =
1223 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1224 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1225 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1226 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1227 },
1228
Andrew de Quincey2a514de2006-08-08 09:10:09 -03001229 .release = tda1004x_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 .init = tda10045_init,
1232 .sleep = tda1004x_sleep,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -03001233 .write = tda1004x_write,
Andrew de Quincey74349bef2006-04-18 17:47:10 -03001234 .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 .set_frontend = tda1004x_set_fe,
1237 .get_frontend = tda1004x_get_fe,
1238 .get_tune_settings = tda1004x_get_tune_settings,
1239
1240 .read_status = tda1004x_read_status,
1241 .read_ber = tda1004x_read_ber,
1242 .read_signal_strength = tda1004x_read_signal_strength,
1243 .read_snr = tda1004x_read_snr,
1244 .read_ucblocks = tda1004x_read_ucblocks,
1245};
1246
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001247struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
1248 struct i2c_adapter* i2c)
1249{
1250 struct tda1004x_state *state;
Mauro Carvalho Chehabbc36ec72008-06-14 10:44:04 -03001251 u8 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001253 /* allocate memory for the internal state */
1254 state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
Mauro Carvalho Chehabbc36ec72008-06-14 10:44:04 -03001255 if (!state) {
1256 printk(KERN_ERR "Can't alocate memory for tda10045 state\n");
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001257 return NULL;
Mauro Carvalho Chehabbc36ec72008-06-14 10:44:04 -03001258 }
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001259
1260 /* setup the state */
1261 state->config = config;
1262 state->i2c = i2c;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001263 state->demod_type = TDA1004X_DEMOD_TDA10045;
1264
1265 /* check if the demod is there */
Mauro Carvalho Chehabbc36ec72008-06-14 10:44:04 -03001266 id = tda1004x_read_byte(state, TDA1004X_CHIPID);
1267 if (id != 0x25) {
1268 printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001269 kfree(state);
1270 return NULL;
1271 }
1272
1273 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001274 memcpy(&state->frontend.ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001275 state->frontend.demodulator_priv = state;
1276 return &state->frontend;
1277}
1278
1279static struct dvb_frontend_ops tda10046_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 .info = {
1281 .name = "Philips TDA10046H DVB-T",
1282 .type = FE_OFDM,
1283 .frequency_min = 51000000,
1284 .frequency_max = 858000000,
1285 .frequency_stepsize = 166667,
1286 .caps =
1287 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1288 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1289 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1290 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1291 },
1292
Andrew de Quincey2a514de2006-08-08 09:10:09 -03001293 .release = tda1004x_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 .init = tda10046_init,
1296 .sleep = tda1004x_sleep,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -03001297 .write = tda1004x_write,
Andrew de Quincey159f8a62006-04-19 18:31:03 -03001298 .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 .set_frontend = tda1004x_set_fe,
1301 .get_frontend = tda1004x_get_fe,
1302 .get_tune_settings = tda1004x_get_tune_settings,
1303
1304 .read_status = tda1004x_read_status,
1305 .read_ber = tda1004x_read_ber,
1306 .read_signal_strength = tda1004x_read_signal_strength,
1307 .read_snr = tda1004x_read_snr,
1308 .read_ucblocks = tda1004x_read_ucblocks,
1309};
1310
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001311struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
1312 struct i2c_adapter* i2c)
1313{
1314 struct tda1004x_state *state;
Mauro Carvalho Chehabbc36ec72008-06-14 10:44:04 -03001315 u8 id;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001316
1317 /* allocate memory for the internal state */
1318 state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
Mauro Carvalho Chehabbc36ec72008-06-14 10:44:04 -03001319 if (!state) {
1320 printk(KERN_ERR "Can't alocate memory for tda10046 state\n");
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001321 return NULL;
Mauro Carvalho Chehabbc36ec72008-06-14 10:44:04 -03001322 }
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001323
1324 /* setup the state */
1325 state->config = config;
1326 state->i2c = i2c;
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001327 state->demod_type = TDA1004X_DEMOD_TDA10046;
1328
1329 /* check if the demod is there */
Mauro Carvalho Chehabbc36ec72008-06-14 10:44:04 -03001330 id = tda1004x_read_byte(state, TDA1004X_CHIPID);
1331 if (id != 0x46) {
1332 printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001333 kfree(state);
1334 return NULL;
1335 }
1336
1337 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001338 memcpy(&state->frontend.ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
Johannes Stezenbach7f5e02d2005-05-16 21:54:30 -07001339 state->frontend.demodulator_priv = state;
1340 return &state->frontend;
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343module_param(debug, int, 0644);
1344MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1345
1346MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
1347MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
1348MODULE_LICENSE("GPL");
1349
1350EXPORT_SYMBOL(tda10045_attach);
1351EXPORT_SYMBOL(tda10046_attach);