blob: e855ee6c86b573b279df01eb8e98105b68433ebc [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 */
521 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
522 if (ret < 0)
523 goto err;
524
525 if (tmp) {
526 /* 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 Palosaari431a6d42013-03-07 18:28:25 -0300643 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300644 if (ret < 0)
645 goto err;
646
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300647 state->dual_mode = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300648 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
649 state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300650
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300651 if (state->dual_mode) {
652 /* read 2nd demodulator I2C address */
Antti Palosaari431a6d42013-03-07 18:28:25 -0300653 ret = af9035_rd_reg(d,
654 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
655 &tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300656 if (ret < 0)
657 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300658
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300659 state->af9033_config[1].i2c_addr = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300660 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
661 __func__, tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300662 }
663
Antti Palosaari431a6d42013-03-07 18:28:25 -0300664 addr = state->eeprom_addr;
665
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300666 for (i = 0; i < state->dual_mode + 1; i++) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300667 /* tuner */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300668 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300669 if (ret < 0)
670 goto err;
671
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300672 if (tmp == 0x00)
673 dev_dbg(&d->udev->dev,
674 "%s: [%d]tuner not set, using default\n",
675 __func__, i);
676 else
677 state->af9033_config[i].tuner = tmp;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300678
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300679 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
680 __func__, i, state->af9033_config[i].tuner);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300681
682 switch (state->af9033_config[i].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300683 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300684 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300685 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300686 case AF9033_TUNER_TDA18218:
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300687 case AF9033_TUNER_FC2580:
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300688 case AF9033_TUNER_FC0012:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300689 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300690 break;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300691 case AF9033_TUNER_IT9135_38:
692 case AF9033_TUNER_IT9135_51:
693 case AF9033_TUNER_IT9135_52:
694 case AF9033_TUNER_IT9135_60:
695 case AF9033_TUNER_IT9135_61:
696 case AF9033_TUNER_IT9135_62:
697 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300698 default:
Antti Palosaari9ea36812013-01-11 22:16:25 -0300699 dev_warn(&d->udev->dev,
700 "%s: tuner id=%02x not supported, please report!",
Antti Palosaari119f7a82012-09-12 20:23:53 -0300701 KBUILD_MODNAME, tmp);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300702 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300703
Antti Palosaaribf97b632012-12-08 22:51:19 -0300704 /* disable dual mode if driver does not support it */
705 if (i == 1)
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300706 switch (state->af9033_config[i].tuner) {
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300707 case AF9033_TUNER_FC0012:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300708 case AF9033_TUNER_IT9135_38:
709 case AF9033_TUNER_IT9135_51:
710 case AF9033_TUNER_IT9135_52:
711 case AF9033_TUNER_IT9135_60:
712 case AF9033_TUNER_IT9135_61:
713 case AF9033_TUNER_IT9135_62:
Jose Alberto Reguero78c7bc42013-02-10 15:43:03 -0300714 case AF9033_TUNER_MXL5007T:
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300715 break;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300716 default:
717 state->dual_mode = false;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300718 dev_info(&d->udev->dev,
719 "%s: driver does not support 2nd tuner and will disable it",
720 KBUILD_MODNAME);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300721 }
722
Antti Palosaari7f882c22012-03-30 09:10:08 -0300723 /* tuner IF frequency */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300724 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300725 if (ret < 0)
726 goto err;
727
728 tmp16 = tmp;
729
Antti Palosaari9ea36812013-01-11 22:16:25 -0300730 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300731 if (ret < 0)
732 goto err;
733
734 tmp16 |= tmp << 8;
735
Antti Palosaari119f7a82012-09-12 20:23:53 -0300736 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300737
Antti Palosaari9ea36812013-01-11 22:16:25 -0300738 addr += 0x10; /* shift for the 2nd tuner params */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300739 }
740
Antti Palosaari9ea36812013-01-11 22:16:25 -0300741skip_eeprom:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300742 /* get demod clock */
743 ret = af9035_rd_reg(d, 0x00d800, &tmp);
744 if (ret < 0)
745 goto err;
746
747 tmp = (tmp >> 0) & 0x0f;
748
Antti Palosaari9ea36812013-01-11 22:16:25 -0300749 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
750 if (state->chip_type == 0x9135)
751 state->af9033_config[i].clock = clock_lut_it9135[tmp];
752 else
753 state->af9033_config[i].clock = clock_lut_af9035[tmp];
Antti Palosaari74c18832013-01-07 15:16:54 -0300754 }
755
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300756 return 0;
757
758err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300759 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300760
761 return ret;
762}
763
Antti Palosaari51639be2012-09-16 22:26:56 -0300764static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
765 int cmd, int arg)
766{
767 int ret;
768 u8 val;
769
770 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
771
772 /*
773 * CEN always enabled by hardware wiring
774 * RESETN GPIOT3
775 * RXEN GPIOT2
776 */
777
778 switch (cmd) {
779 case TUA9001_CMD_RESETN:
780 if (arg)
781 val = 0x00;
782 else
783 val = 0x01;
784
785 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
786 if (ret < 0)
787 goto err;
788 break;
789 case TUA9001_CMD_RXEN:
790 if (arg)
791 val = 0x01;
792 else
793 val = 0x00;
794
795 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
796 if (ret < 0)
797 goto err;
798 break;
799 }
800
801 return 0;
802
803err:
804 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
805
806 return ret;
807}
808
809
Michael Büschffc501f2012-04-02 12:18:36 -0300810static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300811 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300812{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300813 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300814
815 switch (cmd) {
816 case FC0011_FE_CALLBACK_POWER:
817 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300818 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
819 if (ret < 0)
820 goto err;
821
822 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
823 if (ret < 0)
824 goto err;
825
826 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
827 if (ret < 0)
828 goto err;
829
Michael Büschffc501f2012-04-02 12:18:36 -0300830 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300831 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
832 if (ret < 0)
833 goto err;
834
835 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
836 if (ret < 0)
837 goto err;
838
Michael Büschde2bec52012-04-03 05:11:30 -0300839 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300840 break;
841 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300842 ret = af9035_wr_reg(d, 0xd8e9, 1);
843 if (ret < 0)
844 goto err;
845
846 ret = af9035_wr_reg(d, 0xd8e8, 1);
847 if (ret < 0)
848 goto err;
849
850 ret = af9035_wr_reg(d, 0xd8e7, 1);
851 if (ret < 0)
852 goto err;
853
Michael Büschde2bec52012-04-03 05:11:30 -0300854 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300855
856 ret = af9035_wr_reg(d, 0xd8e7, 0);
857 if (ret < 0)
858 goto err;
859
Michael Büschde2bec52012-04-03 05:11:30 -0300860 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300861 break;
862 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300863 ret = -EINVAL;
864 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300865 }
866
867 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300868
869err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300870 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300871
872 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300873}
874
875static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
876{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300877 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300878
879 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300880 case AF9033_TUNER_FC0011:
881 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300882 case AF9033_TUNER_TUA9001:
883 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300884 default:
885 break;
886 }
887
Antti Palosaari1835af12012-09-11 22:27:06 -0300888 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300889}
890
891static int af9035_frontend_callback(void *adapter_priv, int component,
892 int cmd, int arg)
893{
894 struct i2c_adapter *adap = adapter_priv;
895 struct dvb_usb_device *d = i2c_get_adapdata(adap);
896
Antti Palosaari1835af12012-09-11 22:27:06 -0300897 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
898 __func__, component, cmd, arg);
899
Michael Büschffc501f2012-04-02 12:18:36 -0300900 switch (component) {
901 case DVB_FRONTEND_COMPONENT_TUNER:
902 return af9035_tuner_callback(d, cmd, arg);
903 default:
904 break;
905 }
906
Antti Palosaari1835af12012-09-11 22:27:06 -0300907 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300908}
909
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300910static int af9035_get_adapter_count(struct dvb_usb_device *d)
911{
912 struct state *state = d_to_priv(d);
Antti Palosaaribada3422013-01-11 20:45:11 -0300913
914 /* disable 2nd adapter as we don't have PID filters implemented */
915 if (d->udev->speed == USB_SPEED_FULL)
916 return 1;
917 else
918 return state->dual_mode + 1;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300919}
920
Antti Palosaari7f882c22012-03-30 09:10:08 -0300921static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
922{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300923 struct state *state = adap_to_priv(adap);
924 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300925 int ret;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300926 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300927
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300928 if (!state->af9033_config[adap->id].tuner) {
929 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300930 ret = -ENODEV;
931 goto err;
932 }
933
Antti Palosaari7f882c22012-03-30 09:10:08 -0300934 /* attach demodulator */
Antti Palosaaribf97b632012-12-08 22:51:19 -0300935 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
936 &d->i2c_adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300937 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300938 ret = -ENODEV;
939 goto err;
940 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300941
942 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300943 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
944 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300945
946 return 0;
947
948err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300949 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300950
951 return ret;
952}
953
954static struct tua9001_config af9035_tua9001_config = {
955 .i2c_addr = 0x60,
956};
957
Michael Büschffc501f2012-04-02 12:18:36 -0300958static const struct fc0011_config af9035_fc0011_config = {
959 .i2c_address = 0x60,
960};
961
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300962static struct mxl5007t_config af9035_mxl5007t_config[] = {
963 {
964 .xtal_freq_hz = MxL_XTAL_24_MHZ,
965 .if_freq_hz = MxL_IF_4_57_MHZ,
966 .invert_if = 0,
967 .loop_thru_enable = 0,
968 .clk_out_enable = 0,
969 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
970 }, {
971 .xtal_freq_hz = MxL_XTAL_24_MHZ,
972 .if_freq_hz = MxL_IF_4_57_MHZ,
973 .invert_if = 0,
974 .loop_thru_enable = 1,
975 .clk_out_enable = 1,
976 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
977 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300978};
979
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300980static struct tda18218_config af9035_tda18218_config = {
981 .i2c_address = 0x60,
982 .i2c_wr_max = 21,
983};
984
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300985static const struct fc2580_config af9035_fc2580_config = {
986 .i2c_addr = 0x56,
987 .clock = 16384000,
988};
989
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300990static const struct fc0012_config af9035_fc0012_config[] = {
991 {
992 .i2c_address = 0x63,
993 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300994 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300995 .loop_through = true,
996 .clock_out = true,
997 }, {
998 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
999 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -03001000 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001001 }
Antti Palosaariad3a7582012-12-08 23:27:49 -03001002};
1003
Antti Palosaari7f882c22012-03-30 09:10:08 -03001004static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1005{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001006 struct state *state = adap_to_priv(adap);
1007 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001008 int ret;
1009 struct dvb_frontend *fe;
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001010 struct i2c_msg msg[1];
Antti Palosaaribf97b632012-12-08 22:51:19 -03001011 u8 tuner_addr;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001012 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1013
Antti Palosaaribf97b632012-12-08 22:51:19 -03001014 /*
1015 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1016 * to carry info about used I2C bus for dual tuner configuration.
1017 */
Antti Palosaari7f882c22012-03-30 09:10:08 -03001018
Antti Palosaari2a79eef2012-05-07 14:50:40 -03001019 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -03001020 case AF9033_TUNER_TUA9001:
1021 /* AF9035 gpiot3 = TUA9001 RESETN
1022 AF9035 gpiot2 = TUA9001 RXEN */
1023
1024 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001025 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001026 if (ret < 0)
1027 goto err;
1028
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001029 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001030 if (ret < 0)
1031 goto err;
1032
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001033 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001034 if (ret < 0)
1035 goto err;
1036
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001037 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001038 if (ret < 0)
1039 goto err;
1040
Antti Palosaari7f882c22012-03-30 09:10:08 -03001041 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001042 fe = dvb_attach(tua9001_attach, adap->fe[0],
1043 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001044 break;
Michael Büschffc501f2012-04-02 12:18:36 -03001045 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001046 fe = dvb_attach(fc0011_attach, adap->fe[0],
1047 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -03001048 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001049 case AF9033_TUNER_MXL5007T:
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001050 if (adap->id == 0) {
1051 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1052 if (ret < 0)
1053 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001054
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001055 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1056 if (ret < 0)
1057 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001058
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001059 ret = af9035_wr_reg(d, 0x00d8df, 0);
1060 if (ret < 0)
1061 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001062
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001063 msleep(30);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001064
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001065 ret = af9035_wr_reg(d, 0x00d8df, 1);
1066 if (ret < 0)
1067 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001068
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001069 msleep(300);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001070
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001071 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1072 if (ret < 0)
1073 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001074
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001075 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1076 if (ret < 0)
1077 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001078
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001079 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1080 if (ret < 0)
1081 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001082
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001083 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1084 if (ret < 0)
1085 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001086
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001087 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1088 if (ret < 0)
1089 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001090
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001091 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1092 if (ret < 0)
1093 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001094
1095 tuner_addr = 0x60;
1096 } else {
1097 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001098 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001099
1100 /* attach tuner */
Antti Palosaaribf97b632012-12-08 22:51:19 -03001101 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1102 tuner_addr, &af9035_mxl5007t_config[adap->id]);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001103 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001104 case AF9033_TUNER_TDA18218:
1105 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001106 fe = dvb_attach(tda18218_attach, adap->fe[0],
1107 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001108 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001109 case AF9033_TUNER_FC2580:
1110 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1111 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1112 if (ret < 0)
1113 goto err;
1114
1115 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1116 if (ret < 0)
1117 goto err;
1118
1119 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1120 if (ret < 0)
1121 goto err;
1122
1123 usleep_range(10000, 50000);
1124 /* attach tuner */
1125 fe = dvb_attach(fc2580_attach, adap->fe[0],
1126 &d->i2c_adap, &af9035_fc2580_config);
1127 break;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001128 case AF9033_TUNER_FC0012:
1129 /*
1130 * AF9035 gpiot2 = FC0012 enable
1131 * XXX: there seems to be something on gpioh8 too, but on my
1132 * my test I didn't find any difference.
1133 */
1134
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001135 if (adap->id == 0) {
1136 /* configure gpiot2 as output and high */
1137 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1138 if (ret < 0)
1139 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001140
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001141 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1142 if (ret < 0)
1143 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001144
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001145 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1146 if (ret < 0)
1147 goto err;
1148 } else {
1149 /*
1150 * FIXME: That belongs for the FC0012 driver.
1151 * Write 02 to FC0012 master tuner register 0d directly
1152 * in order to make slave tuner working.
1153 */
1154 msg[0].addr = 0x63;
1155 msg[0].flags = 0;
1156 msg[0].len = 2;
1157 msg[0].buf = "\x0d\x02";
1158 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1159 if (ret < 0)
1160 goto err;
1161 }
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001162
1163 usleep_range(10000, 50000);
1164
Antti Palosaariad3a7582012-12-08 23:27:49 -03001165 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001166 &af9035_fc0012_config[adap->id]);
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001167 break;
Antti Palosaariac77fb02013-01-07 09:51:13 -03001168 case AF9033_TUNER_IT9135_38:
Antti Palosaari74c18832013-01-07 15:16:54 -03001169 case AF9033_TUNER_IT9135_51:
1170 case AF9033_TUNER_IT9135_52:
1171 case AF9033_TUNER_IT9135_60:
1172 case AF9033_TUNER_IT9135_61:
1173 case AF9033_TUNER_IT9135_62:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001174 /* attach tuner */
1175 fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1176 state->af9033_config[adap->id].i2c_addr,
Antti Palosaari44af7472013-02-28 00:14:06 -03001177 state->af9033_config[0].tuner);
Antti Palosaariac77fb02013-01-07 09:51:13 -03001178 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -03001179 default:
1180 fe = NULL;
1181 }
1182
1183 if (fe == NULL) {
1184 ret = -ENODEV;
1185 goto err;
1186 }
1187
1188 return 0;
1189
1190err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001191 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001192
1193 return ret;
1194}
1195
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001196static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -03001197{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001198 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001199 int ret, i;
Antti Palosaaribada3422013-01-11 20:45:11 -03001200 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1201 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001202 struct reg_val_mask tab[] = {
1203 { 0x80f99d, 0x01, 0x01 },
1204 { 0x80f9a4, 0x01, 0x01 },
1205 { 0x00dd11, 0x00, 0x20 },
1206 { 0x00dd11, 0x00, 0x40 },
1207 { 0x00dd13, 0x00, 0x20 },
1208 { 0x00dd13, 0x00, 0x40 },
1209 { 0x00dd11, 0x20, 0x20 },
1210 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1211 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1212 { 0x00dd0c, packet_size, 0xff},
1213 { 0x00dd11, state->dual_mode << 6, 0x40 },
1214 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1215 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1216 { 0x00dd0d, packet_size, 0xff },
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001217 { 0x80f9a3, state->dual_mode, 0x01 },
1218 { 0x80f9cd, state->dual_mode, 0x01 },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001219 { 0x80f99d, 0x00, 0x01 },
1220 { 0x80f9a4, 0x00, 0x01 },
1221 };
Antti Palosaari7f882c22012-03-30 09:10:08 -03001222
Antti Palosaaricb9114e2013-06-03 18:42:14 -03001223 dev_dbg(&d->udev->dev,
1224 "%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1225 __func__, d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001226
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001227 /* init endpoints */
1228 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1229 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1230 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001231 if (ret < 0)
1232 goto err;
1233 }
1234
1235 return 0;
1236
1237err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001238 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001239
1240 return ret;
1241}
1242
Antti Palosaari37b44a02013-01-04 15:21:26 -03001243#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001244static int af9035_rc_query(struct dvb_usb_device *d)
1245{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001246 int ret;
Antti Palosaari75cd5882013-03-08 17:50:21 -03001247 u32 key;
1248 u8 buf[4];
1249 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001250
1251 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001252 if (ret == 1)
1253 return 0;
1254 else if (ret < 0)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001255 goto err;
1256
Antti Palosaari75cd5882013-03-08 17:50:21 -03001257 if ((buf[2] + buf[3]) == 0xff) {
1258 if ((buf[0] + buf[1]) == 0xff) {
1259 /* NEC standard 16bit */
1260 key = buf[0] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001261 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001262 /* NEC extended 24bit */
1263 key = buf[0] << 16 | buf[1] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001264 }
1265 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001266 /* NEC full code 32bit */
1267 key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001268 }
1269
Antti Palosaari75cd5882013-03-08 17:50:21 -03001270 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1271
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001272 rc_keydown(d->rc_dev, key, 0);
1273
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001274 return 0;
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001275
1276err:
1277 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1278
1279 return ret;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001280}
1281
1282static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1283{
Antti Palosaari74c18832013-01-07 15:16:54 -03001284 struct state *state = d_to_priv(d);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001285 int ret;
1286 u8 tmp;
1287
Antti Palosaari431a6d42013-03-07 18:28:25 -03001288 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001289 if (ret < 0)
1290 goto err;
1291
Antti Palosaari119f7a82012-09-12 20:23:53 -03001292 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001293
1294 /* don't activate rc if in HID mode or if not available */
1295 if (tmp == 5) {
Antti Palosaari431a6d42013-03-07 18:28:25 -03001296 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
Antti Palosaari9ea36812013-01-11 22:16:25 -03001297 &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001298 if (ret < 0)
1299 goto err;
1300
Antti Palosaari119f7a82012-09-12 20:23:53 -03001301 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001302
1303 switch (tmp) {
1304 case 0: /* NEC */
1305 default:
David Härdemanc003ab12012-10-11 19:11:54 -03001306 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001307 break;
1308 case 1: /* RC6 */
David Härdemanc003ab12012-10-11 19:11:54 -03001309 rc->allowed_protos = RC_BIT_RC6_MCE;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001310 break;
1311 }
1312
1313 rc->query = af9035_rc_query;
1314 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001315
1316 /* load empty to enable rc */
1317 if (!rc->map_name)
1318 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001319 }
1320
1321 return 0;
1322
1323err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001324 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001325
1326 return ret;
1327}
Antti Palosaarieed56702012-12-09 20:18:31 -03001328#else
1329 #define af9035_get_rc_config NULL
1330#endif
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001331
Antti Palosaaribada3422013-01-11 20:45:11 -03001332static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1333 struct usb_data_stream_properties *stream)
1334{
1335 struct dvb_usb_device *d = fe_to_d(fe);
1336 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1337
1338 if (d->udev->speed == USB_SPEED_FULL)
1339 stream->u.bulk.buffersize = 5 * 188;
1340
1341 return 0;
1342}
1343
1344/*
1345 * FIXME: PID filter is property of demodulator and should be moved to the
1346 * correct driver. Also we support only adapter #0 PID filter and will
1347 * disable adapter #1 if USB1.1 is used.
1348 */
1349static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1350{
1351 struct dvb_usb_device *d = adap_to_d(adap);
1352 int ret;
1353
1354 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1355
1356 ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1357 if (ret < 0)
1358 goto err;
1359
1360 return 0;
1361
1362err:
1363 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1364
1365 return ret;
1366}
1367
1368static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1369 int onoff)
1370{
1371 struct dvb_usb_device *d = adap_to_d(adap);
1372 int ret;
1373 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1374
1375 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1376 __func__, index, pid, onoff);
1377
1378 ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1379 if (ret < 0)
1380 goto err;
1381
1382 ret = af9035_wr_reg(d, 0x80f994, onoff);
1383 if (ret < 0)
1384 goto err;
1385
1386 ret = af9035_wr_reg(d, 0x80f995, index);
1387 if (ret < 0)
1388 goto err;
1389
1390 return 0;
1391
1392err:
1393 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1394
1395 return ret;
1396}
1397
Antti Palosaarib799b812013-01-11 16:22:07 -03001398static int af9035_probe(struct usb_interface *intf,
1399 const struct usb_device_id *id)
1400{
1401 struct usb_device *udev = interface_to_usbdev(intf);
1402 char manufacturer[sizeof("Afatech")];
1403
1404 memset(manufacturer, 0, sizeof(manufacturer));
1405 usb_string(udev, udev->descriptor.iManufacturer,
1406 manufacturer, sizeof(manufacturer));
1407 /*
1408 * There is two devices having same ID but different chipset. One uses
1409 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1410 * is iManufacturer string.
1411 *
1412 * idVendor 0x0ccd TerraTec Electronic GmbH
1413 * idProduct 0x0099
1414 * bcdDevice 2.00
1415 * iManufacturer 1 Afatech
1416 * iProduct 2 DVB-T 2
1417 *
1418 * idVendor 0x0ccd TerraTec Electronic GmbH
1419 * idProduct 0x0099
1420 * bcdDevice 2.00
1421 * iManufacturer 1 ITE Technologies, Inc.
1422 * iProduct 2 DVB-T TV Stick
1423 */
1424 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1425 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1426 if (!strcmp("Afatech", manufacturer)) {
1427 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1428 return -ENODEV;
1429 }
1430 }
1431
1432 return dvb_usbv2_probe(intf, id);
1433}
1434
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001435/* interface 0 is used by DVB-T receiver and
1436 interface 1 is for remote controller (HID) */
1437static const struct dvb_usb_device_properties af9035_props = {
1438 .driver_name = KBUILD_MODNAME,
1439 .owner = THIS_MODULE,
1440 .adapter_nr = adapter_nr,
1441 .size_of_priv = sizeof(struct state),
1442
1443 .generic_bulk_ctrl_endpoint = 0x02,
1444 .generic_bulk_ctrl_endpoint_response = 0x81,
1445
1446 .identify_state = af9035_identify_state,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001447 .download_firmware = af9035_download_firmware,
1448
1449 .i2c_algo = &af9035_i2c_algo,
1450 .read_config = af9035_read_config,
1451 .frontend_attach = af9035_frontend_attach,
1452 .tuner_attach = af9035_tuner_attach,
1453 .init = af9035_init,
1454 .get_rc_config = af9035_get_rc_config,
Antti Palosaaribada3422013-01-11 20:45:11 -03001455 .get_stream_config = af9035_get_stream_config,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001456
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001457 .get_adapter_count = af9035_get_adapter_count,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001458 .adapter = {
1459 {
Antti Palosaaribada3422013-01-11 20:45:11 -03001460 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1461 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1462
1463 .pid_filter_count = 32,
1464 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1465 .pid_filter = af9035_pid_filter,
1466
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001467 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1468 }, {
1469 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1470 },
1471 },
1472};
1473
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001474static const struct usb_device_id af9035_id_table[] = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001475 /* AF9035 devices */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001476 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1477 &af9035_props, "Afatech AF9035 reference design", NULL) },
1478 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1479 &af9035_props, "Afatech AF9035 reference design", NULL) },
1480 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1481 &af9035_props, "Afatech AF9035 reference design", NULL) },
1482 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1483 &af9035_props, "Afatech AF9035 reference design", NULL) },
1484 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1485 &af9035_props, "Afatech AF9035 reference design", NULL) },
1486 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1487 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1488 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1489 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1490 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1491 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1492 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1493 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1494 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1495 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1496 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1497 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001498 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1499 &af9035_props, "Asus U3100Mini Plus", NULL) },
Antti Palosaaricb9114e2013-06-03 18:42:14 -03001500 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
Fabrizio Gazzatod9b75952013-02-17 18:25:34 -03001501 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001502 /* IT9135 devices */
1503#if 0
1504 { DVB_USB_DEVICE(0x048d, 0x9135,
1505 &af9035_props, "IT9135 reference design", NULL) },
1506 { DVB_USB_DEVICE(0x048d, 0x9006,
1507 &af9035_props, "IT9135 reference design", NULL) },
1508#endif
Antti Palosaarib799b812013-01-11 16:22:07 -03001509 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1510 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1511 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001512 { }
1513};
1514MODULE_DEVICE_TABLE(usb, af9035_id_table);
1515
Antti Palosaari7f882c22012-03-30 09:10:08 -03001516static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001517 .name = KBUILD_MODNAME,
1518 .id_table = af9035_id_table,
Antti Palosaarib799b812013-01-11 16:22:07 -03001519 .probe = af9035_probe,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001520 .disconnect = dvb_usbv2_disconnect,
1521 .suspend = dvb_usbv2_suspend,
1522 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001523 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001524 .no_dynamic_id = 1,
1525 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001526};
1527
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001528module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001529
1530MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1531MODULE_DESCRIPTION("Afatech AF9035 driver");
1532MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001533MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
Antti Palosaari74c18832013-01-07 15:16:54 -03001534MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1535MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);