1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2786baecfSMauro Carvalho Chehab /*
3786baecfSMauro Carvalho Chehab * mxl111sf-i2c.c - driver for the MaxLinear MXL111SF
4786baecfSMauro Carvalho Chehab *
508e10972SMichael Krufky * Copyright (C) 2010-2014 Michael Krufky <mkrufky@linuxtv.org>
6786baecfSMauro Carvalho Chehab */
7786baecfSMauro Carvalho Chehab
8786baecfSMauro Carvalho Chehab #include "mxl111sf-i2c.h"
9786baecfSMauro Carvalho Chehab #include "mxl111sf.h"
10786baecfSMauro Carvalho Chehab
11786baecfSMauro Carvalho Chehab /* SW-I2C ----------------------------------------------------------------- */
12786baecfSMauro Carvalho Chehab
13786baecfSMauro Carvalho Chehab #define SW_I2C_ADDR 0x1a
14786baecfSMauro Carvalho Chehab #define SW_I2C_EN 0x02
15786baecfSMauro Carvalho Chehab #define SW_SCL_OUT 0x04
16786baecfSMauro Carvalho Chehab #define SW_SDA_OUT 0x08
17786baecfSMauro Carvalho Chehab #define SW_SDA_IN 0x04
18786baecfSMauro Carvalho Chehab
19786baecfSMauro Carvalho Chehab #define SW_I2C_BUSY_ADDR 0x2f
20786baecfSMauro Carvalho Chehab #define SW_I2C_BUSY 0x02
21786baecfSMauro Carvalho Chehab
mxl111sf_i2c_bitbang_sendbyte(struct mxl111sf_state * state,u8 byte)22786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_bitbang_sendbyte(struct mxl111sf_state *state,
23786baecfSMauro Carvalho Chehab u8 byte)
24786baecfSMauro Carvalho Chehab {
25786baecfSMauro Carvalho Chehab int i, ret;
26786baecfSMauro Carvalho Chehab u8 data = 0;
27786baecfSMauro Carvalho Chehab
28786baecfSMauro Carvalho Chehab mxl_i2c("(0x%02x)", byte);
29786baecfSMauro Carvalho Chehab
30786baecfSMauro Carvalho Chehab ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
31786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
32786baecfSMauro Carvalho Chehab goto fail;
33786baecfSMauro Carvalho Chehab
34786baecfSMauro Carvalho Chehab for (i = 0; i < 8; i++) {
35786baecfSMauro Carvalho Chehab
36786baecfSMauro Carvalho Chehab data = (byte & (0x80 >> i)) ? SW_SDA_OUT : 0;
37786baecfSMauro Carvalho Chehab
38786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
39786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | data);
40786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
41786baecfSMauro Carvalho Chehab goto fail;
42786baecfSMauro Carvalho Chehab
43786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
44786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | data | SW_SCL_OUT);
45786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
46786baecfSMauro Carvalho Chehab goto fail;
47786baecfSMauro Carvalho Chehab
48786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
49786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | data);
50786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
51786baecfSMauro Carvalho Chehab goto fail;
52786baecfSMauro Carvalho Chehab }
53786baecfSMauro Carvalho Chehab
54786baecfSMauro Carvalho Chehab /* last bit was 0 so we need to release SDA */
55786baecfSMauro Carvalho Chehab if (!(byte & 1)) {
56786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
57786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SDA_OUT);
58786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
59786baecfSMauro Carvalho Chehab goto fail;
60786baecfSMauro Carvalho Chehab }
61786baecfSMauro Carvalho Chehab
62786baecfSMauro Carvalho Chehab /* CLK high for ACK readback */
63786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
64786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
65786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
66786baecfSMauro Carvalho Chehab goto fail;
67786baecfSMauro Carvalho Chehab
68786baecfSMauro Carvalho Chehab ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
69786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
70786baecfSMauro Carvalho Chehab goto fail;
71786baecfSMauro Carvalho Chehab
72786baecfSMauro Carvalho Chehab /* drop the CLK after getting ACK, SDA will go high right away */
73786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
74786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SDA_OUT);
75786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
76786baecfSMauro Carvalho Chehab goto fail;
77786baecfSMauro Carvalho Chehab
78786baecfSMauro Carvalho Chehab if (data & SW_SDA_IN)
79786baecfSMauro Carvalho Chehab ret = -EIO;
80786baecfSMauro Carvalho Chehab fail:
81786baecfSMauro Carvalho Chehab return ret;
82786baecfSMauro Carvalho Chehab }
83786baecfSMauro Carvalho Chehab
mxl111sf_i2c_bitbang_recvbyte(struct mxl111sf_state * state,u8 * pbyte)84786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_bitbang_recvbyte(struct mxl111sf_state *state,
85786baecfSMauro Carvalho Chehab u8 *pbyte)
86786baecfSMauro Carvalho Chehab {
87786baecfSMauro Carvalho Chehab int i, ret;
88786baecfSMauro Carvalho Chehab u8 byte = 0;
89786baecfSMauro Carvalho Chehab u8 data = 0;
90786baecfSMauro Carvalho Chehab
91786baecfSMauro Carvalho Chehab mxl_i2c("()");
92786baecfSMauro Carvalho Chehab
93786baecfSMauro Carvalho Chehab *pbyte = 0;
94786baecfSMauro Carvalho Chehab
95786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
96786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SDA_OUT);
97786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
98786baecfSMauro Carvalho Chehab goto fail;
99786baecfSMauro Carvalho Chehab
100786baecfSMauro Carvalho Chehab for (i = 0; i < 8; i++) {
101786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
102786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN |
103786baecfSMauro Carvalho Chehab SW_SCL_OUT | SW_SDA_OUT);
104786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
105786baecfSMauro Carvalho Chehab goto fail;
106786baecfSMauro Carvalho Chehab
107786baecfSMauro Carvalho Chehab ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
108786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
109786baecfSMauro Carvalho Chehab goto fail;
110786baecfSMauro Carvalho Chehab
111786baecfSMauro Carvalho Chehab if (data & SW_SDA_IN)
112786baecfSMauro Carvalho Chehab byte |= (0x80 >> i);
113786baecfSMauro Carvalho Chehab
114786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
115786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SDA_OUT);
116786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
117786baecfSMauro Carvalho Chehab goto fail;
118786baecfSMauro Carvalho Chehab }
119786baecfSMauro Carvalho Chehab *pbyte = byte;
120786baecfSMauro Carvalho Chehab fail:
121786baecfSMauro Carvalho Chehab return ret;
122786baecfSMauro Carvalho Chehab }
123786baecfSMauro Carvalho Chehab
mxl111sf_i2c_start(struct mxl111sf_state * state)124786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_start(struct mxl111sf_state *state)
125786baecfSMauro Carvalho Chehab {
126786baecfSMauro Carvalho Chehab int ret;
127786baecfSMauro Carvalho Chehab
128786baecfSMauro Carvalho Chehab mxl_i2c("()");
129786baecfSMauro Carvalho Chehab
130786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
131786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
132786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
133786baecfSMauro Carvalho Chehab goto fail;
134786baecfSMauro Carvalho Chehab
135786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
136786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SCL_OUT);
137786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
138786baecfSMauro Carvalho Chehab goto fail;
139786baecfSMauro Carvalho Chehab
140786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
141786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN); /* start */
142786baecfSMauro Carvalho Chehab mxl_fail(ret);
143786baecfSMauro Carvalho Chehab fail:
144786baecfSMauro Carvalho Chehab return ret;
145786baecfSMauro Carvalho Chehab }
146786baecfSMauro Carvalho Chehab
mxl111sf_i2c_stop(struct mxl111sf_state * state)147786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_stop(struct mxl111sf_state *state)
148786baecfSMauro Carvalho Chehab {
149786baecfSMauro Carvalho Chehab int ret;
150786baecfSMauro Carvalho Chehab
151786baecfSMauro Carvalho Chehab mxl_i2c("()");
152786baecfSMauro Carvalho Chehab
153786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
154786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN); /* stop */
155786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
156786baecfSMauro Carvalho Chehab goto fail;
157786baecfSMauro Carvalho Chehab
158786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
159786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SCL_OUT);
160786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
161786baecfSMauro Carvalho Chehab goto fail;
162786baecfSMauro Carvalho Chehab
163786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
164786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
165786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
166786baecfSMauro Carvalho Chehab goto fail;
167786baecfSMauro Carvalho Chehab
168786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
169786baecfSMauro Carvalho Chehab 0x10 | SW_SCL_OUT | SW_SDA_OUT);
170786baecfSMauro Carvalho Chehab mxl_fail(ret);
171786baecfSMauro Carvalho Chehab fail:
172786baecfSMauro Carvalho Chehab return ret;
173786baecfSMauro Carvalho Chehab }
174786baecfSMauro Carvalho Chehab
mxl111sf_i2c_ack(struct mxl111sf_state * state)175786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_ack(struct mxl111sf_state *state)
176786baecfSMauro Carvalho Chehab {
177786baecfSMauro Carvalho Chehab int ret;
178786baecfSMauro Carvalho Chehab u8 b = 0;
179786baecfSMauro Carvalho Chehab
180786baecfSMauro Carvalho Chehab mxl_i2c("()");
181786baecfSMauro Carvalho Chehab
182786baecfSMauro Carvalho Chehab ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &b);
183786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
184786baecfSMauro Carvalho Chehab goto fail;
185786baecfSMauro Carvalho Chehab
186786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
187786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN);
188786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
189786baecfSMauro Carvalho Chehab goto fail;
190786baecfSMauro Carvalho Chehab
191786baecfSMauro Carvalho Chehab /* pull SDA low */
192786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
193786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SCL_OUT);
194786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
195786baecfSMauro Carvalho Chehab goto fail;
196786baecfSMauro Carvalho Chehab
197786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
198786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SDA_OUT);
199786baecfSMauro Carvalho Chehab mxl_fail(ret);
200786baecfSMauro Carvalho Chehab fail:
201786baecfSMauro Carvalho Chehab return ret;
202786baecfSMauro Carvalho Chehab }
203786baecfSMauro Carvalho Chehab
mxl111sf_i2c_nack(struct mxl111sf_state * state)204786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_nack(struct mxl111sf_state *state)
205786baecfSMauro Carvalho Chehab {
206786baecfSMauro Carvalho Chehab int ret;
207786baecfSMauro Carvalho Chehab
208786baecfSMauro Carvalho Chehab mxl_i2c("()");
209786baecfSMauro Carvalho Chehab
210786baecfSMauro Carvalho Chehab /* SDA high to signal last byte read from slave */
211786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
212786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
213786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
214786baecfSMauro Carvalho Chehab goto fail;
215786baecfSMauro Carvalho Chehab
216786baecfSMauro Carvalho Chehab ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
217786baecfSMauro Carvalho Chehab 0x10 | SW_I2C_EN | SW_SDA_OUT);
218786baecfSMauro Carvalho Chehab mxl_fail(ret);
219786baecfSMauro Carvalho Chehab fail:
220786baecfSMauro Carvalho Chehab return ret;
221786baecfSMauro Carvalho Chehab }
222786baecfSMauro Carvalho Chehab
223786baecfSMauro Carvalho Chehab /* ------------------------------------------------------------------------ */
224786baecfSMauro Carvalho Chehab
mxl111sf_i2c_sw_xfer_msg(struct mxl111sf_state * state,struct i2c_msg * msg)225786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_sw_xfer_msg(struct mxl111sf_state *state,
226786baecfSMauro Carvalho Chehab struct i2c_msg *msg)
227786baecfSMauro Carvalho Chehab {
228786baecfSMauro Carvalho Chehab int i, ret;
229786baecfSMauro Carvalho Chehab
230786baecfSMauro Carvalho Chehab mxl_i2c("()");
231786baecfSMauro Carvalho Chehab
232786baecfSMauro Carvalho Chehab if (msg->flags & I2C_M_RD) {
233786baecfSMauro Carvalho Chehab
234786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_start(state);
235786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
236786baecfSMauro Carvalho Chehab goto fail;
237786baecfSMauro Carvalho Chehab
238786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_bitbang_sendbyte(state,
239786baecfSMauro Carvalho Chehab (msg->addr << 1) | 0x01);
240786baecfSMauro Carvalho Chehab if (mxl_fail(ret)) {
241786baecfSMauro Carvalho Chehab mxl111sf_i2c_stop(state);
242786baecfSMauro Carvalho Chehab goto fail;
243786baecfSMauro Carvalho Chehab }
244786baecfSMauro Carvalho Chehab
245786baecfSMauro Carvalho Chehab for (i = 0; i < msg->len; i++) {
246786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_bitbang_recvbyte(state,
247786baecfSMauro Carvalho Chehab &msg->buf[i]);
248786baecfSMauro Carvalho Chehab if (mxl_fail(ret)) {
249786baecfSMauro Carvalho Chehab mxl111sf_i2c_stop(state);
250786baecfSMauro Carvalho Chehab goto fail;
251786baecfSMauro Carvalho Chehab }
252786baecfSMauro Carvalho Chehab
253786baecfSMauro Carvalho Chehab if (i < msg->len - 1)
254786baecfSMauro Carvalho Chehab mxl111sf_i2c_ack(state);
255786baecfSMauro Carvalho Chehab }
256786baecfSMauro Carvalho Chehab
257786baecfSMauro Carvalho Chehab mxl111sf_i2c_nack(state);
258786baecfSMauro Carvalho Chehab
259786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_stop(state);
260786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
261786baecfSMauro Carvalho Chehab goto fail;
262786baecfSMauro Carvalho Chehab
263786baecfSMauro Carvalho Chehab } else {
264786baecfSMauro Carvalho Chehab
265786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_start(state);
266786baecfSMauro Carvalho Chehab if (mxl_fail(ret))
267786baecfSMauro Carvalho Chehab goto fail;
268786baecfSMauro Carvalho Chehab
269786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_bitbang_sendbyte(state,
270786baecfSMauro Carvalho Chehab (msg->addr << 1) & 0xfe);
271786baecfSMauro Carvalho Chehab if (mxl_fail(ret)) {
272786baecfSMauro Carvalho Chehab mxl111sf_i2c_stop(state);
273786baecfSMauro Carvalho Chehab goto fail;
274786baecfSMauro Carvalho Chehab }
275786baecfSMauro Carvalho Chehab
276786baecfSMauro Carvalho Chehab for (i = 0; i < msg->len; i++) {
277786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_bitbang_sendbyte(state,
278786baecfSMauro Carvalho Chehab msg->buf[i]);
279786baecfSMauro Carvalho Chehab if (mxl_fail(ret)) {
280786baecfSMauro Carvalho Chehab mxl111sf_i2c_stop(state);
281786baecfSMauro Carvalho Chehab goto fail;
282786baecfSMauro Carvalho Chehab }
283786baecfSMauro Carvalho Chehab }
284786baecfSMauro Carvalho Chehab
285786baecfSMauro Carvalho Chehab /* FIXME: we only want to do this on the last transaction */
286786baecfSMauro Carvalho Chehab mxl111sf_i2c_stop(state);
287786baecfSMauro Carvalho Chehab }
288786baecfSMauro Carvalho Chehab fail:
289786baecfSMauro Carvalho Chehab return ret;
290786baecfSMauro Carvalho Chehab }
291786baecfSMauro Carvalho Chehab
292786baecfSMauro Carvalho Chehab /* HW-I2C ----------------------------------------------------------------- */
293786baecfSMauro Carvalho Chehab
294786baecfSMauro Carvalho Chehab #define USB_WRITE_I2C_CMD 0x99
295786baecfSMauro Carvalho Chehab #define USB_READ_I2C_CMD 0xdd
296786baecfSMauro Carvalho Chehab #define USB_END_I2C_CMD 0xfe
297786baecfSMauro Carvalho Chehab
298786baecfSMauro Carvalho Chehab #define USB_WRITE_I2C_CMD_LEN 26
299786baecfSMauro Carvalho Chehab #define USB_READ_I2C_CMD_LEN 24
300786baecfSMauro Carvalho Chehab
301786baecfSMauro Carvalho Chehab #define I2C_MUX_REG 0x30
302786baecfSMauro Carvalho Chehab #define I2C_CONTROL_REG 0x00
303786baecfSMauro Carvalho Chehab #define I2C_SLAVE_ADDR_REG 0x08
304786baecfSMauro Carvalho Chehab #define I2C_DATA_REG 0x0c
305786baecfSMauro Carvalho Chehab #define I2C_INT_STATUS_REG 0x10
306786baecfSMauro Carvalho Chehab
mxl111sf_i2c_send_data(struct mxl111sf_state * state,u8 index,u8 * wdata)307786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_send_data(struct mxl111sf_state *state,
308786baecfSMauro Carvalho Chehab u8 index, u8 *wdata)
309786baecfSMauro Carvalho Chehab {
310d90b336fSDevin Heitmueller int ret = mxl111sf_ctrl_msg(state, wdata[0],
311786baecfSMauro Carvalho Chehab &wdata[1], 25, NULL, 0);
312786baecfSMauro Carvalho Chehab mxl_fail(ret);
313786baecfSMauro Carvalho Chehab
314786baecfSMauro Carvalho Chehab return ret;
315786baecfSMauro Carvalho Chehab }
316786baecfSMauro Carvalho Chehab
mxl111sf_i2c_get_data(struct mxl111sf_state * state,u8 index,u8 * wdata,u8 * rdata)317786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_get_data(struct mxl111sf_state *state,
318786baecfSMauro Carvalho Chehab u8 index, u8 *wdata, u8 *rdata)
319786baecfSMauro Carvalho Chehab {
320d90b336fSDevin Heitmueller int ret = mxl111sf_ctrl_msg(state, wdata[0],
321786baecfSMauro Carvalho Chehab &wdata[1], 25, rdata, 24);
322786baecfSMauro Carvalho Chehab mxl_fail(ret);
323786baecfSMauro Carvalho Chehab
324786baecfSMauro Carvalho Chehab return ret;
325786baecfSMauro Carvalho Chehab }
326786baecfSMauro Carvalho Chehab
mxl111sf_i2c_check_status(struct mxl111sf_state * state)327786baecfSMauro Carvalho Chehab static u8 mxl111sf_i2c_check_status(struct mxl111sf_state *state)
328786baecfSMauro Carvalho Chehab {
329786baecfSMauro Carvalho Chehab u8 status = 0;
330786baecfSMauro Carvalho Chehab u8 buf[26];
331786baecfSMauro Carvalho Chehab
332786baecfSMauro Carvalho Chehab mxl_i2c_adv("()");
333786baecfSMauro Carvalho Chehab
334786baecfSMauro Carvalho Chehab buf[0] = USB_READ_I2C_CMD;
335786baecfSMauro Carvalho Chehab buf[1] = 0x00;
336786baecfSMauro Carvalho Chehab
337786baecfSMauro Carvalho Chehab buf[2] = I2C_INT_STATUS_REG;
338786baecfSMauro Carvalho Chehab buf[3] = 0x00;
339786baecfSMauro Carvalho Chehab buf[4] = 0x00;
340786baecfSMauro Carvalho Chehab
341786baecfSMauro Carvalho Chehab buf[5] = USB_END_I2C_CMD;
342786baecfSMauro Carvalho Chehab
343786baecfSMauro Carvalho Chehab mxl111sf_i2c_get_data(state, 0, buf, buf);
344786baecfSMauro Carvalho Chehab
345786baecfSMauro Carvalho Chehab if (buf[1] & 0x04)
346786baecfSMauro Carvalho Chehab status = 1;
347786baecfSMauro Carvalho Chehab
348786baecfSMauro Carvalho Chehab return status;
349786baecfSMauro Carvalho Chehab }
350786baecfSMauro Carvalho Chehab
mxl111sf_i2c_check_fifo(struct mxl111sf_state * state)351786baecfSMauro Carvalho Chehab static u8 mxl111sf_i2c_check_fifo(struct mxl111sf_state *state)
352786baecfSMauro Carvalho Chehab {
353786baecfSMauro Carvalho Chehab u8 status = 0;
354786baecfSMauro Carvalho Chehab u8 buf[26];
355786baecfSMauro Carvalho Chehab
356786baecfSMauro Carvalho Chehab mxl_i2c("()");
357786baecfSMauro Carvalho Chehab
358786baecfSMauro Carvalho Chehab buf[0] = USB_READ_I2C_CMD;
359786baecfSMauro Carvalho Chehab buf[1] = 0x00;
360786baecfSMauro Carvalho Chehab
361786baecfSMauro Carvalho Chehab buf[2] = I2C_MUX_REG;
362786baecfSMauro Carvalho Chehab buf[3] = 0x00;
363786baecfSMauro Carvalho Chehab buf[4] = 0x00;
364786baecfSMauro Carvalho Chehab
365786baecfSMauro Carvalho Chehab buf[5] = I2C_INT_STATUS_REG;
366786baecfSMauro Carvalho Chehab buf[6] = 0x00;
367786baecfSMauro Carvalho Chehab buf[7] = 0x00;
368786baecfSMauro Carvalho Chehab buf[8] = USB_END_I2C_CMD;
369786baecfSMauro Carvalho Chehab
370786baecfSMauro Carvalho Chehab mxl111sf_i2c_get_data(state, 0, buf, buf);
371786baecfSMauro Carvalho Chehab
372786baecfSMauro Carvalho Chehab if (0x08 == (buf[1] & 0x08))
373786baecfSMauro Carvalho Chehab status = 1;
374786baecfSMauro Carvalho Chehab
375786baecfSMauro Carvalho Chehab if ((buf[5] & 0x02) == 0x02)
376786baecfSMauro Carvalho Chehab mxl_i2c("(buf[5] & 0x02) == 0x02"); /* FIXME */
377786baecfSMauro Carvalho Chehab
378786baecfSMauro Carvalho Chehab return status;
379786baecfSMauro Carvalho Chehab }
380786baecfSMauro Carvalho Chehab
mxl111sf_i2c_readagain(struct mxl111sf_state * state,u8 count,u8 * rbuf)381786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_readagain(struct mxl111sf_state *state,
382786baecfSMauro Carvalho Chehab u8 count, u8 *rbuf)
383786baecfSMauro Carvalho Chehab {
384786baecfSMauro Carvalho Chehab u8 i2c_w_data[26];
385786baecfSMauro Carvalho Chehab u8 i2c_r_data[24];
386786baecfSMauro Carvalho Chehab u8 i = 0;
387786baecfSMauro Carvalho Chehab u8 fifo_status = 0;
388786baecfSMauro Carvalho Chehab int status = 0;
389786baecfSMauro Carvalho Chehab
390786baecfSMauro Carvalho Chehab mxl_i2c("read %d bytes", count);
391786baecfSMauro Carvalho Chehab
392786baecfSMauro Carvalho Chehab while ((fifo_status == 0) && (i++ < 5))
393786baecfSMauro Carvalho Chehab fifo_status = mxl111sf_i2c_check_fifo(state);
394786baecfSMauro Carvalho Chehab
395786baecfSMauro Carvalho Chehab i2c_w_data[0] = 0xDD;
396786baecfSMauro Carvalho Chehab i2c_w_data[1] = 0x00;
397786baecfSMauro Carvalho Chehab
398786baecfSMauro Carvalho Chehab for (i = 2; i < 26; i++)
399786baecfSMauro Carvalho Chehab i2c_w_data[i] = 0xFE;
400786baecfSMauro Carvalho Chehab
401786baecfSMauro Carvalho Chehab for (i = 0; i < count; i++) {
402786baecfSMauro Carvalho Chehab i2c_w_data[2+(i*3)] = 0x0C;
403786baecfSMauro Carvalho Chehab i2c_w_data[3+(i*3)] = 0x00;
404786baecfSMauro Carvalho Chehab i2c_w_data[4+(i*3)] = 0x00;
405786baecfSMauro Carvalho Chehab }
406786baecfSMauro Carvalho Chehab
407786baecfSMauro Carvalho Chehab mxl111sf_i2c_get_data(state, 0, i2c_w_data, i2c_r_data);
408786baecfSMauro Carvalho Chehab
409786baecfSMauro Carvalho Chehab /* Check for I2C NACK status */
410786baecfSMauro Carvalho Chehab if (mxl111sf_i2c_check_status(state) == 1) {
411786baecfSMauro Carvalho Chehab mxl_i2c("error!");
412786baecfSMauro Carvalho Chehab } else {
413786baecfSMauro Carvalho Chehab for (i = 0; i < count; i++) {
414786baecfSMauro Carvalho Chehab rbuf[i] = i2c_r_data[(i*3)+1];
415786baecfSMauro Carvalho Chehab mxl_i2c("%02x\t %02x",
416786baecfSMauro Carvalho Chehab i2c_r_data[(i*3)+1],
417786baecfSMauro Carvalho Chehab i2c_r_data[(i*3)+2]);
418786baecfSMauro Carvalho Chehab }
419786baecfSMauro Carvalho Chehab
420786baecfSMauro Carvalho Chehab status = 1;
421786baecfSMauro Carvalho Chehab }
422786baecfSMauro Carvalho Chehab
423786baecfSMauro Carvalho Chehab return status;
424786baecfSMauro Carvalho Chehab }
425786baecfSMauro Carvalho Chehab
426786baecfSMauro Carvalho Chehab #define HWI2C400 1
mxl111sf_i2c_hw_xfer_msg(struct mxl111sf_state * state,struct i2c_msg * msg)427786baecfSMauro Carvalho Chehab static int mxl111sf_i2c_hw_xfer_msg(struct mxl111sf_state *state,
428786baecfSMauro Carvalho Chehab struct i2c_msg *msg)
429786baecfSMauro Carvalho Chehab {
430786baecfSMauro Carvalho Chehab int i, k, ret = 0;
431786baecfSMauro Carvalho Chehab u16 index = 0;
432786baecfSMauro Carvalho Chehab u8 buf[26];
433786baecfSMauro Carvalho Chehab u8 i2c_r_data[24];
434786baecfSMauro Carvalho Chehab u16 block_len;
435786baecfSMauro Carvalho Chehab u16 left_over_len;
436786baecfSMauro Carvalho Chehab u8 rd_status[8];
437786baecfSMauro Carvalho Chehab u8 ret_status;
438786baecfSMauro Carvalho Chehab u8 readbuff[26];
439786baecfSMauro Carvalho Chehab
440786baecfSMauro Carvalho Chehab mxl_i2c("addr: 0x%02x, read buff len: %d, write buff len: %d",
441786baecfSMauro Carvalho Chehab msg->addr, (msg->flags & I2C_M_RD) ? msg->len : 0,
442786baecfSMauro Carvalho Chehab (!(msg->flags & I2C_M_RD)) ? msg->len : 0);
443786baecfSMauro Carvalho Chehab
444786baecfSMauro Carvalho Chehab for (index = 0; index < 26; index++)
445786baecfSMauro Carvalho Chehab buf[index] = USB_END_I2C_CMD;
446786baecfSMauro Carvalho Chehab
447786baecfSMauro Carvalho Chehab /* command to indicate data payload is destined for I2C interface */
448786baecfSMauro Carvalho Chehab buf[0] = USB_WRITE_I2C_CMD;
449786baecfSMauro Carvalho Chehab buf[1] = 0x00;
450786baecfSMauro Carvalho Chehab
451786baecfSMauro Carvalho Chehab /* enable I2C interface */
452786baecfSMauro Carvalho Chehab buf[2] = I2C_MUX_REG;
453786baecfSMauro Carvalho Chehab buf[3] = 0x80;
454786baecfSMauro Carvalho Chehab buf[4] = 0x00;
455786baecfSMauro Carvalho Chehab
456786baecfSMauro Carvalho Chehab /* enable I2C interface */
457786baecfSMauro Carvalho Chehab buf[5] = I2C_MUX_REG;
458786baecfSMauro Carvalho Chehab buf[6] = 0x81;
459786baecfSMauro Carvalho Chehab buf[7] = 0x00;
460786baecfSMauro Carvalho Chehab
461786baecfSMauro Carvalho Chehab /* set Timeout register on I2C interface */
462786baecfSMauro Carvalho Chehab buf[8] = 0x14;
463786baecfSMauro Carvalho Chehab buf[9] = 0xff;
464786baecfSMauro Carvalho Chehab buf[10] = 0x00;
465786baecfSMauro Carvalho Chehab #if 0
466786baecfSMauro Carvalho Chehab /* enable Interrupts on I2C interface */
467786baecfSMauro Carvalho Chehab buf[8] = 0x24;
468786baecfSMauro Carvalho Chehab buf[9] = 0xF7;
469786baecfSMauro Carvalho Chehab buf[10] = 0x00;
470786baecfSMauro Carvalho Chehab #endif
471786baecfSMauro Carvalho Chehab buf[11] = 0x24;
472786baecfSMauro Carvalho Chehab buf[12] = 0xF7;
473786baecfSMauro Carvalho Chehab buf[13] = 0x00;
474786baecfSMauro Carvalho Chehab
475786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_send_data(state, 0, buf);
476786baecfSMauro Carvalho Chehab
477786baecfSMauro Carvalho Chehab /* write data on I2C bus */
478786baecfSMauro Carvalho Chehab if (!(msg->flags & I2C_M_RD) && (msg->len > 0)) {
479786baecfSMauro Carvalho Chehab mxl_i2c("%d\t%02x", msg->len, msg->buf[0]);
480786baecfSMauro Carvalho Chehab
481786baecfSMauro Carvalho Chehab /* control register on I2C interface to initialize I2C bus */
482786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
483786baecfSMauro Carvalho Chehab buf[3] = 0x5E;
484786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
485786baecfSMauro Carvalho Chehab
486786baecfSMauro Carvalho Chehab /* I2C Slave device Address */
487786baecfSMauro Carvalho Chehab buf[5] = I2C_SLAVE_ADDR_REG;
488786baecfSMauro Carvalho Chehab buf[6] = (msg->addr);
489786baecfSMauro Carvalho Chehab buf[7] = 0x00;
490786baecfSMauro Carvalho Chehab buf[8] = USB_END_I2C_CMD;
491786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_send_data(state, 0, buf);
492786baecfSMauro Carvalho Chehab
493786baecfSMauro Carvalho Chehab /* check for slave device status */
494786baecfSMauro Carvalho Chehab if (mxl111sf_i2c_check_status(state) == 1) {
495786baecfSMauro Carvalho Chehab mxl_i2c("NACK writing slave address %02x",
496786baecfSMauro Carvalho Chehab msg->addr);
497786baecfSMauro Carvalho Chehab /* if NACK, stop I2C bus and exit */
498786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
499786baecfSMauro Carvalho Chehab buf[3] = 0x4E;
500786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
501786baecfSMauro Carvalho Chehab ret = -EIO;
502786baecfSMauro Carvalho Chehab goto exit;
503786baecfSMauro Carvalho Chehab }
504786baecfSMauro Carvalho Chehab
505786baecfSMauro Carvalho Chehab /* I2C interface can do I2C operations in block of 8 bytes of
506786baecfSMauro Carvalho Chehab I2C data. calculation to figure out number of blocks of i2c
507786baecfSMauro Carvalho Chehab data required to program */
508786baecfSMauro Carvalho Chehab block_len = (msg->len / 8);
509786baecfSMauro Carvalho Chehab left_over_len = (msg->len % 8);
510786baecfSMauro Carvalho Chehab
511786baecfSMauro Carvalho Chehab mxl_i2c("block_len %d, left_over_len %d",
512786baecfSMauro Carvalho Chehab block_len, left_over_len);
513786baecfSMauro Carvalho Chehab
514786baecfSMauro Carvalho Chehab for (index = 0; index < block_len; index++) {
515786baecfSMauro Carvalho Chehab for (i = 0; i < 8; i++) {
516786baecfSMauro Carvalho Chehab /* write data on I2C interface */
517786baecfSMauro Carvalho Chehab buf[2+(i*3)] = I2C_DATA_REG;
518786baecfSMauro Carvalho Chehab buf[3+(i*3)] = msg->buf[(index*8)+i];
519786baecfSMauro Carvalho Chehab buf[4+(i*3)] = 0x00;
520786baecfSMauro Carvalho Chehab }
521786baecfSMauro Carvalho Chehab
522786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_send_data(state, 0, buf);
523786baecfSMauro Carvalho Chehab
524786baecfSMauro Carvalho Chehab /* check for I2C NACK status */
525786baecfSMauro Carvalho Chehab if (mxl111sf_i2c_check_status(state) == 1) {
526786baecfSMauro Carvalho Chehab mxl_i2c("NACK writing slave address %02x",
527786baecfSMauro Carvalho Chehab msg->addr);
528786baecfSMauro Carvalho Chehab
529786baecfSMauro Carvalho Chehab /* if NACK, stop I2C bus and exit */
530786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
531786baecfSMauro Carvalho Chehab buf[3] = 0x4E;
532786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
533786baecfSMauro Carvalho Chehab ret = -EIO;
534786baecfSMauro Carvalho Chehab goto exit;
535786baecfSMauro Carvalho Chehab }
536786baecfSMauro Carvalho Chehab
537786baecfSMauro Carvalho Chehab }
538786baecfSMauro Carvalho Chehab
539786baecfSMauro Carvalho Chehab if (left_over_len) {
540786baecfSMauro Carvalho Chehab for (k = 0; k < 26; k++)
541786baecfSMauro Carvalho Chehab buf[k] = USB_END_I2C_CMD;
542786baecfSMauro Carvalho Chehab
543786baecfSMauro Carvalho Chehab buf[0] = 0x99;
544786baecfSMauro Carvalho Chehab buf[1] = 0x00;
545786baecfSMauro Carvalho Chehab
546786baecfSMauro Carvalho Chehab for (i = 0; i < left_over_len; i++) {
547786baecfSMauro Carvalho Chehab buf[2+(i*3)] = I2C_DATA_REG;
548786baecfSMauro Carvalho Chehab buf[3+(i*3)] = msg->buf[(index*8)+i];
549786baecfSMauro Carvalho Chehab mxl_i2c("index = %d %d data %d",
550786baecfSMauro Carvalho Chehab index, i, msg->buf[(index*8)+i]);
551786baecfSMauro Carvalho Chehab buf[4+(i*3)] = 0x00;
552786baecfSMauro Carvalho Chehab }
553786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_send_data(state, 0, buf);
554786baecfSMauro Carvalho Chehab
555786baecfSMauro Carvalho Chehab /* check for I2C NACK status */
556786baecfSMauro Carvalho Chehab if (mxl111sf_i2c_check_status(state) == 1) {
557786baecfSMauro Carvalho Chehab mxl_i2c("NACK writing slave address %02x",
558786baecfSMauro Carvalho Chehab msg->addr);
559786baecfSMauro Carvalho Chehab
560786baecfSMauro Carvalho Chehab /* if NACK, stop I2C bus and exit */
561786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
562786baecfSMauro Carvalho Chehab buf[3] = 0x4E;
563786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
564786baecfSMauro Carvalho Chehab ret = -EIO;
565786baecfSMauro Carvalho Chehab goto exit;
566786baecfSMauro Carvalho Chehab }
567786baecfSMauro Carvalho Chehab
568786baecfSMauro Carvalho Chehab }
569786baecfSMauro Carvalho Chehab
570786baecfSMauro Carvalho Chehab /* issue I2C STOP after write */
571786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
572786baecfSMauro Carvalho Chehab buf[3] = 0x4E;
573786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
574786baecfSMauro Carvalho Chehab
575786baecfSMauro Carvalho Chehab }
576786baecfSMauro Carvalho Chehab
577786baecfSMauro Carvalho Chehab /* read data from I2C bus */
578786baecfSMauro Carvalho Chehab if ((msg->flags & I2C_M_RD) && (msg->len > 0)) {
579786baecfSMauro Carvalho Chehab mxl_i2c("read buf len %d", msg->len);
580786baecfSMauro Carvalho Chehab
581786baecfSMauro Carvalho Chehab /* command to indicate data payload is
582786baecfSMauro Carvalho Chehab destined for I2C interface */
583786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
584786baecfSMauro Carvalho Chehab buf[3] = 0xDF;
585786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
586786baecfSMauro Carvalho Chehab
587786baecfSMauro Carvalho Chehab /* I2C xfer length */
588786baecfSMauro Carvalho Chehab buf[5] = 0x14;
589786baecfSMauro Carvalho Chehab buf[6] = (msg->len & 0xFF);
590786baecfSMauro Carvalho Chehab buf[7] = 0;
591786baecfSMauro Carvalho Chehab
592786baecfSMauro Carvalho Chehab /* I2C slave device Address */
593786baecfSMauro Carvalho Chehab buf[8] = I2C_SLAVE_ADDR_REG;
594786baecfSMauro Carvalho Chehab buf[9] = msg->addr;
595786baecfSMauro Carvalho Chehab buf[10] = 0x00;
596786baecfSMauro Carvalho Chehab buf[11] = USB_END_I2C_CMD;
597786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_send_data(state, 0, buf);
598786baecfSMauro Carvalho Chehab
599786baecfSMauro Carvalho Chehab /* check for I2C NACK status */
600786baecfSMauro Carvalho Chehab if (mxl111sf_i2c_check_status(state) == 1) {
601786baecfSMauro Carvalho Chehab mxl_i2c("NACK reading slave address %02x",
602786baecfSMauro Carvalho Chehab msg->addr);
603786baecfSMauro Carvalho Chehab
604786baecfSMauro Carvalho Chehab /* if NACK, stop I2C bus and exit */
605786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
606786baecfSMauro Carvalho Chehab buf[3] = 0xC7;
607786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
608786baecfSMauro Carvalho Chehab ret = -EIO;
609786baecfSMauro Carvalho Chehab goto exit;
610786baecfSMauro Carvalho Chehab }
611786baecfSMauro Carvalho Chehab
612786baecfSMauro Carvalho Chehab /* I2C interface can do I2C operations in block of 8 bytes of
613786baecfSMauro Carvalho Chehab I2C data. calculation to figure out number of blocks of
614786baecfSMauro Carvalho Chehab i2c data required to program */
615786baecfSMauro Carvalho Chehab block_len = ((msg->len) / 8);
616786baecfSMauro Carvalho Chehab left_over_len = ((msg->len) % 8);
617786baecfSMauro Carvalho Chehab index = 0;
618786baecfSMauro Carvalho Chehab
619786baecfSMauro Carvalho Chehab mxl_i2c("block_len %d, left_over_len %d",
620786baecfSMauro Carvalho Chehab block_len, left_over_len);
621786baecfSMauro Carvalho Chehab
622786baecfSMauro Carvalho Chehab /* command to read data from I2C interface */
623786baecfSMauro Carvalho Chehab buf[0] = USB_READ_I2C_CMD;
624786baecfSMauro Carvalho Chehab buf[1] = 0x00;
625786baecfSMauro Carvalho Chehab
626786baecfSMauro Carvalho Chehab for (index = 0; index < block_len; index++) {
627786baecfSMauro Carvalho Chehab /* setup I2C read request packet on I2C interface */
628786baecfSMauro Carvalho Chehab for (i = 0; i < 8; i++) {
629786baecfSMauro Carvalho Chehab buf[2+(i*3)] = I2C_DATA_REG;
630786baecfSMauro Carvalho Chehab buf[3+(i*3)] = 0x00;
631786baecfSMauro Carvalho Chehab buf[4+(i*3)] = 0x00;
632786baecfSMauro Carvalho Chehab }
633786baecfSMauro Carvalho Chehab
634786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_get_data(state, 0, buf, i2c_r_data);
635786baecfSMauro Carvalho Chehab
636786baecfSMauro Carvalho Chehab /* check for I2C NACK status */
637786baecfSMauro Carvalho Chehab if (mxl111sf_i2c_check_status(state) == 1) {
638786baecfSMauro Carvalho Chehab mxl_i2c("NACK reading slave address %02x",
639786baecfSMauro Carvalho Chehab msg->addr);
640786baecfSMauro Carvalho Chehab
641786baecfSMauro Carvalho Chehab /* if NACK, stop I2C bus and exit */
642786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
643786baecfSMauro Carvalho Chehab buf[3] = 0xC7;
644786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
645786baecfSMauro Carvalho Chehab ret = -EIO;
646786baecfSMauro Carvalho Chehab goto exit;
647786baecfSMauro Carvalho Chehab }
648786baecfSMauro Carvalho Chehab
649786baecfSMauro Carvalho Chehab /* copy data from i2c data payload to read buffer */
650786baecfSMauro Carvalho Chehab for (i = 0; i < 8; i++) {
651786baecfSMauro Carvalho Chehab rd_status[i] = i2c_r_data[(i*3)+2];
652786baecfSMauro Carvalho Chehab
653786baecfSMauro Carvalho Chehab if (rd_status[i] == 0x04) {
654786baecfSMauro Carvalho Chehab if (i < 7) {
6554ab32006SMauro Carvalho Chehab mxl_i2c("i2c fifo empty! @ %d",
6564ab32006SMauro Carvalho Chehab i);
657786baecfSMauro Carvalho Chehab msg->buf[(index*8)+i] =
658786baecfSMauro Carvalho Chehab i2c_r_data[(i*3)+1];
659786baecfSMauro Carvalho Chehab /* read again */
660786baecfSMauro Carvalho Chehab ret_status =
661786baecfSMauro Carvalho Chehab mxl111sf_i2c_readagain(
662786baecfSMauro Carvalho Chehab state, 8-(i+1),
663786baecfSMauro Carvalho Chehab readbuff);
664786baecfSMauro Carvalho Chehab if (ret_status == 1) {
665786baecfSMauro Carvalho Chehab for (k = 0;
666786baecfSMauro Carvalho Chehab k < 8-(i+1);
667786baecfSMauro Carvalho Chehab k++) {
668786baecfSMauro Carvalho Chehab
669786baecfSMauro Carvalho Chehab msg->buf[(index*8)+(k+i+1)] =
670786baecfSMauro Carvalho Chehab readbuff[k];
671786baecfSMauro Carvalho Chehab mxl_i2c("read data: %02x\t %02x",
672786baecfSMauro Carvalho Chehab msg->buf[(index*8)+(k+i)],
673786baecfSMauro Carvalho Chehab (index*8)+(k+i));
674786baecfSMauro Carvalho Chehab mxl_i2c("read data: %02x\t %02x",
675786baecfSMauro Carvalho Chehab msg->buf[(index*8)+(k+i+1)],
676786baecfSMauro Carvalho Chehab readbuff[k]);
677786baecfSMauro Carvalho Chehab
678786baecfSMauro Carvalho Chehab }
679786baecfSMauro Carvalho Chehab goto stop_copy;
680786baecfSMauro Carvalho Chehab } else {
6814ab32006SMauro Carvalho Chehab mxl_i2c("readagain ERROR!");
682786baecfSMauro Carvalho Chehab }
683786baecfSMauro Carvalho Chehab } else {
684786baecfSMauro Carvalho Chehab msg->buf[(index*8)+i] =
685786baecfSMauro Carvalho Chehab i2c_r_data[(i*3)+1];
686786baecfSMauro Carvalho Chehab }
687786baecfSMauro Carvalho Chehab } else {
688786baecfSMauro Carvalho Chehab msg->buf[(index*8)+i] =
689786baecfSMauro Carvalho Chehab i2c_r_data[(i*3)+1];
690786baecfSMauro Carvalho Chehab }
691786baecfSMauro Carvalho Chehab }
692786baecfSMauro Carvalho Chehab stop_copy:
693786baecfSMauro Carvalho Chehab ;
694786baecfSMauro Carvalho Chehab
695786baecfSMauro Carvalho Chehab }
696786baecfSMauro Carvalho Chehab
697786baecfSMauro Carvalho Chehab if (left_over_len) {
698786baecfSMauro Carvalho Chehab for (k = 0; k < 26; k++)
699786baecfSMauro Carvalho Chehab buf[k] = USB_END_I2C_CMD;
700786baecfSMauro Carvalho Chehab
701786baecfSMauro Carvalho Chehab buf[0] = 0xDD;
702786baecfSMauro Carvalho Chehab buf[1] = 0x00;
703786baecfSMauro Carvalho Chehab
704786baecfSMauro Carvalho Chehab for (i = 0; i < left_over_len; i++) {
705786baecfSMauro Carvalho Chehab buf[2+(i*3)] = I2C_DATA_REG;
706786baecfSMauro Carvalho Chehab buf[3+(i*3)] = 0x00;
707786baecfSMauro Carvalho Chehab buf[4+(i*3)] = 0x00;
708786baecfSMauro Carvalho Chehab }
709786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_get_data(state, 0, buf,
710786baecfSMauro Carvalho Chehab i2c_r_data);
711786baecfSMauro Carvalho Chehab
712786baecfSMauro Carvalho Chehab /* check for I2C NACK status */
713786baecfSMauro Carvalho Chehab if (mxl111sf_i2c_check_status(state) == 1) {
714786baecfSMauro Carvalho Chehab mxl_i2c("NACK reading slave address %02x",
715786baecfSMauro Carvalho Chehab msg->addr);
716786baecfSMauro Carvalho Chehab
717786baecfSMauro Carvalho Chehab /* if NACK, stop I2C bus and exit */
718786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
719786baecfSMauro Carvalho Chehab buf[3] = 0xC7;
720786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
721786baecfSMauro Carvalho Chehab ret = -EIO;
722786baecfSMauro Carvalho Chehab goto exit;
723786baecfSMauro Carvalho Chehab }
724786baecfSMauro Carvalho Chehab
725786baecfSMauro Carvalho Chehab for (i = 0; i < left_over_len; i++) {
726786baecfSMauro Carvalho Chehab msg->buf[(block_len*8)+i] =
727786baecfSMauro Carvalho Chehab i2c_r_data[(i*3)+1];
728786baecfSMauro Carvalho Chehab mxl_i2c("read data: %02x\t %02x",
729786baecfSMauro Carvalho Chehab i2c_r_data[(i*3)+1],
730786baecfSMauro Carvalho Chehab i2c_r_data[(i*3)+2]);
731786baecfSMauro Carvalho Chehab }
732786baecfSMauro Carvalho Chehab }
733786baecfSMauro Carvalho Chehab
734786baecfSMauro Carvalho Chehab /* indicate I2C interface to issue NACK
735786baecfSMauro Carvalho Chehab after next I2C read op */
736786baecfSMauro Carvalho Chehab buf[0] = USB_WRITE_I2C_CMD;
737786baecfSMauro Carvalho Chehab buf[1] = 0x00;
738786baecfSMauro Carvalho Chehab
739786baecfSMauro Carvalho Chehab /* control register */
740786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
741786baecfSMauro Carvalho Chehab buf[3] = 0x17;
742786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
743786baecfSMauro Carvalho Chehab
744786baecfSMauro Carvalho Chehab buf[5] = USB_END_I2C_CMD;
745786baecfSMauro Carvalho Chehab ret = mxl111sf_i2c_send_data(state, 0, buf);
746786baecfSMauro Carvalho Chehab
747786baecfSMauro Carvalho Chehab /* control register */
748786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
749786baecfSMauro Carvalho Chehab buf[3] = 0xC7;
750786baecfSMauro Carvalho Chehab buf[4] = (HWI2C400) ? 0x03 : 0x0D;
751786baecfSMauro Carvalho Chehab
752786baecfSMauro Carvalho Chehab }
753786baecfSMauro Carvalho Chehab exit:
754786baecfSMauro Carvalho Chehab /* STOP and disable I2C MUX */
755786baecfSMauro Carvalho Chehab buf[0] = USB_WRITE_I2C_CMD;
756786baecfSMauro Carvalho Chehab buf[1] = 0x00;
757786baecfSMauro Carvalho Chehab
758786baecfSMauro Carvalho Chehab /* de-initilize I2C BUS */
759786baecfSMauro Carvalho Chehab buf[5] = USB_END_I2C_CMD;
760786baecfSMauro Carvalho Chehab mxl111sf_i2c_send_data(state, 0, buf);
761786baecfSMauro Carvalho Chehab
762786baecfSMauro Carvalho Chehab /* Control Register */
763786baecfSMauro Carvalho Chehab buf[2] = I2C_CONTROL_REG;
764786baecfSMauro Carvalho Chehab buf[3] = 0xDF;
765786baecfSMauro Carvalho Chehab buf[4] = 0x03;
766786baecfSMauro Carvalho Chehab
767786baecfSMauro Carvalho Chehab /* disable I2C interface */
768786baecfSMauro Carvalho Chehab buf[5] = I2C_MUX_REG;
769786baecfSMauro Carvalho Chehab buf[6] = 0x00;
770786baecfSMauro Carvalho Chehab buf[7] = 0x00;
771786baecfSMauro Carvalho Chehab
772786baecfSMauro Carvalho Chehab /* de-initilize I2C BUS */
773786baecfSMauro Carvalho Chehab buf[8] = USB_END_I2C_CMD;
774786baecfSMauro Carvalho Chehab mxl111sf_i2c_send_data(state, 0, buf);
775786baecfSMauro Carvalho Chehab
776786baecfSMauro Carvalho Chehab /* disable I2C interface */
777786baecfSMauro Carvalho Chehab buf[2] = I2C_MUX_REG;
778786baecfSMauro Carvalho Chehab buf[3] = 0x81;
779786baecfSMauro Carvalho Chehab buf[4] = 0x00;
780786baecfSMauro Carvalho Chehab
781786baecfSMauro Carvalho Chehab /* disable I2C interface */
782786baecfSMauro Carvalho Chehab buf[5] = I2C_MUX_REG;
783786baecfSMauro Carvalho Chehab buf[6] = 0x00;
784786baecfSMauro Carvalho Chehab buf[7] = 0x00;
785786baecfSMauro Carvalho Chehab
786786baecfSMauro Carvalho Chehab /* disable I2C interface */
787786baecfSMauro Carvalho Chehab buf[8] = I2C_MUX_REG;
788786baecfSMauro Carvalho Chehab buf[9] = 0x00;
789786baecfSMauro Carvalho Chehab buf[10] = 0x00;
790786baecfSMauro Carvalho Chehab
791786baecfSMauro Carvalho Chehab buf[11] = USB_END_I2C_CMD;
792786baecfSMauro Carvalho Chehab mxl111sf_i2c_send_data(state, 0, buf);
793786baecfSMauro Carvalho Chehab
794786baecfSMauro Carvalho Chehab return ret;
795786baecfSMauro Carvalho Chehab }
796786baecfSMauro Carvalho Chehab
797786baecfSMauro Carvalho Chehab /* ------------------------------------------------------------------------ */
798786baecfSMauro Carvalho Chehab
mxl111sf_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg msg[],int num)799786baecfSMauro Carvalho Chehab int mxl111sf_i2c_xfer(struct i2c_adapter *adap,
800786baecfSMauro Carvalho Chehab struct i2c_msg msg[], int num)
801786baecfSMauro Carvalho Chehab {
802786baecfSMauro Carvalho Chehab struct dvb_usb_device *d = i2c_get_adapdata(adap);
803786baecfSMauro Carvalho Chehab struct mxl111sf_state *state = d->priv;
804786baecfSMauro Carvalho Chehab int hwi2c = (state->chip_rev > MXL111SF_V6);
805786baecfSMauro Carvalho Chehab int i, ret;
806786baecfSMauro Carvalho Chehab
807786baecfSMauro Carvalho Chehab if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
808786baecfSMauro Carvalho Chehab return -EAGAIN;
809786baecfSMauro Carvalho Chehab
810786baecfSMauro Carvalho Chehab for (i = 0; i < num; i++) {
811786baecfSMauro Carvalho Chehab ret = (hwi2c) ?
812786baecfSMauro Carvalho Chehab mxl111sf_i2c_hw_xfer_msg(state, &msg[i]) :
813786baecfSMauro Carvalho Chehab mxl111sf_i2c_sw_xfer_msg(state, &msg[i]);
814786baecfSMauro Carvalho Chehab if (mxl_fail(ret)) {
8154ab32006SMauro Carvalho Chehab mxl_debug_adv("failed with error %d on i2c transaction %d of %d, %sing %d bytes to/from 0x%02x",
8164ab32006SMauro Carvalho Chehab ret, i+1, num,
817786baecfSMauro Carvalho Chehab (msg[i].flags & I2C_M_RD) ?
818786baecfSMauro Carvalho Chehab "read" : "writ",
819786baecfSMauro Carvalho Chehab msg[i].len, msg[i].addr);
820786baecfSMauro Carvalho Chehab
821786baecfSMauro Carvalho Chehab break;
822786baecfSMauro Carvalho Chehab }
823786baecfSMauro Carvalho Chehab }
824786baecfSMauro Carvalho Chehab
825786baecfSMauro Carvalho Chehab mutex_unlock(&d->i2c_mutex);
826786baecfSMauro Carvalho Chehab
827786baecfSMauro Carvalho Chehab return i == num ? num : -EREMOTEIO;
828786baecfSMauro Carvalho Chehab }
829