blob: a7e05a18c35f7df3c62b350757d8f95bccad9d74 [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"
Antti Palosaari7f882c22012-03-30 09:10:08 -030026
27DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
28static DEFINE_MUTEX(af9035_usb_mutex);
29static struct config af9035_config;
30static struct dvb_usb_device_properties af9035_properties[1];
31static int af9035_properties_count = ARRAY_SIZE(af9035_properties);
32static struct af9033_config af9035_af9033_config[] = {
33 {
34 .ts_mode = AF9033_TS_MODE_USB,
35 }, {
36 .ts_mode = AF9033_TS_MODE_SERIAL,
37 }
38};
39
Michael Büschb1a95992012-04-01 16:33:48 -030040static u16 af9035_checksum(const u8 *buf, size_t len)
41{
42 size_t i;
43 u16 checksum = 0;
44
45 for (i = 1; i < len; i++) {
46 if (i % 2)
47 checksum += buf[i] << 8;
48 else
49 checksum += buf[i];
50 }
51 checksum = ~checksum;
52
53 return checksum;
54}
55
Antti Palosaari7f882c22012-03-30 09:10:08 -030056static int af9035_ctrl_msg(struct usb_device *udev, struct usb_req *req)
57{
58#define BUF_LEN 63
59#define REQ_HDR_LEN 4 /* send header size */
60#define ACK_HDR_LEN 3 /* rece header size */
61#define CHECKSUM_LEN 2
62#define USB_TIMEOUT 2000
63
Michael Büschb1a95992012-04-01 16:33:48 -030064 int ret, act_len;
Antti Palosaari7f882c22012-03-30 09:10:08 -030065 u8 buf[BUF_LEN];
66 u32 msg_len;
67 static u8 seq; /* packet sequence number */
Michael Büschb1a95992012-04-01 16:33:48 -030068 u16 checksum, tmpsum;
Antti Palosaari7f882c22012-03-30 09:10:08 -030069
70 /* buffer overflow check */
71 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
72 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
73 pr_debug("%s: too much data wlen=%d rlen=%d\n", __func__,
74 req->wlen, req->rlen);
75 return -EINVAL;
76 }
77
78 if (mutex_lock_interruptible(&af9035_usb_mutex) < 0)
79 return -EAGAIN;
80
81 buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
82 buf[1] = req->mbox;
83 buf[2] = req->cmd;
84 buf[3] = seq++;
85 if (req->wlen)
86 memcpy(&buf[4], req->wbuf, req->wlen);
87
88 /* calc and add checksum */
Michael Büschb1a95992012-04-01 16:33:48 -030089 checksum = af9035_checksum(buf, buf[0] - 1);
Antti Palosaari7f882c22012-03-30 09:10:08 -030090 buf[buf[0]-1] = (checksum >> 8);
91 buf[buf[0]-0] = (checksum & 0xff);
92
93 msg_len = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN ;
94
95 /* send req */
96 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
97 &act_len, USB_TIMEOUT);
98 if (ret < 0)
99 err("bulk message failed=%d (%d/%d)", ret, msg_len, act_len);
100 else
101 if (act_len != msg_len)
102 ret = -EIO; /* all data is not send */
103 if (ret < 0)
104 goto err_mutex_unlock;
105
106 /* no ack for those packets */
107 if (req->cmd == CMD_FW_DL)
108 goto exit_mutex_unlock;
109
110 /* receive ack and data if read req */
111 msg_len = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
112 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
113 &act_len, USB_TIMEOUT);
114 if (ret < 0) {
115 err("recv bulk message failed=%d", ret);
116 ret = -EIO;
117 goto err_mutex_unlock;
118 }
Michael Büschb1a95992012-04-01 16:33:48 -0300119 if (act_len != msg_len) {
120 err("recv bulk message truncated (%d != %u)\n",
121 act_len, (unsigned int)msg_len);
122 ret = -EIO;
123 goto err_mutex_unlock;
124 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300125
Michael Büschb1a95992012-04-01 16:33:48 -0300126 /* verify checksum */
127 checksum = af9035_checksum(buf, act_len - 2);
128 tmpsum = (buf[act_len - 2] << 8) | buf[act_len - 1];
129 if (tmpsum != checksum) {
130 err("%s: command=%02X checksum mismatch (%04X != %04X)\n",
131 __func__, req->cmd,
132 (unsigned int)tmpsum, (unsigned int)checksum);
133 ret = -EIO;
134 goto err_mutex_unlock;
135 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300136 /* check status */
137 if (buf[2]) {
138 pr_debug("%s: command=%02x failed fw error=%d\n", __func__,
139 req->cmd, buf[2]);
140 ret = -EIO;
141 goto err_mutex_unlock;
142 }
143
144 /* read request, copy returned data to return buf */
145 if (req->rlen)
146 memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen);
147
148err_mutex_unlock:
149exit_mutex_unlock:
150 mutex_unlock(&af9035_usb_mutex);
151
152 return ret;
153}
154
155/* write multiple registers */
156static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
157{
158 u8 wbuf[6 + len];
159 u8 mbox = (reg >> 16) & 0xff;
160 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
161
162 wbuf[0] = len;
163 wbuf[1] = 2;
164 wbuf[2] = 0;
165 wbuf[3] = 0;
166 wbuf[4] = (reg >> 8) & 0xff;
167 wbuf[5] = (reg >> 0) & 0xff;
168 memcpy(&wbuf[6], val, len);
169
170 return af9035_ctrl_msg(d->udev, &req);
171}
172
173/* read multiple registers */
174static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
175{
176 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
177 u8 mbox = (reg >> 16) & 0xff;
178 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
179
180 return af9035_ctrl_msg(d->udev, &req);
181}
182
183/* write single register */
184static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
185{
186 return af9035_wr_regs(d, reg, &val, 1);
187}
188
189/* read single register */
190static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
191{
192 return af9035_rd_regs(d, reg, val, 1);
193}
194
195/* write single register with mask */
196static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
197 u8 mask)
198{
199 int ret;
200 u8 tmp;
201
202 /* no need for read if whole reg is written */
203 if (mask != 0xff) {
204 ret = af9035_rd_regs(d, reg, &tmp, 1);
205 if (ret)
206 return ret;
207
208 val &= mask;
209 tmp &= ~mask;
210 val |= tmp;
211 }
212
213 return af9035_wr_regs(d, reg, &val, 1);
214}
215
216static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
217 struct i2c_msg msg[], int num)
218{
219 struct dvb_usb_device *d = i2c_get_adapdata(adap);
220 int ret;
221
222 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
223 return -EAGAIN;
224
225 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
226 (msg[1].flags & I2C_M_RD)) {
227 if (msg[0].len > 40 || msg[1].len > 40) {
228 /* TODO: correct limits > 40 */
229 ret = -EOPNOTSUPP;
230 } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) {
231 /* integrated demod */
232 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
233 msg[0].buf[2];
234 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
235 msg[1].len);
236 } else {
237 /* I2C */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300238 u8 buf[4 + msg[0].len];
239 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
240 buf, msg[1].len, msg[1].buf };
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300241 buf[0] = msg[1].len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300242 buf[1] = msg[0].addr << 1;
243 buf[2] = 0x01;
244 buf[3] = 0x00;
245 memcpy(&buf[4], msg[0].buf, msg[0].len);
246 ret = af9035_ctrl_msg(d->udev, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300247 }
248 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
249 if (msg[0].len > 40) {
250 /* TODO: correct limits > 40 */
251 ret = -EOPNOTSUPP;
252 } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) {
253 /* integrated demod */
254 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
255 msg[0].buf[2];
256 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
257 msg[0].len - 3);
258 } else {
259 /* I2C */
260 u8 buf[4 + msg[0].len];
261 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
262 0, NULL };
263 buf[0] = msg[0].len;
264 buf[1] = msg[0].addr << 1;
265 buf[2] = 0x01;
266 buf[3] = 0x00;
267 memcpy(&buf[4], msg[0].buf, msg[0].len);
268 ret = af9035_ctrl_msg(d->udev, &req);
269 }
270 } else {
271 /*
272 * We support only two kind of I2C transactions:
273 * 1) 1 x read + 1 x write
274 * 2) 1 x write
275 */
276 ret = -EOPNOTSUPP;
277 }
278
279 mutex_unlock(&d->i2c_mutex);
280
281 if (ret < 0)
282 return ret;
283 else
284 return num;
285}
286
287static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
288{
289 return I2C_FUNC_I2C;
290}
291
292static struct i2c_algorithm af9035_i2c_algo = {
293 .master_xfer = af9035_i2c_master_xfer,
294 .functionality = af9035_i2c_functionality,
295};
296
297static int af9035_init(struct dvb_usb_device *d)
298{
299 int ret, i;
300 u16 frame_size = 87 * 188 / 4;
301 u8 packet_size = 512 / 4;
302 struct reg_val_mask tab[] = {
303 { 0x80f99d, 0x01, 0x01 },
304 { 0x80f9a4, 0x01, 0x01 },
305 { 0x00dd11, 0x00, 0x20 },
306 { 0x00dd11, 0x00, 0x40 },
307 { 0x00dd13, 0x00, 0x20 },
308 { 0x00dd13, 0x00, 0x40 },
309 { 0x00dd11, 0x20, 0x20 },
310 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
311 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
312 { 0x00dd0c, packet_size, 0xff},
313 { 0x00dd11, af9035_config.dual_mode << 6, 0x40 },
314 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
315 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
316 { 0x00dd0d, packet_size, 0xff },
317 { 0x80f9a3, 0x00, 0x01 },
318 { 0x80f9cd, 0x00, 0x01 },
319 { 0x80f99d, 0x00, 0x01 },
320 { 0x80f9a4, 0x00, 0x01 },
321 };
322
323 pr_debug("%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
324 __func__, d->udev->speed, frame_size, packet_size);
325
326 /* init endpoints */
327 for (i = 0; i < ARRAY_SIZE(tab); i++) {
328 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
329 tab[i].mask);
330 if (ret < 0)
331 goto err;
332 }
333
334 return 0;
335
336err:
337 pr_debug("%s: failed=%d\n", __func__, ret);
338
339 return ret;
340}
341
342static int af9035_identify_state(struct usb_device *udev,
343 struct dvb_usb_device_properties *props,
344 struct dvb_usb_device_description **desc,
345 int *cold)
346{
347 int ret;
348 u8 wbuf[1] = { 1 };
349 u8 rbuf[4];
350 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
351 sizeof(rbuf), rbuf };
352
353 ret = af9035_ctrl_msg(udev, &req);
354 if (ret < 0)
355 goto err;
356
357 pr_debug("%s: reply=%02x %02x %02x %02x\n", __func__,
358 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
359 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
360 *cold = 0;
361 else
362 *cold = 1;
363
364 return 0;
365
366err:
367 pr_debug("%s: failed=%d\n", __func__, ret);
368
369 return ret;
370}
371
372static int af9035_download_firmware(struct usb_device *udev,
373 const struct firmware *fw)
374{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300375 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300376 u8 wbuf[1];
377 u8 rbuf[4];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300378 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
379 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
380 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300381 u8 hdr_core;
382 u16 hdr_addr, hdr_data_len, hdr_checksum;
383 #define MAX_DATA 57
384 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300385
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300386 /*
387 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
388 *
389 * byte 0: MCS 51 core
390 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
391 * address spaces
392 * byte 1-2: Big endian destination address
393 * byte 3-4: Big endian number of data bytes following the header
394 * byte 5-6: Big endian header checksum, apparently ignored by the chip
395 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
396 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300397
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300398 for (i = fw->size; i > HDR_SIZE;) {
399 hdr_core = fw->data[fw->size - i + 0];
400 hdr_addr = fw->data[fw->size - i + 1] << 8;
401 hdr_addr |= fw->data[fw->size - i + 2] << 0;
402 hdr_data_len = fw->data[fw->size - i + 3] << 8;
403 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
404 hdr_checksum = fw->data[fw->size - i + 5] << 8;
405 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300406
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300407 pr_debug("%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
408 __func__, hdr_core, hdr_addr, hdr_data_len,
409 hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300410
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300411 if (((hdr_core != 1) && (hdr_core != 2)) ||
412 (hdr_data_len > i)) {
413 pr_debug("%s: bad firmware\n", __func__);
414 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300415 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300416
417 /* download begin packet */
418 req.cmd = CMD_FW_DL_BEGIN;
419 ret = af9035_ctrl_msg(udev, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300420 if (ret < 0)
421 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300422
423 /* download firmware packet(s) */
424 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
425 len = j;
426 if (len > MAX_DATA)
427 len = MAX_DATA;
428 req_fw_dl.wlen = len;
429 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
430 HDR_SIZE + hdr_data_len - j];
431 ret = af9035_ctrl_msg(udev, &req_fw_dl);
432 if (ret < 0)
433 goto err;
434 }
435
436 /* download end packet */
437 req.cmd = CMD_FW_DL_END;
438 ret = af9035_ctrl_msg(udev, &req);
439 if (ret < 0)
440 goto err;
441
442 i -= hdr_data_len + HDR_SIZE;
443
Gianluca Gennari6ec129882012-04-02 20:32:58 -0300444 pr_debug("%s: data uploaded=%zu\n", __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300445 }
446
447 /* firmware loaded, request boot */
448 req.cmd = CMD_FW_BOOT;
449 ret = af9035_ctrl_msg(udev, &req);
450 if (ret < 0)
451 goto err;
452
453 /* ensure firmware starts */
454 wbuf[0] = 1;
455 ret = af9035_ctrl_msg(udev, &req_fw_ver);
456 if (ret < 0)
457 goto err;
458
Antti Palosaari7f882c22012-03-30 09:10:08 -0300459 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300460 info("firmware did not run");
Antti Palosaari7f882c22012-03-30 09:10:08 -0300461 ret = -ENODEV;
462 goto err;
463 }
464
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300465 info("firmware version=%d.%d.%d.%d", rbuf[0], rbuf[1], rbuf[2],
466 rbuf[3]);
467
Antti Palosaari7f882c22012-03-30 09:10:08 -0300468 return 0;
469
470err:
471 pr_debug("%s: failed=%d\n", __func__, ret);
472
473 return ret;
474}
475
476/* abuse that callback as there is no better one for reading eeprom */
477static int af9035_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
478{
479 int ret, i, eeprom_shift = 0;
480 u8 tmp;
481 u16 tmp16;
482
483 /* check if there is dual tuners */
484 ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
485 if (ret < 0)
486 goto err;
487
488 af9035_config.dual_mode = tmp;
489 pr_debug("%s: dual mode=%d\n", __func__, af9035_config.dual_mode);
490
491 for (i = 0; i < af9035_properties[0].num_adapters; i++) {
492 /* tuner */
493 ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
494 if (ret < 0)
495 goto err;
496
497 af9035_af9033_config[i].tuner = tmp;
498 pr_debug("%s: [%d]tuner=%02x\n", __func__, i, tmp);
499
500 switch (tmp) {
501 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300502 case AF9033_TUNER_FC0011:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300503 af9035_af9033_config[i].spec_inv = 1;
504 break;
505 default:
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300506 af9035_config.hw_not_supported = true;
507 warn("tuner ID=%02x not supported, please report!",
Antti Palosaari7f882c22012-03-30 09:10:08 -0300508 tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300509 };
510
511 /* tuner IF frequency */
512 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
513 if (ret < 0)
514 goto err;
515
516 tmp16 = tmp;
517
518 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
519 if (ret < 0)
520 goto err;
521
522 tmp16 |= tmp << 8;
523
524 pr_debug("%s: [%d]IF=%d\n", __func__, i, tmp16);
525
526 eeprom_shift = 0x10; /* shift for the 2nd tuner params */
527 }
528
529 /* get demod clock */
530 ret = af9035_rd_reg(d, 0x00d800, &tmp);
531 if (ret < 0)
532 goto err;
533
534 tmp = (tmp >> 0) & 0x0f;
535
536 for (i = 0; i < af9035_properties[0].num_adapters; i++)
537 af9035_af9033_config[i].clock = clock_lut[tmp];
538
539 return 0;
540
541err:
542 pr_debug("%s: failed=%d\n", __func__, ret);
543
544 return ret;
545}
546
Michael Büschffc501f2012-04-02 12:18:36 -0300547static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
548 int cmd, int arg)
549{
550 int err;
551
552 switch (cmd) {
553 case FC0011_FE_CALLBACK_POWER:
554 /* Tuner enable */
555 err = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
556 if (err)
557 return err;
558 err = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
559 if (err)
560 return err;
561 err = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
562 if (err)
563 return err;
564 /* LED */
565 err = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
566 if (err)
567 return err;
568 err = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
569 if (err)
570 return err;
571 msleep(10);
572 break;
573 case FC0011_FE_CALLBACK_RESET:
574 err = af9035_wr_reg(d, 0xd8e9, 1);
575 if (err)
576 return err;
577 err = af9035_wr_reg(d, 0xd8e8, 1);
578 if (err)
579 return err;
580 err = af9035_wr_reg(d, 0xd8e7, 1);
581 if (err)
582 return err;
583 msleep(10);
584 err = af9035_wr_reg(d, 0xd8e7, 0);
585 if (err)
586 return err;
587 msleep(10);
588 break;
589 default:
590 return -EINVAL;
591 }
592
593 return 0;
594}
595
596static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
597{
598 switch (af9035_af9033_config[0].tuner) {
599 case AF9033_TUNER_FC0011:
600 return af9035_fc0011_tuner_callback(d, cmd, arg);
601 default:
602 break;
603 }
604
605 return -ENODEV;
606}
607
608static int af9035_frontend_callback(void *adapter_priv, int component,
609 int cmd, int arg)
610{
611 struct i2c_adapter *adap = adapter_priv;
612 struct dvb_usb_device *d = i2c_get_adapdata(adap);
613
614 switch (component) {
615 case DVB_FRONTEND_COMPONENT_TUNER:
616 return af9035_tuner_callback(d, cmd, arg);
617 default:
618 break;
619 }
620
621 return -EINVAL;
622}
623
Antti Palosaari7f882c22012-03-30 09:10:08 -0300624static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
625{
626 int ret;
627
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300628 if (af9035_config.hw_not_supported) {
629 ret = -ENODEV;
630 goto err;
631 }
632
Antti Palosaari7f882c22012-03-30 09:10:08 -0300633 if (adap->id == 0) {
634 ret = af9035_wr_reg(adap->dev, 0x00417f,
635 af9035_af9033_config[1].i2c_addr);
636 if (ret < 0)
637 goto err;
638
639 ret = af9035_wr_reg(adap->dev, 0x00d81a,
640 af9035_config.dual_mode);
641 if (ret < 0)
642 goto err;
643 }
644
645 /* attach demodulator */
646 adap->fe_adap[0].fe = dvb_attach(af9033_attach,
647 &af9035_af9033_config[adap->id], &adap->dev->i2c_adap);
648 if (adap->fe_adap[0].fe == NULL) {
649 ret = -ENODEV;
650 goto err;
651 }
Michael Büschffc501f2012-04-02 12:18:36 -0300652 adap->fe_adap[0].fe->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300653
654 return 0;
655
656err:
657 pr_debug("%s: failed=%d\n", __func__, ret);
658
659 return ret;
660}
661
662static struct tua9001_config af9035_tua9001_config = {
663 .i2c_addr = 0x60,
664};
665
Michael Büschffc501f2012-04-02 12:18:36 -0300666static const struct fc0011_config af9035_fc0011_config = {
667 .i2c_address = 0x60,
668};
669
Antti Palosaari7f882c22012-03-30 09:10:08 -0300670static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
671{
672 int ret;
673 struct dvb_frontend *fe;
674
675 switch (af9035_af9033_config[adap->id].tuner) {
676 case AF9033_TUNER_TUA9001:
677 /* AF9035 gpiot3 = TUA9001 RESETN
678 AF9035 gpiot2 = TUA9001 RXEN */
679
680 /* configure gpiot2 and gpiot2 as output */
681 ret = af9035_wr_reg_mask(adap->dev, 0x00d8ec, 0x01, 0x01);
682 if (ret < 0)
683 goto err;
684
685 ret = af9035_wr_reg_mask(adap->dev, 0x00d8ed, 0x01, 0x01);
686 if (ret < 0)
687 goto err;
688
689 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e8, 0x01, 0x01);
690 if (ret < 0)
691 goto err;
692
693 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e9, 0x01, 0x01);
694 if (ret < 0)
695 goto err;
696
697 /* reset tuner */
698 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x00, 0x01);
699 if (ret < 0)
700 goto err;
701
702 usleep_range(2000, 20000);
703
704 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x01, 0x01);
705 if (ret < 0)
706 goto err;
707
708 /* activate tuner RX */
709 /* TODO: use callback for TUA9001 RXEN */
710 ret = af9035_wr_reg_mask(adap->dev, 0x00d8eb, 0x01, 0x01);
711 if (ret < 0)
712 goto err;
713
714 /* attach tuner */
715 fe = dvb_attach(tua9001_attach, adap->fe_adap[0].fe,
716 &adap->dev->i2c_adap, &af9035_tua9001_config);
717 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300718 case AF9033_TUNER_FC0011:
719 fe = dvb_attach(fc0011_attach, adap->fe_adap[0].fe,
720 &adap->dev->i2c_adap, &af9035_fc0011_config);
721 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300722 default:
723 fe = NULL;
724 }
725
726 if (fe == NULL) {
727 ret = -ENODEV;
728 goto err;
729 }
730
731 return 0;
732
733err:
734 pr_debug("%s: failed=%d\n", __func__, ret);
735
736 return ret;
737}
738
739enum af9035_id_entry {
740 AF9035_0CCD_0093,
Michael Büsch1083a0f2012-04-02 12:34:52 -0300741 AF9035_15A4_9035,
742 AF9035_15A4_1001,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300743};
744
745static struct usb_device_id af9035_id[] = {
746 [AF9035_0CCD_0093] = {
747 USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK)},
Michael Büsch1083a0f2012-04-02 12:34:52 -0300748 [AF9035_15A4_9035] = {
749 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035)},
750 [AF9035_15A4_1001] = {
751 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_2)},
Antti Palosaari7f882c22012-03-30 09:10:08 -0300752 {},
753};
754
755MODULE_DEVICE_TABLE(usb, af9035_id);
756
757static struct dvb_usb_device_properties af9035_properties[] = {
758 {
759 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
760
761 .usb_ctrl = DEVICE_SPECIFIC,
762 .download_firmware = af9035_download_firmware,
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300763 .firmware = "dvb-usb-af9035-02.fw",
Antti Palosaari7f882c22012-03-30 09:10:08 -0300764 .no_reconnect = 1,
765
766 .num_adapters = 1,
767 .adapter = {
768 {
769 .num_frontends = 1,
770 .fe = {
771 {
772 .frontend_attach = af9035_frontend_attach,
773 .tuner_attach = af9035_tuner_attach,
774 .stream = {
775 .type = USB_BULK,
776 .count = 6,
777 .endpoint = 0x84,
778 .u = {
779 .bulk = {
780 .buffersize = (87 * 188),
781 }
782 }
783 }
784 }
785 }
786 }
787 },
788
789 .identify_state = af9035_identify_state,
790 .read_mac_address = af9035_read_mac_address,
791
792 .i2c_algo = &af9035_i2c_algo,
793
Michael Büsch1083a0f2012-04-02 12:34:52 -0300794 .num_device_descs = 2,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300795 .devices = {
796 {
797 .name = "TerraTec Cinergy T Stick",
798 .cold_ids = {
799 &af9035_id[AF9035_0CCD_0093],
800 },
Michael Büsch1083a0f2012-04-02 12:34:52 -0300801 }, {
802 .name = "Afatech Technologies DVB-T stick",
803 .cold_ids = {
804 &af9035_id[AF9035_15A4_9035],
805 &af9035_id[AF9035_15A4_1001],
806 },
807 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300808 }
809 },
810};
811
812static int af9035_usb_probe(struct usb_interface *intf,
813 const struct usb_device_id *id)
814{
815 int ret, i;
816 struct dvb_usb_device *d = NULL;
817 struct usb_device *udev;
818 bool found;
819
820 pr_debug("%s: interface=%d\n", __func__,
821 intf->cur_altsetting->desc.bInterfaceNumber);
822
823 /* interface 0 is used by DVB-T receiver and
824 interface 1 is for remote controller (HID) */
825 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
826 return 0;
827
828 /* Dynamic USB ID support. Replaces first device ID with current one. */
829 udev = interface_to_usbdev(intf);
830
831 for (i = 0, found = false; i < ARRAY_SIZE(af9035_id) - 1; i++) {
832 if (af9035_id[i].idVendor ==
833 le16_to_cpu(udev->descriptor.idVendor) &&
834 af9035_id[i].idProduct ==
835 le16_to_cpu(udev->descriptor.idProduct)) {
836 found = true;
837 break;
838 }
839 }
840
841 if (!found) {
842 pr_debug("%s: using dynamic ID %04x:%04x\n", __func__,
843 le16_to_cpu(udev->descriptor.idVendor),
844 le16_to_cpu(udev->descriptor.idProduct));
845 af9035_properties[0].devices[0].cold_ids[0]->idVendor =
846 le16_to_cpu(udev->descriptor.idVendor);
847 af9035_properties[0].devices[0].cold_ids[0]->idProduct =
848 le16_to_cpu(udev->descriptor.idProduct);
849 }
850
851
852 for (i = 0; i < af9035_properties_count; i++) {
853 ret = dvb_usb_device_init(intf, &af9035_properties[i],
854 THIS_MODULE, &d, adapter_nr);
855
856 if (ret == -ENODEV)
857 continue;
858 else
859 break;
860 }
861
862 if (ret < 0)
863 goto err;
864
865 if (d) {
866 ret = af9035_init(d);
867 if (ret < 0)
868 goto err;
869 }
870
871 return 0;
872
873err:
874 pr_debug("%s: failed=%d\n", __func__, ret);
875
876 return ret;
877}
878
879/* usb specific object needed to register this driver with the usb subsystem */
880static struct usb_driver af9035_usb_driver = {
881 .name = "dvb_usb_af9035",
882 .probe = af9035_usb_probe,
883 .disconnect = dvb_usb_device_exit,
884 .id_table = af9035_id,
885};
886
887/* module stuff */
888static int __init af9035_usb_module_init(void)
889{
890 int ret;
891
892 ret = usb_register(&af9035_usb_driver);
893 if (ret < 0)
894 goto err;
895
896 return 0;
897
898err:
899 pr_debug("%s: failed=%d\n", __func__, ret);
900
901 return ret;
902}
903
904static void __exit af9035_usb_module_exit(void)
905{
906 /* deregister this driver from the USB subsystem */
907 usb_deregister(&af9035_usb_driver);
908}
909
910module_init(af9035_usb_module_init);
911module_exit(af9035_usb_module_exit);
912
913MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
914MODULE_DESCRIPTION("Afatech AF9035 driver");
915MODULE_LICENSE("GPL");