blob: cfcf79b5978a19cfdb8ccde6ddbf3f904472c1a5 [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;
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 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;
Wei Yongjun0170a392013-03-26 01:32:19 -030098 goto exit;
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 Palosaari1bfd5292013-03-08 17:39:12 -0300103 /* fw returns status 1 when IR code was not received */
Wei Yongjun0170a392013-03-26 01:32:19 -0300104 if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
105 ret = 1;
106 goto exit;
107 }
Antti Palosaari1bfd5292013-03-08 17:39:12 -0300108
Antti Palosaari119f7a82012-09-12 20:23:53 -0300109 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
Antti Palosaari3484d372013-02-26 13:56:34 -0300110 __func__, req->cmd, state->buf[2]);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300111 ret = -EIO;
Wei Yongjun0170a392013-03-26 01:32:19 -0300112 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300113 }
114
115 /* read request, copy returned data to return buf */
116 if (req->rlen)
Antti Palosaari3484d372013-02-26 13:56:34 -0300117 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300118exit:
Antti Palosaari3484d372013-02-26 13:56:34 -0300119 mutex_unlock(&d->usb_mutex);
Wei Yongjun0170a392013-03-26 01:32:19 -0300120 if (ret < 0)
Antti Palosaari3484d372013-02-26 13:56:34 -0300121 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300122 return ret;
123}
124
125/* write multiple registers */
126static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
127{
128 u8 wbuf[6 + len];
129 u8 mbox = (reg >> 16) & 0xff;
130 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
131
132 wbuf[0] = len;
133 wbuf[1] = 2;
134 wbuf[2] = 0;
135 wbuf[3] = 0;
136 wbuf[4] = (reg >> 8) & 0xff;
137 wbuf[5] = (reg >> 0) & 0xff;
138 memcpy(&wbuf[6], val, len);
139
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300140 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300141}
142
143/* read multiple registers */
144static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
145{
146 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
147 u8 mbox = (reg >> 16) & 0xff;
148 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
149
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300150 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300151}
152
153/* write single register */
154static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
155{
156 return af9035_wr_regs(d, reg, &val, 1);
157}
158
159/* read single register */
160static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
161{
162 return af9035_rd_regs(d, reg, val, 1);
163}
164
165/* write single register with mask */
166static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
167 u8 mask)
168{
169 int ret;
170 u8 tmp;
171
172 /* no need for read if whole reg is written */
173 if (mask != 0xff) {
174 ret = af9035_rd_regs(d, reg, &tmp, 1);
175 if (ret)
176 return ret;
177
178 val &= mask;
179 tmp &= ~mask;
180 val |= tmp;
181 }
182
183 return af9035_wr_regs(d, reg, &val, 1);
184}
185
186static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
187 struct i2c_msg msg[], int num)
188{
189 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300190 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300191 int ret;
192
193 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
194 return -EAGAIN;
195
Antti Palosaariad30e912012-04-02 20:18:59 -0300196 /*
197 * I2C sub header is 5 bytes long. Meaning of those bytes are:
198 * 0: data len
199 * 1: I2C addr << 1
200 * 2: reg addr len
201 * byte 3 and 4 can be used as reg addr
202 * 3: reg addr MSB
203 * used when reg addr len is set to 2
204 * 4: reg addr LSB
205 * used when reg addr len is set to 1 or 2
206 *
207 * For the simplify we do not use register addr at all.
208 * NOTE: As a firmware knows tuner type there is very small possibility
209 * there could be some tuner I2C hacks done by firmware and this may
210 * lead problems if firmware expects those bytes are used.
211 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300212 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
213 (msg[1].flags & I2C_M_RD)) {
214 if (msg[0].len > 40 || msg[1].len > 40) {
215 /* TODO: correct limits > 40 */
216 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300217 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
218 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300219 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300220 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
221 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300222
223 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300224 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300225
Antti Palosaari7f882c22012-03-30 09:10:08 -0300226 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
227 msg[1].len);
228 } else {
229 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300230 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300231 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
232 buf, msg[1].len, msg[1].buf };
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300233 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300234 buf[0] = msg[1].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300235 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300236 buf[2] = 0x00; /* reg addr len */
237 buf[3] = 0x00; /* reg addr MSB */
238 buf[4] = 0x00; /* reg addr LSB */
239 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300240 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300241 }
242 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
243 if (msg[0].len > 40) {
244 /* TODO: correct limits > 40 */
245 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300246 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
247 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300248 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300249 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
250 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300251
252 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300253 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300254
Antti Palosaari7f882c22012-03-30 09:10:08 -0300255 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
256 msg[0].len - 3);
257 } else {
258 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300259 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300260 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
261 0, NULL };
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300262 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300263 buf[0] = msg[0].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300264 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300265 buf[2] = 0x00; /* reg addr len */
266 buf[3] = 0x00; /* reg addr MSB */
267 buf[4] = 0x00; /* reg addr LSB */
268 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300269 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300270 }
Antti Palosaari3b98c342013-03-11 19:05:39 -0300271 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
272 if (msg[0].len > 40) {
273 /* TODO: correct limits > 40 */
274 ret = -EOPNOTSUPP;
275 } else {
276 /* I2C */
277 u8 buf[5];
278 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
279 buf, msg[0].len, msg[0].buf };
280 req.mbox |= ((msg[0].addr & 0x80) >> 3);
281 buf[0] = msg[0].len;
282 buf[1] = msg[0].addr << 1;
283 buf[2] = 0x00; /* reg addr len */
284 buf[3] = 0x00; /* reg addr MSB */
285 buf[4] = 0x00; /* reg addr LSB */
286 ret = af9035_ctrl_msg(d, &req);
287 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300288 } else {
289 /*
Antti Palosaari3b98c342013-03-11 19:05:39 -0300290 * We support only three kind of I2C transactions:
291 * 1) 1 x read + 1 x write (repeated start)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300292 * 2) 1 x write
Antti Palosaari3b98c342013-03-11 19:05:39 -0300293 * 3) 1 x read
Antti Palosaari7f882c22012-03-30 09:10:08 -0300294 */
295 ret = -EOPNOTSUPP;
296 }
297
298 mutex_unlock(&d->i2c_mutex);
299
300 if (ret < 0)
301 return ret;
302 else
303 return num;
304}
305
306static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
307{
308 return I2C_FUNC_I2C;
309}
310
311static struct i2c_algorithm af9035_i2c_algo = {
312 .master_xfer = af9035_i2c_master_xfer,
313 .functionality = af9035_i2c_functionality,
314};
315
Antti Palosaaria0921af2012-06-18 23:42:53 -0300316static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300317{
Antti Palosaari74c18832013-01-07 15:16:54 -0300318 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300319 int ret;
320 u8 wbuf[1] = { 1 };
321 u8 rbuf[4];
322 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
323 sizeof(rbuf), rbuf };
324
Antti Palosaari74c18832013-01-07 15:16:54 -0300325 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
326 if (ret < 0)
327 goto err;
328
329 state->chip_version = rbuf[0];
330 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
331
332 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
333 if (ret < 0)
334 goto err;
335
336 dev_info(&d->udev->dev,
337 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
338 __func__, state->prechip_version, state->chip_version,
339 state->chip_type);
340
341 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300342 if (state->chip_version == 0x02)
Antti Palosaari74c18832013-01-07 15:16:54 -0300343 *name = AF9035_FIRMWARE_IT9135_V2;
344 else
345 *name = AF9035_FIRMWARE_IT9135_V1;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300346 state->eeprom_addr = EEPROM_BASE_IT9135;
Antti Palosaari74c18832013-01-07 15:16:54 -0300347 } else {
348 *name = AF9035_FIRMWARE_AF9035;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300349 state->eeprom_addr = EEPROM_BASE_AF9035;
Antti Palosaari74c18832013-01-07 15:16:54 -0300350 }
351
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300352 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300353 if (ret < 0)
354 goto err;
355
Antti Palosaari119f7a82012-09-12 20:23:53 -0300356 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300357 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300358 ret = WARM;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300359 else
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300360 ret = COLD;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300361
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300362 return ret;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300363
364err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300365 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300366
367 return ret;
368}
369
Antti Palosaari8229da52013-03-07 17:59:55 -0300370static int af9035_download_firmware_old(struct dvb_usb_device *d,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300371 const struct firmware *fw)
372{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300373 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300374 u8 wbuf[1];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300375 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
376 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300377 u8 hdr_core;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300378 u16 hdr_addr, hdr_data_len, hdr_checksum;
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300379 #define MAX_DATA 58
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300380 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300381
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300382 /*
383 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
384 *
385 * byte 0: MCS 51 core
386 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
387 * address spaces
388 * byte 1-2: Big endian destination address
389 * byte 3-4: Big endian number of data bytes following the header
390 * byte 5-6: Big endian header checksum, apparently ignored by the chip
391 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
392 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300393
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300394 for (i = fw->size; i > HDR_SIZE;) {
395 hdr_core = fw->data[fw->size - i + 0];
396 hdr_addr = fw->data[fw->size - i + 1] << 8;
397 hdr_addr |= fw->data[fw->size - i + 2] << 0;
398 hdr_data_len = fw->data[fw->size - i + 3] << 8;
399 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
400 hdr_checksum = fw->data[fw->size - i + 5] << 8;
401 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300402
Antti Palosaari119f7a82012-09-12 20:23:53 -0300403 dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
404 "checksum=%04x\n", __func__, hdr_core, hdr_addr,
405 hdr_data_len, hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300406
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300407 if (((hdr_core != 1) && (hdr_core != 2)) ||
408 (hdr_data_len > i)) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300409 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300410 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300411 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300412
413 /* download begin packet */
414 req.cmd = CMD_FW_DL_BEGIN;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300415 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300416 if (ret < 0)
417 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300418
419 /* download firmware packet(s) */
420 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
421 len = j;
422 if (len > MAX_DATA)
423 len = MAX_DATA;
424 req_fw_dl.wlen = len;
425 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
426 HDR_SIZE + hdr_data_len - j];
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300427 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300428 if (ret < 0)
429 goto err;
430 }
431
432 /* download end packet */
433 req.cmd = CMD_FW_DL_END;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300434 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300435 if (ret < 0)
436 goto err;
437
438 i -= hdr_data_len + HDR_SIZE;
439
Antti Palosaari119f7a82012-09-12 20:23:53 -0300440 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
441 __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300442 }
443
Antti Palosaariff4e3fe2012-12-09 16:25:08 -0300444 /* print warn if firmware is bad, continue and see what happens */
445 if (i)
446 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
447
Antti Palosaari7f882c22012-03-30 09:10:08 -0300448 return 0;
449
450err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300451 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300452
453 return ret;
454}
455
Antti Palosaari8229da52013-03-07 17:59:55 -0300456static int af9035_download_firmware_new(struct dvb_usb_device *d,
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300457 const struct firmware *fw)
458{
459 int ret, i, i_prev;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300460 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300461 #define HDR_SIZE 7
462
463 /*
464 * There seems to be following firmware header. Meaning of bytes 0-3
465 * is unknown.
466 *
467 * 0: 3
468 * 1: 0, 1
469 * 2: 0
470 * 3: 1, 2, 3
471 * 4: addr MSB
472 * 5: addr LSB
473 * 6: count of data bytes ?
474 */
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300475 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
476 if (i == fw->size ||
477 (fw->data[i + 0] == 0x03 &&
478 (fw->data[i + 1] == 0x00 ||
479 fw->data[i + 1] == 0x01) &&
480 fw->data[i + 2] == 0x00)) {
481 req_fw_dl.wlen = i - i_prev;
482 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
483 i_prev = i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300484 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300485 if (ret < 0)
486 goto err;
487
Antti Palosaari119f7a82012-09-12 20:23:53 -0300488 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
489 __func__, i);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300490 }
491 }
492
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300493 return 0;
494
495err:
496 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
497
498 return ret;
499}
500
501static int af9035_download_firmware(struct dvb_usb_device *d,
502 const struct firmware *fw)
503{
504 struct state *state = d_to_priv(d);
505 int ret;
506 u8 wbuf[1];
507 u8 rbuf[4];
508 u8 tmp;
509 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
510 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
511 dev_dbg(&d->udev->dev, "%s:\n", __func__);
512
513 /*
514 * In case of dual tuner configuration we need to do some extra
515 * initialization in order to download firmware to slave demod too,
516 * which is done by master demod.
517 * Master feeds also clock and controls power via GPIO.
518 */
519 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
520 if (ret < 0)
521 goto err;
522
523 if (tmp) {
524 /* configure gpioh1, reset & power slave demod */
525 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
526 if (ret < 0)
527 goto err;
528
529 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
530 if (ret < 0)
531 goto err;
532
533 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
534 if (ret < 0)
535 goto err;
536
537 usleep_range(10000, 50000);
538
539 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
540 if (ret < 0)
541 goto err;
542
543 /* tell the slave I2C address */
544 ret = af9035_rd_reg(d,
545 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
546 &tmp);
547 if (ret < 0)
548 goto err;
549
550 if (state->chip_type == 0x9135) {
551 ret = af9035_wr_reg(d, 0x004bfb, tmp);
552 if (ret < 0)
553 goto err;
554 } else {
555 ret = af9035_wr_reg(d, 0x00417f, tmp);
556 if (ret < 0)
557 goto err;
558
559 /* enable clock out */
560 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
561 if (ret < 0)
562 goto err;
563 }
564 }
565
Antti Palosaari8229da52013-03-07 17:59:55 -0300566 if (fw->data[0] == 0x01)
567 ret = af9035_download_firmware_old(d, fw);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300568 else
Antti Palosaari8229da52013-03-07 17:59:55 -0300569 ret = af9035_download_firmware_new(d, fw);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300570 if (ret < 0)
571 goto err;
572
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300573 /* firmware loaded, request boot */
574 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300575 ret = af9035_ctrl_msg(d, &req);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300576 if (ret < 0)
577 goto err;
578
579 /* ensure firmware starts */
580 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300581 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300582 if (ret < 0)
583 goto err;
584
585 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300586 dev_err(&d->udev->dev, "%s: firmware did not run\n",
587 KBUILD_MODNAME);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300588 ret = -ENODEV;
589 goto err;
590 }
591
Antti Palosaari119f7a82012-09-12 20:23:53 -0300592 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
593 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300594
595 return 0;
596
597err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300598 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300599
600 return ret;
601}
602
Antti Palosaari9ea36812013-01-11 22:16:25 -0300603static int af9035_read_config(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300604{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300605 struct state *state = d_to_priv(d);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300606 int ret, i;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300607 u8 tmp;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300608 u16 tmp16, addr;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300609
Antti Palosaaribf97b632012-12-08 22:51:19 -0300610 /* demod I2C "address" */
611 state->af9033_config[0].i2c_addr = 0x38;
Antti Palosaari0d94d6a2013-01-07 15:26:05 -0300612 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300613 state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaariab56ad62013-03-07 18:43:51 -0300614 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
615 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300616
Antti Palosaari9ea36812013-01-11 22:16:25 -0300617 /* eeprom memory mapped location */
618 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300619 if (state->chip_version == 0x02) {
620 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300621 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300622 tmp16 = 0x00461d;
623 } else {
624 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300625 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300626 tmp16 = 0x00461b;
627 }
628
Antti Palosaari9ea36812013-01-11 22:16:25 -0300629 /* check if eeprom exists */
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300630 ret = af9035_rd_reg(d, tmp16, &tmp);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300631 if (ret < 0)
632 goto err;
633
Antti Palosaari431a6d42013-03-07 18:28:25 -0300634 if (tmp == 0x00) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300635 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300636 goto skip_eeprom;
637 }
Antti Palosaari9ea36812013-01-11 22:16:25 -0300638 }
639
Antti Palosaari7f882c22012-03-30 09:10:08 -0300640 /* check if there is dual tuners */
Antti Palosaari431a6d42013-03-07 18:28:25 -0300641 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300642 if (ret < 0)
643 goto err;
644
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300645 state->dual_mode = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300646 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
647 state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300648
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300649 if (state->dual_mode) {
650 /* read 2nd demodulator I2C address */
Antti Palosaari431a6d42013-03-07 18:28:25 -0300651 ret = af9035_rd_reg(d,
652 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
653 &tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300654 if (ret < 0)
655 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300656
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300657 state->af9033_config[1].i2c_addr = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300658 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
659 __func__, tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300660 }
661
Antti Palosaari431a6d42013-03-07 18:28:25 -0300662 addr = state->eeprom_addr;
663
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300664 for (i = 0; i < state->dual_mode + 1; i++) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300665 /* tuner */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300666 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300667 if (ret < 0)
668 goto err;
669
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300670 if (tmp == 0x00)
671 dev_dbg(&d->udev->dev,
672 "%s: [%d]tuner not set, using default\n",
673 __func__, i);
674 else
675 state->af9033_config[i].tuner = tmp;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300676
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300677 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
678 __func__, i, state->af9033_config[i].tuner);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300679
680 switch (state->af9033_config[i].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300681 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300682 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300683 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300684 case AF9033_TUNER_TDA18218:
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300685 case AF9033_TUNER_FC2580:
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300686 case AF9033_TUNER_FC0012:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300687 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300688 break;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300689 case AF9033_TUNER_IT9135_38:
690 case AF9033_TUNER_IT9135_51:
691 case AF9033_TUNER_IT9135_52:
692 case AF9033_TUNER_IT9135_60:
693 case AF9033_TUNER_IT9135_61:
694 case AF9033_TUNER_IT9135_62:
695 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300696 default:
Antti Palosaari9ea36812013-01-11 22:16:25 -0300697 dev_warn(&d->udev->dev,
698 "%s: tuner id=%02x not supported, please report!",
Antti Palosaari119f7a82012-09-12 20:23:53 -0300699 KBUILD_MODNAME, tmp);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300700 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300701
Antti Palosaaribf97b632012-12-08 22:51:19 -0300702 /* disable dual mode if driver does not support it */
703 if (i == 1)
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300704 switch (state->af9033_config[i].tuner) {
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300705 case AF9033_TUNER_FC0012:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300706 case AF9033_TUNER_IT9135_38:
707 case AF9033_TUNER_IT9135_51:
708 case AF9033_TUNER_IT9135_52:
709 case AF9033_TUNER_IT9135_60:
710 case AF9033_TUNER_IT9135_61:
711 case AF9033_TUNER_IT9135_62:
Jose Alberto Reguero78c7bc42013-02-10 15:43:03 -0300712 case AF9033_TUNER_MXL5007T:
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300713 break;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300714 default:
715 state->dual_mode = false;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300716 dev_info(&d->udev->dev,
717 "%s: driver does not support 2nd tuner and will disable it",
718 KBUILD_MODNAME);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300719 }
720
Antti Palosaari7f882c22012-03-30 09:10:08 -0300721 /* tuner IF frequency */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300722 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300723 if (ret < 0)
724 goto err;
725
726 tmp16 = tmp;
727
Antti Palosaari9ea36812013-01-11 22:16:25 -0300728 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300729 if (ret < 0)
730 goto err;
731
732 tmp16 |= tmp << 8;
733
Antti Palosaari119f7a82012-09-12 20:23:53 -0300734 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300735
Antti Palosaari9ea36812013-01-11 22:16:25 -0300736 addr += 0x10; /* shift for the 2nd tuner params */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300737 }
738
Antti Palosaari9ea36812013-01-11 22:16:25 -0300739skip_eeprom:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300740 /* get demod clock */
741 ret = af9035_rd_reg(d, 0x00d800, &tmp);
742 if (ret < 0)
743 goto err;
744
745 tmp = (tmp >> 0) & 0x0f;
746
Antti Palosaari9ea36812013-01-11 22:16:25 -0300747 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
748 if (state->chip_type == 0x9135)
749 state->af9033_config[i].clock = clock_lut_it9135[tmp];
750 else
751 state->af9033_config[i].clock = clock_lut_af9035[tmp];
Antti Palosaari74c18832013-01-07 15:16:54 -0300752 }
753
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300754 return 0;
755
756err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300757 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300758
759 return ret;
760}
761
Antti Palosaari51639be2012-09-16 22:26:56 -0300762static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
763 int cmd, int arg)
764{
765 int ret;
766 u8 val;
767
768 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
769
770 /*
771 * CEN always enabled by hardware wiring
772 * RESETN GPIOT3
773 * RXEN GPIOT2
774 */
775
776 switch (cmd) {
777 case TUA9001_CMD_RESETN:
778 if (arg)
779 val = 0x00;
780 else
781 val = 0x01;
782
783 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
784 if (ret < 0)
785 goto err;
786 break;
787 case TUA9001_CMD_RXEN:
788 if (arg)
789 val = 0x01;
790 else
791 val = 0x00;
792
793 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
794 if (ret < 0)
795 goto err;
796 break;
797 }
798
799 return 0;
800
801err:
802 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
803
804 return ret;
805}
806
807
Michael Büschffc501f2012-04-02 12:18:36 -0300808static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300809 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300810{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300811 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300812
813 switch (cmd) {
814 case FC0011_FE_CALLBACK_POWER:
815 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300816 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
817 if (ret < 0)
818 goto err;
819
820 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
821 if (ret < 0)
822 goto err;
823
824 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
825 if (ret < 0)
826 goto err;
827
Michael Büschffc501f2012-04-02 12:18:36 -0300828 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300829 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
830 if (ret < 0)
831 goto err;
832
833 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
834 if (ret < 0)
835 goto err;
836
Michael Büschde2bec52012-04-03 05:11:30 -0300837 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300838 break;
839 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300840 ret = af9035_wr_reg(d, 0xd8e9, 1);
841 if (ret < 0)
842 goto err;
843
844 ret = af9035_wr_reg(d, 0xd8e8, 1);
845 if (ret < 0)
846 goto err;
847
848 ret = af9035_wr_reg(d, 0xd8e7, 1);
849 if (ret < 0)
850 goto err;
851
Michael Büschde2bec52012-04-03 05:11:30 -0300852 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300853
854 ret = af9035_wr_reg(d, 0xd8e7, 0);
855 if (ret < 0)
856 goto err;
857
Michael Büschde2bec52012-04-03 05:11:30 -0300858 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300859 break;
860 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300861 ret = -EINVAL;
862 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300863 }
864
865 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300866
867err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300868 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300869
870 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300871}
872
873static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
874{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300875 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300876
877 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300878 case AF9033_TUNER_FC0011:
879 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300880 case AF9033_TUNER_TUA9001:
881 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300882 default:
883 break;
884 }
885
Antti Palosaari1835af12012-09-11 22:27:06 -0300886 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300887}
888
889static int af9035_frontend_callback(void *adapter_priv, int component,
890 int cmd, int arg)
891{
892 struct i2c_adapter *adap = adapter_priv;
893 struct dvb_usb_device *d = i2c_get_adapdata(adap);
894
Antti Palosaari1835af12012-09-11 22:27:06 -0300895 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
896 __func__, component, cmd, arg);
897
Michael Büschffc501f2012-04-02 12:18:36 -0300898 switch (component) {
899 case DVB_FRONTEND_COMPONENT_TUNER:
900 return af9035_tuner_callback(d, cmd, arg);
901 default:
902 break;
903 }
904
Antti Palosaari1835af12012-09-11 22:27:06 -0300905 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300906}
907
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300908static int af9035_get_adapter_count(struct dvb_usb_device *d)
909{
910 struct state *state = d_to_priv(d);
Antti Palosaaribada3422013-01-11 20:45:11 -0300911
912 /* disable 2nd adapter as we don't have PID filters implemented */
913 if (d->udev->speed == USB_SPEED_FULL)
914 return 1;
915 else
916 return state->dual_mode + 1;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300917}
918
Antti Palosaari7f882c22012-03-30 09:10:08 -0300919static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
920{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300921 struct state *state = adap_to_priv(adap);
922 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300923 int ret;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300924 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300925
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300926 if (!state->af9033_config[adap->id].tuner) {
927 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300928 ret = -ENODEV;
929 goto err;
930 }
931
Antti Palosaari7f882c22012-03-30 09:10:08 -0300932 /* attach demodulator */
Antti Palosaaribf97b632012-12-08 22:51:19 -0300933 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
934 &d->i2c_adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300935 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300936 ret = -ENODEV;
937 goto err;
938 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300939
940 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300941 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
942 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300943
944 return 0;
945
946err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300947 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300948
949 return ret;
950}
951
952static struct tua9001_config af9035_tua9001_config = {
953 .i2c_addr = 0x60,
954};
955
Michael Büschffc501f2012-04-02 12:18:36 -0300956static const struct fc0011_config af9035_fc0011_config = {
957 .i2c_address = 0x60,
958};
959
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300960static struct mxl5007t_config af9035_mxl5007t_config[] = {
961 {
962 .xtal_freq_hz = MxL_XTAL_24_MHZ,
963 .if_freq_hz = MxL_IF_4_57_MHZ,
964 .invert_if = 0,
965 .loop_thru_enable = 0,
966 .clk_out_enable = 0,
967 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
968 }, {
969 .xtal_freq_hz = MxL_XTAL_24_MHZ,
970 .if_freq_hz = MxL_IF_4_57_MHZ,
971 .invert_if = 0,
972 .loop_thru_enable = 1,
973 .clk_out_enable = 1,
974 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
975 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300976};
977
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300978static struct tda18218_config af9035_tda18218_config = {
979 .i2c_address = 0x60,
980 .i2c_wr_max = 21,
981};
982
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300983static const struct fc2580_config af9035_fc2580_config = {
984 .i2c_addr = 0x56,
985 .clock = 16384000,
986};
987
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300988static const struct fc0012_config af9035_fc0012_config[] = {
989 {
990 .i2c_address = 0x63,
991 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300992 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300993 .loop_through = true,
994 .clock_out = true,
995 }, {
996 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
997 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300998 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300999 }
Antti Palosaariad3a7582012-12-08 23:27:49 -03001000};
1001
Antti Palosaari7f882c22012-03-30 09:10:08 -03001002static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1003{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001004 struct state *state = adap_to_priv(adap);
1005 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001006 int ret;
1007 struct dvb_frontend *fe;
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001008 struct i2c_msg msg[1];
Antti Palosaaribf97b632012-12-08 22:51:19 -03001009 u8 tuner_addr;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001010 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1011
Antti Palosaaribf97b632012-12-08 22:51:19 -03001012 /*
1013 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1014 * to carry info about used I2C bus for dual tuner configuration.
1015 */
Antti Palosaari7f882c22012-03-30 09:10:08 -03001016
Antti Palosaari2a79eef2012-05-07 14:50:40 -03001017 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -03001018 case AF9033_TUNER_TUA9001:
1019 /* AF9035 gpiot3 = TUA9001 RESETN
1020 AF9035 gpiot2 = TUA9001 RXEN */
1021
1022 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001023 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001024 if (ret < 0)
1025 goto err;
1026
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001027 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001028 if (ret < 0)
1029 goto err;
1030
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001031 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001032 if (ret < 0)
1033 goto err;
1034
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001035 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001036 if (ret < 0)
1037 goto err;
1038
Antti Palosaari7f882c22012-03-30 09:10:08 -03001039 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001040 fe = dvb_attach(tua9001_attach, adap->fe[0],
1041 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001042 break;
Michael Büschffc501f2012-04-02 12:18:36 -03001043 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001044 fe = dvb_attach(fc0011_attach, adap->fe[0],
1045 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -03001046 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001047 case AF9033_TUNER_MXL5007T:
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001048 if (adap->id == 0) {
1049 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1050 if (ret < 0)
1051 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001052
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001053 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1054 if (ret < 0)
1055 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001056
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001057 ret = af9035_wr_reg(d, 0x00d8df, 0);
1058 if (ret < 0)
1059 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001060
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001061 msleep(30);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001062
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001063 ret = af9035_wr_reg(d, 0x00d8df, 1);
1064 if (ret < 0)
1065 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001066
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001067 msleep(300);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001068
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001069 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1070 if (ret < 0)
1071 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001072
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001073 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1074 if (ret < 0)
1075 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001076
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001077 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1078 if (ret < 0)
1079 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001080
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001081 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1082 if (ret < 0)
1083 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001084
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001085 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1086 if (ret < 0)
1087 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001088
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001089 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1090 if (ret < 0)
1091 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001092
1093 tuner_addr = 0x60;
1094 } else {
1095 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001096 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001097
1098 /* attach tuner */
Antti Palosaaribf97b632012-12-08 22:51:19 -03001099 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1100 tuner_addr, &af9035_mxl5007t_config[adap->id]);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001101 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001102 case AF9033_TUNER_TDA18218:
1103 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001104 fe = dvb_attach(tda18218_attach, adap->fe[0],
1105 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001106 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001107 case AF9033_TUNER_FC2580:
1108 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1109 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1110 if (ret < 0)
1111 goto err;
1112
1113 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1114 if (ret < 0)
1115 goto err;
1116
1117 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1118 if (ret < 0)
1119 goto err;
1120
1121 usleep_range(10000, 50000);
1122 /* attach tuner */
1123 fe = dvb_attach(fc2580_attach, adap->fe[0],
1124 &d->i2c_adap, &af9035_fc2580_config);
1125 break;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001126 case AF9033_TUNER_FC0012:
1127 /*
1128 * AF9035 gpiot2 = FC0012 enable
1129 * XXX: there seems to be something on gpioh8 too, but on my
1130 * my test I didn't find any difference.
1131 */
1132
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001133 if (adap->id == 0) {
1134 /* configure gpiot2 as output and high */
1135 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1136 if (ret < 0)
1137 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001138
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001139 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1140 if (ret < 0)
1141 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001142
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001143 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1144 if (ret < 0)
1145 goto err;
1146 } else {
1147 /*
1148 * FIXME: That belongs for the FC0012 driver.
1149 * Write 02 to FC0012 master tuner register 0d directly
1150 * in order to make slave tuner working.
1151 */
1152 msg[0].addr = 0x63;
1153 msg[0].flags = 0;
1154 msg[0].len = 2;
1155 msg[0].buf = "\x0d\x02";
1156 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1157 if (ret < 0)
1158 goto err;
1159 }
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001160
1161 usleep_range(10000, 50000);
1162
Antti Palosaariad3a7582012-12-08 23:27:49 -03001163 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001164 &af9035_fc0012_config[adap->id]);
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001165 break;
Antti Palosaariac77fb02013-01-07 09:51:13 -03001166 case AF9033_TUNER_IT9135_38:
Antti Palosaari74c18832013-01-07 15:16:54 -03001167 case AF9033_TUNER_IT9135_51:
1168 case AF9033_TUNER_IT9135_52:
1169 case AF9033_TUNER_IT9135_60:
1170 case AF9033_TUNER_IT9135_61:
1171 case AF9033_TUNER_IT9135_62:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001172 /* attach tuner */
1173 fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1174 state->af9033_config[adap->id].i2c_addr,
Antti Palosaari44af7472013-02-28 00:14:06 -03001175 state->af9033_config[0].tuner);
Antti Palosaariac77fb02013-01-07 09:51:13 -03001176 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -03001177 default:
1178 fe = NULL;
1179 }
1180
1181 if (fe == NULL) {
1182 ret = -ENODEV;
1183 goto err;
1184 }
1185
1186 return 0;
1187
1188err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001189 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001190
1191 return ret;
1192}
1193
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001194static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -03001195{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001196 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001197 int ret, i;
Antti Palosaaribada3422013-01-11 20:45:11 -03001198 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1199 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001200 struct reg_val_mask tab[] = {
1201 { 0x80f99d, 0x01, 0x01 },
1202 { 0x80f9a4, 0x01, 0x01 },
1203 { 0x00dd11, 0x00, 0x20 },
1204 { 0x00dd11, 0x00, 0x40 },
1205 { 0x00dd13, 0x00, 0x20 },
1206 { 0x00dd13, 0x00, 0x40 },
1207 { 0x00dd11, 0x20, 0x20 },
1208 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1209 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1210 { 0x00dd0c, packet_size, 0xff},
1211 { 0x00dd11, state->dual_mode << 6, 0x40 },
1212 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1213 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1214 { 0x00dd0d, packet_size, 0xff },
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001215 { 0x80f9a3, state->dual_mode, 0x01 },
1216 { 0x80f9cd, state->dual_mode, 0x01 },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001217 { 0x80f99d, 0x00, 0x01 },
1218 { 0x80f9a4, 0x00, 0x01 },
1219 };
Antti Palosaari7f882c22012-03-30 09:10:08 -03001220
Antti Palosaari119f7a82012-09-12 20:23:53 -03001221 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
1222 "packet_size=%02x\n", __func__,
1223 d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001224
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001225 /* init endpoints */
1226 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1227 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1228 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001229 if (ret < 0)
1230 goto err;
1231 }
1232
1233 return 0;
1234
1235err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001236 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001237
1238 return ret;
1239}
1240
Antti Palosaari37b44a02013-01-04 15:21:26 -03001241#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001242static int af9035_rc_query(struct dvb_usb_device *d)
1243{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001244 int ret;
Antti Palosaari75cd5882013-03-08 17:50:21 -03001245 u32 key;
1246 u8 buf[4];
1247 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001248
1249 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001250 if (ret == 1)
1251 return 0;
1252 else if (ret < 0)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001253 goto err;
1254
Antti Palosaari75cd5882013-03-08 17:50:21 -03001255 if ((buf[2] + buf[3]) == 0xff) {
1256 if ((buf[0] + buf[1]) == 0xff) {
1257 /* NEC standard 16bit */
1258 key = buf[0] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001259 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001260 /* NEC extended 24bit */
1261 key = buf[0] << 16 | buf[1] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001262 }
1263 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001264 /* NEC full code 32bit */
1265 key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001266 }
1267
Antti Palosaari75cd5882013-03-08 17:50:21 -03001268 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1269
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001270 rc_keydown(d->rc_dev, key, 0);
1271
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001272 return 0;
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001273
1274err:
1275 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1276
1277 return ret;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001278}
1279
1280static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1281{
Antti Palosaari74c18832013-01-07 15:16:54 -03001282 struct state *state = d_to_priv(d);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001283 int ret;
1284 u8 tmp;
1285
Antti Palosaari431a6d42013-03-07 18:28:25 -03001286 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &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_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001291
1292 /* don't activate rc if in HID mode or if not available */
1293 if (tmp == 5) {
Antti Palosaari431a6d42013-03-07 18:28:25 -03001294 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
Antti Palosaari9ea36812013-01-11 22:16:25 -03001295 &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001296 if (ret < 0)
1297 goto err;
1298
Antti Palosaari119f7a82012-09-12 20:23:53 -03001299 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001300
1301 switch (tmp) {
1302 case 0: /* NEC */
1303 default:
David Härdemanc003ab12012-10-11 19:11:54 -03001304 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001305 break;
1306 case 1: /* RC6 */
David Härdemanc003ab12012-10-11 19:11:54 -03001307 rc->allowed_protos = RC_BIT_RC6_MCE;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001308 break;
1309 }
1310
1311 rc->query = af9035_rc_query;
1312 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001313
1314 /* load empty to enable rc */
1315 if (!rc->map_name)
1316 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001317 }
1318
1319 return 0;
1320
1321err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001322 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001323
1324 return ret;
1325}
Antti Palosaarieed56702012-12-09 20:18:31 -03001326#else
1327 #define af9035_get_rc_config NULL
1328#endif
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001329
Antti Palosaaribada3422013-01-11 20:45:11 -03001330static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1331 struct usb_data_stream_properties *stream)
1332{
1333 struct dvb_usb_device *d = fe_to_d(fe);
1334 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1335
1336 if (d->udev->speed == USB_SPEED_FULL)
1337 stream->u.bulk.buffersize = 5 * 188;
1338
1339 return 0;
1340}
1341
1342/*
1343 * FIXME: PID filter is property of demodulator and should be moved to the
1344 * correct driver. Also we support only adapter #0 PID filter and will
1345 * disable adapter #1 if USB1.1 is used.
1346 */
1347static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1348{
1349 struct dvb_usb_device *d = adap_to_d(adap);
1350 int ret;
1351
1352 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1353
1354 ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1355 if (ret < 0)
1356 goto err;
1357
1358 return 0;
1359
1360err:
1361 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1362
1363 return ret;
1364}
1365
1366static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1367 int onoff)
1368{
1369 struct dvb_usb_device *d = adap_to_d(adap);
1370 int ret;
1371 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1372
1373 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1374 __func__, index, pid, onoff);
1375
1376 ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1377 if (ret < 0)
1378 goto err;
1379
1380 ret = af9035_wr_reg(d, 0x80f994, onoff);
1381 if (ret < 0)
1382 goto err;
1383
1384 ret = af9035_wr_reg(d, 0x80f995, index);
1385 if (ret < 0)
1386 goto err;
1387
1388 return 0;
1389
1390err:
1391 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1392
1393 return ret;
1394}
1395
Antti Palosaarib799b812013-01-11 16:22:07 -03001396static int af9035_probe(struct usb_interface *intf,
1397 const struct usb_device_id *id)
1398{
1399 struct usb_device *udev = interface_to_usbdev(intf);
1400 char manufacturer[sizeof("Afatech")];
1401
1402 memset(manufacturer, 0, sizeof(manufacturer));
1403 usb_string(udev, udev->descriptor.iManufacturer,
1404 manufacturer, sizeof(manufacturer));
1405 /*
1406 * There is two devices having same ID but different chipset. One uses
1407 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1408 * is iManufacturer string.
1409 *
1410 * idVendor 0x0ccd TerraTec Electronic GmbH
1411 * idProduct 0x0099
1412 * bcdDevice 2.00
1413 * iManufacturer 1 Afatech
1414 * iProduct 2 DVB-T 2
1415 *
1416 * idVendor 0x0ccd TerraTec Electronic GmbH
1417 * idProduct 0x0099
1418 * bcdDevice 2.00
1419 * iManufacturer 1 ITE Technologies, Inc.
1420 * iProduct 2 DVB-T TV Stick
1421 */
1422 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1423 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1424 if (!strcmp("Afatech", manufacturer)) {
1425 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1426 return -ENODEV;
1427 }
1428 }
1429
1430 return dvb_usbv2_probe(intf, id);
1431}
1432
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001433/* interface 0 is used by DVB-T receiver and
1434 interface 1 is for remote controller (HID) */
1435static const struct dvb_usb_device_properties af9035_props = {
1436 .driver_name = KBUILD_MODNAME,
1437 .owner = THIS_MODULE,
1438 .adapter_nr = adapter_nr,
1439 .size_of_priv = sizeof(struct state),
1440
1441 .generic_bulk_ctrl_endpoint = 0x02,
1442 .generic_bulk_ctrl_endpoint_response = 0x81,
1443
1444 .identify_state = af9035_identify_state,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001445 .download_firmware = af9035_download_firmware,
1446
1447 .i2c_algo = &af9035_i2c_algo,
1448 .read_config = af9035_read_config,
1449 .frontend_attach = af9035_frontend_attach,
1450 .tuner_attach = af9035_tuner_attach,
1451 .init = af9035_init,
1452 .get_rc_config = af9035_get_rc_config,
Antti Palosaaribada3422013-01-11 20:45:11 -03001453 .get_stream_config = af9035_get_stream_config,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001454
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001455 .get_adapter_count = af9035_get_adapter_count,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001456 .adapter = {
1457 {
Antti Palosaaribada3422013-01-11 20:45:11 -03001458 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1459 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1460
1461 .pid_filter_count = 32,
1462 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1463 .pid_filter = af9035_pid_filter,
1464
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001465 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1466 }, {
1467 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1468 },
1469 },
1470};
1471
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001472static const struct usb_device_id af9035_id_table[] = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001473 /* AF9035 devices */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001474 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1475 &af9035_props, "Afatech AF9035 reference design", NULL) },
1476 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1477 &af9035_props, "Afatech AF9035 reference design", NULL) },
1478 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1479 &af9035_props, "Afatech AF9035 reference design", NULL) },
1480 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1481 &af9035_props, "Afatech AF9035 reference design", NULL) },
1482 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1483 &af9035_props, "Afatech AF9035 reference design", NULL) },
1484 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1485 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1486 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1487 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1488 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1489 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1490 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1491 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1492 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1493 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1494 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1495 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001496 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1497 &af9035_props, "Asus U3100Mini Plus", NULL) },
Fabrizio Gazzatod9b75952013-02-17 18:25:34 -03001498 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
1499 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001500 /* IT9135 devices */
1501#if 0
1502 { DVB_USB_DEVICE(0x048d, 0x9135,
1503 &af9035_props, "IT9135 reference design", NULL) },
1504 { DVB_USB_DEVICE(0x048d, 0x9006,
1505 &af9035_props, "IT9135 reference design", NULL) },
1506#endif
Antti Palosaarib799b812013-01-11 16:22:07 -03001507 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1508 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1509 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001510 { }
1511};
1512MODULE_DEVICE_TABLE(usb, af9035_id_table);
1513
Antti Palosaari7f882c22012-03-30 09:10:08 -03001514static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001515 .name = KBUILD_MODNAME,
1516 .id_table = af9035_id_table,
Antti Palosaarib799b812013-01-11 16:22:07 -03001517 .probe = af9035_probe,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001518 .disconnect = dvb_usbv2_disconnect,
1519 .suspend = dvb_usbv2_suspend,
1520 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001521 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001522 .no_dynamic_id = 1,
1523 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001524};
1525
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001526module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001527
1528MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1529MODULE_DESCRIPTION("Afatech AF9035 driver");
1530MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001531MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
Antti Palosaari74c18832013-01-07 15:16:54 -03001532MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1533MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);