blob: b638fc1cd5740fdb5a93e8885cddfe616f76fffd [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 }
271 } else {
272 /*
273 * We support only two kind of I2C transactions:
274 * 1) 1 x read + 1 x write
275 * 2) 1 x write
276 */
277 ret = -EOPNOTSUPP;
278 }
279
280 mutex_unlock(&d->i2c_mutex);
281
282 if (ret < 0)
283 return ret;
284 else
285 return num;
286}
287
288static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
289{
290 return I2C_FUNC_I2C;
291}
292
293static struct i2c_algorithm af9035_i2c_algo = {
294 .master_xfer = af9035_i2c_master_xfer,
295 .functionality = af9035_i2c_functionality,
296};
297
Antti Palosaaria0921af2012-06-18 23:42:53 -0300298static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300299{
Antti Palosaari74c18832013-01-07 15:16:54 -0300300 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300301 int ret;
302 u8 wbuf[1] = { 1 };
303 u8 rbuf[4];
304 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
305 sizeof(rbuf), rbuf };
306
Antti Palosaari74c18832013-01-07 15:16:54 -0300307 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
308 if (ret < 0)
309 goto err;
310
311 state->chip_version = rbuf[0];
312 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
313
314 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
315 if (ret < 0)
316 goto err;
317
318 dev_info(&d->udev->dev,
319 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
320 __func__, state->prechip_version, state->chip_version,
321 state->chip_type);
322
323 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300324 if (state->chip_version == 0x02)
Antti Palosaari74c18832013-01-07 15:16:54 -0300325 *name = AF9035_FIRMWARE_IT9135_V2;
326 else
327 *name = AF9035_FIRMWARE_IT9135_V1;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300328 state->eeprom_addr = EEPROM_BASE_IT9135;
Antti Palosaari74c18832013-01-07 15:16:54 -0300329 } else {
330 *name = AF9035_FIRMWARE_AF9035;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300331 state->eeprom_addr = EEPROM_BASE_AF9035;
Antti Palosaari74c18832013-01-07 15:16:54 -0300332 }
333
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300334 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300335 if (ret < 0)
336 goto err;
337
Antti Palosaari119f7a82012-09-12 20:23:53 -0300338 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300339 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300340 ret = WARM;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300341 else
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300342 ret = COLD;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300343
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300344 return ret;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300345
346err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300347 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300348
349 return ret;
350}
351
Antti Palosaari8229da52013-03-07 17:59:55 -0300352static int af9035_download_firmware_old(struct dvb_usb_device *d,
Antti Palosaari7f882c22012-03-30 09:10:08 -0300353 const struct firmware *fw)
354{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300355 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300356 u8 wbuf[1];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300357 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
358 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300359 u8 hdr_core;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300360 u16 hdr_addr, hdr_data_len, hdr_checksum;
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300361 #define MAX_DATA 58
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300362 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300363
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300364 /*
365 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
366 *
367 * byte 0: MCS 51 core
368 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
369 * address spaces
370 * byte 1-2: Big endian destination address
371 * byte 3-4: Big endian number of data bytes following the header
372 * byte 5-6: Big endian header checksum, apparently ignored by the chip
373 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
374 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300375
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300376 for (i = fw->size; i > HDR_SIZE;) {
377 hdr_core = fw->data[fw->size - i + 0];
378 hdr_addr = fw->data[fw->size - i + 1] << 8;
379 hdr_addr |= fw->data[fw->size - i + 2] << 0;
380 hdr_data_len = fw->data[fw->size - i + 3] << 8;
381 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
382 hdr_checksum = fw->data[fw->size - i + 5] << 8;
383 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300384
Antti Palosaari119f7a82012-09-12 20:23:53 -0300385 dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
386 "checksum=%04x\n", __func__, hdr_core, hdr_addr,
387 hdr_data_len, hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300388
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300389 if (((hdr_core != 1) && (hdr_core != 2)) ||
390 (hdr_data_len > i)) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300391 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300392 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300393 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300394
395 /* download begin packet */
396 req.cmd = CMD_FW_DL_BEGIN;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300397 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300398 if (ret < 0)
399 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300400
401 /* download firmware packet(s) */
402 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
403 len = j;
404 if (len > MAX_DATA)
405 len = MAX_DATA;
406 req_fw_dl.wlen = len;
407 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
408 HDR_SIZE + hdr_data_len - j];
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300409 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300410 if (ret < 0)
411 goto err;
412 }
413
414 /* download end packet */
415 req.cmd = CMD_FW_DL_END;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300416 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300417 if (ret < 0)
418 goto err;
419
420 i -= hdr_data_len + HDR_SIZE;
421
Antti Palosaari119f7a82012-09-12 20:23:53 -0300422 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
423 __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300424 }
425
Antti Palosaariff4e3fe2012-12-09 16:25:08 -0300426 /* print warn if firmware is bad, continue and see what happens */
427 if (i)
428 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
429
Antti Palosaari7f882c22012-03-30 09:10:08 -0300430 return 0;
431
432err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300433 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300434
435 return ret;
436}
437
Antti Palosaari8229da52013-03-07 17:59:55 -0300438static int af9035_download_firmware_new(struct dvb_usb_device *d,
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300439 const struct firmware *fw)
440{
441 int ret, i, i_prev;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300442 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300443 #define HDR_SIZE 7
444
445 /*
446 * There seems to be following firmware header. Meaning of bytes 0-3
447 * is unknown.
448 *
449 * 0: 3
450 * 1: 0, 1
451 * 2: 0
452 * 3: 1, 2, 3
453 * 4: addr MSB
454 * 5: addr LSB
455 * 6: count of data bytes ?
456 */
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300457 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
458 if (i == fw->size ||
459 (fw->data[i + 0] == 0x03 &&
460 (fw->data[i + 1] == 0x00 ||
461 fw->data[i + 1] == 0x01) &&
462 fw->data[i + 2] == 0x00)) {
463 req_fw_dl.wlen = i - i_prev;
464 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
465 i_prev = i;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300466 ret = af9035_ctrl_msg(d, &req_fw_dl);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300467 if (ret < 0)
468 goto err;
469
Antti Palosaari119f7a82012-09-12 20:23:53 -0300470 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
471 __func__, i);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300472 }
473 }
474
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300475 return 0;
476
477err:
478 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
479
480 return ret;
481}
482
483static int af9035_download_firmware(struct dvb_usb_device *d,
484 const struct firmware *fw)
485{
486 struct state *state = d_to_priv(d);
487 int ret;
488 u8 wbuf[1];
489 u8 rbuf[4];
490 u8 tmp;
491 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
492 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
493 dev_dbg(&d->udev->dev, "%s:\n", __func__);
494
495 /*
496 * In case of dual tuner configuration we need to do some extra
497 * initialization in order to download firmware to slave demod too,
498 * which is done by master demod.
499 * Master feeds also clock and controls power via GPIO.
500 */
501 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
502 if (ret < 0)
503 goto err;
504
505 if (tmp) {
506 /* configure gpioh1, reset & power slave demod */
507 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
508 if (ret < 0)
509 goto err;
510
511 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
512 if (ret < 0)
513 goto err;
514
515 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
516 if (ret < 0)
517 goto err;
518
519 usleep_range(10000, 50000);
520
521 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
522 if (ret < 0)
523 goto err;
524
525 /* tell the slave I2C address */
526 ret = af9035_rd_reg(d,
527 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
528 &tmp);
529 if (ret < 0)
530 goto err;
531
532 if (state->chip_type == 0x9135) {
533 ret = af9035_wr_reg(d, 0x004bfb, tmp);
534 if (ret < 0)
535 goto err;
536 } else {
537 ret = af9035_wr_reg(d, 0x00417f, tmp);
538 if (ret < 0)
539 goto err;
540
541 /* enable clock out */
542 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
543 if (ret < 0)
544 goto err;
545 }
546 }
547
Antti Palosaari8229da52013-03-07 17:59:55 -0300548 if (fw->data[0] == 0x01)
549 ret = af9035_download_firmware_old(d, fw);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300550 else
Antti Palosaari8229da52013-03-07 17:59:55 -0300551 ret = af9035_download_firmware_new(d, fw);
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300552 if (ret < 0)
553 goto err;
554
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300555 /* firmware loaded, request boot */
556 req.cmd = CMD_FW_BOOT;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300557 ret = af9035_ctrl_msg(d, &req);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300558 if (ret < 0)
559 goto err;
560
561 /* ensure firmware starts */
562 wbuf[0] = 1;
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300563 ret = af9035_ctrl_msg(d, &req_fw_ver);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300564 if (ret < 0)
565 goto err;
566
567 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari119f7a82012-09-12 20:23:53 -0300568 dev_err(&d->udev->dev, "%s: firmware did not run\n",
569 KBUILD_MODNAME);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300570 ret = -ENODEV;
571 goto err;
572 }
573
Antti Palosaari119f7a82012-09-12 20:23:53 -0300574 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
575 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300576
577 return 0;
578
579err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300580 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300581
582 return ret;
583}
584
Antti Palosaari9ea36812013-01-11 22:16:25 -0300585static int af9035_read_config(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -0300586{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300587 struct state *state = d_to_priv(d);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300588 int ret, i;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300589 u8 tmp;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300590 u16 tmp16, addr;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300591
Antti Palosaaribf97b632012-12-08 22:51:19 -0300592 /* demod I2C "address" */
593 state->af9033_config[0].i2c_addr = 0x38;
Antti Palosaari0d94d6a2013-01-07 15:26:05 -0300594 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300595 state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
Antti Palosaariab56ad62013-03-07 18:43:51 -0300596 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
597 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300598
Antti Palosaari9ea36812013-01-11 22:16:25 -0300599 /* eeprom memory mapped location */
600 if (state->chip_type == 0x9135) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300601 if (state->chip_version == 0x02) {
602 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300603 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300604 tmp16 = 0x00461d;
605 } else {
606 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300607 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300608 tmp16 = 0x00461b;
609 }
610
Antti Palosaari9ea36812013-01-11 22:16:25 -0300611 /* check if eeprom exists */
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300612 ret = af9035_rd_reg(d, tmp16, &tmp);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300613 if (ret < 0)
614 goto err;
615
Antti Palosaari431a6d42013-03-07 18:28:25 -0300616 if (tmp == 0x00) {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300617 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300618 goto skip_eeprom;
619 }
Antti Palosaari9ea36812013-01-11 22:16:25 -0300620 }
621
Antti Palosaari7f882c22012-03-30 09:10:08 -0300622 /* check if there is dual tuners */
Antti Palosaari431a6d42013-03-07 18:28:25 -0300623 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300624 if (ret < 0)
625 goto err;
626
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300627 state->dual_mode = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300628 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
629 state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300630
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300631 if (state->dual_mode) {
632 /* read 2nd demodulator I2C address */
Antti Palosaari431a6d42013-03-07 18:28:25 -0300633 ret = af9035_rd_reg(d,
634 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
635 &tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300636 if (ret < 0)
637 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300638
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300639 state->af9033_config[1].i2c_addr = tmp;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300640 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
641 __func__, tmp);
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300642 }
643
Antti Palosaari431a6d42013-03-07 18:28:25 -0300644 addr = state->eeprom_addr;
645
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300646 for (i = 0; i < state->dual_mode + 1; i++) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300647 /* tuner */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300648 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300649 if (ret < 0)
650 goto err;
651
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300652 if (tmp == 0x00)
653 dev_dbg(&d->udev->dev,
654 "%s: [%d]tuner not set, using default\n",
655 __func__, i);
656 else
657 state->af9033_config[i].tuner = tmp;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300658
Antti Palosaaribc3c9e12013-02-01 22:23:07 -0300659 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
660 __func__, i, state->af9033_config[i].tuner);
Antti Palosaari9ea36812013-01-11 22:16:25 -0300661
662 switch (state->af9033_config[i].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300663 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300664 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300665 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300666 case AF9033_TUNER_TDA18218:
Oliver Schinagld67ceb332012-09-20 14:57:17 -0300667 case AF9033_TUNER_FC2580:
Antti Palosaari7e0bc292012-12-02 20:12:29 -0300668 case AF9033_TUNER_FC0012:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300669 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300670 break;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300671 case AF9033_TUNER_IT9135_38:
672 case AF9033_TUNER_IT9135_51:
673 case AF9033_TUNER_IT9135_52:
674 case AF9033_TUNER_IT9135_60:
675 case AF9033_TUNER_IT9135_61:
676 case AF9033_TUNER_IT9135_62:
677 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300678 default:
Antti Palosaari9ea36812013-01-11 22:16:25 -0300679 dev_warn(&d->udev->dev,
680 "%s: tuner id=%02x not supported, please report!",
Antti Palosaari119f7a82012-09-12 20:23:53 -0300681 KBUILD_MODNAME, tmp);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300682 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300683
Antti Palosaaribf97b632012-12-08 22:51:19 -0300684 /* disable dual mode if driver does not support it */
685 if (i == 1)
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300686 switch (state->af9033_config[i].tuner) {
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300687 case AF9033_TUNER_FC0012:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300688 case AF9033_TUNER_IT9135_38:
689 case AF9033_TUNER_IT9135_51:
690 case AF9033_TUNER_IT9135_52:
691 case AF9033_TUNER_IT9135_60:
692 case AF9033_TUNER_IT9135_61:
693 case AF9033_TUNER_IT9135_62:
Jose Alberto Reguero78c7bc42013-02-10 15:43:03 -0300694 case AF9033_TUNER_MXL5007T:
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300695 break;
Antti Palosaaribf97b632012-12-08 22:51:19 -0300696 default:
697 state->dual_mode = false;
Antti Palosaari9ea36812013-01-11 22:16:25 -0300698 dev_info(&d->udev->dev,
699 "%s: driver does not support 2nd tuner and will disable it",
700 KBUILD_MODNAME);
Antti Palosaaribf97b632012-12-08 22:51:19 -0300701 }
702
Antti Palosaari7f882c22012-03-30 09:10:08 -0300703 /* tuner IF frequency */
Antti Palosaari9ea36812013-01-11 22:16:25 -0300704 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300705 if (ret < 0)
706 goto err;
707
708 tmp16 = tmp;
709
Antti Palosaari9ea36812013-01-11 22:16:25 -0300710 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300711 if (ret < 0)
712 goto err;
713
714 tmp16 |= tmp << 8;
715
Antti Palosaari119f7a82012-09-12 20:23:53 -0300716 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300717
Antti Palosaari9ea36812013-01-11 22:16:25 -0300718 addr += 0x10; /* shift for the 2nd tuner params */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300719 }
720
Antti Palosaari9ea36812013-01-11 22:16:25 -0300721skip_eeprom:
Antti Palosaari7f882c22012-03-30 09:10:08 -0300722 /* get demod clock */
723 ret = af9035_rd_reg(d, 0x00d800, &tmp);
724 if (ret < 0)
725 goto err;
726
727 tmp = (tmp >> 0) & 0x0f;
728
Antti Palosaari9ea36812013-01-11 22:16:25 -0300729 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
730 if (state->chip_type == 0x9135)
731 state->af9033_config[i].clock = clock_lut_it9135[tmp];
732 else
733 state->af9033_config[i].clock = clock_lut_af9035[tmp];
Antti Palosaari74c18832013-01-07 15:16:54 -0300734 }
735
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300736 return 0;
737
738err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300739 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300740
741 return ret;
742}
743
Antti Palosaari51639be2012-09-16 22:26:56 -0300744static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
745 int cmd, int arg)
746{
747 int ret;
748 u8 val;
749
750 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
751
752 /*
753 * CEN always enabled by hardware wiring
754 * RESETN GPIOT3
755 * RXEN GPIOT2
756 */
757
758 switch (cmd) {
759 case TUA9001_CMD_RESETN:
760 if (arg)
761 val = 0x00;
762 else
763 val = 0x01;
764
765 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
766 if (ret < 0)
767 goto err;
768 break;
769 case TUA9001_CMD_RXEN:
770 if (arg)
771 val = 0x01;
772 else
773 val = 0x00;
774
775 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
776 if (ret < 0)
777 goto err;
778 break;
779 }
780
781 return 0;
782
783err:
784 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
785
786 return ret;
787}
788
789
Michael Büschffc501f2012-04-02 12:18:36 -0300790static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300791 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300792{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300793 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300794
795 switch (cmd) {
796 case FC0011_FE_CALLBACK_POWER:
797 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300798 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
799 if (ret < 0)
800 goto err;
801
802 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
803 if (ret < 0)
804 goto err;
805
806 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
807 if (ret < 0)
808 goto err;
809
Michael Büschffc501f2012-04-02 12:18:36 -0300810 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300811 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
812 if (ret < 0)
813 goto err;
814
815 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
816 if (ret < 0)
817 goto err;
818
Michael Büschde2bec52012-04-03 05:11:30 -0300819 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300820 break;
821 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300822 ret = af9035_wr_reg(d, 0xd8e9, 1);
823 if (ret < 0)
824 goto err;
825
826 ret = af9035_wr_reg(d, 0xd8e8, 1);
827 if (ret < 0)
828 goto err;
829
830 ret = af9035_wr_reg(d, 0xd8e7, 1);
831 if (ret < 0)
832 goto err;
833
Michael Büschde2bec52012-04-03 05:11:30 -0300834 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300835
836 ret = af9035_wr_reg(d, 0xd8e7, 0);
837 if (ret < 0)
838 goto err;
839
Michael Büschde2bec52012-04-03 05:11:30 -0300840 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300841 break;
842 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300843 ret = -EINVAL;
844 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300845 }
846
847 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300848
849err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300850 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300851
852 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300853}
854
855static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
856{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300857 struct state *state = d_to_priv(d);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300858
859 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300860 case AF9033_TUNER_FC0011:
861 return af9035_fc0011_tuner_callback(d, cmd, arg);
Antti Palosaari51639be2012-09-16 22:26:56 -0300862 case AF9033_TUNER_TUA9001:
863 return af9035_tua9001_tuner_callback(d, cmd, arg);
Michael Büschffc501f2012-04-02 12:18:36 -0300864 default:
865 break;
866 }
867
Antti Palosaari1835af12012-09-11 22:27:06 -0300868 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300869}
870
871static int af9035_frontend_callback(void *adapter_priv, int component,
872 int cmd, int arg)
873{
874 struct i2c_adapter *adap = adapter_priv;
875 struct dvb_usb_device *d = i2c_get_adapdata(adap);
876
Antti Palosaari1835af12012-09-11 22:27:06 -0300877 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
878 __func__, component, cmd, arg);
879
Michael Büschffc501f2012-04-02 12:18:36 -0300880 switch (component) {
881 case DVB_FRONTEND_COMPONENT_TUNER:
882 return af9035_tuner_callback(d, cmd, arg);
883 default:
884 break;
885 }
886
Antti Palosaari1835af12012-09-11 22:27:06 -0300887 return 0;
Michael Büschffc501f2012-04-02 12:18:36 -0300888}
889
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300890static int af9035_get_adapter_count(struct dvb_usb_device *d)
891{
892 struct state *state = d_to_priv(d);
Antti Palosaaribada3422013-01-11 20:45:11 -0300893
894 /* disable 2nd adapter as we don't have PID filters implemented */
895 if (d->udev->speed == USB_SPEED_FULL)
896 return 1;
897 else
898 return state->dual_mode + 1;
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300899}
900
Antti Palosaari7f882c22012-03-30 09:10:08 -0300901static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
902{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300903 struct state *state = adap_to_priv(adap);
904 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300905 int ret;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300906 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300907
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300908 if (!state->af9033_config[adap->id].tuner) {
909 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300910 ret = -ENODEV;
911 goto err;
912 }
913
Antti Palosaari7f882c22012-03-30 09:10:08 -0300914 /* attach demodulator */
Antti Palosaaribf97b632012-12-08 22:51:19 -0300915 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
916 &d->i2c_adap);
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300917 if (adap->fe[0] == NULL) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300918 ret = -ENODEV;
919 goto err;
920 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300921
922 /* disable I2C-gate */
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300923 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
924 adap->fe[0]->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300925
926 return 0;
927
928err:
Antti Palosaari119f7a82012-09-12 20:23:53 -0300929 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300930
931 return ret;
932}
933
934static struct tua9001_config af9035_tua9001_config = {
935 .i2c_addr = 0x60,
936};
937
Michael Büschffc501f2012-04-02 12:18:36 -0300938static const struct fc0011_config af9035_fc0011_config = {
939 .i2c_address = 0x60,
940};
941
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300942static struct mxl5007t_config af9035_mxl5007t_config[] = {
943 {
944 .xtal_freq_hz = MxL_XTAL_24_MHZ,
945 .if_freq_hz = MxL_IF_4_57_MHZ,
946 .invert_if = 0,
947 .loop_thru_enable = 0,
948 .clk_out_enable = 0,
949 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
950 }, {
951 .xtal_freq_hz = MxL_XTAL_24_MHZ,
952 .if_freq_hz = MxL_IF_4_57_MHZ,
953 .invert_if = 0,
954 .loop_thru_enable = 1,
955 .clk_out_enable = 1,
956 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
957 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300958};
959
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300960static struct tda18218_config af9035_tda18218_config = {
961 .i2c_address = 0x60,
962 .i2c_wr_max = 21,
963};
964
Oliver Schinagld67ceb332012-09-20 14:57:17 -0300965static const struct fc2580_config af9035_fc2580_config = {
966 .i2c_addr = 0x56,
967 .clock = 16384000,
968};
969
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300970static const struct fc0012_config af9035_fc0012_config[] = {
971 {
972 .i2c_address = 0x63,
973 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300974 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300975 .loop_through = true,
976 .clock_out = true,
977 }, {
978 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
979 .xtal_freq = FC_XTAL_36_MHZ,
Antti Palosaari3a984772012-12-09 12:33:04 -0300980 .dual_master = true,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300981 }
Antti Palosaariad3a7582012-12-08 23:27:49 -0300982};
983
Antti Palosaari7f882c22012-03-30 09:10:08 -0300984static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
985{
Antti Palosaari5da2aec2012-06-17 23:15:03 -0300986 struct state *state = adap_to_priv(adap);
987 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300988 int ret;
989 struct dvb_frontend *fe;
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -0300990 struct i2c_msg msg[1];
Antti Palosaaribf97b632012-12-08 22:51:19 -0300991 u8 tuner_addr;
Antti Palosaaridf8f1be2013-02-03 13:46:56 -0300992 dev_dbg(&d->udev->dev, "%s:\n", __func__);
993
Antti Palosaaribf97b632012-12-08 22:51:19 -0300994 /*
995 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
996 * to carry info about used I2C bus for dual tuner configuration.
997 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300998
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300999 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -03001000 case AF9033_TUNER_TUA9001:
1001 /* AF9035 gpiot3 = TUA9001 RESETN
1002 AF9035 gpiot2 = TUA9001 RXEN */
1003
1004 /* configure gpiot2 and gpiot2 as output */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001005 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001006 if (ret < 0)
1007 goto err;
1008
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001009 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001010 if (ret < 0)
1011 goto err;
1012
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001013 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001014 if (ret < 0)
1015 goto err;
1016
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001017 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001018 if (ret < 0)
1019 goto err;
1020
Antti Palosaari7f882c22012-03-30 09:10:08 -03001021 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001022 fe = dvb_attach(tua9001_attach, adap->fe[0],
1023 &d->i2c_adap, &af9035_tua9001_config);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001024 break;
Michael Büschffc501f2012-04-02 12:18:36 -03001025 case AF9033_TUNER_FC0011:
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001026 fe = dvb_attach(fc0011_attach, adap->fe[0],
1027 &d->i2c_adap, &af9035_fc0011_config);
Michael Büschffc501f2012-04-02 12:18:36 -03001028 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001029 case AF9033_TUNER_MXL5007T:
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001030 if (adap->id == 0) {
1031 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1032 if (ret < 0)
1033 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001034
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001035 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1036 if (ret < 0)
1037 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001038
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001039 ret = af9035_wr_reg(d, 0x00d8df, 0);
1040 if (ret < 0)
1041 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001042
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001043 msleep(30);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001044
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001045 ret = af9035_wr_reg(d, 0x00d8df, 1);
1046 if (ret < 0)
1047 goto err;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001048
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001049 msleep(300);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001050
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001051 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1052 if (ret < 0)
1053 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001054
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001055 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1056 if (ret < 0)
1057 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001058
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001059 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1060 if (ret < 0)
1061 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001062
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001063 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1064 if (ret < 0)
1065 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001066
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001067 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1068 if (ret < 0)
1069 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001070
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001071 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1072 if (ret < 0)
1073 goto err;
Antti Palosaaribf97b632012-12-08 22:51:19 -03001074
1075 tuner_addr = 0x60;
1076 } else {
1077 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001078 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001079
1080 /* attach tuner */
Antti Palosaaribf97b632012-12-08 22:51:19 -03001081 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1082 tuner_addr, &af9035_mxl5007t_config[adap->id]);
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001083 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001084 case AF9033_TUNER_TDA18218:
1085 /* attach tuner */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001086 fe = dvb_attach(tda18218_attach, adap->fe[0],
1087 &d->i2c_adap, &af9035_tda18218_config);
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001088 break;
Oliver Schinagld67ceb332012-09-20 14:57:17 -03001089 case AF9033_TUNER_FC2580:
1090 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1091 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1092 if (ret < 0)
1093 goto err;
1094
1095 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1096 if (ret < 0)
1097 goto err;
1098
1099 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1100 if (ret < 0)
1101 goto err;
1102
1103 usleep_range(10000, 50000);
1104 /* attach tuner */
1105 fe = dvb_attach(fc2580_attach, adap->fe[0],
1106 &d->i2c_adap, &af9035_fc2580_config);
1107 break;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001108 case AF9033_TUNER_FC0012:
1109 /*
1110 * AF9035 gpiot2 = FC0012 enable
1111 * XXX: there seems to be something on gpioh8 too, but on my
1112 * my test I didn't find any difference.
1113 */
1114
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001115 if (adap->id == 0) {
1116 /* configure gpiot2 as output and high */
1117 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1118 if (ret < 0)
1119 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001120
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001121 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1122 if (ret < 0)
1123 goto err;
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001124
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001125 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1126 if (ret < 0)
1127 goto err;
1128 } else {
1129 /*
1130 * FIXME: That belongs for the FC0012 driver.
1131 * Write 02 to FC0012 master tuner register 0d directly
1132 * in order to make slave tuner working.
1133 */
1134 msg[0].addr = 0x63;
1135 msg[0].flags = 0;
1136 msg[0].len = 2;
1137 msg[0].buf = "\x0d\x02";
1138 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1139 if (ret < 0)
1140 goto err;
1141 }
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001142
1143 usleep_range(10000, 50000);
1144
Antti Palosaariad3a7582012-12-08 23:27:49 -03001145 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaari0bb3d8a2012-12-09 12:01:41 -03001146 &af9035_fc0012_config[adap->id]);
Antti Palosaari7e0bc292012-12-02 20:12:29 -03001147 break;
Antti Palosaariac77fb02013-01-07 09:51:13 -03001148 case AF9033_TUNER_IT9135_38:
Antti Palosaari74c18832013-01-07 15:16:54 -03001149 case AF9033_TUNER_IT9135_51:
1150 case AF9033_TUNER_IT9135_52:
1151 case AF9033_TUNER_IT9135_60:
1152 case AF9033_TUNER_IT9135_61:
1153 case AF9033_TUNER_IT9135_62:
Antti Palosaaridf8f1be2013-02-03 13:46:56 -03001154 /* attach tuner */
1155 fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1156 state->af9033_config[adap->id].i2c_addr,
Antti Palosaari44af7472013-02-28 00:14:06 -03001157 state->af9033_config[0].tuner);
Antti Palosaariac77fb02013-01-07 09:51:13 -03001158 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -03001159 default:
1160 fe = NULL;
1161 }
1162
1163 if (fe == NULL) {
1164 ret = -ENODEV;
1165 goto err;
1166 }
1167
1168 return 0;
1169
1170err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001171 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001172
1173 return ret;
1174}
1175
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001176static int af9035_init(struct dvb_usb_device *d)
Antti Palosaari7f882c22012-03-30 09:10:08 -03001177{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001178 struct state *state = d_to_priv(d);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001179 int ret, i;
Antti Palosaaribada3422013-01-11 20:45:11 -03001180 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1181 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001182 struct reg_val_mask tab[] = {
1183 { 0x80f99d, 0x01, 0x01 },
1184 { 0x80f9a4, 0x01, 0x01 },
1185 { 0x00dd11, 0x00, 0x20 },
1186 { 0x00dd11, 0x00, 0x40 },
1187 { 0x00dd13, 0x00, 0x20 },
1188 { 0x00dd13, 0x00, 0x40 },
1189 { 0x00dd11, 0x20, 0x20 },
1190 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1191 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1192 { 0x00dd0c, packet_size, 0xff},
1193 { 0x00dd11, state->dual_mode << 6, 0x40 },
1194 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1195 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1196 { 0x00dd0d, packet_size, 0xff },
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001197 { 0x80f9a3, state->dual_mode, 0x01 },
1198 { 0x80f9cd, state->dual_mode, 0x01 },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001199 { 0x80f99d, 0x00, 0x01 },
1200 { 0x80f9a4, 0x00, 0x01 },
1201 };
Antti Palosaari7f882c22012-03-30 09:10:08 -03001202
Antti Palosaari119f7a82012-09-12 20:23:53 -03001203 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
1204 "packet_size=%02x\n", __func__,
1205 d->udev->speed, frame_size, packet_size);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001206
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001207 /* init endpoints */
1208 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1209 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1210 tab[i].mask);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001211 if (ret < 0)
1212 goto err;
1213 }
1214
1215 return 0;
1216
1217err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001218 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001219
1220 return ret;
1221}
1222
Antti Palosaari37b44a02013-01-04 15:21:26 -03001223#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001224static int af9035_rc_query(struct dvb_usb_device *d)
1225{
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001226 int ret;
Antti Palosaari75cd5882013-03-08 17:50:21 -03001227 u32 key;
1228 u8 buf[4];
1229 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001230
1231 ret = af9035_ctrl_msg(d, &req);
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001232 if (ret == 1)
1233 return 0;
1234 else if (ret < 0)
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001235 goto err;
1236
Antti Palosaari75cd5882013-03-08 17:50:21 -03001237 if ((buf[2] + buf[3]) == 0xff) {
1238 if ((buf[0] + buf[1]) == 0xff) {
1239 /* NEC standard 16bit */
1240 key = buf[0] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001241 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001242 /* NEC extended 24bit */
1243 key = buf[0] << 16 | buf[1] << 8 | buf[2];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001244 }
1245 } else {
Antti Palosaari75cd5882013-03-08 17:50:21 -03001246 /* NEC full code 32bit */
1247 key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001248 }
1249
Antti Palosaari75cd5882013-03-08 17:50:21 -03001250 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1251
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001252 rc_keydown(d->rc_dev, key, 0);
1253
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001254 return 0;
Antti Palosaari1bfd5292013-03-08 17:39:12 -03001255
1256err:
1257 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1258
1259 return ret;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001260}
1261
1262static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1263{
Antti Palosaari74c18832013-01-07 15:16:54 -03001264 struct state *state = d_to_priv(d);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001265 int ret;
1266 u8 tmp;
1267
Antti Palosaari431a6d42013-03-07 18:28:25 -03001268 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001269 if (ret < 0)
1270 goto err;
1271
Antti Palosaari119f7a82012-09-12 20:23:53 -03001272 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001273
1274 /* don't activate rc if in HID mode or if not available */
1275 if (tmp == 5) {
Antti Palosaari431a6d42013-03-07 18:28:25 -03001276 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
Antti Palosaari9ea36812013-01-11 22:16:25 -03001277 &tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001278 if (ret < 0)
1279 goto err;
1280
Antti Palosaari119f7a82012-09-12 20:23:53 -03001281 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001282
1283 switch (tmp) {
1284 case 0: /* NEC */
1285 default:
David Härdemanc003ab12012-10-11 19:11:54 -03001286 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001287 break;
1288 case 1: /* RC6 */
David Härdemanc003ab12012-10-11 19:11:54 -03001289 rc->allowed_protos = RC_BIT_RC6_MCE;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001290 break;
1291 }
1292
1293 rc->query = af9035_rc_query;
1294 rc->interval = 500;
Antti Palosaaride73bee2012-07-05 19:57:07 -03001295
1296 /* load empty to enable rc */
1297 if (!rc->map_name)
1298 rc->map_name = RC_MAP_EMPTY;
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001299 }
1300
1301 return 0;
1302
1303err:
Antti Palosaari119f7a82012-09-12 20:23:53 -03001304 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001305
1306 return ret;
1307}
Antti Palosaarieed56702012-12-09 20:18:31 -03001308#else
1309 #define af9035_get_rc_config NULL
1310#endif
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001311
Antti Palosaaribada3422013-01-11 20:45:11 -03001312static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1313 struct usb_data_stream_properties *stream)
1314{
1315 struct dvb_usb_device *d = fe_to_d(fe);
1316 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1317
1318 if (d->udev->speed == USB_SPEED_FULL)
1319 stream->u.bulk.buffersize = 5 * 188;
1320
1321 return 0;
1322}
1323
1324/*
1325 * FIXME: PID filter is property of demodulator and should be moved to the
1326 * correct driver. Also we support only adapter #0 PID filter and will
1327 * disable adapter #1 if USB1.1 is used.
1328 */
1329static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1330{
1331 struct dvb_usb_device *d = adap_to_d(adap);
1332 int ret;
1333
1334 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1335
1336 ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1337 if (ret < 0)
1338 goto err;
1339
1340 return 0;
1341
1342err:
1343 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1344
1345 return ret;
1346}
1347
1348static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1349 int onoff)
1350{
1351 struct dvb_usb_device *d = adap_to_d(adap);
1352 int ret;
1353 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1354
1355 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1356 __func__, index, pid, onoff);
1357
1358 ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1359 if (ret < 0)
1360 goto err;
1361
1362 ret = af9035_wr_reg(d, 0x80f994, onoff);
1363 if (ret < 0)
1364 goto err;
1365
1366 ret = af9035_wr_reg(d, 0x80f995, index);
1367 if (ret < 0)
1368 goto err;
1369
1370 return 0;
1371
1372err:
1373 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1374
1375 return ret;
1376}
1377
Antti Palosaarib799b812013-01-11 16:22:07 -03001378static int af9035_probe(struct usb_interface *intf,
1379 const struct usb_device_id *id)
1380{
1381 struct usb_device *udev = interface_to_usbdev(intf);
1382 char manufacturer[sizeof("Afatech")];
1383
1384 memset(manufacturer, 0, sizeof(manufacturer));
1385 usb_string(udev, udev->descriptor.iManufacturer,
1386 manufacturer, sizeof(manufacturer));
1387 /*
1388 * There is two devices having same ID but different chipset. One uses
1389 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1390 * is iManufacturer string.
1391 *
1392 * idVendor 0x0ccd TerraTec Electronic GmbH
1393 * idProduct 0x0099
1394 * bcdDevice 2.00
1395 * iManufacturer 1 Afatech
1396 * iProduct 2 DVB-T 2
1397 *
1398 * idVendor 0x0ccd TerraTec Electronic GmbH
1399 * idProduct 0x0099
1400 * bcdDevice 2.00
1401 * iManufacturer 1 ITE Technologies, Inc.
1402 * iProduct 2 DVB-T TV Stick
1403 */
1404 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1405 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1406 if (!strcmp("Afatech", manufacturer)) {
1407 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1408 return -ENODEV;
1409 }
1410 }
1411
1412 return dvb_usbv2_probe(intf, id);
1413}
1414
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001415/* interface 0 is used by DVB-T receiver and
1416 interface 1 is for remote controller (HID) */
1417static const struct dvb_usb_device_properties af9035_props = {
1418 .driver_name = KBUILD_MODNAME,
1419 .owner = THIS_MODULE,
1420 .adapter_nr = adapter_nr,
1421 .size_of_priv = sizeof(struct state),
1422
1423 .generic_bulk_ctrl_endpoint = 0x02,
1424 .generic_bulk_ctrl_endpoint_response = 0x81,
1425
1426 .identify_state = af9035_identify_state,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001427 .download_firmware = af9035_download_firmware,
1428
1429 .i2c_algo = &af9035_i2c_algo,
1430 .read_config = af9035_read_config,
1431 .frontend_attach = af9035_frontend_attach,
1432 .tuner_attach = af9035_tuner_attach,
1433 .init = af9035_init,
1434 .get_rc_config = af9035_get_rc_config,
Antti Palosaaribada3422013-01-11 20:45:11 -03001435 .get_stream_config = af9035_get_stream_config,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001436
Jose Alberto Reguero98059922012-09-23 16:48:47 -03001437 .get_adapter_count = af9035_get_adapter_count,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001438 .adapter = {
1439 {
Antti Palosaaribada3422013-01-11 20:45:11 -03001440 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1441 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1442
1443 .pid_filter_count = 32,
1444 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1445 .pid_filter = af9035_pid_filter,
1446
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001447 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1448 }, {
1449 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1450 },
1451 },
1452};
1453
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001454static const struct usb_device_id af9035_id_table[] = {
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001455 /* AF9035 devices */
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001456 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1457 &af9035_props, "Afatech AF9035 reference design", NULL) },
1458 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1459 &af9035_props, "Afatech AF9035 reference design", NULL) },
1460 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1461 &af9035_props, "Afatech AF9035 reference design", NULL) },
1462 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1463 &af9035_props, "Afatech AF9035 reference design", NULL) },
1464 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1465 &af9035_props, "Afatech AF9035 reference design", NULL) },
1466 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1467 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1468 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1469 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1470 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1471 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1472 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1473 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1474 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1475 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1476 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1477 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
Oliver Schinagld67ceb332012-09-20 14:57:17 -03001478 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1479 &af9035_props, "Asus U3100Mini Plus", NULL) },
Fabrizio Gazzatod9b75952013-02-17 18:25:34 -03001480 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
1481 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
Antti Palosaaribc3c9e12013-02-01 22:23:07 -03001482 /* IT9135 devices */
1483#if 0
1484 { DVB_USB_DEVICE(0x048d, 0x9135,
1485 &af9035_props, "IT9135 reference design", NULL) },
1486 { DVB_USB_DEVICE(0x048d, 0x9006,
1487 &af9035_props, "IT9135 reference design", NULL) },
1488#endif
Antti Palosaarib799b812013-01-11 16:22:07 -03001489 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1490 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1491 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001492 { }
1493};
1494MODULE_DEVICE_TABLE(usb, af9035_id_table);
1495
Antti Palosaari7f882c22012-03-30 09:10:08 -03001496static struct usb_driver af9035_usb_driver = {
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001497 .name = KBUILD_MODNAME,
1498 .id_table = af9035_id_table,
Antti Palosaarib799b812013-01-11 16:22:07 -03001499 .probe = af9035_probe,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001500 .disconnect = dvb_usbv2_disconnect,
1501 .suspend = dvb_usbv2_suspend,
1502 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001503 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaari5da2aec2012-06-17 23:15:03 -03001504 .no_dynamic_id = 1,
1505 .soft_unbind = 1,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001506};
1507
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001508module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001509
1510MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1511MODULE_DESCRIPTION("Afatech AF9035 driver");
1512MODULE_LICENSE("GPL");
Antti Palosaari4395e4b2012-09-12 12:19:06 -03001513MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
Antti Palosaari74c18832013-01-07 15:16:54 -03001514MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1515MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);