Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
0004  * flexcop-i2c.c - flexcop internal 2Wire bus (I2C) and dvb i2c initialization
0005  * see flexcop.c for copyright information
0006  */
0007 #include "flexcop.h"
0008 
0009 #define FC_MAX_I2C_RETRIES 100000
0010 
0011 static int flexcop_i2c_operation(struct flexcop_device *fc,
0012         flexcop_ibi_value *r100)
0013 {
0014     int i;
0015     flexcop_ibi_value r;
0016 
0017     r100->tw_sm_c_100.working_start = 1;
0018     deb_i2c("r100 before: %08x\n",r100->raw);
0019 
0020     fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero);
0021     fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */
0022 
0023     for (i = 0; i < FC_MAX_I2C_RETRIES; i++) {
0024         r = fc->read_ibi_reg(fc, tw_sm_c_100);
0025 
0026         if (!r.tw_sm_c_100.no_base_addr_ack_error) {
0027             if (r.tw_sm_c_100.st_done) {
0028                 *r100 = r;
0029                 deb_i2c("i2c success\n");
0030                 return 0;
0031             }
0032         } else {
0033             deb_i2c("suffering from an i2c ack_error\n");
0034             return -EREMOTEIO;
0035         }
0036     }
0037     deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",
0038         i);
0039     return -EREMOTEIO;
0040 }
0041 
0042 static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c,
0043         flexcop_ibi_value r100, u8 *buf)
0044 {
0045     flexcop_ibi_value r104;
0046     int len = r100.tw_sm_c_100.total_bytes,
0047         /* remember total_bytes is buflen-1 */
0048         ret;
0049 
0050     /* work-around to have CableStar2 and SkyStar2 rev 2.7 work
0051      * correctly:
0052      *
0053      * the ITD1000 is behind an i2c-gate which closes automatically
0054      * after an i2c-transaction the STV0297 needs 2 consecutive reads
0055      * one with no_base_addr = 0 and one with 1
0056      *
0057      * those two work-arounds are conflictin: we check for the card
0058      * type, it is set when probing the ITD1000 */
0059     if (i2c->fc->dev_type == FC_SKY_REV27)
0060         r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
0061 
0062     ret = flexcop_i2c_operation(i2c->fc, &r100);
0063     if (ret != 0) {
0064         deb_i2c("Retrying operation\n");
0065         r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
0066         ret = flexcop_i2c_operation(i2c->fc, &r100);
0067     }
0068     if (ret != 0) {
0069         deb_i2c("read failed. %d\n", ret);
0070         return ret;
0071     }
0072 
0073     buf[0] = r100.tw_sm_c_100.data1_reg;
0074 
0075     if (len > 0) {
0076         r104 = i2c->fc->read_ibi_reg(i2c->fc, tw_sm_c_104);
0077         deb_i2c("read: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
0078 
0079         /* there is at least one more byte, otherwise we wouldn't be here */
0080         buf[1] = r104.tw_sm_c_104.data2_reg;
0081         if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg;
0082         if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg;
0083     }
0084     return 0;
0085 }
0086 
0087 static int flexcop_i2c_write4(struct flexcop_device *fc,
0088         flexcop_ibi_value r100, u8 *buf)
0089 {
0090     flexcop_ibi_value r104;
0091     int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */
0092     r104.raw = 0;
0093 
0094     /* there is at least one byte, otherwise we wouldn't be here */
0095     r100.tw_sm_c_100.data1_reg = buf[0];
0096     r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
0097     r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
0098     r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
0099 
0100     deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
0101 
0102     /* write the additional i2c data before doing the actual i2c operation */
0103     fc->write_ibi_reg(fc, tw_sm_c_104, r104);
0104     return flexcop_i2c_operation(fc, &r100);
0105 }
0106 
0107 int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c,
0108             flexcop_access_op_t op, u8 chipaddr,
0109             u8 start_addr, u8 *buf, u16 size)
0110 {
0111     int ret;
0112     int len = size;
0113     u8 *p;
0114     u8 addr = start_addr;
0115 
0116     u16 bytes_to_transfer;
0117     flexcop_ibi_value r100;
0118 
0119     deb_i2c("port %d %s(%02x): register %02x, size: %d\n",
0120         i2c->port,
0121         op == FC_READ ? "rd" : "wr",
0122         chipaddr, start_addr, size);
0123     r100.raw = 0;
0124     r100.tw_sm_c_100.chipaddr = chipaddr;
0125     r100.tw_sm_c_100.twoWS_rw = op;
0126     r100.tw_sm_c_100.twoWS_port_reg = i2c->port;
0127 
0128     /* in that case addr is the only value ->
0129      * we write it twice as baseaddr and val0
0130      * BBTI is doing it like that for ISL6421 at least */
0131     if (i2c->no_base_addr && len == 0 && op == FC_WRITE) {
0132         buf = &start_addr;
0133         len = 1;
0134     }
0135 
0136     p = buf;
0137 
0138     while (len != 0) {
0139         bytes_to_transfer = len > 4 ? 4 : len;
0140 
0141         r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
0142         r100.tw_sm_c_100.baseaddr = addr;
0143 
0144         if (op == FC_READ)
0145             ret = flexcop_i2c_read4(i2c, r100, p);
0146         else
0147             ret = flexcop_i2c_write4(i2c->fc, r100, p);
0148 
0149         if (ret < 0)
0150             return ret;
0151 
0152         p  += bytes_to_transfer;
0153         addr += bytes_to_transfer;
0154         len  -= bytes_to_transfer;
0155     }
0156     deb_i2c_dump("port %d %s(%02x): register %02x: %*ph\n",
0157         i2c->port,
0158         op == FC_READ ? "rd" : "wr",
0159         chipaddr, start_addr, size, buf);
0160 
0161     return 0;
0162 }
0163 /* exported for PCI i2c */
0164 EXPORT_SYMBOL(flexcop_i2c_request);
0165 
0166 /* master xfer callback for demodulator */
0167 static int flexcop_master_xfer(struct i2c_adapter *i2c_adap,
0168         struct i2c_msg msgs[], int num)
0169 {
0170     struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
0171     int i, ret = 0;
0172 
0173     /* Some drivers use 1 byte or 0 byte reads as probes, which this
0174      * driver doesn't support.  These probes will always fail, so this
0175      * hack makes them always succeed.  If one knew how, it would of
0176      * course be better to actually do the read.  */
0177     if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1)
0178         return 1;
0179 
0180     if (mutex_lock_interruptible(&i2c->fc->i2c_mutex))
0181         return -ERESTARTSYS;
0182 
0183     for (i = 0; i < num; i++) {
0184         /* reading */
0185         if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) {
0186             ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr,
0187                     msgs[i].buf[0], msgs[i+1].buf,
0188                     msgs[i+1].len);
0189             i++; /* skip the following message */
0190         } else /* writing */
0191             ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr,
0192                     msgs[i].buf[0], &msgs[i].buf[1],
0193                     msgs[i].len - 1);
0194         if (ret < 0) {
0195             deb_i2c("i2c master_xfer failed");
0196             break;
0197         }
0198     }
0199 
0200     mutex_unlock(&i2c->fc->i2c_mutex);
0201 
0202     if (ret == 0)
0203         ret = num;
0204     return ret;
0205 }
0206 
0207 static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
0208 {
0209     return I2C_FUNC_I2C;
0210 }
0211 
0212 static struct i2c_algorithm flexcop_algo = {
0213     .master_xfer    = flexcop_master_xfer,
0214     .functionality  = flexcop_i2c_func,
0215 };
0216 
0217 int flexcop_i2c_init(struct flexcop_device *fc)
0218 {
0219     int ret;
0220     mutex_init(&fc->i2c_mutex);
0221 
0222     fc->fc_i2c_adap[0].fc = fc;
0223     fc->fc_i2c_adap[1].fc = fc;
0224     fc->fc_i2c_adap[2].fc = fc;
0225     fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD;
0226     fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM;
0227     fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER;
0228 
0229     strscpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod",
0230         sizeof(fc->fc_i2c_adap[0].i2c_adap.name));
0231     strscpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom",
0232         sizeof(fc->fc_i2c_adap[1].i2c_adap.name));
0233     strscpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner",
0234         sizeof(fc->fc_i2c_adap[2].i2c_adap.name));
0235 
0236     i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]);
0237     i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]);
0238     i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]);
0239 
0240     fc->fc_i2c_adap[0].i2c_adap.algo =
0241         fc->fc_i2c_adap[1].i2c_adap.algo =
0242         fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo;
0243     fc->fc_i2c_adap[0].i2c_adap.algo_data =
0244         fc->fc_i2c_adap[1].i2c_adap.algo_data =
0245         fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL;
0246     fc->fc_i2c_adap[0].i2c_adap.dev.parent =
0247         fc->fc_i2c_adap[1].i2c_adap.dev.parent =
0248         fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev;
0249 
0250     ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap);
0251     if (ret < 0)
0252         return ret;
0253 
0254     ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap);
0255     if (ret < 0)
0256         goto adap_1_failed;
0257 
0258     ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap);
0259     if (ret < 0)
0260         goto adap_2_failed;
0261 
0262     fc->init_state |= FC_STATE_I2C_INIT;
0263     return 0;
0264 
0265 adap_2_failed:
0266     i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
0267 adap_1_failed:
0268     i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
0269     return ret;
0270 }
0271 
0272 void flexcop_i2c_exit(struct flexcop_device *fc)
0273 {
0274     if (fc->init_state & FC_STATE_I2C_INIT) {
0275         i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap);
0276         i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
0277         i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
0278     }
0279     fc->init_state &= ~FC_STATE_I2C_INIT;
0280 }