2 * Driver for the Conexant CX25821 PCIe bridge
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/module.h>
27 #include <linux/i2c.h>
30 static unsigned int i2c_debug
;
31 module_param(i2c_debug
, int, 0644);
32 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
34 static unsigned int i2c_scan
;
35 module_param(i2c_scan
, int, 0444);
36 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
38 #define dprintk(level, fmt, arg...) \
40 if (i2c_debug >= level) \
41 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ##arg); \
44 #define I2C_WAIT_DELAY 32
45 #define I2C_WAIT_RETRY 64
47 #define I2C_EXTEND (1 << 3)
48 #define I2C_NOSTOP (1 << 4)
50 static inline int i2c_slave_did_ack(struct i2c_adapter
*i2c_adap
)
52 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
53 struct cx25821_dev
*dev
= bus
->dev
;
54 return cx_read(bus
->reg_stat
) & 0x01;
57 static inline int i2c_is_busy(struct i2c_adapter
*i2c_adap
)
59 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
60 struct cx25821_dev
*dev
= bus
->dev
;
61 return cx_read(bus
->reg_stat
) & 0x02 ? 1 : 0;
64 static int i2c_wait_done(struct i2c_adapter
*i2c_adap
)
68 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
69 if (!i2c_is_busy(i2c_adap
))
71 udelay(I2C_WAIT_DELAY
);
74 if (I2C_WAIT_RETRY
== count
)
80 static int i2c_sendbytes(struct i2c_adapter
*i2c_adap
,
81 const struct i2c_msg
*msg
, int joined_rlen
)
83 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
84 struct cx25821_dev
*dev
= bus
->dev
;
85 u32 wdata
, addr
, ctrl
;
89 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__
,
90 msg
->len
, joined_rlen
);
92 dprintk(1, "%s(msg->len=%d)\n", __func__
, msg
->len
);
94 /* Deal with i2c probe functions with zero payload */
96 cx_write(bus
->reg_addr
, msg
->addr
<< 25);
97 cx_write(bus
->reg_ctrl
, bus
->i2c_period
| (1 << 2));
99 if (!i2c_wait_done(i2c_adap
))
102 if (!i2c_slave_did_ack(i2c_adap
))
105 dprintk(1, "%s(): returns 0\n", __func__
);
109 /* dev, reg + first byte */
110 addr
= (msg
->addr
<< 25) | msg
->buf
[0];
113 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2);
116 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
117 else if (joined_rlen
)
120 cx_write(bus
->reg_addr
, addr
);
121 cx_write(bus
->reg_wdata
, wdata
);
122 cx_write(bus
->reg_ctrl
, ctrl
);
124 retval
= i2c_wait_done(i2c_adap
);
132 if (!(ctrl
& I2C_NOSTOP
))
136 for (cnt
= 1; cnt
< msg
->len
; cnt
++) {
137 /* following bytes */
138 wdata
= msg
->buf
[cnt
];
139 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2);
141 if (cnt
< msg
->len
- 1)
142 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
143 else if (joined_rlen
)
146 cx_write(bus
->reg_addr
, addr
);
147 cx_write(bus
->reg_wdata
, wdata
);
148 cx_write(bus
->reg_ctrl
, ctrl
);
150 retval
= i2c_wait_done(i2c_adap
);
158 dprintk(1, " %02x", msg
->buf
[cnt
]);
159 if (!(ctrl
& I2C_NOSTOP
))
170 pr_err(" ERR: %d\n", retval
);
174 static int i2c_readbytes(struct i2c_adapter
*i2c_adap
,
175 const struct i2c_msg
*msg
, int joined
)
177 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
178 struct cx25821_dev
*dev
= bus
->dev
;
182 if (i2c_debug
&& !joined
)
183 dprintk(1, "6-%s(msg->len=%d)\n", __func__
, msg
->len
);
185 /* Deal with i2c probe functions with zero payload */
187 cx_write(bus
->reg_addr
, msg
->addr
<< 25);
188 cx_write(bus
->reg_ctrl
, bus
->i2c_period
| (1 << 2) | 1);
189 if (!i2c_wait_done(i2c_adap
))
191 if (!i2c_slave_did_ack(i2c_adap
))
194 dprintk(1, "%s(): returns 0\n", __func__
);
202 dprintk(1, " <R %02x", (msg
->addr
<< 1) + 1);
205 for (cnt
= 0; cnt
< msg
->len
; cnt
++) {
207 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2) | 1;
209 if (cnt
< msg
->len
- 1)
210 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
212 cx_write(bus
->reg_addr
, msg
->addr
<< 25);
213 cx_write(bus
->reg_ctrl
, ctrl
);
215 retval
= i2c_wait_done(i2c_adap
);
220 msg
->buf
[cnt
] = cx_read(bus
->reg_rdata
) & 0xff;
223 dprintk(1, " %02x", msg
->buf
[cnt
]);
224 if (!(ctrl
& I2C_NOSTOP
))
234 pr_err(" ERR: %d\n", retval
);
238 static int i2c_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
, int num
)
240 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
241 struct cx25821_dev
*dev
= bus
->dev
;
244 dprintk(1, "%s(num = %d)\n", __func__
, num
);
246 for (i
= 0; i
< num
; i
++) {
247 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
248 __func__
, num
, msgs
[i
].addr
, msgs
[i
].len
);
250 if (msgs
[i
].flags
& I2C_M_RD
) {
252 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 0);
253 } else if (i
+ 1 < num
&& (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
254 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
255 /* write then read from same address */
256 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
],
262 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 1);
265 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
], 0);
278 static u32
cx25821_functionality(struct i2c_adapter
*adap
)
280 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
| I2C_FUNC_SMBUS_WORD_DATA
|
281 I2C_FUNC_SMBUS_READ_WORD_DATA
| I2C_FUNC_SMBUS_WRITE_WORD_DATA
;
284 static struct i2c_algorithm cx25821_i2c_algo_template
= {
285 .master_xfer
= i2c_xfer
,
286 .functionality
= cx25821_functionality
,
287 #ifdef NEED_ALGO_CONTROL
288 .algo_control
= dummy_algo_control
,
292 static struct i2c_adapter cx25821_i2c_adap_template
= {
294 .owner
= THIS_MODULE
,
295 .algo
= &cx25821_i2c_algo_template
,
298 static struct i2c_client cx25821_i2c_client_template
= {
299 .name
= "cx25821 internal",
302 /* init + register i2c adapter */
303 int cx25821_i2c_register(struct cx25821_i2c
*bus
)
305 struct cx25821_dev
*dev
= bus
->dev
;
307 dprintk(1, "%s(bus = %d)\n", __func__
, bus
->nr
);
309 bus
->i2c_adap
= cx25821_i2c_adap_template
;
310 bus
->i2c_client
= cx25821_i2c_client_template
;
311 bus
->i2c_adap
.dev
.parent
= &dev
->pci
->dev
;
313 strlcpy(bus
->i2c_adap
.name
, bus
->dev
->name
, sizeof(bus
->i2c_adap
.name
));
315 bus
->i2c_adap
.algo_data
= bus
;
316 i2c_set_adapdata(&bus
->i2c_adap
, &dev
->v4l2_dev
);
317 i2c_add_adapter(&bus
->i2c_adap
);
319 bus
->i2c_client
.adapter
= &bus
->i2c_adap
;
322 bus
->i2c_client
.addr
= (0x88 >> 1);
327 int cx25821_i2c_unregister(struct cx25821_i2c
*bus
)
329 i2c_del_adapter(&bus
->i2c_adap
);
333 #if 0 /* Currently unused */
334 static void cx25821_av_clk(struct cx25821_dev
*dev
, int enable
)
336 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
339 dprintk(1, "%s(enabled = %d)\n", __func__
, enable
);
350 msg
.flags
= I2C_M_TEN
;
354 i2c_xfer(&dev
->i2c_bus
[0].i2c_adap
, &msg
, 1);
358 int cx25821_i2c_read(struct cx25821_i2c
*bus
, u16 reg_addr
, int *value
)
360 struct i2c_client
*client
= &bus
->i2c_client
;
362 u8 addr
[2] = { 0, 0 };
363 u8 buf
[4] = { 0, 0, 0, 0 };
365 struct i2c_msg msgs
[2] = {
367 .addr
= client
->addr
,
372 .addr
= client
->addr
,
379 addr
[0] = (reg_addr
>> 8);
380 addr
[1] = (reg_addr
& 0xff);
384 i2c_xfer(client
->adapter
, msgs
, 2);
386 v
= (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
392 int cx25821_i2c_write(struct cx25821_i2c
*bus
, u16 reg_addr
, int value
)
394 struct i2c_client
*client
= &bus
->i2c_client
;
396 u8 buf
[6] = { 0, 0, 0, 0, 0, 0 };
398 struct i2c_msg msgs
[1] = {
400 .addr
= client
->addr
,
407 buf
[0] = reg_addr
>> 8;
408 buf
[1] = reg_addr
& 0xff;
409 buf
[5] = (value
>> 24) & 0xff;
410 buf
[4] = (value
>> 16) & 0xff;
411 buf
[3] = (value
>> 8) & 0xff;
412 buf
[2] = value
& 0xff;
416 retval
= i2c_xfer(client
->adapter
, msgs
, 1);