blob: 5c025f657144d9e9a879fa0f2b5a7d22439b3495 [file] [log] [blame]
Antti Palosaari7f882c22012-03-30 09:10:08 -03001/*
2 * Afatech AF9035 DVB USB driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "af9035.h"
Antti Palosaari7f882c22012-03-30 09:10:08 -030023
24DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
Antti Palosaari7f882c22012-03-30 09:10:08 -030025
Michael Büschb1a95992012-04-01 16:33:48 -030026static u16 af9035_checksum(const u8 *buf, size_t len)
27{
28 size_t i;
29 u16 checksum = 0;
30
31 for (i = 1; i < len; i++) {
32 if (i % 2)
33 checksum += buf[i] << 8;
34 else
35 checksum += buf[i];
36 }
37 checksum = ~checksum;
38
39 return checksum;
40}
41
Antti Palosaari5da2aec2012-06-17 23:15:03 -030042static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
Antti Palosaari7f882c22012-03-30 09:10:08 -030043{
Antti Palosaari7f882c22012-03-30 09:10:08 -030044#define REQ_HDR_LEN 4 /* send header size */
45#define ACK_HDR_LEN 3 /* rece header size */
46#define CHECKSUM_LEN 2
47#define USB_TIMEOUT 2000
Antti Palosaari5da2aec2012-06-17 23:15:03 -030048 struct state *state = d_to_priv(d);
49 int ret, wlen, rlen;
Antti Palosaari6fb39c52012-04-06 16:32:30 -030050 u16 checksum, tmp_checksum;
Antti Palosaari7f882c22012-03-30 09:10:08 -030051
Antti Palosaari3484d372013-02-26 13:56:34 -030052 mutex_lock(&d->usb_mutex);
53
Antti Palosaari7f882c22012-03-30 09:10:08 -030054 /* buffer overflow check */
55 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
Antti Palosaari119f7a82012-09-12 20:23:53 -030056 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
57 dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
58 __func__, req->wlen, req->rlen);
Antti Palosaari3484d372013-02-26 13:56:34 -030059 ret = -EINVAL;
60 goto err;
Antti Palosaari7f882c22012-03-30 09:10:08 -030061 }
62
Antti Palosaari3484d372013-02-26 13:56:34 -030063 state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
64 state->buf[1] = req->mbox;
65 state->buf[2] = req->cmd;
66 state->buf[3] = state->seq++;
67 memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -030068
69 wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
70 rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
Antti Palosaari7f882c22012-03-30 09:10:08 -030071
72 /* calc and add checksum */
Antti Palosaari3484d372013-02-26 13:56:34 -030073 checksum = af9035_checksum(state->buf, state->buf[0] - 1);
74 state->buf[state->buf[0] - 1] = (checksum >> 8);
75 state->buf[state->buf[0] - 0] = (checksum & 0xff);
Antti Palosaari7f882c22012-03-30 09:10:08 -030076
Antti Palosaari5da2aec2012-06-17 23:15:03 -030077 /* no ack for these packets */
78 if (req->cmd == CMD_FW_DL)
79 rlen = 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -030080
Antti Palosaari3484d372013-02-26 13:56:34 -030081 ret = dvb_usbv2_generic_rw_locked(d,
82 state->buf, wlen, state->buf, rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -030083 if (ret)
84 goto err;
Antti Palosaari7f882c22012-03-30 09:10:08 -030085
86 /* no ack for those packets */
87 if (req->cmd == CMD_FW_DL)
Antti Palosaari5da2aec2012-06-17 23:15:03 -030088 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -030089
Michael Büschb1a95992012-04-01 16:33:48 -030090 /* verify checksum */
Antti Palosaari3484d372013-02-26 13:56:34 -030091 checksum = af9035_checksum(state->buf, rlen - 2);
92 tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
Antti Palosaari6fb39c52012-04-06 16:32:30 -030093 if (tmp_checksum != checksum) {
Antti Palosaari119f7a82012-09-12 20:23:53 -030094 dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \
95 "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd,
96 tmp_checksum, checksum);
Michael Büschb1a95992012-04-01 16:33:48 -030097 ret = -EIO;
Antti Palosaari5da2aec2012-06-17 23:15:03 -030098 goto err;
Michael Büschb1a95992012-04-01 16:33:48 -030099 }
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300100
Antti Palosaari7f882c22012-03-30 09:10:08 -0300101 /* check status */
Antti Palosaari3484d372013-02-26 13:56:34 -0300102 if (state->buf[2]) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300103 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
Antti Palosaari3484d372013-02-26 13:56:34 -0300104 __func__, req->cmd, state->buf[2]);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300105 ret = -EIO;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300106 goto err;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300107 }
108
109 /* read request, copy returned data to return buf */
110 if (req->rlen)
Antti Palosaari3484d372013-02-26 13:56:34 -0300111 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300112exit:
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300113err:
Antti Palosaari3484d372013-02-26 13:56:34 -0300114 mutex_unlock(&d->usb_mutex);
115 if (ret)
116 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300117 return ret;
118}
119
120/* write multiple registers */
121static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
122{
123 u8 wbuf[6 + len];
124 u8 mbox = (reg >> 16) & 0xff;
125 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
126
127 wbuf[0] = len;
128 wbuf[1] = 2;
129 wbuf[2] = 0;
130 wbuf[3] = 0;
131 wbuf[4] = (reg >> 8) & 0xff;
132 wbuf[5] = (reg >> 0) & 0xff;
133 memcpy(&wbuf[6], val, len);
134
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300135 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300136}
137
138/* read multiple registers */
139static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
140{
141 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
142 u8 mbox = (reg >> 16) & 0xff;
143 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
144
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300145 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300146}
147
148/* write single register */
149static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
150{
151 return af9035_wr_regs(d, reg, &val, 1);
152}
153
154/* read single register */
155static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
156{
157 return af9035_rd_regs(d, reg, val, 1);
158}
159
160/* write single register with mask */
161static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
162 u8 mask)
163{
164 int ret;
165 u8 tmp;
166
167 /* no need for read if whole reg is written */
168 if (mask != 0xff) {
169 ret = af9035_rd_regs(d, reg, &tmp, 1);
170 if (ret)
171 return ret;
172
173 val &= mask;
174 tmp &= ~mask;
175 val |= tmp;
176 }
177
178 return af9035_wr_regs(d, reg, &val, 1);
179}
180
181static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
182 struct i2c_msg msg[], int num)
183{
184 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300185 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300186 int ret;
187
188 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
189 return -EAGAIN;
190
Antti Palosaariad30e912012-04-02 20:18:59 -0300191 /*
192 * I2C sub header is 5 bytes long. Meaning of those bytes are:
193 * 0: data len
194 * 1: I2C addr << 1
195 * 2: reg addr len
196 * byte 3 and 4 can be used as reg addr
197 * 3: reg addr MSB
198 * used when reg addr len is set to 2
199 * 4: reg addr LSB
200 * used when reg addr len is set to 1 or 2
201 *
202 * For the simplify we do not use register addr at all.
203 * NOTE: As a firmware knows tuner type there is very small possibility
204 * there could be some tuner I2C hacks done by firmware and this may
205 * lead problems if firmware expects those bytes are used.
206 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300207 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
208 (msg[1].flags & I2C_M_RD)) {
209 if (msg[0].len > 40 || msg[1].len > 40) {
210 /* TODO: correct limits > 40 */
211 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300212 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
213 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300214 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300215 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
216 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300217
218 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300219 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300220
Antti Palosaari7f882c22012-03-30 09:10:08 -0300221 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
222 msg[1].len);
223 } else {
224 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300225 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300226 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
227 buf, msg[1].len, msg[1].buf };
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300228 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300229 buf[0] = msg[1].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300230 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300231 buf[2] = 0x00; /* reg addr len */
232 buf[3] = 0x00; /* reg addr MSB */
233 buf[4] = 0x00; /* reg addr LSB */
234 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300235 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300236 }
237 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
238 if (msg[0].len > 40) {
239 /* TODO: correct limits > 40 */
240 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300241 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
242 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300243 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300244 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
245 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300246
247 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300248 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300249
Antti Palosaari7f882c22012-03-30 09:10:08 -0300250 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
251 msg[0].len - 3);
252 } else {
253 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300254 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300255 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
256 0, NULL };
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300257 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300258 buf[0] = msg[0].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300259 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300260 buf[2] = 0x00; /* reg addr len */
261 buf[3] = 0x00; /* reg addr MSB */
262 buf[4] = 0x00; /* reg addr LSB */
263 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300264 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300265 }
266 } else {
267 /*
268 * We support only two kind of I2C transactions:
269 * 1) 1 x read + 1 x write
270 * 2) 1 x write
271 */
272 ret = -EOPNOTSUPP;
273 }
274
275 mutex_unlock(&d->i2c_mutex);
276
277 if (ret < 0)
278 return ret;
279 else
280 return num;
281}
282
283static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
284{
285 return I2C_FUNC_I2C;
286}
287
288static struct i2c_algorithm af9035_i2c_algo = {
289 .master_xfer = af9035_i2c_master_xfer,
290 .functionality = af9035_i2c_functionality,
291};
292
Antti Palosaaria0921af2012-06-18 23:42:53 -0300293static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300294{
Antti Palosaari74c18832013-01-07 15:16:54 -0300295 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300296 int ret;
297 u8 wbuf[1] = { 1 };
298 u8 rbuf[4];
299 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
300 sizeof(rbuf), rbuf };
301
Antti Palosaari74c18832013-01-07 15:16:54 -0300302 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
303 if (ret < 0)
304 goto err;
305
306 state->chip_version = rbuf[0];
307 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
308
309 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
310 if (ret < 0)
311 goto err;
312
313 dev_info(&d->udev->dev,
314 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
315 __func__, state->prechip_version, state->chip_version,
316 state->chip_type);
317
318 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300319 if (state->chip_version == 0x02)
Antti Palosaari74c18832013-01-07 15:16:54 -0300320 *name = AF9035_FIRMWARE_IT9135_V2;
321 else
322 *name = AF9035_FIRMWARE_IT9135_V1;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300323 state->eeprom_addr = EEPROM_BASE_IT9135;
Antti Palosaari74c18832013-01-07 15:16:54 -0300324 } else {
325 *name = AF9035_FIRMWARE_AF9035;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300326 state->eeprom_addr = EEPROM_BASE_AF9035;
Antti Palosaari74c18832013-01-07 15:16:54 -0300327 }
328
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300329 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300330 if (ret < 0)
331 goto err;
332
Antti Palosaari119f7a82012-09-12 20:23:53 -0300333 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300334 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300335 ret = WARM;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300336 else
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300337 ret = COLD;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300338
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300339 return ret;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300340
341err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300342 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300343
344 return ret;
345}
346
Antti Palosaari74c18832013-01-07 15:16:54 -0300347static int af9035_download_firmware_af9035(struct dvb_usb_device *d,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300348 const struct firmware *fw)
349{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300350 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300351 u8 wbuf[1];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300352 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
353 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300354 u8 hdr_core;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300355 u16 hdr_addr, hdr_data_len, hdr_checksum;
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300356 #define MAX_DATA 58
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300357 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300358
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300359 /*
360 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
361 *
362 * byte 0: MCS 51 core
363 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
364 * address spaces
365 * byte 1-2: Big endian destination address
366 * byte 3-4: Big endian number of data bytes following the header
367 * byte 5-6: Big endian header checksum, apparently ignored by the chip
368 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
369 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300370
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300371 for (i = fw->size; i > HDR_SIZE;) {
372 hdr_core = fw->data[fw->size - i + 0];
373 hdr_addr = fw->data[fw->size - i + 1] << 8;
374 hdr_addr |= fw->data[fw->size - i + 2] << 0;
375 hdr_data_len = fw->data[fw->size - i + 3] << 8;
376 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
377 hdr_checksum = fw->data[fw->size - i + 5] << 8;
378 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300379
Antti Palosaari119f7a82012-09-12 20:23:53 -0300380 dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
381 "checksum=%04x\n", __func__, hdr_core, hdr_addr,
382 hdr_data_len, hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300383
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300384 if (((hdr_core != 1) && (hdr_core != 2)) ||
385 (hdr_data_len > i)) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300386 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300387 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300388 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300389
390 /* download begin packet */
391 req.cmd = CMD_FW_DL_BEGIN;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300392 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300393 if (ret < 0)
394 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300395
396 /* download firmware packet(s) */
397 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
398 len = j;
399 if (len > MAX_DATA)
400 len = MAX_DATA;
401 req_fw_dl.wlen = len;
402 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
403 HDR_SIZE + hdr_data_len - j];
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300404 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300405 if (ret < 0)
406 goto err;
407 }
408
409 /* download end packet */
410 req.cmd = CMD_FW_DL_END;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300411 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300412 if (ret < 0)
413 goto err;
414
415 i -= hdr_data_len + HDR_SIZE;
416
Antti Palosaari119f7a82012-09-12 20:23:53 -0300417 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
418 __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300419 }
420
Antti Palosaariff4e3fe2012-12-09 16:25:08 -0300421 /* print warn if firmware is bad, continue and see what happens */
422 if (i)
423 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
424
Antti Palosaari7f882c22012-03-30 09:10:08 -0300425 return 0;
426
427err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300428 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300429
430 return ret;
431}
432
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300433static int af9035_download_firmware_it9135(struct dvb_usb_device *d,
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300434 const struct firmware *fw)
435{
436 int ret, i, i_prev;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300437 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300438 #define HDR_SIZE 7
439
440 /*
441 * There seems to be following firmware header. Meaning of bytes 0-3
442 * is unknown.
443 *
444 * 0: 3
445 * 1: 0, 1
446 * 2: 0
447 * 3: 1, 2, 3
448 * 4: addr MSB
449 * 5: addr LSB
450 * 6: count of data bytes ?
451 */
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300452 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
453 if (i == fw->size ||
454 (fw->data[i + 0] == 0x03 &&
455 (fw->data[i + 1] == 0x00 ||
456 fw->data[i + 1] == 0x01) &&
457 fw->data[i + 2] == 0x00)) {
458 req_fw_dl.wlen = i - i_prev;
459 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
460 i_prev = i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300461 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300462 if (ret < 0)
463 goto err;
464
Antti Palosaari119f7a82012-09-12 20:23:53 -0300465 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
466 __func__, i);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300467 }
468 }
469
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300470 return 0;
471
472err:
473 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
474
475 return ret;
476}
477
478static int af9035_download_firmware(struct dvb_usb_device *d,
479 const struct firmware *fw)
480{
481 struct state *state = d_to_priv(d);
482 int ret;
483 u8 wbuf[1];
484 u8 rbuf[4];
485 u8 tmp;
486 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
487 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
488 dev_dbg(&d->udev->dev, "%s:\n", __func__);
489
490 /*
491 * In case of dual tuner configuration we need to do some extra
492 * initialization in order to download firmware to slave demod too,
493 * which is done by master demod.
494 * Master feeds also clock and controls power via GPIO.
495 */
496 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
497 if (ret < 0)
498 goto err;
499
500 if (tmp) {
501 /* configure gpioh1, reset & power slave demod */
502 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
503 if (ret < 0)
504 goto err;
505
506 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
507 if (ret < 0)
508 goto err;
509
510 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
511 if (ret < 0)
512 goto err;
513
514 usleep_range(10000, 50000);
515
516 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
517 if (ret < 0)
518 goto err;
519
520 /* tell the slave I2C address */
521 ret = af9035_rd_reg(d,
522 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
523 &tmp);
524 if (ret < 0)
525 goto err;
526
527 if (state->chip_type == 0x9135) {
528 ret = af9035_wr_reg(d, 0x004bfb, tmp);
529 if (ret < 0)
530 goto err;
531 } else {
532 ret = af9035_wr_reg(d, 0x00417f, tmp);
533 if (ret < 0)
534 goto err;
535
536 /* enable clock out */
537 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
538 if (ret < 0)
539 goto err;
540 }
541 }
542
543 if (state->chip_type == 0x9135)
544 ret = af9035_download_firmware_it9135(d, fw);
545 else
546 ret = af9035_download_firmware_af9035(d, fw);
547 if (ret < 0)
548 goto err;
549
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300550 /* firmware loaded, request boot */
551 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300552 ret = af9035_ctrl_msg(d, &req);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300553 if (ret < 0)
554 goto err;
555
556 /* ensure firmware starts */
557 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300558 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300559 if (ret < 0)
560 goto err;
561
562 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300563 dev_err(&d->udev->dev, "%s: firmware did not run\n",
564 KBUILD_MODNAME);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300565 ret = -ENODEV;
566 goto err;
567 }
568
Antti Palosaari119f7a82012-09-12 20:23:53 -0300569 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
570 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300571
572 return 0;
573
574err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300575 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300576
577 return ret;
578}
579
Antti Palosaari9ea36812013-01-11 22:16:25 -0300580static int af9035_read_config(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300581{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300582 struct state *state = d_to_priv(d);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300583 int ret, i;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300584 u8 tmp;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300585 u16 tmp16, addr;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300586
Antti Palosaaribf97b632012-12-08 22:51:19 -0300587 /* demod I2C "address" */
588 state->af9033_config[0].i2c_addr = 0x38;
Antti Palosaari0d94d6a2013-01-07 15:26:05 -0300589 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300590 state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300591
Antti Palosaari9ea36812013-01-11 22:16:25 -0300592 /* eeprom memory mapped location */
593 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300594 if (state->chip_version == 0x02) {
595 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300596 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300597 tmp16 = 0x00461d;
598 } else {
599 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300600 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300601 tmp16 = 0x00461b;
602 }
603
Antti Palosaari9ea36812013-01-11 22:16:25 -0300604 /* check if eeprom exists */
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300605 ret = af9035_rd_reg(d, tmp16, &tmp);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300606 if (ret < 0)
607 goto err;
608
609 if (tmp) {
610 addr = EEPROM_BASE_IT9135;
611 } else {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300612 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300613 goto skip_eeprom;
614 }
615 } else {
616 addr = EEPROM_BASE_AF9035;
617 }
618
Antti Palosaari7f882c22012-03-30 09:10:08 -0300619 /* check if there is dual tuners */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300620 ret = af9035_rd_reg(d, addr + EEPROM_DUAL_MODE, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300621 if (ret < 0)
622 goto err;
623
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300624 state->dual_mode = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300625 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
626 state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300627
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300628 if (state->dual_mode) {
629 /* read 2nd demodulator I2C address */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300630 ret = af9035_rd_reg(d, addr + EEPROM_2ND_DEMOD_ADDR, &tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300631 if (ret < 0)
632 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300633
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300634 state->af9033_config[1].i2c_addr = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300635 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
636 __func__, tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300637 }
638
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300639 for (i = 0; i < state->dual_mode + 1; i++) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300640 /* tuner */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300641 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300642 if (ret < 0)
643 goto err;
644
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300645 if (tmp == 0x00)
646 dev_dbg(&d->udev->dev,
647 "%s: [%d]tuner not set, using default\n",
648 __func__, i);
649 else
650 state->af9033_config[i].tuner = tmp;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300651
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300652 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
653 __func__, i, state->af9033_config[i].tuner);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300654
655 switch (state->af9033_config[i].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300656 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300657 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300658 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300659 case AF9033_TUNER_TDA18218:
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300660 case AF9033_TUNER_FC2580:
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300661 case AF9033_TUNER_FC0012:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300662 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300663 break;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300664 case AF9033_TUNER_IT9135_38:
665 case AF9033_TUNER_IT9135_51:
666 case AF9033_TUNER_IT9135_52:
667 case AF9033_TUNER_IT9135_60:
668 case AF9033_TUNER_IT9135_61:
669 case AF9033_TUNER_IT9135_62:
670 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300671 default:
Antti Palosaari9ea36812013-01-11 22:16:25 -0300672 dev_warn(&d->udev->dev,
673 "%s: tuner id=%02x not supported, please report!",
Antti Palosaari119f7a82012-09-12 20:23:53 -0300674 KBUILD_MODNAME, tmp);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300675 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300676
Antti Palosaaribf97b632012-12-08 22:51:19 -0300677 /* disable dual mode if driver does not support it */
678 if (i == 1)
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300679 switch (state->af9033_config[i].tuner) {
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300680 case AF9033_TUNER_FC0012:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300681 case AF9033_TUNER_IT9135_38:
682 case AF9033_TUNER_IT9135_51:
683 case AF9033_TUNER_IT9135_52:
684 case AF9033_TUNER_IT9135_60:
685 case AF9033_TUNER_IT9135_61:
686 case AF9033_TUNER_IT9135_62:
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300687 break;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300688 default:
689 state->dual_mode = false;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300690 dev_info(&d->udev->dev,
691 "%s: driver does not support 2nd tuner and will disable it",
692 KBUILD_MODNAME);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300693 }
694
Antti Palosaari7f882c22012-03-30 09:10:08 -0300695 /* tuner IF frequency */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300696 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300697 if (ret < 0)
698 goto err;
699
700 tmp16 = tmp;
701
Antti Palosaari9ea36812013-01-11 22:16:25 -0300702 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300703 if (ret < 0)
704 goto err;
705
706 tmp16 |= tmp << 8;
707
Antti Palosaari119f7a82012-09-12 20:23:53 -0300708 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300709
Antti Palosaari9ea36812013-01-11 22:16:25 -0300710 addr += 0x10; /* shift for the 2nd tuner params */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300711 }
712
Antti Palosaari9ea36812013-01-11 22:16:25 -0300713skip_eeprom:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300714 /* get demod clock */
715 ret = af9035_rd_reg(d, 0x00d800, &tmp);
716 if (ret < 0)
717 goto err;
718
719 tmp = (tmp >> 0) & 0x0f;
720
Antti Palosaari9ea36812013-01-11 22:16:25 -0300721 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
722 if (state->chip_type == 0x9135)
723 state->af9033_config[i].clock = clock_lut_it9135[tmp];
724 else
725 state->af9033_config[i].clock = clock_lut_af9035[tmp];
Antti Palosaari74c18832013-01-07 15:16:54 -0300726 }
727
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300728 return 0;
729
730err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300731 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300732
733 return ret;
734}
735
Antti Palosaari51639be2012-09-16 22:26:56 -0300736static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
737 int cmd, int arg)
738{
739 int ret;
740 u8 val;
741
742 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
743
744 /*
745 * CEN always enabled by hardware wiring
746 * RESETN GPIOT3
747 * RXEN GPIOT2
748 */
749
750 switch (cmd) {
751 case TUA9001_CMD_RESETN:
752 if (arg)
753 val = 0x00;
754 else
755 val = 0x01;
756
757 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
758 if (ret < 0)
759 goto err;
760 break;
761 case TUA9001_CMD_RXEN:
762 if (arg)
763 val = 0x01;
764 else
765 val = 0x00;
766
767 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
768 if (ret < 0)
769 goto err;
770 break;
771 }
772
773 return 0;
774
775err:
776 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
777
778 return ret;
779}
780
781
Michael Büschffc501f2012-04-02 12:18:36 -0300782static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300783 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300784{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300785 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300786
787 switch (cmd) {
788 case FC0011_FE_CALLBACK_POWER:
789 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300790 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
791 if (ret < 0)
792 goto err;
793
794 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
795 if (ret < 0)
796 goto err;
797
798 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
799 if (ret < 0)
800 goto err;
801
Michael Büschffc501f2012-04-02 12:18:36 -0300802 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300803 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
804 if (ret < 0)
805 goto err;
806
807 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
808 if (ret < 0)
809 goto err;
810
Michael Büschde2bec52012-04-03 05:11:30 -0300811 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300812 break;
813 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300814 ret = af9035_wr_reg(d, 0xd8e9, 1);
815 if (ret < 0)
816 goto err;
817
818 ret = af9035_wr_reg(d, 0xd8e8, 1);
819 if (ret < 0)
820 goto err;
821
822 ret = af9035_wr_reg(d, 0xd8e7, 1);
823 if (ret < 0)
824 goto err;
825
Michael Büschde2bec52012-04-03 05:11:30 -0300826 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300827
828 ret = af9035_wr_reg(d, 0xd8e7, 0);
829 if (ret < 0)
830 goto err;
831
Michael Büschde2bec52012-04-03 05:11:30 -0300832 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300833 break;
834 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300835 ret = -EINVAL;
836 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300837 }
838
839 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300840
841err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300842 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300843
844 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300845}
846
847static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
848{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300849 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300850
851 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300852 case AF9033_TUNER_FC0011:
853 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300854 case AF9033_TUNER_TUA9001:
855 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300856 default:
857 break;
858 }
859
Antti Palosaari1835af12012-09-11 22:27:06 -0300860 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300861}
862
863static int af9035_frontend_callback(void *adapter_priv, int component,
864 int cmd, int arg)
865{
866 struct i2c_adapter *adap = adapter_priv;
867 struct dvb_usb_device *d = i2c_get_adapdata(adap);
868
Antti Palosaari1835af12012-09-11 22:27:06 -0300869 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
870 __func__, component, cmd, arg);
871
Michael Büschffc501f2012-04-02 12:18:36 -0300872 switch (component) {
873 case DVB_FRONTEND_COMPONENT_TUNER:
874 return af9035_tuner_callback(d, cmd, arg);
875 default:
876 break;
877 }
878
Antti Palosaari1835af12012-09-11 22:27:06 -0300879 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300880}
881
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300882static int af9035_get_adapter_count(struct dvb_usb_device *d)
883{
884 struct state *state = d_to_priv(d);
Antti Palosaaribada3422013-01-11 20:45:11 -0300885
886 /* disable 2nd adapter as we don't have PID filters implemented */
887 if (d->udev->speed == USB_SPEED_FULL)
888 return 1;
889 else
890 return state->dual_mode + 1;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300891}
892
Antti Palosaari7f882c22012-03-30 09:10:08 -0300893static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
894{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300895 struct state *state = adap_to_priv(adap);
896 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300897 int ret;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300898 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300899
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300900 if (!state->af9033_config[adap->id].tuner) {
901 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300902 ret = -ENODEV;
903 goto err;
904 }
905
Antti Palosaari7f882c22012-03-30 09:10:08 -0300906 if (adap->id == 0) {
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300907 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
908 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300909 }
910
911 /* attach demodulator */
Antti Palosaaribf97b632012-12-08 22:51:19 -0300912 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
913 &d->i2c_adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300914 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300915 ret = -ENODEV;
916 goto err;
917 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300918
919 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300920 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
921 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300922
923 return 0;
924
925err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300926 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300927
928 return ret;
929}
930
931static struct tua9001_config af9035_tua9001_config = {
932 .i2c_addr = 0x60,
933};
934
Michael Büschffc501f2012-04-02 12:18:36 -0300935static const struct fc0011_config af9035_fc0011_config = {
936 .i2c_address = 0x60,
937};
938
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300939static struct mxl5007t_config af9035_mxl5007t_config[] = {
940 {
941 .xtal_freq_hz = MxL_XTAL_24_MHZ,
942 .if_freq_hz = MxL_IF_4_57_MHZ,
943 .invert_if = 0,
944 .loop_thru_enable = 0,
945 .clk_out_enable = 0,
946 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
947 }, {
948 .xtal_freq_hz = MxL_XTAL_24_MHZ,
949 .if_freq_hz = MxL_IF_4_57_MHZ,
950 .invert_if = 0,
951 .loop_thru_enable = 1,
952 .clk_out_enable = 1,
953 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
954 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300955};
956
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300957static struct tda18218_config af9035_tda18218_config = {
958 .i2c_address = 0x60,
959 .i2c_wr_max = 21,
960};
961
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300962static const struct fc2580_config af9035_fc2580_config = {
963 .i2c_addr = 0x56,
964 .clock = 16384000,
965};
966
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300967static const struct fc0012_config af9035_fc0012_config[] = {
968 {
969 .i2c_address = 0x63,
970 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300971 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300972 .loop_through = true,
973 .clock_out = true,
974 }, {
975 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
976 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300977 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300978 }
Antti Palosaariad3a7582012-12-08 23:27:49 -0300979};
980
Antti Palosaariac77fb02013-01-07 09:51:13 -0300981static struct ite_config af9035_it913x_config = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300982 .chip_ver = 0x02,
Antti Palosaariac77fb02013-01-07 09:51:13 -0300983 .chip_type = 0x9135,
984 .firmware = 0x00000000,
985 .firmware_ver = 1,
986 .adc_x2 = 1,
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300987 .tuner_id_0 = 0x00,
Antti Palosaariac77fb02013-01-07 09:51:13 -0300988 .tuner_id_1 = 0x00,
989 .dual_mode = 0x00,
990 .adf = 0x00,
991 /* option to read SIGNAL_LEVEL */
992 .read_slevel = 0,
993};
994
Antti Palosaari7f882c22012-03-30 09:10:08 -0300995static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
996{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300997 struct state *state = adap_to_priv(adap);
998 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300999 int ret;
1000 struct dvb_frontend *fe;
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001001 struct i2c_msg msg[1];
Antti Palosaaribf97b632012-12-08 22:51:19 -03001002 u8 tuner_addr;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001003 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1004
Antti Palosaaribf97b632012-12-08 22:51:19 -03001005 /*
1006 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1007 * to carry info about used I2C bus for dual tuner configuration.
1008 */
Antti Palosaari7f882c22012-03-30 09:10:08 -03001009
Antti Palosaari2a79eef2012-05-07 14:50:40 -03001010 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -03001011 case AF9033_TUNER_TUA9001:
1012 /* AF9035 gpiot3 = TUA9001 RESETN
1013 AF9035 gpiot2 = TUA9001 RXEN */
1014
1015 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001016 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001017 if (ret < 0)
1018 goto err;
1019
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001020 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001021 if (ret < 0)
1022 goto err;
1023
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001024 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001025 if (ret < 0)
1026 goto err;
1027
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001028 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001029 if (ret < 0)
1030 goto err;
1031
Antti Palosaari7f882c22012-03-30 09:10:08 -03001032 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001033 fe = dvb_attach(tua9001_attach, adap->fe[0],
1034 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001035 break;
Michael Büschffc501f2012-04-02 12:18:36 -03001036 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001037 fe = dvb_attach(fc0011_attach, adap->fe[0],
1038 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -03001039 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001040 case AF9033_TUNER_MXL5007T:
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001041 if (adap->id == 0) {
1042 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1043 if (ret < 0)
1044 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001045
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001046 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1047 if (ret < 0)
1048 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001049
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001050 ret = af9035_wr_reg(d, 0x00d8df, 0);
1051 if (ret < 0)
1052 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001053
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001054 msleep(30);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001055
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001056 ret = af9035_wr_reg(d, 0x00d8df, 1);
1057 if (ret < 0)
1058 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001059
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001060 msleep(300);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001061
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001062 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1063 if (ret < 0)
1064 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001065
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001066 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1067 if (ret < 0)
1068 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001069
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001070 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1071 if (ret < 0)
1072 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001073
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001074 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1075 if (ret < 0)
1076 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001077
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001078 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1079 if (ret < 0)
1080 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001081
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001082 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1083 if (ret < 0)
1084 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001085
1086 tuner_addr = 0x60;
1087 } else {
1088 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001089 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001090
1091 /* attach tuner */
Antti Palosaaribf97b632012-12-08 22:51:19 -03001092 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1093 tuner_addr, &af9035_mxl5007t_config[adap->id]);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001094 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001095 case AF9033_TUNER_TDA18218:
1096 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001097 fe = dvb_attach(tda18218_attach, adap->fe[0],
1098 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001099 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001100 case AF9033_TUNER_FC2580:
1101 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1102 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1103 if (ret < 0)
1104 goto err;
1105
1106 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1107 if (ret < 0)
1108 goto err;
1109
1110 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1111 if (ret < 0)
1112 goto err;
1113
1114 usleep_range(10000, 50000);
1115 /* attach tuner */
1116 fe = dvb_attach(fc2580_attach, adap->fe[0],
1117 &d->i2c_adap, &af9035_fc2580_config);
1118 break;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001119 case AF9033_TUNER_FC0012:
1120 /*
1121 * AF9035 gpiot2 = FC0012 enable
1122 * XXX: there seems to be something on gpioh8 too, but on my
1123 * my test I didn't find any difference.
1124 */
1125
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001126 if (adap->id == 0) {
1127 /* configure gpiot2 as output and high */
1128 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1129 if (ret < 0)
1130 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001131
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001132 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1133 if (ret < 0)
1134 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001135
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001136 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1137 if (ret < 0)
1138 goto err;
1139 } else {
1140 /*
1141 * FIXME: That belongs for the FC0012 driver.
1142 * Write 02 to FC0012 master tuner register 0d directly
1143 * in order to make slave tuner working.
1144 */
1145 msg[0].addr = 0x63;
1146 msg[0].flags = 0;
1147 msg[0].len = 2;
1148 msg[0].buf = "\x0d\x02";
1149 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1150 if (ret < 0)
1151 goto err;
1152 }
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001153
1154 usleep_range(10000, 50000);
1155
Antti Palosaariad3a7582012-12-08 23:27:49 -03001156 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001157 &af9035_fc0012_config[adap->id]);
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001158 break;
Antti Palosaariac77fb02013-01-07 09:51:13 -03001159 case AF9033_TUNER_IT9135_38:
Antti Palosaari74c18832013-01-07 15:16:54 -03001160 case AF9033_TUNER_IT9135_51:
1161 case AF9033_TUNER_IT9135_52:
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001162 af9035_it913x_config.chip_ver = 0x01;
Antti Palosaari74c18832013-01-07 15:16:54 -03001163 case AF9033_TUNER_IT9135_60:
1164 case AF9033_TUNER_IT9135_61:
1165 case AF9033_TUNER_IT9135_62:
Antti Palosaari74c18832013-01-07 15:16:54 -03001166 af9035_it913x_config.tuner_id_0 = state->af9033_config[0].tuner;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001167 /* attach tuner */
1168 fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1169 state->af9033_config[adap->id].i2c_addr,
1170 &af9035_it913x_config);
Antti Palosaariac77fb02013-01-07 09:51:13 -03001171 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -03001172 default:
1173 fe = NULL;
1174 }
1175
1176 if (fe == NULL) {
1177 ret = -ENODEV;
1178 goto err;
1179 }
1180
1181 return 0;
1182
1183err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001184 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001185
1186 return ret;
1187}
1188
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001189static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -03001190{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001191 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001192 int ret, i;
Antti Palosaaribada3422013-01-11 20:45:11 -03001193 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1194 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001195 struct reg_val_mask tab[] = {
1196 { 0x80f99d, 0x01, 0x01 },
1197 { 0x80f9a4, 0x01, 0x01 },
1198 { 0x00dd11, 0x00, 0x20 },
1199 { 0x00dd11, 0x00, 0x40 },
1200 { 0x00dd13, 0x00, 0x20 },
1201 { 0x00dd13, 0x00, 0x40 },
1202 { 0x00dd11, 0x20, 0x20 },
1203 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1204 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1205 { 0x00dd0c, packet_size, 0xff},
1206 { 0x00dd11, state->dual_mode << 6, 0x40 },
1207 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1208 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1209 { 0x00dd0d, packet_size, 0xff },
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001210 { 0x80f9a3, state->dual_mode, 0x01 },
1211 { 0x80f9cd, state->dual_mode, 0x01 },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001212 { 0x80f99d, 0x00, 0x01 },
1213 { 0x80f9a4, 0x00, 0x01 },
1214 };
Antti Palosaari7f882c22012-03-30 09:10:08 -03001215
Antti Palosaari119f7a82012-09-12 20:23:53 -03001216 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
1217 "packet_size=%02x\n", __func__,
1218 d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001219
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001220 /* init endpoints */
1221 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1222 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1223 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001224 if (ret < 0)
1225 goto err;
1226 }
1227
1228 return 0;
1229
1230err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001231 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001232
1233 return ret;
1234}
1235
Antti Palosaari37b44a02013-01-04 15:21:26 -03001236#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001237static int af9035_rc_query(struct dvb_usb_device *d)
1238{
1239 unsigned int key;
1240 unsigned char b[4];
1241 int ret;
1242 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
1243
1244 ret = af9035_ctrl_msg(d, &req);
1245 if (ret < 0)
1246 goto err;
1247
1248 if ((b[2] + b[3]) == 0xff) {
1249 if ((b[0] + b[1]) == 0xff) {
1250 /* NEC */
1251 key = b[0] << 8 | b[2];
1252 } else {
1253 /* ext. NEC */
1254 key = b[0] << 16 | b[1] << 8 | b[2];
1255 }
1256 } else {
1257 key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
1258 }
1259
1260 rc_keydown(d->rc_dev, key, 0);
1261
1262err:
1263 /* ignore errors */
1264 return 0;
1265}
1266
1267static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1268{
Antti Palosaari74c18832013-01-07 15:16:54 -03001269 struct state *state = d_to_priv(d);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001270 int ret;
1271 u8 tmp;
1272
Antti Palosaari74c18832013-01-07 15:16:54 -03001273 /* TODO: IT9135 remote control support */
1274 if (state->chip_type == 0x9135)
1275 return 0;
1276
Antti Palosaari9ea36812013-01-11 22:16:25 -03001277 ret = af9035_rd_reg(d, EEPROM_BASE_AF9035 + EEPROM_IR_MODE, &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001278 if (ret < 0)
1279 goto err;
1280
Antti Palosaari119f7a82012-09-12 20:23:53 -03001281 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001282
1283 /* don't activate rc if in HID mode or if not available */
1284 if (tmp == 5) {
Antti Palosaari9ea36812013-01-11 22:16:25 -03001285 ret = af9035_rd_reg(d, EEPROM_BASE_AF9035 + EEPROM_IR_TYPE,
1286 &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001287 if (ret < 0)
1288 goto err;
1289
Antti Palosaari119f7a82012-09-12 20:23:53 -03001290 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001291
1292 switch (tmp) {
1293 case 0: /* NEC */
1294 default:
David Härdemanc003ab12012-10-11 19:11:54 -03001295 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001296 break;
1297 case 1: /* RC6 */
David Härdemanc003ab12012-10-11 19:11:54 -03001298 rc->allowed_protos = RC_BIT_RC6_MCE;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001299 break;
1300 }
1301
1302 rc->query = af9035_rc_query;
1303 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001304
1305 /* load empty to enable rc */
1306 if (!rc->map_name)
1307 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001308 }
1309
1310 return 0;
1311
1312err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001313 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001314
1315 return ret;
1316}
Antti Palosaarieed56702012-12-09 20:18:31 -03001317#else
1318 #define af9035_get_rc_config NULL
1319#endif
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001320
Antti Palosaaribada3422013-01-11 20:45:11 -03001321static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1322 struct usb_data_stream_properties *stream)
1323{
1324 struct dvb_usb_device *d = fe_to_d(fe);
1325 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1326
1327 if (d->udev->speed == USB_SPEED_FULL)
1328 stream->u.bulk.buffersize = 5 * 188;
1329
1330 return 0;
1331}
1332
1333/*
1334 * FIXME: PID filter is property of demodulator and should be moved to the
1335 * correct driver. Also we support only adapter #0 PID filter and will
1336 * disable adapter #1 if USB1.1 is used.
1337 */
1338static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1339{
1340 struct dvb_usb_device *d = adap_to_d(adap);
1341 int ret;
1342
1343 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1344
1345 ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1346 if (ret < 0)
1347 goto err;
1348
1349 return 0;
1350
1351err:
1352 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1353
1354 return ret;
1355}
1356
1357static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1358 int onoff)
1359{
1360 struct dvb_usb_device *d = adap_to_d(adap);
1361 int ret;
1362 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1363
1364 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1365 __func__, index, pid, onoff);
1366
1367 ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1368 if (ret < 0)
1369 goto err;
1370
1371 ret = af9035_wr_reg(d, 0x80f994, onoff);
1372 if (ret < 0)
1373 goto err;
1374
1375 ret = af9035_wr_reg(d, 0x80f995, index);
1376 if (ret < 0)
1377 goto err;
1378
1379 return 0;
1380
1381err:
1382 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1383
1384 return ret;
1385}
1386
Antti Palosaarib799b812013-01-11 16:22:07 -03001387static int af9035_probe(struct usb_interface *intf,
1388 const struct usb_device_id *id)
1389{
1390 struct usb_device *udev = interface_to_usbdev(intf);
1391 char manufacturer[sizeof("Afatech")];
1392
1393 memset(manufacturer, 0, sizeof(manufacturer));
1394 usb_string(udev, udev->descriptor.iManufacturer,
1395 manufacturer, sizeof(manufacturer));
1396 /*
1397 * There is two devices having same ID but different chipset. One uses
1398 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1399 * is iManufacturer string.
1400 *
1401 * idVendor 0x0ccd TerraTec Electronic GmbH
1402 * idProduct 0x0099
1403 * bcdDevice 2.00
1404 * iManufacturer 1 Afatech
1405 * iProduct 2 DVB-T 2
1406 *
1407 * idVendor 0x0ccd TerraTec Electronic GmbH
1408 * idProduct 0x0099
1409 * bcdDevice 2.00
1410 * iManufacturer 1 ITE Technologies, Inc.
1411 * iProduct 2 DVB-T TV Stick
1412 */
1413 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1414 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1415 if (!strcmp("Afatech", manufacturer)) {
1416 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1417 return -ENODEV;
1418 }
1419 }
1420
1421 return dvb_usbv2_probe(intf, id);
1422}
1423
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001424/* interface 0 is used by DVB-T receiver and
1425 interface 1 is for remote controller (HID) */
1426static const struct dvb_usb_device_properties af9035_props = {
1427 .driver_name = KBUILD_MODNAME,
1428 .owner = THIS_MODULE,
1429 .adapter_nr = adapter_nr,
1430 .size_of_priv = sizeof(struct state),
1431
1432 .generic_bulk_ctrl_endpoint = 0x02,
1433 .generic_bulk_ctrl_endpoint_response = 0x81,
1434
1435 .identify_state = af9035_identify_state,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001436 .download_firmware = af9035_download_firmware,
1437
1438 .i2c_algo = &af9035_i2c_algo,
1439 .read_config = af9035_read_config,
1440 .frontend_attach = af9035_frontend_attach,
1441 .tuner_attach = af9035_tuner_attach,
1442 .init = af9035_init,
1443 .get_rc_config = af9035_get_rc_config,
Antti Palosaaribada3422013-01-11 20:45:11 -03001444 .get_stream_config = af9035_get_stream_config,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001445
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001446 .get_adapter_count = af9035_get_adapter_count,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001447 .adapter = {
1448 {
Antti Palosaaribada3422013-01-11 20:45:11 -03001449 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1450 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1451
1452 .pid_filter_count = 32,
1453 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1454 .pid_filter = af9035_pid_filter,
1455
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001456 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1457 }, {
1458 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1459 },
1460 },
1461};
1462
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001463static const struct usb_device_id af9035_id_table[] = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001464 /* AF9035 devices */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001465 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1466 &af9035_props, "Afatech AF9035 reference design", NULL) },
1467 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1468 &af9035_props, "Afatech AF9035 reference design", NULL) },
1469 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1470 &af9035_props, "Afatech AF9035 reference design", NULL) },
1471 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1472 &af9035_props, "Afatech AF9035 reference design", NULL) },
1473 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1474 &af9035_props, "Afatech AF9035 reference design", NULL) },
1475 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1476 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1477 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1478 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1479 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1480 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1481 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1482 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1483 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1484 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1485 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1486 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001487 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1488 &af9035_props, "Asus U3100Mini Plus", NULL) },
Fabrizio Gazzatod9b75952013-02-17 18:25:34 -03001489 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
1490 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001491 /* IT9135 devices */
1492#if 0
1493 { DVB_USB_DEVICE(0x048d, 0x9135,
1494 &af9035_props, "IT9135 reference design", NULL) },
1495 { DVB_USB_DEVICE(0x048d, 0x9006,
1496 &af9035_props, "IT9135 reference design", NULL) },
1497#endif
Antti Palosaarib799b812013-01-11 16:22:07 -03001498 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1499 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1500 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001501 { }
1502};
1503MODULE_DEVICE_TABLE(usb, af9035_id_table);
1504
Antti Palosaari7f882c22012-03-30 09:10:08 -03001505static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001506 .name = KBUILD_MODNAME,
1507 .id_table = af9035_id_table,
Antti Palosaarib799b812013-01-11 16:22:07 -03001508 .probe = af9035_probe,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001509 .disconnect = dvb_usbv2_disconnect,
1510 .suspend = dvb_usbv2_suspend,
1511 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001512 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001513 .no_dynamic_id = 1,
1514 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001515};
1516
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001517module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001518
1519MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1520MODULE_DESCRIPTION("Afatech AF9035 driver");
1521MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001522MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
Antti Palosaari74c18832013-01-07 15:16:54 -03001523MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1524MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);