1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Conexant CX23885 PCIe bridge
5 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
15 #include <media/v4l2-common.h>
17 static unsigned int i2c_debug;
18 module_param(i2c_debug, int, 0644);
19 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
21 static unsigned int i2c_scan;
22 module_param(i2c_scan, int, 0444);
23 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
25 #define dprintk(level, fmt, arg...)\
26 do { if (i2c_debug >= level)\
27 printk(KERN_DEBUG pr_fmt("%s: i2c:" fmt), \
31 #define I2C_WAIT_DELAY 32
32 #define I2C_WAIT_RETRY 64
34 #define I2C_EXTEND (1 << 3)
35 #define I2C_NOSTOP (1 << 4)
37 static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
39 struct cx23885_i2c *bus = i2c_adap->algo_data;
40 struct cx23885_dev *dev = bus->dev;
41 return cx_read(bus->reg_stat) & 0x01;
44 static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
46 struct cx23885_i2c *bus = i2c_adap->algo_data;
47 struct cx23885_dev *dev = bus->dev;
48 return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
51 static int i2c_wait_done(struct i2c_adapter *i2c_adap)
55 for (count = 0; count < I2C_WAIT_RETRY; count++) {
56 if (!i2c_is_busy(i2c_adap))
58 udelay(I2C_WAIT_DELAY);
61 if (I2C_WAIT_RETRY == count)
67 static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
68 const struct i2c_msg *msg, int joined_rlen)
70 struct cx23885_i2c *bus = i2c_adap->algo_data;
71 struct cx23885_dev *dev = bus->dev;
72 u32 wdata, addr, ctrl;
76 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
77 msg->len, joined_rlen);
79 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
81 /* Deal with i2c probe functions with zero payload */
83 cx_write(bus->reg_addr, msg->addr << 25);
84 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
85 if (!i2c_wait_done(i2c_adap))
87 if (!i2c_slave_did_ack(i2c_adap))
90 dprintk(1, "%s() returns 0\n", __func__);
95 /* dev, reg + first byte */
96 addr = (msg->addr << 25) | msg->buf[0];
98 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
101 ctrl |= I2C_NOSTOP | I2C_EXTEND;
102 else if (joined_rlen)
105 cx_write(bus->reg_addr, addr);
106 cx_write(bus->reg_wdata, wdata);
107 cx_write(bus->reg_ctrl, ctrl);
109 if (!i2c_wait_done(i2c_adap))
112 printk(KERN_DEBUG " <W %02x %02x", msg->addr << 1, msg->buf[0]);
113 if (!(ctrl & I2C_NOSTOP))
117 for (cnt = 1; cnt < msg->len; cnt++) {
118 /* following bytes */
119 wdata = msg->buf[cnt];
120 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
122 if (cnt < msg->len - 1)
123 ctrl |= I2C_NOSTOP | I2C_EXTEND;
124 else if (joined_rlen)
127 cx_write(bus->reg_addr, addr);
128 cx_write(bus->reg_wdata, wdata);
129 cx_write(bus->reg_ctrl, ctrl);
131 if (!i2c_wait_done(i2c_adap))
134 pr_cont(" %02x", msg->buf[cnt]);
135 if (!(ctrl & I2C_NOSTOP))
144 pr_err(" ERR: %d\n", retval);
148 static int i2c_readbytes(struct i2c_adapter *i2c_adap,
149 const struct i2c_msg *msg, int joined)
151 struct cx23885_i2c *bus = i2c_adap->algo_data;
152 struct cx23885_dev *dev = bus->dev;
157 if (i2c_debug && !joined)
158 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
160 /* Deal with i2c probe functions with zero payload */
162 cx_write(bus->reg_addr, msg->addr << 25);
163 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
164 if (!i2c_wait_done(i2c_adap))
166 if (!i2c_slave_did_ack(i2c_adap))
170 dprintk(1, "%s() returns 0\n", __func__);
178 dprintk(1, " <R %02x", (msg->addr << 1) + 1);
181 for (cnt = 0; cnt < msg->len; cnt++) {
183 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
185 if (cnt < msg->len - 1)
186 ctrl |= I2C_NOSTOP | I2C_EXTEND;
188 cx_write(bus->reg_addr, msg->addr << 25);
189 cx_write(bus->reg_ctrl, ctrl);
191 if (!i2c_wait_done(i2c_adap))
193 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
195 dprintk(1, " %02x", msg->buf[cnt]);
196 if (!(ctrl & I2C_NOSTOP))
205 pr_err(" ERR: %d\n", retval);
209 static int i2c_xfer(struct i2c_adapter *i2c_adap,
210 struct i2c_msg *msgs, int num)
214 dprintk(1, "%s(num = %d)\n", __func__, num);
216 for (i = 0 ; i < num; i++) {
217 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
218 __func__, num, msgs[i].addr, msgs[i].len);
219 if (msgs[i].flags & I2C_M_RD) {
221 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
222 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
223 msgs[i].addr == msgs[i + 1].addr) {
224 /* write then read from same address */
225 retval = i2c_sendbytes(i2c_adap, &msgs[i],
230 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
233 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
244 static u32 cx23885_functionality(struct i2c_adapter *adap)
246 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
249 static const struct i2c_algorithm cx23885_i2c_algo_template = {
250 .master_xfer = i2c_xfer,
251 .functionality = cx23885_functionality,
254 /* ----------------------------------------------------------------------- */
256 static const struct i2c_adapter cx23885_i2c_adap_template = {
258 .owner = THIS_MODULE,
259 .algo = &cx23885_i2c_algo_template,
262 static const struct i2c_client cx23885_i2c_client_template = {
263 .name = "cx23885 internal",
266 static char *i2c_devs[128] = {
267 [0x10 >> 1] = "tda10048",
268 [0x12 >> 1] = "dib7000pc",
269 [0x1c >> 1] = "lgdt3303",
270 [0x80 >> 1] = "cs3308",
271 [0x82 >> 1] = "cs3308",
272 [0x86 >> 1] = "tda9887",
273 [0x32 >> 1] = "cx24227",
274 [0x88 >> 1] = "cx25837",
275 [0x84 >> 1] = "tda8295",
276 [0x98 >> 1] = "flatiron",
277 [0xa0 >> 1] = "eeprom",
278 [0xc0 >> 1] = "tuner/mt2131/tda8275",
279 [0xc2 >> 1] = "tuner/mt2131/tda8275/xc5000/xc3028",
280 [0xc8 >> 1] = "tuner/xc3028L",
283 static void do_i2c_scan(char *name, struct i2c_client *c)
288 for (i = 0; i < 128; i++) {
290 rc = i2c_master_recv(c, &buf, 0);
293 pr_info("%s: i2c scan: found device @ 0x%04x [%s]\n",
294 name, i, i2c_devs[i] ? i2c_devs[i] : "???");
298 /* init + register i2c adapter */
299 int cx23885_i2c_register(struct cx23885_i2c *bus)
301 struct cx23885_dev *dev = bus->dev;
303 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
305 bus->i2c_adap = cx23885_i2c_adap_template;
306 bus->i2c_client = cx23885_i2c_client_template;
307 bus->i2c_adap.dev.parent = &dev->pci->dev;
309 strscpy(bus->i2c_adap.name, bus->dev->name,
310 sizeof(bus->i2c_adap.name));
312 bus->i2c_adap.algo_data = bus;
313 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
314 i2c_add_adapter(&bus->i2c_adap);
316 bus->i2c_client.adapter = &bus->i2c_adap;
318 if (0 == bus->i2c_rc) {
319 dprintk(1, "%s: i2c bus %d registered\n", dev->name, bus->nr);
321 pr_info("%s: scan bus %d:\n",
323 do_i2c_scan(dev->name, &bus->i2c_client);
326 pr_warn("%s: i2c bus %d register FAILED\n",
329 /* Instantiate the IR receiver device, if present */
330 if (0 == bus->i2c_rc) {
331 struct i2c_board_info info;
332 static const unsigned short addr_list[] = {
336 memset(&info, 0, sizeof(struct i2c_board_info));
337 strscpy(info.type, "ir_video", I2C_NAME_SIZE);
338 /* Use quick read command for probe, some IR chips don't
340 i2c_new_scanned_device(&bus->i2c_adap, &info, addr_list,
341 i2c_probe_func_quick_read);
347 int cx23885_i2c_unregister(struct cx23885_i2c *bus)
349 i2c_del_adapter(&bus->i2c_adap);
353 void cx23885_av_clk(struct cx23885_dev *dev, int enable)
355 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
358 dprintk(1, "%s(enabled = %d)\n", __func__, enable);
369 msg.flags = I2C_M_TEN;
373 i2c_xfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);