blob: 43b7335f27e4704d88503cf85372678a0374750c [file] [log] [blame]
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001/*
2 * Afatech AF9033 demodulator driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "af9033_priv.h"
23
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030024/* Max transfer size done by I2C transfer functions */
25#define MAX_XFER_SIZE 64
26
Antti Palosaari09611ca2014-08-31 02:18:34 -030027struct af9033_dev {
Antti Palosaari4b64bb22012-03-30 08:21:25 -030028 struct i2c_adapter *i2c;
29 struct dvb_frontend fe;
30 struct af9033_config cfg;
31
32 u32 bandwidth_hz;
33 bool ts_mode_parallel;
34 bool ts_mode_serial;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -030035
36 u32 ber;
37 u32 ucb;
38 unsigned long last_stat_check;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030039};
40
41/* write multiple registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -030042static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030043 int len)
44{
45 int ret;
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030046 u8 buf[MAX_XFER_SIZE];
Antti Palosaari4b64bb22012-03-30 08:21:25 -030047 struct i2c_msg msg[1] = {
48 {
Antti Palosaari09611ca2014-08-31 02:18:34 -030049 .addr = dev->cfg.i2c_addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030050 .flags = 0,
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030051 .len = 3 + len,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030052 .buf = buf,
53 }
54 };
55
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030056 if (3 + len > sizeof(buf)) {
Antti Palosaari09611ca2014-08-31 02:18:34 -030057 dev_warn(&dev->i2c->dev,
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030058 "%s: i2c wr reg=%04x: len=%d is too big!\n",
59 KBUILD_MODNAME, reg, len);
60 return -EINVAL;
61 }
62
Antti Palosaari4b64bb22012-03-30 08:21:25 -030063 buf[0] = (reg >> 16) & 0xff;
64 buf[1] = (reg >> 8) & 0xff;
65 buf[2] = (reg >> 0) & 0xff;
66 memcpy(&buf[3], val, len);
67
Antti Palosaari09611ca2014-08-31 02:18:34 -030068 ret = i2c_transfer(dev->i2c, msg, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -030069 if (ret == 1) {
70 ret = 0;
71 } else {
Antti Palosaari09611ca2014-08-31 02:18:34 -030072 dev_warn(&dev->i2c->dev,
Antti Palosaari24e419a2014-08-09 14:59:53 -030073 "%s: i2c wr failed=%d reg=%06x len=%d\n",
74 KBUILD_MODNAME, ret, reg, len);
Antti Palosaari4b64bb22012-03-30 08:21:25 -030075 ret = -EREMOTEIO;
76 }
77
78 return ret;
79}
80
81/* read multiple registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -030082static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len)
Antti Palosaari4b64bb22012-03-30 08:21:25 -030083{
84 int ret;
85 u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
86 (reg >> 0) & 0xff };
87 struct i2c_msg msg[2] = {
88 {
Antti Palosaari09611ca2014-08-31 02:18:34 -030089 .addr = dev->cfg.i2c_addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030090 .flags = 0,
91 .len = sizeof(buf),
92 .buf = buf
93 }, {
Antti Palosaari09611ca2014-08-31 02:18:34 -030094 .addr = dev->cfg.i2c_addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030095 .flags = I2C_M_RD,
96 .len = len,
97 .buf = val
98 }
99 };
100
Antti Palosaari09611ca2014-08-31 02:18:34 -0300101 ret = i2c_transfer(dev->i2c, msg, 2);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300102 if (ret == 2) {
103 ret = 0;
104 } else {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300105 dev_warn(&dev->i2c->dev,
Antti Palosaari24e419a2014-08-09 14:59:53 -0300106 "%s: i2c rd failed=%d reg=%06x len=%d\n",
107 KBUILD_MODNAME, ret, reg, len);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300108 ret = -EREMOTEIO;
109 }
110
111 return ret;
112}
113
114
115/* write single register */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300116static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300117{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300118 return af9033_wr_regs(dev, reg, &val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300119}
120
121/* read single register */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300122static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300123{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300124 return af9033_rd_regs(dev, reg, val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300125}
126
127/* write single register with mask */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300128static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300129 u8 mask)
130{
131 int ret;
132 u8 tmp;
133
134 /* no need for read if whole reg is written */
135 if (mask != 0xff) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300136 ret = af9033_rd_regs(dev, reg, &tmp, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300137 if (ret)
138 return ret;
139
140 val &= mask;
141 tmp &= ~mask;
142 val |= tmp;
143 }
144
Antti Palosaari09611ca2014-08-31 02:18:34 -0300145 return af9033_wr_regs(dev, reg, &val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300146}
147
148/* read single register with mask */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300149static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300150 u8 mask)
151{
152 int ret, i;
153 u8 tmp;
154
Antti Palosaari09611ca2014-08-31 02:18:34 -0300155 ret = af9033_rd_regs(dev, reg, &tmp, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300156 if (ret)
157 return ret;
158
159 tmp &= mask;
160
161 /* find position of the first bit */
162 for (i = 0; i < 8; i++) {
163 if ((mask >> i) & 0x01)
164 break;
165 }
166 *val = tmp >> i;
167
168 return 0;
169}
170
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300171/* write reg val table using reg addr auto increment */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300172static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300173 const struct reg_val *tab, int tab_len)
174{
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300175#define MAX_TAB_LEN 212
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300176 int ret, i, j;
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300177 u8 buf[1 + MAX_TAB_LEN];
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300178
Antti Palosaari09611ca2014-08-31 02:18:34 -0300179 dev_dbg(&dev->i2c->dev, "%s: tab_len=%d\n", __func__, tab_len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300180
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300181 if (tab_len > sizeof(buf)) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300182 dev_warn(&dev->i2c->dev, "%s: tab len %d is too big\n",
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300183 KBUILD_MODNAME, tab_len);
184 return -EINVAL;
185 }
186
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300187 for (i = 0, j = 0; i < tab_len; i++) {
188 buf[j] = tab[i].val;
189
190 if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300191 ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300192 if (ret < 0)
193 goto err;
194
195 j = 0;
196 } else {
197 j++;
198 }
199 }
200
201 return 0;
202
203err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300204 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300205
206 return ret;
207}
208
Antti Palosaari09611ca2014-08-31 02:18:34 -0300209static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300210{
211 u32 r = 0, c = 0, i;
212
Antti Palosaari09611ca2014-08-31 02:18:34 -0300213 dev_dbg(&dev->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300214
215 if (a > b) {
216 c = a / b;
217 a = a - c * b;
218 }
219
220 for (i = 0; i < x; i++) {
221 if (a >= b) {
222 r += 1;
223 a -= b;
224 }
225 a <<= 1;
226 r <<= 1;
227 }
228 r = (c << (u32)x) + r;
229
Antti Palosaari09611ca2014-08-31 02:18:34 -0300230 dev_dbg(&dev->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n",
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300231 __func__, a, b, x, r, r);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300232
233 return r;
234}
235
236static void af9033_release(struct dvb_frontend *fe)
237{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300238 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300239
Antti Palosaari09611ca2014-08-31 02:18:34 -0300240 kfree(dev);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300241}
242
243static int af9033_init(struct dvb_frontend *fe)
244{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300245 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300246 int ret, i, len;
247 const struct reg_val *init;
248 u8 buf[4];
249 u32 adc_cw, clock_cw;
250 struct reg_val_mask tab[] = {
251 { 0x80fb24, 0x00, 0x08 },
252 { 0x80004c, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300253 { 0x00f641, dev->cfg.tuner, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300254 { 0x80f5ca, 0x01, 0x01 },
255 { 0x80f715, 0x01, 0x01 },
256 { 0x00f41f, 0x04, 0x04 },
257 { 0x00f41a, 0x01, 0x01 },
258 { 0x80f731, 0x00, 0x01 },
259 { 0x00d91e, 0x00, 0x01 },
260 { 0x00d919, 0x00, 0x01 },
261 { 0x80f732, 0x00, 0x01 },
262 { 0x00d91f, 0x00, 0x01 },
263 { 0x00d91a, 0x00, 0x01 },
264 { 0x80f730, 0x00, 0x01 },
265 { 0x80f778, 0x00, 0xff },
266 { 0x80f73c, 0x01, 0x01 },
267 { 0x80f776, 0x00, 0x01 },
268 { 0x00d8fd, 0x01, 0xff },
269 { 0x00d830, 0x01, 0xff },
270 { 0x00d831, 0x00, 0xff },
271 { 0x00d832, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300272 { 0x80f985, dev->ts_mode_serial, 0x01 },
273 { 0x80f986, dev->ts_mode_parallel, 0x01 },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300274 { 0x00d827, 0x00, 0xff },
275 { 0x00d829, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300276 { 0x800045, dev->cfg.adc_multiplier, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300277 };
278
279 /* program clock control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300280 clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300281 buf[0] = (clock_cw >> 0) & 0xff;
282 buf[1] = (clock_cw >> 8) & 0xff;
283 buf[2] = (clock_cw >> 16) & 0xff;
284 buf[3] = (clock_cw >> 24) & 0xff;
285
Antti Palosaari09611ca2014-08-31 02:18:34 -0300286 dev_dbg(&dev->i2c->dev, "%s: clock=%d clock_cw=%08x\n",
287 __func__, dev->cfg.clock, clock_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300288
Antti Palosaari09611ca2014-08-31 02:18:34 -0300289 ret = af9033_wr_regs(dev, 0x800025, buf, 4);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300290 if (ret < 0)
291 goto err;
292
293 /* program ADC control */
294 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300295 if (clock_adc_lut[i].clock == dev->cfg.clock)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300296 break;
297 }
298
Antti Palosaari09611ca2014-08-31 02:18:34 -0300299 adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300300 buf[0] = (adc_cw >> 0) & 0xff;
301 buf[1] = (adc_cw >> 8) & 0xff;
302 buf[2] = (adc_cw >> 16) & 0xff;
303
Antti Palosaari09611ca2014-08-31 02:18:34 -0300304 dev_dbg(&dev->i2c->dev, "%s: adc=%d adc_cw=%06x\n",
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300305 __func__, clock_adc_lut[i].adc, adc_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300306
Antti Palosaari09611ca2014-08-31 02:18:34 -0300307 ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300308 if (ret < 0)
309 goto err;
310
311 /* program register table */
312 for (i = 0; i < ARRAY_SIZE(tab); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300313 ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300314 tab[i].mask);
315 if (ret < 0)
316 goto err;
317 }
318
Antti Palosaarica681fe2014-08-08 23:56:49 -0300319 /* clock output */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300320 if (dev->cfg.dyn0_clk) {
321 ret = af9033_wr_reg(dev, 0x80fba8, 0x00);
Antti Palosaari9dc0f3f2014-08-09 01:03:14 -0300322 if (ret < 0)
323 goto err;
324 }
325
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300326 /* settings for TS interface */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300327 if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
328 ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300329 if (ret < 0)
330 goto err;
331
Antti Palosaari09611ca2014-08-31 02:18:34 -0300332 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300333 if (ret < 0)
334 goto err;
335 } else {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300336 ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300337 if (ret < 0)
338 goto err;
339
Antti Palosaari09611ca2014-08-31 02:18:34 -0300340 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300341 if (ret < 0)
342 goto err;
343 }
344
345 /* load OFSM settings */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300346 dev_dbg(&dev->i2c->dev, "%s: load ofsm settings\n", __func__);
347 switch (dev->cfg.tuner) {
Antti Palosaarife8eece2013-02-03 13:39:55 -0300348 case AF9033_TUNER_IT9135_38:
349 case AF9033_TUNER_IT9135_51:
350 case AF9033_TUNER_IT9135_52:
Antti Palosaari463c3992013-03-07 00:03:06 -0300351 len = ARRAY_SIZE(ofsm_init_it9135_v1);
352 init = ofsm_init_it9135_v1;
353 break;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300354 case AF9033_TUNER_IT9135_60:
355 case AF9033_TUNER_IT9135_61:
356 case AF9033_TUNER_IT9135_62:
Antti Palosaari463c3992013-03-07 00:03:06 -0300357 len = ARRAY_SIZE(ofsm_init_it9135_v2);
358 init = ofsm_init_it9135_v2;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300359 break;
360 default:
361 len = ARRAY_SIZE(ofsm_init);
362 init = ofsm_init;
363 break;
364 }
365
Antti Palosaari09611ca2014-08-31 02:18:34 -0300366 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300367 if (ret < 0)
368 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300369
370 /* load tuner specific settings */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300371 dev_dbg(&dev->i2c->dev, "%s: load tuner specific settings\n",
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300372 __func__);
Antti Palosaari09611ca2014-08-31 02:18:34 -0300373 switch (dev->cfg.tuner) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300374 case AF9033_TUNER_TUA9001:
375 len = ARRAY_SIZE(tuner_init_tua9001);
376 init = tuner_init_tua9001;
377 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300378 case AF9033_TUNER_FC0011:
379 len = ARRAY_SIZE(tuner_init_fc0011);
380 init = tuner_init_fc0011;
381 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300382 case AF9033_TUNER_MXL5007T:
383 len = ARRAY_SIZE(tuner_init_mxl5007t);
384 init = tuner_init_mxl5007t;
385 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300386 case AF9033_TUNER_TDA18218:
387 len = ARRAY_SIZE(tuner_init_tda18218);
388 init = tuner_init_tda18218;
389 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300390 case AF9033_TUNER_FC2580:
391 len = ARRAY_SIZE(tuner_init_fc2580);
392 init = tuner_init_fc2580;
393 break;
Antti Palosaarie713ad12012-12-02 18:47:00 -0300394 case AF9033_TUNER_FC0012:
395 len = ARRAY_SIZE(tuner_init_fc0012);
396 init = tuner_init_fc0012;
397 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300398 case AF9033_TUNER_IT9135_38:
Antti Palosaaria72cbb72013-03-07 21:17:55 -0300399 len = ARRAY_SIZE(tuner_init_it9135_38);
400 init = tuner_init_it9135_38;
401 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300402 case AF9033_TUNER_IT9135_51:
Antti Palosaaribb2e12a2013-03-07 21:25:16 -0300403 len = ARRAY_SIZE(tuner_init_it9135_51);
404 init = tuner_init_it9135_51;
405 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300406 case AF9033_TUNER_IT9135_52:
Antti Palosaari22d729f2013-03-07 21:29:05 -0300407 len = ARRAY_SIZE(tuner_init_it9135_52);
408 init = tuner_init_it9135_52;
409 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300410 case AF9033_TUNER_IT9135_60:
Antti Palosaaria49f53a2013-03-07 21:38:00 -0300411 len = ARRAY_SIZE(tuner_init_it9135_60);
412 init = tuner_init_it9135_60;
413 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300414 case AF9033_TUNER_IT9135_61:
Antti Palosaari85211322013-03-07 21:44:30 -0300415 len = ARRAY_SIZE(tuner_init_it9135_61);
416 init = tuner_init_it9135_61;
417 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300418 case AF9033_TUNER_IT9135_62:
Antti Palosaaridc4a2c42013-03-07 21:47:20 -0300419 len = ARRAY_SIZE(tuner_init_it9135_62);
420 init = tuner_init_it9135_62;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300421 break;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300422 default:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300423 dev_dbg(&dev->i2c->dev, "%s: unsupported tuner ID=%d\n",
424 __func__, dev->cfg.tuner);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300425 ret = -ENODEV;
426 goto err;
427 }
428
Antti Palosaari09611ca2014-08-31 02:18:34 -0300429 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300430 if (ret < 0)
431 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300432
Antti Palosaari09611ca2014-08-31 02:18:34 -0300433 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
434 ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300435 if (ret < 0)
436 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300437
Antti Palosaari09611ca2014-08-31 02:18:34 -0300438 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300439 if (ret < 0)
440 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300441
Antti Palosaari09611ca2014-08-31 02:18:34 -0300442 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300443 if (ret < 0)
444 goto err;
445 }
446
Antti Palosaari09611ca2014-08-31 02:18:34 -0300447 switch (dev->cfg.tuner) {
Antti Palosaari086991d2013-03-07 22:18:56 -0300448 case AF9033_TUNER_IT9135_60:
449 case AF9033_TUNER_IT9135_61:
450 case AF9033_TUNER_IT9135_62:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300451 ret = af9033_wr_reg(dev, 0x800000, 0x01);
Antti Palosaari086991d2013-03-07 22:18:56 -0300452 if (ret < 0)
453 goto err;
454 }
455
Antti Palosaari09611ca2014-08-31 02:18:34 -0300456 dev->bandwidth_hz = 0; /* force to program all parameters */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300457
458 return 0;
459
460err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300461 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300462
463 return ret;
464}
465
466static int af9033_sleep(struct dvb_frontend *fe)
467{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300468 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300469 int ret, i;
470 u8 tmp;
471
Antti Palosaari09611ca2014-08-31 02:18:34 -0300472 ret = af9033_wr_reg(dev, 0x80004c, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300473 if (ret < 0)
474 goto err;
475
Antti Palosaari09611ca2014-08-31 02:18:34 -0300476 ret = af9033_wr_reg(dev, 0x800000, 0);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300477 if (ret < 0)
478 goto err;
479
480 for (i = 100, tmp = 1; i && tmp; i--) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300481 ret = af9033_rd_reg(dev, 0x80004c, &tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300482 if (ret < 0)
483 goto err;
484
485 usleep_range(200, 10000);
486 }
487
Antti Palosaari09611ca2014-08-31 02:18:34 -0300488 dev_dbg(&dev->i2c->dev, "%s: loop=%d\n", __func__, i);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300489
490 if (i == 0) {
491 ret = -ETIMEDOUT;
492 goto err;
493 }
494
Antti Palosaari09611ca2014-08-31 02:18:34 -0300495 ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300496 if (ret < 0)
497 goto err;
498
499 /* prevent current leak (?) */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300500 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300501 /* enable parallel TS */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300502 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300503 if (ret < 0)
504 goto err;
505
Antti Palosaari09611ca2014-08-31 02:18:34 -0300506 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300507 if (ret < 0)
508 goto err;
509 }
510
511 return 0;
512
513err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300514 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300515
516 return ret;
517}
518
519static int af9033_get_tune_settings(struct dvb_frontend *fe,
520 struct dvb_frontend_tune_settings *fesettings)
521{
Antti Palosaarife8eece2013-02-03 13:39:55 -0300522 /* 800 => 2000 because IT9135 v2 is slow to gain lock */
523 fesettings->min_delay_ms = 2000;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300524 fesettings->step_size = 0;
525 fesettings->max_drift = 0;
526
527 return 0;
528}
529
530static int af9033_set_frontend(struct dvb_frontend *fe)
531{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300532 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300533 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300534 int ret, i, spec_inv, sampling_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300535 u8 tmp, buf[3], bandwidth_reg_val;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300536 u32 if_frequency, freq_cw, adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300537
Antti Palosaari09611ca2014-08-31 02:18:34 -0300538 dev_dbg(&dev->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300539 __func__, c->frequency, c->bandwidth_hz);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300540
541 /* check bandwidth */
542 switch (c->bandwidth_hz) {
543 case 6000000:
544 bandwidth_reg_val = 0x00;
545 break;
546 case 7000000:
547 bandwidth_reg_val = 0x01;
548 break;
549 case 8000000:
550 bandwidth_reg_val = 0x02;
551 break;
552 default:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300553 dev_dbg(&dev->i2c->dev, "%s: invalid bandwidth_hz\n",
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300554 __func__);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300555 ret = -EINVAL;
556 goto err;
557 }
558
559 /* program tuner */
560 if (fe->ops.tuner_ops.set_params)
561 fe->ops.tuner_ops.set_params(fe);
562
563 /* program CFOE coefficients */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300564 if (c->bandwidth_hz != dev->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300565 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300566 if (coeff_lut[i].clock == dev->cfg.clock &&
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300567 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
568 break;
569 }
570 }
Antti Palosaari09611ca2014-08-31 02:18:34 -0300571 ret = af9033_wr_regs(dev, 0x800001,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300572 coeff_lut[i].val, sizeof(coeff_lut[i].val));
573 }
574
575 /* program frequency control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300576 if (c->bandwidth_hz != dev->bandwidth_hz) {
577 spec_inv = dev->cfg.spec_inv ? -1 : 1;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300578
579 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300580 if (clock_adc_lut[i].clock == dev->cfg.clock)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300581 break;
582 }
583 adc_freq = clock_adc_lut[i].adc;
584
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300585 /* get used IF frequency */
586 if (fe->ops.tuner_ops.get_if_frequency)
587 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
588 else
589 if_frequency = 0;
590
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300591 sampling_freq = if_frequency;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300592
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300593 while (sampling_freq > (adc_freq / 2))
594 sampling_freq -= adc_freq;
595
596 if (sampling_freq >= 0)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300597 spec_inv *= -1;
598 else
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300599 sampling_freq *= -1;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300600
Antti Palosaari09611ca2014-08-31 02:18:34 -0300601 freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300602
603 if (spec_inv == -1)
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300604 freq_cw = 0x800000 - freq_cw;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300605
Antti Palosaari09611ca2014-08-31 02:18:34 -0300606 if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300607 freq_cw /= 2;
608
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300609 buf[0] = (freq_cw >> 0) & 0xff;
610 buf[1] = (freq_cw >> 8) & 0xff;
611 buf[2] = (freq_cw >> 16) & 0x7f;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300612
613 /* FIXME: there seems to be calculation error here... */
614 if (if_frequency == 0)
615 buf[2] = 0;
616
Antti Palosaari09611ca2014-08-31 02:18:34 -0300617 ret = af9033_wr_regs(dev, 0x800029, buf, 3);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300618 if (ret < 0)
619 goto err;
620
Antti Palosaari09611ca2014-08-31 02:18:34 -0300621 dev->bandwidth_hz = c->bandwidth_hz;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300622 }
623
Antti Palosaari09611ca2014-08-31 02:18:34 -0300624 ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300625 if (ret < 0)
626 goto err;
627
Antti Palosaari09611ca2014-08-31 02:18:34 -0300628 ret = af9033_wr_reg(dev, 0x800040, 0x00);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300629 if (ret < 0)
630 goto err;
631
Antti Palosaari09611ca2014-08-31 02:18:34 -0300632 ret = af9033_wr_reg(dev, 0x800047, 0x00);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300633 if (ret < 0)
634 goto err;
635
Antti Palosaari09611ca2014-08-31 02:18:34 -0300636 ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300637 if (ret < 0)
638 goto err;
639
640 if (c->frequency <= 230000000)
641 tmp = 0x00; /* VHF */
642 else
643 tmp = 0x01; /* UHF */
644
Antti Palosaari09611ca2014-08-31 02:18:34 -0300645 ret = af9033_wr_reg(dev, 0x80004b, tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300646 if (ret < 0)
647 goto err;
648
Antti Palosaari09611ca2014-08-31 02:18:34 -0300649 ret = af9033_wr_reg(dev, 0x800000, 0x00);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300650 if (ret < 0)
651 goto err;
652
653 return 0;
654
655err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300656 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300657
658 return ret;
659}
660
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300661static int af9033_get_frontend(struct dvb_frontend *fe)
662{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300663 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300664 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300665 int ret;
666 u8 buf[8];
667
Antti Palosaari09611ca2014-08-31 02:18:34 -0300668 dev_dbg(&dev->i2c->dev, "%s:\n", __func__);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300669
670 /* read all needed registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300671 ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf));
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300672 if (ret < 0)
673 goto err;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300674
675 switch ((buf[0] >> 0) & 3) {
676 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300677 c->transmission_mode = TRANSMISSION_MODE_2K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300678 break;
679 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300680 c->transmission_mode = TRANSMISSION_MODE_8K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300681 break;
682 }
683
684 switch ((buf[1] >> 0) & 3) {
685 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300686 c->guard_interval = GUARD_INTERVAL_1_32;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300687 break;
688 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300689 c->guard_interval = GUARD_INTERVAL_1_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300690 break;
691 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300692 c->guard_interval = GUARD_INTERVAL_1_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300693 break;
694 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300695 c->guard_interval = GUARD_INTERVAL_1_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300696 break;
697 }
698
699 switch ((buf[2] >> 0) & 7) {
700 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300701 c->hierarchy = HIERARCHY_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300702 break;
703 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300704 c->hierarchy = HIERARCHY_1;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300705 break;
706 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300707 c->hierarchy = HIERARCHY_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300708 break;
709 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300710 c->hierarchy = HIERARCHY_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300711 break;
712 }
713
714 switch ((buf[3] >> 0) & 3) {
715 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300716 c->modulation = QPSK;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300717 break;
718 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300719 c->modulation = QAM_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300720 break;
721 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300722 c->modulation = QAM_64;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300723 break;
724 }
725
726 switch ((buf[4] >> 0) & 3) {
727 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300728 c->bandwidth_hz = 6000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300729 break;
730 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300731 c->bandwidth_hz = 7000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300732 break;
733 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300734 c->bandwidth_hz = 8000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300735 break;
736 }
737
738 switch ((buf[6] >> 0) & 7) {
739 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300740 c->code_rate_HP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300741 break;
742 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300743 c->code_rate_HP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300744 break;
745 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300746 c->code_rate_HP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300747 break;
748 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300749 c->code_rate_HP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300750 break;
751 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300752 c->code_rate_HP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300753 break;
754 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300755 c->code_rate_HP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300756 break;
757 }
758
759 switch ((buf[7] >> 0) & 7) {
760 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300761 c->code_rate_LP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300762 break;
763 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300764 c->code_rate_LP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300765 break;
766 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300767 c->code_rate_LP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300768 break;
769 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300770 c->code_rate_LP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300771 break;
772 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300773 c->code_rate_LP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300774 break;
775 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300776 c->code_rate_LP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300777 break;
778 }
779
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300780 return 0;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300781
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300782err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300783 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300784
785 return ret;
786}
787
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300788static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
789{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300790 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300791 int ret;
792 u8 tmp;
793
794 *status = 0;
795
796 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300797 ret = af9033_rd_reg(dev, 0x800047, &tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300798 if (ret < 0)
799 goto err;
800
801 /* has signal */
802 if (tmp == 0x01)
803 *status |= FE_HAS_SIGNAL;
804
805 if (tmp != 0x02) {
806 /* TPS lock */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300807 ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300808 if (ret < 0)
809 goto err;
810
811 if (tmp)
812 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
813 FE_HAS_VITERBI;
814
815 /* full lock */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300816 ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300817 if (ret < 0)
818 goto err;
819
820 if (tmp)
821 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
822 FE_HAS_VITERBI | FE_HAS_SYNC |
823 FE_HAS_LOCK;
824 }
825
826 return 0;
827
828err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300829 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300830
831 return ret;
832}
833
834static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
835{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300836 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300837 int ret, i, len;
838 u8 buf[3], tmp;
839 u32 snr_val;
Antti Palosaari24e419a2014-08-09 14:59:53 -0300840 const struct val_snr *snr_lut;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300841
842 /* read value */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300843 ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
Antti Palosaarie898ef62012-04-01 12:50:02 -0300844 if (ret < 0)
845 goto err;
846
847 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
848
849 /* read current modulation */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300850 ret = af9033_rd_reg(dev, 0x80f903, &tmp);
Antti Palosaarie898ef62012-04-01 12:50:02 -0300851 if (ret < 0)
852 goto err;
853
854 switch ((tmp >> 0) & 3) {
855 case 0:
856 len = ARRAY_SIZE(qpsk_snr_lut);
857 snr_lut = qpsk_snr_lut;
858 break;
859 case 1:
860 len = ARRAY_SIZE(qam16_snr_lut);
861 snr_lut = qam16_snr_lut;
862 break;
863 case 2:
864 len = ARRAY_SIZE(qam64_snr_lut);
865 snr_lut = qam64_snr_lut;
866 break;
867 default:
868 goto err;
869 }
870
871 for (i = 0; i < len; i++) {
872 tmp = snr_lut[i].snr;
873
874 if (snr_val < snr_lut[i].val)
875 break;
876 }
877
878 *snr = tmp * 10; /* dB/10 */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300879
880 return 0;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300881
882err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300883 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarie898ef62012-04-01 12:50:02 -0300884
885 return ret;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300886}
887
888static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
889{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300890 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300891 int ret;
892 u8 strength2;
893
894 /* read signal strength of 0-100 scale */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300895 ret = af9033_rd_reg(dev, 0x800048, &strength2);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300896 if (ret < 0)
897 goto err;
898
899 /* scale value to 0x0000-0xffff */
900 *strength = strength2 * 0xffff / 100;
901
902 return 0;
903
904err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300905 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300906
907 return ret;
908}
909
Antti Palosaari09611ca2014-08-31 02:18:34 -0300910static int af9033_update_ch_stat(struct af9033_dev *dev)
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300911{
912 int ret = 0;
913 u32 err_cnt, bit_cnt;
914 u16 abort_cnt;
915 u8 buf[7];
916
917 /* only update data every half second */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300918 if (time_after(jiffies, dev->last_stat_check + msecs_to_jiffies(500))) {
919 ret = af9033_rd_regs(dev, 0x800032, buf, sizeof(buf));
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300920 if (ret < 0)
921 goto err;
922 /* in 8 byte packets? */
923 abort_cnt = (buf[1] << 8) + buf[0];
924 /* in bits */
925 err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
926 /* in 8 byte packets? always(?) 0x2710 = 10000 */
927 bit_cnt = (buf[6] << 8) + buf[5];
928
929 if (bit_cnt < abort_cnt) {
930 abort_cnt = 1000;
Antti Palosaari09611ca2014-08-31 02:18:34 -0300931 dev->ber = 0xffffffff;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300932 } else {
Antti Palosaari24e419a2014-08-09 14:59:53 -0300933 /*
934 * 8 byte packets, that have not been rejected already
935 */
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300936 bit_cnt -= (u32)abort_cnt;
937 if (bit_cnt == 0) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300938 dev->ber = 0xffffffff;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300939 } else {
940 err_cnt -= (u32)abort_cnt * 8 * 8;
941 bit_cnt *= 8 * 8;
Antti Palosaari09611ca2014-08-31 02:18:34 -0300942 dev->ber = err_cnt * (0xffffffff / bit_cnt);
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300943 }
944 }
Antti Palosaari09611ca2014-08-31 02:18:34 -0300945 dev->ucb += abort_cnt;
946 dev->last_stat_check = jiffies;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300947 }
948
949 return 0;
950err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300951 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300952
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300953 return ret;
954}
955
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300956static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
957{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300958 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300959 int ret;
960
Antti Palosaari09611ca2014-08-31 02:18:34 -0300961 ret = af9033_update_ch_stat(dev);
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300962 if (ret < 0)
963 return ret;
964
Antti Palosaari09611ca2014-08-31 02:18:34 -0300965 *ber = dev->ber;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300966
967 return 0;
968}
969
970static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
971{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300972 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300973 int ret;
974
Antti Palosaari09611ca2014-08-31 02:18:34 -0300975 ret = af9033_update_ch_stat(dev);
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300976 if (ret < 0)
977 return ret;
978
Antti Palosaari09611ca2014-08-31 02:18:34 -0300979 *ucblocks = dev->ucb;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300980
981 return 0;
982}
983
984static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
985{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300986 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300987 int ret;
988
Antti Palosaari09611ca2014-08-31 02:18:34 -0300989 dev_dbg(&dev->i2c->dev, "%s: enable=%d\n", __func__, enable);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300990
Antti Palosaari09611ca2014-08-31 02:18:34 -0300991 ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300992 if (ret < 0)
993 goto err;
994
995 return 0;
996
997err:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300998 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300999
1000 return ret;
1001}
1002
Mauro Carvalho Chehabed97a6f2014-03-14 14:29:06 -03001003static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -03001004{
Antti Palosaari09611ca2014-08-31 02:18:34 -03001005 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari040cf862014-02-13 15:40:59 -03001006 int ret;
1007
Antti Palosaari09611ca2014-08-31 02:18:34 -03001008 dev_dbg(&dev->i2c->dev, "%s: onoff=%d\n", __func__, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -03001009
Antti Palosaari09611ca2014-08-31 02:18:34 -03001010 ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01);
Antti Palosaari040cf862014-02-13 15:40:59 -03001011 if (ret < 0)
1012 goto err;
1013
1014 return 0;
1015
1016err:
Antti Palosaari09611ca2014-08-31 02:18:34 -03001017 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari040cf862014-02-13 15:40:59 -03001018
1019 return ret;
1020}
Antti Palosaari040cf862014-02-13 15:40:59 -03001021
Antti Palosaari24e419a2014-08-09 14:59:53 -03001022static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
1023 int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -03001024{
Antti Palosaari09611ca2014-08-31 02:18:34 -03001025 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari040cf862014-02-13 15:40:59 -03001026 int ret;
1027 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1028
Antti Palosaari09611ca2014-08-31 02:18:34 -03001029 dev_dbg(&dev->i2c->dev, "%s: index=%d pid=%04x onoff=%d\n",
Antti Palosaari040cf862014-02-13 15:40:59 -03001030 __func__, index, pid, onoff);
1031
1032 if (pid > 0x1fff)
1033 return 0;
1034
Antti Palosaari09611ca2014-08-31 02:18:34 -03001035 ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2);
Antti Palosaari040cf862014-02-13 15:40:59 -03001036 if (ret < 0)
1037 goto err;
1038
Antti Palosaari09611ca2014-08-31 02:18:34 -03001039 ret = af9033_wr_reg(dev, 0x80f994, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -03001040 if (ret < 0)
1041 goto err;
1042
Antti Palosaari09611ca2014-08-31 02:18:34 -03001043 ret = af9033_wr_reg(dev, 0x80f995, index);
Antti Palosaari040cf862014-02-13 15:40:59 -03001044 if (ret < 0)
1045 goto err;
1046
1047 return 0;
1048
1049err:
Antti Palosaari09611ca2014-08-31 02:18:34 -03001050 dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari040cf862014-02-13 15:40:59 -03001051
1052 return ret;
1053}
Antti Palosaari040cf862014-02-13 15:40:59 -03001054
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001055static struct dvb_frontend_ops af9033_ops;
1056
1057struct dvb_frontend *af9033_attach(const struct af9033_config *config,
Mauro Carvalho Chehabed97a6f2014-03-14 14:29:06 -03001058 struct i2c_adapter *i2c,
1059 struct af9033_ops *ops)
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001060{
1061 int ret;
Antti Palosaari09611ca2014-08-31 02:18:34 -03001062 struct af9033_dev *dev;
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001063 u8 buf[8];
Antti Palosaarief5211f2014-08-31 02:03:05 -03001064 u32 reg;
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001065
Antti Palosaari0a73f2d2012-09-12 20:23:42 -03001066 dev_dbg(&i2c->dev, "%s:\n", __func__);
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001067
1068 /* allocate memory for the internal state */
Antti Palosaari09611ca2014-08-31 02:18:34 -03001069 dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL);
1070 if (dev == NULL)
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001071 goto err;
1072
1073 /* setup the state */
Antti Palosaari09611ca2014-08-31 02:18:34 -03001074 dev->i2c = i2c;
1075 memcpy(&dev->cfg, config, sizeof(struct af9033_config));
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001076
Antti Palosaari09611ca2014-08-31 02:18:34 -03001077 if (dev->cfg.clock != 12000000) {
1078 dev_err(&dev->i2c->dev,
Antti Palosaari24e419a2014-08-09 14:59:53 -03001079 "%s: af9033: unsupported clock=%d, only 12000000 Hz is supported currently\n",
Antti Palosaari09611ca2014-08-31 02:18:34 -03001080 KBUILD_MODNAME, dev->cfg.clock);
Antti Palosaari8e8a5ac2012-04-01 14:13:36 -03001081 goto err;
1082 }
1083
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001084 /* firmware version */
Antti Palosaari09611ca2014-08-31 02:18:34 -03001085 switch (dev->cfg.tuner) {
Antti Palosaarief5211f2014-08-31 02:03:05 -03001086 case AF9033_TUNER_IT9135_38:
1087 case AF9033_TUNER_IT9135_51:
1088 case AF9033_TUNER_IT9135_52:
1089 case AF9033_TUNER_IT9135_60:
1090 case AF9033_TUNER_IT9135_61:
1091 case AF9033_TUNER_IT9135_62:
1092 reg = 0x004bfc;
1093 break;
1094 default:
1095 reg = 0x0083e9;
1096 break;
1097 }
1098
Antti Palosaari09611ca2014-08-31 02:18:34 -03001099 ret = af9033_rd_regs(dev, reg, &buf[0], 4);
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001100 if (ret < 0)
1101 goto err;
1102
Antti Palosaari09611ca2014-08-31 02:18:34 -03001103 ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4);
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001104 if (ret < 0)
1105 goto err;
1106
Antti Palosaari09611ca2014-08-31 02:18:34 -03001107 dev_info(&dev->i2c->dev,
Antti Palosaari24e419a2014-08-09 14:59:53 -03001108 "%s: firmware version: LINK=%d.%d.%d.%d OFDM=%d.%d.%d.%d\n",
1109 KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3], buf[4],
1110 buf[5], buf[6], buf[7]);
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001111
Antti Palosaari0c13c542013-03-07 23:13:31 -03001112 /* sleep */
Antti Palosaari09611ca2014-08-31 02:18:34 -03001113 switch (dev->cfg.tuner) {
Antti Palosaari0c13c542013-03-07 23:13:31 -03001114 case AF9033_TUNER_IT9135_38:
1115 case AF9033_TUNER_IT9135_51:
1116 case AF9033_TUNER_IT9135_52:
1117 case AF9033_TUNER_IT9135_60:
1118 case AF9033_TUNER_IT9135_61:
1119 case AF9033_TUNER_IT9135_62:
1120 /* IT9135 did not like to sleep at that early */
1121 break;
1122 default:
Antti Palosaari09611ca2014-08-31 02:18:34 -03001123 ret = af9033_wr_reg(dev, 0x80004c, 1);
Antti Palosaari4902bb32013-01-07 09:48:03 -03001124 if (ret < 0)
1125 goto err;
1126
Antti Palosaari09611ca2014-08-31 02:18:34 -03001127 ret = af9033_wr_reg(dev, 0x800000, 0);
Antti Palosaari4902bb32013-01-07 09:48:03 -03001128 if (ret < 0)
1129 goto err;
1130 }
Antti Palosaari12897dc2012-09-16 22:26:57 -03001131
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001132 /* configure internal TS mode */
Antti Palosaari09611ca2014-08-31 02:18:34 -03001133 switch (dev->cfg.ts_mode) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001134 case AF9033_TS_MODE_PARALLEL:
Antti Palosaari09611ca2014-08-31 02:18:34 -03001135 dev->ts_mode_parallel = true;
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001136 break;
1137 case AF9033_TS_MODE_SERIAL:
Antti Palosaari09611ca2014-08-31 02:18:34 -03001138 dev->ts_mode_serial = true;
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001139 break;
1140 case AF9033_TS_MODE_USB:
1141 /* usb mode for AF9035 */
1142 default:
1143 break;
1144 }
1145
1146 /* create dvb_frontend */
Antti Palosaari09611ca2014-08-31 02:18:34 -03001147 memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
1148 dev->fe.demodulator_priv = dev;
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001149
Mauro Carvalho Chehabed97a6f2014-03-14 14:29:06 -03001150 if (ops) {
1151 ops->pid_filter = af9033_pid_filter;
1152 ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1153 }
1154
Antti Palosaari09611ca2014-08-31 02:18:34 -03001155 return &dev->fe;
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001156
1157err:
Antti Palosaari09611ca2014-08-31 02:18:34 -03001158 kfree(dev);
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001159 return NULL;
1160}
1161EXPORT_SYMBOL(af9033_attach);
1162
1163static struct dvb_frontend_ops af9033_ops = {
1164 .delsys = { SYS_DVBT },
1165 .info = {
1166 .name = "Afatech AF9033 (DVB-T)",
1167 .frequency_min = 174000000,
1168 .frequency_max = 862000000,
1169 .frequency_stepsize = 250000,
1170 .frequency_tolerance = 0,
1171 .caps = FE_CAN_FEC_1_2 |
1172 FE_CAN_FEC_2_3 |
1173 FE_CAN_FEC_3_4 |
1174 FE_CAN_FEC_5_6 |
1175 FE_CAN_FEC_7_8 |
1176 FE_CAN_FEC_AUTO |
1177 FE_CAN_QPSK |
1178 FE_CAN_QAM_16 |
1179 FE_CAN_QAM_64 |
1180 FE_CAN_QAM_AUTO |
1181 FE_CAN_TRANSMISSION_MODE_AUTO |
1182 FE_CAN_GUARD_INTERVAL_AUTO |
1183 FE_CAN_HIERARCHY_AUTO |
1184 FE_CAN_RECOVER |
1185 FE_CAN_MUTE_TS
1186 },
1187
1188 .release = af9033_release,
1189
1190 .init = af9033_init,
1191 .sleep = af9033_sleep,
1192
1193 .get_tune_settings = af9033_get_tune_settings,
1194 .set_frontend = af9033_set_frontend,
Gianluca Gennari0a4df232012-04-05 12:47:19 -03001195 .get_frontend = af9033_get_frontend,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001196
1197 .read_status = af9033_read_status,
1198 .read_snr = af9033_read_snr,
1199 .read_signal_strength = af9033_read_signal_strength,
1200 .read_ber = af9033_read_ber,
1201 .read_ucblocks = af9033_read_ucblocks,
1202
1203 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1204};
1205
1206MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1207MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1208MODULE_LICENSE("GPL");