1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Conexant CX25821 PCIe bridge
5 * Copyright (C) 2009 Conexant Systems Inc.
6 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
7 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/i2c.h>
16 static unsigned int i2c_debug
;
17 module_param(i2c_debug
, int, 0644);
18 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
20 static unsigned int i2c_scan
;
21 module_param(i2c_scan
, int, 0444);
22 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
24 #define dprintk(level, fmt, arg...) \
26 if (i2c_debug >= level) \
27 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ##arg); \
30 #define I2C_WAIT_DELAY 32
31 #define I2C_WAIT_RETRY 64
33 #define I2C_EXTEND (1 << 3)
34 #define I2C_NOSTOP (1 << 4)
36 static inline int i2c_slave_did_ack(struct i2c_adapter
*i2c_adap
)
38 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
39 struct cx25821_dev
*dev
= bus
->dev
;
40 return cx_read(bus
->reg_stat
) & 0x01;
43 static inline int i2c_is_busy(struct i2c_adapter
*i2c_adap
)
45 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
46 struct cx25821_dev
*dev
= bus
->dev
;
47 return cx_read(bus
->reg_stat
) & 0x02 ? 1 : 0;
50 static int i2c_wait_done(struct i2c_adapter
*i2c_adap
)
54 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
55 if (!i2c_is_busy(i2c_adap
))
57 udelay(I2C_WAIT_DELAY
);
60 if (I2C_WAIT_RETRY
== count
)
66 static int i2c_sendbytes(struct i2c_adapter
*i2c_adap
,
67 const struct i2c_msg
*msg
, int joined_rlen
)
69 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
70 struct cx25821_dev
*dev
= bus
->dev
;
71 u32 wdata
, addr
, ctrl
;
75 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__
,
76 msg
->len
, joined_rlen
);
78 dprintk(1, "%s(msg->len=%d)\n", __func__
, msg
->len
);
80 /* Deal with i2c probe functions with zero payload */
82 cx_write(bus
->reg_addr
, msg
->addr
<< 25);
83 cx_write(bus
->reg_ctrl
, bus
->i2c_period
| (1 << 2));
85 if (!i2c_wait_done(i2c_adap
))
88 if (!i2c_slave_did_ack(i2c_adap
))
91 dprintk(1, "%s(): returns 0\n", __func__
);
95 /* dev, reg + first byte */
96 addr
= (msg
->addr
<< 25) | msg
->buf
[0];
99 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2);
102 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
103 else if (joined_rlen
)
106 cx_write(bus
->reg_addr
, addr
);
107 cx_write(bus
->reg_wdata
, wdata
);
108 cx_write(bus
->reg_ctrl
, ctrl
);
110 retval
= i2c_wait_done(i2c_adap
);
118 if (!(ctrl
& I2C_NOSTOP
))
122 for (cnt
= 1; cnt
< msg
->len
; cnt
++) {
123 /* following bytes */
124 wdata
= msg
->buf
[cnt
];
125 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2);
127 if (cnt
< msg
->len
- 1)
128 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
129 else if (joined_rlen
)
132 cx_write(bus
->reg_addr
, addr
);
133 cx_write(bus
->reg_wdata
, wdata
);
134 cx_write(bus
->reg_ctrl
, ctrl
);
136 retval
= i2c_wait_done(i2c_adap
);
144 dprintk(1, " %02x", msg
->buf
[cnt
]);
145 if (!(ctrl
& I2C_NOSTOP
))
156 pr_err(" ERR: %d\n", retval
);
160 static int i2c_readbytes(struct i2c_adapter
*i2c_adap
,
161 const struct i2c_msg
*msg
, int joined
)
163 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
164 struct cx25821_dev
*dev
= bus
->dev
;
168 if (i2c_debug
&& !joined
)
169 dprintk(1, "6-%s(msg->len=%d)\n", __func__
, msg
->len
);
171 /* Deal with i2c probe functions with zero payload */
173 cx_write(bus
->reg_addr
, msg
->addr
<< 25);
174 cx_write(bus
->reg_ctrl
, bus
->i2c_period
| (1 << 2) | 1);
175 if (!i2c_wait_done(i2c_adap
))
177 if (!i2c_slave_did_ack(i2c_adap
))
180 dprintk(1, "%s(): returns 0\n", __func__
);
188 dprintk(1, " <R %02x", (msg
->addr
<< 1) + 1);
191 for (cnt
= 0; cnt
< msg
->len
; cnt
++) {
193 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2) | 1;
195 if (cnt
< msg
->len
- 1)
196 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
198 cx_write(bus
->reg_addr
, msg
->addr
<< 25);
199 cx_write(bus
->reg_ctrl
, ctrl
);
201 retval
= i2c_wait_done(i2c_adap
);
206 msg
->buf
[cnt
] = cx_read(bus
->reg_rdata
) & 0xff;
209 dprintk(1, " %02x", msg
->buf
[cnt
]);
210 if (!(ctrl
& I2C_NOSTOP
))
220 pr_err(" ERR: %d\n", retval
);
224 static int i2c_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
, int num
)
226 struct cx25821_i2c
*bus
= i2c_adap
->algo_data
;
227 struct cx25821_dev
*dev
= bus
->dev
;
230 dprintk(1, "%s(num = %d)\n", __func__
, num
);
232 for (i
= 0; i
< num
; i
++) {
233 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
234 __func__
, num
, msgs
[i
].addr
, msgs
[i
].len
);
236 if (msgs
[i
].flags
& I2C_M_RD
) {
238 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 0);
239 } else if (i
+ 1 < num
&& (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
240 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
241 /* write then read from same address */
242 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
],
248 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 1);
251 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
], 0);
264 static u32
cx25821_functionality(struct i2c_adapter
*adap
)
266 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
| I2C_FUNC_SMBUS_WORD_DATA
|
267 I2C_FUNC_SMBUS_READ_WORD_DATA
| I2C_FUNC_SMBUS_WRITE_WORD_DATA
;
270 static const struct i2c_algorithm cx25821_i2c_algo_template
= {
271 .master_xfer
= i2c_xfer
,
272 .functionality
= cx25821_functionality
,
273 #ifdef NEED_ALGO_CONTROL
274 .algo_control
= dummy_algo_control
,
278 static const struct i2c_adapter cx25821_i2c_adap_template
= {
280 .owner
= THIS_MODULE
,
281 .algo
= &cx25821_i2c_algo_template
,
284 static const struct i2c_client cx25821_i2c_client_template
= {
285 .name
= "cx25821 internal",
288 /* init + register i2c adapter */
289 int cx25821_i2c_register(struct cx25821_i2c
*bus
)
291 struct cx25821_dev
*dev
= bus
->dev
;
293 dprintk(1, "%s(bus = %d)\n", __func__
, bus
->nr
);
295 bus
->i2c_adap
= cx25821_i2c_adap_template
;
296 bus
->i2c_client
= cx25821_i2c_client_template
;
297 bus
->i2c_adap
.dev
.parent
= &dev
->pci
->dev
;
299 strscpy(bus
->i2c_adap
.name
, bus
->dev
->name
, sizeof(bus
->i2c_adap
.name
));
301 bus
->i2c_adap
.algo_data
= bus
;
302 i2c_set_adapdata(&bus
->i2c_adap
, &dev
->v4l2_dev
);
303 i2c_add_adapter(&bus
->i2c_adap
);
305 bus
->i2c_client
.adapter
= &bus
->i2c_adap
;
308 bus
->i2c_client
.addr
= (0x88 >> 1);
313 int cx25821_i2c_unregister(struct cx25821_i2c
*bus
)
315 i2c_del_adapter(&bus
->i2c_adap
);
319 #if 0 /* Currently unused */
320 static void cx25821_av_clk(struct cx25821_dev
*dev
, int enable
)
322 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
325 dprintk(1, "%s(enabled = %d)\n", __func__
, enable
);
336 msg
.flags
= I2C_M_TEN
;
340 i2c_xfer(&dev
->i2c_bus
[0].i2c_adap
, &msg
, 1);
344 int cx25821_i2c_read(struct cx25821_i2c
*bus
, u16 reg_addr
, int *value
)
346 struct i2c_client
*client
= &bus
->i2c_client
;
348 u8 addr
[2] = { 0, 0 };
349 u8 buf
[4] = { 0, 0, 0, 0 };
351 struct i2c_msg msgs
[2] = {
353 .addr
= client
->addr
,
358 .addr
= client
->addr
,
365 addr
[0] = (reg_addr
>> 8);
366 addr
[1] = (reg_addr
& 0xff);
370 i2c_xfer(client
->adapter
, msgs
, 2);
372 v
= (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
378 int cx25821_i2c_write(struct cx25821_i2c
*bus
, u16 reg_addr
, int value
)
380 struct i2c_client
*client
= &bus
->i2c_client
;
382 u8 buf
[6] = { 0, 0, 0, 0, 0, 0 };
384 struct i2c_msg msgs
[1] = {
386 .addr
= client
->addr
,
393 buf
[0] = reg_addr
>> 8;
394 buf
[1] = reg_addr
& 0xff;
395 buf
[5] = (value
>> 24) & 0xff;
396 buf
[4] = (value
>> 16) & 0xff;
397 buf
[3] = (value
>> 8) & 0xff;
398 buf
[2] = value
& 0xff;
402 retval
= i2c_xfer(client
->adapter
, msgs
, 1);