treewide: Fix typo in printk messages
[linux-2.6/btrfs-unstable.git] / drivers / media / tuners / e4000.c
blob510239f80c0d9f5bb173a8f71d2c784de8b60201
1 /*
2 * Elonics E4000 silicon tuner driver
4 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
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.
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.
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.
21 #include "e4000_priv.h"
22 #include <linux/math64.h>
24 static int e4000_init(struct dvb_frontend *fe)
26 struct e4000 *s = fe->tuner_priv;
27 int ret;
29 dev_dbg(&s->client->dev, "\n");
31 /* dummy I2C to ensure I2C wakes up */
32 ret = regmap_write(s->regmap, 0x02, 0x40);
34 /* reset */
35 ret = regmap_write(s->regmap, 0x00, 0x01);
36 if (ret)
37 goto err;
39 /* disable output clock */
40 ret = regmap_write(s->regmap, 0x06, 0x00);
41 if (ret)
42 goto err;
44 ret = regmap_write(s->regmap, 0x7a, 0x96);
45 if (ret)
46 goto err;
48 /* configure gains */
49 ret = regmap_bulk_write(s->regmap, 0x7e, "\x01\xfe", 2);
50 if (ret)
51 goto err;
53 ret = regmap_write(s->regmap, 0x82, 0x00);
54 if (ret)
55 goto err;
57 ret = regmap_write(s->regmap, 0x24, 0x05);
58 if (ret)
59 goto err;
61 ret = regmap_bulk_write(s->regmap, 0x87, "\x20\x01", 2);
62 if (ret)
63 goto err;
65 ret = regmap_bulk_write(s->regmap, 0x9f, "\x7f\x07", 2);
66 if (ret)
67 goto err;
69 /* DC offset control */
70 ret = regmap_write(s->regmap, 0x2d, 0x1f);
71 if (ret)
72 goto err;
74 ret = regmap_bulk_write(s->regmap, 0x70, "\x01\x01", 2);
75 if (ret)
76 goto err;
78 /* gain control */
79 ret = regmap_write(s->regmap, 0x1a, 0x17);
80 if (ret)
81 goto err;
83 ret = regmap_write(s->regmap, 0x1f, 0x1a);
84 if (ret)
85 goto err;
87 s->active = true;
88 err:
89 if (ret)
90 dev_dbg(&s->client->dev, "failed=%d\n", ret);
92 return ret;
95 static int e4000_sleep(struct dvb_frontend *fe)
97 struct e4000 *s = fe->tuner_priv;
98 int ret;
100 dev_dbg(&s->client->dev, "\n");
102 s->active = false;
104 ret = regmap_write(s->regmap, 0x00, 0x00);
105 if (ret)
106 goto err;
107 err:
108 if (ret)
109 dev_dbg(&s->client->dev, "failed=%d\n", ret);
111 return ret;
114 static int e4000_set_params(struct dvb_frontend *fe)
116 struct e4000 *s = fe->tuner_priv;
117 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
118 int ret, i, sigma_delta;
119 unsigned int pll_n, pll_f;
120 u64 f_vco;
121 u8 buf[5], i_data[4], q_data[4];
123 dev_dbg(&s->client->dev,
124 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
125 c->delivery_system, c->frequency, c->bandwidth_hz);
127 /* gain control manual */
128 ret = regmap_write(s->regmap, 0x1a, 0x00);
129 if (ret)
130 goto err;
132 /* PLL */
133 for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
134 if (c->frequency <= e4000_pll_lut[i].freq)
135 break;
138 if (i == ARRAY_SIZE(e4000_pll_lut)) {
139 ret = -EINVAL;
140 goto err;
143 f_vco = 1ull * c->frequency * e4000_pll_lut[i].mul;
144 pll_n = div_u64_rem(f_vco, s->clock, &pll_f);
145 sigma_delta = div_u64(0x10000ULL * pll_f, s->clock);
146 buf[0] = pll_n;
147 buf[1] = (sigma_delta >> 0) & 0xff;
148 buf[2] = (sigma_delta >> 8) & 0xff;
149 buf[3] = 0x00;
150 buf[4] = e4000_pll_lut[i].div;
152 dev_dbg(&s->client->dev, "f_vco=%llu pll div=%d sigma_delta=%04x\n",
153 f_vco, buf[0], sigma_delta);
155 ret = regmap_bulk_write(s->regmap, 0x09, buf, 5);
156 if (ret)
157 goto err;
159 /* LNA filter (RF filter) */
160 for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
161 if (c->frequency <= e400_lna_filter_lut[i].freq)
162 break;
165 if (i == ARRAY_SIZE(e400_lna_filter_lut)) {
166 ret = -EINVAL;
167 goto err;
170 ret = regmap_write(s->regmap, 0x10, e400_lna_filter_lut[i].val);
171 if (ret)
172 goto err;
174 /* IF filters */
175 for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
176 if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
177 break;
180 if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
181 ret = -EINVAL;
182 goto err;
185 buf[0] = e4000_if_filter_lut[i].reg11_val;
186 buf[1] = e4000_if_filter_lut[i].reg12_val;
188 ret = regmap_bulk_write(s->regmap, 0x11, buf, 2);
189 if (ret)
190 goto err;
192 /* frequency band */
193 for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
194 if (c->frequency <= e4000_band_lut[i].freq)
195 break;
198 if (i == ARRAY_SIZE(e4000_band_lut)) {
199 ret = -EINVAL;
200 goto err;
203 ret = regmap_write(s->regmap, 0x07, e4000_band_lut[i].reg07_val);
204 if (ret)
205 goto err;
207 ret = regmap_write(s->regmap, 0x78, e4000_band_lut[i].reg78_val);
208 if (ret)
209 goto err;
211 /* DC offset */
212 for (i = 0; i < 4; i++) {
213 if (i == 0)
214 ret = regmap_bulk_write(s->regmap, 0x15, "\x00\x7e\x24", 3);
215 else if (i == 1)
216 ret = regmap_bulk_write(s->regmap, 0x15, "\x00\x7f", 2);
217 else if (i == 2)
218 ret = regmap_bulk_write(s->regmap, 0x15, "\x01", 1);
219 else
220 ret = regmap_bulk_write(s->regmap, 0x16, "\x7e", 1);
222 if (ret)
223 goto err;
225 ret = regmap_write(s->regmap, 0x29, 0x01);
226 if (ret)
227 goto err;
229 ret = regmap_bulk_read(s->regmap, 0x2a, buf, 3);
230 if (ret)
231 goto err;
233 i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
234 q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
237 swap(q_data[2], q_data[3]);
238 swap(i_data[2], i_data[3]);
240 ret = regmap_bulk_write(s->regmap, 0x50, q_data, 4);
241 if (ret)
242 goto err;
244 ret = regmap_bulk_write(s->regmap, 0x60, i_data, 4);
245 if (ret)
246 goto err;
248 /* gain control auto */
249 ret = regmap_write(s->regmap, 0x1a, 0x17);
250 if (ret)
251 goto err;
252 err:
253 if (ret)
254 dev_dbg(&s->client->dev, "failed=%d\n", ret);
256 return ret;
259 static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
261 struct e4000 *s = fe->tuner_priv;
263 dev_dbg(&s->client->dev, "\n");
265 *frequency = 0; /* Zero-IF */
267 return 0;
270 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
271 static int e4000_set_lna_gain(struct dvb_frontend *fe)
273 struct e4000 *s = fe->tuner_priv;
274 int ret;
275 u8 u8tmp;
277 dev_dbg(&s->client->dev, "lna auto=%d->%d val=%d->%d\n",
278 s->lna_gain_auto->cur.val, s->lna_gain_auto->val,
279 s->lna_gain->cur.val, s->lna_gain->val);
281 if (s->lna_gain_auto->val && s->if_gain_auto->cur.val)
282 u8tmp = 0x17;
283 else if (s->lna_gain_auto->val)
284 u8tmp = 0x19;
285 else if (s->if_gain_auto->cur.val)
286 u8tmp = 0x16;
287 else
288 u8tmp = 0x10;
290 ret = regmap_write(s->regmap, 0x1a, u8tmp);
291 if (ret)
292 goto err;
294 if (s->lna_gain_auto->val == false) {
295 ret = regmap_write(s->regmap, 0x14, s->lna_gain->val);
296 if (ret)
297 goto err;
299 err:
300 if (ret)
301 dev_dbg(&s->client->dev, "failed=%d\n", ret);
303 return ret;
306 static int e4000_set_mixer_gain(struct dvb_frontend *fe)
308 struct e4000 *s = fe->tuner_priv;
309 int ret;
310 u8 u8tmp;
312 dev_dbg(&s->client->dev, "mixer auto=%d->%d val=%d->%d\n",
313 s->mixer_gain_auto->cur.val, s->mixer_gain_auto->val,
314 s->mixer_gain->cur.val, s->mixer_gain->val);
316 if (s->mixer_gain_auto->val)
317 u8tmp = 0x15;
318 else
319 u8tmp = 0x14;
321 ret = regmap_write(s->regmap, 0x20, u8tmp);
322 if (ret)
323 goto err;
325 if (s->mixer_gain_auto->val == false) {
326 ret = regmap_write(s->regmap, 0x15, s->mixer_gain->val);
327 if (ret)
328 goto err;
330 err:
331 if (ret)
332 dev_dbg(&s->client->dev, "failed=%d\n", ret);
334 return ret;
337 static int e4000_set_if_gain(struct dvb_frontend *fe)
339 struct e4000 *s = fe->tuner_priv;
340 int ret;
341 u8 buf[2];
342 u8 u8tmp;
344 dev_dbg(&s->client->dev, "if auto=%d->%d val=%d->%d\n",
345 s->if_gain_auto->cur.val, s->if_gain_auto->val,
346 s->if_gain->cur.val, s->if_gain->val);
348 if (s->if_gain_auto->val && s->lna_gain_auto->cur.val)
349 u8tmp = 0x17;
350 else if (s->lna_gain_auto->cur.val)
351 u8tmp = 0x19;
352 else if (s->if_gain_auto->val)
353 u8tmp = 0x16;
354 else
355 u8tmp = 0x10;
357 ret = regmap_write(s->regmap, 0x1a, u8tmp);
358 if (ret)
359 goto err;
361 if (s->if_gain_auto->val == false) {
362 buf[0] = e4000_if_gain_lut[s->if_gain->val].reg16_val;
363 buf[1] = e4000_if_gain_lut[s->if_gain->val].reg17_val;
364 ret = regmap_bulk_write(s->regmap, 0x16, buf, 2);
365 if (ret)
366 goto err;
368 err:
369 if (ret)
370 dev_dbg(&s->client->dev, "failed=%d\n", ret);
372 return ret;
375 static int e4000_pll_lock(struct dvb_frontend *fe)
377 struct e4000 *s = fe->tuner_priv;
378 int ret;
379 unsigned int utmp;
381 ret = regmap_read(s->regmap, 0x07, &utmp);
382 if (ret)
383 goto err;
385 s->pll_lock->val = (utmp & 0x01);
386 err:
387 if (ret)
388 dev_dbg(&s->client->dev, "failed=%d\n", ret);
390 return ret;
393 static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
395 struct e4000 *s = container_of(ctrl->handler, struct e4000, hdl);
396 int ret;
398 if (!s->active)
399 return 0;
401 switch (ctrl->id) {
402 case V4L2_CID_RF_TUNER_PLL_LOCK:
403 ret = e4000_pll_lock(s->fe);
404 break;
405 default:
406 dev_dbg(&s->client->dev, "unknown ctrl: id=%d name=%s\n",
407 ctrl->id, ctrl->name);
408 ret = -EINVAL;
411 return ret;
414 static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
416 struct e4000 *s = container_of(ctrl->handler, struct e4000, hdl);
417 struct dvb_frontend *fe = s->fe;
418 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
419 int ret;
421 if (!s->active)
422 return 0;
424 switch (ctrl->id) {
425 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
426 case V4L2_CID_RF_TUNER_BANDWIDTH:
427 c->bandwidth_hz = s->bandwidth->val;
428 ret = e4000_set_params(s->fe);
429 break;
430 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
431 case V4L2_CID_RF_TUNER_LNA_GAIN:
432 ret = e4000_set_lna_gain(s->fe);
433 break;
434 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
435 case V4L2_CID_RF_TUNER_MIXER_GAIN:
436 ret = e4000_set_mixer_gain(s->fe);
437 break;
438 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
439 case V4L2_CID_RF_TUNER_IF_GAIN:
440 ret = e4000_set_if_gain(s->fe);
441 break;
442 default:
443 dev_dbg(&s->client->dev, "unknown ctrl: id=%d name=%s\n",
444 ctrl->id, ctrl->name);
445 ret = -EINVAL;
448 return ret;
451 static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
452 .g_volatile_ctrl = e4000_g_volatile_ctrl,
453 .s_ctrl = e4000_s_ctrl,
455 #endif
457 static const struct dvb_tuner_ops e4000_tuner_ops = {
458 .info = {
459 .name = "Elonics E4000",
460 .frequency_min = 174000000,
461 .frequency_max = 862000000,
464 .init = e4000_init,
465 .sleep = e4000_sleep,
466 .set_params = e4000_set_params,
468 .get_if_frequency = e4000_get_if_frequency,
472 * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
473 * subdev itself, just to avoid reinventing the wheel.
475 static int e4000_probe(struct i2c_client *client,
476 const struct i2c_device_id *id)
478 struct e4000_config *cfg = client->dev.platform_data;
479 struct dvb_frontend *fe = cfg->fe;
480 struct e4000 *s;
481 int ret;
482 unsigned int utmp;
483 static const struct regmap_config regmap_config = {
484 .reg_bits = 8,
485 .val_bits = 8,
486 .max_register = 0xff,
489 s = kzalloc(sizeof(struct e4000), GFP_KERNEL);
490 if (!s) {
491 ret = -ENOMEM;
492 dev_err(&client->dev, "kzalloc() failed\n");
493 goto err;
496 s->clock = cfg->clock;
497 s->client = client;
498 s->fe = cfg->fe;
499 s->regmap = devm_regmap_init_i2c(client, &regmap_config);
500 if (IS_ERR(s->regmap)) {
501 ret = PTR_ERR(s->regmap);
502 goto err;
505 /* check if the tuner is there */
506 ret = regmap_read(s->regmap, 0x02, &utmp);
507 if (ret)
508 goto err;
510 dev_dbg(&s->client->dev, "chip id=%02x\n", utmp);
512 if (utmp != 0x40) {
513 ret = -ENODEV;
514 goto err;
517 /* put sleep as chip seems to be in normal mode by default */
518 ret = regmap_write(s->regmap, 0x00, 0x00);
519 if (ret)
520 goto err;
522 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
523 /* Register controls */
524 v4l2_ctrl_handler_init(&s->hdl, 9);
525 s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
526 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
527 s->bandwidth = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
528 V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
529 v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
530 s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
531 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
532 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
533 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
534 v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false);
535 s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
536 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
537 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
538 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
539 v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false);
540 s->if_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
541 V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
542 s->if_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
543 V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
544 v4l2_ctrl_auto_cluster(2, &s->if_gain_auto, 0, false);
545 s->pll_lock = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
546 V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0);
547 if (s->hdl.error) {
548 ret = s->hdl.error;
549 dev_err(&s->client->dev, "Could not initialize controls\n");
550 v4l2_ctrl_handler_free(&s->hdl);
551 goto err;
554 s->sd.ctrl_handler = &s->hdl;
555 #endif
557 dev_info(&s->client->dev, "Elonics E4000 successfully identified\n");
559 fe->tuner_priv = s;
560 memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
561 sizeof(struct dvb_tuner_ops));
563 v4l2_set_subdevdata(&s->sd, client);
564 i2c_set_clientdata(client, &s->sd);
566 return 0;
567 err:
568 if (ret) {
569 dev_dbg(&client->dev, "failed=%d\n", ret);
570 kfree(s);
573 return ret;
576 static int e4000_remove(struct i2c_client *client)
578 struct v4l2_subdev *sd = i2c_get_clientdata(client);
579 struct e4000 *s = container_of(sd, struct e4000, sd);
580 struct dvb_frontend *fe = s->fe;
582 dev_dbg(&client->dev, "\n");
584 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
585 v4l2_ctrl_handler_free(&s->hdl);
586 #endif
587 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
588 fe->tuner_priv = NULL;
589 kfree(s);
591 return 0;
594 static const struct i2c_device_id e4000_id[] = {
595 {"e4000", 0},
598 MODULE_DEVICE_TABLE(i2c, e4000_id);
600 static struct i2c_driver e4000_driver = {
601 .driver = {
602 .owner = THIS_MODULE,
603 .name = "e4000",
605 .probe = e4000_probe,
606 .remove = e4000_remove,
607 .id_table = e4000_id,
610 module_i2c_driver(e4000_driver);
612 MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
613 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
614 MODULE_LICENSE("GPL");