GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / media / dvb-frontends / itd1000.c
1 /*
2  *  Driver for the Integrant ITD1000 "Zero-IF Tuner IC for Direct Broadcast Satellite"
3  *
4  *  Copyright (c) 2007-8 Patrick Boettcher <pb@linuxtv.org>
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 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/delay.h>
25 #include <linux/dvb/frontend.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28
29 #include "dvb_frontend.h"
30
31 #include "itd1000.h"
32 #include "itd1000_priv.h"
33
34 /* Max transfer size done by I2C transfer functions */
35 #define MAX_XFER_SIZE  64
36
37 static int debug;
38 module_param(debug, int, 0644);
39 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
40
41 #define itd_dbg(args...)  do { \
42         if (debug) { \
43                 printk(KERN_DEBUG   "ITD1000: " args);\
44         } \
45 } while (0)
46
47 #define itd_warn(args...) do { \
48         printk(KERN_WARNING "ITD1000: " args); \
49 } while (0)
50
51 #define itd_info(args...) do { \
52         printk(KERN_INFO    "ITD1000: " args); \
53 } while (0)
54
55 /* don't write more than one byte with flexcop behind */
56 static int itd1000_write_regs(struct itd1000_state *state, u8 reg, u8 v[], u8 len)
57 {
58         u8 buf[MAX_XFER_SIZE];
59         struct i2c_msg msg = {
60                 .addr = state->cfg->i2c_address, .flags = 0, .buf = buf, .len = len+1
61         };
62
63         if (1 + len > sizeof(buf)) {
64                 printk(KERN_WARNING
65                        "itd1000: i2c wr reg=%04x: len=%d is too big!\n",
66                        reg, len);
67                 return -EINVAL;
68         }
69
70         buf[0] = reg;
71         memcpy(&buf[1], v, len);
72
73         /* itd_dbg("wr %02x: %02x\n", reg, v[0]); */
74
75         if (i2c_transfer(state->i2c, &msg, 1) != 1) {
76                 printk(KERN_WARNING "itd1000 I2C write failed\n");
77                 return -EREMOTEIO;
78         }
79         return 0;
80 }
81
82 static int itd1000_read_reg(struct itd1000_state *state, u8 reg)
83 {
84         u8 val;
85         struct i2c_msg msg[2] = {
86                 { .addr = state->cfg->i2c_address, .flags = 0,        .buf = &reg, .len = 1 },
87                 { .addr = state->cfg->i2c_address, .flags = I2C_M_RD, .buf = &val, .len = 1 },
88         };
89
90         /* ugly flexcop workaround */
91         itd1000_write_regs(state, (reg - 1) & 0xff, &state->shadow[(reg - 1) & 0xff], 1);
92
93         if (i2c_transfer(state->i2c, msg, 2) != 2) {
94                 itd_warn("itd1000 I2C read failed\n");
95                 return -EREMOTEIO;
96         }
97         return val;
98 }
99
100 static inline int itd1000_write_reg(struct itd1000_state *state, u8 r, u8 v)
101 {
102         u8 tmp = v; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
103         int ret = itd1000_write_regs(state, r, &tmp, 1);
104         state->shadow[r] = tmp;
105         return ret;
106 }
107
108
109 static struct {
110         u32 symbol_rate;
111         u8  pgaext  : 4; /* PLLFH */
112         u8  bbgvmin : 4; /* BBGVMIN */
113 } itd1000_lpf_pga[] = {
114         {        0, 0x8, 0x3 },
115         {  5200000, 0x8, 0x3 },
116         { 12200000, 0x4, 0x3 },
117         { 15400000, 0x2, 0x3 },
118         { 19800000, 0x2, 0x3 },
119         { 21500000, 0x2, 0x3 },
120         { 24500000, 0x2, 0x3 },
121         { 28400000, 0x2, 0x3 },
122         { 33400000, 0x2, 0x3 },
123         { 34400000, 0x1, 0x4 },
124         { 34400000, 0x1, 0x4 },
125         { 38400000, 0x1, 0x4 },
126         { 38400000, 0x1, 0x4 },
127         { 40400000, 0x1, 0x4 },
128         { 45400000, 0x1, 0x4 },
129 };
130
131 static void itd1000_set_lpf_bw(struct itd1000_state *state, u32 symbol_rate)
132 {
133         u8 i;
134         u8 con1    = itd1000_read_reg(state, CON1)    & 0xfd;
135         u8 pllfh   = itd1000_read_reg(state, PLLFH)   & 0x0f;
136         u8 bbgvmin = itd1000_read_reg(state, BBGVMIN) & 0xf0;
137         u8 bw      = itd1000_read_reg(state, BW)      & 0xf0;
138
139         itd_dbg("symbol_rate = %d\n", symbol_rate);
140
141         /* not sure what is that ? - starting to download the table */
142         itd1000_write_reg(state, CON1, con1 | (1 << 1));
143
144         for (i = 0; i < ARRAY_SIZE(itd1000_lpf_pga); i++)
145                 if (symbol_rate < itd1000_lpf_pga[i].symbol_rate) {
146                         itd_dbg("symrate: index: %d pgaext: %x, bbgvmin: %x\n", i, itd1000_lpf_pga[i].pgaext, itd1000_lpf_pga[i].bbgvmin);
147                         itd1000_write_reg(state, PLLFH,   pllfh | (itd1000_lpf_pga[i].pgaext << 4));
148                         itd1000_write_reg(state, BBGVMIN, bbgvmin | (itd1000_lpf_pga[i].bbgvmin));
149                         itd1000_write_reg(state, BW,      bw | (i & 0x0f));
150                         break;
151                 }
152
153         itd1000_write_reg(state, CON1, con1 | (0 << 1));
154 }
155
156 static struct {
157         u8 vcorg;
158         u32 fmax_rg;
159 } itd1000_vcorg[] = {
160         {  1,  920000 },
161         {  2,  971000 },
162         {  3, 1031000 },
163         {  4, 1091000 },
164         {  5, 1171000 },
165         {  6, 1281000 },
166         {  7, 1381000 },
167         {  8,  500000 },        /* this is intentional. */
168         {  9, 1451000 },
169         { 10, 1531000 },
170         { 11, 1631000 },
171         { 12, 1741000 },
172         { 13, 1891000 },
173         { 14, 2071000 },
174         { 15, 2250000 },
175 };
176
177 static void itd1000_set_vco(struct itd1000_state *state, u32 freq_khz)
178 {
179         u8 i;
180         u8 gvbb_i2c     = itd1000_read_reg(state, GVBB_I2C) & 0xbf;
181         u8 vco_chp1_i2c = itd1000_read_reg(state, VCO_CHP1_I2C) & 0x0f;
182         u8 adcout;
183
184         /* reserved bit again (reset ?) */
185         itd1000_write_reg(state, GVBB_I2C, gvbb_i2c | (1 << 6));
186
187         for (i = 0; i < ARRAY_SIZE(itd1000_vcorg); i++) {
188                 if (freq_khz < itd1000_vcorg[i].fmax_rg) {
189                         itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | (itd1000_vcorg[i].vcorg << 4));
190                         msleep(1);
191
192                         adcout = itd1000_read_reg(state, PLLLOCK) & 0x0f;
193
194                         itd_dbg("VCO: %dkHz: %d -> ADCOUT: %d %02x\n", freq_khz, itd1000_vcorg[i].vcorg, adcout, vco_chp1_i2c);
195
196                         if (adcout > 13) {
197                                 if (!(itd1000_vcorg[i].vcorg == 7 || itd1000_vcorg[i].vcorg == 15))
198                                         itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | ((itd1000_vcorg[i].vcorg + 1) << 4));
199                         } else if (adcout < 2) {
200                                 if (!(itd1000_vcorg[i].vcorg == 1 || itd1000_vcorg[i].vcorg == 9))
201                                         itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | ((itd1000_vcorg[i].vcorg - 1) << 4));
202                         }
203                         break;
204                 }
205         }
206 }
207
208 static const struct {
209         u32 freq;
210         u8 values[10]; /* RFTR, RFST1 - RFST9 */
211 } itd1000_fre_values[] = {
212         { 1075000, { 0x59, 0x1d, 0x1c, 0x17, 0x16, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
213         { 1250000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
214         { 1450000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
215         { 1650000, { 0x69, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
216         { 1750000, { 0x69, 0x1e, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
217         { 1850000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
218         { 1900000, { 0x69, 0x1d, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
219         { 1950000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0d, 0x0b, 0x0a } },
220         { 2050000, { 0x69, 0x1e, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0b, 0x0a } },
221         { 2150000, { 0x69, 0x1d, 0x1c, 0x17, 0x15, 0x14, 0x13, 0x0f, 0x0e, 0x0b } }
222 };
223
224
225 #define FREF 16
226
227 static void itd1000_set_lo(struct itd1000_state *state, u32 freq_khz)
228 {
229         int i, j;
230         u32 plln, pllf;
231         u64 tmp;
232
233         plln = (freq_khz * 1000) / 2 / FREF;
234
235         /* Compute the factional part times 1000 */
236         tmp  = plln % 1000000;
237         plln /= 1000000;
238
239         tmp *= 1048576;
240         do_div(tmp, 1000000);
241         pllf = (u32) tmp;
242
243         state->frequency = ((plln * 1000) + (pllf * 1000)/1048576) * 2*FREF;
244         itd_dbg("frequency: %dkHz (wanted) %dkHz (set), PLLF = %d, PLLN = %d\n", freq_khz, state->frequency, pllf, plln);
245
246         itd1000_write_reg(state, PLLNH, 0x80); /* PLLNH */
247         itd1000_write_reg(state, PLLNL, plln & 0xff);
248         itd1000_write_reg(state, PLLFH, (itd1000_read_reg(state, PLLFH) & 0xf0) | ((pllf >> 16) & 0x0f));
249         itd1000_write_reg(state, PLLFM, (pllf >> 8) & 0xff);
250         itd1000_write_reg(state, PLLFL, (pllf >> 0) & 0xff);
251
252         for (i = 0; i < ARRAY_SIZE(itd1000_fre_values); i++) {
253                 if (freq_khz <= itd1000_fre_values[i].freq) {
254                         itd_dbg("fre_values: %d\n", i);
255                         itd1000_write_reg(state, RFTR, itd1000_fre_values[i].values[0]);
256                         for (j = 0; j < 9; j++)
257                                 itd1000_write_reg(state, RFST1+j, itd1000_fre_values[i].values[j+1]);
258                         break;
259                 }
260         }
261
262         itd1000_set_vco(state, freq_khz);
263 }
264
265 static int itd1000_set_parameters(struct dvb_frontend *fe)
266 {
267         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
268         struct itd1000_state *state = fe->tuner_priv;
269         u8 pllcon1;
270
271         itd1000_set_lo(state, c->frequency);
272         itd1000_set_lpf_bw(state, c->symbol_rate);
273
274         pllcon1 = itd1000_read_reg(state, PLLCON1) & 0x7f;
275         itd1000_write_reg(state, PLLCON1, pllcon1 | (1 << 7));
276         itd1000_write_reg(state, PLLCON1, pllcon1);
277
278         return 0;
279 }
280
281 static int itd1000_get_frequency(struct dvb_frontend *fe, u32 *frequency)
282 {
283         struct itd1000_state *state = fe->tuner_priv;
284         *frequency = state->frequency;
285         return 0;
286 }
287
288 static int itd1000_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
289 {
290         return 0;
291 }
292
293 static u8 itd1000_init_tab[][2] = {
294         { PLLCON1,       0x65 }, /* Register does not change */
295         { PLLNH,         0x80 }, /* Bits [7:6] do not change */
296         { RESERVED_0X6D, 0x3b },
297         { VCO_CHP2_I2C,  0x12 },
298         { 0x72,          0xf9 }, /* No such regsister defined */
299         { RESERVED_0X73, 0xff },
300         { RESERVED_0X74, 0xb2 },
301         { RESERVED_0X75, 0xc7 },
302         { EXTGVBBRF,     0xf0 },
303         { DIVAGCCK,      0x80 },
304         { BBTR,          0xa0 },
305         { RESERVED_0X7E, 0x4f },
306         { 0x82,          0x88 }, /* No such regsister defined */
307         { 0x83,          0x80 }, /* No such regsister defined */
308         { 0x84,          0x80 }, /* No such regsister defined */
309         { RESERVED_0X85, 0x74 },
310         { RESERVED_0X86, 0xff },
311         { RESERVED_0X88, 0x02 },
312         { RESERVED_0X89, 0x16 },
313         { RFST0,         0x1f },
314         { RESERVED_0X94, 0x66 },
315         { RESERVED_0X95, 0x66 },
316         { RESERVED_0X96, 0x77 },
317         { RESERVED_0X97, 0x99 },
318         { RESERVED_0X98, 0xff },
319         { RESERVED_0X99, 0xfc },
320         { RESERVED_0X9A, 0xba },
321         { RESERVED_0X9B, 0xaa },
322 };
323
324 static u8 itd1000_reinit_tab[][2] = {
325         { VCO_CHP1_I2C, 0x8a },
326         { BW,           0x87 },
327         { GVBB_I2C,     0x03 },
328         { BBGVMIN,      0x03 },
329         { CON1,         0x2e },
330 };
331
332
333 static int itd1000_init(struct dvb_frontend *fe)
334 {
335         struct itd1000_state *state = fe->tuner_priv;
336         int i;
337
338         for (i = 0; i < ARRAY_SIZE(itd1000_init_tab); i++)
339                 itd1000_write_reg(state, itd1000_init_tab[i][0], itd1000_init_tab[i][1]);
340
341         for (i = 0; i < ARRAY_SIZE(itd1000_reinit_tab); i++)
342                 itd1000_write_reg(state, itd1000_reinit_tab[i][0], itd1000_reinit_tab[i][1]);
343
344         return 0;
345 }
346
347 static int itd1000_sleep(struct dvb_frontend *fe)
348 {
349         return 0;
350 }
351
352 static int itd1000_release(struct dvb_frontend *fe)
353 {
354         kfree(fe->tuner_priv);
355         fe->tuner_priv = NULL;
356         return 0;
357 }
358
359 static const struct dvb_tuner_ops itd1000_tuner_ops = {
360         .info = {
361                 .name           = "Integrant ITD1000",
362                 .frequency_min  = 950000,
363                 .frequency_max  = 2150000,
364                 .frequency_step = 125,     /* kHz for QPSK frontends */
365         },
366
367         .release       = itd1000_release,
368
369         .init          = itd1000_init,
370         .sleep         = itd1000_sleep,
371
372         .set_params    = itd1000_set_parameters,
373         .get_frequency = itd1000_get_frequency,
374         .get_bandwidth = itd1000_get_bandwidth
375 };
376
377
378 struct dvb_frontend *itd1000_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct itd1000_config *cfg)
379 {
380         struct itd1000_state *state = NULL;
381         u8 i = 0;
382
383         state = kzalloc(sizeof(struct itd1000_state), GFP_KERNEL);
384         if (state == NULL)
385                 return NULL;
386
387         state->cfg = cfg;
388         state->i2c = i2c;
389
390         i = itd1000_read_reg(state, 0);
391         if (i != 0) {
392                 kfree(state);
393                 return NULL;
394         }
395         itd_info("successfully identified (ID: %d)\n", i);
396
397         memset(state->shadow, 0xff, sizeof(state->shadow));
398         for (i = 0x65; i < 0x9c; i++)
399                 state->shadow[i] = itd1000_read_reg(state, i);
400
401         memcpy(&fe->ops.tuner_ops, &itd1000_tuner_ops, sizeof(struct dvb_tuner_ops));
402
403         fe->tuner_priv = state;
404
405         return fe;
406 }
407 EXPORT_SYMBOL(itd1000_attach);
408
409 MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>");
410 MODULE_DESCRIPTION("Integrant ITD1000 driver");
411 MODULE_LICENSE("GPL");