blob: 5297a365c928e2b8c8928c8f3edafc7324db1915 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <media/saa7146_vv.h>
2
3static u32 saa7146_i2c_func(struct i2c_adapter *adapter)
4{
5//fm DEB_I2C(("'%s'.\n", adapter->name));
6
7 return I2C_FUNC_I2C
8 | I2C_FUNC_SMBUS_QUICK
9 | I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE
10 | I2C_FUNC_SMBUS_READ_BYTE_DATA | I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
11}
12
13/* this function returns the status-register of our i2c-device */
14static inline u32 saa7146_i2c_status(struct saa7146_dev *dev)
15{
16 u32 iicsta = saa7146_read(dev, I2C_STATUS);
17/*
18 DEB_I2C(("status: 0x%08x\n",iicsta));
19*/
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. */
27static int saa7146_i2c_msg_prepare(const struct i2c_msg *m, int num, u32 *op)
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
42 /* we assume that op points to a memory of at least SAA7146_I2C_MEM bytes
43 size. if we exceed this limit... */
44 if ( (4*mem) > SAA7146_I2C_MEM ) {
45//fm DEB_I2C(("cannot prepare i2c-message.\n"));
46 return -ENOMEM;
47 }
48
49 /* be careful: clear out the i2c-mem first */
50 memset(op,0,sizeof(u32)*mem);
51
52 /* loop through all messages */
53 for(i = 0; i < num; i++) {
54
55 /* insert the address of the i2c-slave.
56 note: we get 7 bit i2c-addresses,
57 so we have to perform a translation */
58 addr = (m[i].addr*2) + ( (0 != (m[i].flags & I2C_M_RD)) ? 1 : 0);
59 h1 = op_count/3; h2 = op_count%3;
60 op[h1] |= ( (u8)addr << ((3-h2)*8));
61 op[h1] |= (SAA7146_I2C_START << ((3-h2)*2));
62 op_count++;
63
64 /* loop through all bytes of message i */
65 for(j = 0; j < m[i].len; j++) {
66 /* insert the data bytes */
67 h1 = op_count/3; h2 = op_count%3;
68 op[h1] |= ( (u32)((u8)m[i].buf[j]) << ((3-h2)*8));
69 op[h1] |= ( SAA7146_I2C_CONT << ((3-h2)*2));
70 op_count++;
71 }
72
73 }
74
75 /* have a look at the last byte inserted:
76 if it was: ...CONT change it to ...STOP */
77 h1 = (op_count-1)/3; h2 = (op_count-1)%3;
78 if ( SAA7146_I2C_CONT == (0x3 & (op[h1] >> ((3-h2)*2))) ) {
79 op[h1] &= ~(0x2 << ((3-h2)*2));
80 op[h1] |= (SAA7146_I2C_STOP << ((3-h2)*2));
81 }
82
83 /* return the number of u32s to send */
84 return mem;
85}
86
87/* this functions loops through all i2c-messages. normally, it should determine
88 which bytes were read through the adapter and write them back to the corresponding
89 i2c-message. but instead, we simply write back all bytes.
90 fixme: this could be improved. */
91static int saa7146_i2c_msg_cleanup(const struct i2c_msg *m, int num, u32 *op)
92{
93 int i, j;
94 int op_count = 0;
95
96 /* loop through all messages */
97 for(i = 0; i < num; i++) {
98
99 op_count++;
100
101 /* loop throgh all bytes of message i */
102 for(j = 0; j < m[i].len; j++) {
103 /* write back all bytes that could have been read */
104 m[i].buf[j] = (op[op_count/3] >> ((3-(op_count%3))*8));
105 op_count++;
106 }
107 }
108
109 return 0;
110}
111
112/* this functions resets the i2c-device and returns 0 if everything was fine, otherwise -1 */
113static int saa7146_i2c_reset(struct saa7146_dev *dev)
114{
115 /* get current status */
116 u32 status = saa7146_i2c_status(dev);
117
118 /* clear registers for sure */
119 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
120 saa7146_write(dev, I2C_TRANSFER, 0);
121
122 /* check if any operation is still in progress */
123 if ( 0 != ( status & SAA7146_I2C_BUSY) ) {
124
125 /* yes, kill ongoing operation */
126 DEB_I2C(("busy_state detected.\n"));
127
128 /* set "ABORT-OPERATION"-bit (bit 7)*/
129 saa7146_write(dev, I2C_STATUS, (dev->i2c_bitrate | MASK_07));
130 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
131 msleep(SAA7146_I2C_DELAY);
132
133 /* clear all error-bits pending; this is needed because p.123, note 1 */
134 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
135 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
136 msleep(SAA7146_I2C_DELAY);
137 }
138
139 /* check if any error is (still) present. (this can be necessary because p.123, note 1) */
140 status = saa7146_i2c_status(dev);
141
142 if ( dev->i2c_bitrate != status ) {
143
144 DEB_I2C(("error_state detected. status:0x%08x\n",status));
145
146 /* Repeat the abort operation. This seems to be necessary
147 after serious protocol errors caused by e.g. the SAA7740 */
148 saa7146_write(dev, I2C_STATUS, (dev->i2c_bitrate | MASK_07));
149 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
150 msleep(SAA7146_I2C_DELAY);
151
152 /* clear all error-bits pending */
153 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
154 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
155 msleep(SAA7146_I2C_DELAY);
156
157 /* the data sheet says it might be necessary to clear the status
158 twice after an abort */
159 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
160 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
161 msleep(SAA7146_I2C_DELAY);
162 }
163
164 /* if any error is still present, a fatal error has occured ... */
165 status = saa7146_i2c_status(dev);
166 if ( dev->i2c_bitrate != status ) {
167 DEB_I2C(("fatal error. status:0x%08x\n",status));
168 return -1;
169 }
170
171 return 0;
172}
173
174/* this functions writes out the data-byte 'dword' to the i2c-device.
175 it returns 0 if ok, -1 if the transfer failed, -2 if the transfer
176 failed badly (e.g. address error) */
177static int saa7146_i2c_writeout(struct saa7146_dev *dev, u32* dword, int short_delay)
178{
179 u32 status = 0, mc2 = 0;
180 int trial = 0;
181 unsigned long timeout;
182
183 /* write out i2c-command */
184 DEB_I2C(("before: 0x%08x (status: 0x%08x), %d\n",*dword,saa7146_read(dev, I2C_STATUS), dev->i2c_op));
185
186 if( 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags)) {
187
188 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
189 saa7146_write(dev, I2C_TRANSFER, *dword);
190
191 dev->i2c_op = 1;
192 SAA7146_IER_ENABLE(dev, MASK_16|MASK_17);
193 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
194
195 wait_event_interruptible(dev->i2c_wq, dev->i2c_op == 0);
196 if (signal_pending (current)) {
197 /* a signal arrived */
198 return -ERESTARTSYS;
199 }
200 status = saa7146_read(dev, I2C_STATUS);
201 } else {
202 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
203 saa7146_write(dev, I2C_TRANSFER, *dword);
204 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
205
206 /* do not poll for i2c-status before upload is complete */
207 timeout = jiffies + HZ/100 + 1; /* 10ms */
208 while(1) {
209 mc2 = (saa7146_read(dev, MC2) & 0x1);
210 if( 0 != mc2 ) {
211 break;
212 }
213 if (time_after(jiffies,timeout)) {
214 printk(KERN_WARNING "saa7146_i2c_writeout: timed out waiting for MC2\n");
215 return -EIO;
216 }
217 }
218 /* wait until we get a transfer done or error */
219 timeout = jiffies + HZ/100 + 1; /* 10ms */
Oliver Endriss9bb6e252006-10-27 18:02:01 -0300220 /* first read usually delivers bogus results... */
221 saa7146_i2c_status(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 while(1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 status = saa7146_i2c_status(dev);
224 if ((status & 0x3) != 1)
225 break;
226 if (time_after(jiffies,timeout)) {
227 /* this is normal when probing the bus
228 * (no answer from nonexisistant device...)
229 */
230 DEB_I2C(("saa7146_i2c_writeout: timed out waiting for end of xfer\n"));
231 return -EIO;
232 }
Oliver Endriss9bb6e252006-10-27 18:02:01 -0300233 if (++trial < 50 && short_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 udelay(10);
235 else
Oliver Endriss9bb6e252006-10-27 18:02:01 -0300236 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 }
239
240 /* give a detailed status report */
241 if ( 0 != (status & SAA7146_I2C_ERR)) {
242
243 if( 0 != (status & SAA7146_I2C_SPERR) ) {
244 DEB_I2C(("error due to invalid start/stop condition.\n"));
245 }
246 if( 0 != (status & SAA7146_I2C_DTERR) ) {
247 DEB_I2C(("error in data transmission.\n"));
248 }
249 if( 0 != (status & SAA7146_I2C_DRERR) ) {
250 DEB_I2C(("error when receiving data.\n"));
251 }
252 if( 0 != (status & SAA7146_I2C_AL) ) {
253 DEB_I2C(("error because arbitration lost.\n"));
254 }
255
256 /* we handle address-errors here */
257 if( 0 != (status & SAA7146_I2C_APERR) ) {
258 DEB_I2C(("error in address phase.\n"));
259 return -EREMOTEIO;
260 }
261
262 return -EIO;
263 }
264
265 /* read back data, just in case we were reading ... */
266 *dword = saa7146_read(dev, I2C_TRANSFER);
267
268 DEB_I2C(("after: 0x%08x\n",*dword));
269 return 0;
270}
271
272int saa7146_i2c_transfer(struct saa7146_dev *dev, const struct i2c_msg *msgs, int num, int retries)
273{
274 int i = 0, count = 0;
275 u32* buffer = dev->d_i2c.cpu_addr;
276 int err = 0;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800277 int address_err = 0;
278 int short_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Ingo Molnar3593cab2006-02-07 06:49:14 -0200280 if (mutex_lock_interruptible(&dev->i2c_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -ERESTARTSYS;
282
283 for(i=0;i<num;i++) {
284 DEB_I2C(("msg:%d/%d\n",i+1,num));
285 }
286
287 /* prepare the message(s), get number of u32s to transfer */
288 count = saa7146_i2c_msg_prepare(msgs, num, buffer);
289 if ( 0 > count ) {
290 err = -1;
291 goto out;
292 }
293
294 if ( count > 3 || 0 != (SAA7146_I2C_SHORT_DELAY & dev->ext->flags) )
295 short_delay = 1;
296
297 do {
298 /* reset the i2c-device if necessary */
299 err = saa7146_i2c_reset(dev);
300 if ( 0 > err ) {
301 DEB_I2C(("could not reset i2c-device.\n"));
302 goto out;
303 }
304
305 /* write out the u32s one after another */
306 for(i = 0; i < count; i++) {
307 err = saa7146_i2c_writeout(dev, &buffer[i], short_delay);
308 if ( 0 != err) {
309 /* this one is unsatisfying: some i2c slaves on some
310 dvb cards don't acknowledge correctly, so the saa7146
311 thinks that an address error occured. in that case, the
312 transaction should be retrying, even if an address error
313 occured. analog saa7146 based cards extensively rely on
314 i2c address probing, however, and address errors indicate that a
315 device is really *not* there. retrying in that case
316 increases the time the device needs to probe greatly, so
317 it should be avoided. because of the fact, that only
318 analog based cards use irq based i2c transactions (for dvb
319 cards, this screwes up other interrupt sources), we bail out
320 completely for analog cards after an address error and trust
321 the saa7146 address error detection. */
322 if ( -EREMOTEIO == err ) {
323 if( 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags)) {
324 goto out;
325 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800326 address_err++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 DEB_I2C(("error while sending message(s). starting again.\n"));
329 break;
330 }
331 }
332 if( 0 == err ) {
333 err = num;
334 break;
335 }
336
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800337 /* delay a bit before retrying */
338 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 } while (err != num && retries--);
341
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800342 /* if every retry had an address error, exit right away */
343 if (address_err == retries) {
344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 /* if any things had to be read, get the results */
348 if ( 0 != saa7146_i2c_msg_cleanup(msgs, num, buffer)) {
349 DEB_I2C(("could not cleanup i2c-message.\n"));
350 err = -1;
351 goto out;
352 }
353
354 /* return the number of delivered messages */
355 DEB_I2C(("transmission successful. (msg:%d).\n",err));
356out:
357 /* another bug in revision 0: the i2c-registers get uploaded randomly by other
358 uploads, so we better clear them out before continueing */
359 if( 0 == dev->revision ) {
360 u32 zero = 0;
361 saa7146_i2c_reset(dev);
362 if( 0 != saa7146_i2c_writeout(dev, &zero, short_delay)) {
363 INFO(("revision 0 error. this should never happen.\n"));
364 }
365 }
366
Ingo Molnar3593cab2006-02-07 06:49:14 -0200367 mutex_unlock(&dev->i2c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return err;
369}
370
371/* utility functions */
372static int saa7146_i2c_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
373{
374 struct saa7146_dev* dev = i2c_get_adapdata(adapter);
375
376 /* use helper function to transfer data */
377 return saa7146_i2c_transfer(dev, msg, num, adapter->retries);
378}
379
380
381/*****************************************************************************/
382/* i2c-adapter helper functions */
383#include <linux/i2c-id.h>
384
385/* exported algorithm data */
386static struct i2c_algorithm saa7146_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .master_xfer = saa7146_i2c_xfer,
388 .functionality = saa7146_i2c_func,
389};
390
391int saa7146_i2c_adapter_prepare(struct saa7146_dev *dev, struct i2c_adapter *i2c_adapter, u32 bitrate)
392{
393 DEB_EE(("bitrate: 0x%08x\n",bitrate));
394
395 /* enable i2c-port pins */
396 saa7146_write(dev, MC1, (MASK_08 | MASK_24));
397
398 dev->i2c_bitrate = bitrate;
399 saa7146_i2c_reset(dev);
400
401 if( NULL != i2c_adapter ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 BUG_ON(!i2c_adapter->class);
403 i2c_set_adapdata(i2c_adapter,dev);
Philipp Matthias Hahn1ac28542005-09-09 13:03:13 -0700404 i2c_adapter->dev.parent = &dev->pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 i2c_adapter->algo = &saa7146_algo;
406 i2c_adapter->algo_data = NULL;
Jean Delvare1684a9842005-08-11 23:51:10 +0200407 i2c_adapter->id = I2C_HW_SAA7146;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 i2c_adapter->timeout = SAA7146_I2C_TIMEOUT;
409 i2c_adapter->retries = SAA7146_I2C_RETRIES;
410 }
411
412 return 0;
413}