--- zzzz-none-000/linux-3.10.107/drivers/media/usb/dvb-usb-v2/af9035.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/media/usb/dvb-usb-v2/af9035.c 2021-02-04 17:41:59.000000000 +0000 @@ -58,7 +58,7 @@ if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) || req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) { dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n", - __func__, req->wlen, req->rlen); + KBUILD_MODNAME, req->wlen, req->rlen); ret = -EINVAL; goto exit; } @@ -94,9 +94,10 @@ checksum = af9035_checksum(state->buf, rlen - 2); tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1]; if (tmp_checksum != checksum) { - dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \ - "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd, - tmp_checksum, checksum); + dev_err(&d->udev->dev, + "%s: command=%02x checksum mismatch (%04x != %04x)\n", + KBUILD_MODNAME, req->cmd, tmp_checksum, + checksum); ret = -EIO; goto exit; } @@ -192,6 +193,92 @@ return af9035_wr_regs(d, reg, &val, 1); } +static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type, + u8 addr, void *platform_data, struct i2c_adapter *adapter) +{ + int ret, num; + struct state *state = d_to_priv(d); + struct i2c_client *client; + struct i2c_board_info board_info = { + .addr = addr, + .platform_data = platform_data, + }; + + strlcpy(board_info.type, type, I2C_NAME_SIZE); + + /* find first free client */ + for (num = 0; num < AF9035_I2C_CLIENT_MAX; num++) { + if (state->i2c_client[num] == NULL) + break; + } + + dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num); + + if (num == AF9035_I2C_CLIENT_MAX) { + dev_err(&d->udev->dev, "%s: I2C client out of index\n", + KBUILD_MODNAME); + ret = -ENODEV; + goto err; + } + + request_module("%s", board_info.type); + + /* register I2C device */ + client = i2c_new_device(adapter, &board_info); + if (client == NULL || client->dev.driver == NULL) { + ret = -ENODEV; + goto err; + } + + /* increase I2C driver usage count */ + if (!try_module_get(client->dev.driver->owner)) { + i2c_unregister_device(client); + ret = -ENODEV; + goto err; + } + + state->i2c_client[num] = client; + return 0; +err: + dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); + return ret; +} + +static void af9035_del_i2c_dev(struct dvb_usb_device *d) +{ + int num; + struct state *state = d_to_priv(d); + struct i2c_client *client; + + /* find last used client */ + num = AF9035_I2C_CLIENT_MAX; + while (num--) { + if (state->i2c_client[num] != NULL) + break; + } + + dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num); + + if (num == -1) { + dev_err(&d->udev->dev, "%s: I2C client out of index\n", + KBUILD_MODNAME); + goto err; + } + + client = state->i2c_client[num]; + + /* decrease I2C driver usage count */ + module_put(client->dev.driver->owner); + + /* unregister I2C device */ + i2c_unregister_device(client); + + state->i2c_client[num] = NULL; + return; +err: + dev_dbg(&d->udev->dev, "%s: failed\n", __func__); +} + static int af9035_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num) { @@ -203,7 +290,7 @@ return -EAGAIN; /* - * I2C sub header is 5 bytes long. Meaning of those bytes are: + * AF9035 I2C sub header is 5 bytes long. Meaning of those bytes are: * 0: data len * 1: I2C addr << 1 * 2: reg addr len @@ -217,92 +304,156 @@ * NOTE: As a firmware knows tuner type there is very small possibility * there could be some tuner I2C hacks done by firmware and this may * lead problems if firmware expects those bytes are used. + * + * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator. + * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual + * tuner devices, there is also external AF9033 demodulator connected + * via external I2C bus. All AF9033 demod I2C traffic, both single and + * dual tuner configuration, is covered by firmware - actual USB IO + * looks just like a memory access. + * In case of IT913x chip, there is own tuner driver. It is implemented + * currently as a I2C driver, even tuner IP block is likely build + * directly into the demodulator memory space and there is no own I2C + * bus. I2C subsystem does not allow register multiple devices to same + * bus, having same slave address. Due to that we reuse demod address, + * shifted by one bit, on that case. + * + * For IT930x we use a different command and the sub header is + * different as well: + * 0: data len + * 1: I2C bus (0x03 seems to be only value used) + * 2: I2C addr << 1 */ - if (num == 2 && !(msg[0].flags & I2C_M_RD) && - (msg[1].flags & I2C_M_RD)) { +#define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \ + (_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD)) +#define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \ + (_num == 1 && !(_msg[0].flags & I2C_M_RD)) +#define AF9035_IS_I2C_XFER_READ(_msg, _num) \ + (_num == 1 && (_msg[0].flags & I2C_M_RD)) + + if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) { if (msg[0].len > 40 || msg[1].len > 40) { /* TODO: correct limits > 40 */ ret = -EOPNOTSUPP; - } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) || - (msg[0].addr == state->af9033_config[1].i2c_addr)) { + } else if ((msg[0].addr == state->af9033_i2c_addr[0]) || + (msg[0].addr == state->af9033_i2c_addr[1]) || + (state->chip_type == 0x9135)) { /* demod access via firmware interface */ u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | msg[0].buf[2]; - if (msg[0].addr == state->af9033_config[1].i2c_addr) + if (msg[0].addr == state->af9033_i2c_addr[1] || + msg[0].addr == (state->af9033_i2c_addr[1] >> 1)) reg |= 0x100000; ret = af9035_rd_regs(d, reg, &msg[1].buf[0], msg[1].len); } else { - /* I2C */ + /* I2C write + read */ u8 buf[MAX_XFER_SIZE]; struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len, buf, msg[1].len, msg[1].buf }; - if (5 + msg[0].len > sizeof(buf)) { - dev_warn(&d->udev->dev, - "%s: i2c xfer: len=%d is too big!\n", - KBUILD_MODNAME, msg[0].len); - ret = -EOPNOTSUPP; - goto unlock; + if (state->chip_type == 0x9306) { + req.cmd = CMD_GENERIC_I2C_RD; + req.wlen = 3 + msg[0].len; } req.mbox |= ((msg[0].addr & 0x80) >> 3); + buf[0] = msg[1].len; - buf[1] = msg[0].addr << 1; - buf[2] = 0x00; /* reg addr len */ - buf[3] = 0x00; /* reg addr MSB */ - buf[4] = 0x00; /* reg addr LSB */ - memcpy(&buf[5], msg[0].buf, msg[0].len); + if (state->chip_type == 0x9306) { + buf[1] = 0x03; /* I2C bus */ + buf[2] = msg[0].addr << 1; + memcpy(&buf[3], msg[0].buf, msg[0].len); + } else { + buf[1] = msg[0].addr << 1; + buf[2] = 0x00; /* reg addr len */ + buf[3] = 0x00; /* reg addr MSB */ + buf[4] = 0x00; /* reg addr LSB */ + memcpy(&buf[5], msg[0].buf, msg[0].len); + } ret = af9035_ctrl_msg(d, &req); } - } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { + } else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) { if (msg[0].len > 40) { /* TODO: correct limits > 40 */ ret = -EOPNOTSUPP; - } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) || - (msg[0].addr == state->af9033_config[1].i2c_addr)) { + } else if ((msg[0].addr == state->af9033_i2c_addr[0]) || + (msg[0].addr == state->af9033_i2c_addr[1]) || + (state->chip_type == 0x9135)) { /* demod access via firmware interface */ u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | msg[0].buf[2]; - if (msg[0].addr == state->af9033_config[1].i2c_addr) + if (msg[0].addr == state->af9033_i2c_addr[1] || + msg[0].addr == (state->af9033_i2c_addr[1] >> 1)) reg |= 0x100000; ret = af9035_wr_regs(d, reg, &msg[0].buf[3], msg[0].len - 3); } else { - /* I2C */ + /* I2C write */ u8 buf[MAX_XFER_SIZE]; struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len, buf, 0, NULL }; - if (5 + msg[0].len > sizeof(buf)) { - dev_warn(&d->udev->dev, - "%s: i2c xfer: len=%d is too big!\n", - KBUILD_MODNAME, msg[0].len); - ret = -EOPNOTSUPP; - goto unlock; + if (state->chip_type == 0x9306) { + req.cmd = CMD_GENERIC_I2C_WR; + req.wlen = 3 + msg[0].len; + } + + req.mbox |= ((msg[0].addr & 0x80) >> 3); + buf[0] = msg[0].len; + if (state->chip_type == 0x9306) { + buf[1] = 0x03; /* I2C bus */ + buf[2] = msg[0].addr << 1; + memcpy(&buf[3], msg[0].buf, msg[0].len); + } else { + buf[1] = msg[0].addr << 1; + buf[2] = 0x00; /* reg addr len */ + buf[3] = 0x00; /* reg addr MSB */ + buf[4] = 0x00; /* reg addr LSB */ + memcpy(&buf[5], msg[0].buf, msg[0].len); + } + ret = af9035_ctrl_msg(d, &req); + } + } else if (AF9035_IS_I2C_XFER_READ(msg, num)) { + if (msg[0].len > 40) { + /* TODO: correct limits > 40 */ + ret = -EOPNOTSUPP; + } else { + /* I2C read */ + u8 buf[5]; + struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf), + buf, msg[0].len, msg[0].buf }; + + if (state->chip_type == 0x9306) { + req.cmd = CMD_GENERIC_I2C_RD; + req.wlen = 3; } req.mbox |= ((msg[0].addr & 0x80) >> 3); buf[0] = msg[0].len; - buf[1] = msg[0].addr << 1; - buf[2] = 0x00; /* reg addr len */ - buf[3] = 0x00; /* reg addr MSB */ - buf[4] = 0x00; /* reg addr LSB */ - memcpy(&buf[5], msg[0].buf, msg[0].len); + if (state->chip_type == 0x9306) { + buf[1] = 0x03; /* I2C bus */ + buf[2] = msg[0].addr << 1; + } else { + buf[1] = msg[0].addr << 1; + buf[2] = 0x00; /* reg addr len */ + buf[3] = 0x00; /* reg addr MSB */ + buf[4] = 0x00; /* reg addr LSB */ + } ret = af9035_ctrl_msg(d, &req); } } else { /* - * We support only two kind of I2C transactions: - * 1) 1 x read + 1 x write + * We support only three kind of I2C transactions: + * 1) 1 x write + 1 x read (repeated start) * 2) 1 x write + * 3) 1 x read */ ret = -EOPNOTSUPP; } -unlock: mutex_unlock(&d->i2c_mutex); if (ret < 0) @@ -343,8 +494,8 @@ dev_info(&d->udev->dev, "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n", - __func__, state->prechip_version, state->chip_version, - state->chip_type); + KBUILD_MODNAME, state->prechip_version, + state->chip_version, state->chip_type); if (state->chip_type == 0x9135) { if (state->chip_version == 0x02) @@ -352,6 +503,9 @@ else *name = AF9035_FIRMWARE_IT9135_V1; state->eeprom_addr = EEPROM_BASE_IT9135; + } else if (state->chip_type == 0x9306) { + *name = AF9035_FIRMWARE_IT9303; + state->eeprom_addr = EEPROM_BASE_IT9135; } else { *name = AF9035_FIRMWARE_AF9035; state->eeprom_addr = EEPROM_BASE_AF9035; @@ -408,9 +562,10 @@ hdr_checksum = fw->data[fw->size - i + 5] << 8; hdr_checksum |= fw->data[fw->size - i + 6] << 0; - dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \ - "checksum=%04x\n", __func__, hdr_core, hdr_addr, - hdr_data_len, hdr_checksum); + dev_dbg(&d->udev->dev, + "%s: core=%d addr=%04x data_len=%d checksum=%04x\n", + __func__, hdr_core, hdr_addr, hdr_data_len, + hdr_checksum); if (((hdr_core != 1) && (hdr_core != 2)) || (hdr_data_len > i)) { @@ -515,7 +670,8 @@ u8 rbuf[4]; u8 tmp; struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; - struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; + struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf }; + dev_dbg(&d->udev->dev, "%s:\n", __func__); /* @@ -524,11 +680,11 @@ * which is done by master demod. * Master feeds also clock and controls power via GPIO. */ - ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp); + ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp); if (ret < 0) goto err; - if (tmp) { + if (tmp == 1 || tmp == 3) { /* configure gpioh1, reset & power slave demod */ ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01); if (ret < 0) @@ -555,7 +711,12 @@ if (ret < 0) goto err; - if (state->chip_type == 0x9135) { + /* use default I2C address if eeprom has no address set */ + if (!tmp) + tmp = 0x3a; + + if ((state->chip_type == 0x9135) || + (state->chip_type == 0x9306)) { ret = af9035_wr_reg(d, 0x004bfb, tmp); if (ret < 0) goto err; @@ -616,22 +777,26 @@ u16 tmp16, addr; /* demod I2C "address" */ - state->af9033_config[0].i2c_addr = 0x38; + state->af9033_i2c_addr[0] = 0x38; + state->af9033_i2c_addr[1] = 0x3a; state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X; state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X; state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB; state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL; - /* eeprom memory mapped location */ if (state->chip_type == 0x9135) { + /* feed clock for integrated RF tuner */ + state->af9033_config[0].dyn0_clk = true; + state->af9033_config[1].dyn0_clk = true; + if (state->chip_version == 0x02) { state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60; state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60; - tmp16 = 0x00461d; + tmp16 = 0x00461d; /* eeprom memory mapped location */ } else { state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38; state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38; - tmp16 = 0x00461b; + tmp16 = 0x00461b; /* eeprom memory mapped location */ } /* check if eeprom exists */ @@ -643,16 +808,26 @@ dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__); goto skip_eeprom; } + } else if (state->chip_type == 0x9306) { + /* + * IT930x is an USB bridge, only single demod-single tuner + * configurations seen so far. + */ + return 0; } + + /* check if there is dual tuners */ - ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp); + ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp); if (ret < 0) goto err; - state->dual_mode = tmp; - dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__, - state->dual_mode); + if (tmp == 1 || tmp == 3) + state->dual_mode = true; + + dev_dbg(&d->udev->dev, "%s: ts mode=%d dual mode=%d\n", __func__, + tmp, state->dual_mode); if (state->dual_mode) { /* read 2nd demodulator I2C address */ @@ -662,7 +837,9 @@ if (ret < 0) goto err; - state->af9033_config[1].i2c_addr = tmp; + if (tmp) + state->af9033_i2c_addr[1] = tmp; + dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n", __func__, tmp); } @@ -675,15 +852,41 @@ if (ret < 0) goto err; - if (tmp == 0x00) - dev_dbg(&d->udev->dev, - "%s: [%d]tuner not set, using default\n", - __func__, i); - else + dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n", + __func__, i, tmp); + + /* tuner sanity check */ + if (state->chip_type == 0x9135) { + if (state->chip_version == 0x02) { + /* IT9135 BX (v2) */ + switch (tmp) { + case AF9033_TUNER_IT9135_60: + case AF9033_TUNER_IT9135_61: + case AF9033_TUNER_IT9135_62: + state->af9033_config[i].tuner = tmp; + break; + } + } else { + /* IT9135 AX (v1) */ + switch (tmp) { + case AF9033_TUNER_IT9135_38: + case AF9033_TUNER_IT9135_51: + case AF9033_TUNER_IT9135_52: + state->af9033_config[i].tuner = tmp; + break; + } + } + } else { + /* AF9035 */ state->af9033_config[i].tuner = tmp; + } - dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n", - __func__, i, state->af9033_config[i].tuner); + if (state->af9033_config[i].tuner != tmp) { + dev_info(&d->udev->dev, + "%s: [%d] overriding tuner from %02x to %02x\n", + KBUILD_MODNAME, i, tmp, + state->af9033_config[i].tuner); + } switch (state->af9033_config[i].tuner) { case AF9033_TUNER_TUA9001: @@ -917,11 +1120,7 @@ { struct state *state = d_to_priv(d); - /* disable 2nd adapter as we don't have PID filters implemented */ - if (d->udev->speed == USB_SPEED_FULL) - return 1; - else - return state->dual_mode + 1; + return state->dual_mode + 1; } static int af9035_frontend_attach(struct dvb_usb_adapter *adap) @@ -929,7 +1128,8 @@ struct state *state = adap_to_priv(adap); struct dvb_usb_device *d = adap_to_d(adap); int ret; - dev_dbg(&d->udev->dev, "%s:\n", __func__); + + dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); if (!state->af9033_config[adap->id].tuner) { /* unsupported tuner */ @@ -937,9 +1137,13 @@ goto err; } - /* attach demodulator */ - adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id], - &d->i2c_adap); + state->af9033_config[adap->id].fe = &adap->fe[0]; + state->af9033_config[adap->id].ops = &state->ops; + ret = af9035_add_i2c_dev(d, "af9033", state->af9033_i2c_addr[adap->id], + &state->af9033_config[adap->id], &d->i2c_adap); + if (ret) + goto err; + if (adap->fe[0] == NULL) { ret = -ENODEV; goto err; @@ -957,9 +1161,78 @@ return ret; } -static struct tua9001_config af9035_tua9001_config = { - .i2c_addr = 0x60, -}; +static int it930x_frontend_attach(struct dvb_usb_adapter *adap) +{ + struct state *state = adap_to_priv(adap); + struct dvb_usb_device *d = adap_to_d(adap); + int ret; + struct si2168_config si2168_config; + struct i2c_adapter *adapter; + + dev_dbg(&d->udev->dev, "adap->id=%d\n", adap->id); + + memset(&si2168_config, 0, sizeof(si2168_config)); + si2168_config.i2c_adapter = &adapter; + si2168_config.fe = &adap->fe[0]; + si2168_config.ts_mode = SI2168_TS_SERIAL; + + state->af9033_config[adap->id].fe = &adap->fe[0]; + state->af9033_config[adap->id].ops = &state->ops; + ret = af9035_add_i2c_dev(d, "si2168", 0x67, &si2168_config, + &d->i2c_adap); + if (ret) + goto err; + + if (adap->fe[0] == NULL) { + ret = -ENODEV; + goto err; + } + state->i2c_adapter_demod = adapter; + + return 0; + +err: + dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); + + return ret; +} + +static int af9035_frontend_detach(struct dvb_usb_adapter *adap) +{ + struct state *state = adap_to_priv(adap); + struct dvb_usb_device *d = adap_to_d(adap); + int demod2; + + dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); + + /* + * For dual tuner devices we have to resolve 2nd demod client, as there + * is two different kind of tuner drivers; one is using I2C binding + * and the other is using DVB attach/detach binding. + */ + switch (state->af9033_config[adap->id].tuner) { + case AF9033_TUNER_IT9135_38: + case AF9033_TUNER_IT9135_51: + case AF9033_TUNER_IT9135_52: + case AF9033_TUNER_IT9135_60: + case AF9033_TUNER_IT9135_61: + case AF9033_TUNER_IT9135_62: + demod2 = 2; + break; + default: + demod2 = 1; + } + + if (adap->id == 1) { + if (state->i2c_client[demod2]) + af9035_del_i2c_dev(d); + } else if (adap->id == 0) { + if (state->i2c_client[0]) + af9035_del_i2c_dev(d); + } + + return 0; +} static const struct fc0011_config af9035_fc0011_config = { .i2c_address = 0x60, @@ -988,11 +1261,6 @@ .i2c_wr_max = 21, }; -static const struct fc2580_config af9035_fc2580_config = { - .i2c_addr = 0x56, - .clock = 16384000, -}; - static const struct fc0012_config af9035_fc0012_config[] = { { .i2c_address = 0x63, @@ -1015,7 +1283,8 @@ struct dvb_frontend *fe; struct i2c_msg msg[1]; u8 tuner_addr; - dev_dbg(&d->udev->dev, "%s:\n", __func__); + + dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); /* * XXX: Hack used in that function: we abuse unused I2C address bit [7] @@ -1023,9 +1292,15 @@ */ switch (state->af9033_config[adap->id].tuner) { - case AF9033_TUNER_TUA9001: - /* AF9035 gpiot3 = TUA9001 RESETN - AF9035 gpiot2 = TUA9001 RXEN */ + case AF9033_TUNER_TUA9001: { + struct tua9001_platform_data tua9001_pdata = { + .dvb_frontend = adap->fe[0], + }; + + /* + * AF9035 gpiot3 = TUA9001 RESETN + * AF9035 gpiot2 = TUA9001 RXEN + */ /* configure gpiot2 and gpiot2 as output */ ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01); @@ -1045,9 +1320,14 @@ goto err; /* attach tuner */ - fe = dvb_attach(tua9001_attach, adap->fe[0], - &d->i2c_adap, &af9035_tua9001_config); + ret = af9035_add_i2c_dev(d, "tua9001", 0x60, &tua9001_pdata, + &d->i2c_adap); + if (ret) + goto err; + + fe = adap->fe[0]; break; + } case AF9033_TUNER_FC0011: fe = dvb_attach(fc0011_attach, adap->fe[0], &d->i2c_adap, &af9035_fc0011_config); @@ -1112,7 +1392,11 @@ fe = dvb_attach(tda18218_attach, adap->fe[0], &d->i2c_adap, &af9035_tda18218_config); break; - case AF9033_TUNER_FC2580: + case AF9033_TUNER_FC2580: { + struct fc2580_platform_data fc2580_pdata = { + .dvb_frontend = adap->fe[0], + }; + /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */ ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01); if (ret < 0) @@ -1128,9 +1412,14 @@ usleep_range(10000, 50000); /* attach tuner */ - fe = dvb_attach(fc2580_attach, adap->fe[0], - &d->i2c_adap, &af9035_fc2580_config); + ret = af9035_add_i2c_dev(d, "fc2580", 0x56, &fc2580_pdata, + &d->i2c_adap); + if (ret) + goto err; + + fe = adap->fe[0]; break; + } case AF9033_TUNER_FC0012: /* * AF9035 gpiot2 = FC0012 enable @@ -1174,14 +1463,53 @@ case AF9033_TUNER_IT9135_38: case AF9033_TUNER_IT9135_51: case AF9033_TUNER_IT9135_52: + { + struct it913x_config it913x_config = { + .fe = adap->fe[0], + .chip_ver = 1, + }; + + if (state->dual_mode) { + if (adap->id == 0) + it913x_config.role = IT913X_ROLE_DUAL_MASTER; + else + it913x_config.role = IT913X_ROLE_DUAL_SLAVE; + } + + ret = af9035_add_i2c_dev(d, "it913x", + state->af9033_i2c_addr[adap->id] >> 1, + &it913x_config, &d->i2c_adap); + if (ret) + goto err; + + fe = adap->fe[0]; + break; + } case AF9033_TUNER_IT9135_60: case AF9033_TUNER_IT9135_61: case AF9033_TUNER_IT9135_62: - /* attach tuner */ - fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap, - state->af9033_config[adap->id].i2c_addr, - state->af9033_config[0].tuner); + { + struct it913x_config it913x_config = { + .fe = adap->fe[0], + .chip_ver = 2, + }; + + if (state->dual_mode) { + if (adap->id == 0) + it913x_config.role = IT913X_ROLE_DUAL_MASTER; + else + it913x_config.role = IT913X_ROLE_DUAL_SLAVE; + } + + ret = af9035_add_i2c_dev(d, "it913x", + state->af9033_i2c_addr[adap->id] >> 1, + &it913x_config, &d->i2c_adap); + if (ret) + goto err; + + fe = adap->fe[0]; break; + } default: fe = NULL; } @@ -1199,6 +1527,122 @@ return ret; } +static int it930x_tuner_attach(struct dvb_usb_adapter *adap) +{ + struct state *state = adap_to_priv(adap); + struct dvb_usb_device *d = adap_to_d(adap); + int ret; + struct si2157_config si2157_config; + + dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); + + /* I2C master bus 2 clock speed 300k */ + ret = af9035_wr_reg(d, 0x00f6a7, 0x07); + if (ret < 0) + goto err; + + /* I2C master bus 1,3 clock speed 300k */ + ret = af9035_wr_reg(d, 0x00f103, 0x07); + if (ret < 0) + goto err; + + /* set gpio11 low */ + ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01); + if (ret < 0) + goto err; + + ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01); + if (ret < 0) + goto err; + + ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01); + if (ret < 0) + goto err; + + /* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */ + ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01); + if (ret < 0) + goto err; + + ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01); + if (ret < 0) + goto err; + + ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01); + if (ret < 0) + goto err; + + msleep(200); + + ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01); + if (ret < 0) + goto err; + + memset(&si2157_config, 0, sizeof(si2157_config)); + si2157_config.fe = adap->fe[0]; + si2157_config.if_port = 1; + ret = af9035_add_i2c_dev(d, "si2157", 0x63, + &si2157_config, state->i2c_adapter_demod); + + if (ret) + goto err; + + return 0; + +err: + dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); + + return ret; +} + + +static int it930x_tuner_detach(struct dvb_usb_adapter *adap) +{ + struct state *state = adap_to_priv(adap); + struct dvb_usb_device *d = adap_to_d(adap); + + dev_dbg(&d->udev->dev, "adap->id=%d\n", adap->id); + + if (adap->id == 1) { + if (state->i2c_client[3]) + af9035_del_i2c_dev(d); + } else if (adap->id == 0) { + if (state->i2c_client[1]) + af9035_del_i2c_dev(d); + } + + return 0; +} + + +static int af9035_tuner_detach(struct dvb_usb_adapter *adap) +{ + struct state *state = adap_to_priv(adap); + struct dvb_usb_device *d = adap_to_d(adap); + + dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); + + switch (state->af9033_config[adap->id].tuner) { + case AF9033_TUNER_TUA9001: + case AF9033_TUNER_FC2580: + case AF9033_TUNER_IT9135_38: + case AF9033_TUNER_IT9135_51: + case AF9033_TUNER_IT9135_52: + case AF9033_TUNER_IT9135_60: + case AF9033_TUNER_IT9135_61: + case AF9033_TUNER_IT9135_62: + if (adap->id == 1) { + if (state->i2c_client[3]) + af9035_del_i2c_dev(d); + } else if (adap->id == 0) { + if (state->i2c_client[1]) + af9035_del_i2c_dev(d); + } + } + + return 0; +} + static int af9035_init(struct dvb_usb_device *d) { struct state *state = d_to_priv(d); @@ -1226,9 +1670,9 @@ { 0x80f9a4, 0x00, 0x01 }, }; - dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \ - "packet_size=%02x\n", __func__, - d->udev->speed, frame_size, packet_size); + dev_dbg(&d->udev->dev, + "%s: USB speed=%d frame_size=%04x packet_size=%02x\n", + __func__, d->udev->speed, frame_size, packet_size); /* init endpoints */ for (i = 0; i < ARRAY_SIZE(tab); i++) { @@ -1246,6 +1690,89 @@ return ret; } +static int it930x_init(struct dvb_usb_device *d) +{ + struct state *state = d_to_priv(d); + int ret, i; + u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4; + u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4; + struct reg_val_mask tab[] = { + { 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */ + { 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */ + { 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */ + { 0x00f41a, 0x01, 0x01 }, /* dvbt_en */ + { 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */ + { 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */ + { 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */ + { 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */ + { 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */ + { 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */ + { 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */ + { 0x00dd88, (frame_size >> 0) & 0xff, 0xff}, + { 0x00dd89, (frame_size >> 8) & 0xff, 0xff}, + { 0x00dd0c, packet_size, 0xff}, + { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff}, + { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff}, + { 0x00dd0d, packet_size, 0xff }, + { 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */ + { 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */ + { 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */ + { 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */ + { 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */ + + /* suspend gpio1 for TS-C */ + { 0x00d8b0, 0x01, 0xff }, /* gpio1 */ + { 0x00d8b1, 0x01, 0xff }, /* gpio1 */ + { 0x00d8af, 0x00, 0xff }, /* gpio1 */ + + /* suspend gpio7 for TS-D */ + { 0x00d8c4, 0x01, 0xff }, /* gpio7 */ + { 0x00d8c5, 0x01, 0xff }, /* gpio7 */ + { 0x00d8c3, 0x00, 0xff }, /* gpio7 */ + + /* suspend gpio13 for TS-B */ + { 0x00d8dc, 0x01, 0xff }, /* gpio13 */ + { 0x00d8dd, 0x01, 0xff }, /* gpio13 */ + { 0x00d8db, 0x00, 0xff }, /* gpio13 */ + + /* suspend gpio14 for TS-E */ + { 0x00d8e4, 0x01, 0xff }, /* gpio14 */ + { 0x00d8e5, 0x01, 0xff }, /* gpio14 */ + { 0x00d8e3, 0x00, 0xff }, /* gpio14 */ + + /* suspend gpio15 for TS-A */ + { 0x00d8e8, 0x01, 0xff }, /* gpio15 */ + { 0x00d8e9, 0x01, 0xff }, /* gpio15 */ + { 0x00d8e7, 0x00, 0xff }, /* gpio15 */ + + { 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */ + { 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */ + { 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */ + { 0x00da4c, 0x01, 0xff }, /* ts0_en */ + { 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */ + }; + + dev_dbg(&d->udev->dev, + "%s: USB speed=%d frame_size=%04x packet_size=%02x\n", + __func__, d->udev->speed, frame_size, packet_size); + + /* init endpoints */ + for (i = 0; i < ARRAY_SIZE(tab); i++) { + ret = af9035_wr_reg_mask(d, tab[i].reg, + tab[i].val, tab[i].mask); + + if (ret < 0) + goto err; + } + + return 0; +err: + dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); + + return ret; +} + + #if IS_ENABLED(CONFIG_RC_CORE) static int af9035_rc_query(struct dvb_usb_device *d) { @@ -1263,19 +1790,20 @@ if ((buf[2] + buf[3]) == 0xff) { if ((buf[0] + buf[1]) == 0xff) { /* NEC standard 16bit */ - key = buf[0] << 8 | buf[2]; + key = RC_SCANCODE_NEC(buf[0], buf[2]); } else { /* NEC extended 24bit */ - key = buf[0] << 16 | buf[1] << 8 | buf[2]; + key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]); } } else { /* NEC full code 32bit */ - key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]; + key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 | + buf[2] << 8 | buf[3]); } dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf); - rc_keydown(d->rc_dev, key, 0); + rc_keydown(d->rc_dev, RC_TYPE_NEC, key, 0); return 0; @@ -1339,6 +1867,7 @@ struct usb_data_stream_properties *stream) { struct dvb_usb_device *d = fe_to_d(fe); + dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id); if (d->udev->speed == USB_SPEED_FULL) @@ -1347,58 +1876,19 @@ return 0; } -/* - * FIXME: PID filter is property of demodulator and should be moved to the - * correct driver. Also we support only adapter #0 PID filter and will - * disable adapter #1 if USB1.1 is used. - */ static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff) { - struct dvb_usb_device *d = adap_to_d(adap); - int ret; - - dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff); - - ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01); - if (ret < 0) - goto err; - - return 0; - -err: - dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); + struct state *state = adap_to_priv(adap); - return ret; + return state->ops.pid_filter_ctrl(adap->fe[0], onoff); } static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff) { - struct dvb_usb_device *d = adap_to_d(adap); - int ret; - u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff}; - - dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n", - __func__, index, pid, onoff); - - ret = af9035_wr_regs(d, 0x80f996, wbuf, 2); - if (ret < 0) - goto err; - - ret = af9035_wr_reg(d, 0x80f994, onoff); - if (ret < 0) - goto err; - - ret = af9035_wr_reg(d, 0x80f995, index); - if (ret < 0) - goto err; - - return 0; - -err: - dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); + struct state *state = adap_to_priv(adap); - return ret; + return state->ops.pid_filter(adap->fe[0], index, pid, onoff); } static int af9035_probe(struct usb_interface *intf, @@ -1455,7 +1945,9 @@ .i2c_algo = &af9035_i2c_algo, .read_config = af9035_read_config, .frontend_attach = af9035_frontend_attach, + .frontend_detach = af9035_frontend_detach, .tuner_attach = af9035_tuner_attach, + .tuner_detach = af9035_tuner_detach, .init = af9035_init, .get_rc_config = af9035_get_rc_config, .get_stream_config = af9035_get_stream_config, @@ -1472,11 +1964,49 @@ .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188), }, { + .caps = DVB_USB_ADAP_HAS_PID_FILTER | + DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, + + .pid_filter_count = 32, + .pid_filter_ctrl = af9035_pid_filter_ctrl, + .pid_filter = af9035_pid_filter, + .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188), }, }, }; +static const struct dvb_usb_device_properties it930x_props = { + .driver_name = KBUILD_MODNAME, + .owner = THIS_MODULE, + .adapter_nr = adapter_nr, + .size_of_priv = sizeof(struct state), + + .generic_bulk_ctrl_endpoint = 0x02, + .generic_bulk_ctrl_endpoint_response = 0x81, + + .identify_state = af9035_identify_state, + .download_firmware = af9035_download_firmware, + + .i2c_algo = &af9035_i2c_algo, + .read_config = af9035_read_config, + .frontend_attach = it930x_frontend_attach, + .frontend_detach = af9035_frontend_detach, + .tuner_attach = it930x_tuner_attach, + .tuner_detach = it930x_tuner_detach, + .init = it930x_init, + .get_stream_config = af9035_get_stream_config, + + .get_adapter_count = af9035_get_adapter_count, + .adapter = { + { + .stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188), + }, { + .stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188), + }, + }, +}; + static const struct usb_device_id af9035_id_table[] = { /* AF9035 devices */ { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035, @@ -1503,22 +2033,52 @@ &af9035_props, "AVerMedia Twinstar (A825)", NULL) }, { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS, &af9035_props, "Asus U3100Mini Plus", NULL) }, - { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa, + { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa, &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) }, + { DVB_USB_DEVICE(USB_VID_AVERMEDIA, 0x0337, + &af9035_props, "AVerMedia HD Volar (A867)", NULL) }, + /* IT9135 devices */ -#if 0 - { DVB_USB_DEVICE(0x048d, 0x9135, - &af9035_props, "IT9135 reference design", NULL) }, - { DVB_USB_DEVICE(0x048d, 0x9006, - &af9035_props, "IT9135 reference design", NULL) }, -#endif + { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135, + &af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) }, + { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005, + &af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) }, + { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006, + &af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) }, + { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835, + &af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) }, + { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835, + &af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) }, + { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835, + &af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) }, + { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835, + &af9035_props, "Avermedia A835B(4835)", RC_MAP_IT913X_V2) }, + { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335, + &af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) }, + { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09, + &af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) }, + { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137, + &af9035_props, "Sveon STV22 Dual DVB-T HDTV", + RC_MAP_IT913X_V1) }, + { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2, + &af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2", + RC_MAP_IT913X_V1) }, /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */ { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099, - &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) }, + &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", + NULL) }, { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05, &af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) }, { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900, &af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) }, + { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E, + &af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) }, + { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E, + &af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) }, + + /* IT930x devices */ + { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303, + &it930x_props, "ITE 9303 Generic", NULL) }, { } }; MODULE_DEVICE_TABLE(usb, af9035_id_table); @@ -1543,3 +2103,4 @@ MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035); MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1); MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2); +MODULE_FIRMWARE(AF9035_FIRMWARE_IT9303);