blob: 4f682ad97bc2592ca2917fc010a15b4bb4a82bc2 [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
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -030024/* Max transfer size done by I2C transfer functions */
25#define MAX_XFER_SIZE 64
26
Antti Palosaari7f882c22012-03-30 09:10:08 -030027DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
Antti Palosaari7f882c22012-03-30 09:10:08 -030028
Michael Büschb1a95992012-04-01 16:33:48 -030029static u16 af9035_checksum(const u8 *buf, size_t len)
30{
31 size_t i;
32 u16 checksum = 0;
33
34 for (i = 1; i < len; i++) {
35 if (i % 2)
36 checksum += buf[i] << 8;
37 else
38 checksum += buf[i];
39 }
40 checksum = ~checksum;
41
42 return checksum;
43}
44
Antti Palosaari5da2aec2012-06-17 23:15:03 -030045static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
Antti Palosaari7f882c22012-03-30 09:10:08 -030046{
Antti Palosaari7f882c22012-03-30 09:10:08 -030047#define REQ_HDR_LEN 4 /* send header size */
48#define ACK_HDR_LEN 3 /* rece header size */
49#define CHECKSUM_LEN 2
50#define USB_TIMEOUT 2000
Antti Palosaari5da2aec2012-06-17 23:15:03 -030051 struct state *state = d_to_priv(d);
52 int ret, wlen, rlen;
Antti Palosaari6fb39c52012-04-06 16:32:30 -030053 u16 checksum, tmp_checksum;
Antti Palosaari7f882c22012-03-30 09:10:08 -030054
Antti Palosaari3484d372013-02-26 13:56:34 -030055 mutex_lock(&d->usb_mutex);
56
Antti Palosaari7f882c22012-03-30 09:10:08 -030057 /* buffer overflow check */
58 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
Antti Palosaari119f7a82012-09-12 20:23:53 -030059 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
60 dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
Antti Palosaarie8292e22013-06-03 18:50:43 -030061 KBUILD_MODNAME, req->wlen, req->rlen);
Antti Palosaari3484d372013-02-26 13:56:34 -030062 ret = -EINVAL;
Wei Yongjun0170a392013-03-26 01:32:19 -030063 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -030064 }
65
Antti Palosaari3484d372013-02-26 13:56:34 -030066 state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
67 state->buf[1] = req->mbox;
68 state->buf[2] = req->cmd;
69 state->buf[3] = state->seq++;
70 memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -030071
72 wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
73 rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
Antti Palosaari7f882c22012-03-30 09:10:08 -030074
75 /* calc and add checksum */
Antti Palosaari3484d372013-02-26 13:56:34 -030076 checksum = af9035_checksum(state->buf, state->buf[0] - 1);
77 state->buf[state->buf[0] - 1] = (checksum >> 8);
78 state->buf[state->buf[0] - 0] = (checksum & 0xff);
Antti Palosaari7f882c22012-03-30 09:10:08 -030079
Antti Palosaari5da2aec2012-06-17 23:15:03 -030080 /* no ack for these packets */
81 if (req->cmd == CMD_FW_DL)
82 rlen = 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -030083
Antti Palosaari3484d372013-02-26 13:56:34 -030084 ret = dvb_usbv2_generic_rw_locked(d,
85 state->buf, wlen, state->buf, rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -030086 if (ret)
Wei Yongjun0170a392013-03-26 01:32:19 -030087 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -030088
89 /* no ack for those packets */
90 if (req->cmd == CMD_FW_DL)
Antti Palosaari5da2aec2012-06-17 23:15:03 -030091 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -030092
Michael Büschb1a95992012-04-01 16:33:48 -030093 /* verify checksum */
Antti Palosaari3484d372013-02-26 13:56:34 -030094 checksum = af9035_checksum(state->buf, rlen - 2);
95 tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
Antti Palosaari6fb39c52012-04-06 16:32:30 -030096 if (tmp_checksum != checksum) {
Antti Palosaaricb9114e2013-06-03 18:42:14 -030097 dev_err(&d->udev->dev,
98 "%s: command=%02x checksum mismatch (%04x != %04x)\n",
99 KBUILD_MODNAME, req->cmd, tmp_checksum,
100 checksum);
Michael Büschb1a95992012-04-01 16:33:48 -0300101 ret = -EIO;
Wei Yongjun0170a392013-03-26 01:32:19 -0300102 goto exit;
Michael Büschb1a95992012-04-01 16:33:48 -0300103 }
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300104
Antti Palosaari7f882c22012-03-30 09:10:08 -0300105 /* check status */
Antti Palosaari3484d372013-02-26 13:56:34 -0300106 if (state->buf[2]) {
Antti Palosaari1bfd5292013-03-08 17:39:12 -0300107 /* fw returns status 1 when IR code was not received */
Wei Yongjun0170a392013-03-26 01:32:19 -0300108 if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
109 ret = 1;
110 goto exit;
111 }
Antti Palosaari1bfd5292013-03-08 17:39:12 -0300112
Antti Palosaari119f7a82012-09-12 20:23:53 -0300113 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
Antti Palosaari3484d372013-02-26 13:56:34 -0300114 __func__, req->cmd, state->buf[2]);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300115 ret = -EIO;
Wei Yongjun0170a392013-03-26 01:32:19 -0300116 goto exit;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300117 }
118
119 /* read request, copy returned data to return buf */
120 if (req->rlen)
Antti Palosaari3484d372013-02-26 13:56:34 -0300121 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300122exit:
Antti Palosaari3484d372013-02-26 13:56:34 -0300123 mutex_unlock(&d->usb_mutex);
Wei Yongjun0170a392013-03-26 01:32:19 -0300124 if (ret < 0)
Antti Palosaari3484d372013-02-26 13:56:34 -0300125 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300126 return ret;
127}
128
129/* write multiple registers */
130static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
131{
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -0300132 u8 wbuf[MAX_XFER_SIZE];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300133 u8 mbox = (reg >> 16) & 0xff;
Antti Palosaari93232972013-11-27 17:23:00 -0300134 struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL };
Antti Palosaari7f882c22012-03-30 09:10:08 -0300135
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -0300136 if (6 + len > sizeof(wbuf)) {
137 dev_warn(&d->udev->dev, "%s: i2c wr: len=%d is too big!\n",
138 KBUILD_MODNAME, len);
139 return -EOPNOTSUPP;
140 }
141
Antti Palosaari7f882c22012-03-30 09:10:08 -0300142 wbuf[0] = len;
143 wbuf[1] = 2;
144 wbuf[2] = 0;
145 wbuf[3] = 0;
146 wbuf[4] = (reg >> 8) & 0xff;
147 wbuf[5] = (reg >> 0) & 0xff;
148 memcpy(&wbuf[6], val, len);
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/* read multiple registers */
154static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
155{
156 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
157 u8 mbox = (reg >> 16) & 0xff;
158 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
159
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300160 return af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300161}
162
163/* write single register */
164static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
165{
166 return af9035_wr_regs(d, reg, &val, 1);
167}
168
169/* read single register */
170static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
171{
172 return af9035_rd_regs(d, reg, val, 1);
173}
174
175/* write single register with mask */
176static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
177 u8 mask)
178{
179 int ret;
180 u8 tmp;
181
182 /* no need for read if whole reg is written */
183 if (mask != 0xff) {
184 ret = af9035_rd_regs(d, reg, &tmp, 1);
185 if (ret)
186 return ret;
187
188 val &= mask;
189 tmp &= ~mask;
190 val |= tmp;
191 }
192
193 return af9035_wr_regs(d, reg, &val, 1);
194}
195
196static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
197 struct i2c_msg msg[], int num)
198{
199 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300200 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300201 int ret;
202
203 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
204 return -EAGAIN;
205
Antti Palosaariad30e912012-04-02 20:18:59 -0300206 /*
207 * I2C sub header is 5 bytes long. Meaning of those bytes are:
208 * 0: data len
209 * 1: I2C addr << 1
210 * 2: reg addr len
211 * byte 3 and 4 can be used as reg addr
212 * 3: reg addr MSB
213 * used when reg addr len is set to 2
214 * 4: reg addr LSB
215 * used when reg addr len is set to 1 or 2
216 *
217 * For the simplify we do not use register addr at all.
218 * NOTE: As a firmware knows tuner type there is very small possibility
219 * there could be some tuner I2C hacks done by firmware and this may
220 * lead problems if firmware expects those bytes are used.
221 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300222 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
223 (msg[1].flags & I2C_M_RD)) {
224 if (msg[0].len > 40 || msg[1].len > 40) {
225 /* TODO: correct limits > 40 */
226 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300227 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
228 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300229 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300230 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
231 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300232
233 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300234 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300235
Antti Palosaari7f882c22012-03-30 09:10:08 -0300236 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
237 msg[1].len);
238 } else {
239 /* I2C */
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -0300240 u8 buf[MAX_XFER_SIZE];
Antti Palosaari93232972013-11-27 17:23:00 -0300241 struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300242 buf, msg[1].len, msg[1].buf };
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -0300243
244 if (5 + msg[0].len > sizeof(buf)) {
245 dev_warn(&d->udev->dev,
246 "%s: i2c xfer: len=%d is too big!\n",
247 KBUILD_MODNAME, msg[0].len);
Dan Carpenter3189ef02013-11-22 04:50:46 -0300248 ret = -EOPNOTSUPP;
249 goto unlock;
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -0300250 }
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300251 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300252 buf[0] = msg[1].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300253 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300254 buf[2] = 0x00; /* reg addr len */
255 buf[3] = 0x00; /* reg addr MSB */
256 buf[4] = 0x00; /* reg addr LSB */
257 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300258 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300259 }
260 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
261 if (msg[0].len > 40) {
262 /* TODO: correct limits > 40 */
263 ret = -EOPNOTSUPP;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300264 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
265 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
Antti Palosaaribf97b632012-12-08 22:51:19 -0300266 /* demod access via firmware interface */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300267 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
268 msg[0].buf[2];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300269
270 if (msg[0].addr == state->af9033_config[1].i2c_addr)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300271 reg |= 0x100000;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300272
Antti Palosaari7f882c22012-03-30 09:10:08 -0300273 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
274 msg[0].len - 3);
275 } else {
276 /* I2C */
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -0300277 u8 buf[MAX_XFER_SIZE];
Antti Palosaari93232972013-11-27 17:23:00 -0300278 struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len,
279 buf, 0, NULL };
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -0300280
281 if (5 + msg[0].len > sizeof(buf)) {
282 dev_warn(&d->udev->dev,
283 "%s: i2c xfer: len=%d is too big!\n",
284 KBUILD_MODNAME, msg[0].len);
Dan Carpenter3189ef02013-11-22 04:50:46 -0300285 ret = -EOPNOTSUPP;
286 goto unlock;
Mauro Carvalho Chehab7760e142013-11-02 08:07:12 -0300287 }
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300288 req.mbox |= ((msg[0].addr & 0x80) >> 3);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300289 buf[0] = msg[0].len;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300290 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300291 buf[2] = 0x00; /* reg addr len */
292 buf[3] = 0x00; /* reg addr MSB */
293 buf[4] = 0x00; /* reg addr LSB */
294 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300295 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300296 }
Antti Palosaari3b98c342013-03-11 19:05:39 -0300297 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
298 if (msg[0].len > 40) {
299 /* TODO: correct limits > 40 */
300 ret = -EOPNOTSUPP;
301 } else {
302 /* I2C */
303 u8 buf[5];
304 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
305 buf, msg[0].len, msg[0].buf };
306 req.mbox |= ((msg[0].addr & 0x80) >> 3);
307 buf[0] = msg[0].len;
308 buf[1] = msg[0].addr << 1;
309 buf[2] = 0x00; /* reg addr len */
310 buf[3] = 0x00; /* reg addr MSB */
311 buf[4] = 0x00; /* reg addr LSB */
312 ret = af9035_ctrl_msg(d, &req);
313 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300314 } else {
315 /*
Antti Palosaari3b98c342013-03-11 19:05:39 -0300316 * We support only three kind of I2C transactions:
317 * 1) 1 x read + 1 x write (repeated start)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300318 * 2) 1 x write
Antti Palosaari3b98c342013-03-11 19:05:39 -0300319 * 3) 1 x read
Antti Palosaari7f882c22012-03-30 09:10:08 -0300320 */
321 ret = -EOPNOTSUPP;
322 }
323
Dan Carpenter3189ef02013-11-22 04:50:46 -0300324unlock:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300325 mutex_unlock(&d->i2c_mutex);
326
327 if (ret < 0)
328 return ret;
329 else
330 return num;
331}
332
333static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
334{
335 return I2C_FUNC_I2C;
336}
337
338static struct i2c_algorithm af9035_i2c_algo = {
339 .master_xfer = af9035_i2c_master_xfer,
340 .functionality = af9035_i2c_functionality,
341};
342
Antti Palosaaria0921af2012-06-18 23:42:53 -0300343static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300344{
Antti Palosaari74c18832013-01-07 15:16:54 -0300345 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300346 int ret;
347 u8 wbuf[1] = { 1 };
348 u8 rbuf[4];
349 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
350 sizeof(rbuf), rbuf };
351
Antti Palosaari74c18832013-01-07 15:16:54 -0300352 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
353 if (ret < 0)
354 goto err;
355
356 state->chip_version = rbuf[0];
357 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
358
359 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
360 if (ret < 0)
361 goto err;
362
363 dev_info(&d->udev->dev,
364 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
Antti Palosaarie8292e22013-06-03 18:50:43 -0300365 KBUILD_MODNAME, state->prechip_version,
366 state->chip_version, state->chip_type);
Antti Palosaari74c18832013-01-07 15:16:54 -0300367
368 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300369 if (state->chip_version == 0x02)
Antti Palosaari74c18832013-01-07 15:16:54 -0300370 *name = AF9035_FIRMWARE_IT9135_V2;
371 else
372 *name = AF9035_FIRMWARE_IT9135_V1;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300373 state->eeprom_addr = EEPROM_BASE_IT9135;
Antti Palosaari74c18832013-01-07 15:16:54 -0300374 } else {
375 *name = AF9035_FIRMWARE_AF9035;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300376 state->eeprom_addr = EEPROM_BASE_AF9035;
Antti Palosaari74c18832013-01-07 15:16:54 -0300377 }
378
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300379 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300380 if (ret < 0)
381 goto err;
382
Antti Palosaari119f7a82012-09-12 20:23:53 -0300383 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300384 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300385 ret = WARM;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300386 else
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300387 ret = COLD;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300388
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300389 return ret;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300390
391err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300392 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300393
394 return ret;
395}
396
Antti Palosaari8229da52013-03-07 17:59:55 -0300397static int af9035_download_firmware_old(struct dvb_usb_device *d,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300398 const struct firmware *fw)
399{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300400 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300401 u8 wbuf[1];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300402 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
403 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300404 u8 hdr_core;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300405 u16 hdr_addr, hdr_data_len, hdr_checksum;
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300406 #define MAX_DATA 58
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300407 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300408
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300409 /*
410 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
411 *
412 * byte 0: MCS 51 core
413 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
414 * address spaces
415 * byte 1-2: Big endian destination address
416 * byte 3-4: Big endian number of data bytes following the header
417 * byte 5-6: Big endian header checksum, apparently ignored by the chip
418 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
419 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300420
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300421 for (i = fw->size; i > HDR_SIZE;) {
422 hdr_core = fw->data[fw->size - i + 0];
423 hdr_addr = fw->data[fw->size - i + 1] << 8;
424 hdr_addr |= fw->data[fw->size - i + 2] << 0;
425 hdr_data_len = fw->data[fw->size - i + 3] << 8;
426 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
427 hdr_checksum = fw->data[fw->size - i + 5] << 8;
428 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300429
Antti Palosaaricb9114e2013-06-03 18:42:14 -0300430 dev_dbg(&d->udev->dev,
431 "%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
432 __func__, hdr_core, hdr_addr, hdr_data_len,
433 hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300434
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300435 if (((hdr_core != 1) && (hdr_core != 2)) ||
436 (hdr_data_len > i)) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300437 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300438 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300439 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300440
441 /* download begin packet */
442 req.cmd = CMD_FW_DL_BEGIN;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300443 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300444 if (ret < 0)
445 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300446
447 /* download firmware packet(s) */
448 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
449 len = j;
450 if (len > MAX_DATA)
451 len = MAX_DATA;
452 req_fw_dl.wlen = len;
453 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
454 HDR_SIZE + hdr_data_len - j];
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300455 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300456 if (ret < 0)
457 goto err;
458 }
459
460 /* download end packet */
461 req.cmd = CMD_FW_DL_END;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300462 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300463 if (ret < 0)
464 goto err;
465
466 i -= hdr_data_len + HDR_SIZE;
467
Antti Palosaari119f7a82012-09-12 20:23:53 -0300468 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
469 __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300470 }
471
Antti Palosaariff4e3fe2012-12-09 16:25:08 -0300472 /* print warn if firmware is bad, continue and see what happens */
473 if (i)
474 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
475
Antti Palosaari7f882c22012-03-30 09:10:08 -0300476 return 0;
477
478err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300479 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300480
481 return ret;
482}
483
Antti Palosaari8229da52013-03-07 17:59:55 -0300484static int af9035_download_firmware_new(struct dvb_usb_device *d,
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300485 const struct firmware *fw)
486{
487 int ret, i, i_prev;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300488 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300489 #define HDR_SIZE 7
490
491 /*
492 * There seems to be following firmware header. Meaning of bytes 0-3
493 * is unknown.
494 *
495 * 0: 3
496 * 1: 0, 1
497 * 2: 0
498 * 3: 1, 2, 3
499 * 4: addr MSB
500 * 5: addr LSB
501 * 6: count of data bytes ?
502 */
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300503 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
504 if (i == fw->size ||
505 (fw->data[i + 0] == 0x03 &&
506 (fw->data[i + 1] == 0x00 ||
507 fw->data[i + 1] == 0x01) &&
508 fw->data[i + 2] == 0x00)) {
509 req_fw_dl.wlen = i - i_prev;
510 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
511 i_prev = i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300512 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300513 if (ret < 0)
514 goto err;
515
Antti Palosaari119f7a82012-09-12 20:23:53 -0300516 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
517 __func__, i);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300518 }
519 }
520
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300521 return 0;
522
523err:
524 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
525
526 return ret;
527}
528
529static int af9035_download_firmware(struct dvb_usb_device *d,
530 const struct firmware *fw)
531{
532 struct state *state = d_to_priv(d);
533 int ret;
534 u8 wbuf[1];
535 u8 rbuf[4];
536 u8 tmp;
537 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
Antti Palosaaricb9114e2013-06-03 18:42:14 -0300538 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300539 dev_dbg(&d->udev->dev, "%s:\n", __func__);
540
541 /*
542 * In case of dual tuner configuration we need to do some extra
543 * initialization in order to download firmware to slave demod too,
544 * which is done by master demod.
545 * Master feeds also clock and controls power via GPIO.
546 */
Antti Palosaarid716ef42013-06-03 19:39:51 -0300547 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300548 if (ret < 0)
549 goto err;
550
Antti Palosaarid716ef42013-06-03 19:39:51 -0300551 if (tmp == 1 || tmp == 3) {
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300552 /* configure gpioh1, reset & power slave demod */
553 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
554 if (ret < 0)
555 goto err;
556
557 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
558 if (ret < 0)
559 goto err;
560
561 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
562 if (ret < 0)
563 goto err;
564
565 usleep_range(10000, 50000);
566
567 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
568 if (ret < 0)
569 goto err;
570
571 /* tell the slave I2C address */
572 ret = af9035_rd_reg(d,
573 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
574 &tmp);
575 if (ret < 0)
576 goto err;
577
578 if (state->chip_type == 0x9135) {
Malcolm Priestleya1310ff2014-02-09 10:02:49 -0300579 if (!tmp)
580 /* default 0x9135 slave I2C address */
581 tmp = 0x3a;
582
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300583 ret = af9035_wr_reg(d, 0x004bfb, tmp);
584 if (ret < 0)
585 goto err;
586 } else {
587 ret = af9035_wr_reg(d, 0x00417f, tmp);
588 if (ret < 0)
589 goto err;
590
591 /* enable clock out */
592 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
593 if (ret < 0)
594 goto err;
595 }
596 }
597
Antti Palosaari8229da52013-03-07 17:59:55 -0300598 if (fw->data[0] == 0x01)
599 ret = af9035_download_firmware_old(d, fw);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300600 else
Antti Palosaari8229da52013-03-07 17:59:55 -0300601 ret = af9035_download_firmware_new(d, fw);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300602 if (ret < 0)
603 goto err;
604
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300605 /* firmware loaded, request boot */
606 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300607 ret = af9035_ctrl_msg(d, &req);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300608 if (ret < 0)
609 goto err;
610
611 /* ensure firmware starts */
612 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300613 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300614 if (ret < 0)
615 goto err;
616
617 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300618 dev_err(&d->udev->dev, "%s: firmware did not run\n",
619 KBUILD_MODNAME);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300620 ret = -ENODEV;
621 goto err;
622 }
623
Antti Palosaari119f7a82012-09-12 20:23:53 -0300624 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
625 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300626
627 return 0;
628
629err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300630 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300631
632 return ret;
633}
634
Antti Palosaari9ea36812013-01-11 22:16:25 -0300635static int af9035_read_config(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300636{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300637 struct state *state = d_to_priv(d);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300638 int ret, i;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300639 u8 tmp;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300640 u16 tmp16, addr;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300641
Antti Palosaaribf97b632012-12-08 22:51:19 -0300642 /* demod I2C "address" */
643 state->af9033_config[0].i2c_addr = 0x38;
Antti Palosaari0d94d6a2013-01-07 15:26:05 -0300644 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300645 state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaariab56ad62013-03-07 18:43:51 -0300646 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
647 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300648
Antti Palosaari9ea36812013-01-11 22:16:25 -0300649 /* eeprom memory mapped location */
650 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300651 if (state->chip_version == 0x02) {
652 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300653 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300654 tmp16 = 0x00461d;
655 } else {
656 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300657 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300658 tmp16 = 0x00461b;
659 }
660
Antti Palosaari9ea36812013-01-11 22:16:25 -0300661 /* check if eeprom exists */
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300662 ret = af9035_rd_reg(d, tmp16, &tmp);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300663 if (ret < 0)
664 goto err;
665
Antti Palosaari431a6d42013-03-07 18:28:25 -0300666 if (tmp == 0x00) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300667 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300668 goto skip_eeprom;
669 }
Antti Palosaari9ea36812013-01-11 22:16:25 -0300670 }
671
Antti Palosaari7f882c22012-03-30 09:10:08 -0300672 /* check if there is dual tuners */
Antti Palosaarid716ef42013-06-03 19:39:51 -0300673 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300674 if (ret < 0)
675 goto err;
676
Antti Palosaarid716ef42013-06-03 19:39:51 -0300677 if (tmp == 1 || tmp == 3)
678 state->dual_mode = true;
679
680 dev_dbg(&d->udev->dev, "%s: ts mode=%d dual mode=%d\n", __func__,
681 tmp, state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300682
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300683 if (state->dual_mode) {
684 /* read 2nd demodulator I2C address */
Antti Palosaari431a6d42013-03-07 18:28:25 -0300685 ret = af9035_rd_reg(d,
686 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
687 &tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300688 if (ret < 0)
689 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300690
Malcolm Priestleya1310ff2014-02-09 10:02:49 -0300691 if (!tmp && state->chip_type == 0x9135)
692 /* default 0x9135 slave I2C address */
693 tmp = 0x3a;
694
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300695 state->af9033_config[1].i2c_addr = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300696 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
697 __func__, tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300698 }
699
Antti Palosaari431a6d42013-03-07 18:28:25 -0300700 addr = state->eeprom_addr;
701
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300702 for (i = 0; i < state->dual_mode + 1; i++) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300703 /* tuner */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300704 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300705 if (ret < 0)
706 goto err;
707
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300708 if (tmp == 0x00)
709 dev_dbg(&d->udev->dev,
710 "%s: [%d]tuner not set, using default\n",
711 __func__, i);
712 else
713 state->af9033_config[i].tuner = tmp;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300714
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300715 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
716 __func__, i, state->af9033_config[i].tuner);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300717
718 switch (state->af9033_config[i].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300719 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300720 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300721 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300722 case AF9033_TUNER_TDA18218:
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300723 case AF9033_TUNER_FC2580:
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300724 case AF9033_TUNER_FC0012:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300725 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300726 break;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300727 case AF9033_TUNER_IT9135_38:
728 case AF9033_TUNER_IT9135_51:
729 case AF9033_TUNER_IT9135_52:
730 case AF9033_TUNER_IT9135_60:
731 case AF9033_TUNER_IT9135_61:
732 case AF9033_TUNER_IT9135_62:
733 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300734 default:
Antti Palosaari9ea36812013-01-11 22:16:25 -0300735 dev_warn(&d->udev->dev,
736 "%s: tuner id=%02x not supported, please report!",
Antti Palosaari119f7a82012-09-12 20:23:53 -0300737 KBUILD_MODNAME, tmp);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300738 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300739
Antti Palosaaribf97b632012-12-08 22:51:19 -0300740 /* disable dual mode if driver does not support it */
741 if (i == 1)
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300742 switch (state->af9033_config[i].tuner) {
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300743 case AF9033_TUNER_FC0012:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300744 case AF9033_TUNER_IT9135_38:
745 case AF9033_TUNER_IT9135_51:
746 case AF9033_TUNER_IT9135_52:
747 case AF9033_TUNER_IT9135_60:
748 case AF9033_TUNER_IT9135_61:
749 case AF9033_TUNER_IT9135_62:
Jose Alberto Reguero78c7bc42013-02-10 15:43:03 -0300750 case AF9033_TUNER_MXL5007T:
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300751 break;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300752 default:
753 state->dual_mode = false;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300754 dev_info(&d->udev->dev,
755 "%s: driver does not support 2nd tuner and will disable it",
756 KBUILD_MODNAME);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300757 }
758
Antti Palosaari7f882c22012-03-30 09:10:08 -0300759 /* tuner IF frequency */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300760 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300761 if (ret < 0)
762 goto err;
763
764 tmp16 = tmp;
765
Antti Palosaari9ea36812013-01-11 22:16:25 -0300766 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300767 if (ret < 0)
768 goto err;
769
770 tmp16 |= tmp << 8;
771
Antti Palosaari119f7a82012-09-12 20:23:53 -0300772 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300773
Antti Palosaari9ea36812013-01-11 22:16:25 -0300774 addr += 0x10; /* shift for the 2nd tuner params */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300775 }
776
Antti Palosaari9ea36812013-01-11 22:16:25 -0300777skip_eeprom:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300778 /* get demod clock */
779 ret = af9035_rd_reg(d, 0x00d800, &tmp);
780 if (ret < 0)
781 goto err;
782
783 tmp = (tmp >> 0) & 0x0f;
784
Antti Palosaari9ea36812013-01-11 22:16:25 -0300785 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
786 if (state->chip_type == 0x9135)
787 state->af9033_config[i].clock = clock_lut_it9135[tmp];
788 else
789 state->af9033_config[i].clock = clock_lut_af9035[tmp];
Antti Palosaari74c18832013-01-07 15:16:54 -0300790 }
791
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300792 return 0;
793
794err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300795 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300796
797 return ret;
798}
799
Antti Palosaari51639be2012-09-16 22:26:56 -0300800static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
801 int cmd, int arg)
802{
803 int ret;
804 u8 val;
805
806 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
807
808 /*
809 * CEN always enabled by hardware wiring
810 * RESETN GPIOT3
811 * RXEN GPIOT2
812 */
813
814 switch (cmd) {
815 case TUA9001_CMD_RESETN:
816 if (arg)
817 val = 0x00;
818 else
819 val = 0x01;
820
821 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
822 if (ret < 0)
823 goto err;
824 break;
825 case TUA9001_CMD_RXEN:
826 if (arg)
827 val = 0x01;
828 else
829 val = 0x00;
830
831 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
832 if (ret < 0)
833 goto err;
834 break;
835 }
836
837 return 0;
838
839err:
840 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
841
842 return ret;
843}
844
845
Michael Büschffc501f2012-04-02 12:18:36 -0300846static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300847 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300848{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300849 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300850
851 switch (cmd) {
852 case FC0011_FE_CALLBACK_POWER:
853 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300854 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
855 if (ret < 0)
856 goto err;
857
858 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
859 if (ret < 0)
860 goto err;
861
862 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
863 if (ret < 0)
864 goto err;
865
Michael Büschffc501f2012-04-02 12:18:36 -0300866 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300867 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
868 if (ret < 0)
869 goto err;
870
871 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
872 if (ret < 0)
873 goto err;
874
Michael Büschde2bec52012-04-03 05:11:30 -0300875 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300876 break;
877 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300878 ret = af9035_wr_reg(d, 0xd8e9, 1);
879 if (ret < 0)
880 goto err;
881
882 ret = af9035_wr_reg(d, 0xd8e8, 1);
883 if (ret < 0)
884 goto err;
885
886 ret = af9035_wr_reg(d, 0xd8e7, 1);
887 if (ret < 0)
888 goto err;
889
Michael Büschde2bec52012-04-03 05:11:30 -0300890 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300891
892 ret = af9035_wr_reg(d, 0xd8e7, 0);
893 if (ret < 0)
894 goto err;
895
Michael Büschde2bec52012-04-03 05:11:30 -0300896 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300897 break;
898 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300899 ret = -EINVAL;
900 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300901 }
902
903 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300904
905err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300906 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300907
908 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300909}
910
911static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
912{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300913 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300914
915 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300916 case AF9033_TUNER_FC0011:
917 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300918 case AF9033_TUNER_TUA9001:
919 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300920 default:
921 break;
922 }
923
Antti Palosaari1835af12012-09-11 22:27:06 -0300924 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300925}
926
927static int af9035_frontend_callback(void *adapter_priv, int component,
928 int cmd, int arg)
929{
930 struct i2c_adapter *adap = adapter_priv;
931 struct dvb_usb_device *d = i2c_get_adapdata(adap);
932
Antti Palosaari1835af12012-09-11 22:27:06 -0300933 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
934 __func__, component, cmd, arg);
935
Michael Büschffc501f2012-04-02 12:18:36 -0300936 switch (component) {
937 case DVB_FRONTEND_COMPONENT_TUNER:
938 return af9035_tuner_callback(d, cmd, arg);
939 default:
940 break;
941 }
942
Antti Palosaari1835af12012-09-11 22:27:06 -0300943 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300944}
945
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300946static int af9035_get_adapter_count(struct dvb_usb_device *d)
947{
948 struct state *state = d_to_priv(d);
Antti Palosaaribada3422013-01-11 20:45:11 -0300949
950 /* disable 2nd adapter as we don't have PID filters implemented */
951 if (d->udev->speed == USB_SPEED_FULL)
952 return 1;
953 else
954 return state->dual_mode + 1;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300955}
956
Antti Palosaari7f882c22012-03-30 09:10:08 -0300957static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
958{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300959 struct state *state = adap_to_priv(adap);
960 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300961 int ret;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300962 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300963
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300964 if (!state->af9033_config[adap->id].tuner) {
965 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300966 ret = -ENODEV;
967 goto err;
968 }
969
Antti Palosaari7f882c22012-03-30 09:10:08 -0300970 /* attach demodulator */
Antti Palosaaribf97b632012-12-08 22:51:19 -0300971 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
972 &d->i2c_adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300973 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300974 ret = -ENODEV;
975 goto err;
976 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300977
978 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300979 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
980 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300981
982 return 0;
983
984err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300985 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300986
987 return ret;
988}
989
990static struct tua9001_config af9035_tua9001_config = {
991 .i2c_addr = 0x60,
992};
993
Michael Büschffc501f2012-04-02 12:18:36 -0300994static const struct fc0011_config af9035_fc0011_config = {
995 .i2c_address = 0x60,
996};
997
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300998static struct mxl5007t_config af9035_mxl5007t_config[] = {
999 {
1000 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1001 .if_freq_hz = MxL_IF_4_57_MHZ,
1002 .invert_if = 0,
1003 .loop_thru_enable = 0,
1004 .clk_out_enable = 0,
1005 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1006 }, {
1007 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1008 .if_freq_hz = MxL_IF_4_57_MHZ,
1009 .invert_if = 0,
1010 .loop_thru_enable = 1,
1011 .clk_out_enable = 1,
1012 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1013 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001014};
1015
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001016static struct tda18218_config af9035_tda18218_config = {
1017 .i2c_address = 0x60,
1018 .i2c_wr_max = 21,
1019};
1020
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001021static const struct fc2580_config af9035_fc2580_config = {
1022 .i2c_addr = 0x56,
1023 .clock = 16384000,
1024};
1025
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001026static const struct fc0012_config af9035_fc0012_config[] = {
1027 {
1028 .i2c_address = 0x63,
1029 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -03001030 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001031 .loop_through = true,
1032 .clock_out = true,
1033 }, {
1034 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1035 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -03001036 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001037 }
Antti Palosaariad3a7582012-12-08 23:27:49 -03001038};
1039
Antti Palosaari7f882c22012-03-30 09:10:08 -03001040static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1041{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001042 struct state *state = adap_to_priv(adap);
1043 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001044 int ret;
1045 struct dvb_frontend *fe;
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001046 struct i2c_msg msg[1];
Antti Palosaaribf97b632012-12-08 22:51:19 -03001047 u8 tuner_addr;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001048 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1049
Antti Palosaaribf97b632012-12-08 22:51:19 -03001050 /*
1051 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1052 * to carry info about used I2C bus for dual tuner configuration.
1053 */
Antti Palosaari7f882c22012-03-30 09:10:08 -03001054
Antti Palosaari2a79eef2012-05-07 14:50:40 -03001055 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -03001056 case AF9033_TUNER_TUA9001:
1057 /* AF9035 gpiot3 = TUA9001 RESETN
1058 AF9035 gpiot2 = TUA9001 RXEN */
1059
1060 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001061 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001062 if (ret < 0)
1063 goto err;
1064
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001065 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001066 if (ret < 0)
1067 goto err;
1068
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001069 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001070 if (ret < 0)
1071 goto err;
1072
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001073 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001074 if (ret < 0)
1075 goto err;
1076
Antti Palosaari7f882c22012-03-30 09:10:08 -03001077 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001078 fe = dvb_attach(tua9001_attach, adap->fe[0],
1079 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001080 break;
Michael Büschffc501f2012-04-02 12:18:36 -03001081 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001082 fe = dvb_attach(fc0011_attach, adap->fe[0],
1083 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -03001084 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001085 case AF9033_TUNER_MXL5007T:
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001086 if (adap->id == 0) {
1087 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1088 if (ret < 0)
1089 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001090
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001091 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1092 if (ret < 0)
1093 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001094
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001095 ret = af9035_wr_reg(d, 0x00d8df, 0);
1096 if (ret < 0)
1097 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001098
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001099 msleep(30);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001100
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001101 ret = af9035_wr_reg(d, 0x00d8df, 1);
1102 if (ret < 0)
1103 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001104
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001105 msleep(300);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001106
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001107 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1108 if (ret < 0)
1109 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001110
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001111 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1112 if (ret < 0)
1113 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001114
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001115 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1116 if (ret < 0)
1117 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001118
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001119 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1120 if (ret < 0)
1121 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001122
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001123 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1124 if (ret < 0)
1125 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001126
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001127 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1128 if (ret < 0)
1129 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001130
1131 tuner_addr = 0x60;
1132 } else {
1133 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001134 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001135
1136 /* attach tuner */
Antti Palosaaribf97b632012-12-08 22:51:19 -03001137 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1138 tuner_addr, &af9035_mxl5007t_config[adap->id]);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001139 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001140 case AF9033_TUNER_TDA18218:
1141 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001142 fe = dvb_attach(tda18218_attach, adap->fe[0],
1143 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001144 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001145 case AF9033_TUNER_FC2580:
1146 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1147 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1148 if (ret < 0)
1149 goto err;
1150
1151 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1152 if (ret < 0)
1153 goto err;
1154
1155 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1156 if (ret < 0)
1157 goto err;
1158
1159 usleep_range(10000, 50000);
1160 /* attach tuner */
1161 fe = dvb_attach(fc2580_attach, adap->fe[0],
1162 &d->i2c_adap, &af9035_fc2580_config);
1163 break;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001164 case AF9033_TUNER_FC0012:
1165 /*
1166 * AF9035 gpiot2 = FC0012 enable
1167 * XXX: there seems to be something on gpioh8 too, but on my
1168 * my test I didn't find any difference.
1169 */
1170
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001171 if (adap->id == 0) {
1172 /* configure gpiot2 as output and high */
1173 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1174 if (ret < 0)
1175 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001176
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001177 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1178 if (ret < 0)
1179 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001180
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001181 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1182 if (ret < 0)
1183 goto err;
1184 } else {
1185 /*
1186 * FIXME: That belongs for the FC0012 driver.
1187 * Write 02 to FC0012 master tuner register 0d directly
1188 * in order to make slave tuner working.
1189 */
1190 msg[0].addr = 0x63;
1191 msg[0].flags = 0;
1192 msg[0].len = 2;
1193 msg[0].buf = "\x0d\x02";
1194 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1195 if (ret < 0)
1196 goto err;
1197 }
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001198
1199 usleep_range(10000, 50000);
1200
Antti Palosaariad3a7582012-12-08 23:27:49 -03001201 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001202 &af9035_fc0012_config[adap->id]);
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001203 break;
Antti Palosaariac77fb02013-01-07 09:51:13 -03001204 case AF9033_TUNER_IT9135_38:
Antti Palosaari74c18832013-01-07 15:16:54 -03001205 case AF9033_TUNER_IT9135_51:
1206 case AF9033_TUNER_IT9135_52:
1207 case AF9033_TUNER_IT9135_60:
1208 case AF9033_TUNER_IT9135_61:
1209 case AF9033_TUNER_IT9135_62:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001210 /* attach tuner */
1211 fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1212 state->af9033_config[adap->id].i2c_addr,
Antti Palosaari44af7472013-02-28 00:14:06 -03001213 state->af9033_config[0].tuner);
Antti Palosaariac77fb02013-01-07 09:51:13 -03001214 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -03001215 default:
1216 fe = NULL;
1217 }
1218
1219 if (fe == NULL) {
1220 ret = -ENODEV;
1221 goto err;
1222 }
1223
1224 return 0;
1225
1226err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001227 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001228
1229 return ret;
1230}
1231
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001232static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -03001233{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001234 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001235 int ret, i;
Antti Palosaaribada3422013-01-11 20:45:11 -03001236 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1237 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001238 struct reg_val_mask tab[] = {
1239 { 0x80f99d, 0x01, 0x01 },
1240 { 0x80f9a4, 0x01, 0x01 },
1241 { 0x00dd11, 0x00, 0x20 },
1242 { 0x00dd11, 0x00, 0x40 },
1243 { 0x00dd13, 0x00, 0x20 },
1244 { 0x00dd13, 0x00, 0x40 },
1245 { 0x00dd11, 0x20, 0x20 },
1246 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1247 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1248 { 0x00dd0c, packet_size, 0xff},
1249 { 0x00dd11, state->dual_mode << 6, 0x40 },
1250 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1251 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1252 { 0x00dd0d, packet_size, 0xff },
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001253 { 0x80f9a3, state->dual_mode, 0x01 },
1254 { 0x80f9cd, state->dual_mode, 0x01 },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001255 { 0x80f99d, 0x00, 0x01 },
1256 { 0x80f9a4, 0x00, 0x01 },
1257 };
Antti Palosaari7f882c22012-03-30 09:10:08 -03001258
Antti Palosaaricb9114e2013-06-03 18:42:14 -03001259 dev_dbg(&d->udev->dev,
1260 "%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1261 __func__, d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001262
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001263 /* init endpoints */
1264 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1265 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1266 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001267 if (ret < 0)
1268 goto err;
1269 }
1270
1271 return 0;
1272
1273err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001274 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001275
1276 return ret;
1277}
1278
Antti Palosaari37b44a02013-01-04 15:21:26 -03001279#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001280static int af9035_rc_query(struct dvb_usb_device *d)
1281{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001282 int ret;
Antti Palosaari75cd5882013-03-08 17:50:21 -03001283 u32 key;
1284 u8 buf[4];
1285 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001286
1287 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001288 if (ret == 1)
1289 return 0;
1290 else if (ret < 0)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001291 goto err;
1292
Antti Palosaari75cd5882013-03-08 17:50:21 -03001293 if ((buf[2] + buf[3]) == 0xff) {
1294 if ((buf[0] + buf[1]) == 0xff) {
1295 /* NEC standard 16bit */
1296 key = buf[0] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001297 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001298 /* NEC extended 24bit */
1299 key = buf[0] << 16 | buf[1] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001300 }
1301 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001302 /* NEC full code 32bit */
1303 key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001304 }
1305
Antti Palosaari75cd5882013-03-08 17:50:21 -03001306 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1307
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001308 rc_keydown(d->rc_dev, key, 0);
1309
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001310 return 0;
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001311
1312err:
1313 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1314
1315 return ret;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001316}
1317
1318static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1319{
Antti Palosaari74c18832013-01-07 15:16:54 -03001320 struct state *state = d_to_priv(d);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001321 int ret;
1322 u8 tmp;
1323
Antti Palosaari431a6d42013-03-07 18:28:25 -03001324 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001325 if (ret < 0)
1326 goto err;
1327
Antti Palosaari119f7a82012-09-12 20:23:53 -03001328 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001329
1330 /* don't activate rc if in HID mode or if not available */
1331 if (tmp == 5) {
Antti Palosaari431a6d42013-03-07 18:28:25 -03001332 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
Antti Palosaari9ea36812013-01-11 22:16:25 -03001333 &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001334 if (ret < 0)
1335 goto err;
1336
Antti Palosaari119f7a82012-09-12 20:23:53 -03001337 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001338
1339 switch (tmp) {
1340 case 0: /* NEC */
1341 default:
David Härdemanc003ab12012-10-11 19:11:54 -03001342 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001343 break;
1344 case 1: /* RC6 */
David Härdemanc003ab12012-10-11 19:11:54 -03001345 rc->allowed_protos = RC_BIT_RC6_MCE;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001346 break;
1347 }
1348
1349 rc->query = af9035_rc_query;
1350 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001351
1352 /* load empty to enable rc */
1353 if (!rc->map_name)
1354 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001355 }
1356
1357 return 0;
1358
1359err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001360 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001361
1362 return ret;
1363}
Antti Palosaarieed56702012-12-09 20:18:31 -03001364#else
1365 #define af9035_get_rc_config NULL
1366#endif
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001367
Antti Palosaaribada3422013-01-11 20:45:11 -03001368static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1369 struct usb_data_stream_properties *stream)
1370{
1371 struct dvb_usb_device *d = fe_to_d(fe);
1372 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1373
1374 if (d->udev->speed == USB_SPEED_FULL)
1375 stream->u.bulk.buffersize = 5 * 188;
1376
1377 return 0;
1378}
1379
1380/*
1381 * FIXME: PID filter is property of demodulator and should be moved to the
1382 * correct driver. Also we support only adapter #0 PID filter and will
1383 * disable adapter #1 if USB1.1 is used.
1384 */
1385static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1386{
1387 struct dvb_usb_device *d = adap_to_d(adap);
1388 int ret;
1389
1390 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1391
1392 ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1393 if (ret < 0)
1394 goto err;
1395
1396 return 0;
1397
1398err:
1399 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1400
1401 return ret;
1402}
1403
1404static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1405 int onoff)
1406{
1407 struct dvb_usb_device *d = adap_to_d(adap);
1408 int ret;
1409 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1410
1411 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1412 __func__, index, pid, onoff);
1413
1414 ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1415 if (ret < 0)
1416 goto err;
1417
1418 ret = af9035_wr_reg(d, 0x80f994, onoff);
1419 if (ret < 0)
1420 goto err;
1421
1422 ret = af9035_wr_reg(d, 0x80f995, index);
1423 if (ret < 0)
1424 goto err;
1425
1426 return 0;
1427
1428err:
1429 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1430
1431 return ret;
1432}
1433
Antti Palosaarib799b812013-01-11 16:22:07 -03001434static int af9035_probe(struct usb_interface *intf,
1435 const struct usb_device_id *id)
1436{
1437 struct usb_device *udev = interface_to_usbdev(intf);
1438 char manufacturer[sizeof("Afatech")];
1439
1440 memset(manufacturer, 0, sizeof(manufacturer));
1441 usb_string(udev, udev->descriptor.iManufacturer,
1442 manufacturer, sizeof(manufacturer));
1443 /*
1444 * There is two devices having same ID but different chipset. One uses
1445 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1446 * is iManufacturer string.
1447 *
1448 * idVendor 0x0ccd TerraTec Electronic GmbH
1449 * idProduct 0x0099
1450 * bcdDevice 2.00
1451 * iManufacturer 1 Afatech
1452 * iProduct 2 DVB-T 2
1453 *
1454 * idVendor 0x0ccd TerraTec Electronic GmbH
1455 * idProduct 0x0099
1456 * bcdDevice 2.00
1457 * iManufacturer 1 ITE Technologies, Inc.
1458 * iProduct 2 DVB-T TV Stick
1459 */
1460 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1461 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1462 if (!strcmp("Afatech", manufacturer)) {
1463 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1464 return -ENODEV;
1465 }
1466 }
1467
1468 return dvb_usbv2_probe(intf, id);
1469}
1470
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001471/* interface 0 is used by DVB-T receiver and
1472 interface 1 is for remote controller (HID) */
1473static const struct dvb_usb_device_properties af9035_props = {
1474 .driver_name = KBUILD_MODNAME,
1475 .owner = THIS_MODULE,
1476 .adapter_nr = adapter_nr,
1477 .size_of_priv = sizeof(struct state),
1478
1479 .generic_bulk_ctrl_endpoint = 0x02,
1480 .generic_bulk_ctrl_endpoint_response = 0x81,
1481
1482 .identify_state = af9035_identify_state,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001483 .download_firmware = af9035_download_firmware,
1484
1485 .i2c_algo = &af9035_i2c_algo,
1486 .read_config = af9035_read_config,
1487 .frontend_attach = af9035_frontend_attach,
1488 .tuner_attach = af9035_tuner_attach,
1489 .init = af9035_init,
1490 .get_rc_config = af9035_get_rc_config,
Antti Palosaaribada3422013-01-11 20:45:11 -03001491 .get_stream_config = af9035_get_stream_config,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001492
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001493 .get_adapter_count = af9035_get_adapter_count,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001494 .adapter = {
1495 {
Antti Palosaaribada3422013-01-11 20:45:11 -03001496 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1497 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1498
1499 .pid_filter_count = 32,
1500 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1501 .pid_filter = af9035_pid_filter,
1502
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001503 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1504 }, {
1505 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1506 },
1507 },
1508};
1509
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001510static const struct usb_device_id af9035_id_table[] = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001511 /* AF9035 devices */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001512 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1513 &af9035_props, "Afatech AF9035 reference design", NULL) },
1514 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1515 &af9035_props, "Afatech AF9035 reference design", NULL) },
1516 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1517 &af9035_props, "Afatech AF9035 reference design", NULL) },
1518 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1519 &af9035_props, "Afatech AF9035 reference design", NULL) },
1520 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1521 &af9035_props, "Afatech AF9035 reference design", NULL) },
1522 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1523 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1524 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1525 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1526 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1527 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1528 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1529 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1530 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1531 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1532 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1533 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb32012-09-20 14:57:17 -03001534 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1535 &af9035_props, "Asus U3100Mini Plus", NULL) },
Antti Palosaaricb9114e2013-06-03 18:42:14 -03001536 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
Fabrizio Gazzatod9b75952013-02-17 18:25:34 -03001537 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001538 /* IT9135 devices */
Malcolm Priestley7a541ce2014-02-08 13:11:16 -03001539 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
1540 &af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
1541 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
1542 &af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
1543 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
1544 &af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
1545 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
1546 &af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
1547 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
1548 &af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
1549 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
1550 &af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
1551 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
1552 &af9035_props, "Avermedia A835B(4835)", RC_MAP_IT913X_V2) },
1553 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
1554 &af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
Antti Palosaarib799b812013-01-11 16:22:07 -03001555 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1556 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1557 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
Antti Palosaari0c413d12013-08-08 19:41:06 -03001558 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
1559 &af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
Antti Palosaari261cb2002014-02-01 11:30:50 -03001560 { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
1561 &af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001562 { }
1563};
1564MODULE_DEVICE_TABLE(usb, af9035_id_table);
1565
Antti Palosaari7f882c22012-03-30 09:10:08 -03001566static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001567 .name = KBUILD_MODNAME,
1568 .id_table = af9035_id_table,
Antti Palosaarib799b812013-01-11 16:22:07 -03001569 .probe = af9035_probe,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001570 .disconnect = dvb_usbv2_disconnect,
1571 .suspend = dvb_usbv2_suspend,
1572 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001573 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001574 .no_dynamic_id = 1,
1575 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001576};
1577
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001578module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001579
1580MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1581MODULE_DESCRIPTION("Afatech AF9035 driver");
1582MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001583MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
Antti Palosaari74c18832013-01-07 15:16:54 -03001584MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1585MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);