blob: c1ec18ccb0ba65b22e25edfe142e17c09850d49c [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"
Antti Palosaari7f882c22012-03-30 09:10:08 -030023
24DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
Antti Palosaari7f882c22012-03-30 09:10:08 -030025
Michael Büschb1a95992012-04-01 16:33:48 -030026static u16 af9035_checksum(const u8 *buf, size_t len)
27{
28 size_t i;
29 u16 checksum = 0;
30
31 for (i = 1; i < len; i++) {
32 if (i % 2)
33 checksum += buf[i] << 8;
34 else
35 checksum += buf[i];
36 }
37 checksum = ~checksum;
38
39 return checksum;
40}
41
Antti Palosaari5da2aec2012-06-17 23:15:03 -030042static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
Antti Palosaari7f882c22012-03-30 09:10:08 -030043{
Antti Palosaari6fb39c52012-04-06 16:32:30 -030044#define BUF_LEN 64
Antti Palosaari7f882c22012-03-30 09:10:08 -030045#define REQ_HDR_LEN 4 /* send header size */
46#define ACK_HDR_LEN 3 /* rece header size */
47#define CHECKSUM_LEN 2
48#define USB_TIMEOUT 2000
Antti Palosaari5da2aec2012-06-17 23:15:03 -030049 struct state *state = d_to_priv(d);
50 int ret, wlen, rlen;
Antti Palosaari7f882c22012-03-30 09:10:08 -030051 u8 buf[BUF_LEN];
Antti Palosaari6fb39c52012-04-06 16:32:30 -030052 u16 checksum, tmp_checksum;
Antti Palosaari7f882c22012-03-30 09:10:08 -030053
54 /* buffer overflow check */
55 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
Antti Palosaari119f7a82012-09-12 20:23:53 -030056 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
57 dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
58 __func__, req->wlen, req->rlen);
Antti Palosaari7f882c22012-03-30 09:10:08 -030059 return -EINVAL;
60 }
61
Antti Palosaari7f882c22012-03-30 09:10:08 -030062 buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
63 buf[1] = req->mbox;
64 buf[2] = req->cmd;
Antti Palosaari5da2aec2012-06-17 23:15:03 -030065 buf[3] = state->seq++;
66 memcpy(&buf[REQ_HDR_LEN], req->wbuf, req->wlen);
67
68 wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
69 rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
Antti Palosaari7f882c22012-03-30 09:10:08 -030070
71 /* calc and add checksum */
Michael Büschb1a95992012-04-01 16:33:48 -030072 checksum = af9035_checksum(buf, buf[0] - 1);
Antti Palosaari6fb39c52012-04-06 16:32:30 -030073 buf[buf[0] - 1] = (checksum >> 8);
74 buf[buf[0] - 0] = (checksum & 0xff);
Antti Palosaari7f882c22012-03-30 09:10:08 -030075
Antti Palosaari5da2aec2012-06-17 23:15:03 -030076 /* no ack for these packets */
77 if (req->cmd == CMD_FW_DL)
78 rlen = 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -030079
Antti Palosaari1162c7b2012-06-20 20:27:42 -030080 ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -030081 if (ret)
82 goto err;
Antti Palosaari7f882c22012-03-30 09:10:08 -030083
84 /* no ack for those packets */
85 if (req->cmd == CMD_FW_DL)
Antti Palosaari5da2aec2012-06-17 23:15:03 -030086 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -030087
Michael Büschb1a95992012-04-01 16:33:48 -030088 /* verify checksum */
Antti Palosaari5da2aec2012-06-17 23:15:03 -030089 checksum = af9035_checksum(buf, rlen - 2);
90 tmp_checksum = (buf[rlen - 2] << 8) | buf[rlen - 1];
Antti Palosaari6fb39c52012-04-06 16:32:30 -030091 if (tmp_checksum != checksum) {
Antti Palosaari119f7a82012-09-12 20:23:53 -030092 dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \
93 "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd,
94 tmp_checksum, checksum);
Michael Büschb1a95992012-04-01 16:33:48 -030095 ret = -EIO;
Antti Palosaari5da2aec2012-06-17 23:15:03 -030096 goto err;
Michael Büschb1a95992012-04-01 16:33:48 -030097 }
Antti Palosaari6fb39c52012-04-06 16:32:30 -030098
Antti Palosaari7f882c22012-03-30 09:10:08 -030099 /* check status */
100 if (buf[2]) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300101 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
102 __func__, req->cmd, buf[2]);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300103 ret = -EIO;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300104 goto err;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300105 }
106
107 /* read request, copy returned data to return buf */
108 if (req->rlen)
109 memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen);
110
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300111exit:
112 return 0;
113
114err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300115 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300116
117 return ret;
118}
119
120/* write multiple registers */
121static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
122{
123 u8 wbuf[6 + len];
124 u8 mbox = (reg >> 16) & 0xff;
125 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
126
127 wbuf[0] = len;
128 wbuf[1] = 2;
129 wbuf[2] = 0;
130 wbuf[3] = 0;
131 wbuf[4] = (reg >> 8) & 0xff;
132 wbuf[5] = (reg >> 0) & 0xff;
133 memcpy(&wbuf[6], val, len);
134
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300135 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300136}
137
138/* read multiple registers */
139static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
140{
141 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
142 u8 mbox = (reg >> 16) & 0xff;
143 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
144
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300145 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300146}
147
148/* write single register */
149static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
150{
151 return af9035_wr_regs(d, reg, &val, 1);
152}
153
154/* read single register */
155static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
156{
157 return af9035_rd_regs(d, reg, val, 1);
158}
159
160/* write single register with mask */
161static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
162 u8 mask)
163{
164 int ret;
165 u8 tmp;
166
167 /* no need for read if whole reg is written */
168 if (mask != 0xff) {
169 ret = af9035_rd_regs(d, reg, &tmp, 1);
170 if (ret)
171 return ret;
172
173 val &= mask;
174 tmp &= ~mask;
175 val |= tmp;
176 }
177
178 return af9035_wr_regs(d, reg, &val, 1);
179}
180
181static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
182 struct i2c_msg msg[], int num)
183{
184 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300185 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300186 int ret;
187
188 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
189 return -EAGAIN;
190
Antti Palosaariad30e912012-04-02 20:18:59 -0300191 /*
192 * I2C sub header is 5 bytes long. Meaning of those bytes are:
193 * 0: data len
194 * 1: I2C addr << 1
195 * 2: reg addr len
196 * byte 3 and 4 can be used as reg addr
197 * 3: reg addr MSB
198 * used when reg addr len is set to 2
199 * 4: reg addr LSB
200 * used when reg addr len is set to 1 or 2
201 *
202 * For the simplify we do not use register addr at all.
203 * NOTE: As a firmware knows tuner type there is very small possibility
204 * there could be some tuner I2C hacks done by firmware and this may
205 * lead problems if firmware expects those bytes are used.
206 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300207 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
208 (msg[1].flags & I2C_M_RD)) {
209 if (msg[0].len > 40 || msg[1].len > 40) {
210 /* TODO: correct limits > 40 */
211 ret = -EOPNOTSUPP;
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300212 } else if (msg[0].addr == state->af9033_config[0].i2c_addr) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300213 /* integrated demod */
214 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
215 msg[0].buf[2];
216 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
217 msg[1].len);
218 } else {
219 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300220 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300221 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
222 buf, msg[1].len, msg[1].buf };
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300223 buf[0] = msg[1].len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300224 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300225 buf[2] = 0x00; /* reg addr len */
226 buf[3] = 0x00; /* reg addr MSB */
227 buf[4] = 0x00; /* reg addr LSB */
228 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300229 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300230 }
231 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
232 if (msg[0].len > 40) {
233 /* TODO: correct limits > 40 */
234 ret = -EOPNOTSUPP;
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300235 } else if (msg[0].addr == state->af9033_config[0].i2c_addr) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300236 /* integrated demod */
237 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
238 msg[0].buf[2];
239 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
240 msg[0].len - 3);
241 } else {
242 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300243 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300244 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
245 0, NULL };
246 buf[0] = msg[0].len;
247 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300248 buf[2] = 0x00; /* reg addr len */
249 buf[3] = 0x00; /* reg addr MSB */
250 buf[4] = 0x00; /* reg addr LSB */
251 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300252 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300253 }
254 } else {
255 /*
256 * We support only two kind of I2C transactions:
257 * 1) 1 x read + 1 x write
258 * 2) 1 x write
259 */
260 ret = -EOPNOTSUPP;
261 }
262
263 mutex_unlock(&d->i2c_mutex);
264
265 if (ret < 0)
266 return ret;
267 else
268 return num;
269}
270
271static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
272{
273 return I2C_FUNC_I2C;
274}
275
276static struct i2c_algorithm af9035_i2c_algo = {
277 .master_xfer = af9035_i2c_master_xfer,
278 .functionality = af9035_i2c_functionality,
279};
280
Antti Palosaaria0921af2012-06-18 23:42:53 -0300281static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300282{
283 int ret;
284 u8 wbuf[1] = { 1 };
285 u8 rbuf[4];
286 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
287 sizeof(rbuf), rbuf };
288
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300289 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300290 if (ret < 0)
291 goto err;
292
Antti Palosaari119f7a82012-09-12 20:23:53 -0300293 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300294 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300295 ret = WARM;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300296 else
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300297 ret = COLD;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300298
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300299 return ret;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300300
301err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300302 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300303
304 return ret;
305}
306
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300307static int af9035_download_firmware(struct dvb_usb_device *d,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300308 const struct firmware *fw)
309{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300310 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300311 u8 wbuf[1];
312 u8 rbuf[4];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300313 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
314 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
315 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300316 u8 hdr_core;
317 u16 hdr_addr, hdr_data_len, hdr_checksum;
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300318 #define MAX_DATA 58
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300319 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300320
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300321 /*
322 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
323 *
324 * byte 0: MCS 51 core
325 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
326 * address spaces
327 * byte 1-2: Big endian destination address
328 * byte 3-4: Big endian number of data bytes following the header
329 * byte 5-6: Big endian header checksum, apparently ignored by the chip
330 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
331 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300332
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300333 for (i = fw->size; i > HDR_SIZE;) {
334 hdr_core = fw->data[fw->size - i + 0];
335 hdr_addr = fw->data[fw->size - i + 1] << 8;
336 hdr_addr |= fw->data[fw->size - i + 2] << 0;
337 hdr_data_len = fw->data[fw->size - i + 3] << 8;
338 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
339 hdr_checksum = fw->data[fw->size - i + 5] << 8;
340 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300341
Antti Palosaari119f7a82012-09-12 20:23:53 -0300342 dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
343 "checksum=%04x\n", __func__, hdr_core, hdr_addr,
344 hdr_data_len, hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300345
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300346 if (((hdr_core != 1) && (hdr_core != 2)) ||
347 (hdr_data_len > i)) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300348 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300349 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300350 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300351
352 /* download begin packet */
353 req.cmd = CMD_FW_DL_BEGIN;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300354 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300355 if (ret < 0)
356 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300357
358 /* download firmware packet(s) */
359 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
360 len = j;
361 if (len > MAX_DATA)
362 len = MAX_DATA;
363 req_fw_dl.wlen = len;
364 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
365 HDR_SIZE + hdr_data_len - j];
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300366 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300367 if (ret < 0)
368 goto err;
369 }
370
371 /* download end packet */
372 req.cmd = CMD_FW_DL_END;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300373 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300374 if (ret < 0)
375 goto err;
376
377 i -= hdr_data_len + HDR_SIZE;
378
Antti Palosaari119f7a82012-09-12 20:23:53 -0300379 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
380 __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300381 }
382
383 /* firmware loaded, request boot */
384 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300385 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300386 if (ret < 0)
387 goto err;
388
389 /* ensure firmware starts */
390 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300391 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300392 if (ret < 0)
393 goto err;
394
Antti Palosaari7f882c22012-03-30 09:10:08 -0300395 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300396 dev_err(&d->udev->dev, "%s: firmware did not run\n",
397 KBUILD_MODNAME);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300398 ret = -ENODEV;
399 goto err;
400 }
401
Antti Palosaari119f7a82012-09-12 20:23:53 -0300402 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
403 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300404
Antti Palosaari7f882c22012-03-30 09:10:08 -0300405 return 0;
406
407err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300408 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300409
410 return ret;
411}
412
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300413static int af9035_download_firmware_it9135(struct dvb_usb_device *d,
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300414 const struct firmware *fw)
415{
416 int ret, i, i_prev;
417 u8 wbuf[1];
418 u8 rbuf[4];
419 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
420 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
421 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
422 #define HDR_SIZE 7
423
424 /*
425 * There seems to be following firmware header. Meaning of bytes 0-3
426 * is unknown.
427 *
428 * 0: 3
429 * 1: 0, 1
430 * 2: 0
431 * 3: 1, 2, 3
432 * 4: addr MSB
433 * 5: addr LSB
434 * 6: count of data bytes ?
435 */
436
437 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
438 if (i == fw->size ||
439 (fw->data[i + 0] == 0x03 &&
440 (fw->data[i + 1] == 0x00 ||
441 fw->data[i + 1] == 0x01) &&
442 fw->data[i + 2] == 0x00)) {
443 req_fw_dl.wlen = i - i_prev;
444 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
445 i_prev = i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300446 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300447 if (ret < 0)
448 goto err;
449
Antti Palosaari119f7a82012-09-12 20:23:53 -0300450 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
451 __func__, i);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300452 }
453 }
454
455 /* firmware loaded, request boot */
456 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300457 ret = af9035_ctrl_msg(d, &req);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300458 if (ret < 0)
459 goto err;
460
461 /* ensure firmware starts */
462 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300463 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300464 if (ret < 0)
465 goto err;
466
467 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300468 dev_err(&d->udev->dev, "%s: firmware did not run\n",
469 KBUILD_MODNAME);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300470 ret = -ENODEV;
471 goto err;
472 }
473
Antti Palosaari119f7a82012-09-12 20:23:53 -0300474 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
475 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300476
477 return 0;
478
479err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300480 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300481
482 return ret;
483}
484
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300485static int af9035_read_config(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300486{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300487 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300488 int ret, i, eeprom_shift = 0;
489 u8 tmp;
490 u16 tmp16;
491
492 /* check if there is dual tuners */
493 ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
494 if (ret < 0)
495 goto err;
496
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300497 state->dual_mode = tmp;
Antti Palosaari119f7a82012-09-12 20:23:53 -0300498 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n",
499 __func__, state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300500
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300501 for (i = 0; i < state->dual_mode + 1; i++) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300502 /* tuner */
503 ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
504 if (ret < 0)
505 goto err;
506
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300507 state->af9033_config[i].tuner = tmp;
Antti Palosaari119f7a82012-09-12 20:23:53 -0300508 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
509 __func__, i, tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300510
511 switch (tmp) {
512 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300513 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300514 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300515 case AF9033_TUNER_TDA18218:
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300516 case AF9033_TUNER_FC2580:
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300517 case AF9033_TUNER_FC0012:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300518 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300519 break;
520 default:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300521 dev_warn(&d->udev->dev, "%s: tuner id=%02x not " \
522 "supported, please report!",
523 KBUILD_MODNAME, tmp);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300524 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300525
526 /* tuner IF frequency */
527 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
528 if (ret < 0)
529 goto err;
530
531 tmp16 = tmp;
532
533 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
534 if (ret < 0)
535 goto err;
536
537 tmp16 |= tmp << 8;
538
Antti Palosaari119f7a82012-09-12 20:23:53 -0300539 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300540
541 eeprom_shift = 0x10; /* shift for the 2nd tuner params */
542 }
543
544 /* get demod clock */
545 ret = af9035_rd_reg(d, 0x00d800, &tmp);
546 if (ret < 0)
547 goto err;
548
549 tmp = (tmp >> 0) & 0x0f;
550
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300551 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300552 state->af9033_config[i].clock = clock_lut[tmp];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300553
554 return 0;
555
556err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300557 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300558
559 return ret;
560}
561
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300562static int af9035_read_config_it9135(struct dvb_usb_device *d)
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300563{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300564 struct state *state = d_to_priv(d);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300565 int ret, i;
566 u8 tmp;
567
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300568 state->dual_mode = false;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300569
570 /* get demod clock */
571 ret = af9035_rd_reg(d, 0x00d800, &tmp);
572 if (ret < 0)
573 goto err;
574
575 tmp = (tmp >> 0) & 0x0f;
576
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300577 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300578 state->af9033_config[i].clock = clock_lut_it9135[tmp];
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300579
580 return 0;
581
582err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300583 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300584
585 return ret;
586}
587
Antti Palosaari51639be2012-09-16 22:26:56 -0300588static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
589 int cmd, int arg)
590{
591 int ret;
592 u8 val;
593
594 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
595
596 /*
597 * CEN always enabled by hardware wiring
598 * RESETN GPIOT3
599 * RXEN GPIOT2
600 */
601
602 switch (cmd) {
603 case TUA9001_CMD_RESETN:
604 if (arg)
605 val = 0x00;
606 else
607 val = 0x01;
608
609 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
610 if (ret < 0)
611 goto err;
612 break;
613 case TUA9001_CMD_RXEN:
614 if (arg)
615 val = 0x01;
616 else
617 val = 0x00;
618
619 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
620 if (ret < 0)
621 goto err;
622 break;
623 }
624
625 return 0;
626
627err:
628 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
629
630 return ret;
631}
632
633
Michael Büschffc501f2012-04-02 12:18:36 -0300634static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300635 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300636{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300637 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300638
639 switch (cmd) {
640 case FC0011_FE_CALLBACK_POWER:
641 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300642 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
643 if (ret < 0)
644 goto err;
645
646 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
647 if (ret < 0)
648 goto err;
649
650 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
651 if (ret < 0)
652 goto err;
653
Michael Büschffc501f2012-04-02 12:18:36 -0300654 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300655 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
656 if (ret < 0)
657 goto err;
658
659 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
660 if (ret < 0)
661 goto err;
662
Michael Büschde2bec52012-04-03 05:11:30 -0300663 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300664 break;
665 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300666 ret = af9035_wr_reg(d, 0xd8e9, 1);
667 if (ret < 0)
668 goto err;
669
670 ret = af9035_wr_reg(d, 0xd8e8, 1);
671 if (ret < 0)
672 goto err;
673
674 ret = af9035_wr_reg(d, 0xd8e7, 1);
675 if (ret < 0)
676 goto err;
677
Michael Büschde2bec52012-04-03 05:11:30 -0300678 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300679
680 ret = af9035_wr_reg(d, 0xd8e7, 0);
681 if (ret < 0)
682 goto err;
683
Michael Büschde2bec52012-04-03 05:11:30 -0300684 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300685 break;
686 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300687 ret = -EINVAL;
688 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300689 }
690
691 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300692
693err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300694 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300695
696 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300697}
698
699static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
700{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300701 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300702
703 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300704 case AF9033_TUNER_FC0011:
705 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300706 case AF9033_TUNER_TUA9001:
707 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300708 default:
709 break;
710 }
711
Antti Palosaari1835af12012-09-11 22:27:06 -0300712 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300713}
714
715static int af9035_frontend_callback(void *adapter_priv, int component,
716 int cmd, int arg)
717{
718 struct i2c_adapter *adap = adapter_priv;
719 struct dvb_usb_device *d = i2c_get_adapdata(adap);
720
Antti Palosaari1835af12012-09-11 22:27:06 -0300721 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
722 __func__, component, cmd, arg);
723
Michael Büschffc501f2012-04-02 12:18:36 -0300724 switch (component) {
725 case DVB_FRONTEND_COMPONENT_TUNER:
726 return af9035_tuner_callback(d, cmd, arg);
727 default:
728 break;
729 }
730
Antti Palosaari1835af12012-09-11 22:27:06 -0300731 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300732}
733
Antti Palosaari7f882c22012-03-30 09:10:08 -0300734static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
735{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300736 struct state *state = adap_to_priv(adap);
737 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300738 int ret;
739
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300740 if (!state->af9033_config[adap->id].tuner) {
741 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300742 ret = -ENODEV;
743 goto err;
744 }
745
Antti Palosaari7f882c22012-03-30 09:10:08 -0300746 if (adap->id == 0) {
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300747 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
748 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
749
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300750 ret = af9035_wr_reg(d, 0x00417f,
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300751 state->af9033_config[1].i2c_addr);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300752 if (ret < 0)
753 goto err;
754
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300755 ret = af9035_wr_reg(d, 0x00d81a,
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300756 state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300757 if (ret < 0)
758 goto err;
759 }
760
761 /* attach demodulator */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300762 adap->fe[0] = dvb_attach(af9033_attach,
763 &state->af9033_config[adap->id], &d->i2c_adap);
764 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300765 ret = -ENODEV;
766 goto err;
767 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300768
769 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300770 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
771 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300772
773 return 0;
774
775err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300776 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300777
778 return ret;
779}
780
781static struct tua9001_config af9035_tua9001_config = {
782 .i2c_addr = 0x60,
783};
784
Michael Büschffc501f2012-04-02 12:18:36 -0300785static const struct fc0011_config af9035_fc0011_config = {
786 .i2c_address = 0x60,
787};
788
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300789static struct mxl5007t_config af9035_mxl5007t_config = {
790 .xtal_freq_hz = MxL_XTAL_24_MHZ,
791 .if_freq_hz = MxL_IF_4_57_MHZ,
792 .invert_if = 0,
793 .loop_thru_enable = 0,
794 .clk_out_enable = 0,
795 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
796};
797
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300798static struct tda18218_config af9035_tda18218_config = {
799 .i2c_address = 0x60,
800 .i2c_wr_max = 21,
801};
802
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300803static const struct fc2580_config af9035_fc2580_config = {
804 .i2c_addr = 0x56,
805 .clock = 16384000,
806};
807
Antti Palosaari7f882c22012-03-30 09:10:08 -0300808static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
809{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300810 struct state *state = adap_to_priv(adap);
811 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300812 int ret;
813 struct dvb_frontend *fe;
814
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300815 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300816 case AF9033_TUNER_TUA9001:
817 /* AF9035 gpiot3 = TUA9001 RESETN
818 AF9035 gpiot2 = TUA9001 RXEN */
819
820 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300821 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300822 if (ret < 0)
823 goto err;
824
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300825 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300826 if (ret < 0)
827 goto err;
828
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300829 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300830 if (ret < 0)
831 goto err;
832
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300833 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300834 if (ret < 0)
835 goto err;
836
Antti Palosaari7f882c22012-03-30 09:10:08 -0300837 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300838 fe = dvb_attach(tua9001_attach, adap->fe[0],
839 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300840 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300841 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300842 fe = dvb_attach(fc0011_attach, adap->fe[0],
843 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -0300844 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300845 case AF9033_TUNER_MXL5007T:
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300846 ret = af9035_wr_reg(d, 0x00d8e0, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300847 if (ret < 0)
848 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300849 ret = af9035_wr_reg(d, 0x00d8e1, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300850 if (ret < 0)
851 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300852 ret = af9035_wr_reg(d, 0x00d8df, 0);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300853 if (ret < 0)
854 goto err;
855
856 msleep(30);
857
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300858 ret = af9035_wr_reg(d, 0x00d8df, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300859 if (ret < 0)
860 goto err;
861
862 msleep(300);
863
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300864 ret = af9035_wr_reg(d, 0x00d8c0, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300865 if (ret < 0)
866 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300867 ret = af9035_wr_reg(d, 0x00d8c1, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300868 if (ret < 0)
869 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300870 ret = af9035_wr_reg(d, 0x00d8bf, 0);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300871 if (ret < 0)
872 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300873 ret = af9035_wr_reg(d, 0x00d8b4, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300874 if (ret < 0)
875 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300876 ret = af9035_wr_reg(d, 0x00d8b5, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300877 if (ret < 0)
878 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300879 ret = af9035_wr_reg(d, 0x00d8b3, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300880 if (ret < 0)
881 goto err;
882
883 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300884 fe = dvb_attach(mxl5007t_attach, adap->fe[0],
885 &d->i2c_adap, 0x60, &af9035_mxl5007t_config);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300886 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300887 case AF9033_TUNER_TDA18218:
888 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300889 fe = dvb_attach(tda18218_attach, adap->fe[0],
890 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300891 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300892 case AF9033_TUNER_FC2580:
893 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
894 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
895 if (ret < 0)
896 goto err;
897
898 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
899 if (ret < 0)
900 goto err;
901
902 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
903 if (ret < 0)
904 goto err;
905
906 usleep_range(10000, 50000);
907 /* attach tuner */
908 fe = dvb_attach(fc2580_attach, adap->fe[0],
909 &d->i2c_adap, &af9035_fc2580_config);
910 break;
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300911 case AF9033_TUNER_FC0012:
912 /*
913 * AF9035 gpiot2 = FC0012 enable
914 * XXX: there seems to be something on gpioh8 too, but on my
915 * my test I didn't find any difference.
916 */
917
918 /* configure gpiot2 as output and high */
919 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
920 if (ret < 0)
921 goto err;
922
923 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
924 if (ret < 0)
925 goto err;
926
927 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
928 if (ret < 0)
929 goto err;
930
931 usleep_range(10000, 50000);
932
933 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap, 0x63,
934 1, FC_XTAL_36_MHZ);
935 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300936 default:
937 fe = NULL;
938 }
939
940 if (fe == NULL) {
941 ret = -ENODEV;
942 goto err;
943 }
944
945 return 0;
946
947err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300948 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300949
950 return ret;
951}
952
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300953static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300954{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300955 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300956 int ret, i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300957 u16 frame_size = 87 * 188 / 4;
958 u8 packet_size = 512 / 4;
959 struct reg_val_mask tab[] = {
960 { 0x80f99d, 0x01, 0x01 },
961 { 0x80f9a4, 0x01, 0x01 },
962 { 0x00dd11, 0x00, 0x20 },
963 { 0x00dd11, 0x00, 0x40 },
964 { 0x00dd13, 0x00, 0x20 },
965 { 0x00dd13, 0x00, 0x40 },
966 { 0x00dd11, 0x20, 0x20 },
967 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
968 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
969 { 0x00dd0c, packet_size, 0xff},
970 { 0x00dd11, state->dual_mode << 6, 0x40 },
971 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
972 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
973 { 0x00dd0d, packet_size, 0xff },
974 { 0x80f9a3, 0x00, 0x01 },
975 { 0x80f9cd, 0x00, 0x01 },
976 { 0x80f99d, 0x00, 0x01 },
977 { 0x80f9a4, 0x00, 0x01 },
978 };
Antti Palosaari7f882c22012-03-30 09:10:08 -0300979
Antti Palosaari119f7a82012-09-12 20:23:53 -0300980 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
981 "packet_size=%02x\n", __func__,
982 d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300983
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300984 /* init endpoints */
985 for (i = 0; i < ARRAY_SIZE(tab); i++) {
986 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
987 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300988 if (ret < 0)
989 goto err;
990 }
991
992 return 0;
993
994err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300995 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300996
997 return ret;
998}
999
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001000static int af9035_rc_query(struct dvb_usb_device *d)
1001{
1002 unsigned int key;
1003 unsigned char b[4];
1004 int ret;
1005 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
1006
1007 ret = af9035_ctrl_msg(d, &req);
1008 if (ret < 0)
1009 goto err;
1010
1011 if ((b[2] + b[3]) == 0xff) {
1012 if ((b[0] + b[1]) == 0xff) {
1013 /* NEC */
1014 key = b[0] << 8 | b[2];
1015 } else {
1016 /* ext. NEC */
1017 key = b[0] << 16 | b[1] << 8 | b[2];
1018 }
1019 } else {
1020 key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
1021 }
1022
1023 rc_keydown(d->rc_dev, key, 0);
1024
1025err:
1026 /* ignore errors */
1027 return 0;
1028}
1029
1030static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1031{
1032 int ret;
1033 u8 tmp;
1034
1035 ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp);
1036 if (ret < 0)
1037 goto err;
1038
Antti Palosaari119f7a82012-09-12 20:23:53 -03001039 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001040
1041 /* don't activate rc if in HID mode or if not available */
1042 if (tmp == 5) {
1043 ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp);
1044 if (ret < 0)
1045 goto err;
1046
Antti Palosaari119f7a82012-09-12 20:23:53 -03001047 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001048
1049 switch (tmp) {
1050 case 0: /* NEC */
1051 default:
David Härdemanc003ab12012-10-11 19:11:54 -03001052 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001053 break;
1054 case 1: /* RC6 */
David Härdemanc003ab12012-10-11 19:11:54 -03001055 rc->allowed_protos = RC_BIT_RC6_MCE;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001056 break;
1057 }
1058
1059 rc->query = af9035_rc_query;
1060 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001061
1062 /* load empty to enable rc */
1063 if (!rc->map_name)
1064 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001065 }
1066
1067 return 0;
1068
1069err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001070 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001071
1072 return ret;
1073}
1074
1075/* interface 0 is used by DVB-T receiver and
1076 interface 1 is for remote controller (HID) */
1077static const struct dvb_usb_device_properties af9035_props = {
1078 .driver_name = KBUILD_MODNAME,
1079 .owner = THIS_MODULE,
1080 .adapter_nr = adapter_nr,
1081 .size_of_priv = sizeof(struct state),
1082
1083 .generic_bulk_ctrl_endpoint = 0x02,
1084 .generic_bulk_ctrl_endpoint_response = 0x81,
1085
1086 .identify_state = af9035_identify_state,
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001087 .firmware = AF9035_FIRMWARE_AF9035,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001088 .download_firmware = af9035_download_firmware,
1089
1090 .i2c_algo = &af9035_i2c_algo,
1091 .read_config = af9035_read_config,
1092 .frontend_attach = af9035_frontend_attach,
1093 .tuner_attach = af9035_tuner_attach,
1094 .init = af9035_init,
1095 .get_rc_config = af9035_get_rc_config,
1096
1097 .num_adapters = 1,
1098 .adapter = {
1099 {
1100 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1101 }, {
1102 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1103 },
1104 },
1105};
1106
1107static const struct dvb_usb_device_properties it9135_props = {
1108 .driver_name = KBUILD_MODNAME,
1109 .owner = THIS_MODULE,
1110 .adapter_nr = adapter_nr,
1111 .size_of_priv = sizeof(struct state),
1112
1113 .generic_bulk_ctrl_endpoint = 0x02,
1114 .generic_bulk_ctrl_endpoint_response = 0x81,
1115
1116 .identify_state = af9035_identify_state,
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001117 .firmware = AF9035_FIRMWARE_IT9135,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001118 .download_firmware = af9035_download_firmware_it9135,
1119
1120 .i2c_algo = &af9035_i2c_algo,
1121 .read_config = af9035_read_config_it9135,
1122 .frontend_attach = af9035_frontend_attach,
1123 .tuner_attach = af9035_tuner_attach,
1124 .init = af9035_init,
1125 .get_rc_config = af9035_get_rc_config,
1126
1127 .num_adapters = 1,
1128 .adapter = {
1129 {
1130 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1131 }, {
1132 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1133 },
1134 },
1135};
1136
1137static const struct usb_device_id af9035_id_table[] = {
1138 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1139 &af9035_props, "Afatech AF9035 reference design", NULL) },
1140 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1141 &af9035_props, "Afatech AF9035 reference design", NULL) },
1142 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1143 &af9035_props, "Afatech AF9035 reference design", NULL) },
1144 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1145 &af9035_props, "Afatech AF9035 reference design", NULL) },
1146 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1147 &af9035_props, "Afatech AF9035 reference design", NULL) },
1148 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1149 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1150 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1151 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1152 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1153 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1154 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1155 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1156 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1157 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1158 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1159 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001160 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1161 &af9035_props, "Asus U3100Mini Plus", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001162 { }
1163};
1164MODULE_DEVICE_TABLE(usb, af9035_id_table);
1165
Antti Palosaari7f882c22012-03-30 09:10:08 -03001166static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001167 .name = KBUILD_MODNAME,
1168 .id_table = af9035_id_table,
1169 .probe = dvb_usbv2_probe,
1170 .disconnect = dvb_usbv2_disconnect,
1171 .suspend = dvb_usbv2_suspend,
1172 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001173 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001174 .no_dynamic_id = 1,
1175 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001176};
1177
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001178module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001179
1180MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1181MODULE_DESCRIPTION("Afatech AF9035 driver");
1182MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001183MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
1184MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135);