1786baecfSMauro Carvalho Chehab /* 2786baecfSMauro Carvalho Chehab * Afatech AF9035 DVB USB driver 3786baecfSMauro Carvalho Chehab * 4786baecfSMauro Carvalho Chehab * Copyright (C) 2009 Antti Palosaari <crope@iki.fi> 5786baecfSMauro Carvalho Chehab * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> 6786baecfSMauro Carvalho Chehab * 7786baecfSMauro Carvalho Chehab * This program is free software; you can redistribute it and/or modify 8786baecfSMauro Carvalho Chehab * it under the terms of the GNU General Public License as published by 9786baecfSMauro Carvalho Chehab * the Free Software Foundation; either version 2 of the License, or 10786baecfSMauro Carvalho Chehab * (at your option) any later version. 11786baecfSMauro Carvalho Chehab * 12786baecfSMauro Carvalho Chehab * This program is distributed in the hope that it will be useful, 13786baecfSMauro Carvalho Chehab * but WITHOUT ANY WARRANTY; without even the implied warranty of 14786baecfSMauro Carvalho Chehab * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15786baecfSMauro Carvalho Chehab * GNU General Public License for more details. 16786baecfSMauro Carvalho Chehab * 17786baecfSMauro Carvalho Chehab * You should have received a copy of the GNU General Public License along 18786baecfSMauro Carvalho Chehab * with this program; if not, write to the Free Software Foundation, Inc., 19786baecfSMauro Carvalho Chehab * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20786baecfSMauro Carvalho Chehab */ 21786baecfSMauro Carvalho Chehab 22786baecfSMauro Carvalho Chehab #include "af9035.h" 23786baecfSMauro Carvalho Chehab 24786baecfSMauro Carvalho Chehab DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 25786baecfSMauro Carvalho Chehab 26786baecfSMauro Carvalho Chehab static u16 af9035_checksum(const u8 *buf, size_t len) 27786baecfSMauro Carvalho Chehab { 28786baecfSMauro Carvalho Chehab size_t i; 29786baecfSMauro Carvalho Chehab u16 checksum = 0; 30786baecfSMauro Carvalho Chehab 31786baecfSMauro Carvalho Chehab for (i = 1; i < len; i++) { 32786baecfSMauro Carvalho Chehab if (i % 2) 33786baecfSMauro Carvalho Chehab checksum += buf[i] << 8; 34786baecfSMauro Carvalho Chehab else 35786baecfSMauro Carvalho Chehab checksum += buf[i]; 36786baecfSMauro Carvalho Chehab } 37786baecfSMauro Carvalho Chehab checksum = ~checksum; 38786baecfSMauro Carvalho Chehab 39786baecfSMauro Carvalho Chehab return checksum; 40786baecfSMauro Carvalho Chehab } 41786baecfSMauro Carvalho Chehab 42786baecfSMauro Carvalho Chehab static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req) 43786baecfSMauro Carvalho Chehab { 44786baecfSMauro Carvalho Chehab #define BUF_LEN 64 45786baecfSMauro Carvalho Chehab #define REQ_HDR_LEN 4 /* send header size */ 46786baecfSMauro Carvalho Chehab #define ACK_HDR_LEN 3 /* rece header size */ 47786baecfSMauro Carvalho Chehab #define CHECKSUM_LEN 2 48786baecfSMauro Carvalho Chehab #define USB_TIMEOUT 2000 49786baecfSMauro Carvalho Chehab struct state *state = d_to_priv(d); 50786baecfSMauro Carvalho Chehab int ret, wlen, rlen; 51786baecfSMauro Carvalho Chehab u8 buf[BUF_LEN]; 52786baecfSMauro Carvalho Chehab u16 checksum, tmp_checksum; 53786baecfSMauro Carvalho Chehab 54786baecfSMauro Carvalho Chehab /* buffer overflow check */ 55786baecfSMauro Carvalho Chehab if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) || 56786baecfSMauro Carvalho Chehab req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) { 57119f7a8cSAntti Palosaari dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n", 58119f7a8cSAntti Palosaari __func__, req->wlen, req->rlen); 59786baecfSMauro Carvalho Chehab return -EINVAL; 60786baecfSMauro Carvalho Chehab } 61786baecfSMauro Carvalho Chehab 62786baecfSMauro Carvalho Chehab buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1; 63786baecfSMauro Carvalho Chehab buf[1] = req->mbox; 64786baecfSMauro Carvalho Chehab buf[2] = req->cmd; 65786baecfSMauro Carvalho Chehab buf[3] = state->seq++; 66786baecfSMauro Carvalho Chehab memcpy(&buf[REQ_HDR_LEN], req->wbuf, req->wlen); 67786baecfSMauro Carvalho Chehab 68786baecfSMauro Carvalho Chehab wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN; 69786baecfSMauro Carvalho Chehab rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN; 70786baecfSMauro Carvalho Chehab 71786baecfSMauro Carvalho Chehab /* calc and add checksum */ 72786baecfSMauro Carvalho Chehab checksum = af9035_checksum(buf, buf[0] - 1); 73786baecfSMauro Carvalho Chehab buf[buf[0] - 1] = (checksum >> 8); 74786baecfSMauro Carvalho Chehab buf[buf[0] - 0] = (checksum & 0xff); 75786baecfSMauro Carvalho Chehab 76786baecfSMauro Carvalho Chehab /* no ack for these packets */ 77786baecfSMauro Carvalho Chehab if (req->cmd == CMD_FW_DL) 78786baecfSMauro Carvalho Chehab rlen = 0; 79786baecfSMauro Carvalho Chehab 80786baecfSMauro Carvalho Chehab ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen); 81786baecfSMauro Carvalho Chehab if (ret) 82786baecfSMauro Carvalho Chehab goto err; 83786baecfSMauro Carvalho Chehab 84786baecfSMauro Carvalho Chehab /* no ack for those packets */ 85786baecfSMauro Carvalho Chehab if (req->cmd == CMD_FW_DL) 86786baecfSMauro Carvalho Chehab goto exit; 87786baecfSMauro Carvalho Chehab 88786baecfSMauro Carvalho Chehab /* verify checksum */ 89786baecfSMauro Carvalho Chehab checksum = af9035_checksum(buf, rlen - 2); 90786baecfSMauro Carvalho Chehab tmp_checksum = (buf[rlen - 2] << 8) | buf[rlen - 1]; 91786baecfSMauro Carvalho Chehab if (tmp_checksum != checksum) { 92119f7a8cSAntti Palosaari dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \ 93119f7a8cSAntti Palosaari "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd, 94119f7a8cSAntti Palosaari tmp_checksum, checksum); 95786baecfSMauro Carvalho Chehab ret = -EIO; 96786baecfSMauro Carvalho Chehab goto err; 97786baecfSMauro Carvalho Chehab } 98786baecfSMauro Carvalho Chehab 99786baecfSMauro Carvalho Chehab /* check status */ 100786baecfSMauro Carvalho Chehab if (buf[2]) { 101119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n", 102119f7a8cSAntti Palosaari __func__, req->cmd, buf[2]); 103786baecfSMauro Carvalho Chehab ret = -EIO; 104786baecfSMauro Carvalho Chehab goto err; 105786baecfSMauro Carvalho Chehab } 106786baecfSMauro Carvalho Chehab 107786baecfSMauro Carvalho Chehab /* read request, copy returned data to return buf */ 108786baecfSMauro Carvalho Chehab if (req->rlen) 109786baecfSMauro Carvalho Chehab memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen); 110786baecfSMauro Carvalho Chehab 111786baecfSMauro Carvalho Chehab exit: 112786baecfSMauro Carvalho Chehab return 0; 113786baecfSMauro Carvalho Chehab 114786baecfSMauro Carvalho Chehab err: 115119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 116786baecfSMauro Carvalho Chehab 117786baecfSMauro Carvalho Chehab return ret; 118786baecfSMauro Carvalho Chehab } 119786baecfSMauro Carvalho Chehab 120786baecfSMauro Carvalho Chehab /* write multiple registers */ 121786baecfSMauro Carvalho Chehab static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) 122786baecfSMauro Carvalho Chehab { 123786baecfSMauro Carvalho Chehab u8 wbuf[6 + len]; 124786baecfSMauro Carvalho Chehab u8 mbox = (reg >> 16) & 0xff; 125786baecfSMauro Carvalho Chehab struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL }; 126786baecfSMauro Carvalho Chehab 127786baecfSMauro Carvalho Chehab wbuf[0] = len; 128786baecfSMauro Carvalho Chehab wbuf[1] = 2; 129786baecfSMauro Carvalho Chehab wbuf[2] = 0; 130786baecfSMauro Carvalho Chehab wbuf[3] = 0; 131786baecfSMauro Carvalho Chehab wbuf[4] = (reg >> 8) & 0xff; 132786baecfSMauro Carvalho Chehab wbuf[5] = (reg >> 0) & 0xff; 133786baecfSMauro Carvalho Chehab memcpy(&wbuf[6], val, len); 134786baecfSMauro Carvalho Chehab 135786baecfSMauro Carvalho Chehab return af9035_ctrl_msg(d, &req); 136786baecfSMauro Carvalho Chehab } 137786baecfSMauro Carvalho Chehab 138786baecfSMauro Carvalho Chehab /* read multiple registers */ 139786baecfSMauro Carvalho Chehab static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) 140786baecfSMauro Carvalho Chehab { 141786baecfSMauro Carvalho Chehab u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff }; 142786baecfSMauro Carvalho Chehab u8 mbox = (reg >> 16) & 0xff; 143786baecfSMauro Carvalho Chehab struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val }; 144786baecfSMauro Carvalho Chehab 145786baecfSMauro Carvalho Chehab return af9035_ctrl_msg(d, &req); 146786baecfSMauro Carvalho Chehab } 147786baecfSMauro Carvalho Chehab 148786baecfSMauro Carvalho Chehab /* write single register */ 149786baecfSMauro Carvalho Chehab static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val) 150786baecfSMauro Carvalho Chehab { 151786baecfSMauro Carvalho Chehab return af9035_wr_regs(d, reg, &val, 1); 152786baecfSMauro Carvalho Chehab } 153786baecfSMauro Carvalho Chehab 154786baecfSMauro Carvalho Chehab /* read single register */ 155786baecfSMauro Carvalho Chehab static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val) 156786baecfSMauro Carvalho Chehab { 157786baecfSMauro Carvalho Chehab return af9035_rd_regs(d, reg, val, 1); 158786baecfSMauro Carvalho Chehab } 159786baecfSMauro Carvalho Chehab 160786baecfSMauro Carvalho Chehab /* write single register with mask */ 161786baecfSMauro Carvalho Chehab static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val, 162786baecfSMauro Carvalho Chehab u8 mask) 163786baecfSMauro Carvalho Chehab { 164786baecfSMauro Carvalho Chehab int ret; 165786baecfSMauro Carvalho Chehab u8 tmp; 166786baecfSMauro Carvalho Chehab 167786baecfSMauro Carvalho Chehab /* no need for read if whole reg is written */ 168786baecfSMauro Carvalho Chehab if (mask != 0xff) { 169786baecfSMauro Carvalho Chehab ret = af9035_rd_regs(d, reg, &tmp, 1); 170786baecfSMauro Carvalho Chehab if (ret) 171786baecfSMauro Carvalho Chehab return ret; 172786baecfSMauro Carvalho Chehab 173786baecfSMauro Carvalho Chehab val &= mask; 174786baecfSMauro Carvalho Chehab tmp &= ~mask; 175786baecfSMauro Carvalho Chehab val |= tmp; 176786baecfSMauro Carvalho Chehab } 177786baecfSMauro Carvalho Chehab 178786baecfSMauro Carvalho Chehab return af9035_wr_regs(d, reg, &val, 1); 179786baecfSMauro Carvalho Chehab } 180786baecfSMauro Carvalho Chehab 181786baecfSMauro Carvalho Chehab static int af9035_i2c_master_xfer(struct i2c_adapter *adap, 182786baecfSMauro Carvalho Chehab struct i2c_msg msg[], int num) 183786baecfSMauro Carvalho Chehab { 184786baecfSMauro Carvalho Chehab struct dvb_usb_device *d = i2c_get_adapdata(adap); 185786baecfSMauro Carvalho Chehab struct state *state = d_to_priv(d); 186786baecfSMauro Carvalho Chehab int ret; 187786baecfSMauro Carvalho Chehab 188786baecfSMauro Carvalho Chehab if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 189786baecfSMauro Carvalho Chehab return -EAGAIN; 190786baecfSMauro Carvalho Chehab 191786baecfSMauro Carvalho Chehab /* 192786baecfSMauro Carvalho Chehab * I2C sub header is 5 bytes long. Meaning of those bytes are: 193786baecfSMauro Carvalho Chehab * 0: data len 194786baecfSMauro Carvalho Chehab * 1: I2C addr << 1 195786baecfSMauro Carvalho Chehab * 2: reg addr len 196786baecfSMauro Carvalho Chehab * byte 3 and 4 can be used as reg addr 197786baecfSMauro Carvalho Chehab * 3: reg addr MSB 198786baecfSMauro Carvalho Chehab * used when reg addr len is set to 2 199786baecfSMauro Carvalho Chehab * 4: reg addr LSB 200786baecfSMauro Carvalho Chehab * used when reg addr len is set to 1 or 2 201786baecfSMauro Carvalho Chehab * 202786baecfSMauro Carvalho Chehab * For the simplify we do not use register addr at all. 203786baecfSMauro Carvalho Chehab * NOTE: As a firmware knows tuner type there is very small possibility 204786baecfSMauro Carvalho Chehab * there could be some tuner I2C hacks done by firmware and this may 205786baecfSMauro Carvalho Chehab * lead problems if firmware expects those bytes are used. 206786baecfSMauro Carvalho Chehab */ 207786baecfSMauro Carvalho Chehab if (num == 2 && !(msg[0].flags & I2C_M_RD) && 208786baecfSMauro Carvalho Chehab (msg[1].flags & I2C_M_RD)) { 209786baecfSMauro Carvalho Chehab if (msg[0].len > 40 || msg[1].len > 40) { 210786baecfSMauro Carvalho Chehab /* TODO: correct limits > 40 */ 211786baecfSMauro Carvalho Chehab ret = -EOPNOTSUPP; 2129805992fSJose Alberto Reguero } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) || 2139805992fSJose Alberto Reguero (msg[0].addr == state->af9033_config[1].i2c_addr)) { 214bf97b637SAntti Palosaari /* demod access via firmware interface */ 215786baecfSMauro Carvalho Chehab u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | 216786baecfSMauro Carvalho Chehab msg[0].buf[2]; 217bf97b637SAntti Palosaari 218bf97b637SAntti Palosaari if (msg[0].addr == state->af9033_config[1].i2c_addr) 2199805992fSJose Alberto Reguero reg |= 0x100000; 220bf97b637SAntti Palosaari 221786baecfSMauro Carvalho Chehab ret = af9035_rd_regs(d, reg, &msg[1].buf[0], 222786baecfSMauro Carvalho Chehab msg[1].len); 223786baecfSMauro Carvalho Chehab } else { 224786baecfSMauro Carvalho Chehab /* I2C */ 225786baecfSMauro Carvalho Chehab u8 buf[5 + msg[0].len]; 226786baecfSMauro Carvalho Chehab struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf), 227786baecfSMauro Carvalho Chehab buf, msg[1].len, msg[1].buf }; 2289805992fSJose Alberto Reguero req.mbox |= ((msg[0].addr & 0x80) >> 3); 229786baecfSMauro Carvalho Chehab buf[0] = msg[1].len; 230bf97b637SAntti Palosaari buf[1] = msg[0].addr << 1; 231786baecfSMauro Carvalho Chehab buf[2] = 0x00; /* reg addr len */ 232786baecfSMauro Carvalho Chehab buf[3] = 0x00; /* reg addr MSB */ 233786baecfSMauro Carvalho Chehab buf[4] = 0x00; /* reg addr LSB */ 234786baecfSMauro Carvalho Chehab memcpy(&buf[5], msg[0].buf, msg[0].len); 235786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req); 236786baecfSMauro Carvalho Chehab } 237786baecfSMauro Carvalho Chehab } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { 238786baecfSMauro Carvalho Chehab if (msg[0].len > 40) { 239786baecfSMauro Carvalho Chehab /* TODO: correct limits > 40 */ 240786baecfSMauro Carvalho Chehab ret = -EOPNOTSUPP; 2419805992fSJose Alberto Reguero } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) || 2429805992fSJose Alberto Reguero (msg[0].addr == state->af9033_config[1].i2c_addr)) { 243bf97b637SAntti Palosaari /* demod access via firmware interface */ 244786baecfSMauro Carvalho Chehab u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | 245786baecfSMauro Carvalho Chehab msg[0].buf[2]; 246bf97b637SAntti Palosaari 247bf97b637SAntti Palosaari if (msg[0].addr == state->af9033_config[1].i2c_addr) 2489805992fSJose Alberto Reguero reg |= 0x100000; 249bf97b637SAntti Palosaari 250786baecfSMauro Carvalho Chehab ret = af9035_wr_regs(d, reg, &msg[0].buf[3], 251786baecfSMauro Carvalho Chehab msg[0].len - 3); 252786baecfSMauro Carvalho Chehab } else { 253786baecfSMauro Carvalho Chehab /* I2C */ 254786baecfSMauro Carvalho Chehab u8 buf[5 + msg[0].len]; 255786baecfSMauro Carvalho Chehab struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf, 256786baecfSMauro Carvalho Chehab 0, NULL }; 2579805992fSJose Alberto Reguero req.mbox |= ((msg[0].addr & 0x80) >> 3); 258786baecfSMauro Carvalho Chehab buf[0] = msg[0].len; 259bf97b637SAntti Palosaari buf[1] = msg[0].addr << 1; 260786baecfSMauro Carvalho Chehab buf[2] = 0x00; /* reg addr len */ 261786baecfSMauro Carvalho Chehab buf[3] = 0x00; /* reg addr MSB */ 262786baecfSMauro Carvalho Chehab buf[4] = 0x00; /* reg addr LSB */ 263786baecfSMauro Carvalho Chehab memcpy(&buf[5], msg[0].buf, msg[0].len); 264786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req); 265786baecfSMauro Carvalho Chehab } 266786baecfSMauro Carvalho Chehab } else { 267786baecfSMauro Carvalho Chehab /* 268786baecfSMauro Carvalho Chehab * We support only two kind of I2C transactions: 269786baecfSMauro Carvalho Chehab * 1) 1 x read + 1 x write 270786baecfSMauro Carvalho Chehab * 2) 1 x write 271786baecfSMauro Carvalho Chehab */ 272786baecfSMauro Carvalho Chehab ret = -EOPNOTSUPP; 273786baecfSMauro Carvalho Chehab } 274786baecfSMauro Carvalho Chehab 275786baecfSMauro Carvalho Chehab mutex_unlock(&d->i2c_mutex); 276786baecfSMauro Carvalho Chehab 277786baecfSMauro Carvalho Chehab if (ret < 0) 278786baecfSMauro Carvalho Chehab return ret; 279786baecfSMauro Carvalho Chehab else 280786baecfSMauro Carvalho Chehab return num; 281786baecfSMauro Carvalho Chehab } 282786baecfSMauro Carvalho Chehab 283786baecfSMauro Carvalho Chehab static u32 af9035_i2c_functionality(struct i2c_adapter *adapter) 284786baecfSMauro Carvalho Chehab { 285786baecfSMauro Carvalho Chehab return I2C_FUNC_I2C; 286786baecfSMauro Carvalho Chehab } 287786baecfSMauro Carvalho Chehab 288786baecfSMauro Carvalho Chehab static struct i2c_algorithm af9035_i2c_algo = { 289786baecfSMauro Carvalho Chehab .master_xfer = af9035_i2c_master_xfer, 290786baecfSMauro Carvalho Chehab .functionality = af9035_i2c_functionality, 291786baecfSMauro Carvalho Chehab }; 292786baecfSMauro Carvalho Chehab 293786baecfSMauro Carvalho Chehab static int af9035_identify_state(struct dvb_usb_device *d, const char **name) 294786baecfSMauro Carvalho Chehab { 295786baecfSMauro Carvalho Chehab int ret; 296786baecfSMauro Carvalho Chehab u8 wbuf[1] = { 1 }; 297786baecfSMauro Carvalho Chehab u8 rbuf[4]; 298786baecfSMauro Carvalho Chehab struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf, 299786baecfSMauro Carvalho Chehab sizeof(rbuf), rbuf }; 300786baecfSMauro Carvalho Chehab 301786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req); 302786baecfSMauro Carvalho Chehab if (ret < 0) 303786baecfSMauro Carvalho Chehab goto err; 304786baecfSMauro Carvalho Chehab 305119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf); 306786baecfSMauro Carvalho Chehab if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3]) 307786baecfSMauro Carvalho Chehab ret = WARM; 308786baecfSMauro Carvalho Chehab else 309786baecfSMauro Carvalho Chehab ret = COLD; 310786baecfSMauro Carvalho Chehab 311786baecfSMauro Carvalho Chehab return ret; 312786baecfSMauro Carvalho Chehab 313786baecfSMauro Carvalho Chehab err: 314119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 315786baecfSMauro Carvalho Chehab 316786baecfSMauro Carvalho Chehab return ret; 317786baecfSMauro Carvalho Chehab } 318786baecfSMauro Carvalho Chehab 319786baecfSMauro Carvalho Chehab static int af9035_download_firmware(struct dvb_usb_device *d, 320786baecfSMauro Carvalho Chehab const struct firmware *fw) 321786baecfSMauro Carvalho Chehab { 322786baecfSMauro Carvalho Chehab int ret, i, j, len; 323786baecfSMauro Carvalho Chehab u8 wbuf[1]; 324786baecfSMauro Carvalho Chehab u8 rbuf[4]; 325786baecfSMauro Carvalho Chehab struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; 326786baecfSMauro Carvalho Chehab struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL }; 327786baecfSMauro Carvalho Chehab struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; 328bf97b637SAntti Palosaari u8 hdr_core, tmp; 329786baecfSMauro Carvalho Chehab u16 hdr_addr, hdr_data_len, hdr_checksum; 330786baecfSMauro Carvalho Chehab #define MAX_DATA 58 331786baecfSMauro Carvalho Chehab #define HDR_SIZE 7 332786baecfSMauro Carvalho Chehab 333786baecfSMauro Carvalho Chehab /* 334bf97b637SAntti Palosaari * In case of dual tuner configuration we need to do some extra 335bf97b637SAntti Palosaari * initialization in order to download firmware to slave demod too, 336bf97b637SAntti Palosaari * which is done by master demod. 337bf97b637SAntti Palosaari * Master feeds also clock and controls power via GPIO. 338bf97b637SAntti Palosaari */ 339bf97b637SAntti Palosaari ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp); 340bf97b637SAntti Palosaari if (ret < 0) 341bf97b637SAntti Palosaari goto err; 342bf97b637SAntti Palosaari 343bf97b637SAntti Palosaari if (tmp) { 344bf97b637SAntti Palosaari /* configure gpioh1, reset & power slave demod */ 345bf97b637SAntti Palosaari ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01); 346bf97b637SAntti Palosaari if (ret < 0) 347bf97b637SAntti Palosaari goto err; 348bf97b637SAntti Palosaari 349bf97b637SAntti Palosaari ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01); 350bf97b637SAntti Palosaari if (ret < 0) 351bf97b637SAntti Palosaari goto err; 352bf97b637SAntti Palosaari 353bf97b637SAntti Palosaari ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01); 354bf97b637SAntti Palosaari if (ret < 0) 355bf97b637SAntti Palosaari goto err; 356bf97b637SAntti Palosaari 357bf97b637SAntti Palosaari usleep_range(10000, 50000); 358bf97b637SAntti Palosaari 359bf97b637SAntti Palosaari ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01); 360bf97b637SAntti Palosaari if (ret < 0) 361bf97b637SAntti Palosaari goto err; 362bf97b637SAntti Palosaari 363bf97b637SAntti Palosaari /* tell the slave I2C address */ 364bf97b637SAntti Palosaari ret = af9035_rd_reg(d, EEPROM_2ND_DEMOD_ADDR, &tmp); 365bf97b637SAntti Palosaari if (ret < 0) 366bf97b637SAntti Palosaari goto err; 367bf97b637SAntti Palosaari 368bf97b637SAntti Palosaari ret = af9035_wr_reg(d, 0x00417f, tmp); 369bf97b637SAntti Palosaari if (ret < 0) 370bf97b637SAntti Palosaari goto err; 371bf97b637SAntti Palosaari 372bf97b637SAntti Palosaari /* enable clock out */ 373bf97b637SAntti Palosaari ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01); 374bf97b637SAntti Palosaari if (ret < 0) 375bf97b637SAntti Palosaari goto err; 376bf97b637SAntti Palosaari } 377bf97b637SAntti Palosaari 378bf97b637SAntti Palosaari /* 379786baecfSMauro Carvalho Chehab * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info! 380786baecfSMauro Carvalho Chehab * 381786baecfSMauro Carvalho Chehab * byte 0: MCS 51 core 382786baecfSMauro Carvalho Chehab * There are two inside the AF9035 (1=Link and 2=OFDM) with separate 383786baecfSMauro Carvalho Chehab * address spaces 384786baecfSMauro Carvalho Chehab * byte 1-2: Big endian destination address 385786baecfSMauro Carvalho Chehab * byte 3-4: Big endian number of data bytes following the header 386786baecfSMauro Carvalho Chehab * byte 5-6: Big endian header checksum, apparently ignored by the chip 387786baecfSMauro Carvalho Chehab * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256) 388786baecfSMauro Carvalho Chehab */ 389786baecfSMauro Carvalho Chehab 390786baecfSMauro Carvalho Chehab for (i = fw->size; i > HDR_SIZE;) { 391786baecfSMauro Carvalho Chehab hdr_core = fw->data[fw->size - i + 0]; 392786baecfSMauro Carvalho Chehab hdr_addr = fw->data[fw->size - i + 1] << 8; 393786baecfSMauro Carvalho Chehab hdr_addr |= fw->data[fw->size - i + 2] << 0; 394786baecfSMauro Carvalho Chehab hdr_data_len = fw->data[fw->size - i + 3] << 8; 395786baecfSMauro Carvalho Chehab hdr_data_len |= fw->data[fw->size - i + 4] << 0; 396786baecfSMauro Carvalho Chehab hdr_checksum = fw->data[fw->size - i + 5] << 8; 397786baecfSMauro Carvalho Chehab hdr_checksum |= fw->data[fw->size - i + 6] << 0; 398786baecfSMauro Carvalho Chehab 399119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \ 400119f7a8cSAntti Palosaari "checksum=%04x\n", __func__, hdr_core, hdr_addr, 401119f7a8cSAntti Palosaari hdr_data_len, hdr_checksum); 402786baecfSMauro Carvalho Chehab 403786baecfSMauro Carvalho Chehab if (((hdr_core != 1) && (hdr_core != 2)) || 404786baecfSMauro Carvalho Chehab (hdr_data_len > i)) { 405119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__); 406786baecfSMauro Carvalho Chehab break; 407786baecfSMauro Carvalho Chehab } 408786baecfSMauro Carvalho Chehab 409786baecfSMauro Carvalho Chehab /* download begin packet */ 410786baecfSMauro Carvalho Chehab req.cmd = CMD_FW_DL_BEGIN; 411786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req); 412786baecfSMauro Carvalho Chehab if (ret < 0) 413786baecfSMauro Carvalho Chehab goto err; 414786baecfSMauro Carvalho Chehab 415786baecfSMauro Carvalho Chehab /* download firmware packet(s) */ 416786baecfSMauro Carvalho Chehab for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) { 417786baecfSMauro Carvalho Chehab len = j; 418786baecfSMauro Carvalho Chehab if (len > MAX_DATA) 419786baecfSMauro Carvalho Chehab len = MAX_DATA; 420786baecfSMauro Carvalho Chehab req_fw_dl.wlen = len; 421786baecfSMauro Carvalho Chehab req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i + 422786baecfSMauro Carvalho Chehab HDR_SIZE + hdr_data_len - j]; 423786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req_fw_dl); 424786baecfSMauro Carvalho Chehab if (ret < 0) 425786baecfSMauro Carvalho Chehab goto err; 426786baecfSMauro Carvalho Chehab } 427786baecfSMauro Carvalho Chehab 428786baecfSMauro Carvalho Chehab /* download end packet */ 429786baecfSMauro Carvalho Chehab req.cmd = CMD_FW_DL_END; 430786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req); 431786baecfSMauro Carvalho Chehab if (ret < 0) 432786baecfSMauro Carvalho Chehab goto err; 433786baecfSMauro Carvalho Chehab 434786baecfSMauro Carvalho Chehab i -= hdr_data_len + HDR_SIZE; 435786baecfSMauro Carvalho Chehab 436119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n", 437119f7a8cSAntti Palosaari __func__, fw->size - i); 438786baecfSMauro Carvalho Chehab } 439786baecfSMauro Carvalho Chehab 440786baecfSMauro Carvalho Chehab /* firmware loaded, request boot */ 441786baecfSMauro Carvalho Chehab req.cmd = CMD_FW_BOOT; 442786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req); 443786baecfSMauro Carvalho Chehab if (ret < 0) 444786baecfSMauro Carvalho Chehab goto err; 445786baecfSMauro Carvalho Chehab 446786baecfSMauro Carvalho Chehab /* ensure firmware starts */ 447786baecfSMauro Carvalho Chehab wbuf[0] = 1; 448786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req_fw_ver); 449786baecfSMauro Carvalho Chehab if (ret < 0) 450786baecfSMauro Carvalho Chehab goto err; 451786baecfSMauro Carvalho Chehab 452786baecfSMauro Carvalho Chehab if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { 453119f7a8cSAntti Palosaari dev_err(&d->udev->dev, "%s: firmware did not run\n", 454119f7a8cSAntti Palosaari KBUILD_MODNAME); 455786baecfSMauro Carvalho Chehab ret = -ENODEV; 456786baecfSMauro Carvalho Chehab goto err; 457786baecfSMauro Carvalho Chehab } 458786baecfSMauro Carvalho Chehab 459119f7a8cSAntti Palosaari dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d", 460119f7a8cSAntti Palosaari KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]); 461786baecfSMauro Carvalho Chehab 462786baecfSMauro Carvalho Chehab return 0; 463786baecfSMauro Carvalho Chehab 464786baecfSMauro Carvalho Chehab err: 465119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 466786baecfSMauro Carvalho Chehab 467786baecfSMauro Carvalho Chehab return ret; 468786baecfSMauro Carvalho Chehab } 469786baecfSMauro Carvalho Chehab 470786baecfSMauro Carvalho Chehab static int af9035_download_firmware_it9135(struct dvb_usb_device *d, 471786baecfSMauro Carvalho Chehab const struct firmware *fw) 472786baecfSMauro Carvalho Chehab { 473786baecfSMauro Carvalho Chehab int ret, i, i_prev; 474786baecfSMauro Carvalho Chehab u8 wbuf[1]; 475786baecfSMauro Carvalho Chehab u8 rbuf[4]; 476786baecfSMauro Carvalho Chehab struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; 477786baecfSMauro Carvalho Chehab struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL }; 478786baecfSMauro Carvalho Chehab struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; 479786baecfSMauro Carvalho Chehab #define HDR_SIZE 7 480786baecfSMauro Carvalho Chehab 481786baecfSMauro Carvalho Chehab /* 482786baecfSMauro Carvalho Chehab * There seems to be following firmware header. Meaning of bytes 0-3 483786baecfSMauro Carvalho Chehab * is unknown. 484786baecfSMauro Carvalho Chehab * 485786baecfSMauro Carvalho Chehab * 0: 3 486786baecfSMauro Carvalho Chehab * 1: 0, 1 487786baecfSMauro Carvalho Chehab * 2: 0 488786baecfSMauro Carvalho Chehab * 3: 1, 2, 3 489786baecfSMauro Carvalho Chehab * 4: addr MSB 490786baecfSMauro Carvalho Chehab * 5: addr LSB 491786baecfSMauro Carvalho Chehab * 6: count of data bytes ? 492786baecfSMauro Carvalho Chehab */ 493786baecfSMauro Carvalho Chehab 494786baecfSMauro Carvalho Chehab for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) { 495786baecfSMauro Carvalho Chehab if (i == fw->size || 496786baecfSMauro Carvalho Chehab (fw->data[i + 0] == 0x03 && 497786baecfSMauro Carvalho Chehab (fw->data[i + 1] == 0x00 || 498786baecfSMauro Carvalho Chehab fw->data[i + 1] == 0x01) && 499786baecfSMauro Carvalho Chehab fw->data[i + 2] == 0x00)) { 500786baecfSMauro Carvalho Chehab req_fw_dl.wlen = i - i_prev; 501786baecfSMauro Carvalho Chehab req_fw_dl.wbuf = (u8 *) &fw->data[i_prev]; 502786baecfSMauro Carvalho Chehab i_prev = i; 503786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req_fw_dl); 504786baecfSMauro Carvalho Chehab if (ret < 0) 505786baecfSMauro Carvalho Chehab goto err; 506786baecfSMauro Carvalho Chehab 507119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n", 508119f7a8cSAntti Palosaari __func__, i); 509786baecfSMauro Carvalho Chehab } 510786baecfSMauro Carvalho Chehab } 511786baecfSMauro Carvalho Chehab 512786baecfSMauro Carvalho Chehab /* firmware loaded, request boot */ 513786baecfSMauro Carvalho Chehab req.cmd = CMD_FW_BOOT; 514786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req); 515786baecfSMauro Carvalho Chehab if (ret < 0) 516786baecfSMauro Carvalho Chehab goto err; 517786baecfSMauro Carvalho Chehab 518786baecfSMauro Carvalho Chehab /* ensure firmware starts */ 519786baecfSMauro Carvalho Chehab wbuf[0] = 1; 520786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req_fw_ver); 521786baecfSMauro Carvalho Chehab if (ret < 0) 522786baecfSMauro Carvalho Chehab goto err; 523786baecfSMauro Carvalho Chehab 524786baecfSMauro Carvalho Chehab if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { 525119f7a8cSAntti Palosaari dev_err(&d->udev->dev, "%s: firmware did not run\n", 526119f7a8cSAntti Palosaari KBUILD_MODNAME); 527786baecfSMauro Carvalho Chehab ret = -ENODEV; 528786baecfSMauro Carvalho Chehab goto err; 529786baecfSMauro Carvalho Chehab } 530786baecfSMauro Carvalho Chehab 531119f7a8cSAntti Palosaari dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d", 532119f7a8cSAntti Palosaari KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]); 533786baecfSMauro Carvalho Chehab 534786baecfSMauro Carvalho Chehab return 0; 535786baecfSMauro Carvalho Chehab 536786baecfSMauro Carvalho Chehab err: 537119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 538786baecfSMauro Carvalho Chehab 539786baecfSMauro Carvalho Chehab return ret; 540786baecfSMauro Carvalho Chehab } 541786baecfSMauro Carvalho Chehab 542786baecfSMauro Carvalho Chehab static int af9035_read_config(struct dvb_usb_device *d) 543786baecfSMauro Carvalho Chehab { 544786baecfSMauro Carvalho Chehab struct state *state = d_to_priv(d); 545786baecfSMauro Carvalho Chehab int ret, i, eeprom_shift = 0; 546786baecfSMauro Carvalho Chehab u8 tmp; 547786baecfSMauro Carvalho Chehab u16 tmp16; 548786baecfSMauro Carvalho Chehab 549bf97b637SAntti Palosaari /* demod I2C "address" */ 550bf97b637SAntti Palosaari state->af9033_config[0].i2c_addr = 0x38; 551bf97b637SAntti Palosaari 552786baecfSMauro Carvalho Chehab /* check if there is dual tuners */ 553786baecfSMauro Carvalho Chehab ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp); 554786baecfSMauro Carvalho Chehab if (ret < 0) 555786baecfSMauro Carvalho Chehab goto err; 556786baecfSMauro Carvalho Chehab 557786baecfSMauro Carvalho Chehab state->dual_mode = tmp; 558bf97b637SAntti Palosaari dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__, 559bf97b637SAntti Palosaari state->dual_mode); 560786baecfSMauro Carvalho Chehab 5619805992fSJose Alberto Reguero if (state->dual_mode) { 5629805992fSJose Alberto Reguero /* read 2nd demodulator I2C address */ 563bf97b637SAntti Palosaari ret = af9035_rd_reg(d, EEPROM_2ND_DEMOD_ADDR, &tmp); 5649805992fSJose Alberto Reguero if (ret < 0) 5659805992fSJose Alberto Reguero goto err; 566bf97b637SAntti Palosaari 5679805992fSJose Alberto Reguero state->af9033_config[1].i2c_addr = tmp; 568bf97b637SAntti Palosaari dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n", 569bf97b637SAntti Palosaari __func__, tmp); 5709805992fSJose Alberto Reguero } 5719805992fSJose Alberto Reguero 572786baecfSMauro Carvalho Chehab for (i = 0; i < state->dual_mode + 1; i++) { 573786baecfSMauro Carvalho Chehab /* tuner */ 574786baecfSMauro Carvalho Chehab ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp); 575786baecfSMauro Carvalho Chehab if (ret < 0) 576786baecfSMauro Carvalho Chehab goto err; 577786baecfSMauro Carvalho Chehab 578786baecfSMauro Carvalho Chehab state->af9033_config[i].tuner = tmp; 579119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n", 580119f7a8cSAntti Palosaari __func__, i, tmp); 581786baecfSMauro Carvalho Chehab 582786baecfSMauro Carvalho Chehab switch (tmp) { 583786baecfSMauro Carvalho Chehab case AF9033_TUNER_TUA9001: 584786baecfSMauro Carvalho Chehab case AF9033_TUNER_FC0011: 585786baecfSMauro Carvalho Chehab case AF9033_TUNER_MXL5007T: 586786baecfSMauro Carvalho Chehab case AF9033_TUNER_TDA18218: 587d67ceb33SOliver Schinagl case AF9033_TUNER_FC2580: 5887e0bc296SAntti Palosaari case AF9033_TUNER_FC0012: 589786baecfSMauro Carvalho Chehab state->af9033_config[i].spec_inv = 1; 590786baecfSMauro Carvalho Chehab break; 591786baecfSMauro Carvalho Chehab default: 592119f7a8cSAntti Palosaari dev_warn(&d->udev->dev, "%s: tuner id=%02x not " \ 593119f7a8cSAntti Palosaari "supported, please report!", 594119f7a8cSAntti Palosaari KBUILD_MODNAME, tmp); 595c2c1b415SPeter Senna Tschudin } 596786baecfSMauro Carvalho Chehab 597bf97b637SAntti Palosaari /* disable dual mode if driver does not support it */ 598bf97b637SAntti Palosaari if (i == 1) 599bf97b637SAntti Palosaari switch (tmp) { 600bf97b637SAntti Palosaari default: 601bf97b637SAntti Palosaari state->dual_mode = false; 602bf97b637SAntti Palosaari dev_info(&d->udev->dev, "%s: driver does not " \ 603bf97b637SAntti Palosaari "support 2nd tuner and will " \ 604bf97b637SAntti Palosaari "disable it", KBUILD_MODNAME); 605bf97b637SAntti Palosaari } 606bf97b637SAntti Palosaari 607786baecfSMauro Carvalho Chehab /* tuner IF frequency */ 608786baecfSMauro Carvalho Chehab ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp); 609786baecfSMauro Carvalho Chehab if (ret < 0) 610786baecfSMauro Carvalho Chehab goto err; 611786baecfSMauro Carvalho Chehab 612786baecfSMauro Carvalho Chehab tmp16 = tmp; 613786baecfSMauro Carvalho Chehab 614786baecfSMauro Carvalho Chehab ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp); 615786baecfSMauro Carvalho Chehab if (ret < 0) 616786baecfSMauro Carvalho Chehab goto err; 617786baecfSMauro Carvalho Chehab 618786baecfSMauro Carvalho Chehab tmp16 |= tmp << 8; 619786baecfSMauro Carvalho Chehab 620119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16); 621786baecfSMauro Carvalho Chehab 622786baecfSMauro Carvalho Chehab eeprom_shift = 0x10; /* shift for the 2nd tuner params */ 623786baecfSMauro Carvalho Chehab } 624786baecfSMauro Carvalho Chehab 625786baecfSMauro Carvalho Chehab /* get demod clock */ 626786baecfSMauro Carvalho Chehab ret = af9035_rd_reg(d, 0x00d800, &tmp); 627786baecfSMauro Carvalho Chehab if (ret < 0) 628786baecfSMauro Carvalho Chehab goto err; 629786baecfSMauro Carvalho Chehab 630786baecfSMauro Carvalho Chehab tmp = (tmp >> 0) & 0x0f; 631786baecfSMauro Carvalho Chehab 632786baecfSMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) 633786baecfSMauro Carvalho Chehab state->af9033_config[i].clock = clock_lut[tmp]; 634786baecfSMauro Carvalho Chehab 635786baecfSMauro Carvalho Chehab return 0; 636786baecfSMauro Carvalho Chehab 637786baecfSMauro Carvalho Chehab err: 638119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 639786baecfSMauro Carvalho Chehab 640786baecfSMauro Carvalho Chehab return ret; 641786baecfSMauro Carvalho Chehab } 642786baecfSMauro Carvalho Chehab 643786baecfSMauro Carvalho Chehab static int af9035_read_config_it9135(struct dvb_usb_device *d) 644786baecfSMauro Carvalho Chehab { 645786baecfSMauro Carvalho Chehab struct state *state = d_to_priv(d); 646786baecfSMauro Carvalho Chehab int ret, i; 647786baecfSMauro Carvalho Chehab u8 tmp; 648786baecfSMauro Carvalho Chehab 649786baecfSMauro Carvalho Chehab state->dual_mode = false; 650786baecfSMauro Carvalho Chehab 651786baecfSMauro Carvalho Chehab /* get demod clock */ 652786baecfSMauro Carvalho Chehab ret = af9035_rd_reg(d, 0x00d800, &tmp); 653786baecfSMauro Carvalho Chehab if (ret < 0) 654786baecfSMauro Carvalho Chehab goto err; 655786baecfSMauro Carvalho Chehab 656786baecfSMauro Carvalho Chehab tmp = (tmp >> 0) & 0x0f; 657786baecfSMauro Carvalho Chehab 658786baecfSMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) 659786baecfSMauro Carvalho Chehab state->af9033_config[i].clock = clock_lut_it9135[tmp]; 660786baecfSMauro Carvalho Chehab 661786baecfSMauro Carvalho Chehab return 0; 662786baecfSMauro Carvalho Chehab 663786baecfSMauro Carvalho Chehab err: 664119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 665786baecfSMauro Carvalho Chehab 666786baecfSMauro Carvalho Chehab return ret; 667786baecfSMauro Carvalho Chehab } 668786baecfSMauro Carvalho Chehab 66951639be3SAntti Palosaari static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d, 67051639be3SAntti Palosaari int cmd, int arg) 67151639be3SAntti Palosaari { 67251639be3SAntti Palosaari int ret; 67351639be3SAntti Palosaari u8 val; 67451639be3SAntti Palosaari 67551639be3SAntti Palosaari dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg); 67651639be3SAntti Palosaari 67751639be3SAntti Palosaari /* 67851639be3SAntti Palosaari * CEN always enabled by hardware wiring 67951639be3SAntti Palosaari * RESETN GPIOT3 68051639be3SAntti Palosaari * RXEN GPIOT2 68151639be3SAntti Palosaari */ 68251639be3SAntti Palosaari 68351639be3SAntti Palosaari switch (cmd) { 68451639be3SAntti Palosaari case TUA9001_CMD_RESETN: 68551639be3SAntti Palosaari if (arg) 68651639be3SAntti Palosaari val = 0x00; 68751639be3SAntti Palosaari else 68851639be3SAntti Palosaari val = 0x01; 68951639be3SAntti Palosaari 69051639be3SAntti Palosaari ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01); 69151639be3SAntti Palosaari if (ret < 0) 69251639be3SAntti Palosaari goto err; 69351639be3SAntti Palosaari break; 69451639be3SAntti Palosaari case TUA9001_CMD_RXEN: 69551639be3SAntti Palosaari if (arg) 69651639be3SAntti Palosaari val = 0x01; 69751639be3SAntti Palosaari else 69851639be3SAntti Palosaari val = 0x00; 69951639be3SAntti Palosaari 70051639be3SAntti Palosaari ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01); 70151639be3SAntti Palosaari if (ret < 0) 70251639be3SAntti Palosaari goto err; 70351639be3SAntti Palosaari break; 70451639be3SAntti Palosaari } 70551639be3SAntti Palosaari 70651639be3SAntti Palosaari return 0; 70751639be3SAntti Palosaari 70851639be3SAntti Palosaari err: 70951639be3SAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 71051639be3SAntti Palosaari 71151639be3SAntti Palosaari return ret; 71251639be3SAntti Palosaari } 71351639be3SAntti Palosaari 71451639be3SAntti Palosaari 715786baecfSMauro Carvalho Chehab static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d, 716786baecfSMauro Carvalho Chehab int cmd, int arg) 717786baecfSMauro Carvalho Chehab { 718786baecfSMauro Carvalho Chehab int ret; 719786baecfSMauro Carvalho Chehab 720786baecfSMauro Carvalho Chehab switch (cmd) { 721786baecfSMauro Carvalho Chehab case FC0011_FE_CALLBACK_POWER: 722786baecfSMauro Carvalho Chehab /* Tuner enable */ 723786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1); 724786baecfSMauro Carvalho Chehab if (ret < 0) 725786baecfSMauro Carvalho Chehab goto err; 726786baecfSMauro Carvalho Chehab 727786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1); 728786baecfSMauro Carvalho Chehab if (ret < 0) 729786baecfSMauro Carvalho Chehab goto err; 730786baecfSMauro Carvalho Chehab 731786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1); 732786baecfSMauro Carvalho Chehab if (ret < 0) 733786baecfSMauro Carvalho Chehab goto err; 734786baecfSMauro Carvalho Chehab 735786baecfSMauro Carvalho Chehab /* LED */ 736786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1); 737786baecfSMauro Carvalho Chehab if (ret < 0) 738786baecfSMauro Carvalho Chehab goto err; 739786baecfSMauro Carvalho Chehab 740786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1); 741786baecfSMauro Carvalho Chehab if (ret < 0) 742786baecfSMauro Carvalho Chehab goto err; 743786baecfSMauro Carvalho Chehab 744786baecfSMauro Carvalho Chehab usleep_range(10000, 50000); 745786baecfSMauro Carvalho Chehab break; 746786baecfSMauro Carvalho Chehab case FC0011_FE_CALLBACK_RESET: 747786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0xd8e9, 1); 748786baecfSMauro Carvalho Chehab if (ret < 0) 749786baecfSMauro Carvalho Chehab goto err; 750786baecfSMauro Carvalho Chehab 751786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0xd8e8, 1); 752786baecfSMauro Carvalho Chehab if (ret < 0) 753786baecfSMauro Carvalho Chehab goto err; 754786baecfSMauro Carvalho Chehab 755786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0xd8e7, 1); 756786baecfSMauro Carvalho Chehab if (ret < 0) 757786baecfSMauro Carvalho Chehab goto err; 758786baecfSMauro Carvalho Chehab 759786baecfSMauro Carvalho Chehab usleep_range(10000, 20000); 760786baecfSMauro Carvalho Chehab 761786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0xd8e7, 0); 762786baecfSMauro Carvalho Chehab if (ret < 0) 763786baecfSMauro Carvalho Chehab goto err; 764786baecfSMauro Carvalho Chehab 765786baecfSMauro Carvalho Chehab usleep_range(10000, 20000); 766786baecfSMauro Carvalho Chehab break; 767786baecfSMauro Carvalho Chehab default: 768786baecfSMauro Carvalho Chehab ret = -EINVAL; 769786baecfSMauro Carvalho Chehab goto err; 770786baecfSMauro Carvalho Chehab } 771786baecfSMauro Carvalho Chehab 772786baecfSMauro Carvalho Chehab return 0; 773786baecfSMauro Carvalho Chehab 774786baecfSMauro Carvalho Chehab err: 775119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 776786baecfSMauro Carvalho Chehab 777786baecfSMauro Carvalho Chehab return ret; 778786baecfSMauro Carvalho Chehab } 779786baecfSMauro Carvalho Chehab 780786baecfSMauro Carvalho Chehab static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg) 781786baecfSMauro Carvalho Chehab { 782786baecfSMauro Carvalho Chehab struct state *state = d_to_priv(d); 783786baecfSMauro Carvalho Chehab 784786baecfSMauro Carvalho Chehab switch (state->af9033_config[0].tuner) { 785786baecfSMauro Carvalho Chehab case AF9033_TUNER_FC0011: 786786baecfSMauro Carvalho Chehab return af9035_fc0011_tuner_callback(d, cmd, arg); 78751639be3SAntti Palosaari case AF9033_TUNER_TUA9001: 78851639be3SAntti Palosaari return af9035_tua9001_tuner_callback(d, cmd, arg); 789786baecfSMauro Carvalho Chehab default: 790786baecfSMauro Carvalho Chehab break; 791786baecfSMauro Carvalho Chehab } 792786baecfSMauro Carvalho Chehab 7931835af10SAntti Palosaari return 0; 794786baecfSMauro Carvalho Chehab } 795786baecfSMauro Carvalho Chehab 796786baecfSMauro Carvalho Chehab static int af9035_frontend_callback(void *adapter_priv, int component, 797786baecfSMauro Carvalho Chehab int cmd, int arg) 798786baecfSMauro Carvalho Chehab { 799786baecfSMauro Carvalho Chehab struct i2c_adapter *adap = adapter_priv; 800786baecfSMauro Carvalho Chehab struct dvb_usb_device *d = i2c_get_adapdata(adap); 801786baecfSMauro Carvalho Chehab 8021835af10SAntti Palosaari dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n", 8031835af10SAntti Palosaari __func__, component, cmd, arg); 8041835af10SAntti Palosaari 805786baecfSMauro Carvalho Chehab switch (component) { 806786baecfSMauro Carvalho Chehab case DVB_FRONTEND_COMPONENT_TUNER: 807786baecfSMauro Carvalho Chehab return af9035_tuner_callback(d, cmd, arg); 808786baecfSMauro Carvalho Chehab default: 809786baecfSMauro Carvalho Chehab break; 810786baecfSMauro Carvalho Chehab } 811786baecfSMauro Carvalho Chehab 8121835af10SAntti Palosaari return 0; 813786baecfSMauro Carvalho Chehab } 814786baecfSMauro Carvalho Chehab 8159805992fSJose Alberto Reguero static int af9035_get_adapter_count(struct dvb_usb_device *d) 8169805992fSJose Alberto Reguero { 8179805992fSJose Alberto Reguero struct state *state = d_to_priv(d); 8189805992fSJose Alberto Reguero return state->dual_mode + 1; 8199805992fSJose Alberto Reguero } 8209805992fSJose Alberto Reguero 821786baecfSMauro Carvalho Chehab static int af9035_frontend_attach(struct dvb_usb_adapter *adap) 822786baecfSMauro Carvalho Chehab { 823786baecfSMauro Carvalho Chehab struct state *state = adap_to_priv(adap); 824786baecfSMauro Carvalho Chehab struct dvb_usb_device *d = adap_to_d(adap); 825786baecfSMauro Carvalho Chehab int ret; 826786baecfSMauro Carvalho Chehab 827786baecfSMauro Carvalho Chehab if (!state->af9033_config[adap->id].tuner) { 828786baecfSMauro Carvalho Chehab /* unsupported tuner */ 829786baecfSMauro Carvalho Chehab ret = -ENODEV; 830786baecfSMauro Carvalho Chehab goto err; 831786baecfSMauro Carvalho Chehab } 832786baecfSMauro Carvalho Chehab 833786baecfSMauro Carvalho Chehab if (adap->id == 0) { 834786baecfSMauro Carvalho Chehab state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB; 835786baecfSMauro Carvalho Chehab state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL; 836786baecfSMauro Carvalho Chehab 837786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00417f, 838786baecfSMauro Carvalho Chehab state->af9033_config[1].i2c_addr); 839786baecfSMauro Carvalho Chehab if (ret < 0) 840786baecfSMauro Carvalho Chehab goto err; 841786baecfSMauro Carvalho Chehab 842bf97b637SAntti Palosaari ret = af9035_wr_reg(d, 0x00d81a, state->dual_mode); 843786baecfSMauro Carvalho Chehab if (ret < 0) 844786baecfSMauro Carvalho Chehab goto err; 845786baecfSMauro Carvalho Chehab } 846786baecfSMauro Carvalho Chehab 847786baecfSMauro Carvalho Chehab /* attach demodulator */ 848bf97b637SAntti Palosaari adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id], 849bf97b637SAntti Palosaari &d->i2c_adap); 850786baecfSMauro Carvalho Chehab if (adap->fe[0] == NULL) { 851786baecfSMauro Carvalho Chehab ret = -ENODEV; 852786baecfSMauro Carvalho Chehab goto err; 853786baecfSMauro Carvalho Chehab } 854786baecfSMauro Carvalho Chehab 855786baecfSMauro Carvalho Chehab /* disable I2C-gate */ 856786baecfSMauro Carvalho Chehab adap->fe[0]->ops.i2c_gate_ctrl = NULL; 857786baecfSMauro Carvalho Chehab adap->fe[0]->callback = af9035_frontend_callback; 858786baecfSMauro Carvalho Chehab 859786baecfSMauro Carvalho Chehab return 0; 860786baecfSMauro Carvalho Chehab 861786baecfSMauro Carvalho Chehab err: 862119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 863786baecfSMauro Carvalho Chehab 864786baecfSMauro Carvalho Chehab return ret; 865786baecfSMauro Carvalho Chehab } 866786baecfSMauro Carvalho Chehab 867786baecfSMauro Carvalho Chehab static struct tua9001_config af9035_tua9001_config = { 868786baecfSMauro Carvalho Chehab .i2c_addr = 0x60, 869786baecfSMauro Carvalho Chehab }; 870786baecfSMauro Carvalho Chehab 871786baecfSMauro Carvalho Chehab static const struct fc0011_config af9035_fc0011_config = { 872786baecfSMauro Carvalho Chehab .i2c_address = 0x60, 873786baecfSMauro Carvalho Chehab }; 874786baecfSMauro Carvalho Chehab 8759805992fSJose Alberto Reguero static struct mxl5007t_config af9035_mxl5007t_config[] = { 8769805992fSJose Alberto Reguero { 877786baecfSMauro Carvalho Chehab .xtal_freq_hz = MxL_XTAL_24_MHZ, 878786baecfSMauro Carvalho Chehab .if_freq_hz = MxL_IF_4_57_MHZ, 879786baecfSMauro Carvalho Chehab .invert_if = 0, 880786baecfSMauro Carvalho Chehab .loop_thru_enable = 0, 881786baecfSMauro Carvalho Chehab .clk_out_enable = 0, 882786baecfSMauro Carvalho Chehab .clk_out_amp = MxL_CLKOUT_AMP_0_94V, 8839805992fSJose Alberto Reguero }, { 8849805992fSJose Alberto Reguero .xtal_freq_hz = MxL_XTAL_24_MHZ, 8859805992fSJose Alberto Reguero .if_freq_hz = MxL_IF_4_57_MHZ, 8869805992fSJose Alberto Reguero .invert_if = 0, 8879805992fSJose Alberto Reguero .loop_thru_enable = 1, 8889805992fSJose Alberto Reguero .clk_out_enable = 1, 8899805992fSJose Alberto Reguero .clk_out_amp = MxL_CLKOUT_AMP_0_94V, 8909805992fSJose Alberto Reguero } 891786baecfSMauro Carvalho Chehab }; 892786baecfSMauro Carvalho Chehab 893786baecfSMauro Carvalho Chehab static struct tda18218_config af9035_tda18218_config = { 894786baecfSMauro Carvalho Chehab .i2c_address = 0x60, 895786baecfSMauro Carvalho Chehab .i2c_wr_max = 21, 896786baecfSMauro Carvalho Chehab }; 897786baecfSMauro Carvalho Chehab 898d67ceb33SOliver Schinagl static const struct fc2580_config af9035_fc2580_config = { 899d67ceb33SOliver Schinagl .i2c_addr = 0x56, 900d67ceb33SOliver Schinagl .clock = 16384000, 901d67ceb33SOliver Schinagl }; 902d67ceb33SOliver Schinagl 903ad3a758bSAntti Palosaari static const struct fc0012_config af9035_fc0012_config = { 904ad3a758bSAntti Palosaari .i2c_address = 0x63, 905ad3a758bSAntti Palosaari .xtal_freq = FC_XTAL_36_MHZ, 906ad3a758bSAntti Palosaari .dual_master = 1, 907ad3a758bSAntti Palosaari }; 908ad3a758bSAntti Palosaari 909786baecfSMauro Carvalho Chehab static int af9035_tuner_attach(struct dvb_usb_adapter *adap) 910786baecfSMauro Carvalho Chehab { 911786baecfSMauro Carvalho Chehab struct state *state = adap_to_priv(adap); 912786baecfSMauro Carvalho Chehab struct dvb_usb_device *d = adap_to_d(adap); 913786baecfSMauro Carvalho Chehab int ret; 914786baecfSMauro Carvalho Chehab struct dvb_frontend *fe; 915bf97b637SAntti Palosaari u8 tuner_addr; 916bf97b637SAntti Palosaari /* 917bf97b637SAntti Palosaari * XXX: Hack used in that function: we abuse unused I2C address bit [7] 918bf97b637SAntti Palosaari * to carry info about used I2C bus for dual tuner configuration. 919bf97b637SAntti Palosaari */ 920786baecfSMauro Carvalho Chehab 921786baecfSMauro Carvalho Chehab switch (state->af9033_config[adap->id].tuner) { 922786baecfSMauro Carvalho Chehab case AF9033_TUNER_TUA9001: 923786baecfSMauro Carvalho Chehab /* AF9035 gpiot3 = TUA9001 RESETN 924786baecfSMauro Carvalho Chehab AF9035 gpiot2 = TUA9001 RXEN */ 925786baecfSMauro Carvalho Chehab 926786baecfSMauro Carvalho Chehab /* configure gpiot2 and gpiot2 as output */ 927786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01); 928786baecfSMauro Carvalho Chehab if (ret < 0) 929786baecfSMauro Carvalho Chehab goto err; 930786baecfSMauro Carvalho Chehab 931786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01); 932786baecfSMauro Carvalho Chehab if (ret < 0) 933786baecfSMauro Carvalho Chehab goto err; 934786baecfSMauro Carvalho Chehab 935786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01); 936786baecfSMauro Carvalho Chehab if (ret < 0) 937786baecfSMauro Carvalho Chehab goto err; 938786baecfSMauro Carvalho Chehab 939786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01); 940786baecfSMauro Carvalho Chehab if (ret < 0) 941786baecfSMauro Carvalho Chehab goto err; 942786baecfSMauro Carvalho Chehab 943786baecfSMauro Carvalho Chehab /* attach tuner */ 944786baecfSMauro Carvalho Chehab fe = dvb_attach(tua9001_attach, adap->fe[0], 945786baecfSMauro Carvalho Chehab &d->i2c_adap, &af9035_tua9001_config); 946786baecfSMauro Carvalho Chehab break; 947786baecfSMauro Carvalho Chehab case AF9033_TUNER_FC0011: 948786baecfSMauro Carvalho Chehab fe = dvb_attach(fc0011_attach, adap->fe[0], 949786baecfSMauro Carvalho Chehab &d->i2c_adap, &af9035_fc0011_config); 950786baecfSMauro Carvalho Chehab break; 951786baecfSMauro Carvalho Chehab case AF9033_TUNER_MXL5007T: 9529805992fSJose Alberto Reguero if (adap->id == 0) { 953786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8e0, 1); 954786baecfSMauro Carvalho Chehab if (ret < 0) 955786baecfSMauro Carvalho Chehab goto err; 956bf97b637SAntti Palosaari 957786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8e1, 1); 958786baecfSMauro Carvalho Chehab if (ret < 0) 959786baecfSMauro Carvalho Chehab goto err; 960bf97b637SAntti Palosaari 961786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8df, 0); 962786baecfSMauro Carvalho Chehab if (ret < 0) 963786baecfSMauro Carvalho Chehab goto err; 964786baecfSMauro Carvalho Chehab 965786baecfSMauro Carvalho Chehab msleep(30); 966786baecfSMauro Carvalho Chehab 967786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8df, 1); 968786baecfSMauro Carvalho Chehab if (ret < 0) 969786baecfSMauro Carvalho Chehab goto err; 970786baecfSMauro Carvalho Chehab 971786baecfSMauro Carvalho Chehab msleep(300); 972786baecfSMauro Carvalho Chehab 973786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8c0, 1); 974786baecfSMauro Carvalho Chehab if (ret < 0) 975786baecfSMauro Carvalho Chehab goto err; 976bf97b637SAntti Palosaari 977786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8c1, 1); 978786baecfSMauro Carvalho Chehab if (ret < 0) 979786baecfSMauro Carvalho Chehab goto err; 980bf97b637SAntti Palosaari 981786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8bf, 0); 982786baecfSMauro Carvalho Chehab if (ret < 0) 983786baecfSMauro Carvalho Chehab goto err; 984bf97b637SAntti Palosaari 985786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8b4, 1); 986786baecfSMauro Carvalho Chehab if (ret < 0) 987786baecfSMauro Carvalho Chehab goto err; 988bf97b637SAntti Palosaari 989786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8b5, 1); 990786baecfSMauro Carvalho Chehab if (ret < 0) 991786baecfSMauro Carvalho Chehab goto err; 992bf97b637SAntti Palosaari 993786baecfSMauro Carvalho Chehab ret = af9035_wr_reg(d, 0x00d8b3, 1); 994786baecfSMauro Carvalho Chehab if (ret < 0) 995786baecfSMauro Carvalho Chehab goto err; 996bf97b637SAntti Palosaari 997bf97b637SAntti Palosaari tuner_addr = 0x60; 998bf97b637SAntti Palosaari } else { 999bf97b637SAntti Palosaari tuner_addr = 0x60 | 0x80; /* I2C bus hack */ 10009805992fSJose Alberto Reguero } 1001786baecfSMauro Carvalho Chehab 1002786baecfSMauro Carvalho Chehab /* attach tuner */ 1003bf97b637SAntti Palosaari fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap, 1004bf97b637SAntti Palosaari tuner_addr, &af9035_mxl5007t_config[adap->id]); 1005786baecfSMauro Carvalho Chehab break; 1006786baecfSMauro Carvalho Chehab case AF9033_TUNER_TDA18218: 1007786baecfSMauro Carvalho Chehab /* attach tuner */ 1008786baecfSMauro Carvalho Chehab fe = dvb_attach(tda18218_attach, adap->fe[0], 1009786baecfSMauro Carvalho Chehab &d->i2c_adap, &af9035_tda18218_config); 1010786baecfSMauro Carvalho Chehab break; 1011d67ceb33SOliver Schinagl case AF9033_TUNER_FC2580: 1012d67ceb33SOliver Schinagl /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */ 1013d67ceb33SOliver Schinagl ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01); 1014d67ceb33SOliver Schinagl if (ret < 0) 1015d67ceb33SOliver Schinagl goto err; 1016d67ceb33SOliver Schinagl 1017d67ceb33SOliver Schinagl ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01); 1018d67ceb33SOliver Schinagl if (ret < 0) 1019d67ceb33SOliver Schinagl goto err; 1020d67ceb33SOliver Schinagl 1021d67ceb33SOliver Schinagl ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01); 1022d67ceb33SOliver Schinagl if (ret < 0) 1023d67ceb33SOliver Schinagl goto err; 1024d67ceb33SOliver Schinagl 1025d67ceb33SOliver Schinagl usleep_range(10000, 50000); 1026d67ceb33SOliver Schinagl /* attach tuner */ 1027d67ceb33SOliver Schinagl fe = dvb_attach(fc2580_attach, adap->fe[0], 1028d67ceb33SOliver Schinagl &d->i2c_adap, &af9035_fc2580_config); 1029d67ceb33SOliver Schinagl break; 10307e0bc296SAntti Palosaari case AF9033_TUNER_FC0012: 10317e0bc296SAntti Palosaari /* 10327e0bc296SAntti Palosaari * AF9035 gpiot2 = FC0012 enable 10337e0bc296SAntti Palosaari * XXX: there seems to be something on gpioh8 too, but on my 10347e0bc296SAntti Palosaari * my test I didn't find any difference. 10357e0bc296SAntti Palosaari */ 10367e0bc296SAntti Palosaari 10377e0bc296SAntti Palosaari /* configure gpiot2 as output and high */ 10387e0bc296SAntti Palosaari ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01); 10397e0bc296SAntti Palosaari if (ret < 0) 10407e0bc296SAntti Palosaari goto err; 10417e0bc296SAntti Palosaari 10427e0bc296SAntti Palosaari ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01); 10437e0bc296SAntti Palosaari if (ret < 0) 10447e0bc296SAntti Palosaari goto err; 10457e0bc296SAntti Palosaari 10467e0bc296SAntti Palosaari ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01); 10477e0bc296SAntti Palosaari if (ret < 0) 10487e0bc296SAntti Palosaari goto err; 10497e0bc296SAntti Palosaari 10507e0bc296SAntti Palosaari usleep_range(10000, 50000); 10517e0bc296SAntti Palosaari 1052ad3a758bSAntti Palosaari fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap, 1053ad3a758bSAntti Palosaari &af9035_fc0012_config); 10547e0bc296SAntti Palosaari break; 1055786baecfSMauro Carvalho Chehab default: 1056786baecfSMauro Carvalho Chehab fe = NULL; 1057786baecfSMauro Carvalho Chehab } 1058786baecfSMauro Carvalho Chehab 1059786baecfSMauro Carvalho Chehab if (fe == NULL) { 1060786baecfSMauro Carvalho Chehab ret = -ENODEV; 1061786baecfSMauro Carvalho Chehab goto err; 1062786baecfSMauro Carvalho Chehab } 1063786baecfSMauro Carvalho Chehab 1064786baecfSMauro Carvalho Chehab return 0; 1065786baecfSMauro Carvalho Chehab 1066786baecfSMauro Carvalho Chehab err: 1067119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1068786baecfSMauro Carvalho Chehab 1069786baecfSMauro Carvalho Chehab return ret; 1070786baecfSMauro Carvalho Chehab } 1071786baecfSMauro Carvalho Chehab 1072786baecfSMauro Carvalho Chehab static int af9035_init(struct dvb_usb_device *d) 1073786baecfSMauro Carvalho Chehab { 1074786baecfSMauro Carvalho Chehab struct state *state = d_to_priv(d); 1075786baecfSMauro Carvalho Chehab int ret, i; 1076786baecfSMauro Carvalho Chehab u16 frame_size = 87 * 188 / 4; 1077786baecfSMauro Carvalho Chehab u8 packet_size = 512 / 4; 1078786baecfSMauro Carvalho Chehab struct reg_val_mask tab[] = { 1079786baecfSMauro Carvalho Chehab { 0x80f99d, 0x01, 0x01 }, 1080786baecfSMauro Carvalho Chehab { 0x80f9a4, 0x01, 0x01 }, 1081786baecfSMauro Carvalho Chehab { 0x00dd11, 0x00, 0x20 }, 1082786baecfSMauro Carvalho Chehab { 0x00dd11, 0x00, 0x40 }, 1083786baecfSMauro Carvalho Chehab { 0x00dd13, 0x00, 0x20 }, 1084786baecfSMauro Carvalho Chehab { 0x00dd13, 0x00, 0x40 }, 1085786baecfSMauro Carvalho Chehab { 0x00dd11, 0x20, 0x20 }, 1086786baecfSMauro Carvalho Chehab { 0x00dd88, (frame_size >> 0) & 0xff, 0xff}, 1087786baecfSMauro Carvalho Chehab { 0x00dd89, (frame_size >> 8) & 0xff, 0xff}, 1088786baecfSMauro Carvalho Chehab { 0x00dd0c, packet_size, 0xff}, 1089786baecfSMauro Carvalho Chehab { 0x00dd11, state->dual_mode << 6, 0x40 }, 1090786baecfSMauro Carvalho Chehab { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff}, 1091786baecfSMauro Carvalho Chehab { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff}, 1092786baecfSMauro Carvalho Chehab { 0x00dd0d, packet_size, 0xff }, 10939805992fSJose Alberto Reguero { 0x80f9a3, state->dual_mode, 0x01 }, 10949805992fSJose Alberto Reguero { 0x80f9cd, state->dual_mode, 0x01 }, 1095786baecfSMauro Carvalho Chehab { 0x80f99d, 0x00, 0x01 }, 1096786baecfSMauro Carvalho Chehab { 0x80f9a4, 0x00, 0x01 }, 1097786baecfSMauro Carvalho Chehab }; 1098786baecfSMauro Carvalho Chehab 1099119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \ 1100119f7a8cSAntti Palosaari "packet_size=%02x\n", __func__, 1101119f7a8cSAntti Palosaari d->udev->speed, frame_size, packet_size); 1102786baecfSMauro Carvalho Chehab 1103786baecfSMauro Carvalho Chehab /* init endpoints */ 1104786baecfSMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(tab); i++) { 1105786baecfSMauro Carvalho Chehab ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val, 1106786baecfSMauro Carvalho Chehab tab[i].mask); 1107786baecfSMauro Carvalho Chehab if (ret < 0) 1108786baecfSMauro Carvalho Chehab goto err; 1109786baecfSMauro Carvalho Chehab } 1110786baecfSMauro Carvalho Chehab 1111786baecfSMauro Carvalho Chehab return 0; 1112786baecfSMauro Carvalho Chehab 1113786baecfSMauro Carvalho Chehab err: 1114119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1115786baecfSMauro Carvalho Chehab 1116786baecfSMauro Carvalho Chehab return ret; 1117786baecfSMauro Carvalho Chehab } 1118786baecfSMauro Carvalho Chehab 1119786baecfSMauro Carvalho Chehab static int af9035_rc_query(struct dvb_usb_device *d) 1120786baecfSMauro Carvalho Chehab { 1121786baecfSMauro Carvalho Chehab unsigned int key; 1122786baecfSMauro Carvalho Chehab unsigned char b[4]; 1123786baecfSMauro Carvalho Chehab int ret; 1124786baecfSMauro Carvalho Chehab struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b }; 1125786baecfSMauro Carvalho Chehab 1126786baecfSMauro Carvalho Chehab ret = af9035_ctrl_msg(d, &req); 1127786baecfSMauro Carvalho Chehab if (ret < 0) 1128786baecfSMauro Carvalho Chehab goto err; 1129786baecfSMauro Carvalho Chehab 1130786baecfSMauro Carvalho Chehab if ((b[2] + b[3]) == 0xff) { 1131786baecfSMauro Carvalho Chehab if ((b[0] + b[1]) == 0xff) { 1132786baecfSMauro Carvalho Chehab /* NEC */ 1133786baecfSMauro Carvalho Chehab key = b[0] << 8 | b[2]; 1134786baecfSMauro Carvalho Chehab } else { 1135786baecfSMauro Carvalho Chehab /* ext. NEC */ 1136786baecfSMauro Carvalho Chehab key = b[0] << 16 | b[1] << 8 | b[2]; 1137786baecfSMauro Carvalho Chehab } 1138786baecfSMauro Carvalho Chehab } else { 1139786baecfSMauro Carvalho Chehab key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]; 1140786baecfSMauro Carvalho Chehab } 1141786baecfSMauro Carvalho Chehab 1142786baecfSMauro Carvalho Chehab rc_keydown(d->rc_dev, key, 0); 1143786baecfSMauro Carvalho Chehab 1144786baecfSMauro Carvalho Chehab err: 1145786baecfSMauro Carvalho Chehab /* ignore errors */ 1146786baecfSMauro Carvalho Chehab return 0; 1147786baecfSMauro Carvalho Chehab } 1148786baecfSMauro Carvalho Chehab 1149786baecfSMauro Carvalho Chehab static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc) 1150786baecfSMauro Carvalho Chehab { 1151786baecfSMauro Carvalho Chehab int ret; 1152786baecfSMauro Carvalho Chehab u8 tmp; 1153786baecfSMauro Carvalho Chehab 1154786baecfSMauro Carvalho Chehab ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp); 1155786baecfSMauro Carvalho Chehab if (ret < 0) 1156786baecfSMauro Carvalho Chehab goto err; 1157786baecfSMauro Carvalho Chehab 1158119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp); 1159786baecfSMauro Carvalho Chehab 1160786baecfSMauro Carvalho Chehab /* don't activate rc if in HID mode or if not available */ 1161786baecfSMauro Carvalho Chehab if (tmp == 5) { 1162786baecfSMauro Carvalho Chehab ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp); 1163786baecfSMauro Carvalho Chehab if (ret < 0) 1164786baecfSMauro Carvalho Chehab goto err; 1165786baecfSMauro Carvalho Chehab 1166119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp); 1167786baecfSMauro Carvalho Chehab 1168786baecfSMauro Carvalho Chehab switch (tmp) { 1169786baecfSMauro Carvalho Chehab case 0: /* NEC */ 1170786baecfSMauro Carvalho Chehab default: 1171c003ab1bSDavid Härdeman rc->allowed_protos = RC_BIT_NEC; 1172786baecfSMauro Carvalho Chehab break; 1173786baecfSMauro Carvalho Chehab case 1: /* RC6 */ 1174c003ab1bSDavid Härdeman rc->allowed_protos = RC_BIT_RC6_MCE; 1175786baecfSMauro Carvalho Chehab break; 1176786baecfSMauro Carvalho Chehab } 1177786baecfSMauro Carvalho Chehab 1178786baecfSMauro Carvalho Chehab rc->query = af9035_rc_query; 1179786baecfSMauro Carvalho Chehab rc->interval = 500; 1180786baecfSMauro Carvalho Chehab 1181786baecfSMauro Carvalho Chehab /* load empty to enable rc */ 1182786baecfSMauro Carvalho Chehab if (!rc->map_name) 1183786baecfSMauro Carvalho Chehab rc->map_name = RC_MAP_EMPTY; 1184786baecfSMauro Carvalho Chehab } 1185786baecfSMauro Carvalho Chehab 1186786baecfSMauro Carvalho Chehab return 0; 1187786baecfSMauro Carvalho Chehab 1188786baecfSMauro Carvalho Chehab err: 1189119f7a8cSAntti Palosaari dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1190786baecfSMauro Carvalho Chehab 1191786baecfSMauro Carvalho Chehab return ret; 1192786baecfSMauro Carvalho Chehab } 1193786baecfSMauro Carvalho Chehab 1194786baecfSMauro Carvalho Chehab /* interface 0 is used by DVB-T receiver and 1195786baecfSMauro Carvalho Chehab interface 1 is for remote controller (HID) */ 1196786baecfSMauro Carvalho Chehab static const struct dvb_usb_device_properties af9035_props = { 1197786baecfSMauro Carvalho Chehab .driver_name = KBUILD_MODNAME, 1198786baecfSMauro Carvalho Chehab .owner = THIS_MODULE, 1199786baecfSMauro Carvalho Chehab .adapter_nr = adapter_nr, 1200786baecfSMauro Carvalho Chehab .size_of_priv = sizeof(struct state), 1201786baecfSMauro Carvalho Chehab 1202786baecfSMauro Carvalho Chehab .generic_bulk_ctrl_endpoint = 0x02, 1203786baecfSMauro Carvalho Chehab .generic_bulk_ctrl_endpoint_response = 0x81, 1204786baecfSMauro Carvalho Chehab 1205786baecfSMauro Carvalho Chehab .identify_state = af9035_identify_state, 12064395e4b7SAntti Palosaari .firmware = AF9035_FIRMWARE_AF9035, 1207786baecfSMauro Carvalho Chehab .download_firmware = af9035_download_firmware, 1208786baecfSMauro Carvalho Chehab 1209786baecfSMauro Carvalho Chehab .i2c_algo = &af9035_i2c_algo, 1210786baecfSMauro Carvalho Chehab .read_config = af9035_read_config, 1211786baecfSMauro Carvalho Chehab .frontend_attach = af9035_frontend_attach, 1212786baecfSMauro Carvalho Chehab .tuner_attach = af9035_tuner_attach, 1213786baecfSMauro Carvalho Chehab .init = af9035_init, 1214786baecfSMauro Carvalho Chehab .get_rc_config = af9035_get_rc_config, 1215786baecfSMauro Carvalho Chehab 12169805992fSJose Alberto Reguero .get_adapter_count = af9035_get_adapter_count, 1217786baecfSMauro Carvalho Chehab .adapter = { 1218786baecfSMauro Carvalho Chehab { 1219786baecfSMauro Carvalho Chehab .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188), 1220786baecfSMauro Carvalho Chehab }, { 1221786baecfSMauro Carvalho Chehab .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188), 1222786baecfSMauro Carvalho Chehab }, 1223786baecfSMauro Carvalho Chehab }, 1224786baecfSMauro Carvalho Chehab }; 1225786baecfSMauro Carvalho Chehab 1226786baecfSMauro Carvalho Chehab static const struct dvb_usb_device_properties it9135_props = { 1227786baecfSMauro Carvalho Chehab .driver_name = KBUILD_MODNAME, 1228786baecfSMauro Carvalho Chehab .owner = THIS_MODULE, 1229786baecfSMauro Carvalho Chehab .adapter_nr = adapter_nr, 1230786baecfSMauro Carvalho Chehab .size_of_priv = sizeof(struct state), 1231786baecfSMauro Carvalho Chehab 1232786baecfSMauro Carvalho Chehab .generic_bulk_ctrl_endpoint = 0x02, 1233786baecfSMauro Carvalho Chehab .generic_bulk_ctrl_endpoint_response = 0x81, 1234786baecfSMauro Carvalho Chehab 1235786baecfSMauro Carvalho Chehab .identify_state = af9035_identify_state, 12364395e4b7SAntti Palosaari .firmware = AF9035_FIRMWARE_IT9135, 1237786baecfSMauro Carvalho Chehab .download_firmware = af9035_download_firmware_it9135, 1238786baecfSMauro Carvalho Chehab 1239786baecfSMauro Carvalho Chehab .i2c_algo = &af9035_i2c_algo, 1240786baecfSMauro Carvalho Chehab .read_config = af9035_read_config_it9135, 1241786baecfSMauro Carvalho Chehab .frontend_attach = af9035_frontend_attach, 1242786baecfSMauro Carvalho Chehab .tuner_attach = af9035_tuner_attach, 1243786baecfSMauro Carvalho Chehab .init = af9035_init, 1244786baecfSMauro Carvalho Chehab .get_rc_config = af9035_get_rc_config, 1245786baecfSMauro Carvalho Chehab 1246786baecfSMauro Carvalho Chehab .num_adapters = 1, 1247786baecfSMauro Carvalho Chehab .adapter = { 1248786baecfSMauro Carvalho Chehab { 1249786baecfSMauro Carvalho Chehab .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188), 1250786baecfSMauro Carvalho Chehab }, { 1251786baecfSMauro Carvalho Chehab .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188), 1252786baecfSMauro Carvalho Chehab }, 1253786baecfSMauro Carvalho Chehab }, 1254786baecfSMauro Carvalho Chehab }; 1255786baecfSMauro Carvalho Chehab 1256786baecfSMauro Carvalho Chehab static const struct usb_device_id af9035_id_table[] = { 1257786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035, 1258786baecfSMauro Carvalho Chehab &af9035_props, "Afatech AF9035 reference design", NULL) }, 1259786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000, 1260786baecfSMauro Carvalho Chehab &af9035_props, "Afatech AF9035 reference design", NULL) }, 1261786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001, 1262786baecfSMauro Carvalho Chehab &af9035_props, "Afatech AF9035 reference design", NULL) }, 1263786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002, 1264786baecfSMauro Carvalho Chehab &af9035_props, "Afatech AF9035 reference design", NULL) }, 1265786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003, 1266786baecfSMauro Carvalho Chehab &af9035_props, "Afatech AF9035 reference design", NULL) }, 1267786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK, 1268786baecfSMauro Carvalho Chehab &af9035_props, "TerraTec Cinergy T Stick", NULL) }, 1269786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835, 1270786baecfSMauro Carvalho Chehab &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) }, 1271786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835, 1272786baecfSMauro Carvalho Chehab &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) }, 1273786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867, 1274786baecfSMauro Carvalho Chehab &af9035_props, "AVerMedia HD Volar (A867)", NULL) }, 1275786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867, 1276786baecfSMauro Carvalho Chehab &af9035_props, "AVerMedia HD Volar (A867)", NULL) }, 1277786baecfSMauro Carvalho Chehab { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR, 1278786baecfSMauro Carvalho Chehab &af9035_props, "AVerMedia Twinstar (A825)", NULL) }, 1279d67ceb33SOliver Schinagl { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS, 1280d67ceb33SOliver Schinagl &af9035_props, "Asus U3100Mini Plus", NULL) }, 1281786baecfSMauro Carvalho Chehab { } 1282786baecfSMauro Carvalho Chehab }; 1283786baecfSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, af9035_id_table); 1284786baecfSMauro Carvalho Chehab 1285786baecfSMauro Carvalho Chehab static struct usb_driver af9035_usb_driver = { 1286786baecfSMauro Carvalho Chehab .name = KBUILD_MODNAME, 1287786baecfSMauro Carvalho Chehab .id_table = af9035_id_table, 1288786baecfSMauro Carvalho Chehab .probe = dvb_usbv2_probe, 1289786baecfSMauro Carvalho Chehab .disconnect = dvb_usbv2_disconnect, 1290786baecfSMauro Carvalho Chehab .suspend = dvb_usbv2_suspend, 1291786baecfSMauro Carvalho Chehab .resume = dvb_usbv2_resume, 129204966aa8SAntti Palosaari .reset_resume = dvb_usbv2_reset_resume, 1293786baecfSMauro Carvalho Chehab .no_dynamic_id = 1, 1294786baecfSMauro Carvalho Chehab .soft_unbind = 1, 1295786baecfSMauro Carvalho Chehab }; 1296786baecfSMauro Carvalho Chehab 1297786baecfSMauro Carvalho Chehab module_usb_driver(af9035_usb_driver); 1298786baecfSMauro Carvalho Chehab 1299786baecfSMauro Carvalho Chehab MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1300786baecfSMauro Carvalho Chehab MODULE_DESCRIPTION("Afatech AF9035 driver"); 1301786baecfSMauro Carvalho Chehab MODULE_LICENSE("GPL"); 13024395e4b7SAntti Palosaari MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035); 13034395e4b7SAntti Palosaari MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135); 1304