Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[powerpc.git] / drivers / i2c / busses / i2c-iop3xx.c
1 /* ------------------------------------------------------------------------- */
2 /* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x       */
3 /* ------------------------------------------------------------------------- */
4 /* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
5  *                    <Peter dot Milne at D hyphen TACQ dot com>
6  *
7  * With acknowledgements to i2c-algo-ibm_ocp.c by 
8  * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com
9  *
10  * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
11  *
12  * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
13  *  
14  * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>,
15  * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com>
16  *
17  * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005:
18  *
19  * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
20  * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
21  * - Make it work with IXP46x chips
22  * - Cleanup function names, coding style, etc
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, version 2.
27  */
28
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/errno.h>
36 #include <linux/sched.h>
37 #include <linux/platform_device.h>
38 #include <linux/i2c.h>
39
40 #include <asm/io.h>
41
42 #include "i2c-iop3xx.h"
43
44 /* global unit counter */
45 static int i2c_id;
46
47 static inline unsigned char 
48 iic_cook_addr(struct i2c_msg *msg) 
49 {
50         unsigned char addr;
51
52         addr = (msg->addr << 1);
53
54         if (msg->flags & I2C_M_RD)
55                 addr |= 1;
56
57         /*
58          * Read or Write?
59          */
60         if (msg->flags & I2C_M_REV_DIR_ADDR)
61                 addr ^= 1;
62
63         return addr;   
64 }
65
66 static void 
67 iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
68 {
69         /* Follows devman 9.3 */
70         __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
71         __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
72         __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
73
74
75 static void 
76 iop3xx_i2c_set_slave_addr(struct i2c_algo_iop3xx_data *iop3xx_adap)
77 {
78         __raw_writel(MYSAR, iop3xx_adap->ioaddr + SAR_OFFSET);
79 }
80
81 static void 
82 iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
83 {
84         u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
85
86         /* 
87          * Every time unit enable is asserted, GPOD needs to be cleared
88          * on IOP321 to avoid data corruption on the bus.
89          */
90 #ifdef CONFIG_ARCH_IOP321
91 #define IOP321_GPOD_I2C0    0x00c0  /* clear these bits to enable ch0 */
92 #define IOP321_GPOD_I2C1    0x0030  /* clear these bits to enable ch1 */
93
94         *IOP321_GPOD &= (iop3xx_adap->id == 0) ? ~IOP321_GPOD_I2C0 : 
95                 ~IOP321_GPOD_I2C1;
96 #endif
97         /* NB SR bits not same position as CR IE bits :-( */
98         iop3xx_adap->SR_enabled = 
99                 IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
100                 IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
101
102         cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
103                 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
104
105         __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
106 }
107
108 static void 
109 iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
110 {
111         unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
112         
113         cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE | 
114                 IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
115
116         __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
117 }
118
119 /* 
120  * NB: the handler has to clear the source of the interrupt! 
121  * Then it passes the SR flags of interest to BH via adap data
122  */
123 static irqreturn_t 
124 iop3xx_i2c_irq_handler(int this_irq, void *dev_id, struct pt_regs *regs) 
125 {
126         struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
127         u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
128
129         if ((sr &= iop3xx_adap->SR_enabled)) {
130                 __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
131                 iop3xx_adap->SR_received |= sr;
132                 wake_up_interruptible(&iop3xx_adap->waitq);
133         }
134         return IRQ_HANDLED;
135 }
136
137 /* check all error conditions, clear them , report most important */
138 static int 
139 iop3xx_i2c_error(u32 sr)
140 {
141         int rc = 0;
142
143         if ((sr & IOP3XX_ISR_BERRD)) {
144                 if ( !rc ) rc = -I2C_ERR_BERR;
145         }
146         if ((sr & IOP3XX_ISR_ALD)) {
147                 if ( !rc ) rc = -I2C_ERR_ALD;           
148         }
149         return rc;      
150 }
151
152 static inline u32 
153 iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
154 {
155         unsigned long flags;
156         u32 sr;
157
158         spin_lock_irqsave(&iop3xx_adap->lock, flags);
159         sr = iop3xx_adap->SR_received;
160         iop3xx_adap->SR_received = 0;
161         spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
162
163         return sr;
164 }
165
166 /*
167  * sleep until interrupted, then recover and analyse the SR
168  * saved by handler
169  */
170 typedef int (* compare_func)(unsigned test, unsigned mask);
171 /* returns 1 on correct comparison */
172
173 static int 
174 iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap, 
175                           unsigned flags, unsigned* status,
176                           compare_func compare)
177 {
178         unsigned sr = 0;
179         int interrupted;
180         int done;
181         int rc = 0;
182
183         do {
184                 interrupted = wait_event_interruptible_timeout (
185                         iop3xx_adap->waitq,
186                         (done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )),
187                         1 * HZ;
188                         );
189                 if ((rc = iop3xx_i2c_error(sr)) < 0) {
190                         *status = sr;
191                         return rc;
192                 } else if (!interrupted) {
193                         *status = sr;
194                         return -ETIMEDOUT;
195                 }
196         } while(!done);
197
198         *status = sr;
199
200         return 0;
201 }
202
203 /*
204  * Concrete compare_funcs 
205  */
206 static int 
207 all_bits_clear(unsigned test, unsigned mask)
208 {
209         return (test & mask) == 0;
210 }
211
212 static int 
213 any_bits_set(unsigned test, unsigned mask)
214 {
215         return (test & mask) != 0;
216 }
217
218 static int 
219 iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
220 {
221         return iop3xx_i2c_wait_event( 
222                 iop3xx_adap, 
223                 IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
224                 status, any_bits_set);
225 }
226
227 static int 
228 iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
229 {
230         return iop3xx_i2c_wait_event( 
231                 iop3xx_adap, 
232                 IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
233                 status, any_bits_set);
234 }
235
236 static int 
237 iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
238 {
239         return iop3xx_i2c_wait_event( 
240                 iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
241 }
242
243 static int 
244 iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap, 
245                                 struct i2c_msg* msg)
246 {
247         unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
248         int status;
249         int rc;
250
251         __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
252         
253         cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
254         cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
255
256         __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
257         rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
258
259         return rc;
260 }
261
262 static int 
263 iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte, 
264                                 int stop)
265 {
266         unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
267         int status;
268         int rc = 0;
269
270         __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
271         cr &= ~IOP3XX_ICR_MSTART;
272         if (stop) {
273                 cr |= IOP3XX_ICR_MSTOP;
274         } else {
275                 cr &= ~IOP3XX_ICR_MSTOP;
276         }
277         cr |= IOP3XX_ICR_TBYTE;
278         __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
279         rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
280
281         return rc;
282
283
284 static int 
285 iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte, 
286                                 int stop)
287 {
288         unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
289         int status;
290         int rc = 0;
291
292         cr &= ~IOP3XX_ICR_MSTART;
293
294         if (stop) {
295                 cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
296         } else {
297                 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
298         }
299         cr |= IOP3XX_ICR_TBYTE;
300         __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
301
302         rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
303
304         *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
305
306         return rc;
307 }
308
309 static int 
310 iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
311 {
312         struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
313         int ii;
314         int rc = 0;
315
316         for (ii = 0; rc == 0 && ii != count; ++ii) 
317                 rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1);
318         return rc;
319 }
320
321 static int 
322 iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
323 {
324         struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
325         int ii;
326         int rc = 0;
327
328         for (ii = 0; rc == 0 && ii != count; ++ii)
329                 rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1);
330         
331         return rc;
332 }
333
334 /*
335  * Description:  This function implements combined transactions.  Combined
336  * transactions consist of combinations of reading and writing blocks of data.
337  * FROM THE SAME ADDRESS
338  * Each transfer (i.e. a read or a write) is separated by a repeated start
339  * condition.
340  */
341 static int 
342 iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg) 
343 {
344         struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
345         int rc;
346
347         rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
348         if (rc < 0) {
349                 return rc;
350         }
351
352         if ((pmsg->flags&I2C_M_RD)) {
353                 return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
354         } else {
355                 return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
356         }
357 }
358
359 /*
360  * master_xfer() - main read/write entry
361  */
362 static int 
363 iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, 
364                                 int num)
365 {
366         struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
367         int im = 0;
368         int ret = 0;
369         int status;
370
371         iop3xx_i2c_wait_idle(iop3xx_adap, &status);
372         iop3xx_i2c_reset(iop3xx_adap);
373         iop3xx_i2c_enable(iop3xx_adap);
374
375         for (im = 0; ret == 0 && im != num; im++) {
376                 ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
377         }
378
379         iop3xx_i2c_transaction_cleanup(iop3xx_adap);
380         
381         if(ret)
382                 return ret;
383
384         return im;   
385 }
386
387 static int 
388 iop3xx_i2c_algo_control(struct i2c_adapter *adapter, unsigned int cmd,
389                         unsigned long arg)
390 {
391         return 0;
392 }
393
394 static u32 
395 iop3xx_i2c_func(struct i2c_adapter *adap)
396 {
397         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
398 }
399
400 static struct i2c_algorithm iop3xx_i2c_algo = {
401         .master_xfer    = iop3xx_i2c_master_xfer,
402         .algo_control   = iop3xx_i2c_algo_control,
403         .functionality  = iop3xx_i2c_func,
404 };
405
406 static int 
407 iop3xx_i2c_remove(struct platform_device *pdev)
408 {
409         struct i2c_adapter *padapter = platform_get_drvdata(pdev);
410         struct i2c_algo_iop3xx_data *adapter_data = 
411                 (struct i2c_algo_iop3xx_data *)padapter->algo_data;
412         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
413         unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
414
415         /*
416          * Disable the actual HW unit
417          */
418         cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
419                 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
420         __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
421
422         iounmap((void __iomem*)adapter_data->ioaddr);
423         release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
424         kfree(adapter_data);
425         kfree(padapter);
426
427         platform_set_drvdata(pdev, NULL);
428
429         return 0;
430 }
431
432 static int 
433 iop3xx_i2c_probe(struct platform_device *pdev)
434 {
435         struct resource *res;
436         int ret, irq;
437         struct i2c_adapter *new_adapter;
438         struct i2c_algo_iop3xx_data *adapter_data;
439
440         new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
441         if (!new_adapter) {
442                 ret = -ENOMEM;
443                 goto out;
444         }
445
446         adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
447         if (!adapter_data) {
448                 ret = -ENOMEM;
449                 goto free_adapter;
450         }
451
452         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
453         if (!res) {
454                 ret = -ENODEV;
455                 goto free_both;
456         }
457
458         if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
459                 ret = -EBUSY;
460                 goto free_both;
461         }
462
463         /* set the adapter enumeration # */
464         adapter_data->id = i2c_id++;
465
466         adapter_data->ioaddr = (u32)ioremap(res->start, IOP3XX_I2C_IO_SIZE);
467         if (!adapter_data->ioaddr) {
468                 ret = -ENOMEM;
469                 goto release_region;
470         }
471
472         irq = platform_get_irq(pdev, 0);
473         if (irq < 0) {
474                 ret = -ENXIO;
475                 goto unmap;
476         }
477         ret = request_irq(irq, iop3xx_i2c_irq_handler, 0,
478                                 pdev->name, adapter_data);
479
480         if (ret) {
481                 ret = -EIO;
482                 goto unmap;
483         }
484
485         memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
486         new_adapter->id = I2C_HW_IOP3XX;
487         new_adapter->owner = THIS_MODULE;
488         new_adapter->dev.parent = &pdev->dev;
489
490         /*
491          * Default values...should these come in from board code?
492          */
493         new_adapter->timeout = 100;     
494         new_adapter->retries = 3;
495         new_adapter->algo = &iop3xx_i2c_algo;
496
497         init_waitqueue_head(&adapter_data->waitq);
498         spin_lock_init(&adapter_data->lock);
499
500         iop3xx_i2c_reset(adapter_data);
501         iop3xx_i2c_set_slave_addr(adapter_data);
502         iop3xx_i2c_enable(adapter_data);
503
504         platform_set_drvdata(pdev, new_adapter);
505         new_adapter->algo_data = adapter_data;
506
507         i2c_add_adapter(new_adapter);
508
509         return 0;
510
511 unmap:
512         iounmap((void __iomem*)adapter_data->ioaddr);
513
514 release_region:
515         release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
516
517 free_both:
518         kfree(adapter_data);
519
520 free_adapter:
521         kfree(new_adapter);
522
523 out:
524         return ret;
525 }
526
527
528 static struct platform_driver iop3xx_i2c_driver = {
529         .probe          = iop3xx_i2c_probe,
530         .remove         = iop3xx_i2c_remove,
531         .driver         = {
532                 .owner  = THIS_MODULE,
533                 .name   = "IOP3xx-I2C",
534         },
535 };
536
537 static int __init 
538 i2c_iop3xx_init (void)
539 {
540         return platform_driver_register(&iop3xx_i2c_driver);
541 }
542
543 static void __exit 
544 i2c_iop3xx_exit (void)
545 {
546         platform_driver_unregister(&iop3xx_i2c_driver);
547         return;
548 }
549
550 module_init (i2c_iop3xx_init);
551 module_exit (i2c_iop3xx_exit);
552
553 MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
554 MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
555 MODULE_LICENSE("GPL");