blob: b978002af4d8afc76563e8e73235595a68a09e48 [file] [log] [blame]
Antti Palosaari825b9672008-09-15 15:01:52 -03001/*
Antti Palosaaric89f66f62010-06-17 21:19:13 -03002 * Afatech AF9013 demodulator driver
Antti Palosaari825b9672008-09-15 15:01:52 -03003 *
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
Antti Palosaarif571e002011-11-28 20:58:11 -03005 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
Antti Palosaari825b9672008-09-15 15:01:52 -03006 *
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 *
Antti Palosaari825b9672008-09-15 15:01:52 -030019 */
20
Antti Palosaari825b9672008-09-15 15:01:52 -030021#include "af9013_priv.h"
Antti Palosaari825b9672008-09-15 15:01:52 -030022
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030023/* Max transfer size done by I2C transfer functions */
24#define MAX_XFER_SIZE 64
25
Antti Palosaari825b9672008-09-15 15:01:52 -030026struct af9013_state {
27 struct i2c_adapter *i2c;
Antti Palosaarif571e002011-11-28 20:58:11 -030028 struct dvb_frontend fe;
Antti Palosaari825b9672008-09-15 15:01:52 -030029 struct af9013_config config;
30
Antti Palosaari9e35cd22010-10-07 16:34:55 -030031 /* tuner/demod RF and IF AGC limits used for signal strength calc */
32 u8 signal_strength_en, rf_50, rf_80, if_50, if_80;
Antti Palosaari825b9672008-09-15 15:01:52 -030033 u16 signal_strength;
34 u32 ber;
35 u32 ucblocks;
36 u16 snr;
Antti Palosaarif571e002011-11-28 20:58:11 -030037 u32 bandwidth_hz;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030038 enum fe_status fe_status;
Antti Palosaarif571e002011-11-28 20:58:11 -030039 unsigned long set_frontend_jiffies;
40 unsigned long read_status_jiffies;
41 bool first_tune;
42 bool i2c_gate_state;
43 unsigned int statistics_step:3;
44 struct delayed_work statistics_work;
Antti Palosaari825b9672008-09-15 15:01:52 -030045};
46
Antti Palosaarif571e002011-11-28 20:58:11 -030047/* write multiple registers */
48static int af9013_wr_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg,
49 const u8 *val, int len)
Antti Palosaari825b9672008-09-15 15:01:52 -030050{
Antti Palosaarif571e002011-11-28 20:58:11 -030051 int ret;
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030052 u8 buf[MAX_XFER_SIZE];
Antti Palosaarif571e002011-11-28 20:58:11 -030053 struct i2c_msg msg[1] = {
54 {
55 .addr = priv->config.i2c_addr,
56 .flags = 0,
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030057 .len = 3 + len,
Antti Palosaarif571e002011-11-28 20:58:11 -030058 .buf = buf,
59 }
60 };
Antti Palosaari825b9672008-09-15 15:01:52 -030061
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030062 if (3 + len > sizeof(buf)) {
63 dev_warn(&priv->i2c->dev,
64 "%s: i2c wr reg=%04x: len=%d is too big!\n",
65 KBUILD_MODNAME, reg, len);
66 return -EINVAL;
67 }
68
Antti Palosaarif571e002011-11-28 20:58:11 -030069 buf[0] = (reg >> 8) & 0xff;
70 buf[1] = (reg >> 0) & 0xff;
Antti Palosaari825b9672008-09-15 15:01:52 -030071 buf[2] = mbox;
72 memcpy(&buf[3], val, len);
73
Antti Palosaarif571e002011-11-28 20:58:11 -030074 ret = i2c_transfer(priv->i2c, msg, 1);
75 if (ret == 1) {
76 ret = 0;
77 } else {
Antti Palosaari8df379c2012-09-12 20:23:43 -030078 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%04x " \
79 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
Antti Palosaarif571e002011-11-28 20:58:11 -030080 ret = -EREMOTEIO;
Antti Palosaari825b9672008-09-15 15:01:52 -030081 }
Antti Palosaarif571e002011-11-28 20:58:11 -030082 return ret;
83}
84
85/* read multiple registers */
86static int af9013_rd_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg,
87 u8 *val, int len)
88{
89 int ret;
90 u8 buf[3];
91 struct i2c_msg msg[2] = {
92 {
93 .addr = priv->config.i2c_addr,
94 .flags = 0,
95 .len = 3,
96 .buf = buf,
97 }, {
98 .addr = priv->config.i2c_addr,
99 .flags = I2C_M_RD,
100 .len = len,
101 .buf = val,
102 }
103 };
104
105 buf[0] = (reg >> 8) & 0xff;
106 buf[1] = (reg >> 0) & 0xff;
107 buf[2] = mbox;
108
109 ret = i2c_transfer(priv->i2c, msg, 2);
110 if (ret == 2) {
111 ret = 0;
112 } else {
Antti Palosaari8df379c2012-09-12 20:23:43 -0300113 dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%04x " \
114 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
Antti Palosaarif571e002011-11-28 20:58:11 -0300115 ret = -EREMOTEIO;
116 }
117 return ret;
118}
119
120/* write multiple registers */
121static int af9013_wr_regs(struct af9013_state *priv, u16 reg, const u8 *val,
122 int len)
123{
124 int ret, i;
125 u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(1 << 0);
126
Dan Carpenter938ca362012-01-05 02:23:28 -0300127 if ((priv->config.ts_mode == AF9013_TS_USB) &&
128 ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) {
Antti Palosaarif571e002011-11-28 20:58:11 -0300129 mbox |= ((len - 1) << 2);
130 ret = af9013_wr_regs_i2c(priv, mbox, reg, val, len);
131 } else {
132 for (i = 0; i < len; i++) {
133 ret = af9013_wr_regs_i2c(priv, mbox, reg+i, val+i, 1);
134 if (ret)
135 goto err;
136 }
137 }
138
139err:
Antti Palosaari825b9672008-09-15 15:01:52 -0300140 return 0;
141}
142
Antti Palosaarif571e002011-11-28 20:58:11 -0300143/* read multiple registers */
144static int af9013_rd_regs(struct af9013_state *priv, u16 reg, u8 *val, int len)
Antti Palosaari825b9672008-09-15 15:01:52 -0300145{
Antti Palosaarif571e002011-11-28 20:58:11 -0300146 int ret, i;
147 u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(0 << 0);
148
Dan Carpenter938ca362012-01-05 02:23:28 -0300149 if ((priv->config.ts_mode == AF9013_TS_USB) &&
150 ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) {
Antti Palosaarif571e002011-11-28 20:58:11 -0300151 mbox |= ((len - 1) << 2);
152 ret = af9013_rd_regs_i2c(priv, mbox, reg, val, len);
153 } else {
154 for (i = 0; i < len; i++) {
155 ret = af9013_rd_regs_i2c(priv, mbox, reg+i, val+i, 1);
156 if (ret)
157 goto err;
158 }
159 }
160
161err:
162 return 0;
163}
164
165/* write single register */
166static int af9013_wr_reg(struct af9013_state *priv, u16 reg, u8 val)
167{
168 return af9013_wr_regs(priv, reg, &val, 1);
169}
170
171/* read single register */
172static int af9013_rd_reg(struct af9013_state *priv, u16 reg, u8 *val)
173{
174 return af9013_rd_regs(priv, reg, val, 1);
Antti Palosaari825b9672008-09-15 15:01:52 -0300175}
176
177static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val,
178 u8 len)
179{
Antti Palosaarif571e002011-11-28 20:58:11 -0300180 u8 mbox = (1 << 7)|(1 << 6)|((len - 1) << 2)|(1 << 1)|(1 << 0);
181 return af9013_wr_regs_i2c(state, mbox, reg, val, len);
Antti Palosaari825b9672008-09-15 15:01:52 -0300182}
183
Antti Palosaarif571e002011-11-28 20:58:11 -0300184static int af9013_wr_reg_bits(struct af9013_state *state, u16 reg, int pos,
185 int len, u8 val)
Antti Palosaari825b9672008-09-15 15:01:52 -0300186{
187 int ret;
188 u8 tmp, mask;
189
Antti Palosaarif571e002011-11-28 20:58:11 -0300190 /* no need for read if whole reg is written */
191 if (len != 8) {
192 ret = af9013_rd_reg(state, reg, &tmp);
193 if (ret)
194 return ret;
Antti Palosaari825b9672008-09-15 15:01:52 -0300195
Antti Palosaarif571e002011-11-28 20:58:11 -0300196 mask = (0xff >> (8 - len)) << pos;
197 val <<= pos;
198 tmp &= ~mask;
199 val |= tmp;
200 }
Antti Palosaari825b9672008-09-15 15:01:52 -0300201
Antti Palosaarif571e002011-11-28 20:58:11 -0300202 return af9013_wr_reg(state, reg, val);
Antti Palosaari825b9672008-09-15 15:01:52 -0300203}
204
Antti Palosaarif571e002011-11-28 20:58:11 -0300205static int af9013_rd_reg_bits(struct af9013_state *state, u16 reg, int pos,
206 int len, u8 *val)
Antti Palosaari825b9672008-09-15 15:01:52 -0300207{
208 int ret;
209 u8 tmp;
210
Antti Palosaarif571e002011-11-28 20:58:11 -0300211 ret = af9013_rd_reg(state, reg, &tmp);
Antti Palosaari825b9672008-09-15 15:01:52 -0300212 if (ret)
213 return ret;
Antti Palosaarif571e002011-11-28 20:58:11 -0300214
215 *val = (tmp >> pos);
216 *val &= (0xff >> (8 - len));
217
Antti Palosaari825b9672008-09-15 15:01:52 -0300218 return 0;
219}
220
221static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
222{
223 int ret;
224 u8 pos;
225 u16 addr;
Antti Palosaari825b9672008-09-15 15:01:52 -0300226
Antti Palosaari8df379c2012-09-12 20:23:43 -0300227 dev_dbg(&state->i2c->dev, "%s: gpio=%d gpioval=%02x\n",
228 __func__, gpio, gpioval);
Antti Palosaarif571e002011-11-28 20:58:11 -0300229
230 /*
231 * GPIO0 & GPIO1 0xd735
232 * GPIO2 & GPIO3 0xd736
233 */
Antti Palosaari825b9672008-09-15 15:01:52 -0300234
235 switch (gpio) {
236 case 0:
237 case 1:
238 addr = 0xd735;
239 break;
240 case 2:
241 case 3:
242 addr = 0xd736;
243 break;
244
245 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300246 dev_err(&state->i2c->dev, "%s: invalid gpio=%d\n",
247 KBUILD_MODNAME, gpio);
Antti Palosaari825b9672008-09-15 15:01:52 -0300248 ret = -EINVAL;
Antti Palosaarif571e002011-11-28 20:58:11 -0300249 goto err;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300250 }
Antti Palosaari825b9672008-09-15 15:01:52 -0300251
252 switch (gpio) {
253 case 0:
254 case 2:
255 pos = 0;
256 break;
257 case 1:
258 case 3:
259 default:
260 pos = 4;
261 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300262 }
Antti Palosaari825b9672008-09-15 15:01:52 -0300263
Antti Palosaarif571e002011-11-28 20:58:11 -0300264 ret = af9013_wr_reg_bits(state, addr, pos, 4, gpioval);
265 if (ret)
266 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300267
Antti Palosaarif571e002011-11-28 20:58:11 -0300268 return ret;
269err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300270 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari825b9672008-09-15 15:01:52 -0300271 return ret;
272}
273
Antti Palosaari8df379c2012-09-12 20:23:43 -0300274static u32 af9013_div(struct af9013_state *state, u32 a, u32 b, u32 x)
Antti Palosaari825b9672008-09-15 15:01:52 -0300275{
276 u32 r = 0, c = 0, i;
Antti Palosaarif571e002011-11-28 20:58:11 -0300277
Antti Palosaari8df379c2012-09-12 20:23:43 -0300278 dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x);
Antti Palosaari825b9672008-09-15 15:01:52 -0300279
280 if (a > b) {
281 c = a / b;
282 a = a - c * b;
283 }
284
285 for (i = 0; i < x; i++) {
286 if (a >= b) {
287 r += 1;
288 a -= b;
289 }
290 a <<= 1;
291 r <<= 1;
292 }
293 r = (c << (u32)x) + r;
294
Antti Palosaari8df379c2012-09-12 20:23:43 -0300295 dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n",
296 __func__, a, b, x, r, r);
297
Antti Palosaari825b9672008-09-15 15:01:52 -0300298 return r;
299}
300
Antti Palosaarif571e002011-11-28 20:58:11 -0300301static int af9013_power_ctrl(struct af9013_state *state, u8 onoff)
Antti Palosaari825b9672008-09-15 15:01:52 -0300302{
Antti Palosaarif571e002011-11-28 20:58:11 -0300303 int ret, i;
304 u8 tmp;
Antti Palosaari825b9672008-09-15 15:01:52 -0300305
Antti Palosaari8df379c2012-09-12 20:23:43 -0300306 dev_dbg(&state->i2c->dev, "%s: onoff=%d\n", __func__, onoff);
Antti Palosaari825b9672008-09-15 15:01:52 -0300307
Antti Palosaarif571e002011-11-28 20:58:11 -0300308 /* enable reset */
309 ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 1);
310 if (ret)
311 goto err;
Antti Palosaari9b22edd2010-09-12 20:48:58 -0300312
Antti Palosaarif571e002011-11-28 20:58:11 -0300313 /* start reset mechanism */
314 ret = af9013_wr_reg(state, 0xaeff, 1);
315 if (ret)
316 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300317
Antti Palosaarif571e002011-11-28 20:58:11 -0300318 /* wait reset performs */
319 for (i = 0; i < 150; i++) {
320 ret = af9013_rd_reg_bits(state, 0xd417, 1, 1, &tmp);
Antti Palosaari825b9672008-09-15 15:01:52 -0300321 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -0300322 goto err;
323
324 if (tmp)
325 break; /* reset done */
326
327 usleep_range(5000, 25000);
Antti Palosaari825b9672008-09-15 15:01:52 -0300328 }
329
Antti Palosaarif571e002011-11-28 20:58:11 -0300330 if (!tmp)
331 return -ETIMEDOUT;
332
333 if (onoff) {
334 /* clear reset */
335 ret = af9013_wr_reg_bits(state, 0xd417, 1, 1, 0);
336 if (ret)
337 goto err;
338
339 /* disable reset */
340 ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 0);
341
342 /* power on */
343 ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 0);
344 } else {
345 /* power off */
346 ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 1);
347 }
348
349 return ret;
350err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300351 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari825b9672008-09-15 15:01:52 -0300352 return ret;
353}
354
Antti Palosaarif571e002011-11-28 20:58:11 -0300355static int af9013_statistics_ber_unc_start(struct dvb_frontend *fe)
Antti Palosaari825b9672008-09-15 15:01:52 -0300356{
Antti Palosaarif571e002011-11-28 20:58:11 -0300357 struct af9013_state *state = fe->demodulator_priv;
Antti Palosaari825b9672008-09-15 15:01:52 -0300358 int ret;
Antti Palosaari825b9672008-09-15 15:01:52 -0300359
Antti Palosaari8df379c2012-09-12 20:23:43 -0300360 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Antti Palosaari825b9672008-09-15 15:01:52 -0300361
Antti Palosaarif571e002011-11-28 20:58:11 -0300362 /* reset and start BER counter */
363 ret = af9013_wr_reg_bits(state, 0xd391, 4, 1, 1);
364 if (ret)
365 goto err;
366
367 return ret;
368err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300369 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -0300370 return ret;
371}
372
373static int af9013_statistics_ber_unc_result(struct dvb_frontend *fe)
374{
375 struct af9013_state *state = fe->demodulator_priv;
376 int ret;
377 u8 buf[5];
378
Antti Palosaari8df379c2012-09-12 20:23:43 -0300379 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -0300380
381 /* check if error bit count is ready */
382 ret = af9013_rd_reg_bits(state, 0xd391, 4, 1, &buf[0]);
383 if (ret)
384 goto err;
385
386 if (!buf[0]) {
Antti Palosaari8df379c2012-09-12 20:23:43 -0300387 dev_dbg(&state->i2c->dev, "%s: not ready\n", __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -0300388 return 0;
389 }
390
391 ret = af9013_rd_regs(state, 0xd387, buf, 5);
392 if (ret)
393 goto err;
394
395 state->ber = (buf[2] << 16) | (buf[1] << 8) | buf[0];
396 state->ucblocks += (buf[4] << 8) | buf[3];
397
398 return ret;
399err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300400 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -0300401 return ret;
402}
403
404static int af9013_statistics_snr_start(struct dvb_frontend *fe)
405{
406 struct af9013_state *state = fe->demodulator_priv;
407 int ret;
408
Antti Palosaari8df379c2012-09-12 20:23:43 -0300409 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -0300410
411 /* start SNR meas */
412 ret = af9013_wr_reg_bits(state, 0xd2e1, 3, 1, 1);
413 if (ret)
414 goto err;
415
416 return ret;
417err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300418 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -0300419 return ret;
420}
421
422static int af9013_statistics_snr_result(struct dvb_frontend *fe)
423{
424 struct af9013_state *state = fe->demodulator_priv;
425 int ret, i, len;
426 u8 buf[3], tmp;
427 u32 snr_val;
428 const struct af9013_snr *uninitialized_var(snr_lut);
429
Antti Palosaari8df379c2012-09-12 20:23:43 -0300430 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -0300431
432 /* check if SNR ready */
433 ret = af9013_rd_reg_bits(state, 0xd2e1, 3, 1, &tmp);
434 if (ret)
435 goto err;
436
437 if (!tmp) {
Antti Palosaari8df379c2012-09-12 20:23:43 -0300438 dev_dbg(&state->i2c->dev, "%s: not ready\n", __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -0300439 return 0;
440 }
441
442 /* read value */
443 ret = af9013_rd_regs(state, 0xd2e3, buf, 3);
444 if (ret)
445 goto err;
446
447 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
448
449 /* read current modulation */
450 ret = af9013_rd_reg(state, 0xd3c1, &tmp);
451 if (ret)
452 goto err;
453
454 switch ((tmp >> 6) & 3) {
455 case 0:
456 len = ARRAY_SIZE(qpsk_snr_lut);
457 snr_lut = qpsk_snr_lut;
Antti Palosaari825b9672008-09-15 15:01:52 -0300458 break;
Antti Palosaarif571e002011-11-28 20:58:11 -0300459 case 1:
460 len = ARRAY_SIZE(qam16_snr_lut);
461 snr_lut = qam16_snr_lut;
Antti Palosaari825b9672008-09-15 15:01:52 -0300462 break;
Antti Palosaarif571e002011-11-28 20:58:11 -0300463 case 2:
464 len = ARRAY_SIZE(qam64_snr_lut);
465 snr_lut = qam64_snr_lut;
Antti Palosaari825b9672008-09-15 15:01:52 -0300466 break;
467 default:
Antti Palosaarif571e002011-11-28 20:58:11 -0300468 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300469 }
470
Antti Palosaarif571e002011-11-28 20:58:11 -0300471 for (i = 0; i < len; i++) {
472 tmp = snr_lut[i].snr;
Antti Palosaari825b9672008-09-15 15:01:52 -0300473
Antti Palosaarif571e002011-11-28 20:58:11 -0300474 if (snr_val < snr_lut[i].val)
475 break;
Antti Palosaari825b9672008-09-15 15:01:52 -0300476 }
Antti Palosaarif571e002011-11-28 20:58:11 -0300477 state->snr = tmp * 10; /* dB/10 */
478
479 return ret;
480err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300481 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari825b9672008-09-15 15:01:52 -0300482 return ret;
483}
484
Antti Palosaarif571e002011-11-28 20:58:11 -0300485static int af9013_statistics_signal_strength(struct dvb_frontend *fe)
486{
487 struct af9013_state *state = fe->demodulator_priv;
488 int ret = 0;
489 u8 buf[2], rf_gain, if_gain;
490 int signal_strength;
491
Antti Palosaari8df379c2012-09-12 20:23:43 -0300492 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -0300493
494 if (!state->signal_strength_en)
495 return 0;
496
497 ret = af9013_rd_regs(state, 0xd07c, buf, 2);
498 if (ret)
499 goto err;
500
501 rf_gain = buf[0];
502 if_gain = buf[1];
503
504 signal_strength = (0xffff / \
505 (9 * (state->rf_50 + state->if_50) - \
506 11 * (state->rf_80 + state->if_80))) * \
507 (10 * (rf_gain + if_gain) - \
508 11 * (state->rf_80 + state->if_80));
509 if (signal_strength < 0)
510 signal_strength = 0;
511 else if (signal_strength > 0xffff)
512 signal_strength = 0xffff;
513
514 state->signal_strength = signal_strength;
515
516 return ret;
517err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300518 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -0300519 return ret;
520}
521
522static void af9013_statistics_work(struct work_struct *work)
Antti Palosaari825b9672008-09-15 15:01:52 -0300523{
Antti Palosaarif571e002011-11-28 20:58:11 -0300524 struct af9013_state *state = container_of(work,
525 struct af9013_state, statistics_work.work);
526 unsigned int next_msec;
Antti Palosaari825b9672008-09-15 15:01:52 -0300527
Antti Palosaarif571e002011-11-28 20:58:11 -0300528 /* update only signal strength when demod is not locked */
529 if (!(state->fe_status & FE_HAS_LOCK)) {
530 state->statistics_step = 0;
531 state->ber = 0;
532 state->snr = 0;
533 }
534
535 switch (state->statistics_step) {
536 default:
537 state->statistics_step = 0;
538 case 0:
Hans Verkuilfdf07b02012-04-20 08:04:48 -0300539 af9013_statistics_signal_strength(&state->fe);
Antti Palosaarif571e002011-11-28 20:58:11 -0300540 state->statistics_step++;
541 next_msec = 300;
542 break;
543 case 1:
Hans Verkuilfdf07b02012-04-20 08:04:48 -0300544 af9013_statistics_snr_start(&state->fe);
Antti Palosaarif571e002011-11-28 20:58:11 -0300545 state->statistics_step++;
546 next_msec = 200;
547 break;
548 case 2:
Hans Verkuilfdf07b02012-04-20 08:04:48 -0300549 af9013_statistics_ber_unc_start(&state->fe);
Antti Palosaarif571e002011-11-28 20:58:11 -0300550 state->statistics_step++;
551 next_msec = 1000;
552 break;
553 case 3:
Hans Verkuilfdf07b02012-04-20 08:04:48 -0300554 af9013_statistics_snr_result(&state->fe);
Antti Palosaarif571e002011-11-28 20:58:11 -0300555 state->statistics_step++;
556 next_msec = 400;
557 break;
558 case 4:
Hans Verkuilfdf07b02012-04-20 08:04:48 -0300559 af9013_statistics_ber_unc_result(&state->fe);
Antti Palosaarif571e002011-11-28 20:58:11 -0300560 state->statistics_step++;
561 next_msec = 100;
562 break;
563 }
564
565 schedule_delayed_work(&state->statistics_work,
566 msecs_to_jiffies(next_msec));
Antti Palosaarif571e002011-11-28 20:58:11 -0300567}
568
569static int af9013_get_tune_settings(struct dvb_frontend *fe,
570 struct dvb_frontend_tune_settings *fesettings)
571{
572 fesettings->min_delay_ms = 800;
573 fesettings->step_size = 0;
574 fesettings->max_drift = 0;
575
576 return 0;
577}
578
Mauro Carvalho Chehab59d3cc12011-12-30 22:25:27 -0300579static int af9013_set_frontend(struct dvb_frontend *fe)
Antti Palosaarif571e002011-11-28 20:58:11 -0300580{
581 struct af9013_state *state = fe->demodulator_priv;
582 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
583 int ret, i, sampling_freq;
584 bool auto_mode, spec_inv;
585 u8 buf[6];
586 u32 if_frequency, freq_cw;
587
Antti Palosaari8df379c2012-09-12 20:23:43 -0300588 dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
589 __func__, c->frequency, c->bandwidth_hz);
Antti Palosaarif571e002011-11-28 20:58:11 -0300590
591 /* program tuner */
592 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300593 fe->ops.tuner_ops.set_params(fe);
Antti Palosaarif571e002011-11-28 20:58:11 -0300594
595 /* program CFOE coefficients */
596 if (c->bandwidth_hz != state->bandwidth_hz) {
597 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
598 if (coeff_lut[i].clock == state->config.clock &&
599 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
600 break;
601 }
Antti Palosaari825b9672008-09-15 15:01:52 -0300602 }
603
Mauro Carvalho Chehabd7b76c92015-04-28 19:02:19 -0300604 /* Return an error if can't find bandwidth or the right clock */
605 if (i == ARRAY_SIZE(coeff_lut))
606 return -EINVAL;
607
Antti Palosaarif571e002011-11-28 20:58:11 -0300608 ret = af9013_wr_regs(state, 0xae00, coeff_lut[i].val,
609 sizeof(coeff_lut[i].val));
610 }
Antti Palosaari825b9672008-09-15 15:01:52 -0300611
Antti Palosaarif571e002011-11-28 20:58:11 -0300612 /* program frequency control */
613 if (c->bandwidth_hz != state->bandwidth_hz || state->first_tune) {
Antti Palosaari6d42b212011-11-13 00:26:17 -0300614 /* get used IF frequency */
615 if (fe->ops.tuner_ops.get_if_frequency)
Antti Palosaarif571e002011-11-28 20:58:11 -0300616 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
Antti Palosaari6d42b212011-11-13 00:26:17 -0300617 else
Antti Palosaarif571e002011-11-28 20:58:11 -0300618 if_frequency = state->config.if_frequency;
Antti Palosaari825b9672008-09-15 15:01:52 -0300619
Antti Palosaari8df379c2012-09-12 20:23:43 -0300620 dev_dbg(&state->i2c->dev, "%s: if_frequency=%d\n",
621 __func__, if_frequency);
Antti Palosaariab2e06a2012-09-02 18:47:25 -0300622
Antti Palosaarif571e002011-11-28 20:58:11 -0300623 sampling_freq = if_frequency;
Antti Palosaari825b9672008-09-15 15:01:52 -0300624
Antti Palosaarif571e002011-11-28 20:58:11 -0300625 while (sampling_freq > (state->config.clock / 2))
626 sampling_freq -= state->config.clock;
Antti Palosaari825b9672008-09-15 15:01:52 -0300627
Antti Palosaarif571e002011-11-28 20:58:11 -0300628 if (sampling_freq < 0) {
629 sampling_freq *= -1;
630 spec_inv = state->config.spec_inv;
631 } else {
632 spec_inv = !state->config.spec_inv;
Antti Palosaari825b9672008-09-15 15:01:52 -0300633 }
Antti Palosaarif571e002011-11-28 20:58:11 -0300634
Antti Palosaari8df379c2012-09-12 20:23:43 -0300635 freq_cw = af9013_div(state, sampling_freq, state->config.clock,
636 23);
Antti Palosaarif571e002011-11-28 20:58:11 -0300637
638 if (spec_inv)
639 freq_cw = 0x800000 - freq_cw;
640
641 buf[0] = (freq_cw >> 0) & 0xff;
642 buf[1] = (freq_cw >> 8) & 0xff;
643 buf[2] = (freq_cw >> 16) & 0x7f;
644
645 freq_cw = 0x800000 - freq_cw;
646
647 buf[3] = (freq_cw >> 0) & 0xff;
648 buf[4] = (freq_cw >> 8) & 0xff;
649 buf[5] = (freq_cw >> 16) & 0x7f;
650
651 ret = af9013_wr_regs(state, 0xd140, buf, 3);
652 if (ret)
653 goto err;
654
655 ret = af9013_wr_regs(state, 0x9be7, buf, 6);
656 if (ret)
657 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300658 }
Antti Palosaari825b9672008-09-15 15:01:52 -0300659
Antti Palosaarif571e002011-11-28 20:58:11 -0300660 /* clear TPS lock flag */
661 ret = af9013_wr_reg_bits(state, 0xd330, 3, 1, 1);
662 if (ret)
663 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300664
Antti Palosaarif571e002011-11-28 20:58:11 -0300665 /* clear MPEG2 lock flag */
666 ret = af9013_wr_reg_bits(state, 0xd507, 6, 1, 0);
667 if (ret)
668 goto err;
Antti Palosaaria2f5a812009-07-10 20:03:43 -0300669
Antti Palosaarif571e002011-11-28 20:58:11 -0300670 /* empty channel function */
671 ret = af9013_wr_reg_bits(state, 0x9bfe, 0, 1, 0);
672 if (ret)
673 goto err;
674
675 /* empty DVB-T channel function */
676 ret = af9013_wr_reg_bits(state, 0x9bc2, 0, 1, 0);
677 if (ret)
678 goto err;
679
680 /* transmission parameters */
681 auto_mode = false;
682 memset(buf, 0, 3);
683
684 switch (c->transmission_mode) {
Antti Palosaari825b9672008-09-15 15:01:52 -0300685 case TRANSMISSION_MODE_AUTO:
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300686 auto_mode = true;
Antti Palosaarif571e002011-11-28 20:58:11 -0300687 break;
Antti Palosaari825b9672008-09-15 15:01:52 -0300688 case TRANSMISSION_MODE_2K:
689 break;
690 case TRANSMISSION_MODE_8K:
691 buf[0] |= (1 << 0);
692 break;
693 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300694 dev_dbg(&state->i2c->dev, "%s: invalid transmission_mode\n",
695 __func__);
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300696 auto_mode = true;
Antti Palosaari825b9672008-09-15 15:01:52 -0300697 }
698
Antti Palosaarif571e002011-11-28 20:58:11 -0300699 switch (c->guard_interval) {
Antti Palosaari825b9672008-09-15 15:01:52 -0300700 case GUARD_INTERVAL_AUTO:
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300701 auto_mode = true;
Antti Palosaarif571e002011-11-28 20:58:11 -0300702 break;
Antti Palosaari825b9672008-09-15 15:01:52 -0300703 case GUARD_INTERVAL_1_32:
704 break;
705 case GUARD_INTERVAL_1_16:
706 buf[0] |= (1 << 2);
707 break;
708 case GUARD_INTERVAL_1_8:
709 buf[0] |= (2 << 2);
710 break;
711 case GUARD_INTERVAL_1_4:
712 buf[0] |= (3 << 2);
713 break;
714 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300715 dev_dbg(&state->i2c->dev, "%s: invalid guard_interval\n",
716 __func__);
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300717 auto_mode = true;
Antti Palosaari825b9672008-09-15 15:01:52 -0300718 }
719
Antti Palosaarif571e002011-11-28 20:58:11 -0300720 switch (c->hierarchy) {
Antti Palosaari825b9672008-09-15 15:01:52 -0300721 case HIERARCHY_AUTO:
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300722 auto_mode = true;
Antti Palosaarif571e002011-11-28 20:58:11 -0300723 break;
Antti Palosaari825b9672008-09-15 15:01:52 -0300724 case HIERARCHY_NONE:
725 break;
726 case HIERARCHY_1:
727 buf[0] |= (1 << 4);
728 break;
729 case HIERARCHY_2:
730 buf[0] |= (2 << 4);
731 break;
732 case HIERARCHY_4:
733 buf[0] |= (3 << 4);
734 break;
735 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300736 dev_dbg(&state->i2c->dev, "%s: invalid hierarchy\n", __func__);
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300737 auto_mode = true;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300738 }
Antti Palosaari825b9672008-09-15 15:01:52 -0300739
Antti Palosaarif571e002011-11-28 20:58:11 -0300740 switch (c->modulation) {
Antti Palosaari825b9672008-09-15 15:01:52 -0300741 case QAM_AUTO:
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300742 auto_mode = true;
Antti Palosaarif571e002011-11-28 20:58:11 -0300743 break;
Antti Palosaari825b9672008-09-15 15:01:52 -0300744 case QPSK:
745 break;
746 case QAM_16:
747 buf[1] |= (1 << 6);
748 break;
749 case QAM_64:
750 buf[1] |= (2 << 6);
751 break;
752 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300753 dev_dbg(&state->i2c->dev, "%s: invalid modulation\n", __func__);
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300754 auto_mode = true;
Antti Palosaari825b9672008-09-15 15:01:52 -0300755 }
756
757 /* Use HP. How and which case we can switch to LP? */
758 buf[1] |= (1 << 4);
759
Antti Palosaarif571e002011-11-28 20:58:11 -0300760 switch (c->code_rate_HP) {
Antti Palosaari825b9672008-09-15 15:01:52 -0300761 case FEC_AUTO:
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300762 auto_mode = true;
Antti Palosaarif571e002011-11-28 20:58:11 -0300763 break;
Antti Palosaari825b9672008-09-15 15:01:52 -0300764 case FEC_1_2:
765 break;
766 case FEC_2_3:
767 buf[2] |= (1 << 0);
768 break;
769 case FEC_3_4:
770 buf[2] |= (2 << 0);
771 break;
772 case FEC_5_6:
773 buf[2] |= (3 << 0);
774 break;
775 case FEC_7_8:
776 buf[2] |= (4 << 0);
777 break;
778 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300779 dev_dbg(&state->i2c->dev, "%s: invalid code_rate_HP\n",
780 __func__);
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300781 auto_mode = true;
Antti Palosaari825b9672008-09-15 15:01:52 -0300782 }
783
Antti Palosaarif571e002011-11-28 20:58:11 -0300784 switch (c->code_rate_LP) {
Antti Palosaari825b9672008-09-15 15:01:52 -0300785 case FEC_AUTO:
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300786 auto_mode = true;
Antti Palosaarif571e002011-11-28 20:58:11 -0300787 break;
Antti Palosaari825b9672008-09-15 15:01:52 -0300788 case FEC_1_2:
789 break;
790 case FEC_2_3:
791 buf[2] |= (1 << 3);
792 break;
793 case FEC_3_4:
794 buf[2] |= (2 << 3);
795 break;
796 case FEC_5_6:
797 buf[2] |= (3 << 3);
798 break;
799 case FEC_7_8:
800 buf[2] |= (4 << 3);
801 break;
802 case FEC_NONE:
Antti Palosaarif571e002011-11-28 20:58:11 -0300803 break;
Antti Palosaari825b9672008-09-15 15:01:52 -0300804 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300805 dev_dbg(&state->i2c->dev, "%s: invalid code_rate_LP\n",
806 __func__);
Mauro Carvalho Chehab6a5e7fd2014-09-03 15:20:50 -0300807 auto_mode = true;
Antti Palosaari825b9672008-09-15 15:01:52 -0300808 }
809
Antti Palosaarif571e002011-11-28 20:58:11 -0300810 switch (c->bandwidth_hz) {
811 case 6000000:
Antti Palosaari825b9672008-09-15 15:01:52 -0300812 break;
Antti Palosaarif571e002011-11-28 20:58:11 -0300813 case 7000000:
Antti Palosaari825b9672008-09-15 15:01:52 -0300814 buf[1] |= (1 << 2);
815 break;
Antti Palosaarif571e002011-11-28 20:58:11 -0300816 case 8000000:
Antti Palosaari825b9672008-09-15 15:01:52 -0300817 buf[1] |= (2 << 2);
818 break;
819 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300820 dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n",
821 __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -0300822 ret = -EINVAL;
823 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300824 }
825
Antti Palosaarif571e002011-11-28 20:58:11 -0300826 ret = af9013_wr_regs(state, 0xd3c0, buf, 3);
Antti Palosaari825b9672008-09-15 15:01:52 -0300827 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -0300828 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300829
830 if (auto_mode) {
831 /* clear easy mode flag */
Antti Palosaarif571e002011-11-28 20:58:11 -0300832 ret = af9013_wr_reg(state, 0xaefd, 0);
833 if (ret)
834 goto err;
835
Antti Palosaari8df379c2012-09-12 20:23:43 -0300836 dev_dbg(&state->i2c->dev, "%s: auto params\n", __func__);
Antti Palosaari825b9672008-09-15 15:01:52 -0300837 } else {
838 /* set easy mode flag */
Antti Palosaarif571e002011-11-28 20:58:11 -0300839 ret = af9013_wr_reg(state, 0xaefd, 1);
Antti Palosaari825b9672008-09-15 15:01:52 -0300840 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -0300841 goto err;
842
843 ret = af9013_wr_reg(state, 0xaefe, 0);
844 if (ret)
845 goto err;
846
Antti Palosaari8df379c2012-09-12 20:23:43 -0300847 dev_dbg(&state->i2c->dev, "%s: manual params\n", __func__);
Antti Palosaari825b9672008-09-15 15:01:52 -0300848 }
Antti Palosaari825b9672008-09-15 15:01:52 -0300849
Antti Palosaarif571e002011-11-28 20:58:11 -0300850 /* tune */
851 ret = af9013_wr_reg(state, 0xffff, 0);
Antti Palosaari825b9672008-09-15 15:01:52 -0300852 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -0300853 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300854
Antti Palosaarif571e002011-11-28 20:58:11 -0300855 state->bandwidth_hz = c->bandwidth_hz;
856 state->set_frontend_jiffies = jiffies;
857 state->first_tune = false;
858
859 return ret;
860err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300861 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari825b9672008-09-15 15:01:52 -0300862 return ret;
863}
864
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200865static int af9013_get_frontend(struct dvb_frontend *fe,
866 struct dtv_frontend_properties *c)
Antti Palosaari825b9672008-09-15 15:01:52 -0300867{
868 struct af9013_state *state = fe->demodulator_priv;
869 int ret;
Antti Palosaarif571e002011-11-28 20:58:11 -0300870 u8 buf[3];
Antti Palosaari825b9672008-09-15 15:01:52 -0300871
Antti Palosaari8df379c2012-09-12 20:23:43 -0300872 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -0300873
874 ret = af9013_rd_regs(state, 0xd3c0, buf, 3);
875 if (ret)
876 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -0300877
878 switch ((buf[1] >> 6) & 3) {
879 case 0:
Antti Palosaarif571e002011-11-28 20:58:11 -0300880 c->modulation = QPSK;
Antti Palosaari825b9672008-09-15 15:01:52 -0300881 break;
882 case 1:
Antti Palosaarif571e002011-11-28 20:58:11 -0300883 c->modulation = QAM_16;
Antti Palosaari825b9672008-09-15 15:01:52 -0300884 break;
885 case 2:
Antti Palosaarif571e002011-11-28 20:58:11 -0300886 c->modulation = QAM_64;
Antti Palosaari825b9672008-09-15 15:01:52 -0300887 break;
888 }
889
890 switch ((buf[0] >> 0) & 3) {
891 case 0:
Antti Palosaarif571e002011-11-28 20:58:11 -0300892 c->transmission_mode = TRANSMISSION_MODE_2K;
Antti Palosaari825b9672008-09-15 15:01:52 -0300893 break;
894 case 1:
Antti Palosaarif571e002011-11-28 20:58:11 -0300895 c->transmission_mode = TRANSMISSION_MODE_8K;
Antti Palosaari825b9672008-09-15 15:01:52 -0300896 }
897
898 switch ((buf[0] >> 2) & 3) {
899 case 0:
Gianluca Gennari6a2329a2012-01-03 13:47:35 -0300900 c->guard_interval = GUARD_INTERVAL_1_32;
Antti Palosaari825b9672008-09-15 15:01:52 -0300901 break;
902 case 1:
Gianluca Gennari6a2329a2012-01-03 13:47:35 -0300903 c->guard_interval = GUARD_INTERVAL_1_16;
Antti Palosaari825b9672008-09-15 15:01:52 -0300904 break;
905 case 2:
Gianluca Gennari6a2329a2012-01-03 13:47:35 -0300906 c->guard_interval = GUARD_INTERVAL_1_8;
Antti Palosaari825b9672008-09-15 15:01:52 -0300907 break;
908 case 3:
Gianluca Gennari6a2329a2012-01-03 13:47:35 -0300909 c->guard_interval = GUARD_INTERVAL_1_4;
Antti Palosaari825b9672008-09-15 15:01:52 -0300910 break;
911 }
912
913 switch ((buf[0] >> 4) & 7) {
914 case 0:
Antti Palosaarif571e002011-11-28 20:58:11 -0300915 c->hierarchy = HIERARCHY_NONE;
Antti Palosaari825b9672008-09-15 15:01:52 -0300916 break;
917 case 1:
Antti Palosaarif571e002011-11-28 20:58:11 -0300918 c->hierarchy = HIERARCHY_1;
Antti Palosaari825b9672008-09-15 15:01:52 -0300919 break;
920 case 2:
Antti Palosaarif571e002011-11-28 20:58:11 -0300921 c->hierarchy = HIERARCHY_2;
Antti Palosaari825b9672008-09-15 15:01:52 -0300922 break;
923 case 3:
Antti Palosaarif571e002011-11-28 20:58:11 -0300924 c->hierarchy = HIERARCHY_4;
Antti Palosaari825b9672008-09-15 15:01:52 -0300925 break;
926 }
927
928 switch ((buf[2] >> 0) & 7) {
929 case 0:
Antti Palosaarif571e002011-11-28 20:58:11 -0300930 c->code_rate_HP = FEC_1_2;
Antti Palosaari825b9672008-09-15 15:01:52 -0300931 break;
932 case 1:
Antti Palosaarif571e002011-11-28 20:58:11 -0300933 c->code_rate_HP = FEC_2_3;
Antti Palosaari825b9672008-09-15 15:01:52 -0300934 break;
935 case 2:
Antti Palosaarif571e002011-11-28 20:58:11 -0300936 c->code_rate_HP = FEC_3_4;
Antti Palosaari825b9672008-09-15 15:01:52 -0300937 break;
938 case 3:
Antti Palosaarif571e002011-11-28 20:58:11 -0300939 c->code_rate_HP = FEC_5_6;
Antti Palosaari825b9672008-09-15 15:01:52 -0300940 break;
941 case 4:
Antti Palosaarif571e002011-11-28 20:58:11 -0300942 c->code_rate_HP = FEC_7_8;
Antti Palosaari825b9672008-09-15 15:01:52 -0300943 break;
944 }
945
946 switch ((buf[2] >> 3) & 7) {
947 case 0:
Antti Palosaarif571e002011-11-28 20:58:11 -0300948 c->code_rate_LP = FEC_1_2;
Antti Palosaari825b9672008-09-15 15:01:52 -0300949 break;
950 case 1:
Antti Palosaarif571e002011-11-28 20:58:11 -0300951 c->code_rate_LP = FEC_2_3;
Antti Palosaari825b9672008-09-15 15:01:52 -0300952 break;
953 case 2:
Antti Palosaarif571e002011-11-28 20:58:11 -0300954 c->code_rate_LP = FEC_3_4;
Antti Palosaari825b9672008-09-15 15:01:52 -0300955 break;
956 case 3:
Antti Palosaarif571e002011-11-28 20:58:11 -0300957 c->code_rate_LP = FEC_5_6;
Antti Palosaari825b9672008-09-15 15:01:52 -0300958 break;
959 case 4:
Antti Palosaarif571e002011-11-28 20:58:11 -0300960 c->code_rate_LP = FEC_7_8;
Antti Palosaari825b9672008-09-15 15:01:52 -0300961 break;
962 }
963
964 switch ((buf[1] >> 2) & 3) {
965 case 0:
Antti Palosaarif571e002011-11-28 20:58:11 -0300966 c->bandwidth_hz = 6000000;
Antti Palosaari825b9672008-09-15 15:01:52 -0300967 break;
968 case 1:
Antti Palosaarif571e002011-11-28 20:58:11 -0300969 c->bandwidth_hz = 7000000;
Antti Palosaari825b9672008-09-15 15:01:52 -0300970 break;
971 case 2:
Antti Palosaarif571e002011-11-28 20:58:11 -0300972 c->bandwidth_hz = 8000000;
Antti Palosaari825b9672008-09-15 15:01:52 -0300973 break;
974 }
975
Antti Palosaari825b9672008-09-15 15:01:52 -0300976 return ret;
Antti Palosaarif571e002011-11-28 20:58:11 -0300977err:
Antti Palosaari8df379c2012-09-12 20:23:43 -0300978 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari825b9672008-09-15 15:01:52 -0300979 return ret;
980}
981
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300982static int af9013_read_status(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari825b9672008-09-15 15:01:52 -0300983{
984 struct af9013_state *state = fe->demodulator_priv;
Antti Palosaarif571e002011-11-28 20:58:11 -0300985 int ret;
Antti Palosaari825b9672008-09-15 15:01:52 -0300986 u8 tmp;
Antti Palosaarif571e002011-11-28 20:58:11 -0300987
988 /*
989 * Return status from the cache if it is younger than 2000ms with the
990 * exception of last tune is done during 4000ms.
991 */
992 if (time_is_after_jiffies(
993 state->read_status_jiffies + msecs_to_jiffies(2000)) &&
994 time_is_before_jiffies(
995 state->set_frontend_jiffies + msecs_to_jiffies(4000))
996 ) {
997 *status = state->fe_status;
998 return 0;
999 } else {
1000 *status = 0;
1001 }
Antti Palosaari825b9672008-09-15 15:01:52 -03001002
Antti Palosaari825b9672008-09-15 15:01:52 -03001003 /* MPEG2 lock */
Antti Palosaarif571e002011-11-28 20:58:11 -03001004 ret = af9013_rd_reg_bits(state, 0xd507, 6, 1, &tmp);
Antti Palosaari825b9672008-09-15 15:01:52 -03001005 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001006 goto err;
1007
Antti Palosaari825b9672008-09-15 15:01:52 -03001008 if (tmp)
Antti Palosaari8af5e382010-06-17 20:56:27 -03001009 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
1010 FE_HAS_SYNC | FE_HAS_LOCK;
Antti Palosaari825b9672008-09-15 15:01:52 -03001011
Antti Palosaari8af5e382010-06-17 20:56:27 -03001012 if (!*status) {
1013 /* TPS lock */
Antti Palosaarif571e002011-11-28 20:58:11 -03001014 ret = af9013_rd_reg_bits(state, 0xd330, 3, 1, &tmp);
Antti Palosaari8af5e382010-06-17 20:56:27 -03001015 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001016 goto err;
1017
Antti Palosaari8af5e382010-06-17 20:56:27 -03001018 if (tmp)
1019 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
1020 FE_HAS_VITERBI;
1021 }
1022
Antti Palosaarif571e002011-11-28 20:58:11 -03001023 state->fe_status = *status;
1024 state->read_status_jiffies = jiffies;
Antti Palosaari8af5e382010-06-17 20:56:27 -03001025
Antti Palosaari825b9672008-09-15 15:01:52 -03001026 return ret;
Antti Palosaarif571e002011-11-28 20:58:11 -03001027err:
Antti Palosaari8df379c2012-09-12 20:23:43 -03001028 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari825b9672008-09-15 15:01:52 -03001029 return ret;
1030}
1031
1032static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr)
1033{
1034 struct af9013_state *state = fe->demodulator_priv;
Antti Palosaari825b9672008-09-15 15:01:52 -03001035 *snr = state->snr;
Antti Palosaarif571e002011-11-28 20:58:11 -03001036 return 0;
1037}
1038
1039static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
1040{
1041 struct af9013_state *state = fe->demodulator_priv;
1042 *strength = state->signal_strength;
1043 return 0;
1044}
1045
1046static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
1047{
1048 struct af9013_state *state = fe->demodulator_priv;
1049 *ber = state->ber;
1050 return 0;
Antti Palosaari825b9672008-09-15 15:01:52 -03001051}
1052
1053static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1054{
1055 struct af9013_state *state = fe->demodulator_priv;
Antti Palosaari825b9672008-09-15 15:01:52 -03001056 *ucblocks = state->ucblocks;
Antti Palosaarif571e002011-11-28 20:58:11 -03001057 return 0;
Antti Palosaari825b9672008-09-15 15:01:52 -03001058}
1059
1060static int af9013_init(struct dvb_frontend *fe)
1061{
1062 struct af9013_state *state = fe->demodulator_priv;
1063 int ret, i, len;
Antti Palosaarif571e002011-11-28 20:58:11 -03001064 u8 buf[3], tmp;
1065 u32 adc_cw;
1066 const struct af9013_reg_bit *init;
Antti Palosaari825b9672008-09-15 15:01:52 -03001067
Antti Palosaari8df379c2012-09-12 20:23:43 -03001068 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Antti Palosaari825b9672008-09-15 15:01:52 -03001069
1070 /* power on */
1071 ret = af9013_power_ctrl(state, 1);
1072 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001073 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001074
1075 /* enable ADC */
Antti Palosaarif571e002011-11-28 20:58:11 -03001076 ret = af9013_wr_reg(state, 0xd73a, 0xa4);
Antti Palosaari825b9672008-09-15 15:01:52 -03001077 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001078 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001079
1080 /* write API version to firmware */
Antti Palosaarif571e002011-11-28 20:58:11 -03001081 ret = af9013_wr_regs(state, 0x9bf2, state->config.api_version, 4);
1082 if (ret)
1083 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001084
1085 /* program ADC control */
Antti Palosaarif571e002011-11-28 20:58:11 -03001086 switch (state->config.clock) {
1087 case 28800000: /* 28.800 MHz */
1088 tmp = 0;
1089 break;
1090 case 20480000: /* 20.480 MHz */
1091 tmp = 1;
1092 break;
1093 case 28000000: /* 28.000 MHz */
1094 tmp = 2;
1095 break;
1096 case 25000000: /* 25.000 MHz */
1097 tmp = 3;
1098 break;
1099 default:
Antti Palosaari8df379c2012-09-12 20:23:43 -03001100 dev_err(&state->i2c->dev, "%s: invalid clock\n",
1101 KBUILD_MODNAME);
Antti Palosaarif571e002011-11-28 20:58:11 -03001102 return -EINVAL;
1103 }
1104
Antti Palosaari8df379c2012-09-12 20:23:43 -03001105 adc_cw = af9013_div(state, state->config.clock, 1000000ul, 19);
Antti Palosaarif571e002011-11-28 20:58:11 -03001106 buf[0] = (adc_cw >> 0) & 0xff;
1107 buf[1] = (adc_cw >> 8) & 0xff;
1108 buf[2] = (adc_cw >> 16) & 0xff;
1109
1110 ret = af9013_wr_regs(state, 0xd180, buf, 3);
Antti Palosaari825b9672008-09-15 15:01:52 -03001111 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001112 goto err;
1113
1114 ret = af9013_wr_reg_bits(state, 0x9bd2, 0, 4, tmp);
1115 if (ret)
1116 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001117
1118 /* set I2C master clock */
Antti Palosaarif571e002011-11-28 20:58:11 -03001119 ret = af9013_wr_reg(state, 0xd416, 0x14);
Antti Palosaari825b9672008-09-15 15:01:52 -03001120 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001121 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001122
1123 /* set 16 embx */
Antti Palosaarif571e002011-11-28 20:58:11 -03001124 ret = af9013_wr_reg_bits(state, 0xd700, 1, 1, 1);
Antti Palosaari825b9672008-09-15 15:01:52 -03001125 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001126 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001127
1128 /* set no trigger */
Antti Palosaarif571e002011-11-28 20:58:11 -03001129 ret = af9013_wr_reg_bits(state, 0xd700, 2, 1, 0);
Antti Palosaari825b9672008-09-15 15:01:52 -03001130 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001131 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001132
1133 /* set read-update bit for constellation */
Antti Palosaarif571e002011-11-28 20:58:11 -03001134 ret = af9013_wr_reg_bits(state, 0xd371, 1, 1, 1);
Antti Palosaari825b9672008-09-15 15:01:52 -03001135 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001136 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001137
Antti Palosaarif571e002011-11-28 20:58:11 -03001138 /* settings for mp2if */
1139 if (state->config.ts_mode == AF9013_TS_USB) {
1140 /* AF9015 split PSB to 1.5k + 0.5k */
1141 ret = af9013_wr_reg_bits(state, 0xd50b, 2, 1, 1);
1142 if (ret)
1143 goto err;
1144 } else {
1145 /* AF9013 change the output bit to data7 */
1146 ret = af9013_wr_reg_bits(state, 0xd500, 3, 1, 1);
1147 if (ret)
1148 goto err;
1149
1150 /* AF9013 set mpeg to full speed */
1151 ret = af9013_wr_reg_bits(state, 0xd502, 4, 1, 1);
1152 if (ret)
1153 goto err;
1154 }
1155
1156 ret = af9013_wr_reg_bits(state, 0xd520, 4, 1, 1);
Antti Palosaari825b9672008-09-15 15:01:52 -03001157 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001158 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001159
1160 /* load OFSM settings */
Antti Palosaari8df379c2012-09-12 20:23:43 -03001161 dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__);
Antti Palosaari825b9672008-09-15 15:01:52 -03001162 len = ARRAY_SIZE(ofsm_init);
1163 init = ofsm_init;
1164 for (i = 0; i < len; i++) {
Antti Palosaarif571e002011-11-28 20:58:11 -03001165 ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
Antti Palosaari825b9672008-09-15 15:01:52 -03001166 init[i].len, init[i].val);
1167 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001168 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001169 }
1170
1171 /* load tuner specific settings */
Antti Palosaari8df379c2012-09-12 20:23:43 -03001172 dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n",
1173 __func__);
Antti Palosaari825b9672008-09-15 15:01:52 -03001174 switch (state->config.tuner) {
1175 case AF9013_TUNER_MXL5003D:
1176 len = ARRAY_SIZE(tuner_init_mxl5003d);
1177 init = tuner_init_mxl5003d;
1178 break;
1179 case AF9013_TUNER_MXL5005D:
1180 case AF9013_TUNER_MXL5005R:
Antti Palosaaria4f31d02010-09-09 14:53:59 -03001181 case AF9013_TUNER_MXL5007T:
Antti Palosaari825b9672008-09-15 15:01:52 -03001182 len = ARRAY_SIZE(tuner_init_mxl5005);
1183 init = tuner_init_mxl5005;
1184 break;
1185 case AF9013_TUNER_ENV77H11D5:
1186 len = ARRAY_SIZE(tuner_init_env77h11d5);
1187 init = tuner_init_env77h11d5;
1188 break;
1189 case AF9013_TUNER_MT2060:
1190 len = ARRAY_SIZE(tuner_init_mt2060);
1191 init = tuner_init_mt2060;
1192 break;
1193 case AF9013_TUNER_MC44S803:
1194 len = ARRAY_SIZE(tuner_init_mc44s803);
1195 init = tuner_init_mc44s803;
1196 break;
1197 case AF9013_TUNER_QT1010:
1198 case AF9013_TUNER_QT1010A:
1199 len = ARRAY_SIZE(tuner_init_qt1010);
1200 init = tuner_init_qt1010;
1201 break;
1202 case AF9013_TUNER_MT2060_2:
1203 len = ARRAY_SIZE(tuner_init_mt2060_2);
1204 init = tuner_init_mt2060_2;
1205 break;
1206 case AF9013_TUNER_TDA18271:
Antti Palosaari2158e502010-08-13 03:49:24 -03001207 case AF9013_TUNER_TDA18218:
Antti Palosaari825b9672008-09-15 15:01:52 -03001208 len = ARRAY_SIZE(tuner_init_tda18271);
1209 init = tuner_init_tda18271;
1210 break;
1211 case AF9013_TUNER_UNKNOWN:
1212 default:
1213 len = ARRAY_SIZE(tuner_init_unknown);
1214 init = tuner_init_unknown;
1215 break;
1216 }
1217
1218 for (i = 0; i < len; i++) {
Antti Palosaarif571e002011-11-28 20:58:11 -03001219 ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
Antti Palosaari825b9672008-09-15 15:01:52 -03001220 init[i].len, init[i].val);
1221 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001222 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001223 }
1224
Antti Palosaarif571e002011-11-28 20:58:11 -03001225 /* TS mode */
1226 ret = af9013_wr_reg_bits(state, 0xd500, 1, 2, state->config.ts_mode);
Antti Palosaari825b9672008-09-15 15:01:52 -03001227 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001228 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001229
1230 /* enable lock led */
Antti Palosaarif571e002011-11-28 20:58:11 -03001231 ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 1);
Antti Palosaari825b9672008-09-15 15:01:52 -03001232 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001233 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001234
Antti Palosaarif571e002011-11-28 20:58:11 -03001235 /* check if we support signal strength */
1236 if (!state->signal_strength_en) {
1237 ret = af9013_rd_reg_bits(state, 0x9bee, 0, 1,
1238 &state->signal_strength_en);
Antti Palosaari9e35cd22010-10-07 16:34:55 -03001239 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001240 goto err;
Antti Palosaari9e35cd22010-10-07 16:34:55 -03001241 }
1242
Antti Palosaarif571e002011-11-28 20:58:11 -03001243 /* read values needed for signal strength calculation */
1244 if (state->signal_strength_en && !state->rf_50) {
1245 ret = af9013_rd_reg(state, 0x9bbd, &state->rf_50);
1246 if (ret)
1247 goto err;
1248
1249 ret = af9013_rd_reg(state, 0x9bd0, &state->rf_80);
1250 if (ret)
1251 goto err;
1252
1253 ret = af9013_rd_reg(state, 0x9be2, &state->if_50);
1254 if (ret)
1255 goto err;
1256
1257 ret = af9013_rd_reg(state, 0x9be4, &state->if_80);
1258 if (ret)
1259 goto err;
1260 }
1261
1262 /* SNR */
1263 ret = af9013_wr_reg(state, 0xd2e2, 1);
1264 if (ret)
1265 goto err;
1266
1267 /* BER / UCB */
1268 buf[0] = (10000 >> 0) & 0xff;
1269 buf[1] = (10000 >> 8) & 0xff;
1270 ret = af9013_wr_regs(state, 0xd385, buf, 2);
1271 if (ret)
1272 goto err;
1273
1274 /* enable FEC monitor */
1275 ret = af9013_wr_reg_bits(state, 0xd392, 1, 1, 1);
1276 if (ret)
1277 goto err;
1278
1279 state->first_tune = true;
1280 schedule_delayed_work(&state->statistics_work, msecs_to_jiffies(400));
1281
Antti Palosaari825b9672008-09-15 15:01:52 -03001282 return ret;
Antti Palosaarif571e002011-11-28 20:58:11 -03001283err:
Antti Palosaari8df379c2012-09-12 20:23:43 -03001284 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -03001285 return ret;
1286}
1287
1288static int af9013_sleep(struct dvb_frontend *fe)
1289{
1290 struct af9013_state *state = fe->demodulator_priv;
1291 int ret;
1292
Antti Palosaari8df379c2012-09-12 20:23:43 -03001293 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
Antti Palosaarif571e002011-11-28 20:58:11 -03001294
1295 /* stop statistics polling */
1296 cancel_delayed_work_sync(&state->statistics_work);
1297
1298 /* disable lock led */
1299 ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 0);
1300 if (ret)
1301 goto err;
1302
1303 /* power off */
1304 ret = af9013_power_ctrl(state, 0);
1305 if (ret)
1306 goto err;
1307
1308 return ret;
1309err:
Antti Palosaari8df379c2012-09-12 20:23:43 -03001310 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -03001311 return ret;
1312}
1313
1314static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1315{
1316 int ret;
1317 struct af9013_state *state = fe->demodulator_priv;
1318
Antti Palosaari8df379c2012-09-12 20:23:43 -03001319 dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable);
Antti Palosaarif571e002011-11-28 20:58:11 -03001320
1321 /* gate already open or close */
1322 if (state->i2c_gate_state == enable)
1323 return 0;
1324
1325 if (state->config.ts_mode == AF9013_TS_USB)
1326 ret = af9013_wr_reg_bits(state, 0xd417, 3, 1, enable);
1327 else
1328 ret = af9013_wr_reg_bits(state, 0xd607, 2, 1, enable);
1329 if (ret)
1330 goto err;
1331
1332 state->i2c_gate_state = enable;
1333
1334 return ret;
1335err:
Antti Palosaari8df379c2012-09-12 20:23:43 -03001336 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -03001337 return ret;
1338}
1339
1340static void af9013_release(struct dvb_frontend *fe)
1341{
1342 struct af9013_state *state = fe->demodulator_priv;
Ernst Martin Witte631c6942016-01-09 18:18:45 -02001343
1344 /* stop statistics polling */
1345 cancel_delayed_work_sync(&state->statistics_work);
1346
Antti Palosaarif571e002011-11-28 20:58:11 -03001347 kfree(state);
Antti Palosaari825b9672008-09-15 15:01:52 -03001348}
1349
Max Kellermannbd336e62016-08-09 18:32:21 -03001350static const struct dvb_frontend_ops af9013_ops;
Antti Palosaari825b9672008-09-15 15:01:52 -03001351
1352static int af9013_download_firmware(struct af9013_state *state)
1353{
Antti Palosaari6ed9d562011-03-19 18:02:22 -03001354 int i, len, remaining, ret;
Antti Palosaari825b9672008-09-15 15:01:52 -03001355 const struct firmware *fw;
Antti Palosaari825b9672008-09-15 15:01:52 -03001356 u16 checksum = 0;
1357 u8 val;
1358 u8 fw_params[4];
Antti Palosaaria71103a2012-09-12 11:37:28 -03001359 u8 *fw_file = AF9013_FIRMWARE;
Antti Palosaari825b9672008-09-15 15:01:52 -03001360
1361 msleep(100);
1362 /* check whether firmware is already running */
Antti Palosaarif571e002011-11-28 20:58:11 -03001363 ret = af9013_rd_reg(state, 0x98be, &val);
Antti Palosaari825b9672008-09-15 15:01:52 -03001364 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001365 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001366 else
Antti Palosaari8df379c2012-09-12 20:23:43 -03001367 dev_dbg(&state->i2c->dev, "%s: firmware status=%02x\n",
1368 __func__, val);
Antti Palosaari825b9672008-09-15 15:01:52 -03001369
1370 if (val == 0x0c) /* fw is running, no need for download */
1371 goto exit;
1372
Antti Palosaari8df379c2012-09-12 20:23:43 -03001373 dev_info(&state->i2c->dev, "%s: found a '%s' in cold state, will try " \
1374 "to load a firmware\n",
1375 KBUILD_MODNAME, af9013_ops.info.name);
Antti Palosaari825b9672008-09-15 15:01:52 -03001376
1377 /* request the firmware, this will block and timeout */
Jean Delvaree9785252009-04-26 05:43:59 -03001378 ret = request_firmware(&fw, fw_file, state->i2c->dev.parent);
Antti Palosaari825b9672008-09-15 15:01:52 -03001379 if (ret) {
Antti Palosaari8df379c2012-09-12 20:23:43 -03001380 dev_info(&state->i2c->dev, "%s: did not find the firmware " \
1381 "file. (%s) Please see linux/Documentation/dvb/ for " \
1382 "more details on firmware-problems. (%d)\n",
1383 KBUILD_MODNAME, fw_file, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -03001384 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001385 }
1386
Antti Palosaari8df379c2012-09-12 20:23:43 -03001387 dev_info(&state->i2c->dev, "%s: downloading firmware from file '%s'\n",
1388 KBUILD_MODNAME, fw_file);
Antti Palosaari825b9672008-09-15 15:01:52 -03001389
1390 /* calc checksum */
1391 for (i = 0; i < fw->size; i++)
1392 checksum += fw->data[i];
1393
1394 fw_params[0] = checksum >> 8;
1395 fw_params[1] = checksum & 0xff;
1396 fw_params[2] = fw->size >> 8;
1397 fw_params[3] = fw->size & 0xff;
1398
1399 /* write fw checksum & size */
1400 ret = af9013_write_ofsm_regs(state, 0x50fc,
1401 fw_params, sizeof(fw_params));
1402 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001403 goto err_release;
Antti Palosaari825b9672008-09-15 15:01:52 -03001404
Antti Palosaari6ed9d562011-03-19 18:02:22 -03001405 #define FW_ADDR 0x5100 /* firmware start address */
1406 #define LEN_MAX 16 /* max packet size */
1407 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
1408 len = remaining;
1409 if (len > LEN_MAX)
1410 len = LEN_MAX;
Antti Palosaari825b9672008-09-15 15:01:52 -03001411
Antti Palosaari6ed9d562011-03-19 18:02:22 -03001412 ret = af9013_write_ofsm_regs(state,
1413 FW_ADDR + fw->size - remaining,
1414 (u8 *) &fw->data[fw->size - remaining], len);
Antti Palosaari825b9672008-09-15 15:01:52 -03001415 if (ret) {
Antti Palosaari8df379c2012-09-12 20:23:43 -03001416 dev_err(&state->i2c->dev,
1417 "%s: firmware download failed=%d\n",
1418 KBUILD_MODNAME, ret);
Antti Palosaarif571e002011-11-28 20:58:11 -03001419 goto err_release;
Antti Palosaari825b9672008-09-15 15:01:52 -03001420 }
1421 }
1422
Antti Palosaari825b9672008-09-15 15:01:52 -03001423 /* request boot firmware */
Antti Palosaarif571e002011-11-28 20:58:11 -03001424 ret = af9013_wr_reg(state, 0xe205, 1);
Antti Palosaari825b9672008-09-15 15:01:52 -03001425 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001426 goto err_release;
Antti Palosaari825b9672008-09-15 15:01:52 -03001427
1428 for (i = 0; i < 15; i++) {
1429 msleep(100);
1430
1431 /* check firmware status */
Antti Palosaarif571e002011-11-28 20:58:11 -03001432 ret = af9013_rd_reg(state, 0x98be, &val);
Antti Palosaari825b9672008-09-15 15:01:52 -03001433 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001434 goto err_release;
Antti Palosaari825b9672008-09-15 15:01:52 -03001435
Antti Palosaari8df379c2012-09-12 20:23:43 -03001436 dev_dbg(&state->i2c->dev, "%s: firmware status=%02x\n",
1437 __func__, val);
Antti Palosaari825b9672008-09-15 15:01:52 -03001438
1439 if (val == 0x0c || val == 0x04) /* success or fail */
1440 break;
1441 }
1442
1443 if (val == 0x04) {
Antti Palosaari8df379c2012-09-12 20:23:43 -03001444 dev_err(&state->i2c->dev, "%s: firmware did not run\n",
1445 KBUILD_MODNAME);
Antti Palosaarif571e002011-11-28 20:58:11 -03001446 ret = -ENODEV;
Antti Palosaari825b9672008-09-15 15:01:52 -03001447 } else if (val != 0x0c) {
Antti Palosaari8df379c2012-09-12 20:23:43 -03001448 dev_err(&state->i2c->dev, "%s: firmware boot timeout\n",
1449 KBUILD_MODNAME);
Antti Palosaarif571e002011-11-28 20:58:11 -03001450 ret = -ENODEV;
Antti Palosaari825b9672008-09-15 15:01:52 -03001451 }
1452
Antti Palosaarif571e002011-11-28 20:58:11 -03001453err_release:
Antti Palosaari825b9672008-09-15 15:01:52 -03001454 release_firmware(fw);
Antti Palosaarif571e002011-11-28 20:58:11 -03001455err:
Antti Palosaari825b9672008-09-15 15:01:52 -03001456exit:
1457 if (!ret)
Antti Palosaari8df379c2012-09-12 20:23:43 -03001458 dev_info(&state->i2c->dev, "%s: found a '%s' in warm state\n",
1459 KBUILD_MODNAME, af9013_ops.info.name);
Antti Palosaari825b9672008-09-15 15:01:52 -03001460 return ret;
1461}
1462
Antti Palosaari825b9672008-09-15 15:01:52 -03001463struct dvb_frontend *af9013_attach(const struct af9013_config *config,
1464 struct i2c_adapter *i2c)
1465{
1466 int ret;
1467 struct af9013_state *state = NULL;
Antti Palosaarice99efa2010-06-17 21:16:12 -03001468 u8 buf[4], i;
Antti Palosaari825b9672008-09-15 15:01:52 -03001469
1470 /* allocate memory for the internal state */
1471 state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL);
1472 if (state == NULL)
Antti Palosaarif571e002011-11-28 20:58:11 -03001473 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001474
1475 /* setup the state */
1476 state->i2c = i2c;
1477 memcpy(&state->config, config, sizeof(struct af9013_config));
1478
Antti Palosaari825b9672008-09-15 15:01:52 -03001479 /* download firmware */
Antti Palosaarif571e002011-11-28 20:58:11 -03001480 if (state->config.ts_mode != AF9013_TS_USB) {
Antti Palosaari825b9672008-09-15 15:01:52 -03001481 ret = af9013_download_firmware(state);
1482 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001483 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001484 }
1485
1486 /* firmware version */
Antti Palosaarif571e002011-11-28 20:58:11 -03001487 ret = af9013_rd_regs(state, 0x5103, buf, 4);
Antti Palosaari109a2992011-03-19 17:18:18 -03001488 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001489 goto err;
Antti Palosaari109a2992011-03-19 17:18:18 -03001490
Antti Palosaari8df379c2012-09-12 20:23:43 -03001491 dev_info(&state->i2c->dev, "%s: firmware version %d.%d.%d.%d\n",
1492 KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3]);
Antti Palosaari825b9672008-09-15 15:01:52 -03001493
1494 /* set GPIOs */
1495 for (i = 0; i < sizeof(state->config.gpio); i++) {
1496 ret = af9013_set_gpio(state, i, state->config.gpio[i]);
1497 if (ret)
Antti Palosaarif571e002011-11-28 20:58:11 -03001498 goto err;
Antti Palosaari825b9672008-09-15 15:01:52 -03001499 }
1500
1501 /* create dvb_frontend */
Antti Palosaarif571e002011-11-28 20:58:11 -03001502 memcpy(&state->fe.ops, &af9013_ops,
Antti Palosaari825b9672008-09-15 15:01:52 -03001503 sizeof(struct dvb_frontend_ops));
Antti Palosaarif571e002011-11-28 20:58:11 -03001504 state->fe.demodulator_priv = state;
Antti Palosaari825b9672008-09-15 15:01:52 -03001505
Antti Palosaarif571e002011-11-28 20:58:11 -03001506 INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work);
1507
1508 return &state->fe;
1509err:
Antti Palosaari825b9672008-09-15 15:01:52 -03001510 kfree(state);
1511 return NULL;
1512}
1513EXPORT_SYMBOL(af9013_attach);
1514
Max Kellermannbd336e62016-08-09 18:32:21 -03001515static const struct dvb_frontend_ops af9013_ops = {
Mauro Carvalho Chehab59d3cc12011-12-30 22:25:27 -03001516 .delsys = { SYS_DVBT },
Antti Palosaari825b9672008-09-15 15:01:52 -03001517 .info = {
Antti Palosaarif571e002011-11-28 20:58:11 -03001518 .name = "Afatech AF9013",
Antti Palosaari825b9672008-09-15 15:01:52 -03001519 .frequency_min = 174000000,
1520 .frequency_max = 862000000,
1521 .frequency_stepsize = 250000,
1522 .frequency_tolerance = 0,
Antti Palosaarif571e002011-11-28 20:58:11 -03001523 .caps = FE_CAN_FEC_1_2 |
1524 FE_CAN_FEC_2_3 |
1525 FE_CAN_FEC_3_4 |
1526 FE_CAN_FEC_5_6 |
1527 FE_CAN_FEC_7_8 |
1528 FE_CAN_FEC_AUTO |
1529 FE_CAN_QPSK |
1530 FE_CAN_QAM_16 |
1531 FE_CAN_QAM_64 |
1532 FE_CAN_QAM_AUTO |
Antti Palosaari825b9672008-09-15 15:01:52 -03001533 FE_CAN_TRANSMISSION_MODE_AUTO |
1534 FE_CAN_GUARD_INTERVAL_AUTO |
1535 FE_CAN_HIERARCHY_AUTO |
1536 FE_CAN_RECOVER |
1537 FE_CAN_MUTE_TS
1538 },
1539
1540 .release = af9013_release,
Antti Palosaarif571e002011-11-28 20:58:11 -03001541
Antti Palosaari825b9672008-09-15 15:01:52 -03001542 .init = af9013_init,
1543 .sleep = af9013_sleep,
Antti Palosaari825b9672008-09-15 15:01:52 -03001544
Antti Palosaarif571e002011-11-28 20:58:11 -03001545 .get_tune_settings = af9013_get_tune_settings,
Mauro Carvalho Chehab59d3cc12011-12-30 22:25:27 -03001546 .set_frontend = af9013_set_frontend,
1547 .get_frontend = af9013_get_frontend,
Antti Palosaari825b9672008-09-15 15:01:52 -03001548
Antti Palosaari825b9672008-09-15 15:01:52 -03001549 .read_status = af9013_read_status,
Antti Palosaari825b9672008-09-15 15:01:52 -03001550 .read_snr = af9013_read_snr,
Antti Palosaarif571e002011-11-28 20:58:11 -03001551 .read_signal_strength = af9013_read_signal_strength,
1552 .read_ber = af9013_read_ber,
Antti Palosaari825b9672008-09-15 15:01:52 -03001553 .read_ucblocks = af9013_read_ucblocks,
Antti Palosaari825b9672008-09-15 15:01:52 -03001554
Antti Palosaarif571e002011-11-28 20:58:11 -03001555 .i2c_gate_ctrl = af9013_i2c_gate_ctrl,
1556};
Antti Palosaari825b9672008-09-15 15:01:52 -03001557
1558MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1559MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1560MODULE_LICENSE("GPL");
Antti Palosaaria71103a2012-09-12 11:37:28 -03001561MODULE_FIRMWARE(AF9013_FIRMWARE);