Linux Audio

Check our new training course

Loading...
  1/*
  2 *  Driver for the Auvitek AU0828 USB bridge
  3 *
  4 *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
  5 *
  6 *  This program is free software; you can redistribute it and/or modify
  7 *  it under the terms of the GNU General Public License as published by
  8 *  the Free Software Foundation; either version 2 of the License, or
  9 *  (at your option) any later version.
 10 *
 11 *  This program is distributed in the hope that it will be useful,
 12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 *
 15 *  GNU General Public License for more details.
 16 *
 17 *  You should have received a copy of the GNU General Public License
 18 *  along with this program; if not, write to the Free Software
 19 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 20 */
 21
 22#include <linux/module.h>
 23#include <linux/moduleparam.h>
 24#include <linux/init.h>
 25#include <linux/delay.h>
 26#include <linux/io.h>
 27
 28#include "au0828.h"
 29#include "media/tuner.h"
 30#include <media/v4l2-common.h>
 31
 32static int i2c_scan;
 33module_param(i2c_scan, int, 0444);
 34MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
 35
 36#define I2C_WAIT_DELAY 25
 37#define I2C_WAIT_RETRY 1000
 38
 39static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
 40{
 41	struct au0828_dev *dev = i2c_adap->algo_data;
 42	return au0828_read(dev, AU0828_I2C_STATUS_201) &
 43		AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1;
 44}
 45
 46static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
 47{
 48	struct au0828_dev *dev = i2c_adap->algo_data;
 49	return au0828_read(dev, AU0828_I2C_STATUS_201) &
 50		AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1;
 51}
 52
 53static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
 54{
 55	int count;
 56
 57	for (count = 0; count < I2C_WAIT_RETRY; count++) {
 58		if (!i2c_slave_did_read_ack(i2c_adap))
 59			break;
 60		udelay(I2C_WAIT_DELAY);
 61	}
 62
 63	if (I2C_WAIT_RETRY == count)
 64		return 0;
 65
 66	return 1;
 67}
 68
 69static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
 70{
 71	struct au0828_dev *dev = i2c_adap->algo_data;
 72	return au0828_read(dev, AU0828_I2C_STATUS_201) &
 73		AU0828_I2C_STATUS_READ_DONE ? 0 : 1;
 74}
 75
 76static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
 77{
 78	int count;
 79
 80	for (count = 0; count < I2C_WAIT_RETRY; count++) {
 81		if (!i2c_is_read_busy(i2c_adap))
 82			break;
 83		udelay(I2C_WAIT_DELAY);
 84	}
 85
 86	if (I2C_WAIT_RETRY == count)
 87		return 0;
 88
 89	return 1;
 90}
 91
 92static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
 93{
 94	struct au0828_dev *dev = i2c_adap->algo_data;
 95	return au0828_read(dev, AU0828_I2C_STATUS_201) &
 96		AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0;
 97}
 98
 99static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
100{
101	int count;
102
103	for (count = 0; count < I2C_WAIT_RETRY; count++) {
104		if (i2c_is_write_done(i2c_adap))
105			break;
106		udelay(I2C_WAIT_DELAY);
107	}
108
109	if (I2C_WAIT_RETRY == count)
110		return 0;
111
112	return 1;
113}
114
115static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
116{
117	struct au0828_dev *dev = i2c_adap->algo_data;
118	return au0828_read(dev, AU0828_I2C_STATUS_201) &
119		AU0828_I2C_STATUS_BUSY ? 1 : 0;
120}
121
122static int i2c_wait_done(struct i2c_adapter *i2c_adap)
123{
124	int count;
125
126	for (count = 0; count < I2C_WAIT_RETRY; count++) {
127		if (!i2c_is_busy(i2c_adap))
128			break;
129		udelay(I2C_WAIT_DELAY);
130	}
131
132	if (I2C_WAIT_RETRY == count)
133		return 0;
134
135	return 1;
136}
137
138/* FIXME: Implement join handling correctly */
139static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
140	const struct i2c_msg *msg, int joined_rlen)
141{
142	int i, strobe = 0;
143	struct au0828_dev *dev = i2c_adap->algo_data;
144
145	dprintk(4, "%s()\n", __func__);
146
147	au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
148
149	/* Set the I2C clock */
150	if (((dev->board.tuner_type == TUNER_XC5000) ||
151	     (dev->board.tuner_type == TUNER_XC5000C)) &&
152	    (dev->board.tuner_addr == msg->addr) &&
153	    (msg->len == 64)) {
154		/* Hack to speed up firmware load.  The xc5000 lets us do up
155		   to 400 KHz when in firmware download mode */
156		au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
157			     AU0828_I2C_CLK_250KHZ);
158	} else {
159		/* Use the i2c clock speed in the board configuration */
160		au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
161			     dev->board.i2c_clk_divider);
162	}
163
164	/* Hardware needs 8 bit addresses */
165	au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
166
167	dprintk(4, "SEND: %02x\n", msg->addr);
168
169	/* Deal with i2c_scan */
170	if (msg->len == 0) {
171		/* The analog tuner detection code makes use of the SMBUS_QUICK
172		   message (which involves a zero length i2c write).  To avoid
173		   checking the status register when we didn't strobe out any
174		   actual bytes to the bus, just do a read check.  This is
175		   consistent with how I saw i2c device checking done in the
176		   USB trace of the Windows driver */
177		au0828_write(dev, AU0828_I2C_TRIGGER_200,
178			     AU0828_I2C_TRIGGER_READ);
179
180		if (!i2c_wait_done(i2c_adap))
181			return -EIO;
182
183		if (i2c_wait_read_ack(i2c_adap))
184			return -EIO;
185
186		return 0;
187	}
188
189	for (i = 0; i < msg->len;) {
190
191		dprintk(4, " %02x\n", msg->buf[i]);
192
193		au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
194
195		strobe++;
196		i++;
197
198		if ((strobe >= 4) || (i >= msg->len)) {
199
200			/* Strobe the byte into the bus */
201			if (i < msg->len)
202				au0828_write(dev, AU0828_I2C_TRIGGER_200,
203					     AU0828_I2C_TRIGGER_WRITE |
204					     AU0828_I2C_TRIGGER_HOLD);
205			else
206				au0828_write(dev, AU0828_I2C_TRIGGER_200,
207					     AU0828_I2C_TRIGGER_WRITE);
208
209			/* Reset strobe trigger */
210			strobe = 0;
211
212			if (!i2c_wait_write_done(i2c_adap))
213				return -EIO;
214
215		}
216
217	}
218	if (!i2c_wait_done(i2c_adap))
219		return -EIO;
220
221	dprintk(4, "\n");
222
223	return msg->len;
224}
225
226/* FIXME: Implement join handling correctly */
227static int i2c_readbytes(struct i2c_adapter *i2c_adap,
228	const struct i2c_msg *msg, int joined)
229{
230	struct au0828_dev *dev = i2c_adap->algo_data;
231	int i;
232
233	dprintk(4, "%s()\n", __func__);
234
235	au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
236
237	/* Set the I2C clock */
238	au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
239		     dev->board.i2c_clk_divider);
240
241	/* Hardware needs 8 bit addresses */
242	au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
243
244	dprintk(4, " RECV:\n");
245
246	/* Deal with i2c_scan */
247	if (msg->len == 0) {
248		au0828_write(dev, AU0828_I2C_TRIGGER_200,
249			     AU0828_I2C_TRIGGER_READ);
250
251		if (i2c_wait_read_ack(i2c_adap))
252			return -EIO;
253		return 0;
254	}
255
256	for (i = 0; i < msg->len;) {
257
258		i++;
259
260		if (i < msg->len)
261			au0828_write(dev, AU0828_I2C_TRIGGER_200,
262				     AU0828_I2C_TRIGGER_READ |
263				     AU0828_I2C_TRIGGER_HOLD);
264		else
265			au0828_write(dev, AU0828_I2C_TRIGGER_200,
266				     AU0828_I2C_TRIGGER_READ);
267
268		if (!i2c_wait_read_done(i2c_adap))
269			return -EIO;
270
271		msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
272			0xff;
273
274		dprintk(4, " %02x\n", msg->buf[i-1]);
275	}
276	if (!i2c_wait_done(i2c_adap))
277		return -EIO;
278
279	dprintk(4, "\n");
280
281	return msg->len;
282}
283
284static int i2c_xfer(struct i2c_adapter *i2c_adap,
285		    struct i2c_msg *msgs, int num)
286{
287	int i, retval = 0;
288
289	dprintk(4, "%s(num = %d)\n", __func__, num);
290
291	for (i = 0; i < num; i++) {
292		dprintk(4, "%s(num = %d) addr = 0x%02x  len = 0x%x\n",
293			__func__, num, msgs[i].addr, msgs[i].len);
294		if (msgs[i].flags & I2C_M_RD) {
295			/* read */
296			retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
297		} else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
298			   msgs[i].addr == msgs[i + 1].addr) {
299			/* write then read from same address */
300			retval = i2c_sendbytes(i2c_adap, &msgs[i],
301					       msgs[i + 1].len);
302			if (retval < 0)
303				goto err;
304			i++;
305			retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
306		} else {
307			/* write */
308			retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
309		}
310		if (retval < 0)
311			goto err;
312	}
313	return num;
314
315err:
316	return retval;
317}
318
319static u32 au0828_functionality(struct i2c_adapter *adap)
320{
321	return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
322}
323
324static struct i2c_algorithm au0828_i2c_algo_template = {
325	.master_xfer	= i2c_xfer,
326	.functionality	= au0828_functionality,
327};
328
329/* ----------------------------------------------------------------------- */
330
331static struct i2c_adapter au0828_i2c_adap_template = {
332	.name              = DRIVER_NAME,
333	.owner             = THIS_MODULE,
334	.algo              = &au0828_i2c_algo_template,
335};
336
337static struct i2c_client au0828_i2c_client_template = {
338	.name	= "au0828 internal",
339};
340
341static char *i2c_devs[128] = {
342	[0x8e >> 1] = "au8522",
343	[0xa0 >> 1] = "eeprom",
344	[0xc2 >> 1] = "tuner/xc5000",
345};
346
347static void do_i2c_scan(char *name, struct i2c_client *c)
348{
349	unsigned char buf;
350	int i, rc;
351
352	for (i = 0; i < 128; i++) {
353		c->addr = i;
354		rc = i2c_master_recv(c, &buf, 0);
355		if (rc < 0)
356			continue;
357		printk(KERN_INFO "%s: i2c scan: found device @ 0x%x  [%s]\n",
358		       name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
359	}
360}
361
362/* init + register i2c adapter */
363int au0828_i2c_register(struct au0828_dev *dev)
364{
365	dprintk(1, "%s()\n", __func__);
366
367	dev->i2c_adap = au0828_i2c_adap_template;
368	dev->i2c_algo = au0828_i2c_algo_template;
369	dev->i2c_client = au0828_i2c_client_template;
370
371	dev->i2c_adap.dev.parent = &dev->usbdev->dev;
372
373	strlcpy(dev->i2c_adap.name, DRIVER_NAME,
374		sizeof(dev->i2c_adap.name));
375
376	dev->i2c_adap.algo = &dev->i2c_algo;
377	dev->i2c_adap.algo_data = dev;
378#ifdef CONFIG_VIDEO_AU0828_V4L2
379	i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
380#else
381	i2c_set_adapdata(&dev->i2c_adap, dev);
382#endif
383	i2c_add_adapter(&dev->i2c_adap);
384
385	dev->i2c_client.adapter = &dev->i2c_adap;
386
387	if (0 == dev->i2c_rc) {
388		printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
389		if (i2c_scan)
390			do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
391	} else
392		printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
393
394	return dev->i2c_rc;
395}
396
397int au0828_i2c_unregister(struct au0828_dev *dev)
398{
399	i2c_del_adapter(&dev->i2c_adap);
400	return 0;
401}
402
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  Driver for the Auvitek AU0828 USB bridge
  4 *
  5 *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
  6 */
  7
  8#include "au0828.h"
  9
 10#include <linux/module.h>
 11#include <linux/moduleparam.h>
 12#include <linux/init.h>
 13#include <linux/delay.h>
 14#include <linux/io.h>
 15
 16#include "media/tuner.h"
 17#include <media/v4l2-common.h>
 18
 19static int i2c_scan;
 20module_param(i2c_scan, int, 0444);
 21MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
 22
 23#define I2C_WAIT_DELAY 25
 24#define I2C_WAIT_RETRY 1000
 25
 26static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
 27{
 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;
 31}
 32
 33static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
 34{
 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;
 38}
 39
 40static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
 41{
 42	int count;
 43
 44	for (count = 0; count < I2C_WAIT_RETRY; count++) {
 45		if (!i2c_slave_did_read_ack(i2c_adap))
 46			break;
 47		udelay(I2C_WAIT_DELAY);
 48	}
 49
 50	if (I2C_WAIT_RETRY == count)
 51		return 0;
 52
 53	return 1;
 54}
 55
 56static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
 57{
 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;
 61}
 62
 63static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
 64{
 65	int count;
 66
 67	for (count = 0; count < I2C_WAIT_RETRY; count++) {
 68		if (!i2c_is_read_busy(i2c_adap))
 69			break;
 70		udelay(I2C_WAIT_DELAY);
 71	}
 72
 73	if (I2C_WAIT_RETRY == count)
 74		return 0;
 75
 76	return 1;
 77}
 78
 79static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
 80{
 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;
 84}
 85
 86static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
 87{
 88	int count;
 89
 90	for (count = 0; count < I2C_WAIT_RETRY; count++) {
 91		if (i2c_is_write_done(i2c_adap))
 92			break;
 93		udelay(I2C_WAIT_DELAY);
 94	}
 95
 96	if (I2C_WAIT_RETRY == count)
 97		return 0;
 98
 99	return 1;
100}
101
102static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
103{
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;
107}
108
109static int i2c_wait_done(struct i2c_adapter *i2c_adap)
110{
111	int count;
112
113	for (count = 0; count < I2C_WAIT_RETRY; count++) {
114		if (!i2c_is_busy(i2c_adap))
115			break;
116		udelay(I2C_WAIT_DELAY);
117	}
118
119	if (I2C_WAIT_RETRY == count)
120		return 0;
121
122	return 1;
123}
124
125/* FIXME: Implement join handling correctly */
126static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
127	const struct i2c_msg *msg, int joined_rlen)
128{
129	int i, strobe = 0;
130	struct au0828_dev *dev = i2c_adap->algo_data;
131	u8 i2c_speed = dev->board.i2c_clk_divider;
132
133	dprintk(4, "%s()\n", __func__);
134
135	au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
136
137	if (((dev->board.tuner_type == TUNER_XC5000) ||
138	     (dev->board.tuner_type == TUNER_XC5000C)) &&
139	    (dev->board.tuner_addr == msg->addr)) {
140		/*
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.
144		 */
145		if (msg->len == 64)
146			i2c_speed = AU0828_I2C_CLK_250KHZ;
147		else
148			i2c_speed = AU0828_I2C_CLK_20KHZ;
149	}
150	/* Set the I2C clock */
151	au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
152
153	/* Hardware needs 8 bit addresses */
154	au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
155
156	dprintk(4, "SEND: %02x\n", msg->addr);
157
158	/* Deal with i2c_scan */
159	if (msg->len == 0) {
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);
168
169		if (!i2c_wait_done(i2c_adap))
170			return -EIO;
171
172		if (i2c_wait_read_ack(i2c_adap))
173			return -EIO;
174
175		return 0;
176	}
177
178	for (i = 0; i < msg->len;) {
179
180		dprintk(4, " %02x\n", msg->buf[i]);
181
182		au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
183
184		strobe++;
185		i++;
186
187		if ((strobe >= 4) || (i >= msg->len)) {
188
189			/* Strobe the byte into the bus */
190			if (i < msg->len)
191				au0828_write(dev, AU0828_I2C_TRIGGER_200,
192					     AU0828_I2C_TRIGGER_WRITE |
193					     AU0828_I2C_TRIGGER_HOLD);
194			else
195				au0828_write(dev, AU0828_I2C_TRIGGER_200,
196					     AU0828_I2C_TRIGGER_WRITE);
197
198			/* Reset strobe trigger */
199			strobe = 0;
200
201			if (!i2c_wait_write_done(i2c_adap))
202				return -EIO;
203
204		}
205
206	}
207	if (!i2c_wait_done(i2c_adap))
208		return -EIO;
209
210	dprintk(4, "\n");
211
212	return msg->len;
213}
214
215/* FIXME: Implement join handling correctly */
216static int i2c_readbytes(struct i2c_adapter *i2c_adap,
217	const struct i2c_msg *msg, int joined)
218{
219	struct au0828_dev *dev = i2c_adap->algo_data;
220	u8 i2c_speed = dev->board.i2c_clk_divider;
221	int i;
222
223	dprintk(4, "%s()\n", __func__);
224
225	au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
226
227	/*
228	 * Due to xc5000c clock stretch, we cannot use full speed at
229	 * readings from xc5000, as otherwise they'll fail.
230	 */
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;
235
236	/* Set the I2C clock */
237	au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
238
239	/* Hardware needs 8 bit addresses */
240	au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
241
242	dprintk(4, " RECV:\n");
243
244	/* Deal with i2c_scan */
245	if (msg->len == 0) {
246		au0828_write(dev, AU0828_I2C_TRIGGER_200,
247			     AU0828_I2C_TRIGGER_READ);
248
249		if (i2c_wait_read_ack(i2c_adap))
250			return -EIO;
251		return 0;
252	}
253
254	for (i = 0; i < msg->len;) {
255
256		i++;
257
258		if (i < msg->len)
259			au0828_write(dev, AU0828_I2C_TRIGGER_200,
260				     AU0828_I2C_TRIGGER_READ |
261				     AU0828_I2C_TRIGGER_HOLD);
262		else
263			au0828_write(dev, AU0828_I2C_TRIGGER_200,
264				     AU0828_I2C_TRIGGER_READ);
265
266		if (!i2c_wait_read_done(i2c_adap))
267			return -EIO;
268
269		msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
270			0xff;
271
272		dprintk(4, " %02x\n", msg->buf[i-1]);
273	}
274	if (!i2c_wait_done(i2c_adap))
275		return -EIO;
276
277	dprintk(4, "\n");
278
279	return msg->len;
280}
281
282static int i2c_xfer(struct i2c_adapter *i2c_adap,
283		    struct i2c_msg *msgs, int num)
284{
285	int i, retval = 0;
286
287	dprintk(4, "%s(num = %d)\n", __func__, num);
288
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) {
293			/* read */
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],
299					       msgs[i + 1].len);
300			if (retval < 0)
301				goto err;
302			i++;
303			retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
304		} else {
305			/* write */
306			retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
307		}
308		if (retval < 0)
309			goto err;
310	}
311	return num;
312
313err:
314	return retval;
315}
316
317static u32 au0828_functionality(struct i2c_adapter *adap)
318{
319	return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
320}
321
322static const struct i2c_algorithm au0828_i2c_algo_template = {
323	.master_xfer	= i2c_xfer,
324	.functionality	= au0828_functionality,
325};
326
327/* ----------------------------------------------------------------------- */
328
329static const struct i2c_adapter au0828_i2c_adap_template = {
330	.name              = KBUILD_MODNAME,
331	.owner             = THIS_MODULE,
332	.algo              = &au0828_i2c_algo_template,
333};
334
335static const struct i2c_client au0828_i2c_client_template = {
336	.name	= "au0828 internal",
337};
338
339static char *i2c_devs[128] = {
340	[0x8e >> 1] = "au8522",
341	[0xa0 >> 1] = "eeprom",
342	[0xc2 >> 1] = "tuner/xc5000",
343};
344
345static void do_i2c_scan(char *name, struct i2c_client *c)
346{
347	unsigned char buf;
348	int i, rc;
349
350	for (i = 0; i < 128; i++) {
351		c->addr = i;
352		rc = i2c_master_recv(c, &buf, 0);
353		if (rc < 0)
354			continue;
355		pr_info("%s: i2c scan: found device @ 0x%x  [%s]\n",
356		       name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
357	}
358}
359
360/* init + register i2c adapter */
361int au0828_i2c_register(struct au0828_dev *dev)
362{
363	dprintk(1, "%s()\n", __func__);
364
365	dev->i2c_adap = au0828_i2c_adap_template;
366	dev->i2c_algo = au0828_i2c_algo_template;
367	dev->i2c_client = au0828_i2c_client_template;
368
369	dev->i2c_adap.dev.parent = &dev->usbdev->dev;
370
371	strscpy(dev->i2c_adap.name, KBUILD_MODNAME,
372		sizeof(dev->i2c_adap.name));
373
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);
378#else
379	i2c_set_adapdata(&dev->i2c_adap, dev);
380#endif
381	i2c_add_adapter(&dev->i2c_adap);
382
383	dev->i2c_client.adapter = &dev->i2c_adap;
384
385	if (0 == dev->i2c_rc) {
386		pr_info("i2c bus registered\n");
387		if (i2c_scan)
388			do_i2c_scan(KBUILD_MODNAME, &dev->i2c_client);
389	} else
390		pr_info("i2c bus register FAILED\n");
391
392	return dev->i2c_rc;
393}
394
395int au0828_i2c_unregister(struct au0828_dev *dev)
396{
397	i2c_del_adapter(&dev->i2c_adap);
398	return 0;
399}
400