blob: 970bdb6bb60a55cf1f82cfb01e7e9c70b19796d9 [file] [log] [blame]
Antti Palosaari7f882c22012-03-30 09:10:08 -03001/*
2 * Afatech AF9035 DVB USB 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 "af9035.h"
23#include "af9033.h"
24#include "tua9001.h"
Michael Büschffc501f2012-04-02 12:18:36 -030025#include "fc0011.h"
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -030026#include "mxl5007t.h"
Gianluca Gennarice1fe372012-04-02 17:25:14 -030027#include "tda18218.h"
Antti Palosaari7f882c22012-03-30 09:10:08 -030028
29DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
30static DEFINE_MUTEX(af9035_usb_mutex);
31static struct config af9035_config;
32static struct dvb_usb_device_properties af9035_properties[1];
33static int af9035_properties_count = ARRAY_SIZE(af9035_properties);
34static struct af9033_config af9035_af9033_config[] = {
35 {
36 .ts_mode = AF9033_TS_MODE_USB,
37 }, {
38 .ts_mode = AF9033_TS_MODE_SERIAL,
39 }
40};
41
Michael Büschb1a95992012-04-01 16:33:48 -030042static u16 af9035_checksum(const u8 *buf, size_t len)
43{
44 size_t i;
45 u16 checksum = 0;
46
47 for (i = 1; i < len; i++) {
48 if (i % 2)
49 checksum += buf[i] << 8;
50 else
51 checksum += buf[i];
52 }
53 checksum = ~checksum;
54
55 return checksum;
56}
57
Antti Palosaari7f882c22012-03-30 09:10:08 -030058static int af9035_ctrl_msg(struct usb_device *udev, struct usb_req *req)
59{
60#define BUF_LEN 63
61#define REQ_HDR_LEN 4 /* send header size */
62#define ACK_HDR_LEN 3 /* rece header size */
63#define CHECKSUM_LEN 2
64#define USB_TIMEOUT 2000
65
Michael Büschb1a95992012-04-01 16:33:48 -030066 int ret, act_len;
Antti Palosaari7f882c22012-03-30 09:10:08 -030067 u8 buf[BUF_LEN];
68 u32 msg_len;
69 static u8 seq; /* packet sequence number */
Michael Büschb1a95992012-04-01 16:33:48 -030070 u16 checksum, tmpsum;
Antti Palosaari7f882c22012-03-30 09:10:08 -030071
72 /* buffer overflow check */
73 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
74 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
75 pr_debug("%s: too much data wlen=%d rlen=%d\n", __func__,
76 req->wlen, req->rlen);
77 return -EINVAL;
78 }
79
80 if (mutex_lock_interruptible(&af9035_usb_mutex) < 0)
81 return -EAGAIN;
82
83 buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
84 buf[1] = req->mbox;
85 buf[2] = req->cmd;
86 buf[3] = seq++;
87 if (req->wlen)
88 memcpy(&buf[4], req->wbuf, req->wlen);
89
90 /* calc and add checksum */
Michael Büschb1a95992012-04-01 16:33:48 -030091 checksum = af9035_checksum(buf, buf[0] - 1);
Antti Palosaari7f882c22012-03-30 09:10:08 -030092 buf[buf[0]-1] = (checksum >> 8);
93 buf[buf[0]-0] = (checksum & 0xff);
94
95 msg_len = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN ;
96
97 /* send req */
98 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
99 &act_len, USB_TIMEOUT);
100 if (ret < 0)
101 err("bulk message failed=%d (%d/%d)", ret, msg_len, act_len);
102 else
103 if (act_len != msg_len)
104 ret = -EIO; /* all data is not send */
105 if (ret < 0)
106 goto err_mutex_unlock;
107
108 /* no ack for those packets */
109 if (req->cmd == CMD_FW_DL)
110 goto exit_mutex_unlock;
111
112 /* receive ack and data if read req */
113 msg_len = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
114 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
115 &act_len, USB_TIMEOUT);
116 if (ret < 0) {
117 err("recv bulk message failed=%d", ret);
118 ret = -EIO;
119 goto err_mutex_unlock;
120 }
Michael Büschb1a95992012-04-01 16:33:48 -0300121 if (act_len != msg_len) {
122 err("recv bulk message truncated (%d != %u)\n",
123 act_len, (unsigned int)msg_len);
124 ret = -EIO;
125 goto err_mutex_unlock;
126 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300127
Michael Büschb1a95992012-04-01 16:33:48 -0300128 /* verify checksum */
129 checksum = af9035_checksum(buf, act_len - 2);
130 tmpsum = (buf[act_len - 2] << 8) | buf[act_len - 1];
131 if (tmpsum != checksum) {
132 err("%s: command=%02X checksum mismatch (%04X != %04X)\n",
133 __func__, req->cmd,
134 (unsigned int)tmpsum, (unsigned int)checksum);
135 ret = -EIO;
136 goto err_mutex_unlock;
137 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300138 /* check status */
139 if (buf[2]) {
140 pr_debug("%s: command=%02x failed fw error=%d\n", __func__,
141 req->cmd, buf[2]);
142 ret = -EIO;
143 goto err_mutex_unlock;
144 }
145
146 /* read request, copy returned data to return buf */
147 if (req->rlen)
148 memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen);
149
150err_mutex_unlock:
151exit_mutex_unlock:
152 mutex_unlock(&af9035_usb_mutex);
153
154 return ret;
155}
156
157/* write multiple registers */
158static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
159{
160 u8 wbuf[6 + len];
161 u8 mbox = (reg >> 16) & 0xff;
162 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
163
164 wbuf[0] = len;
165 wbuf[1] = 2;
166 wbuf[2] = 0;
167 wbuf[3] = 0;
168 wbuf[4] = (reg >> 8) & 0xff;
169 wbuf[5] = (reg >> 0) & 0xff;
170 memcpy(&wbuf[6], val, len);
171
172 return af9035_ctrl_msg(d->udev, &req);
173}
174
175/* read multiple registers */
176static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
177{
178 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
179 u8 mbox = (reg >> 16) & 0xff;
180 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
181
182 return af9035_ctrl_msg(d->udev, &req);
183}
184
185/* write single register */
186static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
187{
188 return af9035_wr_regs(d, reg, &val, 1);
189}
190
191/* read single register */
192static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
193{
194 return af9035_rd_regs(d, reg, val, 1);
195}
196
197/* write single register with mask */
198static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
199 u8 mask)
200{
201 int ret;
202 u8 tmp;
203
204 /* no need for read if whole reg is written */
205 if (mask != 0xff) {
206 ret = af9035_rd_regs(d, reg, &tmp, 1);
207 if (ret)
208 return ret;
209
210 val &= mask;
211 tmp &= ~mask;
212 val |= tmp;
213 }
214
215 return af9035_wr_regs(d, reg, &val, 1);
216}
217
218static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
219 struct i2c_msg msg[], int num)
220{
221 struct dvb_usb_device *d = i2c_get_adapdata(adap);
222 int ret;
223
224 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
225 return -EAGAIN;
226
227 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
228 (msg[1].flags & I2C_M_RD)) {
229 if (msg[0].len > 40 || msg[1].len > 40) {
230 /* TODO: correct limits > 40 */
231 ret = -EOPNOTSUPP;
232 } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) {
233 /* integrated demod */
234 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
235 msg[0].buf[2];
236 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
237 msg[1].len);
238 } else {
239 /* I2C */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300240 u8 buf[4 + msg[0].len];
241 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
242 buf, msg[1].len, msg[1].buf };
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300243 buf[0] = msg[1].len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300244 buf[1] = msg[0].addr << 1;
245 buf[2] = 0x01;
246 buf[3] = 0x00;
247 memcpy(&buf[4], msg[0].buf, msg[0].len);
248 ret = af9035_ctrl_msg(d->udev, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300249 }
250 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
251 if (msg[0].len > 40) {
252 /* TODO: correct limits > 40 */
253 ret = -EOPNOTSUPP;
254 } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) {
255 /* integrated demod */
256 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
257 msg[0].buf[2];
258 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
259 msg[0].len - 3);
260 } else {
261 /* I2C */
262 u8 buf[4 + msg[0].len];
263 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
264 0, NULL };
265 buf[0] = msg[0].len;
266 buf[1] = msg[0].addr << 1;
267 buf[2] = 0x01;
268 buf[3] = 0x00;
269 memcpy(&buf[4], msg[0].buf, msg[0].len);
270 ret = af9035_ctrl_msg(d->udev, &req);
271 }
272 } else {
273 /*
274 * We support only two kind of I2C transactions:
275 * 1) 1 x read + 1 x write
276 * 2) 1 x write
277 */
278 ret = -EOPNOTSUPP;
279 }
280
281 mutex_unlock(&d->i2c_mutex);
282
283 if (ret < 0)
284 return ret;
285 else
286 return num;
287}
288
289static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
290{
291 return I2C_FUNC_I2C;
292}
293
294static struct i2c_algorithm af9035_i2c_algo = {
295 .master_xfer = af9035_i2c_master_xfer,
296 .functionality = af9035_i2c_functionality,
297};
298
299static int af9035_init(struct dvb_usb_device *d)
300{
301 int ret, i;
302 u16 frame_size = 87 * 188 / 4;
303 u8 packet_size = 512 / 4;
304 struct reg_val_mask tab[] = {
305 { 0x80f99d, 0x01, 0x01 },
306 { 0x80f9a4, 0x01, 0x01 },
307 { 0x00dd11, 0x00, 0x20 },
308 { 0x00dd11, 0x00, 0x40 },
309 { 0x00dd13, 0x00, 0x20 },
310 { 0x00dd13, 0x00, 0x40 },
311 { 0x00dd11, 0x20, 0x20 },
312 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
313 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
314 { 0x00dd0c, packet_size, 0xff},
315 { 0x00dd11, af9035_config.dual_mode << 6, 0x40 },
316 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
317 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
318 { 0x00dd0d, packet_size, 0xff },
319 { 0x80f9a3, 0x00, 0x01 },
320 { 0x80f9cd, 0x00, 0x01 },
321 { 0x80f99d, 0x00, 0x01 },
322 { 0x80f9a4, 0x00, 0x01 },
323 };
324
325 pr_debug("%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
326 __func__, d->udev->speed, frame_size, packet_size);
327
328 /* init endpoints */
329 for (i = 0; i < ARRAY_SIZE(tab); i++) {
330 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
331 tab[i].mask);
332 if (ret < 0)
333 goto err;
334 }
335
336 return 0;
337
338err:
339 pr_debug("%s: failed=%d\n", __func__, ret);
340
341 return ret;
342}
343
344static int af9035_identify_state(struct usb_device *udev,
345 struct dvb_usb_device_properties *props,
346 struct dvb_usb_device_description **desc,
347 int *cold)
348{
349 int ret;
350 u8 wbuf[1] = { 1 };
351 u8 rbuf[4];
352 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
353 sizeof(rbuf), rbuf };
354
355 ret = af9035_ctrl_msg(udev, &req);
356 if (ret < 0)
357 goto err;
358
359 pr_debug("%s: reply=%02x %02x %02x %02x\n", __func__,
360 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
361 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
362 *cold = 0;
363 else
364 *cold = 1;
365
366 return 0;
367
368err:
369 pr_debug("%s: failed=%d\n", __func__, ret);
370
371 return ret;
372}
373
374static int af9035_download_firmware(struct usb_device *udev,
375 const struct firmware *fw)
376{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300377 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300378 u8 wbuf[1];
379 u8 rbuf[4];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300380 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
381 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
382 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300383 u8 hdr_core;
384 u16 hdr_addr, hdr_data_len, hdr_checksum;
385 #define MAX_DATA 57
386 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300387
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300388 /*
389 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
390 *
391 * byte 0: MCS 51 core
392 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
393 * address spaces
394 * byte 1-2: Big endian destination address
395 * byte 3-4: Big endian number of data bytes following the header
396 * byte 5-6: Big endian header checksum, apparently ignored by the chip
397 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
398 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300399
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300400 for (i = fw->size; i > HDR_SIZE;) {
401 hdr_core = fw->data[fw->size - i + 0];
402 hdr_addr = fw->data[fw->size - i + 1] << 8;
403 hdr_addr |= fw->data[fw->size - i + 2] << 0;
404 hdr_data_len = fw->data[fw->size - i + 3] << 8;
405 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
406 hdr_checksum = fw->data[fw->size - i + 5] << 8;
407 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300408
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300409 pr_debug("%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
410 __func__, hdr_core, hdr_addr, hdr_data_len,
411 hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300412
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300413 if (((hdr_core != 1) && (hdr_core != 2)) ||
414 (hdr_data_len > i)) {
415 pr_debug("%s: bad firmware\n", __func__);
416 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300417 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300418
419 /* download begin packet */
420 req.cmd = CMD_FW_DL_BEGIN;
421 ret = af9035_ctrl_msg(udev, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300422 if (ret < 0)
423 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300424
425 /* download firmware packet(s) */
426 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
427 len = j;
428 if (len > MAX_DATA)
429 len = MAX_DATA;
430 req_fw_dl.wlen = len;
431 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
432 HDR_SIZE + hdr_data_len - j];
433 ret = af9035_ctrl_msg(udev, &req_fw_dl);
434 if (ret < 0)
435 goto err;
436 }
437
438 /* download end packet */
439 req.cmd = CMD_FW_DL_END;
440 ret = af9035_ctrl_msg(udev, &req);
441 if (ret < 0)
442 goto err;
443
444 i -= hdr_data_len + HDR_SIZE;
445
Gianluca Gennari6ec129882012-04-02 20:32:58 -0300446 pr_debug("%s: data uploaded=%zu\n", __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300447 }
448
449 /* firmware loaded, request boot */
450 req.cmd = CMD_FW_BOOT;
451 ret = af9035_ctrl_msg(udev, &req);
452 if (ret < 0)
453 goto err;
454
455 /* ensure firmware starts */
456 wbuf[0] = 1;
457 ret = af9035_ctrl_msg(udev, &req_fw_ver);
458 if (ret < 0)
459 goto err;
460
Antti Palosaari7f882c22012-03-30 09:10:08 -0300461 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300462 info("firmware did not run");
Antti Palosaari7f882c22012-03-30 09:10:08 -0300463 ret = -ENODEV;
464 goto err;
465 }
466
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300467 info("firmware version=%d.%d.%d.%d", rbuf[0], rbuf[1], rbuf[2],
468 rbuf[3]);
469
Antti Palosaari7f882c22012-03-30 09:10:08 -0300470 return 0;
471
472err:
473 pr_debug("%s: failed=%d\n", __func__, ret);
474
475 return ret;
476}
477
478/* abuse that callback as there is no better one for reading eeprom */
479static int af9035_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
480{
481 int ret, i, eeprom_shift = 0;
482 u8 tmp;
483 u16 tmp16;
484
485 /* check if there is dual tuners */
486 ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
487 if (ret < 0)
488 goto err;
489
490 af9035_config.dual_mode = tmp;
491 pr_debug("%s: dual mode=%d\n", __func__, af9035_config.dual_mode);
492
493 for (i = 0; i < af9035_properties[0].num_adapters; i++) {
494 /* tuner */
495 ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
496 if (ret < 0)
497 goto err;
498
499 af9035_af9033_config[i].tuner = tmp;
500 pr_debug("%s: [%d]tuner=%02x\n", __func__, i, tmp);
501
502 switch (tmp) {
503 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300504 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300505 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300506 case AF9033_TUNER_TDA18218:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300507 af9035_af9033_config[i].spec_inv = 1;
508 break;
509 default:
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300510 af9035_config.hw_not_supported = true;
511 warn("tuner ID=%02x not supported, please report!",
Antti Palosaari7f882c22012-03-30 09:10:08 -0300512 tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300513 };
514
515 /* tuner IF frequency */
516 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
517 if (ret < 0)
518 goto err;
519
520 tmp16 = tmp;
521
522 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
523 if (ret < 0)
524 goto err;
525
526 tmp16 |= tmp << 8;
527
528 pr_debug("%s: [%d]IF=%d\n", __func__, i, tmp16);
529
530 eeprom_shift = 0x10; /* shift for the 2nd tuner params */
531 }
532
533 /* get demod clock */
534 ret = af9035_rd_reg(d, 0x00d800, &tmp);
535 if (ret < 0)
536 goto err;
537
538 tmp = (tmp >> 0) & 0x0f;
539
540 for (i = 0; i < af9035_properties[0].num_adapters; i++)
541 af9035_af9033_config[i].clock = clock_lut[tmp];
542
543 return 0;
544
545err:
546 pr_debug("%s: failed=%d\n", __func__, ret);
547
548 return ret;
549}
550
Michael Büschffc501f2012-04-02 12:18:36 -0300551static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
552 int cmd, int arg)
553{
554 int err;
555
556 switch (cmd) {
557 case FC0011_FE_CALLBACK_POWER:
558 /* Tuner enable */
559 err = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
560 if (err)
561 return err;
562 err = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
563 if (err)
564 return err;
565 err = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
566 if (err)
567 return err;
568 /* LED */
569 err = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
570 if (err)
571 return err;
572 err = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
573 if (err)
574 return err;
575 msleep(10);
576 break;
577 case FC0011_FE_CALLBACK_RESET:
578 err = af9035_wr_reg(d, 0xd8e9, 1);
579 if (err)
580 return err;
581 err = af9035_wr_reg(d, 0xd8e8, 1);
582 if (err)
583 return err;
584 err = af9035_wr_reg(d, 0xd8e7, 1);
585 if (err)
586 return err;
587 msleep(10);
588 err = af9035_wr_reg(d, 0xd8e7, 0);
589 if (err)
590 return err;
591 msleep(10);
592 break;
593 default:
594 return -EINVAL;
595 }
596
597 return 0;
598}
599
600static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
601{
602 switch (af9035_af9033_config[0].tuner) {
603 case AF9033_TUNER_FC0011:
604 return af9035_fc0011_tuner_callback(d, cmd, arg);
605 default:
606 break;
607 }
608
609 return -ENODEV;
610}
611
612static int af9035_frontend_callback(void *adapter_priv, int component,
613 int cmd, int arg)
614{
615 struct i2c_adapter *adap = adapter_priv;
616 struct dvb_usb_device *d = i2c_get_adapdata(adap);
617
618 switch (component) {
619 case DVB_FRONTEND_COMPONENT_TUNER:
620 return af9035_tuner_callback(d, cmd, arg);
621 default:
622 break;
623 }
624
625 return -EINVAL;
626}
627
Antti Palosaari7f882c22012-03-30 09:10:08 -0300628static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
629{
630 int ret;
631
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300632 if (af9035_config.hw_not_supported) {
633 ret = -ENODEV;
634 goto err;
635 }
636
Antti Palosaari7f882c22012-03-30 09:10:08 -0300637 if (adap->id == 0) {
638 ret = af9035_wr_reg(adap->dev, 0x00417f,
639 af9035_af9033_config[1].i2c_addr);
640 if (ret < 0)
641 goto err;
642
643 ret = af9035_wr_reg(adap->dev, 0x00d81a,
644 af9035_config.dual_mode);
645 if (ret < 0)
646 goto err;
647 }
648
649 /* attach demodulator */
650 adap->fe_adap[0].fe = dvb_attach(af9033_attach,
651 &af9035_af9033_config[adap->id], &adap->dev->i2c_adap);
652 if (adap->fe_adap[0].fe == NULL) {
653 ret = -ENODEV;
654 goto err;
655 }
Michael Büschffc501f2012-04-02 12:18:36 -0300656 adap->fe_adap[0].fe->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300657
658 return 0;
659
660err:
661 pr_debug("%s: failed=%d\n", __func__, ret);
662
663 return ret;
664}
665
666static struct tua9001_config af9035_tua9001_config = {
667 .i2c_addr = 0x60,
668};
669
Michael Büschffc501f2012-04-02 12:18:36 -0300670static const struct fc0011_config af9035_fc0011_config = {
671 .i2c_address = 0x60,
672};
673
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300674static struct mxl5007t_config af9035_mxl5007t_config = {
675 .xtal_freq_hz = MxL_XTAL_24_MHZ,
676 .if_freq_hz = MxL_IF_4_57_MHZ,
677 .invert_if = 0,
678 .loop_thru_enable = 0,
679 .clk_out_enable = 0,
680 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
681};
682
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300683static struct tda18218_config af9035_tda18218_config = {
684 .i2c_address = 0x60,
685 .i2c_wr_max = 21,
686};
687
Antti Palosaari7f882c22012-03-30 09:10:08 -0300688static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
689{
690 int ret;
691 struct dvb_frontend *fe;
692
693 switch (af9035_af9033_config[adap->id].tuner) {
694 case AF9033_TUNER_TUA9001:
695 /* AF9035 gpiot3 = TUA9001 RESETN
696 AF9035 gpiot2 = TUA9001 RXEN */
697
698 /* configure gpiot2 and gpiot2 as output */
699 ret = af9035_wr_reg_mask(adap->dev, 0x00d8ec, 0x01, 0x01);
700 if (ret < 0)
701 goto err;
702
703 ret = af9035_wr_reg_mask(adap->dev, 0x00d8ed, 0x01, 0x01);
704 if (ret < 0)
705 goto err;
706
707 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e8, 0x01, 0x01);
708 if (ret < 0)
709 goto err;
710
711 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e9, 0x01, 0x01);
712 if (ret < 0)
713 goto err;
714
715 /* reset tuner */
716 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x00, 0x01);
717 if (ret < 0)
718 goto err;
719
720 usleep_range(2000, 20000);
721
722 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x01, 0x01);
723 if (ret < 0)
724 goto err;
725
726 /* activate tuner RX */
727 /* TODO: use callback for TUA9001 RXEN */
728 ret = af9035_wr_reg_mask(adap->dev, 0x00d8eb, 0x01, 0x01);
729 if (ret < 0)
730 goto err;
731
732 /* attach tuner */
733 fe = dvb_attach(tua9001_attach, adap->fe_adap[0].fe,
734 &adap->dev->i2c_adap, &af9035_tua9001_config);
735 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300736 case AF9033_TUNER_FC0011:
737 fe = dvb_attach(fc0011_attach, adap->fe_adap[0].fe,
738 &adap->dev->i2c_adap, &af9035_fc0011_config);
739 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300740 case AF9033_TUNER_MXL5007T:
741 ret = af9035_wr_reg(adap->dev, 0x00d8e0, 1);
742 if (ret < 0)
743 goto err;
744 ret = af9035_wr_reg(adap->dev, 0x00d8e1, 1);
745 if (ret < 0)
746 goto err;
747 ret = af9035_wr_reg(adap->dev, 0x00d8df, 0);
748 if (ret < 0)
749 goto err;
750
751 msleep(30);
752
753 ret = af9035_wr_reg(adap->dev, 0x00d8df, 1);
754 if (ret < 0)
755 goto err;
756
757 msleep(300);
758
759 ret = af9035_wr_reg(adap->dev, 0x00d8c0, 1);
760 if (ret < 0)
761 goto err;
762 ret = af9035_wr_reg(adap->dev, 0x00d8c1, 1);
763 if (ret < 0)
764 goto err;
765 ret = af9035_wr_reg(adap->dev, 0x00d8bf, 0);
766 if (ret < 0)
767 goto err;
768 ret = af9035_wr_reg(adap->dev, 0x00d8b4, 1);
769 if (ret < 0)
770 goto err;
771 ret = af9035_wr_reg(adap->dev, 0x00d8b5, 1);
772 if (ret < 0)
773 goto err;
774 ret = af9035_wr_reg(adap->dev, 0x00d8b3, 1);
775 if (ret < 0)
776 goto err;
777
778 /* attach tuner */
779 fe = dvb_attach(mxl5007t_attach, adap->fe_adap[0].fe,
780 &adap->dev->i2c_adap, 0x60, &af9035_mxl5007t_config);
781 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300782 case AF9033_TUNER_TDA18218:
783 /* attach tuner */
784 fe = dvb_attach(tda18218_attach, adap->fe_adap[0].fe,
785 &adap->dev->i2c_adap, &af9035_tda18218_config);
786 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300787 default:
788 fe = NULL;
789 }
790
791 if (fe == NULL) {
792 ret = -ENODEV;
793 goto err;
794 }
795
796 return 0;
797
798err:
799 pr_debug("%s: failed=%d\n", __func__, ret);
800
801 return ret;
802}
803
804enum af9035_id_entry {
805 AF9035_0CCD_0093,
Michael Büsch1083a0f2012-04-02 12:34:52 -0300806 AF9035_15A4_9035,
807 AF9035_15A4_1001,
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300808 AF9035_07CA_A835,
809 AF9035_07CA_B835,
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300810 AF9035_07CA_1867,
Gianluca Gennari728827b2012-04-02 17:25:13 -0300811 AF9035_07CA_A867,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300812};
813
814static struct usb_device_id af9035_id[] = {
815 [AF9035_0CCD_0093] = {
816 USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK)},
Michael Büsch1083a0f2012-04-02 12:34:52 -0300817 [AF9035_15A4_9035] = {
818 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035)},
819 [AF9035_15A4_1001] = {
820 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_2)},
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300821 [AF9035_07CA_A835] = {
822 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835)},
823 [AF9035_07CA_B835] = {
824 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835)},
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300825 [AF9035_07CA_1867] = {
826 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867)},
Gianluca Gennari728827b2012-04-02 17:25:13 -0300827 [AF9035_07CA_A867] = {
828 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867)},
Antti Palosaari7f882c22012-03-30 09:10:08 -0300829 {},
830};
831
832MODULE_DEVICE_TABLE(usb, af9035_id);
833
834static struct dvb_usb_device_properties af9035_properties[] = {
835 {
836 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
837
838 .usb_ctrl = DEVICE_SPECIFIC,
839 .download_firmware = af9035_download_firmware,
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300840 .firmware = "dvb-usb-af9035-02.fw",
Antti Palosaari7f882c22012-03-30 09:10:08 -0300841 .no_reconnect = 1,
842
843 .num_adapters = 1,
844 .adapter = {
845 {
846 .num_frontends = 1,
847 .fe = {
848 {
849 .frontend_attach = af9035_frontend_attach,
850 .tuner_attach = af9035_tuner_attach,
851 .stream = {
852 .type = USB_BULK,
853 .count = 6,
854 .endpoint = 0x84,
855 .u = {
856 .bulk = {
857 .buffersize = (87 * 188),
858 }
859 }
860 }
861 }
862 }
863 }
864 },
865
866 .identify_state = af9035_identify_state,
867 .read_mac_address = af9035_read_mac_address,
868
869 .i2c_algo = &af9035_i2c_algo,
870
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300871 .num_device_descs = 4,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300872 .devices = {
873 {
874 .name = "TerraTec Cinergy T Stick",
875 .cold_ids = {
876 &af9035_id[AF9035_0CCD_0093],
877 },
Michael Büsch1083a0f2012-04-02 12:34:52 -0300878 }, {
879 .name = "Afatech Technologies DVB-T stick",
880 .cold_ids = {
881 &af9035_id[AF9035_15A4_9035],
882 &af9035_id[AF9035_15A4_1001],
883 },
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300884 }, {
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300885 .name = "AVerMedia AVerTV Volar HD/PRO (A835)",
886 .cold_ids = {
887 &af9035_id[AF9035_07CA_A835],
888 &af9035_id[AF9035_07CA_B835],
889 },
890 }, {
Gianluca Gennari728827b2012-04-02 17:25:13 -0300891 .name = "AVerMedia HD Volar (A867)",
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300892 .cold_ids = {
893 &af9035_id[AF9035_07CA_1867],
Gianluca Gennari728827b2012-04-02 17:25:13 -0300894 &af9035_id[AF9035_07CA_A867],
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300895 },
896 },
Antti Palosaari7f882c22012-03-30 09:10:08 -0300897 }
898 },
899};
900
901static int af9035_usb_probe(struct usb_interface *intf,
902 const struct usb_device_id *id)
903{
904 int ret, i;
905 struct dvb_usb_device *d = NULL;
906 struct usb_device *udev;
907 bool found;
908
909 pr_debug("%s: interface=%d\n", __func__,
910 intf->cur_altsetting->desc.bInterfaceNumber);
911
912 /* interface 0 is used by DVB-T receiver and
913 interface 1 is for remote controller (HID) */
914 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
915 return 0;
916
917 /* Dynamic USB ID support. Replaces first device ID with current one. */
918 udev = interface_to_usbdev(intf);
919
920 for (i = 0, found = false; i < ARRAY_SIZE(af9035_id) - 1; i++) {
921 if (af9035_id[i].idVendor ==
922 le16_to_cpu(udev->descriptor.idVendor) &&
923 af9035_id[i].idProduct ==
924 le16_to_cpu(udev->descriptor.idProduct)) {
925 found = true;
926 break;
927 }
928 }
929
930 if (!found) {
931 pr_debug("%s: using dynamic ID %04x:%04x\n", __func__,
932 le16_to_cpu(udev->descriptor.idVendor),
933 le16_to_cpu(udev->descriptor.idProduct));
934 af9035_properties[0].devices[0].cold_ids[0]->idVendor =
935 le16_to_cpu(udev->descriptor.idVendor);
936 af9035_properties[0].devices[0].cold_ids[0]->idProduct =
937 le16_to_cpu(udev->descriptor.idProduct);
938 }
939
940
941 for (i = 0; i < af9035_properties_count; i++) {
942 ret = dvb_usb_device_init(intf, &af9035_properties[i],
943 THIS_MODULE, &d, adapter_nr);
944
945 if (ret == -ENODEV)
946 continue;
947 else
948 break;
949 }
950
951 if (ret < 0)
952 goto err;
953
954 if (d) {
955 ret = af9035_init(d);
956 if (ret < 0)
957 goto err;
958 }
959
960 return 0;
961
962err:
963 pr_debug("%s: failed=%d\n", __func__, ret);
964
965 return ret;
966}
967
968/* usb specific object needed to register this driver with the usb subsystem */
969static struct usb_driver af9035_usb_driver = {
970 .name = "dvb_usb_af9035",
971 .probe = af9035_usb_probe,
972 .disconnect = dvb_usb_device_exit,
973 .id_table = af9035_id,
974};
975
976/* module stuff */
977static int __init af9035_usb_module_init(void)
978{
979 int ret;
980
981 ret = usb_register(&af9035_usb_driver);
982 if (ret < 0)
983 goto err;
984
985 return 0;
986
987err:
988 pr_debug("%s: failed=%d\n", __func__, ret);
989
990 return ret;
991}
992
993static void __exit af9035_usb_module_exit(void)
994{
995 /* deregister this driver from the USB subsystem */
996 usb_deregister(&af9035_usb_driver);
997}
998
999module_init(af9035_usb_module_init);
1000module_exit(af9035_usb_module_exit);
1001
1002MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1003MODULE_DESCRIPTION("Afatech AF9035 driver");
1004MODULE_LICENSE("GPL");