aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/busses/i2c-pxa.c
diff options
context:
space:
mode:
authorMike Rapoport <mike@compulab.co.il>2008-01-27 12:14:50 -0500
committerJean Delvare <khali@hyperion.delvare>2008-01-27 12:14:50 -0500
commitb7a3670131c7662415fa799700fc0bdfe90a54b6 (patch)
tree51ff8432c3bfd82a40b0bdb89b0def7409b5ced8 /drivers/i2c/busses/i2c-pxa.c
parentcea443a81c9c6257bf2d00f1392f7d1d4ce03b75 (diff)
i2c-pxa: Add polling transfer
Add polling I2C transfer implementation for PXA I2C. This is needed for cases where I2C transactions have to occur at times interrups are disabled. Signed-off-by: Mike Rapoport <mike@compulab.co.il> Acked-by: eric miao <eric.miao@marvell.com> Signed-off-by: Jean Delvare <khali@linux-fr.org>
Diffstat (limited to 'drivers/i2c/busses/i2c-pxa.c')
-rw-r--r--drivers/i2c/busses/i2c-pxa.c133
1 files changed, 121 insertions, 12 deletions
diff --git a/drivers/i2c/busses/i2c-pxa.c b/drivers/i2c/busses/i2c-pxa.c
index da3ecf5c591b..2598d29fd7a4 100644
--- a/drivers/i2c/busses/i2c-pxa.c
+++ b/drivers/i2c/busses/i2c-pxa.c
@@ -65,6 +65,7 @@ struct pxa_i2c {
65 unsigned long iosize; 65 unsigned long iosize;
66 66
67 int irq; 67 int irq;
68 int use_pio;
68}; 69};
69 70
70#define _IBMR(i2c) ((i2c)->reg_base + 0) 71#define _IBMR(i2c) ((i2c)->reg_base + 0)
@@ -163,6 +164,7 @@ static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
163#define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0) 164#define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0)
164 165
165static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret); 166static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
167static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
166 168
167static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why) 169static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
168{ 170{
@@ -554,6 +556,71 @@ static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
554 writel(icr, _ICR(i2c)); 556 writel(icr, _ICR(i2c));
555} 557}
556 558
559static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
560{
561 /* make timeout the same as for interrupt based functions */
562 long timeout = 2 * DEF_TIMEOUT;
563
564 /*
565 * Wait for the bus to become free.
566 */
567 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
568 udelay(1000);
569 show_state(i2c);
570 }
571
572 if (timeout <= 0) {
573 show_state(i2c);
574 dev_err(&i2c->adap.dev,
575 "i2c_pxa: timeout waiting for bus free\n");
576 return I2C_RETRY;
577 }
578
579 /*
580 * Set master mode.
581 */
582 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
583
584 return 0;
585}
586
587static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
588 struct i2c_msg *msg, int num)
589{
590 unsigned long timeout = 500000; /* 5 seconds */
591 int ret = 0;
592
593 ret = i2c_pxa_pio_set_master(i2c);
594 if (ret)
595 goto out;
596
597 i2c->msg = msg;
598 i2c->msg_num = num;
599 i2c->msg_idx = 0;
600 i2c->msg_ptr = 0;
601 i2c->irqlogidx = 0;
602
603 i2c_pxa_start_message(i2c);
604
605 while (timeout-- && i2c->msg_num > 0) {
606 i2c_pxa_handler(0, i2c);
607 udelay(10);
608 }
609
610 i2c_pxa_stop_message(i2c);
611
612 /*
613 * We place the return code in i2c->msg_idx.
614 */
615 ret = i2c->msg_idx;
616
617out:
618 if (timeout == 0)
619 i2c_pxa_scream_blue_murder(i2c, "timeout");
620
621 return ret;
622}
623
557/* 624/*
558 * We are protected by the adapter bus mutex. 625 * We are protected by the adapter bus mutex.
559 */ 626 */
@@ -610,6 +677,35 @@ static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
610 return ret; 677 return ret;
611} 678}
612 679
680static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
681 struct i2c_msg msgs[], int num)
682{
683 struct pxa_i2c *i2c = adap->algo_data;
684 int ret, i;
685
686 /* If the I2C controller is disabled we need to reset it
687 (probably due to a suspend/resume destroying state). We do
688 this here as we can then avoid worrying about resuming the
689 controller before its users. */
690 if (!(readl(_ICR(i2c)) & ICR_IUE))
691 i2c_pxa_reset(i2c);
692
693 for (i = adap->retries; i >= 0; i--) {
694 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
695 if (ret != I2C_RETRY)
696 goto out;
697
698 if (i2c_debug)
699 dev_dbg(&adap->dev, "Retrying transmission\n");
700 udelay(100);
701 }
702 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
703 ret = -EREMOTEIO;
704 out:
705 i2c_pxa_set_slave(i2c, ret);
706 return ret;
707}
708
613/* 709/*
614 * i2c_pxa_master_complete - complete the message and wake up. 710 * i2c_pxa_master_complete - complete the message and wake up.
615 */ 711 */
@@ -621,7 +717,8 @@ static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
621 i2c->msg_num = 0; 717 i2c->msg_num = 0;
622 if (ret) 718 if (ret)
623 i2c->msg_idx = ret; 719 i2c->msg_idx = ret;
624 wake_up(&i2c->wait); 720 if (!i2c->use_pio)
721 wake_up(&i2c->wait);
625} 722}
626 723
627static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr) 724static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
@@ -840,6 +937,11 @@ static const struct i2c_algorithm i2c_pxa_algorithm = {
840 .functionality = i2c_pxa_functionality, 937 .functionality = i2c_pxa_functionality,
841}; 938};
842 939
940static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
941 .master_xfer = i2c_pxa_pio_xfer,
942 .functionality = i2c_pxa_functionality,
943};
944
843static void i2c_pxa_enable(struct platform_device *dev) 945static void i2c_pxa_enable(struct platform_device *dev)
844{ 946{
845 if (cpu_is_pxa27x()) { 947 if (cpu_is_pxa27x()) {
@@ -890,7 +992,6 @@ static int i2c_pxa_probe(struct platform_device *dev)
890 } 992 }
891 993
892 i2c->adap.owner = THIS_MODULE; 994 i2c->adap.owner = THIS_MODULE;
893 i2c->adap.algo = &i2c_pxa_algorithm;
894 i2c->adap.retries = 5; 995 i2c->adap.retries = 5;
895 996
896 spin_lock_init(&i2c->lock); 997 spin_lock_init(&i2c->lock);
@@ -927,20 +1028,26 @@ static int i2c_pxa_probe(struct platform_device *dev)
927 clk_enable(i2c->clk); 1028 clk_enable(i2c->clk);
928 i2c_pxa_enable(dev); 1029 i2c_pxa_enable(dev);
929 1030
930 ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED, 1031 if (plat) {
931 i2c->adap.name, i2c); 1032 i2c->adap.class = plat->class;
932 if (ret) 1033 i2c->use_pio = plat->use_pio;
933 goto ereqirq; 1034 }
1035
1036 if (i2c->use_pio) {
1037 i2c->adap.algo = &i2c_pxa_pio_algorithm;
1038 } else {
1039 i2c->adap.algo = &i2c_pxa_algorithm;
1040 ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
1041 i2c->adap.name, i2c);
1042 if (ret)
1043 goto ereqirq;
1044 }
934 1045
935 i2c_pxa_reset(i2c); 1046 i2c_pxa_reset(i2c);
936 1047
937 i2c->adap.algo_data = i2c; 1048 i2c->adap.algo_data = i2c;
938 i2c->adap.dev.parent = &dev->dev; 1049 i2c->adap.dev.parent = &dev->dev;
939 1050
940 if (plat) {
941 i2c->adap.class = plat->class;
942 }
943
944 /* 1051 /*
945 * If "dev->id" is negative we consider it as zero. 1052 * If "dev->id" is negative we consider it as zero.
946 * The reason to do so is to avoid sysfs names that only make 1053 * The reason to do so is to avoid sysfs names that only make
@@ -966,7 +1073,8 @@ static int i2c_pxa_probe(struct platform_device *dev)
966 return 0; 1073 return 0;
967 1074
968eadapt: 1075eadapt:
969 free_irq(irq, i2c); 1076 if (!i2c->use_pio)
1077 free_irq(irq, i2c);
970ereqirq: 1078ereqirq:
971 clk_disable(i2c->clk); 1079 clk_disable(i2c->clk);
972 i2c_pxa_disable(dev); 1080 i2c_pxa_disable(dev);
@@ -986,7 +1094,8 @@ static int i2c_pxa_remove(struct platform_device *dev)
986 platform_set_drvdata(dev, NULL); 1094 platform_set_drvdata(dev, NULL);
987 1095
988 i2c_del_adapter(&i2c->adap); 1096 i2c_del_adapter(&i2c->adap);
989 free_irq(i2c->irq, i2c); 1097 if (!i2c->use_pio)
1098 free_irq(i2c->irq, i2c);
990 1099
991 clk_disable(i2c->clk); 1100 clk_disable(i2c->clk);
992 clk_put(i2c->clk); 1101 clk_put(i2c->clk);