blob: 43a112ec6d44f146b260c64cf12446d036e1283a [file] [log] [blame]
Johannes Stezenbach2add87a2005-05-16 21:54:10 -07001/*
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 Boettcher6394cf52008-03-29 20:49:57 -030012/* #define DUMP_I2C_MESSAGES */
13
Johannes Stezenbachbdc78002005-05-16 21:54:18 -070014static int flexcop_i2c_operation(struct flexcop_device *fc, flexcop_ibi_value *r100)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070015{
Johannes Stezenbachbdc78002005-05-16 21:54:18 -070016 int i;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070017 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 Stezenbachbdc78002005-05-16 21:54:18 -070036 return -EREMOTEIO;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070037 }
38 }
39 deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",i);
40 return -EREMOTEIO;
41}
42
Patrick Boettcher6394cf52008-03-29 20:49:57 -030043static int flexcop_i2c_read4(struct flexcop_i2c_adapter *i2c,
44 flexcop_ibi_value r100, u8 *buf)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070045{
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 Boettcher6394cf52008-03-29 20:49:57 -030050 r100.tw_sm_c_100.no_base_addr_ack_error = i2c->no_base_addr;
51 ret = flexcop_i2c_operation(i2c->fc, &r100);
52 if (ret != 0) {
53 deb_i2c("read failed. %d\n", ret);
54 return ret;
Johannes Stezenbachbdc78002005-05-16 21:54:18 -070055 }
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070056
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070057 buf[0] = r100.tw_sm_c_100.data1_reg;
58
Johannes Stezenbachbdc78002005-05-16 21:54:18 -070059 if (len > 0) {
Patrick Boettcher6394cf52008-03-29 20:49:57 -030060 r104 = i2c->fc->read_ibi_reg(i2c->fc, tw_sm_c_104);
61 deb_i2c("read: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
Johannes Stezenbachbdc78002005-05-16 21:54:18 -070062
63 /* there is at least one more byte, otherwise we wouldn't be here */
64 buf[1] = r104.tw_sm_c_104.data2_reg;
65 if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg;
66 if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg;
67 }
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070068
69 return 0;
70}
71
72static int flexcop_i2c_write4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
73{
74 flexcop_ibi_value r104;
75 int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */
76 r104.raw = 0;
77
78 /* there is at least one byte, otherwise we wouldn't be here */
79 r100.tw_sm_c_100.data1_reg = buf[0];
80
81 r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
82 r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
83 r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
84
Patrick Boettcher6394cf52008-03-29 20:49:57 -030085 deb_i2c("write: r100: %08x, r104: %08x\n", r100.raw, r104.raw);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070086
87 /* write the additional i2c data before doing the actual i2c operation */
Patrick Boettcher6394cf52008-03-29 20:49:57 -030088 fc->write_ibi_reg(fc, tw_sm_c_104, r104);
89 return flexcop_i2c_operation(fc, &r100);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070090}
91
Patrick Boettcher6394cf52008-03-29 20:49:57 -030092int flexcop_i2c_request(struct flexcop_i2c_adapter *i2c,
93 flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070094{
95 int ret;
Patrick Boettcher6394cf52008-03-29 20:49:57 -030096
97#ifdef DUMP_I2C_MESSAGES
98 int i;
99#endif
100
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700101 u16 bytes_to_transfer;
102 flexcop_ibi_value r100;
103
104 deb_i2c("op = %d\n",op);
105 r100.raw = 0;
106 r100.tw_sm_c_100.chipaddr = chipaddr;
107 r100.tw_sm_c_100.twoWS_rw = op;
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300108 r100.tw_sm_c_100.twoWS_port_reg = i2c->port;
109
110#ifdef DUMP_I2C_MESSAGES
111 printk(KERN_DEBUG "%d ", i2c->port);
112 if (op == FC_READ)
113 printk("rd(");
114 else
115 printk("wr(");
116
117 printk("%02x): %02x ", chipaddr, addr);
118#endif
119
120 /* in that case addr is the only value ->
121 * we write it twice as baseaddr and val0
122 * BBTI is doing it like that for ISL6421 at least */
123 if (i2c->no_base_addr && len == 0 && op == FC_WRITE) {
124 buf = &addr;
125 len = 1;
126 }
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700127
128 while (len != 0) {
129 bytes_to_transfer = len > 4 ? 4 : len;
130
131 r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
132 r100.tw_sm_c_100.baseaddr = addr;
133
134 if (op == FC_READ)
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300135 ret = flexcop_i2c_read4(i2c, r100, buf);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700136 else
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300137 ret = flexcop_i2c_write4(i2c->fc, r100, buf);
138
139#ifdef DUMP_I2C_MESSAGES
140 for (i = 0; i < bytes_to_transfer; i++)
141 printk("%02x ", buf[i]);
142#endif
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700143
144 if (ret < 0)
145 return ret;
146
147 buf += bytes_to_transfer;
148 addr += bytes_to_transfer;
149 len -= bytes_to_transfer;
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300150 }
151
152#ifdef DUMP_I2C_MESSAGES
153 printk("\n");
154#endif
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700155
156 return 0;
157}
158/* exported for PCI i2c */
159EXPORT_SYMBOL(flexcop_i2c_request);
160
Johannes Stezenbachbdc78002005-05-16 21:54:18 -0700161/* master xfer callback for demodulator */
162static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
163{
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300164 struct flexcop_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
Johannes Stezenbachbdc78002005-05-16 21:54:18 -0700165 int i, ret = 0;
166
Trent Piepho6175e482007-08-19 05:05:54 -0300167 /* Some drivers use 1 byte or 0 byte reads as probes, which this
168 * driver doesn't support. These probes will always fail, so this
169 * hack makes them always succeed. If one knew how, it would of
170 * course be better to actually do the read. */
171 if (num == 1 && msgs[0].flags == I2C_M_RD && msgs[0].len <= 1)
172 return 1;
173
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300174 if (mutex_lock_interruptible(&i2c->fc->i2c_mutex))
Johannes Stezenbachbdc78002005-05-16 21:54:18 -0700175 return -ERESTARTSYS;
176
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300177 for (i = 0; i < num; i++) {
178 /* reading */
179 if (i+1 < num && (msgs[i+1].flags == I2C_M_RD)) {
180 ret = i2c->fc->i2c_request(i2c, FC_READ, msgs[i].addr,
181 msgs[i].buf[0], msgs[i+1].buf, msgs[i+1].len);
182 i++; /* skip the following message */
183 } else /* writing */
184 ret = i2c->fc->i2c_request(i2c, FC_WRITE, msgs[i].addr,
185 msgs[i].buf[0], &msgs[i].buf[1],
186 msgs[i].len - 1);
187 if (ret < 0) {
188 err("i2c master_xfer failed");
Johannes Stezenbachbdc78002005-05-16 21:54:18 -0700189 break;
190 }
Johannes Stezenbachbdc78002005-05-16 21:54:18 -0700191 }
192
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300193 mutex_unlock(&i2c->fc->i2c_mutex);
194
195 if (ret == 0)
Johannes Stezenbachbdc78002005-05-16 21:54:18 -0700196 ret = num;
Johannes Stezenbachbdc78002005-05-16 21:54:18 -0700197 return ret;
198}
199
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700200static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
201{
202 return I2C_FUNC_I2C;
203}
204
205static struct i2c_algorithm flexcop_algo = {
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700206 .master_xfer = flexcop_master_xfer,
207 .functionality = flexcop_i2c_func,
208};
209
210int flexcop_i2c_init(struct flexcop_device *fc)
211{
212 int ret;
213
Ingo Molnar3593cab2006-02-07 06:49:14 -0200214 mutex_init(&fc->i2c_mutex);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700215
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300216 fc->fc_i2c_adap[0].fc = fc;
217 fc->fc_i2c_adap[1].fc = fc;
218 fc->fc_i2c_adap[2].fc = fc;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700219
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300220 fc->fc_i2c_adap[0].port = FC_I2C_PORT_DEMOD;
221 fc->fc_i2c_adap[1].port = FC_I2C_PORT_EEPROM;
222 fc->fc_i2c_adap[2].port = FC_I2C_PORT_TUNER;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700223
Jean Delvare1d434012008-09-03 17:12:23 -0300224 strlcpy(fc->fc_i2c_adap[0].i2c_adap.name, "B2C2 FlexCop I2C to demod",
225 sizeof(fc->fc_i2c_adap[0].i2c_adap.name));
226 strlcpy(fc->fc_i2c_adap[1].i2c_adap.name, "B2C2 FlexCop I2C to eeprom",
227 sizeof(fc->fc_i2c_adap[1].i2c_adap.name));
228 strlcpy(fc->fc_i2c_adap[2].i2c_adap.name, "B2C2 FlexCop I2C to tuner",
229 sizeof(fc->fc_i2c_adap[2].i2c_adap.name));
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700230
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300231 i2c_set_adapdata(&fc->fc_i2c_adap[0].i2c_adap, &fc->fc_i2c_adap[0]);
232 i2c_set_adapdata(&fc->fc_i2c_adap[1].i2c_adap, &fc->fc_i2c_adap[1]);
233 i2c_set_adapdata(&fc->fc_i2c_adap[2].i2c_adap, &fc->fc_i2c_adap[2]);
234
235 fc->fc_i2c_adap[0].i2c_adap.class =
236 fc->fc_i2c_adap[1].i2c_adap.class =
237 fc->fc_i2c_adap[2].i2c_adap.class = I2C_CLASS_TV_DIGITAL;
238 fc->fc_i2c_adap[0].i2c_adap.algo =
239 fc->fc_i2c_adap[1].i2c_adap.algo =
240 fc->fc_i2c_adap[2].i2c_adap.algo = &flexcop_algo;
241 fc->fc_i2c_adap[0].i2c_adap.algo_data =
242 fc->fc_i2c_adap[1].i2c_adap.algo_data =
243 fc->fc_i2c_adap[2].i2c_adap.algo_data = NULL;
244 fc->fc_i2c_adap[0].i2c_adap.dev.parent =
245 fc->fc_i2c_adap[1].i2c_adap.dev.parent =
246 fc->fc_i2c_adap[2].i2c_adap.dev.parent = fc->dev;
247
248 ret = i2c_add_adapter(&fc->fc_i2c_adap[0].i2c_adap);
249 if (ret < 0)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700250 return ret;
251
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300252 ret = i2c_add_adapter(&fc->fc_i2c_adap[1].i2c_adap);
253 if (ret < 0)
254 goto adap_1_failed;
255
256 ret = i2c_add_adapter(&fc->fc_i2c_adap[2].i2c_adap);
257 if (ret < 0)
258 goto adap_2_failed;
259
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700260 fc->init_state |= FC_STATE_I2C_INIT;
261 return 0;
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300262
263adap_2_failed:
264 i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
265adap_1_failed:
266 i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
267
268 return ret;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700269}
270
271void flexcop_i2c_exit(struct flexcop_device *fc)
272{
Patrick Boettcher6394cf52008-03-29 20:49:57 -0300273 if (fc->init_state & FC_STATE_I2C_INIT) {
274 i2c_del_adapter(&fc->fc_i2c_adap[2].i2c_adap);
275 i2c_del_adapter(&fc->fc_i2c_adap[1].i2c_adap);
276 i2c_del_adapter(&fc->fc_i2c_adap[0].i2c_adap);
277 }
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700278
279 fc->init_state &= ~FC_STATE_I2C_INIT;
280}