blob: dece775836ac87c64cb6d4518ee7bad264608874 [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 },
Antti Palosaari4902bb32013-01-07 09:48:03 -0300226 { 0x800045, state->cfg.adc_multiplier, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300227 };
228
229 /* program clock control */
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300230 clock_cw = af9033_div(state, state->cfg.clock, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300231 buf[0] = (clock_cw >> 0) & 0xff;
232 buf[1] = (clock_cw >> 8) & 0xff;
233 buf[2] = (clock_cw >> 16) & 0xff;
234 buf[3] = (clock_cw >> 24) & 0xff;
235
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300236 dev_dbg(&state->i2c->dev, "%s: clock=%d clock_cw=%08x\n",
237 __func__, state->cfg.clock, clock_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300238
239 ret = af9033_wr_regs(state, 0x800025, buf, 4);
240 if (ret < 0)
241 goto err;
242
243 /* program ADC control */
244 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
245 if (clock_adc_lut[i].clock == state->cfg.clock)
246 break;
247 }
248
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300249 adc_cw = af9033_div(state, clock_adc_lut[i].adc, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300250 buf[0] = (adc_cw >> 0) & 0xff;
251 buf[1] = (adc_cw >> 8) & 0xff;
252 buf[2] = (adc_cw >> 16) & 0xff;
253
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300254 dev_dbg(&state->i2c->dev, "%s: adc=%d adc_cw=%06x\n",
255 __func__, clock_adc_lut[i].adc, adc_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300256
257 ret = af9033_wr_regs(state, 0x80f1cd, buf, 3);
258 if (ret < 0)
259 goto err;
260
261 /* program register table */
262 for (i = 0; i < ARRAY_SIZE(tab); i++) {
263 ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val,
264 tab[i].mask);
265 if (ret < 0)
266 goto err;
267 }
268
269 /* settings for TS interface */
270 if (state->cfg.ts_mode == AF9033_TS_MODE_USB) {
271 ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01);
272 if (ret < 0)
273 goto err;
274
275 ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01);
276 if (ret < 0)
277 goto err;
278 } else {
279 ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01);
280 if (ret < 0)
281 goto err;
282
283 ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01);
284 if (ret < 0)
285 goto err;
286 }
287
288 /* load OFSM settings */
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300289 dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300290 len = ARRAY_SIZE(ofsm_init);
291 init = ofsm_init;
292 for (i = 0; i < len; i++) {
293 ret = af9033_wr_reg(state, init[i].reg, init[i].val);
294 if (ret < 0)
295 goto err;
296 }
297
298 /* load tuner specific settings */
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300299 dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n",
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300300 __func__);
301 switch (state->cfg.tuner) {
302 case AF9033_TUNER_TUA9001:
303 len = ARRAY_SIZE(tuner_init_tua9001);
304 init = tuner_init_tua9001;
305 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300306 case AF9033_TUNER_FC0011:
307 len = ARRAY_SIZE(tuner_init_fc0011);
308 init = tuner_init_fc0011;
309 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300310 case AF9033_TUNER_MXL5007T:
311 len = ARRAY_SIZE(tuner_init_mxl5007t);
312 init = tuner_init_mxl5007t;
313 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300314 case AF9033_TUNER_TDA18218:
315 len = ARRAY_SIZE(tuner_init_tda18218);
316 init = tuner_init_tda18218;
317 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300318 case AF9033_TUNER_FC2580:
319 len = ARRAY_SIZE(tuner_init_fc2580);
320 init = tuner_init_fc2580;
321 break;
Antti Palosaarie713ad12012-12-02 18:47:00 -0300322 case AF9033_TUNER_FC0012:
323 len = ARRAY_SIZE(tuner_init_fc0012);
324 init = tuner_init_fc0012;
325 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300326 case AF9033_TUNER_IT9135_38:
327 case AF9033_TUNER_IT9135_51:
328 case AF9033_TUNER_IT9135_52:
329 case AF9033_TUNER_IT9135_60:
330 case AF9033_TUNER_IT9135_61:
331 case AF9033_TUNER_IT9135_62:
332 len = 0;
333 break;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300334 default:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300335 dev_dbg(&state->i2c->dev, "%s: unsupported tuner ID=%d\n",
336 __func__, state->cfg.tuner);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300337 ret = -ENODEV;
338 goto err;
339 }
340
341 for (i = 0; i < len; i++) {
342 ret = af9033_wr_reg(state, init[i].reg, init[i].val);
343 if (ret < 0)
344 goto err;
345 }
346
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300347 if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
348 ret = af9033_wr_reg_mask(state, 0x00d91c, 0x01, 0x01);
349 if (ret < 0)
350 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300351
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300352 ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
353 if (ret < 0)
354 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300355
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300356 ret = af9033_wr_reg_mask(state, 0x00d916, 0x00, 0x01);
357 if (ret < 0)
358 goto err;
359 }
360
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300361 state->bandwidth_hz = 0; /* force to program all parameters */
362
363 return 0;
364
365err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300366 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300367
368 return ret;
369}
370
371static int af9033_sleep(struct dvb_frontend *fe)
372{
373 struct af9033_state *state = fe->demodulator_priv;
374 int ret, i;
375 u8 tmp;
376
377 ret = af9033_wr_reg(state, 0x80004c, 1);
378 if (ret < 0)
379 goto err;
380
381 ret = af9033_wr_reg(state, 0x800000, 0);
382 if (ret < 0)
383 goto err;
384
385 for (i = 100, tmp = 1; i && tmp; i--) {
386 ret = af9033_rd_reg(state, 0x80004c, &tmp);
387 if (ret < 0)
388 goto err;
389
390 usleep_range(200, 10000);
391 }
392
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300393 dev_dbg(&state->i2c->dev, "%s: loop=%d\n", __func__, i);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300394
395 if (i == 0) {
396 ret = -ETIMEDOUT;
397 goto err;
398 }
399
400 ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08);
401 if (ret < 0)
402 goto err;
403
404 /* prevent current leak (?) */
405 if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
406 /* enable parallel TS */
407 ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
408 if (ret < 0)
409 goto err;
410
411 ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01);
412 if (ret < 0)
413 goto err;
414 }
415
416 return 0;
417
418err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300419 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300420
421 return ret;
422}
423
424static int af9033_get_tune_settings(struct dvb_frontend *fe,
425 struct dvb_frontend_tune_settings *fesettings)
426{
427 fesettings->min_delay_ms = 800;
428 fesettings->step_size = 0;
429 fesettings->max_drift = 0;
430
431 return 0;
432}
433
434static int af9033_set_frontend(struct dvb_frontend *fe)
435{
436 struct af9033_state *state = fe->demodulator_priv;
437 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300438 int ret, i, spec_inv, sampling_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300439 u8 tmp, buf[3], bandwidth_reg_val;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300440 u32 if_frequency, freq_cw, adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300441
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300442 dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
443 __func__, c->frequency, c->bandwidth_hz);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300444
445 /* check bandwidth */
446 switch (c->bandwidth_hz) {
447 case 6000000:
448 bandwidth_reg_val = 0x00;
449 break;
450 case 7000000:
451 bandwidth_reg_val = 0x01;
452 break;
453 case 8000000:
454 bandwidth_reg_val = 0x02;
455 break;
456 default:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300457 dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n",
458 __func__);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300459 ret = -EINVAL;
460 goto err;
461 }
462
463 /* program tuner */
464 if (fe->ops.tuner_ops.set_params)
465 fe->ops.tuner_ops.set_params(fe);
466
467 /* program CFOE coefficients */
468 if (c->bandwidth_hz != state->bandwidth_hz) {
469 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
470 if (coeff_lut[i].clock == state->cfg.clock &&
471 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
472 break;
473 }
474 }
475 ret = af9033_wr_regs(state, 0x800001,
476 coeff_lut[i].val, sizeof(coeff_lut[i].val));
477 }
478
479 /* program frequency control */
480 if (c->bandwidth_hz != state->bandwidth_hz) {
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300481 spec_inv = state->cfg.spec_inv ? -1 : 1;
482
483 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
484 if (clock_adc_lut[i].clock == state->cfg.clock)
485 break;
486 }
487 adc_freq = clock_adc_lut[i].adc;
488
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300489 /* get used IF frequency */
490 if (fe->ops.tuner_ops.get_if_frequency)
491 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
492 else
493 if_frequency = 0;
494
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300495 sampling_freq = if_frequency;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300496
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300497 while (sampling_freq > (adc_freq / 2))
498 sampling_freq -= adc_freq;
499
500 if (sampling_freq >= 0)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300501 spec_inv *= -1;
502 else
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300503 sampling_freq *= -1;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300504
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300505 freq_cw = af9033_div(state, sampling_freq, adc_freq, 23ul);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300506
507 if (spec_inv == -1)
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300508 freq_cw = 0x800000 - freq_cw;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300509
Antti Palosaari4902bb32013-01-07 09:48:03 -0300510 if (state->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300511 freq_cw /= 2;
512
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300513 buf[0] = (freq_cw >> 0) & 0xff;
514 buf[1] = (freq_cw >> 8) & 0xff;
515 buf[2] = (freq_cw >> 16) & 0x7f;
516 ret = af9033_wr_regs(state, 0x800029, buf, 3);
517 if (ret < 0)
518 goto err;
519
520 state->bandwidth_hz = c->bandwidth_hz;
521 }
522
523 ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03);
524 if (ret < 0)
525 goto err;
526
527 ret = af9033_wr_reg(state, 0x800040, 0x00);
528 if (ret < 0)
529 goto err;
530
531 ret = af9033_wr_reg(state, 0x800047, 0x00);
532 if (ret < 0)
533 goto err;
534
535 ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01);
536 if (ret < 0)
537 goto err;
538
539 if (c->frequency <= 230000000)
540 tmp = 0x00; /* VHF */
541 else
542 tmp = 0x01; /* UHF */
543
544 ret = af9033_wr_reg(state, 0x80004b, tmp);
545 if (ret < 0)
546 goto err;
547
548 ret = af9033_wr_reg(state, 0x800000, 0x00);
549 if (ret < 0)
550 goto err;
551
552 return 0;
553
554err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300555 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300556
557 return ret;
558}
559
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300560static int af9033_get_frontend(struct dvb_frontend *fe)
561{
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300562 struct af9033_state *state = fe->demodulator_priv;
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300563 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300564 int ret;
565 u8 buf[8];
566
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300567 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300568
569 /* read all needed registers */
570 ret = af9033_rd_regs(state, 0x80f900, buf, sizeof(buf));
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300571 if (ret < 0)
572 goto err;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300573
574 switch ((buf[0] >> 0) & 3) {
575 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300576 c->transmission_mode = TRANSMISSION_MODE_2K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300577 break;
578 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300579 c->transmission_mode = TRANSMISSION_MODE_8K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300580 break;
581 }
582
583 switch ((buf[1] >> 0) & 3) {
584 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300585 c->guard_interval = GUARD_INTERVAL_1_32;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300586 break;
587 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300588 c->guard_interval = GUARD_INTERVAL_1_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300589 break;
590 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300591 c->guard_interval = GUARD_INTERVAL_1_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300592 break;
593 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300594 c->guard_interval = GUARD_INTERVAL_1_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300595 break;
596 }
597
598 switch ((buf[2] >> 0) & 7) {
599 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300600 c->hierarchy = HIERARCHY_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300601 break;
602 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300603 c->hierarchy = HIERARCHY_1;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300604 break;
605 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300606 c->hierarchy = HIERARCHY_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300607 break;
608 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300609 c->hierarchy = HIERARCHY_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300610 break;
611 }
612
613 switch ((buf[3] >> 0) & 3) {
614 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300615 c->modulation = QPSK;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300616 break;
617 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300618 c->modulation = QAM_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300619 break;
620 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300621 c->modulation = QAM_64;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300622 break;
623 }
624
625 switch ((buf[4] >> 0) & 3) {
626 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300627 c->bandwidth_hz = 6000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300628 break;
629 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300630 c->bandwidth_hz = 7000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300631 break;
632 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300633 c->bandwidth_hz = 8000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300634 break;
635 }
636
637 switch ((buf[6] >> 0) & 7) {
638 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300639 c->code_rate_HP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300640 break;
641 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300642 c->code_rate_HP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300643 break;
644 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300645 c->code_rate_HP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300646 break;
647 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300648 c->code_rate_HP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300649 break;
650 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300651 c->code_rate_HP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300652 break;
653 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300654 c->code_rate_HP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300655 break;
656 }
657
658 switch ((buf[7] >> 0) & 7) {
659 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300660 c->code_rate_LP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300661 break;
662 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300663 c->code_rate_LP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300664 break;
665 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300666 c->code_rate_LP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300667 break;
668 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300669 c->code_rate_LP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300670 break;
671 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300672 c->code_rate_LP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300673 break;
674 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300675 c->code_rate_LP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300676 break;
677 }
678
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300679 return 0;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300680
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300681err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300682 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300683
684 return ret;
685}
686
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300687static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
688{
689 struct af9033_state *state = fe->demodulator_priv;
690 int ret;
691 u8 tmp;
692
693 *status = 0;
694
695 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
696 ret = af9033_rd_reg(state, 0x800047, &tmp);
697 if (ret < 0)
698 goto err;
699
700 /* has signal */
701 if (tmp == 0x01)
702 *status |= FE_HAS_SIGNAL;
703
704 if (tmp != 0x02) {
705 /* TPS lock */
706 ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01);
707 if (ret < 0)
708 goto err;
709
710 if (tmp)
711 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
712 FE_HAS_VITERBI;
713
714 /* full lock */
715 ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01);
716 if (ret < 0)
717 goto err;
718
719 if (tmp)
720 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
721 FE_HAS_VITERBI | FE_HAS_SYNC |
722 FE_HAS_LOCK;
723 }
724
725 return 0;
726
727err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300728 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300729
730 return ret;
731}
732
733static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
734{
Antti Palosaarie898ef62012-04-01 12:50:02 -0300735 struct af9033_state *state = fe->demodulator_priv;
736 int ret, i, len;
737 u8 buf[3], tmp;
738 u32 snr_val;
739 const struct val_snr *uninitialized_var(snr_lut);
740
741 /* read value */
742 ret = af9033_rd_regs(state, 0x80002c, buf, 3);
743 if (ret < 0)
744 goto err;
745
746 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
747
748 /* read current modulation */
749 ret = af9033_rd_reg(state, 0x80f903, &tmp);
750 if (ret < 0)
751 goto err;
752
753 switch ((tmp >> 0) & 3) {
754 case 0:
755 len = ARRAY_SIZE(qpsk_snr_lut);
756 snr_lut = qpsk_snr_lut;
757 break;
758 case 1:
759 len = ARRAY_SIZE(qam16_snr_lut);
760 snr_lut = qam16_snr_lut;
761 break;
762 case 2:
763 len = ARRAY_SIZE(qam64_snr_lut);
764 snr_lut = qam64_snr_lut;
765 break;
766 default:
767 goto err;
768 }
769
770 for (i = 0; i < len; i++) {
771 tmp = snr_lut[i].snr;
772
773 if (snr_val < snr_lut[i].val)
774 break;
775 }
776
777 *snr = tmp * 10; /* dB/10 */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300778
779 return 0;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300780
781err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300782 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarie898ef62012-04-01 12:50:02 -0300783
784 return ret;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300785}
786
787static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
788{
789 struct af9033_state *state = fe->demodulator_priv;
790 int ret;
791 u8 strength2;
792
793 /* read signal strength of 0-100 scale */
794 ret = af9033_rd_reg(state, 0x800048, &strength2);
795 if (ret < 0)
796 goto err;
797
798 /* scale value to 0x0000-0xffff */
799 *strength = strength2 * 0xffff / 100;
800
801 return 0;
802
803err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300804 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300805
806 return ret;
807}
808
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300809static int af9033_update_ch_stat(struct af9033_state *state)
810{
811 int ret = 0;
812 u32 err_cnt, bit_cnt;
813 u16 abort_cnt;
814 u8 buf[7];
815
816 /* only update data every half second */
817 if (time_after(jiffies, state->last_stat_check + msecs_to_jiffies(500))) {
818 ret = af9033_rd_regs(state, 0x800032, buf, sizeof(buf));
819 if (ret < 0)
820 goto err;
821 /* in 8 byte packets? */
822 abort_cnt = (buf[1] << 8) + buf[0];
823 /* in bits */
824 err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
825 /* in 8 byte packets? always(?) 0x2710 = 10000 */
826 bit_cnt = (buf[6] << 8) + buf[5];
827
828 if (bit_cnt < abort_cnt) {
829 abort_cnt = 1000;
830 state->ber = 0xffffffff;
831 } else {
832 /* 8 byte packets, that have not been rejected already */
833 bit_cnt -= (u32)abort_cnt;
834 if (bit_cnt == 0) {
835 state->ber = 0xffffffff;
836 } else {
837 err_cnt -= (u32)abort_cnt * 8 * 8;
838 bit_cnt *= 8 * 8;
839 state->ber = err_cnt * (0xffffffff / bit_cnt);
840 }
841 }
842 state->ucb += abort_cnt;
843 state->last_stat_check = jiffies;
844 }
845
846 return 0;
847err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300848 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
849
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300850 return ret;
851}
852
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300853static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
854{
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300855 struct af9033_state *state = fe->demodulator_priv;
856 int ret;
857
858 ret = af9033_update_ch_stat(state);
859 if (ret < 0)
860 return ret;
861
862 *ber = state->ber;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300863
864 return 0;
865}
866
867static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
868{
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300869 struct af9033_state *state = fe->demodulator_priv;
870 int ret;
871
872 ret = af9033_update_ch_stat(state);
873 if (ret < 0)
874 return ret;
875
876 *ucblocks = state->ucb;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300877
878 return 0;
879}
880
881static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
882{
883 struct af9033_state *state = fe->demodulator_priv;
884 int ret;
885
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300886 dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300887
888 ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01);
889 if (ret < 0)
890 goto err;
891
892 return 0;
893
894err:
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300895 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300896
897 return ret;
898}
899
900static struct dvb_frontend_ops af9033_ops;
901
902struct dvb_frontend *af9033_attach(const struct af9033_config *config,
903 struct i2c_adapter *i2c)
904{
905 int ret;
906 struct af9033_state *state;
907 u8 buf[8];
908
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300909 dev_dbg(&i2c->dev, "%s:\n", __func__);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300910
911 /* allocate memory for the internal state */
912 state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL);
913 if (state == NULL)
914 goto err;
915
916 /* setup the state */
917 state->i2c = i2c;
918 memcpy(&state->cfg, config, sizeof(struct af9033_config));
919
Antti Palosaari8e8a5ac2012-04-01 14:13:36 -0300920 if (state->cfg.clock != 12000000) {
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300921 dev_err(&state->i2c->dev, "%s: af9033: unsupported clock=%d, " \
922 "only 12000000 Hz is supported currently\n",
923 KBUILD_MODNAME, state->cfg.clock);
Antti Palosaari8e8a5ac2012-04-01 14:13:36 -0300924 goto err;
925 }
926
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300927 /* firmware version */
928 ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4);
929 if (ret < 0)
930 goto err;
931
932 ret = af9033_rd_regs(state, 0x804191, &buf[4], 4);
933 if (ret < 0)
934 goto err;
935
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300936 dev_info(&state->i2c->dev, "%s: firmware version: LINK=%d.%d.%d.%d " \
937 "OFDM=%d.%d.%d.%d\n", KBUILD_MODNAME, buf[0], buf[1],
938 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300939
Antti Palosaari12897dc2012-09-16 22:26:57 -0300940
Antti Palosaari4902bb32013-01-07 09:48:03 -0300941 /* FIXME: Do not abuse adc_multiplier for detecting IT9135 */
942 if (state->cfg.adc_multiplier != AF9033_ADC_MULTIPLIER_2X) {
943 /* sleep */
944 ret = af9033_wr_reg(state, 0x80004c, 1);
945 if (ret < 0)
946 goto err;
947
948 ret = af9033_wr_reg(state, 0x800000, 0);
949 if (ret < 0)
950 goto err;
951 }
Antti Palosaari12897dc2012-09-16 22:26:57 -0300952
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300953 /* configure internal TS mode */
954 switch (state->cfg.ts_mode) {
955 case AF9033_TS_MODE_PARALLEL:
956 state->ts_mode_parallel = true;
957 break;
958 case AF9033_TS_MODE_SERIAL:
959 state->ts_mode_serial = true;
960 break;
961 case AF9033_TS_MODE_USB:
962 /* usb mode for AF9035 */
963 default:
964 break;
965 }
966
967 /* create dvb_frontend */
968 memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
969 state->fe.demodulator_priv = state;
970
971 return &state->fe;
972
973err:
974 kfree(state);
975 return NULL;
976}
977EXPORT_SYMBOL(af9033_attach);
978
979static struct dvb_frontend_ops af9033_ops = {
980 .delsys = { SYS_DVBT },
981 .info = {
982 .name = "Afatech AF9033 (DVB-T)",
983 .frequency_min = 174000000,
984 .frequency_max = 862000000,
985 .frequency_stepsize = 250000,
986 .frequency_tolerance = 0,
987 .caps = FE_CAN_FEC_1_2 |
988 FE_CAN_FEC_2_3 |
989 FE_CAN_FEC_3_4 |
990 FE_CAN_FEC_5_6 |
991 FE_CAN_FEC_7_8 |
992 FE_CAN_FEC_AUTO |
993 FE_CAN_QPSK |
994 FE_CAN_QAM_16 |
995 FE_CAN_QAM_64 |
996 FE_CAN_QAM_AUTO |
997 FE_CAN_TRANSMISSION_MODE_AUTO |
998 FE_CAN_GUARD_INTERVAL_AUTO |
999 FE_CAN_HIERARCHY_AUTO |
1000 FE_CAN_RECOVER |
1001 FE_CAN_MUTE_TS
1002 },
1003
1004 .release = af9033_release,
1005
1006 .init = af9033_init,
1007 .sleep = af9033_sleep,
1008
1009 .get_tune_settings = af9033_get_tune_settings,
1010 .set_frontend = af9033_set_frontend,
Gianluca Gennari0a4df232012-04-05 12:47:19 -03001011 .get_frontend = af9033_get_frontend,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001012
1013 .read_status = af9033_read_status,
1014 .read_snr = af9033_read_snr,
1015 .read_signal_strength = af9033_read_signal_strength,
1016 .read_ber = af9033_read_ber,
1017 .read_ucblocks = af9033_read_ucblocks,
1018
1019 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1020};
1021
1022MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1023MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1024MODULE_LICENSE("GPL");