1786baecfSMauro Carvalho Chehab /*
2786baecfSMauro Carvalho Chehab  * Afatech AF9035 DVB USB driver
3786baecfSMauro Carvalho Chehab  *
4786baecfSMauro Carvalho Chehab  * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5786baecfSMauro Carvalho Chehab  * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6786baecfSMauro Carvalho Chehab  *
7786baecfSMauro Carvalho Chehab  *    This program is free software; you can redistribute it and/or modify
8786baecfSMauro Carvalho Chehab  *    it under the terms of the GNU General Public License as published by
9786baecfSMauro Carvalho Chehab  *    the Free Software Foundation; either version 2 of the License, or
10786baecfSMauro Carvalho Chehab  *    (at your option) any later version.
11786baecfSMauro Carvalho Chehab  *
12786baecfSMauro Carvalho Chehab  *    This program is distributed in the hope that it will be useful,
13786baecfSMauro Carvalho Chehab  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14786baecfSMauro Carvalho Chehab  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15786baecfSMauro Carvalho Chehab  *    GNU General Public License for more details.
16786baecfSMauro Carvalho Chehab  *
17786baecfSMauro Carvalho Chehab  *    You should have received a copy of the GNU General Public License along
18786baecfSMauro Carvalho Chehab  *    with this program; if not, write to the Free Software Foundation, Inc.,
19786baecfSMauro Carvalho Chehab  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20786baecfSMauro Carvalho Chehab  */
21786baecfSMauro Carvalho Chehab 
22786baecfSMauro Carvalho Chehab #include "af9035.h"
23786baecfSMauro Carvalho Chehab 
247760e148SMauro Carvalho Chehab /* Max transfer size done by I2C transfer functions */
257760e148SMauro Carvalho Chehab #define MAX_XFER_SIZE  64
267760e148SMauro Carvalho Chehab 
27786baecfSMauro Carvalho Chehab DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
28786baecfSMauro Carvalho Chehab 
29786baecfSMauro Carvalho Chehab static u16 af9035_checksum(const u8 *buf, size_t len)
30786baecfSMauro Carvalho Chehab {
31786baecfSMauro Carvalho Chehab 	size_t i;
32786baecfSMauro Carvalho Chehab 	u16 checksum = 0;
33786baecfSMauro Carvalho Chehab 
34786baecfSMauro Carvalho Chehab 	for (i = 1; i < len; i++) {
35786baecfSMauro Carvalho Chehab 		if (i % 2)
36786baecfSMauro Carvalho Chehab 			checksum += buf[i] << 8;
37786baecfSMauro Carvalho Chehab 		else
38786baecfSMauro Carvalho Chehab 			checksum += buf[i];
39786baecfSMauro Carvalho Chehab 	}
40786baecfSMauro Carvalho Chehab 	checksum = ~checksum;
41786baecfSMauro Carvalho Chehab 
42786baecfSMauro Carvalho Chehab 	return checksum;
43786baecfSMauro Carvalho Chehab }
44786baecfSMauro Carvalho Chehab 
45786baecfSMauro Carvalho Chehab static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
46786baecfSMauro Carvalho Chehab {
47786baecfSMauro Carvalho Chehab #define REQ_HDR_LEN 4 /* send header size */
48786baecfSMauro Carvalho Chehab #define ACK_HDR_LEN 3 /* rece header size */
49786baecfSMauro Carvalho Chehab #define CHECKSUM_LEN 2
50786baecfSMauro Carvalho Chehab #define USB_TIMEOUT 2000
51786baecfSMauro Carvalho Chehab 	struct state *state = d_to_priv(d);
52786baecfSMauro Carvalho Chehab 	int ret, wlen, rlen;
53786baecfSMauro Carvalho Chehab 	u16 checksum, tmp_checksum;
54786baecfSMauro Carvalho Chehab 
553484d37aSAntti Palosaari 	mutex_lock(&d->usb_mutex);
563484d37aSAntti Palosaari 
57786baecfSMauro Carvalho Chehab 	/* buffer overflow check */
58786baecfSMauro Carvalho Chehab 	if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
59786baecfSMauro Carvalho Chehab 			req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
60119f7a8cSAntti Palosaari 		dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
61e8292e28SAntti Palosaari 				KBUILD_MODNAME, req->wlen, req->rlen);
623484d37aSAntti Palosaari 		ret = -EINVAL;
630170a39bSWei Yongjun 		goto exit;
64786baecfSMauro Carvalho Chehab 	}
65786baecfSMauro Carvalho Chehab 
663484d37aSAntti Palosaari 	state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
673484d37aSAntti Palosaari 	state->buf[1] = req->mbox;
683484d37aSAntti Palosaari 	state->buf[2] = req->cmd;
693484d37aSAntti Palosaari 	state->buf[3] = state->seq++;
703484d37aSAntti Palosaari 	memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
71786baecfSMauro Carvalho Chehab 
72786baecfSMauro Carvalho Chehab 	wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
73786baecfSMauro Carvalho Chehab 	rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
74786baecfSMauro Carvalho Chehab 
75786baecfSMauro Carvalho Chehab 	/* calc and add checksum */
763484d37aSAntti Palosaari 	checksum = af9035_checksum(state->buf, state->buf[0] - 1);
773484d37aSAntti Palosaari 	state->buf[state->buf[0] - 1] = (checksum >> 8);
783484d37aSAntti Palosaari 	state->buf[state->buf[0] - 0] = (checksum & 0xff);
79786baecfSMauro Carvalho Chehab 
80786baecfSMauro Carvalho Chehab 	/* no ack for these packets */
81786baecfSMauro Carvalho Chehab 	if (req->cmd == CMD_FW_DL)
82786baecfSMauro Carvalho Chehab 		rlen = 0;
83786baecfSMauro Carvalho Chehab 
843484d37aSAntti Palosaari 	ret = dvb_usbv2_generic_rw_locked(d,
853484d37aSAntti Palosaari 			state->buf, wlen, state->buf, rlen);
86786baecfSMauro Carvalho Chehab 	if (ret)
870170a39bSWei Yongjun 		goto exit;
88786baecfSMauro Carvalho Chehab 
89786baecfSMauro Carvalho Chehab 	/* no ack for those packets */
90786baecfSMauro Carvalho Chehab 	if (req->cmd == CMD_FW_DL)
91786baecfSMauro Carvalho Chehab 		goto exit;
92786baecfSMauro Carvalho Chehab 
93786baecfSMauro Carvalho Chehab 	/* verify checksum */
943484d37aSAntti Palosaari 	checksum = af9035_checksum(state->buf, rlen - 2);
953484d37aSAntti Palosaari 	tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
96786baecfSMauro Carvalho Chehab 	if (tmp_checksum != checksum) {
97cb9114e9SAntti Palosaari 		dev_err(&d->udev->dev,
98cb9114e9SAntti Palosaari 				"%s: command=%02x checksum mismatch (%04x != %04x)\n",
99cb9114e9SAntti Palosaari 				KBUILD_MODNAME, req->cmd, tmp_checksum,
100cb9114e9SAntti Palosaari 				checksum);
101786baecfSMauro Carvalho Chehab 		ret = -EIO;
1020170a39bSWei Yongjun 		goto exit;
103786baecfSMauro Carvalho Chehab 	}
104786baecfSMauro Carvalho Chehab 
105786baecfSMauro Carvalho Chehab 	/* check status */
1063484d37aSAntti Palosaari 	if (state->buf[2]) {
1071bfd5294SAntti Palosaari 		/* fw returns status 1 when IR code was not received */
1080170a39bSWei Yongjun 		if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
1090170a39bSWei Yongjun 			ret = 1;
1100170a39bSWei Yongjun 			goto exit;
1110170a39bSWei Yongjun 		}
1121bfd5294SAntti Palosaari 
113119f7a8cSAntti Palosaari 		dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
1143484d37aSAntti Palosaari 				__func__, req->cmd, state->buf[2]);
115786baecfSMauro Carvalho Chehab 		ret = -EIO;
1160170a39bSWei Yongjun 		goto exit;
117786baecfSMauro Carvalho Chehab 	}
118786baecfSMauro Carvalho Chehab 
119786baecfSMauro Carvalho Chehab 	/* read request, copy returned data to return buf */
120786baecfSMauro Carvalho Chehab 	if (req->rlen)
1213484d37aSAntti Palosaari 		memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
122786baecfSMauro Carvalho Chehab exit:
1233484d37aSAntti Palosaari 	mutex_unlock(&d->usb_mutex);
1240170a39bSWei Yongjun 	if (ret < 0)
125119f7a8cSAntti Palosaari 		dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
126786baecfSMauro Carvalho Chehab 	return ret;
127786baecfSMauro Carvalho Chehab }
128786baecfSMauro Carvalho Chehab 
129786baecfSMauro Carvalho Chehab /* write multiple registers */
130786baecfSMauro Carvalho Chehab static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
131786baecfSMauro Carvalho Chehab {
1327760e148SMauro Carvalho Chehab 	u8 wbuf[MAX_XFER_SIZE];
133786baecfSMauro Carvalho Chehab 	u8 mbox = (reg >> 16) & 0xff;
1349323297dSAntti Palosaari 	struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL };
135786baecfSMauro Carvalho Chehab 
1367760e148SMauro Carvalho Chehab 	if (6 + len > sizeof(wbuf)) {
1377760e148SMauro Carvalho Chehab 		dev_warn(&d->udev->dev, "%s: i2c wr: len=%d is too big!\n",
1387760e148SMauro Carvalho Chehab 			 KBUILD_MODNAME, len);
1397760e148SMauro Carvalho Chehab 		return -EOPNOTSUPP;
1407760e148SMauro Carvalho Chehab 	}
1417760e148SMauro Carvalho Chehab 
142786baecfSMauro Carvalho Chehab 	wbuf[0] = len;
143786baecfSMauro Carvalho Chehab 	wbuf[1] = 2;
144786baecfSMauro Carvalho Chehab 	wbuf[2] = 0;
145786baecfSMauro Carvalho Chehab 	wbuf[3] = 0;
146786baecfSMauro Carvalho Chehab 	wbuf[4] = (reg >> 8) & 0xff;
147786baecfSMauro Carvalho Chehab 	wbuf[5] = (reg >> 0) & 0xff;
148786baecfSMauro Carvalho Chehab 	memcpy(&wbuf[6], val, len);
149786baecfSMauro Carvalho Chehab 
150786baecfSMauro Carvalho Chehab 	return af9035_ctrl_msg(d, &req);
151786baecfSMauro Carvalho Chehab }
152786baecfSMauro Carvalho Chehab 
153786baecfSMauro Carvalho Chehab /* read multiple registers */
154786baecfSMauro Carvalho Chehab static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
155786baecfSMauro Carvalho Chehab {
156786baecfSMauro Carvalho Chehab 	u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
157786baecfSMauro Carvalho Chehab 	u8 mbox = (reg >> 16) & 0xff;
158786baecfSMauro Carvalho Chehab 	struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
159786baecfSMauro Carvalho Chehab 
160786baecfSMauro Carvalho Chehab 	return af9035_ctrl_msg(d, &req);
161786baecfSMauro Carvalho Chehab }
162786baecfSMauro Carvalho Chehab 
163786baecfSMauro Carvalho Chehab /* write single register */
164786baecfSMauro Carvalho Chehab static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
165786baecfSMauro Carvalho Chehab {
166786baecfSMauro Carvalho Chehab 	return af9035_wr_regs(d, reg, &val, 1);
167786baecfSMauro Carvalho Chehab }
168786baecfSMauro Carvalho Chehab 
169786baecfSMauro Carvalho Chehab /* read single register */
170786baecfSMauro Carvalho Chehab static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
171786baecfSMauro Carvalho Chehab {
172786baecfSMauro Carvalho Chehab 	return af9035_rd_regs(d, reg, val, 1);
173786baecfSMauro Carvalho Chehab }
174786baecfSMauro Carvalho Chehab 
175786baecfSMauro Carvalho Chehab /* write single register with mask */
176786baecfSMauro Carvalho Chehab static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
177786baecfSMauro Carvalho Chehab 		u8 mask)
178786baecfSMauro Carvalho Chehab {
179786baecfSMauro Carvalho Chehab 	int ret;
180786baecfSMauro Carvalho Chehab 	u8 tmp;
181786baecfSMauro Carvalho Chehab 
182786baecfSMauro Carvalho Chehab 	/* no need for read if whole reg is written */
183786baecfSMauro Carvalho Chehab 	if (mask != 0xff) {
184786baecfSMauro Carvalho Chehab 		ret = af9035_rd_regs(d, reg, &tmp, 1);
185786baecfSMauro Carvalho Chehab 		if (ret)
186786baecfSMauro Carvalho Chehab 			return ret;
187786baecfSMauro Carvalho Chehab 
188786baecfSMauro Carvalho Chehab 		val &= mask;
189786baecfSMauro Carvalho Chehab 		tmp &= ~mask;
190786baecfSMauro Carvalho Chehab 		val |= tmp;
191786baecfSMauro Carvalho Chehab 	}
192786baecfSMauro Carvalho Chehab 
193786baecfSMauro Carvalho Chehab 	return af9035_wr_regs(d, reg, &val, 1);
194786baecfSMauro Carvalho Chehab }
195786baecfSMauro Carvalho Chehab 
1963b2a5e8cSAntti Palosaari static int af9035_add_i2c_dev(struct dvb_usb_device *d, char *type, u8 addr,
1975dcf5bf6SOlli Salonen 		void *platform_data, struct i2c_adapter *adapter)
1983b2a5e8cSAntti Palosaari {
1993b2a5e8cSAntti Palosaari 	int ret, num;
2003b2a5e8cSAntti Palosaari 	struct state *state = d_to_priv(d);
2013b2a5e8cSAntti Palosaari 	struct i2c_client *client;
2023b2a5e8cSAntti Palosaari 	struct i2c_board_info board_info = {
2033b2a5e8cSAntti Palosaari 		.addr = addr,
2043b2a5e8cSAntti Palosaari 		.platform_data = platform_data,
2053b2a5e8cSAntti Palosaari 	};
2063b2a5e8cSAntti Palosaari 
2073b2a5e8cSAntti Palosaari 	strlcpy(board_info.type, type, I2C_NAME_SIZE);
2083b2a5e8cSAntti Palosaari 
2093b2a5e8cSAntti Palosaari 	/* find first free client */
2103b2a5e8cSAntti Palosaari 	for (num = 0; num < AF9035_I2C_CLIENT_MAX; num++) {
2113b2a5e8cSAntti Palosaari 		if (state->i2c_client[num] == NULL)
2123b2a5e8cSAntti Palosaari 			break;
2133b2a5e8cSAntti Palosaari 	}
2143b2a5e8cSAntti Palosaari 
2153b2a5e8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num);
2163b2a5e8cSAntti Palosaari 
2173b2a5e8cSAntti Palosaari 	if (num == AF9035_I2C_CLIENT_MAX) {
2183b2a5e8cSAntti Palosaari 		dev_err(&d->udev->dev, "%s: I2C client out of index\n",
2193b2a5e8cSAntti Palosaari 				KBUILD_MODNAME);
2203b2a5e8cSAntti Palosaari 		ret = -ENODEV;
2213b2a5e8cSAntti Palosaari 		goto err;
2223b2a5e8cSAntti Palosaari 	}
2233b2a5e8cSAntti Palosaari 
2243b2a5e8cSAntti Palosaari 	request_module(board_info.type);
2253b2a5e8cSAntti Palosaari 
2263b2a5e8cSAntti Palosaari 	/* register I2C device */
2273b2a5e8cSAntti Palosaari 	client = i2c_new_device(adapter, &board_info);
2283b2a5e8cSAntti Palosaari 	if (client == NULL || client->dev.driver == NULL) {
2293b2a5e8cSAntti Palosaari 		ret = -ENODEV;
2303b2a5e8cSAntti Palosaari 		goto err;
2313b2a5e8cSAntti Palosaari 	}
2323b2a5e8cSAntti Palosaari 
2333b2a5e8cSAntti Palosaari 	/* increase I2C driver usage count */
2343b2a5e8cSAntti Palosaari 	if (!try_module_get(client->dev.driver->owner)) {
2353b2a5e8cSAntti Palosaari 		i2c_unregister_device(client);
2363b2a5e8cSAntti Palosaari 		ret = -ENODEV;
2373b2a5e8cSAntti Palosaari 		goto err;
2383b2a5e8cSAntti Palosaari 	}
2393b2a5e8cSAntti Palosaari 
2403b2a5e8cSAntti Palosaari 	state->i2c_client[num] = client;
2413b2a5e8cSAntti Palosaari 	return 0;
2423b2a5e8cSAntti Palosaari err:
2433b2a5e8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
2443b2a5e8cSAntti Palosaari 	return ret;
2453b2a5e8cSAntti Palosaari }
2463b2a5e8cSAntti Palosaari 
2473b2a5e8cSAntti Palosaari static void af9035_del_i2c_dev(struct dvb_usb_device *d)
2483b2a5e8cSAntti Palosaari {
2493b2a5e8cSAntti Palosaari 	int num;
2503b2a5e8cSAntti Palosaari 	struct state *state = d_to_priv(d);
2513b2a5e8cSAntti Palosaari 	struct i2c_client *client;
2523b2a5e8cSAntti Palosaari 
2533b2a5e8cSAntti Palosaari 	/* find last used client */
2543b2a5e8cSAntti Palosaari 	num = AF9035_I2C_CLIENT_MAX;
2553b2a5e8cSAntti Palosaari 	while (num--) {
2563b2a5e8cSAntti Palosaari 		if (state->i2c_client[num] != NULL)
2573b2a5e8cSAntti Palosaari 			break;
2583b2a5e8cSAntti Palosaari 	}
2593b2a5e8cSAntti Palosaari 
2603b2a5e8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num);
2613b2a5e8cSAntti Palosaari 
2623b2a5e8cSAntti Palosaari 	if (num == -1) {
2633b2a5e8cSAntti Palosaari 		dev_err(&d->udev->dev, "%s: I2C client out of index\n",
2643b2a5e8cSAntti Palosaari 				KBUILD_MODNAME);
2653b2a5e8cSAntti Palosaari 		goto err;
2663b2a5e8cSAntti Palosaari 	}
2673b2a5e8cSAntti Palosaari 
2683b2a5e8cSAntti Palosaari 	client = state->i2c_client[num];
2693b2a5e8cSAntti Palosaari 
2703b2a5e8cSAntti Palosaari 	/* decrease I2C driver usage count */
2713b2a5e8cSAntti Palosaari 	module_put(client->dev.driver->owner);
2723b2a5e8cSAntti Palosaari 
2733b2a5e8cSAntti Palosaari 	/* unregister I2C device */
2743b2a5e8cSAntti Palosaari 	i2c_unregister_device(client);
2753b2a5e8cSAntti Palosaari 
2763b2a5e8cSAntti Palosaari 	state->i2c_client[num] = NULL;
2773b2a5e8cSAntti Palosaari 	return;
2783b2a5e8cSAntti Palosaari err:
2793b2a5e8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed\n", __func__);
2803b2a5e8cSAntti Palosaari }
2813b2a5e8cSAntti Palosaari 
282786baecfSMauro Carvalho Chehab static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
283786baecfSMauro Carvalho Chehab 		struct i2c_msg msg[], int num)
284786baecfSMauro Carvalho Chehab {
285786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
286786baecfSMauro Carvalho Chehab 	struct state *state = d_to_priv(d);
287786baecfSMauro Carvalho Chehab 	int ret;
288786baecfSMauro Carvalho Chehab 
289786baecfSMauro Carvalho Chehab 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
290786baecfSMauro Carvalho Chehab 		return -EAGAIN;
291786baecfSMauro Carvalho Chehab 
292786baecfSMauro Carvalho Chehab 	/*
293786baecfSMauro Carvalho Chehab 	 * I2C sub header is 5 bytes long. Meaning of those bytes are:
294786baecfSMauro Carvalho Chehab 	 * 0: data len
295786baecfSMauro Carvalho Chehab 	 * 1: I2C addr << 1
296786baecfSMauro Carvalho Chehab 	 * 2: reg addr len
297786baecfSMauro Carvalho Chehab 	 *    byte 3 and 4 can be used as reg addr
298786baecfSMauro Carvalho Chehab 	 * 3: reg addr MSB
299786baecfSMauro Carvalho Chehab 	 *    used when reg addr len is set to 2
300786baecfSMauro Carvalho Chehab 	 * 4: reg addr LSB
301786baecfSMauro Carvalho Chehab 	 *    used when reg addr len is set to 1 or 2
302786baecfSMauro Carvalho Chehab 	 *
303786baecfSMauro Carvalho Chehab 	 * For the simplify we do not use register addr at all.
304786baecfSMauro Carvalho Chehab 	 * NOTE: As a firmware knows tuner type there is very small possibility
305786baecfSMauro Carvalho Chehab 	 * there could be some tuner I2C hacks done by firmware and this may
306786baecfSMauro Carvalho Chehab 	 * lead problems if firmware expects those bytes are used.
307f5b00a76SAntti Palosaari 	 *
308f5b00a76SAntti Palosaari 	 * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator.
309f5b00a76SAntti Palosaari 	 * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual
310f5b00a76SAntti Palosaari 	 * tuner devices, there is also external AF9033 demodulator connected
311f5b00a76SAntti Palosaari 	 * via external I2C bus. All AF9033 demod I2C traffic, both single and
312f5b00a76SAntti Palosaari 	 * dual tuner configuration, is covered by firmware - actual USB IO
313f5b00a76SAntti Palosaari 	 * looks just like a memory access.
314f5b00a76SAntti Palosaari 	 * In case of IT913x chip, there is own tuner driver. It is implemented
315f5b00a76SAntti Palosaari 	 * currently as a I2C driver, even tuner IP block is likely build
316f5b00a76SAntti Palosaari 	 * directly into the demodulator memory space and there is no own I2C
317f5b00a76SAntti Palosaari 	 * bus. I2C subsystem does not allow register multiple devices to same
318f5b00a76SAntti Palosaari 	 * bus, having same slave address. Due to that we reuse demod address,
319f5b00a76SAntti Palosaari 	 * shifted by one bit, on that case.
320786baecfSMauro Carvalho Chehab 	 */
321ee36381eSAntti Palosaari #define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \
322ee36381eSAntti Palosaari 	(_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
323ee36381eSAntti Palosaari #define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \
324ee36381eSAntti Palosaari 	(_num == 1 && !(_msg[0].flags & I2C_M_RD))
325ee36381eSAntti Palosaari #define AF9035_IS_I2C_XFER_READ(_msg, _num) \
326ee36381eSAntti Palosaari 	(_num == 1 && (_msg[0].flags & I2C_M_RD))
327ee36381eSAntti Palosaari 
328ee36381eSAntti Palosaari 	if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) {
329786baecfSMauro Carvalho Chehab 		if (msg[0].len > 40 || msg[1].len > 40) {
330786baecfSMauro Carvalho Chehab 			/* TODO: correct limits > 40 */
331786baecfSMauro Carvalho Chehab 			ret = -EOPNOTSUPP;
332a781edd1SAntti Palosaari 		} else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
333a781edd1SAntti Palosaari 			   (msg[0].addr == state->af9033_i2c_addr[1]) ||
334f5b00a76SAntti Palosaari 			   (state->chip_type == 0x9135)) {
335bf97b637SAntti Palosaari 			/* demod access via firmware interface */
336786baecfSMauro Carvalho Chehab 			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
337786baecfSMauro Carvalho Chehab 					msg[0].buf[2];
338bf97b637SAntti Palosaari 
339a781edd1SAntti Palosaari 			if (msg[0].addr == state->af9033_i2c_addr[1] ||
340a781edd1SAntti Palosaari 			    msg[0].addr == (state->af9033_i2c_addr[1] >> 1))
3419805992fSJose Alberto Reguero 				reg |= 0x100000;
342bf97b637SAntti Palosaari 
343786baecfSMauro Carvalho Chehab 			ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
344786baecfSMauro Carvalho Chehab 					msg[1].len);
345786baecfSMauro Carvalho Chehab 		} else {
346ee36381eSAntti Palosaari 			/* I2C write + read */
3477760e148SMauro Carvalho Chehab 			u8 buf[MAX_XFER_SIZE];
3489323297dSAntti Palosaari 			struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len,
349786baecfSMauro Carvalho Chehab 					buf, msg[1].len, msg[1].buf };
3507760e148SMauro Carvalho Chehab 
3519805992fSJose Alberto Reguero 			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
352786baecfSMauro Carvalho Chehab 			buf[0] = msg[1].len;
353bf97b637SAntti Palosaari 			buf[1] = msg[0].addr << 1;
354786baecfSMauro Carvalho Chehab 			buf[2] = 0x00; /* reg addr len */
355786baecfSMauro Carvalho Chehab 			buf[3] = 0x00; /* reg addr MSB */
356786baecfSMauro Carvalho Chehab 			buf[4] = 0x00; /* reg addr LSB */
357786baecfSMauro Carvalho Chehab 			memcpy(&buf[5], msg[0].buf, msg[0].len);
358786baecfSMauro Carvalho Chehab 			ret = af9035_ctrl_msg(d, &req);
359786baecfSMauro Carvalho Chehab 		}
360ee36381eSAntti Palosaari 	} else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) {
361786baecfSMauro Carvalho Chehab 		if (msg[0].len > 40) {
362786baecfSMauro Carvalho Chehab 			/* TODO: correct limits > 40 */
363786baecfSMauro Carvalho Chehab 			ret = -EOPNOTSUPP;
364a781edd1SAntti Palosaari 		} else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
365a781edd1SAntti Palosaari 			   (msg[0].addr == state->af9033_i2c_addr[1]) ||
366f5b00a76SAntti Palosaari 			   (state->chip_type == 0x9135)) {
367bf97b637SAntti Palosaari 			/* demod access via firmware interface */
368786baecfSMauro Carvalho Chehab 			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
369786baecfSMauro Carvalho Chehab 					msg[0].buf[2];
370bf97b637SAntti Palosaari 
371a781edd1SAntti Palosaari 			if (msg[0].addr == state->af9033_i2c_addr[1] ||
372a781edd1SAntti Palosaari 			    msg[0].addr == (state->af9033_i2c_addr[1] >> 1))
3739805992fSJose Alberto Reguero 				reg |= 0x100000;
374bf97b637SAntti Palosaari 
375786baecfSMauro Carvalho Chehab 			ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
376786baecfSMauro Carvalho Chehab 					msg[0].len - 3);
377786baecfSMauro Carvalho Chehab 		} else {
378ee36381eSAntti Palosaari 			/* I2C write */
3797760e148SMauro Carvalho Chehab 			u8 buf[MAX_XFER_SIZE];
3809323297dSAntti Palosaari 			struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len,
3819323297dSAntti Palosaari 					buf, 0, NULL };
3827760e148SMauro Carvalho Chehab 
3839805992fSJose Alberto Reguero 			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
384786baecfSMauro Carvalho Chehab 			buf[0] = msg[0].len;
385bf97b637SAntti Palosaari 			buf[1] = msg[0].addr << 1;
386786baecfSMauro Carvalho Chehab 			buf[2] = 0x00; /* reg addr len */
387786baecfSMauro Carvalho Chehab 			buf[3] = 0x00; /* reg addr MSB */
388786baecfSMauro Carvalho Chehab 			buf[4] = 0x00; /* reg addr LSB */
389786baecfSMauro Carvalho Chehab 			memcpy(&buf[5], msg[0].buf, msg[0].len);
390786baecfSMauro Carvalho Chehab 			ret = af9035_ctrl_msg(d, &req);
391786baecfSMauro Carvalho Chehab 		}
392ee36381eSAntti Palosaari 	} else if (AF9035_IS_I2C_XFER_READ(msg, num)) {
3933b98c347SAntti Palosaari 		if (msg[0].len > 40) {
3943b98c347SAntti Palosaari 			/* TODO: correct limits > 40 */
3953b98c347SAntti Palosaari 			ret = -EOPNOTSUPP;
3963b98c347SAntti Palosaari 		} else {
397ee36381eSAntti Palosaari 			/* I2C read */
3983b98c347SAntti Palosaari 			u8 buf[5];
3993b98c347SAntti Palosaari 			struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
4003b98c347SAntti Palosaari 					buf, msg[0].len, msg[0].buf };
4013b98c347SAntti Palosaari 			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
4023b98c347SAntti Palosaari 			buf[0] = msg[0].len;
4033b98c347SAntti Palosaari 			buf[1] = msg[0].addr << 1;
4043b98c347SAntti Palosaari 			buf[2] = 0x00; /* reg addr len */
4053b98c347SAntti Palosaari 			buf[3] = 0x00; /* reg addr MSB */
4063b98c347SAntti Palosaari 			buf[4] = 0x00; /* reg addr LSB */
4073b98c347SAntti Palosaari 			ret = af9035_ctrl_msg(d, &req);
4083b98c347SAntti Palosaari 		}
409786baecfSMauro Carvalho Chehab 	} else {
410786baecfSMauro Carvalho Chehab 		/*
4113b98c347SAntti Palosaari 		 * We support only three kind of I2C transactions:
412ee36381eSAntti Palosaari 		 * 1) 1 x write + 1 x read (repeated start)
413786baecfSMauro Carvalho Chehab 		 * 2) 1 x write
4143b98c347SAntti Palosaari 		 * 3) 1 x read
415786baecfSMauro Carvalho Chehab 		 */
416786baecfSMauro Carvalho Chehab 		ret = -EOPNOTSUPP;
417786baecfSMauro Carvalho Chehab 	}
418786baecfSMauro Carvalho Chehab 
419786baecfSMauro Carvalho Chehab 	mutex_unlock(&d->i2c_mutex);
420786baecfSMauro Carvalho Chehab 
421786baecfSMauro Carvalho Chehab 	if (ret < 0)
422786baecfSMauro Carvalho Chehab 		return ret;
423786baecfSMauro Carvalho Chehab 	else
424786baecfSMauro Carvalho Chehab 		return num;
425786baecfSMauro Carvalho Chehab }
426786baecfSMauro Carvalho Chehab 
427786baecfSMauro Carvalho Chehab static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
428786baecfSMauro Carvalho Chehab {
429786baecfSMauro Carvalho Chehab 	return I2C_FUNC_I2C;
430786baecfSMauro Carvalho Chehab }
431786baecfSMauro Carvalho Chehab 
432786baecfSMauro Carvalho Chehab static struct i2c_algorithm af9035_i2c_algo = {
433786baecfSMauro Carvalho Chehab 	.master_xfer = af9035_i2c_master_xfer,
434786baecfSMauro Carvalho Chehab 	.functionality = af9035_i2c_functionality,
435786baecfSMauro Carvalho Chehab };
436786baecfSMauro Carvalho Chehab 
437786baecfSMauro Carvalho Chehab static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
438786baecfSMauro Carvalho Chehab {
43974c1883aSAntti Palosaari 	struct state *state = d_to_priv(d);
440786baecfSMauro Carvalho Chehab 	int ret;
441786baecfSMauro Carvalho Chehab 	u8 wbuf[1] = { 1 };
442786baecfSMauro Carvalho Chehab 	u8 rbuf[4];
443786baecfSMauro Carvalho Chehab 	struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
444786baecfSMauro Carvalho Chehab 			sizeof(rbuf), rbuf };
445786baecfSMauro Carvalho Chehab 
44674c1883aSAntti Palosaari 	ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
44774c1883aSAntti Palosaari 	if (ret < 0)
44874c1883aSAntti Palosaari 		goto err;
44974c1883aSAntti Palosaari 
45074c1883aSAntti Palosaari 	state->chip_version = rbuf[0];
45174c1883aSAntti Palosaari 	state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
45274c1883aSAntti Palosaari 
45374c1883aSAntti Palosaari 	ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
45474c1883aSAntti Palosaari 	if (ret < 0)
45574c1883aSAntti Palosaari 		goto err;
45674c1883aSAntti Palosaari 
45774c1883aSAntti Palosaari 	dev_info(&d->udev->dev,
45874c1883aSAntti Palosaari 			"%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
459e8292e28SAntti Palosaari 			KBUILD_MODNAME, state->prechip_version,
460e8292e28SAntti Palosaari 			state->chip_version, state->chip_type);
46174c1883aSAntti Palosaari 
46274c1883aSAntti Palosaari 	if (state->chip_type == 0x9135) {
463bc3c9e10SAntti Palosaari 		if (state->chip_version == 0x02)
46474c1883aSAntti Palosaari 			*name = AF9035_FIRMWARE_IT9135_V2;
46574c1883aSAntti Palosaari 		else
46674c1883aSAntti Palosaari 			*name = AF9035_FIRMWARE_IT9135_V1;
467df8f1be1SAntti Palosaari 		state->eeprom_addr = EEPROM_BASE_IT9135;
46874c1883aSAntti Palosaari 	} else {
46974c1883aSAntti Palosaari 		*name = AF9035_FIRMWARE_AF9035;
470df8f1be1SAntti Palosaari 		state->eeprom_addr = EEPROM_BASE_AF9035;
47174c1883aSAntti Palosaari 	}
47274c1883aSAntti Palosaari 
473786baecfSMauro Carvalho Chehab 	ret = af9035_ctrl_msg(d, &req);
474786baecfSMauro Carvalho Chehab 	if (ret < 0)
475786baecfSMauro Carvalho Chehab 		goto err;
476786baecfSMauro Carvalho Chehab 
477119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
478786baecfSMauro Carvalho Chehab 	if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
479786baecfSMauro Carvalho Chehab 		ret = WARM;
480786baecfSMauro Carvalho Chehab 	else
481786baecfSMauro Carvalho Chehab 		ret = COLD;
482786baecfSMauro Carvalho Chehab 
483786baecfSMauro Carvalho Chehab 	return ret;
484786baecfSMauro Carvalho Chehab 
485786baecfSMauro Carvalho Chehab err:
486119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
487786baecfSMauro Carvalho Chehab 
488786baecfSMauro Carvalho Chehab 	return ret;
489786baecfSMauro Carvalho Chehab }
490786baecfSMauro Carvalho Chehab 
4918229da50SAntti Palosaari static int af9035_download_firmware_old(struct dvb_usb_device *d,
492786baecfSMauro Carvalho Chehab 		const struct firmware *fw)
493786baecfSMauro Carvalho Chehab {
494786baecfSMauro Carvalho Chehab 	int ret, i, j, len;
495786baecfSMauro Carvalho Chehab 	u8 wbuf[1];
496786baecfSMauro Carvalho Chehab 	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
497786baecfSMauro Carvalho Chehab 	struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
498df8f1be1SAntti Palosaari 	u8 hdr_core;
499786baecfSMauro Carvalho Chehab 	u16 hdr_addr, hdr_data_len, hdr_checksum;
500786baecfSMauro Carvalho Chehab 	#define MAX_DATA 58
501786baecfSMauro Carvalho Chehab 	#define HDR_SIZE 7
502786baecfSMauro Carvalho Chehab 
503786baecfSMauro Carvalho Chehab 	/*
504786baecfSMauro Carvalho Chehab 	 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
505786baecfSMauro Carvalho Chehab 	 *
506786baecfSMauro Carvalho Chehab 	 * byte 0: MCS 51 core
507786baecfSMauro Carvalho Chehab 	 *  There are two inside the AF9035 (1=Link and 2=OFDM) with separate
508786baecfSMauro Carvalho Chehab 	 *  address spaces
509786baecfSMauro Carvalho Chehab 	 * byte 1-2: Big endian destination address
510786baecfSMauro Carvalho Chehab 	 * byte 3-4: Big endian number of data bytes following the header
511786baecfSMauro Carvalho Chehab 	 * byte 5-6: Big endian header checksum, apparently ignored by the chip
512786baecfSMauro Carvalho Chehab 	 *  Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
513786baecfSMauro Carvalho Chehab 	 */
514786baecfSMauro Carvalho Chehab 
515786baecfSMauro Carvalho Chehab 	for (i = fw->size; i > HDR_SIZE;) {
516786baecfSMauro Carvalho Chehab 		hdr_core = fw->data[fw->size - i + 0];
517786baecfSMauro Carvalho Chehab 		hdr_addr = fw->data[fw->size - i + 1] << 8;
518786baecfSMauro Carvalho Chehab 		hdr_addr |= fw->data[fw->size - i + 2] << 0;
519786baecfSMauro Carvalho Chehab 		hdr_data_len = fw->data[fw->size - i + 3] << 8;
520786baecfSMauro Carvalho Chehab 		hdr_data_len |= fw->data[fw->size - i + 4] << 0;
521786baecfSMauro Carvalho Chehab 		hdr_checksum = fw->data[fw->size - i + 5] << 8;
522786baecfSMauro Carvalho Chehab 		hdr_checksum |= fw->data[fw->size - i + 6] << 0;
523786baecfSMauro Carvalho Chehab 
524cb9114e9SAntti Palosaari 		dev_dbg(&d->udev->dev,
525cb9114e9SAntti Palosaari 				"%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
526cb9114e9SAntti Palosaari 				__func__, hdr_core, hdr_addr, hdr_data_len,
527cb9114e9SAntti Palosaari 				hdr_checksum);
528786baecfSMauro Carvalho Chehab 
529786baecfSMauro Carvalho Chehab 		if (((hdr_core != 1) && (hdr_core != 2)) ||
530786baecfSMauro Carvalho Chehab 				(hdr_data_len > i)) {
531119f7a8cSAntti Palosaari 			dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
532786baecfSMauro Carvalho Chehab 			break;
533786baecfSMauro Carvalho Chehab 		}
534786baecfSMauro Carvalho Chehab 
535786baecfSMauro Carvalho Chehab 		/* download begin packet */
536786baecfSMauro Carvalho Chehab 		req.cmd = CMD_FW_DL_BEGIN;
537786baecfSMauro Carvalho Chehab 		ret = af9035_ctrl_msg(d, &req);
538786baecfSMauro Carvalho Chehab 		if (ret < 0)
539786baecfSMauro Carvalho Chehab 			goto err;
540786baecfSMauro Carvalho Chehab 
541786baecfSMauro Carvalho Chehab 		/* download firmware packet(s) */
542786baecfSMauro Carvalho Chehab 		for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
543786baecfSMauro Carvalho Chehab 			len = j;
544786baecfSMauro Carvalho Chehab 			if (len > MAX_DATA)
545786baecfSMauro Carvalho Chehab 				len = MAX_DATA;
546786baecfSMauro Carvalho Chehab 			req_fw_dl.wlen = len;
547786baecfSMauro Carvalho Chehab 			req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
548786baecfSMauro Carvalho Chehab 					HDR_SIZE + hdr_data_len - j];
549786baecfSMauro Carvalho Chehab 			ret = af9035_ctrl_msg(d, &req_fw_dl);
550786baecfSMauro Carvalho Chehab 			if (ret < 0)
551786baecfSMauro Carvalho Chehab 				goto err;
552786baecfSMauro Carvalho Chehab 		}
553786baecfSMauro Carvalho Chehab 
554786baecfSMauro Carvalho Chehab 		/* download end packet */
555786baecfSMauro Carvalho Chehab 		req.cmd = CMD_FW_DL_END;
556786baecfSMauro Carvalho Chehab 		ret = af9035_ctrl_msg(d, &req);
557786baecfSMauro Carvalho Chehab 		if (ret < 0)
558786baecfSMauro Carvalho Chehab 			goto err;
559786baecfSMauro Carvalho Chehab 
560786baecfSMauro Carvalho Chehab 		i -= hdr_data_len + HDR_SIZE;
561786baecfSMauro Carvalho Chehab 
562119f7a8cSAntti Palosaari 		dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
563119f7a8cSAntti Palosaari 				__func__, fw->size - i);
564786baecfSMauro Carvalho Chehab 	}
565786baecfSMauro Carvalho Chehab 
566ff4e3fe8SAntti Palosaari 	/* print warn if firmware is bad, continue and see what happens */
567ff4e3fe8SAntti Palosaari 	if (i)
568ff4e3fe8SAntti Palosaari 		dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
569ff4e3fe8SAntti Palosaari 
570786baecfSMauro Carvalho Chehab 	return 0;
571786baecfSMauro Carvalho Chehab 
572786baecfSMauro Carvalho Chehab err:
573119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
574786baecfSMauro Carvalho Chehab 
575786baecfSMauro Carvalho Chehab 	return ret;
576786baecfSMauro Carvalho Chehab }
577786baecfSMauro Carvalho Chehab 
5788229da50SAntti Palosaari static int af9035_download_firmware_new(struct dvb_usb_device *d,
579786baecfSMauro Carvalho Chehab 		const struct firmware *fw)
580786baecfSMauro Carvalho Chehab {
581786baecfSMauro Carvalho Chehab 	int ret, i, i_prev;
582786baecfSMauro Carvalho Chehab 	struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
583786baecfSMauro Carvalho Chehab 	#define HDR_SIZE 7
584786baecfSMauro Carvalho Chehab 
585786baecfSMauro Carvalho Chehab 	/*
586786baecfSMauro Carvalho Chehab 	 * There seems to be following firmware header. Meaning of bytes 0-3
587786baecfSMauro Carvalho Chehab 	 * is unknown.
588786baecfSMauro Carvalho Chehab 	 *
589786baecfSMauro Carvalho Chehab 	 * 0: 3
590786baecfSMauro Carvalho Chehab 	 * 1: 0, 1
591786baecfSMauro Carvalho Chehab 	 * 2: 0
592786baecfSMauro Carvalho Chehab 	 * 3: 1, 2, 3
593786baecfSMauro Carvalho Chehab 	 * 4: addr MSB
594786baecfSMauro Carvalho Chehab 	 * 5: addr LSB
595786baecfSMauro Carvalho Chehab 	 * 6: count of data bytes ?
596786baecfSMauro Carvalho Chehab 	 */
597786baecfSMauro Carvalho Chehab 	for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
598786baecfSMauro Carvalho Chehab 		if (i == fw->size ||
599786baecfSMauro Carvalho Chehab 				(fw->data[i + 0] == 0x03 &&
600786baecfSMauro Carvalho Chehab 				(fw->data[i + 1] == 0x00 ||
601786baecfSMauro Carvalho Chehab 				fw->data[i + 1] == 0x01) &&
602786baecfSMauro Carvalho Chehab 				fw->data[i + 2] == 0x00)) {
603786baecfSMauro Carvalho Chehab 			req_fw_dl.wlen = i - i_prev;
604786baecfSMauro Carvalho Chehab 			req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
605786baecfSMauro Carvalho Chehab 			i_prev = i;
606786baecfSMauro Carvalho Chehab 			ret = af9035_ctrl_msg(d, &req_fw_dl);
607786baecfSMauro Carvalho Chehab 			if (ret < 0)
608786baecfSMauro Carvalho Chehab 				goto err;
609786baecfSMauro Carvalho Chehab 
610119f7a8cSAntti Palosaari 			dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
611119f7a8cSAntti Palosaari 					__func__, i);
612786baecfSMauro Carvalho Chehab 		}
613786baecfSMauro Carvalho Chehab 	}
614786baecfSMauro Carvalho Chehab 
615df8f1be1SAntti Palosaari 	return 0;
616df8f1be1SAntti Palosaari 
617df8f1be1SAntti Palosaari err:
618df8f1be1SAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
619df8f1be1SAntti Palosaari 
620df8f1be1SAntti Palosaari 	return ret;
621df8f1be1SAntti Palosaari }
622df8f1be1SAntti Palosaari 
623df8f1be1SAntti Palosaari static int af9035_download_firmware(struct dvb_usb_device *d,
624df8f1be1SAntti Palosaari 		const struct firmware *fw)
625df8f1be1SAntti Palosaari {
626df8f1be1SAntti Palosaari 	struct state *state = d_to_priv(d);
627df8f1be1SAntti Palosaari 	int ret;
628df8f1be1SAntti Palosaari 	u8 wbuf[1];
629df8f1be1SAntti Palosaari 	u8 rbuf[4];
630df8f1be1SAntti Palosaari 	u8 tmp;
631df8f1be1SAntti Palosaari 	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
632df8f1be1SAntti Palosaari 	struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
633c10989a2SAntti Palosaari 
634df8f1be1SAntti Palosaari 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
635df8f1be1SAntti Palosaari 
636df8f1be1SAntti Palosaari 	/*
637df8f1be1SAntti Palosaari 	 * In case of dual tuner configuration we need to do some extra
638df8f1be1SAntti Palosaari 	 * initialization in order to download firmware to slave demod too,
639df8f1be1SAntti Palosaari 	 * which is done by master demod.
640df8f1be1SAntti Palosaari 	 * Master feeds also clock and controls power via GPIO.
641df8f1be1SAntti Palosaari 	 */
642d716ef46SAntti Palosaari 	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
643df8f1be1SAntti Palosaari 	if (ret < 0)
644df8f1be1SAntti Palosaari 		goto err;
645df8f1be1SAntti Palosaari 
646d716ef46SAntti Palosaari 	if (tmp == 1 || tmp == 3) {
647df8f1be1SAntti Palosaari 		/* configure gpioh1, reset & power slave demod */
648df8f1be1SAntti Palosaari 		ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
649df8f1be1SAntti Palosaari 		if (ret < 0)
650df8f1be1SAntti Palosaari 			goto err;
651df8f1be1SAntti Palosaari 
652df8f1be1SAntti Palosaari 		ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
653df8f1be1SAntti Palosaari 		if (ret < 0)
654df8f1be1SAntti Palosaari 			goto err;
655df8f1be1SAntti Palosaari 
656df8f1be1SAntti Palosaari 		ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
657df8f1be1SAntti Palosaari 		if (ret < 0)
658df8f1be1SAntti Palosaari 			goto err;
659df8f1be1SAntti Palosaari 
660df8f1be1SAntti Palosaari 		usleep_range(10000, 50000);
661df8f1be1SAntti Palosaari 
662df8f1be1SAntti Palosaari 		ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
663df8f1be1SAntti Palosaari 		if (ret < 0)
664df8f1be1SAntti Palosaari 			goto err;
665df8f1be1SAntti Palosaari 
666df8f1be1SAntti Palosaari 		/* tell the slave I2C address */
667df8f1be1SAntti Palosaari 		ret = af9035_rd_reg(d,
668df8f1be1SAntti Palosaari 				state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
669df8f1be1SAntti Palosaari 				&tmp);
670df8f1be1SAntti Palosaari 		if (ret < 0)
671df8f1be1SAntti Palosaari 			goto err;
672df8f1be1SAntti Palosaari 
67314992f01SAntti Palosaari 		/* use default I2C address if eeprom has no address set */
674a1310ff4SMalcolm Priestley 		if (!tmp)
675a1310ff4SMalcolm Priestley 			tmp = 0x3a;
676a1310ff4SMalcolm Priestley 
67714992f01SAntti Palosaari 		if (state->chip_type == 0x9135) {
678df8f1be1SAntti Palosaari 			ret = af9035_wr_reg(d, 0x004bfb, tmp);
679df8f1be1SAntti Palosaari 			if (ret < 0)
680df8f1be1SAntti Palosaari 				goto err;
681df8f1be1SAntti Palosaari 		} else {
682df8f1be1SAntti Palosaari 			ret = af9035_wr_reg(d, 0x00417f, tmp);
683df8f1be1SAntti Palosaari 			if (ret < 0)
684df8f1be1SAntti Palosaari 				goto err;
685df8f1be1SAntti Palosaari 
686df8f1be1SAntti Palosaari 			/* enable clock out */
687df8f1be1SAntti Palosaari 			ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
688df8f1be1SAntti Palosaari 			if (ret < 0)
689df8f1be1SAntti Palosaari 				goto err;
690df8f1be1SAntti Palosaari 		}
691df8f1be1SAntti Palosaari 	}
692df8f1be1SAntti Palosaari 
6938229da50SAntti Palosaari 	if (fw->data[0] == 0x01)
6948229da50SAntti Palosaari 		ret = af9035_download_firmware_old(d, fw);
695df8f1be1SAntti Palosaari 	else
6968229da50SAntti Palosaari 		ret = af9035_download_firmware_new(d, fw);
697df8f1be1SAntti Palosaari 	if (ret < 0)
698df8f1be1SAntti Palosaari 		goto err;
699df8f1be1SAntti Palosaari 
700786baecfSMauro Carvalho Chehab 	/* firmware loaded, request boot */
701786baecfSMauro Carvalho Chehab 	req.cmd = CMD_FW_BOOT;
702786baecfSMauro Carvalho Chehab 	ret = af9035_ctrl_msg(d, &req);
703786baecfSMauro Carvalho Chehab 	if (ret < 0)
704786baecfSMauro Carvalho Chehab 		goto err;
705786baecfSMauro Carvalho Chehab 
706786baecfSMauro Carvalho Chehab 	/* ensure firmware starts */
707786baecfSMauro Carvalho Chehab 	wbuf[0] = 1;
708786baecfSMauro Carvalho Chehab 	ret = af9035_ctrl_msg(d, &req_fw_ver);
709786baecfSMauro Carvalho Chehab 	if (ret < 0)
710786baecfSMauro Carvalho Chehab 		goto err;
711786baecfSMauro Carvalho Chehab 
712786baecfSMauro Carvalho Chehab 	if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
713119f7a8cSAntti Palosaari 		dev_err(&d->udev->dev, "%s: firmware did not run\n",
714119f7a8cSAntti Palosaari 				KBUILD_MODNAME);
715786baecfSMauro Carvalho Chehab 		ret = -ENODEV;
716786baecfSMauro Carvalho Chehab 		goto err;
717786baecfSMauro Carvalho Chehab 	}
718786baecfSMauro Carvalho Chehab 
719119f7a8cSAntti Palosaari 	dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
720119f7a8cSAntti Palosaari 			KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
721786baecfSMauro Carvalho Chehab 
722786baecfSMauro Carvalho Chehab 	return 0;
723786baecfSMauro Carvalho Chehab 
724786baecfSMauro Carvalho Chehab err:
725119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
726786baecfSMauro Carvalho Chehab 
727786baecfSMauro Carvalho Chehab 	return ret;
728786baecfSMauro Carvalho Chehab }
729786baecfSMauro Carvalho Chehab 
7309ea3681dSAntti Palosaari static int af9035_read_config(struct dvb_usb_device *d)
731786baecfSMauro Carvalho Chehab {
732786baecfSMauro Carvalho Chehab 	struct state *state = d_to_priv(d);
7339ea3681dSAntti Palosaari 	int ret, i;
734786baecfSMauro Carvalho Chehab 	u8 tmp;
7359ea3681dSAntti Palosaari 	u16 tmp16, addr;
736786baecfSMauro Carvalho Chehab 
737bf97b637SAntti Palosaari 	/* demod I2C "address" */
738a781edd1SAntti Palosaari 	state->af9033_i2c_addr[0] = 0x38;
739a781edd1SAntti Palosaari 	state->af9033_i2c_addr[1] = 0x3a;
7400d94d6a0SAntti Palosaari 	state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
741df8f1be1SAntti Palosaari 	state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
742ab56ad6aSAntti Palosaari 	state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
743ab56ad6aSAntti Palosaari 	state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
744bf97b637SAntti Palosaari 
7459ea3681dSAntti Palosaari 	if (state->chip_type == 0x9135) {
74615ba8202SAntti Palosaari 		/* feed clock for integrated RF tuner */
74715ba8202SAntti Palosaari 		state->af9033_config[0].dyn0_clk = true;
74815ba8202SAntti Palosaari 		state->af9033_config[1].dyn0_clk = true;
74915ba8202SAntti Palosaari 
750bc3c9e10SAntti Palosaari 		if (state->chip_version == 0x02) {
751bc3c9e10SAntti Palosaari 			state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
752df8f1be1SAntti Palosaari 			state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
75315ba8202SAntti Palosaari 			tmp16 = 0x00461d; /* eeprom memory mapped location */
754bc3c9e10SAntti Palosaari 		} else {
755bc3c9e10SAntti Palosaari 			state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
756df8f1be1SAntti Palosaari 			state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
75715ba8202SAntti Palosaari 			tmp16 = 0x00461b; /* eeprom memory mapped location */
758bc3c9e10SAntti Palosaari 		}
759bc3c9e10SAntti Palosaari 
7609ea3681dSAntti Palosaari 		/* check if eeprom exists */
761bc3c9e10SAntti Palosaari 		ret = af9035_rd_reg(d, tmp16, &tmp);
7629ea3681dSAntti Palosaari 		if (ret < 0)
7639ea3681dSAntti Palosaari 			goto err;
7649ea3681dSAntti Palosaari 
765431a6d4aSAntti Palosaari 		if (tmp == 0x00) {
766bc3c9e10SAntti Palosaari 			dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
7679ea3681dSAntti Palosaari 			goto skip_eeprom;
7689ea3681dSAntti Palosaari 		}
7699ea3681dSAntti Palosaari 	}
7709ea3681dSAntti Palosaari 
771786baecfSMauro Carvalho Chehab 	/* check if there is dual tuners */
772d716ef46SAntti Palosaari 	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
773786baecfSMauro Carvalho Chehab 	if (ret < 0)
774786baecfSMauro Carvalho Chehab 		goto err;
775786baecfSMauro Carvalho Chehab 
776d716ef46SAntti Palosaari 	if (tmp == 1 || tmp == 3)
777d716ef46SAntti Palosaari 		state->dual_mode = true;
778d716ef46SAntti Palosaari 
779d716ef46SAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: ts mode=%d dual mode=%d\n", __func__,
780d716ef46SAntti Palosaari 			tmp, state->dual_mode);
781786baecfSMauro Carvalho Chehab 
7829805992fSJose Alberto Reguero 	if (state->dual_mode) {
7839805992fSJose Alberto Reguero 		/* read 2nd demodulator I2C address */
784431a6d4aSAntti Palosaari 		ret = af9035_rd_reg(d,
785431a6d4aSAntti Palosaari 				state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
786431a6d4aSAntti Palosaari 				&tmp);
7879805992fSJose Alberto Reguero 		if (ret < 0)
7889805992fSJose Alberto Reguero 			goto err;
789bf97b637SAntti Palosaari 
79014992f01SAntti Palosaari 		if (tmp)
791a781edd1SAntti Palosaari 			state->af9033_i2c_addr[1] = tmp;
79214992f01SAntti Palosaari 
793bf97b637SAntti Palosaari 		dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
794bf97b637SAntti Palosaari 				__func__, tmp);
7959805992fSJose Alberto Reguero 	}
7969805992fSJose Alberto Reguero 
797431a6d4aSAntti Palosaari 	addr = state->eeprom_addr;
798431a6d4aSAntti Palosaari 
799786baecfSMauro Carvalho Chehab 	for (i = 0; i < state->dual_mode + 1; i++) {
800786baecfSMauro Carvalho Chehab 		/* tuner */
8019ea3681dSAntti Palosaari 		ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
802786baecfSMauro Carvalho Chehab 		if (ret < 0)
803786baecfSMauro Carvalho Chehab 			goto err;
804786baecfSMauro Carvalho Chehab 
805bc3c9e10SAntti Palosaari 		dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
8061cbbf90dSAntti Palosaari 				__func__, i, tmp);
8071cbbf90dSAntti Palosaari 
8081cbbf90dSAntti Palosaari 		/* tuner sanity check */
8091cbbf90dSAntti Palosaari 		if (state->chip_type == 0x9135) {
8101cbbf90dSAntti Palosaari 			if (state->chip_version == 0x02) {
8111cbbf90dSAntti Palosaari 				/* IT9135 BX (v2) */
8121cbbf90dSAntti Palosaari 				switch (tmp) {
8131cbbf90dSAntti Palosaari 				case AF9033_TUNER_IT9135_60:
8141cbbf90dSAntti Palosaari 				case AF9033_TUNER_IT9135_61:
8151cbbf90dSAntti Palosaari 				case AF9033_TUNER_IT9135_62:
8161cbbf90dSAntti Palosaari 					state->af9033_config[i].tuner = tmp;
8171cbbf90dSAntti Palosaari 					break;
8181cbbf90dSAntti Palosaari 				}
8191cbbf90dSAntti Palosaari 			} else {
8201cbbf90dSAntti Palosaari 				/* IT9135 AX (v1) */
8211cbbf90dSAntti Palosaari 				switch (tmp) {
8221cbbf90dSAntti Palosaari 				case AF9033_TUNER_IT9135_38:
8231cbbf90dSAntti Palosaari 				case AF9033_TUNER_IT9135_51:
8241cbbf90dSAntti Palosaari 				case AF9033_TUNER_IT9135_52:
8251cbbf90dSAntti Palosaari 					state->af9033_config[i].tuner = tmp;
8261cbbf90dSAntti Palosaari 					break;
8271cbbf90dSAntti Palosaari 				}
8281cbbf90dSAntti Palosaari 			}
8291cbbf90dSAntti Palosaari 		} else {
8301cbbf90dSAntti Palosaari 			/* AF9035 */
8311cbbf90dSAntti Palosaari 			state->af9033_config[i].tuner = tmp;
8321cbbf90dSAntti Palosaari 		}
8331cbbf90dSAntti Palosaari 
8341cbbf90dSAntti Palosaari 		if (state->af9033_config[i].tuner != tmp) {
8351cbbf90dSAntti Palosaari 			dev_info(&d->udev->dev,
8361cbbf90dSAntti Palosaari 					"%s: [%d] overriding tuner from %02x to %02x\n",
8371cbbf90dSAntti Palosaari 					KBUILD_MODNAME, i, tmp,
8381cbbf90dSAntti Palosaari 					state->af9033_config[i].tuner);
8391cbbf90dSAntti Palosaari 		}
8409ea3681dSAntti Palosaari 
8419ea3681dSAntti Palosaari 		switch (state->af9033_config[i].tuner) {
842786baecfSMauro Carvalho Chehab 		case AF9033_TUNER_TUA9001:
843786baecfSMauro Carvalho Chehab 		case AF9033_TUNER_FC0011:
844786baecfSMauro Carvalho Chehab 		case AF9033_TUNER_MXL5007T:
845786baecfSMauro Carvalho Chehab 		case AF9033_TUNER_TDA18218:
846d67ceb33SOliver Schinagl 		case AF9033_TUNER_FC2580:
8477e0bc296SAntti Palosaari 		case AF9033_TUNER_FC0012:
848786baecfSMauro Carvalho Chehab 			state->af9033_config[i].spec_inv = 1;
849786baecfSMauro Carvalho Chehab 			break;
8509ea3681dSAntti Palosaari 		case AF9033_TUNER_IT9135_38:
8519ea3681dSAntti Palosaari 		case AF9033_TUNER_IT9135_51:
8529ea3681dSAntti Palosaari 		case AF9033_TUNER_IT9135_52:
8539ea3681dSAntti Palosaari 		case AF9033_TUNER_IT9135_60:
8549ea3681dSAntti Palosaari 		case AF9033_TUNER_IT9135_61:
8559ea3681dSAntti Palosaari 		case AF9033_TUNER_IT9135_62:
8569ea3681dSAntti Palosaari 			break;
857786baecfSMauro Carvalho Chehab 		default:
8589ea3681dSAntti Palosaari 			dev_warn(&d->udev->dev,
8599ea3681dSAntti Palosaari 					"%s: tuner id=%02x not supported, please report!",
860119f7a8cSAntti Palosaari 					KBUILD_MODNAME, tmp);
861c2c1b415SPeter Senna Tschudin 		}
862786baecfSMauro Carvalho Chehab 
863bf97b637SAntti Palosaari 		/* disable dual mode if driver does not support it */
864bf97b637SAntti Palosaari 		if (i == 1)
865df8f1be1SAntti Palosaari 			switch (state->af9033_config[i].tuner) {
8660bb3d8acSAntti Palosaari 			case AF9033_TUNER_FC0012:
867df8f1be1SAntti Palosaari 			case AF9033_TUNER_IT9135_38:
868df8f1be1SAntti Palosaari 			case AF9033_TUNER_IT9135_51:
869df8f1be1SAntti Palosaari 			case AF9033_TUNER_IT9135_52:
870df8f1be1SAntti Palosaari 			case AF9033_TUNER_IT9135_60:
871df8f1be1SAntti Palosaari 			case AF9033_TUNER_IT9135_61:
872df8f1be1SAntti Palosaari 			case AF9033_TUNER_IT9135_62:
87378c7bc4bSJose Alberto Reguero 			case AF9033_TUNER_MXL5007T:
8740bb3d8acSAntti Palosaari 				break;
875bf97b637SAntti Palosaari 			default:
876bf97b637SAntti Palosaari 				state->dual_mode = false;
8779ea3681dSAntti Palosaari 				dev_info(&d->udev->dev,
8789ea3681dSAntti Palosaari 						"%s: driver does not support 2nd tuner and will disable it",
8799ea3681dSAntti Palosaari 						KBUILD_MODNAME);
880bf97b637SAntti Palosaari 		}
881bf97b637SAntti Palosaari 
882786baecfSMauro Carvalho Chehab 		/* tuner IF frequency */
8839ea3681dSAntti Palosaari 		ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
884786baecfSMauro Carvalho Chehab 		if (ret < 0)
885786baecfSMauro Carvalho Chehab 			goto err;
886786baecfSMauro Carvalho Chehab 
887786baecfSMauro Carvalho Chehab 		tmp16 = tmp;
888786baecfSMauro Carvalho Chehab 
8899ea3681dSAntti Palosaari 		ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
890786baecfSMauro Carvalho Chehab 		if (ret < 0)
891786baecfSMauro Carvalho Chehab 			goto err;
892786baecfSMauro Carvalho Chehab 
893786baecfSMauro Carvalho Chehab 		tmp16 |= tmp << 8;
894786baecfSMauro Carvalho Chehab 
895119f7a8cSAntti Palosaari 		dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
896786baecfSMauro Carvalho Chehab 
8979ea3681dSAntti Palosaari 		addr += 0x10; /* shift for the 2nd tuner params */
898786baecfSMauro Carvalho Chehab 	}
899786baecfSMauro Carvalho Chehab 
9009ea3681dSAntti Palosaari skip_eeprom:
901786baecfSMauro Carvalho Chehab 	/* get demod clock */
902786baecfSMauro Carvalho Chehab 	ret = af9035_rd_reg(d, 0x00d800, &tmp);
903786baecfSMauro Carvalho Chehab 	if (ret < 0)
904786baecfSMauro Carvalho Chehab 		goto err;
905786baecfSMauro Carvalho Chehab 
906786baecfSMauro Carvalho Chehab 	tmp = (tmp >> 0) & 0x0f;
907786baecfSMauro Carvalho Chehab 
9089ea3681dSAntti Palosaari 	for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
90974c1883aSAntti Palosaari 		if (state->chip_type == 0x9135)
9109ea3681dSAntti Palosaari 			state->af9033_config[i].clock = clock_lut_it9135[tmp];
91174c1883aSAntti Palosaari 		else
9129ea3681dSAntti Palosaari 			state->af9033_config[i].clock = clock_lut_af9035[tmp];
9139ea3681dSAntti Palosaari 	}
9149ea3681dSAntti Palosaari 
9159ea3681dSAntti Palosaari 	return 0;
9169ea3681dSAntti Palosaari 
9179ea3681dSAntti Palosaari err:
9189ea3681dSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
9199ea3681dSAntti Palosaari 
9209ea3681dSAntti Palosaari 	return ret;
92174c1883aSAntti Palosaari }
92274c1883aSAntti Palosaari 
92351639be3SAntti Palosaari static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
92451639be3SAntti Palosaari 		int cmd, int arg)
92551639be3SAntti Palosaari {
92651639be3SAntti Palosaari 	int ret;
92751639be3SAntti Palosaari 	u8 val;
92851639be3SAntti Palosaari 
92951639be3SAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
93051639be3SAntti Palosaari 
93151639be3SAntti Palosaari 	/*
93251639be3SAntti Palosaari 	 * CEN     always enabled by hardware wiring
93351639be3SAntti Palosaari 	 * RESETN  GPIOT3
93451639be3SAntti Palosaari 	 * RXEN    GPIOT2
93551639be3SAntti Palosaari 	 */
93651639be3SAntti Palosaari 
93751639be3SAntti Palosaari 	switch (cmd) {
93851639be3SAntti Palosaari 	case TUA9001_CMD_RESETN:
93951639be3SAntti Palosaari 		if (arg)
94051639be3SAntti Palosaari 			val = 0x00;
94151639be3SAntti Palosaari 		else
94251639be3SAntti Palosaari 			val = 0x01;
94351639be3SAntti Palosaari 
94451639be3SAntti Palosaari 		ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
94551639be3SAntti Palosaari 		if (ret < 0)
94651639be3SAntti Palosaari 			goto err;
94751639be3SAntti Palosaari 		break;
94851639be3SAntti Palosaari 	case TUA9001_CMD_RXEN:
94951639be3SAntti Palosaari 		if (arg)
95051639be3SAntti Palosaari 			val = 0x01;
95151639be3SAntti Palosaari 		else
95251639be3SAntti Palosaari 			val = 0x00;
95351639be3SAntti Palosaari 
95451639be3SAntti Palosaari 		ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
95551639be3SAntti Palosaari 		if (ret < 0)
95651639be3SAntti Palosaari 			goto err;
95751639be3SAntti Palosaari 		break;
95851639be3SAntti Palosaari 	}
95951639be3SAntti Palosaari 
96051639be3SAntti Palosaari 	return 0;
96151639be3SAntti Palosaari 
96251639be3SAntti Palosaari err:
96351639be3SAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
96451639be3SAntti Palosaari 
96551639be3SAntti Palosaari 	return ret;
96651639be3SAntti Palosaari }
96751639be3SAntti Palosaari 
96851639be3SAntti Palosaari 
969786baecfSMauro Carvalho Chehab static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
970786baecfSMauro Carvalho Chehab 		int cmd, int arg)
971786baecfSMauro Carvalho Chehab {
972786baecfSMauro Carvalho Chehab 	int ret;
973786baecfSMauro Carvalho Chehab 
974786baecfSMauro Carvalho Chehab 	switch (cmd) {
975786baecfSMauro Carvalho Chehab 	case FC0011_FE_CALLBACK_POWER:
976786baecfSMauro Carvalho Chehab 		/* Tuner enable */
977786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
978786baecfSMauro Carvalho Chehab 		if (ret < 0)
979786baecfSMauro Carvalho Chehab 			goto err;
980786baecfSMauro Carvalho Chehab 
981786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
982786baecfSMauro Carvalho Chehab 		if (ret < 0)
983786baecfSMauro Carvalho Chehab 			goto err;
984786baecfSMauro Carvalho Chehab 
985786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
986786baecfSMauro Carvalho Chehab 		if (ret < 0)
987786baecfSMauro Carvalho Chehab 			goto err;
988786baecfSMauro Carvalho Chehab 
989786baecfSMauro Carvalho Chehab 		/* LED */
990786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
991786baecfSMauro Carvalho Chehab 		if (ret < 0)
992786baecfSMauro Carvalho Chehab 			goto err;
993786baecfSMauro Carvalho Chehab 
994786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
995786baecfSMauro Carvalho Chehab 		if (ret < 0)
996786baecfSMauro Carvalho Chehab 			goto err;
997786baecfSMauro Carvalho Chehab 
998786baecfSMauro Carvalho Chehab 		usleep_range(10000, 50000);
999786baecfSMauro Carvalho Chehab 		break;
1000786baecfSMauro Carvalho Chehab 	case FC0011_FE_CALLBACK_RESET:
1001786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg(d, 0xd8e9, 1);
1002786baecfSMauro Carvalho Chehab 		if (ret < 0)
1003786baecfSMauro Carvalho Chehab 			goto err;
1004786baecfSMauro Carvalho Chehab 
1005786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg(d, 0xd8e8, 1);
1006786baecfSMauro Carvalho Chehab 		if (ret < 0)
1007786baecfSMauro Carvalho Chehab 			goto err;
1008786baecfSMauro Carvalho Chehab 
1009786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg(d, 0xd8e7, 1);
1010786baecfSMauro Carvalho Chehab 		if (ret < 0)
1011786baecfSMauro Carvalho Chehab 			goto err;
1012786baecfSMauro Carvalho Chehab 
1013786baecfSMauro Carvalho Chehab 		usleep_range(10000, 20000);
1014786baecfSMauro Carvalho Chehab 
1015786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg(d, 0xd8e7, 0);
1016786baecfSMauro Carvalho Chehab 		if (ret < 0)
1017786baecfSMauro Carvalho Chehab 			goto err;
1018786baecfSMauro Carvalho Chehab 
1019786baecfSMauro Carvalho Chehab 		usleep_range(10000, 20000);
1020786baecfSMauro Carvalho Chehab 		break;
1021786baecfSMauro Carvalho Chehab 	default:
1022786baecfSMauro Carvalho Chehab 		ret = -EINVAL;
1023786baecfSMauro Carvalho Chehab 		goto err;
1024786baecfSMauro Carvalho Chehab 	}
1025786baecfSMauro Carvalho Chehab 
1026786baecfSMauro Carvalho Chehab 	return 0;
1027786baecfSMauro Carvalho Chehab 
1028786baecfSMauro Carvalho Chehab err:
1029119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1030786baecfSMauro Carvalho Chehab 
1031786baecfSMauro Carvalho Chehab 	return ret;
1032786baecfSMauro Carvalho Chehab }
1033786baecfSMauro Carvalho Chehab 
1034786baecfSMauro Carvalho Chehab static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
1035786baecfSMauro Carvalho Chehab {
1036786baecfSMauro Carvalho Chehab 	struct state *state = d_to_priv(d);
1037786baecfSMauro Carvalho Chehab 
1038786baecfSMauro Carvalho Chehab 	switch (state->af9033_config[0].tuner) {
1039786baecfSMauro Carvalho Chehab 	case AF9033_TUNER_FC0011:
1040786baecfSMauro Carvalho Chehab 		return af9035_fc0011_tuner_callback(d, cmd, arg);
104151639be3SAntti Palosaari 	case AF9033_TUNER_TUA9001:
104251639be3SAntti Palosaari 		return af9035_tua9001_tuner_callback(d, cmd, arg);
1043786baecfSMauro Carvalho Chehab 	default:
1044786baecfSMauro Carvalho Chehab 		break;
1045786baecfSMauro Carvalho Chehab 	}
1046786baecfSMauro Carvalho Chehab 
10471835af10SAntti Palosaari 	return 0;
1048786baecfSMauro Carvalho Chehab }
1049786baecfSMauro Carvalho Chehab 
1050786baecfSMauro Carvalho Chehab static int af9035_frontend_callback(void *adapter_priv, int component,
1051786baecfSMauro Carvalho Chehab 				    int cmd, int arg)
1052786baecfSMauro Carvalho Chehab {
1053786baecfSMauro Carvalho Chehab 	struct i2c_adapter *adap = adapter_priv;
1054786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
1055786baecfSMauro Carvalho Chehab 
10561835af10SAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
10571835af10SAntti Palosaari 			__func__, component, cmd, arg);
10581835af10SAntti Palosaari 
1059786baecfSMauro Carvalho Chehab 	switch (component) {
1060786baecfSMauro Carvalho Chehab 	case DVB_FRONTEND_COMPONENT_TUNER:
1061786baecfSMauro Carvalho Chehab 		return af9035_tuner_callback(d, cmd, arg);
1062786baecfSMauro Carvalho Chehab 	default:
1063786baecfSMauro Carvalho Chehab 		break;
1064786baecfSMauro Carvalho Chehab 	}
1065786baecfSMauro Carvalho Chehab 
10661835af10SAntti Palosaari 	return 0;
1067786baecfSMauro Carvalho Chehab }
1068786baecfSMauro Carvalho Chehab 
10699805992fSJose Alberto Reguero static int af9035_get_adapter_count(struct dvb_usb_device *d)
10709805992fSJose Alberto Reguero {
10719805992fSJose Alberto Reguero 	struct state *state = d_to_priv(d);
1072c10989a2SAntti Palosaari 
10739805992fSJose Alberto Reguero 	return state->dual_mode + 1;
10749805992fSJose Alberto Reguero }
10759805992fSJose Alberto Reguero 
1076786baecfSMauro Carvalho Chehab static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
1077786baecfSMauro Carvalho Chehab {
1078786baecfSMauro Carvalho Chehab 	struct state *state = adap_to_priv(adap);
1079786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = adap_to_d(adap);
1080786baecfSMauro Carvalho Chehab 	int ret;
1081c10989a2SAntti Palosaari 
1082d4d6a34dSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1083786baecfSMauro Carvalho Chehab 
1084786baecfSMauro Carvalho Chehab 	if (!state->af9033_config[adap->id].tuner) {
1085786baecfSMauro Carvalho Chehab 		/* unsupported tuner */
1086786baecfSMauro Carvalho Chehab 		ret = -ENODEV;
1087786baecfSMauro Carvalho Chehab 		goto err;
1088786baecfSMauro Carvalho Chehab 	}
1089786baecfSMauro Carvalho Chehab 
1090f5b00a76SAntti Palosaari 	state->af9033_config[adap->id].fe = &adap->fe[0];
1091f5b00a76SAntti Palosaari 	state->af9033_config[adap->id].ops = &state->ops;
1092a781edd1SAntti Palosaari 	ret = af9035_add_i2c_dev(d, "af9033", state->af9033_i2c_addr[adap->id],
10935dcf5bf6SOlli Salonen 			&state->af9033_config[adap->id], &d->i2c_adap);
1094f5b00a76SAntti Palosaari 	if (ret)
1095f5b00a76SAntti Palosaari 		goto err;
1096f5b00a76SAntti Palosaari 
1097786baecfSMauro Carvalho Chehab 	if (adap->fe[0] == NULL) {
1098786baecfSMauro Carvalho Chehab 		ret = -ENODEV;
1099786baecfSMauro Carvalho Chehab 		goto err;
1100786baecfSMauro Carvalho Chehab 	}
1101786baecfSMauro Carvalho Chehab 
1102786baecfSMauro Carvalho Chehab 	/* disable I2C-gate */
1103786baecfSMauro Carvalho Chehab 	adap->fe[0]->ops.i2c_gate_ctrl = NULL;
1104786baecfSMauro Carvalho Chehab 	adap->fe[0]->callback = af9035_frontend_callback;
1105786baecfSMauro Carvalho Chehab 
1106786baecfSMauro Carvalho Chehab 	return 0;
1107786baecfSMauro Carvalho Chehab 
1108786baecfSMauro Carvalho Chehab err:
1109119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1110786baecfSMauro Carvalho Chehab 
1111786baecfSMauro Carvalho Chehab 	return ret;
1112786baecfSMauro Carvalho Chehab }
1113786baecfSMauro Carvalho Chehab 
1114d4d6a34dSAntti Palosaari static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
1115d4d6a34dSAntti Palosaari {
1116d4d6a34dSAntti Palosaari 	struct state *state = adap_to_priv(adap);
1117d4d6a34dSAntti Palosaari 	struct dvb_usb_device *d = adap_to_d(adap);
1118d4d6a34dSAntti Palosaari 	int demod2;
1119d4d6a34dSAntti Palosaari 
1120d4d6a34dSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1121d4d6a34dSAntti Palosaari 
1122d4d6a34dSAntti Palosaari 	/*
1123d4d6a34dSAntti Palosaari 	 * For dual tuner devices we have to resolve 2nd demod client, as there
1124d4d6a34dSAntti Palosaari 	 * is two different kind of tuner drivers; one is using I2C binding
1125d4d6a34dSAntti Palosaari 	 * and the other is using DVB attach/detach binding.
1126d4d6a34dSAntti Palosaari 	 */
1127d4d6a34dSAntti Palosaari 	switch (state->af9033_config[adap->id].tuner) {
1128d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_38:
1129d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_51:
1130d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_52:
1131d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_60:
1132d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_61:
1133d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_62:
1134d4d6a34dSAntti Palosaari 		demod2 = 2;
1135d4d6a34dSAntti Palosaari 		break;
1136d4d6a34dSAntti Palosaari 	default:
1137d4d6a34dSAntti Palosaari 		demod2 = 1;
1138d4d6a34dSAntti Palosaari 	}
1139d4d6a34dSAntti Palosaari 
1140d4d6a34dSAntti Palosaari 	if (adap->id == 1) {
1141d4d6a34dSAntti Palosaari 		if (state->i2c_client[demod2])
1142d4d6a34dSAntti Palosaari 			af9035_del_i2c_dev(d);
1143d4d6a34dSAntti Palosaari 	} else if (adap->id == 0) {
1144d4d6a34dSAntti Palosaari 		if (state->i2c_client[0])
1145d4d6a34dSAntti Palosaari 			af9035_del_i2c_dev(d);
1146d4d6a34dSAntti Palosaari 	}
1147d4d6a34dSAntti Palosaari 
1148d4d6a34dSAntti Palosaari 	return 0;
1149d4d6a34dSAntti Palosaari }
1150d4d6a34dSAntti Palosaari 
1151786baecfSMauro Carvalho Chehab static struct tua9001_config af9035_tua9001_config = {
1152786baecfSMauro Carvalho Chehab 	.i2c_addr = 0x60,
1153786baecfSMauro Carvalho Chehab };
1154786baecfSMauro Carvalho Chehab 
1155786baecfSMauro Carvalho Chehab static const struct fc0011_config af9035_fc0011_config = {
1156786baecfSMauro Carvalho Chehab 	.i2c_address = 0x60,
1157786baecfSMauro Carvalho Chehab };
1158786baecfSMauro Carvalho Chehab 
11599805992fSJose Alberto Reguero static struct mxl5007t_config af9035_mxl5007t_config[] = {
11609805992fSJose Alberto Reguero 	{
1161786baecfSMauro Carvalho Chehab 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
1162786baecfSMauro Carvalho Chehab 		.if_freq_hz = MxL_IF_4_57_MHZ,
1163786baecfSMauro Carvalho Chehab 		.invert_if = 0,
1164786baecfSMauro Carvalho Chehab 		.loop_thru_enable = 0,
1165786baecfSMauro Carvalho Chehab 		.clk_out_enable = 0,
1166786baecfSMauro Carvalho Chehab 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
11679805992fSJose Alberto Reguero 	}, {
11689805992fSJose Alberto Reguero 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
11699805992fSJose Alberto Reguero 		.if_freq_hz = MxL_IF_4_57_MHZ,
11709805992fSJose Alberto Reguero 		.invert_if = 0,
11719805992fSJose Alberto Reguero 		.loop_thru_enable = 1,
11729805992fSJose Alberto Reguero 		.clk_out_enable = 1,
11739805992fSJose Alberto Reguero 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
11749805992fSJose Alberto Reguero 	}
1175786baecfSMauro Carvalho Chehab };
1176786baecfSMauro Carvalho Chehab 
1177786baecfSMauro Carvalho Chehab static struct tda18218_config af9035_tda18218_config = {
1178786baecfSMauro Carvalho Chehab 	.i2c_address = 0x60,
1179786baecfSMauro Carvalho Chehab 	.i2c_wr_max = 21,
1180786baecfSMauro Carvalho Chehab };
1181786baecfSMauro Carvalho Chehab 
1182d67ceb33SOliver Schinagl static const struct fc2580_config af9035_fc2580_config = {
1183d67ceb33SOliver Schinagl 	.i2c_addr = 0x56,
1184d67ceb33SOliver Schinagl 	.clock = 16384000,
1185d67ceb33SOliver Schinagl };
1186d67ceb33SOliver Schinagl 
11870bb3d8acSAntti Palosaari static const struct fc0012_config af9035_fc0012_config[] = {
11880bb3d8acSAntti Palosaari 	{
1189ad3a758bSAntti Palosaari 		.i2c_address = 0x63,
1190ad3a758bSAntti Palosaari 		.xtal_freq = FC_XTAL_36_MHZ,
11913a984772SAntti Palosaari 		.dual_master = true,
11920bb3d8acSAntti Palosaari 		.loop_through = true,
11930bb3d8acSAntti Palosaari 		.clock_out = true,
11940bb3d8acSAntti Palosaari 	}, {
11950bb3d8acSAntti Palosaari 		.i2c_address = 0x63 | 0x80, /* I2C bus select hack */
11960bb3d8acSAntti Palosaari 		.xtal_freq = FC_XTAL_36_MHZ,
11973a984772SAntti Palosaari 		.dual_master = true,
11980bb3d8acSAntti Palosaari 	}
1199ad3a758bSAntti Palosaari };
1200ad3a758bSAntti Palosaari 
1201786baecfSMauro Carvalho Chehab static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1202786baecfSMauro Carvalho Chehab {
1203786baecfSMauro Carvalho Chehab 	struct state *state = adap_to_priv(adap);
1204786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = adap_to_d(adap);
1205786baecfSMauro Carvalho Chehab 	int ret;
1206786baecfSMauro Carvalho Chehab 	struct dvb_frontend *fe;
12070bb3d8acSAntti Palosaari 	struct i2c_msg msg[1];
1208bf97b637SAntti Palosaari 	u8 tuner_addr;
1209c10989a2SAntti Palosaari 
1210d4d6a34dSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1211df8f1be1SAntti Palosaari 
1212bf97b637SAntti Palosaari 	/*
1213bf97b637SAntti Palosaari 	 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1214bf97b637SAntti Palosaari 	 * to carry info about used I2C bus for dual tuner configuration.
1215bf97b637SAntti Palosaari 	 */
1216786baecfSMauro Carvalho Chehab 
1217786baecfSMauro Carvalho Chehab 	switch (state->af9033_config[adap->id].tuner) {
1218786baecfSMauro Carvalho Chehab 	case AF9033_TUNER_TUA9001:
1219786baecfSMauro Carvalho Chehab 		/* AF9035 gpiot3 = TUA9001 RESETN
1220786baecfSMauro Carvalho Chehab 		   AF9035 gpiot2 = TUA9001 RXEN */
1221786baecfSMauro Carvalho Chehab 
1222786baecfSMauro Carvalho Chehab 		/* configure gpiot2 and gpiot2 as output */
1223786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1224786baecfSMauro Carvalho Chehab 		if (ret < 0)
1225786baecfSMauro Carvalho Chehab 			goto err;
1226786baecfSMauro Carvalho Chehab 
1227786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1228786baecfSMauro Carvalho Chehab 		if (ret < 0)
1229786baecfSMauro Carvalho Chehab 			goto err;
1230786baecfSMauro Carvalho Chehab 
1231786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1232786baecfSMauro Carvalho Chehab 		if (ret < 0)
1233786baecfSMauro Carvalho Chehab 			goto err;
1234786baecfSMauro Carvalho Chehab 
1235786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1236786baecfSMauro Carvalho Chehab 		if (ret < 0)
1237786baecfSMauro Carvalho Chehab 			goto err;
1238786baecfSMauro Carvalho Chehab 
1239786baecfSMauro Carvalho Chehab 		/* attach tuner */
1240786baecfSMauro Carvalho Chehab 		fe = dvb_attach(tua9001_attach, adap->fe[0],
1241786baecfSMauro Carvalho Chehab 				&d->i2c_adap, &af9035_tua9001_config);
1242786baecfSMauro Carvalho Chehab 		break;
1243786baecfSMauro Carvalho Chehab 	case AF9033_TUNER_FC0011:
1244786baecfSMauro Carvalho Chehab 		fe = dvb_attach(fc0011_attach, adap->fe[0],
1245786baecfSMauro Carvalho Chehab 				&d->i2c_adap, &af9035_fc0011_config);
1246786baecfSMauro Carvalho Chehab 		break;
1247786baecfSMauro Carvalho Chehab 	case AF9033_TUNER_MXL5007T:
12489805992fSJose Alberto Reguero 		if (adap->id == 0) {
1249786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8e0, 1);
1250786baecfSMauro Carvalho Chehab 			if (ret < 0)
1251786baecfSMauro Carvalho Chehab 				goto err;
1252bf97b637SAntti Palosaari 
1253786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8e1, 1);
1254786baecfSMauro Carvalho Chehab 			if (ret < 0)
1255786baecfSMauro Carvalho Chehab 				goto err;
1256bf97b637SAntti Palosaari 
1257786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8df, 0);
1258786baecfSMauro Carvalho Chehab 			if (ret < 0)
1259786baecfSMauro Carvalho Chehab 				goto err;
1260786baecfSMauro Carvalho Chehab 
1261786baecfSMauro Carvalho Chehab 			msleep(30);
1262786baecfSMauro Carvalho Chehab 
1263786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8df, 1);
1264786baecfSMauro Carvalho Chehab 			if (ret < 0)
1265786baecfSMauro Carvalho Chehab 				goto err;
1266786baecfSMauro Carvalho Chehab 
1267786baecfSMauro Carvalho Chehab 			msleep(300);
1268786baecfSMauro Carvalho Chehab 
1269786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8c0, 1);
1270786baecfSMauro Carvalho Chehab 			if (ret < 0)
1271786baecfSMauro Carvalho Chehab 				goto err;
1272bf97b637SAntti Palosaari 
1273786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8c1, 1);
1274786baecfSMauro Carvalho Chehab 			if (ret < 0)
1275786baecfSMauro Carvalho Chehab 				goto err;
1276bf97b637SAntti Palosaari 
1277786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8bf, 0);
1278786baecfSMauro Carvalho Chehab 			if (ret < 0)
1279786baecfSMauro Carvalho Chehab 				goto err;
1280bf97b637SAntti Palosaari 
1281786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8b4, 1);
1282786baecfSMauro Carvalho Chehab 			if (ret < 0)
1283786baecfSMauro Carvalho Chehab 				goto err;
1284bf97b637SAntti Palosaari 
1285786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8b5, 1);
1286786baecfSMauro Carvalho Chehab 			if (ret < 0)
1287786baecfSMauro Carvalho Chehab 				goto err;
1288bf97b637SAntti Palosaari 
1289786baecfSMauro Carvalho Chehab 			ret = af9035_wr_reg(d, 0x00d8b3, 1);
1290786baecfSMauro Carvalho Chehab 			if (ret < 0)
1291786baecfSMauro Carvalho Chehab 				goto err;
1292bf97b637SAntti Palosaari 
1293bf97b637SAntti Palosaari 			tuner_addr = 0x60;
1294bf97b637SAntti Palosaari 		} else {
1295bf97b637SAntti Palosaari 			tuner_addr = 0x60 | 0x80; /* I2C bus hack */
12969805992fSJose Alberto Reguero 		}
1297786baecfSMauro Carvalho Chehab 
1298786baecfSMauro Carvalho Chehab 		/* attach tuner */
1299bf97b637SAntti Palosaari 		fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1300bf97b637SAntti Palosaari 				tuner_addr, &af9035_mxl5007t_config[adap->id]);
1301786baecfSMauro Carvalho Chehab 		break;
1302786baecfSMauro Carvalho Chehab 	case AF9033_TUNER_TDA18218:
1303786baecfSMauro Carvalho Chehab 		/* attach tuner */
1304786baecfSMauro Carvalho Chehab 		fe = dvb_attach(tda18218_attach, adap->fe[0],
1305786baecfSMauro Carvalho Chehab 				&d->i2c_adap, &af9035_tda18218_config);
1306786baecfSMauro Carvalho Chehab 		break;
1307d67ceb33SOliver Schinagl 	case AF9033_TUNER_FC2580:
1308d67ceb33SOliver Schinagl 		/* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
1309d67ceb33SOliver Schinagl 		ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1310d67ceb33SOliver Schinagl 		if (ret < 0)
1311d67ceb33SOliver Schinagl 			goto err;
1312d67ceb33SOliver Schinagl 
1313d67ceb33SOliver Schinagl 		ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1314d67ceb33SOliver Schinagl 		if (ret < 0)
1315d67ceb33SOliver Schinagl 			goto err;
1316d67ceb33SOliver Schinagl 
1317d67ceb33SOliver Schinagl 		ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1318d67ceb33SOliver Schinagl 		if (ret < 0)
1319d67ceb33SOliver Schinagl 			goto err;
1320d67ceb33SOliver Schinagl 
1321d67ceb33SOliver Schinagl 		usleep_range(10000, 50000);
1322d67ceb33SOliver Schinagl 		/* attach tuner */
1323d67ceb33SOliver Schinagl 		fe = dvb_attach(fc2580_attach, adap->fe[0],
1324d67ceb33SOliver Schinagl 				&d->i2c_adap, &af9035_fc2580_config);
1325d67ceb33SOliver Schinagl 		break;
13267e0bc296SAntti Palosaari 	case AF9033_TUNER_FC0012:
13277e0bc296SAntti Palosaari 		/*
13287e0bc296SAntti Palosaari 		 * AF9035 gpiot2 = FC0012 enable
13297e0bc296SAntti Palosaari 		 * XXX: there seems to be something on gpioh8 too, but on my
13307e0bc296SAntti Palosaari 		 * my test I didn't find any difference.
13317e0bc296SAntti Palosaari 		 */
13327e0bc296SAntti Palosaari 
13330bb3d8acSAntti Palosaari 		if (adap->id == 0) {
13347e0bc296SAntti Palosaari 			/* configure gpiot2 as output and high */
13357e0bc296SAntti Palosaari 			ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
13367e0bc296SAntti Palosaari 			if (ret < 0)
13377e0bc296SAntti Palosaari 				goto err;
13387e0bc296SAntti Palosaari 
13397e0bc296SAntti Palosaari 			ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
13407e0bc296SAntti Palosaari 			if (ret < 0)
13417e0bc296SAntti Palosaari 				goto err;
13427e0bc296SAntti Palosaari 
13437e0bc296SAntti Palosaari 			ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
13447e0bc296SAntti Palosaari 			if (ret < 0)
13457e0bc296SAntti Palosaari 				goto err;
13460bb3d8acSAntti Palosaari 		} else {
13470bb3d8acSAntti Palosaari 			/*
13480bb3d8acSAntti Palosaari 			 * FIXME: That belongs for the FC0012 driver.
13490bb3d8acSAntti Palosaari 			 * Write 02 to FC0012 master tuner register 0d directly
13500bb3d8acSAntti Palosaari 			 * in order to make slave tuner working.
13510bb3d8acSAntti Palosaari 			 */
13520bb3d8acSAntti Palosaari 			msg[0].addr = 0x63;
13530bb3d8acSAntti Palosaari 			msg[0].flags = 0;
13540bb3d8acSAntti Palosaari 			msg[0].len = 2;
13550bb3d8acSAntti Palosaari 			msg[0].buf = "\x0d\x02";
13560bb3d8acSAntti Palosaari 			ret = i2c_transfer(&d->i2c_adap, msg, 1);
13570bb3d8acSAntti Palosaari 			if (ret < 0)
13580bb3d8acSAntti Palosaari 				goto err;
13590bb3d8acSAntti Palosaari 		}
13607e0bc296SAntti Palosaari 
13617e0bc296SAntti Palosaari 		usleep_range(10000, 50000);
13627e0bc296SAntti Palosaari 
1363ad3a758bSAntti Palosaari 		fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
13640bb3d8acSAntti Palosaari 				&af9035_fc0012_config[adap->id]);
13657e0bc296SAntti Palosaari 		break;
1366ac77fb0fSAntti Palosaari 	case AF9033_TUNER_IT9135_38:
136774c1883aSAntti Palosaari 	case AF9033_TUNER_IT9135_51:
136874c1883aSAntti Palosaari 	case AF9033_TUNER_IT9135_52:
13693b2a5e8cSAntti Palosaari 	{
13703b2a5e8cSAntti Palosaari 		struct it913x_config it913x_config = {
13713b2a5e8cSAntti Palosaari 			.fe = adap->fe[0],
13723b2a5e8cSAntti Palosaari 			.chip_ver = 1,
13733b2a5e8cSAntti Palosaari 		};
13743b2a5e8cSAntti Palosaari 
1375676c350fSAntti Palosaari 		if (state->dual_mode) {
1376676c350fSAntti Palosaari 			if (adap->id == 0)
1377676c350fSAntti Palosaari 				it913x_config.role = IT913X_ROLE_DUAL_MASTER;
1378676c350fSAntti Palosaari 			else
1379676c350fSAntti Palosaari 				it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
1380676c350fSAntti Palosaari 		}
1381676c350fSAntti Palosaari 
13823b2a5e8cSAntti Palosaari 		ret = af9035_add_i2c_dev(d, "it913x",
1383a781edd1SAntti Palosaari 				state->af9033_i2c_addr[adap->id] >> 1,
13845dcf5bf6SOlli Salonen 				&it913x_config, &d->i2c_adap);
13853b2a5e8cSAntti Palosaari 		if (ret)
13863b2a5e8cSAntti Palosaari 			goto err;
13873b2a5e8cSAntti Palosaari 
13883b2a5e8cSAntti Palosaari 		fe = adap->fe[0];
13893b2a5e8cSAntti Palosaari 		break;
13903b2a5e8cSAntti Palosaari 	}
139174c1883aSAntti Palosaari 	case AF9033_TUNER_IT9135_60:
139274c1883aSAntti Palosaari 	case AF9033_TUNER_IT9135_61:
139374c1883aSAntti Palosaari 	case AF9033_TUNER_IT9135_62:
13943b2a5e8cSAntti Palosaari 	{
13953b2a5e8cSAntti Palosaari 		struct it913x_config it913x_config = {
13963b2a5e8cSAntti Palosaari 			.fe = adap->fe[0],
13973b2a5e8cSAntti Palosaari 			.chip_ver = 2,
13983b2a5e8cSAntti Palosaari 		};
13993b2a5e8cSAntti Palosaari 
1400676c350fSAntti Palosaari 		if (state->dual_mode) {
1401676c350fSAntti Palosaari 			if (adap->id == 0)
1402676c350fSAntti Palosaari 				it913x_config.role = IT913X_ROLE_DUAL_MASTER;
1403676c350fSAntti Palosaari 			else
1404676c350fSAntti Palosaari 				it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
1405676c350fSAntti Palosaari 		}
1406676c350fSAntti Palosaari 
14073b2a5e8cSAntti Palosaari 		ret = af9035_add_i2c_dev(d, "it913x",
1408a781edd1SAntti Palosaari 				state->af9033_i2c_addr[adap->id] >> 1,
14095dcf5bf6SOlli Salonen 				&it913x_config, &d->i2c_adap);
14103b2a5e8cSAntti Palosaari 		if (ret)
14113b2a5e8cSAntti Palosaari 			goto err;
14123b2a5e8cSAntti Palosaari 
14133b2a5e8cSAntti Palosaari 		fe = adap->fe[0];
1414ac77fb0fSAntti Palosaari 		break;
14153b2a5e8cSAntti Palosaari 	}
1416786baecfSMauro Carvalho Chehab 	default:
1417786baecfSMauro Carvalho Chehab 		fe = NULL;
1418786baecfSMauro Carvalho Chehab 	}
1419786baecfSMauro Carvalho Chehab 
1420786baecfSMauro Carvalho Chehab 	if (fe == NULL) {
1421786baecfSMauro Carvalho Chehab 		ret = -ENODEV;
1422786baecfSMauro Carvalho Chehab 		goto err;
1423786baecfSMauro Carvalho Chehab 	}
1424786baecfSMauro Carvalho Chehab 
1425786baecfSMauro Carvalho Chehab 	return 0;
1426786baecfSMauro Carvalho Chehab 
1427786baecfSMauro Carvalho Chehab err:
1428119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1429786baecfSMauro Carvalho Chehab 
1430786baecfSMauro Carvalho Chehab 	return ret;
1431786baecfSMauro Carvalho Chehab }
1432786baecfSMauro Carvalho Chehab 
1433d4d6a34dSAntti Palosaari static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1434d4d6a34dSAntti Palosaari {
1435d4d6a34dSAntti Palosaari 	struct state *state = adap_to_priv(adap);
1436d4d6a34dSAntti Palosaari 	struct dvb_usb_device *d = adap_to_d(adap);
1437d4d6a34dSAntti Palosaari 
1438d4d6a34dSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1439d4d6a34dSAntti Palosaari 
1440d4d6a34dSAntti Palosaari 	switch (state->af9033_config[adap->id].tuner) {
1441d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_38:
1442d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_51:
1443d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_52:
1444d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_60:
1445d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_61:
1446d4d6a34dSAntti Palosaari 	case AF9033_TUNER_IT9135_62:
1447d4d6a34dSAntti Palosaari 		if (adap->id == 1) {
1448d4d6a34dSAntti Palosaari 			if (state->i2c_client[3])
1449d4d6a34dSAntti Palosaari 				af9035_del_i2c_dev(d);
1450d4d6a34dSAntti Palosaari 		} else if (adap->id == 0) {
1451d4d6a34dSAntti Palosaari 			if (state->i2c_client[1])
1452d4d6a34dSAntti Palosaari 				af9035_del_i2c_dev(d);
1453d4d6a34dSAntti Palosaari 		}
1454d4d6a34dSAntti Palosaari 	}
1455d4d6a34dSAntti Palosaari 
1456d4d6a34dSAntti Palosaari 	return 0;
1457d4d6a34dSAntti Palosaari }
1458d4d6a34dSAntti Palosaari 
1459786baecfSMauro Carvalho Chehab static int af9035_init(struct dvb_usb_device *d)
1460786baecfSMauro Carvalho Chehab {
1461786baecfSMauro Carvalho Chehab 	struct state *state = d_to_priv(d);
1462786baecfSMauro Carvalho Chehab 	int ret, i;
1463bada342eSAntti Palosaari 	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1464bada342eSAntti Palosaari 	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1465786baecfSMauro Carvalho Chehab 	struct reg_val_mask tab[] = {
1466786baecfSMauro Carvalho Chehab 		{ 0x80f99d, 0x01, 0x01 },
1467786baecfSMauro Carvalho Chehab 		{ 0x80f9a4, 0x01, 0x01 },
1468786baecfSMauro Carvalho Chehab 		{ 0x00dd11, 0x00, 0x20 },
1469786baecfSMauro Carvalho Chehab 		{ 0x00dd11, 0x00, 0x40 },
1470786baecfSMauro Carvalho Chehab 		{ 0x00dd13, 0x00, 0x20 },
1471786baecfSMauro Carvalho Chehab 		{ 0x00dd13, 0x00, 0x40 },
1472786baecfSMauro Carvalho Chehab 		{ 0x00dd11, 0x20, 0x20 },
1473786baecfSMauro Carvalho Chehab 		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1474786baecfSMauro Carvalho Chehab 		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1475786baecfSMauro Carvalho Chehab 		{ 0x00dd0c, packet_size, 0xff},
1476786baecfSMauro Carvalho Chehab 		{ 0x00dd11, state->dual_mode << 6, 0x40 },
1477786baecfSMauro Carvalho Chehab 		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1478786baecfSMauro Carvalho Chehab 		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1479786baecfSMauro Carvalho Chehab 		{ 0x00dd0d, packet_size, 0xff },
14809805992fSJose Alberto Reguero 		{ 0x80f9a3, state->dual_mode, 0x01 },
14819805992fSJose Alberto Reguero 		{ 0x80f9cd, state->dual_mode, 0x01 },
1482786baecfSMauro Carvalho Chehab 		{ 0x80f99d, 0x00, 0x01 },
1483786baecfSMauro Carvalho Chehab 		{ 0x80f9a4, 0x00, 0x01 },
1484786baecfSMauro Carvalho Chehab 	};
1485786baecfSMauro Carvalho Chehab 
1486cb9114e9SAntti Palosaari 	dev_dbg(&d->udev->dev,
1487cb9114e9SAntti Palosaari 			"%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1488cb9114e9SAntti Palosaari 			__func__, d->udev->speed, frame_size, packet_size);
1489786baecfSMauro Carvalho Chehab 
1490786baecfSMauro Carvalho Chehab 	/* init endpoints */
1491786baecfSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
1492786baecfSMauro Carvalho Chehab 		ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1493786baecfSMauro Carvalho Chehab 				tab[i].mask);
1494786baecfSMauro Carvalho Chehab 		if (ret < 0)
1495786baecfSMauro Carvalho Chehab 			goto err;
1496786baecfSMauro Carvalho Chehab 	}
1497786baecfSMauro Carvalho Chehab 
1498786baecfSMauro Carvalho Chehab 	return 0;
1499786baecfSMauro Carvalho Chehab 
1500786baecfSMauro Carvalho Chehab err:
1501119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1502786baecfSMauro Carvalho Chehab 
1503786baecfSMauro Carvalho Chehab 	return ret;
1504786baecfSMauro Carvalho Chehab }
1505786baecfSMauro Carvalho Chehab 
150637b44a0fSAntti Palosaari #if IS_ENABLED(CONFIG_RC_CORE)
1507786baecfSMauro Carvalho Chehab static int af9035_rc_query(struct dvb_usb_device *d)
1508786baecfSMauro Carvalho Chehab {
1509786baecfSMauro Carvalho Chehab 	int ret;
151075cd5886SAntti Palosaari 	u32 key;
151175cd5886SAntti Palosaari 	u8 buf[4];
151275cd5886SAntti Palosaari 	struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1513786baecfSMauro Carvalho Chehab 
1514786baecfSMauro Carvalho Chehab 	ret = af9035_ctrl_msg(d, &req);
15151bfd5294SAntti Palosaari 	if (ret == 1)
15161bfd5294SAntti Palosaari 		return 0;
15171bfd5294SAntti Palosaari 	else if (ret < 0)
1518786baecfSMauro Carvalho Chehab 		goto err;
1519786baecfSMauro Carvalho Chehab 
152075cd5886SAntti Palosaari 	if ((buf[2] + buf[3]) == 0xff) {
152175cd5886SAntti Palosaari 		if ((buf[0] + buf[1]) == 0xff) {
152275cd5886SAntti Palosaari 			/* NEC standard 16bit */
1523120703f9SDavid Härdeman 			key = RC_SCANCODE_NEC(buf[0], buf[2]);
1524786baecfSMauro Carvalho Chehab 		} else {
152575cd5886SAntti Palosaari 			/* NEC extended 24bit */
1526120703f9SDavid Härdeman 			key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1527786baecfSMauro Carvalho Chehab 		}
1528786baecfSMauro Carvalho Chehab 	} else {
152975cd5886SAntti Palosaari 		/* NEC full code 32bit */
1530120703f9SDavid Härdeman 		key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1531120703f9SDavid Härdeman 					buf[2] << 8  | buf[3]);
1532786baecfSMauro Carvalho Chehab 	}
1533786baecfSMauro Carvalho Chehab 
153475cd5886SAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
153575cd5886SAntti Palosaari 
1536120703f9SDavid Härdeman 	rc_keydown(d->rc_dev, RC_TYPE_NEC, key, 0);
1537786baecfSMauro Carvalho Chehab 
1538786baecfSMauro Carvalho Chehab 	return 0;
15391bfd5294SAntti Palosaari 
15401bfd5294SAntti Palosaari err:
15411bfd5294SAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
15421bfd5294SAntti Palosaari 
15431bfd5294SAntti Palosaari 	return ret;
1544786baecfSMauro Carvalho Chehab }
1545786baecfSMauro Carvalho Chehab 
1546786baecfSMauro Carvalho Chehab static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1547786baecfSMauro Carvalho Chehab {
154874c1883aSAntti Palosaari 	struct state *state = d_to_priv(d);
1549786baecfSMauro Carvalho Chehab 	int ret;
1550786baecfSMauro Carvalho Chehab 	u8 tmp;
1551786baecfSMauro Carvalho Chehab 
1552431a6d4aSAntti Palosaari 	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1553786baecfSMauro Carvalho Chehab 	if (ret < 0)
1554786baecfSMauro Carvalho Chehab 		goto err;
1555786baecfSMauro Carvalho Chehab 
1556119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1557786baecfSMauro Carvalho Chehab 
1558786baecfSMauro Carvalho Chehab 	/* don't activate rc if in HID mode or if not available */
1559786baecfSMauro Carvalho Chehab 	if (tmp == 5) {
1560431a6d4aSAntti Palosaari 		ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
15619ea3681dSAntti Palosaari 				&tmp);
1562786baecfSMauro Carvalho Chehab 		if (ret < 0)
1563786baecfSMauro Carvalho Chehab 			goto err;
1564786baecfSMauro Carvalho Chehab 
1565119f7a8cSAntti Palosaari 		dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1566786baecfSMauro Carvalho Chehab 
1567786baecfSMauro Carvalho Chehab 		switch (tmp) {
1568786baecfSMauro Carvalho Chehab 		case 0: /* NEC */
1569786baecfSMauro Carvalho Chehab 		default:
1570c003ab1bSDavid Härdeman 			rc->allowed_protos = RC_BIT_NEC;
1571786baecfSMauro Carvalho Chehab 			break;
1572786baecfSMauro Carvalho Chehab 		case 1: /* RC6 */
1573c003ab1bSDavid Härdeman 			rc->allowed_protos = RC_BIT_RC6_MCE;
1574786baecfSMauro Carvalho Chehab 			break;
1575786baecfSMauro Carvalho Chehab 		}
1576786baecfSMauro Carvalho Chehab 
1577786baecfSMauro Carvalho Chehab 		rc->query = af9035_rc_query;
1578786baecfSMauro Carvalho Chehab 		rc->interval = 500;
1579786baecfSMauro Carvalho Chehab 
1580786baecfSMauro Carvalho Chehab 		/* load empty to enable rc */
1581786baecfSMauro Carvalho Chehab 		if (!rc->map_name)
1582786baecfSMauro Carvalho Chehab 			rc->map_name = RC_MAP_EMPTY;
1583786baecfSMauro Carvalho Chehab 	}
1584786baecfSMauro Carvalho Chehab 
1585786baecfSMauro Carvalho Chehab 	return 0;
1586786baecfSMauro Carvalho Chehab 
1587786baecfSMauro Carvalho Chehab err:
1588119f7a8cSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1589786baecfSMauro Carvalho Chehab 
1590786baecfSMauro Carvalho Chehab 	return ret;
1591786baecfSMauro Carvalho Chehab }
1592eed5670aSAntti Palosaari #else
1593eed5670aSAntti Palosaari 	#define af9035_get_rc_config NULL
1594eed5670aSAntti Palosaari #endif
1595786baecfSMauro Carvalho Chehab 
1596bada342eSAntti Palosaari static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1597bada342eSAntti Palosaari 		struct usb_data_stream_properties *stream)
1598bada342eSAntti Palosaari {
1599bada342eSAntti Palosaari 	struct dvb_usb_device *d = fe_to_d(fe);
1600c10989a2SAntti Palosaari 
1601bada342eSAntti Palosaari 	dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1602bada342eSAntti Palosaari 
1603bada342eSAntti Palosaari 	if (d->udev->speed == USB_SPEED_FULL)
1604bada342eSAntti Palosaari 		stream->u.bulk.buffersize = 5 * 188;
1605bada342eSAntti Palosaari 
1606bada342eSAntti Palosaari 	return 0;
1607bada342eSAntti Palosaari }
1608bada342eSAntti Palosaari 
1609bada342eSAntti Palosaari static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1610bada342eSAntti Palosaari {
1611ed97a6feSMauro Carvalho Chehab 	struct state *state = adap_to_priv(adap);
1612ed97a6feSMauro Carvalho Chehab 
1613ed97a6feSMauro Carvalho Chehab 	return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1614bada342eSAntti Palosaari }
1615bada342eSAntti Palosaari 
1616bada342eSAntti Palosaari static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1617bada342eSAntti Palosaari 		int onoff)
1618bada342eSAntti Palosaari {
1619ed97a6feSMauro Carvalho Chehab 	struct state *state = adap_to_priv(adap);
1620ed97a6feSMauro Carvalho Chehab 
1621ed97a6feSMauro Carvalho Chehab 	return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1622bada342eSAntti Palosaari }
1623bada342eSAntti Palosaari 
1624b799b810SAntti Palosaari static int af9035_probe(struct usb_interface *intf,
1625b799b810SAntti Palosaari 		const struct usb_device_id *id)
1626b799b810SAntti Palosaari {
1627b799b810SAntti Palosaari 	struct usb_device *udev = interface_to_usbdev(intf);
1628b799b810SAntti Palosaari 	char manufacturer[sizeof("Afatech")];
1629b799b810SAntti Palosaari 
1630b799b810SAntti Palosaari 	memset(manufacturer, 0, sizeof(manufacturer));
1631b799b810SAntti Palosaari 	usb_string(udev, udev->descriptor.iManufacturer,
1632b799b810SAntti Palosaari 			manufacturer, sizeof(manufacturer));
1633b799b810SAntti Palosaari 	/*
1634b799b810SAntti Palosaari 	 * There is two devices having same ID but different chipset. One uses
1635b799b810SAntti Palosaari 	 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1636b799b810SAntti Palosaari 	 * is iManufacturer string.
1637b799b810SAntti Palosaari 	 *
1638b799b810SAntti Palosaari 	 * idVendor           0x0ccd TerraTec Electronic GmbH
1639b799b810SAntti Palosaari 	 * idProduct          0x0099
1640b799b810SAntti Palosaari 	 * bcdDevice            2.00
1641b799b810SAntti Palosaari 	 * iManufacturer           1 Afatech
1642b799b810SAntti Palosaari 	 * iProduct                2 DVB-T 2
1643b799b810SAntti Palosaari 	 *
1644b799b810SAntti Palosaari 	 * idVendor           0x0ccd TerraTec Electronic GmbH
1645b799b810SAntti Palosaari 	 * idProduct          0x0099
1646b799b810SAntti Palosaari 	 * bcdDevice            2.00
1647b799b810SAntti Palosaari 	 * iManufacturer           1 ITE Technologies, Inc.
1648b799b810SAntti Palosaari 	 * iProduct                2 DVB-T TV Stick
1649b799b810SAntti Palosaari 	 */
1650b799b810SAntti Palosaari 	if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1651b799b810SAntti Palosaari 			(le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1652b799b810SAntti Palosaari 		if (!strcmp("Afatech", manufacturer)) {
1653b799b810SAntti Palosaari 			dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1654b799b810SAntti Palosaari 			return -ENODEV;
1655b799b810SAntti Palosaari 		}
1656b799b810SAntti Palosaari 	}
1657b799b810SAntti Palosaari 
1658b799b810SAntti Palosaari 	return dvb_usbv2_probe(intf, id);
1659b799b810SAntti Palosaari }
1660b799b810SAntti Palosaari 
1661786baecfSMauro Carvalho Chehab /* interface 0 is used by DVB-T receiver and
1662786baecfSMauro Carvalho Chehab    interface 1 is for remote controller (HID) */
1663786baecfSMauro Carvalho Chehab static const struct dvb_usb_device_properties af9035_props = {
1664786baecfSMauro Carvalho Chehab 	.driver_name = KBUILD_MODNAME,
1665786baecfSMauro Carvalho Chehab 	.owner = THIS_MODULE,
1666786baecfSMauro Carvalho Chehab 	.adapter_nr = adapter_nr,
1667786baecfSMauro Carvalho Chehab 	.size_of_priv = sizeof(struct state),
1668786baecfSMauro Carvalho Chehab 
1669786baecfSMauro Carvalho Chehab 	.generic_bulk_ctrl_endpoint = 0x02,
1670786baecfSMauro Carvalho Chehab 	.generic_bulk_ctrl_endpoint_response = 0x81,
1671786baecfSMauro Carvalho Chehab 
1672786baecfSMauro Carvalho Chehab 	.identify_state = af9035_identify_state,
1673786baecfSMauro Carvalho Chehab 	.download_firmware = af9035_download_firmware,
1674786baecfSMauro Carvalho Chehab 
1675786baecfSMauro Carvalho Chehab 	.i2c_algo = &af9035_i2c_algo,
1676786baecfSMauro Carvalho Chehab 	.read_config = af9035_read_config,
1677786baecfSMauro Carvalho Chehab 	.frontend_attach = af9035_frontend_attach,
1678d4d6a34dSAntti Palosaari 	.frontend_detach = af9035_frontend_detach,
1679786baecfSMauro Carvalho Chehab 	.tuner_attach = af9035_tuner_attach,
1680d4d6a34dSAntti Palosaari 	.tuner_detach = af9035_tuner_detach,
1681786baecfSMauro Carvalho Chehab 	.init = af9035_init,
1682786baecfSMauro Carvalho Chehab 	.get_rc_config = af9035_get_rc_config,
1683bada342eSAntti Palosaari 	.get_stream_config = af9035_get_stream_config,
1684786baecfSMauro Carvalho Chehab 
16859805992fSJose Alberto Reguero 	.get_adapter_count = af9035_get_adapter_count,
1686786baecfSMauro Carvalho Chehab 	.adapter = {
1687786baecfSMauro Carvalho Chehab 		{
1688bada342eSAntti Palosaari 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
1689bada342eSAntti Palosaari 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1690bada342eSAntti Palosaari 
1691bada342eSAntti Palosaari 			.pid_filter_count = 32,
1692bada342eSAntti Palosaari 			.pid_filter_ctrl = af9035_pid_filter_ctrl,
1693bada342eSAntti Palosaari 			.pid_filter = af9035_pid_filter,
1694bada342eSAntti Palosaari 
1695786baecfSMauro Carvalho Chehab 			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1696786baecfSMauro Carvalho Chehab 		}, {
1697b24c2b4fSAntti Palosaari 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
1698b24c2b4fSAntti Palosaari 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1699b24c2b4fSAntti Palosaari 
1700b24c2b4fSAntti Palosaari 			.pid_filter_count = 32,
1701b24c2b4fSAntti Palosaari 			.pid_filter_ctrl = af9035_pid_filter_ctrl,
1702b24c2b4fSAntti Palosaari 			.pid_filter = af9035_pid_filter,
1703b24c2b4fSAntti Palosaari 
1704786baecfSMauro Carvalho Chehab 			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1705786baecfSMauro Carvalho Chehab 		},
1706786baecfSMauro Carvalho Chehab 	},
1707786baecfSMauro Carvalho Chehab };
1708786baecfSMauro Carvalho Chehab 
1709786baecfSMauro Carvalho Chehab static const struct usb_device_id af9035_id_table[] = {
1710bc3c9e10SAntti Palosaari 	/* AF9035 devices */
1711786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1712786baecfSMauro Carvalho Chehab 		&af9035_props, "Afatech AF9035 reference design", NULL) },
1713786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1714786baecfSMauro Carvalho Chehab 		&af9035_props, "Afatech AF9035 reference design", NULL) },
1715786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1716786baecfSMauro Carvalho Chehab 		&af9035_props, "Afatech AF9035 reference design", NULL) },
1717786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1718786baecfSMauro Carvalho Chehab 		&af9035_props, "Afatech AF9035 reference design", NULL) },
1719786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1720786baecfSMauro Carvalho Chehab 		&af9035_props, "Afatech AF9035 reference design", NULL) },
1721786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1722786baecfSMauro Carvalho Chehab 		&af9035_props, "TerraTec Cinergy T Stick", NULL) },
1723786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1724786baecfSMauro Carvalho Chehab 		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1725786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1726786baecfSMauro Carvalho Chehab 		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1727786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1728786baecfSMauro Carvalho Chehab 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1729786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1730786baecfSMauro Carvalho Chehab 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1731786baecfSMauro Carvalho Chehab 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1732786baecfSMauro Carvalho Chehab 		&af9035_props, "AVerMedia Twinstar (A825)", NULL) },
1733d67ceb33SOliver Schinagl 	{ DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1734d67ceb33SOliver Schinagl 		&af9035_props, "Asus U3100Mini Plus", NULL) },
1735d9b7595bSFabrizio Gazzato 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
1736d9b7595bSFabrizio Gazzato 		&af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
1737bc3c9e10SAntti Palosaari 	/* IT9135 devices */
17387a541ce0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
17397a541ce0SMalcolm Priestley 		&af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
17407a541ce0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
17417a541ce0SMalcolm Priestley 		&af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
17427a541ce0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
17437a541ce0SMalcolm Priestley 		&af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
17447a541ce0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
17457a541ce0SMalcolm Priestley 		&af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
17467a541ce0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
17477a541ce0SMalcolm Priestley 		&af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
17487a541ce0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
17497a541ce0SMalcolm Priestley 		&af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
17507a541ce0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
17517a541ce0SMalcolm Priestley 		&af9035_props, "Avermedia A835B(4835)",	RC_MAP_IT913X_V2) },
17527a541ce0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
17537a541ce0SMalcolm Priestley 		&af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
175437973e01SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
175537973e01SMalcolm Priestley 		&af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
175637973e01SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
175737973e01SMalcolm Priestley 		&af9035_props, "Sveon STV22 Dual DVB-T HDTV",
175837973e01SMalcolm Priestley 							RC_MAP_IT913X_V1) },
175937973e01SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
176037973e01SMalcolm Priestley 		&af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
176137973e01SMalcolm Priestley 							RC_MAP_IT913X_V1) },
1762b799b810SAntti Palosaari 	/* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1763b799b810SAntti Palosaari 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1764c10989a2SAntti Palosaari 		&af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
1765c10989a2SAntti Palosaari 		NULL) },
17660c413d10SAntti Palosaari 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
17670c413d10SAntti Palosaari 		&af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
1768261cb200SAntti Palosaari 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
1769261cb200SAntti Palosaari 		&af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
1770a04646c0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
17715c114a4fSAntti Palosaari 		&af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
1772a04646c0SMalcolm Priestley 	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
17735c114a4fSAntti Palosaari 		&af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
1774786baecfSMauro Carvalho Chehab 	{ }
1775786baecfSMauro Carvalho Chehab };
1776786baecfSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, af9035_id_table);
1777786baecfSMauro Carvalho Chehab 
1778786baecfSMauro Carvalho Chehab static struct usb_driver af9035_usb_driver = {
1779786baecfSMauro Carvalho Chehab 	.name = KBUILD_MODNAME,
1780786baecfSMauro Carvalho Chehab 	.id_table = af9035_id_table,
1781b799b810SAntti Palosaari 	.probe = af9035_probe,
1782786baecfSMauro Carvalho Chehab 	.disconnect = dvb_usbv2_disconnect,
1783786baecfSMauro Carvalho Chehab 	.suspend = dvb_usbv2_suspend,
1784786baecfSMauro Carvalho Chehab 	.resume = dvb_usbv2_resume,
178504966aa8SAntti Palosaari 	.reset_resume = dvb_usbv2_reset_resume,
1786786baecfSMauro Carvalho Chehab 	.no_dynamic_id = 1,
1787786baecfSMauro Carvalho Chehab 	.soft_unbind = 1,
1788786baecfSMauro Carvalho Chehab };
1789786baecfSMauro Carvalho Chehab 
1790786baecfSMauro Carvalho Chehab module_usb_driver(af9035_usb_driver);
1791786baecfSMauro Carvalho Chehab 
1792786baecfSMauro Carvalho Chehab MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1793786baecfSMauro Carvalho Chehab MODULE_DESCRIPTION("Afatech AF9035 driver");
1794786baecfSMauro Carvalho Chehab MODULE_LICENSE("GPL");
17954395e4b7SAntti Palosaari MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
179674c1883aSAntti Palosaari MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
179774c1883aSAntti Palosaari MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
1798