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