GNU Linux-libre 4.9.326-gnu1
[releases.git] / drivers / media / dvb-frontends / tda1004x.c
1   /*
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 /*(DEBLOBBED)*/
23
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/jiffies.h>
28 #include <linux/string.h>
29 #include <linux/slab.h>
30
31 #include "dvb_frontend.h"
32 #include "tda1004x.h"
33
34 static int debug;
35 #define dprintk(args...) \
36         do { \
37                 if (debug) printk(KERN_DEBUG "tda1004x: " args); \
38         } while (0)
39
40 #define TDA1004X_CHIPID          0x00
41 #define TDA1004X_AUTO            0x01
42 #define TDA1004X_IN_CONF1        0x02
43 #define TDA1004X_IN_CONF2        0x03
44 #define TDA1004X_OUT_CONF1       0x04
45 #define TDA1004X_OUT_CONF2       0x05
46 #define TDA1004X_STATUS_CD       0x06
47 #define TDA1004X_CONFC4          0x07
48 #define TDA1004X_DSSPARE2        0x0C
49 #define TDA10045H_CODE_IN        0x0D
50 #define TDA10045H_FWPAGE         0x0E
51 #define TDA1004X_SCAN_CPT        0x10
52 #define TDA1004X_DSP_CMD         0x11
53 #define TDA1004X_DSP_ARG         0x12
54 #define TDA1004X_DSP_DATA1       0x13
55 #define TDA1004X_DSP_DATA2       0x14
56 #define TDA1004X_CONFADC1        0x15
57 #define TDA1004X_CONFC1          0x16
58 #define TDA10045H_S_AGC          0x1a
59 #define TDA10046H_AGC_TUN_LEVEL  0x1a
60 #define TDA1004X_SNR             0x1c
61 #define TDA1004X_CONF_TS1        0x1e
62 #define TDA1004X_CONF_TS2        0x1f
63 #define TDA1004X_CBER_RESET      0x20
64 #define TDA1004X_CBER_MSB        0x21
65 #define TDA1004X_CBER_LSB        0x22
66 #define TDA1004X_CVBER_LUT       0x23
67 #define TDA1004X_VBER_MSB        0x24
68 #define TDA1004X_VBER_MID        0x25
69 #define TDA1004X_VBER_LSB        0x26
70 #define TDA1004X_UNCOR           0x27
71
72 #define TDA10045H_CONFPLL_P      0x2D
73 #define TDA10045H_CONFPLL_M_MSB  0x2E
74 #define TDA10045H_CONFPLL_M_LSB  0x2F
75 #define TDA10045H_CONFPLL_N      0x30
76
77 #define TDA10046H_CONFPLL1       0x2D
78 #define TDA10046H_CONFPLL2       0x2F
79 #define TDA10046H_CONFPLL3       0x30
80 #define TDA10046H_TIME_WREF1     0x31
81 #define TDA10046H_TIME_WREF2     0x32
82 #define TDA10046H_TIME_WREF3     0x33
83 #define TDA10046H_TIME_WREF4     0x34
84 #define TDA10046H_TIME_WREF5     0x35
85
86 #define TDA10045H_UNSURW_MSB     0x31
87 #define TDA10045H_UNSURW_LSB     0x32
88 #define TDA10045H_WREF_MSB       0x33
89 #define TDA10045H_WREF_MID       0x34
90 #define TDA10045H_WREF_LSB       0x35
91 #define TDA10045H_MUXOUT         0x36
92 #define TDA1004X_CONFADC2        0x37
93
94 #define TDA10045H_IOFFSET        0x38
95
96 #define TDA10046H_CONF_TRISTATE1 0x3B
97 #define TDA10046H_CONF_TRISTATE2 0x3C
98 #define TDA10046H_CONF_POLARITY  0x3D
99 #define TDA10046H_FREQ_OFFSET    0x3E
100 #define TDA10046H_GPIO_OUT_SEL   0x41
101 #define TDA10046H_GPIO_SELECT    0x42
102 #define TDA10046H_AGC_CONF       0x43
103 #define TDA10046H_AGC_THR        0x44
104 #define TDA10046H_AGC_RENORM     0x45
105 #define TDA10046H_AGC_GAINS      0x46
106 #define TDA10046H_AGC_TUN_MIN    0x47
107 #define TDA10046H_AGC_TUN_MAX    0x48
108 #define TDA10046H_AGC_IF_MIN     0x49
109 #define TDA10046H_AGC_IF_MAX     0x4A
110
111 #define TDA10046H_FREQ_PHY2_MSB  0x4D
112 #define TDA10046H_FREQ_PHY2_LSB  0x4E
113
114 #define TDA10046H_CVBER_CTRL     0x4F
115 #define TDA10046H_AGC_IF_LEVEL   0x52
116 #define TDA10046H_CODE_CPT       0x57
117 #define TDA10046H_CODE_IN        0x58
118
119
120 static int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
121 {
122         int ret;
123         u8 buf[] = { reg, data };
124         struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
125
126         dprintk("%s: reg=0x%x, data=0x%x\n", __func__, reg, data);
127
128         msg.addr = state->config->demod_address;
129         ret = i2c_transfer(state->i2c, &msg, 1);
130
131         if (ret != 1)
132                 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
133                         __func__, reg, data, ret);
134
135         dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
136                 reg, data, ret);
137         return (ret != 1) ? -1 : 0;
138 }
139
140 static int tda1004x_read_byte(struct tda1004x_state *state, int reg)
141 {
142         int ret;
143         u8 b0[] = { reg };
144         u8 b1[] = { 0 };
145         struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
146                                 { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
147
148         dprintk("%s: reg=0x%x\n", __func__, reg);
149
150         msg[0].addr = state->config->demod_address;
151         msg[1].addr = state->config->demod_address;
152         ret = i2c_transfer(state->i2c, msg, 2);
153
154         if (ret != 2) {
155                 dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg,
156                         ret);
157                 return -EINVAL;
158         }
159
160         dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
161                 reg, b1[0], ret);
162         return b1[0];
163 }
164
165 static int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
166 {
167         int val;
168         dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __func__, reg,
169                 mask, data);
170
171         // read a byte and check
172         val = tda1004x_read_byte(state, reg);
173         if (val < 0)
174                 return val;
175
176         // mask if off
177         val = val & ~mask;
178         val |= data & 0xff;
179
180         // write it out again
181         return tda1004x_write_byteI(state, reg, val);
182 }
183
184 static int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
185 {
186         int i;
187         int result;
188
189         dprintk("%s: reg=0x%x, len=0x%x\n", __func__, reg, len);
190
191         result = 0;
192         for (i = 0; i < len; i++) {
193                 result = tda1004x_write_byteI(state, reg + i, buf[i]);
194                 if (result != 0)
195                         break;
196         }
197
198         return result;
199 }
200
201 static int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
202 {
203         int result;
204         dprintk("%s\n", __func__);
205
206         result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
207         msleep(20);
208         return result;
209 }
210
211 static int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
212 {
213         dprintk("%s\n", __func__);
214
215         return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
216 }
217
218 static int tda10045h_set_bandwidth(struct tda1004x_state *state,
219                                    u32 bandwidth)
220 {
221         static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
222         static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
223         static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
224
225         switch (bandwidth) {
226         case 6000000:
227                 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
228                 break;
229
230         case 7000000:
231                 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
232                 break;
233
234         case 8000000:
235                 tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
236                 break;
237
238         default:
239                 return -EINVAL;
240         }
241
242         tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
243
244         return 0;
245 }
246
247 static int tda10046h_set_bandwidth(struct tda1004x_state *state,
248                                    u32 bandwidth)
249 {
250         static u8 bandwidth_6mhz_53M[] = { 0x7b, 0x2e, 0x11, 0xf0, 0xd2 };
251         static u8 bandwidth_7mhz_53M[] = { 0x6a, 0x02, 0x6a, 0x43, 0x9f };
252         static u8 bandwidth_8mhz_53M[] = { 0x5c, 0x32, 0xc2, 0x96, 0x6d };
253
254         static u8 bandwidth_6mhz_48M[] = { 0x70, 0x02, 0x49, 0x24, 0x92 };
255         static u8 bandwidth_7mhz_48M[] = { 0x60, 0x02, 0xaa, 0xaa, 0xab };
256         static u8 bandwidth_8mhz_48M[] = { 0x54, 0x03, 0x0c, 0x30, 0xc3 };
257         int tda10046_clk53m;
258
259         if ((state->config->if_freq == TDA10046_FREQ_045) ||
260             (state->config->if_freq == TDA10046_FREQ_052))
261                 tda10046_clk53m = 0;
262         else
263                 tda10046_clk53m = 1;
264         switch (bandwidth) {
265         case 6000000:
266                 if (tda10046_clk53m)
267                         tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_53M,
268                                                   sizeof(bandwidth_6mhz_53M));
269                 else
270                         tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_48M,
271                                                   sizeof(bandwidth_6mhz_48M));
272                 if (state->config->if_freq == TDA10046_FREQ_045) {
273                         tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
274                         tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xab);
275                 }
276                 break;
277
278         case 7000000:
279                 if (tda10046_clk53m)
280                         tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_53M,
281                                                   sizeof(bandwidth_7mhz_53M));
282                 else
283                         tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_48M,
284                                                   sizeof(bandwidth_7mhz_48M));
285                 if (state->config->if_freq == TDA10046_FREQ_045) {
286                         tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
287                         tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
288                 }
289                 break;
290
291         case 8000000:
292                 if (tda10046_clk53m)
293                         tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_53M,
294                                                   sizeof(bandwidth_8mhz_53M));
295                 else
296                         tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_48M,
297                                                   sizeof(bandwidth_8mhz_48M));
298                 if (state->config->if_freq == TDA10046_FREQ_045) {
299                         tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
300                         tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x55);
301                 }
302                 break;
303
304         default:
305                 return -EINVAL;
306         }
307
308         return 0;
309 }
310
311 static int tda1004x_do_upload(struct tda1004x_state *state,
312                               const unsigned char *mem, unsigned int len,
313                               u8 dspCodeCounterReg, u8 dspCodeInReg)
314 {
315         u8 buf[65];
316         struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
317         int tx_size;
318         int pos = 0;
319
320         /* clear code counter */
321         tda1004x_write_byteI(state, dspCodeCounterReg, 0);
322         fw_msg.addr = state->config->demod_address;
323
324         i2c_lock_adapter(state->i2c);
325         buf[0] = dspCodeInReg;
326         while (pos != len) {
327                 // work out how much to send this time
328                 tx_size = len - pos;
329                 if (tx_size > 0x10)
330                         tx_size = 0x10;
331
332                 // send the chunk
333                 memcpy(buf + 1, mem + pos, tx_size);
334                 fw_msg.len = tx_size + 1;
335                 if (__i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
336                         printk(KERN_ERR "tda1004x: Error during firmware upload\n");
337                         i2c_unlock_adapter(state->i2c);
338                         return -EIO;
339                 }
340                 pos += tx_size;
341
342                 dprintk("%s: fw_pos=0x%x\n", __func__, pos);
343         }
344         i2c_unlock_adapter(state->i2c);
345
346         /* give the DSP a chance to settle 03/10/05 Hac */
347         msleep(100);
348
349         return 0;
350 }
351
352 static int tda1004x_check_upload_ok(struct tda1004x_state *state)
353 {
354         u8 data1, data2;
355         unsigned long timeout;
356
357         if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
358                 timeout = jiffies + 2 * HZ;
359                 while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
360                         if (time_after(jiffies, timeout)) {
361                                 printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
362                                 break;
363                         }
364                         msleep(1);
365                 }
366         } else
367                 msleep(100);
368
369         // check upload was OK
370         tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
371         tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
372
373         data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
374         data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
375         if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
376                 printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
377                 return -EIO;
378         }
379         printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
380         return 0;
381 }
382
383 static int tda10045_fwupload(struct dvb_frontend* fe)
384 {
385         struct tda1004x_state* state = fe->demodulator_priv;
386         int ret;
387         const struct firmware *fw;
388
389         /* don't re-upload unless necessary */
390         if (tda1004x_check_upload_ok(state) == 0)
391                 return 0;
392
393         /* request the firmware, this will block until someone uploads it */
394         printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", "/*(DEBLOBBED)*/");
395         ret = state->config->request_firmware(fe, &fw, "/*(DEBLOBBED)*/");
396         if (ret) {
397                 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
398                 return ret;
399         }
400
401         /* reset chip */
402         tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
403         tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
404         tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
405         msleep(10);
406
407         /* set parameters */
408         tda10045h_set_bandwidth(state, 8000000);
409
410         ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
411         release_firmware(fw);
412         if (ret)
413                 return ret;
414         printk(KERN_INFO "tda1004x: firmware upload complete\n");
415
416         /* wait for DSP to initialise */
417         /* DSPREADY doesn't seem to work on the TDA10045H */
418         msleep(100);
419
420         return tda1004x_check_upload_ok(state);
421 }
422
423 static void tda10046_init_plls(struct dvb_frontend* fe)
424 {
425         struct tda1004x_state* state = fe->demodulator_priv;
426         int tda10046_clk53m;
427
428         if ((state->config->if_freq == TDA10046_FREQ_045) ||
429             (state->config->if_freq == TDA10046_FREQ_052))
430                 tda10046_clk53m = 0;
431         else
432                 tda10046_clk53m = 1;
433
434         tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
435         if(tda10046_clk53m) {
436                 printk(KERN_INFO "tda1004x: setting up plls for 53MHz sampling clock\n");
437                 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x08); // PLL M = 8
438         } else {
439                 printk(KERN_INFO "tda1004x: setting up plls for 48MHz sampling clock\n");
440                 tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x03); // PLL M = 3
441         }
442         if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
443                 dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __func__);
444                 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
445         } else {
446                 dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __func__);
447                 tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
448         }
449         if(tda10046_clk53m)
450                 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x67);
451         else
452                 tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x72);
453         /* Note clock frequency is handled implicitly */
454         switch (state->config->if_freq) {
455         case TDA10046_FREQ_045:
456                 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
457                 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
458                 break;
459         case TDA10046_FREQ_052:
460                 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
461                 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xc7);
462                 break;
463         case TDA10046_FREQ_3617:
464                 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
465                 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x59);
466                 break;
467         case TDA10046_FREQ_3613:
468                 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
469                 tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x3f);
470                 break;
471         }
472         tda10046h_set_bandwidth(state, 8000000); /* default bandwidth 8 MHz */
473         /* let the PLLs settle */
474         msleep(120);
475 }
476
477 static int tda10046_fwupload(struct dvb_frontend* fe)
478 {
479         struct tda1004x_state* state = fe->demodulator_priv;
480         int ret, confc4;
481         const struct firmware *fw;
482
483         /* reset + wake up chip */
484         if (state->config->xtal_freq == TDA10046_XTAL_4M) {
485                 confc4 = 0;
486         } else {
487                 dprintk("%s: 16MHz Xtal, reducing I2C speed\n", __func__);
488                 confc4 = 0x80;
489         }
490         tda1004x_write_byteI(state, TDA1004X_CONFC4, confc4);
491
492         tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
493         /* set GPIO 1 and 3 */
494         if (state->config->gpio_config != TDA10046_GPTRI) {
495                 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE2, 0x33);
496                 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f, state->config->gpio_config &0x0f);
497         }
498         /* let the clocks recover from sleep */
499         msleep(10);
500
501         /* The PLLs need to be reprogrammed after sleep */
502         tda10046_init_plls(fe);
503         tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0);
504
505         /* don't re-upload unless necessary */
506         if (tda1004x_check_upload_ok(state) == 0)
507                 return 0;
508
509         /*
510            For i2c normal work, we need to slow down the bus speed.
511            However, the slow down breaks the eeprom firmware load.
512            So, use normal speed for eeprom booting and then restore the
513            i2c speed after that. Tested with MSI TV @nyware A/D board,
514            that comes with firmware version 29 inside their eeprom.
515
516            It should also be noticed that no other I2C transfer should
517            be in course while booting from eeprom, otherwise, tda10046
518            goes into an instable state. So, proper locking are needed
519            at the i2c bus master.
520          */
521         printk(KERN_INFO "tda1004x: trying to boot from eeprom\n");
522         tda1004x_write_byteI(state, TDA1004X_CONFC4, 4);
523         msleep(300);
524         tda1004x_write_byteI(state, TDA1004X_CONFC4, confc4);
525
526         /* Checks if eeprom firmware went without troubles */
527         if (tda1004x_check_upload_ok(state) == 0)
528                 return 0;
529
530         /* eeprom firmware didn't work. Load one manually. */
531
532         if (state->config->request_firmware != NULL) {
533                 /* request the firmware, this will block until someone uploads it */
534                 printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
535                 ret = state->config->request_firmware(fe, &fw, "/*(DEBLOBBED)*/");
536                 if (ret) {
537                         /* remain compatible to old bug: try to load with tda10045 image name */
538                         ret = state->config->request_firmware(fe, &fw, "/*(DEBLOBBED)*/");
539                         if (ret) {
540                                 printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
541                                 return ret;
542                         } else {
543                                 printk(KERN_INFO "tda1004x: please rename the firmware file to %s\n",
544                                                   "/*(DEBLOBBED)*/");
545                         }
546                 }
547         } else {
548                 printk(KERN_ERR "tda1004x: no request function defined, can't upload from file\n");
549                 return -EIO;
550         }
551         tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
552         ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
553         release_firmware(fw);
554         return tda1004x_check_upload_ok(state);
555 }
556
557 static int tda1004x_encode_fec(int fec)
558 {
559         // convert known FEC values
560         switch (fec) {
561         case FEC_1_2:
562                 return 0;
563         case FEC_2_3:
564                 return 1;
565         case FEC_3_4:
566                 return 2;
567         case FEC_5_6:
568                 return 3;
569         case FEC_7_8:
570                 return 4;
571         }
572
573         // unsupported
574         return -EINVAL;
575 }
576
577 static int tda1004x_decode_fec(int tdafec)
578 {
579         // convert known FEC values
580         switch (tdafec) {
581         case 0:
582                 return FEC_1_2;
583         case 1:
584                 return FEC_2_3;
585         case 2:
586                 return FEC_3_4;
587         case 3:
588                 return FEC_5_6;
589         case 4:
590                 return FEC_7_8;
591         }
592
593         // unsupported
594         return -1;
595 }
596
597 static int tda1004x_write(struct dvb_frontend* fe, const u8 buf[], int len)
598 {
599         struct tda1004x_state* state = fe->demodulator_priv;
600
601         if (len != 2)
602                 return -EINVAL;
603
604         return tda1004x_write_byteI(state, buf[0], buf[1]);
605 }
606
607 static int tda10045_init(struct dvb_frontend* fe)
608 {
609         struct tda1004x_state* state = fe->demodulator_priv;
610
611         dprintk("%s\n", __func__);
612
613         if (tda10045_fwupload(fe)) {
614                 printk("tda1004x: firmware upload failed\n");
615                 return -EIO;
616         }
617
618         tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
619
620         // tda setup
621         tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
622         tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
623         tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
624         tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
625         tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
626         tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
627         tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
628         tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
629         tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
630         tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
631         tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
632
633         tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
634
635         return 0;
636 }
637
638 static int tda10046_init(struct dvb_frontend* fe)
639 {
640         struct tda1004x_state* state = fe->demodulator_priv;
641         dprintk("%s\n", __func__);
642
643         if (tda10046_fwupload(fe)) {
644                 printk("tda1004x: firmware upload failed\n");
645                 return -EIO;
646         }
647
648         // tda setup
649         tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
650         tda1004x_write_byteI(state, TDA1004X_AUTO, 0x87);    // 100 ppm crystal, select HP stream
651         tda1004x_write_byteI(state, TDA1004X_CONFC1, 0x88);      // enable pulse killer
652
653         switch (state->config->agc_config) {
654         case TDA10046_AGC_DEFAULT:
655                 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x00); // AGC setup
656                 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60);  // set AGC polarities
657                 break;
658         case TDA10046_AGC_IFO_AUTO_NEG:
659                 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
660                 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60);  // set AGC polarities
661                 break;
662         case TDA10046_AGC_IFO_AUTO_POS:
663                 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
664                 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x00);  // set AGC polarities
665                 break;
666         case TDA10046_AGC_TDA827X:
667                 tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02);   // AGC setup
668                 tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70);    // AGC Threshold
669                 tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x08); // Gain Renormalize
670                 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60);  // set AGC polarities
671                 break;
672         }
673         if (state->config->ts_mode == 0) {
674                 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x40);
675                 tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
676         } else {
677                 tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x80);
678                 tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x10,
679                                                         state->config->invert_oclk << 4);
680         }
681         tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
682         tda1004x_write_mask (state, TDA10046H_CONF_TRISTATE1, 0x3e, 0x38); // Turn IF AGC output on
683         tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0);    // }
684         tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
685         tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0);     // }
686         tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff);  // }
687         tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 0x12); // IF gain 2, TUN gain 1
688         tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
689         tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
690         tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
691         // tda1004x_write_mask(state, 0x50, 0x80, 0x80);         // handle out of guard echoes
692
693         return 0;
694 }
695
696 static int tda1004x_set_fe(struct dvb_frontend *fe)
697 {
698         struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
699         struct tda1004x_state* state = fe->demodulator_priv;
700         int tmp;
701         int inversion;
702
703         dprintk("%s\n", __func__);
704
705         if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
706                 // setup auto offset
707                 tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
708                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
709                 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
710
711                 // disable agc_conf[2]
712                 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
713         }
714
715         // set frequency
716         if (fe->ops.tuner_ops.set_params) {
717                 fe->ops.tuner_ops.set_params(fe);
718                 if (fe->ops.i2c_gate_ctrl)
719                         fe->ops.i2c_gate_ctrl(fe, 0);
720         }
721
722         // Hardcoded to use auto as much as possible on the TDA10045 as it
723         // is very unreliable if AUTO mode is _not_ used.
724         if (state->demod_type == TDA1004X_DEMOD_TDA10045) {
725                 fe_params->code_rate_HP = FEC_AUTO;
726                 fe_params->guard_interval = GUARD_INTERVAL_AUTO;
727                 fe_params->transmission_mode = TRANSMISSION_MODE_AUTO;
728         }
729
730         // Set standard params.. or put them to auto
731         if ((fe_params->code_rate_HP == FEC_AUTO) ||
732                 (fe_params->code_rate_LP == FEC_AUTO) ||
733                 (fe_params->modulation == QAM_AUTO) ||
734                 (fe_params->hierarchy == HIERARCHY_AUTO)) {
735                 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 1);        // enable auto
736                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x03, 0); /* turn off modulation bits */
737                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0); // turn off hierarchy bits
738                 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x3f, 0); // turn off FEC bits
739         } else {
740                 tda1004x_write_mask(state, TDA1004X_AUTO, 1, 0);        // disable auto
741
742                 // set HP FEC
743                 tmp = tda1004x_encode_fec(fe_params->code_rate_HP);
744                 if (tmp < 0)
745                         return tmp;
746                 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
747
748                 // set LP FEC
749                 tmp = tda1004x_encode_fec(fe_params->code_rate_LP);
750                 if (tmp < 0)
751                         return tmp;
752                 tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
753
754                 /* set modulation */
755                 switch (fe_params->modulation) {
756                 case QPSK:
757                         tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 0);
758                         break;
759
760                 case QAM_16:
761                         tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 1);
762                         break;
763
764                 case QAM_64:
765                         tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 2);
766                         break;
767
768                 default:
769                         return -EINVAL;
770                 }
771
772                 // set hierarchy
773                 switch (fe_params->hierarchy) {
774                 case HIERARCHY_NONE:
775                         tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0 << 5);
776                         break;
777
778                 case HIERARCHY_1:
779                         tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 1 << 5);
780                         break;
781
782                 case HIERARCHY_2:
783                         tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 2 << 5);
784                         break;
785
786                 case HIERARCHY_4:
787                         tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 3 << 5);
788                         break;
789
790                 default:
791                         return -EINVAL;
792                 }
793         }
794
795         // set bandwidth
796         switch (state->demod_type) {
797         case TDA1004X_DEMOD_TDA10045:
798                 tda10045h_set_bandwidth(state, fe_params->bandwidth_hz);
799                 break;
800
801         case TDA1004X_DEMOD_TDA10046:
802                 tda10046h_set_bandwidth(state, fe_params->bandwidth_hz);
803                 break;
804         }
805
806         // set inversion
807         inversion = fe_params->inversion;
808         if (state->config->invert)
809                 inversion = inversion ? INVERSION_OFF : INVERSION_ON;
810         switch (inversion) {
811         case INVERSION_OFF:
812                 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0);
813                 break;
814
815         case INVERSION_ON:
816                 tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0x20);
817                 break;
818
819         default:
820                 return -EINVAL;
821         }
822
823         // set guard interval
824         switch (fe_params->guard_interval) {
825         case GUARD_INTERVAL_1_32:
826                 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
827                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
828                 break;
829
830         case GUARD_INTERVAL_1_16:
831                 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
832                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 1 << 2);
833                 break;
834
835         case GUARD_INTERVAL_1_8:
836                 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
837                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 2 << 2);
838                 break;
839
840         case GUARD_INTERVAL_1_4:
841                 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
842                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 3 << 2);
843                 break;
844
845         case GUARD_INTERVAL_AUTO:
846                 tda1004x_write_mask(state, TDA1004X_AUTO, 2, 2);
847                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
848                 break;
849
850         default:
851                 return -EINVAL;
852         }
853
854         // set transmission mode
855         switch (fe_params->transmission_mode) {
856         case TRANSMISSION_MODE_2K:
857                 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
858                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0 << 4);
859                 break;
860
861         case TRANSMISSION_MODE_8K:
862                 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
863                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 1 << 4);
864                 break;
865
866         case TRANSMISSION_MODE_AUTO:
867                 tda1004x_write_mask(state, TDA1004X_AUTO, 4, 4);
868                 tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0);
869                 break;
870
871         default:
872                 return -EINVAL;
873         }
874
875         // start the lock
876         switch (state->demod_type) {
877         case TDA1004X_DEMOD_TDA10045:
878                 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
879                 tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
880                 break;
881
882         case TDA1004X_DEMOD_TDA10046:
883                 tda1004x_write_mask(state, TDA1004X_AUTO, 0x40, 0x40);
884                 msleep(1);
885                 tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 1);
886                 break;
887         }
888
889         msleep(10);
890
891         return 0;
892 }
893
894 static int tda1004x_get_fe(struct dvb_frontend *fe,
895                            struct dtv_frontend_properties *fe_params)
896 {
897         struct tda1004x_state* state = fe->demodulator_priv;
898         int status;
899
900         dprintk("%s\n", __func__);
901
902         status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
903         if (status == -1)
904                 return -EIO;
905
906         /* Only update the properties cache if device is locked */
907         if (!(status & 8))
908                 return 0;
909
910         // inversion status
911         fe_params->inversion = INVERSION_OFF;
912         if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
913                 fe_params->inversion = INVERSION_ON;
914         if (state->config->invert)
915                 fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
916
917         // bandwidth
918         switch (state->demod_type) {
919         case TDA1004X_DEMOD_TDA10045:
920                 switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
921                 case 0x14:
922                         fe_params->bandwidth_hz = 8000000;
923                         break;
924                 case 0xdb:
925                         fe_params->bandwidth_hz = 7000000;
926                         break;
927                 case 0x4f:
928                         fe_params->bandwidth_hz = 6000000;
929                         break;
930                 }
931                 break;
932         case TDA1004X_DEMOD_TDA10046:
933                 switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
934                 case 0x5c:
935                 case 0x54:
936                         fe_params->bandwidth_hz = 8000000;
937                         break;
938                 case 0x6a:
939                 case 0x60:
940                         fe_params->bandwidth_hz = 7000000;
941                         break;
942                 case 0x7b:
943                 case 0x70:
944                         fe_params->bandwidth_hz = 6000000;
945                         break;
946                 }
947                 break;
948         }
949
950         // FEC
951         fe_params->code_rate_HP =
952             tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
953         fe_params->code_rate_LP =
954             tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
955
956         /* modulation */
957         switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
958         case 0:
959                 fe_params->modulation = QPSK;
960                 break;
961         case 1:
962                 fe_params->modulation = QAM_16;
963                 break;
964         case 2:
965                 fe_params->modulation = QAM_64;
966                 break;
967         }
968
969         // transmission mode
970         fe_params->transmission_mode = TRANSMISSION_MODE_2K;
971         if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
972                 fe_params->transmission_mode = TRANSMISSION_MODE_8K;
973
974         // guard interval
975         switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
976         case 0:
977                 fe_params->guard_interval = GUARD_INTERVAL_1_32;
978                 break;
979         case 1:
980                 fe_params->guard_interval = GUARD_INTERVAL_1_16;
981                 break;
982         case 2:
983                 fe_params->guard_interval = GUARD_INTERVAL_1_8;
984                 break;
985         case 3:
986                 fe_params->guard_interval = GUARD_INTERVAL_1_4;
987                 break;
988         }
989
990         // hierarchy
991         switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
992         case 0:
993                 fe_params->hierarchy = HIERARCHY_NONE;
994                 break;
995         case 1:
996                 fe_params->hierarchy = HIERARCHY_1;
997                 break;
998         case 2:
999                 fe_params->hierarchy = HIERARCHY_2;
1000                 break;
1001         case 3:
1002                 fe_params->hierarchy = HIERARCHY_4;
1003                 break;
1004         }
1005
1006         return 0;
1007 }
1008
1009 static int tda1004x_read_status(struct dvb_frontend *fe,
1010                                 enum fe_status *fe_status)
1011 {
1012         struct tda1004x_state* state = fe->demodulator_priv;
1013         int status;
1014         int cber;
1015         int vber;
1016
1017         dprintk("%s\n", __func__);
1018
1019         // read status
1020         status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
1021         if (status == -1)
1022                 return -EIO;
1023
1024         // decode
1025         *fe_status = 0;
1026         if (status & 4)
1027                 *fe_status |= FE_HAS_SIGNAL;
1028         if (status & 2)
1029                 *fe_status |= FE_HAS_CARRIER;
1030         if (status & 8)
1031                 *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
1032
1033         // if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
1034         // is getting anything valid
1035         if (!(*fe_status & FE_HAS_VITERBI)) {
1036                 // read the CBER
1037                 cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
1038                 if (cber == -1)
1039                         return -EIO;
1040                 status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
1041                 if (status == -1)
1042                         return -EIO;
1043                 cber |= (status << 8);
1044                 // The address 0x20 should be read to cope with a TDA10046 bug
1045                 tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1046
1047                 if (cber != 65535)
1048                         *fe_status |= FE_HAS_VITERBI;
1049         }
1050
1051         // if we DO have some valid VITERBI output, but don't already have SYNC
1052         // bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
1053         if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
1054                 // read the VBER
1055                 vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
1056                 if (vber == -1)
1057                         return -EIO;
1058                 status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
1059                 if (status == -1)
1060                         return -EIO;
1061                 vber |= (status << 8);
1062                 status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
1063                 if (status == -1)
1064                         return -EIO;
1065                 vber |= (status & 0x0f) << 16;
1066                 // The CVBER_LUT should be read to cope with TDA10046 hardware bug
1067                 tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
1068
1069                 // if RS has passed some valid TS packets, then we must be
1070                 // getting some SYNC bytes
1071                 if (vber < 16632)
1072                         *fe_status |= FE_HAS_SYNC;
1073         }
1074
1075         // success
1076         dprintk("%s: fe_status=0x%x\n", __func__, *fe_status);
1077         return 0;
1078 }
1079
1080 static int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
1081 {
1082         struct tda1004x_state* state = fe->demodulator_priv;
1083         int tmp;
1084         int reg = 0;
1085
1086         dprintk("%s\n", __func__);
1087
1088         // determine the register to use
1089         switch (state->demod_type) {
1090         case TDA1004X_DEMOD_TDA10045:
1091                 reg = TDA10045H_S_AGC;
1092                 break;
1093
1094         case TDA1004X_DEMOD_TDA10046:
1095                 reg = TDA10046H_AGC_IF_LEVEL;
1096                 break;
1097         }
1098
1099         // read it
1100         tmp = tda1004x_read_byte(state, reg);
1101         if (tmp < 0)
1102                 return -EIO;
1103
1104         *signal = (tmp << 8) | tmp;
1105         dprintk("%s: signal=0x%x\n", __func__, *signal);
1106         return 0;
1107 }
1108
1109 static int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
1110 {
1111         struct tda1004x_state* state = fe->demodulator_priv;
1112         int tmp;
1113
1114         dprintk("%s\n", __func__);
1115
1116         // read it
1117         tmp = tda1004x_read_byte(state, TDA1004X_SNR);
1118         if (tmp < 0)
1119                 return -EIO;
1120         tmp = 255 - tmp;
1121
1122         *snr = ((tmp << 8) | tmp);
1123         dprintk("%s: snr=0x%x\n", __func__, *snr);
1124         return 0;
1125 }
1126
1127 static int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
1128 {
1129         struct tda1004x_state* state = fe->demodulator_priv;
1130         int tmp;
1131         int tmp2;
1132         int counter;
1133
1134         dprintk("%s\n", __func__);
1135
1136         // read the UCBLOCKS and reset
1137         counter = 0;
1138         tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
1139         if (tmp < 0)
1140                 return -EIO;
1141         tmp &= 0x7f;
1142         while (counter++ < 5) {
1143                 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1144                 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1145                 tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
1146
1147                 tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
1148                 if (tmp2 < 0)
1149                         return -EIO;
1150                 tmp2 &= 0x7f;
1151                 if ((tmp2 < tmp) || (tmp2 == 0))
1152                         break;
1153         }
1154
1155         if (tmp != 0x7f)
1156                 *ucblocks = tmp;
1157         else
1158                 *ucblocks = 0xffffffff;
1159
1160         dprintk("%s: ucblocks=0x%x\n", __func__, *ucblocks);
1161         return 0;
1162 }
1163
1164 static int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
1165 {
1166         struct tda1004x_state* state = fe->demodulator_priv;
1167         int tmp;
1168
1169         dprintk("%s\n", __func__);
1170
1171         // read it in
1172         tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
1173         if (tmp < 0)
1174                 return -EIO;
1175         *ber = tmp << 1;
1176         tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
1177         if (tmp < 0)
1178                 return -EIO;
1179         *ber |= (tmp << 9);
1180         // The address 0x20 should be read to cope with a TDA10046 bug
1181         tda1004x_read_byte(state, TDA1004X_CBER_RESET);
1182
1183         dprintk("%s: ber=0x%x\n", __func__, *ber);
1184         return 0;
1185 }
1186
1187 static int tda1004x_sleep(struct dvb_frontend* fe)
1188 {
1189         struct tda1004x_state* state = fe->demodulator_priv;
1190         int gpio_conf;
1191
1192         switch (state->demod_type) {
1193         case TDA1004X_DEMOD_TDA10045:
1194                 tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
1195                 break;
1196
1197         case TDA1004X_DEMOD_TDA10046:
1198                 /* set outputs to tristate */
1199                 tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
1200                 /* invert GPIO 1 and 3 if desired*/
1201                 gpio_conf = state->config->gpio_config;
1202                 if (gpio_conf >= TDA10046_GP00_I)
1203                         tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f,
1204                                                         (gpio_conf & 0x0f) ^ 0x0a);
1205
1206                 tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0xc0);
1207                 tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
1208                 break;
1209         }
1210
1211         return 0;
1212 }
1213
1214 static int tda1004x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
1215 {
1216         struct tda1004x_state* state = fe->demodulator_priv;
1217
1218         if (enable) {
1219                 return tda1004x_enable_tuner_i2c(state);
1220         } else {
1221                 return tda1004x_disable_tuner_i2c(state);
1222         }
1223 }
1224
1225 static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1226 {
1227         fesettings->min_delay_ms = 800;
1228         /* Drift compensation makes no sense for DVB-T */
1229         fesettings->step_size = 0;
1230         fesettings->max_drift = 0;
1231         return 0;
1232 }
1233
1234 static void tda1004x_release(struct dvb_frontend* fe)
1235 {
1236         struct tda1004x_state *state = fe->demodulator_priv;
1237         kfree(state);
1238 }
1239
1240 static struct dvb_frontend_ops tda10045_ops = {
1241         .delsys = { SYS_DVBT },
1242         .info = {
1243                 .name = "Philips TDA10045H DVB-T",
1244                 .frequency_min = 51000000,
1245                 .frequency_max = 858000000,
1246                 .frequency_stepsize = 166667,
1247                 .caps =
1248                     FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1249                     FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1250                     FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1251                     FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1252         },
1253
1254         .release = tda1004x_release,
1255
1256         .init = tda10045_init,
1257         .sleep = tda1004x_sleep,
1258         .write = tda1004x_write,
1259         .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
1260
1261         .set_frontend = tda1004x_set_fe,
1262         .get_frontend = tda1004x_get_fe,
1263         .get_tune_settings = tda1004x_get_tune_settings,
1264
1265         .read_status = tda1004x_read_status,
1266         .read_ber = tda1004x_read_ber,
1267         .read_signal_strength = tda1004x_read_signal_strength,
1268         .read_snr = tda1004x_read_snr,
1269         .read_ucblocks = tda1004x_read_ucblocks,
1270 };
1271
1272 struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
1273                                      struct i2c_adapter* i2c)
1274 {
1275         struct tda1004x_state *state;
1276         int id;
1277
1278         /* allocate memory for the internal state */
1279         state = kzalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1280         if (!state) {
1281                 printk(KERN_ERR "Can't allocate memory for tda10045 state\n");
1282                 return NULL;
1283         }
1284
1285         /* setup the state */
1286         state->config = config;
1287         state->i2c = i2c;
1288         state->demod_type = TDA1004X_DEMOD_TDA10045;
1289
1290         /* check if the demod is there */
1291         id = tda1004x_read_byte(state, TDA1004X_CHIPID);
1292         if (id < 0) {
1293                 printk(KERN_ERR "tda10045: chip is not answering. Giving up.\n");
1294                 kfree(state);
1295                 return NULL;
1296         }
1297
1298         if (id != 0x25) {
1299                 printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
1300                 kfree(state);
1301                 return NULL;
1302         }
1303
1304         /* create dvb_frontend */
1305         memcpy(&state->frontend.ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
1306         state->frontend.demodulator_priv = state;
1307         return &state->frontend;
1308 }
1309
1310 static struct dvb_frontend_ops tda10046_ops = {
1311         .delsys = { SYS_DVBT },
1312         .info = {
1313                 .name = "Philips TDA10046H DVB-T",
1314                 .frequency_min = 51000000,
1315                 .frequency_max = 858000000,
1316                 .frequency_stepsize = 166667,
1317                 .caps =
1318                     FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1319                     FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1320                     FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
1321                     FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1322         },
1323
1324         .release = tda1004x_release,
1325
1326         .init = tda10046_init,
1327         .sleep = tda1004x_sleep,
1328         .write = tda1004x_write,
1329         .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
1330
1331         .set_frontend = tda1004x_set_fe,
1332         .get_frontend = tda1004x_get_fe,
1333         .get_tune_settings = tda1004x_get_tune_settings,
1334
1335         .read_status = tda1004x_read_status,
1336         .read_ber = tda1004x_read_ber,
1337         .read_signal_strength = tda1004x_read_signal_strength,
1338         .read_snr = tda1004x_read_snr,
1339         .read_ucblocks = tda1004x_read_ucblocks,
1340 };
1341
1342 struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
1343                                      struct i2c_adapter* i2c)
1344 {
1345         struct tda1004x_state *state;
1346         int id;
1347
1348         /* allocate memory for the internal state */
1349         state = kzalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
1350         if (!state) {
1351                 printk(KERN_ERR "Can't allocate memory for tda10046 state\n");
1352                 return NULL;
1353         }
1354
1355         /* setup the state */
1356         state->config = config;
1357         state->i2c = i2c;
1358         state->demod_type = TDA1004X_DEMOD_TDA10046;
1359
1360         /* check if the demod is there */
1361         id = tda1004x_read_byte(state, TDA1004X_CHIPID);
1362         if (id < 0) {
1363                 printk(KERN_ERR "tda10046: chip is not answering. Giving up.\n");
1364                 kfree(state);
1365                 return NULL;
1366         }
1367         if (id != 0x46) {
1368                 printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
1369                 kfree(state);
1370                 return NULL;
1371         }
1372
1373         /* create dvb_frontend */
1374         memcpy(&state->frontend.ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
1375         state->frontend.demodulator_priv = state;
1376         return &state->frontend;
1377 }
1378
1379 module_param(debug, int, 0644);
1380 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1381
1382 MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
1383 MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
1384 MODULE_LICENSE("GPL");
1385
1386 EXPORT_SYMBOL(tda10045_attach);
1387 EXPORT_SYMBOL(tda10046_attach);