diff options
Diffstat (limited to 'drivers/media/dvb-frontends/af9013.c')
-rw-r--r-- | drivers/media/dvb-frontends/af9013.c | 1524 |
1 files changed, 1524 insertions, 0 deletions
diff --git a/drivers/media/dvb-frontends/af9013.c b/drivers/media/dvb-frontends/af9013.c new file mode 100644 index 000000000000..5bc570d77846 --- /dev/null +++ b/drivers/media/dvb-frontends/af9013.c | |||
@@ -0,0 +1,1524 @@ | |||
1 | /* | ||
2 | * Afatech AF9013 demodulator driver | ||
3 | * | ||
4 | * Copyright (C) 2007 Antti Palosaari <crope@iki.fi> | ||
5 | * Copyright (C) 2011 Antti Palosaari <crope@iki.fi> | ||
6 | * | ||
7 | * Thanks to Afatech who kindly provided information. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
22 | * | ||
23 | */ | ||
24 | |||
25 | #include "af9013_priv.h" | ||
26 | |||
27 | int af9013_debug; | ||
28 | module_param_named(debug, af9013_debug, int, 0644); | ||
29 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); | ||
30 | |||
31 | struct af9013_state { | ||
32 | struct i2c_adapter *i2c; | ||
33 | struct dvb_frontend fe; | ||
34 | struct af9013_config config; | ||
35 | |||
36 | /* tuner/demod RF and IF AGC limits used for signal strength calc */ | ||
37 | u8 signal_strength_en, rf_50, rf_80, if_50, if_80; | ||
38 | u16 signal_strength; | ||
39 | u32 ber; | ||
40 | u32 ucblocks; | ||
41 | u16 snr; | ||
42 | u32 bandwidth_hz; | ||
43 | fe_status_t fe_status; | ||
44 | unsigned long set_frontend_jiffies; | ||
45 | unsigned long read_status_jiffies; | ||
46 | bool first_tune; | ||
47 | bool i2c_gate_state; | ||
48 | unsigned int statistics_step:3; | ||
49 | struct delayed_work statistics_work; | ||
50 | }; | ||
51 | |||
52 | /* write multiple registers */ | ||
53 | static int af9013_wr_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg, | ||
54 | const u8 *val, int len) | ||
55 | { | ||
56 | int ret; | ||
57 | u8 buf[3+len]; | ||
58 | struct i2c_msg msg[1] = { | ||
59 | { | ||
60 | .addr = priv->config.i2c_addr, | ||
61 | .flags = 0, | ||
62 | .len = sizeof(buf), | ||
63 | .buf = buf, | ||
64 | } | ||
65 | }; | ||
66 | |||
67 | buf[0] = (reg >> 8) & 0xff; | ||
68 | buf[1] = (reg >> 0) & 0xff; | ||
69 | buf[2] = mbox; | ||
70 | memcpy(&buf[3], val, len); | ||
71 | |||
72 | ret = i2c_transfer(priv->i2c, msg, 1); | ||
73 | if (ret == 1) { | ||
74 | ret = 0; | ||
75 | } else { | ||
76 | warn("i2c wr failed=%d reg=%04x len=%d", ret, reg, len); | ||
77 | ret = -EREMOTEIO; | ||
78 | } | ||
79 | return ret; | ||
80 | } | ||
81 | |||
82 | /* read multiple registers */ | ||
83 | static int af9013_rd_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg, | ||
84 | u8 *val, int len) | ||
85 | { | ||
86 | int ret; | ||
87 | u8 buf[3]; | ||
88 | struct i2c_msg msg[2] = { | ||
89 | { | ||
90 | .addr = priv->config.i2c_addr, | ||
91 | .flags = 0, | ||
92 | .len = 3, | ||
93 | .buf = buf, | ||
94 | }, { | ||
95 | .addr = priv->config.i2c_addr, | ||
96 | .flags = I2C_M_RD, | ||
97 | .len = len, | ||
98 | .buf = val, | ||
99 | } | ||
100 | }; | ||
101 | |||
102 | buf[0] = (reg >> 8) & 0xff; | ||
103 | buf[1] = (reg >> 0) & 0xff; | ||
104 | buf[2] = mbox; | ||
105 | |||
106 | ret = i2c_transfer(priv->i2c, msg, 2); | ||
107 | if (ret == 2) { | ||
108 | ret = 0; | ||
109 | } else { | ||
110 | warn("i2c rd failed=%d reg=%04x len=%d", ret, reg, len); | ||
111 | ret = -EREMOTEIO; | ||
112 | } | ||
113 | return ret; | ||
114 | } | ||
115 | |||
116 | /* write multiple registers */ | ||
117 | static int af9013_wr_regs(struct af9013_state *priv, u16 reg, const u8 *val, | ||
118 | int len) | ||
119 | { | ||
120 | int ret, i; | ||
121 | u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(1 << 0); | ||
122 | |||
123 | if ((priv->config.ts_mode == AF9013_TS_USB) && | ||
124 | ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) { | ||
125 | mbox |= ((len - 1) << 2); | ||
126 | ret = af9013_wr_regs_i2c(priv, mbox, reg, val, len); | ||
127 | } else { | ||
128 | for (i = 0; i < len; i++) { | ||
129 | ret = af9013_wr_regs_i2c(priv, mbox, reg+i, val+i, 1); | ||
130 | if (ret) | ||
131 | goto err; | ||
132 | } | ||
133 | } | ||
134 | |||
135 | err: | ||
136 | return 0; | ||
137 | } | ||
138 | |||
139 | /* read multiple registers */ | ||
140 | static int af9013_rd_regs(struct af9013_state *priv, u16 reg, u8 *val, int len) | ||
141 | { | ||
142 | int ret, i; | ||
143 | u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(0 << 0); | ||
144 | |||
145 | if ((priv->config.ts_mode == AF9013_TS_USB) && | ||
146 | ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) { | ||
147 | mbox |= ((len - 1) << 2); | ||
148 | ret = af9013_rd_regs_i2c(priv, mbox, reg, val, len); | ||
149 | } else { | ||
150 | for (i = 0; i < len; i++) { | ||
151 | ret = af9013_rd_regs_i2c(priv, mbox, reg+i, val+i, 1); | ||
152 | if (ret) | ||
153 | goto err; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | err: | ||
158 | return 0; | ||
159 | } | ||
160 | |||
161 | /* write single register */ | ||
162 | static int af9013_wr_reg(struct af9013_state *priv, u16 reg, u8 val) | ||
163 | { | ||
164 | return af9013_wr_regs(priv, reg, &val, 1); | ||
165 | } | ||
166 | |||
167 | /* read single register */ | ||
168 | static int af9013_rd_reg(struct af9013_state *priv, u16 reg, u8 *val) | ||
169 | { | ||
170 | return af9013_rd_regs(priv, reg, val, 1); | ||
171 | } | ||
172 | |||
173 | static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val, | ||
174 | u8 len) | ||
175 | { | ||
176 | u8 mbox = (1 << 7)|(1 << 6)|((len - 1) << 2)|(1 << 1)|(1 << 0); | ||
177 | return af9013_wr_regs_i2c(state, mbox, reg, val, len); | ||
178 | } | ||
179 | |||
180 | static int af9013_wr_reg_bits(struct af9013_state *state, u16 reg, int pos, | ||
181 | int len, u8 val) | ||
182 | { | ||
183 | int ret; | ||
184 | u8 tmp, mask; | ||
185 | |||
186 | /* no need for read if whole reg is written */ | ||
187 | if (len != 8) { | ||
188 | ret = af9013_rd_reg(state, reg, &tmp); | ||
189 | if (ret) | ||
190 | return ret; | ||
191 | |||
192 | mask = (0xff >> (8 - len)) << pos; | ||
193 | val <<= pos; | ||
194 | tmp &= ~mask; | ||
195 | val |= tmp; | ||
196 | } | ||
197 | |||
198 | return af9013_wr_reg(state, reg, val); | ||
199 | } | ||
200 | |||
201 | static int af9013_rd_reg_bits(struct af9013_state *state, u16 reg, int pos, | ||
202 | int len, u8 *val) | ||
203 | { | ||
204 | int ret; | ||
205 | u8 tmp; | ||
206 | |||
207 | ret = af9013_rd_reg(state, reg, &tmp); | ||
208 | if (ret) | ||
209 | return ret; | ||
210 | |||
211 | *val = (tmp >> pos); | ||
212 | *val &= (0xff >> (8 - len)); | ||
213 | |||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval) | ||
218 | { | ||
219 | int ret; | ||
220 | u8 pos; | ||
221 | u16 addr; | ||
222 | |||
223 | dbg("%s: gpio=%d gpioval=%02x", __func__, gpio, gpioval); | ||
224 | |||
225 | /* | ||
226 | * GPIO0 & GPIO1 0xd735 | ||
227 | * GPIO2 & GPIO3 0xd736 | ||
228 | */ | ||
229 | |||
230 | switch (gpio) { | ||
231 | case 0: | ||
232 | case 1: | ||
233 | addr = 0xd735; | ||
234 | break; | ||
235 | case 2: | ||
236 | case 3: | ||
237 | addr = 0xd736; | ||
238 | break; | ||
239 | |||
240 | default: | ||
241 | err("invalid gpio:%d\n", gpio); | ||
242 | ret = -EINVAL; | ||
243 | goto err; | ||
244 | }; | ||
245 | |||
246 | switch (gpio) { | ||
247 | case 0: | ||
248 | case 2: | ||
249 | pos = 0; | ||
250 | break; | ||
251 | case 1: | ||
252 | case 3: | ||
253 | default: | ||
254 | pos = 4; | ||
255 | break; | ||
256 | }; | ||
257 | |||
258 | ret = af9013_wr_reg_bits(state, addr, pos, 4, gpioval); | ||
259 | if (ret) | ||
260 | goto err; | ||
261 | |||
262 | return ret; | ||
263 | err: | ||
264 | dbg("%s: failed=%d", __func__, ret); | ||
265 | return ret; | ||
266 | } | ||
267 | |||
268 | static u32 af913_div(u32 a, u32 b, u32 x) | ||
269 | { | ||
270 | u32 r = 0, c = 0, i; | ||
271 | |||
272 | dbg("%s: a=%d b=%d x=%d", __func__, a, b, x); | ||
273 | |||
274 | if (a > b) { | ||
275 | c = a / b; | ||
276 | a = a - c * b; | ||
277 | } | ||
278 | |||
279 | for (i = 0; i < x; i++) { | ||
280 | if (a >= b) { | ||
281 | r += 1; | ||
282 | a -= b; | ||
283 | } | ||
284 | a <<= 1; | ||
285 | r <<= 1; | ||
286 | } | ||
287 | r = (c << (u32)x) + r; | ||
288 | |||
289 | dbg("%s: a=%d b=%d x=%d r=%x", __func__, a, b, x, r); | ||
290 | return r; | ||
291 | } | ||
292 | |||
293 | static int af9013_power_ctrl(struct af9013_state *state, u8 onoff) | ||
294 | { | ||
295 | int ret, i; | ||
296 | u8 tmp; | ||
297 | |||
298 | dbg("%s: onoff=%d", __func__, onoff); | ||
299 | |||
300 | /* enable reset */ | ||
301 | ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 1); | ||
302 | if (ret) | ||
303 | goto err; | ||
304 | |||
305 | /* start reset mechanism */ | ||
306 | ret = af9013_wr_reg(state, 0xaeff, 1); | ||
307 | if (ret) | ||
308 | goto err; | ||
309 | |||
310 | /* wait reset performs */ | ||
311 | for (i = 0; i < 150; i++) { | ||
312 | ret = af9013_rd_reg_bits(state, 0xd417, 1, 1, &tmp); | ||
313 | if (ret) | ||
314 | goto err; | ||
315 | |||
316 | if (tmp) | ||
317 | break; /* reset done */ | ||
318 | |||
319 | usleep_range(5000, 25000); | ||
320 | } | ||
321 | |||
322 | if (!tmp) | ||
323 | return -ETIMEDOUT; | ||
324 | |||
325 | if (onoff) { | ||
326 | /* clear reset */ | ||
327 | ret = af9013_wr_reg_bits(state, 0xd417, 1, 1, 0); | ||
328 | if (ret) | ||
329 | goto err; | ||
330 | |||
331 | /* disable reset */ | ||
332 | ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 0); | ||
333 | |||
334 | /* power on */ | ||
335 | ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 0); | ||
336 | } else { | ||
337 | /* power off */ | ||
338 | ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 1); | ||
339 | } | ||
340 | |||
341 | return ret; | ||
342 | err: | ||
343 | dbg("%s: failed=%d", __func__, ret); | ||
344 | return ret; | ||
345 | } | ||
346 | |||
347 | static int af9013_statistics_ber_unc_start(struct dvb_frontend *fe) | ||
348 | { | ||
349 | struct af9013_state *state = fe->demodulator_priv; | ||
350 | int ret; | ||
351 | |||
352 | dbg("%s", __func__); | ||
353 | |||
354 | /* reset and start BER counter */ | ||
355 | ret = af9013_wr_reg_bits(state, 0xd391, 4, 1, 1); | ||
356 | if (ret) | ||
357 | goto err; | ||
358 | |||
359 | return ret; | ||
360 | err: | ||
361 | dbg("%s: failed=%d", __func__, ret); | ||
362 | return ret; | ||
363 | } | ||
364 | |||
365 | static int af9013_statistics_ber_unc_result(struct dvb_frontend *fe) | ||
366 | { | ||
367 | struct af9013_state *state = fe->demodulator_priv; | ||
368 | int ret; | ||
369 | u8 buf[5]; | ||
370 | |||
371 | dbg("%s", __func__); | ||
372 | |||
373 | /* check if error bit count is ready */ | ||
374 | ret = af9013_rd_reg_bits(state, 0xd391, 4, 1, &buf[0]); | ||
375 | if (ret) | ||
376 | goto err; | ||
377 | |||
378 | if (!buf[0]) { | ||
379 | dbg("%s: not ready", __func__); | ||
380 | return 0; | ||
381 | } | ||
382 | |||
383 | ret = af9013_rd_regs(state, 0xd387, buf, 5); | ||
384 | if (ret) | ||
385 | goto err; | ||
386 | |||
387 | state->ber = (buf[2] << 16) | (buf[1] << 8) | buf[0]; | ||
388 | state->ucblocks += (buf[4] << 8) | buf[3]; | ||
389 | |||
390 | return ret; | ||
391 | err: | ||
392 | dbg("%s: failed=%d", __func__, ret); | ||
393 | return ret; | ||
394 | } | ||
395 | |||
396 | static int af9013_statistics_snr_start(struct dvb_frontend *fe) | ||
397 | { | ||
398 | struct af9013_state *state = fe->demodulator_priv; | ||
399 | int ret; | ||
400 | |||
401 | dbg("%s", __func__); | ||
402 | |||
403 | /* start SNR meas */ | ||
404 | ret = af9013_wr_reg_bits(state, 0xd2e1, 3, 1, 1); | ||
405 | if (ret) | ||
406 | goto err; | ||
407 | |||
408 | return ret; | ||
409 | err: | ||
410 | dbg("%s: failed=%d", __func__, ret); | ||
411 | return ret; | ||
412 | } | ||
413 | |||
414 | static int af9013_statistics_snr_result(struct dvb_frontend *fe) | ||
415 | { | ||
416 | struct af9013_state *state = fe->demodulator_priv; | ||
417 | int ret, i, len; | ||
418 | u8 buf[3], tmp; | ||
419 | u32 snr_val; | ||
420 | const struct af9013_snr *uninitialized_var(snr_lut); | ||
421 | |||
422 | dbg("%s", __func__); | ||
423 | |||
424 | /* check if SNR ready */ | ||
425 | ret = af9013_rd_reg_bits(state, 0xd2e1, 3, 1, &tmp); | ||
426 | if (ret) | ||
427 | goto err; | ||
428 | |||
429 | if (!tmp) { | ||
430 | dbg("%s: not ready", __func__); | ||
431 | return 0; | ||
432 | } | ||
433 | |||
434 | /* read value */ | ||
435 | ret = af9013_rd_regs(state, 0xd2e3, buf, 3); | ||
436 | if (ret) | ||
437 | goto err; | ||
438 | |||
439 | snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; | ||
440 | |||
441 | /* read current modulation */ | ||
442 | ret = af9013_rd_reg(state, 0xd3c1, &tmp); | ||
443 | if (ret) | ||
444 | goto err; | ||
445 | |||
446 | switch ((tmp >> 6) & 3) { | ||
447 | case 0: | ||
448 | len = ARRAY_SIZE(qpsk_snr_lut); | ||
449 | snr_lut = qpsk_snr_lut; | ||
450 | break; | ||
451 | case 1: | ||
452 | len = ARRAY_SIZE(qam16_snr_lut); | ||
453 | snr_lut = qam16_snr_lut; | ||
454 | break; | ||
455 | case 2: | ||
456 | len = ARRAY_SIZE(qam64_snr_lut); | ||
457 | snr_lut = qam64_snr_lut; | ||
458 | break; | ||
459 | default: | ||
460 | goto err; | ||
461 | break; | ||
462 | } | ||
463 | |||
464 | for (i = 0; i < len; i++) { | ||
465 | tmp = snr_lut[i].snr; | ||
466 | |||
467 | if (snr_val < snr_lut[i].val) | ||
468 | break; | ||
469 | } | ||
470 | state->snr = tmp * 10; /* dB/10 */ | ||
471 | |||
472 | return ret; | ||
473 | err: | ||
474 | dbg("%s: failed=%d", __func__, ret); | ||
475 | return ret; | ||
476 | } | ||
477 | |||
478 | static int af9013_statistics_signal_strength(struct dvb_frontend *fe) | ||
479 | { | ||
480 | struct af9013_state *state = fe->demodulator_priv; | ||
481 | int ret = 0; | ||
482 | u8 buf[2], rf_gain, if_gain; | ||
483 | int signal_strength; | ||
484 | |||
485 | dbg("%s", __func__); | ||
486 | |||
487 | if (!state->signal_strength_en) | ||
488 | return 0; | ||
489 | |||
490 | ret = af9013_rd_regs(state, 0xd07c, buf, 2); | ||
491 | if (ret) | ||
492 | goto err; | ||
493 | |||
494 | rf_gain = buf[0]; | ||
495 | if_gain = buf[1]; | ||
496 | |||
497 | signal_strength = (0xffff / \ | ||
498 | (9 * (state->rf_50 + state->if_50) - \ | ||
499 | 11 * (state->rf_80 + state->if_80))) * \ | ||
500 | (10 * (rf_gain + if_gain) - \ | ||
501 | 11 * (state->rf_80 + state->if_80)); | ||
502 | if (signal_strength < 0) | ||
503 | signal_strength = 0; | ||
504 | else if (signal_strength > 0xffff) | ||
505 | signal_strength = 0xffff; | ||
506 | |||
507 | state->signal_strength = signal_strength; | ||
508 | |||
509 | return ret; | ||
510 | err: | ||
511 | dbg("%s: failed=%d", __func__, ret); | ||
512 | return ret; | ||
513 | } | ||
514 | |||
515 | static void af9013_statistics_work(struct work_struct *work) | ||
516 | { | ||
517 | struct af9013_state *state = container_of(work, | ||
518 | struct af9013_state, statistics_work.work); | ||
519 | unsigned int next_msec; | ||
520 | |||
521 | /* update only signal strength when demod is not locked */ | ||
522 | if (!(state->fe_status & FE_HAS_LOCK)) { | ||
523 | state->statistics_step = 0; | ||
524 | state->ber = 0; | ||
525 | state->snr = 0; | ||
526 | } | ||
527 | |||
528 | switch (state->statistics_step) { | ||
529 | default: | ||
530 | state->statistics_step = 0; | ||
531 | case 0: | ||
532 | af9013_statistics_signal_strength(&state->fe); | ||
533 | state->statistics_step++; | ||
534 | next_msec = 300; | ||
535 | break; | ||
536 | case 1: | ||
537 | af9013_statistics_snr_start(&state->fe); | ||
538 | state->statistics_step++; | ||
539 | next_msec = 200; | ||
540 | break; | ||
541 | case 2: | ||
542 | af9013_statistics_ber_unc_start(&state->fe); | ||
543 | state->statistics_step++; | ||
544 | next_msec = 1000; | ||
545 | break; | ||
546 | case 3: | ||
547 | af9013_statistics_snr_result(&state->fe); | ||
548 | state->statistics_step++; | ||
549 | next_msec = 400; | ||
550 | break; | ||
551 | case 4: | ||
552 | af9013_statistics_ber_unc_result(&state->fe); | ||
553 | state->statistics_step++; | ||
554 | next_msec = 100; | ||
555 | break; | ||
556 | } | ||
557 | |||
558 | schedule_delayed_work(&state->statistics_work, | ||
559 | msecs_to_jiffies(next_msec)); | ||
560 | } | ||
561 | |||
562 | static int af9013_get_tune_settings(struct dvb_frontend *fe, | ||
563 | struct dvb_frontend_tune_settings *fesettings) | ||
564 | { | ||
565 | fesettings->min_delay_ms = 800; | ||
566 | fesettings->step_size = 0; | ||
567 | fesettings->max_drift = 0; | ||
568 | |||
569 | return 0; | ||
570 | } | ||
571 | |||
572 | static int af9013_set_frontend(struct dvb_frontend *fe) | ||
573 | { | ||
574 | struct af9013_state *state = fe->demodulator_priv; | ||
575 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; | ||
576 | int ret, i, sampling_freq; | ||
577 | bool auto_mode, spec_inv; | ||
578 | u8 buf[6]; | ||
579 | u32 if_frequency, freq_cw; | ||
580 | |||
581 | dbg("%s: frequency=%d bandwidth_hz=%d", __func__, | ||
582 | c->frequency, c->bandwidth_hz); | ||
583 | |||
584 | /* program tuner */ | ||
585 | if (fe->ops.tuner_ops.set_params) | ||
586 | fe->ops.tuner_ops.set_params(fe); | ||
587 | |||
588 | /* program CFOE coefficients */ | ||
589 | if (c->bandwidth_hz != state->bandwidth_hz) { | ||
590 | for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { | ||
591 | if (coeff_lut[i].clock == state->config.clock && | ||
592 | coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { | ||
593 | break; | ||
594 | } | ||
595 | } | ||
596 | |||
597 | ret = af9013_wr_regs(state, 0xae00, coeff_lut[i].val, | ||
598 | sizeof(coeff_lut[i].val)); | ||
599 | } | ||
600 | |||
601 | /* program frequency control */ | ||
602 | if (c->bandwidth_hz != state->bandwidth_hz || state->first_tune) { | ||
603 | /* get used IF frequency */ | ||
604 | if (fe->ops.tuner_ops.get_if_frequency) | ||
605 | fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); | ||
606 | else | ||
607 | if_frequency = state->config.if_frequency; | ||
608 | |||
609 | sampling_freq = if_frequency; | ||
610 | |||
611 | while (sampling_freq > (state->config.clock / 2)) | ||
612 | sampling_freq -= state->config.clock; | ||
613 | |||
614 | if (sampling_freq < 0) { | ||
615 | sampling_freq *= -1; | ||
616 | spec_inv = state->config.spec_inv; | ||
617 | } else { | ||
618 | spec_inv = !state->config.spec_inv; | ||
619 | } | ||
620 | |||
621 | freq_cw = af913_div(sampling_freq, state->config.clock, 23); | ||
622 | |||
623 | if (spec_inv) | ||
624 | freq_cw = 0x800000 - freq_cw; | ||
625 | |||
626 | buf[0] = (freq_cw >> 0) & 0xff; | ||
627 | buf[1] = (freq_cw >> 8) & 0xff; | ||
628 | buf[2] = (freq_cw >> 16) & 0x7f; | ||
629 | |||
630 | freq_cw = 0x800000 - freq_cw; | ||
631 | |||
632 | buf[3] = (freq_cw >> 0) & 0xff; | ||
633 | buf[4] = (freq_cw >> 8) & 0xff; | ||
634 | buf[5] = (freq_cw >> 16) & 0x7f; | ||
635 | |||
636 | ret = af9013_wr_regs(state, 0xd140, buf, 3); | ||
637 | if (ret) | ||
638 | goto err; | ||
639 | |||
640 | ret = af9013_wr_regs(state, 0x9be7, buf, 6); | ||
641 | if (ret) | ||
642 | goto err; | ||
643 | } | ||
644 | |||
645 | /* clear TPS lock flag */ | ||
646 | ret = af9013_wr_reg_bits(state, 0xd330, 3, 1, 1); | ||
647 | if (ret) | ||
648 | goto err; | ||
649 | |||
650 | /* clear MPEG2 lock flag */ | ||
651 | ret = af9013_wr_reg_bits(state, 0xd507, 6, 1, 0); | ||
652 | if (ret) | ||
653 | goto err; | ||
654 | |||
655 | /* empty channel function */ | ||
656 | ret = af9013_wr_reg_bits(state, 0x9bfe, 0, 1, 0); | ||
657 | if (ret) | ||
658 | goto err; | ||
659 | |||
660 | /* empty DVB-T channel function */ | ||
661 | ret = af9013_wr_reg_bits(state, 0x9bc2, 0, 1, 0); | ||
662 | if (ret) | ||
663 | goto err; | ||
664 | |||
665 | /* transmission parameters */ | ||
666 | auto_mode = false; | ||
667 | memset(buf, 0, 3); | ||
668 | |||
669 | switch (c->transmission_mode) { | ||
670 | case TRANSMISSION_MODE_AUTO: | ||
671 | auto_mode = 1; | ||
672 | break; | ||
673 | case TRANSMISSION_MODE_2K: | ||
674 | break; | ||
675 | case TRANSMISSION_MODE_8K: | ||
676 | buf[0] |= (1 << 0); | ||
677 | break; | ||
678 | default: | ||
679 | dbg("%s: invalid transmission_mode", __func__); | ||
680 | auto_mode = 1; | ||
681 | } | ||
682 | |||
683 | switch (c->guard_interval) { | ||
684 | case GUARD_INTERVAL_AUTO: | ||
685 | auto_mode = 1; | ||
686 | break; | ||
687 | case GUARD_INTERVAL_1_32: | ||
688 | break; | ||
689 | case GUARD_INTERVAL_1_16: | ||
690 | buf[0] |= (1 << 2); | ||
691 | break; | ||
692 | case GUARD_INTERVAL_1_8: | ||
693 | buf[0] |= (2 << 2); | ||
694 | break; | ||
695 | case GUARD_INTERVAL_1_4: | ||
696 | buf[0] |= (3 << 2); | ||
697 | break; | ||
698 | default: | ||
699 | dbg("%s: invalid guard_interval", __func__); | ||
700 | auto_mode = 1; | ||
701 | } | ||
702 | |||
703 | switch (c->hierarchy) { | ||
704 | case HIERARCHY_AUTO: | ||
705 | auto_mode = 1; | ||
706 | break; | ||
707 | case HIERARCHY_NONE: | ||
708 | break; | ||
709 | case HIERARCHY_1: | ||
710 | buf[0] |= (1 << 4); | ||
711 | break; | ||
712 | case HIERARCHY_2: | ||
713 | buf[0] |= (2 << 4); | ||
714 | break; | ||
715 | case HIERARCHY_4: | ||
716 | buf[0] |= (3 << 4); | ||
717 | break; | ||
718 | default: | ||
719 | dbg("%s: invalid hierarchy", __func__); | ||
720 | auto_mode = 1; | ||
721 | }; | ||
722 | |||
723 | switch (c->modulation) { | ||
724 | case QAM_AUTO: | ||
725 | auto_mode = 1; | ||
726 | break; | ||
727 | case QPSK: | ||
728 | break; | ||
729 | case QAM_16: | ||
730 | buf[1] |= (1 << 6); | ||
731 | break; | ||
732 | case QAM_64: | ||
733 | buf[1] |= (2 << 6); | ||
734 | break; | ||
735 | default: | ||
736 | dbg("%s: invalid modulation", __func__); | ||
737 | auto_mode = 1; | ||
738 | } | ||
739 | |||
740 | /* Use HP. How and which case we can switch to LP? */ | ||
741 | buf[1] |= (1 << 4); | ||
742 | |||
743 | switch (c->code_rate_HP) { | ||
744 | case FEC_AUTO: | ||
745 | auto_mode = 1; | ||
746 | break; | ||
747 | case FEC_1_2: | ||
748 | break; | ||
749 | case FEC_2_3: | ||
750 | buf[2] |= (1 << 0); | ||
751 | break; | ||
752 | case FEC_3_4: | ||
753 | buf[2] |= (2 << 0); | ||
754 | break; | ||
755 | case FEC_5_6: | ||
756 | buf[2] |= (3 << 0); | ||
757 | break; | ||
758 | case FEC_7_8: | ||
759 | buf[2] |= (4 << 0); | ||
760 | break; | ||
761 | default: | ||
762 | dbg("%s: invalid code_rate_HP", __func__); | ||
763 | auto_mode = 1; | ||
764 | } | ||
765 | |||
766 | switch (c->code_rate_LP) { | ||
767 | case FEC_AUTO: | ||
768 | auto_mode = 1; | ||
769 | break; | ||
770 | case FEC_1_2: | ||
771 | break; | ||
772 | case FEC_2_3: | ||
773 | buf[2] |= (1 << 3); | ||
774 | break; | ||
775 | case FEC_3_4: | ||
776 | buf[2] |= (2 << 3); | ||
777 | break; | ||
778 | case FEC_5_6: | ||
779 | buf[2] |= (3 << 3); | ||
780 | break; | ||
781 | case FEC_7_8: | ||
782 | buf[2] |= (4 << 3); | ||
783 | break; | ||
784 | case FEC_NONE: | ||
785 | break; | ||
786 | default: | ||
787 | dbg("%s: invalid code_rate_LP", __func__); | ||
788 | auto_mode = 1; | ||
789 | } | ||
790 | |||
791 | switch (c->bandwidth_hz) { | ||
792 | case 6000000: | ||
793 | break; | ||
794 | case 7000000: | ||
795 | buf[1] |= (1 << 2); | ||
796 | break; | ||
797 | case 8000000: | ||
798 | buf[1] |= (2 << 2); | ||
799 | break; | ||
800 | default: | ||
801 | dbg("%s: invalid bandwidth_hz", __func__); | ||
802 | ret = -EINVAL; | ||
803 | goto err; | ||
804 | } | ||
805 | |||
806 | ret = af9013_wr_regs(state, 0xd3c0, buf, 3); | ||
807 | if (ret) | ||
808 | goto err; | ||
809 | |||
810 | if (auto_mode) { | ||
811 | /* clear easy mode flag */ | ||
812 | ret = af9013_wr_reg(state, 0xaefd, 0); | ||
813 | if (ret) | ||
814 | goto err; | ||
815 | |||
816 | dbg("%s: auto params", __func__); | ||
817 | } else { | ||
818 | /* set easy mode flag */ | ||
819 | ret = af9013_wr_reg(state, 0xaefd, 1); | ||
820 | if (ret) | ||
821 | goto err; | ||
822 | |||
823 | ret = af9013_wr_reg(state, 0xaefe, 0); | ||
824 | if (ret) | ||
825 | goto err; | ||
826 | |||
827 | dbg("%s: manual params", __func__); | ||
828 | } | ||
829 | |||
830 | /* tune */ | ||
831 | ret = af9013_wr_reg(state, 0xffff, 0); | ||
832 | if (ret) | ||
833 | goto err; | ||
834 | |||
835 | state->bandwidth_hz = c->bandwidth_hz; | ||
836 | state->set_frontend_jiffies = jiffies; | ||
837 | state->first_tune = false; | ||
838 | |||
839 | return ret; | ||
840 | err: | ||
841 | dbg("%s: failed=%d", __func__, ret); | ||
842 | return ret; | ||
843 | } | ||
844 | |||
845 | static int af9013_get_frontend(struct dvb_frontend *fe) | ||
846 | { | ||
847 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; | ||
848 | struct af9013_state *state = fe->demodulator_priv; | ||
849 | int ret; | ||
850 | u8 buf[3]; | ||
851 | |||
852 | dbg("%s", __func__); | ||
853 | |||
854 | ret = af9013_rd_regs(state, 0xd3c0, buf, 3); | ||
855 | if (ret) | ||
856 | goto err; | ||
857 | |||
858 | switch ((buf[1] >> 6) & 3) { | ||
859 | case 0: | ||
860 | c->modulation = QPSK; | ||
861 | break; | ||
862 | case 1: | ||
863 | c->modulation = QAM_16; | ||
864 | break; | ||
865 | case 2: | ||
866 | c->modulation = QAM_64; | ||
867 | break; | ||
868 | } | ||
869 | |||
870 | switch ((buf[0] >> 0) & 3) { | ||
871 | case 0: | ||
872 | c->transmission_mode = TRANSMISSION_MODE_2K; | ||
873 | break; | ||
874 | case 1: | ||
875 | c->transmission_mode = TRANSMISSION_MODE_8K; | ||
876 | } | ||
877 | |||
878 | switch ((buf[0] >> 2) & 3) { | ||
879 | case 0: | ||
880 | c->guard_interval = GUARD_INTERVAL_1_32; | ||
881 | break; | ||
882 | case 1: | ||
883 | c->guard_interval = GUARD_INTERVAL_1_16; | ||
884 | break; | ||
885 | case 2: | ||
886 | c->guard_interval = GUARD_INTERVAL_1_8; | ||
887 | break; | ||
888 | case 3: | ||
889 | c->guard_interval = GUARD_INTERVAL_1_4; | ||
890 | break; | ||
891 | } | ||
892 | |||
893 | switch ((buf[0] >> 4) & 7) { | ||
894 | case 0: | ||
895 | c->hierarchy = HIERARCHY_NONE; | ||
896 | break; | ||
897 | case 1: | ||
898 | c->hierarchy = HIERARCHY_1; | ||
899 | break; | ||
900 | case 2: | ||
901 | c->hierarchy = HIERARCHY_2; | ||
902 | break; | ||
903 | case 3: | ||
904 | c->hierarchy = HIERARCHY_4; | ||
905 | break; | ||
906 | } | ||
907 | |||
908 | switch ((buf[2] >> 0) & 7) { | ||
909 | case 0: | ||
910 | c->code_rate_HP = FEC_1_2; | ||
911 | break; | ||
912 | case 1: | ||
913 | c->code_rate_HP = FEC_2_3; | ||
914 | break; | ||
915 | case 2: | ||
916 | c->code_rate_HP = FEC_3_4; | ||
917 | break; | ||
918 | case 3: | ||
919 | c->code_rate_HP = FEC_5_6; | ||
920 | break; | ||
921 | case 4: | ||
922 | c->code_rate_HP = FEC_7_8; | ||
923 | break; | ||
924 | } | ||
925 | |||
926 | switch ((buf[2] >> 3) & 7) { | ||
927 | case 0: | ||
928 | c->code_rate_LP = FEC_1_2; | ||
929 | break; | ||
930 | case 1: | ||
931 | c->code_rate_LP = FEC_2_3; | ||
932 | break; | ||
933 | case 2: | ||
934 | c->code_rate_LP = FEC_3_4; | ||
935 | break; | ||
936 | case 3: | ||
937 | c->code_rate_LP = FEC_5_6; | ||
938 | break; | ||
939 | case 4: | ||
940 | c->code_rate_LP = FEC_7_8; | ||
941 | break; | ||
942 | } | ||
943 | |||
944 | switch ((buf[1] >> 2) & 3) { | ||
945 | case 0: | ||
946 | c->bandwidth_hz = 6000000; | ||
947 | break; | ||
948 | case 1: | ||
949 | c->bandwidth_hz = 7000000; | ||
950 | break; | ||
951 | case 2: | ||
952 | c->bandwidth_hz = 8000000; | ||
953 | break; | ||
954 | } | ||
955 | |||
956 | return ret; | ||
957 | err: | ||
958 | dbg("%s: failed=%d", __func__, ret); | ||
959 | return ret; | ||
960 | } | ||
961 | |||
962 | static int af9013_read_status(struct dvb_frontend *fe, fe_status_t *status) | ||
963 | { | ||
964 | struct af9013_state *state = fe->demodulator_priv; | ||
965 | int ret; | ||
966 | u8 tmp; | ||
967 | |||
968 | /* | ||
969 | * Return status from the cache if it is younger than 2000ms with the | ||
970 | * exception of last tune is done during 4000ms. | ||
971 | */ | ||
972 | if (time_is_after_jiffies( | ||
973 | state->read_status_jiffies + msecs_to_jiffies(2000)) && | ||
974 | time_is_before_jiffies( | ||
975 | state->set_frontend_jiffies + msecs_to_jiffies(4000)) | ||
976 | ) { | ||
977 | *status = state->fe_status; | ||
978 | return 0; | ||
979 | } else { | ||
980 | *status = 0; | ||
981 | } | ||
982 | |||
983 | /* MPEG2 lock */ | ||
984 | ret = af9013_rd_reg_bits(state, 0xd507, 6, 1, &tmp); | ||
985 | if (ret) | ||
986 | goto err; | ||
987 | |||
988 | if (tmp) | ||
989 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | | ||
990 | FE_HAS_SYNC | FE_HAS_LOCK; | ||
991 | |||
992 | if (!*status) { | ||
993 | /* TPS lock */ | ||
994 | ret = af9013_rd_reg_bits(state, 0xd330, 3, 1, &tmp); | ||
995 | if (ret) | ||
996 | goto err; | ||
997 | |||
998 | if (tmp) | ||
999 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | | ||
1000 | FE_HAS_VITERBI; | ||
1001 | } | ||
1002 | |||
1003 | state->fe_status = *status; | ||
1004 | state->read_status_jiffies = jiffies; | ||
1005 | |||
1006 | return ret; | ||
1007 | err: | ||
1008 | dbg("%s: failed=%d", __func__, ret); | ||
1009 | return ret; | ||
1010 | } | ||
1011 | |||
1012 | static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr) | ||
1013 | { | ||
1014 | struct af9013_state *state = fe->demodulator_priv; | ||
1015 | *snr = state->snr; | ||
1016 | return 0; | ||
1017 | } | ||
1018 | |||
1019 | static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength) | ||
1020 | { | ||
1021 | struct af9013_state *state = fe->demodulator_priv; | ||
1022 | *strength = state->signal_strength; | ||
1023 | return 0; | ||
1024 | } | ||
1025 | |||
1026 | static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber) | ||
1027 | { | ||
1028 | struct af9013_state *state = fe->demodulator_priv; | ||
1029 | *ber = state->ber; | ||
1030 | return 0; | ||
1031 | } | ||
1032 | |||
1033 | static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) | ||
1034 | { | ||
1035 | struct af9013_state *state = fe->demodulator_priv; | ||
1036 | *ucblocks = state->ucblocks; | ||
1037 | return 0; | ||
1038 | } | ||
1039 | |||
1040 | static int af9013_init(struct dvb_frontend *fe) | ||
1041 | { | ||
1042 | struct af9013_state *state = fe->demodulator_priv; | ||
1043 | int ret, i, len; | ||
1044 | u8 buf[3], tmp; | ||
1045 | u32 adc_cw; | ||
1046 | const struct af9013_reg_bit *init; | ||
1047 | |||
1048 | dbg("%s", __func__); | ||
1049 | |||
1050 | /* power on */ | ||
1051 | ret = af9013_power_ctrl(state, 1); | ||
1052 | if (ret) | ||
1053 | goto err; | ||
1054 | |||
1055 | /* enable ADC */ | ||
1056 | ret = af9013_wr_reg(state, 0xd73a, 0xa4); | ||
1057 | if (ret) | ||
1058 | goto err; | ||
1059 | |||
1060 | /* write API version to firmware */ | ||
1061 | ret = af9013_wr_regs(state, 0x9bf2, state->config.api_version, 4); | ||
1062 | if (ret) | ||
1063 | goto err; | ||
1064 | |||
1065 | /* program ADC control */ | ||
1066 | switch (state->config.clock) { | ||
1067 | case 28800000: /* 28.800 MHz */ | ||
1068 | tmp = 0; | ||
1069 | break; | ||
1070 | case 20480000: /* 20.480 MHz */ | ||
1071 | tmp = 1; | ||
1072 | break; | ||
1073 | case 28000000: /* 28.000 MHz */ | ||
1074 | tmp = 2; | ||
1075 | break; | ||
1076 | case 25000000: /* 25.000 MHz */ | ||
1077 | tmp = 3; | ||
1078 | break; | ||
1079 | default: | ||
1080 | err("invalid clock"); | ||
1081 | return -EINVAL; | ||
1082 | } | ||
1083 | |||
1084 | adc_cw = af913_div(state->config.clock, 1000000ul, 19); | ||
1085 | buf[0] = (adc_cw >> 0) & 0xff; | ||
1086 | buf[1] = (adc_cw >> 8) & 0xff; | ||
1087 | buf[2] = (adc_cw >> 16) & 0xff; | ||
1088 | |||
1089 | ret = af9013_wr_regs(state, 0xd180, buf, 3); | ||
1090 | if (ret) | ||
1091 | goto err; | ||
1092 | |||
1093 | ret = af9013_wr_reg_bits(state, 0x9bd2, 0, 4, tmp); | ||
1094 | if (ret) | ||
1095 | goto err; | ||
1096 | |||
1097 | /* set I2C master clock */ | ||
1098 | ret = af9013_wr_reg(state, 0xd416, 0x14); | ||
1099 | if (ret) | ||
1100 | goto err; | ||
1101 | |||
1102 | /* set 16 embx */ | ||
1103 | ret = af9013_wr_reg_bits(state, 0xd700, 1, 1, 1); | ||
1104 | if (ret) | ||
1105 | goto err; | ||
1106 | |||
1107 | /* set no trigger */ | ||
1108 | ret = af9013_wr_reg_bits(state, 0xd700, 2, 1, 0); | ||
1109 | if (ret) | ||
1110 | goto err; | ||
1111 | |||
1112 | /* set read-update bit for constellation */ | ||
1113 | ret = af9013_wr_reg_bits(state, 0xd371, 1, 1, 1); | ||
1114 | if (ret) | ||
1115 | goto err; | ||
1116 | |||
1117 | /* settings for mp2if */ | ||
1118 | if (state->config.ts_mode == AF9013_TS_USB) { | ||
1119 | /* AF9015 split PSB to 1.5k + 0.5k */ | ||
1120 | ret = af9013_wr_reg_bits(state, 0xd50b, 2, 1, 1); | ||
1121 | if (ret) | ||
1122 | goto err; | ||
1123 | } else { | ||
1124 | /* AF9013 change the output bit to data7 */ | ||
1125 | ret = af9013_wr_reg_bits(state, 0xd500, 3, 1, 1); | ||
1126 | if (ret) | ||
1127 | goto err; | ||
1128 | |||
1129 | /* AF9013 set mpeg to full speed */ | ||
1130 | ret = af9013_wr_reg_bits(state, 0xd502, 4, 1, 1); | ||
1131 | if (ret) | ||
1132 | goto err; | ||
1133 | } | ||
1134 | |||
1135 | ret = af9013_wr_reg_bits(state, 0xd520, 4, 1, 1); | ||
1136 | if (ret) | ||
1137 | goto err; | ||
1138 | |||
1139 | /* load OFSM settings */ | ||
1140 | dbg("%s: load ofsm settings", __func__); | ||
1141 | len = ARRAY_SIZE(ofsm_init); | ||
1142 | init = ofsm_init; | ||
1143 | for (i = 0; i < len; i++) { | ||
1144 | ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos, | ||
1145 | init[i].len, init[i].val); | ||
1146 | if (ret) | ||
1147 | goto err; | ||
1148 | } | ||
1149 | |||
1150 | /* load tuner specific settings */ | ||
1151 | dbg("%s: load tuner specific settings", __func__); | ||
1152 | switch (state->config.tuner) { | ||
1153 | case AF9013_TUNER_MXL5003D: | ||
1154 | len = ARRAY_SIZE(tuner_init_mxl5003d); | ||
1155 | init = tuner_init_mxl5003d; | ||
1156 | break; | ||
1157 | case AF9013_TUNER_MXL5005D: | ||
1158 | case AF9013_TUNER_MXL5005R: | ||
1159 | case AF9013_TUNER_MXL5007T: | ||
1160 | len = ARRAY_SIZE(tuner_init_mxl5005); | ||
1161 | init = tuner_init_mxl5005; | ||
1162 | break; | ||
1163 | case AF9013_TUNER_ENV77H11D5: | ||
1164 | len = ARRAY_SIZE(tuner_init_env77h11d5); | ||
1165 | init = tuner_init_env77h11d5; | ||
1166 | break; | ||
1167 | case AF9013_TUNER_MT2060: | ||
1168 | len = ARRAY_SIZE(tuner_init_mt2060); | ||
1169 | init = tuner_init_mt2060; | ||
1170 | break; | ||
1171 | case AF9013_TUNER_MC44S803: | ||
1172 | len = ARRAY_SIZE(tuner_init_mc44s803); | ||
1173 | init = tuner_init_mc44s803; | ||
1174 | break; | ||
1175 | case AF9013_TUNER_QT1010: | ||
1176 | case AF9013_TUNER_QT1010A: | ||
1177 | len = ARRAY_SIZE(tuner_init_qt1010); | ||
1178 | init = tuner_init_qt1010; | ||
1179 | break; | ||
1180 | case AF9013_TUNER_MT2060_2: | ||
1181 | len = ARRAY_SIZE(tuner_init_mt2060_2); | ||
1182 | init = tuner_init_mt2060_2; | ||
1183 | break; | ||
1184 | case AF9013_TUNER_TDA18271: | ||
1185 | case AF9013_TUNER_TDA18218: | ||
1186 | len = ARRAY_SIZE(tuner_init_tda18271); | ||
1187 | init = tuner_init_tda18271; | ||
1188 | break; | ||
1189 | case AF9013_TUNER_UNKNOWN: | ||
1190 | default: | ||
1191 | len = ARRAY_SIZE(tuner_init_unknown); | ||
1192 | init = tuner_init_unknown; | ||
1193 | break; | ||
1194 | } | ||
1195 | |||
1196 | for (i = 0; i < len; i++) { | ||
1197 | ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos, | ||
1198 | init[i].len, init[i].val); | ||
1199 | if (ret) | ||
1200 | goto err; | ||
1201 | } | ||
1202 | |||
1203 | /* TS mode */ | ||
1204 | ret = af9013_wr_reg_bits(state, 0xd500, 1, 2, state->config.ts_mode); | ||
1205 | if (ret) | ||
1206 | goto err; | ||
1207 | |||
1208 | /* enable lock led */ | ||
1209 | ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 1); | ||
1210 | if (ret) | ||
1211 | goto err; | ||
1212 | |||
1213 | /* check if we support signal strength */ | ||
1214 | if (!state->signal_strength_en) { | ||
1215 | ret = af9013_rd_reg_bits(state, 0x9bee, 0, 1, | ||
1216 | &state->signal_strength_en); | ||
1217 | if (ret) | ||
1218 | goto err; | ||
1219 | } | ||
1220 | |||
1221 | /* read values needed for signal strength calculation */ | ||
1222 | if (state->signal_strength_en && !state->rf_50) { | ||
1223 | ret = af9013_rd_reg(state, 0x9bbd, &state->rf_50); | ||
1224 | if (ret) | ||
1225 | goto err; | ||
1226 | |||
1227 | ret = af9013_rd_reg(state, 0x9bd0, &state->rf_80); | ||
1228 | if (ret) | ||
1229 | goto err; | ||
1230 | |||
1231 | ret = af9013_rd_reg(state, 0x9be2, &state->if_50); | ||
1232 | if (ret) | ||
1233 | goto err; | ||
1234 | |||
1235 | ret = af9013_rd_reg(state, 0x9be4, &state->if_80); | ||
1236 | if (ret) | ||
1237 | goto err; | ||
1238 | } | ||
1239 | |||
1240 | /* SNR */ | ||
1241 | ret = af9013_wr_reg(state, 0xd2e2, 1); | ||
1242 | if (ret) | ||
1243 | goto err; | ||
1244 | |||
1245 | /* BER / UCB */ | ||
1246 | buf[0] = (10000 >> 0) & 0xff; | ||
1247 | buf[1] = (10000 >> 8) & 0xff; | ||
1248 | ret = af9013_wr_regs(state, 0xd385, buf, 2); | ||
1249 | if (ret) | ||
1250 | goto err; | ||
1251 | |||
1252 | /* enable FEC monitor */ | ||
1253 | ret = af9013_wr_reg_bits(state, 0xd392, 1, 1, 1); | ||
1254 | if (ret) | ||
1255 | goto err; | ||
1256 | |||
1257 | state->first_tune = true; | ||
1258 | schedule_delayed_work(&state->statistics_work, msecs_to_jiffies(400)); | ||
1259 | |||
1260 | return ret; | ||
1261 | err: | ||
1262 | dbg("%s: failed=%d", __func__, ret); | ||
1263 | return ret; | ||
1264 | } | ||
1265 | |||
1266 | static int af9013_sleep(struct dvb_frontend *fe) | ||
1267 | { | ||
1268 | struct af9013_state *state = fe->demodulator_priv; | ||
1269 | int ret; | ||
1270 | |||
1271 | dbg("%s", __func__); | ||
1272 | |||
1273 | /* stop statistics polling */ | ||
1274 | cancel_delayed_work_sync(&state->statistics_work); | ||
1275 | |||
1276 | /* disable lock led */ | ||
1277 | ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 0); | ||
1278 | if (ret) | ||
1279 | goto err; | ||
1280 | |||
1281 | /* power off */ | ||
1282 | ret = af9013_power_ctrl(state, 0); | ||
1283 | if (ret) | ||
1284 | goto err; | ||
1285 | |||
1286 | return ret; | ||
1287 | err: | ||
1288 | dbg("%s: failed=%d", __func__, ret); | ||
1289 | return ret; | ||
1290 | } | ||
1291 | |||
1292 | static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) | ||
1293 | { | ||
1294 | int ret; | ||
1295 | struct af9013_state *state = fe->demodulator_priv; | ||
1296 | |||
1297 | dbg("%s: enable=%d", __func__, enable); | ||
1298 | |||
1299 | /* gate already open or close */ | ||
1300 | if (state->i2c_gate_state == enable) | ||
1301 | return 0; | ||
1302 | |||
1303 | if (state->config.ts_mode == AF9013_TS_USB) | ||
1304 | ret = af9013_wr_reg_bits(state, 0xd417, 3, 1, enable); | ||
1305 | else | ||
1306 | ret = af9013_wr_reg_bits(state, 0xd607, 2, 1, enable); | ||
1307 | if (ret) | ||
1308 | goto err; | ||
1309 | |||
1310 | state->i2c_gate_state = enable; | ||
1311 | |||
1312 | return ret; | ||
1313 | err: | ||
1314 | dbg("%s: failed=%d", __func__, ret); | ||
1315 | return ret; | ||
1316 | } | ||
1317 | |||
1318 | static void af9013_release(struct dvb_frontend *fe) | ||
1319 | { | ||
1320 | struct af9013_state *state = fe->demodulator_priv; | ||
1321 | kfree(state); | ||
1322 | } | ||
1323 | |||
1324 | static struct dvb_frontend_ops af9013_ops; | ||
1325 | |||
1326 | static int af9013_download_firmware(struct af9013_state *state) | ||
1327 | { | ||
1328 | int i, len, remaining, ret; | ||
1329 | const struct firmware *fw; | ||
1330 | u16 checksum = 0; | ||
1331 | u8 val; | ||
1332 | u8 fw_params[4]; | ||
1333 | u8 *fw_file = AF9013_DEFAULT_FIRMWARE; | ||
1334 | |||
1335 | msleep(100); | ||
1336 | /* check whether firmware is already running */ | ||
1337 | ret = af9013_rd_reg(state, 0x98be, &val); | ||
1338 | if (ret) | ||
1339 | goto err; | ||
1340 | else | ||
1341 | dbg("%s: firmware status=%02x", __func__, val); | ||
1342 | |||
1343 | if (val == 0x0c) /* fw is running, no need for download */ | ||
1344 | goto exit; | ||
1345 | |||
1346 | info("found a '%s' in cold state, will try to load a firmware", | ||
1347 | af9013_ops.info.name); | ||
1348 | |||
1349 | /* request the firmware, this will block and timeout */ | ||
1350 | ret = request_firmware(&fw, fw_file, state->i2c->dev.parent); | ||
1351 | if (ret) { | ||
1352 | err("did not find the firmware file. (%s) " | ||
1353 | "Please see linux/Documentation/dvb/ for more details" \ | ||
1354 | " on firmware-problems. (%d)", | ||
1355 | fw_file, ret); | ||
1356 | goto err; | ||
1357 | } | ||
1358 | |||
1359 | info("downloading firmware from file '%s'", fw_file); | ||
1360 | |||
1361 | /* calc checksum */ | ||
1362 | for (i = 0; i < fw->size; i++) | ||
1363 | checksum += fw->data[i]; | ||
1364 | |||
1365 | fw_params[0] = checksum >> 8; | ||
1366 | fw_params[1] = checksum & 0xff; | ||
1367 | fw_params[2] = fw->size >> 8; | ||
1368 | fw_params[3] = fw->size & 0xff; | ||
1369 | |||
1370 | /* write fw checksum & size */ | ||
1371 | ret = af9013_write_ofsm_regs(state, 0x50fc, | ||
1372 | fw_params, sizeof(fw_params)); | ||
1373 | if (ret) | ||
1374 | goto err_release; | ||
1375 | |||
1376 | #define FW_ADDR 0x5100 /* firmware start address */ | ||
1377 | #define LEN_MAX 16 /* max packet size */ | ||
1378 | for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) { | ||
1379 | len = remaining; | ||
1380 | if (len > LEN_MAX) | ||
1381 | len = LEN_MAX; | ||
1382 | |||
1383 | ret = af9013_write_ofsm_regs(state, | ||
1384 | FW_ADDR + fw->size - remaining, | ||
1385 | (u8 *) &fw->data[fw->size - remaining], len); | ||
1386 | if (ret) { | ||
1387 | err("firmware download failed:%d", ret); | ||
1388 | goto err_release; | ||
1389 | } | ||
1390 | } | ||
1391 | |||
1392 | /* request boot firmware */ | ||
1393 | ret = af9013_wr_reg(state, 0xe205, 1); | ||
1394 | if (ret) | ||
1395 | goto err_release; | ||
1396 | |||
1397 | for (i = 0; i < 15; i++) { | ||
1398 | msleep(100); | ||
1399 | |||
1400 | /* check firmware status */ | ||
1401 | ret = af9013_rd_reg(state, 0x98be, &val); | ||
1402 | if (ret) | ||
1403 | goto err_release; | ||
1404 | |||
1405 | dbg("%s: firmware status=%02x", __func__, val); | ||
1406 | |||
1407 | if (val == 0x0c || val == 0x04) /* success or fail */ | ||
1408 | break; | ||
1409 | } | ||
1410 | |||
1411 | if (val == 0x04) { | ||
1412 | err("firmware did not run"); | ||
1413 | ret = -ENODEV; | ||
1414 | } else if (val != 0x0c) { | ||
1415 | err("firmware boot timeout"); | ||
1416 | ret = -ENODEV; | ||
1417 | } | ||
1418 | |||
1419 | err_release: | ||
1420 | release_firmware(fw); | ||
1421 | err: | ||
1422 | exit: | ||
1423 | if (!ret) | ||
1424 | info("found a '%s' in warm state.", af9013_ops.info.name); | ||
1425 | return ret; | ||
1426 | } | ||
1427 | |||
1428 | struct dvb_frontend *af9013_attach(const struct af9013_config *config, | ||
1429 | struct i2c_adapter *i2c) | ||
1430 | { | ||
1431 | int ret; | ||
1432 | struct af9013_state *state = NULL; | ||
1433 | u8 buf[4], i; | ||
1434 | |||
1435 | /* allocate memory for the internal state */ | ||
1436 | state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL); | ||
1437 | if (state == NULL) | ||
1438 | goto err; | ||
1439 | |||
1440 | /* setup the state */ | ||
1441 | state->i2c = i2c; | ||
1442 | memcpy(&state->config, config, sizeof(struct af9013_config)); | ||
1443 | |||
1444 | /* download firmware */ | ||
1445 | if (state->config.ts_mode != AF9013_TS_USB) { | ||
1446 | ret = af9013_download_firmware(state); | ||
1447 | if (ret) | ||
1448 | goto err; | ||
1449 | } | ||
1450 | |||
1451 | /* firmware version */ | ||
1452 | ret = af9013_rd_regs(state, 0x5103, buf, 4); | ||
1453 | if (ret) | ||
1454 | goto err; | ||
1455 | |||
1456 | info("firmware version %d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]); | ||
1457 | |||
1458 | /* set GPIOs */ | ||
1459 | for (i = 0; i < sizeof(state->config.gpio); i++) { | ||
1460 | ret = af9013_set_gpio(state, i, state->config.gpio[i]); | ||
1461 | if (ret) | ||
1462 | goto err; | ||
1463 | } | ||
1464 | |||
1465 | /* create dvb_frontend */ | ||
1466 | memcpy(&state->fe.ops, &af9013_ops, | ||
1467 | sizeof(struct dvb_frontend_ops)); | ||
1468 | state->fe.demodulator_priv = state; | ||
1469 | |||
1470 | INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work); | ||
1471 | |||
1472 | return &state->fe; | ||
1473 | err: | ||
1474 | kfree(state); | ||
1475 | return NULL; | ||
1476 | } | ||
1477 | EXPORT_SYMBOL(af9013_attach); | ||
1478 | |||
1479 | static struct dvb_frontend_ops af9013_ops = { | ||
1480 | .delsys = { SYS_DVBT }, | ||
1481 | .info = { | ||
1482 | .name = "Afatech AF9013", | ||
1483 | .frequency_min = 174000000, | ||
1484 | .frequency_max = 862000000, | ||
1485 | .frequency_stepsize = 250000, | ||
1486 | .frequency_tolerance = 0, | ||
1487 | .caps = FE_CAN_FEC_1_2 | | ||
1488 | FE_CAN_FEC_2_3 | | ||
1489 | FE_CAN_FEC_3_4 | | ||
1490 | FE_CAN_FEC_5_6 | | ||
1491 | FE_CAN_FEC_7_8 | | ||
1492 | FE_CAN_FEC_AUTO | | ||
1493 | FE_CAN_QPSK | | ||
1494 | FE_CAN_QAM_16 | | ||
1495 | FE_CAN_QAM_64 | | ||
1496 | FE_CAN_QAM_AUTO | | ||
1497 | FE_CAN_TRANSMISSION_MODE_AUTO | | ||
1498 | FE_CAN_GUARD_INTERVAL_AUTO | | ||
1499 | FE_CAN_HIERARCHY_AUTO | | ||
1500 | FE_CAN_RECOVER | | ||
1501 | FE_CAN_MUTE_TS | ||
1502 | }, | ||
1503 | |||
1504 | .release = af9013_release, | ||
1505 | |||
1506 | .init = af9013_init, | ||
1507 | .sleep = af9013_sleep, | ||
1508 | |||
1509 | .get_tune_settings = af9013_get_tune_settings, | ||
1510 | .set_frontend = af9013_set_frontend, | ||
1511 | .get_frontend = af9013_get_frontend, | ||
1512 | |||
1513 | .read_status = af9013_read_status, | ||
1514 | .read_snr = af9013_read_snr, | ||
1515 | .read_signal_strength = af9013_read_signal_strength, | ||
1516 | .read_ber = af9013_read_ber, | ||
1517 | .read_ucblocks = af9013_read_ucblocks, | ||
1518 | |||
1519 | .i2c_gate_ctrl = af9013_i2c_gate_ctrl, | ||
1520 | }; | ||
1521 | |||
1522 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); | ||
1523 | MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver"); | ||
1524 | MODULE_LICENSE("GPL"); | ||