xref: /OK3568_Linux_fs/kernel/drivers/media/common/saa7146/saa7146_i2c.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include <media/drv-intf/saa7146_vv.h>
5*4882a593Smuzhiyun 
saa7146_i2c_func(struct i2c_adapter * adapter)6*4882a593Smuzhiyun static u32 saa7146_i2c_func(struct i2c_adapter *adapter)
7*4882a593Smuzhiyun {
8*4882a593Smuzhiyun 	/* DEB_I2C("'%s'\n", adapter->name); */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun 	return	  I2C_FUNC_I2C
11*4882a593Smuzhiyun 		| I2C_FUNC_SMBUS_QUICK
12*4882a593Smuzhiyun 		| I2C_FUNC_SMBUS_READ_BYTE	| I2C_FUNC_SMBUS_WRITE_BYTE
13*4882a593Smuzhiyun 		| I2C_FUNC_SMBUS_READ_BYTE_DATA | I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
14*4882a593Smuzhiyun }
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /* this function returns the status-register of our i2c-device */
saa7146_i2c_status(struct saa7146_dev * dev)17*4882a593Smuzhiyun static inline u32 saa7146_i2c_status(struct saa7146_dev *dev)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun 	u32 iicsta = saa7146_read(dev, I2C_STATUS);
20*4882a593Smuzhiyun 	/* DEB_I2C("status: 0x%08x\n", iicsta); */
21*4882a593Smuzhiyun 	return iicsta;
22*4882a593Smuzhiyun }
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /* this function runs through the i2c-messages and prepares the data to be
25*4882a593Smuzhiyun    sent through the saa7146. have a look at the specifications p. 122 ff
26*4882a593Smuzhiyun    to understand this. it returns the number of u32s to send, or -1
27*4882a593Smuzhiyun    in case of an error. */
saa7146_i2c_msg_prepare(const struct i2c_msg * m,int num,__le32 * op)28*4882a593Smuzhiyun static int saa7146_i2c_msg_prepare(const struct i2c_msg *m, int num, __le32 *op)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	int h1, h2;
31*4882a593Smuzhiyun 	int i, j, addr;
32*4882a593Smuzhiyun 	int mem = 0, op_count = 0;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	/* first determine size of needed memory */
35*4882a593Smuzhiyun 	for(i = 0; i < num; i++) {
36*4882a593Smuzhiyun 		mem += m[i].len + 1;
37*4882a593Smuzhiyun 	}
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	/* worst case: we need one u32 for three bytes to be send
40*4882a593Smuzhiyun 	   plus one extra byte to address the device */
41*4882a593Smuzhiyun 	mem = 1 + ((mem-1) / 3);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	/* we assume that op points to a memory of at least
44*4882a593Smuzhiyun 	 * SAA7146_I2C_MEM bytes size. if we exceed this limit...
45*4882a593Smuzhiyun 	 */
46*4882a593Smuzhiyun 	if ((4 * mem) > SAA7146_I2C_MEM) {
47*4882a593Smuzhiyun 		/* DEB_I2C("cannot prepare i2c-message\n"); */
48*4882a593Smuzhiyun 		return -ENOMEM;
49*4882a593Smuzhiyun 	}
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	/* be careful: clear out the i2c-mem first */
52*4882a593Smuzhiyun 	memset(op,0,sizeof(__le32)*mem);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/* loop through all messages */
55*4882a593Smuzhiyun 	for(i = 0; i < num; i++) {
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 		addr = i2c_8bit_addr_from_msg(&m[i]);
58*4882a593Smuzhiyun 		h1 = op_count/3; h2 = op_count%3;
59*4882a593Smuzhiyun 		op[h1] |= cpu_to_le32(	    (u8)addr << ((3-h2)*8));
60*4882a593Smuzhiyun 		op[h1] |= cpu_to_le32(SAA7146_I2C_START << ((3-h2)*2));
61*4882a593Smuzhiyun 		op_count++;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 		/* loop through all bytes of message i */
64*4882a593Smuzhiyun 		for(j = 0; j < m[i].len; j++) {
65*4882a593Smuzhiyun 			/* insert the data bytes */
66*4882a593Smuzhiyun 			h1 = op_count/3; h2 = op_count%3;
67*4882a593Smuzhiyun 			op[h1] |= cpu_to_le32( (u32)((u8)m[i].buf[j]) << ((3-h2)*8));
68*4882a593Smuzhiyun 			op[h1] |= cpu_to_le32(       SAA7146_I2C_CONT << ((3-h2)*2));
69*4882a593Smuzhiyun 			op_count++;
70*4882a593Smuzhiyun 		}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/* have a look at the last byte inserted:
75*4882a593Smuzhiyun 	  if it was: ...CONT change it to ...STOP */
76*4882a593Smuzhiyun 	h1 = (op_count-1)/3; h2 = (op_count-1)%3;
77*4882a593Smuzhiyun 	if ( SAA7146_I2C_CONT == (0x3 & (le32_to_cpu(op[h1]) >> ((3-h2)*2))) ) {
78*4882a593Smuzhiyun 		op[h1] &= ~cpu_to_le32(0x2 << ((3-h2)*2));
79*4882a593Smuzhiyun 		op[h1] |= cpu_to_le32(SAA7146_I2C_STOP << ((3-h2)*2));
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* return the number of u32s to send */
83*4882a593Smuzhiyun 	return mem;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* this functions loops through all i2c-messages. normally, it should determine
87*4882a593Smuzhiyun    which bytes were read through the adapter and write them back to the corresponding
88*4882a593Smuzhiyun    i2c-message. but instead, we simply write back all bytes.
89*4882a593Smuzhiyun    fixme: this could be improved. */
saa7146_i2c_msg_cleanup(const struct i2c_msg * m,int num,__le32 * op)90*4882a593Smuzhiyun static int saa7146_i2c_msg_cleanup(const struct i2c_msg *m, int num, __le32 *op)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	int i, j;
93*4882a593Smuzhiyun 	int op_count = 0;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* loop through all messages */
96*4882a593Smuzhiyun 	for(i = 0; i < num; i++) {
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 		op_count++;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 		/* loop through all bytes of message i */
101*4882a593Smuzhiyun 		for(j = 0; j < m[i].len; j++) {
102*4882a593Smuzhiyun 			/* write back all bytes that could have been read */
103*4882a593Smuzhiyun 			m[i].buf[j] = (le32_to_cpu(op[op_count/3]) >> ((3-(op_count%3))*8));
104*4882a593Smuzhiyun 			op_count++;
105*4882a593Smuzhiyun 		}
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	return 0;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /* this functions resets the i2c-device and returns 0 if everything was fine, otherwise -1 */
saa7146_i2c_reset(struct saa7146_dev * dev)112*4882a593Smuzhiyun static int saa7146_i2c_reset(struct saa7146_dev *dev)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	/* get current status */
115*4882a593Smuzhiyun 	u32 status = saa7146_i2c_status(dev);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/* clear registers for sure */
118*4882a593Smuzhiyun 	saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
119*4882a593Smuzhiyun 	saa7146_write(dev, I2C_TRANSFER, 0);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* check if any operation is still in progress */
122*4882a593Smuzhiyun 	if ( 0 != ( status & SAA7146_I2C_BUSY) ) {
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 		/* yes, kill ongoing operation */
125*4882a593Smuzhiyun 		DEB_I2C("busy_state detected\n");
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		/* set "ABORT-OPERATION"-bit (bit 7)*/
128*4882a593Smuzhiyun 		saa7146_write(dev, I2C_STATUS, (dev->i2c_bitrate | MASK_07));
129*4882a593Smuzhiyun 		saa7146_write(dev, MC2, (MASK_00 | MASK_16));
130*4882a593Smuzhiyun 		msleep(SAA7146_I2C_DELAY);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		/* clear all error-bits pending; this is needed because p.123, note 1 */
133*4882a593Smuzhiyun 		saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
134*4882a593Smuzhiyun 		saa7146_write(dev, MC2, (MASK_00 | MASK_16));
135*4882a593Smuzhiyun 		msleep(SAA7146_I2C_DELAY);
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* check if any error is (still) present. (this can be necessary because p.123, note 1) */
139*4882a593Smuzhiyun 	status = saa7146_i2c_status(dev);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	if ( dev->i2c_bitrate != status ) {
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		DEB_I2C("error_state detected. status:0x%08x\n", status);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		/* Repeat the abort operation. This seems to be necessary
146*4882a593Smuzhiyun 		   after serious protocol errors caused by e.g. the SAA7740 */
147*4882a593Smuzhiyun 		saa7146_write(dev, I2C_STATUS, (dev->i2c_bitrate | MASK_07));
148*4882a593Smuzhiyun 		saa7146_write(dev, MC2, (MASK_00 | MASK_16));
149*4882a593Smuzhiyun 		msleep(SAA7146_I2C_DELAY);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 		/* clear all error-bits pending */
152*4882a593Smuzhiyun 		saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
153*4882a593Smuzhiyun 		saa7146_write(dev, MC2, (MASK_00 | MASK_16));
154*4882a593Smuzhiyun 		msleep(SAA7146_I2C_DELAY);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 		/* the data sheet says it might be necessary to clear the status
157*4882a593Smuzhiyun 		   twice after an abort */
158*4882a593Smuzhiyun 		saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
159*4882a593Smuzhiyun 		saa7146_write(dev, MC2, (MASK_00 | MASK_16));
160*4882a593Smuzhiyun 		msleep(SAA7146_I2C_DELAY);
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/* if any error is still present, a fatal error has occurred ... */
164*4882a593Smuzhiyun 	status = saa7146_i2c_status(dev);
165*4882a593Smuzhiyun 	if ( dev->i2c_bitrate != status ) {
166*4882a593Smuzhiyun 		DEB_I2C("fatal error. status:0x%08x\n", status);
167*4882a593Smuzhiyun 		return -1;
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	return 0;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /* this functions writes out the data-byte 'dword' to the i2c-device.
174*4882a593Smuzhiyun    it returns 0 if ok, -1 if the transfer failed, -2 if the transfer
175*4882a593Smuzhiyun    failed badly (e.g. address error) */
saa7146_i2c_writeout(struct saa7146_dev * dev,__le32 * dword,int short_delay)176*4882a593Smuzhiyun static int saa7146_i2c_writeout(struct saa7146_dev *dev, __le32 *dword, int short_delay)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	u32 status = 0, mc2 = 0;
179*4882a593Smuzhiyun 	int trial = 0;
180*4882a593Smuzhiyun 	unsigned long timeout;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/* write out i2c-command */
183*4882a593Smuzhiyun 	DEB_I2C("before: 0x%08x (status: 0x%08x), %d\n",
184*4882a593Smuzhiyun 		*dword, saa7146_read(dev, I2C_STATUS), dev->i2c_op);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	if( 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags)) {
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 		saa7146_write(dev, I2C_STATUS,	 dev->i2c_bitrate);
189*4882a593Smuzhiyun 		saa7146_write(dev, I2C_TRANSFER, le32_to_cpu(*dword));
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 		dev->i2c_op = 1;
192*4882a593Smuzhiyun 		SAA7146_ISR_CLEAR(dev, MASK_16|MASK_17);
193*4882a593Smuzhiyun 		SAA7146_IER_ENABLE(dev, MASK_16|MASK_17);
194*4882a593Smuzhiyun 		saa7146_write(dev, MC2, (MASK_00 | MASK_16));
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		timeout = HZ/100 + 1; /* 10ms */
197*4882a593Smuzhiyun 		timeout = wait_event_interruptible_timeout(dev->i2c_wq, dev->i2c_op == 0, timeout);
198*4882a593Smuzhiyun 		if (timeout == -ERESTARTSYS || dev->i2c_op) {
199*4882a593Smuzhiyun 			SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
200*4882a593Smuzhiyun 			SAA7146_ISR_CLEAR(dev, MASK_16|MASK_17);
201*4882a593Smuzhiyun 			if (timeout == -ERESTARTSYS)
202*4882a593Smuzhiyun 				/* a signal arrived */
203*4882a593Smuzhiyun 				return -ERESTARTSYS;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 			pr_warn("%s %s [irq]: timed out waiting for end of xfer\n",
206*4882a593Smuzhiyun 				dev->name, __func__);
207*4882a593Smuzhiyun 			return -EIO;
208*4882a593Smuzhiyun 		}
209*4882a593Smuzhiyun 		status = saa7146_read(dev, I2C_STATUS);
210*4882a593Smuzhiyun 	} else {
211*4882a593Smuzhiyun 		saa7146_write(dev, I2C_STATUS,	 dev->i2c_bitrate);
212*4882a593Smuzhiyun 		saa7146_write(dev, I2C_TRANSFER, le32_to_cpu(*dword));
213*4882a593Smuzhiyun 		saa7146_write(dev, MC2, (MASK_00 | MASK_16));
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 		/* do not poll for i2c-status before upload is complete */
216*4882a593Smuzhiyun 		timeout = jiffies + HZ/100 + 1; /* 10ms */
217*4882a593Smuzhiyun 		while(1) {
218*4882a593Smuzhiyun 			mc2 = (saa7146_read(dev, MC2) & 0x1);
219*4882a593Smuzhiyun 			if( 0 != mc2 ) {
220*4882a593Smuzhiyun 				break;
221*4882a593Smuzhiyun 			}
222*4882a593Smuzhiyun 			if (time_after(jiffies,timeout)) {
223*4882a593Smuzhiyun 				pr_warn("%s %s: timed out waiting for MC2\n",
224*4882a593Smuzhiyun 					dev->name, __func__);
225*4882a593Smuzhiyun 				return -EIO;
226*4882a593Smuzhiyun 			}
227*4882a593Smuzhiyun 		}
228*4882a593Smuzhiyun 		/* wait until we get a transfer done or error */
229*4882a593Smuzhiyun 		timeout = jiffies + HZ/100 + 1; /* 10ms */
230*4882a593Smuzhiyun 		/* first read usually delivers bogus results... */
231*4882a593Smuzhiyun 		saa7146_i2c_status(dev);
232*4882a593Smuzhiyun 		while(1) {
233*4882a593Smuzhiyun 			status = saa7146_i2c_status(dev);
234*4882a593Smuzhiyun 			if ((status & 0x3) != 1)
235*4882a593Smuzhiyun 				break;
236*4882a593Smuzhiyun 			if (time_after(jiffies,timeout)) {
237*4882a593Smuzhiyun 				/* this is normal when probing the bus
238*4882a593Smuzhiyun 				 * (no answer from nonexisistant device...)
239*4882a593Smuzhiyun 				 */
240*4882a593Smuzhiyun 				pr_warn("%s %s [poll]: timed out waiting for end of xfer\n",
241*4882a593Smuzhiyun 					dev->name, __func__);
242*4882a593Smuzhiyun 				return -EIO;
243*4882a593Smuzhiyun 			}
244*4882a593Smuzhiyun 			if (++trial < 50 && short_delay)
245*4882a593Smuzhiyun 				udelay(10);
246*4882a593Smuzhiyun 			else
247*4882a593Smuzhiyun 				msleep(1);
248*4882a593Smuzhiyun 		}
249*4882a593Smuzhiyun 	}
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* give a detailed status report */
252*4882a593Smuzhiyun 	if ( 0 != (status & (SAA7146_I2C_SPERR | SAA7146_I2C_APERR |
253*4882a593Smuzhiyun 			     SAA7146_I2C_DTERR | SAA7146_I2C_DRERR |
254*4882a593Smuzhiyun 			     SAA7146_I2C_AL    | SAA7146_I2C_ERR   |
255*4882a593Smuzhiyun 			     SAA7146_I2C_BUSY)) ) {
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 		if ( 0 == (status & SAA7146_I2C_ERR) ||
258*4882a593Smuzhiyun 		     0 == (status & SAA7146_I2C_BUSY) ) {
259*4882a593Smuzhiyun 			/* it may take some time until ERR goes high - ignore */
260*4882a593Smuzhiyun 			DEB_I2C("unexpected i2c status %04x\n", status);
261*4882a593Smuzhiyun 		}
262*4882a593Smuzhiyun 		if( 0 != (status & SAA7146_I2C_SPERR) ) {
263*4882a593Smuzhiyun 			DEB_I2C("error due to invalid start/stop condition\n");
264*4882a593Smuzhiyun 		}
265*4882a593Smuzhiyun 		if( 0 != (status & SAA7146_I2C_DTERR) ) {
266*4882a593Smuzhiyun 			DEB_I2C("error in data transmission\n");
267*4882a593Smuzhiyun 		}
268*4882a593Smuzhiyun 		if( 0 != (status & SAA7146_I2C_DRERR) ) {
269*4882a593Smuzhiyun 			DEB_I2C("error when receiving data\n");
270*4882a593Smuzhiyun 		}
271*4882a593Smuzhiyun 		if( 0 != (status & SAA7146_I2C_AL) ) {
272*4882a593Smuzhiyun 			DEB_I2C("error because arbitration lost\n");
273*4882a593Smuzhiyun 		}
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 		/* we handle address-errors here */
276*4882a593Smuzhiyun 		if( 0 != (status & SAA7146_I2C_APERR) ) {
277*4882a593Smuzhiyun 			DEB_I2C("error in address phase\n");
278*4882a593Smuzhiyun 			return -EREMOTEIO;
279*4882a593Smuzhiyun 		}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 		return -EIO;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	/* read back data, just in case we were reading ... */
285*4882a593Smuzhiyun 	*dword = cpu_to_le32(saa7146_read(dev, I2C_TRANSFER));
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	DEB_I2C("after: 0x%08x\n", *dword);
288*4882a593Smuzhiyun 	return 0;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
saa7146_i2c_transfer(struct saa7146_dev * dev,const struct i2c_msg * msgs,int num,int retries)291*4882a593Smuzhiyun static int saa7146_i2c_transfer(struct saa7146_dev *dev, const struct i2c_msg *msgs, int num, int retries)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	int i = 0, count = 0;
294*4882a593Smuzhiyun 	__le32 *buffer = dev->d_i2c.cpu_addr;
295*4882a593Smuzhiyun 	int err = 0;
296*4882a593Smuzhiyun 	int short_delay = 0;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (mutex_lock_interruptible(&dev->i2c_lock))
299*4882a593Smuzhiyun 		return -ERESTARTSYS;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	for(i=0;i<num;i++) {
302*4882a593Smuzhiyun 		DEB_I2C("msg:%d/%d\n", i+1, num);
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	/* prepare the message(s), get number of u32s to transfer */
306*4882a593Smuzhiyun 	count = saa7146_i2c_msg_prepare(msgs, num, buffer);
307*4882a593Smuzhiyun 	if ( 0 > count ) {
308*4882a593Smuzhiyun 		err = -EIO;
309*4882a593Smuzhiyun 		goto out;
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	if ( count > 3 || 0 != (SAA7146_I2C_SHORT_DELAY & dev->ext->flags) )
313*4882a593Smuzhiyun 		short_delay = 1;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	do {
316*4882a593Smuzhiyun 		/* reset the i2c-device if necessary */
317*4882a593Smuzhiyun 		err = saa7146_i2c_reset(dev);
318*4882a593Smuzhiyun 		if ( 0 > err ) {
319*4882a593Smuzhiyun 			DEB_I2C("could not reset i2c-device\n");
320*4882a593Smuzhiyun 			goto out;
321*4882a593Smuzhiyun 		}
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 		/* write out the u32s one after another */
324*4882a593Smuzhiyun 		for(i = 0; i < count; i++) {
325*4882a593Smuzhiyun 			err = saa7146_i2c_writeout(dev, &buffer[i], short_delay);
326*4882a593Smuzhiyun 			if ( 0 != err) {
327*4882a593Smuzhiyun 				/* this one is unsatisfying: some i2c slaves on some
328*4882a593Smuzhiyun 				   dvb cards don't acknowledge correctly, so the saa7146
329*4882a593Smuzhiyun 				   thinks that an address error occurred. in that case, the
330*4882a593Smuzhiyun 				   transaction should be retrying, even if an address error
331*4882a593Smuzhiyun 				   occurred. analog saa7146 based cards extensively rely on
332*4882a593Smuzhiyun 				   i2c address probing, however, and address errors indicate that a
333*4882a593Smuzhiyun 				   device is really *not* there. retrying in that case
334*4882a593Smuzhiyun 				   increases the time the device needs to probe greatly, so
335*4882a593Smuzhiyun 				   it should be avoided. So we bail out in irq mode after an
336*4882a593Smuzhiyun 				   address error and trust the saa7146 address error detection. */
337*4882a593Smuzhiyun 				if (-EREMOTEIO == err && 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags))
338*4882a593Smuzhiyun 					goto out;
339*4882a593Smuzhiyun 				DEB_I2C("error while sending message(s). starting again\n");
340*4882a593Smuzhiyun 				break;
341*4882a593Smuzhiyun 			}
342*4882a593Smuzhiyun 		}
343*4882a593Smuzhiyun 		if( 0 == err ) {
344*4882a593Smuzhiyun 			err = num;
345*4882a593Smuzhiyun 			break;
346*4882a593Smuzhiyun 		}
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 		/* delay a bit before retrying */
349*4882a593Smuzhiyun 		msleep(10);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	} while (err != num && retries--);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	/* quit if any error occurred */
354*4882a593Smuzhiyun 	if (err != num)
355*4882a593Smuzhiyun 		goto out;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	/* if any things had to be read, get the results */
358*4882a593Smuzhiyun 	if ( 0 != saa7146_i2c_msg_cleanup(msgs, num, buffer)) {
359*4882a593Smuzhiyun 		DEB_I2C("could not cleanup i2c-message\n");
360*4882a593Smuzhiyun 		err = -EIO;
361*4882a593Smuzhiyun 		goto out;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	/* return the number of delivered messages */
365*4882a593Smuzhiyun 	DEB_I2C("transmission successful. (msg:%d)\n", err);
366*4882a593Smuzhiyun out:
367*4882a593Smuzhiyun 	/* another bug in revision 0: the i2c-registers get uploaded randomly by other
368*4882a593Smuzhiyun 	   uploads, so we better clear them out before continuing */
369*4882a593Smuzhiyun 	if( 0 == dev->revision ) {
370*4882a593Smuzhiyun 		__le32 zero = 0;
371*4882a593Smuzhiyun 		saa7146_i2c_reset(dev);
372*4882a593Smuzhiyun 		if( 0 != saa7146_i2c_writeout(dev, &zero, short_delay)) {
373*4882a593Smuzhiyun 			pr_info("revision 0 error. this should never happen\n");
374*4882a593Smuzhiyun 		}
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	mutex_unlock(&dev->i2c_lock);
378*4882a593Smuzhiyun 	return err;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun /* utility functions */
saa7146_i2c_xfer(struct i2c_adapter * adapter,struct i2c_msg * msg,int num)382*4882a593Smuzhiyun static int saa7146_i2c_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
385*4882a593Smuzhiyun 	struct saa7146_dev *dev = to_saa7146_dev(v4l2_dev);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* use helper function to transfer data */
388*4882a593Smuzhiyun 	return saa7146_i2c_transfer(dev, msg, num, adapter->retries);
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun /*****************************************************************************/
393*4882a593Smuzhiyun /* i2c-adapter helper functions                                              */
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun /* exported algorithm data */
396*4882a593Smuzhiyun static const struct i2c_algorithm saa7146_algo = {
397*4882a593Smuzhiyun 	.master_xfer	= saa7146_i2c_xfer,
398*4882a593Smuzhiyun 	.functionality	= saa7146_i2c_func,
399*4882a593Smuzhiyun };
400*4882a593Smuzhiyun 
saa7146_i2c_adapter_prepare(struct saa7146_dev * dev,struct i2c_adapter * i2c_adapter,u32 bitrate)401*4882a593Smuzhiyun int saa7146_i2c_adapter_prepare(struct saa7146_dev *dev, struct i2c_adapter *i2c_adapter, u32 bitrate)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	DEB_EE("bitrate: 0x%08x\n", bitrate);
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	/* enable i2c-port pins */
406*4882a593Smuzhiyun 	saa7146_write(dev, MC1, (MASK_08 | MASK_24));
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	dev->i2c_bitrate = bitrate;
409*4882a593Smuzhiyun 	saa7146_i2c_reset(dev);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	if (i2c_adapter) {
412*4882a593Smuzhiyun 		i2c_set_adapdata(i2c_adapter, &dev->v4l2_dev);
413*4882a593Smuzhiyun 		i2c_adapter->dev.parent    = &dev->pci->dev;
414*4882a593Smuzhiyun 		i2c_adapter->algo	   = &saa7146_algo;
415*4882a593Smuzhiyun 		i2c_adapter->algo_data     = NULL;
416*4882a593Smuzhiyun 		i2c_adapter->timeout = SAA7146_I2C_TIMEOUT;
417*4882a593Smuzhiyun 		i2c_adapter->retries = SAA7146_I2C_RETRIES;
418*4882a593Smuzhiyun 	}
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	return 0;
421*4882a593Smuzhiyun }
422