Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / tuners / fc2580.c
blobf4d4665de168800339156aee08f03f68356e8858
1 /*
2 * FCI FC2580 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 "fc2580_priv.h"
24 * TODO:
25 * I2C write and read works only for one single register. Multiple registers
26 * could not be accessed using normal register address auto-increment.
27 * There could be (very likely) register to change that behavior....
30 /* write single register conditionally only when value differs from 0xff
31 * XXX: This is special routine meant only for writing fc2580_freq_regs_lut[]
32 * values. Do not use for the other purposes. */
33 static int fc2580_wr_reg_ff(struct fc2580_dev *dev, u8 reg, u8 val)
35 if (val == 0xff)
36 return 0;
37 else
38 return regmap_write(dev->regmap, reg, val);
41 static int fc2580_set_params(struct fc2580_dev *dev)
43 struct i2c_client *client = dev->client;
44 int ret, i;
45 unsigned int uitmp, div_ref, div_ref_val, div_n, k, k_cw, div_out;
46 u64 f_vco;
47 u8 synth_config;
48 unsigned long timeout;
50 if (!dev->active) {
51 dev_dbg(&client->dev, "tuner is sleeping\n");
52 return 0;
56 * Fractional-N synthesizer
58 * +---------------------------------------+
59 * v |
60 * Fref +----+ +----+ +-------+ +----+ +------+ +---+
61 * ------> | /R | --> | PD | --> | VCO | ------> | /2 | --> | /N.F | <-- | K |
62 * +----+ +----+ +-------+ +----+ +------+ +---+
63 * |
64 * |
65 * v
66 * +-------+ Fout
67 * | /Rout | ------>
68 * +-------+
70 for (i = 0; i < ARRAY_SIZE(fc2580_pll_lut); i++) {
71 if (dev->f_frequency <= fc2580_pll_lut[i].freq)
72 break;
74 if (i == ARRAY_SIZE(fc2580_pll_lut)) {
75 ret = -EINVAL;
76 goto err;
79 #define DIV_PRE_N 2
80 #define F_REF dev->clk
81 div_out = fc2580_pll_lut[i].div_out;
82 f_vco = (u64) dev->f_frequency * div_out;
83 synth_config = fc2580_pll_lut[i].band;
84 if (f_vco < 2600000000ULL)
85 synth_config |= 0x06;
86 else
87 synth_config |= 0x0e;
89 /* select reference divider R (keep PLL div N in valid range) */
90 #define DIV_N_MIN 76
91 if (f_vco >= div_u64((u64) DIV_PRE_N * DIV_N_MIN * F_REF, 1)) {
92 div_ref = 1;
93 div_ref_val = 0x00;
94 } else if (f_vco >= div_u64((u64) DIV_PRE_N * DIV_N_MIN * F_REF, 2)) {
95 div_ref = 2;
96 div_ref_val = 0x10;
97 } else {
98 div_ref = 4;
99 div_ref_val = 0x20;
102 /* calculate PLL integer and fractional control word */
103 uitmp = DIV_PRE_N * F_REF / div_ref;
104 div_n = div_u64_rem(f_vco, uitmp, &k);
105 k_cw = div_u64((u64) k * 0x100000, uitmp);
107 dev_dbg(&client->dev,
108 "frequency=%u bandwidth=%u f_vco=%llu F_REF=%u div_ref=%u div_n=%u k=%u div_out=%u k_cw=%0x\n",
109 dev->f_frequency, dev->f_bandwidth, f_vco, F_REF, div_ref,
110 div_n, k, div_out, k_cw);
112 ret = regmap_write(dev->regmap, 0x02, synth_config);
113 if (ret)
114 goto err;
116 ret = regmap_write(dev->regmap, 0x18, div_ref_val << 0 | k_cw >> 16);
117 if (ret)
118 goto err;
120 ret = regmap_write(dev->regmap, 0x1a, (k_cw >> 8) & 0xff);
121 if (ret)
122 goto err;
124 ret = regmap_write(dev->regmap, 0x1b, (k_cw >> 0) & 0xff);
125 if (ret)
126 goto err;
128 ret = regmap_write(dev->regmap, 0x1c, div_n);
129 if (ret)
130 goto err;
132 /* registers */
133 for (i = 0; i < ARRAY_SIZE(fc2580_freq_regs_lut); i++) {
134 if (dev->f_frequency <= fc2580_freq_regs_lut[i].freq)
135 break;
137 if (i == ARRAY_SIZE(fc2580_freq_regs_lut)) {
138 ret = -EINVAL;
139 goto err;
142 ret = fc2580_wr_reg_ff(dev, 0x25, fc2580_freq_regs_lut[i].r25_val);
143 if (ret)
144 goto err;
146 ret = fc2580_wr_reg_ff(dev, 0x27, fc2580_freq_regs_lut[i].r27_val);
147 if (ret)
148 goto err;
150 ret = fc2580_wr_reg_ff(dev, 0x28, fc2580_freq_regs_lut[i].r28_val);
151 if (ret)
152 goto err;
154 ret = fc2580_wr_reg_ff(dev, 0x29, fc2580_freq_regs_lut[i].r29_val);
155 if (ret)
156 goto err;
158 ret = fc2580_wr_reg_ff(dev, 0x2b, fc2580_freq_regs_lut[i].r2b_val);
159 if (ret)
160 goto err;
162 ret = fc2580_wr_reg_ff(dev, 0x2c, fc2580_freq_regs_lut[i].r2c_val);
163 if (ret)
164 goto err;
166 ret = fc2580_wr_reg_ff(dev, 0x2d, fc2580_freq_regs_lut[i].r2d_val);
167 if (ret)
168 goto err;
170 ret = fc2580_wr_reg_ff(dev, 0x30, fc2580_freq_regs_lut[i].r30_val);
171 if (ret)
172 goto err;
174 ret = fc2580_wr_reg_ff(dev, 0x44, fc2580_freq_regs_lut[i].r44_val);
175 if (ret)
176 goto err;
178 ret = fc2580_wr_reg_ff(dev, 0x50, fc2580_freq_regs_lut[i].r50_val);
179 if (ret)
180 goto err;
182 ret = fc2580_wr_reg_ff(dev, 0x53, fc2580_freq_regs_lut[i].r53_val);
183 if (ret)
184 goto err;
186 ret = fc2580_wr_reg_ff(dev, 0x5f, fc2580_freq_regs_lut[i].r5f_val);
187 if (ret)
188 goto err;
190 ret = fc2580_wr_reg_ff(dev, 0x61, fc2580_freq_regs_lut[i].r61_val);
191 if (ret)
192 goto err;
194 ret = fc2580_wr_reg_ff(dev, 0x62, fc2580_freq_regs_lut[i].r62_val);
195 if (ret)
196 goto err;
198 ret = fc2580_wr_reg_ff(dev, 0x63, fc2580_freq_regs_lut[i].r63_val);
199 if (ret)
200 goto err;
202 ret = fc2580_wr_reg_ff(dev, 0x67, fc2580_freq_regs_lut[i].r67_val);
203 if (ret)
204 goto err;
206 ret = fc2580_wr_reg_ff(dev, 0x68, fc2580_freq_regs_lut[i].r68_val);
207 if (ret)
208 goto err;
210 ret = fc2580_wr_reg_ff(dev, 0x69, fc2580_freq_regs_lut[i].r69_val);
211 if (ret)
212 goto err;
214 ret = fc2580_wr_reg_ff(dev, 0x6a, fc2580_freq_regs_lut[i].r6a_val);
215 if (ret)
216 goto err;
218 ret = fc2580_wr_reg_ff(dev, 0x6b, fc2580_freq_regs_lut[i].r6b_val);
219 if (ret)
220 goto err;
222 ret = fc2580_wr_reg_ff(dev, 0x6c, fc2580_freq_regs_lut[i].r6c_val);
223 if (ret)
224 goto err;
226 ret = fc2580_wr_reg_ff(dev, 0x6d, fc2580_freq_regs_lut[i].r6d_val);
227 if (ret)
228 goto err;
230 ret = fc2580_wr_reg_ff(dev, 0x6e, fc2580_freq_regs_lut[i].r6e_val);
231 if (ret)
232 goto err;
234 ret = fc2580_wr_reg_ff(dev, 0x6f, fc2580_freq_regs_lut[i].r6f_val);
235 if (ret)
236 goto err;
238 /* IF filters */
239 for (i = 0; i < ARRAY_SIZE(fc2580_if_filter_lut); i++) {
240 if (dev->f_bandwidth <= fc2580_if_filter_lut[i].freq)
241 break;
243 if (i == ARRAY_SIZE(fc2580_if_filter_lut)) {
244 ret = -EINVAL;
245 goto err;
248 ret = regmap_write(dev->regmap, 0x36, fc2580_if_filter_lut[i].r36_val);
249 if (ret)
250 goto err;
252 uitmp = (unsigned int) 8058000 - (dev->f_bandwidth * 122 / 100 / 2);
253 uitmp = div64_u64((u64) dev->clk * uitmp, 1000000000000ULL);
254 ret = regmap_write(dev->regmap, 0x37, uitmp);
255 if (ret)
256 goto err;
258 ret = regmap_write(dev->regmap, 0x39, fc2580_if_filter_lut[i].r39_val);
259 if (ret)
260 goto err;
262 timeout = jiffies + msecs_to_jiffies(30);
263 for (uitmp = ~0xc0; !time_after(jiffies, timeout) && uitmp != 0xc0;) {
264 /* trigger filter */
265 ret = regmap_write(dev->regmap, 0x2e, 0x09);
266 if (ret)
267 goto err;
269 /* locked when [7:6] are set (val: d7 6MHz, d5 7MHz, cd 8MHz) */
270 ret = regmap_read(dev->regmap, 0x2f, &uitmp);
271 if (ret)
272 goto err;
273 uitmp &= 0xc0;
275 ret = regmap_write(dev->regmap, 0x2e, 0x01);
276 if (ret)
277 goto err;
279 if (uitmp != 0xc0)
280 dev_dbg(&client->dev, "filter did not lock %02x\n", uitmp);
282 return 0;
283 err:
284 dev_dbg(&client->dev, "failed=%d\n", ret);
285 return ret;
288 static int fc2580_init(struct fc2580_dev *dev)
290 struct i2c_client *client = dev->client;
291 int ret, i;
293 dev_dbg(&client->dev, "\n");
295 for (i = 0; i < ARRAY_SIZE(fc2580_init_reg_vals); i++) {
296 ret = regmap_write(dev->regmap, fc2580_init_reg_vals[i].reg,
297 fc2580_init_reg_vals[i].val);
298 if (ret)
299 goto err;
302 dev->active = true;
303 return 0;
304 err:
305 dev_dbg(&client->dev, "failed=%d\n", ret);
306 return ret;
309 static int fc2580_sleep(struct fc2580_dev *dev)
311 struct i2c_client *client = dev->client;
312 int ret;
314 dev_dbg(&client->dev, "\n");
316 dev->active = false;
318 ret = regmap_write(dev->regmap, 0x02, 0x0a);
319 if (ret)
320 goto err;
321 return 0;
322 err:
323 dev_dbg(&client->dev, "failed=%d\n", ret);
324 return ret;
328 * DVB API
330 static int fc2580_dvb_set_params(struct dvb_frontend *fe)
332 struct fc2580_dev *dev = fe->tuner_priv;
333 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
335 dev->f_frequency = c->frequency;
336 dev->f_bandwidth = c->bandwidth_hz;
337 return fc2580_set_params(dev);
340 static int fc2580_dvb_init(struct dvb_frontend *fe)
342 return fc2580_init(fe->tuner_priv);
345 static int fc2580_dvb_sleep(struct dvb_frontend *fe)
347 return fc2580_sleep(fe->tuner_priv);
350 static int fc2580_dvb_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
352 *frequency = 0; /* Zero-IF */
353 return 0;
356 static const struct dvb_tuner_ops fc2580_dvb_tuner_ops = {
357 .info = {
358 .name = "FCI FC2580",
359 .frequency_min = 174000000,
360 .frequency_max = 862000000,
363 .init = fc2580_dvb_init,
364 .sleep = fc2580_dvb_sleep,
365 .set_params = fc2580_dvb_set_params,
367 .get_if_frequency = fc2580_dvb_get_if_frequency,
371 * V4L2 API
373 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
374 static const struct v4l2_frequency_band bands[] = {
376 .type = V4L2_TUNER_RF,
377 .index = 0,
378 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
379 .rangelow = 130000000,
380 .rangehigh = 2000000000,
384 static inline struct fc2580_dev *fc2580_subdev_to_dev(struct v4l2_subdev *sd)
386 return container_of(sd, struct fc2580_dev, subdev);
389 static int fc2580_s_power(struct v4l2_subdev *sd, int on)
391 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
392 struct i2c_client *client = dev->client;
393 int ret;
395 dev_dbg(&client->dev, "on=%d\n", on);
397 if (on)
398 ret = fc2580_init(dev);
399 else
400 ret = fc2580_sleep(dev);
401 if (ret)
402 return ret;
404 return fc2580_set_params(dev);
407 static const struct v4l2_subdev_core_ops fc2580_subdev_core_ops = {
408 .s_power = fc2580_s_power,
411 static int fc2580_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
413 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
414 struct i2c_client *client = dev->client;
416 dev_dbg(&client->dev, "index=%d\n", v->index);
418 strlcpy(v->name, "FCI FC2580", sizeof(v->name));
419 v->type = V4L2_TUNER_RF;
420 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
421 v->rangelow = bands[0].rangelow;
422 v->rangehigh = bands[0].rangehigh;
423 return 0;
426 static int fc2580_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
428 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
429 struct i2c_client *client = dev->client;
431 dev_dbg(&client->dev, "index=%d\n", v->index);
432 return 0;
435 static int fc2580_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
437 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
438 struct i2c_client *client = dev->client;
440 dev_dbg(&client->dev, "tuner=%d\n", f->tuner);
441 f->frequency = dev->f_frequency;
442 return 0;
445 static int fc2580_s_frequency(struct v4l2_subdev *sd,
446 const struct v4l2_frequency *f)
448 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
449 struct i2c_client *client = dev->client;
451 dev_dbg(&client->dev, "tuner=%d type=%d frequency=%u\n",
452 f->tuner, f->type, f->frequency);
454 dev->f_frequency = clamp_t(unsigned int, f->frequency,
455 bands[0].rangelow, bands[0].rangehigh);
456 return fc2580_set_params(dev);
459 static int fc2580_enum_freq_bands(struct v4l2_subdev *sd,
460 struct v4l2_frequency_band *band)
462 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
463 struct i2c_client *client = dev->client;
465 dev_dbg(&client->dev, "tuner=%d type=%d index=%d\n",
466 band->tuner, band->type, band->index);
468 if (band->index >= ARRAY_SIZE(bands))
469 return -EINVAL;
471 band->capability = bands[band->index].capability;
472 band->rangelow = bands[band->index].rangelow;
473 band->rangehigh = bands[band->index].rangehigh;
474 return 0;
477 static const struct v4l2_subdev_tuner_ops fc2580_subdev_tuner_ops = {
478 .g_tuner = fc2580_g_tuner,
479 .s_tuner = fc2580_s_tuner,
480 .g_frequency = fc2580_g_frequency,
481 .s_frequency = fc2580_s_frequency,
482 .enum_freq_bands = fc2580_enum_freq_bands,
485 static const struct v4l2_subdev_ops fc2580_subdev_ops = {
486 .core = &fc2580_subdev_core_ops,
487 .tuner = &fc2580_subdev_tuner_ops,
490 static int fc2580_s_ctrl(struct v4l2_ctrl *ctrl)
492 struct fc2580_dev *dev = container_of(ctrl->handler, struct fc2580_dev, hdl);
493 struct i2c_client *client = dev->client;
494 int ret;
496 dev_dbg(&client->dev, "ctrl: id=%d name=%s cur.val=%d val=%d\n",
497 ctrl->id, ctrl->name, ctrl->cur.val, ctrl->val);
499 switch (ctrl->id) {
500 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
501 case V4L2_CID_RF_TUNER_BANDWIDTH:
503 * TODO: Auto logic does not work 100% correctly as tuner driver
504 * do not have information to calculate maximum suitable
505 * bandwidth. Calculating it is responsible of master driver.
507 dev->f_bandwidth = dev->bandwidth->val;
508 ret = fc2580_set_params(dev);
509 break;
510 default:
511 dev_dbg(&client->dev, "unknown ctrl");
512 ret = -EINVAL;
514 return ret;
517 static const struct v4l2_ctrl_ops fc2580_ctrl_ops = {
518 .s_ctrl = fc2580_s_ctrl,
520 #endif
522 static struct v4l2_subdev *fc2580_get_v4l2_subdev(struct i2c_client *client)
524 struct fc2580_dev *dev = i2c_get_clientdata(client);
526 if (dev->subdev.ops)
527 return &dev->subdev;
528 else
529 return NULL;
532 static int fc2580_probe(struct i2c_client *client,
533 const struct i2c_device_id *id)
535 struct fc2580_dev *dev;
536 struct fc2580_platform_data *pdata = client->dev.platform_data;
537 struct dvb_frontend *fe = pdata->dvb_frontend;
538 int ret;
539 unsigned int uitmp;
540 static const struct regmap_config regmap_config = {
541 .reg_bits = 8,
542 .val_bits = 8,
545 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
546 if (!dev) {
547 ret = -ENOMEM;
548 goto err;
551 if (pdata->clk)
552 dev->clk = pdata->clk;
553 else
554 dev->clk = 16384000; /* internal clock */
555 dev->client = client;
556 dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
557 if (IS_ERR(dev->regmap)) {
558 ret = PTR_ERR(dev->regmap);
559 goto err_kfree;
562 /* check if the tuner is there */
563 ret = regmap_read(dev->regmap, 0x01, &uitmp);
564 if (ret)
565 goto err_kfree;
567 dev_dbg(&client->dev, "chip_id=%02x\n", uitmp);
569 switch (uitmp) {
570 case 0x56:
571 case 0x5a:
572 break;
573 default:
574 ret = -ENODEV;
575 goto err_kfree;
578 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
579 /* Register controls */
580 v4l2_ctrl_handler_init(&dev->hdl, 2);
581 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &fc2580_ctrl_ops,
582 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
583 0, 1, 1, 1);
584 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &fc2580_ctrl_ops,
585 V4L2_CID_RF_TUNER_BANDWIDTH,
586 3000, 10000000, 1, 3000);
587 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
588 if (dev->hdl.error) {
589 ret = dev->hdl.error;
590 dev_err(&client->dev, "Could not initialize controls\n");
591 v4l2_ctrl_handler_free(&dev->hdl);
592 goto err_kfree;
594 dev->subdev.ctrl_handler = &dev->hdl;
595 dev->f_frequency = bands[0].rangelow;
596 dev->f_bandwidth = dev->bandwidth->val;
597 v4l2_i2c_subdev_init(&dev->subdev, client, &fc2580_subdev_ops);
598 #endif
599 fe->tuner_priv = dev;
600 memcpy(&fe->ops.tuner_ops, &fc2580_dvb_tuner_ops,
601 sizeof(fe->ops.tuner_ops));
602 pdata->get_v4l2_subdev = fc2580_get_v4l2_subdev;
603 i2c_set_clientdata(client, dev);
605 dev_info(&client->dev, "FCI FC2580 successfully identified\n");
606 return 0;
607 err_kfree:
608 kfree(dev);
609 err:
610 dev_dbg(&client->dev, "failed=%d\n", ret);
611 return ret;
614 static int fc2580_remove(struct i2c_client *client)
616 struct fc2580_dev *dev = i2c_get_clientdata(client);
618 dev_dbg(&client->dev, "\n");
620 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
621 v4l2_ctrl_handler_free(&dev->hdl);
622 #endif
623 kfree(dev);
624 return 0;
627 static const struct i2c_device_id fc2580_id_table[] = {
628 {"fc2580", 0},
631 MODULE_DEVICE_TABLE(i2c, fc2580_id_table);
633 static struct i2c_driver fc2580_driver = {
634 .driver = {
635 .name = "fc2580",
636 .suppress_bind_attrs = true,
638 .probe = fc2580_probe,
639 .remove = fc2580_remove,
640 .id_table = fc2580_id_table,
643 module_i2c_driver(fc2580_driver);
645 MODULE_DESCRIPTION("FCI FC2580 silicon tuner driver");
646 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
647 MODULE_LICENSE("GPL");