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 | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 44 | #define BUF_LEN 64 |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 45 | #define REQ_HDR_LEN 4 /* send header size */ |
| 46 | #define ACK_HDR_LEN 3 /* rece header size */ |
| 47 | #define CHECKSUM_LEN 2 |
| 48 | #define USB_TIMEOUT 2000 |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 49 | struct state *state = d_to_priv(d); |
| 50 | int ret, wlen, rlen; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 51 | u8 buf[BUF_LEN]; |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 52 | u16 checksum, tmp_checksum; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 53 | |
| 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 | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 59 | return -EINVAL; |
| 60 | } |
| 61 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 62 | buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1; |
| 63 | buf[1] = req->mbox; |
| 64 | buf[2] = req->cmd; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 65 | buf[3] = state->seq++; |
| 66 | memcpy(&buf[REQ_HDR_LEN], req->wbuf, req->wlen); |
| 67 | |
| 68 | wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN; |
| 69 | rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 70 | |
| 71 | /* calc and add checksum */ |
Michael Büsch | b1a9599 | 2012-04-01 16:33:48 -0300 | [diff] [blame] | 72 | checksum = af9035_checksum(buf, buf[0] - 1); |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 73 | buf[buf[0] - 1] = (checksum >> 8); |
| 74 | buf[buf[0] - 0] = (checksum & 0xff); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 75 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 76 | /* no ack for these packets */ |
| 77 | if (req->cmd == CMD_FW_DL) |
| 78 | rlen = 0; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 79 | |
Antti Palosaari | 1162c7b | 2012-06-20 20:27:42 -0300 | [diff] [blame] | 80 | ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 81 | if (ret) |
| 82 | goto err; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 83 | |
| 84 | /* no ack for those packets */ |
| 85 | if (req->cmd == CMD_FW_DL) |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 86 | goto exit; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 87 | |
Michael Büsch | b1a9599 | 2012-04-01 16:33:48 -0300 | [diff] [blame] | 88 | /* verify checksum */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 89 | checksum = af9035_checksum(buf, rlen - 2); |
| 90 | tmp_checksum = (buf[rlen - 2] << 8) | buf[rlen - 1]; |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 91 | if (tmp_checksum != checksum) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 92 | dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \ |
| 93 | "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd, |
| 94 | tmp_checksum, checksum); |
Michael Büsch | b1a9599 | 2012-04-01 16:33:48 -0300 | [diff] [blame] | 95 | ret = -EIO; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 96 | goto err; |
Michael Büsch | b1a9599 | 2012-04-01 16:33:48 -0300 | [diff] [blame] | 97 | } |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 98 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 99 | /* check status */ |
| 100 | if (buf[2]) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 101 | dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n", |
| 102 | __func__, req->cmd, buf[2]); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 103 | ret = -EIO; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 104 | goto err; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* read request, copy returned data to return buf */ |
| 108 | if (req->rlen) |
| 109 | memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen); |
| 110 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 111 | exit: |
| 112 | return 0; |
| 113 | |
| 114 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 115 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 116 | |
| 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; |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 212 | } else if (msg[0].addr == state->af9033_config[0].i2c_addr) { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 213 | /* integrated demod */ |
| 214 | u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | |
| 215 | msg[0].buf[2]; |
| 216 | ret = af9035_rd_regs(d, reg, &msg[1].buf[0], |
| 217 | msg[1].len); |
| 218 | } else { |
| 219 | /* I2C */ |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 220 | u8 buf[5 + msg[0].len]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 221 | struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf), |
| 222 | buf, msg[1].len, msg[1].buf }; |
Hans-Frieder Vogt | 812fe6d | 2012-04-01 14:11:29 -0300 | [diff] [blame] | 223 | buf[0] = msg[1].len; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 224 | buf[1] = msg[0].addr << 1; |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 225 | buf[2] = 0x00; /* reg addr len */ |
| 226 | buf[3] = 0x00; /* reg addr MSB */ |
| 227 | buf[4] = 0x00; /* reg addr LSB */ |
| 228 | memcpy(&buf[5], msg[0].buf, msg[0].len); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 229 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 230 | } |
| 231 | } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { |
| 232 | if (msg[0].len > 40) { |
| 233 | /* TODO: correct limits > 40 */ |
| 234 | ret = -EOPNOTSUPP; |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 235 | } else if (msg[0].addr == state->af9033_config[0].i2c_addr) { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 236 | /* integrated demod */ |
| 237 | u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | |
| 238 | msg[0].buf[2]; |
| 239 | ret = af9035_wr_regs(d, reg, &msg[0].buf[3], |
| 240 | msg[0].len - 3); |
| 241 | } else { |
| 242 | /* I2C */ |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 243 | u8 buf[5 + msg[0].len]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 244 | struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf, |
| 245 | 0, NULL }; |
| 246 | buf[0] = msg[0].len; |
| 247 | buf[1] = msg[0].addr << 1; |
Antti Palosaari | ad30e91 | 2012-04-02 20:18:59 -0300 | [diff] [blame] | 248 | buf[2] = 0x00; /* reg addr len */ |
| 249 | buf[3] = 0x00; /* reg addr MSB */ |
| 250 | buf[4] = 0x00; /* reg addr LSB */ |
| 251 | memcpy(&buf[5], msg[0].buf, msg[0].len); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 252 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 253 | } |
| 254 | } else { |
| 255 | /* |
| 256 | * We support only two kind of I2C transactions: |
| 257 | * 1) 1 x read + 1 x write |
| 258 | * 2) 1 x write |
| 259 | */ |
| 260 | ret = -EOPNOTSUPP; |
| 261 | } |
| 262 | |
| 263 | mutex_unlock(&d->i2c_mutex); |
| 264 | |
| 265 | if (ret < 0) |
| 266 | return ret; |
| 267 | else |
| 268 | return num; |
| 269 | } |
| 270 | |
| 271 | static u32 af9035_i2c_functionality(struct i2c_adapter *adapter) |
| 272 | { |
| 273 | return I2C_FUNC_I2C; |
| 274 | } |
| 275 | |
| 276 | static struct i2c_algorithm af9035_i2c_algo = { |
| 277 | .master_xfer = af9035_i2c_master_xfer, |
| 278 | .functionality = af9035_i2c_functionality, |
| 279 | }; |
| 280 | |
Antti Palosaari | a0921af | 2012-06-18 23:42:53 -0300 | [diff] [blame] | 281 | 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] | 282 | { |
| 283 | int ret; |
| 284 | u8 wbuf[1] = { 1 }; |
| 285 | u8 rbuf[4]; |
| 286 | struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf, |
| 287 | sizeof(rbuf), rbuf }; |
| 288 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 289 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 290 | if (ret < 0) |
| 291 | goto err; |
| 292 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 293 | dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 294 | if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3]) |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 295 | ret = WARM; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 296 | else |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 297 | ret = COLD; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 298 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 299 | return ret; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 300 | |
| 301 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 302 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 307 | static int af9035_download_firmware(struct dvb_usb_device *d, |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 308 | const struct firmware *fw) |
| 309 | { |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 310 | int ret, i, j, len; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 311 | u8 wbuf[1]; |
| 312 | u8 rbuf[4]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 313 | struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; |
| 314 | struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL }; |
| 315 | struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 316 | u8 hdr_core; |
| 317 | u16 hdr_addr, hdr_data_len, hdr_checksum; |
Antti Palosaari | 6fb39c5 | 2012-04-06 16:32:30 -0300 | [diff] [blame] | 318 | #define MAX_DATA 58 |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 319 | #define HDR_SIZE 7 |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 320 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 321 | /* |
| 322 | * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info! |
| 323 | * |
| 324 | * byte 0: MCS 51 core |
| 325 | * There are two inside the AF9035 (1=Link and 2=OFDM) with separate |
| 326 | * address spaces |
| 327 | * byte 1-2: Big endian destination address |
| 328 | * byte 3-4: Big endian number of data bytes following the header |
| 329 | * byte 5-6: Big endian header checksum, apparently ignored by the chip |
| 330 | * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256) |
| 331 | */ |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 332 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 333 | for (i = fw->size; i > HDR_SIZE;) { |
| 334 | hdr_core = fw->data[fw->size - i + 0]; |
| 335 | hdr_addr = fw->data[fw->size - i + 1] << 8; |
| 336 | hdr_addr |= fw->data[fw->size - i + 2] << 0; |
| 337 | hdr_data_len = fw->data[fw->size - i + 3] << 8; |
| 338 | hdr_data_len |= fw->data[fw->size - i + 4] << 0; |
| 339 | hdr_checksum = fw->data[fw->size - i + 5] << 8; |
| 340 | hdr_checksum |= fw->data[fw->size - i + 6] << 0; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 341 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 342 | dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \ |
| 343 | "checksum=%04x\n", __func__, hdr_core, hdr_addr, |
| 344 | hdr_data_len, hdr_checksum); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 345 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 346 | if (((hdr_core != 1) && (hdr_core != 2)) || |
| 347 | (hdr_data_len > i)) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 348 | dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__); |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 349 | break; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 350 | } |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 351 | |
| 352 | /* download begin packet */ |
| 353 | req.cmd = CMD_FW_DL_BEGIN; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 354 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 41d44a8 | 2012-04-01 11:06:23 -0300 | [diff] [blame] | 355 | if (ret < 0) |
| 356 | goto err; |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 357 | |
| 358 | /* download firmware packet(s) */ |
| 359 | for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) { |
| 360 | len = j; |
| 361 | if (len > MAX_DATA) |
| 362 | len = MAX_DATA; |
| 363 | req_fw_dl.wlen = len; |
| 364 | req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i + |
| 365 | HDR_SIZE + hdr_data_len - j]; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 366 | ret = af9035_ctrl_msg(d, &req_fw_dl); |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 367 | if (ret < 0) |
| 368 | goto err; |
| 369 | } |
| 370 | |
| 371 | /* download end packet */ |
| 372 | req.cmd = CMD_FW_DL_END; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 373 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 374 | if (ret < 0) |
| 375 | goto err; |
| 376 | |
| 377 | i -= hdr_data_len + HDR_SIZE; |
| 378 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 379 | dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n", |
| 380 | __func__, fw->size - i); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* firmware loaded, request boot */ |
| 384 | req.cmd = CMD_FW_BOOT; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 385 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 386 | if (ret < 0) |
| 387 | goto err; |
| 388 | |
| 389 | /* ensure firmware starts */ |
| 390 | wbuf[0] = 1; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 391 | ret = af9035_ctrl_msg(d, &req_fw_ver); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 392 | if (ret < 0) |
| 393 | goto err; |
| 394 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 395 | if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 396 | dev_err(&d->udev->dev, "%s: firmware did not run\n", |
| 397 | KBUILD_MODNAME); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 398 | ret = -ENODEV; |
| 399 | goto err; |
| 400 | } |
| 401 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 402 | dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d", |
| 403 | KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]); |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 404 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 405 | return 0; |
| 406 | |
| 407 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 408 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 413 | static int af9035_download_firmware_it9135(struct dvb_usb_device *d, |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 414 | const struct firmware *fw) |
| 415 | { |
| 416 | int ret, i, i_prev; |
| 417 | u8 wbuf[1]; |
| 418 | u8 rbuf[4]; |
| 419 | struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; |
| 420 | struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL }; |
| 421 | struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; |
| 422 | #define HDR_SIZE 7 |
| 423 | |
| 424 | /* |
| 425 | * There seems to be following firmware header. Meaning of bytes 0-3 |
| 426 | * is unknown. |
| 427 | * |
| 428 | * 0: 3 |
| 429 | * 1: 0, 1 |
| 430 | * 2: 0 |
| 431 | * 3: 1, 2, 3 |
| 432 | * 4: addr MSB |
| 433 | * 5: addr LSB |
| 434 | * 6: count of data bytes ? |
| 435 | */ |
| 436 | |
| 437 | for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) { |
| 438 | if (i == fw->size || |
| 439 | (fw->data[i + 0] == 0x03 && |
| 440 | (fw->data[i + 1] == 0x00 || |
| 441 | fw->data[i + 1] == 0x01) && |
| 442 | fw->data[i + 2] == 0x00)) { |
| 443 | req_fw_dl.wlen = i - i_prev; |
| 444 | req_fw_dl.wbuf = (u8 *) &fw->data[i_prev]; |
| 445 | i_prev = i; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 446 | ret = af9035_ctrl_msg(d, &req_fw_dl); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 447 | if (ret < 0) |
| 448 | goto err; |
| 449 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 450 | dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n", |
| 451 | __func__, i); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | /* firmware loaded, request boot */ |
| 456 | req.cmd = CMD_FW_BOOT; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 457 | ret = af9035_ctrl_msg(d, &req); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 458 | if (ret < 0) |
| 459 | goto err; |
| 460 | |
| 461 | /* ensure firmware starts */ |
| 462 | wbuf[0] = 1; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 463 | ret = af9035_ctrl_msg(d, &req_fw_ver); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 464 | if (ret < 0) |
| 465 | goto err; |
| 466 | |
| 467 | if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 468 | dev_err(&d->udev->dev, "%s: firmware did not run\n", |
| 469 | KBUILD_MODNAME); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 470 | ret = -ENODEV; |
| 471 | goto err; |
| 472 | } |
| 473 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 474 | dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d", |
| 475 | KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | |
| 479 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 480 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 481 | |
| 482 | return ret; |
| 483 | } |
| 484 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 485 | static int af9035_read_config(struct dvb_usb_device *d) |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 486 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 487 | struct state *state = d_to_priv(d); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 488 | int ret, i, eeprom_shift = 0; |
| 489 | u8 tmp; |
| 490 | u16 tmp16; |
| 491 | |
| 492 | /* check if there is dual tuners */ |
| 493 | ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp); |
| 494 | if (ret < 0) |
| 495 | goto err; |
| 496 | |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 497 | state->dual_mode = tmp; |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 498 | dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", |
| 499 | __func__, state->dual_mode); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 500 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 501 | for (i = 0; i < state->dual_mode + 1; i++) { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 502 | /* tuner */ |
| 503 | ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp); |
| 504 | if (ret < 0) |
| 505 | goto err; |
| 506 | |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 507 | state->af9033_config[i].tuner = tmp; |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 508 | dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n", |
| 509 | __func__, i, tmp); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 510 | |
| 511 | switch (tmp) { |
| 512 | case AF9033_TUNER_TUA9001: |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 513 | case AF9033_TUNER_FC0011: |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 514 | case AF9033_TUNER_MXL5007T: |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 515 | case AF9033_TUNER_TDA18218: |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame^] | 516 | case AF9033_TUNER_FC2580: |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 517 | state->af9033_config[i].spec_inv = 1; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 518 | break; |
| 519 | default: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 520 | dev_warn(&d->udev->dev, "%s: tuner id=%02x not " \ |
| 521 | "supported, please report!", |
| 522 | KBUILD_MODNAME, tmp); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | /* tuner IF frequency */ |
| 526 | ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp); |
| 527 | if (ret < 0) |
| 528 | goto err; |
| 529 | |
| 530 | tmp16 = tmp; |
| 531 | |
| 532 | ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp); |
| 533 | if (ret < 0) |
| 534 | goto err; |
| 535 | |
| 536 | tmp16 |= tmp << 8; |
| 537 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 538 | 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] | 539 | |
| 540 | eeprom_shift = 0x10; /* shift for the 2nd tuner params */ |
| 541 | } |
| 542 | |
| 543 | /* get demod clock */ |
| 544 | ret = af9035_rd_reg(d, 0x00d800, &tmp); |
| 545 | if (ret < 0) |
| 546 | goto err; |
| 547 | |
| 548 | tmp = (tmp >> 0) & 0x0f; |
| 549 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 550 | for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 551 | state->af9033_config[i].clock = clock_lut[tmp]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 552 | |
| 553 | return 0; |
| 554 | |
| 555 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 556 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 557 | |
| 558 | return ret; |
| 559 | } |
| 560 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 561 | static int af9035_read_config_it9135(struct dvb_usb_device *d) |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 562 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 563 | struct state *state = d_to_priv(d); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 564 | int ret, i; |
| 565 | u8 tmp; |
| 566 | |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 567 | state->dual_mode = false; |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 568 | |
| 569 | /* get demod clock */ |
| 570 | ret = af9035_rd_reg(d, 0x00d800, &tmp); |
| 571 | if (ret < 0) |
| 572 | goto err; |
| 573 | |
| 574 | tmp = (tmp >> 0) & 0x0f; |
| 575 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 576 | for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 577 | state->af9033_config[i].clock = clock_lut_it9135[tmp]; |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 578 | |
| 579 | return 0; |
| 580 | |
| 581 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 582 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | f2b61d0 | 2012-04-05 20:28:51 -0300 | [diff] [blame] | 583 | |
| 584 | return ret; |
| 585 | } |
| 586 | |
Antti Palosaari | 51639be | 2012-09-16 22:26:56 -0300 | [diff] [blame] | 587 | static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d, |
| 588 | int cmd, int arg) |
| 589 | { |
| 590 | int ret; |
| 591 | u8 val; |
| 592 | |
| 593 | dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg); |
| 594 | |
| 595 | /* |
| 596 | * CEN always enabled by hardware wiring |
| 597 | * RESETN GPIOT3 |
| 598 | * RXEN GPIOT2 |
| 599 | */ |
| 600 | |
| 601 | switch (cmd) { |
| 602 | case TUA9001_CMD_RESETN: |
| 603 | if (arg) |
| 604 | val = 0x00; |
| 605 | else |
| 606 | val = 0x01; |
| 607 | |
| 608 | ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01); |
| 609 | if (ret < 0) |
| 610 | goto err; |
| 611 | break; |
| 612 | case TUA9001_CMD_RXEN: |
| 613 | if (arg) |
| 614 | val = 0x01; |
| 615 | else |
| 616 | val = 0x00; |
| 617 | |
| 618 | ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01); |
| 619 | if (ret < 0) |
| 620 | goto err; |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | |
| 626 | err: |
| 627 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 633 | static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d, |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 634 | int cmd, int arg) |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 635 | { |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 636 | int ret; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 637 | |
| 638 | switch (cmd) { |
| 639 | case FC0011_FE_CALLBACK_POWER: |
| 640 | /* Tuner enable */ |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 641 | ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1); |
| 642 | if (ret < 0) |
| 643 | goto err; |
| 644 | |
| 645 | ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1); |
| 646 | if (ret < 0) |
| 647 | goto err; |
| 648 | |
| 649 | ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1); |
| 650 | if (ret < 0) |
| 651 | goto err; |
| 652 | |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 653 | /* LED */ |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 654 | ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1); |
| 655 | if (ret < 0) |
| 656 | goto err; |
| 657 | |
| 658 | ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1); |
| 659 | if (ret < 0) |
| 660 | goto err; |
| 661 | |
Michael Büsch | de2bec5 | 2012-04-03 05:11:30 -0300 | [diff] [blame] | 662 | usleep_range(10000, 50000); |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 663 | break; |
| 664 | case FC0011_FE_CALLBACK_RESET: |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 665 | ret = af9035_wr_reg(d, 0xd8e9, 1); |
| 666 | if (ret < 0) |
| 667 | goto err; |
| 668 | |
| 669 | ret = af9035_wr_reg(d, 0xd8e8, 1); |
| 670 | if (ret < 0) |
| 671 | goto err; |
| 672 | |
| 673 | ret = af9035_wr_reg(d, 0xd8e7, 1); |
| 674 | if (ret < 0) |
| 675 | goto err; |
| 676 | |
Michael Büsch | de2bec5 | 2012-04-03 05:11:30 -0300 | [diff] [blame] | 677 | usleep_range(10000, 20000); |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 678 | |
| 679 | ret = af9035_wr_reg(d, 0xd8e7, 0); |
| 680 | if (ret < 0) |
| 681 | goto err; |
| 682 | |
Michael Büsch | de2bec5 | 2012-04-03 05:11:30 -0300 | [diff] [blame] | 683 | usleep_range(10000, 20000); |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 684 | break; |
| 685 | default: |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 686 | ret = -EINVAL; |
| 687 | goto err; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | return 0; |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 691 | |
| 692 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 693 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | daacd5b | 2012-04-05 21:52:21 -0300 | [diff] [blame] | 694 | |
| 695 | return ret; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg) |
| 699 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 700 | struct state *state = d_to_priv(d); |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 701 | |
| 702 | switch (state->af9033_config[0].tuner) { |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 703 | case AF9033_TUNER_FC0011: |
| 704 | return af9035_fc0011_tuner_callback(d, cmd, arg); |
Antti Palosaari | 51639be | 2012-09-16 22:26:56 -0300 | [diff] [blame] | 705 | case AF9033_TUNER_TUA9001: |
| 706 | return af9035_tua9001_tuner_callback(d, cmd, arg); |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 707 | default: |
| 708 | break; |
| 709 | } |
| 710 | |
Antti Palosaari | 1835af1 | 2012-09-11 22:27:06 -0300 | [diff] [blame] | 711 | return 0; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static int af9035_frontend_callback(void *adapter_priv, int component, |
| 715 | int cmd, int arg) |
| 716 | { |
| 717 | struct i2c_adapter *adap = adapter_priv; |
| 718 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
| 719 | |
Antti Palosaari | 1835af1 | 2012-09-11 22:27:06 -0300 | [diff] [blame] | 720 | dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n", |
| 721 | __func__, component, cmd, arg); |
| 722 | |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 723 | switch (component) { |
| 724 | case DVB_FRONTEND_COMPONENT_TUNER: |
| 725 | return af9035_tuner_callback(d, cmd, arg); |
| 726 | default: |
| 727 | break; |
| 728 | } |
| 729 | |
Antti Palosaari | 1835af1 | 2012-09-11 22:27:06 -0300 | [diff] [blame] | 730 | return 0; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 731 | } |
| 732 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 733 | static int af9035_frontend_attach(struct dvb_usb_adapter *adap) |
| 734 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 735 | struct state *state = adap_to_priv(adap); |
| 736 | struct dvb_usb_device *d = adap_to_d(adap); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 737 | int ret; |
| 738 | |
Antti Palosaari | 1cbabf9 | 2012-05-07 14:59:55 -0300 | [diff] [blame] | 739 | if (!state->af9033_config[adap->id].tuner) { |
| 740 | /* unsupported tuner */ |
Antti Palosaari | 5a9abae | 2012-03-30 17:15:16 -0300 | [diff] [blame] | 741 | ret = -ENODEV; |
| 742 | goto err; |
| 743 | } |
| 744 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 745 | if (adap->id == 0) { |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 746 | state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB; |
| 747 | state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL; |
| 748 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 749 | ret = af9035_wr_reg(d, 0x00417f, |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 750 | state->af9033_config[1].i2c_addr); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 751 | if (ret < 0) |
| 752 | goto err; |
| 753 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 754 | ret = af9035_wr_reg(d, 0x00d81a, |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 755 | state->dual_mode); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 756 | if (ret < 0) |
| 757 | goto err; |
| 758 | } |
| 759 | |
| 760 | /* attach demodulator */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 761 | adap->fe[0] = dvb_attach(af9033_attach, |
| 762 | &state->af9033_config[adap->id], &d->i2c_adap); |
| 763 | if (adap->fe[0] == NULL) { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 764 | ret = -ENODEV; |
| 765 | goto err; |
| 766 | } |
Antti Palosaari | 852f0d9 | 2012-04-06 07:09:23 -0300 | [diff] [blame] | 767 | |
| 768 | /* disable I2C-gate */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 769 | adap->fe[0]->ops.i2c_gate_ctrl = NULL; |
| 770 | adap->fe[0]->callback = af9035_frontend_callback; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 771 | |
| 772 | return 0; |
| 773 | |
| 774 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 775 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 776 | |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | static struct tua9001_config af9035_tua9001_config = { |
| 781 | .i2c_addr = 0x60, |
| 782 | }; |
| 783 | |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 784 | static const struct fc0011_config af9035_fc0011_config = { |
| 785 | .i2c_address = 0x60, |
| 786 | }; |
| 787 | |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 788 | static struct mxl5007t_config af9035_mxl5007t_config = { |
| 789 | .xtal_freq_hz = MxL_XTAL_24_MHZ, |
| 790 | .if_freq_hz = MxL_IF_4_57_MHZ, |
| 791 | .invert_if = 0, |
| 792 | .loop_thru_enable = 0, |
| 793 | .clk_out_enable = 0, |
| 794 | .clk_out_amp = MxL_CLKOUT_AMP_0_94V, |
| 795 | }; |
| 796 | |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 797 | static struct tda18218_config af9035_tda18218_config = { |
| 798 | .i2c_address = 0x60, |
| 799 | .i2c_wr_max = 21, |
| 800 | }; |
| 801 | |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame^] | 802 | static const struct fc2580_config af9035_fc2580_config = { |
| 803 | .i2c_addr = 0x56, |
| 804 | .clock = 16384000, |
| 805 | }; |
| 806 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 807 | static int af9035_tuner_attach(struct dvb_usb_adapter *adap) |
| 808 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 809 | struct state *state = adap_to_priv(adap); |
| 810 | struct dvb_usb_device *d = adap_to_d(adap); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 811 | int ret; |
| 812 | struct dvb_frontend *fe; |
| 813 | |
Antti Palosaari | 2a79eef | 2012-05-07 14:50:40 -0300 | [diff] [blame] | 814 | switch (state->af9033_config[adap->id].tuner) { |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 815 | case AF9033_TUNER_TUA9001: |
| 816 | /* AF9035 gpiot3 = TUA9001 RESETN |
| 817 | AF9035 gpiot2 = TUA9001 RXEN */ |
| 818 | |
| 819 | /* configure gpiot2 and gpiot2 as output */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 820 | ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 821 | if (ret < 0) |
| 822 | goto err; |
| 823 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 824 | ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 825 | if (ret < 0) |
| 826 | goto err; |
| 827 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 828 | ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 829 | if (ret < 0) |
| 830 | goto err; |
| 831 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 832 | ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 833 | if (ret < 0) |
| 834 | goto err; |
| 835 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 836 | /* attach tuner */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 837 | fe = dvb_attach(tua9001_attach, adap->fe[0], |
| 838 | &d->i2c_adap, &af9035_tua9001_config); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 839 | break; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 840 | case AF9033_TUNER_FC0011: |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 841 | fe = dvb_attach(fc0011_attach, adap->fe[0], |
| 842 | &d->i2c_adap, &af9035_fc0011_config); |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 843 | break; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 844 | case AF9033_TUNER_MXL5007T: |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 845 | ret = af9035_wr_reg(d, 0x00d8e0, 1); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 846 | if (ret < 0) |
| 847 | goto err; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 848 | ret = af9035_wr_reg(d, 0x00d8e1, 1); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 849 | if (ret < 0) |
| 850 | goto err; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 851 | ret = af9035_wr_reg(d, 0x00d8df, 0); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 852 | if (ret < 0) |
| 853 | goto err; |
| 854 | |
| 855 | msleep(30); |
| 856 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 857 | ret = af9035_wr_reg(d, 0x00d8df, 1); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 858 | if (ret < 0) |
| 859 | goto err; |
| 860 | |
| 861 | msleep(300); |
| 862 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 863 | ret = af9035_wr_reg(d, 0x00d8c0, 1); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 864 | if (ret < 0) |
| 865 | goto err; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 866 | ret = af9035_wr_reg(d, 0x00d8c1, 1); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 867 | if (ret < 0) |
| 868 | goto err; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 869 | ret = af9035_wr_reg(d, 0x00d8bf, 0); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 870 | if (ret < 0) |
| 871 | goto err; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 872 | ret = af9035_wr_reg(d, 0x00d8b4, 1); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 873 | if (ret < 0) |
| 874 | goto err; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 875 | ret = af9035_wr_reg(d, 0x00d8b5, 1); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 876 | if (ret < 0) |
| 877 | goto err; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 878 | ret = af9035_wr_reg(d, 0x00d8b3, 1); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 879 | if (ret < 0) |
| 880 | goto err; |
| 881 | |
| 882 | /* attach tuner */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 883 | fe = dvb_attach(mxl5007t_attach, adap->fe[0], |
| 884 | &d->i2c_adap, 0x60, &af9035_mxl5007t_config); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 885 | break; |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 886 | case AF9033_TUNER_TDA18218: |
| 887 | /* attach tuner */ |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 888 | fe = dvb_attach(tda18218_attach, adap->fe[0], |
| 889 | &d->i2c_adap, &af9035_tda18218_config); |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 890 | break; |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame^] | 891 | case AF9033_TUNER_FC2580: |
| 892 | /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */ |
| 893 | ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01); |
| 894 | if (ret < 0) |
| 895 | goto err; |
| 896 | |
| 897 | ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01); |
| 898 | if (ret < 0) |
| 899 | goto err; |
| 900 | |
| 901 | ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01); |
| 902 | if (ret < 0) |
| 903 | goto err; |
| 904 | |
| 905 | usleep_range(10000, 50000); |
| 906 | /* attach tuner */ |
| 907 | fe = dvb_attach(fc2580_attach, adap->fe[0], |
| 908 | &d->i2c_adap, &af9035_fc2580_config); |
| 909 | break; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 910 | default: |
| 911 | fe = NULL; |
| 912 | } |
| 913 | |
| 914 | if (fe == NULL) { |
| 915 | ret = -ENODEV; |
| 916 | goto err; |
| 917 | } |
| 918 | |
| 919 | return 0; |
| 920 | |
| 921 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 922 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 923 | |
| 924 | return ret; |
| 925 | } |
| 926 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 927 | static int af9035_init(struct dvb_usb_device *d) |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 928 | { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 929 | struct state *state = d_to_priv(d); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 930 | int ret, i; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 931 | u16 frame_size = 87 * 188 / 4; |
| 932 | u8 packet_size = 512 / 4; |
| 933 | struct reg_val_mask tab[] = { |
| 934 | { 0x80f99d, 0x01, 0x01 }, |
| 935 | { 0x80f9a4, 0x01, 0x01 }, |
| 936 | { 0x00dd11, 0x00, 0x20 }, |
| 937 | { 0x00dd11, 0x00, 0x40 }, |
| 938 | { 0x00dd13, 0x00, 0x20 }, |
| 939 | { 0x00dd13, 0x00, 0x40 }, |
| 940 | { 0x00dd11, 0x20, 0x20 }, |
| 941 | { 0x00dd88, (frame_size >> 0) & 0xff, 0xff}, |
| 942 | { 0x00dd89, (frame_size >> 8) & 0xff, 0xff}, |
| 943 | { 0x00dd0c, packet_size, 0xff}, |
| 944 | { 0x00dd11, state->dual_mode << 6, 0x40 }, |
| 945 | { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff}, |
| 946 | { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff}, |
| 947 | { 0x00dd0d, packet_size, 0xff }, |
| 948 | { 0x80f9a3, 0x00, 0x01 }, |
| 949 | { 0x80f9cd, 0x00, 0x01 }, |
| 950 | { 0x80f99d, 0x00, 0x01 }, |
| 951 | { 0x80f9a4, 0x00, 0x01 }, |
| 952 | }; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 953 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 954 | dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \ |
| 955 | "packet_size=%02x\n", __func__, |
| 956 | d->udev->speed, frame_size, packet_size); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 957 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 958 | /* init endpoints */ |
| 959 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 960 | ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val, |
| 961 | tab[i].mask); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 962 | if (ret < 0) |
| 963 | goto err; |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | |
| 968 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 969 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 970 | |
| 971 | return ret; |
| 972 | } |
| 973 | |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 974 | static int af9035_rc_query(struct dvb_usb_device *d) |
| 975 | { |
| 976 | unsigned int key; |
| 977 | unsigned char b[4]; |
| 978 | int ret; |
| 979 | struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b }; |
| 980 | |
| 981 | ret = af9035_ctrl_msg(d, &req); |
| 982 | if (ret < 0) |
| 983 | goto err; |
| 984 | |
| 985 | if ((b[2] + b[3]) == 0xff) { |
| 986 | if ((b[0] + b[1]) == 0xff) { |
| 987 | /* NEC */ |
| 988 | key = b[0] << 8 | b[2]; |
| 989 | } else { |
| 990 | /* ext. NEC */ |
| 991 | key = b[0] << 16 | b[1] << 8 | b[2]; |
| 992 | } |
| 993 | } else { |
| 994 | key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]; |
| 995 | } |
| 996 | |
| 997 | rc_keydown(d->rc_dev, key, 0); |
| 998 | |
| 999 | err: |
| 1000 | /* ignore errors */ |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc) |
| 1005 | { |
| 1006 | int ret; |
| 1007 | u8 tmp; |
| 1008 | |
| 1009 | ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp); |
| 1010 | if (ret < 0) |
| 1011 | goto err; |
| 1012 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1013 | dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1014 | |
| 1015 | /* don't activate rc if in HID mode or if not available */ |
| 1016 | if (tmp == 5) { |
| 1017 | ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp); |
| 1018 | if (ret < 0) |
| 1019 | goto err; |
| 1020 | |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1021 | dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1022 | |
| 1023 | switch (tmp) { |
| 1024 | case 0: /* NEC */ |
| 1025 | default: |
| 1026 | rc->allowed_protos = RC_TYPE_NEC; |
| 1027 | break; |
| 1028 | case 1: /* RC6 */ |
| 1029 | rc->allowed_protos = RC_TYPE_RC6; |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | rc->query = af9035_rc_query; |
| 1034 | rc->interval = 500; |
Antti Palosaari | de73bee | 2012-07-05 19:57:07 -0300 | [diff] [blame] | 1035 | |
| 1036 | /* load empty to enable rc */ |
| 1037 | if (!rc->map_name) |
| 1038 | rc->map_name = RC_MAP_EMPTY; |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | return 0; |
| 1042 | |
| 1043 | err: |
Antti Palosaari | 119f7a8 | 2012-09-12 20:23:53 -0300 | [diff] [blame] | 1044 | dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1045 | |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | /* interface 0 is used by DVB-T receiver and |
| 1050 | interface 1 is for remote controller (HID) */ |
| 1051 | static const struct dvb_usb_device_properties af9035_props = { |
| 1052 | .driver_name = KBUILD_MODNAME, |
| 1053 | .owner = THIS_MODULE, |
| 1054 | .adapter_nr = adapter_nr, |
| 1055 | .size_of_priv = sizeof(struct state), |
| 1056 | |
| 1057 | .generic_bulk_ctrl_endpoint = 0x02, |
| 1058 | .generic_bulk_ctrl_endpoint_response = 0x81, |
| 1059 | |
| 1060 | .identify_state = af9035_identify_state, |
Antti Palosaari | 4395e4b | 2012-09-12 12:19:06 -0300 | [diff] [blame] | 1061 | .firmware = AF9035_FIRMWARE_AF9035, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1062 | .download_firmware = af9035_download_firmware, |
| 1063 | |
| 1064 | .i2c_algo = &af9035_i2c_algo, |
| 1065 | .read_config = af9035_read_config, |
| 1066 | .frontend_attach = af9035_frontend_attach, |
| 1067 | .tuner_attach = af9035_tuner_attach, |
| 1068 | .init = af9035_init, |
| 1069 | .get_rc_config = af9035_get_rc_config, |
| 1070 | |
| 1071 | .num_adapters = 1, |
| 1072 | .adapter = { |
| 1073 | { |
| 1074 | .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188), |
| 1075 | }, { |
| 1076 | .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188), |
| 1077 | }, |
| 1078 | }, |
| 1079 | }; |
| 1080 | |
| 1081 | static const struct dvb_usb_device_properties it9135_props = { |
| 1082 | .driver_name = KBUILD_MODNAME, |
| 1083 | .owner = THIS_MODULE, |
| 1084 | .adapter_nr = adapter_nr, |
| 1085 | .size_of_priv = sizeof(struct state), |
| 1086 | |
| 1087 | .generic_bulk_ctrl_endpoint = 0x02, |
| 1088 | .generic_bulk_ctrl_endpoint_response = 0x81, |
| 1089 | |
| 1090 | .identify_state = af9035_identify_state, |
Antti Palosaari | 4395e4b | 2012-09-12 12:19:06 -0300 | [diff] [blame] | 1091 | .firmware = AF9035_FIRMWARE_IT9135, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1092 | .download_firmware = af9035_download_firmware_it9135, |
| 1093 | |
| 1094 | .i2c_algo = &af9035_i2c_algo, |
| 1095 | .read_config = af9035_read_config_it9135, |
| 1096 | .frontend_attach = af9035_frontend_attach, |
| 1097 | .tuner_attach = af9035_tuner_attach, |
| 1098 | .init = af9035_init, |
| 1099 | .get_rc_config = af9035_get_rc_config, |
| 1100 | |
| 1101 | .num_adapters = 1, |
| 1102 | .adapter = { |
| 1103 | { |
| 1104 | .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188), |
| 1105 | }, { |
| 1106 | .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188), |
| 1107 | }, |
| 1108 | }, |
| 1109 | }; |
| 1110 | |
| 1111 | static const struct usb_device_id af9035_id_table[] = { |
| 1112 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035, |
| 1113 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1114 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000, |
| 1115 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1116 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001, |
| 1117 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1118 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002, |
| 1119 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1120 | { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003, |
| 1121 | &af9035_props, "Afatech AF9035 reference design", NULL) }, |
| 1122 | { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK, |
| 1123 | &af9035_props, "TerraTec Cinergy T Stick", NULL) }, |
| 1124 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835, |
| 1125 | &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) }, |
| 1126 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835, |
| 1127 | &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) }, |
| 1128 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867, |
| 1129 | &af9035_props, "AVerMedia HD Volar (A867)", NULL) }, |
| 1130 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867, |
| 1131 | &af9035_props, "AVerMedia HD Volar (A867)", NULL) }, |
| 1132 | { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR, |
| 1133 | &af9035_props, "AVerMedia Twinstar (A825)", NULL) }, |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame^] | 1134 | { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS, |
| 1135 | &af9035_props, "Asus U3100Mini Plus", NULL) }, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1136 | { } |
| 1137 | }; |
| 1138 | MODULE_DEVICE_TABLE(usb, af9035_id_table); |
| 1139 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1140 | static struct usb_driver af9035_usb_driver = { |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1141 | .name = KBUILD_MODNAME, |
| 1142 | .id_table = af9035_id_table, |
| 1143 | .probe = dvb_usbv2_probe, |
| 1144 | .disconnect = dvb_usbv2_disconnect, |
| 1145 | .suspend = dvb_usbv2_suspend, |
| 1146 | .resume = dvb_usbv2_resume, |
Antti Palosaari | 04966aa | 2012-08-14 22:21:08 -0300 | [diff] [blame] | 1147 | .reset_resume = dvb_usbv2_reset_resume, |
Antti Palosaari | 5da2aec | 2012-06-17 23:15:03 -0300 | [diff] [blame] | 1148 | .no_dynamic_id = 1, |
| 1149 | .soft_unbind = 1, |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1150 | }; |
| 1151 | |
Gianluca Gennari | 48bf7e1 | 2012-04-02 17:25:17 -0300 | [diff] [blame] | 1152 | module_usb_driver(af9035_usb_driver); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1153 | |
| 1154 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1155 | MODULE_DESCRIPTION("Afatech AF9035 driver"); |
| 1156 | MODULE_LICENSE("GPL"); |
Antti Palosaari | 4395e4b | 2012-09-12 12:19:06 -0300 | [diff] [blame] | 1157 | MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035); |
| 1158 | MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135); |