]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/tuners/e4000.c
[media] e4000: various small changes
[karo-tx-linux.git] / drivers / media / tuners / e4000.c
1 /*
2  * Elonics E4000 silicon tuner driver
3  *
4  * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
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  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License along
17  *    with this program; if not, write to the Free Software Foundation, Inc.,
18  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "e4000_priv.h"
22
23 static int e4000_init(struct dvb_frontend *fe)
24 {
25         struct e4000_dev *dev = fe->tuner_priv;
26         struct i2c_client *client = dev->client;
27         int ret;
28
29         dev_dbg(&client->dev, "\n");
30
31         /* reset */
32         ret = regmap_write(dev->regmap, 0x00, 0x01);
33         if (ret)
34                 goto err;
35
36         /* disable output clock */
37         ret = regmap_write(dev->regmap, 0x06, 0x00);
38         if (ret)
39                 goto err;
40
41         ret = regmap_write(dev->regmap, 0x7a, 0x96);
42         if (ret)
43                 goto err;
44
45         /* configure gains */
46         ret = regmap_bulk_write(dev->regmap, 0x7e, "\x01\xfe", 2);
47         if (ret)
48                 goto err;
49
50         ret = regmap_write(dev->regmap, 0x82, 0x00);
51         if (ret)
52                 goto err;
53
54         ret = regmap_write(dev->regmap, 0x24, 0x05);
55         if (ret)
56                 goto err;
57
58         ret = regmap_bulk_write(dev->regmap, 0x87, "\x20\x01", 2);
59         if (ret)
60                 goto err;
61
62         ret = regmap_bulk_write(dev->regmap, 0x9f, "\x7f\x07", 2);
63         if (ret)
64                 goto err;
65
66         /* DC offset control */
67         ret = regmap_write(dev->regmap, 0x2d, 0x1f);
68         if (ret)
69                 goto err;
70
71         ret = regmap_bulk_write(dev->regmap, 0x70, "\x01\x01", 2);
72         if (ret)
73                 goto err;
74
75         /* gain control */
76         ret = regmap_write(dev->regmap, 0x1a, 0x17);
77         if (ret)
78                 goto err;
79
80         ret = regmap_write(dev->regmap, 0x1f, 0x1a);
81         if (ret)
82                 goto err;
83
84         dev->active = true;
85
86         return 0;
87 err:
88         dev_dbg(&client->dev, "failed=%d\n", ret);
89         return ret;
90 }
91
92 static int e4000_sleep(struct dvb_frontend *fe)
93 {
94         struct e4000_dev *dev = fe->tuner_priv;
95         struct i2c_client *client = dev->client;
96         int ret;
97
98         dev_dbg(&client->dev, "\n");
99
100         dev->active = false;
101
102         ret = regmap_write(dev->regmap, 0x00, 0x00);
103         if (ret)
104                 goto err;
105
106         return 0;
107 err:
108         dev_dbg(&client->dev, "failed=%d\n", ret);
109         return ret;
110 }
111
112 static int e4000_set_params(struct dvb_frontend *fe)
113 {
114         struct e4000_dev *dev = fe->tuner_priv;
115         struct i2c_client *client = dev->client;
116         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
117         int ret, i;
118         unsigned int div_n, k, k_cw, div_out;
119         u64 f_vco;
120         u8 buf[5], i_data[4], q_data[4];
121
122         dev_dbg(&client->dev,
123                 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
124                 c->delivery_system, c->frequency, c->bandwidth_hz);
125
126         /* gain control manual */
127         ret = regmap_write(dev->regmap, 0x1a, 0x00);
128         if (ret)
129                 goto err;
130
131         /*
132          * Fractional-N synthesizer
133          *
134          *           +----------------------------+
135          *           v                            |
136          *  Fref   +----+     +-------+         +------+     +---+
137          * ------> | PD | --> |  VCO  | ------> | /N.F | <-- | K |
138          *         +----+     +-------+         +------+     +---+
139          *                      |
140          *                      |
141          *                      v
142          *                    +-------+  Fout
143          *                    | /Rout | ------>
144          *                    +-------+
145          */
146         for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
147                 if (c->frequency <= e4000_pll_lut[i].freq)
148                         break;
149         }
150         if (i == ARRAY_SIZE(e4000_pll_lut)) {
151                 ret = -EINVAL;
152                 goto err;
153         }
154
155         #define F_REF dev->clk
156         div_out = e4000_pll_lut[i].div_out;
157         f_vco = (u64) c->frequency * div_out;
158         /* calculate PLL integer and fractional control word */
159         div_n = div_u64_rem(f_vco, F_REF, &k);
160         k_cw = div_u64((u64) k * 0x10000, F_REF);
161
162         dev_dbg(&client->dev,
163                 "frequency=%u f_vco=%llu F_REF=%u div_n=%u k=%u k_cw=%04x div_out=%u\n",
164                 c->frequency, f_vco, F_REF, div_n, k, k_cw, div_out);
165
166         buf[0] = div_n;
167         buf[1] = (k_cw >> 0) & 0xff;
168         buf[2] = (k_cw >> 8) & 0xff;
169         buf[3] = 0x00;
170         buf[4] = e4000_pll_lut[i].div_out_reg;
171         ret = regmap_bulk_write(dev->regmap, 0x09, buf, 5);
172         if (ret)
173                 goto err;
174
175         /* LNA filter (RF filter) */
176         for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
177                 if (c->frequency <= e400_lna_filter_lut[i].freq)
178                         break;
179         }
180         if (i == ARRAY_SIZE(e400_lna_filter_lut)) {
181                 ret = -EINVAL;
182                 goto err;
183         }
184
185         ret = regmap_write(dev->regmap, 0x10, e400_lna_filter_lut[i].val);
186         if (ret)
187                 goto err;
188
189         /* IF filters */
190         for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
191                 if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
192                         break;
193         }
194         if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
195                 ret = -EINVAL;
196                 goto err;
197         }
198
199         buf[0] = e4000_if_filter_lut[i].reg11_val;
200         buf[1] = e4000_if_filter_lut[i].reg12_val;
201
202         ret = regmap_bulk_write(dev->regmap, 0x11, buf, 2);
203         if (ret)
204                 goto err;
205
206         /* frequency band */
207         for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
208                 if (c->frequency <= e4000_band_lut[i].freq)
209                         break;
210         }
211         if (i == ARRAY_SIZE(e4000_band_lut)) {
212                 ret = -EINVAL;
213                 goto err;
214         }
215
216         ret = regmap_write(dev->regmap, 0x07, e4000_band_lut[i].reg07_val);
217         if (ret)
218                 goto err;
219
220         ret = regmap_write(dev->regmap, 0x78, e4000_band_lut[i].reg78_val);
221         if (ret)
222                 goto err;
223
224         /* DC offset */
225         for (i = 0; i < 4; i++) {
226                 if (i == 0)
227                         ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7e\x24", 3);
228                 else if (i == 1)
229                         ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7f", 2);
230                 else if (i == 2)
231                         ret = regmap_bulk_write(dev->regmap, 0x15, "\x01", 1);
232                 else
233                         ret = regmap_bulk_write(dev->regmap, 0x16, "\x7e", 1);
234
235                 if (ret)
236                         goto err;
237
238                 ret = regmap_write(dev->regmap, 0x29, 0x01);
239                 if (ret)
240                         goto err;
241
242                 ret = regmap_bulk_read(dev->regmap, 0x2a, buf, 3);
243                 if (ret)
244                         goto err;
245
246                 i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
247                 q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
248         }
249
250         swap(q_data[2], q_data[3]);
251         swap(i_data[2], i_data[3]);
252
253         ret = regmap_bulk_write(dev->regmap, 0x50, q_data, 4);
254         if (ret)
255                 goto err;
256
257         ret = regmap_bulk_write(dev->regmap, 0x60, i_data, 4);
258         if (ret)
259                 goto err;
260
261         /* gain control auto */
262         ret = regmap_write(dev->regmap, 0x1a, 0x17);
263         if (ret)
264                 goto err;
265
266         return 0;
267 err:
268         dev_dbg(&client->dev, "failed=%d\n", ret);
269         return ret;
270 }
271
272 static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
273 {
274         struct e4000_dev *dev = fe->tuner_priv;
275         struct i2c_client *client = dev->client;
276
277         dev_dbg(&client->dev, "\n");
278
279         *frequency = 0; /* Zero-IF */
280
281         return 0;
282 }
283
284 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
285 static int e4000_set_lna_gain(struct dvb_frontend *fe)
286 {
287         struct e4000_dev *dev = fe->tuner_priv;
288         struct i2c_client *client = dev->client;
289         int ret;
290         u8 u8tmp;
291
292         dev_dbg(&client->dev, "lna auto=%d->%d val=%d->%d\n",
293                 dev->lna_gain_auto->cur.val, dev->lna_gain_auto->val,
294                 dev->lna_gain->cur.val, dev->lna_gain->val);
295
296         if (dev->lna_gain_auto->val && dev->if_gain_auto->cur.val)
297                 u8tmp = 0x17;
298         else if (dev->lna_gain_auto->val)
299                 u8tmp = 0x19;
300         else if (dev->if_gain_auto->cur.val)
301                 u8tmp = 0x16;
302         else
303                 u8tmp = 0x10;
304
305         ret = regmap_write(dev->regmap, 0x1a, u8tmp);
306         if (ret)
307                 goto err;
308
309         if (dev->lna_gain_auto->val == false) {
310                 ret = regmap_write(dev->regmap, 0x14, dev->lna_gain->val);
311                 if (ret)
312                         goto err;
313         }
314
315         return 0;
316 err:
317         dev_dbg(&client->dev, "failed=%d\n", ret);
318         return ret;
319 }
320
321 static int e4000_set_mixer_gain(struct dvb_frontend *fe)
322 {
323         struct e4000_dev *dev = fe->tuner_priv;
324         struct i2c_client *client = dev->client;
325         int ret;
326         u8 u8tmp;
327
328         dev_dbg(&client->dev, "mixer auto=%d->%d val=%d->%d\n",
329                 dev->mixer_gain_auto->cur.val, dev->mixer_gain_auto->val,
330                 dev->mixer_gain->cur.val, dev->mixer_gain->val);
331
332         if (dev->mixer_gain_auto->val)
333                 u8tmp = 0x15;
334         else
335                 u8tmp = 0x14;
336
337         ret = regmap_write(dev->regmap, 0x20, u8tmp);
338         if (ret)
339                 goto err;
340
341         if (dev->mixer_gain_auto->val == false) {
342                 ret = regmap_write(dev->regmap, 0x15, dev->mixer_gain->val);
343                 if (ret)
344                         goto err;
345         }
346
347         return 0;
348 err:
349         dev_dbg(&client->dev, "failed=%d\n", ret);
350         return ret;
351 }
352
353 static int e4000_set_if_gain(struct dvb_frontend *fe)
354 {
355         struct e4000_dev *dev = fe->tuner_priv;
356         struct i2c_client *client = dev->client;
357         int ret;
358         u8 buf[2];
359         u8 u8tmp;
360
361         dev_dbg(&client->dev, "if auto=%d->%d val=%d->%d\n",
362                 dev->if_gain_auto->cur.val, dev->if_gain_auto->val,
363                 dev->if_gain->cur.val, dev->if_gain->val);
364
365         if (dev->if_gain_auto->val && dev->lna_gain_auto->cur.val)
366                 u8tmp = 0x17;
367         else if (dev->lna_gain_auto->cur.val)
368                 u8tmp = 0x19;
369         else if (dev->if_gain_auto->val)
370                 u8tmp = 0x16;
371         else
372                 u8tmp = 0x10;
373
374         ret = regmap_write(dev->regmap, 0x1a, u8tmp);
375         if (ret)
376                 goto err;
377
378         if (dev->if_gain_auto->val == false) {
379                 buf[0] = e4000_if_gain_lut[dev->if_gain->val].reg16_val;
380                 buf[1] = e4000_if_gain_lut[dev->if_gain->val].reg17_val;
381                 ret = regmap_bulk_write(dev->regmap, 0x16, buf, 2);
382                 if (ret)
383                         goto err;
384         }
385
386         return 0;
387 err:
388         dev_dbg(&client->dev, "failed=%d\n", ret);
389         return ret;
390 }
391
392 static int e4000_pll_lock(struct dvb_frontend *fe)
393 {
394         struct e4000_dev *dev = fe->tuner_priv;
395         struct i2c_client *client = dev->client;
396         int ret;
397         unsigned int uitmp;
398
399         ret = regmap_read(dev->regmap, 0x07, &uitmp);
400         if (ret)
401                 goto err;
402
403         dev->pll_lock->val = (uitmp & 0x01);
404
405         return 0;
406 err:
407         dev_dbg(&client->dev, "failed=%d\n", ret);
408         return ret;
409 }
410
411 static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
412 {
413         struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl);
414         struct i2c_client *client = dev->client;
415         int ret;
416
417         if (!dev->active)
418                 return 0;
419
420         switch (ctrl->id) {
421         case  V4L2_CID_RF_TUNER_PLL_LOCK:
422                 ret = e4000_pll_lock(dev->fe);
423                 break;
424         default:
425                 dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n",
426                         ctrl->id, ctrl->name);
427                 ret = -EINVAL;
428         }
429
430         return ret;
431 }
432
433 static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
434 {
435         struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl);
436         struct i2c_client *client = dev->client;
437         struct dtv_frontend_properties *c = &dev->fe->dtv_property_cache;
438         int ret;
439
440         if (!dev->active)
441                 return 0;
442
443         switch (ctrl->id) {
444         case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
445         case V4L2_CID_RF_TUNER_BANDWIDTH:
446                 c->bandwidth_hz = dev->bandwidth->val;
447                 ret = e4000_set_params(dev->fe);
448                 break;
449         case  V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
450         case  V4L2_CID_RF_TUNER_LNA_GAIN:
451                 ret = e4000_set_lna_gain(dev->fe);
452                 break;
453         case  V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
454         case  V4L2_CID_RF_TUNER_MIXER_GAIN:
455                 ret = e4000_set_mixer_gain(dev->fe);
456                 break;
457         case  V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
458         case  V4L2_CID_RF_TUNER_IF_GAIN:
459                 ret = e4000_set_if_gain(dev->fe);
460                 break;
461         default:
462                 dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n",
463                         ctrl->id, ctrl->name);
464                 ret = -EINVAL;
465         }
466
467         return ret;
468 }
469
470 static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
471         .g_volatile_ctrl = e4000_g_volatile_ctrl,
472         .s_ctrl = e4000_s_ctrl,
473 };
474 #endif
475
476 static const struct dvb_tuner_ops e4000_tuner_ops = {
477         .info = {
478                 .name           = "Elonics E4000",
479                 .frequency_min  = 174000000,
480                 .frequency_max  = 862000000,
481         },
482
483         .init = e4000_init,
484         .sleep = e4000_sleep,
485         .set_params = e4000_set_params,
486
487         .get_if_frequency = e4000_get_if_frequency,
488 };
489
490 /*
491  * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
492  * subdev itself, just to avoid reinventing the wheel.
493  */
494 static int e4000_probe(struct i2c_client *client,
495                        const struct i2c_device_id *id)
496 {
497         struct e4000_dev *dev;
498         struct e4000_config *cfg = client->dev.platform_data;
499         struct dvb_frontend *fe = cfg->fe;
500         int ret;
501         unsigned int uitmp;
502         static const struct regmap_config regmap_config = {
503                 .reg_bits = 8,
504                 .val_bits = 8,
505         };
506
507         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
508         if (!dev) {
509                 ret = -ENOMEM;
510                 goto err;
511         }
512
513         dev->clk = cfg->clock;
514         dev->client = client;
515         dev->fe = cfg->fe;
516         dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
517         if (IS_ERR(dev->regmap)) {
518                 ret = PTR_ERR(dev->regmap);
519                 goto err_kfree;
520         }
521
522         /* check if the tuner is there */
523         ret = regmap_read(dev->regmap, 0x02, &uitmp);
524         if (ret)
525                 goto err_kfree;
526
527         dev_dbg(&client->dev, "chip id=%02x\n", uitmp);
528
529         if (uitmp != 0x40) {
530                 ret = -ENODEV;
531                 goto err_kfree;
532         }
533
534         /* put sleep as chip seems to be in normal mode by default */
535         ret = regmap_write(dev->regmap, 0x00, 0x00);
536         if (ret)
537                 goto err_kfree;
538
539 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
540         /* Register controls */
541         v4l2_ctrl_handler_init(&dev->hdl, 9);
542         dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
543                         V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
544         dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
545                         V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
546         v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
547         dev->lna_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
548                         V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
549         dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
550                         V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
551         v4l2_ctrl_auto_cluster(2, &dev->lna_gain_auto, 0, false);
552         dev->mixer_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
553                         V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
554         dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
555                         V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
556         v4l2_ctrl_auto_cluster(2, &dev->mixer_gain_auto, 0, false);
557         dev->if_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
558                         V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
559         dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
560                         V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
561         v4l2_ctrl_auto_cluster(2, &dev->if_gain_auto, 0, false);
562         dev->pll_lock = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
563                         V4L2_CID_RF_TUNER_PLL_LOCK,  0, 1, 1, 0);
564         if (dev->hdl.error) {
565                 ret = dev->hdl.error;
566                 dev_err(&client->dev, "Could not initialize controls\n");
567                 v4l2_ctrl_handler_free(&dev->hdl);
568                 goto err_kfree;
569         }
570
571         dev->sd.ctrl_handler = &dev->hdl;
572 #endif
573         fe->tuner_priv = dev;
574         memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
575                         sizeof(struct dvb_tuner_ops));
576         v4l2_set_subdevdata(&dev->sd, client);
577         i2c_set_clientdata(client, &dev->sd);
578
579         dev_info(&client->dev, "Elonics E4000 successfully identified\n");
580         return 0;
581 err_kfree:
582         kfree(dev);
583 err:
584         dev_dbg(&client->dev, "failed=%d\n", ret);
585         return ret;
586 }
587
588 static int e4000_remove(struct i2c_client *client)
589 {
590         struct v4l2_subdev *sd = i2c_get_clientdata(client);
591         struct e4000_dev *dev = container_of(sd, struct e4000_dev, sd);
592
593         dev_dbg(&client->dev, "\n");
594
595 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
596         v4l2_ctrl_handler_free(&dev->hdl);
597 #endif
598         kfree(dev);
599
600         return 0;
601 }
602
603 static const struct i2c_device_id e4000_id_table[] = {
604         {"e4000", 0},
605         {}
606 };
607 MODULE_DEVICE_TABLE(i2c, e4000_id_table);
608
609 static struct i2c_driver e4000_driver = {
610         .driver = {
611                 .owner  = THIS_MODULE,
612                 .name   = "e4000",
613                 .suppress_bind_attrs = true,
614         },
615         .probe          = e4000_probe,
616         .remove         = e4000_remove,
617         .id_table       = e4000_id_table,
618 };
619
620 module_i2c_driver(e4000_driver);
621
622 MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
623 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
624 MODULE_LICENSE("GPL");