[media] ts2020: re-implement PLL calculations
[firefly-linux-kernel-4.4.55.git] / drivers / media / dvb-frontends / ts2020.c
1 /*
2     Montage Technology TS2020 - Silicon Tuner driver
3     Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
4
5     Copyright (C) 2009-2012 TurboSight.com
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
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 "dvb_frontend.h"
23 #include "ts2020.h"
24
25 #define TS2020_XTAL_FREQ   27000 /* in kHz */
26 #define FREQ_OFFSET_LOW_SYM_RATE 3000
27
28 struct ts2020_priv {
29         struct dvb_frontend *fe;
30         /* i2c details */
31         int i2c_address;
32         struct i2c_adapter *i2c;
33         u8 clk_out:2;
34         u8 clk_out_div:5;
35         u32 frequency_div; /* LO output divider switch frequency */
36         u32 frequency_khz; /* actual used LO frequency */
37 #define TS2020_M88TS2020 0
38 #define TS2020_M88TS2022 1
39         u8 tuner;
40         u8 loop_through:1;
41 };
42
43 struct ts2020_reg_val {
44         u8 reg;
45         u8 val;
46 };
47
48 static int ts2020_release(struct dvb_frontend *fe)
49 {
50         kfree(fe->tuner_priv);
51         fe->tuner_priv = NULL;
52         return 0;
53 }
54
55 static int ts2020_writereg(struct dvb_frontend *fe, int reg, int data)
56 {
57         struct ts2020_priv *priv = fe->tuner_priv;
58         u8 buf[] = { reg, data };
59         struct i2c_msg msg[] = {
60                 {
61                         .addr = priv->i2c_address,
62                         .flags = 0,
63                         .buf = buf,
64                         .len = 2
65                 }
66         };
67         int err;
68
69         if (fe->ops.i2c_gate_ctrl)
70                 fe->ops.i2c_gate_ctrl(fe, 1);
71
72         err = i2c_transfer(priv->i2c, msg, 1);
73         if (err != 1) {
74                 printk(KERN_ERR
75                        "%s: writereg error(err == %i, reg == 0x%02x, value == 0x%02x)\n",
76                        __func__, err, reg, data);
77                 return -EREMOTEIO;
78         }
79
80         if (fe->ops.i2c_gate_ctrl)
81                 fe->ops.i2c_gate_ctrl(fe, 0);
82
83         return 0;
84 }
85
86 static int ts2020_readreg(struct dvb_frontend *fe, u8 reg)
87 {
88         struct ts2020_priv *priv = fe->tuner_priv;
89         int ret;
90         u8 b0[] = { reg };
91         u8 b1[] = { 0 };
92         struct i2c_msg msg[] = {
93                 {
94                         .addr = priv->i2c_address,
95                         .flags = 0,
96                         .buf = b0,
97                         .len = 1
98                 }, {
99                         .addr = priv->i2c_address,
100                         .flags = I2C_M_RD,
101                         .buf = b1,
102                         .len = 1
103                 }
104         };
105
106         if (fe->ops.i2c_gate_ctrl)
107                 fe->ops.i2c_gate_ctrl(fe, 1);
108
109         ret = i2c_transfer(priv->i2c, msg, 2);
110
111         if (ret != 2) {
112                 printk(KERN_ERR "%s: reg=0x%x(error=%d)\n",
113                        __func__, reg, ret);
114                 return ret;
115         }
116
117         if (fe->ops.i2c_gate_ctrl)
118                 fe->ops.i2c_gate_ctrl(fe, 0);
119
120         return b1[0];
121 }
122
123 static int ts2020_sleep(struct dvb_frontend *fe)
124 {
125         struct ts2020_priv *priv = fe->tuner_priv;
126         u8 u8tmp;
127
128         if (priv->tuner == TS2020_M88TS2020)
129                 u8tmp = 0x0a; /* XXX: probably wrong */
130         else
131                 u8tmp = 0x00;
132
133         return ts2020_writereg(fe, u8tmp, 0x00);
134 }
135
136 static int ts2020_init(struct dvb_frontend *fe)
137 {
138         struct ts2020_priv *priv = fe->tuner_priv;
139         int i;
140         u8 u8tmp;
141
142         if (priv->tuner == TS2020_M88TS2020) {
143                 ts2020_writereg(fe, 0x42, 0x73);
144                 ts2020_writereg(fe, 0x05, priv->clk_out_div);
145                 ts2020_writereg(fe, 0x20, 0x27);
146                 ts2020_writereg(fe, 0x07, 0x02);
147                 ts2020_writereg(fe, 0x11, 0xff);
148                 ts2020_writereg(fe, 0x60, 0xf9);
149                 ts2020_writereg(fe, 0x08, 0x01);
150                 ts2020_writereg(fe, 0x00, 0x41);
151         } else {
152                 static const struct ts2020_reg_val reg_vals[] = {
153                         {0x7d, 0x9d},
154                         {0x7c, 0x9a},
155                         {0x7a, 0x76},
156                         {0x3b, 0x01},
157                         {0x63, 0x88},
158                         {0x61, 0x85},
159                         {0x22, 0x30},
160                         {0x30, 0x40},
161                         {0x20, 0x23},
162                         {0x24, 0x02},
163                         {0x12, 0xa0},
164                 };
165
166                 ts2020_writereg(fe, 0x00, 0x01);
167                 ts2020_writereg(fe, 0x00, 0x03);
168
169                 switch (priv->clk_out) {
170                 case TS2020_CLK_OUT_DISABLED:
171                         u8tmp = 0x60;
172                         break;
173                 case TS2020_CLK_OUT_ENABLED:
174                         u8tmp = 0x70;
175                         ts2020_writereg(fe, 0x05, priv->clk_out_div);
176                         break;
177                 case TS2020_CLK_OUT_ENABLED_XTALOUT:
178                         u8tmp = 0x6c;
179                         break;
180                 default:
181                         u8tmp = 0x60;
182                         break;
183                 }
184
185                 ts2020_writereg(fe, 0x42, u8tmp);
186
187                 if (priv->loop_through)
188                         u8tmp = 0xec;
189                 else
190                         u8tmp = 0x6c;
191
192                 ts2020_writereg(fe, 0x62, u8tmp);
193
194                 for (i = 0; i < ARRAY_SIZE(reg_vals); i++)
195                         ts2020_writereg(fe, reg_vals[i].reg, reg_vals[i].val);
196         }
197
198         return 0;
199 }
200
201 static int ts2020_tuner_gate_ctrl(struct dvb_frontend *fe, u8 offset)
202 {
203         int ret;
204         ret = ts2020_writereg(fe, 0x51, 0x1f - offset);
205         ret |= ts2020_writereg(fe, 0x51, 0x1f);
206         ret |= ts2020_writereg(fe, 0x50, offset);
207         ret |= ts2020_writereg(fe, 0x50, 0x00);
208         msleep(20);
209         return ret;
210 }
211
212 static int ts2020_set_tuner_rf(struct dvb_frontend *fe)
213 {
214         int reg;
215
216         reg = ts2020_readreg(fe, 0x3d);
217         reg &= 0x7f;
218         if (reg < 0x16)
219                 reg = 0xa1;
220         else if (reg == 0x16)
221                 reg = 0x99;
222         else
223                 reg = 0xf9;
224
225         ts2020_writereg(fe, 0x60, reg);
226         reg = ts2020_tuner_gate_ctrl(fe, 0x08);
227
228         return reg;
229 }
230
231 static int ts2020_set_params(struct dvb_frontend *fe)
232 {
233         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
234         struct ts2020_priv *priv = fe->tuner_priv;
235         int ret;
236         u32 symbol_rate = (c->symbol_rate / 1000);
237         u32 f3db, gdiv28;
238         u16 u16tmp, value, lpf_coeff;
239         u8 buf[3], reg10, lpf_mxdiv, mlpf_max, mlpf_min, nlpf;
240         unsigned int f_ref_khz, f_vco_khz, div_ref, div_out, pll_n;
241         unsigned int frequency_khz = c->frequency;
242
243         /*
244          * Integer-N PLL synthesizer
245          * kHz is used for all calculations to keep calculations within 32-bit
246          */
247         f_ref_khz = TS2020_XTAL_FREQ;
248         div_ref = DIV_ROUND_CLOSEST(f_ref_khz, 2000);
249
250         /* select LO output divider */
251         if (frequency_khz < priv->frequency_div) {
252                 div_out = 4;
253                 reg10 = 0x10;
254         } else {
255                 div_out = 2;
256                 reg10 = 0x00;
257         }
258
259         f_vco_khz = frequency_khz * div_out;
260         pll_n = f_vco_khz * div_ref / f_ref_khz;
261         pll_n += pll_n % 2;
262         priv->frequency_khz = pll_n * f_ref_khz / div_ref / div_out;
263
264         pr_debug("frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n",
265                  priv->frequency_khz, priv->frequency_khz - c->frequency,
266                  f_vco_khz, pll_n, div_ref, div_out);
267
268         if (priv->tuner == TS2020_M88TS2020) {
269                 lpf_coeff = 2766;
270                 reg10 |= 0x01;
271                 ret = ts2020_writereg(fe, 0x10, reg10);
272         } else {
273                 lpf_coeff = 3200;
274                 reg10 |= 0x0b;
275                 ret = ts2020_writereg(fe, 0x10, reg10);
276                 ret |= ts2020_writereg(fe, 0x11, 0x40);
277         }
278
279         u16tmp = pll_n - 1024;
280         buf[0] = (u16tmp >> 8) & 0xff;
281         buf[1] = (u16tmp >> 0) & 0xff;
282         buf[2] = div_ref - 8;
283
284         ret |= ts2020_writereg(fe, 0x01, buf[0]);
285         ret |= ts2020_writereg(fe, 0x02, buf[1]);
286         ret |= ts2020_writereg(fe, 0x03, buf[2]);
287
288         ret |= ts2020_tuner_gate_ctrl(fe, 0x10);
289         if (ret < 0)
290                 return -ENODEV;
291
292         ret |= ts2020_tuner_gate_ctrl(fe, 0x08);
293
294         /* Tuner RF */
295         if (priv->tuner == TS2020_M88TS2020)
296                 ret |= ts2020_set_tuner_rf(fe);
297
298         gdiv28 = (TS2020_XTAL_FREQ / 1000 * 1694 + 500) / 1000;
299         ret |= ts2020_writereg(fe, 0x04, gdiv28 & 0xff);
300         ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
301         if (ret < 0)
302                 return -ENODEV;
303
304         if (priv->tuner == TS2020_M88TS2022) {
305                 ret = ts2020_writereg(fe, 0x25, 0x00);
306                 ret |= ts2020_writereg(fe, 0x27, 0x70);
307                 ret |= ts2020_writereg(fe, 0x41, 0x09);
308                 ret |= ts2020_writereg(fe, 0x08, 0x0b);
309                 if (ret < 0)
310                         return -ENODEV;
311         }
312
313         value = ts2020_readreg(fe, 0x26);
314
315         f3db = (symbol_rate * 135) / 200 + 2000;
316         f3db += FREQ_OFFSET_LOW_SYM_RATE;
317         if (f3db < 7000)
318                 f3db = 7000;
319         if (f3db > 40000)
320                 f3db = 40000;
321
322         gdiv28 = gdiv28 * 207 / (value * 2 + 151);
323         mlpf_max = gdiv28 * 135 / 100;
324         mlpf_min = gdiv28 * 78 / 100;
325         if (mlpf_max > 63)
326                 mlpf_max = 63;
327
328         nlpf = (f3db * gdiv28 * 2 / lpf_coeff /
329                 (TS2020_XTAL_FREQ / 1000)  + 1) / 2;
330         if (nlpf > 23)
331                 nlpf = 23;
332         if (nlpf < 1)
333                 nlpf = 1;
334
335         lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
336                 * lpf_coeff * 2  / f3db + 1) / 2;
337
338         if (lpf_mxdiv < mlpf_min) {
339                 nlpf++;
340                 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
341                         * lpf_coeff * 2  / f3db + 1) / 2;
342         }
343
344         if (lpf_mxdiv > mlpf_max)
345                 lpf_mxdiv = mlpf_max;
346
347         ret = ts2020_writereg(fe, 0x04, lpf_mxdiv);
348         ret |= ts2020_writereg(fe, 0x06, nlpf);
349
350         ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
351
352         ret |= ts2020_tuner_gate_ctrl(fe, 0x01);
353
354         msleep(80);
355
356         return (ret < 0) ? -EINVAL : 0;
357 }
358
359 static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)
360 {
361         struct ts2020_priv *priv = fe->tuner_priv;
362
363         *frequency = priv->frequency_khz;
364         return 0;
365 }
366
367 static int ts2020_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
368 {
369         *frequency = 0; /* Zero-IF */
370         return 0;
371 }
372
373 /* read TS2020 signal strength */
374 static int ts2020_read_signal_strength(struct dvb_frontend *fe,
375                                                 u16 *signal_strength)
376 {
377         u16 sig_reading, sig_strength;
378         u8 rfgain, bbgain;
379
380         rfgain = ts2020_readreg(fe, 0x3d) & 0x1f;
381         bbgain = ts2020_readreg(fe, 0x21) & 0x1f;
382
383         if (rfgain > 15)
384                 rfgain = 15;
385         if (bbgain > 13)
386                 bbgain = 13;
387
388         sig_reading = rfgain * 2 + bbgain * 3;
389
390         sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;
391
392         /* cook the value to be suitable for szap-s2 human readable output */
393         *signal_strength = sig_strength * 1000;
394
395         return 0;
396 }
397
398 static struct dvb_tuner_ops ts2020_tuner_ops = {
399         .info = {
400                 .name = "TS2020",
401                 .frequency_min = 950000,
402                 .frequency_max = 2150000
403         },
404         .init = ts2020_init,
405         .release = ts2020_release,
406         .sleep = ts2020_sleep,
407         .set_params = ts2020_set_params,
408         .get_frequency = ts2020_get_frequency,
409         .get_if_frequency = ts2020_get_if_frequency,
410         .get_rf_strength = ts2020_read_signal_strength,
411 };
412
413 struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,
414                                         const struct ts2020_config *config,
415                                         struct i2c_adapter *i2c)
416 {
417         struct ts2020_priv *priv = NULL;
418         u8 buf;
419
420         priv = kzalloc(sizeof(struct ts2020_priv), GFP_KERNEL);
421         if (priv == NULL)
422                 return NULL;
423
424         priv->i2c_address = config->tuner_address;
425         priv->i2c = i2c;
426         priv->clk_out = config->clk_out;
427         priv->clk_out_div = config->clk_out_div;
428         priv->frequency_div = config->frequency_div;
429         priv->fe = fe;
430         fe->tuner_priv = priv;
431
432         if (!priv->frequency_div)
433                 priv->frequency_div = 1060000;
434
435         /* Wake Up the tuner */
436         if ((0x03 & ts2020_readreg(fe, 0x00)) == 0x00) {
437                 ts2020_writereg(fe, 0x00, 0x01);
438                 msleep(2);
439         }
440
441         ts2020_writereg(fe, 0x00, 0x03);
442         msleep(2);
443
444         /* Check the tuner version */
445         buf = ts2020_readreg(fe, 0x00);
446         if ((buf == 0x01) || (buf == 0x41) || (buf == 0x81)) {
447                 printk(KERN_INFO "%s: Find tuner TS2020!\n", __func__);
448                 priv->tuner = TS2020_M88TS2020;
449         } else if ((buf == 0x83) || (buf == 0xc3)) {
450                 printk(KERN_INFO "%s: Find tuner TS2022!\n", __func__);
451                 priv->tuner = TS2020_M88TS2022;
452         } else {
453                 printk(KERN_ERR "%s: Read tuner reg[0] = %d\n", __func__, buf);
454                 kfree(priv);
455                 return NULL;
456         }
457
458         memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
459                                 sizeof(struct dvb_tuner_ops));
460
461         return fe;
462 }
463 EXPORT_SYMBOL(ts2020_attach);
464
465 static int ts2020_probe(struct i2c_client *client,
466                 const struct i2c_device_id *id)
467 {
468         struct ts2020_config *pdata = client->dev.platform_data;
469         struct dvb_frontend *fe = pdata->fe;
470         struct ts2020_priv *dev;
471         int ret;
472         u8 u8tmp;
473         unsigned int utmp;
474         char *chip_str;
475
476         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
477         if (!dev) {
478                 ret = -ENOMEM;
479                 goto err;
480         }
481
482         dev->i2c = client->adapter;
483         dev->i2c_address = client->addr;
484         dev->clk_out = pdata->clk_out;
485         dev->clk_out_div = pdata->clk_out_div;
486         dev->frequency_div = pdata->frequency_div;
487         dev->fe = fe;
488         fe->tuner_priv = dev;
489
490         /* check if the tuner is there */
491         ret = ts2020_readreg(fe, 0x00);
492         if (ret < 0)
493                 goto err;
494         utmp = ret;
495
496         if ((utmp & 0x03) == 0x00) {
497                 ret = ts2020_writereg(fe, 0x00, 0x01);
498                 if (ret)
499                         goto err;
500
501                 usleep_range(2000, 50000);
502         }
503
504         ret = ts2020_writereg(fe, 0x00, 0x03);
505         if (ret)
506                 goto err;
507
508         usleep_range(2000, 50000);
509
510         ret = ts2020_readreg(fe, 0x00);
511         if (ret < 0)
512                 goto err;
513         utmp = ret;
514
515         dev_dbg(&client->dev, "chip_id=%02x\n", utmp);
516
517         switch (utmp) {
518         case 0x01:
519         case 0x41:
520         case 0x81:
521                 dev->tuner = TS2020_M88TS2020;
522                 chip_str = "TS2020";
523                 if (!dev->frequency_div)
524                         dev->frequency_div = 1060000;
525                 break;
526         case 0xc3:
527         case 0x83:
528                 dev->tuner = TS2020_M88TS2022;
529                 chip_str = "TS2022";
530                 if (!dev->frequency_div)
531                         dev->frequency_div = 1103000;
532                 break;
533         default:
534                 ret = -ENODEV;
535                 goto err;
536         }
537
538         if (dev->tuner == TS2020_M88TS2022) {
539                 switch (dev->clk_out) {
540                 case TS2020_CLK_OUT_DISABLED:
541                         u8tmp = 0x60;
542                         break;
543                 case TS2020_CLK_OUT_ENABLED:
544                         u8tmp = 0x70;
545                         ret = ts2020_writereg(fe, 0x05, dev->clk_out_div);
546                         if (ret)
547                                 goto err;
548                         break;
549                 case TS2020_CLK_OUT_ENABLED_XTALOUT:
550                         u8tmp = 0x6c;
551                         break;
552                 default:
553                         ret = -EINVAL;
554                         goto err;
555                 }
556
557                 ret = ts2020_writereg(fe, 0x42, u8tmp);
558                 if (ret)
559                         goto err;
560
561                 if (dev->loop_through)
562                         u8tmp = 0xec;
563                 else
564                         u8tmp = 0x6c;
565
566                 ret = ts2020_writereg(fe, 0x62, u8tmp);
567                 if (ret)
568                         goto err;
569         }
570
571         /* sleep */
572         ret = ts2020_writereg(fe, 0x00, 0x00);
573         if (ret)
574                 goto err;
575
576         dev_info(&client->dev,
577                  "Montage Technology %s successfully identified\n", chip_str);
578
579         memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
580                         sizeof(struct dvb_tuner_ops));
581         fe->ops.tuner_ops.release = NULL;
582
583         i2c_set_clientdata(client, dev);
584         return 0;
585 err:
586         dev_dbg(&client->dev, "failed=%d\n", ret);
587         kfree(dev);
588         return ret;
589 }
590
591 static int ts2020_remove(struct i2c_client *client)
592 {
593         struct ts2020_priv *dev = i2c_get_clientdata(client);
594         struct dvb_frontend *fe = dev->fe;
595
596         dev_dbg(&client->dev, "\n");
597
598         memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
599         fe->tuner_priv = NULL;
600         kfree(dev);
601
602         return 0;
603 }
604
605 static const struct i2c_device_id ts2020_id_table[] = {
606         {"ts2020", 0},
607         {"ts2022", 0},
608         {}
609 };
610 MODULE_DEVICE_TABLE(i2c, ts2020_id_table);
611
612 static struct i2c_driver ts2020_driver = {
613         .driver = {
614                 .owner  = THIS_MODULE,
615                 .name   = "ts2020",
616         },
617         .probe          = ts2020_probe,
618         .remove         = ts2020_remove,
619         .id_table       = ts2020_id_table,
620 };
621
622 module_i2c_driver(ts2020_driver);
623
624 MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
625 MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
626 MODULE_LICENSE("GPL");