1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
4 * flexcop-i2c.c - flexcop internal 2Wire bus (I2C) and dvb i2c initialization
5 * see flexcop.c for copyright information
6 */
7 #include "flexcop.h"
8
9 #define FC_MAX_I2C_RETRIES 100000
10
flexcop_i2c_operation(struct flexcop_device * fc,flexcop_ibi_value * r100)11 static int flexcop_i2c_operation(struct flexcop_device *fc,
12 flexcop_ibi_value *r100)
13 {
14 int i;
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) {
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");
34 return -EREMOTEIO;
35 }
36 }
37 deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",
38 i);
39 return -EREMOTEIO;
40 }
41
flexcop_i2c_read4(struct flexcop_i2c_adapter * i2c,flexcop_ibi_value r100,u8 * buf)42 static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c,
43 flexcop_ibi_value r100, u8 *buf)
44 {
45 flexcop_ibi_value r104;
46 int len = r100.tw_sm_c_100.total_bytes,
47 /* remember total_bytes is buflen-1 */
48 ret;
49
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
62 ret = flexcop_i2c_operation(i2c->fc, &r100);
63 if (ret != 0) {
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) {
69 deb_i2c("read failed. %d\n", ret);
70 return ret;
71 }
72
73 buf[0] = r100.tw_sm_c_100.data1_reg;
74
75 if (len > 0) {
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);
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 }
84 return 0;
85 }
86
flexcop_i2c_write4(struct flexcop_device * fc,flexcop_ibi_value r100,u8 * buf)87 static int flexcop_i2c_write4(struct flexcop_device *fc,
88 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 r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
97 r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
98 r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
99
100 deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
101
102 /* write the additional i2c data before doing the actual i2c operation */
103 fc->write_ibi_reg(fc, tw_sm_c_104, r104);
104 return flexcop_i2c_operation(fc, &r100);
105 }
106
flexcop_i2c_request(struct flexcop_i2c_adapter * i2c,flexcop_access_op_t op,u8 chipaddr,u8 start_addr,u8 * buf,u16 size)107 int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c,
108 flexcop_access_op_t op, u8 chipaddr,
109 u8 start_addr, u8 *buf, u16 size)
110 {
111 int ret;
112 int len = size;
113 u8 *p;
114 u8 addr = start_addr;
115
116 u16 bytes_to_transfer;
117 flexcop_ibi_value r100;
118
119 deb_i2c("port %d %s(%02x): register %02x, size: %d\n",
120 i2c->port,
121 op == FC_READ ? "rd" : "wr",
122 chipaddr, start_addr, size);
123 r100.raw = 0;
124 r100.tw_sm_c_100.chipaddr = chipaddr;
125 r100.tw_sm_c_100.twoWS_rw = op;
126 r100.tw_sm_c_100.twoWS_port_reg = i2c->port;
127
128 /* in that case addr is the only value ->
129 * we write it twice as baseaddr and val0
130 * BBTI is doing it like that for ISL6421 at least */
131 if (i2c->no_base_addr && len == 0 && op == FC_WRITE) {
132 buf = &start_addr;
133 len = 1;
134 }
135
136 p = buf;
137
138 while (len != 0) {
139 bytes_to_transfer = len > 4 ? 4 : len;
140
141 r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
142 r100.tw_sm_c_100.baseaddr = addr;
143
144 if (op == FC_READ)
145 ret = flexcop_i2c_read4(i2c, r100, p);
146 else
147 ret = flexcop_i2c_write4(i2c->fc, r100, p);
148
149 if (ret < 0)
150 return ret;
151
152 p += bytes_to_transfer;
153 addr += bytes_to_transfer;
154 len -= bytes_to_transfer;
155 }
156 deb_i2c_dump("port %d %s(%02x): register %02x: %*ph\n",
157 i2c->port,
158 op == FC_READ ? "rd" : "wr",
159 chipaddr, start_addr, size, buf);
160
161 return 0;
162 }
163 /* exported for PCI i2c */
164 EXPORT_SYMBOL(flexcop_i2c_request);
165
166 /* master xfer callback for demodulator */
flexcop_master_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg msgs[],int num)167 static int flexcop_master_xfer(struct i2c_adapter *i2c_adap,
168 struct i2c_msg msgs[], int num)
169 {
170 struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
171 int i, ret = 0;
172
173 /* Some drivers use 1 byte or 0 byte reads as probes, which this
174 * driver doesn't support. These probes will always fail, so this
175 * hack makes them always succeed. If one knew how, it would of
176 * course be better to actually do the read. */
177 if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1)
178 return 1;
179
180 if (mutex_lock_interruptible(&i2c->fc->i2c_mutex))
181 return -ERESTARTSYS;
182
183 for (i = 0; i < num; i++) {
184 /* reading */
185 if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) {
186 ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr,
187 msgs[i].buf[0], msgs[i+1].buf,
188 msgs[i+1].len);
189 i++; /* skip the following message */
190 } else /* writing */
191 ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr,
192 msgs[i].buf[0], &msgs[i].buf[1],
193 msgs[i].len - 1);
194 if (ret < 0) {
195 deb_i2c("i2c master_xfer failed");
196 break;
197 }
198 }
199
200 mutex_unlock(&i2c->fc->i2c_mutex);
201
202 if (ret == 0)
203 ret = num;
204 return ret;
205 }
206
flexcop_i2c_func(struct i2c_adapter * adapter)207 static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
208 {
209 return I2C_FUNC_I2C;
210 }
211
212 static struct i2c_algorithm flexcop_algo = {
213 .master_xfer = flexcop_master_xfer,
214 .functionality = flexcop_i2c_func,
215 };
216
flexcop_i2c_init(struct flexcop_device * fc)217 int flexcop_i2c_init(struct flexcop_device *fc)
218 {
219 int ret;
220 mutex_init(&fc->i2c_mutex);
221
222 fc->fc_i2c_adap[0].fc = fc;
223 fc->fc_i2c_adap[1].fc = fc;
224 fc->fc_i2c_adap[2].fc = fc;
225 fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD;
226 fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM;
227 fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER;
228
229 strscpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod",
230 sizeof(fc->fc_i2c_adap[0].i2c_adap.name));
231 strscpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom",
232 sizeof(fc->fc_i2c_adap[1].i2c_adap.name));
233 strscpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner",
234 sizeof(fc->fc_i2c_adap[2].i2c_adap.name));
235
236 i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]);
237 i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]);
238 i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]);
239
240 fc->fc_i2c_adap[0].i2c_adap.algo =
241 fc->fc_i2c_adap[1].i2c_adap.algo =
242 fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo;
243 fc->fc_i2c_adap[0].i2c_adap.algo_data =
244 fc->fc_i2c_adap[1].i2c_adap.algo_data =
245 fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL;
246 fc->fc_i2c_adap[0].i2c_adap.dev.parent =
247 fc->fc_i2c_adap[1].i2c_adap.dev.parent =
248 fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev;
249
250 ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap);
251 if (ret < 0)
252 return ret;
253
254 ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap);
255 if (ret < 0)
256 goto adap_1_failed;
257
258 ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap);
259 if (ret < 0)
260 goto adap_2_failed;
261
262 fc->init_state |= FC_STATE_I2C_INIT;
263 return 0;
264
265 adap_2_failed:
266 i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
267 adap_1_failed:
268 i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
269 return ret;
270 }
271
flexcop_i2c_exit(struct flexcop_device * fc)272 void flexcop_i2c_exit(struct flexcop_device *fc)
273 {
274 if (fc->init_state & FC_STATE_I2C_INIT) {
275 i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap);
276 i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
277 i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
278 }
279 fc->init_state &= ~FC_STATE_I2C_INIT;
280 }
281