blob: aabd3fc03ea7e1594c00aab276364b857e9459ff [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 Palosaari2a79eef2012-05-07 14:50:40 -0300517 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300518 break;
519 default:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300520 dev_warn(&d->udev->dev, "%s: tuner id=%02x not " \
521 "supported, please report!",
522 KBUILD_MODNAME, tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300523 };
524
525 /* tuner IF frequency */
526 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
527 if (ret < 0)
528 goto err;
529
530 tmp16 = tmp;
531
532 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
533 if (ret < 0)
534 goto err;
535
536 tmp16 |= tmp << 8;
537
Antti Palosaari119f7a82012-09-12 20:23:53 -0300538 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300539
540 eeprom_shift = 0x10; /* shift for the 2nd tuner params */
541 }
542
543 /* get demod clock */
544 ret = af9035_rd_reg(d, 0x00d800, &tmp);
545 if (ret < 0)
546 goto err;
547
548 tmp = (tmp >> 0) & 0x0f;
549
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300550 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300551 state->af9033_config[i].clock = clock_lut[tmp];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300552
553 return 0;
554
555err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300556 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300557
558 return ret;
559}
560
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300561static int af9035_read_config_it9135(struct dvb_usb_device *d)
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300562{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300563 struct state *state = d_to_priv(d);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300564 int ret, i;
565 u8 tmp;
566
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300567 state->dual_mode = false;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300568
569 /* get demod clock */
570 ret = af9035_rd_reg(d, 0x00d800, &tmp);
571 if (ret < 0)
572 goto err;
573
574 tmp = (tmp >> 0) & 0x0f;
575
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300576 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300577 state->af9033_config[i].clock = clock_lut_it9135[tmp];
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300578
579 return 0;
580
581err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300582 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300583
584 return ret;
585}
586
Antti Palosaari51639be2012-09-16 22:26:56 -0300587static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
588 int cmd, int arg)
589{
590 int ret;
591 u8 val;
592
593 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
594
595 /*
596 * CEN always enabled by hardware wiring
597 * RESETN GPIOT3
598 * RXEN GPIOT2
599 */
600
601 switch (cmd) {
602 case TUA9001_CMD_RESETN:
603 if (arg)
604 val = 0x00;
605 else
606 val = 0x01;
607
608 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
609 if (ret < 0)
610 goto err;
611 break;
612 case TUA9001_CMD_RXEN:
613 if (arg)
614 val = 0x01;
615 else
616 val = 0x00;
617
618 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
619 if (ret < 0)
620 goto err;
621 break;
622 }
623
624 return 0;
625
626err:
627 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
628
629 return ret;
630}
631
632
Michael Büschffc501f2012-04-02 12:18:36 -0300633static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300634 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300635{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300636 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300637
638 switch (cmd) {
639 case FC0011_FE_CALLBACK_POWER:
640 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300641 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
642 if (ret < 0)
643 goto err;
644
645 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
646 if (ret < 0)
647 goto err;
648
649 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
650 if (ret < 0)
651 goto err;
652
Michael Büschffc501f2012-04-02 12:18:36 -0300653 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300654 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
655 if (ret < 0)
656 goto err;
657
658 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
659 if (ret < 0)
660 goto err;
661
Michael Büschde2bec52012-04-03 05:11:30 -0300662 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300663 break;
664 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300665 ret = af9035_wr_reg(d, 0xd8e9, 1);
666 if (ret < 0)
667 goto err;
668
669 ret = af9035_wr_reg(d, 0xd8e8, 1);
670 if (ret < 0)
671 goto err;
672
673 ret = af9035_wr_reg(d, 0xd8e7, 1);
674 if (ret < 0)
675 goto err;
676
Michael Büschde2bec52012-04-03 05:11:30 -0300677 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300678
679 ret = af9035_wr_reg(d, 0xd8e7, 0);
680 if (ret < 0)
681 goto err;
682
Michael Büschde2bec52012-04-03 05:11:30 -0300683 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300684 break;
685 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300686 ret = -EINVAL;
687 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300688 }
689
690 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300691
692err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300693 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300694
695 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300696}
697
698static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
699{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300700 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300701
702 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300703 case AF9033_TUNER_FC0011:
704 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300705 case AF9033_TUNER_TUA9001:
706 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300707 default:
708 break;
709 }
710
Antti Palosaari1835af12012-09-11 22:27:06 -0300711 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300712}
713
714static int af9035_frontend_callback(void *adapter_priv, int component,
715 int cmd, int arg)
716{
717 struct i2c_adapter *adap = adapter_priv;
718 struct dvb_usb_device *d = i2c_get_adapdata(adap);
719
Antti Palosaari1835af12012-09-11 22:27:06 -0300720 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
721 __func__, component, cmd, arg);
722
Michael Büschffc501f2012-04-02 12:18:36 -0300723 switch (component) {
724 case DVB_FRONTEND_COMPONENT_TUNER:
725 return af9035_tuner_callback(d, cmd, arg);
726 default:
727 break;
728 }
729
Antti Palosaari1835af12012-09-11 22:27:06 -0300730 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300731}
732
Antti Palosaari7f882c22012-03-30 09:10:08 -0300733static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
734{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300735 struct state *state = adap_to_priv(adap);
736 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300737 int ret;
738
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300739 if (!state->af9033_config[adap->id].tuner) {
740 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300741 ret = -ENODEV;
742 goto err;
743 }
744
Antti Palosaari7f882c22012-03-30 09:10:08 -0300745 if (adap->id == 0) {
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300746 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
747 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
748
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300749 ret = af9035_wr_reg(d, 0x00417f,
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300750 state->af9033_config[1].i2c_addr);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300751 if (ret < 0)
752 goto err;
753
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300754 ret = af9035_wr_reg(d, 0x00d81a,
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300755 state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300756 if (ret < 0)
757 goto err;
758 }
759
760 /* attach demodulator */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300761 adap->fe[0] = dvb_attach(af9033_attach,
762 &state->af9033_config[adap->id], &d->i2c_adap);
763 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300764 ret = -ENODEV;
765 goto err;
766 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300767
768 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300769 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
770 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300771
772 return 0;
773
774err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300775 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300776
777 return ret;
778}
779
780static struct tua9001_config af9035_tua9001_config = {
781 .i2c_addr = 0x60,
782};
783
Michael Büschffc501f2012-04-02 12:18:36 -0300784static const struct fc0011_config af9035_fc0011_config = {
785 .i2c_address = 0x60,
786};
787
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300788static struct mxl5007t_config af9035_mxl5007t_config = {
789 .xtal_freq_hz = MxL_XTAL_24_MHZ,
790 .if_freq_hz = MxL_IF_4_57_MHZ,
791 .invert_if = 0,
792 .loop_thru_enable = 0,
793 .clk_out_enable = 0,
794 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
795};
796
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300797static struct tda18218_config af9035_tda18218_config = {
798 .i2c_address = 0x60,
799 .i2c_wr_max = 21,
800};
801
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300802static const struct fc2580_config af9035_fc2580_config = {
803 .i2c_addr = 0x56,
804 .clock = 16384000,
805};
806
Antti Palosaari7f882c22012-03-30 09:10:08 -0300807static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
808{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300809 struct state *state = adap_to_priv(adap);
810 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300811 int ret;
812 struct dvb_frontend *fe;
813
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300814 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300815 case AF9033_TUNER_TUA9001:
816 /* AF9035 gpiot3 = TUA9001 RESETN
817 AF9035 gpiot2 = TUA9001 RXEN */
818
819 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300820 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300821 if (ret < 0)
822 goto err;
823
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300824 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300825 if (ret < 0)
826 goto err;
827
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300828 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300829 if (ret < 0)
830 goto err;
831
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300832 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300833 if (ret < 0)
834 goto err;
835
Antti Palosaari7f882c22012-03-30 09:10:08 -0300836 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300837 fe = dvb_attach(tua9001_attach, adap->fe[0],
838 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300839 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300840 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300841 fe = dvb_attach(fc0011_attach, adap->fe[0],
842 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -0300843 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300844 case AF9033_TUNER_MXL5007T:
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300845 ret = af9035_wr_reg(d, 0x00d8e0, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300846 if (ret < 0)
847 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300848 ret = af9035_wr_reg(d, 0x00d8e1, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300849 if (ret < 0)
850 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300851 ret = af9035_wr_reg(d, 0x00d8df, 0);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300852 if (ret < 0)
853 goto err;
854
855 msleep(30);
856
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300857 ret = af9035_wr_reg(d, 0x00d8df, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300858 if (ret < 0)
859 goto err;
860
861 msleep(300);
862
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300863 ret = af9035_wr_reg(d, 0x00d8c0, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300864 if (ret < 0)
865 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300866 ret = af9035_wr_reg(d, 0x00d8c1, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300867 if (ret < 0)
868 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300869 ret = af9035_wr_reg(d, 0x00d8bf, 0);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300870 if (ret < 0)
871 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300872 ret = af9035_wr_reg(d, 0x00d8b4, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300873 if (ret < 0)
874 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300875 ret = af9035_wr_reg(d, 0x00d8b5, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300876 if (ret < 0)
877 goto err;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300878 ret = af9035_wr_reg(d, 0x00d8b3, 1);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300879 if (ret < 0)
880 goto err;
881
882 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300883 fe = dvb_attach(mxl5007t_attach, adap->fe[0],
884 &d->i2c_adap, 0x60, &af9035_mxl5007t_config);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300885 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300886 case AF9033_TUNER_TDA18218:
887 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300888 fe = dvb_attach(tda18218_attach, adap->fe[0],
889 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300890 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300891 case AF9033_TUNER_FC2580:
892 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
893 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
894 if (ret < 0)
895 goto err;
896
897 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
898 if (ret < 0)
899 goto err;
900
901 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
902 if (ret < 0)
903 goto err;
904
905 usleep_range(10000, 50000);
906 /* attach tuner */
907 fe = dvb_attach(fc2580_attach, adap->fe[0],
908 &d->i2c_adap, &af9035_fc2580_config);
909 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300910 default:
911 fe = NULL;
912 }
913
914 if (fe == NULL) {
915 ret = -ENODEV;
916 goto err;
917 }
918
919 return 0;
920
921err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300922 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300923
924 return ret;
925}
926
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300927static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300928{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300929 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300930 int ret, i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300931 u16 frame_size = 87 * 188 / 4;
932 u8 packet_size = 512 / 4;
933 struct reg_val_mask tab[] = {
934 { 0x80f99d, 0x01, 0x01 },
935 { 0x80f9a4, 0x01, 0x01 },
936 { 0x00dd11, 0x00, 0x20 },
937 { 0x00dd11, 0x00, 0x40 },
938 { 0x00dd13, 0x00, 0x20 },
939 { 0x00dd13, 0x00, 0x40 },
940 { 0x00dd11, 0x20, 0x20 },
941 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
942 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
943 { 0x00dd0c, packet_size, 0xff},
944 { 0x00dd11, state->dual_mode << 6, 0x40 },
945 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
946 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
947 { 0x00dd0d, packet_size, 0xff },
948 { 0x80f9a3, 0x00, 0x01 },
949 { 0x80f9cd, 0x00, 0x01 },
950 { 0x80f99d, 0x00, 0x01 },
951 { 0x80f9a4, 0x00, 0x01 },
952 };
Antti Palosaari7f882c22012-03-30 09:10:08 -0300953
Antti Palosaari119f7a82012-09-12 20:23:53 -0300954 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
955 "packet_size=%02x\n", __func__,
956 d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300957
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300958 /* init endpoints */
959 for (i = 0; i < ARRAY_SIZE(tab); i++) {
960 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
961 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300962 if (ret < 0)
963 goto err;
964 }
965
966 return 0;
967
968err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300969 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300970
971 return ret;
972}
973
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300974static int af9035_rc_query(struct dvb_usb_device *d)
975{
976 unsigned int key;
977 unsigned char b[4];
978 int ret;
979 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
980
981 ret = af9035_ctrl_msg(d, &req);
982 if (ret < 0)
983 goto err;
984
985 if ((b[2] + b[3]) == 0xff) {
986 if ((b[0] + b[1]) == 0xff) {
987 /* NEC */
988 key = b[0] << 8 | b[2];
989 } else {
990 /* ext. NEC */
991 key = b[0] << 16 | b[1] << 8 | b[2];
992 }
993 } else {
994 key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
995 }
996
997 rc_keydown(d->rc_dev, key, 0);
998
999err:
1000 /* ignore errors */
1001 return 0;
1002}
1003
1004static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1005{
1006 int ret;
1007 u8 tmp;
1008
1009 ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp);
1010 if (ret < 0)
1011 goto err;
1012
Antti Palosaari119f7a82012-09-12 20:23:53 -03001013 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001014
1015 /* don't activate rc if in HID mode or if not available */
1016 if (tmp == 5) {
1017 ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp);
1018 if (ret < 0)
1019 goto err;
1020
Antti Palosaari119f7a82012-09-12 20:23:53 -03001021 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001022
1023 switch (tmp) {
1024 case 0: /* NEC */
1025 default:
1026 rc->allowed_protos = RC_TYPE_NEC;
1027 break;
1028 case 1: /* RC6 */
1029 rc->allowed_protos = RC_TYPE_RC6;
1030 break;
1031 }
1032
1033 rc->query = af9035_rc_query;
1034 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001035
1036 /* load empty to enable rc */
1037 if (!rc->map_name)
1038 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001039 }
1040
1041 return 0;
1042
1043err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001044 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001045
1046 return ret;
1047}
1048
1049/* interface 0 is used by DVB-T receiver and
1050 interface 1 is for remote controller (HID) */
1051static const struct dvb_usb_device_properties af9035_props = {
1052 .driver_name = KBUILD_MODNAME,
1053 .owner = THIS_MODULE,
1054 .adapter_nr = adapter_nr,
1055 .size_of_priv = sizeof(struct state),
1056
1057 .generic_bulk_ctrl_endpoint = 0x02,
1058 .generic_bulk_ctrl_endpoint_response = 0x81,
1059
1060 .identify_state = af9035_identify_state,
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001061 .firmware = AF9035_FIRMWARE_AF9035,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001062 .download_firmware = af9035_download_firmware,
1063
1064 .i2c_algo = &af9035_i2c_algo,
1065 .read_config = af9035_read_config,
1066 .frontend_attach = af9035_frontend_attach,
1067 .tuner_attach = af9035_tuner_attach,
1068 .init = af9035_init,
1069 .get_rc_config = af9035_get_rc_config,
1070
1071 .num_adapters = 1,
1072 .adapter = {
1073 {
1074 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1075 }, {
1076 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1077 },
1078 },
1079};
1080
1081static const struct dvb_usb_device_properties it9135_props = {
1082 .driver_name = KBUILD_MODNAME,
1083 .owner = THIS_MODULE,
1084 .adapter_nr = adapter_nr,
1085 .size_of_priv = sizeof(struct state),
1086
1087 .generic_bulk_ctrl_endpoint = 0x02,
1088 .generic_bulk_ctrl_endpoint_response = 0x81,
1089
1090 .identify_state = af9035_identify_state,
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001091 .firmware = AF9035_FIRMWARE_IT9135,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001092 .download_firmware = af9035_download_firmware_it9135,
1093
1094 .i2c_algo = &af9035_i2c_algo,
1095 .read_config = af9035_read_config_it9135,
1096 .frontend_attach = af9035_frontend_attach,
1097 .tuner_attach = af9035_tuner_attach,
1098 .init = af9035_init,
1099 .get_rc_config = af9035_get_rc_config,
1100
1101 .num_adapters = 1,
1102 .adapter = {
1103 {
1104 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1105 }, {
1106 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1107 },
1108 },
1109};
1110
1111static const struct usb_device_id af9035_id_table[] = {
1112 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1113 &af9035_props, "Afatech AF9035 reference design", NULL) },
1114 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1115 &af9035_props, "Afatech AF9035 reference design", NULL) },
1116 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1117 &af9035_props, "Afatech AF9035 reference design", NULL) },
1118 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1119 &af9035_props, "Afatech AF9035 reference design", NULL) },
1120 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1121 &af9035_props, "Afatech AF9035 reference design", NULL) },
1122 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1123 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1124 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1125 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1126 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1127 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1128 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1129 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1130 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1131 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1132 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1133 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001134 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1135 &af9035_props, "Asus U3100Mini Plus", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001136 { }
1137};
1138MODULE_DEVICE_TABLE(usb, af9035_id_table);
1139
Antti Palosaari7f882c22012-03-30 09:10:08 -03001140static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001141 .name = KBUILD_MODNAME,
1142 .id_table = af9035_id_table,
1143 .probe = dvb_usbv2_probe,
1144 .disconnect = dvb_usbv2_disconnect,
1145 .suspend = dvb_usbv2_suspend,
1146 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001147 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001148 .no_dynamic_id = 1,
1149 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001150};
1151
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001152module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001153
1154MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1155MODULE_DESCRIPTION("Afatech AF9035 driver");
1156MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001157MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
1158MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135);