1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 23785bc17SMauro Carvalho Chehab /* 33785bc17SMauro Carvalho Chehab * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III 43785bc17SMauro Carvalho Chehab * flexcop-i2c.c - flexcop internal 2Wire bus (I2C) and dvb i2c initialization 53785bc17SMauro Carvalho Chehab * see flexcop.c for copyright information 63785bc17SMauro Carvalho Chehab */ 73785bc17SMauro Carvalho Chehab #include "flexcop.h" 83785bc17SMauro Carvalho Chehab 93785bc17SMauro Carvalho Chehab #define FC_MAX_I2C_RETRIES 100000 103785bc17SMauro Carvalho Chehab 113785bc17SMauro Carvalho Chehab static int flexcop_i2c_operation(struct flexcop_device *fc, 123785bc17SMauro Carvalho Chehab flexcop_ibi_value *r100) 133785bc17SMauro Carvalho Chehab { 143785bc17SMauro Carvalho Chehab int i; 153785bc17SMauro Carvalho Chehab flexcop_ibi_value r; 163785bc17SMauro Carvalho Chehab 173785bc17SMauro Carvalho Chehab r100->tw_sm_c_100.working_start = 1; 183785bc17SMauro Carvalho Chehab deb_i2c("r100 before: %08x\n",r100->raw); 193785bc17SMauro Carvalho Chehab 203785bc17SMauro Carvalho Chehab fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero); 213785bc17SMauro Carvalho Chehab fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */ 223785bc17SMauro Carvalho Chehab 233785bc17SMauro Carvalho Chehab for (i = 0; i < FC_MAX_I2C_RETRIES; i++) { 243785bc17SMauro Carvalho Chehab r = fc->read_ibi_reg(fc, tw_sm_c_100); 253785bc17SMauro Carvalho Chehab 263785bc17SMauro Carvalho Chehab if (!r.tw_sm_c_100.no_base_addr_ack_error) { 273785bc17SMauro Carvalho Chehab if (r.tw_sm_c_100.st_done) { 283785bc17SMauro Carvalho Chehab *r100 = r; 293785bc17SMauro Carvalho Chehab deb_i2c("i2c success\n"); 303785bc17SMauro Carvalho Chehab return 0; 313785bc17SMauro Carvalho Chehab } 323785bc17SMauro Carvalho Chehab } else { 333785bc17SMauro Carvalho Chehab deb_i2c("suffering from an i2c ack_error\n"); 343785bc17SMauro Carvalho Chehab return -EREMOTEIO; 353785bc17SMauro Carvalho Chehab } 363785bc17SMauro Carvalho Chehab } 37d9942ad0SMauro Carvalho Chehab deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n", 38d9942ad0SMauro Carvalho Chehab i); 393785bc17SMauro Carvalho Chehab return -EREMOTEIO; 403785bc17SMauro Carvalho Chehab } 413785bc17SMauro Carvalho Chehab 423785bc17SMauro Carvalho Chehab static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c, 433785bc17SMauro Carvalho Chehab flexcop_ibi_value r100, u8 *buf) 443785bc17SMauro Carvalho Chehab { 453785bc17SMauro Carvalho Chehab flexcop_ibi_value r104; 463785bc17SMauro Carvalho Chehab int len = r100.tw_sm_c_100.total_bytes, 473785bc17SMauro Carvalho Chehab /* remember total_bytes is buflen-1 */ 483785bc17SMauro Carvalho Chehab ret; 493785bc17SMauro Carvalho Chehab 503785bc17SMauro Carvalho Chehab /* work-around to have CableStar2 and SkyStar2 rev 2.7 work 513785bc17SMauro Carvalho Chehab * correctly: 523785bc17SMauro Carvalho Chehab * 533785bc17SMauro Carvalho Chehab * the ITD1000 is behind an i2c-gate which closes automatically 543785bc17SMauro Carvalho Chehab * after an i2c-transaction the STV0297 needs 2 consecutive reads 553785bc17SMauro Carvalho Chehab * one with no_base_addr = 0 and one with 1 563785bc17SMauro Carvalho Chehab * 573785bc17SMauro Carvalho Chehab * those two work-arounds are conflictin: we check for the card 583785bc17SMauro Carvalho Chehab * type, it is set when probing the ITD1000 */ 593785bc17SMauro Carvalho Chehab if (i2c->fc->dev_type == FC_SKY_REV27) 603785bc17SMauro Carvalho Chehab r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr; 613785bc17SMauro Carvalho Chehab 623785bc17SMauro Carvalho Chehab ret = flexcop_i2c_operation(i2c->fc, &r100); 633785bc17SMauro Carvalho Chehab if (ret != 0) { 643785bc17SMauro Carvalho Chehab deb_i2c("Retrying operation\n"); 653785bc17SMauro Carvalho Chehab r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr; 663785bc17SMauro Carvalho Chehab ret = flexcop_i2c_operation(i2c->fc, &r100); 673785bc17SMauro Carvalho Chehab } 683785bc17SMauro Carvalho Chehab if (ret != 0) { 693785bc17SMauro Carvalho Chehab deb_i2c("read failed. %d\n", ret); 703785bc17SMauro Carvalho Chehab return ret; 713785bc17SMauro Carvalho Chehab } 723785bc17SMauro Carvalho Chehab 733785bc17SMauro Carvalho Chehab buf[0] = r100.tw_sm_c_100.data1_reg; 743785bc17SMauro Carvalho Chehab 753785bc17SMauro Carvalho Chehab if (len > 0) { 763785bc17SMauro Carvalho Chehab r104 = i2c->fc->read_ibi_reg(i2c->fc, tw_sm_c_104); 773785bc17SMauro Carvalho Chehab deb_i2c("read: r100: %08x, r104: %08x\n", r100.raw, r104.raw); 783785bc17SMauro Carvalho Chehab 793785bc17SMauro Carvalho Chehab /* there is at least one more byte, otherwise we wouldn't be here */ 803785bc17SMauro Carvalho Chehab buf[1] = r104.tw_sm_c_104.data2_reg; 813785bc17SMauro Carvalho Chehab if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg; 823785bc17SMauro Carvalho Chehab if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg; 833785bc17SMauro Carvalho Chehab } 843785bc17SMauro Carvalho Chehab return 0; 853785bc17SMauro Carvalho Chehab } 863785bc17SMauro Carvalho Chehab 873785bc17SMauro Carvalho Chehab static int flexcop_i2c_write4(struct flexcop_device *fc, 883785bc17SMauro Carvalho Chehab flexcop_ibi_value r100, u8 *buf) 893785bc17SMauro Carvalho Chehab { 903785bc17SMauro Carvalho Chehab flexcop_ibi_value r104; 913785bc17SMauro Carvalho Chehab int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */ 923785bc17SMauro Carvalho Chehab r104.raw = 0; 933785bc17SMauro Carvalho Chehab 943785bc17SMauro Carvalho Chehab /* there is at least one byte, otherwise we wouldn't be here */ 953785bc17SMauro Carvalho Chehab r100.tw_sm_c_100.data1_reg = buf[0]; 963785bc17SMauro Carvalho Chehab r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0; 973785bc17SMauro Carvalho Chehab r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0; 983785bc17SMauro Carvalho Chehab r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0; 993785bc17SMauro Carvalho Chehab 1003785bc17SMauro Carvalho Chehab deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw); 1013785bc17SMauro Carvalho Chehab 1023785bc17SMauro Carvalho Chehab /* write the additional i2c data before doing the actual i2c operation */ 1033785bc17SMauro Carvalho Chehab fc->write_ibi_reg(fc, tw_sm_c_104, r104); 1043785bc17SMauro Carvalho Chehab return flexcop_i2c_operation(fc, &r100); 1053785bc17SMauro Carvalho Chehab } 1063785bc17SMauro Carvalho Chehab 1073785bc17SMauro Carvalho Chehab int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c, 10825d4affbSMauro Carvalho Chehab flexcop_access_op_t op, u8 chipaddr, 10925d4affbSMauro Carvalho Chehab u8 start_addr, u8 *buf, u16 size) 1103785bc17SMauro Carvalho Chehab { 1113785bc17SMauro Carvalho Chehab int ret; 11225d4affbSMauro Carvalho Chehab int len = size; 11325d4affbSMauro Carvalho Chehab u8 *p; 11425d4affbSMauro Carvalho Chehab u8 addr = start_addr; 1153785bc17SMauro Carvalho Chehab 1163785bc17SMauro Carvalho Chehab u16 bytes_to_transfer; 1173785bc17SMauro Carvalho Chehab flexcop_ibi_value r100; 1183785bc17SMauro Carvalho Chehab 11925d4affbSMauro Carvalho Chehab deb_i2c("port %d %s(%02x): register %02x, size: %d\n", 12025d4affbSMauro Carvalho Chehab i2c->port, 12125d4affbSMauro Carvalho Chehab op == FC_READ ? "rd" : "wr", 12225d4affbSMauro Carvalho Chehab chipaddr, start_addr, size); 1233785bc17SMauro Carvalho Chehab r100.raw = 0; 1243785bc17SMauro Carvalho Chehab r100.tw_sm_c_100.chipaddr = chipaddr; 1253785bc17SMauro Carvalho Chehab r100.tw_sm_c_100.twoWS_rw = op; 1263785bc17SMauro Carvalho Chehab r100.tw_sm_c_100.twoWS_port_reg = i2c->port; 1273785bc17SMauro Carvalho Chehab 1283785bc17SMauro Carvalho Chehab /* in that case addr is the only value -> 1293785bc17SMauro Carvalho Chehab * we write it twice as baseaddr and val0 1303785bc17SMauro Carvalho Chehab * BBTI is doing it like that for ISL6421 at least */ 1313785bc17SMauro Carvalho Chehab if (i2c->no_base_addr && len == 0 && op == FC_WRITE) { 13225d4affbSMauro Carvalho Chehab buf = &start_addr; 1333785bc17SMauro Carvalho Chehab len = 1; 1343785bc17SMauro Carvalho Chehab } 1353785bc17SMauro Carvalho Chehab 13625d4affbSMauro Carvalho Chehab p = buf; 13725d4affbSMauro Carvalho Chehab 1383785bc17SMauro Carvalho Chehab while (len != 0) { 1393785bc17SMauro Carvalho Chehab bytes_to_transfer = len > 4 ? 4 : len; 1403785bc17SMauro Carvalho Chehab 1413785bc17SMauro Carvalho Chehab r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1; 1423785bc17SMauro Carvalho Chehab r100.tw_sm_c_100.baseaddr = addr; 1433785bc17SMauro Carvalho Chehab 1443785bc17SMauro Carvalho Chehab if (op == FC_READ) 14525d4affbSMauro Carvalho Chehab ret = flexcop_i2c_read4(i2c, r100, p); 1463785bc17SMauro Carvalho Chehab else 14725d4affbSMauro Carvalho Chehab ret = flexcop_i2c_write4(i2c->fc, r100, p); 1483785bc17SMauro Carvalho Chehab 1493785bc17SMauro Carvalho Chehab if (ret < 0) 1503785bc17SMauro Carvalho Chehab return ret; 1513785bc17SMauro Carvalho Chehab 15225d4affbSMauro Carvalho Chehab p += bytes_to_transfer; 1533785bc17SMauro Carvalho Chehab addr += bytes_to_transfer; 1543785bc17SMauro Carvalho Chehab len -= bytes_to_transfer; 1553785bc17SMauro Carvalho Chehab } 15625d4affbSMauro Carvalho Chehab deb_i2c_dump("port %d %s(%02x): register %02x: %*ph\n", 15725d4affbSMauro Carvalho Chehab i2c->port, 15825d4affbSMauro Carvalho Chehab op == FC_READ ? "rd" : "wr", 15925d4affbSMauro Carvalho Chehab chipaddr, start_addr, size, buf); 1603785bc17SMauro Carvalho Chehab 1613785bc17SMauro Carvalho Chehab return 0; 1623785bc17SMauro Carvalho Chehab } 1633785bc17SMauro Carvalho Chehab /* exported for PCI i2c */ 1643785bc17SMauro Carvalho Chehab EXPORT_SYMBOL(flexcop_i2c_request); 1653785bc17SMauro Carvalho Chehab 1663785bc17SMauro Carvalho Chehab /* master xfer callback for demodulator */ 1673785bc17SMauro Carvalho Chehab static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, 1683785bc17SMauro Carvalho Chehab struct i2c_msg msgs[], int num) 1693785bc17SMauro Carvalho Chehab { 1703785bc17SMauro Carvalho Chehab struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap); 1713785bc17SMauro Carvalho Chehab int i, ret = 0; 1723785bc17SMauro Carvalho Chehab 1733785bc17SMauro Carvalho Chehab /* Some drivers use 1 byte or 0 byte reads as probes, which this 1743785bc17SMauro Carvalho Chehab * driver doesn't support. These probes will always fail, so this 1753785bc17SMauro Carvalho Chehab * hack makes them always succeed. If one knew how, it would of 1763785bc17SMauro Carvalho Chehab * course be better to actually do the read. */ 1773785bc17SMauro Carvalho Chehab if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1) 1783785bc17SMauro Carvalho Chehab return 1; 1793785bc17SMauro Carvalho Chehab 1803785bc17SMauro Carvalho Chehab if (mutex_lock_interruptible(&i2c->fc->i2c_mutex)) 1813785bc17SMauro Carvalho Chehab return -ERESTARTSYS; 1823785bc17SMauro Carvalho Chehab 1833785bc17SMauro Carvalho Chehab for (i = 0; i < num; i++) { 1843785bc17SMauro Carvalho Chehab /* reading */ 1853785bc17SMauro Carvalho Chehab if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) { 1863785bc17SMauro Carvalho Chehab ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr, 1873785bc17SMauro Carvalho Chehab msgs[i].buf[0], msgs[i+1].buf, 1883785bc17SMauro Carvalho Chehab msgs[i+1].len); 1893785bc17SMauro Carvalho Chehab i++; /* skip the following message */ 1903785bc17SMauro Carvalho Chehab } else /* writing */ 1913785bc17SMauro Carvalho Chehab ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr, 1923785bc17SMauro Carvalho Chehab msgs[i].buf[0], &msgs[i].buf[1], 1933785bc17SMauro Carvalho Chehab msgs[i].len - 1); 1943785bc17SMauro Carvalho Chehab if (ret < 0) { 1953785bc17SMauro Carvalho Chehab deb_i2c("i2c master_xfer failed"); 1963785bc17SMauro Carvalho Chehab break; 1973785bc17SMauro Carvalho Chehab } 1983785bc17SMauro Carvalho Chehab } 1993785bc17SMauro Carvalho Chehab 2003785bc17SMauro Carvalho Chehab mutex_unlock(&i2c->fc->i2c_mutex); 2013785bc17SMauro Carvalho Chehab 2023785bc17SMauro Carvalho Chehab if (ret == 0) 2033785bc17SMauro Carvalho Chehab ret = num; 2043785bc17SMauro Carvalho Chehab return ret; 2053785bc17SMauro Carvalho Chehab } 2063785bc17SMauro Carvalho Chehab 2073785bc17SMauro Carvalho Chehab static u32 flexcop_i2c_func(struct i2c_adapter *adapter) 2083785bc17SMauro Carvalho Chehab { 2093785bc17SMauro Carvalho Chehab return I2C_FUNC_I2C; 2103785bc17SMauro Carvalho Chehab } 2113785bc17SMauro Carvalho Chehab 2123785bc17SMauro Carvalho Chehab static struct i2c_algorithm flexcop_algo = { 2133785bc17SMauro Carvalho Chehab .master_xfer = flexcop_master_xfer, 2143785bc17SMauro Carvalho Chehab .functionality = flexcop_i2c_func, 2153785bc17SMauro Carvalho Chehab }; 2163785bc17SMauro Carvalho Chehab 2173785bc17SMauro Carvalho Chehab int flexcop_i2c_init(struct flexcop_device *fc) 2183785bc17SMauro Carvalho Chehab { 2193785bc17SMauro Carvalho Chehab int ret; 2203785bc17SMauro Carvalho Chehab mutex_init(&fc->i2c_mutex); 2213785bc17SMauro Carvalho Chehab 2223785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[0].fc = fc; 2233785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[1].fc = fc; 2243785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[2].fc = fc; 2253785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD; 2263785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM; 2273785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER; 2283785bc17SMauro Carvalho Chehab 229*c0decac1SMauro Carvalho Chehab strscpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod", 2303785bc17SMauro Carvalho Chehab sizeof(fc->fc_i2c_adap[0].i2c_adap.name)); 231*c0decac1SMauro Carvalho Chehab strscpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom", 2323785bc17SMauro Carvalho Chehab sizeof(fc->fc_i2c_adap[1].i2c_adap.name)); 233*c0decac1SMauro Carvalho Chehab strscpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner", 2343785bc17SMauro Carvalho Chehab sizeof(fc->fc_i2c_adap[2].i2c_adap.name)); 2353785bc17SMauro Carvalho Chehab 2363785bc17SMauro Carvalho Chehab i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]); 2373785bc17SMauro Carvalho Chehab i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]); 2383785bc17SMauro Carvalho Chehab i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]); 2393785bc17SMauro Carvalho Chehab 2403785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[0].i2c_adap.algo = 2413785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[1].i2c_adap.algo = 2423785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo; 2433785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[0].i2c_adap.algo_data = 2443785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[1].i2c_adap.algo_data = 2453785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL; 2463785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[0].i2c_adap.dev.parent = 2473785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[1].i2c_adap.dev.parent = 2483785bc17SMauro Carvalho Chehab fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev; 2493785bc17SMauro Carvalho Chehab 2503785bc17SMauro Carvalho Chehab ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap); 2513785bc17SMauro Carvalho Chehab if (ret < 0) 2523785bc17SMauro Carvalho Chehab return ret; 2533785bc17SMauro Carvalho Chehab 2543785bc17SMauro Carvalho Chehab ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap); 2553785bc17SMauro Carvalho Chehab if (ret < 0) 2563785bc17SMauro Carvalho Chehab goto adap_1_failed; 2573785bc17SMauro Carvalho Chehab 2583785bc17SMauro Carvalho Chehab ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap); 2593785bc17SMauro Carvalho Chehab if (ret < 0) 2603785bc17SMauro Carvalho Chehab goto adap_2_failed; 2613785bc17SMauro Carvalho Chehab 2623785bc17SMauro Carvalho Chehab fc->init_state |= FC_STATE_I2C_INIT; 2633785bc17SMauro Carvalho Chehab return 0; 2643785bc17SMauro Carvalho Chehab 2653785bc17SMauro Carvalho Chehab adap_2_failed: 2663785bc17SMauro Carvalho Chehab i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap); 2673785bc17SMauro Carvalho Chehab adap_1_failed: 2683785bc17SMauro Carvalho Chehab i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap); 2693785bc17SMauro Carvalho Chehab return ret; 2703785bc17SMauro Carvalho Chehab } 2713785bc17SMauro Carvalho Chehab 2723785bc17SMauro Carvalho Chehab void flexcop_i2c_exit(struct flexcop_device *fc) 2733785bc17SMauro Carvalho Chehab { 2743785bc17SMauro Carvalho Chehab if (fc->init_state & FC_STATE_I2C_INIT) { 2753785bc17SMauro Carvalho Chehab i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap); 2763785bc17SMauro Carvalho Chehab i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap); 2773785bc17SMauro Carvalho Chehab i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap); 2783785bc17SMauro Carvalho Chehab } 2793785bc17SMauro Carvalho Chehab fc->init_state &= ~FC_STATE_I2C_INIT; 2803785bc17SMauro Carvalho Chehab } 281