blob: f4f3a21092455cd6ca3805b17b6753ff84ceaede [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 Palosaari7f882c22012-03-30 09:10:08 -030044#define REQ_HDR_LEN 4 /* send header size */
45#define ACK_HDR_LEN 3 /* rece header size */
46#define CHECKSUM_LEN 2
47#define USB_TIMEOUT 2000
Antti Palosaari5da2aec2012-06-17 23:15:03 -030048 struct state *state = d_to_priv(d);
49 int ret, wlen, rlen;
Antti Palosaari6fb39c52012-04-06 16:32:30 -030050 u16 checksum, tmp_checksum;
Antti Palosaari7f882c22012-03-30 09:10:08 -030051
Antti Palosaari3484d372013-02-26 13:56:34 -030052 mutex_lock(&d->usb_mutex);
53
Antti Palosaari7f882c22012-03-30 09:10:08 -030054 /* 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 Palosaari3484d372013-02-26 13:56:34 -030059 ret = -EINVAL;
60 goto err;
Antti Palosaari7f882c22012-03-30 09:10:08 -030061 }
62
Antti Palosaari3484d372013-02-26 13:56:34 -030063 state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
64 state->buf[1] = req->mbox;
65 state->buf[2] = req->cmd;
66 state->buf[3] = state->seq++;
67 memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -030068
69 wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
70 rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
Antti Palosaari7f882c22012-03-30 09:10:08 -030071
72 /* calc and add checksum */
Antti Palosaari3484d372013-02-26 13:56:34 -030073 checksum = af9035_checksum(state->buf, state->buf[0] - 1);
74 state->buf[state->buf[0] - 1] = (checksum >> 8);
75 state->buf[state->buf[0] - 0] = (checksum & 0xff);
Antti Palosaari7f882c22012-03-30 09:10:08 -030076
Antti Palosaari5da2aec2012-06-17 23:15:03 -030077 /* no ack for these packets */
78 if (req->cmd == CMD_FW_DL)
79 rlen = 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -030080
Antti Palosaari3484d372013-02-26 13:56:34 -030081 ret = dvb_usbv2_generic_rw_locked(d,
82 state->buf, wlen, state->buf, rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -030083 if (ret)
84 goto err;
Antti Palosaari7f882c22012-03-30 09:10:08 -030085
86 /* no ack for those packets */
87 if (req->cmd == CMD_FW_DL)
Antti Palosaari5da2aec2012-06-17 23:15:03 -030088 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -030089
Michael Büschb1a95992012-04-01 16:33:48 -030090 /* verify checksum */
Antti Palosaari3484d372013-02-26 13:56:34 -030091 checksum = af9035_checksum(state->buf, rlen - 2);
92 tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
Antti Palosaari6fb39c52012-04-06 16:32:30 -030093 if (tmp_checksum != checksum) {
Antti Palosaari119f7a82012-09-12 20:23:53 -030094 dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \
95 "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd,
96 tmp_checksum, checksum);
Michael Büschb1a95992012-04-01 16:33:48 -030097 ret = -EIO;
Antti Palosaari5da2aec2012-06-17 23:15:03 -030098 goto err;
Michael Büschb1a95992012-04-01 16:33:48 -030099 }
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300100
Antti Palosaari7f882c22012-03-30 09:10:08 -0300101 /* check status */
Antti Palosaari3484d372013-02-26 13:56:34 -0300102 if (state->buf[2]) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300103 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
Antti Palosaari3484d372013-02-26 13:56:34 -0300104 __func__, req->cmd, state->buf[2]);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300105 ret = -EIO;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300106 goto err;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300107 }
108
109 /* read request, copy returned data to return buf */
110 if (req->rlen)
Antti Palosaari3484d372013-02-26 13:56:34 -0300111 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300112exit:
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300113err:
Antti Palosaari3484d372013-02-26 13:56:34 -0300114 mutex_unlock(&d->usb_mutex);
115 if (ret)
116 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300117 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;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300212 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
213 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300214 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300215 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
216 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300217
218 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300219 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300220
Antti Palosaari7f882c22012-03-30 09:10:08 -0300221 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
222 msg[1].len);
223 } else {
224 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300225 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300226 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
227 buf, msg[1].len, msg[1].buf };
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300228 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300229 buf[0] = msg[1].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300230 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300231 buf[2] = 0x00; /* reg addr len */
232 buf[3] = 0x00; /* reg addr MSB */
233 buf[4] = 0x00; /* reg addr LSB */
234 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300235 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300236 }
237 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
238 if (msg[0].len > 40) {
239 /* TODO: correct limits > 40 */
240 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300241 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
242 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300243 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300244 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
245 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300246
247 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300248 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300249
Antti Palosaari7f882c22012-03-30 09:10:08 -0300250 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
251 msg[0].len - 3);
252 } else {
253 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300254 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300255 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
256 0, NULL };
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300257 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300258 buf[0] = msg[0].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300259 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300260 buf[2] = 0x00; /* reg addr len */
261 buf[3] = 0x00; /* reg addr MSB */
262 buf[4] = 0x00; /* reg addr LSB */
263 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300264 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300265 }
266 } else {
267 /*
268 * We support only two kind of I2C transactions:
269 * 1) 1 x read + 1 x write
270 * 2) 1 x write
271 */
272 ret = -EOPNOTSUPP;
273 }
274
275 mutex_unlock(&d->i2c_mutex);
276
277 if (ret < 0)
278 return ret;
279 else
280 return num;
281}
282
283static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
284{
285 return I2C_FUNC_I2C;
286}
287
288static struct i2c_algorithm af9035_i2c_algo = {
289 .master_xfer = af9035_i2c_master_xfer,
290 .functionality = af9035_i2c_functionality,
291};
292
Antti Palosaaria0921af2012-06-18 23:42:53 -0300293static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300294{
Antti Palosaari74c18832013-01-07 15:16:54 -0300295 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300296 int ret;
297 u8 wbuf[1] = { 1 };
298 u8 rbuf[4];
299 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
300 sizeof(rbuf), rbuf };
301
Antti Palosaari74c18832013-01-07 15:16:54 -0300302 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
303 if (ret < 0)
304 goto err;
305
306 state->chip_version = rbuf[0];
307 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
308
309 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
310 if (ret < 0)
311 goto err;
312
313 dev_info(&d->udev->dev,
314 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
315 __func__, state->prechip_version, state->chip_version,
316 state->chip_type);
317
318 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300319 if (state->chip_version == 0x02)
Antti Palosaari74c18832013-01-07 15:16:54 -0300320 *name = AF9035_FIRMWARE_IT9135_V2;
321 else
322 *name = AF9035_FIRMWARE_IT9135_V1;
323 } else {
324 *name = AF9035_FIRMWARE_AF9035;
325 }
326
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300327 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300328 if (ret < 0)
329 goto err;
330
Antti Palosaari119f7a82012-09-12 20:23:53 -0300331 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300332 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300333 ret = WARM;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300334 else
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300335 ret = COLD;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300336
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300337 return ret;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300338
339err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300340 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300341
342 return ret;
343}
344
Antti Palosaari74c18832013-01-07 15:16:54 -0300345static int af9035_download_firmware_af9035(struct dvb_usb_device *d,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300346 const struct firmware *fw)
347{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300348 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300349 u8 wbuf[1];
350 u8 rbuf[4];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300351 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
352 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
353 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300354 u8 hdr_core, tmp;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300355 u16 hdr_addr, hdr_data_len, hdr_checksum;
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300356 #define MAX_DATA 58
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300357 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300358
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300359 /*
Antti Palosaaribf97b632012-12-08 22:51:19 -0300360 * In case of dual tuner configuration we need to do some extra
361 * initialization in order to download firmware to slave demod too,
362 * which is done by master demod.
363 * Master feeds also clock and controls power via GPIO.
364 */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300365 ret = af9035_rd_reg(d, EEPROM_BASE_AF9035 + EEPROM_DUAL_MODE, &tmp);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300366 if (ret < 0)
367 goto err;
368
369 if (tmp) {
370 /* configure gpioh1, reset & power slave demod */
371 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
372 if (ret < 0)
373 goto err;
374
375 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
376 if (ret < 0)
377 goto err;
378
379 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
380 if (ret < 0)
381 goto err;
382
383 usleep_range(10000, 50000);
384
385 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
386 if (ret < 0)
387 goto err;
388
389 /* tell the slave I2C address */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300390 ret = af9035_rd_reg(d,
391 EEPROM_BASE_AF9035 + EEPROM_2ND_DEMOD_ADDR,
392 &tmp);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300393 if (ret < 0)
394 goto err;
395
396 ret = af9035_wr_reg(d, 0x00417f, tmp);
397 if (ret < 0)
398 goto err;
399
400 /* enable clock out */
401 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
402 if (ret < 0)
403 goto err;
404 }
405
406 /*
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300407 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
408 *
409 * byte 0: MCS 51 core
410 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
411 * address spaces
412 * byte 1-2: Big endian destination address
413 * byte 3-4: Big endian number of data bytes following the header
414 * byte 5-6: Big endian header checksum, apparently ignored by the chip
415 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
416 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300417
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300418 for (i = fw->size; i > HDR_SIZE;) {
419 hdr_core = fw->data[fw->size - i + 0];
420 hdr_addr = fw->data[fw->size - i + 1] << 8;
421 hdr_addr |= fw->data[fw->size - i + 2] << 0;
422 hdr_data_len = fw->data[fw->size - i + 3] << 8;
423 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
424 hdr_checksum = fw->data[fw->size - i + 5] << 8;
425 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300426
Antti Palosaari119f7a82012-09-12 20:23:53 -0300427 dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
428 "checksum=%04x\n", __func__, hdr_core, hdr_addr,
429 hdr_data_len, hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300430
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300431 if (((hdr_core != 1) && (hdr_core != 2)) ||
432 (hdr_data_len > i)) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300433 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300434 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300435 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300436
437 /* download begin packet */
438 req.cmd = CMD_FW_DL_BEGIN;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300439 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300440 if (ret < 0)
441 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300442
443 /* download firmware packet(s) */
444 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
445 len = j;
446 if (len > MAX_DATA)
447 len = MAX_DATA;
448 req_fw_dl.wlen = len;
449 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
450 HDR_SIZE + hdr_data_len - j];
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300451 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300452 if (ret < 0)
453 goto err;
454 }
455
456 /* download end packet */
457 req.cmd = CMD_FW_DL_END;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300458 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300459 if (ret < 0)
460 goto err;
461
462 i -= hdr_data_len + HDR_SIZE;
463
Antti Palosaari119f7a82012-09-12 20:23:53 -0300464 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
465 __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300466 }
467
Antti Palosaariff4e3fe2012-12-09 16:25:08 -0300468 /* print warn if firmware is bad, continue and see what happens */
469 if (i)
470 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
471
Antti Palosaari7f882c22012-03-30 09:10:08 -0300472 /* firmware loaded, request boot */
473 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300474 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300475 if (ret < 0)
476 goto err;
477
478 /* ensure firmware starts */
479 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300480 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300481 if (ret < 0)
482 goto err;
483
Antti Palosaari7f882c22012-03-30 09:10:08 -0300484 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300485 dev_err(&d->udev->dev, "%s: firmware did not run\n",
486 KBUILD_MODNAME);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300487 ret = -ENODEV;
488 goto err;
489 }
490
Antti Palosaari119f7a82012-09-12 20:23:53 -0300491 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
492 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300493
Antti Palosaari7f882c22012-03-30 09:10:08 -0300494 return 0;
495
496err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300497 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300498
499 return ret;
500}
501
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300502static int af9035_download_firmware_it9135(struct dvb_usb_device *d,
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300503 const struct firmware *fw)
504{
505 int ret, i, i_prev;
506 u8 wbuf[1];
507 u8 rbuf[4];
508 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
509 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
510 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
511 #define HDR_SIZE 7
512
513 /*
514 * There seems to be following firmware header. Meaning of bytes 0-3
515 * is unknown.
516 *
517 * 0: 3
518 * 1: 0, 1
519 * 2: 0
520 * 3: 1, 2, 3
521 * 4: addr MSB
522 * 5: addr LSB
523 * 6: count of data bytes ?
524 */
525
526 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
527 if (i == fw->size ||
528 (fw->data[i + 0] == 0x03 &&
529 (fw->data[i + 1] == 0x00 ||
530 fw->data[i + 1] == 0x01) &&
531 fw->data[i + 2] == 0x00)) {
532 req_fw_dl.wlen = i - i_prev;
533 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
534 i_prev = i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300535 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300536 if (ret < 0)
537 goto err;
538
Antti Palosaari119f7a82012-09-12 20:23:53 -0300539 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
540 __func__, i);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300541 }
542 }
543
544 /* firmware loaded, request boot */
545 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300546 ret = af9035_ctrl_msg(d, &req);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300547 if (ret < 0)
548 goto err;
549
550 /* ensure firmware starts */
551 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300552 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300553 if (ret < 0)
554 goto err;
555
556 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300557 dev_err(&d->udev->dev, "%s: firmware did not run\n",
558 KBUILD_MODNAME);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300559 ret = -ENODEV;
560 goto err;
561 }
562
Antti Palosaari119f7a82012-09-12 20:23:53 -0300563 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
564 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300565
566 return 0;
567
568err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300569 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300570
571 return ret;
572}
573
Antti Palosaari74c18832013-01-07 15:16:54 -0300574static int af9035_download_firmware(struct dvb_usb_device *d,
575 const struct firmware *fw)
576{
577 struct state *state = d_to_priv(d);
578
579 if (state->chip_type == 0x9135)
580 return af9035_download_firmware_it9135(d, fw);
581 else
582 return af9035_download_firmware_af9035(d, fw);
583}
584
Antti Palosaari9ea36812013-01-11 22:16:25 -0300585static int af9035_read_config(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300586{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300587 struct state *state = d_to_priv(d);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300588 int ret, i;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300589 u8 tmp;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300590 u16 tmp16, addr;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300591
Antti Palosaaribf97b632012-12-08 22:51:19 -0300592 /* demod I2C "address" */
593 state->af9033_config[0].i2c_addr = 0x38;
Antti Palosaari0d94d6a2013-01-07 15:26:05 -0300594 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300595
Antti Palosaari9ea36812013-01-11 22:16:25 -0300596 /* eeprom memory mapped location */
597 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300598 if (state->chip_version == 0x02) {
599 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
600 tmp16 = 0x00461d;
601 } else {
602 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
603 tmp16 = 0x00461b;
604 }
605
Antti Palosaari9ea36812013-01-11 22:16:25 -0300606 /* check if eeprom exists */
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300607 ret = af9035_rd_reg(d, tmp16, &tmp);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300608 if (ret < 0)
609 goto err;
610
611 if (tmp) {
612 addr = EEPROM_BASE_IT9135;
613 } else {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300614 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300615 goto skip_eeprom;
616 }
617 } else {
618 addr = EEPROM_BASE_AF9035;
619 }
620
Antti Palosaari7f882c22012-03-30 09:10:08 -0300621 /* check if there is dual tuners */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300622 ret = af9035_rd_reg(d, addr + EEPROM_DUAL_MODE, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300623 if (ret < 0)
624 goto err;
625
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300626 state->dual_mode = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300627 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
628 state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300629
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300630 if (state->dual_mode) {
631 /* read 2nd demodulator I2C address */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300632 ret = af9035_rd_reg(d, addr + EEPROM_2ND_DEMOD_ADDR, &tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300633 if (ret < 0)
634 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300635
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300636 state->af9033_config[1].i2c_addr = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300637 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
638 __func__, tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300639 }
640
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300641 for (i = 0; i < state->dual_mode + 1; i++) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300642 /* tuner */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300643 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300644 if (ret < 0)
645 goto err;
646
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300647 if (tmp == 0x00)
648 dev_dbg(&d->udev->dev,
649 "%s: [%d]tuner not set, using default\n",
650 __func__, i);
651 else
652 state->af9033_config[i].tuner = tmp;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300653
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300654 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
655 __func__, i, state->af9033_config[i].tuner);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300656
657 switch (state->af9033_config[i].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300658 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300659 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300660 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300661 case AF9033_TUNER_TDA18218:
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300662 case AF9033_TUNER_FC2580:
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300663 case AF9033_TUNER_FC0012:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300664 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300665 break;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300666 case AF9033_TUNER_IT9135_38:
667 case AF9033_TUNER_IT9135_51:
668 case AF9033_TUNER_IT9135_52:
669 case AF9033_TUNER_IT9135_60:
670 case AF9033_TUNER_IT9135_61:
671 case AF9033_TUNER_IT9135_62:
672 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300673 default:
Antti Palosaari9ea36812013-01-11 22:16:25 -0300674 dev_warn(&d->udev->dev,
675 "%s: tuner id=%02x not supported, please report!",
Antti Palosaari119f7a82012-09-12 20:23:53 -0300676 KBUILD_MODNAME, tmp);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300677 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300678
Antti Palosaaribf97b632012-12-08 22:51:19 -0300679 /* disable dual mode if driver does not support it */
680 if (i == 1)
681 switch (tmp) {
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300682 case AF9033_TUNER_FC0012:
683 break;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300684 default:
685 state->dual_mode = false;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300686 dev_info(&d->udev->dev,
687 "%s: driver does not support 2nd tuner and will disable it",
688 KBUILD_MODNAME);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300689 }
690
Antti Palosaari7f882c22012-03-30 09:10:08 -0300691 /* tuner IF frequency */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300692 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300693 if (ret < 0)
694 goto err;
695
696 tmp16 = tmp;
697
Antti Palosaari9ea36812013-01-11 22:16:25 -0300698 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300699 if (ret < 0)
700 goto err;
701
702 tmp16 |= tmp << 8;
703
Antti Palosaari119f7a82012-09-12 20:23:53 -0300704 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300705
Antti Palosaari9ea36812013-01-11 22:16:25 -0300706 addr += 0x10; /* shift for the 2nd tuner params */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300707 }
708
Antti Palosaari9ea36812013-01-11 22:16:25 -0300709skip_eeprom:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300710 /* get demod clock */
711 ret = af9035_rd_reg(d, 0x00d800, &tmp);
712 if (ret < 0)
713 goto err;
714
715 tmp = (tmp >> 0) & 0x0f;
716
Antti Palosaari9ea36812013-01-11 22:16:25 -0300717 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
718 if (state->chip_type == 0x9135)
719 state->af9033_config[i].clock = clock_lut_it9135[tmp];
720 else
721 state->af9033_config[i].clock = clock_lut_af9035[tmp];
Antti Palosaari74c18832013-01-07 15:16:54 -0300722 }
723
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300724 return 0;
725
726err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300727 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300728
729 return ret;
730}
731
Antti Palosaari51639be2012-09-16 22:26:56 -0300732static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
733 int cmd, int arg)
734{
735 int ret;
736 u8 val;
737
738 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
739
740 /*
741 * CEN always enabled by hardware wiring
742 * RESETN GPIOT3
743 * RXEN GPIOT2
744 */
745
746 switch (cmd) {
747 case TUA9001_CMD_RESETN:
748 if (arg)
749 val = 0x00;
750 else
751 val = 0x01;
752
753 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
754 if (ret < 0)
755 goto err;
756 break;
757 case TUA9001_CMD_RXEN:
758 if (arg)
759 val = 0x01;
760 else
761 val = 0x00;
762
763 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
764 if (ret < 0)
765 goto err;
766 break;
767 }
768
769 return 0;
770
771err:
772 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
773
774 return ret;
775}
776
777
Michael Büschffc501f2012-04-02 12:18:36 -0300778static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300779 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300780{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300781 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300782
783 switch (cmd) {
784 case FC0011_FE_CALLBACK_POWER:
785 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300786 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
787 if (ret < 0)
788 goto err;
789
790 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
791 if (ret < 0)
792 goto err;
793
794 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
795 if (ret < 0)
796 goto err;
797
Michael Büschffc501f2012-04-02 12:18:36 -0300798 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300799 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
800 if (ret < 0)
801 goto err;
802
803 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
804 if (ret < 0)
805 goto err;
806
Michael Büschde2bec52012-04-03 05:11:30 -0300807 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300808 break;
809 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300810 ret = af9035_wr_reg(d, 0xd8e9, 1);
811 if (ret < 0)
812 goto err;
813
814 ret = af9035_wr_reg(d, 0xd8e8, 1);
815 if (ret < 0)
816 goto err;
817
818 ret = af9035_wr_reg(d, 0xd8e7, 1);
819 if (ret < 0)
820 goto err;
821
Michael Büschde2bec52012-04-03 05:11:30 -0300822 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300823
824 ret = af9035_wr_reg(d, 0xd8e7, 0);
825 if (ret < 0)
826 goto err;
827
Michael Büschde2bec52012-04-03 05:11:30 -0300828 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300829 break;
830 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300831 ret = -EINVAL;
832 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300833 }
834
835 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300836
837err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300838 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300839
840 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300841}
842
843static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
844{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300845 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300846
847 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300848 case AF9033_TUNER_FC0011:
849 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300850 case AF9033_TUNER_TUA9001:
851 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300852 default:
853 break;
854 }
855
Antti Palosaari1835af12012-09-11 22:27:06 -0300856 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300857}
858
859static int af9035_frontend_callback(void *adapter_priv, int component,
860 int cmd, int arg)
861{
862 struct i2c_adapter *adap = adapter_priv;
863 struct dvb_usb_device *d = i2c_get_adapdata(adap);
864
Antti Palosaari1835af12012-09-11 22:27:06 -0300865 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
866 __func__, component, cmd, arg);
867
Michael Büschffc501f2012-04-02 12:18:36 -0300868 switch (component) {
869 case DVB_FRONTEND_COMPONENT_TUNER:
870 return af9035_tuner_callback(d, cmd, arg);
871 default:
872 break;
873 }
874
Antti Palosaari1835af12012-09-11 22:27:06 -0300875 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300876}
877
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300878static int af9035_get_adapter_count(struct dvb_usb_device *d)
879{
880 struct state *state = d_to_priv(d);
Antti Palosaaribada3422013-01-11 20:45:11 -0300881
882 /* disable 2nd adapter as we don't have PID filters implemented */
883 if (d->udev->speed == USB_SPEED_FULL)
884 return 1;
885 else
886 return state->dual_mode + 1;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300887}
888
Antti Palosaari7f882c22012-03-30 09:10:08 -0300889static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
890{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300891 struct state *state = adap_to_priv(adap);
892 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300893 int ret;
894
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300895 if (!state->af9033_config[adap->id].tuner) {
896 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300897 ret = -ENODEV;
898 goto err;
899 }
900
Antti Palosaari7f882c22012-03-30 09:10:08 -0300901 if (adap->id == 0) {
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300902 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
903 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
904
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300905 ret = af9035_wr_reg(d, 0x00417f,
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300906 state->af9033_config[1].i2c_addr);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300907 if (ret < 0)
908 goto err;
909
Antti Palosaaribf97b632012-12-08 22:51:19 -0300910 ret = af9035_wr_reg(d, 0x00d81a, state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300911 if (ret < 0)
912 goto err;
913 }
914
915 /* attach demodulator */
Antti Palosaaribf97b632012-12-08 22:51:19 -0300916 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
917 &d->i2c_adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300918 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300919 ret = -ENODEV;
920 goto err;
921 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300922
923 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300924 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
925 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300926
927 return 0;
928
929err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300930 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300931
932 return ret;
933}
934
935static struct tua9001_config af9035_tua9001_config = {
936 .i2c_addr = 0x60,
937};
938
Michael Büschffc501f2012-04-02 12:18:36 -0300939static const struct fc0011_config af9035_fc0011_config = {
940 .i2c_address = 0x60,
941};
942
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300943static struct mxl5007t_config af9035_mxl5007t_config[] = {
944 {
945 .xtal_freq_hz = MxL_XTAL_24_MHZ,
946 .if_freq_hz = MxL_IF_4_57_MHZ,
947 .invert_if = 0,
948 .loop_thru_enable = 0,
949 .clk_out_enable = 0,
950 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
951 }, {
952 .xtal_freq_hz = MxL_XTAL_24_MHZ,
953 .if_freq_hz = MxL_IF_4_57_MHZ,
954 .invert_if = 0,
955 .loop_thru_enable = 1,
956 .clk_out_enable = 1,
957 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
958 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300959};
960
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300961static struct tda18218_config af9035_tda18218_config = {
962 .i2c_address = 0x60,
963 .i2c_wr_max = 21,
964};
965
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300966static const struct fc2580_config af9035_fc2580_config = {
967 .i2c_addr = 0x56,
968 .clock = 16384000,
969};
970
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300971static const struct fc0012_config af9035_fc0012_config[] = {
972 {
973 .i2c_address = 0x63,
974 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300975 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300976 .loop_through = true,
977 .clock_out = true,
978 }, {
979 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
980 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300981 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300982 }
Antti Palosaariad3a7582012-12-08 23:27:49 -0300983};
984
Antti Palosaariac77fb02013-01-07 09:51:13 -0300985static struct ite_config af9035_it913x_config = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300986 .chip_ver = 0x02,
Antti Palosaariac77fb02013-01-07 09:51:13 -0300987 .chip_type = 0x9135,
988 .firmware = 0x00000000,
989 .firmware_ver = 1,
990 .adc_x2 = 1,
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300991 .tuner_id_0 = 0x00,
Antti Palosaariac77fb02013-01-07 09:51:13 -0300992 .tuner_id_1 = 0x00,
993 .dual_mode = 0x00,
994 .adf = 0x00,
995 /* option to read SIGNAL_LEVEL */
996 .read_slevel = 0,
997};
998
Antti Palosaari7f882c22012-03-30 09:10:08 -0300999static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1000{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001001 struct state *state = adap_to_priv(adap);
1002 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001003 int ret;
1004 struct dvb_frontend *fe;
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001005 struct i2c_msg msg[1];
Antti Palosaaribf97b632012-12-08 22:51:19 -03001006 u8 tuner_addr;
1007 /*
1008 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1009 * to carry info about used I2C bus for dual tuner configuration.
1010 */
Antti Palosaari7f882c22012-03-30 09:10:08 -03001011
Antti Palosaari2a79eef2012-05-07 14:50:40 -03001012 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -03001013 case AF9033_TUNER_TUA9001:
1014 /* AF9035 gpiot3 = TUA9001 RESETN
1015 AF9035 gpiot2 = TUA9001 RXEN */
1016
1017 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001018 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001019 if (ret < 0)
1020 goto err;
1021
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001022 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001023 if (ret < 0)
1024 goto err;
1025
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001026 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001027 if (ret < 0)
1028 goto err;
1029
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001030 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001031 if (ret < 0)
1032 goto err;
1033
Antti Palosaari7f882c22012-03-30 09:10:08 -03001034 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001035 fe = dvb_attach(tua9001_attach, adap->fe[0],
1036 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001037 break;
Michael Büschffc501f2012-04-02 12:18:36 -03001038 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001039 fe = dvb_attach(fc0011_attach, adap->fe[0],
1040 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -03001041 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001042 case AF9033_TUNER_MXL5007T:
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001043 if (adap->id == 0) {
1044 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1045 if (ret < 0)
1046 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001047
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001048 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1049 if (ret < 0)
1050 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001051
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001052 ret = af9035_wr_reg(d, 0x00d8df, 0);
1053 if (ret < 0)
1054 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001055
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001056 msleep(30);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001057
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001058 ret = af9035_wr_reg(d, 0x00d8df, 1);
1059 if (ret < 0)
1060 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001061
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001062 msleep(300);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001063
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001064 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1065 if (ret < 0)
1066 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001067
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001068 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1069 if (ret < 0)
1070 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001071
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001072 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1073 if (ret < 0)
1074 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001075
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001076 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1077 if (ret < 0)
1078 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001079
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001080 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1081 if (ret < 0)
1082 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001083
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001084 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1085 if (ret < 0)
1086 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001087
1088 tuner_addr = 0x60;
1089 } else {
1090 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001091 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001092
1093 /* attach tuner */
Antti Palosaaribf97b632012-12-08 22:51:19 -03001094 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1095 tuner_addr, &af9035_mxl5007t_config[adap->id]);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001096 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001097 case AF9033_TUNER_TDA18218:
1098 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001099 fe = dvb_attach(tda18218_attach, adap->fe[0],
1100 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001101 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001102 case AF9033_TUNER_FC2580:
1103 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1104 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1105 if (ret < 0)
1106 goto err;
1107
1108 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1109 if (ret < 0)
1110 goto err;
1111
1112 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1113 if (ret < 0)
1114 goto err;
1115
1116 usleep_range(10000, 50000);
1117 /* attach tuner */
1118 fe = dvb_attach(fc2580_attach, adap->fe[0],
1119 &d->i2c_adap, &af9035_fc2580_config);
1120 break;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001121 case AF9033_TUNER_FC0012:
1122 /*
1123 * AF9035 gpiot2 = FC0012 enable
1124 * XXX: there seems to be something on gpioh8 too, but on my
1125 * my test I didn't find any difference.
1126 */
1127
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001128 if (adap->id == 0) {
1129 /* configure gpiot2 as output and high */
1130 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1131 if (ret < 0)
1132 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001133
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001134 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1135 if (ret < 0)
1136 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001137
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001138 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1139 if (ret < 0)
1140 goto err;
1141 } else {
1142 /*
1143 * FIXME: That belongs for the FC0012 driver.
1144 * Write 02 to FC0012 master tuner register 0d directly
1145 * in order to make slave tuner working.
1146 */
1147 msg[0].addr = 0x63;
1148 msg[0].flags = 0;
1149 msg[0].len = 2;
1150 msg[0].buf = "\x0d\x02";
1151 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1152 if (ret < 0)
1153 goto err;
1154 }
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001155
1156 usleep_range(10000, 50000);
1157
Antti Palosaariad3a7582012-12-08 23:27:49 -03001158 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001159 &af9035_fc0012_config[adap->id]);
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001160 break;
Antti Palosaariac77fb02013-01-07 09:51:13 -03001161 case AF9033_TUNER_IT9135_38:
Antti Palosaari74c18832013-01-07 15:16:54 -03001162 case AF9033_TUNER_IT9135_51:
1163 case AF9033_TUNER_IT9135_52:
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001164 af9035_it913x_config.chip_ver = 0x01;
Antti Palosaari74c18832013-01-07 15:16:54 -03001165 case AF9033_TUNER_IT9135_60:
1166 case AF9033_TUNER_IT9135_61:
1167 case AF9033_TUNER_IT9135_62:
Antti Palosaariac77fb02013-01-07 09:51:13 -03001168 /* attach tuner */
Antti Palosaari74c18832013-01-07 15:16:54 -03001169 af9035_it913x_config.tuner_id_0 = state->af9033_config[0].tuner;
Antti Palosaariac77fb02013-01-07 09:51:13 -03001170 fe = dvb_attach(it913x_attach, adap->fe[0],
1171 &d->i2c_adap, 0x38, &af9035_it913x_config);
1172 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -03001173 default:
1174 fe = NULL;
1175 }
1176
1177 if (fe == NULL) {
1178 ret = -ENODEV;
1179 goto err;
1180 }
1181
1182 return 0;
1183
1184err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001185 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001186
1187 return ret;
1188}
1189
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001190static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -03001191{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001192 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001193 int ret, i;
Antti Palosaaribada3422013-01-11 20:45:11 -03001194 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1195 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001196 struct reg_val_mask tab[] = {
1197 { 0x80f99d, 0x01, 0x01 },
1198 { 0x80f9a4, 0x01, 0x01 },
1199 { 0x00dd11, 0x00, 0x20 },
1200 { 0x00dd11, 0x00, 0x40 },
1201 { 0x00dd13, 0x00, 0x20 },
1202 { 0x00dd13, 0x00, 0x40 },
1203 { 0x00dd11, 0x20, 0x20 },
1204 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1205 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1206 { 0x00dd0c, packet_size, 0xff},
1207 { 0x00dd11, state->dual_mode << 6, 0x40 },
1208 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1209 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1210 { 0x00dd0d, packet_size, 0xff },
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001211 { 0x80f9a3, state->dual_mode, 0x01 },
1212 { 0x80f9cd, state->dual_mode, 0x01 },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001213 { 0x80f99d, 0x00, 0x01 },
1214 { 0x80f9a4, 0x00, 0x01 },
1215 };
Antti Palosaari7f882c22012-03-30 09:10:08 -03001216
Antti Palosaari119f7a82012-09-12 20:23:53 -03001217 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
1218 "packet_size=%02x\n", __func__,
1219 d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001220
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001221 /* init endpoints */
1222 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1223 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1224 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001225 if (ret < 0)
1226 goto err;
1227 }
1228
1229 return 0;
1230
1231err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001232 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001233
1234 return ret;
1235}
1236
Antti Palosaari37b44a02013-01-04 15:21:26 -03001237#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001238static int af9035_rc_query(struct dvb_usb_device *d)
1239{
1240 unsigned int key;
1241 unsigned char b[4];
1242 int ret;
1243 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
1244
1245 ret = af9035_ctrl_msg(d, &req);
1246 if (ret < 0)
1247 goto err;
1248
1249 if ((b[2] + b[3]) == 0xff) {
1250 if ((b[0] + b[1]) == 0xff) {
1251 /* NEC */
1252 key = b[0] << 8 | b[2];
1253 } else {
1254 /* ext. NEC */
1255 key = b[0] << 16 | b[1] << 8 | b[2];
1256 }
1257 } else {
1258 key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
1259 }
1260
1261 rc_keydown(d->rc_dev, key, 0);
1262
1263err:
1264 /* ignore errors */
1265 return 0;
1266}
1267
1268static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1269{
Antti Palosaari74c18832013-01-07 15:16:54 -03001270 struct state *state = d_to_priv(d);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001271 int ret;
1272 u8 tmp;
1273
Antti Palosaari74c18832013-01-07 15:16:54 -03001274 /* TODO: IT9135 remote control support */
1275 if (state->chip_type == 0x9135)
1276 return 0;
1277
Antti Palosaari9ea36812013-01-11 22:16:25 -03001278 ret = af9035_rd_reg(d, EEPROM_BASE_AF9035 + EEPROM_IR_MODE, &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001279 if (ret < 0)
1280 goto err;
1281
Antti Palosaari119f7a82012-09-12 20:23:53 -03001282 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001283
1284 /* don't activate rc if in HID mode or if not available */
1285 if (tmp == 5) {
Antti Palosaari9ea36812013-01-11 22:16:25 -03001286 ret = af9035_rd_reg(d, EEPROM_BASE_AF9035 + EEPROM_IR_TYPE,
1287 &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001288 if (ret < 0)
1289 goto err;
1290
Antti Palosaari119f7a82012-09-12 20:23:53 -03001291 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001292
1293 switch (tmp) {
1294 case 0: /* NEC */
1295 default:
David Härdemanc003ab12012-10-11 19:11:54 -03001296 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001297 break;
1298 case 1: /* RC6 */
David Härdemanc003ab12012-10-11 19:11:54 -03001299 rc->allowed_protos = RC_BIT_RC6_MCE;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001300 break;
1301 }
1302
1303 rc->query = af9035_rc_query;
1304 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001305
1306 /* load empty to enable rc */
1307 if (!rc->map_name)
1308 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001309 }
1310
1311 return 0;
1312
1313err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001314 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001315
1316 return ret;
1317}
Antti Palosaarieed56702012-12-09 20:18:31 -03001318#else
1319 #define af9035_get_rc_config NULL
1320#endif
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001321
Antti Palosaaribada3422013-01-11 20:45:11 -03001322static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1323 struct usb_data_stream_properties *stream)
1324{
1325 struct dvb_usb_device *d = fe_to_d(fe);
1326 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1327
1328 if (d->udev->speed == USB_SPEED_FULL)
1329 stream->u.bulk.buffersize = 5 * 188;
1330
1331 return 0;
1332}
1333
1334/*
1335 * FIXME: PID filter is property of demodulator and should be moved to the
1336 * correct driver. Also we support only adapter #0 PID filter and will
1337 * disable adapter #1 if USB1.1 is used.
1338 */
1339static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1340{
1341 struct dvb_usb_device *d = adap_to_d(adap);
1342 int ret;
1343
1344 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1345
1346 ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1347 if (ret < 0)
1348 goto err;
1349
1350 return 0;
1351
1352err:
1353 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1354
1355 return ret;
1356}
1357
1358static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1359 int onoff)
1360{
1361 struct dvb_usb_device *d = adap_to_d(adap);
1362 int ret;
1363 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1364
1365 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1366 __func__, index, pid, onoff);
1367
1368 ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1369 if (ret < 0)
1370 goto err;
1371
1372 ret = af9035_wr_reg(d, 0x80f994, onoff);
1373 if (ret < 0)
1374 goto err;
1375
1376 ret = af9035_wr_reg(d, 0x80f995, index);
1377 if (ret < 0)
1378 goto err;
1379
1380 return 0;
1381
1382err:
1383 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1384
1385 return ret;
1386}
1387
Antti Palosaarib799b812013-01-11 16:22:07 -03001388static int af9035_probe(struct usb_interface *intf,
1389 const struct usb_device_id *id)
1390{
1391 struct usb_device *udev = interface_to_usbdev(intf);
1392 char manufacturer[sizeof("Afatech")];
1393
1394 memset(manufacturer, 0, sizeof(manufacturer));
1395 usb_string(udev, udev->descriptor.iManufacturer,
1396 manufacturer, sizeof(manufacturer));
1397 /*
1398 * There is two devices having same ID but different chipset. One uses
1399 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1400 * is iManufacturer string.
1401 *
1402 * idVendor 0x0ccd TerraTec Electronic GmbH
1403 * idProduct 0x0099
1404 * bcdDevice 2.00
1405 * iManufacturer 1 Afatech
1406 * iProduct 2 DVB-T 2
1407 *
1408 * idVendor 0x0ccd TerraTec Electronic GmbH
1409 * idProduct 0x0099
1410 * bcdDevice 2.00
1411 * iManufacturer 1 ITE Technologies, Inc.
1412 * iProduct 2 DVB-T TV Stick
1413 */
1414 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1415 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1416 if (!strcmp("Afatech", manufacturer)) {
1417 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1418 return -ENODEV;
1419 }
1420 }
1421
1422 return dvb_usbv2_probe(intf, id);
1423}
1424
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001425/* interface 0 is used by DVB-T receiver and
1426 interface 1 is for remote controller (HID) */
1427static const struct dvb_usb_device_properties af9035_props = {
1428 .driver_name = KBUILD_MODNAME,
1429 .owner = THIS_MODULE,
1430 .adapter_nr = adapter_nr,
1431 .size_of_priv = sizeof(struct state),
1432
1433 .generic_bulk_ctrl_endpoint = 0x02,
1434 .generic_bulk_ctrl_endpoint_response = 0x81,
1435
1436 .identify_state = af9035_identify_state,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001437 .download_firmware = af9035_download_firmware,
1438
1439 .i2c_algo = &af9035_i2c_algo,
1440 .read_config = af9035_read_config,
1441 .frontend_attach = af9035_frontend_attach,
1442 .tuner_attach = af9035_tuner_attach,
1443 .init = af9035_init,
1444 .get_rc_config = af9035_get_rc_config,
Antti Palosaaribada3422013-01-11 20:45:11 -03001445 .get_stream_config = af9035_get_stream_config,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001446
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001447 .get_adapter_count = af9035_get_adapter_count,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001448 .adapter = {
1449 {
Antti Palosaaribada3422013-01-11 20:45:11 -03001450 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1451 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1452
1453 .pid_filter_count = 32,
1454 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1455 .pid_filter = af9035_pid_filter,
1456
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001457 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1458 }, {
1459 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1460 },
1461 },
1462};
1463
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001464static const struct usb_device_id af9035_id_table[] = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001465 /* AF9035 devices */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001466 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1467 &af9035_props, "Afatech AF9035 reference design", NULL) },
1468 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1469 &af9035_props, "Afatech AF9035 reference design", NULL) },
1470 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1471 &af9035_props, "Afatech AF9035 reference design", NULL) },
1472 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1473 &af9035_props, "Afatech AF9035 reference design", NULL) },
1474 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1475 &af9035_props, "Afatech AF9035 reference design", NULL) },
1476 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1477 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1478 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1479 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1480 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1481 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1482 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1483 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1484 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1485 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1486 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1487 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001488 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1489 &af9035_props, "Asus U3100Mini Plus", NULL) },
Fabrizio Gazzatod9b75952013-02-17 18:25:34 -03001490 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
1491 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001492 /* IT9135 devices */
1493#if 0
1494 { DVB_USB_DEVICE(0x048d, 0x9135,
1495 &af9035_props, "IT9135 reference design", NULL) },
1496 { DVB_USB_DEVICE(0x048d, 0x9006,
1497 &af9035_props, "IT9135 reference design", NULL) },
1498#endif
Antti Palosaarib799b812013-01-11 16:22:07 -03001499 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1500 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1501 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001502 { }
1503};
1504MODULE_DEVICE_TABLE(usb, af9035_id_table);
1505
Antti Palosaari7f882c22012-03-30 09:10:08 -03001506static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001507 .name = KBUILD_MODNAME,
1508 .id_table = af9035_id_table,
Antti Palosaarib799b812013-01-11 16:22:07 -03001509 .probe = af9035_probe,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001510 .disconnect = dvb_usbv2_disconnect,
1511 .suspend = dvb_usbv2_suspend,
1512 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001513 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001514 .no_dynamic_id = 1,
1515 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001516};
1517
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001518module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001519
1520MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1521MODULE_DESCRIPTION("Afatech AF9035 driver");
1522MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001523MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
Antti Palosaari74c18832013-01-07 15:16:54 -03001524MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1525MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);