blob: 381a8ad173b5c1c84074fb1fe7390da055d88f09 [file] [log] [blame]
Antti Palosaari395d00d2013-02-25 08:39:16 -03001/*
nibble.maxf4df95b2014-10-30 05:01:14 -03002 * Montage M88DS3103/M88RS6000 demodulator driver
Antti Palosaari395d00d2013-02-25 08:39:16 -03003 *
4 * Copyright (C) 2013 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.
Antti Palosaari395d00d2013-02-25 08:39:16 -030015 */
16
17#include "m88ds3103_priv.h"
18
19static struct dvb_frontend_ops m88ds3103_ops;
20
21/* write multiple registers */
22static int m88ds3103_wr_regs(struct m88ds3103_priv *priv,
23 u8 reg, const u8 *val, int len)
24{
Antti Palosaari63c80f72013-11-07 17:35:43 -030025#define MAX_WR_LEN 32
26#define MAX_WR_XFER_LEN (MAX_WR_LEN + 1)
Antti Palosaari395d00d2013-02-25 08:39:16 -030027 int ret;
Antti Palosaari63c80f72013-11-07 17:35:43 -030028 u8 buf[MAX_WR_XFER_LEN];
Antti Palosaari395d00d2013-02-25 08:39:16 -030029 struct i2c_msg msg[1] = {
30 {
31 .addr = priv->cfg->i2c_addr,
32 .flags = 0,
Antti Palosaari63c80f72013-11-07 17:35:43 -030033 .len = 1 + len,
Antti Palosaari395d00d2013-02-25 08:39:16 -030034 .buf = buf,
35 }
36 };
37
Antti Palosaari63c80f72013-11-07 17:35:43 -030038 if (WARN_ON(len > MAX_WR_LEN))
39 return -EINVAL;
40
Antti Palosaari395d00d2013-02-25 08:39:16 -030041 buf[0] = reg;
42 memcpy(&buf[1], val, len);
43
44 mutex_lock(&priv->i2c_mutex);
45 ret = i2c_transfer(priv->i2c, msg, 1);
46 mutex_unlock(&priv->i2c_mutex);
47 if (ret == 1) {
48 ret = 0;
49 } else {
50 dev_warn(&priv->i2c->dev,
51 "%s: i2c wr failed=%d reg=%02x len=%d\n",
52 KBUILD_MODNAME, ret, reg, len);
53 ret = -EREMOTEIO;
54 }
55
56 return ret;
57}
58
59/* read multiple registers */
60static int m88ds3103_rd_regs(struct m88ds3103_priv *priv,
61 u8 reg, u8 *val, int len)
62{
Antti Palosaari63c80f72013-11-07 17:35:43 -030063#define MAX_RD_LEN 3
64#define MAX_RD_XFER_LEN (MAX_RD_LEN)
Antti Palosaari395d00d2013-02-25 08:39:16 -030065 int ret;
Antti Palosaari63c80f72013-11-07 17:35:43 -030066 u8 buf[MAX_RD_XFER_LEN];
Antti Palosaari395d00d2013-02-25 08:39:16 -030067 struct i2c_msg msg[2] = {
68 {
69 .addr = priv->cfg->i2c_addr,
70 .flags = 0,
71 .len = 1,
72 .buf = &reg,
73 }, {
74 .addr = priv->cfg->i2c_addr,
75 .flags = I2C_M_RD,
Antti Palosaari63c80f72013-11-07 17:35:43 -030076 .len = len,
Antti Palosaari395d00d2013-02-25 08:39:16 -030077 .buf = buf,
78 }
79 };
80
Antti Palosaari63c80f72013-11-07 17:35:43 -030081 if (WARN_ON(len > MAX_RD_LEN))
82 return -EINVAL;
83
Antti Palosaari395d00d2013-02-25 08:39:16 -030084 mutex_lock(&priv->i2c_mutex);
85 ret = i2c_transfer(priv->i2c, msg, 2);
86 mutex_unlock(&priv->i2c_mutex);
87 if (ret == 2) {
88 memcpy(val, buf, len);
89 ret = 0;
90 } else {
91 dev_warn(&priv->i2c->dev,
92 "%s: i2c rd failed=%d reg=%02x len=%d\n",
93 KBUILD_MODNAME, ret, reg, len);
94 ret = -EREMOTEIO;
95 }
96
97 return ret;
98}
99
100/* write single register */
101static int m88ds3103_wr_reg(struct m88ds3103_priv *priv, u8 reg, u8 val)
102{
103 return m88ds3103_wr_regs(priv, reg, &val, 1);
104}
105
106/* read single register */
107static int m88ds3103_rd_reg(struct m88ds3103_priv *priv, u8 reg, u8 *val)
108{
109 return m88ds3103_rd_regs(priv, reg, val, 1);
110}
111
112/* write single register with mask */
113static int m88ds3103_wr_reg_mask(struct m88ds3103_priv *priv,
114 u8 reg, u8 val, u8 mask)
115{
116 int ret;
117 u8 u8tmp;
118
119 /* no need for read if whole reg is written */
120 if (mask != 0xff) {
121 ret = m88ds3103_rd_regs(priv, reg, &u8tmp, 1);
122 if (ret)
123 return ret;
124
125 val &= mask;
126 u8tmp &= ~mask;
127 val |= u8tmp;
128 }
129
130 return m88ds3103_wr_regs(priv, reg, &val, 1);
131}
132
133/* read single register with mask */
134static int m88ds3103_rd_reg_mask(struct m88ds3103_priv *priv,
135 u8 reg, u8 *val, u8 mask)
136{
137 int ret, i;
138 u8 u8tmp;
139
140 ret = m88ds3103_rd_regs(priv, reg, &u8tmp, 1);
141 if (ret)
142 return ret;
143
144 u8tmp &= mask;
145
146 /* find position of the first bit */
147 for (i = 0; i < 8; i++) {
148 if ((mask >> i) & 0x01)
149 break;
150 }
151 *val = u8tmp >> i;
152
153 return 0;
154}
155
Antti Palosaari06487de2013-12-02 14:08:53 -0300156/* write reg val table using reg addr auto increment */
157static int m88ds3103_wr_reg_val_tab(struct m88ds3103_priv *priv,
158 const struct m88ds3103_reg_val *tab, int tab_len)
159{
160 int ret, i, j;
161 u8 buf[83];
Antti Palosaari41b9aa02014-08-21 21:38:29 -0300162
Antti Palosaari06487de2013-12-02 14:08:53 -0300163 dev_dbg(&priv->i2c->dev, "%s: tab_len=%d\n", __func__, tab_len);
164
nibble.maxf4df95b2014-10-30 05:01:14 -0300165 if (tab_len > 86) {
Antti Palosaari06487de2013-12-02 14:08:53 -0300166 ret = -EINVAL;
167 goto err;
168 }
169
170 for (i = 0, j = 0; i < tab_len; i++, j++) {
171 buf[j] = tab[i].val;
172
173 if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1 ||
174 !((j + 1) % (priv->cfg->i2c_wr_max - 1))) {
175 ret = m88ds3103_wr_regs(priv, tab[i].reg - j, buf, j + 1);
176 if (ret)
177 goto err;
178
179 j = -1;
180 }
181 }
182
183 return 0;
184err:
185 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
186 return ret;
187}
188
Antti Palosaari395d00d2013-02-25 08:39:16 -0300189static int m88ds3103_read_status(struct dvb_frontend *fe, fe_status_t *status)
190{
191 struct m88ds3103_priv *priv = fe->demodulator_priv;
192 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaaric1daf652015-04-13 20:56:13 -0300193 int ret, i, itmp;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300194 u8 u8tmp;
Antti Palosaaric1daf652015-04-13 20:56:13 -0300195 u8 buf[3];
Antti Palosaari395d00d2013-02-25 08:39:16 -0300196
197 *status = 0;
198
199 if (!priv->warm) {
200 ret = -EAGAIN;
201 goto err;
202 }
203
204 switch (c->delivery_system) {
205 case SYS_DVBS:
206 ret = m88ds3103_rd_reg_mask(priv, 0xd1, &u8tmp, 0x07);
207 if (ret)
208 goto err;
209
210 if (u8tmp == 0x07)
211 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
212 FE_HAS_VITERBI | FE_HAS_SYNC |
213 FE_HAS_LOCK;
214 break;
215 case SYS_DVBS2:
216 ret = m88ds3103_rd_reg_mask(priv, 0x0d, &u8tmp, 0x8f);
217 if (ret)
218 goto err;
219
220 if (u8tmp == 0x8f)
221 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
222 FE_HAS_VITERBI | FE_HAS_SYNC |
223 FE_HAS_LOCK;
224 break;
225 default:
226 dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n",
227 __func__);
228 ret = -EINVAL;
229 goto err;
230 }
231
232 priv->fe_status = *status;
233
234 dev_dbg(&priv->i2c->dev, "%s: lock=%02x status=%02x\n",
235 __func__, u8tmp, *status);
236
Antti Palosaaric1daf652015-04-13 20:56:13 -0300237 /* CNR */
238 if (priv->fe_status & FE_HAS_VITERBI) {
239 unsigned int cnr, noise, signal, noise_tot, signal_tot;
240
241 cnr = 0;
242 /* more iterations for more accurate estimation */
243 #define M88DS3103_SNR_ITERATIONS 3
244
245 switch (c->delivery_system) {
246 case SYS_DVBS:
247 itmp = 0;
248
249 for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) {
250 ret = m88ds3103_rd_reg(priv, 0xff, &buf[0]);
251 if (ret)
252 goto err;
253
254 itmp += buf[0];
255 }
256
257 /* use of single register limits max value to 15 dB */
258 /* SNR(X) dB = 10 * ln(X) / ln(10) dB */
259 itmp = DIV_ROUND_CLOSEST(itmp, 8 * M88DS3103_SNR_ITERATIONS);
260 if (itmp)
261 cnr = div_u64((u64) 10000 * intlog2(itmp), intlog2(10));
262 break;
263 case SYS_DVBS2:
264 noise_tot = 0;
265 signal_tot = 0;
266
267 for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) {
268 ret = m88ds3103_rd_regs(priv, 0x8c, buf, 3);
269 if (ret)
270 goto err;
271
272 noise = buf[1] << 6; /* [13:6] */
273 noise |= buf[0] & 0x3f; /* [5:0] */
274 noise >>= 2;
275 signal = buf[2] * buf[2];
276 signal >>= 1;
277
278 noise_tot += noise;
279 signal_tot += signal;
280 }
281
282 noise = noise_tot / M88DS3103_SNR_ITERATIONS;
283 signal = signal_tot / M88DS3103_SNR_ITERATIONS;
284
285 /* SNR(X) dB = 10 * log10(X) dB */
286 if (signal > noise) {
287 itmp = signal / noise;
288 cnr = div_u64((u64) 10000 * intlog10(itmp), (1 << 24));
289 }
290 break;
291 default:
292 dev_dbg(&priv->i2c->dev,
293 "%s: invalid delivery_system\n", __func__);
294 ret = -EINVAL;
295 goto err;
296 }
297
298 if (cnr) {
299 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
300 c->cnr.stat[0].svalue = cnr;
301 } else {
302 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
303 }
304 } else {
305 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
306 }
307
Antti Palosaari395d00d2013-02-25 08:39:16 -0300308 return 0;
309err:
310 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
311 return ret;
312}
313
314static int m88ds3103_set_frontend(struct dvb_frontend *fe)
315{
316 struct m88ds3103_priv *priv = fe->demodulator_priv;
317 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari06487de2013-12-02 14:08:53 -0300318 int ret, len;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300319 const struct m88ds3103_reg_val *init;
nibble.maxb6851412014-11-05 11:59:07 -0300320 u8 u8tmp, u8tmp1 = 0, u8tmp2 = 0; /* silence compiler warning */
nibble.maxf4df95b2014-10-30 05:01:14 -0300321 u8 buf[3];
nibble.maxb6851412014-11-05 11:59:07 -0300322 u16 u16tmp, divide_ratio = 0;
nibble.max79d09332014-08-11 01:22:45 -0300323 u32 tuner_frequency, target_mclk;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300324 s32 s32tmp;
Antti Palosaari41b9aa02014-08-21 21:38:29 -0300325
Antti Palosaari395d00d2013-02-25 08:39:16 -0300326 dev_dbg(&priv->i2c->dev,
327 "%s: delivery_system=%d modulation=%d frequency=%d symbol_rate=%d inversion=%d pilot=%d rolloff=%d\n",
328 __func__, c->delivery_system,
329 c->modulation, c->frequency, c->symbol_rate,
330 c->inversion, c->pilot, c->rolloff);
331
332 if (!priv->warm) {
333 ret = -EAGAIN;
334 goto err;
335 }
336
nibble.maxf4df95b2014-10-30 05:01:14 -0300337 /* reset */
338 ret = m88ds3103_wr_reg(priv, 0x07, 0x80);
339 if (ret)
340 goto err;
341
342 ret = m88ds3103_wr_reg(priv, 0x07, 0x00);
343 if (ret)
344 goto err;
345
346 /* Disable demod clock path */
347 if (priv->chip_id == M88RS6000_CHIP_ID) {
348 ret = m88ds3103_wr_reg(priv, 0x06, 0xe0);
349 if (ret)
350 goto err;
351 }
352
Antti Palosaari395d00d2013-02-25 08:39:16 -0300353 /* program tuner */
354 if (fe->ops.tuner_ops.set_params) {
355 ret = fe->ops.tuner_ops.set_params(fe);
356 if (ret)
357 goto err;
358 }
359
360 if (fe->ops.tuner_ops.get_frequency) {
361 ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_frequency);
362 if (ret)
363 goto err;
Antti Palosaari2f9dff32014-02-01 12:58:28 -0300364 } else {
365 /*
366 * Use nominal target frequency as tuner driver does not provide
367 * actual frequency used. Carrier offset calculation is not
368 * valid.
369 */
370 tuner_frequency = c->frequency;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300371 }
372
nibble.maxf4df95b2014-10-30 05:01:14 -0300373 /* select M88RS6000 demod main mclk and ts mclk from tuner die. */
374 if (priv->chip_id == M88RS6000_CHIP_ID) {
375 if (c->symbol_rate > 45010000)
376 priv->mclk_khz = 110250;
377 else
378 priv->mclk_khz = 96000;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300379
nibble.maxf4df95b2014-10-30 05:01:14 -0300380 if (c->delivery_system == SYS_DVBS)
381 target_mclk = 96000;
382 else
383 target_mclk = 144000;
384
385 /* Enable demod clock path */
386 ret = m88ds3103_wr_reg(priv, 0x06, 0x00);
387 if (ret)
388 goto err;
389 usleep_range(10000, 20000);
390 } else {
391 /* set M88DS3103 mclk and ts mclk. */
392 priv->mclk_khz = 96000;
393
nibble.maxb6851412014-11-05 11:59:07 -0300394 switch (priv->cfg->ts_mode) {
395 case M88DS3103_TS_SERIAL:
396 case M88DS3103_TS_SERIAL_D7:
397 target_mclk = priv->cfg->ts_clk;
398 break;
399 case M88DS3103_TS_PARALLEL:
400 case M88DS3103_TS_CI:
401 if (c->delivery_system == SYS_DVBS)
402 target_mclk = 96000;
403 else {
nibble.maxf4df95b2014-10-30 05:01:14 -0300404 if (c->symbol_rate < 18000000)
405 target_mclk = 96000;
406 else if (c->symbol_rate < 28000000)
407 target_mclk = 144000;
408 else
409 target_mclk = 192000;
nibble.maxf4df95b2014-10-30 05:01:14 -0300410 }
nibble.maxb6851412014-11-05 11:59:07 -0300411 break;
412 default:
413 dev_dbg(&priv->i2c->dev, "%s: invalid ts_mode\n",
414 __func__);
415 ret = -EINVAL;
416 goto err;
nibble.maxf4df95b2014-10-30 05:01:14 -0300417 }
418
419 switch (target_mclk) {
420 case 96000:
421 u8tmp1 = 0x02; /* 0b10 */
422 u8tmp2 = 0x01; /* 0b01 */
423 break;
424 case 144000:
425 u8tmp1 = 0x00; /* 0b00 */
426 u8tmp2 = 0x01; /* 0b01 */
427 break;
428 case 192000:
429 u8tmp1 = 0x03; /* 0b11 */
430 u8tmp2 = 0x00; /* 0b00 */
431 break;
432 }
433 ret = m88ds3103_wr_reg_mask(priv, 0x22, u8tmp1 << 6, 0xc0);
434 if (ret)
435 goto err;
436 ret = m88ds3103_wr_reg_mask(priv, 0x24, u8tmp2 << 6, 0xc0);
437 if (ret)
438 goto err;
439 }
Antti Palosaari395d00d2013-02-25 08:39:16 -0300440
441 ret = m88ds3103_wr_reg(priv, 0xb2, 0x01);
442 if (ret)
443 goto err;
444
445 ret = m88ds3103_wr_reg(priv, 0x00, 0x01);
446 if (ret)
447 goto err;
448
449 switch (c->delivery_system) {
450 case SYS_DVBS:
nibble.maxf4df95b2014-10-30 05:01:14 -0300451 if (priv->chip_id == M88RS6000_CHIP_ID) {
452 len = ARRAY_SIZE(m88rs6000_dvbs_init_reg_vals);
453 init = m88rs6000_dvbs_init_reg_vals;
454 } else {
455 len = ARRAY_SIZE(m88ds3103_dvbs_init_reg_vals);
456 init = m88ds3103_dvbs_init_reg_vals;
457 }
Antti Palosaari395d00d2013-02-25 08:39:16 -0300458 break;
459 case SYS_DVBS2:
nibble.maxf4df95b2014-10-30 05:01:14 -0300460 if (priv->chip_id == M88RS6000_CHIP_ID) {
461 len = ARRAY_SIZE(m88rs6000_dvbs2_init_reg_vals);
462 init = m88rs6000_dvbs2_init_reg_vals;
463 } else {
464 len = ARRAY_SIZE(m88ds3103_dvbs2_init_reg_vals);
465 init = m88ds3103_dvbs2_init_reg_vals;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300466 }
467 break;
468 default:
469 dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n",
470 __func__);
471 ret = -EINVAL;
472 goto err;
473 }
474
475 /* program init table */
476 if (c->delivery_system != priv->delivery_system) {
Antti Palosaari06487de2013-12-02 14:08:53 -0300477 ret = m88ds3103_wr_reg_val_tab(priv, init, len);
478 if (ret)
479 goto err;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300480 }
481
nibble.maxf4df95b2014-10-30 05:01:14 -0300482 if (priv->chip_id == M88RS6000_CHIP_ID) {
483 if ((c->delivery_system == SYS_DVBS2)
484 && ((c->symbol_rate / 1000) <= 5000)) {
485 ret = m88ds3103_wr_reg(priv, 0xc0, 0x04);
486 if (ret)
487 goto err;
488 buf[0] = 0x09;
489 buf[1] = 0x22;
490 buf[2] = 0x88;
491 ret = m88ds3103_wr_regs(priv, 0x8a, buf, 3);
492 if (ret)
493 goto err;
494 }
495 ret = m88ds3103_wr_reg_mask(priv, 0x9d, 0x08, 0x08);
496 if (ret)
497 goto err;
498 ret = m88ds3103_wr_reg(priv, 0xf1, 0x01);
499 if (ret)
500 goto err;
501 ret = m88ds3103_wr_reg_mask(priv, 0x30, 0x80, 0x80);
502 if (ret)
503 goto err;
504 }
505
Antti Palosaari395d00d2013-02-25 08:39:16 -0300506 switch (priv->cfg->ts_mode) {
507 case M88DS3103_TS_SERIAL:
508 u8tmp1 = 0x00;
nibble.max79d09332014-08-11 01:22:45 -0300509 u8tmp = 0x06;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300510 break;
511 case M88DS3103_TS_SERIAL_D7:
512 u8tmp1 = 0x20;
nibble.max79d09332014-08-11 01:22:45 -0300513 u8tmp = 0x06;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300514 break;
515 case M88DS3103_TS_PARALLEL:
nibble.max79d09332014-08-11 01:22:45 -0300516 u8tmp = 0x02;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300517 break;
518 case M88DS3103_TS_CI:
nibble.max79d09332014-08-11 01:22:45 -0300519 u8tmp = 0x03;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300520 break;
521 default:
522 dev_dbg(&priv->i2c->dev, "%s: invalid ts_mode\n", __func__);
523 ret = -EINVAL;
524 goto err;
525 }
526
nibble.max79d09332014-08-11 01:22:45 -0300527 if (priv->cfg->ts_clk_pol)
528 u8tmp |= 0x40;
529
Antti Palosaari395d00d2013-02-25 08:39:16 -0300530 /* TS mode */
Antti Palosaari92676ac2013-11-19 23:06:39 -0300531 ret = m88ds3103_wr_reg(priv, 0xfd, u8tmp);
Antti Palosaari395d00d2013-02-25 08:39:16 -0300532 if (ret)
533 goto err;
534
535 switch (priv->cfg->ts_mode) {
536 case M88DS3103_TS_SERIAL:
537 case M88DS3103_TS_SERIAL_D7:
538 ret = m88ds3103_wr_reg_mask(priv, 0x29, u8tmp1, 0x20);
539 if (ret)
540 goto err;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300541 u8tmp1 = 0;
542 u8tmp2 = 0;
nibble.maxb6851412014-11-05 11:59:07 -0300543 break;
544 default:
545 if (priv->cfg->ts_clk) {
546 divide_ratio = DIV_ROUND_UP(target_mclk, priv->cfg->ts_clk);
547 u8tmp1 = divide_ratio / 2;
548 u8tmp2 = DIV_ROUND_UP(divide_ratio, 2);
549 }
Antti Palosaari395d00d2013-02-25 08:39:16 -0300550 }
551
552 dev_dbg(&priv->i2c->dev,
553 "%s: target_mclk=%d ts_clk=%d divide_ratio=%d\n",
nibble.max79d09332014-08-11 01:22:45 -0300554 __func__, target_mclk, priv->cfg->ts_clk, divide_ratio);
Antti Palosaari395d00d2013-02-25 08:39:16 -0300555
556 u8tmp1--;
557 u8tmp2--;
558 /* u8tmp1[5:2] => fe[3:0], u8tmp1[1:0] => ea[7:6] */
559 u8tmp1 &= 0x3f;
560 /* u8tmp2[5:0] => ea[5:0] */
561 u8tmp2 &= 0x3f;
562
563 ret = m88ds3103_rd_reg(priv, 0xfe, &u8tmp);
564 if (ret)
565 goto err;
566
567 u8tmp = ((u8tmp & 0xf0) << 0) | u8tmp1 >> 2;
568 ret = m88ds3103_wr_reg(priv, 0xfe, u8tmp);
569 if (ret)
570 goto err;
571
572 u8tmp = ((u8tmp1 & 0x03) << 6) | u8tmp2 >> 0;
573 ret = m88ds3103_wr_reg(priv, 0xea, u8tmp);
574 if (ret)
575 goto err;
576
Antti Palosaari395d00d2013-02-25 08:39:16 -0300577 if (c->symbol_rate <= 3000000)
578 u8tmp = 0x20;
579 else if (c->symbol_rate <= 10000000)
580 u8tmp = 0x10;
581 else
582 u8tmp = 0x06;
583
584 ret = m88ds3103_wr_reg(priv, 0xc3, 0x08);
585 if (ret)
586 goto err;
587
588 ret = m88ds3103_wr_reg(priv, 0xc8, u8tmp);
589 if (ret)
590 goto err;
591
592 ret = m88ds3103_wr_reg(priv, 0xc4, 0x08);
593 if (ret)
594 goto err;
595
596 ret = m88ds3103_wr_reg(priv, 0xc7, 0x00);
597 if (ret)
598 goto err;
599
nibble.maxf4df95b2014-10-30 05:01:14 -0300600 u16tmp = DIV_ROUND_CLOSEST((c->symbol_rate / 1000) << 15, priv->mclk_khz / 2);
Antti Palosaari395d00d2013-02-25 08:39:16 -0300601 buf[0] = (u16tmp >> 0) & 0xff;
602 buf[1] = (u16tmp >> 8) & 0xff;
603 ret = m88ds3103_wr_regs(priv, 0x61, buf, 2);
604 if (ret)
605 goto err;
606
607 ret = m88ds3103_wr_reg_mask(priv, 0x4d, priv->cfg->spec_inv << 1, 0x02);
608 if (ret)
609 goto err;
610
611 ret = m88ds3103_wr_reg_mask(priv, 0x30, priv->cfg->agc_inv << 4, 0x10);
612 if (ret)
613 goto err;
614
615 ret = m88ds3103_wr_reg(priv, 0x33, priv->cfg->agc);
616 if (ret)
617 goto err;
618
619 dev_dbg(&priv->i2c->dev, "%s: carrier offset=%d\n", __func__,
620 (tuner_frequency - c->frequency));
621
622 s32tmp = 0x10000 * (tuner_frequency - c->frequency);
nibble.maxf4df95b2014-10-30 05:01:14 -0300623 s32tmp = DIV_ROUND_CLOSEST(s32tmp, priv->mclk_khz);
Antti Palosaari395d00d2013-02-25 08:39:16 -0300624 if (s32tmp < 0)
625 s32tmp += 0x10000;
626
627 buf[0] = (s32tmp >> 0) & 0xff;
628 buf[1] = (s32tmp >> 8) & 0xff;
629 ret = m88ds3103_wr_regs(priv, 0x5e, buf, 2);
630 if (ret)
631 goto err;
632
633 ret = m88ds3103_wr_reg(priv, 0x00, 0x00);
634 if (ret)
635 goto err;
636
637 ret = m88ds3103_wr_reg(priv, 0xb2, 0x00);
638 if (ret)
639 goto err;
640
641 priv->delivery_system = c->delivery_system;
642
643 return 0;
644err:
645 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
646 return ret;
647}
648
649static int m88ds3103_init(struct dvb_frontend *fe)
650{
651 struct m88ds3103_priv *priv = fe->demodulator_priv;
Antti Palosaaric1daf652015-04-13 20:56:13 -0300652 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300653 int ret, len, remaining;
654 const struct firmware *fw = NULL;
nibble.maxf4df95b2014-10-30 05:01:14 -0300655 u8 *fw_file;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300656 u8 u8tmp;
Antti Palosaari41b9aa02014-08-21 21:38:29 -0300657
Antti Palosaari395d00d2013-02-25 08:39:16 -0300658 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
659
660 /* set cold state by default */
661 priv->warm = false;
662
663 /* wake up device from sleep */
664 ret = m88ds3103_wr_reg_mask(priv, 0x08, 0x01, 0x01);
665 if (ret)
666 goto err;
667
668 ret = m88ds3103_wr_reg_mask(priv, 0x04, 0x00, 0x01);
669 if (ret)
670 goto err;
671
672 ret = m88ds3103_wr_reg_mask(priv, 0x23, 0x00, 0x10);
673 if (ret)
674 goto err;
675
Antti Palosaari395d00d2013-02-25 08:39:16 -0300676 /* firmware status */
677 ret = m88ds3103_rd_reg(priv, 0xb9, &u8tmp);
678 if (ret)
679 goto err;
680
681 dev_dbg(&priv->i2c->dev, "%s: firmware=%02x\n", __func__, u8tmp);
682
683 if (u8tmp)
684 goto skip_fw_download;
685
nibble.maxf4df95b2014-10-30 05:01:14 -0300686 /* global reset, global diseqc reset, golbal fec reset */
687 ret = m88ds3103_wr_reg(priv, 0x07, 0xe0);
688 if (ret)
689 goto err;
690
691 ret = m88ds3103_wr_reg(priv, 0x07, 0x00);
692 if (ret)
693 goto err;
694
Antti Palosaari395d00d2013-02-25 08:39:16 -0300695 /* cold state - try to download firmware */
696 dev_info(&priv->i2c->dev, "%s: found a '%s' in cold state\n",
697 KBUILD_MODNAME, m88ds3103_ops.info.name);
698
nibble.maxf4df95b2014-10-30 05:01:14 -0300699 if (priv->chip_id == M88RS6000_CHIP_ID)
700 fw_file = M88RS6000_FIRMWARE;
701 else
702 fw_file = M88DS3103_FIRMWARE;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300703 /* request the firmware, this will block and timeout */
704 ret = request_firmware(&fw, fw_file, priv->i2c->dev.parent);
705 if (ret) {
Yannick Guerrinia87a4d32015-02-26 07:13:06 -0300706 dev_err(&priv->i2c->dev, "%s: firmware file '%s' not found\n",
Antti Palosaari395d00d2013-02-25 08:39:16 -0300707 KBUILD_MODNAME, fw_file);
708 goto err;
709 }
710
711 dev_info(&priv->i2c->dev, "%s: downloading firmware from file '%s'\n",
712 KBUILD_MODNAME, fw_file);
713
714 ret = m88ds3103_wr_reg(priv, 0xb2, 0x01);
715 if (ret)
Markus Elfring5ed0cf82014-11-19 19:20:51 -0300716 goto error_fw_release;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300717
718 for (remaining = fw->size; remaining > 0;
719 remaining -= (priv->cfg->i2c_wr_max - 1)) {
720 len = remaining;
721 if (len > (priv->cfg->i2c_wr_max - 1))
722 len = (priv->cfg->i2c_wr_max - 1);
723
724 ret = m88ds3103_wr_regs(priv, 0xb0,
725 &fw->data[fw->size - remaining], len);
726 if (ret) {
727 dev_err(&priv->i2c->dev,
728 "%s: firmware download failed=%d\n",
729 KBUILD_MODNAME, ret);
Markus Elfring5ed0cf82014-11-19 19:20:51 -0300730 goto error_fw_release;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300731 }
732 }
733
734 ret = m88ds3103_wr_reg(priv, 0xb2, 0x00);
735 if (ret)
Markus Elfring5ed0cf82014-11-19 19:20:51 -0300736 goto error_fw_release;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300737
738 release_firmware(fw);
739 fw = NULL;
740
741 ret = m88ds3103_rd_reg(priv, 0xb9, &u8tmp);
742 if (ret)
743 goto err;
744
745 if (!u8tmp) {
746 dev_info(&priv->i2c->dev, "%s: firmware did not run\n",
747 KBUILD_MODNAME);
748 ret = -EFAULT;
749 goto err;
750 }
751
752 dev_info(&priv->i2c->dev, "%s: found a '%s' in warm state\n",
753 KBUILD_MODNAME, m88ds3103_ops.info.name);
754 dev_info(&priv->i2c->dev, "%s: firmware version %X.%X\n",
755 KBUILD_MODNAME, (u8tmp >> 4) & 0xf, (u8tmp >> 0 & 0xf));
756
757skip_fw_download:
758 /* warm state */
759 priv->warm = true;
Antti Palosaaric1daf652015-04-13 20:56:13 -0300760 /* init stats here in order signal app which stats are supported */
761 c->cnr.len = 1;
762 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300763 return 0;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300764
Markus Elfring5ed0cf82014-11-19 19:20:51 -0300765error_fw_release:
766 release_firmware(fw);
767err:
Antti Palosaari395d00d2013-02-25 08:39:16 -0300768 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
769 return ret;
770}
771
772static int m88ds3103_sleep(struct dvb_frontend *fe)
773{
774 struct m88ds3103_priv *priv = fe->demodulator_priv;
775 int ret;
nibble.maxf4df95b2014-10-30 05:01:14 -0300776 u8 u8tmp;
Antti Palosaari41b9aa02014-08-21 21:38:29 -0300777
Antti Palosaari395d00d2013-02-25 08:39:16 -0300778 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
779
Antti Palosaaric1daf652015-04-13 20:56:13 -0300780 priv->fe_status = 0;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300781 priv->delivery_system = SYS_UNDEFINED;
782
783 /* TS Hi-Z */
nibble.maxf4df95b2014-10-30 05:01:14 -0300784 if (priv->chip_id == M88RS6000_CHIP_ID)
785 u8tmp = 0x29;
786 else
787 u8tmp = 0x27;
788 ret = m88ds3103_wr_reg_mask(priv, u8tmp, 0x00, 0x01);
Antti Palosaari395d00d2013-02-25 08:39:16 -0300789 if (ret)
790 goto err;
791
792 /* sleep */
793 ret = m88ds3103_wr_reg_mask(priv, 0x08, 0x00, 0x01);
794 if (ret)
795 goto err;
796
797 ret = m88ds3103_wr_reg_mask(priv, 0x04, 0x01, 0x01);
798 if (ret)
799 goto err;
800
801 ret = m88ds3103_wr_reg_mask(priv, 0x23, 0x10, 0x10);
802 if (ret)
803 goto err;
804
805 return 0;
806err:
807 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
808 return ret;
809}
810
811static int m88ds3103_get_frontend(struct dvb_frontend *fe)
812{
813 struct m88ds3103_priv *priv = fe->demodulator_priv;
814 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
815 int ret;
816 u8 buf[3];
Antti Palosaari41b9aa02014-08-21 21:38:29 -0300817
Antti Palosaari395d00d2013-02-25 08:39:16 -0300818 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
819
820 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
Antti Palosaari9240c382015-04-13 21:00:09 -0300821 ret = 0;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300822 goto err;
823 }
824
825 switch (c->delivery_system) {
826 case SYS_DVBS:
827 ret = m88ds3103_rd_reg(priv, 0xe0, &buf[0]);
828 if (ret)
829 goto err;
830
831 ret = m88ds3103_rd_reg(priv, 0xe6, &buf[1]);
832 if (ret)
833 goto err;
834
835 switch ((buf[0] >> 2) & 0x01) {
836 case 0:
837 c->inversion = INVERSION_OFF;
838 break;
839 case 1:
840 c->inversion = INVERSION_ON;
841 break;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300842 }
843
844 switch ((buf[1] >> 5) & 0x07) {
845 case 0:
846 c->fec_inner = FEC_7_8;
847 break;
848 case 1:
849 c->fec_inner = FEC_5_6;
850 break;
851 case 2:
852 c->fec_inner = FEC_3_4;
853 break;
854 case 3:
855 c->fec_inner = FEC_2_3;
856 break;
857 case 4:
858 c->fec_inner = FEC_1_2;
859 break;
860 default:
861 dev_dbg(&priv->i2c->dev, "%s: invalid fec_inner\n",
862 __func__);
863 }
864
865 c->modulation = QPSK;
866
867 break;
868 case SYS_DVBS2:
869 ret = m88ds3103_rd_reg(priv, 0x7e, &buf[0]);
870 if (ret)
871 goto err;
872
873 ret = m88ds3103_rd_reg(priv, 0x89, &buf[1]);
874 if (ret)
875 goto err;
876
877 ret = m88ds3103_rd_reg(priv, 0xf2, &buf[2]);
878 if (ret)
879 goto err;
880
881 switch ((buf[0] >> 0) & 0x0f) {
882 case 2:
883 c->fec_inner = FEC_2_5;
884 break;
885 case 3:
886 c->fec_inner = FEC_1_2;
887 break;
888 case 4:
889 c->fec_inner = FEC_3_5;
890 break;
891 case 5:
892 c->fec_inner = FEC_2_3;
893 break;
894 case 6:
895 c->fec_inner = FEC_3_4;
896 break;
897 case 7:
898 c->fec_inner = FEC_4_5;
899 break;
900 case 8:
901 c->fec_inner = FEC_5_6;
902 break;
903 case 9:
904 c->fec_inner = FEC_8_9;
905 break;
906 case 10:
907 c->fec_inner = FEC_9_10;
908 break;
909 default:
910 dev_dbg(&priv->i2c->dev, "%s: invalid fec_inner\n",
911 __func__);
912 }
913
914 switch ((buf[0] >> 5) & 0x01) {
915 case 0:
916 c->pilot = PILOT_OFF;
917 break;
918 case 1:
919 c->pilot = PILOT_ON;
920 break;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300921 }
922
923 switch ((buf[0] >> 6) & 0x07) {
924 case 0:
925 c->modulation = QPSK;
926 break;
927 case 1:
928 c->modulation = PSK_8;
929 break;
930 case 2:
931 c->modulation = APSK_16;
932 break;
933 case 3:
934 c->modulation = APSK_32;
935 break;
936 default:
937 dev_dbg(&priv->i2c->dev, "%s: invalid modulation\n",
938 __func__);
939 }
940
941 switch ((buf[1] >> 7) & 0x01) {
942 case 0:
943 c->inversion = INVERSION_OFF;
944 break;
945 case 1:
946 c->inversion = INVERSION_ON;
947 break;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300948 }
949
950 switch ((buf[2] >> 0) & 0x03) {
951 case 0:
952 c->rolloff = ROLLOFF_35;
953 break;
954 case 1:
955 c->rolloff = ROLLOFF_25;
956 break;
957 case 2:
958 c->rolloff = ROLLOFF_20;
959 break;
960 default:
961 dev_dbg(&priv->i2c->dev, "%s: invalid rolloff\n",
962 __func__);
963 }
964 break;
965 default:
966 dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n",
967 __func__);
968 ret = -EINVAL;
969 goto err;
970 }
971
972 ret = m88ds3103_rd_regs(priv, 0x6d, buf, 2);
973 if (ret)
974 goto err;
975
976 c->symbol_rate = 1ull * ((buf[1] << 8) | (buf[0] << 0)) *
nibble.maxf4df95b2014-10-30 05:01:14 -0300977 priv->mclk_khz * 1000 / 0x10000;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300978
979 return 0;
980err:
981 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
982 return ret;
983}
984
985static int m88ds3103_read_snr(struct dvb_frontend *fe, u16 *snr)
986{
Antti Palosaari395d00d2013-02-25 08:39:16 -0300987 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari41b9aa02014-08-21 21:38:29 -0300988
Antti Palosaaric1daf652015-04-13 20:56:13 -0300989 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
990 *snr = div_s64(c->cnr.stat[0].svalue, 100);
991 else
992 *snr = 0;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300993
994 return 0;
Antti Palosaari395d00d2013-02-25 08:39:16 -0300995}
996
Antti Palosaari4423a2b2014-07-10 08:17:59 -0300997static int m88ds3103_read_ber(struct dvb_frontend *fe, u32 *ber)
998{
999 struct m88ds3103_priv *priv = fe->demodulator_priv;
1000 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1001 int ret;
1002 unsigned int utmp;
1003 u8 buf[3], u8tmp;
Antti Palosaari41b9aa02014-08-21 21:38:29 -03001004
Antti Palosaari4423a2b2014-07-10 08:17:59 -03001005 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
1006
1007 switch (c->delivery_system) {
1008 case SYS_DVBS:
1009 ret = m88ds3103_wr_reg(priv, 0xf9, 0x04);
1010 if (ret)
1011 goto err;
1012
1013 ret = m88ds3103_rd_reg(priv, 0xf8, &u8tmp);
1014 if (ret)
1015 goto err;
1016
1017 if (!(u8tmp & 0x10)) {
1018 u8tmp |= 0x10;
1019
1020 ret = m88ds3103_rd_regs(priv, 0xf6, buf, 2);
1021 if (ret)
1022 goto err;
1023
1024 priv->ber = (buf[1] << 8) | (buf[0] << 0);
1025
1026 /* restart counters */
1027 ret = m88ds3103_wr_reg(priv, 0xf8, u8tmp);
1028 if (ret)
1029 goto err;
1030 }
1031 break;
1032 case SYS_DVBS2:
1033 ret = m88ds3103_rd_regs(priv, 0xd5, buf, 3);
1034 if (ret)
1035 goto err;
1036
1037 utmp = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
1038
1039 if (utmp > 3000) {
1040 ret = m88ds3103_rd_regs(priv, 0xf7, buf, 2);
1041 if (ret)
1042 goto err;
1043
1044 priv->ber = (buf[1] << 8) | (buf[0] << 0);
1045
1046 /* restart counters */
1047 ret = m88ds3103_wr_reg(priv, 0xd1, 0x01);
1048 if (ret)
1049 goto err;
1050
1051 ret = m88ds3103_wr_reg(priv, 0xf9, 0x01);
1052 if (ret)
1053 goto err;
1054
1055 ret = m88ds3103_wr_reg(priv, 0xf9, 0x00);
1056 if (ret)
1057 goto err;
1058
1059 ret = m88ds3103_wr_reg(priv, 0xd1, 0x00);
1060 if (ret)
1061 goto err;
1062 }
1063 break;
1064 default:
1065 dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n",
1066 __func__);
1067 ret = -EINVAL;
1068 goto err;
1069 }
1070
1071 *ber = priv->ber;
1072
1073 return 0;
1074err:
1075 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1076 return ret;
1077}
Antti Palosaari395d00d2013-02-25 08:39:16 -03001078
1079static int m88ds3103_set_tone(struct dvb_frontend *fe,
1080 fe_sec_tone_mode_t fe_sec_tone_mode)
1081{
1082 struct m88ds3103_priv *priv = fe->demodulator_priv;
1083 int ret;
1084 u8 u8tmp, tone, reg_a1_mask;
Antti Palosaari41b9aa02014-08-21 21:38:29 -03001085
Antti Palosaari395d00d2013-02-25 08:39:16 -03001086 dev_dbg(&priv->i2c->dev, "%s: fe_sec_tone_mode=%d\n", __func__,
1087 fe_sec_tone_mode);
1088
1089 if (!priv->warm) {
1090 ret = -EAGAIN;
1091 goto err;
1092 }
1093
1094 switch (fe_sec_tone_mode) {
1095 case SEC_TONE_ON:
1096 tone = 0;
Antti Palosaari418a97c2014-02-01 17:28:21 -03001097 reg_a1_mask = 0x47;
Antti Palosaari395d00d2013-02-25 08:39:16 -03001098 break;
1099 case SEC_TONE_OFF:
1100 tone = 1;
1101 reg_a1_mask = 0x00;
1102 break;
1103 default:
1104 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_tone_mode\n",
1105 __func__);
1106 ret = -EINVAL;
1107 goto err;
1108 }
1109
1110 u8tmp = tone << 7 | priv->cfg->envelope_mode << 5;
1111 ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0);
1112 if (ret)
1113 goto err;
1114
1115 u8tmp = 1 << 2;
1116 ret = m88ds3103_wr_reg_mask(priv, 0xa1, u8tmp, reg_a1_mask);
1117 if (ret)
1118 goto err;
1119
1120 return 0;
1121err:
1122 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1123 return ret;
1124}
1125
nibble.max79d09332014-08-11 01:22:45 -03001126static int m88ds3103_set_voltage(struct dvb_frontend *fe,
Antti Palosaarid28677f2014-08-21 21:06:13 -03001127 fe_sec_voltage_t fe_sec_voltage)
nibble.max79d09332014-08-11 01:22:45 -03001128{
1129 struct m88ds3103_priv *priv = fe->demodulator_priv;
Antti Palosaarid28677f2014-08-21 21:06:13 -03001130 int ret;
1131 u8 u8tmp;
1132 bool voltage_sel, voltage_dis;
nibble.max79d09332014-08-11 01:22:45 -03001133
Antti Palosaarid28677f2014-08-21 21:06:13 -03001134 dev_dbg(&priv->i2c->dev, "%s: fe_sec_voltage=%d\n", __func__,
1135 fe_sec_voltage);
nibble.max79d09332014-08-11 01:22:45 -03001136
Antti Palosaarid28677f2014-08-21 21:06:13 -03001137 if (!priv->warm) {
1138 ret = -EAGAIN;
1139 goto err;
1140 }
nibble.max79d09332014-08-11 01:22:45 -03001141
Antti Palosaarid28677f2014-08-21 21:06:13 -03001142 switch (fe_sec_voltage) {
nibble.max79d09332014-08-11 01:22:45 -03001143 case SEC_VOLTAGE_18:
Mauro Carvalho Chehabafbd6eb2014-09-03 15:24:29 -03001144 voltage_sel = true;
1145 voltage_dis = false;
nibble.max79d09332014-08-11 01:22:45 -03001146 break;
1147 case SEC_VOLTAGE_13:
Mauro Carvalho Chehabafbd6eb2014-09-03 15:24:29 -03001148 voltage_sel = false;
1149 voltage_dis = false;
nibble.max79d09332014-08-11 01:22:45 -03001150 break;
1151 case SEC_VOLTAGE_OFF:
Mauro Carvalho Chehabafbd6eb2014-09-03 15:24:29 -03001152 voltage_sel = false;
1153 voltage_dis = true;
nibble.max79d09332014-08-11 01:22:45 -03001154 break;
Antti Palosaarid28677f2014-08-21 21:06:13 -03001155 default:
1156 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_voltage\n",
1157 __func__);
1158 ret = -EINVAL;
1159 goto err;
nibble.max79d09332014-08-11 01:22:45 -03001160 }
Antti Palosaarid28677f2014-08-21 21:06:13 -03001161
1162 /* output pin polarity */
1163 voltage_sel ^= priv->cfg->lnb_hv_pol;
1164 voltage_dis ^= priv->cfg->lnb_en_pol;
1165
1166 u8tmp = voltage_dis << 1 | voltage_sel << 0;
1167 ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0x03);
1168 if (ret)
1169 goto err;
nibble.max79d09332014-08-11 01:22:45 -03001170
1171 return 0;
Antti Palosaarid28677f2014-08-21 21:06:13 -03001172err:
1173 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1174 return ret;
nibble.max79d09332014-08-11 01:22:45 -03001175}
1176
Antti Palosaari395d00d2013-02-25 08:39:16 -03001177static int m88ds3103_diseqc_send_master_cmd(struct dvb_frontend *fe,
1178 struct dvb_diseqc_master_cmd *diseqc_cmd)
1179{
1180 struct m88ds3103_priv *priv = fe->demodulator_priv;
1181 int ret, i;
1182 u8 u8tmp;
Antti Palosaari41b9aa02014-08-21 21:38:29 -03001183
Antti Palosaari395d00d2013-02-25 08:39:16 -03001184 dev_dbg(&priv->i2c->dev, "%s: msg=%*ph\n", __func__,
1185 diseqc_cmd->msg_len, diseqc_cmd->msg);
1186
1187 if (!priv->warm) {
1188 ret = -EAGAIN;
1189 goto err;
1190 }
1191
1192 if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) {
1193 ret = -EINVAL;
1194 goto err;
1195 }
1196
1197 u8tmp = priv->cfg->envelope_mode << 5;
1198 ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0);
1199 if (ret)
1200 goto err;
1201
1202 ret = m88ds3103_wr_regs(priv, 0xa3, diseqc_cmd->msg,
1203 diseqc_cmd->msg_len);
1204 if (ret)
1205 goto err;
1206
1207 ret = m88ds3103_wr_reg(priv, 0xa1,
1208 (diseqc_cmd->msg_len - 1) << 3 | 0x07);
1209 if (ret)
1210 goto err;
1211
1212 /* DiSEqC message typical period is 54 ms */
1213 usleep_range(40000, 60000);
1214
1215 /* wait DiSEqC TX ready */
1216 for (i = 20, u8tmp = 1; i && u8tmp; i--) {
1217 usleep_range(5000, 10000);
1218
1219 ret = m88ds3103_rd_reg_mask(priv, 0xa1, &u8tmp, 0x40);
1220 if (ret)
1221 goto err;
1222 }
1223
1224 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
1225
1226 if (i == 0) {
1227 dev_dbg(&priv->i2c->dev, "%s: diseqc tx timeout\n", __func__);
1228
1229 ret = m88ds3103_wr_reg_mask(priv, 0xa1, 0x40, 0xc0);
1230 if (ret)
1231 goto err;
1232 }
1233
1234 ret = m88ds3103_wr_reg_mask(priv, 0xa2, 0x80, 0xc0);
1235 if (ret)
1236 goto err;
1237
1238 if (i == 0) {
1239 ret = -ETIMEDOUT;
1240 goto err;
1241 }
1242
1243 return 0;
1244err:
1245 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1246 return ret;
1247}
1248
1249static int m88ds3103_diseqc_send_burst(struct dvb_frontend *fe,
1250 fe_sec_mini_cmd_t fe_sec_mini_cmd)
1251{
1252 struct m88ds3103_priv *priv = fe->demodulator_priv;
1253 int ret, i;
1254 u8 u8tmp, burst;
Antti Palosaari41b9aa02014-08-21 21:38:29 -03001255
Antti Palosaari395d00d2013-02-25 08:39:16 -03001256 dev_dbg(&priv->i2c->dev, "%s: fe_sec_mini_cmd=%d\n", __func__,
1257 fe_sec_mini_cmd);
1258
1259 if (!priv->warm) {
1260 ret = -EAGAIN;
1261 goto err;
1262 }
1263
1264 u8tmp = priv->cfg->envelope_mode << 5;
1265 ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0);
1266 if (ret)
1267 goto err;
1268
1269 switch (fe_sec_mini_cmd) {
1270 case SEC_MINI_A:
1271 burst = 0x02;
1272 break;
1273 case SEC_MINI_B:
1274 burst = 0x01;
1275 break;
1276 default:
1277 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_mini_cmd\n",
1278 __func__);
1279 ret = -EINVAL;
1280 goto err;
1281 }
1282
1283 ret = m88ds3103_wr_reg(priv, 0xa1, burst);
1284 if (ret)
1285 goto err;
1286
1287 /* DiSEqC ToneBurst period is 12.5 ms */
1288 usleep_range(11000, 20000);
1289
1290 /* wait DiSEqC TX ready */
1291 for (i = 5, u8tmp = 1; i && u8tmp; i--) {
1292 usleep_range(800, 2000);
1293
1294 ret = m88ds3103_rd_reg_mask(priv, 0xa1, &u8tmp, 0x40);
1295 if (ret)
1296 goto err;
1297 }
1298
1299 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
1300
1301 ret = m88ds3103_wr_reg_mask(priv, 0xa2, 0x80, 0xc0);
1302 if (ret)
1303 goto err;
1304
1305 if (i == 0) {
1306 dev_dbg(&priv->i2c->dev, "%s: diseqc tx timeout\n", __func__);
1307 ret = -ETIMEDOUT;
1308 goto err;
1309 }
1310
1311 return 0;
1312err:
1313 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1314 return ret;
1315}
1316
1317static int m88ds3103_get_tune_settings(struct dvb_frontend *fe,
1318 struct dvb_frontend_tune_settings *s)
1319{
1320 s->min_delay_ms = 3000;
1321
1322 return 0;
1323}
1324
Antti Palosaari44b90552013-11-19 20:32:42 -03001325static void m88ds3103_release(struct dvb_frontend *fe)
Antti Palosaari395d00d2013-02-25 08:39:16 -03001326{
Antti Palosaari44b90552013-11-19 20:32:42 -03001327 struct m88ds3103_priv *priv = fe->demodulator_priv;
Antti Palosaari41b9aa02014-08-21 21:38:29 -03001328
Antti Palosaari44b90552013-11-19 20:32:42 -03001329 i2c_del_mux_adapter(priv->i2c_adapter);
1330 kfree(priv);
Antti Palosaari395d00d2013-02-25 08:39:16 -03001331}
1332
Antti Palosaari44b90552013-11-19 20:32:42 -03001333static int m88ds3103_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
Antti Palosaari395d00d2013-02-25 08:39:16 -03001334{
Antti Palosaari44b90552013-11-19 20:32:42 -03001335 struct m88ds3103_priv *priv = mux_priv;
Antti Palosaari395d00d2013-02-25 08:39:16 -03001336 int ret;
1337 struct i2c_msg gate_open_msg[1] = {
1338 {
1339 .addr = priv->cfg->i2c_addr,
1340 .flags = 0,
1341 .len = 2,
1342 .buf = "\x03\x11",
1343 }
1344 };
Antti Palosaari395d00d2013-02-25 08:39:16 -03001345
1346 mutex_lock(&priv->i2c_mutex);
1347
Antti Palosaari44b90552013-11-19 20:32:42 -03001348 /* open tuner I2C repeater for 1 xfer, closes automatically */
Antti Palosaari4fc57872013-12-08 19:19:11 -03001349 ret = __i2c_transfer(priv->i2c, gate_open_msg, 1);
Antti Palosaari395d00d2013-02-25 08:39:16 -03001350 if (ret != 1) {
Antti Palosaari44b90552013-11-19 20:32:42 -03001351 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d\n",
Antti Palosaari395d00d2013-02-25 08:39:16 -03001352 KBUILD_MODNAME, ret);
Antti Palosaari44b90552013-11-19 20:32:42 -03001353 if (ret >= 0)
1354 ret = -EREMOTEIO;
1355
1356 return ret;
Antti Palosaari395d00d2013-02-25 08:39:16 -03001357 }
1358
Antti Palosaari44b90552013-11-19 20:32:42 -03001359 return 0;
Antti Palosaari395d00d2013-02-25 08:39:16 -03001360}
1361
Antti Palosaari44b90552013-11-19 20:32:42 -03001362static int m88ds3103_deselect(struct i2c_adapter *adap, void *mux_priv,
1363 u32 chan)
Antti Palosaari395d00d2013-02-25 08:39:16 -03001364{
Antti Palosaari44b90552013-11-19 20:32:42 -03001365 struct m88ds3103_priv *priv = mux_priv;
1366
1367 mutex_unlock(&priv->i2c_mutex);
1368
1369 return 0;
Antti Palosaari395d00d2013-02-25 08:39:16 -03001370}
1371
1372struct dvb_frontend *m88ds3103_attach(const struct m88ds3103_config *cfg,
1373 struct i2c_adapter *i2c, struct i2c_adapter **tuner_i2c_adapter)
1374{
1375 int ret;
1376 struct m88ds3103_priv *priv;
1377 u8 chip_id, u8tmp;
1378
1379 /* allocate memory for the internal priv */
Antti Palosaari8a878dc2013-12-18 12:38:42 -03001380 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Antti Palosaari395d00d2013-02-25 08:39:16 -03001381 if (!priv) {
1382 ret = -ENOMEM;
1383 dev_err(&i2c->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
1384 goto err;
1385 }
1386
1387 priv->cfg = cfg;
1388 priv->i2c = i2c;
1389 mutex_init(&priv->i2c_mutex);
1390
nibble.maxf4df95b2014-10-30 05:01:14 -03001391 /* 0x00: chip id[6:0], 0x01: chip ver[7:0], 0x02: chip ver[15:8] */
1392 ret = m88ds3103_rd_reg(priv, 0x00, &chip_id);
Antti Palosaari395d00d2013-02-25 08:39:16 -03001393 if (ret)
1394 goto err;
1395
nibble.maxf4df95b2014-10-30 05:01:14 -03001396 chip_id >>= 1;
1397 dev_info(&priv->i2c->dev, "%s: chip_id=%02x\n", __func__, chip_id);
Antti Palosaari395d00d2013-02-25 08:39:16 -03001398
1399 switch (chip_id) {
nibble.maxf4df95b2014-10-30 05:01:14 -03001400 case M88RS6000_CHIP_ID:
1401 case M88DS3103_CHIP_ID:
Antti Palosaari395d00d2013-02-25 08:39:16 -03001402 break;
1403 default:
1404 goto err;
1405 }
nibble.maxf4df95b2014-10-30 05:01:14 -03001406 priv->chip_id = chip_id;
Antti Palosaari395d00d2013-02-25 08:39:16 -03001407
1408 switch (priv->cfg->clock_out) {
1409 case M88DS3103_CLOCK_OUT_DISABLED:
1410 u8tmp = 0x80;
1411 break;
1412 case M88DS3103_CLOCK_OUT_ENABLED:
1413 u8tmp = 0x00;
1414 break;
1415 case M88DS3103_CLOCK_OUT_ENABLED_DIV2:
1416 u8tmp = 0x10;
1417 break;
1418 default:
1419 goto err;
1420 }
1421
nibble.maxf4df95b2014-10-30 05:01:14 -03001422 /* 0x29 register is defined differently for m88rs6000. */
1423 /* set internal tuner address to 0x21 */
1424 if (chip_id == M88RS6000_CHIP_ID)
1425 u8tmp = 0x00;
1426
Antti Palosaari395d00d2013-02-25 08:39:16 -03001427 ret = m88ds3103_wr_reg(priv, 0x29, u8tmp);
1428 if (ret)
1429 goto err;
1430
1431 /* sleep */
1432 ret = m88ds3103_wr_reg_mask(priv, 0x08, 0x00, 0x01);
1433 if (ret)
1434 goto err;
1435
1436 ret = m88ds3103_wr_reg_mask(priv, 0x04, 0x01, 0x01);
1437 if (ret)
1438 goto err;
1439
1440 ret = m88ds3103_wr_reg_mask(priv, 0x23, 0x10, 0x10);
1441 if (ret)
1442 goto err;
1443
Antti Palosaari44b90552013-11-19 20:32:42 -03001444 /* create mux i2c adapter for tuner */
1445 priv->i2c_adapter = i2c_add_mux_adapter(i2c, &i2c->dev, priv, 0, 0, 0,
1446 m88ds3103_select, m88ds3103_deselect);
1447 if (priv->i2c_adapter == NULL)
1448 goto err;
1449
1450 *tuner_i2c_adapter = priv->i2c_adapter;
1451
Antti Palosaari395d00d2013-02-25 08:39:16 -03001452 /* create dvb_frontend */
1453 memcpy(&priv->fe.ops, &m88ds3103_ops, sizeof(struct dvb_frontend_ops));
nibble.maxf4df95b2014-10-30 05:01:14 -03001454 if (priv->chip_id == M88RS6000_CHIP_ID)
1455 strncpy(priv->fe.ops.info.name,
1456 "Montage M88RS6000", sizeof(priv->fe.ops.info.name));
Antti Palosaari395d00d2013-02-25 08:39:16 -03001457 priv->fe.demodulator_priv = priv;
1458
Antti Palosaari395d00d2013-02-25 08:39:16 -03001459 return &priv->fe;
1460err:
1461 dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
1462 kfree(priv);
1463 return NULL;
1464}
1465EXPORT_SYMBOL(m88ds3103_attach);
1466
1467static struct dvb_frontend_ops m88ds3103_ops = {
1468 .delsys = { SYS_DVBS, SYS_DVBS2 },
1469 .info = {
1470 .name = "Montage M88DS3103",
1471 .frequency_min = 950000,
1472 .frequency_max = 2150000,
1473 .frequency_tolerance = 5000,
1474 .symbol_rate_min = 1000000,
1475 .symbol_rate_max = 45000000,
1476 .caps = FE_CAN_INVERSION_AUTO |
1477 FE_CAN_FEC_1_2 |
1478 FE_CAN_FEC_2_3 |
1479 FE_CAN_FEC_3_4 |
1480 FE_CAN_FEC_4_5 |
1481 FE_CAN_FEC_5_6 |
1482 FE_CAN_FEC_6_7 |
1483 FE_CAN_FEC_7_8 |
1484 FE_CAN_FEC_8_9 |
1485 FE_CAN_FEC_AUTO |
1486 FE_CAN_QPSK |
1487 FE_CAN_RECOVER |
1488 FE_CAN_2G_MODULATION
1489 },
1490
1491 .release = m88ds3103_release,
1492
1493 .get_tune_settings = m88ds3103_get_tune_settings,
1494
1495 .init = m88ds3103_init,
1496 .sleep = m88ds3103_sleep,
1497
1498 .set_frontend = m88ds3103_set_frontend,
1499 .get_frontend = m88ds3103_get_frontend,
1500
1501 .read_status = m88ds3103_read_status,
1502 .read_snr = m88ds3103_read_snr,
Antti Palosaari4423a2b2014-07-10 08:17:59 -03001503 .read_ber = m88ds3103_read_ber,
Antti Palosaari395d00d2013-02-25 08:39:16 -03001504
1505 .diseqc_send_master_cmd = m88ds3103_diseqc_send_master_cmd,
1506 .diseqc_send_burst = m88ds3103_diseqc_send_burst,
1507
1508 .set_tone = m88ds3103_set_tone,
nibble.max79d09332014-08-11 01:22:45 -03001509 .set_voltage = m88ds3103_set_voltage,
Antti Palosaari395d00d2013-02-25 08:39:16 -03001510};
1511
1512MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1513MODULE_DESCRIPTION("Montage M88DS3103 DVB-S/S2 demodulator driver");
1514MODULE_LICENSE("GPL");
1515MODULE_FIRMWARE(M88DS3103_FIRMWARE);
nibble.maxf4df95b2014-10-30 05:01:14 -03001516MODULE_FIRMWARE(M88RS6000_FIRMWARE);