1#include <media/saa7146_vv.h>
2
3static u32 saa7146_i2c_func(struct i2c_adapter *adapter)
4{
5
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
14static inline u32 saa7146_i2c_status(struct saa7146_dev *dev)
15{
16 u32 iicsta = saa7146_read(dev, I2C_STATUS);
17
18
19
20 return iicsta;
21}
22
23
24
25
26
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
34 for(i = 0; i < num; i++) {
35 mem += m[i].len + 1;
36 }
37
38
39
40 mem = 1 + ((mem-1) / 3);
41
42
43
44 if ( (4*mem) > SAA7146_I2C_MEM ) {
45
46 return -ENOMEM;
47 }
48
49
50 memset(op,0,sizeof(u32)*mem);
51
52
53 for(i = 0; i < num; i++) {
54
55
56
57
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
65 for(j = 0; j < m[i].len; j++) {
66
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
76
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
84 return mem;
85}
86
87
88
89
90
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
97 for(i = 0; i < num; i++) {
98
99 op_count++;
100
101
102 for(j = 0; j < m[i].len; j++) {
103
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
113static int saa7146_i2c_reset(struct saa7146_dev *dev)
114{
115
116 u32 status = saa7146_i2c_status(dev);
117
118
119 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
120 saa7146_write(dev, I2C_TRANSFER, 0);
121
122
123 if ( 0 != ( status & SAA7146_I2C_BUSY) ) {
124
125
126 DEB_I2C(("busy_state detected.\n"));
127
128
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
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
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
147
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
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
158
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
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
175
176
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
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_ISR_CLEAR(dev, MASK_16|MASK_17);
193 SAA7146_IER_ENABLE(dev, MASK_16|MASK_17);
194 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
195
196 timeout = HZ/100 + 1;
197 timeout = wait_event_interruptible_timeout(dev->i2c_wq, dev->i2c_op == 0, timeout);
198 if (timeout == -ERESTARTSYS || dev->i2c_op) {
199 SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
200 SAA7146_ISR_CLEAR(dev, MASK_16|MASK_17);
201 if (timeout == -ERESTARTSYS)
202
203 return -ERESTARTSYS;
204
205 printk(KERN_WARNING "%s %s [irq]: timed out waiting for end of xfer\n",
206 dev->name, __FUNCTION__);
207 return -EIO;
208 }
209 status = saa7146_read(dev, I2C_STATUS);
210 } else {
211 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
212 saa7146_write(dev, I2C_TRANSFER, *dword);
213 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
214
215
216 timeout = jiffies + HZ/100 + 1;
217 while(1) {
218 mc2 = (saa7146_read(dev, MC2) & 0x1);
219 if( 0 != mc2 ) {
220 break;
221 }
222 if (time_after(jiffies,timeout)) {
223 printk(KERN_WARNING "%s %s: timed out waiting for MC2\n",
224 dev->name, __FUNCTION__);
225 return -EIO;
226 }
227 }
228
229 timeout = jiffies + HZ/100 + 1;
230
231 saa7146_i2c_status(dev);
232 while(1) {
233 status = saa7146_i2c_status(dev);
234 if ((status & 0x3) != 1)
235 break;
236 if (time_after(jiffies,timeout)) {
237
238
239
240 printk(KERN_WARNING "%s %s [poll]: timed out waiting for end of xfer\n",
241 dev->name, __FUNCTION__);
242 return -EIO;
243 }
244 if (++trial < 50 && short_delay)
245 udelay(10);
246 else
247 msleep(1);
248 }
249 }
250
251
252 if ( 0 != (status & (SAA7146_I2C_SPERR | SAA7146_I2C_APERR |
253 SAA7146_I2C_DTERR | SAA7146_I2C_DRERR |
254 SAA7146_I2C_AL | SAA7146_I2C_ERR |
255 SAA7146_I2C_BUSY)) ) {
256
257 if ( 0 == (status & SAA7146_I2C_ERR) ||
258 0 == (status & SAA7146_I2C_BUSY) ) {
259
260 DEB_I2C(("unexpected i2c status %04x\n", status));
261 }
262 if( 0 != (status & SAA7146_I2C_SPERR) ) {
263 DEB_I2C(("error due to invalid start/stop condition.\n"));
264 }
265 if( 0 != (status & SAA7146_I2C_DTERR) ) {
266 DEB_I2C(("error in data transmission.\n"));
267 }
268 if( 0 != (status & SAA7146_I2C_DRERR) ) {
269 DEB_I2C(("error when receiving data.\n"));
270 }
271 if( 0 != (status & SAA7146_I2C_AL) ) {
272 DEB_I2C(("error because arbitration lost.\n"));
273 }
274
275
276 if( 0 != (status & SAA7146_I2C_APERR) ) {
277 DEB_I2C(("error in address phase.\n"));
278 return -EREMOTEIO;
279 }
280
281 return -EIO;
282 }
283
284
285 *dword = saa7146_read(dev, I2C_TRANSFER);
286
287 DEB_I2C(("after: 0x%08x\n",*dword));
288 return 0;
289}
290
291static int saa7146_i2c_transfer(struct saa7146_dev *dev, const struct i2c_msg *msgs, int num, int retries)
292{
293 int i = 0, count = 0;
294 u32* buffer = dev->d_i2c.cpu_addr;
295 int err = 0;
296 int address_err = 0;
297 int short_delay = 0;
298
299 if (mutex_lock_interruptible(&dev->i2c_lock))
300 return -ERESTARTSYS;
301
302 for(i=0;i<num;i++) {
303 DEB_I2C(("msg:%d/%d\n",i+1,num));
304 }
305
306
307 count = saa7146_i2c_msg_prepare(msgs, num, buffer);
308 if ( 0 > count ) {
309 err = -1;
310 goto out;
311 }
312
313 if ( count > 3 || 0 != (SAA7146_I2C_SHORT_DELAY & dev->ext->flags) )
314 short_delay = 1;
315
316 do {
317
318 err = saa7146_i2c_reset(dev);
319 if ( 0 > err ) {
320 DEB_I2C(("could not reset i2c-device.\n"));
321 goto out;
322 }
323
324
325 for(i = 0; i < count; i++) {
326 err = saa7146_i2c_writeout(dev, &buffer[i], short_delay);
327 if ( 0 != err) {
328
329
330
331
332
333
334
335
336
337
338
339
340
341 if ( -EREMOTEIO == err ) {
342 if( 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags)) {
343 goto out;
344 }
345 address_err++;
346 }
347 DEB_I2C(("error while sending message(s). starting again.\n"));
348 break;
349 }
350 }
351 if( 0 == err ) {
352 err = num;
353 break;
354 }
355
356
357 msleep(10);
358
359 } while (err != num && retries--);
360
361
362 if (address_err == retries) {
363 goto out;
364 }
365
366
367 if ( 0 != saa7146_i2c_msg_cleanup(msgs, num, buffer)) {
368 DEB_I2C(("could not cleanup i2c-message.\n"));
369 err = -1;
370 goto out;
371 }
372
373
374 DEB_I2C(("transmission successful. (msg:%d).\n",err));
375out:
376
377
378 if( 0 == dev->revision ) {
379 u32 zero = 0;
380 saa7146_i2c_reset(dev);
381 if( 0 != saa7146_i2c_writeout(dev, &zero, short_delay)) {
382 INFO(("revision 0 error. this should never happen.\n"));
383 }
384 }
385
386 mutex_unlock(&dev->i2c_lock);
387 return err;
388}
389
390
391static int saa7146_i2c_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
392{
393 struct saa7146_dev* dev = i2c_get_adapdata(adapter);
394
395
396 return saa7146_i2c_transfer(dev, msg, num, adapter->retries);
397}
398
399
400
401
402#include <linux/i2c-id.h>
403
404
405static struct i2c_algorithm saa7146_algo = {
406 .master_xfer = saa7146_i2c_xfer,
407 .functionality = saa7146_i2c_func,
408};
409
410int saa7146_i2c_adapter_prepare(struct saa7146_dev *dev, struct i2c_adapter *i2c_adapter, u32 bitrate)
411{
412 DEB_EE(("bitrate: 0x%08x\n",bitrate));
413
414
415 saa7146_write(dev, MC1, (MASK_08 | MASK_24));
416
417 dev->i2c_bitrate = bitrate;
418 saa7146_i2c_reset(dev);
419
420 if( NULL != i2c_adapter ) {
421 BUG_ON(!i2c_adapter->class);
422 i2c_set_adapdata(i2c_adapter,dev);
423 i2c_adapter->dev.parent = &dev->pci->dev;
424 i2c_adapter->algo = &saa7146_algo;
425 i2c_adapter->algo_data = NULL;
426 i2c_adapter->id = I2C_HW_SAA7146;
427 i2c_adapter->timeout = SAA7146_I2C_TIMEOUT;
428 i2c_adapter->retries = SAA7146_I2C_RETRIES;
429 }
430
431 return 0;
432}
433