xref: /openbmc/linux/drivers/media/pci/ivtv/ivtv-i2c.c (revision 887069f4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     I2C functions
4     Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
5     Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
6 
7  */
8 
9 /*
10     This file includes an i2c implementation that was reverse engineered
11     from the Hauppauge windows driver.  Older ivtv versions used i2c-algo-bit,
12     which whilst fine under most circumstances, had trouble with the Zilog
13     CPU on the PVR-150 which handles IR functions (occasional inability to
14     communicate with the chip until it was reset) and also with the i2c
15     bus being completely unreachable when multiple PVR cards were present.
16 
17     The implementation is very similar to i2c-algo-bit, but there are enough
18     subtle differences that the two are hard to merge.  The general strategy
19     employed by i2c-algo-bit is to use udelay() to implement the timing
20     when putting out bits on the scl/sda lines.  The general strategy taken
21     here is to poll the lines for state changes (see ivtv_waitscl and
22     ivtv_waitsda).  In addition there are small delays at various locations
23     which poll the SCL line 5 times (ivtv_scldelay).  I would guess that
24     since this is memory mapped I/O that the length of those delays is tied
25     to the PCI bus clock.  There is some extra code to do with recovery
26     and retries.  Since it is not known what causes the actual i2c problems
27     in the first place, the only goal if one was to attempt to use
28     i2c-algo-bit would be to try to make it follow the same code path.
29     This would be a lot of work, and I'm also not convinced that it would
30     provide a generic benefit to i2c-algo-bit.  Therefore consider this
31     an engineering solution -- not pretty, but it works.
32 
33     Some more general comments about what we are doing:
34 
35     The i2c bus is a 2 wire serial bus, with clock (SCL) and data (SDA)
36     lines.  To communicate on the bus (as a master, we don't act as a slave),
37     we first initiate a start condition (ivtv_start).  We then write the
38     address of the device that we want to communicate with, along with a flag
39     that indicates whether this is a read or a write.  The slave then issues
40     an ACK signal (ivtv_ack), which tells us that it is ready for reading /
41     writing.  We then proceed with reading or writing (ivtv_read/ivtv_write),
42     and finally issue a stop condition (ivtv_stop) to make the bus available
43     to other masters.
44 
45     There is an additional form of transaction where a write may be
46     immediately followed by a read.  In this case, there is no intervening
47     stop condition.  (Only the msp3400 chip uses this method of data transfer).
48  */
49 
50 #include "ivtv-driver.h"
51 #include "ivtv-cards.h"
52 #include "ivtv-gpio.h"
53 #include "ivtv-i2c.h"
54 #include <media/drv-intf/cx25840.h>
55 
56 /* i2c implementation for cx23415/6 chip, ivtv project.
57  * Author: Kevin Thayer (nufan_wfk at yahoo.com)
58  */
59 /* i2c stuff */
60 #define IVTV_REG_I2C_SETSCL_OFFSET 0x7000
61 #define IVTV_REG_I2C_SETSDA_OFFSET 0x7004
62 #define IVTV_REG_I2C_GETSCL_OFFSET 0x7008
63 #define IVTV_REG_I2C_GETSDA_OFFSET 0x700c
64 
65 #define IVTV_CS53L32A_I2C_ADDR		0x11
66 #define IVTV_M52790_I2C_ADDR		0x48
67 #define IVTV_CX25840_I2C_ADDR		0x44
68 #define IVTV_SAA7115_I2C_ADDR		0x21
69 #define IVTV_SAA7127_I2C_ADDR		0x44
70 #define IVTV_SAA717x_I2C_ADDR		0x21
71 #define IVTV_MSP3400_I2C_ADDR		0x40
72 #define IVTV_HAUPPAUGE_I2C_ADDR		0x50
73 #define IVTV_WM8739_I2C_ADDR		0x1a
74 #define IVTV_WM8775_I2C_ADDR		0x1b
75 #define IVTV_TEA5767_I2C_ADDR		0x60
76 #define IVTV_UPD64031A_I2C_ADDR		0x12
77 #define IVTV_UPD64083_I2C_ADDR		0x5c
78 #define IVTV_VP27SMPX_I2C_ADDR		0x5b
79 #define IVTV_M52790_I2C_ADDR		0x48
80 #define IVTV_AVERMEDIA_IR_RX_I2C_ADDR	0x40
81 #define IVTV_HAUP_EXT_IR_RX_I2C_ADDR	0x1a
82 #define IVTV_HAUP_INT_IR_RX_I2C_ADDR	0x18
83 #define IVTV_Z8F0811_IR_TX_I2C_ADDR	0x70
84 #define IVTV_Z8F0811_IR_RX_I2C_ADDR	0x71
85 #define IVTV_ADAPTEC_IR_ADDR		0x6b
86 
87 /* This array should match the IVTV_HW_ defines */
88 static const u8 hw_addrs[IVTV_HW_MAX_BITS] = {
89 	IVTV_CX25840_I2C_ADDR,
90 	IVTV_SAA7115_I2C_ADDR,
91 	IVTV_SAA7127_I2C_ADDR,
92 	IVTV_MSP3400_I2C_ADDR,
93 	0,
94 	IVTV_WM8775_I2C_ADDR,
95 	IVTV_CS53L32A_I2C_ADDR,
96 	0,
97 	IVTV_SAA7115_I2C_ADDR,
98 	IVTV_UPD64031A_I2C_ADDR,
99 	IVTV_UPD64083_I2C_ADDR,
100 	IVTV_SAA717x_I2C_ADDR,
101 	IVTV_WM8739_I2C_ADDR,
102 	IVTV_VP27SMPX_I2C_ADDR,
103 	IVTV_M52790_I2C_ADDR,
104 	0,				/* IVTV_HW_GPIO dummy driver ID */
105 	IVTV_AVERMEDIA_IR_RX_I2C_ADDR,	/* IVTV_HW_I2C_IR_RX_AVER */
106 	IVTV_HAUP_EXT_IR_RX_I2C_ADDR,	/* IVTV_HW_I2C_IR_RX_HAUP_EXT */
107 	IVTV_HAUP_INT_IR_RX_I2C_ADDR,	/* IVTV_HW_I2C_IR_RX_HAUP_INT */
108 	IVTV_Z8F0811_IR_RX_I2C_ADDR,	/* IVTV_HW_Z8F0811_IR_HAUP */
109 	IVTV_ADAPTEC_IR_ADDR,		/* IVTV_HW_I2C_IR_RX_ADAPTEC */
110 };
111 
112 /* This array should match the IVTV_HW_ defines */
113 static const char * const hw_devicenames[IVTV_HW_MAX_BITS] = {
114 	"cx25840",
115 	"saa7115",
116 	"saa7127_auto",	/* saa7127 or saa7129 */
117 	"msp3400",
118 	"tuner",
119 	"wm8775",
120 	"cs53l32a",
121 	"tveeprom",
122 	"saa7114",
123 	"upd64031a",
124 	"upd64083",
125 	"saa717x",
126 	"wm8739",
127 	"vp27smpx",
128 	"m52790",
129 	"gpio",
130 	"ir_video",		/* IVTV_HW_I2C_IR_RX_AVER */
131 	"ir_video",		/* IVTV_HW_I2C_IR_RX_HAUP_EXT */
132 	"ir_video",		/* IVTV_HW_I2C_IR_RX_HAUP_INT */
133 	"ir_z8f0811_haup",	/* IVTV_HW_Z8F0811_IR_HAUP */
134 	"ir_video",		/* IVTV_HW_I2C_IR_RX_ADAPTEC */
135 };
136 
137 static int get_key_adaptec(struct IR_i2c *ir, enum rc_proto *protocol,
138 			   u32 *scancode, u8 *toggle)
139 {
140 	unsigned char keybuf[4];
141 
142 	keybuf[0] = 0x00;
143 	i2c_master_send(ir->c, keybuf, 1);
144 	/* poll IR chip */
145 	if (i2c_master_recv(ir->c, keybuf, sizeof(keybuf)) != sizeof(keybuf)) {
146 		return 0;
147 	}
148 
149 	/* key pressed ? */
150 	if (keybuf[2] == 0xff)
151 		return 0;
152 
153 	/* remove repeat bit */
154 	keybuf[2] &= 0x7f;
155 	keybuf[3] |= 0x80;
156 
157 	*protocol = RC_PROTO_UNKNOWN;
158 	*scancode = keybuf[3] | keybuf[2] << 8 | keybuf[1] << 16 |keybuf[0] << 24;
159 	*toggle = 0;
160 	return 1;
161 }
162 
163 static int ivtv_i2c_new_ir(struct ivtv *itv, u32 hw, const char *type, u8 addr)
164 {
165 	struct i2c_board_info info;
166 	struct i2c_adapter *adap = &itv->i2c_adap;
167 	struct IR_i2c_init_data *init_data = &itv->ir_i2c_init_data;
168 	unsigned short addr_list[2] = { addr, I2C_CLIENT_END };
169 
170 	/* Only allow one IR receiver to be registered per board */
171 	if (itv->hw_flags & IVTV_HW_IR_ANY)
172 		return -1;
173 
174 	/* Our default information for ir-kbd-i2c.c to use */
175 	switch (hw) {
176 	case IVTV_HW_I2C_IR_RX_AVER:
177 		init_data->ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
178 		init_data->internal_get_key_func =
179 					IR_KBD_GET_KEY_AVERMEDIA_CARDBUS;
180 		init_data->type = RC_PROTO_BIT_OTHER;
181 		init_data->name = "AVerMedia AVerTV card";
182 		break;
183 	case IVTV_HW_I2C_IR_RX_HAUP_EXT:
184 	case IVTV_HW_I2C_IR_RX_HAUP_INT:
185 		init_data->ir_codes = RC_MAP_HAUPPAUGE;
186 		init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP;
187 		init_data->type = RC_PROTO_BIT_RC5;
188 		init_data->name = itv->card_name;
189 		break;
190 	case IVTV_HW_Z8F0811_IR_HAUP:
191 		/* Default to grey remote */
192 		init_data->ir_codes = RC_MAP_HAUPPAUGE;
193 		init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
194 		init_data->type = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
195 							RC_PROTO_BIT_RC6_6A_32;
196 		init_data->name = itv->card_name;
197 		break;
198 	case IVTV_HW_I2C_IR_RX_ADAPTEC:
199 		init_data->get_key = get_key_adaptec;
200 		init_data->name = itv->card_name;
201 		/* FIXME: The protocol and RC_MAP needs to be corrected */
202 		init_data->ir_codes = RC_MAP_EMPTY;
203 		init_data->type = RC_PROTO_BIT_UNKNOWN;
204 		break;
205 	}
206 
207 	memset(&info, 0, sizeof(struct i2c_board_info));
208 	info.platform_data = init_data;
209 	strscpy(info.type, type, I2C_NAME_SIZE);
210 
211 	return IS_ERR(i2c_new_scanned_device(adap, &info, addr_list, NULL)) ?
212 	       -1 : 0;
213 }
214 
215 /* Instantiate the IR receiver device using probing -- undesirable */
216 void ivtv_i2c_new_ir_legacy(struct ivtv *itv)
217 {
218 	struct i2c_board_info info;
219 	/*
220 	 * The external IR receiver is at i2c address 0x34.
221 	 * The internal IR receiver is at i2c address 0x30.
222 	 *
223 	 * In theory, both can be fitted, and Hauppauge suggests an external
224 	 * overrides an internal.  That's why we probe 0x1a (~0x34) first. CB
225 	 *
226 	 * Some of these addresses we probe may collide with other i2c address
227 	 * allocations, so this function must be called after all other i2c
228 	 * devices we care about are registered.
229 	 */
230 	static const unsigned short addr_list[] = {
231 		0x1a,	/* Hauppauge IR external - collides with WM8739 */
232 		0x18,	/* Hauppauge IR internal */
233 		I2C_CLIENT_END
234 	};
235 
236 	memset(&info, 0, sizeof(struct i2c_board_info));
237 	strscpy(info.type, "ir_video", I2C_NAME_SIZE);
238 	i2c_new_scanned_device(&itv->i2c_adap, &info, addr_list, NULL);
239 }
240 
241 int ivtv_i2c_register(struct ivtv *itv, unsigned idx)
242 {
243 	struct i2c_adapter *adap = &itv->i2c_adap;
244 	struct v4l2_subdev *sd;
245 	const char *type;
246 	u32 hw;
247 
248 	if (idx >= IVTV_HW_MAX_BITS)
249 		return -ENODEV;
250 
251 	type = hw_devicenames[idx];
252 	hw = 1 << idx;
253 
254 	if (hw == IVTV_HW_TUNER) {
255 		/* special tuner handling */
256 		sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,
257 				itv->card_i2c->radio);
258 		if (sd)
259 			sd->grp_id = 1 << idx;
260 		sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,
261 				itv->card_i2c->demod);
262 		if (sd)
263 			sd->grp_id = 1 << idx;
264 		sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,
265 				itv->card_i2c->tv);
266 		if (sd)
267 			sd->grp_id = 1 << idx;
268 		return sd ? 0 : -1;
269 	}
270 
271 	if (hw & IVTV_HW_IR_ANY)
272 		return ivtv_i2c_new_ir(itv, hw, type, hw_addrs[idx]);
273 
274 	/* Is it not an I2C device or one we do not wish to register? */
275 	if (!hw_addrs[idx])
276 		return -1;
277 
278 	/* It's an I2C device other than an analog tuner or IR chip */
279 	if (hw == IVTV_HW_UPD64031A || hw == IVTV_HW_UPD6408X) {
280 		sd = v4l2_i2c_new_subdev(&itv->v4l2_dev,
281 				adap, type, 0, I2C_ADDRS(hw_addrs[idx]));
282 	} else if (hw == IVTV_HW_CX25840) {
283 		struct cx25840_platform_data pdata;
284 		struct i2c_board_info cx25840_info = {
285 			.type = "cx25840",
286 			.addr = hw_addrs[idx],
287 			.platform_data = &pdata,
288 		};
289 
290 		memset(&pdata, 0, sizeof(pdata));
291 		pdata.pvr150_workaround = itv->pvr150_workaround;
292 		sd = v4l2_i2c_new_subdev_board(&itv->v4l2_dev, adap,
293 				&cx25840_info, NULL);
294 	} else {
295 		sd = v4l2_i2c_new_subdev(&itv->v4l2_dev,
296 				adap, type, hw_addrs[idx], NULL);
297 	}
298 	if (sd)
299 		sd->grp_id = 1 << idx;
300 	return sd ? 0 : -1;
301 }
302 
303 struct v4l2_subdev *ivtv_find_hw(struct ivtv *itv, u32 hw)
304 {
305 	struct v4l2_subdev *result = NULL;
306 	struct v4l2_subdev *sd;
307 
308 	spin_lock(&itv->v4l2_dev.lock);
309 	v4l2_device_for_each_subdev(sd, &itv->v4l2_dev) {
310 		if (sd->grp_id == hw) {
311 			result = sd;
312 			break;
313 		}
314 	}
315 	spin_unlock(&itv->v4l2_dev.lock);
316 	return result;
317 }
318 
319 /* Set the serial clock line to the desired state */
320 static void ivtv_setscl(struct ivtv *itv, int state)
321 {
322 	/* write them out */
323 	/* write bits are inverted */
324 	write_reg(~state, IVTV_REG_I2C_SETSCL_OFFSET);
325 }
326 
327 /* Set the serial data line to the desired state */
328 static void ivtv_setsda(struct ivtv *itv, int state)
329 {
330 	/* write them out */
331 	/* write bits are inverted */
332 	write_reg(~state & 1, IVTV_REG_I2C_SETSDA_OFFSET);
333 }
334 
335 /* Read the serial clock line */
336 static int ivtv_getscl(struct ivtv *itv)
337 {
338 	return read_reg(IVTV_REG_I2C_GETSCL_OFFSET) & 1;
339 }
340 
341 /* Read the serial data line */
342 static int ivtv_getsda(struct ivtv *itv)
343 {
344 	return read_reg(IVTV_REG_I2C_GETSDA_OFFSET) & 1;
345 }
346 
347 /* Implement a short delay by polling the serial clock line */
348 static void ivtv_scldelay(struct ivtv *itv)
349 {
350 	int i;
351 
352 	for (i = 0; i < 5; ++i)
353 		ivtv_getscl(itv);
354 }
355 
356 /* Wait for the serial clock line to become set to a specific value */
357 static int ivtv_waitscl(struct ivtv *itv, int val)
358 {
359 	int i;
360 
361 	ivtv_scldelay(itv);
362 	for (i = 0; i < 1000; ++i) {
363 		if (ivtv_getscl(itv) == val)
364 			return 1;
365 	}
366 	return 0;
367 }
368 
369 /* Wait for the serial data line to become set to a specific value */
370 static int ivtv_waitsda(struct ivtv *itv, int val)
371 {
372 	int i;
373 
374 	ivtv_scldelay(itv);
375 	for (i = 0; i < 1000; ++i) {
376 		if (ivtv_getsda(itv) == val)
377 			return 1;
378 	}
379 	return 0;
380 }
381 
382 /* Wait for the slave to issue an ACK */
383 static int ivtv_ack(struct ivtv *itv)
384 {
385 	int ret = 0;
386 
387 	if (ivtv_getscl(itv) == 1) {
388 		IVTV_DEBUG_HI_I2C("SCL was high starting an ack\n");
389 		ivtv_setscl(itv, 0);
390 		if (!ivtv_waitscl(itv, 0)) {
391 			IVTV_DEBUG_I2C("Could not set SCL low starting an ack\n");
392 			return -EREMOTEIO;
393 		}
394 	}
395 	ivtv_setsda(itv, 1);
396 	ivtv_scldelay(itv);
397 	ivtv_setscl(itv, 1);
398 	if (!ivtv_waitsda(itv, 0)) {
399 		IVTV_DEBUG_I2C("Slave did not ack\n");
400 		ret = -EREMOTEIO;
401 	}
402 	ivtv_setscl(itv, 0);
403 	if (!ivtv_waitscl(itv, 0)) {
404 		IVTV_DEBUG_I2C("Failed to set SCL low after ACK\n");
405 		ret = -EREMOTEIO;
406 	}
407 	return ret;
408 }
409 
410 /* Write a single byte to the i2c bus and wait for the slave to ACK */
411 static int ivtv_sendbyte(struct ivtv *itv, unsigned char byte)
412 {
413 	int i, bit;
414 
415 	IVTV_DEBUG_HI_I2C("write %x\n",byte);
416 	for (i = 0; i < 8; ++i, byte<<=1) {
417 		ivtv_setscl(itv, 0);
418 		if (!ivtv_waitscl(itv, 0)) {
419 			IVTV_DEBUG_I2C("Error setting SCL low\n");
420 			return -EREMOTEIO;
421 		}
422 		bit = (byte>>7)&1;
423 		ivtv_setsda(itv, bit);
424 		if (!ivtv_waitsda(itv, bit)) {
425 			IVTV_DEBUG_I2C("Error setting SDA\n");
426 			return -EREMOTEIO;
427 		}
428 		ivtv_setscl(itv, 1);
429 		if (!ivtv_waitscl(itv, 1)) {
430 			IVTV_DEBUG_I2C("Slave not ready for bit\n");
431 			return -EREMOTEIO;
432 		}
433 	}
434 	ivtv_setscl(itv, 0);
435 	if (!ivtv_waitscl(itv, 0)) {
436 		IVTV_DEBUG_I2C("Error setting SCL low\n");
437 		return -EREMOTEIO;
438 	}
439 	return ivtv_ack(itv);
440 }
441 
442 /* Read a byte from the i2c bus and send a NACK if applicable (i.e. for the
443    final byte) */
444 static int ivtv_readbyte(struct ivtv *itv, unsigned char *byte, int nack)
445 {
446 	int i;
447 
448 	*byte = 0;
449 
450 	ivtv_setsda(itv, 1);
451 	ivtv_scldelay(itv);
452 	for (i = 0; i < 8; ++i) {
453 		ivtv_setscl(itv, 0);
454 		ivtv_scldelay(itv);
455 		ivtv_setscl(itv, 1);
456 		if (!ivtv_waitscl(itv, 1)) {
457 			IVTV_DEBUG_I2C("Error setting SCL high\n");
458 			return -EREMOTEIO;
459 		}
460 		*byte = ((*byte)<<1)|ivtv_getsda(itv);
461 	}
462 	ivtv_setscl(itv, 0);
463 	ivtv_scldelay(itv);
464 	ivtv_setsda(itv, nack);
465 	ivtv_scldelay(itv);
466 	ivtv_setscl(itv, 1);
467 	ivtv_scldelay(itv);
468 	ivtv_setscl(itv, 0);
469 	ivtv_scldelay(itv);
470 	IVTV_DEBUG_HI_I2C("read %x\n",*byte);
471 	return 0;
472 }
473 
474 /* Issue a start condition on the i2c bus to alert slaves to prepare for
475    an address write */
476 static int ivtv_start(struct ivtv *itv)
477 {
478 	int sda;
479 
480 	sda = ivtv_getsda(itv);
481 	if (sda != 1) {
482 		IVTV_DEBUG_HI_I2C("SDA was low at start\n");
483 		ivtv_setsda(itv, 1);
484 		if (!ivtv_waitsda(itv, 1)) {
485 			IVTV_DEBUG_I2C("SDA stuck low\n");
486 			return -EREMOTEIO;
487 		}
488 	}
489 	if (ivtv_getscl(itv) != 1) {
490 		ivtv_setscl(itv, 1);
491 		if (!ivtv_waitscl(itv, 1)) {
492 			IVTV_DEBUG_I2C("SCL stuck low at start\n");
493 			return -EREMOTEIO;
494 		}
495 	}
496 	ivtv_setsda(itv, 0);
497 	ivtv_scldelay(itv);
498 	return 0;
499 }
500 
501 /* Issue a stop condition on the i2c bus to release it */
502 static int ivtv_stop(struct ivtv *itv)
503 {
504 	int i;
505 
506 	if (ivtv_getscl(itv) != 0) {
507 		IVTV_DEBUG_HI_I2C("SCL not low when stopping\n");
508 		ivtv_setscl(itv, 0);
509 		if (!ivtv_waitscl(itv, 0)) {
510 			IVTV_DEBUG_I2C("SCL could not be set low\n");
511 		}
512 	}
513 	ivtv_setsda(itv, 0);
514 	ivtv_scldelay(itv);
515 	ivtv_setscl(itv, 1);
516 	if (!ivtv_waitscl(itv, 1)) {
517 		IVTV_DEBUG_I2C("SCL could not be set high\n");
518 		return -EREMOTEIO;
519 	}
520 	ivtv_scldelay(itv);
521 	ivtv_setsda(itv, 1);
522 	if (!ivtv_waitsda(itv, 1)) {
523 		IVTV_DEBUG_I2C("resetting I2C\n");
524 		for (i = 0; i < 16; ++i) {
525 			ivtv_setscl(itv, 0);
526 			ivtv_scldelay(itv);
527 			ivtv_setscl(itv, 1);
528 			ivtv_scldelay(itv);
529 			ivtv_setsda(itv, 1);
530 		}
531 		ivtv_waitsda(itv, 1);
532 		return -EREMOTEIO;
533 	}
534 	return 0;
535 }
536 
537 /* Write a message to the given i2c slave.  do_stop may be 0 to prevent
538    issuing the i2c stop condition (when following with a read) */
539 static int ivtv_write(struct ivtv *itv, unsigned char addr, unsigned char *data, u32 len, int do_stop)
540 {
541 	int retry, ret = -EREMOTEIO;
542 	u32 i;
543 
544 	for (retry = 0; ret != 0 && retry < 8; ++retry) {
545 		ret = ivtv_start(itv);
546 
547 		if (ret == 0) {
548 			ret = ivtv_sendbyte(itv, addr<<1);
549 			for (i = 0; ret == 0 && i < len; ++i)
550 				ret = ivtv_sendbyte(itv, data[i]);
551 		}
552 		if (ret != 0 || do_stop) {
553 			ivtv_stop(itv);
554 		}
555 	}
556 	if (ret)
557 		IVTV_DEBUG_I2C("i2c write to %x failed\n", addr);
558 	return ret;
559 }
560 
561 /* Read data from the given i2c slave.  A stop condition is always issued. */
562 static int ivtv_read(struct ivtv *itv, unsigned char addr, unsigned char *data, u32 len)
563 {
564 	int retry, ret = -EREMOTEIO;
565 	u32 i;
566 
567 	for (retry = 0; ret != 0 && retry < 8; ++retry) {
568 		ret = ivtv_start(itv);
569 		if (ret == 0)
570 			ret = ivtv_sendbyte(itv, (addr << 1) | 1);
571 		for (i = 0; ret == 0 && i < len; ++i) {
572 			ret = ivtv_readbyte(itv, &data[i], i == len - 1);
573 		}
574 		ivtv_stop(itv);
575 	}
576 	if (ret)
577 		IVTV_DEBUG_I2C("i2c read from %x failed\n", addr);
578 	return ret;
579 }
580 
581 /* Kernel i2c transfer implementation.  Takes a number of messages to be read
582    or written.  If a read follows a write, this will occur without an
583    intervening stop condition */
584 static int ivtv_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
585 {
586 	struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);
587 	struct ivtv *itv = to_ivtv(v4l2_dev);
588 	int retval;
589 	int i;
590 
591 	mutex_lock(&itv->i2c_bus_lock);
592 	for (i = retval = 0; retval == 0 && i < num; i++) {
593 		if (msgs[i].flags & I2C_M_RD)
594 			retval = ivtv_read(itv, msgs[i].addr, msgs[i].buf, msgs[i].len);
595 		else {
596 			/* if followed by a read, don't stop */
597 			int stop = !(i + 1 < num && msgs[i + 1].flags == I2C_M_RD);
598 
599 			retval = ivtv_write(itv, msgs[i].addr, msgs[i].buf, msgs[i].len, stop);
600 		}
601 	}
602 	mutex_unlock(&itv->i2c_bus_lock);
603 	return retval ? retval : num;
604 }
605 
606 /* Kernel i2c capabilities */
607 static u32 ivtv_functionality(struct i2c_adapter *adap)
608 {
609 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
610 }
611 
612 static const struct i2c_algorithm ivtv_algo = {
613 	.master_xfer   = ivtv_xfer,
614 	.functionality = ivtv_functionality,
615 };
616 
617 /* template for our-bit banger */
618 static const struct i2c_adapter ivtv_i2c_adap_hw_template = {
619 	.name = "ivtv i2c driver",
620 	.algo = &ivtv_algo,
621 	.algo_data = NULL,			/* filled from template */
622 	.owner = THIS_MODULE,
623 };
624 
625 static void ivtv_setscl_old(void *data, int state)
626 {
627 	struct ivtv *itv = (struct ivtv *)data;
628 
629 	if (state)
630 		itv->i2c_state |= 0x01;
631 	else
632 		itv->i2c_state &= ~0x01;
633 
634 	/* write them out */
635 	/* write bits are inverted */
636 	write_reg(~itv->i2c_state, IVTV_REG_I2C_SETSCL_OFFSET);
637 }
638 
639 static void ivtv_setsda_old(void *data, int state)
640 {
641 	struct ivtv *itv = (struct ivtv *)data;
642 
643 	if (state)
644 		itv->i2c_state |= 0x01;
645 	else
646 		itv->i2c_state &= ~0x01;
647 
648 	/* write them out */
649 	/* write bits are inverted */
650 	write_reg(~itv->i2c_state, IVTV_REG_I2C_SETSDA_OFFSET);
651 }
652 
653 static int ivtv_getscl_old(void *data)
654 {
655 	struct ivtv *itv = (struct ivtv *)data;
656 
657 	return read_reg(IVTV_REG_I2C_GETSCL_OFFSET) & 1;
658 }
659 
660 static int ivtv_getsda_old(void *data)
661 {
662 	struct ivtv *itv = (struct ivtv *)data;
663 
664 	return read_reg(IVTV_REG_I2C_GETSDA_OFFSET) & 1;
665 }
666 
667 /* template for i2c-bit-algo */
668 static const struct i2c_adapter ivtv_i2c_adap_template = {
669 	.name = "ivtv i2c driver",
670 	.algo = NULL,                   /* set by i2c-algo-bit */
671 	.algo_data = NULL,              /* filled from template */
672 	.owner = THIS_MODULE,
673 };
674 
675 #define IVTV_ALGO_BIT_TIMEOUT	(2)	/* seconds */
676 
677 static const struct i2c_algo_bit_data ivtv_i2c_algo_template = {
678 	.setsda		= ivtv_setsda_old,
679 	.setscl		= ivtv_setscl_old,
680 	.getsda		= ivtv_getsda_old,
681 	.getscl		= ivtv_getscl_old,
682 	.udelay		= IVTV_DEFAULT_I2C_CLOCK_PERIOD / 2,  /* microseconds */
683 	.timeout	= IVTV_ALGO_BIT_TIMEOUT * HZ,         /* jiffies */
684 };
685 
686 static const struct i2c_client ivtv_i2c_client_template = {
687 	.name = "ivtv internal",
688 };
689 
690 /* init + register i2c adapter */
691 int init_ivtv_i2c(struct ivtv *itv)
692 {
693 	int retval;
694 
695 	IVTV_DEBUG_I2C("i2c init\n");
696 
697 	/* Sanity checks for the I2C hardware arrays. They must be the
698 	 * same size.
699 	 */
700 	if (ARRAY_SIZE(hw_devicenames) != ARRAY_SIZE(hw_addrs)) {
701 		IVTV_ERR("Mismatched I2C hardware arrays\n");
702 		return -ENODEV;
703 	}
704 	if (itv->options.newi2c > 0) {
705 		itv->i2c_adap = ivtv_i2c_adap_hw_template;
706 	} else {
707 		itv->i2c_adap = ivtv_i2c_adap_template;
708 		itv->i2c_algo = ivtv_i2c_algo_template;
709 	}
710 	itv->i2c_algo.udelay = itv->options.i2c_clock_period / 2;
711 	itv->i2c_algo.data = itv;
712 	itv->i2c_adap.algo_data = &itv->i2c_algo;
713 
714 	sprintf(itv->i2c_adap.name + strlen(itv->i2c_adap.name), " #%d",
715 		itv->instance);
716 	i2c_set_adapdata(&itv->i2c_adap, &itv->v4l2_dev);
717 
718 	itv->i2c_client = ivtv_i2c_client_template;
719 	itv->i2c_client.adapter = &itv->i2c_adap;
720 	itv->i2c_adap.dev.parent = &itv->pdev->dev;
721 
722 	IVTV_DEBUG_I2C("setting scl and sda to 1\n");
723 	ivtv_setscl(itv, 1);
724 	ivtv_setsda(itv, 1);
725 
726 	if (itv->options.newi2c > 0)
727 		retval = i2c_add_adapter(&itv->i2c_adap);
728 	else
729 		retval = i2c_bit_add_bus(&itv->i2c_adap);
730 
731 	return retval;
732 }
733 
734 void exit_ivtv_i2c(struct ivtv *itv)
735 {
736 	IVTV_DEBUG_I2C("i2c exit\n");
737 
738 	i2c_del_adapter(&itv->i2c_adap);
739 }
740