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