blob: 1ea17dc2a76ed51b6560ae642c6f31301f4a57fe [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",
Antti Palosaarie8292e22013-06-03 18:50:43 -030058 KBUILD_MODNAME, req->wlen, req->rlen);
Antti Palosaari3484d372013-02-26 13:56:34 -030059 ret = -EINVAL;
Wei Yongjun0170a392013-03-26 01:32:19 -030060 goto exit;
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)
Wei Yongjun0170a392013-03-26 01:32:19 -030084 goto exit;
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 Palosaaricb9114e2013-06-03 18:42:14 -030094 dev_err(&d->udev->dev,
95 "%s: command=%02x checksum mismatch (%04x != %04x)\n",
96 KBUILD_MODNAME, req->cmd, tmp_checksum,
97 checksum);
Michael Büschb1a95992012-04-01 16:33:48 -030098 ret = -EIO;
Wei Yongjun0170a392013-03-26 01:32:19 -030099 goto exit;
Michael Büschb1a95992012-04-01 16:33:48 -0300100 }
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300101
Antti Palosaari7f882c22012-03-30 09:10:08 -0300102 /* check status */
Antti Palosaari3484d372013-02-26 13:56:34 -0300103 if (state->buf[2]) {
Antti Palosaari1bfd5292013-03-08 17:39:12 -0300104 /* fw returns status 1 when IR code was not received */
Wei Yongjun0170a392013-03-26 01:32:19 -0300105 if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
106 ret = 1;
107 goto exit;
108 }
Antti Palosaari1bfd5292013-03-08 17:39:12 -0300109
Antti Palosaari119f7a82012-09-12 20:23:53 -0300110 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
Antti Palosaari3484d372013-02-26 13:56:34 -0300111 __func__, req->cmd, state->buf[2]);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300112 ret = -EIO;
Wei Yongjun0170a392013-03-26 01:32:19 -0300113 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300114 }
115
116 /* read request, copy returned data to return buf */
117 if (req->rlen)
Antti Palosaari3484d372013-02-26 13:56:34 -0300118 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300119exit:
Antti Palosaari3484d372013-02-26 13:56:34 -0300120 mutex_unlock(&d->usb_mutex);
Wei Yongjun0170a392013-03-26 01:32:19 -0300121 if (ret < 0)
Antti Palosaari3484d372013-02-26 13:56:34 -0300122 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300123 return ret;
124}
125
126/* write multiple registers */
127static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
128{
129 u8 wbuf[6 + len];
130 u8 mbox = (reg >> 16) & 0xff;
131 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
132
133 wbuf[0] = len;
134 wbuf[1] = 2;
135 wbuf[2] = 0;
136 wbuf[3] = 0;
137 wbuf[4] = (reg >> 8) & 0xff;
138 wbuf[5] = (reg >> 0) & 0xff;
139 memcpy(&wbuf[6], val, len);
140
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300141 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300142}
143
144/* read multiple registers */
145static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
146{
147 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
148 u8 mbox = (reg >> 16) & 0xff;
149 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
150
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300151 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300152}
153
154/* write single register */
155static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
156{
157 return af9035_wr_regs(d, reg, &val, 1);
158}
159
160/* read single register */
161static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
162{
163 return af9035_rd_regs(d, reg, val, 1);
164}
165
166/* write single register with mask */
167static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
168 u8 mask)
169{
170 int ret;
171 u8 tmp;
172
173 /* no need for read if whole reg is written */
174 if (mask != 0xff) {
175 ret = af9035_rd_regs(d, reg, &tmp, 1);
176 if (ret)
177 return ret;
178
179 val &= mask;
180 tmp &= ~mask;
181 val |= tmp;
182 }
183
184 return af9035_wr_regs(d, reg, &val, 1);
185}
186
187static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
188 struct i2c_msg msg[], int num)
189{
190 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300191 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300192 int ret;
193
194 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
195 return -EAGAIN;
196
Antti Palosaariad30e912012-04-02 20:18:59 -0300197 /*
198 * I2C sub header is 5 bytes long. Meaning of those bytes are:
199 * 0: data len
200 * 1: I2C addr << 1
201 * 2: reg addr len
202 * byte 3 and 4 can be used as reg addr
203 * 3: reg addr MSB
204 * used when reg addr len is set to 2
205 * 4: reg addr LSB
206 * used when reg addr len is set to 1 or 2
207 *
208 * For the simplify we do not use register addr at all.
209 * NOTE: As a firmware knows tuner type there is very small possibility
210 * there could be some tuner I2C hacks done by firmware and this may
211 * lead problems if firmware expects those bytes are used.
212 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300213 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
214 (msg[1].flags & I2C_M_RD)) {
215 if (msg[0].len > 40 || msg[1].len > 40) {
216 /* TODO: correct limits > 40 */
217 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300218 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
219 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300220 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300221 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
222 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300223
224 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300225 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300226
Antti Palosaari7f882c22012-03-30 09:10:08 -0300227 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
228 msg[1].len);
229 } else {
230 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300231 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300232 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
233 buf, msg[1].len, msg[1].buf };
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300234 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300235 buf[0] = msg[1].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300236 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300237 buf[2] = 0x00; /* reg addr len */
238 buf[3] = 0x00; /* reg addr MSB */
239 buf[4] = 0x00; /* reg addr LSB */
240 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300241 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300242 }
243 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
244 if (msg[0].len > 40) {
245 /* TODO: correct limits > 40 */
246 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300247 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
248 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300249 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300250 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
251 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300252
253 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300254 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300255
Antti Palosaari7f882c22012-03-30 09:10:08 -0300256 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
257 msg[0].len - 3);
258 } else {
259 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300260 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300261 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
262 0, NULL };
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300263 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300264 buf[0] = msg[0].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300265 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300266 buf[2] = 0x00; /* reg addr len */
267 buf[3] = 0x00; /* reg addr MSB */
268 buf[4] = 0x00; /* reg addr LSB */
269 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300270 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300271 }
Antti Palosaari3b98c342013-03-11 19:05:39 -0300272 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
273 if (msg[0].len > 40) {
274 /* TODO: correct limits > 40 */
275 ret = -EOPNOTSUPP;
276 } else {
277 /* I2C */
278 u8 buf[5];
279 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
280 buf, msg[0].len, msg[0].buf };
281 req.mbox |= ((msg[0].addr & 0x80) >> 3);
282 buf[0] = msg[0].len;
283 buf[1] = msg[0].addr << 1;
284 buf[2] = 0x00; /* reg addr len */
285 buf[3] = 0x00; /* reg addr MSB */
286 buf[4] = 0x00; /* reg addr LSB */
287 ret = af9035_ctrl_msg(d, &req);
288 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300289 } else {
290 /*
Antti Palosaari3b98c342013-03-11 19:05:39 -0300291 * We support only three kind of I2C transactions:
292 * 1) 1 x read + 1 x write (repeated start)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300293 * 2) 1 x write
Antti Palosaari3b98c342013-03-11 19:05:39 -0300294 * 3) 1 x read
Antti Palosaari7f882c22012-03-30 09:10:08 -0300295 */
296 ret = -EOPNOTSUPP;
297 }
298
299 mutex_unlock(&d->i2c_mutex);
300
301 if (ret < 0)
302 return ret;
303 else
304 return num;
305}
306
307static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
308{
309 return I2C_FUNC_I2C;
310}
311
312static struct i2c_algorithm af9035_i2c_algo = {
313 .master_xfer = af9035_i2c_master_xfer,
314 .functionality = af9035_i2c_functionality,
315};
316
Antti Palosaaria0921af2012-06-18 23:42:53 -0300317static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300318{
Antti Palosaari74c18832013-01-07 15:16:54 -0300319 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300320 int ret;
321 u8 wbuf[1] = { 1 };
322 u8 rbuf[4];
323 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
324 sizeof(rbuf), rbuf };
325
Antti Palosaari74c18832013-01-07 15:16:54 -0300326 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
327 if (ret < 0)
328 goto err;
329
330 state->chip_version = rbuf[0];
331 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
332
333 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
334 if (ret < 0)
335 goto err;
336
337 dev_info(&d->udev->dev,
338 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
Antti Palosaarie8292e22013-06-03 18:50:43 -0300339 KBUILD_MODNAME, state->prechip_version,
340 state->chip_version, state->chip_type);
Antti Palosaari74c18832013-01-07 15:16:54 -0300341
342 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300343 if (state->chip_version == 0x02)
Antti Palosaari74c18832013-01-07 15:16:54 -0300344 *name = AF9035_FIRMWARE_IT9135_V2;
345 else
346 *name = AF9035_FIRMWARE_IT9135_V1;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300347 state->eeprom_addr = EEPROM_BASE_IT9135;
Antti Palosaari74c18832013-01-07 15:16:54 -0300348 } else {
349 *name = AF9035_FIRMWARE_AF9035;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300350 state->eeprom_addr = EEPROM_BASE_AF9035;
Antti Palosaari74c18832013-01-07 15:16:54 -0300351 }
352
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300353 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300354 if (ret < 0)
355 goto err;
356
Antti Palosaari119f7a82012-09-12 20:23:53 -0300357 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300358 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300359 ret = WARM;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300360 else
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300361 ret = COLD;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300362
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300363 return ret;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300364
365err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300366 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300367
368 return ret;
369}
370
Antti Palosaari8229da52013-03-07 17:59:55 -0300371static int af9035_download_firmware_old(struct dvb_usb_device *d,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300372 const struct firmware *fw)
373{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300374 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300375 u8 wbuf[1];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300376 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
377 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300378 u8 hdr_core;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300379 u16 hdr_addr, hdr_data_len, hdr_checksum;
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300380 #define MAX_DATA 58
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300381 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300382
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300383 /*
384 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
385 *
386 * byte 0: MCS 51 core
387 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
388 * address spaces
389 * byte 1-2: Big endian destination address
390 * byte 3-4: Big endian number of data bytes following the header
391 * byte 5-6: Big endian header checksum, apparently ignored by the chip
392 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
393 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300394
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300395 for (i = fw->size; i > HDR_SIZE;) {
396 hdr_core = fw->data[fw->size - i + 0];
397 hdr_addr = fw->data[fw->size - i + 1] << 8;
398 hdr_addr |= fw->data[fw->size - i + 2] << 0;
399 hdr_data_len = fw->data[fw->size - i + 3] << 8;
400 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
401 hdr_checksum = fw->data[fw->size - i + 5] << 8;
402 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300403
Antti Palosaaricb9114e2013-06-03 18:42:14 -0300404 dev_dbg(&d->udev->dev,
405 "%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
406 __func__, hdr_core, hdr_addr, hdr_data_len,
407 hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300408
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300409 if (((hdr_core != 1) && (hdr_core != 2)) ||
410 (hdr_data_len > i)) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300411 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300412 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300413 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300414
415 /* download begin packet */
416 req.cmd = CMD_FW_DL_BEGIN;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300417 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300418 if (ret < 0)
419 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300420
421 /* download firmware packet(s) */
422 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
423 len = j;
424 if (len > MAX_DATA)
425 len = MAX_DATA;
426 req_fw_dl.wlen = len;
427 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
428 HDR_SIZE + hdr_data_len - j];
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300429 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300430 if (ret < 0)
431 goto err;
432 }
433
434 /* download end packet */
435 req.cmd = CMD_FW_DL_END;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300436 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300437 if (ret < 0)
438 goto err;
439
440 i -= hdr_data_len + HDR_SIZE;
441
Antti Palosaari119f7a82012-09-12 20:23:53 -0300442 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
443 __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300444 }
445
Antti Palosaariff4e3fe2012-12-09 16:25:08 -0300446 /* print warn if firmware is bad, continue and see what happens */
447 if (i)
448 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
449
Antti Palosaari7f882c22012-03-30 09:10:08 -0300450 return 0;
451
452err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300453 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300454
455 return ret;
456}
457
Antti Palosaari8229da52013-03-07 17:59:55 -0300458static int af9035_download_firmware_new(struct dvb_usb_device *d,
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300459 const struct firmware *fw)
460{
461 int ret, i, i_prev;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300462 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300463 #define HDR_SIZE 7
464
465 /*
466 * There seems to be following firmware header. Meaning of bytes 0-3
467 * is unknown.
468 *
469 * 0: 3
470 * 1: 0, 1
471 * 2: 0
472 * 3: 1, 2, 3
473 * 4: addr MSB
474 * 5: addr LSB
475 * 6: count of data bytes ?
476 */
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300477 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
478 if (i == fw->size ||
479 (fw->data[i + 0] == 0x03 &&
480 (fw->data[i + 1] == 0x00 ||
481 fw->data[i + 1] == 0x01) &&
482 fw->data[i + 2] == 0x00)) {
483 req_fw_dl.wlen = i - i_prev;
484 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
485 i_prev = i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300486 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300487 if (ret < 0)
488 goto err;
489
Antti Palosaari119f7a82012-09-12 20:23:53 -0300490 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
491 __func__, i);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300492 }
493 }
494
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300495 return 0;
496
497err:
498 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
499
500 return ret;
501}
502
503static int af9035_download_firmware(struct dvb_usb_device *d,
504 const struct firmware *fw)
505{
506 struct state *state = d_to_priv(d);
507 int ret;
508 u8 wbuf[1];
509 u8 rbuf[4];
510 u8 tmp;
511 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
Antti Palosaaricb9114e2013-06-03 18:42:14 -0300512 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300513 dev_dbg(&d->udev->dev, "%s:\n", __func__);
514
515 /*
516 * In case of dual tuner configuration we need to do some extra
517 * initialization in order to download firmware to slave demod too,
518 * which is done by master demod.
519 * Master feeds also clock and controls power via GPIO.
520 */
Antti Palosaarid716ef42013-06-03 19:39:51 -0300521 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300522 if (ret < 0)
523 goto err;
524
Antti Palosaarid716ef42013-06-03 19:39:51 -0300525 if (tmp == 1 || tmp == 3) {
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300526 /* configure gpioh1, reset & power slave demod */
527 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
528 if (ret < 0)
529 goto err;
530
531 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
532 if (ret < 0)
533 goto err;
534
535 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
536 if (ret < 0)
537 goto err;
538
539 usleep_range(10000, 50000);
540
541 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
542 if (ret < 0)
543 goto err;
544
545 /* tell the slave I2C address */
546 ret = af9035_rd_reg(d,
547 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
548 &tmp);
549 if (ret < 0)
550 goto err;
551
552 if (state->chip_type == 0x9135) {
553 ret = af9035_wr_reg(d, 0x004bfb, tmp);
554 if (ret < 0)
555 goto err;
556 } else {
557 ret = af9035_wr_reg(d, 0x00417f, tmp);
558 if (ret < 0)
559 goto err;
560
561 /* enable clock out */
562 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
563 if (ret < 0)
564 goto err;
565 }
566 }
567
Antti Palosaari8229da52013-03-07 17:59:55 -0300568 if (fw->data[0] == 0x01)
569 ret = af9035_download_firmware_old(d, fw);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300570 else
Antti Palosaari8229da52013-03-07 17:59:55 -0300571 ret = af9035_download_firmware_new(d, fw);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300572 if (ret < 0)
573 goto err;
574
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300575 /* firmware loaded, request boot */
576 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300577 ret = af9035_ctrl_msg(d, &req);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300578 if (ret < 0)
579 goto err;
580
581 /* ensure firmware starts */
582 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300583 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300584 if (ret < 0)
585 goto err;
586
587 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300588 dev_err(&d->udev->dev, "%s: firmware did not run\n",
589 KBUILD_MODNAME);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300590 ret = -ENODEV;
591 goto err;
592 }
593
Antti Palosaari119f7a82012-09-12 20:23:53 -0300594 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
595 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300596
597 return 0;
598
599err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300600 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300601
602 return ret;
603}
604
Antti Palosaari9ea36812013-01-11 22:16:25 -0300605static int af9035_read_config(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300606{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300607 struct state *state = d_to_priv(d);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300608 int ret, i;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300609 u8 tmp;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300610 u16 tmp16, addr;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300611
Antti Palosaaribf97b632012-12-08 22:51:19 -0300612 /* demod I2C "address" */
613 state->af9033_config[0].i2c_addr = 0x38;
Antti Palosaari0d94d6a2013-01-07 15:26:05 -0300614 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300615 state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaariab56ad62013-03-07 18:43:51 -0300616 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
617 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300618
Antti Palosaari9ea36812013-01-11 22:16:25 -0300619 /* eeprom memory mapped location */
620 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300621 if (state->chip_version == 0x02) {
622 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300623 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300624 tmp16 = 0x00461d;
625 } else {
626 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300627 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300628 tmp16 = 0x00461b;
629 }
630
Antti Palosaari9ea36812013-01-11 22:16:25 -0300631 /* check if eeprom exists */
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300632 ret = af9035_rd_reg(d, tmp16, &tmp);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300633 if (ret < 0)
634 goto err;
635
Antti Palosaari431a6d42013-03-07 18:28:25 -0300636 if (tmp == 0x00) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300637 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300638 goto skip_eeprom;
639 }
Antti Palosaari9ea36812013-01-11 22:16:25 -0300640 }
641
Antti Palosaari7f882c22012-03-30 09:10:08 -0300642 /* check if there is dual tuners */
Antti Palosaarid716ef42013-06-03 19:39:51 -0300643 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300644 if (ret < 0)
645 goto err;
646
Antti Palosaarid716ef42013-06-03 19:39:51 -0300647 if (tmp == 1 || tmp == 3)
648 state->dual_mode = true;
649
650 dev_dbg(&d->udev->dev, "%s: ts mode=%d dual mode=%d\n", __func__,
651 tmp, state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300652
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300653 if (state->dual_mode) {
654 /* read 2nd demodulator I2C address */
Antti Palosaari431a6d42013-03-07 18:28:25 -0300655 ret = af9035_rd_reg(d,
656 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
657 &tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300658 if (ret < 0)
659 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300660
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300661 state->af9033_config[1].i2c_addr = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300662 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
663 __func__, tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300664 }
665
Antti Palosaari431a6d42013-03-07 18:28:25 -0300666 addr = state->eeprom_addr;
667
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300668 for (i = 0; i < state->dual_mode + 1; i++) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300669 /* tuner */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300670 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300671 if (ret < 0)
672 goto err;
673
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300674 if (tmp == 0x00)
675 dev_dbg(&d->udev->dev,
676 "%s: [%d]tuner not set, using default\n",
677 __func__, i);
678 else
679 state->af9033_config[i].tuner = tmp;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300680
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300681 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
682 __func__, i, state->af9033_config[i].tuner);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300683
684 switch (state->af9033_config[i].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300685 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300686 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300687 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300688 case AF9033_TUNER_TDA18218:
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300689 case AF9033_TUNER_FC2580:
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300690 case AF9033_TUNER_FC0012:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300691 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300692 break;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300693 case AF9033_TUNER_IT9135_38:
694 case AF9033_TUNER_IT9135_51:
695 case AF9033_TUNER_IT9135_52:
696 case AF9033_TUNER_IT9135_60:
697 case AF9033_TUNER_IT9135_61:
698 case AF9033_TUNER_IT9135_62:
699 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300700 default:
Antti Palosaari9ea36812013-01-11 22:16:25 -0300701 dev_warn(&d->udev->dev,
702 "%s: tuner id=%02x not supported, please report!",
Antti Palosaari119f7a82012-09-12 20:23:53 -0300703 KBUILD_MODNAME, tmp);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300704 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300705
Antti Palosaaribf97b632012-12-08 22:51:19 -0300706 /* disable dual mode if driver does not support it */
707 if (i == 1)
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300708 switch (state->af9033_config[i].tuner) {
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300709 case AF9033_TUNER_FC0012:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300710 case AF9033_TUNER_IT9135_38:
711 case AF9033_TUNER_IT9135_51:
712 case AF9033_TUNER_IT9135_52:
713 case AF9033_TUNER_IT9135_60:
714 case AF9033_TUNER_IT9135_61:
715 case AF9033_TUNER_IT9135_62:
Jose Alberto Reguero78c7bc42013-02-10 15:43:03 -0300716 case AF9033_TUNER_MXL5007T:
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300717 break;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300718 default:
719 state->dual_mode = false;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300720 dev_info(&d->udev->dev,
721 "%s: driver does not support 2nd tuner and will disable it",
722 KBUILD_MODNAME);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300723 }
724
Antti Palosaari7f882c22012-03-30 09:10:08 -0300725 /* tuner IF frequency */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300726 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300727 if (ret < 0)
728 goto err;
729
730 tmp16 = tmp;
731
Antti Palosaari9ea36812013-01-11 22:16:25 -0300732 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300733 if (ret < 0)
734 goto err;
735
736 tmp16 |= tmp << 8;
737
Antti Palosaari119f7a82012-09-12 20:23:53 -0300738 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300739
Antti Palosaari9ea36812013-01-11 22:16:25 -0300740 addr += 0x10; /* shift for the 2nd tuner params */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300741 }
742
Antti Palosaari9ea36812013-01-11 22:16:25 -0300743skip_eeprom:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300744 /* get demod clock */
745 ret = af9035_rd_reg(d, 0x00d800, &tmp);
746 if (ret < 0)
747 goto err;
748
749 tmp = (tmp >> 0) & 0x0f;
750
Antti Palosaari9ea36812013-01-11 22:16:25 -0300751 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
752 if (state->chip_type == 0x9135)
753 state->af9033_config[i].clock = clock_lut_it9135[tmp];
754 else
755 state->af9033_config[i].clock = clock_lut_af9035[tmp];
Antti Palosaari74c18832013-01-07 15:16:54 -0300756 }
757
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300758 return 0;
759
760err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300761 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300762
763 return ret;
764}
765
Antti Palosaari51639be2012-09-16 22:26:56 -0300766static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
767 int cmd, int arg)
768{
769 int ret;
770 u8 val;
771
772 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
773
774 /*
775 * CEN always enabled by hardware wiring
776 * RESETN GPIOT3
777 * RXEN GPIOT2
778 */
779
780 switch (cmd) {
781 case TUA9001_CMD_RESETN:
782 if (arg)
783 val = 0x00;
784 else
785 val = 0x01;
786
787 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
788 if (ret < 0)
789 goto err;
790 break;
791 case TUA9001_CMD_RXEN:
792 if (arg)
793 val = 0x01;
794 else
795 val = 0x00;
796
797 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
798 if (ret < 0)
799 goto err;
800 break;
801 }
802
803 return 0;
804
805err:
806 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
807
808 return ret;
809}
810
811
Michael Büschffc501f2012-04-02 12:18:36 -0300812static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300813 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300814{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300815 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300816
817 switch (cmd) {
818 case FC0011_FE_CALLBACK_POWER:
819 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300820 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
821 if (ret < 0)
822 goto err;
823
824 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
825 if (ret < 0)
826 goto err;
827
828 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
829 if (ret < 0)
830 goto err;
831
Michael Büschffc501f2012-04-02 12:18:36 -0300832 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300833 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
834 if (ret < 0)
835 goto err;
836
837 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
838 if (ret < 0)
839 goto err;
840
Michael Büschde2bec52012-04-03 05:11:30 -0300841 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300842 break;
843 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300844 ret = af9035_wr_reg(d, 0xd8e9, 1);
845 if (ret < 0)
846 goto err;
847
848 ret = af9035_wr_reg(d, 0xd8e8, 1);
849 if (ret < 0)
850 goto err;
851
852 ret = af9035_wr_reg(d, 0xd8e7, 1);
853 if (ret < 0)
854 goto err;
855
Michael Büschde2bec52012-04-03 05:11:30 -0300856 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300857
858 ret = af9035_wr_reg(d, 0xd8e7, 0);
859 if (ret < 0)
860 goto err;
861
Michael Büschde2bec52012-04-03 05:11:30 -0300862 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300863 break;
864 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300865 ret = -EINVAL;
866 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300867 }
868
869 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300870
871err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300872 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300873
874 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300875}
876
877static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
878{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300879 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300880
881 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300882 case AF9033_TUNER_FC0011:
883 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300884 case AF9033_TUNER_TUA9001:
885 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300886 default:
887 break;
888 }
889
Antti Palosaari1835af12012-09-11 22:27:06 -0300890 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300891}
892
893static int af9035_frontend_callback(void *adapter_priv, int component,
894 int cmd, int arg)
895{
896 struct i2c_adapter *adap = adapter_priv;
897 struct dvb_usb_device *d = i2c_get_adapdata(adap);
898
Antti Palosaari1835af12012-09-11 22:27:06 -0300899 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
900 __func__, component, cmd, arg);
901
Michael Büschffc501f2012-04-02 12:18:36 -0300902 switch (component) {
903 case DVB_FRONTEND_COMPONENT_TUNER:
904 return af9035_tuner_callback(d, cmd, arg);
905 default:
906 break;
907 }
908
Antti Palosaari1835af12012-09-11 22:27:06 -0300909 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300910}
911
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300912static int af9035_get_adapter_count(struct dvb_usb_device *d)
913{
914 struct state *state = d_to_priv(d);
Antti Palosaaribada3422013-01-11 20:45:11 -0300915
916 /* disable 2nd adapter as we don't have PID filters implemented */
917 if (d->udev->speed == USB_SPEED_FULL)
918 return 1;
919 else
920 return state->dual_mode + 1;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300921}
922
Antti Palosaari7f882c22012-03-30 09:10:08 -0300923static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
924{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300925 struct state *state = adap_to_priv(adap);
926 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300927 int ret;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300928 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300929
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300930 if (!state->af9033_config[adap->id].tuner) {
931 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300932 ret = -ENODEV;
933 goto err;
934 }
935
Antti Palosaari7f882c22012-03-30 09:10:08 -0300936 /* attach demodulator */
Antti Palosaaribf97b632012-12-08 22:51:19 -0300937 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
938 &d->i2c_adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300939 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300940 ret = -ENODEV;
941 goto err;
942 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300943
944 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300945 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
946 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300947
948 return 0;
949
950err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300951 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300952
953 return ret;
954}
955
956static struct tua9001_config af9035_tua9001_config = {
957 .i2c_addr = 0x60,
958};
959
Michael Büschffc501f2012-04-02 12:18:36 -0300960static const struct fc0011_config af9035_fc0011_config = {
961 .i2c_address = 0x60,
962};
963
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300964static struct mxl5007t_config af9035_mxl5007t_config[] = {
965 {
966 .xtal_freq_hz = MxL_XTAL_24_MHZ,
967 .if_freq_hz = MxL_IF_4_57_MHZ,
968 .invert_if = 0,
969 .loop_thru_enable = 0,
970 .clk_out_enable = 0,
971 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
972 }, {
973 .xtal_freq_hz = MxL_XTAL_24_MHZ,
974 .if_freq_hz = MxL_IF_4_57_MHZ,
975 .invert_if = 0,
976 .loop_thru_enable = 1,
977 .clk_out_enable = 1,
978 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
979 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300980};
981
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300982static struct tda18218_config af9035_tda18218_config = {
983 .i2c_address = 0x60,
984 .i2c_wr_max = 21,
985};
986
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300987static const struct fc2580_config af9035_fc2580_config = {
988 .i2c_addr = 0x56,
989 .clock = 16384000,
990};
991
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300992static const struct fc0012_config af9035_fc0012_config[] = {
993 {
994 .i2c_address = 0x63,
995 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300996 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300997 .loop_through = true,
998 .clock_out = true,
999 }, {
1000 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1001 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -03001002 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001003 }
Antti Palosaariad3a7582012-12-08 23:27:49 -03001004};
1005
Antti Palosaari7f882c22012-03-30 09:10:08 -03001006static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1007{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001008 struct state *state = adap_to_priv(adap);
1009 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001010 int ret;
1011 struct dvb_frontend *fe;
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001012 struct i2c_msg msg[1];
Antti Palosaaribf97b632012-12-08 22:51:19 -03001013 u8 tuner_addr;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001014 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1015
Antti Palosaaribf97b632012-12-08 22:51:19 -03001016 /*
1017 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1018 * to carry info about used I2C bus for dual tuner configuration.
1019 */
Antti Palosaari7f882c22012-03-30 09:10:08 -03001020
Antti Palosaari2a79eef2012-05-07 14:50:40 -03001021 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -03001022 case AF9033_TUNER_TUA9001:
1023 /* AF9035 gpiot3 = TUA9001 RESETN
1024 AF9035 gpiot2 = TUA9001 RXEN */
1025
1026 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001027 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001028 if (ret < 0)
1029 goto err;
1030
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001031 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001032 if (ret < 0)
1033 goto err;
1034
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001035 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001036 if (ret < 0)
1037 goto err;
1038
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001039 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001040 if (ret < 0)
1041 goto err;
1042
Antti Palosaari7f882c22012-03-30 09:10:08 -03001043 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001044 fe = dvb_attach(tua9001_attach, adap->fe[0],
1045 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001046 break;
Michael Büschffc501f2012-04-02 12:18:36 -03001047 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001048 fe = dvb_attach(fc0011_attach, adap->fe[0],
1049 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -03001050 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001051 case AF9033_TUNER_MXL5007T:
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001052 if (adap->id == 0) {
1053 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1054 if (ret < 0)
1055 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001056
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001057 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1058 if (ret < 0)
1059 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001060
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001061 ret = af9035_wr_reg(d, 0x00d8df, 0);
1062 if (ret < 0)
1063 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001064
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001065 msleep(30);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001066
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001067 ret = af9035_wr_reg(d, 0x00d8df, 1);
1068 if (ret < 0)
1069 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001070
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001071 msleep(300);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001072
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001073 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1074 if (ret < 0)
1075 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001076
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001077 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1078 if (ret < 0)
1079 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001080
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001081 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1082 if (ret < 0)
1083 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001084
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001085 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1086 if (ret < 0)
1087 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001088
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001089 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1090 if (ret < 0)
1091 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001092
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001093 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1094 if (ret < 0)
1095 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001096
1097 tuner_addr = 0x60;
1098 } else {
1099 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001100 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001101
1102 /* attach tuner */
Antti Palosaaribf97b632012-12-08 22:51:19 -03001103 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1104 tuner_addr, &af9035_mxl5007t_config[adap->id]);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001105 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001106 case AF9033_TUNER_TDA18218:
1107 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001108 fe = dvb_attach(tda18218_attach, adap->fe[0],
1109 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001110 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001111 case AF9033_TUNER_FC2580:
1112 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1113 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1114 if (ret < 0)
1115 goto err;
1116
1117 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1118 if (ret < 0)
1119 goto err;
1120
1121 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1122 if (ret < 0)
1123 goto err;
1124
1125 usleep_range(10000, 50000);
1126 /* attach tuner */
1127 fe = dvb_attach(fc2580_attach, adap->fe[0],
1128 &d->i2c_adap, &af9035_fc2580_config);
1129 break;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001130 case AF9033_TUNER_FC0012:
1131 /*
1132 * AF9035 gpiot2 = FC0012 enable
1133 * XXX: there seems to be something on gpioh8 too, but on my
1134 * my test I didn't find any difference.
1135 */
1136
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001137 if (adap->id == 0) {
1138 /* configure gpiot2 as output and high */
1139 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1140 if (ret < 0)
1141 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001142
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001143 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1144 if (ret < 0)
1145 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001146
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001147 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1148 if (ret < 0)
1149 goto err;
1150 } else {
1151 /*
1152 * FIXME: That belongs for the FC0012 driver.
1153 * Write 02 to FC0012 master tuner register 0d directly
1154 * in order to make slave tuner working.
1155 */
1156 msg[0].addr = 0x63;
1157 msg[0].flags = 0;
1158 msg[0].len = 2;
1159 msg[0].buf = "\x0d\x02";
1160 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1161 if (ret < 0)
1162 goto err;
1163 }
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001164
1165 usleep_range(10000, 50000);
1166
Antti Palosaariad3a7582012-12-08 23:27:49 -03001167 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001168 &af9035_fc0012_config[adap->id]);
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001169 break;
Antti Palosaariac77fb02013-01-07 09:51:13 -03001170 case AF9033_TUNER_IT9135_38:
Antti Palosaari74c18832013-01-07 15:16:54 -03001171 case AF9033_TUNER_IT9135_51:
1172 case AF9033_TUNER_IT9135_52:
1173 case AF9033_TUNER_IT9135_60:
1174 case AF9033_TUNER_IT9135_61:
1175 case AF9033_TUNER_IT9135_62:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001176 /* attach tuner */
1177 fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1178 state->af9033_config[adap->id].i2c_addr,
Antti Palosaari44af7472013-02-28 00:14:06 -03001179 state->af9033_config[0].tuner);
Antti Palosaariac77fb02013-01-07 09:51:13 -03001180 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -03001181 default:
1182 fe = NULL;
1183 }
1184
1185 if (fe == NULL) {
1186 ret = -ENODEV;
1187 goto err;
1188 }
1189
1190 return 0;
1191
1192err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001193 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001194
1195 return ret;
1196}
1197
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001198static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -03001199{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001200 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001201 int ret, i;
Antti Palosaaribada3422013-01-11 20:45:11 -03001202 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1203 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001204 struct reg_val_mask tab[] = {
1205 { 0x80f99d, 0x01, 0x01 },
1206 { 0x80f9a4, 0x01, 0x01 },
1207 { 0x00dd11, 0x00, 0x20 },
1208 { 0x00dd11, 0x00, 0x40 },
1209 { 0x00dd13, 0x00, 0x20 },
1210 { 0x00dd13, 0x00, 0x40 },
1211 { 0x00dd11, 0x20, 0x20 },
1212 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1213 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1214 { 0x00dd0c, packet_size, 0xff},
1215 { 0x00dd11, state->dual_mode << 6, 0x40 },
1216 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1217 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1218 { 0x00dd0d, packet_size, 0xff },
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001219 { 0x80f9a3, state->dual_mode, 0x01 },
1220 { 0x80f9cd, state->dual_mode, 0x01 },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001221 { 0x80f99d, 0x00, 0x01 },
1222 { 0x80f9a4, 0x00, 0x01 },
1223 };
Antti Palosaari7f882c22012-03-30 09:10:08 -03001224
Antti Palosaaricb9114e2013-06-03 18:42:14 -03001225 dev_dbg(&d->udev->dev,
1226 "%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1227 __func__, d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001228
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001229 /* init endpoints */
1230 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1231 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1232 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001233 if (ret < 0)
1234 goto err;
1235 }
1236
1237 return 0;
1238
1239err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001240 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001241
1242 return ret;
1243}
1244
Antti Palosaari37b44a02013-01-04 15:21:26 -03001245#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001246static int af9035_rc_query(struct dvb_usb_device *d)
1247{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001248 int ret;
Antti Palosaari75cd5882013-03-08 17:50:21 -03001249 u32 key;
1250 u8 buf[4];
1251 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001252
1253 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001254 if (ret == 1)
1255 return 0;
1256 else if (ret < 0)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001257 goto err;
1258
Antti Palosaari75cd5882013-03-08 17:50:21 -03001259 if ((buf[2] + buf[3]) == 0xff) {
1260 if ((buf[0] + buf[1]) == 0xff) {
1261 /* NEC standard 16bit */
1262 key = buf[0] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001263 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001264 /* NEC extended 24bit */
1265 key = buf[0] << 16 | buf[1] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001266 }
1267 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001268 /* NEC full code 32bit */
1269 key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001270 }
1271
Antti Palosaari75cd5882013-03-08 17:50:21 -03001272 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1273
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001274 rc_keydown(d->rc_dev, key, 0);
1275
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001276 return 0;
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001277
1278err:
1279 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1280
1281 return ret;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001282}
1283
1284static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1285{
Antti Palosaari74c18832013-01-07 15:16:54 -03001286 struct state *state = d_to_priv(d);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001287 int ret;
1288 u8 tmp;
1289
Antti Palosaari431a6d42013-03-07 18:28:25 -03001290 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001291 if (ret < 0)
1292 goto err;
1293
Antti Palosaari119f7a82012-09-12 20:23:53 -03001294 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001295
1296 /* don't activate rc if in HID mode or if not available */
1297 if (tmp == 5) {
Antti Palosaari431a6d42013-03-07 18:28:25 -03001298 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
Antti Palosaari9ea36812013-01-11 22:16:25 -03001299 &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001300 if (ret < 0)
1301 goto err;
1302
Antti Palosaari119f7a82012-09-12 20:23:53 -03001303 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001304
1305 switch (tmp) {
1306 case 0: /* NEC */
1307 default:
David Härdemanc003ab12012-10-11 19:11:54 -03001308 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001309 break;
1310 case 1: /* RC6 */
David Härdemanc003ab12012-10-11 19:11:54 -03001311 rc->allowed_protos = RC_BIT_RC6_MCE;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001312 break;
1313 }
1314
1315 rc->query = af9035_rc_query;
1316 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001317
1318 /* load empty to enable rc */
1319 if (!rc->map_name)
1320 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001321 }
1322
1323 return 0;
1324
1325err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001326 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001327
1328 return ret;
1329}
Antti Palosaarieed56702012-12-09 20:18:31 -03001330#else
1331 #define af9035_get_rc_config NULL
1332#endif
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001333
Antti Palosaaribada3422013-01-11 20:45:11 -03001334static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1335 struct usb_data_stream_properties *stream)
1336{
1337 struct dvb_usb_device *d = fe_to_d(fe);
1338 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1339
1340 if (d->udev->speed == USB_SPEED_FULL)
1341 stream->u.bulk.buffersize = 5 * 188;
1342
1343 return 0;
1344}
1345
1346/*
1347 * FIXME: PID filter is property of demodulator and should be moved to the
1348 * correct driver. Also we support only adapter #0 PID filter and will
1349 * disable adapter #1 if USB1.1 is used.
1350 */
1351static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1352{
1353 struct dvb_usb_device *d = adap_to_d(adap);
1354 int ret;
1355
1356 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1357
1358 ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1359 if (ret < 0)
1360 goto err;
1361
1362 return 0;
1363
1364err:
1365 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1366
1367 return ret;
1368}
1369
1370static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1371 int onoff)
1372{
1373 struct dvb_usb_device *d = adap_to_d(adap);
1374 int ret;
1375 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1376
1377 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1378 __func__, index, pid, onoff);
1379
1380 ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1381 if (ret < 0)
1382 goto err;
1383
1384 ret = af9035_wr_reg(d, 0x80f994, onoff);
1385 if (ret < 0)
1386 goto err;
1387
1388 ret = af9035_wr_reg(d, 0x80f995, index);
1389 if (ret < 0)
1390 goto err;
1391
1392 return 0;
1393
1394err:
1395 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1396
1397 return ret;
1398}
1399
Antti Palosaarib799b812013-01-11 16:22:07 -03001400static int af9035_probe(struct usb_interface *intf,
1401 const struct usb_device_id *id)
1402{
1403 struct usb_device *udev = interface_to_usbdev(intf);
1404 char manufacturer[sizeof("Afatech")];
1405
1406 memset(manufacturer, 0, sizeof(manufacturer));
1407 usb_string(udev, udev->descriptor.iManufacturer,
1408 manufacturer, sizeof(manufacturer));
1409 /*
1410 * There is two devices having same ID but different chipset. One uses
1411 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1412 * is iManufacturer string.
1413 *
1414 * idVendor 0x0ccd TerraTec Electronic GmbH
1415 * idProduct 0x0099
1416 * bcdDevice 2.00
1417 * iManufacturer 1 Afatech
1418 * iProduct 2 DVB-T 2
1419 *
1420 * idVendor 0x0ccd TerraTec Electronic GmbH
1421 * idProduct 0x0099
1422 * bcdDevice 2.00
1423 * iManufacturer 1 ITE Technologies, Inc.
1424 * iProduct 2 DVB-T TV Stick
1425 */
1426 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1427 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1428 if (!strcmp("Afatech", manufacturer)) {
1429 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1430 return -ENODEV;
1431 }
1432 }
1433
1434 return dvb_usbv2_probe(intf, id);
1435}
1436
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001437/* interface 0 is used by DVB-T receiver and
1438 interface 1 is for remote controller (HID) */
1439static const struct dvb_usb_device_properties af9035_props = {
1440 .driver_name = KBUILD_MODNAME,
1441 .owner = THIS_MODULE,
1442 .adapter_nr = adapter_nr,
1443 .size_of_priv = sizeof(struct state),
1444
1445 .generic_bulk_ctrl_endpoint = 0x02,
1446 .generic_bulk_ctrl_endpoint_response = 0x81,
1447
1448 .identify_state = af9035_identify_state,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001449 .download_firmware = af9035_download_firmware,
1450
1451 .i2c_algo = &af9035_i2c_algo,
1452 .read_config = af9035_read_config,
1453 .frontend_attach = af9035_frontend_attach,
1454 .tuner_attach = af9035_tuner_attach,
1455 .init = af9035_init,
1456 .get_rc_config = af9035_get_rc_config,
Antti Palosaaribada3422013-01-11 20:45:11 -03001457 .get_stream_config = af9035_get_stream_config,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001458
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001459 .get_adapter_count = af9035_get_adapter_count,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001460 .adapter = {
1461 {
Antti Palosaaribada3422013-01-11 20:45:11 -03001462 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1463 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1464
1465 .pid_filter_count = 32,
1466 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1467 .pid_filter = af9035_pid_filter,
1468
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001469 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1470 }, {
1471 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1472 },
1473 },
1474};
1475
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001476static const struct usb_device_id af9035_id_table[] = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001477 /* AF9035 devices */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001478 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1479 &af9035_props, "Afatech AF9035 reference design", NULL) },
1480 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1481 &af9035_props, "Afatech AF9035 reference design", NULL) },
1482 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1483 &af9035_props, "Afatech AF9035 reference design", NULL) },
1484 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1485 &af9035_props, "Afatech AF9035 reference design", NULL) },
1486 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1487 &af9035_props, "Afatech AF9035 reference design", NULL) },
1488 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1489 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1490 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1491 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1492 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1493 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1494 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1495 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1496 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1497 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1498 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1499 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001500 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1501 &af9035_props, "Asus U3100Mini Plus", NULL) },
Antti Palosaaricb9114e2013-06-03 18:42:14 -03001502 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
Fabrizio Gazzatod9b75952013-02-17 18:25:34 -03001503 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001504 /* IT9135 devices */
1505#if 0
1506 { DVB_USB_DEVICE(0x048d, 0x9135,
1507 &af9035_props, "IT9135 reference design", NULL) },
1508 { DVB_USB_DEVICE(0x048d, 0x9006,
1509 &af9035_props, "IT9135 reference design", NULL) },
1510#endif
Antti Palosaarib799b812013-01-11 16:22:07 -03001511 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1512 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1513 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001514 { }
1515};
1516MODULE_DEVICE_TABLE(usb, af9035_id_table);
1517
Antti Palosaari7f882c22012-03-30 09:10:08 -03001518static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001519 .name = KBUILD_MODNAME,
1520 .id_table = af9035_id_table,
Antti Palosaarib799b812013-01-11 16:22:07 -03001521 .probe = af9035_probe,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001522 .disconnect = dvb_usbv2_disconnect,
1523 .suspend = dvb_usbv2_suspend,
1524 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001525 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001526 .no_dynamic_id = 1,
1527 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001528};
1529
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001530module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001531
1532MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1533MODULE_DESCRIPTION("Afatech AF9035 driver");
1534MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001535MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
Antti Palosaari74c18832013-01-07 15:16:54 -03001536MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1537MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);