blob: 4c2061676bbe5d9fa0ccb715dabb3cffd2e89571 [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 Palosaarif5b00a72014-08-31 20:57:05 -030028 struct i2c_client *client;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030029 struct dvb_frontend fe;
30 struct af9033_config cfg;
Antti Palosaari83f11612014-09-02 03:55:21 -030031 bool is_af9035;
32 bool is_it9135;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030033
34 u32 bandwidth_hz;
35 bool ts_mode_parallel;
36 bool ts_mode_serial;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -030037
Antti Palosaari83f11612014-09-02 03:55:21 -030038 fe_status_t fe_status;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -030039 u32 ber;
40 u32 ucb;
Antti Palosaari83f11612014-09-02 03:55:21 -030041 struct delayed_work stat_work;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -030042 unsigned long last_stat_check;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030043};
44
45/* write multiple registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -030046static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030047 int len)
48{
49 int ret;
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030050 u8 buf[MAX_XFER_SIZE];
Antti Palosaari4b64bb22012-03-30 08:21:25 -030051 struct i2c_msg msg[1] = {
52 {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030053 .addr = dev->client->addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030054 .flags = 0,
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030055 .len = 3 + len,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030056 .buf = buf,
57 }
58 };
59
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030060 if (3 + len > sizeof(buf)) {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030061 dev_warn(&dev->client->dev,
Antti Palosaari6a087f12014-08-31 21:08:09 -030062 "i2c wr reg=%04x: len=%d is too big!\n",
63 reg, len);
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030064 return -EINVAL;
65 }
66
Antti Palosaari4b64bb22012-03-30 08:21:25 -030067 buf[0] = (reg >> 16) & 0xff;
68 buf[1] = (reg >> 8) & 0xff;
69 buf[2] = (reg >> 0) & 0xff;
70 memcpy(&buf[3], val, len);
71
Antti Palosaarif5b00a72014-08-31 20:57:05 -030072 ret = i2c_transfer(dev->client->adapter, msg, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -030073 if (ret == 1) {
74 ret = 0;
75 } else {
Antti Palosaari6a087f12014-08-31 21:08:09 -030076 dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n",
77 ret, reg, len);
Antti Palosaari4b64bb22012-03-30 08:21:25 -030078 ret = -EREMOTEIO;
79 }
80
81 return ret;
82}
83
84/* read multiple registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -030085static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len)
Antti Palosaari4b64bb22012-03-30 08:21:25 -030086{
87 int ret;
88 u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
89 (reg >> 0) & 0xff };
90 struct i2c_msg msg[2] = {
91 {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030092 .addr = dev->client->addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030093 .flags = 0,
94 .len = sizeof(buf),
95 .buf = buf
96 }, {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030097 .addr = dev->client->addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030098 .flags = I2C_M_RD,
99 .len = len,
100 .buf = val
101 }
102 };
103
Antti Palosaarif5b00a72014-08-31 20:57:05 -0300104 ret = i2c_transfer(dev->client->adapter, msg, 2);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300105 if (ret == 2) {
106 ret = 0;
107 } else {
Antti Palosaari6a087f12014-08-31 21:08:09 -0300108 dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n",
109 ret, reg, len);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300110 ret = -EREMOTEIO;
111 }
112
113 return ret;
114}
115
116
117/* write single register */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300118static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300119{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300120 return af9033_wr_regs(dev, reg, &val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300121}
122
123/* read single register */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300124static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300125{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300126 return af9033_rd_regs(dev, reg, val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300127}
128
129/* write single register with mask */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300130static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300131 u8 mask)
132{
133 int ret;
134 u8 tmp;
135
136 /* no need for read if whole reg is written */
137 if (mask != 0xff) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300138 ret = af9033_rd_regs(dev, reg, &tmp, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300139 if (ret)
140 return ret;
141
142 val &= mask;
143 tmp &= ~mask;
144 val |= tmp;
145 }
146
Antti Palosaari09611ca2014-08-31 02:18:34 -0300147 return af9033_wr_regs(dev, reg, &val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300148}
149
150/* read single register with mask */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300151static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300152 u8 mask)
153{
154 int ret, i;
155 u8 tmp;
156
Antti Palosaari09611ca2014-08-31 02:18:34 -0300157 ret = af9033_rd_regs(dev, reg, &tmp, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300158 if (ret)
159 return ret;
160
161 tmp &= mask;
162
163 /* find position of the first bit */
164 for (i = 0; i < 8; i++) {
165 if ((mask >> i) & 0x01)
166 break;
167 }
168 *val = tmp >> i;
169
170 return 0;
171}
172
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300173/* write reg val table using reg addr auto increment */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300174static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300175 const struct reg_val *tab, int tab_len)
176{
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300177#define MAX_TAB_LEN 212
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300178 int ret, i, j;
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300179 u8 buf[1 + MAX_TAB_LEN];
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300180
Antti Palosaari6a087f12014-08-31 21:08:09 -0300181 dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300182
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300183 if (tab_len > sizeof(buf)) {
Antti Palosaari6a087f12014-08-31 21:08:09 -0300184 dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len);
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300185 return -EINVAL;
186 }
187
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300188 for (i = 0, j = 0; i < tab_len; i++) {
189 buf[j] = tab[i].val;
190
191 if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300192 ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300193 if (ret < 0)
194 goto err;
195
196 j = 0;
197 } else {
198 j++;
199 }
200 }
201
202 return 0;
203
204err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300205 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300206
207 return ret;
208}
209
Antti Palosaari09611ca2014-08-31 02:18:34 -0300210static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300211{
212 u32 r = 0, c = 0, i;
213
Antti Palosaari6a087f12014-08-31 21:08:09 -0300214 dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300215
216 if (a > b) {
217 c = a / b;
218 a = a - c * b;
219 }
220
221 for (i = 0; i < x; i++) {
222 if (a >= b) {
223 r += 1;
224 a -= b;
225 }
226 a <<= 1;
227 r <<= 1;
228 }
229 r = (c << (u32)x) + r;
230
Antti Palosaari6a087f12014-08-31 21:08:09 -0300231 dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300232
233 return r;
234}
235
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300236static int af9033_init(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 int ret, i, len;
240 const struct reg_val *init;
241 u8 buf[4];
242 u32 adc_cw, clock_cw;
243 struct reg_val_mask tab[] = {
244 { 0x80fb24, 0x00, 0x08 },
245 { 0x80004c, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300246 { 0x00f641, dev->cfg.tuner, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300247 { 0x80f5ca, 0x01, 0x01 },
248 { 0x80f715, 0x01, 0x01 },
249 { 0x00f41f, 0x04, 0x04 },
250 { 0x00f41a, 0x01, 0x01 },
251 { 0x80f731, 0x00, 0x01 },
252 { 0x00d91e, 0x00, 0x01 },
253 { 0x00d919, 0x00, 0x01 },
254 { 0x80f732, 0x00, 0x01 },
255 { 0x00d91f, 0x00, 0x01 },
256 { 0x00d91a, 0x00, 0x01 },
257 { 0x80f730, 0x00, 0x01 },
258 { 0x80f778, 0x00, 0xff },
259 { 0x80f73c, 0x01, 0x01 },
260 { 0x80f776, 0x00, 0x01 },
261 { 0x00d8fd, 0x01, 0xff },
262 { 0x00d830, 0x01, 0xff },
263 { 0x00d831, 0x00, 0xff },
264 { 0x00d832, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300265 { 0x80f985, dev->ts_mode_serial, 0x01 },
266 { 0x80f986, dev->ts_mode_parallel, 0x01 },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300267 { 0x00d827, 0x00, 0xff },
268 { 0x00d829, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300269 { 0x800045, dev->cfg.adc_multiplier, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300270 };
271
272 /* program clock control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300273 clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300274 buf[0] = (clock_cw >> 0) & 0xff;
275 buf[1] = (clock_cw >> 8) & 0xff;
276 buf[2] = (clock_cw >> 16) & 0xff;
277 buf[3] = (clock_cw >> 24) & 0xff;
278
Antti Palosaari6a087f12014-08-31 21:08:09 -0300279 dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n",
280 dev->cfg.clock, clock_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300281
Antti Palosaari09611ca2014-08-31 02:18:34 -0300282 ret = af9033_wr_regs(dev, 0x800025, buf, 4);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300283 if (ret < 0)
284 goto err;
285
286 /* program ADC control */
287 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300288 if (clock_adc_lut[i].clock == dev->cfg.clock)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300289 break;
290 }
291
Antti Palosaari09611ca2014-08-31 02:18:34 -0300292 adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300293 buf[0] = (adc_cw >> 0) & 0xff;
294 buf[1] = (adc_cw >> 8) & 0xff;
295 buf[2] = (adc_cw >> 16) & 0xff;
296
Antti Palosaari6a087f12014-08-31 21:08:09 -0300297 dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n",
298 clock_adc_lut[i].adc, adc_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300299
Antti Palosaari09611ca2014-08-31 02:18:34 -0300300 ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300301 if (ret < 0)
302 goto err;
303
304 /* program register table */
305 for (i = 0; i < ARRAY_SIZE(tab); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300306 ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300307 tab[i].mask);
308 if (ret < 0)
309 goto err;
310 }
311
Antti Palosaarica681fe2014-08-08 23:56:49 -0300312 /* clock output */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300313 if (dev->cfg.dyn0_clk) {
314 ret = af9033_wr_reg(dev, 0x80fba8, 0x00);
Antti Palosaari9dc0f3f2014-08-09 01:03:14 -0300315 if (ret < 0)
316 goto err;
317 }
318
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300319 /* settings for TS interface */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300320 if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
321 ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300322 if (ret < 0)
323 goto err;
324
Antti Palosaari09611ca2014-08-31 02:18:34 -0300325 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300326 if (ret < 0)
327 goto err;
328 } else {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300329 ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300330 if (ret < 0)
331 goto err;
332
Antti Palosaari09611ca2014-08-31 02:18:34 -0300333 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300334 if (ret < 0)
335 goto err;
336 }
337
338 /* load OFSM settings */
Antti Palosaari6a087f12014-08-31 21:08:09 -0300339 dev_dbg(&dev->client->dev, "load ofsm settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300340 switch (dev->cfg.tuner) {
Antti Palosaarife8eece2013-02-03 13:39:55 -0300341 case AF9033_TUNER_IT9135_38:
342 case AF9033_TUNER_IT9135_51:
343 case AF9033_TUNER_IT9135_52:
Antti Palosaari463c3992013-03-07 00:03:06 -0300344 len = ARRAY_SIZE(ofsm_init_it9135_v1);
345 init = ofsm_init_it9135_v1;
346 break;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300347 case AF9033_TUNER_IT9135_60:
348 case AF9033_TUNER_IT9135_61:
349 case AF9033_TUNER_IT9135_62:
Antti Palosaari463c3992013-03-07 00:03:06 -0300350 len = ARRAY_SIZE(ofsm_init_it9135_v2);
351 init = ofsm_init_it9135_v2;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300352 break;
353 default:
354 len = ARRAY_SIZE(ofsm_init);
355 init = ofsm_init;
356 break;
357 }
358
Antti Palosaari09611ca2014-08-31 02:18:34 -0300359 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300360 if (ret < 0)
361 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300362
363 /* load tuner specific settings */
Antti Palosaari6a087f12014-08-31 21:08:09 -0300364 dev_dbg(&dev->client->dev, "load tuner specific settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300365 switch (dev->cfg.tuner) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300366 case AF9033_TUNER_TUA9001:
367 len = ARRAY_SIZE(tuner_init_tua9001);
368 init = tuner_init_tua9001;
369 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300370 case AF9033_TUNER_FC0011:
371 len = ARRAY_SIZE(tuner_init_fc0011);
372 init = tuner_init_fc0011;
373 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300374 case AF9033_TUNER_MXL5007T:
375 len = ARRAY_SIZE(tuner_init_mxl5007t);
376 init = tuner_init_mxl5007t;
377 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300378 case AF9033_TUNER_TDA18218:
379 len = ARRAY_SIZE(tuner_init_tda18218);
380 init = tuner_init_tda18218;
381 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300382 case AF9033_TUNER_FC2580:
383 len = ARRAY_SIZE(tuner_init_fc2580);
384 init = tuner_init_fc2580;
385 break;
Antti Palosaarie713ad12012-12-02 18:47:00 -0300386 case AF9033_TUNER_FC0012:
387 len = ARRAY_SIZE(tuner_init_fc0012);
388 init = tuner_init_fc0012;
389 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300390 case AF9033_TUNER_IT9135_38:
Antti Palosaaria72cbb72013-03-07 21:17:55 -0300391 len = ARRAY_SIZE(tuner_init_it9135_38);
392 init = tuner_init_it9135_38;
393 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300394 case AF9033_TUNER_IT9135_51:
Antti Palosaaribb2e12a2013-03-07 21:25:16 -0300395 len = ARRAY_SIZE(tuner_init_it9135_51);
396 init = tuner_init_it9135_51;
397 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300398 case AF9033_TUNER_IT9135_52:
Antti Palosaari22d729f2013-03-07 21:29:05 -0300399 len = ARRAY_SIZE(tuner_init_it9135_52);
400 init = tuner_init_it9135_52;
401 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300402 case AF9033_TUNER_IT9135_60:
Antti Palosaaria49f53a2013-03-07 21:38:00 -0300403 len = ARRAY_SIZE(tuner_init_it9135_60);
404 init = tuner_init_it9135_60;
405 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300406 case AF9033_TUNER_IT9135_61:
Antti Palosaari85211322013-03-07 21:44:30 -0300407 len = ARRAY_SIZE(tuner_init_it9135_61);
408 init = tuner_init_it9135_61;
409 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300410 case AF9033_TUNER_IT9135_62:
Antti Palosaaridc4a2c42013-03-07 21:47:20 -0300411 len = ARRAY_SIZE(tuner_init_it9135_62);
412 init = tuner_init_it9135_62;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300413 break;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300414 default:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300415 dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n",
416 dev->cfg.tuner);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300417 ret = -ENODEV;
418 goto err;
419 }
420
Antti Palosaari09611ca2014-08-31 02:18:34 -0300421 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300422 if (ret < 0)
423 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300424
Antti Palosaari09611ca2014-08-31 02:18:34 -0300425 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
426 ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300427 if (ret < 0)
428 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300429
Antti Palosaari09611ca2014-08-31 02:18:34 -0300430 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300431 if (ret < 0)
432 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300433
Antti Palosaari09611ca2014-08-31 02:18:34 -0300434 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300435 if (ret < 0)
436 goto err;
437 }
438
Antti Palosaari09611ca2014-08-31 02:18:34 -0300439 switch (dev->cfg.tuner) {
Antti Palosaari086991d2013-03-07 22:18:56 -0300440 case AF9033_TUNER_IT9135_60:
441 case AF9033_TUNER_IT9135_61:
442 case AF9033_TUNER_IT9135_62:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300443 ret = af9033_wr_reg(dev, 0x800000, 0x01);
Antti Palosaari086991d2013-03-07 22:18:56 -0300444 if (ret < 0)
445 goto err;
446 }
447
Antti Palosaari09611ca2014-08-31 02:18:34 -0300448 dev->bandwidth_hz = 0; /* force to program all parameters */
Antti Palosaari83f11612014-09-02 03:55:21 -0300449 /* start statistics polling */
450 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300451
452 return 0;
453
454err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300455 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300456
457 return ret;
458}
459
460static int af9033_sleep(struct dvb_frontend *fe)
461{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300462 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300463 int ret, i;
464 u8 tmp;
465
Antti Palosaari83f11612014-09-02 03:55:21 -0300466 /* stop statistics polling */
467 cancel_delayed_work_sync(&dev->stat_work);
468
Antti Palosaari09611ca2014-08-31 02:18:34 -0300469 ret = af9033_wr_reg(dev, 0x80004c, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300470 if (ret < 0)
471 goto err;
472
Antti Palosaari09611ca2014-08-31 02:18:34 -0300473 ret = af9033_wr_reg(dev, 0x800000, 0);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300474 if (ret < 0)
475 goto err;
476
477 for (i = 100, tmp = 1; i && tmp; i--) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300478 ret = af9033_rd_reg(dev, 0x80004c, &tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300479 if (ret < 0)
480 goto err;
481
482 usleep_range(200, 10000);
483 }
484
Antti Palosaari6a087f12014-08-31 21:08:09 -0300485 dev_dbg(&dev->client->dev, "loop=%d\n", i);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300486
487 if (i == 0) {
488 ret = -ETIMEDOUT;
489 goto err;
490 }
491
Antti Palosaari09611ca2014-08-31 02:18:34 -0300492 ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300493 if (ret < 0)
494 goto err;
495
496 /* prevent current leak (?) */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300497 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300498 /* enable parallel TS */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300499 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300500 if (ret < 0)
501 goto err;
502
Antti Palosaari09611ca2014-08-31 02:18:34 -0300503 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300504 if (ret < 0)
505 goto err;
506 }
507
508 return 0;
509
510err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300511 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300512
513 return ret;
514}
515
516static int af9033_get_tune_settings(struct dvb_frontend *fe,
517 struct dvb_frontend_tune_settings *fesettings)
518{
Antti Palosaarife8eece2013-02-03 13:39:55 -0300519 /* 800 => 2000 because IT9135 v2 is slow to gain lock */
520 fesettings->min_delay_ms = 2000;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300521 fesettings->step_size = 0;
522 fesettings->max_drift = 0;
523
524 return 0;
525}
526
527static int af9033_set_frontend(struct dvb_frontend *fe)
528{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300529 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300530 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300531 int ret, i, spec_inv, sampling_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300532 u8 tmp, buf[3], bandwidth_reg_val;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300533 u32 if_frequency, freq_cw, adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300534
Antti Palosaari6a087f12014-08-31 21:08:09 -0300535 dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n",
536 c->frequency, c->bandwidth_hz);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300537
538 /* check bandwidth */
539 switch (c->bandwidth_hz) {
540 case 6000000:
541 bandwidth_reg_val = 0x00;
542 break;
543 case 7000000:
544 bandwidth_reg_val = 0x01;
545 break;
546 case 8000000:
547 bandwidth_reg_val = 0x02;
548 break;
549 default:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300550 dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n");
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300551 ret = -EINVAL;
552 goto err;
553 }
554
555 /* program tuner */
556 if (fe->ops.tuner_ops.set_params)
557 fe->ops.tuner_ops.set_params(fe);
558
559 /* program CFOE coefficients */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300560 if (c->bandwidth_hz != dev->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300561 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300562 if (coeff_lut[i].clock == dev->cfg.clock &&
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300563 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
564 break;
565 }
566 }
Antti Palosaari09611ca2014-08-31 02:18:34 -0300567 ret = af9033_wr_regs(dev, 0x800001,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300568 coeff_lut[i].val, sizeof(coeff_lut[i].val));
569 }
570
571 /* program frequency control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300572 if (c->bandwidth_hz != dev->bandwidth_hz) {
573 spec_inv = dev->cfg.spec_inv ? -1 : 1;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300574
575 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300576 if (clock_adc_lut[i].clock == dev->cfg.clock)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300577 break;
578 }
579 adc_freq = clock_adc_lut[i].adc;
580
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300581 /* get used IF frequency */
582 if (fe->ops.tuner_ops.get_if_frequency)
583 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
584 else
585 if_frequency = 0;
586
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300587 sampling_freq = if_frequency;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300588
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300589 while (sampling_freq > (adc_freq / 2))
590 sampling_freq -= adc_freq;
591
592 if (sampling_freq >= 0)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300593 spec_inv *= -1;
594 else
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300595 sampling_freq *= -1;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300596
Antti Palosaari09611ca2014-08-31 02:18:34 -0300597 freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300598
599 if (spec_inv == -1)
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300600 freq_cw = 0x800000 - freq_cw;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300601
Antti Palosaari09611ca2014-08-31 02:18:34 -0300602 if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300603 freq_cw /= 2;
604
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300605 buf[0] = (freq_cw >> 0) & 0xff;
606 buf[1] = (freq_cw >> 8) & 0xff;
607 buf[2] = (freq_cw >> 16) & 0x7f;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300608
609 /* FIXME: there seems to be calculation error here... */
610 if (if_frequency == 0)
611 buf[2] = 0;
612
Antti Palosaari09611ca2014-08-31 02:18:34 -0300613 ret = af9033_wr_regs(dev, 0x800029, buf, 3);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300614 if (ret < 0)
615 goto err;
616
Antti Palosaari09611ca2014-08-31 02:18:34 -0300617 dev->bandwidth_hz = c->bandwidth_hz;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300618 }
619
Antti Palosaari09611ca2014-08-31 02:18:34 -0300620 ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300621 if (ret < 0)
622 goto err;
623
Antti Palosaari09611ca2014-08-31 02:18:34 -0300624 ret = af9033_wr_reg(dev, 0x800040, 0x00);
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, 0x800047, 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_mask(dev, 0x80f999, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300633 if (ret < 0)
634 goto err;
635
636 if (c->frequency <= 230000000)
637 tmp = 0x00; /* VHF */
638 else
639 tmp = 0x01; /* UHF */
640
Antti Palosaari09611ca2014-08-31 02:18:34 -0300641 ret = af9033_wr_reg(dev, 0x80004b, tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300642 if (ret < 0)
643 goto err;
644
Antti Palosaari09611ca2014-08-31 02:18:34 -0300645 ret = af9033_wr_reg(dev, 0x800000, 0x00);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300646 if (ret < 0)
647 goto err;
648
649 return 0;
650
651err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300652 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300653
654 return ret;
655}
656
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300657static int af9033_get_frontend(struct dvb_frontend *fe)
658{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300659 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300660 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300661 int ret;
662 u8 buf[8];
663
Antti Palosaari6a087f12014-08-31 21:08:09 -0300664 dev_dbg(&dev->client->dev, "\n");
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300665
666 /* read all needed registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300667 ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf));
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300668 if (ret < 0)
669 goto err;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300670
671 switch ((buf[0] >> 0) & 3) {
672 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300673 c->transmission_mode = TRANSMISSION_MODE_2K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300674 break;
675 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300676 c->transmission_mode = TRANSMISSION_MODE_8K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300677 break;
678 }
679
680 switch ((buf[1] >> 0) & 3) {
681 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300682 c->guard_interval = GUARD_INTERVAL_1_32;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300683 break;
684 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300685 c->guard_interval = GUARD_INTERVAL_1_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300686 break;
687 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300688 c->guard_interval = GUARD_INTERVAL_1_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300689 break;
690 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300691 c->guard_interval = GUARD_INTERVAL_1_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300692 break;
693 }
694
695 switch ((buf[2] >> 0) & 7) {
696 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300697 c->hierarchy = HIERARCHY_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300698 break;
699 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300700 c->hierarchy = HIERARCHY_1;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300701 break;
702 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300703 c->hierarchy = HIERARCHY_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300704 break;
705 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300706 c->hierarchy = HIERARCHY_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300707 break;
708 }
709
710 switch ((buf[3] >> 0) & 3) {
711 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300712 c->modulation = QPSK;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300713 break;
714 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300715 c->modulation = QAM_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300716 break;
717 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300718 c->modulation = QAM_64;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300719 break;
720 }
721
722 switch ((buf[4] >> 0) & 3) {
723 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300724 c->bandwidth_hz = 6000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300725 break;
726 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300727 c->bandwidth_hz = 7000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300728 break;
729 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300730 c->bandwidth_hz = 8000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300731 break;
732 }
733
734 switch ((buf[6] >> 0) & 7) {
735 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300736 c->code_rate_HP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300737 break;
738 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300739 c->code_rate_HP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300740 break;
741 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300742 c->code_rate_HP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300743 break;
744 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300745 c->code_rate_HP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300746 break;
747 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300748 c->code_rate_HP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300749 break;
750 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300751 c->code_rate_HP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300752 break;
753 }
754
755 switch ((buf[7] >> 0) & 7) {
756 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300757 c->code_rate_LP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300758 break;
759 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300760 c->code_rate_LP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300761 break;
762 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300763 c->code_rate_LP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300764 break;
765 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300766 c->code_rate_LP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300767 break;
768 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300769 c->code_rate_LP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300770 break;
771 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300772 c->code_rate_LP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300773 break;
774 }
775
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300776 return 0;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300777
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300778err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300779 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300780
781 return ret;
782}
783
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300784static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
785{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300786 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300787 int ret;
788 u8 tmp;
789
790 *status = 0;
791
792 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300793 ret = af9033_rd_reg(dev, 0x800047, &tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300794 if (ret < 0)
795 goto err;
796
797 /* has signal */
798 if (tmp == 0x01)
799 *status |= FE_HAS_SIGNAL;
800
801 if (tmp != 0x02) {
802 /* TPS lock */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300803 ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300804 if (ret < 0)
805 goto err;
806
807 if (tmp)
808 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
809 FE_HAS_VITERBI;
810
811 /* full lock */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300812 ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300813 if (ret < 0)
814 goto err;
815
816 if (tmp)
817 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
818 FE_HAS_VITERBI | FE_HAS_SYNC |
819 FE_HAS_LOCK;
820 }
821
Antti Palosaari83f11612014-09-02 03:55:21 -0300822 dev->fe_status = *status;
823
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300824 return 0;
825
826err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300827 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300828
829 return ret;
830}
831
832static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
833{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300834 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari6b457782014-09-02 05:03:21 -0300835 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300836
Antti Palosaari6b457782014-09-02 05:03:21 -0300837 /* use DVBv5 CNR */
838 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
839 *snr = div_s64(c->cnr.stat[0].svalue, 100); /* 1000x => 10x */
840 else
841 *snr = 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300842
843 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300844}
845
846static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
847{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300848 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300849 int ret;
850 u8 strength2;
851
852 /* read signal strength of 0-100 scale */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300853 ret = af9033_rd_reg(dev, 0x800048, &strength2);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300854 if (ret < 0)
855 goto err;
856
857 /* scale value to 0x0000-0xffff */
858 *strength = strength2 * 0xffff / 100;
859
860 return 0;
861
862err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300863 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300864
865 return ret;
866}
867
Antti Palosaari09611ca2014-08-31 02:18:34 -0300868static int af9033_update_ch_stat(struct af9033_dev *dev)
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300869{
870 int ret = 0;
871 u32 err_cnt, bit_cnt;
872 u16 abort_cnt;
873 u8 buf[7];
874
875 /* only update data every half second */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300876 if (time_after(jiffies, dev->last_stat_check + msecs_to_jiffies(500))) {
877 ret = af9033_rd_regs(dev, 0x800032, buf, sizeof(buf));
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300878 if (ret < 0)
879 goto err;
880 /* in 8 byte packets? */
881 abort_cnt = (buf[1] << 8) + buf[0];
882 /* in bits */
883 err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
884 /* in 8 byte packets? always(?) 0x2710 = 10000 */
885 bit_cnt = (buf[6] << 8) + buf[5];
886
887 if (bit_cnt < abort_cnt) {
888 abort_cnt = 1000;
Antti Palosaari09611ca2014-08-31 02:18:34 -0300889 dev->ber = 0xffffffff;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300890 } else {
Antti Palosaari24e419a2014-08-09 14:59:53 -0300891 /*
892 * 8 byte packets, that have not been rejected already
893 */
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300894 bit_cnt -= (u32)abort_cnt;
895 if (bit_cnt == 0) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300896 dev->ber = 0xffffffff;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300897 } else {
898 err_cnt -= (u32)abort_cnt * 8 * 8;
899 bit_cnt *= 8 * 8;
Antti Palosaari09611ca2014-08-31 02:18:34 -0300900 dev->ber = err_cnt * (0xffffffff / bit_cnt);
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300901 }
902 }
Antti Palosaari09611ca2014-08-31 02:18:34 -0300903 dev->ucb += abort_cnt;
904 dev->last_stat_check = jiffies;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300905 }
906
907 return 0;
908err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300909 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300910
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300911 return ret;
912}
913
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300914static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
915{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300916 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300917 int ret;
918
Antti Palosaari09611ca2014-08-31 02:18:34 -0300919 ret = af9033_update_ch_stat(dev);
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300920 if (ret < 0)
921 return ret;
922
Antti Palosaari09611ca2014-08-31 02:18:34 -0300923 *ber = dev->ber;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300924
925 return 0;
926}
927
928static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
929{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300930 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300931 int ret;
932
Antti Palosaari09611ca2014-08-31 02:18:34 -0300933 ret = af9033_update_ch_stat(dev);
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300934 if (ret < 0)
935 return ret;
936
Antti Palosaari09611ca2014-08-31 02:18:34 -0300937 *ucblocks = dev->ucb;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300938
939 return 0;
940}
941
942static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
943{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300944 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300945 int ret;
946
Antti Palosaari6a087f12014-08-31 21:08:09 -0300947 dev_dbg(&dev->client->dev, "enable=%d\n", enable);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300948
Antti Palosaari09611ca2014-08-31 02:18:34 -0300949 ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300950 if (ret < 0)
951 goto err;
952
953 return 0;
954
955err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300956 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300957
958 return ret;
959}
960
Mauro Carvalho Chehabed97a6f2014-03-14 14:29:06 -0300961static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300962{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300963 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari040cf862014-02-13 15:40:59 -0300964 int ret;
965
Antti Palosaari6a087f12014-08-31 21:08:09 -0300966 dev_dbg(&dev->client->dev, "onoff=%d\n", onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300967
Antti Palosaari09611ca2014-08-31 02:18:34 -0300968 ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01);
Antti Palosaari040cf862014-02-13 15:40:59 -0300969 if (ret < 0)
970 goto err;
971
972 return 0;
973
974err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300975 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -0300976
977 return ret;
978}
Antti Palosaari040cf862014-02-13 15:40:59 -0300979
Antti Palosaari24e419a2014-08-09 14:59:53 -0300980static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
981 int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300982{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300983 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari040cf862014-02-13 15:40:59 -0300984 int ret;
985 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
986
Antti Palosaari6a087f12014-08-31 21:08:09 -0300987 dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n",
988 index, pid, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300989
990 if (pid > 0x1fff)
991 return 0;
992
Antti Palosaari09611ca2014-08-31 02:18:34 -0300993 ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2);
Antti Palosaari040cf862014-02-13 15:40:59 -0300994 if (ret < 0)
995 goto err;
996
Antti Palosaari09611ca2014-08-31 02:18:34 -0300997 ret = af9033_wr_reg(dev, 0x80f994, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300998 if (ret < 0)
999 goto err;
1000
Antti Palosaari09611ca2014-08-31 02:18:34 -03001001 ret = af9033_wr_reg(dev, 0x80f995, index);
Antti Palosaari040cf862014-02-13 15:40:59 -03001002 if (ret < 0)
1003 goto err;
1004
1005 return 0;
1006
1007err:
Antti Palosaari6a087f12014-08-31 21:08:09 -03001008 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -03001009
1010 return ret;
1011}
Antti Palosaari040cf862014-02-13 15:40:59 -03001012
Antti Palosaari83f11612014-09-02 03:55:21 -03001013static void af9033_stat_work(struct work_struct *work)
1014{
1015 struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work);
1016 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
Antti Palosaari3e413132014-09-02 04:24:41 -03001017 int ret, tmp, i, len;
1018 u8 u8tmp, buf[3];
Antti Palosaari83f11612014-09-02 03:55:21 -03001019
1020 dev_dbg(&dev->client->dev, "\n");
1021
Antti Palosaari3e413132014-09-02 04:24:41 -03001022 /* signal strength */
Antti Palosaari83f11612014-09-02 03:55:21 -03001023 if (dev->fe_status & FE_HAS_SIGNAL) {
1024 if (dev->is_af9035) {
1025 ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
1026 tmp = -u8tmp * 1000;
1027 } else {
1028 ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
1029 tmp = (u8tmp - 100) * 1000;
1030 }
1031 if (ret)
1032 goto err;
1033
1034 c->strength.len = 1;
1035 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
1036 c->strength.stat[0].svalue = tmp;
1037 } else {
1038 c->strength.len = 1;
1039 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1040 }
1041
Antti Palosaari3e413132014-09-02 04:24:41 -03001042 /* CNR */
1043 if (dev->fe_status & FE_HAS_VITERBI) {
1044 u32 snr_val;
1045 const struct val_snr *snr_lut;
1046
1047 /* read value */
1048 ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
1049 if (ret)
1050 goto err;
1051
1052 snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
1053
1054 /* read current modulation */
1055 ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
1056 if (ret)
1057 goto err;
1058
1059 switch ((u8tmp >> 0) & 3) {
1060 case 0:
1061 len = ARRAY_SIZE(qpsk_snr_lut);
1062 snr_lut = qpsk_snr_lut;
1063 break;
1064 case 1:
1065 len = ARRAY_SIZE(qam16_snr_lut);
1066 snr_lut = qam16_snr_lut;
1067 break;
1068 case 2:
1069 len = ARRAY_SIZE(qam64_snr_lut);
1070 snr_lut = qam64_snr_lut;
1071 break;
1072 default:
1073 goto err_schedule_delayed_work;
1074 }
1075
1076 for (i = 0; i < len; i++) {
1077 tmp = snr_lut[i].snr * 1000;
1078 if (snr_val < snr_lut[i].val)
1079 break;
1080 }
1081
1082 c->cnr.len = 1;
1083 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1084 c->cnr.stat[0].svalue = tmp;
1085 } else {
1086 c->cnr.len = 1;
1087 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1088 }
1089
1090err_schedule_delayed_work:
Antti Palosaari83f11612014-09-02 03:55:21 -03001091 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
1092 return;
1093err:
1094 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1095}
1096
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001097static struct dvb_frontend_ops af9033_ops = {
1098 .delsys = { SYS_DVBT },
1099 .info = {
1100 .name = "Afatech AF9033 (DVB-T)",
1101 .frequency_min = 174000000,
1102 .frequency_max = 862000000,
1103 .frequency_stepsize = 250000,
1104 .frequency_tolerance = 0,
1105 .caps = FE_CAN_FEC_1_2 |
1106 FE_CAN_FEC_2_3 |
1107 FE_CAN_FEC_3_4 |
1108 FE_CAN_FEC_5_6 |
1109 FE_CAN_FEC_7_8 |
1110 FE_CAN_FEC_AUTO |
1111 FE_CAN_QPSK |
1112 FE_CAN_QAM_16 |
1113 FE_CAN_QAM_64 |
1114 FE_CAN_QAM_AUTO |
1115 FE_CAN_TRANSMISSION_MODE_AUTO |
1116 FE_CAN_GUARD_INTERVAL_AUTO |
1117 FE_CAN_HIERARCHY_AUTO |
1118 FE_CAN_RECOVER |
1119 FE_CAN_MUTE_TS
1120 },
1121
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001122 .init = af9033_init,
1123 .sleep = af9033_sleep,
1124
1125 .get_tune_settings = af9033_get_tune_settings,
1126 .set_frontend = af9033_set_frontend,
Gianluca Gennari0a4df232012-04-05 12:47:19 -03001127 .get_frontend = af9033_get_frontend,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001128
1129 .read_status = af9033_read_status,
1130 .read_snr = af9033_read_snr,
1131 .read_signal_strength = af9033_read_signal_strength,
1132 .read_ber = af9033_read_ber,
1133 .read_ucblocks = af9033_read_ucblocks,
1134
1135 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1136};
1137
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001138static int af9033_probe(struct i2c_client *client,
1139 const struct i2c_device_id *id)
1140{
1141 struct af9033_config *cfg = client->dev.platform_data;
1142 struct af9033_dev *dev;
1143 int ret;
1144 u8 buf[8];
1145 u32 reg;
1146
1147 /* allocate memory for the internal state */
1148 dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL);
1149 if (dev == NULL) {
1150 ret = -ENOMEM;
1151 dev_err(&client->dev, "Could not allocate memory for state\n");
1152 goto err;
1153 }
1154
1155 /* setup the state */
1156 dev->client = client;
Antti Palosaari83f11612014-09-02 03:55:21 -03001157 INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001158 memcpy(&dev->cfg, cfg, sizeof(struct af9033_config));
1159
1160 if (dev->cfg.clock != 12000000) {
1161 ret = -ENODEV;
1162 dev_err(&dev->client->dev,
Antti Palosaari6a087f12014-08-31 21:08:09 -03001163 "unsupported clock %d Hz, only 12000000 Hz is supported currently\n",
1164 dev->cfg.clock);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001165 goto err_kfree;
1166 }
1167
1168 /* firmware version */
1169 switch (dev->cfg.tuner) {
1170 case AF9033_TUNER_IT9135_38:
1171 case AF9033_TUNER_IT9135_51:
1172 case AF9033_TUNER_IT9135_52:
1173 case AF9033_TUNER_IT9135_60:
1174 case AF9033_TUNER_IT9135_61:
1175 case AF9033_TUNER_IT9135_62:
Antti Palosaari83f11612014-09-02 03:55:21 -03001176 dev->is_it9135 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001177 reg = 0x004bfc;
1178 break;
1179 default:
Antti Palosaari83f11612014-09-02 03:55:21 -03001180 dev->is_af9035 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001181 reg = 0x0083e9;
1182 break;
1183 }
1184
1185 ret = af9033_rd_regs(dev, reg, &buf[0], 4);
1186 if (ret < 0)
1187 goto err_kfree;
1188
1189 ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4);
1190 if (ret < 0)
1191 goto err_kfree;
1192
1193 dev_info(&dev->client->dev,
Antti Palosaari6a087f12014-08-31 21:08:09 -03001194 "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
1195 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
1196 buf[7]);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001197
1198 /* sleep */
1199 switch (dev->cfg.tuner) {
1200 case AF9033_TUNER_IT9135_38:
1201 case AF9033_TUNER_IT9135_51:
1202 case AF9033_TUNER_IT9135_52:
1203 case AF9033_TUNER_IT9135_60:
1204 case AF9033_TUNER_IT9135_61:
1205 case AF9033_TUNER_IT9135_62:
1206 /* IT9135 did not like to sleep at that early */
1207 break;
1208 default:
1209 ret = af9033_wr_reg(dev, 0x80004c, 1);
1210 if (ret < 0)
1211 goto err_kfree;
1212
1213 ret = af9033_wr_reg(dev, 0x800000, 0);
1214 if (ret < 0)
1215 goto err_kfree;
1216 }
1217
1218 /* configure internal TS mode */
1219 switch (dev->cfg.ts_mode) {
1220 case AF9033_TS_MODE_PARALLEL:
1221 dev->ts_mode_parallel = true;
1222 break;
1223 case AF9033_TS_MODE_SERIAL:
1224 dev->ts_mode_serial = true;
1225 break;
1226 case AF9033_TS_MODE_USB:
1227 /* usb mode for AF9035 */
1228 default:
1229 break;
1230 }
1231
1232 /* create dvb_frontend */
1233 memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
1234 dev->fe.demodulator_priv = dev;
1235 *cfg->fe = &dev->fe;
1236 if (cfg->ops) {
1237 cfg->ops->pid_filter = af9033_pid_filter;
1238 cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1239 }
1240 i2c_set_clientdata(client, dev);
1241
1242 dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n");
1243 return 0;
1244err_kfree:
1245 kfree(dev);
1246err:
Antti Palosaari6a087f12014-08-31 21:08:09 -03001247 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001248 return ret;
1249}
1250
1251static int af9033_remove(struct i2c_client *client)
1252{
1253 struct af9033_dev *dev = i2c_get_clientdata(client);
1254
Antti Palosaari6a087f12014-08-31 21:08:09 -03001255 dev_dbg(&dev->client->dev, "\n");
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001256
1257 dev->fe.ops.release = NULL;
1258 dev->fe.demodulator_priv = NULL;
1259 kfree(dev);
1260
1261 return 0;
1262}
1263
1264static const struct i2c_device_id af9033_id_table[] = {
1265 {"af9033", 0},
1266 {}
1267};
1268MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1269
1270static struct i2c_driver af9033_driver = {
1271 .driver = {
1272 .owner = THIS_MODULE,
1273 .name = "af9033",
1274 },
1275 .probe = af9033_probe,
1276 .remove = af9033_remove,
1277 .id_table = af9033_id_table,
1278};
1279
1280module_i2c_driver(af9033_driver);
1281
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001282MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1283MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1284MODULE_LICENSE("GPL");