Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III |
| 3 | * |
| 4 | * flexcop-i2c.c - flexcop internal 2Wire bus (I2C) and dvb i2c initialization |
| 5 | * |
| 6 | * see flexcop.c for copyright information. |
| 7 | */ |
| 8 | #include "flexcop.h" |
| 9 | |
| 10 | #define FC_MAX_I2C_RETRIES 100000 |
| 11 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 12 | /* #define DUMP_I2C_MESSAGES */ |
| 13 | |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 14 | static int flexcop_i2c_operation(struct flexcop_device *fc, flexcop_ibi_value *r100) |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 15 | { |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 16 | int i; |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 17 | flexcop_ibi_value r; |
| 18 | |
| 19 | r100->tw_sm_c_100.working_start = 1; |
| 20 | deb_i2c("r100 before: %08x\n",r100->raw); |
| 21 | |
| 22 | fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero); |
| 23 | fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */ |
| 24 | |
| 25 | for (i = 0; i < FC_MAX_I2C_RETRIES; i++) { |
| 26 | r = fc->read_ibi_reg(fc, tw_sm_c_100); |
| 27 | |
| 28 | if (!r.tw_sm_c_100.no_base_addr_ack_error) { |
| 29 | if (r.tw_sm_c_100.st_done) { /* && !r.tw_sm_c_100.working_start */ |
| 30 | *r100 = r; |
| 31 | deb_i2c("i2c success\n"); |
| 32 | return 0; |
| 33 | } |
| 34 | } else { |
| 35 | deb_i2c("suffering from an i2c ack_error\n"); |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 36 | return -EREMOTEIO; |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",i); |
| 40 | return -EREMOTEIO; |
| 41 | } |
| 42 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 43 | static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c, |
| 44 | flexcop_ibi_value r100, u8 *buf) |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 45 | { |
| 46 | flexcop_ibi_value r104; |
| 47 | int len = r100.tw_sm_c_100.total_bytes, /* remember total_bytes is buflen-1 */ |
| 48 | ret; |
| 49 | |
Patrick Boettcher | 1662070 | 2009-02-28 10:19:30 -0300 | [diff] [blame^] | 50 | /* work-around to have CableStar2 and SkyStar2 rev 2.7 work |
| 51 | * correctly: |
| 52 | * |
| 53 | * the ITD1000 is behind an i2c-gate which closes automatically |
| 54 | * after an i2c-transaction the STV0297 needs 2 consecutive reads |
| 55 | * one with no_base_addr = 0 and one with 1 |
| 56 | * |
| 57 | * those two work-arounds are conflictin: we check for the card |
| 58 | * type, it is set when probing the ITD1000 */ |
| 59 | if (i2c->fc->dev_type == FC_SKY_REV27) |
| 60 | r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr; |
| 61 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 62 | ret = flexcop_i2c_operation(i2c->fc, &r100); |
| 63 | if (ret != 0) { |
Antti Seppälä | 11c6c7f | 2008-12-01 06:59:37 -0300 | [diff] [blame] | 64 | deb_i2c("Retrying operation\n"); |
| 65 | r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr; |
| 66 | ret = flexcop_i2c_operation(i2c->fc, &r100); |
| 67 | } |
| 68 | if (ret != 0) { |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 69 | deb_i2c("read failed. %d\n", ret); |
| 70 | return ret; |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 71 | } |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 72 | |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 73 | buf[0] = r100.tw_sm_c_100.data1_reg; |
| 74 | |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 75 | if (len > 0) { |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 76 | r104 = i2c->fc->read_ibi_reg(i2c->fc, tw_sm_c_104); |
| 77 | deb_i2c("read: r100: %08x, r104: %08x\n", r100.raw, r104.raw); |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 78 | |
| 79 | /* there is at least one more byte, otherwise we wouldn't be here */ |
| 80 | buf[1] = r104.tw_sm_c_104.data2_reg; |
| 81 | if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg; |
| 82 | if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg; |
| 83 | } |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int flexcop_i2c_write4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf) |
| 89 | { |
| 90 | flexcop_ibi_value r104; |
| 91 | int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */ |
| 92 | r104.raw = 0; |
| 93 | |
| 94 | /* there is at least one byte, otherwise we wouldn't be here */ |
| 95 | r100.tw_sm_c_100.data1_reg = buf[0]; |
| 96 | |
| 97 | r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0; |
| 98 | r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0; |
| 99 | r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0; |
| 100 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 101 | deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw); |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 102 | |
| 103 | /* write the additional i2c data before doing the actual i2c operation */ |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 104 | fc->write_ibi_reg(fc, tw_sm_c_104, r104); |
| 105 | return flexcop_i2c_operation(fc, &r100); |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 108 | int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c, |
| 109 | flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len) |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 110 | { |
| 111 | int ret; |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 112 | |
| 113 | #ifdef DUMP_I2C_MESSAGES |
| 114 | int i; |
| 115 | #endif |
| 116 | |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 117 | u16 bytes_to_transfer; |
| 118 | flexcop_ibi_value r100; |
| 119 | |
| 120 | deb_i2c("op = %d\n",op); |
| 121 | r100.raw = 0; |
| 122 | r100.tw_sm_c_100.chipaddr = chipaddr; |
| 123 | r100.tw_sm_c_100.twoWS_rw = op; |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 124 | r100.tw_sm_c_100.twoWS_port_reg = i2c->port; |
| 125 | |
| 126 | #ifdef DUMP_I2C_MESSAGES |
| 127 | printk(KERN_DEBUG "%d ", i2c->port); |
| 128 | if (op == FC_READ) |
| 129 | printk("rd("); |
| 130 | else |
| 131 | printk("wr("); |
| 132 | |
| 133 | printk("%02x): %02x ", chipaddr, addr); |
| 134 | #endif |
| 135 | |
| 136 | /* in that case addr is the only value -> |
| 137 | * we write it twice as baseaddr and val0 |
| 138 | * BBTI is doing it like that for ISL6421 at least */ |
| 139 | if (i2c->no_base_addr && len == 0 && op == FC_WRITE) { |
| 140 | buf = &addr; |
| 141 | len = 1; |
| 142 | } |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 143 | |
| 144 | while (len != 0) { |
| 145 | bytes_to_transfer = len > 4 ? 4 : len; |
| 146 | |
| 147 | r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1; |
| 148 | r100.tw_sm_c_100.baseaddr = addr; |
| 149 | |
| 150 | if (op == FC_READ) |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 151 | ret = flexcop_i2c_read4(i2c, r100, buf); |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 152 | else |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 153 | ret = flexcop_i2c_write4(i2c->fc, r100, buf); |
| 154 | |
| 155 | #ifdef DUMP_I2C_MESSAGES |
| 156 | for (i = 0; i < bytes_to_transfer; i++) |
| 157 | printk("%02x ", buf[i]); |
| 158 | #endif |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 159 | |
| 160 | if (ret < 0) |
| 161 | return ret; |
| 162 | |
| 163 | buf += bytes_to_transfer; |
| 164 | addr += bytes_to_transfer; |
| 165 | len -= bytes_to_transfer; |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | #ifdef DUMP_I2C_MESSAGES |
| 169 | printk("\n"); |
| 170 | #endif |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | /* exported for PCI i2c */ |
| 175 | EXPORT_SYMBOL(flexcop_i2c_request); |
| 176 | |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 177 | /* master xfer callback for demodulator */ |
| 178 | static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) |
| 179 | { |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 180 | struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap); |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 181 | int i, ret = 0; |
| 182 | |
Trent Piepho | 6175e48 | 2007-08-19 05:05:54 -0300 | [diff] [blame] | 183 | /* Some drivers use 1 byte or 0 byte reads as probes, which this |
| 184 | * driver doesn't support. These probes will always fail, so this |
| 185 | * hack makes them always succeed. If one knew how, it would of |
| 186 | * course be better to actually do the read. */ |
| 187 | if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1) |
| 188 | return 1; |
| 189 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 190 | if (mutex_lock_interruptible(&i2c->fc->i2c_mutex)) |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 191 | return -ERESTARTSYS; |
| 192 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 193 | for (i = 0; i < num; i++) { |
| 194 | /* reading */ |
| 195 | if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) { |
| 196 | ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr, |
| 197 | msgs[i].buf[0], msgs[i+1].buf, msgs[i+1].len); |
| 198 | i++; /* skip the following message */ |
| 199 | } else /* writing */ |
| 200 | ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr, |
| 201 | msgs[i].buf[0], &msgs[i].buf[1], |
| 202 | msgs[i].len - 1); |
| 203 | if (ret < 0) { |
| 204 | err("i2c master_xfer failed"); |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 205 | break; |
| 206 | } |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 209 | mutex_unlock(&i2c->fc->i2c_mutex); |
| 210 | |
| 211 | if (ret == 0) |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 212 | ret = num; |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 216 | static u32 flexcop_i2c_func(struct i2c_adapter *adapter) |
| 217 | { |
| 218 | return I2C_FUNC_I2C; |
| 219 | } |
| 220 | |
| 221 | static struct i2c_algorithm flexcop_algo = { |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 222 | .master_xfer = flexcop_master_xfer, |
| 223 | .functionality = flexcop_i2c_func, |
| 224 | }; |
| 225 | |
| 226 | int flexcop_i2c_init(struct flexcop_device *fc) |
| 227 | { |
| 228 | int ret; |
| 229 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 230 | mutex_init(&fc->i2c_mutex); |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 231 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 232 | fc->fc_i2c_adap[0].fc = fc; |
| 233 | fc->fc_i2c_adap[1].fc = fc; |
| 234 | fc->fc_i2c_adap[2].fc = fc; |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 235 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 236 | fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD; |
| 237 | fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM; |
| 238 | fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER; |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 239 | |
Jean Delvare | 1d43401 | 2008-09-03 17:12:23 -0300 | [diff] [blame] | 240 | strlcpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod", |
| 241 | sizeof(fc->fc_i2c_adap[0].i2c_adap.name)); |
| 242 | strlcpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom", |
| 243 | sizeof(fc->fc_i2c_adap[1].i2c_adap.name)); |
| 244 | strlcpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner", |
| 245 | sizeof(fc->fc_i2c_adap[2].i2c_adap.name)); |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 246 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 247 | i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]); |
| 248 | i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]); |
| 249 | i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]); |
| 250 | |
| 251 | fc->fc_i2c_adap[0].i2c_adap.class = |
| 252 | fc->fc_i2c_adap[1].i2c_adap.class = |
| 253 | fc->fc_i2c_adap[2].i2c_adap.class = I2C_CLASS_TV_DIGITAL; |
| 254 | fc->fc_i2c_adap[0].i2c_adap.algo = |
| 255 | fc->fc_i2c_adap[1].i2c_adap.algo = |
| 256 | fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo; |
| 257 | fc->fc_i2c_adap[0].i2c_adap.algo_data = |
| 258 | fc->fc_i2c_adap[1].i2c_adap.algo_data = |
| 259 | fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL; |
| 260 | fc->fc_i2c_adap[0].i2c_adap.dev.parent = |
| 261 | fc->fc_i2c_adap[1].i2c_adap.dev.parent = |
| 262 | fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev; |
| 263 | |
| 264 | ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap); |
| 265 | if (ret < 0) |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 266 | return ret; |
| 267 | |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 268 | ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap); |
| 269 | if (ret < 0) |
| 270 | goto adap_1_failed; |
| 271 | |
| 272 | ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap); |
| 273 | if (ret < 0) |
| 274 | goto adap_2_failed; |
| 275 | |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 276 | fc->init_state |= FC_STATE_I2C_INIT; |
| 277 | return 0; |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 278 | |
| 279 | adap_2_failed: |
| 280 | i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap); |
| 281 | adap_1_failed: |
| 282 | i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap); |
| 283 | |
| 284 | return ret; |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void flexcop_i2c_exit(struct flexcop_device *fc) |
| 288 | { |
Patrick Boettcher | 6394cf5 | 2008-03-29 20:49:57 -0300 | [diff] [blame] | 289 | if (fc->init_state & FC_STATE_I2C_INIT) { |
| 290 | i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap); |
| 291 | i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap); |
| 292 | i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap); |
| 293 | } |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 294 | |
| 295 | fc->init_state &= ~FC_STATE_I2C_INIT; |
| 296 | } |