xref: /openbmc/linux/drivers/media/tuners/fc2580.c (revision 8b302d43e84dc863729010a1ea9a5a2bc83d25de)
1 /*
2  * FCI FC2580 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 "fc2580_priv.h"
22 
23 /*
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....
28  */
29 
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)
34 {
35 	if (val == 0xff)
36 		return 0;
37 	else
38 		return regmap_write(dev->regmap, reg, val);
39 }
40 
41 static int fc2580_set_params(struct dvb_frontend *fe)
42 {
43 	struct fc2580_dev *dev = fe->tuner_priv;
44 	struct i2c_client *client = dev->client;
45 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
46 	int ret, i;
47 	unsigned int uitmp, div_ref, div_ref_val, div_n, k, k_cw, div_out;
48 	u64 f_vco;
49 	u8 synth_config;
50 	unsigned long timeout;
51 
52 	dev_dbg(&client->dev,
53 		"delivery_system=%u frequency=%u bandwidth_hz=%u\n",
54 		c->delivery_system, c->frequency, c->bandwidth_hz);
55 
56 	/*
57 	 * Fractional-N synthesizer
58 	 *
59 	 *                      +---------------------------------------+
60 	 *                      v                                       |
61 	 *  Fref   +----+     +----+     +-------+         +----+     +------+     +---+
62 	 * ------> | /R | --> | PD | --> |  VCO  | ------> | /2 | --> | /N.F | <-- | K |
63 	 *         +----+     +----+     +-------+         +----+     +------+     +---+
64 	 *                                 |
65 	 *                                 |
66 	 *                                 v
67 	 *                               +-------+  Fout
68 	 *                               | /Rout | ------>
69 	 *                               +-------+
70 	 */
71 	for (i = 0; i < ARRAY_SIZE(fc2580_pll_lut); i++) {
72 		if (c->frequency <= fc2580_pll_lut[i].freq)
73 			break;
74 	}
75 	if (i == ARRAY_SIZE(fc2580_pll_lut)) {
76 		ret = -EINVAL;
77 		goto err;
78 	}
79 
80 	#define DIV_PRE_N 2
81 	#define F_REF dev->clk
82 	div_out = fc2580_pll_lut[i].div_out;
83 	f_vco = (u64) c->frequency * div_out;
84 	synth_config = fc2580_pll_lut[i].band;
85 	if (f_vco < 2600000000ULL)
86 		synth_config |= 0x06;
87 	else
88 		synth_config |= 0x0e;
89 
90 	/* select reference divider R (keep PLL div N in valid range) */
91 	#define DIV_N_MIN 76
92 	if (f_vco >= div_u64((u64) DIV_PRE_N * DIV_N_MIN * F_REF, 1)) {
93 		div_ref = 1;
94 		div_ref_val = 0x00;
95 	} else if (f_vco >= div_u64((u64) DIV_PRE_N * DIV_N_MIN * F_REF, 2)) {
96 		div_ref = 2;
97 		div_ref_val = 0x10;
98 	} else {
99 		div_ref = 4;
100 		div_ref_val = 0x20;
101 	}
102 
103 	/* calculate PLL integer and fractional control word */
104 	uitmp = DIV_PRE_N * F_REF / div_ref;
105 	div_n = div_u64_rem(f_vco, uitmp, &k);
106 	k_cw = div_u64((u64) k * 0x100000, uitmp);
107 
108 	dev_dbg(&client->dev,
109 		"frequency=%u f_vco=%llu F_REF=%u div_ref=%u div_n=%u k=%u div_out=%u k_cw=%0x\n",
110 		c->frequency, f_vco, F_REF, div_ref, div_n, k, div_out, k_cw);
111 
112 	ret = regmap_write(dev->regmap, 0x02, synth_config);
113 	if (ret)
114 		goto err;
115 
116 	ret = regmap_write(dev->regmap, 0x18, div_ref_val << 0 | k_cw >> 16);
117 	if (ret)
118 		goto err;
119 
120 	ret = regmap_write(dev->regmap, 0x1a, (k_cw >> 8) & 0xff);
121 	if (ret)
122 		goto err;
123 
124 	ret = regmap_write(dev->regmap, 0x1b, (k_cw >> 0) & 0xff);
125 	if (ret)
126 		goto err;
127 
128 	ret = regmap_write(dev->regmap, 0x1c, div_n);
129 	if (ret)
130 		goto err;
131 
132 	/* registers */
133 	for (i = 0; i < ARRAY_SIZE(fc2580_freq_regs_lut); i++) {
134 		if (c->frequency <= fc2580_freq_regs_lut[i].freq)
135 			break;
136 	}
137 	if (i == ARRAY_SIZE(fc2580_freq_regs_lut)) {
138 		ret = -EINVAL;
139 		goto err;
140 	}
141 
142 	ret = fc2580_wr_reg_ff(dev, 0x25, fc2580_freq_regs_lut[i].r25_val);
143 	if (ret)
144 		goto err;
145 
146 	ret = fc2580_wr_reg_ff(dev, 0x27, fc2580_freq_regs_lut[i].r27_val);
147 	if (ret)
148 		goto err;
149 
150 	ret = fc2580_wr_reg_ff(dev, 0x28, fc2580_freq_regs_lut[i].r28_val);
151 	if (ret)
152 		goto err;
153 
154 	ret = fc2580_wr_reg_ff(dev, 0x29, fc2580_freq_regs_lut[i].r29_val);
155 	if (ret)
156 		goto err;
157 
158 	ret = fc2580_wr_reg_ff(dev, 0x2b, fc2580_freq_regs_lut[i].r2b_val);
159 	if (ret)
160 		goto err;
161 
162 	ret = fc2580_wr_reg_ff(dev, 0x2c, fc2580_freq_regs_lut[i].r2c_val);
163 	if (ret)
164 		goto err;
165 
166 	ret = fc2580_wr_reg_ff(dev, 0x2d, fc2580_freq_regs_lut[i].r2d_val);
167 	if (ret)
168 		goto err;
169 
170 	ret = fc2580_wr_reg_ff(dev, 0x30, fc2580_freq_regs_lut[i].r30_val);
171 	if (ret)
172 		goto err;
173 
174 	ret = fc2580_wr_reg_ff(dev, 0x44, fc2580_freq_regs_lut[i].r44_val);
175 	if (ret)
176 		goto err;
177 
178 	ret = fc2580_wr_reg_ff(dev, 0x50, fc2580_freq_regs_lut[i].r50_val);
179 	if (ret)
180 		goto err;
181 
182 	ret = fc2580_wr_reg_ff(dev, 0x53, fc2580_freq_regs_lut[i].r53_val);
183 	if (ret)
184 		goto err;
185 
186 	ret = fc2580_wr_reg_ff(dev, 0x5f, fc2580_freq_regs_lut[i].r5f_val);
187 	if (ret)
188 		goto err;
189 
190 	ret = fc2580_wr_reg_ff(dev, 0x61, fc2580_freq_regs_lut[i].r61_val);
191 	if (ret)
192 		goto err;
193 
194 	ret = fc2580_wr_reg_ff(dev, 0x62, fc2580_freq_regs_lut[i].r62_val);
195 	if (ret)
196 		goto err;
197 
198 	ret = fc2580_wr_reg_ff(dev, 0x63, fc2580_freq_regs_lut[i].r63_val);
199 	if (ret)
200 		goto err;
201 
202 	ret = fc2580_wr_reg_ff(dev, 0x67, fc2580_freq_regs_lut[i].r67_val);
203 	if (ret)
204 		goto err;
205 
206 	ret = fc2580_wr_reg_ff(dev, 0x68, fc2580_freq_regs_lut[i].r68_val);
207 	if (ret)
208 		goto err;
209 
210 	ret = fc2580_wr_reg_ff(dev, 0x69, fc2580_freq_regs_lut[i].r69_val);
211 	if (ret)
212 		goto err;
213 
214 	ret = fc2580_wr_reg_ff(dev, 0x6a, fc2580_freq_regs_lut[i].r6a_val);
215 	if (ret)
216 		goto err;
217 
218 	ret = fc2580_wr_reg_ff(dev, 0x6b, fc2580_freq_regs_lut[i].r6b_val);
219 	if (ret)
220 		goto err;
221 
222 	ret = fc2580_wr_reg_ff(dev, 0x6c, fc2580_freq_regs_lut[i].r6c_val);
223 	if (ret)
224 		goto err;
225 
226 	ret = fc2580_wr_reg_ff(dev, 0x6d, fc2580_freq_regs_lut[i].r6d_val);
227 	if (ret)
228 		goto err;
229 
230 	ret = fc2580_wr_reg_ff(dev, 0x6e, fc2580_freq_regs_lut[i].r6e_val);
231 	if (ret)
232 		goto err;
233 
234 	ret = fc2580_wr_reg_ff(dev, 0x6f, fc2580_freq_regs_lut[i].r6f_val);
235 	if (ret)
236 		goto err;
237 
238 	/* IF filters */
239 	for (i = 0; i < ARRAY_SIZE(fc2580_if_filter_lut); i++) {
240 		if (c->bandwidth_hz <= fc2580_if_filter_lut[i].freq)
241 			break;
242 	}
243 	if (i == ARRAY_SIZE(fc2580_if_filter_lut)) {
244 		ret = -EINVAL;
245 		goto err;
246 	}
247 
248 	ret = regmap_write(dev->regmap, 0x36, fc2580_if_filter_lut[i].r36_val);
249 	if (ret)
250 		goto err;
251 
252 	uitmp = (unsigned int) 8058000 - (c->bandwidth_hz * 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;
257 
258 	ret = regmap_write(dev->regmap, 0x39, fc2580_if_filter_lut[i].r39_val);
259 	if (ret)
260 		goto err;
261 
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;
268 
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;
274 
275 		ret = regmap_write(dev->regmap, 0x2e, 0x01);
276 		if (ret)
277 			goto err;
278 	}
279 	if (uitmp != 0xc0)
280 		dev_dbg(&client->dev, "filter did not lock %02x\n", uitmp);
281 
282 	return 0;
283 err:
284 	dev_dbg(&client->dev, "failed=%d\n", ret);
285 	return ret;
286 }
287 
288 static int fc2580_init(struct dvb_frontend *fe)
289 {
290 	struct fc2580_dev *dev = fe->tuner_priv;
291 	struct i2c_client *client = dev->client;
292 	int ret, i;
293 
294 	dev_dbg(&client->dev, "\n");
295 
296 	for (i = 0; i < ARRAY_SIZE(fc2580_init_reg_vals); i++) {
297 		ret = regmap_write(dev->regmap, fc2580_init_reg_vals[i].reg,
298 				fc2580_init_reg_vals[i].val);
299 		if (ret)
300 			goto err;
301 	}
302 
303 	return 0;
304 err:
305 	dev_dbg(&client->dev, "failed=%d\n", ret);
306 	return ret;
307 }
308 
309 static int fc2580_sleep(struct dvb_frontend *fe)
310 {
311 	struct fc2580_dev *dev = fe->tuner_priv;
312 	struct i2c_client *client = dev->client;
313 	int ret;
314 
315 	dev_dbg(&client->dev, "\n");
316 
317 	ret = regmap_write(dev->regmap, 0x02, 0x0a);
318 	if (ret)
319 		goto err;
320 
321 	return 0;
322 err:
323 	dev_dbg(&client->dev, "failed=%d\n", ret);
324 	return ret;
325 }
326 
327 static int fc2580_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
328 {
329 	struct fc2580_dev *dev = fe->tuner_priv;
330 	struct i2c_client *client = dev->client;
331 
332 	dev_dbg(&client->dev, "\n");
333 
334 	*frequency = 0; /* Zero-IF */
335 
336 	return 0;
337 }
338 
339 static const struct dvb_tuner_ops fc2580_tuner_ops = {
340 	.info = {
341 		.name           = "FCI FC2580",
342 		.frequency_min  = 174000000,
343 		.frequency_max  = 862000000,
344 	},
345 
346 	.init = fc2580_init,
347 	.sleep = fc2580_sleep,
348 	.set_params = fc2580_set_params,
349 
350 	.get_if_frequency = fc2580_get_if_frequency,
351 };
352 
353 static int fc2580_probe(struct i2c_client *client,
354 			const struct i2c_device_id *id)
355 {
356 	struct fc2580_dev *dev;
357 	struct fc2580_platform_data *pdata = client->dev.platform_data;
358 	struct dvb_frontend *fe = pdata->dvb_frontend;
359 	int ret;
360 	unsigned int uitmp;
361 	static const struct regmap_config regmap_config = {
362 		.reg_bits = 8,
363 		.val_bits = 8,
364 	};
365 
366 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
367 	if (!dev) {
368 		ret = -ENOMEM;
369 		goto err;
370 	}
371 
372 	if (pdata->clk)
373 		dev->clk = pdata->clk;
374 	else
375 		dev->clk = 16384000; /* internal clock */
376 	dev->client = client;
377 	dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
378 	if (IS_ERR(dev->regmap)) {
379 		ret = PTR_ERR(dev->regmap);
380 		goto err_kfree;
381 	}
382 
383 	/* check if the tuner is there */
384 	ret = regmap_read(dev->regmap, 0x01, &uitmp);
385 	if (ret)
386 		goto err_kfree;
387 
388 	dev_dbg(&client->dev, "chip_id=%02x\n", uitmp);
389 
390 	switch (uitmp) {
391 	case 0x56:
392 	case 0x5a:
393 		break;
394 	default:
395 		goto err_kfree;
396 	}
397 
398 	fe->tuner_priv = dev;
399 	memcpy(&fe->ops.tuner_ops, &fc2580_tuner_ops,
400 			sizeof(struct dvb_tuner_ops));
401 	i2c_set_clientdata(client, dev);
402 
403 	dev_info(&client->dev, "FCI FC2580 successfully identified\n");
404 	return 0;
405 err_kfree:
406 	kfree(dev);
407 err:
408 	dev_dbg(&client->dev, "failed=%d\n", ret);
409 	return ret;
410 }
411 
412 static int fc2580_remove(struct i2c_client *client)
413 {
414 	struct fc2580_dev *dev = i2c_get_clientdata(client);
415 
416 	dev_dbg(&client->dev, "\n");
417 
418 	kfree(dev);
419 	return 0;
420 }
421 
422 static const struct i2c_device_id fc2580_id_table[] = {
423 	{"fc2580", 0},
424 	{}
425 };
426 MODULE_DEVICE_TABLE(i2c, fc2580_id_table);
427 
428 static struct i2c_driver fc2580_driver = {
429 	.driver = {
430 		.owner	= THIS_MODULE,
431 		.name	= "fc2580",
432 		.suppress_bind_attrs = true,
433 	},
434 	.probe		= fc2580_probe,
435 	.remove		= fc2580_remove,
436 	.id_table	= fc2580_id_table,
437 };
438 
439 module_i2c_driver(fc2580_driver);
440 
441 MODULE_DESCRIPTION("FCI FC2580 silicon tuner driver");
442 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
443 MODULE_LICENSE("GPL");
444