blob: 56e9611c0abb33d8d4fd5962ee1be0faaf2d2cb0 [file] [log] [blame]
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001/*
2 * Afatech AF9033 demodulator driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "af9033_priv.h"
23
24struct af9033_state {
25 struct i2c_adapter *i2c;
26 struct dvb_frontend fe;
27 struct af9033_config cfg;
28
29 u32 bandwidth_hz;
30 bool ts_mode_parallel;
31 bool ts_mode_serial;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -030032
33 u32 ber;
34 u32 ucb;
35 unsigned long last_stat_check;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030036};
37
38/* write multiple registers */
39static int af9033_wr_regs(struct af9033_state *state, u32 reg, const u8 *val,
40 int len)
41{
42 int ret;
43 u8 buf[3 + len];
44 struct i2c_msg msg[1] = {
45 {
46 .addr = state->cfg.i2c_addr,
47 .flags = 0,
48 .len = sizeof(buf),
49 .buf = buf,
50 }
51 };
52
53 buf[0] = (reg >> 16) & 0xff;
54 buf[1] = (reg >> 8) & 0xff;
55 buf[2] = (reg >> 0) & 0xff;
56 memcpy(&buf[3], val, len);
57
58 ret = i2c_transfer(state->i2c, msg, 1);
59 if (ret == 1) {
60 ret = 0;
61 } else {
Antti Palosaari0a73f2d2012-09-12 20:23:42 -030062 dev_warn(&state->i2c->dev, "%s: i2c wr failed=%d reg=%06x " \
63 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
Antti Palosaari4b64bb22012-03-30 08:21:25 -030064 ret = -EREMOTEIO;
65 }
66
67 return ret;
68}
69
70/* read multiple registers */
71static int af9033_rd_regs(struct af9033_state *state, u32 reg, u8 *val, int len)
72{
73 int ret;
74 u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
75 (reg >> 0) & 0xff };
76 struct i2c_msg msg[2] = {
77 {
78 .addr = state->cfg.i2c_addr,
79 .flags = 0,
80 .len = sizeof(buf),
81 .buf = buf
82 }, {
83 .addr = state->cfg.i2c_addr,
84 .flags = I2C_M_RD,
85 .len = len,
86 .buf = val
87 }
88 };
89
90 ret = i2c_transfer(state->i2c, msg, 2);
91 if (ret == 2) {
92 ret = 0;
93 } else {
Antti Palosaari0a73f2d2012-09-12 20:23:42 -030094 dev_warn(&state->i2c->dev, "%s: i2c rd failed=%d reg=%06x " \
95 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
Antti Palosaari4b64bb22012-03-30 08:21:25 -030096 ret = -EREMOTEIO;
97 }
98
99 return ret;
100}
101
102
103/* write single register */
104static int af9033_wr_reg(struct af9033_state *state, u32 reg, u8 val)
105{
106 return af9033_wr_regs(state, reg, &val, 1);
107}
108
109/* read single register */
110static int af9033_rd_reg(struct af9033_state *state, u32 reg, u8 *val)
111{
112 return af9033_rd_regs(state, reg, val, 1);
113}
114
115/* write single register with mask */
116static int af9033_wr_reg_mask(struct af9033_state *state, u32 reg, u8 val,
117 u8 mask)
118{
119 int ret;
120 u8 tmp;
121
122 /* no need for read if whole reg is written */
123 if (mask != 0xff) {
124 ret = af9033_rd_regs(state, reg, &tmp, 1);
125 if (ret)
126 return ret;
127
128 val &= mask;
129 tmp &= ~mask;
130 val |= tmp;
131 }
132
133 return af9033_wr_regs(state, reg, &val, 1);
134}
135
136/* read single register with mask */
137static int af9033_rd_reg_mask(struct af9033_state *state, u32 reg, u8 *val,
138 u8 mask)
139{
140 int ret, i;
141 u8 tmp;
142
143 ret = af9033_rd_regs(state, reg, &tmp, 1);
144 if (ret)
145 return ret;
146
147 tmp &= mask;
148
149 /* find position of the first bit */
150 for (i = 0; i < 8; i++) {
151 if ((mask >> i) & 0x01)
152 break;
153 }
154 *val = tmp >> i;
155
156 return 0;
157}
158
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300159static u32 af9033_div(struct af9033_state *state, u32 a, u32 b, u32 x)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300160{
161 u32 r = 0, c = 0, i;
162
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300163 dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300164
165 if (a > b) {
166 c = a / b;
167 a = a - c * b;
168 }
169
170 for (i = 0; i < x; i++) {
171 if (a >= b) {
172 r += 1;
173 a -= b;
174 }
175 a <<= 1;
176 r <<= 1;
177 }
178 r = (c << (u32)x) + r;
179
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300180 dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n",
181 __func__, a, b, x, r, r);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300182
183 return r;
184}
185
186static void af9033_release(struct dvb_frontend *fe)
187{
188 struct af9033_state *state = fe->demodulator_priv;
189
190 kfree(state);
191}
192
193static int af9033_init(struct dvb_frontend *fe)
194{
195 struct af9033_state *state = fe->demodulator_priv;
196 int ret, i, len;
197 const struct reg_val *init;
198 u8 buf[4];
199 u32 adc_cw, clock_cw;
200 struct reg_val_mask tab[] = {
201 { 0x80fb24, 0x00, 0x08 },
202 { 0x80004c, 0x00, 0xff },
203 { 0x00f641, state->cfg.tuner, 0xff },
204 { 0x80f5ca, 0x01, 0x01 },
205 { 0x80f715, 0x01, 0x01 },
206 { 0x00f41f, 0x04, 0x04 },
207 { 0x00f41a, 0x01, 0x01 },
208 { 0x80f731, 0x00, 0x01 },
209 { 0x00d91e, 0x00, 0x01 },
210 { 0x00d919, 0x00, 0x01 },
211 { 0x80f732, 0x00, 0x01 },
212 { 0x00d91f, 0x00, 0x01 },
213 { 0x00d91a, 0x00, 0x01 },
214 { 0x80f730, 0x00, 0x01 },
215 { 0x80f778, 0x00, 0xff },
216 { 0x80f73c, 0x01, 0x01 },
217 { 0x80f776, 0x00, 0x01 },
218 { 0x00d8fd, 0x01, 0xff },
219 { 0x00d830, 0x01, 0xff },
220 { 0x00d831, 0x00, 0xff },
221 { 0x00d832, 0x00, 0xff },
222 { 0x80f985, state->ts_mode_serial, 0x01 },
223 { 0x80f986, state->ts_mode_parallel, 0x01 },
224 { 0x00d827, 0x00, 0xff },
225 { 0x00d829, 0x00, 0xff },
226 };
227
228 /* program clock control */
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300229 clock_cw = af9033_div(state, state->cfg.clock, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300230 buf[0] = (clock_cw >> 0) & 0xff;
231 buf[1] = (clock_cw >> 8) & 0xff;
232 buf[2] = (clock_cw >> 16) & 0xff;
233 buf[3] = (clock_cw >> 24) & 0xff;
234
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300235 dev_dbg(&state->i2c->dev, "%s: clock=%d clock_cw=%08x\n",
236 __func__, state->cfg.clock, clock_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300237
238 ret = af9033_wr_regs(state, 0x800025, buf, 4);
239 if (ret < 0)
240 goto err;
241
242 /* program ADC control */
243 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
244 if (clock_adc_lut[i].clock == state->cfg.clock)
245 break;
246 }
247
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300248 adc_cw = af9033_div(state, clock_adc_lut[i].adc, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300249 buf[0] = (adc_cw >> 0) & 0xff;
250 buf[1] = (adc_cw >> 8) & 0xff;
251 buf[2] = (adc_cw >> 16) & 0xff;
252
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300253 dev_dbg(&state->i2c->dev, "%s: adc=%d adc_cw=%06x\n",
254 __func__, clock_adc_lut[i].adc, adc_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300255
256 ret = af9033_wr_regs(state, 0x80f1cd, buf, 3);
257 if (ret < 0)
258 goto err;
259
260 /* program register table */
261 for (i = 0; i < ARRAY_SIZE(tab); i++) {
262 ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val,
263 tab[i].mask);
264 if (ret < 0)
265 goto err;
266 }
267
268 /* settings for TS interface */
269 if (state->cfg.ts_mode == AF9033_TS_MODE_USB) {
270 ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01);
271 if (ret < 0)
272 goto err;
273
274 ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01);
275 if (ret < 0)
276 goto err;
277 } else {
278 ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01);
279 if (ret < 0)
280 goto err;
281
282 ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01);
283 if (ret < 0)
284 goto err;
285 }
286
287 /* load OFSM settings */
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300288 dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300289 len = ARRAY_SIZE(ofsm_init);
290 init = ofsm_init;
291 for (i = 0; i < len; i++) {
292 ret = af9033_wr_reg(state, init[i].reg, init[i].val);
293 if (ret < 0)
294 goto err;
295 }
296
297 /* load tuner specific settings */
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300298 dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n",
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300299 __func__);
300 switch (state->cfg.tuner) {
301 case AF9033_TUNER_TUA9001:
302 len = ARRAY_SIZE(tuner_init_tua9001);
303 init = tuner_init_tua9001;
304 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300305 case AF9033_TUNER_FC0011:
306 len = ARRAY_SIZE(tuner_init_fc0011);
307 init = tuner_init_fc0011;
308 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300309 case AF9033_TUNER_MXL5007T:
310 len = ARRAY_SIZE(tuner_init_mxl5007t);
311 init = tuner_init_mxl5007t;
312 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300313 case AF9033_TUNER_TDA18218:
314 len = ARRAY_SIZE(tuner_init_tda18218);
315 init = tuner_init_tda18218;
316 break;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300317 default:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300318 dev_dbg(&state->i2c->dev, "%s: unsupported tuner ID=%d\n",
319 __func__, state->cfg.tuner);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300320 ret = -ENODEV;
321 goto err;
322 }
323
324 for (i = 0; i < len; i++) {
325 ret = af9033_wr_reg(state, init[i].reg, init[i].val);
326 if (ret < 0)
327 goto err;
328 }
329
330 state->bandwidth_hz = 0; /* force to program all parameters */
331
332 return 0;
333
334err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300335 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300336
337 return ret;
338}
339
340static int af9033_sleep(struct dvb_frontend *fe)
341{
342 struct af9033_state *state = fe->demodulator_priv;
343 int ret, i;
344 u8 tmp;
345
346 ret = af9033_wr_reg(state, 0x80004c, 1);
347 if (ret < 0)
348 goto err;
349
350 ret = af9033_wr_reg(state, 0x800000, 0);
351 if (ret < 0)
352 goto err;
353
354 for (i = 100, tmp = 1; i && tmp; i--) {
355 ret = af9033_rd_reg(state, 0x80004c, &tmp);
356 if (ret < 0)
357 goto err;
358
359 usleep_range(200, 10000);
360 }
361
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300362 dev_dbg(&state->i2c->dev, "%s: loop=%d\n", __func__, i);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300363
364 if (i == 0) {
365 ret = -ETIMEDOUT;
366 goto err;
367 }
368
369 ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08);
370 if (ret < 0)
371 goto err;
372
373 /* prevent current leak (?) */
374 if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
375 /* enable parallel TS */
376 ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
377 if (ret < 0)
378 goto err;
379
380 ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01);
381 if (ret < 0)
382 goto err;
383 }
384
385 return 0;
386
387err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300388 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300389
390 return ret;
391}
392
393static int af9033_get_tune_settings(struct dvb_frontend *fe,
394 struct dvb_frontend_tune_settings *fesettings)
395{
396 fesettings->min_delay_ms = 800;
397 fesettings->step_size = 0;
398 fesettings->max_drift = 0;
399
400 return 0;
401}
402
403static int af9033_set_frontend(struct dvb_frontend *fe)
404{
405 struct af9033_state *state = fe->demodulator_priv;
406 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300407 int ret, i, spec_inv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300408 u8 tmp, buf[3], bandwidth_reg_val;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300409 u32 if_frequency, freq_cw, adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300410
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300411 dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
412 __func__, c->frequency, c->bandwidth_hz);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300413
414 /* check bandwidth */
415 switch (c->bandwidth_hz) {
416 case 6000000:
417 bandwidth_reg_val = 0x00;
418 break;
419 case 7000000:
420 bandwidth_reg_val = 0x01;
421 break;
422 case 8000000:
423 bandwidth_reg_val = 0x02;
424 break;
425 default:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300426 dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n",
427 __func__);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300428 ret = -EINVAL;
429 goto err;
430 }
431
432 /* program tuner */
433 if (fe->ops.tuner_ops.set_params)
434 fe->ops.tuner_ops.set_params(fe);
435
436 /* program CFOE coefficients */
437 if (c->bandwidth_hz != state->bandwidth_hz) {
438 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
439 if (coeff_lut[i].clock == state->cfg.clock &&
440 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
441 break;
442 }
443 }
444 ret = af9033_wr_regs(state, 0x800001,
445 coeff_lut[i].val, sizeof(coeff_lut[i].val));
446 }
447
448 /* program frequency control */
449 if (c->bandwidth_hz != state->bandwidth_hz) {
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300450 spec_inv = state->cfg.spec_inv ? -1 : 1;
451
452 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
453 if (clock_adc_lut[i].clock == state->cfg.clock)
454 break;
455 }
456 adc_freq = clock_adc_lut[i].adc;
457
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300458 /* get used IF frequency */
459 if (fe->ops.tuner_ops.get_if_frequency)
460 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
461 else
462 if_frequency = 0;
463
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300464 while (if_frequency > (adc_freq / 2))
465 if_frequency -= adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300466
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300467 if (if_frequency >= 0)
468 spec_inv *= -1;
469 else
470 if_frequency *= -1;
471
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300472 freq_cw = af9033_div(state, if_frequency, adc_freq, 23ul);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300473
474 if (spec_inv == -1)
475 freq_cw *= -1;
476
477 /* get adc multiplies */
478 ret = af9033_rd_reg(state, 0x800045, &tmp);
479 if (ret < 0)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300480 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300481
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300482 if (tmp == 1)
483 freq_cw /= 2;
484
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300485 buf[0] = (freq_cw >> 0) & 0xff;
486 buf[1] = (freq_cw >> 8) & 0xff;
487 buf[2] = (freq_cw >> 16) & 0x7f;
488 ret = af9033_wr_regs(state, 0x800029, buf, 3);
489 if (ret < 0)
490 goto err;
491
492 state->bandwidth_hz = c->bandwidth_hz;
493 }
494
495 ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03);
496 if (ret < 0)
497 goto err;
498
499 ret = af9033_wr_reg(state, 0x800040, 0x00);
500 if (ret < 0)
501 goto err;
502
503 ret = af9033_wr_reg(state, 0x800047, 0x00);
504 if (ret < 0)
505 goto err;
506
507 ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01);
508 if (ret < 0)
509 goto err;
510
511 if (c->frequency <= 230000000)
512 tmp = 0x00; /* VHF */
513 else
514 tmp = 0x01; /* UHF */
515
516 ret = af9033_wr_reg(state, 0x80004b, tmp);
517 if (ret < 0)
518 goto err;
519
520 ret = af9033_wr_reg(state, 0x800000, 0x00);
521 if (ret < 0)
522 goto err;
523
524 return 0;
525
526err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300527 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300528
529 return ret;
530}
531
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300532static int af9033_get_frontend(struct dvb_frontend *fe)
533{
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300534 struct af9033_state *state = fe->demodulator_priv;
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300535 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300536 int ret;
537 u8 buf[8];
538
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300539 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300540
541 /* read all needed registers */
542 ret = af9033_rd_regs(state, 0x80f900, buf, sizeof(buf));
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300543 if (ret < 0)
544 goto err;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300545
546 switch ((buf[0] >> 0) & 3) {
547 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300548 c->transmission_mode = TRANSMISSION_MODE_2K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300549 break;
550 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300551 c->transmission_mode = TRANSMISSION_MODE_8K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300552 break;
553 }
554
555 switch ((buf[1] >> 0) & 3) {
556 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300557 c->guard_interval = GUARD_INTERVAL_1_32;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300558 break;
559 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300560 c->guard_interval = GUARD_INTERVAL_1_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300561 break;
562 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300563 c->guard_interval = GUARD_INTERVAL_1_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300564 break;
565 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300566 c->guard_interval = GUARD_INTERVAL_1_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300567 break;
568 }
569
570 switch ((buf[2] >> 0) & 7) {
571 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300572 c->hierarchy = HIERARCHY_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300573 break;
574 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300575 c->hierarchy = HIERARCHY_1;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300576 break;
577 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300578 c->hierarchy = HIERARCHY_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300579 break;
580 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300581 c->hierarchy = HIERARCHY_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300582 break;
583 }
584
585 switch ((buf[3] >> 0) & 3) {
586 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300587 c->modulation = QPSK;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300588 break;
589 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300590 c->modulation = QAM_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300591 break;
592 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300593 c->modulation = QAM_64;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300594 break;
595 }
596
597 switch ((buf[4] >> 0) & 3) {
598 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300599 c->bandwidth_hz = 6000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300600 break;
601 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300602 c->bandwidth_hz = 7000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300603 break;
604 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300605 c->bandwidth_hz = 8000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300606 break;
607 }
608
609 switch ((buf[6] >> 0) & 7) {
610 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300611 c->code_rate_HP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300612 break;
613 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300614 c->code_rate_HP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300615 break;
616 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300617 c->code_rate_HP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300618 break;
619 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300620 c->code_rate_HP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300621 break;
622 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300623 c->code_rate_HP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300624 break;
625 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300626 c->code_rate_HP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300627 break;
628 }
629
630 switch ((buf[7] >> 0) & 7) {
631 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300632 c->code_rate_LP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300633 break;
634 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300635 c->code_rate_LP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300636 break;
637 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300638 c->code_rate_LP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300639 break;
640 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300641 c->code_rate_LP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300642 break;
643 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300644 c->code_rate_LP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300645 break;
646 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300647 c->code_rate_LP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300648 break;
649 }
650
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300651 return 0;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300652
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300653err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300654 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300655
656 return ret;
657}
658
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300659static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
660{
661 struct af9033_state *state = fe->demodulator_priv;
662 int ret;
663 u8 tmp;
664
665 *status = 0;
666
667 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
668 ret = af9033_rd_reg(state, 0x800047, &tmp);
669 if (ret < 0)
670 goto err;
671
672 /* has signal */
673 if (tmp == 0x01)
674 *status |= FE_HAS_SIGNAL;
675
676 if (tmp != 0x02) {
677 /* TPS lock */
678 ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01);
679 if (ret < 0)
680 goto err;
681
682 if (tmp)
683 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
684 FE_HAS_VITERBI;
685
686 /* full lock */
687 ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01);
688 if (ret < 0)
689 goto err;
690
691 if (tmp)
692 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
693 FE_HAS_VITERBI | FE_HAS_SYNC |
694 FE_HAS_LOCK;
695 }
696
697 return 0;
698
699err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300700 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300701
702 return ret;
703}
704
705static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
706{
Antti Palosaarie898ef62012-04-01 12:50:02 -0300707 struct af9033_state *state = fe->demodulator_priv;
708 int ret, i, len;
709 u8 buf[3], tmp;
710 u32 snr_val;
711 const struct val_snr *uninitialized_var(snr_lut);
712
713 /* read value */
714 ret = af9033_rd_regs(state, 0x80002c, buf, 3);
715 if (ret < 0)
716 goto err;
717
718 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
719
720 /* read current modulation */
721 ret = af9033_rd_reg(state, 0x80f903, &tmp);
722 if (ret < 0)
723 goto err;
724
725 switch ((tmp >> 0) & 3) {
726 case 0:
727 len = ARRAY_SIZE(qpsk_snr_lut);
728 snr_lut = qpsk_snr_lut;
729 break;
730 case 1:
731 len = ARRAY_SIZE(qam16_snr_lut);
732 snr_lut = qam16_snr_lut;
733 break;
734 case 2:
735 len = ARRAY_SIZE(qam64_snr_lut);
736 snr_lut = qam64_snr_lut;
737 break;
738 default:
739 goto err;
740 }
741
742 for (i = 0; i < len; i++) {
743 tmp = snr_lut[i].snr;
744
745 if (snr_val < snr_lut[i].val)
746 break;
747 }
748
749 *snr = tmp * 10; /* dB/10 */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300750
751 return 0;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300752
753err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300754 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarie898ef62012-04-01 12:50:02 -0300755
756 return ret;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300757}
758
759static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
760{
761 struct af9033_state *state = fe->demodulator_priv;
762 int ret;
763 u8 strength2;
764
765 /* read signal strength of 0-100 scale */
766 ret = af9033_rd_reg(state, 0x800048, &strength2);
767 if (ret < 0)
768 goto err;
769
770 /* scale value to 0x0000-0xffff */
771 *strength = strength2 * 0xffff / 100;
772
773 return 0;
774
775err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300776 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300777
778 return ret;
779}
780
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300781static int af9033_update_ch_stat(struct af9033_state *state)
782{
783 int ret = 0;
784 u32 err_cnt, bit_cnt;
785 u16 abort_cnt;
786 u8 buf[7];
787
788 /* only update data every half second */
789 if (time_after(jiffies, state->last_stat_check + msecs_to_jiffies(500))) {
790 ret = af9033_rd_regs(state, 0x800032, buf, sizeof(buf));
791 if (ret < 0)
792 goto err;
793 /* in 8 byte packets? */
794 abort_cnt = (buf[1] << 8) + buf[0];
795 /* in bits */
796 err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
797 /* in 8 byte packets? always(?) 0x2710 = 10000 */
798 bit_cnt = (buf[6] << 8) + buf[5];
799
800 if (bit_cnt < abort_cnt) {
801 abort_cnt = 1000;
802 state->ber = 0xffffffff;
803 } else {
804 /* 8 byte packets, that have not been rejected already */
805 bit_cnt -= (u32)abort_cnt;
806 if (bit_cnt == 0) {
807 state->ber = 0xffffffff;
808 } else {
809 err_cnt -= (u32)abort_cnt * 8 * 8;
810 bit_cnt *= 8 * 8;
811 state->ber = err_cnt * (0xffffffff / bit_cnt);
812 }
813 }
814 state->ucb += abort_cnt;
815 state->last_stat_check = jiffies;
816 }
817
818 return 0;
819err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300820 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
821
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300822 return ret;
823}
824
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300825static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
826{
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300827 struct af9033_state *state = fe->demodulator_priv;
828 int ret;
829
830 ret = af9033_update_ch_stat(state);
831 if (ret < 0)
832 return ret;
833
834 *ber = state->ber;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300835
836 return 0;
837}
838
839static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
840{
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300841 struct af9033_state *state = fe->demodulator_priv;
842 int ret;
843
844 ret = af9033_update_ch_stat(state);
845 if (ret < 0)
846 return ret;
847
848 *ucblocks = state->ucb;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300849
850 return 0;
851}
852
853static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
854{
855 struct af9033_state *state = fe->demodulator_priv;
856 int ret;
857
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300858 dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300859
860 ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01);
861 if (ret < 0)
862 goto err;
863
864 return 0;
865
866err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300867 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300868
869 return ret;
870}
871
872static struct dvb_frontend_ops af9033_ops;
873
874struct dvb_frontend *af9033_attach(const struct af9033_config *config,
875 struct i2c_adapter *i2c)
876{
877 int ret;
878 struct af9033_state *state;
879 u8 buf[8];
880
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300881 dev_dbg(&i2c->dev, "%s:\n", __func__);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300882
883 /* allocate memory for the internal state */
884 state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL);
885 if (state == NULL)
886 goto err;
887
888 /* setup the state */
889 state->i2c = i2c;
890 memcpy(&state->cfg, config, sizeof(struct af9033_config));
891
Antti Palosaari8e8a5ac2012-04-01 14:13:36 -0300892 if (state->cfg.clock != 12000000) {
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300893 dev_err(&state->i2c->dev, "%s: af9033: unsupported clock=%d, " \
894 "only 12000000 Hz is supported currently\n",
895 KBUILD_MODNAME, state->cfg.clock);
Antti Palosaari8e8a5ac2012-04-01 14:13:36 -0300896 goto err;
897 }
898
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300899 /* firmware version */
900 ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4);
901 if (ret < 0)
902 goto err;
903
904 ret = af9033_rd_regs(state, 0x804191, &buf[4], 4);
905 if (ret < 0)
906 goto err;
907
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300908 dev_info(&state->i2c->dev, "%s: firmware version: LINK=%d.%d.%d.%d " \
909 "OFDM=%d.%d.%d.%d\n", KBUILD_MODNAME, buf[0], buf[1],
910 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300911
Antti Palosaari12897dc2012-09-16 22:26:57 -0300912 /* sleep */
913 ret = af9033_wr_reg(state, 0x80004c, 1);
914 if (ret < 0)
915 goto err;
916
917 ret = af9033_wr_reg(state, 0x800000, 0);
918 if (ret < 0)
919 goto err;
920
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300921 /* configure internal TS mode */
922 switch (state->cfg.ts_mode) {
923 case AF9033_TS_MODE_PARALLEL:
924 state->ts_mode_parallel = true;
925 break;
926 case AF9033_TS_MODE_SERIAL:
927 state->ts_mode_serial = true;
928 break;
929 case AF9033_TS_MODE_USB:
930 /* usb mode for AF9035 */
931 default:
932 break;
933 }
934
935 /* create dvb_frontend */
936 memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
937 state->fe.demodulator_priv = state;
938
939 return &state->fe;
940
941err:
942 kfree(state);
943 return NULL;
944}
945EXPORT_SYMBOL(af9033_attach);
946
947static struct dvb_frontend_ops af9033_ops = {
948 .delsys = { SYS_DVBT },
949 .info = {
950 .name = "Afatech AF9033 (DVB-T)",
951 .frequency_min = 174000000,
952 .frequency_max = 862000000,
953 .frequency_stepsize = 250000,
954 .frequency_tolerance = 0,
955 .caps = FE_CAN_FEC_1_2 |
956 FE_CAN_FEC_2_3 |
957 FE_CAN_FEC_3_4 |
958 FE_CAN_FEC_5_6 |
959 FE_CAN_FEC_7_8 |
960 FE_CAN_FEC_AUTO |
961 FE_CAN_QPSK |
962 FE_CAN_QAM_16 |
963 FE_CAN_QAM_64 |
964 FE_CAN_QAM_AUTO |
965 FE_CAN_TRANSMISSION_MODE_AUTO |
966 FE_CAN_GUARD_INTERVAL_AUTO |
967 FE_CAN_HIERARCHY_AUTO |
968 FE_CAN_RECOVER |
969 FE_CAN_MUTE_TS
970 },
971
972 .release = af9033_release,
973
974 .init = af9033_init,
975 .sleep = af9033_sleep,
976
977 .get_tune_settings = af9033_get_tune_settings,
978 .set_frontend = af9033_set_frontend,
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300979 .get_frontend = af9033_get_frontend,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300980
981 .read_status = af9033_read_status,
982 .read_snr = af9033_read_snr,
983 .read_signal_strength = af9033_read_signal_strength,
984 .read_ber = af9033_read_ber,
985 .read_ucblocks = af9033_read_ucblocks,
986
987 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
988};
989
990MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
991MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
992MODULE_LICENSE("GPL");