1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Auvitek AU0828 USB bridge
5 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
16 #include "media/tuner.h"
17 #include <media/v4l2-common.h>
20 module_param(i2c_scan, int, 0444);
21 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
23 #define I2C_WAIT_DELAY 25
24 #define I2C_WAIT_RETRY 1000
26 static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
28 struct au0828_dev *dev = i2c_adap->algo_data;
29 return au0828_read(dev, AU0828_I2C_STATUS_201) &
30 AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1;
33 static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
35 struct au0828_dev *dev = i2c_adap->algo_data;
36 return au0828_read(dev, AU0828_I2C_STATUS_201) &
37 AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1;
40 static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
44 for (count = 0; count < I2C_WAIT_RETRY; count++) {
45 if (!i2c_slave_did_read_ack(i2c_adap))
47 udelay(I2C_WAIT_DELAY);
50 if (I2C_WAIT_RETRY == count)
56 static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
58 struct au0828_dev *dev = i2c_adap->algo_data;
59 return au0828_read(dev, AU0828_I2C_STATUS_201) &
60 AU0828_I2C_STATUS_READ_DONE ? 0 : 1;
63 static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
67 for (count = 0; count < I2C_WAIT_RETRY; count++) {
68 if (!i2c_is_read_busy(i2c_adap))
70 udelay(I2C_WAIT_DELAY);
73 if (I2C_WAIT_RETRY == count)
79 static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
81 struct au0828_dev *dev = i2c_adap->algo_data;
82 return au0828_read(dev, AU0828_I2C_STATUS_201) &
83 AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0;
86 static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
90 for (count = 0; count < I2C_WAIT_RETRY; count++) {
91 if (i2c_is_write_done(i2c_adap))
93 udelay(I2C_WAIT_DELAY);
96 if (I2C_WAIT_RETRY == count)
102 static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
104 struct au0828_dev *dev = i2c_adap->algo_data;
105 return au0828_read(dev, AU0828_I2C_STATUS_201) &
106 AU0828_I2C_STATUS_BUSY ? 1 : 0;
109 static int i2c_wait_done(struct i2c_adapter *i2c_adap)
113 for (count = 0; count < I2C_WAIT_RETRY; count++) {
114 if (!i2c_is_busy(i2c_adap))
116 udelay(I2C_WAIT_DELAY);
119 if (I2C_WAIT_RETRY == count)
125 /* FIXME: Implement join handling correctly */
126 static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
127 const struct i2c_msg *msg, int joined_rlen)
130 struct au0828_dev *dev = i2c_adap->algo_data;
131 u8 i2c_speed = dev->board.i2c_clk_divider;
133 dprintk(4, "%s()\n", __func__);
135 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
137 if (((dev->board.tuner_type == TUNER_XC5000) ||
138 (dev->board.tuner_type == TUNER_XC5000C)) &&
139 (dev->board.tuner_addr == msg->addr)) {
141 * Due to I2C clock stretch, we need to use a lower speed
142 * on xc5000 for commands. However, firmware transfer can
143 * speed up to 400 KHz.
146 i2c_speed = AU0828_I2C_CLK_250KHZ;
148 i2c_speed = AU0828_I2C_CLK_20KHZ;
150 /* Set the I2C clock */
151 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
153 /* Hardware needs 8 bit addresses */
154 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
156 dprintk(4, "SEND: %02x\n", msg->addr);
158 /* Deal with i2c_scan */
160 /* The analog tuner detection code makes use of the SMBUS_QUICK
161 message (which involves a zero length i2c write). To avoid
162 checking the status register when we didn't strobe out any
163 actual bytes to the bus, just do a read check. This is
164 consistent with how I saw i2c device checking done in the
165 USB trace of the Windows driver */
166 au0828_write(dev, AU0828_I2C_TRIGGER_200,
167 AU0828_I2C_TRIGGER_READ);
169 if (!i2c_wait_done(i2c_adap))
172 if (i2c_wait_read_ack(i2c_adap))
178 for (i = 0; i < msg->len;) {
180 dprintk(4, " %02x\n", msg->buf[i]);
182 au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
187 if ((strobe >= 4) || (i >= msg->len)) {
189 /* Strobe the byte into the bus */
191 au0828_write(dev, AU0828_I2C_TRIGGER_200,
192 AU0828_I2C_TRIGGER_WRITE |
193 AU0828_I2C_TRIGGER_HOLD);
195 au0828_write(dev, AU0828_I2C_TRIGGER_200,
196 AU0828_I2C_TRIGGER_WRITE);
198 /* Reset strobe trigger */
201 if (!i2c_wait_write_done(i2c_adap))
207 if (!i2c_wait_done(i2c_adap))
215 /* FIXME: Implement join handling correctly */
216 static int i2c_readbytes(struct i2c_adapter *i2c_adap,
217 const struct i2c_msg *msg, int joined)
219 struct au0828_dev *dev = i2c_adap->algo_data;
220 u8 i2c_speed = dev->board.i2c_clk_divider;
223 dprintk(4, "%s()\n", __func__);
225 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
228 * Due to xc5000c clock stretch, we cannot use full speed at
229 * readings from xc5000, as otherwise they'll fail.
231 if (((dev->board.tuner_type == TUNER_XC5000) ||
232 (dev->board.tuner_type == TUNER_XC5000C)) &&
233 (dev->board.tuner_addr == msg->addr))
234 i2c_speed = AU0828_I2C_CLK_20KHZ;
236 /* Set the I2C clock */
237 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
239 /* Hardware needs 8 bit addresses */
240 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
242 dprintk(4, " RECV:\n");
244 /* Deal with i2c_scan */
246 au0828_write(dev, AU0828_I2C_TRIGGER_200,
247 AU0828_I2C_TRIGGER_READ);
249 if (i2c_wait_read_ack(i2c_adap))
254 for (i = 0; i < msg->len;) {
259 au0828_write(dev, AU0828_I2C_TRIGGER_200,
260 AU0828_I2C_TRIGGER_READ |
261 AU0828_I2C_TRIGGER_HOLD);
263 au0828_write(dev, AU0828_I2C_TRIGGER_200,
264 AU0828_I2C_TRIGGER_READ);
266 if (!i2c_wait_read_done(i2c_adap))
269 msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
272 dprintk(4, " %02x\n", msg->buf[i-1]);
274 if (!i2c_wait_done(i2c_adap))
282 static int i2c_xfer(struct i2c_adapter *i2c_adap,
283 struct i2c_msg *msgs, int num)
287 dprintk(4, "%s(num = %d)\n", __func__, num);
289 for (i = 0; i < num; i++) {
290 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
291 __func__, num, msgs[i].addr, msgs[i].len);
292 if (msgs[i].flags & I2C_M_RD) {
294 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
295 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
296 msgs[i].addr == msgs[i + 1].addr) {
297 /* write then read from same address */
298 retval = i2c_sendbytes(i2c_adap, &msgs[i],
303 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
306 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
317 static u32 au0828_functionality(struct i2c_adapter *adap)
319 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
322 static const struct i2c_algorithm au0828_i2c_algo_template = {
323 .master_xfer = i2c_xfer,
324 .functionality = au0828_functionality,
327 /* ----------------------------------------------------------------------- */
329 static const struct i2c_adapter au0828_i2c_adap_template = {
330 .name = KBUILD_MODNAME,
331 .owner = THIS_MODULE,
332 .algo = &au0828_i2c_algo_template,
335 static const struct i2c_client au0828_i2c_client_template = {
336 .name = "au0828 internal",
339 static char *i2c_devs[128] = {
340 [0x8e >> 1] = "au8522",
341 [0xa0 >> 1] = "eeprom",
342 [0xc2 >> 1] = "tuner/xc5000",
345 static void do_i2c_scan(char *name, struct i2c_client *c)
350 for (i = 0; i < 128; i++) {
352 rc = i2c_master_recv(c, &buf, 0);
355 pr_info("%s: i2c scan: found device @ 0x%x [%s]\n",
356 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
360 /* init + register i2c adapter */
361 int au0828_i2c_register(struct au0828_dev *dev)
363 dprintk(1, "%s()\n", __func__);
365 dev->i2c_adap = au0828_i2c_adap_template;
366 dev->i2c_algo = au0828_i2c_algo_template;
367 dev->i2c_client = au0828_i2c_client_template;
369 dev->i2c_adap.dev.parent = &dev->usbdev->dev;
371 strscpy(dev->i2c_adap.name, KBUILD_MODNAME,
372 sizeof(dev->i2c_adap.name));
374 dev->i2c_adap.algo = &dev->i2c_algo;
375 dev->i2c_adap.algo_data = dev;
376 #ifdef CONFIG_VIDEO_AU0828_V4L2
377 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
379 i2c_set_adapdata(&dev->i2c_adap, dev);
381 i2c_add_adapter(&dev->i2c_adap);
383 dev->i2c_client.adapter = &dev->i2c_adap;
385 if (0 == dev->i2c_rc) {
386 pr_info("i2c bus registered\n");
388 do_i2c_scan(KBUILD_MODNAME, &dev->i2c_client);
390 pr_info("i2c bus register FAILED\n");
395 int au0828_i2c_unregister(struct au0828_dev *dev)
397 i2c_del_adapter(&dev->i2c_adap);