Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Afatech AF9035 DVB USB driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Antti Palosaari <crope@iki.fi> |
| 5 | * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "af9035.h" |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 23 | |
| 24 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 25 | |
Michael Büsch | b1a9599 | 2012-04-01 16:33:48 -0300 | [diff] [blame] | 26 | static u16 af9035_checksum(const u8 *buf, size_t len) |
| 27 | { |
| 28 | size_t i; |
| 29 | u16 checksum = 0; |
| 30 | |
| 31 | for (i = 1; i < len; i++) { |
| 32 | if (i % 2) |
| 33 | checksum += buf[i] << 8; |
| 34 | else |
| 35 | checksum += buf[i]; |
| 36 | } |
| 37 | checksum = ~checksum; |
| 38 | |
| 39 | return checksum; |
| 40 | } |
| 41 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 42 | static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req) |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 43 | { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 44 | #define REQ_HDR_LEN 4 /* send header size */ |
| 45 | #define ACK_HDR_LEN 3 /* rece header size */ |
| 46 | #define CHECKSUM_LEN 2 |
| 47 | #define USB_TIMEOUT 2000 |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 48 | struct state *state = d_to_priv(d); |
| 49 | int ret, wlen, rlen; |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 50 | u16 checksum, tmp_checksum; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 51 | |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 52 | mutex_lock(&d->usb_mutex); |
| 53 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 54 | /* buffer overflow check */ |
| 55 | if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) || |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 56 | req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) { |
| 57 | dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n", |
| 58 | __func__, req->wlen, req->rlen); |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 59 | ret = -EINVAL; |
| 60 | goto err; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 61 | } |
| 62 | |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 63 | state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1; |
| 64 | state->buf[1] = req->mbox; |
| 65 | state->buf[2] = req->cmd; |
| 66 | state->buf[3] = state->seq++; |
| 67 | memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 68 | |
| 69 | wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN; |
| 70 | rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 71 | |
| 72 | /* calc and add checksum */ |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 73 | checksum = af9035_checksum(state->buf, state->buf[0] - 1); |
| 74 | state->buf[state->buf[0] - 1] = (checksum >> 8); |
| 75 | state->buf[state->buf[0] - 0] = (checksum & 0xff); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 76 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 77 | /* no ack for these packets */ |
| 78 | if (req->cmd == CMD_FW_DL) |
| 79 | rlen = 0; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 80 | |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 81 | ret = dvb_usbv2_generic_rw_locked(d, |
| 82 | state->buf, wlen, state->buf, rlen); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 83 | if (ret) |
| 84 | goto err; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 85 | |
| 86 | /* no ack for those packets */ |
| 87 | if (req->cmd == CMD_FW_DL) |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 88 | goto exit; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 89 | |
Michael Büsch | b1a9599 | 2012-04-01 16:33:48 -0300 | [diff] [blame] | 90 | /* verify checksum */ |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 91 | checksum = af9035_checksum(state->buf, rlen - 2); |
| 92 | tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1]; |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 93 | if (tmp_checksum != checksum) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 94 | dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \ |
| 95 | "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd, |
| 96 | tmp_checksum, checksum); |
Michael Büsch | b1a9599 | 2012-04-01 16:33:48 -0300 | [diff] [blame] | 97 | ret = -EIO; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 98 | goto err; |
Michael Büsch | b1a9599 | 2012-04-01 16:33:48 -0300 | [diff] [blame] | 99 | } |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 100 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 101 | /* check status */ |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 102 | if (state->buf[2]) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 103 | dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n", |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 104 | __func__, req->cmd, state->buf[2]); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 105 | ret = -EIO; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 106 | goto err; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* read request, copy returned data to return buf */ |
| 110 | if (req->rlen) |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 111 | memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 112 | exit: |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 113 | err: |
Antti Palosaari | 3484d37 | 2013-02-26 13:56:34 -0300 | [diff] [blame] | 114 | mutex_unlock(&d->usb_mutex); |
| 115 | if (ret) |
| 116 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | /* write multiple registers */ |
| 121 | static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) |
| 122 | { |
| 123 | u8 wbuf[6 + len]; |
| 124 | u8 mbox = (reg >> 16) & 0xff; |
| 125 | struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL }; |
| 126 | |
| 127 | wbuf[0] = len; |
| 128 | wbuf[1] = 2; |
| 129 | wbuf[2] = 0; |
| 130 | wbuf[3] = 0; |
| 131 | wbuf[4] = (reg >> 8) & 0xff; |
| 132 | wbuf[5] = (reg >> 0) & 0xff; |
| 133 | memcpy(&wbuf[6], val, len); |
| 134 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 135 | return af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* read multiple registers */ |
| 139 | static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) |
| 140 | { |
| 141 | u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff }; |
| 142 | u8 mbox = (reg >> 16) & 0xff; |
| 143 | struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val }; |
| 144 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 145 | return af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* write single register */ |
| 149 | static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val) |
| 150 | { |
| 151 | return af9035_wr_regs(d, reg, &val, 1); |
| 152 | } |
| 153 | |
| 154 | /* read single register */ |
| 155 | static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val) |
| 156 | { |
| 157 | return af9035_rd_regs(d, reg, val, 1); |
| 158 | } |
| 159 | |
| 160 | /* write single register with mask */ |
| 161 | static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val, |
| 162 | u8 mask) |
| 163 | { |
| 164 | int ret; |
| 165 | u8 tmp; |
| 166 | |
| 167 | /* no need for read if whole reg is written */ |
| 168 | if (mask != 0xff) { |
| 169 | ret = af9035_rd_regs(d, reg, &tmp, 1); |
| 170 | if (ret) |
| 171 | return ret; |
| 172 | |
| 173 | val &= mask; |
| 174 | tmp &= ~mask; |
| 175 | val |= tmp; |
| 176 | } |
| 177 | |
| 178 | return af9035_wr_regs(d, reg, &val, 1); |
| 179 | } |
| 180 | |
| 181 | static int af9035_i2c_master_xfer(struct i2c_adapter *adap, |
| 182 | struct i2c_msg msg[], int num) |
| 183 | { |
| 184 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 185 | struct state *state = d_to_priv(d); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 186 | int ret; |
| 187 | |
| 188 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) |
| 189 | return -EAGAIN; |
| 190 | |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 191 | /* |
| 192 | * I2C sub header is 5 bytes long. Meaning of those bytes are: |
| 193 | * 0: data len |
| 194 | * 1: I2C addr << 1 |
| 195 | * 2: reg addr len |
| 196 | * byte 3 and 4 can be used as reg addr |
| 197 | * 3: reg addr MSB |
| 198 | * used when reg addr len is set to 2 |
| 199 | * 4: reg addr LSB |
| 200 | * used when reg addr len is set to 1 or 2 |
| 201 | * |
| 202 | * For the simplify we do not use register addr at all. |
| 203 | * NOTE: As a firmware knows tuner type there is very small possibility |
| 204 | * there could be some tuner I2C hacks done by firmware and this may |
| 205 | * lead problems if firmware expects those bytes are used. |
| 206 | */ |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 207 | if (num == 2 && !(msg[0].flags & I2C_M_RD) && |
| 208 | (msg[1].flags & I2C_M_RD)) { |
| 209 | if (msg[0].len > 40 || msg[1].len > 40) { |
| 210 | /* TODO: correct limits > 40 */ |
| 211 | ret = -EOPNOTSUPP; |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 212 | } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) || |
| 213 | (msg[0].addr == state->af9033_config[1].i2c_addr)) { |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 214 | /* demod access via firmware interface */ |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 215 | u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | |
| 216 | msg[0].buf[2]; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 217 | |
| 218 | if (msg[0].addr == state->af9033_config[1].i2c_addr) |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 219 | reg |= 0x100000; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 220 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 221 | ret = af9035_rd_regs(d, reg, &msg[1].buf[0], |
| 222 | msg[1].len); |
| 223 | } else { |
| 224 | /* I2C */ |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 225 | u8 buf[5 + msg[0].len]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 226 | struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf), |
| 227 | buf, msg[1].len, msg[1].buf }; |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 228 | req.mbox |= ((msg[0].addr & 0x80) >> 3); |
Hans-Frieder Vogt | 812fe6d | 2012-04-01 14:11:29 -0300 | [diff] [blame] | 229 | buf[0] = msg[1].len; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 230 | buf[1] = msg[0].addr << 1; |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 231 | buf[2] = 0x00; /* reg addr len */ |
| 232 | buf[3] = 0x00; /* reg addr MSB */ |
| 233 | buf[4] = 0x00; /* reg addr LSB */ |
| 234 | memcpy(&buf[5], msg[0].buf, msg[0].len); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 235 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 236 | } |
| 237 | } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { |
| 238 | if (msg[0].len > 40) { |
| 239 | /* TODO: correct limits > 40 */ |
| 240 | ret = -EOPNOTSUPP; |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 241 | } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) || |
| 242 | (msg[0].addr == state->af9033_config[1].i2c_addr)) { |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 243 | /* demod access via firmware interface */ |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 244 | u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | |
| 245 | msg[0].buf[2]; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 246 | |
| 247 | if (msg[0].addr == state->af9033_config[1].i2c_addr) |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 248 | reg |= 0x100000; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 249 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 250 | ret = af9035_wr_regs(d, reg, &msg[0].buf[3], |
| 251 | msg[0].len - 3); |
| 252 | } else { |
| 253 | /* I2C */ |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 254 | u8 buf[5 + msg[0].len]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 255 | struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf, |
| 256 | 0, NULL }; |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 257 | req.mbox |= ((msg[0].addr & 0x80) >> 3); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 258 | buf[0] = msg[0].len; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 259 | buf[1] = msg[0].addr << 1; |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 260 | buf[2] = 0x00; /* reg addr len */ |
| 261 | buf[3] = 0x00; /* reg addr MSB */ |
| 262 | buf[4] = 0x00; /* reg addr LSB */ |
| 263 | memcpy(&buf[5], msg[0].buf, msg[0].len); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 264 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 265 | } |
| 266 | } else { |
| 267 | /* |
| 268 | * We support only two kind of I2C transactions: |
| 269 | * 1) 1 x read + 1 x write |
| 270 | * 2) 1 x write |
| 271 | */ |
| 272 | ret = -EOPNOTSUPP; |
| 273 | } |
| 274 | |
| 275 | mutex_unlock(&d->i2c_mutex); |
| 276 | |
| 277 | if (ret < 0) |
| 278 | return ret; |
| 279 | else |
| 280 | return num; |
| 281 | } |
| 282 | |
| 283 | static u32 af9035_i2c_functionality(struct i2c_adapter *adapter) |
| 284 | { |
| 285 | return I2C_FUNC_I2C; |
| 286 | } |
| 287 | |
| 288 | static struct i2c_algorithm af9035_i2c_algo = { |
| 289 | .master_xfer = af9035_i2c_master_xfer, |
| 290 | .functionality = af9035_i2c_functionality, |
| 291 | }; |
| 292 | |
Antti Palosaari | a0921af | 2012-06-18 23:42:53 -0300 | [diff] [blame] | 293 | static int af9035_identify_state(struct dvb_usb_device *d, const char **name) |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 294 | { |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 295 | struct state *state = d_to_priv(d); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 296 | int ret; |
| 297 | u8 wbuf[1] = { 1 }; |
| 298 | u8 rbuf[4]; |
| 299 | struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf, |
| 300 | sizeof(rbuf), rbuf }; |
| 301 | |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 302 | ret = af9035_rd_regs(d, 0x1222, rbuf, 3); |
| 303 | if (ret < 0) |
| 304 | goto err; |
| 305 | |
| 306 | state->chip_version = rbuf[0]; |
| 307 | state->chip_type = rbuf[2] << 8 | rbuf[1] << 0; |
| 308 | |
| 309 | ret = af9035_rd_reg(d, 0x384f, &state->prechip_version); |
| 310 | if (ret < 0) |
| 311 | goto err; |
| 312 | |
| 313 | dev_info(&d->udev->dev, |
| 314 | "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n", |
| 315 | __func__, state->prechip_version, state->chip_version, |
| 316 | state->chip_type); |
| 317 | |
| 318 | if (state->chip_type == 0x9135) { |
| 319 | if (state->chip_version == 2) |
| 320 | *name = AF9035_FIRMWARE_IT9135_V2; |
| 321 | else |
| 322 | *name = AF9035_FIRMWARE_IT9135_V1; |
| 323 | } else { |
| 324 | *name = AF9035_FIRMWARE_AF9035; |
| 325 | } |
| 326 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 327 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 328 | if (ret < 0) |
| 329 | goto err; |
| 330 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 331 | dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 332 | if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3]) |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 333 | ret = WARM; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 334 | else |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 335 | ret = COLD; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 336 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 337 | return ret; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 338 | |
| 339 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 340 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 345 | static int af9035_download_firmware_af9035(struct dvb_usb_device *d, |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 346 | const struct firmware *fw) |
| 347 | { |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 348 | int ret, i, j, len; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 349 | u8 wbuf[1]; |
| 350 | u8 rbuf[4]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 351 | struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; |
| 352 | struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL }; |
| 353 | struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 354 | u8 hdr_core, tmp; |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 355 | u16 hdr_addr, hdr_data_len, hdr_checksum; |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 356 | #define MAX_DATA 58 |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 357 | #define HDR_SIZE 7 |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 358 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 359 | /* |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 360 | * In case of dual tuner configuration we need to do some extra |
| 361 | * initialization in order to download firmware to slave demod too, |
| 362 | * which is done by master demod. |
| 363 | * Master feeds also clock and controls power via GPIO. |
| 364 | */ |
| 365 | ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp); |
| 366 | if (ret < 0) |
| 367 | goto err; |
| 368 | |
| 369 | if (tmp) { |
| 370 | /* configure gpioh1, reset & power slave demod */ |
| 371 | ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01); |
| 372 | if (ret < 0) |
| 373 | goto err; |
| 374 | |
| 375 | ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01); |
| 376 | if (ret < 0) |
| 377 | goto err; |
| 378 | |
| 379 | ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01); |
| 380 | if (ret < 0) |
| 381 | goto err; |
| 382 | |
| 383 | usleep_range(10000, 50000); |
| 384 | |
| 385 | ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01); |
| 386 | if (ret < 0) |
| 387 | goto err; |
| 388 | |
| 389 | /* tell the slave I2C address */ |
| 390 | ret = af9035_rd_reg(d, EEPROM_2ND_DEMOD_ADDR, &tmp); |
| 391 | if (ret < 0) |
| 392 | goto err; |
| 393 | |
| 394 | ret = af9035_wr_reg(d, 0x00417f, tmp); |
| 395 | if (ret < 0) |
| 396 | goto err; |
| 397 | |
| 398 | /* enable clock out */ |
| 399 | ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01); |
| 400 | if (ret < 0) |
| 401 | goto err; |
| 402 | } |
| 403 | |
| 404 | /* |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 405 | * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info! |
| 406 | * |
| 407 | * byte 0: MCS 51 core |
| 408 | * There are two inside the AF9035 (1=Link and 2=OFDM) with separate |
| 409 | * address spaces |
| 410 | * byte 1-2: Big endian destination address |
| 411 | * byte 3-4: Big endian number of data bytes following the header |
| 412 | * byte 5-6: Big endian header checksum, apparently ignored by the chip |
| 413 | * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256) |
| 414 | */ |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 415 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 416 | for (i = fw->size; i > HDR_SIZE;) { |
| 417 | hdr_core = fw->data[fw->size - i + 0]; |
| 418 | hdr_addr = fw->data[fw->size - i + 1] << 8; |
| 419 | hdr_addr |= fw->data[fw->size - i + 2] << 0; |
| 420 | hdr_data_len = fw->data[fw->size - i + 3] << 8; |
| 421 | hdr_data_len |= fw->data[fw->size - i + 4] << 0; |
| 422 | hdr_checksum = fw->data[fw->size - i + 5] << 8; |
| 423 | hdr_checksum |= fw->data[fw->size - i + 6] << 0; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 424 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 425 | dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \ |
| 426 | "checksum=%04x\n", __func__, hdr_core, hdr_addr, |
| 427 | hdr_data_len, hdr_checksum); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 428 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 429 | if (((hdr_core != 1) && (hdr_core != 2)) || |
| 430 | (hdr_data_len > i)) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 431 | dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__); |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 432 | break; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 433 | } |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 434 | |
| 435 | /* download begin packet */ |
| 436 | req.cmd = CMD_FW_DL_BEGIN; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 437 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 41d44a8 | 2012-04-01 11:06:23 -0300 | [diff] [blame] | 438 | if (ret < 0) |
| 439 | goto err; |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 440 | |
| 441 | /* download firmware packet(s) */ |
| 442 | for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) { |
| 443 | len = j; |
| 444 | if (len > MAX_DATA) |
| 445 | len = MAX_DATA; |
| 446 | req_fw_dl.wlen = len; |
| 447 | req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i + |
| 448 | HDR_SIZE + hdr_data_len - j]; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 449 | ret = af9035_ctrl_msg(d, &req_fw_dl); |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 450 | if (ret < 0) |
| 451 | goto err; |
| 452 | } |
| 453 | |
| 454 | /* download end packet */ |
| 455 | req.cmd = CMD_FW_DL_END; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 456 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 457 | if (ret < 0) |
| 458 | goto err; |
| 459 | |
| 460 | i -= hdr_data_len + HDR_SIZE; |
| 461 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 462 | dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n", |
| 463 | __func__, fw->size - i); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 464 | } |
| 465 | |
Antti Palosaari | ff4e3fe | 2012-12-09 16:25:08 -0300 | [diff] [blame] | 466 | /* print warn if firmware is bad, continue and see what happens */ |
| 467 | if (i) |
| 468 | dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME); |
| 469 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 470 | /* firmware loaded, request boot */ |
| 471 | req.cmd = CMD_FW_BOOT; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 472 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 473 | if (ret < 0) |
| 474 | goto err; |
| 475 | |
| 476 | /* ensure firmware starts */ |
| 477 | wbuf[0] = 1; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 478 | ret = af9035_ctrl_msg(d, &req_fw_ver); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 479 | if (ret < 0) |
| 480 | goto err; |
| 481 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 482 | if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 483 | dev_err(&d->udev->dev, "%s: firmware did not run\n", |
| 484 | KBUILD_MODNAME); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 485 | ret = -ENODEV; |
| 486 | goto err; |
| 487 | } |
| 488 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 489 | dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d", |
| 490 | KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]); |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 491 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 492 | return 0; |
| 493 | |
| 494 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 495 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 500 | static int af9035_download_firmware_it9135(struct dvb_usb_device *d, |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 501 | const struct firmware *fw) |
| 502 | { |
| 503 | int ret, i, i_prev; |
| 504 | u8 wbuf[1]; |
| 505 | u8 rbuf[4]; |
| 506 | struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; |
| 507 | struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL }; |
| 508 | struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; |
| 509 | #define HDR_SIZE 7 |
| 510 | |
| 511 | /* |
| 512 | * There seems to be following firmware header. Meaning of bytes 0-3 |
| 513 | * is unknown. |
| 514 | * |
| 515 | * 0: 3 |
| 516 | * 1: 0, 1 |
| 517 | * 2: 0 |
| 518 | * 3: 1, 2, 3 |
| 519 | * 4: addr MSB |
| 520 | * 5: addr LSB |
| 521 | * 6: count of data bytes ? |
| 522 | */ |
| 523 | |
| 524 | for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) { |
| 525 | if (i == fw->size || |
| 526 | (fw->data[i + 0] == 0x03 && |
| 527 | (fw->data[i + 1] == 0x00 || |
| 528 | fw->data[i + 1] == 0x01) && |
| 529 | fw->data[i + 2] == 0x00)) { |
| 530 | req_fw_dl.wlen = i - i_prev; |
| 531 | req_fw_dl.wbuf = (u8 *) &fw->data[i_prev]; |
| 532 | i_prev = i; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 533 | ret = af9035_ctrl_msg(d, &req_fw_dl); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 534 | if (ret < 0) |
| 535 | goto err; |
| 536 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 537 | dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n", |
| 538 | __func__, i); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | /* firmware loaded, request boot */ |
| 543 | req.cmd = CMD_FW_BOOT; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 544 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 545 | if (ret < 0) |
| 546 | goto err; |
| 547 | |
| 548 | /* ensure firmware starts */ |
| 549 | wbuf[0] = 1; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 550 | ret = af9035_ctrl_msg(d, &req_fw_ver); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 551 | if (ret < 0) |
| 552 | goto err; |
| 553 | |
| 554 | if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 555 | dev_err(&d->udev->dev, "%s: firmware did not run\n", |
| 556 | KBUILD_MODNAME); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 557 | ret = -ENODEV; |
| 558 | goto err; |
| 559 | } |
| 560 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 561 | dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d", |
| 562 | KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | |
| 566 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 567 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 568 | |
| 569 | return ret; |
| 570 | } |
| 571 | |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 572 | static int af9035_download_firmware(struct dvb_usb_device *d, |
| 573 | const struct firmware *fw) |
| 574 | { |
| 575 | struct state *state = d_to_priv(d); |
| 576 | |
| 577 | if (state->chip_type == 0x9135) |
| 578 | return af9035_download_firmware_it9135(d, fw); |
| 579 | else |
| 580 | return af9035_download_firmware_af9035(d, fw); |
| 581 | } |
| 582 | |
| 583 | static int af9035_read_config_af9035(struct dvb_usb_device *d) |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 584 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 585 | struct state *state = d_to_priv(d); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 586 | int ret, i, eeprom_shift = 0; |
| 587 | u8 tmp; |
| 588 | u16 tmp16; |
| 589 | |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 590 | /* demod I2C "address" */ |
| 591 | state->af9033_config[0].i2c_addr = 0x38; |
Antti Palosaari | 0d94d6a | 2013-01-07 15:26:05 -0300 | [diff] [blame] | 592 | state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 593 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 594 | /* check if there is dual tuners */ |
| 595 | ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp); |
| 596 | if (ret < 0) |
| 597 | goto err; |
| 598 | |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 599 | state->dual_mode = tmp; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 600 | dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__, |
| 601 | state->dual_mode); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 602 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 603 | if (state->dual_mode) { |
| 604 | /* read 2nd demodulator I2C address */ |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 605 | ret = af9035_rd_reg(d, EEPROM_2ND_DEMOD_ADDR, &tmp); |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 606 | if (ret < 0) |
| 607 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 608 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 609 | state->af9033_config[1].i2c_addr = tmp; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 610 | dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n", |
| 611 | __func__, tmp); |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 612 | } |
| 613 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 614 | for (i = 0; i < state->dual_mode + 1; i++) { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 615 | /* tuner */ |
| 616 | ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp); |
| 617 | if (ret < 0) |
| 618 | goto err; |
| 619 | |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 620 | state->af9033_config[i].tuner = tmp; |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 621 | dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n", |
| 622 | __func__, i, tmp); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 623 | |
| 624 | switch (tmp) { |
| 625 | case AF9033_TUNER_TUA9001: |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 626 | case AF9033_TUNER_FC0011: |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 627 | case AF9033_TUNER_MXL5007T: |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 628 | case AF9033_TUNER_TDA18218: |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame] | 629 | case AF9033_TUNER_FC2580: |
Antti Palosaari | 7e0bc29 | 2012-12-02 20:12:29 -0300 | [diff] [blame] | 630 | case AF9033_TUNER_FC0012: |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 631 | state->af9033_config[i].spec_inv = 1; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 632 | break; |
| 633 | default: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 634 | dev_warn(&d->udev->dev, "%s: tuner id=%02x not " \ |
| 635 | "supported, please report!", |
| 636 | KBUILD_MODNAME, tmp); |
Peter Senna Tschudin | c2c1b41 | 2012-09-28 05:37:22 -0300 | [diff] [blame] | 637 | } |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 638 | |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 639 | /* disable dual mode if driver does not support it */ |
| 640 | if (i == 1) |
| 641 | switch (tmp) { |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 642 | case AF9033_TUNER_FC0012: |
| 643 | break; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 644 | default: |
| 645 | state->dual_mode = false; |
| 646 | dev_info(&d->udev->dev, "%s: driver does not " \ |
| 647 | "support 2nd tuner and will " \ |
| 648 | "disable it", KBUILD_MODNAME); |
| 649 | } |
| 650 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 651 | /* tuner IF frequency */ |
| 652 | ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp); |
| 653 | if (ret < 0) |
| 654 | goto err; |
| 655 | |
| 656 | tmp16 = tmp; |
| 657 | |
| 658 | ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp); |
| 659 | if (ret < 0) |
| 660 | goto err; |
| 661 | |
| 662 | tmp16 |= tmp << 8; |
| 663 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 664 | dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 665 | |
| 666 | eeprom_shift = 0x10; /* shift for the 2nd tuner params */ |
| 667 | } |
| 668 | |
| 669 | /* get demod clock */ |
| 670 | ret = af9035_rd_reg(d, 0x00d800, &tmp); |
| 671 | if (ret < 0) |
| 672 | goto err; |
| 673 | |
| 674 | tmp = (tmp >> 0) & 0x0f; |
| 675 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 676 | for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 677 | state->af9033_config[i].clock = clock_lut[tmp]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 678 | |
| 679 | return 0; |
| 680 | |
| 681 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 682 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 683 | |
| 684 | return ret; |
| 685 | } |
| 686 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 687 | static int af9035_read_config_it9135(struct dvb_usb_device *d) |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 688 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 689 | struct state *state = d_to_priv(d); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 690 | int ret, i; |
| 691 | u8 tmp; |
| 692 | |
Antti Palosaari | ac77fb0 | 2013-01-07 09:51:13 -0300 | [diff] [blame] | 693 | /* demod I2C "address" */ |
| 694 | state->af9033_config[0].i2c_addr = 0x38; |
| 695 | state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38; |
| 696 | state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X; |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 697 | state->dual_mode = false; |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 698 | |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 699 | /* check if eeprom exists */ |
| 700 | if (state->chip_version == 2) |
| 701 | ret = af9035_rd_reg(d, 0x00461d, &tmp); |
| 702 | else |
| 703 | ret = af9035_rd_reg(d, 0x00461b, &tmp); |
| 704 | if (ret < 0) |
| 705 | goto err; |
| 706 | |
| 707 | if (tmp) { |
| 708 | /* tuner */ |
| 709 | ret = af9035_rd_reg(d, 0x0049d0, &tmp); |
| 710 | if (ret < 0) |
| 711 | goto err; |
| 712 | |
| 713 | dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n", |
| 714 | __func__, 0, tmp); |
| 715 | |
| 716 | if (tmp) |
| 717 | state->af9033_config[0].tuner = tmp; |
| 718 | } |
| 719 | |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 720 | /* get demod clock */ |
| 721 | ret = af9035_rd_reg(d, 0x00d800, &tmp); |
| 722 | if (ret < 0) |
| 723 | goto err; |
| 724 | |
| 725 | tmp = (tmp >> 0) & 0x0f; |
| 726 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 727 | for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 728 | state->af9033_config[i].clock = clock_lut_it9135[tmp]; |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 729 | |
| 730 | return 0; |
| 731 | |
| 732 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 733 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 734 | |
| 735 | return ret; |
| 736 | } |
| 737 | |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 738 | static int af9035_read_config(struct dvb_usb_device *d) |
| 739 | { |
| 740 | struct state *state = d_to_priv(d); |
| 741 | |
| 742 | if (state->chip_type == 0x9135) |
| 743 | return af9035_read_config_it9135(d); |
| 744 | else |
| 745 | return af9035_read_config_af9035(d); |
| 746 | } |
| 747 | |
Antti Palosaari | 51639be | 2012-09-16 22:26:56 -0300 | [diff] [blame] | 748 | static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d, |
| 749 | int cmd, int arg) |
| 750 | { |
| 751 | int ret; |
| 752 | u8 val; |
| 753 | |
| 754 | dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg); |
| 755 | |
| 756 | /* |
| 757 | * CEN always enabled by hardware wiring |
| 758 | * RESETN GPIOT3 |
| 759 | * RXEN GPIOT2 |
| 760 | */ |
| 761 | |
| 762 | switch (cmd) { |
| 763 | case TUA9001_CMD_RESETN: |
| 764 | if (arg) |
| 765 | val = 0x00; |
| 766 | else |
| 767 | val = 0x01; |
| 768 | |
| 769 | ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01); |
| 770 | if (ret < 0) |
| 771 | goto err; |
| 772 | break; |
| 773 | case TUA9001_CMD_RXEN: |
| 774 | if (arg) |
| 775 | val = 0x01; |
| 776 | else |
| 777 | val = 0x00; |
| 778 | |
| 779 | ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01); |
| 780 | if (ret < 0) |
| 781 | goto err; |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | return 0; |
| 786 | |
| 787 | err: |
| 788 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
| 789 | |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 794 | static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d, |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 795 | int cmd, int arg) |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 796 | { |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 797 | int ret; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 798 | |
| 799 | switch (cmd) { |
| 800 | case FC0011_FE_CALLBACK_POWER: |
| 801 | /* Tuner enable */ |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 802 | ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1); |
| 803 | if (ret < 0) |
| 804 | goto err; |
| 805 | |
| 806 | ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1); |
| 807 | if (ret < 0) |
| 808 | goto err; |
| 809 | |
| 810 | ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1); |
| 811 | if (ret < 0) |
| 812 | goto err; |
| 813 | |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 814 | /* LED */ |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 815 | ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1); |
| 816 | if (ret < 0) |
| 817 | goto err; |
| 818 | |
| 819 | ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1); |
| 820 | if (ret < 0) |
| 821 | goto err; |
| 822 | |
Michael Büsch | de2bec5 | 2012-04-03 05:11:30 -0300 | [diff] [blame] | 823 | usleep_range(10000, 50000); |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 824 | break; |
| 825 | case FC0011_FE_CALLBACK_RESET: |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 826 | ret = af9035_wr_reg(d, 0xd8e9, 1); |
| 827 | if (ret < 0) |
| 828 | goto err; |
| 829 | |
| 830 | ret = af9035_wr_reg(d, 0xd8e8, 1); |
| 831 | if (ret < 0) |
| 832 | goto err; |
| 833 | |
| 834 | ret = af9035_wr_reg(d, 0xd8e7, 1); |
| 835 | if (ret < 0) |
| 836 | goto err; |
| 837 | |
Michael Büsch | de2bec5 | 2012-04-03 05:11:30 -0300 | [diff] [blame] | 838 | usleep_range(10000, 20000); |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 839 | |
| 840 | ret = af9035_wr_reg(d, 0xd8e7, 0); |
| 841 | if (ret < 0) |
| 842 | goto err; |
| 843 | |
Michael Büsch | de2bec5 | 2012-04-03 05:11:30 -0300 | [diff] [blame] | 844 | usleep_range(10000, 20000); |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 845 | break; |
| 846 | default: |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 847 | ret = -EINVAL; |
| 848 | goto err; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | return 0; |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 852 | |
| 853 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 854 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 855 | |
| 856 | return ret; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg) |
| 860 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 861 | struct state *state = d_to_priv(d); |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 862 | |
| 863 | switch (state->af9033_config[0].tuner) { |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 864 | case AF9033_TUNER_FC0011: |
| 865 | return af9035_fc0011_tuner_callback(d, cmd, arg); |
Antti Palosaari | 51639be | 2012-09-16 22:26:56 -0300 | [diff] [blame] | 866 | case AF9033_TUNER_TUA9001: |
| 867 | return af9035_tua9001_tuner_callback(d, cmd, arg); |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 868 | default: |
| 869 | break; |
| 870 | } |
| 871 | |
Antti Palosaari | 1835af1 | 2012-09-11 22:27:06 -0300 | [diff] [blame] | 872 | return 0; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static int af9035_frontend_callback(void *adapter_priv, int component, |
| 876 | int cmd, int arg) |
| 877 | { |
| 878 | struct i2c_adapter *adap = adapter_priv; |
| 879 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
| 880 | |
Antti Palosaari | 1835af1 | 2012-09-11 22:27:06 -0300 | [diff] [blame] | 881 | dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n", |
| 882 | __func__, component, cmd, arg); |
| 883 | |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 884 | switch (component) { |
| 885 | case DVB_FRONTEND_COMPONENT_TUNER: |
| 886 | return af9035_tuner_callback(d, cmd, arg); |
| 887 | default: |
| 888 | break; |
| 889 | } |
| 890 | |
Antti Palosaari | 1835af1 | 2012-09-11 22:27:06 -0300 | [diff] [blame] | 891 | return 0; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 892 | } |
| 893 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 894 | static int af9035_get_adapter_count(struct dvb_usb_device *d) |
| 895 | { |
| 896 | struct state *state = d_to_priv(d); |
Antti Palosaari | bada342 | 2013-01-11 20:45:11 -0300 | [diff] [blame^] | 897 | |
| 898 | /* disable 2nd adapter as we don't have PID filters implemented */ |
| 899 | if (d->udev->speed == USB_SPEED_FULL) |
| 900 | return 1; |
| 901 | else |
| 902 | return state->dual_mode + 1; |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 903 | } |
| 904 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 905 | static int af9035_frontend_attach(struct dvb_usb_adapter *adap) |
| 906 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 907 | struct state *state = adap_to_priv(adap); |
| 908 | struct dvb_usb_device *d = adap_to_d(adap); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 909 | int ret; |
| 910 | |
Antti Palosaari | 1cbabf9 | 2012-05-07 14:59:55 -0300 | [diff] [blame] | 911 | if (!state->af9033_config[adap->id].tuner) { |
| 912 | /* unsupported tuner */ |
Antti Palosaari | 5a9abae | 2012-03-30 17:15:16 -0300 | [diff] [blame] | 913 | ret = -ENODEV; |
| 914 | goto err; |
| 915 | } |
| 916 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 917 | if (adap->id == 0) { |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 918 | state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB; |
| 919 | state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL; |
| 920 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 921 | ret = af9035_wr_reg(d, 0x00417f, |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 922 | state->af9033_config[1].i2c_addr); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 923 | if (ret < 0) |
| 924 | goto err; |
| 925 | |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 926 | ret = af9035_wr_reg(d, 0x00d81a, state->dual_mode); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 927 | if (ret < 0) |
| 928 | goto err; |
| 929 | } |
| 930 | |
| 931 | /* attach demodulator */ |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 932 | adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id], |
| 933 | &d->i2c_adap); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 934 | if (adap->fe[0] == NULL) { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 935 | ret = -ENODEV; |
| 936 | goto err; |
| 937 | } |
Antti Palosaari | 852f0d9 | 2012-04-06 07:09:23 -0300 | [diff] [blame] | 938 | |
| 939 | /* disable I2C-gate */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 940 | adap->fe[0]->ops.i2c_gate_ctrl = NULL; |
| 941 | adap->fe[0]->callback = af9035_frontend_callback; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 942 | |
| 943 | return 0; |
| 944 | |
| 945 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 946 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 947 | |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | static struct tua9001_config af9035_tua9001_config = { |
| 952 | .i2c_addr = 0x60, |
| 953 | }; |
| 954 | |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 955 | static const struct fc0011_config af9035_fc0011_config = { |
| 956 | .i2c_address = 0x60, |
| 957 | }; |
| 958 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 959 | static struct mxl5007t_config af9035_mxl5007t_config[] = { |
| 960 | { |
| 961 | .xtal_freq_hz = MxL_XTAL_24_MHZ, |
| 962 | .if_freq_hz = MxL_IF_4_57_MHZ, |
| 963 | .invert_if = 0, |
| 964 | .loop_thru_enable = 0, |
| 965 | .clk_out_enable = 0, |
| 966 | .clk_out_amp = MxL_CLKOUT_AMP_0_94V, |
| 967 | }, { |
| 968 | .xtal_freq_hz = MxL_XTAL_24_MHZ, |
| 969 | .if_freq_hz = MxL_IF_4_57_MHZ, |
| 970 | .invert_if = 0, |
| 971 | .loop_thru_enable = 1, |
| 972 | .clk_out_enable = 1, |
| 973 | .clk_out_amp = MxL_CLKOUT_AMP_0_94V, |
| 974 | } |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 975 | }; |
| 976 | |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 977 | static struct tda18218_config af9035_tda18218_config = { |
| 978 | .i2c_address = 0x60, |
| 979 | .i2c_wr_max = 21, |
| 980 | }; |
| 981 | |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame] | 982 | static const struct fc2580_config af9035_fc2580_config = { |
| 983 | .i2c_addr = 0x56, |
| 984 | .clock = 16384000, |
| 985 | }; |
| 986 | |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 987 | static const struct fc0012_config af9035_fc0012_config[] = { |
| 988 | { |
| 989 | .i2c_address = 0x63, |
| 990 | .xtal_freq = FC_XTAL_36_MHZ, |
Antti Palosaari | 3a98477 | 2012-12-09 12:33:04 -0300 | [diff] [blame] | 991 | .dual_master = true, |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 992 | .loop_through = true, |
| 993 | .clock_out = true, |
| 994 | }, { |
| 995 | .i2c_address = 0x63 | 0x80, /* I2C bus select hack */ |
| 996 | .xtal_freq = FC_XTAL_36_MHZ, |
Antti Palosaari | 3a98477 | 2012-12-09 12:33:04 -0300 | [diff] [blame] | 997 | .dual_master = true, |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 998 | } |
Antti Palosaari | ad3a758 | 2012-12-08 23:27:49 -0300 | [diff] [blame] | 999 | }; |
| 1000 | |
Antti Palosaari | ac77fb0 | 2013-01-07 09:51:13 -0300 | [diff] [blame] | 1001 | static struct ite_config af9035_it913x_config = { |
| 1002 | .chip_ver = 0x01, |
| 1003 | .chip_type = 0x9135, |
| 1004 | .firmware = 0x00000000, |
| 1005 | .firmware_ver = 1, |
| 1006 | .adc_x2 = 1, |
| 1007 | .tuner_id_0 = AF9033_TUNER_IT9135_38, |
| 1008 | .tuner_id_1 = 0x00, |
| 1009 | .dual_mode = 0x00, |
| 1010 | .adf = 0x00, |
| 1011 | /* option to read SIGNAL_LEVEL */ |
| 1012 | .read_slevel = 0, |
| 1013 | }; |
| 1014 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1015 | static int af9035_tuner_attach(struct dvb_usb_adapter *adap) |
| 1016 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1017 | struct state *state = adap_to_priv(adap); |
| 1018 | struct dvb_usb_device *d = adap_to_d(adap); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1019 | int ret; |
| 1020 | struct dvb_frontend *fe; |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 1021 | struct i2c_msg msg[1]; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1022 | u8 tuner_addr; |
| 1023 | /* |
| 1024 | * XXX: Hack used in that function: we abuse unused I2C address bit [7] |
| 1025 | * to carry info about used I2C bus for dual tuner configuration. |
| 1026 | */ |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1027 | |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 1028 | switch (state->af9033_config[adap->id].tuner) { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1029 | case AF9033_TUNER_TUA9001: |
| 1030 | /* AF9035 gpiot3 = TUA9001 RESETN |
| 1031 | AF9035 gpiot2 = TUA9001 RXEN */ |
| 1032 | |
| 1033 | /* configure gpiot2 and gpiot2 as output */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1034 | ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1035 | if (ret < 0) |
| 1036 | goto err; |
| 1037 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1038 | ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1039 | if (ret < 0) |
| 1040 | goto err; |
| 1041 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1042 | ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1043 | if (ret < 0) |
| 1044 | goto err; |
| 1045 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1046 | ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1047 | if (ret < 0) |
| 1048 | goto err; |
| 1049 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1050 | /* attach tuner */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1051 | fe = dvb_attach(tua9001_attach, adap->fe[0], |
| 1052 | &d->i2c_adap, &af9035_tua9001_config); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1053 | break; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 1054 | case AF9033_TUNER_FC0011: |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1055 | fe = dvb_attach(fc0011_attach, adap->fe[0], |
| 1056 | &d->i2c_adap, &af9035_fc0011_config); |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 1057 | break; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 1058 | case AF9033_TUNER_MXL5007T: |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1059 | if (adap->id == 0) { |
| 1060 | ret = af9035_wr_reg(d, 0x00d8e0, 1); |
| 1061 | if (ret < 0) |
| 1062 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1063 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1064 | ret = af9035_wr_reg(d, 0x00d8e1, 1); |
| 1065 | if (ret < 0) |
| 1066 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1067 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1068 | ret = af9035_wr_reg(d, 0x00d8df, 0); |
| 1069 | if (ret < 0) |
| 1070 | goto err; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 1071 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1072 | msleep(30); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 1073 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1074 | ret = af9035_wr_reg(d, 0x00d8df, 1); |
| 1075 | if (ret < 0) |
| 1076 | goto err; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 1077 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1078 | msleep(300); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 1079 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1080 | ret = af9035_wr_reg(d, 0x00d8c0, 1); |
| 1081 | if (ret < 0) |
| 1082 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1083 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1084 | ret = af9035_wr_reg(d, 0x00d8c1, 1); |
| 1085 | if (ret < 0) |
| 1086 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1087 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1088 | ret = af9035_wr_reg(d, 0x00d8bf, 0); |
| 1089 | if (ret < 0) |
| 1090 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1091 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1092 | ret = af9035_wr_reg(d, 0x00d8b4, 1); |
| 1093 | if (ret < 0) |
| 1094 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1095 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1096 | ret = af9035_wr_reg(d, 0x00d8b5, 1); |
| 1097 | if (ret < 0) |
| 1098 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1099 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1100 | ret = af9035_wr_reg(d, 0x00d8b3, 1); |
| 1101 | if (ret < 0) |
| 1102 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1103 | |
| 1104 | tuner_addr = 0x60; |
| 1105 | } else { |
| 1106 | tuner_addr = 0x60 | 0x80; /* I2C bus hack */ |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1107 | } |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 1108 | |
| 1109 | /* attach tuner */ |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 1110 | fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap, |
| 1111 | tuner_addr, &af9035_mxl5007t_config[adap->id]); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 1112 | break; |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 1113 | case AF9033_TUNER_TDA18218: |
| 1114 | /* attach tuner */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1115 | fe = dvb_attach(tda18218_attach, adap->fe[0], |
| 1116 | &d->i2c_adap, &af9035_tda18218_config); |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 1117 | break; |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame] | 1118 | case AF9033_TUNER_FC2580: |
| 1119 | /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */ |
| 1120 | ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01); |
| 1121 | if (ret < 0) |
| 1122 | goto err; |
| 1123 | |
| 1124 | ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01); |
| 1125 | if (ret < 0) |
| 1126 | goto err; |
| 1127 | |
| 1128 | ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01); |
| 1129 | if (ret < 0) |
| 1130 | goto err; |
| 1131 | |
| 1132 | usleep_range(10000, 50000); |
| 1133 | /* attach tuner */ |
| 1134 | fe = dvb_attach(fc2580_attach, adap->fe[0], |
| 1135 | &d->i2c_adap, &af9035_fc2580_config); |
| 1136 | break; |
Antti Palosaari | 7e0bc29 | 2012-12-02 20:12:29 -0300 | [diff] [blame] | 1137 | case AF9033_TUNER_FC0012: |
| 1138 | /* |
| 1139 | * AF9035 gpiot2 = FC0012 enable |
| 1140 | * XXX: there seems to be something on gpioh8 too, but on my |
| 1141 | * my test I didn't find any difference. |
| 1142 | */ |
| 1143 | |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 1144 | if (adap->id == 0) { |
| 1145 | /* configure gpiot2 as output and high */ |
| 1146 | ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01); |
| 1147 | if (ret < 0) |
| 1148 | goto err; |
Antti Palosaari | 7e0bc29 | 2012-12-02 20:12:29 -0300 | [diff] [blame] | 1149 | |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 1150 | ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01); |
| 1151 | if (ret < 0) |
| 1152 | goto err; |
Antti Palosaari | 7e0bc29 | 2012-12-02 20:12:29 -0300 | [diff] [blame] | 1153 | |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 1154 | ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01); |
| 1155 | if (ret < 0) |
| 1156 | goto err; |
| 1157 | } else { |
| 1158 | /* |
| 1159 | * FIXME: That belongs for the FC0012 driver. |
| 1160 | * Write 02 to FC0012 master tuner register 0d directly |
| 1161 | * in order to make slave tuner working. |
| 1162 | */ |
| 1163 | msg[0].addr = 0x63; |
| 1164 | msg[0].flags = 0; |
| 1165 | msg[0].len = 2; |
| 1166 | msg[0].buf = "\x0d\x02"; |
| 1167 | ret = i2c_transfer(&d->i2c_adap, msg, 1); |
| 1168 | if (ret < 0) |
| 1169 | goto err; |
| 1170 | } |
Antti Palosaari | 7e0bc29 | 2012-12-02 20:12:29 -0300 | [diff] [blame] | 1171 | |
| 1172 | usleep_range(10000, 50000); |
| 1173 | |
Antti Palosaari | ad3a758 | 2012-12-08 23:27:49 -0300 | [diff] [blame] | 1174 | fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap, |
Antti Palosaari | 0bb3d8a | 2012-12-09 12:01:41 -0300 | [diff] [blame] | 1175 | &af9035_fc0012_config[adap->id]); |
Antti Palosaari | 7e0bc29 | 2012-12-02 20:12:29 -0300 | [diff] [blame] | 1176 | break; |
Antti Palosaari | ac77fb0 | 2013-01-07 09:51:13 -0300 | [diff] [blame] | 1177 | case AF9033_TUNER_IT9135_38: |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 1178 | case AF9033_TUNER_IT9135_51: |
| 1179 | case AF9033_TUNER_IT9135_52: |
| 1180 | case AF9033_TUNER_IT9135_60: |
| 1181 | case AF9033_TUNER_IT9135_61: |
| 1182 | case AF9033_TUNER_IT9135_62: |
Antti Palosaari | ac77fb0 | 2013-01-07 09:51:13 -0300 | [diff] [blame] | 1183 | /* attach tuner */ |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 1184 | af9035_it913x_config.tuner_id_0 = state->af9033_config[0].tuner; |
Antti Palosaari | ac77fb0 | 2013-01-07 09:51:13 -0300 | [diff] [blame] | 1185 | fe = dvb_attach(it913x_attach, adap->fe[0], |
| 1186 | &d->i2c_adap, 0x38, &af9035_it913x_config); |
| 1187 | break; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1188 | default: |
| 1189 | fe = NULL; |
| 1190 | } |
| 1191 | |
| 1192 | if (fe == NULL) { |
| 1193 | ret = -ENODEV; |
| 1194 | goto err; |
| 1195 | } |
| 1196 | |
| 1197 | return 0; |
| 1198 | |
| 1199 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1200 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1201 | |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1205 | static int af9035_init(struct dvb_usb_device *d) |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1206 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1207 | struct state *state = d_to_priv(d); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1208 | int ret, i; |
Antti Palosaari | bada342 | 2013-01-11 20:45:11 -0300 | [diff] [blame^] | 1209 | u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4; |
| 1210 | u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1211 | struct reg_val_mask tab[] = { |
| 1212 | { 0x80f99d, 0x01, 0x01 }, |
| 1213 | { 0x80f9a4, 0x01, 0x01 }, |
| 1214 | { 0x00dd11, 0x00, 0x20 }, |
| 1215 | { 0x00dd11, 0x00, 0x40 }, |
| 1216 | { 0x00dd13, 0x00, 0x20 }, |
| 1217 | { 0x00dd13, 0x00, 0x40 }, |
| 1218 | { 0x00dd11, 0x20, 0x20 }, |
| 1219 | { 0x00dd88, (frame_size >> 0) & 0xff, 0xff}, |
| 1220 | { 0x00dd89, (frame_size >> 8) & 0xff, 0xff}, |
| 1221 | { 0x00dd0c, packet_size, 0xff}, |
| 1222 | { 0x00dd11, state->dual_mode << 6, 0x40 }, |
| 1223 | { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff}, |
| 1224 | { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff}, |
| 1225 | { 0x00dd0d, packet_size, 0xff }, |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1226 | { 0x80f9a3, state->dual_mode, 0x01 }, |
| 1227 | { 0x80f9cd, state->dual_mode, 0x01 }, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1228 | { 0x80f99d, 0x00, 0x01 }, |
| 1229 | { 0x80f9a4, 0x00, 0x01 }, |
| 1230 | }; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1231 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1232 | dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \ |
| 1233 | "packet_size=%02x\n", __func__, |
| 1234 | d->udev->speed, frame_size, packet_size); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1235 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1236 | /* init endpoints */ |
| 1237 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 1238 | ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val, |
| 1239 | tab[i].mask); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1240 | if (ret < 0) |
| 1241 | goto err; |
| 1242 | } |
| 1243 | |
| 1244 | return 0; |
| 1245 | |
| 1246 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1247 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1248 | |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
Antti Palosaari | 37b44a0 | 2013-01-04 15:21:26 -0300 | [diff] [blame] | 1252 | #if IS_ENABLED(CONFIG_RC_CORE) |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1253 | static int af9035_rc_query(struct dvb_usb_device *d) |
| 1254 | { |
| 1255 | unsigned int key; |
| 1256 | unsigned char b[4]; |
| 1257 | int ret; |
| 1258 | struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b }; |
| 1259 | |
| 1260 | ret = af9035_ctrl_msg(d, &req); |
| 1261 | if (ret < 0) |
| 1262 | goto err; |
| 1263 | |
| 1264 | if ((b[2] + b[3]) == 0xff) { |
| 1265 | if ((b[0] + b[1]) == 0xff) { |
| 1266 | /* NEC */ |
| 1267 | key = b[0] << 8 | b[2]; |
| 1268 | } else { |
| 1269 | /* ext. NEC */ |
| 1270 | key = b[0] << 16 | b[1] << 8 | b[2]; |
| 1271 | } |
| 1272 | } else { |
| 1273 | key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]; |
| 1274 | } |
| 1275 | |
| 1276 | rc_keydown(d->rc_dev, key, 0); |
| 1277 | |
| 1278 | err: |
| 1279 | /* ignore errors */ |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc) |
| 1284 | { |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 1285 | struct state *state = d_to_priv(d); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1286 | int ret; |
| 1287 | u8 tmp; |
| 1288 | |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 1289 | /* TODO: IT9135 remote control support */ |
| 1290 | if (state->chip_type == 0x9135) |
| 1291 | return 0; |
| 1292 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1293 | ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp); |
| 1294 | if (ret < 0) |
| 1295 | goto err; |
| 1296 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1297 | dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1298 | |
| 1299 | /* don't activate rc if in HID mode or if not available */ |
| 1300 | if (tmp == 5) { |
| 1301 | ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp); |
| 1302 | if (ret < 0) |
| 1303 | goto err; |
| 1304 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1305 | dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1306 | |
| 1307 | switch (tmp) { |
| 1308 | case 0: /* NEC */ |
| 1309 | default: |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 1310 | rc->allowed_protos = RC_BIT_NEC; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1311 | break; |
| 1312 | case 1: /* RC6 */ |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 1313 | rc->allowed_protos = RC_BIT_RC6_MCE; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1314 | break; |
| 1315 | } |
| 1316 | |
| 1317 | rc->query = af9035_rc_query; |
| 1318 | rc->interval = 500; |
Antti Palosaari | de73bee | 2012-07-05 19:57:07 -0300 | [diff] [blame] | 1319 | |
| 1320 | /* load empty to enable rc */ |
| 1321 | if (!rc->map_name) |
| 1322 | rc->map_name = RC_MAP_EMPTY; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | return 0; |
| 1326 | |
| 1327 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1328 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1329 | |
| 1330 | return ret; |
| 1331 | } |
Antti Palosaari | eed5670 | 2012-12-09 20:18:31 -0300 | [diff] [blame] | 1332 | #else |
| 1333 | #define af9035_get_rc_config NULL |
| 1334 | #endif |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1335 | |
Antti Palosaari | bada342 | 2013-01-11 20:45:11 -0300 | [diff] [blame^] | 1336 | static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type, |
| 1337 | struct usb_data_stream_properties *stream) |
| 1338 | { |
| 1339 | struct dvb_usb_device *d = fe_to_d(fe); |
| 1340 | dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id); |
| 1341 | |
| 1342 | if (d->udev->speed == USB_SPEED_FULL) |
| 1343 | stream->u.bulk.buffersize = 5 * 188; |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * FIXME: PID filter is property of demodulator and should be moved to the |
| 1350 | * correct driver. Also we support only adapter #0 PID filter and will |
| 1351 | * disable adapter #1 if USB1.1 is used. |
| 1352 | */ |
| 1353 | static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff) |
| 1354 | { |
| 1355 | struct dvb_usb_device *d = adap_to_d(adap); |
| 1356 | int ret; |
| 1357 | |
| 1358 | dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff); |
| 1359 | |
| 1360 | ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01); |
| 1361 | if (ret < 0) |
| 1362 | goto err; |
| 1363 | |
| 1364 | return 0; |
| 1365 | |
| 1366 | err: |
| 1367 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
| 1368 | |
| 1369 | return ret; |
| 1370 | } |
| 1371 | |
| 1372 | static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, |
| 1373 | int onoff) |
| 1374 | { |
| 1375 | struct dvb_usb_device *d = adap_to_d(adap); |
| 1376 | int ret; |
| 1377 | u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff}; |
| 1378 | |
| 1379 | dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n", |
| 1380 | __func__, index, pid, onoff); |
| 1381 | |
| 1382 | ret = af9035_wr_regs(d, 0x80f996, wbuf, 2); |
| 1383 | if (ret < 0) |
| 1384 | goto err; |
| 1385 | |
| 1386 | ret = af9035_wr_reg(d, 0x80f994, onoff); |
| 1387 | if (ret < 0) |
| 1388 | goto err; |
| 1389 | |
| 1390 | ret = af9035_wr_reg(d, 0x80f995, index); |
| 1391 | if (ret < 0) |
| 1392 | goto err; |
| 1393 | |
| 1394 | return 0; |
| 1395 | |
| 1396 | err: |
| 1397 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
| 1398 | |
| 1399 | return ret; |
| 1400 | } |
| 1401 | |
Antti Palosaari | b799b81 | 2013-01-11 16:22:07 -0300 | [diff] [blame] | 1402 | static int af9035_probe(struct usb_interface *intf, |
| 1403 | const struct usb_device_id *id) |
| 1404 | { |
| 1405 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1406 | char manufacturer[sizeof("Afatech")]; |
| 1407 | |
| 1408 | memset(manufacturer, 0, sizeof(manufacturer)); |
| 1409 | usb_string(udev, udev->descriptor.iManufacturer, |
| 1410 | manufacturer, sizeof(manufacturer)); |
| 1411 | /* |
| 1412 | * There is two devices having same ID but different chipset. One uses |
| 1413 | * AF9015 and the other IT9135 chipset. Only difference seen on lsusb |
| 1414 | * is iManufacturer string. |
| 1415 | * |
| 1416 | * idVendor 0x0ccd TerraTec Electronic GmbH |
| 1417 | * idProduct 0x0099 |
| 1418 | * bcdDevice 2.00 |
| 1419 | * iManufacturer 1 Afatech |
| 1420 | * iProduct 2 DVB-T 2 |
| 1421 | * |
| 1422 | * idVendor 0x0ccd TerraTec Electronic GmbH |
| 1423 | * idProduct 0x0099 |
| 1424 | * bcdDevice 2.00 |
| 1425 | * iManufacturer 1 ITE Technologies, Inc. |
| 1426 | * iProduct 2 DVB-T TV Stick |
| 1427 | */ |
| 1428 | if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) && |
| 1429 | (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) { |
| 1430 | if (!strcmp("Afatech", manufacturer)) { |
| 1431 | dev_dbg(&udev->dev, "%s: rejecting device\n", __func__); |
| 1432 | return -ENODEV; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | return dvb_usbv2_probe(intf, id); |
| 1437 | } |
| 1438 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1439 | /* interface 0 is used by DVB-T receiver and |
| 1440 | interface 1 is for remote controller (HID) */ |
| 1441 | static const struct dvb_usb_device_properties af9035_props = { |
| 1442 | .driver_name = KBUILD_MODNAME, |
| 1443 | .owner = THIS_MODULE, |
| 1444 | .adapter_nr = adapter_nr, |
| 1445 | .size_of_priv = sizeof(struct state), |
| 1446 | |
| 1447 | .generic_bulk_ctrl_endpoint = 0x02, |
| 1448 | .generic_bulk_ctrl_endpoint_response = 0x81, |
| 1449 | |
| 1450 | .identify_state = af9035_identify_state, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1451 | .download_firmware = af9035_download_firmware, |
| 1452 | |
| 1453 | .i2c_algo = &af9035_i2c_algo, |
| 1454 | .read_config = af9035_read_config, |
| 1455 | .frontend_attach = af9035_frontend_attach, |
| 1456 | .tuner_attach = af9035_tuner_attach, |
| 1457 | .init = af9035_init, |
| 1458 | .get_rc_config = af9035_get_rc_config, |
Antti Palosaari | bada342 | 2013-01-11 20:45:11 -0300 | [diff] [blame^] | 1459 | .get_stream_config = af9035_get_stream_config, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1460 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 1461 | .get_adapter_count = af9035_get_adapter_count, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1462 | .adapter = { |
| 1463 | { |
Antti Palosaari | bada342 | 2013-01-11 20:45:11 -0300 | [diff] [blame^] | 1464 | .caps = DVB_USB_ADAP_HAS_PID_FILTER | |
| 1465 | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, |
| 1466 | |
| 1467 | .pid_filter_count = 32, |
| 1468 | .pid_filter_ctrl = af9035_pid_filter_ctrl, |
| 1469 | .pid_filter = af9035_pid_filter, |
| 1470 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1471 | .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188), |
| 1472 | }, { |
| 1473 | .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188), |
| 1474 | }, |
| 1475 | }, |
| 1476 | }; |
| 1477 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1478 | static const struct usb_device_id af9035_id_table[] = { |
| 1479 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035, |
| 1480 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1481 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000, |
| 1482 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1483 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001, |
| 1484 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1485 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002, |
| 1486 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1487 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003, |
| 1488 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1489 | { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK, |
| 1490 | &af9035_props, "TerraTec Cinergy T Stick", NULL) }, |
| 1491 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835, |
| 1492 | &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) }, |
| 1493 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835, |
| 1494 | &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) }, |
| 1495 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867, |
| 1496 | &af9035_props, "AVerMedia HD Volar (A867)", NULL) }, |
| 1497 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867, |
| 1498 | &af9035_props, "AVerMedia HD Volar (A867)", NULL) }, |
| 1499 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR, |
| 1500 | &af9035_props, "AVerMedia Twinstar (A825)", NULL) }, |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame] | 1501 | { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS, |
| 1502 | &af9035_props, "Asus U3100Mini Plus", NULL) }, |
Fabrizio Gazzato | d9b7595 | 2013-02-17 18:25:34 -0300 | [diff] [blame] | 1503 | { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa, |
| 1504 | &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) }, |
Antti Palosaari | b799b81 | 2013-01-11 16:22:07 -0300 | [diff] [blame] | 1505 | /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */ |
| 1506 | { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099, |
| 1507 | &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) }, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1508 | { } |
| 1509 | }; |
| 1510 | MODULE_DEVICE_TABLE(usb, af9035_id_table); |
| 1511 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1512 | static struct usb_driver af9035_usb_driver = { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1513 | .name = KBUILD_MODNAME, |
| 1514 | .id_table = af9035_id_table, |
Antti Palosaari | b799b81 | 2013-01-11 16:22:07 -0300 | [diff] [blame] | 1515 | .probe = af9035_probe, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1516 | .disconnect = dvb_usbv2_disconnect, |
| 1517 | .suspend = dvb_usbv2_suspend, |
| 1518 | .resume = dvb_usbv2_resume, |
Antti Palosaari | 04966aa | 2012-08-14 22:21:08 -0300 | [diff] [blame] | 1519 | .reset_resume = dvb_usbv2_reset_resume, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1520 | .no_dynamic_id = 1, |
| 1521 | .soft_unbind = 1, |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1522 | }; |
| 1523 | |
Gianluca Gennari | 48bf7e1 | 2012-04-02 17:25:17 -0300 | [diff] [blame] | 1524 | module_usb_driver(af9035_usb_driver); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1525 | |
| 1526 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1527 | MODULE_DESCRIPTION("Afatech AF9035 driver"); |
| 1528 | MODULE_LICENSE("GPL"); |
Antti Palosaari | 4395e4b | 2012-09-12 12:19:06 -0300 | [diff] [blame] | 1529 | MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035); |
Antti Palosaari | 74c1883 | 2013-01-07 15:16:54 -0300 | [diff] [blame] | 1530 | MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1); |
| 1531 | MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2); |