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 | |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 12 | 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] | 13 | { |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 14 | int i; |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 15 | flexcop_ibi_value r; |
| 16 | |
| 17 | r100->tw_sm_c_100.working_start = 1; |
| 18 | deb_i2c("r100 before: %08x\n",r100->raw); |
| 19 | |
| 20 | fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero); |
| 21 | fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */ |
| 22 | |
| 23 | for (i = 0; i < FC_MAX_I2C_RETRIES; i++) { |
| 24 | r = fc->read_ibi_reg(fc, tw_sm_c_100); |
| 25 | |
| 26 | if (!r.tw_sm_c_100.no_base_addr_ack_error) { |
| 27 | if (r.tw_sm_c_100.st_done) { /* && !r.tw_sm_c_100.working_start */ |
| 28 | *r100 = r; |
| 29 | deb_i2c("i2c success\n"); |
| 30 | return 0; |
| 31 | } |
| 32 | } else { |
| 33 | deb_i2c("suffering from an i2c ack_error\n"); |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 34 | return -EREMOTEIO; |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",i); |
| 38 | return -EREMOTEIO; |
| 39 | } |
| 40 | |
| 41 | static int flexcop_i2c_read4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf) |
| 42 | { |
| 43 | flexcop_ibi_value r104; |
| 44 | int len = r100.tw_sm_c_100.total_bytes, /* remember total_bytes is buflen-1 */ |
| 45 | ret; |
| 46 | |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 47 | if ((ret = flexcop_i2c_operation(fc,&r100)) != 0) { |
| 48 | /* The Cablestar needs a different kind of i2c-transfer (does not |
| 49 | * support "Repeat Start"): |
| 50 | * wait for the ACK failure, |
| 51 | * and do a subsequent read with the Bit 30 enabled |
| 52 | */ |
| 53 | r100.tw_sm_c_100.no_base_addr_ack_error = 1; |
| 54 | if ((ret = flexcop_i2c_operation(fc,&r100)) != 0) { |
| 55 | deb_i2c("no_base_addr read failed. %d\n",ret); |
| 56 | return ret; |
| 57 | } |
| 58 | } |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 59 | |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 60 | buf[0] = r100.tw_sm_c_100.data1_reg; |
| 61 | |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 62 | if (len > 0) { |
| 63 | r104 = fc->read_ibi_reg(fc,tw_sm_c_104); |
| 64 | deb_i2c("read: r100: %08x, r104: %08x\n",r100.raw,r104.raw); |
| 65 | |
| 66 | /* there is at least one more byte, otherwise we wouldn't be here */ |
| 67 | buf[1] = r104.tw_sm_c_104.data2_reg; |
| 68 | if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg; |
| 69 | if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg; |
| 70 | } |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int flexcop_i2c_write4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf) |
| 76 | { |
| 77 | flexcop_ibi_value r104; |
| 78 | int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */ |
| 79 | r104.raw = 0; |
| 80 | |
| 81 | /* there is at least one byte, otherwise we wouldn't be here */ |
| 82 | r100.tw_sm_c_100.data1_reg = buf[0]; |
| 83 | |
| 84 | r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0; |
| 85 | r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0; |
| 86 | r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0; |
| 87 | |
| 88 | deb_i2c("write: r100: %08x, r104: %08x\n",r100.raw,r104.raw); |
| 89 | |
| 90 | /* write the additional i2c data before doing the actual i2c operation */ |
| 91 | fc->write_ibi_reg(fc,tw_sm_c_104,r104); |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 92 | return flexcop_i2c_operation(fc,&r100); |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int flexcop_i2c_request(struct flexcop_device *fc, flexcop_access_op_t op, |
| 96 | flexcop_i2c_port_t port, u8 chipaddr, u8 addr, u8 *buf, u16 len) |
| 97 | { |
| 98 | int ret; |
| 99 | u16 bytes_to_transfer; |
| 100 | flexcop_ibi_value r100; |
| 101 | |
| 102 | deb_i2c("op = %d\n",op); |
| 103 | r100.raw = 0; |
| 104 | r100.tw_sm_c_100.chipaddr = chipaddr; |
| 105 | r100.tw_sm_c_100.twoWS_rw = op; |
| 106 | r100.tw_sm_c_100.twoWS_port_reg = port; |
| 107 | |
| 108 | while (len != 0) { |
| 109 | bytes_to_transfer = len > 4 ? 4 : len; |
| 110 | |
| 111 | r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1; |
| 112 | r100.tw_sm_c_100.baseaddr = addr; |
| 113 | |
| 114 | if (op == FC_READ) |
| 115 | ret = flexcop_i2c_read4(fc, r100, buf); |
| 116 | else |
| 117 | ret = flexcop_i2c_write4(fc,r100, buf); |
| 118 | |
| 119 | if (ret < 0) |
| 120 | return ret; |
| 121 | |
| 122 | buf += bytes_to_transfer; |
| 123 | addr += bytes_to_transfer; |
| 124 | len -= bytes_to_transfer; |
| 125 | }; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | /* exported for PCI i2c */ |
| 130 | EXPORT_SYMBOL(flexcop_i2c_request); |
| 131 | |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 132 | /* master xfer callback for demodulator */ |
| 133 | static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) |
| 134 | { |
| 135 | struct flexcop_device *fc = i2c_get_adapdata(i2c_adap); |
| 136 | int i, ret = 0; |
| 137 | |
Trent Piepho | 6175e48 | 2007-08-19 05:05:54 -0300 | [diff] [blame] | 138 | /* Some drivers use 1 byte or 0 byte reads as probes, which this |
| 139 | * driver doesn't support. These probes will always fail, so this |
| 140 | * hack makes them always succeed. If one knew how, it would of |
| 141 | * course be better to actually do the read. */ |
| 142 | if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1) |
| 143 | return 1; |
| 144 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 145 | if (mutex_lock_interruptible(&fc->i2c_mutex)) |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 146 | return -ERESTARTSYS; |
| 147 | |
| 148 | /* reading */ |
| 149 | if (num == 2 && |
| 150 | msgs[0].flags == 0 && |
| 151 | msgs[1].flags == I2C_M_RD && |
| 152 | msgs[0].buf != NULL && |
| 153 | msgs[1].buf != NULL) { |
| 154 | |
| 155 | ret = fc->i2c_request(fc, FC_READ, FC_I2C_PORT_DEMOD, msgs[0].addr, msgs[0].buf[0], msgs[1].buf, msgs[1].len); |
| 156 | |
| 157 | } else for (i = 0; i < num; i++) { /* writing command */ |
| 158 | if (msgs[i].flags != 0 || msgs[i].buf == NULL || msgs[i].len < 2) { |
| 159 | ret = -EINVAL; |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | ret = fc->i2c_request(fc, FC_WRITE, FC_I2C_PORT_DEMOD, msgs[i].addr, msgs[i].buf[0], &msgs[i].buf[1], msgs[i].len - 1); |
| 164 | } |
| 165 | |
| 166 | if (ret < 0) |
| 167 | err("i2c master_xfer failed"); |
| 168 | else |
| 169 | ret = num; |
| 170 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 171 | mutex_unlock(&fc->i2c_mutex); |
Johannes Stezenbach | bdc7800 | 2005-05-16 21:54:18 -0700 | [diff] [blame] | 172 | |
| 173 | return ret; |
| 174 | } |
| 175 | |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 176 | static u32 flexcop_i2c_func(struct i2c_adapter *adapter) |
| 177 | { |
| 178 | return I2C_FUNC_I2C; |
| 179 | } |
| 180 | |
| 181 | static struct i2c_algorithm flexcop_algo = { |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 182 | .master_xfer = flexcop_master_xfer, |
| 183 | .functionality = flexcop_i2c_func, |
| 184 | }; |
| 185 | |
| 186 | int flexcop_i2c_init(struct flexcop_device *fc) |
| 187 | { |
| 188 | int ret; |
| 189 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 190 | mutex_init(&fc->i2c_mutex); |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 191 | |
| 192 | memset(&fc->i2c_adap, 0, sizeof(struct i2c_adapter)); |
David Brownell | 2096b95 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 193 | strncpy(fc->i2c_adap.name, "B2C2 FlexCop device", |
| 194 | sizeof(fc->i2c_adap.name)); |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 195 | |
| 196 | i2c_set_adapdata(&fc->i2c_adap,fc); |
| 197 | |
| 198 | fc->i2c_adap.class = I2C_CLASS_TV_DIGITAL; |
| 199 | fc->i2c_adap.algo = &flexcop_algo; |
| 200 | fc->i2c_adap.algo_data = NULL; |
Jean Delvare | 12a917f | 2007-02-13 22:09:03 +0100 | [diff] [blame] | 201 | fc->i2c_adap.dev.parent = fc->dev; |
Johannes Stezenbach | 2add87a | 2005-05-16 21:54:10 -0700 | [diff] [blame] | 202 | |
| 203 | if ((ret = i2c_add_adapter(&fc->i2c_adap)) < 0) |
| 204 | return ret; |
| 205 | |
| 206 | fc->init_state |= FC_STATE_I2C_INIT; |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | void flexcop_i2c_exit(struct flexcop_device *fc) |
| 211 | { |
| 212 | if (fc->init_state & FC_STATE_I2C_INIT) |
| 213 | i2c_del_adapter(&fc->i2c_adap); |
| 214 | |
| 215 | fc->init_state &= ~FC_STATE_I2C_INIT; |
| 216 | } |