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" |
| 23 | #include "af9033.h" |
| 24 | #include "tua9001.h" |
| 25 | |
| 26 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 27 | static DEFINE_MUTEX(af9035_usb_mutex); |
| 28 | static struct config af9035_config; |
| 29 | static struct dvb_usb_device_properties af9035_properties[1]; |
| 30 | static int af9035_properties_count = ARRAY_SIZE(af9035_properties); |
| 31 | static struct af9033_config af9035_af9033_config[] = { |
| 32 | { |
| 33 | .ts_mode = AF9033_TS_MODE_USB, |
| 34 | }, { |
| 35 | .ts_mode = AF9033_TS_MODE_SERIAL, |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | static int af9035_ctrl_msg(struct usb_device *udev, struct usb_req *req) |
| 40 | { |
| 41 | #define BUF_LEN 63 |
| 42 | #define REQ_HDR_LEN 4 /* send header size */ |
| 43 | #define ACK_HDR_LEN 3 /* rece header size */ |
| 44 | #define CHECKSUM_LEN 2 |
| 45 | #define USB_TIMEOUT 2000 |
| 46 | |
| 47 | int ret, i, act_len; |
| 48 | u8 buf[BUF_LEN]; |
| 49 | u32 msg_len; |
| 50 | static u8 seq; /* packet sequence number */ |
| 51 | u16 checksum = 0; |
| 52 | |
| 53 | /* buffer overflow check */ |
| 54 | if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) || |
| 55 | req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) { |
| 56 | pr_debug("%s: too much data wlen=%d rlen=%d\n", __func__, |
| 57 | req->wlen, req->rlen); |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | if (mutex_lock_interruptible(&af9035_usb_mutex) < 0) |
| 62 | return -EAGAIN; |
| 63 | |
| 64 | buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1; |
| 65 | buf[1] = req->mbox; |
| 66 | buf[2] = req->cmd; |
| 67 | buf[3] = seq++; |
| 68 | if (req->wlen) |
| 69 | memcpy(&buf[4], req->wbuf, req->wlen); |
| 70 | |
| 71 | /* calc and add checksum */ |
| 72 | for (i = 1; i < buf[0]-1; i++) { |
| 73 | if (i % 2) |
| 74 | checksum += buf[i] << 8; |
| 75 | else |
| 76 | checksum += buf[i]; |
| 77 | } |
| 78 | checksum = ~checksum; |
| 79 | |
| 80 | buf[buf[0]-1] = (checksum >> 8); |
| 81 | buf[buf[0]-0] = (checksum & 0xff); |
| 82 | |
| 83 | msg_len = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN ; |
| 84 | |
| 85 | /* send req */ |
| 86 | ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len, |
| 87 | &act_len, USB_TIMEOUT); |
| 88 | if (ret < 0) |
| 89 | err("bulk message failed=%d (%d/%d)", ret, msg_len, act_len); |
| 90 | else |
| 91 | if (act_len != msg_len) |
| 92 | ret = -EIO; /* all data is not send */ |
| 93 | if (ret < 0) |
| 94 | goto err_mutex_unlock; |
| 95 | |
| 96 | /* no ack for those packets */ |
| 97 | if (req->cmd == CMD_FW_DL) |
| 98 | goto exit_mutex_unlock; |
| 99 | |
| 100 | /* receive ack and data if read req */ |
| 101 | msg_len = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN; |
| 102 | ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len, |
| 103 | &act_len, USB_TIMEOUT); |
| 104 | if (ret < 0) { |
| 105 | err("recv bulk message failed=%d", ret); |
| 106 | ret = -EIO; |
| 107 | goto err_mutex_unlock; |
| 108 | } |
| 109 | |
| 110 | /* check status */ |
| 111 | if (buf[2]) { |
| 112 | pr_debug("%s: command=%02x failed fw error=%d\n", __func__, |
| 113 | req->cmd, buf[2]); |
| 114 | ret = -EIO; |
| 115 | goto err_mutex_unlock; |
| 116 | } |
| 117 | |
| 118 | /* read request, copy returned data to return buf */ |
| 119 | if (req->rlen) |
| 120 | memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen); |
| 121 | |
| 122 | err_mutex_unlock: |
| 123 | exit_mutex_unlock: |
| 124 | mutex_unlock(&af9035_usb_mutex); |
| 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | /* write multiple registers */ |
| 130 | static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) |
| 131 | { |
| 132 | u8 wbuf[6 + len]; |
| 133 | u8 mbox = (reg >> 16) & 0xff; |
| 134 | struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL }; |
| 135 | |
| 136 | wbuf[0] = len; |
| 137 | wbuf[1] = 2; |
| 138 | wbuf[2] = 0; |
| 139 | wbuf[3] = 0; |
| 140 | wbuf[4] = (reg >> 8) & 0xff; |
| 141 | wbuf[5] = (reg >> 0) & 0xff; |
| 142 | memcpy(&wbuf[6], val, len); |
| 143 | |
| 144 | return af9035_ctrl_msg(d->udev, &req); |
| 145 | } |
| 146 | |
| 147 | /* read multiple registers */ |
| 148 | static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) |
| 149 | { |
| 150 | u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff }; |
| 151 | u8 mbox = (reg >> 16) & 0xff; |
| 152 | struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val }; |
| 153 | |
| 154 | return af9035_ctrl_msg(d->udev, &req); |
| 155 | } |
| 156 | |
| 157 | /* write single register */ |
| 158 | static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val) |
| 159 | { |
| 160 | return af9035_wr_regs(d, reg, &val, 1); |
| 161 | } |
| 162 | |
| 163 | /* read single register */ |
| 164 | static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val) |
| 165 | { |
| 166 | return af9035_rd_regs(d, reg, val, 1); |
| 167 | } |
| 168 | |
| 169 | /* write single register with mask */ |
| 170 | static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val, |
| 171 | u8 mask) |
| 172 | { |
| 173 | int ret; |
| 174 | u8 tmp; |
| 175 | |
| 176 | /* no need for read if whole reg is written */ |
| 177 | if (mask != 0xff) { |
| 178 | ret = af9035_rd_regs(d, reg, &tmp, 1); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | |
| 182 | val &= mask; |
| 183 | tmp &= ~mask; |
| 184 | val |= tmp; |
| 185 | } |
| 186 | |
| 187 | return af9035_wr_regs(d, reg, &val, 1); |
| 188 | } |
| 189 | |
| 190 | static int af9035_i2c_master_xfer(struct i2c_adapter *adap, |
| 191 | struct i2c_msg msg[], int num) |
| 192 | { |
| 193 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
| 194 | int ret; |
| 195 | |
| 196 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) |
| 197 | return -EAGAIN; |
| 198 | |
| 199 | if (num == 2 && !(msg[0].flags & I2C_M_RD) && |
| 200 | (msg[1].flags & I2C_M_RD)) { |
| 201 | if (msg[0].len > 40 || msg[1].len > 40) { |
| 202 | /* TODO: correct limits > 40 */ |
| 203 | ret = -EOPNOTSUPP; |
| 204 | } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) { |
| 205 | /* integrated demod */ |
| 206 | u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | |
| 207 | msg[0].buf[2]; |
| 208 | ret = af9035_rd_regs(d, reg, &msg[1].buf[0], |
| 209 | msg[1].len); |
| 210 | } else { |
| 211 | /* I2C */ |
| 212 | #if 0 |
| 213 | /* |
| 214 | * FIXME: Keep that code. It should work but as it is |
| 215 | * not tested I left it disabled and return -EOPNOTSUPP |
| 216 | * for the sure. |
| 217 | */ |
| 218 | u8 buf[4 + msg[0].len]; |
| 219 | struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf), |
| 220 | buf, msg[1].len, msg[1].buf }; |
| 221 | buf[0] = msg[0].len; |
| 222 | buf[1] = msg[0].addr << 1; |
| 223 | buf[2] = 0x01; |
| 224 | buf[3] = 0x00; |
| 225 | memcpy(&buf[4], msg[0].buf, msg[0].len); |
| 226 | ret = af9035_ctrl_msg(d->udev, &req); |
| 227 | #endif |
| 228 | pr_debug("%s: I2C operation not supported\n", __func__); |
| 229 | ret = -EOPNOTSUPP; |
| 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; |
| 235 | } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) { |
| 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 */ |
| 243 | u8 buf[4 + msg[0].len]; |
| 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; |
| 248 | buf[2] = 0x01; |
| 249 | buf[3] = 0x00; |
| 250 | memcpy(&buf[4], msg[0].buf, msg[0].len); |
| 251 | ret = af9035_ctrl_msg(d->udev, &req); |
| 252 | } |
| 253 | } else { |
| 254 | /* |
| 255 | * We support only two kind of I2C transactions: |
| 256 | * 1) 1 x read + 1 x write |
| 257 | * 2) 1 x write |
| 258 | */ |
| 259 | ret = -EOPNOTSUPP; |
| 260 | } |
| 261 | |
| 262 | mutex_unlock(&d->i2c_mutex); |
| 263 | |
| 264 | if (ret < 0) |
| 265 | return ret; |
| 266 | else |
| 267 | return num; |
| 268 | } |
| 269 | |
| 270 | static u32 af9035_i2c_functionality(struct i2c_adapter *adapter) |
| 271 | { |
| 272 | return I2C_FUNC_I2C; |
| 273 | } |
| 274 | |
| 275 | static struct i2c_algorithm af9035_i2c_algo = { |
| 276 | .master_xfer = af9035_i2c_master_xfer, |
| 277 | .functionality = af9035_i2c_functionality, |
| 278 | }; |
| 279 | |
| 280 | static int af9035_init(struct dvb_usb_device *d) |
| 281 | { |
| 282 | int ret, i; |
| 283 | u16 frame_size = 87 * 188 / 4; |
| 284 | u8 packet_size = 512 / 4; |
| 285 | struct reg_val_mask tab[] = { |
| 286 | { 0x80f99d, 0x01, 0x01 }, |
| 287 | { 0x80f9a4, 0x01, 0x01 }, |
| 288 | { 0x00dd11, 0x00, 0x20 }, |
| 289 | { 0x00dd11, 0x00, 0x40 }, |
| 290 | { 0x00dd13, 0x00, 0x20 }, |
| 291 | { 0x00dd13, 0x00, 0x40 }, |
| 292 | { 0x00dd11, 0x20, 0x20 }, |
| 293 | { 0x00dd88, (frame_size >> 0) & 0xff, 0xff}, |
| 294 | { 0x00dd89, (frame_size >> 8) & 0xff, 0xff}, |
| 295 | { 0x00dd0c, packet_size, 0xff}, |
| 296 | { 0x00dd11, af9035_config.dual_mode << 6, 0x40 }, |
| 297 | { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff}, |
| 298 | { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff}, |
| 299 | { 0x00dd0d, packet_size, 0xff }, |
| 300 | { 0x80f9a3, 0x00, 0x01 }, |
| 301 | { 0x80f9cd, 0x00, 0x01 }, |
| 302 | { 0x80f99d, 0x00, 0x01 }, |
| 303 | { 0x80f9a4, 0x00, 0x01 }, |
| 304 | }; |
| 305 | |
| 306 | pr_debug("%s: USB speed=%d frame_size=%04x packet_size=%02x\n", |
| 307 | __func__, d->udev->speed, frame_size, packet_size); |
| 308 | |
| 309 | /* init endpoints */ |
| 310 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 311 | ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val, |
| 312 | tab[i].mask); |
| 313 | if (ret < 0) |
| 314 | goto err; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | |
| 319 | err: |
| 320 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | static int af9035_identify_state(struct usb_device *udev, |
| 326 | struct dvb_usb_device_properties *props, |
| 327 | struct dvb_usb_device_description **desc, |
| 328 | int *cold) |
| 329 | { |
| 330 | int ret; |
| 331 | u8 wbuf[1] = { 1 }; |
| 332 | u8 rbuf[4]; |
| 333 | struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf, |
| 334 | sizeof(rbuf), rbuf }; |
| 335 | |
| 336 | ret = af9035_ctrl_msg(udev, &req); |
| 337 | if (ret < 0) |
| 338 | goto err; |
| 339 | |
| 340 | pr_debug("%s: reply=%02x %02x %02x %02x\n", __func__, |
| 341 | rbuf[0], rbuf[1], rbuf[2], rbuf[3]); |
| 342 | if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3]) |
| 343 | *cold = 0; |
| 344 | else |
| 345 | *cold = 1; |
| 346 | |
| 347 | return 0; |
| 348 | |
| 349 | err: |
| 350 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 351 | |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | static int af9035_download_firmware(struct usb_device *udev, |
| 356 | const struct firmware *fw) |
| 357 | { |
| 358 | u8 *fw_data_ptr = (u8 *) fw->data; |
| 359 | int i, j, len, packets, remainder, ret; |
| 360 | u8 wbuf[1]; |
| 361 | u8 rbuf[4]; |
| 362 | struct fw_header fw_hdr; |
| 363 | struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; |
| 364 | struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL }; |
| 365 | struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; |
| 366 | |
| 367 | /* read firmware segment info from beginning of the firmware file */ |
| 368 | fw_hdr.segment_count = *fw_data_ptr++; |
| 369 | pr_debug("%s: fw segment count=%d\n", __func__, fw_hdr.segment_count); |
| 370 | if (fw_hdr.segment_count > SEGMENT_MAX_COUNT) { |
| 371 | pr_debug("%s: too big fw segmen count=%d\n", __func__, |
| 372 | fw_hdr.segment_count); |
| 373 | fw_hdr.segment_count = SEGMENT_MAX_COUNT; |
| 374 | } |
| 375 | for (i = 0; i < fw_hdr.segment_count; i++) { |
| 376 | fw_hdr.segment[i].type = (*fw_data_ptr++); |
| 377 | fw_hdr.segment[i].len = (*fw_data_ptr++) << 24; |
| 378 | fw_hdr.segment[i].len += (*fw_data_ptr++) << 16; |
| 379 | fw_hdr.segment[i].len += (*fw_data_ptr++) << 8; |
| 380 | fw_hdr.segment[i].len += (*fw_data_ptr++) << 0; |
| 381 | pr_debug("%s: fw segment type=%d len=%d\n", __func__, |
| 382 | fw_hdr.segment[i].type, fw_hdr.segment[i].len); |
| 383 | } |
| 384 | |
| 385 | #define FW_PACKET_MAX_DATA 57 /* 63-4-2, packet_size-header-checksum */ |
| 386 | |
| 387 | /* download all segments */ |
| 388 | for (i = 0; i < fw_hdr.segment_count; i++) { |
| 389 | pr_debug("%s: segment type=%d\n", __func__, |
| 390 | fw_hdr.segment[i].type); |
| 391 | if (fw_hdr.segment[i].type == SEGMENT_FW_DL) { |
| 392 | /* download begin packet */ |
| 393 | req.cmd = CMD_FW_DL_BEGIN; |
| 394 | ret = af9035_ctrl_msg(udev, &req); |
| 395 | if (ret < 0) { |
| 396 | pr_debug("%s: fw dl failed=%d\n", __func__, |
| 397 | ret); |
| 398 | goto err; |
| 399 | } |
| 400 | |
| 401 | packets = fw_hdr.segment[i].len / FW_PACKET_MAX_DATA; |
| 402 | remainder = fw_hdr.segment[i].len % FW_PACKET_MAX_DATA; |
| 403 | len = FW_PACKET_MAX_DATA; |
| 404 | for (j = 0; j <= packets; j++) { |
| 405 | if (j == packets) /* size of the last packet */ |
| 406 | len = remainder; |
| 407 | |
| 408 | req_fw_dl.wlen = len; |
| 409 | req_fw_dl.wbuf = fw_data_ptr; |
| 410 | ret = af9035_ctrl_msg(udev, &req_fw_dl); |
| 411 | if (ret < 0) { |
| 412 | pr_debug("%s: fw dl failed=%d " \ |
| 413 | "segment=%d " \ |
| 414 | "packet=%d\n", |
| 415 | __func__, ret, i, j); |
| 416 | goto err; |
| 417 | } |
| 418 | fw_data_ptr += len; |
| 419 | } |
| 420 | /* download end packet */ |
| 421 | req.cmd = CMD_FW_DL_END; |
| 422 | ret = af9035_ctrl_msg(udev, &req); |
| 423 | if (ret < 0) { |
| 424 | pr_debug("%s: fw dl failed=%d\n", __func__, |
| 425 | ret); |
| 426 | goto err; |
| 427 | } |
| 428 | } else { |
| 429 | pr_debug("%s: segment type=%d not implemented\n", |
| 430 | __func__, fw_hdr.segment[i].type); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* firmware loaded, request boot */ |
| 435 | req.cmd = CMD_FW_BOOT; |
| 436 | ret = af9035_ctrl_msg(udev, &req); |
| 437 | if (ret < 0) |
| 438 | goto err; |
| 439 | |
| 440 | /* ensure firmware starts */ |
| 441 | wbuf[0] = 1; |
| 442 | ret = af9035_ctrl_msg(udev, &req_fw_ver); |
| 443 | if (ret < 0) |
| 444 | goto err; |
| 445 | |
| 446 | pr_debug("%s: reply=%02x %02x %02x %02x\n", __func__, |
| 447 | rbuf[0], rbuf[1], rbuf[2], rbuf[3]); |
| 448 | |
| 449 | if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { |
| 450 | pr_debug("%s: fw did not run\n", __func__); |
| 451 | ret = -ENODEV; |
| 452 | goto err; |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | |
| 457 | err: |
| 458 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | /* abuse that callback as there is no better one for reading eeprom */ |
| 464 | static int af9035_read_mac_address(struct dvb_usb_device *d, u8 mac[6]) |
| 465 | { |
| 466 | int ret, i, eeprom_shift = 0; |
| 467 | u8 tmp; |
| 468 | u16 tmp16; |
| 469 | |
| 470 | /* check if there is dual tuners */ |
| 471 | ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp); |
| 472 | if (ret < 0) |
| 473 | goto err; |
| 474 | |
| 475 | af9035_config.dual_mode = tmp; |
| 476 | pr_debug("%s: dual mode=%d\n", __func__, af9035_config.dual_mode); |
| 477 | |
| 478 | for (i = 0; i < af9035_properties[0].num_adapters; i++) { |
| 479 | /* tuner */ |
| 480 | ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp); |
| 481 | if (ret < 0) |
| 482 | goto err; |
| 483 | |
| 484 | af9035_af9033_config[i].tuner = tmp; |
| 485 | pr_debug("%s: [%d]tuner=%02x\n", __func__, i, tmp); |
| 486 | |
| 487 | switch (tmp) { |
| 488 | case AF9033_TUNER_TUA9001: |
| 489 | af9035_af9033_config[i].spec_inv = 1; |
| 490 | break; |
| 491 | default: |
Antti Palosaari | 5a9abae | 2012-03-30 17:15:16 -0300 | [diff] [blame^] | 492 | af9035_config.hw_not_supported = true; |
| 493 | warn("tuner ID=%02x not supported, please report!", |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 494 | tmp); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | /* tuner IF frequency */ |
| 498 | ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp); |
| 499 | if (ret < 0) |
| 500 | goto err; |
| 501 | |
| 502 | tmp16 = tmp; |
| 503 | |
| 504 | ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp); |
| 505 | if (ret < 0) |
| 506 | goto err; |
| 507 | |
| 508 | tmp16 |= tmp << 8; |
| 509 | |
| 510 | pr_debug("%s: [%d]IF=%d\n", __func__, i, tmp16); |
| 511 | |
| 512 | eeprom_shift = 0x10; /* shift for the 2nd tuner params */ |
| 513 | } |
| 514 | |
| 515 | /* get demod clock */ |
| 516 | ret = af9035_rd_reg(d, 0x00d800, &tmp); |
| 517 | if (ret < 0) |
| 518 | goto err; |
| 519 | |
| 520 | tmp = (tmp >> 0) & 0x0f; |
| 521 | |
| 522 | for (i = 0; i < af9035_properties[0].num_adapters; i++) |
| 523 | af9035_af9033_config[i].clock = clock_lut[tmp]; |
| 524 | |
| 525 | return 0; |
| 526 | |
| 527 | err: |
| 528 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | static int af9035_frontend_attach(struct dvb_usb_adapter *adap) |
| 534 | { |
| 535 | int ret; |
| 536 | |
Antti Palosaari | 5a9abae | 2012-03-30 17:15:16 -0300 | [diff] [blame^] | 537 | if (af9035_config.hw_not_supported) { |
| 538 | ret = -ENODEV; |
| 539 | goto err; |
| 540 | } |
| 541 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 542 | if (adap->id == 0) { |
| 543 | ret = af9035_wr_reg(adap->dev, 0x00417f, |
| 544 | af9035_af9033_config[1].i2c_addr); |
| 545 | if (ret < 0) |
| 546 | goto err; |
| 547 | |
| 548 | ret = af9035_wr_reg(adap->dev, 0x00d81a, |
| 549 | af9035_config.dual_mode); |
| 550 | if (ret < 0) |
| 551 | goto err; |
| 552 | } |
| 553 | |
| 554 | /* attach demodulator */ |
| 555 | adap->fe_adap[0].fe = dvb_attach(af9033_attach, |
| 556 | &af9035_af9033_config[adap->id], &adap->dev->i2c_adap); |
| 557 | if (adap->fe_adap[0].fe == NULL) { |
| 558 | ret = -ENODEV; |
| 559 | goto err; |
| 560 | } |
| 561 | |
| 562 | return 0; |
| 563 | |
| 564 | err: |
| 565 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | static struct tua9001_config af9035_tua9001_config = { |
| 571 | .i2c_addr = 0x60, |
| 572 | }; |
| 573 | |
| 574 | static int af9035_tuner_attach(struct dvb_usb_adapter *adap) |
| 575 | { |
| 576 | int ret; |
| 577 | struct dvb_frontend *fe; |
| 578 | |
| 579 | switch (af9035_af9033_config[adap->id].tuner) { |
| 580 | case AF9033_TUNER_TUA9001: |
| 581 | /* AF9035 gpiot3 = TUA9001 RESETN |
| 582 | AF9035 gpiot2 = TUA9001 RXEN */ |
| 583 | |
| 584 | /* configure gpiot2 and gpiot2 as output */ |
| 585 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8ec, 0x01, 0x01); |
| 586 | if (ret < 0) |
| 587 | goto err; |
| 588 | |
| 589 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8ed, 0x01, 0x01); |
| 590 | if (ret < 0) |
| 591 | goto err; |
| 592 | |
| 593 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8e8, 0x01, 0x01); |
| 594 | if (ret < 0) |
| 595 | goto err; |
| 596 | |
| 597 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8e9, 0x01, 0x01); |
| 598 | if (ret < 0) |
| 599 | goto err; |
| 600 | |
| 601 | /* reset tuner */ |
| 602 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x00, 0x01); |
| 603 | if (ret < 0) |
| 604 | goto err; |
| 605 | |
| 606 | usleep_range(2000, 20000); |
| 607 | |
| 608 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x01, 0x01); |
| 609 | if (ret < 0) |
| 610 | goto err; |
| 611 | |
| 612 | /* activate tuner RX */ |
| 613 | /* TODO: use callback for TUA9001 RXEN */ |
| 614 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8eb, 0x01, 0x01); |
| 615 | if (ret < 0) |
| 616 | goto err; |
| 617 | |
| 618 | /* attach tuner */ |
| 619 | fe = dvb_attach(tua9001_attach, adap->fe_adap[0].fe, |
| 620 | &adap->dev->i2c_adap, &af9035_tua9001_config); |
| 621 | break; |
| 622 | default: |
| 623 | fe = NULL; |
| 624 | } |
| 625 | |
| 626 | if (fe == NULL) { |
| 627 | ret = -ENODEV; |
| 628 | goto err; |
| 629 | } |
| 630 | |
| 631 | return 0; |
| 632 | |
| 633 | err: |
| 634 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 635 | |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | enum af9035_id_entry { |
| 640 | AF9035_0CCD_0093, |
| 641 | }; |
| 642 | |
| 643 | static struct usb_device_id af9035_id[] = { |
| 644 | [AF9035_0CCD_0093] = { |
| 645 | USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK)}, |
| 646 | {}, |
| 647 | }; |
| 648 | |
| 649 | MODULE_DEVICE_TABLE(usb, af9035_id); |
| 650 | |
| 651 | static struct dvb_usb_device_properties af9035_properties[] = { |
| 652 | { |
| 653 | .caps = DVB_USB_IS_AN_I2C_ADAPTER, |
| 654 | |
| 655 | .usb_ctrl = DEVICE_SPECIFIC, |
| 656 | .download_firmware = af9035_download_firmware, |
| 657 | .firmware = "dvb-usb-af9035-01.fw", |
| 658 | .no_reconnect = 1, |
| 659 | |
| 660 | .num_adapters = 1, |
| 661 | .adapter = { |
| 662 | { |
| 663 | .num_frontends = 1, |
| 664 | .fe = { |
| 665 | { |
| 666 | .frontend_attach = af9035_frontend_attach, |
| 667 | .tuner_attach = af9035_tuner_attach, |
| 668 | .stream = { |
| 669 | .type = USB_BULK, |
| 670 | .count = 6, |
| 671 | .endpoint = 0x84, |
| 672 | .u = { |
| 673 | .bulk = { |
| 674 | .buffersize = (87 * 188), |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | }, |
| 682 | |
| 683 | .identify_state = af9035_identify_state, |
| 684 | .read_mac_address = af9035_read_mac_address, |
| 685 | |
| 686 | .i2c_algo = &af9035_i2c_algo, |
| 687 | |
| 688 | .num_device_descs = 1, |
| 689 | .devices = { |
| 690 | { |
| 691 | .name = "TerraTec Cinergy T Stick", |
| 692 | .cold_ids = { |
| 693 | &af9035_id[AF9035_0CCD_0093], |
| 694 | }, |
| 695 | }, |
| 696 | } |
| 697 | }, |
| 698 | }; |
| 699 | |
| 700 | static int af9035_usb_probe(struct usb_interface *intf, |
| 701 | const struct usb_device_id *id) |
| 702 | { |
| 703 | int ret, i; |
| 704 | struct dvb_usb_device *d = NULL; |
| 705 | struct usb_device *udev; |
| 706 | bool found; |
| 707 | |
| 708 | pr_debug("%s: interface=%d\n", __func__, |
| 709 | intf->cur_altsetting->desc.bInterfaceNumber); |
| 710 | |
| 711 | /* interface 0 is used by DVB-T receiver and |
| 712 | interface 1 is for remote controller (HID) */ |
| 713 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) |
| 714 | return 0; |
| 715 | |
| 716 | /* Dynamic USB ID support. Replaces first device ID with current one. */ |
| 717 | udev = interface_to_usbdev(intf); |
| 718 | |
| 719 | for (i = 0, found = false; i < ARRAY_SIZE(af9035_id) - 1; i++) { |
| 720 | if (af9035_id[i].idVendor == |
| 721 | le16_to_cpu(udev->descriptor.idVendor) && |
| 722 | af9035_id[i].idProduct == |
| 723 | le16_to_cpu(udev->descriptor.idProduct)) { |
| 724 | found = true; |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | if (!found) { |
| 730 | pr_debug("%s: using dynamic ID %04x:%04x\n", __func__, |
| 731 | le16_to_cpu(udev->descriptor.idVendor), |
| 732 | le16_to_cpu(udev->descriptor.idProduct)); |
| 733 | af9035_properties[0].devices[0].cold_ids[0]->idVendor = |
| 734 | le16_to_cpu(udev->descriptor.idVendor); |
| 735 | af9035_properties[0].devices[0].cold_ids[0]->idProduct = |
| 736 | le16_to_cpu(udev->descriptor.idProduct); |
| 737 | } |
| 738 | |
| 739 | |
| 740 | for (i = 0; i < af9035_properties_count; i++) { |
| 741 | ret = dvb_usb_device_init(intf, &af9035_properties[i], |
| 742 | THIS_MODULE, &d, adapter_nr); |
| 743 | |
| 744 | if (ret == -ENODEV) |
| 745 | continue; |
| 746 | else |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | if (ret < 0) |
| 751 | goto err; |
| 752 | |
| 753 | if (d) { |
| 754 | ret = af9035_init(d); |
| 755 | if (ret < 0) |
| 756 | goto err; |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | |
| 761 | err: |
| 762 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 763 | |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 768 | static struct usb_driver af9035_usb_driver = { |
| 769 | .name = "dvb_usb_af9035", |
| 770 | .probe = af9035_usb_probe, |
| 771 | .disconnect = dvb_usb_device_exit, |
| 772 | .id_table = af9035_id, |
| 773 | }; |
| 774 | |
| 775 | /* module stuff */ |
| 776 | static int __init af9035_usb_module_init(void) |
| 777 | { |
| 778 | int ret; |
| 779 | |
| 780 | ret = usb_register(&af9035_usb_driver); |
| 781 | if (ret < 0) |
| 782 | goto err; |
| 783 | |
| 784 | return 0; |
| 785 | |
| 786 | err: |
| 787 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 788 | |
| 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | static void __exit af9035_usb_module_exit(void) |
| 793 | { |
| 794 | /* deregister this driver from the USB subsystem */ |
| 795 | usb_deregister(&af9035_usb_driver); |
| 796 | } |
| 797 | |
| 798 | module_init(af9035_usb_module_init); |
| 799 | module_exit(af9035_usb_module_exit); |
| 800 | |
| 801 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 802 | MODULE_DESCRIPTION("Afatech AF9035 driver"); |
| 803 | MODULE_LICENSE("GPL"); |