blob: f5267fdad75e59c22d90953b45689a90921fa493 [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 Palosaarie53c4742014-09-03 23:22:53 -030041 u64 post_bit_error_prev; /* for old read_ber we return (curr - prev) */
Antti Palosaari6bb096c2014-09-02 08:29:46 -030042 u64 post_bit_error;
43 u64 post_bit_count;
Antti Palosaari204f4312014-09-02 08:01:10 -030044 u64 error_block_count;
45 u64 total_block_count;
Antti Palosaari83f11612014-09-02 03:55:21 -030046 struct delayed_work stat_work;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -030047 unsigned long last_stat_check;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030048};
49
50/* write multiple registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -030051static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030052 int len)
53{
54 int ret;
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030055 u8 buf[MAX_XFER_SIZE];
Antti Palosaari4b64bb22012-03-30 08:21:25 -030056 struct i2c_msg msg[1] = {
57 {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030058 .addr = dev->client->addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030059 .flags = 0,
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030060 .len = 3 + len,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030061 .buf = buf,
62 }
63 };
64
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030065 if (3 + len > sizeof(buf)) {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030066 dev_warn(&dev->client->dev,
Antti Palosaari6a087f12014-08-31 21:08:09 -030067 "i2c wr reg=%04x: len=%d is too big!\n",
68 reg, len);
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030069 return -EINVAL;
70 }
71
Antti Palosaari4b64bb22012-03-30 08:21:25 -030072 buf[0] = (reg >> 16) & 0xff;
73 buf[1] = (reg >> 8) & 0xff;
74 buf[2] = (reg >> 0) & 0xff;
75 memcpy(&buf[3], val, len);
76
Antti Palosaarif5b00a72014-08-31 20:57:05 -030077 ret = i2c_transfer(dev->client->adapter, msg, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -030078 if (ret == 1) {
79 ret = 0;
80 } else {
Antti Palosaari6a087f12014-08-31 21:08:09 -030081 dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n",
82 ret, reg, len);
Antti Palosaari4b64bb22012-03-30 08:21:25 -030083 ret = -EREMOTEIO;
84 }
85
86 return ret;
87}
88
89/* read multiple registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -030090static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len)
Antti Palosaari4b64bb22012-03-30 08:21:25 -030091{
92 int ret;
93 u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
94 (reg >> 0) & 0xff };
95 struct i2c_msg msg[2] = {
96 {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030097 .addr = dev->client->addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -030098 .flags = 0,
99 .len = sizeof(buf),
100 .buf = buf
101 }, {
Antti Palosaarif5b00a72014-08-31 20:57:05 -0300102 .addr = dev->client->addr,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300103 .flags = I2C_M_RD,
104 .len = len,
105 .buf = val
106 }
107 };
108
Antti Palosaarif5b00a72014-08-31 20:57:05 -0300109 ret = i2c_transfer(dev->client->adapter, msg, 2);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300110 if (ret == 2) {
111 ret = 0;
112 } else {
Antti Palosaari6a087f12014-08-31 21:08:09 -0300113 dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n",
114 ret, reg, len);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300115 ret = -EREMOTEIO;
116 }
117
118 return ret;
119}
120
121
122/* write single register */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300123static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300124{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300125 return af9033_wr_regs(dev, reg, &val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300126}
127
128/* read single register */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300129static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300130{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300131 return af9033_rd_regs(dev, reg, val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300132}
133
134/* write single register with mask */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300135static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300136 u8 mask)
137{
138 int ret;
139 u8 tmp;
140
141 /* no need for read if whole reg is written */
142 if (mask != 0xff) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300143 ret = af9033_rd_regs(dev, reg, &tmp, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300144 if (ret)
145 return ret;
146
147 val &= mask;
148 tmp &= ~mask;
149 val |= tmp;
150 }
151
Antti Palosaari09611ca2014-08-31 02:18:34 -0300152 return af9033_wr_regs(dev, reg, &val, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300153}
154
155/* read single register with mask */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300156static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300157 u8 mask)
158{
159 int ret, i;
160 u8 tmp;
161
Antti Palosaari09611ca2014-08-31 02:18:34 -0300162 ret = af9033_rd_regs(dev, reg, &tmp, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300163 if (ret)
164 return ret;
165
166 tmp &= mask;
167
168 /* find position of the first bit */
169 for (i = 0; i < 8; i++) {
170 if ((mask >> i) & 0x01)
171 break;
172 }
173 *val = tmp >> i;
174
175 return 0;
176}
177
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300178/* write reg val table using reg addr auto increment */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300179static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300180 const struct reg_val *tab, int tab_len)
181{
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300182#define MAX_TAB_LEN 212
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300183 int ret, i, j;
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300184 u8 buf[1 + MAX_TAB_LEN];
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300185
Antti Palosaari6a087f12014-08-31 21:08:09 -0300186 dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300187
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300188 if (tab_len > sizeof(buf)) {
Antti Palosaari6a087f12014-08-31 21:08:09 -0300189 dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len);
Antti Palosaarid18a88b2013-11-27 17:17:43 -0300190 return -EINVAL;
191 }
192
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300193 for (i = 0, j = 0; i < tab_len; i++) {
194 buf[j] = tab[i].val;
195
196 if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300197 ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300198 if (ret < 0)
199 goto err;
200
201 j = 0;
202 } else {
203 j++;
204 }
205 }
206
207 return 0;
208
209err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300210 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300211
212 return ret;
213}
214
Antti Palosaari09611ca2014-08-31 02:18:34 -0300215static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300216{
217 u32 r = 0, c = 0, i;
218
Antti Palosaari6a087f12014-08-31 21:08:09 -0300219 dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300220
221 if (a > b) {
222 c = a / b;
223 a = a - c * b;
224 }
225
226 for (i = 0; i < x; i++) {
227 if (a >= b) {
228 r += 1;
229 a -= b;
230 }
231 a <<= 1;
232 r <<= 1;
233 }
234 r = (c << (u32)x) + r;
235
Antti Palosaari6a087f12014-08-31 21:08:09 -0300236 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 -0300237
238 return r;
239}
240
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300241static int af9033_init(struct dvb_frontend *fe)
242{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300243 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300244 int ret, i, len;
245 const struct reg_val *init;
246 u8 buf[4];
247 u32 adc_cw, clock_cw;
248 struct reg_val_mask tab[] = {
249 { 0x80fb24, 0x00, 0x08 },
250 { 0x80004c, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300251 { 0x00f641, dev->cfg.tuner, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300252 { 0x80f5ca, 0x01, 0x01 },
253 { 0x80f715, 0x01, 0x01 },
254 { 0x00f41f, 0x04, 0x04 },
255 { 0x00f41a, 0x01, 0x01 },
256 { 0x80f731, 0x00, 0x01 },
257 { 0x00d91e, 0x00, 0x01 },
258 { 0x00d919, 0x00, 0x01 },
259 { 0x80f732, 0x00, 0x01 },
260 { 0x00d91f, 0x00, 0x01 },
261 { 0x00d91a, 0x00, 0x01 },
262 { 0x80f730, 0x00, 0x01 },
263 { 0x80f778, 0x00, 0xff },
264 { 0x80f73c, 0x01, 0x01 },
265 { 0x80f776, 0x00, 0x01 },
266 { 0x00d8fd, 0x01, 0xff },
267 { 0x00d830, 0x01, 0xff },
268 { 0x00d831, 0x00, 0xff },
269 { 0x00d832, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300270 { 0x80f985, dev->ts_mode_serial, 0x01 },
271 { 0x80f986, dev->ts_mode_parallel, 0x01 },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300272 { 0x00d827, 0x00, 0xff },
273 { 0x00d829, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300274 { 0x800045, dev->cfg.adc_multiplier, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300275 };
276
277 /* program clock control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300278 clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300279 buf[0] = (clock_cw >> 0) & 0xff;
280 buf[1] = (clock_cw >> 8) & 0xff;
281 buf[2] = (clock_cw >> 16) & 0xff;
282 buf[3] = (clock_cw >> 24) & 0xff;
283
Antti Palosaari6a087f12014-08-31 21:08:09 -0300284 dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n",
285 dev->cfg.clock, clock_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300286
Antti Palosaari09611ca2014-08-31 02:18:34 -0300287 ret = af9033_wr_regs(dev, 0x800025, buf, 4);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300288 if (ret < 0)
289 goto err;
290
291 /* program ADC control */
292 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300293 if (clock_adc_lut[i].clock == dev->cfg.clock)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300294 break;
295 }
296
Antti Palosaari09611ca2014-08-31 02:18:34 -0300297 adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300298 buf[0] = (adc_cw >> 0) & 0xff;
299 buf[1] = (adc_cw >> 8) & 0xff;
300 buf[2] = (adc_cw >> 16) & 0xff;
301
Antti Palosaari6a087f12014-08-31 21:08:09 -0300302 dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n",
303 clock_adc_lut[i].adc, adc_cw);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300304
Antti Palosaari09611ca2014-08-31 02:18:34 -0300305 ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300306 if (ret < 0)
307 goto err;
308
309 /* program register table */
310 for (i = 0; i < ARRAY_SIZE(tab); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300311 ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300312 tab[i].mask);
313 if (ret < 0)
314 goto err;
315 }
316
Antti Palosaarica681fe2014-08-08 23:56:49 -0300317 /* clock output */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300318 if (dev->cfg.dyn0_clk) {
319 ret = af9033_wr_reg(dev, 0x80fba8, 0x00);
Antti Palosaari9dc0f3f2014-08-09 01:03:14 -0300320 if (ret < 0)
321 goto err;
322 }
323
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300324 /* settings for TS interface */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300325 if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
326 ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300327 if (ret < 0)
328 goto err;
329
Antti Palosaari09611ca2014-08-31 02:18:34 -0300330 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300331 if (ret < 0)
332 goto err;
333 } else {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300334 ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300335 if (ret < 0)
336 goto err;
337
Antti Palosaari09611ca2014-08-31 02:18:34 -0300338 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300339 if (ret < 0)
340 goto err;
341 }
342
343 /* load OFSM settings */
Antti Palosaari6a087f12014-08-31 21:08:09 -0300344 dev_dbg(&dev->client->dev, "load ofsm settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300345 switch (dev->cfg.tuner) {
Antti Palosaarife8eece2013-02-03 13:39:55 -0300346 case AF9033_TUNER_IT9135_38:
347 case AF9033_TUNER_IT9135_51:
348 case AF9033_TUNER_IT9135_52:
Antti Palosaari463c3992013-03-07 00:03:06 -0300349 len = ARRAY_SIZE(ofsm_init_it9135_v1);
350 init = ofsm_init_it9135_v1;
351 break;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300352 case AF9033_TUNER_IT9135_60:
353 case AF9033_TUNER_IT9135_61:
354 case AF9033_TUNER_IT9135_62:
Antti Palosaari463c3992013-03-07 00:03:06 -0300355 len = ARRAY_SIZE(ofsm_init_it9135_v2);
356 init = ofsm_init_it9135_v2;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300357 break;
358 default:
359 len = ARRAY_SIZE(ofsm_init);
360 init = ofsm_init;
361 break;
362 }
363
Antti Palosaari09611ca2014-08-31 02:18:34 -0300364 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300365 if (ret < 0)
366 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300367
368 /* load tuner specific settings */
Antti Palosaari6a087f12014-08-31 21:08:09 -0300369 dev_dbg(&dev->client->dev, "load tuner specific settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300370 switch (dev->cfg.tuner) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300371 case AF9033_TUNER_TUA9001:
372 len = ARRAY_SIZE(tuner_init_tua9001);
373 init = tuner_init_tua9001;
374 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300375 case AF9033_TUNER_FC0011:
376 len = ARRAY_SIZE(tuner_init_fc0011);
377 init = tuner_init_fc0011;
378 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300379 case AF9033_TUNER_MXL5007T:
380 len = ARRAY_SIZE(tuner_init_mxl5007t);
381 init = tuner_init_mxl5007t;
382 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300383 case AF9033_TUNER_TDA18218:
384 len = ARRAY_SIZE(tuner_init_tda18218);
385 init = tuner_init_tda18218;
386 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300387 case AF9033_TUNER_FC2580:
388 len = ARRAY_SIZE(tuner_init_fc2580);
389 init = tuner_init_fc2580;
390 break;
Antti Palosaarie713ad12012-12-02 18:47:00 -0300391 case AF9033_TUNER_FC0012:
392 len = ARRAY_SIZE(tuner_init_fc0012);
393 init = tuner_init_fc0012;
394 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300395 case AF9033_TUNER_IT9135_38:
Antti Palosaaria72cbb72013-03-07 21:17:55 -0300396 len = ARRAY_SIZE(tuner_init_it9135_38);
397 init = tuner_init_it9135_38;
398 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300399 case AF9033_TUNER_IT9135_51:
Antti Palosaaribb2e12a2013-03-07 21:25:16 -0300400 len = ARRAY_SIZE(tuner_init_it9135_51);
401 init = tuner_init_it9135_51;
402 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300403 case AF9033_TUNER_IT9135_52:
Antti Palosaari22d729f2013-03-07 21:29:05 -0300404 len = ARRAY_SIZE(tuner_init_it9135_52);
405 init = tuner_init_it9135_52;
406 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300407 case AF9033_TUNER_IT9135_60:
Antti Palosaaria49f53a2013-03-07 21:38:00 -0300408 len = ARRAY_SIZE(tuner_init_it9135_60);
409 init = tuner_init_it9135_60;
410 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300411 case AF9033_TUNER_IT9135_61:
Antti Palosaari85211322013-03-07 21:44:30 -0300412 len = ARRAY_SIZE(tuner_init_it9135_61);
413 init = tuner_init_it9135_61;
414 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300415 case AF9033_TUNER_IT9135_62:
Antti Palosaaridc4a2c42013-03-07 21:47:20 -0300416 len = ARRAY_SIZE(tuner_init_it9135_62);
417 init = tuner_init_it9135_62;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300418 break;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300419 default:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300420 dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n",
421 dev->cfg.tuner);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300422 ret = -ENODEV;
423 goto err;
424 }
425
Antti Palosaari09611ca2014-08-31 02:18:34 -0300426 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300427 if (ret < 0)
428 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300429
Antti Palosaari09611ca2014-08-31 02:18:34 -0300430 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
431 ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300432 if (ret < 0)
433 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300434
Antti Palosaari09611ca2014-08-31 02:18:34 -0300435 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300436 if (ret < 0)
437 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300438
Antti Palosaari09611ca2014-08-31 02:18:34 -0300439 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300440 if (ret < 0)
441 goto err;
442 }
443
Antti Palosaari09611ca2014-08-31 02:18:34 -0300444 switch (dev->cfg.tuner) {
Antti Palosaari086991d2013-03-07 22:18:56 -0300445 case AF9033_TUNER_IT9135_60:
446 case AF9033_TUNER_IT9135_61:
447 case AF9033_TUNER_IT9135_62:
Antti Palosaari09611ca2014-08-31 02:18:34 -0300448 ret = af9033_wr_reg(dev, 0x800000, 0x01);
Antti Palosaari086991d2013-03-07 22:18:56 -0300449 if (ret < 0)
450 goto err;
451 }
452
Antti Palosaari09611ca2014-08-31 02:18:34 -0300453 dev->bandwidth_hz = 0; /* force to program all parameters */
Antti Palosaari83f11612014-09-02 03:55:21 -0300454 /* start statistics polling */
455 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300456
457 return 0;
458
459err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300460 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300461
462 return ret;
463}
464
465static int af9033_sleep(struct dvb_frontend *fe)
466{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300467 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300468 int ret, i;
469 u8 tmp;
470
Antti Palosaari83f11612014-09-02 03:55:21 -0300471 /* stop statistics polling */
472 cancel_delayed_work_sync(&dev->stat_work);
473
Antti Palosaari09611ca2014-08-31 02:18:34 -0300474 ret = af9033_wr_reg(dev, 0x80004c, 1);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300475 if (ret < 0)
476 goto err;
477
Antti Palosaari09611ca2014-08-31 02:18:34 -0300478 ret = af9033_wr_reg(dev, 0x800000, 0);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300479 if (ret < 0)
480 goto err;
481
482 for (i = 100, tmp = 1; i && tmp; i--) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300483 ret = af9033_rd_reg(dev, 0x80004c, &tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300484 if (ret < 0)
485 goto err;
486
487 usleep_range(200, 10000);
488 }
489
Antti Palosaari6a087f12014-08-31 21:08:09 -0300490 dev_dbg(&dev->client->dev, "loop=%d\n", i);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300491
492 if (i == 0) {
493 ret = -ETIMEDOUT;
494 goto err;
495 }
496
Antti Palosaari09611ca2014-08-31 02:18:34 -0300497 ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300498 if (ret < 0)
499 goto err;
500
501 /* prevent current leak (?) */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300502 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300503 /* enable parallel TS */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300504 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300505 if (ret < 0)
506 goto err;
507
Antti Palosaari09611ca2014-08-31 02:18:34 -0300508 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300509 if (ret < 0)
510 goto err;
511 }
512
513 return 0;
514
515err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300516 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300517
518 return ret;
519}
520
521static int af9033_get_tune_settings(struct dvb_frontend *fe,
522 struct dvb_frontend_tune_settings *fesettings)
523{
Antti Palosaarife8eece2013-02-03 13:39:55 -0300524 /* 800 => 2000 because IT9135 v2 is slow to gain lock */
525 fesettings->min_delay_ms = 2000;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300526 fesettings->step_size = 0;
527 fesettings->max_drift = 0;
528
529 return 0;
530}
531
532static int af9033_set_frontend(struct dvb_frontend *fe)
533{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300534 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300535 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300536 int ret, i, spec_inv, sampling_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300537 u8 tmp, buf[3], bandwidth_reg_val;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300538 u32 if_frequency, freq_cw, adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300539
Antti Palosaari6a087f12014-08-31 21:08:09 -0300540 dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n",
541 c->frequency, c->bandwidth_hz);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300542
543 /* check bandwidth */
544 switch (c->bandwidth_hz) {
545 case 6000000:
546 bandwidth_reg_val = 0x00;
547 break;
548 case 7000000:
549 bandwidth_reg_val = 0x01;
550 break;
551 case 8000000:
552 bandwidth_reg_val = 0x02;
553 break;
554 default:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300555 dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n");
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300556 ret = -EINVAL;
557 goto err;
558 }
559
560 /* program tuner */
561 if (fe->ops.tuner_ops.set_params)
562 fe->ops.tuner_ops.set_params(fe);
563
564 /* program CFOE coefficients */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300565 if (c->bandwidth_hz != dev->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300566 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300567 if (coeff_lut[i].clock == dev->cfg.clock &&
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300568 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
569 break;
570 }
571 }
Antti Palosaari09611ca2014-08-31 02:18:34 -0300572 ret = af9033_wr_regs(dev, 0x800001,
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300573 coeff_lut[i].val, sizeof(coeff_lut[i].val));
574 }
575
576 /* program frequency control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300577 if (c->bandwidth_hz != dev->bandwidth_hz) {
578 spec_inv = dev->cfg.spec_inv ? -1 : 1;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300579
580 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300581 if (clock_adc_lut[i].clock == dev->cfg.clock)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300582 break;
583 }
584 adc_freq = clock_adc_lut[i].adc;
585
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300586 /* get used IF frequency */
587 if (fe->ops.tuner_ops.get_if_frequency)
588 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
589 else
590 if_frequency = 0;
591
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300592 sampling_freq = if_frequency;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300593
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300594 while (sampling_freq > (adc_freq / 2))
595 sampling_freq -= adc_freq;
596
597 if (sampling_freq >= 0)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300598 spec_inv *= -1;
599 else
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300600 sampling_freq *= -1;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300601
Antti Palosaari09611ca2014-08-31 02:18:34 -0300602 freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300603
604 if (spec_inv == -1)
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300605 freq_cw = 0x800000 - freq_cw;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300606
Antti Palosaari09611ca2014-08-31 02:18:34 -0300607 if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300608 freq_cw /= 2;
609
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300610 buf[0] = (freq_cw >> 0) & 0xff;
611 buf[1] = (freq_cw >> 8) & 0xff;
612 buf[2] = (freq_cw >> 16) & 0x7f;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300613
614 /* FIXME: there seems to be calculation error here... */
615 if (if_frequency == 0)
616 buf[2] = 0;
617
Antti Palosaari09611ca2014-08-31 02:18:34 -0300618 ret = af9033_wr_regs(dev, 0x800029, buf, 3);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300619 if (ret < 0)
620 goto err;
621
Antti Palosaari09611ca2014-08-31 02:18:34 -0300622 dev->bandwidth_hz = c->bandwidth_hz;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300623 }
624
Antti Palosaari09611ca2014-08-31 02:18:34 -0300625 ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300626 if (ret < 0)
627 goto err;
628
Antti Palosaari09611ca2014-08-31 02:18:34 -0300629 ret = af9033_wr_reg(dev, 0x800040, 0x00);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300630 if (ret < 0)
631 goto err;
632
Antti Palosaari09611ca2014-08-31 02:18:34 -0300633 ret = af9033_wr_reg(dev, 0x800047, 0x00);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300634 if (ret < 0)
635 goto err;
636
Antti Palosaari09611ca2014-08-31 02:18:34 -0300637 ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300638 if (ret < 0)
639 goto err;
640
641 if (c->frequency <= 230000000)
642 tmp = 0x00; /* VHF */
643 else
644 tmp = 0x01; /* UHF */
645
Antti Palosaari09611ca2014-08-31 02:18:34 -0300646 ret = af9033_wr_reg(dev, 0x80004b, tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300647 if (ret < 0)
648 goto err;
649
Antti Palosaari09611ca2014-08-31 02:18:34 -0300650 ret = af9033_wr_reg(dev, 0x800000, 0x00);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300651 if (ret < 0)
652 goto err;
653
654 return 0;
655
656err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300657 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300658
659 return ret;
660}
661
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300662static int af9033_get_frontend(struct dvb_frontend *fe)
663{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300664 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300665 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300666 int ret;
667 u8 buf[8];
668
Antti Palosaari6a087f12014-08-31 21:08:09 -0300669 dev_dbg(&dev->client->dev, "\n");
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300670
671 /* read all needed registers */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300672 ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf));
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300673 if (ret < 0)
674 goto err;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300675
676 switch ((buf[0] >> 0) & 3) {
677 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300678 c->transmission_mode = TRANSMISSION_MODE_2K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300679 break;
680 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300681 c->transmission_mode = TRANSMISSION_MODE_8K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300682 break;
683 }
684
685 switch ((buf[1] >> 0) & 3) {
686 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300687 c->guard_interval = GUARD_INTERVAL_1_32;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300688 break;
689 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300690 c->guard_interval = GUARD_INTERVAL_1_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300691 break;
692 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300693 c->guard_interval = GUARD_INTERVAL_1_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300694 break;
695 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300696 c->guard_interval = GUARD_INTERVAL_1_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300697 break;
698 }
699
700 switch ((buf[2] >> 0) & 7) {
701 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300702 c->hierarchy = HIERARCHY_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300703 break;
704 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300705 c->hierarchy = HIERARCHY_1;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300706 break;
707 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300708 c->hierarchy = HIERARCHY_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300709 break;
710 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300711 c->hierarchy = HIERARCHY_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300712 break;
713 }
714
715 switch ((buf[3] >> 0) & 3) {
716 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300717 c->modulation = QPSK;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300718 break;
719 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300720 c->modulation = QAM_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300721 break;
722 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300723 c->modulation = QAM_64;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300724 break;
725 }
726
727 switch ((buf[4] >> 0) & 3) {
728 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300729 c->bandwidth_hz = 6000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300730 break;
731 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300732 c->bandwidth_hz = 7000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300733 break;
734 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300735 c->bandwidth_hz = 8000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300736 break;
737 }
738
739 switch ((buf[6] >> 0) & 7) {
740 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300741 c->code_rate_HP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300742 break;
743 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300744 c->code_rate_HP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300745 break;
746 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300747 c->code_rate_HP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300748 break;
749 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300750 c->code_rate_HP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300751 break;
752 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300753 c->code_rate_HP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300754 break;
755 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300756 c->code_rate_HP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300757 break;
758 }
759
760 switch ((buf[7] >> 0) & 7) {
761 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300762 c->code_rate_LP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300763 break;
764 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300765 c->code_rate_LP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300766 break;
767 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300768 c->code_rate_LP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300769 break;
770 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300771 c->code_rate_LP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300772 break;
773 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300774 c->code_rate_LP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300775 break;
776 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300777 c->code_rate_LP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300778 break;
779 }
780
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300781 return 0;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300782
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300783err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300784 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300785
786 return ret;
787}
788
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300789static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
790{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300791 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300792 int ret;
793 u8 tmp;
794
795 *status = 0;
796
797 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300798 ret = af9033_rd_reg(dev, 0x800047, &tmp);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300799 if (ret < 0)
800 goto err;
801
802 /* has signal */
803 if (tmp == 0x01)
804 *status |= FE_HAS_SIGNAL;
805
806 if (tmp != 0x02) {
807 /* TPS lock */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300808 ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300809 if (ret < 0)
810 goto err;
811
812 if (tmp)
813 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
814 FE_HAS_VITERBI;
815
816 /* full lock */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300817 ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300818 if (ret < 0)
819 goto err;
820
821 if (tmp)
822 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
823 FE_HAS_VITERBI | FE_HAS_SYNC |
824 FE_HAS_LOCK;
825 }
826
Antti Palosaari83f11612014-09-02 03:55:21 -0300827 dev->fe_status = *status;
828
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300829 return 0;
830
831err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300832 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300833
834 return ret;
835}
836
837static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
838{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300839 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari6b457782014-09-02 05:03:21 -0300840 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300841
Antti Palosaari6b457782014-09-02 05:03:21 -0300842 /* use DVBv5 CNR */
843 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
844 *snr = div_s64(c->cnr.stat[0].svalue, 100); /* 1000x => 10x */
845 else
846 *snr = 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300847
848 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300849}
850
851static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
852{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300853 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300854 int ret;
855 u8 strength2;
856
857 /* read signal strength of 0-100 scale */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300858 ret = af9033_rd_reg(dev, 0x800048, &strength2);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300859 if (ret < 0)
860 goto err;
861
862 /* scale value to 0x0000-0xffff */
863 *strength = strength2 * 0xffff / 100;
864
865 return 0;
866
867err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300868 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300869
870 return ret;
871}
872
Antti Palosaari09611ca2014-08-31 02:18:34 -0300873static int af9033_update_ch_stat(struct af9033_dev *dev)
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300874{
875 int ret = 0;
876 u32 err_cnt, bit_cnt;
877 u16 abort_cnt;
878 u8 buf[7];
879
880 /* only update data every half second */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300881 if (time_after(jiffies, dev->last_stat_check + msecs_to_jiffies(500))) {
882 ret = af9033_rd_regs(dev, 0x800032, buf, sizeof(buf));
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300883 if (ret < 0)
884 goto err;
885 /* in 8 byte packets? */
886 abort_cnt = (buf[1] << 8) + buf[0];
887 /* in bits */
888 err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
889 /* in 8 byte packets? always(?) 0x2710 = 10000 */
890 bit_cnt = (buf[6] << 8) + buf[5];
891
892 if (bit_cnt < abort_cnt) {
893 abort_cnt = 1000;
Antti Palosaari09611ca2014-08-31 02:18:34 -0300894 dev->ber = 0xffffffff;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300895 } else {
Antti Palosaari24e419a2014-08-09 14:59:53 -0300896 /*
897 * 8 byte packets, that have not been rejected already
898 */
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300899 bit_cnt -= (u32)abort_cnt;
900 if (bit_cnt == 0) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300901 dev->ber = 0xffffffff;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300902 } else {
903 err_cnt -= (u32)abort_cnt * 8 * 8;
904 bit_cnt *= 8 * 8;
Antti Palosaari09611ca2014-08-31 02:18:34 -0300905 dev->ber = err_cnt * (0xffffffff / bit_cnt);
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300906 }
907 }
Antti Palosaari09611ca2014-08-31 02:18:34 -0300908 dev->ucb += abort_cnt;
909 dev->last_stat_check = jiffies;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300910 }
911
912 return 0;
913err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300914 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari0a73f2d2012-09-12 20:23:42 -0300915
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300916 return ret;
917}
918
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300919static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
920{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300921 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300922
Antti Palosaarie53c4742014-09-03 23:22:53 -0300923 *ber = (dev->post_bit_error - dev->post_bit_error_prev);
924 dev->post_bit_error_prev = dev->post_bit_error;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300925
926 return 0;
927}
928
929static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
930{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300931 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300932
Antti Palosaari1d0ceae2014-09-03 23:07:39 -0300933 *ucblocks = dev->error_block_count;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300934 return 0;
935}
936
937static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
938{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300939 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300940 int ret;
941
Antti Palosaari6a087f12014-08-31 21:08:09 -0300942 dev_dbg(&dev->client->dev, "enable=%d\n", enable);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300943
Antti Palosaari09611ca2014-08-31 02:18:34 -0300944 ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300945 if (ret < 0)
946 goto err;
947
948 return 0;
949
950err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300951 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300952
953 return ret;
954}
955
Mauro Carvalho Chehabed97a6f2014-03-14 14:29:06 -0300956static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300957{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300958 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari040cf862014-02-13 15:40:59 -0300959 int ret;
960
Antti Palosaari6a087f12014-08-31 21:08:09 -0300961 dev_dbg(&dev->client->dev, "onoff=%d\n", onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300962
Antti Palosaari09611ca2014-08-31 02:18:34 -0300963 ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01);
Antti Palosaari040cf862014-02-13 15:40:59 -0300964 if (ret < 0)
965 goto err;
966
967 return 0;
968
969err:
Antti Palosaari6a087f12014-08-31 21:08:09 -0300970 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -0300971
972 return ret;
973}
Antti Palosaari040cf862014-02-13 15:40:59 -0300974
Antti Palosaari24e419a2014-08-09 14:59:53 -0300975static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
976 int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300977{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300978 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari040cf862014-02-13 15:40:59 -0300979 int ret;
980 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
981
Antti Palosaari6a087f12014-08-31 21:08:09 -0300982 dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n",
983 index, pid, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300984
985 if (pid > 0x1fff)
986 return 0;
987
Antti Palosaari09611ca2014-08-31 02:18:34 -0300988 ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2);
Antti Palosaari040cf862014-02-13 15:40:59 -0300989 if (ret < 0)
990 goto err;
991
Antti Palosaari09611ca2014-08-31 02:18:34 -0300992 ret = af9033_wr_reg(dev, 0x80f994, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300993 if (ret < 0)
994 goto err;
995
Antti Palosaari09611ca2014-08-31 02:18:34 -0300996 ret = af9033_wr_reg(dev, 0x80f995, index);
Antti Palosaari040cf862014-02-13 15:40:59 -0300997 if (ret < 0)
998 goto err;
999
1000 return 0;
1001
1002err:
Antti Palosaari6a087f12014-08-31 21:08:09 -03001003 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -03001004
1005 return ret;
1006}
Antti Palosaari040cf862014-02-13 15:40:59 -03001007
Antti Palosaari83f11612014-09-02 03:55:21 -03001008static void af9033_stat_work(struct work_struct *work)
1009{
1010 struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work);
1011 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
Antti Palosaari3e413132014-09-02 04:24:41 -03001012 int ret, tmp, i, len;
Antti Palosaari204f4312014-09-02 08:01:10 -03001013 u8 u8tmp, buf[7];
Antti Palosaari83f11612014-09-02 03:55:21 -03001014
1015 dev_dbg(&dev->client->dev, "\n");
1016
Antti Palosaari3e413132014-09-02 04:24:41 -03001017 /* signal strength */
Antti Palosaari83f11612014-09-02 03:55:21 -03001018 if (dev->fe_status & FE_HAS_SIGNAL) {
1019 if (dev->is_af9035) {
1020 ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
1021 tmp = -u8tmp * 1000;
1022 } else {
1023 ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
1024 tmp = (u8tmp - 100) * 1000;
1025 }
1026 if (ret)
1027 goto err;
1028
1029 c->strength.len = 1;
1030 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
1031 c->strength.stat[0].svalue = tmp;
1032 } else {
1033 c->strength.len = 1;
1034 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1035 }
1036
Antti Palosaari3e413132014-09-02 04:24:41 -03001037 /* CNR */
1038 if (dev->fe_status & FE_HAS_VITERBI) {
1039 u32 snr_val;
1040 const struct val_snr *snr_lut;
1041
1042 /* read value */
1043 ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
1044 if (ret)
1045 goto err;
1046
1047 snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
1048
1049 /* read current modulation */
1050 ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
1051 if (ret)
1052 goto err;
1053
1054 switch ((u8tmp >> 0) & 3) {
1055 case 0:
1056 len = ARRAY_SIZE(qpsk_snr_lut);
1057 snr_lut = qpsk_snr_lut;
1058 break;
1059 case 1:
1060 len = ARRAY_SIZE(qam16_snr_lut);
1061 snr_lut = qam16_snr_lut;
1062 break;
1063 case 2:
1064 len = ARRAY_SIZE(qam64_snr_lut);
1065 snr_lut = qam64_snr_lut;
1066 break;
1067 default:
1068 goto err_schedule_delayed_work;
1069 }
1070
1071 for (i = 0; i < len; i++) {
1072 tmp = snr_lut[i].snr * 1000;
1073 if (snr_val < snr_lut[i].val)
1074 break;
1075 }
1076
1077 c->cnr.len = 1;
1078 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1079 c->cnr.stat[0].svalue = tmp;
1080 } else {
1081 c->cnr.len = 1;
1082 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1083 }
1084
Antti Palosaari204f4312014-09-02 08:01:10 -03001085 /* UCB/PER/BER */
1086 if (dev->fe_status & FE_HAS_LOCK) {
1087 /* outer FEC, 204 byte packets */
1088 u16 abort_packet_count, rsd_packet_count;
Antti Palosaari6bb096c2014-09-02 08:29:46 -03001089 /* inner FEC, bits */
1090 u32 rsd_bit_err_count;
Antti Palosaari204f4312014-09-02 08:01:10 -03001091
1092 /*
1093 * Packet count used for measurement is 10000
1094 * (rsd_packet_count). Maybe it should be increased?
1095 */
1096
1097 ret = af9033_rd_regs(dev, 0x800032, buf, 7);
1098 if (ret)
1099 goto err;
1100
1101 abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
Antti Palosaari6bb096c2014-09-02 08:29:46 -03001102 rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
Antti Palosaari204f4312014-09-02 08:01:10 -03001103 rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
1104
1105 dev->error_block_count += abort_packet_count;
1106 dev->total_block_count += rsd_packet_count;
Antti Palosaari6bb096c2014-09-02 08:29:46 -03001107 dev->post_bit_error += rsd_bit_err_count;
1108 dev->post_bit_count += rsd_packet_count * 204 * 8;
Antti Palosaari204f4312014-09-02 08:01:10 -03001109
1110 c->block_count.len = 1;
1111 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1112 c->block_count.stat[0].uvalue = dev->total_block_count;
1113
1114 c->block_error.len = 1;
1115 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1116 c->block_error.stat[0].uvalue = dev->error_block_count;
Antti Palosaari6bb096c2014-09-02 08:29:46 -03001117
1118 c->post_bit_count.len = 1;
1119 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1120 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
1121
1122 c->post_bit_error.len = 1;
1123 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1124 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
Antti Palosaari204f4312014-09-02 08:01:10 -03001125 }
1126
Antti Palosaari3e413132014-09-02 04:24:41 -03001127err_schedule_delayed_work:
Antti Palosaari83f11612014-09-02 03:55:21 -03001128 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
1129 return;
1130err:
1131 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1132}
1133
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001134static struct dvb_frontend_ops af9033_ops = {
1135 .delsys = { SYS_DVBT },
1136 .info = {
1137 .name = "Afatech AF9033 (DVB-T)",
1138 .frequency_min = 174000000,
1139 .frequency_max = 862000000,
1140 .frequency_stepsize = 250000,
1141 .frequency_tolerance = 0,
1142 .caps = FE_CAN_FEC_1_2 |
1143 FE_CAN_FEC_2_3 |
1144 FE_CAN_FEC_3_4 |
1145 FE_CAN_FEC_5_6 |
1146 FE_CAN_FEC_7_8 |
1147 FE_CAN_FEC_AUTO |
1148 FE_CAN_QPSK |
1149 FE_CAN_QAM_16 |
1150 FE_CAN_QAM_64 |
1151 FE_CAN_QAM_AUTO |
1152 FE_CAN_TRANSMISSION_MODE_AUTO |
1153 FE_CAN_GUARD_INTERVAL_AUTO |
1154 FE_CAN_HIERARCHY_AUTO |
1155 FE_CAN_RECOVER |
1156 FE_CAN_MUTE_TS
1157 },
1158
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001159 .init = af9033_init,
1160 .sleep = af9033_sleep,
1161
1162 .get_tune_settings = af9033_get_tune_settings,
1163 .set_frontend = af9033_set_frontend,
Gianluca Gennari0a4df232012-04-05 12:47:19 -03001164 .get_frontend = af9033_get_frontend,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001165
1166 .read_status = af9033_read_status,
1167 .read_snr = af9033_read_snr,
1168 .read_signal_strength = af9033_read_signal_strength,
1169 .read_ber = af9033_read_ber,
1170 .read_ucblocks = af9033_read_ucblocks,
1171
1172 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1173};
1174
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001175static int af9033_probe(struct i2c_client *client,
1176 const struct i2c_device_id *id)
1177{
1178 struct af9033_config *cfg = client->dev.platform_data;
1179 struct af9033_dev *dev;
1180 int ret;
1181 u8 buf[8];
1182 u32 reg;
1183
1184 /* allocate memory for the internal state */
1185 dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL);
1186 if (dev == NULL) {
1187 ret = -ENOMEM;
1188 dev_err(&client->dev, "Could not allocate memory for state\n");
1189 goto err;
1190 }
1191
1192 /* setup the state */
1193 dev->client = client;
Antti Palosaari83f11612014-09-02 03:55:21 -03001194 INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001195 memcpy(&dev->cfg, cfg, sizeof(struct af9033_config));
1196
1197 if (dev->cfg.clock != 12000000) {
1198 ret = -ENODEV;
1199 dev_err(&dev->client->dev,
Antti Palosaari6a087f12014-08-31 21:08:09 -03001200 "unsupported clock %d Hz, only 12000000 Hz is supported currently\n",
1201 dev->cfg.clock);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001202 goto err_kfree;
1203 }
1204
1205 /* firmware version */
1206 switch (dev->cfg.tuner) {
1207 case AF9033_TUNER_IT9135_38:
1208 case AF9033_TUNER_IT9135_51:
1209 case AF9033_TUNER_IT9135_52:
1210 case AF9033_TUNER_IT9135_60:
1211 case AF9033_TUNER_IT9135_61:
1212 case AF9033_TUNER_IT9135_62:
Antti Palosaari83f11612014-09-02 03:55:21 -03001213 dev->is_it9135 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001214 reg = 0x004bfc;
1215 break;
1216 default:
Antti Palosaari83f11612014-09-02 03:55:21 -03001217 dev->is_af9035 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001218 reg = 0x0083e9;
1219 break;
1220 }
1221
1222 ret = af9033_rd_regs(dev, reg, &buf[0], 4);
1223 if (ret < 0)
1224 goto err_kfree;
1225
1226 ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4);
1227 if (ret < 0)
1228 goto err_kfree;
1229
1230 dev_info(&dev->client->dev,
Antti Palosaari6a087f12014-08-31 21:08:09 -03001231 "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
1232 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
1233 buf[7]);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001234
1235 /* sleep */
1236 switch (dev->cfg.tuner) {
1237 case AF9033_TUNER_IT9135_38:
1238 case AF9033_TUNER_IT9135_51:
1239 case AF9033_TUNER_IT9135_52:
1240 case AF9033_TUNER_IT9135_60:
1241 case AF9033_TUNER_IT9135_61:
1242 case AF9033_TUNER_IT9135_62:
1243 /* IT9135 did not like to sleep at that early */
1244 break;
1245 default:
1246 ret = af9033_wr_reg(dev, 0x80004c, 1);
1247 if (ret < 0)
1248 goto err_kfree;
1249
1250 ret = af9033_wr_reg(dev, 0x800000, 0);
1251 if (ret < 0)
1252 goto err_kfree;
1253 }
1254
1255 /* configure internal TS mode */
1256 switch (dev->cfg.ts_mode) {
1257 case AF9033_TS_MODE_PARALLEL:
1258 dev->ts_mode_parallel = true;
1259 break;
1260 case AF9033_TS_MODE_SERIAL:
1261 dev->ts_mode_serial = true;
1262 break;
1263 case AF9033_TS_MODE_USB:
1264 /* usb mode for AF9035 */
1265 default:
1266 break;
1267 }
1268
1269 /* create dvb_frontend */
1270 memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
1271 dev->fe.demodulator_priv = dev;
1272 *cfg->fe = &dev->fe;
1273 if (cfg->ops) {
1274 cfg->ops->pid_filter = af9033_pid_filter;
1275 cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1276 }
1277 i2c_set_clientdata(client, dev);
1278
1279 dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n");
1280 return 0;
1281err_kfree:
1282 kfree(dev);
1283err:
Antti Palosaari6a087f12014-08-31 21:08:09 -03001284 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001285 return ret;
1286}
1287
1288static int af9033_remove(struct i2c_client *client)
1289{
1290 struct af9033_dev *dev = i2c_get_clientdata(client);
1291
Antti Palosaari6a087f12014-08-31 21:08:09 -03001292 dev_dbg(&dev->client->dev, "\n");
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001293
1294 dev->fe.ops.release = NULL;
1295 dev->fe.demodulator_priv = NULL;
1296 kfree(dev);
1297
1298 return 0;
1299}
1300
1301static const struct i2c_device_id af9033_id_table[] = {
1302 {"af9033", 0},
1303 {}
1304};
1305MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1306
1307static struct i2c_driver af9033_driver = {
1308 .driver = {
1309 .owner = THIS_MODULE,
1310 .name = "af9033",
1311 },
1312 .probe = af9033_probe,
1313 .remove = af9033_remove,
1314 .id_table = af9033_id_table,
1315};
1316
1317module_i2c_driver(af9033_driver);
1318
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001319MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1320MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1321MODULE_LICENSE("GPL");