blob: 13c823a434fc404aaae8e7cdb8c4a0fc344288e0 [file] [log] [blame]
Antti Palosaaride8e4202011-08-01 01:07:39 -03001/*
2 * NXP TDA10071 + Conexant CX24118A DVB-S/S2 demodulator + tuner driver
3 *
4 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "tda10071_priv.h"
22
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 Palosaaride8e4202011-08-01 01:07:39 -030026static struct dvb_frontend_ops tda10071_ops;
27
28/* write multiple registers */
29static int tda10071_wr_regs(struct tda10071_priv *priv, u8 reg, u8 *val,
30 int len)
31{
32 int ret;
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030033 u8 buf[MAX_XFER_SIZE];
Antti Palosaaride8e4202011-08-01 01:07:39 -030034 struct i2c_msg msg[1] = {
35 {
Michael Krufky41f55d52012-12-16 19:37:11 -030036 .addr = priv->cfg.demod_i2c_addr,
Antti Palosaaride8e4202011-08-01 01:07:39 -030037 .flags = 0,
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030038 .len = 1 + len,
Antti Palosaaride8e4202011-08-01 01:07:39 -030039 .buf = buf,
40 }
41 };
42
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030043 if (1 + len > sizeof(buf)) {
44 dev_warn(&priv->i2c->dev,
45 "%s: i2c wr reg=%04x: len=%d is too big!\n",
46 KBUILD_MODNAME, reg, len);
47 return -EINVAL;
48 }
49
Antti Palosaaride8e4202011-08-01 01:07:39 -030050 buf[0] = reg;
51 memcpy(&buf[1], val, len);
52
53 ret = i2c_transfer(priv->i2c, msg, 1);
54 if (ret == 1) {
55 ret = 0;
56 } else {
Antti Palosaari279bba02012-07-02 03:46:46 -030057 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x " \
58 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
Antti Palosaaride8e4202011-08-01 01:07:39 -030059 ret = -EREMOTEIO;
60 }
61 return ret;
62}
63
64/* read multiple registers */
65static int tda10071_rd_regs(struct tda10071_priv *priv, u8 reg, u8 *val,
66 int len)
67{
68 int ret;
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030069 u8 buf[MAX_XFER_SIZE];
Antti Palosaaride8e4202011-08-01 01:07:39 -030070 struct i2c_msg msg[2] = {
71 {
Michael Krufky41f55d52012-12-16 19:37:11 -030072 .addr = priv->cfg.demod_i2c_addr,
Antti Palosaaride8e4202011-08-01 01:07:39 -030073 .flags = 0,
74 .len = 1,
75 .buf = &reg,
76 }, {
Michael Krufky41f55d52012-12-16 19:37:11 -030077 .addr = priv->cfg.demod_i2c_addr,
Antti Palosaaride8e4202011-08-01 01:07:39 -030078 .flags = I2C_M_RD,
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030079 .len = len,
Antti Palosaaride8e4202011-08-01 01:07:39 -030080 .buf = buf,
81 }
82 };
83
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030084 if (len > sizeof(buf)) {
85 dev_warn(&priv->i2c->dev,
86 "%s: i2c wr reg=%04x: len=%d is too big!\n",
87 KBUILD_MODNAME, reg, len);
88 return -EINVAL;
89 }
90
Antti Palosaaride8e4202011-08-01 01:07:39 -030091 ret = i2c_transfer(priv->i2c, msg, 2);
92 if (ret == 2) {
93 memcpy(val, buf, len);
94 ret = 0;
95 } else {
Antti Palosaari279bba02012-07-02 03:46:46 -030096 dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%02x " \
97 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
Antti Palosaaride8e4202011-08-01 01:07:39 -030098 ret = -EREMOTEIO;
99 }
100 return ret;
101}
102
103/* write single register */
104static int tda10071_wr_reg(struct tda10071_priv *priv, u8 reg, u8 val)
105{
106 return tda10071_wr_regs(priv, reg, &val, 1);
107}
108
109/* read single register */
110static int tda10071_rd_reg(struct tda10071_priv *priv, u8 reg, u8 *val)
111{
112 return tda10071_rd_regs(priv, reg, val, 1);
113}
114
115/* write single register with mask */
Mauro Carvalho Chehabbabb6182012-10-27 11:27:24 -0300116static int tda10071_wr_reg_mask(struct tda10071_priv *priv,
117 u8 reg, u8 val, u8 mask)
Antti Palosaaride8e4202011-08-01 01:07:39 -0300118{
119 int ret;
120 u8 tmp;
121
122 /* no need for read if whole reg is written */
123 if (mask != 0xff) {
124 ret = tda10071_rd_regs(priv, reg, &tmp, 1);
125 if (ret)
126 return ret;
127
128 val &= mask;
129 tmp &= ~mask;
130 val |= tmp;
131 }
132
133 return tda10071_wr_regs(priv, reg, &val, 1);
134}
135
136/* read single register with mask */
Mauro Carvalho Chehabbabb6182012-10-27 11:27:24 -0300137static int tda10071_rd_reg_mask(struct tda10071_priv *priv,
138 u8 reg, u8 *val, u8 mask)
Antti Palosaaride8e4202011-08-01 01:07:39 -0300139{
140 int ret, i;
141 u8 tmp;
142
143 ret = tda10071_rd_regs(priv, 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
159/* execute firmware command */
160static int tda10071_cmd_execute(struct tda10071_priv *priv,
161 struct tda10071_cmd *cmd)
162{
163 int ret, i;
164 u8 tmp;
165
166 if (!priv->warm) {
167 ret = -EFAULT;
168 goto error;
169 }
170
171 /* write cmd and args for firmware */
172 ret = tda10071_wr_regs(priv, 0x00, cmd->args, cmd->len);
173 if (ret)
174 goto error;
175
176 /* start cmd execution */
177 ret = tda10071_wr_reg(priv, 0x1f, 1);
178 if (ret)
179 goto error;
180
181 /* wait cmd execution terminate */
182 for (i = 1000, tmp = 1; i && tmp; i--) {
183 ret = tda10071_rd_reg(priv, 0x1f, &tmp);
184 if (ret)
185 goto error;
186
Antti Palosaarid102b732011-09-13 11:51:23 -0300187 usleep_range(200, 5000);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300188 }
189
Antti Palosaari279bba02012-07-02 03:46:46 -0300190 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300191
192 if (i == 0) {
193 ret = -ETIMEDOUT;
194 goto error;
195 }
196
197 return ret;
198error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300199 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300200 return ret;
201}
202
203static int tda10071_set_tone(struct dvb_frontend *fe,
204 fe_sec_tone_mode_t fe_sec_tone_mode)
205{
206 struct tda10071_priv *priv = fe->demodulator_priv;
207 struct tda10071_cmd cmd;
208 int ret;
209 u8 tone;
210
211 if (!priv->warm) {
212 ret = -EFAULT;
213 goto error;
214 }
215
Antti Palosaari279bba02012-07-02 03:46:46 -0300216 dev_dbg(&priv->i2c->dev, "%s: tone_mode=%d\n", __func__,
217 fe_sec_tone_mode);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300218
219 switch (fe_sec_tone_mode) {
220 case SEC_TONE_ON:
221 tone = 1;
222 break;
223 case SEC_TONE_OFF:
224 tone = 0;
225 break;
226 default:
Antti Palosaari279bba02012-07-02 03:46:46 -0300227 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_tone_mode\n",
228 __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300229 ret = -EINVAL;
230 goto error;
231 }
232
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300233 cmd.args[0] = CMD_LNB_PCB_CONFIG;
234 cmd.args[1] = 0;
235 cmd.args[2] = 0x00;
236 cmd.args[3] = 0x00;
237 cmd.args[4] = tone;
238 cmd.len = 5;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300239 ret = tda10071_cmd_execute(priv, &cmd);
240 if (ret)
241 goto error;
242
243 return ret;
244error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300245 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300246 return ret;
247}
248
249static int tda10071_set_voltage(struct dvb_frontend *fe,
250 fe_sec_voltage_t fe_sec_voltage)
251{
252 struct tda10071_priv *priv = fe->demodulator_priv;
253 struct tda10071_cmd cmd;
254 int ret;
255 u8 voltage;
256
257 if (!priv->warm) {
258 ret = -EFAULT;
259 goto error;
260 }
261
Antti Palosaari279bba02012-07-02 03:46:46 -0300262 dev_dbg(&priv->i2c->dev, "%s: voltage=%d\n", __func__, fe_sec_voltage);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300263
264 switch (fe_sec_voltage) {
265 case SEC_VOLTAGE_13:
266 voltage = 0;
267 break;
268 case SEC_VOLTAGE_18:
269 voltage = 1;
270 break;
271 case SEC_VOLTAGE_OFF:
272 voltage = 0;
273 break;
274 default:
Antti Palosaari279bba02012-07-02 03:46:46 -0300275 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_voltage\n",
276 __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300277 ret = -EINVAL;
278 goto error;
Peter Senna Tschudin8f5c9972012-09-07 11:24:43 -0300279 }
Antti Palosaaride8e4202011-08-01 01:07:39 -0300280
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300281 cmd.args[0] = CMD_LNB_SET_DC_LEVEL;
282 cmd.args[1] = 0;
283 cmd.args[2] = voltage;
284 cmd.len = 3;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300285 ret = tda10071_cmd_execute(priv, &cmd);
286 if (ret)
287 goto error;
288
289 return ret;
290error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300291 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300292 return ret;
293}
294
295static int tda10071_diseqc_send_master_cmd(struct dvb_frontend *fe,
296 struct dvb_diseqc_master_cmd *diseqc_cmd)
297{
298 struct tda10071_priv *priv = fe->demodulator_priv;
299 struct tda10071_cmd cmd;
300 int ret, i;
301 u8 tmp;
302
303 if (!priv->warm) {
304 ret = -EFAULT;
305 goto error;
306 }
307
Antti Palosaari279bba02012-07-02 03:46:46 -0300308 dev_dbg(&priv->i2c->dev, "%s: msg_len=%d\n", __func__,
309 diseqc_cmd->msg_len);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300310
Antti Palosaari84431c22012-07-01 15:29:15 -0300311 if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) {
Antti Palosaaride8e4202011-08-01 01:07:39 -0300312 ret = -EINVAL;
313 goto error;
314 }
315
316 /* wait LNB TX */
317 for (i = 500, tmp = 0; i && !tmp; i--) {
318 ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x01);
319 if (ret)
320 goto error;
321
Antti Palosaarid102b732011-09-13 11:51:23 -0300322 usleep_range(10000, 20000);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300323 }
324
Antti Palosaari279bba02012-07-02 03:46:46 -0300325 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300326
327 if (i == 0) {
328 ret = -ETIMEDOUT;
329 goto error;
330 }
331
332 ret = tda10071_wr_reg_mask(priv, 0x47, 0x00, 0x01);
333 if (ret)
334 goto error;
335
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300336 cmd.args[0] = CMD_LNB_SEND_DISEQC;
337 cmd.args[1] = 0;
338 cmd.args[2] = 0;
339 cmd.args[3] = 0;
340 cmd.args[4] = 2;
341 cmd.args[5] = 0;
342 cmd.args[6] = diseqc_cmd->msg_len;
343 memcpy(&cmd.args[7], diseqc_cmd->msg, diseqc_cmd->msg_len);
344 cmd.len = 7 + diseqc_cmd->msg_len;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300345 ret = tda10071_cmd_execute(priv, &cmd);
346 if (ret)
347 goto error;
348
349 return ret;
350error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300351 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300352 return ret;
353}
354
355static int tda10071_diseqc_recv_slave_reply(struct dvb_frontend *fe,
356 struct dvb_diseqc_slave_reply *reply)
357{
358 struct tda10071_priv *priv = fe->demodulator_priv;
359 struct tda10071_cmd cmd;
360 int ret, i;
361 u8 tmp;
362
363 if (!priv->warm) {
364 ret = -EFAULT;
365 goto error;
366 }
367
Antti Palosaari279bba02012-07-02 03:46:46 -0300368 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300369
370 /* wait LNB RX */
371 for (i = 500, tmp = 0; i && !tmp; i--) {
372 ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x02);
373 if (ret)
374 goto error;
375
Antti Palosaarid102b732011-09-13 11:51:23 -0300376 usleep_range(10000, 20000);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300377 }
378
Antti Palosaari279bba02012-07-02 03:46:46 -0300379 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300380
381 if (i == 0) {
382 ret = -ETIMEDOUT;
383 goto error;
384 }
385
386 /* reply len */
387 ret = tda10071_rd_reg(priv, 0x46, &tmp);
388 if (ret)
389 goto error;
390
Peter Senna Tschudin8f5c9972012-09-07 11:24:43 -0300391 reply->msg_len = tmp & 0x1f; /* [4:0] */
Antti Palosaaride8e4202011-08-01 01:07:39 -0300392 if (reply->msg_len > sizeof(reply->msg))
393 reply->msg_len = sizeof(reply->msg); /* truncate API max */
394
395 /* read reply */
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300396 cmd.args[0] = CMD_LNB_UPDATE_REPLY;
397 cmd.args[1] = 0;
398 cmd.len = 2;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300399 ret = tda10071_cmd_execute(priv, &cmd);
400 if (ret)
401 goto error;
402
403 ret = tda10071_rd_regs(priv, cmd.len, reply->msg, reply->msg_len);
404 if (ret)
405 goto error;
406
407 return ret;
408error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300409 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300410 return ret;
411}
412
413static int tda10071_diseqc_send_burst(struct dvb_frontend *fe,
414 fe_sec_mini_cmd_t fe_sec_mini_cmd)
415{
416 struct tda10071_priv *priv = fe->demodulator_priv;
417 struct tda10071_cmd cmd;
418 int ret, i;
419 u8 tmp, burst;
420
421 if (!priv->warm) {
422 ret = -EFAULT;
423 goto error;
424 }
425
Antti Palosaari279bba02012-07-02 03:46:46 -0300426 dev_dbg(&priv->i2c->dev, "%s: fe_sec_mini_cmd=%d\n", __func__,
427 fe_sec_mini_cmd);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300428
429 switch (fe_sec_mini_cmd) {
430 case SEC_MINI_A:
431 burst = 0;
432 break;
433 case SEC_MINI_B:
434 burst = 1;
435 break;
436 default:
Antti Palosaari279bba02012-07-02 03:46:46 -0300437 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_mini_cmd\n",
438 __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300439 ret = -EINVAL;
440 goto error;
441 }
442
443 /* wait LNB TX */
444 for (i = 500, tmp = 0; i && !tmp; i--) {
445 ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x01);
446 if (ret)
447 goto error;
448
Antti Palosaarid102b732011-09-13 11:51:23 -0300449 usleep_range(10000, 20000);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300450 }
451
Antti Palosaari279bba02012-07-02 03:46:46 -0300452 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300453
454 if (i == 0) {
455 ret = -ETIMEDOUT;
456 goto error;
457 }
458
459 ret = tda10071_wr_reg_mask(priv, 0x47, 0x00, 0x01);
460 if (ret)
461 goto error;
462
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300463 cmd.args[0] = CMD_LNB_SEND_TONEBURST;
464 cmd.args[1] = 0;
465 cmd.args[2] = burst;
466 cmd.len = 3;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300467 ret = tda10071_cmd_execute(priv, &cmd);
468 if (ret)
469 goto error;
470
471 return ret;
472error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300473 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300474 return ret;
475}
476
477static int tda10071_read_status(struct dvb_frontend *fe, fe_status_t *status)
478{
479 struct tda10071_priv *priv = fe->demodulator_priv;
480 int ret;
481 u8 tmp;
482
483 *status = 0;
484
485 if (!priv->warm) {
486 ret = 0;
487 goto error;
488 }
489
490 ret = tda10071_rd_reg(priv, 0x39, &tmp);
491 if (ret)
492 goto error;
493
Antti Palosaari07115602014-02-11 00:17:37 -0300494 /* 0x39[0] tuner PLL */
Antti Palosaaride8e4202011-08-01 01:07:39 -0300495 if (tmp & 0x02) /* demod PLL */
Antti Palosaari07115602014-02-11 00:17:37 -0300496 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300497 if (tmp & 0x04) /* viterbi or LDPC*/
498 *status |= FE_HAS_VITERBI;
499 if (tmp & 0x08) /* RS or BCH */
500 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
501
502 priv->fe_status = *status;
503
504 return ret;
505error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300506 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300507 return ret;
508}
509
510static int tda10071_read_snr(struct dvb_frontend *fe, u16 *snr)
511{
512 struct tda10071_priv *priv = fe->demodulator_priv;
513 int ret;
514 u8 buf[2];
515
516 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
517 *snr = 0;
518 ret = 0;
519 goto error;
520 }
521
522 ret = tda10071_rd_regs(priv, 0x3a, buf, 2);
523 if (ret)
524 goto error;
525
526 /* Es/No dBx10 */
527 *snr = buf[0] << 8 | buf[1];
528
529 return ret;
530error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300531 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300532 return ret;
533}
534
535static int tda10071_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
536{
537 struct tda10071_priv *priv = fe->demodulator_priv;
538 struct tda10071_cmd cmd;
539 int ret;
540 u8 tmp;
541
542 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
543 *strength = 0;
544 ret = 0;
545 goto error;
546 }
547
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300548 cmd.args[0] = CMD_GET_AGCACC;
549 cmd.args[1] = 0;
550 cmd.len = 2;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300551 ret = tda10071_cmd_execute(priv, &cmd);
552 if (ret)
553 goto error;
554
555 /* input power estimate dBm */
556 ret = tda10071_rd_reg(priv, 0x50, &tmp);
557 if (ret)
558 goto error;
559
560 if (tmp < 181)
561 tmp = 181; /* -75 dBm */
562 else if (tmp > 236)
563 tmp = 236; /* -20 dBm */
564
565 /* scale value to 0x0000-0xffff */
566 *strength = (tmp-181) * 0xffff / (236-181);
567
568 return ret;
569error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300570 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300571 return ret;
572}
573
574static int tda10071_read_ber(struct dvb_frontend *fe, u32 *ber)
575{
576 struct tda10071_priv *priv = fe->demodulator_priv;
577 struct tda10071_cmd cmd;
578 int ret, i, len;
579 u8 tmp, reg, buf[8];
580
581 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
582 *ber = priv->ber = 0;
583 ret = 0;
584 goto error;
585 }
586
587 switch (priv->delivery_system) {
588 case SYS_DVBS:
589 reg = 0x4c;
590 len = 8;
591 i = 1;
592 break;
593 case SYS_DVBS2:
594 reg = 0x4d;
595 len = 4;
596 i = 0;
597 break;
598 default:
599 *ber = priv->ber = 0;
600 return 0;
601 }
602
603 ret = tda10071_rd_reg(priv, reg, &tmp);
604 if (ret)
605 goto error;
606
607 if (priv->meas_count[i] == tmp) {
Antti Palosaari279bba02012-07-02 03:46:46 -0300608 dev_dbg(&priv->i2c->dev, "%s: meas not ready=%02x\n", __func__,
609 tmp);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300610 *ber = priv->ber;
611 return 0;
612 } else {
613 priv->meas_count[i] = tmp;
614 }
615
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300616 cmd.args[0] = CMD_BER_UPDATE_COUNTERS;
617 cmd.args[1] = 0;
618 cmd.args[2] = i;
619 cmd.len = 3;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300620 ret = tda10071_cmd_execute(priv, &cmd);
621 if (ret)
622 goto error;
623
624 ret = tda10071_rd_regs(priv, cmd.len, buf, len);
625 if (ret)
626 goto error;
627
628 if (priv->delivery_system == SYS_DVBS) {
629 *ber = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
630 priv->ucb += (buf[4] << 8) | buf[5];
631 } else {
632 *ber = (buf[0] << 8) | buf[1];
633 }
634 priv->ber = *ber;
635
636 return ret;
637error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300638 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300639 return ret;
640}
641
642static int tda10071_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
643{
644 struct tda10071_priv *priv = fe->demodulator_priv;
645 int ret = 0;
646
647 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
648 *ucblocks = 0;
649 goto error;
650 }
651
652 /* UCB is updated when BER is read. Assume BER is read anyway. */
653
654 *ucblocks = priv->ucb;
655
656 return ret;
657error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300658 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300659 return ret;
660}
661
Mauro Carvalho Chehabb2a29b52011-12-26 14:46:22 -0300662static int tda10071_set_frontend(struct dvb_frontend *fe)
Antti Palosaaride8e4202011-08-01 01:07:39 -0300663{
664 struct tda10071_priv *priv = fe->demodulator_priv;
665 struct tda10071_cmd cmd;
666 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
667 int ret, i;
668 u8 mode, rolloff, pilot, inversion, div;
669
Antti Palosaari279bba02012-07-02 03:46:46 -0300670 dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d modulation=%d " \
671 "frequency=%d symbol_rate=%d inversion=%d pilot=%d " \
672 "rolloff=%d\n", __func__, c->delivery_system, c->modulation,
673 c->frequency, c->symbol_rate, c->inversion, c->pilot,
674 c->rolloff);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300675
676 priv->delivery_system = SYS_UNDEFINED;
677
678 if (!priv->warm) {
679 ret = -EFAULT;
680 goto error;
681 }
682
683 switch (c->inversion) {
684 case INVERSION_OFF:
685 inversion = 1;
686 break;
687 case INVERSION_ON:
688 inversion = 0;
689 break;
690 case INVERSION_AUTO:
691 /* 2 = auto; try first on then off
692 * 3 = auto; try first off then on */
693 inversion = 3;
694 break;
695 default:
Antti Palosaari279bba02012-07-02 03:46:46 -0300696 dev_dbg(&priv->i2c->dev, "%s: invalid inversion\n", __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300697 ret = -EINVAL;
698 goto error;
699 }
700
701 switch (c->delivery_system) {
702 case SYS_DVBS:
703 rolloff = 0;
704 pilot = 2;
705 break;
706 case SYS_DVBS2:
707 switch (c->rolloff) {
708 case ROLLOFF_20:
709 rolloff = 2;
710 break;
711 case ROLLOFF_25:
712 rolloff = 1;
713 break;
714 case ROLLOFF_35:
715 rolloff = 0;
716 break;
717 case ROLLOFF_AUTO:
718 default:
Antti Palosaari279bba02012-07-02 03:46:46 -0300719 dev_dbg(&priv->i2c->dev, "%s: invalid rolloff\n",
720 __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300721 ret = -EINVAL;
722 goto error;
723 }
724
725 switch (c->pilot) {
726 case PILOT_OFF:
727 pilot = 0;
728 break;
729 case PILOT_ON:
730 pilot = 1;
731 break;
732 case PILOT_AUTO:
733 pilot = 2;
734 break;
735 default:
Antti Palosaari279bba02012-07-02 03:46:46 -0300736 dev_dbg(&priv->i2c->dev, "%s: invalid pilot\n",
737 __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300738 ret = -EINVAL;
739 goto error;
740 }
741 break;
742 default:
Antti Palosaari279bba02012-07-02 03:46:46 -0300743 dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n",
744 __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300745 ret = -EINVAL;
746 goto error;
747 }
748
749 for (i = 0, mode = 0xff; i < ARRAY_SIZE(TDA10071_MODCOD); i++) {
750 if (c->delivery_system == TDA10071_MODCOD[i].delivery_system &&
751 c->modulation == TDA10071_MODCOD[i].modulation &&
752 c->fec_inner == TDA10071_MODCOD[i].fec) {
753 mode = TDA10071_MODCOD[i].val;
Antti Palosaari279bba02012-07-02 03:46:46 -0300754 dev_dbg(&priv->i2c->dev, "%s: mode found=%02x\n",
755 __func__, mode);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300756 break;
757 }
758 }
759
760 if (mode == 0xff) {
Antti Palosaari279bba02012-07-02 03:46:46 -0300761 dev_dbg(&priv->i2c->dev, "%s: invalid parameter combination\n",
762 __func__);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300763 ret = -EINVAL;
764 goto error;
765 }
766
767 if (c->symbol_rate <= 5000000)
768 div = 14;
769 else
770 div = 4;
771
772 ret = tda10071_wr_reg(priv, 0x81, div);
773 if (ret)
774 goto error;
775
776 ret = tda10071_wr_reg(priv, 0xe3, div);
777 if (ret)
778 goto error;
779
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300780 cmd.args[0] = CMD_CHANGE_CHANNEL;
781 cmd.args[1] = 0;
782 cmd.args[2] = mode;
783 cmd.args[3] = (c->frequency >> 16) & 0xff;
784 cmd.args[4] = (c->frequency >> 8) & 0xff;
785 cmd.args[5] = (c->frequency >> 0) & 0xff;
786 cmd.args[6] = ((c->symbol_rate / 1000) >> 8) & 0xff;
787 cmd.args[7] = ((c->symbol_rate / 1000) >> 0) & 0xff;
788 cmd.args[8] = (tda10071_ops.info.frequency_tolerance >> 8) & 0xff;
789 cmd.args[9] = (tda10071_ops.info.frequency_tolerance >> 0) & 0xff;
790 cmd.args[10] = rolloff;
791 cmd.args[11] = inversion;
792 cmd.args[12] = pilot;
793 cmd.args[13] = 0x00;
794 cmd.args[14] = 0x00;
795 cmd.len = 15;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300796 ret = tda10071_cmd_execute(priv, &cmd);
797 if (ret)
798 goto error;
799
800 priv->delivery_system = c->delivery_system;
801
802 return ret;
803error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300804 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300805 return ret;
806}
807
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300808static int tda10071_get_frontend(struct dvb_frontend *fe)
Antti Palosaaride8e4202011-08-01 01:07:39 -0300809{
810 struct tda10071_priv *priv = fe->demodulator_priv;
811 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
812 int ret, i;
813 u8 buf[5], tmp;
814
815 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
816 ret = -EFAULT;
817 goto error;
818 }
819
820 ret = tda10071_rd_regs(priv, 0x30, buf, 5);
821 if (ret)
822 goto error;
823
824 tmp = buf[0] & 0x3f;
825 for (i = 0; i < ARRAY_SIZE(TDA10071_MODCOD); i++) {
826 if (tmp == TDA10071_MODCOD[i].val) {
827 c->modulation = TDA10071_MODCOD[i].modulation;
828 c->fec_inner = TDA10071_MODCOD[i].fec;
829 c->delivery_system = TDA10071_MODCOD[i].delivery_system;
830 }
831 }
832
833 switch ((buf[1] >> 0) & 0x01) {
834 case 0:
835 c->inversion = INVERSION_OFF;
836 break;
837 case 1:
838 c->inversion = INVERSION_ON;
839 break;
840 }
841
842 switch ((buf[1] >> 7) & 0x01) {
843 case 0:
844 c->pilot = PILOT_OFF;
845 break;
846 case 1:
847 c->pilot = PILOT_ON;
848 break;
849 }
850
851 c->frequency = (buf[2] << 16) | (buf[3] << 8) | (buf[4] << 0);
852
853 ret = tda10071_rd_regs(priv, 0x52, buf, 3);
854 if (ret)
855 goto error;
856
857 c->symbol_rate = (buf[0] << 16) | (buf[1] << 8) | (buf[2] << 0);
858
859 return ret;
860error:
Antti Palosaari279bba02012-07-02 03:46:46 -0300861 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300862 return ret;
863}
864
865static int tda10071_init(struct dvb_frontend *fe)
866{
867 struct tda10071_priv *priv = fe->demodulator_priv;
868 struct tda10071_cmd cmd;
Antti Palosaari75d404e2011-09-13 07:30:11 -0300869 int ret, i, len, remaining, fw_size;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300870 const struct firmware *fw;
Antti Palosaari03f4efc2012-09-12 11:37:29 -0300871 u8 *fw_file = TDA10071_FIRMWARE;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300872 u8 tmp, buf[4];
873 struct tda10071_reg_val_mask tab[] = {
874 { 0xcd, 0x00, 0x07 },
875 { 0x80, 0x00, 0x02 },
876 { 0xcd, 0x00, 0xc0 },
877 { 0xce, 0x00, 0x1b },
878 { 0x9d, 0x00, 0x01 },
879 { 0x9d, 0x00, 0x02 },
880 { 0x9e, 0x00, 0x01 },
881 { 0x87, 0x00, 0x80 },
882 { 0xce, 0x00, 0x08 },
883 { 0xce, 0x00, 0x10 },
884 };
885 struct tda10071_reg_val_mask tab2[] = {
886 { 0xf1, 0x70, 0xff },
887 { 0x88, priv->cfg.pll_multiplier, 0x3f },
888 { 0x89, 0x00, 0x10 },
889 { 0x89, 0x10, 0x10 },
890 { 0xc0, 0x01, 0x01 },
891 { 0xc0, 0x00, 0x01 },
892 { 0xe0, 0xff, 0xff },
893 { 0xe0, 0x00, 0xff },
894 { 0x96, 0x1e, 0x7e },
895 { 0x8b, 0x08, 0x08 },
896 { 0x8b, 0x00, 0x08 },
897 { 0x8f, 0x1a, 0x7e },
898 { 0x8c, 0x68, 0xff },
899 { 0x8d, 0x08, 0xff },
900 { 0x8e, 0x4c, 0xff },
901 { 0x8f, 0x01, 0x01 },
902 { 0x8b, 0x04, 0x04 },
903 { 0x8b, 0x00, 0x04 },
904 { 0x87, 0x05, 0x07 },
905 { 0x80, 0x00, 0x20 },
906 { 0xc8, 0x01, 0xff },
907 { 0xb4, 0x47, 0xff },
908 { 0xb5, 0x9c, 0xff },
909 { 0xb6, 0x7d, 0xff },
910 { 0xba, 0x00, 0x03 },
911 { 0xb7, 0x47, 0xff },
912 { 0xb8, 0x9c, 0xff },
913 { 0xb9, 0x7d, 0xff },
914 { 0xba, 0x00, 0x0c },
915 { 0xc8, 0x00, 0xff },
916 { 0xcd, 0x00, 0x04 },
917 { 0xcd, 0x00, 0x20 },
918 { 0xe8, 0x02, 0xff },
919 { 0xcf, 0x20, 0xff },
920 { 0x9b, 0xd7, 0xff },
921 { 0x9a, 0x01, 0x03 },
922 { 0xa8, 0x05, 0x0f },
923 { 0xa8, 0x65, 0xf0 },
924 { 0xa6, 0xa0, 0xf0 },
925 { 0x9d, 0x50, 0xfc },
926 { 0x9e, 0x20, 0xe0 },
927 { 0xa3, 0x1c, 0x7c },
928 { 0xd5, 0x03, 0x03 },
929 };
930
Andreas Matthies6894c072013-09-30 13:08:16 -0300931 if (priv->warm) {
Antti Palosaaride8e4202011-08-01 01:07:39 -0300932 /* warm state - wake up device from sleep */
Antti Palosaaride8e4202011-08-01 01:07:39 -0300933
934 for (i = 0; i < ARRAY_SIZE(tab); i++) {
935 ret = tda10071_wr_reg_mask(priv, tab[i].reg,
936 tab[i].val, tab[i].mask);
937 if (ret)
938 goto error;
939 }
940
Antti Palosaari21c8ba32012-07-01 15:59:13 -0300941 cmd.args[0] = CMD_SET_SLEEP_MODE;
942 cmd.args[1] = 0;
943 cmd.args[2] = 0;
944 cmd.len = 3;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300945 ret = tda10071_cmd_execute(priv, &cmd);
946 if (ret)
947 goto error;
948 } else {
949 /* cold state - try to download firmware */
Antti Palosaaride8e4202011-08-01 01:07:39 -0300950
951 /* request the firmware, this will block and timeout */
952 ret = request_firmware(&fw, fw_file, priv->i2c->dev.parent);
953 if (ret) {
Antti Palosaari279bba02012-07-02 03:46:46 -0300954 dev_err(&priv->i2c->dev, "%s: did not find the " \
955 "firmware file. (%s) Please see " \
956 "linux/Documentation/dvb/ for more " \
957 "details on firmware-problems. (%d)\n",
958 KBUILD_MODNAME, fw_file, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300959 goto error;
960 }
961
962 /* init */
963 for (i = 0; i < ARRAY_SIZE(tab2); i++) {
964 ret = tda10071_wr_reg_mask(priv, tab2[i].reg,
965 tab2[i].val, tab2[i].mask);
966 if (ret)
967 goto error_release_firmware;
968 }
969
970 /* download firmware */
971 ret = tda10071_wr_reg(priv, 0xe0, 0x7f);
972 if (ret)
973 goto error_release_firmware;
974
975 ret = tda10071_wr_reg(priv, 0xf7, 0x81);
976 if (ret)
977 goto error_release_firmware;
978
979 ret = tda10071_wr_reg(priv, 0xf8, 0x00);
980 if (ret)
981 goto error_release_firmware;
982
983 ret = tda10071_wr_reg(priv, 0xf9, 0x00);
984 if (ret)
985 goto error_release_firmware;
986
Antti Palosaari279bba02012-07-02 03:46:46 -0300987 dev_info(&priv->i2c->dev, "%s: found a '%s' in cold state, " \
988 "will try to load a firmware\n", KBUILD_MODNAME,
989 tda10071_ops.info.name);
990 dev_info(&priv->i2c->dev, "%s: downloading firmware from " \
991 "file '%s'\n", KBUILD_MODNAME, fw_file);
Antti Palosaaride8e4202011-08-01 01:07:39 -0300992
Antti Palosaari75d404e2011-09-13 07:30:11 -0300993 /* do not download last byte */
994 fw_size = fw->size - 1;
995
996 for (remaining = fw_size; remaining > 0;
Antti Palosaaride8e4202011-08-01 01:07:39 -0300997 remaining -= (priv->cfg.i2c_wr_max - 1)) {
998 len = remaining;
999 if (len > (priv->cfg.i2c_wr_max - 1))
1000 len = (priv->cfg.i2c_wr_max - 1);
1001
1002 ret = tda10071_wr_regs(priv, 0xfa,
Antti Palosaari75d404e2011-09-13 07:30:11 -03001003 (u8 *) &fw->data[fw_size - remaining], len);
Antti Palosaaride8e4202011-08-01 01:07:39 -03001004 if (ret) {
Antti Palosaari279bba02012-07-02 03:46:46 -03001005 dev_err(&priv->i2c->dev, "%s: firmware " \
1006 "download failed=%d\n",
1007 KBUILD_MODNAME, ret);
Dan Carpentera3a528b2014-01-30 09:00:34 -03001008 goto error_release_firmware;
Antti Palosaaride8e4202011-08-01 01:07:39 -03001009 }
1010 }
1011 release_firmware(fw);
1012
1013 ret = tda10071_wr_reg(priv, 0xf7, 0x0c);
1014 if (ret)
1015 goto error;
1016
1017 ret = tda10071_wr_reg(priv, 0xe0, 0x00);
1018 if (ret)
1019 goto error;
1020
1021 /* wait firmware start */
1022 msleep(250);
1023
1024 /* firmware status */
1025 ret = tda10071_rd_reg(priv, 0x51, &tmp);
1026 if (ret)
1027 goto error;
1028
1029 if (tmp) {
Antti Palosaari279bba02012-07-02 03:46:46 -03001030 dev_info(&priv->i2c->dev, "%s: firmware did not run\n",
1031 KBUILD_MODNAME);
Antti Palosaaride8e4202011-08-01 01:07:39 -03001032 ret = -EFAULT;
1033 goto error;
1034 } else {
1035 priv->warm = 1;
1036 }
1037
Antti Palosaari21c8ba32012-07-01 15:59:13 -03001038 cmd.args[0] = CMD_GET_FW_VERSION;
1039 cmd.len = 1;
Antti Palosaaride8e4202011-08-01 01:07:39 -03001040 ret = tda10071_cmd_execute(priv, &cmd);
1041 if (ret)
1042 goto error;
1043
1044 ret = tda10071_rd_regs(priv, cmd.len, buf, 4);
1045 if (ret)
1046 goto error;
1047
Antti Palosaari279bba02012-07-02 03:46:46 -03001048 dev_info(&priv->i2c->dev, "%s: firmware version %d.%d.%d.%d\n",
1049 KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3]);
1050 dev_info(&priv->i2c->dev, "%s: found a '%s' in warm state\n",
1051 KBUILD_MODNAME, tda10071_ops.info.name);
Antti Palosaaride8e4202011-08-01 01:07:39 -03001052
1053 ret = tda10071_rd_regs(priv, 0x81, buf, 2);
1054 if (ret)
1055 goto error;
1056
Antti Palosaari21c8ba32012-07-01 15:59:13 -03001057 cmd.args[0] = CMD_DEMOD_INIT;
1058 cmd.args[1] = ((priv->cfg.xtal / 1000) >> 8) & 0xff;
1059 cmd.args[2] = ((priv->cfg.xtal / 1000) >> 0) & 0xff;
1060 cmd.args[3] = buf[0];
1061 cmd.args[4] = buf[1];
1062 cmd.args[5] = priv->cfg.pll_multiplier;
1063 cmd.args[6] = priv->cfg.spec_inv;
1064 cmd.args[7] = 0x00;
1065 cmd.len = 8;
Antti Palosaaride8e4202011-08-01 01:07:39 -03001066 ret = tda10071_cmd_execute(priv, &cmd);
1067 if (ret)
1068 goto error;
1069
Antti Palosaari21c8ba32012-07-01 15:59:13 -03001070 cmd.args[0] = CMD_TUNER_INIT;
1071 cmd.args[1] = 0x00;
1072 cmd.args[2] = 0x00;
1073 cmd.args[3] = 0x00;
1074 cmd.args[4] = 0x00;
Michael Krufky0176fd42012-12-16 13:24:59 -03001075 cmd.args[5] = (priv->cfg.tuner_i2c_addr) ? priv->cfg.tuner_i2c_addr : 0x14;
Antti Palosaari21c8ba32012-07-01 15:59:13 -03001076 cmd.args[6] = 0x00;
1077 cmd.args[7] = 0x03;
1078 cmd.args[8] = 0x02;
1079 cmd.args[9] = 0x02;
1080 cmd.args[10] = 0x00;
1081 cmd.args[11] = 0x00;
1082 cmd.args[12] = 0x00;
1083 cmd.args[13] = 0x00;
1084 cmd.args[14] = 0x00;
1085 cmd.len = 15;
Antti Palosaaride8e4202011-08-01 01:07:39 -03001086 ret = tda10071_cmd_execute(priv, &cmd);
1087 if (ret)
1088 goto error;
1089
Antti Palosaari21c8ba32012-07-01 15:59:13 -03001090 cmd.args[0] = CMD_MPEG_CONFIG;
1091 cmd.args[1] = 0;
1092 cmd.args[2] = priv->cfg.ts_mode;
1093 cmd.args[3] = 0x00;
1094 cmd.args[4] = 0x04;
1095 cmd.args[5] = 0x00;
1096 cmd.len = 6;
Antti Palosaaride8e4202011-08-01 01:07:39 -03001097 ret = tda10071_cmd_execute(priv, &cmd);
1098 if (ret)
1099 goto error;
1100
1101 ret = tda10071_wr_reg_mask(priv, 0xf0, 0x01, 0x01);
1102 if (ret)
1103 goto error;
1104
Antti Palosaari21c8ba32012-07-01 15:59:13 -03001105 cmd.args[0] = CMD_LNB_CONFIG;
1106 cmd.args[1] = 0;
1107 cmd.args[2] = 150;
1108 cmd.args[3] = 3;
1109 cmd.args[4] = 22;
1110 cmd.args[5] = 1;
1111 cmd.args[6] = 1;
1112 cmd.args[7] = 30;
1113 cmd.args[8] = 30;
1114 cmd.args[9] = 30;
1115 cmd.args[10] = 30;
1116 cmd.len = 11;
Antti Palosaaride8e4202011-08-01 01:07:39 -03001117 ret = tda10071_cmd_execute(priv, &cmd);
1118 if (ret)
1119 goto error;
1120
Antti Palosaari21c8ba32012-07-01 15:59:13 -03001121 cmd.args[0] = CMD_BER_CONTROL;
1122 cmd.args[1] = 0;
1123 cmd.args[2] = 14;
1124 cmd.args[3] = 14;
1125 cmd.len = 4;
Antti Palosaaride8e4202011-08-01 01:07:39 -03001126 ret = tda10071_cmd_execute(priv, &cmd);
1127 if (ret)
1128 goto error;
1129 }
1130
1131 return ret;
1132error_release_firmware:
1133 release_firmware(fw);
1134error:
Antti Palosaari279bba02012-07-02 03:46:46 -03001135 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -03001136 return ret;
1137}
1138
1139static int tda10071_sleep(struct dvb_frontend *fe)
1140{
1141 struct tda10071_priv *priv = fe->demodulator_priv;
1142 struct tda10071_cmd cmd;
1143 int ret, i;
1144 struct tda10071_reg_val_mask tab[] = {
1145 { 0xcd, 0x07, 0x07 },
1146 { 0x80, 0x02, 0x02 },
1147 { 0xcd, 0xc0, 0xc0 },
1148 { 0xce, 0x1b, 0x1b },
1149 { 0x9d, 0x01, 0x01 },
1150 { 0x9d, 0x02, 0x02 },
1151 { 0x9e, 0x01, 0x01 },
1152 { 0x87, 0x80, 0x80 },
1153 { 0xce, 0x08, 0x08 },
1154 { 0xce, 0x10, 0x10 },
1155 };
1156
1157 if (!priv->warm) {
1158 ret = -EFAULT;
1159 goto error;
1160 }
1161
Antti Palosaari21c8ba32012-07-01 15:59:13 -03001162 cmd.args[0] = CMD_SET_SLEEP_MODE;
1163 cmd.args[1] = 0;
1164 cmd.args[2] = 1;
1165 cmd.len = 3;
Antti Palosaaride8e4202011-08-01 01:07:39 -03001166 ret = tda10071_cmd_execute(priv, &cmd);
1167 if (ret)
1168 goto error;
1169
1170 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1171 ret = tda10071_wr_reg_mask(priv, tab[i].reg, tab[i].val,
1172 tab[i].mask);
1173 if (ret)
1174 goto error;
1175 }
1176
1177 return ret;
1178error:
Antti Palosaari279bba02012-07-02 03:46:46 -03001179 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -03001180 return ret;
1181}
1182
1183static int tda10071_get_tune_settings(struct dvb_frontend *fe,
1184 struct dvb_frontend_tune_settings *s)
1185{
1186 s->min_delay_ms = 8000;
1187 s->step_size = 0;
1188 s->max_drift = 0;
1189
1190 return 0;
1191}
1192
1193static void tda10071_release(struct dvb_frontend *fe)
1194{
1195 struct tda10071_priv *priv = fe->demodulator_priv;
1196 kfree(priv);
1197}
1198
1199struct dvb_frontend *tda10071_attach(const struct tda10071_config *config,
1200 struct i2c_adapter *i2c)
1201{
1202 int ret;
1203 struct tda10071_priv *priv = NULL;
1204 u8 tmp;
1205
1206 /* allocate memory for the internal priv */
1207 priv = kzalloc(sizeof(struct tda10071_priv), GFP_KERNEL);
1208 if (priv == NULL) {
1209 ret = -ENOMEM;
1210 goto error;
1211 }
1212
Michael Krufky41f55d52012-12-16 19:37:11 -03001213 /* make sure demod i2c address is specified */
1214 if (!config->demod_i2c_addr) {
1215 dev_dbg(&i2c->dev, "%s: invalid demod i2c address!\n", __func__);
Mauro Carvalho Chehab1c12bf82012-12-21 14:43:19 -02001216 ret = -EINVAL;
Michael Krufky41f55d52012-12-16 19:37:11 -03001217 goto error;
1218 }
1219
1220 /* make sure tuner i2c address is specified */
1221 if (!config->tuner_i2c_addr) {
1222 dev_dbg(&i2c->dev, "%s: invalid tuner i2c address!\n", __func__);
Mauro Carvalho Chehab1c12bf82012-12-21 14:43:19 -02001223 ret = -EINVAL;
Michael Krufky41f55d52012-12-16 19:37:11 -03001224 goto error;
1225 }
1226
Antti Palosaaride8e4202011-08-01 01:07:39 -03001227 /* setup the priv */
1228 priv->i2c = i2c;
1229 memcpy(&priv->cfg, config, sizeof(struct tda10071_config));
1230
1231 /* chip ID */
1232 ret = tda10071_rd_reg(priv, 0xff, &tmp);
1233 if (ret || tmp != 0x0f)
1234 goto error;
1235
1236 /* chip type */
1237 ret = tda10071_rd_reg(priv, 0xdd, &tmp);
1238 if (ret || tmp != 0x00)
1239 goto error;
1240
1241 /* chip version */
1242 ret = tda10071_rd_reg(priv, 0xfe, &tmp);
1243 if (ret || tmp != 0x01)
1244 goto error;
1245
1246 /* create dvb_frontend */
1247 memcpy(&priv->fe.ops, &tda10071_ops, sizeof(struct dvb_frontend_ops));
1248 priv->fe.demodulator_priv = priv;
1249
1250 return &priv->fe;
1251error:
Antti Palosaari279bba02012-07-02 03:46:46 -03001252 dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaride8e4202011-08-01 01:07:39 -03001253 kfree(priv);
1254 return NULL;
1255}
1256EXPORT_SYMBOL(tda10071_attach);
1257
1258static struct dvb_frontend_ops tda10071_ops = {
Antti Palosaari2b49fad2012-03-12 18:21:20 -03001259 .delsys = { SYS_DVBS, SYS_DVBS2 },
Antti Palosaaride8e4202011-08-01 01:07:39 -03001260 .info = {
1261 .name = "NXP TDA10071",
Antti Palosaaride8e4202011-08-01 01:07:39 -03001262 .frequency_min = 950000,
1263 .frequency_max = 2150000,
1264 .frequency_tolerance = 5000,
1265 .symbol_rate_min = 1000000,
1266 .symbol_rate_max = 45000000,
1267 .caps = FE_CAN_INVERSION_AUTO |
1268 FE_CAN_FEC_1_2 |
1269 FE_CAN_FEC_2_3 |
1270 FE_CAN_FEC_3_4 |
1271 FE_CAN_FEC_4_5 |
1272 FE_CAN_FEC_5_6 |
1273 FE_CAN_FEC_6_7 |
1274 FE_CAN_FEC_7_8 |
1275 FE_CAN_FEC_8_9 |
1276 FE_CAN_FEC_AUTO |
1277 FE_CAN_QPSK |
1278 FE_CAN_RECOVER |
1279 FE_CAN_2G_MODULATION
1280 },
1281
1282 .release = tda10071_release,
1283
1284 .get_tune_settings = tda10071_get_tune_settings,
1285
1286 .init = tda10071_init,
1287 .sleep = tda10071_sleep,
1288
Mauro Carvalho Chehabb2a29b52011-12-26 14:46:22 -03001289 .set_frontend = tda10071_set_frontend,
1290 .get_frontend = tda10071_get_frontend,
Antti Palosaaride8e4202011-08-01 01:07:39 -03001291
1292 .read_status = tda10071_read_status,
1293 .read_snr = tda10071_read_snr,
1294 .read_signal_strength = tda10071_read_signal_strength,
1295 .read_ber = tda10071_read_ber,
1296 .read_ucblocks = tda10071_read_ucblocks,
1297
1298 .diseqc_send_master_cmd = tda10071_diseqc_send_master_cmd,
1299 .diseqc_recv_slave_reply = tda10071_diseqc_recv_slave_reply,
1300 .diseqc_send_burst = tda10071_diseqc_send_burst,
1301
1302 .set_tone = tda10071_set_tone,
1303 .set_voltage = tda10071_set_voltage,
1304};
1305
1306MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1307MODULE_DESCRIPTION("NXP TDA10071 DVB-S/S2 demodulator driver");
1308MODULE_LICENSE("GPL");
Antti Palosaari03f4efc2012-09-12 11:37:29 -03001309MODULE_FIRMWARE(TDA10071_FIRMWARE);