diff options
Diffstat (limited to 'drivers/i2c/busses/i2c-pxa.c')
-rw-r--r-- | drivers/i2c/busses/i2c-pxa.c | 189 |
1 files changed, 149 insertions, 40 deletions
diff --git a/drivers/i2c/busses/i2c-pxa.c b/drivers/i2c/busses/i2c-pxa.c index 6426a61f8d4d..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 | ||
165 | static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret); | 166 | static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret); |
167 | static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id); | ||
166 | 168 | ||
167 | static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why) | 169 | static 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 | ||
559 | static 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 | |||
587 | static 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 | |||
617 | out: | ||
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 | ||
680 | static 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 | ||
627 | static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr) | 724 | static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr) |
@@ -840,6 +937,37 @@ static const struct i2c_algorithm i2c_pxa_algorithm = { | |||
840 | .functionality = i2c_pxa_functionality, | 937 | .functionality = i2c_pxa_functionality, |
841 | }; | 938 | }; |
842 | 939 | ||
940 | static const struct i2c_algorithm i2c_pxa_pio_algorithm = { | ||
941 | .master_xfer = i2c_pxa_pio_xfer, | ||
942 | .functionality = i2c_pxa_functionality, | ||
943 | }; | ||
944 | |||
945 | static void i2c_pxa_enable(struct platform_device *dev) | ||
946 | { | ||
947 | if (cpu_is_pxa27x()) { | ||
948 | switch (dev->id) { | ||
949 | case 0: | ||
950 | pxa_gpio_mode(GPIO117_I2CSCL_MD); | ||
951 | pxa_gpio_mode(GPIO118_I2CSDA_MD); | ||
952 | break; | ||
953 | case 1: | ||
954 | local_irq_disable(); | ||
955 | PCFR |= PCFR_PI2CEN; | ||
956 | local_irq_enable(); | ||
957 | break; | ||
958 | } | ||
959 | } | ||
960 | } | ||
961 | |||
962 | static void i2c_pxa_disable(struct platform_device *dev) | ||
963 | { | ||
964 | if (cpu_is_pxa27x() && dev->id == 1) { | ||
965 | local_irq_disable(); | ||
966 | PCFR &= ~PCFR_PI2CEN; | ||
967 | local_irq_enable(); | ||
968 | } | ||
969 | } | ||
970 | |||
843 | #define res_len(r) ((r)->end - (r)->start + 1) | 971 | #define res_len(r) ((r)->end - (r)->start + 1) |
844 | static int i2c_pxa_probe(struct platform_device *dev) | 972 | static int i2c_pxa_probe(struct platform_device *dev) |
845 | { | 973 | { |
@@ -864,7 +992,6 @@ static int i2c_pxa_probe(struct platform_device *dev) | |||
864 | } | 992 | } |
865 | 993 | ||
866 | i2c->adap.owner = THIS_MODULE; | 994 | i2c->adap.owner = THIS_MODULE; |
867 | i2c->adap.algo = &i2c_pxa_algorithm; | ||
868 | i2c->adap.retries = 5; | 995 | i2c->adap.retries = 5; |
869 | 996 | ||
870 | spin_lock_init(&i2c->lock); | 997 | spin_lock_init(&i2c->lock); |
@@ -899,34 +1026,28 @@ static int i2c_pxa_probe(struct platform_device *dev) | |||
899 | #endif | 1026 | #endif |
900 | 1027 | ||
901 | clk_enable(i2c->clk); | 1028 | clk_enable(i2c->clk); |
902 | #ifdef CONFIG_PXA27x | 1029 | i2c_pxa_enable(dev); |
903 | switch (dev->id) { | ||
904 | case 0: | ||
905 | pxa_gpio_mode(GPIO117_I2CSCL_MD); | ||
906 | pxa_gpio_mode(GPIO118_I2CSDA_MD); | ||
907 | break; | ||
908 | case 1: | ||
909 | local_irq_disable(); | ||
910 | PCFR |= PCFR_PI2CEN; | ||
911 | local_irq_enable(); | ||
912 | } | ||
913 | #endif | ||
914 | 1030 | ||
915 | ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED, | 1031 | if (plat) { |
916 | i2c->adap.name, i2c); | 1032 | i2c->adap.class = plat->class; |
917 | if (ret) | 1033 | i2c->use_pio = plat->use_pio; |
918 | goto ereqirq; | 1034 | } |
919 | 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 | } | ||
920 | 1045 | ||
921 | i2c_pxa_reset(i2c); | 1046 | i2c_pxa_reset(i2c); |
922 | 1047 | ||
923 | i2c->adap.algo_data = i2c; | 1048 | i2c->adap.algo_data = i2c; |
924 | i2c->adap.dev.parent = &dev->dev; | 1049 | i2c->adap.dev.parent = &dev->dev; |
925 | 1050 | ||
926 | if (plat) { | ||
927 | i2c->adap.class = plat->class; | ||
928 | } | ||
929 | |||
930 | /* | 1051 | /* |
931 | * If "dev->id" is negative we consider it as zero. | 1052 | * If "dev->id" is negative we consider it as zero. |
932 | * 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 |
@@ -952,17 +1073,11 @@ static int i2c_pxa_probe(struct platform_device *dev) | |||
952 | return 0; | 1073 | return 0; |
953 | 1074 | ||
954 | eadapt: | 1075 | eadapt: |
955 | free_irq(irq, i2c); | 1076 | if (!i2c->use_pio) |
1077 | free_irq(irq, i2c); | ||
956 | ereqirq: | 1078 | ereqirq: |
957 | clk_disable(i2c->clk); | 1079 | clk_disable(i2c->clk); |
958 | 1080 | i2c_pxa_disable(dev); | |
959 | #ifdef CONFIG_PXA27x | ||
960 | if (dev->id == 1) { | ||
961 | local_irq_disable(); | ||
962 | PCFR &= ~PCFR_PI2CEN; | ||
963 | local_irq_enable(); | ||
964 | } | ||
965 | #endif | ||
966 | eremap: | 1081 | eremap: |
967 | clk_put(i2c->clk); | 1082 | clk_put(i2c->clk); |
968 | eclk: | 1083 | eclk: |
@@ -979,18 +1094,12 @@ static int i2c_pxa_remove(struct platform_device *dev) | |||
979 | platform_set_drvdata(dev, NULL); | 1094 | platform_set_drvdata(dev, NULL); |
980 | 1095 | ||
981 | i2c_del_adapter(&i2c->adap); | 1096 | i2c_del_adapter(&i2c->adap); |
982 | free_irq(i2c->irq, i2c); | 1097 | if (!i2c->use_pio) |
1098 | free_irq(i2c->irq, i2c); | ||
983 | 1099 | ||
984 | clk_disable(i2c->clk); | 1100 | clk_disable(i2c->clk); |
985 | clk_put(i2c->clk); | 1101 | clk_put(i2c->clk); |
986 | 1102 | i2c_pxa_disable(dev); | |
987 | #ifdef CONFIG_PXA27x | ||
988 | if (dev->id == 1) { | ||
989 | local_irq_disable(); | ||
990 | PCFR &= ~PCFR_PI2CEN; | ||
991 | local_irq_enable(); | ||
992 | } | ||
993 | #endif | ||
994 | 1103 | ||
995 | release_mem_region(i2c->iobase, i2c->iosize); | 1104 | release_mem_region(i2c->iobase, i2c->iosize); |
996 | kfree(i2c); | 1105 | kfree(i2c); |